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_VXLAN_TUNNEL_H__ |
| 17 | #define __VOM_VXLAN_TUNNEL_H__ |
| 18 | |
| 19 | #include "vom/hw.hpp" |
| 20 | #include "vom/inspect.hpp" |
| 21 | #include "vom/interface.hpp" |
| 22 | #include "vom/object_base.hpp" |
| 23 | #include "vom/om.hpp" |
| 24 | #include "vom/prefix.hpp" |
| 25 | #include "vom/route_domain.hpp" |
| 26 | #include "vom/rpc_cmd.hpp" |
| 27 | #include "vom/singular_db.hpp" |
| 28 | |
| 29 | #include <vapi/vxlan.api.vapi.hpp> |
| 30 | |
| 31 | namespace VOM { |
| 32 | /** |
| 33 | * A representation of a VXLAN Tunnel in VPP |
| 34 | */ |
| 35 | class vxlan_tunnel : public interface |
| 36 | { |
| 37 | public: |
| 38 | /** |
| 39 | * Combaintion of attributes that are a unique key |
| 40 | * for a VXLAN tunnel |
| 41 | */ |
| 42 | struct endpoint_t |
| 43 | { |
| 44 | /** |
| 45 | * Default constructor |
| 46 | */ |
| 47 | endpoint_t(); |
| 48 | /** |
| 49 | * Constructor taking endpoint values |
| 50 | */ |
| 51 | endpoint_t(const boost::asio::ip::address& src, |
| 52 | const boost::asio::ip::address& dst, |
| 53 | uint32_t vni); |
| 54 | |
| 55 | /** |
| 56 | * less-than operator for map storage |
| 57 | */ |
| 58 | bool operator<(const endpoint_t& o) const; |
| 59 | |
| 60 | /** |
| 61 | * Comparison operator |
| 62 | */ |
| 63 | bool operator==(const endpoint_t& o) const; |
| 64 | |
| 65 | /** |
| 66 | * Debug print function |
| 67 | */ |
| 68 | std::string to_string() const; |
| 69 | |
| 70 | /** |
| 71 | * The src IP address of the endpoint |
| 72 | */ |
| 73 | boost::asio::ip::address src; |
| 74 | |
| 75 | /** |
| 76 | * The destination IP address of the endpoint |
| 77 | */ |
| 78 | boost::asio::ip::address dst; |
| 79 | |
| 80 | /** |
| 81 | * The VNI of the endpoint |
| 82 | */ |
| 83 | uint32_t vni; |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Construct a new object matching the desried state |
| 88 | */ |
| 89 | vxlan_tunnel(const boost::asio::ip::address& src, |
| 90 | const boost::asio::ip::address& dst, |
| 91 | uint32_t vni); |
| 92 | |
| 93 | /** |
| 94 | * Construct a new object matching the desried state with a handle |
| 95 | * read from VPP |
| 96 | */ |
| 97 | vxlan_tunnel(const handle_t& hdl, |
| 98 | const boost::asio::ip::address& src, |
| 99 | const boost::asio::ip::address& dst, |
| 100 | uint32_t vni); |
| 101 | |
| 102 | /* |
| 103 | * Destructor |
| 104 | */ |
| 105 | ~vxlan_tunnel(); |
| 106 | |
| 107 | /** |
| 108 | * Copy constructor |
| 109 | */ |
| 110 | vxlan_tunnel(const vxlan_tunnel& o); |
| 111 | |
| 112 | /** |
| 113 | * Return the matching 'singular instance' |
| 114 | */ |
| 115 | std::shared_ptr<vxlan_tunnel> singular() const; |
| 116 | |
| 117 | /** |
| 118 | * Debug rpint function |
| 119 | */ |
| 120 | virtual std::string to_string() const; |
| 121 | |
| 122 | /** |
| 123 | * Return VPP's handle to this object |
| 124 | */ |
| 125 | const handle_t& handle() const; |
| 126 | |
| 127 | /** |
| 128 | * Dump all L3Configs into the stream provided |
| 129 | */ |
| 130 | static void dump(std::ostream& os); |
| 131 | |
| 132 | /** |
| 133 | * A Command class that creates an VXLAN tunnel |
| 134 | */ |
| 135 | class create_cmd : public interface::create_cmd<vapi::Vxlan_add_del_tunnel> |
| 136 | { |
| 137 | public: |
| 138 | /** |
| 139 | * Create command constructor taking HW item to update and the |
| 140 | * endpoint values |
| 141 | */ |
| 142 | create_cmd(HW::item<handle_t>& item, |
| 143 | const std::string& name, |
| 144 | const endpoint_t& ep); |
| 145 | |
| 146 | /** |
| 147 | * Issue the command to VPP/HW |
| 148 | */ |
| 149 | rc_t issue(connection& con); |
| 150 | /** |
| 151 | * convert to string format for debug purposes |
| 152 | */ |
| 153 | std::string to_string() const; |
| 154 | |
| 155 | /** |
| 156 | * Comparison operator - only used for UT |
| 157 | */ |
| 158 | bool operator==(const create_cmd& i) const; |
| 159 | |
| 160 | private: |
| 161 | /** |
| 162 | * Enpoint values of the tunnel to be created |
| 163 | */ |
| 164 | const endpoint_t m_ep; |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * A functor class that creates an VXLAN tunnel |
| 169 | */ |
| 170 | class delete_cmd : public interface::delete_cmd<vapi::Vxlan_add_del_tunnel> |
| 171 | { |
| 172 | public: |
| 173 | /** |
| 174 | * delete command constructor taking HW item to update and the |
| 175 | * endpoint values |
| 176 | */ |
| 177 | delete_cmd(HW::item<handle_t>& item, const endpoint_t& ep); |
| 178 | |
| 179 | /** |
| 180 | * Issue the command to VPP/HW |
| 181 | */ |
| 182 | rc_t issue(connection& con); |
| 183 | |
| 184 | /** |
| 185 | * convert to string format for debug purposes |
| 186 | */ |
| 187 | std::string to_string() const; |
| 188 | |
| 189 | /** |
| 190 | * Comparison operator - only used for UT |
| 191 | */ |
| 192 | bool operator==(const delete_cmd& i) const; |
| 193 | |
| 194 | private: |
| 195 | /** |
| 196 | * Enpoint values of the tunnel to be deleted |
| 197 | */ |
| 198 | const endpoint_t m_ep; |
| 199 | }; |
| 200 | |
| 201 | /** |
| 202 | * A cmd class that Dumps all the Vpp interfaces |
| 203 | */ |
| 204 | class dump_cmd : public VOM::dump_cmd<vapi::Vxlan_tunnel_dump> |
| 205 | { |
| 206 | public: |
| 207 | /** |
| 208 | * Default Constructor |
| 209 | */ |
| 210 | dump_cmd(); |
| 211 | |
| 212 | /** |
| 213 | * Issue the command to VPP/HW |
| 214 | */ |
| 215 | rc_t issue(connection& con); |
| 216 | /** |
| 217 | * convert to string format for debug purposes |
| 218 | */ |
| 219 | std::string to_string() const; |
| 220 | |
| 221 | /** |
| 222 | * Comparison operator - only used for UT |
| 223 | */ |
| 224 | bool operator==(const dump_cmd& i) const; |
| 225 | }; |
| 226 | |
| 227 | private: |
| 228 | /** |
| 229 | * Class definition for listeners to OM events |
| 230 | */ |
| 231 | class event_handler : public OM::listener, public inspect::command_handler |
| 232 | { |
| 233 | public: |
| 234 | event_handler(); |
| 235 | virtual ~event_handler() = default; |
| 236 | |
| 237 | /** |
| 238 | * Handle a populate event |
| 239 | */ |
| 240 | void handle_populate(const client_db::key_t& key); |
| 241 | |
| 242 | /** |
| 243 | * Handle a replay event |
| 244 | */ |
| 245 | void handle_replay(); |
| 246 | |
| 247 | /** |
| 248 | * Show the object in the Singular DB |
| 249 | */ |
| 250 | void show(std::ostream& os); |
| 251 | |
| 252 | /** |
| 253 | * Get the sortable Id of the listener |
| 254 | */ |
| 255 | dependency_t order() const; |
| 256 | }; |
| 257 | |
| 258 | /** |
| 259 | * Event handle to register with OM |
| 260 | */ |
| 261 | static event_handler m_evh; |
| 262 | |
| 263 | /** |
| 264 | * Commit the acculmulated changes into VPP. i.e. to a 'HW" write. |
| 265 | */ |
| 266 | void update(const vxlan_tunnel& obj); |
| 267 | |
| 268 | /** |
| 269 | * Return the matching 'instance' of the sub-interface |
| 270 | * over-ride from the base class |
| 271 | */ |
| 272 | std::shared_ptr<interface> singular_i() const; |
| 273 | |
| 274 | /** |
| 275 | * Find the VXLAN tunnel in the OM |
| 276 | */ |
| 277 | static std::shared_ptr<vxlan_tunnel> find_or_add(const vxlan_tunnel& temp); |
| 278 | |
| 279 | /* |
| 280 | * It's the VPPHW class that updates the objects in HW |
| 281 | */ |
| 282 | friend class OM; |
| 283 | |
| 284 | /** |
| 285 | * It's the singular_db class that calls replay() |
| 286 | */ |
| 287 | friend class singular_db<endpoint_t, vxlan_tunnel>; |
| 288 | |
| 289 | /** |
| 290 | * Sweep/reap the object if still stale |
| 291 | */ |
| 292 | void sweep(void); |
| 293 | |
| 294 | /** |
| 295 | * replay the object to create it in hardware |
| 296 | */ |
| 297 | void replay(void); |
| 298 | |
| 299 | /** |
| 300 | * Tunnel enpoint/key |
| 301 | */ |
| 302 | endpoint_t m_tep; |
| 303 | |
| 304 | /** |
| 305 | * A map of all VLAN tunnela against thier key |
| 306 | */ |
| 307 | static singular_db<endpoint_t, vxlan_tunnel> m_db; |
| 308 | |
| 309 | /** |
| 310 | * Construct a unique name for the tunnel |
| 311 | */ |
| 312 | static std::string mk_name(const boost::asio::ip::address& src, |
| 313 | const boost::asio::ip::address& dst, |
| 314 | uint32_t vni); |
| 315 | }; |
| 316 | |
| 317 | /** |
| 318 | * Ostream output for a tunnel endpoint |
| 319 | */ |
| 320 | std::ostream& operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep); |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * fd.io coding-style-patch-verification: ON |
| 325 | * |
| 326 | * Local Variables: |
| 327 | * eval: (c-set-style "mozilla") |
| 328 | * End: |
| 329 | */ |
| 330 | |
| 331 | #endif |