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 {
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;
99 * TAPv2 interface type
101 const static type_t TAPV2;
104 * vhost-user interface type
106 const static type_t VHOST;
109 * bond interface type
111 const static type_t BOND;
116 const static type_t PIPE;
121 const static type_t PIPE_END;
124 * Convert VPP's name of the interface to a type
126 static type_t from_string(const std::string& str);
130 * Private constructor taking the value and the string name
132 type_t(int v, const std::string& s);
136 * The admin state of the interface
138 struct admin_state_t : enum_base<admin_state_t>
143 const static admin_state_t DOWN;
147 const static admin_state_t UP;
150 * Convert VPP's numerical value to enum type
152 static admin_state_t from_int(uint8_t val);
156 * Private constructor taking the value and the string name
158 admin_state_t(int v, const std::string& s);
162 * The oper state of the interface
164 struct oper_state_t : enum_base<oper_state_t>
167 * Operational DOWN state
169 const static oper_state_t DOWN;
171 * Operational UP state
173 const static oper_state_t UP;
176 * Convert VPP's numerical value to enum type
178 static oper_state_t from_int(uint8_t val);
182 * Private constructor taking the value and the string name
184 oper_state_t(int v, const std::string& s);
194 counter_t m_rx_unicast;
195 counter_t m_tx_unicast;
196 counter_t m_rx_multicast;
197 counter_t m_tx_multicast;
198 counter_t m_rx_broadcast;
199 counter_t m_tx_broadcast;
204 * Construct a new object matching the desried state
206 interface(const std::string& name,
209 const std::string& tag = "");
211 * Construct a new object matching the desried state mapped
212 * to a specific route_domain
214 interface(const std::string& name,
217 const route_domain& rd,
218 const std::string& tag = "");
222 virtual ~interface();
227 interface(const interface& o);
229 static const_iterator_t cbegin();
230 static const_iterator_t cend();
233 * Return the matching'singular' of the interface
235 std::shared_ptr<interface> singular() const;
238 * convert to string format for debug purposes
240 virtual std::string to_string(void) const;
243 * Return VPP's handle to this object
245 const handle_t& handle() const;
248 * Return the interface type
250 const type_t& type() const;
253 * Return the interface type
255 const std::string& name() const;
258 * Return the interface type
260 const key_t& key() const;
263 * Return the L2 Address
265 const l2_address_t& l2_address() const;
268 * Return the admin state
270 const admin_state_t& admin_state() const;
273 * Set the admin state of the interface
275 * N.B. All set function change only the attibute of the object on whcih
276 * they act, they do not make changes in VPP
278 void set(const admin_state_t& state);
283 void set(const l2_address_t& addr);
286 * Set the operational state of the interface, as reported by VPP
288 void set(const oper_state_t& state);
291 * Set the tag to the interface
293 void set(const std::string& tag);
296 * Get the interface stats
298 const stats_t& get_stats(void) const;
301 * Comparison operator - only used for UT
303 virtual bool operator==(const interface& i) const;
306 * A base class for interface Create commands
308 template <typename MSG>
309 class create_cmd : public rpc_cmd<HW::item<handle_t>, MSG>
312 create_cmd(HW::item<handle_t>& item, const std::string& name)
313 : rpc_cmd<HW::item<handle_t>, MSG>(item)
321 virtual ~create_cmd() = default;
324 * Comparison operator - only used for UT
326 virtual bool operator==(const create_cmd& o) const
328 return (m_name == o.m_name);
332 * Indicate the succeeded, when the HW Q is disabled.
336 rpc_cmd<HW::item<handle_t>, MSG>::succeeded();
337 interface::add(m_name, this->item());
341 * add the created interface to the DB
343 void insert_interface() { interface::add(m_name, this->item()); }
345 virtual vapi_error_e operator()(MSG& reply)
347 int sw_if_index = reply.get_response().get_payload().sw_if_index;
348 int retval = reply.get_response().get_payload().retval;
350 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " res:" << retval
351 << " sw-if-index:" << sw_if_index;
353 rc_t rc = rc_t::from_vpp_retval(retval);
354 handle_t handle = handle_t::INVALID;
356 if (rc_t::OK == rc) {
357 handle = sw_if_index;
360 this->fulfill(HW::item<handle_t>(handle, rc));
367 * The name of the interface to be created
369 const std::string& m_name;
373 * Base class for intterface Delete commands
375 template <typename MSG>
376 class delete_cmd : public rpc_cmd<HW::item<handle_t>, MSG>
379 delete_cmd(HW::item<handle_t>& item, const std::string& name)
380 : rpc_cmd<HW::item<handle_t>, MSG>(item)
385 delete_cmd(HW::item<handle_t>& item)
386 : rpc_cmd<HW::item<handle_t>, MSG>(item)
394 virtual ~delete_cmd() = default;
397 * Comparison operator - only used for UT
399 virtual bool operator==(const delete_cmd& o) const
401 return (this->m_hw_item == o.m_hw_item);
405 * Indicate the succeeded, when the HW Q is disabled.
410 * remove the deleted interface from the DB
412 void remove_interface() { interface::remove(this->item()); }
416 * The name of the interface to be created
418 const std::string m_name;
423 event(const interface& itf, const interface::oper_state_t& state)
429 const interface& itf;
430 interface::oper_state_t state;
434 * A class that listens to interface Events
440 * Default Constructor
445 * Virtual function called on the listener when the command has data
448 virtual void handle_interface_event(std::vector<event> es) = 0;
451 * Return the HW::item representing the status
453 HW::item<bool>& status();
457 * The status of the subscription
459 HW::item<bool> m_status;
463 * A class that listens to interface Stats
469 * Default Constructor
473 virtual ~stat_listener() = default;
476 * Virtual function called on the listener when the command has data
479 virtual void handle_interface_stat(const interface&) = 0;
482 * Return the HW::item representing the status
484 HW::item<bool>& status();
488 * The status of the subscription
490 HW::item<bool> m_status;
494 * The the singular instance of the interface in the DB by handle
496 static std::shared_ptr<interface> find(const handle_t& h);
499 * The the singular instance of the interface in the DB by key
501 static std::shared_ptr<interface> find(const key_t& k);
504 * Dump all interfaces into the stream provided
506 static void dump(std::ostream& os);
509 * Enable stats for this interface
511 void enable_stats(stat_listener* el,
512 const stats_type_t& st = stats_type_t::NORMAL);
515 * Disable stats for this interface
517 void disable_stats();
520 * Enable the reception of events of all interfaces
522 static void enable_events(interface::event_listener& el);
525 * disable the reception of events of all interfaces
527 static void disable_events();
531 * Set the handle of an interface object. Only called by the interface
532 * factory during the populate
534 void set(const handle_t& handle);
535 friend class interface_factory;
538 * The SW interface handle VPP has asigned to the interface
540 HW::item<handle_t> m_hdl;
543 * Return the matching 'singular' of the interface
545 virtual std::shared_ptr<interface> singular_i() const;
548 * release/remove an interface form the singular store
553 * Virtual functions to construct an interface create commands.
554 * Overridden in derived classes like the sub_interface
556 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
559 * Virtual functions to construct an interface delete commands.
560 * Overridden in derived classes like the sub_interface
562 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
565 * Sweep/reap the object if still stale
567 virtual void sweep(void);
570 * A map of all interfaces key against the interface's name
572 static singular_db<key_t, interface> m_db;
575 * Add an interface to the DB keyed on handle
577 static void add(const key_t& name, const HW::item<handle_t>& item);
580 * remove an interface from the DB keyed on handle
582 static void remove(const HW::item<handle_t>& item);
586 * Class definition for listeners to OM events
588 class event_handler : public OM::listener, public inspect::command_handler
592 virtual ~event_handler() = default;
595 * Handle a populate event
597 void handle_populate(const client_db::key_t& key);
600 * Handle a replay event
602 void handle_replay();
605 * Show the object in the Singular DB
607 void show(std::ostream& os);
610 * Get the sortable Id of the listener
612 dependency_t order() const;
615 static event_handler m_evh;
618 * friend with stat_reader
625 void publish_stats();
628 * Set the interface stat
630 void set(const counter_t& count, const std::string& stat_type);
633 * enable the interface stats in the singular instance
635 void enable_stats_i(stat_listener* el, const stats_type_t& st);
638 * disable the interface stats in the singular instance
640 void disable_stats_i();
643 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
645 void update(const interface& obj);
648 * return the interface's handle in the singular instance
650 const handle_t& handle_i() const;
653 * It's the OM class that calls singular()
658 * It's the singular_db class that calls replay()
660 friend class singular_db<key_t, interface>;
663 * The interfaces name
665 const std::string m_name;
668 * The interface type. clearly this cannot be changed
669 * once the interface has been created.
674 * shared pointer to the routeDoamin the interface is in.
675 * NULL is not mapped - i.e. in the default table
677 std::shared_ptr<route_domain> m_rd;
680 * The state of the interface
682 HW::item<admin_state_t> m_state;
685 * HW state of the VPP table mapping
687 HW::item<route::table_id_t> m_table_id;
690 * HW state of the L2 address
692 HW::item<l2_address_t> m_l2_address;
695 * The state of the detailed stats collection
697 HW::item<stats_type_t> m_stats_type;
705 * reference to stat listener
707 stat_listener* m_listener;
710 * Operational state of the interface
715 * tag of the interface
720 * A map of all interfaces keyed against VPP's handle
722 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
725 * replay the object to create it in hardware
727 virtual void replay(void);
730 * Create commands are firends so they can add interfaces to the
733 template <typename MSG>
734 friend class create_cmd;
737 * Create commands are firends so they can remove interfaces from the
740 template <typename MSG>
741 friend class delete_cmd;
743 static std::shared_ptr<interface_cmds::events_cmd> m_events_cmd;
747 * stream insertion operator for interface stats
749 std::ostream& operator<<(std::ostream& os, const interface::stats_t& stats);
752 * fd.io coding-style-patch-verification: ON
755 * eval: (c-set-style "mozilla")