FD.io VPP  v18.04-17-g3a0d853
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 /**
35  * A DB of al the interfaces, key on the name
36  */
37 singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
38 
39 bridge_domain::event_handler bridge_domain::m_evh;
40 
41 /**
42  * Construct a new object matching the desried state
43  */
45  : m_id(id)
46  , m_learning_mode(lmode)
47 {
48 }
49 
51  : m_id(o.m_id)
52  , m_learning_mode(o.m_learning_mode)
53 {
54 }
55 
58 {
59  return (m_id.data());
60 }
61 
62 uint32_t
64 {
65  return (m_id.data());
66 }
67 
68 bool
70 {
71  return ((m_learning_mode == b.m_learning_mode) && id() == b.id());
72 }
73 
74 void
75 bridge_domain::sweep()
76 {
77  if (rc_t::OK == m_id.rc()) {
79  }
80  HW::write();
81 }
82 
83 void
84 bridge_domain::replay()
85 {
86  if (rc_t::OK == m_id.rc()) {
87  HW::enqueue(new bridge_domain_cmds::create_cmd(m_id, m_learning_mode));
88  }
89 }
90 
92 {
93  sweep();
94 
95  // not in the DB anymore.
96  m_db.release(m_id.data(), this);
97 }
98 
99 std::string
101 {
102  std::ostringstream s;
103  s << "bridge-domain:[" << m_id.to_string()
104  << " learning-mode:" << m_learning_mode.to_string() << "]";
105 
106  return (s.str());
107 }
108 
109 std::shared_ptr<bridge_domain>
111 {
112  return (m_db.find(key));
113 }
114 
115 void
116 bridge_domain::update(const bridge_domain& desired)
117 {
118  /*
119  * the desired state is always that the interface should be created
120  */
121  if (rc_t::OK != m_id.rc()) {
122  HW::enqueue(new bridge_domain_cmds::create_cmd(m_id, m_learning_mode));
123  }
124 }
125 
126 std::shared_ptr<bridge_domain>
127 bridge_domain::find_or_add(const bridge_domain& temp)
128 {
129  return (m_db.find_or_add(temp.m_id.data(), temp));
130 }
131 
132 std::shared_ptr<bridge_domain>
134 {
135  return find_or_add(*this);
136 }
137 
138 void
139 bridge_domain::dump(std::ostream& os)
140 {
141  db_dump(m_db, os);
142 }
143 
144 void
145 bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
146 {
147  /*
148  * dump VPP Bridge domains
149  */
150  std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd =
151  std::make_shared<bridge_domain_cmds::dump_cmd>();
152 
153  HW::enqueue(cmd);
154  HW::write();
155 
156  for (auto& record : *cmd) {
157  auto& payload = record.get_payload();
158 
159  bridge_domain bd(payload.bd_id);
160 
161  VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
162 
163  /*
164  * Write each of the discovered bridge-domains into the OM,
165  * but disable the HW Command q whilst we do, so that no
166  * commands are sent to VPP
167  */
168  OM::commit(key, bd);
169 
170  /**
171  * For each interface in the BD construct an l2_binding
172  */
173  for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
174  std::shared_ptr<interface> itf =
175  interface::find(payload.sw_if_details[ii].sw_if_index);
176  l2_binding l2(*itf, bd);
177  OM::commit(key, l2);
178  }
179  }
180 }
181 
182 bridge_domain::event_handler::event_handler()
183 {
184  OM::register_listener(this);
185  inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
186 }
187 
188 void
189 bridge_domain::event_handler::handle_replay()
190 {
191  m_db.replay();
192 }
193 
195 bridge_domain::event_handler::order() const
196 {
197  return (dependency_t::TABLE);
198 }
199 
200 void
201 bridge_domain::event_handler::show(std::ostream& os)
202 {
203  db_dump(m_db, os);
204 }
205 }
206 
207 /*
208  * fd.io coding-style-patch-verification: ON
209  *
210  * Local Variables:
211  * eval: (c-set-style "mozilla")
212  * End:
213  */
A Clas representing the binding of an L2 interface to a bridge-domain and the properties of that bind...
Definition: l2_binding.hpp:32
#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:465
static const learning_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:236
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.
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
T & data()
Return the data read/written.
Definition: hw.hpp:108
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:118
#define v
Definition: acl.c:495
static const learning_mode_t OFF
std::string to_string(void) const
convert to string format for debug purposes
uint32_t id() const
Return the bridge domain&#39;s VPP ID.
const key_t & key() const
Return the bridge domain&#39;s key.
Bridge Domain Learning mode.
A base class for all object_base in the VPP object_base-Model.
bool operator==(const bridge_domain &b) const
Comparison operator - for UT.
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
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:112
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
uint32_t key_t
Key Type for Bridge Domains in the sigular DB.
std::shared_ptr< bridge_domain > singular() const
Return the matchin &#39;singular&#39; instance of the bridge-domain.
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
A command class that creates an Bridge-Domain.
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
bridge_domain(uint32_t id, const learning_mode_t &lmode=learning_mode_t::ON)
Construct a new object matching the desried state.
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.