FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
l3_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/l3_binding.hpp"
17 #include "vom/l3_binding_cmds.hpp"
18 
19 namespace VOM {
20 singular_db<l3_binding::key_t, l3_binding> l3_binding::m_db;
21 
22 l3_binding::event_handler l3_binding::m_evh;
23 
24 /**
25  * Construct a new object matching the desried state
26  */
28  : m_itf(itf.singular())
29  , m_pfx(pfx)
30  , m_binding(true, rc_t::NOOP)
31 {
32 }
33 
35  : m_itf(o.m_itf)
36  , m_pfx(o.m_pfx)
37  , m_binding(o.m_binding)
38 {
39 }
40 
42 {
43  sweep();
44 
45  // not in the DB anymore.
46  m_db.release(key(), this);
47 }
48 
49 bool
51 {
52  return ((m_pfx == l.m_pfx) && (*m_itf == *l.m_itf));
53 }
54 
57 {
58  return (make_pair(m_itf->key(), m_pfx));
59 }
60 
61 void
62 l3_binding::sweep()
63 {
64  if (m_binding) {
66  new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx));
67  }
68  HW::write();
69 }
70 
71 void
72 l3_binding::replay()
73 {
74  if (m_binding) {
76  new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
77  }
78 }
79 
80 const route::prefix_t&
82 {
83  return (m_pfx);
84 }
85 
86 const interface&
88 {
89  return (*m_itf);
90 }
91 
94 {
95  return m_db.cbegin();
96 }
97 
100 {
101  return m_db.cend();
102 }
103 
104 std::string
106 {
107  std::ostringstream s;
108  s << "L3-binding:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
109  << " " << m_binding.to_string() << "]";
110 
111  return (s.str());
112 }
113 
114 void
115 l3_binding::update(const l3_binding& desired)
116 {
117  /*
118  * no updates for the binding. chaning the interface or the prefix is a change
119  * to the
120  * key, hence a new object
121  */
122  if (!m_binding) {
123  HW::enqueue(
124  new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
125  }
126 }
127 
128 std::shared_ptr<l3_binding>
129 l3_binding::find_or_add(const l3_binding& temp)
130 {
131  return (m_db.find_or_add(temp.key(), temp));
132 }
133 
134 std::shared_ptr<l3_binding>
136 {
137  return (m_db.find(k));
138 }
139 
140 std::shared_ptr<l3_binding>
142 {
143  return find_or_add(*this);
144 }
145 
146 void
147 l3_binding::dump(std::ostream& os)
148 {
149  m_db.dump(os);
150 }
151 
152 std::ostream&
153 operator<<(std::ostream& os, const l3_binding::key_t& key)
154 {
155  os << "[" << key.first << ", " << key.second << "]";
156 
157  return (os);
158 }
159 
160 std::deque<std::shared_ptr<l3_binding>>
162 {
163  /*
164  * Loop throught the entire map looking for matching interface.
165  * not the most efficient algorithm, but it will do for now. The
166  * number of L3 configs is low and this is only called during bootup
167  */
168  std::deque<std::shared_ptr<l3_binding>> l3s;
169 
170  auto it = m_db.cbegin();
171 
172  while (it != m_db.cend()) {
173  /*
174  * The key in the DB is a pair of the interface's name and prefix.
175  * If the keys match, save the L3-config
176  */
177  auto key = it->first;
178 
179  if (i.key() == key.first) {
180  l3s.push_back(it->second.lock());
181  }
182 
183  ++it;
184  }
185 
186  return (l3s);
187 }
188 
189 l3_binding::event_handler::event_handler()
190 {
191  OM::register_listener(this);
192  inspect::register_handler({ "l3" }, "L3 bindings", this);
193 }
194 
195 void
196 l3_binding::event_handler::handle_replay()
197 {
198  m_db.replay();
199 }
200 
201 void
202 l3_binding::event_handler::handle_populate(const client_db::key_t& key)
203 {
204  /**
205  * This is done while populating the interfaces
206  */
207 }
208 
210 l3_binding::event_handler::order() const
211 {
212  return (dependency_t::BINDING);
213 }
214 
215 void
216 l3_binding::event_handler::show(std::ostream& os)
217 {
218  m_db.dump(os);
219 }
220 }
221 
222 /*
223  * fd.io coding-style-patch-verification: ON
224  *
225  * Local Variables:
226  * eval: (c-set-style "mozilla")
227  * End:
228  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
A functor class that binds the L3 config to the interface.
const interface & itf() const
Return the interface associated with this L3 binding.
Definition: l3_binding.cpp:87
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 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:225
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static const_iterator_t cbegin()
Definition: l3_binding.cpp:93
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
static const_iterator_t cend()
Definition: l3_binding.cpp:99
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:170
const route::prefix_t & prefix() const
Return the prefix associated with this L3 binding.
Definition: l3_binding.cpp:81
static void dump(std::ostream &os)
Dump all l3_bindings into the stream provided.
Definition: l3_binding.cpp:147
std::string to_string() const
convert to string format for debug purposes
Definition: l3_binding.cpp:105
singular_db< key_t, l3_binding >::const_iterator const_iterator_t
The iterator type.
Definition: l3_binding.hpp:66
std::shared_ptr< l3_binding > singular() const
Return the &#39;singular instance&#39; of the L3-Config that matches this object.
Definition: l3_binding.cpp:141
A representation of an interface in VPP.
Definition: interface.hpp:41
bool operator==(const l3_binding &l) const
Comparison operator.
Definition: l3_binding.cpp:50
A cmd class that Unbinds L3 Config from an interface.
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 void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:189
l3_binding(const interface &itf, const route::prefix_t &pfx)
Construct a new object matching the desried state.
Definition: l3_binding.cpp:27
const key_t key() const
Get the object&#39;s key.
Definition: l3_binding.cpp:56
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
Then L2/objects that bind to interfaces, BD, ACLS, etc.
std::pair< interface::key_t, route::prefix_t > key_t
The key type for l3_bindings.
Definition: l3_binding.hpp:36
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
static std::deque< std::shared_ptr< l3_binding > > find(const interface &i)
Find all bindings in the DB for the interface passed.
Definition: l3_binding.cpp:161
A representation of L3 configuration on an interface.
Definition: l3_binding.hpp:30
~l3_binding()
Destructor.
Definition: l3_binding.cpp:41
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:124
A prefix defintion.
Definition: prefix.hpp:91
const key_t & key() const
Return the interface type.
Definition: interface.cpp:236