udp: fix csum computation when offload disabled
[vpp.git] / src / vnet / l2 / l2_efp_filter.c
1 /*
2  * l2_efp_filter.c : layer 2 egress EFP Filter processing
3  *
4  * Copyright (c) 2013 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/vnet.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/l2/feat_bitmap.h>
23 #include <vnet/l2/l2_output.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/l2/l2_efp_filter.h>
26
27 #include <vppinfra/error.h>
28 #include <vppinfra/cache.h>
29
30 /**
31  * @file
32  * @brief EFP-filter - Ethernet Flow Point Filter.
33  *
34  * It is possible to transmit a packet out a subinterface with VLAN tags
35  * that are not compatible with that subinterface. In other words, if that
36  * packet arrived on the output port, it would not be classified as coming
37  * from the output subinterface. This can happen in various ways: through
38  * misconfiguration, by putting subinterfaces with different VLAN encaps in
39  * the same bridge-domain, etc. The EFP Filter Check detects such packets
40  * and drops them. It consists of two checks, one that verifies the packet
41  * prior to output VLAN tag rewrite and one that verifies the packet after
42  * VLAN tag rewrite.
43  *
44  */
45 typedef struct
46 {
47   /* Next nodes for L2 output features */
48   u32 l2_out_feat_next[32];
49
50   /* convenience variables */
51   vlib_main_t *vlib_main;
52   vnet_main_t *vnet_main;
53 } l2_efp_filter_main_t;
54
55
56 typedef struct
57 {
58   /* per-pkt trace data */
59   u8 src[6];
60   u8 dst[6];
61   u8 raw[12];                   /* raw data (vlans) */
62   u32 sw_if_index;
63 } l2_efp_filter_trace_t;
64
65 /* packet trace format function */
66 static u8 *
67 format_l2_efp_filter_trace (u8 * s, va_list * args)
68 {
69   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71   l2_efp_filter_trace_t *t = va_arg (*args, l2_efp_filter_trace_t *);
72
73   s = format (s, "l2-output-vtr: sw_if_index %d dst %U src %U data "
74               "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
75               t->sw_if_index,
76               format_ethernet_address, t->dst,
77               format_ethernet_address, t->src,
78               t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
79               t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
80               t->raw[10], t->raw[11]);
81   return s;
82 }
83
84 extern l2_efp_filter_main_t l2_efp_filter_main;
85
86 #ifndef CLIB_MARCH_VARIANT
87 l2_efp_filter_main_t l2_efp_filter_main;
88 #endif /* CLIB_MARCH_VARIANT */
89
90 #define foreach_l2_efp_filter_error                     \
91 _(L2_EFP_FILTER, "L2 EFP filter packets")               \
92 _(DROP,          "L2 EFP filter post-rewrite drops")
93
94 typedef enum
95 {
96 #define _(sym,str) L2_EFP_FILTER_ERROR_##sym,
97   foreach_l2_efp_filter_error
98 #undef _
99     L2_EFP_FILTER_N_ERROR,
100 } l2_efp_filter_error_t;
101
102 static char *l2_efp_filter_error_strings[] = {
103 #define _(sym,string) string,
104   foreach_l2_efp_filter_error
105 #undef _
106 };
107
108 typedef enum
109 {
110   L2_EFP_FILTER_NEXT_DROP,
111   L2_EFP_FILTER_N_NEXT,
112 } l2_efp_filter_next_t;
113
114
115 /**
116  *  Extract fields from the packet that will be used in interface
117  *  classification.
118  */
119 static_always_inline void
120 extract_keys (vnet_main_t * vnet_main,
121               u32 sw_if_index0,
122               vlib_buffer_t * b0,
123               u32 * port_sw_if_index0,
124               u16 * first_ethertype0,
125               u16 * outer_id0, u16 * inner_id0, u32 * match_flags0)
126 {
127   ethernet_header_t *e0;
128   ethernet_vlan_header_t *h0;
129   u32 tag_len;
130   u32 tag_num;
131
132   *port_sw_if_index0 =
133     vnet_get_sup_sw_interface (vnet_main, sw_if_index0)->sw_if_index;
134
135   e0 = vlib_buffer_get_current (b0);
136   h0 = (ethernet_vlan_header_t *) (e0 + 1);
137
138   *first_ethertype0 = clib_net_to_host_u16 (e0->type);
139   *outer_id0 = clib_net_to_host_u16 (h0[0].priority_cfi_and_id);
140   *inner_id0 = clib_net_to_host_u16 (h0[1].priority_cfi_and_id);
141
142   tag_len = vnet_buffer (b0)->l2.l2_len - sizeof (ethernet_header_t);
143   tag_num = tag_len / sizeof (ethernet_vlan_header_t);
144   *match_flags0 = eth_create_valid_subint_match_flags (tag_num);
145 }
146
147 /*
148  * EFP filtering is a basic switch feature which prevents an interface from
149  * transmitting a packet that doesn't match the interface's ingress match
150  * criteria. The check has two parts, one performed before egress vlan tag
151  * rewrite and one after.
152  *
153  * The pre-rewrite check insures the packet matches what an ingress packet looks
154  * like after going through the interface's ingress tag rewrite operation. Only
155  * pushed tags are compared. So:
156  * - if the ingress vlan tag rewrite pushes no tags (or is not enabled),
157  *   any packet passes the filter
158  * - if the ingress vlan tag rewrite pushes one tag,
159  *   the packet must have at least one tag, and the outer tag must match the pushed tag
160  * - if the ingress vlan tag rewrite pushes two tags,
161  *   the packet must have at least two tags, and the outer two tags must match the pushed tags
162  *
163  * The pre-rewrite check is performed in the l2-output node.
164  *
165  * The post-rewrite check insures the packet matches what an ingress packet looks
166  * like before going through the interface's ingress tag rewrite operation. It verifies
167  * that such a packet arriving on the wire at this port would be classified as arriving
168  * an input interface equal to the packet's output interface. This can be done by running
169  * the output packet's vlan tags and output port through the interface classification,
170  * and checking if the resulting interface matches the output interface.
171  *
172  * The post-rewrite check is performed here.
173  */
174
175 VLIB_NODE_FN (l2_efp_filter_node) (vlib_main_t * vm,
176                                    vlib_node_runtime_t * node,
177                                    vlib_frame_t * frame)
178 {
179   u32 n_left_from, *from, *to_next;
180   l2_efp_filter_next_t next_index;
181   l2_efp_filter_main_t *msm = &l2_efp_filter_main;
182   vlib_node_t *n = vlib_get_node (vm, l2_efp_filter_node.index);
183   u32 node_counter_base_index = n->error_heap_index;
184   vlib_error_main_t *em = &vm->error_main;
185
186   from = vlib_frame_vector_args (frame);
187   n_left_from = frame->n_vectors;       /* number of packets to process */
188   next_index = node->cached_next_index;
189
190   while (n_left_from > 0)
191     {
192       u32 n_left_to_next;
193
194       /* get space to enqueue frame to graph node "next_index" */
195       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
196
197       while (n_left_from >= 6 && n_left_to_next >= 2)
198         {
199           u32 bi0, bi1;
200           vlib_buffer_t *b0, *b1;
201           u32 next0, next1;
202           u32 sw_if_index0, sw_if_index1;
203           u16 first_ethertype0, first_ethertype1;
204           u16 outer_id0, inner_id0, outer_id1, inner_id1;
205           u32 match_flags0, match_flags1;
206           u32 port_sw_if_index0, subint_sw_if_index0, port_sw_if_index1,
207             subint_sw_if_index1;
208           vnet_hw_interface_t *hi0, *hi1;
209           main_intf_t *main_intf0, *main_intf1;
210           vlan_intf_t *vlan_intf0, *vlan_intf1;
211           qinq_intf_t *qinq_intf0, *qinq_intf1;
212           u32 is_l20, is_l21;
213           __attribute__ ((unused)) u32 matched0, matched1;
214           u8 error0, error1;
215
216           /* Prefetch next iteration. */
217           {
218             vlib_buffer_t *p2, *p3, *p4, *p5;
219             __attribute__ ((unused)) u32 sw_if_index2, sw_if_index3;
220
221             p2 = vlib_get_buffer (vm, from[2]);
222             p3 = vlib_get_buffer (vm, from[3]);
223             p4 = vlib_get_buffer (vm, from[4]);
224             p5 = vlib_get_buffer (vm, from[5]);
225
226             /* Prefetch the buffer header and packet for the N+2 loop iteration */
227             vlib_prefetch_buffer_header (p4, LOAD);
228             vlib_prefetch_buffer_header (p5, LOAD);
229
230             clib_prefetch_store (p4->data);
231             clib_prefetch_store (p5->data);
232
233             /*
234              * Prefetch the input config for the N+1 loop iteration
235              * This depends on the buffer header above
236              */
237             sw_if_index2 = vnet_buffer (p2)->sw_if_index[VLIB_TX];
238             sw_if_index3 = vnet_buffer (p3)->sw_if_index[VLIB_TX];
239             /*
240              * $$$ TODO
241              * clib_prefetch_load (vec_elt_at_index(l2output_main.configs,
242              *                     sw_if_index2));
243              * clib_prefetch_load (vec_elt_at_index(l2output_main.configs,
244              *                     sw_if_index3));
245              */
246           }
247
248           /* speculatively enqueue b0 and b1 to the current next frame */
249           /* bi is "buffer index", b is pointer to the buffer */
250           to_next[0] = bi0 = from[0];
251           to_next[1] = bi1 = from[1];
252           from += 2;
253           to_next += 2;
254           n_left_from -= 2;
255           n_left_to_next -= 2;
256
257           b0 = vlib_get_buffer (vm, bi0);
258           b1 = vlib_get_buffer (vm, bi1);
259
260           /* TX interface handles */
261           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
262           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
263
264           /* process 2 packets */
265           em->counters[node_counter_base_index +
266                        L2_EFP_FILTER_ERROR_L2_EFP_FILTER] += 2;
267
268           /* Determine next node */
269           next0 = vnet_l2_feature_next (b0, msm->l2_out_feat_next,
270                                         L2OUTPUT_FEAT_EFP_FILTER);
271           next1 = vnet_l2_feature_next (b1, msm->l2_out_feat_next,
272                                         L2OUTPUT_FEAT_EFP_FILTER);
273
274           /* perform the efp filter check on two packets */
275
276           extract_keys (msm->vnet_main,
277                         sw_if_index0,
278                         b0,
279                         &port_sw_if_index0,
280                         &first_ethertype0,
281                         &outer_id0, &inner_id0, &match_flags0);
282
283           extract_keys (msm->vnet_main,
284                         sw_if_index1,
285                         b1,
286                         &port_sw_if_index1,
287                         &first_ethertype1,
288                         &outer_id1, &inner_id1, &match_flags1);
289
290           eth_vlan_table_lookups (&ethernet_main,
291                                   msm->vnet_main,
292                                   port_sw_if_index0,
293                                   first_ethertype0,
294                                   outer_id0,
295                                   inner_id0,
296                                   &hi0,
297                                   &main_intf0, &vlan_intf0, &qinq_intf0);
298
299           eth_vlan_table_lookups (&ethernet_main,
300                                   msm->vnet_main,
301                                   port_sw_if_index1,
302                                   first_ethertype1,
303                                   outer_id1,
304                                   inner_id1,
305                                   &hi1,
306                                   &main_intf1, &vlan_intf1, &qinq_intf1);
307
308           matched0 = eth_identify_subint (hi0,
309                                           match_flags0,
310                                           main_intf0,
311                                           vlan_intf0,
312                                           qinq_intf0,
313                                           &subint_sw_if_index0,
314                                           &error0, &is_l20);
315
316           matched1 = eth_identify_subint (hi1,
317                                           match_flags1,
318                                           main_intf1,
319                                           vlan_intf1,
320                                           qinq_intf1,
321                                           &subint_sw_if_index1,
322                                           &error1, &is_l21);
323
324           if (PREDICT_FALSE (sw_if_index0 != subint_sw_if_index0))
325             {
326               /* Drop packet */
327               next0 = L2_EFP_FILTER_NEXT_DROP;
328               b0->error = node->errors[L2_EFP_FILTER_ERROR_DROP];
329             }
330
331           if (PREDICT_FALSE (sw_if_index1 != subint_sw_if_index1))
332             {
333               /* Drop packet */
334               next1 = L2_EFP_FILTER_NEXT_DROP;
335               b1->error = node->errors[L2_EFP_FILTER_ERROR_DROP];
336             }
337
338           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
339             {
340               if (b0->flags & VLIB_BUFFER_IS_TRACED)
341                 {
342                   ethernet_header_t *h0 = vlib_buffer_get_current (b0);
343                   l2_efp_filter_trace_t *t =
344                     vlib_add_trace (vm, node, b0, sizeof (*t));
345                   t->sw_if_index = sw_if_index0;
346                   clib_memcpy_fast (t->src, h0->src_address, 6);
347                   clib_memcpy_fast (t->dst, h0->dst_address, 6);
348                   clib_memcpy_fast (t->raw, &h0->type, sizeof (t->raw));
349                 }
350               if (b1->flags & VLIB_BUFFER_IS_TRACED)
351                 {
352                   ethernet_header_t *h1 = vlib_buffer_get_current (b1);
353                   l2_efp_filter_trace_t *t =
354                     vlib_add_trace (vm, node, b1, sizeof (*t));
355                   t->sw_if_index = sw_if_index1;
356                   clib_memcpy_fast (t->src, h1->src_address, 6);
357                   clib_memcpy_fast (t->dst, h1->dst_address, 6);
358                   clib_memcpy_fast (t->raw, &h1->type, sizeof (t->raw));
359                 }
360             }
361
362           /* verify speculative enqueues, maybe switch current next frame */
363           /* if next0==next1==next_index then nothing special needs to be done */
364           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
365                                            to_next, n_left_to_next,
366                                            bi0, bi1, next0, next1);
367         }
368
369       while (n_left_from > 0 && n_left_to_next > 0)
370         {
371           u32 bi0;
372           vlib_buffer_t *b0;
373           u32 next0;
374           u32 sw_if_index0;
375           u16 first_ethertype0;
376           u16 outer_id0, inner_id0;
377           u32 match_flags0;
378           u32 port_sw_if_index0, subint_sw_if_index0;
379           vnet_hw_interface_t *hi0;
380           main_intf_t *main_intf0;
381           vlan_intf_t *vlan_intf0;
382           qinq_intf_t *qinq_intf0;
383           u32 is_l20;
384           __attribute__ ((unused)) u32 matched0;
385           u8 error0;
386
387           /* speculatively enqueue b0 to the current next frame */
388           bi0 = from[0];
389           to_next[0] = bi0;
390           from += 1;
391           to_next += 1;
392           n_left_from -= 1;
393           n_left_to_next -= 1;
394
395           b0 = vlib_get_buffer (vm, bi0);
396           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
397
398           /* process 1 packet */
399           em->counters[node_counter_base_index +
400                        L2_EFP_FILTER_ERROR_L2_EFP_FILTER] += 1;
401
402           /* Determine next node */
403           next0 = vnet_l2_feature_next (b0, msm->l2_out_feat_next,
404                                         L2OUTPUT_FEAT_EFP_FILTER);
405
406           /* perform the efp filter check on one packet */
407
408           extract_keys (msm->vnet_main,
409                         sw_if_index0,
410                         b0,
411                         &port_sw_if_index0,
412                         &first_ethertype0,
413                         &outer_id0, &inner_id0, &match_flags0);
414
415           eth_vlan_table_lookups (&ethernet_main,
416                                   msm->vnet_main,
417                                   port_sw_if_index0,
418                                   first_ethertype0,
419                                   outer_id0,
420                                   inner_id0,
421                                   &hi0,
422                                   &main_intf0, &vlan_intf0, &qinq_intf0);
423
424           matched0 = eth_identify_subint (hi0,
425                                           match_flags0,
426                                           main_intf0,
427                                           vlan_intf0,
428                                           qinq_intf0,
429                                           &subint_sw_if_index0,
430                                           &error0, &is_l20);
431
432           if (PREDICT_FALSE (sw_if_index0 != subint_sw_if_index0))
433             {
434               /* Drop packet */
435               next0 = L2_EFP_FILTER_NEXT_DROP;
436               b0->error = node->errors[L2_EFP_FILTER_ERROR_DROP];
437             }
438
439           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
440                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
441             {
442               ethernet_header_t *h0 = vlib_buffer_get_current (b0);
443               l2_efp_filter_trace_t *t =
444                 vlib_add_trace (vm, node, b0, sizeof (*t));
445               t->sw_if_index = sw_if_index0;
446               clib_memcpy_fast (t->src, h0->src_address, 6);
447               clib_memcpy_fast (t->dst, h0->dst_address, 6);
448               clib_memcpy_fast (t->raw, &h0->type, sizeof (t->raw));
449             }
450
451           /* verify speculative enqueue, maybe switch current next frame */
452           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
453                                            to_next, n_left_to_next,
454                                            bi0, next0);
455         }
456
457       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
458     }
459
460   return frame->n_vectors;
461 }
462
463
464 VLIB_REGISTER_NODE (l2_efp_filter_node) = {
465   .name = "l2-efp-filter",
466   .vector_size = sizeof (u32),
467   .format_trace = format_l2_efp_filter_trace,
468   .type = VLIB_NODE_TYPE_INTERNAL,
469
470   .n_errors = ARRAY_LEN(l2_efp_filter_error_strings),
471   .error_strings = l2_efp_filter_error_strings,
472
473   .n_next_nodes = L2_EFP_FILTER_N_NEXT,
474
475   /* edit / add dispositions here */
476   .next_nodes = {
477        [L2_EFP_FILTER_NEXT_DROP]  = "error-drop",
478   },
479 };
480
481 #ifndef CLIB_MARCH_VARIANT
482 clib_error_t *
483 l2_efp_filter_init (vlib_main_t * vm)
484 {
485   l2_efp_filter_main_t *mp = &l2_efp_filter_main;
486
487   mp->vlib_main = vm;
488   mp->vnet_main = vnet_get_main ();
489
490   /* Initialize the feature next-node indexes */
491   feat_bitmap_init_next_nodes (vm,
492                                l2_efp_filter_node.index,
493                                L2OUTPUT_N_FEAT,
494                                l2output_get_feat_names (),
495                                mp->l2_out_feat_next);
496
497   return 0;
498 }
499
500 VLIB_INIT_FUNCTION (l2_efp_filter_init);
501
502
503 /** Enable/disable the EFP Filter check on the subinterface. */
504 void
505 l2_efp_filter_configure (vnet_main_t * vnet_main, u32 sw_if_index, u8 enable)
506 {
507   /* set the interface flag */
508   l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_EFP_FILTER, enable);
509 }
510
511
512 /**
513  * Set subinterface egress efp filter enable/disable.
514  * The CLI format is:
515  *    set interface l2 efp-filter <interface> [disable]]
516  */
517 static clib_error_t *
518 int_l2_efp_filter (vlib_main_t * vm,
519                    unformat_input_t * input, vlib_cli_command_t * cmd)
520 {
521   vnet_main_t *vnm = vnet_get_main ();
522   clib_error_t *error = 0;
523   u32 sw_if_index;
524   u32 enable;
525
526   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
527     {
528       error = clib_error_return (0, "unknown interface `%U'",
529                                  format_unformat_error, input);
530       goto done;
531     }
532
533   enable = 1;
534   if (unformat (input, "disable"))
535     {
536       enable = 0;
537     }
538
539   /* enable/disable the feature */
540   l2_efp_filter_configure (vnm, sw_if_index, enable);
541
542 done:
543   return error;
544 }
545
546
547 /*?
548  * EFP filtering is a basic switch feature which prevents an interface from
549  * transmitting a packet that doesn't match the interface's ingress match
550  * criteria. The check has two parts, one performed before egress vlan tag
551  * rewrite and one after. This command enables or disables the EFP filtering
552  * for a given sub-interface.
553  *
554  * @cliexpar
555  * Example of how to enable a Layer 2 efp-filter on a sub-interface:
556  * @cliexcmd{set interface l2 efp-filter GigabitEthernet0/8/0.200}
557  * Example of how to disable a Layer 2 efp-filter on a sub-interface:
558  * @cliexcmd{set interface l2 efp-filter GigabitEthernet0/8/0.200 disable}
559 ?*/
560 VLIB_CLI_COMMAND (int_l2_efp_filter_cli, static) = {
561   .path = "set interface l2 efp-filter",
562   .short_help = "set interface l2 efp-filter <interface> [disable]",
563   .function = int_l2_efp_filter,
564 };
565
566 #endif /* CLIB_MARCH_VARIANT */
567
568 /*
569  * fd.io coding-style-patch-verification: ON
570  *
571  * Local Variables:
572  * eval: (c-set-style "gnu")
573  * End:
574  */