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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/ip/ip.h>
18 #include <vnet/fib/fib_types.h>
19 #include <vnet/fib/fib_internal.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/mfib/mfib_types.h>
22 #include <vnet/mpls/mpls.h>
25 * arrays of protocol and link names
27 static const char* fib_protocol_names[] = FIB_PROTOCOLS;
28 static const char* vnet_link_names[] = VNET_LINKS;
29 static const char* fib_forw_chain_names[] = FIB_FORW_CHAINS;
30 static const char* fib_mpls_lsp_mode_names[] = FIB_MPLS_LSP_MODES;
33 format_fib_protocol (u8 * s, va_list * ap)
35 fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
37 return (format (s, "%s", fib_protocol_names[proto]));
41 format_vnet_link (u8 * s, va_list * ap)
43 vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
45 return (format (s, "%s", vnet_link_names[link]));
49 format_fib_forw_chain_type (u8 * s, va_list * args)
51 fib_forward_chain_type_t fct = va_arg(*args, int);
53 return (format (s, "%s", fib_forw_chain_names[fct]));
57 format_fib_mpls_lsp_mode(u8 *s, va_list *ap)
59 fib_mpls_lsp_mode_t mode = va_arg(*ap, int);
61 return (format (s, "%s", fib_mpls_lsp_mode_names[mode]));
65 format_fib_mpls_label (u8 *s, va_list *ap)
67 fib_mpls_label_t *label = va_arg(*ap, fib_mpls_label_t *);
69 s = format(s, "%U %U ttl:%d exp:%d",
70 format_mpls_unicast_label,
72 format_fib_mpls_lsp_mode,
81 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
84 ASSERT(!ip46_address_is_zero(addr));
86 pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
89 pfx->fp_len = ((ip46_address_is_ip4(addr) ?
95 fib_prefix_from_mpls_label (mpls_label_t label,
99 pfx->fp_proto = FIB_PROTOCOL_MPLS;
101 pfx->fp_label = label;
106 fib_prefix_copy (fib_prefix_t *dst,
107 const fib_prefix_t *src)
109 clib_memcpy(dst, src, sizeof(*dst));
113 fib_prefix_cmp (const fib_prefix_t *p1,
114 const fib_prefix_t *p2)
118 res = (p1->fp_proto - p2->fp_proto);
122 switch (p1->fp_proto)
124 case FIB_PROTOCOL_IP4:
125 case FIB_PROTOCOL_IP6:
126 res = (p1->fp_len - p2->fp_len);
130 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
133 case FIB_PROTOCOL_MPLS:
134 res = (p1->fp_label - p2->fp_label);
138 res = (p1->fp_eos - p2->fp_eos);
148 fib_prefix_is_cover (const fib_prefix_t *p1,
149 const fib_prefix_t *p2)
151 switch (p1->fp_proto)
153 case FIB_PROTOCOL_IP4:
154 return (ip4_destination_matches_route(&ip4_main,
158 case FIB_PROTOCOL_IP6:
159 return (ip6_destination_matches_route(&ip6_main,
163 case FIB_PROTOCOL_MPLS:
170 fib_prefix_get_host_length (fib_protocol_t proto)
174 case FIB_PROTOCOL_IP4:
176 case FIB_PROTOCOL_IP6:
178 case FIB_PROTOCOL_MPLS:
185 fib_prefix_is_host (const fib_prefix_t *prefix)
187 switch (prefix->fp_proto)
189 case FIB_PROTOCOL_IP4:
190 return (prefix->fp_len == 32);
191 case FIB_PROTOCOL_IP6:
192 return (prefix->fp_len == 128);
193 case FIB_PROTOCOL_MPLS:
200 format_fib_prefix (u8 * s, va_list * args)
202 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
205 * protocol specific so it prints ::/0 correctly.
207 switch (fp->fp_proto)
209 case FIB_PROTOCOL_IP6:
211 ip6_address_t p6 = fp->fp_addr.ip6;
213 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
214 s = format (s, "%U", format_ip6_address, &p6);
217 case FIB_PROTOCOL_IP4:
219 ip4_address_t p4 = fp->fp_addr.ip4;
220 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
222 s = format (s, "%U", format_ip4_address, &p4);
225 case FIB_PROTOCOL_MPLS:
226 s = format (s, "%U:%U",
227 format_mpls_unicast_label, fp->fp_label,
228 format_mpls_eos_bit, fp->fp_eos);
231 s = format (s, "/%d", fp->fp_len);
237 fib_route_path_cmp (const fib_route_path_t *rpath1,
238 const fib_route_path_t *rpath2)
242 res = ip46_address_cmp(&rpath1->frp_addr,
245 if (0 != res) return (res);
247 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
249 if (0 != res) return (res);
251 if (ip46_address_is_zero(&rpath1->frp_addr))
253 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
260 fib_proto_to_dpo (fib_protocol_t fib_proto)
264 case FIB_PROTOCOL_IP6:
265 return (DPO_PROTO_IP6);
266 case FIB_PROTOCOL_IP4:
267 return (DPO_PROTO_IP4);
268 case FIB_PROTOCOL_MPLS:
269 return (DPO_PROTO_MPLS);
276 dpo_proto_to_fib (dpo_proto_t dpo_proto)
281 return (FIB_PROTOCOL_IP6);
283 return (FIB_PROTOCOL_IP4);
285 return (FIB_PROTOCOL_MPLS);
294 fib_proto_to_link (fib_protocol_t proto)
298 case FIB_PROTOCOL_IP4:
299 return (VNET_LINK_IP4);
300 case FIB_PROTOCOL_IP6:
301 return (VNET_LINK_IP6);
302 case FIB_PROTOCOL_MPLS:
303 return (VNET_LINK_MPLS);
310 fib_proto_to_ip46 (fib_protocol_t fproto)
314 case FIB_PROTOCOL_IP4:
315 return (IP46_TYPE_IP4);
316 case FIB_PROTOCOL_IP6:
317 return (IP46_TYPE_IP6);
318 case FIB_PROTOCOL_MPLS:
319 return (IP46_TYPE_ANY);
322 return (IP46_TYPE_ANY);
326 fib_proto_from_ip46 (ip46_type_t iproto)
331 return FIB_PROTOCOL_IP4;
333 return FIB_PROTOCOL_IP6;
336 return FIB_PROTOCOL_IP4;
340 return FIB_PROTOCOL_IP4;
343 fib_forward_chain_type_t
344 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
349 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
351 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
353 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
354 case DPO_PROTO_ETHERNET:
355 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
357 return (FIB_FORW_CHAIN_TYPE_NSH);
359 return (FIB_FORW_CHAIN_TYPE_BIER);
362 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
365 fib_forward_chain_type_t
366 fib_forw_chain_type_from_fib_proto (fib_protocol_t proto)
370 case FIB_PROTOCOL_IP4:
371 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
372 case FIB_PROTOCOL_IP6:
373 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
374 case FIB_PROTOCOL_MPLS:
375 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
378 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
382 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
386 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
387 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
388 return (VNET_LINK_IP4);
389 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
390 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
391 return (VNET_LINK_IP6);
392 case FIB_FORW_CHAIN_TYPE_ETHERNET:
393 return (VNET_LINK_ETHERNET);
394 case FIB_FORW_CHAIN_TYPE_NSH:
395 return (VNET_LINK_NSH);
396 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
397 case FIB_FORW_CHAIN_TYPE_BIER:
399 * insufficient information to to convert
403 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
404 return (VNET_LINK_MPLS);
406 return (VNET_LINK_IP4);
409 fib_forward_chain_type_t
410 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
415 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
417 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
419 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
420 case VNET_LINK_ETHERNET:
421 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
423 return (FIB_FORW_CHAIN_TYPE_NSH);
429 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
433 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
437 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
438 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
439 return (DPO_PROTO_IP4);
440 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
441 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
442 return (DPO_PROTO_IP6);
443 case FIB_FORW_CHAIN_TYPE_ETHERNET:
444 return (DPO_PROTO_ETHERNET);
445 case FIB_FORW_CHAIN_TYPE_NSH:
446 return (DPO_PROTO_NSH);
447 case FIB_FORW_CHAIN_TYPE_BIER:
448 return (DPO_PROTO_BIER);
449 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
450 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
451 return (DPO_PROTO_MPLS);
453 return (DPO_PROTO_IP4);
457 unformat_fib_route_path (unformat_input_t * input, va_list * args)
459 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
460 dpo_proto_t *payload_proto = va_arg (*args, void*);
461 u32 weight, preference, udp_encap_id, fi;
462 mpls_label_t out_label;
465 vnm = vnet_get_main ();
466 clib_memset(rpath, 0, sizeof(*rpath));
467 rpath->frp_weight = 1;
468 rpath->frp_sw_if_index = ~0;
470 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
472 if (unformat (input, "%U %U",
473 unformat_ip4_address,
474 &rpath->frp_addr.ip4,
475 unformat_vnet_sw_interface, vnm,
476 &rpath->frp_sw_if_index))
478 rpath->frp_proto = DPO_PROTO_IP4;
480 else if (unformat (input, "%U %U",
481 unformat_ip6_address,
482 &rpath->frp_addr.ip6,
483 unformat_vnet_sw_interface, vnm,
484 &rpath->frp_sw_if_index))
486 rpath->frp_proto = DPO_PROTO_IP6;
488 else if (unformat (input, "weight %u", &weight))
490 rpath->frp_weight = weight;
492 else if (unformat (input, "preference %u", &preference))
494 rpath->frp_preference = preference;
496 else if (unformat (input, "%U next-hop-table %d",
497 unformat_ip4_address,
498 &rpath->frp_addr.ip4,
499 &rpath->frp_fib_index))
501 rpath->frp_sw_if_index = ~0;
502 rpath->frp_proto = DPO_PROTO_IP4;
505 * the user enter table-ids, convert to index
507 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
510 rpath->frp_fib_index = fi;
512 else if (unformat (input, "%U next-hop-table %d",
513 unformat_ip6_address,
514 &rpath->frp_addr.ip6,
515 &rpath->frp_fib_index))
517 rpath->frp_sw_if_index = ~0;
518 rpath->frp_proto = DPO_PROTO_IP6;
519 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
522 rpath->frp_fib_index = fi;
524 else if (unformat (input, "%U",
525 unformat_ip4_address,
526 &rpath->frp_addr.ip4))
529 * the recursive next-hops are by default in the default table
531 rpath->frp_fib_index = 0;
532 rpath->frp_sw_if_index = ~0;
533 rpath->frp_proto = DPO_PROTO_IP4;
535 else if (unformat (input, "%U",
536 unformat_ip6_address,
537 &rpath->frp_addr.ip6))
539 rpath->frp_fib_index = 0;
540 rpath->frp_sw_if_index = ~0;
541 rpath->frp_proto = DPO_PROTO_IP6;
543 else if (unformat (input, "udp-encap %d", &udp_encap_id))
545 rpath->frp_udp_encap_id = udp_encap_id;
546 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
547 rpath->frp_proto = *payload_proto;
549 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
551 rpath->frp_proto = *payload_proto;
552 rpath->frp_sw_if_index = ~0;
553 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
555 else if (unformat (input, "resolve-via-host"))
557 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
559 else if (unformat (input, "resolve-via-attached"))
561 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
563 else if (unformat (input, "pop-pw-cw"))
565 rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
567 else if (unformat (input,
568 "ip4-lookup-in-table %d",
569 &rpath->frp_fib_index))
571 rpath->frp_proto = DPO_PROTO_IP4;
572 *payload_proto = DPO_PROTO_IP4;
573 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
576 rpath->frp_fib_index = fi;
578 else if (unformat (input,
579 "ip6-lookup-in-table %d",
580 &rpath->frp_fib_index))
582 rpath->frp_proto = DPO_PROTO_IP6;
583 *payload_proto = DPO_PROTO_IP6;
584 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
587 rpath->frp_fib_index = fi;
589 else if (unformat (input,
590 "mpls-lookup-in-table %d",
591 &rpath->frp_fib_index))
593 rpath->frp_proto = DPO_PROTO_MPLS;
594 *payload_proto = DPO_PROTO_MPLS;
595 fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
598 rpath->frp_fib_index = fi;
600 else if (unformat (input, "src-lookup"))
602 rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
604 else if (unformat (input,
606 unformat_vnet_sw_interface, vnm,
607 &rpath->frp_sw_if_index))
609 rpath->frp_proto = DPO_PROTO_ETHERNET;
610 *payload_proto = DPO_PROTO_ETHERNET;
611 rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
613 else if (unformat (input, "via-label %U",
614 unformat_mpls_unicast_label,
615 &rpath->frp_local_label))
617 rpath->frp_eos = MPLS_NON_EOS;
618 rpath->frp_proto = DPO_PROTO_MPLS;
619 rpath->frp_sw_if_index = ~0;
621 else if (unformat (input, "rx-ip4 %U",
622 unformat_vnet_sw_interface, vnm,
623 &rpath->frp_sw_if_index))
625 rpath->frp_proto = DPO_PROTO_IP4;
626 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
628 else if (unformat (input, "local"))
630 clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
631 rpath->frp_sw_if_index = ~0;
632 rpath->frp_weight = 1;
633 rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
635 else if (unformat (input, "%U",
636 unformat_mfib_itf_flags, &rpath->frp_mitf_flags))
638 else if (unformat (input, "out-labels"))
640 while (unformat (input, "%U",
641 unformat_mpls_unicast_label, &out_label))
643 fib_mpls_label_t fml = {
644 .fml_value = out_label,
646 vec_add1(rpath->frp_label_stack, fml);
649 else if (unformat (input, "%U",
650 unformat_vnet_sw_interface, vnm,
651 &rpath->frp_sw_if_index))
653 rpath->frp_proto = *payload_proto;
655 else if (unformat (input, "via"))
657 /* new path, back up and return */
658 unformat_put_input (input);
659 unformat_put_input (input);
660 unformat_put_input (input);
661 unformat_put_input (input);