blob: 63bd281a3254c84f758ee1196411fa64e731ad5e [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/*
Paul Vinciguerra5b755e22019-10-26 19:34:40 -040016 * ip/ip_lookup.c: ip4/6 adjacency and lookup table management
Ed Warnickecb9cada2015-12-08 15:45:58 -070017 *
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>
Neale Ranns32e1c012016-11-22 17:07:28 +000046#include <vnet/mfib/mfib_table.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047#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>
Neale Ranns948e00f2016-10-20 13:39:34 +010051#include <vnet/dpo/ip_null_dpo.h>
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -070052#include <vnet/dpo/l3_proxy_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Billy McFall0683c9c2016-10-13 08:27:31 -040054/**
55 * @file
Paul Vinciguerra5b755e22019-10-26 19:34:40 -040056 * @brief IPv4 and IPv6 adjacency and lookup table management.
Billy McFall0683c9c2016-10-13 08:27:31 -040057 *
58 */
59
Neale Rannsd96bad82017-03-08 01:12:54 -080060static clib_error_t *
61ip_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
62{
63 vec_validate_init_empty (ip4_main.
64 lookup_main.if_address_pool_index_by_sw_if_index,
65 sw_if_index, ~0);
66 vec_validate_init_empty (ip6_main.
67 lookup_main.if_address_pool_index_by_sw_if_index,
68 sw_if_index, ~0);
69
70 return (NULL);
71}
72
73VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_sw_interface_add_del);
74
Dave Barachd7cb1b52016-12-09 09:52:16 -050075void
76ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Dave Barachd7cb1b52016-12-09 09:52:16 -050078 if (!lm->fib_result_n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 lm->fib_result_n_bytes = sizeof (uword);
80
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 lm->is_ip6 = is_ip6;
Matthew Smith6c92f5b2019-08-07 11:46:30 -050082 mhash_init (&lm->prefix_to_if_prefix_index, sizeof (uword),
83 sizeof (ip_interface_prefix_key_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 if (is_ip6)
85 {
86 lm->format_address_and_length = format_ip6_address_and_length;
87 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
88 sizeof (ip6_address_fib_t));
89 }
90 else
91 {
92 lm->format_address_and_length = format_ip4_address_and_length;
93 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
94 sizeof (ip4_address_fib_t));
95 }
96
97 {
98 int i;
99
100 /* Setup all IP protocols to be punted and builtin-unknown. */
101 for (i = 0; i < 256; i++)
102 {
103 lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
104 lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
105 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500108 lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 :
109 IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
110 lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] =
111 IP_BUILTIN_PROTOCOL_UDP;
112 lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 :
113 IP_PROTOCOL_ICMP] =
114 IP_BUILTIN_PROTOCOL_ICMP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 }
116}
117
Dave Barachd7cb1b52016-12-09 09:52:16 -0500118u8 *
119format_ip_flow_hash_config (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100121 flow_hash_config_t flow_hash_config = va_arg (*args, u32);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123#define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
124 foreach_flow_hash_bit;
125#undef _
126
127 return s;
128}
129
Dave Barachd7cb1b52016-12-09 09:52:16 -0500130u8 *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131format_ip_adjacency_packet_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500133 u8 *packet_data = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 u32 n_packet_data_bytes = va_arg (*args, u32);
BenoƮt Ganne138c37a2019-07-18 17:34:28 +0200135
Neale Ranns0b6a8572019-10-30 17:34:14 +0000136 s = format (s, "%U", format_hex_bytes, packet_data, n_packet_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138 return s;
139}
140
Dave Barachd7cb1b52016-12-09 09:52:16 -0500141static uword
142unformat_dpo (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100144 dpo_id_t *dpo = va_arg (*args, dpo_id_t *);
145 fib_protocol_t fp = va_arg (*args, int);
146 dpo_proto_t proto;
147
Dave Barachd7cb1b52016-12-09 09:52:16 -0500148 proto = fib_proto_to_dpo (fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
150 if (unformat (input, "drop"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500151 dpo_copy (dpo, drop_dpo_get (proto));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 else if (unformat (input, "punt"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500153 dpo_copy (dpo, punt_dpo_get (proto));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 else if (unformat (input, "local"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500155 receive_dpo_add_or_lock (proto, ~0, NULL, dpo);
Neale Ranns948e00f2016-10-20 13:39:34 +0100156 else if (unformat (input, "null-send-unreach"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500157 ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_UNREACH, dpo);
Neale Ranns948e00f2016-10-20 13:39:34 +0100158 else if (unformat (input, "null-send-prohibit"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159 ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_PROHIBIT, dpo);
Neale Ranns948e00f2016-10-20 13:39:34 +0100160 else if (unformat (input, "null"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500161 ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_NONE, dpo);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 else if (unformat (input, "classify"))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100163 {
164 u32 classify_table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100166 if (!unformat (input, "%d", &classify_table_index))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500167 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100168 clib_warning ("classify adj must specify table index");
Dave Barachd7cb1b52016-12-09 09:52:16 -0500169 return 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170 }
171
Dave Barachd7cb1b52016-12-09 09:52:16 -0500172 dpo_set (dpo, DPO_CLASSIFY, proto,
173 classify_dpo_create (proto, classify_table_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100174 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 else
176 return 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100177
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 return 1;
179}
180
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100181const ip46_address_t zero_addr = {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 .as_u64 = {
183 0, 0},
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100184};
185
Neale Ranns039cbfe2018-02-27 03:45:38 -0800186static clib_error_t *
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100187vnet_ip_route_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500188 unformat_input_t * main_input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800191 u32 table_id, is_del, fib_index, payload_proto;
Neale Ranns948e00f2016-10-20 13:39:34 +0100192 dpo_id_t dpo = DPO_INVALID, *dpos = NULL;
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800193 fib_route_path_t *rpaths = NULL, rpath;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100194 fib_prefix_t *prefixs = NULL, pfx;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500195 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 f64 count;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100197 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
199 is_del = 0;
200 table_id = 0;
201 count = 1;
Dave Barachb7b92992018-10-17 10:38:51 -0400202 clib_memset (&pfx, 0, sizeof (pfx));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500205 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 return 0;
207
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
209 {
Dave Barachb7b92992018-10-17 10:38:51 -0400210 clib_memset (&rpath, 0, sizeof (rpath));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100211
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 if (unformat (line_input, "table %d", &table_id))
213 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 else if (unformat (line_input, "count %f", &count))
215 ;
216
217 else if (unformat (line_input, "%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500218 unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
219 {
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800220 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP4;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500221 vec_add1 (prefixs, pfx);
222 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 else if (unformat (line_input, "%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500224 unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len))
225 {
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800226 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP6;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500227 vec_add1 (prefixs, pfx);
228 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 else if (unformat (line_input, "via %U",
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800230 unformat_fib_route_path, &rpath, &payload_proto))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500231 {
Neale Ranns8c2f05c2016-12-12 19:35:58 +0000232 vec_add1 (rpaths, rpath);
233 }
234 else if (vec_len (prefixs) > 0 &&
235 unformat (line_input, "via %U",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100236 unformat_dpo, &dpo, prefixs[0].fp_proto))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500237 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100238 vec_add1 (dpos, dpo);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239 }
pragash352829f2017-08-17 22:53:24 -0400240 else if (unformat (line_input, "del"))
241 is_del = 1;
242 else if (unformat (line_input, "add"))
243 is_del = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500245 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 error = unformat_parse_error (line_input);
247 goto done;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500248 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500250
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100251 if (vec_len (prefixs) == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500252 {
253 error =
254 clib_error_return (0, "expected ip4/ip6 destination address/length.");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 goto done;
256 }
257
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100258 if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100260 error = clib_error_return (0, "expected paths.");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 goto done;
262 }
263
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100264 if (~0 == table_id)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500265 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100266 /*
267 * if no table_id is passed we will manipulate the default
268 */
269 fib_index = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100271 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500272 {
Neale Ranns107e7d42017-04-11 09:55:19 -0700273 fib_index = fib_table_find (prefixs[0].fp_proto, table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100275 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 {
277 error = clib_error_return (0, "Nonexistent table id %d", table_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278 goto done;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500279 }
280 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100282 for (i = 0; i < vec_len (prefixs); i++)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500283 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100284 if (is_del && 0 == vec_len (rpaths))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 {
286 fib_table_entry_delete (fib_index, &prefixs[i], FIB_SOURCE_CLI);
287 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100288 else if (!is_del && 1 == vec_len (dpos))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500289 {
290 fib_table_entry_special_dpo_add (fib_index,
291 &prefixs[i],
292 FIB_SOURCE_CLI,
293 FIB_ENTRY_FLAG_EXCLUSIVE,
294 &dpos[0]);
295 dpo_reset (&dpos[0]);
296 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100297 else if (vec_len (dpos) > 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500298 {
299 error =
300 clib_error_return (0,
301 "Load-balancing over multiple special adjacencies is unsupported");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100302 goto done;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500303 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100304 else if (0 < vec_len (rpaths))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500305 {
Neale Ranns097fa662018-05-01 05:17:55 -0700306 u32 k, n, incr;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100307 ip46_address_t dst = prefixs[i].fp_addr;
308 f64 t[2];
309 n = count;
310 t[0] = vlib_time_now (vm);
311 incr = 1 << ((FIB_PROTOCOL_IP4 == prefixs[0].fp_proto ? 32 : 128) -
312 prefixs[i].fp_len);
313
314 for (k = 0; k < n; k++)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500315 {
Neale Ranns097fa662018-05-01 05:17:55 -0700316 fib_prefix_t rpfx = {
317 .fp_len = prefixs[i].fp_len,
318 .fp_proto = prefixs[i].fp_proto,
319 .fp_addr = dst,
320 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100321
Neale Ranns097fa662018-05-01 05:17:55 -0700322 if (is_del)
323 fib_table_entry_path_remove2 (fib_index,
324 &rpfx, FIB_SOURCE_CLI, rpaths);
325 else
326 fib_table_entry_path_add2 (fib_index,
327 &rpfx,
328 FIB_SOURCE_CLI,
329 FIB_ENTRY_FLAG_NONE, rpaths);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100330
331 if (FIB_PROTOCOL_IP4 == prefixs[0].fp_proto)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100333 dst.ip4.as_u32 =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 clib_host_to_net_u32 (incr +
335 clib_net_to_host_u32 (dst.
336 ip4.as_u32));
337 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100338 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500339 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340 int bucket = (incr < 64 ? 0 : 1);
341 dst.ip6.as_u64[bucket] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500342 clib_host_to_net_u64 (incr +
343 clib_net_to_host_u64 (dst.ip6.as_u64
344 [bucket]));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345 }
346 }
Neale Ranns097fa662018-05-01 05:17:55 -0700347
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100348 t[1] = vlib_time_now (vm);
349 if (count > 1)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
351 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100352 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 {
354 error = clib_error_return (0, "Don't understand what you want...");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100355 goto done;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 }
357 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100358
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359done:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100360 vec_free (dpos);
361 vec_free (prefixs);
362 vec_free (rpaths);
Billy McFalla9a20e72017-02-15 11:39:12 -0500363 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 return error;
365}
366
Neale Ranns15002542017-09-10 04:39:11 -0700367clib_error_t *
368vnet_ip_table_cmd (vlib_main_t * vm,
369 unformat_input_t * main_input,
370 vlib_cli_command_t * cmd, fib_protocol_t fproto)
371{
372 unformat_input_t _line_input, *line_input = &_line_input;
373 clib_error_t *error = NULL;
374 u32 table_id, is_add;
Neale Ranns2297af02017-09-12 09:45:04 -0700375 u8 *name = NULL;
Neale Ranns15002542017-09-10 04:39:11 -0700376
377 is_add = 1;
378 table_id = ~0;
379
380 /* Get a line of input. */
381 if (!unformat_user (main_input, unformat_line_input, line_input))
382 return 0;
383
384 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
385 {
386 if (unformat (line_input, "%d", &table_id))
387 ;
388 else if (unformat (line_input, "del"))
389 is_add = 0;
390 else if (unformat (line_input, "add"))
391 is_add = 1;
Neale Ranns2297af02017-09-12 09:45:04 -0700392 else if (unformat (line_input, "name %s", &name))
393 ;
Neale Ranns15002542017-09-10 04:39:11 -0700394 else
395 {
396 error = unformat_parse_error (line_input);
397 goto done;
398 }
399 }
400
401 if (~0 == table_id)
402 {
403 error = clib_error_return (0, "No table id");
404 goto done;
405 }
406 else if (0 == table_id)
407 {
408 error = clib_error_return (0, "Can't change the default table");
409 goto done;
410 }
411 else
412 {
413 if (is_add)
414 {
Neale Ranns2297af02017-09-12 09:45:04 -0700415 ip_table_create (fproto, table_id, 0, name);
Neale Ranns15002542017-09-10 04:39:11 -0700416 }
417 else
418 {
419 ip_table_delete (fproto, table_id, 0);
420 }
421 }
422
423done:
424 unformat_free (line_input);
425 return error;
426}
427
428clib_error_t *
429vnet_ip4_table_cmd (vlib_main_t * vm,
430 unformat_input_t * main_input, vlib_cli_command_t * cmd)
431{
432 return (vnet_ip_table_cmd (vm, main_input, cmd, FIB_PROTOCOL_IP4));
433}
434
435clib_error_t *
436vnet_ip6_table_cmd (vlib_main_t * vm,
437 unformat_input_t * main_input, vlib_cli_command_t * cmd)
438{
439 return (vnet_ip_table_cmd (vm, main_input, cmd, FIB_PROTOCOL_IP6));
440}
441
Dave Barachd7cb1b52016-12-09 09:52:16 -0500442/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
444 .path = "ip",
445 .short_help = "Internet protocol (IP) commands",
446};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500447/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
Dave Barachd7cb1b52016-12-09 09:52:16 -0500449/* *INDENT-OFF* */
Billy McFall0683c9c2016-10-13 08:27:31 -0400450VLIB_CLI_COMMAND (vlib_cli_ip6_command, static) = {
451 .path = "ip6",
452 .short_help = "Internet protocol version 6 (IPv6) commands",
453};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500454/* *INDENT-ON* */
Billy McFall0683c9c2016-10-13 08:27:31 -0400455
Dave Barachd7cb1b52016-12-09 09:52:16 -0500456/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
458 .path = "show ip",
459 .short_help = "Internet protocol (IP) show commands",
460};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500461/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Dave Barachd7cb1b52016-12-09 09:52:16 -0500463/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
465 .path = "show ip6",
Billy McFall0683c9c2016-10-13 08:27:31 -0400466 .short_help = "Internet protocol version 6 (IPv6) show commands",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500468/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700470/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400471 * This command is used to add or delete IPv4 or IPv6 routes. All
472 * IP Addresses ('<em><dst-ip-addr>/<width></em>',
473 * '<em><next-hop-ip-addr></em>' and '<em><adj-hop-ip-addr></em>')
474 * can be IPv4 or IPv6, but all must be of the same form in a single
475 * command. To display the current set of routes, use the commands
476 * '<em>show ip fib</em>' and '<em>show ip6 fib</em>'.
477 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700478 * @cliexpar
Billy McFall0683c9c2016-10-13 08:27:31 -0400479 * Example of how to add a straight forward static route:
480 * @cliexcmd{ip route add 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0}
481 * Example of how to delete a straight forward static route:
482 * @cliexcmd{ip route del 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700483 * Mainly for route add/del performance testing, one can add or delete
484 * multiple routes by adding 'count N' to the previous item:
Billy McFall0683c9c2016-10-13 08:27:31 -0400485 * @cliexcmd{ip route add count 10 7.0.0.0/24 via 6.0.0.1 GigabitEthernet2/0/0}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700486 * Add multiple routes for the same destination to create equal-cost multipath:
Billy McFall0683c9c2016-10-13 08:27:31 -0400487 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0}
488 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0}
489 * For unequal-cost multipath, specify the desired weights. This
490 * combination of weights results in 3/4 of the traffic following the
491 * second path, 1/4 following the first path:
492 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0 weight 1}
493 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0 weight 3}
494 * To add a route to a particular FIB table (VRF), use:
495 * @cliexcmd{ip route add 172.16.24.0/24 table 7 via GigabitEthernet2/0/0}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700496 ?*/
Billy McFall0683c9c2016-10-13 08:27:31 -0400497/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498VLIB_CLI_COMMAND (ip_route_command, static) = {
499 .path = "ip route",
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800500 .short_help = "ip route [add|del] [count <n>] <dst-ip-addr>/<width> [table <table-id>] via [next-hop-address] [next-hop-interface] [next-hop-table <value>] [weight <value>] [preference <value>] [udp-encap-id <value>] [ip4-lookup-in-table <value>] [ip6-lookup-in-table <value>] [mpls-lookup-in-table <value>] [resolve-via-host] [resolve-via-connected] [rx-ip4 <interface>] [out-labels <value value value>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 .function = vnet_ip_route_cmd,
Dave Barachb2ef4dd2016-03-23 08:56:01 -0400502 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503};
Neale Ranns15002542017-09-10 04:39:11 -0700504
505/* *INDENT-ON* */
506/*?
507 * This command is used to add or delete IPv4 Tables. All
508 * Tables must be explicitly added before that can be used. Creating a
509 * table will add both unicast and multicast FIBs
510 *
511 ?*/
512/* *INDENT-OFF* */
513VLIB_CLI_COMMAND (ip4_table_command, static) = {
514 .path = "ip table",
515 .short_help = "ip table [add|del] <table-id>",
516 .function = vnet_ip4_table_cmd,
Neale Ranns15002542017-09-10 04:39:11 -0700517};
518/* *INDENT-ON* */
519
520/* *INDENT-ON* */
521/*?
522 * This command is used to add or delete IPv4 Tables. All
523 * Tables must be explicitly added before that can be used. Creating a
524 * table will add both unicast and multicast FIBs
525 *
526 ?*/
527/* *INDENT-OFF* */
528VLIB_CLI_COMMAND (ip6_table_command, static) = {
529 .path = "ip6 table",
530 .short_help = "ip6 table [add|del] <table-id>",
531 .function = vnet_ip6_table_cmd,
Neale Ranns15002542017-09-10 04:39:11 -0700532};
533
534static clib_error_t *
535ip_table_bind_cmd (vlib_main_t * vm,
536 unformat_input_t * input,
537 vlib_cli_command_t * cmd,
538 fib_protocol_t fproto)
539{
540 vnet_main_t *vnm = vnet_get_main ();
541 clib_error_t *error = 0;
542 u32 sw_if_index, table_id;
543 int rv;
544
545 sw_if_index = ~0;
546
547 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
548 {
549 error = clib_error_return (0, "unknown interface `%U'",
550 format_unformat_error, input);
551 goto done;
552 }
553
554 if (unformat (input, "%d", &table_id))
555 ;
556 else
557 {
558 error = clib_error_return (0, "expected table id `%U'",
559 format_unformat_error, input);
560 goto done;
561 }
562
563 rv = ip_table_bind (fproto, sw_if_index, table_id, 0);
564
565 if (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE == rv)
566 {
567 error = clib_error_return (0, "IP addresses are still present on %U",
568 format_vnet_sw_if_index_name,
569 vnet_get_main(),
570 sw_if_index);
571 }
572 else if (VNET_API_ERROR_NO_SUCH_FIB == rv)
573 {
574 error = clib_error_return (0, "no such table %d", table_id);
575 }
576 else if (0 != rv)
577 {
578 error = clib_error_return (0, "unknown error");
579 }
580
581 done:
582 return error;
583}
584
585static clib_error_t *
586ip4_table_bind_cmd (vlib_main_t * vm,
587 unformat_input_t * input,
588 vlib_cli_command_t * cmd)
589{
590 return (ip_table_bind_cmd (vm , input, cmd, FIB_PROTOCOL_IP4));
591}
592
593static clib_error_t *
594ip6_table_bind_cmd (vlib_main_t * vm,
595 unformat_input_t * input,
596 vlib_cli_command_t * cmd)
597{
598 return (ip_table_bind_cmd (vm , input, cmd, FIB_PROTOCOL_IP6));
599}
600
601/*?
602 * Place the indicated interface into the supplied IPv4 FIB table (also known
Yichen Wang5c482a92018-08-02 17:12:01 -0700603 * as a VRF). The FIB table must be created using "ip table add" already. To
Neale Ranns15002542017-09-10 04:39:11 -0700604 * display the current IPv4 FIB table, use the command '<em>show ip fib</em>'.
605 * FIB table will only be displayed if a route has been added to the table, or
606 * an IP Address is assigned to an interface in the table (which adds a route
607 * automatically).
608 *
609 * @note IP addresses added after setting the interface IP table are added to
610 * the indicated FIB table. If an IP address is added prior to changing the
611 * table then this is an error. The control plane must remove these addresses
612 * first and then change the table. VPP will not automatically move the
613 * addresses from the old to the new table as it does not know the validity
614 * of such a change.
615 *
616 * @cliexpar
617 * Example of how to add an interface to an IPv4 FIB table (where 2 is the table-id):
618 * @cliexcmd{set interface ip table GigabitEthernet2/0/0 2}
619 ?*/
620/* *INDENT-OFF* */
621VLIB_CLI_COMMAND (set_interface_ip_table_command, static) =
622{
623 .path = "set interface ip table",
624 .function = ip4_table_bind_cmd,
625 .short_help = "set interface ip table <interface> <table-id>",
626};
627/* *INDENT-ON* */
628
629/*?
630 * Place the indicated interface into the supplied IPv6 FIB table (also known
Yichen Wang5c482a92018-08-02 17:12:01 -0700631 * as a VRF). The FIB table must be created using "ip6 table add" already. To
Neale Ranns15002542017-09-10 04:39:11 -0700632 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
633 * FIB table will only be displayed if a route has been added to the table, or
634 * an IP Address is assigned to an interface in the table (which adds a route
635 * automatically).
636 *
637 * @note IP addresses added after setting the interface IP table are added to
638 * the indicated FIB table. If an IP address is added prior to changing the
639 * table then this is an error. The control plane must remove these addresses
640 * first and then change the table. VPP will not automatically move the
641 * addresses from the old to the new table as it does not know the validity
642 * of such a change.
643 *
644 * @cliexpar
645 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
646 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
647 ?*/
648/* *INDENT-OFF* */
649VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
650{
651 .path = "set interface ip6 table",
652 .function = ip6_table_bind_cmd,
653 .short_help = "set interface ip6 table <interface> <table-id>"
654};
Billy McFall0683c9c2016-10-13 08:27:31 -0400655/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Neale Ranns32e1c012016-11-22 17:07:28 +0000657clib_error_t *
658vnet_ip_mroute_cmd (vlib_main_t * vm,
659 unformat_input_t * main_input, vlib_cli_command_t * cmd)
660{
661 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns097fa662018-05-01 05:17:55 -0700662 fib_route_path_t rpath, *rpaths = NULL;
Neale Ranns32e1c012016-11-22 17:07:28 +0000663 clib_error_t *error = NULL;
Neale Ranns097fa662018-05-01 05:17:55 -0700664 u32 table_id, is_del, payload_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +0000665 mfib_prefix_t pfx;
666 u32 fib_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000667 mfib_entry_flags_t eflags = 0;
Neale Ranns52456fc2017-02-15 00:38:27 -0800668 u32 gcount, scount, ss, gg, incr;
669 f64 timet[2];
Neale Rannsad69c1f2018-12-05 16:39:45 +0000670 u32 rpf_id = MFIB_RPF_ID_NONE;
Neale Ranns32e1c012016-11-22 17:07:28 +0000671
Neale Ranns52456fc2017-02-15 00:38:27 -0800672 gcount = scount = 1;
Neale Ranns32e1c012016-11-22 17:07:28 +0000673 is_del = 0;
674 table_id = 0;
Dave Barachb7b92992018-10-17 10:38:51 -0400675 clib_memset (&pfx, 0, sizeof (pfx));
676 clib_memset (&rpath, 0, sizeof (rpath));
Neale Ranns32e1c012016-11-22 17:07:28 +0000677 rpath.frp_sw_if_index = ~0;
678
679 /* Get a line of input. */
680 if (!unformat_user (main_input, unformat_line_input, line_input))
681 return 0;
682
683 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
684 {
685 if (unformat (line_input, "table %d", &table_id))
686 ;
687 else if (unformat (line_input, "del"))
688 is_del = 1;
689 else if (unformat (line_input, "add"))
690 is_del = 0;
Neale Rannsad69c1f2018-12-05 16:39:45 +0000691 else if (unformat (line_input, "rpf-id %d", &rpf_id))
692 ;
Neale Ranns52456fc2017-02-15 00:38:27 -0800693 else if (unformat (line_input, "scount %d", &scount))
694 ;
695 else if (unformat (line_input, "gcount %d", &gcount))
696 ;
Neale Ranns32e1c012016-11-22 17:07:28 +0000697 else if (unformat (line_input, "%U %U",
698 unformat_ip4_address,
699 &pfx.fp_src_addr.ip4,
700 unformat_ip4_address, &pfx.fp_grp_addr.ip4))
701 {
Elias Rudberg224735b2020-06-04 00:15:45 +0200702 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP4;
Neale Ranns32e1c012016-11-22 17:07:28 +0000703 pfx.fp_len = 64;
704 }
705 else if (unformat (line_input, "%U %U",
706 unformat_ip6_address,
707 &pfx.fp_src_addr.ip6,
708 unformat_ip6_address, &pfx.fp_grp_addr.ip6))
709 {
Elias Rudberg224735b2020-06-04 00:15:45 +0200710 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP6;
Neale Ranns32e1c012016-11-22 17:07:28 +0000711 pfx.fp_len = 256;
712 }
713 else if (unformat (line_input, "%U/%d",
714 unformat_ip4_address,
715 &pfx.fp_grp_addr.ip4, &pfx.fp_len))
716 {
Dave Barachb7b92992018-10-17 10:38:51 -0400717 clib_memset (&pfx.fp_src_addr.ip4, 0, sizeof (pfx.fp_src_addr.ip4));
Elias Rudberg224735b2020-06-04 00:15:45 +0200718 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP4;
Neale Ranns32e1c012016-11-22 17:07:28 +0000719 }
720 else if (unformat (line_input, "%U/%d",
721 unformat_ip6_address,
722 &pfx.fp_grp_addr.ip6, &pfx.fp_len))
723 {
Dave Barachb7b92992018-10-17 10:38:51 -0400724 clib_memset (&pfx.fp_src_addr.ip6, 0, sizeof (pfx.fp_src_addr.ip6));
Elias Rudberg224735b2020-06-04 00:15:45 +0200725 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP6;
Neale Ranns32e1c012016-11-22 17:07:28 +0000726 }
727 else if (unformat (line_input, "%U",
728 unformat_ip4_address, &pfx.fp_grp_addr.ip4))
729 {
Dave Barachb7b92992018-10-17 10:38:51 -0400730 clib_memset (&pfx.fp_src_addr.ip4, 0, sizeof (pfx.fp_src_addr.ip4));
Elias Rudberg224735b2020-06-04 00:15:45 +0200731 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP4;
Neale Ranns32e1c012016-11-22 17:07:28 +0000732 pfx.fp_len = 32;
733 }
734 else if (unformat (line_input, "%U",
735 unformat_ip6_address, &pfx.fp_grp_addr.ip6))
736 {
Dave Barachb7b92992018-10-17 10:38:51 -0400737 clib_memset (&pfx.fp_src_addr.ip6, 0, sizeof (pfx.fp_src_addr.ip6));
Elias Rudberg224735b2020-06-04 00:15:45 +0200738 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP6;
Neale Ranns32e1c012016-11-22 17:07:28 +0000739 pfx.fp_len = 128;
740 }
Neale Rannsad69c1f2018-12-05 16:39:45 +0000741 else if (unformat (line_input, "via local Forward"))
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700742 {
Dave Barachb7b92992018-10-17 10:38:51 -0400743 clib_memset (&rpath.frp_addr, 0, sizeof (rpath.frp_addr));
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700744 rpath.frp_sw_if_index = ~0;
745 rpath.frp_weight = 1;
746 rpath.frp_flags |= FIB_ROUTE_PATH_LOCAL;
Neale Ranns37439992018-05-30 02:08:32 -0400747 /*
748 * set the path proto appropriately for the prefix
749 */
750 rpath.frp_proto = fib_proto_to_dpo (pfx.fp_proto);
Neale Ranns097fa662018-05-01 05:17:55 -0700751 rpath.frp_mitf_flags = MFIB_ITF_FLAG_FORWARD;
Neale Rannseacc8c52019-10-01 09:49:53 -0700752
753 vec_add1 (rpaths, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000754 }
Neale Ranns097fa662018-05-01 05:17:55 -0700755 else if (unformat (line_input, "via %U",
756 unformat_fib_route_path, &rpath, &payload_proto))
757 {
758 vec_add1 (rpaths, rpath);
759 }
760 else if (unformat (line_input, "%U",
Neale Ranns32e1c012016-11-22 17:07:28 +0000761 unformat_mfib_entry_flags, &eflags))
762 ;
763 else
764 {
765 error = unformat_parse_error (line_input);
766 goto done;
767 }
768 }
769
Neale Ranns32e1c012016-11-22 17:07:28 +0000770 if (~0 == table_id)
771 {
772 /*
773 * if no table_id is passed we will manipulate the default
774 */
775 fib_index = 0;
776 }
777 else
778 {
779 fib_index = mfib_table_find (pfx.fp_proto, table_id);
780
781 if (~0 == fib_index)
782 {
783 error = clib_error_return (0, "Nonexistent table id %d", table_id);
784 goto done;
785 }
786 }
787
Neale Ranns52456fc2017-02-15 00:38:27 -0800788 timet[0] = vlib_time_now (vm);
789
790 if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +0000791 {
Neale Ranns52456fc2017-02-15 00:38:27 -0800792 incr = 1 << (32 - (pfx.fp_len % 32));
Neale Ranns32e1c012016-11-22 17:07:28 +0000793 }
794 else
795 {
Neale Ranns52456fc2017-02-15 00:38:27 -0800796 incr = 1 << (128 - (pfx.fp_len % 128));
Neale Ranns32e1c012016-11-22 17:07:28 +0000797 }
798
Neale Ranns52456fc2017-02-15 00:38:27 -0800799 for (ss = 0; ss < scount; ss++)
800 {
801 for (gg = 0; gg < gcount; gg++)
802 {
Neale Ranns097fa662018-05-01 05:17:55 -0700803 if (is_del && 0 == vec_len (rpaths))
Neale Ranns52456fc2017-02-15 00:38:27 -0800804 {
805 /* no path provided => route delete */
806 mfib_table_entry_delete (fib_index, &pfx, MFIB_SOURCE_CLI);
807 }
Neale Rannsad69c1f2018-12-05 16:39:45 +0000808 else if (eflags || (MFIB_RPF_ID_NONE != rpf_id))
Neale Ranns52456fc2017-02-15 00:38:27 -0800809 {
810 mfib_table_entry_update (fib_index, &pfx, MFIB_SOURCE_CLI,
Neale Rannsad69c1f2018-12-05 16:39:45 +0000811 rpf_id, eflags);
Neale Ranns52456fc2017-02-15 00:38:27 -0800812 }
813 else
814 {
815 if (is_del)
816 mfib_table_entry_path_remove (fib_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700817 &pfx, MFIB_SOURCE_CLI, rpaths);
Neale Ranns52456fc2017-02-15 00:38:27 -0800818 else
819 mfib_table_entry_path_update (fib_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700820 &pfx, MFIB_SOURCE_CLI, rpaths);
Neale Ranns52456fc2017-02-15 00:38:27 -0800821 }
822
823 if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
824 {
825 pfx.fp_grp_addr.ip4.as_u32 =
826 clib_host_to_net_u32 (incr +
827 clib_net_to_host_u32 (pfx.
828 fp_grp_addr.ip4.
829 as_u32));
830 }
831 else
832 {
833 int bucket = (incr < 64 ? 0 : 1);
834 pfx.fp_grp_addr.ip6.as_u64[bucket] =
835 clib_host_to_net_u64 (incr +
836 clib_net_to_host_u64 (pfx.
837 fp_grp_addr.ip6.as_u64
838 [bucket]));
839
840 }
841 }
842 if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
843 {
844 pfx.fp_src_addr.ip4.as_u32 =
845 clib_host_to_net_u32 (1 +
846 clib_net_to_host_u32 (pfx.fp_src_addr.
847 ip4.as_u32));
848 }
849 else
850 {
851 pfx.fp_src_addr.ip6.as_u64[1] =
852 clib_host_to_net_u64 (1 +
853 clib_net_to_host_u64 (pfx.fp_src_addr.
854 ip6.as_u64[1]));
855 }
856 }
857
858 timet[1] = vlib_time_now (vm);
859
860 if (scount > 1 || gcount > 1)
861 vlib_cli_output (vm, "%.6e routes/sec",
862 (scount * gcount) / (timet[1] - timet[0]));
863
Neale Ranns32e1c012016-11-22 17:07:28 +0000864done:
Neale Ranns097fa662018-05-01 05:17:55 -0700865 vec_free (rpaths);
Billy McFalla9a20e72017-02-15 11:39:12 -0500866 unformat_free (line_input);
867
Neale Ranns32e1c012016-11-22 17:07:28 +0000868 return error;
869}
870
871/*?
Paul Vinciguerra5b755e22019-10-26 19:34:40 -0400872 * This command is used to add or delete IPv4 or IPv6 multicast routes. All
Neale Ranns32e1c012016-11-22 17:07:28 +0000873 * IP Addresses ('<em><dst-ip-addr>/<width></em>',
874 * '<em><next-hop-ip-addr></em>' and '<em><adj-hop-ip-addr></em>')
875 * can be IPv4 or IPv6, but all must be of the same form in a single
876 * command. To display the current set of routes, use the commands
877 * '<em>show ip mfib</em>' and '<em>show ip6 mfib</em>'.
878 * The full set of support flags for interfaces and route is shown via;
879 * '<em>show mfib route flags</em>' and '<em>show mfib itf flags</em>'
880 * respectively.
881 * @cliexpar
882 * Example of how to add a forwarding interface to a route (and create the
883 * route if it does not exist)
884 * @cliexcmd{ip mroute add 232.1.1.1 via GigabitEthernet2/0/0 Forward}
885 * Example of how to add an accepting interface to a route (and create the
886 * route if it does not exist)
887 * @cliexcmd{ip mroute add 232.1.1.1 via GigabitEthernet2/0/1 Accept}
888 * Example of changing the route's flags to send signals via the API
889 * @cliexcmd{ip mroute add 232.1.1.1 Signal}
890
891 ?*/
892/* *INDENT-OFF* */
893VLIB_CLI_COMMAND (ip_mroute_command, static) =
894{
895 .path = "ip mroute",
Neale Rannsad69c1f2018-12-05 16:39:45 +0000896 .short_help = "ip mroute [add|del] <dst-ip-addr>/<width> [table <table-id>] [rpf-id <ID>] [via <next-hop-ip-addr> [<interface>],",
Neale Ranns32e1c012016-11-22 17:07:28 +0000897 .function = vnet_ip_mroute_cmd,
898 .is_mp_safe = 1,
899};
900/* *INDENT-ON* */
901
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700902clib_error_t *
Florin Coras595992c2017-11-06 17:17:08 -0800903vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t * args)
904{
905 u32 fib_index;
906
907 if (!vnet_sw_interface_is_api_valid (vnet_get_main (), args->sw_if_index))
908 return clib_error_return_code (0, VNET_API_ERROR_INVALID_INTERFACE, 0,
909 "invalid sw_if_index");
910
911 fib_index = fib_table_get_table_id_for_sw_if_index (args->prefix.fp_proto,
912 args->sw_if_index);
913 if (args->is_add)
914 {
915 dpo_id_t proxy_dpo = DPO_INVALID;
916 l3_proxy_dpo_add_or_lock (fib_proto_to_dpo (args->prefix.fp_proto),
917 args->sw_if_index, &proxy_dpo);
918 fib_table_entry_special_dpo_add (fib_index,
919 &args->prefix,
920 FIB_SOURCE_PROXY,
921 FIB_ENTRY_FLAG_EXCLUSIVE, &proxy_dpo);
922 dpo_reset (&proxy_dpo);
923 }
924 else
925 {
926 fib_table_entry_special_remove (fib_index, &args->prefix,
927 FIB_SOURCE_PROXY);
928 }
929 return 0;
930}
931
932u8
933ip_container_proxy_is_set (fib_prefix_t * pfx, u32 sw_if_index)
934{
935 u32 fib_index;
936 fib_node_index_t fei;
937 const dpo_id_t *dpo;
938 l3_proxy_dpo_t *l3p;
939 load_balance_t *lb0;
940
941 fib_index = fib_table_get_table_id_for_sw_if_index (pfx->fp_proto,
942 sw_if_index);
943 if (fib_index == ~0)
944 return 0;
945
946 fei = fib_table_lookup_exact_match (fib_index, pfx);
947 if (fei == FIB_NODE_INDEX_INVALID)
948 return 0;
949
950 dpo = fib_entry_contribute_ip_forwarding (fei);
951 lb0 = load_balance_get (dpo->dpoi_index);
952 dpo = load_balance_get_bucket_i (lb0, 0);
953 if (dpo->dpoi_type != DPO_L3_PROXY)
954 return 0;
955
956 l3p = l3_proxy_dpo_get (dpo->dpoi_index);
957 return (l3p->l3p_sw_if_index == sw_if_index);
958}
959
Matus Fabian75b9f452018-10-02 23:27:21 -0700960typedef struct ip_container_proxy_walk_ctx_t_
961{
962 ip_container_proxy_cb_t cb;
963 void *ctx;
964} ip_container_proxy_walk_ctx_t;
965
966static fib_table_walk_rc_t
967ip_container_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
968{
969 ip_container_proxy_walk_ctx_t *ctx = arg;
970 const fib_prefix_t *pfx;
971 const dpo_id_t *dpo;
972 load_balance_t *lb;
973 l3_proxy_dpo_t *l3p;
974
975 pfx = fib_entry_get_prefix (fei);
976 if (fib_entry_is_sourced (fei, FIB_SOURCE_PROXY))
977 {
978 dpo = fib_entry_contribute_ip_forwarding (fei);
979 lb = load_balance_get (dpo->dpoi_index);
980 dpo = load_balance_get_bucket_i (lb, 0);
981 l3p = l3_proxy_dpo_get (dpo->dpoi_index);
982 ctx->cb (pfx, l3p->l3p_sw_if_index, ctx->ctx);
983 }
984
985 return FIB_TABLE_WALK_CONTINUE;
986}
987
988void
989ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx)
990{
991 fib_table_t *fib_table;
992 ip_container_proxy_walk_ctx_t wctx = {
993 .cb = cb,
994 .ctx = ctx,
995 };
996
997 /* *INDENT-OFF* */
998 pool_foreach (fib_table, ip4_main.fibs,
999 ({
1000 fib_table_walk(fib_table->ft_index,
1001 FIB_PROTOCOL_IP4,
1002 ip_container_proxy_fib_table_walk,
1003 &wctx);
1004 }));
1005 pool_foreach (fib_table, ip6_main.fibs,
1006 ({
1007 fib_table_walk(fib_table->ft_index,
1008 FIB_PROTOCOL_IP6,
1009 ip_container_proxy_fib_table_walk,
1010 &wctx);
1011 }));
1012 /* *INDENT-ON* */
1013}
1014
Florin Coras595992c2017-11-06 17:17:08 -08001015clib_error_t *
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001016ip_container_cmd (vlib_main_t * vm,
1017 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1018{
1019 unformat_input_t _line_input, *line_input = &_line_input;
1020 fib_prefix_t pfx;
Florin Corase1682c42017-11-07 17:06:36 -08001021 u32 is_del, addr_set = 0;
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001022 vnet_main_t *vnm;
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001023 u32 sw_if_index;
1024
1025 vnm = vnet_get_main ();
1026 is_del = 0;
1027 sw_if_index = ~0;
Dave Barachb7b92992018-10-17 10:38:51 -04001028 clib_memset (&pfx, 0, sizeof (pfx));
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001029
1030 /* Get a line of input. */
1031 if (!unformat_user (main_input, unformat_line_input, line_input))
1032 return 0;
1033
1034 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1035 {
1036 if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1037 {
1038 pfx.fp_proto = FIB_PROTOCOL_IP4;
1039 pfx.fp_len = 32;
Florin Corase1682c42017-11-07 17:06:36 -08001040 addr_set = 1;
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001041 }
1042 else if (unformat (line_input, "%U",
1043 unformat_ip6_address, &pfx.fp_addr.ip6))
1044 {
1045 pfx.fp_proto = FIB_PROTOCOL_IP6;
1046 pfx.fp_len = 128;
Florin Corase1682c42017-11-07 17:06:36 -08001047 addr_set = 1;
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001048 }
1049 else if (unformat (line_input, "%U",
1050 unformat_vnet_sw_interface, vnm, &sw_if_index))
1051 ;
1052 else if (unformat (line_input, "del"))
1053 is_del = 1;
1054 else
Swarup Nayak76dc22c2017-12-05 09:46:17 +05301055 {
1056 unformat_free (line_input);
1057 return (clib_error_return (0, "unknown input '%U'",
1058 format_unformat_error, line_input));
1059 }
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001060 }
1061
Florin Corase1682c42017-11-07 17:06:36 -08001062 if (~0 == sw_if_index || !addr_set)
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001063 {
Swarup Nayak76dc22c2017-12-05 09:46:17 +05301064 unformat_free (line_input);
Florin Corase1682c42017-11-07 17:06:36 -08001065 vlib_cli_output (vm, "interface and address must be set");
1066 return 0;
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001067 }
1068
Florin Coras595992c2017-11-06 17:17:08 -08001069 vnet_ip_container_proxy_args_t args = {
1070 .prefix = pfx,
1071 .sw_if_index = sw_if_index,
1072 .is_add = !is_del,
1073 };
1074 vnet_ip_container_proxy_add_del (&args);
1075 unformat_free (line_input);
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001076 return (NULL);
1077}
1078
1079/* *INDENT-OFF* */
1080VLIB_CLI_COMMAND (ip_container_command_node, static) = {
1081 .path = "ip container",
1082 .function = ip_container_cmd,
1083 .short_help = "ip container <address> <interface>",
1084 .is_mp_safe = 1,
1085};
1086/* *INDENT-ON* */
1087
Florin Coras595992c2017-11-06 17:17:08 -08001088clib_error_t *
1089show_ip_container_cmd_fn (vlib_main_t * vm, unformat_input_t * main_input,
1090 vlib_cli_command_t * cmd)
1091{
1092 unformat_input_t _line_input, *line_input = &_line_input;
1093 vnet_main_t *vnm = vnet_get_main ();
1094 fib_prefix_t pfx;
1095 u32 sw_if_index = ~0;
1096 u8 has_proxy;
1097
1098 if (!unformat_user (main_input, unformat_line_input, line_input))
1099 return 0;
1100 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1101 {
1102 if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1103 {
1104 pfx.fp_proto = FIB_PROTOCOL_IP4;
1105 pfx.fp_len = 32;
1106 }
1107 else if (unformat (line_input, "%U",
1108 unformat_ip6_address, &pfx.fp_addr.ip6))
1109 {
1110 pfx.fp_proto = FIB_PROTOCOL_IP6;
1111 pfx.fp_len = 128;
1112 }
1113 else if (unformat (line_input, "%U",
1114 unformat_vnet_sw_interface, vnm, &sw_if_index))
1115 ;
1116 else
Swarup Nayak76dc22c2017-12-05 09:46:17 +05301117 {
1118 unformat_free (line_input);
1119 return (clib_error_return (0, "unknown input '%U'",
1120 format_unformat_error, line_input));
1121 }
Florin Coras595992c2017-11-06 17:17:08 -08001122 }
1123
1124 if (~0 == sw_if_index)
1125 {
Swarup Nayak76dc22c2017-12-05 09:46:17 +05301126 unformat_free (line_input);
Florin Coras595992c2017-11-06 17:17:08 -08001127 vlib_cli_output (vm, "no interface");
1128 return (clib_error_return (0, "no interface"));
1129 }
1130
1131 has_proxy = ip_container_proxy_is_set (&pfx, sw_if_index);
1132 vlib_cli_output (vm, "ip container proxy is: %s", has_proxy ? "on" : "off");
1133
1134 unformat_free (line_input);
1135 return 0;
1136}
1137
1138/* *INDENT-OFF* */
1139VLIB_CLI_COMMAND (show_ip_container_command, static) = {
1140 .path = "show ip container",
1141 .function = show_ip_container_cmd_fn,
1142 .short_help = "show ip container <address> <interface>",
1143 .is_mp_safe = 1,
1144};
1145/* *INDENT-ON* */
1146
Dave Barachd7cb1b52016-12-09 09:52:16 -05001147/*
1148 * fd.io coding-style-patch-verification: ON
1149 *
1150 * Local Variables:
1151 * eval: (c-set-style "gnu")
1152 * End:
1153 */