FD.io VPP  v16.09
Vector Packet Processing
interface.c
Go to the documentation of this file.
1 /*
2  * gre_interface.c: gre interfaces
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/gre/gre.h>
21 #include <vnet/ip/format.h>
22 
23 u8 * format_gre_tunnel (u8 * s, va_list * args)
24 {
25  gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
26  gre_main_t * gm = &gre_main;
27 
28  s = format (s,
29  "[%d] %U (src) %U (dst) outer_fib_index %d",
30  t - gm->tunnels,
33  t->outer_fib_index);
34  return s;
35 }
36 
38  (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
39 {
40  gre_main_t * gm = &gre_main;
41  vnet_main_t * vnm = gm->vnet_main;
42  ip4_main_t * im = &ip4_main;
43  gre_tunnel_t * t;
45  u32 hw_if_index, sw_if_index;
46  u32 slot;
47  u32 outer_fib_index;
48  uword * p;
49  u64 key;
50 
51  key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
52  p = hash_get (gm->tunnel_by_key, key);
53 
54  if (a->is_add) {
55  /* check if same src/dst pair exists */
56  if (p)
57  return VNET_API_ERROR_INVALID_VALUE;
58 
60  if (! p)
61  return VNET_API_ERROR_NO_SUCH_FIB;
62 
63  outer_fib_index = p[0];
64 
66  memset (t, 0, sizeof (*t));
67 
68  if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
70 
71  hw_if_index = gm->free_gre_tunnel_hw_if_indices
73  _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
74 
75  hi = vnet_get_hw_interface (vnm, hw_if_index);
76  hi->dev_instance = t - gm->tunnels;
77  hi->hw_instance = hi->dev_instance;
78 
79  /* clear old stats of freed tunnel before reuse */
80  sw_if_index = hi->sw_if_index;
87  (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
89  } else {
90  hw_if_index = vnet_register_interface
91  (vnm, gre_device_class.index, t - gm->tunnels,
93  t - gm->tunnels);
94  hi = vnet_get_hw_interface (vnm, hw_if_index);
95  sw_if_index = hi->sw_if_index;
96  }
97 
98  t->hw_if_index = hw_if_index;
99  t->outer_fib_index = outer_fib_index;
100  t->sw_if_index = sw_if_index;
101 
103  gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
104 
105  vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
106  im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
107 
108  hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
110  /* preamble */ 8 + /* inter frame gap */ 12;
111 
112  /* Standard default gre MTU. */
114 
115  clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
116  clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
117 
118  hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
119 
121  (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
122 
123  ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
124 
125  } else { /* !is_add => delete */
126  /* tunnel needs to exist */
127  if (! p)
128  return VNET_API_ERROR_NO_SUCH_ENTRY;
129 
130  t = pool_elt_at_index (gm->tunnels, p[0]);
131 
132  sw_if_index = t->sw_if_index;
133  vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
134  /* make sure tunnel is removed from l2 bd or xconnect */
135  set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
137  gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
138 
139  hash_unset (gm->tunnel_by_key, key);
140  pool_put (gm->tunnels, t);
141  }
142 
143  if (sw_if_indexp)
144  *sw_if_indexp = sw_if_index;
145 
146  return 0;
147 }
148 
149 
150 static clib_error_t *
152  unformat_input_t * input,
153  vlib_cli_command_t * cmd)
154 {
155  unformat_input_t _line_input, * line_input = &_line_input;
156  vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
157  ip4_address_t src, dst;
158  u32 outer_fib_id = 0;
159  int rv;
160  u32 num_m_args = 0;
161  u8 is_add = 1;
162  u32 sw_if_index;
163 
164  /* Get a line of input. */
165  if (! unformat_user (input, unformat_line_input, line_input))
166  return 0;
167 
168  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
169  if (unformat (line_input, "del"))
170  is_add = 0;
171  else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
172  num_m_args++;
173  else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
174  num_m_args++;
175  else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
176  ;
177  else
178  return clib_error_return (0, "unknown input `%U'",
179  format_unformat_error, input);
180  }
181  unformat_free (line_input);
182 
183  if (num_m_args < 2)
184  return clib_error_return (0, "mandatory argument(s) missing");
185 
186  if (memcmp (&src, &dst, sizeof(src)) == 0)
187  return clib_error_return (0, "src and dst are identical");
188 
189  memset (a, 0, sizeof (*a));
190  a->is_add = is_add;
191  a->outer_fib_id = outer_fib_id;
192  clib_memcpy(&a->src, &src, sizeof(src));
193  clib_memcpy(&a->dst, &dst, sizeof(dst));
194 
195  rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
196 
197  switch(rv)
198  {
199  case 0:
200  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
201  break;
202  case VNET_API_ERROR_INVALID_VALUE:
203  return clib_error_return (0, "GRE tunnel already exists...");
204  case VNET_API_ERROR_NO_SUCH_FIB:
205  return clib_error_return (0, "outer fib ID %d doesn't exist\n",
206  outer_fib_id);
207  default:
208  return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
209  }
210 
211  return 0;
212 }
213 
214 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
215  .path = "create gre tunnel",
216  .short_help = "create gre tunnel src <addr> dst <addr> "
217  "[outer-fib-id <fib>] [del]",
218  .function = create_gre_tunnel_command_fn,
219 };
220 
221 static clib_error_t *
223  unformat_input_t * input,
224  vlib_cli_command_t * cmd)
225 {
226  gre_main_t * gm = &gre_main;
227  gre_tunnel_t * t;
228 
229  if (pool_elts (gm->tunnels) == 0)
230  vlib_cli_output (vm, "No GRE tunnels configured...");
231 
232  pool_foreach (t, gm->tunnels,
233  ({
234  vlib_cli_output (vm, "%U", format_gre_tunnel, t);
235  }));
236 
237  return 0;
238 }
239 
240 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
241  .path = "show gre tunnel",
242  .function = show_gre_tunnel_command_fn,
243 };
244 
245 /* force inclusion from application's main.c */
247 {
248  return 0;
249 }
vnet_main_t * vnet_main
Definition: gre.h:79
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
vmrglw vmrglh hi
#define hash_set(h, key, value)
Definition: hash.h:254
Definition: gre.h:60
u32 hw_if_index
Definition: gre.h:56
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
#define hash_unset(h, key)
Definition: hash.h:260
vnet_interface_main_t interface_main
Definition: vnet.h:64
uword * tunnel_by_key
Definition: gre.h:69
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 outer_fib_index
Definition: gre.h:55
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
u32 * fib_index_by_sw_if_index
Table index indexed by software interface.
Definition: ip4.h:123
format_function_t format_ip4_address
Definition: format.h:71
format_function_t format_vnet_sw_if_index_name
u32 * tunnel_index_by_sw_if_index
Definition: gre.h:75
ip4_address_t tunnel_dst
Definition: gre.h:54
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u32 sw_if_index
Definition: gre.h:57
u32 * free_gre_tunnel_hw_if_indices
Definition: gre.h:72
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:348
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
u8 * format_gre_tunnel(u8 *s, va_list *args)
Definition: interface.c:23
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:501
clib_error_t * gre_interface_init(vlib_main_t *vm)
Definition: interface.c:246
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
unsigned long u64
Definition: types.h:89
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:650
u32 set_int_l2_mode(vlib_main_t *vm, vnet_main_t *vnet_main, u32 mode, u32 sw_if_index, u32 bd_index, u32 bvi, u32 shg, u32 xc_sw_if_index)
Set the subinterface to run in l2 or l3 mode.
Definition: l2_input.c:542
unformat_function_t unformat_ip4_address
Definition: format.h:68
static clib_error_t * create_gre_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: interface.c:151
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread u16 counters, and the shared vlib_counter_t...
Definition: counter.h:321
uword * fib_index_by_table_id
Hash table mapping table id to fib index.
Definition: ip4.h:127
vlib_main_t * vlib_main
Definition: vnet.h:80
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:500
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
static clib_error_t * show_gre_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: interface.c:222
#define clib_memcpy(a, b, c)
Definition: string.h:63
ip4_address_t tunnel_src
Definition: gre.h:53
static void vnet_interface_counter_unlock(vnet_interface_main_t *im)
Definition: interface.h:524
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:516
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:345
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
gre_tunnel_t * tunnels
Definition: gre.h:62
IPv4 main type.
Definition: ip4.h:114
static void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Clear a simple counter Clears the set of per-thread u16 counters, and the u64 counter.
Definition: counter.h:143
vnet_hw_interface_class_t gre_hw_interface_class
u64 uword
Definition: types.h:112
Definition: defs.h:47
vnet_device_class_t gre_device_class
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
#define GRE_OUTPUT_NEXT_LOOKUP
Definition: gre.h:123
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:521
vlib_main_t * vlib_main
Definition: gre.h:78
u32 per_packet_overhead_bytes
Definition: interface.h:342
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
uword vlib_node_add_named_next_with_slot(vlib_main_t *vm, uword node, char *name, uword slot)
Definition: node.c:213
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:445
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
unformat_function_t unformat_line_input
Definition: format.h:281
int vnet_gre_add_del_tunnel(vnet_gre_add_del_tunnel_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:38
gre_main_t gre_main
Definition: gre.c:21
Definition: defs.h:46
#define MODE_L3
Definition: l2_input.h:215
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109