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 <devices/vnet.h>
38 #define INTERFACE "INTERFACE"
42 #define INTERFACE_TYPE 0
44 #define ANY_SRC_TYPE 2
46 struct raw_ethernet_pkt {
48 int type; // vm or link type: INTERFACE|EDGE
49 char data[ETHERNET_PACKET_LEN];
52 //static char *vnet_version = "0.9";
53 static int vnet_server = 0;
54 static bool use_tcp = false;
56 static uint_t vnet_udp_port = 22;
60 #define MAX_DEVICES 16
63 static struct topology g_links[MAX_LINKS];
65 static int g_num_links; //The current number of links
66 static int g_first_link;
67 static int g_last_link;
69 static struct routing g_routes[MAX_ROUTES];
70 static int g_num_routes; //The current number of routes
71 static int g_first_route;
72 static int g_last_route;
74 static struct device_list g_devices[MAX_DEVICES];
75 static int g_num_devices;
76 static int g_first_device;
77 static int g_last_device;
79 static SOCK g_udp_sockfd;
80 static struct gen_queue * g_inpkt_q; //packet receiving queue
83 static void print_packet(char *pkt, int size) {
84 PrintDebug("Vnet: print_data_packet: size: %d\n", size);
85 v3_hexdump(pkt, size, NULL, 0);
89 static void print_packet_addr(char *pkt) {
90 PrintDebug("Vnet: print_packet_destination_addr: ");
91 v3_hexdump(pkt + 8, 6, NULL, 0);
93 PrintDebug("Vnet: print_packet_source_addr: ");
94 v3_hexdump(pkt + 14, 6, NULL, 0);
97 static void print_device_addr(char *ethaddr) {
98 PrintDebug("Vnet: print_device_addr: ");
99 v3_hexdump(ethaddr, 6, NULL, 0);
103 //network connection functions
106 static int raw_ethernet_pkt_sendUdp(struct raw_ethernet_pkt *pt, int sock_fd, int ip, short port) {
109 PrintDebug("Vnet: sending by UDP socket %d ip: %x, port: %d\n", sock_fd, ip, port);
111 if ((size = V3_SendTo_IP(sock_fd, ip, port, pt->data, pt->size)) != pt->size) {
112 PrintError("Vnet: sending by UDP Exception, %x\n", size);
121 static void raw_ethernet_packet_init(struct raw_ethernet_pkt * pt, const char *data, const size_t size) {
123 memcpy(pt->data, data, size);
128 #define HASH_KEY_SIZE 16
129 #define MIN_CACHE_SIZE 100
133 * 6-11: dest_eth_addr
137 typedef char * route_hashkey_t;
139 // This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY
140 struct route_cache_entry {
141 int num_matched_routes;
145 //Header of the route cache
146 static struct hashtable * g_route_cache;
148 static uint_t hash_from_key_fn(addr_t hashkey) {
153 char * key = (char *)hashkey;
155 for (i = 0; i < HASH_KEY_SIZE; i++) {
156 hash = (hash << 4) + *(key + i) + i;
158 if ((temp = (hash & 0xF0000000))) {
159 hash ^= (temp >> 24);
164 PrintDebug("Hash Value: %lu\n", (unsigned long)hash);
169 static int hash_key_equal(addr_t left, addr_t right) {
174 key2 = (char *)right;
176 for (i = 0; i < HASH_KEY_SIZE; i++) {
177 if (key1[i] != key2[i]) {
178 PrintDebug("HASHes not equal\n");
185 static int init_route_cache() {
186 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
188 if (g_route_cache == NULL){
189 PrintError("Vnet: Route Cache Initiate Failurely\n");
196 static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
199 for (j = 0; j < 6; j++) {
200 hashkey[j] = src_addr[j];
201 hashkey[j + 6] = dest_addr[j] + 1;
204 hashkey[12] = src_type;
206 *(int *)(hashkey + 12) = src_index;
209 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * matches) {
210 struct route_cache_entry * new_entry = NULL;
213 new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
214 if (new_entry == NULL){
215 PrintError("Vnet: Malloc fails\n");
219 new_entry->num_matched_routes = num_matched_r;
221 new_entry->matches = (int *)V3_Malloc(num_matched_r * sizeof(int));
223 if (new_entry->matches == NULL){
224 PrintError("Vnet: Malloc fails\n");
228 for(i = 0; i < num_matched_r; i++) {
229 new_entry->matches[i] = matches[i];
232 //here, when v3_htable_insert return 0, it means insert fails
233 if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0){
234 PrintError("Vnet: Insert new route entry to cache failed\n");
235 V3_Free(new_entry->matches);
242 static int clear_hash_cache() {
243 v3_free_htable(g_route_cache, 1, 1);
245 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
247 if (g_route_cache == NULL){
248 PrintError("Vnet: Route Cache Create Failurely\n");
255 static int look_into_cache(route_hashkey_t hashkey, int * matches) {
258 struct route_cache_entry * found = NULL;
260 found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
263 n_matches = found->num_matched_routes;
265 for (i = 0; i < n_matches; i++) {
266 matches[i] = found->matches[i];
276 static inline uint8_t hex_nybble_to_nybble(const uint8_t hexnybble) {
277 uint8_t x = toupper(hexnybble);
282 return 10 + (x - 'A');
286 static inline uint8_t hex_byte_to_byte(const uint8_t hexbyte[2]) {
287 return ((hex_nybble_to_nybble(hexbyte[0]) << 4) +
288 (hex_nybble_to_nybble(hexbyte[1]) & 0xf));
292 static inline void string_to_mac(const char * str, uint8_t mac[6]) {
295 for (k = 0; k < 6; k++) {
296 mac[k] = hex_byte_to_byte(&(str[(2 * k) + k]));
300 static inline void mac_to_string(uint8_t mac[6], char * buf) {
301 snprintf("%x:%x:%x:%x:%x:%x",
302 mac[0], mac[1], mac[2],
303 mac[3], mac[4], mac[5]);
307 static void ip_to_string(uint32_t addr, char * buf) {
311 addr_st = v3_htonl(addr);
312 tmp_str = v3_inet_ntoa(addr_st);
314 memcpy(buf, tmp_str, strlen(tmp_str));
318 int find_link_by_fd(SOCK sock) {
321 FOREACH_LINK(i, g_links, g_first_link) {
322 if (g_links[i].link_sock == sock) {
330 int vnet_add_link_entry(unsigned long dest, int type, int data_port, SOCK fd) {
333 for (i = 0; i < MAX_LINKS; i++) {
334 if (g_links[i].use == 0) {
335 g_links[i].dest = dest;
336 g_links[i].type = type;
337 g_links[i].link_sock = fd;
338 g_links[i].remote_port = data_port;
341 if (g_first_link == -1) {
345 g_links[i].prev = g_last_link;
346 g_links[i].next = -1;
348 if (g_last_link != -1) {
349 g_links[g_last_link].next = i;
364 int add_sock(struct sock_list *socks, int len, int *first_sock, int *last_sock, SOCK fd) {
367 for (i = 0; i < len; i++) {
368 if (socks[i].sock == -1) {
371 if (*first_sock == -1) {
375 socks[i].prev = *last_sock;
378 if (*last_sock != -1) {
379 socks[*last_sock].next = i;
391 int vnet_add_route_entry(char src_mac[6], char dest_mac[6], int src_mac_qual, int dest_mac_qual, int dest, int type, int src, int src_type) {
394 for(i = 0; i < MAX_ROUTES; i++) {
395 if (g_routes[i].use == 0) {
397 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
398 memcpy(g_routes[i].src_mac, src_mac, 6);
400 memset(g_routes[i].src_mac, 0, 6);
403 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
404 memcpy(g_routes[i].dest_mac, dest_mac, 6);
406 memset(g_routes[i].dest_mac, 0, 6);
409 g_routes[i].src_mac_qual = src_mac_qual;
410 g_routes[i].dest_mac_qual = dest_mac_qual;
411 g_routes[i].dest = dest;
412 g_routes[i].type = type;
413 g_routes[i].src = src;
414 g_routes[i].src_type = src_type;
417 if (g_first_route == -1) {
421 g_routes[i].prev = g_last_route;
422 g_routes[i].next = -1;
424 if (g_last_route != -1) {
425 g_routes[g_last_route].next = i;
442 static int find_link_entry(unsigned long dest, int type) {
445 FOREACH_LINK(i, g_links, g_first_link) {
446 if ((g_links[i].dest == dest) &&
447 ((type == -1) || (g_links[i].type == type)) ) {
455 static int delete_link_entry(int index) {
459 if (g_links[index].use == 0) {
463 g_links[index].dest = 0;
464 g_links[index].type = 0;
465 g_links[index].link_sock = -1;
466 g_links[index].use = 0;
468 prev_i = g_links[index].prev;
469 next_i = g_links[index].next;
472 g_links[prev_i].next = g_links[index].next;
476 g_links[next_i].prev = g_links[index].prev;
479 if (g_first_link == index) {
480 g_first_link = g_links[index].next;
483 if (g_last_link == index) {
484 g_last_link = g_links[index].prev;
487 g_links[index].next = -1;
488 g_links[index].prev = -1;
495 int vnet_delete_link_entry_by_addr(unsigned long dest, int type) {
496 int index = find_link_entry(dest, type);
502 return delete_link_entry(index);
506 static int find_route_entry(char src_mac[6],
515 char temp_src_mac[6];
516 char temp_dest_mac[6];
518 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
519 memcpy(temp_src_mac, src_mac, 6);
521 memset(temp_src_mac, 0, 6);
524 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
525 memcpy(temp_dest_mac, dest_mac, 6);
527 memset(temp_dest_mac, 0, 6);
530 FOREACH_LINK(i, g_routes, g_first_route) {
531 if ( (memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) &&
532 (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
533 (g_routes[i].src_mac_qual == src_mac_qual) &&
534 (g_routes[i].dest_mac_qual == dest_mac_qual) &&
536 ( (type == g_routes[i].type) && (g_routes[i].dest == dest)) ) &&
537 ( (src_type == -1) ||
538 ( (src_type == g_routes[i].src_type) && (g_routes[i].src == src)) ) ) {
546 static int delete_route_entry(int index) {
551 memset(g_routes[index].src_mac, 0, 6);
552 memset(g_routes[index].dest_mac, 0, 6);
554 g_routes[index].dest = 0;
555 g_routes[index].src = 0;
556 g_routes[index].src_mac_qual = 0;
557 g_routes[index].dest_mac_qual = 0;
558 g_routes[index].type = -1;
559 g_routes[index].src_type = -1;
560 g_routes[index].use = 0;
562 prev_i = g_routes[index].prev;
563 next_i = g_routes[index].next;
566 g_routes[prev_i].next = g_routes[index].next;
570 g_routes[next_i].prev = g_routes[index].prev;
573 if (g_first_route == index) {
574 g_first_route = g_routes[index].next;
577 if (g_last_route == index) {
578 g_last_route = g_routes[index].prev;
581 g_routes[index].next = -1;
582 g_routes[index].prev = -1;
591 int vnet_delete_route_entry_by_addr(char src_mac[6],
599 int index = find_route_entry(src_mac, dest_mac, src_mac_qual,
600 dest_mac_qual, dest, type, src, src_type);
606 delete_route_entry(index);
611 int delete_sock(struct sock_list * socks, int * first_sock, int * last_sock, SOCK fd) {
617 FOREACH_SOCK(i, socks, (*first_sock)) {
618 if (socks[i].sock == fd) {
619 V3_Close_Socket(socks[i].sock);
622 prev_i = socks[i].prev;
623 next_i = socks[i].next;
626 socks[prev_i].next = socks[i].next;
630 socks[next_i].prev = socks[i].prev;
633 if (*first_sock == i) {
634 *first_sock = socks[i].next;
637 if (*last_sock == i) {
638 *last_sock = socks[i].prev;
650 //setup the topology of the testing network
651 static void store_topologies(SOCK fd) {
653 int src_mac_qual = ANY_TYPE;
654 int dest_mac_qual = ANY_TYPE;
657 dest = (0 | 172 << 24 | 23 << 16 | 1 );
658 PrintDebug("VNET: store_topologies. NOT VNET_SERVER, dest = %x\n", dest);
660 dest = (0 | 172 << 24 | 23 << 16 | 2 );
661 PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest);
666 int src_type= ANY_SRC_TYPE;
670 for (i = 0; i < MAX_LINKS; i++) {
671 if (g_links[i].use == 0) {
672 g_links[i].dest = (int)dest;
673 g_links[i].type = type;
674 g_links[i].link_sock = fd;
675 g_links[i].remote_port = data_port;
678 if (g_first_link == -1) {
682 g_links[i].prev = g_last_link;
683 g_links[i].next = -1;
685 if (g_last_link != -1) {
686 g_links[g_last_link].next = i;
692 PrintDebug("VNET: store_topologies. new link: socket: %d, remote %x:[%d]\n", g_links[i].link_sock, (uint_t)g_links[i].dest, g_links[i].remote_port);
702 for (i = 0; i < MAX_ROUTES; i++) {
703 if (g_routes[i].use == 0) {
704 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
705 // memcpy(g_routes[i].src_mac, src_mac, 6);
707 memset(g_routes[i].src_mac, 0, 6);
710 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
711 // memcpy(g_routes[i].dest_mac, dest_mac, 6);
713 memset(g_routes[i].dest_mac, 0, 6);
716 g_routes[i].src_mac_qual = src_mac_qual;
717 g_routes[i].dest_mac_qual = dest_mac_qual;
718 g_routes[i].dest = (int)dest;
719 g_routes[i].type = type;
720 g_routes[i].src = src;
721 g_routes[i].src_type = src_type;
725 if (g_first_route == -1) {
729 g_routes[i].prev = g_last_route;
730 g_routes[i].next = -1;
732 if (g_last_route != -1) {
733 g_routes[g_last_route].next = i;
740 PrintDebug("VNET: store_topologies. new route: src_mac: %s, dest_mac: %s, dest: %d\n", g_routes[i].src_mac, g_routes[i].dest_mac, dest);
746 static int match_route(char *src_mac, char *dst_mac, int src_type, int src_index, int * matches) {
747 int values[MAX_ROUTES];
748 int matched_routes[MAX_ROUTES];
756 FOREACH_ROUTE(i, g_routes, g_first_route) {
757 if ((g_routes[i].src_type != ANY_SRC_TYPE) &&
758 ((g_routes[i].src_type != src_type) ||
759 ((g_routes[i].src != src_index) &&
760 (g_routes[i].src != -1)))) {
761 PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
765 if ( (g_routes[i].dest_mac_qual == ANY_TYPE) &&
766 (g_routes[i].src_mac_qual == ANY_TYPE) ) {
767 matched_routes[num_matches] = i;
768 values[num_matches] = 3;
772 if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
773 if (g_routes[i].src_mac_qual != NOT_TYPE) {
774 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
775 matched_routes[num_matches] = i;
776 values[num_matches] = 6;
779 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
780 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
781 matched_routes[num_matches] = i;
782 values[num_matches] = 8;
790 if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
791 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
792 if (g_routes[i].src_mac_qual == ANY_TYPE) {
793 matched_routes[num_matches] = i;
794 values[num_matches] = 6;
797 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
798 if (g_routes[i].src_mac_qual != NOT_TYPE) {
799 if (exact_match == 0) {
800 matched_routes[num_matches] = i;
801 values[num_matches] = 8;
809 if ((g_routes[i].dest_mac_qual == NOT_TYPE) &&
810 (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0)) {
811 if (g_routes[i].src_mac_qual == ANY_TYPE) {
812 matched_routes[num_matches] = i;
813 values[num_matches] = 5;
815 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
816 if (g_routes[i].src_mac_qual != NOT_TYPE) {
817 matched_routes[num_matches] = i;
818 values[num_matches] = 7;
824 if ((g_routes[i].src_mac_qual == NOT_TYPE) &&
825 (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0)) {
826 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
827 matched_routes[num_matches] = i;
828 values[num_matches] = 5;
830 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
831 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
832 matched_routes[num_matches] = i;
833 values[num_matches] = 7;
841 FOREACH_ROUTE(i, g_routes, g_first_route) {
842 if ((memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
843 (g_routes[i].dest_mac_qual == NONE_TYPE) &&
844 ((g_routes[i].src_type == ANY_SRC_TYPE) ||
845 ((g_routes[i].src_type == src_type) &&
846 ((g_routes[i].src == src_index) ||
847 (g_routes[i].src == -1))))) {
848 matched_routes[num_matches] = i;
849 values[num_matches] = 4;
850 PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
855 //If many rules have been matched, we choose one which has the highest value rating
856 if (num_matches == 0) {
860 for (i = 0; i < num_matches; i++) {
861 if (values[i] > max) {
864 matches[no] = matched_routes[i];
866 } else if (values[i] == max) {
867 matches[no] = matched_routes[i];
875 static inline int if_write_pkt(struct vnet_if_device *iface, struct raw_ethernet_pkt * pkt) {
876 return iface->input((uchar_t *)pkt->data, pkt->size);
879 static int handle_one_pkt(struct raw_ethernet_pkt * pkt) {
880 int src_link_index = 0; //the value of src_link_index of udp always is 0
885 int matches[g_num_routes];
886 int num_matched_routes = 0;
888 struct HEADERS headers;
890 // get the ethernet and ip headers from the packet
891 memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
894 for (j = 0;j < 6; j++) {
895 src_mac[j] = headers.ethernetsrc[j];
896 dst_mac[j] = headers.ethernetdest[j];
904 mac_to_string(src_mac, src_str);
905 mac_to_string(dst_mac, dest_str);
907 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
910 char hash_key[HASH_KEY_SIZE];
911 make_hash_key(hash_key, src_mac, dst_mac, EDGE_TYPE, src_link_index);
913 num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
915 if (num_matched_routes == -1) { //no match
916 num_matched_routes = match_route(src_mac, dst_mac, pkt->type, src_link_index, matches);
918 if (num_matched_routes > 0) {
919 add_route_to_cache(hash_key, num_matched_routes,matches);
923 PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
925 for (i = 0; i < num_matched_routes; i++) {
926 int route_index = -1;
930 route_index = matches[i];
932 PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
934 if (g_routes[route_index].type == EDGE_TYPE) {
935 link_index = g_routes[route_index].dest;
937 if(g_links[link_index].type == UDP_TYPE) {
940 if ((size = V3_SendTo_IP(g_links[link_index].link_sock, g_links[link_index].dest, g_links[link_index].remote_port, pkt->data, pkt->size)) != pkt->size) {
941 PrintError("Vnet: sending by UDP Exception, %x\n", size);
945 PrintDebug("Vnet: HandleDataOverLink: Serializing UDP Packet to link_sock [%d], dest [%x], remote_port [%d], size [%d]\n", g_links[link_index].link_sock, (uint_t)g_links[link_index].dest, g_links[link_index].remote_port, (int)pkt->size);
947 } else if (g_links[link_index].type == TCP_TYPE) {
950 } else if (g_routes[route_index].type == INTERFACE_TYPE) {
951 dev_index = g_routes[route_index].dest;
953 PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device->name);
955 if (if_write_pkt(g_devices[dev_index].device, pkt) == -1) {
956 PrintDebug("Can't write output packet to link\n");
960 PrintDebug("Vnet: Wrong Edge type\n");
967 static int send_ethernet_pkt(char * buf, int length) {
968 struct raw_ethernet_pkt * pt;
970 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
971 raw_ethernet_packet_init(pt, buf, length); //====here we copy sending data once
973 PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pt->size);
974 print_packet((char *)buf, length);
976 v3_enqueue(g_inpkt_q, (addr_t)pt);
981 int V3_Send_pkt(uchar_t *buf, int length) {
982 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
984 return send_ethernet_pkt((char *)buf, length);
987 static int add_device_to_table(struct vnet_if_device*device, int type) {
990 for (i = 0; i < MAX_DEVICES; i++) {
991 if (g_devices[i].use == 0) {
992 g_devices[i].type = type;
993 g_devices[i].use = 1;
995 if (g_first_device == -1) {
999 g_devices[i].prev = g_last_device;
1000 g_devices[i].next = -1;
1002 if (g_last_device != -1) {
1003 g_devices[g_last_device].next = i;
1016 static int search_device(char *device_name) {
1019 for (i = 0; i < MAX_DEVICES; i++) {
1020 if (g_devices[i].use == 1) {
1021 if (!strcmp(device_name, g_devices[i].device->name)) {
1030 static struct vnet_if_device * delete_device_from_table(int index) {
1033 struct vnet_if_device * device = NULL;
1035 if (g_devices[index].use == 0) {
1039 g_devices[index].use = 0;
1041 prev_i = g_devices[index].prev;
1042 next_i = g_devices[index].next;
1045 g_devices[prev_i].next = g_devices[index].next;
1049 g_devices[next_i].prev = g_devices[index].prev;
1052 if (g_first_device == index) {
1053 g_first_device = g_devices[index].next;
1056 if (g_last_device == index) {
1057 g_last_device = g_devices[index].prev;
1060 g_devices[index].next = -1;
1061 g_devices[index].prev = -1;
1063 device = g_devices[index].device;
1064 g_devices[index].device = NULL;
1072 int vnet_register_device(char * dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void * data) {
1073 struct vnet_if_device * dev;
1075 dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
1078 PrintError("VNET: Malloc fails\n");
1082 strncpy(dev->name, dev_name, 50);
1083 dev->input = netif_input;
1086 if (add_device_to_table(dev, GENERAL_NIC) == -1) {
1093 int vnet_unregister_device(char * dev_name) {
1096 i = search_device(dev_name);
1102 struct vnet_if_device * device = delete_device_from_table(i);
1104 if (device == NULL) {
1113 int V3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size)) {
1114 return vnet_register_device("NE2000", netif_input, NULL);
1117 int vnet_pkt_process() {
1118 struct raw_ethernet_pkt * pt;
1121 PrintDebug("VNET: In vnet_check\n");
1123 while ((pt = (struct raw_ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) {
1124 PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pt->size, (int)pt->type);
1125 v3_hexdump(pt->data, pt->size, NULL, 0);
1127 if(handle_one_pkt(pt)) {
1128 PrintDebug("VNET: vnet_check: handle one packet!\n");
1131 V3_Free(pt); //be careful here
1137 static int process_tcpdata() {
1141 static int process_udpdata() {
1142 struct raw_ethernet_pkt * pt;
1145 uint16_t remote_port = 0;
1146 SOCK link_sock = g_udp_sockfd;
1147 int length = sizeof(struct raw_ethernet_pkt) - (2 * sizeof(int)); //minus the "size" and "type"
1149 //run in a loop to get packets from outside network, adding them to the incoming packet queue
1151 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1153 PrintError("Vnet: process_udp: Malloc fails\n");
1157 PrintDebug("Vnet: route_thread: socket: [%d]. ready to receive from ip [%x], port [%d] or from VMs\n", link_sock, (uint_t)dest, remote_port);
1158 pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length);
1159 PrintDebug("Vnet: route_thread: socket: [%d] receive from ip [%x], port [%d]\n", link_sock, (uint_t)dest, remote_port);
1161 if (pt->size <= 0) {
1162 PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n");
1167 PrintDebug("Vnet: process_udp: get packet\n");
1168 print_packet(pt->data, pt->size);
1170 v3_enqueue(g_inpkt_q, (addr_t)pt);
1176 static int indata_handler( ) {
1186 static int start_recv_data() {
1190 SOCK udp_data_socket;
1192 if ((udp_data_socket = V3_Create_UDP_Socket()) < 0) {
1193 PrintError("VNET: Can't setup udp socket\n");
1197 PrintDebug("Vnet: vnet_setup_udp: get socket: %d\n", udp_data_socket);
1198 g_udp_sockfd = udp_data_socket;
1200 store_topologies(udp_data_socket);
1202 if (V3_Bind_Socket(udp_data_socket, vnet_udp_port) < 0) {
1203 PrintError("VNET: Can't bind socket\n");
1207 PrintDebug("VNET: vnet_setup_udp: bind socket successful\n");
1210 V3_CREATE_THREAD(&indata_handler, NULL, "VNET_DATA_HANDLER");
1215 static void init_link_table() {
1218 for (i = 0; i < MAX_LINKS; i++) {
1220 g_links[i].next = -1;
1221 g_links[i].prev = -1;
1229 static void init_device_table() {
1232 for (i = 0; i < MAX_DEVICES; i++) {
1233 g_devices[i].use = 0;
1234 g_devices[i].next = -1;
1235 g_devices[i].prev = -1;
1238 g_first_device = -1;
1243 static void init_route_table() {
1246 for (i = 0; i < MAX_ROUTES; i++) {
1247 g_routes[i].use = 0;
1248 g_routes[i].next = -1;
1249 g_routes[i].prev = -1;
1257 static void init_tables() {
1259 init_device_table();
1264 static void init_pkt_queue() {
1265 PrintDebug("VNET Init package receiving queue\n");
1267 g_inpkt_q = v3_create_queue();
1268 v3_init_queue(g_inpkt_q);
1271 void v3_vnet_init() {
1277 PrintDebug("VNET Init: Vnet input queue successful.\n");