FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
gbp_subnet.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "vom/gbp_subnet.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_subnet_cmds.hpp"
20 
21 namespace VOM {
22 
23 gbp_subnet::type_t::type_t(int v, const std::string s)
24  : enum_base<gbp_subnet::type_t>(v, s)
25 {
26 }
27 
28 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_INTERNAL(
29  0,
30  "stitched-internal");
31 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_EXTERNAL(
32  1,
33  "stitched-external");
34 const gbp_subnet::type_t gbp_subnet::type_t::TRANSPORT(2, "transport");
35 const gbp_subnet::type_t gbp_subnet::type_t::L3_OUT(3, "l3-out");
36 
37 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
38 
39 gbp_subnet::event_handler gbp_subnet::m_evh;
40 
42  const route::prefix_t& prefix,
43  const type_t& type)
44  : m_hw(false)
45  , m_rd(rd.singular())
46  , m_prefix(prefix)
47  , m_type(type)
48  , m_recirc(nullptr)
49  , m_epg(nullptr)
50  , m_sclass(~0)
51 {
52 }
53 
55  const route::prefix_t& prefix,
56  const gbp_recirc& recirc,
57  const gbp_endpoint_group& epg)
58  : m_hw(false)
59  , m_rd(rd.singular())
60  , m_prefix(prefix)
61  , m_type(type_t::STITCHED_EXTERNAL)
62  , m_recirc(recirc.singular())
63  , m_epg(epg.singular())
64  , m_sclass(~0)
65 {
66 }
67 
69  const route::prefix_t& prefix,
71  : m_hw(false)
72  , m_rd(rd.singular())
73  , m_prefix(prefix)
74  , m_type(type_t::L3_OUT)
75  , m_recirc(nullptr)
76  , m_epg()
77  , m_sclass(sclass)
78 {
79 }
80 
82  : m_hw(o.m_hw)
83  , m_rd(o.m_rd)
84  , m_prefix(o.m_prefix)
85  , m_type(o.m_type)
86  , m_recirc(o.m_recirc)
87  , m_epg(o.m_epg)
88  , m_sclass(o.m_sclass)
89 {
90 }
91 
93 {
94  sweep();
95  m_db.release(key(), this);
96 }
97 
100 {
101  return (std::make_pair(m_rd->key(), m_prefix));
102 }
103 
104 bool
106 {
107  return ((key() == gs.key()) && (m_type == gs.m_type) &&
108  (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg) &&
109  (m_sclass == gs.m_sclass));
110 }
111 
112 void
113 gbp_subnet::sweep()
114 {
115  if (m_hw) {
116  HW::enqueue(new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->id(), m_prefix));
117  }
118  HW::write();
119 }
120 
121 void
123 {
124  if (m_hw) {
126  m_hw, m_rd->id(), m_prefix, m_type,
127  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
128  (m_epg ? m_epg->sclass() : m_sclass)));
129  }
130 }
131 
132 std::string
134 {
135  std::ostringstream s;
136  s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
137  << m_prefix.to_string();
138  if (m_recirc)
139  s << ", " << m_recirc->to_string();
140  if (m_epg)
141  s << ", " << m_epg->to_string();
142 
143  s << "]";
144 
145  return (s.str());
146 }
147 
148 void
149 gbp_subnet::update(const gbp_subnet& r)
150 {
151  if (rc_t::OK != m_hw.rc()) {
153  m_hw, m_rd->id(), m_prefix, m_type,
154  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
155  (m_epg ? m_epg->sclass() : m_sclass)));
156  } else {
157  if (m_type != r.m_type) {
158  m_epg = r.m_epg;
159  m_recirc = r.m_recirc;
160  m_type = r.m_type;
161 
163  m_hw, m_rd->id(), m_prefix, m_type,
164  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
165  (m_epg ? m_epg->sclass() : m_sclass)));
166  }
167  }
168 }
169 
170 std::shared_ptr<gbp_subnet>
171 gbp_subnet::find_or_add(const gbp_subnet& temp)
172 {
173  return (m_db.find_or_add(temp.key(), temp));
174 }
175 
176 std::shared_ptr<gbp_subnet>
178 {
179  return (m_db.find(k));
180 }
181 
182 std::shared_ptr<gbp_subnet>
184 {
185  return find_or_add(*this);
186 }
187 
188 void
189 gbp_subnet::dump(std::ostream& os)
190 {
191  db_dump(m_db, os);
192 }
193 
195 {
196  OM::register_listener(this);
197  inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
198 }
199 
200 void
201 gbp_subnet::event_handler::handle_replay()
202 {
203  m_db.replay();
204 }
205 
206 void
207 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
208 {
209  std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
210  std::make_shared<gbp_subnet_cmds::dump_cmd>();
211 
212  HW::enqueue(cmd);
213  HW::write();
214 
215  for (auto& record : *cmd) {
216  auto& payload = record.get_payload();
217 
218  route::prefix_t pfx = from_api(payload.subnet.prefix);
219  std::shared_ptr<gbp_route_domain> rd =
220  gbp_route_domain::find(payload.subnet.rd_id);
221 
222  if (rd) {
223  switch (payload.subnet.type) {
225  gbp_subnet gs(*rd, pfx, type_t::TRANSPORT);
226  OM::commit(key, gs);
227  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
228  break;
229  }
232  OM::commit(key, gs);
233  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
234  break;
235  }
236  case GBP_API_SUBNET_L3_OUT: {
237  gbp_subnet gs(*rd, pfx, payload.subnet.sclass);
238  OM::commit(key, gs);
239  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
240  break;
241  }
243  std::shared_ptr<interface> itf =
244  interface::find(payload.subnet.sw_if_index);
245  std::shared_ptr<gbp_endpoint_group> epg =
246  gbp_endpoint_group::find(payload.subnet.sclass);
247 
248  if (itf && epg) {
249  std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
250 
251  if (recirc) {
252  gbp_subnet gs(*rd, pfx, *recirc, *epg);
253  OM::commit(key, gs);
254  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
255  }
256  }
257  }
258  }
259  }
260  }
261 }
262 
264 gbp_subnet::event_handler::order() const
265 {
266  return (dependency_t::ENTRY);
267 }
268 
269 void
270 gbp_subnet::event_handler::show(std::ostream& os)
271 {
272  db_dump(m_db, os);
273 }
274 
275 std::ostream&
276 operator<<(std::ostream& os, const gbp_subnet::key_t& key)
277 {
278  os << "[" << key.first << ", " << key.second << "]";
279 
280  return os;
281 }
282 
283 } // namespace VOM
284 
285 /*
286  * fd.io coding-style-patch-verification: ON
287  *
288  * Local Variables:
289  * eval: (c-set-style "mozilla")
290  * End:
291  */
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
uint16_t sclass_t
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
static std::shared_ptr< interface > find(const handle_t &h)
The the singular instance of the interface in the DB by handle.
Definition: interface.cpp:531
std::shared_ptr< gbp_subnet > singular() const
Return the matching &#39;singular instance&#39;.
Definition: gbp_subnet.cpp:183
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static const type_t TRANSPORT
A transport subnet, sent via the RD&#39;s UU-fwd interface.
Definition: gbp_subnet.hpp:54
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
static const log_level_t DEBUG
Definition: logger.hpp:32
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:268
std::string to_string() const
Convert to string for debugging.
Definition: gbp_subnet.cpp:133
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
void replay(void)
replay the object to create it in hardware
Definition: gbp_subnet.cpp:122
static const type_t STITCHED_INTERNAL
Internal subnet is reachable through the source EPG&#39;s uplink interface.
Definition: gbp_subnet.hpp:44
~gbp_subnet()
Destructor.
Definition: gbp_subnet.cpp:92
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:183
static void dump(std::ostream &os)
Dump all bridge_domain-doamin into the stream provided.
Definition: gbp_subnet.cpp:189
u16 sclass
Definition: gbp.api:118
static std::shared_ptr< gbp_subnet > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
Definition: gbp_subnet.cpp:177
A recirculation interface for GBP use pre/post NAT.
Definition: gbp_recirc.hpp:27
A GBP Enpoint (i.e.
Definition: gbp_subnet.hpp:30
bool operator==(const gbp_subnet &bdae) const
comparison operator
Definition: gbp_subnet.cpp:105
std::pair< gbp_route_domain::key_t, route::prefix_t > key_t
The key for a GBP subnet; table and prefix.
Definition: gbp_subnet.hpp:36
gbp_subnet(const gbp_route_domain &rd, const route::prefix_t &prefix, const type_t &type)
Construct an internal GBP subnet.
Definition: gbp_subnet.cpp:41
const key_t key() const
Return the object&#39;s key.
Definition: gbp_subnet.cpp:99
A cmd class that deletes a GBP subnet.
static const type_t STITCHED_EXTERNAL
External subnet requires NAT translation before egress.
Definition: gbp_subnet.hpp:49
A entry in the ARP termination table of a Bridge Domain.
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
void event_handler(void *tls_async)
Definition: tls_async.c:340
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
A command class that creates or updates the GBP subnet.
static std::shared_ptr< gbp_recirc > find(const key_t &k)
Find the instnace of the recirc interface in the OM.
Definition: gbp_recirc.cpp:121
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
A entry in the ARP termination table of a Route Domain.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
A representation of a method call to VPP.
Definition: cmd.hpp:32
static std::shared_ptr< gbp_route_domain > find(const key_t &k)
Find the instnace of the route_domain domain in the OM.
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
typedef prefix
Definition: ip_types.api:35
Entries in Tables.
static std::shared_ptr< gbp_endpoint_group > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
static const type_t L3_OUT
A transport subnet, sent via the RD&#39;s UU-fwd interface.
Definition: gbp_subnet.hpp:59
A prefix defintion.
Definition: prefix.hpp:92