1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <rte_byteorder.h>
9 #include "packet_burst_generator.h"
11 #define UDP_SRC_PORT 1024
12 #define UDP_DST_PORT 1024
15 #define IP_DEFTTL 64 /* from RFC 1340. */
18 copy_buf_to_pkt_segs(void *buf, unsigned len, struct rte_mbuf *pkt,
26 while (offset >= seg->data_len) {
27 offset -= seg->data_len;
30 copy_len = seg->data_len - offset;
31 seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
32 while (len > copy_len) {
33 rte_memcpy(seg_buf, buf, (size_t) copy_len);
35 buf = ((char *) buf + copy_len);
37 seg_buf = rte_pktmbuf_mtod(seg, void *);
39 rte_memcpy(seg_buf, buf, (size_t) len);
43 copy_buf_to_pkt(void *buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
45 if (offset + len <= pkt->data_len) {
46 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset), buf,
50 copy_buf_to_pkt_segs(buf, len, pkt, offset);
54 initialize_eth_header(struct rte_ether_hdr *eth_hdr,
55 struct rte_ether_addr *src_mac,
56 struct rte_ether_addr *dst_mac, uint16_t ether_type,
57 uint8_t vlan_enabled, uint16_t van_id)
59 rte_ether_addr_copy(dst_mac, ð_hdr->d_addr);
60 rte_ether_addr_copy(src_mac, ð_hdr->s_addr);
63 struct rte_vlan_hdr *vhdr = (struct rte_vlan_hdr *)(
64 (uint8_t *)eth_hdr + sizeof(struct rte_ether_hdr));
66 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
68 vhdr->eth_proto = rte_cpu_to_be_16(ether_type);
69 vhdr->vlan_tci = van_id;
71 eth_hdr->ether_type = rte_cpu_to_be_16(ether_type);
76 initialize_arp_header(struct rte_arp_hdr *arp_hdr,
77 struct rte_ether_addr *src_mac,
78 struct rte_ether_addr *dst_mac,
79 uint32_t src_ip, uint32_t dst_ip,
82 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
83 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
84 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
85 arp_hdr->arp_plen = sizeof(uint32_t);
86 arp_hdr->arp_opcode = rte_cpu_to_be_16(opcode);
87 rte_ether_addr_copy(src_mac, &arp_hdr->arp_data.arp_sha);
88 arp_hdr->arp_data.arp_sip = src_ip;
89 rte_ether_addr_copy(dst_mac, &arp_hdr->arp_data.arp_tha);
90 arp_hdr->arp_data.arp_tip = dst_ip;
94 initialize_udp_header(struct rte_udp_hdr *udp_hdr, uint16_t src_port,
95 uint16_t dst_port, uint16_t pkt_data_len)
99 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct rte_udp_hdr));
101 udp_hdr->src_port = rte_cpu_to_be_16(src_port);
102 udp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
103 udp_hdr->dgram_len = rte_cpu_to_be_16(pkt_len);
104 udp_hdr->dgram_cksum = 0; /* No UDP checksum. */
110 initialize_tcp_header(struct rte_tcp_hdr *tcp_hdr, uint16_t src_port,
111 uint16_t dst_port, uint16_t pkt_data_len)
115 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct rte_tcp_hdr));
117 memset(tcp_hdr, 0, sizeof(struct rte_tcp_hdr));
118 tcp_hdr->src_port = rte_cpu_to_be_16(src_port);
119 tcp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
125 initialize_sctp_header(struct rte_sctp_hdr *sctp_hdr, uint16_t src_port,
126 uint16_t dst_port, uint16_t pkt_data_len)
130 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct rte_udp_hdr));
132 sctp_hdr->src_port = rte_cpu_to_be_16(src_port);
133 sctp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
135 sctp_hdr->cksum = 0; /* No SCTP checksum. */
141 initialize_ipv6_header(struct rte_ipv6_hdr *ip_hdr, uint8_t *src_addr,
142 uint8_t *dst_addr, uint16_t pkt_data_len)
144 ip_hdr->vtc_flow = 0;
145 ip_hdr->payload_len = pkt_data_len;
146 ip_hdr->proto = IPPROTO_UDP;
147 ip_hdr->hop_limits = IP_DEFTTL;
149 rte_memcpy(ip_hdr->src_addr, src_addr, sizeof(ip_hdr->src_addr));
150 rte_memcpy(ip_hdr->dst_addr, dst_addr, sizeof(ip_hdr->dst_addr));
152 return (uint16_t) (pkt_data_len + sizeof(struct rte_ipv6_hdr));
156 initialize_ipv4_header(struct rte_ipv4_hdr *ip_hdr, uint32_t src_addr,
157 uint32_t dst_addr, uint16_t pkt_data_len)
160 unaligned_uint16_t *ptr16;
164 * Initialize IP header.
166 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct rte_ipv4_hdr));
168 ip_hdr->version_ihl = RTE_IPV4_VHL_DEF;
169 ip_hdr->type_of_service = 0;
170 ip_hdr->fragment_offset = 0;
171 ip_hdr->time_to_live = IP_DEFTTL;
172 ip_hdr->next_proto_id = IPPROTO_UDP;
173 ip_hdr->packet_id = 0;
174 ip_hdr->total_length = rte_cpu_to_be_16(pkt_len);
175 ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
176 ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
179 * Compute IP header checksum.
181 ptr16 = (unaligned_uint16_t *)ip_hdr;
183 ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
184 ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
185 ip_cksum += ptr16[4];
186 ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
187 ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
190 * Reduce 32 bit checksum to 16 bits and complement it.
192 ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
193 (ip_cksum & 0x0000FFFF);
195 ip_cksum = (~ip_cksum) & 0x0000FFFF;
198 ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
204 initialize_ipv4_header_proto(struct rte_ipv4_hdr *ip_hdr, uint32_t src_addr,
205 uint32_t dst_addr, uint16_t pkt_data_len, uint8_t proto)
208 unaligned_uint16_t *ptr16;
212 * Initialize IP header.
214 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct rte_ipv4_hdr));
216 ip_hdr->version_ihl = RTE_IPV4_VHL_DEF;
217 ip_hdr->type_of_service = 0;
218 ip_hdr->fragment_offset = 0;
219 ip_hdr->time_to_live = IP_DEFTTL;
220 ip_hdr->next_proto_id = proto;
221 ip_hdr->packet_id = 0;
222 ip_hdr->total_length = rte_cpu_to_be_16(pkt_len);
223 ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
224 ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
227 * Compute IP header checksum.
229 ptr16 = (unaligned_uint16_t *)ip_hdr;
231 ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
232 ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
233 ip_cksum += ptr16[4];
234 ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
235 ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
238 * Reduce 32 bit checksum to 16 bits and complement it.
240 ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
241 (ip_cksum & 0x0000FFFF);
243 ip_cksum = (~ip_cksum) & 0x0000FFFF;
246 ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
252 * The maximum number of segments per packet is used when creating
253 * scattered transmit packets composed of a list of mbufs.
255 #define RTE_MAX_SEGS_PER_PKT 255 /**< pkt.nb_segs is a 8-bit unsigned char. */
259 generate_packet_burst(struct rte_mempool *mp, struct rte_mbuf **pkts_burst,
260 struct rte_ether_hdr *eth_hdr, uint8_t vlan_enabled,
261 void *ip_hdr, uint8_t ipv4, struct rte_udp_hdr *udp_hdr,
262 int nb_pkt_per_burst, uint8_t pkt_len, uint8_t nb_pkt_segs)
267 struct rte_mbuf *pkt_seg;
268 struct rte_mbuf *pkt;
270 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
271 pkt = rte_pktmbuf_alloc(mp);
279 pkt->data_len = pkt_len;
281 for (i = 1; i < nb_pkt_segs; i++) {
282 pkt_seg->next = rte_pktmbuf_alloc(mp);
283 if (pkt_seg->next == NULL) {
285 rte_pktmbuf_free(pkt);
288 pkt_seg = pkt_seg->next;
289 pkt_seg->data_len = pkt_len;
291 pkt_seg->next = NULL; /* Last segment of packet. */
294 * Copy headers in first packet segment(s).
297 eth_hdr_size = sizeof(struct rte_ether_hdr) +
298 sizeof(struct rte_vlan_hdr);
300 eth_hdr_size = sizeof(struct rte_ether_hdr);
302 copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
305 copy_buf_to_pkt(ip_hdr, sizeof(struct rte_ipv4_hdr),
307 copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt,
308 eth_hdr_size + sizeof(struct rte_ipv4_hdr));
310 copy_buf_to_pkt(ip_hdr, sizeof(struct rte_ipv6_hdr),
312 copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt,
313 eth_hdr_size + sizeof(struct rte_ipv6_hdr));
317 * Complete first mbuf of packet and append it to the
318 * burst of packets to be transmitted.
320 pkt->nb_segs = nb_pkt_segs;
321 pkt->pkt_len = pkt_len;
322 pkt->l2_len = eth_hdr_size;
325 pkt->vlan_tci = RTE_ETHER_TYPE_IPV4;
326 pkt->l3_len = sizeof(struct rte_ipv4_hdr);
328 pkt->vlan_tci = RTE_ETHER_TYPE_IPV6;
329 pkt->l3_len = sizeof(struct rte_ipv6_hdr);
332 pkts_burst[nb_pkt] = pkt;
339 generate_packet_burst_proto(struct rte_mempool *mp,
340 struct rte_mbuf **pkts_burst, struct rte_ether_hdr *eth_hdr,
341 uint8_t vlan_enabled, void *ip_hdr,
342 uint8_t ipv4, uint8_t proto, void *proto_hdr,
343 int nb_pkt_per_burst, uint8_t pkt_len, uint8_t nb_pkt_segs)
348 struct rte_mbuf *pkt_seg;
349 struct rte_mbuf *pkt;
351 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
352 pkt = rte_pktmbuf_alloc(mp);
360 pkt->data_len = pkt_len;
362 for (i = 1; i < nb_pkt_segs; i++) {
363 pkt_seg->next = rte_pktmbuf_alloc(mp);
364 if (pkt_seg->next == NULL) {
366 rte_pktmbuf_free(pkt);
369 pkt_seg = pkt_seg->next;
370 pkt_seg->data_len = pkt_len;
372 pkt_seg->next = NULL; /* Last segment of packet. */
375 * Copy headers in first packet segment(s).
378 eth_hdr_size = sizeof(struct rte_ether_hdr) +
379 sizeof(struct rte_vlan_hdr);
381 eth_hdr_size = sizeof(struct rte_ether_hdr);
383 copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
386 copy_buf_to_pkt(ip_hdr, sizeof(struct rte_ipv4_hdr),
390 copy_buf_to_pkt(proto_hdr,
391 sizeof(struct rte_udp_hdr), pkt,
393 sizeof(struct rte_ipv4_hdr));
396 copy_buf_to_pkt(proto_hdr,
397 sizeof(struct rte_tcp_hdr), pkt,
399 sizeof(struct rte_ipv4_hdr));
402 copy_buf_to_pkt(proto_hdr,
403 sizeof(struct rte_sctp_hdr), pkt,
405 sizeof(struct rte_ipv4_hdr));
411 copy_buf_to_pkt(ip_hdr, sizeof(struct rte_ipv6_hdr),
415 copy_buf_to_pkt(proto_hdr,
416 sizeof(struct rte_udp_hdr), pkt,
418 sizeof(struct rte_ipv6_hdr));
421 copy_buf_to_pkt(proto_hdr,
422 sizeof(struct rte_tcp_hdr), pkt,
424 sizeof(struct rte_ipv6_hdr));
427 copy_buf_to_pkt(proto_hdr,
428 sizeof(struct rte_sctp_hdr), pkt,
430 sizeof(struct rte_ipv6_hdr));
438 * Complete first mbuf of packet and append it to the
439 * burst of packets to be transmitted.
441 pkt->nb_segs = nb_pkt_segs;
442 pkt->pkt_len = pkt_len;
443 pkt->l2_len = eth_hdr_size;
446 pkt->vlan_tci = RTE_ETHER_TYPE_IPV4;
447 pkt->l3_len = sizeof(struct rte_ipv4_hdr);
449 pkt->vlan_tci = RTE_ETHER_TYPE_IPV6;
450 pkt->l3_len = sizeof(struct rte_ipv6_hdr);
453 pkts_burst[nb_pkt] = pkt;