Fix passing bad context for callback function
[vpp.git] / src / vpp-api / vom / route.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_ROUTE_H__
17 #define __VOM_ROUTE_H__
18
19 #include "vom/interface.hpp"
20 #include "vom/prefix.hpp"
21 #include "vom/route_domain.hpp"
22 #include "vom/singular_db.hpp"
23
24 namespace VOM {
25 /**
26  * Types belonging to Routing
27  */
28 namespace route {
29 /**
30  * A path for IP or MPLS routes
31  */
32 class path
33 {
34 public:
35   /**
36    * Special path types
37    */
38   class special_t : public enum_base<special_t>
39   {
40   public:
41     /**
42      * A standard path type. this includes path types
43      * that use the next-hop and interface
44      */
45     const static special_t STANDARD;
46
47     /**
48      * A local/for-us/recieve
49      */
50     const static special_t LOCAL;
51
52     /**
53      * drop path
54      */
55     const static special_t DROP;
56
57     /**
58      * a path will return ICMP unreachables
59      */
60     const static special_t UNREACH;
61
62     /**
63      * a path will return ICMP prohibit
64      */
65     const static special_t PROHIBIT;
66
67   private:
68     /**
69      * Private constructor taking the value and the string name
70      */
71     special_t(int v, const std::string& s);
72   };
73
74   /**
75    * constructor for special paths
76    */
77   path(special_t special);
78
79   /**
80    * Constructor for standard non-recursive paths
81    */
82   path(const boost::asio::ip::address& nh,
83        const interface& interface,
84        uint8_t weight = 1,
85        uint8_t preference = 0);
86
87   /**
88    * Constructor for standard recursive paths
89    */
90   path(const route_domain& rd,
91        const boost::asio::ip::address& nh,
92        uint8_t weight = 1,
93        uint8_t preference = 0);
94
95   /**
96    * Constructor for DVR paths or attached paths.
97    */
98   path(const interface& interface,
99        const nh_proto_t& proto,
100        uint8_t weight = 1,
101        uint8_t preference = 0);
102
103   /**
104    * Copy Constructor
105    */
106   path(const path& p);
107
108   /**
109    * Destructor
110    */
111   ~path();
112
113   /**
114    * comparison operator
115    */
116   bool operator==(const path& p) const;
117
118   /**
119    * Less than operator for set insertion
120    */
121   bool operator<(const path& p) const;
122
123   /**
124    * convert to string format for debug purposes
125    */
126   std::string to_string() const;
127
128   /**
129    * Getters
130    */
131   special_t type() const;
132   nh_proto_t nh_proto() const;
133   const boost::asio::ip::address& nh() const;
134   std::shared_ptr<route_domain> rd() const;
135   std::shared_ptr<interface> itf() const;
136   uint8_t weight() const;
137   uint8_t preference() const;
138
139 private:
140   /**
141    * The special path tpye
142    */
143   special_t m_type;
144
145   /**
146    * The next-hop protocol
147    */
148   nh_proto_t m_nh_proto;
149
150   /**
151    * The next-hop
152    */
153   boost::asio::ip::address m_nh;
154
155   /**
156    * For recursive routes, this is the table in which the
157    * the next-hop exists.
158    */
159   std::shared_ptr<route_domain> m_rd;
160
161   /**
162    * The next-hop interface [if present].
163    */
164   std::shared_ptr<interface> m_interface;
165
166   /**
167    * UCMP weight
168    */
169   uint8_t m_weight;
170
171   /**
172    * Path preference
173    */
174   uint8_t m_preference;
175 };
176
177 /**
178  * A path-list is a set of paths
179  */
180 typedef std::set<path> path_list_t;
181
182 /**
183  * ostream output for iterator
184  */
185 std::ostream& operator<<(std::ostream& os, const path_list_t& path_list);
186
187 /**
188  * A IP route
189  */
190 class ip_route : public object_base
191 {
192 public:
193   /**
194    * The key for a route
195    */
196   typedef std::pair<route::table_id_t, prefix_t> key_t;
197
198   /**
199    * Construct a route in the default table
200    */
201   ip_route(const prefix_t& prefix);
202
203   /**
204    * Construct a route with a path
205    */
206   ip_route(const prefix_t& prefix, const path& p);
207
208   /**
209    * Copy Construct
210    */
211   ip_route(const ip_route& r);
212
213   /**
214    * Construct a route in the given route domain
215    */
216   ip_route(const route_domain& rd, const prefix_t& prefix);
217
218   /**
219    * Construct a route in the given route domain with a path
220    */
221   ip_route(const route_domain& rd, const prefix_t& prefix, const path& p);
222
223   /**
224    * Destructor
225    */
226   ~ip_route();
227
228   /**
229    * Get the route's key
230    */
231   const key_t key() const;
232
233   /**
234    * Comparison operator
235    */
236   bool operator==(const ip_route& i) const;
237
238   /**
239    * Return the matching 'singular instance'
240    */
241   std::shared_ptr<ip_route> singular() const;
242
243   /**
244    * Add a path.
245    */
246   void add(const path& path);
247
248   /**
249    * remove a path.
250    */
251   void remove(const path& path);
252
253   /**
254    * Find the instnace of the route domain in the OM
255    */
256   static std::shared_ptr<ip_route> find(const ip_route& temp);
257
258   /**
259    * Dump all route-doamin into the stream provided
260    */
261   static void dump(std::ostream& os);
262
263   /**
264    * replay the object to create it in hardware
265    */
266   void replay(void);
267
268   /**
269    * Convert to string for debugging
270    */
271   std::string to_string() const;
272
273   /**
274    * Return the matching 'singular instance'
275    */
276   static std::shared_ptr<ip_route> find(const key_t& k);
277
278 private:
279   /**
280    * Class definition for listeners to OM events
281    */
282   class event_handler : public OM::listener, public inspect::command_handler
283   {
284   public:
285     event_handler();
286     virtual ~event_handler() = default;
287
288     /**
289      * Handle a populate event
290      */
291     void handle_populate(const client_db::key_t& key);
292
293     /**
294      * Handle a replay event
295      */
296     void handle_replay();
297
298     /**
299      * Show the object in the Singular DB
300      */
301     void show(std::ostream& os);
302
303     /**
304      * Get the sortable Id of the listener
305      */
306     dependency_t order() const;
307   };
308
309   /**
310    * event_handler to register with OM
311    */
312   static event_handler m_evh;
313
314   /**
315    * Find or add the instnace of the route domain in the OM
316    */
317   static std::shared_ptr<ip_route> find_or_add(const ip_route& temp);
318
319   /*
320    * It's the OM class that updates the objects in HW
321    */
322   friend class VOM::OM;
323
324   /**
325    * It's the singular_db class that calls replay()
326    */
327   friend class singular_db<key_t, ip_route>;
328
329   /**
330    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
331    */
332   void update(const ip_route& obj);
333
334   /**
335    * Sweep/reap the object if still stale
336    */
337   void sweep(void);
338
339   /**
340    * HW configuration for the result of creating the route
341    */
342   HW::item<bool> m_hw;
343
344   /**
345    * The route domain the route is in.
346    */
347   std::shared_ptr<route_domain> m_rd;
348
349   /**
350    * The prefix to match
351    */
352   prefix_t m_prefix;
353
354   /**
355    * The set of paths
356    */
357   path_list_t m_paths;
358
359   /**
360    * A map of all routes
361    */
362   static singular_db<key_t, ip_route> m_db;
363 };
364
365 std::ostream& operator<<(std::ostream& os, const ip_route::key_t& key);
366 };
367 };
368
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "mozilla")
374  * End:
375  */
376
377 #endif