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"
18 #include "rx/rx_kcommon.h"
20 #ifdef AFS_HPUX110_ENV
26 #ifndef RXK_LISTENER_ENV
27 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
28 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
31 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
32 extern struct interfaceAddr afs_cb_interface;
34 rxk_ports_t rxk_ports;
35 rxk_portRocks_t rxk_portRocks;
39 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
40 #define ADDRSPERSITE 16
41 static afs_uint32 myNetAddrs[ADDRSPERSITE];
42 static int myNetMTUs[ADDRSPERSITE];
43 static int numMyNetAddrs = 0;
46 #if defined(AFS_DARWIN80_ENV)
47 #define sobind sock_bind
48 #define soclose sock_close
51 /* add a port to the monitored list, port # is in network order */
53 rxk_AddPort(u_short aport, char *arock)
56 unsigned short *tsp, ts;
59 zslot = -1; /* look for an empty slot simultaneously */
60 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
61 if (((ts = *tsp) == 0) && (zslot == -1))
67 /* otherwise allocate a new port slot */
69 return E2BIG; /* all full */
70 rxk_ports[zslot] = aport;
71 rxk_portRocks[zslot] = arock;
75 /* remove as port from the monitored list, port # is in network order */
77 rxk_DelPort(u_short aport)
82 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
84 /* found it, adjust ref count and free the port reference if all gone */
89 /* otherwise port not found */
94 rxk_shutdownPorts(void)
97 for (i = 0; i < MAXRXPORTS; i++) {
100 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
101 soclose((struct socket *)rxk_portRocks[i]);
103 rxk_portRocks[i] = NULL;
109 rxi_GetHostUDPSocket(u_int host, u_short port)
112 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
113 if (sockp == (osi_socket *)0)
114 return OSI_NULLSOCKET;
115 rxk_AddPort(port, (char *)sockp);
116 return (osi_socket) sockp;
120 rxi_GetUDPSocket(u_short port)
122 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
125 #if !defined(AFS_LINUX26_ENV)
128 osi_Panic(char *msg, void *a1, void *a2, void *a3)
130 osi_Panic(char *msg, ...)
135 msg = "Unknown AFS panic";
136 printf(msg, a1, a2, a3);
138 #elif (defined(AFS_DARWIN80_ENV) && !defined(AFS_DARWIN90_ENV)) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
142 msg = "Unknown AFS panic";
145 vsnprintf(buf, sizeof(buf), msg, ap);
152 msg = "Unknown AFS panic";
157 # ifdef AFS_LINUX20_ENV
166 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
167 * unsigned long value into the given buffer. Returns 0 on success,
168 * and a value less than 0 on failure. The contents of the buffer is
169 * defined only on success.
173 osi_utoa(char *buf, size_t len, unsigned long val)
175 long k; /* index of first byte of string value */
177 /* we definitely need room for at least one digit and NUL */
183 /* compute the string form from the high end of the buffer */
186 for (k = len - 2; k >= 0; k--) {
187 buf[k] = val % 10 + '0';
194 /* did we finish converting val to string form? */
200 /* this should never happen */
206 /* this should never happen */
212 /* if necessary, relocate string to beginning of buf[] */
217 * We need to achieve the effect of calling
219 * memmove(buf, &buf[k], len - k);
221 * However, since memmove() is not available in all
222 * kernels, we explicitly do an appropriate copy.
228 while ((*dst++ = *src++) != '\0')
236 * osi_AssertFailK() -- used by the osi_Assert() macro.
238 * It essentially does
240 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
242 * Since the kernel version of osi_Panic() only passes its first
243 * argument to the native panic(), we construct a single string and hand
244 * that to osi_Panic().
247 osi_AssertFailK(const char *expr, const char *file, int line)
249 static const char msg0[] = "assertion failed: ";
250 static const char msg1[] = ", file: ";
251 static const char msg2[] = ", line: ";
252 static const char msg3[] = "\n";
255 * These buffers add up to 1K, which is a pleasantly nice round
256 * value, but probably not vital.
261 /* check line number conversion */
263 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
264 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
269 #define ADDBUF(BUF, STR) \
270 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
271 strcat(BUF, (char *)(STR)); \
280 ADDBUF(buf, linebuf);
289 osi_AssertFailK(const char *expr, const char *file, int line)
291 printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
296 /* This is the server process request loop. Kernel server
297 * processes never become listener threads */
299 rx_ServerProc(void *unused)
303 /* jaltman - rxi_dataQuota is protected by a mutex everywhere else */
304 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
305 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
306 /* threadID is used for making decisions in GetCall. Get it by bumping
307 * number of threads handling incoming calls */
308 threadID = rxi_availProcs++;
310 #ifdef RX_ENABLE_LOCKS
312 #endif /* RX_ENABLE_LOCKS */
313 rxi_ServerProc(threadID, NULL, NULL);
314 #ifdef RX_ENABLE_LOCKS
316 #endif /* RX_ENABLE_LOCKS */
320 #endif /* !UKERNEL */
322 #ifndef RXK_LISTENER_ENV
323 /* asize includes the Rx header */
325 MyPacketProc(struct rx_packet **ahandle, int asize)
327 struct rx_packet *tp;
329 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
330 /* If we can't scrounge enough cbufs, then we have to drop the packet,
331 * but we should set a flag so we magic up some more at our leisure.
334 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
335 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
336 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
338 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
339 RX_PACKET_CLASS_RECV_CBUF)) {
342 if (rx_stats_active) {
343 MUTEX_ENTER(&rx_stats_mutex);
344 rx_stats.noPacketBuffersOnRead++;
345 MUTEX_EXIT(&rx_stats_mutex);
351 * XXX if packet is too long for our buffer,
352 * should do this at a higher layer and let other
353 * end know we're losing.
355 if (rx_stats_active) {
356 MUTEX_ENTER(&rx_stats_mutex);
357 rx_stats.bogusPacketOnRead++;
358 MUTEX_EXIT(&rx_stats_mutex);
360 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
361 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
367 /* otherwise we have a packet, set appropriate values */
373 MyArrivalProc(struct rx_packet *ahandle,
374 struct sockaddr_in *afrom,
375 struct socket *arock,
378 /* handle basic rx packet */
379 ahandle->length = asize - RX_HEADER_SIZE;
380 rxi_DecodePacketHeader(ahandle);
382 rxi_ReceivePacket(ahandle, arock,
383 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
386 /* free the packet if it has been returned */
388 rxi_FreePacket(ahandle);
391 #endif /* !RXK_LISTENER_ENV */
394 rxi_StartListener(void)
396 /* if kernel, give name of appropriate procedures */
397 #ifndef RXK_LISTENER_ENV
398 rxk_GetPacketProc = MyPacketProc;
399 rxk_PacketArrivalProc = MyArrivalProc;
404 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
405 to get interesting information. */
407 rxi_InitPeerParams(struct rx_peer *pp)
414 #ifdef AFS_USERSPACE_IP_ADDR
415 i = rxi_Findcbi(pp->host);
418 /* pp->timeout.usec = 0; */
419 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
422 /* pp->timeout.usec = 0; */
423 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
426 mtu = ntohl(afs_cb_interface.mtu[i]);
427 /* Diminish the packet size to one based on the MTU given by
429 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
430 rxmtu = mtu - RX_IPUDP_SIZE;
431 if (rxmtu < pp->ifMTU)
434 } else { /* couldn't find the interface, so assume the worst */
435 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
437 #else /* AFS_USERSPACE_IP_ADDR */
440 #if !defined(AFS_SGI62_ENV)
441 if (numMyNetAddrs == 0)
442 (void)rxi_GetIFInfo();
445 ifn = rxi_FindIfnet(pp->host, NULL);
448 /* pp->timeout.usec = 0; */
449 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
450 #ifdef IFF_POINTOPOINT
451 if (ifnet_flags(ifn) & IFF_POINTOPOINT) {
452 /* wish we knew the bit rate and the chunk size, sigh. */
454 pp->ifMTU = RX_PP_PACKET_SIZE;
456 #endif /* IFF_POINTOPOINT */
457 /* Diminish the packet size to one based on the MTU given by
459 if (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
460 rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
461 if (rxmtu < pp->ifMTU)
464 } else { /* couldn't find the interface, so assume the worst */
466 /* pp->timeout.usec = 0; */
467 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
469 #endif /* else AFS_USERSPACE_IP_ADDR */
470 #else /* AFS_SUN5_ENV */
471 mtu = rxi_FindIfMTU(pp->host);
475 /* pp->timeout.usec = 0; */
476 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
479 /* pp->timeout.usec = 0; */
480 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
484 /* Diminish the packet size to one based on the MTU given by
486 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
487 rxmtu = mtu - RX_IPUDP_SIZE;
488 if (rxmtu < pp->ifMTU)
491 } else { /* couldn't find the interface, so assume the worst */
492 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE,rx_MyMaxSendSize);
494 #endif /* AFS_SUN5_ENV */
495 #else /* ADAPT_MTU */
496 pp->rateFlag = 2; /* start timing after two full packets */
498 pp->ifMTU = OLD_MAX_PACKET_SIZE;
499 #endif /* else ADAPT_MTU */
500 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
501 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
502 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
504 MIN(rxi_nDgramPackets,
505 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
506 pp->maxDgramPackets = 1;
508 /* Initialize slow start parameters */
509 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
511 pp->nDgramPackets = 1;
516 /* The following code is common to several system types, but not all. The
517 * separate ones are found in the system specific subdirectories.
521 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
522 /* Routine called during the afsd "-shutdown" process to put things back to
525 static struct protosw parent_proto; /* udp proto switch */
528 shutdown_rxkernel(void)
530 struct protosw *tpro, *last;
531 last = inetdomain.dom_protoswNPROTOSW;
532 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
533 if (tpro->pr_protocol == IPPROTO_UDP) {
534 /* restore original udp protocol switch */
535 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
536 memset((void *)&parent_proto, 0, sizeof(parent_proto));
541 dpf(("shutdown_rxkernel: no udp proto"));
543 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
545 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
546 /* Determine what the network interfaces are for this machine. */
548 #ifdef AFS_USERSPACE_IP_ADDR
552 int i, j, different = 0, num = ADDRSPERSITE;
555 afs_uint32 addrs[ADDRSPERSITE];
556 int mtus[ADDRSPERSITE];
558 memset((void *)addrs, 0, sizeof(addrs));
559 memset((void *)mtus, 0, sizeof(mtus));
561 if (afs_cb_interface.numberOfInterfaces < num)
562 num = afs_cb_interface.numberOfInterfaces;
563 for (i = 0; i < num; i++) {
564 if (!afs_cb_interface.mtu[i])
565 afs_cb_interface.mtu[i] = htonl(1500);
566 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
567 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
568 if (myNetAddrs[i] != ifinaddr)
572 rxmtu = rxi_AdjustIfMTU(rxmtu);
574 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
575 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
576 addrs[i++] = ifinaddr;
577 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
578 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
579 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
583 rx_maxJumboRecvSize =
584 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
585 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
586 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
589 for (j = 0; j < i; j++) {
590 myNetMTUs[j] = mtus[j];
591 myNetAddrs[j] = addrs[j];
598 /* Returns the afs_cb_interface inxex which best matches address.
599 * If none is found, we return -1.
602 rxi_Findcbi(afs_uint32 addr)
605 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
606 afs_int32 rvalue = -1;
609 if (numMyNetAddrs == 0)
610 (void)rxi_GetcbiInfo();
612 myAddr = ntohl(addr);
614 if (IN_CLASSA(myAddr))
615 netMask = IN_CLASSA_NET;
616 else if (IN_CLASSB(myAddr))
617 netMask = IN_CLASSB_NET;
618 else if (IN_CLASSC(myAddr))
619 netMask = IN_CLASSC_NET;
623 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
624 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
625 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
626 if ((myAddr & netMask) == (thisAddr & netMask)) {
627 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
628 if (myAddr == thisAddr) {
633 if (match_value < 3) {
638 if (match_value < 2) {
649 #else /* AFS_USERSPACE_IP_ADDR */
651 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
652 #define IFADDR2SA(f) (&((f)->ifa_addr))
653 #else /* AFS_AIX41_ENV */
654 #define IFADDR2SA(f) ((f)->ifa_addr)
664 afs_uint32 addrs[ADDRSPERSITE];
665 int mtus[ADDRSPERSITE];
667 #if defined(AFS_DARWIN80_ENV)
673 struct sockaddr sout;
674 struct sockaddr_in *sin;
677 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
681 memset(addrs, 0, sizeof(addrs));
682 memset(mtus, 0, sizeof(mtus));
684 #if defined(AFS_DARWIN80_ENV)
685 if (!ifnet_list_get(AF_INET, &ifn, &count)) {
686 for (m = 0; m < count; m++) {
687 if (!ifnet_get_address_list(ifn[m], &ifads)) {
688 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
689 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
690 sin = (struct sockaddr_in *)&sout;
691 rxmtu = ifnet_mtu(ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
692 ifinaddr = ntohl(sin->sin_addr.s_addr);
693 if (myNetAddrs[i] != ifinaddr) {
697 rxmtu = rxi_AdjustIfMTU(rxmtu);
699 rxmtu * rxi_nRecvFrags +
700 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
701 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
702 addrs[i++] = ifinaddr;
703 if ((ifinaddr != 0x7f000001) &&
704 (maxmtu > rx_maxReceiveSize)) {
706 MIN(RX_MAX_PACKET_SIZE, maxmtu);
708 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
713 ifnet_free_address_list(ifads);
716 ifnet_list_free(ifn);
719 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
720 TAILQ_FOREACH(ifn, &ifnet, if_link) {
721 if (i >= ADDRSPERSITE)
723 #elif defined(AFS_OBSD_ENV)
724 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
725 ifn = ifn->if_list.tqe_next) {
727 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
729 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
730 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
731 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
732 if (i >= ADDRSPERSITE)
734 #elif defined(AFS_OBSD_ENV)
735 for (ifad = ifn->if_addrlist.tqh_first;
736 ifad != NULL && i < ADDRSPERSITE;
737 ifad = ifad->ifa_list.tqe_next) {
739 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
740 ifad = ifad->ifa_next) {
742 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
744 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
746 if (myNetAddrs[i] != ifinaddr) {
750 rxmtu = rxi_AdjustIfMTU(rxmtu);
752 rxmtu * rxi_nRecvFrags +
753 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
754 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
755 addrs[i++] = ifinaddr;
756 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
757 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
759 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
766 rx_maxJumboRecvSize =
767 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
768 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
769 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
773 for (l = 0; l < i; l++) {
774 myNetMTUs[l] = mtus[l];
775 myNetAddrs[l] = addrs[l];
781 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
782 /* Returns ifnet which best matches address */
783 #ifdef AFS_DARWIN80_ENV
788 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
790 struct sockaddr_in s, sr;
791 #ifdef AFS_DARWIN80_ENV
797 s.sin_family = AF_INET;
798 s.sin_addr.s_addr = addr;
799 #ifdef AFS_DARWIN80_ENV
800 ifad = ifaddr_withnet((struct sockaddr *)&s);
802 ifad = ifa_ifwithnet((struct sockaddr *)&s);
805 #ifdef AFS_DARWIN80_ENV
807 ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
808 *maskp = sr.sin_addr.s_addr;
810 return (ifad ? ifaddr_ifnet(ifad) : NULL);
813 *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
814 return (ifad ? ifad->ifa_ifp : NULL);
818 #else /* DARWIN60 || XBSD */
820 /* Returns ifnet which best matches address */
822 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
825 extern struct in_ifaddr *in_ifaddr;
826 struct in_ifaddr *ifa, *ifad = NULL;
830 #if defined(AFS_DARWIN_ENV)
831 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
832 ifa = TAILQ_NEXT(ifa, ia_link)) {
834 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
836 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
837 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
838 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
843 if (match_value < 3) {
848 if (match_value < 2) {
853 } /* if net matches */
854 } /* for all in_ifaddrs */
858 *maskp = ifad->ia_subnetmask;
859 return (ifad ? ifad->ia_ifp : NULL);
861 #endif /* else DARWIN60 || XBSD */
862 #endif /* else AFS_USERSPACE_IP_ADDR */
863 #endif /* !SUN5 && !SGI62 */
866 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
867 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
868 * system specific subdirectories for all systems. But for the moment,
869 * most of it is simple to follow common code.
871 #if !defined(UKERNEL)
872 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
873 /* rxk_NewSocket creates a new socket on the specified port. The port is
874 * in network byte order.
877 rxk_NewSocketHost(afs_uint32 ahost, short aport)
880 #ifdef AFS_DARWIN80_ENV
883 struct socket *newSocket;
885 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
888 struct sockaddr_in myaddr;
889 #ifdef AFS_HPUX110_ENV
890 /* prototype copied from kernel source file streams/str_proto.h */
891 extern MBLKP allocb_wait(int, int);
893 int addrsize = sizeof(struct sockaddr_in);
895 extern struct fileops socketops;
901 AFS_STATCNT(osi_NewSocket);
902 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
903 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
907 #if defined(AFS_HPUX102_ENV)
908 #if defined(AFS_HPUX110_ENV)
909 /* we need a file associated with the socket so sosend in NetSend
911 /* blocking socket */
912 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
916 fp->f_flag = FREAD | FWRITE;
917 fp->f_type = DTYPE_SOCKET;
918 fp->f_ops = &socketops;
920 fp->f_data = (void *)newSocket;
921 newSocket->so_fp = (void *)fp;
923 #else /* AFS_HPUX110_ENV */
924 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
925 #endif /* else AFS_HPUX110_ENV */
926 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
927 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
928 #elif defined(AFS_FBSD50_ENV)
929 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
930 afs_osi_credp, curthread);
931 #elif defined(AFS_FBSD40_ENV)
932 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
933 #elif defined(AFS_DARWIN80_ENV)
934 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
936 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
937 #endif /* AFS_HPUX102_ENV */
941 memset(&myaddr, 0, sizeof myaddr);
942 myaddr.sin_family = AF_INET;
943 myaddr.sin_port = aport;
944 myaddr.sin_addr.s_addr = ahost;
945 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
946 myaddr.sin_len = sizeof(myaddr);
949 #ifdef AFS_HPUX110_ENV
950 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
955 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
957 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
959 code = sobind(newSocket, bindnam, addrsize);
962 #if !defined(AFS_HPUX1122_ENV)
969 #else /* AFS_HPUX110_ENV */
970 #if defined(AFS_DARWIN80_ENV)
975 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
976 &buflen, sizeof(buflen));
977 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
978 &buflen, sizeof(buflen));
982 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
987 code = soreserve(newSocket, 50000, 50000);
989 code = soreserve(newSocket, 32766, 32766);
991 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
994 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
995 #if defined(AFS_FBSD50_ENV)
996 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
997 #elif defined(AFS_FBSD40_ENV)
998 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
1000 code = sobind(newSocket, (struct sockaddr *)&myaddr);
1003 dpf(("sobind fails (%d)\n", (int)code));
1008 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
1010 nam = m_getclr(M_WAIT, MT_SONAME);
1011 #else /* AFS_OSF_ENV */
1012 nam = m_get(M_WAIT, MT_SONAME);
1015 #if defined(KERNEL_HAVE_UERROR)
1020 nam->m_len = sizeof(myaddr);
1021 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
1022 #if defined(AFS_SGI65_ENV)
1023 BHV_PDATA(&bhv) = (void *)newSocket;
1024 code = sobind(&bhv, nam);
1026 #elif defined(AFS_OBSD44_ENV)
1027 code = sobind(newSocket, nam, osi_curproc());
1029 code = sobind(newSocket, nam);
1032 dpf(("sobind fails (%d)\n", (int)code));
1034 #ifndef AFS_SGI65_ENV
1039 #endif /* else AFS_DARWIN_ENV */
1040 #endif /* else AFS_HPUX110_ENV */
1043 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1044 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1046 return (osi_socket *)newSocket;
1050 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1051 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1053 return (osi_socket *)0;
1057 rxk_NewSocket(short aport)
1059 return rxk_NewSocketHost(0, aport);
1062 /* free socket allocated by rxk_NewSocket */
1064 rxk_FreeSocket(struct socket *asocket)
1066 AFS_STATCNT(osi_FreeSocket);
1067 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1068 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1070 #ifdef AFS_HPUX110_ENV
1071 if (asocket->so_fp) {
1072 struct file *fp = asocket->so_fp;
1073 #if !defined(AFS_HPUX1123_ENV)
1074 /* 11.23 still has falloc, but not FPENTRYFREE !
1075 * so for now if we shutdown, we will waist a file
1078 asocket->so_fp = NULL;
1081 #endif /* AFS_HPUX110_ENV */
1083 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1084 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1088 #endif /* !SUN5 && !LINUX20 */
1090 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1092 * Run RX event daemon every second (5 times faster than rest of systems)
1095 afs_rxevent_daemon(void)
1101 #ifdef RX_ENABLE_LOCKS
1103 #endif /* RX_ENABLE_LOCKS */
1105 rxevent_RaiseEvents(&temp);
1107 #ifdef RX_ENABLE_LOCKS
1109 #endif /* RX_ENABLE_LOCKS */
1110 #ifdef RX_KERNEL_TRACE
1111 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1112 "before afs_osi_Wait()");
1114 afs_osi_Wait(500, NULL, 0);
1115 #ifdef RX_KERNEL_TRACE
1116 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1117 "after afs_osi_Wait()");
1119 if (afs_termState == AFSOP_STOP_RXEVENT) {
1120 #ifdef RXK_LISTENER_ENV
1121 afs_termState = AFSOP_STOP_RXK_LISTENER;
1123 #ifdef AFS_SUN510_ENV
1124 afs_termState = AFSOP_STOP_NETIF;
1126 afs_termState = AFSOP_STOP_COMPLETE;
1129 osi_rxWakeup(&afs_termState);
1136 #ifdef RXK_LISTENER_ENV
1138 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1140 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1143 struct sockaddr_in from;
1147 afs_int32 savelen; /* was using rlen but had aliasing problems */
1148 rx_computelen(p, tlen);
1149 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1151 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1152 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1153 * it once in order to avoid races. */
1156 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1164 /* add some padding to the last iovec, it's just to make sure that the
1165 * read doesn't return more data than we expect, and is done to get around
1166 * our problems caused by the lack of a length field in the rx header. */
1167 savelen = p->wirevec[p->niovecs - 1].iov_len;
1168 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1170 nbytes = tlen + sizeof(afs_int32);
1171 #ifdef RX_KERNEL_TRACE
1172 if (ICL_SETACTIVE(afs_iclSetp)) {
1174 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1175 "before osi_NetRecive()");
1179 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1181 #ifdef RX_KERNEL_TRACE
1182 if (ICL_SETACTIVE(afs_iclSetp)) {
1184 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1185 "after osi_NetRecive()");
1189 /* restore the vec to its correct state */
1190 p->wirevec[p->niovecs - 1].iov_len = savelen;
1193 p->length = nbytes - RX_HEADER_SIZE;;
1194 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1196 if (rx_stats_active) {
1197 MUTEX_ENTER(&rx_stats_mutex);
1198 rx_stats.bogusPacketOnRead++;
1199 rx_stats.bogusHost = from.sin_addr.s_addr;
1200 MUTEX_EXIT(&rx_stats_mutex);
1202 dpf(("B: bogus packet from [%x,%d] nb=%d",
1203 from.sin_addr.s_addr, from.sin_port, nbytes));
1207 /* Extract packet header. */
1208 rxi_DecodePacketHeader(p);
1210 *host = from.sin_addr.s_addr;
1211 *port = from.sin_port;
1212 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1213 if (rx_stats_active) {
1214 MUTEX_ENTER(&rx_stats_mutex);
1215 rx_stats.packetsRead[p->header.type - 1]++;
1216 MUTEX_EXIT(&rx_stats_mutex);
1220 #ifdef RX_TRIMDATABUFS
1221 /* Free any empty packet buffers at the end of this packet */
1222 rxi_TrimDataBufs(p, 1);
1232 * Listen for packets on socket. This thread is typically started after
1233 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1234 * the start state is set before proceeding.
1236 * Note that this thread is outside the AFS global lock for much of
1239 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1240 * we want here. So we need to either block all signals (including SIGKILL
1241 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1242 * OS's socket receive routine returns as a result of a signal.
1244 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1245 #ifdef AFS_LINUX20_ENV
1246 struct task_struct *rxk_ListenerTask;
1251 * Run the listener as a kernel thread.
1257 void rxk_ListenerProc(void);
1259 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1260 minclsyspri) == NULL)
1261 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1265 rxk_ListenerProc(void)
1266 #else /* AFS_SUN5_ENV */
1269 #endif /* AFS_SUN5_ENV */
1271 struct rx_packet *rxp = NULL;
1275 #ifdef AFS_LINUX20_ENV
1276 rxk_ListenerPid = current->pid;
1277 rxk_ListenerTask = current;
1280 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1281 #endif /* AFS_SUN5_ENV */
1283 rxk_ListenerPid = curproc->p_pid;
1284 #endif /* AFS_FBSD_ENV */
1285 #ifdef AFS_DARWIN80_ENV
1286 rxk_ListenerPid = proc_selfpid();
1287 #elif defined(AFS_DARWIN_ENV)
1288 rxk_ListenerPid = current_proc()->p_pid;
1290 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1292 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1293 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1295 rxi_RestoreDataBufs(rxp);
1297 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1299 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1301 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1302 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1306 #ifdef RX_ENABLE_LOCKS
1308 #endif /* RX_ENABLE_LOCKS */
1309 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1310 #ifdef AFS_SUN510_ENV
1311 afs_termState = AFSOP_STOP_NETIF;
1313 afs_termState = AFSOP_STOP_COMPLETE;
1315 osi_rxWakeup(&afs_termState);
1317 rxk_ListenerPid = 0;
1318 #ifdef AFS_LINUX20_ENV
1319 rxk_ListenerTask = 0;
1320 osi_rxWakeup(&rxk_ListenerTask);
1322 #if defined(AFS_SUN5_ENV)
1323 osi_rxWakeup(&rxk_ListenerPid);
1327 #endif /* AFS_SUN5_ENV */
1330 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1331 /* The manner of stopping the rx listener thread may vary. Most unix's should
1332 * be able to call soclose.
1335 osi_StopListener(void)
1340 #endif /* RXK_LISTENER_ENV */
1342 #endif /* !NCR && !UKERNEL */