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];
64 static int g_num_links; //The current number of links
65 static int g_first_link;
66 static int g_last_link;
68 static struct routing g_routes[MAX_ROUTES];
69 static int g_num_routes; //The current number of routes
70 static int g_first_route;
71 static int g_last_route;
73 static struct device_list g_devices[MAX_DEVICES];
74 static int g_num_devices;
75 static int g_first_device;
76 static int g_last_device;
78 static SOCK g_udp_sockfd;
79 static struct gen_queue * g_inpkt_q;//packet receiving queue
82 static void print_packet(char *pkt, int size)
86 PrintDebug("Vnet: print_data_packet: size: %d\n", size);
87 for (i = 0; i < size; i ++)
88 PrintDebug("%x ", pkt[i]);
93 static void print_packet_addr(char *pkt)
97 PrintDebug("Vnet: print_packet_destination_addr: ");
98 for (i = 8; i < 14; i ++)
99 PrintDebug("%x ", pkt[i]);
102 PrintDebug("Vnet: print_packet_source_addr: ");
103 for (i = 14; i < 20; i ++)
104 PrintDebug("%x ", pkt[i]);
108 static void print_device_addr(char *ethaddr)
112 PrintDebug("Vnet: print_device_addr: ");
113 for (i = 0; i < 6; i ++)
114 PrintDebug("%x ", ethaddr[i]);
119 //network connection functions
122 static int CreateAndSetupTcpSocket(const int bufsize, const bool nodelay, const bool nonblocking)
126 // create socket for connections
127 if ((mysocket = V3_Create_TCP_Socket()) < 0) {
134 static int BindSocketwPort(const int mysocket, const int myport)
136 if (V3_Bind_Socket(mysocket, myport) < 0) {
143 static int ListenSocket(const int mysocket, const int maxc)
145 return V3_Listen_Socket(mysocket, maxc);
148 static int ConnectToHost(const int mysocket, const int hostip, const int port)
150 return V3_Connect_To_IP(mysocket, hostip, port);
154 static void close(int mysocket)
156 V3_Close_Socket(mysocket);
159 static int raw_ethernet_pkt_sendUdp(struct raw_ethernet_pkt *pt, int sock_fd, int ip, short port)
163 PrintDebug("Vnet: sending by UDP socket %d ip: %x, port: %d\n", sock_fd, ip, port);
165 if ((size = V3_SendTo_IP(sock_fd, ip, port, pt->data, pt->size)) != pt->size) {
166 PrintError("Vnet: sending by UDP Exception, %x\n", size);
175 static void raw_ethernet_packet_init(struct raw_ethernet_pkt *pt, const char *data, const size_t size)
178 memcpy(pt->data, data, size);
181 #define in_range(c, lo, up) ((char)c >= lo && (char)c <= up)
182 //#define islower(c) in_range(c, 'a', 'z')
185 #define HASH_KEY_SIZE 16
186 #define MIN_CACHE_SIZE 100
190 * 6-11: dest_eth_addr
194 typedef char * route_hashkey_t;
196 struct route_cache_entry // This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY
198 int num_matched_routes;
202 static struct hashtable *g_route_cache; //Header of the route cache
204 static uint_t hash_from_key_fn(addr_t hashkey)
210 char *key =(char *)hashkey;
212 for (i = 0; i < HASH_KEY_SIZE; i++) {
213 hash = (hash << 4) + *(key + i) + i;
214 if ((temp = (hash & 0xF0000000))) {
215 hash ^= (temp >> 24);
219 PrintDebug("Hash Value: %lu\n", (unsigned long)hash);
224 static int hash_key_equal(addr_t left, addr_t right)
230 key2 = (char *)right;
232 for(i = 0; i < HASH_KEY_SIZE; i++) {
233 if (key1[i] != key2[i]) {
234 PrintDebug("HASHes not equal\n");
242 static int init_route_cache()
244 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
246 if (g_route_cache == NULL){
247 PrintError("Vnet: Route Cache Initiate Failurely\n");
254 static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
257 for(j = 0; j < 6; j++) {
258 hashkey[j] = src_addr[j];
259 hashkey[j + 6] = dest_addr[j] + 1;
261 hashkey[12] = src_type;
262 *(int *)(hashkey + 12) = src_index;
265 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int *matches)
267 struct route_cache_entry *new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
269 if (new_entry == NULL){
270 PrintError("Vnet: Malloc fails\n");
274 new_entry->num_matched_routes = num_matched_r;
277 new_entry->matches = (int *)V3_Malloc(num_matched_r*sizeof(int));
279 if (new_entry->matches == NULL){
280 PrintError("Vnet: Malloc fails\n");
284 for(i = 0; i < num_matched_r; i++) {
285 new_entry->matches[i] = matches[i];
288 //here, when v3_htable_insert return 0, it means insert fails
289 if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0){
290 PrintError("Vnet: Insert new route entry to cache failed\n");
291 V3_Free(new_entry->matches);
298 static int clear_hash_cache()
300 v3_free_htable(g_route_cache, 1, 1);
302 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
304 if (g_route_cache == NULL){
305 PrintError("Vnet: Route Cache Create Failurely\n");
312 static int look_into_cache(route_hashkey_t hashkey, int *matches)
316 struct route_cache_entry *found;
318 found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
321 n_matches = found->num_matched_routes;
322 for (i = 0; i < n_matches; i++)
323 matches[i] = found->matches[i];
330 static inline char vnet_toupper(char c)
337 static inline char hexnybbletonybble(const char hexnybble) {
338 char x = vnet_toupper(hexnybble);
339 if ((x >= '0') && (x <= '9')) {
342 return 10 + (x - 'A');
346 static inline void hexbytetobyte(const char hexbyte[2], char *byte) {
347 *byte = ((hexnybbletonybble(hexbyte[0]) << 4) +
348 (hexnybbletonybble(hexbyte[1]) & 0xf));
351 static inline char nybbletohexnybble(const char nybble) {
352 return (nybble >= 10) ? (nybble - 10 + 'A') : (nybble + '0');
355 static inline void bytetohexbyte(const char byte, char hexbyte[2]) {
356 hexbyte[0] = nybbletohexnybble((byte >> 4) & 0xf);
357 hexbyte[1] = nybbletohexnybble(byte & 0xf);
360 static inline void string_to_mac(const char * str, char mac[6]) {
362 for(k = 0; k < 6; k++) {
363 hexbytetobyte(&(str[(2 * k) + k]), mac + k);
367 static inline void mac_to_string(char address[6], char * buf) {
369 for (i = 0; i < 6; i++) {
370 bytetohexbyte(address[i], &(buf[3 * i]));
371 buf[(3 * i) + 2] = ':';
377 static void ip_to_string(ulong_t addr, char * buf) {
381 addr_st = v3_htonl(addr);
382 tmp_str = v3_inet_ntoa(addr_st);
384 memcpy(buf, tmp_str, strlen(tmp_str));
388 int find_link_by_fd(SOCK sock) {
391 FOREACH_LINK(i, g_links, g_first_link) {
392 if (g_links[i].link_sock == sock) {
400 int vnet_add_link_entry(unsigned long dest, int type, int data_port, SOCK fd) {
403 for(i = 0; i < MAX_LINKS; i++) {
404 if (g_links[i].use == 0) {
405 g_links[i].dest = dest;
406 g_links[i].type = type;
407 g_links[i].link_sock = fd;
408 g_links[i].remote_port = data_port;
410 if (g_first_link == -1)
413 g_links[i].prev = g_last_link;
414 g_links[i].next = -1;
416 if (g_last_link != -1) {
417 g_links[g_last_link].next = i;
432 int add_sock(struct sock_list *socks, int len, int *first_sock, int *last_sock, SOCK fd) {
435 for (i = 0; i < len; i++) {
436 if (socks[i].sock == -1) {
439 if (*first_sock == -1)
442 socks[i].prev = *last_sock;
445 if (*last_sock != -1)
446 socks[*last_sock].next = i;
457 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) {
460 for(i = 0; i < MAX_ROUTES; i++) {
461 if (g_routes[i].use == 0) {
463 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
464 memcpy(g_routes[i].src_mac, src_mac, 6);
466 memset(g_routes[i].src_mac, 0, 6);
469 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
470 memcpy(g_routes[i].dest_mac, dest_mac, 6);
472 memset(g_routes[i].dest_mac, 0, 6);
475 g_routes[i].src_mac_qual = src_mac_qual;
476 g_routes[i].dest_mac_qual = dest_mac_qual;
477 g_routes[i].dest = dest;
478 g_routes[i].type = type;
479 g_routes[i].src = src;
480 g_routes[i].src_type = src_type;
483 if (g_first_route == -1)
486 g_routes[i].prev = g_last_route;
487 g_routes[i].next = -1;
489 if (g_last_route != -1) {
490 g_routes[g_last_route].next = i;
507 static int find_link_entry(unsigned long dest, int type)
511 FOREACH_LINK(i, g_links, g_first_link) {
512 if ((g_links[i].dest == dest) &&
513 ((type == -1) || (g_links[i].type == type)) ) {
521 static int delete_link_entry(int index)
526 if (g_links[index].use == 0) {
530 g_links[index].dest = 0;
531 g_links[index].type = 0;
532 g_links[index].link_sock = -1;
533 g_links[index].use = 0;
535 prev_i = g_links[index].prev;
536 next_i = g_links[index].next;
539 g_links[prev_i].next = g_links[index].next;
542 g_links[next_i].prev = g_links[index].prev;
544 if (g_first_link == index)
545 g_first_link = g_links[index].next;
547 if (g_last_link == index)
548 g_last_link = g_links[index].prev;
550 g_links[index].next = -1;
551 g_links[index].prev = -1;
558 int vnet_delete_link_entry_by_addr(unsigned long dest, int type)
560 int index = find_link_entry(dest, type);
566 return delete_link_entry(index);
570 static int find_route_entry(char src_mac[6],
580 char temp_src_mac[6];
581 char temp_dest_mac[6];
583 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
584 memcpy(temp_src_mac, src_mac, 6);
586 memset(temp_src_mac, 0, 6);
589 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
590 memcpy(temp_dest_mac, dest_mac, 6);
592 memset(temp_dest_mac, 0, 6);
595 FOREACH_LINK(i, g_routes, g_first_route) {
596 if ((memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) &&
597 (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
598 (g_routes[i].src_mac_qual == src_mac_qual) &&
599 (g_routes[i].dest_mac_qual == dest_mac_qual) &&
601 ((type == g_routes[i].type) && (g_routes[i].dest == dest))) &&
603 ((src_type == g_routes[i].src_type) && (g_routes[i].src == src))) )
612 static int delete_route_entry(int index)
618 memset(g_routes[index].src_mac, 0, 6);
619 memset(g_routes[index].dest_mac, 0, 6);
621 g_routes[index].dest = 0;
622 g_routes[index].src = 0;
623 g_routes[index].src_mac_qual = 0;
624 g_routes[index].dest_mac_qual = 0;
625 g_routes[index].type = -1;
626 g_routes[index].src_type = -1;
627 g_routes[index].use = 0;
629 prev_i = g_routes[index].prev;
630 next_i = g_routes[index].next;
633 g_routes[prev_i].next = g_routes[index].next;
636 g_routes[next_i].prev = g_routes[index].prev;
638 if (g_first_route == index)
639 g_first_route = g_routes[index].next;
641 if (g_last_route == index)
642 g_last_route = g_routes[index].prev;
644 g_routes[index].next = -1;
645 g_routes[index].prev = -1;
654 int vnet_delete_route_entry_by_addr(char src_mac[6],
663 int index = find_route_entry(src_mac, dest_mac, src_mac_qual,
664 dest_mac_qual, dest, type, src, src_type);
670 delete_route_entry(index);
675 int delete_sock(struct sock_list * socks, int *first_sock, int *last_sock, SOCK fd) {
681 FOREACH_SOCK(i, socks, (*first_sock)) {
682 if (socks[i].sock == fd) {
683 V3_Close_Socket(socks[i].sock);
686 prev_i = socks[i].prev;
687 next_i = socks[i].next;
690 socks[prev_i].next = socks[i].next;
693 socks[next_i].prev = socks[i].prev;
695 if (*first_sock == i)
696 *first_sock = socks[i].next;
699 *last_sock = socks[i].prev;
710 //setup the topology of the testing network
711 static void store_topologies(SOCK fd)
714 int src_mac_qual = ANY_TYPE;
715 int dest_mac_qual = ANY_TYPE;
718 dest = (0 | 172 << 24 | 23 << 16 | 1 );
719 PrintDebug("VNET: store_topologies. NOT VNET_SERVER, dest = %x\n", dest);
721 dest = (0 | 172 << 24 | 23 << 16 | 2 );
722 PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest);
727 int src_type= ANY_SRC_TYPE;
731 for(i = 0; i < MAX_LINKS; i++) {
732 if (g_links[i].use == 0) {
733 g_links[i].dest = (int)dest;
734 g_links[i].type = type;
735 g_links[i].link_sock = fd;
736 g_links[i].remote_port = data_port;
739 if (g_first_link == -1)
742 g_links[i].prev = g_last_link;
743 g_links[i].next = -1;
745 if (g_last_link != -1) {
746 g_links[g_last_link].next = i;
752 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);
762 for(i = 0; i < MAX_ROUTES; i++) {
763 if (g_routes[i].use == 0) {
764 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
765 // memcpy(g_routes[i].src_mac, src_mac, 6);
767 memset(g_routes[i].src_mac, 0, 6);
770 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
771 // memcpy(g_routes[i].dest_mac, dest_mac, 6);
773 memset(g_routes[i].dest_mac, 0, 6);
776 g_routes[i].src_mac_qual = src_mac_qual;
777 g_routes[i].dest_mac_qual = dest_mac_qual;
778 g_routes[i].dest = (int)dest;
779 g_routes[i].type = type;
780 g_routes[i].src = src;
781 g_routes[i].src_type = src_type;
785 if (g_first_route == -1)
788 g_routes[i].prev = g_last_route;
789 g_routes[i].next = -1;
791 if (g_last_route != -1) {
792 g_routes[g_last_route].next = i;
799 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);
805 static int match_route(char *src_mac, char *dst_mac, int src_type, int src_index, int *matches)
807 int values[MAX_ROUTES];
808 int matched_routes[MAX_ROUTES];
816 FOREACH_ROUTE(i, g_routes, g_first_route) {
817 if ((g_routes[i].src_type != ANY_SRC_TYPE) &&
818 ((g_routes[i].src_type != src_type) ||
819 ((g_routes[i].src != src_index) &&
820 (g_routes[i].src != -1))))
822 PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
826 if ( (g_routes[i].dest_mac_qual == ANY_TYPE) &&
827 (g_routes[i].src_mac_qual == ANY_TYPE) )
829 matched_routes[num_matches] = i;
830 values[num_matches] = 3;
834 if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0)
836 if (g_routes[i].src_mac_qual != NOT_TYPE) {
837 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
838 matched_routes[num_matches] = i;
839 values[num_matches] = 6;
842 }else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
843 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
844 matched_routes[num_matches] = i;
845 values[num_matches] = 8;
853 if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0)
855 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
856 if (g_routes[i].src_mac_qual == ANY_TYPE) {
857 matched_routes[num_matches] = i;
858 values[num_matches] = 6;
861 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
862 if (g_routes[i].src_mac_qual != NOT_TYPE) {
863 if (exact_match == 0) {
864 matched_routes[num_matches] = i;
865 values[num_matches] = 8;
873 if ((g_routes[i].dest_mac_qual == NOT_TYPE) &&
874 (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0))
876 if (g_routes[i].src_mac_qual == ANY_TYPE) {
877 matched_routes[num_matches] = i;
878 values[num_matches] = 5;
880 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
881 if (g_routes[i].src_mac_qual != NOT_TYPE) {
882 matched_routes[num_matches] = i;
883 values[num_matches] = 7;
889 if ((g_routes[i].src_mac_qual == NOT_TYPE) &&
890 (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0))
892 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
893 matched_routes[num_matches] = i;
894 values[num_matches] = 5;
896 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
897 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
898 matched_routes[num_matches] = i;
899 values[num_matches] = 7;
906 FOREACH_ROUTE(i, g_routes, g_first_route) {
907 if ((memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
908 (g_routes[i].dest_mac_qual == NONE_TYPE) &&
909 ((g_routes[i].src_type == ANY_SRC_TYPE) ||
910 ((g_routes[i].src_type == src_type) &&
911 ((g_routes[i].src == src_index) ||
912 (g_routes[i].src == -1))))) {
913 matched_routes[num_matches] = i;
914 values[num_matches] = 4;
915 PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
920 //If many rules have been matched, we choose one which has the highest value rating
921 if (num_matches == 0) {
925 for (i = 0; i < num_matches; i++) {
926 if (values[i] > max) {
929 matches[no] = matched_routes[i];
931 } else if (values[i] == max) {
932 matches[no] = matched_routes[i];
940 static inline int if_write_pkt(struct vnet_if_device *iface, struct raw_ethernet_pkt *pkt)
942 return iface->input((uchar_t *)pkt->data, pkt->size);
945 static int handle_one_pkt(struct raw_ethernet_pkt *pkt)
947 int src_link_index = 0; //the value of src_link_index of udp always is 0
952 int matches[g_num_routes];
953 int num_matched_routes = 0;
955 struct HEADERS headers;
957 // get the ethernet and ip headers from the packet
958 memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
961 for(j = 0;j < 6; j++) {
962 src_mac[j] = headers.ethernetsrc[j];
963 dst_mac[j] = headers.ethernetdest[j];
971 mac_to_string(src_mac, src_str);
972 mac_to_string(dst_mac, dest_str);
974 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
977 char hash_key[HASH_KEY_SIZE];
978 make_hash_key(hash_key, src_mac, dst_mac, EDGE_TYPE, src_link_index);
980 num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
982 if (num_matched_routes == -1) {//no match
983 num_matched_routes = match_route(src_mac, dst_mac, pkt->type, src_link_index, matches);
985 if (num_matched_routes > 0)
986 add_route_to_cache(hash_key, num_matched_routes,matches);
989 PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
991 for (i = 0; i < num_matched_routes; i++) {
992 int route_index = -1;
996 route_index = matches[i];
998 PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
1000 if (g_routes[route_index].type == EDGE_TYPE) {
1001 link_index = g_routes[route_index].dest;
1003 if(g_links[link_index].type == UDP_TYPE) {
1006 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) {
1007 PrintError("Vnet: sending by UDP Exception, %x\n", size);
1011 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);
1013 }else if (g_links[link_index].type == TCP_TYPE) {
1016 } else if (g_routes[route_index].type == INTERFACE_TYPE) {
1017 dev_index = g_routes[route_index].dest;
1019 PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device->name);
1021 if (if_write_pkt(g_devices[dev_index].device, pkt) == -1) {
1022 PrintDebug("Can't write output packet to link\n");
1026 PrintDebug("Vnet: Wrong Edge type\n");
1033 static int send_ethernet_pkt(char *buf, int length)
1035 struct raw_ethernet_pkt *pt;
1037 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1038 raw_ethernet_packet_init(pt, buf, length); //====here we copy sending data once
1040 PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pt->size);
1041 print_packet((char *)buf, length);
1043 v3_enqueue(g_inpkt_q, (addr_t)pt);
1048 int V3_Send_pkt(uchar_t *buf, int length)
1050 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
1052 return send_ethernet_pkt((char *)buf, length);
1055 static int add_device_to_table(struct vnet_if_device*device, int type) {
1058 for (i = 0; i < MAX_DEVICES; i++) {
1059 if (g_devices[i].use == 0) {
1060 g_devices[i].type = type;
1061 g_devices[i].use = 1;
1063 if (g_first_device == -1)
1066 g_devices[i].prev = g_last_device;
1067 g_devices[i].next = -1;
1069 if (g_last_device != -1)
1070 g_devices[g_last_device].next = i;
1082 static int search_device(char *device_name)
1086 for (i=0; i<MAX_DEVICES; i++)
1087 if (g_devices[i].use == 1){
1088 if (!strcmp(device_name, g_devices[i].device->name))
1095 static struct vnet_if_device* delete_device_from_table(int index)
1099 struct vnet_if_device *device = NULL;
1101 if (g_devices[index].use == 0)
1104 g_devices[index].use = 0;
1106 prev_i = g_devices[index].prev;
1107 next_i = g_devices[index].next;
1110 g_devices[prev_i].next = g_devices[index].next;
1113 g_devices[next_i].prev = g_devices[index].prev;
1115 if (g_first_device == index)
1116 g_first_device = g_devices[index].next;
1118 if (g_last_device == index)
1119 g_last_device = g_devices[index].prev;
1121 g_devices[index].next = -1;
1122 g_devices[index].prev = -1;
1124 device = g_devices[index].device;
1125 g_devices[index].device = NULL;
1133 int vnet_register_device(char *dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void *data)
1135 struct vnet_if_device *dev;
1137 dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
1140 PrintError("VNET: Malloc fails\n");
1144 strncpy(dev->name, dev_name, (strlen(dev_name) < 50)?strlen(dev_name):50);
1145 dev->input = netif_input;
1148 if (add_device_to_table(dev, GENERAL_NIC) == -1)
1154 int vnet_unregister_device(char *dev_name)
1158 i=search_device(dev_name);
1162 struct vnet_if_device *device = delete_device_from_table(i);
1171 int V3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size))
1173 return vnet_register_device("NE2000", netif_input, NULL);
1176 int vnet_pkt_process()
1178 struct raw_ethernet_pkt *pt;
1181 PrintDebug("VNET: In vnet_check\n");
1183 while ((pt = (struct raw_ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL){
1185 PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pt->size, (int)pt->type);
1187 for (i = 0; i < (int)pt->size; i++)
1188 PrintDebug("%x ", pt->data[i]);
1191 if(handle_one_pkt(pt)) {
1192 PrintDebug("VNET: vnet_check: handle one packet!\n");
1195 V3_Free(pt); //be careful here
1201 static int process_tcpdata()
1206 static int process_udpdata()
1209 struct raw_ethernet_pkt *pt;
1211 unsigned long dest = 0;
1212 unsigned short remote_port = 0;
1213 SOCK link_sock = g_udp_sockfd;
1214 int length = sizeof(struct raw_ethernet_pkt) - 2*sizeof(int); //minus the "size" and "type"
1216 //run in a loop to get packets from outside network, adding them to the incoming packet queue
1218 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1220 PrintError("Vnet: process_udp: Malloc fails\n");
1224 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);
1225 pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length);
1226 PrintDebug("Vnet: route_thread: socket: [%d] receive from ip [%x], port [%d]\n", link_sock, (uint_t)dest, remote_port);
1229 PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n");
1234 PrintDebug("Vnet: process_udp: get packet\n");
1235 print_packet(pt->data, pt->size);
1237 v3_enqueue(g_inpkt_q, (addr_t)pt);
1243 static int indata_handler( )
1253 static int start_recv_data()
1258 SOCK udp_data_socket;
1260 if ((udp_data_socket = V3_Create_UDP_Socket()) < 0){
1261 PrintError("VNET: Can't setup udp socket\n");
1264 PrintDebug("Vnet: vnet_setup_udp: get socket: %d\n", udp_data_socket);
1265 g_udp_sockfd = udp_data_socket;
1267 store_topologies(udp_data_socket);
1269 if (V3_Bind_Socket(udp_data_socket, vnet_udp_port) < 0){
1270 PrintError("VNET: Can't bind socket\n");
1273 PrintDebug("VNET: vnet_setup_udp: bind socket successful\n");
1276 V3_CREATE_THREAD(&indata_handler, NULL, "VNET_DATA_HANDLER");
1280 static void init_link_table()
1283 for (i = 0; i < MAX_LINKS; i++) {
1285 g_links[i].next = -1;
1286 g_links[i].prev = -1;
1294 static void init_device_table()
1297 for (i = 0; i < MAX_DEVICES; i++) {
1298 g_devices[i].use = 0;
1299 g_devices[i].next = -1;
1300 g_devices[i].prev = -1;
1303 g_first_device = -1;
1308 static void init_route_table()
1311 for (i = 0; i < MAX_ROUTES; i++) {
1312 g_routes[i].use = 0;
1313 g_routes[i].next = -1;
1314 g_routes[i].prev = -1;
1322 static void init_tables()
1325 init_device_table();
1330 static void init_pkt_queue()
1332 PrintDebug("VNET Init package receiving queue\n");
1334 g_inpkt_q = v3_create_queue();
1335 v3_init_queue(g_inpkt_q);
1345 PrintDebug("VNET Init: Vnet input queue successful.\n");