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) ?
96 format_fib_route_path_flags (u8 *s, va_list *ap)
98 fib_route_path_flags_t flags = va_arg (*ap, fib_route_path_flags_t);
100 if (flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
101 s = format (s, "via-host");
102 if (flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
103 s = format (s, "via-attached,");
104 if (flags & FIB_ROUTE_PATH_LOCAL)
105 s = format (s, "local,");
106 if (flags & FIB_ROUTE_PATH_ATTACHED)
107 s = format (s, "attached,");
108 if (flags & FIB_ROUTE_PATH_DROP)
109 s = format (s, "drop,");
110 if (flags & FIB_ROUTE_PATH_EXCLUSIVE)
111 s = format (s, "exclusive,");
112 if (flags & FIB_ROUTE_PATH_INTF_RX)
113 s = format (s, "intf-rx,");
114 if (flags & FIB_ROUTE_PATH_RPF_ID)
115 s = format (s, "rpf-id,");
116 if (flags & FIB_ROUTE_PATH_SOURCE_LOOKUP)
117 s = format (s, "src-lkup,");
118 if (flags & FIB_ROUTE_PATH_UDP_ENCAP)
119 s = format (s, "udp-encap,");
120 if (flags & FIB_ROUTE_PATH_BIER_FMASK)
121 s = format (s, "bier-fmask,");
122 if (flags & FIB_ROUTE_PATH_BIER_TABLE)
123 s = format (s, "bier-table,");
124 if (flags & FIB_ROUTE_PATH_BIER_IMP)
125 s = format (s, "bier-imp,");
126 if (flags & FIB_ROUTE_PATH_DEAG)
127 s = format (s, "deag,");
128 if (flags & FIB_ROUTE_PATH_DVR)
129 s = format (s, "dvr,");
130 if (flags & FIB_ROUTE_PATH_ICMP_UNREACH)
131 s = format (s, "imcp-unreach,");
132 if (flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
133 s = format (s, "icmp-prohibit,");
134 if (flags & FIB_ROUTE_PATH_CLASSIFY)
135 s = format (s, "classify,");
136 if (flags & FIB_ROUTE_PATH_POP_PW_CW)
137 s = format (s, "pop-pw-cw,");
143 format_fib_route_path (u8 *s, va_list *ap)
145 fib_route_path_t *rpath = va_arg (*ap, fib_route_path_t*);
147 s = format (s, "%U %U, %U, [%U]",
148 format_dpo_proto, rpath->frp_proto,
149 format_ip46_address, &rpath->frp_addr, IP46_TYPE_ANY,
150 format_vnet_sw_if_index_name, vnet_get_main (),
151 rpath->frp_sw_if_index,
152 format_fib_route_path_flags, rpath->frp_flags);
158 fib_prefix_from_mpls_label (mpls_label_t label,
162 pfx->fp_proto = FIB_PROTOCOL_MPLS;
164 pfx->fp_label = label;
166 pfx->___fp___pad = 0;
170 fib_prefix_copy (fib_prefix_t *dst,
171 const fib_prefix_t *src)
173 clib_memcpy(dst, src, sizeof(*dst));
177 fib_prefix_cmp (const fib_prefix_t *p1,
178 const fib_prefix_t *p2)
182 res = (p1->fp_proto - p2->fp_proto);
186 switch (p1->fp_proto)
188 case FIB_PROTOCOL_IP4:
189 case FIB_PROTOCOL_IP6:
190 res = (p1->fp_len - p2->fp_len);
194 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
197 case FIB_PROTOCOL_MPLS:
198 res = (p1->fp_label - p2->fp_label);
202 res = (p1->fp_eos - p2->fp_eos);
212 fib_prefix_is_cover (const fib_prefix_t *p1,
213 const fib_prefix_t *p2)
215 switch (p1->fp_proto)
217 case FIB_PROTOCOL_IP4:
218 return (ip4_destination_matches_route(&ip4_main,
222 case FIB_PROTOCOL_IP6:
223 return (ip6_destination_matches_route(&ip6_main,
227 case FIB_PROTOCOL_MPLS:
234 fib_prefix_get_host_length (fib_protocol_t proto)
238 case FIB_PROTOCOL_IP4:
240 case FIB_PROTOCOL_IP6:
242 case FIB_PROTOCOL_MPLS:
249 fib_prefix_is_host (const fib_prefix_t *prefix)
251 switch (prefix->fp_proto)
253 case FIB_PROTOCOL_IP4:
254 return (prefix->fp_len == 32);
255 case FIB_PROTOCOL_IP6:
256 return (prefix->fp_len == 128);
257 case FIB_PROTOCOL_MPLS:
264 format_fib_prefix (u8 * s, va_list * args)
266 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
269 * protocol specific so it prints ::/0 correctly.
271 switch (fp->fp_proto)
273 case FIB_PROTOCOL_IP6:
275 ip6_address_t p6 = fp->fp_addr.ip6;
277 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
278 s = format (s, "%U", format_ip6_address, &p6);
281 case FIB_PROTOCOL_IP4:
283 ip4_address_t p4 = fp->fp_addr.ip4;
284 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
286 s = format (s, "%U", format_ip4_address, &p4);
289 case FIB_PROTOCOL_MPLS:
290 s = format (s, "%U:%U",
291 format_mpls_unicast_label, fp->fp_label,
292 format_mpls_eos_bit, fp->fp_eos);
295 s = format (s, "/%d", fp->fp_len);
301 fib_route_path_cmp (const fib_route_path_t *rpath1,
302 const fib_route_path_t *rpath2)
306 res = ip46_address_cmp(&rpath1->frp_addr,
309 if (0 != res) return (res);
311 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
313 if (0 != res) return (res);
315 if (ip46_address_is_zero(&rpath1->frp_addr))
317 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
324 fib_proto_to_dpo (fib_protocol_t fib_proto)
328 case FIB_PROTOCOL_IP6:
329 return (DPO_PROTO_IP6);
330 case FIB_PROTOCOL_IP4:
331 return (DPO_PROTO_IP4);
332 case FIB_PROTOCOL_MPLS:
333 return (DPO_PROTO_MPLS);
340 dpo_proto_to_fib (dpo_proto_t dpo_proto)
345 return (FIB_PROTOCOL_IP6);
347 return (FIB_PROTOCOL_IP4);
349 return (FIB_PROTOCOL_MPLS);
358 fib_proto_to_link (fib_protocol_t proto)
362 case FIB_PROTOCOL_IP4:
363 return (VNET_LINK_IP4);
364 case FIB_PROTOCOL_IP6:
365 return (VNET_LINK_IP6);
366 case FIB_PROTOCOL_MPLS:
367 return (VNET_LINK_MPLS);
374 fib_proto_to_ip46 (fib_protocol_t fproto)
378 case FIB_PROTOCOL_IP4:
379 return (IP46_TYPE_IP4);
380 case FIB_PROTOCOL_IP6:
381 return (IP46_TYPE_IP6);
382 case FIB_PROTOCOL_MPLS:
383 return (IP46_TYPE_ANY);
386 return (IP46_TYPE_ANY);
390 fib_proto_from_ip46 (ip46_type_t iproto)
395 return FIB_PROTOCOL_IP4;
397 return FIB_PROTOCOL_IP6;
400 return FIB_PROTOCOL_IP4;
404 return FIB_PROTOCOL_IP4;
407 fib_forward_chain_type_t
408 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
413 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
415 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
417 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
418 case DPO_PROTO_ETHERNET:
419 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
421 return (FIB_FORW_CHAIN_TYPE_NSH);
423 return (FIB_FORW_CHAIN_TYPE_BIER);
426 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
429 fib_forward_chain_type_t
430 fib_forw_chain_type_from_fib_proto (fib_protocol_t proto)
434 case FIB_PROTOCOL_IP4:
435 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
436 case FIB_PROTOCOL_IP6:
437 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
438 case FIB_PROTOCOL_MPLS:
439 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
442 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
446 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
450 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
451 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
452 return (VNET_LINK_IP4);
453 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
454 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
455 return (VNET_LINK_IP6);
456 case FIB_FORW_CHAIN_TYPE_ETHERNET:
457 return (VNET_LINK_ETHERNET);
458 case FIB_FORW_CHAIN_TYPE_NSH:
459 return (VNET_LINK_NSH);
460 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
461 case FIB_FORW_CHAIN_TYPE_BIER:
463 * insufficient information to to convert
467 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
468 return (VNET_LINK_MPLS);
470 return (VNET_LINK_IP4);
473 fib_forward_chain_type_t
474 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
479 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
481 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
483 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
484 case VNET_LINK_ETHERNET:
485 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
487 return (FIB_FORW_CHAIN_TYPE_NSH);
493 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
497 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
501 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
502 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
503 return (DPO_PROTO_IP4);
504 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
505 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
506 return (DPO_PROTO_IP6);
507 case FIB_FORW_CHAIN_TYPE_ETHERNET:
508 return (DPO_PROTO_ETHERNET);
509 case FIB_FORW_CHAIN_TYPE_NSH:
510 return (DPO_PROTO_NSH);
511 case FIB_FORW_CHAIN_TYPE_BIER:
512 return (DPO_PROTO_BIER);
513 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
514 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
515 return (DPO_PROTO_MPLS);
517 return (DPO_PROTO_IP4);
521 unformat_fib_route_path (unformat_input_t * input, va_list * args)
523 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
524 dpo_proto_t *payload_proto = va_arg (*args, void*);
525 u32 weight, preference, udp_encap_id, fi;
526 mpls_label_t out_label;
529 vnm = vnet_get_main ();
530 clib_memset(rpath, 0, sizeof(*rpath));
531 rpath->frp_weight = 1;
532 rpath->frp_sw_if_index = ~0;
534 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
536 if (unformat (input, "%U %U",
537 unformat_ip4_address,
538 &rpath->frp_addr.ip4,
539 unformat_vnet_sw_interface, vnm,
540 &rpath->frp_sw_if_index))
542 rpath->frp_proto = DPO_PROTO_IP4;
544 else if (unformat (input, "%U %U",
545 unformat_ip6_address,
546 &rpath->frp_addr.ip6,
547 unformat_vnet_sw_interface, vnm,
548 &rpath->frp_sw_if_index))
550 rpath->frp_proto = DPO_PROTO_IP6;
552 else if (unformat (input, "weight %u", &weight))
554 rpath->frp_weight = weight;
556 else if (unformat (input, "preference %u", &preference))
558 rpath->frp_preference = preference;
560 else if (unformat (input, "%U next-hop-table %d",
561 unformat_ip4_address,
562 &rpath->frp_addr.ip4,
563 &rpath->frp_fib_index))
565 rpath->frp_sw_if_index = ~0;
566 rpath->frp_proto = DPO_PROTO_IP4;
569 * the user enter table-ids, convert to index
571 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
574 rpath->frp_fib_index = fi;
576 else if (unformat (input, "%U next-hop-table %d",
577 unformat_ip6_address,
578 &rpath->frp_addr.ip6,
579 &rpath->frp_fib_index))
581 rpath->frp_sw_if_index = ~0;
582 rpath->frp_proto = DPO_PROTO_IP6;
583 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
586 rpath->frp_fib_index = fi;
588 else if (unformat (input, "%U",
589 unformat_ip4_address,
590 &rpath->frp_addr.ip4))
593 * the recursive next-hops are by default in the default table
595 rpath->frp_fib_index = 0;
596 rpath->frp_sw_if_index = ~0;
597 rpath->frp_proto = DPO_PROTO_IP4;
599 else if (unformat (input, "%U",
600 unformat_ip6_address,
601 &rpath->frp_addr.ip6))
603 rpath->frp_fib_index = 0;
604 rpath->frp_sw_if_index = ~0;
605 rpath->frp_proto = DPO_PROTO_IP6;
607 else if (unformat (input, "udp-encap %d", &udp_encap_id))
609 rpath->frp_udp_encap_id = udp_encap_id;
610 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
611 rpath->frp_proto = *payload_proto;
613 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
615 rpath->frp_proto = *payload_proto;
616 rpath->frp_sw_if_index = ~0;
617 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
619 else if (unformat (input, "resolve-via-host"))
621 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
623 else if (unformat (input, "resolve-via-attached"))
625 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
627 else if (unformat (input, "pop-pw-cw"))
629 rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
631 else if (unformat (input,
632 "ip4-lookup-in-table %d",
633 &rpath->frp_fib_index))
635 rpath->frp_proto = DPO_PROTO_IP4;
636 *payload_proto = DPO_PROTO_IP4;
637 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
640 rpath->frp_fib_index = fi;
642 else if (unformat (input,
643 "ip6-lookup-in-table %d",
644 &rpath->frp_fib_index))
646 rpath->frp_proto = DPO_PROTO_IP6;
647 *payload_proto = DPO_PROTO_IP6;
648 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
651 rpath->frp_fib_index = fi;
653 else if (unformat (input,
654 "mpls-lookup-in-table %d",
655 &rpath->frp_fib_index))
657 rpath->frp_proto = DPO_PROTO_MPLS;
658 *payload_proto = DPO_PROTO_MPLS;
659 fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
662 rpath->frp_fib_index = fi;
664 else if (unformat (input, "src-lookup"))
666 rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
668 else if (unformat (input,
670 unformat_vnet_sw_interface, vnm,
671 &rpath->frp_sw_if_index))
673 rpath->frp_proto = DPO_PROTO_ETHERNET;
674 *payload_proto = DPO_PROTO_ETHERNET;
675 rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
677 else if (unformat (input, "via-label %U",
678 unformat_mpls_unicast_label,
679 &rpath->frp_local_label))
681 rpath->frp_eos = MPLS_NON_EOS;
682 rpath->frp_proto = DPO_PROTO_MPLS;
683 rpath->frp_sw_if_index = ~0;
685 else if (unformat (input, "rx-ip4 %U",
686 unformat_vnet_sw_interface, vnm,
687 &rpath->frp_sw_if_index))
689 rpath->frp_proto = DPO_PROTO_IP4;
690 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
692 else if (unformat (input, "local"))
694 clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
695 rpath->frp_sw_if_index = ~0;
696 rpath->frp_weight = 1;
697 rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
699 else if (unformat (input, "%U",
700 unformat_mfib_itf_flags, &rpath->frp_mitf_flags))
702 else if (unformat (input, "out-labels"))
704 while (unformat (input, "%U",
705 unformat_mpls_unicast_label, &out_label))
707 fib_mpls_label_t fml = {
708 .fml_value = out_label,
710 vec_add1(rpath->frp_label_stack, fml);
713 else if (unformat (input, "%U",
714 unformat_vnet_sw_interface, vnm,
715 &rpath->frp_sw_if_index))
717 rpath->frp_proto = *payload_proto;
719 else if (unformat (input, "via"))
721 /* new path, back up and return */
722 unformat_put_input (input);
723 unformat_put_input (input);
724 unformat_put_input (input);
725 unformat_put_input (input);