blob: 028eefbee8ba55a74d528d61a000ce1b6db9cab6 [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
Filip Tehlar0fce11f2019-03-04 09:21:59 -080022#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -070023gre_main_t gre_main;
24
Swarup Nayak9ff647a2017-11-27 10:27:43 +053025typedef struct
26{
27 union
28 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 ip4_and_gre_header_t ip4_and_gre;
30 u64 as_u64[3];
31 };
32} ip4_and_gre_union_t;
33
Swarup Nayak9ff647a2017-11-27 10:27:43 +053034typedef struct
35{
36 union
37 {
Ciara Loftus7eac9162016-09-30 15:47:03 +010038 ip6_and_gre_header_t ip6_and_gre;
39 u64 as_u64[3];
40 };
41} ip6_and_gre_union_t;
Filip Tehlar0fce11f2019-03-04 09:21:59 -080042#endif /* CLIB_MARCH_VARIANT */
Ciara Loftus7eac9162016-09-30 15:47:03 +010043
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
45/* Packet trace structure */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053046typedef struct
47{
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 /* Tunnel-id / index in tunnel vector */
49 u32 tunnel_id;
50
51 /* pkt length */
52 u32 length;
53
Ciara Loftus7eac9162016-09-30 15:47:03 +010054 /* tunnel ip addresses */
55 ip46_address_t src;
56 ip46_address_t dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057} gre_tx_trace_t;
58
Filip Tehlar0fce11f2019-03-04 09:21:59 -080059static u8 *
Swarup Nayak9ff647a2017-11-27 10:27:43 +053060format_gre_tx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061{
62 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053064 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010065
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
John Loa43ccae2018-02-13 17:15:23 -050067 t->tunnel_id, t->length,
Ciara Loftus7eac9162016-09-30 15:47:03 +010068 format_ip46_address, &t->src, IP46_TYPE_ANY,
69 format_ip46_address, &t->dst, IP46_TYPE_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 return s;
71}
72
Filip Tehlar0fce11f2019-03-04 09:21:59 -080073#ifndef CLIB_MARCH_VARIANT
Swarup Nayak9ff647a2017-11-27 10:27:43 +053074u8 *
75format_gre_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
77 gre_protocol_t p = va_arg (*args, u32);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053078 gre_main_t *gm = &gre_main;
79 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 if (pi)
82 s = format (s, "%s", pi->name);
83 else
84 s = format (s, "0x%04x", p);
85
86 return s;
87}
88
Swarup Nayak9ff647a2017-11-27 10:27:43 +053089u8 *
90format_gre_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053092 gre_main_t *gm = &gre_main;
93 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 u32 max_header_bytes = va_arg (*args, u32);
95 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
Christophe Fontained3c008d2017-10-02 18:10:54 +020096 u32 indent, header_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98 header_bytes = sizeof (h[0]);
99 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
100 return format (s, "gre header truncated");
101
102 indent = format_get_indent (s);
103
104 s = format (s, "GRE %U", format_gre_protocol, p);
105
John Loa43ccae2018-02-13 17:15:23 -0500106 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530108 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
109 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 if (node->format_buffer)
111 s = format (s, "\n%U%U",
112 format_white_space, indent,
113 node->format_buffer, (void *) (h + 1),
114 max_header_bytes - header_bytes);
115 }
116
117 return s;
118}
119
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530120u8 *
121format_gre_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530123 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 return format (s, "%U", format_gre_header_with_length, h, 0);
125}
126
127/* Returns gre protocol as an int in host byte order. */
128uword
129unformat_gre_protocol_host_byte_order (unformat_input_t * input,
130 va_list * args)
131{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530132 u16 *result = va_arg (*args, u16 *);
133 gre_main_t *gm = &gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 int i;
135
136 /* Named type. */
137 if (unformat_user (input, unformat_vlib_number_by_name,
138 gm->protocol_info_by_name, &i))
139 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530140 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 *result = pi->protocol;
142 return 1;
143 }
144
145 return 0;
146}
147
148uword
149unformat_gre_protocol_net_byte_order (unformat_input_t * input,
150 va_list * args)
151{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530152 u16 *result = va_arg (*args, u16 *);
153 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 return 0;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530155 *result = clib_host_to_net_u16 ((u16) * result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 return 1;
157}
158
159uword
160unformat_gre_header (unformat_input_t * input, va_list * args)
161{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530162 u8 **result = va_arg (*args, u8 **);
163 gre_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 u16 p;
165
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530166 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 return 0;
168
169 h->protocol = clib_host_to_net_u16 (p);
170
171 /* Add header to result. */
172 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530173 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 u32 n_bytes = sizeof (h[0]);
175
176 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100177 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100179
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 return 1;
181}
182
Neale Rannsb80c5362016-10-08 13:03:40 +0100183static int
184gre_proto_from_vnet_link (vnet_link_t link)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530186 switch (link)
Neale Rannsb80c5362016-10-08 13:03:40 +0100187 {
188 case VNET_LINK_IP4:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530189 return (GRE_PROTOCOL_ip4);
Neale Rannsb80c5362016-10-08 13:03:40 +0100190 case VNET_LINK_IP6:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530191 return (GRE_PROTOCOL_ip6);
Neale Rannsb80c5362016-10-08 13:03:40 +0100192 case VNET_LINK_MPLS:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530193 return (GRE_PROTOCOL_mpls_unicast);
Neale Rannsb80c5362016-10-08 13:03:40 +0100194 case VNET_LINK_ETHERNET:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530195 return (GRE_PROTOCOL_teb);
Neale Rannsb80c5362016-10-08 13:03:40 +0100196 case VNET_LINK_ARP:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530197 return (GRE_PROTOCOL_arp);
Florin Corasce1b4c72017-01-26 14:25:34 -0800198 case VNET_LINK_NSH:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530199 ASSERT (0);
200 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100201 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530202 ASSERT (0);
203 return (GRE_PROTOCOL_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204}
205
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530206static u8 *
Neale Rannsb80c5362016-10-08 13:03:40 +0100207gre_build_rewrite (vnet_main_t * vnm,
208 u32 sw_if_index,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530209 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530211 gre_main_t *gm = &gre_main;
212 ip4_and_gre_header_t *h4;
213 ip6_and_gre_header_t *h6;
John Loa43ccae2018-02-13 17:15:23 -0500214 gre_header_t *gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530215 u8 *rewrite = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100216 gre_tunnel_t *t;
217 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100218 u8 is_ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Neale Rannsb80c5362016-10-08 13:03:40 +0100220 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Neale Rannsb80c5362016-10-08 13:03:40 +0100222 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530223 /* not one of ours */
224 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530226 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns5e575b12016-10-03 09:40:25 +0100227
Ciara Loftus7eac9162016-09-30 15:47:03 +0100228 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Ciara Loftus7eac9162016-09-30 15:47:03 +0100230 if (!is_ipv6)
231 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530232 vec_validate (rewrite, sizeof (*h4) - 1);
233 h4 = (ip4_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500234 gre = &h4->gre;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100235 h4->ip4.ip_version_and_header_length = 0x45;
236 h4->ip4.ttl = 254;
237 h4->ip4.protocol = IP_PROTOCOL_GRE;
238 /* fixup ip4 header length and checksum after-the-fact */
239 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
240 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
241 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
242 }
243 else
244 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530245 vec_validate (rewrite, sizeof (*h6) - 1);
246 h6 = (ip6_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500247 gre = &h6->gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530248 h6->ip6.ip_version_traffic_class_and_flow_label =
249 clib_host_to_net_u32 (6 << 28);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100250 h6->ip6.hop_limit = 255;
251 h6->ip6.protocol = IP_PROTOCOL_GRE;
252 /* fixup ip6 header length and checksum after-the-fact */
253 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
254 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
255 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
256 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
John Loa43ccae2018-02-13 17:15:23 -0500259 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
260 {
261 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
262 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
263 }
264 else
265 gre->protocol =
266 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
267
Neale Rannsb80c5362016-10-08 13:03:40 +0100268 return (rewrite);
Neale Ranns5e575b12016-10-03 09:40:25 +0100269}
270
Ciara Loftus7eac9162016-09-30 15:47:03 +0100271#define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
272
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800273static void
274gre4_fixup (vlib_main_t * vm,
275 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Neale Rannsb80c5362016-10-08 13:03:40 +0100276{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530277 ip4_header_t *ip0;
Neale Rannsb80c5362016-10-08 13:03:40 +0100278
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530279 ip0 = vlib_buffer_get_current (b0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100280
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530281 /* Fixup the checksum and len fields in the GRE tunnel encap
282 * that was applied at the midchain node */
283 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
284 ip0->checksum = ip4_header_checksum (ip0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100285}
286
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800287static void
288gre6_fixup (vlib_main_t * vm,
289 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100290{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530291 ip6_header_t *ip0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100292
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530293 ip0 = vlib_buffer_get_current (b0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100294
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530295 /* Fixup the payload length field in the GRE tunnel encap that was applied
296 * at the midchain node */
297 ip0->payload_length =
Ole Troanda6e11b2018-05-23 11:21:42 +0200298 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
299 sizeof (*ip0));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100300}
301
302void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530303gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100304{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530305 gre_main_t *gm = &gre_main;
306 gre_tunnel_t *t;
Neale Ranns521a8d72018-12-06 13:46:49 +0000307 adj_flags_t af;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530308 u8 is_ipv6;
Neale Ranns521a8d72018-12-06 13:46:49 +0000309 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100310
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530311 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
312 t = pool_elt_at_index (gm->tunnels, ti);
313 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Neale Ranns521a8d72018-12-06 13:46:49 +0000314 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
315
316 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
317 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100318
John Loa43ccae2018-02-13 17:15:23 -0500319 adj_nbr_midchain_update_rewrite
Neale Ranns521a8d72018-12-06 13:46:49 +0000320 (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
John Loa43ccae2018-02-13 17:15:23 -0500321 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
Neale Rannsb80c5362016-10-08 13:03:40 +0100322
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530323 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100324}
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800325#endif /* CLIB_MARCH_VARIANT */
Neale Rannsb80c5362016-10-08 13:03:40 +0100326
John Loa43ccae2018-02-13 17:15:23 -0500327
328typedef enum
329{
John Loa43ccae2018-02-13 17:15:23 -0500330 GRE_ENCAP_NEXT_L2_MIDCHAIN,
331 GRE_ENCAP_N_NEXT,
332} gre_encap_next_t;
333
Neale Rannsb80c5362016-10-08 13:03:40 +0100334/**
John Loa43ccae2018-02-13 17:15:23 -0500335 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
336 * L3 traffic uses the adj-midchains.
Neale Rannsb80c5362016-10-08 13:03:40 +0100337 */
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800338VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm,
339 vlib_node_runtime_t * node,
340 vlib_frame_t * frame)
Neale Ranns5e575b12016-10-03 09:40:25 +0100341{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530342 gre_main_t *gm = &gre_main;
John Loa43ccae2018-02-13 17:15:23 -0500343 vnet_main_t *vnm = gm->vnet_main;
Neale Ranns5e575b12016-10-03 09:40:25 +0100344 u32 next_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530345 u32 *from, *to_next, n_left_from, n_left_to_next;
Dave Baracha5160d72019-03-13 15:29:15 -0400346 u32 sw_if_index0 = ~0;
347 u32 sw_if_index1 = ~0;
John Loa43ccae2018-02-13 17:15:23 -0500348 adj_index_t adj_index0 = ADJ_INDEX_INVALID;
349 adj_index_t adj_index1 = ADJ_INDEX_INVALID;
350 gre_tunnel_t *gt0 = NULL;
351 gre_tunnel_t *gt1 = NULL;
Neale Ranns5e575b12016-10-03 09:40:25 +0100352
353 /* Vector of buffer / pkt indices we're supposed to process */
354 from = vlib_frame_vector_args (frame);
355
356 /* Number of buffers / pkts */
357 n_left_from = frame->n_vectors;
358
359 /* Speculatively send the first buffer to the last disposition we used */
Neale Ranns756cd942018-04-06 09:18:11 -0700360 next_index = GRE_ENCAP_NEXT_L2_MIDCHAIN;
Neale Ranns5e575b12016-10-03 09:40:25 +0100361
362 while (n_left_from > 0)
363 {
364 /* set up to enqueue to our disposition with index = next_index */
365 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
366
John Loa43ccae2018-02-13 17:15:23 -0500367 while (n_left_from >= 4 && n_left_to_next >= 2)
368 {
369 u32 bi0 = from[0];
370 u32 bi1 = from[1];
371 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
372 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
373
374 to_next[0] = bi0;
375 to_next[1] = bi1;
376 from += 2;
377 to_next += 2;
378 n_left_to_next -= 2;
379 n_left_from -= 2;
380
381 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
382 {
383 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
384 vnet_hw_interface_t *hi0 =
385 vnet_get_sup_hw_interface (vnm, sw_if_index0);
386 gt0 = &gm->tunnels[hi0->dev_instance];
387 adj_index0 = gt0->l2_adj_index;
388 }
389
John Lo25d417f2018-02-15 15:47:53 -0500390 if (sw_if_index1 != vnet_buffer (b1)->sw_if_index[VLIB_TX])
John Loa43ccae2018-02-13 17:15:23 -0500391 {
John Lo25d417f2018-02-15 15:47:53 -0500392 if (sw_if_index0 == vnet_buffer (b1)->sw_if_index[VLIB_TX])
393 {
394 sw_if_index1 = sw_if_index0;
395 gt1 = gt0;
396 adj_index1 = adj_index0;
397 }
398 else
399 {
400 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
401 vnet_hw_interface_t *hi1 =
402 vnet_get_sup_hw_interface (vnm, sw_if_index1);
403 gt1 = &gm->tunnels[hi1->dev_instance];
404 adj_index1 = gt1->l2_adj_index;
405 }
John Loa43ccae2018-02-13 17:15:23 -0500406 }
407
408 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
409 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = adj_index1;
410
411 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
412 {
413 /* Encap GRE seq# and ERSPAN type II header */
414 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
415 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000416 u32 seq_num = clib_atomic_fetch_add (&gt0->gre_sn->seq_num, 1);
John Lo2bf8b812018-02-27 16:35:03 -0500417 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
John Loa43ccae2018-02-13 17:15:23 -0500418 h0->seq_num = clib_host_to_net_u32 (seq_num);
John Lo2bf8b812018-02-27 16:35:03 -0500419 h0->t2_u64 = hdr;
420 h0->t2.cos_en_t_session |=
John Loa43ccae2018-02-13 17:15:23 -0500421 clib_host_to_net_u16 (gt0->session_id);
422 }
423 if (PREDICT_FALSE (gt1->type == GRE_TUNNEL_TYPE_ERSPAN))
424 {
425 /* Encap GRE seq# and ERSPAN type II header */
426 vlib_buffer_advance (b1, -sizeof (erspan_t2_t));
427 erspan_t2_t *h1 = vlib_buffer_get_current (b1);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000428 u32 seq_num = clib_atomic_fetch_add (&gt1->gre_sn->seq_num, 1);
John Lo2bf8b812018-02-27 16:35:03 -0500429 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
John Loa43ccae2018-02-13 17:15:23 -0500430 h1->seq_num = clib_host_to_net_u32 (seq_num);
John Lo2bf8b812018-02-27 16:35:03 -0500431 h1->t2_u64 = hdr;
432 h1->t2.cos_en_t_session |=
John Loa43ccae2018-02-13 17:15:23 -0500433 clib_host_to_net_u16 (gt1->session_id);
434 }
435
436 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
437 {
438 gre_tx_trace_t *tr0 = vlib_add_trace (vm, node,
439 b0, sizeof (*tr0));
440 tr0->tunnel_id = gt0 - gm->tunnels;
441 tr0->src = gt0->tunnel_src;
442 tr0->dst = gt0->tunnel_dst.fp_addr;
443 tr0->length = vlib_buffer_length_in_chain (vm, b0);
444 }
445 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
446 {
447 gre_tx_trace_t *tr1 = vlib_add_trace (vm, node,
448 b1, sizeof (*tr1));
449 tr1->tunnel_id = gt1 - gm->tunnels;
450 tr1->src = gt1->tunnel_src;
451 tr1->dst = gt1->tunnel_dst.fp_addr;
452 tr1->length = vlib_buffer_length_in_chain (vm, b1);
453 }
John Loa43ccae2018-02-13 17:15:23 -0500454 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100455
456 while (n_left_from > 0 && n_left_to_next > 0)
457 {
John Loa43ccae2018-02-13 17:15:23 -0500458 u32 bi0 = from[0];
459 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100460
Neale Ranns5e575b12016-10-03 09:40:25 +0100461 to_next[0] = bi0;
462 from += 1;
463 to_next += 1;
464 n_left_from -= 1;
465 n_left_to_next -= 1;
466
John Loa43ccae2018-02-13 17:15:23 -0500467 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
468 {
469 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
470 vnet_hw_interface_t *hi0 =
471 vnet_get_sup_hw_interface (vnm, sw_if_index0);
472 gt0 = &gm->tunnels[hi0->dev_instance];
473 adj_index0 = gt0->l2_adj_index;
474 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100475
John Loa43ccae2018-02-13 17:15:23 -0500476 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
477
478 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
479 {
480 /* Encap GRE seq# and ERSPAN type II header */
481 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
482 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000483 u32 seq_num = clib_atomic_fetch_add (&gt0->gre_sn->seq_num, 1);
John Lo2bf8b812018-02-27 16:35:03 -0500484 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
John Loa43ccae2018-02-13 17:15:23 -0500485 h0->seq_num = clib_host_to_net_u32 (seq_num);
John Lo2bf8b812018-02-27 16:35:03 -0500486 h0->t2_u64 = hdr;
487 h0->t2.cos_en_t_session |=
John Loa43ccae2018-02-13 17:15:23 -0500488 clib_host_to_net_u16 (gt0->session_id);
489 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100490
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530491 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns5e575b12016-10-03 09:40:25 +0100492 {
493 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
494 b0, sizeof (*tr));
John Loa43ccae2018-02-13 17:15:23 -0500495 tr->tunnel_id = gt0 - gm->tunnels;
496 tr->src = gt0->tunnel_src;
497 tr->dst = gt0->tunnel_dst.fp_addr;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530498 tr->length = vlib_buffer_length_in_chain (vm, b0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100499 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100500 }
501
502 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
503 }
504
John Loa43ccae2018-02-13 17:15:23 -0500505 vlib_node_increment_counter (vm, node->node_index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100506 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
508 return frame->n_vectors;
509}
510
John Loa43ccae2018-02-13 17:15:23 -0500511static char *gre_error_strings[] = {
512#define gre_error(n,s) s,
513#include "error.def"
514#undef gre_error
515};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000516
John Loa43ccae2018-02-13 17:15:23 -0500517/* *INDENT-OFF* */
518VLIB_REGISTER_NODE (gre_encap_node) =
Neale Ranns177bbdc2016-11-15 09:46:51 +0000519{
John Loa43ccae2018-02-13 17:15:23 -0500520 .name = "gre-encap",
521 .vector_size = sizeof (u32),
522 .format_trace = format_gre_tx_trace,
523 .type = VLIB_NODE_TYPE_INTERNAL,
524 .n_errors = GRE_N_ERROR,
525 .error_strings = gre_error_strings,
526 .n_next_nodes = GRE_ENCAP_N_NEXT,
527 .next_nodes = {
John Loa43ccae2018-02-13 17:15:23 -0500528 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
529 },
530};
John Loa43ccae2018-02-13 17:15:23 -0500531/* *INDENT-ON* */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000532
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530533static u8 *
534format_gre_tunnel_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535{
536 u32 dev_instance = va_arg (*args, u32);
John Loa43ccae2018-02-13 17:15:23 -0500537 gre_main_t *gm = &gre_main;
538 gre_tunnel_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
John Loa43ccae2018-02-13 17:15:23 -0500540 if (dev_instance >= vec_len (gm->tunnels))
541 return format (s, "<improperly-referenced>");
542
543 t = pool_elt_at_index (gm->tunnels, dev_instance);
544 return format (s, "gre%d", t->user_instance);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000545}
546
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530547static u8 *
548format_gre_device (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549{
550 u32 dev_instance = va_arg (*args, u32);
551 CLIB_UNUSED (int verbose) = va_arg (*args, int);
552
553 s = format (s, "GRE tunnel: id %d\n", dev_instance);
554 return s;
555}
556
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530557/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558VNET_DEVICE_CLASS (gre_device_class) = {
559 .name = "GRE tunnel device",
560 .format_device_name = format_gre_tunnel_name,
561 .format_device = format_gre_device,
562 .format_tx_trace = format_gre_tx_trace,
Chris Luke393490e2016-05-06 17:09:09 -0400563 .admin_up_down_function = gre_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564#ifdef SOON
565 .clear counter = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566#endif
567};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000568
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800569#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
571 .name = "GRE",
572 .format_header = format_gre_header_with_length,
573 .unformat_header = unformat_gre_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100574 .build_rewrite = gre_build_rewrite,
575 .update_adjacency = gre_update_adj,
576 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530578/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530580static void
581add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530583 gre_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 u32 i;
585
586 vec_add2 (gm->protocol_infos, pi, 1);
587 i = pi - gm->protocol_infos;
588
589 pi->name = protocol_name;
590 pi->protocol = protocol;
591 pi->next_index = pi->node_index = ~0;
592
593 hash_set (gm->protocol_info_by_protocol, protocol, i);
594 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
595}
596
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530597static clib_error_t *
598gre_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530600 gre_main_t *gm = &gre_main;
601 clib_error_t *error;
602 ip_main_t *im = &ip_main;
603 ip_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
Dave Barachb7b92992018-10-17 10:38:51 -0400605 clib_memset (gm, 0, sizeof (gm[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 gm->vlib_main = vm;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530607 gm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
609 if ((error = vlib_call_init_function (vm, ip_main_init)))
610 return error;
611
612 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
613 return error;
614
Ciara Loftus7eac9162016-09-30 15:47:03 +0100615 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
616 return error;
617
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 /* Set up the ip packet generator */
619 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
620 pi->format_header = format_gre_header;
621 pi->unformat_pg_edit = unformat_pg_gre_header;
622
623 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
624 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
Neale Ranns33ce60d2017-12-14 08:51:32 -0800625 gm->tunnel_by_key4 =
626 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
627 gm->tunnel_by_key6 =
628 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
John Loa43ccae2018-02-13 17:15:23 -0500629 gm->seq_num_by_key =
630 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
632#define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
633 foreach_gre_protocol
634#undef _
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530635 return vlib_call_init_function (vm, gre_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636}
637
638VLIB_INIT_FUNCTION (gre_init);
639
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800640#endif /* CLIB_MARCH_VARIANT */
641
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530642/*
643 * fd.io coding-style-patch-verification: ON
644 *
645 * Local Variables:
646 * eval: (c-set-style "gnu")
647 * End:
648 */