FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
l2_xconnect.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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_xconnect.hpp"
17 #include "vom/l2_xconnect_cmds.hpp"
19 
20 namespace VOM {
21 /**
22  * A DB of all the L2 x-connect Configs
23  */
24 singular_db<l2_xconnect::key_t, l2_xconnect> l2_xconnect::m_db;
25 
26 l2_xconnect::event_handler l2_xconnect::m_evh;
27 
28 /**
29  * Construct a new object matching the desried state
30  */
31 l2_xconnect::l2_xconnect(const interface& east_itf, const interface& west_itf)
32  : m_east_itf(east_itf.singular())
33  , m_west_itf(west_itf.singular())
34  , m_xconnect_east(0)
35  , m_xconnect_west(0)
36 {
37 }
38 
40  : m_east_itf(o.m_east_itf)
41  , m_west_itf(o.m_west_itf)
42  , m_xconnect_east(o.m_xconnect_east)
43  , m_xconnect_west(o.m_xconnect_west)
44 {
45 }
46 
49 {
50  if (m_east_itf->name() < m_west_itf->name())
51  return (std::make_pair(m_east_itf->key(), m_west_itf->key()));
52  return (std::make_pair(m_west_itf->key(), m_east_itf->key()));
53 }
54 
55 bool
57 {
58  return ((*m_east_itf == *l.m_east_itf) && (*m_west_itf == *l.m_west_itf));
59 }
60 
61 std::shared_ptr<l2_xconnect>
63 {
64  return (m_db.find(key));
65 }
66 
67 void
68 l2_xconnect::sweep()
69 {
70  if (m_xconnect_east && m_xconnect_west &&
71  handle_t::INVALID != m_east_itf->handle() &&
72  handle_t::INVALID != m_west_itf->handle()) {
74  m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
76  m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
77  }
78 
79  HW::write();
80 }
81 
82 void
83 l2_xconnect::replay()
84 {
85  if (m_xconnect_east && m_xconnect_west &&
86  handle_t::INVALID != m_east_itf->handle() &&
87  handle_t::INVALID != m_west_itf->handle()) {
89  m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
91  m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
92  }
93 }
94 
96 {
97  sweep();
98 
99  // not in the DB anymore.
100  m_db.release(key(), this);
101 }
102 
103 std::string
105 {
106  std::ostringstream s;
107  s << "L2-xconnect:[" << m_east_itf->to_string() << " "
108  << m_west_itf->to_string() << " " << m_xconnect_east.to_string() << " "
109  << m_xconnect_west.to_string() << "]";
110 
111  return (s.str());
112 }
113 
114 void
115 l2_xconnect::update(const l2_xconnect& desired)
116 {
117  /*
118  * the desired state is always that the interface should be created
119  */
120  if (rc_t::OK != m_xconnect_east.rc() && rc_t::OK != m_xconnect_west.rc()) {
122  m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
124  m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
125  }
126 }
127 
128 std::shared_ptr<l2_xconnect>
129 l2_xconnect::find_or_add(const l2_xconnect& temp)
130 {
131  return (m_db.find_or_add(temp.key(), temp));
132 }
133 
134 std::shared_ptr<l2_xconnect>
136 {
137  return find_or_add(*this);
138 }
139 
140 void
141 l2_xconnect::dump(std::ostream& os)
142 {
143  db_dump(m_db, os);
144 }
145 
147 {
148  OM::register_listener(this);
149  inspect::register_handler({ "l2-xconnect" }, "L2 xconnects", this);
150 }
151 
152 void
153 l2_xconnect::event_handler::handle_replay()
154 {
155  m_db.replay();
156 }
157 
158 void
159 l2_xconnect::event_handler::handle_populate(const client_db::key_t& key)
160 {
161  /**
162  * This needs to be done here
163  */
164  std::shared_ptr<l2_xconnect_cmds::dump_cmd> cmd =
165  std::make_shared<l2_xconnect_cmds::dump_cmd>();
166 
167  HW::enqueue(cmd);
168  HW::write();
169 
170  for (auto& x_record : *cmd) {
171  auto& payload = x_record.get_payload();
172 
173  VOM_LOG(log_level_t::DEBUG) << "l2-xconnect dump: "
174  << " east-itf: " << payload.rx_sw_if_index
175  << " west-itf: " << payload.tx_sw_if_index;
176 
177  std::shared_ptr<interface> east_itf =
178  interface::find(payload.rx_sw_if_index);
179  std::shared_ptr<interface> west_itf =
180  interface::find(payload.tx_sw_if_index);
181 
182  if (east_itf && west_itf) {
183  if (east_itf->name() > west_itf->name())
184  continue;
185  l2_xconnect l2_xc(*east_itf, *west_itf);
186  OM::commit(key, l2_xc);
187  }
188  }
189 }
190 
192 l2_xconnect::event_handler::order() const
193 {
194  return (dependency_t::BINDING);
195 }
196 
197 void
198 l2_xconnect::event_handler::show(std::ostream& os)
199 {
200  db_dump(m_db, os);
201 }
202 }
203 
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "mozilla")
209  * End:
210  */
static std::shared_ptr< l2_xconnect > find(const key_t &key)
Static function to find the bridge_domain in the model.
Definition: l2_xconnect.cpp:62
A functor class that binds L2 configuration to an interface.
#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:527
A Class representing the cross connnect of an L2 interface with another l2 interface.
Definition: l2_xconnect.hpp:31
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
std::shared_ptr< l2_xconnect > singular() const
Return the &#39;singular instance&#39; of the L2 config that matches this object.
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:268
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
A cmd class that Unbinds L2 configuration from an interface.
static void dump(std::ostream &os)
Dump all l2_xconnects into the stream provided.
A representation of an interface in VPP.
Definition: interface.hpp:41
bool operator==(const l2_xconnect &l) const
Comparison operator - for UT.
Definition: l2_xconnect.cpp:56
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
l2_xconnect(const interface &east_itf, const interface &west_itf)
Construct a new object matching the desried state.
Definition: l2_xconnect.cpp:31
std::pair< interface::key_t, interface::key_t > key_t
Key type for an L2 xconnect in the singular DB.
Definition: l2_xconnect.hpp:37
~l2_xconnect()
Destructor.
Definition: l2_xconnect.cpp:95
std::string to_string() const
convert to string format for debug purposes
Then L2/objects that bind to interfaces, BD, ACLS, etc.
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
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
const key_t key() const
Return the xconnect&#39;s key.
Definition: l2_xconnect.cpp:48