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"
28 #include "vom/stat_client.hpp"
32 * Forward declaration of the stats and events command
34 namespace interface_cmds {
40 * A representation of an interface in VPP
42 class interface : public object_base
45 struct stats_type_t : public enum_base<stats_type_t>
47 const static stats_type_t DETAILED;
48 const static stats_type_t NORMAL;
51 stats_type_t(int v, const std::string& s);
55 * The key for interface's key
57 typedef std::string key_t;
62 typedef singular_db<const std::string, interface>::const_iterator
68 struct type_t : enum_base<type_t>
73 const static type_t UNKNOWN;
75 * A brideged Virtual interface (aka SVI or IRB)
77 const static type_t BVI;
81 const static type_t VXLAN;
83 * Ethernet interface type
85 const static type_t ETHERNET;
87 * AF-Packet interface type
89 const static type_t AFPACKET;
91 * loopback interface type
93 const static type_t LOOPBACK;
95 * Local interface type (specific to VPP)
97 const static type_t LOCAL;
100 * TAPv2 interface type
102 const static type_t TAPV2;
105 * vhost-user interface type
107 const static type_t VHOST;
110 * bond interface type
112 const static type_t BOND;
117 const static type_t PIPE;
122 const static type_t PIPE_END;
125 * Convert VPP's name of the interface to a type
127 static type_t from_string(const std::string& str);
131 * Private constructor taking the value and the string name
133 type_t(int v, const std::string& s);
137 * The admin state of the interface
139 struct admin_state_t : enum_base<admin_state_t>
144 const static admin_state_t DOWN;
148 const static admin_state_t UP;
151 * Convert VPP's numerical value to enum type
153 static admin_state_t from_int(uint8_t val);
157 * Private constructor taking the value and the string name
159 admin_state_t(int v, const std::string& s);
163 * The oper state of the interface
165 struct oper_state_t : enum_base<oper_state_t>
168 * Operational DOWN state
170 const static oper_state_t DOWN;
172 * Operational UP state
174 const static oper_state_t UP;
177 * Convert VPP's numerical value to enum type
179 static oper_state_t from_int(uint8_t val);
183 * Private constructor taking the value and the string name
185 oper_state_t(int v, const std::string& s);
195 counter_t m_rx_unicast;
196 counter_t m_tx_unicast;
197 counter_t m_rx_multicast;
198 counter_t m_tx_multicast;
199 counter_t m_rx_broadcast;
200 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 * Set the admin state of the interface
270 void set(const admin_state_t& state);
275 void set(const l2_address_t& addr);
278 * Set the operational state of the interface, as reported by VPP
280 void set(const oper_state_t& state);
283 * Set the tag to the interface
285 void set(const std::string& tag);
288 * Get the interface stats
290 const stats_t& get_stats(void) const;
293 * Comparison operator - only used for UT
295 virtual bool operator==(const interface& i) const;
298 * A base class for interface Create commands
300 template <typename MSG>
301 class create_cmd : public rpc_cmd<HW::item<handle_t>, MSG>
304 create_cmd(HW::item<handle_t>& item, const std::string& name)
305 : rpc_cmd<HW::item<handle_t>, MSG>(item)
313 virtual ~create_cmd() = default;
316 * Comparison operator - only used for UT
318 virtual bool operator==(const create_cmd& o) const
320 return (m_name == o.m_name);
324 * Indicate the succeeded, when the HW Q is disabled.
328 rpc_cmd<HW::item<handle_t>, MSG>::succeeded();
329 interface::add(m_name, this->item());
333 * add the created interface to the DB
335 void insert_interface() { interface::add(m_name, this->item()); }
337 virtual vapi_error_e operator()(MSG& reply)
339 int sw_if_index = reply.get_response().get_payload().sw_if_index;
340 int retval = reply.get_response().get_payload().retval;
342 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " res:" << retval
343 << " sw-if-index:" << sw_if_index;
345 rc_t rc = rc_t::from_vpp_retval(retval);
346 handle_t handle = handle_t::INVALID;
348 if (rc_t::OK == rc) {
349 handle = sw_if_index;
352 this->fulfill(HW::item<handle_t>(handle, rc));
359 * The name of the interface to be created
361 const std::string& m_name;
365 * Base class for intterface Delete commands
367 template <typename MSG>
368 class delete_cmd : public rpc_cmd<HW::item<handle_t>, MSG>
371 delete_cmd(HW::item<handle_t>& item, const std::string& name)
372 : rpc_cmd<HW::item<handle_t>, MSG>(item)
377 delete_cmd(HW::item<handle_t>& item)
378 : rpc_cmd<HW::item<handle_t>, MSG>(item)
386 virtual ~delete_cmd() = default;
389 * Comparison operator - only used for UT
391 virtual bool operator==(const delete_cmd& o) const
393 return (this->m_hw_item == o.m_hw_item);
397 * Indicate the succeeded, when the HW Q is disabled.
402 * remove the deleted interface from the DB
404 void remove_interface() { interface::remove(this->item()); }
408 * The name of the interface to be created
410 const std::string m_name;
415 event(const interface& itf, const interface::oper_state_t& state)
421 const interface& itf;
422 interface::oper_state_t state;
426 * A class that listens to interface Events
432 * Default Constructor
437 * Virtual function called on the listener when the command has data
440 virtual void handle_interface_event(std::vector<event> es) = 0;
443 * Return the HW::item representing the status
445 HW::item<bool>& status();
449 * The status of the subscription
451 HW::item<bool> m_status;
455 * A class that listens to interface Stats
461 * Default Constructor
465 virtual ~stat_listener() = default;
468 * Virtual function called on the listener when the command has data
471 virtual void handle_interface_stat(const interface&) = 0;
474 * Return the HW::item representing the status
476 HW::item<bool>& status();
480 * The status of the subscription
482 HW::item<bool> m_status;
486 * The the singular instance of the interface in the DB by handle
488 static std::shared_ptr<interface> find(const handle_t& h);
491 * The the singular instance of the interface in the DB by key
493 static std::shared_ptr<interface> find(const key_t& k);
496 * Dump all interfaces into the stream provided
498 static void dump(std::ostream& os);
501 * Enable stats for this interface
503 void enable_stats(stat_listener* el,
504 const stats_type_t& st = stats_type_t::NORMAL);
507 * Disable stats for this interface
509 void disable_stats();
512 * Enable the reception of events of all interfaces
514 static void enable_events(interface::event_listener& el);
517 * disable the reception of events of all interfaces
519 static void disable_events();
523 * Set the handle of an interface object. Only called by the interface
524 * factory during the populate
526 void set(const handle_t& handle);
527 friend class interface_factory;
530 * The SW interface handle VPP has asigned to the interface
532 HW::item<handle_t> m_hdl;
535 * Return the matching 'singular' of the interface
537 virtual std::shared_ptr<interface> singular_i() const;
540 * release/remove an interface form the singular store
545 * Virtual functions to construct an interface create commands.
546 * Overridden in derived classes like the sub_interface
548 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
551 * Virtual functions to construct an interface delete commands.
552 * Overridden in derived classes like the sub_interface
554 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
557 * Sweep/reap the object if still stale
559 virtual void sweep(void);
562 * A map of all interfaces key against the interface's name
564 static singular_db<key_t, interface> m_db;
567 * Add an interface to the DB keyed on handle
569 static void add(const key_t& name, const HW::item<handle_t>& item);
572 * remove an interface from the DB keyed on handle
574 static void remove(const HW::item<handle_t>& item);
578 * Class definition for listeners to OM events
580 class event_handler : public OM::listener, public inspect::command_handler
584 virtual ~event_handler() = default;
587 * Handle a populate event
589 void handle_populate(const client_db::key_t& key);
592 * Handle a replay event
594 void handle_replay();
597 * Show the object in the Singular DB
599 void show(std::ostream& os);
602 * Get the sortable Id of the listener
604 dependency_t order() const;
607 static event_handler m_evh;
610 * friend with stat_reader
617 void publish_stats();
620 * Set the interface stat
622 void set(counter_t count, const std::string& stat_type);
625 * enable the interface stats in the singular instance
627 void enable_stats_i(stat_listener* el, const stats_type_t& st);
630 * disable the interface stats in the singular instance
632 void disable_stats_i();
635 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
637 void update(const interface& obj);
640 * return the interface's handle in the singular instance
642 const handle_t& handle_i() const;
645 * It's the OM class that calls singular()
650 * It's the singular_db class that calls replay()
652 friend class singular_db<key_t, interface>;
655 * The interfaces name
657 const std::string m_name;
660 * The interface type. clearly this cannot be changed
661 * once the interface has been created.
666 * shared pointer to the routeDoamin the interface is in.
667 * NULL is not mapped - i.e. in the default table
669 std::shared_ptr<route_domain> m_rd;
672 * The state of the interface
674 HW::item<admin_state_t> m_state;
677 * HW state of the VPP table mapping
679 HW::item<route::table_id_t> m_table_id;
682 * HW state of the L2 address
684 HW::item<l2_address_t> m_l2_address;
687 * The state of the detailed stats collection
689 HW::item<stats_type_t> m_stats_type;
697 * reference to stat listener
699 stat_listener* m_listener;
702 * Operational state of the interface
707 * tag of the interface
712 * A map of all interfaces keyed against VPP's handle
714 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
717 * replay the object to create it in hardware
719 virtual void replay(void);
722 * Create commands are firends so they can add interfaces to the
725 template <typename MSG>
726 friend class create_cmd;
729 * Create commands are firends so they can remove interfaces from the
732 template <typename MSG>
733 friend class delete_cmd;
735 static std::shared_ptr<interface_cmds::events_cmd> m_events_cmd;
739 * stream insertion operator for interface stats
741 std::ostream& operator<<(std::ostream& os, const interface::stats_t& stats);
744 * fd.io coding-style-patch-verification: ON
747 * eval: (c-set-style "mozilla")