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