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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_PREFIX_H__
17 #define __VOM_PREFIX_H__
19 #include "vom/enum_base.hpp"
20 #include <boost/asio/ip/address.hpp>
24 * Types belonging to Routing
28 * A next-hop protocol describes the protocol of a peer to which packets
29 * are sent after matching a route.
31 class nh_proto_t : public enum_base<nh_proto_t>
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;
39 static const nh_proto_t& from_address(const boost::asio::ip::address& addr);
43 * Private constructor taking the value and the string name
45 nh_proto_t(int v, const std::string& s);
49 * An L3 protocol can be used to construct a prefix that is used
50 * to match packets are part of a route.
52 class l3_proto_t : public enum_base<l3_proto_t>
55 const static l3_proto_t IPV4;
56 const static l3_proto_t IPV6;
57 const static l3_proto_t MPLS;
62 static const l3_proto_t& from_address(const boost::asio::ip::address& addr);
64 const nh_proto_t& to_nh_proto() const;
68 * Private constructor taking the value and the string name
70 l3_proto_t(int v, const std::string& s);
74 * Ostream output for l3_proto_t
76 std::ostream& operator<<(std::ostream& os, const l3_proto_t& l3p);
81 class ip_dscp_t : public enum_base<ip_dscp_t>
84 /* unfortunately some of the CSX names are defined in terminos.h
85 * as macros, hence the longer names */
86 const static ip_dscp_t DSCP_CS0;
87 const static ip_dscp_t DSCP_CS1;
88 const static ip_dscp_t DSCP_CS2;
89 const static ip_dscp_t DSCP_CS3;
90 const static ip_dscp_t DSCP_CS4;
91 const static ip_dscp_t DSCP_CS5;
92 const static ip_dscp_t DSCP_CS6;
93 const static ip_dscp_t DSCP_CS7;
94 const static ip_dscp_t DSCP_AF11;
95 const static ip_dscp_t DSCP_AF12;
96 const static ip_dscp_t DSCP_AF13;
97 const static ip_dscp_t DSCP_AF21;
98 const static ip_dscp_t DSCP_AF22;
99 const static ip_dscp_t DSCP_AF23;
100 const static ip_dscp_t DSCP_AF31;
101 const static ip_dscp_t DSCP_AF32;
102 const static ip_dscp_t DSCP_AF33;
103 const static ip_dscp_t DSCP_AF41;
104 const static ip_dscp_t DSCP_AF42;
105 const static ip_dscp_t DSCP_AF43;
106 const static ip_dscp_t DSCP_EF;
109 * Constructor allows the creation of any DSCP value
114 ip_dscp_t(int v, const std::string& s);
119 * type def the table-id
121 typedef uint32_t table_id_t;
124 * The table-id for the default table
126 const static table_id_t DEFAULT_TABLE = 0;
129 * A prefix defintion. Address + length
135 * Default Constructor - creates ::/0
139 * Constructor with address and length
141 prefix_t(const boost::asio::ip::address& addr, uint8_t len);
143 * Constructor with just the address, this creates a
146 prefix_t(const boost::asio::ip::address& addr);
149 * Constructor with string and length
151 prefix_t(const std::string& s, uint8_t len);
156 prefix_t(const prefix_t&);
159 * Constructor with VPP API prefix representation
161 prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
170 const boost::asio::ip::address& address() const;
173 * Get the network mask width
175 uint8_t mask_width() const;
180 prefix_t& operator=(const prefix_t&);
185 bool operator<(const prefix_t& o) const;
190 bool operator==(const prefix_t& o) const;
195 bool operator!=(const prefix_t& o) const;
198 * convert to string format for debug purposes
200 std::string to_string() const;
203 * The all Zeros prefix
205 const static prefix_t ZERO;
208 * The all Zeros v6 prefix
210 const static prefix_t ZEROv6;
213 * Convert the prefix into VPP API parameters
215 void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
218 * Return a address representation of the mask, e.g. 255.255.0.0
220 boost::asio::ip::address mask() const;
223 * get the lowest address in the prefix
225 prefix_t low() const;
228 * Get the highest address in the prefix
230 prefix_t high() const;
233 * Get the L3 protocol
235 l3_proto_t l3_proto() const;
241 boost::asio::ip::address m_addr;
250 * A prefix defintion. Address + length
256 * Default Constructor - creates ::/0
260 * Constructor for (S,G)
262 mprefix_t(const boost::asio::ip::address& saddr,
263 const boost::asio::ip::address& gaddr);
265 * Constructor for (*,G)
267 mprefix_t(const boost::asio::ip::address& gaddr);
270 * Constructor for (*,G/n)
272 mprefix_t(const boost::asio::ip::address& gaddr, uint8_t len);
275 *Constructor for (S,G)
277 mprefix_t(const boost::asio::ip::address& saddr,
278 const boost::asio::ip::address& gaddr,
284 mprefix_t(const mprefix_t&);
294 const boost::asio::ip::address& grp_address() const;
295 const boost::asio::ip::address& src_address() const;
298 * Get the network mask width
300 uint8_t mask_width() const;
305 mprefix_t& operator=(const mprefix_t&);
310 bool operator<(const mprefix_t& o) const;
315 bool operator==(const mprefix_t& o) const;
320 bool operator!=(const mprefix_t& o) const;
323 * convert to string format for debug purposes
325 std::string to_string() const;
328 * The all Zeros prefix
330 const static mprefix_t ZERO;
333 * The all Zeros v6 prefix
335 const static mprefix_t ZEROv6;
338 * Get the L3 protocol
340 l3_proto_t l3_proto() const;
346 boost::asio::ip::address m_gaddr;
347 boost::asio::ip::address m_saddr;
355 }; // namespace route
357 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
358 const boost::asio::ip::address_v4& addr2);
360 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
361 const boost::asio::ip::address_v4& addr2);
363 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
365 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
366 const boost::asio::ip::address_v6& addr2);
368 boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
369 const boost::asio::ip::address_v6& addr2);
371 boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
373 boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
374 const boost::asio::ip::address& addr2);
376 boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
377 const boost::asio::ip::address& addr2);
379 boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
382 * Ostream printer for prefix_t
384 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
387 * Convert a boost address into a VPP bytes string
389 void to_bytes(const boost::asio::ip::address& addr,
392 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
393 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
396 * Get the prefix mask length of a host route from the boost address
398 uint32_t mask_width(const boost::asio::ip::address& addr);
401 * Convert a VPP byte stinrg into a boost addresss
403 boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t* array);
407 * fd.io coding-style-patch-verification: ON
410 * eval: (c-set-style "mozilla")