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