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"
20 #include "rx/rx_kcommon.h"
22 #ifdef AFS_HPUX110_ENV
28 #ifndef RXK_LISTENER_ENV
29 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
30 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
33 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
34 extern struct interfaceAddr afs_cb_interface;
36 rxk_ports_t rxk_ports;
37 rxk_portRocks_t rxk_portRocks;
41 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
42 #define ADDRSPERSITE 16
43 static afs_uint32 myNetAddrs[ADDRSPERSITE];
44 static int myNetMTUs[ADDRSPERSITE];
45 static int numMyNetAddrs = 0;
48 #if defined(AFS_DARWIN80_ENV)
49 #define sobind sock_bind
50 #define soclose sock_close
53 /* add a port to the monitored list, port # is in network order */
55 rxk_AddPort(u_short aport, char *arock)
58 unsigned short *tsp, ts;
61 zslot = -1; /* look for an empty slot simultaneously */
62 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
63 if (((ts = *tsp) == 0) && (zslot == -1))
69 /* otherwise allocate a new port slot */
71 return E2BIG; /* all full */
72 rxk_ports[zslot] = aport;
73 rxk_portRocks[zslot] = arock;
77 /* remove as port from the monitored list, port # is in network order */
79 rxk_DelPort(u_short aport)
82 register unsigned short *tsp;
84 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
86 /* found it, adjust ref count and free the port reference if all gone */
91 /* otherwise port not found */
96 rxk_shutdownPorts(void)
99 for (i = 0; i < MAXRXPORTS; i++) {
102 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
103 soclose((struct socket *)rxk_portRocks[i]);
105 rxk_portRocks[i] = NULL;
111 rxi_GetHostUDPSocket(u_int host, u_short port)
114 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
115 if (sockp == (osi_socket *)0)
116 return OSI_NULLSOCKET;
117 rxk_AddPort(port, (char *)sockp);
118 return (osi_socket) sockp;
122 rxi_GetUDPSocket(u_short port)
124 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
127 #if !defined(AFS_LINUX26_ENV)
130 osi_Panic(char *msg, void *a1, void *a2, void *a3)
132 osi_Panic(char *msg, ...)
137 msg = "Unknown AFS panic";
138 printf(msg, a1, a2, a3);
142 msg = "Unknown AFS panic";
147 #ifdef AFS_LINUX20_ENV
155 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
156 * unsigned long value into the given buffer. Returns 0 on success,
157 * and a value less than 0 on failure. The contents of the buffer is
158 * defined only on success.
162 osi_utoa(char *buf, size_t len, unsigned long val)
164 long k; /* index of first byte of string value */
166 /* we definitely need room for at least one digit and NUL */
172 /* compute the string form from the high end of the buffer */
175 for (k = len - 2; k >= 0; k--) {
176 buf[k] = val % 10 + '0';
183 /* did we finish converting val to string form? */
189 /* this should never happen */
195 /* this should never happen */
201 /* if necessary, relocate string to beginning of buf[] */
206 * We need to achieve the effect of calling
208 * memmove(buf, &buf[k], len - k);
210 * However, since memmove() is not available in all
211 * kernels, we explicitly do an appropriate copy.
217 while ((*dst++ = *src++) != '\0')
225 * osi_AssertFailK() -- used by the osi_Assert() macro.
227 * It essentially does
229 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
231 * Since the kernel version of osi_Panic() only passes its first
232 * argument to the native panic(), we construct a single string and hand
233 * that to osi_Panic().
236 osi_AssertFailK(const char *expr, const char *file, int line)
238 static const char msg0[] = "assertion failed: ";
239 static const char msg1[] = ", file: ";
240 static const char msg2[] = ", line: ";
241 static const char msg3[] = "\n";
244 * These buffers add up to 1K, which is a pleasantly nice round
245 * value, but probably not vital.
250 /* check line number conversion */
252 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
253 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
258 #define ADDBUF(BUF, STR) \
259 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
260 strcat(BUF, (char *)(STR)); \
269 ADDBUF(buf, linebuf);
278 osi_AssertFailK(const char *expr, const char *file, int line)
280 printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
285 /* This is the server process request loop. Kernel server
286 * processes never become listener threads */
288 rx_ServerProc(void *unused)
292 /* jaltman - rxi_dataQuota is protected by a mutex everywhere else */
293 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
294 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
295 /* threadID is used for making decisions in GetCall. Get it by bumping
296 * number of threads handling incoming calls */
297 threadID = rxi_availProcs++;
299 #ifdef RX_ENABLE_LOCKS
301 #endif /* RX_ENABLE_LOCKS */
302 rxi_ServerProc(threadID, NULL, NULL);
303 #ifdef RX_ENABLE_LOCKS
305 #endif /* RX_ENABLE_LOCKS */
309 #endif /* !UKERNEL */
311 #ifndef RXK_LISTENER_ENV
312 /* asize includes the Rx header */
314 MyPacketProc(struct rx_packet **ahandle, int asize)
316 struct rx_packet *tp;
318 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
319 /* If we can't scrounge enough cbufs, then we have to drop the packet,
320 * but we should set a flag so we magic up some more at our leisure.
323 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
324 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
325 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
327 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
328 RX_PACKET_CLASS_RECV_CBUF)) {
331 MUTEX_ENTER(&rx_stats_mutex);
332 rx_stats.noPacketBuffersOnRead++;
333 MUTEX_EXIT(&rx_stats_mutex);
338 * XXX if packet is too long for our buffer,
339 * should do this at a higher layer and let other
340 * end know we're losing.
342 MUTEX_ENTER(&rx_stats_mutex);
343 rx_stats.bogusPacketOnRead++;
344 MUTEX_EXIT(&rx_stats_mutex);
345 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
346 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
352 /* otherwise we have a packet, set appropriate values */
358 MyArrivalProc(struct rx_packet *ahandle,
359 struct sockaddr_in *afrom,
360 struct socket *arock,
363 /* handle basic rx packet */
364 ahandle->length = asize - RX_HEADER_SIZE;
365 rxi_DecodePacketHeader(ahandle);
367 rxi_ReceivePacket(ahandle, arock,
368 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
371 /* free the packet if it has been returned */
373 rxi_FreePacket(ahandle);
376 #endif /* !RXK_LISTENER_ENV */
379 rxi_StartListener(void)
381 /* if kernel, give name of appropriate procedures */
382 #ifndef RXK_LISTENER_ENV
383 rxk_GetPacketProc = MyPacketProc;
384 rxk_PacketArrivalProc = MyArrivalProc;
389 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
390 to get interesting information. */
392 rxi_InitPeerParams(register struct rx_peer *pp)
399 #ifdef AFS_USERSPACE_IP_ADDR
400 i = rxi_Findcbi(pp->host);
403 /* pp->timeout.usec = 0; */
404 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
407 /* pp->timeout.usec = 0; */
408 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
411 mtu = ntohl(afs_cb_interface.mtu[i]);
412 /* Diminish the packet size to one based on the MTU given by
414 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
415 rxmtu = mtu - RX_IPUDP_SIZE;
416 if (rxmtu < pp->ifMTU)
419 } else { /* couldn't find the interface, so assume the worst */
420 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
422 #else /* AFS_USERSPACE_IP_ADDR */
425 #if !defined(AFS_SGI62_ENV)
426 if (numMyNetAddrs == 0)
427 (void)rxi_GetIFInfo();
430 ifn = rxi_FindIfnet(pp->host, NULL);
433 /* pp->timeout.usec = 0; */
434 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
435 #ifdef IFF_POINTOPOINT
436 if (ifnet_flags(ifn) & IFF_POINTOPOINT) {
437 /* wish we knew the bit rate and the chunk size, sigh. */
439 pp->ifMTU = RX_PP_PACKET_SIZE;
441 #endif /* IFF_POINTOPOINT */
442 /* Diminish the packet size to one based on the MTU given by
444 if (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
445 rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
446 if (rxmtu < pp->ifMTU)
449 } else { /* couldn't find the interface, so assume the worst */
451 /* pp->timeout.usec = 0; */
452 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
454 #endif /* else AFS_USERSPACE_IP_ADDR */
455 #else /* AFS_SUN5_ENV */
456 mtu = rxi_FindIfMTU(pp->host);
460 /* pp->timeout.usec = 0; */
461 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
464 /* pp->timeout.usec = 0; */
465 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
469 /* Diminish the packet size to one based on the MTU given by
471 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
472 rxmtu = mtu - RX_IPUDP_SIZE;
473 if (rxmtu < pp->ifMTU)
476 } else { /* couldn't find the interface, so assume the worst */
477 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
479 #endif /* AFS_SUN5_ENV */
480 #else /* ADAPT_MTU */
481 pp->rateFlag = 2; /* start timing after two full packets */
483 pp->ifMTU = OLD_MAX_PACKET_SIZE;
484 #endif /* else ADAPT_MTU */
485 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
486 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
487 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
489 MIN(rxi_nDgramPackets,
490 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
491 pp->maxDgramPackets = 1;
493 /* Initialize slow start parameters */
494 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
496 pp->nDgramPackets = 1;
501 /* The following code is common to several system types, but not all. The
502 * separate ones are found in the system specific subdirectories.
506 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
507 /* Routine called during the afsd "-shutdown" process to put things back to
510 static struct protosw parent_proto; /* udp proto switch */
513 shutdown_rxkernel(void)
515 register struct protosw *tpro, *last;
516 last = inetdomain.dom_protoswNPROTOSW;
517 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
518 if (tpro->pr_protocol == IPPROTO_UDP) {
519 /* restore original udp protocol switch */
520 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
521 memset((void *)&parent_proto, 0, sizeof(parent_proto));
526 dpf(("shutdown_rxkernel: no udp proto"));
528 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
530 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
531 /* Determine what the network interfaces are for this machine. */
533 #ifdef AFS_USERSPACE_IP_ADDR
537 int i, j, different = 0, num = ADDRSPERSITE;
540 afs_uint32 addrs[ADDRSPERSITE];
541 int mtus[ADDRSPERSITE];
543 memset((void *)addrs, 0, sizeof(addrs));
544 memset((void *)mtus, 0, sizeof(mtus));
546 if (afs_cb_interface.numberOfInterfaces < num)
547 num = afs_cb_interface.numberOfInterfaces;
548 for (i = 0; i < num; i++) {
549 if (!afs_cb_interface.mtu[i])
550 afs_cb_interface.mtu[i] = htonl(1500);
551 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
552 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
553 if (myNetAddrs[i] != ifinaddr)
557 rxmtu = rxi_AdjustIfMTU(rxmtu);
559 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
560 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
561 addrs[i++] = ifinaddr;
562 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
563 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
564 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
568 rx_maxJumboRecvSize =
569 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
570 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
571 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
574 for (j = 0; j < i; j++) {
575 myNetMTUs[j] = mtus[j];
576 myNetAddrs[j] = addrs[j];
583 /* Returns the afs_cb_interface inxex which best matches address.
584 * If none is found, we return -1.
587 rxi_Findcbi(afs_uint32 addr)
590 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
591 afs_int32 rvalue = -1;
594 if (numMyNetAddrs == 0)
595 (void)rxi_GetcbiInfo();
597 myAddr = ntohl(addr);
599 if (IN_CLASSA(myAddr))
600 netMask = IN_CLASSA_NET;
601 else if (IN_CLASSB(myAddr))
602 netMask = IN_CLASSB_NET;
603 else if (IN_CLASSC(myAddr))
604 netMask = IN_CLASSC_NET;
608 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
609 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
610 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
611 if ((myAddr & netMask) == (thisAddr & netMask)) {
612 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
613 if (myAddr == thisAddr) {
618 if (match_value < 3) {
623 if (match_value < 2) {
634 #else /* AFS_USERSPACE_IP_ADDR */
636 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
637 #define IFADDR2SA(f) (&((f)->ifa_addr))
638 #else /* AFS_AIX41_ENV */
639 #define IFADDR2SA(f) ((f)->ifa_addr)
648 register int rxmtu, maxmtu;
649 afs_uint32 addrs[ADDRSPERSITE];
650 int mtus[ADDRSPERSITE];
652 #if defined(AFS_DARWIN80_ENV)
658 struct sockaddr sout;
659 struct sockaddr_in *sin;
662 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
663 register struct ifnet *ifn;
666 memset(addrs, 0, sizeof(addrs));
667 memset(mtus, 0, sizeof(mtus));
669 #if defined(AFS_DARWIN80_ENV)
670 if (!ifnet_list_get(AF_INET, &ifn, &count)) {
671 for (m = 0; m < count; m++) {
672 if (!ifnet_get_address_list(ifn[m], &ifads)) {
673 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
674 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
675 sin = (struct sockaddr_in *)&sout;
676 rxmtu = ifnet_mtu(ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
677 ifinaddr = ntohl(sin->sin_addr.s_addr);
678 if (myNetAddrs[i] != ifinaddr) {
682 rxmtu = rxi_AdjustIfMTU(rxmtu);
684 rxmtu * rxi_nRecvFrags +
685 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
686 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
687 addrs[i++] = ifinaddr;
688 if ((ifinaddr != 0x7f000001) &&
689 (maxmtu > rx_maxReceiveSize)) {
691 MIN(RX_MAX_PACKET_SIZE, maxmtu);
693 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
698 ifnet_free_address_list(ifads);
701 ifnet_list_free(ifn);
704 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
705 TAILQ_FOREACH(ifn, &ifnet, if_link) {
706 if (i >= ADDRSPERSITE)
708 #elif defined(AFS_OBSD_ENV)
709 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
710 ifn = ifn->if_list.tqe_next) {
712 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
714 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
715 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
716 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
717 if (i >= ADDRSPERSITE)
719 #elif defined(AFS_OBSD_ENV)
720 for (ifad = ifn->if_addrlist.tqh_first;
721 ifad != NULL && i < ADDRSPERSITE;
722 ifad = ifad->ifa_list.tqe_next) {
724 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
725 ifad = ifad->ifa_next) {
727 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
729 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
731 if (myNetAddrs[i] != ifinaddr) {
735 rxmtu = rxi_AdjustIfMTU(rxmtu);
737 rxmtu * rxi_nRecvFrags +
738 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
739 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
740 addrs[i++] = ifinaddr;
741 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
742 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
744 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
751 rx_maxJumboRecvSize =
752 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
753 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
754 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
758 for (l = 0; l < i; l++) {
759 myNetMTUs[l] = mtus[l];
760 myNetAddrs[l] = addrs[l];
766 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
767 /* Returns ifnet which best matches address */
768 #ifdef AFS_DARWIN80_ENV
773 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
775 struct sockaddr_in s, sr;
776 #ifdef AFS_DARWIN80_ENV
782 s.sin_family = AF_INET;
783 s.sin_addr.s_addr = addr;
784 #ifdef AFS_DARWIN80_ENV
785 ifad = ifaddr_withnet((struct sockaddr *)&s);
787 ifad = ifa_ifwithnet((struct sockaddr *)&s);
790 #ifdef AFS_DARWIN80_ENV
792 ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
793 *maskp = sr.sin_addr.s_addr;
795 return (ifad ? ifaddr_ifnet(ifad) : NULL);
798 *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
799 return (ifad ? ifad->ifa_ifp : NULL);
803 #else /* DARWIN60 || XBSD */
805 /* Returns ifnet which best matches address */
807 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
810 extern struct in_ifaddr *in_ifaddr;
811 struct in_ifaddr *ifa, *ifad = NULL;
815 #if defined(AFS_DARWIN_ENV)
816 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
817 ifa = TAILQ_NEXT(ifa, ia_link)) {
819 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
821 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
822 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
823 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
828 if (match_value < 3) {
833 if (match_value < 2) {
838 } /* if net matches */
839 } /* for all in_ifaddrs */
843 *maskp = ifad->ia_subnetmask;
844 return (ifad ? ifad->ia_ifp : NULL);
846 #endif /* else DARWIN60 || XBSD */
847 #endif /* else AFS_USERSPACE_IP_ADDR */
848 #endif /* !SUN5 && !SGI62 */
851 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
852 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
853 * system specific subdirectories for all systems. But for the moment,
854 * most of it is simple to follow common code.
856 #if !defined(UKERNEL)
857 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
858 /* rxk_NewSocket creates a new socket on the specified port. The port is
859 * in network byte order.
862 rxk_NewSocketHost(afs_uint32 ahost, short aport)
864 register afs_int32 code;
865 #ifdef AFS_DARWIN80_ENV
868 struct socket *newSocket;
870 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
873 struct sockaddr_in myaddr;
874 #ifdef AFS_HPUX110_ENV
875 /* prototype copied from kernel source file streams/str_proto.h */
876 extern MBLKP allocb_wait(int, int);
878 int addrsize = sizeof(struct sockaddr_in);
880 extern struct fileops socketops;
886 AFS_STATCNT(osi_NewSocket);
887 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
888 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
892 #if defined(AFS_HPUX102_ENV)
893 #if defined(AFS_HPUX110_ENV)
894 /* we need a file associated with the socket so sosend in NetSend
896 /* blocking socket */
897 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
901 fp->f_flag = FREAD | FWRITE;
902 fp->f_type = DTYPE_SOCKET;
903 fp->f_ops = &socketops;
905 fp->f_data = (void *)newSocket;
906 newSocket->so_fp = (void *)fp;
908 #else /* AFS_HPUX110_ENV */
909 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
910 #endif /* else AFS_HPUX110_ENV */
911 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
912 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
913 #elif defined(AFS_FBSD50_ENV)
914 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
915 afs_osi_credp, curthread);
916 #elif defined(AFS_FBSD40_ENV)
917 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
918 #elif defined(AFS_DARWIN80_ENV)
919 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
921 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
922 #endif /* AFS_HPUX102_ENV */
926 memset(&myaddr, 0, sizeof myaddr);
927 myaddr.sin_family = AF_INET;
928 myaddr.sin_port = aport;
929 myaddr.sin_addr.s_addr = ahost;
930 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
931 myaddr.sin_len = sizeof(myaddr);
934 #ifdef AFS_HPUX110_ENV
935 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
940 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
942 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
944 code = sobind(newSocket, bindnam, addrsize);
947 #if !defined(AFS_HPUX1122_ENV)
954 #else /* AFS_HPUX110_ENV */
955 #if defined(AFS_DARWIN80_ENV)
960 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
961 &buflen, sizeof(buflen));
962 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
963 &buflen, sizeof(buflen));
967 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
972 code = soreserve(newSocket, 50000, 50000);
974 code = soreserve(newSocket, 32766, 32766);
976 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
979 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
980 #if defined(AFS_FBSD50_ENV)
981 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
982 #elif defined(AFS_FBSD40_ENV)
983 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
985 code = sobind(newSocket, (struct sockaddr *)&myaddr);
988 dpf(("sobind fails (%d)\n", (int)code));
993 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
995 nam = m_getclr(M_WAIT, MT_SONAME);
996 #else /* AFS_OSF_ENV */
997 nam = m_get(M_WAIT, MT_SONAME);
1000 #if defined(KERNEL_HAVE_UERROR)
1005 nam->m_len = sizeof(myaddr);
1006 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
1007 #ifdef AFS_SGI65_ENV
1008 BHV_PDATA(&bhv) = (void *)newSocket;
1009 code = sobind(&bhv, nam);
1012 code = sobind(newSocket, nam);
1015 dpf(("sobind fails (%d)\n", (int)code));
1017 #ifndef AFS_SGI65_ENV
1022 #endif /* else AFS_DARWIN_ENV */
1023 #endif /* else AFS_HPUX110_ENV */
1026 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1027 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1029 return (osi_socket *)newSocket;
1033 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1034 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1036 return (osi_socket *)0;
1040 rxk_NewSocket(short aport)
1042 return rxk_NewSocketHost(0, aport);
1045 /* free socket allocated by rxk_NewSocket */
1047 rxk_FreeSocket(register struct socket *asocket)
1049 AFS_STATCNT(osi_FreeSocket);
1050 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1051 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1053 #ifdef AFS_HPUX110_ENV
1054 if (asocket->so_fp) {
1055 struct file *fp = asocket->so_fp;
1056 #if !defined(AFS_HPUX1123_ENV)
1057 /* 11.23 still has falloc, but not FPENTRYFREE !
1058 * so for now if we shutdown, we will waist a file
1061 asocket->so_fp = NULL;
1064 #endif /* AFS_HPUX110_ENV */
1066 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1067 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1071 #endif /* !SUN5 && !LINUX20 */
1073 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1075 * Run RX event daemon every second (5 times faster than rest of systems)
1078 afs_rxevent_daemon(void)
1084 #ifdef RX_ENABLE_LOCKS
1086 #endif /* RX_ENABLE_LOCKS */
1088 rxevent_RaiseEvents(&temp);
1090 #ifdef RX_ENABLE_LOCKS
1092 #endif /* RX_ENABLE_LOCKS */
1093 #ifdef RX_KERNEL_TRACE
1094 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1095 "before afs_osi_Wait()");
1097 afs_osi_Wait(500, NULL, 0);
1098 #ifdef RX_KERNEL_TRACE
1099 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1100 "after afs_osi_Wait()");
1102 if (afs_termState == AFSOP_STOP_RXEVENT) {
1103 #ifdef RXK_LISTENER_ENV
1104 afs_termState = AFSOP_STOP_RXK_LISTENER;
1106 #ifdef AFS_SUN510_ENV
1107 afs_termState = AFSOP_STOP_NETIF;
1109 afs_termState = AFSOP_STOP_COMPLETE;
1112 osi_rxWakeup(&afs_termState);
1119 #ifdef RXK_LISTENER_ENV
1121 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1123 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1126 struct sockaddr_in from;
1129 register afs_int32 tlen;
1130 afs_int32 savelen; /* was using rlen but had aliasing problems */
1131 rx_computelen(p, tlen);
1132 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1134 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1135 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1136 * it once in order to avoid races. */
1139 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1147 /* add some padding to the last iovec, it's just to make sure that the
1148 * read doesn't return more data than we expect, and is done to get around
1149 * our problems caused by the lack of a length field in the rx header. */
1150 savelen = p->wirevec[p->niovecs - 1].iov_len;
1151 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1153 nbytes = tlen + sizeof(afs_int32);
1154 #ifdef RX_KERNEL_TRACE
1155 if (ICL_SETACTIVE(afs_iclSetp)) {
1157 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1158 "before osi_NetRecive()");
1162 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1164 #ifdef RX_KERNEL_TRACE
1165 if (ICL_SETACTIVE(afs_iclSetp)) {
1167 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1168 "after osi_NetRecive()");
1172 /* restore the vec to its correct state */
1173 p->wirevec[p->niovecs - 1].iov_len = savelen;
1176 p->length = nbytes - RX_HEADER_SIZE;;
1177 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1179 MUTEX_ENTER(&rx_stats_mutex);
1180 rx_stats.bogusPacketOnRead++;
1181 rx_stats.bogusHost = from.sin_addr.s_addr;
1182 MUTEX_EXIT(&rx_stats_mutex);
1183 dpf(("B: bogus packet from [%x,%d] nb=%d",
1184 from.sin_addr.s_addr, from.sin_port, nbytes));
1188 /* Extract packet header. */
1189 rxi_DecodePacketHeader(p);
1191 *host = from.sin_addr.s_addr;
1192 *port = from.sin_port;
1193 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1194 MUTEX_ENTER(&rx_stats_mutex);
1195 rx_stats.packetsRead[p->header.type - 1]++;
1196 MUTEX_EXIT(&rx_stats_mutex);
1199 /* Free any empty packet buffers at the end of this packet */
1200 rxi_TrimDataBufs(p, 1);
1210 * Listen for packets on socket. This thread is typically started after
1211 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1212 * the start state is set before proceeding.
1214 * Note that this thread is outside the AFS global lock for much of
1217 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1218 * we want here. So we need to either block all signals (including SIGKILL
1219 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1220 * OS's socket receive routine returns as a result of a signal.
1222 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1223 #ifdef AFS_LINUX20_ENV
1224 struct task_struct *rxk_ListenerTask;
1229 * Run the listener as a kernel thread.
1235 void rxk_ListenerProc(void);
1237 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1238 minclsyspri) == NULL)
1239 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1243 rxk_ListenerProc(void)
1244 #else /* AFS_SUN5_ENV */
1247 #endif /* AFS_SUN5_ENV */
1249 struct rx_packet *rxp = NULL;
1253 #ifdef AFS_LINUX20_ENV
1254 rxk_ListenerPid = current->pid;
1255 rxk_ListenerTask = current;
1258 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1259 #endif /* AFS_SUN5_ENV */
1261 rxk_ListenerPid = curproc->p_pid;
1262 #endif /* AFS_FBSD_ENV */
1263 #ifdef AFS_DARWIN80_ENV
1264 rxk_ListenerPid = proc_selfpid();
1265 #elif defined(AFS_DARWIN_ENV)
1266 rxk_ListenerPid = current_proc()->p_pid;
1268 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1270 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1271 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1273 rxi_RestoreDataBufs(rxp);
1275 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1277 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1279 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1280 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1284 #ifdef RX_ENABLE_LOCKS
1286 #endif /* RX_ENABLE_LOCKS */
1287 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1288 #ifdef AFS_SUN510_ENV
1289 afs_termState = AFSOP_STOP_NETIF;
1291 afs_termState = AFSOP_STOP_COMPLETE;
1293 osi_rxWakeup(&afs_termState);
1295 rxk_ListenerPid = 0;
1296 #ifdef AFS_LINUX20_ENV
1297 rxk_ListenerTask = 0;
1298 osi_rxWakeup(&rxk_ListenerTask);
1300 #if defined(AFS_SUN5_ENV)
1301 osi_rxWakeup(&rxk_ListenerPid);
1305 #endif /* AFS_SUN5_ENV */
1308 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1309 /* The manner of stopping the rx listener thread may vary. Most unix's should
1310 * be able to call soclose.
1313 osi_StopListener(void)
1318 #endif /* RXK_LISTENER_ENV */
1320 #endif /* !NCR && !UKERNEL */