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
45 * The key for interface's key
47 typedef std::string key_t;
52 typedef singular_db<const std::string, interface>::const_iterator
58 struct type_t : enum_base<type_t>
63 const static type_t UNKNOWN;
65 * A brideged Virtual interface (aka SVI or IRB)
67 const static type_t BVI;
71 const static type_t VXLAN;
73 * Ethernet interface type
75 const static type_t ETHERNET;
77 * AF-Packet interface type
79 const static type_t AFPACKET;
81 * loopback interface type
83 const static type_t LOOPBACK;
85 * Local interface type (specific to VPP)
87 const static type_t LOCAL;
91 const static type_t TAP;
94 * vhost-user interface type
96 const static type_t VHOST;
101 const static type_t BOND;
104 * Convert VPP's name of the interface to a type
106 static type_t from_string(const std::string& str);
110 * Private constructor taking the value and the string name
112 type_t(int v, const std::string& s);
116 * The admin state of the interface
118 struct admin_state_t : enum_base<admin_state_t>
123 const static admin_state_t DOWN;
127 const static admin_state_t UP;
130 * Convert VPP's numerical value to enum type
132 static admin_state_t from_int(uint8_t val);
136 * Private constructor taking the value and the string name
138 admin_state_t(int v, const std::string& s);
142 * The oper state of the interface
144 struct oper_state_t : enum_base<oper_state_t>
147 * Operational DOWN state
149 const static oper_state_t DOWN;
151 * Operational UP state
153 const static oper_state_t UP;
156 * Convert VPP's numerical value to enum type
158 static oper_state_t from_int(uint8_t val);
162 * Private constructor taking the value and the string name
164 oper_state_t(int v, const std::string& s);
168 * Construct a new object matching the desried state
170 interface(const std::string& name,
173 const std::string& tag = "");
175 * Construct a new object matching the desried state mapped
176 * to a specific route_domain
178 interface(const std::string& name,
181 const route_domain& rd,
182 const std::string& tag = "");
186 virtual ~interface();
191 interface(const interface& o);
193 static const_iterator_t cbegin();
194 static const_iterator_t cend();
197 * Return the matching'singular' of the interface
199 std::shared_ptr<interface> singular() const;
202 * convert to string format for debug purposes
204 virtual std::string to_string(void) const;
207 * Return VPP's handle to this object
209 const handle_t& handle() const;
212 * Return the interface type
214 const type_t& type() const;
217 * Return the interface type
219 const std::string& name() const;
222 * Return the interface type
224 const key_t& key() const;
227 * Return the L2 Address
229 const l2_address_t& l2_address() const;
232 * Set the admin state of the interface
234 void set(const admin_state_t& state);
239 void set(const l2_address_t& addr);
242 * Set the operational state of the interface, as reported by VPP
244 void set(const oper_state_t& state);
247 * Set the tag to the interface
249 void set(const std::string& tag);
252 * Comparison operator - only used for UT
254 virtual bool operator==(const interface& i) const;
257 * A base class for interface Create commands
259 template <typename MSG>
260 class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
263 create_cmd(HW::item<handle_t>& item, const std::string& name)
264 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
272 virtual ~create_cmd() = default;
275 * Comparison operator - only used for UT
277 virtual bool operator==(const create_cmd& o) const
279 return (m_name == o.m_name);
283 * Indicate the succeeded, when the HW Q is disabled.
287 rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
288 interface::add(m_name, this->item());
292 * add the created interface to the DB
294 void insert_interface() { interface::add(m_name, this->item()); }
296 virtual vapi_error_e operator()(MSG& reply)
298 int sw_if_index = reply.get_response().get_payload().sw_if_index;
299 int retval = reply.get_response().get_payload().retval;
301 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
303 rc_t rc = rc_t::from_vpp_retval(retval);
304 handle_t handle = handle_t::INVALID;
306 if (rc_t::OK == rc) {
307 handle = sw_if_index;
310 HW::item<handle_t> res(handle, rc);
319 * The name of the interface to be created
321 const std::string& m_name;
325 * Base class for intterface Delete commands
327 template <typename MSG>
328 class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
331 delete_cmd(HW::item<handle_t>& item, const std::string& name)
332 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
337 delete_cmd(HW::item<handle_t>& item)
338 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
346 virtual ~delete_cmd() = default;
349 * Comparison operator - only used for UT
351 virtual bool operator==(const delete_cmd& o) const
353 return (this->m_hw_item == o.m_hw_item);
357 * Indicate the succeeded, when the HW Q is disabled.
362 * remove the deleted interface from the DB
364 void remove_interface() { interface::remove(this->item()); }
368 * The name of the interface to be created
370 const std::string m_name;
374 * A class that listens to interface Events
380 * Default Constructor
385 * Virtual function called on the listener when the command has data
388 virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
391 * Return the HW::item representing the status
393 HW::item<bool>& status();
397 * The status of the subscription
399 HW::item<bool> m_status;
403 * A class that listens to interface Stats
409 * Default Constructor
414 * Virtual function called on the listener when the command has data
417 virtual void handle_interface_stat(
418 interface_cmds::stats_enable_cmd* cmd) = 0;
421 * Return the HW::item representing the status
423 HW::item<bool>& status();
427 * The status of the subscription
429 HW::item<bool> m_status;
433 * The the singular instance of the interface in the DB by handle
435 static std::shared_ptr<interface> find(const handle_t& h);
438 * The the singular instance of the interface in the DB by key
440 static std::shared_ptr<interface> find(const key_t& k);
443 * Dump all interfaces into the stream provided
445 static void dump(std::ostream& os);
448 * Enable stats for this interface
450 void enable_stats(stat_listener& el);
454 * Set the handle of an interface object. Only called by the interface
455 * factory during the populate
457 void set(const handle_t& handle);
458 friend class interface_factory;
461 * The SW interface handle VPP has asigned to the interface
463 HW::item<handle_t> m_hdl;
466 * Return the matching 'singular' of the interface
468 virtual std::shared_ptr<interface> singular_i() const;
471 * release/remove an interface form the singular store
476 * Virtual functions to construct an interface create commands.
477 * Overridden in derived classes like the sub_interface
479 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
482 * Virtual functions to construct an interface delete commands.
483 * Overridden in derived classes like the sub_interface
485 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
488 * Sweep/reap the object if still stale
490 virtual void sweep(void);
493 * A map of all interfaces key against the interface's name
495 static singular_db<key_t, interface> m_db;
498 * Add an interface to the DB keyed on handle
500 static void add(const key_t& name, const HW::item<handle_t>& item);
503 * remove an interface from the DB keyed on handle
505 static void remove(const HW::item<handle_t>& item);
509 * Class definition for listeners to OM events
511 class event_handler : public OM::listener, public inspect::command_handler
515 virtual ~event_handler() = default;
518 * Handle a populate event
520 void handle_populate(const client_db::key_t& key);
523 * Handle a replay event
525 void handle_replay();
528 * Show the object in the Singular DB
530 void show(std::ostream& os);
533 * Get the sortable Id of the listener
535 dependency_t order() const;
538 static event_handler m_evh;
541 * enable the interface stats in the singular instance
543 void enable_stats_i(stat_listener& el);
546 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
548 void update(const interface& obj);
551 * return the interface's handle in the singular instance
553 const handle_t& handle_i() const;
556 * It's the OM class that calls singular()
561 * It's the singular_db class that calls replay()
563 friend class singular_db<key_t, interface>;
566 * The interfaces name
568 const std::string m_name;
571 * The interface type. clearly this cannot be changed
572 * once the interface has been created.
577 * shared pointer to the routeDoamin the interface is in.
578 * NULL is not mapped - i.e. in the default table
580 std::shared_ptr<route_domain> m_rd;
583 * shared pointer to the stats object for this interface.
585 std::shared_ptr<interface_cmds::stats_enable_cmd> m_stats;
588 * The state of the interface
590 HW::item<admin_state_t> m_state;
593 * HW state of the VPP table mapping
595 HW::item<route::table_id_t> m_table_id;
598 * HW state of the L2 address
600 HW::item<l2_address_t> m_l2_address;
603 * Operational state of the interface
608 * tag of the interface
613 * A map of all interfaces keyed against VPP's handle
615 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
618 * replay the object to create it in hardware
620 virtual void replay(void);
623 * Create commands are firends so they can add interfaces to the
626 template <typename MSG>
627 friend class create_cmd;
630 * Create commands are firends so they can remove interfaces from the
633 template <typename MSG>
634 friend class delete_cmd;
638 * fd.io coding-style-patch-verification: ON
641 * eval: (c-set-style "mozilla")