Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / vom / l3_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_L3_BINDING_H__
17 #define __VOM_L3_BINDING_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/ip.api.vapi.hpp>
29
30 namespace VOM {
31 /**
32  * A representation of L3 configuration on an interface
33  */
34 class l3_binding : public object_base
35 {
36 public:
37   /**
38    * Construct a new object matching the desried state
39    */
40   l3_binding(const interface& itf, const route::prefix_t& pfx);
41
42   /**
43    * Copy Constructor
44    */
45   l3_binding(const l3_binding& o);
46
47   /**
48    * Destructor
49    */
50   ~l3_binding();
51
52   /**
53    * Return the 'singular instance' of the L3-Config that matches this
54    * object
55    */
56   std::shared_ptr<l3_binding> singular() const;
57
58   /**
59    * convert to string format for debug purposes
60    */
61   std::string to_string() const;
62
63   /**
64    * Return the prefix associated with this L3config
65    */
66   const route::prefix_t& prefix() const;
67
68   /**
69    * Dump all l3_bindings into the stream provided
70    */
71   static void dump(std::ostream& os);
72
73   /**
74    * The key type for l3_bindings
75    */
76   typedef std::pair<interface::key_type, route::prefix_t> key_type_t;
77
78   /**
79    * Find an singular instance in the DB for the interface passed
80    */
81   static std::deque<std::shared_ptr<l3_binding>> find(const interface& i);
82
83   /**
84    * A functor class that binds the L3 config to the interface
85    */
86   class bind_cmd
87     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Sw_interface_add_del_address>
88   {
89   public:
90     /**
91      * Constructor
92      */
93     bind_cmd(HW::item<bool>& item,
94              const handle_t& itf,
95              const route::prefix_t& pfx);
96
97     /**
98      * Issue the command to VPP/HW
99      */
100     rc_t issue(connection& con);
101     /**
102      * convert to string format for debug purposes
103      */
104     std::string to_string() const;
105
106     /**
107      * Comparison operator - only used for UT
108      */
109     bool operator==(const bind_cmd& i) const;
110
111   private:
112     /**
113      * Reference to the interface to bind to
114      */
115     const handle_t& m_itf;
116
117     /**
118      * The prefix to bind
119      */
120     const route::prefix_t& m_pfx;
121   };
122
123   /**
124    * A cmd class that Unbinds L3 Config from an interface
125    */
126   class unbind_cmd
127     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Sw_interface_add_del_address>
128   {
129   public:
130     /**
131      * Constructor
132      */
133     unbind_cmd(HW::item<bool>& item,
134                const handle_t& itf,
135                const route::prefix_t& pfx);
136
137     /**
138      * Issue the command to VPP/HW
139      */
140     rc_t issue(connection& con);
141     /**
142      * convert to string format for debug purposes
143      */
144     std::string to_string() const;
145
146     /**
147      * Comparison operator - only used for UT
148      */
149     bool operator==(const unbind_cmd& i) const;
150
151   private:
152     /**
153      * Reference to the interface to unbind fomr
154      */
155     const handle_t& m_itf;
156
157     /**
158      * The prefix to unbind
159      */
160     const route::prefix_t& m_pfx;
161   };
162
163   /**
164    * A cmd class that Dumps all the IPv4 L3 configs
165    */
166   class dump_v4_cmd : public dump_cmd<vapi::Ip_address_dump>
167   {
168   public:
169     /**
170      * Constructor
171      */
172     dump_v4_cmd(const handle_t& itf);
173     dump_v4_cmd(const dump_v4_cmd& d);
174
175     /**
176      * Issue the command to VPP/HW
177      */
178     rc_t issue(connection& con);
179     /**
180      * convert to string format for debug purposes
181      */
182     std::string to_string() const;
183
184     /**
185      * Comparison operator - only used for UT
186      */
187     bool operator==(const dump_v4_cmd& i) const;
188
189   private:
190     /**
191      * HW reutrn code
192      */
193     HW::item<bool> item;
194
195     /**
196      * The interface to get the addresses for
197      */
198     const handle_t& m_itf;
199   };
200
201 private:
202   /**
203    * Class definition for listeners to OM events
204    */
205   class event_handler : public OM::listener, public inspect::command_handler
206   {
207   public:
208     event_handler();
209     virtual ~event_handler() = default;
210
211     /**
212      * Handle a populate event
213      */
214     void handle_populate(const client_db::key_t& key);
215
216     /**
217      * Handle a replay event
218      */
219     void handle_replay();
220
221     /**
222      * Show the object in the Singular DB
223      */
224     void show(std::ostream& os);
225
226     /**
227      * Get the sortable Id of the listener
228      */
229     dependency_t order() const;
230   };
231
232   /**
233    * event_handler to register with OM
234    */
235   static event_handler m_evh;
236
237   /**
238    * Enquue commonds to the VPP command Q for the update
239    */
240   void update(const l3_binding& obj);
241
242   /**
243    * Find or add the singular instance in the DB
244    */
245   static std::shared_ptr<l3_binding> find_or_add(const l3_binding& temp);
246
247   /*
248    * It's the VPPHW class that updates the objects in HW
249    */
250   friend class OM;
251
252   /**
253      e* It's the singular_db class that calls replay()
254   */
255   friend class singular_db<key_type_t, l3_binding>;
256
257   /**
258    * Sweep/reap the object if still stale
259    */
260   void sweep(void);
261
262   /**
263    * replay the object to create it in hardware
264    */
265   void replay(void);
266
267   /**
268    * A reference counting pointer the interface that this L3 layer
269    * represents. By holding the reference here, we can guarantee that
270    * this object will outlive the interface
271    */
272   const std::shared_ptr<interface> m_itf;
273
274   /**
275    * The prefix for this L3 configuration
276    */
277   const route::prefix_t& m_pfx;
278
279   /**
280    * HW configuration for the binding. The bool representing the
281    * do/don't bind.
282    */
283   HW::item<bool> m_binding;
284
285   /**
286    * A map of all L3 configs keyed against a combination of the interface
287    * and subnet's keys.
288    */
289   static singular_db<key_type_t, l3_binding> m_db;
290 };
291
292 /**
293  * Ostream output for the key
294  */
295 std::ostream& operator<<(std::ostream& os, const l3_binding::key_type_t& key);
296 };
297
298 /*
299  * fd.io coding-style-patch-verification: ON
300  *
301  * Local Variables:
302  * eval: (c-set-style "mozilla")
303  * End:
304  */
305
306 #endif