Initial commit of vpp code.
[vpp.git] / vnet / vnet / l2 / l2_classify.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * l2_classify.c
17  */
18
19 #include <vnet/l2/l2_classify.h>
20 #include <vnet/api_errno.h>
21
22 typedef struct {
23   /* per-pkt trace data */ 
24   u32 sw_if_index;
25   u32 next_index;
26   u32 table_index;
27   u32 session_offset;
28 } l2_classify_trace_t;
29
30 typedef struct {
31   vnet_classify_main_t * vcm;
32   l2_classify_main_t * l2cm;
33 } l2_classify_runtime_t;
34
35 /* packet trace format function */
36 static u8 * format_l2_classify_trace (u8 * s, va_list * args)
37 {
38   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40   l2_classify_trace_t * t = va_arg (*args, l2_classify_trace_t *);
41   
42   s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d",
43               t->sw_if_index, t->table_index, t->session_offset, t->next_index);
44   return s;
45 }
46
47 l2_classify_main_t l2_classify_main;
48
49 vlib_node_registration_t l2_classify_node;
50
51 #define foreach_l2_classify_error               \
52 _(MISS, "Classify misses")                      \
53 _(HIT, "Classify hits")                         \
54 _(CHAIN_HIT, "Classify hits after chain walk")  \
55 _(DROP, "L2 Classify Drops")
56
57 typedef enum {
58 #define _(sym,str) L2_CLASSIFY_ERROR_##sym,
59   foreach_l2_classify_error
60 #undef _
61   L2_CLASSIFY_N_ERROR,
62 } l2_classify_error_t;
63
64 static char * l2_classify_error_strings[] = {
65 #define _(sym,string) string,
66   foreach_l2_classify_error
67 #undef _
68 };
69
70 static uword
71 l2_classify_node_fn (vlib_main_t * vm,
72                   vlib_node_runtime_t * node,
73                   vlib_frame_t * frame)
74 {
75   u32 n_left_from, * from, * to_next;
76   l2_classify_next_t next_index;
77   l2_classify_main_t * cm = &l2_classify_main;
78   vnet_classify_main_t * vcm = cm->vnet_classify_main;
79   l2_classify_runtime_t * rt = (l2_classify_runtime_t *)node->runtime_data;
80   u32 feature_bitmap;
81   u32 hits = 0;
82   u32 misses = 0;
83   u32 chain_hits = 0;
84   f64 now;
85   
86   now = vlib_time_now(vm);
87
88   n_left_from = frame->n_vectors;
89   from = vlib_frame_vector_args (frame);
90
91   /* First pass: compute hash */
92
93   while (n_left_from > 2)
94     {
95       vlib_buffer_t * b0, * b1;
96       u32 bi0, bi1;
97       ethernet_header_t * h0, * h1;
98       u32 sw_if_index0, sw_if_index1;
99       u16 type0, type1;
100       int type_index0, type_index1;
101       vnet_classify_table_t * t0, * t1;
102       u32 table_index0, table_index1;
103       u64 hash0, hash1;
104
105
106       /* prefetch next iteration */
107         {
108           vlib_buffer_t * p1, * p2;
109           
110           p1 = vlib_get_buffer (vm, from[1]);
111           p2 = vlib_get_buffer (vm, from[2]);
112           
113           vlib_prefetch_buffer_header (p1, STORE);
114           CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
115           vlib_prefetch_buffer_header (p2, STORE);
116           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
117         }
118         
119       bi0 = from[0];
120       b0 = vlib_get_buffer (vm, bi0);
121       h0 = vlib_buffer_get_current (b0);
122
123       bi1 = from[1];
124       b1 = vlib_get_buffer (vm, bi1);
125       h1 = vlib_buffer_get_current (b1);
126         
127       sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
128       vnet_buffer(b0)->l2_classify.table_index = ~0;
129
130       sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
131       vnet_buffer(b1)->l2_classify.table_index = ~0;
132         
133       /* Select classifier table based on ethertype */
134       type0 = clib_net_to_host_u16 (h0->type);
135       type1 = clib_net_to_host_u16 (h1->type);
136
137       type_index0 = (type0 == ETHERNET_TYPE_IP4) 
138           ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
139       type_index0 = (type0 == ETHERNET_TYPE_IP6) 
140           ? L2_CLASSIFY_TABLE_IP6 : type_index0;
141
142       type_index1 = (type1 == ETHERNET_TYPE_IP4) 
143           ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
144       type_index1 = (type1 == ETHERNET_TYPE_IP6) 
145           ? L2_CLASSIFY_TABLE_IP6 : type_index1;
146       
147       vnet_buffer(b0)->l2_classify.table_index = 
148           table_index0 = 
149           rt->l2cm->classify_table_index_by_sw_if_index
150           [type_index0][sw_if_index0];
151             
152       if (table_index0 != ~0)
153         {
154           t0 = pool_elt_at_index (vcm->tables, table_index0);
155           
156           vnet_buffer(b0)->l2_classify.hash = hash0 = 
157               vnet_classify_hash_packet (t0, (u8 *) h0);
158           vnet_classify_prefetch_bucket (t0, hash0);
159         }
160
161       vnet_buffer(b1)->l2_classify.table_index = 
162         table_index1 = 
163         rt->l2cm->classify_table_index_by_sw_if_index
164         [type_index1][sw_if_index1];
165       
166       if (table_index1 != ~0)
167         {
168           t1 = pool_elt_at_index (vcm->tables, table_index1);
169           
170           vnet_buffer(b1)->l2_classify.hash = hash1 = 
171             vnet_classify_hash_packet (t1, (u8 *) h1);
172           vnet_classify_prefetch_bucket (t1, hash1);
173         }
174
175       from += 2;
176       n_left_from -= 2;
177     }
178
179   while (n_left_from > 0)
180     {
181       vlib_buffer_t * b0;
182       u32 bi0;
183       ethernet_header_t * h0;
184       u32 sw_if_index0;
185       u16 type0;
186       u32 type_index0;
187       vnet_classify_table_t * t0;
188       u32 table_index0;
189       u64 hash0;
190
191       bi0 = from[0];
192       b0 = vlib_get_buffer (vm, bi0);
193       h0 = vlib_buffer_get_current (b0);
194         
195       sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
196       vnet_buffer(b0)->l2_classify.table_index = ~0;
197         
198       /* Select classifier table based on ethertype */
199       type0 = clib_net_to_host_u16 (h0->type);
200         
201       type_index0 = (type0 == ETHERNET_TYPE_IP4) 
202           ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
203       type_index0 = (type0 == ETHERNET_TYPE_IP6) 
204           ? L2_CLASSIFY_TABLE_IP6 : type_index0;
205
206       vnet_buffer(b0)->l2_classify.table_index = 
207           table_index0 = rt->l2cm->classify_table_index_by_sw_if_index
208           [type_index0][sw_if_index0];
209             
210       if (table_index0 != ~0)
211         {
212           t0 = pool_elt_at_index (vcm->tables, table_index0);
213           
214           vnet_buffer(b0)->l2_classify.hash = hash0 = 
215             vnet_classify_hash_packet (t0, (u8 *) h0);
216           vnet_classify_prefetch_bucket (t0, hash0);
217         }
218       from++;
219       n_left_from--;
220     }
221         
222   next_index = node->cached_next_index;
223   from = vlib_frame_vector_args (frame);
224   n_left_from = frame->n_vectors;
225
226   while (n_left_from > 0)
227     {
228       u32 n_left_to_next;
229
230       vlib_get_next_frame (vm, node, next_index,
231                            to_next, n_left_to_next);
232
233       /* Not enough load/store slots to dual loop... */
234       while (n_left_from > 0 && n_left_to_next > 0)
235         {
236           u32 bi0;
237           vlib_buffer_t * b0;
238           u32 next0 = L2_CLASSIFY_NEXT_ETHERNET_INPUT;
239           ethernet_header_t * h0;
240           u32 table_index0;
241           u64 hash0;
242           vnet_classify_table_t * t0;
243           vnet_classify_entry_t * e0;
244
245           if (PREDICT_TRUE (n_left_from > 2))
246             {
247               vlib_buffer_t * p2 = vlib_get_buffer(vm, from[2]);
248               u64 phash2;
249               u32 table_index2;
250               vnet_classify_table_t * tp2;
251
252               /* 
253                * Prefetch table entry two ahead. Buffer / data
254                * were prefetched above...
255                */
256               table_index2 = vnet_buffer(p2)->l2_classify.table_index;
257
258               if (PREDICT_TRUE (table_index2 != ~0))
259                 {
260                   tp2 = pool_elt_at_index (vcm->tables, table_index2);
261                   phash2 = vnet_buffer(p2)->l2_classify.hash;
262                   vnet_classify_prefetch_entry (tp2, phash2); 
263                 }
264             }
265
266           /* speculatively enqueue b0 to the current next frame */
267           bi0 = from[0];
268           to_next[0] = bi0;
269           from += 1;
270           to_next += 1;
271           n_left_from -= 1;
272           n_left_to_next -= 1;
273
274           b0 = vlib_get_buffer (vm, bi0);
275           h0 = vlib_buffer_get_current(b0);
276           table_index0 = vnet_buffer(b0)->l2_classify.table_index;
277           e0 = 0;
278
279           if (PREDICT_TRUE(table_index0 != ~0))
280             {
281               hash0 = vnet_buffer(b0)->l2_classify.hash;
282               t0 = pool_elt_at_index (vcm->tables, table_index0);
283
284               e0 = vnet_classify_find_entry (t0, (u8 *) h0, 
285                                              hash0, now);
286               if (e0)
287                 {
288                   vnet_buffer(b0)->l2_classify.opaque_index 
289                     = e0->opaque_index;
290                   vlib_buffer_advance (b0, e0->advance);
291                   next0 = (e0->next_index < L2_CLASSIFY_N_NEXT)?
292                            e0->next_index:next0;
293                   hits++;
294                 }
295               else
296                 {
297                   while (1)
298                     {
299                       if (t0->next_table_index != ~0)
300                         t0 = pool_elt_at_index (vcm->tables,
301                                                 t0->next_table_index);
302                       else
303                         {
304                           next0 = (t0->miss_next_index < L2_CLASSIFY_N_NEXT)?
305                                    t0->miss_next_index:next0;
306                           misses++;
307                           break;
308                         }
309
310                       hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
311                       e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
312                       if (e0)
313                         {
314                           vnet_buffer(b0)->l2_classify.opaque_index
315                             = e0->opaque_index;
316                           vlib_buffer_advance (b0, e0->advance);
317                           next0 = (e0->next_index < L2_CLASSIFY_N_NEXT)?
318                                    e0->next_index:next0;
319                           hits++;
320                           chain_hits++;
321                           break;
322                         }
323                     }
324                 }
325             }
326
327           if (PREDICT_FALSE(next0 == 0))
328             b0->error = node->errors[L2_CLASSIFY_ERROR_DROP];
329
330           if (PREDICT_FALSE (next0 == ~0))
331             {
332             
333               // Remove ourself from the feature bitmap
334               feature_bitmap = vnet_buffer(b0)->l2.feature_bitmap 
335                 & ~L2INPUT_FEAT_CLASSIFY;
336               
337               // save for next feature graph nodes
338               vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap;
339
340               // Determine the next node
341               next0 = feat_bitmap_get_next_node_index(cm->feat_next_node_index,
342                                                       feature_bitmap);
343             }
344
345           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
346                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
347             {
348               l2_classify_trace_t *t = 
349                 vlib_add_trace (vm, node, b0, sizeof (*t));
350               t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
351               t->table_index = table_index0;
352               t->next_index = next0;
353               t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0;
354             }
355
356           /* verify speculative enqueue, maybe switch current next frame */
357           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
358                                            to_next, n_left_to_next,
359                                            bi0, next0);
360         }
361
362       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
363     }
364
365   vlib_node_increment_counter (vm, node->node_index, 
366                                L2_CLASSIFY_ERROR_MISS, 
367                                misses);
368   vlib_node_increment_counter (vm, node->node_index, 
369                                L2_CLASSIFY_ERROR_HIT, 
370                                hits);
371   vlib_node_increment_counter (vm, node->node_index, 
372                                L2_CLASSIFY_ERROR_CHAIN_HIT, 
373                                chain_hits);
374   return frame->n_vectors;
375 }
376
377 VLIB_REGISTER_NODE (l2_classify_node) = {
378   .function = l2_classify_node_fn,
379   .name = "l2-classify",
380   .vector_size = sizeof (u32),
381   .format_trace = format_l2_classify_trace,
382   .type = VLIB_NODE_TYPE_INTERNAL,
383   
384   .n_errors = ARRAY_LEN(l2_classify_error_strings),
385   .error_strings = l2_classify_error_strings,
386
387   .runtime_data_bytes = sizeof (l2_classify_runtime_t),
388
389   .n_next_nodes = L2_CLASSIFY_N_NEXT,
390
391   /* edit / add dispositions here */
392   .next_nodes = {
393        [L2_CLASSIFY_NEXT_DROP]  = "error-drop",
394        [L2_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
395        [L2_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
396        [L2_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
397        [L2_CLASSIFY_NEXT_LI] = "li-hit",
398   },
399 };
400
401 clib_error_t *l2_classify_init (vlib_main_t *vm)
402 {
403   l2_classify_main_t * cm = &l2_classify_main;
404   l2_classify_runtime_t * rt;
405
406   rt = vlib_node_get_runtime_data (vm, l2_classify_node.index);
407  
408   cm->vlib_main = vm;
409   cm->vnet_main = vnet_get_main();
410   cm->vnet_classify_main = &vnet_classify_main;
411
412   // Initialize the feature next-node indexes
413   feat_bitmap_init_next_nodes(vm,
414                               l2_classify_node.index,
415                               L2INPUT_N_FEAT,
416                               l2input_get_feat_names(),
417                               cm->feat_next_node_index);
418   rt->l2cm = cm;
419   rt->vcm = cm->vnet_classify_main;
420
421   return 0;
422 }
423
424 VLIB_INIT_FUNCTION (l2_classify_init);
425
426
427 void vnet_l2_classify_enable_disable (u32 sw_if_index,
428                                       int enable_disable)
429 {
430   vlib_main_t * vm = vlib_get_main();
431   vnet_main_t * vnm = vnet_get_main();
432   
433   if (enable_disable)
434     set_int_l2_mode (vm, vnm, MODE_L2_CLASSIFY, sw_if_index,
435                      0, 0, 0, 0);
436   else
437     set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index,
438                      0, 0, 0, 0);
439 }
440
441 int vnet_l2_classify_set_tables (u32 sw_if_index, 
442                                  u32 ip4_table_index, 
443                                  u32 ip6_table_index, 
444                                  u32 other_table_index)
445 {
446   l2_classify_main_t * cm = &l2_classify_main;
447   vnet_classify_main_t * vcm = cm->vnet_classify_main;
448
449   /* Assume that we've validated sw_if_index in the API layer */
450
451   if (ip4_table_index != ~0 &&
452       pool_is_free_index (vcm->tables, ip4_table_index))
453     return VNET_API_ERROR_NO_SUCH_TABLE;
454
455   if (ip6_table_index != ~0 &&
456       pool_is_free_index (vcm->tables, ip6_table_index))
457     return VNET_API_ERROR_NO_SUCH_TABLE2;
458   
459   if (other_table_index != ~0 &&
460       pool_is_free_index (vcm->tables, other_table_index))
461     return VNET_API_ERROR_NO_SUCH_TABLE3;
462   
463   vec_validate 
464     (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4], 
465      sw_if_index);
466   
467   vec_validate 
468     (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6], 
469      sw_if_index);
470   
471   vec_validate 
472     (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER], 
473      sw_if_index);
474
475   cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
476     [sw_if_index] = ip4_table_index;
477   
478   cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
479     [sw_if_index] = ip6_table_index;
480   
481   cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
482     [sw_if_index] = other_table_index;
483
484   return 0;
485 }
486
487 static clib_error_t *
488 int_l2_classify_command_fn (vlib_main_t * vm,
489                             unformat_input_t * input,
490                             vlib_cli_command_t * cmd)
491 {
492   vnet_main_t * vnm = vnet_get_main();
493   u32 sw_if_index = ~0;
494   u32 ip4_table_index = ~0;
495   u32 ip6_table_index = ~0;
496   u32 other_table_index = ~0;
497   int rv;
498
499   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
500     {
501       if (unformat (input, "intfc %U", unformat_vnet_sw_interface, 
502                     vnm, &sw_if_index))
503         ;
504       else if (unformat (input, "ip4-table %d", &ip4_table_index))
505         ;
506       else if (unformat (input, "ip6-table %d", &ip6_table_index))
507         ;
508       else if (unformat (input, "other-table %d", &other_table_index))
509         ;
510       else
511         break;
512     }
513
514   if (sw_if_index == ~0)
515     return clib_error_return (0, "interface must be specified");
516   
517
518   if (ip4_table_index == ~0 && ip6_table_index == ~0 
519       && other_table_index == ~0)
520     {
521       vlib_cli_output (vm, "L2 classification disabled");
522       vnet_l2_classify_enable_disable (sw_if_index, 0 /* enable */);
523       return 0;
524     }
525
526   rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index, 
527                                     ip6_table_index, other_table_index);
528   switch(rv)
529     {
530     case 0:
531       vnet_l2_classify_enable_disable (sw_if_index, 1 /* enable */);
532       break;
533
534     default:
535       return clib_error_return (0, "vnet_l2_classify_set_tables: %d",
536                                 rv);
537       break;
538     }
539
540   return 0;
541 }
542
543 VLIB_CLI_COMMAND (int_l2_classify_cli, static) = {
544   .path = "set interface l2 classify",
545   .short_help = 
546   "set interface l2 classify intfc <int> [ip4-table <n>]\n"
547   "  [ip6-table <n>] [other-table <n>]",
548   .function = int_l2_classify_command_fn,
549 };
550
551