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 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_is_host (const fib_prefix_t *prefix)
172 switch (prefix->fp_proto)
174 case FIB_PROTOCOL_IP4:
175 return (prefix->fp_len == 32);
176 case FIB_PROTOCOL_IP6:
177 return (prefix->fp_len == 128);
178 case FIB_PROTOCOL_MPLS:
185 format_fib_prefix (u8 * s, va_list * args)
187 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
190 * protocol specific so it prints ::/0 correctly.
192 switch (fp->fp_proto)
194 case FIB_PROTOCOL_IP6:
196 ip6_address_t p6 = fp->fp_addr.ip6;
198 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
199 s = format (s, "%U", format_ip6_address, &p6);
202 case FIB_PROTOCOL_IP4:
204 ip4_address_t p4 = fp->fp_addr.ip4;
205 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
207 s = format (s, "%U", format_ip4_address, &p4);
210 case FIB_PROTOCOL_MPLS:
211 s = format (s, "%U:%U",
212 format_mpls_unicast_label, fp->fp_label,
213 format_mpls_eos_bit, fp->fp_eos);
216 s = format (s, "/%d", fp->fp_len);
222 fib_route_path_cmp (const fib_route_path_t *rpath1,
223 const fib_route_path_t *rpath2)
227 res = ip46_address_cmp(&rpath1->frp_addr,
230 if (0 != res) return (res);
232 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
234 if (0 != res) return (res);
236 if (ip46_address_is_zero(&rpath1->frp_addr))
238 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
245 fib_proto_to_dpo (fib_protocol_t fib_proto)
249 case FIB_PROTOCOL_IP6:
250 return (DPO_PROTO_IP6);
251 case FIB_PROTOCOL_IP4:
252 return (DPO_PROTO_IP4);
253 case FIB_PROTOCOL_MPLS:
254 return (DPO_PROTO_MPLS);
261 dpo_proto_to_fib (dpo_proto_t dpo_proto)
266 return (FIB_PROTOCOL_IP6);
268 return (FIB_PROTOCOL_IP4);
270 return (FIB_PROTOCOL_MPLS);
279 fib_proto_to_link (fib_protocol_t proto)
283 case FIB_PROTOCOL_IP4:
284 return (VNET_LINK_IP4);
285 case FIB_PROTOCOL_IP6:
286 return (VNET_LINK_IP6);
287 case FIB_PROTOCOL_MPLS:
288 return (VNET_LINK_MPLS);
295 fib_proto_to_ip46 (fib_protocol_t fproto)
299 case FIB_PROTOCOL_IP4:
300 return (IP46_TYPE_IP4);
301 case FIB_PROTOCOL_IP6:
302 return (IP46_TYPE_IP6);
303 case FIB_PROTOCOL_MPLS:
304 return (IP46_TYPE_ANY);
307 return (IP46_TYPE_ANY);
311 fib_proto_from_ip46 (ip46_type_t iproto)
316 return FIB_PROTOCOL_IP4;
318 return FIB_PROTOCOL_IP6;
321 return FIB_PROTOCOL_IP4;
325 return FIB_PROTOCOL_IP4;
328 fib_forward_chain_type_t
329 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
334 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
336 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
338 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
339 case DPO_PROTO_ETHERNET:
340 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
342 return (FIB_FORW_CHAIN_TYPE_NSH);
344 return (FIB_FORW_CHAIN_TYPE_BIER);
347 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
350 fib_forward_chain_type_t
351 fib_forw_chain_type_from_fib_proto (fib_protocol_t proto)
355 case FIB_PROTOCOL_IP4:
356 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
357 case FIB_PROTOCOL_IP6:
358 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
359 case FIB_PROTOCOL_MPLS:
360 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
363 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
367 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
371 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
372 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
373 return (VNET_LINK_IP4);
374 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
375 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
376 return (VNET_LINK_IP6);
377 case FIB_FORW_CHAIN_TYPE_ETHERNET:
378 return (VNET_LINK_ETHERNET);
379 case FIB_FORW_CHAIN_TYPE_NSH:
380 return (VNET_LINK_NSH);
381 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
382 case FIB_FORW_CHAIN_TYPE_BIER:
384 * insufficient information to to convert
388 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
389 return (VNET_LINK_MPLS);
391 return (VNET_LINK_IP4);
394 fib_forward_chain_type_t
395 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
400 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
402 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
404 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
405 case VNET_LINK_ETHERNET:
406 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
408 return (FIB_FORW_CHAIN_TYPE_NSH);
414 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
418 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
422 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
423 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
424 return (DPO_PROTO_IP4);
425 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
426 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
427 return (DPO_PROTO_IP6);
428 case FIB_FORW_CHAIN_TYPE_ETHERNET:
429 return (DPO_PROTO_ETHERNET);
430 case FIB_FORW_CHAIN_TYPE_NSH:
431 return (DPO_PROTO_NSH);
432 case FIB_FORW_CHAIN_TYPE_BIER:
433 return (DPO_PROTO_BIER);
434 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
435 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
436 return (DPO_PROTO_MPLS);
438 return (DPO_PROTO_IP4);
442 unformat_fib_route_path (unformat_input_t * input, va_list * args)
444 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
445 dpo_proto_t *payload_proto = va_arg (*args, void*);
446 u32 weight, preference, udp_encap_id, fi;
447 mpls_label_t out_label;
450 vnm = vnet_get_main ();
451 clib_memset(rpath, 0, sizeof(*rpath));
452 rpath->frp_weight = 1;
453 rpath->frp_sw_if_index = ~0;
455 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
457 if (unformat (input, "%U %U",
458 unformat_ip4_address,
459 &rpath->frp_addr.ip4,
460 unformat_vnet_sw_interface, vnm,
461 &rpath->frp_sw_if_index))
463 rpath->frp_proto = DPO_PROTO_IP4;
465 else if (unformat (input, "%U %U",
466 unformat_ip6_address,
467 &rpath->frp_addr.ip6,
468 unformat_vnet_sw_interface, vnm,
469 &rpath->frp_sw_if_index))
471 rpath->frp_proto = DPO_PROTO_IP6;
473 else if (unformat (input, "weight %u", &weight))
475 rpath->frp_weight = weight;
477 else if (unformat (input, "preference %u", &preference))
479 rpath->frp_preference = preference;
481 else if (unformat (input, "%U next-hop-table %d",
482 unformat_ip4_address,
483 &rpath->frp_addr.ip4,
484 &rpath->frp_fib_index))
486 rpath->frp_sw_if_index = ~0;
487 rpath->frp_proto = DPO_PROTO_IP4;
490 * the user enter table-ids, convert to index
492 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
495 rpath->frp_fib_index = fi;
497 else if (unformat (input, "%U next-hop-table %d",
498 unformat_ip6_address,
499 &rpath->frp_addr.ip6,
500 &rpath->frp_fib_index))
502 rpath->frp_sw_if_index = ~0;
503 rpath->frp_proto = DPO_PROTO_IP6;
504 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
507 rpath->frp_fib_index = fi;
509 else if (unformat (input, "%U",
510 unformat_ip4_address,
511 &rpath->frp_addr.ip4))
514 * the recursive next-hops are by default in the default table
516 rpath->frp_fib_index = 0;
517 rpath->frp_sw_if_index = ~0;
518 rpath->frp_proto = DPO_PROTO_IP4;
520 else if (unformat (input, "%U",
521 unformat_ip6_address,
522 &rpath->frp_addr.ip6))
524 rpath->frp_fib_index = 0;
525 rpath->frp_sw_if_index = ~0;
526 rpath->frp_proto = DPO_PROTO_IP6;
528 else if (unformat (input, "udp-encap %d", &udp_encap_id))
530 rpath->frp_udp_encap_id = udp_encap_id;
531 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
532 rpath->frp_proto = *payload_proto;
534 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
536 rpath->frp_proto = *payload_proto;
537 rpath->frp_sw_if_index = ~0;
538 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
540 else if (unformat (input, "resolve-via-host"))
542 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
544 else if (unformat (input, "resolve-via-attached"))
546 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
548 else if (unformat (input, "pop-pw-cw"))
550 rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
552 else if (unformat (input,
553 "ip4-lookup-in-table %d",
554 &rpath->frp_fib_index))
556 rpath->frp_proto = DPO_PROTO_IP4;
557 *payload_proto = DPO_PROTO_IP4;
558 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
561 rpath->frp_fib_index = fi;
563 else if (unformat (input,
564 "ip6-lookup-in-table %d",
565 &rpath->frp_fib_index))
567 rpath->frp_proto = DPO_PROTO_IP6;
568 *payload_proto = DPO_PROTO_IP6;
569 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
572 rpath->frp_fib_index = fi;
574 else if (unformat (input,
575 "mpls-lookup-in-table %d",
576 &rpath->frp_fib_index))
578 rpath->frp_proto = DPO_PROTO_MPLS;
579 *payload_proto = DPO_PROTO_MPLS;
580 fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
583 rpath->frp_fib_index = fi;
585 else if (unformat (input, "src-lookup"))
587 rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
589 else if (unformat (input,
591 unformat_vnet_sw_interface, vnm,
592 &rpath->frp_sw_if_index))
594 rpath->frp_proto = DPO_PROTO_ETHERNET;
595 *payload_proto = DPO_PROTO_ETHERNET;
596 rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
598 else if (unformat (input, "via-label %U",
599 unformat_mpls_unicast_label,
600 &rpath->frp_local_label))
602 rpath->frp_eos = MPLS_NON_EOS;
603 rpath->frp_proto = DPO_PROTO_MPLS;
604 rpath->frp_sw_if_index = ~0;
606 else if (unformat (input, "rx-ip4 %U",
607 unformat_vnet_sw_interface, vnm,
608 &rpath->frp_sw_if_index))
610 rpath->frp_proto = DPO_PROTO_IP4;
611 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
613 else if (unformat (input, "local"))
615 clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
616 rpath->frp_sw_if_index = ~0;
617 rpath->frp_weight = 1;
618 rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
620 else if (unformat (input, "%U",
621 unformat_mfib_itf_flags, &rpath->frp_mitf_flags))
623 else if (unformat (input, "out-labels"))
625 while (unformat (input, "%U",
626 unformat_mpls_unicast_label, &out_label))
628 fib_mpls_label_t fml = {
629 .fml_value = out_label,
631 vec_add1(rpath->frp_label_stack, fml);
634 else if (unformat (input, "%U",
635 unformat_vnet_sw_interface, vnm,
636 &rpath->frp_sw_if_index))
638 rpath->frp_proto = *payload_proto;
640 else if (unformat (input, "via"))
642 /* new path, back up and return */
643 unformat_put_input (input);
644 unformat_put_input (input);
645 unformat_put_input (input);
646 unformat_put_input (input);