FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
bridge_domain.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/bridge_domain.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/l2_binding.hpp"
21 
22 namespace VOM {
23 
24 const bridge_domain::learning_mode_t bridge_domain::learning_mode_t::ON(1,
25  "on");
26 const bridge_domain::learning_mode_t bridge_domain::learning_mode_t::OFF(0,
27  "off");
28 
29 bridge_domain::learning_mode_t::learning_mode_t(int v, const std::string& s)
30  : enum_base<bridge_domain::learning_mode_t>(v, s)
31 {
32 }
33 
34 const bridge_domain::flood_mode_t bridge_domain::flood_mode_t::ON(1, "on");
35 const bridge_domain::flood_mode_t bridge_domain::flood_mode_t::OFF(0, "off");
36 
37 bridge_domain::flood_mode_t::flood_mode_t(int v, const std::string& s)
38  : enum_base<bridge_domain::flood_mode_t>(v, s)
39 {
40 }
41 
42 const bridge_domain::uu_flood_mode_t bridge_domain::uu_flood_mode_t::ON(1,
43  "on");
44 const bridge_domain::uu_flood_mode_t bridge_domain::uu_flood_mode_t::OFF(0,
45  "off");
46 
47 bridge_domain::uu_flood_mode_t::uu_flood_mode_t(int v, const std::string& s)
48  : enum_base<bridge_domain::uu_flood_mode_t>(v, s)
49 {
50 }
51 
52 const bridge_domain::mac_age_mode_t bridge_domain::mac_age_mode_t::ON(1, "on");
53 const bridge_domain::mac_age_mode_t bridge_domain::mac_age_mode_t::OFF(0,
54  "off");
55 
56 bridge_domain::mac_age_mode_t::mac_age_mode_t(int v, const std::string& s)
57  : enum_base<bridge_domain::mac_age_mode_t>(v, s)
58 {
59 }
60 
61 const bridge_domain::arp_term_mode_t bridge_domain::arp_term_mode_t::ON(1,
62  "on");
63 const bridge_domain::arp_term_mode_t bridge_domain::arp_term_mode_t::OFF(0,
64  "off");
65 
66 bridge_domain::arp_term_mode_t::arp_term_mode_t(int v, const std::string& s)
67  : enum_base<bridge_domain::arp_term_mode_t>(v, s)
68 {
69 }
70 
71 const bridge_domain::arp_ufwd_mode_t bridge_domain::arp_ufwd_mode_t::ON(1,
72  "on");
73 const bridge_domain::arp_ufwd_mode_t bridge_domain::arp_ufwd_mode_t::OFF(0,
74  "off");
75 
76 bridge_domain::arp_ufwd_mode_t::arp_ufwd_mode_t(int v, const std::string& s)
77  : enum_base<bridge_domain::arp_ufwd_mode_t>(v, s)
78 {
79 }
80 
81 /**
82  * A DB of al the interfaces, key on the name
83  */
84 singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
85 
86 bridge_domain::event_handler bridge_domain::m_evh;
87 
88 /**
89  * Construct a new object matching the desried state
90  */
92  const learning_mode_t& lmode,
93  const arp_term_mode_t& amode,
94  const arp_ufwd_mode_t& aumode,
95  const flood_mode_t& fmode,
96  const uu_flood_mode_t& uufmode,
97  const mac_age_mode_t& mmode)
98  : m_id(id)
99  , m_learning_mode(lmode)
100  , m_arp_term_mode(amode)
101  , m_arp_ufwd_mode(aumode)
102  , m_flood_mode(fmode)
103  , m_uu_flood_mode(uufmode)
104  , m_mac_age_mode(mmode)
105 {
106 }
107 
109  : m_id(o.m_id)
110  , m_learning_mode(o.m_learning_mode)
111  , m_arp_term_mode(o.m_arp_term_mode)
112  , m_arp_ufwd_mode(o.m_arp_ufwd_mode)
113  , m_flood_mode(o.m_flood_mode)
114  , m_uu_flood_mode(o.m_uu_flood_mode)
115  , m_mac_age_mode(o.m_mac_age_mode)
116 {
117 }
118 
121 {
122  return (m_id.data());
123 }
124 
125 uint32_t
127 {
128  return (m_id.data());
129 }
130 
131 bool
133 {
134  return ((m_learning_mode == b.m_learning_mode) &&
135  (m_flood_mode == b.m_flood_mode) &&
136  (m_uu_flood_mode == b.m_uu_flood_mode) &&
137  (m_mac_age_mode == b.m_mac_age_mode) &&
138  (m_arp_term_mode == b.m_arp_term_mode) &&
139  (m_arp_ufwd_mode == b.m_arp_ufwd_mode) && id() == b.id());
140 }
141 
142 void
143 bridge_domain::sweep()
144 {
145  if (rc_t::OK == m_id.rc()) {
147  }
148  HW::write();
149 }
150 
151 void
152 bridge_domain::replay()
153 {
154  if (rc_t::OK == m_id.rc()) {
155  HW::enqueue(new bridge_domain_cmds::create_cmd(
156  m_id, m_learning_mode, m_arp_term_mode, m_arp_ufwd_mode, m_flood_mode,
157  m_uu_flood_mode, m_mac_age_mode));
158  }
159 }
160 
162 {
163  sweep();
164 
165  // not in the DB anymore.
166  m_db.release(m_id.data(), this);
167 }
168 
171 {
172  std::ostringstream s;
173  s << "bridge-domain:[" << m_id.to_string()
174  << " learning-mode:" << m_learning_mode.to_string() << "]";
175 
176  return (s.str());
177 }
178 
179 std::shared_ptr<bridge_domain>
181 {
182  return (m_db.find(key));
183 }
184 
185 void
186 bridge_domain::update(const bridge_domain& desired)
187 {
188  /*
189  * the desired state is always that the interface should be created
190  */
191  if (rc_t::OK != m_id.rc()) {
193  m_id, m_learning_mode, m_arp_term_mode, m_arp_ufwd_mode, m_flood_mode,
194  m_uu_flood_mode, m_mac_age_mode));
195  }
196 }
197 
198 std::shared_ptr<bridge_domain>
199 bridge_domain::find_or_add(const bridge_domain& temp)
200 {
201  return (m_db.find_or_add(temp.m_id.data(), temp));
202 }
203 
204 std::shared_ptr<bridge_domain>
206 {
207  return find_or_add(*this);
208 }
209 
210 void
211 bridge_domain::dump(std::ostream& os)
212 {
213  db_dump(m_db, os);
214 }
215 
216 void
217 bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
218 {
219  /*
220  * dump VPP Bridge domains
221  */
222  std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd =
223  std::make_shared<bridge_domain_cmds::dump_cmd>();
224 
225  HW::enqueue(cmd);
226  HW::write();
227 
228  for (auto& record : *cmd) {
229  auto& payload = record.get_payload();
230 
231  bridge_domain bd(
232  payload.bd_id,
233  (payload.learn ? learning_mode_t::ON : learning_mode_t::OFF),
234  (payload.arp_term ? arp_term_mode_t::ON : arp_term_mode_t::OFF),
235  (payload.arp_ufwd ? arp_ufwd_mode_t::ON : arp_ufwd_mode_t::OFF),
236  (payload.flood ? flood_mode_t::ON : flood_mode_t::OFF),
237  (payload.uu_flood ? uu_flood_mode_t::ON : uu_flood_mode_t::OFF),
238  (payload.mac_age ? mac_age_mode_t::ON : mac_age_mode_t::OFF));
239 
240  VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
241 
242  /*
243  * Write each of the discovered bridge-domains into the OM,
244  * but disable the HW Command q whilst we do, so that no
245  * commands are sent to VPP
246  */
247  OM::commit(key, bd);
248 
249  std::shared_ptr<interface> uu_fwd_itf =
250  interface::find(payload.uu_fwd_sw_if_index);
251  if (uu_fwd_itf) {
252  l2_binding l2(*uu_fwd_itf, bd,
254  OM::commit(key, l2);
255  } else {
256  VOM_LOG(log_level_t::ERROR) << "no uu-fwd interface:"
257  << payload.uu_fwd_sw_if_index;
258  }
259 
260  /**
261  * For each interface in the BD construct an l2_binding
262  */
263  for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
264  std::shared_ptr<interface> itf =
265  interface::find(payload.sw_if_details[ii].sw_if_index);
266  if (itf) {
267  l2_binding l2(*itf, bd);
268  OM::commit(key, l2);
269  } else {
270  VOM_LOG(log_level_t::ERROR) << "no interface:"
271  << payload.sw_if_details[ii].sw_if_index;
272  }
273  }
274  }
275 }
276 
278 {
279  OM::register_listener(this);
280  inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
281 }
282 
283 void
284 bridge_domain::event_handler::handle_replay()
285 {
286  m_db.replay();
287 }
288 
290 bridge_domain::event_handler::order() const
291 {
292  return (dependency_t::TABLE);
293 }
294 
295 void
296 bridge_domain::event_handler::show(std::ostream& os)
297 {
298  db_dump(m_db, os);
299 }
300 }
301 
302 /*
303  * fd.io coding-style-patch-verification: OFF
304  *
305  * Local Variables:
306  * eval: (c-set-style "mozilla")
307  * End:
308  */
VOM::bridge_domain
A base class for all object_base in the VPP object_base-Model.
Definition: bridge_domain.hpp:31
VOM::bridge_domain::flood_mode_t::OFF
const static flood_mode_t OFF
Definition: bridge_domain.hpp:105
VOM::bridge_domain::arp_term_mode_t
Bridge Domain ARP termination mode.
Definition: bridge_domain.hpp:57
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::bridge_domain::singular
std::shared_ptr< bridge_domain > singular() const
Return the matchin 'singular' instance of the bridge-domain.
Definition: bridge_domain.cpp:205
VOM::bridge_domain::arp_ufwd_mode_t
Bridge Domain ARP Unicast Forward mode.
Definition: bridge_domain.hpp:72
VOM_LOG
#define VOM_LOG(lvl)
Definition: logger.hpp:181
VOM::bridge_domain::dump
static void dump(std::ostream &os)
Dump all bridge-doamin into the stream provided.
Definition: bridge_domain.cpp:211
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::bridge_domain::flood_mode_t
Bridge Domain flood mode.
Definition: bridge_domain.hpp:102
VOM::bridge_domain_cmds::delete_cmd
A cmd class that Delete an Bridge-Domain.
Definition: bridge_domain_cmds.hpp:89
VOM::bridge_domain::learning_mode_t::OFF
const static learning_mode_t OFF
Definition: bridge_domain.hpp:45
bridge_domain_cmds.hpp
VOM::bridge_domain::mac_age_mode_t::ON
const static mac_age_mode_t ON
Definition: bridge_domain.hpp:89
VOM::bridge_domain::~bridge_domain
~bridge_domain()
Destructor.
Definition: bridge_domain.cpp:161
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::OM::register_listener
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
VOM::bridge_domain::find
static std::shared_ptr< bridge_domain > find(const key_t &key)
Static function to find the bridge_domain in the model.
Definition: bridge_domain.cpp:180
VOM::ACL::l2_binding
binding< l2_list > l2_binding
Typedef the L2 binding type.
Definition: acl_binding.hpp:215
VOM::bridge_domain::uu_flood_mode_t::OFF
const static uu_flood_mode_t OFF
Definition: bridge_domain.hpp:120
VOM::bridge_domain::arp_ufwd_mode_t::ON
const static arp_ufwd_mode_t ON
Definition: bridge_domain.hpp:74
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
VOM::HW::item::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
VOM::log_level_t::DEBUG
const static log_level_t DEBUG
Definition: logger.hpp:32
VOM::dependency_t::TABLE
@ TABLE
Tables in which entries are added, e.g bridge/route-domains.
VOM::bridge_domain::arp_ufwd_mode_t::OFF
const static arp_ufwd_mode_t OFF
Definition: bridge_domain.hpp:75
singular_db_funcs.hpp
id
u8 id[64]
Definition: dhcp.api:160
VOM::bridge_domain::arp_term_mode_t::OFF
const static arp_term_mode_t OFF
Definition: bridge_domain.hpp:60
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::HW::item::data
T & data()
Return the data read/written.
Definition: hw.hpp:109
VOM::bridge_domain::to_string
std::string to_string(void) const
convert to string format for debug purposes
Definition: bridge_domain.cpp:170
bridge_domain.hpp
VOM::bridge_domain::arp_term_mode_t::ON
const static arp_term_mode_t ON
Definition: bridge_domain.hpp:59
VOM::bridge_domain::flood_mode_t::ON
const static flood_mode_t ON
Definition: bridge_domain.hpp:104
VOM::bridge_domain::bridge_domain
bridge_domain(uint32_t id, const learning_mode_t &lmode=learning_mode_t::ON, const arp_term_mode_t &amode=arp_term_mode_t::ON, const arp_ufwd_mode_t &aumode=arp_ufwd_mode_t::OFF, const flood_mode_t &fmode=flood_mode_t::ON, const uu_flood_mode_t &uufmode=uu_flood_mode_t::ON, const mac_age_mode_t &mmode=mac_age_mode_t::OFF)
Construct a new object matching the desried state.
Definition: bridge_domain.cpp:91
VOM::bridge_domain::uu_flood_mode_t::ON
const static uu_flood_mode_t ON
Definition: bridge_domain.hpp:119
interface.hpp
VOM::bridge_domain::key
const key_t & key() const
Return the bridge domain's key.
Definition: bridge_domain.cpp:120
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
l2_binding.hpp
VOM::bridge_domain::learning_mode_t
Bridge Domain Learning mode.
Definition: bridge_domain.hpp:42
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
VOM::bridge_domain::uu_flood_mode_t
Bridge Domain Unknown Unicast Flood mode.
Definition: bridge_domain.hpp:117
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
VOM::bridge_domain::operator==
bool operator==(const bridge_domain &b) const
Comparison operator - for UT.
Definition: bridge_domain.cpp:132
VOM::bridge_domain::key_t
uint32_t key_t
Key Type for Bridge Domains in the sigular DB.
Definition: bridge_domain.hpp:37
VOM::l2_binding::l2_port_type_t::L2_PORT_TYPE_UU_FWD
const static l2_port_type_t L2_PORT_TYPE_UU_FWD
Definition: l2_binding.hpp:48
VOM::bridge_domain::learning_mode_t::ON
const static learning_mode_t ON
Definition: bridge_domain.hpp:44
VOM::cmd
A representation of a method call to VPP.
Definition: cmd.hpp:32
VOM::bridge_domain_cmds::create_cmd
A command class that creates an Bridge-Domain.
Definition: bridge_domain_cmds.hpp:30
VOM::bridge_domain::id
uint32_t id() const
Return the bridge domain's VPP ID.
Definition: bridge_domain.cpp:126
VOM::log_level_t::ERROR
const static log_level_t ERROR
Definition: logger.hpp:29
VOM::bridge_domain::mac_age_mode_t::OFF
const static mac_age_mode_t OFF
Definition: bridge_domain.hpp:90
VOM::bridge_domain::mac_age_mode_t
Bridge Domain MAC aging mode.
Definition: bridge_domain.hpp:87
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::l2_binding
A Clas representing the binding of an L2 interface to a bridge-domain and the properties of that bind...
Definition: l2_binding.hpp:33
string
const char *const string
Definition: cJSON.h:172