Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Cleanup and sanity-checking of use of strncpy/strcpy (Coverity static analysis)
[palacios.git] / palacios / src / devices / netdisk.c
index 625fca8..8e0ec7f 100644 (file)
@@ -19,9 +19,9 @@
 
 #include <palacios/vmm.h>
 #include <palacios/vmm_dev_mgr.h>
-#include <palacios/vmm_socket.h>
+#include <interfaces/vmm_socket.h>
 
-#ifndef CONFIG_DEBUG_IDE
+#ifndef V3_CONFIG_DEBUG_IDE
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
 #endif
 struct disk_state {
     uint64_t capacity; // in bytes
 
-    int socket;
+    v3_sock_t socket;
 
     uint32_t ip_addr;
     uint16_t port;
 
+    struct v3_vm_info * vm;
+
     char disk_name[32];
 
 };
 
 
-static int send_all(int socket, char * buf, int length) {
+static int send_all(v3_sock_t socket, char * buf, int length) {
     int bytes_sent = 0;
     
-    PrintDebug("Sending %d bytes\n", length - bytes_sent);
+    PrintDebug(VM_NONE, VCORE_NONE, "Sending %d bytes\n", length - bytes_sent);
     while (bytes_sent < length) {
-       int tmp_bytes = V3_Send(socket, buf + bytes_sent, length - bytes_sent);
-       PrintDebug("Sent %d bytes\n", tmp_bytes);
+       int tmp_bytes = v3_socket_send(socket, buf + bytes_sent, length - bytes_sent);
+       PrintDebug(VM_NONE, VCORE_NONE, "Sent %d bytes\n", tmp_bytes);
        
        if (tmp_bytes == 0) {
-           PrintError("Connection Closed unexpectedly\n");
+           PrintError(VM_NONE, VCORE_NONE, "Connection Closed unexpectedly\n");
            return -1;
        }
        
@@ -68,16 +70,16 @@ static int send_all(int socket, char * buf, int length) {
 }
 
 
-static int recv_all(int socket, char * buf, int length) {
+static int recv_all(v3_sock_t socket, char * buf, int length) {
     int bytes_read = 0;
     
-    PrintDebug("Reading %d bytes\n", length - bytes_read);
+    PrintDebug(VM_NONE, VCORE_NONE, "Reading %d bytes\n", length - bytes_read);
     while (bytes_read < length) {
-       int tmp_bytes = V3_Recv(socket, buf + bytes_read, length - bytes_read);
-       PrintDebug("Received %d bytes\n", tmp_bytes);
+       int tmp_bytes = v3_socket_recv(socket, buf + bytes_read, length - bytes_read);
+       PrintDebug(VM_NONE, VCORE_NONE, "Received %d bytes\n", tmp_bytes);
        
        if (tmp_bytes == 0) {
-           PrintError("Connection Closed unexpectedly\n");
+           PrintError(VM_NONE, VCORE_NONE, "Connection Closed unexpectedly\n");
            return -1;
        }
        
@@ -100,46 +102,46 @@ static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_
     nbd_cmd[0] = NBD_READ_CMD;
     
     if (send_all(disk->socket, nbd_cmd, 4) == -1) {
-       PrintError("Error sending read command\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error sending read command\n");
        return -1;
     }
     
     if (send_all(disk->socket, (char *)&offset, 8) == -1) {
-       PrintError("Error sending read offset\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error sending read offset\n");
        return -1;
     }
 
     if (send_all(disk->socket, (char *)&length, 4) == -1) {
-       PrintError("Error sending read length\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error sending read length\n");
        return -1;
     }
 
     if (recv_all(disk->socket, (char *)&status, 1) == -1) {
-       PrintError("Error receiving status\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error receiving status\n");
        return -1;
     }
 
     if (status != NBD_STATUS_OK) {
-       PrintError("NBD Error....\n");
+       PrintError(VM_NONE, VCORE_NONE, "NBD Error....\n");
        return -1;
     }
 
-    PrintDebug("Reading Data Ret Length\n");
+    PrintDebug(VM_NONE, VCORE_NONE, "Reading Data Ret Length\n");
 
     if (recv_all(disk->socket, (char *)&ret_len, 4) == -1) {
-       PrintError("Error receiving Return read length\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error receiving Return read length\n");
        return -1;
     }
 
     if (ret_len != length) {
-       PrintError("Read length mismatch (req=%llu) (result=%u)\n", length, ret_len);
+       PrintError(VM_NONE, VCORE_NONE, "Read length mismatch (req=%llu) (result=%u)\n", length, ret_len);
        return -1;
     }
 
-    PrintDebug("Reading Data (%d bytes)\n", ret_len);
+    PrintDebug(VM_NONE, VCORE_NONE, "Reading Data (%d bytes)\n", ret_len);
 
     if (recv_all(disk->socket, (char *)buf, ret_len) == -1) {
-       PrintError("Read Data Error\n");
+       PrintError(VM_NONE, VCORE_NONE, "Read Data Error\n");
        return -1;
     }
 
@@ -157,34 +159,34 @@ static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private
     nbd_cmd[0] = NBD_WRITE_CMD;
 
     if (send_all(disk->socket, nbd_cmd, 4) == -1) {
-       PrintError("Error sending write command\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error sending write command\n");
        return -1;
     }
 
     if (send_all(disk->socket, (char *)&offset, 8) == -1) {
-       PrintError("Error sending write offset\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error sending write offset\n");
        return -1;
     }
 
     if (send_all(disk->socket, (char *)&length, 4) == -1) {
-       PrintError("Error sending write length\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error sending write length\n");
        return -1;
     }
 
-    PrintDebug("Writing Data (%d bytes)\n", length);
+    PrintDebug(VM_NONE, VCORE_NONE, "Writing Data (%d bytes)\n", length);
 
     if (send_all(disk->socket, (char *)buf, length) == -1) {
-       PrintError("Write Data Error\n");
+       PrintError(VM_NONE, VCORE_NONE, "Write Data Error\n");
        return -1;
     }
 
     if (recv_all(disk->socket, (char *)&status, 1) == -1) {
-       PrintError("Error receiving status\n");
+       PrintError(VM_NONE, VCORE_NONE, "Error receiving status\n");
        return -1;
     }
 
     if (status != NBD_STATUS_OK) {
-       PrintError("NBD Error....\n");
+       PrintError(VM_NONE, VCORE_NONE, "NBD Error....\n");
        return -1;
     }
 
@@ -208,31 +210,32 @@ static struct v3_dev_blk_ops blk_ops = {
 
 
 
-static int disk_free(struct vm_device * dev) {
+static int disk_free(struct disk_state * disk) {
+
+    v3_socket_close(disk->socket);
+
+    V3_Free(disk);
     return 0;
 }
 
 static struct v3_device_ops dev_ops = {
-    .free = disk_free,
-    .reset = NULL,
-    .start = NULL,
-    .stop = NULL,
+    .free = (int (*)(void *))disk_free,
 };
 
 
 static int socket_init(struct disk_state * disk) {
     char header[64];
     
-    PrintDebug("Intializing Net Disk\n");
+    PrintDebug(VM_NONE, VCORE_NONE, "Intializing Net Disk\n");
 
-    disk->socket = V3_Create_TCP_Socket();
+    disk->socket = v3_create_tcp_socket(disk->vm);
 
-    PrintDebug("DISK socket: %d\n", disk->socket);
-    PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(disk->ip_addr), disk->port);
+    PrintDebug(VM_NONE, VCORE_NONE, "DISK socket: %p\n", disk->socket);
+    PrintDebug(VM_NONE, VCORE_NONE, "Connecting to: %s:%d\n", v3_inet_ntoa(disk->ip_addr), disk->port);
 
-    V3_Connect_To_IP(disk->socket, v3_ntohl(disk->ip_addr), disk->port);
+    v3_connect_to_ip(disk->socket, v3_ntohl(disk->ip_addr), disk->port);
 
-    PrintDebug("Connected to NBD server\n");
+    PrintDebug(VM_NONE, VCORE_NONE, "Connected to NBD server\n");
 
     //snprintf(header, 64, "V3_NBD_1 %s\n", cd->disk_name);
     strcpy(header, "V3_NBD_1 ");
@@ -241,7 +244,7 @@ static int socket_init(struct disk_state * disk) {
 
 
     if (send_all(disk->socket, header, strlen(header)) == -1) {
-       PrintError("Error connecting to Network Block Device: %s\n", disk->disk_name);
+       PrintError(VM_NONE, VCORE_NONE, "Error connecting to Network Block Device: %s\n", disk->disk_name);
        return -1;
     }
 
@@ -252,16 +255,16 @@ static int socket_init(struct disk_state * disk) {
        nbd_cmd[0] = NBD_CAPACITY_CMD;
        
        if (send_all(disk->socket, nbd_cmd, 4) == -1) {
-           PrintError("Error sending capacity command\n");
+           PrintError(VM_NONE, VCORE_NONE, "Error sending capacity command\n");
            return -1;
        }
 
        if (recv_all(disk->socket, (char *)&(disk->capacity), 8) == -1) {
-           PrintError("Error Receiving Capacity\n");
+           PrintError(VM_NONE, VCORE_NONE, "Error Receiving Capacity\n");
            return -1;
        }       
 
-       PrintDebug("Capacity: %p\n", (void *)(disk->capacity));
+       PrintDebug(VM_NONE, VCORE_NONE, "Capacity: %p\n", (void *)(addr_t)disk->capacity);
     }
 
 
@@ -273,40 +276,50 @@ static int socket_init(struct disk_state * disk) {
 static int disk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     struct disk_state * disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
 
+    if (!disk) {
+       PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
+       return -1;
+    }
+
     char * ip_str = v3_cfg_val(cfg, "IP");
     char * port_str = v3_cfg_val(cfg, "port");
     char * disk_tag = v3_cfg_val(cfg, "tag");
-    char * name = v3_cfg_val(cfg, "name");
+    char * dev_id = v3_cfg_val(cfg, "ID");
 
     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
 
-    PrintDebug("Registering Net disk at %s:%s disk=%s\n", ip_str, port_str, disk_tag);
+    PrintDebug(vm, VCORE_NONE, "Registering Net disk at %s:%s disk=%s\n", ip_str, port_str, disk_tag);
 
     strncpy(disk->disk_name, disk_tag, sizeof(disk->disk_name));
+    disk->disk_name[sizeof(disk->disk_name)-1] = 0;
     disk->ip_addr = v3_inet_addr(ip_str);
     disk->port = atoi(port_str);
-       
-    struct vm_device * dev = v3_allocate_device(name, &dev_ops, disk);
+    disk->vm = vm;
+
+    struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, disk);
 
-    if (v3_attach_device(vm, dev) == -1) {
-       PrintError("Could not attach device %s\n", name);
+    if (dev == NULL) {
+       PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
+       V3_Free(disk);
        return -1;
     }
 
     if (socket_init(disk) == -1) {
-       PrintError("could not initialize network connection\n");
+       PrintError(vm, VCORE_NONE, "could not initialize network connection\n");
+       v3_remove_device(dev);
        return -1;
     }
 
-    PrintDebug("Registering Disk\n");
+    PrintDebug(vm, VCORE_NONE, "Registering Disk\n");
 
     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
                           &blk_ops, frontend_cfg, disk) == -1) {
-       PrintError("Could not connect %s to frontend\n", name);
+       PrintError(vm, VCORE_NONE, "Could not connect %s to frontend\n", dev_id);
+       v3_remove_device(dev);
        return -1;
     }
 
-    PrintDebug("intialization done\n");
+    PrintDebug(vm, VCORE_NONE, "intialization done\n");
 
     return 0;
 }