FD.io VPP  v20.05.1-6-gf53edbc3b
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  */
146  prefix_t(const boost::asio::ip::address& addr);
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  */
220  boost::asio::ip::address mask() const;
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  */
267  mprefix_t(const boost::asio::ip::address& gaddr);
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,
278  const boost::asio::ip::address& gaddr,
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: ON
408  *
409  * Local Variables:
410  * eval: (c-set-style "mozilla")
411  * End:
412  */
413 
414 #endif
uint32_t table_id_t
type def the table-id
Definition: prefix.hpp:121
static const ip_dscp_t DSCP_AF21
Definition: prefix.hpp:97
static const ip_dscp_t DSCP_CS4
Definition: prefix.hpp:90
static const ip_dscp_t DSCP_AF11
Definition: prefix.hpp:94
bool operator!=(const enum_base &e) const
Comparison operator.
Definition: enum_base.hpp:78
A template base class for all enum types.
Definition: enum_base.hpp:30
static const nh_proto_t IPV6
Definition: prefix.hpp:35
static const nh_proto_t IPV4
Definition: prefix.hpp:34
static const ip_dscp_t DSCP_AF33
Definition: prefix.hpp:102
static const prefix_t ZEROv6
The all Zeros v6 prefix.
Definition: prefix.hpp:210
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
static const ip_dscp_t DSCP_AF12
Definition: prefix.hpp:95
static const nh_proto_t & from_address(const boost::asio::ip::address &addr)
Definition: prefix.cpp:90
vhost_vring_addr_t addr
Definition: vhost_user.h:254
boost::asio::ip::address_v4 operator &(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
Definition: prefix.cpp:318
bool operator==(const enum_base &e) const
Comparison operator.
Definition: enum_base.hpp:41
static const l3_proto_t MPLS
Definition: prefix.hpp:57
static const ip_dscp_t DSCP_AF13
Definition: prefix.hpp:96
static const ip_dscp_t DSCP_CS2
Definition: prefix.hpp:88
enum_base operator|(const enum_base &e1) const
bitwise or
Definition: enum_base.hpp:68
static const ip_dscp_t DSCP_AF32
Definition: prefix.hpp:101
Types belonging to Routing.
Definition: prefix.hpp:31
static const l3_proto_t IPV4
Definition: prefix.hpp:55
static const ip_dscp_t DSCP_CS5
Definition: prefix.hpp:91
bool is_ip6
Definition: ip.api:43
static const l3_proto_t IPV6
Definition: prefix.hpp:56
void to_bytes(const boost::asio::ip::address_v6 &addr, uint8_t *array)
Definition: prefix.cpp:249
static const table_id_t DEFAULT_TABLE
The table-id for the default table.
Definition: prefix.hpp:126
static void to_vpp(const l2_rule &rule, vapi_type_macip_acl_rule &payload)
static const ip_dscp_t DSCP_CS7
Definition: prefix.hpp:93
static const mprefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:330
boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4 &addr1)
Definition: prefix.cpp:327
vl_api_address_union_t src_address
Definition: ip_types.api:99
static const ip_dscp_t DSCP_CS6
Definition: prefix.hpp:92
static const mprefix_t ZEROv6
The all Zeros v6 prefix.
Definition: prefix.hpp:335
static const ip_dscp_t DSCP_AF41
Definition: prefix.hpp:103
u8 len
Definition: ip_types.api:92
vl_api_ip4_address_t gaddr
Definition: igmp.api:70
IP DSCP values.
Definition: prefix.hpp:81
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
vl_api_ip4_address_t low
Definition: arp.api:36
static const ip_dscp_t DSCP_AF43
Definition: prefix.hpp:105
static const ip_dscp_t DSCP_CS1
Definition: prefix.hpp:87
static const ip_dscp_t DSCP_AF22
Definition: prefix.hpp:98
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
static const ip_dscp_t DSCP_AF23
Definition: prefix.hpp:99
static const ip_dscp_t DSCP_EF
Definition: prefix.hpp:106
manual_print typedef address
Definition: ip_types.api:85
static const ip_dscp_t DSCP_AF42
Definition: prefix.hpp:104
enum ip_dscp_t_ ip_dscp_t
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
enum_base & operator=(const enum_base &e)
Assignment.
Definition: enum_base.hpp:46
static const ip_dscp_t DSCP_AF31
Definition: prefix.hpp:100
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
static const nh_proto_t MPLS
Definition: prefix.hpp:36
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A prefix defintion.
Definition: prefix.hpp:252
static const ip_dscp_t DSCP_CS0
Definition: prefix.hpp:86
static const prefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:205
static const ip_dscp_t DSCP_CS3
Definition: prefix.hpp:89
vl_api_address_union_t grp_address
Definition: ip_types.api:98
bool is_ipv6
Definition: dhcp.api:202
A prefix defintion.
Definition: prefix.hpp:131
static const nh_proto_t ETHERNET
Definition: prefix.hpp:37