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/api_types.hpp"
18 #include "vom/neighbour_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
22 singular_db<neighbour::key_t, neighbour> neighbour::m_db;
23 neighbour::event_handler neighbour::m_evh;
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");
29 neighbour::flags_t::flags_t(int v, const std::string s)
34 neighbour::neighbour(const interface& itf,
35 const boost::asio::ip::address& ip_addr,
36 const mac_address_t& mac,
39 , m_itf(itf.singular())
46 neighbour::neighbour(const neighbour& n)
49 , m_ip_addr(n.m_ip_addr)
55 neighbour::~neighbour()
59 // not in the DB anymore.
60 m_db.release(key(), this);
64 neighbour::operator==(const neighbour& n) const
66 return ((key() == n.key()) && (m_mac == n.m_mac));
69 const neighbour::key_t
70 neighbour::key() const
72 return (std::make_pair(m_itf->key(), m_ip_addr));
79 HW::enqueue(new neighbour_cmds::delete_cmd(m_hw, m_itf->handle(), m_mac,
89 HW::enqueue(new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac,
95 neighbour::to_string() const
98 s << "neighbour:[" << m_itf->to_string() << ", " << m_mac.to_string() << ", "
99 << m_ip_addr.to_string() << " " << m_flags.to_string() << "]";
105 neighbour::update(const neighbour& r)
108 * create the table if it is not yet created
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));
116 std::shared_ptr<neighbour>
117 neighbour::find_or_add(const neighbour& temp)
119 return (m_db.find_or_add(temp.key(), temp));
122 std::shared_ptr<neighbour>
123 neighbour::find(const key_t& k)
125 return (m_db.find(k));
128 std::shared_ptr<neighbour>
129 neighbour::singular() const
131 return find_or_add(*this);
135 neighbour::dump(std::ostream& os)
141 operator<<(std::ostream& os, const neighbour::key_t& key)
143 os << "[" << key.first << ", " << key.second << "]";
148 neighbour::event_handler::event_handler()
150 OM::register_listener(this);
151 inspect::register_handler({ "neighbour" }, "Neighbours", this);
155 neighbour::event_handler::handle_replay()
161 neighbour::populate_i(const client_db::key_t& key,
162 std::shared_ptr<interface> itf,
163 const l3_proto_t& proto)
166 * dump VPP current states
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));
175 for (auto& record : *cmd) {
177 * construct a neighbour from each recieved record.
179 auto& payload = record.get_payload();
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);
187 VOM_LOG(log_level_t::DEBUG) << "neighbour-dump: " << itf->to_string() << " "
188 << mac.to_string() << " " << ip_addr.to_string()
189 << " " << f.to_string();
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
201 neighbour::event_handler::handle_populate(const client_db::key_t& key)
203 auto it = interface::cbegin();
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);
214 neighbour::event_handler::order() const
216 return (dependency_t::ENTRY);
220 neighbour::event_handler::show(std::ostream& os)
227 * fd.io coding-style-patch-verification: ON
230 * eval: (c-set-style "mozilla")