VOM: bridge-domain learning mode and route help commands
[vpp.git] / src / vpp-api / vom / route.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/route.hpp"
17 #include "vom/route_cmds.hpp"
18 #include "vom/singular_db.hpp"
19
20 namespace VOM {
21 namespace route {
22 ip_route::event_handler ip_route::m_evh;
23 singular_db<ip_route::key_t, ip_route> ip_route::m_db;
24
25 const path::special_t path::special_t::STANDARD(0, "standard");
26 const path::special_t path::special_t::LOCAL(0, "local");
27 const path::special_t path::special_t::DROP(0, "standard");
28 const path::special_t path::special_t::UNREACH(0, "unreachable");
29 const path::special_t path::special_t::PROHIBIT(0, "prohibit");
30
31 path::special_t::special_t(int v, const std::string& s)
32   : enum_base<path::special_t>(v, s)
33 {
34 }
35
36 path::path(special_t special)
37   : m_type(special)
38   , m_nh_proto(nh_proto_t::IPV4)
39   , m_nh()
40   , m_rd(nullptr)
41   , m_interface(nullptr)
42   , m_weight(1)
43   , m_preference(0)
44 {
45 }
46
47 path::path(const boost::asio::ip::address& nh,
48            const interface& interface,
49            uint8_t weight,
50            uint8_t preference)
51   : m_type(special_t::STANDARD)
52   , m_nh_proto(nh_proto_t::from_address(nh))
53   , m_nh(nh)
54   , m_rd(nullptr)
55   , m_interface(interface.singular())
56   , m_weight(weight)
57   , m_preference(preference)
58 {
59 }
60
61 path::path(const route_domain& rd,
62            const boost::asio::ip::address& nh,
63            uint8_t weight,
64            uint8_t preference)
65   : m_type(special_t::STANDARD)
66   , m_nh_proto(nh_proto_t::from_address(nh))
67   , m_nh(nh)
68   , m_rd(rd.singular())
69   , m_interface(nullptr)
70   , m_weight(weight)
71   , m_preference(preference)
72 {
73 }
74
75 path::path(const interface& interface,
76            const nh_proto_t& proto,
77            uint8_t weight,
78            uint8_t preference)
79   : m_type(special_t::STANDARD)
80   , m_nh_proto(proto)
81   , m_nh()
82   , m_rd(nullptr)
83   , m_interface(interface.singular())
84   , m_weight(weight)
85   , m_preference(preference)
86 {
87 }
88
89 path::path(const path& p)
90   : m_type(p.m_type)
91   , m_nh_proto(p.m_nh_proto)
92   , m_nh(p.m_nh)
93   , m_rd(p.m_rd)
94   , m_interface(p.m_interface)
95   , m_weight(p.m_weight)
96   , m_preference(p.m_preference)
97 {
98 }
99
100 bool
101 path::operator<(const path& p) const
102 {
103   if (m_type < p.m_type)
104     return true;
105   if (m_rd->table_id() < p.m_rd->table_id())
106     return true;
107   if (m_nh < p.m_nh)
108     return true;
109   if (m_interface->handle() < p.m_interface->handle())
110     return true;
111
112   return (false);
113 }
114
115 std::string
116 path::to_string() const
117 {
118   std::ostringstream s;
119
120   s << "path:["
121     << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
122     << " neighbour:" << m_nh.to_string();
123   if (m_rd) {
124     s << " " << m_rd->to_string();
125   }
126   if (m_interface) {
127     s << " " << m_interface->to_string();
128   }
129   s << " weight:" << static_cast<int>(m_weight)
130     << " preference:" << static_cast<int>(m_preference) << "]";
131
132   return (s.str());
133 }
134
135 path::special_t
136 path::type() const
137 {
138   return m_type;
139 }
140
141 nh_proto_t
142 path::nh_proto() const
143 {
144   return m_nh_proto;
145 }
146
147 const boost::asio::ip::address&
148 path::nh() const
149 {
150   return m_nh;
151 }
152
153 std::shared_ptr<route_domain>
154 path::rd() const
155 {
156   return m_rd;
157 }
158
159 std::shared_ptr<interface>
160 path::itf() const
161 {
162   return m_interface;
163 }
164
165 uint8_t
166 path::weight() const
167 {
168   return m_weight;
169 }
170
171 uint8_t
172 path::preference() const
173 {
174   return m_preference;
175 }
176
177 ip_route::ip_route(const prefix_t& prefix)
178   : m_hw(false)
179   , m_rd(route_domain::get_default())
180   , m_prefix(prefix)
181   , m_paths()
182 {
183 }
184
185 ip_route::ip_route(const ip_route& r)
186   : m_hw(r.m_hw)
187   , m_rd(r.m_rd)
188   , m_prefix(r.m_prefix)
189   , m_paths(r.m_paths)
190 {
191 }
192
193 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
194   : m_hw(false)
195   , m_rd(rd.singular())
196   , m_prefix(prefix)
197   , m_paths()
198 {
199 }
200
201 ip_route::~ip_route()
202 {
203   sweep();
204
205   // not in the DB anymore.
206   m_db.release(std::make_pair(m_rd->table_id(), m_prefix), this);
207 }
208
209 void
210 ip_route::add(const path& path)
211 {
212   m_paths.insert(path);
213 }
214
215 void
216 ip_route::remove(const path& path)
217 {
218   m_paths.erase(path);
219 }
220
221 void
222 ip_route::sweep()
223 {
224   if (m_hw) {
225     HW::enqueue(
226       new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
227   }
228   HW::write();
229 }
230
231 void
232 ip_route::replay()
233 {
234   if (m_hw) {
235     HW::enqueue(
236       new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
237   }
238 }
239 std::string
240 ip_route::to_string() const
241 {
242   std::ostringstream s;
243   s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
244     << m_paths << "]"
245     << "]";
246
247   return (s.str());
248 }
249
250 void
251 ip_route::update(const ip_route& r)
252 {
253   /*
254 * create the table if it is not yet created
255 */
256   if (rc_t::OK != m_hw.rc()) {
257     HW::enqueue(
258       new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
259   }
260 }
261
262 std::shared_ptr<ip_route>
263 ip_route::find_or_add(const ip_route& temp)
264 {
265   return (m_db.find_or_add(std::make_pair(temp.m_rd->table_id(), temp.m_prefix),
266                            temp));
267 }
268
269 std::shared_ptr<ip_route>
270 ip_route::singular() const
271 {
272   return find_or_add(*this);
273 }
274
275 void
276 ip_route::dump(std::ostream& os)
277 {
278   m_db.dump(os);
279 }
280
281 ip_route::event_handler::event_handler()
282 {
283   OM::register_listener(this);
284   inspect::register_handler({ "ip-route" }, "ip route configurations", this);
285 }
286
287 void
288 ip_route::event_handler::handle_replay()
289 {
290   m_db.replay();
291 }
292
293 void
294 ip_route::event_handler::handle_populate(const client_db::key_t& key)
295 {
296   std::shared_ptr<ip_route_cmds::dump_v4_cmd> cmd_v4(
297     new ip_route_cmds::dump_v4_cmd());
298   std::shared_ptr<ip_route_cmds::dump_v6_cmd> cmd_v6(
299     new ip_route_cmds::dump_v6_cmd());
300
301   HW::enqueue(cmd_v4);
302   HW::enqueue(cmd_v6);
303   HW::write();
304
305   for (auto& record : *cmd_v4) {
306     auto& payload = record.get_payload();
307
308     prefix_t pfx(0, payload.address, payload.address_length);
309
310     /**
311 * populating the route domain here
312 */
313     route_domain rd_temp(payload.table_id);
314     std::shared_ptr<route_domain> rd = route_domain::find(rd_temp);
315     if (!rd) {
316       OM::commit(key, rd_temp);
317     }
318     ip_route ip_r(rd_temp, pfx);
319
320     for (unsigned int i = 0; i < payload.count; i++) {
321       vapi_type_fib_path p = payload.path[i];
322       if (p.is_local) {
323         path path_v4(path::special_t::LOCAL);
324         ip_r.add(path_v4);
325       } else if (p.is_drop) {
326         path path_v4(path::special_t::DROP);
327         ip_r.add(path_v4);
328       } else if (p.is_unreach) {
329         path path_v4(path::special_t::UNREACH);
330         ip_r.add(path_v4);
331       } else if (p.is_prohibit) {
332         path path_v4(path::special_t::PROHIBIT);
333         ip_r.add(path_v4);
334       } else {
335         std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
336         boost::asio::ip::address address = from_bytes(0, p.next_hop);
337         path path_v4(address, *itf, p.weight, p.preference);
338         ip_r.add(path_v4);
339       }
340     }
341     VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
342
343     /*
344 * Write each of the discovered interfaces into the OM,
345 * but disable the HW Command q whilst we do, so that no
346 * commands are sent to VPP
347 */
348     OM::commit(key, ip_r);
349   }
350
351   for (auto& record : *cmd_v6) {
352     auto& payload = record.get_payload();
353
354     prefix_t pfx(1, payload.address, payload.address_length);
355     route_domain rd_temp(payload.table_id);
356     std::shared_ptr<route_domain> rd = route_domain::find(rd_temp);
357     if (!rd) {
358       OM::commit(key, rd_temp);
359     }
360     ip_route ip_r(rd_temp, pfx);
361
362     for (unsigned int i = 0; i < payload.count; i++) {
363       vapi_type_fib_path p = payload.path[i];
364       if (p.is_local) {
365         path path_v6(path::special_t::LOCAL);
366         ip_r.add(path_v6);
367       } else if (p.is_drop) {
368         path path_v6(path::special_t::DROP);
369         ip_r.add(path_v6);
370       } else if (p.is_unreach) {
371         path path_v6(path::special_t::UNREACH);
372         ip_r.add(path_v6);
373       } else if (p.is_prohibit) {
374         path path_v6(path::special_t::PROHIBIT);
375         ip_r.add(path_v6);
376       } else {
377         std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
378         boost::asio::ip::address address = from_bytes(1, p.next_hop);
379         path path_v6(address, *itf, p.weight, p.preference);
380         ip_r.add(path_v6);
381       }
382     }
383     VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
384
385     /*
386 * Write each of the discovered interfaces into the OM,
387 * but disable the HW Command q whilst we do, so that no
388 * commands are sent to VPP
389 */
390     OM::commit(key, ip_r);
391   }
392 }
393
394 dependency_t
395 ip_route::event_handler::order() const
396 {
397   return (dependency_t::BINDING);
398 }
399
400 void
401 ip_route::event_handler::show(std::ostream& os)
402 {
403   m_db.dump(os);
404 }
405
406 std::ostream&
407 operator<<(std::ostream& os, const ip_route::key_t& key)
408 {
409   os << "[" << key.first << ", " << key.second.to_string() << "]";
410
411   return (os);
412 }
413
414 std::ostream&
415 operator<<(std::ostream& os, const path_list_t& key)
416 {
417   os << "[";
418   for (auto k : key) {
419     os << k.to_string() << " ";
420   }
421   os << "]";
422
423   return (os);
424 }
425 }
426 }
427 /*
428  * fd.io coding-style-patch-verification: ON
429  *
430  * Local Variables:
431  * eval: (c-set-style "mozilla")
432  * End:
433  */