FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
interface.hpp
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
16 #ifndef __VOM_INTERFACE_H__
17 #define __VOM_INTERFACE_H__
18 
19 #include "vom/enum_base.hpp"
20 #include "vom/hw.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/object_base.hpp"
23 #include "vom/om.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 
29 namespace VOM {
30 /**
31  * Forward declaration of the stats and events command
32  */
33 namespace interface_cmds {
34 class stats_enable_cmd;
35 class events_cmd;
36 };
37 
38 /**
39  * A representation of an interface in VPP
40  */
41 class interface : public object_base
42 {
43 public:
44  /**
45  * The key for interface's key
46  */
47  typedef std::string key_t;
48 
49  /**
50  * The iterator type
51  */
54 
55  /**
56  * An interface type
57  */
58  struct type_t : enum_base<type_t>
59  {
60  /**
61  * Unkown type
62  */
63  const static type_t UNKNOWN;
64  /**
65  * A brideged Virtual interface (aka SVI or IRB)
66  */
67  const static type_t BVI;
68  /**
69  * VXLAN interface
70  */
71  const static type_t VXLAN;
72  /**
73  * Ethernet interface type
74  */
75  const static type_t ETHERNET;
76  /**
77  * AF-Packet interface type
78  */
79  const static type_t AFPACKET;
80  /**
81  * loopback interface type
82  */
83  const static type_t LOOPBACK;
84  /**
85  * Local interface type (specific to VPP)
86  */
87  const static type_t LOCAL;
88  /**
89  * TAP interface type
90  */
91  const static type_t TAP;
92 
93  /**
94  * Convert VPP's name of the interface to a type
95  */
96  static type_t from_string(const std::string& str);
97 
98  private:
99  /**
100  * Private constructor taking the value and the string name
101  */
102  type_t(int v, const std::string& s);
103  };
104 
105  /**
106  * The admin state of the interface
107  */
108  struct admin_state_t : enum_base<admin_state_t>
109  {
110  /**
111  * Admin DOWN state
112  */
113  const static admin_state_t DOWN;
114  /**
115  * Admin UP state
116  */
117  const static admin_state_t UP;
118 
119  /**
120  * Convert VPP's numerical value to enum type
121  */
122  static admin_state_t from_int(uint8_t val);
123 
124  private:
125  /**
126  * Private constructor taking the value and the string name
127  */
128  admin_state_t(int v, const std::string& s);
129  };
130 
131  /**
132  * The oper state of the interface
133  */
134  struct oper_state_t : enum_base<oper_state_t>
135  {
136  /**
137  * Operational DOWN state
138  */
139  const static oper_state_t DOWN;
140  /**
141  * Operational UP state
142  */
143  const static oper_state_t UP;
144 
145  /**
146  * Convert VPP's numerical value to enum type
147  */
148  static oper_state_t from_int(uint8_t val);
149 
150  private:
151  /**
152  * Private constructor taking the value and the string name
153  */
154  oper_state_t(int v, const std::string& s);
155  };
156 
157  /**
158  * Construct a new object matching the desried state
159  */
160  interface(const std::string& name, type_t type, admin_state_t state);
161  /**
162  * Construct a new object matching the desried state mapped
163  * to a specific route_domain
164  */
165  interface(const std::string& name,
166  type_t type,
168  const route_domain& rd);
169  /**
170  * Destructor
171  */
172  virtual ~interface();
173 
174  /**
175  * Copy Constructor
176  */
177  interface(const interface& o);
178 
179  static const_iterator_t cbegin();
180  static const_iterator_t cend();
181 
182  /**
183  * Return the matching'singular' of the interface
184  */
185  std::shared_ptr<interface> singular() const;
186 
187  /**
188  * convert to string format for debug purposes
189  */
190  virtual std::string to_string(void) const;
191 
192  /**
193  * Return VPP's handle to this object
194  */
195  const handle_t& handle() const;
196 
197  /**
198  * Return the interface type
199  */
200  const type_t& type() const;
201 
202  /**
203  * Return the interface type
204  */
205  const std::string& name() const;
206 
207  /**
208  * Return the interface type
209  */
210  const key_t& key() const;
211 
212  /**
213  * Return the L2 Address
214  */
215  const l2_address_t& l2_address() const;
216 
217  /**
218  * Set the L2 Address
219  */
220  void set(const l2_address_t& addr);
221 
222  /**
223  * Set the operational state of the interface, as reported by VPP
224  */
225  void set(const oper_state_t& state);
226 
227  /**
228  * Comparison operator - only used for UT
229  */
230  virtual bool operator==(const interface& i) const;
231 
232  /**
233  * A base class for interface Create commands
234  */
235  template <typename MSG>
236  class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
237  {
238  public:
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)
241  , m_name(name)
242  {
243  }
244 
245  /**
246  * Destructor
247  */
248  virtual ~create_cmd() = default;
249 
250  /**
251  * Comparison operator - only used for UT
252  */
253  virtual bool operator==(const create_cmd& o) const
254  {
255  return (m_name == o.m_name);
256  }
257 
258  /**
259  * Indicate the succeeded, when the HW Q is disabled.
260  */
261  void succeeded()
262  {
264  interface::add(m_name, this->item());
265  }
266 
267  /**
268  * add the created interface to the DB
269  */
270  void insert_interface() { interface::add(m_name, this->item()); }
271 
272  virtual vapi_error_e operator()(MSG& reply)
273  {
274  int sw_if_index = reply.get_response().get_payload().sw_if_index;
275  int retval = reply.get_response().get_payload().retval;
276 
277  VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
278 
279  rc_t rc = rc_t::from_vpp_retval(retval);
280  handle_t handle = handle_t::INVALID;
281 
282  if (rc_t::OK == rc) {
283  handle = sw_if_index;
284  }
285 
286  HW::item<handle_t> res(handle, rc);
287 
288  this->fulfill(res);
289 
290  return (VAPI_OK);
291  }
292 
293  protected:
294  /**
295  * The name of the interface to be created
296  */
297  const std::string& m_name;
298  };
299 
300  /**
301  * Base class for intterface Delete commands
302  */
303  template <typename MSG>
304  class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
305  {
306  public:
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)
309  , m_name(name)
310  {
311  }
312 
314  : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
315  , m_name()
316  {
317  }
318 
319  /**
320  * Destructor
321  */
322  virtual ~delete_cmd() = default;
323 
324  /**
325  * Comparison operator - only used for UT
326  */
327  virtual bool operator==(const delete_cmd& o) const
328  {
329  return (this->m_hw_item == o.m_hw_item);
330  }
331 
332  /**
333  * Indicate the succeeded, when the HW Q is disabled.
334  */
335  void succeeded() {}
336 
337  /**
338  * add the created interface to the DB
339  */
340  void remove_interface() { interface::remove(this->item()); }
341 
342  protected:
343  /**
344  * The name of the interface to be created
345  */
346  const std::string m_name;
347  };
348 
349  /**
350  * A class that listens to interface Events
351  */
353  {
354  public:
355  /**
356  * Default Constructor
357  */
358  event_listener();
359 
360  /**
361  * Virtual function called on the listener when the command has data
362  * ready to process
363  */
364  virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
365 
366  /**
367  * Return the HW::item representing the status
368  */
369  HW::item<bool>& status();
370 
371  protected:
372  /**
373  * The status of the subscription
374  */
376  };
377 
378  /**
379  * A class that listens to interface Stats
380  */
382  {
383  public:
384  /**
385  * Default Constructor
386  */
387  stat_listener();
388 
389  /**
390  * Virtual function called on the listener when the command has data
391  * ready to process
392  */
393  virtual void handle_interface_stat(
395 
396  /**
397  * Return the HW::item representing the status
398  */
399  HW::item<bool>& status();
400 
401  protected:
402  /**
403  * The status of the subscription
404  */
406  };
407 
408  /**
409  * The the singular instance of the interface in the DB by handle
410  */
411  static std::shared_ptr<interface> find(const handle_t& h);
412 
413  /**
414  * The the singular instance of the interface in the DB by key
415  */
416  static std::shared_ptr<interface> find(const key_t& k);
417 
418  /**
419  * Dump all interfaces into the stream provided
420  */
421  static void dump(std::ostream& os);
422 
423  /**
424  * Enable stats for this interface
425  */
426  void enable_stats(stat_listener& el);
427 
428 protected:
429  /**
430  * Set the handle of an interface object. Only called by the interface
431  * factory during the populate
432  */
433  void set(const handle_t& handle);
434  friend class interface_factory;
435 
436  /**
437  * The SW interface handle VPP has asigned to the interface
438  */
440 
441  /**
442  * Return the matching 'singular' of the interface
443  */
444  virtual std::shared_ptr<interface> singular_i() const;
445 
446  /**
447  * release/remove an interface form the singular store
448  */
449  void release();
450 
451  /**
452  * Virtual functions to construct an interface create commands.
453  * Overridden in derived classes like the sub_interface
454  */
455  virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
456 
457  /**
458  * Virtual functions to construct an interface delete commands.
459  * Overridden in derived classes like the sub_interface
460  */
461  virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
462 
463  /**
464  * Sweep/reap the object if still stale
465  */
466  virtual void sweep(void);
467 
468  /**
469  * A map of all interfaces key against the interface's name
470  */
472 
473  /**
474  * Add an interface to the DB keyed on handle
475  */
476  static void add(const key_t& name, const HW::item<handle_t>& item);
477 
478  /**
479  * remove an interface from the DB keyed on handle
480  */
481  static void remove(const HW::item<handle_t>& item);
482 
483 private:
484  /**
485  * Class definition for listeners to OM events
486  */
487  class event_handler : public OM::listener, public inspect::command_handler
488  {
489  public:
490  event_handler();
491  virtual ~event_handler() = default;
492 
493  /**
494  * Handle a populate event
495  */
496  void handle_populate(const client_db::key_t& key);
497 
498  /**
499  * Handle a replay event
500  */
501  void handle_replay();
502 
503  /**
504  * Show the object in the Singular DB
505  */
506  void show(std::ostream& os);
507 
508  /**
509  * Get the sortable Id of the listener
510  */
511  dependency_t order() const;
512  };
513 
514  static event_handler m_evh;
515 
516  /**
517  * enable the interface stats in the singular instance
518  */
519  void enable_stats_i(stat_listener& el);
520 
521  /**
522  * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
523  */
524  void update(const interface& obj);
525 
526  /*
527  * return the interface's handle in the singular instance
528  */
529  const handle_t& handle_i() const;
530 
531  /*
532  * It's the OM class that calls singular()
533  */
534  friend class OM;
535 
536  /**
537  * It's the singular_db class that calls replay()
538  */
539  friend class singular_db<key_t, interface>;
540 
541  /**
542  * The interfaces name
543  */
544  const std::string m_name;
545 
546  /**
547  * The interface type. clearly this cannot be changed
548  * once the interface has been created.
549  */
550  const type_t m_type;
551 
552  /**
553  * shared pointer to the routeDoamin the interface is in.
554  * NULL is not mapped - i.e. in the default table
555  */
556  std::shared_ptr<route_domain> m_rd;
557 
558  /**
559  * shared pointer to the stats object for this interface.
560  */
561  std::shared_ptr<interface_cmds::stats_enable_cmd> m_stats;
562 
563  /**
564  * The state of the interface
565  */
566  HW::item<admin_state_t> m_state;
567 
568  /**
569  * HW state of the VPP table mapping
570  */
571  HW::item<route::table_id_t> m_table_id;
572 
573  /**
574  * HW state of the L2 address
575  */
576  HW::item<l2_address_t> m_l2_address;
577 
578  /**
579  * Operational state of the interface
580  */
581  oper_state_t m_oper;
582 
583  /**
584  * A map of all interfaces keyed against VPP's handle
585  */
586  static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
587 
588  /**
589  * replay the object to create it in hardware
590  */
591  virtual void replay(void);
592 
593  /**
594  * Create commands are firends so they can add interfaces to the
595  * handle store.
596  */
597  template <typename MSG>
598  friend class create_cmd;
599 
600  /**
601  * Create commands are firends so they can remove interfaces from the
602  * handle store.
603  */
604  template <typename MSG>
605  friend class delete_cmd;
606 };
607 };
608 /*
609  * fd.io coding-style-patch-verification: ON
610  *
611  * Local Variables:
612  * eval: (c-set-style "mozilla")
613  * End:
614  */
615 #endif
delete_cmd(HW::item< handle_t > &item, const std::string &name)
Definition: interface.hpp:307
HWITEM & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:131
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
static const rc_t & from_vpp_retval(int32_t rv)
Get the rc_t from the VPP API value.
Definition: types.cpp:36
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void succeeded()
Indicate the succeeded, when the HW Q is disabled.
Definition: interface.hpp:335
static const type_t AFPACKET
AF-Packet interface type.
Definition: interface.hpp:79
A template base class for all enum types.
Definition: enum_base.hpp:30
void remove_interface()
add the created interface to the DB
Definition: interface.hpp:340
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
HW::item< handle_t > m_hdl
The SW interface handle VPP has asigned to the interface.
Definition: interface.hpp:439
Definition: hw.hpp:33
static const oper_state_t UP
Operational UP state.
Definition: interface.hpp:143
virtual bool operator==(const delete_cmd &o) const
Comparison operator - only used for UT.
Definition: interface.hpp:327
void succeeded()
Indicate the succeeded, when the HW Q is disabled.
Definition: interface.hpp:261
virtual vapi_error_e operator()(MSG &reply)
call operator used as a callback by VAPI when the reply is available
Definition: interface.hpp:272
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static const log_level_t DEBUG
Definition: logger.hpp:32
create_cmd(HW::item< handle_t > &item, const std::string &name)
Definition: interface.hpp:239
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:199
The oper state of the interface.
Definition: interface.hpp:134
Type def of a L2 address as read from VPP.
Definition: types.hpp:265
A route-domain is a VRF.
static singular_db< key_t, interface > m_db
A map of all interfaces key against the interface&#39;s name.
Definition: interface.hpp:471
static const type_t LOCAL
Local interface type (specific to VPP)
Definition: interface.hpp:87
const std::string & m_name
The name of the interface to be created.
Definition: interface.hpp:297
static const type_t ETHERNET
Ethernet interface type.
Definition: interface.hpp:75
HW::item< bool > m_status
The status of the subscription.
Definition: interface.hpp:375
static void remove(const HW::item< handle_t > &item)
remove an interface from the DB keyed on handle
Definition: interface.cpp:418
static const oper_state_t DOWN
Operational DOWN state.
Definition: interface.hpp:139
add
Definition: vector_sse2.h:279
A class that listens to interface Stats.
Definition: interface.hpp:381
#define v
Definition: acl.c:341
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:32
A class that listens to interface Events.
Definition: interface.hpp:352
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:38
static void add(const key_t &name, const HW::item< handle_t > &item)
Add an interface to the DB keyed on handle.
Definition: interface.cpp:408
virtual bool operator==(const create_cmd &o) const
Comparison operator - only used for UT.
Definition: interface.hpp:253
A command class represents our desire to recieve interface events.
vec_header_t h
Definition: buffer.c:282
The admin state of the interface.
Definition: interface.hpp:108
A representation of an interface in VPP.
Definition: interface.hpp:41
Class definition for listeners to OM events.
Definition: om.hpp:284
inspect command handler Handler
Definition: inspect.hpp:54
std::map< KEY, std::weak_ptr< OBJ > >::const_iterator const_iterator
Iterator.
Definition: singular_db.hpp:44
Base class for intterface Delete commands.
Definition: interface.hpp:304
A type declaration of an interface handle in VPP.
Definition: types.hpp:164
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:106
success
Definition: vapi_common.h:27
The interface to writing objects into VPP OM.
Definition: om.hpp:140
vhost_vring_state_t state
Definition: vhost-user.h:82
A base class for all object_base in the VPP object_base-Model.
Definition: object_base.hpp:29
static const type_t UNKNOWN
Unkown type.
Definition: interface.hpp:63
const std::string m_name
The name of the interface to be created.
Definition: interface.hpp:346
A command class represents our desire to recieve interface stats.
An interface type.
Definition: interface.hpp:58
static const type_t VXLAN
VXLAN interface.
Definition: interface.hpp:71
A base class for interface Create commands.
Definition: interface.hpp:236
void insert_interface()
add the created interface to the DB
Definition: interface.hpp:270
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static const admin_state_t UP
Admin UP state.
Definition: interface.hpp:117
A representation of a method call to VPP.
Definition: cmd.hpp:32
static const type_t BVI
A brideged Virtual interface (aka SVI or IRB)
Definition: interface.hpp:67
HW::item< bool > m_status
The status of the subscription.
Definition: interface.hpp:405
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
static const admin_state_t DOWN
Admin DOWN state.
Definition: interface.hpp:113
static const type_t TAP
TAP interface type.
Definition: interface.hpp:91
singular_db< const std::string, interface >::const_iterator const_iterator_t
The iterator type.
Definition: interface.hpp:53
vhost_vring_addr_t addr
Definition: vhost-user.h:83
static const type_t LOOPBACK
loopback interface type
Definition: interface.hpp:83
std::string key_t
The key for interface&#39;s key.
Definition: interface.hpp:47
delete_cmd(HW::item< handle_t > &item)
Definition: interface.hpp:313
vapi_error_e
Definition: vapi_common.h:25