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