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>
26 #ifdef HAVE_SYS_FILE_H
27 # include <sys/file.h>
33 #include "rx_atomic.h"
34 #include "rx_globals.h"
37 #include "rx_xmit_nt.h"
40 #define MAXTHREADNAMELENGTH 64
42 int debugSelectFailure; /* # of times select failed */
45 * Sleep on the unique wait channel provided.
50 LWP_WaitProcess(addr);
54 * Wakeup any threads on the channel provided.
55 * They may be woken up spuriously, and must check any conditions.
58 rxi_Wakeup(void *addr)
60 LWP_NoYieldSignal(addr);
63 PROCESS rx_listenerPid = 0; /* LWP process id of socket listener process */
64 static void* rx_ListenerProc(void *dummy);
67 * Delay the current thread the specified number of seconds.
75 static int quitListening = 0;
77 /* This routine will kill the listener thread, if it exists. */
79 rxi_StopListener(void)
82 rxi_ReScheduleEvents();
85 /* This routine will get called by the event package whenever a new,
86 earlier than others, event is posted. If the Listener process
87 is blocked in selects, this will unblock it. It also can be called
88 to force a new trip through the rxi_Listener select loop when the set
89 of file descriptors it should be listening to changes... */
91 rxi_ReScheduleEvents(void)
94 IOMGR_Cancel(rx_listenerPid);
98 rxi_InitializeThreadSupport(void)
102 LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY, &junk);
104 FD_ZERO(&rx_selectMask);
108 rxi_StartServerProc(void *(*proc) (void *), int stacksize)
111 static int number = 0;
114 sprintf(name, "srv_%d", ++number);
115 LWP_CreateProcess(proc, stacksize, RX_PROCESS_PRIORITY, NULL,
116 "rx_ServerProc", &scratchPid);
118 (*registerProgram) (scratchPid, name);
122 rxi_StartListener(void)
124 /* Priority of listener should be high, so it can keep conns alive */
125 #define RX_LIST_STACK 24000
126 LWP_CreateProcess(rx_ListenerProc, RX_LIST_STACK, LWP_MAX_PRIORITY,
127 NULL, "rx_Listener", &rx_listenerPid);
129 (*registerProgram) (rx_listenerPid, "listener");
132 /* The main loop which listens to the net for datagrams, and handles timeouts
133 and retransmissions, etc. It also is responsible for scheduling the
134 execution of pending events (in conjunction with event.c).
136 Note interaction of nextPollTime and lastPollWorked. The idea is that if rx is not
137 keeping up with the incoming stream of packets (because there are threads that
138 are interfering with its running sufficiently often), rx does a polling select
139 before doing a real IOMGR_Select system call. Doing a real select means that
140 we don't have to let other processes run before processing more packets.
142 So, our algorithm is that if the last poll on the file descriptor found useful data, or
143 we're at the time nextPollTime (which is advanced so that it occurs every 3 or 4 seconds),
144 then we try the polling select before the IOMGR_Select. If we eventually catch up
145 (which we can tell by the polling select returning no input packets ready), then we
146 don't do a polling select again until several seconds later (via nextPollTime mechanism).
150 rxi_ListenerProc(fd_set * rfds, int *tnop, struct rx_call **newcallp)
154 struct rx_packet *p = (struct rx_packet *)0;
157 afs_int32 nextPollTime; /* time to next poll FD before sleeping */
158 int lastPollWorked, doingPoll; /* true iff last poll was useful */
159 struct timeval tv, *tvp;
165 char name[MAXTHREADNAMELENGTH] = "srv_0";
170 code = LWP_CurrentProcess(&pid);
172 osi_Panic("rxi_Listener: Can't get my pid.\n");
174 rx_listenerPid = pid;
176 (*swapNameProgram) (pid, "listener", &name[0]);
179 /* See if a check for additional packets was issued */
182 /* Grab a new packet only if necessary (otherwise re-use the old one) */
184 rxi_RestoreDataBufs(p);
186 if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE)))
187 osi_Panic("rxi_ListenerProc: no packets!"); /* Shouldn't happen */
189 /* Wait for the next event time or a packet to arrive. */
190 /* event_RaiseEvents schedules any events whose time has come and
191 * then atomically computes the time to the next event, guaranteeing
192 * that this is positive. If there is no next event, it returns 0 */
194 if (!rxevent_RaiseEvents(&cv))
197 /* It's important to copy cv to tv, because the 4.3 documentation
198 * for select threatens that *tv may be updated after a select, in
199 * future editions of the system, to indicate how much of the time
200 * period has elapsed. So we shouldn't rely on tv not being altered. */
201 tv.tv_sec = cv.sec; /* Time to next event */
202 tv.tv_usec = cv.usec;
206 rx_atomic_inc(&rx_stats.selects);
208 *rfds = rx_selectMask;
210 if (lastPollWorked || nextPollTime < clock_Sec()) {
211 /* we're catching up, or haven't tried to for a few seconds */
213 nextPollTime = clock_Sec() + 4; /* try again in 4 seconds no matter what */
214 tv.tv_sec = tv.tv_usec = 0; /* make sure we poll */
216 code = select((int)(rx_maxSocketNumber + 1), rfds, 0, 0, tvp);
219 code = IOMGR_Select((int)(rx_maxSocketNumber + 1), rfds, 0, 0, tvp);
221 lastPollWorked = 0; /* default is that it didn't find anything */
225 LWP_DestroyProcess(pid);
231 * If it was a timer interrupt then we can assume that
232 * the time has advanced by roughly the value of the
233 * previous timeout, and that there is now at least
239 /* select or IOMGR_Select returned failure */
240 debugSelectFailure++; /* update debugging counter */
245 * IOMGR_Cancel is invoked whenever a new event is
246 * posted that is earlier than any existing events.
247 * So we re-evaluate the time, and then go back to
254 /* Packets have arrived, presumably:
255 * If it wasn't a timer interrupt, then no event should have
256 * timed out yet (well some event may have, but only just...), so
257 * we don't bother looking to see if any have timed out, but just
258 * go directly to reading the data packets
264 for (i = 0; p && i < rfds->fd_count; i++) {
265 socket = rfds->fd_array[i];
266 if (rxi_ReadPacket(socket, p, &host, &port)) {
268 p = rxi_ReceivePacket(p, socket, host, port, tnop,
270 if (newcallp && *newcallp) {
274 if (swapNameProgram) {
275 (*swapNameProgram) (rx_listenerPid, name, 0);
283 for (socket = rx_minSocketNumber;
284 p && socket <= rx_maxSocketNumber; socket++) {
285 if (!FD_ISSET(socket, rfds))
287 if (rxi_ReadPacket(socket, p, &host, &port)) {
288 p = rxi_ReceivePacket(p, socket, host, port, tnop,
290 if (newcallp && *newcallp) {
294 if (swapNameProgram) {
295 (*swapNameProgram) (rx_listenerPid, name, 0);
309 /* This is the listener process request loop. The listener process loop
310 * becomes a server thread when rxi_ListenerProc returns, and stays
311 * server thread until rxi_ServerProc returns. */
313 rx_ListenerProc(void *dummy)
317 struct rx_call *newcall;
320 if (!(rfds = IOMGR_AllocFDSet())) {
321 osi_Panic("rx_ListenerProc: no fd_sets!\n");
327 rxi_ListenerProc(rfds, &threadID, &newcall);
328 /* osi_Assert(threadID != -1); */
329 /* osi_Assert(newcall != NULL); */
330 sock = OSI_NULLSOCKET;
331 rxi_ServerProc(threadID, newcall, &sock);
332 /* osi_Assert(sock != OSI_NULLSOCKET); */
338 /* This is the server process request loop. The server process loop
339 * becomes a listener thread when rxi_ServerProc returns, and stays
340 * listener thread until rxi_ListenerProc returns. */
342 rx_ServerProc(void * unused)
346 struct rx_call *newcall = NULL;
349 if (!(rfds = IOMGR_AllocFDSet())) {
350 osi_Panic("rxi_ListenerProc: no fd_sets!\n");
353 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
354 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
355 /* threadID is used for making decisions in GetCall. Get it by bumping
356 * number of threads handling incoming calls */
357 threadID = rxi_availProcs++;
360 sock = OSI_NULLSOCKET;
361 rxi_ServerProc(threadID, newcall, &sock);
362 /* osi_Assert(sock != OSI_NULLSOCKET); */
364 rxi_ListenerProc(rfds, &threadID, &newcall);
365 /* osi_Assert(threadID != -1); */
366 /* osi_Assert(newcall != NULL); */
373 * Called from GetUDPSocket.
374 * Called from a single thread at startup.
375 * Returns 0 on success; -1 on failure.
378 rxi_Listen(osi_socket sock)
382 * Put the socket into non-blocking mode so that rx_Listener
383 * can do a polling read before entering select
385 if (fcntl(sock, F_SETFL, FNDELAY) == -1) {
387 (osi_Msg "rxi_Listen: unable to set non-blocking mode on socket\n");
391 if (sock > FD_SETSIZE - 1) {
392 (osi_Msg "rxi_Listen: socket descriptor > (FD_SETSIZE-1) = %d\n",
398 FD_SET(sock, &rx_selectMask);
399 if (sock > rx_maxSocketNumber)
400 rx_maxSocketNumber = sock;
401 if (sock < rx_minSocketNumber)
402 rx_minSocketNumber = sock;
410 rxi_Recvmsg(osi_socket socket, struct msghdr *msg_p, int flags)
412 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
413 while((rxi_HandleSocketError(socket)) > 0)
416 return recvmsg(socket, msg_p, flags);
420 * Simulate a blocking sendmsg on the non-blocking socket.
421 * It's non blocking because it was set that way for recvmsg.
424 rxi_Sendmsg(osi_socket socket, struct msghdr *msg_p, int flags)
426 fd_set *sfds = (fd_set *) 0;
427 while (sendmsg(socket, msg_p, flags) == -1) {
430 rx_atomic_inc(&rx_stats.sendSelects);
433 if (!(sfds = IOMGR_AllocFDSet())) {
434 (osi_Msg "rx failed to alloc fd_set: ");
435 perror("rx_sendmsg");
438 FD_SET(socket, sfds);
440 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
441 while((rxi_HandleSocketError(socket)) > 0)
445 if (WSAGetLastError())
446 #elif defined(AFS_LINUX22_ENV)
447 /* linux unfortunately returns ECONNREFUSED if the target port
448 * is no longer in use */
449 /* and EAGAIN if a UDP checksum is incorrect */
450 if (errno != EWOULDBLOCK && errno != ENOBUFS && errno != ECONNREFUSED
453 if (errno != EWOULDBLOCK && errno != ENOBUFS)
456 (osi_Msg "rx failed to send packet: ");
457 perror("rx_sendmsg");
462 if (WSAGetLastError() > 0)
463 return -WSAGetLastError();
467 while ((err = select(
473 0, sfds, 0, 0)) != 1) {
474 if (err >= 0 || errno != EINTR)
475 osi_Panic("rxi_sendmsg: select error %d.%d", err, errno);
477 FD_SET(socket, sfds);
481 IOMGR_FreeFDSet(sfds);