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