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