blob: 25b188ff86c7f038318c53341cb89dbdc36328a4 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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
23namespace 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 */
32class l3_proto_t : public enum_base<l3_proto_t>
33{
34public:
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
44private:
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/**
Neale Ranns041fa502017-12-20 08:49:51 -080052 * Ostream output for l3_proto_t
53 */
54std::ostream& operator<<(std::ostream& os, const l3_proto_t& l3p);
55
56/**
Neale Ranns812ed392017-10-16 04:20:13 -070057 * A next-hop protocol describes the protocol of a peer to which packets
58 * are sent after matching a route.
59 */
60class nh_proto_t : public enum_base<nh_proto_t>
61{
62public:
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
70private:
71 /**
72 * Private constructor taking the value and the string name
73 */
74 nh_proto_t(int v, const std::string& s);
75};
76
77namespace route {
78/**
79 * type def the table-id
80 */
81typedef uint32_t table_id_t;
82
83/**
84 * The table-id for the default table
85 */
86const static table_id_t DEFAULT_TABLE = 0;
87
88/**
89 * A prefix defintion. Address + length
90 */
91class prefix_t
92{
93public:
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 */
Neale Rannsd3464b52017-12-07 08:48:02 -0800178 boost::asio::ip::address mask() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700179
180 /**
181 * get the lowest address in the prefix
182 */
Neale Rannsd3464b52017-12-07 08:48:02 -0800183 prefix_t low() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700184
185 /**
186 * Get the highest address in the prefix
187 */
Neale Rannsd3464b52017-12-07 08:48:02 -0800188 prefix_t high() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700189
190 /**
191 * Get the L3 protocol
192 */
193 l3_proto_t l3_proto() const;
194
195private:
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
208boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
209 const boost::asio::ip::address_v4& addr2);
210
211boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
212 const boost::asio::ip::address_v4& addr2);
213
214boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
215
Neale Rannsd3464b52017-12-07 08:48:02 -0800216boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
217 const boost::asio::ip::address_v6& addr2);
218
219boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
220 const boost::asio::ip::address_v6& addr2);
221
222boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
223
224boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
225 const boost::asio::ip::address& addr2);
226
227boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
228 const boost::asio::ip::address& addr2);
229
230boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
231
Neale Ranns812ed392017-10-16 04:20:13 -0700232/**
233 * Ostream printer for prefix_t
234 */
235std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
236
237/**
238 * Convert a boost address into a VPP bytes string
239 */
240void to_bytes(const boost::asio::ip::address& addr,
241 uint8_t* is_ip6,
242 uint8_t* array);
243void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
244void 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 */
249uint32_t mask_width(const boost::asio::ip::address& addr);
250
251/**
252 * Convert a VPP byte stinrg into a boost addresss
253 */
254boost::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