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;
99 * Convert VPP's name of the interface to a type
101 static type_t from_string(const std::string& str);
105 * Private constructor taking the value and the string name
107 type_t(int v, const std::string& s);
111 * The admin state of the interface
113 struct admin_state_t : enum_base<admin_state_t>
118 const static admin_state_t DOWN;
122 const static admin_state_t UP;
125 * Convert VPP's numerical value to enum type
127 static admin_state_t from_int(uint8_t val);
131 * Private constructor taking the value and the string name
133 admin_state_t(int v, const std::string& s);
137 * The oper state of the interface
139 struct oper_state_t : enum_base<oper_state_t>
142 * Operational DOWN state
144 const static oper_state_t DOWN;
146 * Operational UP state
148 const static oper_state_t UP;
151 * Convert VPP's numerical value to enum type
153 static oper_state_t from_int(uint8_t val);
157 * Private constructor taking the value and the string name
159 oper_state_t(int v, const std::string& s);
163 * Construct a new object matching the desried state
165 interface(const std::string& name,
168 const std::string& tag = "");
170 * Construct a new object matching the desried state mapped
171 * to a specific route_domain
173 interface(const std::string& name,
176 const route_domain& rd,
177 const std::string& tag = "");
181 virtual ~interface();
186 interface(const interface& o);
188 static const_iterator_t cbegin();
189 static const_iterator_t cend();
192 * Return the matching'singular' of the interface
194 std::shared_ptr<interface> singular() const;
197 * convert to string format for debug purposes
199 virtual std::string to_string(void) const;
202 * Return VPP's handle to this object
204 const handle_t& handle() const;
207 * Return the interface type
209 const type_t& type() const;
212 * Return the interface type
214 const std::string& name() const;
217 * Return the interface type
219 const key_t& key() const;
222 * Return the L2 Address
224 const l2_address_t& l2_address() const;
227 * Set the admin state of the interface
229 void set(const admin_state_t& state);
234 void set(const l2_address_t& addr);
237 * Set the operational state of the interface, as reported by VPP
239 void set(const oper_state_t& state);
242 * Set the tag to the interface
244 void set(const std::string& tag);
247 * Comparison operator - only used for UT
249 virtual bool operator==(const interface& i) const;
252 * A base class for interface Create commands
254 template <typename MSG>
255 class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
258 create_cmd(HW::item<handle_t>& item, const std::string& name)
259 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
267 virtual ~create_cmd() = default;
270 * Comparison operator - only used for UT
272 virtual bool operator==(const create_cmd& o) const
274 return (m_name == o.m_name);
278 * Indicate the succeeded, when the HW Q is disabled.
282 rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
283 interface::add(m_name, this->item());
287 * add the created interface to the DB
289 void insert_interface() { interface::add(m_name, this->item()); }
291 virtual vapi_error_e operator()(MSG& reply)
293 int sw_if_index = reply.get_response().get_payload().sw_if_index;
294 int retval = reply.get_response().get_payload().retval;
296 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
298 rc_t rc = rc_t::from_vpp_retval(retval);
299 handle_t handle = handle_t::INVALID;
301 if (rc_t::OK == rc) {
302 handle = sw_if_index;
305 HW::item<handle_t> res(handle, rc);
314 * The name of the interface to be created
316 const std::string& m_name;
320 * Base class for intterface Delete commands
322 template <typename MSG>
323 class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
326 delete_cmd(HW::item<handle_t>& item, const std::string& name)
327 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
332 delete_cmd(HW::item<handle_t>& item)
333 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
341 virtual ~delete_cmd() = default;
344 * Comparison operator - only used for UT
346 virtual bool operator==(const delete_cmd& o) const
348 return (this->m_hw_item == o.m_hw_item);
352 * Indicate the succeeded, when the HW Q is disabled.
357 * remove the deleted interface from the DB
359 void remove_interface() { interface::remove(this->item()); }
363 * The name of the interface to be created
365 const std::string m_name;
369 * A class that listens to interface Events
375 * Default Constructor
380 * Virtual function called on the listener when the command has data
383 virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
386 * Return the HW::item representing the status
388 HW::item<bool>& status();
392 * The status of the subscription
394 HW::item<bool> m_status;
398 * A class that listens to interface Stats
404 * Default Constructor
409 * Virtual function called on the listener when the command has data
412 virtual void handle_interface_stat(
413 interface_cmds::stats_enable_cmd* cmd) = 0;
416 * Return the HW::item representing the status
418 HW::item<bool>& status();
422 * The status of the subscription
424 HW::item<bool> m_status;
428 * The the singular instance of the interface in the DB by handle
430 static std::shared_ptr<interface> find(const handle_t& h);
433 * The the singular instance of the interface in the DB by key
435 static std::shared_ptr<interface> find(const key_t& k);
438 * Dump all interfaces into the stream provided
440 static void dump(std::ostream& os);
443 * Enable stats for this interface
445 void enable_stats(stat_listener& el);
449 * Set the handle of an interface object. Only called by the interface
450 * factory during the populate
452 void set(const handle_t& handle);
453 friend class interface_factory;
456 * The SW interface handle VPP has asigned to the interface
458 HW::item<handle_t> m_hdl;
461 * Return the matching 'singular' of the interface
463 virtual std::shared_ptr<interface> singular_i() const;
466 * release/remove an interface form the singular store
471 * Virtual functions to construct an interface create commands.
472 * Overridden in derived classes like the sub_interface
474 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
477 * Virtual functions to construct an interface delete commands.
478 * Overridden in derived classes like the sub_interface
480 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
483 * Sweep/reap the object if still stale
485 virtual void sweep(void);
488 * A map of all interfaces key against the interface's name
490 static singular_db<key_t, interface> m_db;
493 * Add an interface to the DB keyed on handle
495 static void add(const key_t& name, const HW::item<handle_t>& item);
498 * remove an interface from the DB keyed on handle
500 static void remove(const HW::item<handle_t>& item);
504 * Class definition for listeners to OM events
506 class event_handler : public OM::listener, public inspect::command_handler
510 virtual ~event_handler() = default;
513 * Handle a populate event
515 void handle_populate(const client_db::key_t& key);
518 * Handle a replay event
520 void handle_replay();
523 * Show the object in the Singular DB
525 void show(std::ostream& os);
528 * Get the sortable Id of the listener
530 dependency_t order() const;
533 static event_handler m_evh;
536 * enable the interface stats in the singular instance
538 void enable_stats_i(stat_listener& el);
541 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
543 void update(const interface& obj);
546 * return the interface's handle in the singular instance
548 const handle_t& handle_i() const;
551 * It's the OM class that calls singular()
556 * It's the singular_db class that calls replay()
558 friend class singular_db<key_t, interface>;
561 * The interfaces name
563 const std::string m_name;
566 * The interface type. clearly this cannot be changed
567 * once the interface has been created.
572 * shared pointer to the routeDoamin the interface is in.
573 * NULL is not mapped - i.e. in the default table
575 std::shared_ptr<route_domain> m_rd;
578 * shared pointer to the stats object for this interface.
580 std::shared_ptr<interface_cmds::stats_enable_cmd> m_stats;
583 * The state of the interface
585 HW::item<admin_state_t> m_state;
588 * HW state of the VPP table mapping
590 HW::item<route::table_id_t> m_table_id;
593 * HW state of the L2 address
595 HW::item<l2_address_t> m_l2_address;
598 * Operational state of the interface
603 * tag of the interface
608 * A map of all interfaces keyed against VPP's handle
610 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
613 * replay the object to create it in hardware
615 virtual void replay(void);
618 * Create commands are firends so they can add interfaces to the
621 template <typename MSG>
622 friend class create_cmd;
625 * Create commands are firends so they can remove interfaces from the
628 template <typename MSG>
629 friend class delete_cmd;
633 * fd.io coding-style-patch-verification: ON
636 * eval: (c-set-style "mozilla")