1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2017 Mellanox Technologies, Ltd 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <stdint.h> 9 #include <inttypes.h> 10 #include <sys/types.h> 11 #include <sys/queue.h> 12 #include <setjmp.h> 13 #include <stdarg.h> 14 #include <ctype.h> 15 #include <errno.h> 16 #include <getopt.h> 17 #include <signal.h> 18 #include <stdbool.h> 19 20 #include <rte_eal.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> 26 #include <rte_mbuf.h> 27 #include <rte_net.h> 28 #include <rte_flow.h> 29 #include <rte_cycles.h> 30 31 static volatile bool force_quit; 32 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; 38 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 */ 43 44 #include "flow_blocks.c" 45 46 static inline void 47 print_ether_addr(const char *what, struct rte_ether_addr *eth_addr) 48 { 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); 52 } 53 54 /* Main_loop for flow filtering. 8< */ 55 static int 56 main_loop(void) 57 { 58 struct rte_mbuf *mbufs[32]; 59 struct rte_ether_hdr *eth_hdr; 60 struct rte_flow_error error; 61 uint16_t nb_rx; 62 uint16_t i; 63 uint16_t j; 64 int ret; 65 66 /* Reading the packets from all queues. 8< */ 67 while (!force_quit) { 68 for (i = 0; i < nr_queues; i++) { 69 nb_rx = rte_eth_rx_burst(port_id, 70 i, mbufs, 32); 71 if (nb_rx) { 72 for (j = 0; j < nb_rx; j++) { 73 struct rte_mbuf *m = mbufs[j]; 74 75 eth_hdr = rte_pktmbuf_mtod(m, 76 struct rte_ether_hdr *); 77 print_ether_addr("src=", 78 ð_hdr->src_addr); 79 print_ether_addr(" - dst=", 80 ð_hdr->dst_addr); 81 printf(" - queue=0x%x", 82 (unsigned int)i); 83 printf("\n"); 84 85 rte_pktmbuf_free(m); 86 } 87 } 88 } 89 } 90 /* >8 End of reading the packets from all queues. */ 91 92 /* closing and releasing resources */ 93 rte_flow_flush(port_id, &error); 94 ret = rte_eth_dev_stop(port_id); 95 if (ret < 0) 96 printf("Failed to stop port %u: %s", 97 port_id, rte_strerror(-ret)); 98 rte_eth_dev_close(port_id); 99 return ret; 100 } 101 /* >8 End of main_loop for flow filtering. */ 102 103 #define CHECK_INTERVAL 1000 /* 100ms */ 104 #define MAX_REPEAT_TIMES 90 /* 9s (90 * 100ms) in total */ 105 106 static void 107 assert_link_status(void) 108 { 109 struct rte_eth_link link; 110 uint8_t rep_cnt = MAX_REPEAT_TIMES; 111 int link_get_err = -EINVAL; 112 113 memset(&link, 0, sizeof(link)); 114 do { 115 link_get_err = rte_eth_link_get(port_id, &link); 116 if (link_get_err == 0 && link.link_status == RTE_ETH_LINK_UP) 117 break; 118 rte_delay_ms(CHECK_INTERVAL); 119 } while (--rep_cnt); 120 121 if (link_get_err < 0) 122 rte_exit(EXIT_FAILURE, ":: error: link get is failing: %s\n", 123 rte_strerror(-link_get_err)); 124 if (link.link_status == RTE_ETH_LINK_DOWN) 125 rte_exit(EXIT_FAILURE, ":: error: link is still down\n"); 126 } 127 128 /* Port initialization used in flow filtering. 8< */ 129 static void 130 init_port(void) 131 { 132 int ret; 133 uint16_t i; 134 /* Ethernet port configured with default settings. 8< */ 135 struct rte_eth_conf port_conf = { 136 .txmode = { 137 .offloads = 138 RTE_ETH_TX_OFFLOAD_VLAN_INSERT | 139 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | 140 RTE_ETH_TX_OFFLOAD_UDP_CKSUM | 141 RTE_ETH_TX_OFFLOAD_TCP_CKSUM | 142 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM | 143 RTE_ETH_TX_OFFLOAD_TCP_TSO, 144 }, 145 }; 146 struct rte_eth_txconf txq_conf; 147 struct rte_eth_rxconf rxq_conf; 148 struct rte_eth_dev_info dev_info; 149 150 ret = rte_eth_dev_info_get(port_id, &dev_info); 151 if (ret != 0) 152 rte_exit(EXIT_FAILURE, 153 "Error during getting device (port %u) info: %s\n", 154 port_id, strerror(-ret)); 155 156 port_conf.txmode.offloads &= dev_info.tx_offload_capa; 157 printf(":: initializing port: %d\n", port_id); 158 ret = rte_eth_dev_configure(port_id, 159 nr_queues, nr_queues, &port_conf); 160 if (ret < 0) { 161 rte_exit(EXIT_FAILURE, 162 ":: cannot configure device: err=%d, port=%u\n", 163 ret, port_id); 164 } 165 166 rxq_conf = dev_info.default_rxconf; 167 rxq_conf.offloads = port_conf.rxmode.offloads; 168 /* >8 End of ethernet port configured with default settings. */ 169 170 /* Configuring number of RX and TX queues connected to single port. 8< */ 171 for (i = 0; i < nr_queues; i++) { 172 ret = rte_eth_rx_queue_setup(port_id, i, 512, 173 rte_eth_dev_socket_id(port_id), 174 &rxq_conf, 175 mbuf_pool); 176 if (ret < 0) { 177 rte_exit(EXIT_FAILURE, 178 ":: Rx queue setup failed: err=%d, port=%u\n", 179 ret, port_id); 180 } 181 } 182 183 txq_conf = dev_info.default_txconf; 184 txq_conf.offloads = port_conf.txmode.offloads; 185 186 for (i = 0; i < nr_queues; i++) { 187 ret = rte_eth_tx_queue_setup(port_id, i, 512, 188 rte_eth_dev_socket_id(port_id), 189 &txq_conf); 190 if (ret < 0) { 191 rte_exit(EXIT_FAILURE, 192 ":: Tx queue setup failed: err=%d, port=%u\n", 193 ret, port_id); 194 } 195 } 196 /* >8 End of Configuring RX and TX queues connected to single port. */ 197 198 /* Setting the RX port to promiscuous mode. 8< */ 199 ret = rte_eth_promiscuous_enable(port_id); 200 if (ret != 0) 201 rte_exit(EXIT_FAILURE, 202 ":: promiscuous mode enable failed: err=%s, port=%u\n", 203 rte_strerror(-ret), port_id); 204 /* >8 End of setting the RX port to promiscuous mode. */ 205 206 /* Starting the port. 8< */ 207 ret = rte_eth_dev_start(port_id); 208 if (ret < 0) { 209 rte_exit(EXIT_FAILURE, 210 "rte_eth_dev_start:err=%d, port=%u\n", 211 ret, port_id); 212 } 213 /* >8 End of starting the port. */ 214 215 assert_link_status(); 216 217 printf(":: initializing port: %d done\n", port_id); 218 } 219 /* >8 End of Port initialization used in flow filtering. */ 220 221 static void 222 signal_handler(int signum) 223 { 224 if (signum == SIGINT || signum == SIGTERM) { 225 printf("\n\nSignal %d received, preparing to exit...\n", 226 signum); 227 force_quit = true; 228 } 229 } 230 231 int 232 main(int argc, char **argv) 233 { 234 int ret; 235 uint16_t nr_ports; 236 struct rte_flow_error error; 237 238 /* Initialize EAL. 8< */ 239 ret = rte_eal_init(argc, argv); 240 if (ret < 0) 241 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n"); 242 /* >8 End of Initialization of EAL. */ 243 244 force_quit = false; 245 signal(SIGINT, signal_handler); 246 signal(SIGTERM, signal_handler); 247 248 nr_ports = rte_eth_dev_count_avail(); 249 if (nr_ports == 0) 250 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n"); 251 port_id = 0; 252 if (nr_ports != 1) { 253 printf(":: warn: %d ports detected, but we use only one: port %u\n", 254 nr_ports, port_id); 255 } 256 /* Allocates a mempool to hold the mbufs. 8< */ 257 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0, 258 RTE_MBUF_DEFAULT_BUF_SIZE, 259 rte_socket_id()); 260 /* >8 End of allocating a mempool to hold the mbufs. */ 261 if (mbuf_pool == NULL) 262 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n"); 263 264 /* Initializes all the ports using the user defined init_port(). 8< */ 265 init_port(); 266 /* >8 End of Initializing the ports using user defined init_port(). */ 267 268 /* Create flow for send packet with. 8< */ 269 flow = generate_ipv4_flow(port_id, selected_queue, 270 SRC_IP, EMPTY_MASK, 271 DEST_IP, FULL_MASK, &error); 272 /* >8 End of create flow and the flow rule. */ 273 if (!flow) { 274 printf("Flow can't be created %d message: %s\n", 275 error.type, 276 error.message ? error.message : "(no stated reason)"); 277 rte_exit(EXIT_FAILURE, "error in creating flow"); 278 } 279 /* >8 End of creating flow for send packet with. */ 280 281 /* Launching main_loop(). 8< */ 282 ret = main_loop(); 283 /* >8 End of launching main_loop(). */ 284 285 /* clean up the EAL */ 286 rte_eal_cleanup(); 287 288 return ret; 289 } 290