From: Jack Lange Date: Fri, 5 Jun 2009 22:01:01 +0000 (-0500) Subject: nbd server updates X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=commitdiff_plain;h=2b1dfc3d1281140e4bcc1ea963ae34e5eef9c8b8 nbd server updates --- diff --git a/misc/network_servers/v3_nbd/iso.cc b/misc/network_servers/v3_nbd/iso.cc index 2ca53fa..31faa85 100644 --- a/misc/network_servers/v3_nbd/iso.cc +++ b/misc/network_servers/v3_nbd/iso.cc @@ -26,7 +26,9 @@ iso_image::iso_image(string & filename) : v3_disk(filename){ - this->f = fopen(filename.c_str(), "w+"); + this->f = fopen(filename.c_str(), "r"); + + this->get_capacity(); } iso_image::~iso_image() { @@ -42,16 +44,29 @@ off_t iso_image::get_capacity() { -int iso_image::read(unsigned char * buf, unsigned long long offset, int length) { - - return -1; +unsigned int iso_image::read(unsigned char * buf, off_t offset, int length) { + int total_bytes = length; + int bytes_read = 0; + + fseeko(this->f, offset, SEEK_SET); + + while (bytes_read < total_bytes) { + int tmp_bytes = fread(buf, 1, length, this->f); + + if (tmp_bytes == 0) { + break; + } + bytes_read += tmp_bytes; + } + + return bytes_read; } -int iso_image::write(unsigned char * buf, unsigned long long offset, int length) { +unsigned int iso_image::write(unsigned char * buf, off_t offset, int length) { - return -1; + return 0; } void iso_image::attach() { diff --git a/misc/network_servers/v3_nbd/iso.h b/misc/network_servers/v3_nbd/iso.h index 14d6cd6..54a0cf1 100644 --- a/misc/network_servers/v3_nbd/iso.h +++ b/misc/network_servers/v3_nbd/iso.h @@ -31,8 +31,8 @@ class iso_image : public v3_disk { ~iso_image(); off_t get_capacity(); - int read(unsigned char * buf, unsigned long long offset, int length); - int write(unsigned char * buf, unsigned long long offset, int length); + unsigned int read(unsigned char * buf, off_t offset, int length); + unsigned int write(unsigned char * buf, off_t offset, int length); void attach(); void detach(); diff --git a/misc/network_servers/v3_nbd/raw.cc b/misc/network_servers/v3_nbd/raw.cc index f4d5ef5..668850f 100644 --- a/misc/network_servers/v3_nbd/raw.cc +++ b/misc/network_servers/v3_nbd/raw.cc @@ -22,7 +22,7 @@ raw_disk::raw_disk(string & filename) : v3_disk(filename){ - this->f = fopen(filename.c_str(), "w+"); + this->f = fopen(filename.c_str(), "r+"); } @@ -33,16 +33,16 @@ off_t raw_disk::get_capacity() { -int raw_disk::read(unsigned char * buf, unsigned long long offset, int length) { +unsigned int raw_disk::read(unsigned char * buf, off_t offset, int length) { - return -1; + return 0; } -int raw_disk::write(unsigned char * buf, unsigned long long offset, int length) { +unsigned int raw_disk::write(unsigned char * buf, off_t offset, int length) { - return -1; + return 0; } diff --git a/misc/network_servers/v3_nbd/raw.h b/misc/network_servers/v3_nbd/raw.h index fc4ac4e..246daa2 100644 --- a/misc/network_servers/v3_nbd/raw.h +++ b/misc/network_servers/v3_nbd/raw.h @@ -30,8 +30,8 @@ class raw_disk : public v3_disk { raw_disk(string & filename); off_t get_capacity(); - int read(unsigned char * buf, unsigned long long offset, int length); - int write(unsigned char * buf, unsigned long long offset, int length); + unsigned int read(unsigned char * buf, off_t offset, int length); + unsigned int write(unsigned char * buf, off_t offset, int length); void attach(); void detach(); diff --git a/misc/network_servers/v3_nbd/v3_disk.h b/misc/network_servers/v3_nbd/v3_disk.h index 9b7ce17..ba0d8b8 100644 --- a/misc/network_servers/v3_nbd/v3_disk.h +++ b/misc/network_servers/v3_nbd/v3_disk.h @@ -32,8 +32,8 @@ class v3_disk { virtual ~v3_disk(); virtual off_t get_capacity()=0; - virtual int read(unsigned char * buf, unsigned long long offset, int length)=0; - virtual int write(unsigned char * buf, unsigned long long offset, int length)=0; + virtual unsigned int read(unsigned char * buf, off_t offset, int length)=0; + virtual unsigned int write(unsigned char * buf, off_t offset, int length)=0; virtual void attach()=0; virtual void detach()=0; diff --git a/misc/network_servers/v3_nbd/v3_nbd.cc b/misc/network_servers/v3_nbd/v3_nbd.cc index 90aa71e..c0b3923 100644 --- a/misc/network_servers/v3_nbd/v3_nbd.cc +++ b/misc/network_servers/v3_nbd/v3_nbd.cc @@ -44,6 +44,9 @@ #define NBD_WRITE_CMD 0x2 #define NBD_CAPACITY_CMD 0x3 +#define NBD_STATUS_OK 0x00 +#define NBD_STATUS_ERR 0xff + #define DEFAULT_LOG_FILE "./status.log" #define DEFAULT_CONF_FILE "v3_nbd.ini" @@ -208,6 +211,8 @@ int serv_loop(int serv_sock) { // new connection conn_socket = accept(serv_sock, (struct sockaddr *)&rem_addr, &addr_len); + vtl_debug("New Connection...\n"); + if (conn_socket < 0) { if (errno == EINTR) { continue; @@ -245,8 +250,9 @@ int serv_loop(int serv_sock) { tmp_disk->detach(); - close(tmp_sock); + FD_CLR(tmp_sock, &all_set); + close(tmp_sock); conns.erase(tmp_iter); } else { @@ -261,18 +267,22 @@ int serv_loop(int serv_sock) { // check pending connections for (list::iterator pending_iter = pending_cons.begin(); - pending_iter != pending_cons.end(); - pending_iter++) { + pending_iter != pending_cons.end();) { if (FD_ISSET(*pending_iter, &read_set)) { if (handle_new_connection(*pending_iter) == -1) { // error vtl_debug("Error: Could not connect to disk\n"); + FD_CLR(*pending_iter, &all_set); } - - pending_cons.erase(pending_iter); + list::iterator tmp_iter = pending_iter; + pending_iter++; + + pending_cons.erase(tmp_iter); if (--nready <= 0) break; + } else { + pending_iter++; } } @@ -385,6 +395,8 @@ int handle_disk_request(SOCK conn, v3_disk * disk) { int handle_capacity_request(SOCK conn, v3_disk * disk) { off_t capacity = disk->get_capacity(); + vtl_debug("Returing capacity %d\n", capacity); + return Send(conn, (char *)&capacity, 8, true); } @@ -396,12 +408,74 @@ int handle_capacity_request(SOCK conn, v3_disk * disk) { // 4 bytes : return length // x bytes : data int handle_read_request(SOCK conn, v3_disk * disk) { - return -1; + off_t offset = 0; + unsigned int length = 0; + unsigned char * buf = NULL; + unsigned int ret_len = 0; + unsigned char status = NBD_STATUS_OK; + + vtl_debug("Read Request\n"); + + if (Receive(conn, (char *)&offset, 8, true) <= 0) { + vtl_debug("Error receiving read offset\n"); + return -1; + } + + vtl_debug("Read Offset %d\n", offset); + + if (Receive(conn, (char *)&length, 4, true) <= 0) { + vtl_debug("Error receiving read length\n"); + return -1; + } + + vtl_debug("Read length: %d\n", length); + + buf = new unsigned char[length]; + + ret_len = disk->read(buf, offset, length); + + vtl_debug("Read %d bytes from source disk\n", ret_len); + + if (ret_len == 0) { + vtl_debug("Read Error\n"); + status = NBD_STATUS_ERR; + } + + vtl_debug("Sending Status byte (%d)\n", status); + + if (Send(conn, (char *)&status, 1, true) <= 0) { + vtl_debug("Error Sending Read Status\n"); + return -1; + } + + vtl_debug("Sending Ret Len: %d\n", ret_len); + + if (Send(conn, (char *)&ret_len, 4, true) <= 0) { + vtl_debug("Error Sending Read Length\n"); + return -1; + } + + + + if (ret_len > 0) { + vtl_debug("Sending Data\n"); + if (Send(conn, (char *)buf, ret_len, true) <= 0) { + vtl_debug("Error sending Read Data\n"); + return -1; + } + } + + vtl_debug("Read Complete\n"); + + delete buf; + + return 0; } // receive: // 8 bytes : offset // 4 bytes : length +// x bytes : data // send : // 1 bytes : status int handle_write_request(SOCK conn, v3_disk * disk) { @@ -421,6 +495,8 @@ int handle_new_connection(SOCK new_conn) { GetLine(new_conn, input); + vtl_debug("New Connection: %s\n", input.c_str()); + { istringstream is(input, istringstream::in); is >> key_str >> tag_str; @@ -466,6 +542,13 @@ int config_nbd(string conf_file_name) { return -1; } + if (config_map.count(LOGFILE_TAG) == 0) { + config_map[LOGFILE_TAG] = DEFAULT_LOG_FILE; + } + + vtl_debug_init(config_map[LOGFILE_TAG], enable_debug); + + if (config_map.count(PORT_TAG) > 0) { server_port = atoi(config_map[PORT_TAG].c_str()); } else { @@ -492,11 +575,6 @@ int config_nbd(string conf_file_name) { return -1; } - if (config_map.count(LOGFILE_TAG) == 0) { - config_map[LOGFILE_TAG] = DEFAULT_LOG_FILE; - } - - vtl_debug_init(config_map[LOGFILE_TAG], enable_debug); return 0; } @@ -521,6 +599,7 @@ void setup_disk(string disk_tag, config_t &config_map) { if (type == "RAW") { disk = new raw_disk(config_map[file_tag]); } else if (type == "ISO") { + vtl_debug("Setting up ISO\n"); disk = new iso_image(config_map[file_tag]); }