FD.io VPP  v19.04.4-rc0-5-ge88582fac
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::mac_age_mode_t bridge_domain::mac_age_mode_t::ON(1, "on");
43 const bridge_domain::mac_age_mode_t bridge_domain::mac_age_mode_t::OFF(0,
44  "off");
45 
46 bridge_domain::mac_age_mode_t::mac_age_mode_t(int v, const std::string& s)
47  : enum_base<bridge_domain::mac_age_mode_t>(v, s)
48 {
49 }
50 
51 const bridge_domain::arp_term_mode_t bridge_domain::arp_term_mode_t::ON(1,
52  "on");
53 const bridge_domain::arp_term_mode_t bridge_domain::arp_term_mode_t::OFF(0,
54  "off");
55 
56 bridge_domain::arp_term_mode_t::arp_term_mode_t(int v, const std::string& s)
57  : enum_base<bridge_domain::arp_term_mode_t>(v, s)
58 {
59 }
60 
61 /**
62  * A DB of al the interfaces, key on the name
63  */
64 singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
65 
66 bridge_domain::event_handler bridge_domain::m_evh;
67 
68 /**
69  * Construct a new object matching the desried state
70  */
72  const learning_mode_t& lmode,
73  const arp_term_mode_t& amode,
74  const flood_mode_t& fmode,
75  const mac_age_mode_t& mmode)
76  : m_id(id)
77  , m_learning_mode(lmode)
78  , m_arp_term_mode(amode)
79  , m_flood_mode(fmode)
80  , m_mac_age_mode(mmode)
81 {
82 }
83 
85  : m_id(o.m_id)
86  , m_learning_mode(o.m_learning_mode)
87  , m_arp_term_mode(o.m_arp_term_mode)
88  , m_flood_mode(o.m_flood_mode)
89  , m_mac_age_mode(o.m_mac_age_mode)
90 {
91 }
92 
95 {
96  return (m_id.data());
97 }
98 
99 uint32_t
101 {
102  return (m_id.data());
103 }
104 
105 bool
107 {
108  return ((m_learning_mode == b.m_learning_mode) &&
109  (m_flood_mode == b.m_flood_mode) &&
110  (m_mac_age_mode == b.m_mac_age_mode) &&
111  (m_arp_term_mode == b.m_arp_term_mode) && id() == b.id());
112 }
113 
114 void
115 bridge_domain::sweep()
116 {
117  if (rc_t::OK == m_id.rc()) {
119  }
120  HW::write();
121 }
122 
123 void
124 bridge_domain::replay()
125 {
126  if (rc_t::OK == m_id.rc()) {
128  m_id, m_learning_mode, m_arp_term_mode, m_flood_mode, m_mac_age_mode));
129  }
130 }
131 
133 {
134  sweep();
135 
136  // not in the DB anymore.
137  m_db.release(m_id.data(), this);
138 }
139 
140 std::string
142 {
143  std::ostringstream s;
144  s << "bridge-domain:[" << m_id.to_string()
145  << " learning-mode:" << m_learning_mode.to_string() << "]";
146 
147  return (s.str());
148 }
149 
150 std::shared_ptr<bridge_domain>
152 {
153  return (m_db.find(key));
154 }
155 
156 void
157 bridge_domain::update(const bridge_domain& desired)
158 {
159  /*
160  * the desired state is always that the interface should be created
161  */
162  if (rc_t::OK != m_id.rc()) {
164  m_id, m_learning_mode, m_arp_term_mode, m_flood_mode, m_mac_age_mode));
165  }
166 }
167 
168 std::shared_ptr<bridge_domain>
169 bridge_domain::find_or_add(const bridge_domain& temp)
170 {
171  return (m_db.find_or_add(temp.m_id.data(), temp));
172 }
173 
174 std::shared_ptr<bridge_domain>
176 {
177  return find_or_add(*this);
178 }
179 
180 void
181 bridge_domain::dump(std::ostream& os)
182 {
183  db_dump(m_db, os);
184 }
185 
186 void
187 bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
188 {
189  /*
190  * dump VPP Bridge domains
191  */
192  std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd =
193  std::make_shared<bridge_domain_cmds::dump_cmd>();
194 
195  HW::enqueue(cmd);
196  HW::write();
197 
198  for (auto& record : *cmd) {
199  auto& payload = record.get_payload();
200 
201  bridge_domain bd(payload.bd_id);
202 
203  VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
204 
205  /*
206  * Write each of the discovered bridge-domains into the OM,
207  * but disable the HW Command q whilst we do, so that no
208  * commands are sent to VPP
209  */
210  OM::commit(key, bd);
211 
212  std::shared_ptr<interface> uu_fwd_itf =
213  interface::find(payload.uu_fwd_sw_if_index);
214  if (uu_fwd_itf) {
215  l2_binding l2(*uu_fwd_itf, bd,
217  OM::commit(key, l2);
218  } else {
219  VOM_LOG(log_level_t::ERROR) << "no uu-fwd interface:"
220  << payload.uu_fwd_sw_if_index;
221  }
222 
223  /**
224  * For each interface in the BD construct an l2_binding
225  */
226  for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
227  std::shared_ptr<interface> itf =
228  interface::find(payload.sw_if_details[ii].sw_if_index);
229  if (itf) {
230  l2_binding l2(*itf, bd);
231  OM::commit(key, l2);
232  } else {
233  VOM_LOG(log_level_t::ERROR) << "no interface:"
234  << payload.sw_if_details[ii].sw_if_index;
235  }
236  }
237  }
238 }
239 
241 {
242  OM::register_listener(this);
243  inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
244 }
245 
246 void
247 bridge_domain::event_handler::handle_replay()
248 {
249  m_db.replay();
250 }
251 
253 bridge_domain::event_handler::order() const
254 {
255  return (dependency_t::TABLE);
256 }
257 
258 void
259 bridge_domain::event_handler::show(std::ostream& os)
260 {
261  db_dump(m_db, os);
262 }
263 }
264 
265 /*
266  * fd.io coding-style-patch-verification: ON
267  *
268  * Local Variables:
269  * eval: (c-set-style "mozilla")
270  * End:
271  */
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:531
std::shared_ptr< bridge_domain > singular() const
Return the matchin &#39;singular&#39; instance of the bridge-domain.
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 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
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: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
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 flood_mode_t &fmode=flood_mode_t::ON, const mac_age_mode_t &mmode=mac_age_mode_t::OFF)
Construct a new object matching the desried state.
uint32_t key_t
Key Type for Bridge Domains in the sigular DB.
static const log_level_t ERROR
Definition: logger.hpp:29
bool operator==(const bridge_domain &b) const
Comparison operator - for UT.
Bridge Domain Learning 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:105
uint32_t id() const
Return the bridge domain&#39;s VPP ID.
A command class that creates an Bridge-Domain.
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
A cmd class that Delete an Bridge-Domain.
~bridge_domain()
Destructor.