Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace VOM { |
| 24 | /** |
| 25 | * Types belonging to Routing |
| 26 | */ |
| 27 | |
| 28 | /** |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 29 | * A next-hop protocol describes the protocol of a peer to which packets |
| 30 | * are sent after matching a route. |
| 31 | */ |
| 32 | class nh_proto_t : public enum_base<nh_proto_t> |
| 33 | { |
| 34 | public: |
| 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; |
| 39 | |
| 40 | static const nh_proto_t& from_address(const boost::asio::ip::address& addr); |
| 41 | |
| 42 | private: |
| 43 | /** |
| 44 | * Private constructor taking the value and the string name |
| 45 | */ |
| 46 | nh_proto_t(int v, const std::string& s); |
| 47 | }; |
| 48 | |
Neale Ranns | f068c3e | 2018-01-03 04:18:48 -0800 | [diff] [blame] | 49 | /** |
| 50 | * An L3 protocol can be used to construct a prefix that is used |
| 51 | * to match packets are part of a route. |
| 52 | */ |
| 53 | class l3_proto_t : public enum_base<l3_proto_t> |
| 54 | { |
| 55 | public: |
| 56 | const static l3_proto_t IPV4; |
| 57 | const static l3_proto_t IPV6; |
| 58 | const static l3_proto_t MPLS; |
| 59 | |
| 60 | bool is_ipv4(); |
| 61 | bool is_ipv6(); |
| 62 | |
| 63 | static const l3_proto_t& from_address(const boost::asio::ip::address& addr); |
| 64 | |
| 65 | const nh_proto_t& to_nh_proto() const; |
| 66 | |
| 67 | private: |
| 68 | /** |
| 69 | * Private constructor taking the value and the string name |
| 70 | */ |
| 71 | l3_proto_t(int v, const std::string& s); |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * Ostream output for l3_proto_t |
| 76 | */ |
| 77 | std::ostream& operator<<(std::ostream& os, const l3_proto_t& l3p); |
| 78 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 79 | namespace route { |
| 80 | /** |
| 81 | * type def the table-id |
| 82 | */ |
| 83 | typedef uint32_t table_id_t; |
| 84 | |
| 85 | /** |
| 86 | * The table-id for the default table |
| 87 | */ |
| 88 | const static table_id_t DEFAULT_TABLE = 0; |
| 89 | |
| 90 | /** |
| 91 | * A prefix defintion. Address + length |
| 92 | */ |
| 93 | class prefix_t |
| 94 | { |
| 95 | public: |
| 96 | /** |
| 97 | * Default Constructor - creates ::/0 |
| 98 | */ |
| 99 | prefix_t(); |
| 100 | /** |
| 101 | * Constructor with address and length |
| 102 | */ |
| 103 | prefix_t(const boost::asio::ip::address& addr, uint8_t len); |
| 104 | /** |
| 105 | * Constructor with just the address, this creates a |
| 106 | * host prefix |
| 107 | */ |
| 108 | prefix_t(const boost::asio::ip::address& addr); |
| 109 | |
| 110 | /** |
| 111 | * Constructor with string and length |
| 112 | */ |
| 113 | prefix_t(const std::string& s, uint8_t len); |
| 114 | /** |
| 115 | * Copy Constructor |
| 116 | */ |
| 117 | prefix_t(const prefix_t&); |
| 118 | /** |
| 119 | * Constructor with VPP API prefix representation |
| 120 | */ |
| 121 | prefix_t(uint8_t is_ip6, uint8_t* addr, uint8_t len); |
| 122 | /** |
| 123 | * Destructor |
| 124 | */ |
| 125 | ~prefix_t(); |
| 126 | |
| 127 | /** |
| 128 | * Get the address |
| 129 | */ |
| 130 | const boost::asio::ip::address& address() const; |
| 131 | |
| 132 | /** |
| 133 | * Get the network mask width |
| 134 | */ |
| 135 | uint8_t mask_width() const; |
| 136 | |
| 137 | /** |
| 138 | * Assignement |
| 139 | */ |
| 140 | prefix_t& operator=(const prefix_t&); |
| 141 | |
| 142 | /** |
| 143 | * Less than operator |
| 144 | */ |
| 145 | bool operator<(const prefix_t& o) const; |
| 146 | |
| 147 | /** |
| 148 | * equals operator |
| 149 | */ |
| 150 | bool operator==(const prefix_t& o) const; |
| 151 | |
| 152 | /** |
| 153 | * not equal opartor |
| 154 | */ |
| 155 | bool operator!=(const prefix_t& o) const; |
| 156 | |
| 157 | /** |
| 158 | * convert to string format for debug purposes |
| 159 | */ |
| 160 | std::string to_string() const; |
| 161 | |
| 162 | /** |
| 163 | * The all Zeros prefix |
| 164 | */ |
| 165 | const static prefix_t ZERO; |
| 166 | |
| 167 | /** |
| 168 | * The all Zeros v6 prefix |
| 169 | */ |
| 170 | const static prefix_t ZEROv6; |
| 171 | |
| 172 | /** |
| 173 | * Convert the prefix into VPP API parameters |
| 174 | */ |
| 175 | void to_vpp(uint8_t* is_ip6, uint8_t* addr, uint8_t* len) const; |
| 176 | |
| 177 | /** |
| 178 | * Return a address representation of the mask, e.g. 255.255.0.0 |
| 179 | */ |
Neale Ranns | d3464b5 | 2017-12-07 08:48:02 -0800 | [diff] [blame] | 180 | boost::asio::ip::address mask() const; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * get the lowest address in the prefix |
| 184 | */ |
Neale Ranns | d3464b5 | 2017-12-07 08:48:02 -0800 | [diff] [blame] | 185 | prefix_t low() const; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * Get the highest address in the prefix |
| 189 | */ |
Neale Ranns | d3464b5 | 2017-12-07 08:48:02 -0800 | [diff] [blame] | 190 | prefix_t high() const; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Get the L3 protocol |
| 194 | */ |
| 195 | l3_proto_t l3_proto() const; |
| 196 | |
| 197 | private: |
| 198 | /** |
| 199 | * The address |
| 200 | */ |
| 201 | boost::asio::ip::address m_addr; |
| 202 | |
| 203 | /** |
| 204 | * The prefix length |
| 205 | */ |
| 206 | uint8_t m_len; |
| 207 | }; |
| 208 | }; |
| 209 | |
| 210 | boost::asio::ip::address_v4 operator|(const boost::asio::ip::address_v4& addr1, |
| 211 | const boost::asio::ip::address_v4& addr2); |
| 212 | |
| 213 | boost::asio::ip::address_v4 operator&(const boost::asio::ip::address_v4& addr1, |
| 214 | const boost::asio::ip::address_v4& addr2); |
| 215 | |
| 216 | boost::asio::ip::address_v4 operator~(const boost::asio::ip::address_v4& addr1); |
| 217 | |
Neale Ranns | d3464b5 | 2017-12-07 08:48:02 -0800 | [diff] [blame] | 218 | boost::asio::ip::address_v6 operator|(const boost::asio::ip::address_v6& addr1, |
| 219 | const boost::asio::ip::address_v6& addr2); |
| 220 | |
| 221 | boost::asio::ip::address_v6 operator&(const boost::asio::ip::address_v6& addr1, |
| 222 | const boost::asio::ip::address_v6& addr2); |
| 223 | |
| 224 | boost::asio::ip::address_v6 operator~(const boost::asio::ip::address_v6& addr1); |
| 225 | |
| 226 | boost::asio::ip::address operator|(const boost::asio::ip::address& addr1, |
| 227 | const boost::asio::ip::address& addr2); |
| 228 | |
| 229 | boost::asio::ip::address operator&(const boost::asio::ip::address& addr1, |
| 230 | const boost::asio::ip::address& addr2); |
| 231 | |
| 232 | boost::asio::ip::address operator~(const boost::asio::ip::address& addr1); |
| 233 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Ostream printer for prefix_t |
| 236 | */ |
| 237 | std::ostream& operator<<(std::ostream& os, const route::prefix_t& pfx); |
| 238 | |
| 239 | /** |
| 240 | * Convert a boost address into a VPP bytes string |
| 241 | */ |
| 242 | void to_bytes(const boost::asio::ip::address& addr, |
| 243 | uint8_t* is_ip6, |
| 244 | uint8_t* array); |
| 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); |
| 247 | |
| 248 | /** |
| 249 | * Get the prefix mask length of a host route from the boost address |
| 250 | */ |
| 251 | uint32_t mask_width(const boost::asio::ip::address& addr); |
| 252 | |
| 253 | /** |
| 254 | * Convert a VPP byte stinrg into a boost addresss |
| 255 | */ |
| 256 | boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t* array); |
| 257 | }; |
| 258 | |
| 259 | /* |
| 260 | * fd.io coding-style-patch-verification: ON |
| 261 | * |
| 262 | * Local Variables: |
| 263 | * eval: (c-set-style "mozilla") |
| 264 | * End: |
| 265 | */ |
| 266 | |
| 267 | #endif |