2 * SPDX-License-Identifier: Apache-2.0
3 * Copyright(c) 2021 Cisco Systems, Inc.
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>
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
21 typedef struct _gso_test_data
24 const char *description;
30 struct _gso_test_data *next;
44 gso_test_data_t *gso_test_data;
47 gso_test_main_t gso_test_main;
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) \
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; \
57 __VA_ARGS__ gso_test_data_t __gso_test_data_##x
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
68 GSO_TEST_REGISTER_DATA (gso_ipv4_tcp, static) = {
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),
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
89 GSO_TEST_REGISTER_DATA (gso_ipv6_tcp, static) = {
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),
100 * this does not support tunnel packets
103 set_hdr_offsets (vlib_buffer_t *b0, u8 is_l2)
105 u16 ethertype = 0, l2hdr_sz = 0;
106 vnet_buffer_oflags_t oflags = 0;
111 switch (b0->data[0] & 0xf0)
114 ethertype = ETHERNET_TYPE_IP4;
117 ethertype = ETHERNET_TYPE_IP6;
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);
127 if (ethernet_frame_is_tagged (ethertype))
129 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
131 ethertype = clib_net_to_host_u16 (vlan->type);
132 l2hdr_sz += sizeof (*vlan);
133 if (ethertype == ETHERNET_TYPE_VLAN)
136 ethertype = clib_net_to_host_u16 (vlan->type);
137 l2hdr_sz += sizeof (*vlan);
142 vnet_buffer (b0)->l2_hdr_offset = 0;
143 vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz;
145 if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
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);
155 else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
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);
165 if (l4_proto == IP_PROTOCOL_TCP)
167 oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
169 else if (l4_proto == IP_PROTOCOL_UDP)
171 oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
174 vnet_buffer_offload_flags_set (b0, oflags);
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)
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;
188 for (i = 0; i < n_buffers; i++)
190 u64 seed = clib_cpu_time_now ();
191 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
194 (packet_size > buffer_size) ? (packet_size - buffer_size) : 0;
196 clib_memcpy_fast (b->data, data, data_size);
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;
205 vlib_buffer_t *pb = b;
207 n_bufs = ((remaining_data + buffer_size - 1) / buffer_size);
212 vec_validate (buffers, n_bufs - 1);
213 n_alloc = vlib_buffer_alloc (vm, buffers, n_bufs);
214 if (n_alloc < n_bufs)
216 vlib_buffer_free (vm, buffers, n_alloc);
218 vm, "vlib buffer alloc failed at %u requested %u actual %u", i,
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;
235 len += fill_data_size;
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;
244 b->total_length_not_including_first_buffer = len;
245 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
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)
254 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
257 if (PREDICT_TRUE (b->flags & VNET_BUFFER_F_GSO))
259 n_tx_bytes = gso_segment_buffer_inline (vm, ptd, b, is_l2);
265 static clib_error_t *
266 test_gso_perf (vlib_main_t *vm, gso_test_main_t *gtm)
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);
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;
279 if (gtm->buffer_size > buffer_size)
280 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
282 if (gtm->packet_size > packet_size)
283 return clib_error_return (0, "gso packet size must be <= %u", packet_size);
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);
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;
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);
302 "GSO Segmentation: packet-size %u gso-size %u buffer-size "
303 "%u n_buffers %u rounds %u "
305 packet_size, gso_size, buffer_size, n_buffers, rounds,
307 vlib_cli_output (vm, " cpu-freq %.2f GHz",
308 (f64) vm->clib_time.clocks_per_second * 1e-9);
310 while (gso_test_data)
313 u32 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
314 if (n_alloc != n_buffers)
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);
321 n_filled = fill_buffers (vm, buffer_indices, gso_test_data, n_buffers,
322 buffer_size, packet_size, gso_size);
324 u8 is_l2 = gso_test_data->is_l2;
326 for (k = 0; k < warmup_rounds; k++)
328 for (j = 0; j < n_filled; j++)
329 gso_segment_buffer_test (vm, buffer_indices[j], &ptd[j], is_l2);
331 for (j = 0; j < n_filled; j++)
333 vlib_buffer_free (vm, ptd[j].split_buffers,
334 vec_len (ptd[j].split_buffers));
335 vec_free (ptd[j].split_buffers);
339 for (i = 0; i < 10; i++)
341 for (k = 0; k < rounds; k++)
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],
348 t1 = clib_cpu_time_now ();
350 for (j = 0; j < n_filled; j++)
352 vlib_buffer_free (vm, ptd[j].split_buffers,
353 vec_len (ptd[j].split_buffers));
354 vec_free (ptd[j].split_buffers);
360 vm, "===========================================================");
361 vlib_cli_output (vm, " Test: %s", gso_test_data->description);
363 vm, "===========================================================");
364 for (i = 0; i < 10; i++)
367 f64 tpp1 = (f64) (t2[i]) / (n_filled * rounds);
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;
376 vm, "%-2u: %.03f ticks/packet, %.02f Kpps, %.02f Gbps\n", i + 1,
380 vlib_buffer_free (vm, buffer_indices, n_alloc);
381 clib_memset (t2, 0, sizeof (t2));
382 gso_test_data = gso_test_data->next;
388 vec_free (buffer_indices);
392 static clib_error_t *
393 test_gso_command_fn (vlib_main_t *vm, unformat_input_t *input,
394 vlib_cli_command_t *cmd)
396 gso_test_main_t *gtm = &gso_test_main;
397 clib_error_t *err = 0;
398 f64 end, start, total_time;
400 gtm->gso_size = DEFAULT_GSO_SEGMENT_SIZE;
401 gtm->warmup_rounds = 0;
404 gtm->buffer_size = 0;
405 gtm->packet_size = 0;
407 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
409 if (unformat (input, "verbose"))
411 else if (unformat (input, "detail"))
413 else if (unformat (input, "buffers %u", >m->n_buffers))
415 else if (unformat (input, "buffer-size %u", >m->buffer_size))
417 else if (unformat (input, "packet-size %u", >m->packet_size))
419 else if (unformat (input, "gso-size %u", >m->gso_size))
421 else if (unformat (input, "rounds %u", >m->rounds))
423 else if (unformat (input, "warmup-rounds %u", >m->warmup_rounds))
427 return clib_error_return (0, "unknown input '%U'",
428 format_unformat_error, input);
432 start = clib_cpu_time_now ();
433 err = test_gso_perf (vm, gtm);
434 end = clib_cpu_time_now ();
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);
442 VLIB_CLI_COMMAND (test_gso_command, static) = {
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,
450 static clib_error_t *
451 gso_test_init (vlib_main_t *vm)
456 VLIB_INIT_FUNCTION (gso_test_init);