FD.io VPP  v21.01.1
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()) {
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  */
A Clas representing the binding of an L2 interface to a bridge-domain and the properties of that bind...
Definition: l2_binding.hpp:33
Bridge Domain MAC aging mode.
#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.
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:538
std::shared_ptr< bridge_domain > singular() const
Return the matchin &#39;singular&#39; instance of the bridge-domain.
const char *const string
Definition: cJSON.h:172
static const arp_term_mode_t OFF
static const flood_mode_t OFF
static const learning_mode_t ON
static const arp_term_mode_t ON
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 rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
static const uu_flood_mode_t OFF
static const log_level_t DEBUG
Definition: logger.hpp:32
static std::shared_ptr< bridge_domain > find(const key_t &key)
Static function to find the bridge_domain in the model.
static void dump(std::ostream &os)
Dump all bridge-doamin into the stream provided.
static const flood_mode_t ON
T & data()
Return the data read/written.
Definition: hw.hpp:109
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
const cJSON *const b
Definition: cJSON.h:255
Bridge Domain Unknown Unicast Flood mode.
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.
static const learning_mode_t OFF
enum_base(int value, const std::string desc)
Constructor of an enum - takes value and string description.
Definition: enum_base.hpp:94
static const l2_port_type_t L2_PORT_TYPE_UU_FWD
Definition: l2_binding.hpp:48
static const mac_age_mode_t ON
std::string to_string(void) const
convert to string format for debug purposes
const key_t & key() const
Return the bridge domain&#39;s key.
static const mac_age_mode_t OFF
Bridge Domain ARP termination mode.
Bridge Domain Learning mode.
A base class for all object_base in the VPP object_base-Model.
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
Tables in which entries are added, e.g bridge/route-domains.
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:334
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
uint32_t key_t
Key Type for Bridge Domains in the sigular DB.
Bridge Domain ARP Unicast Forward mode.
static const log_level_t ERROR
Definition: logger.hpp:29
bool operator==(const bridge_domain &b) const
Comparison operator - for UT.
static const arp_ufwd_mode_t OFF
Bridge Domain flood mode.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
uint32_t id() const
Return the bridge domain&#39;s VPP ID.
A command class that creates an Bridge-Domain.
static const uu_flood_mode_t ON
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
A cmd class that Delete an Bridge-Domain.
static const arp_ufwd_mode_t ON
~bridge_domain()
Destructor.