2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2010, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2009, Yuan Tang <ytang@northwestern.edu>
12 * Copyright (c) 2009, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Lei Xia <lxia@northwestern.edu>
16 * Yuan Tang <ytang@northwestern.edu>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <vnet/vnet.h>
23 #include <palacios/vm_guest_mem.h>
24 #include <palacios/vmm_lock.h>
25 #include <palacios/vmm_queue.h>
26 #include <palacios/vmm_sprintf.h>
27 #include <palacios/vmm_ethernet.h>
29 #ifndef V3_CONFIG_DEBUG_VNET
31 #define PrintDebug(fmt, args...)
37 uint8_t dst_mac[ETH_ALEN];
38 uint8_t src_mac[ETH_ALEN];
39 uint16_t type; /* indicates layer 3 protocol type */
40 } __attribute__((packed));
45 uint8_t mac_addr[ETH_ALEN];
46 struct v3_vm_info * vm;
47 struct v3_vnet_dev_ops dev_ops;
50 struct list_head node;
51 } __attribute__((packed));
55 struct v3_vm_info * vm;
56 struct v3_vnet_bridge_ops brg_ops;
61 } __attribute__((packed));
65 struct vnet_route_info {
66 struct v3_vnet_route route_def;
68 struct vnet_dev * dst_dev;
69 struct vnet_dev * src_dev;
71 struct list_head node;
72 struct list_head match_node; // used for route matching
77 uint8_t hash_buf[VNET_HASH_SIZE];
80 struct vnet_route_info * routes[0];
81 } __attribute__((packed));
86 struct v3_vnet_pkt pkt;
91 #define VNET_QUEUE_SIZE 1024
93 struct queue_entry buf[VNET_QUEUE_SIZE];
100 struct list_head routes;
101 struct list_head devs;
106 struct vnet_brg_dev * bridge;
109 struct vnet_stat stats;
111 struct vnet_thread * pkt_flush_thread;
113 struct vnet_queue pkt_q;
115 struct hashtable * route_cache;
119 #ifdef V3_CONFIG_DEBUG_VNET
120 static inline void mac_to_string(uint8_t * mac, char * buf) {
121 snprintf(buf, 100, "%2x:%2x:%2x:%2x:%2x:%2x",
122 mac[0], mac[1], mac[2],
123 mac[3], mac[4], mac[5]);
126 static void print_route(struct v3_vnet_route * route){
129 mac_to_string(route->src_mac, str);
130 PrintDebug("Src Mac (%s), src_qual (%d)\n",
131 str, route->src_mac_qual);
132 mac_to_string(route->dst_mac, str);
133 PrintDebug("Dst Mac (%s), dst_qual (%d)\n",
134 str, route->dst_mac_qual);
135 PrintDebug("Src dev id (%d), src type (%d)",
138 PrintDebug("Dst dev id (%d), dst type (%d)\n",
143 static void dump_routes(){
144 struct vnet_route_info *route;
147 PrintDebug("\n========Dump routes starts ============\n");
148 list_for_each_entry(route, &(vnet_state.routes), node) {
149 PrintDebug("\nroute %d:\n", i++);
151 print_route(&(route->route_def));
152 if (route->route_def.dst_type == LINK_INTERFACE) {
153 PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
155 route->dst_dev->dev_id,
156 (void *)&(route->dst_dev->dev_ops),
157 route->dst_dev->private_data);
161 PrintDebug("\n========Dump routes end ============\n");
168 * A VNET packet is a packed struct with the hashed fields grouped together.
169 * This means we can generate the hash from an offset into the pkt struct
171 static inline uint_t hash_fn(addr_t hdr_ptr) {
172 uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
174 return v3_hash_buffer(hdr_buf, VNET_HASH_SIZE);
177 static inline int hash_eq(addr_t key1, addr_t key2) {
178 return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
181 static int add_route_to_cache(const struct v3_vnet_pkt * pkt, struct route_list * routes) {
182 memcpy(routes->hash_buf, pkt->hash_buf, VNET_HASH_SIZE);
184 if (v3_htable_insert(vnet_state.route_cache, (addr_t)routes->hash_buf, (addr_t)routes) == 0) {
185 PrintError("VNET/P Core: Failed to insert new route entry to the cache\n");
192 static int clear_hash_cache() {
193 v3_free_htable(vnet_state.route_cache, 1, 1);
194 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
199 static int look_into_cache(const struct v3_vnet_pkt * pkt,
200 struct route_list ** routes) {
201 *routes = (struct route_list *)v3_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
207 static struct vnet_dev * dev_by_id(int idx) {
208 struct vnet_dev * dev = NULL;
210 list_for_each_entry(dev, &(vnet_state.devs), node) {
211 int dev_id = dev->dev_id;
220 static struct vnet_dev * dev_by_mac(uint8_t * mac) {
221 struct vnet_dev * dev = NULL;
223 list_for_each_entry(dev, &(vnet_state.devs), node) {
224 if (!compare_ethaddr(dev->mac_addr, mac)){
233 int v3_vnet_find_dev(uint8_t * mac) {
234 struct vnet_dev * dev = NULL;
236 dev = dev_by_mac(mac);
246 int v3_vnet_add_route(struct v3_vnet_route route) {
247 struct vnet_route_info * new_route = NULL;
250 new_route = (struct vnet_route_info *)V3_Malloc(sizeof(struct vnet_route_info));
251 memset(new_route, 0, sizeof(struct vnet_route_info));
253 #ifdef V3_CONFIG_DEBUG_VNET
254 PrintDebug("VNET/P Core: add_route_entry:\n");
258 memcpy(new_route->route_def.src_mac, route.src_mac, ETH_ALEN);
259 memcpy(new_route->route_def.dst_mac, route.dst_mac, ETH_ALEN);
260 new_route->route_def.src_mac_qual = route.src_mac_qual;
261 new_route->route_def.dst_mac_qual = route.dst_mac_qual;
262 new_route->route_def.dst_type = route.dst_type;
263 new_route->route_def.src_type = route.src_type;
264 new_route->route_def.src_id = route.src_id;
265 new_route->route_def.dst_id = route.dst_id;
267 if (new_route->route_def.dst_type == LINK_INTERFACE) {
268 new_route->dst_dev = dev_by_id(new_route->route_def.dst_id);
271 if (new_route->route_def.src_type == LINK_INTERFACE) {
272 new_route->src_dev = dev_by_id(new_route->route_def.src_id);
276 flags = v3_lock_irqsave(vnet_state.lock);
278 list_add(&(new_route->node), &(vnet_state.routes));
281 v3_unlock_irqrestore(vnet_state.lock, flags);
284 #ifdef V3_CONFIG_DEBUG_VNET
292 /* delete all route entries with specfied src or dst device id */
293 static void inline del_routes_by_dev(int dev_id){
294 struct vnet_route_info * route = NULL;
297 flags = v3_lock_irqsave(vnet_state.lock);
299 list_for_each_entry(route, &(vnet_state.routes), node) {
300 if((route->route_def.dst_type == LINK_INTERFACE &&
301 route->route_def.dst_id == dev_id) ||
302 (route->route_def.src_type == LINK_INTERFACE &&
303 route->route_def.src_id == dev_id)){
305 list_del(&(route->node));
306 list_del(&(route->match_node));
311 v3_unlock_irqrestore(vnet_state.lock, flags);
314 /* At the end allocate a route_list
315 * This list will be inserted into the cache so we don't need to free it
317 static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
318 struct vnet_route_info * route = NULL;
319 struct route_list * matches = NULL;
322 struct list_head match_list;
323 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
324 // uint8_t src_type = pkt->src_type;
325 // uint32_t src_link = pkt->src_id;
327 #ifdef V3_CONFIG_DEBUG_VNET
332 mac_to_string(hdr->src_mac, src_str);
333 mac_to_string(hdr->dst_mac, dst_str);
334 PrintDebug("VNET/P Core: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
338 INIT_LIST_HEAD(&match_list);
340 #define UPDATE_MATCHES(rank) do { \
341 if (max_rank < (rank)) { \
343 INIT_LIST_HEAD(&match_list); \
345 list_add(&(route->match_node), &match_list); \
347 } else if (max_rank == (rank)) { \
348 list_add(&(route->match_node), &match_list); \
354 list_for_each_entry(route, &(vnet_state.routes), node) {
355 struct v3_vnet_route * route_def = &(route->route_def);
358 // CHECK SOURCE TYPE HERE
359 if ( (route_def->src_type != LINK_ANY) &&
360 ( (route_def->src_type != src_type) ||
361 ( (route_def->src_id != src_link) &&
362 (route_def->src_id != -1)))) {
367 if ((route_def->dst_mac_qual == MAC_ANY) &&
368 (route_def->src_mac_qual == MAC_ANY)) {
372 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
373 if (route_def->src_mac_qual != MAC_NOT) {
374 if (route_def->dst_mac_qual == MAC_ANY) {
376 } else if (route_def->dst_mac_qual != MAC_NOT &&
377 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
383 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
384 if (route_def->dst_mac_qual != MAC_NOT) {
385 if (route_def->src_mac_qual == MAC_ANY) {
387 } else if ((route_def->src_mac_qual != MAC_NOT) &&
388 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
394 if ((route_def->dst_mac_qual == MAC_NOT) &&
395 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
396 if (route_def->src_mac_qual == MAC_ANY) {
398 } else if ((route_def->src_mac_qual != MAC_NOT) &&
399 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
404 if ((route_def->src_mac_qual == MAC_NOT) &&
405 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
406 if (route_def->dst_mac_qual == MAC_ANY) {
408 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
409 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
415 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
416 (route_def->dst_mac_qual == MAC_NONE)) {
421 PrintDebug("VNET/P Core: match_route: Matches=%d\n", num_matches);
423 if (num_matches == 0) {
427 matches = (struct route_list *)V3_Malloc(sizeof(struct route_list) +
428 (sizeof(struct vnet_route_info *) * num_matches));
430 matches->num_routes = num_matches;
434 list_for_each_entry(route, &match_list, match_node) {
435 matches->routes[i++] = route;
443 int vnet_tx_one_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
444 struct route_list * matched_routes = NULL;
448 int cpu = V3_Get_CPU();
449 V3_Net_Print(2, "VNET/P Core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
450 cpu, pkt->size, pkt->src_id,
451 pkt->src_type, pkt->dst_id, pkt->dst_type);
452 if(v3_net_debug >= 4){
453 v3_hexdump(pkt->data, pkt->size, NULL, 0);
456 flags = v3_lock_irqsave(vnet_state.lock);
458 vnet_state.stats.rx_bytes += pkt->size;
459 vnet_state.stats.rx_pkts++;
461 look_into_cache(pkt, &matched_routes);
462 if (matched_routes == NULL) {
463 PrintDebug("VNET/P Core: send pkt Looking into routing table\n");
465 matched_routes = match_route(pkt);
467 if (matched_routes) {
468 add_route_to_cache(pkt, matched_routes);
470 PrintDebug("VNET/P Core: Could not find route for packet... discards packet\n");
471 v3_unlock_irqrestore(vnet_state.lock, flags);
472 return 0; /* do we return -1 here?*/
476 v3_unlock_irqrestore(vnet_state.lock, flags);
478 PrintDebug("VNET/P Core: send pkt route matches %d\n", matched_routes->num_routes);
480 for (i = 0; i < matched_routes->num_routes; i++) {
481 struct vnet_route_info * route = matched_routes->routes[i];
483 if (route->route_def.dst_type == LINK_EDGE) {
484 struct vnet_brg_dev * bridge = vnet_state.bridge;
485 pkt->dst_type = LINK_EDGE;
486 pkt->dst_id = route->route_def.dst_id;
488 if (bridge == NULL) {
489 V3_Net_Print(2, "VNET/P Core: No active bridge to sent data to\n");
493 if(bridge->brg_ops.input(bridge->vm, pkt, bridge->private_data) < 0){
494 V3_Net_Print(2, "VNET/P Core: Packet not sent properly to bridge\n");
497 vnet_state.stats.tx_bytes += pkt->size;
498 vnet_state.stats.tx_pkts ++;
499 } else if (route->route_def.dst_type == LINK_INTERFACE) {
500 if (route->dst_dev == NULL){
501 V3_Net_Print(2, "VNET/P Core: No active device to sent data to\n");
505 if(route->dst_dev->dev_ops.input(route->dst_dev->vm, pkt, route->dst_dev->private_data) < 0) {
506 V3_Net_Print(2, "VNET/P Core: Packet not sent properly\n");
509 vnet_state.stats.tx_bytes += pkt->size;
510 vnet_state.stats.tx_pkts ++;
512 PrintError("VNET/P Core: Wrong dst type\n");
520 static int vnet_pkt_enqueue(struct v3_vnet_pkt * pkt){
522 struct queue_entry * entry;
523 struct vnet_queue * q = &(vnet_state.pkt_q);
526 flags = v3_lock_irqsave(q->lock);
528 if (q->count >= VNET_QUEUE_SIZE){
529 V3_Net_Print(1, "VNET Queue overflow!\n");
530 v3_unlock_irqrestore(q->lock, flags);
535 entry = &(q->buf[q->tail++]);
536 q->tail %= VNET_QUEUE_SIZE;
538 v3_unlock_irqrestore(q->lock, flags);
540 /* this is ugly, but should happen very unlikely */
543 if(entry->size_alloc < pkt->size){
544 if(entry->data != NULL){
545 V3_FreePages(V3_PAddr(entry->data), (entry->size_alloc / PAGE_SIZE));
549 num_pages = 1 + (pkt->size / PAGE_SIZE);
550 entry->data = V3_VAddr(V3_AllocPages(num_pages));
551 if(entry->data == NULL){
554 entry->size_alloc = PAGE_SIZE * num_pages;
557 entry->pkt.data = entry->data;
558 memcpy(&(entry->pkt), pkt, sizeof(struct v3_vnet_pkt));
559 memcpy(entry->data, pkt->data, pkt->size);
567 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data, int synchronize) {
569 vnet_tx_one_pkt(pkt, NULL);
571 vnet_pkt_enqueue(pkt);
572 V3_Net_Print(2, "VNET/P Core: Put pkt into Queue: pkt size %d\n", pkt->size);
578 int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t * mac,
579 struct v3_vnet_dev_ops *ops,
581 struct vnet_dev * new_dev = NULL;
584 new_dev = (struct vnet_dev *)V3_Malloc(sizeof(struct vnet_dev));
586 if (new_dev == NULL) {
587 PrintError("Malloc fails\n");
591 memcpy(new_dev->mac_addr, mac, 6);
592 new_dev->dev_ops.input = ops->input;
593 new_dev->private_data = priv_data;
597 flags = v3_lock_irqsave(vnet_state.lock);
599 if (dev_by_mac(mac) == NULL) {
600 list_add(&(new_dev->node), &(vnet_state.devs));
601 new_dev->dev_id = ++vnet_state.num_devs;
604 v3_unlock_irqrestore(vnet_state.lock, flags);
606 /* if the device was found previosly the id should still be 0 */
607 if (new_dev->dev_id == 0) {
608 PrintError("VNET/P Core: Device Already exists\n");
612 PrintDebug("VNET/P Core: Add Device: dev_id %d\n", new_dev->dev_id);
614 return new_dev->dev_id;
618 int v3_vnet_del_dev(int dev_id){
619 struct vnet_dev * dev = NULL;
622 flags = v3_lock_irqsave(vnet_state.lock);
624 dev = dev_by_id(dev_id);
626 list_del(&(dev->node));
627 del_routes_by_dev(dev_id);
630 v3_unlock_irqrestore(vnet_state.lock, flags);
634 PrintDebug("VNET/P Core: Remove Device: dev_id %d\n", dev_id);
640 int v3_vnet_stat(struct vnet_stat * stats){
642 stats->rx_bytes = vnet_state.stats.rx_bytes;
643 stats->rx_pkts = vnet_state.stats.rx_pkts;
644 stats->tx_bytes = vnet_state.stats.tx_bytes;
645 stats->tx_pkts = vnet_state.stats.tx_pkts;
650 static void free_devices(){
651 struct vnet_dev * dev = NULL;
653 list_for_each_entry(dev, &(vnet_state.devs), node) {
654 list_del(&(dev->node));
659 static void free_routes(){
660 struct vnet_route_info * route = NULL;
662 list_for_each_entry(route, &(vnet_state.routes), node) {
663 list_del(&(route->node));
664 list_del(&(route->match_node));
669 int v3_vnet_add_bridge(struct v3_vm_info * vm,
670 struct v3_vnet_bridge_ops * ops,
675 struct vnet_brg_dev * tmp_bridge = NULL;
677 flags = v3_lock_irqsave(vnet_state.lock);
678 if (vnet_state.bridge == NULL) {
680 vnet_state.bridge = (void *)1;
682 v3_unlock_irqrestore(vnet_state.lock, flags);
684 if (bridge_free == 0) {
685 PrintError("VNET/P Core: Bridge already set\n");
689 tmp_bridge = (struct vnet_brg_dev *)V3_Malloc(sizeof(struct vnet_brg_dev));
691 if (tmp_bridge == NULL) {
692 PrintError("Malloc Fails\n");
693 vnet_state.bridge = NULL;
698 tmp_bridge->brg_ops.input = ops->input;
699 tmp_bridge->brg_ops.poll = ops->poll;
700 tmp_bridge->private_data = priv_data;
701 tmp_bridge->type = type;
703 /* make this atomic to avoid possible race conditions */
704 flags = v3_lock_irqsave(vnet_state.lock);
705 vnet_state.bridge = tmp_bridge;
706 v3_unlock_irqrestore(vnet_state.lock, flags);
711 static int vnet_tx_flush(void *args){
713 struct queue_entry * entry;
714 struct vnet_queue * q = &(vnet_state.pkt_q);
716 V3_Print("VNET/P Handing Pkt Thread Starting ....\n");
719 /* we need thread sleep/wakeup in Palacios */
721 flags = v3_lock_irqsave(q->lock);
724 v3_unlock_irqrestore(q->lock, flags);
729 entry = &(q->buf[q->head++]);
730 q->head %= VNET_QUEUE_SIZE;
732 v3_unlock_irqrestore(q->lock, flags);
734 /* this is ugly, but should happen very unlikely */
736 vnet_tx_one_pkt(&(entry->pkt), NULL);
738 /* asynchronizely release allocated memory for buffer entry here */
741 V3_Net_Print(2, "vnet_tx_flush: pkt (size %d)\n", entry->pkt.size);
747 memset(&vnet_state, 0, sizeof(vnet_state));
749 INIT_LIST_HEAD(&(vnet_state.routes));
750 INIT_LIST_HEAD(&(vnet_state.devs));
752 vnet_state.num_devs = 0;
753 vnet_state.num_routes = 0;
755 if (v3_lock_init(&(vnet_state.lock)) == -1){
756 PrintError("VNET/P Core: Fails to initiate lock\n");
759 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
760 if (vnet_state.route_cache == NULL) {
761 PrintError("VNET/P Core: Fails to initiate route cache\n");
765 v3_lock_init(&(vnet_state.pkt_q.lock));
767 vnet_state.pkt_flush_thread = vnet_thread_create(vnet_tx_flush, NULL, "VNET_Pkts");
769 PrintDebug("VNET/P Core is initiated\n");
775 void v3_deinit_vnet(){
777 v3_lock_deinit(&(vnet_state.lock));
782 v3_free_htable(vnet_state.route_cache, 1, 1);
783 V3_Free(vnet_state.bridge);