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 "../afs/param.h"
15 #include "../rx/rx_kcommon.h"
17 #ifdef AFS_HPUX110_ENV
18 #include "../h/tihdr.h"
20 #include "../afs/hpux_110.h"
22 #include "../afsint/afsint.h"
24 afs_int32 rxi_Findcbi();
25 extern struct interfaceAddr afs_cb_interface;
27 #ifndef RXK_LISTENER_ENV
28 int (*rxk_GetPacketProc)(); /* set to packet allocation procedure */
29 int (*rxk_PacketArrivalProc)();
32 rxk_ports_t rxk_ports;
33 rxk_portRocks_t rxk_portRocks;
37 /* add a port to the monitored list, port # is in network order */
38 static int rxk_AddPort(u_short aport, char * arock)
41 unsigned short *tsp, ts;
44 zslot = -1; /* look for an empty slot simultaneously */
45 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
46 if (((ts = *tsp) == 0) && (zslot == -1))
52 /* otherwise allocate a new port slot */
53 if (zslot < 0) return E2BIG; /* all full */
54 rxk_ports[zslot] = aport;
55 rxk_portRocks[zslot] = arock;
59 /* remove as port from the monitored list, port # is in network order */
63 register unsigned short *tsp;
65 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
67 /* found it, adjust ref count and free the port reference if all gone */
72 /* otherwise port not found */
76 void rxk_shutdownPorts(void)
79 for (i=0; i<MAXRXPORTS;i++) {
82 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
83 soclose((struct socket *)rxk_portRocks[i]);
85 rxk_portRocks[i] = (char *)0;
90 osi_socket rxi_GetUDPSocket(port)
93 struct osi_socket *sockp;
94 sockp = (struct osi_socket *) rxk_NewSocket(port);
95 if (sockp == (struct osi_socket *) 0) return OSI_NULLSOCKET;
96 rxk_AddPort(port, (char *) sockp);
97 return (osi_socket)sockp;
101 void osi_Panic(msg, a1, a2, a3)
105 msg = "Unknown AFS panic";
107 printf(msg, a1, a2, a3);
108 #ifdef AFS_LINUX20_ENV
109 *((char*)0xffffffff) = 42;
116 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
117 * unsigned long value into the given buffer. Returns 0 on success,
118 * and a value less than 0 on failure. The contents of the buffer is
119 * defined only on success.
123 osi_utoa(char *buf, size_t len, unsigned long val)
125 long k; /* index of first byte of string value */
127 /* we definitely need room for at least one digit and NUL */
133 /* compute the string form from the high end of the buffer */
136 for (k = len - 2; k >= 0; k--) {
137 buf[k] = val % 10 + '0';
144 /* did we finish converting val to string form? */
150 /* this should never happen */
156 /* this should never happen */
162 /* if necessary, relocate string to beginning of buf[] */
167 * We need to achieve the effect of calling
169 * memmove(buf, &buf[k], len - k);
171 * However, since memmove() is not available in all
172 * kernels, we explicitly do an appropriate copy.
178 while((*dst++ = *src++) != '\0')
186 * osi_AssertFailK() -- used by the osi_Assert() macro.
188 * It essentially does
190 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
192 * Since the kernel version of osi_Panic() only passes its first
193 * argument to the native panic(), we construct a single string and hand
194 * that to osi_Panic().
197 osi_AssertFailK(const char *expr, const char *file, int line)
199 static const char msg0[] = "assertion failed: ";
200 static const char msg1[] = ", file: ";
201 static const char msg2[] = ", line: ";
202 static const char msg3[] = "\n";
205 * These buffers add up to 1K, which is a pleasantly nice round
206 * value, but probably not vital.
211 /* check line number conversion */
213 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
214 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
219 #define ADDBUF(BUF, STR) \
220 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
221 strcat(BUF, (char *)(STR)); \
230 ADDBUF(buf, linebuf);
239 /* This is the server process request loop. Kernel server
240 * processes never become listener threads */
245 rxi_MorePackets(rx_maxReceiveWindow+2); /* alloc more packets */
246 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
247 /* threadID is used for making decisions in GetCall. Get it by bumping
248 * number of threads handling incoming calls */
249 threadID = rxi_availProcs++;
251 #ifdef RX_ENABLE_LOCKS
253 #endif /* RX_ENABLE_LOCKS */
254 rxi_ServerProc(threadID, NULL, NULL);
255 #ifdef RX_ENABLE_LOCKS
257 #endif /* RX_ENABLE_LOCKS */
259 #endif /* !UKERNEL */
261 #ifndef RXK_LISTENER_ENV
262 static int MyPacketProc(ahandle, asize)
263 int asize; /* this includes the Rx header */
266 register struct rx_packet *tp;
268 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
269 /* If we can't scrounge enough cbufs, then we have to drop the packet,
270 * but we should set a flag so we magic up some more at our leisure.
273 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
274 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
275 if (tp && (tp->length + RX_HEADER_SIZE) < asize ) {
276 if (0 < rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
277 RX_PACKET_CLASS_RECV_CBUF)) {
280 MUTEX_ENTER(&rx_stats_mutex);
281 rx_stats.noPacketBuffersOnRead++;
282 MUTEX_EXIT(&rx_stats_mutex);
287 * XXX if packet is too long for our buffer,
288 * should do this at a higher layer and let other
289 * end know we're losing.
291 MUTEX_ENTER(&rx_stats_mutex);
292 rx_stats.bogusPacketOnRead++;
293 MUTEX_EXIT(&rx_stats_mutex);
294 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
295 printf("rx: packet dropped: bad ulen=%d\n", asize);
300 /* otherwise we have a packet, set appropriate values */
301 *ahandle = (char *) tp;
305 static int MyArrivalProc(ahandle, afrom, arock, asize)
306 register struct rx_packet *ahandle;
307 register struct sockaddr_in *afrom;
310 /* handle basic rx packet */
311 ahandle->length = asize - RX_HEADER_SIZE;
312 rxi_DecodePacketHeader(ahandle);
313 ahandle = rxi_ReceivePacket(ahandle, (struct socket *) arock,
314 afrom->sin_addr.s_addr, afrom->sin_port,
317 /* free the packet if it has been returned */
318 if (ahandle) rxi_FreePacket(ahandle);
321 #endif /* !RXK_LISTENER_ENV */
324 rxi_StartListener() {
325 /* if kernel, give name of appropriate procedures */
326 #ifndef RXK_LISTENER_ENV
327 rxk_GetPacketProc = MyPacketProc;
328 rxk_PacketArrivalProc = MyArrivalProc;
333 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
334 to get interesting information. */
335 void rxi_InitPeerParams(pp)
336 register struct rx_peer *pp;
342 #ifdef AFS_USERSPACE_IP_ADDR
343 i = rxi_Findcbi(pp->host);
346 /* pp->timeout.usec = 0; */
347 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
350 /* pp->timeout.usec = 0; */
351 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
354 mtu = ntohl(afs_cb_interface.mtu[i]);
355 /* Diminish the packet size to one based on the MTU given by
357 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
358 rxmtu = mtu - RX_IPUDP_SIZE;
359 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
362 else { /* couldn't find the interface, so assume the worst */
363 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
365 #else /* AFS_USERSPACE_IP_ADDR */
366 struct in_ifaddr *ifad = (struct in_ifaddr *) 0;
369 /* At some time we need to iterate through rxi_FindIfnet() to find the
372 ifn = rxi_FindIfnet(pp->host, &ifad);
373 if (ifn == NULL) { /* not local */
375 /* pp->timeout.usec = 0; */
376 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
379 /* pp->timeout.usec = 0; */
380 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
383 #ifdef IFF_POINTOPOINT
384 if (ifn->if_flags & IFF_POINTOPOINT) {
385 /* wish we knew the bit rate and the chunk size, sigh. */
387 pp->ifMTU = RX_PP_PACKET_SIZE;
389 #endif /* IFF_POINTOPOINT */
390 /* Diminish the packet size to one based on the MTU given by
392 if (ifn->if_mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
393 rxmtu = ifn->if_mtu - RX_IPUDP_SIZE;
394 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
397 else { /* couldn't find the interface, so assume the worst */
398 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
400 #endif/* else AFS_USERSPACE_IP_ADDR */
401 #else /* ADAPT_MTU */
402 pp->rateFlag = 2; /* start timing after two full packets */
404 pp->ifMTU = OLD_MAX_PACKET_SIZE;
405 #endif /* else ADAPT_MTU */
406 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
407 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
408 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
409 pp->ifDgramPackets = MIN(rxi_nDgramPackets,
410 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
411 pp->maxDgramPackets = 1;
413 /* Initialize slow start parameters */
414 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
416 pp->nDgramPackets = 1;
421 /* The following code is common to several system types, but not all. The
422 * separate ones are found in the system specific subdirectories.
426 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_FBSD_ENV)
427 /* Routine called during the afsd "-shutdown" process to put things back to
430 static struct protosw parent_proto; /* udp proto switch */
432 void shutdown_rxkernel(void)
434 register struct protosw *tpro, *last;
435 last = inetdomain.dom_protoswNPROTOSW;
436 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
437 if (tpro->pr_protocol == IPPROTO_UDP) {
438 /* restore original udp protocol switch */
439 bcopy((void *)&parent_proto, (void *)tpro, sizeof(parent_proto));
440 bzero((void *)&parent_proto, sizeof(parent_proto));
445 printf("shutdown_rxkernel: no udp proto");
447 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
449 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
450 /* Determine what the network interfaces are for this machine. */
452 #define ADDRSPERSITE 16
453 static afs_uint32 myNetAddrs[ADDRSPERSITE];
454 static int myNetMTUs[ADDRSPERSITE];
455 static int myNetFlags[ADDRSPERSITE];
456 static int numMyNetAddrs = 0;
458 #ifdef AFS_USERSPACE_IP_ADDR
461 int i, j, different = 0;
464 afs_uint32 addrs[ADDRSPERSITE];
465 int mtus[ADDRSPERSITE];
467 bzero((void *)addrs, sizeof(addrs));
468 bzero((void *)mtus, sizeof(mtus));
470 for (i=0; i<afs_cb_interface.numberOfInterfaces; i++) {
471 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
472 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
473 if (myNetAddrs[i] != ifinaddr) different++;
476 rxmtu = rxi_AdjustIfMTU(rxmtu);
477 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
478 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
479 addrs[i++] = ifinaddr;
480 if ( ( ifinaddr != 0x7f000001 ) && (maxmtu > rx_maxReceiveSize) ) {
481 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
482 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
486 rx_maxJumboRecvSize = RX_HEADER_SIZE +
487 ( rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
488 ((rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE);
489 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
492 for (j=0; j<i; j++) {
493 myNetMTUs[j] = mtus[j];
494 myNetAddrs[j] = addrs[j];
501 /* Returns the afs_cb_interface inxex which best matches address.
502 * If none is found, we return -1.
504 afs_int32 rxi_Findcbi(addr)
508 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
509 afs_int32 rvalue = -1;
512 if (numMyNetAddrs == 0)
513 (void) rxi_GetcbiInfo();
515 myAddr = ntohl(addr);
517 if ( IN_CLASSA(myAddr) ) netMask = IN_CLASSA_NET;
518 else if ( IN_CLASSB(myAddr) ) netMask = IN_CLASSB_NET;
519 else if ( IN_CLASSC(myAddr) ) netMask = IN_CLASSC_NET;
522 for (j=0; j<afs_cb_interface.numberOfInterfaces; j++) {
523 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
524 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
525 if ((myAddr & netMask) == (thisAddr & netMask)) {
526 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
527 if (myAddr == thisAddr) {
532 if (match_value < 3) {
537 if (match_value < 2) {
549 #else /* AFS_USERSPACE_IP_ADDR */
551 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
552 #define IFADDR2SA(f) (&((f)->ifa_addr))
553 #else /* AFS_AIX41_ENV */
554 #define IFADDR2SA(f) ((f)->ifa_addr)
562 register struct ifnet *ifn;
563 register int rxmtu, maxmtu;
564 afs_uint32 addrs[ADDRSPERSITE];
565 int mtus[ADDRSPERSITE];
566 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
569 bzero(addrs, sizeof(addrs));
570 bzero(mtus, sizeof(mtus));
572 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
573 TAILQ_FOREACH(ifn, &ifnet, if_link) {
574 if (i >= ADDRSPERSITE) break;
576 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
578 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
579 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
580 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
581 if (i >= ADDRSPERSITE) break;
583 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
584 ifad = ifad->ifa_next){
586 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
587 ifinaddr = ntohl(((struct sockaddr_in *) IFADDR2SA(ifad))->sin_addr.s_addr);
588 if (myNetAddrs[i] != ifinaddr) {
592 rxmtu = rxi_AdjustIfMTU(rxmtu);
593 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
594 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
595 addrs[i++] = ifinaddr;
596 if ( ( ifinaddr != 0x7f000001 ) &&
597 (maxmtu > rx_maxReceiveSize) ) {
598 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
599 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
605 rx_maxJumboRecvSize = RX_HEADER_SIZE
606 + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE
607 + (rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE;
608 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
612 for (j=0; j< i; j++) {
613 myNetMTUs[j] = mtus[j];
614 myNetAddrs[j] = addrs[j];
620 /* Returns ifnet which best matches address */
622 rxi_FindIfnet(addr, pifad)
624 struct in_ifaddr **pifad;
628 extern struct in_ifaddr *in_ifaddr;
629 struct in_ifaddr *ifa;
630 struct sockaddr_in *sin;
632 if (numMyNetAddrs == 0)
633 (void) rxi_GetIFInfo();
635 ppaddr = ntohl(addr);
637 /* if we're given an address, skip everything until we find it */
639 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
640 *pifad = TAILQ_FIRST(&in_ifaddrhead);
645 if (((ppaddr & (*pifad)->ia_subnetmask) == (*pifad)->ia_subnet))
646 match_value = 2; /* don't find matching nets, just subnets */
647 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
648 *pifad = TAILQ_NEXT(*pifad, ia_link);
650 *pifad = (*pifad)->ia_next;
654 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
655 for (ifa = *pifad; ifa; ifa = TAILQ_NEXT(ifa, ia_link) ) {
657 for (ifa = *pifad; ifa; ifa = ifa->ia_next ) {
659 if ((ppaddr & ifa->ia_netmask) == ifa->ia_net) {
660 if ((ppaddr & ifa->ia_subnetmask) == ifa->ia_subnet) {
662 if ( sin->sin_addr.s_addr == ppaddr) { /* ie, ME!!! */
667 if (match_value < 3) {
673 if (match_value < 2) {
678 } /* if net matches */
679 } /* for all in_ifaddrs */
682 return (*pifad ? (*pifad)->ia_ifp : NULL );
684 #endif /* else AFS_USERSPACE_IP_ADDR */
685 #endif /* !SUN5 && !SGI62 */
688 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
689 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
690 * system specific subdirectories for all systems. But for the moment,
691 * most of it is simple to follow common code.
693 #if !defined(UKERNEL)
694 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
695 /* rxk_NewSocket creates a new socket on the specified port. The port is
696 * in network byte order.
698 struct osi_socket *rxk_NewSocket(short aport)
700 register afs_int32 code;
701 struct socket *newSocket;
702 register struct mbuf *nam;
703 struct sockaddr_in myaddr;
705 #ifdef AFS_HPUX110_ENV
706 /* prototype copied from kernel source file streams/str_proto.h */
707 extern MBLKP allocb_wait(int, int);
709 int addrsize = sizeof(struct sockaddr_in);
715 AFS_STATCNT(osi_NewSocket);
716 #if (defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)) && defined(KERNEL_FUNNEL)
717 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
719 #if defined(AFS_HPUX102_ENV)
720 #if defined(AFS_HPUX110_ENV)
721 /* blocking socket */
722 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
723 #else /* AFS_HPUX110_ENV */
724 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
725 #endif /* else AFS_HPUX110_ENV */
728 code = socreate(AF_INET, &newSocket, SOCK_DGRAM,IPPROTO_UDP);
729 #elif defined(AFS_FBSD_ENV)
730 code = socreate(AF_INET, &newSocket, SOCK_DGRAM,IPPROTO_UDP, curproc);
732 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
733 #endif /* AFS_SGI65_ENV */
734 #endif /* AFS_HPUX102_ENV */
737 myaddr.sin_family = AF_INET;
738 myaddr.sin_port = aport;
739 myaddr.sin_addr.s_addr = 0;
741 #ifdef AFS_HPUX110_ENV
742 bindnam = allocb_wait((addrsize+SO_MSGOFFSET+1), BPRI_MED);
747 bcopy((caddr_t)&myaddr, (caddr_t)bindnam->b_rptr+SO_MSGOFFSET, addrsize);
748 bindnam->b_wptr = bindnam->b_rptr + (addrsize+SO_MSGOFFSET+1);
750 code = sobind(newSocket, bindnam, addrsize);
758 #else /* AFS_HPUX110_ENV */
759 code = soreserve(newSocket, 50000, 50000);
761 code = soreserve(newSocket, 32766, 32766);
763 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
765 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
766 myaddr.sin_len = sizeof(myaddr);
767 #if defined(AFS_FBSD_ENV)
768 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
770 code = sobind(newSocket, (struct sockaddr *)&myaddr);
773 printf("sobind fails\n");
779 nam = m_getclr(M_WAIT, MT_SONAME);
780 #else /* AFS_OSF_ENV */
781 nam = m_get(M_WAIT, MT_SONAME);
784 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_SGI64_ENV) && !defined(AFS_FBSD_ENV)
789 nam->m_len = sizeof(myaddr);
791 myaddr.sin_len = nam->m_len;
792 #endif /* AFS_OSF_ENV */
793 bcopy(&myaddr, mtod(nam, caddr_t), sizeof(myaddr));
795 BHV_PDATA(&bhv) = (void*)newSocket;
796 code = sobind(&bhv, nam);
798 #elif defined(AFS_FBSD_ENV)
799 code = sobind(newSocket, nam, curproc);
801 code = sobind(newSocket, nam);
805 #ifndef AFS_SGI65_ENV
810 #endif /* else AFS_DARWIN_ENV */
811 #endif /* else AFS_HPUX110_ENV */
813 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
814 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
816 return (struct osi_socket *) newSocket;
819 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
820 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
822 return (struct osi_socket *) 0;
826 /* free socket allocated by rxk_NewSocket */
827 int rxk_FreeSocket(asocket)
828 register struct socket *asocket;
830 AFS_STATCNT(osi_FreeSocket);
831 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
832 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
835 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
836 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
840 #endif /* !SUN5 && !LINUX20 */
842 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
844 * Run RX event daemon every second (5 times faster than rest of systems)
853 #ifdef RX_ENABLE_LOCKS
855 #endif /* RX_ENABLE_LOCKS */
858 rxevent_RaiseEvents(&temp);
861 #ifdef RX_ENABLE_LOCKS
863 #endif /* RX_ENABLE_LOCKS */
864 afs_osi_Wait(500, (char *)0, 0);
865 if (afs_termState == AFSOP_STOP_RXEVENT )
867 #ifdef RXK_LISTENER_ENV
868 afs_termState = AFSOP_STOP_RXK_LISTENER;
870 afs_termState = AFSOP_STOP_COMPLETE;
872 afs_osi_Wakeup(&afs_termState);
879 #ifdef RXK_LISTENER_ENV
881 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
882 int rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
885 struct sockaddr_in from;
888 register afs_int32 tlen;
889 afs_int32 savelen; /* was using rlen but had aliasing problems */
890 rx_computelen(p, tlen);
891 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
893 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
894 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
895 * it once in order to avoid races. */
898 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
906 /* add some padding to the last iovec, it's just to make sure that the
907 * read doesn't return more data than we expect, and is done to get around
908 * our problems caused by the lack of a length field in the rx header. */
909 savelen = p->wirevec[p->niovecs-1].iov_len;
910 p->wirevec[p->niovecs-1].iov_len = savelen + RX_EXTRABUFFERSIZE;
912 nbytes = tlen + sizeof(afs_int32);
913 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs,
916 /* restore the vec to its correct state */
917 p->wirevec[p->niovecs-1].iov_len = savelen;
920 p->length = nbytes - RX_HEADER_SIZE;;
921 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
923 rxi_MorePackets(rx_initSendWindow);
925 MUTEX_ENTER(&rx_stats_mutex);
926 rx_stats.bogusPacketOnRead++;
927 rx_stats.bogusHost = from.sin_addr.s_addr;
928 MUTEX_EXIT(&rx_stats_mutex);
929 dpf(("B: bogus packet from [%x,%d] nb=%d", from.sin_addr.s_addr,
930 from.sin_port,nbytes));
935 /* Extract packet header. */
936 rxi_DecodePacketHeader(p);
938 *host = from.sin_addr.s_addr;
939 *port = from.sin_port;
940 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
941 MUTEX_ENTER(&rx_stats_mutex);
942 rx_stats.packetsRead[p->header.type-1]++;
943 MUTEX_EXIT(&rx_stats_mutex);
946 /* Free any empty packet buffers at the end of this packet */
947 rxi_TrimDataBufs(p, 1);
958 * Listen for packets on socket. This thread is typically started after
959 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
960 * the start state is set before proceeding.
962 * Note that this thread is outside the AFS global lock for much of
965 * In many OS's, the socket receive code sleeps interruptibly. That's not what
966 * we want here. So we need to either block all signals (including SIGKILL
967 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
968 * OS's socket receive routine returns as a result of a signal.
970 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
974 * Run the listener as a kernel process.
976 void rxk_Listener(void)
979 void rxk_ListenerProc(void);
980 if (newproc(rxk_ListenerProc, syscid, 59))
981 osi_Panic("rxk_Listener: failed to fork listener process!\n");
984 void rxk_ListenerProc(void)
985 #else /* AFS_SUN5_ENV */
986 void rxk_Listener(void)
987 #endif /* AFS_SUN5_ENV */
989 struct rx_packet *rxp = NULL;
993 #ifdef AFS_LINUX20_ENV
994 rxk_ListenerPid = current->pid;
997 rxk_ListenerPid = ttoproc(curthread)->p_pidp->pid_id;
998 #endif /* AFS_SUN5_ENV */
999 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1000 rxk_ListenerPid = current_proc()->p_pid;
1002 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1004 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1006 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1008 rxi_RestoreDataBufs(rxp);
1011 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1013 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1015 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1017 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port);
1020 if (afs_termState == AFSOP_STOP_RXK_LISTENER)
1025 #ifdef RX_ENABLE_LOCKS
1027 #endif /* RX_ENABLE_LOCKS */
1028 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1029 afs_termState = AFSOP_STOP_COMPLETE;
1030 afs_osi_Wakeup(&afs_termState);
1032 rxk_ListenerPid = 0;
1033 #ifdef AFS_LINUX24_ENV
1034 afs_osi_Wakeup(&rxk_ListenerPid);
1038 #endif /* AFS_SUN5_ENV */
1041 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
1042 /* The manner of stopping the rx listener thread may vary. Most unix's should
1043 * be able to call soclose.
1045 void osi_StopListener(void)
1050 #endif /* RXK_LISTENER_ENV */
1052 #endif /* !NCR && !UKERNEL */