FD.io VPP  v19.01.2-3-gf61a1a8
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/neighbour_cmds.hpp"
19 
20 namespace VOM {
21 singular_db<neighbour::key_t, neighbour> neighbour::m_db;
22 neighbour::event_handler neighbour::m_evh;
23 
25  const boost::asio::ip::address& ip_addr,
26  const mac_address_t& mac)
27  : m_hw(false)
28  , m_itf(itf.singular())
29  , m_ip_addr(ip_addr)
30  , m_mac(mac)
31 {
32 }
33 
35  : m_hw(bde.m_hw)
36  , m_itf(bde.m_itf)
37  , m_ip_addr(bde.m_ip_addr)
38  , m_mac(bde.m_mac)
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 ((key() == n.key()) && (m_mac == n.m_mac));
54 }
55 
56 const neighbour::key_t
58 {
59  return (std::make_pair(m_itf->key(), m_ip_addr));
60 }
61 
62 void
63 neighbour::sweep()
64 {
65  if (m_hw) {
67  new neighbour_cmds::delete_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
68  }
69  HW::write();
70 }
71 
72 void
74 {
75  if (m_hw) {
77  new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
78  }
79 }
80 
81 std::string
83 {
84  std::ostringstream s;
85  s << "neighbour:[" << m_itf->to_string() << ", " << m_mac.to_string() << ", "
86  << m_ip_addr.to_string() << "]";
87 
88  return (s.str());
89 }
90 
91 void
92 neighbour::update(const neighbour& r)
93 {
94  /*
95  * create the table if it is not yet created
96  */
97  if (rc_t::OK != m_hw.rc()) {
99  new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
100  }
101 }
102 
103 std::shared_ptr<neighbour>
104 neighbour::find_or_add(const neighbour& temp)
105 {
106  return (m_db.find_or_add(temp.key(), temp));
107 }
108 
109 std::shared_ptr<neighbour>
111 {
112  return (m_db.find(k));
113 }
114 
115 std::shared_ptr<neighbour>
117 {
118  return find_or_add(*this);
119 }
120 
121 void
122 neighbour::dump(std::ostream& os)
123 {
124  db_dump(m_db, os);
125 }
126 
127 std::ostream&
128 operator<<(std::ostream& os, const neighbour::key_t& key)
129 {
130  os << "[" << key.first << ", " << key.second << "]";
131 
132  return (os);
133 }
134 
136 {
137  OM::register_listener(this);
138  inspect::register_handler({ "neighbour" }, "Neighbours", this);
139 }
140 
141 void
142 neighbour::event_handler::handle_replay()
143 {
144  m_db.replay();
145 }
146 
147 void
148 neighbour::populate_i(const client_db::key_t& key,
149  std::shared_ptr<interface> itf,
150  const l3_proto_t& proto)
151 {
152  /*
153  * dump VPP current states
154  */
155  std::shared_ptr<neighbour_cmds::dump_cmd> cmd =
156  std::make_shared<neighbour_cmds::dump_cmd>(
157  neighbour_cmds::dump_cmd(itf->handle(), proto));
158 
159  HW::enqueue(cmd);
160  HW::write();
161 
162  for (auto& record : *cmd) {
163  /*
164  * construct a neighbour from each recieved record.
165  */
166  auto& payload = record.get_payload();
167 
168  mac_address_t mac(payload.mac_address);
169  boost::asio::ip::address ip_addr =
170  from_bytes(payload.is_ipv6, payload.ip_address);
171  neighbour n(*itf, ip_addr, mac);
172 
173  VOM_LOG(log_level_t::DEBUG) << "neighbour-dump: " << itf->to_string()
174  << mac.to_string() << ip_addr.to_string();
175 
176  /*
177  * Write each of the discovered interfaces into the OM,
178  * but disable the HW Command q whilst we do, so that no
179  * commands are sent to VPP
180  */
181  OM::commit(key, n);
182  }
183 }
184 
185 void
186 neighbour::event_handler::handle_populate(const client_db::key_t& key)
187 {
188  auto it = interface::cbegin();
189 
190  while (it != interface::cend()) {
191  neighbour::populate_i(key, it->second.lock(), l3_proto_t::IPV4);
192  neighbour::populate_i(key, it->second.lock(), l3_proto_t::IPV6);
193 
194  ++it;
195  }
196 }
197 
199 neighbour::event_handler::order() const
200 {
201  return (dependency_t::ENTRY);
202 }
203 
204 void
205 neighbour::event_handler::show(std::ostream& os)
206 {
207  db_dump(m_db, os);
208 }
209 }
210 
211 /*
212  * fd.io coding-style-patch-verification: ON
213  *
214  * Local Variables:
215  * eval: (c-set-style "mozilla")
216  * End:
217  */
typedef address
Definition: ip_types.api:30
A cmd class that deletes a bridge domain ARP entry.
#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 key_t key() const
Return the object&#39;s key.
Definition: neighbour.cpp:57
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
std::shared_ptr< neighbour > singular() const
Return the matching &#39;singular instance&#39;.
Definition: neighbour.cpp:116
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
String conversion.
Definition: types.cpp:148
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
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
std::string to_string() const
Convert to string for debugging.
Definition: neighbour.cpp:82
static const_iterator_t cbegin()
Definition: interface.cpp:163
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
static const l3_proto_t IPV4
Definition: prefix.hpp:55
static const l3_proto_t IPV6
Definition: prefix.hpp:56
bool operator==(const neighbour &n) const
Comparison operator.
Definition: neighbour.cpp:51
A entry in the neighbour entry (ARP or IPv6 ND)
Definition: neighbour.hpp:27
A representation of an interface in VPP.
Definition: interface.hpp:41
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
void replay(void)
replay the object to create it in hardware
Definition: neighbour.cpp:73
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
boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:193
A cmd class that Dumps all the neighbours.
static void dump(std::ostream &os)
Dump all neighbours into the stream provided.
Definition: neighbour.cpp:122
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:110
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A command class that creates or updates the bridge domain ARP Entry.
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:42
Entries in Tables.
neighbour(const interface &itf, const boost::asio::ip::address &ip_addr, const mac_address_t &mac)
Construct an ARP entry.
Definition: neighbour.cpp:24
static const_iterator_t cend()
Definition: interface.cpp:169
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:34
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
vl_api_mac_address_t mac
Definition: gbp.api:118