blob: 0669c676bf5e733ba15dad7bac07cd60a444e936 [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>
Neale Rannse5b94dd2019-12-31 05:13:14 +000021#include <vnet/tunnel/tunnel_dp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
Benoît Ganne8e220542019-03-01 14:19:55 +010023extern gre_main_t gre_main;
24
Filip Tehlar0fce11f2019-03-04 09:21:59 -080025#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -070026gre_main_t gre_main;
27
Swarup Nayak9ff647a2017-11-27 10:27:43 +053028typedef struct
29{
30 union
31 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070032 ip4_and_gre_header_t ip4_and_gre;
33 u64 as_u64[3];
34 };
35} ip4_and_gre_union_t;
36
Swarup Nayak9ff647a2017-11-27 10:27:43 +053037typedef struct
38{
39 union
40 {
Ciara Loftus7eac9162016-09-30 15:47:03 +010041 ip6_and_gre_header_t ip6_and_gre;
42 u64 as_u64[3];
43 };
44} ip6_and_gre_union_t;
Filip Tehlar0fce11f2019-03-04 09:21:59 -080045#endif /* CLIB_MARCH_VARIANT */
Ciara Loftus7eac9162016-09-30 15:47:03 +010046
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
48/* Packet trace structure */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053049typedef struct
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 /* Tunnel-id / index in tunnel vector */
52 u32 tunnel_id;
53
54 /* pkt length */
55 u32 length;
56
Ciara Loftus7eac9162016-09-30 15:47:03 +010057 /* tunnel ip addresses */
58 ip46_address_t src;
59 ip46_address_t dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060} gre_tx_trace_t;
61
Benoît Ganne8e220542019-03-01 14:19:55 +010062extern u8 *format_gre_tx_trace (u8 * s, va_list * args);
63
64#ifndef CLIB_MARCH_VARIANT
65u8 *
Swarup Nayak9ff647a2017-11-27 10:27:43 +053066format_gre_tx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070067{
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053070 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010071
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
John Loa43ccae2018-02-13 17:15:23 -050073 t->tunnel_id, t->length,
Ciara Loftus7eac9162016-09-30 15:47:03 +010074 format_ip46_address, &t->src, IP46_TYPE_ANY,
75 format_ip46_address, &t->dst, IP46_TYPE_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 return s;
77}
78
Swarup Nayak9ff647a2017-11-27 10:27:43 +053079u8 *
80format_gre_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070081{
82 gre_protocol_t p = va_arg (*args, u32);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053083 gre_main_t *gm = &gre_main;
84 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 if (pi)
87 s = format (s, "%s", pi->name);
88 else
89 s = format (s, "0x%04x", p);
90
91 return s;
92}
93
Swarup Nayak9ff647a2017-11-27 10:27:43 +053094u8 *
95format_gre_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053097 gre_main_t *gm = &gre_main;
98 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 u32 max_header_bytes = va_arg (*args, u32);
100 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200101 u32 indent, header_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103 header_bytes = sizeof (h[0]);
104 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
105 return format (s, "gre header truncated");
106
107 indent = format_get_indent (s);
108
109 s = format (s, "GRE %U", format_gre_protocol, p);
110
John Loa43ccae2018-02-13 17:15:23 -0500111 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530113 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
114 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 if (node->format_buffer)
116 s = format (s, "\n%U%U",
117 format_white_space, indent,
118 node->format_buffer, (void *) (h + 1),
119 max_header_bytes - header_bytes);
120 }
121
122 return s;
123}
124
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530125u8 *
126format_gre_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530128 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 return format (s, "%U", format_gre_header_with_length, h, 0);
130}
131
132/* Returns gre protocol as an int in host byte order. */
133uword
134unformat_gre_protocol_host_byte_order (unformat_input_t * input,
135 va_list * args)
136{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530137 u16 *result = va_arg (*args, u16 *);
138 gre_main_t *gm = &gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 int i;
140
141 /* Named type. */
142 if (unformat_user (input, unformat_vlib_number_by_name,
143 gm->protocol_info_by_name, &i))
144 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530145 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 *result = pi->protocol;
147 return 1;
148 }
149
150 return 0;
151}
152
153uword
154unformat_gre_protocol_net_byte_order (unformat_input_t * input,
155 va_list * args)
156{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530157 u16 *result = va_arg (*args, u16 *);
158 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 return 0;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530160 *result = clib_host_to_net_u16 ((u16) * result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 return 1;
162}
163
164uword
165unformat_gre_header (unformat_input_t * input, va_list * args)
166{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530167 u8 **result = va_arg (*args, u8 **);
168 gre_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 u16 p;
170
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530171 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 return 0;
173
174 h->protocol = clib_host_to_net_u16 (p);
175
176 /* Add header to result. */
177 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530178 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 u32 n_bytes = sizeof (h[0]);
180
181 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100182 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100184
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 return 1;
186}
187
Neale Rannsb80c5362016-10-08 13:03:40 +0100188static int
189gre_proto_from_vnet_link (vnet_link_t link)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530191 switch (link)
Neale Rannsb80c5362016-10-08 13:03:40 +0100192 {
193 case VNET_LINK_IP4:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530194 return (GRE_PROTOCOL_ip4);
Neale Rannsb80c5362016-10-08 13:03:40 +0100195 case VNET_LINK_IP6:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530196 return (GRE_PROTOCOL_ip6);
Neale Rannsb80c5362016-10-08 13:03:40 +0100197 case VNET_LINK_MPLS:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530198 return (GRE_PROTOCOL_mpls_unicast);
Neale Rannsb80c5362016-10-08 13:03:40 +0100199 case VNET_LINK_ETHERNET:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530200 return (GRE_PROTOCOL_teb);
Neale Rannsb80c5362016-10-08 13:03:40 +0100201 case VNET_LINK_ARP:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530202 return (GRE_PROTOCOL_arp);
Florin Corasce1b4c72017-01-26 14:25:34 -0800203 case VNET_LINK_NSH:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530204 ASSERT (0);
205 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100206 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530207 ASSERT (0);
208 return (GRE_PROTOCOL_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209}
210
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530211static u8 *
Neale Rannsb80c5362016-10-08 13:03:40 +0100212gre_build_rewrite (vnet_main_t * vnm,
213 u32 sw_if_index,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530214 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530216 gre_main_t *gm = &gre_main;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000217 const ip46_address_t *dst;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530218 ip4_and_gre_header_t *h4;
219 ip6_and_gre_header_t *h6;
John Loa43ccae2018-02-13 17:15:23 -0500220 gre_header_t *gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530221 u8 *rewrite = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100222 gre_tunnel_t *t;
223 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100224 u8 is_ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Neale Ranns5f8f6172019-04-18 10:23:56 +0000226 dst = dst_address;
Neale Rannsb80c5362016-10-08 13:03:40 +0100227 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Neale Rannsb80c5362016-10-08 13:03:40 +0100229 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530230 /* not one of ours */
231 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530233 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns5e575b12016-10-03 09:40:25 +0100234
Ciara Loftus7eac9162016-09-30 15:47:03 +0100235 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Ciara Loftus7eac9162016-09-30 15:47:03 +0100237 if (!is_ipv6)
238 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530239 vec_validate (rewrite, sizeof (*h4) - 1);
240 h4 = (ip4_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500241 gre = &h4->gre;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100242 h4->ip4.ip_version_and_header_length = 0x45;
243 h4->ip4.ttl = 254;
244 h4->ip4.protocol = IP_PROTOCOL_GRE;
245 /* fixup ip4 header length and checksum after-the-fact */
246 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000247 h4->ip4.dst_address.as_u32 = dst->ip4.as_u32;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100248 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
249 }
250 else
251 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530252 vec_validate (rewrite, sizeof (*h6) - 1);
253 h6 = (ip6_and_gre_header_t *) rewrite;
John Loa43ccae2018-02-13 17:15:23 -0500254 gre = &h6->gre;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530255 h6->ip6.ip_version_traffic_class_and_flow_label =
256 clib_host_to_net_u32 (6 << 28);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100257 h6->ip6.hop_limit = 255;
258 h6->ip6.protocol = IP_PROTOCOL_GRE;
259 /* fixup ip6 header length and checksum after-the-fact */
260 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
261 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
Neale Ranns5f8f6172019-04-18 10:23:56 +0000262 h6->ip6.dst_address.as_u64[0] = dst->ip6.as_u64[0];
263 h6->ip6.dst_address.as_u64[1] = dst->ip6.as_u64[1];
Ciara Loftus7eac9162016-09-30 15:47:03 +0100264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
John Loa43ccae2018-02-13 17:15:23 -0500266 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
267 {
268 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
269 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
270 }
271 else
272 gre->protocol =
273 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
274
Neale Rannsb80c5362016-10-08 13:03:40 +0100275 return (rewrite);
Neale Ranns5e575b12016-10-03 09:40:25 +0100276}
277
Neale Rannse5b94dd2019-12-31 05:13:14 +0000278static void
279gre44_fixup (vlib_main_t * vm,
280 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
281{
282 tunnel_encap_decap_flags_t flags;
283 ip4_and_gre_header_t *ip0;
284
285 ip0 = vlib_buffer_get_current (b0);
286 flags = pointer_to_uword (data);
287
288 /* Fixup the checksum and len fields in the GRE tunnel encap
289 * that was applied at the midchain node */
290 ip0->ip4.length =
291 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
292 tunnel_encap_fixup_4o4 (flags, (ip4_header_t *) (ip0 + 1), &ip0->ip4);
293 ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4);
294}
Ciara Loftus7eac9162016-09-30 15:47:03 +0100295
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800296static void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000297gre64_fixup (vlib_main_t * vm,
298 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
299{
300 tunnel_encap_decap_flags_t flags;
301 ip4_and_gre_header_t *ip0;
302
303 ip0 = vlib_buffer_get_current (b0);
304 flags = pointer_to_uword (data);
305
306 /* Fixup the checksum and len fields in the GRE tunnel encap
307 * that was applied at the midchain node */
308 ip0->ip4.length =
309 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
310 tunnel_encap_fixup_6o4 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip4);
311 ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4);
312}
313
314static void
315grex4_fixup (vlib_main_t * vm,
316 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Neale Rannsb80c5362016-10-08 13:03:40 +0100317{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530318 ip4_header_t *ip0;
Neale Rannsb80c5362016-10-08 13:03:40 +0100319
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530320 ip0 = vlib_buffer_get_current (b0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100321
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530322 /* Fixup the checksum and len fields in the GRE tunnel encap
323 * that was applied at the midchain node */
324 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
325 ip0->checksum = ip4_header_checksum (ip0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100326}
327
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800328static void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000329gre46_fixup (vlib_main_t * vm,
330 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
331{
332 tunnel_encap_decap_flags_t flags;
333 ip6_and_gre_header_t *ip0;
334
335 ip0 = vlib_buffer_get_current (b0);
336 flags = pointer_to_uword (data);
337
338 /* Fixup the payload length field in the GRE tunnel encap that was applied
339 * at the midchain node */
340 ip0->ip6.payload_length =
341 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
Aloys Augustincf86740a2020-07-16 11:01:01 +0200342 sizeof (ip0->ip6));
Neale Rannsa91cb452021-02-04 11:02:52 +0000343 tunnel_encap_fixup_4o6 (flags, b0, (ip4_header_t *) (ip0 + 1), &ip0->ip6);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000344}
345
346static void
347gre66_fixup (vlib_main_t * vm,
348 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
349{
350 tunnel_encap_decap_flags_t flags;
351 ip6_and_gre_header_t *ip0;
352
353 ip0 = vlib_buffer_get_current (b0);
354 flags = pointer_to_uword (data);
355
356 /* Fixup the payload length field in the GRE tunnel encap that was applied
357 * at the midchain node */
358 ip0->ip6.payload_length =
359 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
Aloys Augustincf86740a2020-07-16 11:01:01 +0200360 sizeof (ip0->ip6));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000361 tunnel_encap_fixup_6o6 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip6);
362}
363
364static void
365grex6_fixup (vlib_main_t * vm,
366 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100367{
Aloys Augustincf86740a2020-07-16 11:01:01 +0200368 ip6_and_gre_header_t *ip0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100369
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530370 ip0 = vlib_buffer_get_current (b0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100371
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530372 /* Fixup the payload length field in the GRE tunnel encap that was applied
373 * at the midchain node */
Aloys Augustincf86740a2020-07-16 11:01:01 +0200374 ip0->ip6.payload_length =
Ole Troanda6e11b2018-05-23 11:21:42 +0200375 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
Aloys Augustincf86740a2020-07-16 11:01:01 +0200376 sizeof (ip0->ip6));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100377}
378
Neale Rannse5b94dd2019-12-31 05:13:14 +0000379/**
380 * return the appropriate fixup function given the overlay (link-type) and
381 * underlay (fproto) combination
382 */
383static adj_midchain_fixup_t
384gre_get_fixup (fib_protocol_t fproto, vnet_link_t lt)
385{
386 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP6)
387 return (gre66_fixup);
388 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP4)
389 return (gre46_fixup);
390 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP6)
391 return (gre64_fixup);
392 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP4)
393 return (gre44_fixup);
Neale Ranns533bf082020-10-08 08:10:34 +0000394 if (fproto == FIB_PROTOCOL_IP6)
Neale Rannse5b94dd2019-12-31 05:13:14 +0000395 return (grex6_fixup);
Neale Ranns533bf082020-10-08 08:10:34 +0000396 if (fproto == FIB_PROTOCOL_IP4)
Neale Rannse5b94dd2019-12-31 05:13:14 +0000397 return (grex4_fixup);
398
399 ASSERT (0);
400 return (gre44_fixup);
401}
402
Ciara Loftus7eac9162016-09-30 15:47:03 +0100403void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530404gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100405{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530406 gre_main_t *gm = &gre_main;
407 gre_tunnel_t *t;
Neale Ranns521a8d72018-12-06 13:46:49 +0000408 adj_flags_t af;
Neale Ranns521a8d72018-12-06 13:46:49 +0000409 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100410
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530411 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
412 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns65d789e2021-02-08 15:24:56 +0000413 af = ADJ_FLAG_NONE;
Mohammed Hawari33c45f52020-11-30 13:50:24 +0100414
Neale Ranns65d789e2021-02-08 15:24:56 +0000415 /*
416 * the user has not requested that the load-balancing be based on
417 * a flow hash of the inner packet. so use the stacking to choose
418 * a path.
419 */
420 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
421 af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
Neale Ranns521a8d72018-12-06 13:46:49 +0000422
423 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
424 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100425
John Loa43ccae2018-02-13 17:15:23 -0500426 adj_nbr_midchain_update_rewrite
Neale Rannse5b94dd2019-12-31 05:13:14 +0000427 (ai, gre_get_fixup (t->tunnel_dst.fp_proto,
428 adj_get_link_type (ai)),
429 uword_to_pointer (t->flags, void *), af,
Neale Ranns5f8f6172019-04-18 10:23:56 +0000430 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai),
431 &t->tunnel_dst.fp_addr));
Neale Rannsb80c5362016-10-08 13:03:40 +0100432
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530433 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100434}
Neale Ranns5f8f6172019-04-18 10:23:56 +0000435
Neale Ranns14053c92019-12-29 23:55:18 +0000436adj_walk_rc_t
437mgre_mk_complete_walk (adj_index_t ai, void *data)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000438{
Neale Ranns14053c92019-12-29 23:55:18 +0000439 mgre_walk_ctx_t *ctx = data;
Mohammed Hawari33c45f52020-11-30 13:50:24 +0100440 adj_flags_t af;
441
Neale Ranns65d789e2021-02-08 15:24:56 +0000442 af = ADJ_FLAG_NONE;
443
444 /*
445 * the user has not requested that the load-balancing be based on
446 * a flow hash of the inner packet. so use the stacking to choose
447 * a path.
448 */
449 if (!(ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
450 af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
Neale Ranns14053c92019-12-29 23:55:18 +0000451
452 adj_nbr_midchain_update_rewrite
Neale Rannse5b94dd2019-12-31 05:13:14 +0000453 (ai, gre_get_fixup (ctx->t->tunnel_dst.fp_proto,
454 adj_get_link_type (ai)),
455 uword_to_pointer (ctx->t->flags, void *),
Mohammed Hawari33c45f52020-11-30 13:50:24 +0100456 af,
Neale Ranns14053c92019-12-29 23:55:18 +0000457 gre_build_rewrite (vnet_get_main (),
458 ctx->t->sw_if_index,
459 adj_get_link_type (ai),
Neale Ranns03ce4622020-02-03 10:55:09 +0000460 &teib_entry_get_nh (ctx->ne)->fp_addr));
Neale Ranns14053c92019-12-29 23:55:18 +0000461
Neale Ranns03ce4622020-02-03 10:55:09 +0000462 teib_entry_adj_stack (ctx->ne, ai);
Neale Ranns14053c92019-12-29 23:55:18 +0000463
464 return (ADJ_WALK_RC_CONTINUE);
465}
466
467adj_walk_rc_t
468mgre_mk_incomplete_walk (adj_index_t ai, void *data)
469{
470 gre_tunnel_t *t = data;
Neale Ranns14053c92019-12-29 23:55:18 +0000471
Neale Rannse5b94dd2019-12-31 05:13:14 +0000472 adj_nbr_midchain_update_rewrite (ai, gre_get_fixup (t->tunnel_dst.fp_proto,
473 adj_get_link_type (ai)),
474 NULL, ADJ_FLAG_NONE, NULL);
Neale Ranns14053c92019-12-29 23:55:18 +0000475
476 adj_midchain_delegate_unstack (ai);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000477
478 return (ADJ_WALK_RC_CONTINUE);
479}
480
481void
482mgre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
483{
484 gre_main_t *gm = &gre_main;
485 ip_adjacency_t *adj;
Neale Ranns03ce4622020-02-03 10:55:09 +0000486 teib_entry_t *ne;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000487 gre_tunnel_t *t;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000488 u32 ti;
489
490 adj = adj_get (ai);
491 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
492 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000493
Neale Rannse6b83052020-09-17 12:56:47 +0000494 ne = teib_entry_find_46 (sw_if_index,
495 adj->ia_nh_proto, &adj->sub_type.nbr.next_hop);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000496
497 if (NULL == ne)
498 // no NHRP entry to provide the next-hop
499 return;
500
Neale Ranns14053c92019-12-29 23:55:18 +0000501 mgre_walk_ctx_t ctx = {
502 .t = t,
503 .ne = ne
504 };
505 adj_nbr_walk_nh (sw_if_index,
506 adj->ia_nh_proto,
507 &adj->sub_type.nbr.next_hop, mgre_mk_complete_walk, &ctx);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000508}
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800509#endif /* CLIB_MARCH_VARIANT */
Neale Rannsb80c5362016-10-08 13:03:40 +0100510
John Loa43ccae2018-02-13 17:15:23 -0500511typedef enum
512{
John Loa43ccae2018-02-13 17:15:23 -0500513 GRE_ENCAP_NEXT_L2_MIDCHAIN,
514 GRE_ENCAP_N_NEXT,
515} gre_encap_next_t;
516
Neale Rannsb80c5362016-10-08 13:03:40 +0100517/**
John Loa43ccae2018-02-13 17:15:23 -0500518 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
519 * L3 traffic uses the adj-midchains.
Neale Rannsb80c5362016-10-08 13:03:40 +0100520 */
Neale Ranns07bbaef2020-04-09 07:34:08 -0400521static_always_inline u32
522gre_encap_inline (vlib_main_t * vm,
523 vlib_node_runtime_t * node,
524 vlib_frame_t * frame, gre_tunnel_type_t type)
Neale Ranns5e575b12016-10-03 09:40:25 +0100525{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530526 gre_main_t *gm = &gre_main;
Benoît Ganne8e220542019-03-01 14:19:55 +0100527 u32 *from, n_left_from;
528 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
529 u32 sw_if_index[2] = { ~0, ~0 };
530 const gre_tunnel_t *gt[2] = { 0 };
531 adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
Neale Ranns5e575b12016-10-03 09:40:25 +0100532
Neale Ranns5e575b12016-10-03 09:40:25 +0100533 from = vlib_frame_vector_args (frame);
Neale Ranns5e575b12016-10-03 09:40:25 +0100534 n_left_from = frame->n_vectors;
Benoît Ganne8e220542019-03-01 14:19:55 +0100535 vlib_get_buffers (vm, from, bufs, n_left_from);
Neale Ranns5e575b12016-10-03 09:40:25 +0100536
Benoît Ganne8e220542019-03-01 14:19:55 +0100537 while (n_left_from >= 2)
Neale Ranns5e575b12016-10-03 09:40:25 +0100538 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100539
Benoît Ganne8e220542019-03-01 14:19:55 +0100540 if (PREDICT_FALSE
541 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
John Loa43ccae2018-02-13 17:15:23 -0500542 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100543 const vnet_hw_interface_t *hi;
544 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
545 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
546 gt[0] = &gm->tunnels[hi->dev_instance];
547 adj_index[0] = gt[0]->l2_adj_index;
548 }
549 if (PREDICT_FALSE
550 (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
551 {
552 const vnet_hw_interface_t *hi;
553 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
554 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
555 gt[1] = &gm->tunnels[hi->dev_instance];
556 adj_index[1] = gt[1]->l2_adj_index;
John Loa43ccae2018-02-13 17:15:23 -0500557 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100558
Benoît Ganne8e220542019-03-01 14:19:55 +0100559 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
560 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
561
Neale Ranns07bbaef2020-04-09 07:34:08 -0400562 if (type == GRE_TUNNEL_TYPE_ERSPAN)
Neale Ranns5e575b12016-10-03 09:40:25 +0100563 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100564 /* Encap GRE seq# and ERSPAN type II header */
565 erspan_t2_t *h0;
566 u32 seq_num;
567 u64 hdr;
568 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
569 h0 = vlib_buffer_get_current (b[0]);
570 seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
571 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
572 h0->seq_num = clib_host_to_net_u32 (seq_num);
573 h0->t2_u64 = hdr;
574 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
575 }
Neale Ranns07bbaef2020-04-09 07:34:08 -0400576 if (type == GRE_TUNNEL_TYPE_ERSPAN)
Benoît Ganne8e220542019-03-01 14:19:55 +0100577 {
578 /* Encap GRE seq# and ERSPAN type II header */
579 erspan_t2_t *h0;
580 u32 seq_num;
581 u64 hdr;
582 vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
583 h0 = vlib_buffer_get_current (b[1]);
584 seq_num = clib_atomic_fetch_add (&gt[1]->gre_sn->seq_num, 1);
585 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
586 h0->seq_num = clib_host_to_net_u32 (seq_num);
587 h0->t2_u64 = hdr;
588 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
Neale Ranns5e575b12016-10-03 09:40:25 +0100589 }
590
Benoît Ganne8e220542019-03-01 14:19:55 +0100591 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
592 {
593 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
594 b[0], sizeof (*tr));
595 tr->tunnel_id = gt[0] - gm->tunnels;
596 tr->src = gt[0]->tunnel_src;
597 tr->dst = gt[0]->tunnel_dst.fp_addr;
598 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
599 }
600 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
601 {
602 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
603 b[1], sizeof (*tr));
604 tr->tunnel_id = gt[1] - gm->tunnels;
605 tr->src = gt[1]->tunnel_src;
606 tr->dst = gt[1]->tunnel_dst.fp_addr;
607 tr->length = vlib_buffer_length_in_chain (vm, b[1]);
608 }
609
610 b += 2;
611 n_left_from -= 2;
Neale Ranns5e575b12016-10-03 09:40:25 +0100612 }
613
Benoît Ganne8e220542019-03-01 14:19:55 +0100614 while (n_left_from >= 1)
615 {
616
617 if (PREDICT_FALSE
618 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
619 {
620 const vnet_hw_interface_t *hi;
621 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
622 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
623 gt[0] = &gm->tunnels[hi->dev_instance];
624 adj_index[0] = gt[0]->l2_adj_index;
625 }
626
627 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
628
Neale Ranns07bbaef2020-04-09 07:34:08 -0400629 if (type == GRE_TUNNEL_TYPE_ERSPAN)
Benoît Ganne8e220542019-03-01 14:19:55 +0100630 {
631 /* Encap GRE seq# and ERSPAN type II header */
632 erspan_t2_t *h0;
633 u32 seq_num;
634 u64 hdr;
Neale Ranns07bbaef2020-04-09 07:34:08 -0400635 ASSERT (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN);
Benoît Ganne8e220542019-03-01 14:19:55 +0100636 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
637 h0 = vlib_buffer_get_current (b[0]);
638 seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
639 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
640 h0->seq_num = clib_host_to_net_u32 (seq_num);
641 h0->t2_u64 = hdr;
642 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
643 }
644
645 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
646 {
647 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
648 b[0], sizeof (*tr));
649 tr->tunnel_id = gt[0] - gm->tunnels;
650 tr->src = gt[0]->tunnel_src;
651 tr->dst = gt[0]->tunnel_dst.fp_addr;
652 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
653 }
654
655 b += 1;
656 n_left_from -= 1;
657 }
658
659 vlib_buffer_enqueue_to_single_next (vm, node, from,
660 GRE_ENCAP_NEXT_L2_MIDCHAIN,
661 frame->n_vectors);
662
John Loa43ccae2018-02-13 17:15:23 -0500663 vlib_node_increment_counter (vm, node->node_index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100664 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
666 return frame->n_vectors;
667}
668
John Loa43ccae2018-02-13 17:15:23 -0500669static char *gre_error_strings[] = {
670#define gre_error(n,s) s,
671#include "error.def"
672#undef gre_error
673};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000674
Neale Ranns07bbaef2020-04-09 07:34:08 -0400675VLIB_NODE_FN (gre_teb_encap_node) (vlib_main_t * vm,
676 vlib_node_runtime_t * node,
677 vlib_frame_t * frame)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000678{
Neale Ranns07bbaef2020-04-09 07:34:08 -0400679 return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_TEB));
680}
681
682VLIB_NODE_FN (gre_erspan_encap_node) (vlib_main_t * vm,
683 vlib_node_runtime_t * node,
684 vlib_frame_t * frame)
685{
686 return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_ERSPAN));
687}
688
689/* *INDENT-OFF* */
690VLIB_REGISTER_NODE (gre_teb_encap_node) =
691{
692 .name = "gre-teb-encap",
693 .vector_size = sizeof (u32),
694 .format_trace = format_gre_tx_trace,
695 .type = VLIB_NODE_TYPE_INTERNAL,
696 .n_errors = GRE_N_ERROR,
697 .error_strings = gre_error_strings,
698 .n_next_nodes = GRE_ENCAP_N_NEXT,
699 .next_nodes = {
700 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
701 },
702};
703VLIB_REGISTER_NODE (gre_erspan_encap_node) =
704{
705 .name = "gre-erspan-encap",
John Loa43ccae2018-02-13 17:15:23 -0500706 .vector_size = sizeof (u32),
707 .format_trace = format_gre_tx_trace,
708 .type = VLIB_NODE_TYPE_INTERNAL,
709 .n_errors = GRE_N_ERROR,
710 .error_strings = gre_error_strings,
711 .n_next_nodes = GRE_ENCAP_N_NEXT,
712 .next_nodes = {
John Loa43ccae2018-02-13 17:15:23 -0500713 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
714 },
715};
John Loa43ccae2018-02-13 17:15:23 -0500716/* *INDENT-ON* */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000717
Benoît Ganne8e220542019-03-01 14:19:55 +0100718#ifndef CLIB_MARCH_VARIANT
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530719static u8 *
720format_gre_tunnel_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721{
722 u32 dev_instance = va_arg (*args, u32);
John Loa43ccae2018-02-13 17:15:23 -0500723 gre_main_t *gm = &gre_main;
724 gre_tunnel_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
John Loa43ccae2018-02-13 17:15:23 -0500726 if (dev_instance >= vec_len (gm->tunnels))
727 return format (s, "<improperly-referenced>");
728
729 t = pool_elt_at_index (gm->tunnels, dev_instance);
730 return format (s, "gre%d", t->user_instance);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000731}
732
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530733static u8 *
734format_gre_device (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735{
736 u32 dev_instance = va_arg (*args, u32);
737 CLIB_UNUSED (int verbose) = va_arg (*args, int);
738
739 s = format (s, "GRE tunnel: id %d\n", dev_instance);
740 return s;
741}
742
Neale Rannsc87b66c2019-02-07 07:26:12 -0800743static int
744gre_tunnel_desc (u32 sw_if_index,
745 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
746{
747 gre_main_t *gm = &gre_main;
748 gre_tunnel_t *t;
749 u32 ti;
750
751 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
752
753 if (~0 == ti)
754 /* not one of ours */
755 return -1;
756
757 t = pool_elt_at_index (gm->tunnels, ti);
758
759 *src = t->tunnel_src;
760 *dst = t->tunnel_dst.fp_addr;
761 *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB;
762
763 return (0);
764}
765
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530766/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767VNET_DEVICE_CLASS (gre_device_class) = {
768 .name = "GRE tunnel device",
769 .format_device_name = format_gre_tunnel_name,
770 .format_device = format_gre_device,
771 .format_tx_trace = format_gre_tx_trace,
Chris Luke393490e2016-05-06 17:09:09 -0400772 .admin_up_down_function = gre_interface_admin_up_down,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800773 .ip_tun_desc = gre_tunnel_desc,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774#ifdef SOON
775 .clear counter = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776#endif
777};
Neale Ranns177bbdc2016-11-15 09:46:51 +0000778
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
780 .name = "GRE",
781 .format_header = format_gre_header_with_length,
782 .unformat_header = unformat_gre_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100783 .build_rewrite = gre_build_rewrite,
784 .update_adjacency = gre_update_adj,
785 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786};
Neale Ranns5f8f6172019-04-18 10:23:56 +0000787
788VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = {
789 .name = "mGRE",
790 .format_header = format_gre_header_with_length,
791 .unformat_header = unformat_gre_header,
792 .build_rewrite = gre_build_rewrite,
793 .update_adjacency = mgre_update_adj,
794 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
795};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530796/* *INDENT-ON* */
Benoît Ganne8e220542019-03-01 14:19:55 +0100797#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530799static void
800add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530802 gre_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803 u32 i;
804
805 vec_add2 (gm->protocol_infos, pi, 1);
806 i = pi - gm->protocol_infos;
807
808 pi->name = protocol_name;
809 pi->protocol = protocol;
810 pi->next_index = pi->node_index = ~0;
811
812 hash_set (gm->protocol_info_by_protocol, protocol, i);
813 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
814}
815
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530816static clib_error_t *
817gre_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530819 gre_main_t *gm = &gre_main;
820 clib_error_t *error;
821 ip_main_t *im = &ip_main;
822 ip_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
Dave Barachb7b92992018-10-17 10:38:51 -0400824 clib_memset (gm, 0, sizeof (gm[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825 gm->vlib_main = vm;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530826 gm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
828 if ((error = vlib_call_init_function (vm, ip_main_init)))
829 return error;
830
831 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
832 return error;
833
Ciara Loftus7eac9162016-09-30 15:47:03 +0100834 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
835 return error;
836
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837 /* Set up the ip packet generator */
838 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
839 pi->format_header = format_gre_header;
840 pi->unformat_pg_edit = unformat_pg_gre_header;
841
842 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
843 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
Neale Ranns33ce60d2017-12-14 08:51:32 -0800844 gm->tunnel_by_key4 =
845 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
846 gm->tunnel_by_key6 =
847 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
John Loa43ccae2018-02-13 17:15:23 -0500848 gm->seq_num_by_key =
849 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850
851#define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
852 foreach_gre_protocol
853#undef _
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530854 return vlib_call_init_function (vm, gre_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855}
856
857VLIB_INIT_FUNCTION (gre_init);
858
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530859/*
860 * fd.io coding-style-patch-verification: ON
861 *
862 * Local Variables:
863 * eval: (c-set-style "gnu")
864 * End:
865 */