4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <rte_byteorder.h>
37 #include "packet_burst_generator.h"
39 #define UDP_SRC_PORT 1024
40 #define UDP_DST_PORT 1024
43 #define IP_DEFTTL 64 /* from RFC 1340. */
44 #define IP_VERSION 0x40
45 #define IP_HDRLEN 0x05 /* default IP header length == five 32-bits words. */
46 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
49 copy_buf_to_pkt_segs(void *buf, unsigned len, struct rte_mbuf *pkt,
57 while (offset >= seg->data_len) {
58 offset -= seg->data_len;
61 copy_len = seg->data_len - offset;
62 seg_buf = rte_pktmbuf_mtod(seg, char *) + offset;
63 while (len > copy_len) {
64 rte_memcpy(seg_buf, buf, (size_t) copy_len);
66 buf = ((char *) buf + copy_len);
68 seg_buf = rte_pktmbuf_mtod(seg, void *);
70 rte_memcpy(seg_buf, buf, (size_t) len);
74 copy_buf_to_pkt(void *buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
76 if (offset + len <= pkt->data_len) {
77 rte_memcpy(rte_pktmbuf_mtod(pkt, char *) + offset,
81 copy_buf_to_pkt_segs(buf, len, pkt, offset);
86 initialize_eth_header(struct ether_hdr *eth_hdr, struct ether_addr *src_mac,
87 struct ether_addr *dst_mac, uint8_t vlan_enabled, uint16_t van_id)
89 ether_addr_copy(dst_mac, ð_hdr->d_addr);
90 ether_addr_copy(src_mac, ð_hdr->s_addr);
93 struct vlan_hdr *vhdr = (struct vlan_hdr *)((uint8_t *)eth_hdr +
94 sizeof(struct ether_hdr));
96 eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
98 vhdr->eth_proto = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
99 vhdr->vlan_tci = van_id;
101 eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
107 initialize_udp_header(struct udp_hdr *udp_hdr, uint16_t src_port,
108 uint16_t dst_port, uint16_t pkt_data_len)
112 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
114 udp_hdr->src_port = rte_cpu_to_be_16(src_port);
115 udp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
116 udp_hdr->dgram_len = rte_cpu_to_be_16(pkt_len);
117 udp_hdr->dgram_cksum = 0; /* No UDP checksum. */
124 initialize_ipv6_header(struct ipv6_hdr *ip_hdr, uint8_t *src_addr,
125 uint8_t *dst_addr, uint16_t pkt_data_len)
127 ip_hdr->vtc_flow = 0;
128 ip_hdr->payload_len = pkt_data_len;
129 ip_hdr->proto = IPPROTO_UDP;
130 ip_hdr->hop_limits = IP_DEFTTL;
132 rte_memcpy(ip_hdr->src_addr, src_addr, sizeof(ip_hdr->src_addr));
133 rte_memcpy(ip_hdr->dst_addr, dst_addr, sizeof(ip_hdr->dst_addr));
135 return (uint16_t) (pkt_data_len + sizeof(struct ipv6_hdr));
139 initialize_ipv4_header(struct ipv4_hdr *ip_hdr, uint32_t src_addr,
140 uint32_t dst_addr, uint16_t pkt_data_len)
147 * Initialize IP header.
149 pkt_len = (uint16_t) (pkt_data_len + sizeof(struct ipv4_hdr));
151 ip_hdr->version_ihl = IP_VHL_DEF;
152 ip_hdr->type_of_service = 0;
153 ip_hdr->fragment_offset = 0;
154 ip_hdr->time_to_live = IP_DEFTTL;
155 ip_hdr->next_proto_id = IPPROTO_UDP;
156 ip_hdr->packet_id = 0;
157 ip_hdr->total_length = rte_cpu_to_be_16(pkt_len);
158 ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
159 ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
162 * Compute IP header checksum.
164 ptr16 = (uint16_t *)ip_hdr;
166 ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
167 ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
168 ip_cksum += ptr16[4];
169 ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
170 ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
173 * Reduce 32 bit checksum to 16 bits and complement it.
175 ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
176 (ip_cksum & 0x0000FFFF);
178 ip_cksum = (~ip_cksum) & 0x0000FFFF;
181 ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
189 * The maximum number of segments per packet is used when creating
190 * scattered transmit packets composed of a list of mbufs.
192 #define RTE_MAX_SEGS_PER_PKT 255 /**< pkt.nb_segs is a 8-bit unsigned char. */
194 #define TXONLY_DEF_PACKET_LEN 64
195 #define TXONLY_DEF_PACKET_LEN_128 128
197 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN;
198 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
199 TXONLY_DEF_PACKET_LEN_128,
202 uint8_t tx_pkt_nb_segs = 1;
205 generate_packet_burst(struct rte_mempool *mp, struct rte_mbuf **pkts_burst,
206 struct ether_hdr *eth_hdr, uint8_t vlan_enabled, void *ip_hdr,
207 uint8_t ipv4, struct udp_hdr *udp_hdr, int nb_pkt_per_burst)
212 struct rte_mbuf *pkt_seg;
213 struct rte_mbuf *pkt;
215 for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
216 pkt = rte_pktmbuf_alloc(mp);
224 pkt->data_len = tx_pkt_seg_lengths[0];
226 for (i = 1; i < tx_pkt_nb_segs; i++) {
227 pkt_seg->next = rte_pktmbuf_alloc(mp);
228 if (pkt_seg->next == NULL) {
230 rte_pktmbuf_free(pkt);
233 pkt_seg = pkt_seg->next;
234 pkt_seg->data_len = tx_pkt_seg_lengths[i];
236 pkt_seg->next = NULL; /* Last segment of packet. */
239 * Copy headers in first packet segment(s).
242 eth_hdr_size = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
244 eth_hdr_size = sizeof(struct ether_hdr);
246 copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
249 copy_buf_to_pkt(ip_hdr, sizeof(struct ipv4_hdr), pkt, eth_hdr_size);
250 copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
251 sizeof(struct ipv4_hdr));
253 copy_buf_to_pkt(ip_hdr, sizeof(struct ipv6_hdr), pkt, eth_hdr_size);
254 copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
255 sizeof(struct ipv6_hdr));
259 * Complete first mbuf of packet and append it to the
260 * burst of packets to be transmitted.
262 pkt->nb_segs = tx_pkt_nb_segs;
263 pkt->pkt_len = tx_pkt_length;
264 pkt->l2_len = eth_hdr_size;
267 pkt->vlan_tci = ETHER_TYPE_IPv4;
268 pkt->l3_len = sizeof(struct ipv4_hdr);
271 pkt->ol_flags = PKT_RX_IPV4_HDR | PKT_RX_VLAN_PKT;
273 pkt->ol_flags = PKT_RX_IPV4_HDR;
275 pkt->vlan_tci = ETHER_TYPE_IPv6;
276 pkt->l3_len = sizeof(struct ipv6_hdr);
279 pkt->ol_flags = PKT_RX_IPV6_HDR | PKT_RX_VLAN_PKT;
281 pkt->ol_flags = PKT_RX_IPV6_HDR;
284 pkts_burst[nb_pkt] = pkt;