FD.io VPP  v21.10.1-2-g0a485f517
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"
18 #include "vom/l2_vtr_cmds.hpp"
20 
21 namespace VOM {
22 /**
23  * A DB of all the L2 Configs
24  */
25 singular_db<l2_binding::key_t, l2_binding> l2_binding::m_db;
26 
27 l2_binding::event_handler l2_binding::m_evh;
28 
29 const l2_binding::l2_port_type_t
31 const l2_binding::l2_port_type_t l2_binding::l2_port_type_t::L2_PORT_TYPE_BVI(
32  1,
33  "bvi");
34 const l2_binding::l2_port_type_t
36 
38  : enum_base<l2_binding::l2_port_type_t>(v, s)
39 {
40 }
41 
42 /**
43  * Construct a new object matching the desried state
44  */
46  : m_itf(itf.singular())
47  , m_bd(bd.singular())
48  , m_port_type(l2_port_type_t::L2_PORT_TYPE_NORMAL)
49  , m_binding(0)
50  , m_vtr_op(l2_vtr::option_t::DISABLED, rc_t::UNSET)
51  , m_vtr_op_tag(0)
52 {
53  if (interface::type_t::BVI == m_itf->type())
55 }
56 
57 /**
58  * Construct a new object matching the desried state
59  */
61  const bridge_domain& bd,
62  const l2_port_type_t& port_type)
63  : m_itf(itf.singular())
64  , m_bd(bd.singular())
65  , m_port_type(port_type)
66  , m_binding(0)
67  , m_vtr_op(l2_vtr::option_t::DISABLED, rc_t::UNSET)
68  , m_vtr_op_tag(0)
69 {
70 }
71 
73  : m_itf(o.m_itf)
74  , m_bd(o.m_bd)
75  , m_port_type(o.m_port_type)
76  , m_binding(0)
77  , m_vtr_op(o.m_vtr_op)
78  , m_vtr_op_tag(o.m_vtr_op_tag)
79 {
80 }
81 
82 const l2_binding::key_t&
84 {
85  return (m_itf->key());
86 }
87 
88 bool
90 {
91  return ((*m_itf == *l.m_itf) && (*m_bd == *l.m_bd) &&
92  (m_port_type == l.m_port_type));
93 }
94 
95 std::shared_ptr<l2_binding>
97 {
98  return (m_db.find(key));
99 }
100 
101 void
102 l2_binding::sweep()
103 {
104  if (m_binding && handle_t::INVALID != m_itf->handle()) {
105  HW::enqueue(new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(),
106  m_bd->id(), m_port_type));
107  }
108 
109  // no need to undo the VTR operation.
110  HW::write();
111 }
112 
113 void
114 l2_binding::replay()
115 {
116  if (m_binding && handle_t::INVALID != m_itf->handle()) {
117  HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
118  m_bd->id(), m_port_type));
119  }
120 
121  if (m_vtr_op && handle_t::INVALID != m_itf->handle()) {
122  HW::enqueue(
123  new l2_vtr_cmds::set_cmd(m_vtr_op, m_itf->handle(), m_vtr_op_tag));
124  }
125 }
126 
128 {
129  sweep();
130 
131  // not in the DB anymore.
132  m_db.release(m_itf->key(), this);
133 }
134 
137 {
138  std::ostringstream s;
139  s << "L2-binding:[" << m_itf->to_string() << " " << m_bd->to_string() << " "
140  << m_port_type.to_string() << " " << m_binding.to_string() << "]";
141 
142  return (s.str());
143 }
144 
145 void
146 l2_binding::set(const l2_vtr::option_t& op, uint16_t tag)
147 {
148  assert(rc_t::UNSET == m_vtr_op.rc());
149  m_vtr_op.set(rc_t::NOOP);
150  m_vtr_op.update(op);
151  m_vtr_op_tag = tag;
152 }
153 
154 void
155 l2_binding::update(const l2_binding& desired)
156 {
157  /*
158  * the desired state is always that the interface should be created
159  */
160  if (rc_t::OK != m_binding.rc()) {
161  HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
162  m_bd->id(), m_port_type));
163  } else if (!(*m_bd == *desired.m_bd)) {
164  /*
165  * re-binding to a different BD. do unbind, bind.
166  */
167  HW::enqueue(new l2_binding_cmds::unbind_cmd(m_binding, m_itf->handle(),
168  m_bd->id(), m_port_type));
169  m_bd = desired.m_bd;
170  HW::enqueue(new l2_binding_cmds::bind_cmd(m_binding, m_itf->handle(),
171  m_bd->id(), m_port_type));
172  }
173 
174  /*
175  * set the VTR operation if request
176  */
177  if (m_vtr_op.update(desired.m_vtr_op)) {
178  HW::enqueue(
179  new l2_vtr_cmds::set_cmd(m_vtr_op, m_itf->handle(), m_vtr_op_tag));
180  }
181 }
182 
183 std::shared_ptr<l2_binding>
184 l2_binding::find_or_add(const l2_binding& temp)
185 {
186  return (m_db.find_or_add(temp.m_itf->key(), temp));
187 }
188 
189 std::shared_ptr<l2_binding>
191 {
192  return find_or_add(*this);
193 }
194 
195 void
196 l2_binding::dump(std::ostream& os)
197 {
198  db_dump(m_db, os);
199 }
200 
202 {
203  OM::register_listener(this);
204  inspect::register_handler({ "l2" }, "L2 bindings", this);
205 }
206 
207 void
208 l2_binding::event_handler::handle_replay()
209 {
210  m_db.replay();
211 }
212 
213 void
214 l2_binding::event_handler::handle_populate(const client_db::key_t& key)
215 {
216  /**
217  * This is done while populating the bridge-domain
218  */
219 }
220 
222 l2_binding::event_handler::order() const
223 {
224  return (dependency_t::BINDING);
225 }
226 
227 void
228 l2_binding::event_handler::show(std::ostream& os)
229 {
230  db_dump(m_db, os);
231 }
232 }
233 
234 /*
235  * fd.io coding-style-patch-verification: OFF
236  *
237  * Local Variables:
238  * eval: (c-set-style "mozilla")
239  * End:
240  */
VOM::bridge_domain
A base class for all object_base in the VPP object_base-Model.
Definition: bridge_domain.hpp:31
VOM::l2_binding_cmds::unbind_cmd
A cmd class that Unbinds L2 configuration from an interface.
Definition: l2_binding_cmds.hpp:77
VOM::HW::write
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
VOM::interface::type_t::BVI
const static type_t BVI
A brideged Virtual interface (aka SVI or IRB)
Definition: interface.hpp:76
assert
#define assert(x)
Definition: dlmalloc.c:31
VOM::HW::item::set
void set(const rc_t &rc)
Set the HW return code - should only be called from the family of Command objects.
Definition: hw.hpp:125
VOM::l2_binding::l2_port_type_t::l2_port_type_t
l2_port_type_t(const l2_port_type_t &l)=default
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::l2_binding::key
const key_t & key() const
Return the binding's key.
Definition: l2_binding.cpp:83
key
typedef key
Definition: ipsec_types.api:91
VOM::rc_t::UNSET
const static rc_t UNSET
The value un-set.
Definition: types.hpp:99
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::ACL::l2_binding
binding< l2_list > l2_binding
Typedef the L2 binding type.
Definition: acl_binding.hpp:215
VOM::l2_binding::key_t
interface::key_t key_t
Key type for an L2 binding in the singular DB.
Definition: l2_binding.hpp:39
l2_binding_cmds.hpp
VOM::interface
A representation of an interface in VPP.
Definition: interface.hpp:41
VOM::rc_t::NOOP
const static rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:104
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::l2_binding::~l2_binding
~l2_binding()
Destructor.
Definition: l2_binding.cpp:127
VOM::l2_binding_cmds::bind_cmd
A functor class that binds L2 configuration to an interface.
Definition: l2_binding_cmds.hpp:31
l2_vtr_cmds.hpp
singular_db_funcs.hpp
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::l2_binding::operator==
bool operator==(const l2_binding &l) const
Comparison operator - for UT.
Definition: l2_binding.cpp:89
VOM::l2_binding::dump
static void dump(std::ostream &os)
Dump all l2_bindings into the stream provided.
Definition: l2_binding.cpp:196
VOM::l2_binding::l2_port_type_t::L2_PORT_TYPE_NORMAL
const static l2_port_type_t L2_PORT_TYPE_NORMAL
Definition: l2_binding.hpp:46
VOM::l2_binding::l2_binding
l2_binding(const interface &itf, const bridge_domain &bd)
Construct a new object matching the desried state.
Definition: l2_binding.cpp:45
VOM::l2_binding::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: l2_binding.cpp:136
VOM::handle_t::INVALID
const static handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:268
VOM::l2_binding::l2_port_type_t
Definition: l2_binding.hpp:41
VOM::l2_binding::find
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:96
VOM::l2_binding::l2_port_type_t::L2_PORT_TYPE_BVI
const static l2_port_type_t L2_PORT_TYPE_BVI
Definition: l2_binding.hpp:47
VOM::dependency_t::BINDING
@ BINDING
Then L2/objects that bind to interfaces, BD, ACLS, etc.
l2_binding.hpp
VOM::HW::item::update
bool update(const item &desired)
update the item to the desired state.
Definition: hw.hpp:136
VOM::rc_t
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
VOM::l2_vtr::option_t
Definition: l2_vtr.hpp:24
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::l2_binding::singular
std::shared_ptr< l2_binding > singular() const
Return the 'singular instance' of the L2 config that matches this object.
Definition: l2_binding.cpp:190
UNSET
@ UNSET
Definition: hash.c:541
VOM::l2_binding::set
void set(const l2_vtr::option_t &op, uint16_t tag)
Set the VTR operation on the binding/interface.
Definition: l2_binding.cpp:146
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