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;
69 struct list_head node;
70 struct list_head match_node; // used for route matching
75 uint8_t hash_buf[VNET_HASH_SIZE];
78 struct vnet_route_info * routes[0];
79 } __attribute__((packed));
84 struct v3_vnet_pkt pkt;
89 #define VNET_QUEUE_SIZE 1024
91 struct queue_entry buf[VNET_QUEUE_SIZE];
98 struct list_head routes;
99 struct list_head devs;
104 struct vnet_brg_dev * bridge;
107 struct vnet_stat stats;
109 struct vnet_thread * pkt_flush_thread;
111 struct vnet_queue pkt_q;
113 struct hashtable * route_cache;
117 #ifdef V3_CONFIG_DEBUG_VNET
118 static inline void mac_to_string(uint8_t * mac, char * buf) {
119 snprintf(buf, 100, "%2x:%2x:%2x:%2x:%2x:%2x",
120 mac[0], mac[1], mac[2],
121 mac[3], mac[4], mac[5]);
124 static void print_route(struct v3_vnet_route * route){
127 mac_to_string(route->src_mac, str);
128 Vnet_Debug("Src Mac (%s), src_qual (%d)\n",
129 str, route->src_mac_qual);
130 mac_to_string(route->dst_mac, str);
131 Vnet_Debug("Dst Mac (%s), dst_qual (%d)\n",
132 str, route->dst_mac_qual);
133 Vnet_Debug("Src dev id (%d), src type (%d)",
136 Vnet_Debug("Dst dev id (%d), dst type (%d)\n",
141 static void dump_routes(){
142 struct vnet_route_info *route;
145 Vnet_Debug("\n========Dump routes starts ============\n");
146 list_for_each_entry(route, &(vnet_state.routes), node) {
147 Vnet_Debug("\nroute %d:\n", i++);
149 print_route(&(route->route_def));
150 if (route->route_def.dst_type == LINK_INTERFACE) {
151 Vnet_Debug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
153 route->dst_dev->dev_id,
154 (void *)&(route->dst_dev->dev_ops),
155 route->dst_dev->private_data);
159 Vnet_Debug("\n========Dump routes end ============\n");
166 * A VNET packet is a packed struct with the hashed fields grouped together.
167 * This means we can generate the hash from an offset into the pkt struct
169 static inline uint_t hash_fn(addr_t hdr_ptr) {
170 uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
172 return vnet_hash_buffer(hdr_buf, VNET_HASH_SIZE);
175 static inline int hash_eq(addr_t key1, addr_t key2) {
176 return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
179 static int add_route_to_cache(const struct v3_vnet_pkt * pkt, struct route_list * routes) {
180 memcpy(routes->hash_buf, pkt->hash_buf, VNET_HASH_SIZE);
182 if (vnet_htable_insert(vnet_state.route_cache, (addr_t)routes->hash_buf, (addr_t)routes) == 0) {
183 PrintError("VNET/P Core: Failed to insert new route entry to the cache\n");
190 static int clear_hash_cache() {
191 vnet_free_htable(vnet_state.route_cache, 1, 1);
192 vnet_state.route_cache = vnet_create_htable(0, &hash_fn, &hash_eq);
197 static int look_into_cache(const struct v3_vnet_pkt * pkt,
198 struct route_list ** routes) {
199 *routes = (struct route_list *)vnet_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
205 static struct vnet_dev * dev_by_id(int idx) {
206 struct vnet_dev * dev = NULL;
208 list_for_each_entry(dev, &(vnet_state.devs), node) {
209 int dev_id = dev->dev_id;
218 static struct vnet_dev * dev_by_mac(uint8_t * mac) {
219 struct vnet_dev * dev = NULL;
221 list_for_each_entry(dev, &(vnet_state.devs), node) {
222 if (!compare_ethaddr(dev->mac_addr, mac)){
231 int v3_vnet_find_dev(uint8_t * mac) {
232 struct vnet_dev * dev = NULL;
234 dev = dev_by_mac(mac);
244 int v3_vnet_add_route(struct v3_vnet_route route) {
245 struct vnet_route_info * new_route = NULL;
248 new_route = (struct vnet_route_info *)Vnet_Malloc(sizeof(struct vnet_route_info));
249 memset(new_route, 0, sizeof(struct vnet_route_info));
251 #ifdef V3_CONFIG_DEBUG_VNET
252 Vnet_Debug("VNET/P Core: add_route_entry:\n");
256 memcpy(new_route->route_def.src_mac, route.src_mac, ETH_ALEN);
257 memcpy(new_route->route_def.dst_mac, route.dst_mac, ETH_ALEN);
258 new_route->route_def.src_mac_qual = route.src_mac_qual;
259 new_route->route_def.dst_mac_qual = route.dst_mac_qual;
260 new_route->route_def.dst_type = route.dst_type;
261 new_route->route_def.src_type = route.src_type;
262 new_route->route_def.src_id = route.src_id;
263 new_route->route_def.dst_id = route.dst_id;
265 if (new_route->route_def.dst_type == LINK_INTERFACE) {
266 new_route->dst_dev = dev_by_id(new_route->route_def.dst_id);
269 if (new_route->route_def.src_type == LINK_INTERFACE) {
270 new_route->src_dev = dev_by_id(new_route->route_def.src_id);
274 flags = vnet_lock_irqsave(vnet_state.lock);
276 list_add(&(new_route->node), &(vnet_state.routes));
279 vnet_unlock_irqrestore(vnet_state.lock, flags);
282 #ifdef V3_CONFIG_DEBUG_VNET
290 /* delete all route entries with specfied src or dst device id */
291 static void inline del_routes_by_dev(int dev_id){
292 struct vnet_route_info * route = NULL;
295 flags = vnet_lock_irqsave(vnet_state.lock);
297 list_for_each_entry(route, &(vnet_state.routes), node) {
298 if((route->route_def.dst_type == LINK_INTERFACE &&
299 route->route_def.dst_id == dev_id) ||
300 (route->route_def.src_type == LINK_INTERFACE &&
301 route->route_def.src_id == dev_id)){
303 list_del(&(route->node));
304 list_del(&(route->match_node));
309 vnet_unlock_irqrestore(vnet_state.lock, flags);
312 /* At the end allocate a route_list
313 * This list will be inserted into the cache so we don't need to free it
315 static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
316 struct vnet_route_info * route = NULL;
317 struct route_list * matches = NULL;
320 struct list_head match_list;
321 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
322 // uint8_t src_type = pkt->src_type;
323 // uint32_t src_link = pkt->src_id;
325 #ifdef V3_CONFIG_DEBUG_VNET
330 mac_to_string(hdr->src_mac, src_str);
331 mac_to_string(hdr->dst_mac, dst_str);
332 Vnet_Debug("VNET/P Core: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
336 INIT_LIST_HEAD(&match_list);
338 #define UPDATE_MATCHES(rank) do { \
339 if (max_rank < (rank)) { \
341 INIT_LIST_HEAD(&match_list); \
343 list_add(&(route->match_node), &match_list); \
345 } else if (max_rank == (rank)) { \
346 list_add(&(route->match_node), &match_list); \
352 list_for_each_entry(route, &(vnet_state.routes), node) {
353 struct v3_vnet_route * route_def = &(route->route_def);
356 // CHECK SOURCE TYPE HERE
357 if ( (route_def->src_type != LINK_ANY) &&
358 ( (route_def->src_type != src_type) ||
359 ( (route_def->src_id != src_link) &&
360 (route_def->src_id != -1)))) {
365 if ((route_def->dst_mac_qual == MAC_ANY) &&
366 (route_def->src_mac_qual == MAC_ANY)) {
370 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
371 if (route_def->src_mac_qual != MAC_NOT) {
372 if (route_def->dst_mac_qual == MAC_ANY) {
374 } else if (route_def->dst_mac_qual != MAC_NOT &&
375 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
381 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
382 if (route_def->dst_mac_qual != MAC_NOT) {
383 if (route_def->src_mac_qual == MAC_ANY) {
385 } else if ((route_def->src_mac_qual != MAC_NOT) &&
386 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
392 if ((route_def->dst_mac_qual == MAC_NOT) &&
393 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
394 if (route_def->src_mac_qual == MAC_ANY) {
396 } else if ((route_def->src_mac_qual != MAC_NOT) &&
397 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
402 if ((route_def->src_mac_qual == MAC_NOT) &&
403 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
404 if (route_def->dst_mac_qual == MAC_ANY) {
406 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
407 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
413 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
414 (route_def->dst_mac_qual == MAC_NONE)) {
419 Vnet_Debug("VNET/P Core: match_route: Matches=%d\n", num_matches);
421 if (num_matches == 0) {
425 matches = (struct route_list *)Vnet_Malloc(sizeof(struct route_list) +
426 (sizeof(struct vnet_route_info *) * num_matches));
428 matches->num_routes = num_matches;
432 list_for_each_entry(route, &match_list, match_node) {
433 matches->routes[i++] = route;
441 int vnet_tx_one_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
442 struct route_list * matched_routes = NULL;
446 int cpu = V3_Get_CPU();
447 Vnet_Print(2, "VNET/P Core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
448 cpu, pkt->size, pkt->src_id,
449 pkt->src_type, pkt->dst_id, pkt->dst_type);
451 v3_hexdump(pkt->data, pkt->size, NULL, 0);
454 flags = vnet_lock_irqsave(vnet_state.lock);
456 vnet_state.stats.rx_bytes += pkt->size;
457 vnet_state.stats.rx_pkts++;
459 look_into_cache(pkt, &matched_routes);
460 if (matched_routes == NULL) {
461 Vnet_Debug("VNET/P Core: send pkt Looking into routing table\n");
463 matched_routes = match_route(pkt);
465 if (matched_routes) {
466 add_route_to_cache(pkt, matched_routes);
468 Vnet_Debug("VNET/P Core: Could not find route for packet... discards packet\n");
469 vnet_unlock_irqrestore(vnet_state.lock, flags);
470 return 0; /* do we return -1 here?*/
474 vnet_unlock_irqrestore(vnet_state.lock, flags);
476 Vnet_Debug("VNET/P Core: send pkt route matches %d\n", matched_routes->num_routes);
478 for (i = 0; i < matched_routes->num_routes; i++) {
479 struct vnet_route_info * route = matched_routes->routes[i];
481 if (route->route_def.dst_type == LINK_EDGE) {
482 struct vnet_brg_dev * bridge = vnet_state.bridge;
483 pkt->dst_type = LINK_EDGE;
484 pkt->dst_id = route->route_def.dst_id;
486 if (bridge == NULL) {
487 Vnet_Print(2, "VNET/P Core: No active bridge to sent data to\n");
491 if(bridge->brg_ops.input(bridge->vm, pkt, bridge->private_data) < 0){
492 Vnet_Print(2, "VNET/P Core: Packet not sent properly to bridge\n");
495 vnet_state.stats.tx_bytes += pkt->size;
496 vnet_state.stats.tx_pkts ++;
497 } else if (route->route_def.dst_type == LINK_INTERFACE) {
498 if (route->dst_dev == NULL){
499 Vnet_Print(2, "VNET/P Core: No active device to sent data to\n");
503 if(route->dst_dev->dev_ops.input(route->dst_dev->vm, pkt, route->dst_dev->private_data) < 0) {
504 Vnet_Print(2, "VNET/P Core: Packet not sent properly\n");
507 vnet_state.stats.tx_bytes += pkt->size;
508 vnet_state.stats.tx_pkts ++;
510 Vnet_Print(0, "VNET/P Core: Wrong dst type\n");
518 static int vnet_pkt_enqueue(struct v3_vnet_pkt * pkt){
520 struct queue_entry * entry;
521 struct vnet_queue * q = &(vnet_state.pkt_q);
524 flags = vnet_lock_irqsave(q->lock);
526 if (q->count >= VNET_QUEUE_SIZE){
527 Vnet_Print(1, "VNET Queue overflow!\n");
528 vnet_unlock_irqrestore(q->lock, flags);
533 entry = &(q->buf[q->tail++]);
534 q->tail %= VNET_QUEUE_SIZE;
536 vnet_unlock_irqrestore(q->lock, flags);
538 /* this is ugly, but should happen very unlikely */
541 if(entry->size_alloc < pkt->size){
542 if(entry->data != NULL){
543 Vnet_FreePages(Vnet_PAddr(entry->data), (entry->size_alloc / PAGE_SIZE));
547 num_pages = 1 + (pkt->size / PAGE_SIZE);
548 entry->data = Vnet_VAddr(Vnet_AllocPages(num_pages));
549 if(entry->data == NULL){
552 entry->size_alloc = PAGE_SIZE * num_pages;
555 entry->pkt.data = entry->data;
556 memcpy(&(entry->pkt), pkt, sizeof(struct v3_vnet_pkt));
557 memcpy(entry->data, pkt->data, pkt->size);
565 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data, int synchronize) {
567 vnet_tx_one_pkt(pkt, NULL);
569 vnet_pkt_enqueue(pkt);
570 Vnet_Print(2, "VNET/P Core: Put pkt into Queue: pkt size %d\n", pkt->size);
576 int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t * mac,
577 struct v3_vnet_dev_ops *ops,
579 struct vnet_dev * new_dev = NULL;
582 new_dev = (struct vnet_dev *)Vnet_Malloc(sizeof(struct vnet_dev));
584 if (new_dev == NULL) {
585 Vnet_Print(0, "Malloc fails\n");
589 memcpy(new_dev->mac_addr, mac, 6);
590 new_dev->dev_ops.input = ops->input;
591 new_dev->private_data = priv_data;
595 flags = vnet_lock_irqsave(vnet_state.lock);
597 if (dev_by_mac(mac) == NULL) {
598 list_add(&(new_dev->node), &(vnet_state.devs));
599 new_dev->dev_id = ++vnet_state.num_devs;
602 vnet_unlock_irqrestore(vnet_state.lock, flags);
604 /* if the device was found previosly the id should still be 0 */
605 if (new_dev->dev_id == 0) {
606 Vnet_Print(0, "VNET/P Core: Device Already exists\n");
610 Vnet_Debug("VNET/P Core: Add Device: dev_id %d\n", new_dev->dev_id);
612 return new_dev->dev_id;
616 int v3_vnet_del_dev(int dev_id){
617 struct vnet_dev * dev = NULL;
620 flags = vnet_lock_irqsave(vnet_state.lock);
622 dev = dev_by_id(dev_id);
624 list_del(&(dev->node));
625 del_routes_by_dev(dev_id);
628 vnet_unlock_irqrestore(vnet_state.lock, flags);
632 Vnet_Debug("VNET/P Core: Remove Device: dev_id %d\n", dev_id);
638 int v3_vnet_stat(struct vnet_stat * stats){
640 stats->rx_bytes = vnet_state.stats.rx_bytes;
641 stats->rx_pkts = vnet_state.stats.rx_pkts;
642 stats->tx_bytes = vnet_state.stats.tx_bytes;
643 stats->tx_pkts = vnet_state.stats.tx_pkts;
648 static void free_devices(){
649 struct vnet_dev * dev = NULL;
651 list_for_each_entry(dev, &(vnet_state.devs), node) {
652 list_del(&(dev->node));
657 static void free_routes(){
658 struct vnet_route_info * route = NULL;
660 list_for_each_entry(route, &(vnet_state.routes), node) {
661 list_del(&(route->node));
662 list_del(&(route->match_node));
667 int v3_vnet_add_bridge(struct v3_vm_info * vm,
668 struct v3_vnet_bridge_ops * ops,
673 struct vnet_brg_dev * tmp_bridge = NULL;
675 flags = vnet_lock_irqsave(vnet_state.lock);
676 if (vnet_state.bridge == NULL) {
678 vnet_state.bridge = (void *)1;
680 vnet_unlock_irqrestore(vnet_state.lock, flags);
682 if (bridge_free == 0) {
683 PrintError("VNET/P Core: Bridge already set\n");
687 tmp_bridge = (struct vnet_brg_dev *)Vnet_Malloc(sizeof(struct vnet_brg_dev));
689 if (tmp_bridge == NULL) {
690 PrintError("Malloc Fails\n");
691 vnet_state.bridge = NULL;
696 tmp_bridge->brg_ops.input = ops->input;
697 tmp_bridge->brg_ops.poll = ops->poll;
698 tmp_bridge->private_data = priv_data;
699 tmp_bridge->type = type;
701 /* make this atomic to avoid possible race conditions */
702 flags = vnet_lock_irqsave(vnet_state.lock);
703 vnet_state.bridge = tmp_bridge;
704 vnet_unlock_irqrestore(vnet_state.lock, flags);
709 static int vnet_tx_flush(void *args){
711 struct queue_entry * entry;
712 struct vnet_queue * q = &(vnet_state.pkt_q);
714 Vnet_Print(0, "VNET/P Handing Pkt Thread Starting ....\n");
716 /* we need thread sleep/wakeup in Palacios */
717 while(!vnet_thread_should_stop()){
718 flags = vnet_lock_irqsave(q->lock);
721 vnet_unlock_irqrestore(q->lock, flags);
725 entry = &(q->buf[q->head++]);
726 q->head %= VNET_QUEUE_SIZE;
728 vnet_unlock_irqrestore(q->lock, flags);
730 /* this is ugly, but should happen very unlikely */
732 vnet_tx_one_pkt(&(entry->pkt), NULL);
734 /* asynchronizely release allocated memory for buffer entry here */
737 Vnet_Print(2, "vnet_tx_flush: pkt (size %d)\n", entry->pkt.size);
745 memset(&vnet_state, 0, sizeof(vnet_state));
747 INIT_LIST_HEAD(&(vnet_state.routes));
748 INIT_LIST_HEAD(&(vnet_state.devs));
750 vnet_state.num_devs = 0;
751 vnet_state.num_routes = 0;
753 if (vnet_lock_init(&(vnet_state.lock)) == -1){
754 PrintError("VNET/P Core: Fails to initiate lock\n");
757 vnet_state.route_cache = vnet_create_htable(0, &hash_fn, &hash_eq);
758 if (vnet_state.route_cache == NULL) {
759 PrintError("VNET/P Core: Fails to initiate route cache\n");
763 vnet_lock_init(&(vnet_state.pkt_q.lock));
765 vnet_state.pkt_flush_thread = vnet_start_thread(vnet_tx_flush, NULL, "VNET_Pkts");
767 Vnet_Debug("VNET/P Core is initiated\n");
773 void v3_deinit_vnet(){
775 vnet_lock_deinit(&(vnet_state.lock));
780 vnet_free_htable(vnet_state.route_cache, 1, 1);
781 Vnet_Free(vnet_state.bridge);