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) 2009, 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...)
38 uint16_t type; // indicates layer 3 protocol type
39 } __attribute__((packed));
48 struct v3_vm_info * vm;
50 int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data);
54 struct list_head node;
55 } __attribute__((packed));
59 struct v3_vm_info * vm;
61 int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data);
63 } __attribute__((packed));
69 struct vnet_route_info {
70 struct v3_vnet_route route_def;
72 struct vnet_dev * dst_dev;
73 struct vnet_dev * src_dev;
75 struct list_head node;
76 struct list_head match_node; // used for route matching
83 uint8_t hash_buf[VNET_HASH_SIZE];
86 struct vnet_route_info * routes[0];
87 } __attribute__((packed));
92 struct list_head routes;
93 struct list_head devs;
98 struct vnet_brg_dev *bridge;
102 struct gen_queue * inpkt_q;
103 struct hashtable * route_cache;
110 #ifdef CONFIG_DEBUG_VNET
111 static inline void mac_to_string(char mac[6], char * buf) {
112 snprintf(buf, 50, "%x:%x:%x:%x:%x:%x",
113 mac[0], mac[1], mac[2],
114 mac[3], mac[4], mac[5]);
117 static void print_route(struct vnet_route_info *route){
120 mac_to_string(route->route_def.src_mac, str);
121 PrintDebug("Src Mac (%s), src_qual (%d)\n",
122 str, route->route_def.src_mac_qual);
123 mac_to_string(route->route_def.dst_mac, str);
124 PrintDebug("Dst Mac (%s), dst_qual (%d)\n",
125 str, route->route_def.dst_mac_qual);
126 PrintDebug("Src dev id (%d), src type (%d)",
127 route->route_def.src_id,
128 route->route_def.src_type);
129 PrintDebug("Dst dev id (%d), dst type (%d)\n",
130 route->route_def.dst_id,
131 route->route_def.dst_type);
132 if (route->route_def.dst_type == LINK_INTERFACE) {
133 PrintDebug("dst_dev (%p), dst_dev_id (%d), dst_dev_input (%p), dst_dev_data (%p)\n",
135 route->dst_dev->dev_id,
136 route->dst_dev->input,
137 route->dst_dev->private_data);
141 static void dump_routes(){
142 struct vnet_route_info *route;
145 PrintDebug("\n========Dump routes starts ============\n");
146 list_for_each_entry(route, &(vnet_state.routes), node) {
147 PrintDebug("\nroute %d:\n", ++i);
151 PrintDebug("\n========Dump routes end ============\n");
158 * A VNET packet is a packed struct with the hashed fields grouped together.
159 * This means we can generate the hash from an offset into the pkt struct
161 static inline uint_t hash_fn(addr_t hdr_ptr) {
162 uint8_t * hdr_buf = (uint8_t *)hdr_ptr;
164 return v3_hash_buffer(hdr_buf, VNET_HASH_SIZE);
167 static inline int hash_eq(addr_t key1, addr_t key2) {
168 return (memcmp((uint8_t *)key1, (uint8_t *)key2, VNET_HASH_SIZE) == 0);
172 static int add_route_to_cache(struct v3_vnet_pkt * pkt, struct route_list * routes) {
173 memcpy(routes->hash_buf, pkt->hash_buf, VNET_HASH_SIZE);
175 if (v3_htable_insert(vnet_state.route_cache, (addr_t)routes->hash_buf, (addr_t)routes) == 0) {
176 PrintError("Vnet: Failed to insert new route entry to the cache\n");
183 static int clear_hash_cache() {
185 v3_free_htable(vnet_state.route_cache, 1, 1);
186 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
191 static int look_into_cache(struct v3_vnet_pkt * pkt, struct route_list ** routes) {
193 *routes = (struct route_list *)v3_htable_search(vnet_state.route_cache, (addr_t)(pkt->hash_buf));
199 static struct vnet_dev * find_dev_by_id(int idx) {
200 struct vnet_dev * dev = NULL;
202 list_for_each_entry(dev, &(vnet_state.devs), node) {
203 int dev_id = dev->dev_id;
212 static struct vnet_dev * find_dev_by_mac(char mac[6]) {
213 struct vnet_dev * dev = NULL;
215 list_for_each_entry(dev, &(vnet_state.devs), node) {
216 if (!memcmp(dev->mac_addr, mac, 6))
223 int get_device_id_by_mac(char mac[6]){
225 struct vnet_dev *dev = find_dev_by_mac(mac);
234 int v3_vnet_add_route(struct v3_vnet_route route) {
235 struct vnet_route_info * new_route = NULL;
238 new_route = (struct vnet_route_info *)V3_Malloc(sizeof(struct vnet_route_info));
239 memset(new_route, 0, sizeof(struct vnet_route_info));
241 PrintDebug("Vnet: vnet_add_route_entry: dst_id: %d, dst_type: %d\n",
242 route.dst_id, route.dst_type);
244 memcpy(new_route->route_def.src_mac, route.src_mac, 6);
245 memcpy(new_route->route_def.dst_mac, route.dst_mac, 6);
246 new_route->route_def.src_mac_qual = route.src_mac_qual;
247 new_route->route_def.dst_mac_qual = route.dst_mac_qual;
248 new_route->route_def.dst_id = route.dst_id;
249 new_route->route_def.dst_type = route.dst_type;
250 new_route->route_def.src_id = route.src_id;
251 new_route->route_def.src_type = route.src_type;
253 if (new_route->route_def.dst_type == LINK_INTERFACE) {
254 new_route->dst_dev = find_dev_by_id(new_route->route_def.dst_id);
255 PrintDebug("Vnet: Add route, get device: dev_id %d, input : %p, private_data %p\n",
256 new_route->dst_dev->dev_id, new_route->dst_dev->input, new_route->dst_dev->private_data);
259 if (new_route->route_def.src_type == LINK_INTERFACE) {
260 new_route->src_dev = find_dev_by_id(new_route->route_def.src_id);
263 flags = v3_lock_irqsave(vnet_state.lock);
264 list_add(&(new_route->node), &(vnet_state.routes));
265 v3_unlock_irqrestore(vnet_state.lock, flags);
269 #ifdef CONFIG_DEBUG_VNET
278 // At the end allocate a route_list
279 // This list will be inserted into the cache so we don't need to free it
280 static struct route_list * match_route(struct v3_vnet_pkt * pkt) {
281 struct vnet_route_info * route = NULL;
282 struct route_list * matches = NULL;
285 struct list_head match_list;
286 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
287 uint8_t src_type = pkt->src_type;
288 uint32_t src_link = pkt->src_id;
290 #ifdef CONFIG_DEBUG_VNET
295 mac_to_string(hdr->src_mac, src_str);
296 mac_to_string(hdr->dst_mac, dst_str);
297 PrintDebug("Vnet: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dst_str);
301 INIT_LIST_HEAD(&match_list);
303 #define UPDATE_MATCHES(rank) do { \
304 if (max_rank < (rank)) { \
306 INIT_LIST_HEAD(&match_list); \
308 list_add(&(route->match_node), &match_list); \
310 } else if (max_rank == (rank)) { \
311 list_add(&(route->match_node), &match_list); \
317 list_for_each_entry(route, &(vnet_state.routes), node) {
318 struct v3_vnet_route * route_def = &(route->route_def);
320 // CHECK SOURCE TYPE HERE
321 if ( (route_def->src_type != LINK_ANY) &&
322 ( (route_def->src_type != src_type) ||
323 ( (route_def->src_id != src_link) &&
324 (route_def->src_id != (uint32_t)-1)))) {
329 if ((route_def->dst_mac_qual == MAC_ANY) &&
330 (route_def->src_mac_qual == MAC_ANY)) {
334 if (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) {
335 if (route_def->src_mac_qual != MAC_NOT) {
336 if (route_def->dst_mac_qual == MAC_ANY) {
338 } else if (route_def->dst_mac_qual != MAC_NOT &&
339 memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
345 if (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0) {
346 if (route_def->dst_mac_qual != MAC_NOT) {
347 if (route_def->src_mac_qual == MAC_ANY) {
349 } else if ((route_def->src_mac_qual != MAC_NOT) &&
350 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
356 if ((route_def->dst_mac_qual == MAC_NOT) &&
357 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) != 0)) {
358 if (route_def->src_mac_qual == MAC_ANY) {
360 } else if ((route_def->src_mac_qual != MAC_NOT) &&
361 (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0)) {
366 if ((route_def->src_mac_qual == MAC_NOT) &&
367 (memcmp(route_def->src_mac, hdr->src_mac, 6) != 0)) {
368 if (route_def->dst_mac_qual == MAC_ANY) {
370 } else if ((route_def->dst_mac_qual != MAC_NOT) &&
371 (memcmp(route_def->dst_mac, hdr->dst_mac, 6) == 0)) {
377 if ( (memcmp(route_def->src_mac, hdr->src_mac, 6) == 0) &&
378 (route_def->dst_mac_qual == MAC_NONE)) {
383 PrintDebug("Vnet: match_route: Matches=%d\n", num_matches);
385 if (num_matches == 0) {
389 matches = V3_Malloc(sizeof(struct route_list) +
390 (sizeof(struct vnet_route_info *) * num_matches));
392 matches->num_routes = num_matches;
396 list_for_each_entry(route, &match_list, match_node) {
397 matches->routes[i++] = route;
404 static int handle_one_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
405 struct route_list * matched_routes = NULL;
410 #ifdef CONFIG_DEBUG_VNET
412 struct eth_hdr * hdr = (struct eth_hdr *)(pkt->data);
416 mac_to_string(hdr->src_mac, src_str);
417 mac_to_string(hdr->dst_mac, dest_str);
418 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
422 #ifdef CONFIG_VNET_PROFILE
423 struct guest_info *core = (struct guest_info *)private_data;
428 flags = v3_lock_irqsave(vnet_state.lock);
430 look_into_cache(pkt, &matched_routes);
432 if (matched_routes == NULL) {
433 PrintDebug("Vnet: can not find route in cache, looking into routing table\n");
435 matched_routes = match_route(pkt);
437 if (matched_routes) {
438 add_route_to_cache(pkt, matched_routes);
440 PrintDebug("Could not find route for packet...\n");
441 v3_unlock_irqrestore(vnet_state.lock, flags);
446 v3_unlock_irqrestore(vnet_state.lock, flags);
448 #ifdef CONFIG_VNET_PROFILE
451 core->vnet_times.time_route_lookup = end - start;
455 PrintDebug("Vnet: HandleOnePacket: route matches %d\n", matched_routes->num_routes);
456 for (i = 0; i < matched_routes->num_routes; i++) {
457 struct vnet_route_info * route = matched_routes->routes[i];
459 if (route->route_def.dst_type == LINK_EDGE) {
460 pkt->dst_type = LINK_EDGE;
461 pkt->dst_id = route->route_def.dst_id;
462 if (vnet_state.bridge == NULL) {
463 PrintDebug("VNET: No bridge to sent data to links\n");
466 if (vnet_state.bridge->input(vnet_state.bridge->vm, pkt, vnet_state.bridge->private_data) == -1) {
467 PrintDebug("VNET: Packet not sent properly\n");
470 } else if (route->route_def.dst_type == LINK_INTERFACE) {
471 if (route->dst_dev->input(route->dst_dev->vm, pkt, route->dst_dev->private_data) == -1) {
472 PrintDebug("VNET: Packet not sent properly\n");
476 PrintDebug("Vnet: Wrong Edge type\n");
480 PrintDebug("Vnet: HandleOnePacket: Forward packet according to Route\n");
483 #ifdef CONFIG_VNET_PROFILE
486 core->vnet_times.time_copy_to_guest = start - end;
493 int v3_vnet_send_pkt(struct v3_vnet_pkt * pkt, void *private_data) {
494 PrintDebug("In Vnet Send: pkt size: %d\n", pkt->size);
496 if (handle_one_pkt(pkt, private_data) != -1) {
497 PrintDebug("VNET: send one packet! pt length %d\n", pkt->size);
499 PrintDebug("VNET: Fail to forward one packet, discard it!\n");
505 int v3_vnet_add_dev(struct v3_vm_info *vm,uint8_t mac[6],
506 int (*netif_input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data),
508 struct vnet_dev * new_dev = NULL;
512 flags = v3_lock_irqsave(vnet_state.lock);
514 new_dev = find_dev_by_mac(mac);
517 PrintDebug("VNET: register device: Already has device with the same mac\n");
522 new_dev = (struct vnet_dev *)V3_Malloc(sizeof(struct vnet_dev));
524 if (new_dev == NULL) {
525 PrintError("VNET: Malloc fails\n");
530 memcpy(new_dev->mac_addr, mac, 6);
531 new_dev->input = netif_input;
532 new_dev->private_data = priv_data;
535 list_add(&(new_dev->node), &(vnet_state.devs));
536 vnet_state.num_devs ++;
537 new_dev->dev_id = vnet_state.num_devs;
538 dev_id = new_dev->dev_id;
540 PrintDebug("Vnet: Add Device: dev_id %d, input : %p, private_data %p\n",
541 new_dev->dev_id, new_dev->input, new_dev->private_data);
545 v3_unlock_irqrestore(vnet_state.lock, flags);
551 int v3_vnet_add_bridge(struct v3_vm_info * vm,
552 int (*input)(struct v3_vm_info * vm, struct v3_vnet_pkt * pkt, void * private_data),
556 flags = v3_lock_irqsave(vnet_state.lock);
558 if(vnet_state.bridge != NULL){
559 PrintDebug("Vnet: Replace current bridge with a new one\n");
561 vnet_state.bridge = (struct vnet_brg_dev *)V3_Malloc(sizeof(struct vnet_brg_dev));
562 if (vnet_state.bridge == NULL) {
563 PrintError("Malloc Fails\n");
568 vnet_state.bridge->vm = vm;
569 vnet_state.bridge->input = input;
570 vnet_state.bridge->private_data = priv_data;
572 v3_unlock_irqrestore(vnet_state.lock, flags);
579 INIT_LIST_HEAD(&(vnet_state.routes));
580 INIT_LIST_HEAD(&(vnet_state.devs));
582 vnet_state.num_devs = 0;
583 vnet_state.num_routes = 0;
585 PrintDebug("VNET: Links and Routes tables initiated\n");
587 if (v3_lock_init(&(vnet_state.lock)) == -1){
588 PrintError("VNET: Failure to init lock for routes table\n");
591 PrintDebug("VNET: Locks initiated\n");
593 vnet_state.inpkt_q = v3_create_queue();
594 v3_init_queue(vnet_state.inpkt_q);
595 PrintDebug("VNET: Receiving queue initiated\n");
597 vnet_state.route_cache = v3_create_htable(0, &hash_fn, &hash_eq);
599 if (vnet_state.route_cache == NULL) {
600 PrintError("Vnet: Route Cache Init Fails\n");
604 PrintDebug("VNET: initiated\n");