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"
24 #include "../afs/hpux_110.h"
26 #include "../afsint/afsint.h"
28 afs_int32 rxi_Findcbi();
29 extern struct interfaceAddr afs_cb_interface;
31 #ifndef RXK_LISTENER_ENV
32 int (*rxk_GetPacketProc)(); /* set to packet allocation procedure */
33 int (*rxk_PacketArrivalProc)();
36 rxk_ports_t rxk_ports;
37 rxk_portRocks_t rxk_portRocks;
41 /* add a port to the monitored list, port # is in network order */
42 static int rxk_AddPort(u_short aport, char * arock)
45 unsigned short *tsp, ts;
48 zslot = -1; /* look for an empty slot simultaneously */
49 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
50 if (((ts = *tsp) == 0) && (zslot == -1))
56 /* otherwise allocate a new port slot */
57 if (zslot < 0) return E2BIG; /* all full */
58 rxk_ports[zslot] = aport;
59 rxk_portRocks[zslot] = arock;
63 /* remove as port from the monitored list, port # is in network order */
67 register unsigned short *tsp;
69 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
71 /* found it, adjust ref count and free the port reference if all gone */
76 /* otherwise port not found */
80 void rxk_shutdownPorts(void)
83 for (i=0; i<MAXRXPORTS;i++) {
86 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
87 soclose((struct socket *)rxk_portRocks[i]);
89 rxk_portRocks[i] = (char *)0;
94 osi_socket rxi_GetUDPSocket(port)
97 struct osi_socket *sockp;
98 sockp = (struct osi_socket *) rxk_NewSocket(port);
99 if (sockp == (struct osi_socket *) 0) return OSI_NULLSOCKET;
100 rxk_AddPort(port, (char *) sockp);
101 return (osi_socket)sockp;
105 void osi_Panic(msg, a1, a2, a3)
109 msg = "Unknown AFS panic";
111 printf(msg, a1, a2, a3);
112 #ifdef AFS_LINUX20_ENV
113 *((char*)0xffffffff) = 42;
120 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
121 * unsigned long value into the given buffer. Returns 0 on success,
122 * and a value less than 0 on failure. The contents of the buffer is
123 * defined only on success.
127 osi_utoa(char *buf, size_t len, unsigned long val)
129 long k; /* index of first byte of string value */
131 /* we definitely need room for at least one digit and NUL */
137 /* compute the string form from the high end of the buffer */
140 for (k = len - 2; k >= 0; k--) {
141 buf[k] = val % 10 + '0';
148 /* did we finish converting val to string form? */
154 /* this should never happen */
160 /* this should never happen */
166 /* if necessary, relocate string to beginning of buf[] */
171 * We need to achieve the effect of calling
173 * memmove(buf, &buf[k], len - k);
175 * However, since memmove() is not available in all
176 * kernels, we explicitly do an appropriate copy.
182 while((*dst++ = *src++) != '\0')
190 * osi_AssertFailK() -- used by the osi_Assert() macro.
192 * It essentially does
194 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
196 * Since the kernel version of osi_Panic() only passes its first
197 * argument to the native panic(), we construct a single string and hand
198 * that to osi_Panic().
201 osi_AssertFailK(const char *expr, const char *file, int line)
203 static const char msg0[] = "assertion failed: ";
204 static const char msg1[] = ", file: ";
205 static const char msg2[] = ", line: ";
206 static const char msg3[] = "\n";
209 * These buffers add up to 1K, which is a pleasantly nice round
210 * value, but probably not vital.
215 /* check line number conversion */
217 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
218 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
223 #define ADDBUF(BUF, STR) \
224 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
225 strcat(BUF, (char *)(STR)); \
234 ADDBUF(buf, linebuf);
243 /* This is the server process request loop. Kernel server
244 * processes never become listener threads */
249 rxi_MorePackets(rx_maxReceiveWindow+2); /* alloc more packets */
250 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
251 /* threadID is used for making decisions in GetCall. Get it by bumping
252 * number of threads handling incoming calls */
253 threadID = rxi_availProcs++;
255 #ifdef RX_ENABLE_LOCKS
257 #endif /* RX_ENABLE_LOCKS */
258 rxi_ServerProc(threadID, NULL, NULL);
259 #ifdef RX_ENABLE_LOCKS
261 #endif /* RX_ENABLE_LOCKS */
263 #endif /* !UKERNEL */
265 #ifndef RXK_LISTENER_ENV
266 static int MyPacketProc(ahandle, asize)
267 int asize; /* this includes the Rx header */
270 register struct rx_packet *tp;
272 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
273 /* If we can't scrounge enough cbufs, then we have to drop the packet,
274 * but we should set a flag so we magic up some more at our leisure.
277 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
278 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
279 if (tp && (tp->length + RX_HEADER_SIZE) < asize ) {
280 if (0 < rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
281 RX_PACKET_CLASS_RECV_CBUF)) {
284 MUTEX_ENTER(&rx_stats_mutex);
285 rx_stats.noPacketBuffersOnRead++;
286 MUTEX_EXIT(&rx_stats_mutex);
291 * XXX if packet is too long for our buffer,
292 * should do this at a higher layer and let other
293 * end know we're losing.
295 MUTEX_ENTER(&rx_stats_mutex);
296 rx_stats.bogusPacketOnRead++;
297 MUTEX_EXIT(&rx_stats_mutex);
298 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
299 printf("rx: packet dropped: bad ulen=%d\n", asize);
304 /* otherwise we have a packet, set appropriate values */
305 *ahandle = (char *) tp;
309 static int MyArrivalProc(ahandle, afrom, arock, asize)
310 register struct rx_packet *ahandle;
311 register struct sockaddr_in *afrom;
314 /* handle basic rx packet */
315 ahandle->length = asize - RX_HEADER_SIZE;
316 rxi_DecodePacketHeader(ahandle);
317 ahandle = rxi_ReceivePacket(ahandle, (struct socket *) arock,
318 afrom->sin_addr.s_addr, afrom->sin_port,
321 /* free the packet if it has been returned */
322 if (ahandle) rxi_FreePacket(ahandle);
325 #endif /* !RXK_LISTENER_ENV */
328 rxi_StartListener() {
329 /* if kernel, give name of appropriate procedures */
330 #ifndef RXK_LISTENER_ENV
331 rxk_GetPacketProc = MyPacketProc;
332 rxk_PacketArrivalProc = MyArrivalProc;
337 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
338 to get interesting information. */
339 void rxi_InitPeerParams(pp)
340 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 struct in_ifaddr *ifad = (struct in_ifaddr *) 0;
374 /* At some time we need to iterate through rxi_FindIfnet() to find the
377 ifn = rxi_FindIfnet(pp->host, &ifad);
378 if (ifn == NULL) { /* not local */
380 /* pp->timeout.usec = 0; */
381 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
384 /* pp->timeout.usec = 0; */
385 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
388 #ifdef IFF_POINTOPOINT
389 if (ifn->if_flags & IFF_POINTOPOINT) {
390 /* wish we knew the bit rate and the chunk size, sigh. */
392 pp->ifMTU = RX_PP_PACKET_SIZE;
394 #endif /* IFF_POINTOPOINT */
395 /* Diminish the packet size to one based on the MTU given by
397 if (ifn->if_mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
398 rxmtu = ifn->if_mtu - RX_IPUDP_SIZE;
399 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
402 else { /* couldn't find the interface, so assume the worst */
403 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
405 #endif/* else AFS_USERSPACE_IP_ADDR */
406 #else /* AFS_SUN5_ENV */
407 mtu = rxi_FindIfMTU(pp->host);
411 /* pp->timeout.usec = 0; */
412 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
415 /* pp->timeout.usec = 0; */
416 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
420 /* Diminish the packet size to one based on the MTU given by
422 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
423 rxmtu = mtu - RX_IPUDP_SIZE;
424 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
426 } else { /* couldn't find the interface, so assume the worst */
427 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
429 #endif /* AFS_SUN5_ENV */
430 #else /* ADAPT_MTU */
431 pp->rateFlag = 2; /* start timing after two full packets */
433 pp->ifMTU = OLD_MAX_PACKET_SIZE;
434 #endif /* else ADAPT_MTU */
435 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
436 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
437 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
438 pp->ifDgramPackets = MIN(rxi_nDgramPackets,
439 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
440 pp->maxDgramPackets = 1;
442 /* Initialize slow start parameters */
443 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
445 pp->nDgramPackets = 1;
450 /* The following code is common to several system types, but not all. The
451 * separate ones are found in the system specific subdirectories.
455 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
456 /* Routine called during the afsd "-shutdown" process to put things back to
459 static struct protosw parent_proto; /* udp proto switch */
461 void shutdown_rxkernel(void)
463 register struct protosw *tpro, *last;
464 last = inetdomain.dom_protoswNPROTOSW;
465 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
466 if (tpro->pr_protocol == IPPROTO_UDP) {
467 /* restore original udp protocol switch */
468 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
469 memset((void *)&parent_proto, 0, sizeof(parent_proto));
474 printf("shutdown_rxkernel: no udp proto");
476 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
478 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
479 /* Determine what the network interfaces are for this machine. */
481 #define ADDRSPERSITE 16
482 static afs_uint32 myNetAddrs[ADDRSPERSITE];
483 static int myNetMTUs[ADDRSPERSITE];
484 static int myNetFlags[ADDRSPERSITE];
485 static int numMyNetAddrs = 0;
487 #ifdef AFS_USERSPACE_IP_ADDR
490 int i, j, different = 0;
493 afs_uint32 addrs[ADDRSPERSITE];
494 int mtus[ADDRSPERSITE];
496 memset((void *)addrs, 0, sizeof(addrs));
497 memset((void *)mtus, 0, sizeof(mtus));
499 for (i=0; i<afs_cb_interface.numberOfInterfaces; i++) {
500 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
501 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
502 if (myNetAddrs[i] != ifinaddr) different++;
505 rxmtu = rxi_AdjustIfMTU(rxmtu);
506 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
507 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
508 addrs[i++] = ifinaddr;
509 if ( ( ifinaddr != 0x7f000001 ) && (maxmtu > rx_maxReceiveSize) ) {
510 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
511 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
515 rx_maxJumboRecvSize = RX_HEADER_SIZE +
516 ( rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
517 ((rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE);
518 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
521 for (j=0; j<i; j++) {
522 myNetMTUs[j] = mtus[j];
523 myNetAddrs[j] = addrs[j];
530 /* Returns the afs_cb_interface inxex which best matches address.
531 * If none is found, we return -1.
533 afs_int32 rxi_Findcbi(addr)
537 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
538 afs_int32 rvalue = -1;
541 if (numMyNetAddrs == 0)
542 (void) rxi_GetcbiInfo();
544 myAddr = ntohl(addr);
546 if ( IN_CLASSA(myAddr) ) netMask = IN_CLASSA_NET;
547 else if ( IN_CLASSB(myAddr) ) netMask = IN_CLASSB_NET;
548 else if ( IN_CLASSC(myAddr) ) netMask = IN_CLASSC_NET;
551 for (j=0; j<afs_cb_interface.numberOfInterfaces; j++) {
552 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
553 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
554 if ((myAddr & netMask) == (thisAddr & netMask)) {
555 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
556 if (myAddr == thisAddr) {
561 if (match_value < 3) {
566 if (match_value < 2) {
578 #else /* AFS_USERSPACE_IP_ADDR */
580 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
581 #define IFADDR2SA(f) (&((f)->ifa_addr))
582 #else /* AFS_AIX41_ENV */
583 #define IFADDR2SA(f) ((f)->ifa_addr)
591 register struct ifnet *ifn;
592 register int rxmtu, maxmtu;
593 afs_uint32 addrs[ADDRSPERSITE];
594 int mtus[ADDRSPERSITE];
595 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
598 memset(addrs, 0, sizeof(addrs));
599 memset(mtus, 0, sizeof(mtus));
601 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
602 TAILQ_FOREACH(ifn, &ifnet, if_link) {
603 if (i >= ADDRSPERSITE) break;
605 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
607 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
608 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
609 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
610 if (i >= ADDRSPERSITE) break;
612 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
613 ifad = ifad->ifa_next){
615 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
616 ifinaddr = ntohl(((struct sockaddr_in *) IFADDR2SA(ifad))->sin_addr.s_addr);
617 if (myNetAddrs[i] != ifinaddr) {
621 rxmtu = rxi_AdjustIfMTU(rxmtu);
622 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
623 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
624 addrs[i++] = ifinaddr;
625 if ( ( ifinaddr != 0x7f000001 ) &&
626 (maxmtu > rx_maxReceiveSize) ) {
627 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
628 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
634 rx_maxJumboRecvSize = RX_HEADER_SIZE
635 + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE
636 + (rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE;
637 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
641 for (j=0; j< i; j++) {
642 myNetMTUs[j] = mtus[j];
643 myNetAddrs[j] = addrs[j];
649 /* Returns ifnet which best matches address */
651 rxi_FindIfnet(addr, pifad)
653 struct in_ifaddr **pifad;
657 extern struct in_ifaddr *in_ifaddr;
658 struct in_ifaddr *ifa;
659 struct sockaddr_in *sin;
661 if (numMyNetAddrs == 0)
662 (void) rxi_GetIFInfo();
664 ppaddr = ntohl(addr);
666 /* if we're given an address, skip everything until we find it */
668 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
669 *pifad = TAILQ_FIRST(&in_ifaddrhead);
674 if (((ppaddr & (*pifad)->ia_subnetmask) == (*pifad)->ia_subnet))
675 match_value = 2; /* don't find matching nets, just subnets */
676 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
677 *pifad = TAILQ_NEXT(*pifad, ia_link);
679 *pifad = (*pifad)->ia_next;
683 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
684 for (ifa = *pifad; ifa; ifa = TAILQ_NEXT(ifa, ia_link) ) {
686 for (ifa = *pifad; ifa; ifa = ifa->ia_next ) {
688 if ((ppaddr & ifa->ia_netmask) == ifa->ia_net) {
689 if ((ppaddr & ifa->ia_subnetmask) == ifa->ia_subnet) {
691 if ( sin->sin_addr.s_addr == ppaddr) { /* ie, ME!!! */
696 if (match_value < 3) {
702 if (match_value < 2) {
707 } /* if net matches */
708 } /* for all in_ifaddrs */
711 return (*pifad ? (*pifad)->ia_ifp : NULL );
713 #endif /* else AFS_USERSPACE_IP_ADDR */
714 #endif /* !SUN5 && !SGI62 */
717 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
718 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
719 * system specific subdirectories for all systems. But for the moment,
720 * most of it is simple to follow common code.
722 #if !defined(UKERNEL)
723 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
724 /* rxk_NewSocket creates a new socket on the specified port. The port is
725 * in network byte order.
727 struct osi_socket *rxk_NewSocket(short aport)
729 register afs_int32 code;
730 struct socket *newSocket;
731 register struct mbuf *nam;
732 struct sockaddr_in myaddr;
734 #ifdef AFS_HPUX110_ENV
735 /* prototype copied from kernel source file streams/str_proto.h */
736 extern MBLKP allocb_wait(int, int);
738 int addrsize = sizeof(struct sockaddr_in);
744 AFS_STATCNT(osi_NewSocket);
745 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
746 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
748 #if defined(AFS_HPUX102_ENV)
749 #if defined(AFS_HPUX110_ENV)
750 /* blocking socket */
751 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
752 #else /* AFS_HPUX110_ENV */
753 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
754 #endif /* else AFS_HPUX110_ENV */
757 code = socreate(AF_INET, &newSocket, SOCK_DGRAM,IPPROTO_UDP);
758 #elif defined(AFS_XBSD_ENV)
759 code = socreate(AF_INET, &newSocket, SOCK_DGRAM,IPPROTO_UDP, curproc);
761 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
762 #endif /* AFS_SGI65_ENV */
763 #endif /* AFS_HPUX102_ENV */
766 myaddr.sin_family = AF_INET;
767 myaddr.sin_port = aport;
768 myaddr.sin_addr.s_addr = 0;
770 #ifdef AFS_HPUX110_ENV
771 bindnam = allocb_wait((addrsize+SO_MSGOFFSET+1), BPRI_MED);
776 memcpy((caddr_t)bindnam->b_rptr+SO_MSGOFFSET, (caddr_t)&myaddr, addrsize);
777 bindnam->b_wptr = bindnam->b_rptr + (addrsize+SO_MSGOFFSET+1);
779 code = sobind(newSocket, bindnam, addrsize);
787 #else /* AFS_HPUX110_ENV */
788 code = soreserve(newSocket, 50000, 50000);
790 code = soreserve(newSocket, 32766, 32766);
792 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
794 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
795 myaddr.sin_len = sizeof(myaddr);
796 #if defined(AFS_XBSD_ENV)
797 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
799 code = sobind(newSocket, (struct sockaddr *)&myaddr);
802 printf("sobind fails\n");
808 nam = m_getclr(M_WAIT, MT_SONAME);
809 #else /* AFS_OSF_ENV */
810 nam = m_get(M_WAIT, MT_SONAME);
813 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_SGI64_ENV) && !defined(AFS_XBSD_ENV)
818 nam->m_len = sizeof(myaddr);
820 myaddr.sin_len = nam->m_len;
821 #endif /* AFS_OSF_ENV */
822 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
824 BHV_PDATA(&bhv) = (void*)newSocket;
825 code = sobind(&bhv, nam);
827 #elif defined(AFS_XBSD_ENV)
828 code = sobind(newSocket, nam, curproc);
830 code = sobind(newSocket, nam);
834 #ifndef AFS_SGI65_ENV
839 #endif /* else AFS_DARWIN_ENV */
840 #endif /* else AFS_HPUX110_ENV */
842 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
843 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
845 return (struct osi_socket *) newSocket;
848 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
849 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
851 return (struct osi_socket *) 0;
855 /* free socket allocated by rxk_NewSocket */
856 int rxk_FreeSocket(asocket)
857 register struct socket *asocket;
859 AFS_STATCNT(osi_FreeSocket);
860 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
861 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
864 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
865 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
869 #endif /* !SUN5 && !LINUX20 */
871 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
873 * Run RX event daemon every second (5 times faster than rest of systems)
882 #ifdef RX_ENABLE_LOCKS
884 #endif /* RX_ENABLE_LOCKS */
887 rxevent_RaiseEvents(&temp);
890 #ifdef RX_ENABLE_LOCKS
892 #endif /* RX_ENABLE_LOCKS */
893 afs_osi_Wait(500, (char *)0, 0);
894 if (afs_termState == AFSOP_STOP_RXEVENT )
896 #ifdef RXK_LISTENER_ENV
897 afs_termState = AFSOP_STOP_RXK_LISTENER;
899 afs_termState = AFSOP_STOP_COMPLETE;
901 afs_osi_Wakeup(&afs_termState);
908 #ifdef RXK_LISTENER_ENV
910 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
911 int rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
914 struct sockaddr_in from;
917 register afs_int32 tlen;
918 afs_int32 savelen; /* was using rlen but had aliasing problems */
919 rx_computelen(p, tlen);
920 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
922 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
923 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
924 * it once in order to avoid races. */
927 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
935 /* add some padding to the last iovec, it's just to make sure that the
936 * read doesn't return more data than we expect, and is done to get around
937 * our problems caused by the lack of a length field in the rx header. */
938 savelen = p->wirevec[p->niovecs-1].iov_len;
939 p->wirevec[p->niovecs-1].iov_len = savelen + RX_EXTRABUFFERSIZE;
941 nbytes = tlen + sizeof(afs_int32);
942 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs,
945 /* restore the vec to its correct state */
946 p->wirevec[p->niovecs-1].iov_len = savelen;
949 p->length = nbytes - RX_HEADER_SIZE;;
950 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
952 rxi_MorePackets(rx_initSendWindow);
954 MUTEX_ENTER(&rx_stats_mutex);
955 rx_stats.bogusPacketOnRead++;
956 rx_stats.bogusHost = from.sin_addr.s_addr;
957 MUTEX_EXIT(&rx_stats_mutex);
958 dpf(("B: bogus packet from [%x,%d] nb=%d", from.sin_addr.s_addr,
959 from.sin_port,nbytes));
964 /* Extract packet header. */
965 rxi_DecodePacketHeader(p);
967 *host = from.sin_addr.s_addr;
968 *port = from.sin_port;
969 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
970 MUTEX_ENTER(&rx_stats_mutex);
971 rx_stats.packetsRead[p->header.type-1]++;
972 MUTEX_EXIT(&rx_stats_mutex);
975 /* Free any empty packet buffers at the end of this packet */
976 rxi_TrimDataBufs(p, 1);
987 * Listen for packets on socket. This thread is typically started after
988 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
989 * the start state is set before proceeding.
991 * Note that this thread is outside the AFS global lock for much of
994 * In many OS's, the socket receive code sleeps interruptibly. That's not what
995 * we want here. So we need to either block all signals (including SIGKILL
996 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
997 * OS's socket receive routine returns as a result of a signal.
999 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1003 * Run the listener as a kernel process.
1005 void rxk_Listener(void)
1008 void rxk_ListenerProc(void);
1009 if (newproc(rxk_ListenerProc, syscid, 59))
1010 osi_Panic("rxk_Listener: failed to fork listener process!\n");
1013 void rxk_ListenerProc(void)
1014 #else /* AFS_SUN5_ENV */
1015 void rxk_Listener(void)
1016 #endif /* AFS_SUN5_ENV */
1018 struct rx_packet *rxp = NULL;
1022 #ifdef AFS_LINUX20_ENV
1023 rxk_ListenerPid = current->pid;
1026 rxk_ListenerPid = curproc->p_pid;
1027 #endif /* AFS_SUN5_ENV */
1028 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1029 rxk_ListenerPid = current_proc()->p_pid;
1031 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1033 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1035 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1037 rxi_RestoreDataBufs(rxp);
1040 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1042 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1044 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1046 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port);
1051 #ifdef RX_ENABLE_LOCKS
1053 #endif /* RX_ENABLE_LOCKS */
1054 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1055 afs_termState = AFSOP_STOP_COMPLETE;
1056 afs_osi_Wakeup(&afs_termState);
1058 rxk_ListenerPid = 0;
1059 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1060 afs_osi_Wakeup(&rxk_ListenerPid);
1064 #endif /* AFS_SUN5_ENV */
1067 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1068 /* The manner of stopping the rx listener thread may vary. Most unix's should
1069 * be able to call soclose.
1071 void osi_StopListener(void)
1076 #endif /* RXK_LISTENER_ENV */
1078 #endif /* !NCR && !UKERNEL */