jvpp: unify notification handling
[vpp.git] / src / vpp-api / vom / prefix.hpp
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  * A next-hop protocol describes the protocol of a peer to which packets
53  * are sent after matching a route.
54  */
55 class nh_proto_t : public enum_base<nh_proto_t>
56 {
57 public:
58   const static nh_proto_t IPV4;
59   const static nh_proto_t IPV6;
60   const static nh_proto_t MPLS;
61   const static nh_proto_t ETHERNET;
62
63   static const nh_proto_t& from_address(const boost::asio::ip::address& addr);
64
65 private:
66   /**
67    * Private constructor taking the value and the string name
68    */
69   nh_proto_t(int v, const std::string& s);
70 };
71
72 namespace route {
73 /**
74  * type def the table-id
75  */
76 typedef uint32_t table_id_t;
77
78 /**
79  * The table-id for the default table
80  */
81 const static table_id_t DEFAULT_TABLE = 0;
82
83 /**
84  * A prefix defintion. Address + length
85  */
86 class prefix_t
87 {
88 public:
89   /**
90    * Default Constructor - creates ::/0
91    */
92   prefix_t();
93   /**
94    * Constructor with address and length
95    */
96   prefix_t(const boost::asio::ip::address& addr, uint8_t len);
97   /**
98    * Constructor with just the address, this creates a
99    * host prefix
100    */
101   prefix_t(const boost::asio::ip::address& addr);
102
103   /**
104    * Constructor with string and length
105    */
106   prefix_t(const std::string& s, uint8_t len);
107   /**
108    * Copy Constructor
109    */
110   prefix_t(const prefix_t&);
111   /**
112    * Constructor with VPP API prefix representation
113    */
114   prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
115   /**
116    * Destructor
117    */
118   ~prefix_t();
119
120   /**
121    * Get the address
122    */
123   const boost::asio::ip::address& address() const;
124
125   /**
126    * Get the network mask width
127    */
128   uint8_t mask_width() const;
129
130   /**
131    * Assignement
132    */
133   prefix_t& operator=(const prefix_t&);
134
135   /**
136    * Less than operator
137    */
138   bool operator<(const prefix_t& o) const;
139
140   /**
141    * equals operator
142    */
143   bool operator==(const prefix_t& o) const;
144
145   /**
146    * not equal opartor
147    */
148   bool operator!=(const prefix_t& o) const;
149
150   /**
151    * convert to string format for debug purposes
152    */
153   std::string to_string() const;
154
155   /**
156    * The all Zeros prefix
157    */
158   const static prefix_t ZERO;
159
160   /**
161    * The all Zeros v6 prefix
162    */
163   const static prefix_t ZEROv6;
164
165   /**
166    * Convert the prefix into VPP API parameters
167    */
168   void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
169
170   /**
171    * Return a address representation of the mask, e.g. 255.255.0.0
172    */
173   boost::asio::ip::address_v4 mask() const;
174
175   /**
176    * get the lowest address in the prefix
177    */
178   boost::asio::ip::address_v4 low() const;
179
180   /**
181    * Get the highest address in the prefix
182    */
183   boost::asio::ip::address_v4 high() const;
184
185   /**
186    * Get the L3 protocol
187    */
188   l3_proto_t l3_proto() const;
189
190 private:
191   /**
192    * The address
193    */
194   boost::asio::ip::address m_addr;
195
196   /**
197    * The prefix length
198    */
199   uint8_t m_len;
200 };
201 };
202
203 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
204                                       const boost::asio::ip::address_v4& addr2);
205
206 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
207                                       const boost::asio::ip::address_v4& addr2);
208
209 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
210
211 /**
212  * Ostream printer for prefix_t
213  */
214 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
215
216 /**
217  * Convert a boost address into a VPP bytes string
218  */
219 void to_bytes(const boost::asio::ip::address& addr,
220               uint8_t* is_ip6,
221               uint8_t* array);
222 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
223 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
224
225 /**
226  * Get the prefix mask length of a host route from the boost address
227  */
228 uint32_t mask_width(const boost::asio::ip::address& addr);
229
230 /**
231  * Convert a VPP byte stinrg into a boost addresss
232  */
233 boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array);
234 };
235
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "mozilla")
241  * End:
242  */
243
244 #endif