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>
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 #include <palacios/vmm_queue.h>
29 #ifndef V3_CONFIG_DEBUG_VNET
31 #define Vnet_Debug(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;
51 #define VNET_MAX_QUOTE 64
56 struct list_head node;
\r
57 } __attribute__((packed));
61 struct v3_vm_info * vm;
62 struct v3_vnet_bridge_ops brg_ops;
67 } __attribute__((packed));
71 struct vnet_route_info {
72 struct v3_vnet_route route_def;
74 struct vnet_dev * dst_dev;
75 struct vnet_dev * src_dev;
79 struct list_head node;
80 struct list_head match_node; // used for route matching
85 uint8_t hash_buf[VNET_HASH_SIZE];
88 struct vnet_route_info * routes[0];
89 } __attribute__((packed));
94 struct v3_vnet_pkt pkt;
101 struct list_head routes;
102 struct list_head devs;
111 struct vnet_brg_dev * bridge;
114 struct vnet_stat stats;
116 /* device queue that are waiting to be polled */
117 struct v3_queue * poll_devs;
119 struct vnet_thread * pkt_flush_thread;
121 struct hashtable * route_cache;
125 #ifdef V3_CONFIG_DEBUG_VNET
126 static inline void mac2str(uint8_t * mac, char * buf) {
127 snprintf(buf, 100, "%2x:%2x:%2x:%2x:%2x:%2x",
128 mac[0], mac[1], mac[2],
129 mac[3], mac[4], mac[5]);
132 static void print_route(struct v3_vnet_route * route){
135 mac2str(route->src_mac, str);
136 Vnet_Debug("Src Mac (%s), src_qual (%d)\n",
137 str, route->src_mac_qual);
138 mac2str(route->dst_mac, str);
139 Vnet_Debug("Dst Mac (%s), dst_qual (%d)\n",
140 str, route->dst_mac_qual);
141 Vnet_Debug("Src dev id (%d), src type (%d)",
144 Vnet_Debug("Dst dev id (%d), dst type (%d)\n",
149 static void dump_routes(){
150 struct vnet_route_info *route;
152 Vnet_Debug("\n========Dump routes starts ============\n");
153 list_for_each_entry(route, &(vnet_state.routes), node) {
154 Vnet_Debug("\nroute %d:\n", route->idx);
156 print_route(&(route->route_def));
157 if (route->route_def.dst_type == LINK_INTERFACE) {
158 Vnet_Debug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
160 route->dst_dev->dev_id,
161 (void *)&(route->dst_dev->dev_ops),
162 route->dst_dev->private_data);
166 Vnet_Debug("\n========Dump routes end ============\n");
173 * A VNET packet is a packed struct with the hashed fields grouped together.
174 * This means we can generate the hash from an offset into the pkt struct
176 static inline uint_t hash_fn(addr_t hdr_ptr) {
177 uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
179 return vnet_hash_buffer(hdr_buf, VNET_HASH_SIZE);
182 static inline int hash_eq(addr_t key1, addr_t key2) {
183 return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
186 static int add_route_to_cache(const struct v3_vnet_pkt * pkt, struct route_list * routes) {
187 memcpy(routes->hash_buf, pkt->hash_buf, VNET_HASH_SIZE);
189 if (vnet_htable_insert(vnet_state.route_cache, (addr_t)routes->hash_buf, (addr_t)routes) == 0) {
190 PrintError("VNET/P Core: Failed to insert new route entry to the cache\n");
197 static int clear_hash_cache() {
198 vnet_free_htable(vnet_state.route_cache, 1, 1);
199 vnet_state.route_cache = vnet_create_htable(0, &hash_fn, &hash_eq);
204 static int look_into_cache(const struct v3_vnet_pkt * pkt,
205 struct route_list ** routes) {
206 *routes = (struct route_list *)vnet_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
212 static struct vnet_dev * dev_by_id(int idx) {
213 struct vnet_dev * dev = NULL;
215 list_for_each_entry(dev, &(vnet_state.devs), node) {
216 int dev_id = dev->dev_id;
225 static struct vnet_dev * dev_by_mac(uint8_t * mac) {
226 struct vnet_dev * dev = NULL;
228 list_for_each_entry(dev, &(vnet_state.devs), node) {
229 if (!compare_ethaddr(dev->mac_addr, mac)){
238 int v3_vnet_find_dev(uint8_t * mac) {
239 struct vnet_dev * dev = NULL;
241 dev = dev_by_mac(mac);
251 int v3_vnet_add_route(struct v3_vnet_route route) {
252 struct vnet_route_info * new_route = NULL;
255 new_route = (struct vnet_route_info *)Vnet_Malloc(sizeof(struct vnet_route_info));
256 memset(new_route, 0, sizeof(struct vnet_route_info));
258 #ifdef V3_CONFIG_DEBUG_VNET
259 Vnet_Debug("VNET/P Core: add_route_entry:\n");
263 memcpy(new_route->route_def.src_mac, route.src_mac, ETH_ALEN);
264 memcpy(new_route->route_def.dst_mac, route.dst_mac, ETH_ALEN);
265 new_route->route_def.src_mac_qual = route.src_mac_qual;
266 new_route->route_def.dst_mac_qual = route.dst_mac_qual;
267 new_route->route_def.dst_type = route.dst_type;
268 new_route->route_def.src_type = route.src_type;
269 new_route->route_def.src_id = route.src_id;
270 new_route->route_def.dst_id = route.dst_id;
272 if (new_route->route_def.dst_type == LINK_INTERFACE) {
273 new_route->dst_dev = dev_by_id(new_route->route_def.dst_id);
276 if (new_route->route_def.src_type == LINK_INTERFACE) {
277 new_route->src_dev = dev_by_id(new_route->route_def.src_id);
281 flags = vnet_lock_irqsave(vnet_state.lock);
283 list_add(&(new_route->node), &(vnet_state.routes));
284 new_route->idx = ++ vnet_state.route_idx;
285 vnet_state.num_routes ++;
287 vnet_unlock_irqrestore(vnet_state.lock, flags);
291 #ifdef V3_CONFIG_DEBUG_VNET
295 return new_route->idx;
299 void v3_vnet_del_route(uint32_t route_idx){
300 struct vnet_route_info * route = NULL;
303 flags = vnet_lock_irqsave(vnet_state.lock);
305 list_for_each_entry(route, &(vnet_state.routes), node) {
306 V3_Print("v3_vnet_del_route, route idx: %d\n", route->idx);
307 if(route->idx == route_idx){
308 list_del(&(route->node));
314 vnet_unlock_irqrestore(vnet_state.lock, flags);
317 #ifdef V3_CONFIG_DEBUG_VNET
323 /* delete all route entries with specfied src or dst device id */
324 static void inline del_routes_by_dev(int dev_id){
325 struct vnet_route_info * route = NULL;
328 flags = vnet_lock_irqsave(vnet_state.lock);
330 list_for_each_entry(route, &(vnet_state.routes), node) {
331 if((route->route_def.dst_type == LINK_INTERFACE &&
332 route->route_def.dst_id == dev_id) ||
333 (route->route_def.src_type == LINK_INTERFACE &&
334 route->route_def.src_id == dev_id)){
336 list_del(&(route->node));
337 list_del(&(route->match_node));
342 vnet_unlock_irqrestore(vnet_state.lock, flags);
345 /* At the end allocate a route_list
346 * This list will be inserted into the cache so we don't need to free it
348 static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
349 struct vnet_route_info * route = NULL;
350 struct route_list * matches = NULL;
353 struct list_head match_list;
354 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
355 // uint8_t src_type = pkt->src_type;
356 // uint32_t src_link = pkt->src_id;
358 #ifdef V3_CONFIG_DEBUG_VNET
363 mac2str(hdr->src_mac, src_str);
364 mac2str(hdr->dst_mac, dst_str);
365 Vnet_Debug("VNET/P Core: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
369 INIT_LIST_HEAD(&match_list);
371 #define UPDATE_MATCHES(rank) do { \
372 if (max_rank < (rank)) { \
374 INIT_LIST_HEAD(&match_list); \
376 list_add(&(route->match_node), &match_list); \
378 } else if (max_rank == (rank)) { \
379 list_add(&(route->match_node), &match_list); \
385 list_for_each_entry(route, &(vnet_state.routes), node) {
386 struct v3_vnet_route * route_def = &(route->route_def);
389 // CHECK SOURCE TYPE HERE
390 if ( (route_def->src_type != LINK_ANY) &&
391 ( (route_def->src_type != src_type) ||
392 ( (route_def->src_id != src_link) &&
393 (route_def->src_id != -1)))) {
398 if ((route_def->dst_mac_qual == MAC_ANY) &&
399 (route_def->src_mac_qual == MAC_ANY)) {
403 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
404 if (route_def->src_mac_qual != MAC_NOT) {
405 if (route_def->dst_mac_qual == MAC_ANY) {
407 } else if (route_def->dst_mac_qual != MAC_NOT &&
408 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
414 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
415 if (route_def->dst_mac_qual != MAC_NOT) {
416 if (route_def->src_mac_qual == MAC_ANY) {
418 } else if ((route_def->src_mac_qual != MAC_NOT) &&
419 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
425 if ((route_def->dst_mac_qual == MAC_NOT) &&
426 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
427 if (route_def->src_mac_qual == MAC_ANY) {
429 } else if ((route_def->src_mac_qual != MAC_NOT) &&
430 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
435 if ((route_def->src_mac_qual == MAC_NOT) &&
436 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
437 if (route_def->dst_mac_qual == MAC_ANY) {
439 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
440 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
446 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
447 (route_def->dst_mac_qual == MAC_NONE)) {
452 Vnet_Debug("VNET/P Core: match_route: Matches=%d\n", num_matches);
454 if (num_matches == 0) {
458 matches = (struct route_list *)Vnet_Malloc(sizeof(struct route_list) +
459 (sizeof(struct vnet_route_info *) * num_matches));
461 matches->num_routes = num_matches;
465 list_for_each_entry(route, &match_list, match_node) {
466 matches->routes[i++] = route;
474 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
475 struct route_list * matched_routes = NULL;
479 int cpu = V3_Get_CPU();
480 Vnet_Print(2, "VNET/P Core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
481 cpu, pkt->size, pkt->src_id,
482 pkt->src_type, pkt->dst_id, pkt->dst_type);
484 v3_hexdump(pkt->data, pkt->size, NULL, 0);
487 flags = vnet_lock_irqsave(vnet_state.lock);
489 vnet_state.stats.rx_bytes += pkt->size;
490 vnet_state.stats.rx_pkts++;
492 look_into_cache(pkt, &matched_routes);
493 if (matched_routes == NULL) {
494 Vnet_Debug("VNET/P Core: send pkt Looking into routing table\n");
496 matched_routes = match_route(pkt);
498 if (matched_routes) {
499 add_route_to_cache(pkt, matched_routes);
501 Vnet_Debug("VNET/P Core: Could not find route for packet... discards packet\n");
502 vnet_unlock_irqrestore(vnet_state.lock, flags);
503 return 0; /* do we return -1 here?*/
507 vnet_unlock_irqrestore(vnet_state.lock, flags);
509 Vnet_Debug("VNET/P Core: send pkt route matches %d\n", matched_routes->num_routes);
511 for (i = 0; i < matched_routes->num_routes; i++) {
512 struct vnet_route_info * route = matched_routes->routes[i];
514 if (route->route_def.dst_type == LINK_EDGE) {
515 struct vnet_brg_dev * bridge = vnet_state.bridge;
516 pkt->dst_type = LINK_EDGE;
517 pkt->dst_id = route->route_def.dst_id;
519 if (bridge == NULL) {
520 Vnet_Print(2, "VNET/P Core: No active bridge to sent data to\n");
524 if(bridge->brg_ops.input(bridge->vm, pkt, bridge->private_data) < 0){
525 Vnet_Print(2, "VNET/P Core: Packet not sent properly to bridge\n");
528 vnet_state.stats.tx_bytes += pkt->size;
529 vnet_state.stats.tx_pkts ++;
530 } else if (route->route_def.dst_type == LINK_INTERFACE) {
531 if (route->dst_dev == NULL){
532 Vnet_Print(2, "VNET/P Core: No active device to sent data to\n");
536 if(route->dst_dev->dev_ops.input(route->dst_dev->vm, pkt, route->dst_dev->private_data) < 0) {
537 Vnet_Print(2, "VNET/P Core: Packet not sent properly\n");
540 vnet_state.stats.tx_bytes += pkt->size;
541 vnet_state.stats.tx_pkts ++;
543 Vnet_Print(0, "VNET/P Core: Wrong dst type\n");
551 int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t * mac,
552 struct v3_vnet_dev_ops *ops, int quote, int poll_state,
554 struct vnet_dev * new_dev = NULL;
557 new_dev = (struct vnet_dev *)Vnet_Malloc(sizeof(struct vnet_dev));
559 if (new_dev == NULL) {
560 Vnet_Print(0, "Malloc fails\n");
564 memcpy(new_dev->mac_addr, mac, 6);
565 new_dev->dev_ops.input = ops->input;
566 new_dev->private_data = priv_data;
569 new_dev->quote = quote<VNET_MAX_QUOTE?quote:VNET_MAX_QUOTE;
570 new_dev->poll = poll_state;
572 flags = vnet_lock_irqsave(vnet_state.lock);
574 if (dev_by_mac(mac) == NULL) {
575 list_add(&(new_dev->node), &(vnet_state.devs));
576 new_dev->dev_id = ++ vnet_state.dev_idx;
577 vnet_state.num_devs ++;
580 vnet_unlock_irqrestore(vnet_state.lock, flags);
582 /* if the device was found previosly the id should still be 0 */
583 if (new_dev->dev_id == 0) {
584 Vnet_Print(0, "VNET/P Core: Device Already exists\n");
588 Vnet_Debug("VNET/P Core: Add Device: dev_id %d\n", new_dev->dev_id);
590 return new_dev->dev_id;
594 int v3_vnet_del_dev(int dev_id){
595 struct vnet_dev * dev = NULL;
598 flags = vnet_lock_irqsave(vnet_state.lock);
600 dev = dev_by_id(dev_id);
602 list_del(&(dev->node));
603 //del_routes_by_dev(dev_id);
604 vnet_state.num_devs --;
607 vnet_unlock_irqrestore(vnet_state.lock, flags);
611 Vnet_Debug("VNET/P Core: Remove Device: dev_id %d\n", dev_id);
617 int v3_vnet_stat(struct vnet_stat * stats){
618 stats->rx_bytes = vnet_state.stats.rx_bytes;
619 stats->rx_pkts = vnet_state.stats.rx_pkts;
620 stats->tx_bytes = vnet_state.stats.tx_bytes;
621 stats->tx_pkts = vnet_state.stats.tx_pkts;
626 static void deinit_devices_list(){
627 struct vnet_dev * dev = NULL;
629 list_for_each_entry(dev, &(vnet_state.devs), node) {
630 list_del(&(dev->node));
635 static void deinit_routes_list(){
636 struct vnet_route_info * route = NULL;
638 list_for_each_entry(route, &(vnet_state.routes), node) {
639 list_del(&(route->node));
640 list_del(&(route->match_node));
645 int v3_vnet_add_bridge(struct v3_vm_info * vm,
646 struct v3_vnet_bridge_ops * ops,
651 struct vnet_brg_dev * tmp_bridge = NULL;
653 flags = vnet_lock_irqsave(vnet_state.lock);
654 if (vnet_state.bridge == NULL) {
656 vnet_state.bridge = (void *)1;
658 vnet_unlock_irqrestore(vnet_state.lock, flags);
660 if (bridge_free == 0) {
661 PrintError("VNET/P Core: Bridge already set\n");
665 tmp_bridge = (struct vnet_brg_dev *)Vnet_Malloc(sizeof(struct vnet_brg_dev));
667 if (tmp_bridge == NULL) {
668 PrintError("Malloc Fails\n");
669 vnet_state.bridge = NULL;
674 tmp_bridge->brg_ops.input = ops->input;
675 tmp_bridge->brg_ops.poll = ops->poll;
676 tmp_bridge->private_data = priv_data;
677 tmp_bridge->type = type;
679 /* make this atomic to avoid possible race conditions */
680 flags = vnet_lock_irqsave(vnet_state.lock);
681 vnet_state.bridge = tmp_bridge;
682 vnet_unlock_irqrestore(vnet_state.lock, flags);
688 void v3_vnet_del_bridge(uint8_t type) {
690 struct vnet_brg_dev * tmp_bridge = NULL;
692 flags = vnet_lock_irqsave(vnet_state.lock);
694 if (vnet_state.bridge != NULL && vnet_state.bridge->type == type) {
695 tmp_bridge = vnet_state.bridge;
696 vnet_state.bridge = NULL;
699 vnet_unlock_irqrestore(vnet_state.lock, flags);
702 Vnet_Free(tmp_bridge);
707 /* can be instanieoued to multiple threads
708 * that runs on multiple cores
709 * or it could be running on a dedicated side core
711 static int vnet_tx_flush(void *args){
712 struct vnet_dev * dev = NULL;
715 Vnet_Print(0, "VNET/P Polling Thread Starting ....\n");
717 /* we need thread sleep/wakeup in Palacios */
718 while(!vnet_thread_should_stop()){
719 dev = (struct vnet_dev *)v3_dequeue(vnet_state.poll_devs);
721 if(dev->poll && dev->dev_ops.poll != NULL){
722 ret = dev->dev_ops.poll(dev->vm, dev->quote, dev->private_data);
725 PrintDebug("VNET/P: poll from device %p error!\n", dev);
728 v3_enqueue(vnet_state.poll_devs, (addr_t)dev);
730 }else { /* no device needs to be polled */
741 memset(&vnet_state, 0, sizeof(vnet_state));
743 INIT_LIST_HEAD(&(vnet_state.routes));
744 INIT_LIST_HEAD(&(vnet_state.devs));
746 vnet_state.num_devs = 0;
747 vnet_state.num_routes = 0;
749 if (vnet_lock_init(&(vnet_state.lock)) == -1){
750 PrintError("VNET/P: Fails to initiate lock\n");
753 vnet_state.route_cache = vnet_create_htable(0, &hash_fn, &hash_eq);
754 if (vnet_state.route_cache == NULL) {
755 PrintError("VNET/P: Fails to initiate route cache\n");
759 vnet_state.poll_devs = v3_create_queue();
761 vnet_state.pkt_flush_thread = vnet_start_thread(vnet_tx_flush, NULL, "vnetd");
763 Vnet_Debug("VNET/P is initiated\n");
769 void v3_deinit_vnet(){
771 vnet_lock_deinit(&(vnet_state.lock));
773 deinit_devices_list();
774 deinit_routes_list();
776 vnet_free_htable(vnet_state.route_cache, 1, 1);
777 Vnet_Free(vnet_state.bridge);