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 <palacios/vmm_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>
28 #ifndef CONFIG_DEBUG_VNET
30 #define PrintDebug(fmt, args...)
36 uint16_t type; /* indicates layer 3 protocol type */
37 } __attribute__((packed));
43 struct v3_vm_info * vm;
44 struct v3_vnet_dev_ops dev_ops;
48 uint8_t mode; //vmm_drivern or guest_drivern
50 struct list_head node;
51 } __attribute__((packed));
55 struct v3_vm_info * vm;
56 struct v3_vnet_bridge_ops brg_ops;
62 } __attribute__((packed));
66 struct vnet_route_info {
67 struct v3_vnet_route route_def;
69 struct vnet_dev * dst_dev;
70 struct vnet_dev * src_dev;
72 struct list_head node;
73 struct list_head match_node; // used for route matching
78 uint8_t hash_buf[VNET_HASH_SIZE];
81 struct vnet_route_info * routes[0];
82 } __attribute__((packed));
86 struct list_head routes;
87 struct list_head devs;
92 struct vnet_brg_dev *bridge;
96 struct hashtable * route_cache;
101 #ifdef CONFIG_DEBUG_VNET
102 static inline void mac_to_string(char mac[6], char * buf) {
103 snprintf(buf, 100, "%d:%d:%d:%d:%d:%d",
104 mac[0], mac[1], mac[2],
105 mac[3], mac[4], mac[5]);
108 static void print_route(struct vnet_route_info * route){
111 mac_to_string(route->route_def.src_mac, str);
112 PrintDebug("Src Mac (%s), src_qual (%d)\n",
113 str, route->route_def.src_mac_qual);
114 mac_to_string(route->route_def.dst_mac, str);
115 PrintDebug("Dst Mac (%s), dst_qual (%d)\n",
116 str, route->route_def.dst_mac_qual);
117 PrintDebug("Src dev id (%d), src type (%d)",
118 route->route_def.src_id,
119 route->route_def.src_type);
120 PrintDebug("Dst dev id (%d), dst type (%d)\n",
121 route->route_def.dst_id,
122 route->route_def.dst_type);
123 if (route->route_def.dst_type == LINK_INTERFACE) {
124 PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_ops(%p), dst_dev_data (%p)\n",
126 route->dst_dev->dev_id,
127 (void *)&(route->dst_dev->dev_ops),
128 route->dst_dev->private_data);
132 static void dump_routes(){
133 struct vnet_route_info *route;
136 PrintDebug("\n========Dump routes starts ============\n");
137 list_for_each_entry(route, &(vnet_state.routes), node) {
138 PrintDebug("\nroute %d:\n", i++);
142 PrintDebug("\n========Dump routes end ============\n");
149 * A VNET packet is a packed struct with the hashed fields grouped together.
150 * This means we can generate the hash from an offset into the pkt struct
152 static inline uint_t hash_fn(addr_t hdr_ptr) {
153 uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
155 return v3_hash_buffer(hdr_buf, VNET_HASH_SIZE);
158 static inline int hash_eq(addr_t key1, addr_t key2) {
159 return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
162 static int add_route_to_cache(const struct v3_vnet_pkt * pkt, struct route_list * routes) {
163 memcpy(routes->hash_buf, pkt->hash_buf, VNET_HASH_SIZE);
165 if (v3_htable_insert(vnet_state.route_cache, (addr_t)routes->hash_buf, (addr_t)routes) == 0) {
166 PrintError("Vnet: Failed to insert new route entry to the cache\n");
173 static int clear_hash_cache() {
175 v3_free_htable(vnet_state.route_cache, 1, 1);
176 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
181 static int look_into_cache(const struct v3_vnet_pkt * pkt, struct route_list ** routes) {
183 *routes = (struct route_list *)v3_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
189 static struct vnet_dev * find_dev_by_id(int idx) {
190 struct vnet_dev * dev = NULL;
192 list_for_each_entry(dev, &(vnet_state.devs), node) {
193 int dev_id = dev->dev_id;
202 static struct vnet_dev * find_dev_by_mac(char mac[6]) {
203 struct vnet_dev * dev = NULL;
205 list_for_each_entry(dev, &(vnet_state.devs), node) {
206 if (!memcmp(dev->mac_addr, mac, 6))
213 int v3_vnet_id_by_mac(char mac[6]){
215 struct vnet_dev *dev = find_dev_by_mac(mac);
224 int v3_vnet_add_route(struct v3_vnet_route route) {
225 struct vnet_route_info * new_route = NULL;
228 new_route = (struct vnet_route_info *)V3_Malloc(sizeof(struct vnet_route_info));
229 memset(new_route, 0, sizeof(struct vnet_route_info));
231 PrintDebug("Vnet: vnet_add_route_entry: dst_id: %d, dst_type: %d\n",
232 route.dst_id, route.dst_type);
234 memcpy(new_route->route_def.src_mac, route.src_mac, 6);
235 memcpy(new_route->route_def.dst_mac, route.dst_mac, 6);
236 new_route->route_def.src_mac_qual = route.src_mac_qual;
237 new_route->route_def.dst_mac_qual = route.dst_mac_qual;
238 new_route->route_def.dst_id = route.dst_id;
239 new_route->route_def.dst_type = route.dst_type;
240 new_route->route_def.src_id = route.src_id;
241 new_route->route_def.src_type = route.src_type;
243 if (new_route->route_def.dst_type == LINK_INTERFACE) {
244 new_route->dst_dev = find_dev_by_id(new_route->route_def.dst_id);
247 if (new_route->route_def.src_type == LINK_INTERFACE) {
248 new_route->src_dev = find_dev_by_id(new_route->route_def.src_id);
251 flags = v3_lock_irqsave(vnet_state.lock);
253 list_add(&(new_route->node), &(vnet_state.routes));
256 v3_unlock_irqrestore(vnet_state.lock, flags);
259 #ifdef CONFIG_DEBUG_VNET
267 /* delete all route entries with specfied src or dst device id */
268 static void inline del_routes(int dev_id){
269 struct vnet_route_info * route = NULL;
272 flags = v3_lock_irqsave(vnet_state.lock);
274 list_for_each_entry(route, &(vnet_state.routes), node) {
275 if((route->route_def.dst_type == LINK_INTERFACE &&
276 route->route_def.dst_id == dev_id) ||
277 (route->route_def.src_type == LINK_INTERFACE &&
278 route->route_def.src_id == dev_id)){
280 list_del(&(route->node));
281 list_del(&(route->match_node));
286 v3_unlock_irqrestore(vnet_state.lock, flags);
289 /* At the end allocate a route_list
290 * This list will be inserted into the cache so we don't need to free it
292 static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
293 struct vnet_route_info * route = NULL;
294 struct route_list * matches = NULL;
297 struct list_head match_list;
298 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
299 uint8_t src_type = pkt->src_type;
300 uint32_t src_link = pkt->src_id;
302 #ifdef CONFIG_DEBUG_VNET
307 mac_to_string(hdr->src_mac, src_str);
308 mac_to_string(hdr->dst_mac, dst_str);
309 PrintDebug("Vnet: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
313 INIT_LIST_HEAD(&match_list);
315 #define UPDATE_MATCHES(rank) do { \
316 if (max_rank < (rank)) { \
318 INIT_LIST_HEAD(&match_list); \
320 list_add(&(route->match_node), &match_list); \
322 } else if (max_rank == (rank)) { \
323 list_add(&(route->match_node), &match_list); \
329 list_for_each_entry(route, &(vnet_state.routes), node) {
330 struct v3_vnet_route * route_def = &(route->route_def);
332 // CHECK SOURCE TYPE HERE
333 if ( (route_def->src_type != LINK_ANY) &&
334 ( (route_def->src_type != src_type) ||
335 ( (route_def->src_id != src_link) &&
336 (route_def->src_id != (uint32_t)-1)))) {
341 if ((route_def->dst_mac_qual == MAC_ANY) &&
342 (route_def->src_mac_qual == MAC_ANY)) {
346 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
347 if (route_def->src_mac_qual != MAC_NOT) {
348 if (route_def->dst_mac_qual == MAC_ANY) {
350 } else if (route_def->dst_mac_qual != MAC_NOT &&
351 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
357 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
358 if (route_def->dst_mac_qual != MAC_NOT) {
359 if (route_def->src_mac_qual == MAC_ANY) {
361 } else if ((route_def->src_mac_qual != MAC_NOT) &&
362 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
368 if ((route_def->dst_mac_qual == MAC_NOT) &&
369 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
370 if (route_def->src_mac_qual == MAC_ANY) {
372 } else if ((route_def->src_mac_qual != MAC_NOT) &&
373 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
378 if ((route_def->src_mac_qual == MAC_NOT) &&
379 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
380 if (route_def->dst_mac_qual == MAC_ANY) {
382 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
383 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
389 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
390 (route_def->dst_mac_qual == MAC_NONE)) {
395 PrintDebug("Vnet: match_route: Matches=%d\n", num_matches);
397 if (num_matches == 0) {
401 matches = (struct route_list *)V3_Malloc(sizeof(struct route_list) +
402 (sizeof(struct vnet_route_info *) * num_matches));
404 matches->num_routes = num_matches;
408 list_for_each_entry(route, &match_list, match_node) {
409 matches->routes[i++] = route;
417 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
418 struct route_list * matched_routes = NULL;
422 #ifdef CONFIG_DEBUG_VNET
424 int cpu = V3_Get_CPU();
425 PrintDebug("VNET-core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
426 cpu, pkt->size, pkt->src_id,
427 pkt->src_type, pkt->dst_id, pkt->dst_type);
428 //v3_hexdump(pkt->data, pkt->size, NULL, 0);
432 flags = v3_lock_irqsave(vnet_state.lock);
434 look_into_cache(pkt, &matched_routes);
436 if (matched_routes == NULL) {
437 PrintDebug("Vnet: send pkt Looking into routing table\n");
439 matched_routes = match_route(pkt);
441 if (matched_routes) {
442 add_route_to_cache(pkt, matched_routes);
444 PrintDebug("Could not find route for packet... discards packet\n");
445 v3_unlock_irqrestore(vnet_state.lock, flags);
446 return 0; /* do we return -1 here?*/
450 v3_unlock_irqrestore(vnet_state.lock, flags);
452 PrintDebug("Vnet: send pkt route matches %d\n", matched_routes->num_routes);
454 for (i = 0; i < matched_routes->num_routes; i++) {
455 struct vnet_route_info * route = matched_routes->routes[i];
457 if (route->route_def.dst_type == LINK_EDGE) {
458 struct vnet_brg_dev *bridge = vnet_state.bridge;
459 pkt->dst_type = LINK_EDGE;
460 pkt->dst_id = route->route_def.dst_id;
462 if (bridge == NULL || (bridge->active == 0)) {
463 PrintDebug("VNET: No active bridge to sent data to\n");
467 if(bridge->brg_ops.input(bridge->vm, pkt, bridge->private_data) < 0){
468 PrintDebug("VNET: Packet not sent properly to bridge\n");
471 } else if (route->route_def.dst_type == LINK_INTERFACE) {
472 if (route->dst_dev == NULL || route->dst_dev->active == 0){
473 PrintDebug("VNET: No active device to sent data to\n");
477 if(route->dst_dev->dev_ops.input(route->dst_dev->vm, pkt, route->dst_dev->private_data) < 0) {
478 PrintDebug("VNET: Packet not sent properly\n");
482 PrintError("VNET: Wrong dst type\n");
489 int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t mac[6],
490 struct v3_vnet_dev_ops *ops,
492 struct vnet_dev * new_dev = NULL;
495 new_dev = (struct vnet_dev *)V3_Malloc(sizeof(struct vnet_dev));
497 if (new_dev == NULL) {
498 PrintError("VNET: Malloc fails\n");
502 memcpy(new_dev->mac_addr, mac, 6);
503 new_dev->dev_ops.input = ops->input;
504 new_dev->dev_ops.poll = ops->poll;
505 new_dev->private_data = priv_data;
509 new_dev->mode = GUEST_DRIVERN;
511 flags = v3_lock_irqsave(vnet_state.lock);
513 if (!find_dev_by_mac(mac)) {
514 list_add(&(new_dev->node), &(vnet_state.devs));
515 new_dev->dev_id = ++vnet_state.num_devs;
518 v3_unlock_irqrestore(vnet_state.lock, flags);
520 /* if the device was found previosly the id should still be 0 */
521 if (new_dev->dev_id == 0) {
522 PrintError("Device Already exists\n");
526 PrintDebug("Vnet: Add Device: dev_id %d\n", new_dev->dev_id);
528 return new_dev->dev_id;
533 int v3_vnet_del_dev(int dev_id){
534 struct vnet_dev * dev = NULL;
537 flags = v3_lock_irqsave(vnet_state.lock);
539 dev = find_dev_by_id(dev_id);
541 list_del(&(dev->node));
545 v3_unlock_irqrestore(vnet_state.lock, flags);
549 PrintDebug("Vnet: Remove Device: dev_id %d\n", dev_id);
555 static void free_devices(){
556 struct vnet_dev * dev = NULL;
558 list_for_each_entry(dev, &(vnet_state.devs), node) {
559 list_del(&(dev->node));
564 static void free_routes(){
565 struct vnet_route_info * route = NULL;
567 list_for_each_entry(route, &(vnet_state.routes), node) {
568 list_del(&(route->node));
569 list_del(&(route->match_node));
575 void v3_vnet_poll(struct v3_vm_info * vm){
576 struct vnet_dev * dev = NULL;
577 struct vnet_brg_dev *bridge = vnet_state.bridge;
579 list_for_each_entry(dev, &(vnet_state.devs), node) {
580 if(dev->mode == VMM_DRIVERN &&
584 dev->dev_ops.poll(vm, dev->private_data);
588 if (bridge != NULL &&
590 bridge->mode == VMM_DRIVERN) {
592 bridge->brg_ops.poll(bridge->vm, bridge->private_data);
597 int v3_vnet_add_bridge(struct v3_vm_info * vm,
598 struct v3_vnet_bridge_ops * ops,
603 struct vnet_brg_dev * tmp_bridge = NULL;
605 flags = v3_lock_irqsave(vnet_state.lock);
607 if (vnet_state.bridge == NULL) {
609 vnet_state.bridge = (void *)1;
612 v3_unlock_irqrestore(vnet_state.lock, flags);
614 if (bridge_free == 0) {
615 PrintError("Bridge already set\n");
619 tmp_bridge = (struct vnet_brg_dev *)V3_Malloc(sizeof(struct vnet_brg_dev));
621 if (tmp_bridge == NULL) {
622 PrintError("Malloc Fails\n");
623 vnet_state.bridge = NULL;
628 tmp_bridge->brg_ops.input = ops->input;
629 tmp_bridge->brg_ops.poll = ops->poll;
630 tmp_bridge->private_data = priv_data;
631 tmp_bridge->active = 1;
632 tmp_bridge->mode = GUEST_DRIVERN;
633 tmp_bridge->type = type;
635 /* make this atomic to avoid possible race conditions */
636 flags = v3_lock_irqsave(vnet_state.lock);
637 vnet_state.bridge = tmp_bridge;
638 v3_unlock_irqrestore(vnet_state.lock, flags);
645 memset(&vnet_state, 0, sizeof(vnet_state));
647 INIT_LIST_HEAD(&(vnet_state.routes));
648 INIT_LIST_HEAD(&(vnet_state.devs));
650 vnet_state.num_devs = 0;
651 vnet_state.num_routes = 0;
653 PrintDebug("VNET: Links and Routes tables initiated\n");
655 if (v3_lock_init(&(vnet_state.lock)) == -1){
656 PrintError("VNET: Failure to init lock for routes table\n");
658 PrintDebug("VNET: Locks initiated\n");
660 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
662 if (vnet_state.route_cache == NULL) {
663 PrintError("Vnet: Route Cache Init Fails\n");
667 PrintDebug("VNET: initiated\n");
673 void v3_deinit_vnet(){
675 v3_lock_deinit(&(vnet_state.lock));
680 v3_free_htable(vnet_state.route_cache, 1, 1);
681 V3_Free(vnet_state.bridge);