Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not use recv() but read() to check whether a socket returned by select() is valid...
[simgrid.git] / src / gras / Transport / transport_plugin_tcp.c
index 256820f..cfce9e2 100644 (file)
@@ -1,6 +1,6 @@
 /* $Id$ */
 
-/* buf trp (transport) - buffered transport using the TCP one            */
+/* buf trp (transport) - buffered transport using the TCP one               */
 
 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
 
 #include "xbt/misc.h"
 #include "xbt/sysdep.h"
 #include "xbt/ex.h"
-#include "transport_private.h"
+#include "gras/Transport/transport_private.h"
+
+/* FIXME maybe READV is sometime a good thing? */
+#undef HAVE_READV
+
+#ifdef HAVE_READV
+#include <sys/uio.h>
+#endif       
 
 #ifndef MIN
 #define MIN(a,b) ((a)<(b)?(a):(b))
 #endif
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_tcp,gras_trp,
       "TCP buffered transport");
 
 /***
@@ -54,33 +61,42 @@ struct gras_trp_bufdata_{
 /*****************************/
 /****[ SOCKET MANAGEMENT ]****/
 /*****************************/
+/* we exchange port number on client side on socket creation,
+   so we need to be able to talk right now. */
+static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, const char *data,
+                                    unsigned long int size);
+static int gras_trp_tcp_recv(gras_socket_t sock, char *data,
+                            unsigned long int size);
+
+
 static int _gras_tcp_proto_number(void);
 
-static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
+static XBT_INLINE void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
                                               gras_socket_t sock){
   
   struct sockaddr_in addr;
   struct hostent *he;
   struct in_addr *haddr;
-  int size = sock->bufSize * 1024; 
+  int size = sock->buf_size; 
+  uint32_t myport = htonl(((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport);
 
   sock->incoming = 1; /* TCP sockets are duplex'ed */
 
   sock->sd = socket (AF_INET, SOCK_STREAM, 0);
   
   if (sock->sd < 0) {
-    THROW1(system_error,0, "Failed to create socket: %s", sock_errstr);
+    THROW1(system_error,0, "Failed to create socket: %s", sock_errstr(sock_errno));
   }
 
   if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
       setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
-     WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
+     WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr(sock_errno));
   }
   
   he = gethostbyname (sock->peer_name);
   if (he == NULL) {
     THROW2(system_error,0, "Failed to lookup hostname %s: %s",
-          sock->peer_name, sock_errstr);
+          sock->peer_name, sock_errstr(sock_errno));
   }
   
   haddr = ((struct in_addr *) (he->h_addr_list)[0]);
@@ -94,8 +110,12 @@ static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
     tcp_close(sock->sd);
     THROW3(system_error,0,
           "Failed to connect socket to %s:%d (%s)",
-          sock->peer_name, sock->peer_port, sock_errstr);
+          sock->peer_name, sock->peer_port, sock_errstr(sock_errno));
   }
+
+  gras_trp_tcp_send(sock,(char*)&myport,sizeof(uint32_t));
+  DEBUG1("peerport sent to %d", sock->peer_port);
+
   VERB4("Connect to %s:%d (sd=%d, port %d here)",
        sock->peer_name, sock->peer_port, sock->sd, sock->port);
 }
@@ -105,9 +125,9 @@ static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
  *
  * Open a socket used to receive messages.
  */
-static inline void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
+static XBT_INLINE void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
                                               gras_socket_t sock){
-  int size = sock->bufSize * 1024
+  int size = sock->buf_size
   int on = 1;
   struct sockaddr_in server;
 
@@ -117,27 +137,34 @@ static inline void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
   server.sin_addr.s_addr = INADDR_ANY;
   server.sin_family = AF_INET;
   if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-    THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr);
+    THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr(sock_errno));
 
   if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)))
-     THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
-           sock_errstr);
+     THROW1(system_error,0,
+           "setsockopt failed, cannot condition the socket: %s",
+           sock_errstr(sock_errno));
    
-  if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
-      setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
+  if (   setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF,
+                   (char *)&size, sizeof(size)) 
+      || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, 
+                   (char *)&size, sizeof(size))) {
      WARN1("setsockopt failed, cannot set buffer size: %s",
-          sock_errstr);
+          sock_errstr(sock_errno));
   }
        
   if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
     tcp_close(sock->sd);
-    THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr);
+    THROW2(system_error,0,
+          "Cannot bind to port %d: %s",
+          sock->port, sock_errstr(sock_errno));
   }
 
   DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
   if (listen(sock->sd, 5) < 0) {
     tcp_close(sock->sd);
-    THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr);
+    THROW2(system_error,0,
+          "Cannot listen on port %d: %s",
+          sock->port,sock_errstr(sock_errno));
   }
 
   VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
@@ -155,29 +182,31 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
 
   int i = 1;
   socklen_t s = sizeof(int);
+
+  uint32_t hisport;
                
   XBT_IN;
   gras_trp_socket_new(1,&res);
 
   sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
-  tmp_errno = errno;
+  tmp_errno = sock_errno;
 
   if (sd == -1) {
     gras_socket_close(sock);
     THROW1(system_error,0,
-          "Accept failed (%s). Droping server socket.", sock_errstr);
+          "Accept failed (%s). Droping server socket.", sock_errstr(tmp_errno));
   }
   
   if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s) 
       || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
     THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
-          sock_errstr);
+          sock_errstr(tmp_errno));
 
-  res->bufSize = sock->bufSize;
-  size = sock->bufSize * 1024;
+  res->buf_size = sock->buf_size;
+  size = sock->buf_size;
   if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
       || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
-    WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr);
+    WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr(tmp_errno));
      
   res->plugin    = sock->plugin;
   res->incoming  = sock->incoming;
@@ -185,7 +214,10 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
   res->accepting = 0;
   res->sd        = sd;
   res->port      = -1;
-  res->peer_port = peer_in.sin_port;
+
+  gras_trp_tcp_recv(res,(char*)&hisport,sizeof(hisport));
+  res->peer_port = ntohl(hisport);
+  DEBUG1("peerport %d received",res->peer_port);
 
   /* FIXME: Lock to protect inet_ntoa */
   if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
@@ -205,7 +237,9 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
   }
   
   VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
-  
+  xbt_dynar_push(((gras_trp_procdata_t)
+                 gras_libdata_by_id(gras_trp_libdata_id))->sockets,&res);
+   
   XBT_OUT;
   return res;
 }
@@ -234,7 +268,7 @@ static void gras_trp_sock_socket_close(gras_socket_t sock){
   /* close the socket */
   if(tcp_close(sock->sd) < 0) {
     WARN3("error while closing tcp socket %d: %d (%s)\n", 
-            sock->sd, sock_errno, sock_errstr);
+            sock->sd, sock_errno, sock_errstr(sock_errno));
   }
 
 }
@@ -248,7 +282,7 @@ static void gras_trp_sock_socket_close(gras_socket_t sock){
 /************************************/
 /* Temptation to merge this with file data exchange is great, 
    but doesn't work on BillWare (see tcp_write() in portable.h) */
-static inline void gras_trp_tcp_send(gras_socket_t sock,
+static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
                                     const char *data,
                                     unsigned long int size) {
   
@@ -259,9 +293,16 @@ static inline void gras_trp_tcp_send(gras_socket_t sock,
     DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
     
     if (status < 0) {
+#ifdef EWOULDBLOCK
+       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
+#else
+       if (errno == EINTR || errno == EAGAIN)
+#endif
+        continue;
+       
       THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
             sock->sd, data, size,
-            sock_errstr);
+            sock_errstr(sock_errno));
     }
     
     if (status) {
@@ -269,11 +310,11 @@ static inline void gras_trp_tcp_send(gras_socket_t sock,
       data  += status;
     } else {
       THROW1(system_error,0,"file descriptor closed (%s)",
-             sock_errstr);
+             sock_errstr(sock_errno));
     }
   }
 }
-static inline int 
+static XBT_INLINE int 
 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
                                   char *data,
                                   unsigned long int size,
@@ -281,28 +322,39 @@ gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
 
   int got = 0;
 
+  if (sock->recvd) {
+     data[0] = sock->recvd_val;
+     sock->recvd = 0;
+     got++;
+     bufsize--;
+  }   
+
   while (size>got) {
     int status = 0;
     
     DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
          sock->sd, data+got, bufsize, got,
-         hexa_str(data,got));
+         hexa_str((unsigned char*)data,got,0));
     status = tcp_read(sock->sd, data+got, (size_t)bufsize);
     
     if (status < 0) {
-      THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
+      THROW7(system_error,0,"read(%d,%p,%d) from %s:%d failed: %s; got %d so far",
             sock->sd, data+got, (int)size,
-            sock_errstr);
+            gras_socket_peer_name(sock),gras_socket_peer_port(sock),
+            sock_errstr(sock_errno),
+            got);
     }
-    DEBUG2("Got %d more bytes (%s)",status,hexa_str(data+got,status));
+    DEBUG2("Got %d more bytes (%s)",status,hexa_str((unsigned char*)data+got,status,0));
     
     if (status) {
       bufsize -= status;
       got     += status;
     } else {
-      THROW0(system_error,0,"Socket closed by remote side");
+      THROW1(system_error,errno,"Socket closed by remote side (got %d bytes before this)",
+            got);
     }
   }
+
   return got;
 }
 
@@ -332,7 +384,7 @@ gras_trp_bufiov_flush(gras_socket_t sock) {
   
   DEBUG0("Flush");
   if (data->out == buffering_buf) {
-    if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
+    if (XBT_LOG_ISENABLED(gras_trp_tcp,xbt_log_priority_debug))
       hexa_print("chunk to send ",
                 (unsigned char *) data->out_buf.data,data->out_buf.size);
     if ((data->out_buf.size - data->out_buf.pos) != 0) { 
@@ -346,6 +398,7 @@ gras_trp_bufiov_flush(gras_socket_t sock) {
 
 #ifdef HAVE_READV
   if (data->out == buffering_iov) {
+    DEBUG0("Flush out iov");
     vect = sock->bufdata->out_buf_v;
     if ((size = xbt_dynar_length(vect))) {
       DEBUG1("Flush %d chunks out of this socket",size);
@@ -356,6 +409,7 @@ gras_trp_bufiov_flush(gras_socket_t sock) {
   }
 
   if (data->in == buffering_iov) {
+    DEBUG0("Flush in iov");
     vect = sock->bufdata->in_buf_v;
     if ((size = xbt_dynar_length(vect))) {
       DEBUG1("Get %d chunks from of this socket",size);
@@ -383,14 +437,14 @@ gras_trp_buf_send(gras_socket_t sock,
           (int)data->out_buf.size,
           ((int)data->out_buf.size) + thissize -1,
           size,
-          hexa_str((char*)chunk,thissize));
+          hexa_str((unsigned char*)chunk,thissize,0));
 
     memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
 
     data->out_buf.size += thissize;
     chunk_pos      += thissize;
     DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
-          data->out_buf.size,size-chunk_pos,size,hexa_str((char*)chunk,chunk_pos));
+          data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
 
     if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
       gras_trp_bufiov_flush(sock);
@@ -434,9 +488,12 @@ gras_trp_buf_recv(gras_socket_t sock,
     data->in_buf.pos += thissize;
     chunk_pos        += thissize;
     DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
-          data->in_buf.pos,size - chunk_pos,size,hexa_str(chunk,chunk_pos));
+          data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
   }
-
+  /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
+  sock->moredata = (data->in_buf.size > data->in_buf.pos);
+  DEBUG1("There is %smore data",(sock->moredata?"":"no "));
+   
   XBT_OUT;
   return chunk_pos;
 }
@@ -459,7 +516,7 @@ gras_trp_iov_send(gras_socket_t sock,
     
 
   DEBUG1("Buffer one chunk to be sent later (%s)",
-       hexa_str((char*)chunk,size));
+       hexa_str((char*)chunk,size,0));
 
   elm.iov_len = (size_t)size;
 
@@ -537,15 +594,16 @@ gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
   data->out_buf.data = xbt_malloc(data->buffsize);
   data->out_buf.pos  = data->out_buf.size;
 
-  data->in_buf_v = data->out_buf_v = NULL;
 #ifdef HAVE_READV
+  data->in_buf_v = data->out_buf_v = NULL;
   data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
   data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
+  data->out = buffering_iov;
+#else
+  data->out = buffering_buf;
 #endif
    
   data->in = buffering_buf;
-  data->out = buffering_iov;
-  /*data->out = buffering_buf;*/
 
   sock->bufdata = data;
   return sock;
@@ -555,15 +613,18 @@ gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
  *** Code
  ***/
 void
-gras_trp_buf_setup(gras_trp_plugin_t plug) {
+gras_trp_tcp_setup(gras_trp_plugin_t plug) {
 
   plug->socket_client = gras_trp_buf_socket_client;
   plug->socket_server = gras_trp_buf_socket_server;
   plug->socket_accept = gras_trp_buf_socket_accept;
   plug->socket_close  = gras_trp_buf_socket_close;
 
+#ifdef HAVE_READV
   plug->send = gras_trp_iov_send;
-  /*plug->send = gras_trp_buf_send;*/
+#else
+  plug->send = gras_trp_buf_send;
+#endif
   plug->recv = gras_trp_buf_recv;
 
   plug->raw_send    = gras_trp_tcp_send;