FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
prefix.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_PREFIX_H__
17 #define __VOM_PREFIX_H__
18 
19 #include "vom/enum_base.hpp"
20 #include <boost/asio/ip/address.hpp>
21 
22 namespace VOM {
23 /**
24  * Types belonging to Routing
25  */
26 
27 /**
28  * A next-hop protocol describes the protocol of a peer to which packets
29  * are sent after matching a route.
30  */
31 class nh_proto_t : public enum_base<nh_proto_t>
32 {
33 public:
34  const static nh_proto_t IPV4;
35  const static nh_proto_t IPV6;
36  const static nh_proto_t MPLS;
37  const static nh_proto_t ETHERNET;
38 
40 
41 private:
42  /**
43  * Private constructor taking the value and the string name
44  */
45  nh_proto_t(int v, const std::string& s);
46 };
47 
48 /**
49  * An L3 protocol can be used to construct a prefix that is used
50  * to match packets are part of a route.
51  */
52 class l3_proto_t : public enum_base<l3_proto_t>
53 {
54 public:
55  const static l3_proto_t IPV4;
56  const static l3_proto_t IPV6;
57  const static l3_proto_t MPLS;
58 
59  bool is_ipv4() const;
60  bool is_ipv6() const;
61 
63 
64  const nh_proto_t& to_nh_proto() const;
65 
66 private:
67  /**
68  * Private constructor taking the value and the string name
69  */
70  l3_proto_t(int v, const std::string& s);
71 };
72 
73 /**
74  * Ostream output for l3_proto_t
75  */
76 std::ostream& operator<<(std::ostream& os, const l3_proto_t& l3p);
77 
78 /**
79  * IP DSCP values
80  */
81 class ip_dscp_t : public enum_base<ip_dscp_t>
82 {
83 public:
84  /* unfortunately some of the CSX names are defined in terminos.h
85  * as macros, hence the longer names */
86  const static ip_dscp_t DSCP_CS0;
87  const static ip_dscp_t DSCP_CS1;
88  const static ip_dscp_t DSCP_CS2;
89  const static ip_dscp_t DSCP_CS3;
90  const static ip_dscp_t DSCP_CS4;
91  const static ip_dscp_t DSCP_CS5;
92  const static ip_dscp_t DSCP_CS6;
93  const static ip_dscp_t DSCP_CS7;
94  const static ip_dscp_t DSCP_AF11;
95  const static ip_dscp_t DSCP_AF12;
96  const static ip_dscp_t DSCP_AF13;
97  const static ip_dscp_t DSCP_AF21;
98  const static ip_dscp_t DSCP_AF22;
99  const static ip_dscp_t DSCP_AF23;
100  const static ip_dscp_t DSCP_AF31;
101  const static ip_dscp_t DSCP_AF32;
102  const static ip_dscp_t DSCP_AF33;
103  const static ip_dscp_t DSCP_AF41;
104  const static ip_dscp_t DSCP_AF42;
105  const static ip_dscp_t DSCP_AF43;
106  const static ip_dscp_t DSCP_EF;
107 
108  /**
109  * Constructor allows the creation of any DSCP value
110  */
111  ip_dscp_t(int v);
112 
113 private:
114  ip_dscp_t(int v, const std::string& s);
115 };
116 
117 namespace route {
118 /**
119  * type def the table-id
120  */
121 typedef uint32_t table_id_t;
122 
123 /**
124  * The table-id for the default table
125  */
126 const static table_id_t DEFAULT_TABLE = 0;
127 
128 /**
129  * A prefix defintion. Address + length
130  */
131 class prefix_t
132 {
133 public:
134  /**
135  * Default Constructor - creates ::/0
136  */
137  prefix_t();
138  /**
139  * Constructor with address and length
140  */
141  prefix_t(const boost::asio::ip::address& addr, uint8_t len);
142  /**
143  * Constructor with just the address, this creates a
144  * host prefix
145  */
147 
148  /**
149  * Constructor with string and length
150  */
151  prefix_t(const std::string& s, uint8_t len);
152 
153  /**
154  * Copy Constructor
155  */
156  prefix_t(const prefix_t&);
157 
158  /**
159  * Constructor with VPP API prefix representation
160  */
161  prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
162  /**
163  * Destructor
164  */
165  ~prefix_t();
166 
167  /**
168  * Get the address
169  */
170  const boost::asio::ip::address& address() const;
171 
172  /**
173  * Get the network mask width
174  */
175  uint8_t mask_width() const;
176 
177  /**
178  * Assignement
179  */
180  prefix_t& operator=(const prefix_t&);
181 
182  /**
183  * Less than operator
184  */
185  bool operator<(const prefix_t& o) const;
186 
187  /**
188  * equals operator
189  */
190  bool operator==(const prefix_t& o) const;
191 
192  /**
193  * not equal opartor
194  */
195  bool operator!=(const prefix_t& o) const;
196 
197  /**
198  * convert to string format for debug purposes
199  */
200  std::string to_string() const;
201 
202  /**
203  * The all Zeros prefix
204  */
205  const static prefix_t ZERO;
206 
207  /**
208  * The all Zeros v6 prefix
209  */
210  const static prefix_t ZEROv6;
211 
212  /**
213  * Convert the prefix into VPP API parameters
214  */
215  void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
216 
217  /**
218  * Return a address representation of the mask, e.g. 255.255.0.0
219  */
221 
222  /**
223  * get the lowest address in the prefix
224  */
225  prefix_t low() const;
226 
227  /**
228  * Get the highest address in the prefix
229  */
230  prefix_t high() const;
231 
232  /**
233  * Get the L3 protocol
234  */
235  l3_proto_t l3_proto() const;
236 
237 private:
238  /**
239  * The address
240  */
242 
243  /**
244  * The prefix length
245  */
246  uint8_t m_len;
247 };
248 
249 /**
250 * A prefix defintion. Address + length
251 */
253 {
254 public:
255  /**
256  * Default Constructor - creates ::/0
257  */
258  mprefix_t();
259  /**
260  * Constructor for (S,G)
261  */
262  mprefix_t(const boost::asio::ip::address& saddr,
264  /*
265  * Constructor for (*,G)
266  */
268 
269  /*
270  * Constructor for (*,G/n)
271  */
272  mprefix_t(const boost::asio::ip::address& gaddr, uint8_t len);
273 
274  /**
275  *Constructor for (S,G)
276  */
277  mprefix_t(const boost::asio::ip::address& saddr,
279  uint16_t len);
280 
281  /**
282  * Copy Constructor
283  */
284  mprefix_t(const mprefix_t&);
285 
286  /**
287  * Destructor
288  */
289  ~mprefix_t();
290 
291  /**
292  * Get the address
293  */
294  const boost::asio::ip::address& grp_address() const;
295  const boost::asio::ip::address& src_address() const;
296 
297  /**
298  * Get the network mask width
299  */
300  uint8_t mask_width() const;
301 
302  /**
303  * Assignement
304  */
305  mprefix_t& operator=(const mprefix_t&);
306 
307  /**
308  * Less than operator
309  */
310  bool operator<(const mprefix_t& o) const;
311 
312  /**
313  * equals operator
314  */
315  bool operator==(const mprefix_t& o) const;
316 
317  /**
318  * not equal opartor
319  */
320  bool operator!=(const mprefix_t& o) const;
321 
322  /**
323  * convert to string format for debug purposes
324  */
325  std::string to_string() const;
326 
327  /**
328  * The all Zeros prefix
329  */
330  const static mprefix_t ZERO;
331 
332  /**
333  * The all Zeros v6 prefix
334  */
335  const static mprefix_t ZEROv6;
336 
337  /**
338  * Get the L3 protocol
339  */
340  l3_proto_t l3_proto() const;
341 
342 private:
343  /**
344  * The address
345  */
346  boost::asio::ip::address m_gaddr;
347  boost::asio::ip::address m_saddr;
348 
349  /**
350  * The prefix length
351  */
352  uint8_t m_len;
353 };
354 
355 }; // namespace route
356 
357 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
358  const boost::asio::ip::address_v4& addr2);
359 
360 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
361  const boost::asio::ip::address_v4& addr2);
362 
363 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
364 
365 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
366  const boost::asio::ip::address_v6& addr2);
367 
368 boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
369  const boost::asio::ip::address_v6& addr2);
370 
371 boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
372 
374  const boost::asio::ip::address& addr2);
375 
377  const boost::asio::ip::address& addr2);
378 
380 
381 /**
382  * Ostream printer for prefix_t
383  */
384 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
385 
386 /**
387  * Convert a boost address into a VPP bytes string
388  */
390  uint8_t* is_ip6,
391  uint8_t* array);
392 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
393 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
394 
395 /**
396  * Get the prefix mask length of a host route from the boost address
397  */
398 uint32_t mask_width(const boost::asio::ip::address& addr);
399 
400 /**
401  * Convert a VPP byte stinrg into a boost addresss
402  */
403 boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t* array);
404 };
405 
406 /*
407  * fd.io coding-style-patch-verification: OFF
408  *
409  * Local Variables:
410  * eval: (c-set-style "mozilla")
411  * End:
412  */
413 
414 #endif
VOM::l3_proto_t::MPLS
const static l3_proto_t MPLS
Definition: prefix.hpp:57
VOM::route::prefix_t
A prefix defintion.
Definition: prefix.hpp:131
VOM::route::mprefix_t::operator!=
bool operator!=(const mprefix_t &o) const
not equal opartor
Definition: prefix.cpp:568
VOM::ip_dscp_t::DSCP_CS2
const static ip_dscp_t DSCP_CS2
Definition: prefix.hpp:88
VOM::operator&
boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
Definition: prefix.cpp:318
VOM::route::prefix_t::mask_width
uint8_t mask_width() const
Get the network mask width.
Definition: prefix.cpp:186
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::route::prefix_t::ZERO
const static prefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:205
VOM::ip_dscp_t::DSCP_CS4
const static ip_dscp_t DSCP_CS4
Definition: prefix.hpp:90
VOM::operator~
boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4 &addr1)
Definition: prefix.cpp:327
VOM::nh_proto_t::MPLS
const static nh_proto_t MPLS
Definition: prefix.hpp:36
VOM::route::prefix_t::prefix_t
prefix_t()
Default Constructor - creates ::/0.
Definition: prefix.cpp:160
VOM::to_bytes
void to_bytes(const boost::asio::ip::address_v6 &addr, uint8_t *array)
Definition: prefix.cpp:249
VOM::ip_dscp_t::DSCP_AF33
const static ip_dscp_t DSCP_AF33
Definition: prefix.hpp:102
VOM::l3_proto_t::is_ipv4
bool is_ipv4() const
Definition: prefix.cpp:41
VOM::l3_proto_t::IPV4
const static l3_proto_t IPV4
Definition: prefix.hpp:55
VOM::route::mprefix_t::mprefix_t
mprefix_t()
Default Constructor - creates ::/0.
Definition: prefix.cpp:497
VOM::route::mprefix_t::grp_address
const boost::asio::ip::address & grp_address() const
Get the address.
Definition: prefix.cpp:509
VOM::route::prefix_t::low
prefix_t low() const
get the lowest address in the prefix
Definition: prefix.cpp:435
VOM::mask_width
uint32_t mask_width(const boost::asio::ip::address &addr)
Get the prefix mask length of a host route from the boost address.
Definition: prefix.cpp:273
VOM::route::prefix_t::~prefix_t
~prefix_t()
Destructor.
Definition: prefix.cpp:166
VOM::route::mprefix_t::mask_width
uint8_t mask_width() const
Get the network mask width.
Definition: prefix.cpp:521
VOM::from_bytes
boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:224
VOM::ip_dscp_t::DSCP_AF21
const static ip_dscp_t DSCP_AF21
Definition: prefix.hpp:97
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
VOM::operator|
boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
Definition: prefix.cpp:309
VOM::route::prefix_t::operator=
prefix_t & operator=(const prefix_t &)
Assignement.
Definition: prefix.cpp:171
VOM::ip_dscp_t::DSCP_AF12
const static ip_dscp_t DSCP_AF12
Definition: prefix.hpp:95
VOM::route::mprefix_t
A prefix defintion.
Definition: prefix.hpp:252
VOM::route::mprefix_t::l3_proto
l3_proto_t l3_proto() const
Get the L3 protocol.
Definition: prefix.cpp:527
VOM::route::mprefix_t::src_address
const boost::asio::ip::address & src_address() const
Definition: prefix.cpp:515
VOM::route::prefix_t::address
const boost::asio::ip::address & address() const
Get the address.
Definition: prefix.cpp:180
VOM::route::prefix_t::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:214
VOM::ip_dscp_t::DSCP_AF43
const static ip_dscp_t DSCP_AF43
Definition: prefix.hpp:105
len
u8 len
Definition: ip_types.api:103
VOM::route::prefix_t::operator!=
bool operator!=(const prefix_t &o) const
not equal opartor
Definition: prefix.cpp:208
VOM::ip_dscp_t::DSCP_CS7
const static ip_dscp_t DSCP_CS7
Definition: prefix.hpp:93
VOM::ip_dscp_t::DSCP_AF23
const static ip_dscp_t DSCP_AF23
Definition: prefix.hpp:99
VOM::route::mprefix_t::ZERO
const static mprefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:330
enum_base.hpp
VOM::nh_proto_t::ETHERNET
const static nh_proto_t ETHERNET
Definition: prefix.hpp:37
VOM::ip_dscp_t::DSCP_AF41
const static ip_dscp_t DSCP_AF41
Definition: prefix.hpp:103
VOM::enum_base
A template base class for all enum types.
Definition: enum_base.hpp:30
VOM::ip_dscp_t::DSCP_CS3
const static ip_dscp_t DSCP_CS3
Definition: prefix.hpp:89
VOM::route::prefix_t::operator==
bool operator==(const prefix_t &o) const
equals operator
Definition: prefix.cpp:202
VOM::ip_dscp_t::DSCP_EF
const static ip_dscp_t DSCP_EF
Definition: prefix.hpp:106
address
manual_print typedef address
Definition: ip_types.api:96
VOM::l3_proto_t::from_address
static const l3_proto_t & from_address(const boost::asio::ip::address &addr)
Definition: prefix.cpp:47
VOM::route::prefix_t::high
prefix_t high() const
Get the highest address in the prefix.
Definition: prefix.cpp:445
VOM::ip_dscp_t::DSCP_CS5
const static ip_dscp_t DSCP_CS5
Definition: prefix.hpp:91
VOM::ip_dscp_t::DSCP_AF22
const static ip_dscp_t DSCP_AF22
Definition: prefix.hpp:98
VOM::route::mprefix_t::operator==
bool operator==(const mprefix_t &o) const
equals operator
Definition: prefix.cpp:562
VOM::ip_dscp_t::DSCP_AF11
const static ip_dscp_t DSCP_AF11
Definition: prefix.hpp:94
VOM::operator<<
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
Definition: acl_binding.cpp:201
VOM::route::prefix_t::l3_proto
l3_proto_t l3_proto() const
Get the L3 protocol.
Definition: prefix.cpp:289
VOM::route::mprefix_t::operator<
bool operator<(const mprefix_t &o) const
Less than operator.
Definition: prefix.cpp:549
VOM::l3_proto_t::is_ipv6
bool is_ipv6() const
Definition: prefix.cpp:35
VOM::route::mprefix_t::ZEROv6
const static mprefix_t ZEROv6
The all Zeros v6 prefix.
Definition: prefix.hpp:335
VOM::nh_proto_t::from_address
static const nh_proto_t & from_address(const boost::asio::ip::address &addr)
Definition: prefix.cpp:90
is_ip6
bool is_ip6
Definition: ip.api:43
VOM::l3_proto_t
An L3 protocol can be used to construct a prefix that is used to match packets are part of a route.
Definition: prefix.hpp:52
VOM::nh_proto_t::IPV6
const static nh_proto_t IPV6
Definition: prefix.hpp:35
VOM::ip_dscp_t::DSCP_AF32
const static ip_dscp_t DSCP_AF32
Definition: prefix.hpp:101
VOM::route::prefix_t::mask
boost::asio::ip::address mask() const
Return a address representation of the mask, e.g.
Definition: prefix.cpp:406
gaddr
vl_api_ip4_address_t gaddr
Definition: igmp.api:70
VOM::nh_proto_t
Types belonging to Routing.
Definition: prefix.hpp:31
VOM::ip_dscp_t
IP DSCP values.
Definition: prefix.hpp:81
VOM::route::prefix_t::to_vpp
void to_vpp(uint8_t *is_ip6, uint8_t *addr, uint8_t *len) const
Convert the prefix into VPP API parameters.
Definition: prefix.cpp:282
VOM::ip_dscp_t::ip_dscp_t
ip_dscp_t(int v)
Constructor allows the creation of any DSCP value.
Definition: prefix.cpp:125
VOM::route::mprefix_t::~mprefix_t
~mprefix_t()
Destructor.
Definition: prefix.cpp:504
VOM::ip_dscp_t::DSCP_CS0
const static ip_dscp_t DSCP_CS0
Definition: prefix.hpp:86
VOM::route::prefix_t::ZEROv6
const static prefix_t ZEROv6
The all Zeros v6 prefix.
Definition: prefix.hpp:210
VOM::ip_dscp_t::DSCP_AF42
const static ip_dscp_t DSCP_AF42
Definition: prefix.hpp:104
VOM::route::table_id_t
uint32_t table_id_t
type def the table-id
Definition: prefix.hpp:121
VOM::ip_dscp_t::DSCP_CS6
const static ip_dscp_t DSCP_CS6
Definition: prefix.hpp:92
VOM::route::mprefix_t::operator=
mprefix_t & operator=(const mprefix_t &)
Assignement.
Definition: prefix.cpp:539
VOM::nh_proto_t::IPV4
const static nh_proto_t IPV4
Definition: prefix.hpp:34
VOM::route::prefix_t::operator<
bool operator<(const prefix_t &o) const
Less than operator.
Definition: prefix.cpp:192
VOM::route::DEFAULT_TABLE
const static table_id_t DEFAULT_TABLE
The table-id for the default table.
Definition: prefix.hpp:126
VOM::ip_dscp_t::DSCP_CS1
const static ip_dscp_t DSCP_CS1
Definition: prefix.hpp:87
VOM::l3_proto_t::to_nh_proto
const nh_proto_t & to_nh_proto() const
Definition: prefix.cpp:57
VOM::ip_dscp_t::DSCP_AF31
const static ip_dscp_t DSCP_AF31
Definition: prefix.hpp:100
VOM::ip_dscp_t::DSCP_AF13
const static ip_dscp_t DSCP_AF13
Definition: prefix.hpp:96
VOM::l3_proto_t::IPV6
const static l3_proto_t IPV6
Definition: prefix.hpp:56
VOM::route::mprefix_t::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:574
string
const char *const string
Definition: cJSON.h:172