Typos. A bunch of typos I've been collecting.
[vpp.git] / src / vnet / vxlan-gbp / decap.c
1 /*
2  * decap.c: vxlan gbp tunnel decap packet processing
3  *
4  * Copyright (c) 2018 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/vxlan-gbp/vxlan_gbp.h>
21
22 typedef struct
23 {
24   u32 next_index;
25   u32 tunnel_index;
26   u32 error;
27   u32 vni;
28   u16 sclass;
29   u8 flags;
30 } vxlan_gbp_rx_trace_t;
31
32 static u8 *
33 format_vxlan_gbp_rx_trace (u8 * s, va_list * args)
34 {
35   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37   vxlan_gbp_rx_trace_t *t = va_arg (*args, vxlan_gbp_rx_trace_t *);
38
39   if (t->tunnel_index == ~0)
40     return format (s,
41                    "VXLAN_GBP decap error - tunnel for vni %d does not exist",
42                    t->vni);
43   return format (s,
44                  "VXLAN_GBP decap from vxlan_gbp_tunnel%d vni %d sclass %d"
45                  " flags %U next %d error %d",
46                  t->tunnel_index, t->vni, t->sclass,
47                  format_vxlan_gbp_header_gpflags, t->flags,
48                  t->next_index, t->error);
49 }
50
51 always_inline u32
52 buf_fib_index (vlib_buffer_t * b, u32 is_ip4)
53 {
54   u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
55   if (sw_if_index != (u32) ~ 0)
56     return sw_if_index;
57
58   u32 *fib_index_by_sw_if_index = is_ip4 ?
59     ip4_main.fib_index_by_sw_if_index : ip6_main.fib_index_by_sw_if_index;
60   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
61
62   return vec_elt (fib_index_by_sw_if_index, sw_if_index);
63 }
64
65 typedef vxlan4_gbp_tunnel_key_t last_tunnel_cache4;
66
67 always_inline vxlan_gbp_tunnel_t *
68 vxlan4_gbp_find_tunnel (vxlan_gbp_main_t * vxm, last_tunnel_cache4 * cache,
69                         u32 fib_index, ip4_header_t * ip4_0,
70                         vxlan_gbp_header_t * vxlan_gbp0)
71 {
72   /*
73    * Check unicast first since that's where most of the traffic comes from
74    *  Make sure VXLAN_GBP tunnel exist according to packet SIP, DIP and VNI
75    */
76   vxlan4_gbp_tunnel_key_t key4;
77   int rv;
78
79   key4.key[1] = ((u64) fib_index << 32) | vxlan_gbp0->vni_reserved;
80   key4.key[0] = (((u64) ip4_0->dst_address.as_u32 << 32) |
81                  ip4_0->src_address.as_u32);
82
83   if (PREDICT_FALSE (key4.key[0] != cache->key[0] ||
84                      key4.key[1] != cache->key[1]))
85     {
86       rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_gbp_tunnel_by_key,
87                                            &key4);
88       if (PREDICT_FALSE (rv == 0))
89         {
90           *cache = key4;
91           return (pool_elt_at_index (vxm->tunnels, cache->value));
92         }
93     }
94   else
95     {
96       return (pool_elt_at_index (vxm->tunnels, cache->value));
97     }
98
99   /* No unicast match - try multicast */
100   if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
101     return (NULL);
102
103   key4.key[0] = ip4_0->dst_address.as_u32;
104   /* Make sure mcast VXLAN_GBP tunnel exist by packet DIP and VNI */
105   rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_gbp_tunnel_by_key, &key4);
106
107   if (PREDICT_FALSE (rv != 0))
108     return (NULL);
109
110   return (pool_elt_at_index (vxm->tunnels, key4.value));
111 }
112
113 typedef vxlan6_gbp_tunnel_key_t last_tunnel_cache6;
114
115 always_inline vxlan_gbp_tunnel_t *
116 vxlan6_gbp_find_tunnel (vxlan_gbp_main_t * vxm, last_tunnel_cache6 * cache,
117                         u32 fib_index, ip6_header_t * ip6_0,
118                         vxlan_gbp_header_t * vxlan_gbp0)
119 {
120   /* Make sure VXLAN_GBP tunnel exist according to packet SIP and VNI */
121   vxlan6_gbp_tunnel_key_t key6 = {
122     .key = {
123             [0] = ip6_0->src_address.as_u64[0],
124             [1] = ip6_0->src_address.as_u64[1],
125             [2] = (((u64) fib_index) << 32) | vxlan_gbp0->vni_reserved,
126             }
127   };
128   int rv;
129
130   if (PREDICT_FALSE
131       (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0))
132     {
133       rv = clib_bihash_search_inline_24_8 (&vxm->vxlan6_gbp_tunnel_by_key,
134                                            &key6);
135       if (PREDICT_FALSE (rv != 0))
136         return NULL;
137
138       *cache = key6;
139     }
140   vxlan_gbp_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
141
142   /* Validate VXLAN_GBP tunnel SIP against packet DIP */
143   if (PREDICT_FALSE
144       (!ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6)))
145     {
146       /* try multicast */
147       if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
148         return 0;
149
150       /* Make sure mcast VXLAN_GBP tunnel exist by packet DIP and VNI */
151       key6.key[0] = ip6_0->dst_address.as_u64[0];
152       key6.key[1] = ip6_0->dst_address.as_u64[1];
153       rv = clib_bihash_search_inline_24_8 (&vxm->vxlan6_gbp_tunnel_by_key,
154                                            &key6);
155       if (PREDICT_FALSE (rv != 0))
156         return 0;
157
158     }
159
160   return t0;
161 }
162
163 always_inline vxlan_gbp_input_next_t
164 vxlan_gbp_tunnel_get_next (const vxlan_gbp_tunnel_t * t, vlib_buffer_t * b0)
165 {
166   if (VXLAN_GBP_TUNNEL_MODE_L2 == t->mode)
167     return (VXLAN_GBP_INPUT_NEXT_L2_INPUT);
168   else
169     {
170       ethernet_header_t *e0;
171       u16 type0;
172
173       e0 = vlib_buffer_get_current (b0);
174       vlib_buffer_advance (b0, sizeof (*e0));
175       type0 = clib_net_to_host_u16 (e0->type);
176       switch (type0)
177         {
178         case ETHERNET_TYPE_IP4:
179           return (VXLAN_GBP_INPUT_NEXT_IP4_INPUT);
180         case ETHERNET_TYPE_IP6:
181           return (VXLAN_GBP_INPUT_NEXT_IP6_INPUT);
182         }
183     }
184   return (VXLAN_GBP_INPUT_NEXT_DROP);
185 }
186
187 always_inline uword
188 vxlan_gbp_input (vlib_main_t * vm,
189                  vlib_node_runtime_t * node,
190                  vlib_frame_t * from_frame, u8 is_ip4)
191 {
192   vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
193   vnet_main_t *vnm = vxm->vnet_main;
194   vnet_interface_main_t *im = &vnm->interface_main;
195   vlib_combined_counter_main_t *rx_counter =
196     im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
197   vlib_combined_counter_main_t *drop_counter =
198     im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP;
199   last_tunnel_cache4 last4;
200   last_tunnel_cache6 last6;
201   u32 pkts_decapsulated = 0;
202   u32 thread_index = vlib_get_thread_index ();
203
204   if (is_ip4)
205     clib_memset (&last4, 0xff, sizeof last4);
206   else
207     clib_memset (&last6, 0xff, sizeof last6);
208
209   u32 next_index = node->cached_next_index;
210
211   u32 *from = vlib_frame_vector_args (from_frame);
212   u32 n_left_from = from_frame->n_vectors;
213
214   while (n_left_from > 0)
215     {
216       u32 *to_next, n_left_to_next;
217       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
218
219       while (n_left_from >= 4 && n_left_to_next >= 2)
220         {
221           /* Prefetch next iteration. */
222           {
223             vlib_buffer_t *p2, *p3;
224
225             p2 = vlib_get_buffer (vm, from[2]);
226             p3 = vlib_get_buffer (vm, from[3]);
227
228             vlib_prefetch_buffer_header (p2, LOAD);
229             vlib_prefetch_buffer_header (p3, LOAD);
230
231             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
232             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
233           }
234
235           u32 bi0 = to_next[0] = from[0];
236           u32 bi1 = to_next[1] = from[1];
237           from += 2;
238           to_next += 2;
239           n_left_to_next -= 2;
240           n_left_from -= 2;
241
242           vlib_buffer_t *b0, *b1;
243           b0 = vlib_get_buffer (vm, bi0);
244           b1 = vlib_get_buffer (vm, bi1);
245
246           /* udp leaves current_data pointing at the vxlan_gbp header */
247           void *cur0 = vlib_buffer_get_current (b0);
248           void *cur1 = vlib_buffer_get_current (b1);
249           vxlan_gbp_header_t *vxlan_gbp0 = cur0;
250           vxlan_gbp_header_t *vxlan_gbp1 = cur1;
251
252           ip4_header_t *ip4_0, *ip4_1;
253           ip6_header_t *ip6_0, *ip6_1;
254           if (is_ip4)
255             {
256               ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
257               ip4_1 = cur1 - sizeof (udp_header_t) - sizeof (ip4_header_t);
258             }
259           else
260             {
261               ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
262               ip6_1 = cur1 - sizeof (udp_header_t) - sizeof (ip6_header_t);
263             }
264
265           u32 fi0 = buf_fib_index (b0, is_ip4);
266           u32 fi1 = buf_fib_index (b1, is_ip4);
267
268           vxlan_gbp_tunnel_t *t0, *t1;
269           if (is_ip4)
270             {
271               t0 =
272                 vxlan4_gbp_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan_gbp0);
273               t1 =
274                 vxlan4_gbp_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan_gbp1);
275             }
276           else
277             {
278               t0 =
279                 vxlan6_gbp_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan_gbp0);
280               t1 =
281                 vxlan6_gbp_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan_gbp1);
282             }
283
284           u32 len0 = vlib_buffer_length_in_chain (vm, b0);
285           u32 len1 = vlib_buffer_length_in_chain (vm, b1);
286
287           vxlan_gbp_input_next_t next0, next1;
288           u8 error0 = 0, error1 = 0;
289           u8 flags0 = vxlan_gbp_get_flags (vxlan_gbp0);
290           u8 flags1 = vxlan_gbp_get_flags (vxlan_gbp1);
291           /* Required to make the l2 tag push / pop code work on l2 subifs */
292           /* pop vxlan_gbp */
293           vlib_buffer_advance (b0, sizeof *vxlan_gbp0);
294           vlib_buffer_advance (b1, sizeof *vxlan_gbp1);
295
296           /* Validate VXLAN_GBP tunnel encap-fib index against packet */
297           if (PREDICT_FALSE
298               (t0 == NULL
299                || flags0 != (VXLAN_GBP_FLAGS_I | VXLAN_GBP_FLAGS_G)))
300             {
301               if (t0 != NULL
302                   && flags0 != (VXLAN_GBP_FLAGS_I | VXLAN_GBP_FLAGS_G))
303                 {
304                   error0 = VXLAN_GBP_ERROR_BAD_FLAGS;
305                   vlib_increment_combined_counter
306                     (drop_counter, thread_index, t0->sw_if_index, 1, len0);
307                   next0 = VXLAN_GBP_INPUT_NEXT_DROP;
308                 }
309               else
310                 {
311                   error0 = VXLAN_GBP_ERROR_NO_SUCH_TUNNEL;
312                   next0 = VXLAN_GBP_INPUT_NEXT_NO_TUNNEL;
313                 }
314               b0->error = node->errors[error0];
315             }
316           else
317             {
318               next0 = vxlan_gbp_tunnel_get_next (t0, b0);
319
320               /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
321               vnet_buffer (b0)->sw_if_index[VLIB_RX] = t0->sw_if_index;
322               vlib_increment_combined_counter
323                 (rx_counter, thread_index, t0->sw_if_index, 1, len0);
324               pkts_decapsulated++;
325             }
326
327           vnet_buffer2 (b0)->gbp.flags = vxlan_gbp_get_gpflags (vxlan_gbp0);
328           vnet_buffer2 (b0)->gbp.sclass = vxlan_gbp_get_sclass (vxlan_gbp0);
329
330
331           if (PREDICT_FALSE
332               (t1 == 0 || flags1 != (VXLAN_GBP_FLAGS_I | VXLAN_GBP_FLAGS_G)))
333             {
334               if (t1 != 0
335                   && flags1 != (VXLAN_GBP_FLAGS_I | VXLAN_GBP_FLAGS_G))
336                 {
337                   error1 = VXLAN_GBP_ERROR_BAD_FLAGS;
338                   vlib_increment_combined_counter
339                     (drop_counter, thread_index, t1->sw_if_index, 1, len1);
340                   next1 = VXLAN_GBP_INPUT_NEXT_DROP;
341                 }
342               else
343                 {
344                   error1 = VXLAN_GBP_ERROR_NO_SUCH_TUNNEL;
345                   next1 = VXLAN_GBP_INPUT_NEXT_NO_TUNNEL;
346                 }
347               b1->error = node->errors[error1];
348             }
349           else
350             {
351               next1 = vxlan_gbp_tunnel_get_next (t1, b1);
352
353               /* Set packet input sw_if_index to unicast VXLAN_GBP tunnel for learning */
354               vnet_buffer (b1)->sw_if_index[VLIB_RX] = t1->sw_if_index;
355               pkts_decapsulated++;
356
357               vlib_increment_combined_counter
358                 (rx_counter, thread_index, t1->sw_if_index, 1, len1);
359             }
360
361           vnet_buffer2 (b1)->gbp.flags = vxlan_gbp_get_gpflags (vxlan_gbp1);
362           vnet_buffer2 (b1)->gbp.sclass = vxlan_gbp_get_sclass (vxlan_gbp1);
363
364           vnet_update_l2_len (b0);
365           vnet_update_l2_len (b1);
366
367           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
368             {
369               vxlan_gbp_rx_trace_t *tr =
370                 vlib_add_trace (vm, node, b0, sizeof (*tr));
371               tr->next_index = next0;
372               tr->error = error0;
373               tr->tunnel_index = t0 == 0 ? ~0 : t0 - vxm->tunnels;
374               tr->vni = vxlan_gbp_get_vni (vxlan_gbp0);
375               tr->sclass = vxlan_gbp_get_sclass (vxlan_gbp0);
376               tr->flags = vxlan_gbp_get_gpflags (vxlan_gbp0);
377             }
378           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
379             {
380               vxlan_gbp_rx_trace_t *tr =
381                 vlib_add_trace (vm, node, b1, sizeof (*tr));
382               tr->next_index = next1;
383               tr->error = error1;
384               tr->tunnel_index = t1 == 0 ? ~0 : t1 - vxm->tunnels;
385               tr->vni = vxlan_gbp_get_vni (vxlan_gbp1);
386               tr->sclass = vxlan_gbp_get_sclass (vxlan_gbp1);
387               tr->flags = vxlan_gbp_get_gpflags (vxlan_gbp1);
388             }
389
390           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
391                                            to_next, n_left_to_next,
392                                            bi0, bi1, next0, next1);
393         }
394
395       while (n_left_from > 0 && n_left_to_next > 0)
396         {
397           u32 bi0 = to_next[0] = from[0];
398           from += 1;
399           to_next += 1;
400           n_left_from -= 1;
401           n_left_to_next -= 1;
402
403           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
404
405           /* udp leaves current_data pointing at the vxlan_gbp header */
406           void *cur0 = vlib_buffer_get_current (b0);
407           vxlan_gbp_header_t *vxlan_gbp0 = cur0;
408           ip4_header_t *ip4_0;
409           ip6_header_t *ip6_0;
410           if (is_ip4)
411             ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
412           else
413             ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
414
415           u32 fi0 = buf_fib_index (b0, is_ip4);
416
417           vxlan_gbp_tunnel_t *t0;
418           if (is_ip4)
419             t0 = vxlan4_gbp_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan_gbp0);
420           else
421             t0 = vxlan6_gbp_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan_gbp0);
422
423           uword len0 = vlib_buffer_length_in_chain (vm, b0);
424
425           vxlan_gbp_input_next_t next0;
426           u8 error0 = 0;
427           u8 flags0 = vxlan_gbp_get_flags (vxlan_gbp0);
428
429           /* pop (ip, udp, vxlan_gbp) */
430           vlib_buffer_advance (b0, sizeof (*vxlan_gbp0));
431           /* Validate VXLAN_GBP tunnel encap-fib index against packet */
432           if (PREDICT_FALSE
433               (t0 == NULL
434                || flags0 != (VXLAN_GBP_FLAGS_I | VXLAN_GBP_FLAGS_G)))
435             {
436               if (t0 != NULL
437                   && flags0 != (VXLAN_GBP_FLAGS_I | VXLAN_GBP_FLAGS_G))
438                 {
439                   error0 = VXLAN_GBP_ERROR_BAD_FLAGS;
440                   vlib_increment_combined_counter
441                     (drop_counter, thread_index, t0->sw_if_index, 1, len0);
442                   next0 = VXLAN_GBP_INPUT_NEXT_DROP;
443                 }
444               else
445                 {
446                   error0 = VXLAN_GBP_ERROR_NO_SUCH_TUNNEL;
447                   next0 = VXLAN_GBP_INPUT_NEXT_NO_TUNNEL;
448                 }
449               b0->error = node->errors[error0];
450             }
451           else
452             {
453               next0 = vxlan_gbp_tunnel_get_next (t0, b0);
454               /* Set packet input sw_if_index to unicast VXLAN_GBP tunnel for learning */
455               vnet_buffer (b0)->sw_if_index[VLIB_RX] = t0->sw_if_index;
456               pkts_decapsulated++;
457
458               vlib_increment_combined_counter
459                 (rx_counter, thread_index, t0->sw_if_index, 1, len0);
460             }
461           vnet_buffer2 (b0)->gbp.flags = vxlan_gbp_get_gpflags (vxlan_gbp0);
462           vnet_buffer2 (b0)->gbp.sclass = vxlan_gbp_get_sclass (vxlan_gbp0);
463
464           /* Required to make the l2 tag push / pop code work on l2 subifs */
465           vnet_update_l2_len (b0);
466
467           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
468             {
469               vxlan_gbp_rx_trace_t *tr
470                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
471               tr->next_index = next0;
472               tr->error = error0;
473               tr->tunnel_index = t0 == 0 ? ~0 : t0 - vxm->tunnels;
474               tr->vni = vxlan_gbp_get_vni (vxlan_gbp0);
475               tr->sclass = vxlan_gbp_get_sclass (vxlan_gbp0);
476               tr->flags = vxlan_gbp_get_gpflags (vxlan_gbp0);
477             }
478           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
479                                            to_next, n_left_to_next,
480                                            bi0, next0);
481         }
482
483       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
484     }
485   /* Do we still need this now that tunnel tx stats is kept? */
486   u32 node_idx =
487     is_ip4 ? vxlan4_gbp_input_node.index : vxlan6_gbp_input_node.index;
488   vlib_node_increment_counter (vm, node_idx, VXLAN_GBP_ERROR_DECAPSULATED,
489                                pkts_decapsulated);
490
491   return from_frame->n_vectors;
492 }
493
494 VLIB_NODE_FN (vxlan4_gbp_input_node) (vlib_main_t * vm,
495                                       vlib_node_runtime_t * node,
496                                       vlib_frame_t * from_frame)
497 {
498   return vxlan_gbp_input (vm, node, from_frame, /* is_ip4 */ 1);
499 }
500
501 VLIB_NODE_FN (vxlan6_gbp_input_node) (vlib_main_t * vm,
502                                       vlib_node_runtime_t * node,
503                                       vlib_frame_t * from_frame)
504 {
505   return vxlan_gbp_input (vm, node, from_frame, /* is_ip4 */ 0);
506 }
507
508 static char *vxlan_gbp_error_strings[] = {
509 #define vxlan_gbp_error(n,s) s,
510 #include <vnet/vxlan-gbp/vxlan_gbp_error.def>
511 #undef vxlan_gbp_error
512 #undef _
513 };
514
515 /* *INDENT-OFF* */
516 VLIB_REGISTER_NODE (vxlan4_gbp_input_node) =
517 {
518   .name = "vxlan4-gbp-input",
519   .vector_size = sizeof (u32),
520   .n_errors = VXLAN_GBP_N_ERROR,
521   .error_strings = vxlan_gbp_error_strings,
522   .n_next_nodes = VXLAN_GBP_INPUT_N_NEXT,
523   .format_trace = format_vxlan_gbp_rx_trace,
524   .next_nodes = {
525 #define _(s,n) [VXLAN_GBP_INPUT_NEXT_##s] = n,
526     foreach_vxlan_gbp_input_next
527 #undef _
528   },
529 };
530
531 VLIB_REGISTER_NODE (vxlan6_gbp_input_node) =
532 {
533   .name = "vxlan6-gbp-input",
534   .vector_size = sizeof (u32),
535   .n_errors = VXLAN_GBP_N_ERROR,
536   .error_strings = vxlan_gbp_error_strings,
537   .n_next_nodes = VXLAN_GBP_INPUT_N_NEXT,
538   .next_nodes = {
539 #define _(s,n) [VXLAN_GBP_INPUT_NEXT_##s] = n,
540     foreach_vxlan_gbp_input_next
541 #undef _
542   },
543   .format_trace = format_vxlan_gbp_rx_trace,
544 };
545 /* *INDENT-ON* */
546
547 typedef enum
548 {
549   IP_VXLAN_GBP_BYPASS_NEXT_DROP,
550   IP_VXLAN_GBP_BYPASS_NEXT_VXLAN_GBP,
551   IP_VXLAN_GBP_BYPASS_N_NEXT,
552 } ip_vxlan_gbp_bypass_next_t;
553
554 always_inline uword
555 ip_vxlan_gbp_bypass_inline (vlib_main_t * vm,
556                             vlib_node_runtime_t * node,
557                             vlib_frame_t * frame, u32 is_ip4)
558 {
559   vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
560   u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
561   vlib_node_runtime_t *error_node =
562     vlib_node_get_runtime (vm, ip4_input_node.index);
563   ip4_address_t addr4;          /* last IPv4 address matching a local VTEP address */
564   ip6_address_t addr6;          /* last IPv6 address matching a local VTEP address */
565
566   from = vlib_frame_vector_args (frame);
567   n_left_from = frame->n_vectors;
568   next_index = node->cached_next_index;
569
570   if (node->flags & VLIB_NODE_FLAG_TRACE)
571     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
572
573   if (is_ip4)
574     addr4.data_u32 = ~0;
575   else
576     ip6_address_set_zero (&addr6);
577
578   while (n_left_from > 0)
579     {
580       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
581
582       while (n_left_from >= 4 && n_left_to_next >= 2)
583         {
584           vlib_buffer_t *b0, *b1;
585           ip4_header_t *ip40, *ip41;
586           ip6_header_t *ip60, *ip61;
587           udp_header_t *udp0, *udp1;
588           u32 bi0, ip_len0, udp_len0, flags0, next0;
589           u32 bi1, ip_len1, udp_len1, flags1, next1;
590           i32 len_diff0, len_diff1;
591           u8 error0, good_udp0, proto0;
592           u8 error1, good_udp1, proto1;
593
594           /* Prefetch next iteration. */
595           {
596             vlib_buffer_t *p2, *p3;
597
598             p2 = vlib_get_buffer (vm, from[2]);
599             p3 = vlib_get_buffer (vm, from[3]);
600
601             vlib_prefetch_buffer_header (p2, LOAD);
602             vlib_prefetch_buffer_header (p3, LOAD);
603
604             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
605             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
606           }
607
608           bi0 = to_next[0] = from[0];
609           bi1 = to_next[1] = from[1];
610           from += 2;
611           n_left_from -= 2;
612           to_next += 2;
613           n_left_to_next -= 2;
614
615           b0 = vlib_get_buffer (vm, bi0);
616           b1 = vlib_get_buffer (vm, bi1);
617           if (is_ip4)
618             {
619               ip40 = vlib_buffer_get_current (b0);
620               ip41 = vlib_buffer_get_current (b1);
621             }
622           else
623             {
624               ip60 = vlib_buffer_get_current (b0);
625               ip61 = vlib_buffer_get_current (b1);
626             }
627
628           /* Setup packet for next IP feature */
629           vnet_feature_next (&next0, b0);
630           vnet_feature_next (&next1, b1);
631
632           if (is_ip4)
633             {
634               /* Treat IP frag packets as "experimental" protocol for now
635                  until support of IP frag reassembly is implemented */
636               proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
637               proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol;
638             }
639           else
640             {
641               proto0 = ip60->protocol;
642               proto1 = ip61->protocol;
643             }
644
645           /* Process packet 0 */
646           if (proto0 != IP_PROTOCOL_UDP)
647             goto exit0;         /* not UDP packet */
648
649           if (is_ip4)
650             udp0 = ip4_next_header (ip40);
651           else
652             udp0 = ip6_next_header (ip60);
653
654           if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gbp))
655             goto exit0;         /* not VXLAN_GBP packet */
656
657           /* Validate DIP against VTEPs */
658           if (is_ip4)
659             {
660               if (addr4.as_u32 != ip40->dst_address.as_u32)
661                 {
662                   if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
663                     goto exit0; /* no local VTEP for VXLAN_GBP packet */
664                   addr4 = ip40->dst_address;
665                 }
666             }
667           else
668             {
669               if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
670                 {
671                   if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
672                     goto exit0; /* no local VTEP for VXLAN_GBP packet */
673                   addr6 = ip60->dst_address;
674                 }
675             }
676
677           flags0 = b0->flags;
678           good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
679
680           /* Don't verify UDP checksum for packets with explicit zero checksum. */
681           good_udp0 |= udp0->checksum == 0;
682
683           /* Verify UDP length */
684           if (is_ip4)
685             ip_len0 = clib_net_to_host_u16 (ip40->length);
686           else
687             ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
688           udp_len0 = clib_net_to_host_u16 (udp0->length);
689           len_diff0 = ip_len0 - udp_len0;
690
691           /* Verify UDP checksum */
692           if (PREDICT_FALSE (!good_udp0))
693             {
694               if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
695                 {
696                   if (is_ip4)
697                     flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
698                   else
699                     flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
700                   good_udp0 =
701                     (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
702                 }
703             }
704
705           if (is_ip4)
706             {
707               error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
708               error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
709             }
710           else
711             {
712               error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
713               error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
714             }
715
716           next0 = error0 ?
717             IP_VXLAN_GBP_BYPASS_NEXT_DROP :
718             IP_VXLAN_GBP_BYPASS_NEXT_VXLAN_GBP;
719           b0->error = error0 ? error_node->errors[error0] : 0;
720
721           /* vxlan-gbp-input node expect current at VXLAN_GBP header */
722           if (is_ip4)
723             vlib_buffer_advance (b0,
724                                  sizeof (ip4_header_t) +
725                                  sizeof (udp_header_t));
726           else
727             vlib_buffer_advance (b0,
728                                  sizeof (ip6_header_t) +
729                                  sizeof (udp_header_t));
730
731         exit0:
732           /* Process packet 1 */
733           if (proto1 != IP_PROTOCOL_UDP)
734             goto exit1;         /* not UDP packet */
735
736           if (is_ip4)
737             udp1 = ip4_next_header (ip41);
738           else
739             udp1 = ip6_next_header (ip61);
740
741           if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gbp))
742             goto exit1;         /* not VXLAN_GBP packet */
743
744           /* Validate DIP against VTEPs */
745           if (is_ip4)
746             {
747               if (addr4.as_u32 != ip41->dst_address.as_u32)
748                 {
749                   if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32))
750                     goto exit1; /* no local VTEP for VXLAN_GBP packet */
751                   addr4 = ip41->dst_address;
752                 }
753             }
754           else
755             {
756               if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
757                 {
758                   if (!hash_get_mem (vxm->vtep6, &ip61->dst_address))
759                     goto exit1; /* no local VTEP for VXLAN_GBP packet */
760                   addr6 = ip61->dst_address;
761                 }
762             }
763
764           flags1 = b1->flags;
765           good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
766
767           /* Don't verify UDP checksum for packets with explicit zero checksum. */
768           good_udp1 |= udp1->checksum == 0;
769
770           /* Verify UDP length */
771           if (is_ip4)
772             ip_len1 = clib_net_to_host_u16 (ip41->length);
773           else
774             ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
775           udp_len1 = clib_net_to_host_u16 (udp1->length);
776           len_diff1 = ip_len1 - udp_len1;
777
778           /* Verify UDP checksum */
779           if (PREDICT_FALSE (!good_udp1))
780             {
781               if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
782                 {
783                   if (is_ip4)
784                     flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
785                   else
786                     flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
787                   good_udp1 =
788                     (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
789                 }
790             }
791
792           if (is_ip4)
793             {
794               error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
795               error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
796             }
797           else
798             {
799               error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
800               error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
801             }
802
803           next1 = error1 ?
804             IP_VXLAN_GBP_BYPASS_NEXT_DROP :
805             IP_VXLAN_GBP_BYPASS_NEXT_VXLAN_GBP;
806           b1->error = error1 ? error_node->errors[error1] : 0;
807
808           /* vxlan_gbp-input node expect current at VXLAN_GBP header */
809           if (is_ip4)
810             vlib_buffer_advance (b1,
811                                  sizeof (ip4_header_t) +
812                                  sizeof (udp_header_t));
813           else
814             vlib_buffer_advance (b1,
815                                  sizeof (ip6_header_t) +
816                                  sizeof (udp_header_t));
817
818         exit1:
819           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
820                                            to_next, n_left_to_next,
821                                            bi0, bi1, next0, next1);
822         }
823
824       while (n_left_from > 0 && n_left_to_next > 0)
825         {
826           vlib_buffer_t *b0;
827           ip4_header_t *ip40;
828           ip6_header_t *ip60;
829           udp_header_t *udp0;
830           u32 bi0, ip_len0, udp_len0, flags0, next0;
831           i32 len_diff0;
832           u8 error0, good_udp0, proto0;
833
834           bi0 = to_next[0] = from[0];
835           from += 1;
836           n_left_from -= 1;
837           to_next += 1;
838           n_left_to_next -= 1;
839
840           b0 = vlib_get_buffer (vm, bi0);
841           if (is_ip4)
842             ip40 = vlib_buffer_get_current (b0);
843           else
844             ip60 = vlib_buffer_get_current (b0);
845
846           /* Setup packet for next IP feature */
847           vnet_feature_next (&next0, b0);
848
849           if (is_ip4)
850             /* Treat IP4 frag packets as "experimental" protocol for now
851                until support of IP frag reassembly is implemented */
852             proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
853           else
854             proto0 = ip60->protocol;
855
856           if (proto0 != IP_PROTOCOL_UDP)
857             goto exit;          /* not UDP packet */
858
859           if (is_ip4)
860             udp0 = ip4_next_header (ip40);
861           else
862             udp0 = ip6_next_header (ip60);
863
864           if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gbp))
865             goto exit;          /* not VXLAN_GBP packet */
866
867           /* Validate DIP against VTEPs */
868           if (is_ip4)
869             {
870               if (addr4.as_u32 != ip40->dst_address.as_u32)
871                 {
872                   if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
873                     goto exit;  /* no local VTEP for VXLAN_GBP packet */
874                   addr4 = ip40->dst_address;
875                 }
876             }
877           else
878             {
879               if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
880                 {
881                   if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
882                     goto exit;  /* no local VTEP for VXLAN_GBP packet */
883                   addr6 = ip60->dst_address;
884                 }
885             }
886
887           flags0 = b0->flags;
888           good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
889
890           /* Don't verify UDP checksum for packets with explicit zero checksum. */
891           good_udp0 |= udp0->checksum == 0;
892
893           /* Verify UDP length */
894           if (is_ip4)
895             ip_len0 = clib_net_to_host_u16 (ip40->length);
896           else
897             ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
898           udp_len0 = clib_net_to_host_u16 (udp0->length);
899           len_diff0 = ip_len0 - udp_len0;
900
901           /* Verify UDP checksum */
902           if (PREDICT_FALSE (!good_udp0))
903             {
904               if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
905                 {
906                   if (is_ip4)
907                     flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
908                   else
909                     flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
910                   good_udp0 =
911                     (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
912                 }
913             }
914
915           if (is_ip4)
916             {
917               error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
918               error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
919             }
920           else
921             {
922               error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
923               error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
924             }
925
926           next0 = error0 ?
927             IP_VXLAN_GBP_BYPASS_NEXT_DROP :
928             IP_VXLAN_GBP_BYPASS_NEXT_VXLAN_GBP;
929           b0->error = error0 ? error_node->errors[error0] : 0;
930
931           /* vxlan_gbp-input node expect current at VXLAN_GBP header */
932           if (is_ip4)
933             vlib_buffer_advance (b0,
934                                  sizeof (ip4_header_t) +
935                                  sizeof (udp_header_t));
936           else
937             vlib_buffer_advance (b0,
938                                  sizeof (ip6_header_t) +
939                                  sizeof (udp_header_t));
940
941         exit:
942           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
943                                            to_next, n_left_to_next,
944                                            bi0, next0);
945         }
946
947       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
948     }
949
950   return frame->n_vectors;
951 }
952
953 VLIB_NODE_FN (ip4_vxlan_gbp_bypass_node) (vlib_main_t * vm,
954                                           vlib_node_runtime_t * node,
955                                           vlib_frame_t * frame)
956 {
957   return ip_vxlan_gbp_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
958 }
959
960 /* *INDENT-OFF* */
961 VLIB_REGISTER_NODE (ip4_vxlan_gbp_bypass_node) =
962 {
963   .name = "ip4-vxlan-gbp-bypass",
964   .vector_size = sizeof (u32),
965   .n_next_nodes = IP_VXLAN_GBP_BYPASS_N_NEXT,
966   .next_nodes = {
967           [IP_VXLAN_GBP_BYPASS_NEXT_DROP] = "error-drop",
968           [IP_VXLAN_GBP_BYPASS_NEXT_VXLAN_GBP] = "vxlan4-gbp-input",
969   },
970   .format_buffer = format_ip4_header,
971   .format_trace = format_ip4_forward_next_trace,
972 };
973 /* *INDENT-ON* */
974
975 #ifndef CLIB_MARCH_VARIANT
976 /* Dummy init function to get us linked in. */
977 clib_error_t *
978 ip4_vxlan_gbp_bypass_init (vlib_main_t * vm)
979 {
980   return 0;
981 }
982
983 VLIB_INIT_FUNCTION (ip4_vxlan_gbp_bypass_init);
984 #endif /* CLIB_MARCH_VARIANT */
985
986 VLIB_NODE_FN (ip6_vxlan_gbp_bypass_node) (vlib_main_t * vm,
987                                           vlib_node_runtime_t * node,
988                                           vlib_frame_t * frame)
989 {
990   return ip_vxlan_gbp_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
991 }
992
993 /* *INDENT-OFF* */
994 VLIB_REGISTER_NODE (ip6_vxlan_gbp_bypass_node) =
995 {
996   .name = "ip6-vxlan-gbp-bypass",
997   .vector_size = sizeof (u32),
998   .n_next_nodes = IP_VXLAN_GBP_BYPASS_N_NEXT,
999   .next_nodes = {
1000     [IP_VXLAN_GBP_BYPASS_NEXT_DROP] = "error-drop",
1001     [IP_VXLAN_GBP_BYPASS_NEXT_VXLAN_GBP] = "vxlan6-gbp-input",
1002   },
1003   .format_buffer = format_ip6_header,
1004   .format_trace = format_ip6_forward_next_trace,
1005 };
1006 /* *INDENT-ON* */
1007
1008 #ifndef CLIB_MARCH_VARIANT
1009 /* Dummy init function to get us linked in. */
1010 clib_error_t *
1011 ip6_vxlan_gbp_bypass_init (vlib_main_t * vm)
1012 {
1013   return 0;
1014 }
1015
1016 VLIB_INIT_FUNCTION (ip6_vxlan_gbp_bypass_init);
1017 #endif /* CLIB_MARCH_VARIANT */
1018
1019 /*
1020  * fd.io coding-style-patch-verification: ON
1021  *
1022  * Local Variables:
1023  * eval: (c-set-style "gnu")
1024  * End:
1025  */