rx: Process error queue after noticing errors
[openafs.git] / src / rx / LINUX / rx_knet.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * rx_knet.c - RX kernel send, receive and timer routines.
12  *
13  * Linux implementation.
14  */
15 #include <afsconfig.h>
16 #include "afs/param.h"
17
18
19 #include <linux/version.h>
20 #include "rx/rx_kcommon.h"
21 #include "rx.h"
22 #include "rx_atomic.h"
23 #include "rx_globals.h"
24 #include "rx_stats.h"
25 #include "rx_peer.h"
26 #include "rx_packet.h"
27 #include "rx_internal.h"
28 #include <asm/uaccess.h>
29 #ifdef AFS_RXERRQ_ENV
30 #include <linux/errqueue.h>
31 #include <linux/icmp.h>
32 #endif
33
34 #include "osi_compat.h"
35
36 /* rxk_NewSocket
37  * open and bind RX socket
38  */
39 osi_socket *
40 rxk_NewSocketHost(afs_uint32 ahost, short aport)
41 {
42     struct socket *sockp;
43     struct sockaddr_in myaddr;
44     int code;
45 #ifdef AFS_ADAPT_PMTU
46     int pmtu = IP_PMTUDISC_WANT;
47 #else
48     int pmtu = IP_PMTUDISC_DONT;
49 #endif
50
51     /* We need a better test for this. if you need it back, tell us
52      * how to detect it. 
53      */
54 #ifdef LINUX_KERNEL_SOCK_CREATE_V
55     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0);
56 #else
57     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
58 #endif
59     if (code < 0)
60         return NULL;
61
62     /* Bind socket */
63     myaddr.sin_family = AF_INET;
64     myaddr.sin_addr.s_addr = ahost;
65     myaddr.sin_port = aport;
66     code =
67         sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr));
68
69     if (code < 0) {
70         printk("sock_release(rx_socket) FIXME\n");
71         return NULL;
72     }
73
74     kernel_setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu,
75                       sizeof(pmtu));
76 #ifdef AFS_RXERRQ_ENV
77     {
78         int recverr = 1;
79         kernel_setsockopt(sockp, SOL_IP, IP_RECVERR, (char *)&recverr,
80                           sizeof(recverr));
81     }
82 #endif
83     return (osi_socket *)sockp;
84 }
85
86 osi_socket *
87 rxk_NewSocket(short aport)
88 {
89     return rxk_NewSocketHost(htonl(INADDR_ANY), aport);
90 }
91
92 /* free socket allocated by osi_NetSocket */
93 int
94 rxk_FreeSocket(struct socket *asocket)
95 {
96     AFS_STATCNT(osi_FreeSocket);
97     return 0;
98 }
99
100 #ifdef AFS_RXERRQ_ENV
101 int
102 osi_HandleSocketError(osi_socket so)
103 {
104     int ret = 0;
105     struct msghdr msg;
106     struct cmsghdr *cmsg;
107     struct sock_extended_err *err;
108     struct sockaddr_in addr;
109     struct sockaddr *offender;
110     char *controlmsgbuf = NULL;
111     int code;
112     struct socket *sop = (struct socket *)so;
113
114     if (!(controlmsgbuf = rxi_Alloc(256)))
115         goto out;
116     msg.msg_name = &addr;
117     msg.msg_namelen = sizeof(addr);
118     msg.msg_control = controlmsgbuf;
119     msg.msg_controllen = 256;
120     msg.msg_flags = 0;
121
122     code = kernel_recvmsg(sop, &msg, NULL, 0, 0,
123                           MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
124
125     if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
126         goto out;
127
128     /* kernel_recvmsg changes msg_control to point at the _end_ of the buffer,
129      * and msg_controllen is set to the number of bytes remaining */
130     msg.msg_controllen = ((char*)msg.msg_control - (char*)controlmsgbuf);
131     msg.msg_control = controlmsgbuf;
132
133     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg && CMSG_OK(&msg, cmsg);
134          cmsg = CMSG_NXTHDR(&msg, cmsg)) {
135         if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
136             break;
137     }
138     if (!cmsg)
139         goto out;
140
141     ret = 1;
142     err = CMSG_DATA(cmsg);
143     offender = SO_EE_OFFENDER(err);
144     
145     if (offender->sa_family != AF_INET)
146        goto out;
147
148     memcpy(&addr, offender, sizeof(addr));
149
150     rxi_ProcessNetError(err, addr.sin_addr.s_addr, addr.sin_port);
151
152  out:
153     if (controlmsgbuf) {
154         rxi_Free(controlmsgbuf, 256);
155     }
156     return ret;
157 }
158 #endif
159
160 /* osi_NetSend
161  *
162  * Return codes:
163  * 0 = success
164  * non-zero = failure
165  */
166 int
167 osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iovec,
168             int iovcnt, afs_int32 size, int istack)
169 {
170     struct msghdr msg;
171     int code;
172
173
174     msg.msg_name = to;
175     msg.msg_namelen = sizeof(*to);
176     msg.msg_control = NULL;
177     msg.msg_controllen = 0;
178     msg.msg_flags = 0;
179
180     code = kernel_sendmsg(sop, &msg, (struct kvec *) iovec, iovcnt, size);
181
182 #ifdef AFS_RXERRQ_ENV
183     if (code < 0) {
184         while (osi_HandleSocketError(sop))
185             ;
186     }
187 #endif
188
189     return (code < 0) ? code : 0;
190 }
191
192
193 /* osi_NetReceive
194  * OS dependent part of kernel RX listener thread.
195  *
196  * Arguments:
197  *      so      socket to receive on, typically rx_socket
198  *      from    pointer to a sockaddr_in. 
199  *      iov     array of iovecs to fill in.
200  *      iovcnt  how many iovecs there are.
201  *      lengthp IN/OUT in: total space available in iovecs. out: size of read.
202  *
203  * Return
204  * 0 if successful
205  * error code (such as EINTER) if not
206  *
207  * Environment
208  *      Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
209  *      so we have a little space to look for packets larger than 
210  *      rx_maxReceiveSize.
211  */
212 int rxk_lastSocketError;
213 int rxk_nSocketErrors;
214 int
215 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
216                int iovcnt, int *lengthp)
217 {
218     struct msghdr msg;
219     int code;
220     struct iovec tmpvec[RX_MAXWVECS + 2];
221     struct socket *sop = (struct socket *)so;
222
223     if (iovcnt > RX_MAXWVECS + 2) {
224         osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
225     }
226
227     memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
228     msg.msg_name = from;
229     msg.msg_iov = tmpvec;
230     msg.msg_iovlen = iovcnt;
231     msg.msg_control = NULL;
232     msg.msg_controllen = 0;
233     msg.msg_flags = 0;
234
235     code = kernel_recvmsg(sop, &msg, (struct kvec *)tmpvec, iovcnt,
236                           *lengthp, 0);
237     if (code < 0) {
238         afs_try_to_freeze();
239
240         /* Clear the error before using the socket again.
241          * Oh joy, Linux has hidden header files as well. It appears we can
242          * simply call again and have it clear itself via sock_error().
243          */
244         flush_signals(current); /* We don't want no stinkin' signals. */
245         rxk_lastSocketError = code;
246         rxk_nSocketErrors++;
247
248 #ifdef AFS_RXERRQ_ENV
249         while (osi_HandleSocketError(so))
250             ;
251 #endif
252     } else {
253         *lengthp = code;
254         code = 0;
255     }
256
257     return code;
258 }
259
260 void
261 osi_StopListener(void)
262 {
263     extern struct task_struct *rxk_ListenerTask;
264
265     while (rxk_ListenerTask) {
266         if (rxk_ListenerTask) {
267             flush_signals(rxk_ListenerTask);
268             force_sig(SIGKILL, rxk_ListenerTask);
269         }
270         if (!rxk_ListenerTask)
271             break;
272         afs_osi_Sleep(&rxk_ListenerTask);
273     }
274     sock_release(rx_socket);
275     rx_socket = NULL;
276 }
277