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