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
11 * rx_kcommon.c - Common kernel RX code for all system types.
14 #include <afsconfig.h>
15 #include "../afs/param.h"
19 #include "../rx/rx_kcommon.h"
21 #ifdef AFS_HPUX110_ENV
22 #include "../h/tihdr.h"
25 #include "../afsint/afsint.h"
27 #ifdef AFS_DARWIN60_ENV
28 struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct ifaddr **pifad);
30 struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct in_ifaddr **pifad);
33 #ifndef RXK_LISTENER_ENV
34 int (*rxk_PacketArrivalProc)(register struct rx_packet *ahandle,
35 register struct sockaddr_in *afrom, char *arock,
36 afs_int32 asize); /* set to packet allocation procedure */
37 int (*rxk_GetPacketProc)(char **ahandle, int asize);
40 extern struct interfaceAddr afs_cb_interface;
42 rxk_ports_t rxk_ports;
43 rxk_portRocks_t rxk_portRocks;
47 /* add a port to the monitored list, port # is in network order */
48 static int rxk_AddPort(u_short aport, char * arock)
51 unsigned short *tsp, ts;
54 zslot = -1; /* look for an empty slot simultaneously */
55 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
56 if (((ts = *tsp) == 0) && (zslot == -1))
62 /* otherwise allocate a new port slot */
63 if (zslot < 0) return E2BIG; /* all full */
64 rxk_ports[zslot] = aport;
65 rxk_portRocks[zslot] = arock;
69 /* remove as port from the monitored list, port # is in network order */
70 int rxk_DelPort(u_short aport)
73 register unsigned short *tsp;
75 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
77 /* found it, adjust ref count and free the port reference if all gone */
82 /* otherwise port not found */
86 void rxk_shutdownPorts(void)
89 for (i=0; i<MAXRXPORTS;i++) {
92 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
93 soclose((struct socket *)rxk_portRocks[i]);
95 rxk_portRocks[i] = NULL;
100 osi_socket rxi_GetUDPSocket(u_short port)
102 struct osi_socket *sockp;
103 sockp = (struct osi_socket *) rxk_NewSocket(port);
104 if (sockp == (struct osi_socket *) 0) return OSI_NULLSOCKET;
105 rxk_AddPort(port, (char *) sockp);
106 return (osi_socket)sockp;
110 void osi_Panic(msg, a1, a2, a3)
114 msg = "Unknown AFS panic";
116 printf(msg, a1, a2, a3);
117 #ifdef AFS_LINUX20_ENV
118 *((char*)0xffffffff) = 42;
125 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
126 * unsigned long value into the given buffer. Returns 0 on success,
127 * and a value less than 0 on failure. The contents of the buffer is
128 * defined only on success.
131 int osi_utoa(char *buf, size_t len, unsigned long val)
133 long k; /* index of first byte of string value */
135 /* we definitely need room for at least one digit and NUL */
141 /* compute the string form from the high end of the buffer */
144 for (k = len - 2; k >= 0; k--) {
145 buf[k] = val % 10 + '0';
152 /* did we finish converting val to string form? */
158 /* this should never happen */
164 /* this should never happen */
170 /* if necessary, relocate string to beginning of buf[] */
175 * We need to achieve the effect of calling
177 * memmove(buf, &buf[k], len - k);
179 * However, since memmove() is not available in all
180 * kernels, we explicitly do an appropriate copy.
186 while((*dst++ = *src++) != '\0')
194 * osi_AssertFailK() -- used by the osi_Assert() macro.
196 * It essentially does
198 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
200 * Since the kernel version of osi_Panic() only passes its first
201 * argument to the native panic(), we construct a single string and hand
202 * that to osi_Panic().
204 void osi_AssertFailK(const char *expr, const char *file, int line)
206 static const char msg0[] = "assertion failed: ";
207 static const char msg1[] = ", file: ";
208 static const char msg2[] = ", line: ";
209 static const char msg3[] = "\n";
212 * These buffers add up to 1K, which is a pleasantly nice round
213 * value, but probably not vital.
218 /* check line number conversion */
220 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
221 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
226 #define ADDBUF(BUF, STR) \
227 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
228 strcat(BUF, (char *)(STR)); \
237 ADDBUF(buf, linebuf);
246 /* This is the server process request loop. Kernel server
247 * processes never become listener threads */
248 void rx_ServerProc(void)
252 rxi_MorePackets(rx_maxReceiveWindow+2); /* alloc more packets */
253 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
254 /* threadID is used for making decisions in GetCall. Get it by bumping
255 * number of threads handling incoming calls */
256 threadID = rxi_availProcs++;
258 #ifdef RX_ENABLE_LOCKS
260 #endif /* RX_ENABLE_LOCKS */
261 rxi_ServerProc(threadID, NULL, NULL);
262 #ifdef RX_ENABLE_LOCKS
264 #endif /* RX_ENABLE_LOCKS */
266 #endif /* !UKERNEL */
268 #ifndef RXK_LISTENER_ENV
269 /* asize includes the Rx header */
270 static int MyPacketProc(char **ahandle, int asize)
272 register struct rx_packet *tp;
274 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
275 /* If we can't scrounge enough cbufs, then we have to drop the packet,
276 * but we should set a flag so we magic up some more at our leisure.
279 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
280 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
281 if (tp && (tp->length + RX_HEADER_SIZE) < asize ) {
282 if (0 < rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
283 RX_PACKET_CLASS_RECV_CBUF)) {
286 MUTEX_ENTER(&rx_stats_mutex);
287 rx_stats.noPacketBuffersOnRead++;
288 MUTEX_EXIT(&rx_stats_mutex);
293 * XXX if packet is too long for our buffer,
294 * should do this at a higher layer and let other
295 * end know we're losing.
297 MUTEX_ENTER(&rx_stats_mutex);
298 rx_stats.bogusPacketOnRead++;
299 MUTEX_EXIT(&rx_stats_mutex);
300 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
301 printf("rx: packet dropped: bad ulen=%d\n", asize);
306 /* otherwise we have a packet, set appropriate values */
307 *ahandle = (char *) tp;
311 static int MyArrivalProc(register struct rx_packet *ahandle,
312 register struct sockaddr_in *afrom, char *arock,
315 /* handle basic rx packet */
316 ahandle->length = asize - RX_HEADER_SIZE;
317 rxi_DecodePacketHeader(ahandle);
318 ahandle = rxi_ReceivePacket(ahandle, (struct socket *) arock,
319 afrom->sin_addr.s_addr, afrom->sin_port,
322 /* free the packet if it has been returned */
323 if (ahandle) rxi_FreePacket(ahandle);
326 #endif /* !RXK_LISTENER_ENV */
328 void rxi_StartListener(void)
330 /* if kernel, give name of appropriate procedures */
331 #ifndef RXK_LISTENER_ENV
332 rxk_GetPacketProc = MyPacketProc;
333 rxk_PacketArrivalProc = MyArrivalProc;
338 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
339 to get interesting information. */
340 void rxi_InitPeerParams(register struct rx_peer *pp)
347 #ifdef AFS_USERSPACE_IP_ADDR
348 i = rxi_Findcbi(pp->host);
351 /* pp->timeout.usec = 0; */
352 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
355 /* pp->timeout.usec = 0; */
356 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
359 mtu = ntohl(afs_cb_interface.mtu[i]);
360 /* Diminish the packet size to one based on the MTU given by
362 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
363 rxmtu = mtu - RX_IPUDP_SIZE;
364 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
367 else { /* couldn't find the interface, so assume the worst */
368 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
370 #else /* AFS_USERSPACE_IP_ADDR */
371 #ifdef AFS_DARWIN60_ENV
372 struct ifaddr *ifad = (struct ifaddr *) 0;
374 struct in_ifaddr *ifad = (struct in_ifaddr *) 0;
378 /* At some time we need to iterate through rxi_FindIfnet() to find the
381 ifn = rxi_FindIfnet(pp->host, &ifad);
382 if (ifn == NULL) { /* not local */
384 /* pp->timeout.usec = 0; */
385 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
388 /* pp->timeout.usec = 0; */
389 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
392 #ifdef IFF_POINTOPOINT
393 if (ifn->if_flags & IFF_POINTOPOINT) {
394 /* wish we knew the bit rate and the chunk size, sigh. */
396 pp->ifMTU = RX_PP_PACKET_SIZE;
398 #endif /* IFF_POINTOPOINT */
399 /* Diminish the packet size to one based on the MTU given by
401 if (ifn->if_mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
402 rxmtu = ifn->if_mtu - RX_IPUDP_SIZE;
403 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
406 else { /* couldn't find the interface, so assume the worst */
407 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
409 #endif/* else AFS_USERSPACE_IP_ADDR */
410 #else /* AFS_SUN5_ENV */
411 mtu = rxi_FindIfMTU(pp->host);
415 /* pp->timeout.usec = 0; */
416 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
419 /* pp->timeout.usec = 0; */
420 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
424 /* Diminish the packet size to one based on the MTU given by
426 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
427 rxmtu = mtu - RX_IPUDP_SIZE;
428 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
430 } else { /* couldn't find the interface, so assume the worst */
431 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
433 #endif /* AFS_SUN5_ENV */
434 #else /* ADAPT_MTU */
435 pp->rateFlag = 2; /* start timing after two full packets */
437 pp->ifMTU = OLD_MAX_PACKET_SIZE;
438 #endif /* else ADAPT_MTU */
439 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
440 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
441 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
442 pp->ifDgramPackets = MIN(rxi_nDgramPackets,
443 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
444 pp->maxDgramPackets = 1;
446 /* Initialize slow start parameters */
447 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
449 pp->nDgramPackets = 1;
454 /* The following code is common to several system types, but not all. The
455 * separate ones are found in the system specific subdirectories.
459 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
460 /* Routine called during the afsd "-shutdown" process to put things back to
463 static struct protosw parent_proto; /* udp proto switch */
465 void shutdown_rxkernel(void)
467 register struct protosw *tpro, *last;
468 last = inetdomain.dom_protoswNPROTOSW;
469 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
470 if (tpro->pr_protocol == IPPROTO_UDP) {
471 /* restore original udp protocol switch */
472 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
473 memset((void *)&parent_proto, 0, sizeof(parent_proto));
478 printf("shutdown_rxkernel: no udp proto");
480 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
482 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
483 /* Determine what the network interfaces are for this machine. */
485 #define ADDRSPERSITE 16
486 static afs_uint32 myNetAddrs[ADDRSPERSITE];
487 static int myNetMTUs[ADDRSPERSITE];
488 static int numMyNetAddrs = 0;
490 #ifdef AFS_USERSPACE_IP_ADDR
491 int rxi_GetcbiInfo(void)
493 int i, j, different = 0;
496 afs_uint32 addrs[ADDRSPERSITE];
497 int mtus[ADDRSPERSITE];
499 memset((void *)addrs, 0, sizeof(addrs));
500 memset((void *)mtus, 0, sizeof(mtus));
502 for (i=0; i<afs_cb_interface.numberOfInterfaces; i++) {
503 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
504 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
505 if (myNetAddrs[i] != ifinaddr) different++;
508 rxmtu = rxi_AdjustIfMTU(rxmtu);
509 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
510 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
511 addrs[i++] = ifinaddr;
512 if ( ( ifinaddr != 0x7f000001 ) && (maxmtu > rx_maxReceiveSize) ) {
513 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
514 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
518 rx_maxJumboRecvSize = RX_HEADER_SIZE +
519 ( rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
520 ((rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE);
521 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
524 for (j=0; j<i; j++) {
525 myNetMTUs[j] = mtus[j];
526 myNetAddrs[j] = addrs[j];
533 /* Returns the afs_cb_interface inxex which best matches address.
534 * If none is found, we return -1.
536 afs_int32 rxi_Findcbi(afs_uint32 addr)
539 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
540 afs_int32 rvalue = -1;
543 if (numMyNetAddrs == 0)
544 (void) rxi_GetcbiInfo();
546 myAddr = ntohl(addr);
548 if ( IN_CLASSA(myAddr) ) netMask = IN_CLASSA_NET;
549 else if ( IN_CLASSB(myAddr) ) netMask = IN_CLASSB_NET;
550 else if ( IN_CLASSC(myAddr) ) netMask = IN_CLASSC_NET;
553 for (j=0; j<afs_cb_interface.numberOfInterfaces; j++) {
554 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
555 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
556 if ((myAddr & netMask) == (thisAddr & netMask)) {
557 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
558 if (myAddr == thisAddr) {
563 if (match_value < 3) {
568 if (match_value < 2) {
579 #else /* AFS_USERSPACE_IP_ADDR */
581 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
582 #define IFADDR2SA(f) (&((f)->ifa_addr))
583 #else /* AFS_AIX41_ENV */
584 #define IFADDR2SA(f) ((f)->ifa_addr)
587 int rxi_GetIFInfo(void)
592 register struct ifnet *ifn;
593 register int rxmtu, maxmtu;
594 afs_uint32 addrs[ADDRSPERSITE];
595 int mtus[ADDRSPERSITE];
596 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
599 memset(addrs, 0, sizeof(addrs));
600 memset(mtus, 0, sizeof(mtus));
602 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
603 TAILQ_FOREACH(ifn, &ifnet, if_link) {
604 if (i >= ADDRSPERSITE) break;
606 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
608 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
609 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
610 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
611 if (i >= ADDRSPERSITE) break;
613 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
614 ifad = ifad->ifa_next){
616 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
617 ifinaddr = ntohl(((struct sockaddr_in *) IFADDR2SA(ifad))->sin_addr.s_addr);
618 if (myNetAddrs[i] != ifinaddr) {
622 rxmtu = rxi_AdjustIfMTU(rxmtu);
623 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
624 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
625 addrs[i++] = ifinaddr;
626 if ( ( ifinaddr != 0x7f000001 ) &&
627 (maxmtu > rx_maxReceiveSize) ) {
628 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
629 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
635 rx_maxJumboRecvSize = RX_HEADER_SIZE
636 + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE
637 + (rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE;
638 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
642 for (j=0; j< i; j++) {
643 myNetMTUs[j] = mtus[j];
644 myNetAddrs[j] = addrs[j];
649 #ifdef AFS_DARWIN60_ENV
650 /* Returns ifnet which best matches address */
652 rxi_FindIfnet(addr, pifad)
654 struct ifaddr **pifad;
656 struct sockaddr_in s;
658 if (numMyNetAddrs == 0)
659 (void) rxi_GetIFInfo();
661 s.sin_family=AF_INET;
662 s.sin_addr.s_addr=addr;
663 *pifad=ifa_ifwithnet((struct sockaddr *)&s);
665 return (*pifad ? (*pifad)->ifa_ifp : NULL );
668 /* Returns ifnet which best matches address */
669 struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct in_ifaddr **pifad)
673 extern struct in_ifaddr *in_ifaddr;
674 struct in_ifaddr *ifa;
675 struct sockaddr_in *sin;
677 if (numMyNetAddrs == 0)
678 (void) rxi_GetIFInfo();
680 ppaddr = ntohl(addr);
682 /* if we're given an address, skip everything until we find it */
684 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
685 *pifad = TAILQ_FIRST(&in_ifaddrhead);
690 if (((ppaddr & (*pifad)->ia_subnetmask) == (*pifad)->ia_subnet))
691 match_value = 2; /* don't find matching nets, just subnets */
692 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
693 *pifad = TAILQ_NEXT(*pifad, ia_link);
695 *pifad = (*pifad)->ia_next;
699 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
700 for (ifa = *pifad; ifa; ifa = TAILQ_NEXT(ifa, ia_link) ) {
702 for (ifa = *pifad; ifa; ifa = ifa->ia_next ) {
704 if ((ppaddr & ifa->ia_netmask) == ifa->ia_net) {
705 if ((ppaddr & ifa->ia_subnetmask) == ifa->ia_subnet) {
707 if ( sin->sin_addr.s_addr == ppaddr) { /* ie, ME!!! */
712 if (match_value < 3) {
718 if (match_value < 2) {
723 } /* if net matches */
724 } /* for all in_ifaddrs */
727 return (*pifad ? (*pifad)->ia_ifp : NULL );
730 #endif /* else AFS_USERSPACE_IP_ADDR */
731 #endif /* !SUN5 && !SGI62 */
734 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
735 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
736 * system specific subdirectories for all systems. But for the moment,
737 * most of it is simple to follow common code.
739 #if !defined(UKERNEL)
740 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
741 /* rxk_NewSocket creates a new socket on the specified port. The port is
742 * in network byte order.
744 struct osi_socket *rxk_NewSocket(short aport)
746 register afs_int32 code;
747 struct socket *newSocket;
748 register struct mbuf *nam;
749 struct sockaddr_in myaddr;
751 #ifdef AFS_HPUX110_ENV
752 /* prototype copied from kernel source file streams/str_proto.h */
753 extern MBLKP allocb_wait(int, int);
755 int addrsize = sizeof(struct sockaddr_in);
761 AFS_STATCNT(osi_NewSocket);
762 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
763 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
765 #if defined(AFS_HPUX102_ENV)
766 #if defined(AFS_HPUX110_ENV)
767 /* blocking socket */
768 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
769 #else /* AFS_HPUX110_ENV */
770 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
771 #endif /* else AFS_HPUX110_ENV */
774 code = socreate(AF_INET, &newSocket, SOCK_DGRAM,IPPROTO_UDP);
775 #elif defined(AFS_XBSD_ENV)
776 code = socreate(AF_INET, &newSocket, SOCK_DGRAM,IPPROTO_UDP, curproc);
778 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
779 #endif /* AFS_SGI65_ENV */
780 #endif /* AFS_HPUX102_ENV */
783 myaddr.sin_family = AF_INET;
784 myaddr.sin_port = aport;
785 myaddr.sin_addr.s_addr = 0;
786 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
787 myaddr.sin_len = sizeof(myaddr);
790 #ifdef AFS_HPUX110_ENV
791 bindnam = allocb_wait((addrsize+SO_MSGOFFSET+1), BPRI_MED);
796 memcpy((caddr_t)bindnam->b_rptr+SO_MSGOFFSET, (caddr_t)&myaddr, addrsize);
797 bindnam->b_wptr = bindnam->b_rptr + (addrsize+SO_MSGOFFSET+1);
799 code = sobind(newSocket, bindnam, addrsize);
807 #else /* AFS_HPUX110_ENV */
808 code = soreserve(newSocket, 50000, 50000);
810 code = soreserve(newSocket, 32766, 32766);
812 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
814 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
815 #if defined(AFS_XBSD_ENV)
816 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
818 code = sobind(newSocket, (struct sockaddr *)&myaddr);
821 printf("sobind fails\n");
827 nam = m_getclr(M_WAIT, MT_SONAME);
828 #else /* AFS_OSF_ENV */
829 nam = m_get(M_WAIT, MT_SONAME);
832 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_SGI64_ENV) && !defined(AFS_XBSD_ENV)
837 nam->m_len = sizeof(myaddr);
839 myaddr.sin_len = nam->m_len;
840 #endif /* AFS_OSF_ENV */
841 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
843 BHV_PDATA(&bhv) = (void*)newSocket;
844 code = sobind(&bhv, nam);
846 #elif defined(AFS_XBSD_ENV)
847 code = sobind(newSocket, nam, curproc);
849 code = sobind(newSocket, nam);
853 #ifndef AFS_SGI65_ENV
858 #endif /* else AFS_DARWIN_ENV */
859 #endif /* else AFS_HPUX110_ENV */
861 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
862 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
864 return (struct osi_socket *) newSocket;
867 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
868 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
870 return (struct osi_socket *) 0;
874 /* free socket allocated by rxk_NewSocket */
875 int rxk_FreeSocket(register struct socket *asocket)
877 AFS_STATCNT(osi_FreeSocket);
878 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
879 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
882 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
883 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
887 #endif /* !SUN5 && !LINUX20 */
889 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
891 * Run RX event daemon every second (5 times faster than rest of systems)
893 void afs_rxevent_daemon(void)
899 #ifdef RX_ENABLE_LOCKS
901 #endif /* RX_ENABLE_LOCKS */
904 rxevent_RaiseEvents(&temp);
907 #ifdef RX_ENABLE_LOCKS
909 #endif /* RX_ENABLE_LOCKS */
910 afs_osi_Wait(500, NULL, 0);
911 if (afs_termState == AFSOP_STOP_RXEVENT )
913 #ifdef RXK_LISTENER_ENV
914 afs_termState = AFSOP_STOP_RXK_LISTENER;
916 afs_termState = AFSOP_STOP_COMPLETE;
918 afs_osi_Wakeup(&afs_termState);
925 #ifdef RXK_LISTENER_ENV
927 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
928 int rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
931 struct sockaddr_in from;
934 register afs_int32 tlen;
935 afs_int32 savelen; /* was using rlen but had aliasing problems */
936 rx_computelen(p, tlen);
937 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
939 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
940 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
941 * it once in order to avoid races. */
944 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
952 /* add some padding to the last iovec, it's just to make sure that the
953 * read doesn't return more data than we expect, and is done to get around
954 * our problems caused by the lack of a length field in the rx header. */
955 savelen = p->wirevec[p->niovecs-1].iov_len;
956 p->wirevec[p->niovecs-1].iov_len = savelen + RX_EXTRABUFFERSIZE;
958 nbytes = tlen + sizeof(afs_int32);
959 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs,
962 /* restore the vec to its correct state */
963 p->wirevec[p->niovecs-1].iov_len = savelen;
966 p->length = nbytes - RX_HEADER_SIZE;;
967 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
969 rxi_MorePackets(rx_initSendWindow);
971 MUTEX_ENTER(&rx_stats_mutex);
972 rx_stats.bogusPacketOnRead++;
973 rx_stats.bogusHost = from.sin_addr.s_addr;
974 MUTEX_EXIT(&rx_stats_mutex);
975 dpf(("B: bogus packet from [%x,%d] nb=%d", from.sin_addr.s_addr,
976 from.sin_port,nbytes));
981 /* Extract packet header. */
982 rxi_DecodePacketHeader(p);
984 *host = from.sin_addr.s_addr;
985 *port = from.sin_port;
986 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
987 MUTEX_ENTER(&rx_stats_mutex);
988 rx_stats.packetsRead[p->header.type-1]++;
989 MUTEX_EXIT(&rx_stats_mutex);
992 /* Free any empty packet buffers at the end of this packet */
993 rxi_TrimDataBufs(p, 1);
1004 * Listen for packets on socket. This thread is typically started after
1005 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1006 * the start state is set before proceeding.
1008 * Note that this thread is outside the AFS global lock for much of
1011 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1012 * we want here. So we need to either block all signals (including SIGKILL
1013 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1014 * OS's socket receive routine returns as a result of a signal.
1016 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1020 * Run the listener as a kernel thread.
1022 void rxk_Listener(void)
1025 void rxk_ListenerProc(void);
1026 if (thread_create(NULL, DEFAULTSTKSZ, rxk_ListenerProc,
1027 0, 0, &p0, TS_RUN, minclsyspri) == NULL)
1028 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1031 void rxk_ListenerProc(void)
1032 #else /* AFS_SUN5_ENV */
1033 void rxk_Listener(void)
1034 #endif /* AFS_SUN5_ENV */
1036 struct rx_packet *rxp = NULL;
1040 #ifdef AFS_LINUX20_ENV
1041 rxk_ListenerPid = current->pid;
1044 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1045 #endif /* AFS_SUN5_ENV */
1047 rxk_ListenerPid = curproc->p_pid;
1048 #endif /* AFS_FBSD_ENV */
1049 #if defined(AFS_DARWIN_ENV)
1050 rxk_ListenerPid = current_proc()->p_pid;
1052 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1054 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1056 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1058 rxi_RestoreDataBufs(rxp);
1061 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1063 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1065 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1067 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1072 #ifdef RX_ENABLE_LOCKS
1074 #endif /* RX_ENABLE_LOCKS */
1075 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1076 afs_termState = AFSOP_STOP_COMPLETE;
1077 afs_osi_Wakeup(&afs_termState);
1079 rxk_ListenerPid = 0;
1080 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1081 afs_osi_Wakeup(&rxk_ListenerPid);
1085 #endif /* AFS_SUN5_ENV */
1088 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1089 /* The manner of stopping the rx listener thread may vary. Most unix's should
1090 * be able to call soclose.
1092 void osi_StopListener(void)
1097 #endif /* RXK_LISTENER_ENV */
1099 #endif /* !NCR && !UKERNEL */