X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fgeekos%2Fsocket.c;h=1fc868cab51499a4e5a81d901781b2ca5777b852;hb=4f7c3b759e3889870c5b5e7d09b3ffcc168e5632;hp=64b5f00b2535e27c3e282a9c03694644fbc68bfe;hpb=b2d5697513a87d61e63a4317c89a0927b62b7ea1;p=palacios.git diff --git a/palacios/src/geekos/socket.c b/palacios/src/geekos/socket.c index 64b5f00..1fc868c 100644 --- a/palacios/src/geekos/socket.c +++ b/palacios/src/geekos/socket.c @@ -1,12 +1,15 @@ +/* Northwestern University */ +/* (c) 2008, Jack Lange */ +/* (c) 2008, Lei Xia */ + #include #include -#include #include #include #include #include - -#define NULL (void *)0 +#include +#include #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) @@ -16,21 +19,31 @@ struct socket sockets[MAX_SOCKS]; -extern void* memcpy(void *dst, const void* src, int n); +void socket_appcall(void); +#ifndef UIP_APPCALL +#define UIP_APPCALL socket_appcall +#endif /* UIP_APPCALL */ + + -int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt); +static int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt); +static void periodic_caller(int timer_id); -void init_network() { +void init_socket_layer() { int i = 0; + bool iflag; + for (i = 0; i < MAX_SOCKS; i++) { sockets[i].in_use = 0; sockets[i].send_buf = NULL; sockets[i].recv_buf = NULL; + sockets[i].state = CLOSED; } + //initiate uIP uip_init(); uip_arp_init(); @@ -38,10 +51,22 @@ void init_network() { //setup device driver Init_Ne2k(&Packet_Received); + iflag = Begin_Int_Atomic(); + Start_Timer(2, periodic_caller); + End_Int_Atomic(iflag); + +} + + +void set_ip_addr(uchar_t addr[4]) { + uip_ipaddr_t ipaddr; + uip_ipaddr(ipaddr, addr[0], addr[1], addr[2], addr[3]); /* Local IP address */ + uip_sethostaddr(ipaddr); } + static int allocate_socket_fd() { int i = 0; @@ -70,6 +95,7 @@ static int release_socket_fd(int sockfd){ free_ring_buffer(sockets[sockfd].recv_buf); sockets[sockfd].send_buf = NULL; sockets[sockfd].recv_buf = NULL; + sockets[sockfd].state = CLOSED; } return 0; @@ -81,10 +107,35 @@ struct socket * get_socket_from_fd(int fd) { } + +static void periodic_caller(int timer_id) { + int i; + //handle the periodic calls of uIP + + //PrintBoth("Timer CALLBACK handler\n"); + + for(i = 0; i < UIP_CONNS; ++i) { + uip_periodic(i); + if(uip_len > 0) { + //devicedriver_send(); + PrintBoth("Sending Packet\n"); + NE2K_Transmit(uip_len); + } + } + for(i = 0; i < UIP_UDP_CONNS; i++) { + uip_udp_periodic(i); + if(uip_len > 0) { + //devicedriver_send(); + NE2K_Transmit(uip_len); + } + } +} + + int connect(const uchar_t ip_addr[4], ushort_t port) { int sockfd = -1; sockfd = allocate_socket_fd(); - uip_ipaddr_t ipaddr; + static uip_ipaddr_t ipaddr; if (sockfd == -1) { return -1; @@ -92,7 +143,7 @@ int connect(const uchar_t ip_addr[4], ushort_t port) { uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]); - sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port)); + sockets[sockfd].con = uip_connect(&ipaddr, htons(port)); if (sockets[sockfd].con == NULL){ @@ -100,6 +151,16 @@ int connect(const uchar_t ip_addr[4], ushort_t port) { return -1; } + + PrintBoth("Connection start\n"); + Wait(&(sockets[sockfd].recv_wait_queue)); + + + PrintBoth("Connected\n"); + + + + return sockfd; } @@ -121,28 +182,17 @@ buf_read: return recvlen; } -void timer_int_Handler(struct Interrupt_State * state){ - int i; - //handle the periodic calls of uIP - for(i = 0; i < UIP_CONNS; ++i) { - uip_periodic(i); - if(uip_len > 0) { - //devicedriver_send(); - NE2K_Transmit(uip_len); - } - } - for(i = 0; i < UIP_UDP_CONNS; i++) { - uip_udp_periodic(i); - if(uip_len > 0) { - //devicedriver_send(); - NE2K_Transmit(uip_len); - } - } -} + // a series of utilities to handle conncetion states -static void connected(int sockfd){ +static void connected(int sockfd) { + struct socket * sock = get_socket_from_fd(sockfd); + + PrintBoth("Connected Interrupt\n"); + sock->state = ESTABLISHED; + + Wake_Up(&(sock->recv_wait_queue)); } static void closed(int sockfd){ @@ -180,12 +230,19 @@ static void newdata(int sockfd){ } // not finished yet -static void -senddata(int sockfd){ +static void send_to_driver(int sockfd) { + + PrintBoth("Sending data to driver\n"); + +} + + + +int send(int sockfd, void * buf, uint_t len) { struct socket * sock = get_socket_from_fd(sockfd); - int mss = uip_mss(); - int pending_bytes = rb_data_len(sock->send_buf); - int len = (mss < pending_bytes) ? mss: pending_bytes; + //int mss = uip_mss(); + //int pending_bytes = rb_data_len(sock->send_buf); + //int len = (mss < pending_bytes) ? mss: pending_bytes; int bytes_read = 0; uchar_t * send_buf = uip_appdata; @@ -193,16 +250,18 @@ senddata(int sockfd){ if (bytes_read == 0) { // no packet for send - return; + return -1; } uip_send(send_buf, len); + + return len; } //get the socket id by the local tcp port -static int get_socket_from_port(ushort_t lport) { +static int get_socket_from_port(ushort_t lport) { int i; @@ -223,6 +282,8 @@ void socket_appcall(void) { sockfd = get_socket_from_port(uip_conn->lport); + PrintBoth("Appcall\n"); + if (sockfd == -1) { return; @@ -251,45 +312,53 @@ void socket_appcall(void) { uip_acked() || uip_connected() || uip_poll()) { - senddata(sockfd); + send_to_driver(sockfd); } } -int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) { +static int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) { //int i; - uip_len = info->size; // for (i = 0; i < info->size; i++) { // uip_buf[i] = *(pkt + i); //} + PrintBoth("Packet REceived\n"); + memcpy(uip_buf, pkt, uip_len); Free(pkt); if (BUF->type == htons(UIP_ETHTYPE_ARP)) { + PrintBoth("ARP PACKET\n"); + uip_arp_arpin(); + if (uip_len > 0) { + PrintBoth("Transmitting\n"); NE2K_Transmit(uip_len); - } + } + } else { - + PrintBoth("Data PACKET\n"); uip_arp_ipin(); uip_input(); + if (uip_len > 0) { + PrintBoth("Transmitting\n"); uip_arp_out(); NE2K_Transmit(uip_len); } + } - - return 0; + return 0; }