1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
10 #include <sys/types.h>
11 #include <sys/queue.h>
21 #include <rte_common.h>
22 #include <rte_malloc.h>
23 #include <rte_ether.h>
24 #include <rte_ethdev.h>
25 #include <rte_mempool.h>
29 #include <rte_cycles.h>
31 static volatile bool force_quit;
33 static uint16_t port_id;
34 static uint16_t nr_queues = 5;
35 static uint8_t selected_queue = 1;
36 struct rte_mempool *mbuf_pool;
37 struct rte_flow *flow;
39 #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */
40 #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */
41 #define FULL_MASK 0xffffffff /* full mask */
42 #define EMPTY_MASK 0x0 /* empty mask */
44 #include "flow_blocks.c"
47 print_ether_addr(const char *what, struct rte_ether_addr *eth_addr)
49 char buf[RTE_ETHER_ADDR_FMT_SIZE];
50 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
51 printf("%s%s", what, buf);
57 struct rte_mbuf *mbufs[32];
58 struct rte_ether_hdr *eth_hdr;
59 struct rte_flow_error error;
66 for (i = 0; i < nr_queues; i++) {
67 nb_rx = rte_eth_rx_burst(port_id,
70 for (j = 0; j < nb_rx; j++) {
71 struct rte_mbuf *m = mbufs[j];
73 eth_hdr = rte_pktmbuf_mtod(m,
74 struct rte_ether_hdr *);
75 print_ether_addr("src=",
77 print_ether_addr(" - dst=",
79 printf(" - queue=0x%x",
89 /* closing and releasing resources */
90 rte_flow_flush(port_id, &error);
91 ret = rte_eth_dev_stop(port_id);
93 printf("Failed to stop port %u: %s",
94 port_id, rte_strerror(-ret));
95 rte_eth_dev_close(port_id);
99 #define CHECK_INTERVAL 1000 /* 100ms */
100 #define MAX_REPEAT_TIMES 90 /* 9s (90 * 100ms) in total */
103 assert_link_status(void)
105 struct rte_eth_link link;
106 uint8_t rep_cnt = MAX_REPEAT_TIMES;
107 int link_get_err = -EINVAL;
109 memset(&link, 0, sizeof(link));
111 link_get_err = rte_eth_link_get(port_id, &link);
112 if (link_get_err == 0 && link.link_status == ETH_LINK_UP)
114 rte_delay_ms(CHECK_INTERVAL);
117 if (link_get_err < 0)
118 rte_exit(EXIT_FAILURE, ":: error: link get is failing: %s\n",
119 rte_strerror(-link_get_err));
120 if (link.link_status == ETH_LINK_DOWN)
121 rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
129 struct rte_eth_conf port_conf = {
135 DEV_TX_OFFLOAD_VLAN_INSERT |
136 DEV_TX_OFFLOAD_IPV4_CKSUM |
137 DEV_TX_OFFLOAD_UDP_CKSUM |
138 DEV_TX_OFFLOAD_TCP_CKSUM |
139 DEV_TX_OFFLOAD_SCTP_CKSUM |
140 DEV_TX_OFFLOAD_TCP_TSO,
143 struct rte_eth_txconf txq_conf;
144 struct rte_eth_rxconf rxq_conf;
145 struct rte_eth_dev_info dev_info;
147 ret = rte_eth_dev_info_get(port_id, &dev_info);
149 rte_exit(EXIT_FAILURE,
150 "Error during getting device (port %u) info: %s\n",
151 port_id, strerror(-ret));
153 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
154 printf(":: initializing port: %d\n", port_id);
155 ret = rte_eth_dev_configure(port_id,
156 nr_queues, nr_queues, &port_conf);
158 rte_exit(EXIT_FAILURE,
159 ":: cannot configure device: err=%d, port=%u\n",
163 rxq_conf = dev_info.default_rxconf;
164 rxq_conf.offloads = port_conf.rxmode.offloads;
165 for (i = 0; i < nr_queues; i++) {
166 ret = rte_eth_rx_queue_setup(port_id, i, 512,
167 rte_eth_dev_socket_id(port_id),
171 rte_exit(EXIT_FAILURE,
172 ":: Rx queue setup failed: err=%d, port=%u\n",
177 txq_conf = dev_info.default_txconf;
178 txq_conf.offloads = port_conf.txmode.offloads;
180 for (i = 0; i < nr_queues; i++) {
181 ret = rte_eth_tx_queue_setup(port_id, i, 512,
182 rte_eth_dev_socket_id(port_id),
185 rte_exit(EXIT_FAILURE,
186 ":: Tx queue setup failed: err=%d, port=%u\n",
191 ret = rte_eth_promiscuous_enable(port_id);
193 rte_exit(EXIT_FAILURE,
194 ":: promiscuous mode enable failed: err=%s, port=%u\n",
195 rte_strerror(-ret), port_id);
197 ret = rte_eth_dev_start(port_id);
199 rte_exit(EXIT_FAILURE,
200 "rte_eth_dev_start:err=%d, port=%u\n",
204 assert_link_status();
206 printf(":: initializing port: %d done\n", port_id);
210 signal_handler(int signum)
212 if (signum == SIGINT || signum == SIGTERM) {
213 printf("\n\nSignal %d received, preparing to exit...\n",
220 main(int argc, char **argv)
224 struct rte_flow_error error;
226 ret = rte_eal_init(argc, argv);
228 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
231 signal(SIGINT, signal_handler);
232 signal(SIGTERM, signal_handler);
234 nr_ports = rte_eth_dev_count_avail();
236 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
239 printf(":: warn: %d ports detected, but we use only one: port %u\n",
242 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
243 RTE_MBUF_DEFAULT_BUF_SIZE,
245 if (mbuf_pool == NULL)
246 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
250 /* create flow for send packet with */
251 flow = generate_ipv4_flow(port_id, selected_queue,
253 DEST_IP, FULL_MASK, &error);
255 printf("Flow can't be created %d message: %s\n",
257 error.message ? error.message : "(no stated reason)");
258 rte_exit(EXIT_FAILURE, "error in creating flow");