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