builtinurl: mark api as deprecated
[vpp.git] / src / vnet / fib / fib_types.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/ip/ip.h>
17
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>
23
24 /*
25  * arrays of protocol and link names
26  */
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;
31
32 u8 *
33 format_fib_protocol (u8 * s, va_list * ap)
34 {
35     fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
36
37     return (format (s, "%s", fib_protocol_names[proto]));
38 }
39
40 u8 *
41 format_vnet_link (u8 * s, va_list * ap)
42 {
43     vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
44
45     return (format (s, "%s", vnet_link_names[link]));
46 }
47
48 u8 *
49 format_fib_forw_chain_type (u8 * s, va_list * args)
50 {
51     fib_forward_chain_type_t fct = va_arg(*args, int);
52
53     return (format (s, "%s", fib_forw_chain_names[fct]));
54 }
55
56 u8 *
57 format_fib_mpls_lsp_mode(u8 *s, va_list *ap)
58 {
59     fib_mpls_lsp_mode_t mode = va_arg(*ap, int);
60
61     return (format (s, "%s", fib_mpls_lsp_mode_names[mode])); 
62 }
63
64 u8 *
65 format_fib_mpls_label (u8 *s, va_list *ap)
66 {
67     fib_mpls_label_t *label = va_arg(*ap, fib_mpls_label_t *);
68
69     s = format(s, "%U %U ttl:%d exp:%d",
70                format_mpls_unicast_label,
71                label->fml_value,
72                format_fib_mpls_lsp_mode,
73                label->fml_mode,
74                label->fml_ttl,
75                label->fml_exp);
76
77     return (s);
78 }
79
80 void
81 fib_prefix_from_ip46_addr (fib_protocol_t fproto,
82                            const ip46_address_t *addr,
83                            fib_prefix_t *pfx)
84 {
85     ASSERT(FIB_PROTOCOL_MPLS != fproto);
86
87     pfx->fp_proto = fproto;
88     pfx->fp_len = ((FIB_PROTOCOL_IP4 == fproto) ?
89                     32 : 128);
90     pfx->fp_addr = *addr;
91     pfx->___fp___pad = 0;
92 }
93
94 u8 *
95 format_fib_route_path_flags (u8 *s, va_list *ap)
96 {
97     fib_route_path_flags_t flags = va_arg (*ap, fib_route_path_flags_t);
98
99     if (flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
100         s = format (s, "via-host");
101     if (flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
102         s = format (s, "via-attached,");
103     if (flags & FIB_ROUTE_PATH_LOCAL)
104         s = format (s, "local,");
105     if (flags & FIB_ROUTE_PATH_ATTACHED)
106         s = format (s, "attached,");
107     if (flags & FIB_ROUTE_PATH_DROP)
108          s = format (s, "drop,");
109    if (flags & FIB_ROUTE_PATH_EXCLUSIVE)
110         s = format (s, "exclusive,");
111     if (flags & FIB_ROUTE_PATH_INTF_RX)
112         s = format (s, "intf-rx,");
113     if (flags & FIB_ROUTE_PATH_RPF_ID)
114         s = format (s, "rpf-id,");
115     if (flags & FIB_ROUTE_PATH_SOURCE_LOOKUP)
116         s = format (s, "src-lkup,");
117     if (flags & FIB_ROUTE_PATH_UDP_ENCAP)
118         s = format (s, "udp-encap,");
119     if (flags & FIB_ROUTE_PATH_BIER_FMASK)
120         s = format (s, "bier-fmask,");
121     if (flags & FIB_ROUTE_PATH_BIER_TABLE)
122         s = format (s, "bier-table,");
123     if (flags & FIB_ROUTE_PATH_BIER_IMP)
124         s = format (s, "bier-imp,");
125     if (flags & FIB_ROUTE_PATH_DEAG)
126         s = format (s, "deag,");
127     if (flags & FIB_ROUTE_PATH_DVR)
128         s = format (s, "dvr,");
129     if (flags & FIB_ROUTE_PATH_ICMP_UNREACH)
130         s = format (s, "imcp-unreach,");
131     if (flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
132         s = format (s, "icmp-prohibit,");
133     if (flags & FIB_ROUTE_PATH_CLASSIFY)
134         s = format (s, "classify,");
135     if (flags & FIB_ROUTE_PATH_POP_PW_CW)
136         s = format (s, "pop-pw-cw,");
137
138     return (s);
139 }
140
141 u8 *
142 format_fib_route_path (u8 *s, va_list *ap)
143 {
144     fib_route_path_t *rpath = va_arg (*ap, fib_route_path_t*);
145
146     s = format (s, "%U %U, %U, [%U]",
147                 format_dpo_proto, rpath->frp_proto,
148                 format_ip46_address, &rpath->frp_addr, IP46_TYPE_ANY,
149                 format_vnet_sw_if_index_name, vnet_get_main (),
150                 rpath->frp_sw_if_index,
151                 format_fib_route_path_flags, rpath->frp_flags);
152
153     return (s);
154 }
155
156 void
157 fib_prefix_from_mpls_label (mpls_label_t label,
158                             mpls_eos_bit_t eos,
159                             fib_prefix_t *pfx)
160 {
161     pfx->fp_proto = FIB_PROTOCOL_MPLS;
162     pfx->fp_len = 21;
163     pfx->fp_label = label;
164     pfx->fp_eos = eos;
165     pfx->___fp___pad = 0;
166 }
167
168 void
169 fib_prefix_copy (fib_prefix_t *dst,
170                  const fib_prefix_t *src)
171 {
172     clib_memcpy(dst, src, sizeof(*dst));
173 }
174
175 int
176 fib_prefix_cmp (const fib_prefix_t *p1,
177                 const fib_prefix_t *p2)
178 {
179     int res;
180
181     res = (p1->fp_proto - p2->fp_proto);
182
183     if (0 == res)
184     {
185         switch (p1->fp_proto)
186         {
187         case FIB_PROTOCOL_IP4:
188         case FIB_PROTOCOL_IP6:
189             res = (p1->fp_len - p2->fp_len);
190
191             if (0 == res)
192             {
193                 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
194             }
195             break;
196         case FIB_PROTOCOL_MPLS:
197             res = (p1->fp_label - p2->fp_label);
198
199             if (0 == res)
200             {
201                 res = (p1->fp_eos - p2->fp_eos);
202             }
203             break;
204         }
205     }
206
207     return (res);
208 }
209
210 int
211 fib_prefix_is_cover (const fib_prefix_t *p1,
212                      const fib_prefix_t *p2)
213 {
214     switch (p1->fp_proto)
215     {
216     case FIB_PROTOCOL_IP4:
217         return (ip4_destination_matches_route(&ip4_main,
218                                               &p1->fp_addr.ip4,
219                                               &p2->fp_addr.ip4,
220                                               p1->fp_len));
221     case FIB_PROTOCOL_IP6:
222         return (ip6_destination_matches_route(&ip6_main,
223                                               &p1->fp_addr.ip6,
224                                               &p2->fp_addr.ip6,
225                                               p1->fp_len));
226     case FIB_PROTOCOL_MPLS:
227         break;
228     }
229     return (0);
230 }
231
232 u8
233 fib_prefix_get_host_length (fib_protocol_t proto)
234 {
235     switch (proto)
236     {
237     case FIB_PROTOCOL_IP4:
238         return (32);
239     case FIB_PROTOCOL_IP6:
240         return (128);
241     case FIB_PROTOCOL_MPLS:
242         return (21);
243     }
244     return (0);
245 }
246
247 int
248 fib_prefix_is_host (const fib_prefix_t *prefix)
249 {
250     switch (prefix->fp_proto)
251     {
252     case FIB_PROTOCOL_IP4:
253         return (prefix->fp_len == 32);
254     case FIB_PROTOCOL_IP6:
255         return (prefix->fp_len == 128);
256     case FIB_PROTOCOL_MPLS:
257         return (!0);
258     }
259     return (0);
260 }
261
262 void
263 fib_prefix_normalize (const fib_prefix_t *p,
264                       fib_prefix_t *out)
265 {
266     fib_prefix_copy (out, p);
267
268     switch (p->fp_proto)
269     {
270     case FIB_PROTOCOL_IP4:
271         ip4_address_normalize(&out->fp_addr.ip4, out->fp_len);
272         break;
273     case FIB_PROTOCOL_IP6:
274         ip6_address_normalize(&out->fp_addr.ip6, out->fp_len);
275         break;
276     case FIB_PROTOCOL_MPLS:
277         break;
278     }
279 }
280
281 u8 *
282 format_fib_prefix (u8 * s, va_list * args)
283 {
284     fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
285
286     /*
287      * protocol specific so it prints ::/0 correctly.
288      */
289     switch (fp->fp_proto)
290     {
291     case FIB_PROTOCOL_IP6:
292     {
293         ip6_address_t p6 = fp->fp_addr.ip6;
294
295         ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
296         s = format (s, "%U", format_ip6_address, &p6);
297         break;
298     }
299     case FIB_PROTOCOL_IP4:
300     {
301         ip4_address_t p4 = fp->fp_addr.ip4;
302         p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
303
304         s = format (s, "%U", format_ip4_address, &p4);
305         break;
306     }
307     case FIB_PROTOCOL_MPLS:
308         s = format (s, "%U:%U",
309                     format_mpls_unicast_label, fp->fp_label,
310                     format_mpls_eos_bit, fp->fp_eos);
311         break;
312     }
313     s = format (s, "/%d", fp->fp_len);
314
315     return (s);
316 }
317
318 int
319 fib_route_path_cmp (const fib_route_path_t *rpath1,
320                     const fib_route_path_t *rpath2)
321 {
322     int res;
323
324     res = ip46_address_cmp(&rpath1->frp_addr,
325                            &rpath2->frp_addr);
326
327     if (0 != res) return (res);
328
329     res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
330
331     if (0 != res) return (res);
332
333     if (ip46_address_is_zero(&rpath1->frp_addr))
334     {
335         res = rpath1->frp_fib_index - rpath2->frp_fib_index;
336     }
337
338     return (res);
339 }
340
341 dpo_proto_t
342 fib_proto_to_dpo (fib_protocol_t fib_proto)
343 {
344     switch (fib_proto)
345     {
346     case FIB_PROTOCOL_IP6:
347         return (DPO_PROTO_IP6);
348     case FIB_PROTOCOL_IP4:
349         return (DPO_PROTO_IP4);
350     case FIB_PROTOCOL_MPLS:
351         return (DPO_PROTO_MPLS);
352     }
353     ASSERT(0);
354     return (0);
355 }
356
357 fib_protocol_t
358 dpo_proto_to_fib (dpo_proto_t dpo_proto)
359 {
360     switch (dpo_proto)
361     {
362     case DPO_PROTO_IP6:
363         return (FIB_PROTOCOL_IP6);
364     case DPO_PROTO_IP4:
365         return (FIB_PROTOCOL_IP4);
366     case DPO_PROTO_MPLS:
367         return (FIB_PROTOCOL_MPLS);
368     default:
369         break;
370     }
371     ASSERT(0);
372     return (0);
373 }
374
375 vnet_link_t
376 fib_proto_to_link (fib_protocol_t proto)
377 {
378     switch (proto)
379     {
380     case FIB_PROTOCOL_IP4:
381         return (VNET_LINK_IP4);
382     case FIB_PROTOCOL_IP6:
383         return (VNET_LINK_IP6);
384     case FIB_PROTOCOL_MPLS:
385         return (VNET_LINK_MPLS);
386     }
387     ASSERT(0);
388     return (0);
389 }
390
391 ip46_type_t
392 fib_proto_to_ip46 (fib_protocol_t fproto)
393 {
394     switch (fproto)
395     {
396     case FIB_PROTOCOL_IP4:
397         return (IP46_TYPE_IP4);
398     case FIB_PROTOCOL_IP6:
399         return (IP46_TYPE_IP6);
400     case FIB_PROTOCOL_MPLS:
401         return (IP46_TYPE_ANY);
402     }
403     ASSERT(0);
404     return (IP46_TYPE_ANY);
405 }
406
407 fib_protocol_t
408 fib_proto_from_ip46 (ip46_type_t iproto)
409 {
410     switch (iproto)
411     {
412     case IP46_TYPE_IP4:
413         return FIB_PROTOCOL_IP4;
414     case IP46_TYPE_IP6:
415         return FIB_PROTOCOL_IP6;
416     case IP46_TYPE_ANY:
417         ASSERT(0);
418         return FIB_PROTOCOL_IP4;
419     }
420
421     ASSERT(0);
422     return FIB_PROTOCOL_IP4;
423 }
424
425 fib_forward_chain_type_t
426 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
427 {
428     switch (proto)
429     {
430     case DPO_PROTO_IP4:
431         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
432     case DPO_PROTO_IP6:
433         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
434     case DPO_PROTO_MPLS:
435         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
436     case DPO_PROTO_ETHERNET:
437         return (FIB_FORW_CHAIN_TYPE_ETHERNET);
438     case DPO_PROTO_NSH:
439         return (FIB_FORW_CHAIN_TYPE_NSH);
440     case DPO_PROTO_BIER:
441         return (FIB_FORW_CHAIN_TYPE_BIER);
442     }
443     ASSERT(0);
444     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
445 }
446
447 fib_forward_chain_type_t
448 fib_forw_chain_type_from_fib_proto (fib_protocol_t proto)
449 {
450     switch (proto)
451     {
452     case FIB_PROTOCOL_IP4:
453         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
454     case FIB_PROTOCOL_IP6:
455         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
456     case FIB_PROTOCOL_MPLS:
457         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
458     }
459     ASSERT(0);
460     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
461 }
462
463 vnet_link_t
464 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
465 {
466     switch (fct)
467     {
468     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
469     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
470         return (VNET_LINK_IP4);
471     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
472     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
473         return (VNET_LINK_IP6);
474     case FIB_FORW_CHAIN_TYPE_ETHERNET:
475         return (VNET_LINK_ETHERNET);
476     case FIB_FORW_CHAIN_TYPE_NSH:
477         return (VNET_LINK_NSH);
478     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
479     case FIB_FORW_CHAIN_TYPE_BIER:
480         /*
481          * insufficient information to to convert
482          */
483         ASSERT(0);
484         break;
485     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
486         return (VNET_LINK_MPLS);
487     }
488     return (VNET_LINK_IP4);
489 }
490
491 fib_forward_chain_type_t
492 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
493 {
494     switch (link_type)
495     {
496     case VNET_LINK_IP4:
497         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
498     case VNET_LINK_IP6:
499         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
500     case VNET_LINK_MPLS:
501         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
502     case VNET_LINK_ETHERNET:
503         return (FIB_FORW_CHAIN_TYPE_ETHERNET);
504     case VNET_LINK_NSH:
505         return (FIB_FORW_CHAIN_TYPE_NSH);
506     case VNET_LINK_ARP:
507         break;
508     }
509
510     ASSERT(0);
511     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
512 }
513
514 dpo_proto_t
515 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
516 {
517     switch (fct)
518     {
519     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
520     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
521         return (DPO_PROTO_IP4);
522     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
523     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
524         return (DPO_PROTO_IP6);
525     case FIB_FORW_CHAIN_TYPE_ETHERNET:
526         return (DPO_PROTO_ETHERNET);
527     case FIB_FORW_CHAIN_TYPE_NSH:
528         return (DPO_PROTO_NSH);
529     case FIB_FORW_CHAIN_TYPE_BIER:
530         return (DPO_PROTO_BIER);
531     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
532     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
533         return (DPO_PROTO_MPLS);
534     }
535     return (DPO_PROTO_IP4);
536 }
537
538 uword
539 unformat_fib_route_path (unformat_input_t * input, va_list * args)
540 {
541     fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
542     dpo_proto_t *payload_proto = va_arg (*args, void*);
543     dpo_proto_t explicit_proto = DPO_PROTO_NONE;
544     u32 weight, preference, udp_encap_id, fi;
545     mpls_label_t out_label;
546     vnet_main_t *vnm;
547
548     vnm = vnet_get_main ();
549     clib_memset(rpath, 0, sizeof(*rpath));
550     rpath->frp_weight = 1;
551     rpath->frp_sw_if_index = ~0;
552
553     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
554     {
555         if (unformat (input, "%U %U",
556                       unformat_ip4_address,
557                       &rpath->frp_addr.ip4,
558                       unformat_vnet_sw_interface, vnm,
559                       &rpath->frp_sw_if_index))
560         {
561             rpath->frp_proto = DPO_PROTO_IP4;
562         }
563         else if (unformat (input, "%U %U",
564                            unformat_ip6_address,
565                            &rpath->frp_addr.ip6,
566                            unformat_vnet_sw_interface, vnm,
567                            &rpath->frp_sw_if_index))
568         {
569             rpath->frp_proto = DPO_PROTO_IP6;
570         }
571         else if (unformat (input, "weight %u", &weight))
572         {
573             rpath->frp_weight = weight;
574         }
575         else if (unformat (input, "preference %u", &preference))
576         {
577             rpath->frp_preference = preference;
578         }
579         else if (unformat (input, "%U next-hop-table %d",
580                            unformat_ip4_address,
581                            &rpath->frp_addr.ip4,
582                            &rpath->frp_fib_index))
583         {
584             rpath->frp_sw_if_index = ~0;
585             rpath->frp_proto = DPO_PROTO_IP4;
586
587             /*
588              * the user enter table-ids, convert to index
589              */
590             fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
591             if (~0 == fi)
592                 return 0;
593             rpath->frp_fib_index = fi;
594         }
595         else if (unformat (input, "%U next-hop-table %d",
596                            unformat_ip6_address,
597                            &rpath->frp_addr.ip6,
598                            &rpath->frp_fib_index))
599         {
600             rpath->frp_sw_if_index = ~0;
601             rpath->frp_proto = DPO_PROTO_IP6;
602             fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
603             if (~0 == fi)
604                 return 0;
605             rpath->frp_fib_index = fi;
606         }
607         else if (unformat (input, "%U",
608                            unformat_ip4_address,
609                            &rpath->frp_addr.ip4))
610         {
611             /*
612              * the recursive next-hops are by default in the default table
613              */
614             rpath->frp_fib_index = 0;
615             rpath->frp_sw_if_index = ~0;
616             rpath->frp_proto = DPO_PROTO_IP4;
617         }
618         else if (unformat (input, "%U",
619                            unformat_ip6_address,
620                            &rpath->frp_addr.ip6))
621         {
622             rpath->frp_fib_index = 0;
623             rpath->frp_sw_if_index = ~0;
624             rpath->frp_proto = DPO_PROTO_IP6;
625         }
626         else if (unformat (input, "udp-encap %d", &udp_encap_id))
627         {
628             rpath->frp_udp_encap_id = udp_encap_id;
629             rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
630             rpath->frp_proto = *payload_proto;
631         }
632         else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
633         {
634             rpath->frp_proto = *payload_proto;
635             rpath->frp_sw_if_index = ~0;
636             rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
637         }
638         else if (unformat (input, "resolve-via-host"))
639         {
640             rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
641         }
642         else if (unformat (input, "resolve-via-attached"))
643         {
644             rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
645         }
646         else if (unformat (input, "pop-pw-cw"))
647         {
648             rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
649         }
650         else if (unformat (input,
651                            "ip4-lookup-in-table %d",
652                            &rpath->frp_fib_index))
653         {
654             rpath->frp_proto = DPO_PROTO_IP4;
655             *payload_proto = DPO_PROTO_IP4;
656             fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
657             if (~0 == fi)
658                 return 0;
659             rpath->frp_fib_index = fi;
660         }
661         else if (unformat (input,
662                            "ip6-lookup-in-table %d",
663                            &rpath->frp_fib_index))
664         {
665             rpath->frp_proto = DPO_PROTO_IP6;
666             *payload_proto = DPO_PROTO_IP6;
667             fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
668             if (~0 == fi)
669                 return 0;
670             rpath->frp_fib_index = fi;
671         }
672         else if (unformat (input,
673                            "mpls-lookup-in-table %d",
674                            &rpath->frp_fib_index))
675         {
676             rpath->frp_proto = DPO_PROTO_MPLS;
677             *payload_proto = DPO_PROTO_MPLS;
678             fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
679             if (~0 == fi)
680                 return 0;
681             rpath->frp_fib_index = fi;
682         }
683         else if (unformat (input, "src-lookup"))
684         {
685             rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
686         }
687         else if (unformat (input,
688                            "l2-input-on %U",
689                            unformat_vnet_sw_interface, vnm,
690                            &rpath->frp_sw_if_index))
691         {
692             rpath->frp_proto = DPO_PROTO_ETHERNET;
693             *payload_proto = DPO_PROTO_ETHERNET;
694             rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
695         }
696         else if (unformat (input, "via-label %U",
697                            unformat_mpls_unicast_label,
698                            &rpath->frp_local_label))
699         {
700             rpath->frp_eos = MPLS_NON_EOS;
701             rpath->frp_proto = DPO_PROTO_MPLS;
702             rpath->frp_sw_if_index = ~0;
703         }
704         else if (unformat (input, "rx-ip4 %U",
705                            unformat_vnet_sw_interface, vnm,
706                            &rpath->frp_sw_if_index))
707         {
708             rpath->frp_proto = DPO_PROTO_IP4;
709             rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
710         }
711         else if (unformat (input, "rx-ip6 %U",
712                            unformat_vnet_sw_interface, vnm,
713                            &rpath->frp_sw_if_index))
714         {
715             rpath->frp_proto = DPO_PROTO_IP6;
716             rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
717         }
718       else if (unformat (input, "local"))
719         {
720           clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
721           rpath->frp_sw_if_index = ~0;
722           rpath->frp_weight = 1;
723           rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
724         }
725       else if (unformat (input, "out-labels"))
726         {
727             while (unformat (input, "%U",
728                              unformat_mpls_unicast_label, &out_label))
729             {
730                 fib_mpls_label_t fml = {
731                     .fml_value = out_label,
732                 };
733                 vec_add1(rpath->frp_label_stack, fml);
734             }
735         }
736       else if (unformat (input, "ip4"))
737         {
738           explicit_proto = DPO_PROTO_IP4;
739         }
740       else if (unformat (input, "ip6"))
741         {
742           explicit_proto = DPO_PROTO_IP6;
743         }
744         else if (unformat (input, "%U",
745                            unformat_vnet_sw_interface, vnm,
746                            &rpath->frp_sw_if_index))
747         {
748             rpath->frp_proto = *payload_proto;
749         }
750         else if (unformat (input, "%U",
751                          unformat_mfib_itf_flags, &rpath->frp_mitf_flags))
752         ;
753         else if (unformat (input, "via"))
754         {
755             /* new path, back up and return */
756             unformat_put_input (input);
757             unformat_put_input (input);
758             unformat_put_input (input);
759             unformat_put_input (input);
760             break;
761         }
762         else
763         {
764             return (0);
765         }
766     }
767
768     if (DPO_PROTO_NONE != explicit_proto)
769       *payload_proto = rpath->frp_proto = explicit_proto;
770
771     return (1);
772 }
773
774 /*
775  * Return true if the path is attached
776  */
777 int
778 fib_route_path_is_attached (const fib_route_path_t *rpath)
779 {
780     /*
781      * DVR paths are not attached, since we are not playing the
782      * L3 game with these
783      */
784     if (rpath->frp_flags & (FIB_ROUTE_PATH_DVR |
785                             FIB_ROUTE_PATH_INTF_RX |
786                             FIB_ROUTE_PATH_UDP_ENCAP))
787     {
788         return (0);
789     }
790
791     /*
792      * - All zeros next-hop
793      * - a valid interface
794      */
795     if (ip46_address_is_zero(&rpath->frp_addr) &&
796         (~0 != rpath->frp_sw_if_index))
797     {
798         return (!0);
799     }
800     else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED ||
801              rpath->frp_flags & FIB_ROUTE_PATH_GLEAN)
802     {
803         return (!0);
804     }
805     return (0);
806 }
807
808 static void
809 fib_prefix_ip4_addr_increment (fib_prefix_t *pfx)
810 {
811     /* Calculate the addend based on the host length of address */
812     u32 incr = 1ULL << (32 - pfx->fp_len);
813     ip4_address_t dst = (pfx->fp_addr).ip4;
814     dst.as_u32 = clib_host_to_net_u32 (incr + clib_net_to_host_u32 (dst.as_u32));
815     pfx->fp_addr.ip4.as_u32 = dst.as_u32;
816 }
817
818 static void
819 fib_prefix_ip6_addr_increment (fib_prefix_t *pfx)
820 {
821     /*
822      * Calculate the addend based on the host length of address
823      * and which part(lower 64 bits or higher 64 bits) it lies
824      * in
825      */
826     u32 host_len = 128 - pfx->fp_len;
827     u64 incr = 1ULL << ((host_len > 64) ? (host_len - 64) : host_len);
828     i32 bucket = (host_len < 64 ? 1 : 0);
829     ip6_address_t dst = (pfx->fp_addr).ip6;
830     u64 tmp = incr + clib_net_to_host_u64 (dst.as_u64[bucket]);
831     /* Handle overflow */
832     if (bucket && (tmp < incr))
833     {
834         dst.as_u64[1] = clib_host_to_net_u64 (tmp);
835         dst.as_u64[0] = clib_host_to_net_u64 (1ULL + clib_net_to_host_u64 (dst.as_u64[0]));
836     }
837     else
838         dst.as_u64[bucket] = clib_host_to_net_u64 (tmp);
839
840     pfx->fp_addr.ip6.as_u128 = dst.as_u128;
841 }
842
843 /*
844  * Increase IPv4/IPv6 address according to the prefix length
845  */
846 void fib_prefix_increment (fib_prefix_t *pfx)
847 {
848     switch (pfx->fp_proto)
849     {
850     case FIB_PROTOCOL_IP4:
851         fib_prefix_ip4_addr_increment (pfx);
852         break;
853     case FIB_PROTOCOL_IP6:
854         fib_prefix_ip6_addr_increment (pfx);
855         break;
856     case FIB_PROTOCOL_MPLS:
857         break;
858     }
859 }