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 #define MAX_ADDRESS 10
29 typedef enum {LOCAL,REMOTE} ctype;
35 struct vnet_device *local_device;
38 struct vnet_device *remote_device;
39 struct ethAddr addresses[MAX_ADDRESS];
47 char data[ETHERNET_PACKET_LEN];
53 #define NUM_HANDLERS 1
55 static struct vnet_device *available_devices[NUM_DEVICES];
57 struct handler *active_handlers[NUM_HANDLERS];
59 static int bind_address = 0;
60 static short bind_port = 22;
61 //static char *vnet_version = "0.9";
62 static int vnet_server = 0;
63 static bool use_tcp = false;
67 static void print_packet(char *pkt, int size)
71 PrintDebug("Vnet: packet: size: %d\n", size);
72 for (i = 0; i < size; i ++)
73 PrintDebug("%x ", pkt[i]);
79 static struct vnet_device *get_device(RawEthernetPacket *pt)
81 return available_devices[0];
85 static struct handler * get_handler()
88 return active_handlers[0];
91 static inline bool add_handler(struct handler *hd)
95 while (active_handlers[num] != NULL) num ++;
97 if (num >= NUM_HANDLERS)
100 active_handlers[num] = hd;
105 static inline bool add_device(struct vnet_device *dev)
109 while (available_devices[num] != NULL) num ++;
111 if (num >= NUM_DEVICES)
114 available_devices[num] = dev;
120 int CreateAndSetupTcpSocket(const int bufsize, const bool nodelay, const bool nonblocking)
124 // create socket for connections
125 if ((mysocket = V3_Create_TCP_Socket()) < 0) {
132 int BindSocketwPort(const int mysocket, const int myport)
134 if (V3_Bind_Socket(mysocket, myport) < 0) {
141 int ListenSocket(const int mysocket, const int maxc)
143 return V3_Listen_Socket(mysocket, maxc);
146 int ConnectToHost(const int mysocket, const int hostip, const int port)
148 return V3_Connect_To_IP(mysocket, hostip, port);
152 void close(int mysocket)
154 V3_Close_Socket(mysocket);
158 #if 0 //May be needed later
159 int SendTo(int const mysocket,
160 const unsigned ip, const int port,
161 const char *buf, const int len, const bool sendall)
167 return V3_SendTo_IP(mysocket, ip, port, buf, len);
172 sent = V3_SendTo_IP(mysocket, ip, port, buf, len);
184 int ReceiveFrom(const int mysocket,
185 const unsigned ip, const int port,
186 char *buf, const int len,
193 return V3_RecvFrom_IP(mysocket, ip, port, buf, len);
198 received = V3_RecvFrom_IP(mysocket, ip, port, buf, len);
201 } else if (received == 0) {
213 int Send(int socketfd, const char *buf, const int len, const bool sendall)
219 return V3_Send(socketfd, buf, len);
224 sent = V3_Send(socketfd, &(buf[len-left]), left);
228 } else if (sent == 0) {
239 int GetLine(int fd, char *buf, int size)
246 rc = Receive(fd, &c, 1, true);
252 if (rc == 0 || c == '\n') {
259 if (received >= size)
267 int PutLine(int mysocket, char *buf, int size)
270 return (Send(mysocket, buf, size+1, true) - 1);
275 static int readall(const int fd, char *buf, const int len, const int oneshot, const int awaitblock)
283 rc = V3_Recv(fd, &(buf[len-left]), left);
299 static int writeall(const int fd, const char *buf, const int len, const int oneshot, const int awaitblock)
307 rc = V3_Send(fd, &(buf[len-left]), left);
322 static void RawEthernetPacketInit(RawEthernetPacket *pt, const char *data, const size_t size)
325 memcpy(pt->data, data, size);
328 static bool RawEthernetPacketSerialize(RawEthernetPacket *pt, const int fd)
330 if (writeall(fd,(char*)&(pt->size),sizeof(pt->size),0,1)!=sizeof(pt->size))
332 PrintError("Vnet: Serialization Exception\n");
335 if (writeall(fd,pt->data,pt->size,0,1)!=(int)(pt->size))
337 PrintError("Vnet: Serialization Exception\n");
344 static bool RawEthernetPacketUnserialize(RawEthernetPacket *pt, const int fd)
346 if (readall(fd,(char*)&(pt->size),sizeof(pt->size),0,1) != sizeof(pt->size))
348 PrintError("Vnet: Unserialization Exception\n");
351 if (readall(fd,pt->data,pt->size,0,1) != pt->size)
353 PrintError("Vnet: Unserialization Exception\n");
360 static bool RawEthernetPacketSendUdp(RawEthernetPacket *pt, int sock_fd, int ip, short port)
364 PrintDebug("Vnet: sending by UDP socket %d ip: %x, port: %d\n", sock_fd, ip, port);
366 if ((size = V3_SendTo_IP(sock_fd, ip, port, pt->data, pt->size)) != pt->size)
368 PrintError("Vnet: sending by UDP Exception, %x\n", size);
377 static void print_packet_addr(char *pkt)
381 PrintDebug("Vnet: print_packet_destination_addr: ");
382 for (i = 8; i < 14; i ++)
383 PrintDebug("%x ", pkt[i]);
386 PrintDebug("Vnet: print_packet_source_addr: ");
387 for (i = 14; i < 20; i ++)
388 PrintDebug("%x ", pkt[i]);
393 static void print_device_addr(char *ethaddr)
397 PrintDebug("Vnet: print_device_addr: ");
398 for (i = 0; i < 6; i ++)
399 PrintDebug("%x ", ethaddr[i]);
406 #if ROUTE //new routing accrding to VNET-VTL, no hash --TY
408 struct topology g_links[MAX_LINKS];
409 int g_num_links; //The current number of links
413 struct routing g_routes[MAX_ROUTES];
414 int g_num_routes; //The current number of routes
418 struct device_list g_devices[MAX_DEVICES];
423 #define in_range(c, lo, up) ((char)c >= lo && (char)c <= up)
424 #define islower(c) in_range(c, 'a', 'z')
426 //------------------hash function begin-------------------
428 #define HASH_KEY_SIZE 16
431 * 6-11: dest_eth_addr
440 struct hash_value // This is the hash value, defined as a dynamic array. Format: 0: num_matched_routes, 1...n: matches[] -- TY
442 int num_matched_routes;
446 static int insert_some (struct hashtable * htable, struct hash_key *key, struct hash_value *value) {
447 return hashtable_insert(htable, (addr_t)key, (addr_t)value);
451 DEFINE_HASHTABLE_INSERT(insert_some, struct hash_key *, struct hash_value *);
452 DEFINE_HASHTABLE_SEARCH(search_some, struct hash_key *, struct hash_value);
453 //DEFINE_HASHTABLE_REMOVE(remove_some, struct hash_key *, struct hash_value, 1);
454 //DEFINE_HASHTABLE_ITERATOR_SEARCH(search_itr_some, struct key);
456 struct hash_key *cache_key; //maybe these three variables should be defined as local variable. -- TY
457 struct hash_value *cache_entry;
458 struct hashtable *hash_route;
463 cache_key = (hash_key *)V3_Malloc(sizeof(hash_key));
464 addr_t key = *cache_key;
467 // This is the hash algorithm used for UNIX ELF object files
468 inline size_t hash_from_key(const char * arg)
474 for(i = 0; i < HASH_KEY_SIZE; i++) {
475 hash = (hash << 4) + *(arg + i) + i;
476 if ((temp = (hash & 0xF0000000))) {
477 hash ^= (temp >> 24);
481 PrintDebug("Hash Value: %lu\n", (unsigned long)hash);
485 inline bool hash_key_equal(const char * left, const char * right)
488 for(i = 0; i < HASH_KEY_SIZE; i++) {
489 if (left[i] != right[i]) {
490 PrintDebug("HASHes not equal\n");
497 void make_hash_key(char * hash_key, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
500 for(j = 0; j < 6; j++) {
501 hash_key[j] = src_addr[j];
502 hash_key[j + 6] = dest_addr[j] + 1;
505 hash_key[12] = src_type;
507 *(int *)(hash_key + 12) = src_index;
510 int AddMatchedRoutesToCache(int num_matched_r, int * matches)
512 cache_entry->num_matched_routes = num_matched_r;
514 for(i = 0; i < num_matched_r; i++) {
515 cache_entry->matches = (int *)V3_Malloc(sizeof(int));
516 *(cache_entry->matches) = matches[i];
518 if (!insert_some(hash_route, cache_key, cache_entry)) return -1; /*oom*/
522 void clear_hash_cache()
524 hashtable_destroy(hash_route, 1, 1); //maybe there are some problems.
525 /* hashtable_destroy(hash_route, 1, 1);
528 V3_Free(cache_entry);
534 //int LookIntoCache(char compare_address[HASH_KEY_SIZE], int * do_we_analyze, int * matches) {
535 int LookIntoCache(char compare_address[HASH_KEY_SIZE], int * matches)
539 struct hash_value *found;
541 cache_key->key_from_addr = compare_address;
542 if (NULL != (found = search_some(hash_route, cache_key))) {
543 n_matches = found->num_matched_routes;
544 for (i = 0; i < found->num_matched_routes; i++) {
545 //*(matches+i) = *(found->matches+i);
546 matches[i] = found->matches[i];
550 PrintDebug("VNET: LookIntoCache, hash key not found\n");
556 //------------------hash function end-------------------
558 char vnet_toupper(char c)
565 char hexnybbletonybble(const char hexnybble) {
566 char x = vnet_toupper(hexnybble);
567 if ((x >= '0') && (x <= '9')) {
570 return 10 + (x - 'A');
574 void hexbytetobyte(const char hexbyte[2], char *byte) {
575 *byte = ((hexnybbletonybble(hexbyte[0]) << 4) +
576 (hexnybbletonybble(hexbyte[1]) & 0xf));
579 char nybbletohexnybble(const char nybble) {
580 return (nybble >= 10) ? (nybble - 10 + 'A') : (nybble + '0');
583 void bytetohexbyte(const char byte, char hexbyte[2]) {
584 hexbyte[0] = nybbletohexnybble((byte >> 4) & 0xf);
585 hexbyte[1] = nybbletohexnybble(byte & 0xf);
588 void string_to_mac(const char * str, char mac[6]) {
590 for(k = 0; k < 6; k++) {
591 hexbytetobyte(&(str[(2 * k) + k]), mac + k);
595 void mac_to_string(char address[6], char * buf) {
597 for (i = 0; i < 6; i++) {
598 bytetohexbyte(address[i], &(buf[3 * i]));
599 buf[(3 * i) + 2] = ':';
604 void ip_to_string(ulong_t addr, char * buf) {
605 struct in_addr addr_st;
608 addr_st.s_addr = htonl(addr);
609 tmp_str = inet_ntoa(addr_st);
611 memcpy(buf, tmp_str, strlen(tmp_str));
614 int find_link_by_fd(SOCK sock) {
617 FOREACH_LINK(i, g_links, g_first_link) {
618 if (g_links[i].link_sock == sock) {
626 int if_write_pkt(iface_t *iface, RawEthernetPacket *pkt)
628 iface->input((uchar_t *)pkt->data, pkt->size);
634 //int add_link_entry(unsigned long dest, int type, int link_class, int data_port, int authenticated, SOCK fd) {
635 int add_link_entry(unsigned long dest, int type, int data_port, SOCK fd) {
638 for(i = 0; i < MAX_LINKS; i++) {
639 if (g_links[i].use == 0) {
640 g_links[i].dest = dest;
641 // g_links[i].authenticated = authenticated;
642 g_links[i].type = type;
643 g_links[i].link_sock = fd;
644 g_links[i].remote_port = data_port;
646 // g_links[i].link_class = link_class;
648 if (g_first_link == -1)
651 g_links[i].prev = g_last_link;
652 g_links[i].next = -1;
654 if (g_last_link != -1) {
655 g_links[g_last_link].next = i;
670 int add_sock(struct sock_list * socks, int len, int * first_sock, int * last_sock, SOCK fd) {
673 for (i = 0; i < len; i++) {
674 if (socks[i].sock == -1) {
677 if (*first_sock == -1)
680 socks[i].prev = *last_sock;
683 if (*last_sock != -1)
684 socks[*last_sock].next = i;
697 int 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) {
700 for(i = 0; i < MAX_ROUTES; i++) {
701 if (g_routes[i].use == 0) {
703 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
704 memcpy(g_routes[i].src_mac, src_mac, 6);
706 memset(g_routes[i].src_mac, 0, 6);
709 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
710 memcpy(g_routes[i].dest_mac, dest_mac, 6);
712 memset(g_routes[i].dest_mac, 0, 6);
715 g_routes[i].src_mac_qual = src_mac_qual;
716 g_routes[i].dest_mac_qual = dest_mac_qual;
717 g_routes[i].dest = dest;
718 g_routes[i].type = type;
719 g_routes[i].src = src;
720 g_routes[i].src_type = src_type;
724 if (g_first_route == -1)
727 g_routes[i].prev = g_last_route;
728 g_routes[i].next = -1;
730 if (g_last_route != -1) {
731 g_routes[g_last_route].next = i;
745 * This returns an integer, if the interger is negative then AddLink failed, else it was
746 * successful and returns the socket descriptor
750 int AddLink(string input, SOCK * link_sock, string * ret_str) {
752 string hello, password, version, command;
753 string remote_address, type, remote_password;
758 struct hostent * remote_he;
759 struct in_addr dest_addr;
765 int ret = CTRL_DO_NOTHING;
767 struct sockaddr_in local_address;
773 istringstream is(input,istringstream::in);
774 is >> hello >> password >> version >> command >> remote_address >> ctrl_port >> data_port >> remote_password >> type;
778 if ((remote_address.empty()) || (remote_password.empty()) || (type.empty()) || (ctrl_port == 0) || (data_port == 0)) {
779 *ret_str = "Invalid Addlink command";
783 if (get_tunnel_type(type) == -1) {
784 *ret_str = "Invalid link type";
788 if ((remote_he = gethostbyname(remote_address.c_str())) == NULL) {
789 *ret_str = "Could not lookup address of " + remote_address;
793 dest_addr = *((struct in_addr *)remote_he->h_addr);
794 dest = dest_addr.s_addr;
796 link_index = find_link_entry(dest, get_tunnel_type(type));
798 if (link_index != -1) {
799 JRLDBG("Link already exists\n");
800 *ret_str = "Link already Exists";
805 if(type == TCP_STR) {
806 struct sockaddr_in serv_addr;
808 if ((data_sock = CreateAndSetupTcpSocket()) < 0) {
809 cerr << "the client descriptor could not be opened" << endl;
810 *ret_str = "Could not establish link";
814 serv_addr.sin_family = AF_INET;
815 serv_addr.sin_port = htons(data_port);
816 serv_addr.sin_addr = dest_addr;
817 memset(&(serv_addr.sin_zero), 0, 8);
819 JRLDBG("Connecting TCP Data link\n");
820 /*we establish a connection with the server */
821 if (connect(data_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
822 cerr << "the connection could not be established : " << endl;
823 *ret_str = "Could not establish link";
828 local_port = data_port;
830 *ret_str = "Successfully added a TCP link to " + remote_address;
831 ret = CTRL_ADD_TCP_SOCK;
833 } else if(type == UDP_STR) {
835 if (GetLocalSockAddress(g_udp_sockfd, (struct sockaddr *)&local_address) == -1) {
836 JRLDBG("Could not get local address\n");
839 local_port = ntohs(local_address.sin_port);
841 data_sock = g_udp_sockfd;
842 *ret_str = "Successfully added a virtual UDP link to " + remote_address;
846 *link_sock = data_sock;
848 if (create_ctrl_connection(&ctrl_con, remote_address, ctrl_port) == -1) {
849 JRLDBG("Could not establish control connection\n");
850 *ret_str = "Could not register new link";
851 if (type == TCP_STR) {
852 CLOSE_SSL(use_ssl_data, ssl);
858 JRLDBG("ctrl session established\n");
860 snprintf(buf, 128, "%s %d", "myself", local_port);
861 output = "REGISTERLINK ";
862 output = output + buf + " " + type;
864 if (send_ctrl_msg(ctrl_con, output, remote_password) == -1) {
865 JRLDBG("Could not send the control message\n");
866 *ret_str = "Could not register new link";
867 if (type == TCP_STR) {
868 CLOSE_SSL(use_ssl_data, ssl);
874 JRLDBG("Ctrl message sent\n");
875 if (close_ctrl_connection(ctrl_con, remote_password) == -1) {
876 JRLDBG("Could not close control connection\n");
879 link_index = add_link_entry(dest, get_tunnel_type(type), LINK, data_port, 1, data_sock);
881 if (link_index == -1) {
882 *ret_str = "Could not add link entry";
883 if (type == TCP_STR) {
884 CLOSE_SSL(use_ssl_data, ssl);
890 JRLDBG("Addlink Created link: FD=%d\n", g_links[link_index].link_sock);
897 int AddRoute(string input, string * ret_str) {
912 string hello, version, password, command;
913 string src_str, dest_str, src_mac_str, dest_mac_str, src_type, type;
917 istringstream is(input,istringstream::in);
918 is >> hello >> password >> version >> command >> src_mac_str >> dest_mac_str >> type >> dest_str >> src_type >> src_str;
921 if ((src_mac_str.empty()) || (dest_mac_str.empty()) ||
922 (dest_str.empty()) || (type.empty())) {
923 *ret_str = "Invalid AddRoute command";
928 if (get_route_type(type) == -1) {
929 *ret_str = "Invalid route type";
934 if (!(src_type.empty()) && (get_route_src_type(src_type) == -1)) {
935 *ret_str = "Invalid route source type";
940 //We will first do some parsing to obtain correct the qualifier for source and destination address
941 if (src_mac_str.find(":",0) == string::npos) {
942 src_mac_qual = get_qual_type(src_mac_str);
944 if (src_mac_qual == -1) {
945 *ret_str = "Invalid Qual Type";
949 memset(src_mac, 0, 6);
951 if(src_mac_str.substr(0,3) == NOT) {
952 src_mac_qual = NOT_TYPE;
953 src_mac_str = src_mac_str.substr(4);
955 src_mac_qual = EMPTY_TYPE;
958 src_mac_str = src_mac_str.substr(0,2) + src_mac_str.substr(3,2) + src_mac_str.substr(6,2) + src_mac_str.substr(9,2) + src_mac_str.substr(12,2) + src_mac_str.substr(15,2);
960 ConvertHexEthernetAddressToBinary(src_mac_str.c_str(), src_mac);
964 if (dest_mac_str.find (":",0) == string::npos) {
965 dest_mac_qual = get_qual_type(dest_mac_str);
967 if (dest_mac_qual == -1) {
968 ret_str->assign("Invalid Qual Type");
972 memset(dest_mac, 0, 6);
974 if(dest_mac_str.substr(0,3) == NOT) {
975 dest_mac_qual = NOT_TYPE;
976 dest_mac_str = dest_mac_str.substr(4);
978 dest_mac_qual = EMPTY_TYPE;
981 dest_mac_str = dest_mac_str.substr(0,2) + dest_mac_str.substr(3,2) + dest_mac_str.substr(6,2) + dest_mac_str.substr(9,2) + dest_mac_str.substr(12,2) + dest_mac_str.substr(15,2);
983 ConvertHexEthernetAddressToBinary(dest_mac_str.c_str(), dest_mac);
986 if (type == INTERFACE) {
989 dest_dev = get_device(dest_str);
991 if (dest_dev == -1) {
992 ret_str->assign("Could not find Destination Interface");
998 route_type = INTERFACE_TYPE;
999 } else if (type == EDGE) {
1000 hostent * remote_he;
1002 if ((remote_he = gethostbyname(dest_str.c_str())) == NULL) {
1003 ret_str->assign("Could not lookup address for " + dest_str);
1007 dest = find_link_entry((*((struct in_addr *)remote_he->h_addr)).s_addr, -1);
1009 JRLDBG("AddRoute: Invalid Destination: %s\n", inet_ntoa((*((struct in_addr *)remote_he->h_addr))));
1010 ret_str->assign("Addroute requested for invalid destination");
1014 route_type = EDGE_TYPE;
1018 if (src_type.empty() || (src_type == ANY_SRC)) {
1020 src_route_type = ANY_SRC_TYPE;
1021 } else if (src_type == INTERFACE) {
1023 if (src_str.empty()) {
1024 ret_str->assign("Missing Source INTERFACE");
1028 if (src_str == ANY_SRC) {
1032 src_dev = get_device(src_str);
1034 if (src_dev == -1) {
1035 ret_str->assign("Could not find Source Interface");
1041 src_route_type = INTERFACE_TYPE;
1043 } else if (src_type == EDGE) {
1045 if (src_str == ANY_SRC) {
1050 if (src_str.empty()) {
1051 ret_str->assign("Missing Source EDGE Address");
1055 if ((src_he = gethostbyname(src_str.c_str())) == NULL) {
1056 ret_str->assign("Could not lookup address for " + src_str);
1060 src = find_link_entry((*((struct in_addr *)src_he->h_addr)).s_addr, -1);
1063 JRLDBG("AddRoute: Invalid Destination: %s\n", inet_ntoa((*((struct in_addr *)src_he->h_addr))));
1064 ret_str->assign("Addroute requested for invalid destination");
1069 src_route_type = EDGE_TYPE;
1074 JRLDBG("Route Def: %d:%d:%d:%d:%d:%d %d:%d:%d:%d:%d:%d src_qual=%d, dest_qual=%d, dest=%d, type=%d src=%d, src_type=%d\n", src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5], dest_mac[0], dest_mac[1], dest_mac[2], dest_mac[3], dest_mac[4], dest_mac[5], src_mac_qual, dest_mac_qual, dest, route_type, src, src_route_type);
1076 route_index = find_route_entry(src_mac, dest_mac, src_mac_qual, dest_mac_qual, dest, route_type, src, src_route_type);
1078 if (route_index != -1) {
1079 JRLDBG("Route Already Exists, Index: %d\n", route_index);
1080 ret_str->assign("Route alredy Exists");
1084 route_index = add_route_entry(src_mac, dest_mac, src_mac_qual, dest_mac_qual, dest, get_route_type(type), src, src_route_type);
1086 if (route_index >= 0) {
1087 ret_str->assign("Successfully added the route for " +src_mac_str+ " and "+dest_mac_str);
1089 ret_str->assign("Could not add route for " + src_mac_str + " and " + dest_mac_str);
1098 int add_device_to_table(char *device_name, int type) {
1101 for (i = 0; i < MAX_DEVICES; i++) {
1102 if (g_devices[i].use == 0) {
1103 // g_devices[i].device_name = device_name;
1104 strcpy(g_devices[i].device_name, device_name);
1105 g_devices[i].type = type;
1106 g_devices[i].use = 1;
1108 if (g_first_device == -1)
1111 g_devices[i].prev = g_last_device;
1112 g_devices[i].next = -1;
1114 if (g_last_device != -1)
1115 g_devices[g_last_device].next = i;
1127 int find_link_entry(unsigned long dest, int type) {
1130 // PringDebug("Src: %lu, Dst: %lu\n", src, dest);
1132 FOREACH_LINK(i, g_links, g_first_link) {
1133 if ( (g_links[i].dest == dest) &&
1134 ((type == -1) || (g_links[i].type == type)) ) {
1142 int delete_link_entry(int index) {
1146 if (g_links[index].use == 0) {
1150 g_links[index].dest = 0;
1151 // g_links[index].authenticated = 0;
1152 g_links[index].type = 0;
1153 g_links[index].link_sock = -1;
1154 g_links[index].use = 0;
1156 prev_i = g_links[index].prev;
1157 next_i = g_links[index].next;
1160 g_links[prev_i].next = g_links[index].next;
1164 g_links[next_i].prev = g_links[index].prev;
1166 if (g_first_link == index)
1167 g_first_link = g_links[index].next;
1169 if (g_last_link == index)
1170 g_last_link = g_links[index].prev;
1172 g_links[index].next = -1;
1173 g_links[index].prev = -1;
1180 int delete_link_entry_by_addr(unsigned long dest, int type) {
1181 int index = find_link_entry(dest, type);
1187 return delete_link_entry(index);
1191 int delete_device(string device_name) {
1192 int index = get_device(device_name);
1198 delete_device(index);
1205 int delete_device(int index) {
1209 g_devices[index].device_name = "";
1210 g_devices[index].use = 0;
1213 prev_i = g_devices[index].prev;
1214 next_i = g_devices[index].next;
1217 g_devices[prev_i].next = g_devices[index].next;
1221 g_devices[next_i].prev = g_devices[index].prev;
1223 if (g_first_device == index)
1224 g_first_device = g_devices[index].next;
1226 if (g_last_device == index)
1227 g_last_device = g_devices[index].prev;
1229 g_devices[index].next = -1;
1230 g_devices[index].prev = -1;
1237 int find_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) {
1239 char temp_src_mac[6];
1240 char temp_dest_mac[6];
1242 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
1243 memcpy(temp_src_mac, src_mac, 6);
1245 memset(temp_src_mac, 0, 6);
1248 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
1249 memcpy(temp_dest_mac, dest_mac, 6);
1251 memset(temp_dest_mac, 0, 6);
1254 FOREACH_LINK(i, g_routes, g_first_route) {
1255 if ( (memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) &&
1256 (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
1257 (g_routes[i].src_mac_qual == src_mac_qual) &&
1258 (g_routes[i].dest_mac_qual == dest_mac_qual) &&
1260 ((type == g_routes[i].type) && (g_routes[i].dest == dest))) &&
1261 ( (src_type == -1) ||
1262 ((src_type == g_routes[i].src_type) && (g_routes[i].src == src))) ) {
1270 int delete_route_entry(int index) {
1275 memset(g_routes[index].src_mac, 0, 6);
1276 memset(g_routes[index].dest_mac, 0, 6);
1278 g_routes[index].dest = 0;
1279 g_routes[index].src = 0;
1280 g_routes[index].src_mac_qual = 0;
1281 g_routes[index].dest_mac_qual = 0;
1282 g_routes[index].type = -1;
1283 g_routes[index].src_type = -1;
1284 g_routes[index].use = 0;
1287 prev_i = g_routes[index].prev;
1288 next_i = g_routes[index].next;
1291 g_routes[prev_i].next = g_routes[index].next;
1295 g_routes[next_i].prev = g_routes[index].prev;
1297 if (g_first_route == index)
1298 g_first_route = g_routes[index].next;
1300 if (g_last_route == index)
1301 g_last_route = g_routes[index].prev;
1303 g_routes[index].next = -1;
1304 g_routes[index].prev = -1;
1311 int delete_route_entry_by_addr(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) {
1312 int index = find_route_entry(src_mac, dest_mac, src_mac_qual, dest_mac_qual, dest, type, src, src_type);
1318 delete_route_entry(index);
1325 int DeleteLink(string input, SOCK * link_sock, string * ret_str) {
1327 string hello,password, version, command;
1328 string remote_address, type, remote_password;
1331 struct hostent * remote_he;
1332 unsigned long dest_addr;
1335 int ret = CTRL_DO_NOTHING;
1338 struct sockaddr_in local_address;
1344 istringstream is(input,istringstream::in);
1345 is >> hello >> password >> version >> command >> remote_address >> ctrl_port >> remote_password >> type ;
1348 if ((remote_address.empty()) || (remote_password.empty()) ||
1349 (type.empty()) || (ctrl_port == 0)) {
1350 *ret_str = "Invalid Deletelink command";
1354 if (get_tunnel_type(type) == -1) {
1355 *ret_str = "Invalid link type";
1359 if ((remote_he = gethostbyname(remote_address.c_str())) == NULL) {
1360 *ret_str = "could not lookup address of " + remote_address;
1364 dest_addr = (*((struct in_addr *)remote_he->h_addr)).s_addr;
1366 JRLDBG("Deleting Dst: %lu\n", dest_addr);
1368 link_index = find_link_entry(dest_addr, get_tunnel_type(type));
1371 if (link_index == -1) {
1373 JRLDBG("Link Does Not Exist\n");
1374 *ret_str = "Link Does not exist";
1378 if (type == TCP_STR) {
1379 *link_sock = g_links[link_index].link_sock;
1380 *ret_str = "Successfully deleted a TCP link to " + remote_address;
1381 ret = CTRL_DELETE_TCP_SOCK;
1383 } else if (type == UDP_STR) {
1384 *ret_str = "Successfully deleted a virtual UDP link to " + remote_address;
1385 } else if (type == VTP_STR) {
1386 *ret_str = "Successfully deleted a VTP link to " + remote_address;
1387 // TODO: Send delete control message
1392 if (create_ctrl_connection(&ctrl_con, remote_address, ctrl_port) == -1) {
1393 JRLDBG("Could not establish control connection\n");
1394 *ret_str = "Could not deregister link";
1401 if (GetLocalSockAddress(ctrl_con.sock, (struct sockaddr *)&local_address) == -1) {
1402 JRLDBG("Could not get local address\n");
1403 *ret_str = "Could not deregister link";
1408 snprintf(buf, 128, "%s", inet_ntoa(local_address.sin_addr));
1410 output = "DEREGISTERLINK ";
1411 output = output + "myself" + " " + type;
1413 if (send_ctrl_msg(ctrl_con, output, remote_password) == -1) {
1414 // Could not register on the remote host,
1416 *ret_str = "Could not deregister link on remote host";
1420 if (close_ctrl_connection(ctrl_con, remote_password) == -1) {
1421 JRLDBG("Could not close control connection\n");
1424 if (close_link(link_index) == -1) {
1425 *ret_str = "Could not delete link";
1433 int DeleteRoute(string input, string * ret_str) {
1447 string hello, version, password, command;
1448 string src_str, dest_str, src_mac_str, dest_mac_str, type, src_type;
1451 istringstream is(input,istringstream::in);
1452 is >> hello >> password >> version >> command >> src_mac_str >> dest_mac_str >> type >> dest_str >> src_type >> src_str ;
1455 if ((src_mac_str.empty()) || (dest_mac_str.empty()) ||
1456 (dest_str.empty()) || (type.empty())) {
1457 ret_str->assign("Invalid Deleteroute command");
1462 if (get_route_type(type) == -1) {
1463 ret_str->assign("Invalid route type");
1467 if (!(src_type.empty()) && (get_route_src_type(src_type) == -1)) {
1468 ret_str->assign("Invalid route source type");
1472 //We will first do some parsing to obtain correct the qualifier for source and destination address
1473 if (src_mac_str.find(":",0) == string::npos) {
1474 src_mac_qual = get_qual_type(src_mac_str);
1476 if (src_mac_qual == -1) {
1477 ret_str->assign("Invalid qual type");
1481 memset(src_mac, 0, 6);
1483 if(src_mac_str.substr(0,3) == NOT) {
1484 src_mac_qual = NOT_TYPE;
1485 src_mac_str = src_mac_str.substr(4);
1487 src_mac_qual = EMPTY_TYPE;
1490 src_mac_str = src_mac_str.substr(0,2) + src_mac_str.substr(3,2) + src_mac_str.substr(6,2) + src_mac_str.substr(9,2) + src_mac_str.substr(12,2) + src_mac_str.substr(15,2);
1492 ConvertHexEthernetAddressToBinary(src_mac_str.c_str(), src_mac);
1496 if (dest_mac_str.find (":",0) == string::npos) {
1497 dest_mac_qual = get_qual_type(dest_mac_str);
1499 if (dest_mac_qual == -1) {
1500 ret_str->assign("Invalid Qual Type");
1504 memset(dest_mac, 0, 6);
1506 if(dest_mac_str.substr(0,3) == NOT) {
1507 dest_mac_qual = NOT_TYPE;
1508 dest_mac_str = dest_mac_str.substr(4);
1510 dest_mac_qual = EMPTY_TYPE;
1513 dest_mac_str = dest_mac_str.substr(0,2) + dest_mac_str.substr(3,2) + dest_mac_str.substr(6,2) + dest_mac_str.substr(9,2) + dest_mac_str.substr(12,2) + dest_mac_str.substr(15,2);
1515 ConvertHexEthernetAddressToBinary(dest_mac_str.c_str(), dest_mac);
1518 if (type == INTERFACE) {
1521 dest_dev = get_device(dest_str);
1523 if (dest_dev == -1) {
1524 ret_str->assign("Could not find Destination Interface");
1530 route_type = INTERFACE_TYPE;
1531 } else if (type == EDGE) {
1532 hostent * remote_he;
1534 if ((remote_he = gethostbyname(dest_str.c_str())) == NULL) {
1535 ret_str->assign("Could not lookup address for " + dest_str);
1539 dest = find_link_entry((*((struct in_addr *)remote_he->h_addr)).s_addr, -1);
1541 ret_str->assign("Route requested for invalid destination");
1545 route_type = EDGE_TYPE;
1549 if (src_type.empty() || (src_type == ANY_SRC)) {
1551 src_route_type = ANY_SRC_TYPE;
1552 } else if (src_type == INTERFACE) {
1554 if (src_str.empty()) {
1555 ret_str->assign("Missing Source INTERFACE");
1558 if (src_str == ANY_SRC) {
1562 src_dev = get_device(src_str);
1564 if (src_dev == -1) {
1565 ret_str->assign("Could not find Source Interface");
1571 src_route_type = INTERFACE_TYPE;
1573 } else if (src_type == EDGE) {
1575 if (src_str.empty()) {
1576 ret_str->assign("Missing Source EDGE Address");
1580 if (src_str == ANY_SRC) {
1585 if ((src_he = gethostbyname(src_str.c_str())) == NULL) {
1586 ret_str->assign("Could not lookup address for " + src_str);
1590 src = find_link_entry((*((struct in_addr *)src_he->h_addr)).s_addr, -1);
1593 JRLDBG("DeleteRoute: Invalid Destination: %s\n", inet_ntoa((*((struct in_addr *)src_he->h_addr))));
1594 ret_str->assign("DeleteToute requested for invalid destination");
1598 src_route_type = EDGE_TYPE;
1601 ret = delete_route_entry(src_mac, dest_mac, src_mac_qual, dest_mac_qual, dest, route_type, src, src_route_type);
1604 ret_str->assign("Successfully deleted the route for " +src_mac_str+ " and "+dest_mac_str);
1606 ret_str->assign("Could not delete the route");
1614 int delete_sock(struct sock_list * socks, int *first_sock, int *last_sock, SOCK fd) {
1620 FOREACH_SOCK(i, socks, (*first_sock)) {
1621 if (socks[i].sock == fd) {
1622 V3_Close_Socket(socks[i].sock);
1625 prev_i = socks[i].prev;
1626 next_i = socks[i].next;
1629 socks[prev_i].next = socks[i].next;
1632 socks[next_i].prev = socks[i].prev;
1634 if (*first_sock == i)
1635 *first_sock = socks[i].next;
1637 if (*last_sock == i)
1638 *last_sock = socks[i].prev;
1650 //During testing, route table, link table, etc. are writen in program, isteadjing of AddRoute(), AddLink()...
1651 void Create_topologies()
1654 unsigned long dest; // dest_addr = *((struct in_addr *)remote_he->h_addr); dest = dest_addr.s_addr;
1655 add_link_entry(dest, TCP_STR, int data_port, SOCK fd) //the parameter need modify
1657 //create device table
1659 add_device_to_table(eth0)
1661 //create route table
1662 //example(VnetClientJava):FORWARD endeavor.cs.northwestern.edu EDGE not-00:0C:29:0F:4E:AA 00:0C:29:0F:4E:AA virtuoso-22.cs.northwestern.edu ANY ANY
1663 //format(VnetClientJava): String routeStr = "FORWARD " + host.getHostname() + " " + dstType + " " + srcMacStr + " " + dstMacStr + " " + dst + " " + srcType + " " + src;
1664 char src_mac[6]; //={}; //00:0C:29:0F:4E:AA
1665 char dest_mac[6]; //00:0C:29:0F:4E:AA, empty type is only a mac adress.
1666 add_route_entry(src_mac, dest_mac, NOT_TYPE, EMPTY_TYPE, , EDGE, ANY, ANY); //decide "dest" after creade device table or link table.
1672 //int data_port, is udp/tcp port. SOCK fd comes from socket() function.. so this function is called after socket().
1673 void store_topologies(int data_port, SOCK fd)
1676 int src_mac_qual = ANY_TYPE;
1677 int dest_mac_qual = ANY_TYPE;
1678 int dest =0; //this is in_addr.s_addr
1679 int type = UDP_TYPE;
1681 int src_type= ANY_SRC_TYPE;
1684 for(i = 0; i < 1; i++) {
1685 if (g_links[i].use == 0) {
1686 g_links[i].dest = dest;
1687 // g_links[i].authenticated = authenticated;
1688 g_links[i].type = type;
1689 g_links[i].link_sock = fd;
1690 g_links[i].remote_port = data_port;
1692 // g_links[i].link_class = link_class;
1694 if (g_first_link == -1)
1697 g_links[i].prev = g_last_link;
1698 g_links[i].next = -1;
1700 if (g_last_link != -1) {
1701 g_links[g_last_link].next = i;
1714 for(i = 0; i < 1; i++) {
1715 if (g_routes[i].use == 0) {
1717 if ((src_mac_qual != ANY_TYPE) && (src_mac_qual != NONE_TYPE)) {
1718 // memcpy(g_routes[i].src_mac, src_mac, 6);
1720 memset(g_routes[i].src_mac, 0, 6);
1723 if ((dest_mac_qual != ANY_TYPE) && (dest_mac_qual != NONE_TYPE)) {
1724 // memcpy(g_routes[i].dest_mac, dest_mac, 6);
1726 memset(g_routes[i].dest_mac, 0, 6);
1729 g_routes[i].src_mac_qual = src_mac_qual;
1730 g_routes[i].dest_mac_qual = dest_mac_qual;
1731 g_routes[i].dest = dest;
1732 g_routes[i].type = type;
1733 g_routes[i].src = src;
1734 g_routes[i].src_type = src_type;
1736 g_routes[i].use = 1;
1738 if (g_first_route == -1)
1741 g_routes[i].prev = g_last_route;
1742 g_routes[i].next = -1;
1744 if (g_last_route != -1) {
1745 g_routes[g_last_route].next = i;
1756 //int MatchRoute(char * src_mac, char * dst_mac, int src_type, int src_index, int * do_we_analyze, int * matches) {
1757 int MatchRoute(char * src_mac, char * dst_mac, int src_type, int src_index, int * matches) {
1759 int values[g_num_routes];
1760 int matched_routes[g_num_routes];
1762 int num_matches = 0;
1766 int exact_match = 0;
1770 FOREACH_ROUTE(i, g_routes, g_first_route) {
1771 if ( (g_routes[i].src_type != ANY_SRC_TYPE) &&
1772 ( (g_routes[i].src_type != src_type) ||
1773 ( (g_routes[i].src != src_index) &&
1774 (g_routes[i].src != -1) ) ) ) {
1775 PrintDebug("Source route is on and does not match\n");
1779 if ( (g_routes[i].dest_mac_qual == ANY_TYPE) &&
1780 (g_routes[i].src_mac_qual == ANY_TYPE) ) {
1782 matched_routes[num_matches] = i;
1783 values[num_matches] = 3;
1788 if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
1789 if (g_routes[i].src_mac_qual != NOT_TYPE) {
1790 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
1792 matched_routes[num_matches] = i;
1793 values[num_matches] = 6;
1796 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
1797 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
1799 matched_routes[num_matches] = i;
1800 values[num_matches] = 8;
1810 if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
1811 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
1812 if (g_routes[i].src_mac_qual == ANY_TYPE) {
1814 matched_routes[num_matches] = i;
1815 values[num_matches] = 6;
1818 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
1819 if (g_routes[i].src_mac_qual != NOT_TYPE) {
1821 if (exact_match == 0) {
1822 matched_routes[num_matches] = i;
1823 values[num_matches] = 8;
1832 if ( (g_routes[i].dest_mac_qual == NOT_TYPE) &&
1833 (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0)) {
1834 if (g_routes[i].src_mac_qual == ANY_TYPE) {
1836 matched_routes[num_matches] = i;
1837 values[num_matches] = 5;
1840 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
1841 if (g_routes[i].src_mac_qual != NOT_TYPE) {
1843 matched_routes[num_matches] = i;
1844 values[num_matches] = 7;
1851 if ( (g_routes[i].src_mac_qual == NOT_TYPE) &&
1852 (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0) ) {
1853 if (g_routes[i].dest_mac_qual == ANY_TYPE) {
1855 matched_routes[num_matches] = i;
1856 values[num_matches] = 5;
1859 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
1860 if (g_routes[i].dest_mac_qual != NOT_TYPE) {
1862 matched_routes[num_matches] = i;
1863 values[num_matches] = 7;
1873 FOREACH_ROUTE(i, g_routes, g_first_route) {
1874 if ( (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
1875 (g_routes[i].dest_mac_qual == NONE_TYPE) &&
1876 ( (g_routes[i].src_type == ANY_SRC_TYPE) ||
1877 ( (g_routes[i].src_type == src_type) &&
1878 ( (g_routes[i].src == src_index) ||
1879 (g_routes[i].src == -1) )))) {
1881 matched_routes[num_matches] = i;
1882 values[num_matches] = 4;
1883 PrintDebug("We matched a default route (%d)\n", i);
1888 //If many rules have been matched, we choose one which has the highest value rating
1889 if (num_matches == 0) {
1893 for (i = 0; i < num_matches; i++) {
1894 if (values[i] > max) {
1898 matches[no] = matched_routes[i];
1901 } else if (values[i] == max) {
1902 matches[no] = matched_routes[i];
1911 bool HandleDataOverLink(RawEthernetPacket *pkt, int src_link_index)
1916 // int do_we_analyze;
1918 int matches[g_num_routes];
1920 int num_matched_routes = 0;
1922 struct HEADERS headers;
1923 // char hash_key[HASH_KEY_SIZE];
1925 // get the ethernet and ip headers from the packet
1926 memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
1928 for(j = 0;j < 6; j++) {
1929 src_mac[j] = headers.ethernetsrc[j];
1930 dst_mac[j] = headers.ethernetdest[j];
1938 mac_to_string(src_mac, src_str);
1939 mac_to_string(dst_mac, dest_str);
1941 PrintDebug("SRC(%s), DEST(%s)\n", src_str, dest_str);
1944 // make_hash_key(hash_key, src_mac, dst_mac, EDGE_TYPE, src_link_index);
1947 // num_matched_routes = MatchRoute(src_mac, dst_mac, EDGE_TYPE, src_link_index, &do_we_analyze, matches);
1948 num_matched_routes = MatchRoute(src_mac, dst_mac, EDGE_TYPE, src_link_index, matches);
1950 PrintDebug("Matches=%d\n", num_matched_routes);
1952 for (i = 0; i < num_matched_routes; i++) {
1953 int route_index = -1;
1954 int link_index = -1;
1957 route_index = matches[i];
1962 PrintDebug("Forward packet from link according to Route entry %d\n", route_index);
1963 // route_to_str(route_index, &route_str);
1964 // PrintDebug("Route Rule: %s\n", route_str.c_str());
1968 if (g_routes[route_index].type == EDGE_TYPE) {
1970 link_index = g_routes[route_index].dest;
1972 // PrintDebug("Destination Host: %s\n", ip_to_string(htonl(g_links[link_index].dest)));
1974 if(g_links[link_index].type == UDP_TYPE) {
1977 PrintDebug("Serializing UDP Packet from LINK, fd=%d\n", g_links[link_index].link_sock);
1979 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) {
1980 PrintError("Vnet: sending by UDP Exception, %x\n", size);
1984 } else if (g_links[link_index].type == TCP_TYPE) {
1985 // PrintDebug("Serializing TCP Packet from link to LINk %s\n", ip_to_string(htonl(g_links[link_index].dest)));
1986 RawEthernetPacketSerialize(pkt, g_links[link_index].link_sock);
1988 } else if (g_routes[route_index].type == INTERFACE_TYPE) {
1989 dev_index = g_routes[route_index].dest;
1991 PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device_name);
1993 if (if_write_pkt(g_devices[dev_index].vnet_device, pkt) == -1) {
1994 PrintDebug("Can't write output packet to link\n");
2008 static void hanlder_ProcessTcp(struct handler *hd)
2010 RawEthernetPacket *pt;
2012 pt = (RawEthernetPacket *)V3_Malloc(sizeof(RawEthernetPacket));
2014 PrintError("Vnet: Malloc fails\n");
2018 if (RawEthernetPacketUnserialize(pt, hd->fd) == false){
2019 PrintError("Vnet: Hanlder:ProcessTcp: receiving packet from TCP fails\n");
2023 PrintDebug("Vnet: In Hanlder.ProcessTcp: get packet\n");
2024 print_packet(pt->data, pt->size);
2026 /* int *link_index ;
2027 link_index = (int *)V3_Malloc(sizeof(int));
2028 if (link_index == NULL){
2029 PrintError("Vnet: Malloc fails\n");
2033 *link_index = find_link_by_fd(hd->fd);
2036 // int *link_index_ptr;
2037 link_index = find_link_by_fd(hd->fd);
2038 // link_index_ptr = &link_index;
2039 // v3_enqueue(src_link_index_q, (addr_t)link_index_ptr);
2040 pt->index = link_index;
2042 v3_enqueue(vnet_inpkt_q, (addr_t)pt);
2045 static void RunUdp(struct handler *hd)
2047 RawEthernetPacket *pt;
2050 pt = (RawEthernetPacket *)V3_Malloc(sizeof(RawEthernetPacket));
2052 PrintError("Vnet: Malloc fails\n");
2056 PrintDebug("Vnet: In Hanlder.RunUdp: socket: %d receive from ip %x, port %d\n", hd->fd, hd->remote_address, hd->remote_port);
2058 pt->size = V3_RecvFrom_IP(hd->fd, hd->remote_address, hd->remote_port, pt->data, ETHERNET_PACKET_LEN);
2061 PrintDebug("Vnet: Hanlder:ProcessUdp: receiving packet from UDP fails\n");
2066 PrintDebug("Vnet: In Hanlder.RunUdp: get packet\n");
2067 print_packet(pt->data, pt->size);
2070 v3_enqueue(vnet_inpkt_q, (addr_t)pt);
2076 static void RunTcp(struct handler *hd)
2078 struct v3_sock_set fds;
2083 V3_SOCK_SET(hd->fd, &fds);
2084 rc = V3_Select_Socket(&fds, NULL, NULL, NULL);
2086 PrintDebug("Vnet: In Hanlder.Run: Return from Select(): rc : %d\n", rc);
2089 PrintError("Vnet: In Hanlder.Run: Return from Select() Error: rc : %d\n", rc);
2091 } else if (rc == 0) {
2092 // huh? didn't ask for timeouts so just repeat
2095 hanlder_ProcessTcp(hd);
2100 static int vnet_handle(struct handler *hd)
2110 static bool handler_tx_packet(struct handler *hd, RawEthernetPacket *pt)
2114 PrintError("VNET Hanlder: sending a NULL packet\n");
2119 PrintDebug("VNET Hanlder: socket: %d local %x:[%d], remote %x:[%d]\n", hd->fd, hd->local_address, hd->local_port, hd->remote_address, hd->remote_port);
2122 return RawEthernetPacketSerialize(pt, hd->fd);
2124 return RawEthernetPacketSendUdp(pt, hd->fd, hd->remote_address, hd->remote_port);
2128 int V3_Send_pkt(uchar_t *buf, int length)
2130 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
2132 print_packet((char *)buf, length);
2134 return vnet_send_pkt((char *)buf, length);
2137 int V3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size))
2139 return vnet_register_pkt_event("NE2000", netif_input, NULL);
2142 int vnet_send_pkt(char *buf, int length)
2144 struct handler * hd;
2145 RawEthernetPacket pt;
2153 PrintDebug("Vnet: vnet_send_pkt: get handler: %p\n", hd);
2155 RawEthernetPacketInit(&pt, buf, length); //====here we copy sending data once
2157 if (!handler_tx_packet(hd, &pt))
2166 int vnet_register_pkt_event(char *dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void *data)
2168 struct vnet_device *dev;
2170 dev = (struct vnet_device *)V3_Malloc(sizeof(struct vnet_device));
2173 PrintError("VNET: Malloc fails\n");
2177 strncpy(dev->name, dev_name, (strlen(dev_name) < 50)?strlen(dev_name):50);
2179 dev->input = netif_input;
2182 if (!add_device(dev))
2190 // struct vnet_device *(*dev)[]; -- YT
2191 // struct vnet_device *dev;
2192 RawEthernetPacket *pt;
2196 //PrintDebug("VNET: In vnet_check\n");
2200 // while (((pt = (RawEthernetPacket *)v3_dequeue(vnet_inpkt_q)) != NULL)&&((link_index = (int *)v3_dequeue(src_link_index_q)) != NULL)){
2201 while ((pt = (RawEthernetPacket *)v3_dequeue(vnet_inpkt_q)) != NULL){
2203 /* dev = get_device(pt);
2204 //while (dev[device_num] != NULL) { //multi-get_device -- YT
2206 PrintDebug("VNET: In vnet_check: pkt length %d, no destination device, pkt discarded\n", (int)pt->size);
2211 dev->input((uchar_t *)pt->data, pt->size);
2213 PrintDebug("VNET: In vnet_check: pkt length %d, device: %s\n", (int)pt->size, dev->name);
2215 for (i = 0; i < (int)pt->size; i++)
2216 PrintDebug("%x ", pt->data[i]);
2219 link_index = pt->index;
2220 if(HandleDataOverLink(pt, link_index)) {
2221 PrintDebug("VNET: vnet_check: Receive and Send a packet!\n"); //--YT
2224 // V3_Free(link_index);
2225 V3_Free(pt); //be careful here
2231 static int handler_thread(void *arg)
2233 struct handler *hd = (struct handler *)arg;
2235 return vnet_handle(hd);
2238 static int vnet_setup_handler(int con_fd, ctype local_config, int remote_addr, int remote_port)
2242 h = (struct handler *)V3_Malloc(sizeof(struct handler));
2244 if (local_config != LOCAL && local_config != REMOTE){
2245 PrintError("VNET: bad local config\n");
2248 h->local_config = local_config;
2250 h->local_address = bind_address;
2251 h->local_port = bind_port;
2253 h->remote_address = remote_addr;
2254 h->remote_port = remote_port;
2257 if (h->local_config == LOCAL) {
2258 if ((h->fd = V3_Create_TCP_Socket() < 0)){
2259 PrintError("VNET: can't create socket\n");
2263 if (V3_Connect_To_IP(h->fd, h->remote_address, h->remote_port) < 0){
2264 V3_Close_Socket(h->fd);
2265 PrintError("VNET: can't connect to remote VNET server\n");
2269 PrintDebug("VNET: ConnectToHost done\n");
2270 } else if (local_config == REMOTE) {
2272 PrintError("VNET: Invalid socket discriptor\n");
2282 // Now run protocol to bootstrap the remote VNET server
2283 PrintDebug("VNET: socket connection done socket: %d\n", h->fd);
2285 V3_CREATE_THREAD(&handler_thread, (void *)h, "VNET_HANDLER");
2290 static int vnet_setup_tcp()
2295 int connection_socket;
2296 uint_t server_ip = (0 | 172 << 24 | 23 << 16 | 1 );
2297 uint_t server_port = 22;
2301 if ((accept_socket = V3_Create_TCP_Socket() < 0)){
2302 PrintError("VNET: Can't setup socket\n");
2306 if (bind_address == 0) {
2307 if (V3_Bind_Socket(accept_socket, bind_port) < 0){
2308 PrintError("VNET: Can't bind socket\n");
2312 if (0) {//BindSocketwAdx(accept_socket, bind_address, bind_port) < 0){
2313 PrintError("VNET: Can't bind socket\n");
2318 if (V3_Listen_Socket(accept_socket, 1) < 0) {
2319 PrintError("VNET: Can't listen socket\n");
2323 // create new connection for each handler remotely
2324 // this will stuck the initiation of vnet
2325 // TODO: put this in a separate kernel thread
2327 PrintDebug("VNET: In Accepting Socket\n");
2329 connection_socket = V3_Accept_Socket(accept_socket, &remote_ip, &remote_port);
2331 PrintDebug("VNET: return from Accepting Socket %d\n", connection_socket);
2333 if (connection_socket < 0){
2334 PrintError("VNET: Accept failed");
2338 //At this point our TCP connection is setup and running
2339 vnet_setup_handler(connection_socket, REMOTE, 0, 0);
2342 PrintDebug("VNET: In host status\n");
2344 //At this point our TCP connection is not setup
2345 vnet_setup_handler(-1, LOCAL, server_ip, server_port);
2352 static int vnet_setup_udp()
2354 uint_t client_ip = (0 | 172 << 24 | 23 << 16 | 2 );
2355 uint_t client_port = 22;
2357 uint_t server_ip = (0 | 172 << 24 | 23 << 16 | 1 );
2358 uint_t server_port = 22;
2361 hd = (struct handler *)V3_Malloc(sizeof(struct handler));
2363 if ((socket = V3_Create_UDP_Socket()) < 0){
2364 PrintError("VNET: Can't setup udp socket\n");
2369 if (vnet_server) {// vnet_proxy
2370 PrintDebug("VNET: In proxy status local ip: %x, %d\n", server_ip, server_port);
2372 if (V3_Bind_Socket(socket, server_port) < 0){
2373 PrintError("VNET: Can't bind socket\n");
2377 hd->local_address = server_ip;
2378 hd->local_port = server_port;
2379 hd->remote_address = client_ip;
2380 hd->remote_port = client_port;
2382 hd->remote_config = REMOTE;
2384 PrintDebug("VNET: In host status local ip: %x\n", client_ip);
2386 if (V3_Bind_Socket(socket, client_port) < 0){
2387 PrintError("VNET: Can't bind socket\n");
2391 hd->local_address = client_ip;
2392 hd->local_port = client_port;
2393 hd->remote_address = server_ip;
2394 hd->remote_port = server_port;
2396 hd->remote_config = LOCAL;
2399 PrintDebug("VNET: vnet_setup_udp new handler: socket: %d local %x:[%d], remote %x:[%d]\n", hd->fd, hd->local_address, hd->local_port, hd->remote_address, hd->remote_port);
2403 PrintDebug("VNET: add handler: %d\n", hd->fd);
2405 V3_CREATE_THREAD(&handler_thread, (void *)hd, "VNET_HANDLER");
2419 extern struct v3_socket_hooks * sock_hooks;
2420 PrintDebug("In VMM_SOCK: %p\n", sock_hooks);
2422 PrintDebug("VNET Init: VNET_SERVER: %d\n", vnet_server);
2424 vnet_inpkt_q = v3_create_queue();
2425 v3_init_queue(vnet_inpkt_q);
2427 //queue for src_link_index -- YT
2429 // src_link_index_q = v3_create_queue();
2430 // v3_init_queue(src_link_index_q);
2433 for (i = 0; i < NUM_DEVICES; i++)
2434 available_devices[i] = NULL;
2436 for (i = 0; i < NUM_HANDLERS; i++)
2437 active_handlers[i] = NULL;
2444 //not continue on the guest
2450 static void test_send(int sock, int time)
2452 char *buf = "\001\002\003\004\005\006\007\008\009\010\011\012\013\014\015\n";
2458 num = V3_Send_pkt((uchar_t *)buf, strlen(buf));
2460 PrintDebug("VNET: In test Send: sent size %d\n", num);
2462 for (j = 0; i < strlen(buf); j++)
2463 PrintDebug("%x ", buf[j]);