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