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