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