X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fnet_cd.c;h=f980bab6abcc3fbb6fcf5ebe55d0c93828fc93bf;hp=fb88e37ebaef290b202678fdc6d7aa7755deceb7;hb=0e097100a26bc43eb8964734fa43130fc4c71429;hpb=d885ccd3aacee8aaad051576839a5de545c79bdc diff --git a/palacios/src/devices/net_cd.c b/palacios/src/devices/net_cd.c index fb88e37..f980bab 100644 --- a/palacios/src/devices/net_cd.c +++ b/palacios/src/devices/net_cd.c @@ -18,18 +18,32 @@ */ #include -#include +#include #include +#include -#ifndef DEBUG_IDE +#ifndef CONFIG_DEBUG_IDE #undef PrintDebug #define PrintDebug(fmt, args...) -#endif +#endif +#define NBD_READ_CMD 0x1 +#define NBD_WRITE_CMD 0x2 +#define NBD_CAPACITY_CMD 0x3 + +#define NBD_STATUS_OK 0x00 +#define NBD_STATUS_ERR 0xff + struct cd_state { - addr_t disk_image; - uint32_t capacity; // in bytes + uint64_t capacity; // in bytes + + int socket; + + uint32_t ip_addr; + uint16_t port; + + char disk_name[32]; struct vm_device * ide; @@ -38,17 +52,101 @@ struct cd_state { }; +static int send_all(int socket, char * buf, int length) { + int bytes_sent = 0; + + PrintDebug("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); + + if (tmp_bytes == 0) { + PrintError("Connection Closed unexpectedly\n"); + return -1; + } + + bytes_sent += tmp_bytes; + } + + return 0; +} + + +static int recv_all(int socket, char * buf, int length) { + int bytes_read = 0; + + PrintDebug("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); + + if (tmp_bytes == 0) { + PrintError("Connection Closed unexpectedly\n"); + return -1; + } + + bytes_read += tmp_bytes; + } + + return 0; +} + // CDs always read 2048 byte blocks... ? static int cd_read(uint8_t * buf, int block_count, uint64_t lba, void * private_data) { struct vm_device * cd_dev = (struct vm_device *)private_data; struct cd_state * cd = (struct cd_state *)(cd_dev->private_data); - int offset = lba * ATAPI_BLOCK_SIZE; + uint64_t offset = lba * ATAPI_BLOCK_SIZE; int length = block_count * ATAPI_BLOCK_SIZE; + uint8_t status; + uint32_t ret_len = 0; + char nbd_cmd[4] = {0,0,0,0}; + + nbd_cmd[0] = NBD_READ_CMD; + + if (send_all(cd->socket, nbd_cmd, 4) == -1) { + PrintError("Error sending capacity command\n"); + return -1; + } + + if (send_all(cd->socket, (char *)&offset, 8) == -1) { + PrintError("Error sending read offset\n"); + return -1; + } + + if (send_all(cd->socket, (char *)&length, 4) == -1) { + PrintError("Error sending read length\n"); + return -1; + } + + if (recv_all(cd->socket, (char *)&status, 1) == -1) { + PrintError("Error receiving status\n"); + return -1; + } + + if (status != NBD_STATUS_OK) { + PrintError("NBD Error....\n"); + return -1; + } + + PrintDebug("Reading Data Ret Length\n"); + + if (recv_all(cd->socket, (char *)&ret_len, 4) == -1) { + PrintError("Error receiving Return read length\n"); + return -1; + } - PrintDebug("Reading RAM CD at (LBA=%d) offset %d (length=%d)\n", (uint32_t)lba, offset, length); + if (ret_len != length) { + PrintError("Read length mismatch (req=%d) (result=%d)\n", length, ret_len); + return -1; + } - memcpy(buf, (uint8_t *)(cd->disk_image + offset), length); + PrintDebug("Reading Data (%d bytes)\n", ret_len); + if (recv_all(cd->socket, (char *)buf, ret_len) == -1) { + PrintError("Read Data Error\n"); + return -1; + } + return 0; } @@ -56,63 +154,124 @@ static int cd_read(uint8_t * buf, int block_count, uint64_t lba, void * private static uint32_t cd_get_capacity(void * private_data) { struct vm_device * cd_dev = (struct vm_device *)private_data; struct cd_state * cd = (struct cd_state *)(cd_dev->private_data); - PrintDebug("Querying RAM CD capacity (bytes=%d) (ret = %d)\n", - cd->capacity, cd->capacity / ATAPI_BLOCK_SIZE); + return cd->capacity / ATAPI_BLOCK_SIZE; } -static struct v3_ide_cd_ops cd_ops = { +static struct v3_cd_ops cd_ops = { .read = cd_read, .get_capacity = cd_get_capacity, }; -static int cd_init(struct vm_device * dev) { - struct cd_state * cd = (struct cd_state *)(dev->private_data); - - if (v3_ide_register_cdrom(cd->ide, cd->bus, cd->drive, "RAM-CD", &cd_ops, dev) == -1) { - return -1; - } - - return 0; -} -static int cd_deinit(struct vm_device * dev) { +static int cd_free(struct vm_device * dev) { return 0; } -static struct vm_device_ops dev_ops = { - .init = cd_init, - .deinit = cd_deinit, +static struct v3_device_ops dev_ops = { + .free = cd_free, .reset = NULL, .start = NULL, .stop = NULL, }; -struct vm_device * v3_create_ram_cd(struct vm_device * ide, - uint_t bus, uint_t drive, - addr_t ramdisk, uint32_t size) { - struct cd_state * cd = NULL; - if (size % ATAPI_BLOCK_SIZE) { - PrintError("CD image must be an integral of block size (ATAPI_BLOCK_SIZE=%d)\n", ATAPI_BLOCK_SIZE); - return NULL; + +static int socket_init(struct cd_state * cd) { + char header[64]; + + PrintDebug("Intializing Net CD\n"); + + cd->socket = V3_Create_TCP_Socket(); + + PrintDebug("CD socket: %d\n", cd->socket); + PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(cd->ip_addr), cd->port); + + V3_Connect_To_IP(cd->socket, v3_ntohl(cd->ip_addr), cd->port); + + + PrintDebug("Connected to NBD server\n"); + + //snprintf(header, 64, "V3_NBD_1 %s\n", cd->disk_name); + strcpy(header, "V3_NBD_1 "); + strncat(header, cd->disk_name, 32); + strncat(header, "\n", 1); + + + if (send_all(cd->socket, header, strlen(header)) == -1) { + PrintError("Error connecting to Network Block Device: %s\n", cd->disk_name); + return -1; } - cd = (struct cd_state *)V3_Malloc(sizeof(struct cd_state)); + // Cache Capacity + { + char nbd_cmd[4] = {0,0,0,0}; + + nbd_cmd[0] = NBD_CAPACITY_CMD; + + if (send_all(cd->socket, nbd_cmd, 4) == -1) { + PrintError("Error sending capacity command\n"); + return -1; + } + + if (recv_all(cd->socket, (char *)&(cd->capacity), 8) == -1) { + PrintError("Error Receiving Capacity\n"); + return -1; + } + + PrintDebug("Capacity: %p\n", (void *)(cd->capacity)); + } - PrintDebug("Registering Ram CD at %p (size=%d)\n", (void *)ramdisk, size); + return 0; +} - - cd->disk_image = ramdisk; - cd->capacity = size; +static int cd_init(struct guest_info * vm, void * cfg_data) { + struct net_cd_cfg * cfg = (struct net_cd_cfg *)cfg_data; + struct cd_state * cd = (struct cd_state *)V3_Malloc(sizeof(struct cd_state)); - cd->ide = ide; - cd->bus = bus; - cd->drive = drive; + PrintDebug("Registering Net CD at %s:%d disk=%s\n", cfg->ip_str, cfg->port, cfg->disk_tag); + + strncpy(cd->disk_name, cfg->disk_tag, sizeof(cd->disk_name)); + cd->ip_addr = v3_inet_addr(cfg->ip_str); + cd->port = cfg->port; + + cd->ide = (struct vm_device * )v3_find_dev(vm, cfg->ide); + + if (cd->ide == 0) { + PrintError("Could not find backend %s\n", cfg->ide); + return -1; + } + + cd->bus = cfg->bus; + cd->drive = cfg->drive; - struct vm_device * cd_dev = v3_create_device("RAM-CD", &dev_ops, cd); + struct vm_device * dev = v3_allocate_device("NET-CD", &dev_ops, cd); - return cd_dev; + + if (v3_attach_device(vm, dev) == -1) { + PrintError("Could not attach device %s\n", "NET-CD"); + return -1; + } + + if (socket_init(cd) == -1) { + PrintError("Could not initialize socket connection\n"); + return -1; + } + + PrintDebug("Registering CD\n"); + + if (v3_ide_register_cdrom(cd->ide, cd->bus, cd->drive, "NET-CD", &cd_ops, dev) == -1) { + return -1; + } + + PrintDebug("intialization done\n"); + + return 0; } + + + + +device_register("NET-CD", cd_init)