blob: 780881e5805bdeca44db22353e810178f467143c [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#include "vom/route.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/route_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018#include "vom/singular_db.hpp"
19
Neale Ranns812ed392017-10-16 04:20:13 -070020namespace VOM {
21namespace route {
Neale Ranns10e7a9f2017-11-14 08:40:43 -080022ip_route::event_handler ip_route::m_evh;
Neale Ranns812ed392017-10-16 04:20:13 -070023singular_db<ip_route::key_t, ip_route> ip_route::m_db;
24
25const path::special_t path::special_t::STANDARD(0, "standard");
26const path::special_t path::special_t::LOCAL(0, "local");
27const path::special_t path::special_t::DROP(0, "standard");
28const path::special_t path::special_t::UNREACH(0, "unreachable");
29const path::special_t path::special_t::PROHIBIT(0, "prohibit");
30
31path::special_t::special_t(int v, const std::string& s)
32 : enum_base<path::special_t>(v, s)
33{
34}
35
Neale Rannsf068c3e2018-01-03 04:18:48 -080036const path::flags_t path::flags_t::NONE(0, "none");
37const path::flags_t path::flags_t::DVR((1 << 0), "dvr");
38
39path::flags_t::flags_t(int v, const std::string& s)
40 : enum_base<path::flags_t>(v, s)
41{
42}
43
Neale Ranns812ed392017-10-16 04:20:13 -070044path::path(special_t special)
45 : m_type(special)
46 , m_nh_proto(nh_proto_t::IPV4)
Neale Rannsf068c3e2018-01-03 04:18:48 -080047 , m_flags(flags_t::NONE)
Neale Ranns812ed392017-10-16 04:20:13 -070048 , m_nh()
49 , m_rd(nullptr)
50 , m_interface(nullptr)
51 , m_weight(1)
52 , m_preference(0)
53{
54}
55
56path::path(const boost::asio::ip::address& nh,
57 const interface& interface,
58 uint8_t weight,
59 uint8_t preference)
60 : m_type(special_t::STANDARD)
61 , m_nh_proto(nh_proto_t::from_address(nh))
Neale Rannsf068c3e2018-01-03 04:18:48 -080062 , m_flags(flags_t::NONE)
Neale Ranns812ed392017-10-16 04:20:13 -070063 , m_nh(nh)
64 , m_rd(nullptr)
65 , m_interface(interface.singular())
66 , m_weight(weight)
67 , m_preference(preference)
68{
69}
70
71path::path(const route_domain& rd,
72 const boost::asio::ip::address& nh,
73 uint8_t weight,
74 uint8_t preference)
75 : m_type(special_t::STANDARD)
76 , m_nh_proto(nh_proto_t::from_address(nh))
Neale Rannsf068c3e2018-01-03 04:18:48 -080077 , m_flags(flags_t::NONE)
Neale Ranns812ed392017-10-16 04:20:13 -070078 , m_nh(nh)
79 , m_rd(rd.singular())
80 , m_interface(nullptr)
81 , m_weight(weight)
82 , m_preference(preference)
83{
84}
85
86path::path(const interface& interface,
87 const nh_proto_t& proto,
Neale Rannsf068c3e2018-01-03 04:18:48 -080088 const flags_t& flags,
Neale Ranns812ed392017-10-16 04:20:13 -070089 uint8_t weight,
90 uint8_t preference)
91 : m_type(special_t::STANDARD)
92 , m_nh_proto(proto)
Neale Rannsf068c3e2018-01-03 04:18:48 -080093 , m_flags(flags)
Neale Ranns812ed392017-10-16 04:20:13 -070094 , m_nh()
95 , m_rd(nullptr)
96 , m_interface(interface.singular())
97 , m_weight(weight)
98 , m_preference(preference)
99{
100}
101
102path::path(const path& p)
103 : m_type(p.m_type)
104 , m_nh_proto(p.m_nh_proto)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800105 , m_flags(p.m_flags)
Neale Ranns812ed392017-10-16 04:20:13 -0700106 , m_nh(p.m_nh)
107 , m_rd(p.m_rd)
108 , m_interface(p.m_interface)
109 , m_weight(p.m_weight)
110 , m_preference(p.m_preference)
111{
112}
113
114bool
115path::operator<(const path& p) const
116{
Neale Rannsf068c3e2018-01-03 04:18:48 -0800117 if (m_nh_proto < p.m_nh_proto)
118 return true;
119 if (m_flags < p.m_flags)
120 return true;
Neale Ranns812ed392017-10-16 04:20:13 -0700121 if (m_type < p.m_type)
122 return true;
Neale Rannsfd920602017-11-23 12:15:00 -0800123 if (m_rd && !p.m_rd)
124 return false;
125 if (!m_rd && p.m_rd)
126 return true;
Neale Ranns812ed392017-10-16 04:20:13 -0700127 if (m_rd->table_id() < p.m_rd->table_id())
128 return true;
129 if (m_nh < p.m_nh)
130 return true;
Neale Rannsfd920602017-11-23 12:15:00 -0800131 if (m_interface && !p.m_interface)
132 return false;
133 if (!m_interface && p.m_interface)
134 return true;
Neale Ranns812ed392017-10-16 04:20:13 -0700135 if (m_interface->handle() < p.m_interface->handle())
136 return true;
137
138 return (false);
139}
140
Neale Rannsfd920602017-11-23 12:15:00 -0800141path::~path()
142{
143}
144
145bool
146path::operator==(const path& p) const
147{
148 bool result = true;
149 if (m_rd && !p.m_rd)
150 return false;
151 if (!m_rd && p.m_rd)
152 return false;
153 if (m_rd && p.m_rd)
154 result &= (*m_rd == *p.m_rd);
155 if (m_interface && !p.m_interface)
156 return false;
157 if (!m_interface && p.m_interface)
158 return false;
159 if (m_interface && p.m_interface)
160 result &= (*m_interface == *p.m_interface);
Neale Rannsf068c3e2018-01-03 04:18:48 -0800161 return (result && (m_type == p.m_type) && (m_nh == p.m_nh) &&
162 (m_nh_proto == p.m_nh_proto) && (m_flags == p.m_flags));
Neale Rannsfd920602017-11-23 12:15:00 -0800163}
164
Neale Ranns812ed392017-10-16 04:20:13 -0700165std::string
166path::to_string() const
167{
168 std::ostringstream s;
169
170 s << "path:["
171 << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800172 << " flags:" << m_flags.to_string() << " neighbour:" << m_nh.to_string();
Neale Ranns812ed392017-10-16 04:20:13 -0700173 if (m_rd) {
174 s << " " << m_rd->to_string();
175 }
176 if (m_interface) {
177 s << " " << m_interface->to_string();
178 }
179 s << " weight:" << static_cast<int>(m_weight)
180 << " preference:" << static_cast<int>(m_preference) << "]";
181
182 return (s.str());
183}
184
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700185path::special_t
186path::type() const
187{
188 return m_type;
189}
190
191nh_proto_t
192path::nh_proto() const
193{
194 return m_nh_proto;
195}
196
Neale Rannsf068c3e2018-01-03 04:18:48 -0800197path::flags_t
198path::flags() const
199{
200 return m_flags;
201}
202
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700203const boost::asio::ip::address&
204path::nh() const
205{
206 return m_nh;
207}
208
209std::shared_ptr<route_domain>
210path::rd() const
211{
212 return m_rd;
213}
214
215std::shared_ptr<interface>
216path::itf() const
217{
218 return m_interface;
219}
220
221uint8_t
222path::weight() const
223{
224 return m_weight;
225}
226
227uint8_t
228path::preference() const
229{
230 return m_preference;
231}
232
Neale Rannsfd920602017-11-23 12:15:00 -0800233ip_route::ip_route(const prefix_t& prefix, const path& p)
234 : m_hw(false)
235 , m_rd(route_domain::get_default())
236 , m_prefix(prefix)
237 , m_paths({ p })
238{
239}
240
Neale Ranns812ed392017-10-16 04:20:13 -0700241ip_route::ip_route(const prefix_t& prefix)
242 : m_hw(false)
243 , m_rd(route_domain::get_default())
244 , m_prefix(prefix)
245 , m_paths()
246{
247}
248
249ip_route::ip_route(const ip_route& r)
250 : m_hw(r.m_hw)
251 , m_rd(r.m_rd)
252 , m_prefix(r.m_prefix)
253 , m_paths(r.m_paths)
254{
255}
256
257ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
258 : m_hw(false)
259 , m_rd(rd.singular())
260 , m_prefix(prefix)
261 , m_paths()
262{
263}
264
Neale Rannsfd920602017-11-23 12:15:00 -0800265ip_route::ip_route(const route_domain& rd,
266 const prefix_t& prefix,
267 const path& p)
268 : m_hw(false)
269 , m_rd(rd.singular())
270 , m_prefix(prefix)
271 , m_paths({ p })
272{
273}
274
Neale Ranns812ed392017-10-16 04:20:13 -0700275ip_route::~ip_route()
276{
277 sweep();
278
279 // not in the DB anymore.
Neale Rannsfd920602017-11-23 12:15:00 -0800280 m_db.release(key(), this);
281 m_paths.clear();
282}
283
284const ip_route::key_t
285ip_route::key() const
286{
287 return (std::make_pair(m_rd->table_id(), m_prefix));
288}
289
290bool
291ip_route::operator==(const ip_route& i) const
292{
293 return ((key() == i.key()) && (m_paths == i.m_paths));
Neale Ranns812ed392017-10-16 04:20:13 -0700294}
295
296void
297ip_route::add(const path& path)
298{
299 m_paths.insert(path);
300}
301
302void
303ip_route::remove(const path& path)
304{
305 m_paths.erase(path);
306}
307
308void
309ip_route::sweep()
310{
311 if (m_hw) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700312 HW::enqueue(
313 new ip_route_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
Neale Ranns812ed392017-10-16 04:20:13 -0700314 }
315 HW::write();
316}
317
318void
319ip_route::replay()
320{
321 if (m_hw) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700322 HW::enqueue(
323 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
Neale Ranns812ed392017-10-16 04:20:13 -0700324 }
325}
326std::string
327ip_route::to_string() const
328{
329 std::ostringstream s;
330 s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
331 << m_paths << "]"
332 << "]";
333
334 return (s.str());
335}
336
337void
338ip_route::update(const ip_route& r)
339{
340 /*
341* create the table if it is not yet created
342*/
343 if (rc_t::OK != m_hw.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700344 HW::enqueue(
345 new ip_route_cmds::update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
Neale Ranns812ed392017-10-16 04:20:13 -0700346 }
347}
348
349std::shared_ptr<ip_route>
350ip_route::find_or_add(const ip_route& temp)
351{
Neale Rannsfd920602017-11-23 12:15:00 -0800352 return (m_db.find_or_add(temp.key(), temp));
353}
354
355std::shared_ptr<ip_route>
356ip_route::find(const key_t& k)
357{
358 return (m_db.find(k));
Neale Ranns812ed392017-10-16 04:20:13 -0700359}
360
361std::shared_ptr<ip_route>
362ip_route::singular() const
363{
364 return find_or_add(*this);
365}
366
367void
368ip_route::dump(std::ostream& os)
369{
370 m_db.dump(os);
371}
372
373ip_route::event_handler::event_handler()
374{
375 OM::register_listener(this);
376 inspect::register_handler({ "ip-route" }, "ip route configurations", this);
377}
378
379void
380ip_route::event_handler::handle_replay()
381{
382 m_db.replay();
383}
384
385void
386ip_route::event_handler::handle_populate(const client_db::key_t& key)
387{
Neale Ranns1d781552017-11-27 04:52:35 -0800388 std::shared_ptr<ip_route_cmds::dump_v4_cmd> cmd_v4 =
389 std::make_shared<ip_route_cmds::dump_v4_cmd>();
390 std::shared_ptr<ip_route_cmds::dump_v6_cmd> cmd_v6 =
391 std::make_shared<ip_route_cmds::dump_v6_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700392
393 HW::enqueue(cmd_v4);
394 HW::enqueue(cmd_v6);
395 HW::write();
396
397 for (auto& record : *cmd_v4) {
398 auto& payload = record.get_payload();
399
400 prefix_t pfx(0, payload.address, payload.address_length);
401
402 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800403 * populating the route domain here
404 */
Neale Ranns812ed392017-10-16 04:20:13 -0700405 route_domain rd_temp(payload.table_id);
Neale Ranns7016b002018-01-25 07:28:19 -0800406 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
Neale Ranns812ed392017-10-16 04:20:13 -0700407 if (!rd) {
408 OM::commit(key, rd_temp);
409 }
410 ip_route ip_r(rd_temp, pfx);
411
412 for (unsigned int i = 0; i < payload.count; i++) {
413 vapi_type_fib_path p = payload.path[i];
414 if (p.is_local) {
415 path path_v4(path::special_t::LOCAL);
416 ip_r.add(path_v4);
417 } else if (p.is_drop) {
418 path path_v4(path::special_t::DROP);
419 ip_r.add(path_v4);
420 } else if (p.is_unreach) {
421 path path_v4(path::special_t::UNREACH);
422 ip_r.add(path_v4);
423 } else if (p.is_prohibit) {
424 path path_v4(path::special_t::PROHIBIT);
425 ip_r.add(path_v4);
426 } else {
Neale Ranns812ed392017-10-16 04:20:13 -0700427 boost::asio::ip::address address = from_bytes(0, p.next_hop);
Mohsin Kazmia3706792018-01-12 19:11:44 +0100428 std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
429 if (itf) {
430 path path_v4(address, *itf, p.weight, p.preference);
431 ip_r.add(path_v4);
432 } else {
433 path path_v4(rd_temp, address, p.weight, p.preference);
434 ip_r.add(path_v4);
435 }
Neale Ranns812ed392017-10-16 04:20:13 -0700436 }
437 }
438 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
439
440 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800441 * Write each of the discovered interfaces into the OM,
442 * but disable the HW Command q whilst we do, so that no
443 * commands are sent to VPP
444 */
Neale Ranns812ed392017-10-16 04:20:13 -0700445 OM::commit(key, ip_r);
446 }
447
448 for (auto& record : *cmd_v6) {
449 auto& payload = record.get_payload();
450
451 prefix_t pfx(1, payload.address, payload.address_length);
452 route_domain rd_temp(payload.table_id);
Neale Ranns7016b002018-01-25 07:28:19 -0800453 std::shared_ptr<route_domain> rd = route_domain::find(payload.table_id);
Neale Ranns812ed392017-10-16 04:20:13 -0700454 if (!rd) {
455 OM::commit(key, rd_temp);
456 }
457 ip_route ip_r(rd_temp, pfx);
458
459 for (unsigned int i = 0; i < payload.count; i++) {
460 vapi_type_fib_path p = payload.path[i];
461 if (p.is_local) {
462 path path_v6(path::special_t::LOCAL);
463 ip_r.add(path_v6);
464 } else if (p.is_drop) {
465 path path_v6(path::special_t::DROP);
466 ip_r.add(path_v6);
467 } else if (p.is_unreach) {
468 path path_v6(path::special_t::UNREACH);
469 ip_r.add(path_v6);
470 } else if (p.is_prohibit) {
471 path path_v6(path::special_t::PROHIBIT);
472 ip_r.add(path_v6);
473 } else {
474 std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
475 boost::asio::ip::address address = from_bytes(1, p.next_hop);
Mohsin Kazmia3706792018-01-12 19:11:44 +0100476 if (itf) {
477 path path_v6(address, *itf, p.weight, p.preference);
478 ip_r.add(path_v6);
479 } else {
480 path path_v6(rd_temp, address, p.weight, p.preference);
481 ip_r.add(path_v6);
482 }
Neale Ranns812ed392017-10-16 04:20:13 -0700483 }
484 }
485 VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
486
487 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800488 * Write each of the discovered interfaces into the OM,
489 * but disable the HW Command q whilst we do, so that no
490 * commands are sent to VPP
491 */
Neale Ranns812ed392017-10-16 04:20:13 -0700492 OM::commit(key, ip_r);
493 }
494}
495
496dependency_t
497ip_route::event_handler::order() const
498{
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100499 return (dependency_t::ENTRY);
Neale Ranns812ed392017-10-16 04:20:13 -0700500}
501
502void
503ip_route::event_handler::show(std::ostream& os)
504{
505 m_db.dump(os);
506}
507
508std::ostream&
509operator<<(std::ostream& os, const ip_route::key_t& key)
510{
511 os << "[" << key.first << ", " << key.second.to_string() << "]";
512
513 return (os);
514}
515
516std::ostream&
517operator<<(std::ostream& os, const path_list_t& key)
518{
519 os << "[";
520 for (auto k : key) {
521 os << k.to_string() << " ";
522 }
523 os << "]";
524
525 return (os);
526}
527}
528}
529/*
530 * fd.io coding-style-patch-verification: ON
531 *
532 * Local Variables:
533 * eval: (c-set-style "mozilla")
534 * End:
535 */