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/route_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
22 ip_route::event_handler ip_route::m_evh;
23 singular_db<ip_route::key_t, ip_route> ip_route::m_db;
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");
31 path::special_t::special_t(int v, const std::string& s)
32 : enum_base<path::special_t>(v, s)
36 const path::flags_t path::flags_t::NONE(0, "none");
37 const path::flags_t path::flags_t::DVR((1 << 0), "dvr");
39 path::flags_t::flags_t(int v, const std::string& s)
40 : enum_base<path::flags_t>(v, s)
44 path::path(special_t special)
46 , m_nh_proto(nh_proto_t::IPV4)
47 , m_flags(flags_t::NONE)
50 , m_interface(nullptr)
56 path::path(const boost::asio::ip::address& nh,
57 const interface& interface,
60 : m_type(special_t::STANDARD)
61 , m_nh_proto(nh_proto_t::from_address(nh))
62 , m_flags(flags_t::NONE)
65 , m_interface(interface.singular())
67 , m_preference(preference)
71 path::path(const route_domain& rd,
72 const boost::asio::ip::address& nh,
75 : m_type(special_t::STANDARD)
76 , m_nh_proto(nh_proto_t::from_address(nh))
77 , m_flags(flags_t::NONE)
80 , m_interface(nullptr)
82 , m_preference(preference)
86 path::path(const interface& interface,
87 const nh_proto_t& proto,
91 : m_type(special_t::STANDARD)
96 , m_interface(interface.singular())
98 , m_preference(preference)
102 path::path(const path& p)
104 , m_nh_proto(p.m_nh_proto)
108 , m_interface(p.m_interface)
109 , m_weight(p.m_weight)
110 , m_preference(p.m_preference)
115 path::operator<(const path& p) const
117 if (m_nh_proto < p.m_nh_proto)
119 if (m_flags < p.m_flags)
121 if (m_type < p.m_type)
127 if (m_rd->table_id() < p.m_rd->table_id())
131 if (m_interface && !p.m_interface)
133 if (!m_interface && p.m_interface)
135 if (m_interface->handle() < p.m_interface->handle())
146 path::operator==(const path& p) const
154 result &= (*m_rd == *p.m_rd);
155 if (m_interface && !p.m_interface)
157 if (!m_interface && p.m_interface)
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));
166 path::to_string() const
168 std::ostringstream s;
171 << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
172 << " flags:" << m_flags.to_string() << " neighbour:" << m_nh.to_string();
174 s << " " << m_rd->to_string();
177 s << " " << m_interface->to_string();
179 s << " weight:" << static_cast<int>(m_weight)
180 << " preference:" << static_cast<int>(m_preference) << "]";
192 path::nh_proto() const
203 const boost::asio::ip::address&
209 std::shared_ptr<route_domain>
215 std::shared_ptr<interface>
228 path::preference() const
233 ip_route::ip_route(const prefix_t& prefix, const path& p)
235 , m_rd(route_domain::get_default())
241 ip_route::ip_route(const prefix_t& prefix)
243 , m_rd(route_domain::get_default())
249 ip_route::ip_route(const ip_route& r)
252 , m_prefix(r.m_prefix)
257 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
259 , m_rd(rd.singular())
265 ip_route::ip_route(const route_domain& rd,
266 const prefix_t& prefix,
269 , m_rd(rd.singular())
275 ip_route::~ip_route()
279 // not in the DB anymore.
280 m_db.release(key(), this);
284 const ip_route::key_t
285 ip_route::key() const
287 return (std::make_pair(m_rd->table_id(), m_prefix));
291 ip_route::operator==(const ip_route& i) const
293 return ((key() == i.key()) && (m_paths == i.m_paths));
297 ip_route::add(const path& path)
299 m_paths.insert(path);
303 ip_route::remove(const path& path)
313 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
323 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
327 ip_route::to_string() const
329 std::ostringstream s;
330 s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
338 ip_route::update(const ip_route& r)
341 * create the table if it is not yet created
343 if (rc_t::OK != m_hw.rc()) {
345 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
349 std::shared_ptr<ip_route>
350 ip_route::find_or_add(const ip_route& temp)
352 return (m_db.find_or_add(temp.key(), temp));
355 std::shared_ptr<ip_route>
356 ip_route::find(const key_t& k)
358 return (m_db.find(k));
361 std::shared_ptr<ip_route>
362 ip_route::singular() const
364 return find_or_add(*this);
368 ip_route::dump(std::ostream& os)
373 ip_route::event_handler::event_handler()
375 OM::register_listener(this);
376 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
380 ip_route::event_handler::handle_replay()
386 ip_route::event_handler::handle_populate(const client_db::key_t& key)
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>();
397 for (auto& record : *cmd_v4) {
398 auto& payload = record.get_payload();
400 prefix_t pfx(0, payload.address, payload.address_length);
403 * populating the route domain here
405 route_domain rd_temp(payload.table_id);
406 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
408 OM::commit(key, rd_temp);
410 ip_route ip_r(rd_temp, pfx);
412 for (unsigned int i = 0; i < payload.count; i++) {
413 vapi_type_fib_path p = payload.path[i];
415 path path_v4(path::special_t::LOCAL);
417 } else if (p.is_drop) {
418 path path_v4(path::special_t::DROP);
420 } else if (p.is_unreach) {
421 path path_v4(path::special_t::UNREACH);
423 } else if (p.is_prohibit) {
424 path path_v4(path::special_t::PROHIBIT);
427 boost::asio::ip::address address = from_bytes(0, p.next_hop);
428 std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
431 path path_v4(*itf, nh_proto_t::IPV4, route::path::flags_t::DVR,
432 p.weight, p.preference);
435 path path_v4(address, *itf, p.weight, p.preference);
439 path path_v4(rd_temp, address, p.weight, p.preference);
444 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
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
451 OM::commit(key, ip_r);
454 for (auto& record : *cmd_v6) {
455 auto& payload = record.get_payload();
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);
461 OM::commit(key, rd_temp);
463 ip_route ip_r(rd_temp, pfx);
465 for (unsigned int i = 0; i < payload.count; i++) {
466 vapi_type_fib_path p = payload.path[i];
468 path path_v6(path::special_t::LOCAL);
470 } else if (p.is_drop) {
471 path path_v6(path::special_t::DROP);
473 } else if (p.is_unreach) {
474 path path_v6(path::special_t::UNREACH);
476 } else if (p.is_prohibit) {
477 path path_v6(path::special_t::PROHIBIT);
480 std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
481 boost::asio::ip::address address = from_bytes(1, p.next_hop);
484 path path_v6(*itf, nh_proto_t::IPV6, route::path::flags_t::DVR,
485 p.weight, p.preference);
488 path path_v6(address, *itf, p.weight, p.preference);
492 path path_v6(rd_temp, address, p.weight, p.preference);
497 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
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
504 OM::commit(key, ip_r);
509 ip_route::event_handler::order() const
511 return (dependency_t::TABLE);
515 ip_route::event_handler::show(std::ostream& os)
521 operator<<(std::ostream& os, const ip_route::key_t& key)
523 os << "[" << key.first << ", " << key.second.to_string() << "]";
529 operator<<(std::ostream& os, const path_list_t& key)
533 os << k.to_string() << " ";
542 * fd.io coding-style-patch-verification: ON
545 * eval: (c-set-style "mozilla")