blob: e30319f5f99e93fd3df0c06c8ebe3984452b6f64 [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
Swarup Nayak9ff647a2017-11-27 10:27:43 +053024typedef struct
25{
26 union
27 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070028 ip4_and_gre_header_t ip4_and_gre;
29 u64 as_u64[3];
30 };
31} ip4_and_gre_union_t;
32
Swarup Nayak9ff647a2017-11-27 10:27:43 +053033typedef struct
34{
35 union
36 {
Ciara Loftus7eac9162016-09-30 15:47:03 +010037 ip6_and_gre_header_t ip6_and_gre;
38 u64 as_u64[3];
39 };
40} ip6_and_gre_union_t;
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
43/* Packet trace structure */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053044typedef struct
45{
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 /* Tunnel-id / index in tunnel vector */
47 u32 tunnel_id;
48
49 /* pkt length */
50 u32 length;
51
Ciara Loftus7eac9162016-09-30 15:47:03 +010052 /* tunnel ip addresses */
53 ip46_address_t src;
54 ip46_address_t dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} gre_tx_trace_t;
56
Swarup Nayak9ff647a2017-11-27 10:27:43 +053057u8 *
58format_gre_tx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053062 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010063
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
John Loa43ccae2018-02-13 17:15:23 -050065 t->tunnel_id, t->length,
Ciara Loftus7eac9162016-09-30 15:47:03 +010066 format_ip46_address, &t->src, IP46_TYPE_ANY,
67 format_ip46_address, &t->dst, IP46_TYPE_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 return s;
69}
70
Swarup Nayak9ff647a2017-11-27 10:27:43 +053071u8 *
72format_gre_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
74 gre_protocol_t p = va_arg (*args, u32);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053075 gre_main_t *gm = &gre_main;
76 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78 if (pi)
79 s = format (s, "%s", pi->name);
80 else
81 s = format (s, "0x%04x", p);
82
83 return s;
84}
85
Swarup Nayak9ff647a2017-11-27 10:27:43 +053086u8 *
87format_gre_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070088{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053089 gre_main_t *gm = &gre_main;
90 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 u32 max_header_bytes = va_arg (*args, u32);
92 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
Christophe Fontained3c008d2017-10-02 18:10:54 +020093 u32 indent, header_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 header_bytes = sizeof (h[0]);
96 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
97 return format (s, "gre header truncated");
98
99 indent = format_get_indent (s);
100
101 s = format (s, "GRE %U", format_gre_protocol, p);
102
John Loa43ccae2018-02-13 17:15:23 -0500103 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530105 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
106 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 if (node->format_buffer)
108 s = format (s, "\n%U%U",
109 format_white_space, indent,
110 node->format_buffer, (void *) (h + 1),
111 max_header_bytes - header_bytes);
112 }
113
114 return s;
115}
116
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530117u8 *
118format_gre_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530120 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 return format (s, "%U", format_gre_header_with_length, h, 0);
122}
123
124/* Returns gre protocol as an int in host byte order. */
125uword
126unformat_gre_protocol_host_byte_order (unformat_input_t * input,
127 va_list * args)
128{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530129 u16 *result = va_arg (*args, u16 *);
130 gre_main_t *gm = &gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 int i;
132
133 /* Named type. */
134 if (unformat_user (input, unformat_vlib_number_by_name,
135 gm->protocol_info_by_name, &i))
136 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530137 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 *result = pi->protocol;
139 return 1;
140 }
141
142 return 0;
143}
144
145uword
146unformat_gre_protocol_net_byte_order (unformat_input_t * input,
147 va_list * args)
148{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530149 u16 *result = va_arg (*args, u16 *);
150 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 return 0;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530152 *result = clib_host_to_net_u16 ((u16) * result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 return 1;
154}
155
156uword
157unformat_gre_header (unformat_input_t * input, va_list * args)
158{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530159 u8 **result = va_arg (*args, u8 **);
160 gre_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u16 p;
162
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530163 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 return 0;
165
166 h->protocol = clib_host_to_net_u16 (p);
167
168 /* Add header to result. */
169 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530170 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 u32 n_bytes = sizeof (h[0]);
172
173 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100174 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 return 1;
178}
179
Neale Rannsb80c5362016-10-08 13:03:40 +0100180static int
181gre_proto_from_vnet_link (vnet_link_t link)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530183 switch (link)
Neale Rannsb80c5362016-10-08 13:03:40 +0100184 {
185 case VNET_LINK_IP4:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530186 return (GRE_PROTOCOL_ip4);
Neale Rannsb80c5362016-10-08 13:03:40 +0100187 case VNET_LINK_IP6:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530188 return (GRE_PROTOCOL_ip6);
Neale Rannsb80c5362016-10-08 13:03:40 +0100189 case VNET_LINK_MPLS:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530190 return (GRE_PROTOCOL_mpls_unicast);
Neale Rannsb80c5362016-10-08 13:03:40 +0100191 case VNET_LINK_ETHERNET:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530192 return (GRE_PROTOCOL_teb);
Neale Rannsb80c5362016-10-08 13:03:40 +0100193 case VNET_LINK_ARP:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530194 return (GRE_PROTOCOL_arp);
Florin Corasce1b4c72017-01-26 14:25:34 -0800195 case VNET_LINK_NSH:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530196 ASSERT (0);
197 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100198 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530199 ASSERT (0);
200 return (GRE_PROTOCOL_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201}
202
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530203static u8 *
Neale Rannsb80c5362016-10-08 13:03:40 +0100204gre_build_rewrite (vnet_main_t * vnm,
205 u32 sw_if_index,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530206 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530208 gre_main_t *gm = &gre_main;
209 ip4_and_gre_header_t *h4;
210 ip6_and_gre_header_t *h6;
John Loa43ccae2018-02-13 17:15:23 -0500211 gre_header_t *gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530212 u8 *rewrite = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100213 gre_tunnel_t *t;
214 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100215 u8 is_ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Neale Rannsb80c5362016-10-08 13:03:40 +0100217 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Neale Rannsb80c5362016-10-08 13:03:40 +0100219 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530220 /* not one of ours */
221 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530223 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns5e575b12016-10-03 09:40:25 +0100224
Ciara Loftus7eac9162016-09-30 15:47:03 +0100225 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Ciara Loftus7eac9162016-09-30 15:47:03 +0100227 if (!is_ipv6)
228 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530229 vec_validate (rewrite, sizeof (*h4) - 1);
230 h4 = (ip4_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500231 gre = &h4->gre;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100232 h4->ip4.ip_version_and_header_length = 0x45;
233 h4->ip4.ttl = 254;
234 h4->ip4.protocol = IP_PROTOCOL_GRE;
235 /* fixup ip4 header length and checksum after-the-fact */
236 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
237 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
238 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
239 }
240 else
241 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530242 vec_validate (rewrite, sizeof (*h6) - 1);
243 h6 = (ip6_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500244 gre = &h6->gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530245 h6->ip6.ip_version_traffic_class_and_flow_label =
246 clib_host_to_net_u32 (6 << 28);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100247 h6->ip6.hop_limit = 255;
248 h6->ip6.protocol = IP_PROTOCOL_GRE;
249 /* fixup ip6 header length and checksum after-the-fact */
250 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
251 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
252 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
253 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
254 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
John Loa43ccae2018-02-13 17:15:23 -0500256 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
257 {
258 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
259 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
260 }
261 else
262 gre->protocol =
263 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
264
Neale Rannsb80c5362016-10-08 13:03:40 +0100265 return (rewrite);
Neale Ranns5e575b12016-10-03 09:40:25 +0100266}
267
Ciara Loftus7eac9162016-09-30 15:47:03 +0100268#define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
269
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800270static void
271gre4_fixup (vlib_main_t * vm,
272 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Neale Rannsb80c5362016-10-08 13:03:40 +0100273{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530274 ip4_header_t *ip0;
Neale Rannsb80c5362016-10-08 13:03:40 +0100275
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530276 ip0 = vlib_buffer_get_current (b0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100277
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530278 /* Fixup the checksum and len fields in the GRE tunnel encap
279 * that was applied at the midchain node */
280 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
281 ip0->checksum = ip4_header_checksum (ip0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100282}
283
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800284static void
285gre6_fixup (vlib_main_t * vm,
286 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100287{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530288 ip6_header_t *ip0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100289
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530290 ip0 = vlib_buffer_get_current (b0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100291
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530292 /* Fixup the payload length field in the GRE tunnel encap that was applied
293 * at the midchain node */
294 ip0->payload_length =
Ole Troanda6e11b2018-05-23 11:21:42 +0200295 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
296 sizeof (*ip0));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100297}
298
299void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530300gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100301{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530302 gre_main_t *gm = &gre_main;
303 gre_tunnel_t *t;
Neale Ranns521a8d72018-12-06 13:46:49 +0000304 adj_flags_t af;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530305 u8 is_ipv6;
Neale Ranns521a8d72018-12-06 13:46:49 +0000306 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100307
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530308 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
309 t = pool_elt_at_index (gm->tunnels, ti);
310 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Neale Ranns521a8d72018-12-06 13:46:49 +0000311 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
312
313 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
314 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100315
John Loa43ccae2018-02-13 17:15:23 -0500316 adj_nbr_midchain_update_rewrite
Neale Ranns521a8d72018-12-06 13:46:49 +0000317 (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
John Loa43ccae2018-02-13 17:15:23 -0500318 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
Neale Rannsb80c5362016-10-08 13:03:40 +0100319
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530320 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100321}
322
John Loa43ccae2018-02-13 17:15:23 -0500323
324typedef enum
325{
John Loa43ccae2018-02-13 17:15:23 -0500326 GRE_ENCAP_NEXT_L2_MIDCHAIN,
327 GRE_ENCAP_N_NEXT,
328} gre_encap_next_t;
329
Neale Rannsb80c5362016-10-08 13:03:40 +0100330/**
John Loa43ccae2018-02-13 17:15:23 -0500331 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
332 * L3 traffic uses the adj-midchains.
Neale Rannsb80c5362016-10-08 13:03:40 +0100333 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100334static uword
John Loa43ccae2018-02-13 17:15:23 -0500335gre_interface_tx (vlib_main_t * vm,
336 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns5e575b12016-10-03 09:40:25 +0100337{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530338 gre_main_t *gm = &gre_main;
John Loa43ccae2018-02-13 17:15:23 -0500339 vnet_main_t *vnm = gm->vnet_main;
Neale Ranns5e575b12016-10-03 09:40:25 +0100340 u32 next_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530341 u32 *from, *to_next, n_left_from, n_left_to_next;
John Loa43ccae2018-02-13 17:15:23 -0500342 u32 sw_if_index0 = 0;
343 u32 sw_if_index1 = 0;
344 adj_index_t adj_index0 = ADJ_INDEX_INVALID;
345 adj_index_t adj_index1 = ADJ_INDEX_INVALID;
346 gre_tunnel_t *gt0 = NULL;
347 gre_tunnel_t *gt1 = NULL;
Neale Ranns5e575b12016-10-03 09:40:25 +0100348
349 /* Vector of buffer / pkt indices we're supposed to process */
350 from = vlib_frame_vector_args (frame);
351
352 /* Number of buffers / pkts */
353 n_left_from = frame->n_vectors;
354
355 /* Speculatively send the first buffer to the last disposition we used */
Neale Ranns756cd942018-04-06 09:18:11 -0700356 next_index = GRE_ENCAP_NEXT_L2_MIDCHAIN;
Neale Ranns5e575b12016-10-03 09:40:25 +0100357
358 while (n_left_from > 0)
359 {
360 /* set up to enqueue to our disposition with index = next_index */
361 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
362
John Loa43ccae2018-02-13 17:15:23 -0500363 while (n_left_from >= 4 && n_left_to_next >= 2)
364 {
365 u32 bi0 = from[0];
366 u32 bi1 = from[1];
367 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
368 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
369
370 to_next[0] = bi0;
371 to_next[1] = bi1;
372 from += 2;
373 to_next += 2;
374 n_left_to_next -= 2;
375 n_left_from -= 2;
376
377 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
378 {
379 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
380 vnet_hw_interface_t *hi0 =
381 vnet_get_sup_hw_interface (vnm, sw_if_index0);
382 gt0 = &gm->tunnels[hi0->dev_instance];
383 adj_index0 = gt0->l2_adj_index;
384 }
385
John Lo25d417f2018-02-15 15:47:53 -0500386 if (sw_if_index1 != vnet_buffer (b1)->sw_if_index[VLIB_TX])
John Loa43ccae2018-02-13 17:15:23 -0500387 {
John Lo25d417f2018-02-15 15:47:53 -0500388 if (sw_if_index0 == vnet_buffer (b1)->sw_if_index[VLIB_TX])
389 {
390 sw_if_index1 = sw_if_index0;
391 gt1 = gt0;
392 adj_index1 = adj_index0;
393 }
394 else
395 {
396 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
397 vnet_hw_interface_t *hi1 =
398 vnet_get_sup_hw_interface (vnm, sw_if_index1);
399 gt1 = &gm->tunnels[hi1->dev_instance];
400 adj_index1 = gt1->l2_adj_index;
401 }
John Loa43ccae2018-02-13 17:15:23 -0500402 }
403
404 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
405 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = adj_index1;
406
407 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
408 {
409 /* Encap GRE seq# and ERSPAN type II header */
410 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
411 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000412 u32 seq_num = clib_atomic_fetch_add (&gt0->gre_sn->seq_num, 1);
John Lo2bf8b812018-02-27 16:35:03 -0500413 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
John Loa43ccae2018-02-13 17:15:23 -0500414 h0->seq_num = clib_host_to_net_u32 (seq_num);
John Lo2bf8b812018-02-27 16:35:03 -0500415 h0->t2_u64 = hdr;
416 h0->t2.cos_en_t_session |=
John Loa43ccae2018-02-13 17:15:23 -0500417 clib_host_to_net_u16 (gt0->session_id);
418 }
419 if (PREDICT_FALSE (gt1->type == GRE_TUNNEL_TYPE_ERSPAN))
420 {
421 /* Encap GRE seq# and ERSPAN type II header */
422 vlib_buffer_advance (b1, -sizeof (erspan_t2_t));
423 erspan_t2_t *h1 = vlib_buffer_get_current (b1);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000424 u32 seq_num = clib_atomic_fetch_add (&gt1->gre_sn->seq_num, 1);
John Lo2bf8b812018-02-27 16:35:03 -0500425 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
John Loa43ccae2018-02-13 17:15:23 -0500426 h1->seq_num = clib_host_to_net_u32 (seq_num);
John Lo2bf8b812018-02-27 16:35:03 -0500427 h1->t2_u64 = hdr;
428 h1->t2.cos_en_t_session |=
John Loa43ccae2018-02-13 17:15:23 -0500429 clib_host_to_net_u16 (gt1->session_id);
430 }
431
432 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
433 {
434 gre_tx_trace_t *tr0 = vlib_add_trace (vm, node,
435 b0, sizeof (*tr0));
436 tr0->tunnel_id = gt0 - gm->tunnels;
437 tr0->src = gt0->tunnel_src;
438 tr0->dst = gt0->tunnel_dst.fp_addr;
439 tr0->length = vlib_buffer_length_in_chain (vm, b0);
440 }
441 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
442 {
443 gre_tx_trace_t *tr1 = vlib_add_trace (vm, node,
444 b1, sizeof (*tr1));
445 tr1->tunnel_id = gt1 - gm->tunnels;
446 tr1->src = gt1->tunnel_src;
447 tr1->dst = gt1->tunnel_dst.fp_addr;
448 tr1->length = vlib_buffer_length_in_chain (vm, b1);
449 }
John Loa43ccae2018-02-13 17:15:23 -0500450 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100451
452 while (n_left_from > 0 && n_left_to_next > 0)
453 {
John Loa43ccae2018-02-13 17:15:23 -0500454 u32 bi0 = from[0];
455 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100456
Neale Ranns5e575b12016-10-03 09:40:25 +0100457 to_next[0] = bi0;
458 from += 1;
459 to_next += 1;
460 n_left_from -= 1;
461 n_left_to_next -= 1;
462
John Loa43ccae2018-02-13 17:15:23 -0500463 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
464 {
465 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
466 vnet_hw_interface_t *hi0 =
467 vnet_get_sup_hw_interface (vnm, sw_if_index0);
468 gt0 = &gm->tunnels[hi0->dev_instance];
469 adj_index0 = gt0->l2_adj_index;
470 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100471
John Loa43ccae2018-02-13 17:15:23 -0500472 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
473
474 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
475 {
476 /* Encap GRE seq# and ERSPAN type II header */
477 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
478 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000479 u32 seq_num = clib_atomic_fetch_add (&gt0->gre_sn->seq_num, 1);
John Lo2bf8b812018-02-27 16:35:03 -0500480 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
John Loa43ccae2018-02-13 17:15:23 -0500481 h0->seq_num = clib_host_to_net_u32 (seq_num);
John Lo2bf8b812018-02-27 16:35:03 -0500482 h0->t2_u64 = hdr;
483 h0->t2.cos_en_t_session |=
John Loa43ccae2018-02-13 17:15:23 -0500484 clib_host_to_net_u16 (gt0->session_id);
485 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100486
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530487 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns5e575b12016-10-03 09:40:25 +0100488 {
489 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
490 b0, sizeof (*tr));
John Loa43ccae2018-02-13 17:15:23 -0500491 tr->tunnel_id = gt0 - gm->tunnels;
492 tr->src = gt0->tunnel_src;
493 tr->dst = gt0->tunnel_dst.fp_addr;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530494 tr->length = vlib_buffer_length_in_chain (vm, b0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100495 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100496 }
497
498 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
499 }
500
John Loa43ccae2018-02-13 17:15:23 -0500501 vlib_node_increment_counter (vm, node->node_index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100502 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
504 return frame->n_vectors;
505}
506
John Loa43ccae2018-02-13 17:15:23 -0500507static char *gre_error_strings[] = {
508#define gre_error(n,s) s,
509#include "error.def"
510#undef gre_error
511};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000512
John Loa43ccae2018-02-13 17:15:23 -0500513/* *INDENT-OFF* */
514VLIB_REGISTER_NODE (gre_encap_node) =
Neale Ranns177bbdc2016-11-15 09:46:51 +0000515{
John Loa43ccae2018-02-13 17:15:23 -0500516 .function = gre_interface_tx,
517 .name = "gre-encap",
518 .vector_size = sizeof (u32),
519 .format_trace = format_gre_tx_trace,
520 .type = VLIB_NODE_TYPE_INTERNAL,
521 .n_errors = GRE_N_ERROR,
522 .error_strings = gre_error_strings,
523 .n_next_nodes = GRE_ENCAP_N_NEXT,
524 .next_nodes = {
John Loa43ccae2018-02-13 17:15:23 -0500525 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
526 },
527};
528
529VLIB_NODE_FUNCTION_MULTIARCH (gre_encap_node, gre_interface_tx)
530/* *INDENT-ON* */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000531
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530532static u8 *
533format_gre_tunnel_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534{
535 u32 dev_instance = va_arg (*args, u32);
John Loa43ccae2018-02-13 17:15:23 -0500536 gre_main_t *gm = &gre_main;
537 gre_tunnel_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538
John Loa43ccae2018-02-13 17:15:23 -0500539 if (dev_instance >= vec_len (gm->tunnels))
540 return format (s, "<improperly-referenced>");
541
542 t = pool_elt_at_index (gm->tunnels, dev_instance);
543 return format (s, "gre%d", t->user_instance);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000544}
545
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530546static u8 *
547format_gre_device (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548{
549 u32 dev_instance = va_arg (*args, u32);
550 CLIB_UNUSED (int verbose) = va_arg (*args, int);
551
552 s = format (s, "GRE tunnel: id %d\n", dev_instance);
553 return s;
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,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563#ifdef SOON
564 .clear counter = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565#endif
566};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000567
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
569 .name = "GRE",
570 .format_header = format_gre_header_with_length,
571 .unformat_header = unformat_gre_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100572 .build_rewrite = gre_build_rewrite,
573 .update_adjacency = gre_update_adj,
574 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530576/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530578static void
579add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530581 gre_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 u32 i;
583
584 vec_add2 (gm->protocol_infos, pi, 1);
585 i = pi - gm->protocol_infos;
586
587 pi->name = protocol_name;
588 pi->protocol = protocol;
589 pi->next_index = pi->node_index = ~0;
590
591 hash_set (gm->protocol_info_by_protocol, protocol, i);
592 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
593}
594
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530595static clib_error_t *
596gre_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530598 gre_main_t *gm = &gre_main;
599 clib_error_t *error;
600 ip_main_t *im = &ip_main;
601 ip_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
Dave Barachb7b92992018-10-17 10:38:51 -0400603 clib_memset (gm, 0, sizeof (gm[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604 gm->vlib_main = vm;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530605 gm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
607 if ((error = vlib_call_init_function (vm, ip_main_init)))
608 return error;
609
610 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
611 return error;
612
Ciara Loftus7eac9162016-09-30 15:47:03 +0100613 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
614 return error;
615
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 /* Set up the ip packet generator */
617 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
618 pi->format_header = format_gre_header;
619 pi->unformat_pg_edit = unformat_pg_gre_header;
620
621 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
622 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
Neale Ranns33ce60d2017-12-14 08:51:32 -0800623 gm->tunnel_by_key4 =
624 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
625 gm->tunnel_by_key6 =
626 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
John Loa43ccae2018-02-13 17:15:23 -0500627 gm->seq_num_by_key =
628 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
630#define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
631 foreach_gre_protocol
632#undef _
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530633 return vlib_call_init_function (vm, gre_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634}
635
636VLIB_INIT_FUNCTION (gre_init);
637
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530638/*
639 * fd.io coding-style-patch-verification: ON
640 *
641 * Local Variables:
642 * eval: (c-set-style "gnu")
643 * End:
644 */