db7f02345f33116ca3d026c8278cd9cf6163a5fb
[dpdk.git] / app / test / packet_burst_generator.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <rte_byteorder.h>
35 #include <rte_mbuf.h>
36
37 #include "packet_burst_generator.h"
38
39 #define UDP_SRC_PORT 1024
40 #define UDP_DST_PORT 1024
41
42
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)
47
48 static void
49 copy_buf_to_pkt_segs(void *buf, unsigned len, struct rte_mbuf *pkt,
50                 unsigned offset)
51 {
52         struct rte_mbuf *seg;
53         void *seg_buf;
54         unsigned copy_len;
55
56         seg = pkt;
57         while (offset >= seg->data_len) {
58                 offset -= seg->data_len;
59                 seg = seg->next;
60         }
61         copy_len = seg->data_len - offset;
62         seg_buf = ((char *) seg->data + offset);
63         while (len > copy_len) {
64                 rte_memcpy(seg_buf, buf, (size_t) copy_len);
65                 len -= copy_len;
66                 buf = ((char *) buf + copy_len);
67                 seg = seg->next;
68                 seg_buf = seg->data;
69         }
70         rte_memcpy(seg_buf, buf, (size_t) len);
71 }
72
73 static inline void
74 copy_buf_to_pkt(void *buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
75 {
76         if (offset + len <= pkt->data_len) {
77                 rte_memcpy(((char *) pkt->data + offset), buf, (size_t) len);
78                 return;
79         }
80         copy_buf_to_pkt_segs(buf, len, pkt, offset);
81 }
82
83
84 void
85 initialize_eth_header(struct ether_hdr *eth_hdr, struct ether_addr *src_mac,
86                 struct ether_addr *dst_mac, uint8_t vlan_enabled, uint16_t van_id)
87 {
88         ether_addr_copy(dst_mac, &eth_hdr->d_addr);
89         ether_addr_copy(src_mac, &eth_hdr->s_addr);
90
91         if (vlan_enabled) {
92                 struct vlan_hdr *vhdr = (struct vlan_hdr *)((uint8_t *)eth_hdr +
93                                 sizeof(struct ether_hdr));
94
95                 eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
96
97                 vhdr->eth_proto =  rte_cpu_to_be_16(ETHER_TYPE_IPv4);
98                 vhdr->vlan_tci = van_id;
99         } else {
100                 eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
101         }
102
103 }
104
105 uint16_t
106 initialize_udp_header(struct udp_hdr *udp_hdr, uint16_t src_port,
107                 uint16_t dst_port, uint16_t pkt_data_len)
108 {
109         uint16_t pkt_len;
110
111         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
112
113         udp_hdr->src_port = rte_cpu_to_be_16(src_port);
114         udp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
115         udp_hdr->dgram_len = rte_cpu_to_be_16(pkt_len);
116         udp_hdr->dgram_cksum = 0; /* No UDP checksum. */
117
118         return pkt_len;
119 }
120
121
122 uint16_t
123 initialize_ipv6_header(struct ipv6_hdr *ip_hdr, uint8_t *src_addr,
124                 uint8_t *dst_addr, uint16_t pkt_data_len)
125 {
126         ip_hdr->vtc_flow = 0;
127         ip_hdr->payload_len = pkt_data_len;
128         ip_hdr->proto = IPPROTO_UDP;
129         ip_hdr->hop_limits = IP_DEFTTL;
130
131         rte_memcpy(ip_hdr->src_addr, src_addr, sizeof(ip_hdr->src_addr));
132         rte_memcpy(ip_hdr->dst_addr, dst_addr, sizeof(ip_hdr->dst_addr));
133
134         return (uint16_t) (pkt_data_len + sizeof(struct ipv6_hdr));
135 }
136
137 uint16_t
138 initialize_ipv4_header(struct ipv4_hdr *ip_hdr, uint32_t src_addr,
139                 uint32_t dst_addr, uint16_t pkt_data_len)
140 {
141         uint16_t pkt_len;
142         uint16_t *ptr16;
143         uint32_t ip_cksum;
144
145         /*
146          * Initialize IP header.
147          */
148         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct ipv4_hdr));
149
150         ip_hdr->version_ihl   = IP_VHL_DEF;
151         ip_hdr->type_of_service   = 0;
152         ip_hdr->fragment_offset = 0;
153         ip_hdr->time_to_live   = IP_DEFTTL;
154         ip_hdr->next_proto_id = IPPROTO_UDP;
155         ip_hdr->packet_id = 0;
156         ip_hdr->total_length   = rte_cpu_to_be_16(pkt_len);
157         ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
158         ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
159
160         /*
161          * Compute IP header checksum.
162          */
163         ptr16 = (uint16_t *)ip_hdr;
164         ip_cksum = 0;
165         ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
166         ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
167         ip_cksum += ptr16[4];
168         ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
169         ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
170
171         /*
172          * Reduce 32 bit checksum to 16 bits and complement it.
173          */
174         ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
175                 (ip_cksum & 0x0000FFFF);
176         ip_cksum %= 65536;
177         ip_cksum = (~ip_cksum) & 0x0000FFFF;
178         if (ip_cksum == 0)
179                 ip_cksum = 0xFFFF;
180         ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
181
182         return pkt_len;
183 }
184
185
186
187 /*
188  * The maximum number of segments per packet is used when creating
189  * scattered transmit packets composed of a list of mbufs.
190  */
191 #define RTE_MAX_SEGS_PER_PKT 255 /**< pkt.nb_segs is a 8-bit unsigned char. */
192
193 #define TXONLY_DEF_PACKET_LEN 64
194 #define TXONLY_DEF_PACKET_LEN_128 128
195
196 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN;
197 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
198                 TXONLY_DEF_PACKET_LEN_128,
199 };
200
201 uint8_t  tx_pkt_nb_segs = 1;
202
203 int
204 generate_packet_burst(struct rte_mempool *mp, struct rte_mbuf **pkts_burst,
205                 struct ether_hdr *eth_hdr, uint8_t vlan_enabled, void *ip_hdr,
206                 uint8_t ipv4, struct udp_hdr *udp_hdr, int nb_pkt_per_burst)
207 {
208         int i, nb_pkt = 0;
209         size_t eth_hdr_size;
210
211         struct rte_mbuf *pkt_seg;
212         struct rte_mbuf *pkt;
213
214         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
215                 pkt = rte_pktmbuf_alloc(mp);
216                 if (pkt == NULL) {
217 nomore_mbuf:
218                         if (nb_pkt == 0)
219                                 return -1;
220                         break;
221                 }
222
223                 pkt->data_len = tx_pkt_seg_lengths[0];
224                 pkt_seg = pkt;
225                 for (i = 1; i < tx_pkt_nb_segs; i++) {
226                         pkt_seg->next = rte_pktmbuf_alloc(mp);
227                         if (pkt_seg->next == NULL) {
228                                 pkt->nb_segs = i;
229                                 rte_pktmbuf_free(pkt);
230                                 goto nomore_mbuf;
231                         }
232                         pkt_seg = pkt_seg->next;
233                         pkt_seg->data_len = tx_pkt_seg_lengths[i];
234                 }
235                 pkt_seg->next = NULL; /* Last segment of packet. */
236
237                 /*
238                  * Copy headers in first packet segment(s).
239                  */
240                 if (vlan_enabled)
241                         eth_hdr_size = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
242                 else
243                         eth_hdr_size = sizeof(struct ether_hdr);
244
245                 copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
246
247                 if (ipv4) {
248                         copy_buf_to_pkt(ip_hdr, sizeof(struct ipv4_hdr), pkt, eth_hdr_size);
249                         copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
250                                         sizeof(struct ipv4_hdr));
251                 } else {
252                         copy_buf_to_pkt(ip_hdr, sizeof(struct ipv6_hdr), pkt, eth_hdr_size);
253                         copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
254                                         sizeof(struct ipv6_hdr));
255                 }
256
257                 /*
258                  * Complete first mbuf of packet and append it to the
259                  * burst of packets to be transmitted.
260                  */
261                 pkt->nb_segs = tx_pkt_nb_segs;
262                 pkt->pkt_len = tx_pkt_length;
263                 pkt->l2_len = eth_hdr_size;
264
265                 if (ipv4) {
266                         pkt->vlan_tci  = ETHER_TYPE_IPv4;
267                         pkt->l3_len = sizeof(struct ipv4_hdr);
268
269                         if (vlan_enabled)
270                                 pkt->ol_flags = PKT_RX_IPV4_HDR | PKT_RX_VLAN_PKT;
271                         else
272                                 pkt->ol_flags = PKT_RX_IPV4_HDR;
273                 } else {
274                         pkt->vlan_tci  = ETHER_TYPE_IPv6;
275                         pkt->l3_len = sizeof(struct ipv6_hdr);
276
277                         if (vlan_enabled)
278                                 pkt->ol_flags = PKT_RX_IPV6_HDR | PKT_RX_VLAN_PKT;
279                         else
280                                 pkt->ol_flags = PKT_RX_IPV6_HDR;
281                 }
282
283                 pkts_burst[nb_pkt] = pkt;
284         }
285
286         return nb_pkt;
287 }