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
268 /* At the end allocate a route_list
269 * This list will be inserted into the cache so we don't need to free it
271 static struct route_list * match_route(const struct v3_vnet_pkt * pkt) {
272 struct vnet_route_info * route = NULL;
273 struct route_list * matches = NULL;
276 struct list_head match_list;
277 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
278 uint8_t src_type = pkt->src_type;
279 uint32_t src_link = pkt->src_id;
281 #ifdef CONFIG_DEBUG_VNET
286 mac_to_string(hdr->src_mac, src_str);
287 mac_to_string(hdr->dst_mac, dst_str);
288 PrintDebug("Vnet: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
292 INIT_LIST_HEAD(&match_list);
294 #define UPDATE_MATCHES(rank) do { \
295 if (max_rank < (rank)) { \
297 INIT_LIST_HEAD(&match_list); \
299 list_add(&(route->match_node), &match_list); \
301 } else if (max_rank == (rank)) { \
302 list_add(&(route->match_node), &match_list); \
308 list_for_each_entry(route, &(vnet_state.routes), node) {
309 struct v3_vnet_route * route_def = &(route->route_def);
311 // CHECK SOURCE TYPE HERE
312 if ( (route_def->src_type != LINK_ANY) &&
313 ( (route_def->src_type != src_type) ||
314 ( (route_def->src_id != src_link) &&
315 (route_def->src_id != (uint32_t)-1)))) {
320 if ((route_def->dst_mac_qual == MAC_ANY) &&
321 (route_def->src_mac_qual == MAC_ANY)) {
325 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
326 if (route_def->src_mac_qual != MAC_NOT) {
327 if (route_def->dst_mac_qual == MAC_ANY) {
329 } else if (route_def->dst_mac_qual != MAC_NOT &&
330 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
336 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
337 if (route_def->dst_mac_qual != MAC_NOT) {
338 if (route_def->src_mac_qual == MAC_ANY) {
340 } else if ((route_def->src_mac_qual != MAC_NOT) &&
341 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
347 if ((route_def->dst_mac_qual == MAC_NOT) &&
348 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
349 if (route_def->src_mac_qual == MAC_ANY) {
351 } else if ((route_def->src_mac_qual != MAC_NOT) &&
352 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
357 if ((route_def->src_mac_qual == MAC_NOT) &&
358 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
359 if (route_def->dst_mac_qual == MAC_ANY) {
361 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
362 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
368 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
369 (route_def->dst_mac_qual == MAC_NONE)) {
374 PrintDebug("Vnet: match_route: Matches=%d\n", num_matches);
376 if (num_matches == 0) {
380 matches = (struct route_list *)V3_Malloc(sizeof(struct route_list) +
381 (sizeof(struct vnet_route_info *) * num_matches));
383 matches->num_routes = num_matches;
387 list_for_each_entry(route, &match_list, match_node) {
388 matches->routes[i++] = route;
396 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void * private_data) {
397 struct route_list * matched_routes = NULL;
401 #ifdef CONFIG_DEBUG_VNET
403 int cpu = V3_Get_CPU();
404 PrintDebug("VNET-core: cpu %d: pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
405 cpu, pkt->size, pkt->src_id,
406 pkt->src_type, pkt->dst_id, pkt->dst_type);
407 //v3_hexdump(pkt->data, pkt->size, NULL, 0);
411 flags = v3_lock_irqsave(vnet_state.lock);
413 look_into_cache(pkt, &matched_routes);
415 if (matched_routes == NULL) {
416 PrintDebug("Vnet: send pkt Looking into routing table\n");
418 matched_routes = match_route(pkt);
420 if (matched_routes) {
421 add_route_to_cache(pkt, matched_routes);
423 PrintDebug("Could not find route for packet... discards packet\n");
424 v3_unlock_irqrestore(vnet_state.lock, flags);
425 return 0; /* do we return -1 here?*/
429 v3_unlock_irqrestore(vnet_state.lock, flags);
431 PrintDebug("Vnet: send pkt route matches %d\n", matched_routes->num_routes);
433 for (i = 0; i < matched_routes->num_routes; i++) {
434 struct vnet_route_info * route = matched_routes->routes[i];
436 if (route->route_def.dst_type == LINK_EDGE) {
437 struct vnet_brg_dev *bridge = vnet_state.bridge;
438 pkt->dst_type = LINK_EDGE;
439 pkt->dst_id = route->route_def.dst_id;
441 if (bridge == NULL || (bridge->active == 0)) {
442 PrintDebug("VNET: No active bridge to sent data to\n");
446 if(bridge->brg_ops.input(bridge->vm, pkt, bridge->private_data) < 0){
447 PrintDebug("VNET: Packet not sent properly to bridge\n");
450 } else if (route->route_def.dst_type == LINK_INTERFACE) {
451 if (route->dst_dev == NULL || route->dst_dev->active == 0){
452 PrintDebug("VNET: No active device to sent data to\n");
456 if(route->dst_dev->dev_ops.input(route->dst_dev->vm, pkt, route->dst_dev->private_data) < 0) {
457 PrintDebug("VNET: Packet not sent properly\n");
461 PrintError("VNET: Wrong dst type\n");
468 int v3_vnet_add_dev(struct v3_vm_info * vm, uint8_t mac[6],
469 struct v3_vnet_dev_ops *ops,
471 struct vnet_dev * new_dev = NULL;
474 new_dev = (struct vnet_dev *)V3_Malloc(sizeof(struct vnet_dev));
476 if (new_dev == NULL) {
477 PrintError("VNET: Malloc fails\n");
481 memcpy(new_dev->mac_addr, mac, 6);
482 new_dev->dev_ops.input = ops->input;
483 new_dev->dev_ops.poll = ops->poll;
484 new_dev->private_data = priv_data;
488 new_dev->mode = GUEST_DRIVERN;
490 flags = v3_lock_irqsave(vnet_state.lock);
492 if (!find_dev_by_mac(mac)) {
493 list_add(&(new_dev->node), &(vnet_state.devs));
494 new_dev->dev_id = ++vnet_state.num_devs;
497 v3_unlock_irqrestore(vnet_state.lock, flags);
499 /* if the device was found previosly the id should still be 0 */
500 if (new_dev->dev_id == 0) {
501 PrintError("Device Alrady exists\n");
505 PrintDebug("Vnet: Add Device: dev_id %d\n", new_dev->dev_id);
507 return new_dev->dev_id;
511 void v3_vnet_poll(struct v3_vm_info * vm){
512 struct vnet_dev * dev = NULL;
513 struct vnet_brg_dev *bridge = vnet_state.bridge;
515 list_for_each_entry(dev, &(vnet_state.devs), node) {
516 if(dev->mode == VMM_DRIVERN &&
520 dev->dev_ops.poll(vm, dev->private_data);
524 if (bridge != NULL &&
526 bridge->mode == VMM_DRIVERN) {
528 bridge->brg_ops.poll(bridge->vm, bridge->private_data);
533 int v3_vnet_add_bridge(struct v3_vm_info * vm,
534 struct v3_vnet_bridge_ops * ops,
539 struct vnet_brg_dev * tmp_bridge = NULL;
541 flags = v3_lock_irqsave(vnet_state.lock);
543 if (vnet_state.bridge == NULL) {
545 vnet_state.bridge = (void *)1;
548 v3_unlock_irqrestore(vnet_state.lock, flags);
550 if (bridge_free == 0) {
551 PrintError("Bridge already set\n");
555 tmp_bridge = (struct vnet_brg_dev *)V3_Malloc(sizeof(struct vnet_brg_dev));
557 if (tmp_bridge == NULL) {
558 PrintError("Malloc Fails\n");
559 vnet_state.bridge = NULL;
564 tmp_bridge->brg_ops.input = ops->input;
565 tmp_bridge->brg_ops.poll = ops->poll;
566 tmp_bridge->private_data = priv_data;
567 tmp_bridge->active = 1;
568 tmp_bridge->mode = GUEST_DRIVERN;
569 tmp_bridge->type = type;
571 /* make this atomic to avoid possible race conditions */
572 flags = v3_lock_irqsave(vnet_state.lock);
573 vnet_state.bridge = tmp_bridge;
574 v3_unlock_irqrestore(vnet_state.lock, flags);
581 memset(&vnet_state, 0, sizeof(vnet_state));
583 INIT_LIST_HEAD(&(vnet_state.routes));
584 INIT_LIST_HEAD(&(vnet_state.devs));
586 vnet_state.num_devs = 0;
587 vnet_state.num_routes = 0;
589 PrintDebug("VNET: Links and Routes tables initiated\n");
591 if (v3_lock_init(&(vnet_state.lock)) == -1){
592 PrintError("VNET: Failure to init lock for routes table\n");
594 PrintDebug("VNET: Locks initiated\n");
596 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
598 if (vnet_state.route_cache == NULL) {
599 PrintError("Vnet: Route Cache Init Fails\n");
603 PrintDebug("VNET: initiated\n");