1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
7 #include <sys/socket.h>
21 #include <rte_common.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.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>
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>
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>
59 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
61 #define NB_MBUF (1024*8)
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 */
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.
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. */
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.
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. */
88 * Configurable number of RX/TX ring descriptors
90 #define RTE_RX_DESC_DEFAULT 1024
91 #define RTE_TX_DESC_DEFAULT 1024
98 /* not defined under linux */
100 #define NIPQUAD_FMT "%u.%u.%u.%u"
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))
108 uint16_t slaves[RTE_MAX_ETHPORTS];
109 uint16_t slaves_count;
111 static uint16_t BOND_PORT = 0xffff;
113 static struct rte_mempool *mbuf_pool;
115 static struct rte_eth_conf port_conf = {
117 .mq_mode = RTE_ETH_MQ_RX_NONE,
123 .rss_hf = RTE_ETH_RSS_IP,
127 .mq_mode = RTE_ETH_MQ_TX_NONE,
132 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
135 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
136 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
137 struct rte_eth_dev_info dev_info;
138 struct rte_eth_rxconf rxq_conf;
139 struct rte_eth_txconf txq_conf;
140 struct rte_eth_conf local_port_conf = port_conf;
142 if (!rte_eth_dev_is_valid_port(portid))
143 rte_exit(EXIT_FAILURE, "Invalid port\n");
145 retval = rte_eth_dev_info_get(portid, &dev_info);
147 rte_exit(EXIT_FAILURE,
148 "Error during getting device (port %u) info: %s\n",
149 portid, strerror(-retval));
151 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
152 local_port_conf.txmode.offloads |=
153 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
155 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
156 dev_info.flow_type_rss_offloads;
157 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
158 port_conf.rx_adv_conf.rss_conf.rss_hf) {
159 printf("Port %u modified RSS hash function based on hardware support,"
160 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
162 port_conf.rx_adv_conf.rss_conf.rss_hf,
163 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
166 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
168 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
171 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
173 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
174 "failed (res=%d)\n", portid, retval);
177 rxq_conf = dev_info.default_rxconf;
178 rxq_conf.offloads = local_port_conf.rxmode.offloads;
179 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
180 rte_eth_dev_socket_id(portid),
184 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
188 txq_conf = dev_info.default_txconf;
189 txq_conf.offloads = local_port_conf.txmode.offloads;
190 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
191 rte_eth_dev_socket_id(portid), &txq_conf);
194 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
197 retval = rte_eth_dev_start(portid);
200 "Start port %d failed (res=%d)",
203 struct rte_ether_addr addr;
205 retval = rte_eth_macaddr_get(portid, &addr);
208 "Mac address get port %d failed (res=%d)",
211 printf("Port %u MAC: ", portid);
217 bond_port_init(struct rte_mempool *mbuf_pool)
221 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
222 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
223 struct rte_eth_dev_info dev_info;
224 struct rte_eth_rxconf rxq_conf;
225 struct rte_eth_txconf txq_conf;
226 struct rte_eth_conf local_port_conf = port_conf;
227 uint16_t wait_counter = 20;
229 retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
230 0 /*SOCKET_ID_ANY*/);
232 rte_exit(EXIT_FAILURE,
233 "Faled to create bond port\n");
237 retval = rte_eth_dev_info_get(BOND_PORT, &dev_info);
239 rte_exit(EXIT_FAILURE,
240 "Error during getting device (port %u) info: %s\n",
241 BOND_PORT, strerror(-retval));
243 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
244 local_port_conf.txmode.offloads |=
245 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
246 retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
248 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
251 retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
253 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
254 "failed (res=%d)\n", BOND_PORT, retval);
256 for (i = 0; i < slaves_count; i++) {
257 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
258 rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
259 slaves[i], BOND_PORT);
264 rxq_conf = dev_info.default_rxconf;
265 rxq_conf.offloads = local_port_conf.rxmode.offloads;
266 retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
267 rte_eth_dev_socket_id(BOND_PORT),
268 &rxq_conf, mbuf_pool);
270 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
274 txq_conf = dev_info.default_txconf;
275 txq_conf.offloads = local_port_conf.txmode.offloads;
276 retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
277 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
280 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
283 retval = rte_eth_dev_start(BOND_PORT);
285 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
287 printf("Waiting for slaves to become active...");
288 while (wait_counter) {
289 uint16_t act_slaves[16] = {0};
290 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) ==
297 if (--wait_counter == 0)
298 rte_exit(-1, "\nFailed to activate slaves\n");
301 retval = rte_eth_promiscuous_enable(BOND_PORT);
303 rte_exit(EXIT_FAILURE,
304 "port %u: promiscuous mode enable failed: %s\n",
305 BOND_PORT, rte_strerror(-retval));
309 struct rte_ether_addr addr;
311 retval = rte_eth_macaddr_get(BOND_PORT, &addr);
313 rte_exit(retval, "port %u: Mac address get failed (res=%d)",
316 printf("Port %u MAC: ", (unsigned)BOND_PORT);
322 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
324 size_t vlan_offset = 0;
326 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
327 struct rte_vlan_hdr *vlan_hdr =
328 (struct rte_vlan_hdr *)(eth_hdr + 1);
330 vlan_offset = sizeof(struct rte_vlan_hdr);
331 *proto = vlan_hdr->eth_proto;
333 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
334 vlan_hdr = vlan_hdr + 1;
336 *proto = vlan_hdr->eth_proto;
337 vlan_offset += sizeof(struct rte_vlan_hdr);
343 struct global_flag_stru_t {
344 int LcoreMainIsRunning;
346 uint32_t port_packets[4];
349 struct global_flag_stru_t global_flag_stru;
350 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
353 * Main thread that does the work, reading from INPUT_PORT
354 * and writing to OUTPUT_PORT
356 static int lcore_main(__rte_unused void *arg1)
358 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
359 struct rte_ether_addr dst_addr;
361 struct rte_ether_addr bond_mac_addr;
362 struct rte_ether_hdr *eth_hdr;
363 struct rte_arp_hdr *arp_hdr;
364 struct rte_ipv4_hdr *ipv4_hdr;
365 uint16_t ether_type, offset;
373 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
374 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
376 rte_spinlock_trylock(&global_flag_stru_p->lock);
378 while (global_flag_stru_p->LcoreMainIsRunning) {
379 rte_spinlock_unlock(&global_flag_stru_p->lock);
380 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
383 /* If didn't receive any packets, wait and go to next iteration */
389 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
391 printf("Bond (port %u) MAC address get failed: %s.\n"
392 "%u packets dropped", BOND_PORT, strerror(-ret),
394 rte_pktmbuf_free(pkts[i]);
398 /* Search incoming data for ARP packets and prepare response */
399 for (i = 0; i < rx_cnt; i++) {
400 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
401 global_flag_stru_p->port_packets[0]++;
402 rte_spinlock_unlock(&global_flag_stru_p->lock);
404 eth_hdr = rte_pktmbuf_mtod(pkts[i],
405 struct rte_ether_hdr *);
406 ether_type = eth_hdr->ether_type;
407 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
408 printf("VLAN taged frame, offset:");
409 offset = get_vlan_offset(eth_hdr, ðer_type);
411 printf("%d\n", offset);
412 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
413 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
414 global_flag_stru_p->port_packets[1]++;
415 rte_spinlock_unlock(&global_flag_stru_p->lock);
417 arp_hdr = (struct rte_arp_hdr *)(
418 (char *)(eth_hdr + 1) + offset);
419 if (arp_hdr->arp_data.arp_tip == bond_ip) {
420 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
421 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
422 /* Switch src and dst data and set bonding MAC */
423 rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr);
424 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr);
425 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
426 &arp_hdr->arp_data.arp_tha);
427 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
428 rte_ether_addr_copy(&bond_mac_addr, &dst_addr);
429 rte_ether_addr_copy(&dst_addr, &arp_hdr->arp_data.arp_sha);
430 arp_hdr->arp_data.arp_sip = bond_ip;
431 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
434 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
437 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
438 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
439 global_flag_stru_p->port_packets[2]++;
440 rte_spinlock_unlock(&global_flag_stru_p->lock);
442 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
443 if (ipv4_hdr->dst_addr == bond_ip) {
444 rte_ether_addr_copy(ð_hdr->src_addr,
446 rte_ether_addr_copy(&bond_mac_addr,
448 ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
449 ipv4_hdr->src_addr = bond_ip;
450 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
455 /* Free processed packets */
457 rte_pktmbuf_free(pkts[i]);
459 rte_spinlock_trylock(&global_flag_stru_p->lock);
461 rte_spinlock_unlock(&global_flag_stru_p->lock);
462 printf("BYE lcore_main\n");
466 struct cmd_obj_send_result {
467 cmdline_fixed_string_t action;
470 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
472 snprintf(buf, size, NIPQUAD_FMT,
473 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
474 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
475 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
476 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
479 static void cmd_obj_send_parsed(void *parsed_result,
480 __rte_unused struct cmdline *cl,
481 __rte_unused void *data)
484 struct cmd_obj_send_result *res = parsed_result;
485 char ip_str[INET6_ADDRSTRLEN];
487 struct rte_ether_addr bond_mac_addr;
488 struct rte_mbuf *created_pkt;
489 struct rte_ether_hdr *eth_hdr;
490 struct rte_arp_hdr *arp_hdr;
496 if (res->ip.family == AF_INET)
497 get_string(res, ip_str, INET_ADDRSTRLEN);
499 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
501 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
502 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
504 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
507 "Failed to get bond (port %u) MAC address: %s\n",
508 BOND_PORT, strerror(-ret));
511 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
512 if (created_pkt == NULL) {
513 cmdline_printf(cl, "Failed to allocate mbuf\n");
517 pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
518 created_pkt->data_len = pkt_size;
519 created_pkt->pkt_len = pkt_size;
521 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
522 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr);
523 memset(ð_hdr->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
524 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
526 arp_hdr = (struct rte_arp_hdr *)(
527 (char *)eth_hdr + sizeof(struct rte_ether_hdr));
528 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
529 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
530 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
531 arp_hdr->arp_plen = sizeof(uint32_t);
532 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST);
534 rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha);
535 arp_hdr->arp_data.arp_sip = bond_ip;
536 memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN);
537 arp_hdr->arp_data.arp_tip =
538 ((unsigned char *)&res->ip.addr.ipv4)[0] |
539 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8) |
540 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
541 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
542 rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
545 cmdline_printf(cl, "\n");
548 cmdline_parse_token_string_t cmd_obj_action_send =
549 TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
550 cmdline_parse_token_ipaddr_t cmd_obj_ip =
551 TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
553 cmdline_parse_inst_t cmd_obj_send = {
554 .f = cmd_obj_send_parsed, /* function to call */
555 .data = NULL, /* 2nd arg of func */
556 .help_str = "send client_ip",
557 .tokens = { /* token list, NULL terminated */
558 (void *)&cmd_obj_action_send,
564 struct cmd_start_result {
565 cmdline_fixed_string_t start;
568 static void cmd_start_parsed(__rte_unused void *parsed_result,
570 __rte_unused void *data)
572 int worker_core_id = rte_lcore_id();
574 rte_spinlock_trylock(&global_flag_stru_p->lock);
575 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
576 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore)
578 rte_spinlock_unlock(&global_flag_stru_p->lock);
581 rte_spinlock_unlock(&global_flag_stru_p->lock);
583 cmdline_printf(cl, "lcore_main already running on core:%d\n",
584 global_flag_stru_p->LcoreMainCore);
585 rte_spinlock_unlock(&global_flag_stru_p->lock);
589 /* start lcore main on core != main_core - ARP response thread */
590 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
591 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0))
594 rte_spinlock_trylock(&global_flag_stru_p->lock);
595 global_flag_stru_p->LcoreMainIsRunning = 1;
596 rte_spinlock_unlock(&global_flag_stru_p->lock);
598 "Starting lcore_main on core %d:%d "
599 "Our IP:%d.%d.%d.%d\n",
601 rte_eal_remote_launch(lcore_main, NULL, worker_core_id),
609 cmdline_parse_token_string_t cmd_start_start =
610 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
612 cmdline_parse_inst_t cmd_start = {
613 .f = cmd_start_parsed, /* function to call */
614 .data = NULL, /* 2nd arg of func */
615 .help_str = "starts listening if not started at startup",
616 .tokens = { /* token list, NULL terminated */
617 (void *)&cmd_start_start,
622 struct cmd_help_result {
623 cmdline_fixed_string_t help;
626 static void cmd_help_parsed(__rte_unused void *parsed_result,
628 __rte_unused void *data)
631 "ALB - link bonding mode 6 example\n"
632 "send IP - sends one ARPrequest through bonding for IP.\n"
633 "start - starts listening ARPs.\n"
634 "stop - stops lcore_main.\n"
635 "show - shows some bond info: ex. active slaves etc.\n"
636 "help - prints help.\n"
637 "quit - terminate all threads and quit.\n"
641 cmdline_parse_token_string_t cmd_help_help =
642 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
644 cmdline_parse_inst_t cmd_help = {
645 .f = cmd_help_parsed, /* function to call */
646 .data = NULL, /* 2nd arg of func */
647 .help_str = "show help",
648 .tokens = { /* token list, NULL terminated */
649 (void *)&cmd_help_help,
654 struct cmd_stop_result {
655 cmdline_fixed_string_t stop;
658 static void cmd_stop_parsed(__rte_unused void *parsed_result,
660 __rte_unused void *data)
662 rte_spinlock_trylock(&global_flag_stru_p->lock);
663 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
665 "lcore_main not running on core:%d\n",
666 global_flag_stru_p->LcoreMainCore);
667 rte_spinlock_unlock(&global_flag_stru_p->lock);
670 global_flag_stru_p->LcoreMainIsRunning = 0;
671 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
673 "error: lcore_main can not stop on core:%d\n",
674 global_flag_stru_p->LcoreMainCore);
677 "lcore_main stopped on core:%d\n",
678 global_flag_stru_p->LcoreMainCore);
679 rte_spinlock_unlock(&global_flag_stru_p->lock);
682 cmdline_parse_token_string_t cmd_stop_stop =
683 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
685 cmdline_parse_inst_t cmd_stop = {
686 .f = cmd_stop_parsed, /* function to call */
687 .data = NULL, /* 2nd arg of func */
688 .help_str = "this command do not handle any arguments",
689 .tokens = { /* token list, NULL terminated */
690 (void *)&cmd_stop_stop,
695 struct cmd_quit_result {
696 cmdline_fixed_string_t quit;
699 static void cmd_quit_parsed(__rte_unused void *parsed_result,
701 __rte_unused void *data)
703 rte_spinlock_trylock(&global_flag_stru_p->lock);
704 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
706 "lcore_main not running on core:%d\n",
707 global_flag_stru_p->LcoreMainCore);
708 rte_spinlock_unlock(&global_flag_stru_p->lock);
712 global_flag_stru_p->LcoreMainIsRunning = 0;
713 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
715 "error: lcore_main can not stop on core:%d\n",
716 global_flag_stru_p->LcoreMainCore);
719 "lcore_main stopped on core:%d\n",
720 global_flag_stru_p->LcoreMainCore);
721 rte_spinlock_unlock(&global_flag_stru_p->lock);
725 cmdline_parse_token_string_t cmd_quit_quit =
726 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
728 cmdline_parse_inst_t cmd_quit = {
729 .f = cmd_quit_parsed, /* function to call */
730 .data = NULL, /* 2nd arg of func */
731 .help_str = "this command do not handle any arguments",
732 .tokens = { /* token list, NULL terminated */
733 (void *)&cmd_quit_quit,
738 struct cmd_show_result {
739 cmdline_fixed_string_t show;
742 static void cmd_show_parsed(__rte_unused void *parsed_result,
744 __rte_unused void *data)
746 uint16_t slaves[16] = {0};
748 struct rte_ether_addr addr;
752 for (i = 0; i < slaves_count; i++) {
753 ret = rte_eth_macaddr_get(i, &addr);
756 "Failed to get port %u MAC address: %s\n",
765 rte_spinlock_trylock(&global_flag_stru_p->lock);
768 "packets received:Tot:%d Arp:%d IPv4:%d\n",
769 rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
770 global_flag_stru_p->port_packets[0],
771 global_flag_stru_p->port_packets[1],
772 global_flag_stru_p->port_packets[2]);
773 rte_spinlock_unlock(&global_flag_stru_p->lock);
776 cmdline_parse_token_string_t cmd_show_show =
777 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
779 cmdline_parse_inst_t cmd_show = {
780 .f = cmd_show_parsed, /* function to call */
781 .data = NULL, /* 2nd arg of func */
782 .help_str = "this command do not handle any arguments",
783 .tokens = { /* token list, NULL terminated */
784 (void *)&cmd_show_show,
789 /****** CONTEXT (list of instruction) */
791 cmdline_parse_ctx_t main_ctx[] = {
792 (cmdline_parse_inst_t *)&cmd_start,
793 (cmdline_parse_inst_t *)&cmd_obj_send,
794 (cmdline_parse_inst_t *)&cmd_stop,
795 (cmdline_parse_inst_t *)&cmd_show,
796 (cmdline_parse_inst_t *)&cmd_quit,
797 (cmdline_parse_inst_t *)&cmd_help,
801 /* prompt function, called from main on MAIN lcore */
802 static void prompt(__rte_unused void *arg1)
806 cl = cmdline_stdin_new(main_ctx, "bond6>");
808 cmdline_interact(cl);
809 cmdline_stdin_exit(cl);
813 /* Main function, does initialisation and calls the per-lcore functions */
815 main(int argc, char *argv[])
817 int ret, worker_core_id;
818 uint16_t nb_ports, i;
821 ret = rte_eal_init(argc, argv);
822 rte_devargs_dump(stdout);
824 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
828 nb_ports = rte_eth_dev_count_avail();
830 rte_exit(EXIT_FAILURE, "Give at least one port\n");
831 else if (nb_ports > MAX_PORTS)
832 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
834 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
835 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
836 if (mbuf_pool == NULL)
837 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
839 /* initialize all ports */
840 slaves_count = nb_ports;
841 RTE_ETH_FOREACH_DEV(i) {
842 slave_port_init(i, mbuf_pool);
846 bond_port_init(mbuf_pool);
848 rte_spinlock_init(&global_flag_stru_p->lock);
850 /* check state of lcores */
851 RTE_LCORE_FOREACH_WORKER(worker_core_id) {
852 if (rte_eal_get_lcore_state(worker_core_id) != WAIT)
856 /* start lcore main on core != main_core - ARP response thread */
857 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
858 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0))
861 global_flag_stru_p->LcoreMainIsRunning = 1;
862 global_flag_stru_p->LcoreMainCore = worker_core_id;
863 printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
865 rte_eal_remote_launch((lcore_function_t *)lcore_main,
874 /* Start prompt for user interact */
879 /* clean up the EAL */