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
45 * The key for interface's key
47 typedef std::string key_type;
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_type& 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 * A base class for interface Create commands
230 template <typename MSG>
231 class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
234 create_cmd(HW::item<handle_t>& item, const std::string& name)
235 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
243 virtual ~create_cmd() = default;
246 * Comparison operator - only used for UT
248 virtual bool operator==(const create_cmd& o) const
250 return (m_name == o.m_name);
254 * Indicate the succeeded, when the HW Q is disabled.
258 rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
259 interface::add(m_name, this->item());
263 * add the created interface to the DB
265 void insert_interface() { interface::add(m_name, this->item()); }
267 virtual vapi_error_e operator()(MSG& reply)
269 int sw_if_index = reply.get_response().get_payload().sw_if_index;
270 int retval = reply.get_response().get_payload().retval;
272 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
274 rc_t rc = rc_t::from_vpp_retval(retval);
275 handle_t handle = handle_t::INVALID;
277 if (rc_t::OK == rc) {
278 handle = sw_if_index;
281 HW::item<handle_t> res(handle, rc);
290 * The name of the interface to be created
292 const std::string& m_name;
296 * Base class for intterface Delete commands
298 template <typename MSG>
299 class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
302 delete_cmd(HW::item<handle_t>& item, const std::string& name)
303 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
308 delete_cmd(HW::item<handle_t>& item)
309 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
317 virtual ~delete_cmd() = default;
320 * Comparison operator - only used for UT
322 virtual bool operator==(const delete_cmd& o) const
324 return (this->m_hw_item == o.m_hw_item);
328 * Indicate the succeeded, when the HW Q is disabled.
333 * add the created interface to the DB
335 void remove_interface() { interface::remove(this->item()); }
339 * The name of the interface to be created
341 const std::string m_name;
345 * A class that listens to interface Events
351 * Default Constructor
356 * Virtual function called on the listener when the command has data
359 virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
362 * Return the HW::item representing the status
364 HW::item<bool>& status();
368 * The status of the subscription
370 HW::item<bool> m_status;
374 * A class that listens to interface Stats
380 * Default Constructor
385 * Virtual function called on the listener when the command has data
388 virtual void handle_interface_stat(interface_cmds::stats_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 * The the singular instance of the interface in the object_base-Model
405 static std::shared_ptr<interface> find(const interface& temp);
408 * The the singular instance of the interface in the object_base-Model
411 static std::shared_ptr<interface> find(const handle_t& h);
414 * The the singular instance of the interface in the object_base-Model
417 static std::shared_ptr<interface> find(const std::string& s);
420 * Dump all interfaces into the stream provided
422 static void dump(std::ostream& os);
426 * Construct an interface object with a handle and a HW address
428 interface(const handle_t& handle,
429 const l2_address_t& l2_address,
430 const std::string& name,
432 admin_state_t state);
433 friend class interface_factory;
436 * The SW interface handle VPP has asigned to the interface
438 HW::item<handle_t> m_hdl;
441 * Return the matching 'singular' of the interface
443 virtual std::shared_ptr<interface> singular_i() const;
446 * release/remove an interface form the singular store
451 * Virtual functions to construct an interface create commands.
452 * Overridden in derived classes like the sub_interface
454 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
457 * Virtual functions to construct an interface delete commands.
458 * Overridden in derived classes like the sub_interface
460 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
463 * Sweep/reap the object if still stale
465 virtual void sweep(void);
468 * A map of all interfaces key against the interface's name
470 static singular_db<const std::string, interface> m_db;
473 * Add an interface to the DB keyed on handle
475 static void add(const std::string& name, const HW::item<handle_t>& item);
478 * remove an interface from the DB keyed on handle
480 static void remove(const HW::item<handle_t>& item);
484 * Class definition for listeners to OM events
486 class event_handler : public OM::listener, public inspect::command_handler
490 virtual ~event_handler() = default;
493 * Handle a populate event
495 void handle_populate(const client_db::key_t& key);
498 * Handle a replay event
500 void handle_replay();
503 * Show the object in the Singular DB
505 void show(std::ostream& os);
508 * Get the sortable Id of the listener
510 dependency_t order() const;
513 static event_handler m_evh;
516 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
518 void update(const interface& obj);
521 * It's the OM class that calls singular()
526 * It's the singular_db class that calls replay()
528 friend class singular_db<const std::string, interface>;
531 * The interfaces name
533 const std::string m_name;
536 * The interface type. clearly this cannot be changed
537 * once the interface has been created.
542 * shared pointer to the routeDoamin the interface is in.
543 * NULL is not mapped - i.e. in eht default table
545 const std::shared_ptr<route_domain> m_rd;
548 * The state of the interface
550 HW::item<admin_state_t> m_state;
553 * HW state of the VPP table mapping
555 HW::item<route::table_id_t> m_table_id;
558 * HW state of the L2 address
560 HW::item<l2_address_t> m_l2_address;
563 * Operational state of the interface
568 * A map of all interfaces keyed against VPP's handle
570 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
573 * replay the object to create it in hardware
575 virtual void replay(void);
578 * Create commands are firends so they can add interfaces to the
581 template <typename MSG>
582 friend class create_cmd;
585 * Create commands are firends so they can remove interfaces from the
588 template <typename MSG>
589 friend class delete_cmd;
593 * fd.io coding-style-patch-verification: ON
596 * eval: (c-set-style "mozilla")