Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / vom / l2_binding.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_L2_BINDING_H__
17 #define __VOM_L2_BINDING_H__
18
19 #include "vom/bridge_domain.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 #include "vom/vxlan_tunnel.hpp"
28
29 namespace VOM {
30 /**
31  * A Clas representing the binding of an L2 interface to a bridge-domain
32  * and the properties of that binding.
33  */
34 class l2_binding : public object_base
35 {
36 public:
37   struct l2_vtr_op_t : public enum_base<l2_vtr_op_t>
38   {
39     l2_vtr_op_t(const l2_vtr_op_t& l) = default;
40     ~l2_vtr_op_t() = default;
41
42     const static l2_vtr_op_t L2_VTR_DISABLED;
43     const static l2_vtr_op_t L2_VTR_PUSH_1;
44     const static l2_vtr_op_t L2_VTR_PUSH_2;
45     const static l2_vtr_op_t L2_VTR_POP_1;
46     const static l2_vtr_op_t L2_VTR_POP_2;
47     const static l2_vtr_op_t L2_VTR_TRANSLATE_1_1;
48     const static l2_vtr_op_t L2_VTR_TRANSLATE_1_2;
49     const static l2_vtr_op_t L2_VTR_TRANSLATE_2_1;
50     const static l2_vtr_op_t L2_VTR_TRANSLATE_2_2;
51
52   private:
53     l2_vtr_op_t(int v, const std::string s);
54   };
55
56   /**
57    * Construct a new object matching the desried state
58    */
59   l2_binding(const interface& itf, const bridge_domain& bd);
60
61   /**
62    * Copy Constructor
63    */
64   l2_binding(const l2_binding& o);
65
66   /**
67    * Destructor
68    */
69   ~l2_binding();
70
71   /**
72    * Return the 'singular instance' of the L2 config that matches this
73    * object
74    */
75   std::shared_ptr<l2_binding> singular() const;
76
77   /**
78    * convert to string format for debug purposes
79    */
80   std::string to_string() const;
81
82   /**
83    * Dump all l2_bindings into the stream provided
84    */
85   static void dump(std::ostream& os);
86
87   /**
88    * Set the VTR operation on the binding/interface
89    */
90   void set(const l2_vtr_op_t& op, uint16_t tag);
91
92   /**
93    * A functor class that binds L2 configuration to an interface
94    */
95   class bind_cmd
96     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Sw_interface_set_l2_bridge>
97   {
98   public:
99     /**
100      * Constructor
101      */
102     bind_cmd(HW::item<bool>& item,
103              const handle_t& itf,
104              uint32_t bd,
105              bool is_bvi);
106
107     /**
108      * Issue the command to VPP/HW
109      */
110     rc_t issue(connection& con);
111     /**
112      * convert to string format for debug purposes
113      */
114     std::string to_string() const;
115
116     /**
117      * Comparison operator - only used for UT
118      */
119     bool operator==(const bind_cmd& i) const;
120
121   private:
122     /**
123      * The interface to bind
124      */
125     const handle_t m_itf;
126
127     /**
128      * The bridge-domain to bind to
129      */
130     uint32_t m_bd;
131
132     /**
133      * Is it a BVI interface that is being bound
134      */
135     bool m_is_bvi;
136   };
137
138   /**
139    * A cmd class that Unbinds L2 configuration from an interface
140    */
141   class unbind_cmd
142     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Sw_interface_set_l2_bridge>
143   {
144   public:
145     /**
146      * Constructor
147      */
148     unbind_cmd(HW::item<bool>& item,
149                const handle_t& itf,
150                uint32_t bd,
151                bool is_bvi);
152
153     /**
154      * Issue the command to VPP/HW
155      */
156     rc_t issue(connection& con);
157     /**
158      * convert to string format for debug purposes
159      */
160     std::string to_string() const;
161
162     /**
163      * Comparison operator - only used for UT
164      */
165     bool operator==(const unbind_cmd& i) const;
166
167   private:
168     /**
169      * The interface to bind
170      */
171     const handle_t m_itf;
172
173     /**
174      * The bridge-domain to bind to
175      */
176     uint32_t m_bd;
177
178     /**
179      * Is it a BVI interface that is being bound
180      */
181     bool m_is_bvi;
182   };
183
184   /**
185    * A cmd class sets the VTR operation
186    */
187   class set_vtr_op_cmd : public rpc_cmd<HW::item<l2_vtr_op_t>,
188                                         rc_t,
189                                         vapi::L2_interface_vlan_tag_rewrite>
190   {
191   public:
192     /**
193      * Constructor
194      */
195     set_vtr_op_cmd(HW::item<l2_vtr_op_t>& item,
196                    const handle_t& itf,
197                    uint16_t tag);
198
199     /**
200      * Issue the command to VPP/HW
201      */
202     rc_t issue(connection& con);
203
204     /**
205      * convert to string format for debug purposes
206      */
207     std::string to_string() const;
208
209     /**
210      * Comparison operator - only used for UT
211      */
212     bool operator==(const set_vtr_op_cmd& i) const;
213
214   private:
215     /**
216      * The interface to bind
217      */
218     const handle_t m_itf;
219
220     /**
221      * The tag for the operation
222      */
223     uint16_t m_tag;
224   };
225
226 private:
227   /**
228    * Class definition for listeners to OM events
229    */
230   class event_handler : public OM::listener, public inspect::command_handler
231   {
232   public:
233     event_handler();
234     virtual ~event_handler() = default;
235
236     /**
237      * Handle a populate event
238      */
239     void handle_populate(const client_db::key_t& key);
240
241     /**
242      * Handle a replay event
243      */
244     void handle_replay();
245
246     /**
247      * Show the object in the Singular DB
248      */
249     void show(std::ostream& os);
250
251     /**
252      * Get the sortable Id of the listener
253      */
254     dependency_t order() const;
255   };
256
257   /**
258    * event_handler to register with OM
259    */
260   static event_handler m_evh;
261
262   /**
263    * Enquue commonds to the VPP command Q for the update
264    */
265   void update(const l2_binding& obj);
266
267   /**
268    * Find or Add the singular instance in the DB
269    */
270   static std::shared_ptr<l2_binding> find_or_add(const l2_binding& temp);
271
272   /*
273    * It's the OM class that calls singular()
274    */
275   friend class OM;
276
277   /**
278    * It's the singular_db class that calls replay()
279    */
280   friend class singular_db<const handle_t, l2_binding>;
281
282   /**
283    * Sweep/reap the object if still stale
284    */
285   void sweep(void);
286
287   /**
288    * replay the object to create it in hardware
289    */
290   void replay(void);
291
292   /**
293    * A reference counting pointer the interface that this L2 layer
294    * represents. By holding the reference here, we can guarantee that
295    * this object will outlive the interface
296    */
297   const std::shared_ptr<interface> m_itf;
298
299   /**
300    * A reference counting pointer the Bridge-Domain that this L2
301    * interface is bound to. By holding the reference here, we can
302    * guarantee that this object will outlive the BD.
303    */
304   const std::shared_ptr<bridge_domain> m_bd;
305
306   /**
307    * HW configuration for the binding. The bool representing the
308    * do/don't bind.
309  */
310   HW::item<bool> m_binding;
311
312   /**
313    * HW configuration for the VTR option
314    */
315   HW::item<l2_vtr_op_t> m_vtr_op;
316
317   /**
318    * The Dot1q tag for the VTR operation
319    */
320   uint16_t m_vtr_op_tag;
321
322   /**
323    * A map of all L2 interfaces key against the interface's handle_t
324    */
325   static singular_db<const handle_t, l2_binding> m_db;
326 };
327 };
328
329 /*
330  * fd.io coding-style-patch-verification: ON
331  *
332  * Local Variables:
333  * eval: (c-set-style "mozilla")
334  * End:
335  */
336
337 #endif