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 <vnet/vnet_hashtable.h>
24 #include <vnet/vnet_host.h>
25 #include <vnet/vnet_vmm.h>
27 #ifndef V3_CONFIG_DEBUG_VNET
29 #define Vnet_Debug(fmt, args...)
35 uint8_t dst_mac[ETH_ALEN];
36 uint8_t src_mac[ETH_ALEN];
37 uint16_t type; /* indicates layer 3 protocol type */
38 } __attribute__((packed));
43 uint8_t mac_addr[ETH_ALEN];
44 struct v3_vm_info * vm;
45 struct v3_vnet_dev_ops dev_ops;
48 struct list_head node;
49 } __attribute__((packed));
53 struct v3_vm_info * vm;
54 struct v3_vnet_bridge_ops brg_ops;
59 } __attribute__((packed));
63 struct vnet_route_info {
64 struct v3_vnet_route route_def;
66 struct vnet_dev * dst_dev;
67 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;
108 struct vnet_brg_dev * bridge;
111 struct vnet_stat stats;
113 struct vnet_thread * pkt_flush_thread;
115 struct vnet_queue pkt_q;
117 struct hashtable * route_cache;
121 #ifdef V3_CONFIG_DEBUG_VNET
122 static inline void mac2str(uint8_t * mac, char * buf) {
123 snprintf(buf, 100, "%2x:%2x:%2x:%2x:%2x:%2x",
124 mac[0], mac[1], mac[2],
125 mac[3], mac[4], mac[5]);
128 static void print_route(struct v3_vnet_route * route){
131 mac2str(route->src_mac, str);
132 Vnet_Debug("Src Mac (%s), src_qual (%d)\n",
133 str, route->src_mac_qual);
134 mac2str(route->dst_mac, str);
135 Vnet_Debug("Dst Mac (%s), dst_qual (%d)\n",
136 str, route->dst_mac_qual);
137 Vnet_Debug("Src dev id (%d), src type (%d)",
140 Vnet_Debug("Dst dev id (%d), dst type (%d)\n",
145 static void dump_routes(){
146 struct vnet_route_info *route;
149 Vnet_Debug("\n========Dump routes starts ============\n");
150 list_for_each_entry(route, &(vnet_state.routes), node) {
151 Vnet_Debug("\nroute %d:\n", i++);
153 print_route(&(route->route_def));
154 if (route->route_def.dst_type == LINK_INTERFACE) {
155 Vnet_Debug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
157 route->dst_dev->dev_id,
158 (void *)&(route->dst_dev->dev_ops),
159 route->dst_dev->private_data);
163 Vnet_Debug("\n========Dump routes end ============\n");
170 * A VNET packet is a packed struct with the hashed fields grouped together.
171 * This means we can generate the hash from an offset into the pkt struct
173 static inline uint_t hash_fn(addr_t hdr_ptr) {
174 uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
176 return vnet_hash_buffer(hdr_buf, VNET_HASH_SIZE);
179 static inline int hash_eq(addr_t key1, addr_t key2) {
180 return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
183 static int add_route_to_cache(const struct v3_vnet_pkt * pkt, struct route_list * routes) {
184 memcpy(routes->hash_buf, pkt->hash_buf, VNET_HASH_SIZE);
186 if (vnet_htable_insert(vnet_state.route_cache, (addr_t)routes->hash_buf, (addr_t)routes) == 0) {
187 PrintError("VNET/P Core: Failed to insert new route entry to the cache\n");
194 static int clear_hash_cache() {
195 vnet_free_htable(vnet_state.route_cache, 1, 1);
196 vnet_state.route_cache = vnet_create_htable(0, &hash_fn, &hash_eq);
201 static int look_into_cache(const struct v3_vnet_pkt * pkt,
202 struct route_list ** routes) {
203 *routes = (struct route_list *)vnet_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
209 static struct vnet_dev * dev_by_id(int idx) {
210 struct vnet_dev * dev = NULL;
212 list_for_each_entry(dev, &(vnet_state.devs), node) {
213 int dev_id = dev->dev_id;
222 static struct vnet_dev * dev_by_mac(uint8_t * mac) {
223 struct vnet_dev * dev = NULL;
225 list_for_each_entry(dev, &(vnet_state.devs), node) {
226 if (!compare_ethaddr(dev->mac_addr, mac)){
235 int v3_vnet_find_dev(uint8_t * mac) {
236 struct vnet_dev * dev = NULL;
238 dev = dev_by_mac(mac);
248 int v3_vnet_add_route(struct v3_vnet_route route) {
249 struct vnet_route_info * new_route = NULL;
252 new_route = (struct vnet_route_info *)Vnet_Malloc(sizeof(struct vnet_route_info));
253 memset(new_route, 0, sizeof(struct vnet_route_info));
255 #ifdef V3_CONFIG_DEBUG_VNET
256 Vnet_Debug("VNET/P Core: add_route_entry:\n");
260 memcpy(new_route->route_def.src_mac, route.src_mac, ETH_ALEN);
261 memcpy(new_route->route_def.dst_mac, route.dst_mac, ETH_ALEN);
262 new_route->route_def.src_mac_qual = route.src_mac_qual;
263 new_route->route_def.dst_mac_qual = route.dst_mac_qual;
264 new_route->route_def.dst_type = route.dst_type;
265 new_route->route_def.src_type = route.src_type;
266 new_route->route_def.src_id = route.src_id;
267 new_route->route_def.dst_id = route.dst_id;
269 if (new_route->route_def.dst_type == LINK_INTERFACE) {
270 new_route->dst_dev = dev_by_id(new_route->route_def.dst_id);
273 if (new_route->route_def.src_type == LINK_INTERFACE) {
274 new_route->src_dev = dev_by_id(new_route->route_def.src_id);
278 flags = vnet_lock_irqsave(vnet_state.lock);
280 list_add(&(new_route->node), &(vnet_state.routes));
281 new_route->idx = ++ vnet_state.route_idx;
282 vnet_state.num_routes ++;
284 vnet_unlock_irqrestore(vnet_state.lock, flags);
288 #ifdef V3_CONFIG_DEBUG_VNET
292 return new_route->idx;
296 void v3_vnet_del_route(uint32_t route_idx){
297 struct vnet_route_info * route = NULL;
300 flags = vnet_lock_irqsave(vnet_state.lock);
302 list_for_each_entry(route, &(vnet_state.routes), node) {
303 if(route->idx == route_idx){
304 list_del(&(route->node));
305 list_del(&(route->match_node));
310 vnet_unlock_irqrestore(vnet_state.lock, flags);
314 /* delete all route entries with specfied src or dst device id */
315 static void inline del_routes_by_dev(int dev_id){
316 struct vnet_route_info * route = NULL;
319 flags = vnet_lock_irqsave(vnet_state.lock);
321 list_for_each_entry(route, &(vnet_state.routes), node) {
322 if((route->route_def.dst_type == LINK_INTERFACE &&
323 route->route_def.dst_id == dev_id) ||
324 (route->route_def.src_type == LINK_INTERFACE &&
325 route->route_def.src_id == dev_id)){
327 list_del(&(route->node));
328 list_del(&(route->match_node));
333 vnet_unlock_irqrestore(vnet_state.lock, flags);
336 /* At the end allocate a route_list
337 * This list will be inserted into the cache so we don't need to free it
339 static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
340 struct vnet_route_info * route = NULL;
341 struct route_list * matches = NULL;
344 struct list_head match_list;
345 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
346 // uint8_t src_type = pkt->src_type;
347 // uint32_t src_link = pkt->src_id;
349 #ifdef V3_CONFIG_DEBUG_VNET
354 mac2str(hdr->src_mac, src_str);
355 mac2str(hdr->dst_mac, dst_str);
356 Vnet_Debug("VNET/P Core: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
360 INIT_LIST_HEAD(&match_list);
362 #define UPDATE_MATCHES(rank) do { \
363 if (max_rank < (rank)) { \
365 INIT_LIST_HEAD(&match_list); \
367 list_add(&(route->match_node), &match_list); \
369 } else if (max_rank == (rank)) { \
370 list_add(&(route->match_node), &match_list); \
376 list_for_each_entry(route, &(vnet_state.routes), node) {
377 struct v3_vnet_route * route_def = &(route->route_def);
380 // CHECK SOURCE TYPE HERE
381 if ( (route_def->src_type != LINK_ANY) &&
382 ( (route_def->src_type != src_type) ||
383 ( (route_def->src_id != src_link) &&
384 (route_def->src_id != -1)))) {
389 if ((route_def->dst_mac_qual == MAC_ANY) &&
390 (route_def->src_mac_qual == MAC_ANY)) {
394 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
395 if (route_def->src_mac_qual != MAC_NOT) {
396 if (route_def->dst_mac_qual == MAC_ANY) {
398 } else if (route_def->dst_mac_qual != MAC_NOT &&
399 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
405 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
406 if (route_def->dst_mac_qual != MAC_NOT) {
407 if (route_def->src_mac_qual == MAC_ANY) {
409 } else if ((route_def->src_mac_qual != MAC_NOT) &&
410 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
416 if ((route_def->dst_mac_qual == MAC_NOT) &&
417 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
418 if (route_def->src_mac_qual == MAC_ANY) {
420 } else if ((route_def->src_mac_qual != MAC_NOT) &&
421 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
426 if ((route_def->src_mac_qual == MAC_NOT) &&
427 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
428 if (route_def->dst_mac_qual == MAC_ANY) {
430 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
431 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
437 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
438 (route_def->dst_mac_qual == MAC_NONE)) {
443 Vnet_Debug("VNET/P Core: match_route: Matches=%d\n", num_matches);
445 if (num_matches == 0) {
449 matches = (struct route_list *)Vnet_Malloc(sizeof(struct route_list) +
450 (sizeof(struct vnet_route_info *) * num_matches));
452 matches->num_routes = num_matches;
456 list_for_each_entry(route, &match_list, match_node) {
457 matches->routes[i++] = route;
465 int vnet_tx_one_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
466 struct route_list * matched_routes = NULL;
470 int cpu = V3_Get_CPU();
471 Vnet_Print(2, "VNET/P Core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
472 cpu, pkt->size, pkt->src_id,
473 pkt->src_type, pkt->dst_id, pkt->dst_type);
475 v3_hexdump(pkt->data, pkt->size, NULL, 0);
478 flags = vnet_lock_irqsave(vnet_state.lock);
480 vnet_state.stats.rx_bytes += pkt->size;
481 vnet_state.stats.rx_pkts++;
483 look_into_cache(pkt, &matched_routes);
484 if (matched_routes == NULL) {
485 Vnet_Debug("VNET/P Core: send pkt Looking into routing table\n");
487 matched_routes = match_route(pkt);
489 if (matched_routes) {
490 add_route_to_cache(pkt, matched_routes);
492 Vnet_Debug("VNET/P Core: Could not find route for packet... discards packet\n");
493 vnet_unlock_irqrestore(vnet_state.lock, flags);
494 return 0; /* do we return -1 here?*/
498 vnet_unlock_irqrestore(vnet_state.lock, flags);
500 Vnet_Debug("VNET/P Core: send pkt route matches %d\n", matched_routes->num_routes);
502 for (i = 0; i < matched_routes->num_routes; i++) {
503 struct vnet_route_info * route = matched_routes->routes[i];
505 if (route->route_def.dst_type == LINK_EDGE) {
506 struct vnet_brg_dev * bridge = vnet_state.bridge;
507 pkt->dst_type = LINK_EDGE;
508 pkt->dst_id = route->route_def.dst_id;
510 if (bridge == NULL) {
511 Vnet_Print(2, "VNET/P Core: No active bridge to sent data to\n");
515 if(bridge->brg_ops.input(bridge->vm, pkt, bridge->private_data) < 0){
516 Vnet_Print(2, "VNET/P Core: Packet not sent properly to bridge\n");
519 vnet_state.stats.tx_bytes += pkt->size;
520 vnet_state.stats.tx_pkts ++;
521 } else if (route->route_def.dst_type == LINK_INTERFACE) {
522 if (route->dst_dev == NULL){
523 Vnet_Print(2, "VNET/P Core: No active device to sent data to\n");
527 if(route->dst_dev->dev_ops.input(route->dst_dev->vm, pkt, route->dst_dev->private_data) < 0) {
528 Vnet_Print(2, "VNET/P Core: Packet not sent properly\n");
531 vnet_state.stats.tx_bytes += pkt->size;
532 vnet_state.stats.tx_pkts ++;
534 Vnet_Print(0, "VNET/P Core: Wrong dst type\n");
542 static int vnet_pkt_enqueue(struct v3_vnet_pkt * pkt){
544 struct queue_entry * entry;
545 struct vnet_queue * q = &(vnet_state.pkt_q);
548 flags = vnet_lock_irqsave(q->lock);
550 if (q->count >= VNET_QUEUE_SIZE){
551 Vnet_Print(1, "VNET Queue overflow!\n");
552 vnet_unlock_irqrestore(q->lock, flags);
557 entry = &(q->buf[q->tail++]);
558 q->tail %= VNET_QUEUE_SIZE;
560 vnet_unlock_irqrestore(q->lock, flags);
562 /* this is ugly, but should happen very unlikely */
565 if(entry->size_alloc < pkt->size){
566 if(entry->data != NULL){
567 Vnet_FreePages(Vnet_PAddr(entry->data), (entry->size_alloc / PAGE_SIZE));
571 num_pages = 1 + (pkt->size / PAGE_SIZE);
572 entry->data = Vnet_VAddr(Vnet_AllocPages(num_pages));
573 if(entry->data == NULL){
576 entry->size_alloc = PAGE_SIZE * num_pages;
579 entry->pkt.data = entry->data;
580 memcpy(&(entry->pkt), pkt, sizeof(struct v3_vnet_pkt));
581 memcpy(entry->data, pkt->data, pkt->size);
589 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data, int synchronize) {
591 vnet_tx_one_pkt(pkt, NULL);
593 vnet_pkt_enqueue(pkt);
594 Vnet_Print(2, "VNET/P Core: Put pkt into Queue: pkt size %d\n", pkt->size);
600 int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t * mac,
601 struct v3_vnet_dev_ops *ops,
603 struct vnet_dev * new_dev = NULL;
606 new_dev = (struct vnet_dev *)Vnet_Malloc(sizeof(struct vnet_dev));
608 if (new_dev == NULL) {
609 Vnet_Print(0, "Malloc fails\n");
613 memcpy(new_dev->mac_addr, mac, 6);
614 new_dev->dev_ops.input = ops->input;
615 new_dev->private_data = priv_data;
619 flags = vnet_lock_irqsave(vnet_state.lock);
621 if (dev_by_mac(mac) == NULL) {
622 list_add(&(new_dev->node), &(vnet_state.devs));
623 new_dev->dev_id = ++ vnet_state.dev_idx;
624 vnet_state.num_devs ++;
627 vnet_unlock_irqrestore(vnet_state.lock, flags);
629 /* if the device was found previosly the id should still be 0 */
630 if (new_dev->dev_id == 0) {
631 Vnet_Print(0, "VNET/P Core: Device Already exists\n");
635 Vnet_Debug("VNET/P Core: Add Device: dev_id %d\n", new_dev->dev_id);
637 return new_dev->dev_id;
641 int v3_vnet_del_dev(int dev_id){
642 struct vnet_dev * dev = NULL;
645 flags = vnet_lock_irqsave(vnet_state.lock);
647 dev = dev_by_id(dev_id);
649 list_del(&(dev->node));
650 //del_routes_by_dev(dev_id);
651 vnet_state.num_devs --;
654 vnet_unlock_irqrestore(vnet_state.lock, flags);
658 Vnet_Debug("VNET/P Core: Remove Device: dev_id %d\n", dev_id);
664 int v3_vnet_stat(struct vnet_stat * stats){
665 stats->rx_bytes = vnet_state.stats.rx_bytes;
666 stats->rx_pkts = vnet_state.stats.rx_pkts;
667 stats->tx_bytes = vnet_state.stats.tx_bytes;
668 stats->tx_pkts = vnet_state.stats.tx_pkts;
673 static void deinit_devices_list(){
674 struct vnet_dev * dev = NULL;
676 list_for_each_entry(dev, &(vnet_state.devs), node) {
677 list_del(&(dev->node));
682 static void deinit_routes_list(){
683 struct vnet_route_info * route = NULL;
685 list_for_each_entry(route, &(vnet_state.routes), node) {
686 list_del(&(route->node));
687 list_del(&(route->match_node));
692 int v3_vnet_add_bridge(struct v3_vm_info * vm,
693 struct v3_vnet_bridge_ops * ops,
698 struct vnet_brg_dev * tmp_bridge = NULL;
700 flags = vnet_lock_irqsave(vnet_state.lock);
701 if (vnet_state.bridge == NULL) {
703 vnet_state.bridge = (void *)1;
705 vnet_unlock_irqrestore(vnet_state.lock, flags);
707 if (bridge_free == 0) {
708 PrintError("VNET/P Core: Bridge already set\n");
712 tmp_bridge = (struct vnet_brg_dev *)Vnet_Malloc(sizeof(struct vnet_brg_dev));
714 if (tmp_bridge == NULL) {
715 PrintError("Malloc Fails\n");
716 vnet_state.bridge = NULL;
721 tmp_bridge->brg_ops.input = ops->input;
722 tmp_bridge->brg_ops.poll = ops->poll;
723 tmp_bridge->private_data = priv_data;
724 tmp_bridge->type = type;
726 /* make this atomic to avoid possible race conditions */
727 flags = vnet_lock_irqsave(vnet_state.lock);
728 vnet_state.bridge = tmp_bridge;
729 vnet_unlock_irqrestore(vnet_state.lock, flags);
735 void v3_vnet_del_bridge(uint8_t type) {
737 struct vnet_brg_dev * tmp_bridge = NULL;
739 flags = vnet_lock_irqsave(vnet_state.lock);
741 if (vnet_state.bridge != NULL && vnet_state.bridge->type == type) {
742 tmp_bridge = vnet_state.bridge;
743 vnet_state.bridge = NULL;
746 vnet_unlock_irqrestore(vnet_state.lock, flags);
749 Vnet_Free(tmp_bridge);
754 static int vnet_tx_flush(void *args){
756 struct queue_entry * entry;
757 struct vnet_queue * q = &(vnet_state.pkt_q);
759 Vnet_Print(0, "VNET/P Handing Pkt Thread Starting ....\n");
761 /* we need thread sleep/wakeup in Palacios */
762 while(!vnet_thread_should_stop()){
763 flags = vnet_lock_irqsave(q->lock);
766 vnet_unlock_irqrestore(q->lock, flags);
770 entry = &(q->buf[q->head++]);
771 q->head %= VNET_QUEUE_SIZE;
773 vnet_unlock_irqrestore(q->lock, flags);
775 /* this is ugly, but should happen very unlikely */
777 vnet_tx_one_pkt(&(entry->pkt), NULL);
779 /* asynchronizely release allocated memory for buffer entry here */
782 Vnet_Print(2, "vnet_tx_flush: pkt (size %d)\n", entry->pkt.size);
790 memset(&vnet_state, 0, sizeof(vnet_state));
792 INIT_LIST_HEAD(&(vnet_state.routes));
793 INIT_LIST_HEAD(&(vnet_state.devs));
795 vnet_state.num_devs = 0;
796 vnet_state.num_routes = 0;
798 if (vnet_lock_init(&(vnet_state.lock)) == -1){
799 PrintError("VNET/P Core: Fails to initiate lock\n");
802 vnet_state.route_cache = vnet_create_htable(0, &hash_fn, &hash_eq);
803 if (vnet_state.route_cache == NULL) {
804 PrintError("VNET/P Core: Fails to initiate route cache\n");
808 vnet_lock_init(&(vnet_state.pkt_q.lock));
810 vnet_state.pkt_flush_thread = vnet_start_thread(vnet_tx_flush, NULL, "VNET_Pkts");
812 Vnet_Debug("VNET/P Core is initiated\n");
818 void v3_deinit_vnet(){
820 vnet_lock_deinit(&(vnet_state.lock));
822 deinit_devices_list();
823 deinit_routes_list();
825 vnet_free_htable(vnet_state.route_cache, 1, 1);
826 Vnet_Free(vnet_state.bridge);