FD.io VPP  v21.10.1-2-g0a485f517
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 const gbp_subnet::type_t gbp_subnet::type_t::ANON_L3_OUT(4, "anon-l3-out");
37 
38 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
39 
40 gbp_subnet::event_handler gbp_subnet::m_evh;
41 
43  const route::prefix_t& prefix,
44  const type_t& type)
45  : m_hw(false)
46  , m_rd(rd.singular())
47  , m_prefix(prefix)
48  , m_type(type)
49  , m_recirc(nullptr)
50  , m_epg(nullptr)
51  , m_sclass(~0)
52 {
53 }
54 
56  const route::prefix_t& prefix,
57  const gbp_recirc& recirc,
58  const gbp_endpoint_group& epg)
59  : m_hw(false)
60  , m_rd(rd.singular())
61  , m_prefix(prefix)
62  , m_type(type_t::STITCHED_EXTERNAL)
63  , m_recirc(recirc.singular())
64  , m_epg(epg.singular())
65  , m_sclass(~0)
66 {
67 }
68 
70  const route::prefix_t& prefix,
72  const type_t& type)
73  : m_hw(false)
74  , m_rd(rd.singular())
75  , m_prefix(prefix)
76  , m_type(type)
77  , m_recirc(nullptr)
78  , m_epg()
79  , m_sclass(sclass)
80 {
81 }
82 
84  : m_hw(o.m_hw)
85  , m_rd(o.m_rd)
86  , m_prefix(o.m_prefix)
87  , m_type(o.m_type)
88  , m_recirc(o.m_recirc)
89  , m_epg(o.m_epg)
90  , m_sclass(o.m_sclass)
91 {
92 }
93 
95 {
96  sweep();
97  m_db.release(key(), this);
98 }
99 
100 const gbp_subnet::key_t
102 {
103  return (std::make_pair(m_rd->key(), m_prefix));
104 }
105 
106 bool
108 {
109  return ((key() == gs.key()) && (m_type == gs.m_type) &&
110  (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg) &&
111  (m_sclass == gs.m_sclass));
112 }
113 
114 void
115 gbp_subnet::sweep()
116 {
117  if (m_hw) {
118  HW::enqueue(new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->id(), m_prefix));
119  }
120  HW::write();
121 }
122 
123 void
125 {
126  if (m_hw) {
128  m_hw, m_rd->id(), m_prefix, m_type,
129  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
130  (m_epg ? m_epg->sclass() : m_sclass)));
131  }
132 }
133 
136 {
137  std::ostringstream s;
138  s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
139  << m_prefix.to_string();
140  if (m_recirc)
141  s << ", " << m_recirc->to_string();
142  if (m_epg)
143  s << ", " << m_epg->to_string();
144 
145  s << "]";
146 
147  return (s.str());
148 }
149 
150 void
151 gbp_subnet::update(const gbp_subnet& r)
152 {
153  if (rc_t::OK != m_hw.rc()) {
155  m_hw, m_rd->id(), m_prefix, m_type,
156  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
157  (m_epg ? m_epg->sclass() : m_sclass)));
158  } else {
159  if (m_type != r.m_type) {
160  m_epg = r.m_epg;
161  m_recirc = r.m_recirc;
162  m_type = r.m_type;
163 
164  HW::enqueue(new gbp_subnet_cmds::create_cmd(
165  m_hw, m_rd->id(), m_prefix, m_type,
166  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
167  (m_epg ? m_epg->sclass() : m_sclass)));
168  }
169  }
170 }
171 
172 std::shared_ptr<gbp_subnet>
173 gbp_subnet::find_or_add(const gbp_subnet& temp)
174 {
175  return (m_db.find_or_add(temp.key(), temp));
176 }
177 
178 std::shared_ptr<gbp_subnet>
180 {
181  return (m_db.find(k));
182 }
183 
184 std::shared_ptr<gbp_subnet>
186 {
187  return find_or_add(*this);
188 }
189 
190 void
191 gbp_subnet::dump(std::ostream& os)
192 {
193  db_dump(m_db, os);
194 }
195 
197 {
198  OM::register_listener(this);
199  inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
200 }
201 
202 void
203 gbp_subnet::event_handler::handle_replay()
204 {
205  m_db.replay();
206 }
207 
208 void
209 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
210 {
211  std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
212  std::make_shared<gbp_subnet_cmds::dump_cmd>();
213 
214  HW::enqueue(cmd);
215  HW::write();
216 
217  for (auto& record : *cmd) {
218  auto& payload = record.get_payload();
219 
220  route::prefix_t pfx = from_api(payload.subnet.prefix);
221  std::shared_ptr<gbp_route_domain> rd =
222  gbp_route_domain::find(payload.subnet.rd_id);
223 
224  if (rd) {
225  switch (payload.subnet.type) {
227  gbp_subnet gs(*rd, pfx, type_t::TRANSPORT);
228  OM::commit(key, gs);
229  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
230  break;
231  }
233  gbp_subnet gs(*rd, pfx, type_t::STITCHED_INTERNAL);
234  OM::commit(key, gs);
235  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
236  break;
237  }
238  case GBP_API_SUBNET_L3_OUT: {
239  gbp_subnet gs(*rd, pfx, payload.subnet.sclass);
240  OM::commit(key, gs);
241  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
242  break;
243  }
245  gbp_subnet gs(*rd, pfx, payload.subnet.sclass, type_t::ANON_L3_OUT);
246  OM::commit(key, gs);
247  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
248  break;
249  }
251  std::shared_ptr<interface> itf =
252  interface::find(payload.subnet.sw_if_index);
253  std::shared_ptr<gbp_endpoint_group> epg =
254  gbp_endpoint_group::find(payload.subnet.sclass);
255 
256  if (itf && epg) {
257  std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
258 
259  if (recirc) {
260  gbp_subnet gs(*rd, pfx, *recirc, *epg);
261  OM::commit(key, gs);
262  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
263  }
264  }
265  }
266  }
267  }
268  }
269 }
270 
272 gbp_subnet::event_handler::order() const
273 {
274  return (dependency_t::ENTRY);
275 }
276 
277 void
278 gbp_subnet::event_handler::show(std::ostream& os)
279 {
280  db_dump(m_db, os);
281 }
282 
283 std::ostream&
284 operator<<(std::ostream& os, const gbp_subnet::key_t& key)
285 {
286  os << "[" << key.first << ", " << key.second << "]";
287 
288  return os;
289 }
290 
291 } // namespace VOM
292 
293 /*
294  * fd.io coding-style-patch-verification: OFF
295  *
296  * Local Variables:
297  * eval: (c-set-style "mozilla")
298  * End:
299  */
VOM::route::prefix_t
A prefix defintion.
Definition: prefix.hpp:131
GBP_API_SUBNET_L3_OUT
@ GBP_API_SUBNET_L3_OUT
Definition: gbp.api:258
VOM::HW::write
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
VOM::OM::commit
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
VOM::gbp_subnet::type_t::STITCHED_INTERNAL
const static type_t STITCHED_INTERNAL
Internal subnet is reachable through the source EPG's uplink interface.
Definition: gbp_subnet.hpp:44
VOM::gbp_subnet::gbp_subnet
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:42
GBP_API_SUBNET_ANON_L3_OUT
@ GBP_API_SUBNET_ANON_L3_OUT
Definition: gbp.api:259
VOM_LOG
#define VOM_LOG(lvl)
Definition: logger.hpp:181
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::inspect::register_handler
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
VOM::dependency_t
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
VOM::rc_t::OK
const static rc_t OK
The HW write was successfull.
Definition: types.hpp:109
VOM::gbp_subnet::type_t::ANON_L3_OUT
const static type_t ANON_L3_OUT
An anonymous L3-out subnet.
Definition: gbp_subnet.hpp:64
VOM::gbp_subnet::find
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:179
VOM::gbp_subnet::operator==
bool operator==(const gbp_subnet &bdae) const
comparison operator
Definition: gbp_subnet.cpp:107
VOM::gbp_subnet
A GBP Enpoint (i.e.
Definition: gbp_subnet.hpp:30
VOM::dependency_t::ENTRY
@ ENTRY
Entries in Tables.
VOM::sclass_t
uint16_t sclass_t
Definition: gbp_types.hpp:27
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1089
gbp_subnet.hpp
GBP_API_SUBNET_STITCHED_INTERNAL
@ GBP_API_SUBNET_STITCHED_INTERNAL
Definition: gbp.api:256
key
typedef key
Definition: ipsec_types.api:91
VOM::db_dump
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
Definition: singular_db_funcs.hpp:35
VOM::gbp_subnet::to_string
std::string to_string() const
Convert to string for debugging.
Definition: gbp_subnet.cpp:135
VOM::route::prefix_t::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:214
VOM::OM::register_listener
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
VOM::gbp_endpoint_group::find
static std::shared_ptr< gbp_endpoint_group > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
Definition: gbp_endpoint_group.cpp:188
gbp_subnet
typedef gbp_subnet
Definition: gbp.api:263
VOM::gbp_recirc
A recirculation interface for GBP use pre/post NAT.
Definition: gbp_recirc.hpp:27
VOM::gbp_subnet_cmds::delete_cmd
A cmd class that deletes a GBP subnet.
Definition: gbp_subnet_cmds.hpp:69
VOM::gbp_subnet::replay
void replay(void)
replay the object to create it in hardware
Definition: gbp_subnet.cpp:124
VOM::client_db::key_t
const typedef 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
VOM::HW::enqueue
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
sclass
u16 sclass
Definition: gbp.api:131
VOM::log_level_t::DEBUG
const static log_level_t DEBUG
Definition: logger.hpp:32
VOM::operator<<
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
Definition: acl_binding.cpp:201
GBP_API_SUBNET_STITCHED_EXTERNAL
@ GBP_API_SUBNET_STITCHED_EXTERNAL
Definition: gbp.api:257
VOM::gbp_subnet_cmds::create_cmd
A command class that creates or updates the GBP subnet.
Definition: gbp_subnet_cmds.hpp:30
false
#define false
Definition: cJSON.c:70
singular_db_funcs.hpp
VOM::gbp_subnet::type_t::STITCHED_EXTERNAL
const static type_t STITCHED_EXTERNAL
External subnet requires NAT translation before egress.
Definition: gbp_subnet.hpp:49
VOM::enum_base::to_string
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
VOM::HW::item::rc
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
VOM::gbp_subnet::dump
static void dump(std::ostream &os)
Dump all bridge_domain-doamin into the stream provided.
Definition: gbp_subnet.cpp:191
VOM::gbp_subnet::type_t::TRANSPORT
const static type_t TRANSPORT
A transport subnet, sent via the RD's UU-fwd interface.
Definition: gbp_subnet.hpp:54
VOM::gbp_subnet::singular
std::shared_ptr< gbp_subnet > singular() const
Return the matching 'singular instance'.
Definition: gbp_subnet.cpp:185
VOM::gbp_route_domain
A entry in the ARP termination table of a Route Domain.
Definition: gbp_route_domain.hpp:30
VOM::gbp_subnet::~gbp_subnet
~gbp_subnet()
Destructor.
Definition: gbp_subnet.cpp:94
VOM::handle_t::INVALID
const static handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:268
VOM::gbp_subnet::type_t::L3_OUT
const static type_t L3_OUT
A L3-out subnet.
Definition: gbp_subnet.hpp:59
VOM::gbp_subnet::key
const key_t key() const
Return the object's key.
Definition: gbp_subnet.cpp:101
VOM::gbp_subnet::key_t
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
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
VOM::gbp_subnet::type_t
Definition: gbp_subnet.hpp:38
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
VOM::gbp_endpoint_group
A entry in the ARP termination table of a Bridge Domain.
Definition: gbp_endpoint_group.hpp:32
gbp_subnet_cmds.hpp
VOM::from_api
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
api_types.hpp
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
GBP_API_SUBNET_TRANSPORT
@ GBP_API_SUBNET_TRANSPORT
Definition: gbp.api:255
VOM::gbp_recirc::find
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
prefix
vl_api_prefix_t prefix
Definition: ip.api:175
VOM::interface::find
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:538
VOM::gbp_route_domain::find
static std::shared_ptr< gbp_route_domain > find(const key_t &k)
Find the instnace of the route_domain domain in the OM.
Definition: gbp_route_domain.cpp:179
string
const char *const string
Definition: cJSON.h:172