vfio: use generic multi-process channel
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio_mp_sync.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <string.h>
7
8 #include <rte_compat.h>
9 #include <rte_log.h>
10 #include <rte_vfio.h>
11 #include <rte_eal.h>
12
13 #include "eal_vfio.h"
14
15 /**
16  * @file
17  * VFIO socket for communication between primary and secondary processes.
18  *
19  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
20  */
21
22 #ifdef VFIO_PRESENT
23
24 static int
25 vfio_mp_primary(const struct rte_mp_msg *msg, const void *peer)
26 {
27         int fd = -1;
28         int ret;
29         struct rte_mp_msg reply;
30         struct vfio_mp_param *r = (struct vfio_mp_param *)reply.param;
31         const struct vfio_mp_param *m =
32                 (const struct vfio_mp_param *)msg->param;
33
34         if (msg->len_param != sizeof(*m)) {
35                 RTE_LOG(ERR, EAL, "vfio received invalid message!\n");
36                 return -1;
37         }
38
39         memset(&reply, 0, sizeof(reply));
40
41         switch (m->req) {
42         case SOCKET_REQ_GROUP:
43                 r->req = SOCKET_REQ_GROUP;
44                 r->group_num = m->group_num;
45                 fd = rte_vfio_get_group_fd(m->group_num);
46                 if (fd < 0)
47                         r->result = SOCKET_ERR;
48                 else if (fd == 0)
49                         /* if VFIO group exists but isn't bound to VFIO driver */
50                         r->result = SOCKET_NO_FD;
51                 else {
52                         /* if group exists and is bound to VFIO driver */
53                         r->result = SOCKET_OK;
54                         reply.num_fds = 1;
55                         reply.fds[0] = fd;
56                 }
57                 break;
58         case SOCKET_CLR_GROUP:
59                 r->req = SOCKET_CLR_GROUP;
60                 r->group_num = m->group_num;
61                 if (rte_vfio_clear_group(m->group_num) < 0)
62                         r->result = SOCKET_NO_FD;
63                 else
64                         r->result = SOCKET_OK;
65                 break;
66         case SOCKET_REQ_CONTAINER:
67                 r->req = SOCKET_REQ_CONTAINER;
68                 fd = rte_vfio_get_container_fd();
69                 if (fd < 0)
70                         r->result = SOCKET_ERR;
71                 else {
72                         r->result = SOCKET_OK;
73                         reply.num_fds = 1;
74                         reply.fds[0] = fd;
75                 }
76                 break;
77         default:
78                 RTE_LOG(ERR, EAL, "vfio received invalid message!\n");
79                 return -1;
80         }
81
82         strcpy(reply.name, EAL_VFIO_MP);
83         reply.len_param = sizeof(*r);
84
85         ret = rte_mp_reply(&reply, peer);
86         if (m->req == SOCKET_REQ_CONTAINER && fd >= 0)
87                 close(fd);
88         return ret;
89 }
90
91 int
92 vfio_mp_sync_setup(void)
93 {
94         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
95                 return rte_mp_action_register(EAL_VFIO_MP, vfio_mp_primary);
96
97         return 0;
98 }
99
100 #endif