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 struct raw_ethernet_pkt {
30 int type; // vm or link type: INTERFACE|EDGE
31 char data[ETHERNET_PACKET_LEN];
34 char *vnet_version = "0.9";
35 static int vnet_server = 0;
36 static bool use_tcp = false;
38 static uint_t vnet_udp_port = 22;
42 #define MAX_DEVICES 16
45 struct topology g_links[MAX_LINKS];
46 int g_num_links; //The current number of links
50 struct routing g_routes[MAX_ROUTES];
51 int g_num_routes; //The current number of routes
55 struct device_list g_devices[MAX_DEVICES];
61 static struct gen_queue * g_inpkt_q;//packet receiving queue
64 static void print_packet(char *pkt, int size)
68 PrintDebug("Vnet: print_data_packet: size: %d\n", size);
69 for (i = 0; i < size; i ++)
70 PrintDebug("%x ", pkt[i]);
75 static void print_packet_addr(char *pkt)
79 PrintDebug("Vnet: print_packet_destination_addr: ");
80 for (i = 8; i < 14; i ++)
81 PrintDebug("%x ", pkt[i]);
84 PrintDebug("Vnet: print_packet_source_addr: ");
85 for (i = 14; i < 20; i ++)
86 PrintDebug("%x ", pkt[i]);
90 static void print_device_addr(char *ethaddr)
94 PrintDebug("Vnet: print_device_addr: ");
95 for (i = 0; i < 6; i ++)
96 PrintDebug("%x ", ethaddr[i]);
101 //network connection functions
104 static int CreateAndSetupTcpSocket(const int bufsize, const bool nodelay, const bool nonblocking)
108 // create socket for connections
109 if ((mysocket = V3_Create_TCP_Socket()) < 0) {
116 static int BindSocketwPort(const int mysocket, const int myport)
118 if (V3_Bind_Socket(mysocket, myport) < 0) {
125 static int ListenSocket(const int mysocket, const int maxc)
127 return V3_Listen_Socket(mysocket, maxc);
130 static int ConnectToHost(const int mysocket, const int hostip, const int port)
132 return V3_Connect_To_IP(mysocket, hostip, port);
136 static void close(int mysocket)
138 V3_Close_Socket(mysocket);
141 static int raw_ethernet_pkt_sendUdp(struct raw_ethernet_pkt *pt, int sock_fd, int ip, short port)
145 PrintDebug("Vnet: sending by UDP socket %d ip: %x, port: %d\n", sock_fd, ip, port);
147 if ((size = V3_SendTo_IP(sock_fd, ip, port, pt->data, pt->size)) != pt->size) {
148 PrintError("Vnet: sending by UDP Exception, %x\n", size);
157 static void raw_ethernet_packet_init(struct raw_ethernet_pkt *pt, const char *data, const size_t size)
160 memcpy(pt->data, data, size);
163 #define in_range(c, lo, up) ((char)c >= lo && (char)c <= up)
164 //#define islower(c) in_range(c, 'a', 'z')
167 #define HASH_KEY_SIZE 16
168 #define MIN_CACHE_SIZE 100
172 * 6-11: dest_eth_addr
176 typedef char * route_hashkey_t;
178 struct route_cache_entry // This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY
180 int num_matched_routes;
184 struct hashtable *g_route_cache; //Header of the route cache
186 static uint_t hash_from_key_fn(addr_t hashkey)
192 char *key =(char *)hashkey;
194 for (i = 0; i < HASH_KEY_SIZE; i++) {
195 hash = (hash << 4) + *(key + i) + i;
196 if ((temp = (hash & 0xF0000000))) {
197 hash ^= (temp >> 24);
201 PrintDebug("Hash Value: %lu\n", (unsigned long)hash);
206 static int hash_key_equal(addr_t left, addr_t right)
212 key2 = (char *)right;
214 for(i = 0; i < HASH_KEY_SIZE; i++) {
215 if (key1[i] != key2[i]) {
216 PrintDebug("HASHes not equal\n");
224 static int init_route_cache()
226 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
228 if (g_route_cache == NULL){
229 PrintError("Vnet: Route Cache Initiate Failurely\n");
236 static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
239 for(j = 0; j < 6; j++) {
240 hashkey[j] = src_addr[j];
241 hashkey[j + 6] = dest_addr[j] + 1;
243 hashkey[12] = src_type;
244 *(int *)(hashkey + 12) = src_index;
247 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int *matches)
249 struct route_cache_entry *new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
251 if (new_entry == NULL){
252 PrintError("Vnet: Malloc fails\n");
256 new_entry->num_matched_routes = num_matched_r;
259 new_entry->matches = (int *)V3_Malloc(num_matched_r*sizeof(int)); // TODO: Here need to consider where to release the memory when clear cache;
261 if (new_entry->matches == NULL){
262 PrintError("Vnet: Malloc fails\n");
266 for(i = 0; i < num_matched_r; i++) {
267 new_entry->matches[i] = matches[i];
270 //here, when v3_htable_insert return 0, it means insert fails
271 if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0){
272 PrintError("Vnet: Insert new route entry to cache failed\n");
273 V3_Free(new_entry->matches);
280 static int clear_hash_cache()
282 v3_free_htable(g_route_cache, 1, 1);
284 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
286 if (g_route_cache == NULL){
287 PrintError("Vnet: Route Cache Create Failurely\n");
294 static int look_into_cache(route_hashkey_t hashkey, int *matches)
298 struct route_cache_entry *found;
300 found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
303 n_matches = found->num_matched_routes;
304 for (i = 0; i < n_matches; i++)
305 matches[i] = found->matches[i];
312 static inline char vnet_toupper(char c)
319 static inline char hexnybbletonybble(const char hexnybble) {
320 char x = vnet_toupper(hexnybble);
321 if ((x >= '0') && (x <= '9')) {
324 return 10 + (x - 'A');
328 static inline void hexbytetobyte(const char hexbyte[2], char *byte) {
329 *byte = ((hexnybbletonybble(hexbyte[0]) << 4) +
330 (hexnybbletonybble(hexbyte[1]) & 0xf));
333 static inline char nybbletohexnybble(const char nybble) {
334 return (nybble >= 10) ? (nybble - 10 + 'A') : (nybble + '0');
337 static inline void bytetohexbyte(const char byte, char hexbyte[2]) {
338 hexbyte[0] = nybbletohexnybble((byte >> 4) & 0xf);
339 hexbyte[1] = nybbletohexnybble(byte & 0xf);
342 static inline void string_to_mac(const char * str, char mac[6]) {
344 for(k = 0; k < 6; k++) {
345 hexbytetobyte(&(str[(2 * k) + k]), mac + k);
349 static inline void mac_to_string(char address[6], char * buf) {
351 for (i = 0; i < 6; i++) {
352 bytetohexbyte(address[i], &(buf[3 * i]));
353 buf[(3 * i) + 2] = ':';
359 static void ip_to_string(ulong_t addr, char * buf) {
363 addr_st = v3_htonl(addr);
364 tmp_str = v3_inet_ntoa(addr_st);
366 memcpy(buf, tmp_str, strlen(tmp_str));
370 int find_link_by_fd(SOCK sock) {
373 FOREACH_LINK(i, g_links, g_first_link) {
374 if (g_links[i].link_sock == sock) {
382 int vnet_add_link_entry(unsigned long dest, int type, int data_port, SOCK fd) {
385 for(i = 0; i < MAX_LINKS; i++) {
386 if (g_links[i].use == 0) {
387 g_links[i].dest = dest;
388 g_links[i].type = type;
389 g_links[i].link_sock = fd;
390 g_links[i].remote_port = data_port;
392 if (g_first_link == -1)
395 g_links[i].prev = g_last_link;
396 g_links[i].next = -1;
398 if (g_last_link != -1) {
399 g_links[g_last_link].next = i;
414 // TODO: What is this used for?
415 int add_sock(struct sock_list *socks, int len, int *first_sock, int *last_sock, SOCK fd) {
418 for (i = 0; i < len; i++) {
419 if (socks[i].sock == -1) {
422 if (*first_sock == -1)
425 socks[i].prev = *last_sock;
428 if (*last_sock != -1)
429 socks[*last_sock].next = i;
440 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) {
443 for(i = 0; i < MAX_ROUTES; i++) {
444 if (g_routes[i].use == 0) {
446 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
447 memcpy(g_routes[i].src_mac, src_mac, 6);
449 memset(g_routes[i].src_mac, 0, 6);
452 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
453 memcpy(g_routes[i].dest_mac, dest_mac, 6);
455 memset(g_routes[i].dest_mac, 0, 6);
458 g_routes[i].src_mac_qual = src_mac_qual;
459 g_routes[i].dest_mac_qual = dest_mac_qual;
460 g_routes[i].dest = dest;
461 g_routes[i].type = type;
462 g_routes[i].src = src;
463 g_routes[i].src_type = src_type;
466 if (g_first_route == -1)
469 g_routes[i].prev = g_last_route;
470 g_routes[i].next = -1;
472 if (g_last_route != -1) {
473 g_routes[g_last_route].next = i;
490 static int find_link_entry(unsigned long dest, int type)
494 FOREACH_LINK(i, g_links, g_first_link) {
495 if ((g_links[i].dest == dest) &&
496 ((type == -1) || (g_links[i].type == type)) ) {
504 static int delete_link_entry(int index)
509 if (g_links[index].use == 0) {
513 g_links[index].dest = 0;
514 g_links[index].type = 0;
515 g_links[index].link_sock = -1;
516 g_links[index].use = 0;
518 prev_i = g_links[index].prev;
519 next_i = g_links[index].next;
522 g_links[prev_i].next = g_links[index].next;
525 g_links[next_i].prev = g_links[index].prev;
527 if (g_first_link == index)
528 g_first_link = g_links[index].next;
530 if (g_last_link == index)
531 g_last_link = g_links[index].prev;
533 g_links[index].next = -1;
534 g_links[index].prev = -1;
541 int vnet_delete_link_entry_by_addr(unsigned long dest, int type)
543 int index = find_link_entry(dest, type);
549 return delete_link_entry(index);
553 static int find_route_entry(char src_mac[6],
563 char temp_src_mac[6];
564 char temp_dest_mac[6];
566 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
567 memcpy(temp_src_mac, src_mac, 6);
569 memset(temp_src_mac, 0, 6);
572 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
573 memcpy(temp_dest_mac, dest_mac, 6);
575 memset(temp_dest_mac, 0, 6);
578 FOREACH_LINK(i, g_routes, g_first_route) {
579 if ((memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) &&
580 (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
581 (g_routes[i].src_mac_qual == src_mac_qual) &&
582 (g_routes[i].dest_mac_qual == dest_mac_qual) &&
584 ((type == g_routes[i].type) && (g_routes[i].dest == dest))) &&
586 ((src_type == g_routes[i].src_type) && (g_routes[i].src == src))) )
595 static int delete_route_entry(int index)
601 memset(g_routes[index].src_mac, 0, 6);
602 memset(g_routes[index].dest_mac, 0, 6);
604 g_routes[index].dest = 0;
605 g_routes[index].src = 0;
606 g_routes[index].src_mac_qual = 0;
607 g_routes[index].dest_mac_qual = 0;
608 g_routes[index].type = -1;
609 g_routes[index].src_type = -1;
610 g_routes[index].use = 0;
612 prev_i = g_routes[index].prev;
613 next_i = g_routes[index].next;
616 g_routes[prev_i].next = g_routes[index].next;
619 g_routes[next_i].prev = g_routes[index].prev;
621 if (g_first_route == index)
622 g_first_route = g_routes[index].next;
624 if (g_last_route == index)
625 g_last_route = g_routes[index].prev;
627 g_routes[index].next = -1;
628 g_routes[index].prev = -1;
637 int vnet_delete_route_entry_by_addr(char src_mac[6],
646 int index = find_route_entry(src_mac, dest_mac, src_mac_qual,
647 dest_mac_qual, dest, type, src, src_type);
653 delete_route_entry(index);
658 int delete_sock(struct sock_list * socks, int *first_sock, int *last_sock, SOCK fd) {
664 FOREACH_SOCK(i, socks, (*first_sock)) {
665 if (socks[i].sock == fd) {
666 V3_Close_Socket(socks[i].sock);
669 prev_i = socks[i].prev;
670 next_i = socks[i].next;
673 socks[prev_i].next = socks[i].next;
676 socks[next_i].prev = socks[i].prev;
678 if (*first_sock == i)
679 *first_sock = socks[i].next;
682 *last_sock = socks[i].prev;
693 //setup the topology of the testing network
694 static void store_topologies(SOCK fd)
697 int src_mac_qual = ANY_TYPE;
698 int dest_mac_qual = ANY_TYPE;
701 dest = (0 | 172 << 24 | 23 << 16 | 1 ); //this is in_addr.s_addr
702 PrintDebug("VNET: store_topologies. NO VNET_SERVER, dest = %x\n", dest);
704 dest = (0 | 172 << 24 | 23 << 16 | 2 ); //this is in_addr.s_addr
705 PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest);
710 int src_type= ANY_SRC_TYPE;
714 for(i = 0; i < MAX_LINKS; i++) {
715 if (g_links[i].use == 0) {
716 g_links[i].dest = (int)dest;
717 g_links[i].type = type;
718 g_links[i].link_sock = fd;
719 g_links[i].remote_port = data_port;
722 if (g_first_link == -1)
725 g_links[i].prev = g_last_link;
726 g_links[i].next = -1;
728 if (g_last_link != -1) {
729 g_links[g_last_link].next = i;
735 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);
745 for(i = 0; i < MAX_ROUTES; i++) {
746 if (g_routes[i].use == 0) {
747 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
748 // memcpy(g_routes[i].src_mac, src_mac, 6);
750 memset(g_routes[i].src_mac, 0, 6);
753 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
754 // memcpy(g_routes[i].dest_mac, dest_mac, 6);
756 memset(g_routes[i].dest_mac, 0, 6);
759 g_routes[i].src_mac_qual = src_mac_qual;
760 g_routes[i].dest_mac_qual = dest_mac_qual;
761 g_routes[i].dest = (int)dest;
762 g_routes[i].type = type;
763 g_routes[i].src = src;
764 g_routes[i].src_type = src_type;
768 if (g_first_route == -1)
771 g_routes[i].prev = g_last_route;
772 g_routes[i].next = -1;
774 if (g_last_route != -1) {
775 g_routes[g_last_route].next = i;
782 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);
788 static int match_route(char *src_mac, char *dst_mac, int src_type, int src_index, int *matches)
790 int values[MAX_ROUTES];
791 int matched_routes[MAX_ROUTES];
799 FOREACH_ROUTE(i, g_routes, g_first_route) {
800 if ((g_routes[i].src_type != ANY_SRC_TYPE) &&
801 ((g_routes[i].src_type != src_type) ||
802 ((g_routes[i].src != src_index) &&
803 (g_routes[i].src != -1))))
805 PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
809 if ( (g_routes[i].dest_mac_qual == ANY_TYPE) &&
810 (g_routes[i].src_mac_qual == ANY_TYPE) )
812 matched_routes[num_matches] = i;
813 values[num_matches] = 3;
817 if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0)
819 if (g_routes[i].src_mac_qual != NOT_TYPE) {
820 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
821 matched_routes[num_matches] = i;
822 values[num_matches] = 6;
825 }else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
826 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
827 matched_routes[num_matches] = i;
828 values[num_matches] = 8;
836 if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0)
838 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
839 if (g_routes[i].src_mac_qual == ANY_TYPE) {
840 matched_routes[num_matches] = i;
841 values[num_matches] = 6;
844 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
845 if (g_routes[i].src_mac_qual != NOT_TYPE) {
846 if (exact_match == 0) {
847 matched_routes[num_matches] = i;
848 values[num_matches] = 8;
856 if ((g_routes[i].dest_mac_qual == NOT_TYPE) &&
857 (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0))
859 if (g_routes[i].src_mac_qual == ANY_TYPE) {
860 matched_routes[num_matches] = i;
861 values[num_matches] = 5;
863 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
864 if (g_routes[i].src_mac_qual != NOT_TYPE) {
865 matched_routes[num_matches] = i;
866 values[num_matches] = 7;
872 if ((g_routes[i].src_mac_qual == NOT_TYPE) &&
873 (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0))
875 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
876 matched_routes[num_matches] = i;
877 values[num_matches] = 5;
879 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
880 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
881 matched_routes[num_matches] = i;
882 values[num_matches] = 7;
889 FOREACH_ROUTE(i, g_routes, g_first_route) {
890 if ((memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
891 (g_routes[i].dest_mac_qual == NONE_TYPE) &&
892 ((g_routes[i].src_type == ANY_SRC_TYPE) ||
893 ((g_routes[i].src_type == src_type) &&
894 ((g_routes[i].src == src_index) ||
895 (g_routes[i].src == -1))))) {
896 matched_routes[num_matches] = i;
897 values[num_matches] = 4;
898 PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
903 //If many rules have been matched, we choose one which has the highest value rating
904 if (num_matches == 0) {
908 for (i = 0; i < num_matches; i++) {
909 if (values[i] > max) {
912 matches[no] = matched_routes[i];
914 } else if (values[i] == max) {
915 matches[no] = matched_routes[i];
923 static inline int if_write_pkt(struct vnet_if_device *iface, struct raw_ethernet_pkt *pkt)
925 return iface->input((uchar_t *)pkt->data, pkt->size);
928 static int handle_one_pkt(struct raw_ethernet_pkt *pkt)
930 int src_link_index = 0; //the value of src_link_index of udp always is 0
935 int matches[g_num_routes];
936 int num_matched_routes = 0;
938 struct HEADERS headers;
940 // get the ethernet and ip headers from the packet
941 memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
944 for(j = 0;j < 6; j++) {
945 src_mac[j] = headers.ethernetsrc[j];
946 dst_mac[j] = headers.ethernetdest[j];
954 mac_to_string(src_mac, src_str);
955 mac_to_string(dst_mac, dest_str);
957 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
960 char hash_key[HASH_KEY_SIZE];
961 make_hash_key(hash_key, src_mac, dst_mac, EDGE_TYPE, src_link_index);
963 num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
965 if (num_matched_routes == -1) {//no match
966 num_matched_routes = match_route(src_mac, dst_mac, pkt->type, src_link_index, matches);
968 if (num_matched_routes > 0)
969 add_route_to_cache(hash_key, num_matched_routes,matches);
972 PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
974 for (i = 0; i < num_matched_routes; i++) {
975 int route_index = -1;
979 route_index = matches[i];
981 PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
983 if (g_routes[route_index].type == EDGE_TYPE) {
984 link_index = g_routes[route_index].dest;
986 if(g_links[link_index].type == UDP_TYPE) {
989 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) {
990 PrintError("Vnet: sending by UDP Exception, %x\n", size);
994 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);
996 }else if (g_links[link_index].type == TCP_TYPE) {
999 } else if (g_routes[route_index].type == INTERFACE_TYPE) {
1000 dev_index = g_routes[route_index].dest;
1002 PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device->name);
1004 if (if_write_pkt(g_devices[dev_index].device, pkt) == -1) {
1005 PrintDebug("Can't write output packet to link\n");
1009 PrintDebug("Vnet: Wrong Edge type\n");
1016 static int send_ethernet_pkt(char *buf, int length)
1018 struct raw_ethernet_pkt *pt;
1020 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1021 raw_ethernet_packet_init(pt, buf, length); //====here we copy sending data once
1023 PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pt->size);
1024 print_packet((char *)buf, length);
1026 v3_enqueue(g_inpkt_q, (addr_t)pt);
1031 int V3_Send_pkt(uchar_t *buf, int length)
1033 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
1035 return send_ethernet_pkt((char *)buf, length);
1038 static int add_device_to_table(struct vnet_if_device*device, int type) {
1041 for (i = 0; i < MAX_DEVICES; i++) {
1042 if (g_devices[i].use == 0) {
1043 g_devices[i].type = type;
1044 g_devices[i].use = 1;
1046 if (g_first_device == -1)
1049 g_devices[i].prev = g_last_device;
1050 g_devices[i].next = -1;
1052 if (g_last_device != -1)
1053 g_devices[g_last_device].next = i;
1065 static int search_device(char *device_name)
1069 for (i=0; i<MAX_DEVICES; i++)
1070 if (g_devices[i].use == 1){
1071 if (!strcmp(device_name, g_devices[i].device->name))
1078 static struct vnet_if_device* delete_device_from_table(int index)
1082 struct vnet_if_device *device = NULL;
1084 if (g_devices[index].use == 0)
1087 g_devices[index].use = 0;
1089 prev_i = g_devices[index].prev;
1090 next_i = g_devices[index].next;
1093 g_devices[prev_i].next = g_devices[index].next;
1096 g_devices[next_i].prev = g_devices[index].prev;
1098 if (g_first_device == index)
1099 g_first_device = g_devices[index].next;
1101 if (g_last_device == index)
1102 g_last_device = g_devices[index].prev;
1104 g_devices[index].next = -1;
1105 g_devices[index].prev = -1;
1107 device = g_devices[index].device;
1108 g_devices[index].device = NULL;
1116 int vnet_register_device(char *dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void *data)
1118 struct vnet_if_device *dev;
1120 dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
1123 PrintError("VNET: Malloc fails\n");
1127 strncpy(dev->name, dev_name, (strlen(dev_name) < 50)?strlen(dev_name):50);
1128 dev->input = netif_input;
1131 if (add_device_to_table(dev, GENERAL_NIC) == -1)
1137 int vnet_unregister_device(char *dev_name)
1141 i=search_device(dev_name);
1145 struct vnet_if_device *device = delete_device_from_table(i);
1154 int V3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size))
1156 return vnet_register_device("NE2000", netif_input, NULL);
1159 int vnet_pkt_process()
1161 struct raw_ethernet_pkt *pt;
1164 PrintDebug("VNET: In vnet_check\n");
1166 while ((pt = (struct raw_ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL){
1168 PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pt->size, (int)pt->type);
1170 for (i = 0; i < (int)pt->size; i++)
1171 PrintDebug("%x ", pt->data[i]);
1174 if(handle_one_pkt(pt)) {
1175 PrintDebug("VNET: vnet_check: handle one packet!\n");
1178 V3_Free(pt); //be careful here
1184 static int process_tcpdata()
1189 static int process_udpdata()
1192 struct raw_ethernet_pkt *pt;
1194 unsigned long dest = 0;
1195 unsigned short remote_port = 0;
1196 SOCK link_sock = g_udp_sockfd;
1197 int length = sizeof(struct raw_ethernet_pkt) - 2*sizeof(int); //minus the "size" and "type"
1199 //run in a loop to get packets from outside network, adding them to the incoming packet queue
1201 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1203 PrintError("Vnet: process_udp: Malloc fails\n");
1207 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);
1208 pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length);
1209 PrintDebug("Vnet: route_thread: socket: %d receive from ip %x, port [%d]\n", link_sock, (uint_t)dest, remote_port);
1212 PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n");
1217 PrintDebug("Vnet: process_udp: get packet\n");
1218 print_packet(pt->data, pt->size);
1220 v3_enqueue(g_inpkt_q, (addr_t)pt);
1226 static int indata_handler( )
1236 static int start_recv_data()
1241 SOCK udp_data_socket;
1243 if ((udp_data_socket = V3_Create_UDP_Socket()) < 0){
1244 PrintError("VNET: Can't setup udp socket\n");
1247 PrintDebug("Vnet: vnet_setup_udp: get socket: %d\n", udp_data_socket);
1248 g_udp_sockfd = udp_data_socket;
1250 store_topologies(udp_data_socket);
1252 if (V3_Bind_Socket(udp_data_socket, vnet_udp_port) < 0){
1253 PrintError("VNET: Can't bind socket\n");
1256 PrintDebug("VNET: vnet_setup_udp: bind socket successful\n");
1259 V3_CREATE_THREAD(&indata_handler, NULL, "VNET_DATA_HANDLER");
1263 static void init_link_table()
1266 for (i = 0; i < MAX_LINKS; i++) {
1268 g_links[i].next = -1;
1269 g_links[i].prev = -1;
1277 static void init_device_table()
1280 for (i = 0; i < MAX_DEVICES; i++) {
1281 g_devices[i].use = 0;
1282 g_devices[i].next = -1;
1283 g_devices[i].prev = -1;
1286 g_first_device = -1;
1291 static void init_route_table()
1294 for (i = 0; i < MAX_ROUTES; i++) {
1295 g_routes[i].use = 0;
1296 g_routes[i].next = -1;
1297 g_routes[i].prev = -1;
1305 static void init_tables()
1308 init_device_table();
1313 static void init_pkt_queue()
1315 PrintDebug("VNET Init package receiving queue\n");
1317 g_inpkt_q = v3_create_queue();
1318 v3_init_queue(g_inpkt_q);
1328 PrintDebug("VNET Init: Vnet input queue successful.\n");