FD.io VPP  v18.10-32-g1161dda
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();
60  bool is_ipv6();
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 namespace route {
79 /**
80  * type def the table-id
81  */
82 typedef uint32_t table_id_t;
83 
84 /**
85  * The table-id for the default table
86  */
87 const static table_id_t DEFAULT_TABLE = 0;
88 
89 /**
90  * A prefix defintion. Address + length
91  */
92 class prefix_t
93 {
94 public:
95  /**
96  * Default Constructor - creates ::/0
97  */
98  prefix_t();
99  /**
100  * Constructor with address and length
101  */
102  prefix_t(const boost::asio::ip::address& addr, uint8_t len);
103  /**
104  * Constructor with just the address, this creates a
105  * host prefix
106  */
107  prefix_t(const boost::asio::ip::address& addr);
108 
109  /**
110  * Constructor with string and length
111  */
112  prefix_t(const std::string& s, uint8_t len);
113 
114  /**
115  * Copy Constructor
116  */
117  prefix_t(const prefix_t&);
118 
119  /**
120  * Constructor with VPP API prefix representation
121  */
122  prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
123  /**
124  * Destructor
125  */
126  ~prefix_t();
127 
128  /**
129  * Get the address
130  */
131  const boost::asio::ip::address& address() const;
132 
133  /**
134  * Get the network mask width
135  */
136  uint8_t mask_width() const;
137 
138  /**
139  * Assignement
140  */
141  prefix_t& operator=(const prefix_t&);
142 
143  /**
144  * Less than operator
145  */
146  bool operator<(const prefix_t& o) const;
147 
148  /**
149  * equals operator
150  */
151  bool operator==(const prefix_t& o) const;
152 
153  /**
154  * not equal opartor
155  */
156  bool operator!=(const prefix_t& o) const;
157 
158  /**
159  * convert to string format for debug purposes
160  */
161  std::string to_string() const;
162 
163  /**
164  * The all Zeros prefix
165  */
166  const static prefix_t ZERO;
167 
168  /**
169  * The all Zeros v6 prefix
170  */
171  const static prefix_t ZEROv6;
172 
173  /**
174  * Convert the prefix into VPP API parameters
175  */
176  void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
177 
178  /**
179  * Return a address representation of the mask, e.g. 255.255.0.0
180  */
181  boost::asio::ip::address mask() const;
182 
183  /**
184  * get the lowest address in the prefix
185  */
186  prefix_t low() const;
187 
188  /**
189  * Get the highest address in the prefix
190  */
191  prefix_t high() const;
192 
193  /**
194  * Get the L3 protocol
195  */
196  l3_proto_t l3_proto() const;
197 
198 private:
199  /**
200  * The address
201  */
203 
204  /**
205  * The prefix length
206  */
207  uint8_t m_len;
208 };
209 };
210 
211 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
212  const boost::asio::ip::address_v4& addr2);
213 
214 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
215  const boost::asio::ip::address_v4& addr2);
216 
217 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
218 
219 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
220  const boost::asio::ip::address_v6& addr2);
221 
222 boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
223  const boost::asio::ip::address_v6& addr2);
224 
225 boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
226 
228  const boost::asio::ip::address& addr2);
229 
231  const boost::asio::ip::address& addr2);
232 
234 
235 /**
236  * Ostream printer for prefix_t
237  */
238 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
239 
240 /**
241  * Convert a boost address into a VPP bytes string
242  */
244  uint8_t* is_ip6,
245  uint8_t* array);
246 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
247 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
248 
249 /**
250  * Get the prefix mask length of a host route from the boost address
251  */
252 uint32_t mask_width(const boost::asio::ip::address& addr);
253 
254 /**
255  * Convert a VPP byte stinrg into a boost addresss
256  */
257 boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array);
258 };
259 
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "mozilla")
265  * End:
266  */
267 
268 #endif
uint32_t table_id_t
type def the table-id
Definition: prefix.hpp:82
typedef address
Definition: ip_types.api:35
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 prefix_t ZEROv6
The all Zeros v6 prefix.
Definition: prefix.hpp:171
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 nh_proto_t & from_address(const boost::asio::ip::address &addr)
Definition: prefix.cpp:90
vhost_vring_addr_t addr
Definition: vhost_user.h:121
static const l3_proto_t MPLS
Definition: prefix.hpp:57
Types belonging to Routing.
Definition: prefix.hpp:31
static const l3_proto_t IPV4
Definition: prefix.hpp:55
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:218
static const table_id_t DEFAULT_TABLE
The table-id for the default table.
Definition: prefix.hpp:87
static void to_vpp(const l2_rule &rule, vapi_type_macip_acl_rule &payload)
boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
Definition: prefix.cpp:278
#define v
Definition: acl.c:496
bool operator!=(const enum_base &e) const
Comparison operator.
Definition: enum_base.hpp:57
boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4 &addr1)
Definition: prefix.cpp:296
bool operator==(const enum_base &e) const
Comparison operator.
Definition: enum_base.hpp:41
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:242
boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
Definition: prefix.cpp:287
boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:193
enum_base & operator=(const enum_base &e)
Assignment.
Definition: enum_base.hpp:46
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
static const prefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:166
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
A prefix defintion.
Definition: prefix.hpp:92
static const nh_proto_t ETHERNET
Definition: prefix.hpp:37