blob: 65797b7c2f98cf3d6b37a972a521391280438184 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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
Neale Ranns812ed392017-10-16 04:20:13 -070024namespace VOM {
25/**
26 * Types belonging to Routing
27 */
28namespace route {
29/**
30 * A path for IP or MPLS routes
31 */
32class path
33{
34public:
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 /**
Neale Rannsf068c3e2018-01-03 04:18:48 -080075 * Path flags
76 */
77 class flags_t : public enum_base<flags_t>
78 {
79 public:
80 /**
81 * No flags
82 */
83 const static flags_t NONE;
84
85 /**
86 * A path that resolves via a DVR next-hop
87 */
88 const static flags_t DVR;
89
90 private:
91 /**
92 * Private constructor taking the value and the string name
93 */
94 flags_t(int v, const std::string& s);
95 };
96
97 /**
Neale Ranns812ed392017-10-16 04:20:13 -070098 * constructor for special paths
99 */
100 path(special_t special);
101
102 /**
103 * Constructor for standard non-recursive paths
104 */
105 path(const boost::asio::ip::address& nh,
106 const interface& interface,
107 uint8_t weight = 1,
108 uint8_t preference = 0);
109
110 /**
111 * Constructor for standard recursive paths
112 */
113 path(const route_domain& rd,
114 const boost::asio::ip::address& nh,
115 uint8_t weight = 1,
116 uint8_t preference = 0);
117
118 /**
119 * Constructor for DVR paths or attached paths.
120 */
121 path(const interface& interface,
122 const nh_proto_t& proto,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800123 const flags_t& flags = flags_t::NONE,
Neale Ranns812ed392017-10-16 04:20:13 -0700124 uint8_t weight = 1,
125 uint8_t preference = 0);
126
127 /**
128 * Copy Constructor
129 */
130 path(const path& p);
131
132 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800133 * Destructor
Neale Ranns812ed392017-10-16 04:20:13 -0700134 */
Neale Rannsfd920602017-11-23 12:15:00 -0800135 ~path();
136
137 /**
138 * comparison operator
139 */
140 bool operator==(const path& p) const;
Neale Ranns812ed392017-10-16 04:20:13 -0700141
142 /**
143 * Less than operator for set insertion
144 */
145 bool operator<(const path& p) const;
146
147 /**
148 * convert to string format for debug purposes
149 */
150 std::string to_string() const;
151
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700152 /**
153 * Getters
154 */
155 special_t type() const;
156 nh_proto_t nh_proto() const;
Neale Rannsf068c3e2018-01-03 04:18:48 -0800157 flags_t flags() const;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700158 const boost::asio::ip::address& nh() const;
159 std::shared_ptr<route_domain> rd() const;
160 std::shared_ptr<interface> itf() const;
161 uint8_t weight() const;
162 uint8_t preference() const;
163
Neale Ranns812ed392017-10-16 04:20:13 -0700164private:
165 /**
166 * The special path tpye
167 */
168 special_t m_type;
169
170 /**
171 * The next-hop protocol
172 */
173 nh_proto_t m_nh_proto;
174
175 /**
Neale Rannsf068c3e2018-01-03 04:18:48 -0800176 * Flags for the path
177 */
178 flags_t m_flags;
179
180 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700181 * The next-hop
182 */
183 boost::asio::ip::address m_nh;
184
185 /**
186 * For recursive routes, this is the table in which the
187 * the next-hop exists.
188 */
189 std::shared_ptr<route_domain> m_rd;
190
191 /**
192 * The next-hop interface [if present].
193 */
194 std::shared_ptr<interface> m_interface;
195
196 /**
197 * UCMP weight
198 */
199 uint8_t m_weight;
200
201 /**
202 * Path preference
203 */
204 uint8_t m_preference;
205};
206
207/**
208 * A path-list is a set of paths
209 */
210typedef std::set<path> path_list_t;
211
212/**
213 * ostream output for iterator
214 */
215std::ostream& operator<<(std::ostream& os, const path_list_t& path_list);
216
217/**
218 * A IP route
219 */
220class ip_route : public object_base
221{
222public:
223 /**
224 * The key for a route
225 */
226 typedef std::pair<route::table_id_t, prefix_t> key_t;
227
228 /**
229 * Construct a route in the default table
230 */
231 ip_route(const prefix_t& prefix);
232
233 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800234 * Construct a route with a path
235 */
236 ip_route(const prefix_t& prefix, const path& p);
237
238 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700239 * Copy Construct
240 */
241 ip_route(const ip_route& r);
242
243 /**
244 * Construct a route in the given route domain
245 */
246 ip_route(const route_domain& rd, const prefix_t& prefix);
247
248 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800249 * Construct a route in the given route domain with a path
250 */
251 ip_route(const route_domain& rd, const prefix_t& prefix, const path& p);
252
253 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700254 * Destructor
255 */
256 ~ip_route();
257
258 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800259 * Get the route's key
260 */
261 const key_t key() const;
262
263 /**
264 * Comparison operator
265 */
266 bool operator==(const ip_route& i) const;
267
268 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700269 * Return the matching 'singular instance'
270 */
271 std::shared_ptr<ip_route> singular() const;
272
273 /**
274 * Add a path.
275 */
276 void add(const path& path);
277
278 /**
279 * remove a path.
280 */
281 void remove(const path& path);
282
283 /**
284 * Find the instnace of the route domain in the OM
285 */
286 static std::shared_ptr<ip_route> find(const ip_route& temp);
287
288 /**
289 * Dump all route-doamin into the stream provided
290 */
291 static void dump(std::ostream& os);
292
293 /**
294 * replay the object to create it in hardware
295 */
296 void replay(void);
297
298 /**
299 * Convert to string for debugging
300 */
301 std::string to_string() const;
302
Neale Rannsfd920602017-11-23 12:15:00 -0800303 /**
304 * Return the matching 'singular instance'
305 */
306 static std::shared_ptr<ip_route> find(const key_t& k);
307
Neale Ranns812ed392017-10-16 04:20:13 -0700308private:
309 /**
310 * Class definition for listeners to OM events
311 */
312 class event_handler : public OM::listener, public inspect::command_handler
313 {
314 public:
315 event_handler();
316 virtual ~event_handler() = default;
317
318 /**
319 * Handle a populate event
320 */
321 void handle_populate(const client_db::key_t& key);
322
323 /**
324 * Handle a replay event
325 */
326 void handle_replay();
327
328 /**
329 * Show the object in the Singular DB
330 */
331 void show(std::ostream& os);
332
333 /**
334 * Get the sortable Id of the listener
335 */
336 dependency_t order() const;
337 };
338
339 /**
340 * event_handler to register with OM
341 */
342 static event_handler m_evh;
343
344 /**
345 * Find or add the instnace of the route domain in the OM
346 */
347 static std::shared_ptr<ip_route> find_or_add(const ip_route& temp);
348
349 /*
350 * It's the OM class that updates the objects in HW
351 */
352 friend class VOM::OM;
353
354 /**
355 * It's the singular_db class that calls replay()
356 */
357 friend class singular_db<key_t, ip_route>;
358
359 /**
360 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
361 */
362 void update(const ip_route& obj);
363
364 /**
365 * Sweep/reap the object if still stale
366 */
367 void sweep(void);
368
369 /**
370 * HW configuration for the result of creating the route
371 */
372 HW::item<bool> m_hw;
373
374 /**
375 * The route domain the route is in.
376 */
377 std::shared_ptr<route_domain> m_rd;
378
379 /**
380 * The prefix to match
381 */
382 prefix_t m_prefix;
383
384 /**
385 * The set of paths
386 */
387 path_list_t m_paths;
388
389 /**
390 * A map of all routes
391 */
392 static singular_db<key_t, ip_route> m_db;
393};
394
395std::ostream& operator<<(std::ostream& os, const ip_route::key_t& key);
396};
397};
398
399/*
400 * fd.io coding-style-patch-verification: ON
401 *
402 * Local Variables:
403 * eval: (c-set-style "mozilla")
404 * End:
405 */
406
407#endif