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 * Convert VPP's name of the interface to a type
96 static type_t from_string(const std::string& str);
100 * Private constructor taking the value and the string name
102 type_t(int v, const std::string& s);
106 * The admin state of the interface
108 struct admin_state_t : enum_base<admin_state_t>
113 const static admin_state_t DOWN;
117 const static admin_state_t UP;
120 * Convert VPP's numerical value to enum type
122 static admin_state_t from_int(uint8_t val);
126 * Private constructor taking the value and the string name
128 admin_state_t(int v, const std::string& s);
132 * The oper state of the interface
134 struct oper_state_t : enum_base<oper_state_t>
137 * Operational DOWN state
139 const static oper_state_t DOWN;
141 * Operational UP state
143 const static oper_state_t UP;
146 * Convert VPP's numerical value to enum type
148 static oper_state_t from_int(uint8_t val);
152 * Private constructor taking the value and the string name
154 oper_state_t(int v, const std::string& s);
158 * Construct a new object matching the desried state
160 interface(const std::string& name, type_t type, admin_state_t state);
162 * Construct a new object matching the desried state mapped
163 * to a specific route_domain
165 interface(const std::string& name,
168 const route_domain& rd);
172 virtual ~interface();
177 interface(const interface& o);
179 static const_iterator_t cbegin();
180 static const_iterator_t cend();
183 * Return the matching'singular' of the interface
185 std::shared_ptr<interface> singular() const;
188 * convert to string format for debug purposes
190 virtual std::string to_string(void) const;
193 * Return VPP's handle to this object
195 const handle_t& handle() const;
198 * Return the interface type
200 const type_t& type() const;
203 * Return the interface type
205 const std::string& name() const;
208 * Return the interface type
210 const key_t& key() const;
213 * Return the L2 Address
215 const l2_address_t& l2_address() const;
220 void set(const l2_address_t& addr);
223 * Set the operational state of the interface, as reported by VPP
225 void set(const oper_state_t& state);
228 * Comparison operator - only used for UT
230 virtual bool operator==(const interface& i) const;
233 * A base class for interface Create commands
235 template <typename MSG>
236 class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
239 create_cmd(HW::item<handle_t>& item, const std::string& name)
240 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
248 virtual ~create_cmd() = default;
251 * Comparison operator - only used for UT
253 virtual bool operator==(const create_cmd& o) const
255 return (m_name == o.m_name);
259 * Indicate the succeeded, when the HW Q is disabled.
263 rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
264 interface::add(m_name, this->item());
268 * add the created interface to the DB
270 void insert_interface() { interface::add(m_name, this->item()); }
272 virtual vapi_error_e operator()(MSG& reply)
274 int sw_if_index = reply.get_response().get_payload().sw_if_index;
275 int retval = reply.get_response().get_payload().retval;
277 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
279 rc_t rc = rc_t::from_vpp_retval(retval);
280 handle_t handle = handle_t::INVALID;
282 if (rc_t::OK == rc) {
283 handle = sw_if_index;
286 HW::item<handle_t> res(handle, rc);
295 * The name of the interface to be created
297 const std::string& m_name;
301 * Base class for intterface Delete commands
303 template <typename MSG>
304 class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
307 delete_cmd(HW::item<handle_t>& item, const std::string& name)
308 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
313 delete_cmd(HW::item<handle_t>& item)
314 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
322 virtual ~delete_cmd() = default;
325 * Comparison operator - only used for UT
327 virtual bool operator==(const delete_cmd& o) const
329 return (this->m_hw_item == o.m_hw_item);
333 * Indicate the succeeded, when the HW Q is disabled.
338 * add the created interface to the DB
340 void remove_interface() { interface::remove(this->item()); }
344 * The name of the interface to be created
346 const std::string m_name;
350 * A class that listens to interface Events
356 * Default Constructor
361 * Virtual function called on the listener when the command has data
364 virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
367 * Return the HW::item representing the status
369 HW::item<bool>& status();
373 * The status of the subscription
375 HW::item<bool> m_status;
379 * A class that listens to interface Stats
385 * Default Constructor
390 * Virtual function called on the listener when the command has data
393 virtual void handle_interface_stat(
394 interface_cmds::stats_enable_cmd* cmd) = 0;
397 * Return the HW::item representing the status
399 HW::item<bool>& status();
403 * The status of the subscription
405 HW::item<bool> m_status;
409 * The the singular instance of the interface in the DB by handle
411 static std::shared_ptr<interface> find(const handle_t& h);
414 * The the singular instance of the interface in the DB by key
416 static std::shared_ptr<interface> find(const key_t& k);
419 * Dump all interfaces into the stream provided
421 static void dump(std::ostream& os);
424 * Enable stats for this interface
426 void enable_stats(stat_listener& el);
430 * Set the handle of an interface object. Only called by the interface
431 * factory during the populate
433 void set(const handle_t& handle);
434 friend class interface_factory;
437 * The SW interface handle VPP has asigned to the interface
439 HW::item<handle_t> m_hdl;
442 * Return the matching 'singular' of the interface
444 virtual std::shared_ptr<interface> singular_i() const;
447 * release/remove an interface form the singular store
452 * Virtual functions to construct an interface create commands.
453 * Overridden in derived classes like the sub_interface
455 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
458 * Virtual functions to construct an interface delete commands.
459 * Overridden in derived classes like the sub_interface
461 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
464 * Sweep/reap the object if still stale
466 virtual void sweep(void);
469 * A map of all interfaces key against the interface's name
471 static singular_db<key_t, interface> m_db;
474 * Add an interface to the DB keyed on handle
476 static void add(const key_t& name, const HW::item<handle_t>& item);
479 * remove an interface from the DB keyed on handle
481 static void remove(const HW::item<handle_t>& item);
485 * Class definition for listeners to OM events
487 class event_handler : public OM::listener, public inspect::command_handler
491 virtual ~event_handler() = default;
494 * Handle a populate event
496 void handle_populate(const client_db::key_t& key);
499 * Handle a replay event
501 void handle_replay();
504 * Show the object in the Singular DB
506 void show(std::ostream& os);
509 * Get the sortable Id of the listener
511 dependency_t order() const;
514 static event_handler m_evh;
517 * enable the interface stats in the singular instance
519 void enable_stats_i(stat_listener& el);
522 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
524 void update(const interface& obj);
527 * return the interface's handle in the singular instance
529 const handle_t& handle_i() const;
532 * It's the OM class that calls singular()
537 * It's the singular_db class that calls replay()
539 friend class singular_db<key_t, interface>;
542 * The interfaces name
544 const std::string m_name;
547 * The interface type. clearly this cannot be changed
548 * once the interface has been created.
553 * shared pointer to the routeDoamin the interface is in.
554 * NULL is not mapped - i.e. in the default table
556 std::shared_ptr<route_domain> m_rd;
559 * shared pointer to the stats object for this interface.
561 std::shared_ptr<interface_cmds::stats_enable_cmd> m_stats;
564 * The state of the interface
566 HW::item<admin_state_t> m_state;
569 * HW state of the VPP table mapping
571 HW::item<route::table_id_t> m_table_id;
574 * HW state of the L2 address
576 HW::item<l2_address_t> m_l2_address;
579 * Operational state of the interface
584 * A map of all interfaces keyed against VPP's handle
586 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
589 * replay the object to create it in hardware
591 virtual void replay(void);
594 * Create commands are firends so they can add interfaces to the
597 template <typename MSG>
598 friend class create_cmd;
601 * Create commands are firends so they can remove interfaces from the
604 template <typename MSG>
605 friend class delete_cmd;
609 * fd.io coding-style-patch-verification: ON
612 * eval: (c-set-style "mozilla")