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.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 path::path(special_t special)
38 , m_nh_proto(nh_proto_t::IPV4)
41 , m_interface(nullptr)
47 path::path(const boost::asio::ip::address& nh,
48 const interface& interface,
51 : m_type(special_t::STANDARD)
52 , m_nh_proto(nh_proto_t::from_address(nh))
55 , m_interface(interface.singular())
57 , m_preference(preference)
61 path::path(const route_domain& rd,
62 const boost::asio::ip::address& nh,
65 : m_type(special_t::STANDARD)
66 , m_nh_proto(nh_proto_t::from_address(nh))
69 , m_interface(nullptr)
71 , m_preference(preference)
75 path::path(const interface& interface,
76 const nh_proto_t& proto,
79 : m_type(special_t::STANDARD)
83 , m_interface(interface.singular())
85 , m_preference(preference)
89 path::path(const path& p)
91 , m_nh_proto(p.m_nh_proto)
94 , m_interface(p.m_interface)
95 , m_weight(p.m_weight)
96 , m_preference(p.m_preference)
101 path::operator<(const path& p) const
103 if (m_type < p.m_type)
109 if (m_rd->table_id() < p.m_rd->table_id())
113 if (m_interface && !p.m_interface)
115 if (!m_interface && p.m_interface)
117 if (m_interface->handle() < p.m_interface->handle())
128 path::operator==(const path& p) const
136 result &= (*m_rd == *p.m_rd);
137 if (m_interface && !p.m_interface)
139 if (!m_interface && p.m_interface)
141 if (m_interface && p.m_interface)
142 result &= (*m_interface == *p.m_interface);
143 return (result && (m_type == p.m_type) && (m_nh == p.m_nh));
147 path::to_string() const
149 std::ostringstream s;
152 << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
153 << " neighbour:" << m_nh.to_string();
155 s << " " << m_rd->to_string();
158 s << " " << m_interface->to_string();
160 s << " weight:" << static_cast<int>(m_weight)
161 << " preference:" << static_cast<int>(m_preference) << "]";
173 path::nh_proto() const
178 const boost::asio::ip::address&
184 std::shared_ptr<route_domain>
190 std::shared_ptr<interface>
203 path::preference() const
208 ip_route::ip_route(const prefix_t& prefix, const path& p)
210 , m_rd(route_domain::get_default())
216 ip_route::ip_route(const prefix_t& prefix)
218 , m_rd(route_domain::get_default())
224 ip_route::ip_route(const ip_route& r)
227 , m_prefix(r.m_prefix)
232 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
234 , m_rd(rd.singular())
240 ip_route::ip_route(const route_domain& rd,
241 const prefix_t& prefix,
244 , m_rd(rd.singular())
250 ip_route::~ip_route()
254 // not in the DB anymore.
255 m_db.release(key(), this);
259 const ip_route::key_t
260 ip_route::key() const
262 return (std::make_pair(m_rd->table_id(), m_prefix));
266 ip_route::operator==(const ip_route& i) const
268 return ((key() == i.key()) && (m_paths == i.m_paths));
272 ip_route::add(const path& path)
274 m_paths.insert(path);
278 ip_route::remove(const path& path)
288 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
298 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
302 ip_route::to_string() const
304 std::ostringstream s;
305 s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
313 ip_route::update(const ip_route& r)
316 * create the table if it is not yet created
318 if (rc_t::OK != m_hw.rc()) {
320 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
324 std::shared_ptr<ip_route>
325 ip_route::find_or_add(const ip_route& temp)
327 return (m_db.find_or_add(temp.key(), temp));
330 std::shared_ptr<ip_route>
331 ip_route::find(const key_t& k)
333 return (m_db.find(k));
336 std::shared_ptr<ip_route>
337 ip_route::singular() const
339 return find_or_add(*this);
343 ip_route::dump(std::ostream& os)
348 ip_route::event_handler::event_handler()
350 OM::register_listener(this);
351 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
355 ip_route::event_handler::handle_replay()
361 ip_route::event_handler::handle_populate(const client_db::key_t& key)
363 std::shared_ptr<ip_route_cmds::dump_v4_cmd> cmd_v4 =
364 std::make_shared<ip_route_cmds::dump_v4_cmd>();
365 std::shared_ptr<ip_route_cmds::dump_v6_cmd> cmd_v6 =
366 std::make_shared<ip_route_cmds::dump_v6_cmd>();
372 for (auto& record : *cmd_v4) {
373 auto& payload = record.get_payload();
375 prefix_t pfx(0, payload.address, payload.address_length);
378 * populating the route domain here
380 route_domain rd_temp(payload.table_id);
381 std::shared_ptr<route_domain> rd = route_domain::find(rd_temp);
383 OM::commit(key, rd_temp);
385 ip_route ip_r(rd_temp, pfx);
387 for (unsigned int i = 0; i < payload.count; i++) {
388 vapi_type_fib_path p = payload.path[i];
390 path path_v4(path::special_t::LOCAL);
392 } else if (p.is_drop) {
393 path path_v4(path::special_t::DROP);
395 } else if (p.is_unreach) {
396 path path_v4(path::special_t::UNREACH);
398 } else if (p.is_prohibit) {
399 path path_v4(path::special_t::PROHIBIT);
402 std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
403 boost::asio::ip::address address = from_bytes(0, p.next_hop);
404 path path_v4(address, *itf, p.weight, p.preference);
408 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
411 * Write each of the discovered interfaces into the OM,
412 * but disable the HW Command q whilst we do, so that no
413 * commands are sent to VPP
415 OM::commit(key, ip_r);
418 for (auto& record : *cmd_v6) {
419 auto& payload = record.get_payload();
421 prefix_t pfx(1, payload.address, payload.address_length);
422 route_domain rd_temp(payload.table_id);
423 std::shared_ptr<route_domain> rd = route_domain::find(rd_temp);
425 OM::commit(key, rd_temp);
427 ip_route ip_r(rd_temp, pfx);
429 for (unsigned int i = 0; i < payload.count; i++) {
430 vapi_type_fib_path p = payload.path[i];
432 path path_v6(path::special_t::LOCAL);
434 } else if (p.is_drop) {
435 path path_v6(path::special_t::DROP);
437 } else if (p.is_unreach) {
438 path path_v6(path::special_t::UNREACH);
440 } else if (p.is_prohibit) {
441 path path_v6(path::special_t::PROHIBIT);
444 std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
445 boost::asio::ip::address address = from_bytes(1, p.next_hop);
446 path path_v6(address, *itf, p.weight, p.preference);
450 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
453 * Write each of the discovered interfaces into the OM,
454 * but disable the HW Command q whilst we do, so that no
455 * commands are sent to VPP
457 OM::commit(key, ip_r);
462 ip_route::event_handler::order() const
464 return (dependency_t::BINDING);
468 ip_route::event_handler::show(std::ostream& os)
474 operator<<(std::ostream& os, const ip_route::key_t& key)
476 os << "[" << key.first << ", " << key.second.to_string() << "]";
482 operator<<(std::ostream& os, const path_list_t& key)
486 os << k.to_string() << " ";
495 * fd.io coding-style-patch-verification: ON
498 * eval: (c-set-style "mozilla")