Imported Upstream version 16.07-rc3
[deb_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 <pthread.h>
47
48 #include <rte_log.h>
49 #include <rte_virtio_net.h>
50
51 #include "fd_man.h"
52 #include "vhost-net-user.h"
53 #include "vhost-net.h"
54 #include "virtio-net-user.h"
55
56 /*
57  * Every time rte_vhost_driver_register() is invoked, an associated
58  * vhost_user_socket struct will be created.
59  */
60 struct vhost_user_socket {
61         char *path;
62         int listenfd;
63         bool is_server;
64         bool reconnect;
65 };
66
67 struct vhost_user_connection {
68         struct vhost_user_socket *vsocket;
69         int vid;
70 };
71
72 #define MAX_VHOST_SOCKET 1024
73 struct vhost_user {
74         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
75         struct fdset fdset;
76         int vsocket_cnt;
77         pthread_mutex_t mutex;
78 };
79
80 #define MAX_VIRTIO_BACKLOG 128
81
82 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
83 static void vhost_user_msg_handler(int fd, void *dat, int *remove);
84 static int vhost_user_create_client(struct vhost_user_socket *vsocket);
85
86 static struct vhost_user vhost_user = {
87         .fdset = {
88                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
89                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
90                 .num = 0
91         },
92         .vsocket_cnt = 0,
93         .mutex = PTHREAD_MUTEX_INITIALIZER,
94 };
95
96 static const char *vhost_message_str[VHOST_USER_MAX] = {
97         [VHOST_USER_NONE] = "VHOST_USER_NONE",
98         [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
99         [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
100         [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
101         [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
102         [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
103         [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
104         [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
105         [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
106         [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
107         [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
108         [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
109         [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
110         [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
111         [VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
112         [VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
113         [VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
114         [VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
115         [VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
116         [VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
117 };
118
119 /* return bytes# of read on success or negative val on failure. */
120 static int
121 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
122 {
123         struct iovec iov;
124         struct msghdr msgh;
125         size_t fdsize = fd_num * sizeof(int);
126         char control[CMSG_SPACE(fdsize)];
127         struct cmsghdr *cmsg;
128         int ret;
129
130         memset(&msgh, 0, sizeof(msgh));
131         iov.iov_base = buf;
132         iov.iov_len  = buflen;
133
134         msgh.msg_iov = &iov;
135         msgh.msg_iovlen = 1;
136         msgh.msg_control = control;
137         msgh.msg_controllen = sizeof(control);
138
139         ret = recvmsg(sockfd, &msgh, 0);
140         if (ret <= 0) {
141                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
142                 return ret;
143         }
144
145         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
146                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
147                 return -1;
148         }
149
150         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
151                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
152                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
153                         (cmsg->cmsg_type == SCM_RIGHTS)) {
154                         memcpy(fds, CMSG_DATA(cmsg), fdsize);
155                         break;
156                 }
157         }
158
159         return ret;
160 }
161
162 /* return bytes# of read on success or negative val on failure. */
163 static int
164 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
165 {
166         int ret;
167
168         ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
169                 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
170         if (ret <= 0)
171                 return ret;
172
173         if (msg && msg->size) {
174                 if (msg->size > sizeof(msg->payload)) {
175                         RTE_LOG(ERR, VHOST_CONFIG,
176                                 "invalid msg size: %d\n", msg->size);
177                         return -1;
178                 }
179                 ret = read(sockfd, &msg->payload, msg->size);
180                 if (ret <= 0)
181                         return ret;
182                 if (ret != (int)msg->size) {
183                         RTE_LOG(ERR, VHOST_CONFIG,
184                                 "read control message failed\n");
185                         return -1;
186                 }
187         }
188
189         return ret;
190 }
191
192 static int
193 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
194 {
195
196         struct iovec iov;
197         struct msghdr msgh;
198         size_t fdsize = fd_num * sizeof(int);
199         char control[CMSG_SPACE(fdsize)];
200         struct cmsghdr *cmsg;
201         int ret;
202
203         memset(&msgh, 0, sizeof(msgh));
204         iov.iov_base = buf;
205         iov.iov_len = buflen;
206
207         msgh.msg_iov = &iov;
208         msgh.msg_iovlen = 1;
209
210         if (fds && fd_num > 0) {
211                 msgh.msg_control = control;
212                 msgh.msg_controllen = sizeof(control);
213                 cmsg = CMSG_FIRSTHDR(&msgh);
214                 cmsg->cmsg_len = CMSG_LEN(fdsize);
215                 cmsg->cmsg_level = SOL_SOCKET;
216                 cmsg->cmsg_type = SCM_RIGHTS;
217                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
218         } else {
219                 msgh.msg_control = NULL;
220                 msgh.msg_controllen = 0;
221         }
222
223         do {
224                 ret = sendmsg(sockfd, &msgh, 0);
225         } while (ret < 0 && errno == EINTR);
226
227         if (ret < 0) {
228                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
229                 return ret;
230         }
231
232         return ret;
233 }
234
235 static int
236 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
237 {
238         int ret;
239
240         if (!msg)
241                 return 0;
242
243         msg->flags &= ~VHOST_USER_VERSION_MASK;
244         msg->flags |= VHOST_USER_VERSION;
245         msg->flags |= VHOST_USER_REPLY_MASK;
246
247         ret = send_fd_message(sockfd, (char *)msg,
248                 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
249
250         return ret;
251 }
252
253
254 static void
255 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
256 {
257         int vid;
258         size_t size;
259         struct vhost_user_connection *conn;
260         int ret;
261
262         conn = malloc(sizeof(*conn));
263         if (conn == NULL) {
264                 close(fd);
265                 return;
266         }
267
268         vid = vhost_new_device();
269         if (vid == -1) {
270                 close(fd);
271                 free(conn);
272                 return;
273         }
274
275         size = strnlen(vsocket->path, PATH_MAX);
276         vhost_set_ifname(vid, vsocket->path, size);
277
278         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
279
280         conn->vsocket = vsocket;
281         conn->vid = vid;
282         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_msg_handler,
283                         NULL, conn);
284         if (ret < 0) {
285                 free(conn);
286                 close(fd);
287                 RTE_LOG(ERR, VHOST_CONFIG,
288                         "failed to add fd %d into vhost server fdset\n",
289                         fd);
290         }
291 }
292
293 /* call back when there is new vhost-user connection from client  */
294 static void
295 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
296 {
297         struct vhost_user_socket *vsocket = dat;
298
299         fd = accept(fd, NULL, NULL);
300         if (fd < 0)
301                 return;
302
303         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
304         vhost_user_add_connection(fd, vsocket);
305 }
306
307 /* callback when there is message on the connfd */
308 static void
309 vhost_user_msg_handler(int connfd, void *dat, int *remove)
310 {
311         int vid;
312         struct vhost_user_connection *conn = dat;
313         struct VhostUserMsg msg;
314         uint64_t features;
315         int ret;
316
317         vid = conn->vid;
318         ret = read_vhost_message(connfd, &msg);
319         if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
320                 struct vhost_user_socket *vsocket = conn->vsocket;
321
322                 if (ret < 0)
323                         RTE_LOG(ERR, VHOST_CONFIG,
324                                 "vhost read message failed\n");
325                 else if (ret == 0)
326                         RTE_LOG(INFO, VHOST_CONFIG,
327                                 "vhost peer closed\n");
328                 else
329                         RTE_LOG(ERR, VHOST_CONFIG,
330                                 "vhost read incorrect message\n");
331
332                 close(connfd);
333                 *remove = 1;
334                 free(conn);
335                 vhost_destroy_device(vid);
336
337                 if (vsocket->reconnect)
338                         vhost_user_create_client(vsocket);
339
340                 return;
341         }
342
343         RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
344                 vhost_message_str[msg.request]);
345         switch (msg.request) {
346         case VHOST_USER_GET_FEATURES:
347                 ret = vhost_get_features(vid, &features);
348                 msg.payload.u64 = features;
349                 msg.size = sizeof(msg.payload.u64);
350                 send_vhost_message(connfd, &msg);
351                 break;
352         case VHOST_USER_SET_FEATURES:
353                 features = msg.payload.u64;
354                 vhost_set_features(vid, &features);
355                 break;
356
357         case VHOST_USER_GET_PROTOCOL_FEATURES:
358                 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
359                 msg.size = sizeof(msg.payload.u64);
360                 send_vhost_message(connfd, &msg);
361                 break;
362         case VHOST_USER_SET_PROTOCOL_FEATURES:
363                 user_set_protocol_features(vid, msg.payload.u64);
364                 break;
365
366         case VHOST_USER_SET_OWNER:
367                 vhost_set_owner(vid);
368                 break;
369         case VHOST_USER_RESET_OWNER:
370                 vhost_reset_owner(vid);
371                 break;
372
373         case VHOST_USER_SET_MEM_TABLE:
374                 user_set_mem_table(vid, &msg);
375                 break;
376
377         case VHOST_USER_SET_LOG_BASE:
378                 user_set_log_base(vid, &msg);
379
380                 /* it needs a reply */
381                 msg.size = sizeof(msg.payload.u64);
382                 send_vhost_message(connfd, &msg);
383                 break;
384         case VHOST_USER_SET_LOG_FD:
385                 close(msg.fds[0]);
386                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
387                 break;
388
389         case VHOST_USER_SET_VRING_NUM:
390                 vhost_set_vring_num(vid, &msg.payload.state);
391                 break;
392         case VHOST_USER_SET_VRING_ADDR:
393                 vhost_set_vring_addr(vid, &msg.payload.addr);
394                 break;
395         case VHOST_USER_SET_VRING_BASE:
396                 vhost_set_vring_base(vid, &msg.payload.state);
397                 break;
398
399         case VHOST_USER_GET_VRING_BASE:
400                 ret = user_get_vring_base(vid, &msg.payload.state);
401                 msg.size = sizeof(msg.payload.state);
402                 send_vhost_message(connfd, &msg);
403                 break;
404
405         case VHOST_USER_SET_VRING_KICK:
406                 user_set_vring_kick(vid, &msg);
407                 break;
408         case VHOST_USER_SET_VRING_CALL:
409                 user_set_vring_call(vid, &msg);
410                 break;
411
412         case VHOST_USER_SET_VRING_ERR:
413                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
414                         close(msg.fds[0]);
415                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
416                 break;
417
418         case VHOST_USER_GET_QUEUE_NUM:
419                 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
420                 msg.size = sizeof(msg.payload.u64);
421                 send_vhost_message(connfd, &msg);
422                 break;
423
424         case VHOST_USER_SET_VRING_ENABLE:
425                 user_set_vring_enable(vid, &msg.payload.state);
426                 break;
427         case VHOST_USER_SEND_RARP:
428                 user_send_rarp(vid, &msg);
429                 break;
430
431         default:
432                 break;
433
434         }
435 }
436
437 static int
438 create_unix_socket(const char *path, struct sockaddr_un *un, bool is_server)
439 {
440         int fd;
441
442         fd = socket(AF_UNIX, SOCK_STREAM, 0);
443         if (fd < 0)
444                 return -1;
445         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
446                 is_server ? "server" : "client", fd);
447
448         memset(un, 0, sizeof(*un));
449         un->sun_family = AF_UNIX;
450         strncpy(un->sun_path, path, sizeof(un->sun_path));
451         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
452
453         return fd;
454 }
455
456 static int
457 vhost_user_create_server(struct vhost_user_socket *vsocket)
458 {
459         int fd;
460         int ret;
461         struct sockaddr_un un;
462         const char *path = vsocket->path;
463
464         fd = create_unix_socket(path, &un, vsocket->is_server);
465         if (fd < 0)
466                 return -1;
467
468         ret = bind(fd, (struct sockaddr *)&un, sizeof(un));
469         if (ret < 0) {
470                 RTE_LOG(ERR, VHOST_CONFIG,
471                         "failed to bind to %s: %s; remove it and try again\n",
472                         path, strerror(errno));
473                 goto err;
474         }
475         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
476
477         ret = listen(fd, MAX_VIRTIO_BACKLOG);
478         if (ret < 0)
479                 goto err;
480
481         vsocket->listenfd = fd;
482         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
483                   NULL, vsocket);
484         if (ret < 0) {
485                 RTE_LOG(ERR, VHOST_CONFIG,
486                         "failed to add listen fd %d to vhost server fdset\n",
487                         fd);
488                 goto err;
489         }
490
491         return 0;
492
493 err:
494         close(fd);
495         return -1;
496 }
497
498 struct vhost_user_reconnect {
499         struct sockaddr_un un;
500         int fd;
501         struct vhost_user_socket *vsocket;
502
503         TAILQ_ENTRY(vhost_user_reconnect) next;
504 };
505
506 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
507 struct vhost_user_reconnect_list {
508         struct vhost_user_reconnect_tailq_list head;
509         pthread_mutex_t mutex;
510 };
511
512 static struct vhost_user_reconnect_list reconn_list;
513 static pthread_t reconn_tid;
514
515 static void *
516 vhost_user_client_reconnect(void *arg __rte_unused)
517 {
518         struct vhost_user_reconnect *reconn, *next;
519
520         while (1) {
521                 pthread_mutex_lock(&reconn_list.mutex);
522
523                 /*
524                  * An equal implementation of TAILQ_FOREACH_SAFE,
525                  * which does not exist on all platforms.
526                  */
527                 for (reconn = TAILQ_FIRST(&reconn_list.head);
528                      reconn != NULL; reconn = next) {
529                         next = TAILQ_NEXT(reconn, next);
530
531                         if (connect(reconn->fd, (struct sockaddr *)&reconn->un,
532                                     sizeof(reconn->un)) < 0)
533                                 continue;
534
535                         RTE_LOG(INFO, VHOST_CONFIG,
536                                 "%s: connected\n", reconn->vsocket->path);
537                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
538                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
539                         free(reconn);
540                 }
541
542                 pthread_mutex_unlock(&reconn_list.mutex);
543                 sleep(1);
544         }
545
546         return NULL;
547 }
548
549 static int
550 vhost_user_reconnect_init(void)
551 {
552         int ret;
553
554         pthread_mutex_init(&reconn_list.mutex, NULL);
555         TAILQ_INIT(&reconn_list.head);
556
557         ret = pthread_create(&reconn_tid, NULL,
558                              vhost_user_client_reconnect, NULL);
559         if (ret < 0)
560                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
561
562         return ret;
563 }
564
565 static int
566 vhost_user_create_client(struct vhost_user_socket *vsocket)
567 {
568         int fd;
569         int ret;
570         struct sockaddr_un un;
571         const char *path = vsocket->path;
572         struct vhost_user_reconnect *reconn;
573
574         fd = create_unix_socket(path, &un, vsocket->is_server);
575         if (fd < 0)
576                 return -1;
577
578         ret = connect(fd, (struct sockaddr *)&un, sizeof(un));
579         if (ret == 0) {
580                 vhost_user_add_connection(fd, vsocket);
581                 return 0;
582         }
583
584         RTE_LOG(ERR, VHOST_CONFIG,
585                 "failed to connect to %s: %s\n",
586                 path, strerror(errno));
587
588         if (!vsocket->reconnect) {
589                 close(fd);
590                 return -1;
591         }
592
593         RTE_LOG(ERR, VHOST_CONFIG, "%s: reconnecting...\n", path);
594         reconn = malloc(sizeof(*reconn));
595         if (reconn == NULL) {
596                 RTE_LOG(ERR, VHOST_CONFIG,
597                         "failed to allocate memory for reconnect\n");
598                 close(fd);
599                 return -1;
600         }
601         reconn->un = un;
602         reconn->fd = fd;
603         reconn->vsocket = vsocket;
604         pthread_mutex_lock(&reconn_list.mutex);
605         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
606         pthread_mutex_unlock(&reconn_list.mutex);
607
608         return 0;
609 }
610
611 /*
612  * Register a new vhost-user socket; here we could act as server
613  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
614  * is set.
615  */
616 int
617 rte_vhost_driver_register(const char *path, uint64_t flags)
618 {
619         int ret = -1;
620         struct vhost_user_socket *vsocket;
621
622         if (!path)
623                 return -1;
624
625         pthread_mutex_lock(&vhost_user.mutex);
626
627         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
628                 RTE_LOG(ERR, VHOST_CONFIG,
629                         "error: the number of vhost sockets reaches maximum\n");
630                 goto out;
631         }
632
633         vsocket = malloc(sizeof(struct vhost_user_socket));
634         if (!vsocket)
635                 goto out;
636         memset(vsocket, 0, sizeof(struct vhost_user_socket));
637         vsocket->path = strdup(path);
638
639         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
640                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
641                 if (vsocket->reconnect && reconn_tid == 0) {
642                         if (vhost_user_reconnect_init() < 0) {
643                                 free(vsocket->path);
644                                 free(vsocket);
645                                 goto out;
646                         }
647                 }
648                 ret = vhost_user_create_client(vsocket);
649         } else {
650                 vsocket->is_server = true;
651                 ret = vhost_user_create_server(vsocket);
652         }
653         if (ret < 0) {
654                 free(vsocket->path);
655                 free(vsocket);
656                 goto out;
657         }
658
659         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
660
661 out:
662         pthread_mutex_unlock(&vhost_user.mutex);
663
664         return ret;
665 }
666
667 /**
668  * Unregister the specified vhost socket
669  */
670 int
671 rte_vhost_driver_unregister(const char *path)
672 {
673         int i;
674         int count;
675
676         pthread_mutex_lock(&vhost_user.mutex);
677
678         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
679                 if (!strcmp(vhost_user.vsockets[i]->path, path)) {
680                         if (vhost_user.vsockets[i]->is_server) {
681                                 fdset_del(&vhost_user.fdset,
682                                         vhost_user.vsockets[i]->listenfd);
683                                 close(vhost_user.vsockets[i]->listenfd);
684                                 unlink(path);
685                         }
686
687                         free(vhost_user.vsockets[i]->path);
688                         free(vhost_user.vsockets[i]);
689
690                         count = --vhost_user.vsocket_cnt;
691                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
692                         vhost_user.vsockets[count] = NULL;
693                         pthread_mutex_unlock(&vhost_user.mutex);
694
695                         return 0;
696                 }
697         }
698         pthread_mutex_unlock(&vhost_user.mutex);
699
700         return -1;
701 }
702
703 int
704 rte_vhost_driver_session_start(void)
705 {
706         fdset_event_dispatch(&vhost_user.fdset);
707         return 0;
708 }