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/route.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/mroute_cmds.hpp"
19 #include "vom/route_api_types.hpp"
20 #include "vom/route_cmds.hpp"
21 #include "vom/singular_db_funcs.hpp"
25 ip_route::event_handler ip_route::m_evh;
26 ip_mroute::event_handler ip_mroute::m_evh;
27 singular_db<ip_route::key_t, ip_route> ip_route::m_db;
28 singular_db<ip_mroute::key_t, ip_mroute> ip_mroute::m_db;
30 const path::special_t path::special_t::STANDARD(0, "standard");
31 const path::special_t path::special_t::LOCAL(1, "local");
32 const path::special_t path::special_t::DROP(2, "standard");
33 const path::special_t path::special_t::UNREACH(3, "unreachable");
34 const path::special_t path::special_t::PROHIBIT(4, "prohibit");
36 path::special_t::special_t(int v, const std::string& s)
37 : enum_base<path::special_t>(v, s)
41 const path::flags_t path::flags_t::NONE(0, "none");
42 const path::flags_t path::flags_t::DVR((1 << 0), "dvr");
44 path::flags_t::flags_t(int v, const std::string& s)
45 : enum_base<path::flags_t>(v, s)
49 const itf_flags_t itf_flags_t::NONE(0, "none");
50 const itf_flags_t itf_flags_t::ACCEPT((1 << 2), "accept");
51 const itf_flags_t itf_flags_t::FORWARD((1 << 3), "forward");
53 itf_flags_t::itf_flags_t(int v, const std::string& s)
54 : enum_base<itf_flags_t>(v, s)
58 itf_flags_t::from_vpp(uint32_t val)
60 if (itf_flags_t::ACCEPT == (int)val)
61 return itf_flags_t::ACCEPT;
63 return itf_flags_t::FORWARD;
66 path::path(special_t special)
68 , m_nh_proto(nh_proto_t::IPV4)
69 , m_flags(flags_t::NONE)
72 , m_interface(nullptr)
78 path::path(const boost::asio::ip::address& nh,
79 const interface& interface,
82 : m_type(special_t::STANDARD)
83 , m_nh_proto(nh_proto_t::from_address(nh))
84 , m_flags(flags_t::NONE)
87 , m_interface(interface.singular())
89 , m_preference(preference)
93 path::path(const route_domain& rd,
94 const boost::asio::ip::address& nh,
97 : m_type(special_t::STANDARD)
98 , m_nh_proto(nh_proto_t::from_address(nh))
99 , m_flags(flags_t::NONE)
101 , m_rd(rd.singular())
102 , m_interface(nullptr)
104 , m_preference(preference)
108 path::path(const interface& interface,
109 const nh_proto_t& proto,
110 const flags_t& flags,
113 : m_type(special_t::STANDARD)
118 , m_interface(interface.singular())
120 , m_preference(preference)
124 path::path(const path& p)
126 , m_nh_proto(p.m_nh_proto)
130 , m_interface(p.m_interface)
131 , m_weight(p.m_weight)
132 , m_preference(p.m_preference)
137 path::operator<(const path& p) const
139 if (m_nh_proto < p.m_nh_proto)
141 if (m_flags < p.m_flags)
143 if (m_type < p.m_type)
149 if (m_rd && p.m_rd) {
150 if (m_rd->table_id() < p.m_rd->table_id())
152 else if (m_rd->table_id() > p.m_rd->table_id())
157 if (m_interface && !p.m_interface)
159 if (!m_interface && p.m_interface)
161 if (m_interface && p.m_interface) {
162 if (m_interface->handle() < p.m_interface->handle())
164 if (p.m_interface->handle() < m_interface->handle())
176 path::operator==(const path& p) const
184 result &= (*m_rd == *p.m_rd);
185 if (m_interface && !p.m_interface)
187 if (!m_interface && p.m_interface)
189 if (m_interface && p.m_interface)
190 result &= (*m_interface == *p.m_interface);
191 return (result && (m_type == p.m_type) && (m_nh == p.m_nh) &&
192 (m_nh_proto == p.m_nh_proto) && (m_flags == p.m_flags));
196 path::to_string() const
198 std::ostringstream s;
201 << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
202 << " flags:" << m_flags.to_string() << " neighbour:" << m_nh.to_string();
204 s << " " << m_rd->to_string();
207 s << " " << m_interface->to_string();
209 s << " weight:" << static_cast<int>(m_weight)
210 << " preference:" << static_cast<int>(m_preference) << "]";
222 path::nh_proto() const
233 const boost::asio::ip::address&
239 std::shared_ptr<route_domain>
245 std::shared_ptr<interface>
258 path::preference() const
263 ip_route::ip_route(const prefix_t& prefix, const path& p)
265 , m_rd(route_domain::get_default())
271 ip_route::ip_route(const prefix_t& prefix)
273 , m_rd(route_domain::get_default())
279 ip_route::ip_route(const ip_route& r)
282 , m_prefix(r.m_prefix)
287 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
289 , m_rd(rd.singular())
295 ip_route::ip_route(const route_domain& rd,
296 const prefix_t& prefix,
299 , m_rd(rd.singular())
305 ip_route::~ip_route()
309 // not in the DB anymore.
310 m_db.release(key(), this);
314 const ip_route::key_t
315 ip_route::key() const
317 return (std::make_pair(m_rd->table_id(), m_prefix));
321 ip_route::operator==(const ip_route& i) const
323 return ((key() == i.key()) && (m_paths == i.m_paths));
327 ip_route::add(const path& path)
329 m_paths.insert(path);
333 ip_route::remove(const path& path)
342 for (auto& p : m_paths)
344 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix, p));
353 for (auto& p : m_paths)
355 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, p));
359 ip_route::to_string() const
361 std::ostringstream s;
362 s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
370 ip_route::update(const ip_route& r)
372 if (rc_t::OK != m_hw.rc()) {
374 * route not yet installed. install each of the desired paths
378 for (auto& p : m_paths)
380 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, p));
383 * add each path that is not installed yet and remove each that is no longer
387 set_difference(r.m_paths.begin(), r.m_paths.end(), m_paths.begin(),
388 m_paths.end(), std::inserter(to_add, to_add.begin()));
390 for (auto& p : to_add)
392 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, p));
395 set_difference(m_paths.begin(), m_paths.end(), r.m_paths.begin(),
396 r.m_paths.end(), std::inserter(to_del, to_del.begin()));
398 for (auto& p : to_del)
400 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix, p));
406 std::shared_ptr<ip_route>
407 ip_route::find_or_add(const ip_route& temp)
409 return (m_db.find_or_add(temp.key(), temp));
412 std::shared_ptr<ip_route>
413 ip_route::find(const key_t& k)
415 return (m_db.find(k));
418 std::shared_ptr<ip_route>
419 ip_route::singular() const
421 return find_or_add(*this);
425 ip_route::dump(std::ostream& os)
430 ip_route::event_handler::event_handler()
432 OM::register_listener(this);
433 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
437 ip_route::event_handler::handle_replay()
443 ip_route::event_handler::handle_populate(const client_db::key_t& key)
445 std::shared_ptr<ip_route_cmds::dump_v4_cmd> cmd_v4 =
446 std::make_shared<ip_route_cmds::dump_v4_cmd>();
447 std::shared_ptr<ip_route_cmds::dump_v6_cmd> cmd_v6 =
448 std::make_shared<ip_route_cmds::dump_v6_cmd>();
454 for (auto& record : *cmd_v4) {
455 auto& payload = record.get_payload();
457 prefix_t pfx(0, payload.address, payload.address_length);
460 * populating the route domain here
462 route_domain rd_temp(payload.table_id);
463 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
465 OM::commit(key, rd_temp);
467 ip_route ip_r(rd_temp, pfx);
469 for (unsigned int i = 0; i < payload.count; i++) {
470 ip_r.add(from_vpp(payload.path[i], nh_proto_t::IPV4));
472 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
475 * Write each of the discovered interfaces into the OM,
476 * but disable the HW Command q whilst we do, so that no
477 * commands are sent to VPP
479 OM::commit(key, ip_r);
482 for (auto& record : *cmd_v6) {
483 auto& payload = record.get_payload();
485 prefix_t pfx(1, payload.address, payload.address_length);
486 route_domain rd_temp(payload.table_id);
487 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
489 OM::commit(key, rd_temp);
491 ip_route ip_r(rd_temp, pfx);
493 for (unsigned int i = 0; i < payload.count; i++) {
494 ip_r.add(from_vpp(payload.path[i], nh_proto_t::IPV6));
496 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
499 * Write each of the discovered interfaces into the OM,
500 * but disable the HW Command q whilst we do, so that no
501 * commands are sent to VPP
503 OM::commit(key, ip_r);
508 ip_route::event_handler::order() const
510 return (dependency_t::TABLE);
514 ip_route::event_handler::show(std::ostream& os)
519 ip_mroute::ip_mroute(const mprefix_t& mprefix)
521 , m_rd(route_domain::get_default())
527 ip_mroute::ip_mroute(const ip_mroute& r)
530 , m_mprefix(r.m_mprefix)
535 ip_mroute::ip_mroute(const route_domain& rd, const mprefix_t& mprefix)
537 , m_rd(rd.singular())
544 ip_mroute::add(const path& path, const itf_flags_t& flag)
546 m_paths.insert(std::make_pair(path, flag));
549 ip_mroute::~ip_mroute()
552 m_db.release(key(), this);
555 const ip_mroute::key_t
556 ip_mroute::key() const
558 return (std::make_pair(m_rd->table_id(), m_mprefix));
562 ip_mroute::operator==(const ip_mroute& i) const
564 return ((key() == i.key()) && (m_paths == i.m_paths));
571 for (auto& p : m_paths)
572 HW::enqueue(new ip_mroute_cmds::delete_cmd(m_hw, m_rd->table_id(),
573 m_mprefix, p.first, p.second));
582 for (auto& p : m_paths)
583 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
584 m_mprefix, p.first, p.second));
588 ip_mroute::to_string() const
590 std::ostringstream s;
591 s << "route:[" << m_rd->to_string() << ", " << m_mprefix.to_string() << " ["
599 ip_mroute::update(const ip_mroute& r)
601 if (rc_t::OK != m_hw.rc()) {
602 for (auto& p : m_paths)
603 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
604 m_mprefix, p.first, p.second));
608 std::shared_ptr<ip_mroute>
609 ip_mroute::find_or_add(const ip_mroute& temp)
611 return (m_db.find_or_add(temp.key(), temp));
614 std::shared_ptr<ip_mroute>
615 ip_mroute::find(const key_t& k)
617 return (m_db.find(k));
620 std::shared_ptr<ip_mroute>
621 ip_mroute::singular() const
623 return find_or_add(*this);
627 ip_mroute::dump(std::ostream& os)
632 ip_mroute::event_handler::event_handler()
634 OM::register_listener(this);
635 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
639 ip_mroute::event_handler::handle_replay()
645 ip_mroute::event_handler::handle_populate(const client_db::key_t& key)
647 std::shared_ptr<ip_mroute_cmds::dump_v4_cmd> cmd_v4 =
648 std::make_shared<ip_mroute_cmds::dump_v4_cmd>();
649 std::shared_ptr<ip_mroute_cmds::dump_v6_cmd> cmd_v6 =
650 std::make_shared<ip_mroute_cmds::dump_v6_cmd>();
656 VOM_LOG(log_level_t::INFO) << "ip-mroute-dump: ";
658 for (auto& record : *cmd_v4) {
659 auto& payload = record.get_payload();
661 ip_address_t gaddr = from_bytes(0, payload.grp_address);
662 ip_address_t saddr = from_bytes(0, payload.src_address);
663 mprefix_t pfx(saddr, gaddr, payload.address_length);
666 * populating the route domain here
668 route_domain rd_temp(payload.table_id);
669 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
671 OM::commit(key, rd_temp);
673 ip_mroute ip_r(rd_temp, pfx);
675 for (unsigned int i = 0; i < payload.count; i++) {
676 vapi_type_mfib_path& p = payload.path[i];
677 ip_r.add(from_vpp(p.path, nh_proto_t::IPV4),
678 itf_flags_t::from_vpp(p.itf_flags));
680 VOM_LOG(log_level_t::INFO) << "ip-mroute-dump: " << ip_r.to_string();
683 * Write each of the discovered interfaces into the OM,
684 * but disable the HW Command q whilst we do, so that no
685 * commands are sent to VPP
687 OM::commit(key, ip_r);
690 for (auto& record : *cmd_v6) {
691 auto& payload = record.get_payload();
693 ip_address_t gaddr = from_bytes(1, payload.grp_address);
694 ip_address_t saddr = from_bytes(1, payload.src_address);
695 mprefix_t pfx(saddr, gaddr, payload.address_length);
697 route_domain rd_temp(payload.table_id);
698 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
700 OM::commit(key, rd_temp);
702 ip_mroute ip_r(rd_temp, pfx);
704 for (unsigned int i = 0; i < payload.count; i++) {
705 vapi_type_mfib_path& p = payload.path[i];
706 ip_r.add(from_vpp(p.path, nh_proto_t::IPV6),
707 itf_flags_t::from_vpp(p.itf_flags));
709 VOM_LOG(log_level_t::INFO) << "ip-mroute-dump: " << ip_r.to_string();
712 * Write each of the discovered interfaces into the OM,
713 * but disable the HW Command q whilst we do, so that no
714 * commands are sent to VPP
716 OM::commit(key, ip_r);
721 ip_mroute::event_handler::order() const
723 return (dependency_t::TABLE);
727 ip_mroute::event_handler::show(std::ostream& os)
733 operator<<(std::ostream& os, const ip_route::key_t& key)
735 os << "[" << key.first << ", " << key.second.to_string() << "]";
741 operator<<(std::ostream& os, const ip_mroute::key_t& key)
743 os << "[" << key.first << ", " << key.second.to_string() << "]";
749 operator<<(std::ostream& os, const path_list_t& key)
753 os << k.to_string() << " ";
761 operator<<(std::ostream& os, const mpath_list_t& key)
765 os << "[" << k.first.to_string() << ", " << k.second.to_string() << "]";
772 }; // namespace route
776 * fd.io coding-style-patch-verification: ON
779 * eval: (c-set-style "mozilla")