venus: Remove dedebug
[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 #if defined(HAVE_LINUX_UACCESS_H)
29 #include <linux/uaccess.h>
30 #else
31 #include <asm/uaccess.h>
32 #endif
33 #ifdef AFS_RXERRQ_ENV
34 #include <linux/errqueue.h>
35 #include <linux/icmp.h>
36 #endif
37 #include "osi_compat.h"
38
39 /* rxk_NewSocket
40  * open and bind RX socket
41  */
42 osi_socket *
43 rxk_NewSocketHost(afs_uint32 ahost, short aport)
44 {
45     struct socket *sockp;
46     struct sockaddr_in myaddr;
47     int code;
48 #ifdef AFS_ADAPT_PMTU
49     int pmtu = IP_PMTUDISC_WANT;
50 #else
51     int pmtu = IP_PMTUDISC_DONT;
52 #endif
53
54 #ifdef HAVE_LINUX_SOCK_CREATE_KERN_NS
55     code = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
56 #elif defined(HAVE_LINUX_SOCK_CREATE_KERN)
57     code = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
58 #elif defined(LINUX_KERNEL_SOCK_CREATE_V)
59     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0);
60 #else
61     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
62 #endif
63     if (code < 0)
64         return NULL;
65
66     /* Bind socket */
67     myaddr.sin_family = AF_INET;
68     myaddr.sin_addr.s_addr = ahost;
69     myaddr.sin_port = aport;
70     code =
71         sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr));
72
73     if (code < 0) {
74         printk("sock_release(rx_socket) FIXME\n");
75         return NULL;
76     }
77
78     afs_linux_sock_set_mtu_discover(sockp, pmtu);
79
80 #ifdef AFS_RXERRQ_ENV
81     afs_linux_sock_set_recverr(sockp);
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, void *cmsgbuf, size_t cmsgbuf_len)
103 {
104     struct msghdr msg;
105     struct cmsghdr *cmsg;
106     struct sock_extended_err *err;
107     struct sockaddr_in addr;
108     int code;
109     struct socket *sop = (struct socket *)so;
110
111     msg.msg_name = &addr;
112     msg.msg_namelen = sizeof(addr);
113     msg.msg_control = cmsgbuf;
114     msg.msg_controllen = cmsgbuf_len;
115     msg.msg_flags = 0;
116
117     code = kernel_recvmsg(sop, &msg, NULL, 0, 0,
118                           MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
119
120     if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
121         return 0;
122
123     /* kernel_recvmsg changes msg_control to point at the _end_ of the buffer,
124      * and msg_controllen is set to the number of bytes remaining */
125     msg.msg_controllen = ((char*)msg.msg_control - (char*)cmsgbuf);
126     msg.msg_control = cmsgbuf;
127
128     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg && CMSG_OK(&msg, cmsg);
129          cmsg = CMSG_NXTHDR(&msg, cmsg)) {
130
131         if (cmsg->cmsg_level != SOL_IP || cmsg->cmsg_type != IP_RECVERR) {
132             continue;
133         }
134
135         err = CMSG_DATA(cmsg);
136         rxi_ProcessNetError(err, addr.sin_addr.s_addr, addr.sin_port);
137     }
138
139     return 1;
140 }
141 #endif
142
143 /* osi_NetSend
144  *
145  * Return codes:
146  * 0 = success
147  * non-zero = failure
148  */
149 int
150 osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iovec,
151             int iovcnt, afs_int32 size, int istack)
152 {
153     struct msghdr msg;
154     int code;
155
156
157     msg.msg_name = to;
158     msg.msg_namelen = sizeof(*to);
159     msg.msg_control = NULL;
160     msg.msg_controllen = 0;
161     msg.msg_flags = 0;
162
163     code = kernel_sendmsg(sop, &msg, (struct kvec *) iovec, iovcnt, size);
164
165     return (code < 0) ? code : 0;
166 }
167
168
169 /* osi_NetReceive
170  * OS dependent part of kernel RX listener thread.
171  *
172  * Arguments:
173  *      so      socket to receive on, typically rx_socket
174  *      from    pointer to a sockaddr_in. 
175  *      iov     array of iovecs to fill in.
176  *      iovcnt  how many iovecs there are.
177  *      lengthp IN/OUT in: total space available in iovecs. out: size of read.
178  *
179  * Return
180  * 0 if successful
181  * error code (such as EINTER) if not
182  *
183  * Environment
184  *      Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
185  *      so we have a little space to look for packets larger than 
186  *      rx_maxReceiveSize.
187  */
188 int rxk_lastSocketError;
189 int rxk_nSocketErrors;
190 int
191 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
192                int iovcnt, int *lengthp)
193 {
194     struct msghdr msg;
195     int code;
196     struct iovec tmpvec[RX_MAXWVECS + 2];
197     struct socket *sop = (struct socket *)so;
198
199     if (iovcnt > RX_MAXWVECS + 2) {
200         osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
201     }
202
203     memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
204     msg.msg_name = from;
205 #if defined(STRUCT_MSGHDR_HAS_MSG_ITER)
206     msg.msg_iter.iov = tmpvec;
207     msg.msg_iter.nr_segs = iovcnt;
208 #else
209     msg.msg_iov = tmpvec;
210     msg.msg_iovlen = iovcnt;
211 #endif
212     msg.msg_control = NULL;
213     msg.msg_controllen = 0;
214     msg.msg_flags = 0;
215
216     code = kernel_recvmsg(sop, &msg, (struct kvec *)tmpvec, iovcnt,
217                           *lengthp, 0);
218     if (code < 0) {
219         afs_try_to_freeze();
220
221         /* Clear the error before using the socket again.
222          * Oh joy, Linux has hidden header files as well. It appears we can
223          * simply call again and have it clear itself via sock_error().
224          */
225         flush_signals(current); /* We don't want no stinkin' signals. */
226         rxk_lastSocketError = code;
227         rxk_nSocketErrors++;
228
229         rxi_HandleSocketErrors(so);
230     } else {
231         *lengthp = code;
232         code = 0;
233     }
234
235     return code;
236 }
237
238 void
239 osi_StopListener(void)
240 {
241     extern struct task_struct *rxk_ListenerTask;
242
243     while (rxk_ListenerTask) {
244         if (rxk_ListenerTask) {
245             flush_signals(rxk_ListenerTask);
246             send_sig(SIGKILL, rxk_ListenerTask, 1);
247         }
248         if (!rxk_ListenerTask)
249             break;
250         afs_osi_Sleep(&rxk_ListenerTask);
251     }
252     sock_release(rx_socket);
253     rx_socket = NULL;
254 }
255