FIB path preference
[vpp.git] / src / 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/mfib/mfib_table.h>
47 #include <vnet/dpo/drop_dpo.h>
48 #include <vnet/dpo/classify_dpo.h>
49 #include <vnet/dpo/punt_dpo.h>
50 #include <vnet/dpo/receive_dpo.h>
51 #include <vnet/dpo/ip_null_dpo.h>
52 #include <vnet/ip/ip6_neighbor.h>
53
54 /**
55  * @file
56  * @brief IPv4 and IPv6 adjacency and lookup table managment.
57  *
58  */
59
60 clib_error_t *
61 ip_interface_address_add_del (ip_lookup_main_t * lm,
62                               u32 sw_if_index,
63                               void *addr_fib,
64                               u32 address_length,
65                               u32 is_del, u32 * result_if_address_index)
66 {
67   vnet_main_t *vnm = vnet_get_main ();
68   ip_interface_address_t *a, *prev, *next;
69   uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
70
71   vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
72                            sw_if_index, ~0);
73   a = p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
74
75   /* Verify given length. */
76   if ((a && (address_length != a->address_length)) ||
77       (address_length == 0) ||
78       (lm->is_ip6 && address_length > 128) ||
79       (!lm->is_ip6 && address_length > 32))
80     {
81       vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
82       return clib_error_create
83         ("%U wrong length (expected %d) for interface %U",
84          lm->format_address_and_length, addr_fib,
85          address_length, a ? a->address_length : -1,
86          format_vnet_sw_if_index_name, vnm, sw_if_index);
87     }
88
89   if (is_del)
90     {
91       if (!a)
92         {
93           vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
94           vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
95           return clib_error_create ("%U not found for interface %U",
96                                     lm->format_address_and_length,
97                                     addr_fib, address_length,
98                                     format_vnet_sw_interface_name, vnm, si);
99         }
100
101       if (a->prev_this_sw_interface != ~0)
102         {
103           prev =
104             pool_elt_at_index (lm->if_address_pool,
105                                a->prev_this_sw_interface);
106           prev->next_this_sw_interface = a->next_this_sw_interface;
107         }
108       if (a->next_this_sw_interface != ~0)
109         {
110           next =
111             pool_elt_at_index (lm->if_address_pool,
112                                a->next_this_sw_interface);
113           next->prev_this_sw_interface = a->prev_this_sw_interface;
114
115           if (a->prev_this_sw_interface == ~0)
116             lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
117               a->next_this_sw_interface;
118         }
119
120       if ((a->next_this_sw_interface == ~0)
121           && (a->prev_this_sw_interface == ~0))
122         lm->if_address_pool_index_by_sw_if_index[sw_if_index] = ~0;
123
124       mhash_unset (&lm->address_to_if_address_index, addr_fib,
125                    /* old_value */ 0);
126       pool_put (lm->if_address_pool, a);
127
128       if (result_if_address_index)
129         *result_if_address_index = ~0;
130     }
131
132   else if (!a)
133     {
134       u32 pi;                   /* previous index */
135       u32 ai;
136       u32 hi;                   /* head index */
137
138       pool_get (lm->if_address_pool, a);
139       memset (a, ~0, sizeof (a[0]));
140       ai = a - lm->if_address_pool;
141
142       hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
143       prev = 0;
144       while (pi != (u32) ~ 0)
145         {
146           prev = pool_elt_at_index (lm->if_address_pool, pi);
147           pi = prev->next_this_sw_interface;
148         }
149       pi = prev ? prev - lm->if_address_pool : (u32) ~ 0;
150
151       a->address_key = mhash_set (&lm->address_to_if_address_index,
152                                   addr_fib, ai, /* old_value */ 0);
153       a->address_length = address_length;
154       a->sw_if_index = sw_if_index;
155       a->flags = 0;
156       a->prev_this_sw_interface = pi;
157       a->next_this_sw_interface = ~0;
158       if (prev)
159         prev->next_this_sw_interface = ai;
160
161       lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
162         (hi != ~0) ? hi : ai;
163       if (result_if_address_index)
164         *result_if_address_index = ai;
165     }
166   else
167     {
168       if (result_if_address_index)
169         *result_if_address_index = a - lm->if_address_pool;
170     }
171
172
173   return /* no error */ 0;
174 }
175
176 static clib_error_t *
177 ip_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
178 {
179   vec_validate_init_empty (ip4_main.
180                            lookup_main.if_address_pool_index_by_sw_if_index,
181                            sw_if_index, ~0);
182   vec_validate_init_empty (ip6_main.
183                            lookup_main.if_address_pool_index_by_sw_if_index,
184                            sw_if_index, ~0);
185
186   return (NULL);
187 }
188
189 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_sw_interface_add_del);
190
191 void
192 ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
193 {
194   if (!lm->fib_result_n_bytes)
195     lm->fib_result_n_bytes = sizeof (uword);
196
197   lm->is_ip6 = is_ip6;
198   if (is_ip6)
199     {
200       lm->format_address_and_length = format_ip6_address_and_length;
201       mhash_init (&lm->address_to_if_address_index, sizeof (uword),
202                   sizeof (ip6_address_fib_t));
203     }
204   else
205     {
206       lm->format_address_and_length = format_ip4_address_and_length;
207       mhash_init (&lm->address_to_if_address_index, sizeof (uword),
208                   sizeof (ip4_address_fib_t));
209     }
210
211   {
212     int i;
213
214     /* Setup all IP protocols to be punted and builtin-unknown. */
215     for (i = 0; i < 256; i++)
216       {
217         lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
218         lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
219       }
220
221     lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
222     lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 :
223                                   IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
224     lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] =
225       IP_BUILTIN_PROTOCOL_UDP;
226     lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 :
227                                         IP_PROTOCOL_ICMP] =
228       IP_BUILTIN_PROTOCOL_ICMP;
229   }
230 }
231
232 u8 *
233 format_ip_flow_hash_config (u8 * s, va_list * args)
234 {
235   flow_hash_config_t flow_hash_config = va_arg (*args, u32);
236
237 #define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
238   foreach_flow_hash_bit;
239 #undef _
240
241   return s;
242 }
243
244 u8 *
245 format_ip_lookup_next (u8 * s, va_list * args)
246 {
247   /* int promotion of ip_lookup_next_t */
248   ip_lookup_next_t n = va_arg (*args, int);
249   char *t = 0;
250
251   switch (n)
252     {
253     default:
254       s = format (s, "unknown %d", n);
255       return s;
256
257     case IP_LOOKUP_NEXT_DROP:
258       t = "drop";
259       break;
260     case IP_LOOKUP_NEXT_PUNT:
261       t = "punt";
262       break;
263     case IP_LOOKUP_NEXT_ARP:
264       t = "arp";
265       break;
266     case IP_LOOKUP_NEXT_MIDCHAIN:
267       t = "midchain";
268       break;
269     case IP_LOOKUP_NEXT_GLEAN:
270       t = "glean";
271       break;
272     case IP_LOOKUP_NEXT_MCAST:
273       t = "mcast";
274       break;
275     case IP_LOOKUP_NEXT_REWRITE:
276       break;
277     }
278
279   if (t)
280     vec_add (s, t, strlen (t));
281
282   return s;
283 }
284
285 u8 *
286 format_ip_adjacency_packet_data (u8 * s, va_list * args)
287 {
288   u32 adj_index = va_arg (*args, u32);
289   u8 *packet_data = va_arg (*args, u8 *);
290   u32 n_packet_data_bytes = va_arg (*args, u32);
291   ip_adjacency_t *adj = adj_get (adj_index);
292
293   switch (adj->lookup_next_index)
294     {
295     case IP_LOOKUP_NEXT_REWRITE:
296     case IP_LOOKUP_NEXT_MCAST:
297       s =
298         format (s, "%U", format_hex_bytes, packet_data, n_packet_data_bytes);
299       break;
300
301     default:
302       break;
303     }
304
305   return s;
306 }
307
308 static uword
309 unformat_dpo (unformat_input_t * input, va_list * args)
310 {
311   dpo_id_t *dpo = va_arg (*args, dpo_id_t *);
312   fib_protocol_t fp = va_arg (*args, int);
313   dpo_proto_t proto;
314
315   proto = fib_proto_to_dpo (fp);
316
317   if (unformat (input, "drop"))
318     dpo_copy (dpo, drop_dpo_get (proto));
319   else if (unformat (input, "punt"))
320     dpo_copy (dpo, punt_dpo_get (proto));
321   else if (unformat (input, "local"))
322     receive_dpo_add_or_lock (proto, ~0, NULL, dpo);
323   else if (unformat (input, "null-send-unreach"))
324     ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_UNREACH, dpo);
325   else if (unformat (input, "null-send-prohibit"))
326     ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_PROHIBIT, dpo);
327   else if (unformat (input, "null"))
328     ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_NONE, dpo);
329   else if (unformat (input, "classify"))
330     {
331       u32 classify_table_index;
332
333       if (!unformat (input, "%d", &classify_table_index))
334         {
335           clib_warning ("classify adj must specify table index");
336           return 0;
337         }
338
339       dpo_set (dpo, DPO_CLASSIFY, proto,
340                classify_dpo_create (proto, classify_table_index));
341     }
342   else
343     return 0;
344
345   return 1;
346 }
347
348 const ip46_address_t zero_addr = {
349   .as_u64 = {
350              0, 0},
351 };
352
353 clib_error_t *
354 vnet_ip_route_cmd (vlib_main_t * vm,
355                    unformat_input_t * main_input, vlib_cli_command_t * cmd)
356 {
357   unformat_input_t _line_input, *line_input = &_line_input;
358   fib_route_path_t *rpaths = NULL, rpath;
359   dpo_id_t dpo = DPO_INVALID, *dpos = NULL;
360   fib_prefix_t *prefixs = NULL, pfx;
361   mpls_label_t out_label, via_label;
362   clib_error_t *error = NULL;
363   u32 weight, preference;
364   u32 table_id, is_del;
365   vnet_main_t *vnm;
366   u32 fib_index;
367   f64 count;
368   int i;
369
370   vnm = vnet_get_main ();
371   is_del = 0;
372   table_id = 0;
373   count = 1;
374   memset (&pfx, 0, sizeof (pfx));
375   out_label = via_label = MPLS_LABEL_INVALID;
376
377   /* Get a line of input. */
378   if (!unformat_user (main_input, unformat_line_input, line_input))
379     return 0;
380
381   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
382     {
383       memset (&rpath, 0, sizeof (rpath));
384
385       if (unformat (line_input, "table %d", &table_id))
386         ;
387       else if (unformat (line_input, "del"))
388         is_del = 1;
389       else if (unformat (line_input, "add"))
390         is_del = 0;
391       else if (unformat (line_input, "resolve-via-host"))
392         {
393           if (vec_len (rpaths) == 0)
394             {
395               error = clib_error_return (0, "Paths then flags");
396               goto done;
397             }
398           rpaths[vec_len (rpaths) - 1].frp_flags |=
399             FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
400         }
401       else if (unformat (line_input, "resolve-via-attached"))
402         {
403           if (vec_len (rpaths) == 0)
404             {
405               error = clib_error_return (0, "Paths then flags");
406               goto done;
407             }
408           rpaths[vec_len (rpaths) - 1].frp_flags |=
409             FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
410         }
411       else if (unformat (line_input, "out-label %U",
412                          unformat_mpls_unicast_label, &out_label))
413         {
414           if (vec_len (rpaths) == 0)
415             {
416               error = clib_error_return (0, "Paths then labels");
417               goto done;
418             }
419           vec_add1 (rpaths[vec_len (rpaths) - 1].frp_label_stack, out_label);
420         }
421       else if (unformat (line_input, "via-label %U",
422                          unformat_mpls_unicast_label, &rpath.frp_local_label))
423         {
424           rpath.frp_weight = 1;
425           rpath.frp_eos = MPLS_NON_EOS;
426           rpath.frp_proto = FIB_PROTOCOL_MPLS;
427           rpath.frp_sw_if_index = ~0;
428           vec_add1 (rpaths, rpath);
429         }
430       else if (unformat (line_input, "count %f", &count))
431         ;
432
433       else if (unformat (line_input, "%U/%d",
434                          unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
435         {
436           pfx.fp_proto = FIB_PROTOCOL_IP4;
437           vec_add1 (prefixs, pfx);
438         }
439       else if (unformat (line_input, "%U/%d",
440                          unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len))
441         {
442           pfx.fp_proto = FIB_PROTOCOL_IP6;
443           vec_add1 (prefixs, pfx);
444         }
445       else if (unformat (line_input, "via %U %U",
446                          unformat_ip4_address,
447                          &rpath.frp_addr.ip4,
448                          unformat_vnet_sw_interface, vnm,
449                          &rpath.frp_sw_if_index))
450         {
451           rpath.frp_weight = 1;
452           rpath.frp_proto = FIB_PROTOCOL_IP4;
453           vec_add1 (rpaths, rpath);
454         }
455
456       else if (unformat (line_input, "via %U %U",
457                          unformat_ip6_address,
458                          &rpath.frp_addr.ip6,
459                          unformat_vnet_sw_interface, vnm,
460                          &rpath.frp_sw_if_index))
461         {
462           rpath.frp_weight = 1;
463           rpath.frp_proto = FIB_PROTOCOL_IP6;
464           vec_add1 (rpaths, rpath);
465         }
466       else if (unformat (line_input, "weight %u", &weight))
467         {
468           ASSERT (vec_len (rpaths));
469           rpaths[vec_len (rpaths) - 1].frp_weight = weight;
470         }
471       else if (unformat (line_input, "preference %u", &preference))
472         {
473           ASSERT (vec_len (rpaths));
474           rpaths[vec_len (rpaths) - 1].frp_preference = preference;
475         }
476       else if (unformat (line_input, "via %U next-hop-table %d",
477                          unformat_ip4_address,
478                          &rpath.frp_addr.ip4, &rpath.frp_fib_index))
479         {
480           rpath.frp_weight = 1;
481           rpath.frp_sw_if_index = ~0;
482           rpath.frp_proto = FIB_PROTOCOL_IP4;
483           vec_add1 (rpaths, rpath);
484         }
485       else if (unformat (line_input, "via %U next-hop-table %d",
486                          unformat_ip6_address,
487                          &rpath.frp_addr.ip6, &rpath.frp_fib_index))
488         {
489           rpath.frp_weight = 1;
490           rpath.frp_sw_if_index = ~0;
491           rpath.frp_proto = FIB_PROTOCOL_IP6;
492           vec_add1 (rpaths, rpath);
493         }
494       else if (unformat (line_input, "via %U",
495                          unformat_ip4_address, &rpath.frp_addr.ip4))
496         {
497           /*
498            * the recursive next-hops are by default in the same table
499            * as the prefix
500            */
501           rpath.frp_fib_index = table_id;
502           rpath.frp_weight = 1;
503           rpath.frp_sw_if_index = ~0;
504           rpath.frp_proto = FIB_PROTOCOL_IP4;
505           vec_add1 (rpaths, rpath);
506         }
507       else if (unformat (line_input, "via %U",
508                          unformat_ip6_address, &rpath.frp_addr.ip6))
509         {
510           rpath.frp_fib_index = table_id;
511           rpath.frp_weight = 1;
512           rpath.frp_sw_if_index = ~0;
513           rpath.frp_proto = FIB_PROTOCOL_IP6;
514           vec_add1 (rpaths, rpath);
515         }
516       else if (unformat (line_input,
517                          "lookup in table %d", &rpath.frp_fib_index))
518         {
519           rpath.frp_proto = pfx.fp_proto;
520           rpath.frp_sw_if_index = ~0;
521           vec_add1 (rpaths, rpath);
522         }
523       else if (vec_len (prefixs) > 0 &&
524                unformat (line_input, "via %U",
525                          unformat_vnet_sw_interface, vnm,
526                          &rpath.frp_sw_if_index))
527         {
528           rpath.frp_weight = 1;
529           rpath.frp_proto = prefixs[0].fp_proto;
530           vec_add1 (rpaths, rpath);
531         }
532       else if (vec_len (prefixs) > 0 &&
533                unformat (line_input, "via %U",
534                          unformat_dpo, &dpo, prefixs[0].fp_proto))
535         {
536           vec_add1 (dpos, dpo);
537         }
538       else
539         {
540           error = unformat_parse_error (line_input);
541           goto done;
542         }
543     }
544
545   if (vec_len (prefixs) == 0)
546     {
547       error =
548         clib_error_return (0, "expected ip4/ip6 destination address/length.");
549       goto done;
550     }
551
552   if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0)
553     {
554       error = clib_error_return (0, "expected paths.");
555       goto done;
556     }
557
558   if (~0 == table_id)
559     {
560       /*
561        * if no table_id is passed we will manipulate the default
562        */
563       fib_index = 0;
564     }
565   else
566     {
567       fib_index = fib_table_find (prefixs[0].fp_proto, table_id);
568
569       if (~0 == fib_index)
570         {
571           error = clib_error_return (0, "Nonexistent table id %d", table_id);
572           goto done;
573         }
574     }
575
576   for (i = 0; i < vec_len (prefixs); i++)
577     {
578       if (is_del && 0 == vec_len (rpaths))
579         {
580           fib_table_entry_delete (fib_index, &prefixs[i], FIB_SOURCE_CLI);
581         }
582       else if (!is_del && 1 == vec_len (dpos))
583         {
584           fib_table_entry_special_dpo_add (fib_index,
585                                            &prefixs[i],
586                                            FIB_SOURCE_CLI,
587                                            FIB_ENTRY_FLAG_EXCLUSIVE,
588                                            &dpos[0]);
589           dpo_reset (&dpos[0]);
590         }
591       else if (vec_len (dpos) > 0)
592         {
593           error =
594             clib_error_return (0,
595                                "Load-balancing over multiple special adjacencies is unsupported");
596           goto done;
597         }
598       else if (0 < vec_len (rpaths))
599         {
600           u32 k, j, n, incr;
601           ip46_address_t dst = prefixs[i].fp_addr;
602           f64 t[2];
603           n = count;
604           t[0] = vlib_time_now (vm);
605           incr = 1 << ((FIB_PROTOCOL_IP4 == prefixs[0].fp_proto ? 32 : 128) -
606                        prefixs[i].fp_len);
607
608           for (k = 0; k < n; k++)
609             {
610               for (j = 0; j < vec_len (rpaths); j++)
611                 {
612                   u32 fi;
613                   /*
614                    * the CLI parsing stored table Ids, swap to FIB indicies
615                    */
616                   fi = fib_table_find (prefixs[i].fp_proto,
617                                        rpaths[i].frp_fib_index);
618
619                   if (~0 == fi)
620                     {
621                       error =
622                         clib_error_return (0, "Via table %d does not exist",
623                                            rpaths[i].frp_fib_index);
624                       goto done;
625                     }
626                   rpaths[i].frp_fib_index = fi;
627
628                   fib_prefix_t rpfx = {
629                     .fp_len = prefixs[i].fp_len,
630                     .fp_proto = prefixs[i].fp_proto,
631                     .fp_addr = dst,
632                   };
633
634                   if (is_del)
635                     fib_table_entry_path_remove2 (fib_index,
636                                                   &rpfx,
637                                                   FIB_SOURCE_CLI, &rpaths[j]);
638                   else
639                     fib_table_entry_path_add2 (fib_index,
640                                                &rpfx,
641                                                FIB_SOURCE_CLI,
642                                                FIB_ENTRY_FLAG_NONE,
643                                                &rpaths[j]);
644                 }
645
646               if (FIB_PROTOCOL_IP4 == prefixs[0].fp_proto)
647                 {
648                   dst.ip4.as_u32 =
649                     clib_host_to_net_u32 (incr +
650                                           clib_net_to_host_u32 (dst.
651                                                                 ip4.as_u32));
652                 }
653               else
654                 {
655                   int bucket = (incr < 64 ? 0 : 1);
656                   dst.ip6.as_u64[bucket] =
657                     clib_host_to_net_u64 (incr +
658                                           clib_net_to_host_u64 (dst.ip6.as_u64
659                                                                 [bucket]));
660
661                 }
662             }
663           t[1] = vlib_time_now (vm);
664           if (count > 1)
665             vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
666         }
667       else
668         {
669           error = clib_error_return (0, "Don't understand what you want...");
670           goto done;
671         }
672     }
673
674
675 done:
676   vec_free (dpos);
677   vec_free (prefixs);
678   vec_free (rpaths);
679   unformat_free (line_input);
680   return error;
681 }
682
683 /* *INDENT-OFF* */
684 VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
685   .path = "ip",
686   .short_help = "Internet protocol (IP) commands",
687 };
688 /* *INDENT-ON* */
689
690 /* *INDENT-OFF* */
691 VLIB_CLI_COMMAND (vlib_cli_ip6_command, static) = {
692   .path = "ip6",
693   .short_help = "Internet protocol version 6 (IPv6) commands",
694 };
695 /* *INDENT-ON* */
696
697 /* *INDENT-OFF* */
698 VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
699   .path = "show ip",
700   .short_help = "Internet protocol (IP) show commands",
701 };
702 /* *INDENT-ON* */
703
704 /* *INDENT-OFF* */
705 VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
706   .path = "show ip6",
707   .short_help = "Internet protocol version 6 (IPv6) show commands",
708 };
709 /* *INDENT-ON* */
710
711 /*?
712  * This command is used to add or delete IPv4 or IPv6 routes. All
713  * IP Addresses ('<em><dst-ip-addr>/<width></em>',
714  * '<em><next-hop-ip-addr></em>' and '<em><adj-hop-ip-addr></em>')
715  * can be IPv4 or IPv6, but all must be of the same form in a single
716  * command. To display the current set of routes, use the commands
717  * '<em>show ip fib</em>' and '<em>show ip6 fib</em>'.
718  *
719  * @cliexpar
720  * Example of how to add a straight forward static route:
721  * @cliexcmd{ip route add 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0}
722  * Example of how to delete a straight forward static route:
723  * @cliexcmd{ip route del 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0}
724  * Mainly for route add/del performance testing, one can add or delete
725  * multiple routes by adding 'count N' to the previous item:
726  * @cliexcmd{ip route add count 10 7.0.0.0/24 via 6.0.0.1 GigabitEthernet2/0/0}
727  * Add multiple routes for the same destination to create equal-cost multipath:
728  * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0}
729  * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0}
730  * For unequal-cost multipath, specify the desired weights. This
731  * combination of weights results in 3/4 of the traffic following the
732  * second path, 1/4 following the first path:
733  * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0 weight 1}
734  * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0 weight 3}
735  * To add a route to a particular FIB table (VRF), use:
736  * @cliexcmd{ip route add 172.16.24.0/24 table 7 via GigabitEthernet2/0/0}
737  ?*/
738 /* *INDENT-OFF* */
739 VLIB_CLI_COMMAND (ip_route_command, static) = {
740   .path = "ip route",
741   .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>]",
742   .function = vnet_ip_route_cmd,
743   .is_mp_safe = 1,
744 };
745 /* *INDENT-ON* */
746
747 clib_error_t *
748 vnet_ip_mroute_cmd (vlib_main_t * vm,
749                     unformat_input_t * main_input, vlib_cli_command_t * cmd)
750 {
751   unformat_input_t _line_input, *line_input = &_line_input;
752   clib_error_t *error = NULL;
753   fib_route_path_t rpath;
754   u32 table_id, is_del;
755   vnet_main_t *vnm;
756   mfib_prefix_t pfx;
757   u32 fib_index;
758   mfib_itf_flags_t iflags = 0;
759   mfib_entry_flags_t eflags = 0;
760   u32 gcount, scount, ss, gg, incr;
761   f64 timet[2];
762
763   gcount = scount = 1;
764   vnm = vnet_get_main ();
765   is_del = 0;
766   table_id = 0;
767   memset (&pfx, 0, sizeof (pfx));
768   memset (&rpath, 0, sizeof (rpath));
769   rpath.frp_sw_if_index = ~0;
770
771   /* Get a line of input. */
772   if (!unformat_user (main_input, unformat_line_input, line_input))
773     return 0;
774
775   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
776     {
777       if (unformat (line_input, "table %d", &table_id))
778         ;
779       else if (unformat (line_input, "del"))
780         is_del = 1;
781       else if (unformat (line_input, "add"))
782         is_del = 0;
783       else if (unformat (line_input, "scount %d", &scount))
784         ;
785       else if (unformat (line_input, "gcount %d", &gcount))
786         ;
787       else if (unformat (line_input, "%U %U",
788                          unformat_ip4_address,
789                          &pfx.fp_src_addr.ip4,
790                          unformat_ip4_address, &pfx.fp_grp_addr.ip4))
791         {
792           pfx.fp_proto = FIB_PROTOCOL_IP4;
793           pfx.fp_len = 64;
794         }
795       else if (unformat (line_input, "%U %U",
796                          unformat_ip6_address,
797                          &pfx.fp_src_addr.ip6,
798                          unformat_ip6_address, &pfx.fp_grp_addr.ip6))
799         {
800           pfx.fp_proto = FIB_PROTOCOL_IP6;
801           pfx.fp_len = 256;
802         }
803       else if (unformat (line_input, "%U/%d",
804                          unformat_ip4_address,
805                          &pfx.fp_grp_addr.ip4, &pfx.fp_len))
806         {
807           memset (&pfx.fp_src_addr.ip4, 0, sizeof (pfx.fp_src_addr.ip4));
808           pfx.fp_proto = FIB_PROTOCOL_IP4;
809         }
810       else if (unformat (line_input, "%U/%d",
811                          unformat_ip6_address,
812                          &pfx.fp_grp_addr.ip6, &pfx.fp_len))
813         {
814           memset (&pfx.fp_src_addr.ip6, 0, sizeof (pfx.fp_src_addr.ip6));
815           pfx.fp_proto = FIB_PROTOCOL_IP6;
816         }
817       else if (unformat (line_input, "%U",
818                          unformat_ip4_address, &pfx.fp_grp_addr.ip4))
819         {
820           memset (&pfx.fp_src_addr.ip4, 0, sizeof (pfx.fp_src_addr.ip4));
821           pfx.fp_proto = FIB_PROTOCOL_IP4;
822           pfx.fp_len = 32;
823         }
824       else if (unformat (line_input, "%U",
825                          unformat_ip6_address, &pfx.fp_grp_addr.ip6))
826         {
827           memset (&pfx.fp_src_addr.ip6, 0, sizeof (pfx.fp_src_addr.ip6));
828           pfx.fp_proto = FIB_PROTOCOL_IP6;
829           pfx.fp_len = 128;
830         }
831       else if (unformat (line_input, "via %U",
832                          unformat_vnet_sw_interface, vnm,
833                          &rpath.frp_sw_if_index))
834         {
835           rpath.frp_weight = 1;
836         }
837       else if (unformat (line_input, "via local"))
838         {
839           rpath.frp_sw_if_index = ~0;
840           rpath.frp_weight = 1;
841           rpath.frp_flags |= FIB_ROUTE_PATH_LOCAL;
842         }
843       else if (unformat (line_input, "%U", unformat_mfib_itf_flags, &iflags))
844         ;
845       else if (unformat (line_input, "%U",
846                          unformat_mfib_entry_flags, &eflags))
847         ;
848       else
849         {
850           error = unformat_parse_error (line_input);
851           goto done;
852         }
853     }
854
855   if (~0 == table_id)
856     {
857       /*
858        * if no table_id is passed we will manipulate the default
859        */
860       fib_index = 0;
861     }
862   else
863     {
864       fib_index = mfib_table_find (pfx.fp_proto, table_id);
865
866       if (~0 == fib_index)
867         {
868           error = clib_error_return (0, "Nonexistent table id %d", table_id);
869           goto done;
870         }
871     }
872
873   timet[0] = vlib_time_now (vm);
874
875   if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
876     {
877       incr = 1 << (32 - (pfx.fp_len % 32));
878     }
879   else
880     {
881       incr = 1 << (128 - (pfx.fp_len % 128));
882     }
883
884   for (ss = 0; ss < scount; ss++)
885     {
886       for (gg = 0; gg < gcount; gg++)
887         {
888           if (is_del && 0 == rpath.frp_weight)
889             {
890               /* no path provided => route delete */
891               mfib_table_entry_delete (fib_index, &pfx, MFIB_SOURCE_CLI);
892             }
893           else if (eflags)
894             {
895               mfib_table_entry_update (fib_index, &pfx, MFIB_SOURCE_CLI,
896                                        MFIB_RPF_ID_NONE, eflags);
897             }
898           else
899             {
900               if (is_del)
901                 mfib_table_entry_path_remove (fib_index,
902                                               &pfx, MFIB_SOURCE_CLI, &rpath);
903               else
904                 mfib_table_entry_path_update (fib_index,
905                                               &pfx, MFIB_SOURCE_CLI, &rpath,
906                                               iflags);
907             }
908
909           if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
910             {
911               pfx.fp_grp_addr.ip4.as_u32 =
912                 clib_host_to_net_u32 (incr +
913                                       clib_net_to_host_u32 (pfx.
914                                                             fp_grp_addr.ip4.
915                                                             as_u32));
916             }
917           else
918             {
919               int bucket = (incr < 64 ? 0 : 1);
920               pfx.fp_grp_addr.ip6.as_u64[bucket] =
921                 clib_host_to_net_u64 (incr +
922                                       clib_net_to_host_u64 (pfx.
923                                                             fp_grp_addr.ip6.as_u64
924                                                             [bucket]));
925
926             }
927         }
928       if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
929         {
930           pfx.fp_src_addr.ip4.as_u32 =
931             clib_host_to_net_u32 (1 +
932                                   clib_net_to_host_u32 (pfx.fp_src_addr.
933                                                         ip4.as_u32));
934         }
935       else
936         {
937           pfx.fp_src_addr.ip6.as_u64[1] =
938             clib_host_to_net_u64 (1 +
939                                   clib_net_to_host_u64 (pfx.fp_src_addr.
940                                                         ip6.as_u64[1]));
941         }
942     }
943
944   timet[1] = vlib_time_now (vm);
945
946   if (scount > 1 || gcount > 1)
947     vlib_cli_output (vm, "%.6e routes/sec",
948                      (scount * gcount) / (timet[1] - timet[0]));
949
950 done:
951   unformat_free (line_input);
952
953   return error;
954 }
955
956 /*?
957  * This command is used to add or delete IPv4 or IPv6  multicastroutes. All
958  * IP Addresses ('<em><dst-ip-addr>/<width></em>',
959  * '<em><next-hop-ip-addr></em>' and '<em><adj-hop-ip-addr></em>')
960  * can be IPv4 or IPv6, but all must be of the same form in a single
961  * command. To display the current set of routes, use the commands
962  * '<em>show ip mfib</em>' and '<em>show ip6 mfib</em>'.
963  * The full set of support flags for interfaces and route is shown via;
964  * '<em>show mfib route flags</em>' and '<em>show mfib itf flags</em>'
965  * respectively.
966  * @cliexpar
967  * Example of how to add a forwarding interface to a route (and create the
968  * route if it does not exist)
969  * @cliexcmd{ip mroute add 232.1.1.1 via GigabitEthernet2/0/0 Forward}
970  * Example of how to add an accepting interface to a route (and create the
971  * route if it does not exist)
972  * @cliexcmd{ip mroute add 232.1.1.1 via GigabitEthernet2/0/1 Accept}
973  * Example of changing the route's flags to send signals via the API
974  * @cliexcmd{ip mroute add 232.1.1.1 Signal}
975
976  ?*/
977 /* *INDENT-OFF* */
978 VLIB_CLI_COMMAND (ip_mroute_command, static) =
979 {
980   .path = "ip mroute",
981   .short_help = "ip mroute [add|del] <dst-ip-addr>/<width> [table <table-id>] [via <next-hop-ip-addr> [<interface>],",
982   .function = vnet_ip_mroute_cmd,
983   .is_mp_safe = 1,
984 };
985 /* *INDENT-ON* */
986
987 /*
988  * The next two routines address a longstanding script hemorrhoid.
989  * Probing a v4 or v6 neighbor needs to appear to be synchronous,
990  * or dependent route-adds will simply fail.
991  */
992 static clib_error_t *
993 ip6_probe_neighbor_wait (vlib_main_t * vm, ip6_address_t * a, u32 sw_if_index,
994                          int retry_count)
995 {
996   vnet_main_t *vnm = vnet_get_main ();
997   clib_error_t *e;
998   int i;
999   int resolved = 0;
1000   uword event_type;
1001   uword *event_data = 0;
1002
1003   ASSERT (vlib_in_process_context (vm));
1004
1005   if (retry_count > 0)
1006     vnet_register_ip6_neighbor_resolution_event
1007       (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
1008        1 /* event */ , 0 /* data */ );
1009
1010   for (i = 0; i < retry_count; i++)
1011     {
1012       /* The interface may be down, etc. */
1013       e = ip6_probe_neighbor (vm, a, sw_if_index);
1014
1015       if (e)
1016         return e;
1017
1018       vlib_process_wait_for_event_or_clock (vm, 1.0);
1019       event_type = vlib_process_get_events (vm, &event_data);
1020       switch (event_type)
1021         {
1022         case 1:         /* resolved... */
1023           vlib_cli_output (vm, "Resolved %U", format_ip6_address, a);
1024           resolved = 1;
1025           goto done;
1026
1027         case ~0:                /* timeout */
1028           break;
1029
1030         default:
1031           clib_warning ("unknown event_type %d", event_type);
1032         }
1033       vec_reset_length (event_data);
1034     }
1035
1036 done:
1037
1038   if (!resolved)
1039     return clib_error_return (0, "Resolution failed for %U",
1040                               format_ip6_address, a);
1041   return 0;
1042 }
1043
1044 static clib_error_t *
1045 ip4_probe_neighbor_wait (vlib_main_t * vm, ip4_address_t * a, u32 sw_if_index,
1046                          int retry_count)
1047 {
1048   vnet_main_t *vnm = vnet_get_main ();
1049   clib_error_t *e;
1050   int i;
1051   int resolved = 0;
1052   uword event_type;
1053   uword *event_data = 0;
1054
1055   ASSERT (vlib_in_process_context (vm));
1056
1057   if (retry_count > 0)
1058     vnet_register_ip4_arp_resolution_event
1059       (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
1060        1 /* event */ , 0 /* data */ );
1061
1062   for (i = 0; i < retry_count; i++)
1063     {
1064       /* The interface may be down, etc. */
1065       e = ip4_probe_neighbor (vm, a, sw_if_index);
1066
1067       if (e)
1068         return e;
1069
1070       vlib_process_wait_for_event_or_clock (vm, 1.0);
1071       event_type = vlib_process_get_events (vm, &event_data);
1072       switch (event_type)
1073         {
1074         case 1:         /* resolved... */
1075           vlib_cli_output (vm, "Resolved %U", format_ip4_address, a);
1076           resolved = 1;
1077           goto done;
1078
1079         case ~0:                /* timeout */
1080           break;
1081
1082         default:
1083           clib_warning ("unknown event_type %d", event_type);
1084         }
1085       vec_reset_length (event_data);
1086     }
1087
1088 done:
1089
1090   vec_reset_length (event_data);
1091
1092   if (!resolved)
1093     return clib_error_return (0, "Resolution failed for %U",
1094                               format_ip4_address, a);
1095   return 0;
1096 }
1097
1098 static clib_error_t *
1099 probe_neighbor_address (vlib_main_t * vm,
1100                         unformat_input_t * input, vlib_cli_command_t * cmd)
1101 {
1102   vnet_main_t *vnm = vnet_get_main ();
1103   unformat_input_t _line_input, *line_input = &_line_input;
1104   ip4_address_t a4;
1105   ip6_address_t a6;
1106   clib_error_t *error = 0;
1107   u32 sw_if_index = ~0;
1108   int retry_count = 3;
1109   int is_ip4 = 1;
1110   int address_set = 0;
1111
1112   /* Get a line of input. */
1113   if (!unformat_user (input, unformat_line_input, line_input))
1114     return 0;
1115
1116   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1117     {
1118       if (unformat_user (line_input, unformat_vnet_sw_interface, vnm,
1119                          &sw_if_index))
1120         ;
1121       else if (unformat (line_input, "retry %d", &retry_count))
1122         ;
1123
1124       else if (unformat (line_input, "%U", unformat_ip4_address, &a4))
1125         address_set++;
1126       else if (unformat (line_input, "%U", unformat_ip6_address, &a6))
1127         {
1128           address_set++;
1129           is_ip4 = 0;
1130         }
1131       else
1132         {
1133           error = clib_error_return (0, "unknown input '%U'",
1134                                      format_unformat_error, line_input);
1135           goto done;
1136         }
1137     }
1138
1139   if (sw_if_index == ~0)
1140     {
1141       error = clib_error_return (0, "Interface required, not set.");
1142       goto done;
1143     }
1144   if (address_set == 0)
1145     {
1146       error = clib_error_return (0, "ip address required, not set.");
1147       goto done;
1148     }
1149   if (address_set > 1)
1150     {
1151       error = clib_error_return (0, "Multiple ip addresses not supported.");
1152       goto done;
1153     }
1154
1155   if (is_ip4)
1156     error = ip4_probe_neighbor_wait (vm, &a4, sw_if_index, retry_count);
1157   else
1158     error = ip6_probe_neighbor_wait (vm, &a6, sw_if_index, retry_count);
1159
1160 done:
1161   unformat_free (line_input);
1162
1163   return error;
1164 }
1165
1166 /*?
1167  * The '<em>ip probe-neighbor</em>' command ARPs for IPv4 addresses or
1168  * attempts IPv6 neighbor discovery depending on the supplied IP address
1169  * format.
1170  *
1171  * @note This command will not immediately affect the indicated FIB; it
1172  * is not suitable for use in establishing a FIB entry prior to adding
1173  * recursive FIB entries. As in: don't use it in a script to probe a
1174  * gateway prior to adding a default route. It won't work. Instead,
1175  * configure a static ARP cache entry [see '<em>set ip arp</em>'], or
1176  * a static IPv6 neighbor [see '<em>set ip6 neighbor</em>'].
1177  *
1178  * @cliexpar
1179  * Example of probe for an IPv4 address:
1180  * @cliexcmd{ip probe-neighbor GigabitEthernet2/0/0 172.16.1.2}
1181 ?*/
1182 /* *INDENT-OFF* */
1183 VLIB_CLI_COMMAND (ip_probe_neighbor_command, static) = {
1184   .path = "ip probe-neighbor",
1185   .function = probe_neighbor_address,
1186   .short_help = "ip probe-neighbor <interface> <ip4-addr> | <ip6-addr> [retry nn]",
1187   .is_mp_safe = 1,
1188 };
1189 /* *INDENT-ON* */
1190
1191 /*
1192  * fd.io coding-style-patch-verification: ON
1193  *
1194  * Local Variables:
1195  * eval: (c-set-style "gnu")
1196  * End:
1197  */