Line data Source code
1 : /* 2 : * vr_uvhost_client.c - client handling in user space vhost server that 3 : * peers with the vhost client inside qemu (version 2.1 and later). 4 : * 5 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 6 : */ 7 : 8 : #include "vr_dpdk.h" 9 : #include "vr_uvhost_client.h" 10 : #include "vr_uvhost_util.h" 11 : #include "vr_uvhost_msg.h" 12 : 13 : static vr_uvh_client_t vr_uvh_clients[VR_UVH_MAX_CLIENTS]; 14 : 15 : /* 16 : * vr_uvhost_client_init - initialize the client array. 17 : */ 18 : void 19 53 : vr_uvhost_client_init(void) 20 : { 21 : int i; 22 : 23 230868 : for (i = 0; i < VR_UVH_MAX_CLIENTS; i++) { 24 230815 : vr_uvh_clients[i].vruc_fd = -1; 25 : } 26 : 27 53 : return; 28 : } 29 : 30 : /* 31 : * vr_uvhost_new_client - initializes state for a new user space vhost client 32 : * FD is a file descriptor for the client socket. path is the UNIX domain 33 : * socket path. cidx is the index of the client. 34 : * 35 : * Returns a pointer to the client state on success, NULL otherwise. 36 : */ 37 : vr_uvh_client_t * 38 332 : vr_uvhost_new_client(int fd, char *path, int cidx) 39 : { 40 332 : if (cidx >= VR_UVH_MAX_CLIENTS) { 41 0 : return NULL; 42 : } 43 : 44 332 : if (vr_uvh_clients[cidx].vruc_fd != -1) { 45 97 : return NULL; 46 : } 47 : 48 235 : vr_uvh_clients[cidx].vruc_fd = fd; 49 235 : strncpy(vr_uvh_clients[cidx].vruc_path, path, VR_UNIX_PATH_MAX - 1); 50 235 : vr_uvh_clients[cidx].vruc_flags = 0; 51 : 52 235 : return &vr_uvh_clients[cidx]; 53 : } 54 : 55 : /* 56 : * vr_uvhost_del_client - removes a vhost client. 57 : * 58 : * Returns nothing. 59 : */ 60 : void 61 166 : vr_uvhost_del_client(vr_uvh_client_t *vru_cl) 62 : { 63 : /* Remove both the socket we listen for and the socket we have accepted */ 64 166 : vr_uvhost_del_fds_by_arg(vru_cl); 65 : 66 : /* If a VIF is added but not connected, vru_cl->vruc_fd is not added to 67 : * the fd list even though it is created. This can happen when VM is 68 : * stopped. In this case, vr_uvhost_del_fds_by_arg() would not close 69 : * the fd. So, add the fcntl() call below to check if vruc_fd is closed 70 : * or not. 71 : * */ 72 166 : if(fcntl(vru_cl->vruc_fd, F_GETFL) != -1 ){ 73 166 : vr_uvhost_log("Closing socket fd: %d \n", vru_cl->vruc_fd); 74 166 : close(vru_cl->vruc_fd); 75 : } 76 : 77 166 : vru_cl->vruc_fd = -1; 78 166 : if (vru_cl->vruc_vhostuser_mode == VRNU_VIF_MODE_CLIENT) 79 0 : unlink(vru_cl->vruc_path); 80 166 : vru_cl->vruc_flags = 0; 81 : 82 166 : return; 83 : } 84 : 85 : /* 86 : * vr_uvhost_cl_set_fd - set the FD for a user space vhost client 87 : */ 88 : void 89 0 : vr_uvhost_cl_set_fd(vr_uvh_client_t *vru_cl, int fd) 90 : { 91 0 : vru_cl->vruc_fd = fd; 92 : 93 0 : return; 94 : } 95 : 96 : /* 97 : * vr_uvhost_get_client - Returns the client at the specified index, NULL if 98 : * it cannot be found. 99 : */ 100 : vr_uvh_client_t * 101 166 : vr_uvhost_get_client(unsigned int cidx) 102 : { 103 166 : if (cidx >= VR_UVH_MAX_CLIENTS) { 104 0 : return NULL; 105 : } 106 : 107 166 : return &vr_uvh_clients[cidx]; 108 : }