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, Jack Lange <jarusl@cs.northwestern.edu>
13 * Copyright (c) 2009, Peter Dinda <pdinda@northwestern.edu>
14 * Copyright (c) 2009, The V3VEE Project <http://www.v3vee.org>
15 * All rights reserved.
17 * Author: Lei Xia <lxia@northwestern.edu>
18 * Yuan Tang <ytang@northwestern.edu>
19 * Jack Lange <jarusl@cs.northwestern.edu>
20 * Peter Dinda <pdinda@northwestern.edu
22 * This is free software. You are permitted to use,
23 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
26 #include <palacios/vmm_vnet.h>
27 #include <palacios/vmm_hypercall.h>
28 #include <palacios/vm_guest_mem.h>
30 #ifndef CONFIG_DEBUG_VNET
32 #define PrintDebug(fmt, args...)
37 uint32_t size; //size of data
39 struct udp_link_header ext_hdr; //possible externel header to applied to data before sent
40 char data[ETHERNET_PACKET_LEN];
44 // 14 (ethernet frame) + 20 bytes
45 struct in_pkt_header {
48 unsigned char ethernettype[2]; //layer 3 protocol type
52 #define VNET_INITAB_HCALL 0xca00 // inital hypercall id
56 #define HASH_KEY_LEN 16
57 #define MIN_CACHE_SIZE 100
58 static const uint_t hash_key_size = 16;
61 struct link_entry * links[MAX_LINKS];
65 struct routing_table {
66 struct routing_entry * routes[MAX_ROUTES];
70 static struct link_table g_links;
71 static struct routing_table g_routes;
72 static struct gen_queue * g_inpkt_q;
80 typedef char * route_hashkey_t;
82 // This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY
83 struct route_cache_entry {
84 int num_matched_routes;
89 static struct hashtable * g_route_cache;
92 static void print_packet(uchar_t *pkt, int size) {
93 PrintDebug("Vnet: print_data_packet: size: %d\n", size);
94 v3_hexdump(pkt, size, NULL, 0);
98 static void print_packet_addr(char * pkt) {
99 PrintDebug("Vnet: print_packet_destination_addr: ");
100 v3_hexdump(pkt + 8, 6, NULL, 0);
102 PrintDebug("Vnet: print_packet_source_addr: ");
103 v3_hexdump(pkt + 14, 6, NULL, 0);
106 static void print_device_addr(char * ethaddr) {
107 PrintDebug("Vnet: print_device_addr: ");
108 v3_hexdump(ethaddr, 6, NULL, 0);
112 static uint16_t ip_xsum(struct ip_header *ip_hdr, int hdr_len){
115 uint16_t *p = (uint16_t*) ip_hdr;
119 sum = (sum & 0xFFFF) + (sum >> 16);
124 sum += (uint16_t) *(uchar_t *)p;
127 sum = (sum & 0xFFFF) + (sum >> 16);
129 return (uint16_t)~sum;
132 static inline void ethernet_packet_init(struct ethernet_pkt *pt, uchar_t *data, const size_t size) {
134 memcpy(pt->data, data, size);
137 static uint_t hash_from_key_fn(addr_t hashkey) {
138 uint8_t * key = (uint8_t *)hashkey;
139 return v3_hash_buffer(key, HASH_KEY_LEN);
142 static int hash_key_equal(addr_t key1, addr_t key2) {
143 uint8_t * buf1 = (uint8_t *)key1;
144 uint8_t * buf2 = (uint8_t *)key2;
145 return (memcmp(buf1, buf2, HASH_KEY_LEN) == 0);
148 static int init_route_cache() {
149 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
151 if (g_route_cache == NULL) {
152 PrintError("Vnet: Route Cache Initiate Failurely\n");
159 static void make_hash_key(route_hashkey_t hashkey,
166 for (j = 0; j < 6; j++) {
167 hashkey[j] = src_addr[j];
168 hashkey[j + 6] = dest_addr[j] + 1;
171 hashkey[12] = src_type;
173 *(int *)(hashkey + 12) = src_index;
176 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * matches) {
177 struct route_cache_entry * new_entry = NULL;
180 new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
181 if (new_entry == NULL) {
182 PrintError("Vnet: Malloc fails\n");
186 new_entry->num_matched_routes = num_matched_r;
188 new_entry->matches = (int *)V3_Malloc(num_matched_r * sizeof(int));
190 if (new_entry->matches == NULL) {
191 PrintError("Vnet: Malloc fails\n");
195 for (i = 0; i < num_matched_r; i++) {
196 new_entry->matches[i] = matches[i];
199 // here, when v3_htable_insert return 0, it means insert fails
200 if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0) {
201 PrintError("Vnet: Insert new route entry to cache failed\n");
202 V3_Free(new_entry->matches);
209 static int clear_hash_cache() {
210 v3_free_htable(g_route_cache, 1, 1);
212 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
214 if (g_route_cache == NULL) {
215 PrintError("Vnet: Route Cache Create Failurely\n");
222 static int look_into_cache(route_hashkey_t hashkey, int * matches) {
223 struct route_cache_entry * found = NULL;
227 found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
230 n_matches = found->num_matched_routes;
232 for (i = 0; i < n_matches; i++) {
233 matches[i] = found->matches[i];
240 #ifdef CONFIG_DEBUG_VNET
241 static inline uint8_t hex_nybble_to_nybble(const uint8_t hexnybble) {
242 uint8_t x = toupper(hexnybble);
247 return 10 + (x - 'A');
251 static inline uint8_t hex_byte_to_byte(const uint8_t hexbyte[2]) {
252 return ((hex_nybble_to_nybble(hexbyte[0]) << 4) +
253 (hex_nybble_to_nybble(hexbyte[1]) & 0xf));
256 static inline void string_to_mac(const char *str, uint8_t mac[6]) {
259 for (k = 0; k < 6; k++) {
260 mac[k] = hex_byte_to_byte(&(str[(2 * k) + k]));
264 static inline void mac_to_string(char mac[6], char * buf) {
265 snprintf(buf, 20, "%x:%x:%x:%x:%x:%x",
266 mac[0], mac[1], mac[2],
267 mac[3], mac[4], mac[5]);
271 static int __add_link_entry(struct link_entry * link) {
274 for (idx = 0; idx < MAX_LINKS; idx++) {
275 if (g_links.links[idx] == NULL) {
276 g_links.links[idx] = link;
283 PrintError("No available Link entry\n");
287 static int __add_route_entry(struct routing_entry * route) {
290 for (idx = 0; idx < MAX_ROUTES; idx++) {
291 if (g_routes.routes[idx] == NULL) {
292 g_routes.routes[idx] = route;
299 PrintError("No available route entry\n");
304 static int vnet_add_route_entry(char src_mac[6],
309 link_type_t link_type,
311 link_type_t src_type) {
312 struct routing_entry * new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry));
315 memset(new_route, 0, sizeof(struct routing_entry));
317 if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
318 memcpy(new_route->src_mac, src_mac, 6);
321 if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
322 memcpy(new_route->dest_mac, dest_mac, 6);
325 new_route->src_mac_qual = src_mac_qual;
326 new_route->dest_mac_qual = dest_mac_qual;
327 new_route->link_idx= link_idx;
328 new_route->link_type = link_type;
329 new_route->src_link_idx = src;
330 new_route->src_type = src_type;
332 if ((idx = __add_route_entry(new_route)) == -1) {
333 PrintError("Could not add route entry\n");
342 static void * __delete_link_entry(int index) {
343 struct link_entry * link = NULL;
347 if ((index >= MAX_LINKS) || (g_links.links[index] == NULL)) {
351 link = g_links.links[index];
352 type = g_links.links[index]->type;
354 if (type == LINK_INTERFACE) {
355 ret = (void *)g_links.links[index]->dst_dev;
356 } else if (type == LINK_EDGE) {
357 ret = (void *)g_links.links[index]->dst_link;
360 g_links.links[index] = NULL;
368 static int find_route_entry(char src_mac[6],
373 link_type_t link_type,
375 link_type_t src_type) {
377 char temp_src_mac[6];
378 char temp_dest_mac[6];
380 if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
381 memcpy(temp_src_mac, src_mac, 6);
383 memset(temp_src_mac, 0, 6);
386 if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
387 memcpy(temp_dest_mac, dest_mac, 6);
389 memset(temp_dest_mac, 0, 6);
392 for (i = 0; i < MAX_ROUTES; i++) {
393 if (g_routes.routes[i] != NULL) {
394 if ((memcmp(temp_src_mac, g_routes.routes[i]->src_mac, 6) == 0) &&
395 (memcmp(temp_dest_mac, g_routes.routes[i]->dest_mac, 6) == 0) &&
396 (g_routes.routes[i]->src_mac_qual == src_mac_qual) &&
397 (g_routes.routes[i]->dest_mac_qual == dest_mac_qual) &&
398 ( (link_type == LINK_ANY) ||
399 ((link_type == g_routes.routes[i]->link_type) && (g_routes.routes[i]->link_idx == link_idx))) &&
400 ( (src_type == LINK_ANY) ||
401 ((src_type == g_routes.routes[i]->src_type) && (g_routes.routes[i]->src_link_idx == src)))) {
410 static int __delete_route_entry(int index) {
411 struct routing_entry * route;
413 if ((index >= MAX_ROUTES) || (g_routes.routes[index] == NULL)) {
414 PrintDebug("VNET: wrong index in delete route entry %d\n", index);
418 route = g_routes.routes[index];
419 g_routes.routes[index] = NULL;
429 static int vnet_delete_route_entry_by_addr(char src_mac[6],
436 link_type_t src_type) {
437 int index = find_route_entry(src_mac, dest_mac, src_mac_qual,
438 dest_mac_qual, link_idx, type, src, src_type);
441 PrintDebug("VNET: wrong in delete route entry %d\n", index);
445 return __delete_route_entry(index);
448 static int match_route(uint8_t * src_mac,
450 link_type_t src_type,
453 int values[MAX_ROUTES];
454 int matched_routes[MAX_ROUTES];
462 for(i = 0; i < MAX_ROUTES; i++) {
463 if (g_routes.routes[i] != NULL){
464 if ( (g_routes.routes[i]->src_type != LINK_ANY) &&
465 ( (g_routes.routes[i]->src_type != src_type) ||
466 ( (g_routes.routes[i]->src_link_idx != src_index) &&
467 (g_routes.routes[i]->src_link_idx != -1)))) {
468 PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
472 if ( (g_routes.routes[i]->dest_mac_qual == MAC_ANY) &&
473 (g_routes.routes[i]->src_mac_qual == MAC_ANY)) {
475 matched_routes[num_matches] = i;
476 values[num_matches] = 3;
480 if (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) {
481 if (g_routes.routes[i]->src_mac_qual != MAC_NOT) {
482 if (g_routes.routes[i]->dest_mac_qual == MAC_ANY) {
483 matched_routes[num_matches] = i;
484 values[num_matches] = 6;
487 } else if (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) == 0) {
488 if (g_routes.routes[i]->dest_mac_qual != MAC_NOT) {
489 matched_routes[num_matches] = i;
490 values[num_matches] = 8;
498 if (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) == 0) {
499 if (g_routes.routes[i]->dest_mac_qual != MAC_NOT) {
500 if (g_routes.routes[i]->src_mac_qual == MAC_ANY) {
501 matched_routes[num_matches] = i;
502 values[num_matches] = 6;
505 } else if (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) {
506 if (g_routes.routes[i]->src_mac_qual != MAC_NOT) {
507 if (exact_match == 0) {
508 matched_routes[num_matches] = i;
509 values[num_matches] = 8;
517 if ( (g_routes.routes[i]->dest_mac_qual == MAC_NOT) &&
518 (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) != 0)) {
519 if (g_routes.routes[i]->src_mac_qual == MAC_ANY) {
520 matched_routes[num_matches] = i;
521 values[num_matches] = 5;
523 } else if (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) {
524 if (g_routes.routes[i]->src_mac_qual != MAC_NOT) {
525 matched_routes[num_matches] = i;
526 values[num_matches] = 7;
532 if ( (g_routes.routes[i]->src_mac_qual == MAC_NOT) &&
533 (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) != 0)) {
534 if (g_routes.routes[i]->dest_mac_qual == MAC_ANY) {
535 matched_routes[num_matches] = i;
536 values[num_matches] = 5;
538 } else if (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) == 0) {
539 if (g_routes.routes[i]->dest_mac_qual != MAC_NOT) {
540 matched_routes[num_matches] = i;
541 values[num_matches] = 7;
549 for (i = 0; i < MAX_ROUTES; i++) {
550 if ( (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) &&
551 (g_routes.routes[i]->dest_mac_qual == MAC_NONE) &&
552 ( (g_routes.routes[i]->src_type == LINK_ANY) ||
553 ( (g_routes.routes[i]->src_type == src_type) &&
554 ( (g_routes.routes[i]->src_link_idx == src_index) ||
555 (g_routes.routes[i]->src_link_idx == -1))))) {
556 matched_routes[num_matches] = i;
557 values[num_matches] = 4;
558 PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
563 //If many rules have been matched, we choose one which has the highest value rating
564 if (num_matches == 0) {
568 for (i = 0; i < num_matches; i++) {
569 if (values[i] > max) {
572 matches[no] = matched_routes[i];
574 } else if (values[i] == max) {
575 matches[no] = matched_routes[i];
583 static int handle_one_pkt(struct ethernet_pkt *pkt) {
584 int src_link_index = -1; //the value of src_link_index of udp always is 0
589 int matches[MAX_ROUTES];
590 int num_matched_routes = 0;
592 struct in_pkt_header header;
594 char hash_key[hash_key_size];
596 // get the ethernet and ip headers from the packet
597 memcpy((void *)&header, (void *)pkt->data, sizeof(header));
598 memcpy(src_mac, header.ethernetsrc, 6);
599 memcpy(dst_mac, header.ethernetdest, 6);
601 #ifdef CONFIG_DEBUG_VNET
605 mac_to_string(src_mac, src_str);
606 mac_to_string(dst_mac, dest_str);
608 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
611 // link_edge -> pt->type???
612 make_hash_key(hash_key, src_mac, dst_mac, LINK_EDGE, src_link_index);
614 num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
616 if (num_matched_routes == -1) {
617 // no match in the cache
618 num_matched_routes = match_route(src_mac, dst_mac, LINK_ANY, src_link_index, matches);
620 if (num_matched_routes > 0) {
621 add_route_to_cache(hash_key, num_matched_routes,matches);
625 PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
627 for (i = 0; i < num_matched_routes; i++) {//send packet to all destinations
628 int route_index = -1;
631 struct link_entry * link = NULL;
633 route_index = matches[i];
635 PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
637 link_index = g_routes.routes[route_index]->link_idx;
639 if ((link_index < 0) ||
640 (link_index > MAX_LINKS) ||
641 (g_links.links[link_index] == NULL)) {
645 link = g_links.links[link_index];
647 if (g_routes.routes[route_index]->link_type == LINK_EDGE) {
649 //apply the header in the beginning of the packet to be sent
650 if (link->dst_link->pro_type == UDP_TYPE) {
651 struct udp_link_header *hdr = &(link->dst_link->vnet_header);
652 struct ip_header *ip = &hdr->ip_hdr;
653 struct udp_header *udp = &hdr->udp_hdr;
654 udp->len = pkt_len + sizeof(struct udp_header);
655 ip->total_len = pkt_len + sizeof(struct udp_header) + sizeof(struct ip_header);
656 ip->cksum = ip_xsum(ip, sizeof(struct ip_header));
658 int hdr_size = sizeof(struct udp_link_header);
659 memcpy(&pkt->ext_hdr, hdr, hdr_size);
662 if ((link->dst_link->input((uchar_t *)&pkt->ext_hdr, pkt_len, link->dst_link->private_data)) != pkt_len) {
663 PrintDebug("VNET: Packet not sent properly\n");
667 PrintDebug("VNET: Link protocol type not support\n");
670 } else if (g_routes.routes[route_index]->link_type == LINK_INTERFACE) {
671 if ((link->dst_dev->input(pkt->data, pkt_len, link->dst_dev->private_data)) != pkt_len) {
672 PrintDebug("VNET: Packet not sent properly\n");
676 PrintDebug("Vnet: Wrong Edge type\n");
684 static int send_ethernet_pkt(uchar_t *data, int len) {
685 struct ethernet_pkt *pkt;
687 pkt = (struct ethernet_pkt *)V3_Malloc(sizeof(struct ethernet_pkt));
688 memset(pkt, 0, sizeof(struct ethernet_pkt));
691 PrintError("VNET: Memory allocate fails\n");
695 ethernet_packet_init(pkt, data, len); //====here we copy sending data once
697 PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pkt->size);
698 print_packet((char *)data, len);
700 v3_enqueue(g_inpkt_q, (addr_t)pkt);
705 //send raw ethernet packet
706 int v3_vnet_send_rawpkt(uchar_t * buf, int len, void *private_data) {
707 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", len);
709 return send_ethernet_pkt(buf, len);
712 //sending the packet from Dom0, should remove the link header
713 int v3_vnet_send_udppkt(uchar_t * buf, int len, void *private_data) {
714 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", len);
716 uint_t hdr_len = sizeof(struct udp_link_header);
718 return send_ethernet_pkt((uchar_t *)(buf+hdr_len), len - hdr_len);
721 static int search_device(char * device_name) {
724 for (i = 0; i < MAX_LINKS; i++) {
725 if ((g_links.links[i] != NULL) && (g_links.links[i]->type == LINK_INTERFACE)) {
726 if (strcmp(device_name, g_links.links[i]->dst_dev->name) == 0) {
735 int vnet_register_device(struct vm_device * vdev,
738 int (*netif_input)(uchar_t * pkt, uint_t size, void * private_data),
740 struct vnet_if_device * if_dev;
742 int idx = search_device(dev_name);
745 PrintDebug("VNET: register device: Already has device with the name %s\n", dev_name);
749 if_dev = (struct vnet_if_device *)(sizeof(struct vnet_if_device));
751 if (if_dev == NULL) {
752 PrintError("VNET: Malloc fails\n");
756 strcpy(if_dev->name, dev_name);
757 strncpy(if_dev->mac_addr, mac, 6);
759 if_dev->input = netif_input;
760 if_dev->private_data = data;
762 struct link_entry * link = (struct link_entry *)(sizeof(struct link_entry));
764 link->type = LINK_INTERFACE;
765 link->dst_dev = if_dev;
767 idx = __add_link_entry(link);
769 PrintDebug("VNET: Add device %s to link table, idx %d", dev_name, idx);
775 static int vnet_unregister_device(char * dev_name) {
778 idx = search_device(dev_name);
781 PrintDebug("VNET: No device with name %s found\n", dev_name);
785 struct vnet_if_device * device = (struct vnet_if_device *)__delete_link_entry(idx);
786 if (device == NULL) {
787 PrintError("VNET: Device %s not in the link table %d, something may be wrong in link table\n", dev_name, idx);
798 int v3_vnet_pkt_process() {
799 struct ethernet_pkt * pkt;
801 PrintDebug("VNET: In vnet_check\n");
803 while ((pkt = (struct ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) {
804 PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pkt->size, (int)pkt->type);
805 v3_hexdump(pkt->data, pkt->size, NULL, 0);
807 if (handle_one_pkt(pkt)) {
808 PrintDebug("VNET: vnet_check: handle one packet!\n");
810 PrintError("VNET: vnet_check: fail to forward one packet, discard it!\n");
813 V3_Free(pkt); // be careful here
820 static void init_empty_link_table() {
823 for (i = 0; i < MAX_LINKS; i++) {
824 g_links.links[i] = NULL;
830 static void init_empty_route_table() {
833 for (i = 0; i < MAX_ROUTES; i++) {
834 g_routes.routes[i] = NULL;
840 static void init_tables() {
841 init_empty_link_table();
842 init_empty_route_table();
846 static void init_pkt_queue() {
847 PrintDebug("VNET Init package receiving queue\n");
849 g_inpkt_q = v3_create_queue();
850 v3_init_queue(g_inpkt_q);
853 static void free_link_mem(struct link_entry *link){
854 V3_Free(link->dst_dev);
859 static int addto_routing_link_tables(struct routing_entry *route_tab,
861 struct link_entry *link_tab,
863 struct routing_entry *route, *new_route;
864 struct link_entry *link, *new_link;
867 int link_idxs[MAX_LINKS];
869 //add all of the links first, record their new indexs
870 for (i = 0; i < num_links; i++) {
873 new_link = (struct link_entry *)V3_Malloc(sizeof(struct link_entry));
875 if (new_link == NULL){
876 PrintError("VNET: Memory allocate error\n");
880 new_link->type = link->type;
882 //TODO: how to set the input parameters here
883 if (link->type == LINK_EDGE){
884 struct vnet_if_device *ifdev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
887 PrintError("VNET: Memory allocate fails\n");
891 memcpy(ifdev->name, link->dst_dev->name, DEVICE_NAME_LEN);
896 //ifdev->private_data
898 new_link->dst_dev = ifdev;
899 }else if (link->type == LINK_INTERFACE){
900 struct vnet_if_link *iflink = (struct vnet_if_link *)V3_Malloc(sizeof(struct vnet_if_link));
903 PrintError("VNET: Memory allocate fails\n");
906 iflink->pro_type = link->dst_link->pro_type;
907 iflink->dest_ip = link->dst_link->dest_ip;
908 iflink->dest_port = link->dst_link->dest_port;
909 memcpy(&iflink->vnet_header, &link->dst_link->vnet_header, sizeof(struct udp_link_header));
913 //iflink->private_data =
915 new_link->dst_link = iflink;
917 PrintDebug("VNET: invalid link type\n");
922 new_idx = __add_link_entry(new_link);
924 PrintError("VNET: Adding link fails\n");
925 free_link_mem(new_link);
928 link_idxs[i] = new_idx;
931 //add all of routes, replace with new link indexs
932 for (i = 0; i < num_routes; i++) {
933 route = &route_tab[i];
934 if (route->link_idx < 0 || route->link_idx >= num_links ||
935 ((route->src_link_idx != -1) &&
936 (route->src_link_idx < 0 || route->src_link_idx >= num_links))){
937 PrintError("VNET: There is error in the intial tables data from guest\n");
941 new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry));
943 if (new_route == NULL){
944 PrintError("VNET: Memory allocate fails\n");
947 memcpy(new_route, route, sizeof(struct routing_entry));
949 new_route->link_idx = link_idxs[new_route->link_idx];
951 if (route->src_link_idx != -1)
952 new_route->src_link_idx = link_idxs[new_route->src_link_idx];
954 if((__add_route_entry(new_route)) == -1){
955 PrintDebug("VNET: Adding route fails");
965 struct table_init_info {
966 addr_t routing_table_start;
967 uint16_t routing_table_size;
968 addr_t link_table_start;
969 uint16_t link_table_size;
972 //add the guest specified routes and links to the tables
973 static int handle_init_tables_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
974 addr_t guest_addr = (addr_t)info->vm_regs.rcx;
975 addr_t info_addr, route_addr, link_addr;
976 struct table_init_info *init_info;
977 struct link_entry *link_array;
978 struct routing_entry *route_array;
980 if (guest_va_to_host_va(info, guest_addr, &info_addr) == -1) {
981 PrintError("VNET: Could not translate guest address\n");
985 init_info = (struct table_init_info *)info_addr;
987 if (guest_va_to_host_va(info, init_info->routing_table_start, &route_addr) == -1) {
988 PrintError("VNET: Could not translate guest address\n");
991 route_array = (struct routing_entry *)route_addr;
993 if (guest_va_to_host_va(info, init_info->link_table_start, &link_addr) == -1) {
994 PrintError("VNET: Could not translate guest address\n");
997 link_array = (struct link_entry *)link_addr;
999 addto_routing_link_tables(route_array, init_info->routing_table_size, link_array, init_info->link_table_size);
1005 void v3_vnet_init(struct guest_info * vm) {
1009 v3_register_hypercall(vm, VNET_INITAB_HCALL, handle_init_tables_hcall, NULL);
1011 PrintDebug("VNET Initialized\n");