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.


updates to the nbd server
[palacios.git] / misc / network_servers / v3_nbd / v3_nbd.cc
index 1b1099d..4a54726 100644 (file)
  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
  */
 
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <stdio.h>
 #include <sstream>
 
+#ifdef linux 
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+#elif defined(WIN32) && !defined(__CYGWIN__)
 
+#endif
+
+#include "vtl.h"
+
+
+#define DEFAULT_LOG_FILE "./status.log"
+#define DEFAULT_CONF_FILE "v3_nbd.ini"
+
+
+#define DEFAULT_PORT 9500
+#define MAX_STRING_SIZE 1024
+#define MAX_DISKS 32
+
+#define LOGFILE_TAG "logfile"
+#define PORT_TAG  "port"
+#define DISKS_TAG "disks"
+
+// Turn on 64 bit file offset support (see 'man fseeko')
+#define _FILE_OFFSET_BITS 64
 
-nbd_config_t g_nbd_conf;
 
 using namespace std;
 //using namespace __gnu_cxx;
 
 
+typedef enum {INVALID, ISO, RAW} disk_type_t;
+
+struct disk_info {
+    string filename;
+    string tag;
+    disk_type_t type;
+
+    FILE * disk_file;
+};
+
+
+
+struct eqsock {
+    bool operator()(const SOCK sock1, const SOCK sock2) const {
+       return sock1 == sock2;
+    }
+};
+
+
+// Server Port that we'll listen on
+int server_port;
+
+// List of disks being served 
+// eqstr from vtl (config.h)
+map<const string, struct disk_info *, eqstr> disks;
+
+
+// List of open connections
+map<SOCK, struct disk_info *, eqsock> conns;
+
+// Enable Debugging
+static const int enable_debug = 1;
+
+
+void usage();
+int config_nbd(string conf_file_name);
+int serv_loop(int serv_sock);
+void setup_disk(string disk_tag, config_t &config_map);
 
-config_t g_config;
 
 int __main (int argc, char ** argv);
 
+disk_type_t get_disk_type(const string type_str) {
+
+    if (type_str == "ISO") {
+       return ISO;
+    } else if (type_str == "RAW") {
+       return RAW;
+    } 
+
+    return INVALID;
+}
+
+
 #ifdef linux
 
 int main(int argc, char ** argv) {
@@ -48,9 +124,7 @@ void main() {
 
 int __main (int argc, char ** argv) {
   string config_file;
-  SOCK vnet_sock = 0;
-  struct vnet_config vnet_info;
-  iface_t * iface;
+  SOCK serv_sock;
   if (argc > 2) {
     usage();
     exit(0);
@@ -59,78 +133,93 @@ int __main (int argc, char ** argv) {
   if (argc == 2) {
     config_file = string(argv[1]);
   } else {
-    config_file = VIDS_CONF_FILE;
+    config_file = DEFAULT_CONF_FILE;
   }
 
-
-  int * foo;
-  int num_ports = GetOpenUdpPorts(&foo);
-  int i;
-  for (i = 0; i < num_ports; i++) {
-    printf("port %d open\n", foo[i]);
-  }
-  
-
-  //  g_conf.log_file = "./vids.log";
-
-  if (config_vids(config_file) == -1) {
+  if (config_nbd(config_file) == -1) {
     cerr << "Configuration Error" << endl;
     exit(-1);
   }
 
-  // JRL DEBUG
-  debug_init(g_config[LOGFILE_TAG].c_str());
-  JRLDBG("testing...\n");
+  // setup network sockets
+  serv_sock = CreateAndSetupTcpSocket();
+  
+  if (serv_sock == -1) {
+      cerr << "Could not create server socket, exiting..." << endl;
+      exit(-1);
+  }
 
+  if (BindSocket(serv_sock, server_port) == -1) {
+      cerr << "Could not bind socket to port: " << server_port << endl;
+      exit(-1);
+  }
 
+  if (ListenSocket(serv_sock) == -1) {
+      cerr << "Could not listen on server socket (port=" << server_port << ")" << endl;
+      exit(-1);
+  }
 
-  // Configure pcap filter...
 
-  vids_loop(iface, vnet_sock, &vnet_info);
+  vtl_debug("Starting Server Loop\n");
+  serv_loop(serv_sock);
 
   return 0;
 }
 
 
 #ifdef linux
-int vids_loop(iface_t * iface, SOCK vnet_sock, struct vnet_config * vnet_info) {
-  fd_set all_set, read_set;
-  int max_fd = -1;
-  RawEthernetPacket pkt;
+int serv_loop(int serv_sock) {
+    fd_set all_set, read_set;
+    int max_fd = -1;
+    RawEthernetPacket pkt;
 
-  FD_ZERO(&all_set);
-  FD_SET(vnet_sock, &all_set);
-  max_fd = vnet_sock;
+    FD_ZERO(&all_set);
+    FD_SET(serv_sock, &all_set);
+    max_fd = serv_sock;
 
 
-  while (1) {
-    int nready = 0;
-    read_set = all_set;
-    nready = select(max_fd + 1, &read_set, NULL, NULL, NULL);
+    while (1) {
+       int nready = 0;
+       read_set = all_set;
+       nready = select(max_fd + 1, &read_set, NULL, NULL, NULL);
     
-    
-    if (nready == -1) {
-      if (errno == EINTR) {
-       continue;
-      } else {
-       perror("Select returned error: ");
-       break;
-      }
-    }
+       if (nready == -1) {
+           if (errno == EINTR) {
+               continue;
+           } else {
+               vtl_debug("Select returned error\n");
+               exit(-1);
+           }
+       }
     
 
-    if (FD_ISSET(vnet_sock, &read_set)) {
-      //vnet_recv();
-      
+       if (FD_ISSET(serv_sock, &read_set)) {
+           SOCK conn_socket;
+           struct sockaddr_in rem_addr;
+           socklen_t addr_len = sizeof(struct sockaddr_in);
+           // new connection
+           conn_socket = accept(serv_sock, (struct sockaddr *)&rem_addr, &addr_len);
 
+           if (conn_socket < 0) {
+               if (errno == EINTR) {
+                   continue;
+               } else {
+                   vtl_debug("Accept returned error\n");
+                   exit(-1);
+               }
+           }
+
+           // configure socket
+
+       }
     }
-  }
 
-  return 0;
+    return 0;
 }
 
 #elif WIN32
-int vids_loop(iface_t * iface, SOCK vnet_sock, struct vnet_config * vnet_info) {
+int serv_loop(iface_t * iface, SOCK vnet_sock, struct vnet_config * vnet_info) {
   int ret;
   RawEthernetPacket pkt;
   WSANETWORKEVENTS net_events;
@@ -160,7 +249,6 @@ int vids_loop(iface_t * iface, SOCK vnet_sock, struct vnet_config * vnet_info) {
       }
       if (net_events.lNetworkEvents & FD_READ) {
        
-       JRLDBG("Receied VNET Packet\n");
        // we received data
        
        if (vnet_info->link_type == TCP_LINK) {
@@ -187,102 +275,82 @@ int vids_loop(iface_t * iface, SOCK vnet_sock, struct vnet_config * vnet_info) {
 
 
 
-int config_vids(string conf_file_name) {
-  if (read_config(conf_file_name, &g_config) != 0) {
-    return -1;
-  }
-
-  if (g_config.count(VIDS_SERVER_TAG) > 0) {
-    g_vids_conf.server_addr = ToIPAddress(g_config[VIDS_SERVER_TAG].c_str());
-  } else {
-    printf("Must specify VIDS server address\n");
-    return -1;
-  }
-
-  if (g_config.count(VIDS_SERVER_PORT_TAG) > 0) {
-    g_vids_conf.server_port = atoi(g_config[VIDS_SERVER_PORT_TAG].c_str());
-  } else {
-    printf("Must specify VIDS server port\n");
-    return -1;
-  }
+int config_nbd(string conf_file_name) {
+    config_t config_map;
+    
+    if (read_config(conf_file_name, &config_map) != 0) {
+       cerr << "Could not read config file..." << endl;
+       return -1;
+    }
 
-  if (g_config.count(TCP_PORTS_TAG) > 0) {
-    istringstream port_stream(g_config[TCP_PORTS_TAG], istringstream::in);
-    int port;
-    int i = 0;
+    if (config_map.count(PORT_TAG) > 0) {
+       server_port = atoi(config_map[PORT_TAG].c_str());
+    } else {
+       server_port = DEFAULT_PORT;
+    }
+       
+    if (config_map.count(DISKS_TAG) > 0) {
+       istringstream disk_stream(config_map[DISKS_TAG], istringstream::in);
+       string disk_tag;
+       int i = 0;
+       
+       while (disk_stream >> disk_tag) {
+
+           if (i >= MAX_DISKS) {
+               cerr << "You specified too many disks, truncating..." << endl;
+               break;
+           }
+           
+           setup_disk(disk_tag, config_map);
+           i++;
+       }       
+    } else {
+       cerr << "Must specify a set of disks" << endl;
+       return -1;
+    }
     
-    while (port_stream >> port) {
-      if (i >= MAX_PORTS) {
-       cerr << "You specified too many ports to forward, truncating..." << endl;
-       break;
-      }
-      
-      g_vids_conf.tcp_ports[i] = port;      
-      i++;
+    if (config_map.count(LOGFILE_TAG) == 0) {
+       config_map[LOGFILE_TAG] = DEFAULT_LOG_FILE;
     }
+    
+    vtl_debug_init(config_map[LOGFILE_TAG], enable_debug);
 
-    g_vids_conf.num_tcp_ports = i;
-  }
+    return 0;
+}
 
+void setup_disk(string disk_tag, config_t &config_map) {
+    string file_tag = disk_tag +  ".file";
+    string type_tag = disk_tag + ".type";
+    struct disk_info * disk = (struct disk_info *)malloc(sizeof(struct disk_info));
 
+    cout << "Setting up " << disk_tag.c_str() << endl;
 
-  if (g_config.count(VIRTUAL_MAC_TAG) > 0) {
-   string_to_mac(g_config[VIRTUAL_MAC_TAG].c_str(), g_vids_conf.virtual_mac);
-  }
+    if ((config_map.count(file_tag) == 0) && 
+       (config_map.count(type_tag) == 0)) {
+       cerr << "Missing Disk configuration directive for " << disk_tag << endl;
+    }
 
-  if (g_config.count(LOGFILE_TAG) == 0) {
-    g_config[LOGFILE_TAG] = DEFAULT_LOG_FILE;
-  }
+    disk->tag = disk_tag;
+    disk->filename = config_map[file_tag];
+    disk->type = get_disk_type(config_map[type_tag]);
 
-  if (GetLocalMacAddress(g_config[INTERFACE_TAG], g_vids_conf.local_mac) == -1) {
-    cerr << "Could not get local mac address" << endl;
-    return -1;
-  }
+    if (disk->type == RAW) {
+       disk->disk_file = fopen(disk->filename.c_str(), "w+");
+    } else if (disk->type == ISO) {
+       disk->disk_file = fopen(disk->filename.c_str(), "r");
+    }
 
+    disks[disk->tag] = disk;
 
-  return 0;
+    return;
 }
 
 
-int read_config(string conf_file_name) {
-  fstream conf_file(conf_file_name.c_str(), ios::in);
-  char line[MAX_STRING_SIZE];
 
-  while ((conf_file.getline(line, MAX_STRING_SIZE))) {
-    string conf_line = line;
-    string tag;
-    string value;
-    int offset, ltrim_index, rtrim_index;
 
-    if (conf_line[0] == '#') {
-      continue;
-    }
-
-    offset = conf_line.find(":", 0);
-    tag = conf_line.substr(0,offset);
-
-    // kill white space
-    istringstream tag_stream(tag, istringstream::in);
-    tag_stream >> tag;
-
-    if (tag.empty()) {
-      continue;
-    }
-
-    // basic whitespace trimming, we assume that the config handlers will deal with 
-    // tokenizing and further formatting
-    value = conf_line.substr(offset + 1, conf_line.length() - offset);
-    ltrim_index = value.find_first_not_of(" \t");
-    rtrim_index = value.find_last_not_of(" \t");
-    value = value.substr(ltrim_index, (rtrim_index + 1) - ltrim_index);
-
-    g_config[tag] = value;
-  }
-  return 0;
-}
 
 
 void usage() {
-  cout << "Usage: vids [config_file]" << endl;
+  cout << "Usage: v3_nbd [config_file]" << endl;
   return;
 }