FD.io VPP  v16.06
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 
70 
71  hw_if_index = gm->free_vxlan_tunnel_hw_if_indices
73  _vec_len (gm->free_vxlan_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 
163  /* Get a line of input. */
164  if (! unformat_user (input, unformat_line_input, line_input))
165  return 0;
166 
167  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
168  if (unformat (line_input, "del"))
169  is_add = 0;
170  else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
171  num_m_args++;
172  else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
173  num_m_args++;
174  else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
175  ;
176  else
177  return clib_error_return (0, "unknown input `%U'",
178  format_unformat_error, input);
179  }
180  unformat_free (line_input);
181 
182  if (num_m_args < 2)
183  return clib_error_return (0, "mandatory argument(s) missing");
184 
185  if (memcmp (&src, &dst, sizeof(src)) == 0)
186  return clib_error_return (0, "src and dst are identical");
187 
188  memset (a, 0, sizeof (*a));
189  a->is_add = is_add;
190  a->outer_table_id = outer_fib_id;
191  clib_memcpy(&a->src, &src, sizeof(src));
192  clib_memcpy(&a->dst, &dst, sizeof(dst));
193 
194  rv = vnet_gre_add_del_tunnel (a, 0);
195 
196  switch(rv)
197  {
198  case 0:
199  break;
200  case VNET_API_ERROR_INVALID_VALUE:
201  return clib_error_return (0, "GRE tunnel already exists...");
202  case VNET_API_ERROR_NO_SUCH_FIB:
203  return clib_error_return (0, "outer fib ID %d doesn't exist\n",
204  outer_fib_id);
205  default:
206  return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
207  }
208 
209  return 0;
210 }
211 
212 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
213  .path = "create gre tunnel",
214  .short_help = "create gre tunnel src <addr> dst <addr> "
215  "[outer-fib-id <fib>] [del]",
216  .function = create_gre_tunnel_command_fn,
217 };
218 
219 static clib_error_t *
221  unformat_input_t * input,
222  vlib_cli_command_t * cmd)
223 {
224  gre_main_t * gm = &gre_main;
225  gre_tunnel_t * t;
226 
227  if (pool_elts (gm->tunnels) == 0)
228  vlib_cli_output (vm, "No GRE tunnels configured...");
229 
230  pool_foreach (t, gm->tunnels,
231  ({
232  vlib_cli_output (vm, "%U", format_gre_tunnel, t);
233  }));
234 
235  return 0;
236 }
237 
238 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
239  .path = "show gre tunnel",
240  .function = show_gre_tunnel_command_fn,
241 };
242 
243 /* force inclusion from application's main.c */
245 {
246  return 0;
247 }
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:394
vmrglw vmrglh hi
#define hash_set(h, key, value)
Definition: hash.h:237
Definition: gre.h:60
u32 hw_if_index
Definition: gre.h:56
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
#define hash_unset(h, key)
Definition: hash.h:243
vnet_interface_main_t interface_main
Definition: vnet.h:62
uword * tunnel_by_key
Definition: gre.h:69
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
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:480
u32 * free_vxlan_tunnel_hw_if_indices
Definition: gre.h:72
u32 * fib_index_by_sw_if_index
Definition: ip4.h:137
format_function_t format_ip4_address
Definition: format.h:71
u32 * tunnel_index_by_sw_if_index
Definition: gre.h:75
ip4_address_t tunnel_dst
Definition: gre.h:54
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
u32 sw_if_index
Definition: gre.h:57
#define pool_foreach(VAR, POOL, BODY)
Definition: pool.h:328
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
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:458
clib_error_t * gre_interface_init(vlib_main_t *vm)
Definition: interface.c:244
always_inline uword pool_elts(void *v)
Definition: pool.h:97
unsigned long u64
Definition: types.h:89
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
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:583
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)
Definition: l2_input.c:509
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:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
uword * fib_index_by_table_id
Definition: ip4.h:141
vlib_main_t * vlib_main
Definition: vnet.h:78
#define pool_put(P, E)
Definition: pool.h:200
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:457
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
#define pool_get_aligned(P, E, A)
Definition: pool.h:155
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:220
#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:478
always_inline vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:472
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:313
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
gre_tunnel_t * tunnels
Definition: gre.h:62
vnet_hw_interface_class_t gre_hw_interface_class
always_inline void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Definition: counter.h:277
u64 uword
Definition: types.h:112
Definition: defs.h:46
vnet_device_class_t gre_device_class
#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
always_inline void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Definition: counter.h:122
ip4_main_t ip4_main
Definition: ip4_forward.c:1394
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:462
vlib_main_t * vlib_main
Definition: gre.h:78
u32 per_packet_overhead_bytes
Definition: interface.h:310
#define clib_error_return(e, args...)
Definition: error.h:112
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:219
#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:443
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
unformat_function_t unformat_line_input
Definition: format.h:279
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:45
#define MODE_L3
Definition: l2_input.h:208