FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
neighbour.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/neighbour.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/neighbour_cmds.hpp"
20 
21 namespace VOM {
22 singular_db<neighbour::key_t, neighbour> neighbour::m_db;
23 neighbour::event_handler neighbour::m_evh;
24 
25 const neighbour::flags_t neighbour::flags_t::NONE(0, "");
26 const neighbour::flags_t neighbour::flags_t::STATIC(1, "static");
27 const neighbour::flags_t neighbour::flags_t::NO_FIB_ENTRY(2, "no-fib-entry");
28 
29 neighbour::flags_t::flags_t(int v, const std::string s)
30  : enum_base(v, s)
31 {
32 }
33 
35  const boost::asio::ip::address& ip_addr,
36  const mac_address_t& mac,
37  const flags_t flags)
38  : m_hw(false)
39  , m_itf(itf.singular())
40  , m_ip_addr(ip_addr)
41  , m_mac(mac)
42  , m_flags(flags)
43 {
44 }
45 
47  : m_hw(n.m_hw)
48  , m_itf(n.m_itf)
49  , m_ip_addr(n.m_ip_addr)
50  , m_mac(n.m_mac)
51  , m_flags(n.m_flags)
52 {
53 }
54 
56 {
57  sweep();
58 
59  // not in the DB anymore.
60  m_db.release(key(), this);
61 }
62 
63 bool
65 {
66  return ((key() == n.key()) && (m_mac == n.m_mac));
67 }
68 
69 const neighbour::key_t
71 {
72  return (std::make_pair(m_itf->key(), m_ip_addr));
73 }
74 
75 void
76 neighbour::sweep()
77 {
78  if (m_hw) {
79  HW::enqueue(new neighbour_cmds::delete_cmd(m_hw, m_itf->handle(), m_mac,
80  m_ip_addr, m_flags));
81  }
82  HW::write();
83 }
84 
85 void
87 {
88  if (m_hw) {
89  HW::enqueue(new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac,
90  m_ip_addr, m_flags));
91  }
92 }
93 
94 std::string
96 {
97  std::ostringstream s;
98  s << "neighbour:[" << m_itf->to_string() << ", " << m_mac.to_string() << ", "
99  << m_ip_addr.to_string() << " " << m_flags.to_string() << "]";
100 
101  return (s.str());
102 }
103 
104 void
105 neighbour::update(const neighbour& r)
106 {
107  /*
108  * create the table if it is not yet created
109  */
110  if (rc_t::OK != m_hw.rc()) {
111  HW::enqueue(new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac,
112  m_ip_addr, m_flags));
113  }
114 }
115 
116 std::shared_ptr<neighbour>
117 neighbour::find_or_add(const neighbour& temp)
118 {
119  return (m_db.find_or_add(temp.key(), temp));
120 }
121 
122 std::shared_ptr<neighbour>
124 {
125  return (m_db.find(k));
126 }
127 
128 std::shared_ptr<neighbour>
130 {
131  return find_or_add(*this);
132 }
133 
134 void
135 neighbour::dump(std::ostream& os)
136 {
137  db_dump(m_db, os);
138 }
139 
140 std::ostream&
141 operator<<(std::ostream& os, const neighbour::key_t& key)
142 {
143  os << "[" << key.first << ", " << key.second << "]";
144 
145  return (os);
146 }
147 
149 {
150  OM::register_listener(this);
151  inspect::register_handler({ "neighbour" }, "Neighbours", this);
152 }
153 
154 void
155 neighbour::event_handler::handle_replay()
156 {
157  m_db.replay();
158 }
159 
160 void
161 neighbour::populate_i(const client_db::key_t& key,
162  std::shared_ptr<interface> itf,
163  const l3_proto_t& proto)
164 {
165  /*
166  * dump VPP current states
167  */
168  std::shared_ptr<neighbour_cmds::dump_cmd> cmd =
169  std::make_shared<neighbour_cmds::dump_cmd>(
170  neighbour_cmds::dump_cmd(itf->handle(), proto));
171 
172  HW::enqueue(cmd);
173  HW::write();
174 
175  for (auto& record : *cmd) {
176  /*
177  * construct a neighbour from each recieved record.
178  */
179  auto& payload = record.get_payload();
180 
181  mac_address_t mac = from_api(payload.neighbor.mac_address);
182  boost::asio::ip::address ip_addr = from_api(payload.neighbor.ip_address);
183  neighbour::flags_t f = from_api(payload.neighbor.flags);
184  neighbour n(*itf, ip_addr, mac, f);
185  ;
186 
187  VOM_LOG(log_level_t::DEBUG) << "neighbour-dump: " << itf->to_string() << " "
188  << mac.to_string() << " " << ip_addr.to_string()
189  << " " << f.to_string();
190 
191  /*
192  * Write each of the discovered interfaces into the OM,
193  * but disable the HW Command q whilst we do, so that no
194  * commands are sent to VPP
195  */
196  OM::commit(key, n);
197  }
198 }
199 
200 void
201 neighbour::event_handler::handle_populate(const client_db::key_t& key)
202 {
203  auto it = interface::cbegin();
204 
205  while (it != interface::cend()) {
206  neighbour::populate_i(key, it->second.lock(), l3_proto_t::IPV4);
207  neighbour::populate_i(key, it->second.lock(), l3_proto_t::IPV6);
208 
209  ++it;
210  }
211 }
212 
214 neighbour::event_handler::order() const
215 {
216  return (dependency_t::ENTRY);
217 }
218 
219 void
220 neighbour::event_handler::show(std::ostream& os)
221 {
222  db_dump(m_db, os);
223 }
224 }
225 
226 /*
227  * fd.io coding-style-patch-verification: ON
228  *
229  * Local Variables:
230  * eval: (c-set-style "mozilla")
231  * End:
232  */
const key_t key() const
Return the object&#39;s key.
Definition: neighbour.cpp:70
A cmd class that deletes a bridge domain ARP entry.
#define VOM_LOG(lvl)
Definition: logger.hpp:181
vl_api_mac_address_t mac
Definition: l2.api:502
std::shared_ptr< neighbour > singular() const
Return the matching &#39;singular instance&#39;.
Definition: neighbour.cpp:129
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
A template base class for all enum types.
Definition: enum_base.hpp:30
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
std::string to_string() const
Convert to string for debugging.
Definition: neighbour.cpp:95
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 flags_t STATIC
Definition: neighbour.hpp:50
static const log_level_t DEBUG
Definition: logger.hpp:32
An L3 protocol can be used to construct a prefix that is used to match packets are part of a route...
Definition: prefix.hpp:52
neighbour(const interface &itf, const boost::asio::ip::address &ip_addr, const mac_address_t &mac, const flags_t flags=flags_t::STATIC)
Construct an ARP entry.
Definition: neighbour.cpp:34
bool operator==(const neighbour &n) const
Comparison operator.
Definition: neighbour.cpp:64
static const_iterator_t cbegin()
Definition: interface.cpp:170
static const flags_t NONE
Definition: neighbour.hpp:49
static const l3_proto_t IPV4
Definition: prefix.hpp:55
static const l3_proto_t IPV6
Definition: prefix.hpp:56
vl_api_ip_proto_t proto
Definition: acl_types.api:50
A entry in the neighbour entry (ARP or IPv6 ND)
Definition: neighbour.hpp:27
u32 flags
Definition: vhost_user.h:248
flags_t(int v, const std::string s)
Constructor.
Definition: neighbour.cpp:29
A representation of an interface in VPP.
Definition: interface.hpp:41
static const flags_t NO_FIB_ENTRY
Definition: neighbour.hpp:51
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
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:334
void replay(void)
replay the object to create it in hardware
Definition: neighbour.cpp:86
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
manual_print typedef address
Definition: ip_types.api:85
A cmd class that Dumps all the neighbours.
static void dump(std::ostream &os)
Dump all neighbours into the stream provided.
Definition: neighbour.cpp:135
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
static std::shared_ptr< neighbour > find(const key_t &k)
Find the neighbour fromits key.
Definition: neighbour.cpp:123
std::string to_string() const
String conversion.
Definition: types.cpp:148
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A command class that creates or updates the bridge domain ARP Entry.
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
A representation of a method call to VPP.
Definition: cmd.hpp:32
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
~neighbour()
Destructor.
Definition: neighbour.cpp:55
Entries in Tables.
static const_iterator_t cend()
Definition: interface.cpp:176
save_rewrite_length must be aligned so that reass doesn t overwrite it
Definition: buffer.h:401
std::pair< interface::key_t, boost::asio::ip::address > key_t
The key for a neighbour entry; the interface and IP address.
Definition: neighbour.hpp:58
Type def of a Ethernet address.
Definition: types.hpp:295
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127