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_INTERFACE_H__
17 #define __VOM_INTERFACE_H__
19 #include "vom/enum_base.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/object_base.hpp"
24 #include "vom/prefix.hpp"
25 #include "vom/route_domain.hpp"
26 #include "vom/rpc_cmd.hpp"
27 #include "vom/singular_db.hpp"
31 * Forward declaration of the stats and events command
33 namespace interface_cmds {
34 class stats_enable_cmd;
39 * A representation of an interface in VPP
41 class interface : public object_base
44 struct stats_type_t : public enum_base<stats_type_t>
46 const static stats_type_t DETAILED;
47 const static stats_type_t NORMAL;
50 stats_type_t(int v, const std::string& s);
54 * The key for interface's key
56 typedef std::string key_t;
61 typedef singular_db<const std::string, interface>::const_iterator
67 struct type_t : enum_base<type_t>
72 const static type_t UNKNOWN;
74 * A brideged Virtual interface (aka SVI or IRB)
76 const static type_t BVI;
80 const static type_t VXLAN;
82 * Ethernet interface type
84 const static type_t ETHERNET;
86 * AF-Packet interface type
88 const static type_t AFPACKET;
90 * loopback interface type
92 const static type_t LOOPBACK;
94 * Local interface type (specific to VPP)
96 const static type_t LOCAL;
100 const static type_t TAP;
103 * vhost-user interface type
105 const static type_t VHOST;
108 * bond interface type
110 const static type_t BOND;
113 * Convert VPP's name of the interface to a type
115 static type_t from_string(const std::string& str);
119 * Private constructor taking the value and the string name
121 type_t(int v, const std::string& s);
125 * The admin state of the interface
127 struct admin_state_t : enum_base<admin_state_t>
132 const static admin_state_t DOWN;
136 const static admin_state_t UP;
139 * Convert VPP's numerical value to enum type
141 static admin_state_t from_int(uint8_t val);
145 * Private constructor taking the value and the string name
147 admin_state_t(int v, const std::string& s);
151 * The oper state of the interface
153 struct oper_state_t : enum_base<oper_state_t>
156 * Operational DOWN state
158 const static oper_state_t DOWN;
160 * Operational UP state
162 const static oper_state_t UP;
165 * Convert VPP's numerical value to enum type
167 static oper_state_t from_int(uint8_t val);
171 * Private constructor taking the value and the string name
173 oper_state_t(int v, const std::string& s);
177 * Construct a new object matching the desried state
179 interface(const std::string& name,
182 const std::string& tag = "");
184 * Construct a new object matching the desried state mapped
185 * to a specific route_domain
187 interface(const std::string& name,
190 const route_domain& rd,
191 const std::string& tag = "");
195 virtual ~interface();
200 interface(const interface& o);
202 static const_iterator_t cbegin();
203 static const_iterator_t cend();
206 * Return the matching'singular' of the interface
208 std::shared_ptr<interface> singular() const;
211 * convert to string format for debug purposes
213 virtual std::string to_string(void) const;
216 * Return VPP's handle to this object
218 const handle_t& handle() const;
221 * Return the interface type
223 const type_t& type() const;
226 * Return the interface type
228 const std::string& name() const;
231 * Return the interface type
233 const key_t& key() const;
236 * Return the L2 Address
238 const l2_address_t& l2_address() const;
241 * Set the admin state of the interface
243 void set(const admin_state_t& state);
248 void set(const l2_address_t& addr);
251 * Set the operational state of the interface, as reported by VPP
253 void set(const oper_state_t& state);
256 * Set the tag to the interface
258 void set(const std::string& tag);
261 * Comparison operator - only used for UT
263 virtual bool operator==(const interface& i) const;
266 * A base class for interface Create commands
268 template <typename MSG>
269 class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
272 create_cmd(HW::item<handle_t>& item, const std::string& name)
273 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
281 virtual ~create_cmd() = default;
284 * Comparison operator - only used for UT
286 virtual bool operator==(const create_cmd& o) const
288 return (m_name == o.m_name);
292 * Indicate the succeeded, when the HW Q is disabled.
296 rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
297 interface::add(m_name, this->item());
301 * add the created interface to the DB
303 void insert_interface() { interface::add(m_name, this->item()); }
305 virtual vapi_error_e operator()(MSG& reply)
307 int sw_if_index = reply.get_response().get_payload().sw_if_index;
308 int retval = reply.get_response().get_payload().retval;
310 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
312 rc_t rc = rc_t::from_vpp_retval(retval);
313 handle_t handle = handle_t::INVALID;
315 if (rc_t::OK == rc) {
316 handle = sw_if_index;
319 HW::item<handle_t> res(handle, rc);
328 * The name of the interface to be created
330 const std::string& m_name;
334 * Base class for intterface Delete commands
336 template <typename MSG>
337 class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
340 delete_cmd(HW::item<handle_t>& item, const std::string& name)
341 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
346 delete_cmd(HW::item<handle_t>& item)
347 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
355 virtual ~delete_cmd() = default;
358 * Comparison operator - only used for UT
360 virtual bool operator==(const delete_cmd& o) const
362 return (this->m_hw_item == o.m_hw_item);
366 * Indicate the succeeded, when the HW Q is disabled.
371 * remove the deleted interface from the DB
373 void remove_interface() { interface::remove(this->item()); }
377 * The name of the interface to be created
379 const std::string m_name;
383 * A class that listens to interface Events
389 * Default Constructor
394 * Virtual function called on the listener when the command has data
397 virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
400 * Return the HW::item representing the status
402 HW::item<bool>& status();
406 * The status of the subscription
408 HW::item<bool> m_status;
412 * A class that listens to interface Stats
418 * Default Constructor
423 * Virtual function called on the listener when the command has data
426 virtual void handle_interface_stat(
427 interface_cmds::stats_enable_cmd* cmd) = 0;
430 * Return the HW::item representing the status
432 HW::item<bool>& status();
436 * The status of the subscription
438 HW::item<bool> m_status;
442 * The the singular instance of the interface in the DB by handle
444 static std::shared_ptr<interface> find(const handle_t& h);
447 * The the singular instance of the interface in the DB by key
449 static std::shared_ptr<interface> find(const key_t& k);
452 * Dump all interfaces into the stream provided
454 static void dump(std::ostream& os);
457 * Enable stats for this interface
459 void enable_stats(stat_listener& el,
460 const stats_type_t& st = stats_type_t::NORMAL);
464 * Set the handle of an interface object. Only called by the interface
465 * factory during the populate
467 void set(const handle_t& handle);
468 friend class interface_factory;
471 * The SW interface handle VPP has asigned to the interface
473 HW::item<handle_t> m_hdl;
476 * Return the matching 'singular' of the interface
478 virtual std::shared_ptr<interface> singular_i() const;
481 * release/remove an interface form the singular store
486 * Virtual functions to construct an interface create commands.
487 * Overridden in derived classes like the sub_interface
489 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
492 * Virtual functions to construct an interface delete commands.
493 * Overridden in derived classes like the sub_interface
495 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
498 * Sweep/reap the object if still stale
500 virtual void sweep(void);
503 * A map of all interfaces key against the interface's name
505 static singular_db<key_t, interface> m_db;
508 * Add an interface to the DB keyed on handle
510 static void add(const key_t& name, const HW::item<handle_t>& item);
513 * remove an interface from the DB keyed on handle
515 static void remove(const HW::item<handle_t>& item);
519 * Class definition for listeners to OM events
521 class event_handler : public OM::listener, public inspect::command_handler
525 virtual ~event_handler() = default;
528 * Handle a populate event
530 void handle_populate(const client_db::key_t& key);
533 * Handle a replay event
535 void handle_replay();
538 * Show the object in the Singular DB
540 void show(std::ostream& os);
543 * Get the sortable Id of the listener
545 dependency_t order() const;
548 static event_handler m_evh;
551 * enable the interface stats in the singular instance
553 void enable_stats_i(stat_listener& el, const stats_type_t& st);
556 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
558 void update(const interface& obj);
561 * return the interface's handle in the singular instance
563 const handle_t& handle_i() const;
566 * It's the OM class that calls singular()
571 * It's the singular_db class that calls replay()
573 friend class singular_db<key_t, interface>;
576 * The interfaces name
578 const std::string m_name;
581 * The interface type. clearly this cannot be changed
582 * once the interface has been created.
587 * shared pointer to the routeDoamin the interface is in.
588 * NULL is not mapped - i.e. in the default table
590 std::shared_ptr<route_domain> m_rd;
593 * shared pointer to the stats object for this interface.
595 std::shared_ptr<interface_cmds::stats_enable_cmd> m_stats;
598 * The state of the interface
600 HW::item<admin_state_t> m_state;
603 * HW state of the VPP table mapping
605 HW::item<route::table_id_t> m_table_id;
608 * HW state of the L2 address
610 HW::item<l2_address_t> m_l2_address;
613 * The state of the detailed stats collection
615 HW::item<stats_type_t> m_stats_type;
618 * Operational state of the interface
623 * tag of the interface
628 * A map of all interfaces keyed against VPP's handle
630 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
633 * replay the object to create it in hardware
635 virtual void replay(void);
638 * Create commands are firends so they can add interfaces to the
641 template <typename MSG>
642 friend class create_cmd;
645 * Create commands are firends so they can remove interfaces from the
648 template <typename MSG>
649 friend class delete_cmd;
653 * fd.io coding-style-patch-verification: ON
656 * eval: (c-set-style "mozilla")