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);
343 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
347 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
348 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
349 return (VNET_LINK_IP4);
350 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
351 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
352 return (VNET_LINK_IP6);
353 case FIB_FORW_CHAIN_TYPE_ETHERNET:
354 return (VNET_LINK_ETHERNET);
355 case FIB_FORW_CHAIN_TYPE_NSH:
356 return (VNET_LINK_NSH);
357 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
358 case FIB_FORW_CHAIN_TYPE_BIER:
360 * insufficient information to to convert
364 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
365 return (VNET_LINK_MPLS);
367 return (VNET_LINK_IP4);
370 fib_forward_chain_type_t
371 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
376 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
378 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
380 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
381 case VNET_LINK_ETHERNET:
382 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
384 return (FIB_FORW_CHAIN_TYPE_NSH);
390 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
394 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
398 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
399 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
400 return (DPO_PROTO_IP4);
401 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
402 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
403 return (DPO_PROTO_IP6);
404 case FIB_FORW_CHAIN_TYPE_ETHERNET:
405 return (DPO_PROTO_ETHERNET);
406 case FIB_FORW_CHAIN_TYPE_NSH:
407 return (DPO_PROTO_NSH);
408 case FIB_FORW_CHAIN_TYPE_BIER:
409 return (DPO_PROTO_BIER);
410 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
411 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
412 return (DPO_PROTO_MPLS);
414 return (DPO_PROTO_IP4);
418 unformat_fib_route_path (unformat_input_t * input, va_list * args)
420 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
421 u32 *payload_proto = va_arg (*args, u32*);
422 u32 weight, preference, udp_encap_id, fi;
423 mpls_label_t out_label;
426 vnm = vnet_get_main ();
427 memset(rpath, 0, sizeof(*rpath));
428 rpath->frp_weight = 1;
429 rpath->frp_sw_if_index = ~0;
431 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
433 if (unformat (input, "%U %U",
434 unformat_ip4_address,
435 &rpath->frp_addr.ip4,
436 unformat_vnet_sw_interface, vnm,
437 &rpath->frp_sw_if_index))
439 rpath->frp_proto = DPO_PROTO_IP4;
441 else if (unformat (input, "%U %U",
442 unformat_ip6_address,
443 &rpath->frp_addr.ip6,
444 unformat_vnet_sw_interface, vnm,
445 &rpath->frp_sw_if_index))
447 rpath->frp_proto = DPO_PROTO_IP6;
449 else if (unformat (input, "weight %u", &weight))
451 rpath->frp_weight = weight;
453 else if (unformat (input, "preference %u", &preference))
455 rpath->frp_preference = preference;
457 else if (unformat (input, "%U next-hop-table %d",
458 unformat_ip4_address,
459 &rpath->frp_addr.ip4,
460 &rpath->frp_fib_index))
462 rpath->frp_sw_if_index = ~0;
463 rpath->frp_proto = DPO_PROTO_IP4;
466 * the user enter table-ids, convert to index
468 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
471 rpath->frp_fib_index = fi;
473 else if (unformat (input, "%U next-hop-table %d",
474 unformat_ip6_address,
475 &rpath->frp_addr.ip6,
476 &rpath->frp_fib_index))
478 rpath->frp_sw_if_index = ~0;
479 rpath->frp_proto = DPO_PROTO_IP6;
480 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
483 rpath->frp_fib_index = fi;
485 else if (unformat (input, "%U",
486 unformat_ip4_address,
487 &rpath->frp_addr.ip4))
490 * the recursive next-hops are by default in the default table
492 rpath->frp_fib_index = 0;
493 rpath->frp_sw_if_index = ~0;
494 rpath->frp_proto = DPO_PROTO_IP4;
496 else if (unformat (input, "%U",
497 unformat_ip6_address,
498 &rpath->frp_addr.ip6))
500 rpath->frp_fib_index = 0;
501 rpath->frp_sw_if_index = ~0;
502 rpath->frp_proto = DPO_PROTO_IP6;
504 else if (unformat (input, "udp-encap %d", &udp_encap_id))
506 rpath->frp_udp_encap_id = udp_encap_id;
507 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
508 rpath->frp_proto = *payload_proto;
510 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
512 rpath->frp_proto = *payload_proto;
513 rpath->frp_sw_if_index = ~0;
514 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
516 else if (unformat (input, "resolve-via-host"))
518 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
520 else if (unformat (input, "resolve-via-attached"))
522 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
524 else if (unformat (input,
525 "ip4-lookup-in-table %d",
526 &rpath->frp_fib_index))
528 rpath->frp_proto = DPO_PROTO_IP4;
529 *payload_proto = DPO_PROTO_IP4;
530 fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
533 rpath->frp_fib_index = fi;
535 else if (unformat (input,
536 "ip6-lookup-in-table %d",
537 &rpath->frp_fib_index))
539 rpath->frp_proto = DPO_PROTO_IP6;
540 *payload_proto = DPO_PROTO_IP6;
541 fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
544 rpath->frp_fib_index = fi;
546 else if (unformat (input,
547 "mpls-lookup-in-table %d",
548 &rpath->frp_fib_index))
550 rpath->frp_proto = DPO_PROTO_MPLS;
551 *payload_proto = DPO_PROTO_MPLS;
552 fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
555 rpath->frp_fib_index = fi;
557 else if (unformat (input, "src-lookup"))
559 rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
561 else if (unformat (input,
563 unformat_vnet_sw_interface, vnm,
564 &rpath->frp_sw_if_index))
566 rpath->frp_proto = DPO_PROTO_ETHERNET;
567 *payload_proto = DPO_PROTO_ETHERNET;
568 rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
570 else if (unformat (input, "via-label %U",
571 unformat_mpls_unicast_label,
572 &rpath->frp_local_label))
574 rpath->frp_eos = MPLS_NON_EOS;
575 rpath->frp_proto = DPO_PROTO_MPLS;
576 rpath->frp_sw_if_index = ~0;
578 else if (unformat (input, "rx-ip4 %U",
579 unformat_vnet_sw_interface, vnm,
580 &rpath->frp_sw_if_index))
582 rpath->frp_proto = DPO_PROTO_IP4;
583 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
585 else if (unformat (input, "out-labels"))
587 while (unformat (input, "%U",
588 unformat_mpls_unicast_label, &out_label))
590 fib_mpls_label_t fml = {
591 .fml_value = out_label,
593 vec_add1(rpath->frp_label_stack, fml);
596 else if (unformat (input, "%U",
597 unformat_vnet_sw_interface, vnm,
598 &rpath->frp_sw_if_index))
600 rpath->frp_proto = *payload_proto;