GBP: add allowed ethertypes to contracts
[vpp.git] / extras / vom / vom / interface_span.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_SPAN_H__
17 #define __VOM_INTERFACE_SPAN_H__
18
19 #include "vom/hw.hpp"
20 #include "vom/inspect.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/object_base.hpp"
23 #include "vom/om.hpp"
24 #include "vom/singular_db.hpp"
25
26 namespace VOM {
27 /**
28  * A representation of interface span configuration
29  */
30 class interface_span : public object_base
31 {
32 public:
33   /**
34    * The state of the interface - rx/tx or both to be mirrored
35    */
36   struct state_t : enum_base<state_t>
37   {
38     /**
39      * DISABLED state
40      */
41     const static state_t DISABLED;
42     /**
43      * RX enable state
44      */
45     const static state_t RX_ENABLED;
46     /**
47      * TX enable state
48      */
49     const static state_t TX_ENABLED;
50     /**
51      * TX and RX enable state
52      */
53     const static state_t TX_RX_ENABLED;
54
55     /**
56      * Convert VPP's numerical value to enum type
57      */
58     static state_t from_int(uint8_t val);
59
60   private:
61     /**
62      * Private constructor taking the value and the string name
63      */
64     state_t(int v, const std::string& s);
65   };
66
67   /**
68    * Construct a new object matching the desried state
69    *
70    * @param itf_from - The interface to be mirrored
71    * @param itf_to - The interface where the traffic is mirrored
72    */
73   interface_span(const interface& itf_from,
74                  const interface& itf_to,
75                  state_t state);
76
77   /**
78    * Copy Constructor
79    */
80   interface_span(const interface_span& o);
81
82   /**
83    * Destructor
84    */
85   ~interface_span();
86
87   /**
88    * Return the 'singular instance' of the interface_span that matches
89    * this object
90    */
91   std::shared_ptr<interface_span> singular() const;
92
93   /**
94    * convert to string format for debug purposes
95    */
96   std::string to_string() const;
97
98   /**
99    * Dump all interface_spans into the stream provided
100    */
101   static void dump(std::ostream& os);
102
103   /**
104    * The key type for interface_spans
105    */
106   typedef std::pair<interface::key_t, interface::key_t> key_t;
107
108   /**
109    * Find a singular instance in the DB for the interface passed
110    */
111   static std::shared_ptr<interface_span> find(const interface& i);
112
113 private:
114   /**
115    * Class definition for listeners to OM events
116    */
117   class event_handler : public OM::listener, public inspect::command_handler
118   {
119   public:
120     event_handler();
121     virtual ~event_handler() = default;
122
123     /**
124      * Handle a populate event
125      */
126     void handle_populate(const client_db::key_t& key);
127
128     /**
129      * Handle a replay event
130      */
131     void handle_replay();
132
133     /**
134      * Show the object in the Singular DB
135      */
136     void show(std::ostream& os);
137
138     /**
139      * Get the sortable Id of the listener
140      */
141     dependency_t order() const;
142   };
143
144   /**
145    * event_handler to register with OM
146    */
147   static event_handler m_evh;
148
149   /**
150    * Enquue commonds to the VPP command Q for the update
151    */
152   void update(const interface_span& obj);
153
154   /**
155    * Find or add the singular instance in the DB
156    */
157   static std::shared_ptr<interface_span> find_or_add(
158     const interface_span& temp);
159
160   /*
161    * It's the VPPHW class that updates the objects in HW
162    */
163   friend class OM;
164
165   /**
166      e* It's the singular_db class that calls replay()
167   */
168   friend class singular_db<key_t, interface_span>;
169
170   /**
171    * Sweep/reap the object if still stale
172    */
173   void sweep(void);
174
175   /**
176    * replay the object to create it in hardware
177    */
178   void replay(void);
179
180   /**
181    * A reference counting pointer the interface to be mirrored
182    */
183   const std::shared_ptr<interface> m_itf_from;
184   /**
185    * A reference counting pointer the interface where the traffic is
186    * mirrored
187  */
188   const std::shared_ptr<interface> m_itf_to;
189
190   /**
191    * the state (rx, tx or both) of the interface to be mirrored
192    */
193   const state_t m_state;
194
195   /**
196    * HW configuration for the binding. The bool representing the
197    * do/don't bind.
198  */
199   HW::item<bool> m_config;
200
201   /**
202    * A map of all interface span keyed against the interface to be
203    * mirrored.
204  */
205   static singular_db<key_t, interface_span> m_db;
206 };
207
208 /**
209  * Ostream output for the key
210  */
211 std::ostream& operator<<(std::ostream& os, const interface_span::key_t& key);
212 };
213
214 /*
215  * fd.io coding-style-patch-verification: ON
216  *
217  * Local Variables:
218  * eval: (c-set-style "mozilla")
219  * End:
220  */
221
222 #endif