VOM reshuffle
[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    * Convert the path into the VPP API representation
110    */
111   void to_vpp(vapi_payload_ip_add_del_route& payload) const;
112
113   /**
114    * Less than operator for set insertion
115    */
116   bool operator<(const path& p) const;
117
118   /**
119    * convert to string format for debug purposes
120    */
121   std::string to_string() const;
122
123   /**
124    * Getters
125    */
126   special_t type() const;
127   nh_proto_t nh_proto() const;
128   const boost::asio::ip::address& nh() const;
129   std::shared_ptr<route_domain> rd() const;
130   std::shared_ptr<interface> itf() const;
131   uint8_t weight() const;
132   uint8_t preference() const;
133
134 private:
135   /**
136    * The special path tpye
137    */
138   special_t m_type;
139
140   /**
141    * The next-hop protocol
142    */
143   nh_proto_t m_nh_proto;
144
145   /**
146    * The next-hop
147    */
148   boost::asio::ip::address m_nh;
149
150   /**
151    * For recursive routes, this is the table in which the
152    * the next-hop exists.
153    */
154   std::shared_ptr<route_domain> m_rd;
155
156   /**
157    * The next-hop interface [if present].
158    */
159   std::shared_ptr<interface> m_interface;
160
161   /**
162    * UCMP weight
163    */
164   uint8_t m_weight;
165
166   /**
167    * Path preference
168    */
169   uint8_t m_preference;
170 };
171
172 /**
173  * A path-list is a set of paths
174  */
175 typedef std::set<path> path_list_t;
176
177 /**
178  * ostream output for iterator
179  */
180 std::ostream& operator<<(std::ostream& os, const path_list_t& path_list);
181
182 /**
183  * A IP route
184  */
185 class ip_route : public object_base
186 {
187 public:
188   /**
189    * The key for a route
190    */
191   typedef std::pair<route::table_id_t, prefix_t> key_t;
192
193   /**
194    * Construct a route in the default table
195    */
196   ip_route(const prefix_t& prefix);
197
198   /**
199    * Copy Construct
200    */
201   ip_route(const ip_route& r);
202
203   /**
204    * Construct a route in the given route domain
205    */
206   ip_route(const route_domain& rd, const prefix_t& prefix);
207
208   /**
209    * Destructor
210    */
211   ~ip_route();
212
213   /**
214    * Return the matching 'singular instance'
215    */
216   std::shared_ptr<ip_route> singular() const;
217
218   /**
219    * Add a path.
220    */
221   void add(const path& path);
222
223   /**
224    * remove a path.
225    */
226   void remove(const path& path);
227
228   /**
229    * Find the instnace of the route domain in the OM
230    */
231   static std::shared_ptr<ip_route> find(const ip_route& temp);
232
233   /**
234    * Dump all route-doamin into the stream provided
235    */
236   static void dump(std::ostream& os);
237
238   /**
239    * replay the object to create it in hardware
240    */
241   void replay(void);
242
243   /**
244    * Convert to string for debugging
245    */
246   std::string to_string() const;
247
248 private:
249   /**
250    * Class definition for listeners to OM events
251    */
252   class event_handler : public OM::listener, public inspect::command_handler
253   {
254   public:
255     event_handler();
256     virtual ~event_handler() = default;
257
258     /**
259      * Handle a populate event
260      */
261     void handle_populate(const client_db::key_t& key);
262
263     /**
264      * Handle a replay event
265      */
266     void handle_replay();
267
268     /**
269      * Show the object in the Singular DB
270      */
271     void show(std::ostream& os);
272
273     /**
274      * Get the sortable Id of the listener
275      */
276     dependency_t order() const;
277   };
278
279   /**
280    * event_handler to register with OM
281    */
282   static event_handler m_evh;
283
284   /**
285    * Find or add the instnace of the route domain in the OM
286    */
287   static std::shared_ptr<ip_route> find_or_add(const ip_route& temp);
288
289   /*
290    * It's the OM class that updates the objects in HW
291    */
292   friend class VOM::OM;
293
294   /**
295    * It's the singular_db class that calls replay()
296    */
297   friend class singular_db<key_t, ip_route>;
298
299   /**
300    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
301    */
302   void update(const ip_route& obj);
303
304   /**
305    * Sweep/reap the object if still stale
306    */
307   void sweep(void);
308
309   /**
310    * HW configuration for the result of creating the route
311    */
312   HW::item<bool> m_hw;
313
314   /**
315    * The route domain the route is in.
316    */
317   std::shared_ptr<route_domain> m_rd;
318
319   /**
320    * The prefix to match
321    */
322   prefix_t m_prefix;
323
324   /**
325    * The set of paths
326    */
327   path_list_t m_paths;
328
329   /**
330    * A map of all routes
331    */
332   static singular_db<key_t, ip_route> m_db;
333 };
334
335 std::ostream& operator<<(std::ostream& os, const ip_route::key_t& key);
336 };
337 };
338
339 /*
340  * fd.io coding-style-patch-verification: ON
341  *
342  * Local Variables:
343  * eval: (c-set-style "mozilla")
344  * End:
345  */
346
347 #endif