vlib: clean up r2 plugin registration relocator
[vpp.git] / src / plugins / unittest / gso_test.c
1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  * Copyright(c) 2021 Cisco Systems, Inc.
4  */
5
6 #include <vlib/vlib.h>
7 #include <vppinfra/time.h>
8 #include <vppinfra/cache.h>
9 #include <vppinfra/error.h>
10 #include <vnet/ethernet/ethernet.h>
11 #include <vnet/ip/ip.h>
12 #include <vnet/gso/gso.h>
13 #include <vnet/gso/hdr_offset_parser.h>
14 #include <vnet/tcp/tcp_packet.h>
15
16 #define MAX_GSO_PACKET_SIZE      (TCP_MAX_GSO_SZ - 1)
17 #define MIN_GSO_SEGMENT_SIZE     128
18 #define MAX_GSO_SEGMENT_SIZE     2048
19 #define DEFAULT_GSO_SEGMENT_SIZE 1448
20
21 typedef struct _gso_test_data
22 {
23   const char *name;
24   const char *description;
25   u8 *data;
26   u32 data_size;
27   u32 l4_hdr_len;
28   u8 is_l2;
29   u8 is_ip6;
30   struct _gso_test_data *next;
31 } gso_test_data_t;
32
33 typedef struct
34 {
35   int verbose;
36
37   char *gso_name;
38   u32 warmup_rounds;
39   u32 rounds;
40   u32 n_buffers;
41   u32 buffer_size;
42   u32 packet_size;
43   u32 gso_size;
44   gso_test_data_t *gso_test_data;
45 } gso_test_main_t;
46
47 gso_test_main_t gso_test_main;
48
49 #define GSO_TEST_REGISTER_DATA(x, ...)                                        \
50   __VA_ARGS__ gso_test_data_t __gso_test_data_##x;                            \
51   static void __clib_constructor __gso_test_data_fn_##x (void)                \
52   {                                                                           \
53     gso_test_main_t *gtm = &gso_test_main;                                    \
54     __gso_test_data_##x.next = gtm->gso_test_data;                            \
55     gtm->gso_test_data = &__gso_test_data_##x;                                \
56   }                                                                           \
57   __VA_ARGS__ gso_test_data_t __gso_test_data_##x
58
59 // ipv4
60 u8 gso_ipv4_tcp_data[64] = {
61   0x02, 0xfe, 0x39, 0xe5, 0x09, 0x8f, 0x02, 0xfe, 0x2d, 0x18, 0x63, 0x18, 0x08,
62   0x00, 0x45, 0x00, 0x05, 0xdc, 0xdb, 0x42, 0x40, 0x00, 0x40, 0x06, 0xc4, 0x85,
63   0xc0, 0xa8, 0x0a, 0x02, 0xc0, 0xa8, 0x0a, 0x01, 0xd8, 0xde, 0x14, 0x51, 0x34,
64   0x93, 0xa8, 0x1b, 0x7b, 0xef, 0x2e, 0x7e, 0x80, 0x10, 0x00, 0xe5, 0xc7, 0x03,
65   0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xce, 0xaa, 0x00, 0x2f, 0xf2, 0xc3
66 };
67
68 GSO_TEST_REGISTER_DATA (gso_ipv4_tcp, static) = {
69   .name = "ipv4-tcp",
70   .description = "IPv4 TCP",
71   .data = gso_ipv4_tcp_data,
72   .data_size = sizeof (gso_ipv4_tcp_data),
73   .l4_hdr_len = sizeof (tcp_header_t),
74   .is_l2 = 1,
75   .is_ip6 = 0,
76 };
77
78 // ipv6
79 u8 gso_ipv6_tcp_data[] = {
80   0x02, 0xfe, 0x39, 0xe5, 0x09, 0x8f, 0x02, 0xfe, 0x2d, 0x18, 0x63, 0x18,
81   0x08, 0x00, 0x60, 0x0d, 0xf4, 0x97, 0x00, 0x40, 0x06, 0x40, 0xfd, 0x01,
82   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83   0x10, 0x00, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84   0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0xd8, 0xde, 0x14, 0x51, 0x34, 0x93,
85   0xa8, 0x1b, 0x7b, 0xef, 0x2e, 0x7e, 0x80, 0x10, 0x00, 0xe5, 0xc7, 0x03,
86   0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xce, 0xaa, 0x00, 0x2f, 0xf2, 0xc3
87 };
88
89 GSO_TEST_REGISTER_DATA (gso_ipv6_tcp, static) = {
90   .name = "ipv6-tcp",
91   .description = "IPv6 TCP",
92   .data = gso_ipv6_tcp_data,
93   .data_size = sizeof (gso_ipv6_tcp_data),
94   .l4_hdr_len = sizeof (tcp_header_t),
95   .is_l2 = 1,
96   .is_ip6 = 1,
97 };
98
99 /*
100  * this does not support tunnel packets
101  */
102 static void
103 set_hdr_offsets (vlib_buffer_t *b0, u8 is_l2)
104 {
105   u16 ethertype = 0, l2hdr_sz = 0;
106   vnet_buffer_oflags_t oflags = 0;
107   u8 l4_proto = 0;
108
109   if (!is_l2)
110     {
111       switch (b0->data[0] & 0xf0)
112         {
113         case 0x40:
114           ethertype = ETHERNET_TYPE_IP4;
115           break;
116         case 0x60:
117           ethertype = ETHERNET_TYPE_IP6;
118           break;
119         }
120     }
121   else
122     {
123       ethernet_header_t *eh = (ethernet_header_t *) b0->data;
124       ethertype = clib_net_to_host_u16 (eh->type);
125       l2hdr_sz = sizeof (ethernet_header_t);
126
127       if (ethernet_frame_is_tagged (ethertype))
128         {
129           ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
130
131           ethertype = clib_net_to_host_u16 (vlan->type);
132           l2hdr_sz += sizeof (*vlan);
133           if (ethertype == ETHERNET_TYPE_VLAN)
134             {
135               vlan++;
136               ethertype = clib_net_to_host_u16 (vlan->type);
137               l2hdr_sz += sizeof (*vlan);
138             }
139         }
140     }
141
142   vnet_buffer (b0)->l2_hdr_offset = 0;
143   vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz;
144
145   if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
146     {
147       ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l2hdr_sz);
148       vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
149       l4_proto = ip4->protocol;
150       oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM;
151       b0->flags |= (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
152                     VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
153                     VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
154     }
155   else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
156     {
157       ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l2hdr_sz);
158       vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
159       /* FIXME IPv6 EH traversal */
160       l4_proto = ip6->protocol;
161       b0->flags |= (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
162                     VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
163                     VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
164     }
165   if (l4_proto == IP_PROTOCOL_TCP)
166     {
167       oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
168     }
169   else if (l4_proto == IP_PROTOCOL_UDP)
170     {
171       oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
172     }
173   if (oflags)
174     vnet_buffer_offload_flags_set (b0, oflags);
175 }
176
177 static u32
178 fill_buffers (vlib_main_t *vm, u32 *buffer_indices,
179               gso_test_data_t *gso_test_data, u32 n_buffers, u32 buffer_size,
180               u32 packet_size, u32 gso_size)
181 {
182   u32 i;
183   u8 *data = gso_test_data->data;
184   u32 data_size = gso_test_data->data_size;
185   u32 l4_hdr_len = gso_test_data->l4_hdr_len;
186   u8 is_l2 = gso_test_data->is_l2;
187
188   for (i = 0; i < n_buffers; i++)
189     {
190       u64 seed = clib_cpu_time_now ();
191       vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
192       u32 len = 0;
193       u32 remaining_data =
194         (packet_size > buffer_size) ? (packet_size - buffer_size) : 0;
195
196       clib_memcpy_fast (b->data, data, data_size);
197       b->current_data = 0;
198
199       for (u32 j = data_size; j < buffer_size; j += 8)
200         *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
201       b->current_length = buffer_size;
202
203       if (remaining_data)
204         {
205           vlib_buffer_t *pb = b;
206           u32 n_alloc,
207             n_bufs = ((remaining_data + buffer_size - 1) / buffer_size);
208           u32 *buffers = 0;
209           u32 fill_data_size;
210           u32 k = 0;
211
212           vec_validate (buffers, n_bufs - 1);
213           n_alloc = vlib_buffer_alloc (vm, buffers, n_bufs);
214           if (n_alloc < n_bufs)
215             {
216               vlib_buffer_free (vm, buffers, n_alloc);
217               vlib_cli_output (
218                 vm, "vlib buffer alloc failed at %u requested %u actual %u", i,
219                 n_bufs, n_alloc);
220               return i;
221             }
222
223           do
224             {
225               pb->next_buffer = buffers[k];
226               pb->flags |= VLIB_BUFFER_NEXT_PRESENT;
227               pb = vlib_get_buffer (vm, buffers[k]);
228               pb->current_data = 0;
229               fill_data_size = clib_min (buffer_size, remaining_data);
230               remaining_data -= fill_data_size;
231               for (u32 l = 0; l < fill_data_size; l += 8)
232                 *(u64 *) (pb->data + l) = 1 + random_u64 (&seed);
233               pb->current_length = fill_data_size;
234               k++;
235               len += fill_data_size;
236             }
237           while (k < n_bufs);
238
239           set_hdr_offsets (b, is_l2);
240           b->flags |= VNET_BUFFER_F_GSO;
241           vnet_buffer2 (b)->gso_size = gso_size;
242           vnet_buffer2 (b)->gso_l4_hdr_sz = l4_hdr_len;
243         }
244       b->total_length_not_including_first_buffer = len;
245       b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
246     }
247   return i;
248 }
249
250 static_always_inline u32
251 gso_segment_buffer_test (vlib_main_t *vm, u32 bi,
252                          vnet_interface_per_thread_data_t *ptd, u8 is_l2)
253 {
254   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
255   u32 n_tx_bytes = 0;
256
257   if (PREDICT_TRUE (b->flags & VNET_BUFFER_F_GSO))
258     {
259       n_tx_bytes = gso_segment_buffer_inline (vm, ptd, b, is_l2);
260     }
261
262   return n_tx_bytes;
263 }
264
265 static clib_error_t *
266 test_gso_perf (vlib_main_t *vm, gso_test_main_t *gtm)
267 {
268   clib_error_t *err = 0;
269   vnet_interface_per_thread_data_t *ptd = 0;
270   u32 packet_size = MAX_GSO_PACKET_SIZE;
271   u32 buffer_size = vlib_buffer_get_default_data_size (vm);
272   u32 gso_size;
273   u32 n_buffers, warmup_rounds, rounds;
274   u32 *buffer_indices = 0;
275   u64 t0, t1, t2[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
276   gso_test_data_t *gso_test_data = gtm->gso_test_data;
277   int i, j, k;
278
279   if (gtm->buffer_size > buffer_size)
280     return clib_error_return (0, "buffer size must be <= %u", buffer_size);
281
282   if (gtm->packet_size > packet_size)
283     return clib_error_return (0, "gso packet size must be <= %u", packet_size);
284
285   if ((gtm->gso_size > MAX_GSO_SEGMENT_SIZE) ||
286       (gtm->gso_size < MIN_GSO_SEGMENT_SIZE))
287     return clib_error_return (
288       0, "gso segment size must be in between %u >= and <= %u",
289       MIN_GSO_SEGMENT_SIZE, MAX_GSO_SEGMENT_SIZE);
290
291   rounds = gtm->rounds ? gtm->rounds : 256;
292   n_buffers = gtm->n_buffers ? gtm->n_buffers : 256;
293   warmup_rounds = gtm->warmup_rounds ? gtm->warmup_rounds : 256;
294   buffer_size = gtm->buffer_size ? gtm->buffer_size : buffer_size;
295   gso_size = gtm->gso_size;
296   packet_size = gtm->packet_size ? gtm->packet_size : packet_size;
297
298   vec_validate_aligned (ptd, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
299   vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
300
301   vlib_cli_output (vm,
302                    "GSO Segmentation: packet-size %u gso-size %u buffer-size "
303                    "%u n_buffers %u rounds %u "
304                    "warmup-rounds %u",
305                    packet_size, gso_size, buffer_size, n_buffers, rounds,
306                    warmup_rounds);
307   vlib_cli_output (vm, "   cpu-freq %.2f GHz",
308                    (f64) vm->clib_time.clocks_per_second * 1e-9);
309
310   while (gso_test_data)
311     {
312       u32 n_filled = 0;
313       u32 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
314       if (n_alloc != n_buffers)
315         {
316           vlib_cli_output (vm, " Test: %s FAILED", gso_test_data->description);
317           err = clib_error_return (0, "buffer alloc failure");
318           vlib_buffer_free (vm, buffer_indices, n_alloc);
319           goto done;
320         }
321       n_filled = fill_buffers (vm, buffer_indices, gso_test_data, n_buffers,
322                                buffer_size, packet_size, gso_size);
323
324       u8 is_l2 = gso_test_data->is_l2;
325
326       for (k = 0; k < warmup_rounds; k++)
327         {
328           for (j = 0; j < n_filled; j++)
329             gso_segment_buffer_test (vm, buffer_indices[j], &ptd[j], is_l2);
330
331           for (j = 0; j < n_filled; j++)
332             {
333               vlib_buffer_free (vm, ptd[j].split_buffers,
334                                 vec_len (ptd[j].split_buffers));
335               vec_free (ptd[j].split_buffers);
336             }
337         }
338
339       for (i = 0; i < 10; i++)
340         {
341           for (k = 0; k < rounds; k++)
342             {
343               t0 = clib_cpu_time_now ();
344               for (j = 0; j < n_filled; j++)
345                 gso_segment_buffer_test (vm, buffer_indices[j], &ptd[j],
346                                          is_l2);
347
348               t1 = clib_cpu_time_now ();
349               t2[i] += (t1 - t0);
350               for (j = 0; j < n_filled; j++)
351                 {
352                   vlib_buffer_free (vm, ptd[j].split_buffers,
353                                     vec_len (ptd[j].split_buffers));
354                   vec_free (ptd[j].split_buffers);
355                 }
356             }
357         }
358
359       vlib_cli_output (
360         vm, "===========================================================");
361       vlib_cli_output (vm, " Test: %s", gso_test_data->description);
362       vlib_cli_output (
363         vm, "===========================================================");
364       for (i = 0; i < 10; i++)
365         {
366           // ticks per packet
367           f64 tpp1 = (f64) (t2[i]) / (n_filled * rounds);
368           // ticks per Byte
369           f64 tpB1 = (f64) (t2[i]) / (n_filled * rounds * packet_size);
370           // Packets per second
371           f64 Kpps1 = vm->clib_time.clocks_per_second * 1e-3 / tpp1;
372           // Throughput Giga-bits per second
373           f64 Gbps1 = vm->clib_time.clocks_per_second * 8 * 1e-9 / tpB1;
374
375           vlib_cli_output (
376             vm, "%-2u: %.03f ticks/packet, %.02f Kpps, %.02f Gbps\n", i + 1,
377             tpp1, Kpps1, Gbps1);
378         }
379       if (n_alloc)
380         vlib_buffer_free (vm, buffer_indices, n_alloc);
381       clib_memset (t2, 0, sizeof (t2));
382       gso_test_data = gso_test_data->next;
383     }
384
385 done:
386
387   vec_free (ptd);
388   vec_free (buffer_indices);
389   return err;
390 }
391
392 static clib_error_t *
393 test_gso_command_fn (vlib_main_t *vm, unformat_input_t *input,
394                      vlib_cli_command_t *cmd)
395 {
396   gso_test_main_t *gtm = &gso_test_main;
397   clib_error_t *err = 0;
398   f64 end, start, total_time;
399
400   gtm->gso_size = DEFAULT_GSO_SEGMENT_SIZE;
401   gtm->warmup_rounds = 0;
402   gtm->rounds = 0;
403   gtm->n_buffers = 0;
404   gtm->buffer_size = 0;
405   gtm->packet_size = 0;
406
407   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
408     {
409       if (unformat (input, "verbose"))
410         gtm->verbose = 1;
411       else if (unformat (input, "detail"))
412         gtm->verbose = 2;
413       else if (unformat (input, "buffers %u", &gtm->n_buffers))
414         ;
415       else if (unformat (input, "buffer-size %u", &gtm->buffer_size))
416         ;
417       else if (unformat (input, "packet-size %u", &gtm->packet_size))
418         ;
419       else if (unformat (input, "gso-size %u", &gtm->gso_size))
420         ;
421       else if (unformat (input, "rounds %u", &gtm->rounds))
422         ;
423       else if (unformat (input, "warmup-rounds %u", &gtm->warmup_rounds))
424         ;
425       else
426         {
427           return clib_error_return (0, "unknown input '%U'",
428                                     format_unformat_error, input);
429         }
430     }
431
432   start = clib_cpu_time_now ();
433   err = test_gso_perf (vm, gtm);
434   end = clib_cpu_time_now ();
435
436   total_time = (f64) (end - start) / vm->clib_time.clocks_per_second;
437   vlib_cli_output (vm, "Total Time Test Took %.02f seconds", total_time);
438
439   return err;
440 }
441
442 VLIB_CLI_COMMAND (test_gso_command, static) = {
443   .path = "test gso",
444   .short_help = "test gso [buffers <n>] [buffer-size <size>] [packet-size "
445                 "<size>] [gso-size <size>] [rounds <n>] "
446                 "[warmup-rounds <n>]",
447   .function = test_gso_command_fn,
448 };
449
450 static clib_error_t *
451 gso_test_init (vlib_main_t *vm)
452 {
453   return (0);
454 }
455
456 VLIB_INIT_FUNCTION (gso_test_init);