rx: Make statistics interface use Atomics
[openafs.git] / src / rx / rx_kcommon.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_kcommon.c - Common kernel RX code for all system types.
12  */
13
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17
18 #include "rx/rx_kcommon.h"
19 #include "rx_atomic.h"
20 #include "rx_stats.h"
21
22 #ifdef AFS_HPUX110_ENV
23 #include "h/tihdr.h"
24 #include <xti.h>
25 #endif
26 #include "afsint.h"
27
28 #ifndef RXK_LISTENER_ENV
29 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize);   /* set to packet allocation procedure */
30 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
31 #endif
32
33 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
34 extern struct interfaceAddr afs_cb_interface;
35
36 rxk_ports_t rxk_ports;
37 rxk_portRocks_t rxk_portRocks;
38
39 int rxk_initDone = 0;
40
41 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
42 #define ADDRSPERSITE 16
43 static afs_uint32 myNetAddrs[ADDRSPERSITE];
44 static int myNetMTUs[ADDRSPERSITE];
45 static int numMyNetAddrs = 0;
46 #endif
47
48 #if defined(AFS_DARWIN80_ENV)
49 #define sobind sock_bind
50 #define soclose sock_close
51 #endif
52
53 /* add a port to the monitored list, port # is in network order */
54 static int
55 rxk_AddPort(u_short aport, char *arock)
56 {
57     int i;
58     unsigned short *tsp, ts;
59     int zslot;
60
61     zslot = -1;                 /* look for an empty slot simultaneously */
62     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
63         if (((ts = *tsp) == 0) && (zslot == -1))
64             zslot = i;
65         if (ts == aport) {
66             return 0;
67         }
68     }
69     /* otherwise allocate a new port slot */
70     if (zslot < 0)
71         return E2BIG;           /* all full */
72     rxk_ports[zslot] = aport;
73     rxk_portRocks[zslot] = arock;
74     return 0;
75 }
76
77 /* remove as port from the monitored list, port # is in network order */
78 int
79 rxk_DelPort(u_short aport)
80 {
81     int i;
82     unsigned short *tsp;
83
84     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
85         if (*tsp == aport) {
86             /* found it, adjust ref count and free the port reference if all gone */
87             *tsp = 0;
88             return 0;
89         }
90     }
91     /* otherwise port not found */
92     return ENOENT;
93 }
94
95 void
96 rxk_shutdownPorts(void)
97 {
98     int i;
99     for (i = 0; i < MAXRXPORTS; i++) {
100         if (rxk_ports[i]) {
101             rxk_ports[i] = 0;
102 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
103             soclose((struct socket *)rxk_portRocks[i]);
104 #endif
105             rxk_portRocks[i] = NULL;
106         }
107     }
108 }
109
110 osi_socket
111 rxi_GetHostUDPSocket(u_int host, u_short port)
112 {
113     osi_socket *sockp;
114     sockp = (osi_socket *)rxk_NewSocketHost(host, port);
115     if (sockp == (osi_socket *)0)
116         return OSI_NULLSOCKET;
117         rxk_AddPort(port, (char *)sockp);
118     return (osi_socket) sockp;
119 }
120
121 osi_socket
122 rxi_GetUDPSocket(u_short port)
123 {
124     return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
125 }
126
127 /*
128  * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
129  * unsigned long value into the given buffer.  Returns 0 on success,
130  * and a value less than 0 on failure.  The contents of the buffer is
131  * defined only on success.
132  */
133
134 int
135 osi_utoa(char *buf, size_t len, unsigned long val)
136 {
137     long k;                     /* index of first byte of string value */
138
139     /* we definitely need room for at least one digit and NUL */
140
141     if (len < 2) {
142         return -1;
143     }
144
145     /* compute the string form from the high end of the buffer */
146
147     buf[len - 1] = '\0';
148     for (k = len - 2; k >= 0; k--) {
149         buf[k] = val % 10 + '0';
150         val /= 10;
151
152         if (val == 0)
153             break;
154     }
155
156     /* did we finish converting val to string form? */
157
158     if (val != 0) {
159         return -2;
160     }
161
162     /* this should never happen */
163
164     if (k < 0) {
165         return -3;
166     }
167
168     /* this should never happen */
169
170     if (k >= len) {
171         return -4;
172     }
173
174     /* if necessary, relocate string to beginning of buf[] */
175
176     if (k > 0) {
177
178         /*
179          * We need to achieve the effect of calling
180          *
181          * memmove(buf, &buf[k], len - k);
182          *
183          * However, since memmove() is not available in all
184          * kernels, we explicitly do an appropriate copy.
185          */
186
187         char *dst = buf;
188         char *src = buf + k;
189
190         while ((*dst++ = *src++) != '\0')
191             continue;
192     }
193
194     return 0;
195 }
196
197 #ifndef AFS_LINUX26_ENV
198 /*
199  * osi_AssertFailK() -- used by the osi_Assert() macro.
200  *
201  * It essentially does
202  *
203  * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
204  *
205  * Since the kernel version of osi_Panic() only passes its first
206  * argument to the native panic(), we construct a single string and hand
207  * that to osi_Panic().
208  */
209 void
210 osi_AssertFailK(const char *expr, const char *file, int line)
211 {
212     static const char msg0[] = "assertion failed: ";
213     static const char msg1[] = ", file: ";
214     static const char msg2[] = ", line: ";
215     static const char msg3[] = "\n";
216
217     /*
218      * These buffers add up to 1K, which is a pleasantly nice round
219      * value, but probably not vital.
220      */
221     char buf[1008];
222     char linebuf[16];
223
224     /* check line number conversion */
225
226     if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
227         osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
228     }
229
230     /* okay, panic */
231
232 #define ADDBUF(BUF, STR)                                        \
233         if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) {    \
234                 strcat(BUF, (char *)(STR));                             \
235         }
236
237     buf[0] = '\0';
238     ADDBUF(buf, msg0);
239     ADDBUF(buf, expr);
240     ADDBUF(buf, msg1);
241     ADDBUF(buf, file);
242     ADDBUF(buf, msg2);
243     ADDBUF(buf, linebuf);
244     ADDBUF(buf, msg3);
245
246 #undef ADDBUF
247
248     osi_Panic("%s", buf);
249 }
250 #else
251 void
252 osi_AssertFailK(const char *expr, const char *file, int line)
253 {
254     printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
255 }
256 #endif
257
258 #ifndef UKERNEL
259 /* This is the server process request loop. Kernel server
260  * processes never become listener threads */
261 void *
262 rx_ServerProc(void *unused)
263 {
264     int threadID;
265
266     rxi_MorePackets(rx_maxReceiveWindow + 2);   /* alloc more packets */
267     MUTEX_ENTER(&rx_quota_mutex);
268     rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
269     /* threadID is used for making decisions in GetCall.  Get it by bumping
270      * number of threads handling incoming calls */
271     threadID = rxi_availProcs++;
272     MUTEX_EXIT(&rx_quota_mutex);
273
274 #ifdef RX_ENABLE_LOCKS
275     AFS_GUNLOCK();
276 #endif /* RX_ENABLE_LOCKS */
277     rxi_ServerProc(threadID, NULL, NULL);
278 #ifdef RX_ENABLE_LOCKS
279     AFS_GLOCK();
280 #endif /* RX_ENABLE_LOCKS */
281
282     return NULL;
283 }
284 #endif /* !UKERNEL */
285
286 #ifndef RXK_LISTENER_ENV
287 /* asize includes the Rx header */
288 static int
289 MyPacketProc(struct rx_packet **ahandle, int asize)
290 {
291     struct rx_packet *tp;
292
293     /* If this is larger than we expected, increase rx_maxReceiveDataSize */
294     /* If we can't scrounge enough cbufs, then we have to drop the packet,
295      * but we should set a flag so we magic up some more at our leisure.
296      */
297
298     if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
299         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
300         if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
301             if (0 <
302                 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
303                                  RX_PACKET_CLASS_RECV_CBUF)) {
304                 rxi_FreePacket(tp);
305                 tp = NULL;
306                 if (rx_stats_active) {
307                     MUTEX_ENTER(&rx_stats_mutex);
308                     rx_stats.noPacketBuffersOnRead++;
309                     MUTEX_EXIT(&rx_stats_mutex);
310                 }
311             }
312         }
313     } else {
314         /*
315          * XXX if packet is too long for our buffer,
316          * should do this at a higher layer and let other
317          * end know we're losing.
318          */
319         if (rx_stats_active) {
320             MUTEX_ENTER(&rx_stats_mutex);
321             rx_stats.bogusPacketOnRead++;
322             MUTEX_EXIT(&rx_stats_mutex);
323         }
324         /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
325         dpf(("rx: packet dropped: bad ulen=%d\n", asize));
326         tp = NULL;
327     }
328
329     if (!tp)
330         return -1;
331     /* otherwise we have a packet, set appropriate values */
332     *ahandle = tp;
333     return 0;
334 }
335
336 static int
337 MyArrivalProc(struct rx_packet *ahandle,
338               struct sockaddr_in *afrom,
339               struct socket *arock,
340               afs_int32 asize)
341 {
342     /* handle basic rx packet */
343     ahandle->length = asize - RX_HEADER_SIZE;
344     rxi_DecodePacketHeader(ahandle);
345     ahandle =
346         rxi_ReceivePacket(ahandle, arock,
347                           afrom->sin_addr.s_addr, afrom->sin_port, NULL,
348                           NULL);
349
350     /* free the packet if it has been returned */
351     if (ahandle)
352         rxi_FreePacket(ahandle);
353     return 0;
354 }
355 #endif /* !RXK_LISTENER_ENV */
356
357 void
358 rxi_StartListener(void)
359 {
360     /* if kernel, give name of appropriate procedures */
361 #ifndef RXK_LISTENER_ENV
362     rxk_GetPacketProc = MyPacketProc;
363     rxk_PacketArrivalProc = MyArrivalProc;
364     rxk_init();
365 #endif
366 }
367
368 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
369   to get interesting information. */
370 void
371 rxi_InitPeerParams(struct rx_peer *pp)
372 {
373     u_short rxmtu;
374
375 #ifdef  ADAPT_MTU
376 # ifndef AFS_SUN5_ENV
377 #  ifdef AFS_USERSPACE_IP_ADDR
378     afs_int32 i;
379     afs_int32 mtu;
380
381     i = rxi_Findcbi(pp->host);
382     if (i == -1) {
383         pp->timeout.sec = 3;
384         /* pp->timeout.usec = 0; */
385         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
386     } else {
387         pp->timeout.sec = 2;
388         /* pp->timeout.usec = 0; */
389         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
390         mtu = ntohl(afs_cb_interface.mtu[i]);
391         /* Diminish the packet size to one based on the MTU given by
392          * the interface. */
393         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
394             rxmtu = mtu - RX_IPUDP_SIZE;
395             if (rxmtu < pp->ifMTU)
396                 pp->ifMTU = rxmtu;
397         }
398     }
399 #  else /* AFS_USERSPACE_IP_ADDR */
400     rx_ifnet_t ifn;
401
402 #   if !defined(AFS_SGI62_ENV)
403     if (numMyNetAddrs == 0)
404         (void)rxi_GetIFInfo();
405 #   endif
406
407     ifn = rxi_FindIfnet(pp->host, NULL);
408     if (ifn) {
409         pp->timeout.sec = 2;
410         /* pp->timeout.usec = 0; */
411         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
412 #   ifdef IFF_POINTOPOINT
413         if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
414             /* wish we knew the bit rate and the chunk size, sigh. */
415             pp->timeout.sec = 4;
416             pp->ifMTU = RX_PP_PACKET_SIZE;
417         }
418 #   endif /* IFF_POINTOPOINT */
419         /* Diminish the packet size to one based on the MTU given by
420          * the interface. */
421         if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
422             rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
423             if (rxmtu < pp->ifMTU)
424                 pp->ifMTU = rxmtu;
425         }
426     } else {                    /* couldn't find the interface, so assume the worst */
427         pp->timeout.sec = 3;
428         /* pp->timeout.usec = 0; */
429         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
430     }
431 #  endif /* else AFS_USERSPACE_IP_ADDR */
432 # else /* AFS_SUN5_ENV */
433     afs_int32 mtu;
434
435     mtu = rxi_FindIfMTU(pp->host);
436
437     if (mtu <= 0) {
438         pp->timeout.sec = 3;
439         /* pp->timeout.usec = 0; */
440         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
441     } else {
442         pp->timeout.sec = 2;
443         /* pp->timeout.usec = 0; */
444         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
445
446         /* Diminish the packet size to one based on the MTU given by
447          * the interface. */
448         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
449             rxmtu = mtu - RX_IPUDP_SIZE;
450             if (rxmtu < pp->ifMTU)
451                 pp->ifMTU = rxmtu;
452         }
453     }
454 # endif /* AFS_SUN5_ENV */
455 #else /* ADAPT_MTU */
456     pp->rateFlag = 2;           /* start timing after two full packets */
457     pp->timeout.sec = 2;
458     pp->ifMTU = OLD_MAX_PACKET_SIZE;
459 #endif /* else ADAPT_MTU */
460     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
461     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
462     pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
463     pp->ifDgramPackets =
464         MIN(rxi_nDgramPackets,
465             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
466     pp->maxDgramPackets = 1;
467
468     /* Initialize slow start parameters */
469     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
470     pp->cwind = 1;
471     pp->nDgramPackets = 1;
472     pp->congestSeq = 0;
473 }
474
475
476 /* The following code is common to several system types, but not all. The
477  * separate ones are found in the system specific subdirectories.
478  */
479
480
481 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
482 /* Routine called during the afsd "-shutdown" process to put things back to
483  * the initial state.
484  */
485 static struct protosw parent_proto;     /* udp proto switch */
486
487 void
488 shutdown_rxkernel(void)
489 {
490     struct protosw *tpro, *last;
491     last = inetdomain.dom_protoswNPROTOSW;
492     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
493         if (tpro->pr_protocol == IPPROTO_UDP) {
494             /* restore original udp protocol switch */
495             memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
496             memset((void *)&parent_proto, 0, sizeof(parent_proto));
497             rxk_initDone = 0;
498             rxk_shutdownPorts();
499             return;
500         }
501     dpf(("shutdown_rxkernel: no udp proto"));
502 }
503 #endif /* !AIX && !SUN && !NCR  && !UKERNEL */
504
505 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
506 /* Determine what the network interfaces are for this machine. */
507
508 #ifdef AFS_USERSPACE_IP_ADDR
509 int
510 rxi_GetcbiInfo(void)
511 {
512     int i, j, different = 0, num = ADDRSPERSITE;
513     int rxmtu, maxmtu;
514     afs_uint32 ifinaddr;
515     afs_uint32 addrs[ADDRSPERSITE];
516     int mtus[ADDRSPERSITE];
517
518     memset((void *)addrs, 0, sizeof(addrs));
519     memset((void *)mtus, 0, sizeof(mtus));
520
521     if (afs_cb_interface.numberOfInterfaces < num)
522         num = afs_cb_interface.numberOfInterfaces;
523     for (i = 0; i < num; i++) {
524         if (!afs_cb_interface.mtu[i])
525             afs_cb_interface.mtu[i] = htonl(1500);
526         rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
527         ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
528         if (myNetAddrs[i] != ifinaddr)
529             different++;
530
531         mtus[i] = rxmtu;
532         rxmtu = rxi_AdjustIfMTU(rxmtu);
533         maxmtu =
534             rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
535         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
536         addrs[i++] = ifinaddr;
537         if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
538             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
539             rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
540         }
541     }
542
543     rx_maxJumboRecvSize =
544         RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
545         ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
546     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
547
548     if (different) {
549         for (j = 0; j < i; j++) {
550             myNetMTUs[j] = mtus[j];
551             myNetAddrs[j] = addrs[j];
552         }
553     }
554     return different;
555 }
556
557
558 /* Returns the afs_cb_interface inxex which best matches address.
559  * If none is found, we return -1.
560  */
561 afs_int32
562 rxi_Findcbi(afs_uint32 addr)
563 {
564     int j;
565     afs_uint32 myAddr, thisAddr, netMask, subnetMask;
566     afs_int32 rvalue = -1;
567     int match_value = 0;
568
569     if (numMyNetAddrs == 0)
570         (void)rxi_GetcbiInfo();
571
572     myAddr = ntohl(addr);
573
574     if (IN_CLASSA(myAddr))
575         netMask = IN_CLASSA_NET;
576     else if (IN_CLASSB(myAddr))
577         netMask = IN_CLASSB_NET;
578     else if (IN_CLASSC(myAddr))
579         netMask = IN_CLASSC_NET;
580     else
581         netMask = 0;
582
583     for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
584         thisAddr = ntohl(afs_cb_interface.addr_in[j]);
585         subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
586         if ((myAddr & netMask) == (thisAddr & netMask)) {
587             if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
588                 if (myAddr == thisAddr) {
589                     match_value = 4;
590                     rvalue = j;
591                     break;
592                 }
593                 if (match_value < 3) {
594                     match_value = 3;
595                     rvalue = j;
596                 }
597             } else {
598                 if (match_value < 2) {
599                     match_value = 2;
600                     rvalue = j;
601                 }
602             }
603         }
604     }
605
606     return (rvalue);
607 }
608
609 #else /* AFS_USERSPACE_IP_ADDR */
610
611 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
612 #define IFADDR2SA(f) (&((f)->ifa_addr))
613 #else /* AFS_AIX41_ENV */
614 #define IFADDR2SA(f) ((f)->ifa_addr)
615 #endif
616
617 int
618 rxi_GetIFInfo(void)
619 {
620     int i = 0;
621     int different = 0;
622
623     int rxmtu, maxmtu;
624     afs_uint32 addrs[ADDRSPERSITE];
625     int mtus[ADDRSPERSITE];
626     afs_uint32 ifinaddr;
627 #if defined(AFS_DARWIN80_ENV)
628     errno_t t;
629     unsigned int count;
630     int cnt=0, m, j;
631     rx_ifaddr_t *ifads;
632     rx_ifnet_t *ifns;
633     struct sockaddr sout;
634     struct sockaddr_in *sin;
635     struct in_addr pin;
636 #else
637     rx_ifaddr_t ifad;   /* ifnet points to a if_addrlist of ifaddrs */
638     rx_ifnet_t ifn;
639 #endif
640
641     memset(addrs, 0, sizeof(addrs));
642     memset(mtus, 0, sizeof(mtus));
643
644 #if defined(AFS_DARWIN80_ENV)
645     if (!ifnet_list_get(AF_INET, &ifns, &count)) {
646         for (m = 0; m < count; m++) {
647             if (!ifnet_get_address_list(ifns[m], &ifads)) {
648                 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
649                     if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
650                         sin = (struct sockaddr_in *)&sout;
651                         rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
652                         ifinaddr = ntohl(sin->sin_addr.s_addr);
653                         if (myNetAddrs[i] != ifinaddr) {
654                             different++;
655                         }
656                         mtus[i] = rxmtu;
657                         rxmtu = rxi_AdjustIfMTU(rxmtu);
658                         maxmtu =
659                             rxmtu * rxi_nRecvFrags +
660                             ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
661                         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
662                         addrs[i++] = ifinaddr;
663                         if (!rx_IsLoopbackAddr(ifinaddr) &&
664                             (maxmtu > rx_maxReceiveSize)) {
665                             rx_maxReceiveSize =
666                                 MIN(RX_MAX_PACKET_SIZE, maxmtu);
667                             rx_maxReceiveSize =
668                                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
669                         }
670                         cnt++;
671                     }
672                 }
673                 ifnet_free_address_list(ifads);
674             }
675         }
676         ifnet_list_free(ifns);
677     }
678 #else
679 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
680     TAILQ_FOREACH(ifn, &ifnet, if_link) {
681         if (i >= ADDRSPERSITE)
682             break;
683 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
684     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
685          ifn = ifn->if_list.tqe_next) {
686 #else
687     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
688 #endif
689         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
690 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
691         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
692             if (i >= ADDRSPERSITE)
693                 break;
694 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
695         for (ifad = ifn->if_addrlist.tqh_first;
696              ifad != NULL && i < ADDRSPERSITE;
697              ifad = ifad->ifa_list.tqe_next) {
698 #else
699         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
700              ifad = ifad->ifa_next) {
701 #endif
702             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
703                 ifinaddr =
704                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
705                           s_addr);
706                 if (myNetAddrs[i] != ifinaddr) {
707                     different++;
708                 }
709                 mtus[i] = rxmtu;
710                 rxmtu = rxi_AdjustIfMTU(rxmtu);
711                 maxmtu =
712                     rxmtu * rxi_nRecvFrags +
713                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
714                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
715                 addrs[i++] = ifinaddr;
716                 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
717                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
718                     rx_maxReceiveSize =
719                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
720                 }
721             }
722         }
723     }
724 #endif
725
726     rx_maxJumboRecvSize =
727         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
728         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
729     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
730
731     if (different) {
732         int l;
733         for (l = 0; l < i; l++) {
734             myNetMTUs[l] = mtus[l];
735             myNetAddrs[l] = addrs[l];
736         }
737     }
738     return different;
739 }
740
741 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
742 /* Returns ifnet which best matches address */
743 rx_ifnet_t
744 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
745 {
746     struct sockaddr_in s, sr;
747     rx_ifaddr_t ifad;
748
749     s.sin_family = AF_INET;
750     s.sin_addr.s_addr = addr;
751     ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
752
753     if (ifad && maskp) {
754         rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
755         *maskp = sr.sin_addr.s_addr;
756     }
757     return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
758 }
759
760 #else /* DARWIN || XBSD */
761
762 /* Returns ifnet which best matches address */
763 rx_ifnet_t
764 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
765 {
766     int match_value = 0;
767     extern struct in_ifaddr *in_ifaddr;
768     struct in_ifaddr *ifa, *ifad = NULL;
769
770     addr = ntohl(addr);
771
772 #if defined(AFS_DARWIN_ENV)
773     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
774          ifa = TAILQ_NEXT(ifa, ia_link)) {
775 #else
776     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
777 #endif
778         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
779             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
780                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
781                     match_value = 4;
782                     ifad = ifa;
783                     goto done;
784                 }
785                 if (match_value < 3) {
786                     ifad = ifa;
787                     match_value = 3;
788                 }
789             } else {
790                 if (match_value < 2) {
791                     ifad = ifa;
792                     match_value = 2;
793                 }
794             }
795         }                       /* if net matches */
796     }                           /* for all in_ifaddrs */
797
798   done:
799     if (ifad && maskp)
800         *maskp = ifad->ia_subnetmask;
801     return (ifad ? ifad->ia_ifp : NULL);
802 }
803 #endif /* else DARWIN || XBSD */
804 #endif /* else AFS_USERSPACE_IP_ADDR */
805 #endif /* !SUN5 && !SGI62 */
806
807
808 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
809  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
810  * system specific subdirectories for all systems. But for the moment,
811  * most of it is simple to follow common code.
812  */
813 #if !defined(UKERNEL)
814 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
815 /* rxk_NewSocket creates a new socket on the specified port. The port is
816  * in network byte order.
817  */
818 osi_socket *
819 rxk_NewSocketHost(afs_uint32 ahost, short aport)
820 {
821     afs_int32 code;
822 #ifdef AFS_DARWIN80_ENV
823     socket_t newSocket;
824 #else
825     struct socket *newSocket;
826 #endif
827 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
828     struct mbuf *nam;
829 #endif
830     struct sockaddr_in myaddr;
831 #ifdef AFS_HPUX110_ENV
832     /* prototype copied from kernel source file streams/str_proto.h */
833     extern MBLKP allocb_wait(int, int);
834     MBLKP bindnam;
835     int addrsize = sizeof(struct sockaddr_in);
836     struct file *fp;
837     extern struct fileops socketops;
838 #endif
839 #ifdef AFS_SGI65_ENV
840     bhv_desc_t bhv;
841 #endif
842
843     AFS_STATCNT(osi_NewSocket);
844 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
845     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
846 #endif
847     AFS_ASSERT_GLOCK();
848     AFS_GUNLOCK();
849 #if     defined(AFS_HPUX102_ENV)
850 #if     defined(AFS_HPUX110_ENV)
851     /* we need a file associated with the socket so sosend in NetSend
852      * will not fail */
853     /* blocking socket */
854     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
855     fp = falloc();
856     if (!fp)
857         goto bad;
858     fp->f_flag = FREAD | FWRITE;
859     fp->f_type = DTYPE_SOCKET;
860     fp->f_ops = &socketops;
861
862     fp->f_data = (void *)newSocket;
863     newSocket->so_fp = (void *)fp;
864
865 #else /* AFS_HPUX110_ENV */
866     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
867 #endif /* else AFS_HPUX110_ENV */
868 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
869     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
870 #elif defined(AFS_FBSD_ENV)
871     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
872                     afs_osi_credp, curthread);
873 #elif defined(AFS_DARWIN80_ENV)
874     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
875 #elif defined(AFS_NBSD50_ENV)
876         code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
877 #elif defined(AFS_NBSD40_ENV)
878     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
879 #else
880     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
881 #endif /* AFS_HPUX102_ENV */
882     if (code)
883         goto bad;
884
885     memset(&myaddr, 0, sizeof myaddr);
886     myaddr.sin_family = AF_INET;
887     myaddr.sin_port = aport;
888     myaddr.sin_addr.s_addr = ahost;
889 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
890     myaddr.sin_len = sizeof(myaddr);
891 #endif
892
893 #ifdef AFS_HPUX110_ENV
894     bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
895     if (!bindnam) {
896         setuerror(ENOBUFS);
897         goto bad;
898     }
899     memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
900            addrsize);
901     bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
902 #if defined(AFS_NBSD40_ENV)
903     code = sobind(newSocket, bindnam, addrsize, osi_curproc());
904 #else
905     code = sobind(newSocket, bindnam, addrsize);
906 #endif
907     if (code) {
908         soclose(newSocket);
909 #if !defined(AFS_HPUX1122_ENV)
910         m_freem(nam);
911 #endif
912         goto bad;
913     }
914
915     freeb(bindnam);
916 #else /* AFS_HPUX110_ENV */
917 #if defined(AFS_DARWIN80_ENV)
918     {
919        int buflen = 50000;
920        int i,code2;
921        for (i=0;i<2;i++) {
922            code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
923                                   &buflen, sizeof(buflen));
924            code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
925                                   &buflen, sizeof(buflen));
926            if (!code && !code2)
927                break;
928            if (i == 2)
929               osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
930            buflen = 32766;
931        }
932     }
933 #else
934     code = soreserve(newSocket, 50000, 50000);
935     if (code) {
936         code = soreserve(newSocket, 32766, 32766);
937         if (code)
938             osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
939     }
940 #endif
941 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
942 #if defined(AFS_FBSD_ENV)
943     code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
944 #else
945     code = sobind(newSocket, (struct sockaddr *)&myaddr);
946 #endif
947     if (code) {
948         dpf(("sobind fails (%d)\n", (int)code));
949         soclose(newSocket);
950         goto bad;
951     }
952 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
953 #ifdef  AFS_OSF_ENV
954     nam = m_getclr(M_WAIT, MT_SONAME);
955 #else /* AFS_OSF_ENV */
956     nam = m_get(M_WAIT, MT_SONAME);
957 #endif
958     if (nam == NULL) {
959 #if defined(KERNEL_HAVE_UERROR)
960         setuerror(ENOBUFS);
961 #endif
962         goto bad;
963     }
964     nam->m_len = sizeof(myaddr);
965     memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
966 #if defined(AFS_SGI65_ENV)
967     BHV_PDATA(&bhv) = (void *)newSocket;
968     code = sobind(&bhv, nam);
969     m_freem(nam);
970 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
971     code = sobind(newSocket, nam, osi_curproc());
972 #else
973     code = sobind(newSocket, nam);
974 #endif
975     if (code) {
976         dpf(("sobind fails (%d)\n", (int)code));
977         soclose(newSocket);
978 #ifndef AFS_SGI65_ENV
979         m_freem(nam);
980 #endif
981         goto bad;
982     }
983 #endif /* else AFS_DARWIN_ENV */
984 #endif /* else AFS_HPUX110_ENV */
985
986     AFS_GLOCK();
987 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
988     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
989 #endif
990     return (osi_socket *)newSocket;
991
992   bad:
993     AFS_GLOCK();
994 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
995     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
996 #endif
997     return (osi_socket *)0;
998 }
999
1000 osi_socket *
1001 rxk_NewSocket(short aport)
1002 {
1003     return rxk_NewSocketHost(0, aport);
1004 }
1005
1006 /* free socket allocated by rxk_NewSocket */
1007 int
1008 rxk_FreeSocket(struct socket *asocket)
1009 {
1010     AFS_STATCNT(osi_FreeSocket);
1011 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1012     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1013 #endif
1014 #ifdef AFS_HPUX110_ENV
1015     if (asocket->so_fp) {
1016         struct file *fp = asocket->so_fp;
1017 #if !defined(AFS_HPUX1123_ENV)
1018         /* 11.23 still has falloc, but not FPENTRYFREE !
1019          * so for now if we shutdown, we will waist a file
1020          * structure */
1021         FPENTRYFREE(fp);
1022         asocket->so_fp = NULL;
1023 #endif
1024     }
1025 #endif /* AFS_HPUX110_ENV */
1026     soclose(asocket);
1027 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1028     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1029 #endif
1030     return 0;
1031 }
1032 #endif /* !SUN5 && !LINUX20 */
1033
1034 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1035 #ifdef AFS_DARWIN80_ENV
1036 /* Shutting down should wake us up, as should an earlier event. */
1037 void
1038 rxi_ReScheduleEvents(void)
1039 {
1040     /* needed to allow startup */
1041     int glock = ISAFS_GLOCK();
1042     if (!glock)
1043         AFS_GLOCK();
1044     osi_rxWakeup(&afs_termState);
1045     if (!glock)
1046         AFS_GUNLOCK();
1047 }
1048 #endif
1049 /*
1050  * Run RX event daemon every second (5 times faster than rest of systems)
1051  */
1052 void
1053 afs_rxevent_daemon(void)
1054 {
1055     struct clock temp;
1056     SPLVAR;
1057
1058     while (1) {
1059 #ifdef RX_ENABLE_LOCKS
1060         AFS_GUNLOCK();
1061 #endif /* RX_ENABLE_LOCKS */
1062         NETPRI;
1063         rxevent_RaiseEvents(&temp);
1064         USERPRI;
1065 #ifdef RX_ENABLE_LOCKS
1066         AFS_GLOCK();
1067 #endif /* RX_ENABLE_LOCKS */
1068 #ifdef RX_KERNEL_TRACE
1069         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1070                    "before afs_osi_Wait()");
1071 #endif
1072 #ifdef AFS_DARWIN80_ENV
1073         afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1074                                                      (temp.usec / 1000))), 0);
1075 #else
1076         afs_osi_Wait(500, NULL, 0);
1077 #endif
1078 #ifdef RX_KERNEL_TRACE
1079         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1080                    "after afs_osi_Wait()");
1081 #endif
1082         if (afs_termState == AFSOP_STOP_RXEVENT) {
1083 #ifdef RXK_LISTENER_ENV
1084             afs_termState = AFSOP_STOP_RXK_LISTENER;
1085 #else
1086 #ifdef AFS_SUN510_ENV
1087             afs_termState = AFSOP_STOP_NETIF;
1088 #else
1089             afs_termState = AFSOP_STOP_COMPLETE;
1090 #endif
1091 #endif
1092             osi_rxWakeup(&afs_termState);
1093             return;
1094         }
1095     }
1096 }
1097 #endif
1098
1099 #ifdef RXK_LISTENER_ENV
1100
1101 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1102 int
1103 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1104 {
1105     int code;
1106     struct sockaddr_in from;
1107     int nbytes;
1108     afs_int32 rlen;
1109     afs_int32 tlen;
1110     afs_int32 savelen;          /* was using rlen but had aliasing problems */
1111     rx_computelen(p, tlen);
1112     rx_SetDataSize(p, tlen);    /* this is the size of the user data area */
1113
1114     tlen += RX_HEADER_SIZE;     /* now this is the size of the entire packet */
1115     rlen = rx_maxJumboRecvSize; /* this is what I am advertising.  Only check
1116                                  * it once in order to avoid races.  */
1117     tlen = rlen - tlen;
1118     if (tlen > 0) {
1119         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1120         if (tlen > 0) {
1121             tlen = rlen - tlen;
1122         } else
1123             tlen = rlen;
1124     } else
1125         tlen = rlen;
1126
1127     /* add some padding to the last iovec, it's just to make sure that the
1128      * read doesn't return more data than we expect, and is done to get around
1129      * our problems caused by the lack of a length field in the rx header. */
1130     savelen = p->wirevec[p->niovecs - 1].iov_len;
1131     p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1132
1133     nbytes = tlen + sizeof(afs_int32);
1134 #ifdef RX_KERNEL_TRACE
1135     if (ICL_SETACTIVE(afs_iclSetp)) {
1136         AFS_GLOCK();
1137         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1138                    "before osi_NetRecive()");
1139         AFS_GUNLOCK();
1140     }
1141 #endif
1142     code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1143
1144 #ifdef RX_KERNEL_TRACE
1145     if (ICL_SETACTIVE(afs_iclSetp)) {
1146         AFS_GLOCK();
1147         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1148                    "after osi_NetRecive()");
1149         AFS_GUNLOCK();
1150     }
1151 #endif
1152     /* restore the vec to its correct state */
1153     p->wirevec[p->niovecs - 1].iov_len = savelen;
1154
1155     if (!code) {
1156         p->length = nbytes - RX_HEADER_SIZE;;
1157         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
1158             if (nbytes <= 0) {
1159                 if (rx_stats_active) {
1160                     MUTEX_ENTER(&rx_stats_mutex);
1161                     rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1162                     rx_stats.bogusHost = from.sin_addr.s_addr;
1163                     MUTEX_EXIT(&rx_stats_mutex);
1164                 }
1165                 dpf(("B: bogus packet from [%x,%d] nb=%d",
1166                      from.sin_addr.s_addr, from.sin_port, nbytes));
1167             }
1168             return -1;
1169         } else {
1170             /* Extract packet header. */
1171             rxi_DecodePacketHeader(p);
1172
1173             *host = from.sin_addr.s_addr;
1174             *port = from.sin_port;
1175             if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1176                 if (rx_stats_active) {
1177                     rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1178                 }
1179             }
1180
1181 #ifdef RX_TRIMDATABUFS
1182             /* Free any empty packet buffers at the end of this packet */
1183             rxi_TrimDataBufs(p, 1);
1184 #endif
1185             return 0;
1186         }
1187     } else
1188         return code;
1189 }
1190
1191 /* rxk_Listener()
1192  *
1193  * Listen for packets on socket. This thread is typically started after
1194  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1195  * the start state is set before proceeding.
1196  *
1197  * Note that this thread is outside the AFS global lock for much of
1198  * it's existence.
1199  *
1200  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1201  * we want here. So we need to either block all signals (including SIGKILL
1202  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1203  * OS's socket receive routine returns as a result of a signal.
1204  */
1205 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1206 #ifdef AFS_LINUX20_ENV
1207 struct task_struct *rxk_ListenerTask;
1208 #endif
1209
1210 #ifdef AFS_SUN5_ENV
1211 /*
1212  * Run the listener as a kernel thread.
1213  */
1214 void
1215 rxk_Listener(void)
1216 {
1217     extern id_t syscid;
1218     void rxk_ListenerProc(void);
1219     if (thread_create
1220         (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1221          minclsyspri) == NULL)
1222         osi_Panic("rxk_Listener: failed to start listener thread!\n");
1223 }
1224
1225 void
1226 rxk_ListenerProc(void)
1227 #else /* AFS_SUN5_ENV */
1228 void
1229 rxk_Listener(void)
1230 #endif                          /* AFS_SUN5_ENV */
1231 {
1232     struct rx_packet *rxp = NULL;
1233     int code;
1234     int host, port;
1235
1236 #ifdef AFS_LINUX20_ENV
1237     rxk_ListenerPid = current->pid;
1238     rxk_ListenerTask = current;
1239 #endif
1240 #ifdef AFS_SUN5_ENV
1241     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1242 #endif /* AFS_SUN5_ENV */
1243 #ifdef AFS_XBSD_ENV
1244     rxk_ListenerPid = curproc->p_pid;
1245 #endif /* AFS_FBSD_ENV */
1246 #ifdef AFS_DARWIN80_ENV
1247     rxk_ListenerPid = proc_selfpid();
1248 #elif defined(AFS_DARWIN_ENV)
1249     rxk_ListenerPid = current_proc()->p_pid;
1250 #endif
1251 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1252     AFS_GUNLOCK();
1253 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1254     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1255         /* See if a check for additional packets was issued */
1256         rx_CheckPackets();
1257
1258         if (rxp) {
1259             rxi_RestoreDataBufs(rxp);
1260         } else {
1261             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1262             if (!rxp)
1263                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1264         }
1265         if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1266             rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1267         }
1268     }
1269
1270 #ifdef RX_ENABLE_LOCKS
1271     AFS_GLOCK();
1272 #endif /* RX_ENABLE_LOCKS */
1273     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1274 #ifdef AFS_SUN510_ENV
1275         afs_termState = AFSOP_STOP_NETIF;
1276 #else
1277         afs_termState = AFSOP_STOP_COMPLETE;
1278 #endif
1279         osi_rxWakeup(&afs_termState);
1280     }
1281     rxk_ListenerPid = 0;
1282 #ifdef AFS_LINUX20_ENV
1283     rxk_ListenerTask = 0;
1284     osi_rxWakeup(&rxk_ListenerTask);
1285 #endif
1286 #if defined(AFS_SUN5_ENV)
1287     osi_rxWakeup(&rxk_ListenerPid);
1288 #endif
1289 #ifdef AFS_SUN5_ENV
1290     AFS_GUNLOCK();
1291 #endif /* AFS_SUN5_ENV */
1292 }
1293
1294 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1295 /* The manner of stopping the rx listener thread may vary. Most unix's should
1296  * be able to call soclose.
1297  */
1298 void
1299 osi_StopListener(void)
1300 {
1301     soclose(rx_socket);
1302 }
1303 #endif
1304 #endif /* RXK_LISTENER_ENV */
1305 #endif /* !NCR && !UKERNEL */
1306
1307 #if !defined(AFS_LINUX26_ENV)
1308 void
1309 #if defined(AFS_AIX_ENV)
1310 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1311 #else
1312 osi_Panic(char *msg, ...)
1313 #endif
1314 {
1315 #ifdef AFS_AIX_ENV
1316     if (!msg)
1317         msg = "Unknown AFS panic";
1318     /*
1319      * we should probably use the errsave facility here. it is not
1320      * varargs-aware
1321      */
1322
1323     printf(msg, a1, a2, a3);
1324     panic(msg);
1325 #elif defined(AFS_SGI_ENV)
1326     va_list ap;
1327
1328     /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1329     if (!msg) {
1330         cmn_err(CE_PANIC, "Unknown AFS panic");
1331     } else {
1332         va_start(ap, msg);
1333         icmn_err(CE_PANIC, msg, ap);
1334         va_end(ap);
1335     }
1336 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1337     char buf[256];
1338     va_list ap;
1339     if (!msg)
1340         msg = "Unknown AFS panic";
1341
1342     va_start(ap, msg);
1343     vsnprintf(buf, sizeof(buf), msg, ap);
1344     va_end(ap);
1345     printf("%s", buf);
1346     panic(buf);
1347 #else
1348     va_list ap;
1349     if (!msg)
1350         msg = "Unknown AFS panic";
1351
1352     va_start(ap, msg);
1353     vprintf(msg, ap);
1354     va_end(ap);
1355 # ifdef AFS_LINUX20_ENV
1356     * ((char *) 0) = 0;
1357 # else
1358     panic(msg);
1359 # endif
1360 #endif
1361 }
1362 #endif