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, "Connecting to %s.", pmd->remote_name);
184 memif_msg_receive_init(struct memif_control_channel *cc, memif_msg_t *msg)
186 memif_msg_init_t *i = &msg->init;
187 struct memif_socket_dev_list_elt *elt;
188 struct pmd_internals *pmd;
189 struct rte_eth_dev *dev;
191 if (i->version != MEMIF_VERSION) {
192 memif_msg_enq_disconnect(cc, "Incompatible memif version", 0);
196 if (cc->socket == NULL) {
197 memif_msg_enq_disconnect(cc, "Device error", 0);
201 /* Find device with requested ID */
202 TAILQ_FOREACH(elt, &cc->socket->dev_queue, next) {
204 pmd = dev->data->dev_private;
205 if (((pmd->flags & ETH_MEMIF_FLAG_DISABLED) == 0) &&
206 (pmd->id == i->id) && (pmd->role == MEMIF_ROLE_MASTER)) {
207 if (pmd->flags & (ETH_MEMIF_FLAG_CONNECTING |
208 ETH_MEMIF_FLAG_CONNECTED)) {
209 memif_msg_enq_disconnect(cc,
210 "Already connected", 0);
214 /* assign control channel to device */
218 if (i->mode != MEMIF_INTERFACE_MODE_ETHERNET) {
219 memif_msg_enq_disconnect(pmd->cc,
220 "Only ethernet mode supported",
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, "Remote interface %s connected.", pmd->remote_if_name);
347 memif_msg_receive_connected(struct rte_eth_dev *dev, memif_msg_t *msg)
349 struct pmd_internals *pmd = dev->data->dev_private;
350 memif_msg_connected_t *c = &msg->connected;
353 ret = memif_connect(dev);
357 strlcpy(pmd->remote_if_name, (char *)c->if_name,
358 sizeof(pmd->remote_if_name));
359 MIF_LOG(INFO, "Remote interface %s connected.", pmd->remote_if_name);
365 memif_msg_receive_disconnect(struct rte_eth_dev *dev, memif_msg_t *msg)
367 struct pmd_internals *pmd = dev->data->dev_private;
368 memif_msg_disconnect_t *d = &msg->disconnect;
370 memset(pmd->remote_disc_string, 0, sizeof(pmd->remote_disc_string));
371 strlcpy(pmd->remote_disc_string, (char *)d->string,
372 sizeof(pmd->remote_disc_string));
374 MIF_LOG(INFO, "Disconnect received: %s", pmd->remote_disc_string);
376 memset(pmd->local_disc_string, 0, 96);
377 memif_disconnect(dev);
382 memif_msg_enq_ack(struct rte_eth_dev *dev)
384 struct pmd_internals *pmd = dev->data->dev_private;
385 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
389 e->msg.type = MEMIF_MSG_TYPE_ACK;
395 memif_msg_enq_init(struct rte_eth_dev *dev)
397 struct pmd_internals *pmd = dev->data->dev_private;
398 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
399 memif_msg_init_t *i = &e->msg.init;
405 e->msg.type = MEMIF_MSG_TYPE_INIT;
406 i->version = MEMIF_VERSION;
408 i->mode = MEMIF_INTERFACE_MODE_ETHERNET;
410 strlcpy((char *)i->name, rte_version(), sizeof(i->name));
412 if (*pmd->secret != '\0')
413 strlcpy((char *)i->secret, pmd->secret, sizeof(i->secret));
419 memif_msg_enq_add_region(struct rte_eth_dev *dev, uint8_t idx)
421 struct pmd_internals *pmd = dev->data->dev_private;
422 struct pmd_process_private *proc_private = dev->process_private;
423 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
424 memif_msg_add_region_t *ar;
425 struct memif_region *mr = proc_private->regions[idx];
430 ar = &e->msg.add_region;
431 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
434 ar->size = mr->region_size;
440 memif_msg_enq_add_ring(struct rte_eth_dev *dev, uint8_t idx,
441 memif_ring_type_t type)
443 struct pmd_internals *pmd = dev->data->dev_private;
444 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
445 struct memif_queue *mq;
446 memif_msg_add_ring_t *ar;
451 ar = &e->msg.add_ring;
452 mq = (type == MEMIF_RING_S2M) ? dev->data->tx_queues[idx] :
453 dev->data->rx_queues[idx];
455 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
456 e->fd = mq->intr_handle.fd;
458 ar->offset = mq->ring_offset;
459 ar->region = mq->region;
460 ar->log2_ring_size = mq->log2_ring_size;
461 ar->flags = (type == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
462 ar->private_hdr_size = 0;
468 memif_msg_enq_connect(struct rte_eth_dev *dev)
470 struct pmd_internals *pmd = dev->data->dev_private;
471 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
472 memif_msg_connect_t *c;
478 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
479 strlcpy((char *)c->if_name, dev->data->name, sizeof(c->if_name));
485 memif_msg_enq_connected(struct rte_eth_dev *dev)
487 struct pmd_internals *pmd = dev->data->dev_private;
488 struct memif_msg_queue_elt *e = memif_msg_enq(pmd->cc);
489 memif_msg_connected_t *c;
494 c = &e->msg.connected;
495 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
496 strlcpy((char *)c->if_name, dev->data->name, sizeof(c->if_name));
502 memif_intr_unregister_handler(struct rte_intr_handle *intr_handle, void *arg)
504 struct memif_msg_queue_elt *elt;
505 struct memif_control_channel *cc = arg;
507 /* close control channel fd */
508 close(intr_handle->fd);
509 /* clear message queue */
510 while ((elt = TAILQ_FIRST(&cc->msg_queue)) != NULL) {
511 TAILQ_REMOVE(&cc->msg_queue, elt, next);
514 /* free control channel */
519 memif_disconnect(struct rte_eth_dev *dev)
521 struct pmd_internals *pmd = dev->data->dev_private;
522 struct memif_msg_queue_elt *elt, *next;
523 struct memif_queue *mq;
524 struct rte_intr_handle *ih;
528 dev->data->dev_link.link_status = ETH_LINK_DOWN;
529 pmd->flags &= ~ETH_MEMIF_FLAG_CONNECTING;
530 pmd->flags &= ~ETH_MEMIF_FLAG_CONNECTED;
532 rte_spinlock_lock(&pmd->cc_lock);
533 if (pmd->cc != NULL) {
534 /* Clear control message queue (except disconnect message if any). */
535 for (elt = TAILQ_FIRST(&pmd->cc->msg_queue); elt != NULL; elt = next) {
536 next = TAILQ_NEXT(elt, next);
537 if (elt->msg.type != MEMIF_MSG_TYPE_DISCONNECT) {
538 TAILQ_REMOVE(&pmd->cc->msg_queue, elt, next);
542 /* send disconnect message (if there is any in queue) */
543 memif_msg_send_from_queue(pmd->cc);
545 /* at this point, there should be no more messages in queue */
546 if (TAILQ_FIRST(&pmd->cc->msg_queue) != NULL) {
548 "Unexpected message(s) in message queue.");
551 ih = &pmd->cc->intr_handle;
553 ret = rte_intr_callback_unregister(ih,
557 * If callback is active (disconnecting based on
558 * received control message).
560 if (ret == -EAGAIN) {
561 ret = rte_intr_callback_unregister_pending(ih,
564 memif_intr_unregister_handler);
565 } else if (ret > 0) {
572 "Failed to unregister control channel callback.");
575 rte_spinlock_unlock(&pmd->cc_lock);
577 /* unconfig interrupts */
578 for (i = 0; i < pmd->cfg.num_s2m_rings; i++) {
579 if (pmd->role == MEMIF_ROLE_SLAVE) {
580 if (dev->data->tx_queues != NULL)
581 mq = dev->data->tx_queues[i];
585 if (dev->data->rx_queues != NULL)
586 mq = dev->data->rx_queues[i];
590 if (mq->intr_handle.fd > 0) {
591 close(mq->intr_handle.fd);
592 mq->intr_handle.fd = -1;
595 for (i = 0; i < pmd->cfg.num_m2s_rings; i++) {
596 if (pmd->role == MEMIF_ROLE_MASTER) {
597 if (dev->data->tx_queues != NULL)
598 mq = dev->data->tx_queues[i];
602 if (dev->data->rx_queues != NULL)
603 mq = dev->data->rx_queues[i];
607 if (mq->intr_handle.fd > 0) {
608 close(mq->intr_handle.fd);
609 mq->intr_handle.fd = -1;
613 memif_free_regions(dev);
615 /* reset connection configuration */
616 memset(&pmd->run, 0, sizeof(pmd->run));
618 MIF_LOG(DEBUG, "Disconnected, id: %d, role: %s.", pmd->id,
619 (pmd->role == MEMIF_ROLE_MASTER) ? "master" : "slave");
623 memif_msg_receive(struct memif_control_channel *cc)
625 char ctl[CMSG_SPACE(sizeof(int)) +
626 CMSG_SPACE(sizeof(struct ucred))] = { 0 };
627 struct msghdr mh = { 0 };
629 memif_msg_t msg = { 0 };
632 struct ucred *cr __rte_unused;
634 struct cmsghdr *cmsg;
637 struct pmd_internals *pmd;
638 struct pmd_process_private *proc_private;
640 iov[0].iov_base = (void *)&msg;
641 iov[0].iov_len = sizeof(memif_msg_t);
644 mh.msg_control = ctl;
645 mh.msg_controllen = sizeof(ctl);
647 size = recvmsg(cc->intr_handle.fd, &mh, 0);
648 if (size != sizeof(memif_msg_t)) {
649 MIF_LOG(DEBUG, "Invalid message size = %zd", size);
651 /* 0 means end-of-file, negative size means error,
652 * don't send further disconnect message in such cases.
654 memif_msg_enq_disconnect(cc, "Invalid message size", 0);
657 MIF_LOG(DEBUG, "Received msg type: %u.", msg.type);
659 cmsg = CMSG_FIRSTHDR(&mh);
661 if (cmsg->cmsg_level == SOL_SOCKET) {
662 if (cmsg->cmsg_type == SCM_CREDENTIALS)
663 cr = (struct ucred *)CMSG_DATA(cmsg);
664 else if (cmsg->cmsg_type == SCM_RIGHTS)
665 rte_memcpy(&afd, CMSG_DATA(cmsg), sizeof(int));
667 cmsg = CMSG_NXTHDR(&mh, cmsg);
670 if (cc->dev == NULL && msg.type != MEMIF_MSG_TYPE_INIT) {
671 MIF_LOG(DEBUG, "Unexpected message.");
672 memif_msg_enq_disconnect(cc, "Unexpected message", 0);
676 /* get device from hash data */
678 case MEMIF_MSG_TYPE_ACK:
680 case MEMIF_MSG_TYPE_HELLO:
681 ret = memif_msg_receive_hello(cc->dev, &msg);
684 ret = memif_init_regions_and_queues(cc->dev);
687 ret = memif_msg_enq_init(cc->dev);
690 pmd = cc->dev->data->dev_private;
691 proc_private = cc->dev->process_private;
692 for (i = 0; i < proc_private->regions_num; i++) {
693 ret = memif_msg_enq_add_region(cc->dev, i);
697 for (i = 0; i < pmd->run.num_s2m_rings; i++) {
698 ret = memif_msg_enq_add_ring(cc->dev, i,
703 for (i = 0; i < pmd->run.num_m2s_rings; i++) {
704 ret = memif_msg_enq_add_ring(cc->dev, i,
709 ret = memif_msg_enq_connect(cc->dev);
713 case MEMIF_MSG_TYPE_INIT:
715 * This cc does not have an interface asociated with it.
716 * If suitable interface is found it will be assigned here.
718 ret = memif_msg_receive_init(cc, &msg);
721 ret = memif_msg_enq_ack(cc->dev);
725 case MEMIF_MSG_TYPE_ADD_REGION:
726 ret = memif_msg_receive_add_region(cc->dev, &msg, afd);
729 ret = memif_msg_enq_ack(cc->dev);
733 case MEMIF_MSG_TYPE_ADD_RING:
734 ret = memif_msg_receive_add_ring(cc->dev, &msg, afd);
737 ret = memif_msg_enq_ack(cc->dev);
741 case MEMIF_MSG_TYPE_CONNECT:
742 ret = memif_msg_receive_connect(cc->dev, &msg);
745 ret = memif_msg_enq_connected(cc->dev);
749 case MEMIF_MSG_TYPE_CONNECTED:
750 ret = memif_msg_receive_connected(cc->dev, &msg);
752 case MEMIF_MSG_TYPE_DISCONNECT:
753 ret = memif_msg_receive_disconnect(cc->dev, &msg);
758 memif_msg_enq_disconnect(cc, "Unknown message type", 0);
768 memif_intr_handler(void *arg)
770 struct memif_control_channel *cc = arg;
773 ret = memif_msg_receive(cc);
774 /* if driver failed to assign device */
775 if (cc->dev == NULL) {
776 memif_msg_send_from_queue(cc);
777 ret = rte_intr_callback_unregister_pending(&cc->intr_handle,
780 memif_intr_unregister_handler);
783 "Failed to unregister control channel callback.");
786 /* if memif_msg_receive failed */
790 ret = memif_msg_send_from_queue(cc);
797 if (cc->dev == NULL) {
798 MIF_LOG(WARNING, "eth dev not allocated");
801 memif_disconnect(cc->dev);
805 memif_listener_handler(void *arg)
807 struct memif_socket *socket = arg;
810 struct sockaddr_un client;
811 struct memif_control_channel *cc;
814 addr_len = sizeof(client);
815 sockfd = accept(socket->intr_handle.fd, (struct sockaddr *)&client,
816 (socklen_t *)&addr_len);
819 "Failed to accept connection request on socket fd %d",
820 socket->intr_handle.fd);
824 MIF_LOG(DEBUG, "%s: Connection request accepted.", socket->filename);
826 cc = rte_zmalloc("memif-cc", sizeof(struct memif_control_channel), 0);
828 MIF_LOG(ERR, "Failed to allocate control channel.");
832 cc->intr_handle.fd = sockfd;
833 cc->intr_handle.type = RTE_INTR_HANDLE_EXT;
836 TAILQ_INIT(&cc->msg_queue);
838 ret = rte_intr_callback_register(&cc->intr_handle, memif_intr_handler, cc);
840 MIF_LOG(ERR, "Failed to register control channel callback.");
844 ret = memif_msg_enq_hello(cc);
846 MIF_LOG(ERR, "Failed to enqueue hello message.");
849 ret = memif_msg_send_from_queue(cc);
864 static struct memif_socket *
865 memif_socket_create(char *key, uint8_t listener)
867 struct memif_socket *sock;
868 struct sockaddr_un un;
873 sock = rte_zmalloc("memif-socket", sizeof(struct memif_socket), 0);
875 MIF_LOG(ERR, "Failed to allocate memory for memif socket");
879 sock->listener = listener;
880 strlcpy(sock->filename, key, MEMIF_SOCKET_UN_SIZE);
881 TAILQ_INIT(&sock->dev_queue);
884 sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
888 un.sun_family = AF_UNIX;
889 strlcpy(un.sun_path, sock->filename, MEMIF_SOCKET_UN_SIZE);
891 ret = setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on,
896 ret = bind(sockfd, (struct sockaddr *)&un, sizeof(un));
900 ret = listen(sockfd, 1);
904 MIF_LOG(DEBUG, "Memif listener socket %s created.", 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, "Failed to register interrupt "
912 "callback for listener socket");
920 MIF_LOG(ERR, "Failed to setup socket %s: %s", key, strerror(errno));
928 static struct rte_hash *
929 memif_create_socket_hash(void)
931 struct rte_hash_parameters params = { 0 };
933 params.name = MEMIF_SOCKET_HASH_NAME;
934 params.entries = 256;
935 params.key_len = MEMIF_SOCKET_UN_SIZE;
936 params.hash_func = rte_jhash;
937 params.hash_func_init_val = 0;
938 return rte_hash_create(¶ms);
942 memif_socket_init(struct rte_eth_dev *dev, const char *socket_filename)
944 struct pmd_internals *pmd = dev->data->dev_private;
945 struct memif_socket *socket = NULL;
946 struct memif_socket_dev_list_elt *elt;
947 struct pmd_internals *tmp_pmd;
948 struct rte_hash *hash;
950 char key[MEMIF_SOCKET_UN_SIZE];
952 hash = rte_hash_find_existing(MEMIF_SOCKET_HASH_NAME);
954 hash = memif_create_socket_hash();
956 MIF_LOG(ERR, "Failed to create memif socket hash.");
961 memset(key, 0, MEMIF_SOCKET_UN_SIZE);
962 strlcpy(key, socket_filename, MEMIF_SOCKET_UN_SIZE);
963 ret = rte_hash_lookup_data(hash, key, (void **)&socket);
965 socket = memif_socket_create(key,
966 (pmd->role == MEMIF_ROLE_SLAVE) ? 0 : 1);
969 ret = rte_hash_add_key_data(hash, key, socket);
971 MIF_LOG(ERR, "Failed to add socket to socket hash.");
975 pmd->socket_filename = socket->filename;
977 TAILQ_FOREACH(elt, &socket->dev_queue, next) {
978 tmp_pmd = elt->dev->data->dev_private;
979 if (tmp_pmd->id == pmd->id && tmp_pmd->role == pmd->role) {
980 MIF_LOG(ERR, "Two interfaces with the same id (%d) can "
981 "not have the same role.", pmd->id);
986 elt = rte_malloc("pmd-queue", sizeof(struct memif_socket_dev_list_elt), 0);
988 MIF_LOG(ERR, "Failed to add device to socket device list.");
992 TAILQ_INSERT_TAIL(&socket->dev_queue, elt, next);
998 memif_socket_remove_device(struct rte_eth_dev *dev)
1000 struct pmd_internals *pmd = dev->data->dev_private;
1001 struct memif_socket *socket = NULL;
1002 struct memif_socket_dev_list_elt *elt, *next;
1003 struct rte_hash *hash;
1006 hash = rte_hash_find_existing(MEMIF_SOCKET_HASH_NAME);
1010 if (pmd->socket_filename == NULL)
1013 if (rte_hash_lookup_data(hash, pmd->socket_filename, (void **)&socket) < 0)
1016 for (elt = TAILQ_FIRST(&socket->dev_queue); elt != NULL; elt = next) {
1017 next = TAILQ_NEXT(elt, next);
1018 if (elt->dev == dev) {
1019 TAILQ_REMOVE(&socket->dev_queue, elt, next);
1021 pmd->socket_filename = NULL;
1025 /* remove socket, if this was the last device using it */
1026 if (TAILQ_EMPTY(&socket->dev_queue)) {
1027 rte_hash_del_key(hash, socket->filename);
1028 if (socket->listener) {
1029 /* remove listener socket file,
1030 * so we can create new one later.
1032 ret = remove(socket->filename);
1034 MIF_LOG(ERR, "Failed to remove socket file: %s",
1042 memif_connect_master(struct rte_eth_dev *dev)
1044 struct pmd_internals *pmd = dev->data->dev_private;
1046 memset(pmd->local_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
1047 memset(pmd->remote_disc_string, 0, ETH_MEMIF_DISC_STRING_SIZE);
1048 pmd->flags &= ~ETH_MEMIF_FLAG_DISABLED;
1053 memif_connect_slave(struct rte_eth_dev *dev)
1057 struct sockaddr_un sun;
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;
1064 sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1066 MIF_LOG(ERR, "Failed to open socket.");
1070 sun.sun_family = AF_UNIX;
1072 memcpy(sun.sun_path, pmd->socket_filename, sizeof(sun.sun_path) - 1);
1074 ret = connect(sockfd, (struct sockaddr *)&sun,
1075 sizeof(struct sockaddr_un));
1077 MIF_LOG(ERR, "Failed to connect socket: %s.", pmd->socket_filename);
1081 MIF_LOG(DEBUG, "Memif socket: %s connected.", pmd->socket_filename);
1083 pmd->cc = rte_zmalloc("memif-cc",
1084 sizeof(struct memif_control_channel), 0);
1085 if (pmd->cc == NULL) {
1086 MIF_LOG(ERR, "Failed to allocate control channel.");
1090 pmd->cc->intr_handle.fd = sockfd;
1091 pmd->cc->intr_handle.type = RTE_INTR_HANDLE_EXT;
1092 pmd->cc->socket = NULL;
1094 TAILQ_INIT(&pmd->cc->msg_queue);
1096 ret = rte_intr_callback_register(&pmd->cc->intr_handle,
1097 memif_intr_handler, pmd->cc);
1099 MIF_LOG(ERR, "Failed to register interrupt callback for control fd");
1110 if (pmd->cc != NULL) {