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);
80 * type def the table-id
82 typedef uint32_t table_id_t;
85 * The table-id for the default table
87 const static table_id_t DEFAULT_TABLE = 0;
90 * A prefix defintion. Address + length
96 * Default Constructor - creates ::/0
100 * Constructor with address and length
102 prefix_t(const boost::asio::ip::address& addr, uint8_t len);
104 * Constructor with just the address, this creates a
107 prefix_t(const boost::asio::ip::address& addr);
110 * Constructor with string and length
112 prefix_t(const std::string& s, uint8_t len);
117 prefix_t(const prefix_t&);
120 * Constructor with VPP API prefix representation
122 prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len);
131 const boost::asio::ip::address& address() const;
134 * Get the network mask width
136 uint8_t mask_width() const;
141 prefix_t& operator=(const prefix_t&);
146 bool operator<(const prefix_t& o) const;
151 bool operator==(const prefix_t& o) const;
156 bool operator!=(const prefix_t& o) const;
159 * convert to string format for debug purposes
161 std::string to_string() const;
164 * The all Zeros prefix
166 const static prefix_t ZERO;
169 * The all Zeros v6 prefix
171 const static prefix_t ZEROv6;
174 * Convert the prefix into VPP API parameters
176 void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const;
179 * Return a address representation of the mask, e.g. 255.255.0.0
181 boost::asio::ip::address mask() const;
184 * get the lowest address in the prefix
186 prefix_t low() const;
189 * Get the highest address in the prefix
191 prefix_t high() const;
194 * Get the L3 protocol
196 l3_proto_t l3_proto() const;
202 boost::asio::ip::address m_addr;
211 * A prefix defintion. Address + length
217 * Default Constructor - creates ::/0
221 * Constructor for (S,G)
223 mprefix_t(const boost::asio::ip::address& saddr,
224 const boost::asio::ip::address& gaddr);
226 * Constructor for (*,G)
228 mprefix_t(const boost::asio::ip::address& gaddr);
231 * Constructor for (*,G/n)
233 mprefix_t(const boost::asio::ip::address& gaddr, uint8_t len);
236 *Constructor for (S,G)
238 mprefix_t(const boost::asio::ip::address& saddr,
239 const boost::asio::ip::address& gaddr,
245 mprefix_t(const mprefix_t&);
255 const boost::asio::ip::address& grp_address() const;
256 const boost::asio::ip::address& src_address() const;
259 * Get the network mask width
261 uint8_t mask_width() const;
266 mprefix_t& operator=(const mprefix_t&);
271 bool operator<(const mprefix_t& o) const;
276 bool operator==(const mprefix_t& o) const;
281 bool operator!=(const mprefix_t& o) const;
284 * convert to string format for debug purposes
286 std::string to_string() const;
289 * The all Zeros prefix
291 const static mprefix_t ZERO;
294 * The all Zeros v6 prefix
296 const static mprefix_t ZEROv6;
299 * Get the L3 protocol
301 l3_proto_t l3_proto() const;
303 void to_vpp(uint8_t* is_ip6,
306 uint16_t* len) const;
312 boost::asio::ip::address m_gaddr;
313 boost::asio::ip::address m_saddr;
321 }; // namespace route
323 boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1,
324 const boost::asio::ip::address_v4& addr2);
326 boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1,
327 const boost::asio::ip::address_v4& addr2);
329 boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1);
331 boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1,
332 const boost::asio::ip::address_v6& addr2);
334 boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1,
335 const boost::asio::ip::address_v6& addr2);
337 boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1);
339 boost::asio::ip::address operator|(const boost::asio::ip::address& addr1,
340 const boost::asio::ip::address& addr2);
342 boost::asio::ip::address operator&(const boost::asio::ip::address& addr1,
343 const boost::asio::ip::address& addr2);
345 boost::asio::ip::address operator~(const boost::asio::ip::address& addr1);
348 * Ostream printer for prefix_t
350 std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx);
353 * Convert a boost address into a VPP bytes string
355 void to_bytes(const boost::asio::ip::address& addr,
358 void to_bytes(const boost::asio::ip::address_v4& addr, uint8_t* array);
359 void to_bytes(const boost::asio::ip::address_v6& addr, uint8_t* array);
362 * Get the prefix mask length of a host route from the boost address
364 uint32_t mask_width(const boost::asio::ip::address& addr);
367 * Convert a VPP byte stinrg into a boost addresss
369 boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t* array);
373 * fd.io coding-style-patch-verification: ON
376 * eval: (c-set-style "mozilla")