rx: Process error queue after noticing errors
[openafs.git] / src / rx / LINUX / rx_knet.c
index 800ca02..48a493d 100644 (file)
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
 #include <linux/version.h>
-#ifdef AFS_LINUX22_ENV
 #include "rx/rx_kcommon.h"
-#if defined(AFS_LINUX24_ENV)
-#include "h/smp_lock.h"
-#endif
+#include "rx.h"
+#include "rx_atomic.h"
+#include "rx_globals.h"
+#include "rx_stats.h"
+#include "rx_peer.h"
+#include "rx_packet.h"
+#include "rx_internal.h"
 #include <asm/uaccess.h>
+#ifdef AFS_RXERRQ_ENV
+#include <linux/errqueue.h>
+#include <linux/icmp.h>
+#endif
+
+#include "osi_compat.h"
 
 /* rxk_NewSocket
  * open and bind RX socket
  */
-struct osi_socket *
+osi_socket *
 rxk_NewSocketHost(afs_uint32 ahost, short aport)
 {
     struct socket *sockp;
     struct sockaddr_in myaddr;
     int code;
+#ifdef AFS_ADAPT_PMTU
+    int pmtu = IP_PMTUDISC_WANT;
+#else
+    int pmtu = IP_PMTUDISC_DONT;
+#endif
 
-
-#ifdef LINUX_KERNEL_IS_SELINUX
+    /* We need a better test for this. if you need it back, tell us
+     * how to detect it. 
+     */
+#ifdef LINUX_KERNEL_SOCK_CREATE_V
     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0);
 #else
     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
@@ -53,18 +67,23 @@ rxk_NewSocketHost(afs_uint32 ahost, short aport)
        sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr));
 
     if (code < 0) {
-#if defined(AFS_LINUX24_ENV)
        printk("sock_release(rx_socket) FIXME\n");
-#else
-       sock_release(sockp);
-#endif
        return NULL;
     }
 
-    return (struct osi_socket *)sockp;
+    kernel_setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu,
+                     sizeof(pmtu));
+#ifdef AFS_RXERRQ_ENV
+    {
+       int recverr = 1;
+       kernel_setsockopt(sockp, SOL_IP, IP_RECVERR, (char *)&recverr,
+                         sizeof(recverr));
+    }
+#endif
+    return (osi_socket *)sockp;
 }
 
-struct osi_socket *
+osi_socket *
 rxk_NewSocket(short aport)
 {
     return rxk_NewSocketHost(htonl(INADDR_ANY), aport);
@@ -72,12 +91,71 @@ rxk_NewSocket(short aport)
 
 /* free socket allocated by osi_NetSocket */
 int
-rxk_FreeSocket(register struct socket *asocket)
+rxk_FreeSocket(struct socket *asocket)
 {
     AFS_STATCNT(osi_FreeSocket);
     return 0;
 }
 
+#ifdef AFS_RXERRQ_ENV
+int
+osi_HandleSocketError(osi_socket so)
+{
+    int ret = 0;
+    struct msghdr msg;
+    struct cmsghdr *cmsg;
+    struct sock_extended_err *err;
+    struct sockaddr_in addr;
+    struct sockaddr *offender;
+    char *controlmsgbuf = NULL;
+    int code;
+    struct socket *sop = (struct socket *)so;
+
+    if (!(controlmsgbuf = rxi_Alloc(256)))
+       goto out;
+    msg.msg_name = &addr;
+    msg.msg_namelen = sizeof(addr);
+    msg.msg_control = controlmsgbuf;
+    msg.msg_controllen = 256;
+    msg.msg_flags = 0;
+
+    code = kernel_recvmsg(sop, &msg, NULL, 0, 0,
+                         MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
+
+    if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
+       goto out;
+
+    /* kernel_recvmsg changes msg_control to point at the _end_ of the buffer,
+     * and msg_controllen is set to the number of bytes remaining */
+    msg.msg_controllen = ((char*)msg.msg_control - (char*)controlmsgbuf);
+    msg.msg_control = controlmsgbuf;
+
+    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg && CMSG_OK(&msg, cmsg);
+         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+       if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
+           break;
+    }
+    if (!cmsg)
+       goto out;
+
+    ret = 1;
+    err = CMSG_DATA(cmsg);
+    offender = SO_EE_OFFENDER(err);
+    
+    if (offender->sa_family != AF_INET)
+       goto out;
+
+    memcpy(&addr, offender, sizeof(addr));
+
+    rxi_ProcessNetError(err, addr.sin_addr.s_addr, addr.sin_port);
+
+ out:
+    if (controlmsgbuf) {
+       rxi_Free(controlmsgbuf, 256);
+    }
+    return ret;
+}
+#endif
 
 /* osi_NetSend
  *
@@ -86,36 +164,28 @@ rxk_FreeSocket(register struct socket *asocket)
  * non-zero = failure
  */
 int
-osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iov,
+osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iovec,
            int iovcnt, afs_int32 size, int istack)
 {
-    KERNEL_SPACE_DECL;
     struct msghdr msg;
     int code;
-    struct iovec tmpvec[RX_MAXWVECS + 2];
 
-    if (iovcnt > RX_MAXWVECS + 2) {
-       osi_Panic("Too many (%d) iovecs passed to osi_NetSend\n", iovcnt);
-    }
 
-    if (iovcnt <= 2) {         /* avoid pointless uiomove */
-       tmpvec[0].iov_base = iov[0].iov_base;
-       tmpvec[0].iov_len = size;
-       msg.msg_iovlen = 1;
-    } else {
-       memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
-       msg.msg_iovlen = iovcnt;
-    }
-    msg.msg_iov = tmpvec;
     msg.msg_name = to;
     msg.msg_namelen = sizeof(*to);
     msg.msg_control = NULL;
     msg.msg_controllen = 0;
     msg.msg_flags = 0;
 
-    TO_USER_SPACE();
-    code = sock_sendmsg(sop, &msg, size);
-    TO_KERNEL_SPACE();
+    code = kernel_sendmsg(sop, &msg, (struct kvec *) iovec, iovcnt, size);
+
+#ifdef AFS_RXERRQ_ENV
+    if (code < 0) {
+       while (osi_HandleSocketError(sop))
+           ;
+    }
+#endif
+
     return (code < 0) ? code : 0;
 }
 
@@ -145,7 +215,6 @@ int
 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
               int iovcnt, int *lengthp)
 {
-    KERNEL_SPACE_DECL;
     struct msghdr msg;
     int code;
     struct iovec tmpvec[RX_MAXWVECS + 2];
@@ -154,6 +223,7 @@ osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
     if (iovcnt > RX_MAXWVECS + 2) {
        osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
     }
+
     memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
     msg.msg_name = from;
     msg.msg_iov = tmpvec;
@@ -162,22 +232,23 @@ osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
     msg.msg_controllen = 0;
     msg.msg_flags = 0;
 
-    TO_USER_SPACE();
-    code = sock_recvmsg(sop, &msg, *lengthp, 0);
-    TO_KERNEL_SPACE();
-
+    code = kernel_recvmsg(sop, &msg, (struct kvec *)tmpvec, iovcnt,
+                         *lengthp, 0);
     if (code < 0) {
+       afs_try_to_freeze();
+
        /* Clear the error before using the socket again.
         * Oh joy, Linux has hidden header files as well. It appears we can
         * simply call again and have it clear itself via sock_error().
         */
-#ifdef AFS_LINUX22_ENV
        flush_signals(current); /* We don't want no stinkin' signals. */
-#else
-       current->signal = 0;    /* We don't want no stinkin' signals. */
-#endif
        rxk_lastSocketError = code;
        rxk_nSocketErrors++;
+
+#ifdef AFS_RXERRQ_ENV
+       while (osi_HandleSocketError(so))
+           ;
+#endif
     } else {
        *lengthp = code;
        code = 0;
@@ -189,25 +260,18 @@ osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
 void
 osi_StopListener(void)
 {
-    struct task_struct *listener;
-    extern int rxk_ListenerPid;
-
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
-    read_lock(&tasklist_lock);
-#endif
-    listener = find_task_by_pid(rxk_ListenerPid);
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
-    read_unlock(&tasklist_lock);
-#endif
-    while (rxk_ListenerPid) {
-       struct task_struct *p;
+    extern struct task_struct *rxk_ListenerTask;
 
-       flush_signals(listener);
-       force_sig(SIGKILL, listener);
-       afs_osi_Sleep(&rxk_ListenerPid);
+    while (rxk_ListenerTask) {
+        if (rxk_ListenerTask) {
+           flush_signals(rxk_ListenerTask);
+           force_sig(SIGKILL, rxk_ListenerTask);
+       }
+       if (!rxk_ListenerTask)
+           break;
+       afs_osi_Sleep(&rxk_ListenerTask);
     }
     sock_release(rx_socket);
     rx_socket = NULL;
 }
 
-#endif /* AFS_LINUX22_ENV */