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>
28 static const char any_type_str[] = "any";
29 static const char not_type_str[] = "not";
30 static const char none_type_str[] = "none";
31 static const char empty_type_str[] = "empty";
33 static const char link_edge_str[] = "EDGE";
34 static const char link_any_str[] = "ANY";
36 typedef enum {MAC_ANY, MAC_NOT, MAC_NONE, MAC_EMPTY} mac_type_t;
37 typedef enum {LINK_EDGE, LINK_ANY} link_type_t;
40 struct raw_ethernet_pkt {
42 char data[ETHERNET_PACKET_LEN];
45 static struct topology g_links[MAX_LINKS];
46 static int g_num_links; //The current number of links
47 static int g_first_link;
48 static int g_last_link;
50 static struct routing g_routes[MAX_ROUTES];
51 static int g_num_routes; //The current number of routes
52 static int g_first_route;
53 static int g_last_route;
55 static struct device_list g_devices[MAX_DEVICES];
56 static int g_num_devices;
57 static int g_first_device;
58 static int g_last_device;
61 static void print_packet(char *pkt, int size) {
62 PrintDebug("Vnet: print_data_packet: size: %d\n", size);
63 v3_hexdump(pkt, size, NULL, 0);
67 static void print_packet_addr(char *pkt) {
68 PrintDebug("Vnet: print_packet_destination_addr: ");
69 v3_hexdump(pkt + 8, 6, NULL, 0);
71 PrintDebug("Vnet: print_packet_source_addr: ");
72 v3_hexdump(pkt + 14, 6, NULL, 0);
75 static void print_device_addr(char *ethaddr) {
76 PrintDebug("Vnet: print_device_addr: ");
77 v3_hexdump(ethaddr, 6, NULL, 0);
82 //network connection functions
83 static inline void raw_ethernet_packet_init(struct raw_ethernet_pkt * pt, const char * data, const size_t size) {
85 memcpy(pt->data, data, size);
98 typedef char * route_hashkey_t;
100 // This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY
101 struct route_cache_entry {
102 int num_matched_routes;
106 #define HASH_KEY_LEN 16
107 #define MIN_CACHE_SIZE 100
109 //Header of the route cache
110 static struct hashtable * g_route_cache;
112 static uint_t hash_from_key_fn(addr_t hashkey) {
113 uint8_t * key = (uint8_t *)hashkey;
114 return v3_hash_buffer(key, HASH_KEY_LEN);
117 static int hash_key_equal(addr_t key1, addr_t key2) {
118 uint8_t * buf1 = (uint8_t *)key1;
119 uint8_t * buf2 = (uint8_t *)key2;
120 return (memcmp(buf1, buf2, HASH_KEY_LEN) == 0);
123 static int init_route_cache() {
124 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
126 if (g_route_cache == NULL){
127 PrintError("Vnet: Route Cache Initiate Failurely\n");
134 static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
137 for (j = 0; j < 6; j++) {
138 hashkey[j] = src_addr[j];
139 hashkey[j + 6] = dest_addr[j] + 1;
142 hashkey[12] = src_type;
144 *(int *)(hashkey + 12) = src_index;
146 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * matches) {
147 struct route_cache_entry * new_entry = NULL;
150 new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
151 if (new_entry == NULL){
152 PrintError("Vnet: Malloc fails\n");
156 new_entry->num_matched_routes = num_matched_r;
158 new_entry->matches = (int *)V3_Malloc(num_matched_r * sizeof(int));
160 if (new_entry->matches == NULL){
161 PrintError("Vnet: Malloc fails\n");
165 for (i = 0; i < num_matched_r; i++) {
166 new_entry->matches[i] = matches[i];
169 //here, when v3_htable_insert return 0, it means insert fails
170 if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0){
171 PrintError("Vnet: Insert new route entry to cache failed\n");
172 V3_Free(new_entry->matches);
179 static int clear_hash_cache() {
180 v3_free_htable(g_route_cache, 1, 1);
182 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
184 if (g_route_cache == NULL){
185 PrintError("Vnet: Route Cache Create Failurely\n");
192 static int look_into_cache(route_hashkey_t hashkey, int * matches) {
195 struct route_cache_entry * found = NULL;
197 found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
200 n_matches = found->num_matched_routes;
202 for (i = 0; i < n_matches; i++) {
203 matches[i] = found->matches[i];
213 static inline uint8_t hex_nybble_to_nybble(const uint8_t hexnybble) {
214 uint8_t x = toupper(hexnybble);
219 return 10 + (x - 'A');
223 static inline uint8_t hex_byte_to_byte(const uint8_t hexbyte[2]) {
224 return ((hex_nybble_to_nybble(hexbyte[0]) << 4) +
225 (hex_nybble_to_nybble(hexbyte[1]) & 0xf));
229 static inline void string_to_mac(const char * str, uint8_t mac[6]) {
232 for (k = 0; k < 6; k++) {
233 mac[k] = hex_byte_to_byte(&(str[(2 * k) + k]));
237 static inline void mac_to_string(uint8_t mac[6], char * buf) {
238 snprintf("%x:%x:%x:%x:%x:%x",
239 mac[0], mac[1], mac[2],
240 mac[3], mac[4], mac[5]);
244 static void ip_to_string(uint32_t addr, char * buf) {
248 addr_st = v3_htonl(addr);
249 tmp_str = v3_inet_ntoa(addr_st);
251 memcpy(buf, tmp_str, strlen(tmp_str));
255 int find_link_by_fd(SOCK sock) {
258 FOREACH_LINK(i, g_links, g_first_link) {
259 if (g_links[i].link_sock == sock) {
267 int vnet_add_link_entry(unsigned long dest, int type, int data_port, SOCK fd) {
270 for (i = 0; i < MAX_LINKS; i++) {
271 if (g_links[i].use == 0) {
272 g_links[i].dest = dest;
273 g_links[i].type = type;
274 g_links[i].link_sock = fd;
275 g_links[i].remote_port = data_port;
278 if (g_first_link == -1) {
282 g_links[i].prev = g_last_link;
283 g_links[i].next = -1;
285 if (g_last_link != -1) {
286 g_links[g_last_link].next = i;
301 int add_sock(struct sock_list *socks, int len, int *first_sock, int *last_sock, SOCK fd) {
304 for (i = 0; i < len; i++) {
305 if (socks[i].sock == -1) {
308 if (*first_sock == -1) {
312 socks[i].prev = *last_sock;
315 if (*last_sock != -1) {
316 socks[*last_sock].next = i;
328 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) {
331 for(i = 0; i < MAX_ROUTES; i++) {
332 if (g_routes[i].use == 0) {
334 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
335 memcpy(g_routes[i].src_mac, src_mac, 6);
337 memset(g_routes[i].src_mac, 0, 6);
340 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
341 memcpy(g_routes[i].dest_mac, dest_mac, 6);
343 memset(g_routes[i].dest_mac, 0, 6);
346 g_routes[i].src_mac_qual = src_mac_qual;
347 g_routes[i].dest_mac_qual = dest_mac_qual;
348 g_routes[i].dest = dest;
349 g_routes[i].type = type;
350 g_routes[i].src = src;
351 g_routes[i].src_type = src_type;
354 if (g_first_route == -1) {
358 g_routes[i].prev = g_last_route;
359 g_routes[i].next = -1;
361 if (g_last_route != -1) {
362 g_routes[g_last_route].next = i;
379 static int find_link_entry(unsigned long dest, int type) {
382 FOREACH_LINK(i, g_links, g_first_link) {
383 if ((g_links[i].dest == dest) &&
384 ((type == -1) || (g_links[i].type == type)) ) {
392 static int delete_link_entry(int index) {
396 if (g_links[index].use == 0) {
400 g_links[index].dest = 0;
401 g_links[index].type = 0;
402 g_links[index].link_sock = -1;
403 g_links[index].use = 0;
405 prev_i = g_links[index].prev;
406 next_i = g_links[index].next;
409 g_links[prev_i].next = g_links[index].next;
413 g_links[next_i].prev = g_links[index].prev;
416 if (g_first_link == index) {
417 g_first_link = g_links[index].next;
420 if (g_last_link == index) {
421 g_last_link = g_links[index].prev;
424 g_links[index].next = -1;
425 g_links[index].prev = -1;
432 int vnet_delete_link_entry_by_addr(unsigned long dest, int type) {
433 int index = find_link_entry(dest, type);
439 return delete_link_entry(index);
443 static int find_route_entry(char src_mac[6],
452 char temp_src_mac[6];
453 char temp_dest_mac[6];
455 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
456 memcpy(temp_src_mac, src_mac, 6);
458 memset(temp_src_mac, 0, 6);
461 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
462 memcpy(temp_dest_mac, dest_mac, 6);
464 memset(temp_dest_mac, 0, 6);
467 FOREACH_LINK(i, g_routes, g_first_route) {
468 if ( (memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) &&
469 (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
470 (g_routes[i].src_mac_qual == src_mac_qual) &&
471 (g_routes[i].dest_mac_qual == dest_mac_qual) &&
473 ( (type == g_routes[i].type) && (g_routes[i].dest == dest)) ) &&
474 ( (src_type == -1) ||
475 ( (src_type == g_routes[i].src_type) && (g_routes[i].src == src)) ) ) {
483 static int delete_route_entry(int index) {
487 memset(g_routes[index].src_mac, 0, 6);
488 memset(g_routes[index].dest_mac, 0, 6);
490 g_routes[index].dest = 0;
491 g_routes[index].src = 0;
492 g_routes[index].src_mac_qual = 0;
493 g_routes[index].dest_mac_qual = 0;
494 g_routes[index].type = -1;
495 g_routes[index].src_type = -1;
496 g_routes[index].use = 0;
498 prev_i = g_routes[index].prev;
499 next_i = g_routes[index].next;
502 g_routes[prev_i].next = g_routes[index].next;
506 g_routes[next_i].prev = g_routes[index].prev;
509 if (g_first_route == index) {
510 g_first_route = g_routes[index].next;
513 if (g_last_route == index) {
514 g_last_route = g_routes[index].prev;
517 g_routes[index].next = -1;
518 g_routes[index].prev = -1;
527 int vnet_delete_route_entry_by_addr(char src_mac[6],
535 int index = find_route_entry(src_mac, dest_mac, src_mac_qual,
536 dest_mac_qual, dest, type, src, src_type);
542 delete_route_entry(index);
547 int delete_sock(struct sock_list * socks, int * first_sock, int * last_sock, SOCK fd) {
553 FOREACH_SOCK(i, socks, (*first_sock)) {
554 if (socks[i].sock == fd) {
555 V3_Close_Socket(socks[i].sock);
558 prev_i = socks[i].prev;
559 next_i = socks[i].next;
562 socks[prev_i].next = socks[i].next;
566 socks[next_i].prev = socks[i].prev;
569 if (*first_sock == i) {
570 *first_sock = socks[i].next;
573 if (*last_sock == i) {
574 *last_sock = socks[i].prev;
586 //setup the topology of the testing network
587 static void store_topologies(SOCK fd) {
589 int src_mac_qual = ANY_TYPE;
590 int dest_mac_qual = ANY_TYPE;
593 dest = (0 | 172 << 24 | 23 << 16 | 1 );
594 PrintDebug("VNET: store_topologies. NOT VNET_SERVER, dest = %x\n", dest);
596 dest = (0 | 172 << 24 | 23 << 16 | 2 );
597 PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest);
602 int src_type= ANY_SRC_TYPE;
606 for (i = 0; i < MAX_LINKS; i++) {
607 if (g_links[i].use == 0) {
608 g_links[i].dest = (int)dest;
609 g_links[i].type = type;
610 g_links[i].link_sock = fd;
611 g_links[i].remote_port = data_port;
614 if (g_first_link == -1) {
618 g_links[i].prev = g_last_link;
619 g_links[i].next = -1;
621 if (g_last_link != -1) {
622 g_links[g_last_link].next = i;
628 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);
638 for (i = 0; i < MAX_ROUTES; i++) {
639 if (g_routes[i].use == 0) {
640 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
641 // memcpy(g_routes[i].src_mac, src_mac, 6);
643 memset(g_routes[i].src_mac, 0, 6);
646 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
647 // memcpy(g_routes[i].dest_mac, dest_mac, 6);
649 memset(g_routes[i].dest_mac, 0, 6);
652 g_routes[i].src_mac_qual = src_mac_qual;
653 g_routes[i].dest_mac_qual = dest_mac_qual;
654 g_routes[i].dest = (int)dest;
655 g_routes[i].type = type;
656 g_routes[i].src = src;
657 g_routes[i].src_type = src_type;
661 if (g_first_route == -1) {
665 g_routes[i].prev = g_last_route;
666 g_routes[i].next = -1;
668 if (g_last_route != -1) {
669 g_routes[g_last_route].next = i;
676 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);
682 static int match_route(uint8_t * src_mac, uint8_t * dst_mac, int src_type, int src_index, int * matches) {
683 int values[MAX_ROUTES];
684 int matched_routes[MAX_ROUTES];
692 FOREACH_ROUTE(i, g_routes, g_first_route) {
693 if ((g_routes[i].src_type != ANY_SRC_TYPE) &&
694 ((g_routes[i].src_type != src_type) ||
695 ((g_routes[i].src != src_index) &&
696 (g_routes[i].src != -1)))) {
697 PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
701 if ( (g_routes[i].dest_mac_qual == ANY_TYPE) &&
702 (g_routes[i].src_mac_qual == ANY_TYPE) ) {
703 matched_routes[num_matches] = i;
704 values[num_matches] = 3;
708 if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
709 if (g_routes[i].src_mac_qual != NOT_TYPE) {
710 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
711 matched_routes[num_matches] = i;
712 values[num_matches] = 6;
715 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
716 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
717 matched_routes[num_matches] = i;
718 values[num_matches] = 8;
726 if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
727 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
728 if (g_routes[i].src_mac_qual == ANY_TYPE) {
729 matched_routes[num_matches] = i;
730 values[num_matches] = 6;
733 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
734 if (g_routes[i].src_mac_qual != NOT_TYPE) {
735 if (exact_match == 0) {
736 matched_routes[num_matches] = i;
737 values[num_matches] = 8;
745 if ((g_routes[i].dest_mac_qual == NOT_TYPE) &&
746 (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0)) {
747 if (g_routes[i].src_mac_qual == ANY_TYPE) {
748 matched_routes[num_matches] = i;
749 values[num_matches] = 5;
751 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
752 if (g_routes[i].src_mac_qual != NOT_TYPE) {
753 matched_routes[num_matches] = i;
754 values[num_matches] = 7;
760 if ((g_routes[i].src_mac_qual == NOT_TYPE) &&
761 (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0)) {
762 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
763 matched_routes[num_matches] = i;
764 values[num_matches] = 5;
766 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
767 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
768 matched_routes[num_matches] = i;
769 values[num_matches] = 7;
777 FOREACH_ROUTE(i, g_routes, g_first_route) {
778 if ((memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
779 (g_routes[i].dest_mac_qual == NONE_TYPE) &&
780 ((g_routes[i].src_type == ANY_SRC_TYPE) ||
781 ((g_routes[i].src_type == src_type) &&
782 ((g_routes[i].src == src_index) ||
783 (g_routes[i].src == -1))))) {
784 matched_routes[num_matches] = i;
785 values[num_matches] = 4;
786 PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
791 //If many rules have been matched, we choose one which has the highest value rating
792 if (num_matches == 0) {
796 for (i = 0; i < num_matches; i++) {
797 if (values[i] > max) {
800 matches[no] = matched_routes[i];
802 } else if (values[i] == max) {
803 matches[no] = matched_routes[i];
811 static inline int if_write_pkt(struct vnet_if_device *iface, struct raw_ethernet_pkt * pkt) {
812 return iface->input((uchar_t *)pkt->data, pkt->size);
815 static int handle_one_pkt(struct raw_ethernet_pkt * pkt) {
816 int src_link_index = 0; //the value of src_link_index of udp always is 0
821 int matches[g_num_routes];
822 int num_matched_routes = 0;
824 struct HEADERS headers;
826 // get the ethernet and ip headers from the packet
827 memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
830 for (j = 0;j < 6; j++) {
831 src_mac[j] = headers.ethernetsrc[j];
832 dst_mac[j] = headers.ethernetdest[j];
840 mac_to_string(src_mac, src_str);
841 mac_to_string(dst_mac, dest_str);
843 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
846 char hash_key[HASH_KEY_SIZE];
847 make_hash_key(hash_key, src_mac, dst_mac, EDGE_TYPE, src_link_index);
849 num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
851 if (num_matched_routes == -1) { //no match
852 num_matched_routes = match_route(src_mac, dst_mac, pkt->type, src_link_index, matches);
854 if (num_matched_routes > 0) {
855 add_route_to_cache(hash_key, num_matched_routes,matches);
859 PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
861 for (i = 0; i < num_matched_routes; i++) {
862 int route_index = -1;
866 route_index = matches[i];
868 PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
870 if (g_routes[route_index].type == EDGE_TYPE) {
871 link_index = g_routes[route_index].dest;
873 if(g_links[link_index].type == UDP_TYPE) {
876 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) {
877 PrintError("Vnet: sending by UDP Exception, %x\n", size);
881 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);
883 } else if (g_links[link_index].type == TCP_TYPE) {
886 } else if (g_routes[route_index].type == INTERFACE_TYPE) {
887 dev_index = g_routes[route_index].dest;
889 PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device->name);
891 if (if_write_pkt(g_devices[dev_index].device, pkt) == -1) {
892 PrintDebug("Can't write output packet to link\n");
896 PrintDebug("Vnet: Wrong Edge type\n");
903 static int send_ethernet_pkt(char * buf, int length) {
904 struct raw_ethernet_pkt * pt;
906 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
907 raw_ethernet_packet_init(pt, buf, length); //====here we copy sending data once
909 PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pt->size);
910 print_packet((char *)buf, length);
912 v3_enqueue(g_inpkt_q, (addr_t)pt);
917 int V3_Send_pkt(uchar_t *buf, int length) {
918 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
920 return send_ethernet_pkt((char *)buf, length);
923 static int add_device_to_table(struct vnet_if_device*device, int type) {
926 for (i = 0; i < MAX_DEVICES; i++) {
927 if (g_devices[i].use == 0) {
928 g_devices[i].type = type;
929 g_devices[i].use = 1;
931 if (g_first_device == -1) {
935 g_devices[i].prev = g_last_device;
936 g_devices[i].next = -1;
938 if (g_last_device != -1) {
939 g_devices[g_last_device].next = i;
952 static int search_device(char *device_name) {
955 for (i = 0; i < MAX_DEVICES; i++) {
956 if (g_devices[i].use == 1) {
957 if (!strcmp(device_name, g_devices[i].device->name)) {
966 static struct vnet_if_device * delete_device_from_table(int index) {
969 struct vnet_if_device * device = NULL;
971 if (g_devices[index].use == 0) {
975 g_devices[index].use = 0;
977 prev_i = g_devices[index].prev;
978 next_i = g_devices[index].next;
981 g_devices[prev_i].next = g_devices[index].next;
985 g_devices[next_i].prev = g_devices[index].prev;
988 if (g_first_device == index) {
989 g_first_device = g_devices[index].next;
992 if (g_last_device == index) {
993 g_last_device = g_devices[index].prev;
996 g_devices[index].next = -1;
997 g_devices[index].prev = -1;
999 device = g_devices[index].device;
1000 g_devices[index].device = NULL;
1008 int vnet_register_device(char * dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void * data) {
1009 struct vnet_if_device * dev;
1011 dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
1014 PrintError("VNET: Malloc fails\n");
1018 strncpy(dev->name, dev_name, 50);
1019 dev->input = netif_input;
1022 if (add_device_to_table(dev, GENERAL_NIC) == -1) {
1029 int vnet_unregister_device(char * dev_name) {
1032 i = search_device(dev_name);
1038 struct vnet_if_device * device = delete_device_from_table(i);
1040 if (device == NULL) {
1049 int V3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size)) {
1050 return vnet_register_device("NE2000", netif_input, NULL);
1053 int vnet_pkt_process() {
1054 struct raw_ethernet_pkt * pt;
1057 PrintDebug("VNET: In vnet_check\n");
1059 while ((pt = (struct raw_ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) {
1060 PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pt->size, (int)pt->type);
1061 v3_hexdump(pt->data, pt->size, NULL, 0);
1063 if(handle_one_pkt(pt)) {
1064 PrintDebug("VNET: vnet_check: handle one packet!\n");
1067 V3_Free(pt); //be careful here
1075 static int process_udpdata() {
1076 struct raw_ethernet_pkt * pt;
1079 uint16_t remote_port = 0;
1080 SOCK link_sock = g_udp_sockfd;
1081 int length = sizeof(struct raw_ethernet_pkt) - (2 * sizeof(int)); //minus the "size" and "type"
1083 //run in a loop to get packets from outside network, adding them to the incoming packet queue
1085 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1088 PrintError("Vnet: process_udp: Malloc fails\n");
1092 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);
1093 pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length);
1094 PrintDebug("Vnet: route_thread: socket: [%d] receive from ip [%x], port [%d]\n", link_sock, (uint_t)dest, remote_port);
1096 if (pt->size <= 0) {
1097 PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n");
1102 PrintDebug("Vnet: process_udp: get packet\n");
1103 print_packet(pt->data, pt->size);
1112 static void init_link_table() {
1115 for (i = 0; i < MAX_LINKS; i++) {
1117 g_links[i].next = -1;
1118 g_links[i].prev = -1;
1126 static void init_device_table() {
1129 for (i = 0; i < MAX_DEVICES; i++) {
1130 g_devices[i].use = 0;
1131 g_devices[i].next = -1;
1132 g_devices[i].prev = -1;
1135 g_first_device = -1;
1140 static void init_route_table() {
1143 for (i = 0; i < MAX_ROUTES; i++) {
1144 g_routes[i].use = 0;
1145 g_routes[i].next = -1;
1146 g_routes[i].prev = -1;
1154 static void init_tables() {
1156 init_device_table();
1162 void v3_vnet_init() {
1164 PrintDebug("VNET Init: Vnet input queue successful.\n");
1169 store_topologies(udp_data_socket);