reindent-20030715
[openafs.git] / src / rx / UKERNEL / 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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
16 #include "rx/rx_kcommon.h"
17
18
19 #define SECONDS_TO_SLEEP        0
20 #define NANO_SECONDS_TO_SLEEP   100000000       /* 100 milliseconds */
21 #define LOOPS_PER_WAITCHECK     10      /* once per second */
22
23 unsigned short usr_rx_port = 0;
24
25 struct usr_ifnet *usr_ifnet = NULL;
26 struct usr_in_ifaddr *usr_in_ifaddr = NULL;
27
28 void rxk_InitializeSocket();
29
30 void
31 afs_rxevent_daemon(void)
32 {
33     struct timespec tv;
34     struct clock temp;
35     int i = 0;
36
37     AFS_GUNLOCK();
38     while (1) {
39         tv.tv_sec = SECONDS_TO_SLEEP;
40         tv.tv_nsec = NANO_SECONDS_TO_SLEEP;
41         usr_thread_sleep(&tv);
42         /*
43          * Check for shutdown, don't try to stop the listener
44          */
45         if (afs_termState == AFSOP_STOP_RXEVENT
46             || afs_termState == AFSOP_STOP_RXK_LISTENER) {
47             AFS_GLOCK();
48             afs_termState = AFSOP_STOP_COMPLETE;
49             afs_osi_Wakeup(&afs_termState);
50             return;
51         }
52         rxevent_RaiseEvents(&temp);
53         if (++i >= LOOPS_PER_WAITCHECK) {
54             i = 0;
55             afs_osi_CheckTimedWaits();
56         }
57     }
58 }
59
60
61 /* Loop to listen on a socket. Return setting *newcallp if this
62  * thread should become a server thread.  */
63 void
64 rxi_ListenerProc(osi_socket usockp, int *tnop, struct rx_call **newcallp)
65 {
66     struct rx_packet *tp;
67     afs_uint32 host;
68     u_short port;
69     int rc;
70
71     /*
72      * Use the rxk_GetPacketProc and rxk_PacketArrivalProc routines
73      * to allocate rx_packet buffers and pass them to the RX layer
74      * for processing.
75      */
76     while (1) {
77         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
78         usr_assert(tp != NULL);
79         rc = rxi_ReadPacket(usockp->sock, tp, &host, &port);
80         if (rc != 0) {
81             tp = rxi_ReceivePacket(tp, usockp, host, port, tnop, newcallp);
82             if (newcallp && *newcallp) {
83                 if (tp) {
84                     rxi_FreePacket(tp);
85                 }
86                 return;
87             }
88         }
89         if (tp) {
90             rxi_FreePacket(tp);
91         }
92         if (afs_termState == AFSOP_STOP_RXEVENT) {
93             afs_termState = AFSOP_STOP_RXK_LISTENER;
94             afs_osi_Wakeup(&afs_termState);
95         }
96     }
97 }
98
99 /* This is the listener process request loop. The listener process loop
100  * becomes a server thread when rxi_ListenerProc returns, and stays
101  * server thread until rxi_ServerProc returns. */
102 void
103 rxk_Listener(void)
104 {
105     int threadID;
106     osi_socket sock = (osi_socket) rx_socket;
107     struct rx_call *newcall;
108     struct usr_socket *usockp;
109
110     /*
111      * Initialize the rx_socket and start the receiver threads
112      */
113     rxk_InitializeSocket();
114
115     usockp = (struct usr_socket *)rx_socket;
116     assert(usockp != NULL);
117
118     AFS_GUNLOCK();
119     while (1) {
120         newcall = NULL;
121         threadID = -1;
122         rxi_ListenerProc(sock, &threadID, &newcall);
123         /* assert(threadID != -1); */
124         /* assert(newcall != NULL); */
125         sock = OSI_NULLSOCKET;
126         rxi_ServerProc(threadID, newcall, &sock);
127         if (sock == OSI_NULLSOCKET) {
128             break;
129         }
130     }
131     AFS_GLOCK();
132 }
133
134 /* This is the server process request loop. The server process loop
135  * becomes a listener thread when rxi_ServerProc returns, and stays
136  * listener thread until rxi_ListenerProc returns. */
137 void
138 rx_ServerProc(void)
139 {
140     osi_socket sock;
141     int threadID;
142     struct rx_call *newcall = NULL;
143
144     rxi_MorePackets(rx_maxReceiveWindow + 2);   /* alloc more packets */
145     rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
146     /* threadID is used for making decisions in GetCall.  Get it by bumping
147      * number of threads handling incoming calls */
148     threadID = rxi_availProcs++;
149
150     AFS_GUNLOCK();
151     while (1) {
152         sock = OSI_NULLSOCKET;
153         rxi_ServerProc(threadID, newcall, &sock);
154         if (sock == OSI_NULLSOCKET) {
155             break;
156         }
157         newcall = NULL;
158         threadID = -1;
159         rxi_ListenerProc(sock, &threadID, &newcall);
160         /* assert(threadID != -1); */
161         /* assert(newcall != NULL); */
162     }
163     AFS_GLOCK();
164 }
165
166 /*
167  * At this point, RX wants a socket, but still has not initialized the
168  * rx_port variable or the pointers to the packet allocater and arrival
169  * routines. Allocate the socket buffer here, but don't open it until
170  * we start the receiver threads.
171  */
172 struct osi_socket *
173 rxk_NewSocket(short aport)
174 {
175     struct usr_socket *usockp;
176
177     usockp = (struct usr_socket *)afs_osi_Alloc(sizeof(struct usr_socket));
178     usr_assert(usockp != NULL);
179
180     usockp->sock = -1;
181
182     return (struct osi_socket *)usockp;
183 }
184
185 /*
186  * This routine is called from rxk_Listener. By this time rx_port
187  * is set to 7001 and rx_socket points to the socket buffer
188  * we allocated in rxk_NewSocket. Now is the time to bind our
189  * socket and start the receiver threads.
190  */
191 void
192 rxk_InitializeSocket(void)
193 {
194     int rc, sock, i;
195 #ifdef AFS_USR_AIX_ENV
196     unsigned long len, optval, optval0, optlen;
197 #else /* AFS_USR_AIX_ENV */
198     int len, optval, optval0, optlen;
199 #endif /* AFS_USR_AIX_ENV */
200     struct usr_socket *usockp;
201     struct sockaddr_in lcladdr;
202
203     usr_assert(rx_socket != NULL);
204     usockp = (struct usr_socket *)rx_socket;
205
206 #undef socket
207     sock = socket(PF_INET, SOCK_DGRAM, 0);
208     usr_assert(sock >= 0);
209
210     memset((void *)&lcladdr, 0, sizeof(struct sockaddr_in));
211     lcladdr.sin_family = AF_INET;
212     lcladdr.sin_port = htons(usr_rx_port);
213     lcladdr.sin_addr.s_addr = INADDR_ANY;
214     rc = bind(sock, (struct sockaddr *)&lcladdr, sizeof(struct sockaddr_in));
215     usr_assert(rc >= 0);
216     len = sizeof(struct sockaddr_in);
217     rc = getsockname(sock, (struct sockaddr *)&lcladdr, &len);
218     usr_assert(rc >= 0);
219 #ifdef AFS_USR_LINUX22_ENV
220     optval0 = 131070;
221 #else
222     optval0 = 131072;
223 #endif
224     optval = optval0;
225     rc = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&optval,
226                     sizeof(optval));
227     usr_assert(rc == 0);
228     optlen = sizeof(optval);
229     rc = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&optval, &optlen);
230     usr_assert(rc == 0);
231     usr_assert(optval == optval0);
232 #ifdef AFS_USR_LINUX22_ENV
233     optval0 = 131070;
234 #else
235     optval0 = 131072;
236 #endif
237     optval = optval0;
238     rc = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&optval,
239                     sizeof(optval));
240     usr_assert(rc == 0);
241     optlen = sizeof(optval);
242     rc = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&optval, &optlen);
243     usr_assert(rc == 0);
244     usr_assert(optval == optval0);
245
246 #ifdef AFS_USR_AIX_ENV
247     optval = 1;
248     rc = setsockopt(sock, SOL_SOCKET, SO_CKSUMRECV, (void *)&optval,
249                     sizeof(optval));
250     usr_assert(rc == 0);
251 #endif /* AFS_USR_AIX_ENV */
252
253     usockp->sock = sock;
254     usockp->port = lcladdr.sin_port;
255
256     /*
257      * Set the value of rx_port to reflect the address we actually
258      * are listening on, since the kernel is probably already using 7001.
259      */
260     rx_port = usockp->port;
261 }
262
263 int
264 rxk_FreeSocket(struct usr_socket *sockp)
265 {
266     return 0;
267 }
268
269 void
270 osi_StopListener(void)
271 {
272     rxk_FreeSocket((struct usr_socket *)rx_socket);
273 }
274
275 int
276 osi_NetSend(osi_socket sockp, struct sockaddr_in *addr, struct iovec *iov,
277             int nio, afs_int32 size, int stack)
278 {
279     int rc;
280     int i;
281     unsigned long tmp;
282     struct usr_socket *usockp = (struct usr_socket *)sockp;
283     struct msghdr msg;
284     struct iovec tmpiov[64];
285
286     /*
287      * The header is in the first iovec
288      */
289     usr_assert(nio > 0 && nio <= 64);
290     for (i = 0; i < nio; i++) {
291         tmpiov[i].iov_base = iov[i].iov_base;
292         tmpiov[i].iov_len = iov[i].iov_len;
293     }
294
295     memset(&msg, 0, sizeof(msg));
296     msg.msg_name = (void *)addr;
297     msg.msg_namelen = sizeof(struct sockaddr_in);
298     msg.msg_iov = &tmpiov[0];
299     msg.msg_iovlen = nio;
300
301     rc = sendmsg(usockp->sock, &msg, 0);
302     if (rc < 0) {
303         return errno;
304     }
305     usr_assert(rc == size);
306
307     return 0;
308 }
309
310 void
311 shutdown_rxkernel(void)
312 {
313     rxk_initDone = 0;
314     rxk_shutdownPorts();
315 }
316
317 void
318 rx_Finalize(void)
319 {
320     usr_assert(0);
321 }
322
323 /*
324  * Recvmsg.
325  *
326  */
327 int
328 rxi_Recvmsg(int socket, struct msghdr *msg_p, int flags)
329 {
330     int ret;
331     do {
332         ret = recvmsg(socket, msg_p, flags);
333     } while (ret == -1 && errno == EAGAIN);
334     return ret;
335 }