f60cb1366354ea088d64334fb4a91bf9d83681de
[dpdk.git] / lib / librte_vhost / vhost_user / vhost-net-user.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdbool.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sys/queue.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <pthread.h>
48
49 #include <rte_log.h>
50 #include <rte_virtio_net.h>
51
52 #include "fd_man.h"
53 #include "vhost-net-user.h"
54 #include "vhost-net.h"
55 #include "virtio-net-user.h"
56
57 /*
58  * Every time rte_vhost_driver_register() is invoked, an associated
59  * vhost_user_socket struct will be created.
60  */
61 struct vhost_user_socket {
62         char *path;
63         int listenfd;
64         bool is_server;
65         bool reconnect;
66 };
67
68 struct vhost_user_connection {
69         struct vhost_user_socket *vsocket;
70         int vid;
71 };
72
73 #define MAX_VHOST_SOCKET 1024
74 struct vhost_user {
75         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
76         struct fdset fdset;
77         int vsocket_cnt;
78         pthread_mutex_t mutex;
79 };
80
81 #define MAX_VIRTIO_BACKLOG 128
82
83 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
84 static void vhost_user_msg_handler(int fd, void *dat, int *remove);
85 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
86
87 static struct vhost_user vhost_user = {
88         .fdset = {
89                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
91                 .num = 0
92         },
93         .vsocket_cnt = 0,
94         .mutex = PTHREAD_MUTEX_INITIALIZER,
95 };
96
97 static const char *vhost_message_str[VHOST_USER_MAX] = {
98         [VHOST_USER_NONE] = "VHOST_USER_NONE",
99         [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
100         [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
101         [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
102         [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
103         [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
104         [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
105         [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
106         [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
107         [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
108         [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
109         [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
110         [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
111         [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
112         [VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
113         [VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
114         [VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
115         [VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
116         [VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
117         [VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
118 };
119
120 /* return bytes# of read on success or negative val on failure. */
121 static int
122 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
123 {
124         struct iovec iov;
125         struct msghdr msgh;
126         size_t fdsize = fd_num * sizeof(int);
127         char control[CMSG_SPACE(fdsize)];
128         struct cmsghdr *cmsg;
129         int ret;
130
131         memset(&msgh, 0, sizeof(msgh));
132         iov.iov_base = buf;
133         iov.iov_len  = buflen;
134
135         msgh.msg_iov = &iov;
136         msgh.msg_iovlen = 1;
137         msgh.msg_control = control;
138         msgh.msg_controllen = sizeof(control);
139
140         ret = recvmsg(sockfd, &msgh, 0);
141         if (ret <= 0) {
142                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
143                 return ret;
144         }
145
146         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
147                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
148                 return -1;
149         }
150
151         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
152                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
153                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
154                         (cmsg->cmsg_type == SCM_RIGHTS)) {
155                         memcpy(fds, CMSG_DATA(cmsg), fdsize);
156                         break;
157                 }
158         }
159
160         return ret;
161 }
162
163 /* return bytes# of read on success or negative val on failure. */
164 static int
165 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
166 {
167         int ret;
168
169         ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
170                 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
171         if (ret <= 0)
172                 return ret;
173
174         if (msg && msg->size) {
175                 if (msg->size > sizeof(msg->payload)) {
176                         RTE_LOG(ERR, VHOST_CONFIG,
177                                 "invalid msg size: %d\n", msg->size);
178                         return -1;
179                 }
180                 ret = read(sockfd, &msg->payload, msg->size);
181                 if (ret <= 0)
182                         return ret;
183                 if (ret != (int)msg->size) {
184                         RTE_LOG(ERR, VHOST_CONFIG,
185                                 "read control message failed\n");
186                         return -1;
187                 }
188         }
189
190         return ret;
191 }
192
193 static int
194 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
195 {
196
197         struct iovec iov;
198         struct msghdr msgh;
199         size_t fdsize = fd_num * sizeof(int);
200         char control[CMSG_SPACE(fdsize)];
201         struct cmsghdr *cmsg;
202         int ret;
203
204         memset(&msgh, 0, sizeof(msgh));
205         iov.iov_base = buf;
206         iov.iov_len = buflen;
207
208         msgh.msg_iov = &iov;
209         msgh.msg_iovlen = 1;
210
211         if (fds && fd_num > 0) {
212                 msgh.msg_control = control;
213                 msgh.msg_controllen = sizeof(control);
214                 cmsg = CMSG_FIRSTHDR(&msgh);
215                 cmsg->cmsg_len = CMSG_LEN(fdsize);
216                 cmsg->cmsg_level = SOL_SOCKET;
217                 cmsg->cmsg_type = SCM_RIGHTS;
218                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
219         } else {
220                 msgh.msg_control = NULL;
221                 msgh.msg_controllen = 0;
222         }
223
224         do {
225                 ret = sendmsg(sockfd, &msgh, 0);
226         } while (ret < 0 && errno == EINTR);
227
228         if (ret < 0) {
229                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
230                 return ret;
231         }
232
233         return ret;
234 }
235
236 static int
237 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
238 {
239         int ret;
240
241         if (!msg)
242                 return 0;
243
244         msg->flags &= ~VHOST_USER_VERSION_MASK;
245         msg->flags |= VHOST_USER_VERSION;
246         msg->flags |= VHOST_USER_REPLY_MASK;
247
248         ret = send_fd_message(sockfd, (char *)msg,
249                 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
250
251         return ret;
252 }
253
254
255 static void
256 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
257 {
258         int vid;
259         size_t size;
260         struct vhost_user_connection *conn;
261         int ret;
262
263         conn = malloc(sizeof(*conn));
264         if (conn == NULL) {
265                 close(fd);
266                 return;
267         }
268
269         vid = vhost_new_device();
270         if (vid == -1) {
271                 close(fd);
272                 free(conn);
273                 return;
274         }
275
276         size = strnlen(vsocket->path, PATH_MAX);
277         vhost_set_ifname(vid, vsocket->path, size);
278
279         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
280
281         conn->vsocket = vsocket;
282         conn->vid = vid;
283         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_msg_handler,
284                         NULL, conn);
285         if (ret < 0) {
286                 free(conn);
287                 close(fd);
288                 RTE_LOG(ERR, VHOST_CONFIG,
289                         "failed to add fd %d into vhost server fdset\n",
290                         fd);
291         }
292 }
293
294 /* call back when there is new vhost-user connection from client  */
295 static void
296 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
297 {
298         struct vhost_user_socket *vsocket = dat;
299
300         fd = accept(fd, NULL, NULL);
301         if (fd < 0)
302                 return;
303
304         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
305         vhost_user_add_connection(fd, vsocket);
306 }
307
308 /* callback when there is message on the connfd */
309 static void
310 vhost_user_msg_handler(int connfd, void *dat, int *remove)
311 {
312         int vid;
313         struct vhost_user_connection *conn = dat;
314         struct VhostUserMsg msg;
315         uint64_t features;
316         int ret;
317
318         vid = conn->vid;
319         ret = read_vhost_message(connfd, &msg);
320         if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
321                 struct vhost_user_socket *vsocket = conn->vsocket;
322
323                 if (ret < 0)
324                         RTE_LOG(ERR, VHOST_CONFIG,
325                                 "vhost read message failed\n");
326                 else if (ret == 0)
327                         RTE_LOG(INFO, VHOST_CONFIG,
328                                 "vhost peer closed\n");
329                 else
330                         RTE_LOG(ERR, VHOST_CONFIG,
331                                 "vhost read incorrect message\n");
332
333                 close(connfd);
334                 *remove = 1;
335                 free(conn);
336                 vhost_destroy_device(vid);
337
338                 if (vsocket->reconnect)
339                         vhost_user_create_client(vsocket);
340
341                 return;
342         }
343
344         RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
345                 vhost_message_str[msg.request]);
346         switch (msg.request) {
347         case VHOST_USER_GET_FEATURES:
348                 ret = vhost_get_features(vid, &features);
349                 msg.payload.u64 = features;
350                 msg.size = sizeof(msg.payload.u64);
351                 send_vhost_message(connfd, &msg);
352                 break;
353         case VHOST_USER_SET_FEATURES:
354                 features = msg.payload.u64;
355                 vhost_set_features(vid, &features);
356                 break;
357
358         case VHOST_USER_GET_PROTOCOL_FEATURES:
359                 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
360                 msg.size = sizeof(msg.payload.u64);
361                 send_vhost_message(connfd, &msg);
362                 break;
363         case VHOST_USER_SET_PROTOCOL_FEATURES:
364                 user_set_protocol_features(vid, msg.payload.u64);
365                 break;
366
367         case VHOST_USER_SET_OWNER:
368                 vhost_set_owner(vid);
369                 break;
370         case VHOST_USER_RESET_OWNER:
371                 vhost_reset_owner(vid);
372                 break;
373
374         case VHOST_USER_SET_MEM_TABLE:
375                 user_set_mem_table(vid, &msg);
376                 break;
377
378         case VHOST_USER_SET_LOG_BASE:
379                 user_set_log_base(vid, &msg);
380
381                 /* it needs a reply */
382                 msg.size = sizeof(msg.payload.u64);
383                 send_vhost_message(connfd, &msg);
384                 break;
385         case VHOST_USER_SET_LOG_FD:
386                 close(msg.fds[0]);
387                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
388                 break;
389
390         case VHOST_USER_SET_VRING_NUM:
391                 vhost_set_vring_num(vid, &msg.payload.state);
392                 break;
393         case VHOST_USER_SET_VRING_ADDR:
394                 vhost_set_vring_addr(vid, &msg.payload.addr);
395                 break;
396         case VHOST_USER_SET_VRING_BASE:
397                 vhost_set_vring_base(vid, &msg.payload.state);
398                 break;
399
400         case VHOST_USER_GET_VRING_BASE:
401                 ret = user_get_vring_base(vid, &msg.payload.state);
402                 msg.size = sizeof(msg.payload.state);
403                 send_vhost_message(connfd, &msg);
404                 break;
405
406         case VHOST_USER_SET_VRING_KICK:
407                 user_set_vring_kick(vid, &msg);
408                 break;
409         case VHOST_USER_SET_VRING_CALL:
410                 user_set_vring_call(vid, &msg);
411                 break;
412
413         case VHOST_USER_SET_VRING_ERR:
414                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
415                         close(msg.fds[0]);
416                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
417                 break;
418
419         case VHOST_USER_GET_QUEUE_NUM:
420                 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
421                 msg.size = sizeof(msg.payload.u64);
422                 send_vhost_message(connfd, &msg);
423                 break;
424
425         case VHOST_USER_SET_VRING_ENABLE:
426                 user_set_vring_enable(vid, &msg.payload.state);
427                 break;
428         case VHOST_USER_SEND_RARP:
429                 user_send_rarp(vid, &msg);
430                 break;
431
432         default:
433                 break;
434
435         }
436 }
437
438 static int
439 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
440 {
441         int fd;
442
443         fd = socket(AF_UNIX, SOCK_STREAM, 0);
444         if (fd < 0)
445                 return -1;
446         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
447                 is_server ? "server" : "client", fd);
448
449         if (!is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
450                 RTE_LOG(ERR, VHOST_CONFIG,
451                         "vhost-user: can't set nonblocking mode for socket, fd: "
452                         "%d (%s)\n", fd, strerror(errno));
453                 close(fd);
454                 return -1;
455         }
456
457         memset(un, 0, sizeof(*un));
458         un->sun_family = AF_UNIX;
459         strncpy(un->sun_path, path, sizeof(un->sun_path));
460         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
461
462         return fd;
463 }
464
465 static int
466 vhost_user_create_server(struct vhost_user_socket *vsocket)
467 {
468         int fd;
469         int ret;
470         struct sockaddr_un un;
471         const char *path = vsocket->path;
472
473         fd = create_unix_socket(path, &un, vsocket->is_server);
474         if (fd < 0)
475                 return -1;
476
477         ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
478         if (ret < 0) {
479                 RTE_LOG(ERR, VHOST_CONFIG,
480                         "failed to bind to %s: %s; remove it and try again\n",
481                         path, strerror(errno));
482                 goto err;
483         }
484         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
485
486         ret = listen(fd, MAX_VIRTIO_BACKLOG);
487         if (ret < 0)
488                 goto err;
489
490         vsocket->listenfd = fd;
491         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
492                   NULL, vsocket);
493         if (ret < 0) {
494                 RTE_LOG(ERR, VHOST_CONFIG,
495                         "failed to add listen fd %d to vhost server fdset\n",
496                         fd);
497                 goto err;
498         }
499
500         return 0;
501
502 err:
503         close(fd);
504         return -1;
505 }
506
507 struct vhost_user_reconnect {
508         struct sockaddr_un un;
509         int fd;
510         struct vhost_user_socket *vsocket;
511
512         TAILQ_ENTRY(vhost_user_reconnect) next;
513 };
514
515 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
516 struct vhost_user_reconnect_list {
517         struct vhost_user_reconnect_tailq_list head;
518         pthread_mutex_t mutex;
519 };
520
521 static struct vhost_user_reconnect_list reconn_list;
522 static pthread_t reconn_tid;
523
524 static int
525 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
526 {
527         int ret, flags;
528
529         ret = connect(fd, un, sz);
530         if (ret < 0 && errno != EISCONN)
531                 return -1;
532
533         flags = fcntl(fd, F_GETFL, 0);
534         if (flags < 0) {
535                 RTE_LOG(ERR, VHOST_CONFIG,
536                         "can't get flags for connfd %d\n", fd);
537                 return -2;
538         }
539         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
540                 RTE_LOG(ERR, VHOST_CONFIG,
541                                 "can't disable nonblocking on fd %d\n", fd);
542                 return -2;
543         }
544         return 0;
545 }
546
547 static void *
548 vhost_user_client_reconnect(void *arg __rte_unused)
549 {
550         int ret;
551         struct vhost_user_reconnect *reconn, *next;
552
553         while (1) {
554                 pthread_mutex_lock(&reconn_list.mutex);
555
556                 /*
557                  * An equal implementation of TAILQ_FOREACH_SAFE,
558                  * which does not exist on all platforms.
559                  */
560                 for (reconn = TAILQ_FIRST(&reconn_list.head);
561                      reconn != NULL; reconn = next) {
562                         next = TAILQ_NEXT(reconn, next);
563
564                         ret = vhost_user_connect_nonblock(reconn->fd,
565                                                 (struct sockaddr *)&reconn->un,
566                                                 sizeof(reconn->un));
567                         if (ret == -2) {
568                                 close(reconn->fd);
569                                 RTE_LOG(ERR, VHOST_CONFIG,
570                                         "reconnection for fd %d failed\n",
571                                         reconn->fd);
572                                 goto remove_fd;
573                         }
574                         if (ret == -1)
575                                 continue;
576
577                         RTE_LOG(INFO, VHOST_CONFIG,
578                                 "%s: connected\n", reconn->vsocket->path);
579                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
580 remove_fd:
581                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
582                         free(reconn);
583                 }
584
585                 pthread_mutex_unlock(&reconn_list.mutex);
586                 sleep(1);
587         }
588
589         return NULL;
590 }
591
592 static int
593 vhost_user_reconnect_init(void)
594 {
595         int ret;
596
597         pthread_mutex_init(&reconn_list.mutex, NULL);
598         TAILQ_INIT(&reconn_list.head);
599
600         ret = pthread_create(&reconn_tid, NULL,
601                              vhost_user_client_reconnect, NULL);
602         if (ret < 0)
603                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
604
605         return ret;
606 }
607
608 static int
609 vhost_user_create_client(struct vhost_user_socket *vsocket)
610 {
611         int fd;
612         int ret;
613         struct sockaddr_un un;
614         const char *path = vsocket->path;
615         struct vhost_user_reconnect *reconn;
616
617         fd = create_unix_socket(path, &un, vsocket->is_server);
618         if (fd < 0)
619                 return -1;
620
621         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&un,
622                                           sizeof(un));
623         if (ret == 0) {
624                 vhost_user_add_connection(fd, vsocket);
625                 return 0;
626         }
627
628         RTE_LOG(ERR, VHOST_CONFIG,
629                 "failed to connect to %s: %s\n",
630                 path, strerror(errno));
631
632         if (ret == -2 || !vsocket->reconnect) {
633                 close(fd);
634                 return -1;
635         }
636
637         RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
638         reconn = malloc(sizeof(*reconn));
639         if (reconn == NULL) {
640                 RTE_LOG(ERR, VHOST_CONFIG,
641                         "failed to allocate memory for reconnect\n");
642                 close(fd);
643                 return -1;
644         }
645         reconn->un = un;
646         reconn->fd = fd;
647         reconn->vsocket = vsocket;
648         pthread_mutex_lock(&reconn_list.mutex);
649         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
650         pthread_mutex_unlock(&reconn_list.mutex);
651
652         return 0;
653 }
654
655 /*
656  * Register a new vhost-user socket; here we could act as server
657  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
658  * is set.
659  */
660 int
661 rte_vhost_driver_register(const char *path, uint64_t flags)
662 {
663         int ret = -1;
664         struct vhost_user_socket *vsocket;
665
666         if (!path)
667                 return -1;
668
669         pthread_mutex_lock(&vhost_user.mutex);
670
671         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
672                 RTE_LOG(ERR, VHOST_CONFIG,
673                         "error: the number of vhost sockets reaches maximum\n");
674                 goto out;
675         }
676
677         vsocket = malloc(sizeof(struct vhost_user_socket));
678         if (!vsocket)
679                 goto out;
680         memset(vsocket, 0, sizeof(struct vhost_user_socket));
681         vsocket->path = strdup(path);
682
683         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
684                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
685                 if (vsocket->reconnect && reconn_tid == 0) {
686                         if (vhost_user_reconnect_init() < 0) {
687                                 free(vsocket->path);
688                                 free(vsocket);
689                                 goto out;
690                         }
691                 }
692                 ret = vhost_user_create_client(vsocket);
693         } else {
694                 vsocket->is_server = true;
695                 ret = vhost_user_create_server(vsocket);
696         }
697         if (ret < 0) {
698                 free(vsocket->path);
699                 free(vsocket);
700                 goto out;
701         }
702
703         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
704
705 out:
706         pthread_mutex_unlock(&vhost_user.mutex);
707
708         return ret;
709 }
710
711 /**
712  * Unregister the specified vhost socket
713  */
714 int
715 rte_vhost_driver_unregister(const char *path)
716 {
717         int i;
718         int count;
719
720         pthread_mutex_lock(&vhost_user.mutex);
721
722         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
723                 if (!strcmp(vhost_user.vsockets[i]->path, path)) {
724                         if (vhost_user.vsockets[i]->is_server) {
725                                 fdset_del(&vhost_user.fdset,
726                                         vhost_user.vsockets[i]->listenfd);
727                                 close(vhost_user.vsockets[i]->listenfd);
728                                 unlink(path);
729                         }
730
731                         free(vhost_user.vsockets[i]->path);
732                         free(vhost_user.vsockets[i]);
733
734                         count = --vhost_user.vsocket_cnt;
735                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
736                         vhost_user.vsockets[count] = NULL;
737                         pthread_mutex_unlock(&vhost_user.mutex);
738
739                         return 0;
740                 }
741         }
742         pthread_mutex_unlock(&vhost_user.mutex);
743
744         return -1;
745 }
746
747 int
748 rte_vhost_driver_session_start(void)
749 {
750         fdset_event_dispatch(&vhost_user.fdset);
751         return 0;
752 }