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