blob: e776235bc00c945d44ef84f5455e84c62b0a25e9 [file] [log] [blame]
Neale Ranns81458422018-03-12 06:59:36 -07001/*
2 * Copyright (c) 2016 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 <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/fib/fib_api.h>
Neale Ranns097fa662018-05-01 05:17:55 -070019#include <vnet/ip/ip_types_api.h>
Neale Ranns81458422018-03-12 06:59:36 -070020#include <vnet/fib/fib_table.h>
Neale Ranns775f73c2018-12-20 03:01:49 -080021#include <vnet/mfib/mfib_table.h>
Neale Ranns81458422018-03-12 06:59:36 -070022#include <vnet/bier/bier_disp_table.h>
Neale Ranns097fa662018-05-01 05:17:55 -070023#include <vpp/api/types.h>
Neale Ranns81458422018-03-12 06:59:36 -070024
25#include <vnet/vnet_msg_enum.h>
26
27#define vl_typedefs /* define message structures */
28#include <vnet/vnet_all_api_h.h>
29#undef vl_typedefs
30
31#define vl_endianfun /* define message structures */
32#include <vnet/vnet_all_api_h.h>
33#undef vl_endianfun
34
35/* instantiate all the print functions we know about */
36#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37#define vl_printfun
38#include <vnet/vnet_all_api_h.h>
39#undef vl_printfun
40
41#include <vlibapi/api_helper_macros.h>
42
43int
Neale Ranns097fa662018-05-01 05:17:55 -070044fib_api_table_id_decode (fib_protocol_t fproto,
45 u32 table_id,
46 u32 *fib_index)
Neale Ranns81458422018-03-12 06:59:36 -070047{
Neale Ranns097fa662018-05-01 05:17:55 -070048 *fib_index = fib_table_find(fproto, table_id);
Neale Ranns81458422018-03-12 06:59:36 -070049
Neale Ranns097fa662018-05-01 05:17:55 -070050 if (INDEX_INVALID == *fib_index)
Neale Rannsf726f532019-03-11 05:34:50 -070051 {
Neale Ranns097fa662018-05-01 05:17:55 -070052 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsf726f532019-03-11 05:34:50 -070053 }
54
Neale Ranns097fa662018-05-01 05:17:55 -070055 return (0);
56}
57
58int
59fib_api_mtable_id_decode (fib_protocol_t fproto,
60 u32 table_id,
61 u32 *fib_index)
62{
63 *fib_index = mfib_table_find(fproto, table_id);
64
65 if (~0 == *fib_index)
Neale Ranns81458422018-03-12 06:59:36 -070066 {
Neale Ranns097fa662018-05-01 05:17:55 -070067 return VNET_API_ERROR_NO_SUCH_FIB;
68 }
69
70 return (0);
71}
72
73static void
74fib_api_next_hop_decode (const vl_api_fib_path_t *in,
75 ip46_address_t *out)
76{
77 if (in->proto == FIB_API_PATH_NH_PROTO_IP4)
78 memcpy (&out->ip4, &in->nh.address.ip4, sizeof (out->ip4));
79 else if (in->proto == FIB_API_PATH_NH_PROTO_IP6)
80 memcpy (&out->ip6, &in->nh.address.ip6, sizeof (out->ip6));
81}
82
83static vl_api_fib_path_nh_proto_t
84fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
85{
86 switch (dproto)
87 {
88 case DPO_PROTO_IP4:
89 return (FIB_API_PATH_NH_PROTO_IP4);
90 case DPO_PROTO_IP6:
91 return (FIB_API_PATH_NH_PROTO_IP6);
92 case DPO_PROTO_MPLS:
93 return (FIB_API_PATH_NH_PROTO_MPLS);
94 case DPO_PROTO_BIER:
95 return (FIB_API_PATH_NH_PROTO_BIER);
96 case DPO_PROTO_ETHERNET:
97 return (FIB_API_PATH_NH_PROTO_ETHERNET);
98 case DPO_PROTO_NSH:
99 ASSERT(0);
100 break;
101 }
102 return (FIB_API_PATH_NH_PROTO_IP4);
103}
104
105
106static void
107fib_api_next_hop_encode (const fib_route_path_t *rpath,
108 vl_api_fib_path_t *fp)
109{
110 fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
111
112 if (rpath->frp_proto == DPO_PROTO_IP4)
113 memcpy (&fp->nh.address.ip4,
114 &rpath->frp_addr.ip4,
115 sizeof (rpath->frp_addr.ip4));
116 else if (rpath->frp_proto == DPO_PROTO_IP6)
117 memcpy (&fp->nh.address.ip6,
118 &rpath->frp_addr.ip6,
119 sizeof (rpath->frp_addr.ip6));
120}
121
122static int
123fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
124 dpo_proto_t *dproto)
125{
126 switch (pp)
127 {
128 case FIB_API_PATH_NH_PROTO_IP4:
129 *dproto = DPO_PROTO_IP4;
130 break;
131 case FIB_API_PATH_NH_PROTO_IP6:
132 *dproto = DPO_PROTO_IP6;
133 break;
134 case FIB_API_PATH_NH_PROTO_MPLS:
135 *dproto = DPO_PROTO_MPLS;
136 break;
137 case FIB_API_PATH_NH_PROTO_BIER:
138 *dproto = DPO_PROTO_BIER;
139 break;
140 case FIB_API_PATH_NH_PROTO_ETHERNET:
141 *dproto = DPO_PROTO_ETHERNET;
142 break;
143 default:
144 return (-1);
145 }
146 return (0);
147}
148
149int
150fib_api_path_decode (vl_api_fib_path_t *in,
151 fib_route_path_t *out)
152{
153 vnet_classify_main_t *cm = &vnet_classify_main;
154 int rv = 0, n_labels;
155 vnet_main_t *vnm;
156 u8 ii;
157
158 vnm = vnet_get_main ();
159 clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
160
161 /* enums are u32 */
162 in->flags = ntohl (in->flags);
163 in->type = ntohl (in->type);
164 in->proto = ntohl (in->proto);
165
166 /*
167 * attributes that apply to all path types
168 */
169 out->frp_flags = 0;
170 out->frp_weight = in->weight;
171 if (0 == out->frp_weight)
172 {
173 out->frp_weight = 1;
174 }
175 out->frp_preference = in->preference;
176
177 rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
178
179 if (0 != rv)
180 return (rv);
181
182 /*
183 * convert the flags and the AFI to determine the path type
184 */
185 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
186 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
187 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
188 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
189
190 switch (in->type)
191 {
192 case FIB_API_PATH_TYPE_DVR:
193 out->frp_sw_if_index = ntohl(in->sw_if_index);
194 out->frp_flags |= FIB_ROUTE_PATH_DVR;
195 break;
196 case FIB_API_PATH_TYPE_INTERFACE_RX:
197 out->frp_sw_if_index = ntohl(in->sw_if_index);
198 out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
199 break;
200 case FIB_API_PATH_TYPE_DROP:
201 out->frp_flags |= FIB_ROUTE_PATH_DROP;
202 break;
203 case FIB_API_PATH_TYPE_LOCAL:
204 out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
205 out->frp_sw_if_index = ntohl(in->sw_if_index);
206 break;
207 case FIB_API_PATH_TYPE_ICMP_UNREACH:
208 out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
209 break;
210 case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
211 out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
212 break;
213 case FIB_API_PATH_TYPE_CLASSIFY:
214 out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
215
216 if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
217 {
218 return VNET_API_ERROR_NO_SUCH_TABLE;
219 }
220 out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
221 break;
222 case FIB_API_PATH_TYPE_UDP_ENCAP:
223 out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
224 out->frp_udp_encap_id = ntohl(in->nh.obj_id);
225 break;
226 case FIB_API_PATH_TYPE_BIER_IMP:
227 out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
228 out->frp_bier_imp = ntohl (in->nh.obj_id);
229 break;
230
231 case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
232 out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
233 /* fall through */
234 case FIB_API_PATH_TYPE_NORMAL:
235 switch (out->frp_proto)
236 {
237 case DPO_PROTO_IP4:
238 case DPO_PROTO_IP6:
239 fib_api_next_hop_decode(in, &out->frp_addr);
240 out->frp_sw_if_index = ntohl(in->sw_if_index);
241 out->frp_rpf_id = ntohl(in->rpf_id);
242
243 if (0 == out->frp_rpf_id)
244 {
245 /* allow 0 to be an unset value on the API */
246 out->frp_rpf_id = ~0;
247 }
248
249 if (~0 != out->frp_rpf_id)
250 {
251 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
252 }
253
254 if (~0 == out->frp_sw_if_index)
255 {
256 /* recursive or deag, validate the next-hop FIB */
257 if (~0 != out->frp_rpf_id)
258 {
259 rv = fib_api_mtable_id_decode(
260 dpo_proto_to_fib(out->frp_proto),
261 ntohl(in->table_id),
262 &out->frp_fib_index);
263 }
264 else
265 {
266 rv = fib_api_table_id_decode(
267 dpo_proto_to_fib(out->frp_proto),
268 ntohl(in->table_id),
269 &out->frp_fib_index);
270 }
271 if (0 != rv)
272 {
273 return (rv);
274 }
275 }
276 else
277 {
278 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
279 out->frp_sw_if_index))
280 {
281 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
282 }
283 }
284
285 if (ip46_address_is_zero(&out->frp_addr))
286 {
287 if (~0 == out->frp_sw_if_index &&
288 ~0 != out->frp_fib_index)
289 {
290 out->frp_flags |= FIB_ROUTE_PATH_DEAG;
291 }
292 }
293
294 break;
295 case DPO_PROTO_MPLS:
296 out->frp_local_label = ntohl (in->nh.via_label);
297 out->frp_eos = MPLS_NON_EOS;
298 out->frp_sw_if_index = ~0;
299 break;
300 case DPO_PROTO_BIER:
301 out->frp_sw_if_index = ntohl(in->sw_if_index);
302 out->frp_rpf_id = ntohl(in->rpf_id);
303
304 if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
305 {
306 fib_api_next_hop_decode(in, &out->frp_addr);
307
308 if (ip46_address_is_zero(&out->frp_addr))
309 {
310 index_t bdti;
311
312 bdti = bier_disp_table_find(ntohl(in->table_id));
313
314 if (INDEX_INVALID != bdti)
315 {
316 out->frp_fib_index = bdti;
317 }
318 else
319 {
320 return (VNET_API_ERROR_NO_SUCH_FIB);
321 }
322 }
323 }
324 break;
325 case DPO_PROTO_ETHERNET:
326 out->frp_sw_if_index = ntohl(in->sw_if_index);
327 break;
328 case DPO_PROTO_NSH:
329 break;
330 }
Neale Ranns81458422018-03-12 06:59:36 -0700331 }
332
333 n_labels = in->n_labels;
Neale Ranns097fa662018-05-01 05:17:55 -0700334 if (n_labels != 0)
Neale Ranns81458422018-03-12 06:59:36 -0700335 {
336 vec_validate (out->frp_label_stack, n_labels - 1);
337 for (ii = 0; ii < n_labels; ii++)
338 {
339 out->frp_label_stack[ii].fml_value =
340 ntohl(in->label_stack[ii].label);
341 out->frp_label_stack[ii].fml_ttl =
342 in->label_stack[ii].ttl;
343 out->frp_label_stack[ii].fml_exp =
344 in->label_stack[ii].exp;
345 out->frp_label_stack[ii].fml_mode =
346 (in->label_stack[ii].is_uniform ?
347 FIB_MPLS_LSP_MODE_UNIFORM :
348 FIB_MPLS_LSP_MODE_PIPE);
349 }
350 }
351
Neale Ranns097fa662018-05-01 05:17:55 -0700352 return (0);
Neale Ranns81458422018-03-12 06:59:36 -0700353}
354
355void
Neale Ranns097fa662018-05-01 05:17:55 -0700356fib_api_path_encode (const fib_route_path_t * rpath,
Neale Ranns81458422018-03-12 06:59:36 -0700357 vl_api_fib_path_t *out)
358{
Neale Ranns097fa662018-05-01 05:17:55 -0700359 memset (out, 0, sizeof (*out));
Neale Ranns775f73c2018-12-20 03:01:49 -0800360
Neale Ranns097fa662018-05-01 05:17:55 -0700361 out->weight = rpath->frp_weight;
362 out->preference = rpath->frp_preference;
363 out->sw_if_index = htonl (rpath->frp_sw_if_index);
364 out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
365 out->rpf_id = rpath->frp_rpf_id;
366 fib_api_next_hop_encode (rpath, out);
367
368 if (0 != rpath->frp_fib_index)
Neale Ranns81458422018-03-12 06:59:36 -0700369 {
Neale Ranns097fa662018-05-01 05:17:55 -0700370 if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
371 (DPO_PROTO_IP4 == rpath->frp_proto))
Neale Ranns81458422018-03-12 06:59:36 -0700372 {
Neale Ranns097fa662018-05-01 05:17:55 -0700373 if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
374 {
375 out->table_id = htonl (mfib_table_get_table_id(
376 rpath->frp_fib_index,
377 dpo_proto_to_fib(rpath->frp_proto)));
378 }
379 else
380 {
381 out->table_id = htonl (fib_table_get_table_id(
382 rpath->frp_fib_index,
383 dpo_proto_to_fib(rpath->frp_proto)));
384 }
Neale Ranns81458422018-03-12 06:59:36 -0700385 }
Neale Ranns097fa662018-05-01 05:17:55 -0700386 }
387
388 if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
389 {
390 out->type = FIB_API_PATH_TYPE_DVR;
391 }
392 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
393 {
394 out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
395 }
396 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
397 {
398 out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
399 }
400 else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
401 {
402 out->type = FIB_API_PATH_TYPE_LOCAL;
403 }
404 else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
405 {
406 out->type = FIB_API_PATH_TYPE_DROP;
407 }
408 else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
409 {
410 out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
411 out->nh.obj_id = rpath->frp_udp_encap_id;
412 }
413 else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
414 {
415 out->type = FIB_API_PATH_TYPE_BIER_IMP;
416 out->nh.obj_id = rpath->frp_bier_imp;
417 }
418 else
419 {
420 out->type = FIB_API_PATH_TYPE_NORMAL;
421 }
422 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
423 {
424 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
425 }
426 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
427 {
428 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
429 }
430
431 out->flags = htonl (out->flags);
432 out->type = htonl (out->type);
433 out->proto = htonl (out->proto);
434
435 if (rpath->frp_label_stack)
436 {
437 int ii;
438
439 for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
440 {
441 out->label_stack[ii].label =
442 htonl(rpath->frp_label_stack[ii].fml_value);
443 out->label_stack[ii].ttl =
444 rpath->frp_label_stack[ii].fml_ttl;
445 out->label_stack[ii].exp =
446 rpath->frp_label_stack[ii].fml_exp;
447 }
448 out->n_labels = ii;
449 }
450}
451
Neale Rannsc2ac2352019-07-02 14:33:29 +0000452int
Neale Ranns097fa662018-05-01 05:17:55 -0700453fib_api_route_add_del (u8 is_add,
454 u8 is_multipath,
455 u32 fib_index,
456 const fib_prefix_t * prefix,
457 fib_entry_flag_t entry_flags,
458 fib_route_path_t *rpaths)
459{
Neale Rannsc2ac2352019-07-02 14:33:29 +0000460 if (is_multipath)
Neale Ranns097fa662018-05-01 05:17:55 -0700461 {
Neale Rannsc2ac2352019-07-02 14:33:29 +0000462 if (vec_len(rpaths) == 0)
463 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
464
465 /* Iterative path add/remove */
466 if (is_add)
467 fib_table_entry_path_add2 (fib_index,
468 prefix,
469 FIB_SOURCE_API,
470 entry_flags,
471 rpaths);
472 else
473 fib_table_entry_path_remove2 (fib_index,
474 prefix,
475 FIB_SOURCE_API,
476 rpaths);
Neale Ranns097fa662018-05-01 05:17:55 -0700477 }
Neale Rannsc2ac2352019-07-02 14:33:29 +0000478 else
Neale Ranns097fa662018-05-01 05:17:55 -0700479 {
Neale Rannsc2ac2352019-07-02 14:33:29 +0000480 if (is_add)
481 {
482 if (vec_len(rpaths) == 0)
483 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
484
485 /* path replacement */
486 fib_table_entry_update (fib_index,
487 prefix,
488 FIB_SOURCE_API,
489 entry_flags,
490 rpaths);
491 }
492 else
493 /* entry delete */
494 fib_table_entry_delete (fib_index,
495 prefix,
496 FIB_SOURCE_API);
Neale Ranns097fa662018-05-01 05:17:55 -0700497 }
Neale Rannsc2ac2352019-07-02 14:33:29 +0000498
499 return (0);
Neale Ranns097fa662018-05-01 05:17:55 -0700500}
501
502u8*
503format_vl_api_fib_path (u8 * s, va_list * args)
504{
505 const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
506
507 s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
508 switch (clib_net_to_host_u32(path->proto))
509 {
510 case FIB_API_PATH_NH_PROTO_IP4:
511 s = format (s, " %U", format_vl_api_address_union,
512 &path->nh.address, ADDRESS_IP4);
513 break;
514 case FIB_API_PATH_NH_PROTO_IP6:
515 s = format (s, " %U", format_vl_api_address_union,
516 &path->nh.address, ADDRESS_IP6);
Neale Ranns81458422018-03-12 06:59:36 -0700517 break;
518 default:
519 break;
520 }
Neale Ranns097fa662018-05-01 05:17:55 -0700521 s = format (s, " weight %d", path->weight);
522 s = format (s, " preference %d", path->preference);
523 s = format (s, " type %d", ntohl(path->type));
524 s = format (s, " proto %d", ntohl(path->proto));
525 s = format (s, " flags %d", ntohl(path->flags));
526 s = format (s, " n_labels %d", ntohl(path->n_labels));
527 s = format (s, " table-id %d", ntohl(path->table_id));
528 s = format (s, " rpf-id %d", ntohl(path->rpf_id));
Neale Ranns81458422018-03-12 06:59:36 -0700529
Neale Ranns097fa662018-05-01 05:17:55 -0700530 return (s);
Neale Ranns81458422018-03-12 06:59:36 -0700531}
Neale Ranns2303cb12018-02-21 04:57:17 -0800532
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700533fib_protocol_t
534fib_proto_from_api_address_family (int af)
535{
536 switch (clib_net_to_host_u32 (af))
537 {
538 case ADDRESS_IP4:
539 return (FIB_PROTOCOL_IP4);
540 case ADDRESS_IP6:
541 return (FIB_PROTOCOL_IP6);
542 }
543
544 ASSERT(0);
545 return (FIB_PROTOCOL_IP4);
546}
547
548int
549fib_proto_to_api_address_family (fib_protocol_t fproto)
550{
551 switch (fproto)
552 {
553 case FIB_PROTOCOL_IP4:
554 return (clib_net_to_host_u32 (ADDRESS_IP4));
555 case FIB_PROTOCOL_IP6:
556 return (clib_net_to_host_u32 (ADDRESS_IP6));
557 default:
558 break;
559 }
560
561 ASSERT(0);
562 return (clib_net_to_host_u32 (ADDRESS_IP4));
563}