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.


Clear code
[palacios.git] / palacios / src / palacios / vmm_vnet.c
index f87ad58..bdc4719 100644 (file)
@@ -47,7 +47,7 @@ struct vnet_dev {
     uint8_t mac_addr[6];
     struct v3_vm_info * vm;
     
-    int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt,  void * private_data);
+    int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data);
     void * private_data;
     
     int dev_id;
@@ -55,6 +55,14 @@ struct vnet_dev {
 } __attribute__((packed));
 
 
+struct vnet_brg_dev {
+    struct v3_vm_info * vm;
+    
+    int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data);
+    void * private_data;
+} __attribute__((packed));
+
+
 
 
 
@@ -83,10 +91,12 @@ struct route_list {
 static struct {
     struct list_head routes;
     struct list_head devs;
-
+    
     int num_routes;
     int num_devs;
 
+    struct vnet_brg_dev *bridge;
+
     v3_lock_t lock;
 
     struct gen_queue * inpkt_q;
@@ -116,15 +126,16 @@ static void print_route(struct vnet_route_info *route){
     PrintDebug("Src dev id (%d), src type (%d)", 
                        route->route_def.src_id, 
                        route->route_def.src_type);
-    PrintDebug("Dst dev id (%d), dst type (%d)", 
+    PrintDebug("Dst dev id (%d), dst type (%d)\n", 
                        route->route_def.dst_id, 
                        route->route_def.dst_type);
-    PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_input (%p), dst_dev_data (%p)\n",
+    if (route->route_def.dst_type == LINK_INTERFACE) {
+       PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_input (%p), dst_dev_data (%p)\n",
                                        route->dst_dev,
                                        route->dst_dev->dev_id,
                                        route->dst_dev->input,
                                        route->dst_dev->private_data);
-
+    }
 }
 
 static void dump_routes(){
@@ -148,12 +159,12 @@ static void dump_routes(){
  * This means we can generate the hash from an offset into the pkt struct
  */
 static inline uint_t hash_fn(addr_t hdr_ptr) {    
-    uint8_t * hdr_buf = (uint8_t *)&(hdr_ptr);
-    
+    uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
+
     return v3_hash_buffer(hdr_buf, VNET_HASH_SIZE);
 }
 
-static inline int hash_eq(addr_t key1, addr_t key2) {
+static inline int hash_eq(addr_t key1, addr_t key2) {  
     return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
 }
 
@@ -179,7 +190,7 @@ static int clear_hash_cache() {
 
 static int look_into_cache(struct v3_vnet_pkt * pkt, struct route_list ** routes) {
     
-    *routes = (struct route_list *)v3_htable_search(vnet_state.route_cache, (addr_t)pkt);
+    *routes = (struct route_list *)v3_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
    
     return 0;
 }
@@ -250,10 +261,12 @@ int v3_vnet_add_route(struct v3_vnet_route route) {
     }
 
     flags = v3_lock_irqsave(vnet_state.lock);
+
     list_add(&(new_route->node), &(vnet_state.routes));
+    clear_hash_cache();
+
     v3_unlock_irqrestore(vnet_state.lock, flags);
    
-    clear_hash_cache();
 
 #ifdef CONFIG_DEBUG_VNET
     dump_routes();
@@ -390,12 +403,12 @@ static struct route_list * match_route(struct v3_vnet_pkt * pkt) {
     return matches;
 }
 
-static int handle_one_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
+
+int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
     struct route_list * matched_routes = NULL;
     unsigned long flags;
     int i;
 
-
 #ifdef CONFIG_DEBUG_VNET
    {
        struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
@@ -408,17 +421,13 @@ static int handle_one_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
    }
 #endif
 
-#ifdef CONFIG_VNET_PROFILE
-    struct guest_info *core = (struct guest_info *)private_data;
-    uint64_t start, end;
-    rdtscll(start);
-#endif
-
     flags = v3_lock_irqsave(vnet_state.lock);
 
     look_into_cache(pkt, &matched_routes);
        
     if (matched_routes == NULL) {  
+       PrintDebug("Vnet: can not find route in cache, looking into routing table\n");
+       
        matched_routes = match_route(pkt);
                
        if (matched_routes) {
@@ -432,18 +441,25 @@ static int handle_one_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
 
     v3_unlock_irqrestore(vnet_state.lock, flags);
 
-#ifdef CONFIG_VNET_PROFILE
-    {
-       rdtscll(end);
-       core->vnet_times.time_route_lookup = end - start;
-    }
-#endif
 
-    PrintDebug("Vnet: HandleOnePacket: %d\n", matched_routes->num_routes);
+    PrintDebug("Vnet: HandleOnePacket: route matches %d\n", matched_routes->num_routes);
+
     for (i = 0; i < matched_routes->num_routes; i++) {
         struct vnet_route_info * route = matched_routes->routes[i];
        
         if (route->route_def.dst_type == LINK_EDGE) {
+            pkt->dst_type = LINK_EDGE;
+            pkt->dst_id = route->route_def.dst_id;
+
+            if (vnet_state.bridge == NULL) {
+                PrintDebug("VNET: No bridge to sent data to links\n");
+                continue;
+            }
+
+            if (vnet_state.bridge->input(vnet_state.bridge->vm, pkt, vnet_state.bridge->private_data) == -1) {
+                PrintDebug("VNET: Packet not sent properly\n");
+                continue;
+           } 
 
         } else if (route->route_def.dst_type == LINK_INTERFACE) {
             if (route->dst_dev->input(route->dst_dev->vm, pkt, route->dst_dev->private_data) == -1) {
@@ -457,75 +473,92 @@ static int handle_one_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
 
         PrintDebug("Vnet: HandleOnePacket: Forward packet according to Route\n");
     }
-
-#ifdef CONFIG_VNET_PROFILE
-    {
-       rdtscll(start);
-       core->vnet_times.time_copy_to_guest = start - end;
-    }
-#endif
     
     return 0;
 }
 
-int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
-    PrintDebug("In Vnet Send: pkt size: %d\n", pkt->size);
-               
-    if (handle_one_pkt(pkt, private_data) != -1) {
-        PrintDebug("VNET: send one packet! pt length %d\n", pkt->size);  
-    } else {
-        PrintDebug("VNET: Fail to forward one packet, discard it!\n"); 
-    }
-
-    return 0;
-}
-
-int v3_vnet_add_dev(struct v3_vm_info *vm,uint8_t mac[6], 
+int v3_vnet_add_dev(struct v3_vm_info *vm, uint8_t mac[6], 
                    int (*netif_input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data), 
                    void * priv_data){
     struct vnet_dev * new_dev = NULL;
     unsigned long flags;
-    int dev_id;
 
-    flags = v3_lock_irqsave(vnet_state.lock);
-       
-    new_dev = find_dev_by_mac(mac);
-
-    if (new_dev) {
-       PrintDebug("VNET: register device: Already has device with the same mac\n");
-       dev_id = -1;
-       goto exit;
-    }
-    
     new_dev = (struct vnet_dev *)V3_Malloc(sizeof(struct vnet_dev)); 
 
     if (new_dev == NULL) {
        PrintError("VNET: Malloc fails\n");
-       dev_id = -1;
-       goto exit;
+       return -1;
     }
    
     memcpy(new_dev->mac_addr, mac, 6);
     new_dev->input = netif_input;
     new_dev->private_data = priv_data;
     new_dev->vm = vm;
-       
-    list_add(&(new_dev->node), &(vnet_state.devs));
-    vnet_state.num_devs ++;
-    new_dev->dev_id = vnet_state.num_devs;
-    dev_id = new_dev->dev_id;
+    new_dev->dev_id = 0;       
+
+    flags = v3_lock_irqsave(vnet_state.lock);
+
+    if (!find_dev_by_mac(mac)) {
+       list_add(&(new_dev->node), &(vnet_state.devs));
+       new_dev->dev_id = ++vnet_state.num_devs;
+    }
+
+    v3_unlock_irqrestore(vnet_state.lock, flags);
+
+    // if the device was found previosly the id should still be 0
+    if (new_dev->dev_id == 0) {
+       PrintError("Device Alrady exists\n");
+       return -1;
+    }
 
     PrintDebug("Vnet: Add Device: dev_id %d, input : %p, private_data %p\n",
                        new_dev->dev_id, new_dev->input, new_dev->private_data);
 
-exit:
-       
-    v3_unlock_irqrestore(vnet_state.lock, flags);
-    return dev_id;
+    return new_dev->dev_id;
 }
 
 
+int v3_vnet_add_bridge(struct v3_vm_info * vm,
+                      int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data), 
+                      void * priv_data) {
+    unsigned long flags;
+    int bridge_free = 0;
+    struct vnet_brg_dev * tmp_bridge = NULL;
+    
+    
+    flags = v3_lock_irqsave(vnet_state.lock);
+
+    if (vnet_state.bridge == NULL) {
+       bridge_free = 1;
+       vnet_state.bridge = (void *)1;
+    }
+
+    v3_unlock_irqrestore(vnet_state.lock, flags);
+
+    if (bridge_free == 0) {
+       PrintError("Bridge already set\n");
+       return -1;
+    }
+
+    tmp_bridge = (struct vnet_brg_dev *)V3_Malloc(sizeof(struct vnet_brg_dev));
+
+    if (tmp_bridge == NULL) {
+       PrintError("Malloc Fails\n");
+       return -1;
+    }
+    
+    tmp_bridge->vm = vm;
+    tmp_bridge->input = input;
+    tmp_bridge->private_data = priv_data;
+
+    // make this atomic to avoid possible race conditions
+    flags = v3_lock_irqsave(vnet_state.lock);
+    vnet_state.bridge = tmp_bridge;
+    v3_unlock_irqrestore(vnet_state.lock, flags);
+
+    return 0;
+}
+
 int V3_init_vnet() {
        
     INIT_LIST_HEAD(&(vnet_state.routes));