15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#include <rte_byteorder.h>
355129044dSC.J. Collier#include <rte_mbuf.h>
365129044dSC.J. Collier
375129044dSC.J. Collier#include "packet_burst_generator.h"
385129044dSC.J. Collier
395129044dSC.J. Collier#define UDP_SRC_PORT 1024
405129044dSC.J. Collier#define UDP_DST_PORT 1024
415129044dSC.J. Collier
425129044dSC.J. Collier
435129044dSC.J. Collier#define IP_DEFTTL  64   /* from RFC 1340. */
445129044dSC.J. Collier#define IP_VERSION 0x40
455129044dSC.J. Collier#define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
465129044dSC.J. Collier#define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
475129044dSC.J. Collier
485129044dSC.J. Collierstatic void
495129044dSC.J. Colliercopy_buf_to_pkt_segs(void *buf, unsigned len, struct rte_mbuf *pkt,
505129044dSC.J. Collier		unsigned offset)
515129044dSC.J. Collier{
525129044dSC.J. Collier	struct rte_mbuf *seg;
535129044dSC.J. Collier	void *seg_buf;
545129044dSC.J. Collier	unsigned copy_len;
555129044dSC.J. Collier
565129044dSC.J. Collier	seg = pkt;
575129044dSC.J. Collier	while (offset >= seg->data_len) {
585129044dSC.J. Collier		offset -= seg->data_len;
595129044dSC.J. Collier		seg = seg->next;
605129044dSC.J. Collier	}
615129044dSC.J. Collier	copy_len = seg->data_len - offset;
625129044dSC.J. Collier	seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
635129044dSC.J. Collier	while (len > copy_len) {
645129044dSC.J. Collier		rte_memcpy(seg_buf, buf, (size_t) copy_len);
655129044dSC.J. Collier		len -= copy_len;
665129044dSC.J. Collier		buf = ((char *) buf + copy_len);
675129044dSC.J. Collier		seg = seg->next;
685129044dSC.J. Collier		seg_buf = rte_pktmbuf_mtod(seg, void *);
695129044dSC.J. Collier	}
705129044dSC.J. Collier	rte_memcpy(seg_buf, buf, (size_t) len);
715129044dSC.J. Collier}
725129044dSC.J. Collier
735129044dSC.J. Collierstatic inline void
745129044dSC.J. Colliercopy_buf_to_pkt(void *buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
755129044dSC.J. Collier{
765129044dSC.J. Collier	if (offset + len <= pkt->data_len) {
775129044dSC.J. Collier		rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset), buf,
785129044dSC.J. Collier			   (size_t) len);
795129044dSC.J. Collier		return;
805129044dSC.J. Collier	}
815129044dSC.J. Collier	copy_buf_to_pkt_segs(buf, len, pkt, offset);
825129044dSC.J. Collier}
835129044dSC.J. Collier
845129044dSC.J. Colliervoid
855129044dSC.J. Collierinitialize_eth_header(struct ether_hdr *eth_hdr, struct ether_addr *src_mac,
865129044dSC.J. Collier		struct ether_addr *dst_mac, uint16_t ether_type,
875129044dSC.J. Collier		uint8_t vlan_enabled, uint16_t van_id)
885129044dSC.J. Collier{
895129044dSC.J. Collier	ether_addr_copy(dst_mac, &eth_hdr->d_addr);
905129044dSC.J. Collier	ether_addr_copy(src_mac, &eth_hdr->s_addr);
915129044dSC.J. Collier
925129044dSC.J. Collier	if (vlan_enabled) {
935129044dSC.J. Collier		struct vlan_hdr *vhdr = (struct vlan_hdr *)((uint8_t *)eth_hdr +
945129044dSC.J. Collier				sizeof(struct ether_hdr));
955129044dSC.J. Collier
965129044dSC.J. Collier		eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_VLAN);
975129044dSC.J. Collier
985129044dSC.J. Collier		vhdr->eth_proto =  rte_cpu_to_be_16(ether_type);
995129044dSC.J. Collier		vhdr->vlan_tci = van_id;
1005129044dSC.J. Collier	} else {
1015129044dSC.J. Collier		eth_hdr->ether_type = rte_cpu_to_be_16(ether_type);
1025129044dSC.J. Collier	}
1035129044dSC.J. Collier}
1045129044dSC.J. Collier
1055129044dSC.J. Colliervoid
1065129044dSC.J. Collierinitialize_arp_header(struct arp_hdr *arp_hdr, struct ether_addr *src_mac,
1075129044dSC.J. Collier		struct ether_addr *dst_mac, uint32_t src_ip, uint32_t dst_ip,
1085129044dSC.J. Collier		uint32_t opcode)
1095129044dSC.J. Collier{
1105129044dSC.J. Collier	arp_hdr->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
1115129044dSC.J. Collier	arp_hdr->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
1125129044dSC.J. Collier	arp_hdr->arp_hln = ETHER_ADDR_LEN;
1135129044dSC.J. Collier	arp_hdr->arp_pln = sizeof(uint32_t);
1145129044dSC.J. Collier	arp_hdr->arp_op = rte_cpu_to_be_16(opcode);
1155129044dSC.J. Collier	ether_addr_copy(src_mac, &arp_hdr->arp_data.arp_sha);
1165129044dSC.J. Collier	arp_hdr->arp_data.arp_sip = src_ip;
1175129044dSC.J. Collier	ether_addr_copy(dst_mac, &arp_hdr->arp_data.arp_tha);
1185129044dSC.J. Collier	arp_hdr->arp_data.arp_tip = dst_ip;
1195129044dSC.J. Collier}
1205129044dSC.J. Collier
1215129044dSC.J. Collieruint16_t
1225129044dSC.J. Collierinitialize_udp_header(struct udp_hdr *udp_hdr, uint16_t src_port,
1235129044dSC.J. Collier		uint16_t dst_port, uint16_t pkt_data_len)
1245129044dSC.J. Collier{
1255129044dSC.J. Collier	uint16_t pkt_len;
1265129044dSC.J. Collier
1275129044dSC.J. Collier	pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
1285129044dSC.J. Collier
1295129044dSC.J. Collier	udp_hdr->src_port = rte_cpu_to_be_16(src_port);
1305129044dSC.J. Collier	udp_hdr->dst_port = rte_cpu_to_be_16(dst_port);
1315129044dSC.J. Collier	udp_hdr->dgram_len = rte_cpu_to_be_16(pkt_len);
1325129044dSC.J. Collier	udp_hdr->dgram_cksum = 0; /* No UDP checksum. */
1335129044dSC.J. Collier
1345129044dSC.J. Collier	return pkt_len;
1355129044dSC.J. Collier}
1365129044dSC.J. Collier
1375129044dSC.J. Collier
1385129044dSC.J. Collieruint16_t
1395129044dSC.J. Collierinitialize_ipv6_header(struct ipv6_hdr *ip_hdr, uint8_t *src_addr,
1405129044dSC.J. Collier		uint8_t *dst_addr, uint16_t pkt_data_len)
1415129044dSC.J. Collier{
1425129044dSC.J. Collier	ip_hdr->vtc_flow = 0;
1435129044dSC.J. Collier	ip_hdr->payload_len = pkt_data_len;
1445129044dSC.J. Collier	ip_hdr->proto = IPPROTO_UDP;
1455129044dSC.J. Collier	ip_hdr->hop_limits = IP_DEFTTL;
1465129044dSC.J. Collier
1475129044dSC.J. Collier	rte_memcpy(ip_hdr->src_addr, src_addr, sizeof(ip_hdr->src_addr));
1485129044dSC.J. Collier	rte_memcpy(ip_hdr->dst_addr, dst_addr, sizeof(ip_hdr->dst_addr));
1495129044dSC.J. Collier
1505129044dSC.J. Collier	return (uint16_t) (pkt_data_len + sizeof(struct ipv6_hdr));
1515129044dSC.J. Collier}
1525129044dSC.J. Collier
1535129044dSC.J. Collieruint16_t
1545129044dSC.J. Collierinitialize_ipv4_header(struct ipv4_hdr *ip_hdr, uint32_t src_addr,
1555129044dSC.J. Collier		uint32_t dst_addr, uint16_t pkt_data_len)
1565129044dSC.J. Collier{
1575129044dSC.J. Collier	uint16_t pkt_len;
1585129044dSC.J. Collier	unaligned_uint16_t *ptr16;
1595129044dSC.J. Collier	uint32_t ip_cksum;
1605129044dSC.J. Collier
1615129044dSC.J. Collier	/*
1625129044dSC.J. Collier	 * Initialize IP header.
1635129044dSC.J. Collier	 */
1645129044dSC.J. Collier	pkt_len = (uint16_t) (pkt_data_len + sizeof(struct ipv4_hdr));
1655129044dSC.J. Collier
1665129044dSC.J. Collier	ip_hdr->version_ihl   = IP_VHL_DEF;
1675129044dSC.J. Collier	ip_hdr->type_of_service   = 0;
1685129044dSC.J. Collier	ip_hdr->fragment_offset = 0;
1695129044dSC.J. Collier	ip_hdr->time_to_live   = IP_DEFTTL;
1705129044dSC.J. Collier	ip_hdr->next_proto_id = IPPROTO_UDP;
1715129044dSC.J. Collier	ip_hdr->packet_id = 0;
1725129044dSC.J. Collier	ip_hdr->total_length   = rte_cpu_to_be_16(pkt_len);
1735129044dSC.J. Collier	ip_hdr->src_addr = rte_cpu_to_be_32(src_addr);
1745129044dSC.J. Collier	ip_hdr->dst_addr = rte_cpu_to_be_32(dst_addr);
1755129044dSC.J. Collier
1765129044dSC.J. Collier	/*
1775129044dSC.J. Collier	 * Compute IP header checksum.
1785129044dSC.J. Collier	 */
1795129044dSC.J. Collier	ptr16 = (unaligned_uint16_t *)ip_hdr;
1805129044dSC.J. Collier	ip_cksum = 0;
1815129044dSC.J. Collier	ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
1825129044dSC.J. Collier	ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
1835129044dSC.J. Collier	ip_cksum += ptr16[4];
1845129044dSC.J. Collier	ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
1855129044dSC.J. Collier	ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
1865129044dSC.J. Collier
1875129044dSC.J. Collier	/*
1885129044dSC.J. Collier	 * Reduce 32 bit checksum to 16 bits and complement it.
1895129044dSC.J. Collier	 */
1905129044dSC.J. Collier	ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
1915129044dSC.J. Collier		(ip_cksum & 0x0000FFFF);
1925129044dSC.J. Collier	ip_cksum %= 65536;
1935129044dSC.J. Collier	ip_cksum = (~ip_cksum) & 0x0000FFFF;
1945129044dSC.J. Collier	if (ip_cksum == 0)
1955129044dSC.J. Collier		ip_cksum = 0xFFFF;
1965129044dSC.J. Collier	ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
1975129044dSC.J. Collier
1985129044dSC.J. Collier	return pkt_len;
1995129044dSC.J. Collier}
2005129044dSC.J. Collier
2015129044dSC.J. Collier
2025129044dSC.J. Collier
2035129044dSC.J. Collier/*
2045129044dSC.J. Collier * The maximum number of segments per packet is used when creating
2055129044dSC.J. Collier * scattered transmit packets composed of a list of mbufs.
2065129044dSC.J. Collier */
2075129044dSC.J. Collier#define RTE_MAX_SEGS_PER_PKT 255 /**< pkt.nb_segs is a 8-bit unsigned char. */
2085129044dSC.J. Collier
2095129044dSC.J. Collier
2105129044dSC.J. Collierint
2115129044dSC.J. Colliergenerate_packet_burst(struct rte_mempool *mp, struct rte_mbuf **pkts_burst,
2125129044dSC.J. Collier		struct ether_hdr *eth_hdr, uint8_t vlan_enabled, void *ip_hdr,
2135129044dSC.J. Collier		uint8_t ipv4, struct udp_hdr *udp_hdr, int nb_pkt_per_burst,
2145129044dSC.J. Collier		uint8_t pkt_len, uint8_t nb_pkt_segs)
2155129044dSC.J. Collier{
2165129044dSC.J. Collier	int i, nb_pkt = 0;
2175129044dSC.J. Collier	size_t eth_hdr_size;
2185129044dSC.J. Collier
2195129044dSC.J. Collier	struct rte_mbuf *pkt_seg;
2205129044dSC.J. Collier	struct rte_mbuf *pkt;
2215129044dSC.J. Collier
2225129044dSC.J. Collier	for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
2235129044dSC.J. Collier		pkt = rte_pktmbuf_alloc(mp);
2245129044dSC.J. Collier		if (pkt == NULL) {
2255129044dSC.J. Colliernomore_mbuf:
2265129044dSC.J. Collier			if (nb_pkt == 0)
2275129044dSC.J. Collier				return -1;
2285129044dSC.J. Collier			break;
2295129044dSC.J. Collier		}
2305129044dSC.J. Collier
2315129044dSC.J. Collier		pkt->data_len = pkt_len;
2325129044dSC.J. Collier		pkt_seg = pkt;
2335129044dSC.J. Collier		for (i = 1; i < nb_pkt_segs; i++) {
2345129044dSC.J. Collier			pkt_seg->next = rte_pktmbuf_alloc(mp);
2355129044dSC.J. Collier			if (pkt_seg->next == NULL) {
2365129044dSC.J. Collier				pkt->nb_segs = i;
2375129044dSC.J. Collier				rte_pktmbuf_free(pkt);
2385129044dSC.J. Collier				goto nomore_mbuf;
2395129044dSC.J. Collier			}
2405129044dSC.J. Collier			pkt_seg = pkt_seg->next;
2415129044dSC.J. Collier			pkt_seg->data_len = pkt_len;
2425129044dSC.J. Collier		}
2435129044dSC.J. Collier		pkt_seg->next = NULL; /* Last segment of packet. */
2445129044dSC.J. Collier
2455129044dSC.J. Collier		/*
2465129044dSC.J. Collier		 * Copy headers in first packet segment(s).
2475129044dSC.J. Collier		 */
2485129044dSC.J. Collier		if (vlan_enabled)
2495129044dSC.J. Collier			eth_hdr_size = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
2505129044dSC.J. Collier		else
2515129044dSC.J. Collier			eth_hdr_size = sizeof(struct ether_hdr);
2525129044dSC.J. Collier
2535129044dSC.J. Collier		copy_buf_to_pkt(eth_hdr, eth_hdr_size, pkt, 0);
2545129044dSC.J. Collier
2555129044dSC.J. Collier		if (ipv4) {
2565129044dSC.J. Collier			copy_buf_to_pkt(ip_hdr, sizeof(struct ipv4_hdr), pkt, eth_hdr_size);
2575129044dSC.J. Collier			copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
2585129044dSC.J. Collier					sizeof(struct ipv4_hdr));
2595129044dSC.J. Collier		} else {
2605129044dSC.J. Collier			copy_buf_to_pkt(ip_hdr, sizeof(struct ipv6_hdr), pkt, eth_hdr_size);
2615129044dSC.J. Collier			copy_buf_to_pkt(udp_hdr, sizeof(*udp_hdr), pkt, eth_hdr_size +
2625129044dSC.J. Collier					sizeof(struct ipv6_hdr));
2635129044dSC.J. Collier		}
2645129044dSC.J. Collier
2655129044dSC.J. Collier		/*
2665129044dSC.J. Collier		 * Complete first mbuf of packet and append it to the
2675129044dSC.J. Collier		 * burst of packets to be transmitted.
2685129044dSC.J. Collier		 */
2695129044dSC.J. Collier		pkt->nb_segs = nb_pkt_segs;
2705129044dSC.J. Collier		pkt->pkt_len = pkt_len;
2715129044dSC.J. Collier		pkt->l2_len = eth_hdr_size;
2725129044dSC.J. Collier
2735129044dSC.J. Collier		if (ipv4) {
2745129044dSC.J. Collier			pkt->vlan_tci  = ETHER_TYPE_IPv4;
2755129044dSC.J. Collier			pkt->l3_len = sizeof(struct ipv4_hdr);
2765129044dSC.J. Collier		} else {
2775129044dSC.J. Collier			pkt->vlan_tci  = ETHER_TYPE_IPv6;
2785129044dSC.J. Collier			pkt->l3_len = sizeof(struct ipv6_hdr);
2795129044dSC.J. Collier		}
2805129044dSC.J. Collier
2815129044dSC.J. Collier		pkts_burst[nb_pkt] = pkt;
2825129044dSC.J. Collier	}
2835129044dSC.J. Collier
2845129044dSC.J. Collier	return nb_pkt;
2855129044dSC.J. Collier}
286