vxlan: vxlan/vxlan.api API cleanup
[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 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");
28
29 neighbour::flags_t::flags_t(int v, const std::string s)
30   : enum_base(v, s)
31 {
32 }
33
34 neighbour::neighbour(const interface& itf,
35                      const boost::asio::ip::address& ip_addr,
36                      const mac_address_t& mac,
37                      const flags_t flags)
38   : m_hw(false)
39   , m_itf(itf.singular())
40   , m_ip_addr(ip_addr)
41   , m_mac(mac)
42   , m_flags(flags)
43 {
44 }
45
46 neighbour::neighbour(const neighbour& n)
47   : m_hw(n.m_hw)
48   , m_itf(n.m_itf)
49   , m_ip_addr(n.m_ip_addr)
50   , m_mac(n.m_mac)
51   , m_flags(n.m_flags)
52 {
53 }
54
55 neighbour::~neighbour()
56 {
57   sweep();
58
59   // not in the DB anymore.
60   m_db.release(key(), this);
61 }
62
63 bool
64 neighbour::operator==(const neighbour& n) const
65 {
66   return ((key() == n.key()) && (m_mac == n.m_mac));
67 }
68
69 const neighbour::key_t
70 neighbour::key() const
71 {
72   return (std::make_pair(m_itf->key(), m_ip_addr));
73 }
74
75 void
76 neighbour::sweep()
77 {
78   if (m_hw) {
79     HW::enqueue(new neighbour_cmds::delete_cmd(m_hw, m_itf->handle(), m_mac,
80                                                m_ip_addr, m_flags));
81   }
82   HW::write();
83 }
84
85 void
86 neighbour::replay()
87 {
88   if (m_hw) {
89     HW::enqueue(new neighbour_cmds::create_cmd(m_hw, m_itf->handle(), m_mac,
90                                                m_ip_addr, m_flags));
91   }
92 }
93
94 std::string
95 neighbour::to_string() const
96 {
97   std::ostringstream s;
98   s << "neighbour:[" << m_itf->to_string() << ", " << m_mac.to_string() << ", "
99     << m_ip_addr.to_string() << " " << m_flags.to_string() << "]";
100
101   return (s.str());
102 }
103
104 void
105 neighbour::update(const neighbour& r)
106 {
107   /*
108  * create the table if it is not yet created
109  */
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));
113   }
114 }
115
116 std::shared_ptr<neighbour>
117 neighbour::find_or_add(const neighbour& temp)
118 {
119   return (m_db.find_or_add(temp.key(), temp));
120 }
121
122 std::shared_ptr<neighbour>
123 neighbour::find(const key_t& k)
124 {
125   return (m_db.find(k));
126 }
127
128 std::shared_ptr<neighbour>
129 neighbour::singular() const
130 {
131   return find_or_add(*this);
132 }
133
134 void
135 neighbour::dump(std::ostream& os)
136 {
137   db_dump(m_db, os);
138 }
139
140 std::ostream&
141 operator<<(std::ostream& os, const neighbour::key_t& key)
142 {
143   os << "[" << key.first << ", " << key.second << "]";
144
145   return (os);
146 }
147
148 neighbour::event_handler::event_handler()
149 {
150   OM::register_listener(this);
151   inspect::register_handler({ "neighbour" }, "Neighbours", this);
152 }
153
154 void
155 neighbour::event_handler::handle_replay()
156 {
157   m_db.replay();
158 }
159
160 void
161 neighbour::populate_i(const client_db::key_t& key,
162                       std::shared_ptr<interface> itf,
163                       const l3_proto_t& proto)
164 {
165   /*
166    * dump VPP current states
167    */
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));
171
172   HW::enqueue(cmd);
173   HW::write();
174
175   for (auto& record : *cmd) {
176     /*
177      * construct a neighbour from each recieved record.
178      */
179     auto& payload = record.get_payload();
180
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);
185     ;
186
187     VOM_LOG(log_level_t::DEBUG) << "neighbour-dump: " << itf->to_string() << " "
188                                 << mac.to_string() << " " << ip_addr.to_string()
189                                 << " " << f.to_string();
190
191     /*
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
195      */
196     OM::commit(key, n);
197   }
198 }
199
200 void
201 neighbour::event_handler::handle_populate(const client_db::key_t& key)
202 {
203   auto it = interface::cbegin();
204
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);
208
209     ++it;
210   }
211 }
212
213 dependency_t
214 neighbour::event_handler::order() const
215 {
216   return (dependency_t::ENTRY);
217 }
218
219 void
220 neighbour::event_handler::show(std::ostream& os)
221 {
222   db_dump(m_db, os);
223 }
224 }
225
226 /*
227  * fd.io coding-style-patch-verification: ON
228  *
229  * Local Variables:
230  * eval: (c-set-style "mozilla")
231  * End:
232  */