2 * Copyright (c) 2018 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vppinfra/error.h>
18 #include <vnet/vnet.h>
19 #include <vnet/fib/fib_table.h>
22 create_ipip_tunnel_command_fn (vlib_main_t * vm,
23 unformat_input_t * input,
24 vlib_cli_command_t * cmd)
26 unformat_input_t _line_input, *line_input = &_line_input;
27 ip46_address_t src = ip46_address_initializer, dst =
28 ip46_address_initializer;
35 clib_error_t *error = NULL;
36 bool ip4_set = false, ip6_set = false;
37 tunnel_mode_t mode = TUNNEL_MODE_P2P;
39 /* Get a line of input. */
40 if (!unformat_user (input, unformat_line_input, line_input))
43 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
45 if (unformat (line_input, "instance %d", &instance))
48 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
54 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
60 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
66 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
71 else if (unformat (line_input, "%U", unformat_tunnel_mode, &mode))
75 else if (unformat (line_input, "outer-table-id %d", &table_id))
80 clib_error_return (0, "unknown input `%U'", format_unformat_error,
88 error = clib_error_return (0, "mandatory argument(s) missing");
91 if (ip4_set && ip6_set)
95 "source and destination must be of same address family");
99 fib_index = fib_table_find (fib_ip_proto (ip6_set), table_id);
103 rv = VNET_API_ERROR_NO_SUCH_FIB;
107 rv = ipip_add_tunnel (ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
112 TUNNEL_ENCAP_DECAP_FLAG_NONE,
113 IP_DSCP_CS0, mode, &sw_if_index);
119 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
120 vnet_get_main (), sw_if_index);
122 case VNET_API_ERROR_IF_ALREADY_EXISTS:
123 error = clib_error_return (0, "IPIP tunnel already exists...");
125 case VNET_API_ERROR_NO_SUCH_FIB:
127 clib_error_return (0, "outer fib ID %d doesn't exist\n", fib_index);
129 case VNET_API_ERROR_NO_SUCH_ENTRY:
130 error = clib_error_return (0, "IPIP tunnel doesn't exist");
132 case VNET_API_ERROR_INSTANCE_IN_USE:
133 error = clib_error_return (0, "Instance is in use");
135 case VNET_API_ERROR_INVALID_DST_ADDRESS:
137 clib_error_return (0,
138 "destination IP address when mode is multi-point");
142 clib_error_return (0, "vnet_ipip_add_del_tunnel returned %d", rv);
147 unformat_free (line_input);
152 static clib_error_t *
153 delete_ipip_tunnel_command_fn (vlib_main_t * vm,
154 unformat_input_t * input,
155 vlib_cli_command_t * cmd)
157 unformat_input_t _line_input, *line_input = &_line_input;
160 u32 sw_if_index = ~0;
161 clib_error_t *error = NULL;
163 /* Get a line of input. */
164 if (!unformat_user (input, unformat_line_input, line_input))
167 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
169 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
174 clib_error_return (0, "unknown input `%U'", format_unformat_error,
182 error = clib_error_return (0, "mandatory argument(s) missing");
186 rv = ipip_del_tunnel (sw_if_index);
187 printf ("RV %d\n", rv);
190 unformat_free (line_input);
196 VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = {
197 .path = "create ipip tunnel",
198 .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] "
199 "[outer-table-id <ID>] [p2mp]",
200 .function = create_ipip_tunnel_command_fn,
202 VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = {
203 .path = "delete ipip tunnel",
204 .short_help = "delete ipip tunnel sw_if_index <sw_if_index>",
205 .function = delete_ipip_tunnel_command_fn,
210 format_ipip_tunnel (u8 * s, va_list * args)
212 ipip_tunnel_t *t = va_arg (*args, ipip_tunnel_t *);
215 (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6;
218 table_id = fib_table_get_table_id (t->fib_index,
219 fib_proto_from_ip46 (type));
223 s = format (s, "[%d] 6rd src %U ip6-pfx %U/%d ",
225 format_ip46_address, &t->tunnel_src, type,
226 format_ip6_address, &t->sixrd.ip6_prefix,
227 t->sixrd.ip6_prefix_len);
230 s = format (s, "[%d] instance %d src %U dst %U ",
231 t->dev_instance, t->user_instance,
232 format_ip46_address, &t->tunnel_src, type,
233 format_ip46_address, &t->tunnel_dst, type);
236 s = format (s, "[%d] instance %d p2mp src %U ",
237 t->dev_instance, t->user_instance,
238 format_ip46_address, &t->tunnel_src, type);
242 s = format (s, "table-ID %d sw-if-idx %d flags [%U] dscp %U",
243 table_id, t->sw_if_index,
244 format_tunnel_encap_decap_flags, t->flags,
245 format_ip_dscp, t->dscp);
250 static clib_error_t *
251 show_ipip_tunnel_command_fn (vlib_main_t * vm,
252 unformat_input_t * input,
253 vlib_cli_command_t * cmd)
255 ipip_main_t *gm = &ipip_main;
259 if (pool_elts (gm->tunnels) == 0)
260 vlib_cli_output (vm, "No IPIP tunnels configured...");
262 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
264 if (unformat (input, "%d", &ti))
273 pool_foreach(t, gm->tunnels,
274 ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
279 if (pool_is_free_index (gm->tunnels, ti))
280 return clib_error_return (0, "unknown index:%d", ti);
281 t = pool_elt_at_index (gm->tunnels, ti);
283 vlib_cli_output (vm, "%U", format_ipip_tunnel, t);
289 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
290 .path = "show ipip tunnel",
291 .function = show_ipip_tunnel_command_fn,
296 format_ipip_tunnel_key (u8 * s, va_list * args)
298 ipip_tunnel_key_t *t = va_arg (*args, ipip_tunnel_key_t *);
300 s = format (s, "src:%U dst:%U fib:%d transport:%d mode:%d",
301 format_ip46_address, &t->src, IP46_TYPE_ANY,
302 format_ip46_address, &t->dst, IP46_TYPE_ANY,
303 t->fib_index, t->transport, t->mode);
308 static clib_error_t *
309 ipip_tunnel_hash_show (vlib_main_t * vm,
310 unformat_input_t * input, vlib_cli_command_t * cmd)
312 ipip_main_t *im = &ipip_main;
313 ipip_tunnel_key_t *key;
317 hash_foreach(key, index, im->tunnel_by_key,
319 vlib_cli_output (vm, " %U -> %d", format_ipip_tunnel_key, key, index);
327 * show IPSEC tunnel protection hash tables
330 VLIB_CLI_COMMAND (ipip_tunnel_hash_show_node, static) =
332 .path = "show ipip tunnel-hash",
333 .function = ipip_tunnel_hash_show,
334 .short_help = "show ipip tunnel-hash",
338 static clib_error_t *
339 create_sixrd_tunnel_command_fn (vlib_main_t * vm,
340 unformat_input_t * input,
341 vlib_cli_command_t * cmd)
343 unformat_input_t _line_input, *line_input = &_line_input;
344 ip4_address_t ip4_prefix;
345 ip6_address_t ip6_prefix;
346 ip4_address_t ip4_src;
347 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
349 /* Optional arguments */
350 u32 ip4_table_id = 0, ip4_fib_index;
351 u32 ip6_table_id = 0, ip6_fib_index;
352 clib_error_t *error = 0;
353 bool security_check = false;
356 /* Get a line of input. */
357 if (!unformat_user (input, unformat_line_input, line_input))
359 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
361 if (unformat (line_input, "security-check"))
362 security_check = true;
363 else if (unformat (line_input, "ip6-pfx %U/%d", unformat_ip6_address,
364 &ip6_prefix, &ip6_prefix_len))
366 else if (unformat (line_input, "ip4-pfx %U/%d", unformat_ip4_address,
367 &ip4_prefix, &ip4_prefix_len))
371 (line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
373 else if (unformat (line_input, "ip4-table-id %d", &ip4_table_id))
375 else if (unformat (line_input, "ip6-table-id %d", &ip6_table_id))
380 clib_error_return (0, "unknown input `%U'", format_unformat_error,
388 error = clib_error_return (0, "mandatory argument(s) missing");
391 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ip4_table_id);
392 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ip6_table_id);
394 if (~0 == ip4_fib_index)
396 error = clib_error_return (0, "No such IP4 table %d", ip4_table_id);
397 rv = VNET_API_ERROR_NO_SUCH_FIB;
399 else if (~0 == ip6_fib_index)
401 error = clib_error_return (0, "No such IP6 table %d", ip6_table_id);
402 rv = VNET_API_ERROR_NO_SUCH_FIB;
406 rv = sixrd_add_tunnel (&ip6_prefix, ip6_prefix_len, &ip4_prefix,
407 ip4_prefix_len, &ip4_src, security_check,
408 ip4_fib_index, ip6_fib_index,
409 &sixrd_tunnel_index);
412 error = clib_error_return (0, "adding tunnel failed %d", rv);
416 unformat_free (line_input);
421 static clib_error_t *
422 delete_sixrd_tunnel_command_fn (vlib_main_t * vm,
423 unformat_input_t * input,
424 vlib_cli_command_t * cmd)
426 unformat_input_t _line_input, *line_input = &_line_input;
428 /* Optional arguments */
429 clib_error_t *error = 0;
430 u32 sw_if_index = ~0;
432 /* Get a line of input. */
433 if (!unformat_user (input, unformat_line_input, line_input))
435 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
437 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
442 clib_error_return (0, "unknown input `%U'", format_unformat_error,
450 error = clib_error_return (0, "mandatory argument(s) missing");
453 int rv = sixrd_del_tunnel (sw_if_index);
454 printf ("RV %d\n", rv);
457 unformat_free (line_input);
463 VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = {
464 .path = "create 6rd tunnel",
465 .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
466 "ip4-src <ip4-addr> ip4-table-id <ID> ip6-table-id <ID> "
468 .function = create_sixrd_tunnel_command_fn,
470 VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = {
471 .path = "delete 6rd tunnel",
472 .short_help = "delete 6rd tunnel sw_if_index <sw_if_index>",
473 .function = delete_sixrd_tunnel_command_fn,
478 * fd.io coding-style-patch-verification: ON
481 * eval: (c-set-style "gnu")