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_cmp (const fib_prefix_t *p1,
107 const fib_prefix_t *p2)
111 res = (p1->fp_proto - p2->fp_proto);
115 switch (p1->fp_proto)
117 case FIB_PROTOCOL_IP4:
118 case FIB_PROTOCOL_IP6:
119 res = (p1->fp_len - p2->fp_len);
123 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
126 case FIB_PROTOCOL_MPLS:
127 res = (p1->fp_label - p2->fp_label);
131 res = (p1->fp_eos - p2->fp_eos);
141 fib_prefix_is_cover (const fib_prefix_t *p1,
142 const fib_prefix_t *p2)
144 switch (p1->fp_proto)
146 case FIB_PROTOCOL_IP4:
147 return (ip4_destination_matches_route(&ip4_main,
151 case FIB_PROTOCOL_IP6:
152 return (ip6_destination_matches_route(&ip6_main,
156 case FIB_PROTOCOL_MPLS:
163 fib_prefix_is_host (const fib_prefix_t *prefix)
165 switch (prefix->fp_proto)
167 case FIB_PROTOCOL_IP4:
168 return (prefix->fp_len == 32);
169 case FIB_PROTOCOL_IP6:
170 return (prefix->fp_len == 128);
171 case FIB_PROTOCOL_MPLS:
178 format_fib_prefix (u8 * s, va_list * args)
180 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
183 * protocol specific so it prints ::/0 correctly.
185 switch (fp->fp_proto)
187 case FIB_PROTOCOL_IP6:
189 ip6_address_t p6 = fp->fp_addr.ip6;
191 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
192 s = format (s, "%U", format_ip6_address, &p6);
195 case FIB_PROTOCOL_IP4:
197 ip4_address_t p4 = fp->fp_addr.ip4;
198 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
200 s = format (s, "%U", format_ip4_address, &p4);
203 case FIB_PROTOCOL_MPLS:
204 s = format (s, "%U:%U",
205 format_mpls_unicast_label, fp->fp_label,
206 format_mpls_eos_bit, fp->fp_eos);
209 s = format (s, "/%d", fp->fp_len);
215 fib_route_path_cmp (const fib_route_path_t *rpath1,
216 const fib_route_path_t *rpath2)
220 res = ip46_address_cmp(&rpath1->frp_addr,
223 if (0 != res) return (res);
225 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
227 if (0 != res) return (res);
229 if (ip46_address_is_zero(&rpath1->frp_addr))
231 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
238 fib_proto_to_dpo (fib_protocol_t fib_proto)
242 case FIB_PROTOCOL_IP6:
243 return (DPO_PROTO_IP6);
244 case FIB_PROTOCOL_IP4:
245 return (DPO_PROTO_IP4);
246 case FIB_PROTOCOL_MPLS:
247 return (DPO_PROTO_MPLS);
254 dpo_proto_to_fib (dpo_proto_t dpo_proto)
259 return (FIB_PROTOCOL_IP6);
261 return (FIB_PROTOCOL_IP4);
263 return (FIB_PROTOCOL_MPLS);
272 fib_proto_to_link (fib_protocol_t proto)
276 case FIB_PROTOCOL_IP4:
277 return (VNET_LINK_IP4);
278 case FIB_PROTOCOL_IP6:
279 return (VNET_LINK_IP6);
280 case FIB_PROTOCOL_MPLS:
281 return (VNET_LINK_MPLS);
288 fib_proto_to_ip46 (fib_protocol_t fproto)
292 case FIB_PROTOCOL_IP4:
293 return (IP46_TYPE_IP4);
294 case FIB_PROTOCOL_IP6:
295 return (IP46_TYPE_IP6);
296 case FIB_PROTOCOL_MPLS:
297 return (IP46_TYPE_ANY);
300 return (IP46_TYPE_ANY);
304 fib_proto_from_ip46 (ip46_type_t iproto)
309 return FIB_PROTOCOL_IP4;
311 return FIB_PROTOCOL_IP6;
314 return FIB_PROTOCOL_IP4;
318 return FIB_PROTOCOL_IP4;
321 fib_forward_chain_type_t
322 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
327 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
329 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
331 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
332 case DPO_PROTO_ETHERNET:
333 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
335 return (FIB_FORW_CHAIN_TYPE_NSH);
337 return (FIB_FORW_CHAIN_TYPE_BIER);
340 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
343 fib_forward_chain_type_t
344 fib_forw_chain_type_from_fib_proto (fib_protocol_t proto)
348 case FIB_PROTOCOL_IP4:
349 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
350 case FIB_PROTOCOL_IP6:
351 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
352 case FIB_PROTOCOL_MPLS:
353 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
356 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
360 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
364 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
365 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
366 return (VNET_LINK_IP4);
367 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
368 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
369 return (VNET_LINK_IP6);
370 case FIB_FORW_CHAIN_TYPE_ETHERNET:
371 return (VNET_LINK_ETHERNET);
372 case FIB_FORW_CHAIN_TYPE_NSH:
373 return (VNET_LINK_NSH);
374 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
375 case FIB_FORW_CHAIN_TYPE_BIER:
377 * insufficient information to to convert
381 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
382 return (VNET_LINK_MPLS);
384 return (VNET_LINK_IP4);
387 fib_forward_chain_type_t
388 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
393 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
395 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
397 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
398 case VNET_LINK_ETHERNET:
399 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
401 return (FIB_FORW_CHAIN_TYPE_NSH);
407 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
411 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
415 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
416 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
417 return (DPO_PROTO_IP4);
418 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
419 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
420 return (DPO_PROTO_IP6);
421 case FIB_FORW_CHAIN_TYPE_ETHERNET:
422 return (DPO_PROTO_ETHERNET);
423 case FIB_FORW_CHAIN_TYPE_NSH:
424 return (DPO_PROTO_NSH);
425 case FIB_FORW_CHAIN_TYPE_BIER:
426 return (DPO_PROTO_BIER);
427 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
428 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
429 return (DPO_PROTO_MPLS);
431 return (DPO_PROTO_IP4);
435 unformat_fib_route_path (unformat_input_t * input, va_list * args)
437 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
438 dpo_proto_t *payload_proto = va_arg (*args, void*);
439 u32 weight, preference, udp_encap_id, fi;
440 mpls_label_t out_label;
443 vnm = vnet_get_main ();
444 clib_memset(rpath, 0, sizeof(*rpath));
445 rpath->frp_weight = 1;
446 rpath->frp_sw_if_index = ~0;
448 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
450 if (unformat (input, "%U %U",
451 unformat_ip4_address,
452 &rpath->frp_addr.ip4,
453 unformat_vnet_sw_interface, vnm,
454 &rpath->frp_sw_if_index))
456 rpath->frp_proto = DPO_PROTO_IP4;
458 else if (unformat (input, "%U %U",
459 unformat_ip6_address,
460 &rpath->frp_addr.ip6,
461 unformat_vnet_sw_interface, vnm,
462 &rpath->frp_sw_if_index))
464 rpath->frp_proto = DPO_PROTO_IP6;
466 else if (unformat (input, "weight %u", &weight))
468 rpath->frp_weight = weight;
470 else if (unformat (input, "preference %u", &preference))
472 rpath->frp_preference = preference;
474 else if (unformat (input, "%U next-hop-table %d",
475 unformat_ip4_address,
476 &rpath->frp_addr.ip4,
477 &rpath->frp_fib_index))
479 rpath->frp_sw_if_index = ~0;
480 rpath->frp_proto = DPO_PROTO_IP4;
483 * the user enter table-ids, convert to index
485 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
488 rpath->frp_fib_index = fi;
490 else if (unformat (input, "%U next-hop-table %d",
491 unformat_ip6_address,
492 &rpath->frp_addr.ip6,
493 &rpath->frp_fib_index))
495 rpath->frp_sw_if_index = ~0;
496 rpath->frp_proto = DPO_PROTO_IP6;
497 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
500 rpath->frp_fib_index = fi;
502 else if (unformat (input, "%U",
503 unformat_ip4_address,
504 &rpath->frp_addr.ip4))
507 * the recursive next-hops are by default in the default table
509 rpath->frp_fib_index = 0;
510 rpath->frp_sw_if_index = ~0;
511 rpath->frp_proto = DPO_PROTO_IP4;
513 else if (unformat (input, "%U",
514 unformat_ip6_address,
515 &rpath->frp_addr.ip6))
517 rpath->frp_fib_index = 0;
518 rpath->frp_sw_if_index = ~0;
519 rpath->frp_proto = DPO_PROTO_IP6;
521 else if (unformat (input, "udp-encap %d", &udp_encap_id))
523 rpath->frp_udp_encap_id = udp_encap_id;
524 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
525 rpath->frp_proto = *payload_proto;
527 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
529 rpath->frp_proto = *payload_proto;
530 rpath->frp_sw_if_index = ~0;
531 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
533 else if (unformat (input, "resolve-via-host"))
535 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
537 else if (unformat (input, "resolve-via-attached"))
539 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
541 else if (unformat (input, "pop-pw-cw"))
543 rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
545 else if (unformat (input,
546 "ip4-lookup-in-table %d",
547 &rpath->frp_fib_index))
549 rpath->frp_proto = DPO_PROTO_IP4;
550 *payload_proto = DPO_PROTO_IP4;
551 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
554 rpath->frp_fib_index = fi;
556 else if (unformat (input,
557 "ip6-lookup-in-table %d",
558 &rpath->frp_fib_index))
560 rpath->frp_proto = DPO_PROTO_IP6;
561 *payload_proto = DPO_PROTO_IP6;
562 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
565 rpath->frp_fib_index = fi;
567 else if (unformat (input,
568 "mpls-lookup-in-table %d",
569 &rpath->frp_fib_index))
571 rpath->frp_proto = DPO_PROTO_MPLS;
572 *payload_proto = DPO_PROTO_MPLS;
573 fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
576 rpath->frp_fib_index = fi;
578 else if (unformat (input, "src-lookup"))
580 rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
582 else if (unformat (input,
584 unformat_vnet_sw_interface, vnm,
585 &rpath->frp_sw_if_index))
587 rpath->frp_proto = DPO_PROTO_ETHERNET;
588 *payload_proto = DPO_PROTO_ETHERNET;
589 rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
591 else if (unformat (input, "via-label %U",
592 unformat_mpls_unicast_label,
593 &rpath->frp_local_label))
595 rpath->frp_eos = MPLS_NON_EOS;
596 rpath->frp_proto = DPO_PROTO_MPLS;
597 rpath->frp_sw_if_index = ~0;
599 else if (unformat (input, "rx-ip4 %U",
600 unformat_vnet_sw_interface, vnm,
601 &rpath->frp_sw_if_index))
603 rpath->frp_proto = DPO_PROTO_IP4;
604 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
606 else if (unformat (input, "local"))
608 clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
609 rpath->frp_sw_if_index = ~0;
610 rpath->frp_weight = 1;
611 rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
613 else if (unformat (input, "%U",
614 unformat_mfib_itf_flags, &rpath->frp_mitf_flags))
616 else if (unformat (input, "out-labels"))
618 while (unformat (input, "%U",
619 unformat_mpls_unicast_label, &out_label))
621 fib_mpls_label_t fml = {
622 .fml_value = out_label,
624 vec_add1(rpath->frp_label_stack, fml);
627 else if (unformat (input, "%U",
628 unformat_vnet_sw_interface, vnm,
629 &rpath->frp_sw_if_index))
631 rpath->frp_proto = *payload_proto;
633 else if (unformat (input, "via"))
635 /* new path, back up and return */
636 unformat_put_input (input);
637 unformat_put_input (input);
638 unformat_put_input (input);
639 unformat_put_input (input);