test: fix autotest list
[dpdk.git] / test / test / packet_burst_generator.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <rte_byteorder.h>
6 #include <rte_mbuf.h>
7
8 #include "packet_burst_generator.h"
9
10 #define UDP_SRC_PORT 1024
11 #define UDP_DST_PORT 1024
12
13
14 #define IP_DEFTTL  64   /* from RFC 1340. */
15 #define IP_VERSION 0x40
16 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
17 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
18
19 static void
20 copy_buf_to_pkt_segs(void *buf, unsigned len, struct rte_mbuf *pkt,
21                 unsigned offset)
22 {
23         struct rte_mbuf *seg;
24         void *seg_buf;
25         unsigned copy_len;
26
27         seg = pkt;
28         while (offset >= seg->data_len) {
29                 offset -= seg->data_len;
30                 seg = seg->next;
31         }
32         copy_len = seg->data_len - offset;
33         seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
34         while (len > copy_len) {
35                 rte_memcpy(seg_buf, buf, (size_t) copy_len);
36                 len -= copy_len;
37                 buf = ((char *) buf + copy_len);
38                 seg = seg->next;
39                 seg_buf = rte_pktmbuf_mtod(seg, void *);
40         }
41         rte_memcpy(seg_buf, buf, (size_t) len);
42 }
43
44 static inline void
45 copy_buf_to_pkt(void *buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
46 {
47         if (offset + len <= pkt->data_len) {
48                 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset), buf,
49                            (size_t) len);
50                 return;
51         }
52         copy_buf_to_pkt_segs(buf, len, pkt, offset);
53 }
54
55 void
56 initialize_eth_header(struct ether_hdr *eth_hdr, struct ether_addr *src_mac,
57                 struct ether_addr *dst_mac, uint16_t ether_type,
58                 uint8_t vlan_enabled, uint16_t van_id)
59 {
60         ether_addr_copy(dst_mac, &eth_hdr->d_addr);
61         ether_addr_copy(src_mac, &eth_hdr->s_addr);
62
63         if (vlan_enabled) {
64                 struct vlan_hdr *vhdr = (struct vlan_hdr *)((uint8_t *)eth_hdr +
65                                 sizeof(struct ether_hdr));
66
67                 eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
68
69                 vhdr->eth_proto =  rte_cpu_to_be_16(ether_type);
70                 vhdr->vlan_tci = van_id;
71         } else {
72                 eth_hdr->ether_type = rte_cpu_to_be_16(ether_type);
73         }
74 }
75
76 void
77 initialize_arp_header(struct arp_hdr *arp_hdr, struct ether_addr *src_mac,
78                 struct ether_addr *dst_mac, uint32_t src_ip, uint32_t dst_ip,
79                 uint32_t opcode)
80 {
81         arp_hdr->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
82         arp_hdr->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
83         arp_hdr->arp_hln = ETHER_ADDR_LEN;
84         arp_hdr->arp_pln = sizeof(uint32_t);
85         arp_hdr->arp_op = rte_cpu_to_be_16(opcode);
86         ether_addr_copy(src_mac, &arp_hdr->arp_data.arp_sha);
87         arp_hdr->arp_data.arp_sip = src_ip;
88         ether_addr_copy(dst_mac, &arp_hdr->arp_data.arp_tha);
89         arp_hdr->arp_data.arp_tip = dst_ip;
90 }
91
92 uint16_t
93 initialize_udp_header(struct udp_hdr *udp_hdr, uint16_t src_port,
94                 uint16_t dst_port, uint16_t pkt_data_len)
95 {
96         uint16_t pkt_len;
97
98         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
99
100         udp_hdr->src_port = rte_cpu_to_be_16(src_port);
101         udp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
102         udp_hdr->dgram_len = rte_cpu_to_be_16(pkt_len);
103         udp_hdr->dgram_cksum = 0; /* No UDP checksum. */
104
105         return pkt_len;
106 }
107
108 uint16_t
109 initialize_tcp_header(struct tcp_hdr *tcp_hdr, uint16_t src_port,
110                 uint16_t dst_port, uint16_t pkt_data_len)
111 {
112         uint16_t pkt_len;
113
114         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct tcp_hdr));
115
116         memset(tcp_hdr, 0, sizeof(struct tcp_hdr));
117         tcp_hdr->src_port = rte_cpu_to_be_16(src_port);
118         tcp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
119
120         return pkt_len;
121 }
122
123 uint16_t
124 initialize_sctp_header(struct sctp_hdr *sctp_hdr, uint16_t src_port,
125                 uint16_t dst_port, uint16_t pkt_data_len)
126 {
127         uint16_t pkt_len;
128
129         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
130
131         sctp_hdr->src_port = rte_cpu_to_be_16(src_port);
132         sctp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
133         sctp_hdr->tag = 0;
134         sctp_hdr->cksum = 0; /* No SCTP checksum. */
135
136         return pkt_len;
137 }
138
139 uint16_t
140 initialize_ipv6_header(struct ipv6_hdr *ip_hdr, uint8_t *src_addr,
141                 uint8_t *dst_addr, uint16_t pkt_data_len)
142 {
143         ip_hdr->vtc_flow = 0;
144         ip_hdr->payload_len = pkt_data_len;
145         ip_hdr->proto = IPPROTO_UDP;
146         ip_hdr->hop_limits = IP_DEFTTL;
147
148         rte_memcpy(ip_hdr->src_addr, src_addr, sizeof(ip_hdr->src_addr));
149         rte_memcpy(ip_hdr->dst_addr, dst_addr, sizeof(ip_hdr->dst_addr));
150
151         return (uint16_t) (pkt_data_len + sizeof(struct ipv6_hdr));
152 }
153
154 uint16_t
155 initialize_ipv4_header(struct ipv4_hdr *ip_hdr, uint32_t src_addr,
156                 uint32_t dst_addr, uint16_t pkt_data_len)
157 {
158         uint16_t pkt_len;
159         unaligned_uint16_t *ptr16;
160         uint32_t ip_cksum;
161
162         /*
163          * Initialize IP header.
164          */
165         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct ipv4_hdr));
166
167         ip_hdr->version_ihl   = IP_VHL_DEF;
168         ip_hdr->type_of_service   = 0;
169         ip_hdr->fragment_offset = 0;
170         ip_hdr->time_to_live   = IP_DEFTTL;
171         ip_hdr->next_proto_id = IPPROTO_UDP;
172         ip_hdr->packet_id = 0;
173         ip_hdr->total_length   = rte_cpu_to_be_16(pkt_len);
174         ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
175         ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
176
177         /*
178          * Compute IP header checksum.
179          */
180         ptr16 = (unaligned_uint16_t *)ip_hdr;
181         ip_cksum = 0;
182         ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
183         ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
184         ip_cksum += ptr16[4];
185         ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
186         ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
187
188         /*
189          * Reduce 32 bit checksum to 16 bits and complement it.
190          */
191         ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
192                 (ip_cksum & 0x0000FFFF);
193         ip_cksum %= 65536;
194         ip_cksum = (~ip_cksum) & 0x0000FFFF;
195         if (ip_cksum == 0)
196                 ip_cksum = 0xFFFF;
197         ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
198
199         return pkt_len;
200 }
201
202 uint16_t
203 initialize_ipv4_header_proto(struct ipv4_hdr *ip_hdr, uint32_t src_addr,
204                 uint32_t dst_addr, uint16_t pkt_data_len, uint8_t proto)
205 {
206         uint16_t pkt_len;
207         unaligned_uint16_t *ptr16;
208         uint32_t ip_cksum;
209
210         /*
211          * Initialize IP header.
212          */
213         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct ipv4_hdr));
214
215         ip_hdr->version_ihl   = IP_VHL_DEF;
216         ip_hdr->type_of_service   = 0;
217         ip_hdr->fragment_offset = 0;
218         ip_hdr->time_to_live   = IP_DEFTTL;
219         ip_hdr->next_proto_id = proto;
220         ip_hdr->packet_id = 0;
221         ip_hdr->total_length   = rte_cpu_to_be_16(pkt_len);
222         ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
223         ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
224
225         /*
226          * Compute IP header checksum.
227          */
228         ptr16 = (unaligned_uint16_t *)ip_hdr;
229         ip_cksum = 0;
230         ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
231         ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
232         ip_cksum += ptr16[4];
233         ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
234         ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
235
236         /*
237          * Reduce 32 bit checksum to 16 bits and complement it.
238          */
239         ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
240                 (ip_cksum & 0x0000FFFF);
241         ip_cksum %= 65536;
242         ip_cksum = (~ip_cksum) & 0x0000FFFF;
243         if (ip_cksum == 0)
244                 ip_cksum = 0xFFFF;
245         ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
246
247         return pkt_len;
248 }
249
250 /*
251  * The maximum number of segments per packet is used when creating
252  * scattered transmit packets composed of a list of mbufs.
253  */
254 #define RTE_MAX_SEGS_PER_PKT 255 /**< pkt.nb_segs is a 8-bit unsigned char. */
255
256
257 int
258 generate_packet_burst(struct rte_mempool *mp, struct rte_mbuf **pkts_burst,
259                 struct ether_hdr *eth_hdr, uint8_t vlan_enabled, void *ip_hdr,
260                 uint8_t ipv4, struct udp_hdr *udp_hdr, int nb_pkt_per_burst,
261                 uint8_t pkt_len, uint8_t nb_pkt_segs)
262 {
263         int i, nb_pkt = 0;
264         size_t eth_hdr_size;
265
266         struct rte_mbuf *pkt_seg;
267         struct rte_mbuf *pkt;
268
269         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
270                 pkt = rte_pktmbuf_alloc(mp);
271                 if (pkt == NULL) {
272 nomore_mbuf:
273                         if (nb_pkt == 0)
274                                 return -1;
275                         break;
276                 }
277
278                 pkt->data_len = pkt_len;
279                 pkt_seg = pkt;
280                 for (i = 1; i < nb_pkt_segs; i++) {
281                         pkt_seg->next = rte_pktmbuf_alloc(mp);
282                         if (pkt_seg->next == NULL) {
283                                 pkt->nb_segs = i;
284                                 rte_pktmbuf_free(pkt);
285                                 goto nomore_mbuf;
286                         }
287                         pkt_seg = pkt_seg->next;
288                         pkt_seg->data_len = pkt_len;
289                 }
290                 pkt_seg->next = NULL; /* Last segment of packet. */
291
292                 /*
293                  * Copy headers in first packet segment(s).
294                  */
295                 if (vlan_enabled)
296                         eth_hdr_size = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
297                 else
298                         eth_hdr_size = sizeof(struct ether_hdr);
299
300                 copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
301
302                 if (ipv4) {
303                         copy_buf_to_pkt(ip_hdr, sizeof(struct ipv4_hdr), pkt, eth_hdr_size);
304                         copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
305                                         sizeof(struct ipv4_hdr));
306                 } else {
307                         copy_buf_to_pkt(ip_hdr, sizeof(struct ipv6_hdr), pkt, eth_hdr_size);
308                         copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
309                                         sizeof(struct ipv6_hdr));
310                 }
311
312                 /*
313                  * Complete first mbuf of packet and append it to the
314                  * burst of packets to be transmitted.
315                  */
316                 pkt->nb_segs = nb_pkt_segs;
317                 pkt->pkt_len = pkt_len;
318                 pkt->l2_len = eth_hdr_size;
319
320                 if (ipv4) {
321                         pkt->vlan_tci  = ETHER_TYPE_IPv4;
322                         pkt->l3_len = sizeof(struct ipv4_hdr);
323                 } else {
324                         pkt->vlan_tci  = ETHER_TYPE_IPv6;
325                         pkt->l3_len = sizeof(struct ipv6_hdr);
326                 }
327
328                 pkts_burst[nb_pkt] = pkt;
329         }
330
331         return nb_pkt;
332 }
333
334 int
335 generate_packet_burst_proto(struct rte_mempool *mp,
336                 struct rte_mbuf **pkts_burst,
337                 struct ether_hdr *eth_hdr, uint8_t vlan_enabled, void *ip_hdr,
338                 uint8_t ipv4, uint8_t proto, void *proto_hdr,
339                 int nb_pkt_per_burst, uint8_t pkt_len, uint8_t nb_pkt_segs)
340 {
341         int i, nb_pkt = 0;
342         size_t eth_hdr_size;
343
344         struct rte_mbuf *pkt_seg;
345         struct rte_mbuf *pkt;
346
347         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
348                 pkt = rte_pktmbuf_alloc(mp);
349                 if (pkt == NULL) {
350 nomore_mbuf:
351                         if (nb_pkt == 0)
352                                 return -1;
353                         break;
354                 }
355
356                 pkt->data_len = pkt_len;
357                 pkt_seg = pkt;
358                 for (i = 1; i < nb_pkt_segs; i++) {
359                         pkt_seg->next = rte_pktmbuf_alloc(mp);
360                         if (pkt_seg->next == NULL) {
361                                 pkt->nb_segs = i;
362                                 rte_pktmbuf_free(pkt);
363                                 goto nomore_mbuf;
364                         }
365                         pkt_seg = pkt_seg->next;
366                         pkt_seg->data_len = pkt_len;
367                 }
368                 pkt_seg->next = NULL; /* Last segment of packet. */
369
370                 /*
371                  * Copy headers in first packet segment(s).
372                  */
373                 if (vlan_enabled)
374                         eth_hdr_size = sizeof(struct ether_hdr) +
375                                 sizeof(struct vlan_hdr);
376                 else
377                         eth_hdr_size = sizeof(struct ether_hdr);
378
379                 copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
380
381                 if (ipv4) {
382                         copy_buf_to_pkt(ip_hdr, sizeof(struct ipv4_hdr), pkt,
383                                 eth_hdr_size);
384                         switch (proto) {
385                         case IPPROTO_UDP:
386                                 copy_buf_to_pkt(proto_hdr,
387                                         sizeof(struct udp_hdr), pkt,
388                                         eth_hdr_size + sizeof(struct ipv4_hdr));
389                                 break;
390                         case IPPROTO_TCP:
391                                 copy_buf_to_pkt(proto_hdr,
392                                         sizeof(struct tcp_hdr), pkt,
393                                         eth_hdr_size + sizeof(struct ipv4_hdr));
394                                 break;
395                         case IPPROTO_SCTP:
396                                 copy_buf_to_pkt(proto_hdr,
397                                         sizeof(struct sctp_hdr), pkt,
398                                         eth_hdr_size + sizeof(struct ipv4_hdr));
399                                 break;
400                         default:
401                                 break;
402                         }
403                 } else {
404                         copy_buf_to_pkt(ip_hdr, sizeof(struct ipv6_hdr), pkt,
405                                 eth_hdr_size);
406                         switch (proto) {
407                         case IPPROTO_UDP:
408                                 copy_buf_to_pkt(proto_hdr,
409                                         sizeof(struct udp_hdr), pkt,
410                                         eth_hdr_size + sizeof(struct ipv6_hdr));
411                                 break;
412                         case IPPROTO_TCP:
413                                 copy_buf_to_pkt(proto_hdr,
414                                         sizeof(struct tcp_hdr), pkt,
415                                         eth_hdr_size + sizeof(struct ipv6_hdr));
416                                 break;
417                         case IPPROTO_SCTP:
418                                 copy_buf_to_pkt(proto_hdr,
419                                         sizeof(struct sctp_hdr), pkt,
420                                         eth_hdr_size + sizeof(struct ipv6_hdr));
421                                 break;
422                         default:
423                                 break;
424                         }
425                 }
426
427                 /*
428                  * Complete first mbuf of packet and append it to the
429                  * burst of packets to be transmitted.
430                  */
431                 pkt->nb_segs = nb_pkt_segs;
432                 pkt->pkt_len = pkt_len;
433                 pkt->l2_len = eth_hdr_size;
434
435                 if (ipv4) {
436                         pkt->vlan_tci  = ETHER_TYPE_IPv4;
437                         pkt->l3_len = sizeof(struct ipv4_hdr);
438                 } else {
439                         pkt->vlan_tci  = ETHER_TYPE_IPv6;
440                         pkt->l3_len = sizeof(struct ipv6_hdr);
441                 }
442
443                 pkts_burst[nb_pkt] = pkt;
444         }
445
446         return nb_pkt;
447 }