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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/neighbour.hpp"
17 #include "vom/neighbour_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
21 singular_db<neighbour::key_t, neighbour> neighbour::m_db;
22 neighbour::event_handler neighbour::m_evh;
24 neighbour::neighbour(const interface& itf,
25 const boost::asio::ip::address& ip_addr,
26 const mac_address_t& mac)
28 , m_itf(itf.singular())
34 neighbour::neighbour(const neighbour& bde)
37 , m_ip_addr(bde.m_ip_addr)
42 neighbour::~neighbour()
46 // not in the DB anymore.
47 m_db.release(key(), this);
51 neighbour::operator==(const neighbour& n) const
53 return ((key() == n.key()) && (m_mac == n.m_mac));
56 const neighbour::key_t
57 neighbour::key() const
59 return (std::make_pair(m_itf->key(), m_ip_addr));
67 new neighbour_cmds::delete_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
77 new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
82 neighbour::to_string() const
85 s << "neighbour:[" << m_itf->to_string() << ", " << m_mac.to_string() << ", "
86 << m_ip_addr.to_string() << "]";
92 neighbour::update(const neighbour& r)
95 * create the table if it is not yet created
97 if (rc_t::OK != m_hw.rc()) {
99 new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
103 std::shared_ptr<neighbour>
104 neighbour::find_or_add(const neighbour& temp)
106 return (m_db.find_or_add(temp.key(), temp));
109 std::shared_ptr<neighbour>
110 neighbour::find(const key_t& k)
112 return (m_db.find(k));
115 std::shared_ptr<neighbour>
116 neighbour::singular() const
118 return find_or_add(*this);
122 neighbour::dump(std::ostream& os)
128 operator<<(std::ostream& os, const neighbour::key_t& key)
130 os << "[" << key.first << ", " << key.second << "]";
135 neighbour::event_handler::event_handler()
137 OM::register_listener(this);
138 inspect::register_handler({ "neighbour" }, "Neighbours", this);
142 neighbour::event_handler::handle_replay()
148 neighbour::populate_i(const client_db::key_t& key,
149 std::shared_ptr<interface> itf,
150 const l3_proto_t& proto)
153 * dump VPP current states
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));
162 for (auto& record : *cmd) {
164 * construct a neighbour from each recieved record.
166 auto& payload = record.get_payload();
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);
173 VOM_LOG(log_level_t::DEBUG) << "neighbour-dump: " << itf->to_string()
174 << mac.to_string() << ip_addr.to_string();
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
186 neighbour::event_handler::handle_populate(const client_db::key_t& key)
188 auto it = interface::cbegin();
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);
199 neighbour::event_handler::order() const
201 return (dependency_t::ENTRY);
205 neighbour::event_handler::show(std::ostream& os)
212 * fd.io coding-style-patch-verification: ON
215 * eval: (c-set-style "mozilla")