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.


Merge branch 'devel' of palacios@newskysaw.cs.northwestern.edu:/home/palacios/palacio...
[palacios.git] / palacios / src / palacios / vmm_vnet_core.c
index 6baef5a..e0e0ac7 100644 (file)
@@ -46,7 +46,9 @@ struct vnet_dev {
     void * private_data;
 
     int active;
-    uint8_t mode;  //vmm_drivern or guest_drivern
+
+    uint64_t bytes_tx, bytes_rx;
+    uint32_t pkts_tx, pkt_rx;
     
     struct list_head node;
 } __attribute__((packed));
@@ -57,7 +59,7 @@ struct vnet_brg_dev {
     struct v3_vnet_bridge_ops brg_ops;
 
     uint8_t type;
-    uint8_t mode;
+
     int active;
     void * private_data;
 } __attribute__((packed));
@@ -90,9 +92,10 @@ static struct {
     int num_routes;
     int num_devs;
 
-    struct vnet_brg_dev *bridge;
+    struct vnet_brg_dev * bridge;
 
     v3_lock_t lock;
+    struct vnet_stat stats;
 
     struct hashtable * route_cache;
 } vnet_state;
@@ -101,46 +104,47 @@ static struct {
 
 #ifdef CONFIG_DEBUG_VNET
 static inline void mac_to_string(uint8_t * mac, char * buf) {
-    snprintf(buf, 100, "%d:%d:%d:%d:%d:%d", 
+    snprintf(buf, 100, "%2x:%2x:%2x:%2x:%2x:%2x", 
             mac[0], mac[1], mac[2],
             mac[3], mac[4], mac[5]);
 }
 
-static void print_route(struct vnet_route_info * route){
+static void print_route(struct v3_vnet_route * route){
     char str[50];
 
-    mac_to_string(route->route_def.src_mac, str);
+    mac_to_string(route->src_mac, str);
     PrintDebug("Src Mac (%s),  src_qual (%d)\n", 
-              str, route->route_def.src_mac_qual);
-    mac_to_string(route->route_def.dst_mac, str);
+              str, route->src_mac_qual);
+    mac_to_string(route->dst_mac, str);
     PrintDebug("Dst Mac (%s),  dst_qual (%d)\n", 
-              str, route->route_def.dst_mac_qual);
+              str, route->dst_mac_qual);
     PrintDebug("Src dev id (%d), src type (%d)", 
-              route->route_def.src_id, 
-              route->route_def.src_type);
+              route->src_id, 
+              route->src_type);
     PrintDebug("Dst dev id (%d), dst type (%d)\n", 
-              route->route_def.dst_id, 
-              route->route_def.dst_type);
-    if (route->route_def.dst_type == LINK_INTERFACE) {
-       PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
-              route->dst_dev,
-              route->dst_dev->dev_id,
-              (void *)&(route->dst_dev->dev_ops),
-              route->dst_dev->private_data);
-    }
+              route->dst_id, 
+              route->dst_type);
 }
 
 static void dump_routes(){
-       struct vnet_route_info *route;
+    struct vnet_route_info *route;
 
-       int i = 0;
-       PrintDebug("\n========Dump routes starts ============\n");
-       list_for_each_entry(route, &(vnet_state.routes), node) {
-           PrintDebug("\nroute %d:\n", i++);
+    int i = 0;
+    PrintDebug("\n========Dump routes starts ============\n");
+    list_for_each_entry(route, &(vnet_state.routes), node) {
+       PrintDebug("\nroute %d:\n", i++);
                
-           print_route(route);
+       print_route(&(route->route_def));
+       if (route->route_def.dst_type == LINK_INTERFACE) {
+           PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
+               route->dst_dev,
+               route->dst_dev->dev_id,
+               (void *)&(route->dst_dev->dev_ops),
+               route->dst_dev->private_data);
        }
-       PrintDebug("\n========Dump routes end ============\n");
+    }
+
+    PrintDebug("\n========Dump routes end ============\n");
 }
 
 #endif
@@ -202,14 +206,28 @@ static struct vnet_dev * dev_by_mac(uint8_t * mac) {
     struct vnet_dev * dev = NULL; 
     
     list_for_each_entry(dev, &(vnet_state.devs), node) {
-       if (!memcmp(dev->mac_addr, mac, ETH_ALEN))
+       if (!compare_ethaddr(dev->mac_addr, mac)){
            return dev;
+       }
     }
 
     return NULL;
 }
 
 
+int v3_vnet_find_dev(uint8_t  * mac) {
+    struct vnet_dev * dev = NULL;
+
+    dev = dev_by_mac(mac);
+
+    if(dev != NULL) {
+       return dev->dev_id;
+    }
+
+    return -1;
+}
+
+
 int v3_vnet_add_route(struct v3_vnet_route route) {
     struct vnet_route_info * new_route = NULL;
     unsigned long flags; 
@@ -217,8 +235,10 @@ int v3_vnet_add_route(struct v3_vnet_route route) {
     new_route = (struct vnet_route_info *)V3_Malloc(sizeof(struct vnet_route_info));
     memset(new_route, 0, sizeof(struct vnet_route_info));
 
-    PrintDebug("VNET/P Core: add_route_entry: dst_id: %d, dst_type: %d\n",
-              route.dst_id, route.dst_type);   
+#ifdef CONFIG_DEBUG_VNET
+    PrintDebug("VNET/P Core: add_route_entry:\n");
+    print_route(&route);
+#endif
     
     memcpy(new_route->route_def.src_mac, route.src_mac, ETH_ALEN);
     memcpy(new_route->route_def.dst_mac, route.dst_mac, ETH_ALEN);
@@ -226,31 +246,18 @@ int v3_vnet_add_route(struct v3_vnet_route route) {
     new_route->route_def.dst_mac_qual = route.dst_mac_qual;
     new_route->route_def.dst_type = route.dst_type;
     new_route->route_def.src_type = route.src_type;
-       
-    if(route.dst_id == -1){
-       if (new_route->route_def.dst_type == LINK_INTERFACE) {
-           new_route->dst_dev = dev_by_mac(route.dst_mac);
-       }
-       new_route->route_def.dst_id = new_route->dst_dev->dev_id;
-    } else {
-       new_route->route_def.dst_id = route.dst_id;
-       if (new_route->route_def.dst_type == LINK_INTERFACE) {
-           new_route->dst_dev = dev_by_id(new_route->route_def.dst_id);
-       }
+    new_route->route_def.src_id = route.src_id;
+    new_route->route_def.dst_id = route.dst_id;
+
+    if (new_route->route_def.dst_type == LINK_INTERFACE) {
+       new_route->dst_dev = dev_by_id(new_route->route_def.dst_id);
     }
 
-    if(route.src_id == -1){
-       if (new_route->route_def.src_type == LINK_INTERFACE) {
-           new_route->src_dev = dev_by_mac(route.src_mac);
-       }
-       new_route->route_def.src_id = new_route->src_dev->dev_id;
-    } else {
-       new_route->route_def.src_id = route.src_id;
-       if (new_route->route_def.src_type == LINK_INTERFACE) {
-           new_route->src_dev = dev_by_id(new_route->route_def.src_id);
-       }
+    if (new_route->route_def.src_type == LINK_INTERFACE) {
+       new_route->src_dev = dev_by_id(new_route->route_def.src_id);
     }
 
+
     flags = v3_lock_irqsave(vnet_state.lock);
 
     list_add(&(new_route->node), &(vnet_state.routes));
@@ -299,8 +306,8 @@ static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
     int max_rank = 0;
     struct list_head match_list;
     struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
-    uint8_t src_type = pkt->src_type;
-    uint32_t src_link = pkt->src_id;
+//    uint8_t src_type = pkt->src_type;
+  //  uint32_t src_link = pkt->src_id;
 
 #ifdef CONFIG_DEBUG_VNET
     {
@@ -332,14 +339,15 @@ static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
     list_for_each_entry(route, &(vnet_state.routes), node) {
        struct v3_vnet_route * route_def = &(route->route_def);
 
+/*
        // CHECK SOURCE TYPE HERE
        if ( (route_def->src_type != LINK_ANY) && 
             ( (route_def->src_type != src_type) || 
               ( (route_def->src_id != src_link) &&
-                (route_def->src_id != (uint32_t)-1)))) {
+                (route_def->src_id != -1)))) {
            continue;
        }
-
+*/
 
        if ((route_def->dst_mac_qual == MAC_ANY) &&
            (route_def->src_mac_qual == MAC_ANY)) {      
@@ -428,12 +436,14 @@ int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
        PrintDebug("VNET/P Core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
                  cpu, pkt->size, pkt->src_id, 
                  pkt->src_type, pkt->dst_id, pkt->dst_type);
-       //v3_hexdump(pkt->data, pkt->size, NULL, 0);
    }
 #endif
 
     flags = v3_lock_irqsave(vnet_state.lock);
 
+    vnet_state.stats.rx_bytes += pkt->size;
+    vnet_state.stats.rx_pkts++;
+
     look_into_cache(pkt, &matched_routes);
     if (matched_routes == NULL) {  
        PrintDebug("VNET/P Core: send pkt Looking into routing table\n");
@@ -470,6 +480,8 @@ int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
                 PrintDebug("VNET/P Core: Packet not sent properly to bridge\n");
                 continue;
            }         
+           vnet_state.stats.tx_bytes += pkt->size;
+           vnet_state.stats.tx_pkts ++;
         } else if (route->route_def.dst_type == LINK_INTERFACE) {
             if (route->dst_dev == NULL || route->dst_dev->active == 0){
                PrintDebug("VNET/P Core: No active device to sent data to\n");
@@ -480,6 +492,8 @@ int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
                 PrintDebug("VNET/P Core: Packet not sent properly\n");
                 continue;
            }
+           vnet_state.stats.tx_bytes += pkt->size;
+           vnet_state.stats.tx_pkts ++;
         } else {
             PrintError("VNET/P Core: Wrong dst type\n");
         }
@@ -508,7 +522,6 @@ int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t * mac,
     new_dev->vm = vm;
     new_dev->dev_id = 0;
     new_dev->active = 1;
-    new_dev->mode = GUEST_DRIVERN;
 
     flags = v3_lock_irqsave(vnet_state.lock);
 
@@ -553,6 +566,15 @@ int v3_vnet_del_dev(int dev_id){
     return 0;
 }
 
+int v3_vnet_stat(struct vnet_stat * stats){
+       
+    stats->rx_bytes = vnet_state.stats.rx_bytes;
+    stats->rx_pkts = vnet_state.stats.rx_pkts;
+    stats->tx_bytes = vnet_state.stats.tx_bytes;
+    stats->tx_pkts = vnet_state.stats.tx_pkts;
+
+    return 0;
+}
 
 static void free_devices(){
     struct vnet_dev * dev = NULL; 
@@ -573,29 +595,6 @@ static void free_routes(){
     }
 }
 
-/* TODO: Round-bin or ?? */
-void  v3_vnet_poll(struct v3_vm_info * vm){
-    struct vnet_dev * dev = NULL; 
-    struct vnet_brg_dev *bridge = vnet_state.bridge;
-
-    list_for_each_entry(dev, &(vnet_state.devs), node) {
-       if(dev->mode == VMM_DRIVERN && 
-           dev->active && 
-           dev->vm == vm){
-           
-           dev->dev_ops.poll(vm, dev->private_data);
-       }
-    }
-
-    if (bridge != NULL && 
-         bridge->active && 
-         bridge->mode == VMM_DRIVERN) {
-       
-       bridge->brg_ops.poll(bridge->vm, bridge->private_data);
-    }
-       
-}
-
 int v3_vnet_add_bridge(struct v3_vm_info * vm,
                       struct v3_vnet_bridge_ops * ops,
                       uint8_t type,
@@ -631,7 +630,6 @@ int v3_vnet_add_bridge(struct v3_vm_info * vm,
     tmp_bridge->brg_ops.poll = ops->poll;
     tmp_bridge->private_data = priv_data;
     tmp_bridge->active = 1;
-    tmp_bridge->mode = GUEST_DRIVERN;
     tmp_bridge->type = type;
        
     /* make this atomic to avoid possible race conditions */
@@ -643,6 +641,20 @@ int v3_vnet_add_bridge(struct v3_vm_info * vm,
 }
 
 
+void v3_vnet_do_poll(struct v3_vm_info * vm){
+    struct vnet_dev * dev = NULL;
+
+    /* TODO: run this on separate threads
+      * round-robin schedule, with maximal budget for each poll
+      */
+    list_for_each_entry(dev, &(vnet_state.devs), node) {
+           if(dev->dev_ops.poll != NULL){
+               dev->dev_ops.poll(vm, -1, dev->private_data);
+           }
+    }
+}
+
+
 int v3_init_vnet() {
     memset(&vnet_state, 0, sizeof(vnet_state));