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 #ifndef __VOM_ROUTE_H__
17 #define __VOM_ROUTE_H__
19 #include "vom/interface.hpp"
20 #include "vom/prefix.hpp"
21 #include "vom/route_domain.hpp"
22 #include "vom/singular_db.hpp"
26 * Types belonging to Routing
30 * A path for IP or MPLS routes
38 class special_t : public enum_base<special_t>
42 * A standard path type. this includes path types
43 * that use the next-hop and interface
45 const static special_t STANDARD;
48 * A local/for-us/recieve
50 const static special_t LOCAL;
55 const static special_t DROP;
58 * a path will return ICMP unreachables
60 const static special_t UNREACH;
63 * a path will return ICMP prohibit
65 const static special_t PROHIBIT;
69 * Private constructor taking the value and the string name
71 special_t(int v, const std::string& s);
77 class flags_t : public enum_base<flags_t>
83 const static flags_t NONE;
86 * A path that resolves via a DVR next-hop
88 const static flags_t DVR;
92 * Private constructor taking the value and the string name
94 flags_t(int v, const std::string& s);
98 * constructor for special paths
100 path(special_t special, const nh_proto_t& proto = nh_proto_t::IPV4);
103 * Constructor for standard non-recursive paths
105 path(const boost::asio::ip::address& nh,
106 const interface& interface,
108 uint8_t preference = 0);
111 * Constructor for standard recursive paths
113 path(const route_domain& rd,
114 const boost::asio::ip::address& nh,
116 uint8_t preference = 0);
119 * Constructor for DVR paths or attached paths.
121 path(const interface& interface,
122 const nh_proto_t& proto,
123 const flags_t& flags = flags_t::NONE,
125 uint8_t preference = 0);
138 * comparison operator
140 bool operator==(const path& p) const;
143 * Less than operator for set insertion
145 bool operator<(const path& p) const;
148 * convert to string format for debug purposes
150 std::string to_string() const;
155 special_t type() const;
156 nh_proto_t nh_proto() const;
157 flags_t flags() const;
158 const boost::asio::ip::address& nh() const;
159 std::shared_ptr<route_domain> rd() const;
160 std::shared_ptr<interface> itf() const;
161 uint8_t weight() const;
162 uint8_t preference() const;
166 * The special path tpye
171 * The next-hop protocol
173 nh_proto_t m_nh_proto;
183 boost::asio::ip::address m_nh;
186 * For recursive routes, this is the table in which the
187 * the next-hop exists.
189 std::shared_ptr<route_domain> m_rd;
192 * The next-hop interface [if present].
194 std::shared_ptr<interface> m_interface;
204 uint8_t m_preference;
207 class itf_flags_t : public enum_base<itf_flags_t>
210 const static itf_flags_t NONE;
212 * Path is accepting multicast traffic
214 const static itf_flags_t ACCEPT;
217 * A local/for-us/recieve
219 const static itf_flags_t FORWARD;
221 static const itf_flags_t& from_vpp(uint32_t val);
225 * Private constructor taking the value and the string name
227 itf_flags_t(int v, const std::string& s);
231 * A path-list is a set of paths
233 typedef std::set<path> path_list_t;
236 * A mpath-list is a set of paths and interface flags
238 typedef std::set<std::pair<path, itf_flags_t>> mpath_list_t;
241 * ostream output for iterator
243 std::ostream& operator<<(std::ostream& os, const path_list_t& path_list);
244 std::ostream& operator<<(std::ostream& os, const mpath_list_t& path_list);
249 class ip_route : public object_base
253 * The key for a route
255 typedef std::pair<route::table_id_t, prefix_t> key_t;
258 * Construct a route in the default table
260 ip_route(const prefix_t& prefix);
263 * Construct a route with a path
265 ip_route(const prefix_t& prefix, const path& p);
270 ip_route(const ip_route& r);
273 * Construct a route in the given route domain
275 ip_route(const route_domain& rd, const prefix_t& prefix);
278 * Construct a route in the given route domain with a path
280 ip_route(const route_domain& rd, const prefix_t& prefix, const path& p);
288 * Get the route's key
290 const key_t key() const;
293 * Comparison operator
295 bool operator==(const ip_route& i) const;
298 * Return the matching 'singular instance'
300 std::shared_ptr<ip_route> singular() const;
305 void add(const path& path);
310 void remove(const path& path);
313 * Find the instnace of the route domain in the OM
315 static std::shared_ptr<ip_route> find(const ip_route& temp);
318 * Dump all route-doamin into the stream provided
320 static void dump(std::ostream& os);
323 * replay the object to create it in hardware
328 * Convert to string for debugging
330 std::string to_string() const;
333 * Return the matching 'singular instance'
335 static std::shared_ptr<ip_route> find(const key_t& k);
339 * Class definition for listeners to OM events
341 class event_handler : public OM::listener, public inspect::command_handler
345 virtual ~event_handler() = default;
348 * Handle a populate event
350 void handle_populate(const client_db::key_t& key);
353 * Handle a replay event
355 void handle_replay();
358 * Show the object in the Singular DB
360 void show(std::ostream& os);
363 * Get the sortable Id of the listener
365 dependency_t order() const;
369 * event_handler to register with OM
371 static event_handler m_evh;
374 * Find or add the instnace of the route domain in the OM
376 static std::shared_ptr<ip_route> find_or_add(const ip_route& temp);
379 * It's the OM class that updates the objects in HW
381 friend class VOM::OM;
384 * It's the singular_db class that calls replay()
386 friend class singular_db<key_t, ip_route>;
389 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
391 void update(const ip_route& obj);
394 * Sweep/reap the object if still stale
399 * HW configuration for the result of creating the route
404 * The route domain the route is in.
406 std::shared_ptr<route_domain> m_rd;
409 * The prefix to match
419 * A map of all routes
421 static singular_db<key_t, ip_route> m_db;
425 * A IP multicast route
427 class ip_mroute : public object_base
431 * The key for a route
433 typedef std::pair<route::table_id_t, mprefix_t> key_t;
436 * Construct a route in the default table
438 ip_mroute(const mprefix_t& mprefix);
443 ip_mroute(const ip_mroute& r);
446 * Construct a route in the given route domain
448 ip_mroute(const route_domain& rd, const mprefix_t& mprefix);
456 * Get the route's key
458 const key_t key() const;
461 * Comparison operator
463 bool operator==(const ip_mroute& i) const;
466 * Return the matching 'singular instance'
468 std::shared_ptr<ip_mroute> singular() const;
471 * Find the instnace of the route domain in the OM
473 static std::shared_ptr<ip_mroute> find(const ip_mroute& temp);
476 * Dump all route-doamin into the stream provided
478 static void dump(std::ostream& os);
481 * replay the object to create it in hardware
486 * Convert to string for debugging
488 std::string to_string() const;
491 * Return the matching 'singular instance'
493 static std::shared_ptr<ip_mroute> find(const key_t& k);
495 void add(const path& path, const itf_flags_t& flag);
499 * Class definition for listeners to OM events
501 class event_handler : public OM::listener, public inspect::command_handler
505 virtual ~event_handler() = default;
508 * Handle a populate event
510 void handle_populate(const client_db::key_t& key);
513 * Handle a replay event
515 void handle_replay();
518 * Show the object in the Singular DB
520 void show(std::ostream& os);
523 * Get the sortable Id of the listener
525 dependency_t order() const;
529 * event_handler to register with OM
531 static event_handler m_evh;
534 * Find or add the instnace of the route domain in the OM
536 static std::shared_ptr<ip_mroute> find_or_add(const ip_mroute& temp);
539 * It's the OM class that updates the objects in HW
541 friend class VOM::OM;
544 * It's the singular_db class that calls replay()
546 friend class singular_db<key_t, ip_mroute>;
549 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
551 void update(const ip_mroute& obj);
554 * Sweep/reap the object if still stale
559 * HW configuration for the result of creating the route
564 * The route domain the route is in.
566 std::shared_ptr<route_domain> m_rd;
569 * The mprefix to match
576 mpath_list_t m_paths;
579 * A map of all routes
581 static singular_db<key_t, ip_mroute> m_db;
584 std::ostream& operator<<(std::ostream& os, const ip_route::key_t& key);
585 std::ostream& operator<<(std::ostream& os, const ip_mroute::key_t& key);
586 }; // namespace route
590 * fd.io coding-style-patch-verification: ON
593 * eval: (c-set-style "mozilla")