* Set up environment to talk with a vhost user backend. * * @return * - (-1) if fail; * - (0) if succeed. */
| 813 | * - (0) if succeed. |
| 814 | */ |
| 815 | static int |
| 816 | vhost_user_setup(struct virtio_user_dev *dev) |
| 817 | { |
| 818 | int fd; |
| 819 | int flag; |
| 820 | struct sockaddr_un un; |
| 821 | struct vhost_user_data *data; |
| 822 | |
| 823 | data = malloc(sizeof(*data)); |
| 824 | if (!data) { |
| 825 | PMD_DRV_LOG(ERR, "(%s) Failed to allocate Vhost-user data", dev->path); |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | memset(data, 0, sizeof(*data)); |
| 830 | |
| 831 | dev->backend_data = data; |
| 832 | |
| 833 | data->vhostfd = -1; |
| 834 | data->listenfd = -1; |
| 835 | |
| 836 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 837 | if (fd < 0) { |
| 838 | PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno)); |
| 839 | goto err_data; |
| 840 | } |
| 841 | |
| 842 | flag = fcntl(fd, F_GETFD); |
| 843 | if (flag == -1) |
| 844 | PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", strerror(errno)); |
| 845 | else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0) |
| 846 | PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", strerror(errno)); |
| 847 | |
| 848 | memset(&un, 0, sizeof(un)); |
| 849 | un.sun_family = AF_UNIX; |
| 850 | strlcpy(un.sun_path, dev->path, sizeof(un.sun_path)); |
| 851 | |
| 852 | if (dev->is_server) { |
| 853 | data->listenfd = fd; |
| 854 | if (vhost_user_start_server(dev, &un) < 0) { |
| 855 | PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode"); |
| 856 | goto err_socket; |
| 857 | } |
| 858 | } else { |
| 859 | if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) { |
| 860 | PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno)); |
| 861 | goto err_socket; |
| 862 | } |
| 863 | data->vhostfd = fd; |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | |
| 868 | err_socket: |
| 869 | close(fd); |
| 870 | err_data: |
| 871 | free(data); |
| 872 | dev->backend_data = NULL; |