1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018-2019 Cisco Systems, Inc. All rights reserved.
9 #include <sys/socket.h>
10 #include <sys/ioctl.h>
13 #include <rte_version.h>
15 #include <rte_ether.h>
16 #include <rte_ethdev_driver.h>
17 #include <rte_ethdev_vdev.h>
18 #include <rte_malloc.h>
19 #include <rte_kvargs.h>
20 #include <rte_bus_vdev.h>
22 #include <rte_jhash.h>
23 #include <rte_string_fns.h>
25 #include "rte_eth_memif.h"
26 #include "memif_socket.h"
28 static void memif_intr_handler(void *arg);
31 memif_msg_send(int fd, memif_msg_t *msg, int afd)
33 struct msghdr mh = { 0 };
36 char ctl[CMSG_SPACE(sizeof(int))];
38 iov[0].iov_base = msg;
39 iov[0].iov_len = sizeof(memif_msg_t);
44 memset(&ctl, 0, sizeof(ctl));
46 mh.msg_controllen = sizeof(ctl);
47 cmsg = CMSG_FIRSTHDR(&mh);
48 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
49 cmsg->cmsg_level = SOL_SOCKET;
50 cmsg->cmsg_type = SCM_RIGHTS;
51 rte_memcpy(CMSG_DATA(cmsg), &afd, sizeof(int));
54 return sendmsg(fd, &mh, 0);
58 memif_msg_send_from_queue(struct memif_control_channel *cc)
62 struct memif_msg_queue_elt *e;
64 e = TAILQ_FIRST(&cc->msg_queue);
68 size = memif_msg_send(cc->intr_handle.fd, &e->msg, e->fd);
69 if (size != sizeof(memif_msg_t)) {
70 MIF_LOG(ERR, "sendmsg fail: %s.", strerror(errno));
73 MIF_LOG(DEBUG, "Sent msg type %u.", e->msg.type);
75 TAILQ_REMOVE(&cc->msg_queue, e, next);
81 static struct memif_msg_queue_elt *
82 memif_msg_enq(struct memif_control_channel *cc)
84 struct memif_msg_queue_elt *e;
86 e = rte_zmalloc("memif_msg", sizeof(struct memif_msg_queue_elt), 0);
88 MIF_LOG(ERR, "Failed to allocate control message.");
93 TAILQ_INSERT_TAIL(&cc->msg_queue, e, next);
99 memif_msg_enq_disconnect(struct memif_control_channel *cc, const char *reason,
102 struct memif_msg_queue_elt *e;
103 struct pmd_internals *pmd;
104 memif_msg_disconnect_t *d;
107 MIF_LOG(DEBUG, "Missing control channel.");
111 e = memif_msg_enq(cc);
113 MIF_LOG(WARNING, "Failed to enqueue disconnect message.");
117 d = &e->msg.disconnect;
119 e->msg.type = MEMIF_MSG_TYPE_DISCONNECT;
122 if (reason != NULL) {
123 strlcpy((char *)d->string, reason, sizeof(d->string));
124 if (cc->dev != NULL) {
125 pmd = cc->dev->data->dev_private;
126 strlcpy(pmd->local_disc_string, reason,
127 sizeof(pmd->local_disc_string));
133 memif_msg_enq_hello(struct memif_control_channel *cc)
135 struct memif_msg_queue_elt *e = memif_msg_enq(cc);
136 memif_msg_hello_t *h;
143 e->msg.type = MEMIF_MSG_TYPE_HELLO;
144 h->min_version = MEMIF_VERSION;
145 h->max_version = MEMIF_VERSION;
146 h->max_s2m_ring = ETH_MEMIF_MAX_NUM_Q_PAIRS;
147 h->max_m2s_ring = ETH_MEMIF_MAX_NUM_Q_PAIRS;
148 h->max_region = ETH_MEMIF_MAX_REGION_NUM - 1;
149 h->max_log2_ring_size = ETH_MEMIF_MAX_LOG2_RING_SIZE;
151 strlcpy((char *)h->name, rte_version(), sizeof(h->name));
157 memif_msg_receive_hello(struct rte_eth_dev *dev, memif_msg_t *msg)
159 struct pmd_internals *pmd = dev->data->dev_private;
160 memif_msg_hello_t *h = &msg->hello;
162 if (h->min_version > MEMIF_VERSION || h->max_version < MEMIF_VERSION) {
163 memif_msg_enq_disconnect(pmd->cc, "Incompatible memif version", 0);
167 /* Set parameters for active connection */
168 pmd->run.num_s2m_rings = RTE_MIN(h->max_s2m_ring + 1,
169 pmd->cfg.num_s2m_rings);
170 pmd->run.num_m2s_rings = RTE_MIN(h->max_m2s_ring + 1,
171 pmd->cfg.num_m2s_rings);
172 pmd->run.log2_ring_size = RTE_MIN(h->max_log2_ring_size,
173 pmd->cfg.log2_ring_size);
174 pmd->run.pkt_buffer_size = pmd->cfg.pkt_buffer_size;
176 strlcpy(pmd->remote_name, (char *)h->name, sizeof(pmd->remote_name));
178 MIF_LOG(DEBUG, "%s: Connecting to %s.",
179 rte_vdev_device_name(pmd->vdev), pmd->remote_name);
185 memif_msg_receive_init(struct memif_control_channel *cc, memif_msg_t *msg)
187 memif_msg_init_t *i = &msg->init;
188 struct memif_socket_dev_list_elt *elt;
189 struct pmd_internals *pmd;
190 struct rte_eth_dev *dev;
192 if (i->version != MEMIF_VERSION) {
193 memif_msg_enq_disconnect(cc, "Incompatible memif version", 0);
197 if (cc->socket == NULL) {
198 memif_msg_enq_disconnect(cc, "Device error", 0);
202 /* Find device with requested ID */
203 TAILQ_FOREACH(elt, &cc->socket->dev_queue, next) {
205 pmd = dev->data->dev_private;
206 if (((pmd->flags & ETH_MEMIF_FLAG_DISABLED) == 0) &&
208 /* assign control channel to device */
212 if (i->mode != MEMIF_INTERFACE_MODE_ETHERNET) {
213 memif_msg_enq_disconnect(pmd->cc,
214 "Only ethernet mode supported",
219 if (pmd->flags & (ETH_MEMIF_FLAG_CONNECTING |
220 ETH_MEMIF_FLAG_CONNECTED)) {
221 memif_msg_enq_disconnect(pmd->cc,
222 "Already connected", 0);
225 strlcpy(pmd->remote_name, (char *)i->name,
226 sizeof(pmd->remote_name));
228 if (*pmd->secret != '\0') {
229 if (*i->secret == '\0') {
230 memif_msg_enq_disconnect(pmd->cc,
231 "Secret required", 0);
234 if (strncmp(pmd->secret, (char *)i->secret,
235 ETH_MEMIF_SECRET_SIZE) != 0) {
236 memif_msg_enq_disconnect(pmd->cc,
237 "Incorrect secret", 0);
242 pmd->flags |= ETH_MEMIF_FLAG_CONNECTING;
247 /* ID not found on this socket */
248 MIF_LOG(DEBUG, "ID %u not found.", i->id);
249 memif_msg_enq_disconnect(cc, "ID not found", 0);
254 memif_msg_receive_add_region(struct rte_eth_dev *dev, memif_msg_t *msg,
257 struct pmd_internals *pmd = dev->data->dev_private;
258 struct pmd_process_private *proc_private = dev->process_private;
259 memif_msg_add_region_t *ar = &msg->add_region;
260 struct memif_region *r;
263 memif_msg_enq_disconnect(pmd->cc, "Missing region fd", 0);
267 if (ar->index >= ETH_MEMIF_MAX_REGION_NUM ||
268 ar->index != proc_private->regions_num ||
269 proc_private->regions[ar->index] != NULL) {
270 memif_msg_enq_disconnect(pmd->cc, "Invalid region index", 0);
274 r = rte_zmalloc("region", sizeof(struct memif_region), 0);
276 memif_msg_enq_disconnect(pmd->cc, "Failed to alloc memif region.", 0);
281 r->region_size = ar->size;
284 proc_private->regions[ar->index] = r;
285 proc_private->regions_num++;
291 memif_msg_receive_add_ring(struct rte_eth_dev *dev, memif_msg_t *msg, int fd)
293 struct pmd_internals *pmd = dev->data->dev_private;
294 memif_msg_add_ring_t *ar = &msg->add_ring;
295 struct memif_queue *mq;
298 memif_msg_enq_disconnect(pmd->cc, "Missing interrupt fd", 0);
302 /* check if we have enough queues */
303 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) {
304 if (ar->index >= pmd->cfg.num_s2m_rings) {
305 memif_msg_enq_disconnect(pmd->cc, "Invalid ring index", 0);
308 pmd->run.num_s2m_rings++;
310 if (ar->index >= pmd->cfg.num_m2s_rings) {
311 memif_msg_enq_disconnect(pmd->cc, "Invalid ring index", 0);
314 pmd->run.num_m2s_rings++;
317 mq = (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) ?
318 dev->data->rx_queues[ar->index] : dev->data->tx_queues[ar->index];
320 mq->intr_handle.fd = fd;
321 mq->log2_ring_size = ar->log2_ring_size;
322 mq->region = ar->region;
323 mq->ring_offset = ar->offset;
329 memif_msg_receive_connect(struct rte_eth_dev *dev, memif_msg_t *msg)
331 struct pmd_internals *pmd = dev->data->dev_private;
332 memif_msg_connect_t *c = &msg->connect;
335 ret = memif_connect(dev);
339 strlcpy(pmd->remote_if_name, (char *)c->if_name,
340 sizeof(pmd->remote_if_name));
341 MIF_LOG(INFO, "%s: Remote interface %s connected.",
342 rte_vdev_device_name(pmd->vdev), pmd->remote_if_name);
348 memif_msg_receive_connected(struct rte_eth_dev *dev, memif_msg_t *msg)
350 struct pmd_internals *pmd = dev->data->dev_private;
351 memif_msg_connected_t *c = &msg->connected;
354 ret = memif_connect(dev);
358 strlcpy(pmd->remote_if_name, (char *)c->if_name,
359 sizeof(pmd->remote_if_name));
360 MIF_LOG(INFO, "%s: Remote interface %s connected.",
361 rte_vdev_device_name(pmd->vdev), pmd->remote_if_name);
367 memif_msg_receive_disconnect(struct rte_eth_dev *dev, memif_msg_t *msg)
369 struct pmd_internals *pmd = dev->data->dev_private;
370 memif_msg_disconnect_t *d = &msg->disconnect;
372 memset(pmd->remote_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
373 strlcpy(pmd->remote_disc_string, (char *)d->string,
374 sizeof(pmd->remote_disc_string));
376 MIF_LOG(INFO, "%s: Disconnect received: %s",
377 rte_vdev_device_name(pmd->vdev), pmd->remote_disc_string);
379 memset(pmd->local_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
380 memif_disconnect(dev);
385 memif_msg_enq_ack(struct rte_eth_dev *dev)
387 struct pmd_internals *pmd = dev->data->dev_private;
388 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
392 e->msg.type = MEMIF_MSG_TYPE_ACK;
398 memif_msg_enq_init(struct rte_eth_dev *dev)
400 struct pmd_internals *pmd = dev->data->dev_private;
401 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
402 memif_msg_init_t *i = &e->msg.init;
408 e->msg.type = MEMIF_MSG_TYPE_INIT;
409 i->version = MEMIF_VERSION;
411 i->mode = MEMIF_INTERFACE_MODE_ETHERNET;
413 strlcpy((char *)i->name, rte_version(), sizeof(i->name));
415 if (*pmd->secret != '\0')
416 strlcpy((char *)i->secret, pmd->secret, sizeof(i->secret));
422 memif_msg_enq_add_region(struct rte_eth_dev *dev, uint8_t idx)
424 struct pmd_internals *pmd = dev->data->dev_private;
425 struct pmd_process_private *proc_private = dev->process_private;
426 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
427 memif_msg_add_region_t *ar;
428 struct memif_region *mr = proc_private->regions[idx];
433 ar = &e->msg.add_region;
434 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
437 ar->size = mr->region_size;
443 memif_msg_enq_add_ring(struct rte_eth_dev *dev, uint8_t idx,
444 memif_ring_type_t type)
446 struct pmd_internals *pmd = dev->data->dev_private;
447 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
448 struct memif_queue *mq;
449 memif_msg_add_ring_t *ar;
454 ar = &e->msg.add_ring;
455 mq = (type == MEMIF_RING_S2M) ? dev->data->tx_queues[idx] :
456 dev->data->rx_queues[idx];
458 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
459 e->fd = mq->intr_handle.fd;
461 ar->offset = mq->ring_offset;
462 ar->region = mq->region;
463 ar->log2_ring_size = mq->log2_ring_size;
464 ar->flags = (type == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
465 ar->private_hdr_size = 0;
471 memif_msg_enq_connect(struct rte_eth_dev *dev)
473 struct pmd_internals *pmd = dev->data->dev_private;
474 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
475 const char *name = rte_vdev_device_name(pmd->vdev);
476 memif_msg_connect_t *c;
482 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
483 strlcpy((char *)c->if_name, name, sizeof(c->if_name));
489 memif_msg_enq_connected(struct rte_eth_dev *dev)
491 struct pmd_internals *pmd = dev->data->dev_private;
492 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
493 const char *name = rte_vdev_device_name(pmd->vdev);
494 memif_msg_connected_t *c;
499 c = &e->msg.connected;
500 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
501 strlcpy((char *)c->if_name, name, sizeof(c->if_name));
507 memif_intr_unregister_handler(struct rte_intr_handle *intr_handle, void *arg)
509 struct memif_msg_queue_elt *elt;
510 struct memif_control_channel *cc = arg;
512 /* close control channel fd */
513 close(intr_handle->fd);
514 /* clear message queue */
515 while ((elt = TAILQ_FIRST(&cc->msg_queue)) != NULL) {
516 TAILQ_REMOVE(&cc->msg_queue, elt, next);
519 /* free control channel */
524 memif_disconnect(struct rte_eth_dev *dev)
526 struct pmd_internals *pmd = dev->data->dev_private;
527 struct pmd_process_private *proc_private = dev->process_private;
528 struct memif_msg_queue_elt *elt, *next;
529 struct memif_queue *mq;
530 struct rte_intr_handle *ih;
534 dev->data->dev_link.link_status = ETH_LINK_DOWN;
535 pmd->flags &= ~ETH_MEMIF_FLAG_CONNECTING;
536 pmd->flags &= ~ETH_MEMIF_FLAG_CONNECTED;
538 if (pmd->cc != NULL) {
539 /* Clear control message queue (except disconnect message if any). */
540 for (elt = TAILQ_FIRST(&pmd->cc->msg_queue); elt != NULL; elt = next) {
541 next = TAILQ_NEXT(elt, next);
542 if (elt->msg.type != MEMIF_MSG_TYPE_DISCONNECT) {
543 TAILQ_REMOVE(&pmd->cc->msg_queue, elt, next);
547 /* send disconnect message (if there is any in queue) */
548 memif_msg_send_from_queue(pmd->cc);
550 /* at this point, there should be no more messages in queue */
551 if (TAILQ_FIRST(&pmd->cc->msg_queue) != NULL) {
553 "Unexpected message(s) in message queue.");
556 ih = &pmd->cc->intr_handle;
558 ret = rte_intr_callback_unregister(ih,
562 * If callback is active (disconnecting based on
563 * received control message).
565 if (ret == -EAGAIN) {
566 ret = rte_intr_callback_unregister_pending(ih,
569 memif_intr_unregister_handler);
570 } else if (ret > 0) {
577 "Failed to unregister control channel callback.");
581 /* unconfig interrupts */
582 for (i = 0; i < pmd->cfg.num_s2m_rings; i++) {
583 if (pmd->role == MEMIF_ROLE_SLAVE) {
584 if (dev->data->tx_queues != NULL)
585 mq = dev->data->tx_queues[i];
589 if (dev->data->rx_queues != NULL)
590 mq = dev->data->rx_queues[i];
594 if (mq->intr_handle.fd > 0) {
595 close(mq->intr_handle.fd);
596 mq->intr_handle.fd = -1;
599 for (i = 0; i < pmd->cfg.num_m2s_rings; i++) {
600 if (pmd->role == MEMIF_ROLE_MASTER) {
601 if (dev->data->tx_queues != NULL)
602 mq = dev->data->tx_queues[i];
606 if (dev->data->rx_queues != NULL)
607 mq = dev->data->rx_queues[i];
611 if (mq->intr_handle.fd > 0) {
612 close(mq->intr_handle.fd);
613 mq->intr_handle.fd = -1;
617 memif_free_regions(proc_private);
619 /* reset connection configuration */
620 memset(&pmd->run, 0, sizeof(pmd->run));
622 MIF_LOG(DEBUG, "Disconnected.");
626 memif_msg_receive(struct memif_control_channel *cc)
628 char ctl[CMSG_SPACE(sizeof(int)) +
629 CMSG_SPACE(sizeof(struct ucred))] = { 0 };
630 struct msghdr mh = { 0 };
632 memif_msg_t msg = { 0 };
635 struct ucred *cr __rte_unused;
637 struct cmsghdr *cmsg;
640 struct pmd_internals *pmd;
641 struct pmd_process_private *proc_private;
643 iov[0].iov_base = (void *)&msg;
644 iov[0].iov_len = sizeof(memif_msg_t);
647 mh.msg_control = ctl;
648 mh.msg_controllen = sizeof(ctl);
650 size = recvmsg(cc->intr_handle.fd, &mh, 0);
651 if (size != sizeof(memif_msg_t)) {
652 MIF_LOG(DEBUG, "Invalid message size.");
653 memif_msg_enq_disconnect(cc, "Invalid message size", 0);
656 MIF_LOG(DEBUG, "Received msg type: %u.", msg.type);
658 cmsg = CMSG_FIRSTHDR(&mh);
660 if (cmsg->cmsg_level == SOL_SOCKET) {
661 if (cmsg->cmsg_type == SCM_CREDENTIALS)
662 cr = (struct ucred *)CMSG_DATA(cmsg);
663 else if (cmsg->cmsg_type == SCM_RIGHTS)
664 memcpy(&afd, CMSG_DATA(cmsg), sizeof(int));
666 cmsg = CMSG_NXTHDR(&mh, cmsg);
669 if (cc->dev == NULL && msg.type != MEMIF_MSG_TYPE_INIT) {
670 MIF_LOG(DEBUG, "Unexpected message.");
671 memif_msg_enq_disconnect(cc, "Unexpected message", 0);
675 /* get device from hash data */
677 case MEMIF_MSG_TYPE_ACK:
679 case MEMIF_MSG_TYPE_HELLO:
680 ret = memif_msg_receive_hello(cc->dev, &msg);
683 ret = memif_init_regions_and_queues(cc->dev);
686 ret = memif_msg_enq_init(cc->dev);
689 pmd = cc->dev->data->dev_private;
690 proc_private = cc->dev->process_private;
691 for (i = 0; i < proc_private->regions_num; i++) {
692 ret = memif_msg_enq_add_region(cc->dev, i);
696 for (i = 0; i < pmd->run.num_s2m_rings; i++) {
697 ret = memif_msg_enq_add_ring(cc->dev, i,
702 for (i = 0; i < pmd->run.num_m2s_rings; i++) {
703 ret = memif_msg_enq_add_ring(cc->dev, i,
708 ret = memif_msg_enq_connect(cc->dev);
712 case MEMIF_MSG_TYPE_INIT:
714 * This cc does not have an interface asociated with it.
715 * If suitable interface is found it will be assigned here.
717 ret = memif_msg_receive_init(cc, &msg);
720 ret = memif_msg_enq_ack(cc->dev);
724 case MEMIF_MSG_TYPE_ADD_REGION:
725 ret = memif_msg_receive_add_region(cc->dev, &msg, afd);
728 ret = memif_msg_enq_ack(cc->dev);
732 case MEMIF_MSG_TYPE_ADD_RING:
733 ret = memif_msg_receive_add_ring(cc->dev, &msg, afd);
736 ret = memif_msg_enq_ack(cc->dev);
740 case MEMIF_MSG_TYPE_CONNECT:
741 ret = memif_msg_receive_connect(cc->dev, &msg);
744 ret = memif_msg_enq_connected(cc->dev);
748 case MEMIF_MSG_TYPE_CONNECTED:
749 ret = memif_msg_receive_connected(cc->dev, &msg);
751 case MEMIF_MSG_TYPE_DISCONNECT:
752 ret = memif_msg_receive_disconnect(cc->dev, &msg);
757 memif_msg_enq_disconnect(cc, "Unknown message type", 0);
767 memif_intr_handler(void *arg)
769 struct memif_control_channel *cc = arg;
772 ret = memif_msg_receive(cc);
773 /* if driver failed to assign device */
774 if (cc->dev == NULL) {
775 ret = rte_intr_callback_unregister_pending(&cc->intr_handle,
778 memif_intr_unregister_handler);
781 "Failed to unregister control channel callback.");
784 /* if memif_msg_receive failed */
788 ret = memif_msg_send_from_queue(cc);
795 if (cc->dev == NULL) {
796 MIF_LOG(WARNING, "eth dev not allocated");
799 memif_disconnect(cc->dev);
803 memif_listener_handler(void *arg)
805 struct memif_socket *socket = arg;
808 struct sockaddr_un client;
809 struct memif_control_channel *cc;
812 addr_len = sizeof(client);
813 sockfd = accept(socket->intr_handle.fd, (struct sockaddr *)&client,
814 (socklen_t *)&addr_len);
817 "Failed to accept connection request on socket fd %d",
818 socket->intr_handle.fd);
822 MIF_LOG(DEBUG, "%s: Connection request accepted.", socket->filename);
824 cc = rte_zmalloc("memif-cc", sizeof(struct memif_control_channel), 0);
826 MIF_LOG(ERR, "Failed to allocate control channel.");
830 cc->intr_handle.fd = sockfd;
831 cc->intr_handle.type = RTE_INTR_HANDLE_EXT;
834 TAILQ_INIT(&cc->msg_queue);
836 ret = rte_intr_callback_register(&cc->intr_handle, memif_intr_handler, cc);
838 MIF_LOG(ERR, "Failed to register control channel callback.");
842 ret = memif_msg_enq_hello(cc);
844 MIF_LOG(ERR, "Failed to enqueue hello message.");
847 ret = memif_msg_send_from_queue(cc);
862 static struct memif_socket *
863 memif_socket_create(struct pmd_internals *pmd,
864 const char *key, uint8_t listener)
866 struct memif_socket *sock;
867 struct sockaddr_un un;
872 sock = rte_zmalloc("memif-socket", sizeof(struct memif_socket), 0);
874 MIF_LOG(ERR, "Failed to allocate memory for memif socket");
878 sock->listener = listener;
879 strlcpy(sock->filename, key, MEMIF_SOCKET_UN_SIZE);
880 TAILQ_INIT(&sock->dev_queue);
883 sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
887 un.sun_family = AF_UNIX;
888 strlcpy(un.sun_path, sock->filename, MEMIF_SOCKET_UN_SIZE);
890 ret = setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on,
895 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
899 ret = listen(sockfd, 1);
903 MIF_LOG(DEBUG, "%s: Memif listener socket %s created.",
904 rte_vdev_device_name(pmd->vdev), sock->filename);
906 sock->intr_handle.fd = sockfd;
907 sock->intr_handle.type = RTE_INTR_HANDLE_EXT;
908 ret = rte_intr_callback_register(&sock->intr_handle,
909 memif_listener_handler, sock);
911 MIF_LOG(ERR, "%s: Failed to register interrupt "
912 "callback for listener socket",
913 rte_vdev_device_name(pmd->vdev));
921 MIF_LOG(ERR, "%s: Failed to setup socket %s: %s",
922 rte_vdev_device_name(pmd->vdev) ?
923 rte_vdev_device_name(pmd->vdev) : "NULL", key, strerror(errno));
931 static struct rte_hash *
932 memif_create_socket_hash(void)
934 struct rte_hash_parameters params = { 0 };
936 params.name = MEMIF_SOCKET_HASH_NAME;
937 params.entries = 256;
938 params.key_len = MEMIF_SOCKET_UN_SIZE;
939 params.hash_func = rte_jhash;
940 params.hash_func_init_val = 0;
941 return rte_hash_create(¶ms);
945 memif_socket_init(struct rte_eth_dev *dev, const char *socket_filename)
947 struct pmd_internals *pmd = dev->data->dev_private;
948 struct memif_socket *socket = NULL;
949 struct memif_socket_dev_list_elt *elt;
950 struct pmd_internals *tmp_pmd;
951 struct rte_hash *hash;
953 char key[MEMIF_SOCKET_UN_SIZE];
955 hash = rte_hash_find_existing(MEMIF_SOCKET_HASH_NAME);
957 hash = memif_create_socket_hash();
959 MIF_LOG(ERR, "Failed to create memif socket hash.");
964 memset(key, 0, MEMIF_SOCKET_UN_SIZE);
965 strlcpy(key, socket_filename, MEMIF_SOCKET_UN_SIZE);
966 ret = rte_hash_lookup_data(hash, key, (void **)&socket);
968 socket = memif_socket_create(pmd, key,
970 MEMIF_ROLE_SLAVE) ? 0 : 1);
973 ret = rte_hash_add_key_data(hash, key, socket);
975 MIF_LOG(ERR, "Failed to add socket to socket hash.");
979 pmd->socket_filename = socket->filename;
981 if (socket->listener != 0 && pmd->role == MEMIF_ROLE_SLAVE) {
982 MIF_LOG(ERR, "Socket is a listener.");
984 } else if ((socket->listener == 0) && (pmd->role == MEMIF_ROLE_MASTER)) {
985 MIF_LOG(ERR, "Socket is not a listener.");
989 TAILQ_FOREACH(elt, &socket->dev_queue, next) {
990 tmp_pmd = elt->dev->data->dev_private;
991 if (tmp_pmd->id == pmd->id) {
992 MIF_LOG(ERR, "Memif device with id %d already "
993 "exists on socket %s",
994 pmd->id, socket->filename);
999 elt = rte_malloc("pmd-queue", sizeof(struct memif_socket_dev_list_elt), 0);
1001 MIF_LOG(ERR, "%s: Failed to add device to socket device list.",
1002 rte_vdev_device_name(pmd->vdev));
1006 TAILQ_INSERT_TAIL(&socket->dev_queue, elt, next);
1012 memif_socket_remove_device(struct rte_eth_dev *dev)
1014 struct pmd_internals *pmd = dev->data->dev_private;
1015 struct memif_socket *socket = NULL;
1016 struct memif_socket_dev_list_elt *elt, *next;
1017 struct rte_hash *hash;
1020 hash = rte_hash_find_existing(MEMIF_SOCKET_HASH_NAME);
1024 if (pmd->socket_filename == NULL)
1027 if (rte_hash_lookup_data(hash, pmd->socket_filename, (void **)&socket) < 0)
1030 for (elt = TAILQ_FIRST(&socket->dev_queue); elt != NULL; elt = next) {
1031 next = TAILQ_NEXT(elt, next);
1032 if (elt->dev == dev) {
1033 TAILQ_REMOVE(&socket->dev_queue, elt, next);
1035 pmd->socket_filename = NULL;
1039 /* remove socket, if this was the last device using it */
1040 if (TAILQ_EMPTY(&socket->dev_queue)) {
1041 rte_hash_del_key(hash, socket->filename);
1042 if (socket->listener) {
1043 /* remove listener socket file,
1044 * so we can create new one later.
1046 ret = remove(socket->filename);
1048 MIF_LOG(ERR, "Failed to remove socket file: %s",
1056 memif_connect_master(struct rte_eth_dev *dev)
1058 struct pmd_internals *pmd = dev->data->dev_private;
1060 memset(pmd->local_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
1061 memset(pmd->remote_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
1062 pmd->flags &= ~ETH_MEMIF_FLAG_DISABLED;
1067 memif_connect_slave(struct rte_eth_dev *dev)
1071 struct sockaddr_un sun;
1072 struct pmd_internals *pmd = dev->data->dev_private;
1074 memset(pmd->local_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
1075 memset(pmd->remote_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
1076 pmd->flags &= ~ETH_MEMIF_FLAG_DISABLED;
1078 sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1080 MIF_LOG(ERR, "%s: Failed to open socket.",
1081 rte_vdev_device_name(pmd->vdev));
1085 sun.sun_family = AF_UNIX;
1087 memcpy(sun.sun_path, pmd->socket_filename, sizeof(sun.sun_path) - 1);
1089 ret = connect(sockfd, (struct sockaddr *)&sun,
1090 sizeof(struct sockaddr_un));
1092 MIF_LOG(ERR, "%s: Failed to connect socket: %s.",
1093 rte_vdev_device_name(pmd->vdev), pmd->socket_filename);
1097 MIF_LOG(DEBUG, "%s: Memif socket: %s connected.",
1098 rte_vdev_device_name(pmd->vdev), pmd->socket_filename);
1100 pmd->cc = rte_zmalloc("memif-cc",
1101 sizeof(struct memif_control_channel), 0);
1102 if (pmd->cc == NULL) {
1103 MIF_LOG(ERR, "%s: Failed to allocate control channel.",
1104 rte_vdev_device_name(pmd->vdev));
1108 pmd->cc->intr_handle.fd = sockfd;
1109 pmd->cc->intr_handle.type = RTE_INTR_HANDLE_EXT;
1110 pmd->cc->socket = NULL;
1112 TAILQ_INIT(&pmd->cc->msg_queue);
1114 ret = rte_intr_callback_register(&pmd->cc->intr_handle,
1115 memif_intr_handler, pmd->cc);
1117 MIF_LOG(ERR, "%s: Failed to register interrupt callback "
1118 "for control fd", rte_vdev_device_name(pmd->vdev));
1129 if (pmd->cc != NULL) {