FD.io VPP  v18.07-rc0-415-g6c78436
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 
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 
71  instance,
72  &src,
73  &dst,
74  fib_index,
75  0,
76  &sw_if_index);
77 
78  switch (rv) {
79  case 0:
81  sw_if_index);
82  break;
83  case VNET_API_ERROR_IF_ALREADY_EXISTS:
84  error = clib_error_return(0, "IPIP tunnel already exists...");
85  goto done;
86  case VNET_API_ERROR_NO_SUCH_FIB:
87  error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index);
88  goto done;
89  case VNET_API_ERROR_NO_SUCH_ENTRY:
90  error = clib_error_return(0, "IPIP tunnel doesn't exist");
91  goto done;
92  case VNET_API_ERROR_INSTANCE_IN_USE:
93  error = clib_error_return(0, "Instance is in use");
94  goto done;
95  default:
96  error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv);
97  goto done;
98  }
99 
100 done:
101  unformat_free(line_input);
102 
103  return error;
104 }
105 
107  unformat_input_t *input,
108  vlib_cli_command_t *cmd) {
109  unformat_input_t _line_input, *line_input = &_line_input;
110  int rv;
111  u32 num_m_args = 0;
112  u32 sw_if_index = ~0;
113  clib_error_t *error = NULL;
114 
115  /* Get a line of input. */
116  if (!unformat_user(input, unformat_line_input, line_input))
117  return 0;
118 
119  while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
120  if (unformat(line_input, "sw_if_index %d", &sw_if_index))
121  num_m_args++;
122  else {
123  error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
124  line_input);
125  goto done;
126  }
127  }
128 
129  if (num_m_args < 1) {
130  error = clib_error_return(0, "mandatory argument(s) missing");
131  goto done;
132  }
133 
134  rv = ipip_del_tunnel(sw_if_index);
135  printf("RV %d\n", rv);
136 
137 done:
138  unformat_free(line_input);
139 
140  return error;
141 }
142 
143 /* *INDENT-OFF* */
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,
149 };
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,
154 };
155 /* *INDENT-ON* */
156 
157 static u8 *format_ipip_tunnel(u8 *s, va_list *args) {
158  ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *);
159 
161  switch (t->mode) {
162  case IPIP_MODE_6RD:
163  s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d fib-idx %d sw-if-idx %d ",
164  t->dev_instance,
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);
168  break;
169  case IPIP_MODE_P2P:
170  default:
171  s = format(s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ",
173  format_ip46_address, &t->tunnel_src, type,
174  format_ip46_address, &t->tunnel_dst, type,
175  t->fib_index, t->sw_if_index);
176  break;
177  }
178 
179  return s;
180 }
181 
183  unformat_input_t *input,
184  vlib_cli_command_t *cmd) {
185  ipip_main_t *gm = &ipip_main;
186  ipip_tunnel_t *t;
187  u32 ti = ~0;
188 
189  if (pool_elts(gm->tunnels) == 0)
190  vlib_cli_output(vm, "No IPIP tunnels configured...");
191 
192  while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
193  if (unformat(input, "%d", &ti))
194  ;
195  else
196  break;
197  }
198 
199  if (ti == ~0) {
200  /* *INDENT-OFF* */
201  pool_foreach(t, gm->tunnels,
202  ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); }));
203  /* *INDENT-ON* */
204  } else {
205  t = pool_elt_at_index(gm->tunnels, ti);
206  if (t)
207  vlib_cli_output(vm, "%U", format_ipip_tunnel, t);
208  }
209  return 0;
210 }
211 
212 /* *INDENT-OFF* */
213 VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = {
214  .path = "show ipip tunnel",
215  .function = show_ipip_tunnel_command_fn,
216 };
217 /* *INDENT-ON* */
218 
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;
227  u32 num_m_args = 0;
228  /* Optional arguments */
229  u32 fib_index = 0;
230  clib_error_t *error = 0;
231  bool security_check = false;
232 
233  /* Get a line of input. */
234  if (!unformat_user(input, unformat_line_input, line_input))
235  return 0;
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))
241  num_m_args++;
242  else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address,
243  &ip4_prefix, &ip4_prefix_len))
244  num_m_args++;
245  else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
246  num_m_args++;
247  else if (unformat(line_input, "fib-id %d", &fib_index))
248  ;
249  else {
250  error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
251  line_input);
252  goto done;
253  }
254  }
255 
256  if (num_m_args < 3) {
257  error = clib_error_return(0, "mandatory argument(s) missing");
258  goto done;
259  }
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);
263  if (rv)
264  error = clib_error_return(0, "adding tunnel failed %d", rv);
265 
266  done:
267  unformat_free(line_input);
268 
269  return error;
270 }
271 
273  unformat_input_t *input,
274  vlib_cli_command_t *cmd) {
275  unformat_input_t _line_input, *line_input = &_line_input;
276  u32 num_m_args = 0;
277  /* Optional arguments */
278  clib_error_t *error = 0;
279  u32 sw_if_index = ~0;
280 
281  /* Get a line of input. */
282  if (!unformat_user(input, unformat_line_input, line_input))
283  return 0;
284  while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
285  if (unformat(line_input, "sw_if_index %d", &sw_if_index))
286  num_m_args++;
287  else {
288  error = clib_error_return(0, "unknown input `%U'", format_unformat_error,
289  line_input);
290  goto done;
291  }
292  }
293 
294  if (num_m_args < 1) {
295  error = clib_error_return(0, "mandatory argument(s) missing");
296  goto done;
297  }
298  int rv = sixrd_del_tunnel(sw_if_index);
299  printf("RV %d\n", rv);
300 
301 done:
302  unformat_free(line_input);
303 
304  return error;
305 }
306 
307 /* *INDENT-OFF* */
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,
313 };
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,
318 };
319 /* *INDENT-ON* */
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:20
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:182
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:106
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:440
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:219
unsigned int u32
Definition: types.h:88
unformat_function_t unformat_line_input
Definition: format.h:281
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
struct _unformat_input_t unformat_input_t
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 fib_index, u32 *sw_if_index)
Definition: sixrd.c:281
static u8 * format_ipip_tunnel(u8 *s, va_list *args)
Definition: ipip_cli.c:157
ipip_tunnel_t * tunnels
Definition: ipip.h:108
int sixrd_del_tunnel(u32 sw_if_index)
Definition: sixrd.c:385
#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:272
ipip_main_t ipip_main
Definition: ipip.c:28
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
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
ipip_transport_t transport
Definition: ipip.h:75
u32 sw_if_index
Definition: ipip.h:81
ipip_mode_t mode
Definition: ipip.h:74
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
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
struct ipip_tunnel_t::@227::@230 sixrd
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:169
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128