1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
5 #include "l2fwd_poll.h"
8 l2fwd_poll_simple_forward(struct l2fwd_resources *rsrc, struct rte_mbuf *m,
11 struct rte_eth_dev_tx_buffer *buffer;
15 dst_port = rsrc->dst_ports[portid];
17 if (rsrc->mac_updating)
18 l2fwd_mac_updating(m, dst_port, &rsrc->eth_addr[dst_port]);
20 buffer = ((struct l2fwd_poll_resources *)rsrc->poll_rsrc)->tx_buffer[
22 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
24 rsrc->port_stats[dst_port].tx += sent;
27 /* main poll mode processing loop */
29 l2fwd_poll_main_loop(struct l2fwd_resources *rsrc)
31 uint64_t prev_tsc, diff_tsc, cur_tsc, drain_tsc;
32 struct l2fwd_poll_resources *poll_rsrc = rsrc->poll_rsrc;
33 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
34 struct rte_eth_dev_tx_buffer *buf;
35 struct lcore_queue_conf *qconf;
36 uint32_t i, j, port_id, nb_rx;
41 drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
45 lcore_id = rte_lcore_id();
46 qconf = &poll_rsrc->lcore_queue_conf[lcore_id];
48 if (qconf->n_rx_port == 0) {
49 printf("lcore %u has nothing to do\n", lcore_id);
53 printf("entering main loop on lcore %u\n", lcore_id);
55 for (i = 0; i < qconf->n_rx_port; i++) {
57 port_id = qconf->rx_port_list[i];
58 printf(" -- lcoreid=%u port_id=%u\n", lcore_id, port_id);
62 while (!rsrc->force_quit) {
64 /* Draining TX queue in main loop. 8< */
65 cur_tsc = rte_rdtsc();
68 * TX burst queue drain
70 diff_tsc = cur_tsc - prev_tsc;
71 if (unlikely(diff_tsc > drain_tsc)) {
72 for (i = 0; i < qconf->n_rx_port; i++) {
74 rsrc->dst_ports[qconf->rx_port_list[i]];
75 buf = poll_rsrc->tx_buffer[port_id];
76 sent = rte_eth_tx_buffer_flush(port_id, 0, buf);
78 rsrc->port_stats[port_id].tx += sent;
83 /* >8 End of draining TX queue in main loop. */
85 /* Reading ingress packets. 8< */
87 /* Read packet from RX queues */
88 for (i = 0; i < qconf->n_rx_port; i++) {
90 port_id = qconf->rx_port_list[i];
91 nb_rx = rte_eth_rx_burst(port_id, 0, pkts_burst,
94 rsrc->port_stats[port_id].rx += nb_rx;
96 for (j = 0; j < nb_rx; j++) {
98 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
99 l2fwd_poll_simple_forward(rsrc, m, port_id);
102 /* >8 End of reading ingress packets. */
107 l2fwd_poll_lcore_config(struct l2fwd_resources *rsrc)
109 struct l2fwd_poll_resources *poll_rsrc = rsrc->poll_rsrc;
110 struct lcore_queue_conf *qconf = NULL;
111 uint32_t rx_lcore_id = 0;
114 /* Initialize the port/queue configuration of each logical core */
115 RTE_ETH_FOREACH_DEV(port_id) {
116 /* skip ports that are not enabled */
117 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
120 /* get the lcore_id for this port */
121 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
122 rx_lcore_id == rte_get_main_lcore() ||
123 poll_rsrc->lcore_queue_conf[rx_lcore_id].n_rx_port ==
124 rsrc->rx_queue_per_lcore) {
126 if (rx_lcore_id >= RTE_MAX_LCORE)
127 rte_panic("Not enough cores\n");
130 if (qconf != &poll_rsrc->lcore_queue_conf[rx_lcore_id]) {
131 /* Assigned a new logical core in the loop above. */
132 qconf = &poll_rsrc->lcore_queue_conf[rx_lcore_id];
135 qconf->rx_port_list[qconf->n_rx_port] = port_id;
137 printf("Lcore %u: RX port %u\n", rx_lcore_id, port_id);
142 l2fwd_poll_init_tx_buffers(struct l2fwd_resources *rsrc)
144 struct l2fwd_poll_resources *poll_rsrc = rsrc->poll_rsrc;
148 RTE_ETH_FOREACH_DEV(port_id) {
149 /* Initialize TX buffers */
150 poll_rsrc->tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
151 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
152 rte_eth_dev_socket_id(port_id));
153 if (poll_rsrc->tx_buffer[port_id] == NULL)
154 rte_panic("Cannot allocate buffer for tx on port %u\n",
157 rte_eth_tx_buffer_init(poll_rsrc->tx_buffer[port_id],
160 ret = rte_eth_tx_buffer_set_err_callback(
161 poll_rsrc->tx_buffer[port_id],
162 rte_eth_tx_buffer_count_callback,
163 &rsrc->port_stats[port_id].dropped);
165 rte_panic("Cannot set error callback for tx buffer on port %u\n",
171 l2fwd_poll_resource_setup(struct l2fwd_resources *rsrc)
173 struct l2fwd_poll_resources *poll_rsrc;
175 poll_rsrc = rte_zmalloc("l2fwd_poll_rsrc",
176 sizeof(struct l2fwd_poll_resources), 0);
177 if (poll_rsrc == NULL)
178 rte_panic("Failed to allocate resources for l2fwd poll mode\n");
180 rsrc->poll_rsrc = poll_rsrc;
181 l2fwd_poll_lcore_config(rsrc);
182 l2fwd_poll_init_tx_buffers(rsrc);
184 poll_rsrc->poll_main_loop = l2fwd_poll_main_loop;