Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / 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/dump_cmd.hpp"
20 #include "vom/hw.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/object_base.hpp"
24 #include "vom/om.hpp"
25 #include "vom/rpc_cmd.hpp"
26 #include "vom/singular_db.hpp"
27
28 #include <vapi/span.api.vapi.hpp>
29
30 namespace VOM {
31 /**
32  * A representation of interface span configuration
33  */
34 class interface_span : public object_base
35 {
36 public:
37   /**
38    * The state of the interface - rx/tx or both to be mirrored
39    */
40   struct state_t : enum_base<state_t>
41   {
42     /**
43      * DISABLED state
44      */
45     const static state_t DISABLED;
46     /**
47      * RX enable state
48      */
49     const static state_t RX_ENABLED;
50     /**
51      * TX enable state
52      */
53     const static state_t TX_ENABLED;
54     /**
55      * TX and RX enable state
56      */
57     const static state_t TX_RX_ENABLED;
58
59     /**
60      * Convert VPP's numerical value to enum type
61      */
62     static state_t from_int(uint8_t val);
63
64   private:
65     /**
66      * Private constructor taking the value and the string name
67      */
68     state_t(int v, const std::string& s);
69   };
70
71   /**
72    * Construct a new object matching the desried state
73    *
74    * @param itf_from - The interface to be mirrored
75    * @param itf_to - The interface where the traffic is mirrored
76    */
77   interface_span(const interface& itf_from,
78                  const interface& itf_to,
79                  state_t state);
80
81   /**
82    * Copy Constructor
83    */
84   interface_span(const interface_span& o);
85
86   /**
87    * Destructor
88    */
89   ~interface_span();
90
91   /**
92    * Return the 'singular instance' of the interface_span that matches
93    * this object
94    */
95   std::shared_ptr<interface_span> singular() const;
96
97   /**
98    * convert to string format for debug purposes
99    */
100   std::string to_string() const;
101
102   /**
103    * Dump all interface_spans into the stream provided
104    */
105   static void dump(std::ostream& os);
106
107   /**
108    * The key type for interface_spans
109    */
110   typedef std::pair<interface::key_type, interface::key_type> key_type_t;
111
112   /**
113    * Find a singular instance in the DB for the interface passed
114    */
115   static std::shared_ptr<interface_span> find(const interface& i);
116
117   /**
118    * A command class that configures the interface span
119    */
120   class config_cmd : public rpc_cmd<HW::item<bool>,
121                                     rc_t,
122                                     vapi::Sw_interface_span_enable_disable>
123   {
124   public:
125     /**
126      * Constructor
127      */
128     config_cmd(HW::item<bool>& item,
129                const handle_t& itf_from,
130                const handle_t& itf_to,
131                const state_t& state);
132
133     /**
134      * Issue the command to VPP/HW
135      */
136     rc_t issue(connection& con);
137     /**
138      * convert to string format for debug purposes
139      */
140     std::string to_string() const;
141
142     /**
143      * Comparison operator - only used for UT
144      */
145     bool operator==(const config_cmd& i) const;
146
147   private:
148     /**
149      * Reference to the interface to be mirrored
150      */
151     const handle_t& m_itf_from;
152     /**
153      * Reference to the interface where the traffic is mirrored
154      */
155     const handle_t& m_itf_to;
156     /**
157      * the state (rx, tx or both) of the interface to be mirrored
158      */
159     const state_t& m_state;
160   };
161
162   /**
163    * A cmd class that Unconfigs interface span
164    */
165   class unconfig_cmd : public rpc_cmd<HW::item<bool>,
166                                       rc_t,
167                                       vapi::Sw_interface_span_enable_disable>
168   {
169   public:
170     /**
171      * Constructor
172      */
173     unconfig_cmd(HW::item<bool>& item,
174                  const handle_t& itf_from,
175                  const handle_t& itf_to);
176
177     /**
178      * Issue the command to VPP/HW
179      */
180     rc_t issue(connection& con);
181     /**
182      * convert to string format for debug purposes
183      */
184     std::string to_string() const;
185
186     /**
187      * Comparison operator - only used for UT
188      */
189     bool operator==(const unconfig_cmd& i) const;
190
191   private:
192     /**
193      * Reference to the interface to be mirrored
194      */
195     const handle_t& m_itf_from;
196     /**
197      * Reference to the interface where the traffic is mirrored
198      */
199     const handle_t& m_itf_to;
200   };
201
202   /**
203    * A cmd class that Dumps all the interface spans
204    */
205   class dump_cmd : public VOM::dump_cmd<vapi::Sw_interface_span_dump>
206   {
207   public:
208     /**
209      * Constructor
210      */
211     dump_cmd();
212     dump_cmd(const dump_cmd& d);
213
214     /**
215      * Issue the command to VPP/HW
216      */
217     rc_t issue(connection& con);
218     /**
219      * convert to string format for debug purposes
220      */
221     std::string to_string() const;
222
223     /**
224      * Comparison operator - only used for UT
225      */
226     bool operator==(const dump_cmd& i) const;
227
228   private:
229     /**
230      * HW reutrn code
231      */
232     HW::item<bool> item;
233   };
234
235 private:
236   /**
237    * Class definition for listeners to OM events
238    */
239   class event_handler : public OM::listener, public inspect::command_handler
240   {
241   public:
242     event_handler();
243     virtual ~event_handler() = default;
244
245     /**
246      * Handle a populate event
247      */
248     void handle_populate(const client_db::key_t& key);
249
250     /**
251      * Handle a replay event
252      */
253     void handle_replay();
254
255     /**
256      * Show the object in the Singular DB
257      */
258     void show(std::ostream& os);
259
260     /**
261      * Get the sortable Id of the listener
262      */
263     dependency_t order() const;
264   };
265
266   /**
267    * event_handler to register with OM
268    */
269   static event_handler m_evh;
270
271   /**
272    * Enquue commonds to the VPP command Q for the update
273    */
274   void update(const interface_span& obj);
275
276   /**
277    * Find or add the singular instance in the DB
278    */
279   static std::shared_ptr<interface_span> find_or_add(
280     const interface_span& temp);
281
282   /*
283    * It's the VPPHW class that updates the objects in HW
284    */
285   friend class OM;
286
287   /**
288      e* It's the singular_db class that calls replay()
289   */
290   friend class singular_db<key_type_t, interface_span>;
291
292   /**
293    * Sweep/reap the object if still stale
294    */
295   void sweep(void);
296
297   /**
298    * replay the object to create it in hardware
299    */
300   void replay(void);
301
302   /**
303    * A reference counting pointer the interface to be mirrored
304    */
305   const std::shared_ptr<interface> m_itf_from;
306   /**
307    * A reference counting pointer the interface where the traffic is
308    * mirrored
309  */
310   const std::shared_ptr<interface> m_itf_to;
311
312   /**
313    * the state (rx, tx or both) of the interface to be mirrored
314    */
315   const state_t m_state;
316
317   /**
318    * HW configuration for the binding. The bool representing the
319    * do/don't bind.
320  */
321   HW::item<bool> m_config;
322
323   /**
324    * A map of all interface span keyed against the interface to be
325    * mirrored.
326  */
327   static singular_db<key_type_t, interface_span> m_db;
328 };
329
330 /**
331  * Ostream output for the key
332  */
333 std::ostream& operator<<(std::ostream& os,
334                          const interface_span::key_type_t& key);
335 };
336
337 /*
338  * fd.io coding-style-patch-verification: ON
339  *
340  * Local Variables:
341  * eval: (c-set-style "mozilla")
342  * End:
343  */
344
345 #endif