1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2015 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <sys/queue.h> 7 #include <sys/socket.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <stdio.h> 11 #include <assert.h> 12 #include <errno.h> 13 #include <signal.h> 14 #include <stdarg.h> 15 #include <inttypes.h> 16 #include <getopt.h> 17 #include <termios.h> 18 #include <unistd.h> 19 #include <pthread.h> 20 21 #include <rte_common.h> 22 #include <rte_log.h> 23 #include <rte_memory.h> 24 #include <rte_memcpy.h> 25 #include <rte_eal.h> 26 #include <rte_launch.h> 27 #include <rte_cycles.h> 28 #include <rte_prefetch.h> 29 #include <rte_lcore.h> 30 #include <rte_per_lcore.h> 31 #include <rte_branch_prediction.h> 32 #include <rte_interrupts.h> 33 #include <rte_random.h> 34 #include <rte_debug.h> 35 #include <rte_ether.h> 36 #include <rte_ethdev.h> 37 #include <rte_mempool.h> 38 #include <rte_mbuf.h> 39 #include <rte_ip.h> 40 #include <rte_tcp.h> 41 #include <rte_arp.h> 42 #include <rte_spinlock.h> 43 #include <rte_devargs.h> 44 #include <rte_byteorder.h> 45 #include <rte_cpuflags.h> 46 #include <rte_eth_bond.h> 47 48 #include <cmdline_rdline.h> 49 #include <cmdline_parse.h> 50 #include <cmdline_parse_num.h> 51 #include <cmdline_parse_string.h> 52 #include <cmdline_parse_ipaddr.h> 53 #include <cmdline_parse_etheraddr.h> 54 #include <cmdline_socket.h> 55 #include <cmdline.h> 56 57 #include "main.h" 58 59 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1 60 61 #define NB_MBUF (1024*8) 62 63 #define MAX_PKT_BURST 32 64 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 65 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */ 66 67 /* 68 * RX and TX Prefetch, Host, and Write-back threshold values should be 69 * carefully set for optimal performance. Consult the network 70 * controller's datasheet and supporting DPDK documentation for guidance 71 * on how these parameters should be set. 72 */ 73 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */ 74 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */ 75 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */ 76 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */ 77 78 /* 79 * These default values are optimized for use with the Intel(R) 82599 10 GbE 80 * Controller and the DPDK ixgbe PMD. Consider using other values for other 81 * network controllers and/or network drivers. 82 */ 83 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */ 84 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */ 85 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */ 86 87 /* 88 * Configurable number of RX/TX ring descriptors 89 */ 90 #define RTE_RX_DESC_DEFAULT 1024 91 #define RTE_TX_DESC_DEFAULT 1024 92 93 #define BOND_IP_1 7 94 #define BOND_IP_2 0 95 #define BOND_IP_3 0 96 #define BOND_IP_4 10 97 98 /* not defined under linux */ 99 #ifndef NIPQUAD 100 #define NIPQUAD_FMT "%u.%u.%u.%u" 101 #endif 102 103 #define MAX_PORTS 4 104 #define PRINT_MAC(addr) printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \ 105 ":%02"PRIx8":%02"PRIx8":%02"PRIx8, \ 106 RTE_ETHER_ADDR_BYTES(&addr)) 107 108 uint16_t slaves[RTE_MAX_ETHPORTS]; 109 uint16_t slaves_count; 110 111 static uint16_t BOND_PORT = 0xffff; 112 113 static struct rte_mempool *mbuf_pool; 114 115 static struct rte_eth_conf port_conf = { 116 .rxmode = { 117 .mq_mode = ETH_MQ_RX_NONE, 118 .max_rx_pkt_len = RTE_ETHER_MAX_LEN, 119 .split_hdr_size = 0, 120 }, 121 .rx_adv_conf = { 122 .rss_conf = { 123 .rss_key = NULL, 124 .rss_hf = ETH_RSS_IP, 125 }, 126 }, 127 .txmode = { 128 .mq_mode = ETH_MQ_TX_NONE, 129 }, 130 }; 131 132 static void 133 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool) 134 { 135 int retval; 136 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT; 137 uint16_t nb_txd = RTE_TX_DESC_DEFAULT; 138 struct rte_eth_dev_info dev_info; 139 struct rte_eth_rxconf rxq_conf; 140 struct rte_eth_txconf txq_conf; 141 struct rte_eth_conf local_port_conf = port_conf; 142 143 if (!rte_eth_dev_is_valid_port(portid)) 144 rte_exit(EXIT_FAILURE, "Invalid port\n"); 145 146 retval = rte_eth_dev_info_get(portid, &dev_info); 147 if (retval != 0) 148 rte_exit(EXIT_FAILURE, 149 "Error during getting device (port %u) info: %s\n", 150 portid, strerror(-retval)); 151 152 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 153 local_port_conf.txmode.offloads |= 154 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 155 156 local_port_conf.rx_adv_conf.rss_conf.rss_hf &= 157 dev_info.flow_type_rss_offloads; 158 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf != 159 port_conf.rx_adv_conf.rss_conf.rss_hf) { 160 printf("Port %u modified RSS hash function based on hardware support," 161 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 162 portid, 163 port_conf.rx_adv_conf.rss_conf.rss_hf, 164 local_port_conf.rx_adv_conf.rss_conf.rss_hf); 165 } 166 167 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf); 168 if (retval != 0) 169 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n", 170 portid, retval); 171 172 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd); 173 if (retval != 0) 174 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc " 175 "failed (res=%d)\n", portid, retval); 176 177 /* RX setup */ 178 rxq_conf = dev_info.default_rxconf; 179 rxq_conf.offloads = local_port_conf.rxmode.offloads; 180 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 181 rte_eth_dev_socket_id(portid), 182 &rxq_conf, 183 mbuf_pool); 184 if (retval < 0) 185 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)", 186 portid, retval); 187 188 /* TX setup */ 189 txq_conf = dev_info.default_txconf; 190 txq_conf.offloads = local_port_conf.txmode.offloads; 191 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd, 192 rte_eth_dev_socket_id(portid), &txq_conf); 193 194 if (retval < 0) 195 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)", 196 portid, retval); 197 198 retval = rte_eth_dev_start(portid); 199 if (retval < 0) 200 rte_exit(retval, 201 "Start port %d failed (res=%d)", 202 portid, retval); 203 204 struct rte_ether_addr addr; 205 206 retval = rte_eth_macaddr_get(portid, &addr); 207 if (retval != 0) 208 rte_exit(retval, 209 "Mac address get port %d failed (res=%d)", 210 portid, retval); 211 212 printf("Port %u MAC: ", portid); 213 PRINT_MAC(addr); 214 printf("\n"); 215 } 216 217 static void 218 bond_port_init(struct rte_mempool *mbuf_pool) 219 { 220 int retval; 221 uint8_t i; 222 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT; 223 uint16_t nb_txd = RTE_TX_DESC_DEFAULT; 224 struct rte_eth_dev_info dev_info; 225 struct rte_eth_rxconf rxq_conf; 226 struct rte_eth_txconf txq_conf; 227 struct rte_eth_conf local_port_conf = port_conf; 228 uint16_t wait_counter = 20; 229 230 retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB, 231 0 /*SOCKET_ID_ANY*/); 232 if (retval < 0) 233 rte_exit(EXIT_FAILURE, 234 "Faled to create bond port\n"); 235 236 BOND_PORT = retval; 237 238 retval = rte_eth_dev_info_get(BOND_PORT, &dev_info); 239 if (retval != 0) 240 rte_exit(EXIT_FAILURE, 241 "Error during getting device (port %u) info: %s\n", 242 BOND_PORT, strerror(-retval)); 243 244 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 245 local_port_conf.txmode.offloads |= 246 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 247 retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf); 248 if (retval != 0) 249 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n", 250 BOND_PORT, retval); 251 252 retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd); 253 if (retval != 0) 254 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc " 255 "failed (res=%d)\n", BOND_PORT, retval); 256 257 for (i = 0; i < slaves_count; i++) { 258 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1) 259 rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n", 260 slaves[i], BOND_PORT); 261 262 } 263 264 /* RX setup */ 265 rxq_conf = dev_info.default_rxconf; 266 rxq_conf.offloads = local_port_conf.rxmode.offloads; 267 retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd, 268 rte_eth_dev_socket_id(BOND_PORT), 269 &rxq_conf, mbuf_pool); 270 if (retval < 0) 271 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)", 272 BOND_PORT, retval); 273 274 /* TX setup */ 275 txq_conf = dev_info.default_txconf; 276 txq_conf.offloads = local_port_conf.txmode.offloads; 277 retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd, 278 rte_eth_dev_socket_id(BOND_PORT), &txq_conf); 279 280 if (retval < 0) 281 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)", 282 BOND_PORT, retval); 283 284 retval = rte_eth_dev_start(BOND_PORT); 285 if (retval < 0) 286 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval); 287 288 printf("Waiting for slaves to become active..."); 289 while (wait_counter) { 290 uint16_t act_slaves[16] = {0}; 291 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) == 292 slaves_count) { 293 printf("\n"); 294 break; 295 } 296 sleep(1); 297 printf("..."); 298 if (--wait_counter == 0) 299 rte_exit(-1, "\nFailed to activate slaves\n"); 300 } 301 302 retval = rte_eth_promiscuous_enable(BOND_PORT); 303 if (retval != 0) { 304 rte_exit(EXIT_FAILURE, 305 "port %u: promiscuous mode enable failed: %s\n", 306 BOND_PORT, rte_strerror(-retval)); 307 return; 308 } 309 310 struct rte_ether_addr addr; 311 312 retval = rte_eth_macaddr_get(BOND_PORT, &addr); 313 if (retval != 0) 314 rte_exit(retval, "port %u: Mac address get failed (res=%d)", 315 BOND_PORT, retval); 316 317 printf("Port %u MAC: ", (unsigned)BOND_PORT); 318 PRINT_MAC(addr); 319 printf("\n"); 320 } 321 322 static inline size_t 323 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto) 324 { 325 size_t vlan_offset = 0; 326 327 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) { 328 struct rte_vlan_hdr *vlan_hdr = 329 (struct rte_vlan_hdr *)(eth_hdr + 1); 330 331 vlan_offset = sizeof(struct rte_vlan_hdr); 332 *proto = vlan_hdr->eth_proto; 333 334 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) { 335 vlan_hdr = vlan_hdr + 1; 336 337 *proto = vlan_hdr->eth_proto; 338 vlan_offset += sizeof(struct rte_vlan_hdr); 339 } 340 } 341 return vlan_offset; 342 } 343 344 struct global_flag_stru_t { 345 int LcoreMainIsRunning; 346 int LcoreMainCore; 347 uint32_t port_packets[4]; 348 rte_spinlock_t lock; 349 }; 350 struct global_flag_stru_t global_flag_stru; 351 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru; 352 353 /* 354 * Main thread that does the work, reading from INPUT_PORT 355 * and writing to OUTPUT_PORT 356 */ 357 static int lcore_main(__rte_unused void *arg1) 358 { 359 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned; 360 struct rte_ether_addr dst_addr; 361 362 struct rte_ether_addr bond_mac_addr; 363 struct rte_ether_hdr *eth_hdr; 364 struct rte_arp_hdr *arp_hdr; 365 struct rte_ipv4_hdr *ipv4_hdr; 366 uint16_t ether_type, offset; 367 368 uint16_t rx_cnt; 369 uint32_t bond_ip; 370 int i = 0; 371 uint8_t is_free; 372 int ret; 373 374 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) | 375 (BOND_IP_3 << 16) | (BOND_IP_4 << 24); 376 377 rte_spinlock_trylock(&global_flag_stru_p->lock); 378 379 while (global_flag_stru_p->LcoreMainIsRunning) { 380 rte_spinlock_unlock(&global_flag_stru_p->lock); 381 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST); 382 is_free = 0; 383 384 /* If didn't receive any packets, wait and go to next iteration */ 385 if (rx_cnt == 0) { 386 rte_delay_us(50); 387 continue; 388 } 389 390 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr); 391 if (ret != 0) { 392 printf("Bond (port %u) MAC address get failed: %s.\n" 393 "%u packets dropped", BOND_PORT, strerror(-ret), 394 rx_cnt); 395 rte_pktmbuf_free(pkts[i]); 396 continue; 397 } 398 399 /* Search incoming data for ARP packets and prepare response */ 400 for (i = 0; i < rx_cnt; i++) { 401 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) { 402 global_flag_stru_p->port_packets[0]++; 403 rte_spinlock_unlock(&global_flag_stru_p->lock); 404 } 405 eth_hdr = rte_pktmbuf_mtod(pkts[i], 406 struct rte_ether_hdr *); 407 ether_type = eth_hdr->ether_type; 408 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) 409 printf("VLAN taged frame, offset:"); 410 offset = get_vlan_offset(eth_hdr, ðer_type); 411 if (offset > 0) 412 printf("%d\n", offset); 413 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) { 414 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) { 415 global_flag_stru_p->port_packets[1]++; 416 rte_spinlock_unlock(&global_flag_stru_p->lock); 417 } 418 arp_hdr = (struct rte_arp_hdr *)( 419 (char *)(eth_hdr + 1) + offset); 420 if (arp_hdr->arp_data.arp_tip == bond_ip) { 421 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) { 422 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY); 423 /* Switch src and dst data and set bonding MAC */ 424 rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr); 425 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr); 426 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha, 427 &arp_hdr->arp_data.arp_tha); 428 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip; 429 rte_ether_addr_copy(&bond_mac_addr, &dst_addr); 430 rte_ether_addr_copy(&dst_addr, &arp_hdr->arp_data.arp_sha); 431 arp_hdr->arp_data.arp_sip = bond_ip; 432 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1); 433 is_free = 1; 434 } else { 435 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0); 436 } 437 } 438 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) { 439 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) { 440 global_flag_stru_p->port_packets[2]++; 441 rte_spinlock_unlock(&global_flag_stru_p->lock); 442 } 443 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset); 444 if (ipv4_hdr->dst_addr == bond_ip) { 445 rte_ether_addr_copy(ð_hdr->src_addr, 446 ð_hdr->dst_addr); 447 rte_ether_addr_copy(&bond_mac_addr, 448 ð_hdr->src_addr); 449 ipv4_hdr->dst_addr = ipv4_hdr->src_addr; 450 ipv4_hdr->src_addr = bond_ip; 451 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1); 452 } 453 454 } 455 456 /* Free processed packets */ 457 if (is_free == 0) 458 rte_pktmbuf_free(pkts[i]); 459 } 460 rte_spinlock_trylock(&global_flag_stru_p->lock); 461 } 462 rte_spinlock_unlock(&global_flag_stru_p->lock); 463 printf("BYE lcore_main\n"); 464 return 0; 465 } 466 467 struct cmd_obj_send_result { 468 cmdline_fixed_string_t action; 469 cmdline_ipaddr_t ip; 470 }; 471 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size) 472 { 473 snprintf(buf, size, NIPQUAD_FMT, 474 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]), 475 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]), 476 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]), 477 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3]) 478 ); 479 } 480 static void cmd_obj_send_parsed(void *parsed_result, 481 __rte_unused struct cmdline *cl, 482 __rte_unused void *data) 483 { 484 485 struct cmd_obj_send_result *res = parsed_result; 486 char ip_str[INET6_ADDRSTRLEN]; 487 488 struct rte_ether_addr bond_mac_addr; 489 struct rte_mbuf *created_pkt; 490 struct rte_ether_hdr *eth_hdr; 491 struct rte_arp_hdr *arp_hdr; 492 493 uint32_t bond_ip; 494 size_t pkt_size; 495 int ret; 496 497 if (res->ip.family == AF_INET) 498 get_string(res, ip_str, INET_ADDRSTRLEN); 499 else 500 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n"); 501 502 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) | 503 (BOND_IP_3 << 16) | (BOND_IP_4 << 24); 504 505 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr); 506 if (ret != 0) { 507 cmdline_printf(cl, 508 "Failed to get bond (port %u) MAC address: %s\n", 509 BOND_PORT, strerror(-ret)); 510 } 511 512 created_pkt = rte_pktmbuf_alloc(mbuf_pool); 513 if (created_pkt == NULL) { 514 cmdline_printf(cl, "Failed to allocate mbuf\n"); 515 return; 516 } 517 518 pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr); 519 created_pkt->data_len = pkt_size; 520 created_pkt->pkt_len = pkt_size; 521 522 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *); 523 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr); 524 memset(ð_hdr->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN); 525 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP); 526 527 arp_hdr = (struct rte_arp_hdr *)( 528 (char *)eth_hdr + sizeof(struct rte_ether_hdr)); 529 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER); 530 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4); 531 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN; 532 arp_hdr->arp_plen = sizeof(uint32_t); 533 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST); 534 535 rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha); 536 arp_hdr->arp_data.arp_sip = bond_ip; 537 memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN); 538 arp_hdr->arp_data.arp_tip = 539 ((unsigned char *)&res->ip.addr.ipv4)[0] | 540 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8) | 541 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) | 542 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24); 543 rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1); 544 545 rte_delay_ms(100); 546 cmdline_printf(cl, "\n"); 547 } 548 549 cmdline_parse_token_string_t cmd_obj_action_send = 550 TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send"); 551 cmdline_parse_token_ipaddr_t cmd_obj_ip = 552 TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip); 553 554 cmdline_parse_inst_t cmd_obj_send = { 555 .f = cmd_obj_send_parsed, /* function to call */ 556 .data = NULL, /* 2nd arg of func */ 557 .help_str = "send client_ip", 558 .tokens = { /* token list, NULL terminated */ 559 (void *)&cmd_obj_action_send, 560 (void *)&cmd_obj_ip, 561 NULL, 562 }, 563 }; 564 565 struct cmd_start_result { 566 cmdline_fixed_string_t start; 567 }; 568 569 static void cmd_start_parsed(__rte_unused void *parsed_result, 570 struct cmdline *cl, 571 __rte_unused void *data) 572 { 573 int worker_core_id = rte_lcore_id(); 574 575 rte_spinlock_trylock(&global_flag_stru_p->lock); 576 if (global_flag_stru_p->LcoreMainIsRunning == 0) { 577 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore) 578 != WAIT) { 579 rte_spinlock_unlock(&global_flag_stru_p->lock); 580 return; 581 } 582 rte_spinlock_unlock(&global_flag_stru_p->lock); 583 } else { 584 cmdline_printf(cl, "lcore_main already running on core:%d\n", 585 global_flag_stru_p->LcoreMainCore); 586 rte_spinlock_unlock(&global_flag_stru_p->lock); 587 return; 588 } 589 590 /* start lcore main on core != main_core - ARP response thread */ 591 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0); 592 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0)) 593 return; 594 595 rte_spinlock_trylock(&global_flag_stru_p->lock); 596 global_flag_stru_p->LcoreMainIsRunning = 1; 597 rte_spinlock_unlock(&global_flag_stru_p->lock); 598 cmdline_printf(cl, 599 "Starting lcore_main on core %d:%d " 600 "Our IP:%d.%d.%d.%d\n", 601 worker_core_id, 602 rte_eal_remote_launch(lcore_main, NULL, worker_core_id), 603 BOND_IP_1, 604 BOND_IP_2, 605 BOND_IP_3, 606 BOND_IP_4 607 ); 608 } 609 610 cmdline_parse_token_string_t cmd_start_start = 611 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start"); 612 613 cmdline_parse_inst_t cmd_start = { 614 .f = cmd_start_parsed, /* function to call */ 615 .data = NULL, /* 2nd arg of func */ 616 .help_str = "starts listening if not started at startup", 617 .tokens = { /* token list, NULL terminated */ 618 (void *)&cmd_start_start, 619 NULL, 620 }, 621 }; 622 623 struct cmd_help_result { 624 cmdline_fixed_string_t help; 625 }; 626 627 static void cmd_help_parsed(__rte_unused void *parsed_result, 628 struct cmdline *cl, 629 __rte_unused void *data) 630 { 631 cmdline_printf(cl, 632 "ALB - link bonding mode 6 example\n" 633 "send IP - sends one ARPrequest through bonding for IP.\n" 634 "start - starts listening ARPs.\n" 635 "stop - stops lcore_main.\n" 636 "show - shows some bond info: ex. active slaves etc.\n" 637 "help - prints help.\n" 638 "quit - terminate all threads and quit.\n" 639 ); 640 } 641 642 cmdline_parse_token_string_t cmd_help_help = 643 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help"); 644 645 cmdline_parse_inst_t cmd_help = { 646 .f = cmd_help_parsed, /* function to call */ 647 .data = NULL, /* 2nd arg of func */ 648 .help_str = "show help", 649 .tokens = { /* token list, NULL terminated */ 650 (void *)&cmd_help_help, 651 NULL, 652 }, 653 }; 654 655 struct cmd_stop_result { 656 cmdline_fixed_string_t stop; 657 }; 658 659 static void cmd_stop_parsed(__rte_unused void *parsed_result, 660 struct cmdline *cl, 661 __rte_unused void *data) 662 { 663 rte_spinlock_trylock(&global_flag_stru_p->lock); 664 if (global_flag_stru_p->LcoreMainIsRunning == 0) { 665 cmdline_printf(cl, 666 "lcore_main not running on core:%d\n", 667 global_flag_stru_p->LcoreMainCore); 668 rte_spinlock_unlock(&global_flag_stru_p->lock); 669 return; 670 } 671 global_flag_stru_p->LcoreMainIsRunning = 0; 672 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0) 673 cmdline_printf(cl, 674 "error: lcore_main can not stop on core:%d\n", 675 global_flag_stru_p->LcoreMainCore); 676 else 677 cmdline_printf(cl, 678 "lcore_main stopped on core:%d\n", 679 global_flag_stru_p->LcoreMainCore); 680 rte_spinlock_unlock(&global_flag_stru_p->lock); 681 } 682 683 cmdline_parse_token_string_t cmd_stop_stop = 684 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop"); 685 686 cmdline_parse_inst_t cmd_stop = { 687 .f = cmd_stop_parsed, /* function to call */ 688 .data = NULL, /* 2nd arg of func */ 689 .help_str = "this command do not handle any arguments", 690 .tokens = { /* token list, NULL terminated */ 691 (void *)&cmd_stop_stop, 692 NULL, 693 }, 694 }; 695 696 struct cmd_quit_result { 697 cmdline_fixed_string_t quit; 698 }; 699 700 static void cmd_quit_parsed(__rte_unused void *parsed_result, 701 struct cmdline *cl, 702 __rte_unused void *data) 703 { 704 rte_spinlock_trylock(&global_flag_stru_p->lock); 705 if (global_flag_stru_p->LcoreMainIsRunning == 0) { 706 cmdline_printf(cl, 707 "lcore_main not running on core:%d\n", 708 global_flag_stru_p->LcoreMainCore); 709 rte_spinlock_unlock(&global_flag_stru_p->lock); 710 cmdline_quit(cl); 711 return; 712 } 713 global_flag_stru_p->LcoreMainIsRunning = 0; 714 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0) 715 cmdline_printf(cl, 716 "error: lcore_main can not stop on core:%d\n", 717 global_flag_stru_p->LcoreMainCore); 718 else 719 cmdline_printf(cl, 720 "lcore_main stopped on core:%d\n", 721 global_flag_stru_p->LcoreMainCore); 722 rte_spinlock_unlock(&global_flag_stru_p->lock); 723 cmdline_quit(cl); 724 } 725 726 cmdline_parse_token_string_t cmd_quit_quit = 727 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit"); 728 729 cmdline_parse_inst_t cmd_quit = { 730 .f = cmd_quit_parsed, /* function to call */ 731 .data = NULL, /* 2nd arg of func */ 732 .help_str = "this command do not handle any arguments", 733 .tokens = { /* token list, NULL terminated */ 734 (void *)&cmd_quit_quit, 735 NULL, 736 }, 737 }; 738 739 struct cmd_show_result { 740 cmdline_fixed_string_t show; 741 }; 742 743 static void cmd_show_parsed(__rte_unused void *parsed_result, 744 struct cmdline *cl, 745 __rte_unused void *data) 746 { 747 uint16_t slaves[16] = {0}; 748 uint8_t len = 16; 749 struct rte_ether_addr addr; 750 uint16_t i; 751 int ret; 752 753 for (i = 0; i < slaves_count; i++) { 754 ret = rte_eth_macaddr_get(i, &addr); 755 if (ret != 0) { 756 cmdline_printf(cl, 757 "Failed to get port %u MAC address: %s\n", 758 i, strerror(-ret)); 759 continue; 760 } 761 762 PRINT_MAC(addr); 763 printf("\n"); 764 } 765 766 rte_spinlock_trylock(&global_flag_stru_p->lock); 767 cmdline_printf(cl, 768 "Active_slaves:%d " 769 "packets received:Tot:%d Arp:%d IPv4:%d\n", 770 rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len), 771 global_flag_stru_p->port_packets[0], 772 global_flag_stru_p->port_packets[1], 773 global_flag_stru_p->port_packets[2]); 774 rte_spinlock_unlock(&global_flag_stru_p->lock); 775 } 776 777 cmdline_parse_token_string_t cmd_show_show = 778 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show"); 779 780 cmdline_parse_inst_t cmd_show = { 781 .f = cmd_show_parsed, /* function to call */ 782 .data = NULL, /* 2nd arg of func */ 783 .help_str = "this command do not handle any arguments", 784 .tokens = { /* token list, NULL terminated */ 785 (void *)&cmd_show_show, 786 NULL, 787 }, 788 }; 789 790 /****** CONTEXT (list of instruction) */ 791 792 cmdline_parse_ctx_t main_ctx[] = { 793 (cmdline_parse_inst_t *)&cmd_start, 794 (cmdline_parse_inst_t *)&cmd_obj_send, 795 (cmdline_parse_inst_t *)&cmd_stop, 796 (cmdline_parse_inst_t *)&cmd_show, 797 (cmdline_parse_inst_t *)&cmd_quit, 798 (cmdline_parse_inst_t *)&cmd_help, 799 NULL, 800 }; 801 802 /* prompt function, called from main on MAIN lcore */ 803 static void prompt(__rte_unused void *arg1) 804 { 805 struct cmdline *cl; 806 807 cl = cmdline_stdin_new(main_ctx, "bond6>"); 808 if (cl != NULL) { 809 cmdline_interact(cl); 810 cmdline_stdin_exit(cl); 811 } 812 } 813 814 /* Main function, does initialisation and calls the per-lcore functions */ 815 int 816 main(int argc, char *argv[]) 817 { 818 int ret, worker_core_id; 819 uint16_t nb_ports, i; 820 821 /* init EAL */ 822 ret = rte_eal_init(argc, argv); 823 rte_devargs_dump(stdout); 824 if (ret < 0) 825 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 826 argc -= ret; 827 argv += ret; 828 829 nb_ports = rte_eth_dev_count_avail(); 830 if (nb_ports == 0) 831 rte_exit(EXIT_FAILURE, "Give at least one port\n"); 832 else if (nb_ports > MAX_PORTS) 833 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n"); 834 835 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32, 836 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 837 if (mbuf_pool == NULL) 838 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 839 840 /* initialize all ports */ 841 slaves_count = nb_ports; 842 RTE_ETH_FOREACH_DEV(i) { 843 slave_port_init(i, mbuf_pool); 844 slaves[i] = i; 845 } 846 847 bond_port_init(mbuf_pool); 848 849 rte_spinlock_init(&global_flag_stru_p->lock); 850 851 /* check state of lcores */ 852 RTE_LCORE_FOREACH_WORKER(worker_core_id) { 853 if (rte_eal_get_lcore_state(worker_core_id) != WAIT) 854 return -EBUSY; 855 } 856 857 /* start lcore main on core != main_core - ARP response thread */ 858 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0); 859 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0)) 860 return -EPERM; 861 862 global_flag_stru_p->LcoreMainIsRunning = 1; 863 global_flag_stru_p->LcoreMainCore = worker_core_id; 864 printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n", 865 worker_core_id, 866 rte_eal_remote_launch((lcore_function_t *)lcore_main, 867 NULL, 868 worker_core_id), 869 BOND_IP_1, 870 BOND_IP_2, 871 BOND_IP_3, 872 BOND_IP_4 873 ); 874 875 /* Start prompt for user interact */ 876 prompt(NULL); 877 878 rte_delay_ms(100); 879 880 /* clean up the EAL */ 881 rte_eal_cleanup(); 882 883 return 0; 884 } 885