FD.io VPP  v18.10-32-g1161dda
Vector Packet Processing
l2_binding.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/l2_binding.hpp"
17 #include "vom/l2_binding_cmds.hpp"
19 
20 namespace VOM {
21 /**
22  * A DB of all the L2 Configs
23  */
24 singular_db<l2_binding::key_t, l2_binding> l2_binding::m_db;
25 
26 l2_binding::event_handler l2_binding::m_evh;
27 
28 /*
29  * Make sure these are in sync with the smae enum in VPP
30  */
32  0,
33  "disabled");
35  "push-1");
37  "push-2");
41  5,
42  "translate-1-1");
44  6,
45  "translate-1-2");
47  7,
48  "translate-2-1");
50  5,
51  "translate-2-2");
52 
53 l2_binding::l2_vtr_op_t::l2_vtr_op_t(int v, const std::string s)
54  : enum_base<l2_binding::l2_vtr_op_t>(v, s)
55 {
56 }
57 
58 const l2_binding::l2_port_type_t
60 const l2_binding::l2_port_type_t l2_binding::l2_port_type_t::L2_PORT_TYPE_BVI(
61  1,
62  "bvi");
63 const l2_binding::l2_port_type_t
65 
66 l2_binding::l2_port_type_t::l2_port_type_t(int v, const std::string s)
67  : enum_base<l2_binding::l2_port_type_t>(v, s)
68 {
69 }
70 
71 /**
72  * Construct a new object matching the desried state
73  */
75  : m_itf(itf.singular())
76  , m_bd(bd.singular())
77  , m_port_type(l2_port_type_t::L2_PORT_TYPE_NORMAL)
78  , m_binding(0)
79  , m_vtr_op(l2_vtr_op_t::L2_VTR_DISABLED, rc_t::UNSET)
80  , m_vtr_op_tag(0)
81 {
82  if (interface::type_t::BVI == m_itf->type())
84 }
85 
86 /**
87  * Construct a new object matching the desried state
88  */
90  const bridge_domain& bd,
91  const l2_port_type_t& port_type)
92  : m_itf(itf.singular())
93  , m_bd(bd.singular())
94  , m_port_type(port_type)
95  , m_binding(0)
96  , m_vtr_op(l2_vtr_op_t::L2_VTR_DISABLED, rc_t::UNSET)
97  , m_vtr_op_tag(0)
98 {
99 }
100 
102  : m_itf(o.m_itf)
103  , m_bd(o.m_bd)
104  , m_port_type(o.m_port_type)
105  , m_binding(0)
106  , m_vtr_op(o.m_vtr_op)
107  , m_vtr_op_tag(o.m_vtr_op_tag)
108 {
109 }
110 
111 const l2_binding::key_t&
113 {
114  return (m_itf->key());
115 }
116 
117 bool
119 {
120  return ((*m_itf == *l.m_itf) && (*m_bd == *l.m_bd) &&
121  (m_port_type == l.m_port_type));
122 }
123 
124 std::shared_ptr<l2_binding>
126 {
127  return (m_db.find(key));
128 }
129 
130 void
131 l2_binding::sweep()
132 {
133  if (m_binding && handle_t::INVALID != m_itf->handle()) {
134  HW::enqueue(new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(),
135  m_bd->id(), m_port_type));
136  }
137 
138  // no need to undo the VTR operation.
139  HW::write();
140 }
141 
142 void
143 l2_binding::replay()
144 {
145  if (m_binding && handle_t::INVALID != m_itf->handle()) {
146  HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
147  m_bd->id(), m_port_type));
148  }
149 
150  if (m_vtr_op && handle_t::INVALID != m_itf->handle()) {
151  HW::enqueue(new l2_binding_cmds::set_vtr_op_cmd(m_vtr_op, m_itf->handle(),
152  m_vtr_op_tag));
153  }
154 }
155 
157 {
158  sweep();
159 
160  // not in the DB anymore.
161  m_db.release(m_itf->key(), this);
162 }
163 
164 std::string
166 {
167  std::ostringstream s;
168  s << "L2-binding:[" << m_itf->to_string() << " " << m_bd->to_string() << " "
169  << m_port_type.to_string() << " " << m_binding.to_string() << "]";
170 
171  return (s.str());
172 }
173 
174 void
175 l2_binding::set(const l2_vtr_op_t& op, uint16_t tag)
176 {
177  assert(rc_t::UNSET == m_vtr_op.rc());
178  m_vtr_op.set(rc_t::NOOP);
179  m_vtr_op.update(op);
180  m_vtr_op_tag = tag;
181 }
182 
183 void
184 l2_binding::update(const l2_binding& desired)
185 {
186  /*
187  * the desired state is always that the interface should be created
188  */
189  if (rc_t::OK != m_binding.rc()) {
190  HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
191  m_bd->id(), m_port_type));
192  } else if (!(*m_bd == *desired.m_bd)) {
193  /*
194  * re-binding to a different BD. do unbind, bind.
195  */
196  HW::enqueue(new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(),
197  m_bd->id(), m_port_type));
198  m_bd = desired.m_bd;
199  HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
200  m_bd->id(), m_port_type));
201  }
202 
203  /*
204  * set the VTR operation if request
205  */
206  if (m_vtr_op.update(desired.m_vtr_op)) {
207  HW::enqueue(new l2_binding_cmds::set_vtr_op_cmd(m_vtr_op, m_itf->handle(),
208  m_vtr_op_tag));
209  }
210 }
211 
212 std::shared_ptr<l2_binding>
213 l2_binding::find_or_add(const l2_binding& temp)
214 {
215  return (m_db.find_or_add(temp.m_itf->key(), temp));
216 }
217 
218 std::shared_ptr<l2_binding>
220 {
221  return find_or_add(*this);
222 }
223 
224 void
225 l2_binding::dump(std::ostream& os)
226 {
227  db_dump(m_db, os);
228 }
229 
231 {
232  OM::register_listener(this);
233  inspect::register_handler({ "l2" }, "L2 bindings", this);
234 }
235 
236 void
237 l2_binding::event_handler::handle_replay()
238 {
239  m_db.replay();
240 }
241 
242 void
243 l2_binding::event_handler::handle_populate(const client_db::key_t& key)
244 {
245  /**
246  * This is done while populating the bridge-domain
247  */
248 }
249 
251 l2_binding::event_handler::order() const
252 {
253  return (dependency_t::BINDING);
254 }
255 
256 void
257 l2_binding::event_handler::show(std::ostream& os)
258 {
259  db_dump(m_db, os);
260 }
261 }
262 
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "mozilla")
268  * End:
269  */
binding< l2_list > l2_binding
Typedef the L2 binding type.
static const rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:99
A Clas representing the binding of an L2 interface to a bridge-domain and the properties of that bind...
Definition: l2_binding.hpp:32
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 const l2_vtr_op_t L2_VTR_PUSH_1
Definition: l2_binding.hpp:46
static const l2_vtr_op_t L2_VTR_POP_1
Definition: l2_binding.hpp:48
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 const l2_vtr_op_t L2_VTR_DISABLED
Definition: l2_binding.hpp:45
static const l2_vtr_op_t L2_VTR_TRANSLATE_1_2
Definition: l2_binding.hpp:51
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:236
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static std::shared_ptr< l2_binding > find(const key_t &key)
Static function to find the bridge_domain in the model.
Definition: l2_binding.cpp:125
static const l2_port_type_t L2_PORT_TYPE_NORMAL
Definition: l2_binding.hpp:64
bool operator==(const l2_binding &l) const
Comparison operator - for UT.
Definition: l2_binding.cpp:118
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:263
#define assert(x)
Definition: dlmalloc.c:30
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
static const l2_vtr_op_t L2_VTR_PUSH_2
Definition: l2_binding.hpp:47
A functor class that binds L2 configuration to an interface.
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:118
std::string to_string() const
convert to string format for debug purposes
Definition: l2_binding.cpp:165
#define v
Definition: acl.c:496
const key_t & key() const
Return the binding&#39;s key.
Definition: l2_binding.cpp:112
static const l2_vtr_op_t L2_VTR_TRANSLATE_2_1
Definition: l2_binding.hpp:52
interface::key_t key_t
Key type for an L2 binding in the singular DB.
Definition: l2_binding.hpp:38
static const l2_port_type_t L2_PORT_TYPE_UU_FWD
Definition: l2_binding.hpp:66
std::shared_ptr< l2_binding > singular() const
Return the &#39;singular instance&#39; of the L2 config that matches this object.
Definition: l2_binding.cpp:219
A representation of an interface in VPP.
Definition: interface.hpp:41
A base class for all object_base in the VPP object_base-Model.
~l2_binding()
Destructor.
Definition: l2_binding.cpp:156
static const l2_port_type_t L2_PORT_TYPE_BVI
Definition: l2_binding.hpp:65
void event_handler(void *tls_async)
Definition: tls_async.c:339
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
A cmd class that Unbinds L2 configuration from an interface.
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:104
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
l2_port_type_t(const l2_port_type_t &l)=default
void set(const l2_vtr_op_t &op, uint16_t tag)
Set the VTR operation on the binding/interface.
Definition: l2_binding.cpp:175
l2_vtr_op_t
Definition: l2_vtr.h:27
Then L2/objects that bind to interfaces, BD, ACLS, etc.
static const l2_vtr_op_t L2_VTR_TRANSLATE_2_2
Definition: l2_binding.hpp:53
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static const rc_t UNSET
The value un-set.
Definition: types.hpp:94
Definition: hash.c:490
static const type_t BVI
A brideged Virtual interface (aka SVI or IRB)
Definition: interface.hpp:76
l2_binding(const interface &itf, const bridge_domain &bd)
Construct a new object matching the desried state.
Definition: l2_binding.cpp:74
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
l2_vtr_op_t(const l2_vtr_op_t &l)=default
static const l2_vtr_op_t L2_VTR_TRANSLATE_1_1
Definition: l2_binding.hpp:50
A cmd class sets the VTR operation.
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
static void dump(std::ostream &os)
Dump all l2_bindings into the stream provided.
Definition: l2_binding.cpp:225
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
static const l2_vtr_op_t L2_VTR_POP_2
Definition: l2_binding.hpp:49