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/mpls/mpls.h>
24 * arrays of protocol and link names
26 static const char* fib_protocol_names[] = FIB_PROTOCOLS;
27 static const char* vnet_link_names[] = VNET_LINKS;
28 static const char* fib_forw_chain_names[] = FIB_FORW_CHAINS;
29 static const char* fib_mpls_lsp_mode_names[] = FIB_MPLS_LSP_MODES;
32 format_fib_protocol (u8 * s, va_list * ap)
34 fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
36 return (format (s, "%s", fib_protocol_names[proto]));
40 format_vnet_link (u8 * s, va_list * ap)
42 vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
44 return (format (s, "%s", vnet_link_names[link]));
48 format_fib_forw_chain_type (u8 * s, va_list * args)
50 fib_forward_chain_type_t fct = va_arg(*args, int);
52 return (format (s, "%s", fib_forw_chain_names[fct]));
56 format_fib_mpls_lsp_mode(u8 *s, va_list *ap)
58 fib_mpls_lsp_mode_t mode = va_arg(*ap, int);
60 return (format (s, "%s", fib_mpls_lsp_mode_names[mode]));
64 format_fib_mpls_label (u8 *s, va_list *ap)
66 fib_mpls_label_t *label = va_arg(*ap, fib_mpls_label_t *);
68 s = format(s, "%U %U ttl:%d exp:%d",
69 format_mpls_unicast_label,
71 format_fib_mpls_lsp_mode,
80 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
83 ASSERT(!ip46_address_is_zero(addr));
85 pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
88 pfx->fp_len = ((ip46_address_is_ip4(addr) ?
94 fib_prefix_from_mpls_label (mpls_label_t label,
98 pfx->fp_proto = FIB_PROTOCOL_MPLS;
100 pfx->fp_label = label;
105 fib_prefix_cmp (const fib_prefix_t *p1,
106 const fib_prefix_t *p2)
110 res = (p1->fp_proto - p2->fp_proto);
114 switch (p1->fp_proto)
116 case FIB_PROTOCOL_IP4:
117 case FIB_PROTOCOL_IP6:
118 res = (p1->fp_len - p2->fp_len);
122 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
125 case FIB_PROTOCOL_MPLS:
126 res = (p1->fp_label - p2->fp_label);
130 res = (p1->fp_eos - p2->fp_eos);
140 fib_prefix_is_cover (const fib_prefix_t *p1,
141 const fib_prefix_t *p2)
143 switch (p1->fp_proto)
145 case FIB_PROTOCOL_IP4:
146 return (ip4_destination_matches_route(&ip4_main,
150 case FIB_PROTOCOL_IP6:
151 return (ip6_destination_matches_route(&ip6_main,
155 case FIB_PROTOCOL_MPLS:
162 fib_prefix_is_host (const fib_prefix_t *prefix)
164 switch (prefix->fp_proto)
166 case FIB_PROTOCOL_IP4:
167 return (prefix->fp_len == 32);
168 case FIB_PROTOCOL_IP6:
169 return (prefix->fp_len == 128);
170 case FIB_PROTOCOL_MPLS:
177 format_fib_prefix (u8 * s, va_list * args)
179 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
182 * protocol specific so it prints ::/0 correctly.
184 switch (fp->fp_proto)
186 case FIB_PROTOCOL_IP6:
188 ip6_address_t p6 = fp->fp_addr.ip6;
190 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
191 s = format (s, "%U", format_ip6_address, &p6);
194 case FIB_PROTOCOL_IP4:
196 ip4_address_t p4 = fp->fp_addr.ip4;
197 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
199 s = format (s, "%U", format_ip4_address, &p4);
202 case FIB_PROTOCOL_MPLS:
203 s = format (s, "%U:%U",
204 format_mpls_unicast_label, fp->fp_label,
205 format_mpls_eos_bit, fp->fp_eos);
208 s = format (s, "/%d", fp->fp_len);
214 fib_route_path_cmp (const fib_route_path_t *rpath1,
215 const fib_route_path_t *rpath2)
219 res = ip46_address_cmp(&rpath1->frp_addr,
222 if (0 != res) return (res);
224 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
226 if (0 != res) return (res);
228 if (ip46_address_is_zero(&rpath1->frp_addr))
230 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
237 fib_proto_to_dpo (fib_protocol_t fib_proto)
241 case FIB_PROTOCOL_IP6:
242 return (DPO_PROTO_IP6);
243 case FIB_PROTOCOL_IP4:
244 return (DPO_PROTO_IP4);
245 case FIB_PROTOCOL_MPLS:
246 return (DPO_PROTO_MPLS);
253 dpo_proto_to_fib (dpo_proto_t dpo_proto)
258 return (FIB_PROTOCOL_IP6);
260 return (FIB_PROTOCOL_IP4);
262 return (FIB_PROTOCOL_MPLS);
271 fib_proto_to_link (fib_protocol_t proto)
275 case FIB_PROTOCOL_IP4:
276 return (VNET_LINK_IP4);
277 case FIB_PROTOCOL_IP6:
278 return (VNET_LINK_IP6);
279 case FIB_PROTOCOL_MPLS:
280 return (VNET_LINK_MPLS);
287 fib_proto_to_ip46 (fib_protocol_t fproto)
291 case FIB_PROTOCOL_IP4:
292 return (IP46_TYPE_IP4);
293 case FIB_PROTOCOL_IP6:
294 return (IP46_TYPE_IP6);
295 case FIB_PROTOCOL_MPLS:
296 return (IP46_TYPE_ANY);
299 return (IP46_TYPE_ANY);
303 fib_proto_from_ip46 (ip46_type_t iproto)
308 return FIB_PROTOCOL_IP4;
310 return FIB_PROTOCOL_IP6;
313 return FIB_PROTOCOL_IP4;
317 return FIB_PROTOCOL_IP4;
320 fib_forward_chain_type_t
321 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
326 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
328 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
330 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
331 case DPO_PROTO_ETHERNET:
332 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
334 return (FIB_FORW_CHAIN_TYPE_NSH);
336 return (FIB_FORW_CHAIN_TYPE_BIER);
339 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
342 fib_forward_chain_type_t
343 fib_forw_chain_type_from_fib_proto (fib_protocol_t proto)
347 case FIB_PROTOCOL_IP4:
348 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
349 case FIB_PROTOCOL_IP6:
350 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
351 case FIB_PROTOCOL_MPLS:
352 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
355 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
359 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
363 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
364 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
365 return (VNET_LINK_IP4);
366 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
367 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
368 return (VNET_LINK_IP6);
369 case FIB_FORW_CHAIN_TYPE_ETHERNET:
370 return (VNET_LINK_ETHERNET);
371 case FIB_FORW_CHAIN_TYPE_NSH:
372 return (VNET_LINK_NSH);
373 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
374 case FIB_FORW_CHAIN_TYPE_BIER:
376 * insufficient information to to convert
380 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
381 return (VNET_LINK_MPLS);
383 return (VNET_LINK_IP4);
386 fib_forward_chain_type_t
387 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
392 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
394 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
396 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
397 case VNET_LINK_ETHERNET:
398 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
400 return (FIB_FORW_CHAIN_TYPE_NSH);
406 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
410 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
414 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
415 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
416 return (DPO_PROTO_IP4);
417 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
418 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
419 return (DPO_PROTO_IP6);
420 case FIB_FORW_CHAIN_TYPE_ETHERNET:
421 return (DPO_PROTO_ETHERNET);
422 case FIB_FORW_CHAIN_TYPE_NSH:
423 return (DPO_PROTO_NSH);
424 case FIB_FORW_CHAIN_TYPE_BIER:
425 return (DPO_PROTO_BIER);
426 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
427 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
428 return (DPO_PROTO_MPLS);
430 return (DPO_PROTO_IP4);
434 unformat_fib_route_path (unformat_input_t * input, va_list * args)
436 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
437 u32 *payload_proto = va_arg (*args, u32*);
438 u32 weight, preference, udp_encap_id, fi;
439 mpls_label_t out_label;
442 vnm = vnet_get_main ();
443 clib_memset(rpath, 0, sizeof(*rpath));
444 rpath->frp_weight = 1;
445 rpath->frp_sw_if_index = ~0;
447 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
449 if (unformat (input, "%U %U",
450 unformat_ip4_address,
451 &rpath->frp_addr.ip4,
452 unformat_vnet_sw_interface, vnm,
453 &rpath->frp_sw_if_index))
455 rpath->frp_proto = DPO_PROTO_IP4;
457 else if (unformat (input, "%U %U",
458 unformat_ip6_address,
459 &rpath->frp_addr.ip6,
460 unformat_vnet_sw_interface, vnm,
461 &rpath->frp_sw_if_index))
463 rpath->frp_proto = DPO_PROTO_IP6;
465 else if (unformat (input, "weight %u", &weight))
467 rpath->frp_weight = weight;
469 else if (unformat (input, "preference %u", &preference))
471 rpath->frp_preference = preference;
473 else if (unformat (input, "%U next-hop-table %d",
474 unformat_ip4_address,
475 &rpath->frp_addr.ip4,
476 &rpath->frp_fib_index))
478 rpath->frp_sw_if_index = ~0;
479 rpath->frp_proto = DPO_PROTO_IP4;
482 * the user enter table-ids, convert to index
484 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
487 rpath->frp_fib_index = fi;
489 else if (unformat (input, "%U next-hop-table %d",
490 unformat_ip6_address,
491 &rpath->frp_addr.ip6,
492 &rpath->frp_fib_index))
494 rpath->frp_sw_if_index = ~0;
495 rpath->frp_proto = DPO_PROTO_IP6;
496 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
499 rpath->frp_fib_index = fi;
501 else if (unformat (input, "%U",
502 unformat_ip4_address,
503 &rpath->frp_addr.ip4))
506 * the recursive next-hops are by default in the default table
508 rpath->frp_fib_index = 0;
509 rpath->frp_sw_if_index = ~0;
510 rpath->frp_proto = DPO_PROTO_IP4;
512 else if (unformat (input, "%U",
513 unformat_ip6_address,
514 &rpath->frp_addr.ip6))
516 rpath->frp_fib_index = 0;
517 rpath->frp_sw_if_index = ~0;
518 rpath->frp_proto = DPO_PROTO_IP6;
520 else if (unformat (input, "udp-encap %d", &udp_encap_id))
522 rpath->frp_udp_encap_id = udp_encap_id;
523 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
524 rpath->frp_proto = *payload_proto;
526 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
528 rpath->frp_proto = *payload_proto;
529 rpath->frp_sw_if_index = ~0;
530 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
532 else if (unformat (input, "resolve-via-host"))
534 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
536 else if (unformat (input, "resolve-via-attached"))
538 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
540 else if (unformat (input, "pop-pw-cw"))
542 rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
544 else if (unformat (input,
545 "ip4-lookup-in-table %d",
546 &rpath->frp_fib_index))
548 rpath->frp_proto = DPO_PROTO_IP4;
549 *payload_proto = DPO_PROTO_IP4;
550 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
553 rpath->frp_fib_index = fi;
555 else if (unformat (input,
556 "ip6-lookup-in-table %d",
557 &rpath->frp_fib_index))
559 rpath->frp_proto = DPO_PROTO_IP6;
560 *payload_proto = DPO_PROTO_IP6;
561 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
564 rpath->frp_fib_index = fi;
566 else if (unformat (input,
567 "mpls-lookup-in-table %d",
568 &rpath->frp_fib_index))
570 rpath->frp_proto = DPO_PROTO_MPLS;
571 *payload_proto = DPO_PROTO_MPLS;
572 fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
575 rpath->frp_fib_index = fi;
577 else if (unformat (input, "src-lookup"))
579 rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
581 else if (unformat (input,
583 unformat_vnet_sw_interface, vnm,
584 &rpath->frp_sw_if_index))
586 rpath->frp_proto = DPO_PROTO_ETHERNET;
587 *payload_proto = DPO_PROTO_ETHERNET;
588 rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
590 else if (unformat (input, "via-label %U",
591 unformat_mpls_unicast_label,
592 &rpath->frp_local_label))
594 rpath->frp_eos = MPLS_NON_EOS;
595 rpath->frp_proto = DPO_PROTO_MPLS;
596 rpath->frp_sw_if_index = ~0;
598 else if (unformat (input, "rx-ip4 %U",
599 unformat_vnet_sw_interface, vnm,
600 &rpath->frp_sw_if_index))
602 rpath->frp_proto = DPO_PROTO_IP4;
603 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
605 else if (unformat (input, "local"))
607 clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
608 rpath->frp_sw_if_index = ~0;
609 rpath->frp_weight = 1;
610 rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
612 else if (unformat (input, "out-labels"))
614 while (unformat (input, "%U",
615 unformat_mpls_unicast_label, &out_label))
617 fib_mpls_label_t fml = {
618 .fml_value = out_label,
620 vec_add1(rpath->frp_label_stack, fml);
623 else if (unformat (input, "%U",
624 unformat_vnet_sw_interface, vnm,
625 &rpath->frp_sw_if_index))
627 rpath->frp_proto = *payload_proto;
629 else if (unformat (input, "via"))
631 /* new path, back up and return */
632 unformat_put_input (input);
633 unformat_put_input (input);
634 unformat_put_input (input);
635 unformat_put_input (input);