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