blob: 0faed13eb293bd4a458b18c96aaeab73e94dd9e3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * gre.c: gre
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/gre/gre.h>
Neale Rannsb80c5362016-10-08 13:03:40 +010020#include <vnet/adj/adj_midchain.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
22gre_main_t gre_main;
23
Ed Warnickecb9cada2015-12-08 15:45:58 -070024typedef struct {
25 union {
26 ip4_and_gre_header_t ip4_and_gre;
27 u64 as_u64[3];
28 };
29} ip4_and_gre_union_t;
30
31
32/* Packet trace structure */
33typedef struct {
34 /* Tunnel-id / index in tunnel vector */
35 u32 tunnel_id;
36
37 /* pkt length */
38 u32 length;
39
40 /* tunnel ip4 addresses */
41 ip4_address_t src;
42 ip4_address_t dst;
43} gre_tx_trace_t;
44
45u8 * format_gre_tx_trace (u8 * s, va_list * args)
46{
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 gre_tx_trace_t * t = va_arg (*args, gre_tx_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010050
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
Neale Ranns5e575b12016-10-03 09:40:25 +010052 t->tunnel_id, clib_net_to_host_u16 (t->length),
53 format_ip4_address, &t->src.as_u8,
54 format_ip4_address, &t->dst.as_u8);
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 return s;
56}
57
58u8 * format_gre_protocol (u8 * s, va_list * args)
59{
60 gre_protocol_t p = va_arg (*args, u32);
61 gre_main_t * gm = &gre_main;
62 gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
63
64 if (pi)
65 s = format (s, "%s", pi->name);
66 else
67 s = format (s, "0x%04x", p);
68
69 return s;
70}
71
72u8 * format_gre_header_with_length (u8 * s, va_list * args)
73{
74 gre_main_t * gm = &gre_main;
75 gre_header_t * h = va_arg (*args, gre_header_t *);
76 u32 max_header_bytes = va_arg (*args, u32);
77 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
78 uword indent, header_bytes;
79
80 header_bytes = sizeof (h[0]);
81 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
82 return format (s, "gre header truncated");
83
84 indent = format_get_indent (s);
85
86 s = format (s, "GRE %U", format_gre_protocol, p);
87
88 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
89 {
90 gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
91 vlib_node_t * node = vlib_get_node (gm->vlib_main, pi->node_index);
92 if (node->format_buffer)
93 s = format (s, "\n%U%U",
94 format_white_space, indent,
95 node->format_buffer, (void *) (h + 1),
96 max_header_bytes - header_bytes);
97 }
98
99 return s;
100}
101
102u8 * format_gre_header (u8 * s, va_list * args)
103{
104 gre_header_t * h = va_arg (*args, gre_header_t *);
105 return format (s, "%U", format_gre_header_with_length, h, 0);
106}
107
108/* Returns gre protocol as an int in host byte order. */
109uword
110unformat_gre_protocol_host_byte_order (unformat_input_t * input,
111 va_list * args)
112{
113 u16 * result = va_arg (*args, u16 *);
114 gre_main_t * gm = &gre_main;
115 int i;
116
117 /* Named type. */
118 if (unformat_user (input, unformat_vlib_number_by_name,
119 gm->protocol_info_by_name, &i))
120 {
121 gre_protocol_info_t * pi = vec_elt_at_index (gm->protocol_infos, i);
122 *result = pi->protocol;
123 return 1;
124 }
125
126 return 0;
127}
128
129uword
130unformat_gre_protocol_net_byte_order (unformat_input_t * input,
131 va_list * args)
132{
133 u16 * result = va_arg (*args, u16 *);
134 if (! unformat_user (input, unformat_gre_protocol_host_byte_order, result))
135 return 0;
136 *result = clib_host_to_net_u16 ((u16) *result);
137 return 1;
138}
139
140uword
141unformat_gre_header (unformat_input_t * input, va_list * args)
142{
143 u8 ** result = va_arg (*args, u8 **);
144 gre_header_t _h, * h = &_h;
145 u16 p;
146
147 if (! unformat (input, "%U",
148 unformat_gre_protocol_host_byte_order, &p))
149 return 0;
150
151 h->protocol = clib_host_to_net_u16 (p);
152
153 /* Add header to result. */
154 {
155 void * p;
156 u32 n_bytes = sizeof (h[0]);
157
158 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100159 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100161
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 return 1;
163}
164
Neale Rannsb80c5362016-10-08 13:03:40 +0100165static int
166gre_proto_from_vnet_link (vnet_link_t link)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167{
Neale Rannsb80c5362016-10-08 13:03:40 +0100168 switch (link)
169 {
170 case VNET_LINK_IP4:
171 return (GRE_PROTOCOL_ip4);
172 case VNET_LINK_IP6:
173 return (GRE_PROTOCOL_ip6);
174 case VNET_LINK_MPLS:
175 return (GRE_PROTOCOL_mpls_unicast);
176 case VNET_LINK_ETHERNET:
177 return (GRE_PROTOCOL_teb);
178 case VNET_LINK_ARP:
179 return (GRE_PROTOCOL_arp);
180 }
181 ASSERT(0);
182 return (GRE_PROTOCOL_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183}
184
Neale Rannsb80c5362016-10-08 13:03:40 +0100185static u8*
186gre_build_rewrite (vnet_main_t * vnm,
187 u32 sw_if_index,
188 vnet_link_t link_type,
189 const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
191 gre_main_t * gm = &gre_main;
Neale Rannsb80c5362016-10-08 13:03:40 +0100192 ip4_and_gre_header_t * h;
193 u8* rewrite = NULL;
194 gre_tunnel_t *t;
195 u32 ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Neale Rannsb80c5362016-10-08 13:03:40 +0100197 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Neale Rannsb80c5362016-10-08 13:03:40 +0100199 if (~0 == ti)
200 /* not one of ours */
201 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Neale Rannsb80c5362016-10-08 13:03:40 +0100203 t = pool_elt_at_index(gm->tunnels, ti);
Neale Ranns5e575b12016-10-03 09:40:25 +0100204
Neale Rannsb80c5362016-10-08 13:03:40 +0100205 vec_validate(rewrite, sizeof(*h)-1);
206 h = (ip4_and_gre_header_t*)rewrite;
207 h->gre.protocol = clib_host_to_net_u16(gre_proto_from_vnet_link(link_type));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Neale Rannsb80c5362016-10-08 13:03:40 +0100209 h->ip4.ip_version_and_header_length = 0x45;
210 h->ip4.ttl = 254;
211 h->ip4.protocol = IP_PROTOCOL_GRE;
212 /* fixup ip4 header length and checksum after-the-fact */
213 h->ip4.src_address.as_u32 = t->tunnel_src.as_u32;
214 h->ip4.dst_address.as_u32 = t->tunnel_dst.as_u32;
215 h->ip4.checksum = ip4_header_checksum (&h->ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Neale Rannsb80c5362016-10-08 13:03:40 +0100217 return (rewrite);
Neale Ranns5e575b12016-10-03 09:40:25 +0100218}
219
Neale Rannsb80c5362016-10-08 13:03:40 +0100220void
221gre_fixup (vlib_main_t *vm,
222 ip_adjacency_t *adj,
223 vlib_buffer_t *b0)
224{
225 ip4_header_t * ip0;
226
227 ip0 = vlib_buffer_get_current (b0);
228
229 /* Fixup the checksum and len fields in the GRE tunnel encap
230 * that was applied at the midchain node */
231 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
232 ip0->checksum = ip4_header_checksum (ip0);
233}
234
235void
236gre_update_adj (vnet_main_t * vnm,
237 u32 sw_if_index,
238 adj_index_t ai)
239{
240 adj_nbr_midchain_update_rewrite (ai, gre_fixup,
241 ADJ_MIDCHAIN_FLAG_NONE,
242 gre_build_rewrite(vnm, sw_if_index,
243 adj_get_link_type(ai),
244 NULL));
245
246 gre_tunnel_stack(ai);
247}
248
249/**
250 * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
251 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100252static uword
Neale Ranns177bbdc2016-11-15 09:46:51 +0000253gre_interface_tx_inline (vlib_main_t * vm,
254 vlib_node_runtime_t * node,
255 vlib_frame_t * frame)
Neale Ranns5e575b12016-10-03 09:40:25 +0100256{
257 gre_main_t * gm = &gre_main;
258 u32 next_index;
259 u32 * from, * to_next, n_left_from, n_left_to_next;
260 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
261 const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
262
263 /* Vector of buffer / pkt indices we're supposed to process */
264 from = vlib_frame_vector_args (frame);
265
266 /* Number of buffers / pkts */
267 n_left_from = frame->n_vectors;
268
269 /* Speculatively send the first buffer to the last disposition we used */
270 next_index = node->cached_next_index;
271
272 while (n_left_from > 0)
273 {
274 /* set up to enqueue to our disposition with index = next_index */
275 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
276
277 /*
278 * FIXME DUAL LOOP
279 */
280
281 while (n_left_from > 0 && n_left_to_next > 0)
282 {
283 vlib_buffer_t * b0;
284 u32 bi0;
285
286 bi0 = from[0];
287 to_next[0] = bi0;
288 from += 1;
289 to_next += 1;
290 n_left_from -= 1;
291 n_left_to_next -= 1;
292
293 b0 = vlib_get_buffer(vm, bi0);
294
Neale Rannsb80c5362016-10-08 13:03:40 +0100295 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = gt->l2_adj_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100296
297 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
298 {
299 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
300 b0, sizeof (*tr));
301 tr->tunnel_id = gt - gm->tunnels;
302 tr->length = vlib_buffer_length_in_chain (vm, b0);
303 tr->src.as_u32 = gt->tunnel_src.as_u32;
304 tr->dst.as_u32 = gt->tunnel_src.as_u32;
305 }
306
307 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
308 to_next, n_left_to_next,
309 bi0, gt->l2_tx_arc);
310 }
311
312 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
313 }
314
315 vlib_node_increment_counter (vm, gre_input_node.index,
316 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
318 return frame->n_vectors;
319}
320
Neale Ranns177bbdc2016-11-15 09:46:51 +0000321static uword
322gre_interface_tx (vlib_main_t * vm,
323 vlib_node_runtime_t * node,
324 vlib_frame_t * frame)
325{
326 return (gre_interface_tx_inline (vm, node, frame));
327}
328
329static uword
330gre_teb_interface_tx (vlib_main_t * vm,
331 vlib_node_runtime_t * node,
332 vlib_frame_t * frame)
333{
334 return (gre_interface_tx_inline (vm, node, frame));
335}
336
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337static u8 * format_gre_tunnel_name (u8 * s, va_list * args)
338{
339 u32 dev_instance = va_arg (*args, u32);
340 return format (s, "gre%d", dev_instance);
341}
342
Neale Ranns177bbdc2016-11-15 09:46:51 +0000343static u8 * format_gre_tunnel_teb_name (u8 * s, va_list * args)
344{
345 u32 dev_instance = va_arg (*args, u32);
346 return format (s, "teb-gre%d", dev_instance);
347}
348
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349static u8 * format_gre_device (u8 * s, va_list * args)
350{
351 u32 dev_instance = va_arg (*args, u32);
352 CLIB_UNUSED (int verbose) = va_arg (*args, int);
353
354 s = format (s, "GRE tunnel: id %d\n", dev_instance);
355 return s;
356}
357
358VNET_DEVICE_CLASS (gre_device_class) = {
359 .name = "GRE tunnel device",
360 .format_device_name = format_gre_tunnel_name,
361 .format_device = format_gre_device,
362 .format_tx_trace = format_gre_tx_trace,
363 .tx_function = gre_interface_tx,
Chris Luke393490e2016-05-06 17:09:09 -0400364 .admin_up_down_function = gre_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365#ifdef SOON
366 .clear counter = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367#endif
368};
369
Damjan Marion1c80e832016-05-11 23:07:18 +0200370VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_class,
371 gre_interface_tx)
372
Neale Ranns177bbdc2016-11-15 09:46:51 +0000373VNET_DEVICE_CLASS (gre_device_teb_class) = {
374 .name = "GRE TEB tunnel device",
375 .format_device_name = format_gre_tunnel_teb_name,
376 .format_device = format_gre_device,
377 .format_tx_trace = format_gre_tx_trace,
378 .tx_function = gre_teb_interface_tx,
379 .admin_up_down_function = gre_interface_admin_up_down,
380#ifdef SOON
381 .clear counter = 0;
382#endif
383};
384
385VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_teb_class,
386 gre_teb_interface_tx)
387
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
389 .name = "GRE",
390 .format_header = format_gre_header_with_length,
391 .unformat_header = unformat_gre_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100392 .build_rewrite = gre_build_rewrite,
393 .update_adjacency = gre_update_adj,
394 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395};
396
397static void add_protocol (gre_main_t * gm,
398 gre_protocol_t protocol,
399 char * protocol_name)
400{
401 gre_protocol_info_t * pi;
402 u32 i;
403
404 vec_add2 (gm->protocol_infos, pi, 1);
405 i = pi - gm->protocol_infos;
406
407 pi->name = protocol_name;
408 pi->protocol = protocol;
409 pi->next_index = pi->node_index = ~0;
410
411 hash_set (gm->protocol_info_by_protocol, protocol, i);
412 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
413}
414
415static clib_error_t * gre_init (vlib_main_t * vm)
416{
417 gre_main_t * gm = &gre_main;
418 clib_error_t * error;
419 ip_main_t * im = &ip_main;
420 ip_protocol_info_t * pi;
421
422 memset (gm, 0, sizeof (gm[0]));
423 gm->vlib_main = vm;
424 gm->vnet_main = vnet_get_main();
425
426 if ((error = vlib_call_init_function (vm, ip_main_init)))
427 return error;
428
429 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
430 return error;
431
432 /* Set up the ip packet generator */
433 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
434 pi->format_header = format_gre_header;
435 pi->unformat_pg_edit = unformat_pg_gre_header;
436
437 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
438 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
439 gm->tunnel_by_key = hash_create (0, sizeof (uword));
440
441#define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
442 foreach_gre_protocol
443#undef _
444
445 return vlib_call_init_function (vm, gre_input_init);
446}
447
448VLIB_INIT_FUNCTION (gre_init);
449
450gre_main_t * gre_get_main (vlib_main_t * vm)
451{
452 vlib_call_init_function (vm, gre_init);
453 return &gre_main;
454}
455