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_atomic.h>
28 #include <rte_cycles.h>
29 #include <rte_prefetch.h>
30 #include <rte_lcore.h>
31 #include <rte_per_lcore.h>
32 #include <rte_branch_prediction.h>
33 #include <rte_interrupts.h>
34 #include <rte_random.h>
35 #include <rte_debug.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_mempool.h>
43 #include <rte_spinlock.h>
44 #include <rte_devargs.h>
45 #include <rte_byteorder.h>
46 #include <rte_cpuflags.h>
47 #include <rte_eth_bond.h>
49 #include <cmdline_rdline.h>
50 #include <cmdline_parse.h>
51 #include <cmdline_parse_num.h>
52 #include <cmdline_parse_string.h>
53 #include <cmdline_parse_ipaddr.h>
54 #include <cmdline_parse_etheraddr.h>
55 #include <cmdline_socket.h>
60 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
62 #define NB_MBUF (1024*8)
64 #define MAX_PKT_BURST 32
65 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
66 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */
69 * RX and TX Prefetch, Host, and Write-back threshold values should be
70 * carefully set for optimal performance. Consult the network
71 * controller's datasheet and supporting DPDK documentation for guidance
72 * on how these parameters should be set.
74 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
75 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
76 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
77 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */
80 * These default values are optimized for use with the Intel(R) 82599 10 GbE
81 * Controller and the DPDK ixgbe PMD. Consider using other values for other
82 * network controllers and/or network drivers.
84 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
85 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
86 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
89 * Configurable number of RX/TX ring descriptors
91 #define RTE_RX_DESC_DEFAULT 1024
92 #define RTE_TX_DESC_DEFAULT 1024
99 /* not defined under linux */
101 #define NIPQUAD_FMT "%u.%u.%u.%u"
105 #define PRINT_MAC(addr) printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \
106 ":%02"PRIx8":%02"PRIx8":%02"PRIx8, \
107 RTE_ETHER_ADDR_BYTES(&addr))
109 uint16_t slaves[RTE_MAX_ETHPORTS];
110 uint16_t slaves_count;
112 static uint16_t BOND_PORT = 0xffff;
114 static struct rte_mempool *mbuf_pool;
116 static struct rte_eth_conf port_conf = {
118 .mq_mode = ETH_MQ_RX_NONE,
119 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
125 .rss_hf = ETH_RSS_IP,
129 .mq_mode = ETH_MQ_TX_NONE,
134 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
137 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
138 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
139 struct rte_eth_dev_info dev_info;
140 struct rte_eth_rxconf rxq_conf;
141 struct rte_eth_txconf txq_conf;
142 struct rte_eth_conf local_port_conf = port_conf;
144 if (!rte_eth_dev_is_valid_port(portid))
145 rte_exit(EXIT_FAILURE, "Invalid port\n");
147 retval = rte_eth_dev_info_get(portid, &dev_info);
149 rte_exit(EXIT_FAILURE,
150 "Error during getting device (port %u) info: %s\n",
151 portid, strerror(-retval));
153 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
154 local_port_conf.txmode.offloads |=
155 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
157 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
158 dev_info.flow_type_rss_offloads;
159 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
160 port_conf.rx_adv_conf.rss_conf.rss_hf) {
161 printf("Port %u modified RSS hash function based on hardware support,"
162 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
164 port_conf.rx_adv_conf.rss_conf.rss_hf,
165 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
168 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
170 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
173 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
175 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
176 "failed (res=%d)\n", portid, retval);
179 rxq_conf = dev_info.default_rxconf;
180 rxq_conf.offloads = local_port_conf.rxmode.offloads;
181 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
182 rte_eth_dev_socket_id(portid),
186 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
190 txq_conf = dev_info.default_txconf;
191 txq_conf.offloads = local_port_conf.txmode.offloads;
192 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
193 rte_eth_dev_socket_id(portid), &txq_conf);
196 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
199 retval = rte_eth_dev_start(portid);
202 "Start port %d failed (res=%d)",
205 struct rte_ether_addr addr;
207 retval = rte_eth_macaddr_get(portid, &addr);
210 "Mac address get port %d failed (res=%d)",
213 printf("Port %u MAC: ", portid);
219 bond_port_init(struct rte_mempool *mbuf_pool)
223 uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
224 uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
225 struct rte_eth_dev_info dev_info;
226 struct rte_eth_rxconf rxq_conf;
227 struct rte_eth_txconf txq_conf;
228 struct rte_eth_conf local_port_conf = port_conf;
229 uint16_t wait_counter = 20;
231 retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
232 0 /*SOCKET_ID_ANY*/);
234 rte_exit(EXIT_FAILURE,
235 "Faled to create bond port\n");
239 retval = rte_eth_dev_info_get(BOND_PORT, &dev_info);
241 rte_exit(EXIT_FAILURE,
242 "Error during getting device (port %u) info: %s\n",
243 BOND_PORT, strerror(-retval));
245 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
246 local_port_conf.txmode.offloads |=
247 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
248 retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
250 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
253 retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
255 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
256 "failed (res=%d)\n", BOND_PORT, retval);
258 for (i = 0; i < slaves_count; i++) {
259 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
260 rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
261 slaves[i], BOND_PORT);
266 rxq_conf = dev_info.default_rxconf;
267 rxq_conf.offloads = local_port_conf.rxmode.offloads;
268 retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
269 rte_eth_dev_socket_id(BOND_PORT),
270 &rxq_conf, mbuf_pool);
272 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
276 txq_conf = dev_info.default_txconf;
277 txq_conf.offloads = local_port_conf.txmode.offloads;
278 retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
279 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
282 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
285 retval = rte_eth_dev_start(BOND_PORT);
287 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
289 printf("Waiting for slaves to become active...");
290 while (wait_counter) {
291 uint16_t act_slaves[16] = {0};
292 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) ==
299 if (--wait_counter == 0)
300 rte_exit(-1, "\nFailed to activate slaves\n");
303 retval = rte_eth_promiscuous_enable(BOND_PORT);
305 rte_exit(EXIT_FAILURE,
306 "port %u: promiscuous mode enable failed: %s\n",
307 BOND_PORT, rte_strerror(-retval));
311 struct rte_ether_addr addr;
313 retval = rte_eth_macaddr_get(BOND_PORT, &addr);
315 rte_exit(retval, "port %u: Mac address get failed (res=%d)",
318 printf("Port %u MAC: ", (unsigned)BOND_PORT);
324 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
326 size_t vlan_offset = 0;
328 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
329 struct rte_vlan_hdr *vlan_hdr =
330 (struct rte_vlan_hdr *)(eth_hdr + 1);
332 vlan_offset = sizeof(struct rte_vlan_hdr);
333 *proto = vlan_hdr->eth_proto;
335 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
336 vlan_hdr = vlan_hdr + 1;
338 *proto = vlan_hdr->eth_proto;
339 vlan_offset += sizeof(struct rte_vlan_hdr);
345 struct global_flag_stru_t {
346 int LcoreMainIsRunning;
348 uint32_t port_packets[4];
351 struct global_flag_stru_t global_flag_stru;
352 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
355 * Main thread that does the work, reading from INPUT_PORT
356 * and writing to OUTPUT_PORT
358 static int lcore_main(__rte_unused void *arg1)
360 struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
361 struct rte_ether_addr dst_addr;
363 struct rte_ether_addr bond_mac_addr;
364 struct rte_ether_hdr *eth_hdr;
365 struct rte_arp_hdr *arp_hdr;
366 struct rte_ipv4_hdr *ipv4_hdr;
367 uint16_t ether_type, offset;
375 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
376 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
378 rte_spinlock_trylock(&global_flag_stru_p->lock);
380 while (global_flag_stru_p->LcoreMainIsRunning) {
381 rte_spinlock_unlock(&global_flag_stru_p->lock);
382 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
385 /* If didn't receive any packets, wait and go to next iteration */
391 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
393 printf("Bond (port %u) MAC address get failed: %s.\n"
394 "%u packets dropped", BOND_PORT, strerror(-ret),
396 rte_pktmbuf_free(pkts[i]);
400 /* Search incoming data for ARP packets and prepare response */
401 for (i = 0; i < rx_cnt; i++) {
402 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
403 global_flag_stru_p->port_packets[0]++;
404 rte_spinlock_unlock(&global_flag_stru_p->lock);
406 eth_hdr = rte_pktmbuf_mtod(pkts[i],
407 struct rte_ether_hdr *);
408 ether_type = eth_hdr->ether_type;
409 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
410 printf("VLAN taged frame, offset:");
411 offset = get_vlan_offset(eth_hdr, ðer_type);
413 printf("%d\n", offset);
414 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
415 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
416 global_flag_stru_p->port_packets[1]++;
417 rte_spinlock_unlock(&global_flag_stru_p->lock);
419 arp_hdr = (struct rte_arp_hdr *)(
420 (char *)(eth_hdr + 1) + offset);
421 if (arp_hdr->arp_data.arp_tip == bond_ip) {
422 if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
423 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
424 /* Switch src and dst data and set bonding MAC */
425 rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr);
426 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr);
427 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
428 &arp_hdr->arp_data.arp_tha);
429 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
430 rte_ether_addr_copy(&bond_mac_addr, &dst_addr);
431 rte_ether_addr_copy(&dst_addr, &arp_hdr->arp_data.arp_sha);
432 arp_hdr->arp_data.arp_sip = bond_ip;
433 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
436 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
439 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
440 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
441 global_flag_stru_p->port_packets[2]++;
442 rte_spinlock_unlock(&global_flag_stru_p->lock);
444 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
445 if (ipv4_hdr->dst_addr == bond_ip) {
446 rte_ether_addr_copy(ð_hdr->src_addr,
448 rte_ether_addr_copy(&bond_mac_addr,
450 ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
451 ipv4_hdr->src_addr = bond_ip;
452 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
457 /* Free processed packets */
459 rte_pktmbuf_free(pkts[i]);
461 rte_spinlock_trylock(&global_flag_stru_p->lock);
463 rte_spinlock_unlock(&global_flag_stru_p->lock);
464 printf("BYE lcore_main\n");
468 struct cmd_obj_send_result {
469 cmdline_fixed_string_t action;
472 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
474 snprintf(buf, size, NIPQUAD_FMT,
475 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
476 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
477 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
478 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
481 static void cmd_obj_send_parsed(void *parsed_result,
482 __rte_unused struct cmdline *cl,
483 __rte_unused void *data)
486 struct cmd_obj_send_result *res = parsed_result;
487 char ip_str[INET6_ADDRSTRLEN];
489 struct rte_ether_addr bond_mac_addr;
490 struct rte_mbuf *created_pkt;
491 struct rte_ether_hdr *eth_hdr;
492 struct rte_arp_hdr *arp_hdr;
498 if (res->ip.family == AF_INET)
499 get_string(res, ip_str, INET_ADDRSTRLEN);
501 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
503 bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
504 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
506 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
509 "Failed to get bond (port %u) MAC address: %s\n",
510 BOND_PORT, strerror(-ret));
513 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
514 if (created_pkt == NULL) {
515 cmdline_printf(cl, "Failed to allocate mbuf\n");
519 pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
520 created_pkt->data_len = pkt_size;
521 created_pkt->pkt_len = pkt_size;
523 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
524 rte_ether_addr_copy(&bond_mac_addr, ð_hdr->src_addr);
525 memset(ð_hdr->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
526 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
528 arp_hdr = (struct rte_arp_hdr *)(
529 (char *)eth_hdr + sizeof(struct rte_ether_hdr));
530 arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
531 arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
532 arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
533 arp_hdr->arp_plen = sizeof(uint32_t);
534 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST);
536 rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha);
537 arp_hdr->arp_data.arp_sip = bond_ip;
538 memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN);
539 arp_hdr->arp_data.arp_tip =
540 ((unsigned char *)&res->ip.addr.ipv4)[0] |
541 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8) |
542 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
543 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
544 rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
547 cmdline_printf(cl, "\n");
550 cmdline_parse_token_string_t cmd_obj_action_send =
551 TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
552 cmdline_parse_token_ipaddr_t cmd_obj_ip =
553 TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
555 cmdline_parse_inst_t cmd_obj_send = {
556 .f = cmd_obj_send_parsed, /* function to call */
557 .data = NULL, /* 2nd arg of func */
558 .help_str = "send client_ip",
559 .tokens = { /* token list, NULL terminated */
560 (void *)&cmd_obj_action_send,
566 struct cmd_start_result {
567 cmdline_fixed_string_t start;
570 static void cmd_start_parsed(__rte_unused void *parsed_result,
572 __rte_unused void *data)
574 int worker_core_id = rte_lcore_id();
576 rte_spinlock_trylock(&global_flag_stru_p->lock);
577 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
578 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore)
580 rte_spinlock_unlock(&global_flag_stru_p->lock);
583 rte_spinlock_unlock(&global_flag_stru_p->lock);
585 cmdline_printf(cl, "lcore_main already running on core:%d\n",
586 global_flag_stru_p->LcoreMainCore);
587 rte_spinlock_unlock(&global_flag_stru_p->lock);
591 /* start lcore main on core != main_core - ARP response thread */
592 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
593 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0))
596 rte_spinlock_trylock(&global_flag_stru_p->lock);
597 global_flag_stru_p->LcoreMainIsRunning = 1;
598 rte_spinlock_unlock(&global_flag_stru_p->lock);
600 "Starting lcore_main on core %d:%d "
601 "Our IP:%d.%d.%d.%d\n",
603 rte_eal_remote_launch(lcore_main, NULL, worker_core_id),
611 cmdline_parse_token_string_t cmd_start_start =
612 TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
614 cmdline_parse_inst_t cmd_start = {
615 .f = cmd_start_parsed, /* function to call */
616 .data = NULL, /* 2nd arg of func */
617 .help_str = "starts listening if not started at startup",
618 .tokens = { /* token list, NULL terminated */
619 (void *)&cmd_start_start,
624 struct cmd_help_result {
625 cmdline_fixed_string_t help;
628 static void cmd_help_parsed(__rte_unused void *parsed_result,
630 __rte_unused void *data)
633 "ALB - link bonding mode 6 example\n"
634 "send IP - sends one ARPrequest through bonding for IP.\n"
635 "start - starts listening ARPs.\n"
636 "stop - stops lcore_main.\n"
637 "show - shows some bond info: ex. active slaves etc.\n"
638 "help - prints help.\n"
639 "quit - terminate all threads and quit.\n"
643 cmdline_parse_token_string_t cmd_help_help =
644 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
646 cmdline_parse_inst_t cmd_help = {
647 .f = cmd_help_parsed, /* function to call */
648 .data = NULL, /* 2nd arg of func */
649 .help_str = "show help",
650 .tokens = { /* token list, NULL terminated */
651 (void *)&cmd_help_help,
656 struct cmd_stop_result {
657 cmdline_fixed_string_t stop;
660 static void cmd_stop_parsed(__rte_unused void *parsed_result,
662 __rte_unused void *data)
664 rte_spinlock_trylock(&global_flag_stru_p->lock);
665 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
667 "lcore_main not running on core:%d\n",
668 global_flag_stru_p->LcoreMainCore);
669 rte_spinlock_unlock(&global_flag_stru_p->lock);
672 global_flag_stru_p->LcoreMainIsRunning = 0;
673 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
675 "error: lcore_main can not stop on core:%d\n",
676 global_flag_stru_p->LcoreMainCore);
679 "lcore_main stopped on core:%d\n",
680 global_flag_stru_p->LcoreMainCore);
681 rte_spinlock_unlock(&global_flag_stru_p->lock);
684 cmdline_parse_token_string_t cmd_stop_stop =
685 TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
687 cmdline_parse_inst_t cmd_stop = {
688 .f = cmd_stop_parsed, /* function to call */
689 .data = NULL, /* 2nd arg of func */
690 .help_str = "this command do not handle any arguments",
691 .tokens = { /* token list, NULL terminated */
692 (void *)&cmd_stop_stop,
697 struct cmd_quit_result {
698 cmdline_fixed_string_t quit;
701 static void cmd_quit_parsed(__rte_unused void *parsed_result,
703 __rte_unused void *data)
705 rte_spinlock_trylock(&global_flag_stru_p->lock);
706 if (global_flag_stru_p->LcoreMainIsRunning == 0) {
708 "lcore_main not running on core:%d\n",
709 global_flag_stru_p->LcoreMainCore);
710 rte_spinlock_unlock(&global_flag_stru_p->lock);
714 global_flag_stru_p->LcoreMainIsRunning = 0;
715 if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
717 "error: lcore_main can not stop on core:%d\n",
718 global_flag_stru_p->LcoreMainCore);
721 "lcore_main stopped on core:%d\n",
722 global_flag_stru_p->LcoreMainCore);
723 rte_spinlock_unlock(&global_flag_stru_p->lock);
727 cmdline_parse_token_string_t cmd_quit_quit =
728 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
730 cmdline_parse_inst_t cmd_quit = {
731 .f = cmd_quit_parsed, /* function to call */
732 .data = NULL, /* 2nd arg of func */
733 .help_str = "this command do not handle any arguments",
734 .tokens = { /* token list, NULL terminated */
735 (void *)&cmd_quit_quit,
740 struct cmd_show_result {
741 cmdline_fixed_string_t show;
744 static void cmd_show_parsed(__rte_unused void *parsed_result,
746 __rte_unused void *data)
748 uint16_t slaves[16] = {0};
750 struct rte_ether_addr addr;
754 for (i = 0; i < slaves_count; i++) {
755 ret = rte_eth_macaddr_get(i, &addr);
758 "Failed to get port %u MAC address: %s\n",
767 rte_spinlock_trylock(&global_flag_stru_p->lock);
770 "packets received:Tot:%d Arp:%d IPv4:%d\n",
771 rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
772 global_flag_stru_p->port_packets[0],
773 global_flag_stru_p->port_packets[1],
774 global_flag_stru_p->port_packets[2]);
775 rte_spinlock_unlock(&global_flag_stru_p->lock);
778 cmdline_parse_token_string_t cmd_show_show =
779 TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
781 cmdline_parse_inst_t cmd_show = {
782 .f = cmd_show_parsed, /* function to call */
783 .data = NULL, /* 2nd arg of func */
784 .help_str = "this command do not handle any arguments",
785 .tokens = { /* token list, NULL terminated */
786 (void *)&cmd_show_show,
791 /****** CONTEXT (list of instruction) */
793 cmdline_parse_ctx_t main_ctx[] = {
794 (cmdline_parse_inst_t *)&cmd_start,
795 (cmdline_parse_inst_t *)&cmd_obj_send,
796 (cmdline_parse_inst_t *)&cmd_stop,
797 (cmdline_parse_inst_t *)&cmd_show,
798 (cmdline_parse_inst_t *)&cmd_quit,
799 (cmdline_parse_inst_t *)&cmd_help,
803 /* prompt function, called from main on MAIN lcore */
804 static void prompt(__rte_unused void *arg1)
808 cl = cmdline_stdin_new(main_ctx, "bond6>");
810 cmdline_interact(cl);
811 cmdline_stdin_exit(cl);
815 /* Main function, does initialisation and calls the per-lcore functions */
817 main(int argc, char *argv[])
819 int ret, worker_core_id;
820 uint16_t nb_ports, i;
823 ret = rte_eal_init(argc, argv);
824 rte_devargs_dump(stdout);
826 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
830 nb_ports = rte_eth_dev_count_avail();
832 rte_exit(EXIT_FAILURE, "Give at least one port\n");
833 else if (nb_ports > MAX_PORTS)
834 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
836 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
837 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
838 if (mbuf_pool == NULL)
839 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
841 /* initialize all ports */
842 slaves_count = nb_ports;
843 RTE_ETH_FOREACH_DEV(i) {
844 slave_port_init(i, mbuf_pool);
848 bond_port_init(mbuf_pool);
850 rte_spinlock_init(&global_flag_stru_p->lock);
852 /* check state of lcores */
853 RTE_LCORE_FOREACH_WORKER(worker_core_id) {
854 if (rte_eal_get_lcore_state(worker_core_id) != WAIT)
858 /* start lcore main on core != main_core - ARP response thread */
859 worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
860 if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0))
863 global_flag_stru_p->LcoreMainIsRunning = 1;
864 global_flag_stru_p->LcoreMainCore = worker_core_id;
865 printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
867 rte_eal_remote_launch((lcore_function_t *)lcore_main,
876 /* Start prompt for user interact */
881 /* clean up the EAL */