L2 BD API to flush all IP-MAC entries in the specified BD
[vpp.git] / extras / vom / 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_funcs.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 const path::flags_t path::flags_t::NONE(0, "none");
37 const path::flags_t path::flags_t::DVR((1 << 0), "dvr");
38
39 path::flags_t::flags_t(int v, const std::string& s)
40   : enum_base<path::flags_t>(v, s)
41 {
42 }
43
44 path::path(special_t special)
45   : m_type(special)
46   , m_nh_proto(nh_proto_t::IPV4)
47   , m_flags(flags_t::NONE)
48   , m_nh()
49   , m_rd(nullptr)
50   , m_interface(nullptr)
51   , m_weight(1)
52   , m_preference(0)
53 {
54 }
55
56 path::path(const boost::asio::ip::address& nh,
57            const interface& interface,
58            uint8_t weight,
59            uint8_t preference)
60   : m_type(special_t::STANDARD)
61   , m_nh_proto(nh_proto_t::from_address(nh))
62   , m_flags(flags_t::NONE)
63   , m_nh(nh)
64   , m_rd(nullptr)
65   , m_interface(interface.singular())
66   , m_weight(weight)
67   , m_preference(preference)
68 {
69 }
70
71 path::path(const route_domain& rd,
72            const boost::asio::ip::address& nh,
73            uint8_t weight,
74            uint8_t preference)
75   : m_type(special_t::STANDARD)
76   , m_nh_proto(nh_proto_t::from_address(nh))
77   , m_flags(flags_t::NONE)
78   , m_nh(nh)
79   , m_rd(rd.singular())
80   , m_interface(nullptr)
81   , m_weight(weight)
82   , m_preference(preference)
83 {
84 }
85
86 path::path(const interface& interface,
87            const nh_proto_t& proto,
88            const flags_t& flags,
89            uint8_t weight,
90            uint8_t preference)
91   : m_type(special_t::STANDARD)
92   , m_nh_proto(proto)
93   , m_flags(flags)
94   , m_nh()
95   , m_rd(nullptr)
96   , m_interface(interface.singular())
97   , m_weight(weight)
98   , m_preference(preference)
99 {
100 }
101
102 path::path(const path& p)
103   : m_type(p.m_type)
104   , m_nh_proto(p.m_nh_proto)
105   , m_flags(p.m_flags)
106   , m_nh(p.m_nh)
107   , m_rd(p.m_rd)
108   , m_interface(p.m_interface)
109   , m_weight(p.m_weight)
110   , m_preference(p.m_preference)
111 {
112 }
113
114 bool
115 path::operator<(const path& p) const
116 {
117   if (m_nh_proto < p.m_nh_proto)
118     return true;
119   if (m_flags < p.m_flags)
120     return true;
121   if (m_type < p.m_type)
122     return true;
123   if (m_rd && !p.m_rd)
124     return false;
125   if (!m_rd && p.m_rd)
126     return true;
127   if (m_rd->table_id() < p.m_rd->table_id())
128     return true;
129   if (m_nh < p.m_nh)
130     return true;
131   if (m_interface && !p.m_interface)
132     return false;
133   if (!m_interface && p.m_interface)
134     return true;
135   if (m_interface->handle() < p.m_interface->handle())
136     return true;
137
138   return (false);
139 }
140
141 path::~path()
142 {
143 }
144
145 bool
146 path::operator==(const path& p) const
147 {
148   bool result = true;
149   if (m_rd && !p.m_rd)
150     return false;
151   if (!m_rd && p.m_rd)
152     return false;
153   if (m_rd && p.m_rd)
154     result &= (*m_rd == *p.m_rd);
155   if (m_interface && !p.m_interface)
156     return false;
157   if (!m_interface && p.m_interface)
158     return false;
159   if (m_interface && p.m_interface)
160     result &= (*m_interface == *p.m_interface);
161   return (result && (m_type == p.m_type) && (m_nh == p.m_nh) &&
162           (m_nh_proto == p.m_nh_proto) && (m_flags == p.m_flags));
163 }
164
165 std::string
166 path::to_string() const
167 {
168   std::ostringstream s;
169
170   s << "path:["
171     << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
172     << " flags:" << m_flags.to_string() << " neighbour:" << m_nh.to_string();
173   if (m_rd) {
174     s << " " << m_rd->to_string();
175   }
176   if (m_interface) {
177     s << " " << m_interface->to_string();
178   }
179   s << " weight:" << static_cast<int>(m_weight)
180     << " preference:" << static_cast<int>(m_preference) << "]";
181
182   return (s.str());
183 }
184
185 path::special_t
186 path::type() const
187 {
188   return m_type;
189 }
190
191 nh_proto_t
192 path::nh_proto() const
193 {
194   return m_nh_proto;
195 }
196
197 path::flags_t
198 path::flags() const
199 {
200   return m_flags;
201 }
202
203 const boost::asio::ip::address&
204 path::nh() const
205 {
206   return m_nh;
207 }
208
209 std::shared_ptr<route_domain>
210 path::rd() const
211 {
212   return m_rd;
213 }
214
215 std::shared_ptr<interface>
216 path::itf() const
217 {
218   return m_interface;
219 }
220
221 uint8_t
222 path::weight() const
223 {
224   return m_weight;
225 }
226
227 uint8_t
228 path::preference() const
229 {
230   return m_preference;
231 }
232
233 ip_route::ip_route(const prefix_t& prefix, const path& p)
234   : m_hw(false)
235   , m_rd(route_domain::get_default())
236   , m_prefix(prefix)
237   , m_paths({ p })
238 {
239 }
240
241 ip_route::ip_route(const prefix_t& prefix)
242   : m_hw(false)
243   , m_rd(route_domain::get_default())
244   , m_prefix(prefix)
245   , m_paths()
246 {
247 }
248
249 ip_route::ip_route(const ip_route& r)
250   : m_hw(r.m_hw)
251   , m_rd(r.m_rd)
252   , m_prefix(r.m_prefix)
253   , m_paths(r.m_paths)
254 {
255 }
256
257 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
258   : m_hw(false)
259   , m_rd(rd.singular())
260   , m_prefix(prefix)
261   , m_paths()
262 {
263 }
264
265 ip_route::ip_route(const route_domain& rd,
266                    const prefix_t& prefix,
267                    const path& p)
268   : m_hw(false)
269   , m_rd(rd.singular())
270   , m_prefix(prefix)
271   , m_paths({ p })
272 {
273 }
274
275 ip_route::~ip_route()
276 {
277   sweep();
278
279   // not in the DB anymore.
280   m_db.release(key(), this);
281   m_paths.clear();
282 }
283
284 const ip_route::key_t
285 ip_route::key() const
286 {
287   return (std::make_pair(m_rd->table_id(), m_prefix));
288 }
289
290 bool
291 ip_route::operator==(const ip_route& i) const
292 {
293   return ((key() == i.key()) && (m_paths == i.m_paths));
294 }
295
296 void
297 ip_route::add(const path& path)
298 {
299   m_paths.insert(path);
300 }
301
302 void
303 ip_route::remove(const path& path)
304 {
305   m_paths.erase(path);
306 }
307
308 void
309 ip_route::sweep()
310 {
311   if (m_hw) {
312     HW::enqueue(
313       new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
314   }
315   HW::write();
316 }
317
318 void
319 ip_route::replay()
320 {
321   if (m_hw) {
322     HW::enqueue(
323       new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
324   }
325 }
326 std::string
327 ip_route::to_string() const
328 {
329   std::ostringstream s;
330   s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
331     << m_paths << "]"
332     << "]";
333
334   return (s.str());
335 }
336
337 void
338 ip_route::update(const ip_route& r)
339 {
340   /*
341 * create the table if it is not yet created
342 */
343   if (rc_t::OK != m_hw.rc()) {
344     HW::enqueue(
345       new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
346   }
347 }
348
349 std::shared_ptr<ip_route>
350 ip_route::find_or_add(const ip_route& temp)
351 {
352   return (m_db.find_or_add(temp.key(), temp));
353 }
354
355 std::shared_ptr<ip_route>
356 ip_route::find(const key_t& k)
357 {
358   return (m_db.find(k));
359 }
360
361 std::shared_ptr<ip_route>
362 ip_route::singular() const
363 {
364   return find_or_add(*this);
365 }
366
367 void
368 ip_route::dump(std::ostream& os)
369 {
370   db_dump(m_db, os);
371 }
372
373 ip_route::event_handler::event_handler()
374 {
375   OM::register_listener(this);
376   inspect::register_handler({ "ip-route" }, "ip route configurations", this);
377 }
378
379 void
380 ip_route::event_handler::handle_replay()
381 {
382   m_db.replay();
383 }
384
385 void
386 ip_route::event_handler::handle_populate(const client_db::key_t& key)
387 {
388   std::shared_ptr<ip_route_cmds::dump_v4_cmd> cmd_v4 =
389     std::make_shared<ip_route_cmds::dump_v4_cmd>();
390   std::shared_ptr<ip_route_cmds::dump_v6_cmd> cmd_v6 =
391     std::make_shared<ip_route_cmds::dump_v6_cmd>();
392
393   HW::enqueue(cmd_v4);
394   HW::enqueue(cmd_v6);
395   HW::write();
396
397   for (auto& record : *cmd_v4) {
398     auto& payload = record.get_payload();
399
400     prefix_t pfx(0, payload.address, payload.address_length);
401
402     /**
403      * populating the route domain here
404      */
405     route_domain rd_temp(payload.table_id);
406     std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
407     if (!rd) {
408       OM::commit(key, rd_temp);
409     }
410     ip_route ip_r(rd_temp, pfx);
411
412     for (unsigned int i = 0; i < payload.count; i++) {
413       vapi_type_fib_path p = payload.path[i];
414       if (p.is_local) {
415         path path_v4(path::special_t::LOCAL);
416         ip_r.add(path_v4);
417       } else if (p.is_drop) {
418         path path_v4(path::special_t::DROP);
419         ip_r.add(path_v4);
420       } else if (p.is_unreach) {
421         path path_v4(path::special_t::UNREACH);
422         ip_r.add(path_v4);
423       } else if (p.is_prohibit) {
424         path path_v4(path::special_t::PROHIBIT);
425         ip_r.add(path_v4);
426       } else {
427         boost::asio::ip::address address = from_bytes(0, p.next_hop);
428         std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
429         if (itf) {
430           if (p.is_dvr) {
431             path path_v4(*itf, nh_proto_t::IPV4, route::path::flags_t::DVR,
432                          p.weight, p.preference);
433             ip_r.add(path_v4);
434           } else {
435             path path_v4(address, *itf, p.weight, p.preference);
436             ip_r.add(path_v4);
437           }
438         } else {
439           path path_v4(rd_temp, address, p.weight, p.preference);
440           ip_r.add(path_v4);
441         }
442       }
443     }
444     VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
445
446     /*
447      * Write each of the discovered interfaces into the OM,
448      * but disable the HW Command q whilst we do, so that no
449      * commands are sent to VPP
450      */
451     OM::commit(key, ip_r);
452   }
453
454   for (auto& record : *cmd_v6) {
455     auto& payload = record.get_payload();
456
457     prefix_t pfx(1, payload.address, payload.address_length);
458     route_domain rd_temp(payload.table_id);
459     std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
460     if (!rd) {
461       OM::commit(key, rd_temp);
462     }
463     ip_route ip_r(rd_temp, pfx);
464
465     for (unsigned int i = 0; i < payload.count; i++) {
466       vapi_type_fib_path p = payload.path[i];
467       if (p.is_local) {
468         path path_v6(path::special_t::LOCAL);
469         ip_r.add(path_v6);
470       } else if (p.is_drop) {
471         path path_v6(path::special_t::DROP);
472         ip_r.add(path_v6);
473       } else if (p.is_unreach) {
474         path path_v6(path::special_t::UNREACH);
475         ip_r.add(path_v6);
476       } else if (p.is_prohibit) {
477         path path_v6(path::special_t::PROHIBIT);
478         ip_r.add(path_v6);
479       } else {
480         std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
481         boost::asio::ip::address address = from_bytes(1, p.next_hop);
482         if (itf) {
483           if (p.is_dvr) {
484             path path_v6(*itf, nh_proto_t::IPV6, route::path::flags_t::DVR,
485                          p.weight, p.preference);
486             ip_r.add(path_v6);
487           } else {
488             path path_v6(address, *itf, p.weight, p.preference);
489             ip_r.add(path_v6);
490           }
491         } else {
492           path path_v6(rd_temp, address, p.weight, p.preference);
493           ip_r.add(path_v6);
494         }
495       }
496     }
497     VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
498
499     /*
500      * Write each of the discovered interfaces into the OM,
501      * but disable the HW Command q whilst we do, so that no
502      * commands are sent to VPP
503      */
504     OM::commit(key, ip_r);
505   }
506 }
507
508 dependency_t
509 ip_route::event_handler::order() const
510 {
511   return (dependency_t::TABLE);
512 }
513
514 void
515 ip_route::event_handler::show(std::ostream& os)
516 {
517   db_dump(m_db, os);
518 }
519
520 std::ostream&
521 operator<<(std::ostream& os, const ip_route::key_t& key)
522 {
523   os << "[" << key.first << ", " << key.second.to_string() << "]";
524
525   return (os);
526 }
527
528 std::ostream&
529 operator<<(std::ostream& os, const path_list_t& key)
530 {
531   os << "[";
532   for (auto k : key) {
533     os << k.to_string() << " ";
534   }
535   os << "]";
536
537   return (os);
538 }
539 }
540 }
541 /*
542  * fd.io coding-style-patch-verification: ON
543  *
544  * Local Variables:
545  * eval: (c-set-style "mozilla")
546  * End:
547  */