1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018-2019 Cisco Systems, Inc. All rights reserved.
9 #include <sys/socket.h>
11 #include <sys/ioctl.h>
13 #include <linux/if_ether.h>
15 #include <sys/eventfd.h>
17 #include <rte_version.h>
19 #include <rte_ether.h>
20 #include <ethdev_driver.h>
21 #include <ethdev_vdev.h>
22 #include <rte_malloc.h>
23 #include <rte_kvargs.h>
24 #include <rte_bus_vdev.h>
25 #include <rte_string_fns.h>
26 #include <rte_errno.h>
27 #include <rte_memory.h>
28 #include <rte_memzone.h>
29 #include <rte_eal_memconfig.h>
31 #include "rte_eth_memif.h"
32 #include "memif_socket.h"
34 #define ETH_MEMIF_ID_ARG "id"
35 #define ETH_MEMIF_ROLE_ARG "role"
36 #define ETH_MEMIF_PKT_BUFFER_SIZE_ARG "bsize"
37 #define ETH_MEMIF_RING_SIZE_ARG "rsize"
38 #define ETH_MEMIF_SOCKET_ARG "socket"
39 #define ETH_MEMIF_SOCKET_ABSTRACT_ARG "socket-abstract"
40 #define ETH_MEMIF_MAC_ARG "mac"
41 #define ETH_MEMIF_ZC_ARG "zero-copy"
42 #define ETH_MEMIF_SECRET_ARG "secret"
44 static const char * const valid_arguments[] = {
47 ETH_MEMIF_PKT_BUFFER_SIZE_ARG,
48 ETH_MEMIF_RING_SIZE_ARG,
50 ETH_MEMIF_SOCKET_ABSTRACT_ARG,
57 static const struct rte_eth_link pmd_link = {
58 .link_speed = ETH_SPEED_NUM_10G,
59 .link_duplex = ETH_LINK_FULL_DUPLEX,
60 .link_status = ETH_LINK_DOWN,
61 .link_autoneg = ETH_LINK_AUTONEG
64 #define MEMIF_MP_SEND_REGION "memif_mp_send_region"
67 static int memif_region_init_zc(const struct rte_memseg_list *msl,
68 const struct rte_memseg *ms, void *arg);
73 return ("memif-" RTE_STR(MEMIF_VERSION_MAJOR) "." RTE_STR(MEMIF_VERSION_MINOR));
76 /* Message header to synchronize regions */
77 struct mp_region_msg {
78 char port_name[RTE_DEV_NAME_MAX_LEN];
79 memif_region_index_t idx;
80 memif_region_size_t size;
84 memif_mp_send_region(const struct rte_mp_msg *msg, const void *peer)
86 struct rte_eth_dev *dev;
87 struct pmd_process_private *proc_private;
88 const struct mp_region_msg *msg_param = (const struct mp_region_msg *)msg->param;
89 struct rte_mp_msg reply;
90 struct mp_region_msg *reply_param = (struct mp_region_msg *)reply.param;
94 /* Get requested port */
95 ret = rte_eth_dev_get_port_by_name(msg_param->port_name, &port_id);
97 MIF_LOG(ERR, "Failed to get port id for %s",
98 msg_param->port_name);
101 dev = &rte_eth_devices[port_id];
102 proc_private = dev->process_private;
104 memset(&reply, 0, sizeof(reply));
105 strlcpy(reply.name, msg->name, sizeof(reply.name));
106 reply_param->idx = msg_param->idx;
107 if (proc_private->regions[msg_param->idx] != NULL) {
108 reply_param->size = proc_private->regions[msg_param->idx]->region_size;
109 reply.fds[0] = proc_private->regions[msg_param->idx]->fd;
112 reply.len_param = sizeof(*reply_param);
113 if (rte_mp_reply(&reply, peer) < 0) {
114 MIF_LOG(ERR, "Failed to reply to an add region request");
123 * Called by secondary process, when ports link status goes up.
126 memif_mp_request_regions(struct rte_eth_dev *dev)
129 struct timespec timeout = {.tv_sec = 5, .tv_nsec = 0};
130 struct rte_mp_msg msg, *reply;
131 struct rte_mp_reply replies;
132 struct mp_region_msg *msg_param = (struct mp_region_msg *)msg.param;
133 struct mp_region_msg *reply_param;
134 struct memif_region *r;
135 struct pmd_process_private *proc_private = dev->process_private;
136 struct pmd_internals *pmd = dev->data->dev_private;
137 /* in case of zero-copy client, only request region 0 */
138 uint16_t max_region_num = (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) ?
139 1 : ETH_MEMIF_MAX_REGION_NUM;
141 MIF_LOG(DEBUG, "Requesting memory regions");
143 for (i = 0; i < max_region_num; i++) {
144 /* Prepare the message */
145 memset(&msg, 0, sizeof(msg));
146 strlcpy(msg.name, MEMIF_MP_SEND_REGION, sizeof(msg.name));
147 strlcpy(msg_param->port_name, dev->data->name,
148 sizeof(msg_param->port_name));
150 msg.len_param = sizeof(*msg_param);
153 ret = rte_mp_request_sync(&msg, &replies, &timeout);
154 if (ret < 0 || replies.nb_received != 1) {
155 MIF_LOG(ERR, "Failed to send mp msg: %d",
160 reply = &replies.msgs[0];
161 reply_param = (struct mp_region_msg *)reply->param;
163 if (reply_param->size > 0) {
164 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
166 MIF_LOG(ERR, "Failed to alloc memif region.");
170 r->region_size = reply_param->size;
171 if (reply->num_fds < 1) {
172 MIF_LOG(ERR, "Missing file descriptor.");
176 r->fd = reply->fds[0];
179 proc_private->regions[reply_param->idx] = r;
180 proc_private->regions_num++;
185 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
186 ret = rte_memseg_walk(memif_region_init_zc, (void *)proc_private);
191 return memif_connect(dev);
195 memif_dev_info(struct rte_eth_dev *dev __rte_unused, struct rte_eth_dev_info *dev_info)
197 dev_info->max_mac_addrs = 1;
198 dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
199 dev_info->max_rx_queues = ETH_MEMIF_MAX_NUM_Q_PAIRS;
200 dev_info->max_tx_queues = ETH_MEMIF_MAX_NUM_Q_PAIRS;
201 dev_info->min_rx_bufsize = 0;
202 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS;
207 static memif_ring_t *
208 memif_get_ring(struct pmd_internals *pmd, struct pmd_process_private *proc_private,
209 memif_ring_type_t type, uint16_t ring_num)
211 /* rings only in region 0 */
212 void *p = proc_private->regions[0]->addr;
213 int ring_size = sizeof(memif_ring_t) + sizeof(memif_desc_t) *
214 (1 << pmd->run.log2_ring_size);
216 p = (uint8_t *)p + (ring_num + type * pmd->run.num_c2s_rings) * ring_size;
218 return (memif_ring_t *)p;
221 static memif_region_offset_t
222 memif_get_ring_offset(struct rte_eth_dev *dev, struct memif_queue *mq,
223 memif_ring_type_t type, uint16_t num)
225 struct pmd_internals *pmd = dev->data->dev_private;
226 struct pmd_process_private *proc_private = dev->process_private;
228 return ((uint8_t *)memif_get_ring(pmd, proc_private, type, num) -
229 (uint8_t *)proc_private->regions[mq->region]->addr);
232 static memif_ring_t *
233 memif_get_ring_from_queue(struct pmd_process_private *proc_private,
234 struct memif_queue *mq)
236 struct memif_region *r;
238 r = proc_private->regions[mq->region];
242 return (memif_ring_t *)((uint8_t *)r->addr + mq->ring_offset);
246 memif_get_buffer(struct pmd_process_private *proc_private, memif_desc_t *d)
248 return ((uint8_t *)proc_private->regions[d->region]->addr + d->offset);
251 /* Free mbufs received by server */
253 memif_free_stored_mbufs(struct pmd_process_private *proc_private, struct memif_queue *mq)
256 uint16_t mask = (1 << mq->log2_ring_size) - 1;
257 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
259 /* FIXME: improve performance */
260 /* The ring->tail acts as a guard variable between Tx and Rx
261 * threads, so using load-acquire pairs with store-release
262 * in function eth_memif_rx for C2S queues.
264 cur_tail = __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE);
265 while (mq->last_tail != cur_tail) {
266 RTE_MBUF_PREFETCH_TO_FREE(mq->buffers[(mq->last_tail + 1) & mask]);
267 /* Decrement refcnt and free mbuf. (current segment) */
268 rte_mbuf_refcnt_update(mq->buffers[mq->last_tail & mask], -1);
269 rte_pktmbuf_free_seg(mq->buffers[mq->last_tail & mask]);
275 memif_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *cur_tail,
276 struct rte_mbuf *tail)
278 /* Check for number-of-segments-overflow */
279 if (unlikely(head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS))
282 /* Chain 'tail' onto the old tail */
283 cur_tail->next = tail;
285 /* accumulate number of segments and total length. */
286 head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
288 tail->pkt_len = tail->data_len;
289 head->pkt_len += tail->pkt_len;
295 eth_memif_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
297 struct memif_queue *mq = queue;
298 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
299 struct pmd_process_private *proc_private =
300 rte_eth_devices[mq->in_port].process_private;
301 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
302 uint16_t cur_slot, last_slot, n_slots, ring_size, mask, s0;
303 uint16_t n_rx_pkts = 0;
304 uint16_t mbuf_size = rte_pktmbuf_data_room_size(mq->mempool) -
305 RTE_PKTMBUF_HEADROOM;
306 uint16_t src_len, src_off, dst_len, dst_off, cp_len;
307 memif_ring_type_t type = mq->type;
309 struct rte_mbuf *mbuf, *mbuf_head, *mbuf_tail;
311 ssize_t size __rte_unused;
314 struct rte_eth_link link;
316 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
318 if (unlikely(ring == NULL)) {
319 /* Secondary process will attempt to request regions. */
320 ret = rte_eth_link_get(mq->in_port, &link);
322 MIF_LOG(ERR, "Failed to get port %u link info: %s",
323 mq->in_port, rte_strerror(-ret));
327 /* consume interrupt */
328 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
329 size = read(mq->intr_handle.fd, &b, sizeof(b));
331 ring_size = 1 << mq->log2_ring_size;
332 mask = ring_size - 1;
334 if (type == MEMIF_RING_C2S) {
335 cur_slot = mq->last_head;
336 last_slot = __atomic_load_n(&ring->head, __ATOMIC_ACQUIRE);
338 cur_slot = mq->last_tail;
339 last_slot = __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE);
342 if (cur_slot == last_slot)
344 n_slots = last_slot - cur_slot;
346 while (n_slots && n_rx_pkts < nb_pkts) {
347 mbuf_head = rte_pktmbuf_alloc(mq->mempool);
348 if (unlikely(mbuf_head == NULL))
351 mbuf->port = mq->in_port;
354 s0 = cur_slot & mask;
355 d0 = &ring->desc[s0];
357 src_len = d0->length;
362 dst_len = mbuf_size - dst_off;
367 /* store pointer to tail */
369 mbuf = rte_pktmbuf_alloc(mq->mempool);
370 if (unlikely(mbuf == NULL))
372 mbuf->port = mq->in_port;
373 ret = memif_pktmbuf_chain(mbuf_head, mbuf_tail, mbuf);
374 if (unlikely(ret < 0)) {
375 MIF_LOG(ERR, "number-of-segments-overflow");
376 rte_pktmbuf_free(mbuf);
380 cp_len = RTE_MIN(dst_len, src_len);
382 rte_pktmbuf_data_len(mbuf) += cp_len;
383 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf);
384 if (mbuf != mbuf_head)
385 rte_pktmbuf_pkt_len(mbuf_head) += cp_len;
387 rte_memcpy(rte_pktmbuf_mtod_offset(mbuf, void *,
389 (uint8_t *)memif_get_buffer(proc_private, d0) +
400 if (d0->flags & MEMIF_DESC_FLAG_NEXT)
403 mq->n_bytes += rte_pktmbuf_pkt_len(mbuf_head);
409 if (type == MEMIF_RING_C2S) {
410 __atomic_store_n(&ring->tail, cur_slot, __ATOMIC_RELEASE);
411 mq->last_head = cur_slot;
413 mq->last_tail = cur_slot;
417 if (type == MEMIF_RING_S2C) {
418 /* ring->head is updated by the receiver and this function
419 * is called in the context of receiver thread. The loads in
420 * the receiver do not need to synchronize with its own stores.
422 head = __atomic_load_n(&ring->head, __ATOMIC_RELAXED);
423 n_slots = ring_size - head + mq->last_tail;
427 d0 = &ring->desc[s0];
428 d0->length = pmd->run.pkt_buffer_size;
430 __atomic_store_n(&ring->head, head, __ATOMIC_RELEASE);
433 mq->n_pkts += n_rx_pkts;
438 eth_memif_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
440 struct memif_queue *mq = queue;
441 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
442 struct pmd_process_private *proc_private =
443 rte_eth_devices[mq->in_port].process_private;
444 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
445 uint16_t cur_slot, last_slot, n_slots, ring_size, mask, s0, head;
446 uint16_t n_rx_pkts = 0;
448 struct rte_mbuf *mbuf, *mbuf_tail;
449 struct rte_mbuf *mbuf_head = NULL;
451 struct rte_eth_link link;
453 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
455 if (unlikely(ring == NULL)) {
456 /* Secondary process will attempt to request regions. */
457 rte_eth_link_get(mq->in_port, &link);
461 /* consume interrupt */
462 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) {
464 ssize_t size __rte_unused;
465 size = read(mq->intr_handle.fd, &b, sizeof(b));
468 ring_size = 1 << mq->log2_ring_size;
469 mask = ring_size - 1;
471 cur_slot = mq->last_tail;
472 /* The ring->tail acts as a guard variable between Tx and Rx
473 * threads, so using load-acquire pairs with store-release
474 * to synchronize it between threads.
476 last_slot = __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE);
477 if (cur_slot == last_slot)
479 n_slots = last_slot - cur_slot;
481 while (n_slots && n_rx_pkts < nb_pkts) {
482 s0 = cur_slot & mask;
484 d0 = &ring->desc[s0];
485 mbuf_head = mq->buffers[s0];
489 /* prefetch next descriptor */
490 if (n_rx_pkts + 1 < nb_pkts)
491 rte_prefetch0(&ring->desc[(cur_slot + 1) & mask]);
493 mbuf->port = mq->in_port;
494 rte_pktmbuf_data_len(mbuf) = d0->length;
495 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf);
497 mq->n_bytes += rte_pktmbuf_data_len(mbuf);
501 if (d0->flags & MEMIF_DESC_FLAG_NEXT) {
502 s0 = cur_slot & mask;
503 d0 = &ring->desc[s0];
505 mbuf = mq->buffers[s0];
506 ret = memif_pktmbuf_chain(mbuf_head, mbuf_tail, mbuf);
507 if (unlikely(ret < 0)) {
508 MIF_LOG(ERR, "number-of-segments-overflow");
518 mq->last_tail = cur_slot;
520 /* Supply server with new buffers */
522 /* ring->head is updated by the receiver and this function
523 * is called in the context of receiver thread. The loads in
524 * the receiver do not need to synchronize with its own stores.
526 head = __atomic_load_n(&ring->head, __ATOMIC_RELAXED);
527 n_slots = ring_size - head + mq->last_tail;
532 ret = rte_pktmbuf_alloc_bulk(mq->mempool, &mq->buffers[head & mask], n_slots);
533 if (unlikely(ret < 0))
539 rte_prefetch0(mq->buffers[head & mask]);
540 d0 = &ring->desc[s0];
541 /* store buffer header */
542 mbuf = mq->buffers[s0];
543 /* populate descriptor */
544 d0->length = rte_pktmbuf_data_room_size(mq->mempool) -
545 RTE_PKTMBUF_HEADROOM;
547 d0->offset = rte_pktmbuf_mtod(mbuf, uint8_t *) -
548 (uint8_t *)proc_private->regions[d0->region]->addr;
551 /* The ring->head acts as a guard variable between Tx and Rx
552 * threads, so using store-release pairs with load-acquire
553 * in function eth_memif_tx.
555 __atomic_store_n(&ring->head, head, __ATOMIC_RELEASE);
557 mq->n_pkts += n_rx_pkts;
563 eth_memif_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
565 struct memif_queue *mq = queue;
566 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
567 struct pmd_process_private *proc_private =
568 rte_eth_devices[mq->in_port].process_private;
569 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
570 uint16_t slot, saved_slot, n_free, ring_size, mask, n_tx_pkts = 0;
571 uint16_t src_len, src_off, dst_len, dst_off, cp_len, nb_segs;
572 memif_ring_type_t type = mq->type;
574 struct rte_mbuf *mbuf;
575 struct rte_mbuf *mbuf_head;
578 struct rte_eth_link link;
580 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
582 if (unlikely(ring == NULL)) {
585 /* Secondary process will attempt to request regions. */
586 ret = rte_eth_link_get(mq->in_port, &link);
588 MIF_LOG(ERR, "Failed to get port %u link info: %s",
589 mq->in_port, rte_strerror(-ret));
593 ring_size = 1 << mq->log2_ring_size;
594 mask = ring_size - 1;
596 if (type == MEMIF_RING_C2S) {
597 /* For C2S queues ring->head is updated by the sender and
598 * this function is called in the context of sending thread.
599 * The loads in the sender do not need to synchronize with
600 * its own stores. Hence, the following load can be a
603 slot = __atomic_load_n(&ring->head, __ATOMIC_RELAXED);
604 n_free = ring_size - slot +
605 __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE);
607 /* For S2C queues ring->tail is updated by the sender and
608 * this function is called in the context of sending thread.
609 * The loads in the sender do not need to synchronize with
610 * its own stores. Hence, the following load can be a
613 slot = __atomic_load_n(&ring->tail, __ATOMIC_RELAXED);
614 n_free = __atomic_load_n(&ring->head, __ATOMIC_ACQUIRE) - slot;
617 while (n_tx_pkts < nb_pkts && n_free) {
619 nb_segs = mbuf_head->nb_segs;
623 d0 = &ring->desc[slot & mask];
625 dst_len = (type == MEMIF_RING_C2S) ?
626 pmd->run.pkt_buffer_size : d0->length;
630 src_len = rte_pktmbuf_data_len(mbuf);
637 d0->flags |= MEMIF_DESC_FLAG_NEXT;
638 d0 = &ring->desc[slot & mask];
640 dst_len = (type == MEMIF_RING_C2S) ?
641 pmd->run.pkt_buffer_size : d0->length;
648 cp_len = RTE_MIN(dst_len, src_len);
650 rte_memcpy((uint8_t *)memif_get_buffer(proc_private,
652 rte_pktmbuf_mtod_offset(mbuf, void *, src_off),
655 mq->n_bytes += cp_len;
661 d0->length = dst_off;
672 rte_pktmbuf_free(mbuf_head);
676 if (type == MEMIF_RING_C2S)
677 __atomic_store_n(&ring->head, slot, __ATOMIC_RELEASE);
679 __atomic_store_n(&ring->tail, slot, __ATOMIC_RELEASE);
681 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) {
683 size = write(mq->intr_handle.fd, &a, sizeof(a));
684 if (unlikely(size < 0)) {
686 "Failed to send interrupt. %s", strerror(errno));
690 mq->n_pkts += n_tx_pkts;
696 memif_tx_one_zc(struct pmd_process_private *proc_private, struct memif_queue *mq,
697 memif_ring_t *ring, struct rte_mbuf *mbuf, const uint16_t mask,
698 uint16_t slot, uint16_t n_free)
701 uint16_t nb_segs = mbuf->nb_segs;
705 /* store pointer to mbuf to free it later */
706 mq->buffers[slot & mask] = mbuf;
707 /* Increment refcnt to make sure the buffer is not freed before server
708 * receives it. (current segment)
710 rte_mbuf_refcnt_update(mbuf, 1);
711 /* populate descriptor */
712 d0 = &ring->desc[slot & mask];
713 d0->length = rte_pktmbuf_data_len(mbuf);
714 mq->n_bytes += rte_pktmbuf_data_len(mbuf);
715 /* FIXME: get region index */
717 d0->offset = rte_pktmbuf_mtod(mbuf, uint8_t *) -
718 (uint8_t *)proc_private->regions[d0->region]->addr;
721 /* check if buffer is chained */
725 /* mark buffer as chained */
726 d0->flags |= MEMIF_DESC_FLAG_NEXT;
729 /* update counters */
739 eth_memif_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
741 struct memif_queue *mq = queue;
742 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
743 struct pmd_process_private *proc_private =
744 rte_eth_devices[mq->in_port].process_private;
745 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
746 uint16_t slot, n_free, ring_size, mask, n_tx_pkts = 0;
747 struct rte_eth_link link;
749 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
751 if (unlikely(ring == NULL)) {
752 /* Secondary process will attempt to request regions. */
753 rte_eth_link_get(mq->in_port, &link);
757 ring_size = 1 << mq->log2_ring_size;
758 mask = ring_size - 1;
760 /* free mbufs received by server */
761 memif_free_stored_mbufs(proc_private, mq);
763 /* ring type always MEMIF_RING_C2S */
764 /* For C2S queues ring->head is updated by the sender and
765 * this function is called in the context of sending thread.
766 * The loads in the sender do not need to synchronize with
767 * its own stores. Hence, the following load can be a
770 slot = __atomic_load_n(&ring->head, __ATOMIC_RELAXED);
771 n_free = ring_size - slot + mq->last_tail;
775 while (n_free && (n_tx_pkts < nb_pkts)) {
776 while ((n_free > 4) && ((nb_pkts - n_tx_pkts) > 4)) {
777 if ((nb_pkts - n_tx_pkts) > 8) {
778 rte_prefetch0(*bufs + 4);
779 rte_prefetch0(*bufs + 5);
780 rte_prefetch0(*bufs + 6);
781 rte_prefetch0(*bufs + 7);
783 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
785 if (unlikely(used_slots < 1))
789 n_free -= used_slots;
791 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
793 if (unlikely(used_slots < 1))
797 n_free -= used_slots;
799 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
801 if (unlikely(used_slots < 1))
805 n_free -= used_slots;
807 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
809 if (unlikely(used_slots < 1))
813 n_free -= used_slots;
815 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
817 if (unlikely(used_slots < 1))
821 n_free -= used_slots;
825 /* ring type always MEMIF_RING_C2S */
826 /* The ring->head acts as a guard variable between Tx and Rx
827 * threads, so using store-release pairs with load-acquire
828 * in function eth_memif_rx for C2S rings.
830 __atomic_store_n(&ring->head, slot, __ATOMIC_RELEASE);
832 /* Send interrupt, if enabled. */
833 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) {
835 ssize_t size = write(mq->intr_handle.fd, &a, sizeof(a));
836 if (unlikely(size < 0)) {
838 "Failed to send interrupt. %s", strerror(errno));
842 /* increment queue counters */
843 mq->n_pkts += n_tx_pkts;
849 memif_free_regions(struct rte_eth_dev *dev)
851 struct pmd_process_private *proc_private = dev->process_private;
852 struct pmd_internals *pmd = dev->data->dev_private;
854 struct memif_region *r;
856 /* regions are allocated contiguously, so it's
857 * enough to loop until 'proc_private->regions_num'
859 for (i = 0; i < proc_private->regions_num; i++) {
860 r = proc_private->regions[i];
862 /* This is memzone */
863 if (i > 0 && (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)) {
868 if (r->addr != NULL) {
869 munmap(r->addr, r->region_size);
876 proc_private->regions[i] = NULL;
879 proc_private->regions_num = 0;
883 memif_region_init_zc(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
886 struct pmd_process_private *proc_private = (struct pmd_process_private *)arg;
887 struct memif_region *r;
889 if (proc_private->regions_num < 1) {
890 MIF_LOG(ERR, "Missing descriptor region");
894 r = proc_private->regions[proc_private->regions_num - 1];
896 if (r->addr != msl->base_va)
897 r = proc_private->regions[++proc_private->regions_num - 1];
900 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
902 MIF_LOG(ERR, "Failed to alloc memif region.");
906 r->addr = msl->base_va;
907 r->region_size = ms->len;
908 r->fd = rte_memseg_get_fd(ms);
911 r->pkt_buffer_offset = 0;
913 proc_private->regions[proc_private->regions_num - 1] = r;
915 r->region_size += ms->len;
922 memif_region_init_shm(struct rte_eth_dev *dev, uint8_t has_buffers)
924 struct pmd_internals *pmd = dev->data->dev_private;
925 struct pmd_process_private *proc_private = dev->process_private;
926 char shm_name[ETH_MEMIF_SHM_NAME_SIZE];
928 struct memif_region *r;
930 if (proc_private->regions_num >= ETH_MEMIF_MAX_REGION_NUM) {
931 MIF_LOG(ERR, "Too many regions.");
935 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
937 MIF_LOG(ERR, "Failed to alloc memif region.");
941 /* calculate buffer offset */
942 r->pkt_buffer_offset = (pmd->run.num_c2s_rings + pmd->run.num_s2c_rings) *
943 (sizeof(memif_ring_t) + sizeof(memif_desc_t) *
944 (1 << pmd->run.log2_ring_size));
946 r->region_size = r->pkt_buffer_offset;
947 /* if region has buffers, add buffers size to region_size */
948 if (has_buffers == 1)
949 r->region_size += (uint32_t)(pmd->run.pkt_buffer_size *
950 (1 << pmd->run.log2_ring_size) *
951 (pmd->run.num_c2s_rings +
952 pmd->run.num_s2c_rings));
954 memset(shm_name, 0, sizeof(char) * ETH_MEMIF_SHM_NAME_SIZE);
955 snprintf(shm_name, ETH_MEMIF_SHM_NAME_SIZE, "memif_region_%d",
956 proc_private->regions_num);
958 r->fd = memfd_create(shm_name, MFD_ALLOW_SEALING);
960 MIF_LOG(ERR, "Failed to create shm file: %s.", strerror(errno));
965 ret = fcntl(r->fd, F_ADD_SEALS, F_SEAL_SHRINK);
967 MIF_LOG(ERR, "Failed to add seals to shm file: %s.", strerror(errno));
971 ret = ftruncate(r->fd, r->region_size);
973 MIF_LOG(ERR, "Failed to truncate shm file: %s.", strerror(errno));
977 r->addr = mmap(NULL, r->region_size, PROT_READ |
978 PROT_WRITE, MAP_SHARED, r->fd, 0);
979 if (r->addr == MAP_FAILED) {
980 MIF_LOG(ERR, "Failed to mmap shm region: %s.", strerror(ret));
985 proc_private->regions[proc_private->regions_num] = r;
986 proc_private->regions_num++;
999 memif_regions_init(struct rte_eth_dev *dev)
1001 struct pmd_internals *pmd = dev->data->dev_private;
1005 * Zero-copy exposes dpdk memory.
1006 * Each memseg list will be represented by memif region.
1007 * Zero-copy regions indexing: memseg list idx + 1,
1008 * as we already have region 0 reserved for descriptors.
1010 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1011 /* create region idx 0 containing descriptors */
1012 ret = memif_region_init_shm(dev, 0);
1015 ret = rte_memseg_walk(memif_region_init_zc, (void *)dev->process_private);
1019 /* create one memory region contaning rings and buffers */
1020 ret = memif_region_init_shm(dev, /* has buffers */ 1);
1029 memif_init_rings(struct rte_eth_dev *dev)
1031 struct pmd_internals *pmd = dev->data->dev_private;
1032 struct pmd_process_private *proc_private = dev->process_private;
1037 for (i = 0; i < pmd->run.num_c2s_rings; i++) {
1038 ring = memif_get_ring(pmd, proc_private, MEMIF_RING_C2S, i);
1039 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1040 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1041 ring->cookie = MEMIF_COOKIE;
1044 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)
1047 for (j = 0; j < (1 << pmd->run.log2_ring_size); j++) {
1048 slot = i * (1 << pmd->run.log2_ring_size) + j;
1049 ring->desc[j].region = 0;
1050 ring->desc[j].offset =
1051 proc_private->regions[0]->pkt_buffer_offset +
1052 (uint32_t)(slot * pmd->run.pkt_buffer_size);
1053 ring->desc[j].length = pmd->run.pkt_buffer_size;
1057 for (i = 0; i < pmd->run.num_s2c_rings; i++) {
1058 ring = memif_get_ring(pmd, proc_private, MEMIF_RING_S2C, i);
1059 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1060 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1061 ring->cookie = MEMIF_COOKIE;
1064 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)
1067 for (j = 0; j < (1 << pmd->run.log2_ring_size); j++) {
1068 slot = (i + pmd->run.num_c2s_rings) *
1069 (1 << pmd->run.log2_ring_size) + j;
1070 ring->desc[j].region = 0;
1071 ring->desc[j].offset =
1072 proc_private->regions[0]->pkt_buffer_offset +
1073 (uint32_t)(slot * pmd->run.pkt_buffer_size);
1074 ring->desc[j].length = pmd->run.pkt_buffer_size;
1079 /* called only by client */
1081 memif_init_queues(struct rte_eth_dev *dev)
1083 struct pmd_internals *pmd = dev->data->dev_private;
1084 struct memif_queue *mq;
1087 for (i = 0; i < pmd->run.num_c2s_rings; i++) {
1088 mq = dev->data->tx_queues[i];
1089 mq->log2_ring_size = pmd->run.log2_ring_size;
1090 /* queues located only in region 0 */
1092 mq->ring_offset = memif_get_ring_offset(dev, mq, MEMIF_RING_C2S, i);
1095 mq->intr_handle.fd = eventfd(0, EFD_NONBLOCK);
1096 if (mq->intr_handle.fd < 0) {
1098 "Failed to create eventfd for tx queue %d: %s.", i,
1102 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1103 mq->buffers = rte_zmalloc("bufs", sizeof(struct rte_mbuf *) *
1104 (1 << mq->log2_ring_size), 0);
1105 if (mq->buffers == NULL)
1110 for (i = 0; i < pmd->run.num_s2c_rings; i++) {
1111 mq = dev->data->rx_queues[i];
1112 mq->log2_ring_size = pmd->run.log2_ring_size;
1113 /* queues located only in region 0 */
1115 mq->ring_offset = memif_get_ring_offset(dev, mq, MEMIF_RING_S2C, i);
1118 mq->intr_handle.fd = eventfd(0, EFD_NONBLOCK);
1119 if (mq->intr_handle.fd < 0) {
1121 "Failed to create eventfd for rx queue %d: %s.", i,
1125 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1126 mq->buffers = rte_zmalloc("bufs", sizeof(struct rte_mbuf *) *
1127 (1 << mq->log2_ring_size), 0);
1128 if (mq->buffers == NULL)
1136 memif_init_regions_and_queues(struct rte_eth_dev *dev)
1140 ret = memif_regions_init(dev);
1144 memif_init_rings(dev);
1146 ret = memif_init_queues(dev);
1154 memif_connect(struct rte_eth_dev *dev)
1156 struct pmd_internals *pmd = dev->data->dev_private;
1157 struct pmd_process_private *proc_private = dev->process_private;
1158 struct memif_region *mr;
1159 struct memif_queue *mq;
1163 for (i = 0; i < proc_private->regions_num; i++) {
1164 mr = proc_private->regions[i];
1166 if (mr->addr == NULL) {
1169 mr->addr = mmap(NULL, mr->region_size,
1170 PROT_READ | PROT_WRITE,
1171 MAP_SHARED, mr->fd, 0);
1172 if (mr->addr == MAP_FAILED) {
1173 MIF_LOG(ERR, "mmap failed: %s\n",
1178 if (i > 0 && (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)) {
1179 /* close memseg file */
1186 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1187 for (i = 0; i < pmd->run.num_c2s_rings; i++) {
1188 mq = (pmd->role == MEMIF_ROLE_CLIENT) ?
1189 dev->data->tx_queues[i] : dev->data->rx_queues[i];
1190 ring = memif_get_ring_from_queue(proc_private, mq);
1191 if (ring == NULL || ring->cookie != MEMIF_COOKIE) {
1192 MIF_LOG(ERR, "Wrong ring");
1195 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1196 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1199 /* enable polling mode */
1200 if (pmd->role == MEMIF_ROLE_SERVER)
1201 ring->flags = MEMIF_RING_FLAG_MASK_INT;
1203 for (i = 0; i < pmd->run.num_s2c_rings; i++) {
1204 mq = (pmd->role == MEMIF_ROLE_CLIENT) ?
1205 dev->data->rx_queues[i] : dev->data->tx_queues[i];
1206 ring = memif_get_ring_from_queue(proc_private, mq);
1207 if (ring == NULL || ring->cookie != MEMIF_COOKIE) {
1208 MIF_LOG(ERR, "Wrong ring");
1211 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1212 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1215 /* enable polling mode */
1216 if (pmd->role == MEMIF_ROLE_CLIENT)
1217 ring->flags = MEMIF_RING_FLAG_MASK_INT;
1220 pmd->flags &= ~ETH_MEMIF_FLAG_CONNECTING;
1221 pmd->flags |= ETH_MEMIF_FLAG_CONNECTED;
1222 dev->data->dev_link.link_status = ETH_LINK_UP;
1224 MIF_LOG(INFO, "Connected.");
1229 memif_dev_start(struct rte_eth_dev *dev)
1231 struct pmd_internals *pmd = dev->data->dev_private;
1234 switch (pmd->role) {
1235 case MEMIF_ROLE_CLIENT:
1236 ret = memif_connect_client(dev);
1238 case MEMIF_ROLE_SERVER:
1239 ret = memif_connect_server(dev);
1242 MIF_LOG(ERR, "Unknown role: %d.", pmd->role);
1251 memif_dev_close(struct rte_eth_dev *dev)
1253 struct pmd_internals *pmd = dev->data->dev_private;
1256 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1257 memif_msg_enq_disconnect(pmd->cc, "Device closed", 0);
1258 memif_disconnect(dev);
1260 for (i = 0; i < dev->data->nb_rx_queues; i++)
1261 (*dev->dev_ops->rx_queue_release)(dev, i);
1262 for (i = 0; i < dev->data->nb_tx_queues; i++)
1263 (*dev->dev_ops->tx_queue_release)(dev, i);
1265 memif_socket_remove_device(dev);
1267 memif_disconnect(dev);
1270 rte_free(dev->process_private);
1276 memif_dev_configure(struct rte_eth_dev *dev)
1278 struct pmd_internals *pmd = dev->data->dev_private;
1284 pmd->cfg.num_c2s_rings = (pmd->role == MEMIF_ROLE_CLIENT) ?
1285 dev->data->nb_tx_queues : dev->data->nb_rx_queues;
1291 pmd->cfg.num_s2c_rings = (pmd->role == MEMIF_ROLE_CLIENT) ?
1292 dev->data->nb_rx_queues : dev->data->nb_tx_queues;
1298 memif_tx_queue_setup(struct rte_eth_dev *dev,
1300 uint16_t nb_tx_desc __rte_unused,
1301 unsigned int socket_id __rte_unused,
1302 const struct rte_eth_txconf *tx_conf __rte_unused)
1304 struct pmd_internals *pmd = dev->data->dev_private;
1305 struct memif_queue *mq;
1307 mq = rte_zmalloc("tx-queue", sizeof(struct memif_queue), 0);
1309 MIF_LOG(ERR, "Failed to allocate tx queue id: %u", qid);
1314 (pmd->role == MEMIF_ROLE_CLIENT) ? MEMIF_RING_C2S : MEMIF_RING_S2C;
1317 mq->intr_handle.fd = -1;
1318 mq->intr_handle.type = RTE_INTR_HANDLE_EXT;
1319 mq->in_port = dev->data->port_id;
1320 dev->data->tx_queues[qid] = mq;
1326 memif_rx_queue_setup(struct rte_eth_dev *dev,
1328 uint16_t nb_rx_desc __rte_unused,
1329 unsigned int socket_id __rte_unused,
1330 const struct rte_eth_rxconf *rx_conf __rte_unused,
1331 struct rte_mempool *mb_pool)
1333 struct pmd_internals *pmd = dev->data->dev_private;
1334 struct memif_queue *mq;
1336 mq = rte_zmalloc("rx-queue", sizeof(struct memif_queue), 0);
1338 MIF_LOG(ERR, "Failed to allocate rx queue id: %u", qid);
1342 mq->type = (pmd->role == MEMIF_ROLE_CLIENT) ? MEMIF_RING_S2C : MEMIF_RING_C2S;
1345 mq->intr_handle.fd = -1;
1346 mq->intr_handle.type = RTE_INTR_HANDLE_EXT;
1347 mq->mempool = mb_pool;
1348 mq->in_port = dev->data->port_id;
1349 dev->data->rx_queues[qid] = mq;
1355 memif_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1357 struct memif_queue *mq = dev->data->rx_queues[qid];
1366 memif_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1368 struct memif_queue *mq = dev->data->tx_queues[qid];
1377 memif_link_update(struct rte_eth_dev *dev,
1378 int wait_to_complete __rte_unused)
1380 struct pmd_process_private *proc_private;
1382 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1383 proc_private = dev->process_private;
1384 if (dev->data->dev_link.link_status == ETH_LINK_UP &&
1385 proc_private->regions_num == 0) {
1386 memif_mp_request_regions(dev);
1387 } else if (dev->data->dev_link.link_status == ETH_LINK_DOWN &&
1388 proc_private->regions_num > 0) {
1389 memif_free_regions(dev);
1396 memif_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1398 struct pmd_internals *pmd = dev->data->dev_private;
1399 struct memif_queue *mq;
1403 stats->ipackets = 0;
1405 stats->opackets = 0;
1408 tmp = (pmd->role == MEMIF_ROLE_CLIENT) ? pmd->run.num_c2s_rings :
1409 pmd->run.num_s2c_rings;
1410 nq = (tmp < RTE_ETHDEV_QUEUE_STAT_CNTRS) ? tmp :
1411 RTE_ETHDEV_QUEUE_STAT_CNTRS;
1414 for (i = 0; i < nq; i++) {
1415 mq = dev->data->rx_queues[i];
1416 stats->q_ipackets[i] = mq->n_pkts;
1417 stats->q_ibytes[i] = mq->n_bytes;
1418 stats->ipackets += mq->n_pkts;
1419 stats->ibytes += mq->n_bytes;
1422 tmp = (pmd->role == MEMIF_ROLE_CLIENT) ? pmd->run.num_s2c_rings :
1423 pmd->run.num_c2s_rings;
1424 nq = (tmp < RTE_ETHDEV_QUEUE_STAT_CNTRS) ? tmp :
1425 RTE_ETHDEV_QUEUE_STAT_CNTRS;
1428 for (i = 0; i < nq; i++) {
1429 mq = dev->data->tx_queues[i];
1430 stats->q_opackets[i] = mq->n_pkts;
1431 stats->q_obytes[i] = mq->n_bytes;
1432 stats->opackets += mq->n_pkts;
1433 stats->obytes += mq->n_bytes;
1439 memif_stats_reset(struct rte_eth_dev *dev)
1441 struct pmd_internals *pmd = dev->data->dev_private;
1443 struct memif_queue *mq;
1445 for (i = 0; i < pmd->run.num_c2s_rings; i++) {
1446 mq = (pmd->role == MEMIF_ROLE_CLIENT) ? dev->data->tx_queues[i] :
1447 dev->data->rx_queues[i];
1451 for (i = 0; i < pmd->run.num_s2c_rings; i++) {
1452 mq = (pmd->role == MEMIF_ROLE_CLIENT) ? dev->data->rx_queues[i] :
1453 dev->data->tx_queues[i];
1462 memif_rx_queue_intr_enable(struct rte_eth_dev *dev __rte_unused,
1463 uint16_t qid __rte_unused)
1465 MIF_LOG(WARNING, "Interrupt mode not supported.");
1471 memif_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t qid __rte_unused)
1473 struct pmd_internals *pmd __rte_unused = dev->data->dev_private;
1478 static const struct eth_dev_ops ops = {
1479 .dev_start = memif_dev_start,
1480 .dev_close = memif_dev_close,
1481 .dev_infos_get = memif_dev_info,
1482 .dev_configure = memif_dev_configure,
1483 .tx_queue_setup = memif_tx_queue_setup,
1484 .rx_queue_setup = memif_rx_queue_setup,
1485 .rx_queue_release = memif_rx_queue_release,
1486 .tx_queue_release = memif_tx_queue_release,
1487 .rx_queue_intr_enable = memif_rx_queue_intr_enable,
1488 .rx_queue_intr_disable = memif_rx_queue_intr_disable,
1489 .link_update = memif_link_update,
1490 .stats_get = memif_stats_get,
1491 .stats_reset = memif_stats_reset,
1495 memif_create(struct rte_vdev_device *vdev, enum memif_role_t role,
1496 memif_interface_id_t id, uint32_t flags,
1497 const char *socket_filename,
1498 memif_log2_ring_size_t log2_ring_size,
1499 uint16_t pkt_buffer_size, const char *secret,
1500 struct rte_ether_addr *ether_addr)
1503 struct rte_eth_dev *eth_dev;
1504 struct rte_eth_dev_data *data;
1505 struct pmd_internals *pmd;
1506 struct pmd_process_private *process_private;
1507 const unsigned int numa_node = vdev->device.numa_node;
1508 const char *name = rte_vdev_device_name(vdev);
1510 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1511 if (eth_dev == NULL) {
1512 MIF_LOG(ERR, "%s: Unable to allocate device struct.", name);
1516 process_private = (struct pmd_process_private *)
1517 rte_zmalloc(name, sizeof(struct pmd_process_private),
1518 RTE_CACHE_LINE_SIZE);
1520 if (process_private == NULL) {
1521 MIF_LOG(ERR, "Failed to alloc memory for process private");
1524 eth_dev->process_private = process_private;
1526 pmd = eth_dev->data->dev_private;
1527 memset(pmd, 0, sizeof(*pmd));
1531 pmd->flags |= ETH_MEMIF_FLAG_DISABLED;
1533 /* Zero-copy flag irelevant to server. */
1534 if (pmd->role == MEMIF_ROLE_SERVER)
1535 pmd->flags &= ~ETH_MEMIF_FLAG_ZERO_COPY;
1537 ret = memif_socket_init(eth_dev, socket_filename);
1541 memset(pmd->secret, 0, sizeof(char) * ETH_MEMIF_SECRET_SIZE);
1543 strlcpy(pmd->secret, secret, sizeof(pmd->secret));
1545 pmd->cfg.log2_ring_size = log2_ring_size;
1546 /* set in .dev_configure() */
1547 pmd->cfg.num_c2s_rings = 0;
1548 pmd->cfg.num_s2c_rings = 0;
1550 pmd->cfg.pkt_buffer_size = pkt_buffer_size;
1551 rte_spinlock_init(&pmd->cc_lock);
1553 data = eth_dev->data;
1554 data->dev_private = pmd;
1555 data->numa_node = numa_node;
1556 data->dev_link = pmd_link;
1557 data->mac_addrs = ether_addr;
1558 data->promiscuous = 1;
1559 data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1561 eth_dev->dev_ops = &ops;
1562 eth_dev->device = &vdev->device;
1563 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1564 eth_dev->rx_pkt_burst = eth_memif_rx_zc;
1565 eth_dev->tx_pkt_burst = eth_memif_tx_zc;
1567 eth_dev->rx_pkt_burst = eth_memif_rx;
1568 eth_dev->tx_pkt_burst = eth_memif_tx;
1571 rte_eth_dev_probing_finish(eth_dev);
1577 memif_set_role(const char *key __rte_unused, const char *value,
1580 enum memif_role_t *role = (enum memif_role_t *)extra_args;
1582 if (strstr(value, "server") != NULL) {
1583 *role = MEMIF_ROLE_SERVER;
1584 } else if (strstr(value, "client") != NULL) {
1585 *role = MEMIF_ROLE_CLIENT;
1586 } else if (strstr(value, "master") != NULL) {
1587 MIF_LOG(NOTICE, "Role argument \"master\" is deprecated, use \"server\"");
1588 *role = MEMIF_ROLE_SERVER;
1589 } else if (strstr(value, "slave") != NULL) {
1590 MIF_LOG(NOTICE, "Role argument \"slave\" is deprecated, use \"client\"");
1591 *role = MEMIF_ROLE_CLIENT;
1593 MIF_LOG(ERR, "Unknown role: %s.", value);
1600 memif_set_zc(const char *key __rte_unused, const char *value, void *extra_args)
1602 uint32_t *flags = (uint32_t *)extra_args;
1604 if (strstr(value, "yes") != NULL) {
1605 if (!rte_mcfg_get_single_file_segments()) {
1606 MIF_LOG(ERR, "Zero-copy doesn't support multi-file segments.");
1609 *flags |= ETH_MEMIF_FLAG_ZERO_COPY;
1610 } else if (strstr(value, "no") != NULL) {
1611 *flags &= ~ETH_MEMIF_FLAG_ZERO_COPY;
1613 MIF_LOG(ERR, "Failed to parse zero-copy param: %s.", value);
1620 memif_set_id(const char *key __rte_unused, const char *value, void *extra_args)
1622 memif_interface_id_t *id = (memif_interface_id_t *)extra_args;
1624 /* even if parsing fails, 0 is a valid id */
1625 *id = strtoul(value, NULL, 10);
1630 memif_set_bs(const char *key __rte_unused, const char *value, void *extra_args)
1633 uint16_t *pkt_buffer_size = (uint16_t *)extra_args;
1635 tmp = strtoul(value, NULL, 10);
1636 if (tmp == 0 || tmp > 0xFFFF) {
1637 MIF_LOG(ERR, "Invalid buffer size: %s.", value);
1640 *pkt_buffer_size = tmp;
1645 memif_set_rs(const char *key __rte_unused, const char *value, void *extra_args)
1648 memif_log2_ring_size_t *log2_ring_size =
1649 (memif_log2_ring_size_t *)extra_args;
1651 tmp = strtoul(value, NULL, 10);
1652 if (tmp == 0 || tmp > ETH_MEMIF_MAX_LOG2_RING_SIZE) {
1653 MIF_LOG(ERR, "Invalid ring size: %s (max %u).",
1654 value, ETH_MEMIF_MAX_LOG2_RING_SIZE);
1657 *log2_ring_size = tmp;
1661 /* check if directory exists and if we have permission to read/write */
1663 memif_check_socket_filename(const char *filename)
1665 char *dir = NULL, *tmp;
1669 if (strlen(filename) >= MEMIF_SOCKET_UN_SIZE) {
1670 MIF_LOG(ERR, "Unix socket address too long (max 108).");
1674 tmp = strrchr(filename, '/');
1676 idx = tmp - filename;
1677 dir = rte_zmalloc("memif_tmp", sizeof(char) * (idx + 1), 0);
1679 MIF_LOG(ERR, "Failed to allocate memory.");
1682 strlcpy(dir, filename, sizeof(char) * (idx + 1));
1685 if (dir == NULL || (faccessat(-1, dir, F_OK | R_OK |
1686 W_OK, AT_EACCESS) < 0)) {
1687 MIF_LOG(ERR, "Invalid socket directory.");
1698 memif_set_socket_filename(const char *key __rte_unused, const char *value,
1701 const char **socket_filename = (const char **)extra_args;
1703 *socket_filename = value;
1708 memif_set_is_socket_abstract(const char *key __rte_unused, const char *value, void *extra_args)
1710 uint32_t *flags = (uint32_t *)extra_args;
1712 if (strstr(value, "yes") != NULL) {
1713 *flags |= ETH_MEMIF_FLAG_SOCKET_ABSTRACT;
1714 } else if (strstr(value, "no") != NULL) {
1715 *flags &= ~ETH_MEMIF_FLAG_SOCKET_ABSTRACT;
1717 MIF_LOG(ERR, "Failed to parse socket-abstract param: %s.", value);
1724 memif_set_mac(const char *key __rte_unused, const char *value, void *extra_args)
1726 struct rte_ether_addr *ether_addr = (struct rte_ether_addr *)extra_args;
1728 if (rte_ether_unformat_addr(value, ether_addr) < 0)
1729 MIF_LOG(WARNING, "Failed to parse mac '%s'.", value);
1734 memif_set_secret(const char *key __rte_unused, const char *value, void *extra_args)
1736 const char **secret = (const char **)extra_args;
1743 rte_pmd_memif_probe(struct rte_vdev_device *vdev)
1745 RTE_BUILD_BUG_ON(sizeof(memif_msg_t) != 128);
1746 RTE_BUILD_BUG_ON(sizeof(memif_desc_t) != 16);
1748 struct rte_kvargs *kvlist;
1749 const char *name = rte_vdev_device_name(vdev);
1750 enum memif_role_t role = MEMIF_ROLE_CLIENT;
1751 memif_interface_id_t id = 0;
1752 uint16_t pkt_buffer_size = ETH_MEMIF_DEFAULT_PKT_BUFFER_SIZE;
1753 memif_log2_ring_size_t log2_ring_size = ETH_MEMIF_DEFAULT_RING_SIZE;
1754 const char *socket_filename = ETH_MEMIF_DEFAULT_SOCKET_FILENAME;
1756 const char *secret = NULL;
1757 struct rte_ether_addr *ether_addr = rte_zmalloc("",
1758 sizeof(struct rte_ether_addr), 0);
1759 struct rte_eth_dev *eth_dev;
1761 rte_eth_random_addr(ether_addr->addr_bytes);
1763 MIF_LOG(INFO, "Initialize MEMIF: %s.", name);
1765 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1766 eth_dev = rte_eth_dev_attach_secondary(name);
1768 MIF_LOG(ERR, "Failed to probe %s", name);
1772 eth_dev->dev_ops = &ops;
1773 eth_dev->device = &vdev->device;
1774 eth_dev->rx_pkt_burst = eth_memif_rx;
1775 eth_dev->tx_pkt_burst = eth_memif_tx;
1777 if (!rte_eal_primary_proc_alive(NULL)) {
1778 MIF_LOG(ERR, "Primary process is missing");
1782 eth_dev->process_private = (struct pmd_process_private *)
1784 sizeof(struct pmd_process_private),
1785 RTE_CACHE_LINE_SIZE);
1786 if (eth_dev->process_private == NULL) {
1788 "Failed to alloc memory for process private");
1792 rte_eth_dev_probing_finish(eth_dev);
1797 ret = rte_mp_action_register(MEMIF_MP_SEND_REGION, memif_mp_send_region);
1799 * Primary process can continue probing, but secondary process won't
1800 * be able to get memory regions information
1802 if (ret < 0 && rte_errno != EEXIST)
1803 MIF_LOG(WARNING, "Failed to register mp action callback: %s",
1804 strerror(rte_errno));
1806 /* use abstract address by default */
1807 flags |= ETH_MEMIF_FLAG_SOCKET_ABSTRACT;
1809 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_arguments);
1811 /* parse parameters */
1812 if (kvlist != NULL) {
1813 ret = rte_kvargs_process(kvlist, ETH_MEMIF_ROLE_ARG,
1814 &memif_set_role, &role);
1817 ret = rte_kvargs_process(kvlist, ETH_MEMIF_ID_ARG,
1818 &memif_set_id, &id);
1821 ret = rte_kvargs_process(kvlist, ETH_MEMIF_PKT_BUFFER_SIZE_ARG,
1822 &memif_set_bs, &pkt_buffer_size);
1825 ret = rte_kvargs_process(kvlist, ETH_MEMIF_RING_SIZE_ARG,
1826 &memif_set_rs, &log2_ring_size);
1829 ret = rte_kvargs_process(kvlist, ETH_MEMIF_SOCKET_ARG,
1830 &memif_set_socket_filename,
1831 (void *)(&socket_filename));
1834 ret = rte_kvargs_process(kvlist, ETH_MEMIF_SOCKET_ABSTRACT_ARG,
1835 &memif_set_is_socket_abstract, &flags);
1838 ret = rte_kvargs_process(kvlist, ETH_MEMIF_MAC_ARG,
1839 &memif_set_mac, ether_addr);
1842 ret = rte_kvargs_process(kvlist, ETH_MEMIF_ZC_ARG,
1843 &memif_set_zc, &flags);
1846 ret = rte_kvargs_process(kvlist, ETH_MEMIF_SECRET_ARG,
1847 &memif_set_secret, (void *)(&secret));
1852 if (!(flags & ETH_MEMIF_FLAG_SOCKET_ABSTRACT)) {
1853 ret = memif_check_socket_filename(socket_filename);
1858 /* create interface */
1859 ret = memif_create(vdev, role, id, flags, socket_filename,
1860 log2_ring_size, pkt_buffer_size, secret, ether_addr);
1864 rte_kvargs_free(kvlist);
1869 rte_pmd_memif_remove(struct rte_vdev_device *vdev)
1871 struct rte_eth_dev *eth_dev;
1873 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
1874 if (eth_dev == NULL)
1877 return rte_eth_dev_close(eth_dev->data->port_id);
1880 static struct rte_vdev_driver pmd_memif_drv = {
1881 .probe = rte_pmd_memif_probe,
1882 .remove = rte_pmd_memif_remove,
1885 RTE_PMD_REGISTER_VDEV(net_memif, pmd_memif_drv);
1887 RTE_PMD_REGISTER_PARAM_STRING(net_memif,
1888 ETH_MEMIF_ID_ARG "=<int>"
1889 ETH_MEMIF_ROLE_ARG "=server|client"
1890 ETH_MEMIF_PKT_BUFFER_SIZE_ARG "=<int>"
1891 ETH_MEMIF_RING_SIZE_ARG "=<int>"
1892 ETH_MEMIF_SOCKET_ARG "=<string>"
1893 ETH_MEMIF_SOCKET_ABSTRACT_ARG "=yes|no"
1894 ETH_MEMIF_MAC_ARG "=xx:xx:xx:xx:xx:xx"
1895 ETH_MEMIF_ZC_ARG "=yes|no"
1896 ETH_MEMIF_SECRET_ARG "=<string>");
1898 RTE_LOG_REGISTER_DEFAULT(memif_logtype, NOTICE);