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 .rxmode = { 137 .split_hdr_size = 0, 138 }, 139 .txmode = { 140 .offloads = 141 RTE_ETH_TX_OFFLOAD_VLAN_INSERT | 142 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | 143 RTE_ETH_TX_OFFLOAD_UDP_CKSUM | 144 RTE_ETH_TX_OFFLOAD_TCP_CKSUM | 145 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM | 146 RTE_ETH_TX_OFFLOAD_TCP_TSO, 147 }, 148 }; 149 struct rte_eth_txconf txq_conf; 150 struct rte_eth_rxconf rxq_conf; 151 struct rte_eth_dev_info dev_info; 152 153 ret = rte_eth_dev_info_get(port_id, &dev_info); 154 if (ret != 0) 155 rte_exit(EXIT_FAILURE, 156 "Error during getting device (port %u) info: %s\n", 157 port_id, strerror(-ret)); 158 159 port_conf.txmode.offloads &= dev_info.tx_offload_capa; 160 printf(":: initializing port: %d\n", port_id); 161 ret = rte_eth_dev_configure(port_id, 162 nr_queues, nr_queues, &port_conf); 163 if (ret < 0) { 164 rte_exit(EXIT_FAILURE, 165 ":: cannot configure device: err=%d, port=%u\n", 166 ret, port_id); 167 } 168 169 rxq_conf = dev_info.default_rxconf; 170 rxq_conf.offloads = port_conf.rxmode.offloads; 171 /* >8 End of ethernet port configured with default settings. */ 172 173 /* Configuring number of RX and TX queues connected to single port. 8< */ 174 for (i = 0; i < nr_queues; i++) { 175 ret = rte_eth_rx_queue_setup(port_id, i, 512, 176 rte_eth_dev_socket_id(port_id), 177 &rxq_conf, 178 mbuf_pool); 179 if (ret < 0) { 180 rte_exit(EXIT_FAILURE, 181 ":: Rx queue setup failed: err=%d, port=%u\n", 182 ret, port_id); 183 } 184 } 185 186 txq_conf = dev_info.default_txconf; 187 txq_conf.offloads = port_conf.txmode.offloads; 188 189 for (i = 0; i < nr_queues; i++) { 190 ret = rte_eth_tx_queue_setup(port_id, i, 512, 191 rte_eth_dev_socket_id(port_id), 192 &txq_conf); 193 if (ret < 0) { 194 rte_exit(EXIT_FAILURE, 195 ":: Tx queue setup failed: err=%d, port=%u\n", 196 ret, port_id); 197 } 198 } 199 /* >8 End of Configuring RX and TX queues connected to single port. */ 200 201 /* Setting the RX port to promiscuous mode. 8< */ 202 ret = rte_eth_promiscuous_enable(port_id); 203 if (ret != 0) 204 rte_exit(EXIT_FAILURE, 205 ":: promiscuous mode enable failed: err=%s, port=%u\n", 206 rte_strerror(-ret), port_id); 207 /* >8 End of setting the RX port to promiscuous mode. */ 208 209 /* Starting the port. 8< */ 210 ret = rte_eth_dev_start(port_id); 211 if (ret < 0) { 212 rte_exit(EXIT_FAILURE, 213 "rte_eth_dev_start:err=%d, port=%u\n", 214 ret, port_id); 215 } 216 /* >8 End of starting the port. */ 217 218 assert_link_status(); 219 220 printf(":: initializing port: %d done\n", port_id); 221 } 222 /* >8 End of Port initialization used in flow filtering. */ 223 224 static void 225 signal_handler(int signum) 226 { 227 if (signum == SIGINT || signum == SIGTERM) { 228 printf("\n\nSignal %d received, preparing to exit...\n", 229 signum); 230 force_quit = true; 231 } 232 } 233 234 int 235 main(int argc, char **argv) 236 { 237 int ret; 238 uint16_t nr_ports; 239 struct rte_flow_error error; 240 241 /* Initialize EAL. 8< */ 242 ret = rte_eal_init(argc, argv); 243 if (ret < 0) 244 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n"); 245 /* >8 End of Initialization of EAL. */ 246 247 force_quit = false; 248 signal(SIGINT, signal_handler); 249 signal(SIGTERM, signal_handler); 250 251 nr_ports = rte_eth_dev_count_avail(); 252 if (nr_ports == 0) 253 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n"); 254 port_id = 0; 255 if (nr_ports != 1) { 256 printf(":: warn: %d ports detected, but we use only one: port %u\n", 257 nr_ports, port_id); 258 } 259 /* Allocates a mempool to hold the mbufs. 8< */ 260 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0, 261 RTE_MBUF_DEFAULT_BUF_SIZE, 262 rte_socket_id()); 263 /* >8 End of allocating a mempool to hold the mbufs. */ 264 if (mbuf_pool == NULL) 265 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n"); 266 267 /* Initializes all the ports using the user defined init_port(). 8< */ 268 init_port(); 269 /* >8 End of Initializing the ports using user defined init_port(). */ 270 271 /* Create flow for send packet with. 8< */ 272 flow = generate_ipv4_flow(port_id, selected_queue, 273 SRC_IP, EMPTY_MASK, 274 DEST_IP, FULL_MASK, &error); 275 /* >8 End of create flow and the flow rule. */ 276 if (!flow) { 277 printf("Flow can't be created %d message: %s\n", 278 error.type, 279 error.message ? error.message : "(no stated reason)"); 280 rte_exit(EXIT_FAILURE, "error in creating flow"); 281 } 282 /* >8 End of creating flow for send packet with. */ 283 284 /* Launching main_loop(). 8< */ 285 ret = main_loop(); 286 /* >8 End of launching main_loop(). */ 287 288 /* clean up the EAL */ 289 rte_eal_cleanup(); 290 291 return ret; 292 } 293