blob: e252f3a519e4741b3249ff5d06727640209073b4 [file] [log] [blame]
Ole Troan298c6952018-03-08 12:30:43 +01001/*
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:
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
16#include "ipip.h"
17#include <vppinfra/error.h>
18#include <vnet/vnet.h>
Neale Ranns61502112018-08-22 00:21:14 -070019#include <vnet/fib/fib_table.h>
Ole Troan298c6952018-03-08 12:30:43 +010020
21static 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;
26 u32 instance = ~0;
27 u32 fib_index = 0;
Neale Ranns61502112018-08-22 00:21:14 -070028 u32 table_id = 0;
Ole Troan298c6952018-03-08 12:30:43 +010029 int rv;
30 u32 num_m_args = 0;
31 u32 sw_if_index;
32 clib_error_t *error = NULL;
33 bool ip4_set = false, ip6_set = false;
34
35 /* Get a line of input. */
36 if (!unformat_user(input, unformat_line_input, line_input))
37 return 0;
38
39 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
40 if (unformat(line_input, "instance %d", &instance))
41 ;
42 else if (unformat(line_input, "src %U", unformat_ip4_address, &src.ip4)) {
43 num_m_args++;
44 ip4_set = true;
45 } else if (unformat(line_input, "dst %U", unformat_ip4_address, &dst.ip4)) {
46 num_m_args++;
47 ip4_set = true;
48 } else if (unformat(line_input, "src %U", unformat_ip6_address, &src.ip6)) {
49 num_m_args++;
50 ip6_set = true;
51 } else if (unformat(line_input, "dst %U", unformat_ip6_address, &dst.ip6)) {
52 num_m_args++;
53 ip6_set = true;
Neale Ranns61502112018-08-22 00:21:14 -070054 } else if (unformat(line_input, "outer-table-id %d", &table_id))
Ole Troan298c6952018-03-08 12:30:43 +010055 ;
56 else {
57 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
58 line_input);
59 goto done;
60 }
61 }
62
63 if (num_m_args < 2) {
64 error = clib_error_return(0, "mandatory argument(s) missing");
65 goto done;
66 }
67 if (ip4_set && ip6_set) {
68 error = clib_error_return(0, "source and destination must be of same address family");
69 goto done;
70 }
71
Neale Ranns61502112018-08-22 00:21:14 -070072 fib_index = fib_table_find(fib_ip_proto(ip6_set), table_id);
Ole Troan298c6952018-03-08 12:30:43 +010073
Neale Ranns61502112018-08-22 00:21:14 -070074 if (~0 == fib_index)
75 {
76 rv = VNET_API_ERROR_NO_SUCH_FIB;
77 }
78 else
79 {
80 rv = ipip_add_tunnel(ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
81 instance,
82 &src,
83 &dst,
84 fib_index,
Neale Ranns95346962019-11-25 13:04:44 +000085 IPIP_TUNNEL_FLAG_NONE,
86 IP_DSCP_CS0,
Neale Ranns61502112018-08-22 00:21:14 -070087 &sw_if_index);
88 }
89
90 switch (rv) {
Ole Troan298c6952018-03-08 12:30:43 +010091 case 0:
92 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(),
93 sw_if_index);
94 break;
95 case VNET_API_ERROR_IF_ALREADY_EXISTS:
96 error = clib_error_return(0, "IPIP tunnel already exists...");
97 goto done;
98 case VNET_API_ERROR_NO_SUCH_FIB:
99 error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
100 goto done;
101 case VNET_API_ERROR_NO_SUCH_ENTRY:
102 error = clib_error_return(0, "IPIP tunnel doesn't exist");
103 goto done;
104 case VNET_API_ERROR_INSTANCE_IN_USE:
105 error = clib_error_return(0, "Instance is in use");
106 goto done;
107 default:
108 error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
109 goto done;
110 }
111
112done:
113 unformat_free(line_input);
114
115 return error;
116}
117
118static clib_error_t *delete_ipip_tunnel_command_fn(vlib_main_t *vm,
119 unformat_input_t *input,
120 vlib_cli_command_t *cmd) {
121 unformat_input_t _line_input, *line_input = &_line_input;
122 int rv;
123 u32 num_m_args = 0;
124 u32 sw_if_index = ~0;
125 clib_error_t *error = NULL;
126
127 /* Get a line of input. */
128 if (!unformat_user(input, unformat_line_input, line_input))
129 return 0;
130
131 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
132 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
133 num_m_args++;
134 else {
135 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
136 line_input);
137 goto done;
138 }
139 }
140
141 if (num_m_args < 1) {
142 error = clib_error_return(0, "mandatory argument(s) missing");
143 goto done;
144 }
145
146 rv = ipip_del_tunnel(sw_if_index);
147 printf("RV %d\n", rv);
148
149done:
150 unformat_free(line_input);
151
152 return error;
153}
154
155/* *INDENT-OFF* */
156VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = {
157 .path = "create ipip tunnel",
158 .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] "
Neale Ranns61502112018-08-22 00:21:14 -0700159 "[outer-table-id <ID>]",
Ole Troan298c6952018-03-08 12:30:43 +0100160 .function = create_ipip_tunnel_command_fn,
161};
162VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = {
163 .path = "delete ipip tunnel",
Ignas Bacius3d93ad92019-10-10 16:14:47 +0300164 .short_help = "delete ipip tunnel sw_if_index <sw_if_index>",
Ole Troan298c6952018-03-08 12:30:43 +0100165 .function = delete_ipip_tunnel_command_fn,
166};
167/* *INDENT-ON* */
168
169static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
170 ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
171
172 ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6;
Neale Ranns61502112018-08-22 00:21:14 -0700173 u32 table_id;
174
175 table_id = fib_table_get_table_id(t->fib_index,
176 fib_proto_from_ip46(type));
Ole Troan298c6952018-03-08 12:30:43 +0100177 switch (t->mode) {
178 case IPIP_MODE_6RD:
Neale Ranns95346962019-11-25 13:04:44 +0000179 s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d ",
Ole Troan298c6952018-03-08 12:30:43 +0100180 t->dev_instance,
181 format_ip46_address, &t->tunnel_src, type,
Neale Ranns95346962019-11-25 13:04:44 +0000182 format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len);
Ole Troan298c6952018-03-08 12:30:43 +0100183 break;
184 case IPIP_MODE_P2P:
185 default:
Neale Ranns95346962019-11-25 13:04:44 +0000186 s = format(s, "[%d] instance %d src %U dst %U ",
Ole Troan298c6952018-03-08 12:30:43 +0100187 t->dev_instance, t->user_instance,
188 format_ip46_address, &t->tunnel_src, type,
Neale Ranns95346962019-11-25 13:04:44 +0000189 format_ip46_address, &t->tunnel_dst, type);
Ole Troan298c6952018-03-08 12:30:43 +0100190 break;
191 }
192
Neale Ranns95346962019-11-25 13:04:44 +0000193 s = format(s, "table-ID %d sw-if-idx %d flags [%U] dscp %U",
194 table_id, t->sw_if_index,
195 format_ipip_tunnel_flags, t->flags,
196 format_ip_dscp, t->dscp);
197
Ole Troan298c6952018-03-08 12:30:43 +0100198 return s;
199}
200
201static clib_error_t *show_ipip_tunnel_command_fn(vlib_main_t *vm,
202 unformat_input_t *input,
203 vlib_cli_command_t *cmd) {
204 ipip_main_t *gm = &ipip_main;
205 ipip_tunnel_t *t;
206 u32 ti = ~0;
207
208 if (pool_elts(gm->tunnels) == 0)
209 vlib_cli_output(vm, "No IPIP tunnels configured...");
210
211 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
212 if (unformat(input, "%d", &ti))
213 ;
214 else
215 break;
216 }
217
218 if (ti == ~0) {
219 /* *INDENT-OFF* */
220 pool_foreach(t, gm->tunnels,
221 ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
222 /* *INDENT-ON* */
223 } else {
224 t = pool_elt_at_index(gm->tunnels, ti);
225 if (t)
226 vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
227 }
228 return 0;
229}
230
231/* *INDENT-OFF* */
232VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
233 .path = "show ipip tunnel",
234 .function = show_ipip_tunnel_command_fn,
235};
236/* *INDENT-ON* */
237
238static clib_error_t *create_sixrd_tunnel_command_fn(vlib_main_t *vm,
239 unformat_input_t *input,
240 vlib_cli_command_t *cmd) {
241 unformat_input_t _line_input, *line_input = &_line_input;
242 ip4_address_t ip4_prefix;
243 ip6_address_t ip6_prefix;
244 ip4_address_t ip4_src;
245 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
246 u32 num_m_args = 0;
247 /* Optional arguments */
Neale Ranns61502112018-08-22 00:21:14 -0700248 u32 ip4_table_id = 0, ip4_fib_index;
249 u32 ip6_table_id = 0, ip6_fib_index;
Ole Troan298c6952018-03-08 12:30:43 +0100250 clib_error_t *error = 0;
251 bool security_check = false;
Neale Ranns61502112018-08-22 00:21:14 -0700252 int rv;
Ole Troan298c6952018-03-08 12:30:43 +0100253
254 /* Get a line of input. */
255 if (!unformat_user(input, unformat_line_input, line_input))
256 return 0;
257 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
258 if (unformat(line_input, "security-check"))
259 security_check = true;
260 else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address,
261 &ip6_prefix, &ip6_prefix_len))
262 num_m_args++;
263 else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
264 &ip4_prefix, &ip4_prefix_len))
265 num_m_args++;
266 else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
267 num_m_args++;
Neale Ranns61502112018-08-22 00:21:14 -0700268 else if (unformat(line_input, "ip4-table-id %d", &ip4_table_id))
269 ;
270 else if (unformat(line_input, "ip6-table-id %d", &ip6_table_id))
Ole Troan298c6952018-03-08 12:30:43 +0100271 ;
272 else {
273 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
274 line_input);
275 goto done;
276 }
277 }
278
279 if (num_m_args < 3) {
280 error = clib_error_return(0, "mandatory argument(s) missing");
281 goto done;
282 }
Neale Ranns61502112018-08-22 00:21:14 -0700283 ip4_fib_index = fib_table_find(FIB_PROTOCOL_IP4, ip4_table_id);
284 ip6_fib_index = fib_table_find(FIB_PROTOCOL_IP6, ip6_table_id);
Ole Troan298c6952018-03-08 12:30:43 +0100285
Neale Ranns61502112018-08-22 00:21:14 -0700286 if (~0 == ip4_fib_index)
287 {
288 error = clib_error_return(0, "No such IP4 table %d", ip4_table_id);
289 rv = VNET_API_ERROR_NO_SUCH_FIB;
290 }
291 else if (~0 == ip6_fib_index)
292 {
293 error = clib_error_return(0, "No such IP6 table %d", ip6_table_id);
294 rv = VNET_API_ERROR_NO_SUCH_FIB;
295 }
296 else
297 {
298 rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix,
299 ip4_prefix_len, &ip4_src, security_check,
300 ip4_fib_index, ip6_fib_index,
301 &sixrd_tunnel_index);
302
303 if (rv)
304 error = clib_error_return(0, "adding tunnel failed %d", rv);
305 }
306
307done:
Ole Troan298c6952018-03-08 12:30:43 +0100308 unformat_free(line_input);
309
310 return error;
311}
312
313static clib_error_t *delete_sixrd_tunnel_command_fn(vlib_main_t *vm,
314 unformat_input_t *input,
315 vlib_cli_command_t *cmd) {
316 unformat_input_t _line_input, *line_input = &_line_input;
317 u32 num_m_args = 0;
318 /* Optional arguments */
319 clib_error_t *error = 0;
320 u32 sw_if_index = ~0;
321
322 /* Get a line of input. */
323 if (!unformat_user(input, unformat_line_input, line_input))
324 return 0;
325 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
326 if (unformat(line_input, "sw_if_index %d", &sw_if_index))
327 num_m_args++;
328 else {
329 error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
330 line_input);
331 goto done;
332 }
333 }
334
335 if (num_m_args < 1) {
336 error = clib_error_return(0, "mandatory argument(s) missing");
337 goto done;
338 }
339 int rv = sixrd_del_tunnel(sw_if_index);
340 printf("RV %d\n", rv);
341
342done:
343 unformat_free(line_input);
344
345 return error;
346}
347
348/* *INDENT-OFF* */
349VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = {
350 .path = "create 6rd tunnel",
351 .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
Ignas Bacius3d93ad92019-10-10 16:14:47 +0300352 "ip4-src <ip4-addr> ip4-table-id <ID> ip6-table-id <ID> "
BenoƮt Ganne1b52ca92019-04-19 10:12:42 +0200353 "[security-check]",
Ole Troan298c6952018-03-08 12:30:43 +0100354 .function = create_sixrd_tunnel_command_fn,
355};
356VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = {
357 .path = "delete 6rd tunnel",
Ignas Bacius3d93ad92019-10-10 16:14:47 +0300358 .short_help = "delete 6rd tunnel sw_if_index <sw_if_index>",
Ole Troan298c6952018-03-08 12:30:43 +0100359 .function = delete_sixrd_tunnel_command_fn,
360};
361/* *INDENT-ON* */