1 /******************************************************************************
3 * Copyright (c) 2019 Intel.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *******************************************************************************/
20 * @brief This file has all definitions for the Ethernet Data Interface Layer
22 * @ingroup group_lte_source_auxlib
23 * @author Intel Corporation
32 #include <sys/queue.h>
36 #include <linux/limits.h>
37 #include <sys/types.h>
41 #include <rte_config.h>
42 #include <rte_common.h>
44 #include <rte_memory.h>
45 #include <rte_memcpy.h>
46 #include <rte_memzone.h>
48 #include <rte_per_lcore.h>
49 #include <rte_launch.h>
50 #include <rte_atomic.h>
51 #include <rte_cycles.h>
52 #include <rte_prefetch.h>
53 #include <rte_lcore.h>
54 #include <rte_per_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_interrupts.h>
58 #include <rte_debug.h>
59 #include <rte_ether.h>
60 #include <rte_ethdev.h>
62 #include <rte_mempool.h>
64 #include <rte_errno.h>
70 struct rte_mempool *_eth_mbuf_pool = NULL;
71 struct rte_mempool *_eth_mbuf_pool_inderect = NULL;
72 struct rte_mempool *_eth_mbuf_pool_rx = NULL;
73 struct rte_mempool *_eth_mbuf_pool_small = NULL;
74 struct rte_mempool *_eth_mbuf_pool_big = NULL;
76 struct rte_mempool *socket_direct_pool = NULL;
77 struct rte_mempool *socket_indirect_pool = NULL;
81 * Make sure the ring indexes are big enough to cover buf space x2
82 * This ring-buffer maintains the property head - tail <= RINGSIZE.
83 * head == tail: ring buffer empty
84 * head - tail == RINGSIZE: ring buffer full
86 typedef uint16_t ring_idx;
91 char buf[1024]; /* needs power of 2! */
92 } io_ring = { {0}, 0, 0};
94 #define RINGSIZE sizeof(io_ring.buf)
95 #define RINGMASK (RINGSIZE - 1)
97 int __xran_delayed_msg(const char *fmt, ...)
102 char localbuf[RINGSIZE];
103 ring_idx old_head, new_head;
106 /* first prep a copy of the message on the local stack */
108 msg_len = vsnprintf(localbuf, RINGSIZE, fmt, ap);
111 /* atomically reserve space in the ring */
113 old_head = io_ring.head; /* snapshot head */
114 /* free always within range of [0, RINGSIZE] - proof by induction */
115 const ring_idx free = RINGSIZE - (old_head - io_ring.tail);
117 copy_len = RTE_MIN(msg_len, free);
119 return 0; /* vsnprintf error or ringbuff full. Drop log. */
121 new_head = old_head + copy_len;
122 RTE_ASSERT((ring_idx)(new_head - io_ring.tail) <= RINGSIZE);
124 if (likely(__atomic_compare_exchange_n(&io_ring.head, &old_head,
125 new_head, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)))
129 /* Now copy data in at ease. */
130 const int copy_start = (old_head & RINGMASK);
131 if (copy_start < (new_head & RINGMASK)) /* no wrap */
132 memcpy(io_ring.buf + copy_start, localbuf, copy_len);
133 else { /* wrap-around */
134 const int chunk_len = RINGSIZE - copy_start;
136 memcpy(io_ring.buf + copy_start, localbuf, chunk_len);
137 memcpy(io_ring.buf, localbuf + chunk_len, copy_len - chunk_len);
140 /* wait for previous writes to complete before updating read_head. */
141 while (io_ring.read_head != old_head)
143 io_ring.read_head = new_head;
152 * Display part of the message stored in the ring buffer.
153 * Might require multiple calls to print the full message.
154 * Will return 0 when nothing left to print.
157 int xran_show_delayed_message(void)
159 ring_idx tail = io_ring.tail;
160 ring_idx wlen = io_ring.read_head - tail; /* always within [0, RINGSIZE] */
165 tail &= RINGMASK; /* modulo the range down now that we have wlen */
167 /* Make sure we're not going over buffer end. Next call will wrap. */
168 if (tail + wlen > RINGSIZE)
169 wlen = RINGSIZE - tail;
171 RTE_ASSERT(tail + wlen <= RINGSIZE);
173 /* We use write() here to avoid recaculating string length in fwrite(). */
174 const ssize_t written = write(STDOUT_FILENO, io_ring.buf + tail, wlen);
176 return 0; /* To avoid moving tail the wrong way on error. */
178 /* Move tail up. Only we touch it. And we only print from one core. */
179 io_ring.tail += written;
181 return written; /* next invocation will print the rest if any */
185 void xran_init_mbuf_pool(void)
187 /* Init the buffer pool */
188 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
189 _eth_mbuf_pool = rte_pktmbuf_pool_create("mempool", NUM_MBUFS,
190 MBUF_CACHE, 0, MBUF_POOL_ELEMENT, rte_socket_id());
191 #ifdef XRAN_ATTACH_MBUF
192 _eth_mbuf_pool_inderect = rte_pktmbuf_pool_create("mempool_indirect", NUM_MBUFS,
193 MBUF_CACHE, 0, MBUF_POOL_ELEMENT, rte_socket_id());*/
195 _eth_mbuf_pool_rx = rte_pktmbuf_pool_create("mempool_rx", NUM_MBUFS,
196 MBUF_CACHE, 0, MBUF_POOL_ELEMENT, rte_socket_id());
197 _eth_mbuf_pool_small = rte_pktmbuf_pool_create("mempool_small",
198 NUM_MBUFS, MBUF_CACHE, 0, MBUF_POOL_ELM_SMALL, rte_socket_id());
199 _eth_mbuf_pool_big = rte_pktmbuf_pool_create("mempool_big",
200 NUM_MBUFS_BIG, 0, 0, MBUF_POOL_ELM_BIG, rte_socket_id());
202 _eth_mbuf_pool = rte_mempool_lookup("mempool");
203 _eth_mbuf_pool_inderect = rte_mempool_lookup("mempool_indirect");
204 _eth_mbuf_pool_rx = rte_mempool_lookup("mempool_rx");
205 _eth_mbuf_pool_small = rte_mempool_lookup("mempool_small");
206 _eth_mbuf_pool_big = rte_mempool_lookup("mempool_big");
208 if (_eth_mbuf_pool == NULL)
209 rte_panic("Cannot create mbuf pool: %s\n", rte_strerror(rte_errno));
210 #ifdef XRAN_ATTACH_MBUF
211 if (_eth_mbuf_pool_inderect == NULL)
212 rte_panic("Cannot create mbuf pool: %s\n", rte_strerror(rte_errno));
214 if (_eth_mbuf_pool_rx == NULL)
215 rte_panic("Cannot create mbuf pool: %s\n", rte_strerror(rte_errno));
216 if (_eth_mbuf_pool_small == NULL)
217 rte_panic("Cannot create small mbuf pool: %s\n", rte_strerror(rte_errno));
218 if (_eth_mbuf_pool_big == NULL)
219 rte_panic("Cannot create big mbuf pool: %s\n", rte_strerror(rte_errno));
221 if (socket_direct_pool == NULL)
222 socket_direct_pool = _eth_mbuf_pool;
224 if (socket_indirect_pool == NULL)
225 socket_indirect_pool = _eth_mbuf_pool_inderect;
228 /* Init NIC port, then start the port */
229 void xran_init_port(int p_id)
231 static uint16_t nb_rxd = BURST_SIZE;
232 static uint16_t nb_txd = BURST_SIZE;
233 struct rte_ether_addr addr;
234 struct rte_eth_rxmode rxmode =
235 { .split_hdr_size = 0,
236 .max_rx_pkt_len = MAX_RX_LEN,
237 .offloads=(DEV_RX_OFFLOAD_JUMBO_FRAME /*|DEV_RX_OFFLOAD_CRC_STRIP*/)
239 struct rte_eth_txmode txmode = {
240 .mq_mode = ETH_MQ_TX_NONE
242 struct rte_eth_conf port_conf = {
246 struct rte_eth_rxconf rxq_conf;
247 struct rte_eth_txconf txq_conf;
250 struct rte_eth_dev_info dev_info;
251 const char *drv_name = "";
252 int sock_id = rte_eth_dev_socket_id(p_id);
254 rte_eth_dev_info_get(p_id, &dev_info);
255 if (dev_info.driver_name)
256 drv_name = dev_info.driver_name;
257 printf("initializing port %d for TX, drv=%s\n", p_id, drv_name);
259 rte_eth_macaddr_get(p_id, &addr);
261 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
262 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
264 addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2],
265 addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5]);
268 ret = rte_eth_dev_configure(p_id, 1, 1, &port_conf);
270 rte_panic("Cannot configure port %u (%d)\n", p_id, ret);
272 ret = rte_eth_dev_adjust_nb_rx_tx_desc(p_id, &nb_rxd,&nb_txd);
276 rte_exit(EXIT_FAILURE, "Cannot adjust number of "
277 "descriptors: err=%d, port=%d\n", ret, p_id);
279 printf("Port %u: nb_rxd %d nb_txd %d\n", p_id, nb_rxd, nb_txd);
282 rxq_conf = dev_info.default_rxconf;
283 ret = rte_eth_rx_queue_setup(p_id, 0, nb_rxd,
284 sock_id, &rxq_conf, _eth_mbuf_pool_rx);
286 rte_panic("Cannot init RX for port %u (%d)\n",
290 txq_conf = dev_info.default_txconf;
291 ret = rte_eth_tx_queue_setup(p_id, 0, nb_txd, sock_id, &txq_conf);
293 rte_panic("Cannot init TX for port %u (%d)\n",
297 ret = rte_eth_dev_start(p_id);
299 rte_panic("Cannot start port %u (%d)\n", p_id, ret);
304 /* Prepend ethernet header, possibly vlan tag. */
305 void xran_add_eth_hdr_vlan(struct rte_ether_addr *dst, uint16_t ethertype, struct rte_mbuf *mb)
307 /* add in the ethernet header */
308 struct rte_ether_hdr *h = (struct rte_ether_hdr *)rte_pktmbuf_mtod(mb, struct rte_ether_hdr*);
310 PANIC_ON(h == NULL, "mbuf prepend of ether_hdr failed");
312 /* Fill in the ethernet header. */
313 rte_eth_macaddr_get(mb->port, &h->s_addr); /* set source addr */
314 h->d_addr = *dst; /* set dst addr */
315 h->ether_type = rte_cpu_to_be_16(ethertype); /* ethertype too */
317 #if defined(DPDKIO_DEBUG) && DPDKIO_DEBUG > 1
319 char dst[RTE_ETHER_ADDR_FMT_SIZE] = "(empty)";
320 char src[RTE_ETHER_ADDR_FMT_SIZE] = "(empty)";
322 printf("*** packet for TX below (len %d) ***", rte_pktmbuf_pkt_len(mb));
323 rte_ether_format_addr(src, sizeof(src), &h->s_addr);
324 rte_ether_format_addr(dst, sizeof(dst), &h->d_addr);
325 printf("src: %s dst: %s ethertype: %.4X", src, dst, ethertype);