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>
21 static clib_error_t *create_ipip_tunnel_command_fn(vlib_main_t *vm,
22 unformat_input_t *input,
23 vlib_cli_command_t *cmd) {
24 unformat_input_t _line_input, *line_input = &_line_input;
25 ip46_address_t src = ip46_address_initializer, dst = ip46_address_initializer;
32 clib_error_t *error = NULL;
33 bool ip4_set = false, ip6_set = false;
34 tunnel_mode_t mode = TUNNEL_MODE_P2P;
36 /* Get a line of input. */
37 if (!unformat_user(input, unformat_line_input, line_input))
40 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
41 if (unformat(line_input, "instance %d", &instance))
43 else if (unformat(line_input, "src %U", unformat_ip4_address, &src.ip4)) {
46 } else if (unformat(line_input, "dst %U", unformat_ip4_address, &dst.ip4)) {
49 } else if (unformat(line_input, "src %U", unformat_ip6_address, &src.ip6)) {
52 } else if (unformat(line_input, "dst %U", unformat_ip6_address, &dst.ip6)) {
55 } else if (unformat(line_input, "%U", unformat_tunnel_mode, &mode)) {
57 } else if (unformat(line_input, "outer-table-id %d", &table_id))
60 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
67 error = clib_error_return(0, "mandatory argument(s) missing");
70 if (ip4_set && ip6_set) {
71 error = clib_error_return(0, "source and destination must be of same address family");
75 fib_index = fib_table_find(fib_ip_proto(ip6_set), table_id);
79 rv = VNET_API_ERROR_NO_SUCH_FIB;
83 rv = ipip_add_tunnel(ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
88 TUNNEL_ENCAP_DECAP_FLAG_NONE,
96 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(),
99 case VNET_API_ERROR_IF_ALREADY_EXISTS:
100 error = clib_error_return(0, "IPIP tunnel already exists...");
102 case VNET_API_ERROR_NO_SUCH_FIB:
103 error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
105 case VNET_API_ERROR_NO_SUCH_ENTRY:
106 error = clib_error_return(0, "IPIP tunnel doesn't exist");
108 case VNET_API_ERROR_INSTANCE_IN_USE:
109 error = clib_error_return(0, "Instance is in use");
111 case VNET_API_ERROR_INVALID_DST_ADDRESS:
112 error = clib_error_return(0, "destination IP address when mode is multi-point");
115 error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
120 unformat_free(line_input);
125 static clib_error_t *delete_ipip_tunnel_command_fn(vlib_main_t *vm,
126 unformat_input_t *input,
127 vlib_cli_command_t *cmd) {
128 unformat_input_t _line_input, *line_input = &_line_input;
131 u32 sw_if_index = ~0;
132 clib_error_t *error = NULL;
134 /* Get a line of input. */
135 if (!unformat_user(input, unformat_line_input, line_input))
138 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
139 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
142 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
148 if (num_m_args < 1) {
149 error = clib_error_return(0, "mandatory argument(s) missing");
153 rv = ipip_del_tunnel(sw_if_index);
154 printf("RV %d\n", rv);
157 unformat_free(line_input);
163 VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = {
164 .path = "create ipip tunnel",
165 .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] "
166 "[outer-table-id <ID>] [p2mp]",
167 .function = create_ipip_tunnel_command_fn,
169 VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = {
170 .path = "delete ipip tunnel",
171 .short_help = "delete ipip tunnel sw_if_index <sw_if_index>",
172 .function = delete_ipip_tunnel_command_fn,
176 static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
177 ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
179 ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6;
182 table_id = fib_table_get_table_id(t->fib_index,
183 fib_proto_from_ip46(type));
186 s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d ",
188 format_ip46_address, &t->tunnel_src, type,
189 format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len);
192 s = format(s, "[%d] instance %d src %U dst %U ",
193 t->dev_instance, t->user_instance,
194 format_ip46_address, &t->tunnel_src, type,
195 format_ip46_address, &t->tunnel_dst, type);
198 s = format(s, "[%d] instance %d p2mp src %U ",
199 t->dev_instance, t->user_instance,
200 format_ip46_address, &t->tunnel_src, type);
204 s = format(s, "table-ID %d sw-if-idx %d flags [%U] dscp %U",
205 table_id, t->sw_if_index,
206 format_tunnel_encap_decap_flags, t->flags,
207 format_ip_dscp, t->dscp);
212 static clib_error_t *show_ipip_tunnel_command_fn(vlib_main_t *vm,
213 unformat_input_t *input,
214 vlib_cli_command_t *cmd) {
215 ipip_main_t *gm = &ipip_main;
219 if (pool_elts(gm->tunnels) == 0)
220 vlib_cli_output(vm, "No IPIP tunnels configured...");
222 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
223 if (unformat(input, "%d", &ti))
231 pool_foreach(t, gm->tunnels,
232 ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
235 if (pool_is_free_index (gm->tunnels, ti))
236 return clib_error_return(0, "unknown index:%d", ti);
237 t = pool_elt_at_index(gm->tunnels, ti);
239 vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
245 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
246 .path = "show ipip tunnel",
247 .function = show_ipip_tunnel_command_fn,
252 format_ipip_tunnel_key (u8 *s, va_list *args)
254 ipip_tunnel_key_t *t = va_arg(*args, ipip_tunnel_key_t *);
256 s = format (s, "src:%U dst:%U fib:%d transport:%d mode:%d",
257 format_ip46_address, &t->src, IP46_TYPE_ANY,
258 format_ip46_address, &t->dst, IP46_TYPE_ANY,
259 t->fib_index, t->transport, t->mode);
264 static clib_error_t *
265 ipip_tunnel_hash_show (vlib_main_t * vm,
266 unformat_input_t * input,
267 vlib_cli_command_t * cmd)
269 ipip_main_t *im = &ipip_main;
270 ipip_tunnel_key_t *key;
274 hash_foreach(key, index, im->tunnel_by_key,
276 vlib_cli_output (vm, " %U -> %d", format_ipip_tunnel_key, key, index);
284 * show IPSEC tunnel protection hash tables
287 VLIB_CLI_COMMAND (ipip_tunnel_hash_show_node, static) =
289 .path = "show ipip tunnel-hash",
290 .function = ipip_tunnel_hash_show,
291 .short_help = "show ipip tunnel-hash",
295 static clib_error_t *create_sixrd_tunnel_command_fn(vlib_main_t *vm,
296 unformat_input_t *input,
297 vlib_cli_command_t *cmd) {
298 unformat_input_t _line_input, *line_input = &_line_input;
299 ip4_address_t ip4_prefix;
300 ip6_address_t ip6_prefix;
301 ip4_address_t ip4_src;
302 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
304 /* Optional arguments */
305 u32 ip4_table_id = 0, ip4_fib_index;
306 u32 ip6_table_id = 0, ip6_fib_index;
307 clib_error_t *error = 0;
308 bool security_check = false;
311 /* Get a line of input. */
312 if (!unformat_user(input, unformat_line_input, line_input))
314 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
315 if (unformat(line_input, "security-check"))
316 security_check = true;
317 else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address,
318 &ip6_prefix, &ip6_prefix_len))
320 else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
321 &ip4_prefix, &ip4_prefix_len))
323 else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
325 else if (unformat(line_input, "ip4-table-id %d", &ip4_table_id))
327 else if (unformat(line_input, "ip6-table-id %d", &ip6_table_id))
330 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
336 if (num_m_args < 3) {
337 error = clib_error_return(0, "mandatory argument(s) missing");
340 ip4_fib_index = fib_table_find(FIB_PROTOCOL_IP4, ip4_table_id);
341 ip6_fib_index = fib_table_find(FIB_PROTOCOL_IP6, ip6_table_id);
343 if (~0 == ip4_fib_index)
345 error = clib_error_return(0, "No such IP4 table %d", ip4_table_id);
346 rv = VNET_API_ERROR_NO_SUCH_FIB;
348 else if (~0 == ip6_fib_index)
350 error = clib_error_return(0, "No such IP6 table %d", ip6_table_id);
351 rv = VNET_API_ERROR_NO_SUCH_FIB;
355 rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix,
356 ip4_prefix_len, &ip4_src, security_check,
357 ip4_fib_index, ip6_fib_index,
358 &sixrd_tunnel_index);
361 error = clib_error_return(0, "adding tunnel failed %d", rv);
365 unformat_free(line_input);
370 static clib_error_t *delete_sixrd_tunnel_command_fn(vlib_main_t *vm,
371 unformat_input_t *input,
372 vlib_cli_command_t *cmd) {
373 unformat_input_t _line_input, *line_input = &_line_input;
375 /* Optional arguments */
376 clib_error_t *error = 0;
377 u32 sw_if_index = ~0;
379 /* Get a line of input. */
380 if (!unformat_user(input, unformat_line_input, line_input))
382 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
383 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
386 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
392 if (num_m_args < 1) {
393 error = clib_error_return(0, "mandatory argument(s) missing");
396 int rv = sixrd_del_tunnel(sw_if_index);
397 printf("RV %d\n", rv);
400 unformat_free(line_input);
406 VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = {
407 .path = "create 6rd tunnel",
408 .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
409 "ip4-src <ip4-addr> ip4-table-id <ID> ip6-table-id <ID> "
411 .function = create_sixrd_tunnel_command_fn,
413 VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = {
414 .path = "delete 6rd tunnel",
415 .short_help = "delete 6rd tunnel sw_if_index <sw_if_index>",
416 .function = delete_sixrd_tunnel_command_fn,