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,
79 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(),
82 case VNET_API_ERROR_IF_ALREADY_EXISTS:
83 error = clib_error_return(0, "IPIP tunnel already exists...");
85 case VNET_API_ERROR_NO_SUCH_FIB:
86 error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
88 case VNET_API_ERROR_NO_SUCH_ENTRY:
89 error = clib_error_return(0, "IPIP tunnel doesn't exist");
91 case VNET_API_ERROR_INSTANCE_IN_USE:
92 error = clib_error_return(0, "Instance is in use");
95 error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
100 unformat_free(line_input);
105 static clib_error_t *delete_ipip_tunnel_command_fn(vlib_main_t *vm,
106 unformat_input_t *input,
107 vlib_cli_command_t *cmd) {
108 unformat_input_t _line_input, *line_input = &_line_input;
111 u32 sw_if_index = ~0;
112 clib_error_t *error = NULL;
114 /* Get a line of input. */
115 if (!unformat_user(input, unformat_line_input, line_input))
118 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
119 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
122 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
128 if (num_m_args < 1) {
129 error = clib_error_return(0, "mandatory argument(s) missing");
133 rv = ipip_del_tunnel(sw_if_index);
134 printf("RV %d\n", rv);
137 unformat_free(line_input);
143 VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = {
144 .path = "create ipip tunnel",
145 .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] "
146 "[outer-fib-id <fib>]",
147 .function = create_ipip_tunnel_command_fn,
149 VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = {
150 .path = "delete ipip tunnel",
151 .short_help = "delete ipip tunnel sw_if_index <sw_if_index ",
152 .function = delete_ipip_tunnel_command_fn,
156 static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
157 ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
159 ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6;
162 s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d fib-idx %d sw-if-idx %d ",
164 format_ip46_address, &t->tunnel_src, type,
165 format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len,
166 t->fib_index, t->sw_if_index);
170 s = format(s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ",
171 t->dev_instance, t->user_instance,
172 format_ip46_address, &t->tunnel_src, type,
173 format_ip46_address, &t->tunnel_dst, type,
174 t->fib_index, t->sw_if_index);
181 static clib_error_t *show_ipip_tunnel_command_fn(vlib_main_t *vm,
182 unformat_input_t *input,
183 vlib_cli_command_t *cmd) {
184 ipip_main_t *gm = &ipip_main;
188 if (pool_elts(gm->tunnels) == 0)
189 vlib_cli_output(vm, "No IPIP tunnels configured...");
191 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
192 if (unformat(input, "%d", &ti))
200 pool_foreach(t, gm->tunnels,
201 ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
204 t = pool_elt_at_index(gm->tunnels, ti);
206 vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
212 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
213 .path = "show ipip tunnel",
214 .function = show_ipip_tunnel_command_fn,
218 static clib_error_t *create_sixrd_tunnel_command_fn(vlib_main_t *vm,
219 unformat_input_t *input,
220 vlib_cli_command_t *cmd) {
221 unformat_input_t _line_input, *line_input = &_line_input;
222 ip4_address_t ip4_prefix;
223 ip6_address_t ip6_prefix;
224 ip4_address_t ip4_src;
225 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
227 /* Optional arguments */
229 clib_error_t *error = 0;
230 bool security_check = false;
232 /* Get a line of input. */
233 if (!unformat_user(input, unformat_line_input, line_input))
235 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
236 if (unformat(line_input, "security-check"))
237 security_check = true;
238 else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address,
239 &ip6_prefix, &ip6_prefix_len))
241 else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
242 &ip4_prefix, &ip4_prefix_len))
244 else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
246 else if (unformat(line_input, "fib-id %d", &fib_index))
249 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
255 if (num_m_args < 3) {
256 error = clib_error_return(0, "mandatory argument(s) missing");
259 int rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix,
260 ip4_prefix_len, &ip4_src, security_check,
261 fib_index, &sixrd_tunnel_index);
263 error = clib_error_return(0, "adding tunnel failed %d", rv);
266 unformat_free(line_input);
271 static clib_error_t *delete_sixrd_tunnel_command_fn(vlib_main_t *vm,
272 unformat_input_t *input,
273 vlib_cli_command_t *cmd) {
274 unformat_input_t _line_input, *line_input = &_line_input;
276 /* Optional arguments */
277 clib_error_t *error = 0;
278 u32 sw_if_index = ~0;
280 /* Get a line of input. */
281 if (!unformat_user(input, unformat_line_input, line_input))
283 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
284 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
287 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
293 if (num_m_args < 1) {
294 error = clib_error_return(0, "mandatory argument(s) missing");
297 int rv = sixrd_del_tunnel(sw_if_index);
298 printf("RV %d\n", rv);
301 unformat_free(line_input);
307 VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = {
308 .path = "create 6rd tunnel",
309 .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
310 "ip4-src <ip4-addr> [del]",
311 .function = create_sixrd_tunnel_command_fn,
313 VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = {
314 .path = "delete 6rd tunnel",
315 .short_help = "delete 6rd tunnel sw_if_index <sw_if_index",
316 .function = delete_sixrd_tunnel_command_fn,