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 << 1), "accept");
51 const itf_flags_t itf_flags_t::FORWARD((1 << 2), "forward");
53 itf_flags_t::itf_flags_t(int v, const std::string& s)
54 : enum_base<itf_flags_t>(v, s)
58 path::path(special_t special, const nh_proto_t& proto)
61 , m_flags(flags_t::NONE)
64 , m_interface(nullptr)
70 path::path(const boost::asio::ip::address& nh,
71 const interface& interface,
74 : m_type(special_t::STANDARD)
75 , m_nh_proto(nh_proto_t::from_address(nh))
76 , m_flags(flags_t::NONE)
79 , m_interface(interface.singular())
81 , m_preference(preference)
85 path::path(const route_domain& rd,
86 const boost::asio::ip::address& nh,
89 : m_type(special_t::STANDARD)
90 , m_nh_proto(nh_proto_t::from_address(nh))
91 , m_flags(flags_t::NONE)
94 , m_interface(nullptr)
96 , m_preference(preference)
100 path::path(const interface& interface,
101 const nh_proto_t& proto,
102 const flags_t& flags,
105 : m_type(special_t::STANDARD)
110 , m_interface(interface.singular())
112 , m_preference(preference)
116 path::path(const path& p)
118 , m_nh_proto(p.m_nh_proto)
122 , m_interface(p.m_interface)
123 , m_weight(p.m_weight)
124 , m_preference(p.m_preference)
129 path::operator<(const path& p) const
131 if (m_nh_proto < p.m_nh_proto)
133 if (m_flags < p.m_flags)
135 if (m_type < p.m_type)
141 if (m_rd && p.m_rd) {
142 if (m_rd->table_id() < p.m_rd->table_id())
144 else if (m_rd->table_id() > p.m_rd->table_id())
149 if (m_interface && !p.m_interface)
151 if (!m_interface && p.m_interface)
153 if (m_interface && p.m_interface) {
154 if (m_interface->handle() < p.m_interface->handle())
156 if (p.m_interface->handle() < m_interface->handle())
168 path::operator==(const path& p) const
176 result &= (*m_rd == *p.m_rd);
177 if (m_interface && !p.m_interface)
179 if (!m_interface && p.m_interface)
181 if (m_interface && p.m_interface)
182 result &= (*m_interface == *p.m_interface);
183 return (result && (m_type == p.m_type) && (m_nh == p.m_nh) &&
184 (m_nh_proto == p.m_nh_proto) && (m_flags == p.m_flags));
188 path::to_string() const
190 std::ostringstream s;
193 << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
194 << " flags:" << m_flags.to_string() << " neighbour:" << m_nh.to_string();
196 s << " " << m_rd->to_string();
199 s << " " << m_interface->to_string();
201 s << " weight:" << static_cast<int>(m_weight)
202 << " preference:" << static_cast<int>(m_preference) << "]";
214 path::nh_proto() const
225 const boost::asio::ip::address&
231 std::shared_ptr<route_domain>
237 std::shared_ptr<interface>
250 path::preference() const
255 ip_route::ip_route(const prefix_t& prefix, const path& p)
257 , m_rd(route_domain::get_default())
263 ip_route::ip_route(const prefix_t& prefix)
265 , m_rd(route_domain::get_default())
271 ip_route::ip_route(const ip_route& r)
274 , m_prefix(r.m_prefix)
279 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
281 , m_rd(rd.singular())
287 ip_route::ip_route(const route_domain& rd,
288 const prefix_t& prefix,
291 , m_rd(rd.singular())
297 ip_route::~ip_route()
301 // not in the DB anymore.
302 m_db.release(key(), this);
306 const ip_route::key_t
307 ip_route::key() const
309 return (std::make_pair(m_rd->table_id(), m_prefix));
313 ip_route::operator==(const ip_route& i) const
315 return ((key() == i.key()) && (m_paths == i.m_paths));
319 ip_route::add(const path& path)
321 m_paths.insert(path);
325 ip_route::remove(const path& path)
335 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
345 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
349 ip_route::to_string() const
351 std::ostringstream s;
352 s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
360 ip_route::update(const ip_route& r)
364 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
367 std::shared_ptr<ip_route>
368 ip_route::find_or_add(const ip_route& temp)
370 return (m_db.find_or_add(temp.key(), temp));
373 std::shared_ptr<ip_route>
374 ip_route::find(const key_t& k)
376 return (m_db.find(k));
379 std::shared_ptr<ip_route>
380 ip_route::singular() const
382 return find_or_add(*this);
386 ip_route::dump(std::ostream& os)
391 ip_route::event_handler::event_handler()
393 OM::register_listener(this);
394 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
398 ip_route::event_handler::handle_replay()
404 ip_route::event_handler::handle_populate(const client_db::key_t& key)
406 // for each known route-domain
407 auto it = route_domain::cbegin();
409 while (it != route_domain::cend()) {
411 std::vector<l3_proto_t> l3s = { l3_proto_t::IPV4, l3_proto_t::IPV4 };
413 for (auto l3 : l3s) {
414 std::shared_ptr<ip_route_cmds::dump_cmd> cmd =
415 std::make_shared<ip_route_cmds::dump_cmd>(it->second.lock()->table_id(),
421 for (auto& record : *cmd) {
422 auto& payload = record.get_payload();
424 std::shared_ptr<route_domain> rd =
425 route_domain::find(payload.route.table_id);
431 prefix_t pfx = from_api(payload.route.prefix);
432 ip_route ip_r(*rd, pfx);
434 for (unsigned int i = 0; i < payload.route.n_paths; i++) {
435 ip_r.add(from_api(payload.route.paths[i]));
437 // vapi_type_fib_path& p = payload.route.paths[i];
438 /* if (p.is_local) { */
439 /* path path_v4(path::special_t::LOCAL); */
440 /* ip_r.add(path_v4); */
442 /* } else if (p.is_drop) { */
443 /* path path_v4(path::special_t::DROP); */
444 /* ip_r.add(path_v4); */
445 /* } else if (p.is_unreach) { */
446 /* path path_v4(path::special_t::UNREACH); */
447 /* ip_r.add(path_v4); */
448 /* } else if (p.is_prohibit) { */
449 /* path path_v4(path::special_t::PROHIBIT); */
450 /* ip_r.add(path_v4); */
452 /* boost::asio::ip::address address = from_bytes(0, p.next_hop);
454 /* std::shared_ptr<interface> itf =
455 * interface::find(p.sw_if_index); */
457 /* if (p.is_dvr) { */
458 /* path path_v4(*itf, nh_proto_t::IPV4,
459 * route::path::flags_t::DVR,
461 /* p.weight, p.preference); */
462 /* ip_r.add(path_v4); */
464 /* path path_v4(address, *itf, p.weight, p.preference); */
465 /* ip_r.add(path_v4); */
468 /* path path_v4(rd_temp, address, p.weight, p.preference); */
469 /* ip_r.add(path_v4); */
474 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
477 * Write each of the discovered interfaces into the OM,
478 * but disable the HW Command q whilst we do, so that no
479 * commands are sent to VPP
481 OM::commit(key, ip_r);
488 ip_route::event_handler::order() const
490 return (dependency_t::TABLE);
494 ip_route::event_handler::show(std::ostream& os)
499 ip_mroute::ip_mroute(const mprefix_t& mprefix)
501 , m_rd(route_domain::get_default())
507 ip_mroute::ip_mroute(const ip_mroute& r)
510 , m_mprefix(r.m_mprefix)
515 ip_mroute::ip_mroute(const route_domain& rd, const mprefix_t& mprefix)
517 , m_rd(rd.singular())
524 ip_mroute::add(const path& path, const itf_flags_t& flag)
526 m_paths.insert(std::make_pair(path, flag));
529 ip_mroute::~ip_mroute()
532 m_db.release(key(), this);
535 const ip_mroute::key_t
536 ip_mroute::key() const
538 return (std::make_pair(m_rd->table_id(), m_mprefix));
542 ip_mroute::operator==(const ip_mroute& i) const
544 return ((key() == i.key()) && (m_paths == i.m_paths));
551 for (auto& p : m_paths)
552 HW::enqueue(new ip_mroute_cmds::delete_cmd(m_hw, m_rd->table_id(),
553 m_mprefix, p.first, p.second));
562 for (auto& p : m_paths)
563 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
564 m_mprefix, p.first, p.second));
568 ip_mroute::to_string() const
570 std::ostringstream s;
571 s << "route:[" << m_rd->to_string() << ", " << m_mprefix.to_string() << " ["
579 ip_mroute::update(const ip_mroute& r)
581 if (rc_t::OK != m_hw.rc()) {
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 std::shared_ptr<ip_mroute>
589 ip_mroute::find_or_add(const ip_mroute& temp)
591 return (m_db.find_or_add(temp.key(), temp));
594 std::shared_ptr<ip_mroute>
595 ip_mroute::find(const key_t& k)
597 return (m_db.find(k));
600 std::shared_ptr<ip_mroute>
601 ip_mroute::singular() const
603 return find_or_add(*this);
607 ip_mroute::dump(std::ostream& os)
612 ip_mroute::event_handler::event_handler()
614 OM::register_listener(this);
615 inspect::register_handler({ "ip-mroute" },
616 "ip multicast route configurations", this);
620 ip_mroute::event_handler::handle_replay()
626 ip_mroute::event_handler::handle_populate(const client_db::key_t& key)
628 // for each known route-domain
629 auto it = route_domain::cbegin();
631 while (it != route_domain::cend()) {
633 std::vector<l3_proto_t> l3s = { l3_proto_t::IPV4, l3_proto_t::IPV4 };
635 for (auto l3 : l3s) {
636 std::shared_ptr<ip_mroute_cmds::dump_cmd> cmd =
637 std::make_shared<ip_mroute_cmds::dump_cmd>(
638 it->second.lock()->table_id(), l3);
643 VOM_LOG(log_level_t::DEBUG) << "ip-mroute-dump: ";
645 for (auto& record : *cmd) {
646 auto& payload = record.get_payload();
648 std::shared_ptr<route_domain> rd =
649 route_domain::find(payload.route.table_id);
655 mprefix_t pfx = from_api(payload.route.prefix);
656 ip_mroute ip_r(*rd, pfx);
658 for (unsigned int i = 0; i < payload.route.n_paths; i++) {
659 ip_r.add(from_api(payload.route.paths[i].path),
660 from_api(payload.route.paths[i].itf_flags));
663 VOM_LOG(log_level_t::DEBUG) << "ip-mroute-dump: " << ip_r.to_string();
666 * Write each of the discovered interfaces into the OM,
667 * but disable the HW Command q whilst we do, so that no
668 * commands are sent to VPP
670 OM::commit(key, ip_r);
677 ip_mroute::event_handler::order() const
679 return (dependency_t::TABLE);
683 ip_mroute::event_handler::show(std::ostream& os)
689 operator<<(std::ostream& os, const ip_route::key_t& key)
691 os << "[" << key.first << ", " << key.second.to_string() << "]";
697 operator<<(std::ostream& os, const ip_mroute::key_t& key)
699 os << "[" << key.first << ", " << key.second.to_string() << "]";
705 operator<<(std::ostream& os, const path_list_t& key)
709 os << k.to_string() << " ";
717 operator<<(std::ostream& os, const mpath_list_t& key)
721 os << "[" << k.first.to_string() << ", " << k.second.to_string() << "]";
728 }; // namespace route
732 * fd.io coding-style-patch-verification: ON
735 * eval: (c-set-style "mozilla")