Use IP and MAC API types for neighbors
[vpp.git] / extras / vom / vom / neighbour.cpp
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"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22 singular_db<neighbour::key_t, neighbour> neighbour::m_db;
23 neighbour::event_handler neighbour::m_evh;
24
25 neighbour::neighbour(const interface& itf,
26                      const boost::asio::ip::address& ip_addr,
27                      const mac_address_t& mac)
28   : m_hw(false)
29   , m_itf(itf.singular())
30   , m_ip_addr(ip_addr)
31   , m_mac(mac)
32 {
33 }
34
35 neighbour::neighbour(const neighbour& bde)
36   : m_hw(bde.m_hw)
37   , m_itf(bde.m_itf)
38   , m_ip_addr(bde.m_ip_addr)
39   , m_mac(bde.m_mac)
40 {
41 }
42
43 neighbour::~neighbour()
44 {
45   sweep();
46
47   // not in the DB anymore.
48   m_db.release(key(), this);
49 }
50
51 bool
52 neighbour::operator==(const neighbour& n) const
53 {
54   return ((key() == n.key()) && (m_mac == n.m_mac));
55 }
56
57 const neighbour::key_t
58 neighbour::key() const
59 {
60   return (std::make_pair(m_itf->key(), m_ip_addr));
61 }
62
63 void
64 neighbour::sweep()
65 {
66   if (m_hw) {
67     HW::enqueue(
68       new neighbour_cmds::delete_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
69   }
70   HW::write();
71 }
72
73 void
74 neighbour::replay()
75 {
76   if (m_hw) {
77     HW::enqueue(
78       new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
79   }
80 }
81
82 std::string
83 neighbour::to_string() const
84 {
85   std::ostringstream s;
86   s << "neighbour:[" << m_itf->to_string() << ", " << m_mac.to_string() << ", "
87     << m_ip_addr.to_string() << "]";
88
89   return (s.str());
90 }
91
92 void
93 neighbour::update(const neighbour& r)
94 {
95   /*
96  * create the table if it is not yet created
97  */
98   if (rc_t::OK != m_hw.rc()) {
99     HW::enqueue(
100       new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac, m_ip_addr));
101   }
102 }
103
104 std::shared_ptr<neighbour>
105 neighbour::find_or_add(const neighbour& temp)
106 {
107   return (m_db.find_or_add(temp.key(), temp));
108 }
109
110 std::shared_ptr<neighbour>
111 neighbour::find(const key_t& k)
112 {
113   return (m_db.find(k));
114 }
115
116 std::shared_ptr<neighbour>
117 neighbour::singular() const
118 {
119   return find_or_add(*this);
120 }
121
122 void
123 neighbour::dump(std::ostream& os)
124 {
125   db_dump(m_db, os);
126 }
127
128 std::ostream&
129 operator<<(std::ostream& os, const neighbour::key_t& key)
130 {
131   os << "[" << key.first << ", " << key.second << "]";
132
133   return (os);
134 }
135
136 neighbour::event_handler::event_handler()
137 {
138   OM::register_listener(this);
139   inspect::register_handler({ "neighbour" }, "Neighbours", this);
140 }
141
142 void
143 neighbour::event_handler::handle_replay()
144 {
145   m_db.replay();
146 }
147
148 void
149 neighbour::populate_i(const client_db::key_t& key,
150                       std::shared_ptr<interface> itf,
151                       const l3_proto_t& proto)
152 {
153   /*
154    * dump VPP current states
155    */
156   std::shared_ptr<neighbour_cmds::dump_cmd> cmd =
157     std::make_shared<neighbour_cmds::dump_cmd>(
158       neighbour_cmds::dump_cmd(itf->handle(), proto));
159
160   HW::enqueue(cmd);
161   HW::write();
162
163   for (auto& record : *cmd) {
164     /*
165      * construct a neighbour from each recieved record.
166      */
167     auto& payload = record.get_payload();
168
169     mac_address_t mac = from_api(payload.neighbor.mac_address);
170     boost::asio::ip::address ip_addr = from_api(payload.neighbor.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
198 dependency_t
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  */