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]));
438 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
441 * Write each of the discovered interfaces into the OM,
442 * but disable the HW Command q whilst we do, so that no
443 * commands are sent to VPP
445 OM::commit(key, ip_r);
453 ip_route::event_handler::order() const
455 return (dependency_t::TABLE);
459 ip_route::event_handler::show(std::ostream& os)
464 ip_mroute::ip_mroute(const mprefix_t& mprefix)
466 , m_rd(route_domain::get_default())
472 ip_mroute::ip_mroute(const ip_mroute& r)
475 , m_mprefix(r.m_mprefix)
480 ip_mroute::ip_mroute(const route_domain& rd, const mprefix_t& mprefix)
482 , m_rd(rd.singular())
489 ip_mroute::add(const path& path, const itf_flags_t& flag)
491 m_paths.insert(std::make_pair(path, flag));
494 ip_mroute::~ip_mroute()
497 m_db.release(key(), this);
500 const ip_mroute::key_t
501 ip_mroute::key() const
503 return (std::make_pair(m_rd->table_id(), m_mprefix));
507 ip_mroute::operator==(const ip_mroute& i) const
509 return ((key() == i.key()) && (m_paths == i.m_paths));
516 for (auto& p : m_paths)
517 HW::enqueue(new ip_mroute_cmds::delete_cmd(m_hw, m_rd->table_id(),
518 m_mprefix, p.first, p.second));
527 for (auto& p : m_paths)
528 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
529 m_mprefix, p.first, p.second));
533 ip_mroute::to_string() const
535 std::ostringstream s;
536 s << "route:[" << m_rd->to_string() << ", " << m_mprefix.to_string() << " ["
544 ip_mroute::update(const ip_mroute& r)
546 if (rc_t::OK != m_hw.rc()) {
547 for (auto& p : m_paths)
548 HW::enqueue(new ip_mroute_cmds::update_cmd(m_hw, m_rd->table_id(),
549 m_mprefix, p.first, p.second));
553 std::shared_ptr<ip_mroute>
554 ip_mroute::find_or_add(const ip_mroute& temp)
556 return (m_db.find_or_add(temp.key(), temp));
559 std::shared_ptr<ip_mroute>
560 ip_mroute::find(const key_t& k)
562 return (m_db.find(k));
565 std::shared_ptr<ip_mroute>
566 ip_mroute::singular() const
568 return find_or_add(*this);
572 ip_mroute::dump(std::ostream& os)
577 ip_mroute::event_handler::event_handler()
579 OM::register_listener(this);
580 inspect::register_handler({ "ip-mroute" },
581 "ip multicast route configurations", this);
585 ip_mroute::event_handler::handle_replay()
591 ip_mroute::event_handler::handle_populate(const client_db::key_t& key)
593 // for each known route-domain
594 auto it = route_domain::cbegin();
596 while (it != route_domain::cend()) {
598 std::vector<l3_proto_t> l3s = { l3_proto_t::IPV4, l3_proto_t::IPV4 };
600 for (auto l3 : l3s) {
601 std::shared_ptr<ip_mroute_cmds::dump_cmd> cmd =
602 std::make_shared<ip_mroute_cmds::dump_cmd>(
603 it->second.lock()->table_id(), l3);
608 VOM_LOG(log_level_t::DEBUG) << "ip-mroute-dump: ";
610 for (auto& record : *cmd) {
611 auto& payload = record.get_payload();
613 std::shared_ptr<route_domain> rd =
614 route_domain::find(payload.route.table_id);
620 mprefix_t pfx = from_api(payload.route.prefix);
621 ip_mroute ip_r(*rd, pfx);
623 for (unsigned int i = 0; i < payload.route.n_paths; i++) {
624 ip_r.add(from_api(payload.route.paths[i].path),
625 from_api(payload.route.paths[i].itf_flags));
628 VOM_LOG(log_level_t::DEBUG) << "ip-mroute-dump: " << ip_r.to_string();
631 * Write each of the discovered interfaces into the OM,
632 * but disable the HW Command q whilst we do, so that no
633 * commands are sent to VPP
635 OM::commit(key, ip_r);
643 ip_mroute::event_handler::order() const
645 return (dependency_t::TABLE);
649 ip_mroute::event_handler::show(std::ostream& os)
655 operator<<(std::ostream& os, const ip_route::key_t& key)
657 os << "[" << key.first << ", " << key.second.to_string() << "]";
663 operator<<(std::ostream& os, const ip_mroute::key_t& key)
665 os << "[" << key.first << ", " << key.second.to_string() << "]";
671 operator<<(std::ostream& os, const path_list_t& key)
675 os << k.to_string() << " ";
683 operator<<(std::ostream& os, const mpath_list_t& key)
687 os << "[" << k.first.to_string() << ", " << k.second.to_string() << "]";
694 }; // namespace route
698 * fd.io coding-style-patch-verification: ON
701 * eval: (c-set-style "mozilla")