180139e35SThomas Monjalon /* SPDX-License-Identifier: BSD-3-Clause
280139e35SThomas Monjalon * Copyright 2014-2020 Mellanox Technologies, Ltd
3e9e23a61SCyril Chemparathy */
4e9e23a61SCyril Chemparathy
5e9e23a61SCyril Chemparathy #include <stdarg.h>
6e9e23a61SCyril Chemparathy #include <string.h>
7e9e23a61SCyril Chemparathy #include <stdio.h>
8e9e23a61SCyril Chemparathy #include <errno.h>
9e9e23a61SCyril Chemparathy #include <stdint.h>
10e9e23a61SCyril Chemparathy #include <unistd.h>
11e9e23a61SCyril Chemparathy #include <inttypes.h>
12e9e23a61SCyril Chemparathy
13e9e23a61SCyril Chemparathy #include <sys/queue.h>
14e9e23a61SCyril Chemparathy #include <sys/stat.h>
15e9e23a61SCyril Chemparathy
16e9e23a61SCyril Chemparathy #include <rte_common.h>
17e9e23a61SCyril Chemparathy #include <rte_byteorder.h>
18e9e23a61SCyril Chemparathy #include <rte_log.h>
19e9e23a61SCyril Chemparathy #include <rte_debug.h>
20e9e23a61SCyril Chemparathy #include <rte_cycles.h>
21e9e23a61SCyril Chemparathy #include <rte_memory.h>
22e9e23a61SCyril Chemparathy #include <rte_memcpy.h>
23e9e23a61SCyril Chemparathy #include <rte_launch.h>
24e9e23a61SCyril Chemparathy #include <rte_eal.h>
25e9e23a61SCyril Chemparathy #include <rte_per_lcore.h>
26e9e23a61SCyril Chemparathy #include <rte_lcore.h>
27e9e23a61SCyril Chemparathy #include <rte_branch_prediction.h>
28e9e23a61SCyril Chemparathy #include <rte_mempool.h>
29e9e23a61SCyril Chemparathy #include <rte_mbuf.h>
30e9e23a61SCyril Chemparathy #include <rte_interrupts.h>
31e9e23a61SCyril Chemparathy #include <rte_ether.h>
32e9e23a61SCyril Chemparathy #include <rte_ethdev.h>
33e9e23a61SCyril Chemparathy #include <rte_ip.h>
34e9e23a61SCyril Chemparathy #include <rte_tcp.h>
35e9e23a61SCyril Chemparathy #include <rte_udp.h>
36e9e23a61SCyril Chemparathy #include <rte_string_fns.h>
37938a184aSAdrien Mazarguil #include <rte_flow.h>
38e9e23a61SCyril Chemparathy
39e9e23a61SCyril Chemparathy #include "testpmd.h"
40e9e23a61SCyril Chemparathy
410c9da755SDavid Marchand static uint32_t cfg_ip_src = RTE_IPV4(10, 254, 0, 0);
420c9da755SDavid Marchand static uint32_t cfg_ip_dst = RTE_IPV4(10, 253, 0, 0);
43e9e23a61SCyril Chemparathy static uint16_t cfg_udp_src = 1000;
44e9e23a61SCyril Chemparathy static uint16_t cfg_udp_dst = 1001;
456d13ea8eSOlivier Matz static struct rte_ether_addr cfg_ether_src =
46e9e23a61SCyril Chemparathy {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x00 }};
476d13ea8eSOlivier Matz static struct rte_ether_addr cfg_ether_dst =
48e9e23a61SCyril Chemparathy {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }};
49e9e23a61SCyril Chemparathy
50e9e23a61SCyril Chemparathy #define IP_DEFTTL 64 /* from RFC 1340. */
51e9e23a61SCyril Chemparathy
5208d4d507SZhihong Wang RTE_DEFINE_PER_LCORE(int, _next_flow);
5308d4d507SZhihong Wang
54e9e23a61SCyril Chemparathy /*
55e9e23a61SCyril Chemparathy * Multi-flow generation mode.
56e9e23a61SCyril Chemparathy *
57e9e23a61SCyril Chemparathy * We originate a bunch of flows (varying destination IP addresses), and
58e9e23a61SCyril Chemparathy * terminate receive traffic. Received traffic is simply discarded, but we
59e9e23a61SCyril Chemparathy * still do so in order to maintain traffic statistics.
60e9e23a61SCyril Chemparathy */
6106c20561SDavid Marchand static bool
pkt_burst_flow_gen(struct fwd_stream * fs)62e9e23a61SCyril Chemparathy pkt_burst_flow_gen(struct fwd_stream *fs)
63e9e23a61SCyril Chemparathy {
642ebacaa7SMaciej Czekaj unsigned pkt_size = tx_pkt_length - 4; /* Adjust FCS */
65e9e23a61SCyril Chemparathy struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
66e9e23a61SCyril Chemparathy struct rte_mempool *mbp;
676c02043eSIgor Russkikh struct rte_mbuf *pkt = NULL;
686d13ea8eSOlivier Matz struct rte_ether_hdr *eth_hdr;
69a7c528e5SOlivier Matz struct rte_ipv4_hdr *ip_hdr;
70e73e3547SOlivier Matz struct rte_udp_hdr *udp_hdr;
7192ebda07SHelin Zhang uint16_t vlan_tci, vlan_tci_outer;
722482a004SFerruh Yigit uint64_t ol_flags = 0;
73e9e23a61SCyril Chemparathy uint16_t nb_rx;
74e9e23a61SCyril Chemparathy uint16_t nb_tx;
75861e7684SZhihong Wang uint16_t nb_dropped;
76e9e23a61SCyril Chemparathy uint16_t nb_pkt;
776c02043eSIgor Russkikh uint16_t nb_clones = nb_pkt_flowgen_clones;
783eecba26SShahaf Shuler uint64_t tx_offloads;
7908d4d507SZhihong Wang int next_flow = RTE_PER_LCORE(_next_flow);
80e9e23a61SCyril Chemparathy
81e9e23a61SCyril Chemparathy /* Receive a burst of packets and discard them. */
82d3dae396SDavid Marchand nb_rx = common_fwd_stream_receive(fs, pkts_burst, nb_pkt_per_burst);
83e9e23a61SCyril Chemparathy
84d00fee5dSDavid Marchand rte_pktmbuf_free_bulk(pkts_burst, nb_rx);
85e9e23a61SCyril Chemparathy
86e9e23a61SCyril Chemparathy mbp = current_fwd_lcore()->mbp;
87e9e23a61SCyril Chemparathy vlan_tci = ports[fs->tx_port].tx_vlan_id;
8892ebda07SHelin Zhang vlan_tci_outer = ports[fs->tx_port].tx_vlan_id_outer;
89b62678f7SShahaf Shuler
903eecba26SShahaf Shuler tx_offloads = ports[fs->tx_port].dev_conf.txmode.offloads;
91295968d1SFerruh Yigit if (tx_offloads & RTE_ETH_TX_OFFLOAD_VLAN_INSERT)
92daa02b5cSOlivier Matz ol_flags |= RTE_MBUF_F_TX_VLAN;
93295968d1SFerruh Yigit if (tx_offloads & RTE_ETH_TX_OFFLOAD_QINQ_INSERT)
94daa02b5cSOlivier Matz ol_flags |= RTE_MBUF_F_TX_QINQ;
95295968d1SFerruh Yigit if (tx_offloads & RTE_ETH_TX_OFFLOAD_MACSEC_INSERT)
96daa02b5cSOlivier Matz ol_flags |= RTE_MBUF_F_TX_MACSEC;
97e9e23a61SCyril Chemparathy
98e9e23a61SCyril Chemparathy for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
996c02043eSIgor Russkikh if (!nb_pkt || !nb_clones) {
1006c02043eSIgor Russkikh nb_clones = nb_pkt_flowgen_clones;
1016c02043eSIgor Russkikh /* Logic limitation */
1026c02043eSIgor Russkikh if (nb_clones > nb_pkt_per_burst)
1036c02043eSIgor Russkikh nb_clones = nb_pkt_per_burst;
1046c02043eSIgor Russkikh
105fbfd9955SOlivier Matz pkt = rte_mbuf_raw_alloc(mbp);
106e9e23a61SCyril Chemparathy if (!pkt)
107e9e23a61SCyril Chemparathy break;
108e9e23a61SCyril Chemparathy
109ea672a8bSOlivier Matz pkt->data_len = pkt_size;
110ea672a8bSOlivier Matz pkt->next = NULL;
111e9e23a61SCyril Chemparathy
112e9e23a61SCyril Chemparathy /* Initialize Ethernet header. */
1136d13ea8eSOlivier Matz eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
11404d43857SDmitry Kozlyuk rte_ether_addr_copy(&cfg_ether_dst, ð_hdr->dst_addr);
11504d43857SDmitry Kozlyuk rte_ether_addr_copy(&cfg_ether_src, ð_hdr->src_addr);
1160c9da755SDavid Marchand eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
117e9e23a61SCyril Chemparathy
118e9e23a61SCyril Chemparathy /* Initialize IP header. */
119a7c528e5SOlivier Matz ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
120e9e23a61SCyril Chemparathy memset(ip_hdr, 0, sizeof(*ip_hdr));
1215fde1a75SReshma Pattan ip_hdr->version_ihl = RTE_IPV4_VHL_DEF;
122e9e23a61SCyril Chemparathy ip_hdr->type_of_service = 0;
123e9e23a61SCyril Chemparathy ip_hdr->fragment_offset = 0;
124e9e23a61SCyril Chemparathy ip_hdr->time_to_live = IP_DEFTTL;
125e9e23a61SCyril Chemparathy ip_hdr->next_proto_id = IPPROTO_UDP;
126e9e23a61SCyril Chemparathy ip_hdr->packet_id = 0;
127e9e23a61SCyril Chemparathy ip_hdr->src_addr = rte_cpu_to_be_32(cfg_ip_src);
128e9e23a61SCyril Chemparathy ip_hdr->dst_addr = rte_cpu_to_be_32(cfg_ip_dst +
129e9e23a61SCyril Chemparathy next_flow);
130e9e23a61SCyril Chemparathy ip_hdr->total_length = RTE_CPU_TO_BE_16(pkt_size -
131e9e23a61SCyril Chemparathy sizeof(*eth_hdr));
1327f4d3d12SZhihong Wang ip_hdr->hdr_checksum = rte_ipv4_cksum(ip_hdr);
133e9e23a61SCyril Chemparathy
134e9e23a61SCyril Chemparathy /* Initialize UDP header. */
135e73e3547SOlivier Matz udp_hdr = (struct rte_udp_hdr *)(ip_hdr + 1);
136e9e23a61SCyril Chemparathy udp_hdr->src_port = rte_cpu_to_be_16(cfg_udp_src);
137e9e23a61SCyril Chemparathy udp_hdr->dst_port = rte_cpu_to_be_16(cfg_udp_dst);
138e9e23a61SCyril Chemparathy udp_hdr->dgram_cksum = 0; /* No UDP checksum. */
139e9e23a61SCyril Chemparathy udp_hdr->dgram_len = RTE_CPU_TO_BE_16(pkt_size -
140e9e23a61SCyril Chemparathy sizeof(*eth_hdr) -
141e9e23a61SCyril Chemparathy sizeof(*ip_hdr));
142ea672a8bSOlivier Matz pkt->nb_segs = 1;
143ea672a8bSOlivier Matz pkt->pkt_len = pkt_size;
144daa02b5cSOlivier Matz pkt->ol_flags &= RTE_MBUF_F_EXTERNAL;
14572512e18SViacheslav Ovsiienko pkt->ol_flags |= ol_flags;
1467869536fSBruce Richardson pkt->vlan_tci = vlan_tci;
14792ebda07SHelin Zhang pkt->vlan_tci_outer = vlan_tci_outer;
1486d13ea8eSOlivier Matz pkt->l2_len = sizeof(struct rte_ether_hdr);
149a7c528e5SOlivier Matz pkt->l3_len = sizeof(struct rte_ipv4_hdr);
1506c02043eSIgor Russkikh } else {
1516c02043eSIgor Russkikh nb_clones--;
1526c02043eSIgor Russkikh rte_mbuf_refcnt_update(pkt, 1);
1536c02043eSIgor Russkikh }
154e9e23a61SCyril Chemparathy pkts_burst[nb_pkt] = pkt;
155e9e23a61SCyril Chemparathy
156861e7684SZhihong Wang if (++next_flow >= nb_flows_flowgen)
15708d4d507SZhihong Wang next_flow = 0;
158e9e23a61SCyril Chemparathy }
159e9e23a61SCyril Chemparathy
160*655131ccSDavid Marchand nb_tx = common_fwd_stream_transmit(fs, pkts_burst, nb_pkt);
161861e7684SZhihong Wang nb_dropped = nb_pkt - nb_tx;
162861e7684SZhihong Wang if (unlikely(nb_dropped > 0)) {
163e9e23a61SCyril Chemparathy /* Back out the flow counter. */
164861e7684SZhihong Wang next_flow -= nb_dropped;
165e9e23a61SCyril Chemparathy while (next_flow < 0)
166861e7684SZhihong Wang next_flow += nb_flows_flowgen;
167e9e23a61SCyril Chemparathy }
168bc700b67SDharmik Thakkar
16908d4d507SZhihong Wang RTE_PER_LCORE(_next_flow) = next_flow;
17008d4d507SZhihong Wang
17106c20561SDavid Marchand return true;
172e9e23a61SCyril Chemparathy }
173e9e23a61SCyril Chemparathy
174a78040c9SAlvin Zhang static int
flowgen_begin(portid_t pi)175861e7684SZhihong Wang flowgen_begin(portid_t pi)
176861e7684SZhihong Wang {
177861e7684SZhihong Wang printf(" number of flows for port %u: %d\n", pi, nb_flows_flowgen);
178a78040c9SAlvin Zhang return 0;
179861e7684SZhihong Wang }
180861e7684SZhihong Wang
181e9e23a61SCyril Chemparathy struct fwd_engine flow_gen_engine = {
182e9e23a61SCyril Chemparathy .fwd_mode_name = "flowgen",
183861e7684SZhihong Wang .port_fwd_begin = flowgen_begin,
184180ba023SDavid Marchand .stream_init = common_fwd_stream_init,
185e9e23a61SCyril Chemparathy .packet_fwd = pkt_burst_flow_gen,
186e9e23a61SCyril Chemparathy };
187