blob: 5cfdc23ea012f6b66daa5de24a8d696cd6c90a26 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#include <vnet/ip/ip.h>
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010041#include <vnet/adj/adj.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010042#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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
51clib_error_t *
52ip_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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
159{
Dave Barachdbf19ca2016-03-15 10:21:54 +0100160 /* 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
Dave Barachb2ef4dd2016-03-23 08:56:01 -0400164 /* Preallocate three "special" adjacencies */
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100165 lm->adjacency_heap = adj_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
167 if (! lm->fib_result_n_bytes)
168 lm->fib_result_n_bytes = sizeof (uword);
169
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
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
201u8 * format_ip_flow_hash_config (u8 * s, va_list * args)
202{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100203 flow_hash_config_t flow_hash_config = va_arg (*args, u32);
204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205#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
212u8 * format_ip_lookup_next (u8 * s, va_list * args)
213{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100214 ip_lookup_next_t n = va_arg (*args, ip_lookup_next_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 char * t = 0;
216
217 switch (n)
218 {
219 default:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100220 s = format (s, "unknown %d", n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 return s;
222
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 case IP_LOOKUP_NEXT_DROP: t = "drop"; break;
224 case IP_LOOKUP_NEXT_PUNT: t = "punt"; break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 case IP_LOOKUP_NEXT_ARP: t = "arp"; break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100226 case IP_LOOKUP_NEXT_MIDCHAIN: t="midchain"; break;
227 case IP_LOOKUP_NEXT_GLEAN: t="glean"; break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238u8 * format_ip_adjacency_packet_data (u8 * s, va_list * args)
239{
240 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 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);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244 ip_adjacency_t * adj = adj_get(adj_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
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
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100261static uword unformat_dpo (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100263 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
269 if (unformat (input, "drop"))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270 dpo_copy(dpo, drop_dpo_get(proto));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 else if (unformat (input, "punt"))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100272 dpo_copy(dpo, punt_dpo_get(proto));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 else if (unformat (input, "local"))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100274 receive_dpo_add_or_lock(proto, ~0, NULL, dpo);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 else if (unformat (input, "classify"))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100276 {
277 u32 classify_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 else
289 return 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100290
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 return 1;
292}
293
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100294const ip46_address_t zero_addr = {
295 .as_u64 = {
296 0, 0
297 },
298};
299
300u32
301fib_table_id_find_fib_index (fib_protocol_t proto,
302 u32 table_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100304 ip4_main_t *im4 = &ip4_main;
305 ip6_main_t *im6 = &ip6_main;
306 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100308 switch (proto)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100310 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100320 if (NULL != p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100322 return (p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100324 return (~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325}
326
327clib_error_t *
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100328vnet_ip_route_cmd (vlib_main_t * vm,
329 unformat_input_t * main_input,
330 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 unformat_input_t _line_input, * line_input = &_line_input;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100333 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 f64 count;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100344 vnm = vnet_get_main();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 is_del = 0;
346 table_id = 0;
347 count = 1;
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100348 memset(&pfx, 0, sizeof(pfx));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
350 /* Get a line of input. */
351 if (! unformat_user (main_input, unformat_line_input, line_input))
352 return 0;
353
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
355 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100356 memset(&rpath, 0, sizeof(rpath));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 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;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100364 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 else if (unformat (line_input, "count %f", &count))
394 ;
395
396 else if (unformat (line_input, "%U/%d",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100397 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 else if (unformat (line_input, "%U/%d",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100405 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
424 else if (unformat (line_input, "via %U %U weight %u",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100425 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
436 else if (unformat (line_input, "via %U %U",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100437 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
448 else if (unformat (line_input, "via %U %U",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100449 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 else if (unformat (line_input, "via %U",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100482 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 else if (unformat (line_input, "via %U",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100497 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;
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100513 rpath.frp_sw_if_index = ~0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100514 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100524 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 error = unformat_parse_error (line_input);
526 goto done;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100527 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 }
529
530 unformat_free (line_input);
531
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100532 if (vec_len (prefixs) == 0)
533 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 error = clib_error_return (0, "expected ip4/ip6 destination address/length.");
535 goto done;
536 }
537
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100538 if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100540 error = clib_error_return (0, "expected paths.");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 goto done;
542 }
543
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100544 if (~0 == table_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100546 /*
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100556 if (~0 == fib_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100558 error = clib_error_return (0,
559 "Nonexistent table id %d",
560 table_id);
561 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 }
563 }
564
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100565 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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655 done:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100656 vec_free (dpos);
657 vec_free (prefixs);
658 vec_free (rpaths);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 return error;
660}
661
662VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
663 .path = "ip",
664 .short_help = "Internet protocol (IP) commands",
665};
666
667VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
668 .path = "show ip",
669 .short_help = "Internet protocol (IP) show commands",
670};
671
672VLIB_CLI_COMMAND (vlib_cli_show_ip4_command, static) = {
673 .path = "show ip4",
674 .short_help = "Internet protocol version 4 (IP4) show commands",
675};
676
677VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
678 .path = "show ip6",
679 .short_help = "Internet protocol version 6 (IP6) show commands",
680};
681
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700682/*?
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 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709VLIB_CLI_COMMAND (ip_route_command, static) = {
710 .path = "ip route",
711 .short_help = "Add/delete IP routes",
712 .function = vnet_ip_route_cmd,
Dave Barachb2ef4dd2016-03-23 08:56:01 -0400713 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714};
715
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100716/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 * 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 */
721static clib_error_t *
722ip6_probe_neighbor_wait (vlib_main_t *vm, ip6_address_t * a, u32 sw_if_index,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100723 int retry_count)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724{
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)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100735 vnet_register_ip6_neighbor_resolution_event
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 (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);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100743
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 if (e)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100745 return e;
746
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 vlib_process_wait_for_event_or_clock (vm, 1.0);
748 event_type = vlib_process_get_events (vm, &event_data);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100749 switch (event_type)
750 {
751 case 1: /* resolved... */
752 vlib_cli_output (vm, "Resolved %U",
753 format_ip6_address, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 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 }
Dave Barachb2ef4dd2016-03-23 08:56:01 -0400763 vec_reset_length (event_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 }
765
766 done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
768 if (!resolved)
769 return clib_error_return (0, "Resolution failed for %U",
770 format_ip6_address, a);
771 return 0;
772}
773
774static clib_error_t *
775ip4_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 }
Dave Barachb2ef4dd2016-03-23 08:56:01 -0400816 vec_reset_length (event_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 }
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
829static clib_error_t *
830probe_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
885VLIB_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]",
Dave Barachb2ef4dd2016-03-23 08:56:01 -0400889 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890};