FD.io VPP  v18.10-34-gcce845e
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"
19 
20 namespace VOM {
21 singular_db<l3_binding::key_t, l3_binding> l3_binding::m_db;
22 
23 l3_binding::event_handler l3_binding::m_evh;
24 
25 /**
26  * Construct a new object matching the desried state
27  */
29  : m_itf(itf.singular())
30  , m_pfx(pfx)
31  , m_binding(true, rc_t::NOOP)
32 {
33 }
34 
36  : m_itf(o.m_itf)
37  , m_pfx(o.m_pfx)
38  , m_binding(o.m_binding)
39 {
40 }
41 
43 {
44  sweep();
45 
46  // not in the DB anymore.
47  m_db.release(key(), this);
48 }
49 
50 bool
52 {
53  return ((m_pfx == l.m_pfx) && (*m_itf == *l.m_itf));
54 }
55 
58 {
59  return (make_pair(m_itf->key(), m_pfx));
60 }
61 
62 void
63 l3_binding::sweep()
64 {
65  if (m_binding) {
67  new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx));
68  }
69  HW::write();
70 }
71 
72 void
73 l3_binding::replay()
74 {
75  if (m_binding) {
77  new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
78  }
79 }
80 
81 const route::prefix_t&
83 {
84  return (m_pfx);
85 }
86 
87 const interface&
89 {
90  return (*m_itf);
91 }
92 
95 {
96  return m_db.begin();
97 }
98 
101 {
102  return m_db.end();
103 }
104 
105 std::string
107 {
108  std::ostringstream s;
109  s << "L3-binding:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
110  << " " << m_binding.to_string() << "]";
111 
112  return (s.str());
113 }
114 
115 void
116 l3_binding::update(const l3_binding& desired)
117 {
118  /*
119  * no updates for the binding. chaning the interface or the prefix is a change
120  * to the
121  * key, hence a new object
122  */
123  if (!m_binding) {
124  HW::enqueue(
125  new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
126  }
127 }
128 
129 std::shared_ptr<l3_binding>
130 l3_binding::find_or_add(const l3_binding& temp)
131 {
132  return (m_db.find_or_add(temp.key(), temp));
133 }
134 
135 std::shared_ptr<l3_binding>
137 {
138  return (m_db.find(k));
139 }
140 
141 std::shared_ptr<l3_binding>
143 {
144  return find_or_add(*this);
145 }
146 
147 void
148 l3_binding::dump(std::ostream& os)
149 {
150  db_dump(m_db, os);
151 }
152 
153 std::ostream&
154 operator<<(std::ostream& os, const l3_binding::key_t& key)
155 {
156  os << "[" << key.first << ", " << key.second << "]";
157 
158  return (os);
159 }
160 
162 {
163  OM::register_listener(this);
164  inspect::register_handler({ "l3" }, "L3 bindings", this);
165 }
166 
167 void
168 l3_binding::event_handler::handle_replay()
169 {
170  m_db.replay();
171 }
172 
173 void
174 l3_binding::event_handler::handle_populate(const client_db::key_t& key)
175 {
176  /**
177  * This is done while populating the interfaces
178  */
179 }
180 
182 l3_binding::event_handler::order() const
183 {
184  return (dependency_t::BINDING);
185 }
186 
187 void
188 l3_binding::event_handler::show(std::ostream& os)
189 {
190  db_dump(m_db, os);
191 }
192 }
193 
194 /*
195  * fd.io coding-style-patch-verification: ON
196  *
197  * Local Variables:
198  * eval: (c-set-style "mozilla")
199  * End:
200  */
A functor class that binds the L3 config to the interface.
static std::shared_ptr< l3_binding > find(const key_t &k)
Find a binding from its key.
Definition: l3_binding.cpp:136
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
const interface & itf() const
Return the interface associated with this L3 binding.
Definition: l3_binding.cpp:88
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:236
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static const_iterator_t cbegin()
Definition: l3_binding.cpp:94
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:100
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:183
const route::prefix_t & prefix() const
Return the prefix associated with this L3 binding.
Definition: l3_binding.cpp:82
static void dump(std::ostream &os)
Dump all l3_bindings into the stream provided.
Definition: l3_binding.cpp:148
std::string to_string() const
convert to string format for debug purposes
Definition: l3_binding.cpp:106
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:142
A representation of an interface in VPP.
Definition: interface.hpp:41
bool operator==(const l3_binding &l) const
Comparison operator.
Definition: l3_binding.cpp:51
A cmd class that Unbinds L3 Config from an interface.
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
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
l3_binding(const interface &itf, const route::prefix_t &pfx)
Construct a new object matching the desried state.
Definition: l3_binding.cpp:28
const key_t key() const
Get the object&#39;s key.
Definition: l3_binding.cpp:57
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
A representation of L3 configuration on an interface.
Definition: l3_binding.hpp:30
~l3_binding()
Destructor.
Definition: l3_binding.cpp:42
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
A prefix defintion.
Definition: prefix.hpp:92