blob: 4f16b5db3878bd409f2289bc275e91a84ad896ea [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
Benoît Ganne8e220542019-03-01 14:19:55 +010022extern gre_main_t gre_main;
23
Filip Tehlar0fce11f2019-03-04 09:21:59 -080024#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -070025gre_main_t gre_main;
26
Swarup Nayak9ff647a2017-11-27 10:27:43 +053027typedef struct
28{
29 union
30 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 ip4_and_gre_header_t ip4_and_gre;
32 u64 as_u64[3];
33 };
34} ip4_and_gre_union_t;
35
Swarup Nayak9ff647a2017-11-27 10:27:43 +053036typedef struct
37{
38 union
39 {
Ciara Loftus7eac9162016-09-30 15:47:03 +010040 ip6_and_gre_header_t ip6_and_gre;
41 u64 as_u64[3];
42 };
43} ip6_and_gre_union_t;
Filip Tehlar0fce11f2019-03-04 09:21:59 -080044#endif /* CLIB_MARCH_VARIANT */
Ciara Loftus7eac9162016-09-30 15:47:03 +010045
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47/* Packet trace structure */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053048typedef struct
49{
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 /* Tunnel-id / index in tunnel vector */
51 u32 tunnel_id;
52
53 /* pkt length */
54 u32 length;
55
Ciara Loftus7eac9162016-09-30 15:47:03 +010056 /* tunnel ip addresses */
57 ip46_address_t src;
58 ip46_address_t dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059} gre_tx_trace_t;
60
Benoît Ganne8e220542019-03-01 14:19:55 +010061extern u8 *format_gre_tx_trace (u8 * s, va_list * args);
62
63#ifndef CLIB_MARCH_VARIANT
64u8 *
Swarup Nayak9ff647a2017-11-27 10:27:43 +053065format_gre_tx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053069 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010070
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
John Loa43ccae2018-02-13 17:15:23 -050072 t->tunnel_id, t->length,
Ciara Loftus7eac9162016-09-30 15:47:03 +010073 format_ip46_address, &t->src, IP46_TYPE_ANY,
74 format_ip46_address, &t->dst, IP46_TYPE_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 return s;
76}
77
Swarup Nayak9ff647a2017-11-27 10:27:43 +053078u8 *
79format_gre_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
81 gre_protocol_t p = va_arg (*args, u32);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053082 gre_main_t *gm = &gre_main;
83 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 if (pi)
86 s = format (s, "%s", pi->name);
87 else
88 s = format (s, "0x%04x", p);
89
90 return s;
91}
92
Swarup Nayak9ff647a2017-11-27 10:27:43 +053093u8 *
94format_gre_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070095{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053096 gre_main_t *gm = &gre_main;
97 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 u32 max_header_bytes = va_arg (*args, u32);
99 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200100 u32 indent, header_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
102 header_bytes = sizeof (h[0]);
103 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
104 return format (s, "gre header truncated");
105
106 indent = format_get_indent (s);
107
108 s = format (s, "GRE %U", format_gre_protocol, p);
109
John Loa43ccae2018-02-13 17:15:23 -0500110 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530112 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
113 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 if (node->format_buffer)
115 s = format (s, "\n%U%U",
116 format_white_space, indent,
117 node->format_buffer, (void *) (h + 1),
118 max_header_bytes - header_bytes);
119 }
120
121 return s;
122}
123
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530124u8 *
125format_gre_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530127 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 return format (s, "%U", format_gre_header_with_length, h, 0);
129}
130
131/* Returns gre protocol as an int in host byte order. */
132uword
133unformat_gre_protocol_host_byte_order (unformat_input_t * input,
134 va_list * args)
135{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530136 u16 *result = va_arg (*args, u16 *);
137 gre_main_t *gm = &gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 int i;
139
140 /* Named type. */
141 if (unformat_user (input, unformat_vlib_number_by_name,
142 gm->protocol_info_by_name, &i))
143 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530144 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 *result = pi->protocol;
146 return 1;
147 }
148
149 return 0;
150}
151
152uword
153unformat_gre_protocol_net_byte_order (unformat_input_t * input,
154 va_list * args)
155{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530156 u16 *result = va_arg (*args, u16 *);
157 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 return 0;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530159 *result = clib_host_to_net_u16 ((u16) * result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 return 1;
161}
162
163uword
164unformat_gre_header (unformat_input_t * input, va_list * args)
165{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530166 u8 **result = va_arg (*args, u8 **);
167 gre_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 u16 p;
169
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530170 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 return 0;
172
173 h->protocol = clib_host_to_net_u16 (p);
174
175 /* Add header to result. */
176 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530177 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 u32 n_bytes = sizeof (h[0]);
179
180 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100181 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100183
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 return 1;
185}
186
Neale Rannsb80c5362016-10-08 13:03:40 +0100187static int
188gre_proto_from_vnet_link (vnet_link_t link)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530190 switch (link)
Neale Rannsb80c5362016-10-08 13:03:40 +0100191 {
192 case VNET_LINK_IP4:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530193 return (GRE_PROTOCOL_ip4);
Neale Rannsb80c5362016-10-08 13:03:40 +0100194 case VNET_LINK_IP6:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530195 return (GRE_PROTOCOL_ip6);
Neale Rannsb80c5362016-10-08 13:03:40 +0100196 case VNET_LINK_MPLS:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530197 return (GRE_PROTOCOL_mpls_unicast);
Neale Rannsb80c5362016-10-08 13:03:40 +0100198 case VNET_LINK_ETHERNET:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530199 return (GRE_PROTOCOL_teb);
Neale Rannsb80c5362016-10-08 13:03:40 +0100200 case VNET_LINK_ARP:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530201 return (GRE_PROTOCOL_arp);
Florin Corasce1b4c72017-01-26 14:25:34 -0800202 case VNET_LINK_NSH:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530203 ASSERT (0);
204 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100205 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530206 ASSERT (0);
207 return (GRE_PROTOCOL_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208}
209
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530210static u8 *
Neale Rannsb80c5362016-10-08 13:03:40 +0100211gre_build_rewrite (vnet_main_t * vnm,
212 u32 sw_if_index,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530213 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530215 gre_main_t *gm = &gre_main;
216 ip4_and_gre_header_t *h4;
217 ip6_and_gre_header_t *h6;
John Loa43ccae2018-02-13 17:15:23 -0500218 gre_header_t *gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530219 u8 *rewrite = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100220 gre_tunnel_t *t;
221 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100222 u8 is_ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
Neale Rannsb80c5362016-10-08 13:03:40 +0100224 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Neale Rannsb80c5362016-10-08 13:03:40 +0100226 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530227 /* not one of ours */
228 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530230 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns5e575b12016-10-03 09:40:25 +0100231
Ciara Loftus7eac9162016-09-30 15:47:03 +0100232 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Ciara Loftus7eac9162016-09-30 15:47:03 +0100234 if (!is_ipv6)
235 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530236 vec_validate (rewrite, sizeof (*h4) - 1);
237 h4 = (ip4_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500238 gre = &h4->gre;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100239 h4->ip4.ip_version_and_header_length = 0x45;
240 h4->ip4.ttl = 254;
241 h4->ip4.protocol = IP_PROTOCOL_GRE;
242 /* fixup ip4 header length and checksum after-the-fact */
243 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
244 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
245 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
246 }
247 else
248 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530249 vec_validate (rewrite, sizeof (*h6) - 1);
250 h6 = (ip6_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500251 gre = &h6->gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530252 h6->ip6.ip_version_traffic_class_and_flow_label =
253 clib_host_to_net_u32 (6 << 28);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100254 h6->ip6.hop_limit = 255;
255 h6->ip6.protocol = IP_PROTOCOL_GRE;
256 /* fixup ip6 header length and checksum after-the-fact */
257 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
258 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
259 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
260 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
261 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
John Loa43ccae2018-02-13 17:15:23 -0500263 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
264 {
265 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
266 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
267 }
268 else
269 gre->protocol =
270 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
271
Neale Rannsb80c5362016-10-08 13:03:40 +0100272 return (rewrite);
Neale Ranns5e575b12016-10-03 09:40:25 +0100273}
274
Ciara Loftus7eac9162016-09-30 15:47:03 +0100275#define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
276
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800277static void
278gre4_fixup (vlib_main_t * vm,
Neale Ranns960eeea2019-12-02 23:28:50 +0000279 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Neale Rannsb80c5362016-10-08 13:03:40 +0100280{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530281 ip4_header_t *ip0;
Neale Rannsb80c5362016-10-08 13:03:40 +0100282
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530283 ip0 = vlib_buffer_get_current (b0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100284
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530285 /* Fixup the checksum and len fields in the GRE tunnel encap
286 * that was applied at the midchain node */
287 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
288 ip0->checksum = ip4_header_checksum (ip0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100289}
290
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800291static void
292gre6_fixup (vlib_main_t * vm,
Neale Ranns960eeea2019-12-02 23:28:50 +0000293 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100294{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530295 ip6_header_t *ip0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100296
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530297 ip0 = vlib_buffer_get_current (b0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100298
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530299 /* Fixup the payload length field in the GRE tunnel encap that was applied
300 * at the midchain node */
301 ip0->payload_length =
Ole Troanda6e11b2018-05-23 11:21:42 +0200302 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
303 sizeof (*ip0));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100304}
305
306void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530307gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100308{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530309 gre_main_t *gm = &gre_main;
310 gre_tunnel_t *t;
Neale Ranns521a8d72018-12-06 13:46:49 +0000311 adj_flags_t af;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530312 u8 is_ipv6;
Neale Ranns521a8d72018-12-06 13:46:49 +0000313 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100314
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530315 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
316 t = pool_elt_at_index (gm->tunnels, ti);
317 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Neale Ranns521a8d72018-12-06 13:46:49 +0000318 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
319
320 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
321 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100322
John Loa43ccae2018-02-13 17:15:23 -0500323 adj_nbr_midchain_update_rewrite
Neale Ranns521a8d72018-12-06 13:46:49 +0000324 (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
John Loa43ccae2018-02-13 17:15:23 -0500325 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
Neale Rannsb80c5362016-10-08 13:03:40 +0100326
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530327 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100328}
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800329#endif /* CLIB_MARCH_VARIANT */
Neale Rannsb80c5362016-10-08 13:03:40 +0100330
John Loa43ccae2018-02-13 17:15:23 -0500331typedef enum
332{
John Loa43ccae2018-02-13 17:15:23 -0500333 GRE_ENCAP_NEXT_L2_MIDCHAIN,
334 GRE_ENCAP_N_NEXT,
335} gre_encap_next_t;
336
Neale Rannsb80c5362016-10-08 13:03:40 +0100337/**
John Loa43ccae2018-02-13 17:15:23 -0500338 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
339 * L3 traffic uses the adj-midchains.
Neale Rannsb80c5362016-10-08 13:03:40 +0100340 */
Benoît Ganne8e220542019-03-01 14:19:55 +0100341VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800342 vlib_frame_t * frame)
Neale Ranns5e575b12016-10-03 09:40:25 +0100343{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530344 gre_main_t *gm = &gre_main;
Benoît Ganne8e220542019-03-01 14:19:55 +0100345 u32 *from, n_left_from;
346 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
347 u32 sw_if_index[2] = { ~0, ~0 };
348 const gre_tunnel_t *gt[2] = { 0 };
349 adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
Neale Ranns5e575b12016-10-03 09:40:25 +0100350
Neale Ranns5e575b12016-10-03 09:40:25 +0100351 from = vlib_frame_vector_args (frame);
Neale Ranns5e575b12016-10-03 09:40:25 +0100352 n_left_from = frame->n_vectors;
Benoît Ganne8e220542019-03-01 14:19:55 +0100353 vlib_get_buffers (vm, from, bufs, n_left_from);
Neale Ranns5e575b12016-10-03 09:40:25 +0100354
Benoît Ganne8e220542019-03-01 14:19:55 +0100355 while (n_left_from >= 2)
Neale Ranns5e575b12016-10-03 09:40:25 +0100356 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100357
Benoît Ganne8e220542019-03-01 14:19:55 +0100358 if (PREDICT_FALSE
359 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
John Loa43ccae2018-02-13 17:15:23 -0500360 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100361 const vnet_hw_interface_t *hi;
362 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
363 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
364 gt[0] = &gm->tunnels[hi->dev_instance];
365 adj_index[0] = gt[0]->l2_adj_index;
366 }
367 if (PREDICT_FALSE
368 (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
369 {
370 const vnet_hw_interface_t *hi;
371 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
372 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
373 gt[1] = &gm->tunnels[hi->dev_instance];
374 adj_index[1] = gt[1]->l2_adj_index;
John Loa43ccae2018-02-13 17:15:23 -0500375 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100376
Benoît Ganne8e220542019-03-01 14:19:55 +0100377 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
378 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
379
380 if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN))
Neale Ranns5e575b12016-10-03 09:40:25 +0100381 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100382 /* Encap GRE seq# and ERSPAN type II header */
383 erspan_t2_t *h0;
384 u32 seq_num;
385 u64 hdr;
386 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
387 h0 = vlib_buffer_get_current (b[0]);
388 seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
389 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
390 h0->seq_num = clib_host_to_net_u32 (seq_num);
391 h0->t2_u64 = hdr;
392 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
393 }
394 if (PREDICT_FALSE (gt[1]->type == GRE_TUNNEL_TYPE_ERSPAN))
395 {
396 /* Encap GRE seq# and ERSPAN type II header */
397 erspan_t2_t *h0;
398 u32 seq_num;
399 u64 hdr;
400 vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
401 h0 = vlib_buffer_get_current (b[1]);
402 seq_num = clib_atomic_fetch_add (&gt[1]->gre_sn->seq_num, 1);
403 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
404 h0->seq_num = clib_host_to_net_u32 (seq_num);
405 h0->t2_u64 = hdr;
406 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
Neale Ranns5e575b12016-10-03 09:40:25 +0100407 }
408
Benoît Ganne8e220542019-03-01 14:19:55 +0100409 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
410 {
411 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
412 b[0], sizeof (*tr));
413 tr->tunnel_id = gt[0] - gm->tunnels;
414 tr->src = gt[0]->tunnel_src;
415 tr->dst = gt[0]->tunnel_dst.fp_addr;
416 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
417 }
418 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
419 {
420 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
421 b[1], sizeof (*tr));
422 tr->tunnel_id = gt[1] - gm->tunnels;
423 tr->src = gt[1]->tunnel_src;
424 tr->dst = gt[1]->tunnel_dst.fp_addr;
425 tr->length = vlib_buffer_length_in_chain (vm, b[1]);
426 }
427
428 b += 2;
429 n_left_from -= 2;
Neale Ranns5e575b12016-10-03 09:40:25 +0100430 }
431
Benoît Ganne8e220542019-03-01 14:19:55 +0100432 while (n_left_from >= 1)
433 {
434
435 if (PREDICT_FALSE
436 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
437 {
438 const vnet_hw_interface_t *hi;
439 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
440 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
441 gt[0] = &gm->tunnels[hi->dev_instance];
442 adj_index[0] = gt[0]->l2_adj_index;
443 }
444
445 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
446
447 if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN))
448 {
449 /* Encap GRE seq# and ERSPAN type II header */
450 erspan_t2_t *h0;
451 u32 seq_num;
452 u64 hdr;
453 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
454 h0 = vlib_buffer_get_current (b[0]);
455 seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
456 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
457 h0->seq_num = clib_host_to_net_u32 (seq_num);
458 h0->t2_u64 = hdr;
459 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
460 }
461
462 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
463 {
464 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
465 b[0], sizeof (*tr));
466 tr->tunnel_id = gt[0] - gm->tunnels;
467 tr->src = gt[0]->tunnel_src;
468 tr->dst = gt[0]->tunnel_dst.fp_addr;
469 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
470 }
471
472 b += 1;
473 n_left_from -= 1;
474 }
475
476 vlib_buffer_enqueue_to_single_next (vm, node, from,
477 GRE_ENCAP_NEXT_L2_MIDCHAIN,
478 frame->n_vectors);
479
John Loa43ccae2018-02-13 17:15:23 -0500480 vlib_node_increment_counter (vm, node->node_index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100481 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482
483 return frame->n_vectors;
484}
485
John Loa43ccae2018-02-13 17:15:23 -0500486static char *gre_error_strings[] = {
487#define gre_error(n,s) s,
488#include "error.def"
489#undef gre_error
490};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000491
John Loa43ccae2018-02-13 17:15:23 -0500492/* *INDENT-OFF* */
493VLIB_REGISTER_NODE (gre_encap_node) =
Neale Ranns177bbdc2016-11-15 09:46:51 +0000494{
John Loa43ccae2018-02-13 17:15:23 -0500495 .name = "gre-encap",
496 .vector_size = sizeof (u32),
497 .format_trace = format_gre_tx_trace,
498 .type = VLIB_NODE_TYPE_INTERNAL,
499 .n_errors = GRE_N_ERROR,
500 .error_strings = gre_error_strings,
501 .n_next_nodes = GRE_ENCAP_N_NEXT,
502 .next_nodes = {
John Loa43ccae2018-02-13 17:15:23 -0500503 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
504 },
505};
John Loa43ccae2018-02-13 17:15:23 -0500506/* *INDENT-ON* */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000507
Benoît Ganne8e220542019-03-01 14:19:55 +0100508#ifndef CLIB_MARCH_VARIANT
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530509static u8 *
510format_gre_tunnel_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511{
512 u32 dev_instance = va_arg (*args, u32);
John Loa43ccae2018-02-13 17:15:23 -0500513 gre_main_t *gm = &gre_main;
514 gre_tunnel_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
John Loa43ccae2018-02-13 17:15:23 -0500516 if (dev_instance >= vec_len (gm->tunnels))
517 return format (s, "<improperly-referenced>");
518
519 t = pool_elt_at_index (gm->tunnels, dev_instance);
520 return format (s, "gre%d", t->user_instance);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000521}
522
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530523static u8 *
524format_gre_device (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525{
526 u32 dev_instance = va_arg (*args, u32);
527 CLIB_UNUSED (int verbose) = va_arg (*args, int);
528
529 s = format (s, "GRE tunnel: id %d\n", dev_instance);
530 return s;
531}
532
Neale Rannsc87b66c2019-02-07 07:26:12 -0800533static int
534gre_tunnel_desc (u32 sw_if_index,
535 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
536{
537 gre_main_t *gm = &gre_main;
538 gre_tunnel_t *t;
539 u32 ti;
540
541 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
542
543 if (~0 == ti)
544 /* not one of ours */
545 return -1;
546
547 t = pool_elt_at_index (gm->tunnels, ti);
548
549 *src = t->tunnel_src;
550 *dst = t->tunnel_dst.fp_addr;
551 *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB;
552
553 return (0);
554}
555
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530556/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557VNET_DEVICE_CLASS (gre_device_class) = {
558 .name = "GRE tunnel device",
559 .format_device_name = format_gre_tunnel_name,
560 .format_device = format_gre_device,
561 .format_tx_trace = format_gre_tx_trace,
Chris Luke393490e2016-05-06 17:09:09 -0400562 .admin_up_down_function = gre_interface_admin_up_down,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800563 .ip_tun_desc = gre_tunnel_desc,
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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
570 .name = "GRE",
571 .format_header = format_gre_header_with_length,
572 .unformat_header = unformat_gre_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100573 .build_rewrite = gre_build_rewrite,
574 .update_adjacency = gre_update_adj,
575 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530577/* *INDENT-ON* */
Benoît Ganne8e220542019-03-01 14:19:55 +0100578#endif /* CLIB_MARCH_VARIANT */
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
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530640/*
641 * fd.io coding-style-patch-verification: ON
642 *
643 * Local Variables:
644 * eval: (c-set-style "gnu")
645 * End:
646 */