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_INTERFACE_H__ |
| 17 | #define __VOM_INTERFACE_H__ |
| 18 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 19 | #include "vom/enum_base.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 20 | #include "vom/hw.hpp" |
| 21 | #include "vom/inspect.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 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 29 | namespace VOM { |
| 30 | /** |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 31 | * Forward declaration of the stats and events command |
| 32 | */ |
| 33 | namespace interface_cmds { |
| 34 | class stats_cmd; |
| 35 | class events_cmd; |
| 36 | }; |
| 37 | |
| 38 | /** |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 39 | * A representation of an interface in VPP |
| 40 | */ |
| 41 | class interface : public object_base |
| 42 | { |
| 43 | public: |
| 44 | /** |
| 45 | * The key for interface's key |
| 46 | */ |
| 47 | typedef std::string key_type; |
| 48 | |
| 49 | /** |
| 50 | * The iterator type |
| 51 | */ |
| 52 | typedef singular_db<const std::string, interface>::const_iterator |
| 53 | const_iterator_t; |
| 54 | |
| 55 | /** |
| 56 | * An interface type |
| 57 | */ |
| 58 | struct type_t : enum_base<type_t> |
| 59 | { |
| 60 | /** |
| 61 | * Unkown type |
| 62 | */ |
| 63 | const static type_t UNKNOWN; |
| 64 | /** |
| 65 | * A brideged Virtual interface (aka SVI or IRB) |
| 66 | */ |
| 67 | const static type_t BVI; |
| 68 | /** |
| 69 | * VXLAN interface |
| 70 | */ |
| 71 | const static type_t VXLAN; |
| 72 | /** |
| 73 | * Ethernet interface type |
| 74 | */ |
| 75 | const static type_t ETHERNET; |
| 76 | /** |
| 77 | * AF-Packet interface type |
| 78 | */ |
| 79 | const static type_t AFPACKET; |
| 80 | /** |
| 81 | * loopback interface type |
| 82 | */ |
| 83 | const static type_t LOOPBACK; |
| 84 | /** |
| 85 | * Local interface type (specific to VPP) |
| 86 | */ |
| 87 | const static type_t LOCAL; |
| 88 | /** |
| 89 | * TAP interface type |
| 90 | */ |
| 91 | const static type_t TAP; |
| 92 | |
| 93 | /** |
| 94 | * Convert VPP's name of the interface to a type |
| 95 | */ |
| 96 | static type_t from_string(const std::string& str); |
| 97 | |
| 98 | private: |
| 99 | /** |
| 100 | * Private constructor taking the value and the string name |
| 101 | */ |
| 102 | type_t(int v, const std::string& s); |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * The admin state of the interface |
| 107 | */ |
| 108 | struct admin_state_t : enum_base<admin_state_t> |
| 109 | { |
| 110 | /** |
| 111 | * Admin DOWN state |
| 112 | */ |
| 113 | const static admin_state_t DOWN; |
| 114 | /** |
| 115 | * Admin UP state |
| 116 | */ |
| 117 | const static admin_state_t UP; |
| 118 | |
| 119 | /** |
| 120 | * Convert VPP's numerical value to enum type |
| 121 | */ |
| 122 | static admin_state_t from_int(uint8_t val); |
| 123 | |
| 124 | private: |
| 125 | /** |
| 126 | * Private constructor taking the value and the string name |
| 127 | */ |
| 128 | admin_state_t(int v, const std::string& s); |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * The oper state of the interface |
| 133 | */ |
| 134 | struct oper_state_t : enum_base<oper_state_t> |
| 135 | { |
| 136 | /** |
| 137 | * Operational DOWN state |
| 138 | */ |
| 139 | const static oper_state_t DOWN; |
| 140 | /** |
| 141 | * Operational UP state |
| 142 | */ |
| 143 | const static oper_state_t UP; |
| 144 | |
| 145 | /** |
| 146 | * Convert VPP's numerical value to enum type |
| 147 | */ |
| 148 | static oper_state_t from_int(uint8_t val); |
| 149 | |
| 150 | private: |
| 151 | /** |
| 152 | * Private constructor taking the value and the string name |
| 153 | */ |
| 154 | oper_state_t(int v, const std::string& s); |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * Construct a new object matching the desried state |
| 159 | */ |
| 160 | interface(const std::string& name, type_t type, admin_state_t state); |
| 161 | /** |
| 162 | * Construct a new object matching the desried state mapped |
| 163 | * to a specific route_domain |
| 164 | */ |
| 165 | interface(const std::string& name, |
| 166 | type_t type, |
| 167 | admin_state_t state, |
| 168 | const route_domain& rd); |
| 169 | /** |
| 170 | * Destructor |
| 171 | */ |
| 172 | virtual ~interface(); |
| 173 | |
| 174 | /** |
| 175 | * Copy Constructor |
| 176 | */ |
| 177 | interface(const interface& o); |
| 178 | |
| 179 | static const_iterator_t cbegin(); |
| 180 | static const_iterator_t cend(); |
| 181 | |
| 182 | /** |
| 183 | * Return the matching'singular' of the interface |
| 184 | */ |
| 185 | std::shared_ptr<interface> singular() const; |
| 186 | |
| 187 | /** |
| 188 | * convert to string format for debug purposes |
| 189 | */ |
| 190 | virtual std::string to_string(void) const; |
| 191 | |
| 192 | /** |
| 193 | * Return VPP's handle to this object |
| 194 | */ |
| 195 | const handle_t& handle() const; |
| 196 | |
| 197 | /** |
| 198 | * Return the interface type |
| 199 | */ |
| 200 | const type_t& type() const; |
| 201 | |
| 202 | /** |
| 203 | * Return the interface type |
| 204 | */ |
| 205 | const std::string& name() const; |
| 206 | |
| 207 | /** |
| 208 | * Return the interface type |
| 209 | */ |
| 210 | const key_type& key() const; |
| 211 | |
| 212 | /** |
| 213 | * Return the L2 Address |
| 214 | */ |
| 215 | const l2_address_t& l2_address() const; |
| 216 | |
| 217 | /** |
| 218 | * Set the L2 Address |
| 219 | */ |
| 220 | void set(const l2_address_t& addr); |
| 221 | |
| 222 | /** |
| 223 | * Set the operational state of the interface, as reported by VPP |
| 224 | */ |
| 225 | void set(const oper_state_t& state); |
| 226 | |
| 227 | /** |
| 228 | * A base class for interface Create commands |
| 229 | */ |
| 230 | template <typename MSG> |
| 231 | class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG> |
| 232 | { |
| 233 | public: |
| 234 | create_cmd(HW::item<handle_t>& item, const std::string& name) |
| 235 | : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item) |
| 236 | , m_name(name) |
| 237 | { |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Destructor |
| 242 | */ |
| 243 | virtual ~create_cmd() = default; |
| 244 | |
| 245 | /** |
| 246 | * Comparison operator - only used for UT |
| 247 | */ |
| 248 | virtual bool operator==(const create_cmd& o) const |
| 249 | { |
| 250 | return (m_name == o.m_name); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Indicate the succeeded, when the HW Q is disabled. |
| 255 | */ |
| 256 | void succeeded() |
| 257 | { |
| 258 | rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded(); |
| 259 | interface::add(m_name, this->item()); |
| 260 | } |
| 261 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 262 | /** |
| 263 | * add the created interface to the DB |
| 264 | */ |
| 265 | void insert_interface() { interface::add(m_name, this->item()); } |
| 266 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 267 | virtual vapi_error_e operator()(MSG& reply) |
| 268 | { |
| 269 | int sw_if_index = reply.get_response().get_payload().sw_if_index; |
| 270 | int retval = reply.get_response().get_payload().retval; |
| 271 | |
| 272 | VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval; |
| 273 | |
| 274 | rc_t rc = rc_t::from_vpp_retval(retval); |
| 275 | handle_t handle = handle_t::INVALID; |
| 276 | |
| 277 | if (rc_t::OK == rc) { |
| 278 | handle = sw_if_index; |
| 279 | } |
| 280 | |
| 281 | HW::item<handle_t> res(handle, rc); |
| 282 | |
| 283 | this->fulfill(res); |
| 284 | |
| 285 | return (VAPI_OK); |
| 286 | } |
| 287 | |
| 288 | protected: |
| 289 | /** |
| 290 | * The name of the interface to be created |
| 291 | */ |
| 292 | const std::string& m_name; |
| 293 | }; |
| 294 | |
| 295 | /** |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 296 | * Base class for intterface Delete commands |
| 297 | */ |
| 298 | template <typename MSG> |
| 299 | class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG> |
| 300 | { |
| 301 | public: |
| 302 | delete_cmd(HW::item<handle_t>& item, const std::string& name) |
| 303 | : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item) |
| 304 | , m_name(name) |
| 305 | { |
| 306 | } |
| 307 | |
| 308 | delete_cmd(HW::item<handle_t>& item) |
| 309 | : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item) |
| 310 | , m_name() |
| 311 | { |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Destructor |
| 316 | */ |
| 317 | virtual ~delete_cmd() = default; |
| 318 | |
| 319 | /** |
| 320 | * Comparison operator - only used for UT |
| 321 | */ |
| 322 | virtual bool operator==(const delete_cmd& o) const |
| 323 | { |
| 324 | return (this->m_hw_item == o.m_hw_item); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Indicate the succeeded, when the HW Q is disabled. |
| 329 | */ |
| 330 | void succeeded() {} |
| 331 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 332 | /** |
| 333 | * add the created interface to the DB |
| 334 | */ |
| 335 | void remove_interface() { interface::remove(this->item()); } |
| 336 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 337 | protected: |
| 338 | /** |
| 339 | * The name of the interface to be created |
| 340 | */ |
| 341 | const std::string m_name; |
| 342 | }; |
| 343 | |
| 344 | /** |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 345 | * A class that listens to interface Events |
| 346 | */ |
| 347 | class event_listener |
| 348 | { |
| 349 | public: |
| 350 | /** |
| 351 | * Default Constructor |
| 352 | */ |
| 353 | event_listener(); |
| 354 | |
| 355 | /** |
| 356 | * Virtual function called on the listener when the command has data |
| 357 | * ready to process |
| 358 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 359 | virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Return the HW::item representing the status |
| 363 | */ |
| 364 | HW::item<bool>& status(); |
| 365 | |
| 366 | protected: |
| 367 | /** |
| 368 | * The status of the subscription |
| 369 | */ |
| 370 | HW::item<bool> m_status; |
| 371 | }; |
| 372 | |
| 373 | /** |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 374 | * A class that listens to interface Stats |
| 375 | */ |
| 376 | class stat_listener |
| 377 | { |
| 378 | public: |
| 379 | /** |
| 380 | * Default Constructor |
| 381 | */ |
| 382 | stat_listener(); |
| 383 | |
| 384 | /** |
| 385 | * Virtual function called on the listener when the command has data |
| 386 | * ready to process |
| 387 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 388 | virtual void handle_interface_stat(interface_cmds::stats_cmd* cmd) = 0; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * Return the HW::item representing the status |
| 392 | */ |
| 393 | HW::item<bool>& status(); |
| 394 | |
| 395 | protected: |
| 396 | /** |
| 397 | * The status of the subscription |
| 398 | */ |
| 399 | HW::item<bool> m_status; |
| 400 | }; |
| 401 | |
| 402 | /** |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 403 | * The the singular instance of the interface in the object_base-Model |
| 404 | */ |
| 405 | static std::shared_ptr<interface> find(const interface& temp); |
| 406 | |
| 407 | /** |
| 408 | * The the singular instance of the interface in the object_base-Model |
| 409 | * by handle |
| 410 | */ |
| 411 | static std::shared_ptr<interface> find(const handle_t& h); |
| 412 | |
| 413 | /** |
| 414 | * The the singular instance of the interface in the object_base-Model |
| 415 | * by name |
| 416 | */ |
| 417 | static std::shared_ptr<interface> find(const std::string& s); |
| 418 | |
| 419 | /** |
| 420 | * Dump all interfaces into the stream provided |
| 421 | */ |
| 422 | static void dump(std::ostream& os); |
| 423 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 424 | protected: |
| 425 | /** |
| 426 | * Construct an interface object with a handle and a HW address |
| 427 | */ |
| 428 | interface(const handle_t& handle, |
| 429 | const l2_address_t& l2_address, |
| 430 | const std::string& name, |
| 431 | type_t type, |
| 432 | admin_state_t state); |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 433 | friend class interface_factory; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 434 | |
| 435 | /** |
| 436 | * The SW interface handle VPP has asigned to the interface |
| 437 | */ |
| 438 | HW::item<handle_t> m_hdl; |
| 439 | |
| 440 | /** |
| 441 | * Return the matching 'singular' of the interface |
| 442 | */ |
| 443 | virtual std::shared_ptr<interface> singular_i() const; |
| 444 | |
| 445 | /** |
| 446 | * release/remove an interface form the singular store |
| 447 | */ |
| 448 | void release(); |
| 449 | |
| 450 | /** |
| 451 | * Virtual functions to construct an interface create commands. |
| 452 | * Overridden in derived classes like the sub_interface |
| 453 | */ |
| 454 | virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds); |
| 455 | |
| 456 | /** |
| 457 | * Virtual functions to construct an interface delete commands. |
| 458 | * Overridden in derived classes like the sub_interface |
| 459 | */ |
| 460 | virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds); |
| 461 | |
| 462 | /** |
| 463 | * Sweep/reap the object if still stale |
| 464 | */ |
| 465 | virtual void sweep(void); |
| 466 | |
| 467 | /** |
| 468 | * A map of all interfaces key against the interface's name |
| 469 | */ |
| 470 | static singular_db<const std::string, interface> m_db; |
| 471 | |
| 472 | /** |
| 473 | * Add an interface to the DB keyed on handle |
| 474 | */ |
| 475 | static void add(const std::string& name, const HW::item<handle_t>& item); |
| 476 | |
| 477 | /** |
| 478 | * remove an interface from the DB keyed on handle |
| 479 | */ |
| 480 | static void remove(const HW::item<handle_t>& item); |
| 481 | |
| 482 | private: |
| 483 | /** |
| 484 | * Class definition for listeners to OM events |
| 485 | */ |
| 486 | class event_handler : public OM::listener, public inspect::command_handler |
| 487 | { |
| 488 | public: |
| 489 | event_handler(); |
| 490 | virtual ~event_handler() = default; |
| 491 | |
| 492 | /** |
| 493 | * Handle a populate event |
| 494 | */ |
| 495 | void handle_populate(const client_db::key_t& key); |
| 496 | |
| 497 | /** |
| 498 | * Handle a replay event |
| 499 | */ |
| 500 | void handle_replay(); |
| 501 | |
| 502 | /** |
| 503 | * Show the object in the Singular DB |
| 504 | */ |
| 505 | void show(std::ostream& os); |
| 506 | |
| 507 | /** |
| 508 | * Get the sortable Id of the listener |
| 509 | */ |
| 510 | dependency_t order() const; |
| 511 | }; |
| 512 | |
| 513 | static event_handler m_evh; |
| 514 | |
| 515 | /** |
| 516 | * Commit the acculmulated changes into VPP. i.e. to a 'HW" write. |
| 517 | */ |
| 518 | void update(const interface& obj); |
| 519 | |
| 520 | /* |
Neale Ranns | 263f955 | 2017-11-15 02:52:13 -0800 | [diff] [blame] | 521 | * return the interface's handle in the singular instance |
| 522 | */ |
| 523 | const handle_t& handle_i() const; |
| 524 | |
| 525 | /* |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 526 | * It's the OM class that calls singular() |
| 527 | */ |
| 528 | friend class OM; |
| 529 | |
| 530 | /** |
| 531 | * It's the singular_db class that calls replay() |
| 532 | */ |
| 533 | friend class singular_db<const std::string, interface>; |
| 534 | |
| 535 | /** |
| 536 | * The interfaces name |
| 537 | */ |
| 538 | const std::string m_name; |
| 539 | |
| 540 | /** |
| 541 | * The interface type. clearly this cannot be changed |
| 542 | * once the interface has been created. |
| 543 | */ |
| 544 | const type_t m_type; |
| 545 | |
| 546 | /** |
| 547 | * shared pointer to the routeDoamin the interface is in. |
Neale Ranns | 352ea0c | 2017-11-14 11:04:28 -0800 | [diff] [blame] | 548 | * NULL is not mapped - i.e. in the default table |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 549 | */ |
Neale Ranns | 352ea0c | 2017-11-14 11:04:28 -0800 | [diff] [blame] | 550 | std::shared_ptr<route_domain> m_rd; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 551 | |
| 552 | /** |
| 553 | * The state of the interface |
| 554 | */ |
| 555 | HW::item<admin_state_t> m_state; |
| 556 | |
| 557 | /** |
| 558 | * HW state of the VPP table mapping |
| 559 | */ |
| 560 | HW::item<route::table_id_t> m_table_id; |
| 561 | |
| 562 | /** |
| 563 | * HW state of the L2 address |
| 564 | */ |
| 565 | HW::item<l2_address_t> m_l2_address; |
| 566 | |
| 567 | /** |
| 568 | * Operational state of the interface |
| 569 | */ |
| 570 | oper_state_t m_oper; |
| 571 | |
| 572 | /** |
| 573 | * A map of all interfaces keyed against VPP's handle |
| 574 | */ |
| 575 | static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db; |
| 576 | |
| 577 | /** |
| 578 | * replay the object to create it in hardware |
| 579 | */ |
| 580 | virtual void replay(void); |
| 581 | |
| 582 | /** |
| 583 | * Create commands are firends so they can add interfaces to the |
| 584 | * handle store. |
| 585 | */ |
| 586 | template <typename MSG> |
| 587 | friend class create_cmd; |
| 588 | |
| 589 | /** |
| 590 | * Create commands are firends so they can remove interfaces from the |
| 591 | * handle store. |
| 592 | */ |
| 593 | template <typename MSG> |
| 594 | friend class delete_cmd; |
| 595 | }; |
| 596 | }; |
| 597 | /* |
| 598 | * fd.io coding-style-patch-verification: ON |
| 599 | * |
| 600 | * Local Variables: |
| 601 | * eval: (c-set-style "mozilla") |
| 602 | * End: |
| 603 | */ |
| 604 | #endif |