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 * An L3 protocol can be used to construct a prefix that is used
30 * to match packets are part of a route.
32 class l3_proto_t : public enum_base<l3_proto_t>
35 const static l3_proto_t IPV4;
36 const static l3_proto_t IPV6;
37 const static l3_proto_t MPLS;
42 static const l3_proto_t& from_address(const boost::asio::ip::address& addr);
46 * Private constructor taking the value and the string name
48 l3_proto_t(int v, const std::string& s);
52 * A next-hop protocol describes the protocol of a peer to which packets
53 * are sent after matching a route.
55 class nh_proto_t : public enum_base<nh_proto_t>
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;
63 static const nh_proto_t& from_address(const boost::asio::ip::address& addr);
67 * Private constructor taking the value and the string name
69 nh_proto_t(int v, const std::string& s);
74 * type def the table-id
76 typedef uint32_t table_id_t;
79 * The table-id for the default table
81 const static table_id_t DEFAULT_TABLE = 0;
84 * A prefix defintion. Address + length
90 * Default Constructor - creates ::/0
94 * Constructor with address and length
96 prefix_t(const boost::asio::ip::address& addr, uint8_t len);
98 * Constructor with just the address, this creates a
101 prefix_t(const boost::asio::ip::address& addr);
104 * Constructor with string and length
106 prefix_t(const std::string& s, uint8_t len);
110 prefix_t(const prefix_t&);
112 * Constructor with VPP API prefix representation
114 prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
123 const boost::asio::ip::address& address() const;
126 * Get the network mask width
128 uint8_t mask_width() const;
133 prefix_t& operator=(const prefix_t&);
138 bool operator<(const prefix_t& o) const;
143 bool operator==(const prefix_t& o) const;
148 bool operator!=(const prefix_t& o) const;
151 * convert to string format for debug purposes
153 std::string to_string() const;
156 * The all Zeros prefix
158 const static prefix_t ZERO;
161 * The all Zeros v6 prefix
163 const static prefix_t ZEROv6;
166 * Convert the prefix into VPP API parameters
168 void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
171 * Return a address representation of the mask, e.g. 255.255.0.0
173 boost::asio::ip::address mask() const;
176 * get the lowest address in the prefix
178 prefix_t low() const;
181 * Get the highest address in the prefix
183 prefix_t high() const;
186 * Get the L3 protocol
188 l3_proto_t l3_proto() const;
194 boost::asio::ip::address m_addr;
203 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
204 const boost::asio::ip::address_v4& addr2);
206 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
207 const boost::asio::ip::address_v4& addr2);
209 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
211 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
212 const boost::asio::ip::address_v6& addr2);
214 boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
215 const boost::asio::ip::address_v6& addr2);
217 boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
219 boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
220 const boost::asio::ip::address& addr2);
222 boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
223 const boost::asio::ip::address& addr2);
225 boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
228 * Ostream printer for prefix_t
230 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
233 * Convert a boost address into a VPP bytes string
235 void to_bytes(const boost::asio::ip::address& addr,
238 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
239 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
242 * Get the prefix mask length of a host route from the boost address
244 uint32_t mask_width(const boost::asio::ip::address& addr);
247 * Convert a VPP byte stinrg into a boost addresss
249 boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array);
253 * fd.io coding-style-patch-verification: ON
256 * eval: (c-set-style "mozilla")