IPIP: Add IP{v4,v6} over IP{v4,v6} configured tunnel support.
[vpp.git] / src / vnet / ipip / ipip_cli.c
1 /*
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>
19
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;
25   u32 instance = ~0;
26   u32 fib_index = 0;
27   int rv;
28   u32 num_m_args = 0;
29   u32 sw_if_index;
30   clib_error_t *error = NULL;
31   bool ip4_set = false, ip6_set = false;
32
33   /* Get a line of input. */
34   if (!unformat_user(input, unformat_line_input, line_input))
35     return 0;
36
37   while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
38     if (unformat(line_input, "instance %d", &instance))
39       ;
40     else if (unformat(line_input, "src %U", unformat_ip4_address, &src.ip4)) {
41       num_m_args++;
42       ip4_set = true;
43     } else if (unformat(line_input, "dst %U", unformat_ip4_address, &dst.ip4)) {
44       num_m_args++;
45       ip4_set = true;
46     } else if (unformat(line_input, "src %U", unformat_ip6_address, &src.ip6)) {
47       num_m_args++;
48       ip6_set = true;
49     } else if (unformat(line_input, "dst %U", unformat_ip6_address, &dst.ip6)) {
50       num_m_args++;
51       ip6_set = true;
52     } else if (unformat(line_input, "outer-fib-id %d", &fib_index))
53       ;
54     else {
55       error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
56                                 line_input);
57       goto done;
58     }
59   }
60
61   if (num_m_args < 2) {
62     error = clib_error_return(0, "mandatory argument(s) missing");
63     goto done;
64   } 
65   if (ip4_set && ip6_set) {
66     error = clib_error_return(0, "source and destination must be of same address family");
67     goto done;
68   }
69
70   rv = ipip_add_tunnel(ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
71                        instance,
72                        &src,
73                        &dst,
74                        fib_index,
75                        &sw_if_index);
76
77   switch (rv) {
78   case 0:
79     vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(),
80                     sw_if_index);
81     break;
82   case VNET_API_ERROR_IF_ALREADY_EXISTS:
83     error = clib_error_return(0, "IPIP tunnel already exists...");
84     goto done;
85   case VNET_API_ERROR_NO_SUCH_FIB:
86     error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
87     goto done;
88   case VNET_API_ERROR_NO_SUCH_ENTRY:
89     error = clib_error_return(0, "IPIP tunnel doesn't exist");
90     goto done;
91   case VNET_API_ERROR_INSTANCE_IN_USE:
92     error = clib_error_return(0, "Instance is in use");
93     goto done;
94   default:
95     error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
96     goto done;
97   }
98
99 done:
100   unformat_free(line_input);
101
102   return error;
103 }
104
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;
109   int rv;
110   u32 num_m_args = 0;
111   u32 sw_if_index = ~0;
112   clib_error_t *error = NULL;
113
114   /* Get a line of input. */
115   if (!unformat_user(input, unformat_line_input, line_input))
116     return 0;
117
118   while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
119     if (unformat(line_input, "sw_if_index %d", &sw_if_index))
120       num_m_args++;
121     else {
122       error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
123                                 line_input);
124       goto done;
125     }
126   }
127
128   if (num_m_args < 1) {
129     error = clib_error_return(0, "mandatory argument(s) missing");
130     goto done;
131   } 
132
133   rv = ipip_del_tunnel(sw_if_index);
134   printf("RV %d\n", rv);
135
136 done:
137   unformat_free(line_input);
138
139   return error;
140 }
141
142 /* *INDENT-OFF* */
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,
148 };
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,
153 };
154 /* *INDENT-ON* */
155
156 static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
157   ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
158
159   ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6;
160   switch (t->mode) {
161   case IPIP_MODE_6RD:
162     s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d fib-idx %d sw-if-idx %d ",
163                t->dev_instance,
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);
167     break;
168   case IPIP_MODE_P2P:
169   default:
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);
175     break;
176   }
177
178   return s;
179 }
180
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;
185   ipip_tunnel_t *t;
186   u32 ti = ~0;
187
188   if (pool_elts(gm->tunnels) == 0)
189     vlib_cli_output(vm, "No IPIP tunnels configured...");
190
191   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
192     if (unformat(input, "%d", &ti))
193       ;
194     else
195       break;
196   }
197
198   if (ti == ~0) {
199     /* *INDENT-OFF* */
200     pool_foreach(t, gm->tunnels,
201                  ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
202     /* *INDENT-ON* */
203   } else {
204     t = pool_elt_at_index(gm->tunnels, ti);
205     if (t)
206       vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
207   }
208   return 0;
209 }
210
211 /* *INDENT-OFF* */
212 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
213     .path = "show ipip tunnel",
214     .function = show_ipip_tunnel_command_fn,
215 };
216 /* *INDENT-ON* */
217
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;
226   u32 num_m_args = 0;
227   /* Optional arguments */
228   u32 fib_index = 0;
229   clib_error_t *error = 0;
230   bool security_check = false;
231
232   /* Get a line of input. */
233   if (!unformat_user(input, unformat_line_input, line_input))
234     return 0;
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))
240       num_m_args++;
241     else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
242                       &ip4_prefix, &ip4_prefix_len))
243       num_m_args++;
244     else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
245       num_m_args++;
246     else if (unformat(line_input, "fib-id %d", &fib_index))
247       ;
248     else {
249       error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
250                                 line_input);
251       goto done;
252     }
253   }
254
255   if (num_m_args < 3) {
256     error = clib_error_return(0, "mandatory argument(s) missing");
257     goto done;
258   }
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);
262   if (rv)
263     error = clib_error_return(0, "adding tunnel failed %d", rv);
264
265  done:
266   unformat_free(line_input);
267
268   return error;
269 }
270
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;
275   u32 num_m_args = 0;
276   /* Optional arguments */
277   clib_error_t *error = 0;
278   u32 sw_if_index = ~0;
279
280   /* Get a line of input. */
281   if (!unformat_user(input, unformat_line_input, line_input))
282     return 0;
283   while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
284     if (unformat(line_input, "sw_if_index %d", &sw_if_index))
285       num_m_args++;
286     else {
287       error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
288                                 line_input);
289       goto done;
290     }
291   }
292
293   if (num_m_args < 1) {
294     error = clib_error_return(0, "mandatory argument(s) missing");
295     goto done;
296   }
297   int rv = sixrd_del_tunnel(sw_if_index);
298   printf("RV %d\n", rv);
299
300 done:
301   unformat_free(line_input);
302
303   return error;
304 }
305
306 /* *INDENT-OFF* */
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,
312 };
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,
317 };
318 /* *INDENT-ON* */