FD.io VPP  v18.01.2-1-g9b554f3
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 <boost/asio/ip/address.hpp>
20 
21 #include "vom/enum_base.hpp"
22 
23 namespace VOM {
24 /**
25  * Types belonging to Routing
26  */
27 
28 /**
29  * An L3 protocol can be used to construct a prefix that is used
30  * to match packets are part of a route.
31  */
32 class l3_proto_t : public enum_base<l3_proto_t>
33 {
34 public:
35  const static l3_proto_t IPV4;
36  const static l3_proto_t IPV6;
37  const static l3_proto_t MPLS;
38 
39  bool is_ipv4();
40  bool is_ipv6();
41 
42  static const l3_proto_t& from_address(const boost::asio::ip::address& addr);
43 
44 private:
45  /**
46  * Private constructor taking the value and the string name
47  */
48  l3_proto_t(int v, const std::string& s);
49 };
50 
51 /**
52  * Ostream output for l3_proto_t
53  */
54 std::ostream& operator<<(std::ostream& os, const l3_proto_t& l3p);
55 
56 /**
57  * A next-hop protocol describes the protocol of a peer to which packets
58  * are sent after matching a route.
59  */
60 class nh_proto_t : public enum_base<nh_proto_t>
61 {
62 public:
63  const static nh_proto_t IPV4;
64  const static nh_proto_t IPV6;
65  const static nh_proto_t MPLS;
66  const static nh_proto_t ETHERNET;
67 
68  static const nh_proto_t& from_address(const boost::asio::ip::address& addr);
69 
70 private:
71  /**
72  * Private constructor taking the value and the string name
73  */
74  nh_proto_t(int v, const std::string& s);
75 };
76 
77 namespace route {
78 /**
79  * type def the table-id
80  */
81 typedef uint32_t table_id_t;
82 
83 /**
84  * The table-id for the default table
85  */
86 const static table_id_t DEFAULT_TABLE = 0;
87 
88 /**
89  * A prefix defintion. Address + length
90  */
91 class prefix_t
92 {
93 public:
94  /**
95  * Default Constructor - creates ::/0
96  */
97  prefix_t();
98  /**
99  * Constructor with address and length
100  */
101  prefix_t(const boost::asio::ip::address& addr, uint8_t len);
102  /**
103  * Constructor with just the address, this creates a
104  * host prefix
105  */
106  prefix_t(const boost::asio::ip::address& addr);
107 
108  /**
109  * Constructor with string and length
110  */
111  prefix_t(const std::string& s, uint8_t len);
112  /**
113  * Copy Constructor
114  */
115  prefix_t(const prefix_t&);
116  /**
117  * Constructor with VPP API prefix representation
118  */
119  prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
120  /**
121  * Destructor
122  */
123  ~prefix_t();
124 
125  /**
126  * Get the address
127  */
128  const boost::asio::ip::address& address() const;
129 
130  /**
131  * Get the network mask width
132  */
133  uint8_t mask_width() const;
134 
135  /**
136  * Assignement
137  */
138  prefix_t& operator=(const prefix_t&);
139 
140  /**
141  * Less than operator
142  */
143  bool operator<(const prefix_t& o) const;
144 
145  /**
146  * equals operator
147  */
148  bool operator==(const prefix_t& o) const;
149 
150  /**
151  * not equal opartor
152  */
153  bool operator!=(const prefix_t& o) const;
154 
155  /**
156  * convert to string format for debug purposes
157  */
158  std::string to_string() const;
159 
160  /**
161  * The all Zeros prefix
162  */
163  const static prefix_t ZERO;
164 
165  /**
166  * The all Zeros v6 prefix
167  */
168  const static prefix_t ZEROv6;
169 
170  /**
171  * Convert the prefix into VPP API parameters
172  */
173  void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
174 
175  /**
176  * Return a address representation of the mask, e.g. 255.255.0.0
177  */
178  boost::asio::ip::address mask() const;
179 
180  /**
181  * get the lowest address in the prefix
182  */
183  prefix_t low() const;
184 
185  /**
186  * Get the highest address in the prefix
187  */
188  prefix_t high() const;
189 
190  /**
191  * Get the L3 protocol
192  */
193  l3_proto_t l3_proto() const;
194 
195 private:
196  /**
197  * The address
198  */
199  boost::asio::ip::address m_addr;
200 
201  /**
202  * The prefix length
203  */
204  uint8_t m_len;
205 };
206 };
207 
208 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
209  const boost::asio::ip::address_v4& addr2);
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 
216 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
217  const boost::asio::ip::address_v6& addr2);
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 
224 boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
225  const boost::asio::ip::address& addr2);
226 
227 boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
228  const boost::asio::ip::address& addr2);
229 
230 boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
231 
232 /**
233  * Ostream printer for prefix_t
234  */
235 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
236 
237 /**
238  * Convert a boost address into a VPP bytes string
239  */
240 void to_bytes(const boost::asio::ip::address& addr,
241  uint8_t* is_ip6,
242  uint8_t* array);
243 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
244 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
245 
246 /**
247  * Get the prefix mask length of a host route from the boost address
248  */
249 uint32_t mask_width(const boost::asio::ip::address& addr);
250 
251 /**
252  * Convert a VPP byte stinrg into a boost addresss
253  */
254 boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array);
255 };
256 
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "mozilla")
262  * End:
263  */
264 
265 #endif
uint32_t table_id_t
type def the table-id
Definition: prefix.hpp:81
A template base class for all enum types.
Definition: enum_base.hpp:30
static const nh_proto_t IPV6
Definition: prefix.hpp:64
static const nh_proto_t IPV4
Definition: prefix.hpp:63
static const prefix_t ZEROv6
The all Zeros v6 prefix.
Definition: prefix.hpp:168
Types belonging to Routing.
Definition: prefix.hpp:32
static const l3_proto_t MPLS
Definition: prefix.hpp:37
A next-hop protocol describes the protocol of a peer to which packets are sent after matching a route...
Definition: prefix.hpp:60
bool is_ipv4()
Definition: prefix.cpp:41
static const l3_proto_t IPV4
Definition: prefix.hpp:35
static const l3_proto_t IPV6
Definition: prefix.hpp:36
void to_bytes(const boost::asio::ip::address_v6 &addr, uint8_t *array)
Definition: prefix.cpp:205
static const table_id_t DEFAULT_TABLE
The table-id for the default table.
Definition: prefix.hpp:86
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:265
#define v
Definition: acl.c:341
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:283
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:229
boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4 &addr1, const boost::asio::ip::address_v4 &addr2)
Definition: prefix.cpp:274
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:180
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 l3_proto_t & from_address(const boost::asio::ip::address &addr)
Definition: prefix.cpp:47
static const nh_proto_t MPLS
Definition: prefix.hpp:65
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static const prefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:163
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
vhost_vring_addr_t addr
Definition: vhost-user.h:83
A prefix defintion.
Definition: prefix.hpp:91
static const nh_proto_t ETHERNET
Definition: prefix.hpp:66
bool is_ipv6()
Definition: prefix.cpp:35