VPP Object Model (VOM)
[vpp.git] / src / vpp-api / vom / route.cpp
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 #include "vom/route.hpp"
17 #include "vom/singular_db.hpp"
18
19 #include <vapi/ip.api.vapi.hpp>
20
21 namespace VOM {
22 namespace route {
23 singular_db<ip_route::key_t, ip_route> ip_route::m_db;
24
25 const path::special_t path::special_t::STANDARD(0, "standard");
26 const path::special_t path::special_t::LOCAL(0, "local");
27 const path::special_t path::special_t::DROP(0, "standard");
28 const path::special_t path::special_t::UNREACH(0, "unreachable");
29 const path::special_t path::special_t::PROHIBIT(0, "prohibit");
30
31 path::special_t::special_t(int v, const std::string& s)
32   : enum_base<path::special_t>(v, s)
33 {
34 }
35
36 path::path(special_t special)
37   : m_type(special)
38   , m_nh_proto(nh_proto_t::IPV4)
39   , m_nh()
40   , m_rd(nullptr)
41   , m_interface(nullptr)
42   , m_weight(1)
43   , m_preference(0)
44 {
45 }
46
47 path::path(const boost::asio::ip::address& nh,
48            const interface& interface,
49            uint8_t weight,
50            uint8_t preference)
51   : m_type(special_t::STANDARD)
52   , m_nh_proto(nh_proto_t::from_address(nh))
53   , m_nh(nh)
54   , m_rd(nullptr)
55   , m_interface(interface.singular())
56   , m_weight(weight)
57   , m_preference(preference)
58 {
59 }
60
61 path::path(const route_domain& rd,
62            const boost::asio::ip::address& nh,
63            uint8_t weight,
64            uint8_t preference)
65   : m_type(special_t::STANDARD)
66   , m_nh_proto(nh_proto_t::from_address(nh))
67   , m_nh(nh)
68   , m_rd(rd.singular())
69   , m_interface(nullptr)
70   , m_weight(weight)
71   , m_preference(preference)
72 {
73 }
74
75 path::path(const interface& interface,
76            const nh_proto_t& proto,
77            uint8_t weight,
78            uint8_t preference)
79   : m_type(special_t::STANDARD)
80   , m_nh_proto(proto)
81   , m_nh()
82   , m_rd(nullptr)
83   , m_interface(interface.singular())
84   , m_weight(weight)
85   , m_preference(preference)
86 {
87 }
88
89 path::path(const path& p)
90   : m_type(p.m_type)
91   , m_nh_proto(p.m_nh_proto)
92   , m_nh(p.m_nh)
93   , m_rd(p.m_rd)
94   , m_interface(p.m_interface)
95   , m_weight(p.m_weight)
96   , m_preference(p.m_preference)
97 {
98 }
99
100 bool
101 path::operator<(const path& p) const
102 {
103   if (m_type < p.m_type)
104     return true;
105   if (m_rd->table_id() < p.m_rd->table_id())
106     return true;
107   if (m_nh < p.m_nh)
108     return true;
109   if (m_interface->handle() < p.m_interface->handle())
110     return true;
111
112   return (false);
113 }
114
115 void
116 path::to_vpp(vapi_payload_ip_add_del_route& payload) const
117 {
118   payload.is_drop = 0;
119   payload.is_unreach = 0;
120   payload.is_prohibit = 0;
121   payload.is_local = 0;
122   payload.is_classify = 0;
123   payload.is_multipath = 0;
124   payload.is_resolve_host = 0;
125   payload.is_resolve_attached = 0;
126
127   if (nh_proto_t::ETHERNET == m_nh_proto) {
128     payload.is_l2_bridged = 1;
129   }
130
131   if (special_t::STANDARD == m_type) {
132     uint8_t path_v6;
133     to_bytes(m_nh, &path_v6, payload.next_hop_address);
134
135     if (m_rd) {
136       payload.next_hop_table_id = m_rd->table_id();
137     }
138     if (m_interface) {
139       payload.next_hop_sw_if_index = m_interface->handle().value();
140     }
141   } else if (special_t::DROP == m_type) {
142     payload.is_drop = 1;
143   } else if (special_t::UNREACH == m_type) {
144     payload.is_unreach = 1;
145   } else if (special_t::PROHIBIT == m_type) {
146     payload.is_prohibit = 1;
147   } else if (special_t::LOCAL == m_type) {
148     payload.is_local = 1;
149   }
150   payload.next_hop_weight = m_weight;
151   payload.next_hop_preference = m_preference;
152   payload.next_hop_via_label = 0;
153   payload.classify_table_index = 0;
154 }
155
156 std::string
157 path::to_string() const
158 {
159   std::ostringstream s;
160
161   s << "path:["
162     << "type:" << m_type.to_string() << " proto:" << m_nh_proto.to_string()
163     << " neighbour:" << m_nh.to_string();
164   if (m_rd) {
165     s << " " << m_rd->to_string();
166   }
167   if (m_interface) {
168     s << " " << m_interface->to_string();
169   }
170   s << " weight:" << static_cast<int>(m_weight)
171     << " preference:" << static_cast<int>(m_preference) << "]";
172
173   return (s.str());
174 }
175
176 ip_route::ip_route(const prefix_t& prefix)
177   : m_hw(false)
178   , m_rd(route_domain::get_default())
179   , m_prefix(prefix)
180   , m_paths()
181 {
182 }
183
184 ip_route::ip_route(const ip_route& r)
185   : m_hw(r.m_hw)
186   , m_rd(r.m_rd)
187   , m_prefix(r.m_prefix)
188   , m_paths(r.m_paths)
189 {
190 }
191
192 ip_route::ip_route(const route_domain& rd, const prefix_t& prefix)
193   : m_hw(false)
194   , m_rd(rd.singular())
195   , m_prefix(prefix)
196   , m_paths()
197 {
198 }
199
200 ip_route::~ip_route()
201 {
202   sweep();
203
204   // not in the DB anymore.
205   m_db.release(std::make_pair(m_rd->table_id(), m_prefix), this);
206 }
207
208 void
209 ip_route::add(const path& path)
210 {
211   m_paths.insert(path);
212 }
213
214 void
215 ip_route::remove(const path& path)
216 {
217   m_paths.erase(path);
218 }
219
220 void
221 ip_route::sweep()
222 {
223   if (m_hw) {
224     HW::enqueue(new delete_cmd(m_hw, m_rd->table_id(), m_prefix));
225   }
226   HW::write();
227 }
228
229 void
230 ip_route::replay()
231 {
232   if (m_hw) {
233     HW::enqueue(new update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
234   }
235 }
236 std::string
237 ip_route::to_string() const
238 {
239   std::ostringstream s;
240   s << "route:[" << m_rd->to_string() << ", " << m_prefix.to_string() << " ["
241     << m_paths << "]"
242     << "]";
243
244   return (s.str());
245 }
246
247 void
248 ip_route::update(const ip_route& r)
249 {
250   /*
251 * create the table if it is not yet created
252 */
253   if (rc_t::OK != m_hw.rc()) {
254     HW::enqueue(new update_cmd(m_hw, m_rd->table_id(), m_prefix, m_paths));
255   }
256 }
257
258 std::shared_ptr<ip_route>
259 ip_route::find_or_add(const ip_route& temp)
260 {
261   return (m_db.find_or_add(std::make_pair(temp.m_rd->table_id(), temp.m_prefix),
262                            temp));
263 }
264
265 std::shared_ptr<ip_route>
266 ip_route::singular() const
267 {
268   return find_or_add(*this);
269 }
270
271 void
272 ip_route::dump(std::ostream& os)
273 {
274   m_db.dump(os);
275 }
276
277 ip_route::event_handler::event_handler()
278 {
279   OM::register_listener(this);
280   inspect::register_handler({ "ip-route" }, "ip route configurations", this);
281 }
282
283 void
284 ip_route::event_handler::handle_replay()
285 {
286   m_db.replay();
287 }
288
289 void
290 ip_route::event_handler::handle_populate(const client_db::key_t& key)
291 {
292   std::shared_ptr<ip_route::dump_v4_cmd> cmd_v4(new ip_route::dump_v4_cmd());
293   std::shared_ptr<ip_route::dump_v6_cmd> cmd_v6(new ip_route::dump_v6_cmd());
294
295   HW::enqueue(cmd_v4);
296   HW::enqueue(cmd_v6);
297   HW::write();
298
299   for (auto& record : *cmd_v4) {
300     auto& payload = record.get_payload();
301
302     prefix_t pfx(0, payload.address, payload.address_length);
303
304     /**
305 * populating the route domain here
306 */
307     route_domain rd_temp(payload.table_id);
308     std::shared_ptr<route_domain> rd = route_domain::find(rd_temp);
309     if (!rd) {
310       OM::commit(key, rd_temp);
311     }
312     ip_route ip_r(rd_temp, pfx);
313
314     for (unsigned int i = 0; i < payload.count; i++) {
315       vapi_type_fib_path p = payload.path[i];
316       if (p.is_local) {
317         path path_v4(path::special_t::LOCAL);
318         ip_r.add(path_v4);
319       } else if (p.is_drop) {
320         path path_v4(path::special_t::DROP);
321         ip_r.add(path_v4);
322       } else if (p.is_unreach) {
323         path path_v4(path::special_t::UNREACH);
324         ip_r.add(path_v4);
325       } else if (p.is_prohibit) {
326         path path_v4(path::special_t::PROHIBIT);
327         ip_r.add(path_v4);
328       } else {
329         std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
330         boost::asio::ip::address address = from_bytes(0, p.next_hop);
331         path path_v4(address, *itf, p.weight, p.preference);
332         ip_r.add(path_v4);
333       }
334     }
335     VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
336
337     /*
338 * Write each of the discovered interfaces into the OM,
339 * but disable the HW Command q whilst we do, so that no
340 * commands are sent to VPP
341 */
342     OM::commit(key, ip_r);
343   }
344
345   for (auto& record : *cmd_v6) {
346     auto& payload = record.get_payload();
347
348     prefix_t pfx(1, payload.address, payload.address_length);
349     route_domain rd_temp(payload.table_id);
350     std::shared_ptr<route_domain> rd = route_domain::find(rd_temp);
351     if (!rd) {
352       OM::commit(key, rd_temp);
353     }
354     ip_route ip_r(rd_temp, pfx);
355
356     for (unsigned int i = 0; i < payload.count; i++) {
357       vapi_type_fib_path p = payload.path[i];
358       if (p.is_local) {
359         path path_v6(path::special_t::LOCAL);
360         ip_r.add(path_v6);
361       } else if (p.is_drop) {
362         path path_v6(path::special_t::DROP);
363         ip_r.add(path_v6);
364       } else if (p.is_unreach) {
365         path path_v6(path::special_t::UNREACH);
366         ip_r.add(path_v6);
367       } else if (p.is_prohibit) {
368         path path_v6(path::special_t::PROHIBIT);
369         ip_r.add(path_v6);
370       } else {
371         std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
372         boost::asio::ip::address address = from_bytes(1, p.next_hop);
373         path path_v6(address, *itf, p.weight, p.preference);
374         ip_r.add(path_v6);
375       }
376     }
377     VOM_LOG(log_level_t::DEBUG) << "ip-route-dump: " << ip_r.to_string();
378
379     /*
380 * Write each of the discovered interfaces into the OM,
381 * but disable the HW Command q whilst we do, so that no
382 * commands are sent to VPP
383 */
384     OM::commit(key, ip_r);
385   }
386 }
387
388 dependency_t
389 ip_route::event_handler::order() const
390 {
391   return (dependency_t::BINDING);
392 }
393
394 void
395 ip_route::event_handler::show(std::ostream& os)
396 {
397   m_db.dump(os);
398 }
399
400 std::ostream&
401 operator<<(std::ostream& os, const ip_route::key_t& key)
402 {
403   os << "[" << key.first << ", " << key.second.to_string() << "]";
404
405   return (os);
406 }
407
408 std::ostream&
409 operator<<(std::ostream& os, const path_list_t& key)
410 {
411   os << "[";
412   for (auto k : key) {
413     os << k.to_string() << " ";
414   }
415   os << "]";
416
417   return (os);
418 }
419 }
420 }
421 /*
422  * fd.io coding-style-patch-verification: ON
423  *
424  * Local Variables:
425  * eval: (c-set-style "mozilla")
426  * End:
427  */