Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


*** empty log message ***
[palacios.releases.git] / palacios / src / geekos / socket.c
index b18eaeb..64b5f00 100644 (file)
@@ -4,52 +4,42 @@
 #include <geekos/ne2k.h>
 #include <uip/uip.h>
 #include <uip/uip_arp.h>
-#include <geekos/int.h>
 #include <geekos/vmm_stubs.h>
-#include <palacios/vmm_queue.h>
 
+#define NULL (void *)0
 
-// for some reason, there are compile warnings without these
-extern void v3_init_queue(struct gen_queue * queue);
-extern void v3_enqueue(struct gen_queue * queue, addr_t entry);
-extern addr_t v3_dequeue(struct gen_queue * queue);
 
 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
 
 #define MAX_SOCKS 1024
+#define BUF_SIZE 1000
 
 struct socket sockets[MAX_SOCKS];
 
-struct gen_queue in_packets;
+extern void* memcpy(void *dst, const void* src, int n);
 
-struct sock_packet {
-       int size;
-       uchar_t *data;
-};
 
-int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) ;
+int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt);
 
 void init_network() {
    int i = 0;
    
-   v3_init_queue(&in_packets);
-
    for (i = 0; i < MAX_SOCKS; i++) {
       sockets[i].in_use = 0;
-      v3_init_queue(&(sockets[i].send_queue));
-      v3_init_queue(&(sockets[i].recv_queue));
+      sockets[i].send_buf = NULL;
+      sockets[i].recv_buf = NULL;
    }
 
-  //initiate uIP
+
+    //initiate uIP
     uip_init();
     uip_arp_init();
-
-  // set up interrupt handler
-  // set up device driver
-
+       
+    //setup device driver
     Init_Ne2k(&Packet_Received);
 
 
+
 }
 
 static int allocate_socket_fd() {
@@ -58,6 +48,14 @@ static int allocate_socket_fd() {
   for (i = 0; i < MAX_SOCKS; i++) {
     if (sockets[i].in_use == 0) {
       sockets[i].in_use = 1;
+      sockets[i].send_buf = create_ring_buffer(BUF_SIZE);
+      if (sockets[i].send_buf == NULL)
+               return -1;
+      sockets[i].recv_buf = create_ring_buffer(BUF_SIZE);
+      if (sockets[i].recv_buf == NULL){
+               free_ring_buffer(sockets[i].send_buf);
+               return -1;
+      }
       return i;
     }
   }
@@ -66,7 +64,14 @@ static int allocate_socket_fd() {
 }
 
 static int release_socket_fd(int sockfd){
-       sockets[sockfd].in_use = 0;
+       if (sockfd >= 0 && sockfd < MAX_SOCKS){
+               sockets[sockfd].in_use = 0;
+               free_ring_buffer(sockets[sockfd].send_buf);
+               free_ring_buffer(sockets[sockfd].recv_buf);
+               sockets[sockfd].send_buf = NULL;
+               sockets[sockfd].recv_buf = NULL;
+       }
+
        return 0;
 }
 
@@ -76,8 +81,6 @@ struct socket * get_socket_from_fd(int fd) {
 }
 
 
-
-
 int connect(const uchar_t ip_addr[4], ushort_t port) {
   int sockfd = -1;
   sockfd = allocate_socket_fd();
@@ -91,14 +94,32 @@ int connect(const uchar_t ip_addr[4], ushort_t port) {
   
   sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port));
 
+
   if (sockets[sockfd].con == NULL){
-       release_socket_fd(sockfd);
-       return -1;
+    release_socket_fd(sockfd);
+    return -1;
   }
 
   return sockfd;
 }
 
+int recv(int sockfd, void * buf, uint_t len){
+  uint_t recvlen;
+
+  struct socket *sock = get_socket_from_fd(sockfd);
+
+
+  // here we need some lock mechnism, just disable interrupt may not work properly because recv() will be run as a kernel thread 
+buf_read:
+  recvlen = rb_read(sock->recv_buf, buf, len);
+
+  if (recvlen == 0){
+       Wait(&(sock->recv_wait_queue));
+       goto buf_read;
+  }
+
+  return recvlen;
+}
 
 void timer_int_Handler(struct Interrupt_State * state){
        int i;
@@ -133,23 +154,49 @@ static void acked(int sockfd){
 }
 
 static void newdata(int sockfd){
+  uint_t len;
+  char *dataptr;
+  uint_t wrlen;
+  struct socket *sock;
+    
+  len = uip_datalen();
+  dataptr = (char *)uip_appdata;
+
+  if (len == 0)
+       return;
+
+  sock = get_socket_from_fd(sockfd);
+
+  wrlen = rb_write(sock->recv_buf, dataptr, len);
+
+  if (wrlen < len){ //write error, what should I do?
+       return;
+  }
+
+  Wake_Up(&(sock->recv_wait_queue));
+
+  return;    
 
 }
 
 // not finished yet
 static void
 senddata(int sockfd){
-    uchar_t *bufptr;
-    int len = 0;
-
-    bufptr = uip_appdata;
+  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 bytes_read = 0;
+  uchar_t * send_buf = uip_appdata;
   
-    if(len < uip_mss()) {
-      // memcpy(bufptr, data, len);
-    } else {
+  bytes_read = rb_peek(sock->send_buf, send_buf, len);
 
-    }
-  //uip_send(uip_appdata,len);
+  if (bytes_read == 0) {
+    // no packet for send
+    return;
+  }
+
+  uip_send(send_buf, len);
 }
 
 
@@ -158,9 +205,11 @@ senddata(int sockfd){
 static int  get_socket_from_port(ushort_t lport) {
   int i;
   
-  for (i = 0; i<MAX_SOCKS; i++){
-       if (sockets[i].con->lport == lport) 
-               return i;
+
+  for (i = 0; i < MAX_SOCKS; i++){
+    if (sockets[i].con->lport == lport) {
+      return i;
+    }
   }
   
   return -1;
@@ -168,110 +217,79 @@ static int  get_socket_from_port(ushort_t lport) {
 
 
 
+void socket_appcall(void) {
 
-void
-socket_appcall(void)
-{
   int sockfd; 
-
+  
   sockfd = get_socket_from_port(uip_conn->lport);
 
-  if (sockfd == -1) return;
-    
-  if(uip_connected()) {
-       connected(sockfd);
+  
+  if (sockfd == -1) {
+    return;
+  }
+
+  if (uip_connected()) {
+    connected(sockfd);
+
   }
   
-  if(uip_closed() ||uip_aborted() ||uip_timedout()) {
+  if (uip_closed() ||uip_aborted() ||uip_timedout()) {
      closed(sockfd);
      return;
   }
   
-  if(uip_acked()) {
+  if (uip_acked()) {
      acked(sockfd);
   }
   
-  if(uip_newdata()) {
+  if (uip_newdata()) {
      newdata(sockfd);
   }
   
-  if(uip_rexmit() ||
-     uip_newdata() ||
-     uip_acked() ||
-     uip_connected() ||
-     uip_poll()) {
-     senddata(sockfd);
+  if (uip_rexmit() ||
+      uip_newdata() ||
+      uip_acked() ||
+      uip_connected() ||
+      uip_poll()) {
+    senddata(sockfd);
   }
 }
 
 
 
-int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) 
-{
-         struct sock_packet next;
-         int i;
-         
-         next.size = info->size;
-         next.data = (uchar_t *)VMM_Malloc(next.size);
 
-         if (next.data == NULL) return 1;
-         
-         //uip_len = info->size;  
+int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) {
+  //int i;
+  
+  uip_len = info->size; 
 
-         for(i = 0; i < info->size; i++) {
-           *((next.data)+i) = *(pkt+i);
-         }
-         Free(pkt);
+  //  for (i = 0; i < info->size; i++) {
+  //  uip_buf[i] = *(pkt + i);
+  //}
 
-         Disable_Interrupts();
-         v3_enqueue(&in_packets, (addr_t)(&next));
-         Enable_Interrupts();
+  memcpy(uip_buf, pkt, uip_len);
 
-         //triger_receiver_interrupt();
-        
-         return 0;
-}
 
+  Free(pkt);
+
+  if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
+    uip_arp_arpin();
 
-void int_handler_packet_receive(struct Interrupt_State * state){
-       //device driver got a incoming packet and enqueue that packet to the receive queue
-               struct sock_packet *next_packet; 
-               addr_t pkt;
-               int i; 
-                       
-               while(1){ 
-                     //currently disable interrupt because no lock for the queue
-                      Disable_Interrupts();
-                       pkt = v3_dequeue(&in_packets);
-                       Enable_Interrupts();
+    if (uip_len > 0) {
+      NE2K_Transmit(uip_len);
+    }                                  
 
-                       if (pkt == 0) break;
+  } else {
 
-                       //there are new packets in the receiver queue
-                       next_packet = (struct sock_packet *)pkt;
-                       uip_len = next_packet->size;  
+    uip_arp_ipin();
+    uip_input();
 
-                       for(i = 0; i < uip_len; i++) {
-                           uip_buf[i] = *((next_packet->data)+i);
-                       }
+    if (uip_len > 0) {
+      uip_arp_out();
+      NE2K_Transmit(uip_len);
+    }
+  }
                          
-                       Free(next_packet->data);
-                       Free(next_packet);
-
-                       if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
-                           uip_arp_arpin();
-                           if (uip_len > 0){
-                               //ethernet_devicedriver_send();
-                               NE2K_Transmit(uip_len);
-                           }                                   
-                       } else {
-                           uip_arp_ipin();
-                           uip_input();
-                           if(uip_len > 0) {
-                               uip_arp_out();
-                                //ethernet_devicedriver_send();
-                                NE2K_Transmit(uip_len);
-                           }
-                       }                         
-               }       
+  return 0;
+
 }