FD.io VPP  v19.04-6-g6f05f72
Vector Packet Processing
route.hpp
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
16 #ifndef __VOM_ROUTE_H__
17 #define __VOM_ROUTE_H__
18 
19 #include "vom/interface.hpp"
20 #include "vom/prefix.hpp"
21 #include "vom/route_domain.hpp"
22 #include "vom/singular_db.hpp"
23 
24 namespace VOM {
25 /**
26  * Types belonging to Routing
27  */
28 namespace route {
29 /**
30  * A path for IP or MPLS routes
31  */
32 class path
33 {
34 public:
35  /**
36  * Special path types
37  */
38  class special_t : public enum_base<special_t>
39  {
40  public:
41  /**
42  * A standard path type. this includes path types
43  * that use the next-hop and interface
44  */
45  const static special_t STANDARD;
46 
47  /**
48  * A local/for-us/recieve
49  */
50  const static special_t LOCAL;
51 
52  /**
53  * drop path
54  */
55  const static special_t DROP;
56 
57  /**
58  * a path will return ICMP unreachables
59  */
60  const static special_t UNREACH;
61 
62  /**
63  * a path will return ICMP prohibit
64  */
65  const static special_t PROHIBIT;
66 
67  private:
68  /**
69  * Private constructor taking the value and the string name
70  */
71  special_t(int v, const std::string& s);
72  };
73 
74  /**
75  * Path flags
76  */
77  class flags_t : public enum_base<flags_t>
78  {
79  public:
80  /**
81  * No flags
82  */
83  const static flags_t NONE;
84 
85  /**
86  * A path that resolves via a DVR next-hop
87  */
88  const static flags_t DVR;
89 
90  private:
91  /**
92  * Private constructor taking the value and the string name
93  */
94  flags_t(int v, const std::string& s);
95  };
96 
97  /**
98  * constructor for special paths
99  */
100  path(special_t special, const nh_proto_t& proto = nh_proto_t::IPV4);
101 
102  /**
103  * Constructor for standard non-recursive paths
104  */
106  const interface& interface,
107  uint8_t weight = 1,
108  uint8_t preference = 0);
109 
110  /**
111  * Constructor for standard recursive paths
112  */
113  path(const route_domain& rd,
115  uint8_t weight = 1,
116  uint8_t preference = 0);
117 
118  /**
119  * Constructor for DVR paths or attached paths.
120  */
121  path(const interface& interface,
122  const nh_proto_t& proto,
123  const flags_t& flags = flags_t::NONE,
124  uint8_t weight = 1,
125  uint8_t preference = 0);
126 
127  /**
128  * Copy Constructor
129  */
130  path(const path& p);
131 
132  /**
133  * Destructor
134  */
135  ~path();
136 
137  /**
138  * comparison operator
139  */
140  bool operator==(const path& p) const;
141 
142  /**
143  * Less than operator for set insertion
144  */
145  bool operator<(const path& p) const;
146 
147  /**
148  * convert to string format for debug purposes
149  */
150  std::string to_string() const;
151 
152  /**
153  * Getters
154  */
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;
163 
164 private:
165  /**
166  * The special path tpye
167  */
168  special_t m_type;
169 
170  /**
171  * The next-hop protocol
172  */
173  nh_proto_t m_nh_proto;
174 
175  /**
176  * Flags for the path
177  */
178  flags_t m_flags;
179 
180  /**
181  * The next-hop
182  */
184 
185  /**
186  * For recursive routes, this is the table in which the
187  * the next-hop exists.
188  */
189  std::shared_ptr<route_domain> m_rd;
190 
191  /**
192  * The next-hop interface [if present].
193  */
194  std::shared_ptr<interface> m_interface;
195 
196  /**
197  * UCMP weight
198  */
199  uint8_t m_weight;
200 
201  /**
202  * Path preference
203  */
204  uint8_t m_preference;
205 };
206 
207 class itf_flags_t : public enum_base<itf_flags_t>
208 {
209 public:
210  const static itf_flags_t NONE;
211  /**
212  * Path is accepting multicast traffic
213  */
214  const static itf_flags_t ACCEPT;
215 
216  /**
217  * A local/for-us/recieve
218  */
219  const static itf_flags_t FORWARD;
220 
221  static const itf_flags_t& from_vpp(uint32_t val);
222 
223 private:
224  /**
225  * Private constructor taking the value and the string name
226  */
227  itf_flags_t(int v, const std::string& s);
228 };
229 
230 /**
231  * A path-list is a set of paths
232  */
233 typedef std::set<path> path_list_t;
234 
235 /**
236  * A mpath-list is a set of paths and interface flags
237  */
238 typedef std::set<std::pair<path, itf_flags_t>> mpath_list_t;
239 
240 /**
241  * ostream output for iterator
242  */
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);
245 
246 /**
247  * A IP route
248  */
249 class ip_route : public object_base
250 {
251 public:
252  /**
253  * The key for a route
254  */
255  typedef std::pair<route::table_id_t, prefix_t> key_t;
256 
257  /**
258  * Construct a route in the default table
259  */
260  ip_route(const prefix_t& prefix);
261 
262  /**
263  * Construct a route with a path
264  */
265  ip_route(const prefix_t& prefix, const path& p);
266 
267  /**
268  * Copy Construct
269  */
270  ip_route(const ip_route& r);
271 
272  /**
273  * Construct a route in the given route domain
274  */
275  ip_route(const route_domain& rd, const prefix_t& prefix);
276 
277  /**
278  * Construct a route in the given route domain with a path
279  */
280  ip_route(const route_domain& rd, const prefix_t& prefix, const path& p);
281 
282  /**
283  * Destructor
284  */
285  ~ip_route();
286 
287  /**
288  * Get the route's key
289  */
290  const key_t key() const;
291 
292  /**
293  * Comparison operator
294  */
295  bool operator==(const ip_route& i) const;
296 
297  /**
298  * Return the matching 'singular instance'
299  */
300  std::shared_ptr<ip_route> singular() const;
301 
302  /**
303  * Add a path.
304  */
305  void add(const path& path);
306 
307  /**
308  * remove a path.
309  */
310  void remove(const path& path);
311 
312  /**
313  * Find the instnace of the route domain in the OM
314  */
315  static std::shared_ptr<ip_route> find(const ip_route& temp);
316 
317  /**
318  * Dump all route-doamin into the stream provided
319  */
320  static void dump(std::ostream& os);
321 
322  /**
323  * replay the object to create it in hardware
324  */
325  void replay(void);
326 
327  /**
328  * Convert to string for debugging
329  */
330  std::string to_string() const;
331 
332  /**
333  * Return the matching 'singular instance'
334  */
335  static std::shared_ptr<ip_route> find(const key_t& k);
336 
337 private:
338  /**
339  * Class definition for listeners to OM events
340  */
342  {
343  public:
344  event_handler();
345  virtual ~event_handler() = default;
346 
347  /**
348  * Handle a populate event
349  */
350  void handle_populate(const client_db::key_t& key);
351 
352  /**
353  * Handle a replay event
354  */
355  void handle_replay();
356 
357  /**
358  * Show the object in the Singular DB
359  */
360  void show(std::ostream& os);
361 
362  /**
363  * Get the sortable Id of the listener
364  */
365  dependency_t order() const;
366  };
367 
368  /**
369  * event_handler to register with OM
370  */
371  static event_handler m_evh;
372 
373  /**
374  * Find or add the instnace of the route domain in the OM
375  */
376  static std::shared_ptr<ip_route> find_or_add(const ip_route& temp);
377 
378  /*
379  * It's the OM class that updates the objects in HW
380  */
381  friend class VOM::OM;
382 
383  /**
384  * It's the singular_db class that calls replay()
385  */
386  friend class singular_db<key_t, ip_route>;
387 
388  /**
389  * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
390  */
391  void update(const ip_route& obj);
392 
393  /**
394  * Sweep/reap the object if still stale
395  */
396  void sweep(void);
397 
398  /**
399  * HW configuration for the result of creating the route
400  */
401  HW::item<bool> m_hw;
402 
403  /**
404  * The route domain the route is in.
405  */
406  std::shared_ptr<route_domain> m_rd;
407 
408  /**
409  * The prefix to match
410  */
411  prefix_t m_prefix;
412 
413  /**
414  * The set of paths
415  */
416  path_list_t m_paths;
417 
418  /**
419  * A map of all routes
420  */
421  static singular_db<key_t, ip_route> m_db;
422 };
423 
424 /**
425  * A IP multicast route
426  */
427 class ip_mroute : public object_base
428 {
429 public:
430  /**
431  * The key for a route
432  */
433  typedef std::pair<route::table_id_t, mprefix_t> key_t;
434 
435  /**
436  * Construct a route in the default table
437  */
438  ip_mroute(const mprefix_t& mprefix);
439 
440  /**
441  * Copy Construct
442  */
443  ip_mroute(const ip_mroute& r);
444 
445  /**
446  * Construct a route in the given route domain
447  */
448  ip_mroute(const route_domain& rd, const mprefix_t& mprefix);
449 
450  /**
451  * Destructor
452  */
453  ~ip_mroute();
454 
455  /**
456  * Get the route's key
457  */
458  const key_t key() const;
459 
460  /**
461  * Comparison operator
462  */
463  bool operator==(const ip_mroute& i) const;
464 
465  /**
466  * Return the matching 'singular instance'
467  */
468  std::shared_ptr<ip_mroute> singular() const;
469 
470  /**
471  * Find the instnace of the route domain in the OM
472  */
473  static std::shared_ptr<ip_mroute> find(const ip_mroute& temp);
474 
475  /**
476  * Dump all route-doamin into the stream provided
477  */
478  static void dump(std::ostream& os);
479 
480  /**
481  * replay the object to create it in hardware
482  */
483  void replay(void);
484 
485  /**
486  * Convert to string for debugging
487  */
488  std::string to_string() const;
489 
490  /**
491  * Return the matching 'singular instance'
492  */
493  static std::shared_ptr<ip_mroute> find(const key_t& k);
494 
495  void add(const path& path, const itf_flags_t& flag);
496 
497 private:
498  /**
499  * Class definition for listeners to OM events
500  */
502  {
503  public:
504  event_handler();
505  virtual ~event_handler() = default;
506 
507  /**
508  * Handle a populate event
509  */
510  void handle_populate(const client_db::key_t& key);
511 
512  /**
513  * Handle a replay event
514  */
515  void handle_replay();
516 
517  /**
518  * Show the object in the Singular DB
519  */
520  void show(std::ostream& os);
521 
522  /**
523  * Get the sortable Id of the listener
524  */
525  dependency_t order() const;
526  };
527 
528  /**
529  * event_handler to register with OM
530  */
531  static event_handler m_evh;
532 
533  /**
534  * Find or add the instnace of the route domain in the OM
535  */
536  static std::shared_ptr<ip_mroute> find_or_add(const ip_mroute& temp);
537 
538  /*
539  * It's the OM class that updates the objects in HW
540  */
541  friend class VOM::OM;
542 
543  /**
544  * It's the singular_db class that calls replay()
545  */
546  friend class singular_db<key_t, ip_mroute>;
547 
548  /**
549  * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
550  */
551  void update(const ip_mroute& obj);
552 
553  /**
554  * Sweep/reap the object if still stale
555  */
556  void sweep(void);
557 
558  /**
559  * HW configuration for the result of creating the route
560  */
561  HW::item<bool> m_hw;
562 
563  /**
564  * The route domain the route is in.
565  */
566  std::shared_ptr<route_domain> m_rd;
567 
568  /**
569  * The mprefix to match
570  */
571  mprefix_t m_mprefix;
572 
573  /**
574  * The set of paths
575  */
576  mpath_list_t m_paths;
577 
578  /**
579  * A map of all routes
580  */
581  static singular_db<key_t, ip_mroute> m_db;
582 };
583 
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
587 }; // namesapce VPP
588 
589 /*
590  * fd.io coding-style-patch-verification: ON
591  *
592  * Local Variables:
593  * eval: (c-set-style "mozilla")
594  * End:
595  */
596 
597 #endif
flags_t flags() const
Definition: route.cpp:228
std::pair< route::table_id_t, mprefix_t > key_t
The key for a route.
Definition: route.hpp:433
nh_proto_t nh_proto() const
Definition: route.cpp:222
typedef address
Definition: ip_types.api:30
static const special_t STANDARD
A standard path type.
Definition: route.hpp:45
static const itf_flags_t ACCEPT
Path is accepting multicast traffic.
Definition: route.hpp:214
A template base class for all enum types.
Definition: enum_base.hpp:30
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
static const nh_proto_t IPV4
Definition: prefix.hpp:34
A path for IP or MPLS routes.
Definition: route.hpp:32
route::path from_vpp(const vapi_type_fib_path &p, const nh_proto_t &nhp)
int i
static const itf_flags_t FORWARD
A local/for-us/recieve.
Definition: route.hpp:219
std::shared_ptr< interface > itf() const
Definition: route.cpp:246
A route-domain is a VRF.
static const itf_flags_t NONE
Definition: route.hpp:210
static const special_t LOCAL
A local/for-us/recieve.
Definition: route.hpp:50
static const special_t DROP
drop path
Definition: route.hpp:55
~path()
Destructor.
Definition: route.cpp:171
std::ostream & operator<<(std::ostream &os, const ip_route::key_t &key)
Definition: route.cpp:734
Types belonging to Routing.
Definition: prefix.hpp:31
static const special_t UNREACH
a path will return ICMP unreachables
Definition: route.hpp:60
std::shared_ptr< route_domain > rd() const
Definition: route.cpp:240
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:33
A IP multicast route.
Definition: route.hpp:427
static const special_t PROHIBIT
a path will return ICMP prohibit
Definition: route.hpp:65
bool operator==(const enum_base &e) const
Comparison operator.
Definition: enum_base.hpp:41
std::set< path > path_list_t
A path-list is a set of paths.
Definition: route.hpp:233
A representation of an interface in VPP.
Definition: interface.hpp:41
std::set< std::pair< path, itf_flags_t > > mpath_list_t
A mpath-list is a set of paths and interface flags.
Definition: route.hpp:238
Class definition for listeners to OM events.
Definition: om.hpp:284
static const flags_t DVR
A path that resolves via a DVR next-hop.
Definition: route.hpp:88
inspect command handler Handler
Definition: inspect.hpp:54
void event_handler(void *tls_async)
Definition: tls_async.c:340
Special path types.
Definition: route.hpp:38
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
uint8_t preference() const
Definition: route.cpp:258
The interface to writing objects into VPP OM.
Definition: om.hpp:140
A base class for all object_base in the VPP object_base-Model.
Definition: object_base.hpp:29
typedef mprefix
Definition: ip_types.api:40
const boost::asio::ip::address & nh() const
Definition: route.cpp:234
path(special_t special, const nh_proto_t &proto=nh_proto_t::IPV4)
constructor for special paths
Definition: route.cpp:66
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A prefix defintion.
Definition: prefix.hpp:213
bool operator<(const path &p) const
Less than operator for set insertion.
Definition: route.cpp:137
typedef key
Definition: ipsec.api:244
A IP route.
Definition: route.hpp:249
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
typedef prefix
Definition: ip_types.api:35
special_t type() const
Getters.
Definition: route.cpp:216
uint8_t weight() const
Definition: route.cpp:252
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
static const flags_t NONE
No flags.
Definition: route.hpp:83
A prefix defintion.
Definition: prefix.hpp:92
std::pair< route::table_id_t, prefix_t > key_t
The key for a route.
Definition: route.hpp:255