blob: e395e1760ca995181b8ab51a30f1febb8fecf6e7 [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/**
52 * A next-hop protocol describes the protocol of a peer to which packets
53 * are sent after matching a route.
54 */
55class nh_proto_t : public enum_base<nh_proto_t>
56{
57public:
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
65private:
66 /**
67 * Private constructor taking the value and the string name
68 */
69 nh_proto_t(int v, const std::string& s);
70};
71
72namespace route {
73/**
74 * type def the table-id
75 */
76typedef uint32_t table_id_t;
77
78/**
79 * The table-id for the default table
80 */
81const static table_id_t DEFAULT_TABLE = 0;
82
83/**
84 * A prefix defintion. Address + length
85 */
86class prefix_t
87{
88public:
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 */
Neale Rannsd3464b52017-12-07 08:48:02 -0800173 boost::asio::ip::address mask() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700174
175 /**
176 * get the lowest address in the prefix
177 */
Neale Rannsd3464b52017-12-07 08:48:02 -0800178 prefix_t low() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700179
180 /**
181 * Get the highest address in the prefix
182 */
Neale Rannsd3464b52017-12-07 08:48:02 -0800183 prefix_t high() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700184
185 /**
186 * Get the L3 protocol
187 */
188 l3_proto_t l3_proto() const;
189
190private:
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
203boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
204 const boost::asio::ip::address_v4& addr2);
205
206boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
207 const boost::asio::ip::address_v4& addr2);
208
209boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
210
Neale Rannsd3464b52017-12-07 08:48:02 -0800211boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
212 const boost::asio::ip::address_v6& addr2);
213
214boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
215 const boost::asio::ip::address_v6& addr2);
216
217boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
218
219boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
220 const boost::asio::ip::address& addr2);
221
222boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
223 const boost::asio::ip::address& addr2);
224
225boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
226
Neale Ranns812ed392017-10-16 04:20:13 -0700227/**
228 * Ostream printer for prefix_t
229 */
230std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
231
232/**
233 * Convert a boost address into a VPP bytes string
234 */
235void to_bytes(const boost::asio::ip::address& addr,
236 uint8_t* is_ip6,
237 uint8_t* array);
238void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
239void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
240
241/**
242 * Get the prefix mask length of a host route from the boost address
243 */
244uint32_t mask_width(const boost::asio::ip::address& addr);
245
246/**
247 * Convert a VPP byte stinrg into a boost addresss
248 */
249boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array);
250};
251
252/*
253 * fd.io coding-style-patch-verification: ON
254 *
255 * Local Variables:
256 * eval: (c-set-style "mozilla")
257 * End:
258 */
259
260#endif