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 <rte_ethdev_driver.h>
21 #include <rte_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_MAC_ARG "mac"
40 #define ETH_MEMIF_ZC_ARG "zero-copy"
41 #define ETH_MEMIF_SECRET_ARG "secret"
43 static const char * const valid_arguments[] = {
46 ETH_MEMIF_PKT_BUFFER_SIZE_ARG,
47 ETH_MEMIF_RING_SIZE_ARG,
55 static const struct rte_eth_link pmd_link = {
56 .link_speed = ETH_SPEED_NUM_10G,
57 .link_duplex = ETH_LINK_FULL_DUPLEX,
58 .link_status = ETH_LINK_DOWN,
59 .link_autoneg = ETH_LINK_AUTONEG
62 #define MEMIF_MP_SEND_REGION "memif_mp_send_region"
65 static int memif_region_init_zc(const struct rte_memseg_list *msl,
66 const struct rte_memseg *ms, void *arg);
71 return ("memif-" RTE_STR(MEMIF_VERSION_MAJOR) "." RTE_STR(MEMIF_VERSION_MINOR));
74 /* Message header to synchronize regions */
75 struct mp_region_msg {
76 char port_name[RTE_DEV_NAME_MAX_LEN];
77 memif_region_index_t idx;
78 memif_region_size_t size;
82 memif_mp_send_region(const struct rte_mp_msg *msg, const void *peer)
84 struct rte_eth_dev *dev;
85 struct pmd_process_private *proc_private;
86 const struct mp_region_msg *msg_param = (const struct mp_region_msg *)msg->param;
87 struct rte_mp_msg reply;
88 struct mp_region_msg *reply_param = (struct mp_region_msg *)reply.param;
92 /* Get requested port */
93 ret = rte_eth_dev_get_port_by_name(msg_param->port_name, &port_id);
95 MIF_LOG(ERR, "Failed to get port id for %s",
96 msg_param->port_name);
99 dev = &rte_eth_devices[port_id];
100 proc_private = dev->process_private;
102 memset(&reply, 0, sizeof(reply));
103 strlcpy(reply.name, msg->name, sizeof(reply.name));
104 reply_param->idx = msg_param->idx;
105 if (proc_private->regions[msg_param->idx] != NULL) {
106 reply_param->size = proc_private->regions[msg_param->idx]->region_size;
107 reply.fds[0] = proc_private->regions[msg_param->idx]->fd;
110 reply.len_param = sizeof(*reply_param);
111 if (rte_mp_reply(&reply, peer) < 0) {
112 MIF_LOG(ERR, "Failed to reply to an add region request");
121 * Called by secondary process, when ports link status goes up.
124 memif_mp_request_regions(struct rte_eth_dev *dev)
127 struct timespec timeout = {.tv_sec = 5, .tv_nsec = 0};
128 struct rte_mp_msg msg, *reply;
129 struct rte_mp_reply replies;
130 struct mp_region_msg *msg_param = (struct mp_region_msg *)msg.param;
131 struct mp_region_msg *reply_param;
132 struct memif_region *r;
133 struct pmd_process_private *proc_private = dev->process_private;
134 struct pmd_internals *pmd = dev->data->dev_private;
135 /* in case of zero-copy slave, only request region 0 */
136 uint16_t max_region_num = (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) ?
137 1 : ETH_MEMIF_MAX_REGION_NUM;
139 MIF_LOG(DEBUG, "Requesting memory regions");
141 for (i = 0; i < max_region_num; i++) {
142 /* Prepare the message */
143 memset(&msg, 0, sizeof(msg));
144 strlcpy(msg.name, MEMIF_MP_SEND_REGION, sizeof(msg.name));
145 strlcpy(msg_param->port_name, dev->data->name,
146 sizeof(msg_param->port_name));
148 msg.len_param = sizeof(*msg_param);
151 ret = rte_mp_request_sync(&msg, &replies, &timeout);
152 if (ret < 0 || replies.nb_received != 1) {
153 MIF_LOG(ERR, "Failed to send mp msg: %d",
158 reply = &replies.msgs[0];
159 reply_param = (struct mp_region_msg *)reply->param;
161 if (reply_param->size > 0) {
162 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
164 MIF_LOG(ERR, "Failed to alloc memif region.");
168 r->region_size = reply_param->size;
169 if (reply->num_fds < 1) {
170 MIF_LOG(ERR, "Missing file descriptor.");
174 r->fd = reply->fds[0];
177 proc_private->regions[reply_param->idx] = r;
178 proc_private->regions_num++;
183 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
184 ret = rte_memseg_walk(memif_region_init_zc, (void *)proc_private);
189 return memif_connect(dev);
193 memif_dev_info(struct rte_eth_dev *dev __rte_unused, struct rte_eth_dev_info *dev_info)
195 dev_info->max_mac_addrs = 1;
196 dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
197 dev_info->max_rx_queues = ETH_MEMIF_MAX_NUM_Q_PAIRS;
198 dev_info->max_tx_queues = ETH_MEMIF_MAX_NUM_Q_PAIRS;
199 dev_info->min_rx_bufsize = 0;
204 static memif_ring_t *
205 memif_get_ring(struct pmd_internals *pmd, struct pmd_process_private *proc_private,
206 memif_ring_type_t type, uint16_t ring_num)
208 /* rings only in region 0 */
209 void *p = proc_private->regions[0]->addr;
210 int ring_size = sizeof(memif_ring_t) + sizeof(memif_desc_t) *
211 (1 << pmd->run.log2_ring_size);
213 p = (uint8_t *)p + (ring_num + type * pmd->run.num_s2m_rings) * ring_size;
215 return (memif_ring_t *)p;
218 static memif_region_offset_t
219 memif_get_ring_offset(struct rte_eth_dev *dev, struct memif_queue *mq,
220 memif_ring_type_t type, uint16_t num)
222 struct pmd_internals *pmd = dev->data->dev_private;
223 struct pmd_process_private *proc_private = dev->process_private;
225 return ((uint8_t *)memif_get_ring(pmd, proc_private, type, num) -
226 (uint8_t *)proc_private->regions[mq->region]->addr);
229 static memif_ring_t *
230 memif_get_ring_from_queue(struct pmd_process_private *proc_private,
231 struct memif_queue *mq)
233 struct memif_region *r;
235 r = proc_private->regions[mq->region];
239 return (memif_ring_t *)((uint8_t *)r->addr + mq->ring_offset);
243 memif_get_buffer(struct pmd_process_private *proc_private, memif_desc_t *d)
245 return ((uint8_t *)proc_private->regions[d->region]->addr + d->offset);
248 /* Free mbufs received by master */
250 memif_free_stored_mbufs(struct pmd_process_private *proc_private, struct memif_queue *mq)
252 uint16_t mask = (1 << mq->log2_ring_size) - 1;
253 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
255 /* FIXME: improve performance */
256 while (mq->last_tail != ring->tail) {
257 RTE_MBUF_PREFETCH_TO_FREE(mq->buffers[(mq->last_tail + 1) & mask]);
258 /* Decrement refcnt and free mbuf. (current segment) */
259 rte_mbuf_refcnt_update(mq->buffers[mq->last_tail & mask], -1);
260 rte_pktmbuf_free_seg(mq->buffers[mq->last_tail & mask]);
266 memif_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *cur_tail,
267 struct rte_mbuf *tail)
269 /* Check for number-of-segments-overflow */
270 if (unlikely(head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS))
273 /* Chain 'tail' onto the old tail */
274 cur_tail->next = tail;
276 /* accumulate number of segments and total length. */
277 head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
279 tail->pkt_len = tail->data_len;
280 head->pkt_len += tail->pkt_len;
286 eth_memif_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
288 struct memif_queue *mq = queue;
289 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
290 struct pmd_process_private *proc_private =
291 rte_eth_devices[mq->in_port].process_private;
292 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
293 uint16_t cur_slot, last_slot, n_slots, ring_size, mask, s0;
294 uint16_t n_rx_pkts = 0;
295 uint16_t mbuf_size = rte_pktmbuf_data_room_size(mq->mempool) -
296 RTE_PKTMBUF_HEADROOM;
297 uint16_t src_len, src_off, dst_len, dst_off, cp_len;
298 memif_ring_type_t type = mq->type;
300 struct rte_mbuf *mbuf, *mbuf_head, *mbuf_tail;
302 ssize_t size __rte_unused;
305 struct rte_eth_link link;
307 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
309 if (unlikely(ring == NULL)) {
310 /* Secondary process will attempt to request regions. */
311 ret = rte_eth_link_get(mq->in_port, &link);
313 MIF_LOG(ERR, "Failed to get port %u link info: %s",
314 mq->in_port, rte_strerror(-ret));
318 /* consume interrupt */
319 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
320 size = read(mq->intr_handle.fd, &b, sizeof(b));
322 ring_size = 1 << mq->log2_ring_size;
323 mask = ring_size - 1;
325 if (type == MEMIF_RING_S2M) {
326 cur_slot = mq->last_head;
327 last_slot = __atomic_load_n(&ring->head, __ATOMIC_ACQUIRE);
329 cur_slot = mq->last_tail;
330 last_slot = __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE);
333 if (cur_slot == last_slot)
335 n_slots = last_slot - cur_slot;
337 while (n_slots && n_rx_pkts < nb_pkts) {
338 mbuf_head = rte_pktmbuf_alloc(mq->mempool);
339 if (unlikely(mbuf_head == NULL))
342 mbuf->port = mq->in_port;
345 s0 = cur_slot & mask;
346 d0 = &ring->desc[s0];
348 src_len = d0->length;
353 dst_len = mbuf_size - dst_off;
358 /* store pointer to tail */
360 mbuf = rte_pktmbuf_alloc(mq->mempool);
361 if (unlikely(mbuf == NULL))
363 mbuf->port = mq->in_port;
364 ret = memif_pktmbuf_chain(mbuf_head, mbuf_tail, mbuf);
365 if (unlikely(ret < 0)) {
366 MIF_LOG(ERR, "number-of-segments-overflow");
367 rte_pktmbuf_free(mbuf);
371 cp_len = RTE_MIN(dst_len, src_len);
373 rte_pktmbuf_data_len(mbuf) += cp_len;
374 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf);
375 if (mbuf != mbuf_head)
376 rte_pktmbuf_pkt_len(mbuf_head) += cp_len;
378 memcpy(rte_pktmbuf_mtod_offset(mbuf, void *, dst_off),
379 (uint8_t *)memif_get_buffer(proc_private, d0) + src_off,
390 if (d0->flags & MEMIF_DESC_FLAG_NEXT)
393 mq->n_bytes += rte_pktmbuf_pkt_len(mbuf_head);
399 if (type == MEMIF_RING_S2M) {
400 __atomic_store_n(&ring->tail, cur_slot, __ATOMIC_RELEASE);
401 mq->last_head = cur_slot;
403 mq->last_tail = cur_slot;
407 if (type == MEMIF_RING_M2S) {
408 head = __atomic_load_n(&ring->head, __ATOMIC_ACQUIRE);
409 n_slots = ring_size - head + mq->last_tail;
413 d0 = &ring->desc[s0];
414 d0->length = pmd->run.pkt_buffer_size;
416 __atomic_store_n(&ring->head, head, __ATOMIC_RELEASE);
419 mq->n_pkts += n_rx_pkts;
424 eth_memif_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
426 struct memif_queue *mq = queue;
427 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
428 struct pmd_process_private *proc_private =
429 rte_eth_devices[mq->in_port].process_private;
430 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
431 uint16_t cur_slot, last_slot, n_slots, ring_size, mask, s0, head;
432 uint16_t n_rx_pkts = 0;
434 struct rte_mbuf *mbuf, *mbuf_tail;
435 struct rte_mbuf *mbuf_head = NULL;
437 struct rte_eth_link link;
439 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
441 if (unlikely(ring == NULL)) {
442 /* Secondary process will attempt to request regions. */
443 rte_eth_link_get(mq->in_port, &link);
447 /* consume interrupt */
448 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) {
450 ssize_t size __rte_unused;
451 size = read(mq->intr_handle.fd, &b, sizeof(b));
454 ring_size = 1 << mq->log2_ring_size;
455 mask = ring_size - 1;
457 cur_slot = mq->last_tail;
458 last_slot = ring->tail;
459 if (cur_slot == last_slot)
461 n_slots = last_slot - cur_slot;
463 while (n_slots && n_rx_pkts < nb_pkts) {
464 s0 = cur_slot & mask;
466 d0 = &ring->desc[s0];
467 mbuf_head = mq->buffers[s0];
471 /* prefetch next descriptor */
472 if (n_rx_pkts + 1 < nb_pkts)
473 rte_prefetch0(&ring->desc[(cur_slot + 1) & mask]);
475 mbuf->port = mq->in_port;
476 rte_pktmbuf_data_len(mbuf) = d0->length;
477 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf);
479 mq->n_bytes += rte_pktmbuf_data_len(mbuf);
483 if (d0->flags & MEMIF_DESC_FLAG_NEXT) {
484 s0 = cur_slot & mask;
485 d0 = &ring->desc[s0];
487 mbuf = mq->buffers[s0];
488 ret = memif_pktmbuf_chain(mbuf_head, mbuf_tail, mbuf);
489 if (unlikely(ret < 0)) {
490 MIF_LOG(ERR, "number-of-segments-overflow");
500 mq->last_tail = cur_slot;
502 /* Supply master with new buffers */
505 n_slots = ring_size - head + mq->last_tail;
510 ret = rte_pktmbuf_alloc_bulk(mq->mempool, &mq->buffers[head & mask], n_slots);
511 if (unlikely(ret < 0))
517 rte_prefetch0(mq->buffers[head & mask]);
518 d0 = &ring->desc[s0];
519 /* store buffer header */
520 mbuf = mq->buffers[s0];
521 /* populate descriptor */
522 d0->length = rte_pktmbuf_data_room_size(mq->mempool) -
523 RTE_PKTMBUF_HEADROOM;
525 d0->offset = rte_pktmbuf_mtod(mbuf, uint8_t *) -
526 (uint8_t *)proc_private->regions[d0->region]->addr;
532 mq->n_pkts += n_rx_pkts;
538 eth_memif_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
540 struct memif_queue *mq = queue;
541 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
542 struct pmd_process_private *proc_private =
543 rte_eth_devices[mq->in_port].process_private;
544 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
545 uint16_t slot, saved_slot, n_free, ring_size, mask, n_tx_pkts = 0;
546 uint16_t src_len, src_off, dst_len, dst_off, cp_len;
547 memif_ring_type_t type = mq->type;
549 struct rte_mbuf *mbuf;
550 struct rte_mbuf *mbuf_head;
553 struct rte_eth_link link;
555 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
557 if (unlikely(ring == NULL)) {
560 /* Secondary process will attempt to request regions. */
561 ret = rte_eth_link_get(mq->in_port, &link);
563 MIF_LOG(ERR, "Failed to get port %u link info: %s",
564 mq->in_port, rte_strerror(-ret));
568 ring_size = 1 << mq->log2_ring_size;
569 mask = ring_size - 1;
571 n_free = __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE) - mq->last_tail;
572 mq->last_tail += n_free;
574 if (type == MEMIF_RING_S2M) {
575 slot = __atomic_load_n(&ring->head, __ATOMIC_ACQUIRE);
576 n_free = ring_size - slot + mq->last_tail;
578 slot = __atomic_load_n(&ring->tail, __ATOMIC_ACQUIRE);
579 n_free = __atomic_load_n(&ring->head, __ATOMIC_ACQUIRE) - slot;
582 while (n_tx_pkts < nb_pkts && n_free) {
587 d0 = &ring->desc[slot & mask];
589 dst_len = (type == MEMIF_RING_S2M) ?
590 pmd->run.pkt_buffer_size : d0->length;
594 src_len = rte_pktmbuf_data_len(mbuf);
601 d0->flags |= MEMIF_DESC_FLAG_NEXT;
602 d0 = &ring->desc[slot & mask];
604 dst_len = (type == MEMIF_RING_S2M) ?
605 pmd->run.pkt_buffer_size : d0->length;
612 cp_len = RTE_MIN(dst_len, src_len);
614 memcpy((uint8_t *)memif_get_buffer(proc_private, d0) + dst_off,
615 rte_pktmbuf_mtod_offset(mbuf, void *, src_off),
618 mq->n_bytes += cp_len;
624 d0->length = dst_off;
627 if (rte_pktmbuf_is_contiguous(mbuf) == 0) {
635 rte_pktmbuf_free(mbuf_head);
639 if (type == MEMIF_RING_S2M)
640 __atomic_store_n(&ring->head, slot, __ATOMIC_RELEASE);
642 __atomic_store_n(&ring->tail, slot, __ATOMIC_RELEASE);
644 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) {
646 size = write(mq->intr_handle.fd, &a, sizeof(a));
647 if (unlikely(size < 0)) {
649 "Failed to send interrupt. %s", strerror(errno));
653 mq->n_pkts += n_tx_pkts;
659 memif_tx_one_zc(struct pmd_process_private *proc_private, struct memif_queue *mq,
660 memif_ring_t *ring, struct rte_mbuf *mbuf, const uint16_t mask,
661 uint16_t slot, uint16_t n_free)
667 /* store pointer to mbuf to free it later */
668 mq->buffers[slot & mask] = mbuf;
669 /* Increment refcnt to make sure the buffer is not freed before master
670 * receives it. (current segment)
672 rte_mbuf_refcnt_update(mbuf, 1);
673 /* populate descriptor */
674 d0 = &ring->desc[slot & mask];
675 d0->length = rte_pktmbuf_data_len(mbuf);
676 /* FIXME: get region index */
678 d0->offset = rte_pktmbuf_mtod(mbuf, uint8_t *) -
679 (uint8_t *)proc_private->regions[d0->region]->addr;
682 /* check if buffer is chained */
683 if (rte_pktmbuf_is_contiguous(mbuf) == 0) {
686 /* mark buffer as chained */
687 d0->flags |= MEMIF_DESC_FLAG_NEXT;
690 /* update counters */
700 eth_memif_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
702 struct memif_queue *mq = queue;
703 struct pmd_internals *pmd = rte_eth_devices[mq->in_port].data->dev_private;
704 struct pmd_process_private *proc_private =
705 rte_eth_devices[mq->in_port].process_private;
706 memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq);
707 uint16_t slot, n_free, ring_size, mask, n_tx_pkts = 0;
708 memif_ring_type_t type = mq->type;
709 struct rte_eth_link link;
711 if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0))
713 if (unlikely(ring == NULL)) {
714 /* Secondary process will attempt to request regions. */
715 rte_eth_link_get(mq->in_port, &link);
719 ring_size = 1 << mq->log2_ring_size;
720 mask = ring_size - 1;
722 /* free mbufs received by master */
723 memif_free_stored_mbufs(proc_private, mq);
725 /* ring type always MEMIF_RING_S2M */
727 n_free = ring_size - ring->head + mq->last_tail;
731 while (n_free && (n_tx_pkts < nb_pkts)) {
732 while ((n_free > 4) && ((nb_pkts - n_tx_pkts) > 4)) {
733 if ((nb_pkts - n_tx_pkts) > 8) {
734 rte_prefetch0(*bufs + 4);
735 rte_prefetch0(*bufs + 5);
736 rte_prefetch0(*bufs + 6);
737 rte_prefetch0(*bufs + 7);
739 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
741 if (unlikely(used_slots < 1))
745 n_free -= used_slots;
747 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
749 if (unlikely(used_slots < 1))
753 n_free -= used_slots;
755 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
757 if (unlikely(used_slots < 1))
761 n_free -= used_slots;
763 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
765 if (unlikely(used_slots < 1))
769 n_free -= used_slots;
771 used_slots = memif_tx_one_zc(proc_private, mq, ring, *bufs++,
773 if (unlikely(used_slots < 1))
777 n_free -= used_slots;
782 /* update ring pointers */
783 if (type == MEMIF_RING_S2M)
788 /* Send interrupt, if enabled. */
789 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) {
791 ssize_t size = write(mq->intr_handle.fd, &a, sizeof(a));
792 if (unlikely(size < 0)) {
794 "Failed to send interrupt. %s", strerror(errno));
798 /* increment queue counters */
799 mq->n_pkts += n_tx_pkts;
805 memif_free_regions(struct rte_eth_dev *dev)
807 struct pmd_process_private *proc_private = dev->process_private;
808 struct pmd_internals *pmd = dev->data->dev_private;
810 struct memif_region *r;
812 /* regions are allocated contiguously, so it's
813 * enough to loop until 'proc_private->regions_num'
815 for (i = 0; i < proc_private->regions_num; i++) {
816 r = proc_private->regions[i];
818 /* This is memzone */
819 if (i > 0 && (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)) {
824 if (r->addr != NULL) {
825 munmap(r->addr, r->region_size);
832 proc_private->regions[i] = NULL;
835 proc_private->regions_num = 0;
839 memif_region_init_zc(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
842 struct pmd_process_private *proc_private = (struct pmd_process_private *)arg;
843 struct memif_region *r;
845 if (proc_private->regions_num < 1) {
846 MIF_LOG(ERR, "Missing descriptor region");
850 r = proc_private->regions[proc_private->regions_num - 1];
852 if (r->addr != msl->base_va)
853 r = proc_private->regions[++proc_private->regions_num - 1];
856 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
858 MIF_LOG(ERR, "Failed to alloc memif region.");
862 r->addr = msl->base_va;
863 r->region_size = ms->len;
864 r->fd = rte_memseg_get_fd(ms);
867 r->pkt_buffer_offset = 0;
869 proc_private->regions[proc_private->regions_num - 1] = r;
871 r->region_size += ms->len;
878 memif_region_init_shm(struct rte_eth_dev *dev, uint8_t has_buffers)
880 struct pmd_internals *pmd = dev->data->dev_private;
881 struct pmd_process_private *proc_private = dev->process_private;
882 char shm_name[ETH_MEMIF_SHM_NAME_SIZE];
884 struct memif_region *r;
886 if (proc_private->regions_num >= ETH_MEMIF_MAX_REGION_NUM) {
887 MIF_LOG(ERR, "Too many regions.");
891 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
893 MIF_LOG(ERR, "Failed to alloc memif region.");
897 /* calculate buffer offset */
898 r->pkt_buffer_offset = (pmd->run.num_s2m_rings + pmd->run.num_m2s_rings) *
899 (sizeof(memif_ring_t) + sizeof(memif_desc_t) *
900 (1 << pmd->run.log2_ring_size));
902 r->region_size = r->pkt_buffer_offset;
903 /* if region has buffers, add buffers size to region_size */
904 if (has_buffers == 1)
905 r->region_size += (uint32_t)(pmd->run.pkt_buffer_size *
906 (1 << pmd->run.log2_ring_size) *
907 (pmd->run.num_s2m_rings +
908 pmd->run.num_m2s_rings));
910 memset(shm_name, 0, sizeof(char) * ETH_MEMIF_SHM_NAME_SIZE);
911 snprintf(shm_name, ETH_MEMIF_SHM_NAME_SIZE, "memif_region_%d",
912 proc_private->regions_num);
914 r->fd = memfd_create(shm_name, MFD_ALLOW_SEALING);
916 MIF_LOG(ERR, "Failed to create shm file: %s.", strerror(errno));
921 ret = fcntl(r->fd, F_ADD_SEALS, F_SEAL_SHRINK);
923 MIF_LOG(ERR, "Failed to add seals to shm file: %s.", strerror(errno));
927 ret = ftruncate(r->fd, r->region_size);
929 MIF_LOG(ERR, "Failed to truncate shm file: %s.", strerror(errno));
933 r->addr = mmap(NULL, r->region_size, PROT_READ |
934 PROT_WRITE, MAP_SHARED, r->fd, 0);
935 if (r->addr == MAP_FAILED) {
936 MIF_LOG(ERR, "Failed to mmap shm region: %s.", strerror(ret));
941 proc_private->regions[proc_private->regions_num] = r;
942 proc_private->regions_num++;
955 memif_regions_init(struct rte_eth_dev *dev)
957 struct pmd_internals *pmd = dev->data->dev_private;
961 * Zero-copy exposes dpdk memory.
962 * Each memseg list will be represented by memif region.
963 * Zero-copy regions indexing: memseg list idx + 1,
964 * as we already have region 0 reserved for descriptors.
966 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
967 /* create region idx 0 containing descriptors */
968 ret = memif_region_init_shm(dev, 0);
971 ret = rte_memseg_walk(memif_region_init_zc, (void *)dev->process_private);
975 /* create one memory region contaning rings and buffers */
976 ret = memif_region_init_shm(dev, /* has buffers */ 1);
985 memif_init_rings(struct rte_eth_dev *dev)
987 struct pmd_internals *pmd = dev->data->dev_private;
988 struct pmd_process_private *proc_private = dev->process_private;
993 for (i = 0; i < pmd->run.num_s2m_rings; i++) {
994 ring = memif_get_ring(pmd, proc_private, MEMIF_RING_S2M, i);
995 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
996 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
997 ring->cookie = MEMIF_COOKIE;
1000 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)
1003 for (j = 0; j < (1 << pmd->run.log2_ring_size); j++) {
1004 slot = i * (1 << pmd->run.log2_ring_size) + j;
1005 ring->desc[j].region = 0;
1006 ring->desc[j].offset =
1007 proc_private->regions[0]->pkt_buffer_offset +
1008 (uint32_t)(slot * pmd->run.pkt_buffer_size);
1009 ring->desc[j].length = pmd->run.pkt_buffer_size;
1013 for (i = 0; i < pmd->run.num_m2s_rings; i++) {
1014 ring = memif_get_ring(pmd, proc_private, MEMIF_RING_M2S, i);
1015 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1016 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1017 ring->cookie = MEMIF_COOKIE;
1020 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)
1023 for (j = 0; j < (1 << pmd->run.log2_ring_size); j++) {
1024 slot = (i + pmd->run.num_s2m_rings) *
1025 (1 << pmd->run.log2_ring_size) + j;
1026 ring->desc[j].region = 0;
1027 ring->desc[j].offset =
1028 proc_private->regions[0]->pkt_buffer_offset +
1029 (uint32_t)(slot * pmd->run.pkt_buffer_size);
1030 ring->desc[j].length = pmd->run.pkt_buffer_size;
1035 /* called only by slave */
1037 memif_init_queues(struct rte_eth_dev *dev)
1039 struct pmd_internals *pmd = dev->data->dev_private;
1040 struct memif_queue *mq;
1043 for (i = 0; i < pmd->run.num_s2m_rings; i++) {
1044 mq = dev->data->tx_queues[i];
1045 mq->log2_ring_size = pmd->run.log2_ring_size;
1046 /* queues located only in region 0 */
1048 mq->ring_offset = memif_get_ring_offset(dev, mq, MEMIF_RING_S2M, i);
1051 mq->intr_handle.fd = eventfd(0, EFD_NONBLOCK);
1052 if (mq->intr_handle.fd < 0) {
1054 "Failed to create eventfd for tx queue %d: %s.", i,
1058 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1059 mq->buffers = rte_zmalloc("bufs", sizeof(struct rte_mbuf *) *
1060 (1 << mq->log2_ring_size), 0);
1061 if (mq->buffers == NULL)
1066 for (i = 0; i < pmd->run.num_m2s_rings; i++) {
1067 mq = dev->data->rx_queues[i];
1068 mq->log2_ring_size = pmd->run.log2_ring_size;
1069 /* queues located only in region 0 */
1071 mq->ring_offset = memif_get_ring_offset(dev, mq, MEMIF_RING_M2S, i);
1074 mq->intr_handle.fd = eventfd(0, EFD_NONBLOCK);
1075 if (mq->intr_handle.fd < 0) {
1077 "Failed to create eventfd for rx queue %d: %s.", i,
1081 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1082 mq->buffers = rte_zmalloc("bufs", sizeof(struct rte_mbuf *) *
1083 (1 << mq->log2_ring_size), 0);
1084 if (mq->buffers == NULL)
1092 memif_init_regions_and_queues(struct rte_eth_dev *dev)
1096 ret = memif_regions_init(dev);
1100 memif_init_rings(dev);
1102 ret = memif_init_queues(dev);
1110 memif_connect(struct rte_eth_dev *dev)
1112 struct pmd_internals *pmd = dev->data->dev_private;
1113 struct pmd_process_private *proc_private = dev->process_private;
1114 struct memif_region *mr;
1115 struct memif_queue *mq;
1119 for (i = 0; i < proc_private->regions_num; i++) {
1120 mr = proc_private->regions[i];
1122 if (mr->addr == NULL) {
1125 mr->addr = mmap(NULL, mr->region_size,
1126 PROT_READ | PROT_WRITE,
1127 MAP_SHARED, mr->fd, 0);
1128 if (mr->addr == MAP_FAILED) {
1129 MIF_LOG(ERR, "mmap failed: %s\n",
1134 if (i > 0 && (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY)) {
1135 /* close memseg file */
1142 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1143 for (i = 0; i < pmd->run.num_s2m_rings; i++) {
1144 mq = (pmd->role == MEMIF_ROLE_SLAVE) ?
1145 dev->data->tx_queues[i] : dev->data->rx_queues[i];
1146 ring = memif_get_ring_from_queue(proc_private, mq);
1147 if (ring == NULL || ring->cookie != MEMIF_COOKIE) {
1148 MIF_LOG(ERR, "Wrong ring");
1151 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1152 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1155 /* enable polling mode */
1156 if (pmd->role == MEMIF_ROLE_MASTER)
1157 ring->flags = MEMIF_RING_FLAG_MASK_INT;
1159 for (i = 0; i < pmd->run.num_m2s_rings; i++) {
1160 mq = (pmd->role == MEMIF_ROLE_SLAVE) ?
1161 dev->data->rx_queues[i] : dev->data->tx_queues[i];
1162 ring = memif_get_ring_from_queue(proc_private, mq);
1163 if (ring == NULL || ring->cookie != MEMIF_COOKIE) {
1164 MIF_LOG(ERR, "Wrong ring");
1167 __atomic_store_n(&ring->head, 0, __ATOMIC_RELAXED);
1168 __atomic_store_n(&ring->tail, 0, __ATOMIC_RELAXED);
1171 /* enable polling mode */
1172 if (pmd->role == MEMIF_ROLE_SLAVE)
1173 ring->flags = MEMIF_RING_FLAG_MASK_INT;
1176 pmd->flags &= ~ETH_MEMIF_FLAG_CONNECTING;
1177 pmd->flags |= ETH_MEMIF_FLAG_CONNECTED;
1178 dev->data->dev_link.link_status = ETH_LINK_UP;
1180 MIF_LOG(INFO, "Connected.");
1185 memif_dev_start(struct rte_eth_dev *dev)
1187 struct pmd_internals *pmd = dev->data->dev_private;
1190 switch (pmd->role) {
1191 case MEMIF_ROLE_SLAVE:
1192 ret = memif_connect_slave(dev);
1194 case MEMIF_ROLE_MASTER:
1195 ret = memif_connect_master(dev);
1198 MIF_LOG(ERR, "Unknown role: %d.", pmd->role);
1207 memif_dev_close(struct rte_eth_dev *dev)
1209 struct pmd_internals *pmd = dev->data->dev_private;
1212 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1213 memif_msg_enq_disconnect(pmd->cc, "Device closed", 0);
1214 memif_disconnect(dev);
1216 for (i = 0; i < dev->data->nb_rx_queues; i++)
1217 (*dev->dev_ops->rx_queue_release)(dev->data->rx_queues[i]);
1218 for (i = 0; i < dev->data->nb_tx_queues; i++)
1219 (*dev->dev_ops->tx_queue_release)(dev->data->tx_queues[i]);
1221 memif_socket_remove_device(dev);
1223 memif_disconnect(dev);
1226 rte_free(dev->process_private);
1230 memif_dev_configure(struct rte_eth_dev *dev)
1232 struct pmd_internals *pmd = dev->data->dev_private;
1238 pmd->cfg.num_s2m_rings = (pmd->role == MEMIF_ROLE_SLAVE) ?
1239 dev->data->nb_tx_queues : dev->data->nb_rx_queues;
1245 pmd->cfg.num_m2s_rings = (pmd->role == MEMIF_ROLE_SLAVE) ?
1246 dev->data->nb_rx_queues : dev->data->nb_tx_queues;
1252 memif_tx_queue_setup(struct rte_eth_dev *dev,
1254 uint16_t nb_tx_desc __rte_unused,
1255 unsigned int socket_id __rte_unused,
1256 const struct rte_eth_txconf *tx_conf __rte_unused)
1258 struct pmd_internals *pmd = dev->data->dev_private;
1259 struct memif_queue *mq;
1261 mq = rte_zmalloc("tx-queue", sizeof(struct memif_queue), 0);
1263 MIF_LOG(ERR, "Failed to allocate tx queue id: %u", qid);
1268 (pmd->role == MEMIF_ROLE_SLAVE) ? MEMIF_RING_S2M : MEMIF_RING_M2S;
1271 mq->intr_handle.fd = -1;
1272 mq->intr_handle.type = RTE_INTR_HANDLE_EXT;
1273 mq->in_port = dev->data->port_id;
1274 dev->data->tx_queues[qid] = mq;
1280 memif_rx_queue_setup(struct rte_eth_dev *dev,
1282 uint16_t nb_rx_desc __rte_unused,
1283 unsigned int socket_id __rte_unused,
1284 const struct rte_eth_rxconf *rx_conf __rte_unused,
1285 struct rte_mempool *mb_pool)
1287 struct pmd_internals *pmd = dev->data->dev_private;
1288 struct memif_queue *mq;
1290 mq = rte_zmalloc("rx-queue", sizeof(struct memif_queue), 0);
1292 MIF_LOG(ERR, "Failed to allocate rx queue id: %u", qid);
1296 mq->type = (pmd->role == MEMIF_ROLE_SLAVE) ? MEMIF_RING_M2S : MEMIF_RING_S2M;
1299 mq->intr_handle.fd = -1;
1300 mq->intr_handle.type = RTE_INTR_HANDLE_EXT;
1301 mq->mempool = mb_pool;
1302 mq->in_port = dev->data->port_id;
1303 dev->data->rx_queues[qid] = mq;
1309 memif_queue_release(void *queue)
1311 struct memif_queue *mq = (struct memif_queue *)queue;
1320 memif_link_update(struct rte_eth_dev *dev,
1321 int wait_to_complete __rte_unused)
1323 struct pmd_process_private *proc_private;
1325 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1326 proc_private = dev->process_private;
1327 if (dev->data->dev_link.link_status == ETH_LINK_UP &&
1328 proc_private->regions_num == 0) {
1329 memif_mp_request_regions(dev);
1330 } else if (dev->data->dev_link.link_status == ETH_LINK_DOWN &&
1331 proc_private->regions_num > 0) {
1332 memif_free_regions(dev);
1339 memif_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1341 struct pmd_internals *pmd = dev->data->dev_private;
1342 struct memif_queue *mq;
1346 stats->ipackets = 0;
1348 stats->opackets = 0;
1351 tmp = (pmd->role == MEMIF_ROLE_SLAVE) ? pmd->run.num_s2m_rings :
1352 pmd->run.num_m2s_rings;
1353 nq = (tmp < RTE_ETHDEV_QUEUE_STAT_CNTRS) ? tmp :
1354 RTE_ETHDEV_QUEUE_STAT_CNTRS;
1357 for (i = 0; i < nq; i++) {
1358 mq = dev->data->rx_queues[i];
1359 stats->q_ipackets[i] = mq->n_pkts;
1360 stats->q_ibytes[i] = mq->n_bytes;
1361 stats->ipackets += mq->n_pkts;
1362 stats->ibytes += mq->n_bytes;
1365 tmp = (pmd->role == MEMIF_ROLE_SLAVE) ? pmd->run.num_m2s_rings :
1366 pmd->run.num_s2m_rings;
1367 nq = (tmp < RTE_ETHDEV_QUEUE_STAT_CNTRS) ? tmp :
1368 RTE_ETHDEV_QUEUE_STAT_CNTRS;
1371 for (i = 0; i < nq; i++) {
1372 mq = dev->data->tx_queues[i];
1373 stats->q_opackets[i] = mq->n_pkts;
1374 stats->q_obytes[i] = mq->n_bytes;
1375 stats->opackets += mq->n_pkts;
1376 stats->obytes += mq->n_bytes;
1382 memif_stats_reset(struct rte_eth_dev *dev)
1384 struct pmd_internals *pmd = dev->data->dev_private;
1386 struct memif_queue *mq;
1388 for (i = 0; i < pmd->run.num_s2m_rings; i++) {
1389 mq = (pmd->role == MEMIF_ROLE_SLAVE) ? dev->data->tx_queues[i] :
1390 dev->data->rx_queues[i];
1394 for (i = 0; i < pmd->run.num_m2s_rings; i++) {
1395 mq = (pmd->role == MEMIF_ROLE_SLAVE) ? dev->data->rx_queues[i] :
1396 dev->data->tx_queues[i];
1405 memif_rx_queue_intr_enable(struct rte_eth_dev *dev __rte_unused,
1406 uint16_t qid __rte_unused)
1408 MIF_LOG(WARNING, "Interrupt mode not supported.");
1414 memif_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t qid __rte_unused)
1416 struct pmd_internals *pmd __rte_unused = dev->data->dev_private;
1421 static const struct eth_dev_ops ops = {
1422 .dev_start = memif_dev_start,
1423 .dev_close = memif_dev_close,
1424 .dev_infos_get = memif_dev_info,
1425 .dev_configure = memif_dev_configure,
1426 .tx_queue_setup = memif_tx_queue_setup,
1427 .rx_queue_setup = memif_rx_queue_setup,
1428 .rx_queue_release = memif_queue_release,
1429 .tx_queue_release = memif_queue_release,
1430 .rx_queue_intr_enable = memif_rx_queue_intr_enable,
1431 .rx_queue_intr_disable = memif_rx_queue_intr_disable,
1432 .link_update = memif_link_update,
1433 .stats_get = memif_stats_get,
1434 .stats_reset = memif_stats_reset,
1438 memif_create(struct rte_vdev_device *vdev, enum memif_role_t role,
1439 memif_interface_id_t id, uint32_t flags,
1440 const char *socket_filename,
1441 memif_log2_ring_size_t log2_ring_size,
1442 uint16_t pkt_buffer_size, const char *secret,
1443 struct rte_ether_addr *ether_addr)
1446 struct rte_eth_dev *eth_dev;
1447 struct rte_eth_dev_data *data;
1448 struct pmd_internals *pmd;
1449 struct pmd_process_private *process_private;
1450 const unsigned int numa_node = vdev->device.numa_node;
1451 const char *name = rte_vdev_device_name(vdev);
1453 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1454 if (eth_dev == NULL) {
1455 MIF_LOG(ERR, "%s: Unable to allocate device struct.", name);
1459 process_private = (struct pmd_process_private *)
1460 rte_zmalloc(name, sizeof(struct pmd_process_private),
1461 RTE_CACHE_LINE_SIZE);
1463 if (process_private == NULL) {
1464 MIF_LOG(ERR, "Failed to alloc memory for process private");
1467 eth_dev->process_private = process_private;
1469 pmd = eth_dev->data->dev_private;
1470 memset(pmd, 0, sizeof(*pmd));
1474 pmd->flags |= ETH_MEMIF_FLAG_DISABLED;
1476 /* Zero-copy flag irelevant to master. */
1477 if (pmd->role == MEMIF_ROLE_MASTER)
1478 pmd->flags &= ~ETH_MEMIF_FLAG_ZERO_COPY;
1480 ret = memif_socket_init(eth_dev, socket_filename);
1484 memset(pmd->secret, 0, sizeof(char) * ETH_MEMIF_SECRET_SIZE);
1486 strlcpy(pmd->secret, secret, sizeof(pmd->secret));
1488 pmd->cfg.log2_ring_size = log2_ring_size;
1489 /* set in .dev_configure() */
1490 pmd->cfg.num_s2m_rings = 0;
1491 pmd->cfg.num_m2s_rings = 0;
1493 pmd->cfg.pkt_buffer_size = pkt_buffer_size;
1494 rte_spinlock_init(&pmd->cc_lock);
1496 data = eth_dev->data;
1497 data->dev_private = pmd;
1498 data->numa_node = numa_node;
1499 data->dev_link = pmd_link;
1500 data->mac_addrs = ether_addr;
1501 data->promiscuous = 1;
1503 eth_dev->dev_ops = &ops;
1504 eth_dev->device = &vdev->device;
1505 if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) {
1506 eth_dev->rx_pkt_burst = eth_memif_rx_zc;
1507 eth_dev->tx_pkt_burst = eth_memif_tx_zc;
1509 eth_dev->rx_pkt_burst = eth_memif_rx;
1510 eth_dev->tx_pkt_burst = eth_memif_tx;
1514 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1516 rte_eth_dev_probing_finish(eth_dev);
1522 memif_set_role(const char *key __rte_unused, const char *value,
1525 enum memif_role_t *role = (enum memif_role_t *)extra_args;
1527 if (strstr(value, "master") != NULL) {
1528 *role = MEMIF_ROLE_MASTER;
1529 } else if (strstr(value, "slave") != NULL) {
1530 *role = MEMIF_ROLE_SLAVE;
1532 MIF_LOG(ERR, "Unknown role: %s.", value);
1539 memif_set_zc(const char *key __rte_unused, const char *value, void *extra_args)
1541 uint32_t *flags = (uint32_t *)extra_args;
1543 if (strstr(value, "yes") != NULL) {
1544 if (!rte_mcfg_get_single_file_segments()) {
1545 MIF_LOG(ERR, "Zero-copy doesn't support multi-file segments.");
1548 *flags |= ETH_MEMIF_FLAG_ZERO_COPY;
1549 } else if (strstr(value, "no") != NULL) {
1550 *flags &= ~ETH_MEMIF_FLAG_ZERO_COPY;
1552 MIF_LOG(ERR, "Failed to parse zero-copy param: %s.", value);
1559 memif_set_id(const char *key __rte_unused, const char *value, void *extra_args)
1561 memif_interface_id_t *id = (memif_interface_id_t *)extra_args;
1563 /* even if parsing fails, 0 is a valid id */
1564 *id = strtoul(value, NULL, 10);
1569 memif_set_bs(const char *key __rte_unused, const char *value, void *extra_args)
1572 uint16_t *pkt_buffer_size = (uint16_t *)extra_args;
1574 tmp = strtoul(value, NULL, 10);
1575 if (tmp == 0 || tmp > 0xFFFF) {
1576 MIF_LOG(ERR, "Invalid buffer size: %s.", value);
1579 *pkt_buffer_size = tmp;
1584 memif_set_rs(const char *key __rte_unused, const char *value, void *extra_args)
1587 memif_log2_ring_size_t *log2_ring_size =
1588 (memif_log2_ring_size_t *)extra_args;
1590 tmp = strtoul(value, NULL, 10);
1591 if (tmp == 0 || tmp > ETH_MEMIF_MAX_LOG2_RING_SIZE) {
1592 MIF_LOG(ERR, "Invalid ring size: %s (max %u).",
1593 value, ETH_MEMIF_MAX_LOG2_RING_SIZE);
1596 *log2_ring_size = tmp;
1600 /* check if directory exists and if we have permission to read/write */
1602 memif_check_socket_filename(const char *filename)
1604 char *dir = NULL, *tmp;
1608 if (strlen(filename) >= MEMIF_SOCKET_UN_SIZE) {
1609 MIF_LOG(ERR, "Unix socket address too long (max 108).");
1613 tmp = strrchr(filename, '/');
1615 idx = tmp - filename;
1616 dir = rte_zmalloc("memif_tmp", sizeof(char) * (idx + 1), 0);
1618 MIF_LOG(ERR, "Failed to allocate memory.");
1621 strlcpy(dir, filename, sizeof(char) * (idx + 1));
1624 if (dir == NULL || (faccessat(-1, dir, F_OK | R_OK |
1625 W_OK, AT_EACCESS) < 0)) {
1626 MIF_LOG(ERR, "Invalid socket directory.");
1637 memif_set_socket_filename(const char *key __rte_unused, const char *value,
1640 const char **socket_filename = (const char **)extra_args;
1642 *socket_filename = value;
1643 return memif_check_socket_filename(*socket_filename);
1647 memif_set_mac(const char *key __rte_unused, const char *value, void *extra_args)
1649 struct rte_ether_addr *ether_addr = (struct rte_ether_addr *)extra_args;
1651 if (rte_ether_unformat_addr(value, ether_addr) < 0)
1652 MIF_LOG(WARNING, "Failed to parse mac '%s'.", value);
1657 memif_set_secret(const char *key __rte_unused, const char *value, void *extra_args)
1659 const char **secret = (const char **)extra_args;
1666 rte_pmd_memif_probe(struct rte_vdev_device *vdev)
1668 RTE_BUILD_BUG_ON(sizeof(memif_msg_t) != 128);
1669 RTE_BUILD_BUG_ON(sizeof(memif_desc_t) != 16);
1671 struct rte_kvargs *kvlist;
1672 const char *name = rte_vdev_device_name(vdev);
1673 enum memif_role_t role = MEMIF_ROLE_SLAVE;
1674 memif_interface_id_t id = 0;
1675 uint16_t pkt_buffer_size = ETH_MEMIF_DEFAULT_PKT_BUFFER_SIZE;
1676 memif_log2_ring_size_t log2_ring_size = ETH_MEMIF_DEFAULT_RING_SIZE;
1677 const char *socket_filename = ETH_MEMIF_DEFAULT_SOCKET_FILENAME;
1679 const char *secret = NULL;
1680 struct rte_ether_addr *ether_addr = rte_zmalloc("",
1681 sizeof(struct rte_ether_addr), 0);
1682 struct rte_eth_dev *eth_dev;
1684 rte_eth_random_addr(ether_addr->addr_bytes);
1686 MIF_LOG(INFO, "Initialize MEMIF: %s.", name);
1688 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1689 eth_dev = rte_eth_dev_attach_secondary(name);
1691 MIF_LOG(ERR, "Failed to probe %s", name);
1695 eth_dev->dev_ops = &ops;
1696 eth_dev->device = &vdev->device;
1697 eth_dev->rx_pkt_burst = eth_memif_rx;
1698 eth_dev->tx_pkt_burst = eth_memif_tx;
1700 if (!rte_eal_primary_proc_alive(NULL)) {
1701 MIF_LOG(ERR, "Primary process is missing");
1705 eth_dev->process_private = (struct pmd_process_private *)
1707 sizeof(struct pmd_process_private),
1708 RTE_CACHE_LINE_SIZE);
1709 if (eth_dev->process_private == NULL) {
1711 "Failed to alloc memory for process private");
1715 rte_eth_dev_probing_finish(eth_dev);
1720 ret = rte_mp_action_register(MEMIF_MP_SEND_REGION, memif_mp_send_region);
1722 * Primary process can continue probing, but secondary process won't
1723 * be able to get memory regions information
1725 if (ret < 0 && rte_errno != EEXIST)
1726 MIF_LOG(WARNING, "Failed to register mp action callback: %s",
1727 strerror(rte_errno));
1729 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_arguments);
1731 /* parse parameters */
1732 if (kvlist != NULL) {
1733 ret = rte_kvargs_process(kvlist, ETH_MEMIF_ROLE_ARG,
1734 &memif_set_role, &role);
1737 ret = rte_kvargs_process(kvlist, ETH_MEMIF_ID_ARG,
1738 &memif_set_id, &id);
1741 ret = rte_kvargs_process(kvlist, ETH_MEMIF_PKT_BUFFER_SIZE_ARG,
1742 &memif_set_bs, &pkt_buffer_size);
1745 ret = rte_kvargs_process(kvlist, ETH_MEMIF_RING_SIZE_ARG,
1746 &memif_set_rs, &log2_ring_size);
1749 ret = rte_kvargs_process(kvlist, ETH_MEMIF_SOCKET_ARG,
1750 &memif_set_socket_filename,
1751 (void *)(&socket_filename));
1754 ret = rte_kvargs_process(kvlist, ETH_MEMIF_MAC_ARG,
1755 &memif_set_mac, ether_addr);
1758 ret = rte_kvargs_process(kvlist, ETH_MEMIF_ZC_ARG,
1759 &memif_set_zc, &flags);
1762 ret = rte_kvargs_process(kvlist, ETH_MEMIF_SECRET_ARG,
1763 &memif_set_secret, (void *)(&secret));
1768 /* create interface */
1769 ret = memif_create(vdev, role, id, flags, socket_filename,
1770 log2_ring_size, pkt_buffer_size, secret, ether_addr);
1774 rte_kvargs_free(kvlist);
1779 rte_pmd_memif_remove(struct rte_vdev_device *vdev)
1781 struct rte_eth_dev *eth_dev;
1783 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
1784 if (eth_dev == NULL)
1787 rte_eth_dev_close(eth_dev->data->port_id);
1792 static struct rte_vdev_driver pmd_memif_drv = {
1793 .probe = rte_pmd_memif_probe,
1794 .remove = rte_pmd_memif_remove,
1797 RTE_PMD_REGISTER_VDEV(net_memif, pmd_memif_drv);
1799 RTE_PMD_REGISTER_PARAM_STRING(net_memif,
1800 ETH_MEMIF_ID_ARG "=<int>"
1801 ETH_MEMIF_ROLE_ARG "=master|slave"
1802 ETH_MEMIF_PKT_BUFFER_SIZE_ARG "=<int>"
1803 ETH_MEMIF_RING_SIZE_ARG "=<int>"
1804 ETH_MEMIF_SOCKET_ARG "=<string>"
1805 ETH_MEMIF_MAC_ARG "=xx:xx:xx:xx:xx:xx"
1806 ETH_MEMIF_ZC_ARG "=yes|no"
1807 ETH_MEMIF_SECRET_ARG "=<string>");
1811 RTE_INIT(memif_init_log)
1813 memif_logtype = rte_log_register("pmd.net.memif");
1814 if (memif_logtype >= 0)
1815 rte_log_set_level(memif_logtype, RTE_LOG_NOTICE);