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 <boost/asio/ip/address.hpp>
21 #include "vom/enum_base.hpp"
25 * Types belonging to Routing
29 * A next-hop protocol describes the protocol of a peer to which packets
30 * are sent after matching a route.
32 class nh_proto_t : public enum_base<nh_proto_t>
35 const static nh_proto_t IPV4;
36 const static nh_proto_t IPV6;
37 const static nh_proto_t MPLS;
38 const static nh_proto_t ETHERNET;
40 static const nh_proto_t& from_address(const boost::asio::ip::address& addr);
44 * Private constructor taking the value and the string name
46 nh_proto_t(int v, const std::string& s);
50 * An L3 protocol can be used to construct a prefix that is used
51 * to match packets are part of a route.
53 class l3_proto_t : public enum_base<l3_proto_t>
56 const static l3_proto_t IPV4;
57 const static l3_proto_t IPV6;
58 const static l3_proto_t MPLS;
63 static const l3_proto_t& from_address(const boost::asio::ip::address& addr);
65 const nh_proto_t& to_nh_proto() const;
69 * Private constructor taking the value and the string name
71 l3_proto_t(int v, const std::string& s);
75 * Ostream output for l3_proto_t
77 std::ostream& operator<<(std::ostream& os, const l3_proto_t& l3p);
81 * type def the table-id
83 typedef uint32_t table_id_t;
86 * The table-id for the default table
88 const static table_id_t DEFAULT_TABLE = 0;
91 * A prefix defintion. Address + length
97 * Default Constructor - creates ::/0
101 * Constructor with address and length
103 prefix_t(const boost::asio::ip::address& addr, uint8_t len);
105 * Constructor with just the address, this creates a
108 prefix_t(const boost::asio::ip::address& addr);
111 * Constructor with string and length
113 prefix_t(const std::string& s, uint8_t len);
117 prefix_t(const prefix_t&);
119 * Constructor with VPP API prefix representation
121 prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
130 const boost::asio::ip::address& address() const;
133 * Get the network mask width
135 uint8_t mask_width() const;
140 prefix_t& operator=(const prefix_t&);
145 bool operator<(const prefix_t& o) const;
150 bool operator==(const prefix_t& o) const;
155 bool operator!=(const prefix_t& o) const;
158 * convert to string format for debug purposes
160 std::string to_string() const;
163 * The all Zeros prefix
165 const static prefix_t ZERO;
168 * The all Zeros v6 prefix
170 const static prefix_t ZEROv6;
173 * Convert the prefix into VPP API parameters
175 void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
178 * Return a address representation of the mask, e.g. 255.255.0.0
180 boost::asio::ip::address mask() const;
183 * get the lowest address in the prefix
185 prefix_t low() const;
188 * Get the highest address in the prefix
190 prefix_t high() const;
193 * Get the L3 protocol
195 l3_proto_t l3_proto() const;
201 boost::asio::ip::address m_addr;
210 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
211 const boost::asio::ip::address_v4& addr2);
213 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
214 const boost::asio::ip::address_v4& addr2);
216 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
218 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
219 const boost::asio::ip::address_v6& addr2);
221 boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
222 const boost::asio::ip::address_v6& addr2);
224 boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
226 boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
227 const boost::asio::ip::address& addr2);
229 boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
230 const boost::asio::ip::address& addr2);
232 boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
235 * Ostream printer for prefix_t
237 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
240 * Convert a boost address into a VPP bytes string
242 void to_bytes(const boost::asio::ip::address& addr,
245 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
246 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
249 * Get the prefix mask length of a host route from the boost address
251 uint32_t mask_width(const boost::asio::ip::address& addr);
254 * Convert a VPP byte stinrg into a boost addresss
256 boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array);
260 * fd.io coding-style-patch-verification: ON
263 * eval: (c-set-style "mozilla")