FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
ipip_cli.c
Go to the documentation of this file.
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 #include <vnet/fib/fib_table.h>
20 
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;
28  u32 table_id = 0;
29  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;
54  } else if (unformat(line_input, "outer-table-id %d", &table_id))
55  ;
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 
72  fib_index = fib_table_find(fib_ip_proto(ip6_set), table_id);
73 
74  if (~0 == fib_index)
75  {
76  rv = VNET_API_ERROR_NO_SUCH_FIB;
77  }
78  else
79  {
81  instance,
82  &src,
83  &dst,
84  fib_index,
85  0,
86  &sw_if_index);
87  }
88 
89  switch (rv) {
90  case 0:
92  sw_if_index);
93  break;
94  case VNET_API_ERROR_IF_ALREADY_EXISTS:
95  error = clib_error_return(0, "IPIP tunnel already exists...");
96  goto done;
97  case VNET_API_ERROR_NO_SUCH_FIB:
98  error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
99  goto done;
100  case VNET_API_ERROR_NO_SUCH_ENTRY:
101  error = clib_error_return(0, "IPIP tunnel doesn't exist");
102  goto done;
103  case VNET_API_ERROR_INSTANCE_IN_USE:
104  error = clib_error_return(0, "Instance is in use");
105  goto done;
106  default:
107  error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
108  goto done;
109  }
110 
111 done:
112  unformat_free(line_input);
113 
114  return error;
115 }
116 
118  unformat_input_t *input,
119  vlib_cli_command_t *cmd) {
120  unformat_input_t _line_input, *line_input = &_line_input;
121  int rv;
122  u32 num_m_args = 0;
123  u32 sw_if_index = ~0;
124  clib_error_t *error = NULL;
125 
126  /* Get a line of input. */
127  if (!unformat_user(input, unformat_line_input, line_input))
128  return 0;
129 
130  while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
131  if (unformat(line_input, "sw_if_index %d", &sw_if_index))
132  num_m_args++;
133  else {
134  error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
135  line_input);
136  goto done;
137  }
138  }
139 
140  if (num_m_args < 1) {
141  error = clib_error_return(0, "mandatory argument(s) missing");
142  goto done;
143  }
144 
145  rv = ipip_del_tunnel(sw_if_index);
146  printf("RV %d\n", rv);
147 
148 done:
149  unformat_free(line_input);
150 
151  return error;
152 }
153 
154 /* *INDENT-OFF* */
155 VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = {
156  .path = "create ipip tunnel",
157  .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] "
158  "[outer-table-id <ID>]",
159  .function = create_ipip_tunnel_command_fn,
160 };
161 VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = {
162  .path = "delete ipip tunnel",
163  .short_help = "delete ipip tunnel sw_if_index <sw_if_index ",
164  .function = delete_ipip_tunnel_command_fn,
165 };
166 /* *INDENT-ON* */
167 
168 static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
169  ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
170 
172  u32 table_id;
173 
174  table_id = fib_table_get_table_id(t->fib_index,
178  switch (t->mode) {
179  case IPIP_MODE_6RD:
180  s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d table-ID %d sw-if-idx %d ",
181  t->dev_instance,
182  format_ip46_address, &t->tunnel_src, type,
183  format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len,
184  table_id, t->sw_if_index);
185  break;
186  case IPIP_MODE_P2P:
187  default:
188  s = format(s, "[%d] instance %d src %U dst %U table-ID %d sw-if-idx %d ",
190  format_ip46_address, &t->tunnel_src, type,
191  format_ip46_address, &t->tunnel_dst, type,
192  table_id, t->sw_if_index);
193  break;
194  }
195 
196  return s;
197 }
198 
200  unformat_input_t *input,
201  vlib_cli_command_t *cmd) {
202  ipip_main_t *gm = &ipip_main;
203  ipip_tunnel_t *t;
204  u32 ti = ~0;
205 
206  if (pool_elts(gm->tunnels) == 0)
207  vlib_cli_output(vm, "No IPIP tunnels configured...");
208 
209  while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
210  if (unformat(input, "%d", &ti))
211  ;
212  else
213  break;
214  }
215 
216  if (ti == ~0) {
217  /* *INDENT-OFF* */
218  pool_foreach(t, gm->tunnels,
219  ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
220  /* *INDENT-ON* */
221  } else {
222  t = pool_elt_at_index(gm->tunnels, ti);
223  if (t)
224  vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
225  }
226  return 0;
227 }
228 
229 /* *INDENT-OFF* */
230 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
231  .path = "show ipip tunnel",
232  .function = show_ipip_tunnel_command_fn,
233 };
234 /* *INDENT-ON* */
235 
237  unformat_input_t *input,
238  vlib_cli_command_t *cmd) {
239  unformat_input_t _line_input, *line_input = &_line_input;
240  ip4_address_t ip4_prefix;
241  ip6_address_t ip6_prefix;
242  ip4_address_t ip4_src;
243  u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
244  u32 num_m_args = 0;
245  /* Optional arguments */
246  u32 ip4_table_id = 0, ip4_fib_index;
247  u32 ip6_table_id = 0, ip6_fib_index;
248  clib_error_t *error = 0;
249  bool security_check = false;
250  int rv;
251 
252  /* Get a line of input. */
253  if (!unformat_user(input, unformat_line_input, line_input))
254  return 0;
255  while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
256  if (unformat(line_input, "security-check"))
257  security_check = true;
258  else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address,
259  &ip6_prefix, &ip6_prefix_len))
260  num_m_args++;
261  else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
262  &ip4_prefix, &ip4_prefix_len))
263  num_m_args++;
264  else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
265  num_m_args++;
266  else if (unformat(line_input, "ip4-table-id %d", &ip4_table_id))
267  ;
268  else if (unformat(line_input, "ip6-table-id %d", &ip6_table_id))
269  ;
270  else {
271  error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
272  line_input);
273  goto done;
274  }
275  }
276 
277  if (num_m_args < 3) {
278  error = clib_error_return(0, "mandatory argument(s) missing");
279  goto done;
280  }
281  ip4_fib_index = fib_table_find(FIB_PROTOCOL_IP4, ip4_table_id);
282  ip6_fib_index = fib_table_find(FIB_PROTOCOL_IP6, ip6_table_id);
283 
284  if (~0 == ip4_fib_index)
285  {
286  error = clib_error_return(0, "No such IP4 table %d", ip4_table_id);
287  rv = VNET_API_ERROR_NO_SUCH_FIB;
288  }
289  else if (~0 == ip6_fib_index)
290  {
291  error = clib_error_return(0, "No such IP6 table %d", ip6_table_id);
292  rv = VNET_API_ERROR_NO_SUCH_FIB;
293  }
294  else
295  {
296  rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix,
297  ip4_prefix_len, &ip4_src, security_check,
298  ip4_fib_index, ip6_fib_index,
299  &sixrd_tunnel_index);
300 
301  if (rv)
302  error = clib_error_return(0, "adding tunnel failed %d", rv);
303  }
304 
305 done:
306  unformat_free(line_input);
307 
308  return error;
309 }
310 
312  unformat_input_t *input,
313  vlib_cli_command_t *cmd) {
314  unformat_input_t _line_input, *line_input = &_line_input;
315  u32 num_m_args = 0;
316  /* Optional arguments */
317  clib_error_t *error = 0;
318  u32 sw_if_index = ~0;
319 
320  /* Get a line of input. */
321  if (!unformat_user(input, unformat_line_input, line_input))
322  return 0;
323  while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
324  if (unformat(line_input, "sw_if_index %d", &sw_if_index))
325  num_m_args++;
326  else {
327  error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
328  line_input);
329  goto done;
330  }
331  }
332 
333  if (num_m_args < 1) {
334  error = clib_error_return(0, "mandatory argument(s) missing");
335  goto done;
336  }
337  int rv = sixrd_del_tunnel(sw_if_index);
338  printf("RV %d\n", rv);
339 
340 done:
341  unformat_free(line_input);
342 
343  return error;
344 }
345 
346 /* *INDENT-OFF* */
347 VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = {
348  .path = "create 6rd tunnel",
349  .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
350  "ip4-src <ip4-addr> table-id <ID> [del]",
351  .function = create_sixrd_tunnel_command_fn,
352 };
353 VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = {
354  .path = "delete 6rd tunnel",
355  .short_help = "delete 6rd tunnel sw_if_index <sw_if_index",
356  .function = delete_sixrd_tunnel_command_fn,
357 };
358 /* *INDENT-ON* */
int sixrd_add_tunnel(ip6_address_t *ip6_prefix, u8 ip6_prefix_len, ip4_address_t *ip4_prefix, u8 ip4_prefix_len, ip4_address_t *ip4_src, bool security_check, u32 ip4_fib_index, u32 ip6_fib_index, u32 *sw_if_index)
Definition: sixrd.c:282
static clib_error_t * create_ipip_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipip_cli.c:21
ip46_type_t
Definition: format.h:63
static clib_error_t * show_ipip_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipip_cli.c:199
A representation of a IPIP tunnel.
Definition: ipip.h:69
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
int ipip_add_tunnel(ipip_transport_t transport, u32 instance, ip46_address_t *src, ip46_address_t *dst, u32 fib_index, u8 tc_tos, u32 *sw_if_indexp)
Definition: ipip.c:463
ip46_address_t tunnel_src
Definition: ipip.h:77
#define NULL
Definition: clib.h:55
static clib_error_t * delete_ipip_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipip_cli.c:117
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
format_function_t format_ip46_address
Definition: format.h:61
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:443
unformat_function_t unformat_ip4_address
Definition: format.h:76
u32 user_instance
Definition: ipip.h:83
#define clib_error_return(e, args...)
Definition: error.h:99
static clib_error_t * create_sixrd_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipip_cli.c:236
unsigned int u32
Definition: types.h:88
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1056
unformat_function_t unformat_line_input
Definition: format.h:282
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
struct _unformat_input_t unformat_input_t
static u8 * format_ipip_tunnel(u8 *s, va_list *args)
Definition: ipip_cli.c:168
ipip_tunnel_t * tunnels
Definition: ipip.h:109
int sixrd_del_tunnel(u32 sw_if_index)
Definition: sixrd.c:387
#define ip46_address_initializer
Definition: ip6_packet.h:89
unformat_function_t unformat_ip6_address
Definition: format.h:97
static clib_error_t * delete_sixrd_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipip_cli.c:311
ipip_main_t ipip_main
Definition: ipip.c:28
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
format_function_t format_ip6_address
Definition: format.h:99
vlib_main_t * vm
Definition: buffer.c:294
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
u32 fib_index
Definition: ipip.h:79
struct ipip_tunnel_t::@229::@232 sixrd
ipip_transport_t transport
Definition: ipip.h:75
u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto)
Get the Table-ID of the FIB from protocol and index.
Definition: fib_table.c:1045
u32 sw_if_index
Definition: ipip.h:81
static fib_protocol_t fib_ip_proto(bool is_ip6)
Convert from boolean is_ip6 to FIB protocol.
Definition: fib_types.h:80
ipip_mode_t mode
Definition: ipip.h:74
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
int ipip_del_tunnel(u32 sw_if_index)
Definition: ipip.c:562
u32 dev_instance
Definition: ipip.h:82
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
ip46_address_t tunnel_dst
Definition: ipip.h:78
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:681
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128