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)
343 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
353 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
357 ip_route::to_string() const
359 std::ostringstream s;
360 s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
368 ip_route::update(const ip_route& r)
371 * create the table if it is not yet created
373 if (rc_t::OK != m_hw.rc()) {
375 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
379 std::shared_ptr<ip_route>
380 ip_route::find_or_add(const ip_route& temp)
382 return (m_db.find_or_add(temp.key(), temp));
385 std::shared_ptr<ip_route>
386 ip_route::find(const key_t& k)
388 return (m_db.find(k));
391 std::shared_ptr<ip_route>
392 ip_route::singular() const
394 return find_or_add(*this);
398 ip_route::dump(std::ostream& os)
403 ip_route::event_handler::event_handler()
405 OM::register_listener(this);
406 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
410 ip_route::event_handler::handle_replay()
416 ip_route::event_handler::handle_populate(const client_db::key_t& key)
418 std::shared_ptr<ip_route_cmds::dump_v4_cmd> cmd_v4 =
419 std::make_shared<ip_route_cmds::dump_v4_cmd>();
420 std::shared_ptr<ip_route_cmds::dump_v6_cmd> cmd_v6 =
421 std::make_shared<ip_route_cmds::dump_v6_cmd>();
427 for (auto& record : *cmd_v4) {
428 auto& payload = record.get_payload();
430 prefix_t pfx(0, payload.address, payload.address_length);
433 * populating the route domain here
435 route_domain rd_temp(payload.table_id);
436 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
438 OM::commit(key, rd_temp);
440 ip_route ip_r(rd_temp, pfx);
442 for (unsigned int i = 0; i < payload.count; i++) {
443 ip_r.add(from_vpp(payload.path[i], nh_proto_t::IPV4));
445 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
448 * Write each of the discovered interfaces into the OM,
449 * but disable the HW Command q whilst we do, so that no
450 * commands are sent to VPP
452 OM::commit(key, ip_r);
455 for (auto& record : *cmd_v6) {
456 auto& payload = record.get_payload();
458 prefix_t pfx(1, payload.address, payload.address_length);
459 route_domain rd_temp(payload.table_id);
460 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
462 OM::commit(key, rd_temp);
464 ip_route ip_r(rd_temp, pfx);
466 for (unsigned int i = 0; i < payload.count; i++) {
467 ip_r.add(from_vpp(payload.path[i], nh_proto_t::IPV6));
469 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
472 * Write each of the discovered interfaces into the OM,
473 * but disable the HW Command q whilst we do, so that no
474 * commands are sent to VPP
476 OM::commit(key, ip_r);
481 ip_route::event_handler::order() const
483 return (dependency_t::TABLE);
487 ip_route::event_handler::show(std::ostream& os)
492 ip_mroute::ip_mroute(const mprefix_t& mprefix)
494 , m_rd(route_domain::get_default())
500 ip_mroute::ip_mroute(const ip_mroute& r)
503 , m_mprefix(r.m_mprefix)
508 ip_mroute::ip_mroute(const route_domain& rd, const mprefix_t& mprefix)
510 , m_rd(rd.singular())
517 ip_mroute::add(const path& path, const itf_flags_t& flag)
519 m_paths.insert(std::make_pair(path, flag));
522 ip_mroute::~ip_mroute()
525 m_db.release(key(), this);
528 const ip_mroute::key_t
529 ip_mroute::key() const
531 return (std::make_pair(m_rd->table_id(), m_mprefix));
535 ip_mroute::operator==(const ip_mroute& i) const
537 return ((key() == i.key()) && (m_paths == i.m_paths));
544 for (auto& p : m_paths)
545 HW::enqueue(new ip_mroute_cmds::delete_cmd(m_hw, m_rd->table_id(),
546 m_mprefix, p.first, p.second));
555 for (auto& p : m_paths)
556 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
557 m_mprefix, p.first, p.second));
561 ip_mroute::to_string() const
563 std::ostringstream s;
564 s << "route:[" << m_rd->to_string() << ", " << m_mprefix.to_string() << " ["
572 ip_mroute::update(const ip_mroute& r)
574 if (rc_t::OK != m_hw.rc()) {
575 for (auto& p : m_paths)
576 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
577 m_mprefix, p.first, p.second));
581 std::shared_ptr<ip_mroute>
582 ip_mroute::find_or_add(const ip_mroute& temp)
584 return (m_db.find_or_add(temp.key(), temp));
587 std::shared_ptr<ip_mroute>
588 ip_mroute::find(const key_t& k)
590 return (m_db.find(k));
593 std::shared_ptr<ip_mroute>
594 ip_mroute::singular() const
596 return find_or_add(*this);
600 ip_mroute::dump(std::ostream& os)
605 ip_mroute::event_handler::event_handler()
607 OM::register_listener(this);
608 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
612 ip_mroute::event_handler::handle_replay()
618 ip_mroute::event_handler::handle_populate(const client_db::key_t& key)
620 std::shared_ptr<ip_mroute_cmds::dump_v4_cmd> cmd_v4 =
621 std::make_shared<ip_mroute_cmds::dump_v4_cmd>();
622 std::shared_ptr<ip_mroute_cmds::dump_v6_cmd> cmd_v6 =
623 std::make_shared<ip_mroute_cmds::dump_v6_cmd>();
629 VOM_LOG(log_level_t::INFO) << "ip-mroute-dump: ";
631 for (auto& record : *cmd_v4) {
632 auto& payload = record.get_payload();
634 ip_address_t gaddr = from_bytes(0, payload.grp_address);
635 ip_address_t saddr = from_bytes(0, payload.src_address);
636 mprefix_t pfx(saddr, gaddr, payload.address_length);
639 * populating the route domain here
641 route_domain rd_temp(payload.table_id);
642 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
644 OM::commit(key, rd_temp);
646 ip_mroute ip_r(rd_temp, pfx);
648 for (unsigned int i = 0; i < payload.count; i++) {
649 vapi_type_mfib_path& p = payload.path[i];
650 ip_r.add(from_vpp(p.path, nh_proto_t::IPV4),
651 itf_flags_t::from_vpp(p.itf_flags));
653 VOM_LOG(log_level_t::INFO) << "ip-mroute-dump: " << ip_r.to_string();
656 * Write each of the discovered interfaces into the OM,
657 * but disable the HW Command q whilst we do, so that no
658 * commands are sent to VPP
660 OM::commit(key, ip_r);
663 for (auto& record : *cmd_v6) {
664 auto& payload = record.get_payload();
666 ip_address_t gaddr = from_bytes(1, payload.grp_address);
667 ip_address_t saddr = from_bytes(1, payload.src_address);
668 mprefix_t pfx(saddr, gaddr, payload.address_length);
670 route_domain rd_temp(payload.table_id);
671 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
673 OM::commit(key, rd_temp);
675 ip_mroute ip_r(rd_temp, pfx);
677 for (unsigned int i = 0; i < payload.count; i++) {
678 vapi_type_mfib_path& p = payload.path[i];
679 ip_r.add(from_vpp(p.path, nh_proto_t::IPV6),
680 itf_flags_t::from_vpp(p.itf_flags));
682 VOM_LOG(log_level_t::INFO) << "ip-mroute-dump: " << ip_r.to_string();
685 * Write each of the discovered interfaces into the OM,
686 * but disable the HW Command q whilst we do, so that no
687 * commands are sent to VPP
689 OM::commit(key, ip_r);
694 ip_mroute::event_handler::order() const
696 return (dependency_t::TABLE);
700 ip_mroute::event_handler::show(std::ostream& os)
706 operator<<(std::ostream& os, const ip_route::key_t& key)
708 os << "[" << key.first << ", " << key.second.to_string() << "]";
714 operator<<(std::ostream& os, const ip_mroute::key_t& key)
716 os << "[" << key.first << ", " << key.second.to_string() << "]";
722 operator<<(std::ostream& os, const path_list_t& key)
726 os << k.to_string() << " ";
734 operator<<(std::ostream& os, const mpath_list_t& key)
738 os << "[" << k.first.to_string() << ", " << k.second.to_string() << "]";
745 }; // namespace route
749 * fd.io coding-style-patch-verification: ON
752 * eval: (c-set-style "mozilla")