FIB Memory Usage Diagnostics
[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 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_pool;
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   memset(&pfx, 0, sizeof(pfx));
349
350   /* Get a line of input. */
351   if (! unformat_user (main_input, unformat_line_input, line_input))
352     return 0;
353
354   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
355     {
356       memset(&rpath, 0, sizeof(rpath));
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           rpath.frp_sw_if_index = ~0;
514           vec_add1(rpaths, rpath);
515       }
516       else if (vec_len (prefixs) > 0 &&
517                unformat (line_input, "via %U",
518                          unformat_dpo, &dpo, prefixs[0].fp_proto))
519       {
520           rpath.frp_label = MPLS_LABEL_INVALID;
521           vec_add1 (dpos, dpo);
522       }
523       else
524       {
525           error = unformat_parse_error (line_input);
526           goto done;
527       }
528     }
529     
530   unformat_free (line_input);
531
532   if (vec_len (prefixs) == 0)
533   {
534       error = clib_error_return (0, "expected ip4/ip6 destination address/length.");
535       goto done;
536     }
537
538   if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0)
539     {
540       error = clib_error_return (0, "expected paths.");
541       goto done;
542     }
543
544   if (~0 == table_id)
545   {
546       /*
547        * if no table_id is passed we will manipulate the default
548        */
549       fib_index = 0;
550   }
551   else
552   {
553       fib_index = fib_table_id_find_fib_index(prefixs[0].fp_proto,
554                                               table_id);
555
556       if (~0 == fib_index)
557       {
558           error = clib_error_return (0,
559                                      "Nonexistent table id %d", 
560                                      table_id);
561           goto done;
562       }
563   }
564
565   for (i = 0; i < vec_len (prefixs); i++)
566   {
567       if (is_del && 0 == vec_len (rpaths))
568       {
569           fib_table_entry_delete(fib_index,
570                                  &prefixs[i],
571                                  FIB_SOURCE_CLI);
572       }
573       else if (!is_del && 1 == vec_len (dpos))
574       {
575           fib_table_entry_special_dpo_add(fib_index,
576                                           &prefixs[i],
577                                           FIB_SOURCE_CLI,
578                                           FIB_ENTRY_FLAG_EXCLUSIVE,
579                                           &dpos[0]);
580           dpo_reset(&dpos[0]);
581       }
582       else if (vec_len (dpos) > 0)
583       {
584           error = clib_error_return(0 , "Load-balancing over multiple special adjacencies is unsupported");
585           goto done;
586       }
587       else if (0 < vec_len (rpaths))
588       {
589           u32 k, j, n, incr;
590           ip46_address_t dst = prefixs[i].fp_addr;
591           f64 t[2];
592           n = count;
593           t[0] = vlib_time_now (vm);
594           incr = 1 << ((FIB_PROTOCOL_IP4 == prefixs[0].fp_proto ? 32 : 128) -
595                        prefixs[i].fp_len);
596
597           for (k = 0; k < n; k++)
598           {
599               for (j = 0; j < vec_len (rpaths); j++)
600               {
601                   /*
602                    * the CLI parsing stored table Ids, swap to FIB indicies
603                    */
604                   rpaths[i].frp_fib_index =
605                       fib_table_id_find_fib_index(prefixs[i].fp_proto,
606                                                   rpaths[i].frp_fib_index);
607
608                   fib_prefix_t rpfx = {
609                       .fp_len = prefixs[i].fp_len,
610                       .fp_proto = prefixs[i].fp_proto,
611                       .fp_addr = dst,
612                   };
613
614                   if (is_del)
615                       fib_table_entry_path_remove2(fib_index,
616                                                    &rpfx,
617                                                    FIB_SOURCE_CLI,
618                                                    &rpaths[j]);
619                   else
620                       fib_table_entry_path_add2(fib_index,
621                                                 &rpfx,
622                                                 FIB_SOURCE_CLI,
623                                                 FIB_ENTRY_FLAG_NONE,
624                                                 &rpaths[j]);
625               }
626
627               if (FIB_PROTOCOL_IP4 == prefixs[0].fp_proto)
628               {
629                   dst.ip4.as_u32 =
630                       clib_host_to_net_u32(incr +
631                                            clib_net_to_host_u32 (dst.ip4.as_u32));
632               }
633               else
634               {
635                   int bucket = (incr < 64 ? 0 : 1);
636                   dst.ip6.as_u64[bucket] =
637                       clib_host_to_net_u64(incr +
638                                            clib_net_to_host_u64 (
639                                                dst.ip6.as_u64[bucket]));
640
641               }
642           }
643           t[1] = vlib_time_now (vm);
644           if (count > 1)
645               vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
646       }
647       else
648       {
649           error = clib_error_return(0 , "Don't understand what you want...");
650           goto done;
651       }
652   }
653
654
655  done:
656   vec_free (dpos);
657   vec_free (prefixs);
658   vec_free (rpaths);
659   return error;
660 }
661
662 VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
663   .path = "ip",
664   .short_help = "Internet protocol (IP) commands",
665 };
666
667 VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
668   .path = "show ip",
669   .short_help = "Internet protocol (IP) show commands",
670 };
671
672 VLIB_CLI_COMMAND (vlib_cli_show_ip4_command, static) = {
673   .path = "show ip4",
674   .short_help = "Internet protocol version 4 (IP4) show commands",
675 };
676
677 VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
678   .path = "show ip6",
679   .short_help = "Internet protocol version 6 (IP6) show commands",
680 };
681
682 /*?
683  * To add or delete routes, use ip route add / del
684  * @cliexpar
685  * @cliexstart{ip route}
686  * To add or delete straightforward static routes, use ip route add / del:
687  *  vpp# ip route add 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0
688  *  vpp# ip route del 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0
689  *
690  * Multiple routes
691  *
692  * Mainly for route add/del performance testing, one can add or delete
693  * multiple routes by adding 'count N' to the previous item:
694  *  vpp# ip route add count 10 7.0.0.0/24 via 6.0.0.1 GigabitEthernet2/0/0
695  *
696  * Multipath
697  *
698  * Add multiple routes for the same destination to create equal-cost multipath:
699  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0
700  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0
701  *
702  * For unequal-cost multipath, specify the desired weights:
703  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0 weight 1
704  *  vpp# ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0 weight 3
705  *
706  * This combination of weights results in 3/4 of the traffic following the second path, 1/4 following the first path.
707  * @cliexend
708  ?*/
709 VLIB_CLI_COMMAND (ip_route_command, static) = {
710   .path = "ip route",
711   .short_help = "Add/delete IP routes",
712   .function = vnet_ip_route_cmd,
713   .is_mp_safe = 1,
714 };
715
716 /*
717  * The next two routines address a longstanding script hemorrhoid.
718  * Probing a v4 or v6 neighbor needs to appear to be synchronous,
719  * or dependent route-adds will simply fail.
720  */
721 static clib_error_t *
722 ip6_probe_neighbor_wait (vlib_main_t *vm, ip6_address_t * a, u32 sw_if_index,
723                          int retry_count)
724 {
725   vnet_main_t * vnm = vnet_get_main();
726   clib_error_t * e;
727   int i;
728   int resolved = 0;
729   uword event_type;
730   uword *event_data = 0;
731
732   ASSERT (vlib_in_process_context(vm));
733
734   if (retry_count > 0)
735     vnet_register_ip6_neighbor_resolution_event
736       (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
737        1 /* event */, 0 /* data */);
738
739   for (i = 0; i < retry_count; i++)
740     {
741       /* The interface may be down, etc. */
742       e = ip6_probe_neighbor (vm, a, sw_if_index);
743
744       if (e)
745         return e;
746
747       vlib_process_wait_for_event_or_clock (vm, 1.0);
748       event_type = vlib_process_get_events (vm, &event_data);
749       switch (event_type)
750         {
751         case 1: /* resolved... */
752           vlib_cli_output (vm, "Resolved %U",
753                            format_ip6_address, a);
754           resolved = 1;
755           goto done;
756           
757         case ~0: /* timeout */
758           break;
759           
760         default:
761           clib_warning ("unknown event_type %d", event_type);
762         }
763       vec_reset_length (event_data);
764     }
765   
766  done:
767
768   if (!resolved)
769     return clib_error_return (0, "Resolution failed for %U",
770                               format_ip6_address, a);
771   return 0;
772 }
773
774 static clib_error_t *
775 ip4_probe_neighbor_wait (vlib_main_t *vm, ip4_address_t * a, u32 sw_if_index,
776                          int retry_count)
777 {
778   vnet_main_t * vnm = vnet_get_main();
779   clib_error_t * e;
780   int i;
781   int resolved = 0;
782   uword event_type;
783   uword *event_data = 0;
784
785   ASSERT (vlib_in_process_context(vm));
786
787   if (retry_count > 0)
788     vnet_register_ip4_arp_resolution_event 
789       (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
790        1 /* event */, 0 /* data */);
791   
792   for (i = 0; i < retry_count; i++)
793     {
794       /* The interface may be down, etc. */
795       e = ip4_probe_neighbor (vm, a, sw_if_index);
796       
797       if (e)
798         return e;
799       
800       vlib_process_wait_for_event_or_clock (vm, 1.0);
801       event_type = vlib_process_get_events (vm, &event_data);
802       switch (event_type) 
803         {
804         case 1: /* resolved... */
805           vlib_cli_output (vm, "Resolved %U", 
806                            format_ip4_address, a);
807           resolved = 1;
808           goto done;
809           
810         case ~0: /* timeout */
811           break;
812           
813         default:
814           clib_warning ("unknown event_type %d", event_type);
815         }
816       vec_reset_length (event_data);
817     }
818   
819  done:
820
821   vec_reset_length (event_data);
822
823   if (!resolved)
824     return clib_error_return (0, "Resolution failed for %U",
825                               format_ip4_address, a);
826   return 0;
827 }
828
829 static clib_error_t *
830 probe_neighbor_address (vlib_main_t * vm,
831                         unformat_input_t * input,
832                         vlib_cli_command_t * cmd)
833 {
834   vnet_main_t * vnm = vnet_get_main();
835   unformat_input_t _line_input, * line_input = &_line_input;
836   ip4_address_t a4;
837   ip6_address_t a6;
838   clib_error_t * error = 0;
839   u32 sw_if_index = ~0;
840   int retry_count = 3;
841   int is_ip4 = 1;
842   int address_set = 0;
843
844   /* Get a line of input. */
845   if (! unformat_user (input, unformat_line_input, line_input))
846     return 0;
847
848   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) 
849     {
850       if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, 
851                          &sw_if_index))
852         ;
853       else if (unformat (line_input, "retry %d", &retry_count))
854         ;
855
856       else if (unformat (line_input, "%U", unformat_ip4_address, &a4))
857         address_set++;
858       else if (unformat (line_input, "%U", unformat_ip6_address, &a6))
859         {
860           address_set++;
861           is_ip4 = 0;
862         }
863       else
864         return clib_error_return (0, "unknown input '%U'",
865                                   format_unformat_error, line_input);
866     }
867
868   unformat_free (line_input);
869
870   if (sw_if_index == ~0)
871     return clib_error_return (0, "Interface required, not set.");
872   if (address_set == 0)
873     return clib_error_return (0, "ip address required, not set.");
874   if (address_set > 1)
875     return clib_error_return (0, "Multiple ip addresses not supported.");
876     
877   if (is_ip4)
878     error = ip4_probe_neighbor_wait (vm, &a4, sw_if_index, retry_count);
879   else 
880     error = ip6_probe_neighbor_wait (vm, &a6, sw_if_index, retry_count);
881
882   return error;
883 }
884
885 VLIB_CLI_COMMAND (ip_probe_neighbor_command, static) = {
886   .path = "ip probe-neighbor",
887   .function = probe_neighbor_address,
888   .short_help = "ip probe-neighbor <intfc> <ip4-addr> | <ip6-addr> [retry nn]",
889   .is_mp_safe = 1,
890 };