l2-rw: Packet rewrite feature for L2 bridges
[vpp.git] / vnet / vnet / l2 / l2_rw.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 #include <vlib/vlib.h>
17 #include <vnet/l2/feat_bitmap.h>
18 #include <vnet/l2/l2_rw.h>
19
20 l2_rw_main_t l2_rw_main;
21
22 vlib_node_registration_t l2_rw_node;
23
24 typedef struct {
25   u32 sw_if_index;
26   u32 classify_table_index;
27   u32 rewrite_entry_index;
28 } l2_rw_trace_t;
29
30 static u8 *format_l2_rw_entry (u8 * s, va_list *args)
31 {
32   l2_rw_entry_t *e = va_arg (*args, l2_rw_entry_t *);
33   l2_rw_main_t *rw = &l2_rw_main;
34   s = format (s, "%d -  mask:%U value:%U\n",
35               e - rw->entries,
36               format_hex_bytes, e->mask, e->rewrite_n_vectors * sizeof(u32x4),
37               format_hex_bytes, e->value, e->rewrite_n_vectors * sizeof(u32x4));
38   s = format (s, "      hits:%d skip_bytes:%d",
39                 e->hit_count, e->skip_n_vectors * sizeof(u32x4));
40   return s;
41 }
42
43 static u8 *format_l2_rw_config (u8 * s, va_list *args)
44 {
45   l2_rw_config_t *c = va_arg (*args, l2_rw_config_t *);
46   return format(s, "table-index:%d miss-index:%d",
47                 c->table_index,
48                 c->miss_index);
49 }
50
51 /* packet trace format function */
52 static u8 *format_l2_rw_trace (u8 * s, va_list * args)
53 {
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 *);
56   l2_rw_trace_t * t = va_arg (*args, l2_rw_trace_t *);
57   return format (s, "l2-rw: sw_if_index %d, table %d, entry %d",
58               t->sw_if_index, t->classify_table_index,
59               t->rewrite_entry_index);
60 }
61
62 always_inline l2_rw_config_t *l2_rw_get_config(u32 sw_if_index)
63 {
64   l2_rw_main_t *rw = &l2_rw_main;
65   if (PREDICT_FALSE(!clib_bitmap_get(rw->configs_bitmap, sw_if_index))) {
66     vec_validate(rw->configs, sw_if_index);
67     rw->configs[sw_if_index].table_index = ~0;
68     rw->configs[sw_if_index].miss_index = ~0;
69     rw->configs_bitmap = clib_bitmap_set(rw->configs_bitmap, sw_if_index, 1);
70   }
71   return &rw->configs[sw_if_index];
72 }
73
74 static_always_inline void l2_rw_rewrite(l2_rw_entry_t *rwe, u8 *h)
75 {
76   if (U32X4_ALIGNED(h)) {
77     u32x4 *d = ((u32x4 *) h) + rwe->skip_n_vectors;
78     switch(rwe->rewrite_n_vectors) {
79       case 5:
80         d[4] = (d[4] & ~rwe->mask[4]) | rwe->value[4];
81         /* FALLTHROUGH */
82       case 4:
83         d[3] = (d[3] & ~rwe->mask[3]) | rwe->value[3];
84         /* FALLTHROUGH */
85       case 3:
86         d[2] = (d[2] & ~rwe->mask[2]) | rwe->value[2];
87         /* FALLTHROUGH */
88       case 2:
89         d[1] = (d[1] & ~rwe->mask[1]) | rwe->value[1];
90         /* FALLTHROUGH */
91       case 1:
92         d[0] = (d[0] & ~rwe->mask[0]) | rwe->value[0];
93         break;
94       default:
95         abort();
96     }
97   } else {
98     u64 *d = ((u64 *) h) + rwe->skip_n_vectors * 2;
99     switch(rwe->rewrite_n_vectors) {
100       case 5:
101         d[8] = (d[8] & ~(((u64*)rwe->mask)[8])) | (((u64*)rwe->value)[8]);
102         d[9] = (d[9] & ~(((u64*)rwe->mask)[9])) | (((u64*)rwe->value)[9]);
103         /* FALLTHROUGH */
104       case 4:
105         d[6] = (d[6] & ~(((u64*)rwe->mask)[6])) | (((u64*)rwe->value)[6]);
106         d[7] = (d[7] & ~(((u64*)rwe->mask)[7])) | (((u64*)rwe->value)[7]);
107         /* FALLTHROUGH */
108       case 3:
109         d[4] = (d[4] & ~(((u64*)rwe->mask)[4])) | (((u64*)rwe->value)[4]);
110         d[5] = (d[5] & ~(((u64*)rwe->mask)[5])) | (((u64*)rwe->value)[5]);
111         /* FALLTHROUGH */
112       case 2:
113         d[2] = (d[2] & ~(((u64*)rwe->mask)[2])) | (((u64*)rwe->value)[2]);
114         d[3] = (d[3] & ~(((u64*)rwe->mask)[3])) | (((u64*)rwe->value)[3]);
115         /* FALLTHROUGH */
116       case 1:
117         d[0] = (d[0] & ~(((u64*)rwe->mask)[0])) | (((u64*)rwe->value)[0]);
118         d[1] = (d[1] & ~(((u64*)rwe->mask)[1])) | (((u64*)rwe->value)[1]);
119         break;
120       default:
121         abort();
122     }
123   }
124 }
125
126 static uword
127 l2_rw_node_fn(vlib_main_t * vm,
128               vlib_node_runtime_t * node,
129               vlib_frame_t * frame)
130 {
131   l2_rw_main_t *rw = &l2_rw_main;
132   u32 n_left_from, * from, * to_next, next_index;
133   //vlib_node_t *n = vlib_get_node(vm, node->node_index);
134   vnet_classify_main_t *vcm = &vnet_classify_main;
135   f64 now = vlib_time_now(vlib_get_main());
136   u32 prefetch_size = 0;
137
138   from = vlib_frame_vector_args (frame);
139   n_left_from = frame->n_vectors; /* number of packets to process */
140   next_index = node->cached_next_index;
141
142   while (n_left_from > 0)
143   {
144     u32 n_left_to_next;
145
146     /* get space to enqueue frame to graph node "next_index" */
147     vlib_get_next_frame (vm, node, next_index,
148                          to_next, n_left_to_next);
149
150     while (n_left_from >= 4 && n_left_to_next >= 2)
151     {
152       u32 bi0, next0, sw_if_index0, feature_bitmap0, rwe_index0;
153       u32 bi1, next1, sw_if_index1, feature_bitmap1, rwe_index1;
154       vlib_buffer_t *b0, *b1;
155       ethernet_header_t *h0, *h1;
156       l2_rw_config_t *config0, *config1;
157       u64 hash0, hash1;
158       vnet_classify_table_t *t0, *t1;
159       vnet_classify_entry_t *e0, *e1;
160       l2_rw_entry_t *rwe0, *rwe1;
161
162       {
163         vlib_buffer_t * p2, * p3;
164         p2 = vlib_get_buffer (vm, from[2]);
165         p3 = vlib_get_buffer (vm, from[3]);
166
167         vlib_prefetch_buffer_header (p2, LOAD);
168         vlib_prefetch_buffer_header (p3, LOAD);
169         CLIB_PREFETCH (p2->data, prefetch_size, LOAD);
170         CLIB_PREFETCH (p3->data, prefetch_size, LOAD);
171       }
172
173       bi0 = from[0];
174       bi1 = from[1];
175       to_next[0] = bi0;
176       to_next[1] = bi1;
177       from += 2;
178       to_next += 2;
179       n_left_from -= 2;
180       n_left_to_next -= 2;
181
182       b0 = vlib_get_buffer(vm, bi0);
183       b1 = vlib_get_buffer(vm, bi1);
184       h0 = vlib_buffer_get_current(b0);
185       h1 = vlib_buffer_get_current(b1);
186
187       sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
188       sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
189       config0 = l2_rw_get_config(sw_if_index0); //TODO: check sw_if_index0 value
190       config1 = l2_rw_get_config(sw_if_index1); //TODO: check sw_if_index0 value
191       t0 = pool_elt_at_index(vcm->tables, config0->table_index);
192       t1 = pool_elt_at_index(vcm->tables, config1->table_index);
193       prefetch_size = (t1->skip_n_vectors + t1->match_n_vectors)*sizeof(u32x4);
194
195       hash0 = vnet_classify_hash_packet(t0, (u8 *)h0);
196       hash1 = vnet_classify_hash_packet(t1, (u8 *)h1);
197       e0 = vnet_classify_find_entry(t0, (u8 *) h0, hash0, now);
198       e1 = vnet_classify_find_entry(t1, (u8 *) h1, hash1, now);
199
200       rwe_index0 = e0?e0->opaque_index:config0->miss_index;
201       rwe_index1 = e1?e1->opaque_index:config1->miss_index;
202
203       if (rwe_index0 != ~0) {
204         rwe0 = pool_elt_at_index(rw->entries, rwe_index0);
205         l2_rw_rewrite(rwe0, (u8 *)h0);
206       }
207       if (rwe_index1 != ~0) {
208         rwe1 = pool_elt_at_index(rw->entries, rwe_index1);
209         l2_rw_rewrite(rwe1, (u8 *)h1);
210       }
211
212       if (PREDICT_FALSE((b0->flags & VLIB_BUFFER_IS_TRACED))) {
213         l2_rw_trace_t *t =
214             vlib_add_trace (vm, node, b0, sizeof (*t));
215         t->sw_if_index = sw_if_index0;
216         t->classify_table_index = config0->table_index;
217         t->rewrite_entry_index = rwe_index0;
218       }
219
220       if (PREDICT_FALSE((b1->flags & VLIB_BUFFER_IS_TRACED))) {
221         l2_rw_trace_t *t =
222             vlib_add_trace (vm, node, b1, sizeof (*t));
223         t->sw_if_index = sw_if_index1;
224         t->classify_table_index = config1->table_index;
225         t->rewrite_entry_index = rwe_index1;
226       }
227
228       // Update feature bitmap and get next feature index
229       feature_bitmap0 = vnet_buffer(b0)->l2.feature_bitmap & ~L2INPUT_FEAT_RW;
230       feature_bitmap1 = vnet_buffer(b1)->l2.feature_bitmap & ~L2INPUT_FEAT_RW;
231       vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap0;
232       vnet_buffer(b1)->l2.feature_bitmap = feature_bitmap1;
233       next0 = feat_bitmap_get_next_node_index(rw->feat_next_node_index,
234                                               feature_bitmap0);
235       next1 = feat_bitmap_get_next_node_index(rw->feat_next_node_index,
236                                                     feature_bitmap1);
237
238       vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
239                                        to_next, n_left_to_next,
240                                        bi0, bi1, next0, next1);
241     }
242
243     while (n_left_from > 0 && n_left_to_next > 0)
244     {
245       u32 bi0, next0, sw_if_index0, feature_bitmap0, rwe_index0;
246       vlib_buffer_t *b0;
247       ethernet_header_t *h0;
248       l2_rw_config_t *config0;
249       u64 hash0;
250       vnet_classify_table_t *t0;
251       vnet_classify_entry_t *e0;
252       l2_rw_entry_t *rwe0;
253
254       bi0 = from[0];
255       to_next[0] = bi0;
256       from += 1;
257       to_next += 1;
258       n_left_from -= 1;
259       n_left_to_next -= 1;
260
261       b0 = vlib_get_buffer(vm, bi0);
262       h0 = vlib_buffer_get_current(b0);
263
264       sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
265       config0 = l2_rw_get_config(sw_if_index0); //TODO: check sw_if_index0 value
266       t0 = pool_elt_at_index(vcm->tables, config0->table_index);
267
268       hash0 = vnet_classify_hash_packet(t0, (u8 *)h0);
269       e0 = vnet_classify_find_entry(t0, (u8 *) h0, hash0, now);
270
271       rwe_index0 = e0?e0->opaque_index:config0->miss_index;
272
273       if (rwe_index0 != ~0) {
274         rwe0 = pool_elt_at_index(rw->entries, rwe_index0);
275         l2_rw_rewrite(rwe0, (u8 *)h0);
276       }
277
278       if (PREDICT_FALSE((b0->flags & VLIB_BUFFER_IS_TRACED))) {
279         l2_rw_trace_t *t =
280             vlib_add_trace (vm, node, b0, sizeof (*t));
281         t->sw_if_index = sw_if_index0;
282         t->classify_table_index = config0->table_index;
283         t->rewrite_entry_index = rwe_index0;
284       }
285
286       // Update feature bitmap and get next feature index
287       feature_bitmap0 = vnet_buffer(b0)->l2.feature_bitmap & ~L2INPUT_FEAT_RW;
288       vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap0;
289       next0 = feat_bitmap_get_next_node_index(rw->feat_next_node_index,
290                                               feature_bitmap0);
291
292       vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
293                                        to_next, n_left_to_next,
294                                        bi0, next0);
295     }
296     vlib_put_next_frame (vm, node, next_index, n_left_to_next);
297   }
298
299   return frame->n_vectors;
300 }
301
302 int l2_rw_mod_entry(u32 *index,
303                     u8 *mask, u8 *value, u32 len,
304                     u32 skip, u8 is_del)
305 {
306   l2_rw_main_t *rw = &l2_rw_main;
307   l2_rw_entry_t *e = 0;
308   if (*index != ~0) {
309     if (pool_is_free_index(rw->entries, *index)) {
310       return -1;
311     }
312     e = pool_elt_at_index(rw->entries, *index);
313   } else {
314     pool_get(rw->entries, e);
315     *index = e - rw->entries;
316   }
317
318   if (!e)
319     return -1;
320
321   if (is_del) {
322     pool_put(rw->entries, e);
323     return 0;
324   }
325
326   e->skip_n_vectors = skip / sizeof(u32x4);
327   skip -= e->skip_n_vectors * sizeof(u32x4);
328   e->rewrite_n_vectors = (skip + len - 1) / sizeof(u32x4) + 1;
329   vec_alloc_aligned(e->mask, e->rewrite_n_vectors, sizeof(u32x4));
330   memset(e->mask, 0, e->rewrite_n_vectors * sizeof(u32x4));
331   vec_alloc_aligned(e->value, e->rewrite_n_vectors, sizeof(u32x4));
332   memset(e->value, 0, e->rewrite_n_vectors * sizeof(u32x4));
333
334   memcpy(((u8 *)e->value) + skip, value, len);
335   memcpy(((u8 *)e->mask) + skip, mask, len);
336
337   int i;
338   for (i = 0; i < e->rewrite_n_vectors; i++) {
339     e->value[i] &= e->mask[i];
340   }
341
342   return 0;
343 }
344
345 static clib_error_t *
346 l2_rw_entry_cli_fn (vlib_main_t * vm,
347                             unformat_input_t * input,
348                             vlib_cli_command_t * cmd)
349 {
350   u32 index = ~0;
351   u8 *mask = 0;
352   u8 *value = 0;
353   u32 skip = 0;
354   u8 del = 0;
355
356   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
357   {
358     if (unformat (input, "index %d", &index))
359       ;
360     else if (unformat (input, "mask %U", unformat_hex_string, &mask))
361       ;
362     else if (unformat (input, "value %U", unformat_hex_string, &value))
363       ;
364     else if (unformat (input, "skip %d", &skip))
365       ;
366     else if (unformat (input, "del"))
367       del = 1;
368     else
369       break;
370   }
371
372   if (!mask || !value)
373     return clib_error_return(0, "Unspecified mask or value");
374
375   if (vec_len(mask) != vec_len(value))
376     return clib_error_return(0, "Mask and value lengths must be identical");
377
378   int ret;
379   if ((ret = l2_rw_mod_entry(&index, mask, value, vec_len(mask), skip, del)))
380     return clib_error_return(0, "Could not add entry");
381
382   return 0;
383 }
384
385 VLIB_CLI_COMMAND (l2_rw_entry_cli, static) = {
386   .path = "l2 rewrite entry",
387   .short_help =
388   "l2 rewrite entry [index <index>] [mask <hex-mask>] [value <hex-value>] [skip <n_bytes>] [del]",
389   .function = l2_rw_entry_cli_fn,
390 };
391
392 int l2_rw_interface_set_table(u32 sw_if_index,
393                               u32 table_index,
394                               u32 miss_index)
395 {
396   l2_rw_config_t *c = l2_rw_get_config(sw_if_index);
397   l2_rw_main_t *rw = &l2_rw_main;
398
399   c->table_index = table_index;
400   c->miss_index = miss_index;
401   u32 feature_bitmap = (table_index == ~0)?0:L2INPUT_FEAT_RW;
402
403   l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_RW, feature_bitmap);
404
405   if (c->table_index == ~0)
406     clib_bitmap_set(rw->configs_bitmap, sw_if_index, 0);
407
408   return 0;
409 }
410
411 static clib_error_t *
412 l2_rw_interface_cli_fn (vlib_main_t * vm,
413                             unformat_input_t * input,
414                             vlib_cli_command_t * cmd)
415 {
416   vnet_main_t * vnm = vnet_get_main();
417   u32 table_index = ~0;
418   u32 sw_if_index = ~0;
419   u32 miss_index = ~0;
420
421   if (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
422     unformat (input, "%U", unformat_vnet_sw_interface,
423                           vnm, &sw_if_index);
424   }
425
426   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
427   {
428     if (unformat (input, "table %d", &table_index))
429       ;
430     else if (unformat (input, "miss-index %d", &miss_index))
431       ;
432     else
433       break;
434   }
435
436   if (sw_if_index == ~0)
437     return clib_error_return(0, "You must specify an interface 'iface <interface>'",
438                                    format_unformat_error, input);
439   int ret;
440   if((ret = l2_rw_interface_set_table(sw_if_index, table_index, miss_index)))
441     return clib_error_return(0, "l2_rw_interface_set_table returned %d", ret);
442
443   return 0;
444 }
445
446 VLIB_CLI_COMMAND (l2_rw_interface_cli, static) = {
447   .path = "set interface l2 rewrite",
448   .short_help =
449   "set interface l2 rewrite <interface> [table <table index>] [miss-index <entry-index>]",
450   .function = l2_rw_interface_cli_fn,
451 };
452
453 static clib_error_t *
454 l2_rw_show_interfaces_cli_fn (vlib_main_t * vm,
455                         unformat_input_t * input,
456                         vlib_cli_command_t * cmd)
457 {
458   l2_rw_main_t *rw = &l2_rw_main;
459   if (clib_bitmap_count_set_bits(rw->configs_bitmap) == 0)
460       vlib_cli_output (vm, "No interface is currently using l2 rewrite\n");
461
462   uword i;
463   clib_bitmap_foreach(i, rw->configs_bitmap, {
464       vlib_cli_output (vm, "sw_if_index:%d %U\n", i, format_l2_rw_config, &rw->configs[i]);
465   });
466   return 0;
467 }
468
469 VLIB_CLI_COMMAND (l2_rw_show_interfaces_cli, static) = {
470   .path = "show l2 rewrite interfaces",
471   .short_help =
472   "show l2 rewrite interfaces",
473   .function = l2_rw_show_interfaces_cli_fn,
474 };
475
476 static clib_error_t *
477 l2_rw_show_entries_cli_fn (vlib_main_t * vm,
478                         unformat_input_t * input,
479                         vlib_cli_command_t * cmd)
480 {
481   l2_rw_main_t *rw = &l2_rw_main;
482   l2_rw_entry_t *e;
483   if (pool_elts(rw->entries) == 0)
484     vlib_cli_output (vm, "No entries\n");
485
486   pool_foreach(e, rw->entries, {
487     vlib_cli_output (vm, "%U\n", format_l2_rw_entry, e);
488   });
489   return 0;
490 }
491
492 VLIB_CLI_COMMAND (l2_rw_show_entries_cli, static) = {
493   .path = "show l2 rewrite entries",
494   .short_help =
495   "show l2 rewrite entries",
496   .function = l2_rw_show_entries_cli_fn,
497 };
498
499 int
500 l2_rw_enable_disable(u32 bridge_domain, u8 disable)
501 {
502   u32 mask = L2INPUT_FEAT_RW;
503   l2input_set_bridge_features(bridge_domain, mask,
504                               disable ? 0: mask);
505   return 0;
506 }
507
508 static clib_error_t *
509 l2_rw_set_cli_fn (vlib_main_t * vm,
510                         unformat_input_t * input,
511                         vlib_cli_command_t * cmd)
512 {
513   u32 bridge_domain;
514   u8 disable = 0;
515
516   if (unformat_check_input(input) == UNFORMAT_END_OF_INPUT ||
517       !unformat (input, "%d", &bridge_domain)) {
518     return clib_error_return(0, "You must specify a bridge domain");
519   }
520
521   if (unformat_check_input(input) != UNFORMAT_END_OF_INPUT &&
522       unformat (input, "disable")) {
523     disable = 1;
524   }
525
526   if (l2_rw_enable_disable(bridge_domain, disable))
527     return clib_error_return(0, "Could not enable or disable rewrite");
528
529   return 0;
530 }
531
532 VLIB_CLI_COMMAND (l2_rw_set_cli, static) = {
533   .path = "set bridge-domain rewrite",
534   .short_help =
535   "set bridge-domain rewrite <bridge-domain> [disable]",
536   .function = l2_rw_set_cli_fn,
537 };
538
539 static
540 clib_error_t *l2_rw_init (vlib_main_t *vm)
541 {
542   l2_rw_main_t *rw = &l2_rw_main;
543   rw->configs = 0;
544   rw->entries = 0;
545   clib_bitmap_alloc(rw->configs_bitmap, 1);
546   feat_bitmap_init_next_nodes(vm,
547                               l2_rw_node.index,
548                               L2INPUT_N_FEAT,
549                               l2input_get_feat_names(),
550                               rw->feat_next_node_index);
551   return 0;
552 }
553 VLIB_INIT_FUNCTION (l2_rw_init);
554
555 enum {
556   L2_RW_NEXT_DROP,
557   L2_RW_N_NEXT,
558 };
559
560 #define foreach_l2_rw_error               \
561 _(UNKNOWN, "Unknown error")
562
563 typedef enum {
564 #define _(sym,str) L2_RW_ERROR_##sym,
565   foreach_l2_rw_error
566 #undef _
567   L2_RW_N_ERROR,
568 } l2_rw_error_t;
569
570 static char * l2_rw_error_strings[] = {
571 #define _(sym,string) string,
572     foreach_l2_rw_error
573 #undef _
574 };
575
576 VLIB_REGISTER_NODE (l2_rw_node) = {
577   .function = l2_rw_node_fn,
578   .name = "l2-rw",
579   .vector_size = sizeof (u32),
580   .format_trace = format_l2_rw_trace,
581   .type = VLIB_NODE_TYPE_INTERNAL,
582   .n_errors = ARRAY_LEN(l2_rw_error_strings),
583   .error_strings = l2_rw_error_strings,
584   .runtime_data_bytes = 0,
585   .n_next_nodes = L2_RW_N_NEXT,
586   .next_nodes = { [L2_RW_NEXT_DROP]  = "error-drop"},
587 };
588