linux-2620-rc1-update-20061228
[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 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 (osi_socket *)sockp;
74 }
75
76 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 *iovec,
99             int iovcnt, afs_int32 size, int istack)
100 {
101     KERNEL_SPACE_DECL;
102     struct msghdr msg;
103     int code;
104
105     msg.msg_iovlen = iovcnt;
106     msg.msg_iov = iovec;
107     msg.msg_name = to;
108     msg.msg_namelen = sizeof(*to);
109     msg.msg_control = NULL;
110     msg.msg_controllen = 0;
111     msg.msg_flags = 0;
112
113     TO_USER_SPACE();
114     code = sock_sendmsg(sop, &msg, size);
115     TO_KERNEL_SPACE();
116     return (code < 0) ? code : 0;
117 }
118
119
120 /* osi_NetReceive
121  * OS dependent part of kernel RX listener thread.
122  *
123  * Arguments:
124  *      so      socket to receive on, typically rx_socket
125  *      from    pointer to a sockaddr_in. 
126  *      iov     array of iovecs to fill in.
127  *      iovcnt  how many iovecs there are.
128  *      lengthp IN/OUT in: total space available in iovecs. out: size of read.
129  *
130  * Return
131  * 0 if successful
132  * error code (such as EINTER) if not
133  *
134  * Environment
135  *      Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
136  *      so we have a little space to look for packets larger than 
137  *      rx_maxReceiveSize.
138  */
139 int rxk_lastSocketError;
140 int rxk_nSocketErrors;
141 int
142 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
143                int iovcnt, int *lengthp)
144 {
145     KERNEL_SPACE_DECL;
146     struct msghdr msg;
147     int code;
148     struct iovec tmpvec[RX_MAXWVECS + 2];
149     struct socket *sop = (struct socket *)so;
150
151     if (iovcnt > RX_MAXWVECS + 2) {
152         osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
153     }
154     memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
155     msg.msg_name = from;
156     msg.msg_iov = tmpvec;
157     msg.msg_iovlen = iovcnt;
158     msg.msg_control = NULL;
159     msg.msg_controllen = 0;
160     msg.msg_flags = 0;
161
162     TO_USER_SPACE();
163     code = sock_recvmsg(sop, &msg, *lengthp, 0);
164     TO_KERNEL_SPACE();
165
166     if (code < 0) {
167 #ifdef AFS_LINUX26_ENV
168 #ifdef CONFIG_PM
169         if (
170 #ifdef PF_FREEZE
171             current->flags & PF_FREEZE
172 #else
173 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
174             !current->todo
175 #else
176             test_ti_thread_flag(current->thread_info, TIF_FREEZE)
177 #endif
178 #endif
179             )
180 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
181             refrigerator(PF_FREEZE);
182 #else
183             refrigerator();
184 #endif
185             set_current_state(TASK_INTERRUPTIBLE);
186 #endif
187 #endif
188
189         /* Clear the error before using the socket again.
190          * Oh joy, Linux has hidden header files as well. It appears we can
191          * simply call again and have it clear itself via sock_error().
192          */
193 #ifdef AFS_LINUX22_ENV
194         flush_signals(current); /* We don't want no stinkin' signals. */
195 #else
196         current->signal = 0;    /* We don't want no stinkin' signals. */
197 #endif
198         rxk_lastSocketError = code;
199         rxk_nSocketErrors++;
200     } else {
201         *lengthp = code;
202         code = 0;
203     }
204
205     return code;
206 }
207 extern rwlock_t tasklist_lock __attribute__((weak));
208 void
209 osi_StopListener(void)
210 {
211     struct task_struct *listener;
212     extern int rxk_ListenerPid;
213
214     if (&tasklist_lock)
215       read_lock(&tasklist_lock);
216 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
217     else
218       rcu_read_lock();
219 #endif
220     listener = find_task_by_pid(rxk_ListenerPid);
221     if (&tasklist_lock)
222        read_unlock(&tasklist_lock);
223 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
224     else
225       rcu_read_unlock();
226 #endif
227     while (rxk_ListenerPid) {
228         flush_signals(listener);
229         force_sig(SIGKILL, listener);
230         afs_osi_Sleep(&rxk_ListenerPid);
231     }
232     sock_release(rx_socket);
233     rx_socket = NULL;
234 }
235
236 #endif /* AFS_LINUX22_ENV */