linux-tasklist-lock-weak-20050129
[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 RCSID
19     ("$Header$");
20
21 #include <linux/version.h>
22 #ifdef AFS_LINUX22_ENV
23 #include "rx/rx_kcommon.h"
24 #if defined(AFS_LINUX24_ENV)
25 #include "h/smp_lock.h"
26 #endif
27 #include <asm/uaccess.h>
28
29 /* rxk_NewSocket
30  * open and bind RX socket
31  */
32 struct osi_socket *
33 rxk_NewSocketHost(afs_uint32 ahost, short aport)
34 {
35     struct socket *sockp;
36     struct sockaddr_in myaddr;
37     int code;
38     KERNEL_SPACE_DECL;
39     int pmtu = IP_PMTUDISC_DONT;
40
41
42     /* We need a better test for this. if you need it back, tell us
43      * how to detect it. 
44      */
45 #ifdef LINUX_KERNEL_SOCK_CREATE_V
46     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0);
47 #else
48     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
49 #endif
50     if (code < 0)
51         return NULL;
52
53     /* Bind socket */
54     myaddr.sin_family = AF_INET;
55     myaddr.sin_addr.s_addr = ahost;
56     myaddr.sin_port = aport;
57     code =
58         sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr));
59
60     if (code < 0) {
61 #if defined(AFS_LINUX24_ENV)
62         printk("sock_release(rx_socket) FIXME\n");
63 #else
64         sock_release(sockp);
65 #endif
66         return NULL;
67     }
68
69     TO_USER_SPACE();
70     sockp->ops->setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu,
71                            sizeof(pmtu));
72     TO_KERNEL_SPACE();
73     return (struct osi_socket *)sockp;
74 }
75
76 struct osi_socket *
77 rxk_NewSocket(short aport)
78 {
79     return rxk_NewSocketHost(htonl(INADDR_ANY), aport);
80 }
81
82 /* free socket allocated by osi_NetSocket */
83 int
84 rxk_FreeSocket(register struct socket *asocket)
85 {
86     AFS_STATCNT(osi_FreeSocket);
87     return 0;
88 }
89
90
91 /* osi_NetSend
92  *
93  * Return codes:
94  * 0 = success
95  * non-zero = failure
96  */
97 int
98 osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iov,
99             int iovcnt, afs_int32 size, int istack)
100 {
101     KERNEL_SPACE_DECL;
102     struct msghdr msg;
103     int code;
104     struct iovec tmpvec[RX_MAXWVECS + 2];
105
106     if (iovcnt > RX_MAXWVECS + 2) {
107         osi_Panic("Too many (%d) iovecs passed to osi_NetSend\n", iovcnt);
108     }
109
110     if (iovcnt <= 2) {          /* avoid pointless uiomove */
111         tmpvec[0].iov_base = iov[0].iov_base;
112         tmpvec[0].iov_len = size;
113         msg.msg_iovlen = 1;
114     } else {
115         memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
116         msg.msg_iovlen = iovcnt;
117     }
118     msg.msg_iov = tmpvec;
119     msg.msg_name = to;
120     msg.msg_namelen = sizeof(*to);
121     msg.msg_control = NULL;
122     msg.msg_controllen = 0;
123     msg.msg_flags = 0;
124
125     TO_USER_SPACE();
126     code = sock_sendmsg(sop, &msg, size);
127     TO_KERNEL_SPACE();
128     return (code < 0) ? code : 0;
129 }
130
131
132 /* osi_NetReceive
133  * OS dependent part of kernel RX listener thread.
134  *
135  * Arguments:
136  *      so      socket to receive on, typically rx_socket
137  *      from    pointer to a sockaddr_in. 
138  *      iov     array of iovecs to fill in.
139  *      iovcnt  how many iovecs there are.
140  *      lengthp IN/OUT in: total space available in iovecs. out: size of read.
141  *
142  * Return
143  * 0 if successful
144  * error code (such as EINTER) if not
145  *
146  * Environment
147  *      Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
148  *      so we have a little space to look for packets larger than 
149  *      rx_maxReceiveSize.
150  */
151 int rxk_lastSocketError;
152 int rxk_nSocketErrors;
153 int
154 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
155                int iovcnt, int *lengthp)
156 {
157     KERNEL_SPACE_DECL;
158     struct msghdr msg;
159     int code;
160     struct iovec tmpvec[RX_MAXWVECS + 2];
161     struct socket *sop = (struct socket *)so;
162
163     if (iovcnt > RX_MAXWVECS + 2) {
164         osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
165     }
166     memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
167     msg.msg_name = from;
168     msg.msg_iov = tmpvec;
169     msg.msg_iovlen = iovcnt;
170     msg.msg_control = NULL;
171     msg.msg_controllen = 0;
172     msg.msg_flags = 0;
173
174     TO_USER_SPACE();
175     code = sock_recvmsg(sop, &msg, *lengthp, 0);
176     TO_KERNEL_SPACE();
177
178     if (code < 0) {
179         /* Clear the error before using the socket again.
180          * Oh joy, Linux has hidden header files as well. It appears we can
181          * simply call again and have it clear itself via sock_error().
182          */
183 #ifdef AFS_LINUX22_ENV
184         flush_signals(current); /* We don't want no stinkin' signals. */
185 #else
186         current->signal = 0;    /* We don't want no stinkin' signals. */
187 #endif
188         rxk_lastSocketError = code;
189         rxk_nSocketErrors++;
190     } else {
191         *lengthp = code;
192         code = 0;
193     }
194
195     return code;
196 }
197 extern rwlock_t tasklist_lock __attribute__((weak));
198 void
199 osi_StopListener(void)
200 {
201     struct task_struct *listener;
202     extern int rxk_ListenerPid;
203
204     if (&tasklist_lock)
205       read_lock(&tasklist_lock);
206     listener = find_task_by_pid(rxk_ListenerPid);
207     if (&tasklist_lock)
208        read_unlock(&tasklist_lock);
209     while (rxk_ListenerPid) {
210         flush_signals(listener);
211         force_sig(SIGKILL, listener);
212         afs_osi_Sleep(&rxk_ListenerPid);
213     }
214     sock_release(rx_socket);
215     rx_socket = NULL;
216 }
217
218 #endif /* AFS_LINUX22_ENV */