2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* rx_user.c contains routines specific to the user space UNIX implementation of rx */
12 /* This controls the size of an fd_set; it must be defined early before
13 * the system headers define that type and the macros that operate on it.
14 * Its value should be as large as the maximum file descriptor limit we
15 * are likely to run into on any platform. Right now, that is 65536
16 * which is the default hard fd limit on Solaris 9 */
18 #define FD_SETSIZE 65536
21 #include <afsconfig.h>
22 #include <afs/param.h>
25 # include <sys/types.h> /* fd_set on older platforms */
29 # include <winsock2.h>
31 # include <unistd.h> /* select() prototype */
32 # include <sys/time.h> /* struct timeval, select() prototype */
34 # include <sys/select.h> /* fd_set on newer platforms */
36 # include <sys/socket.h>
37 # include <sys/file.h>
39 # include <sys/stat.h>
40 # include <netinet/in.h>
42 # include <sys/ioctl.h>
43 # include <sys/time.h>
46 # include "rx_atomic.h"
47 # include "rx_globals.h"
48 # include "rx_stats.h"
51 #define MAXTHREADNAMELENGTH 64
53 int debugSelectFailure; /* # of times select failed */
56 * Sleep on the unique wait channel provided.
61 LWP_WaitProcess(addr);
65 * Wakeup any threads on the channel provided.
66 * They may be woken up spuriously, and must check any conditions.
69 rxi_Wakeup(void *addr)
71 LWP_NoYieldSignal(addr);
74 PROCESS rx_listenerPid = 0; /* LWP process id of socket listener process */
75 static void* rx_ListenerProc(void *dummy);
78 * Delay the current thread the specified number of seconds.
86 static int quitListening = 0;
88 /* This routine will kill the listener thread, if it exists. */
90 rxi_StopListener(void)
93 rxi_ReScheduleEvents();
96 /* This routine will get called by the event package whenever a new,
97 earlier than others, event is posted. If the Listener process
98 is blocked in selects, this will unblock it. It also can be called
99 to force a new trip through the rxi_Listener select loop when the set
100 of file descriptors it should be listening to changes... */
102 rxi_ReScheduleEvents(void)
105 IOMGR_Cancel(rx_listenerPid);
109 rxi_InitializeThreadSupport(void)
113 LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY, &junk);
115 FD_ZERO(&rx_selectMask);
119 rxi_StartServerProc(void *(*proc) (void *), int stacksize)
122 static int number = 0;
125 sprintf(name, "srv_%d", ++number);
126 LWP_CreateProcess(proc, stacksize, RX_PROCESS_PRIORITY, NULL,
127 "rx_ServerProc", &scratchPid);
129 (*registerProgram) (scratchPid, name);
133 rxi_StartListener(void)
135 /* Priority of listener should be high, so it can keep conns alive */
136 #define RX_LIST_STACK 24000
137 LWP_CreateProcess(rx_ListenerProc, RX_LIST_STACK, LWP_MAX_PRIORITY,
138 NULL, "rx_Listener", &rx_listenerPid);
140 (*registerProgram) (rx_listenerPid, "listener");
143 /* The main loop which listens to the net for datagrams, and handles timeouts
144 and retransmissions, etc. It also is responsible for scheduling the
145 execution of pending events (in conjunction with event.c).
147 Note interaction of nextPollTime and lastPollWorked. The idea is that if rx is not
148 keeping up with the incoming stream of packets (because there are threads that
149 are interfering with its running sufficiently often), rx does a polling select
150 before doing a real IOMGR_Select system call. Doing a real select means that
151 we don't have to let other processes run before processing more packets.
153 So, our algorithm is that if the last poll on the file descriptor found useful data, or
154 we're at the time nextPollTime (which is advanced so that it occurs every 3 or 4 seconds),
155 then we try the polling select before the IOMGR_Select. If we eventually catch up
156 (which we can tell by the polling select returning no input packets ready), then we
157 don't do a polling select again until several seconds later (via nextPollTime mechanism).
161 rxi_ListenerProc(fd_set * rfds, int *tnop, struct rx_call **newcallp)
165 struct rx_packet *p = (struct rx_packet *)0;
168 afs_int32 nextPollTime; /* time to next poll FD before sleeping */
169 int lastPollWorked, doingPoll; /* true iff last poll was useful */
170 struct timeval tv, *tvp;
176 char name[MAXTHREADNAMELENGTH] = "srv_0";
181 code = LWP_CurrentProcess(&pid);
183 osi_Panic("rxi_Listener: Can't get my pid.\n");
185 rx_listenerPid = pid;
187 (*swapNameProgram) (pid, "listener", &name[0]);
190 /* See if a check for additional packets was issued */
193 /* Grab a new packet only if necessary (otherwise re-use the old one) */
195 rxi_RestoreDataBufs(p);
197 if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE)))
198 osi_Panic("rxi_ListenerProc: no packets!"); /* Shouldn't happen */
200 /* Wait for the next event time or a packet to arrive. */
201 /* event_RaiseEvents schedules any events whose time has come and
202 * then atomically computes the time to the next event, guaranteeing
203 * that this is positive. If there is no next event, it returns 0 */
205 if (!rxevent_RaiseEvents(&cv))
208 /* It's important to copy cv to tv, because the 4.3 documentation
209 * for select threatens that *tv may be updated after a select, in
210 * future editions of the system, to indicate how much of the time
211 * period has elapsed. So we shouldn't rely on tv not being altered. */
212 tv.tv_sec = cv.sec; /* Time to next event */
213 tv.tv_usec = cv.usec;
217 rx_atomic_inc(&rx_stats.selects);
219 *rfds = rx_selectMask;
221 if (lastPollWorked || nextPollTime < clock_Sec()) {
222 /* we're catching up, or haven't tried to for a few seconds */
224 nextPollTime = clock_Sec() + 4; /* try again in 4 seconds no matter what */
225 tv.tv_sec = tv.tv_usec = 0; /* make sure we poll */
227 code = select((int)(rx_maxSocketNumber + 1), rfds, 0, 0, tvp);
230 code = IOMGR_Select((int)(rx_maxSocketNumber + 1), rfds, 0, 0, tvp);
232 lastPollWorked = 0; /* default is that it didn't find anything */
236 LWP_DestroyProcess(pid);
242 * If it was a timer interrupt then we can assume that
243 * the time has advanced by roughly the value of the
244 * previous timeout, and that there is now at least
250 /* select or IOMGR_Select returned failure */
251 debugSelectFailure++; /* update debugging counter */
256 * IOMGR_Cancel is invoked whenever a new event is
257 * posted that is earlier than any existing events.
258 * So we re-evaluate the time, and then go back to
265 /* Packets have arrived, presumably:
266 * If it wasn't a timer interrupt, then no event should have
267 * timed out yet (well some event may have, but only just...), so
268 * we don't bother looking to see if any have timed out, but just
269 * go directly to reading the data packets
275 for (i = 0; p && i < rfds->fd_count; i++) {
276 socket = rfds->fd_array[i];
277 if (rxi_ReadPacket(socket, p, &host, &port)) {
279 p = rxi_ReceivePacket(p, socket, host, port, tnop,
281 if (newcallp && *newcallp) {
285 if (swapNameProgram) {
286 (*swapNameProgram) (rx_listenerPid, name, 0);
294 for (socket = rx_minSocketNumber;
295 p && socket <= rx_maxSocketNumber; socket++) {
296 if (!FD_ISSET(socket, rfds))
298 if (rxi_ReadPacket(socket, p, &host, &port)) {
299 p = rxi_ReceivePacket(p, socket, host, port, tnop,
301 if (newcallp && *newcallp) {
305 if (swapNameProgram) {
306 (*swapNameProgram) (rx_listenerPid, name, 0);
320 /* This is the listener process request loop. The listener process loop
321 * becomes a server thread when rxi_ListenerProc returns, and stays
322 * server thread until rxi_ServerProc returns. */
324 rx_ListenerProc(void *dummy)
328 struct rx_call *newcall;
331 if (!(rfds = IOMGR_AllocFDSet())) {
332 osi_Panic("rx_ListenerProc: no fd_sets!\n");
338 rxi_ListenerProc(rfds, &threadID, &newcall);
339 /* osi_Assert(threadID != -1); */
340 /* osi_Assert(newcall != NULL); */
341 sock = OSI_NULLSOCKET;
342 rxi_ServerProc(threadID, newcall, &sock);
343 /* osi_Assert(sock != OSI_NULLSOCKET); */
349 /* This is the server process request loop. The server process loop
350 * becomes a listener thread when rxi_ServerProc returns, and stays
351 * listener thread until rxi_ListenerProc returns. */
353 rx_ServerProc(void * unused)
357 struct rx_call *newcall = NULL;
360 if (!(rfds = IOMGR_AllocFDSet())) {
361 osi_Panic("rxi_ListenerProc: no fd_sets!\n");
364 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
365 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
366 /* threadID is used for making decisions in GetCall. Get it by bumping
367 * number of threads handling incoming calls */
368 threadID = rxi_availProcs++;
371 sock = OSI_NULLSOCKET;
372 rxi_ServerProc(threadID, newcall, &sock);
373 /* osi_Assert(sock != OSI_NULLSOCKET); */
375 rxi_ListenerProc(rfds, &threadID, &newcall);
376 /* osi_Assert(threadID != -1); */
377 /* osi_Assert(newcall != NULL); */
384 * Called from GetUDPSocket.
385 * Called from a single thread at startup.
386 * Returns 0 on success; -1 on failure.
389 rxi_Listen(osi_socket sock)
393 * Put the socket into non-blocking mode so that rx_Listener
394 * can do a polling read before entering select
396 if (fcntl(sock, F_SETFL, FNDELAY) == -1) {
398 (osi_Msg "rxi_Listen: unable to set non-blocking mode on socket\n");
402 if (sock > FD_SETSIZE - 1) {
403 (osi_Msg "rxi_Listen: socket descriptor > (FD_SETSIZE-1) = %d\n",
409 FD_SET(sock, &rx_selectMask);
410 if (sock > rx_maxSocketNumber)
411 rx_maxSocketNumber = sock;
412 if (sock < rx_minSocketNumber)
413 rx_minSocketNumber = sock;
421 rxi_Recvmsg(osi_socket socket, struct msghdr *msg_p, int flags)
423 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
424 while((rxi_HandleSocketError(socket)) > 0)
427 return recvmsg(socket, msg_p, flags);
431 * Simulate a blocking sendmsg on the non-blocking socket.
432 * It's non blocking because it was set that way for recvmsg.
435 rxi_Sendmsg(osi_socket socket, struct msghdr *msg_p, int flags)
437 fd_set *sfds = (fd_set *) 0;
438 while (sendmsg(socket, msg_p, flags) == -1) {
441 rx_atomic_inc(&rx_stats.sendSelects);
444 if (!(sfds = IOMGR_AllocFDSet())) {
445 (osi_Msg "rx failed to alloc fd_set: ");
446 perror("rx_sendmsg");
449 FD_SET(socket, sfds);
451 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
452 while((rxi_HandleSocketError(socket)) > 0)
456 if (WSAGetLastError())
457 #elif defined(AFS_LINUX22_ENV)
458 /* linux unfortunately returns ECONNREFUSED if the target port
459 * is no longer in use */
460 /* and EAGAIN if a UDP checksum is incorrect */
461 if (errno != EWOULDBLOCK && errno != ENOBUFS && errno != ECONNREFUSED
464 if (errno != EWOULDBLOCK && errno != ENOBUFS)
467 (osi_Msg "rx failed to send packet: ");
468 perror("rx_sendmsg");
473 if (WSAGetLastError() > 0)
474 return -WSAGetLastError();
478 while ((err = select(socket + 1, 0, sfds, 0, 0)) != 1) {
479 if (err >= 0 || errno != EINTR)
480 osi_Panic("rxi_sendmsg: select error %d.%d", err, errno);
484 IOMGR_FreeFDSet(sfds);