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>
20 static clib_error_t *create_ipip_tunnel_command_fn(vlib_main_t *vm,
21 unformat_input_t *input,
22 vlib_cli_command_t *cmd) {
23 unformat_input_t _line_input, *line_input = &_line_input;
24 ip46_address_t src = ip46_address_initializer, dst = ip46_address_initializer;
30 clib_error_t *error = NULL;
31 bool ip4_set = false, ip6_set = false;
33 /* Get a line of input. */
34 if (!unformat_user(input, unformat_line_input, line_input))
37 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
38 if (unformat(line_input, "instance %d", &instance))
40 else if (unformat(line_input, "src %U", unformat_ip4_address, &src.ip4)) {
43 } else if (unformat(line_input, "dst %U", unformat_ip4_address, &dst.ip4)) {
46 } else if (unformat(line_input, "src %U", unformat_ip6_address, &src.ip6)) {
49 } else if (unformat(line_input, "dst %U", unformat_ip6_address, &dst.ip6)) {
52 } else if (unformat(line_input, "outer-fib-id %d", &fib_index))
55 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
62 error = clib_error_return(0, "mandatory argument(s) missing");
65 if (ip4_set && ip6_set) {
66 error = clib_error_return(0, "source and destination must be of same address family");
70 rv = ipip_add_tunnel(ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
80 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(),
83 case VNET_API_ERROR_IF_ALREADY_EXISTS:
84 error = clib_error_return(0, "IPIP tunnel already exists...");
86 case VNET_API_ERROR_NO_SUCH_FIB:
87 error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
89 case VNET_API_ERROR_NO_SUCH_ENTRY:
90 error = clib_error_return(0, "IPIP tunnel doesn't exist");
92 case VNET_API_ERROR_INSTANCE_IN_USE:
93 error = clib_error_return(0, "Instance is in use");
96 error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
101 unformat_free(line_input);
106 static clib_error_t *delete_ipip_tunnel_command_fn(vlib_main_t *vm,
107 unformat_input_t *input,
108 vlib_cli_command_t *cmd) {
109 unformat_input_t _line_input, *line_input = &_line_input;
112 u32 sw_if_index = ~0;
113 clib_error_t *error = NULL;
115 /* Get a line of input. */
116 if (!unformat_user(input, unformat_line_input, line_input))
119 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
120 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
123 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
129 if (num_m_args < 1) {
130 error = clib_error_return(0, "mandatory argument(s) missing");
134 rv = ipip_del_tunnel(sw_if_index);
135 printf("RV %d\n", rv);
138 unformat_free(line_input);
144 VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = {
145 .path = "create ipip tunnel",
146 .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] "
147 "[outer-fib-id <fib>]",
148 .function = create_ipip_tunnel_command_fn,
150 VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = {
151 .path = "delete ipip tunnel",
152 .short_help = "delete ipip tunnel sw_if_index <sw_if_index ",
153 .function = delete_ipip_tunnel_command_fn,
157 static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
158 ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
160 ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6;
163 s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d fib-idx %d sw-if-idx %d ",
165 format_ip46_address, &t->tunnel_src, type,
166 format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len,
167 t->fib_index, t->sw_if_index);
171 s = format(s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ",
172 t->dev_instance, t->user_instance,
173 format_ip46_address, &t->tunnel_src, type,
174 format_ip46_address, &t->tunnel_dst, type,
175 t->fib_index, t->sw_if_index);
182 static clib_error_t *show_ipip_tunnel_command_fn(vlib_main_t *vm,
183 unformat_input_t *input,
184 vlib_cli_command_t *cmd) {
185 ipip_main_t *gm = &ipip_main;
189 if (pool_elts(gm->tunnels) == 0)
190 vlib_cli_output(vm, "No IPIP tunnels configured...");
192 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
193 if (unformat(input, "%d", &ti))
201 pool_foreach(t, gm->tunnels,
202 ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
205 t = pool_elt_at_index(gm->tunnels, ti);
207 vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
213 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
214 .path = "show ipip tunnel",
215 .function = show_ipip_tunnel_command_fn,
219 static clib_error_t *create_sixrd_tunnel_command_fn(vlib_main_t *vm,
220 unformat_input_t *input,
221 vlib_cli_command_t *cmd) {
222 unformat_input_t _line_input, *line_input = &_line_input;
223 ip4_address_t ip4_prefix;
224 ip6_address_t ip6_prefix;
225 ip4_address_t ip4_src;
226 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
228 /* Optional arguments */
230 clib_error_t *error = 0;
231 bool security_check = false;
233 /* Get a line of input. */
234 if (!unformat_user(input, unformat_line_input, line_input))
236 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
237 if (unformat(line_input, "security-check"))
238 security_check = true;
239 else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address,
240 &ip6_prefix, &ip6_prefix_len))
242 else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
243 &ip4_prefix, &ip4_prefix_len))
245 else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
247 else if (unformat(line_input, "fib-id %d", &fib_index))
250 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
256 if (num_m_args < 3) {
257 error = clib_error_return(0, "mandatory argument(s) missing");
260 int rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix,
261 ip4_prefix_len, &ip4_src, security_check,
262 fib_index, &sixrd_tunnel_index);
264 error = clib_error_return(0, "adding tunnel failed %d", rv);
267 unformat_free(line_input);
272 static clib_error_t *delete_sixrd_tunnel_command_fn(vlib_main_t *vm,
273 unformat_input_t *input,
274 vlib_cli_command_t *cmd) {
275 unformat_input_t _line_input, *line_input = &_line_input;
277 /* Optional arguments */
278 clib_error_t *error = 0;
279 u32 sw_if_index = ~0;
281 /* Get a line of input. */
282 if (!unformat_user(input, unformat_line_input, line_input))
284 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
285 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
288 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
294 if (num_m_args < 1) {
295 error = clib_error_return(0, "mandatory argument(s) missing");
298 int rv = sixrd_del_tunnel(sw_if_index);
299 printf("RV %d\n", rv);
302 unformat_free(line_input);
308 VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = {
309 .path = "create 6rd tunnel",
310 .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
311 "ip4-src <ip4-addr> [del]",
312 .function = create_sixrd_tunnel_command_fn,
314 VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = {
315 .path = "delete 6rd tunnel",
316 .short_help = "delete 6rd tunnel sw_if_index <sw_if_index",
317 .function = delete_sixrd_tunnel_command_fn,