38845e36870e13ab99ff734660100cc147edde30
[vpp.git] / src / vpp-api / vom / interface_ip6_nd.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_IP6_ND_H__
17 #define __VOM_INTERFACE_IP6_ND_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/ra_config.hpp"
26 #include "vom/ra_prefix.hpp"
27 #include "vom/rpc_cmd.hpp"
28 #include "vom/singular_db.hpp"
29 #include "vom/sub_interface.hpp"
30
31 #include <vapi/ip.api.vapi.hpp>
32
33 namespace VOM {
34 /**
35  * A representation of L3 configuration on an interface
36  */
37 template <typename CLASS, typename CMD>
38 class interface_ip6_nd : public object_base
39 {
40 public:
41   typedef CLASS class_t;
42   /**
43    * Construct a new object matching the desried state
44    */
45   interface_ip6_nd(const interface& itf, const class_t cls)
46     : m_itf(itf.singular())
47     , m_cls(cls)
48     , m_config(true)
49   {
50   }
51
52   /**
53    * Copy Constructor
54    */
55   interface_ip6_nd(const interface_ip6_nd& o)
56     : m_itf(o.m_itf)
57     , m_cls(o.m_cls)
58     , m_config(o.m_config)
59   {
60   }
61
62   /**
63    * Destructor
64    */
65   ~interface_ip6_nd()
66   {
67     sweep();
68     m_db.release(m_itf->key(), this);
69   }
70
71   /**
72    * Return the 'singular instance' of the interface ip6nd that matches
73    * this object
74  */
75   std::shared_ptr<interface_ip6_nd> singular() const
76   {
77     return find_or_add(*this);
78   }
79
80   /**
81    * convert to string format for debug purposes
82    */
83   std::string to_string() const
84   {
85     std::ostringstream s;
86     s << "interface-ip6-nd:["
87       << " itf:" << m_itf->to_string() << " " << m_cls.to_string() << " "
88       << m_config.to_string() << "]";
89
90     return (s.str());
91   }
92
93   /**
94    * Dump all config into the stream provided
95    */
96   static void dump(std::ostream& os) { m_db.dump(os); }
97
98   /**
99    * The key type for interface ip6 nd
100    */
101   typedef interface::key_type key_t;
102
103   /**
104    * Find an singular instance in the DB for the interface passed
105    */
106   static std::shared_ptr<interface_ip6_nd> find(const interface& i)
107   {
108     /*
109      * Loop throught the entire map looking for matching interface.
110      * not the most efficient algorithm, but it will do for now. The
111      * number of ra configs is low.
112      */
113     std::deque<std::shared_ptr<interface_ip6_nd>> rac;
114
115     auto it = m_db.cbegin();
116
117     while (it != m_db.cend()) {
118       /*
119        * The key in the DB is a pair of the interface's name.
120        * If the keys match, save the ra-config
121        */
122       auto key = it->first;
123
124       if (i.key() == key.first) {
125         rac.push_back(it->second.lock());
126       }
127
128       ++it;
129     }
130
131     return (rac);
132   }
133
134   /**
135    * A functor class that binds the ra config to the interface
136    */
137   class config_cmd : public rpc_cmd<HW::item<bool>, rc_t, CMD>
138   {
139   public:
140     /**
141      * Constructor
142      */
143     config_cmd(HW::item<bool>& item, const handle_t& itf, const class_t& cls)
144       : rpc_cmd<HW::item<bool>, rc_t, CMD>(item)
145       , m_itf(itf)
146       , m_cls(cls)
147     {
148     }
149
150     /**
151      * Issue the command to VPP/HW
152      */
153     rc_t issue(connection& con);
154
155     /**
156      * convert to string format for debug purposes
157      */
158     std::string to_string() const
159     {
160       std::ostringstream s;
161       s << "interface-ip6-nd: " << this->item().to_string()
162         << " itf:" << m_itf.to_string() << " " << m_cls.to_string();
163
164       return (s.str());
165     }
166
167     /**
168      * Comparison operator - only used for UT
169      */
170     bool operator==(const config_cmd& other) const
171     {
172       return ((m_itf == other.m_itf) && (m_cls == other.m_cls));
173     }
174
175   private:
176     /**
177      * Reference to the interface to bind to
178      */
179     const handle_t& m_itf;
180
181     /**
182      * Reference to the config class
183      */
184     const class_t& m_cls;
185   };
186
187   /**
188    * A cmd class that Unbinds L3 Config from an interface
189    */
190   class unconfig_cmd : public rpc_cmd<HW::item<bool>, rc_t, CMD>
191   {
192   public:
193     /**
194      * Constructor
195      */
196     unconfig_cmd(HW::item<bool>& item, const handle_t& itf, const class_t& cls)
197       : rpc_cmd<HW::item<bool>, rc_t, CMD>(item)
198       , m_itf(itf)
199       , m_cls(cls)
200     {
201     }
202
203     /**
204      * Issue the command to VPP/HW
205      */
206     rc_t issue(connection& con);
207
208     /**
209      * convert to string format for debug purposes
210      */
211     std::string to_string() const
212     {
213       std::ostringstream s;
214       s << "interface-ip6-nd: " << this->item().to_string()
215         << " itf:" << m_itf.to_string() << " " << m_cls.to_string();
216
217       return (s.str());
218     }
219
220     /**
221      * Comparison operator - only used for UT
222      */
223     bool operator==(const unconfig_cmd& other) const
224     {
225       return ((m_itf == other.m_itf) && (m_cls == other.m_cls));
226     }
227
228   private:
229     /**
230      * Reference to the interface to unbind fomr
231      */
232     const handle_t& m_itf;
233
234     /**
235      * Reference to the config class to undo configurations
236      */
237     const class_t& m_cls;
238   };
239
240 private:
241   /**
242    * Class definition for listeners to OM events
243    */
244   class event_handler : public OM::listener, public inspect::command_handler
245   {
246   public:
247     event_handler()
248     {
249       OM::register_listener(this);
250       inspect::register_handler({ "ip6_nd " }, "interface ip6 nd", this);
251     }
252
253     virtual ~event_handler() = default;
254
255     /**
256      * Handle a populate event
257      */
258     void handle_populate(const client_db::key_t& key)
259     {
260       /**
261        * VPP provides no dump for ra config
262        */
263     }
264
265     /**
266      * Handle a replay event
267      */
268     void handle_replay() { m_db.replay(); }
269
270     /**
271      * Show the object in the Singular DB
272      */
273     void show(std::ostream& os) { m_db.dump(os); }
274
275     /**
276      * Get the sortable Id of the listener
277      */
278     dependency_t order() const { return (dependency_t::BINDING); }
279   };
280
281   /**
282    * event_handler to register with OM
283    */
284   static event_handler m_evh;
285
286   /**
287    * Enqueue commands to the VPP for the update
288    */
289   void update(const interface_ip6_nd& obj)
290   {
291     if (!m_config) {
292       HW::enqueue(new config_cmd(m_config, m_itf->handle(), m_cls));
293     }
294   }
295
296   void sweep()
297   {
298     if (m_config) {
299       HW::enqueue(new unconfig_cmd(m_config, m_itf->handle(), m_cls));
300     }
301     HW::write();
302   }
303
304   /**
305    * Replay the objects state to HW
306    */
307   void replay(void)
308   {
309     if (m_config) {
310       HW::enqueue(new config_cmd(m_config, m_itf->handle(), m_cls));
311     }
312   }
313
314   /**
315    * Find or add the singular instance in the DB
316    */
317   static std::shared_ptr<interface_ip6_nd> find_or_add(
318     const interface_ip6_nd& temp)
319   {
320     return (m_db.find_or_add(temp.m_itf->key(), temp));
321   }
322
323   /*
324    * It's the VPPHW class that updates the objects in HW
325    */
326   friend class OM;
327
328   /**
329    * It's the singular_db class that calls replay()
330    */
331   friend class singular_db<key_t, interface_ip6_nd>;
332
333   const std::shared_ptr<interface> m_itf;
334
335   const class_t m_cls;
336
337   const key_t m_key;
338
339   /**
340    * HW configuration for the binding. The bool representing the
341    * do/don't bind.
342  */
343   HW::item<bool> m_config;
344
345   /**
346    * A map of all interface ip6 nd keyed against a combination of the
347    * interface and subnet's keys.
348    */
349   static singular_db<key_t, interface_ip6_nd> m_db;
350 };
351
352 /**
353  * Typedef the ip6nd_ra_config
354  */
355 typedef interface_ip6_nd<ra_config, vapi::Sw_interface_ip6nd_ra_config>
356   ip6nd_ra_config;
357
358 /**
359  * Typedef the ip6nd_ra_prefix
360  */
361 typedef interface_ip6_nd<ra_prefix, vapi::Sw_interface_ip6nd_ra_prefix>
362   ip6nd_ra_prefix;
363
364 /**
365  * Definition of the static singular_db for ACL Lists
366  */
367 template <typename CLASS, typename CMD>
368 singular_db<typename interface_ip6_nd<CLASS, CMD>::key_t,
369             interface_ip6_nd<CLASS, CMD>>
370   interface_ip6_nd<CLASS, CMD>::m_db;
371
372 template <typename CLASS, typename CMD>
373 typename interface_ip6_nd<CLASS, CMD>::event_handler
374   interface_ip6_nd<CLASS, CMD>::m_evh;
375 };
376
377 /*
378  * fd.io coding-style-patch-verification: ON
379  *
380  * Local Variables:
381  * eval: (c-set-style "mozilla")
382  * End:
383  */
384
385 #endif