Whamcloud - gitweb
Land b_release_1_4_4 onto HEAD (20050810_0253)
[fs/lustre-release.git] / lnet / ulnds / socklnd / connection.c
index 3e64b33..49cca96 100644 (file)
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <portals/types.h>
-#include <portals/list.h>
 #include <portals/lib-types.h>
 #include <portals/socknal.h>
-#include <linux/kp30.h>
+#include <libcfs/kp30.h>
 #include <connection.h>
 #include <pthread.h>
 #include <errno.h>
@@ -201,103 +200,120 @@ static int new_connection(void *z)
     return(1);
 }
 
-/* FIXME assuming little endian, cleanup!! */
-#define __cpu_to_le64(x) ((__u64)(x))
-#define __le64_to_cpu(x) ((__u64)(x))
-#define __cpu_to_le32(x) ((__u32)(x))
-#define __le32_to_cpu(x) ((__u32)(x))
-#define __cpu_to_le16(x) ((__u16)(x))
-#define __le16_to_cpu(x) ((__u16)(x))
-
 extern ptl_nid_t tcpnal_mynid;
 
 int
 tcpnal_hello (int sockfd, ptl_nid_t *nid, int type, __u64 incarnation)
 {
         int                 rc;
+        int                 nob;
         ptl_hdr_t           hdr;
         ptl_magicversion_t *hmv = (ptl_magicversion_t *)&hdr.dest_nid;
 
         LASSERT (sizeof (*hmv) == sizeof (hdr.dest_nid));
 
         memset (&hdr, 0, sizeof (hdr));
-        hmv->magic         = __cpu_to_le32 (PORTALS_PROTO_MAGIC);
-        hmv->version_major = __cpu_to_le32 (PORTALS_PROTO_VERSION_MAJOR);
-        hmv->version_minor = __cpu_to_le32 (PORTALS_PROTO_VERSION_MINOR);
+        hmv->magic         = cpu_to_le32(PORTALS_PROTO_MAGIC);
+        hmv->version_major = cpu_to_le32(PORTALS_PROTO_VERSION_MAJOR);
+        hmv->version_minor = cpu_to_le32(PORTALS_PROTO_VERSION_MINOR);
         
-        hdr.src_nid = __cpu_to_le64 (tcpnal_mynid);
-        hdr.type    = __cpu_to_le32 (PTL_MSG_HELLO);
+        hdr.src_nid = cpu_to_le64(tcpnal_mynid);
+        hdr.type    = cpu_to_le32(PTL_MSG_HELLO);
+
+        hdr.msg.hello.type = cpu_to_le32(type);
+        hdr.msg.hello.incarnation = cpu_to_le64(incarnation);
 
-        hdr.msg.hello.type = __cpu_to_le32 (type);
-        hdr.msg.hello.incarnation = 0;
+        /* I don't send any interface info */
 
         /* Assume sufficient socket buffering for this message */
         rc = syscall(SYS_write, sockfd, &hdr, sizeof(hdr));
         if (rc <= 0) {
-                CERROR ("Error %d sending HELLO to %llx\n", rc, *nid);
+                CERROR ("Error %d sending HELLO to "LPX64"\n", rc, *nid);
                 return (rc);
         }
 
         rc = syscall(SYS_read, sockfd, hmv, sizeof(*hmv));
         if (rc <= 0) {
-                CERROR ("Error %d reading HELLO from %llx\n", rc, *nid);
+                CERROR ("Error %d reading HELLO from "LPX64"\n", rc, *nid);
                 return (rc);
         }
         
-        if (hmv->magic != __le32_to_cpu (PORTALS_PROTO_MAGIC)) {
-                CERROR ("Bad magic %#08x (%#08x expected) from %llx\n",
-                        __cpu_to_le32 (hmv->magic), PORTALS_PROTO_MAGIC, *nid);
+        if (hmv->magic != le32_to_cpu(PORTALS_PROTO_MAGIC)) {
+                CERROR ("Bad magic %#08x (%#08x expected) from "LPX64"\n",
+                        cpu_to_le32(hmv->magic), PORTALS_PROTO_MAGIC, *nid);
                 return (-EPROTO);
         }
 
-        if (hmv->version_major != __cpu_to_le16 (PORTALS_PROTO_VERSION_MAJOR) ||
-            hmv->version_minor != __cpu_to_le16 (PORTALS_PROTO_VERSION_MINOR)) {
+        if (hmv->version_major != cpu_to_le16 (PORTALS_PROTO_VERSION_MAJOR) ||
+            hmv->version_minor != cpu_to_le16 (PORTALS_PROTO_VERSION_MINOR)) {
                 CERROR ("Incompatible protocol version %d.%d (%d.%d expected)"
-                        " from %llx\n",
-                        __le16_to_cpu (hmv->version_major),
-                        __le16_to_cpu (hmv->version_minor),
+                        " from "LPX64"\n",
+                        le16_to_cpu (hmv->version_major),
+                        le16_to_cpu (hmv->version_minor),
                         PORTALS_PROTO_VERSION_MAJOR,
                         PORTALS_PROTO_VERSION_MINOR,
                         *nid);
                 return (-EPROTO);
         }
 
-#if (PORTALS_PROTO_VERSION_MAJOR != 0)
-# error "This code only understands protocol version 0.x"
+#if (PORTALS_PROTO_VERSION_MAJOR != 1)
+# error "This code only understands protocol version 1.x"
 #endif
-        /* version 0 sends magic/version as the dest_nid of a 'hello' header,
+        /* version 1 sends magic/version as the dest_nid of a 'hello' header,
          * so read the rest of it in now... */
 
         rc = syscall(SYS_read, sockfd, hmv + 1, sizeof(hdr) - sizeof(*hmv));
         if (rc <= 0) {
-                CERROR ("Error %d reading rest of HELLO hdr from %llx\n",
+                CERROR ("Error %d reading rest of HELLO hdr from "LPX64"\n",
                         rc, *nid);
                 return (rc);
         }
 
         /* ...and check we got what we expected */
-        if (hdr.type != __cpu_to_le32 (PTL_MSG_HELLO) ||
-            hdr.payload_length != __cpu_to_le32 (0)) {
-                CERROR ("Expecting a HELLO hdr with 0 payload,"
-                        " but got type %d with %d payload from %llx\n",
-                        __le32_to_cpu (hdr.type),
-                        __le32_to_cpu (hdr.payload_length), *nid);
+        if (hdr.type != cpu_to_le32 (PTL_MSG_HELLO)) {
+                CERROR ("Expecting a HELLO hdr "
+                        " but got type %d with %d payload from "LPX64"\n",
+                        le32_to_cpu (hdr.type),
+                        le32_to_cpu (hdr.payload_length), *nid);
                 return (-EPROTO);
         }
 
-        if (__le64_to_cpu(hdr.src_nid) == PTL_NID_ANY) {
+        if (le64_to_cpu(hdr.src_nid) == PTL_NID_ANY) {
                 CERROR("Expecting a HELLO hdr with a NID, but got PTL_NID_ANY\n");
                 return (-EPROTO);
         }
 
         if (*nid == PTL_NID_ANY) {              /* don't know peer's nid yet */
-                *nid = __le64_to_cpu(hdr.src_nid);
-        } else if (*nid != __le64_to_cpu (hdr.src_nid)) {
-                CERROR ("Connected to nid %llx, but expecting %llx\n",
-                        __le64_to_cpu (hdr.src_nid), *nid);
+                *nid = le64_to_cpu(hdr.src_nid);
+        } else if (*nid != le64_to_cpu (hdr.src_nid)) {
+                CERROR ("Connected to nid "LPX64", but expecting "LPX64"\n",
+                        le64_to_cpu (hdr.src_nid), *nid);
                 return (-EPROTO);
         }
 
+        /* Ignore any interface info in the payload */
+        nob = le32_to_cpu(hdr.payload_length);
+        if (nob > getpagesize()) {
+                CERROR("Unexpected HELLO payload %d from "LPX64"\n",
+                       nob, *nid);
+                return (-EPROTO);
+        }
+        if (nob > 0) {
+                char *space = (char *)malloc(nob);
+                
+                if (space == NULL) {
+                        CERROR("Can't allocate scratch buffer %d\n", nob);
+                        return (-ENOMEM);
+                }
+                
+                rc = syscall(SYS_read, sockfd, space, nob);
+                if (rc <= 0) {
+                        CERROR("Error %d skipping HELLO payload from "
+                               LPX64"\n", rc, *nid);
+                        return (rc);
+                }
+        }
+
         return (0);
 }
 
@@ -309,11 +325,21 @@ tcpnal_hello (int sockfd, ptl_nid_t *nid, int type, __u64 incarnation)
  */
 connection force_tcp_connection(manager m,
                                 unsigned int ip,
-                                unsigned short port)
+                                unsigned short port,
+                                procbridge pb)
 {
     connection conn;
     struct sockaddr_in addr;
+    struct sockaddr_in locaddr; 
     unsigned int id[2];
+    struct timeval tv;
+    __u64 incarnation;
+
+    int fd;
+    int option;
+    int rc;
+    int rport;
+    ptl_nid_t peernid = PTL_NID_ANY;
 
     port = tcpnal_acceptor_port;
 
@@ -323,42 +349,82 @@ connection force_tcp_connection(manager m,
     pthread_mutex_lock(&m->conn_lock);
 
     conn = hash_table_find(m->connections, id);
-    if (!conn) {
-        int fd;
-        int option;
-        ptl_nid_t peernid = PTL_NID_ANY;
-
-        bzero((char *) &addr, sizeof(addr));
-        addr.sin_family      = AF_INET;
-        addr.sin_addr.s_addr = htonl(ip);
-        addr.sin_port        = htons(port);
-
-        if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 
-            perror("tcpnal socket failed");
-            exit(-1);
-        }
-        if (connect(fd, (struct sockaddr *)&addr,
-                    sizeof(struct sockaddr_in))) {
-            perror("tcpnal connect");
-            return(0);
-        }
+    if (conn)
+            goto out;
 
+    memset(&addr, 0, sizeof(addr));
+    addr.sin_family      = AF_INET;
+    addr.sin_addr.s_addr = htonl(ip);
+    addr.sin_port        = htons(port);
+
+    memset(&locaddr, 0, sizeof(locaddr)); 
+    locaddr.sin_family = AF_INET; 
+    locaddr.sin_addr.s_addr = INADDR_ANY;
+
+    for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
+            fd = socket(AF_INET, SOCK_STREAM, 0);
+            if (fd < 0) {
+                    perror("tcpnal socket failed");
+                    goto out;
+            } 
+            
+            option = 1;
+            rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 
+                            &option, sizeof(option));
+            if (rc != 0) {
+                    perror ("Can't set SO_REUSEADDR for socket"); 
+                    close(fd);
+                    goto out;
+            } 
+
+            locaddr.sin_port = htons(rport);
+            rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
+            if (rc == 0 || errno == EACCES) {
+                    rc = connect(fd, (struct sockaddr *)&addr,
+                                 sizeof(struct sockaddr_in));
+                    if (rc == 0) {
+                            break;
+                    } else if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
+                            perror("Error connecting to remote host");
+                            close(fd);
+                            goto out;
+                    }
+            } else if (errno != EADDRINUSE) {
+                    perror("Error binding to privileged port");
+                    close(fd);
+                    goto out;
+            }
+            close(fd);
+    }
+    
+    if (rport == IPPORT_RESERVED / 2) {
+            fprintf(stderr, "Out of ports trying to bind to a reserved port\n");
+            goto out;
+    }
+    
 #if 1
-        option = 1;
-        setsockopt(fd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
-        option = 1<<20;
-        setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
-        option = 1<<20;
-        setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
+    option = 1;
+    setsockopt(fd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
+    option = 1<<20;
+    setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
+    option = 1<<20;
+    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
 #endif
    
-        /* say hello */
-        if (tcpnal_hello(fd, &peernid, SOCKNAL_CONN_ANY, 0))
-            exit(-1);
+    gettimeofday(&tv, NULL);
+    incarnation = (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
 
-        conn = allocate_connection(m, ip, port, fd);
-    }
+    /* say hello */
+    if (tcpnal_hello(fd, &peernid, SOCKNAL_CONN_ANY, incarnation))
+            exit(-1);
+    
+    conn = allocate_connection(m, ip, port, fd);
+    
+    /* let nal thread know this event right away */
+    if (conn)
+            procbridge_wakeup_nal(pb);
 
+out:
     pthread_mutex_unlock(&m->conn_lock);
     return (conn);
 }
@@ -389,7 +455,7 @@ static int bind_socket(manager m,unsigned short port)
     addr.sin_port        = htons(port);
 
     if (bind(m->bound,(struct sockaddr *)&addr,alen)<0){
-        perror ("tcpnal bind"); 
+        fprintf(stderr, "tcpnal bind: %s port %u\n", strerror(errno), port); 
         return(0);
     }