vconn: Make errors in vconn names non-fatal errors.
[openvswitch] / lib / vconn-tcp.c
index 0053faf3d3f6690ef13450a3e40e6b5decb5b163..80c8f56c5b6c558846d3c8686042a592edf763cf 100644 (file)
@@ -34,6 +34,7 @@
 #include "vconn.h"
 #include <assert.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <netdb.h>
 #include <poll.h>
 #include <sys/types.h>
@@ -47,6 +48,7 @@
 #include "util.h"
 #include "openflow.h"
 #include "ofp-print.h"
+#include "packets.h"
 #include "poll-loop.h"
 
 #include "vlog.h"
@@ -65,7 +67,7 @@ struct tcp_vconn
 
 static int
 new_tcp_vconn(const char *name, int fd, int connect_status,
-              struct vconn **vconnp)
+              const struct sockaddr_in *sin, struct vconn **vconnp)
 {
     struct tcp_vconn *tcp;
     int on = 1;
@@ -81,6 +83,7 @@ new_tcp_vconn(const char *name, int fd, int connect_status,
     tcp = xmalloc(sizeof *tcp);
     tcp->vconn.class = &tcp_vconn_class;
     tcp->vconn.connect_status = connect_status;
+    tcp->vconn.ip = sin->sin_addr.s_addr;
     tcp->fd = fd;
     tcp->txbuf = NULL;
     tcp->tx_waiter = NULL;
@@ -114,7 +117,8 @@ tcp_open(const char *name, char *suffix, struct vconn **vconnp)
     host_name = strtok_r(suffix, "::", &save_ptr);
     port_string = strtok_r(NULL, "::", &save_ptr);
     if (!host_name) {
-        fatal(0, "%s: bad peer name format", name);
+        error(0, "%s: bad peer name format", name);
+        return EAFNOSUPPORT;
     }
 
     memset(&sin, 0, sizeof sin);
@@ -139,7 +143,7 @@ tcp_open(const char *name, char *suffix, struct vconn **vconnp)
     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
     if (retval < 0) {
         if (errno == EINPROGRESS) {
-            return new_tcp_vconn(name, fd, EAGAIN, vconnp);
+            return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
         } else {
             int error = errno;
             VLOG_ERR("%s: connect: %s", name, strerror(error));
@@ -147,7 +151,7 @@ tcp_open(const char *name, char *suffix, struct vconn **vconnp)
             return error;
         }
     } else {
-        return new_tcp_vconn(name, fd, 0, vconnp);
+        return new_tcp_vconn(name, fd, 0, &sin, vconnp);
     }
 }
 
@@ -191,8 +195,13 @@ again:
             return EPROTO;
         }
         want_bytes = length - rx->size;
+        if (!want_bytes) {
+            *bufferp = rx;
+            tcp->rxbuf = NULL;
+            return 0;
+        }
     }
-    buffer_reserve_tailroom(rx, want_bytes);
+    buffer_prealloc_tailroom(rx, want_bytes);
 
     retval = read(tcp->fd, buffer_tail(rx), want_bytes);
     if (retval > 0) {
@@ -208,7 +217,12 @@ again:
         }
         return EAGAIN;
     } else if (retval == 0) {
-        return rx->size ? EPROTO : EOF;
+        if (rx->size) {
+            VLOG_ERR("connection dropped mid-packet");
+            return EPROTO;
+        } else {
+            return EOF; 
+        }
     } else {
         return retval ? errno : EAGAIN;
     }
@@ -276,19 +290,19 @@ tcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
     switch (wait) {
     case WAIT_CONNECT:
-        poll_fd_wait(tcp->fd, POLLOUT, NULL);
+        poll_fd_wait(tcp->fd, POLLOUT);
         break;
 
     case WAIT_SEND:
         if (!tcp->txbuf) {
-            poll_fd_wait(tcp->fd, POLLOUT, NULL);
+            poll_fd_wait(tcp->fd, POLLOUT);
         } else {
             /* Nothing to do: need to drain txbuf first. */
         }
         break;
 
     case WAIT_RECV:
-        poll_fd_wait(tcp->fd, POLLIN, NULL);
+        poll_fd_wait(tcp->fd, POLLIN);
         break;
 
     default:
@@ -388,10 +402,13 @@ static int
 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
 {
     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
+    struct sockaddr_in sin;
+    socklen_t sin_len = sizeof sin;
+    char name[128];
     int new_fd;
     int error;
 
-    new_fd = accept(ptcp->fd, NULL, NULL);
+    new_fd = accept(ptcp->fd, &sin, &sin_len);
     if (new_fd < 0) {
         int error = errno;
         if (error != EAGAIN) {
@@ -406,7 +423,11 @@ ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
         return error;
     }
 
-    return new_tcp_vconn("tcp" /* FIXME */, new_fd, 0, new_vconnp);
+    sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin.sin_addr));
+    if (sin.sin_port != htons(OFP_TCP_PORT)) {
+        sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin.sin_port));
+    }
+    return new_tcp_vconn(name, new_fd, 0, &sin, new_vconnp);
 }
 
 static void
@@ -414,7 +435,7 @@ ptcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
 {
     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
     assert(wait == WAIT_ACCEPT);
-    poll_fd_wait(ptcp->fd, POLLIN, NULL);
+    poll_fd_wait(ptcp->fd, POLLIN);
 }
 
 struct vconn_class ptcp_vconn_class = {