blob: 891fc9c08d4188d49b60f3630f75fa6a15cf472c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * node.c: gre packet processing
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 <vlib/vlib.h>
19#include <vnet/pg/pg.h>
20#include <vnet/gre/gre.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/mpls/mpls.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <vppinfra/sparse_vec.h>
23
24#define foreach_gre_input_next \
25_(PUNT, "error-punt") \
26_(DROP, "error-drop") \
David Hothama8cd3092016-09-19 09:55:07 -070027_(ETHERNET_INPUT, "ethernet-input") \
Ed Warnickecb9cada2015-12-08 15:45:58 -070028_(IP4_INPUT, "ip4-input") \
Neale Ranns0bfe5d82016-08-25 15:29:12 +010029_(IP6_INPUT, "ip6-input") \
30_(MPLS_INPUT, "mpls-input")
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
Swarup Nayak9ff647a2017-11-27 10:27:43 +053032typedef enum
33{
Ed Warnickecb9cada2015-12-08 15:45:58 -070034#define _(s,n) GRE_INPUT_NEXT_##s,
35 foreach_gre_input_next
36#undef _
Swarup Nayak9ff647a2017-11-27 10:27:43 +053037 GRE_INPUT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070038} gre_input_next_t;
39
Swarup Nayak9ff647a2017-11-27 10:27:43 +053040typedef struct
41{
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 u32 tunnel_id;
43 u32 length;
Ciara Loftus7eac9162016-09-30 15:47:03 +010044 ip46_address_t src;
45 ip46_address_t dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -070046} gre_rx_trace_t;
47
Benoît Ganne8e220542019-03-01 14:19:55 +010048extern u8 *format_gre_rx_trace (u8 * s, va_list * args);
49
50#ifndef CLIB_MARCH_VARIANT
51u8 *
Swarup Nayak9ff647a2017-11-27 10:27:43 +053052format_gre_rx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053{
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053056 gre_rx_trace_t *t = va_arg (*args, gre_rx_trace_t *);
Ciara Loftus7eac9162016-09-30 15:47:03 +010057
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
Swarup Nayak9ff647a2017-11-27 10:27:43 +053059 t->tunnel_id, clib_net_to_host_u16 (t->length),
60 format_ip46_address, &t->src, IP46_TYPE_ANY,
61 format_ip46_address, &t->dst, IP46_TYPE_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 return s;
63}
Benoît Ganne8e220542019-03-01 14:19:55 +010064#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
Swarup Nayak9ff647a2017-11-27 10:27:43 +053066typedef struct
67{
Ciara Loftus7eac9162016-09-30 15:47:03 +010068 /* Sparse vector mapping gre protocol in network byte order
69 to next index. */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053070 u16 *next_by_protocol;
Ciara Loftus7eac9162016-09-30 15:47:03 +010071} gre_input_runtime_t;
72
Benoît Ganne8e220542019-03-01 14:19:55 +010073always_inline void
74gre_trace (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b,
75 u32 tun_sw_if_index, const ip6_header_t * ip6,
76 const ip4_header_t * ip4, int is_ipv6)
77{
78 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
79 b, sizeof (*tr));
80 tr->tunnel_id = tun_sw_if_index;
81 if (is_ipv6)
82 {
83 tr->length = ip6->payload_length;
84 tr->src.ip6.as_u64[0] = ip6->src_address.as_u64[0];
85 tr->src.ip6.as_u64[1] = ip6->src_address.as_u64[1];
86 tr->dst.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
87 tr->dst.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
88 }
89 else
90 {
91 tr->length = ip4->length;
92 tr->src.as_u64[0] = tr->src.as_u64[1] = 0;
93 tr->dst.as_u64[0] = tr->dst.as_u64[1] = 0;
94 tr->src.ip4.as_u32 = ip4->src_address.as_u32;
95 tr->dst.ip4.as_u32 = ip4->dst_address.as_u32;
96 }
97}
98
99always_inline void
100gre_tunnel_get (const gre_main_t * gm, vlib_node_runtime_t * node,
101 vlib_buffer_t * b, u16 * next, const gre_tunnel_key_t * key,
102 gre_tunnel_key_t * cached_key, u32 * tun_sw_if_index,
103 u32 * cached_tun_sw_if_index, int is_ipv6)
104{
105 const uword *p;
106 p = is_ipv6 ? hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6)
107 : hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4);
108 if (PREDICT_FALSE (!p))
109 {
110 *next = GRE_INPUT_NEXT_DROP;
111 b->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
112 *tun_sw_if_index = ~0;
113 }
114 else
115 {
116 const gre_tunnel_t *tun;
117 tun = pool_elt_at_index (gm->tunnels, *p);
118 *cached_tun_sw_if_index = *tun_sw_if_index = tun->sw_if_index;
119 if (is_ipv6)
120 cached_key->gtk_v6 = key->gtk_v6;
121 else
122 cached_key->gtk_v4 = key->gtk_v4;
123 }
124}
125
Ciara Loftus7eac9162016-09-30 15:47:03 +0100126always_inline uword
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127gre_input (vlib_main_t * vm,
Benoît Ganne8e220542019-03-01 14:19:55 +0100128 vlib_node_runtime_t * node, vlib_frame_t * frame,
129 const int is_ipv6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530131 gre_main_t *gm = &gre_main;
Benoît Ganne8e220542019-03-01 14:19:55 +0100132 u32 *from, n_left_from;
133 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
134 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
135 u16 cached_protocol = ~0;
136 u32 cached_next_index = SPARSE_VEC_INVALID_INDEX;
137 u32 cached_tun_sw_if_index = ~0;
138 gre_tunnel_key_t cached_key;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800139
Benoît Ganne8e220542019-03-01 14:19:55 +0100140 from = vlib_frame_vector_args (frame);
141 n_left_from = frame->n_vectors;
142 vlib_get_buffers (vm, from, bufs, n_left_from);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Benoît Ganne8e220542019-03-01 14:19:55 +0100144 if (is_ipv6)
145 clib_memset (&cached_key.gtk_v6, 0xff, sizeof (cached_key.gtk_v6));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100146 else
Benoît Ganne8e220542019-03-01 14:19:55 +0100147 clib_memset (&cached_key.gtk_v4, 0xff, sizeof (cached_key.gtk_v4));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100148
Benoît Ganne8e220542019-03-01 14:19:55 +0100149 while (n_left_from >= 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100151 const ip6_header_t *ip6[2];
152 const ip4_header_t *ip4[2];
153 const gre_header_t *gre[2];
154 u32 nidx[2];
155 next_info_t ni[2];
156 u8 type[2];
157 u16 version[2];
158 u32 len[2];
159 gre_tunnel_key_t key[2];
160 u8 matched[2];
161 u32 tun_sw_if_index[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Benoît Ganne8e220542019-03-01 14:19:55 +0100163 if (PREDICT_TRUE (n_left_from >= 6))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100165 vlib_prefetch_buffer_data (b[2], LOAD);
166 vlib_prefetch_buffer_data (b[3], LOAD);
167 vlib_prefetch_buffer_header (b[4], STORE);
168 vlib_prefetch_buffer_header (b[5], STORE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 }
Ciara Loftus7eac9162016-09-30 15:47:03 +0100170
Benoît Ganne8e220542019-03-01 14:19:55 +0100171 if (is_ipv6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 {
Benoît Ganne8e220542019-03-01 14:19:55 +0100173 /* ip6_local hands us the ip header, not the gre header */
174 ip6[0] = vlib_buffer_get_current (b[0]);
175 ip6[1] = vlib_buffer_get_current (b[1]);
176 gre[0] = (void *) (ip6[0] + 1);
177 gre[1] = (void *) (ip6[1] + 1);
178 vlib_buffer_advance (b[0], sizeof (*ip6[0]) + sizeof (*gre[0]));
179 vlib_buffer_advance (b[1], sizeof (*ip6[0]) + sizeof (*gre[0]));
180 }
181 else
182 {
183 /* ip4_local hands us the ip header, not the gre header */
184 ip4[0] = vlib_buffer_get_current (b[0]);
185 ip4[1] = vlib_buffer_get_current (b[1]);
186 gre[0] = (void *) (ip4[0] + 1);
187 gre[1] = (void *) (ip4[1] + 1);
188 vlib_buffer_advance (b[0], sizeof (*ip4[0]) + sizeof (*gre[0]));
189 vlib_buffer_advance (b[1], sizeof (*ip4[0]) + sizeof (*gre[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 }
191
Benoît Ganne8e220542019-03-01 14:19:55 +0100192 if (PREDICT_TRUE (cached_protocol == gre[0]->protocol))
193 {
194 nidx[0] = cached_next_index;
195 }
196 else
197 {
198 cached_next_index = nidx[0] =
199 sparse_vec_index (gm->next_by_protocol, gre[0]->protocol);
200 cached_protocol = gre[0]->protocol;
201 }
202 if (PREDICT_TRUE (cached_protocol == gre[1]->protocol))
203 {
204 nidx[1] = cached_next_index;
205 }
206 else
207 {
208 cached_next_index = nidx[1] =
209 sparse_vec_index (gm->next_by_protocol, gre[1]->protocol);
210 cached_protocol = gre[1]->protocol;
211 }
212
213 ni[0] = vec_elt (gm->next_by_protocol, nidx[0]);
214 ni[1] = vec_elt (gm->next_by_protocol, nidx[1]);
215 next[0] = ni[0].next_index;
216 next[1] = ni[1].next_index;
217 type[0] = ni[0].tunnel_type;
218 type[1] = ni[1].tunnel_type;
219
220 b[0]->error = nidx[0] == SPARSE_VEC_INVALID_INDEX
221 ? node->errors[GRE_ERROR_UNKNOWN_PROTOCOL]
222 : node->errors[GRE_ERROR_NONE];
223 b[1]->error = nidx[1] == SPARSE_VEC_INVALID_INDEX
224 ? node->errors[GRE_ERROR_UNKNOWN_PROTOCOL]
225 : node->errors[GRE_ERROR_NONE];
226
227 version[0] = clib_net_to_host_u16 (gre[0]->flags_and_version);
228 version[1] = clib_net_to_host_u16 (gre[1]->flags_and_version);
229 version[0] &= GRE_VERSION_MASK;
230 version[1] &= GRE_VERSION_MASK;
231
232 b[0]->error = version[0]
233 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[0]->error;
234 next[0] = version[0] ? GRE_INPUT_NEXT_DROP : next[0];
235 b[1]->error = version[1]
236 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[1]->error;
237 next[1] = version[1] ? GRE_INPUT_NEXT_DROP : next[1];
238
239 len[0] = vlib_buffer_length_in_chain (vm, b[0]);
240 len[1] = vlib_buffer_length_in_chain (vm, b[1]);
241
242 if (is_ipv6)
243 {
244 gre_mk_key6 (&ip6[0]->dst_address,
245 &ip6[0]->src_address,
246 vnet_buffer (b[0])->ip.fib_index,
247 type[0], 0, &key[0].gtk_v6);
248 gre_mk_key6 (&ip6[1]->dst_address,
249 &ip6[1]->src_address,
250 vnet_buffer (b[1])->ip.fib_index,
251 type[1], 0, &key[1].gtk_v6);
252 matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6);
253 matched[1] = gre_match_key6 (&cached_key.gtk_v6, &key[1].gtk_v6);
254 }
255 else
256 {
257 gre_mk_key4 (ip4[0]->dst_address,
258 ip4[0]->src_address,
259 vnet_buffer (b[0])->ip.fib_index,
260 type[0], 0, &key[0].gtk_v4);
261 gre_mk_key4 (ip4[1]->dst_address,
262 ip4[1]->src_address,
263 vnet_buffer (b[1])->ip.fib_index,
264 type[1], 0, &key[1].gtk_v4);
265 matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4);
266 matched[1] = gre_match_key4 (&cached_key.gtk_v4, &key[1].gtk_v4);
267 }
268
269 tun_sw_if_index[0] = cached_tun_sw_if_index;
270 tun_sw_if_index[1] = cached_tun_sw_if_index;
271 if (PREDICT_FALSE (!matched[0]))
272 gre_tunnel_get (gm, node, b[0], &next[0], &key[0], &cached_key,
273 &tun_sw_if_index[0], &cached_tun_sw_if_index,
274 is_ipv6);
275 if (PREDICT_FALSE (!matched[1]))
276 gre_tunnel_get (gm, node, b[1], &next[1], &key[1], &cached_key,
277 &tun_sw_if_index[1], &cached_tun_sw_if_index,
278 is_ipv6);
279
280 if (PREDICT_TRUE (next[0] > GRE_INPUT_NEXT_DROP))
281 {
282 vlib_increment_combined_counter (&gm->vnet_main->
283 interface_main.combined_sw_if_counters
284 [VNET_INTERFACE_COUNTER_RX],
285 vm->thread_index,
286 tun_sw_if_index[0],
287 1 /* packets */ ,
288 len[0] /* bytes */ );
289 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = tun_sw_if_index[0];
290 }
291 if (PREDICT_TRUE (next[1] > GRE_INPUT_NEXT_DROP))
292 {
293 vlib_increment_combined_counter (&gm->vnet_main->
294 interface_main.combined_sw_if_counters
295 [VNET_INTERFACE_COUNTER_RX],
296 vm->thread_index,
297 tun_sw_if_index[1],
298 1 /* packets */ ,
299 len[1] /* bytes */ );
300 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = tun_sw_if_index[1];
301 }
302
303 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
304 gre_trace (vm, node, b[0], tun_sw_if_index[0], ip6[0], ip4[0],
305 is_ipv6);
306 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
307 gre_trace (vm, node, b[1], tun_sw_if_index[1], ip6[1], ip4[1],
308 is_ipv6);
309
310 b += 2;
311 next += 2;
312 n_left_from -= 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 }
Benoît Ganne8e220542019-03-01 14:19:55 +0100314
315 while (n_left_from >= 1)
316 {
317 const ip6_header_t *ip6[1];
318 const ip4_header_t *ip4[1];
319 const gre_header_t *gre[1];
320 u32 nidx[1];
321 next_info_t ni[1];
322 u8 type[1];
323 u16 version[1];
324 u32 len[1];
325 gre_tunnel_key_t key[1];
326 u8 matched[1];
327 u32 tun_sw_if_index[1];
328
329 if (PREDICT_TRUE (n_left_from >= 3))
330 {
331 vlib_prefetch_buffer_data (b[1], LOAD);
332 vlib_prefetch_buffer_header (b[2], STORE);
333 }
334
335 if (is_ipv6)
336 {
337 /* ip6_local hands us the ip header, not the gre header */
338 ip6[0] = vlib_buffer_get_current (b[0]);
339 gre[0] = (void *) (ip6[0] + 1);
340 vlib_buffer_advance (b[0], sizeof (*ip6[0]) + sizeof (*gre[0]));
341 }
342 else
343 {
344 /* ip4_local hands us the ip header, not the gre header */
345 ip4[0] = vlib_buffer_get_current (b[0]);
346 gre[0] = (void *) (ip4[0] + 1);
347 vlib_buffer_advance (b[0], sizeof (*ip4[0]) + sizeof (*gre[0]));
348 }
349
350 if (PREDICT_TRUE (cached_protocol == gre[0]->protocol))
351 {
352 nidx[0] = cached_next_index;
353 }
354 else
355 {
356 cached_next_index = nidx[0] =
357 sparse_vec_index (gm->next_by_protocol, gre[0]->protocol);
358 cached_protocol = gre[0]->protocol;
359 }
360
361 ni[0] = vec_elt (gm->next_by_protocol, nidx[0]);
362 next[0] = ni[0].next_index;
363 type[0] = ni[0].tunnel_type;
364
365 b[0]->error = nidx[0] == SPARSE_VEC_INVALID_INDEX
366 ? node->errors[GRE_ERROR_UNKNOWN_PROTOCOL]
367 : node->errors[GRE_ERROR_NONE];
368
369 version[0] = clib_net_to_host_u16 (gre[0]->flags_and_version);
370 version[0] &= GRE_VERSION_MASK;
371
372 b[0]->error = version[0]
373 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[0]->error;
374 next[0] = version[0] ? GRE_INPUT_NEXT_DROP : next[0];
375
376 len[0] = vlib_buffer_length_in_chain (vm, b[0]);
377
378 if (is_ipv6)
379 {
380 gre_mk_key6 (&ip6[0]->dst_address,
381 &ip6[0]->src_address,
382 vnet_buffer (b[0])->ip.fib_index,
383 type[0], 0, &key[0].gtk_v6);
384 matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6);
385 }
386 else
387 {
388 gre_mk_key4 (ip4[0]->dst_address,
389 ip4[0]->src_address,
390 vnet_buffer (b[0])->ip.fib_index,
391 type[0], 0, &key[0].gtk_v4);
392 matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4);
393 }
394
395 tun_sw_if_index[0] = cached_tun_sw_if_index;
396 if (PREDICT_FALSE (!matched[0]))
397 gre_tunnel_get (gm, node, b[0], &next[0], &key[0], &cached_key,
398 &tun_sw_if_index[0], &cached_tun_sw_if_index,
399 is_ipv6);
400
401 if (PREDICT_TRUE (next[0] > GRE_INPUT_NEXT_DROP))
402 {
403 vlib_increment_combined_counter (&gm->vnet_main->
404 interface_main.combined_sw_if_counters
405 [VNET_INTERFACE_COUNTER_RX],
406 vm->thread_index,
407 tun_sw_if_index[0],
408 1 /* packets */ ,
409 len[0] /* bytes */ );
410 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = tun_sw_if_index[0];
411 }
412
413 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
414 gre_trace (vm, node, b[0], tun_sw_if_index[0], ip6[0], ip4[0],
415 is_ipv6);
416
417 b += 1;
418 next += 1;
419 n_left_from -= 1;
420 }
421
422 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
423
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530424 vlib_node_increment_counter (vm,
Benoît Ganne8e220542019-03-01 14:19:55 +0100425 is_ipv6 ? gre6_input_node.index :
426 gre4_input_node.index, GRE_ERROR_PKTS_DECAP,
427 n_left_from);
428
429 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430}
431
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800432VLIB_NODE_FN (gre4_input_node) (vlib_main_t * vm,
433 vlib_node_runtime_t * node,
434 vlib_frame_t * from_frame)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100435{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530436 return gre_input (vm, node, from_frame, /* is_ip6 */ 0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100437}
438
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800439VLIB_NODE_FN (gre6_input_node) (vlib_main_t * vm,
440 vlib_node_runtime_t * node,
441 vlib_frame_t * from_frame)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100442{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530443 return gre_input (vm, node, from_frame, /* is_ip6 */ 1);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100444}
445
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530446static char *gre_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447#define gre_error(n,s) s,
448#include "error.def"
449#undef gre_error
450};
451
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530452/* *INDENT-OFF* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100453VLIB_REGISTER_NODE (gre4_input_node) = {
Ciara Loftus7eac9162016-09-30 15:47:03 +0100454 .name = "gre4-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 /* Takes a vector of packets. */
456 .vector_size = sizeof (u32),
457
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458 .n_errors = GRE_N_ERROR,
459 .error_strings = gre_error_strings,
460
461 .n_next_nodes = GRE_INPUT_N_NEXT,
462 .next_nodes = {
463#define _(s,n) [GRE_INPUT_NEXT_##s] = n,
464 foreach_gre_input_next
465#undef _
466 },
467
468 .format_buffer = format_gre_header_with_length,
469 .format_trace = format_gre_rx_trace,
470 .unformat_buffer = unformat_gre_header,
471};
472
Ciara Loftus7eac9162016-09-30 15:47:03 +0100473VLIB_REGISTER_NODE (gre6_input_node) = {
Ciara Loftus7eac9162016-09-30 15:47:03 +0100474 .name = "gre6-input",
475 /* Takes a vector of packets. */
476 .vector_size = sizeof (u32),
477
478 .runtime_data_bytes = sizeof (gre_input_runtime_t),
479
480 .n_errors = GRE_N_ERROR,
481 .error_strings = gre_error_strings,
482
483 .n_next_nodes = GRE_INPUT_N_NEXT,
484 .next_nodes = {
485#define _(s,n) [GRE_INPUT_NEXT_##s] = n,
486 foreach_gre_input_next
487#undef _
488 },
489
490 .format_buffer = format_gre_header_with_length,
491 .format_trace = format_gre_rx_trace,
492 .unformat_buffer = unformat_gre_header,
493};
John Loa43ccae2018-02-13 17:15:23 -0500494/* *INDENT-ON* */
495
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800496#ifndef CLIB_MARCH_VARIANT
John Loa43ccae2018-02-13 17:15:23 -0500497void
498gre_register_input_protocol (vlib_main_t * vm,
499 gre_protocol_t protocol, u32 node_index,
500 gre_tunnel_type_t tunnel_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530502 gre_main_t *em = &gre_main;
503 gre_protocol_info_t *pi;
John Loa43ccae2018-02-13 17:15:23 -0500504 next_info_t *n;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100505 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
507 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530508 clib_error_t *error = vlib_call_init_function (vm, gre_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 if (error)
510 clib_error_report (error);
511 }
512
513 pi = gre_get_protocol_info (em, protocol);
514 pi->node_index = node_index;
John Loa43ccae2018-02-13 17:15:23 -0500515 pi->tunnel_type = tunnel_type;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100516 pi->next_index = vlib_node_add_next (vm, gre4_input_node.index, node_index);
517 i = vlib_node_add_next (vm, gre6_input_node.index, node_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530518 ASSERT (i == pi->next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
520 /* Setup gre protocol -> next index sparse vector mapping. */
Damjan Marion63d5bae2017-04-04 01:28:26 +0200521 n = sparse_vec_validate (em->next_by_protocol,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530522 clib_host_to_net_u16 (protocol));
John Loa43ccae2018-02-13 17:15:23 -0500523 n->next_index = pi->next_index;
524 n->tunnel_type = tunnel_type;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525}
526
527static void
528gre_setup_node (vlib_main_t * vm, u32 node_index)
529{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530530 vlib_node_t *n = vlib_get_node (vm, node_index);
531 pg_node_t *pn = pg_get_node (node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
533 n->format_buffer = format_gre_header_with_length;
534 n->unformat_buffer = unformat_gre_header;
535 pn->unformat_edit = unformat_pg_gre_header;
536}
537
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530538static clib_error_t *
539gre_input_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530541 gre_main_t *gm = &gre_main;
David Hothama8cd3092016-09-19 09:55:07 -0700542 vlib_node_t *ethernet_input, *ip4_input, *ip6_input, *mpls_unicast_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
544 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530545 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 error = vlib_call_init_function (vm, gre_init);
547 if (error)
548 clib_error_report (error);
549 }
550
Ciara Loftus7eac9162016-09-30 15:47:03 +0100551 gre_setup_node (vm, gre4_input_node.index);
552 gre_setup_node (vm, gre6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
Damjan Marion63d5bae2017-04-04 01:28:26 +0200554 gm->next_by_protocol = sparse_vec_new
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530555 ( /* elt bytes */ sizeof (gm->next_by_protocol[0]),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 /* bits in index */ BITS (((gre_header_t *) 0)->protocol));
557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 /* These could be moved to the supported protocol input node defn's */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530559 ethernet_input = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
560 ASSERT (ethernet_input);
561 ip4_input = vlib_get_node_by_name (vm, (u8 *) "ip4-input");
562 ASSERT (ip4_input);
563 ip6_input = vlib_get_node_by_name (vm, (u8 *) "ip6-input");
564 ASSERT (ip6_input);
565 mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *) "mpls-input");
566 ASSERT (mpls_unicast_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
John Loa43ccae2018-02-13 17:15:23 -0500568 gre_register_input_protocol (vm, GRE_PROTOCOL_teb,
569 ethernet_input->index, GRE_TUNNEL_TYPE_TEB);
David Hothama8cd3092016-09-19 09:55:07 -0700570
John Loa43ccae2018-02-13 17:15:23 -0500571 gre_register_input_protocol (vm, GRE_PROTOCOL_ip4,
572 ip4_input->index, GRE_TUNNEL_TYPE_L3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
John Loa43ccae2018-02-13 17:15:23 -0500574 gre_register_input_protocol (vm, GRE_PROTOCOL_ip6,
575 ip6_input->index, GRE_TUNNEL_TYPE_L3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
577 gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
John Loa43ccae2018-02-13 17:15:23 -0500578 mpls_unicast_input->index, GRE_TUNNEL_TYPE_L3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580 return 0;
581}
582
583VLIB_INIT_FUNCTION (gre_input_init);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530584
Filip Tehlar0fce11f2019-03-04 09:21:59 -0800585#endif /* CLIB_MARCH_VARIANT */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530586/*
587 * fd.io coding-style-patch-verification: ON
588 *
589 * Local Variables:
590 * eval: (c-set-style "gnu")
591 * End:
592 */