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.


Fix a virtio ring buffer free run index overflow bug
[palacios.git] / palacios / src / palacios / vmm_vnet.c
index 02f5182..188f730 100644 (file)
  * http://www.v3vee.org
  *
  * Copyright (c) 2009, Lei Xia <lxia@northwestern.edu> 
- * Copyright (c) 2009, Yuan Tang <ytang@northwestern.edu> 
- * Copyright (c) 2009, Jack Lange <jarusl@cs.northwestern.edu> 
- * Copyright (c) 2009, Peter Dinda <pdinda@northwestern.edu>
+ * Copyright (c) 2009, Yuan Tang <ytang@northwestern.edu>  
+ * Copyright (c) 2009, Zheng Cui <czheng@unm.edu>
  * Copyright (c) 2009, The V3VEE Project <http://www.v3vee.org> 
  * All rights reserved.
  *
  * Author: Lei Xia <lxia@northwestern.edu>
  *        Yuan Tang <ytang@northwestern.edu>
- *        Jack Lange <jarusl@cs.northwestern.edu> 
- *        Peter Dinda <pdinda@northwestern.edu
+ *       Zheng Cui <czheng@unm.edu>
  *
  * This is free software.  You are permitted to use,
  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
  */
  
 #include <palacios/vmm_vnet.h>
+#include <palacios/vmm_hypercall.h>
+#include <palacios/vm_guest_mem.h>
 
-static const char any_type_str[] = "any";
-static const char not_type_str[] = "not";
-static const char none_type_str[] = "none";
-static const char empty_type_str[] = "empty";
-
-static const char link_interface_str[] = "INTERFACE";
-static const char link_edge_str[] = "EDGE";
-static const char link_any_str[] = "ANY";
-
-static const char link_prot_tcp_str[] = "tcp";
-static const char link_prot_udp_str[] = "udp";
-
-typedef enum {MAC_ANY, MAC_NOT, MAC_NONE, MAC_EMPTY} mac_type_t; //for 'src_mac_qual' and 'dst_mac_qual'
-typedef enum {LINK_INTERFACE, LINK_EDGE, LINK_ANY} link_type_t; //for 'type' and 'src_type' in struct routing
-typedef enum {TCP_TYPE, UDP_TYPE} link_prot_type_t;
-
-static const uint_t hash_key_size = 16;
-static SOCK g_udp_sockfd;
-static struct gen_queue * g_inpkt_q;
-static bool use_tcp = false;
-static uint_t vnet_udp_port = 1022;
-
-struct raw_ethernet_pkt {
-  int  size;
-  int type; // vm or link type:  INTERFACE|EDGE
-  char  data[ETHERNET_PACKET_LEN];
-};
-
-//static char *vnet_version = "0.9";
-//static int vnet_server = 0;
-
-
-#define MAX_LINKS 1
-#define MAX_ROUTES 1
-#define MAX_DEVICES 16
-
-static struct topology g_links[MAX_LINKS];
-static int g_num_links; //The current number of links
-static int g_first_link;
-static int g_last_link;
-
-static struct routing g_routes[MAX_ROUTES];
-static int g_num_routes; //The current number of routes
-static int g_first_route;
-static int g_last_route;
+#ifndef CONFIG_DEBUG_VNET
+#undef PrintDebug
+#define PrintDebug(fmt, args...)
+#endif
 
-static struct device_list g_devices[MAX_DEVICES];
-static int g_num_devices;
-static int g_first_device;
-static int g_last_device;
 
+struct ethernet_pkt {
+    uint32_t size; //size of data
+    uint16_t type;
+    struct udp_link_header ext_hdr; //possible externel header to applied to data before sent
+    char data[ETHERNET_PACKET_LEN];
+}__attribute__((packed));
 
-static void print_packet(char *pkt, int size) {
-    PrintDebug("Vnet: print_data_packet: size: %d\n", size);
-    v3_hexdump(pkt, size, NULL, 0);
-}
 
-#if 0
-static void print_packet_addr(char *pkt) {
-    PrintDebug("Vnet: print_packet_destination_addr: ");
-    v3_hexdump(pkt + 8, 6, NULL, 0);
-    
-    PrintDebug("Vnet: print_packet_source_addr: ");
-    v3_hexdump(pkt + 14, 6, NULL, 0);
-}
+// 14 (ethernet frame) + 20 bytes
+struct in_pkt_header {
+    uchar_t ethernetdest[6];
+    uchar_t ethernetsrc[6];
+    uchar_t ethernettype[2]; //layer 3 protocol type
+    char ip[20];
+}__attribute__((packed));
 
-static void print_device_addr(char *ethaddr) {
-    PrintDebug("Vnet: print_device_addr: ");
-    v3_hexdump(ethaddr, 6, NULL, 0);
-} 
-#endif
+#define VNET_INITAB_HCALL 0xca00  // inital hypercall id
 
+#define MAX_LINKS 10
+#define MAX_ROUTES 10
+#define HASH_KEY_LEN 16
+#define MIN_CACHE_SIZE 100
+static const uint_t hash_key_size = 16;
 
-//network connection functions 
-static inline void raw_ethernet_packet_init(struct raw_ethernet_pkt * pt, const char * data, const size_t size) {
-    pt->size = size;
-    memcpy(pt->data, data, size);
-}
+struct link_table {
+    struct link_entry * links[MAX_LINKS];
+    uint16_t size;
+}__attribute__((packed));
 
+struct routing_table {
+    struct routing_entry * routes[MAX_ROUTES];
+    uint16_t size;
+}__attribute__((packed));
 
+static struct link_table g_links;
+static struct routing_table g_routes;
+static struct gen_queue * g_inpkt_q;
 
 /* Hash key format:
  * 0-5:     src_eth_addr
@@ -120,12 +83,33 @@ struct route_cache_entry {
     int * matches; 
 };
 
-#define HASH_KEY_LEN 16
-#define MIN_CACHE_SIZE 100
+// the route cache
+static struct hashtable * g_route_cache; 
 
+static uint16_t ip_xsum(struct ip_header *ip_hdr, int hdr_len){
+    long sum = 0;
 
-//Header of the route cache
-static struct hashtable * g_route_cache; 
+    uint16_t *p = (uint16_t*) ip_hdr;
+    while(hdr_len > 1){
+        sum += *(p++);
+        if(sum & 0x80000000)
+            sum = (sum & 0xFFFF) + (sum >> 16);
+            hdr_len -= 2;
+        }
+    if(hdr_len) 
+        sum += (uint16_t) *(uchar_t *)p;
+          
+    while(sum>>16)
+        sum = (sum & 0xFFFF) + (sum >> 16);
+
+    return (uint16_t)~sum;
+}
+
+static inline void ethernet_packet_init(struct ethernet_pkt *pt, uchar_t *data, const size_t size) {
+    pt->size = size;
+    memcpy(pt->data, data, size);
+}
 
 static uint_t hash_from_key_fn(addr_t hashkey) {    
     uint8_t * key = (uint8_t *)hashkey;
@@ -141,7 +125,7 @@ static int hash_key_equal(addr_t key1, addr_t key2) {
 static int init_route_cache() {
     g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
 
-    if (g_route_cache == NULL){
+    if (g_route_cache == NULL) {
         PrintError("Vnet: Route Cache Initiate Failurely\n");
         return -1;
     }
@@ -149,7 +133,11 @@ static int init_route_cache() {
     return 0;
 }
 
-static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
+static void make_hash_key(route_hashkey_t hashkey,
+                         char src_addr[6],
+                         char dest_addr[6],
+                         char src_type,
+                         int src_index) {
     int j;
 
     for (j = 0; j < 6; j++) {
@@ -161,12 +149,13 @@ static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_a
 
     *(int *)(hashkey + 12) = src_index;
 }
+
 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * matches) {
     struct route_cache_entry * new_entry = NULL;
     int i;
     
     new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
-    if (new_entry == NULL){
+    if (new_entry == NULL) {
        PrintError("Vnet: Malloc fails\n");
        return -1;
     }
@@ -175,7 +164,7 @@ static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int *
 
     new_entry->matches = (int *)V3_Malloc(num_matched_r * sizeof(int));
     
-    if (new_entry->matches == NULL){
+    if (new_entry->matches == NULL) {
        PrintError("Vnet: Malloc fails\n");
        return -1;
     }
@@ -184,8 +173,8 @@ static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int *
        new_entry->matches[i] = matches[i];
     }
     
-    //here, when v3_htable_insert return 0, it means insert fails
-    if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0){
+    // here, when v3_htable_insert return 0, it means insert fails
+    if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0) {
        PrintError("Vnet: Insert new route entry to cache failed\n");
        V3_Free(new_entry->matches);
        V3_Free(new_entry);
@@ -199,7 +188,7 @@ static int clear_hash_cache() {
                
     g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
     
-    if (g_route_cache == NULL){
+    if (g_route_cache == NULL) {
         PrintError("Vnet: Route Cache Create Failurely\n");
         return -1;
     }
@@ -208,9 +197,9 @@ static int clear_hash_cache() {
 }
 
 static int look_into_cache(route_hashkey_t hashkey, int * matches) {
+    struct route_cache_entry * found = NULL;
     int n_matches = -1;
     int i = 0;
-    struct route_cache_entry * found = NULL;
     
     found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
    
@@ -226,7 +215,12 @@ static int look_into_cache(route_hashkey_t hashkey, int * matches) {
 }
 
 
+#ifdef CONFIG_DEBUG_VNET
 
+static void print_packet(uchar_t *pkt, int size) {
+    PrintDebug("Vnet: print_data_packet: size: %d\n", size);
+    v3_hexdump(pkt, size, NULL, 0);
+}
 
 static inline uint8_t hex_nybble_to_nybble(const uint8_t hexnybble) {
     uint8_t x = toupper(hexnybble);
@@ -243,8 +237,7 @@ static inline uint8_t hex_byte_to_byte(const uint8_t hexbyte[2]) {
            (hex_nybble_to_nybble(hexbyte[1]) & 0xf));
 }
 
-
-static inline void string_to_mac(const char * str, uint8_t mac[6]) {
+static inline void string_to_mac(const char *str, uint8_t mac[6]) {
     int k;
 
     for (k = 0; k < 6; k++) {
@@ -252,220 +245,181 @@ static inline void string_to_mac(const char * str, uint8_t mac[6]) {
     }
 }
 
-static inline void mac_to_string(int mac[6], char * buf) {
-    snprintf(buf, 20, "%x:%x:%x:%x:%x:%x", 
+static inline void mac_to_string(char mac[6], char * buf) {
+    snprintf(buf, 20, "%02x:%02x:%02x:%02x:%02x:%02x", 
             mac[0], mac[1], mac[2],
             mac[3], mac[4], mac[5]);
 }
 
-#if 0
-static void ip_to_string(uint32_t addr, char * buf) {
-    uint32_t addr_st;
-    char * tmp_str;
-    
-    addr_st = v3_htonl(addr);
-    tmp_str = v3_inet_ntoa(addr_st);
+static void dump_routes(struct routing_entry **route_tables) {
+    char dest_str[18];
+    char src_str[18];
+    struct routing_entry *route = NULL;
+    int i;
+
+    PrintDebug("\nVnet: route table dump start =====\n");
+
+    for(i = 0; i < MAX_ROUTES; i++) {
+        if (route_tables[i] != NULL){
+            route = route_tables[i];
     
-    memcpy(buf, tmp_str, strlen(tmp_str));
+            mac_to_string(route->src_mac, src_str);  
+            mac_to_string(route->dest_mac, dest_str);
+
+            PrintDebug("route: %d\n", i);
+            PrintDebug("SRC(%s), DEST(%s), src_mac_qual(%d), dst_mac_qual(%d)\n", src_str, dest_str, route->src_mac_qual, route->dest_mac_qual);
+            PrintDebug("Src_Link(%d), src_type(%d), dst_link(%d), dst_type(%d)\n\n", route->src_link_idx, route->src_type, route->link_idx, route->link_type);
+        }
+    }
+
+    PrintDebug("\nVnet: route table dump end =====\n");
 }
-#endif
 
-int find_link_by_fd(SOCK sock) {
+static void dump_dom0_routes(struct routing_entry routes[], int size) {
+    char dest_str[18];
+    char src_str[18];
+    struct routing_entry *route = NULL;
     int i;
 
-    FOREACH_LINK(i, g_links, g_first_link) {
-       if (g_links[i].link_sock == sock) {
-           return i;
-       }
+    PrintDebug("\nVnet: route table from dom0 guest =====\n");
+
+    for(i = 0; i <size; i++) {
+        route = &routes[i];
+        mac_to_string(route->src_mac, src_str);  
+        mac_to_string(route->dest_mac, dest_str);
+        PrintDebug("route: %d\n", i);
+        PrintDebug("SRC(%s), DEST(%s), src_mac_qual(%d), dst_mac_qual(%d)\n", src_str, dest_str, route->src_mac_qual, route->dest_mac_qual);
+        PrintDebug("Src_Link(%d), src_type(%d), dst_link(%d), dst_type(%d)\n\n", route->src_link_idx, route->src_type, route->link_idx, route->link_type);
     }
-    
-    return -1;
+
+    PrintDebug("\nVnet: route table dom0 guest end =====\n");
 }
 
-int vnet_add_link_entry(unsigned long dest, int type, int data_port,  SOCK fd) {
+static void dump_dom0_links(struct vnet_if_link links[], int size) {
+    struct vnet_if_link *link = NULL;
     int i;
-    
-    for (i = 0; i < MAX_LINKS; i++) {
-       if (g_links[i].use == 0) {
-           g_links[i].dest = dest;
-           g_links[i].type = type;
-           g_links[i].link_sock = fd;
-           g_links[i].remote_port = data_port;
-           g_links[i].use = 1;
-
-           if (g_first_link == -1) {
-               g_first_link = i;
-           }
 
-           g_links[i].prev = g_last_link;
-           g_links[i].next = -1;
-           
-           if (g_last_link != -1) {
-               g_links[g_last_link].next = i;
-           }
-           
-           g_last_link = i;
-           
-           g_num_links++;
-           
-           return i;
-       }
+    PrintDebug("\nVnet: link table from dom0 guest =====\n");
+
+    for(i = 0; i <size; i++) {
+        link = &links[i];
+        PrintDebug("link: %d\n", i);
+        PrintDebug("dest_ip(%ld), dst_port(%d), prot_type(%d)\n", link->dest_ip, link->dest_port, link->pro_type);
+        PrintDebug("vnet_header:\n");
+        v3_hexdump(&link->vnet_header, sizeof(struct udp_link_header), NULL, 0);
     }
-    
-    return -1;
-}
 
+    PrintDebug("\nVnet: link table dom0 guest end =====\n");
+}
 
-int add_sock(struct sock_list *socks, int  len, int *first_sock, int *last_sock, SOCK fd) {
-    int i;
+#endif
 
-    for (i = 0; i < len; i++) {
-       if (socks[i].sock == -1) {
-           socks[i].sock = fd;
-           
-           if (*first_sock == -1) {
-               *first_sock = i;
-           }
-           
-           socks[i].prev = *last_sock;
-           socks[i].next = -1;
-           
-           if (*last_sock != -1) {
-               socks[*last_sock].next = i;
-           }
-           
-           *last_sock = i;
+static int __add_link_entry(struct link_entry * link) {
+    int idx;
+    
+    for (idx = 0; idx < MAX_LINKS; idx++) {
+       if (g_links.links[idx] == NULL) {
+           g_links.links[idx] = link;
+           g_links.size++;
            
-           return i;
+           return idx;
        }
     }
+
+    PrintError("No available Link entry\n");
     return -1;
 }
 
-
-int vnet_add_route_entry(char src_mac[6], char dest_mac[6], int src_mac_qual, int dest_mac_qual, int dest, int type, int src, int src_type) {
-    int i;
+static int __add_route_entry(struct routing_entry * route) {
+    int idx;
     
-    for(i = 0; i < MAX_ROUTES; i++) {
-       if (g_routes[i].use == 0) {
-           
-           if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
-               memcpy(g_routes[i].src_mac, src_mac, 6);
-           } else {
-               memset(g_routes[i].src_mac, 0, 6);
-           }
-           
-           if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
-               memcpy(g_routes[i].dest_mac, dest_mac, 6);
-           } else {
-               memset(g_routes[i].dest_mac, 0, 6);
-           }
-           
-           g_routes[i].src_mac_qual = src_mac_qual;
-           g_routes[i].dest_mac_qual = dest_mac_qual;
-           g_routes[i].dest = dest;
-           g_routes[i].type = type;
-           g_routes[i].src = src;
-           g_routes[i].src_type = src_type;
-           g_routes[i].use = 1;
-           
-           if (g_first_route == -1) {
-               g_first_route = i;
-           }
-           
-           g_routes[i].prev = g_last_route;
-           g_routes[i].next = -1;
-           
-           if (g_last_route != -1) {
-               g_routes[g_last_route].next = i;
-           }
+    for (idx = 0; idx < MAX_ROUTES; idx++) {
+       if (g_routes.routes[idx] == NULL) {
+           g_routes.routes[idx] = route;
+           g_routes.size++;
            
-           g_last_route = i;
-           
-           g_num_routes++;
-           
-           return i;
+           return idx;
        }
     }
-    
-    clear_hash_cache();
-    
-    return -1;
-}
 
-
-static int find_link_entry(unsigned long dest, int type) {
-    int i;
-
-    FOREACH_LINK(i, g_links, g_first_link) {
-       if ((g_links[i].dest == dest) && 
-           ((type == -1) || (g_links[i].type == type)) ) {
-           return i;
-       }
-    } 
-    
+    PrintError("No available route entry\n");
     return -1;
 }
 
-static int delete_link_entry(int index) {
-    int next_i;
-    int prev_i;
-  
-    if (g_links[index].use == 0) {
-       return -1;
-    }
-
-    g_links[index].dest = 0;
-    g_links[index].type = 0;
-    g_links[index].link_sock = -1;
-    g_links[index].use = 0;
 
-    prev_i = g_links[index].prev;
-    next_i = g_links[index].next;
+int vnet_add_route_entry(char src_mac[6],
+                               char dest_mac[6],
+                               int src_mac_qual,
+                               int dest_mac_qual,
+                               int link_idx,
+                               link_type_t link_type,
+                               int src_link_idx,
+                               link_type_t src_link_type) {
+    struct routing_entry * new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry));
+    int idx = -1;
 
-    if (prev_i != -1) {
-       g_links[prev_i].next = g_links[index].next;
-    }    
+    memset(new_route, 0, sizeof(struct routing_entry));
 
-    if (next_i != -1) {
-       g_links[next_i].prev = g_links[index].prev;
+    if ((src_mac_qual != MAC_ANY)) {
+        memcpy(new_route->src_mac, src_mac, 6);
     }
-    
-    if (g_first_link == index) {
-       g_first_link = g_links[index].next;
-    }    
-
-    if (g_last_link == index) {
-       g_last_link = g_links[index].prev;
-    }    
+           
+    if ((dest_mac_qual != MAC_ANY)) {
+        memcpy(new_route->dest_mac, dest_mac, 6);
+    }
+           
+    new_route->src_mac_qual = src_mac_qual;
+    new_route->dest_mac_qual = dest_mac_qual;
+    new_route->link_idx= link_idx;
+    new_route->link_type = link_type;
+    new_route->src_link_idx = src_link_idx;
+    new_route->src_type = src_link_type;
 
-    g_links[index].next = -1;
-    g_links[index].prev = -1;
+    if ((idx = __add_route_entry(new_route)) == -1) {
+       PrintError("Could not add route entry\n");
+        return -1;
+    }
     
-    g_num_links--;
+    clear_hash_cache();
     
-    return 0;
+    return idx;
 }
 
-int vnet_delete_link_entry_by_addr(unsigned long dest, int type) {
-    int index = find_link_entry(dest, type);
+#if 0
+static void * __delete_link_entry(int index) {
+    struct link_entry * link = NULL;
+    void * ret = NULL;
+    link_type_t type;
   
-    if (index == -1) {
-       return -1;
+    if ((index >= MAX_LINKS) || (g_links.links[index] == NULL)) {
+       return NULL;
     }
 
-    return delete_link_entry(index);
-}
+    link = g_links.links[index];
+    type = g_links.links[index]->type;
 
+    if (type == LINK_INTERFACE) {
+       ret = (void *)g_links.links[index]->dst_dev;
+    } else if (type == LINK_EDGE) {
+       ret = (void *)g_links.links[index]->dst_link;
+    }
+
+    g_links.links[index] = NULL;
+    g_links.size--;
+
+    V3_Free(link);
+       
+    return ret;
+}
 
 static int find_route_entry(char src_mac[6], 
                            char dest_mac[6], 
                            int src_mac_qual, 
                            int dest_mac_qual, 
-                           int dest, 
-                           int type, 
+                           int link_idx, 
+                           link_type_t link_type, 
                            int src, 
-                           int src_type) {
+                           link_type_t src_type) {
     int i;
     char temp_src_mac[6];
     char temp_dest_mac[6];
@@ -482,450 +436,177 @@ static int find_route_entry(char src_mac[6],
        memset(temp_dest_mac, 0, 6);
     }
     
-    FOREACH_LINK(i, g_routes, g_first_route) {
-       if ( (memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) && 
-            (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
-            (g_routes[i].src_mac_qual == src_mac_qual) &&
-            (g_routes[i].dest_mac_qual == dest_mac_qual)  &&
-            ( (type == -1) || 
-              ( (type == g_routes[i].type) && (g_routes[i].dest == dest)) ) &&
-            ( (src_type == -1) || 
-              ( (src_type == g_routes[i].src_type) && (g_routes[i].src == src)) ) ) {
-           return i;
-       }
-    } 
+    for (i = 0; i < MAX_ROUTES; i++) {
+       if (g_routes.routes[i] != NULL) {
+           if ((memcmp(temp_src_mac, g_routes.routes[i]->src_mac, 6) == 0) && 
+               (memcmp(temp_dest_mac, g_routes.routes[i]->dest_mac, 6) == 0) &&
+               (g_routes.routes[i]->src_mac_qual == src_mac_qual) &&
+               (g_routes.routes[i]->dest_mac_qual == dest_mac_qual)  &&
+               ( (link_type == LINK_ANY) || 
+                 ((link_type == g_routes.routes[i]->link_type) && (g_routes.routes[i]->link_idx == link_idx))) &&
+               ( (src_type == LINK_ANY) || 
+                 ((src_type == g_routes.routes[i]->src_type) && (g_routes.routes[i]->src_link_idx == src)))) {
+               return i;
+           }
+        } 
+     }
     
     return -1;
 }
 
-static int delete_route_entry(int index) {
-    int next_i;
-    int prev_i;
-
-    memset(g_routes[index].src_mac, 0, 6);
-    memset(g_routes[index].dest_mac, 0, 6);
+static int __delete_route_entry(int index) {
+    struct routing_entry * route;
 
-    g_routes[index].dest = 0;
-    g_routes[index].src = 0;
-    g_routes[index].src_mac_qual = 0;
-    g_routes[index].dest_mac_qual = 0;
-    g_routes[index].type = -1;
-    g_routes[index].src_type = -1;
-    g_routes[index].use = 0;
-
-    prev_i = g_routes[index].prev;
-    next_i = g_routes[index].next;
-  
-    if (prev_i != -1) {
-       g_routes[prev_i].next = g_routes[index].next;
-    }
-    
-    if (next_i != -1) {
-       g_routes[next_i].prev = g_routes[index].prev;
+    if ((index >= MAX_ROUTES) || (g_routes.routes[index] == NULL)) {
+       PrintDebug("VNET: wrong index in delete route entry %d\n", index);
+       return -1;
     }
-    
-    if (g_first_route == index) {
-       g_first_route = g_routes[index].next;
-    }    
 
-    if (g_last_route == index) {
-       g_last_route = g_routes[index].prev;
-    }    
+    route = g_routes.routes[index];
+    g_routes.routes[index] = NULL;
+    g_routes.size--;
+
+    V3_Free(route);
 
-    g_routes[index].next = -1;
-    g_routes[index].prev = -1;
-    
-    g_num_routes--;
-    
     clear_hash_cache();
     
     return 0;
 }
 
-int vnet_delete_route_entry_by_addr(char src_mac[6], 
-                                   char dest_mac[6], 
-                                   int src_mac_qual, 
-                                   int dest_mac_qual, 
-                                   int dest, 
-                                   int type, 
-                                   int src, 
-                                   int src_type) {
+static int vnet_delete_route_entry_by_addr(char src_mac[6], 
+                                          char dest_mac[6], 
+                                          int src_mac_qual, 
+                                          int dest_mac_qual, 
+                                          int link_idx, 
+                                          link_type_t type, 
+                                          int src, 
+                                          link_type_t src_type) {
     int index = find_route_entry(src_mac, dest_mac, src_mac_qual, 
-                                dest_mac_qual, dest, type, src, src_type);
+                                dest_mac_qual, link_idx, type, src, src_type);
     
     if (index == -1) {
+       PrintDebug("VNET: wrong in delete route entry %d\n", index);
        return -1;
     }
     
-    delete_route_entry(index);
-    
-    return 0;
-}
-
-int delete_sock(struct sock_list * socks, int * first_sock, int * last_sock, SOCK fd) {
-    int i;
-    int prev_i;
-    int next_i;
-    
-  
-    FOREACH_SOCK(i, socks, (*first_sock)) {
-       if (socks[i].sock == fd) {
-           V3_Close_Socket(socks[i].sock);
-           socks[i].sock = -1;
-           
-           prev_i = socks[i].prev;
-           next_i = socks[i].next;
-           
-           if (prev_i != -1) {
-               socks[prev_i].next = socks[i].next;
-           }
-           
-           if (next_i != -1) {
-               socks[next_i].prev = socks[i].prev;
-           }
-           
-           if (*first_sock == i) {
-               *first_sock = socks[i].next;
-           }
-           
-           if (*last_sock == i) {
-               *last_sock = socks[i].prev;
-           }
-           
-           socks[i].next = -1;
-           socks[i].prev = -1;
-           
-           return 0;
-       }
-    }
-    return -1;
+    return __delete_route_entry(index);
 }
-
-//setup the topology of the testing network
-static void store_topologies(SOCK fd) {
-    int i;
-    int src_mac_qual = MAC_ANY;
-    int dest_mac_qual = MAC_ANY;
-    uint_t dest;
-#ifndef VNET_SERVER
-    dest = (0 | 172 << 24 | 23 << 16 | 1 );
-    PrintDebug("VNET: store_topologies. NOT VNET_SERVER, dest = %x\n", dest);
-#else
-    dest = (0 | 172 << 24 | 23 << 16 | 2 );
-    PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest);
 #endif
 
-    int type = UDP_TYPE;
-    int src = 0;
-    int src_type= LINK_ANY; //ANY_SRC_TYPE
-    int data_port = 22;
-    
-    //store link table
-    for (i = 0; i < MAX_LINKS; i++) {
-       if (g_links[i].use == 0) {
-           g_links[i].dest = (int)dest;
-           g_links[i].type = type;
-           g_links[i].link_sock = fd;
-           g_links[i].remote_port = data_port;
-           g_links[i].use = 1;
-           
-           if (g_first_link == -1) {
-               g_first_link = i;
-           }       
-
-           g_links[i].prev = g_last_link;
-           g_links[i].next = -1;
-           
-           if (g_last_link != -1) {
-               g_links[g_last_link].next = i;
-           }
-           
-           g_last_link = i;
-           
-           g_num_links++;
-           PrintDebug("VNET: store_topologies. new link: socket: %d, remote %x:[%d]\n", g_links[i].link_sock, (uint_t)g_links[i].dest, g_links[i].remote_port);
-       }
-    }
-    
-    
-    //store route table
-    
-    type = LINK_EDGE;
-    dest = 0;
-    
-    for (i = 0; i < MAX_ROUTES; i++) {
-       if (g_routes[i].use == 0) {
-           if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
-               //                  memcpy(g_routes[i].src_mac, src_mac, 6);
-           } else {
-               memset(g_routes[i].src_mac, 0, 6);
-           }
-           
-           if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
-               //                  memcpy(g_routes[i].dest_mac, dest_mac, 6);
-           } else {
-               memset(g_routes[i].dest_mac, 0, 6);
-           }
-           
-           g_routes[i].src_mac_qual = src_mac_qual;
-           g_routes[i].dest_mac_qual = dest_mac_qual;
-           g_routes[i].dest = (int)dest;
-           g_routes[i].type = type;
-           g_routes[i].src = src;
-           g_routes[i].src_type = src_type;
-           
-           g_routes[i].use = 1;
-           
-           if (g_first_route == -1) {
-               g_first_route = i;
-           }
-           
-           g_routes[i].prev = g_last_route;
-           g_routes[i].next = -1;
-           
-           if (g_last_route != -1) {
-               g_routes[g_last_route].next = i;
-           }
-           
-           g_last_route = i;
-           
-           g_num_routes++;
-           
-           PrintDebug("VNET: store_topologies. new route: src_mac: %s, dest_mac: %s, dest: %d\n", g_routes[i].src_mac, g_routes[i].dest_mac, dest);
-           
-       }
-    }
-}
-
-static int match_route(uint8_t * src_mac, uint8_t * dst_mac, int src_type, int src_index, int * matches) { 
-    int values[MAX_ROUTES];
+static int match_route(uint8_t * src_mac, 
+                      uint8_t * dst_mac, 
+                      link_type_t src_type, 
+                      int src_index, 
+                      int * matches) {
     int matched_routes[MAX_ROUTES];
-    
     int num_matches = 0;
     int i;
-    int max = 0;
-    int no = 0;
-    int exact_match = 0;
-    
-    FOREACH_ROUTE(i, g_routes, g_first_route) {
-       if ((g_routes[i].src_type != LINK_ANY) &&
-           ((g_routes[i].src_type != src_type) ||
-            ((g_routes[i].src != src_index) &&
-             (g_routes[i].src != -1)))) {
-           PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
-           continue;
-       }
-       
-       if ( (g_routes[i].dest_mac_qual == MAC_ANY) &&
-            (g_routes[i].src_mac_qual == MAC_ANY) ) {      
-           matched_routes[num_matches] = i;
-           values[num_matches] = 3;
-           num_matches++;
-       }
-       
-       if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
-           if (g_routes[i].src_mac_qual !=  MAC_NOT) {
-               if (g_routes[i].dest_mac_qual == MAC_ANY) {
-                   matched_routes[num_matches] = i;
-                   values[num_matches] = 6;
-                   
-                   num_matches++;
-               } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
-                   if (g_routes[i].dest_mac_qual != MAC_NOT) {   
-                       matched_routes[num_matches] = i;
-                       values[num_matches] = 8;    
-                       exact_match = 1;
-                       num_matches++;
-                   }
-               }
-           }
-       }
-       
-       if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
-           if (g_routes[i].dest_mac_qual != MAC_NOT) {
-               if (g_routes[i].src_mac_qual == MAC_ANY) {
-                   matched_routes[num_matches] = i;
-                   values[num_matches] = 6;
-                   
-                   num_matches++;
-               } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
-                   if (g_routes[i].src_mac_qual != MAC_NOT) {
-                       if (exact_match == 0) {
-                           matched_routes[num_matches] = i;
-                           values[num_matches] = 8;
-                           num_matches++;
-                       }
-                   }
-               }
-           }
-       }
-       
-       if ((g_routes[i].dest_mac_qual == MAC_NOT) &&
-           (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0)) {
-           if (g_routes[i].src_mac_qual == MAC_ANY) {
-               matched_routes[num_matches] = i;
-               values[num_matches] = 5;                    
-               num_matches++;    
-           } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
-               if (g_routes[i].src_mac_qual != MAC_NOT) {      
-                   matched_routes[num_matches] = i;
-                   values[num_matches] = 7;                  
-                   num_matches++;
-               }
-           }
-       }
-       
-       if ((g_routes[i].src_mac_qual == MAC_NOT) &&
-           (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0)) {
-           if (g_routes[i].dest_mac_qual == MAC_ANY) {
-               matched_routes[num_matches] = i;
-               values[num_matches] = 5;            
-               num_matches++;
-           } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
-               if (g_routes[i].dest_mac_qual != MAC_NOT) { 
-                   matched_routes[num_matches] = i;
-                   values[num_matches] = 7;
-                   num_matches++;
-               }
-           }
-       }
-    }
-    //end FOREACH_ROUTE
-    
-    FOREACH_ROUTE(i, g_routes, g_first_route) {
-       if ((memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
-           (g_routes[i].dest_mac_qual == MAC_NONE) &&
-           ((g_routes[i].src_type == LINK_ANY) ||
-            ((g_routes[i].src_type == src_type) &&
-             ((g_routes[i].src == src_index) ||
-              (g_routes[i].src == -1))))) {
-           matched_routes[num_matches] = i;
-           values[num_matches] = 4;
-           PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
-           num_matches++;
-       }
-    }
-    
-    //If many rules have been matched, we choose one which has the highest value rating
-    if (num_matches == 0) {
-       return 0;
-    }
+    struct routing_entry *route = NULL; 
+
+#ifdef CONFIG_DEBUG_VNET
+    char dest_str[18];
+    char src_str[18];
     
-    for (i = 0; i < num_matches; i++) {
-       if (values[i] > max) {
-           no = 0;
-           max = values[i];
-           matches[no] = matched_routes[i];
-           no++;
-       } else if (values[i] == max) {
-           matches[no] = matched_routes[i];
-           no++;
-       }
-    }
+    mac_to_string(src_mac, src_str);  
+    mac_to_string(dst_mac, dest_str);
     
-    return no;
-}
+    PrintDebug("Vnet: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dest_str);
 
+    dump_routes(g_routes.routes);
+#endif
 
-static int process_udpdata() {
-    struct raw_ethernet_pkt * pt;
+    for(i = 0; i < MAX_ROUTES; i++) {
+        if (g_routes.routes[i] != NULL){
+            route = g_routes.routes[i];
+
+            if(src_type == LINK_ANY && src_index == -1) {
+                if ((route->dest_mac_qual == MAC_ANY) &&
+                      (route->src_mac_qual == MAC_ANY)) {      
+                    matched_routes[num_matches] = i;
+                    num_matches++;
+                }
+       
+                if (memcmp((void *)&route->src_mac, (void *)src_mac, 6) == 0) {
+                    if (route->src_mac_qual !=  MAC_NOT) {
+                         if (route->dest_mac_qual == MAC_ANY) {
+                             matched_routes[num_matches] = i;
+                             num_matches++;
+                         } else if (route->dest_mac_qual != MAC_NOT &&
+                                     memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) == 0) {
+                            matched_routes[num_matches] = i;
+                            num_matches++;
+                        }
+                    }
+                }
 
-    uint32_t dest = 0;
-    uint16_t remote_port = 0;
-    SOCK link_sock = g_udp_sockfd;
-    int length = sizeof(struct raw_ethernet_pkt) - (2 * sizeof(int));   //minus the "size" and "type" 
+                if (memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) == 0) {
+                    if (route->dest_mac_qual != MAC_NOT) {
+                        if (route->src_mac_qual == MAC_ANY) {
+                            matched_routes[num_matches] = i;
+                            num_matches++;
+                        } else if ((route->src_mac_qual != MAC_NOT) && 
+                                       (memcmp((void *)&route->src_mac, (void *)src_mac, 6) == 0)) {
+                            matched_routes[num_matches] = i;
+                            num_matches++;
+                        }
+                     }
+                }
 
-    //run in a loop to get packets from outside network, adding them to the incoming packet queue
-    while (1) {
-       pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
+                if ((route->dest_mac_qual == MAC_NOT) &&
+                      (memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) != 0)) {
+                    if (route->src_mac_qual == MAC_ANY) {
+                        matched_routes[num_matches] = i;           
+                        num_matches++;    
+                    } else if ((route->src_mac_qual != MAC_NOT) && 
+                                   (memcmp((void *)&route->src_mac, (void *)src_mac, 6) == 0)) {     
+                        matched_routes[num_matches] = i;     
+                        num_matches++;
+                     }
+                }
 
-       if (pt == NULL){
-           PrintError("Vnet: process_udp: Malloc fails\n");
-           continue;
-       }
-       
-       PrintDebug("Vnet: route_thread: socket: [%d]. ready to receive from ip [%x], port [%d] or from VMs\n", link_sock, (uint_t)dest, remote_port);
-       pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length);
-       PrintDebug("Vnet: route_thread: socket: [%d] receive from ip [%x], port [%d]\n", link_sock, (uint_t)dest, remote_port);
-       
-       if (pt->size <= 0) {
-           PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n");
-           V3_Free(pt);
-           return -1;
-       }
-       
-       PrintDebug("Vnet: process_udp: get packet\n");
-       print_packet(pt->data, pt->size);
+                if ((route->src_mac_qual == MAC_NOT) &&
+                      (memcmp((void *)&route->src_mac, (void *)src_mac, 6) != 0)) {
+                    if (route->dest_mac_qual == MAC_ANY) {
+                        matched_routes[num_matches] = i;   
+                        num_matches++;
+                    } else if ((route->dest_mac_qual != MAC_NOT) &&
+                                   (memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) == 0)) {
+                        matched_routes[num_matches] = i;
+                        num_matches++;
+                    }
+                }
+            }//end if src_type == Link_any
+        }      
+    }//end for
 
+    PrintDebug("Vnet: match_route: Matches=%d\n", num_matches);
        
-       //V3_Yield();
+    for (i = 0; i < num_matches; i++) {
+        matches[i] = matched_routes[i];
     }
+    
+    return num_matches;
 }
 
-
-
-static int indata_handler( )
-{
-      if (!use_tcp)
-          process_udpdata();     
-
-      return 0;   
-}
-
-static int start_recv_data()
-{
-       if (use_tcp){
-               
-       } else {
-               SOCK udp_data_socket;
-  
-               if ((udp_data_socket = V3_Create_UDP_Socket()) < 0){
-                       PrintError("VNET: Can't setup udp socket\n");
-                       return -1; 
-               }
-               PrintDebug("Vnet: vnet_setup_udp: get socket: %d\n", udp_data_socket);
-               g_udp_sockfd = udp_data_socket;
-
-               store_topologies(udp_data_socket);
-
-               if (V3_Bind_Socket(udp_data_socket, vnet_udp_port) < 0){ 
-                       PrintError("VNET: Can't bind socket\n");
-                       return -1;
-               }
-               PrintDebug("VNET: vnet_setup_udp: bind socket successful\n");
-       }
-
-       V3_CREATE_THREAD(&indata_handler, NULL, "VNET_DATA_HANDLER");
-       return 0;
-}
-
-
-static inline int if_write_pkt(struct vnet_if_device *iface, struct raw_ethernet_pkt * pkt) {
-    return iface->input((uchar_t *)pkt->data, pkt->size);
-}
-
-static int handle_one_pkt(struct raw_ethernet_pkt * pkt) {
-    int src_link_index = 0;    //the value of src_link_index of udp always is 0
+static int handle_one_pkt(struct ethernet_pkt *pkt) {
+    int src_link_index = -1;   //the value of src_link_index of udp always is 0
     int i;
     char src_mac[6];
     char dst_mac[6];
 
-    int matches[g_num_routes];
+    int matches[MAX_ROUTES];
     int num_matched_routes = 0;
 
-    struct HEADERS headers;
+    struct in_pkt_header header;
+
+    char hash_key[hash_key_size];
   
     // get the ethernet and ip headers from the packet
-    memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
-
-    int j;
-    for (j = 0;j < 6; j++) {
-       src_mac[j] = headers.ethernetsrc[j];
-       dst_mac[j] = headers.ethernetdest[j];
-    }
-
+    memcpy((void *)&header, (void *)pkt->data, sizeof(header));
+    memcpy(src_mac, header.ethernetsrc, 6);
+    memcpy(dst_mac, header.ethernetdest, 6);
 
-#ifdef DEBUG
+#ifdef CONFIG_DEBUG_VNET
     char dest_str[18];
     char src_str[18];
     
@@ -934,306 +615,407 @@ static int handle_one_pkt(struct raw_ethernet_pkt * pkt) {
     
     PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
 #endif
-    
-    char hash_key[hash_key_size];
-    make_hash_key(hash_key, src_mac, dst_mac, LINK_EDGE, src_link_index);//link_edge -> pt->type???
+
+    // link_edge -> pt->type???
+    make_hash_key(hash_key, src_mac, dst_mac, LINK_EDGE, src_link_index); 
     
     num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
     
-    if (num_matched_routes == -1) {  //no match
-        num_matched_routes = match_route(src_mac, dst_mac, pkt->type, src_link_index, matches);
+    if (num_matched_routes == -1) {  
+    // no match in the cache
+        num_matched_routes = match_route(src_mac, dst_mac, LINK_ANY, src_link_index, matches);
        
-       if (num_matched_routes > 0) {
-           add_route_to_cache(hash_key, num_matched_routes,matches);      
-       }
+        if (num_matched_routes > 0) {
+            add_route_to_cache(hash_key, num_matched_routes,matches);      
+        }
     }
     
     PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
+
+    if (num_matched_routes == 0) {
+        return -1;
+    }
     
-    for (i = 0; i < num_matched_routes; i++) {
+    for (i = 0; i < num_matched_routes; i++) {//send packet to all destinations
         int route_index = -1;
         int link_index = -1;
-        int dev_index = -1;
-       
+        int pkt_len = 0;
+        struct link_entry * link = NULL;
+
         route_index = matches[i];
-       
-        PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
-       
-        if (g_routes[route_index].type == LINK_EDGE) {
-            link_index = g_routes[route_index].dest;
-           
-            if(g_links[link_index].type == UDP_TYPE) {
-                int size;
+        link_index = g_routes.routes[route_index]->link_idx;
 
-                               
-                 PrintDebug("===Vnet: HandleDataOverLink: Serializing UDP Packet to link_sock [%d], dest [%x], remote_port [%d], size [%d]\n", g_links[link_index].link_sock, (uint_t)g_links[link_index].dest,  g_links[link_index].remote_port, (int)pkt->size);
+        if ((link_index < 0) || (link_index > MAX_LINKS) || 
+            (g_links.links[link_index] == NULL)) {
+            continue;
+        }
+       
+        link = g_links.links[link_index];
+        pkt_len = pkt->size;
+        if (link->type == LINK_EDGE) {
+
+             //apply the header in the beginning of the packet to be sent
+            if (link->dst_link->pro_type == UDP_TYPE) {
+                struct udp_link_header *hdr = &(link->dst_link->vnet_header);
+                struct ip_header *ip = &hdr->ip_hdr;
+                struct udp_header *udp = &hdr->udp_hdr;
+                 udp->len = pkt_len + sizeof(struct udp_header);
+                 ip->total_len = pkt_len + sizeof(struct udp_header) + sizeof(struct ip_header);
+                 ip->cksum = ip_xsum(ip, sizeof(struct ip_header));
                
-                if ((size = V3_SendTo_IP(g_links[link_index].link_sock,  g_links[link_index].dest,  g_links[link_index].remote_port, pkt->data, pkt->size)) != pkt->size)  {
-                    PrintError("Vnet: sending by UDP Exception, %x\n", size);
-                    return -1;
+                int hdr_size = sizeof(struct udp_link_header);
+                memcpy(&pkt->ext_hdr, hdr, hdr_size);
+
+                pkt_len += hdr_size;
+                if ((link->dst_link->input((uchar_t *)&pkt->ext_hdr, pkt_len, link->dst_link->private_data)) != pkt_len) {
+                    PrintDebug("VNET: Packet not sent properly to link: %d\n", link_index);
+                    continue;
                 }
-               
-                PrintDebug("Vnet: HandleDataOverLink: Serializing UDP Packet to link_sock [%d], dest [%x], remote_port [%d], size [%d]\n", g_links[link_index].link_sock, (uint_t)g_links[link_index].dest,  g_links[link_index].remote_port, (int)pkt->size);
-               
-            } else if (g_links[link_index].type == TCP_TYPE) {
-               
-            }
-        } else if (g_routes[route_index].type == LINK_INTERFACE) {
-            dev_index = g_routes[route_index].dest;
-      
-            PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device->name);
-
-            if (if_write_pkt(g_devices[dev_index].device, pkt) == -1) {
-               PrintDebug("Can't write output packet to link\n");
-                return -1;
-            }
+            }else {
+                PrintDebug("VNET: Link protocol type not support\n");
+                continue;
+            }
+        } else if (link->type == LINK_INTERFACE) {
+            if ((link->dst_dev->input(pkt->data, pkt_len, link->dst_dev->private_data)) != pkt_len) {
+                PrintDebug("VNET: Packet not sent properly to link: %d\n", link_index);
+                continue;
+            }
         } else {
-            PrintDebug("Vnet: Wrong Edge type\n");
+            PrintDebug("Vnet: Wrong Edge type of link: %d\n", link_index);
+            continue;
         }
-    }
 
-     return 0;
+        PrintDebug("Vnet: HandleDataOverLink: Forward packet according to Route entry %d to link %d\n", route_index, link_index);
+    }
+    
+    return 0;
 }
 
-static int send_ethernet_pkt(char * buf, int length) {
-       struct raw_ethernet_pkt * pt;
+static int send_ethernet_pkt(uchar_t *data, int len) {
+    struct ethernet_pkt *pkt;
 
-       pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
-       raw_ethernet_packet_init(pt, buf, length);  //====here we copy sending data once 
-       
-       PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pt->size);
-       print_packet((char *)buf, length);
+    pkt = (struct ethernet_pkt *)V3_Malloc(sizeof(struct ethernet_pkt));
+   
+    if(pkt == NULL){
+        PrintError("VNET: Memory allocate fails\n");
+        return -1;
+    }
+    memset(pkt, 0, sizeof(struct ethernet_pkt));
        
-       v3_enqueue(g_inpkt_q, (addr_t)pt);
-       return 0;
+    ethernet_packet_init(pkt, data, len);  //====here we copy sending data once 
        
-}
+    PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pkt->size);
 
-int v3_Send_pkt(uchar_t *buf, int length) {
-    PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
+#ifdef CONFIG_DEBUG_VNET
+    print_packet((char *)data, len);
+#endif
     
-    return send_ethernet_pkt((char *)buf, length);
+    v3_enqueue(g_inpkt_q, (addr_t)pkt);
+
+    return 0;
 }
 
-static int add_device_to_table(struct vnet_if_device*device, int type) {
-    int i;
+//send raw ethernet packet
+int v3_vnet_send_rawpkt(uchar_t * buf, int len, void *private_data) {
+    PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", len);
     
-    for (i = 0; i < MAX_DEVICES; i++) {
-       if (g_devices[i].use == 0) {
-           g_devices[i].type = type;
-           g_devices[i].use = 1;
-           
-           if (g_first_device == -1) {
-               g_first_device = i;
-           }       
+    return send_ethernet_pkt(buf, len);
+}
 
-           g_devices[i].prev = g_last_device;
-           g_devices[i].next = -1;
-           
-           if (g_last_device != -1) {
-               g_devices[g_last_device].next = i;
-           }
+//sending the packet from Dom0, should remove the link header
+int v3_vnet_send_udppkt(uchar_t * buf, int len, void *private_data) {
+    PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", len);
 
-           g_last_device = i;
-           g_num_devices++;
-           
-           return i;
-       }
-    }
-    
-    return -1;
+    uint_t hdr_len = sizeof(struct udp_link_header);
+   
+    return send_ethernet_pkt((uchar_t *)(buf+hdr_len), len - hdr_len);
 }
 
-static int search_device(char *device_name) {
+static int search_device(char * device_name) {
     int i;
 
-    for (i = 0; i < MAX_DEVICES; i++) {
-        if (g_devices[i].use == 1) {
-           if (!strcmp(device_name, g_devices[i].device->name)) {
+    for (i = 0; i < MAX_LINKS; i++) {
+        if ((g_links.links[i] != NULL) && (g_links.links[i]->type == LINK_INTERFACE)) {
+           if (strcmp(device_name, g_links.links[i]->dst_dev->name) == 0) {
                return i;
            }
         }
     }
-    
-    return -1;
-}
-
-static struct vnet_if_device * delete_device_from_table(int index) {
-    int next_i;
-    int prev_i;
-    struct vnet_if_device * device = NULL;
-
-    if (g_devices[index].use == 0) {
-       return NULL;
-    }
-
-    g_devices[index].use = 0;
-
-    prev_i = g_devices[index].prev;
-    next_i = g_devices[index].next;
-
-    if (prev_i != -1) {
-        g_devices[prev_i].next = g_devices[index].next;
-    }
-
-    if (next_i != -1) {
-        g_devices[next_i].prev = g_devices[index].prev;
-    }
 
-    if (g_first_device == index) {
-        g_first_device = g_devices[index].next;
-    }
-
-    if (g_last_device == index) {
-        g_last_device = g_devices[index].prev;
-    }
-
-    g_devices[index].next = -1;
-    g_devices[index].prev = -1;
-
-    device = g_devices[index].device;
-    g_devices[index].device = NULL;
-
-    g_num_devices--;
-
-    return device;
+    return -1;
 }
 
+int vnet_register_device(struct vm_device * vdev, 
+                        char * dev_name, 
+                        uchar_t mac[6], 
+                        int (*netif_input)(uchar_t * pkt, uint_t size, void * private_data), 
+                        void * data) {
+    struct vnet_if_device * if_dev;
 
-int vnet_register_device(char * dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void * data) {
-    struct vnet_if_device * dev;
-    
-    dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
-    
-    if (dev == NULL){
-       PrintError("VNET: Malloc fails\n");
+    int idx = search_device(dev_name);
+    if (idx != -1) {
+       PrintDebug("VNET: register device: Already has device with the name %s\n", dev_name);
        return -1;
     }
     
-    strncpy(dev->name, dev_name, 50);
-    dev->input = netif_input;
-    dev->data = data;
-    
-    if (add_device_to_table(dev, GENERAL_NIC) == -1) {
+    if_dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device)); 
+    if (if_dev == NULL) {
+       PrintError("VNET: Malloc fails\n");
        return -1;
     }
     
-    return 0;
+    strcpy(if_dev->name, dev_name);
+    strncpy(if_dev->mac_addr, mac, 6);
+    if_dev->dev = vdev;
+    if_dev->input = netif_input;
+    if_dev->private_data = data;
+
+    struct link_entry * link = (struct link_entry *)V3_Malloc(sizeof(struct link_entry));
+    link->type = LINK_INTERFACE;
+    link->dst_dev = if_dev;
+
+    idx = __add_link_entry(link);
+
+    return idx;
 }
 
-int vnet_unregister_device(char * dev_name) {
-    int i;
+#if 0
+static int vnet_unregister_device(char * dev_name) {
+    int idx;
 
-    i = search_device(dev_name);
+    idx = search_device(dev_name);
     
-    if (i == -1) {
+    if (idx == -1) {
+       PrintDebug("VNET: No device with name %s found\n", dev_name);
         return -1;
     }
 
-    struct vnet_if_device * device = delete_device_from_table(i);
-
+    struct vnet_if_device * device = (struct vnet_if_device *)__delete_link_entry(idx);
     if (device == NULL) {
+       PrintError("VNET: Device %s not in the link table %d, something may be wrong in link table\n", dev_name, idx);
        return -1;
     }
 
     V3_Free(device);
 
-    return 0;
+    return idx;
 }
 
-int v3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size)) {
-    return vnet_register_device("NE2000", netif_input, NULL);
-}
+#endif
 
 int v3_vnet_pkt_process() {
-    struct raw_ethernet_pkt * pt;
+    struct ethernet_pkt * pkt;
 
-    PrintDebug("VNET: In vnet_check\n");
-       
-    while ((pt = (struct raw_ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) {
-       PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pt->size, (int)pt->type);
-       v3_hexdump(pt->data, pt->size, NULL, 0);
-       
-       if(handle_one_pkt(pt)) {
-           PrintDebug("VNET: vnet_check: handle one packet!\n");  
-       }
+    while ((pkt = (struct ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) {
+        PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pkt->size, (int)pkt->type);
+
+        if (handle_one_pkt(pkt) != -1) {
+            PrintDebug("VNET: vnet_check: handle one packet!\n");  
+        } else {
+            PrintDebug("VNET: vnet_check: Fail to forward one packet, discard it!\n"); 
+        }
        
-       V3_Free(pt); //be careful here
+        V3_Free(pkt); // be careful here
     }
     
     return 0;
 }
 
-static void init_link_table() {
+static void init_empty_link_table() {
     int i;
 
     for (i = 0; i < MAX_LINKS; i++) {
-        g_links[i].use = 0;
-        g_links[i].next = -1;
-        g_links[i].prev = -1;
+        g_links.links[i] = NULL;
     }
-       
-    g_first_link = -1;
-    g_last_link = -1;
-    g_num_links = 0;
-}
-
-static void init_device_table() {
-    int i;
 
-    for (i = 0; i < MAX_DEVICES; i++) {
-        g_devices[i].use = 0;
-        g_devices[i].next = -1;
-        g_devices[i].prev = -1;
-    }
-       
-    g_first_device = -1;
-    g_last_device = -1;
-    g_num_devices = 0;
+    g_links.size = 0;
 }
 
-static void init_route_table() {       
+static void init_empty_route_table() { 
     int i;
 
     for (i = 0; i < MAX_ROUTES; i++) {
-        g_routes[i].use = 0;
-        g_routes[i].next = -1;
-        g_routes[i].prev = -1;
+        g_routes.routes[i] = NULL;
     }
-       
-     g_first_route = -1;
-     g_last_route = -1;
-     g_num_routes = 0;
+
+    g_links.size = 0;
 }
 
 static void init_tables() {
-    init_link_table();
-    init_device_table();
-    init_route_table();
+    init_empty_link_table();
+    init_empty_route_table();
     init_route_cache();
 }
 
-static void init_pkt_queue()
-{
+static void init_pkt_queue() {
     PrintDebug("VNET Init package receiving queue\n");
 
     g_inpkt_q = v3_create_queue();
     v3_init_queue(g_inpkt_q);
 }
 
+static void free_link_mem(struct link_entry *link){
+    V3_Free(link->dst_dev);
+    V3_Free(link);
+}
+
+// TODO:
+static int addto_routing_link_tables(struct routing_entry *route_tab, 
+                                                       uint16_t num_routes,
+                                                       struct link_entry *link_tab,
+                                                       uint16_t num_links){
+    struct routing_entry *route, *new_route;
+    struct link_entry *link, *new_link;
+    int new_idx;
+    int i;
+    int link_idxs[MAX_LINKS];
+
+    //add all of the links first, record their new indexs
+    for (i = 0; i < num_links; i++) {
+       link_idxs[i] = -1;
+       link = &link_tab[i];
+       new_link = (struct link_entry *)V3_Malloc(sizeof(struct link_entry));
+
+       if (new_link == NULL){
+           PrintError("VNET: Memory allocate error\n");
+           return -1;
+       }
+
+       new_link->type = link->type;
+       
+       //TODO: how to set the input parameters here
+       if (link->type == LINK_EDGE){
+           struct vnet_if_device *ifdev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
+
+           if (ifdev == NULL){
+               PrintError("VNET: Memory allocate fails\n");
+               return -1;
+           }
+           
+           memcpy(ifdev->name, link->dst_dev->name, DEVICE_NAME_LEN);
+
+           // TODO:
+           //ifdev->mac_addr
+           //ifdev->input
+           //ifdev->private_data
 
+           new_link->dst_dev = ifdev;    
+       }else if (link->type == LINK_INTERFACE){
+           struct vnet_if_link *iflink = (struct vnet_if_link *)V3_Malloc(sizeof(struct vnet_if_link));
 
-void v3_vnet_init() {  
+           if (iflink == NULL){
+               PrintError("VNET: Memory allocate fails\n");
+               return -1;
+           }
+           iflink->pro_type = link->dst_link->pro_type;
+           iflink->dest_ip = link->dst_link->dest_ip;
+           iflink->dest_port = link->dst_link->dest_port;
+           memcpy(&iflink->vnet_header, &link->dst_link->vnet_header, sizeof(struct udp_link_header));
+
+           // TODO:
+           //iflink->input = 
+           //iflink->private_data = 
+           
+           new_link->dst_link = iflink;
+       }else{
+           PrintDebug("VNET: invalid link type\n");
+           V3_Free(new_link);
+           continue;
+       }
+       
+       new_idx = __add_link_entry(new_link);
+       if (new_idx < 0) {
+           PrintError("VNET: Adding link fails\n");
+           free_link_mem(new_link);
+           continue;
+       }       
+       link_idxs[i] = new_idx;
+    }
+
+    //add all of routes, replace with new link indexs
+    for (i = 0; i < num_routes; i++) {
+       route = &route_tab[i];
+       if (route->link_idx < 0 || route->link_idx >= num_links || 
+           ((route->src_link_idx != -1) && 
+             (route->src_link_idx < 0 || route->src_link_idx >= num_links))){
+           PrintError("VNET: There is error in the intial tables data from guest\n");
+           continue;
+       }
 
-       PrintDebug("VNET Init: Vnet input queue successful.\n");
+       new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry));
 
-       init_tables();
+       if (new_route == NULL){
+           PrintError("VNET: Memory allocate fails\n");
+           return -1;
+       }
+       memcpy(new_route, route, sizeof(struct routing_entry));
+       
+       new_route->link_idx = link_idxs[new_route->link_idx];
+               
+       if (route->src_link_idx != -1)
+           new_route->src_link_idx = link_idxs[new_route->src_link_idx];
+
+       if((__add_route_entry(new_route)) == -1){
+           PrintDebug("VNET: Adding route fails");
+           V3_Free(new_route);
+       }
+       new_route = NULL;       
+    }
+
+    return 0;
+}
+
+//add the guest specified routes and links to the tables
+static int handle_init_tables_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
+    struct vnet_if_link *link_array;
+    struct routing_entry *route_array;
+    addr_t routes_addr, routes_addr_gva;
+    addr_t links_addr, links_addr_gva;
+    uint16_t route_size, link_size;
+
+    routes_addr_gva = (addr_t)info->vm_regs.rbx;
+    route_size = (uint16_t)info->vm_regs.rcx;
+    links_addr_gva  = (addr_t)info->vm_regs.rdx;
+    link_size = (uint16_t)info->vm_regs.rsi;
+
+    PrintDebug("Vnet: In handle_init_tables_hcall\n");
+    PrintDebug("Vnet: route_table_start: %x, route size: %d\n", (int)routes_addr_gva, route_size);
+    PrintDebug("Vnet: link_table_start: %x, link size: %d\n", (int)links_addr_gva, link_size);
+       
+    if (guest_va_to_host_va(info, routes_addr_gva, &routes_addr) == -1) {
+       PrintError("VNET: Could not translate guest address\n");
+       return -1;
+    }
+    route_array = (struct routing_entry *)routes_addr;
+
+#ifdef CONFIG_DEBUG_VNET
+    dump_dom0_routes(route_array, route_size);
+#endif
+
+    if (guest_va_to_host_va(info, links_addr_gva, &links_addr) == -1) {
+       PrintError("VNET: Could not translate guest address\n");
+       return -1;
+    }  
+    link_array = (struct vnet_if_link *)links_addr;
+
+#ifdef CONFIG_DEBUG_VNET
+    dump_dom0_links(link_array, link_size);
+#endif
+
+
+// TODO:
+    if (0)addto_routing_link_tables(route_array, route_size, NULL, 0);
+
+    return 0;
+}
 
-       init_pkt_queue();
+void v3_vnet_init(struct guest_info * vm) {
+    init_tables();
+    init_pkt_queue();
        
-       //store_topologies(udp_data_socket);
+    v3_register_hypercall(vm, VNET_INITAB_HCALL, handle_init_tables_hcall, NULL);
 
-       start_recv_data();
+    PrintDebug("VNET Initialized\n");
 }