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