A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / ip / lookup.c
1 /*
2  * Copyright (c) 2015 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  * ip/ip_lookup.c: ip4/6 adjacency and lookup table managment
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/ip/ip.h>
41 #include <vnet/adj/adj_alloc.h>
42 #include <vnet/fib/fib_table.h>
43 #include <vnet/fib/ip4_fib.h>
44 #include <vnet/fib/ip6_fib.h>
45 #include <vnet/mpls/mpls.h>
46 #include <vnet/dpo/drop_dpo.h>
47 #include <vnet/dpo/classify_dpo.h>
48 #include <vnet/dpo/punt_dpo.h>
49 #include <vnet/dpo/receive_dpo.h>
50
51 clib_error_t *
52 ip_interface_address_add_del (ip_lookup_main_t * lm,
53                               u32 sw_if_index,
54                               void * addr_fib,
55                               u32 address_length,
56                               u32 is_del,
57                               u32 * result_if_address_index)
58 {
59   vnet_main_t * vnm = vnet_get_main();
60   ip_interface_address_t * a, * prev, * next;
61   uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
62
63   vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
64   a = p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
65
66   /* Verify given length. */
67   if ((a && (address_length != a->address_length)) || (address_length == 0))
68     {
69       vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
70       return clib_error_create 
71         ( "%U wrong length (expected %d) for interface %U",
72           lm->format_address_and_length, addr_fib,
73           address_length, a? a->address_length : -1,
74           format_vnet_sw_if_index_name, vnm, sw_if_index);
75     }
76
77   if (is_del)
78     {
79       if (!a) 
80         {
81           vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
82           vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
83           return clib_error_create ("%U not found for interface %U",
84                                     lm->format_address_and_length, 
85                                     addr_fib, address_length,
86                                     format_vnet_sw_interface_name, vnm, si);
87         }
88
89       if (a->prev_this_sw_interface != ~0)
90         {
91           prev = pool_elt_at_index (lm->if_address_pool, a->prev_this_sw_interface);
92           prev->next_this_sw_interface = a->next_this_sw_interface;
93         }
94       if (a->next_this_sw_interface != ~0)
95         {
96           next = pool_elt_at_index (lm->if_address_pool, a->next_this_sw_interface);
97           next->prev_this_sw_interface = a->prev_this_sw_interface;
98
99           if(a->prev_this_sw_interface == ~0)
100                  lm->if_address_pool_index_by_sw_if_index[sw_if_index]  = a->next_this_sw_interface;
101         }
102
103       if ((a->next_this_sw_interface  == ~0) &&  (a->prev_this_sw_interface == ~0))
104         lm->if_address_pool_index_by_sw_if_index[sw_if_index] = ~0;
105
106       mhash_unset (&lm->address_to_if_address_index, addr_fib,
107                    /* old_value */ 0);
108       pool_put (lm->if_address_pool, a);
109
110       if (result_if_address_index)
111         *result_if_address_index = ~0;
112     }
113
114   else if (! a)
115     {
116       u32 pi; /* previous index */
117       u32 ai; 
118       u32 hi; /* head index */
119
120       pool_get (lm->if_address_pool, a);
121       memset (a, ~0, sizeof (a[0]));
122       ai = a - lm->if_address_pool;
123
124       hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
125       prev = 0;
126       while (pi != (u32)~0)
127         {
128           prev = pool_elt_at_index(lm->if_address_pool, pi);
129           pi = prev->next_this_sw_interface;
130         }
131       pi = prev ? prev - lm->if_address_pool : (u32)~0;
132
133       a->address_key = mhash_set (&lm->address_to_if_address_index,
134                                   addr_fib, ai, /* old_value */ 0);
135       a->address_length = address_length;
136       a->sw_if_index = sw_if_index;
137       a->flags = 0;
138       a->prev_this_sw_interface = pi;
139       a->next_this_sw_interface = ~0;
140       if (prev)
141           prev->next_this_sw_interface = ai;
142
143       lm->if_address_pool_index_by_sw_if_index[sw_if_index] = 
144         (hi != ~0) ? hi : ai;
145       if (result_if_address_index)
146         *result_if_address_index = ai;
147     }
148   else
149     {
150       if (result_if_address_index)
151         *result_if_address_index = a - lm->if_address_pool;
152     }
153     
154
155   return /* no error */ 0;
156 }
157
158 void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
159 {
160   /* ensure that adjacency is cacheline aligned and sized */
161   ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline0) == 0);
162   ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
163
164   /* Preallocate three "special" adjacencies */
165   lm->adjacency_heap = adj_heap;
166
167   if (! lm->fib_result_n_bytes)
168     lm->fib_result_n_bytes = sizeof (uword);
169
170   lm->is_ip6 = is_ip6;
171   if (is_ip6)
172     {
173       lm->format_address_and_length = format_ip6_address_and_length;
174       mhash_init (&lm->address_to_if_address_index, sizeof (uword),
175                   sizeof (ip6_address_fib_t));
176     }
177   else
178     {
179       lm->format_address_and_length = format_ip4_address_and_length;
180       mhash_init (&lm->address_to_if_address_index, sizeof (uword),
181                   sizeof (ip4_address_fib_t));
182     }
183
184   {
185     int i;
186
187     /* Setup all IP protocols to be punted and builtin-unknown. */
188     for (i = 0; i < 256; i++)
189       {
190         lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
191         lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
192       }
193
194     lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
195     lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
196     lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] = IP_BUILTIN_PROTOCOL_UDP;
197     lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_BUILTIN_PROTOCOL_ICMP;
198   }
199 }
200
201 u8 * format_ip_flow_hash_config (u8 * s, va_list * args)
202 {
203   flow_hash_config_t flow_hash_config = va_arg (*args, u32);
204     
205 #define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
206   foreach_flow_hash_bit;
207 #undef _
208
209   return s;
210 }
211
212 u8 * format_ip_lookup_next (u8 * s, va_list * args)
213 {
214   ip_lookup_next_t n = va_arg (*args, ip_lookup_next_t);
215   char * t = 0;
216
217   switch (n)
218     {
219     default:
220       s = format (s, "unknown %d", n);
221       return s;
222
223     case IP_LOOKUP_NEXT_DROP: t = "drop"; break;
224     case IP_LOOKUP_NEXT_PUNT: t = "punt"; break;
225     case IP_LOOKUP_NEXT_ARP: t = "arp"; break;
226     case IP_LOOKUP_NEXT_MIDCHAIN: t="midchain"; break;
227     case IP_LOOKUP_NEXT_GLEAN: t="glean"; break;
228     case IP_LOOKUP_NEXT_REWRITE:
229       break;
230     }
231
232   if (t)
233     vec_add (s, t, strlen (t));
234
235   return s;
236 }
237
238 u8 * format_ip_adjacency_packet_data (u8 * s, va_list * args)
239 {
240   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
241   u32 adj_index = va_arg (*args, u32);
242   u8 * packet_data = va_arg (*args, u8 *);
243   u32 n_packet_data_bytes = va_arg (*args, u32);
244   ip_adjacency_t * adj = adj_get(adj_index);
245
246   switch (adj->lookup_next_index)
247     {
248     case IP_LOOKUP_NEXT_REWRITE:
249       s = format (s, "%U",
250                   format_vnet_rewrite_header,
251                   vnm->vlib_main, &adj->rewrite_header, packet_data, n_packet_data_bytes);
252       break;
253
254     default:
255       break;
256     }
257
258   return s;
259 }
260
261 static uword unformat_dpo (unformat_input_t * input, va_list * args)
262 {
263   dpo_id_t *dpo = va_arg (*args, dpo_id_t *);
264   fib_protocol_t fp = va_arg (*args, int);
265   dpo_proto_t proto;
266
267   proto = fib_proto_to_dpo(fp);
268
269   if (unformat (input, "drop"))
270     dpo_copy(dpo, drop_dpo_get(proto));
271   else if (unformat (input, "punt"))
272     dpo_copy(dpo, punt_dpo_get(proto));
273   else if (unformat (input, "local"))
274     receive_dpo_add_or_lock(proto, ~0, NULL, dpo);
275   else if (unformat (input, "classify"))
276     {
277       u32 classify_table_index;
278
279       if (!unformat (input, "%d", &classify_table_index))
280         {
281           clib_warning ("classify adj must specify table index");
282           return 0;
283         }
284
285       dpo_set(dpo, DPO_CLASSIFY, proto,
286               classify_dpo_create(fp, classify_table_index));
287     }
288   else
289     return 0;
290
291   return 1;
292 }
293
294 const ip46_address_t zero_addr = {
295     .as_u64 = {
296         0, 0
297     },
298 };
299
300 u32
301 fib_table_id_find_fib_index (fib_protocol_t proto,
302                              u32 table_id)
303 {
304     ip4_main_t *im4 = &ip4_main;
305     ip6_main_t *im6 = &ip6_main;
306     uword * p;
307
308     switch (proto)
309     {
310     case FIB_PROTOCOL_IP4:
311         p = hash_get(im4->fib_index_by_table_id, table_id);
312         break;
313     case FIB_PROTOCOL_IP6:
314         p = hash_get(im6->fib_index_by_table_id, table_id);
315         break;
316     default:
317         p = NULL;
318         break;
319     }
320     if (NULL != p)
321     {
322         return (p[0]);
323     }
324     return (~0);
325 }
326
327 clib_error_t *
328 vnet_ip_route_cmd (vlib_main_t * vm,
329                    unformat_input_t * main_input,
330                    vlib_cli_command_t * cmd)
331 {
332   unformat_input_t _line_input, * line_input = &_line_input;
333   fib_route_path_t *rpaths = NULL, rpath;
334   dpo_id_t dpo = DPO_NULL, *dpos = NULL;
335   fib_prefix_t *prefixs = NULL, pfx;
336   clib_error_t * error = NULL;
337   mpls_label_t out_label;
338   u32 table_id, is_del;
339   vnet_main_t * vnm;
340   u32 fib_index;
341   f64 count;
342   int i;
343
344   vnm = vnet_get_main();
345   is_del = 0;
346   table_id = 0;
347   count = 1;
348
349   /* Get a line of input. */
350   if (! unformat_user (main_input, unformat_line_input, line_input))
351     return 0;
352
353   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
354     {
355       memset(&rpath, 0, sizeof(rpath));
356       memset(&pfx, 0, sizeof(pfx));
357
358       if (unformat (line_input, "table %d", &table_id))
359         ;
360       else if (unformat (line_input, "del"))
361         is_del = 1;
362       else if (unformat (line_input, "add"))
363         is_del = 0;
364       else if (unformat (line_input, "resolve-via-host"))
365       {
366           if (vec_len(rpaths) == 0)
367           {
368               error = clib_error_return(0 , "Paths then flags");
369               goto done;
370           }
371           rpaths[vec_len(rpaths)-1].frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
372       }
373       else if (unformat (line_input, "resolve-via-attached"))
374       {
375           if (vec_len(rpaths) == 0)
376           {
377               error = clib_error_return(0 , "Paths then flags");
378               goto done;
379           }
380           rpaths[vec_len(rpaths)-1].frp_flags |=
381               FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
382       }
383       else if (unformat (line_input, "out-label %U",
384                          unformat_mpls_unicast_label, &out_label))
385       {
386           if (vec_len(rpaths) == 0)
387           {
388               error = clib_error_return(0 , "Paths then labels");
389               goto done;
390           }
391           rpaths[vec_len(rpaths)-1].frp_label = out_label;
392       }
393       else if (unformat (line_input, "count %f", &count))
394         ;
395
396       else if (unformat (line_input, "%U/%d",
397                          unformat_ip4_address,
398                          &pfx.fp_addr.ip4,
399                          &pfx.fp_len))
400       {
401           pfx.fp_proto = FIB_PROTOCOL_IP4;
402           vec_add1(prefixs, pfx);
403       }
404       else if (unformat (line_input, "%U/%d",
405                          unformat_ip6_address,
406                          &pfx.fp_addr.ip6,
407                          &pfx.fp_len))
408       {
409           pfx.fp_proto = FIB_PROTOCOL_IP6;
410           vec_add1(prefixs, pfx);
411       }
412       else if (unformat (line_input, "via %U %U weight %u",
413                          unformat_ip4_address,
414                          &rpath.frp_addr.ip4,
415                          unformat_vnet_sw_interface, vnm,
416                          &rpath.frp_sw_if_index,
417                          &rpath.frp_weight))
418       {
419           rpath.frp_label = MPLS_LABEL_INVALID;
420           rpath.frp_proto = FIB_PROTOCOL_IP4;
421           vec_add1(rpaths, rpath);
422       }
423
424       else if (unformat (line_input, "via %U %U weight %u",
425                          unformat_ip6_address,
426                          &rpath.frp_addr.ip6,
427                          unformat_vnet_sw_interface, vnm,
428                          &rpath.frp_sw_if_index,
429                          &rpath.frp_weight))
430       {
431           rpath.frp_label = MPLS_LABEL_INVALID;
432           rpath.frp_proto = FIB_PROTOCOL_IP6;
433           vec_add1(rpaths, rpath);
434       }
435
436       else if (unformat (line_input, "via %U %U",
437                          unformat_ip4_address,
438                          &rpath.frp_addr.ip4,
439                          unformat_vnet_sw_interface, vnm,
440                          &rpath.frp_sw_if_index))
441       {
442           rpath.frp_label = MPLS_LABEL_INVALID;
443           rpath.frp_weight = 1;
444           rpath.frp_proto = FIB_PROTOCOL_IP4;
445           vec_add1(rpaths, rpath);
446       }
447                          
448       else if (unformat (line_input, "via %U %U",
449                          unformat_ip6_address,
450                          &rpath.frp_addr.ip6,
451                          unformat_vnet_sw_interface, vnm,
452                          &rpath.frp_sw_if_index))
453       {
454           rpath.frp_label = MPLS_LABEL_INVALID;
455           rpath.frp_weight = 1;
456           rpath.frp_proto = FIB_PROTOCOL_IP6;
457           vec_add1(rpaths, rpath);
458       }
459       else if (unformat (line_input, "via %U next-hop-table %d",
460                          unformat_ip4_address,
461                          &rpath.frp_addr.ip4,
462                          &rpath.frp_fib_index))
463       {
464           rpath.frp_weight = 1;
465           rpath.frp_sw_if_index = ~0;
466           rpath.frp_label = MPLS_LABEL_INVALID;
467           rpath.frp_proto = FIB_PROTOCOL_IP4;
468           vec_add1(rpaths, rpath);
469       }
470       else if (unformat (line_input, "via %U next-hop-table %d",
471                          unformat_ip6_address,
472                          &rpath.frp_addr.ip6,
473                          &rpath.frp_fib_index))
474       {
475           rpath.frp_weight = 1;
476           rpath.frp_sw_if_index = ~0;
477           rpath.frp_label = MPLS_LABEL_INVALID;
478           rpath.frp_proto = FIB_PROTOCOL_IP6;
479           vec_add1(rpaths, rpath);
480       }
481       else if (unformat (line_input, "via %U",
482                          unformat_ip4_address,
483                          &rpath.frp_addr.ip4))
484       {
485           /*
486            * the recursive next-hops are by default in the same table
487            * as the prefix
488            */
489           rpath.frp_fib_index = table_id;
490           rpath.frp_weight = 1;
491           rpath.frp_sw_if_index = ~0;
492           rpath.frp_label = MPLS_LABEL_INVALID;
493           rpath.frp_proto = FIB_PROTOCOL_IP4;
494           vec_add1(rpaths, rpath);
495       }
496       else if (unformat (line_input, "via %U",
497                          unformat_ip6_address,
498                          &rpath.frp_addr.ip6))
499       {
500           rpath.frp_fib_index = table_id;
501           rpath.frp_weight = 1;
502           rpath.frp_sw_if_index = ~0;
503           rpath.frp_label = MPLS_LABEL_INVALID;
504           rpath.frp_proto = FIB_PROTOCOL_IP6;
505           vec_add1(rpaths, rpath);
506       }
507       else if (unformat (line_input,
508                          "lookup in table %d",
509                          &rpath.frp_fib_index))
510       {
511           rpath.frp_label = MPLS_LABEL_INVALID;
512           rpath.frp_proto = pfx.fp_proto;
513           vec_add1(rpaths, rpath);
514       }
515       else if (vec_len (prefixs) > 0 &&
516                unformat (line_input, "via %U",
517                          unformat_dpo, &dpo, prefixs[0].fp_proto))
518       {
519           rpath.frp_label = MPLS_LABEL_INVALID;
520           vec_add1 (dpos, dpo);
521       }
522       else
523       {
524           error = unformat_parse_error (line_input);
525           goto done;
526       }
527     }
528     
529   unformat_free (line_input);
530
531   if (vec_len (prefixs) == 0)
532   {
533       error = clib_error_return (0, "expected ip4/ip6 destination address/length.");
534       goto done;
535     }
536
537   if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0)
538     {
539       error = clib_error_return (0, "expected paths.");
540       goto done;
541     }
542
543   if (~0 == table_id)
544   {
545       /*
546        * if no table_id is passed we will manipulate the default
547        */
548       fib_index = 0;
549   }
550   else
551   {
552       fib_index = fib_table_id_find_fib_index(prefixs[0].fp_proto,
553                                               table_id);
554
555       if (~0 == fib_index)
556       {
557           error = clib_error_return (0,
558                                      "Nonexistent table id %d", 
559                                      table_id);
560           goto done;
561       }
562   }
563
564   for (i = 0; i < vec_len (prefixs); i++)
565   {
566       if (is_del && 0 == vec_len (rpaths))
567       {
568           fib_table_entry_delete(fib_index,
569                                  &prefixs[i],
570                                  FIB_SOURCE_CLI);
571       }
572       else if (!is_del && 1 == vec_len (dpos))
573       {
574           fib_table_entry_special_dpo_add(fib_index,
575                                           &prefixs[i],
576                                           FIB_SOURCE_CLI,
577                                           FIB_ENTRY_FLAG_EXCLUSIVE,
578                                           &dpos[0]);
579           dpo_reset(&dpos[0]);
580       }
581       else if (vec_len (dpos) > 0)
582       {
583           error = clib_error_return(0 , "Load-balancing over multiple special adjacencies is unsupported");
584           goto done;
585       }
586       else if (0 < vec_len (rpaths))
587       {
588           u32 k, j, n, incr;
589           ip46_address_t dst = prefixs[i].fp_addr;
590           f64 t[2];
591           n = count;
592           t[0] = vlib_time_now (vm);
593           incr = 1 << ((FIB_PROTOCOL_IP4 == prefixs[0].fp_proto ? 32 : 128) -
594                        prefixs[i].fp_len);
595
596           for (k = 0; k < n; k++)
597           {
598               for (j = 0; j < vec_len (rpaths); j++)
599               {
600                   /*
601                    * the CLI parsing stored table Ids, swap to FIB indicies
602                    */
603                   rpaths[i].frp_fib_index =
604                       fib_table_id_find_fib_index(prefixs[i].fp_proto,
605                                                   rpaths[i].frp_fib_index);
606
607                   fib_prefix_t rpfx = {
608                       .fp_len = prefixs[i].fp_len,
609                       .fp_proto = prefixs[i].fp_proto,
610                       .fp_addr = dst,
611                   };
612
613                   if (is_del)
614                       fib_table_entry_path_remove2(fib_index,
615                                                    &rpfx,
616                                                    FIB_SOURCE_CLI,
617                                                    &rpaths[j]);
618                   else
619                       fib_table_entry_path_add2(fib_index,
620                                                 &rpfx,
621                                                 FIB_SOURCE_CLI,
622                                                 FIB_ENTRY_FLAG_NONE,
623                                                 &rpaths[j]);
624               }
625
626               if (FIB_PROTOCOL_IP4 == prefixs[0].fp_proto)
627               {
628                   dst.ip4.as_u32 =
629                       clib_host_to_net_u32(incr +
630                                            clib_net_to_host_u32 (dst.ip4.as_u32));
631               }
632               else
633               {
634                   int bucket = (incr < 64 ? 0 : 1);
635                   dst.ip6.as_u64[bucket] =
636                       clib_host_to_net_u64(incr +
637                                            clib_net_to_host_u64 (
638                                                dst.ip6.as_u64[bucket]));
639
640               }
641           }
642           t[1] = vlib_time_now (vm);
643           if (count > 1)
644               vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
645       }
646       else
647       {
648           error = clib_error_return(0 , "Don't understand what you want...");
649           goto done;
650       }
651   }
652
653
654  done:
655   vec_free (dpos);
656   vec_free (prefixs);
657   vec_free (rpaths);
658   return error;
659 }
660
661 VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
662   .path = "ip",
663   .short_help = "Internet protocol (IP) commands",
664 };
665
666 VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
667   .path = "show ip",
668   .short_help = "Internet protocol (IP) show commands",
669 };
670
671 VLIB_CLI_COMMAND (vlib_cli_show_ip4_command, static) = {
672   .path = "show ip4",
673   .short_help = "Internet protocol version 4 (IP4) show commands",
674 };
675
676 VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
677   .path = "show ip6",
678   .short_help = "Internet protocol version 6 (IP6) show commands",
679 };
680
681 /*?
682  * To add or delete routes, use ip route add / del
683  * @cliexpar
684  * @cliexstart{ip route}
685  * To add or delete straightforward static routes, use ip route add / del:
686  *  vpp# ip route add 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0
687  *  vpp# ip route del 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0
688  *
689  * Multiple routes
690  *
691  * Mainly for route add/del performance testing, one can add or delete
692  * multiple routes by adding 'count N' to the previous item:
693  *  vpp# ip route add count 10 7.0.0.0/24 via 6.0.0.1 GigabitEthernet2/0/0
694  *
695  * Multipath
696  *
697  * Add multiple routes for the same destination to create equal-cost multipath:
698  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0
699  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0
700  *
701  * For unequal-cost multipath, specify the desired weights:
702  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0 weight 1
703  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0 weight 3
704  *
705  * This combination of weights results in 3/4 of the traffic following the second path, 1/4 following the first path.
706  * @cliexend
707  ?*/
708 VLIB_CLI_COMMAND (ip_route_command, static) = {
709   .path = "ip route",
710   .short_help = "Add/delete IP routes",
711   .function = vnet_ip_route_cmd,
712   .is_mp_safe = 1,
713 };
714
715 /*
716  * The next two routines address a longstanding script hemorrhoid.
717  * Probing a v4 or v6 neighbor needs to appear to be synchronous,
718  * or dependent route-adds will simply fail.
719  */
720 static clib_error_t *
721 ip6_probe_neighbor_wait (vlib_main_t *vm, ip6_address_t * a, u32 sw_if_index,
722                          int retry_count)
723 {
724   vnet_main_t * vnm = vnet_get_main();
725   clib_error_t * e;
726   int i;
727   int resolved = 0;
728   uword event_type;
729   uword *event_data = 0;
730
731   ASSERT (vlib_in_process_context(vm));
732
733   if (retry_count > 0)
734     vnet_register_ip6_neighbor_resolution_event
735       (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
736        1 /* event */, 0 /* data */);
737
738   for (i = 0; i < retry_count; i++)
739     {
740       /* The interface may be down, etc. */
741       e = ip6_probe_neighbor (vm, a, sw_if_index);
742
743       if (e)
744         return e;
745
746       vlib_process_wait_for_event_or_clock (vm, 1.0);
747       event_type = vlib_process_get_events (vm, &event_data);
748       switch (event_type)
749         {
750         case 1: /* resolved... */
751           vlib_cli_output (vm, "Resolved %U",
752                            format_ip6_address, a);
753           resolved = 1;
754           goto done;
755           
756         case ~0: /* timeout */
757           break;
758           
759         default:
760           clib_warning ("unknown event_type %d", event_type);
761         }
762       vec_reset_length (event_data);
763     }
764   
765  done:
766
767   if (!resolved)
768     return clib_error_return (0, "Resolution failed for %U",
769                               format_ip6_address, a);
770   return 0;
771 }
772
773 static clib_error_t *
774 ip4_probe_neighbor_wait (vlib_main_t *vm, ip4_address_t * a, u32 sw_if_index,
775                          int retry_count)
776 {
777   vnet_main_t * vnm = vnet_get_main();
778   clib_error_t * e;
779   int i;
780   int resolved = 0;
781   uword event_type;
782   uword *event_data = 0;
783
784   ASSERT (vlib_in_process_context(vm));
785
786   if (retry_count > 0)
787     vnet_register_ip4_arp_resolution_event 
788       (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
789        1 /* event */, 0 /* data */);
790   
791   for (i = 0; i < retry_count; i++)
792     {
793       /* The interface may be down, etc. */
794       e = ip4_probe_neighbor (vm, a, sw_if_index);
795       
796       if (e)
797         return e;
798       
799       vlib_process_wait_for_event_or_clock (vm, 1.0);
800       event_type = vlib_process_get_events (vm, &event_data);
801       switch (event_type) 
802         {
803         case 1: /* resolved... */
804           vlib_cli_output (vm, "Resolved %U", 
805                            format_ip4_address, a);
806           resolved = 1;
807           goto done;
808           
809         case ~0: /* timeout */
810           break;
811           
812         default:
813           clib_warning ("unknown event_type %d", event_type);
814         }
815       vec_reset_length (event_data);
816     }
817   
818  done:
819
820   vec_reset_length (event_data);
821
822   if (!resolved)
823     return clib_error_return (0, "Resolution failed for %U",
824                               format_ip4_address, a);
825   return 0;
826 }
827
828 static clib_error_t *
829 probe_neighbor_address (vlib_main_t * vm,
830                         unformat_input_t * input,
831                         vlib_cli_command_t * cmd)
832 {
833   vnet_main_t * vnm = vnet_get_main();
834   unformat_input_t _line_input, * line_input = &_line_input;
835   ip4_address_t a4;
836   ip6_address_t a6;
837   clib_error_t * error = 0;
838   u32 sw_if_index = ~0;
839   int retry_count = 3;
840   int is_ip4 = 1;
841   int address_set = 0;
842
843   /* Get a line of input. */
844   if (! unformat_user (input, unformat_line_input, line_input))
845     return 0;
846
847   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) 
848     {
849       if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, 
850                          &sw_if_index))
851         ;
852       else if (unformat (line_input, "retry %d", &retry_count))
853         ;
854
855       else if (unformat (line_input, "%U", unformat_ip4_address, &a4))
856         address_set++;
857       else if (unformat (line_input, "%U", unformat_ip6_address, &a6))
858         {
859           address_set++;
860           is_ip4 = 0;
861         }
862       else
863         return clib_error_return (0, "unknown input '%U'",
864                                   format_unformat_error, line_input);
865     }
866
867   unformat_free (line_input);
868
869   if (sw_if_index == ~0)
870     return clib_error_return (0, "Interface required, not set.");
871   if (address_set == 0)
872     return clib_error_return (0, "ip address required, not set.");
873   if (address_set > 1)
874     return clib_error_return (0, "Multiple ip addresses not supported.");
875     
876   if (is_ip4)
877     error = ip4_probe_neighbor_wait (vm, &a4, sw_if_index, retry_count);
878   else 
879     error = ip6_probe_neighbor_wait (vm, &a6, sw_if_index, retry_count);
880
881   return error;
882 }
883
884 VLIB_CLI_COMMAND (ip_probe_neighbor_command, static) = {
885   .path = "ip probe-neighbor",
886   .function = probe_neighbor_address,
887   .short_help = "ip probe-neighbor <intfc> <ip4-addr> | <ip6-addr> [retry nn]",
888   .is_mp_safe = 1,
889 };