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)
129 osi_Panic(char *msg, ...)
133 msg = "Unknown AFS panic";
137 #ifdef AFS_LINUX20_ENV
145 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
146 * unsigned long value into the given buffer. Returns 0 on success,
147 * and a value less than 0 on failure. The contents of the buffer is
148 * defined only on success.
152 osi_utoa(char *buf, size_t len, unsigned long val)
154 long k; /* index of first byte of string value */
156 /* we definitely need room for at least one digit and NUL */
162 /* compute the string form from the high end of the buffer */
165 for (k = len - 2; k >= 0; k--) {
166 buf[k] = val % 10 + '0';
173 /* did we finish converting val to string form? */
179 /* this should never happen */
185 /* this should never happen */
191 /* if necessary, relocate string to beginning of buf[] */
196 * We need to achieve the effect of calling
198 * memmove(buf, &buf[k], len - k);
200 * However, since memmove() is not available in all
201 * kernels, we explicitly do an appropriate copy.
207 while ((*dst++ = *src++) != '\0')
215 * osi_AssertFailK() -- used by the osi_Assert() macro.
217 * It essentially does
219 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
221 * Since the kernel version of osi_Panic() only passes its first
222 * argument to the native panic(), we construct a single string and hand
223 * that to osi_Panic().
226 osi_AssertFailK(const char *expr, const char *file, int line)
228 static const char msg0[] = "assertion failed: ";
229 static const char msg1[] = ", file: ";
230 static const char msg2[] = ", line: ";
231 static const char msg3[] = "\n";
234 * These buffers add up to 1K, which is a pleasantly nice round
235 * value, but probably not vital.
240 /* check line number conversion */
242 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
243 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
248 #define ADDBUF(BUF, STR) \
249 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
250 strcat(BUF, (char *)(STR)); \
259 ADDBUF(buf, linebuf);
268 osi_AssertFailK(const char *expr, const char *file, int line)
270 printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
275 /* This is the server process request loop. Kernel server
276 * processes never become listener threads */
278 rx_ServerProc(void *unused)
282 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
283 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
284 /* threadID is used for making decisions in GetCall. Get it by bumping
285 * number of threads handling incoming calls */
286 threadID = rxi_availProcs++;
288 #ifdef RX_ENABLE_LOCKS
290 #endif /* RX_ENABLE_LOCKS */
291 rxi_ServerProc(threadID, NULL, NULL);
292 #ifdef RX_ENABLE_LOCKS
294 #endif /* RX_ENABLE_LOCKS */
298 #endif /* !UKERNEL */
300 #ifndef RXK_LISTENER_ENV
301 /* asize includes the Rx header */
303 MyPacketProc(struct rx_packet **ahandle, int asize)
305 struct rx_packet *tp;
307 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
308 /* If we can't scrounge enough cbufs, then we have to drop the packet,
309 * but we should set a flag so we magic up some more at our leisure.
312 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
313 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
314 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
316 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
317 RX_PACKET_CLASS_RECV_CBUF)) {
320 MUTEX_ENTER(&rx_stats_mutex);
321 rx_stats.noPacketBuffersOnRead++;
322 MUTEX_EXIT(&rx_stats_mutex);
327 * XXX if packet is too long for our buffer,
328 * should do this at a higher layer and let other
329 * end know we're losing.
331 MUTEX_ENTER(&rx_stats_mutex);
332 rx_stats.bogusPacketOnRead++;
333 MUTEX_EXIT(&rx_stats_mutex);
334 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
335 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
341 /* otherwise we have a packet, set appropriate values */
347 MyArrivalProc(struct rx_packet *ahandle,
348 struct sockaddr_in *afrom,
349 struct socket *arock,
352 /* handle basic rx packet */
353 ahandle->length = asize - RX_HEADER_SIZE;
354 rxi_DecodePacketHeader(ahandle);
356 rxi_ReceivePacket(ahandle, arock,
357 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
360 /* free the packet if it has been returned */
362 rxi_FreePacket(ahandle);
365 #endif /* !RXK_LISTENER_ENV */
368 rxi_StartListener(void)
370 /* if kernel, give name of appropriate procedures */
371 #ifndef RXK_LISTENER_ENV
372 rxk_GetPacketProc = MyPacketProc;
373 rxk_PacketArrivalProc = MyArrivalProc;
378 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
379 to get interesting information. */
381 rxi_InitPeerParams(register struct rx_peer *pp)
388 #ifdef AFS_USERSPACE_IP_ADDR
389 i = rxi_Findcbi(pp->host);
392 /* pp->timeout.usec = 0; */
393 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
396 /* pp->timeout.usec = 0; */
397 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
400 mtu = ntohl(afs_cb_interface.mtu[i]);
401 /* Diminish the packet size to one based on the MTU given by
403 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
404 rxmtu = mtu - RX_IPUDP_SIZE;
405 if (rxmtu < pp->ifMTU)
408 } else { /* couldn't find the interface, so assume the worst */
409 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
411 #else /* AFS_USERSPACE_IP_ADDR */
414 #if !defined(AFS_SGI62_ENV)
415 if (numMyNetAddrs == 0)
416 (void)rxi_GetIFInfo();
419 ifn = rxi_FindIfnet(pp->host, NULL);
422 /* pp->timeout.usec = 0; */
423 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
424 #ifdef IFF_POINTOPOINT
425 if (ifnet_flags(ifn) & IFF_POINTOPOINT) {
426 /* wish we knew the bit rate and the chunk size, sigh. */
428 pp->ifMTU = RX_PP_PACKET_SIZE;
430 #endif /* IFF_POINTOPOINT */
431 /* Diminish the packet size to one based on the MTU given by
433 if (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
434 rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
435 if (rxmtu < pp->ifMTU)
438 } else { /* couldn't find the interface, so assume the worst */
440 /* pp->timeout.usec = 0; */
441 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
443 #endif /* else AFS_USERSPACE_IP_ADDR */
444 #else /* AFS_SUN5_ENV */
445 mtu = rxi_FindIfMTU(pp->host);
449 /* pp->timeout.usec = 0; */
450 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
453 /* pp->timeout.usec = 0; */
454 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
458 /* Diminish the packet size to one based on the MTU given by
460 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
461 rxmtu = mtu - RX_IPUDP_SIZE;
462 if (rxmtu < pp->ifMTU)
465 } else { /* couldn't find the interface, so assume the worst */
466 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
468 #endif /* AFS_SUN5_ENV */
469 #else /* ADAPT_MTU */
470 pp->rateFlag = 2; /* start timing after two full packets */
472 pp->ifMTU = OLD_MAX_PACKET_SIZE;
473 #endif /* else ADAPT_MTU */
474 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
475 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
476 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
478 MIN(rxi_nDgramPackets,
479 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
480 pp->maxDgramPackets = 1;
482 /* Initialize slow start parameters */
483 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
485 pp->nDgramPackets = 1;
490 /* The following code is common to several system types, but not all. The
491 * separate ones are found in the system specific subdirectories.
495 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
496 /* Routine called during the afsd "-shutdown" process to put things back to
499 static struct protosw parent_proto; /* udp proto switch */
502 shutdown_rxkernel(void)
504 register struct protosw *tpro, *last;
505 last = inetdomain.dom_protoswNPROTOSW;
506 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
507 if (tpro->pr_protocol == IPPROTO_UDP) {
508 /* restore original udp protocol switch */
509 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
510 memset((void *)&parent_proto, 0, sizeof(parent_proto));
515 dpf(("shutdown_rxkernel: no udp proto"));
517 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
519 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
520 /* Determine what the network interfaces are for this machine. */
522 #ifdef AFS_USERSPACE_IP_ADDR
526 int i, j, different = 0, num = ADDRSPERSITE;
529 afs_uint32 addrs[ADDRSPERSITE];
530 int mtus[ADDRSPERSITE];
532 memset((void *)addrs, 0, sizeof(addrs));
533 memset((void *)mtus, 0, sizeof(mtus));
535 if (afs_cb_interface.numberOfInterfaces < num)
536 num = afs_cb_interface.numberOfInterfaces;
537 for (i = 0; i < num; i++) {
538 if (!afs_cb_interface.mtu[i])
539 afs_cb_interface.mtu[i] = htonl(1500);
540 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
541 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
542 if (myNetAddrs[i] != ifinaddr)
546 rxmtu = rxi_AdjustIfMTU(rxmtu);
548 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
549 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
550 addrs[i++] = ifinaddr;
551 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
552 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
553 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
557 rx_maxJumboRecvSize =
558 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
559 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
560 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
563 for (j = 0; j < i; j++) {
564 myNetMTUs[j] = mtus[j];
565 myNetAddrs[j] = addrs[j];
572 /* Returns the afs_cb_interface inxex which best matches address.
573 * If none is found, we return -1.
576 rxi_Findcbi(afs_uint32 addr)
579 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
580 afs_int32 rvalue = -1;
583 if (numMyNetAddrs == 0)
584 (void)rxi_GetcbiInfo();
586 myAddr = ntohl(addr);
588 if (IN_CLASSA(myAddr))
589 netMask = IN_CLASSA_NET;
590 else if (IN_CLASSB(myAddr))
591 netMask = IN_CLASSB_NET;
592 else if (IN_CLASSC(myAddr))
593 netMask = IN_CLASSC_NET;
597 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
598 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
599 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
600 if ((myAddr & netMask) == (thisAddr & netMask)) {
601 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
602 if (myAddr == thisAddr) {
607 if (match_value < 3) {
612 if (match_value < 2) {
623 #else /* AFS_USERSPACE_IP_ADDR */
625 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
626 #define IFADDR2SA(f) (&((f)->ifa_addr))
627 #else /* AFS_AIX41_ENV */
628 #define IFADDR2SA(f) ((f)->ifa_addr)
637 register int rxmtu, maxmtu;
638 afs_uint32 addrs[ADDRSPERSITE];
639 int mtus[ADDRSPERSITE];
641 #if defined(AFS_DARWIN80_ENV)
647 struct sockaddr sout;
648 struct sockaddr_in *sin;
651 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
652 register struct ifnet *ifn;
655 memset(addrs, 0, sizeof(addrs));
656 memset(mtus, 0, sizeof(mtus));
658 #if defined(AFS_DARWIN80_ENV)
659 if (!ifnet_list_get(AF_INET, &ifn, &count)) {
660 for (m = 0; m < count; m++) {
661 if (!ifnet_get_address_list(ifn[m], &ifads)) {
662 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
663 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
664 sin = (struct sockaddr_in *)&sout;
665 rxmtu = ifnet_mtu(ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
666 ifinaddr = ntohl(sin->sin_addr.s_addr);
667 if (myNetAddrs[i] != ifinaddr) {
671 rxmtu = rxi_AdjustIfMTU(rxmtu);
673 rxmtu * rxi_nRecvFrags +
674 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
675 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
676 addrs[i++] = ifinaddr;
677 if ((ifinaddr != 0x7f000001) &&
678 (maxmtu > rx_maxReceiveSize)) {
680 MIN(RX_MAX_PACKET_SIZE, maxmtu);
682 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
687 ifnet_free_address_list(ifads);
690 ifnet_list_free(ifn);
693 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
694 TAILQ_FOREACH(ifn, &ifnet, if_link) {
695 if (i >= ADDRSPERSITE)
697 #elif defined(AFS_OBSD_ENV)
698 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
699 ifn = ifn->if_list.tqe_next) {
701 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
703 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
704 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
705 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
706 if (i >= ADDRSPERSITE)
708 #elif defined(AFS_OBSD_ENV)
709 for (ifad = ifn->if_addrlist.tqh_first;
710 ifad != NULL && i < ADDRSPERSITE;
711 ifad = ifad->ifa_list.tqe_next) {
713 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
714 ifad = ifad->ifa_next) {
716 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
718 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
720 if (myNetAddrs[i] != ifinaddr) {
724 rxmtu = rxi_AdjustIfMTU(rxmtu);
726 rxmtu * rxi_nRecvFrags +
727 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
728 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
729 addrs[i++] = ifinaddr;
730 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
731 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
733 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
740 rx_maxJumboRecvSize =
741 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
742 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
743 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
747 for (l = 0; l < i; l++) {
748 myNetMTUs[l] = mtus[l];
749 myNetAddrs[l] = addrs[l];
755 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
756 /* Returns ifnet which best matches address */
757 #ifdef AFS_DARWIN80_ENV
762 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
764 struct sockaddr_in s, sr;
765 #ifdef AFS_DARWIN80_ENV
771 s.sin_family = AF_INET;
772 s.sin_addr.s_addr = addr;
773 #ifdef AFS_DARWIN80_ENV
774 ifad = ifaddr_withnet((struct sockaddr *)&s);
776 ifad = ifa_ifwithnet((struct sockaddr *)&s);
779 #ifdef AFS_DARWIN80_ENV
781 ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
782 *maskp = sr.sin_addr.s_addr;
784 return (ifad ? ifaddr_ifnet(ifad) : NULL);
787 *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
788 return (ifad ? ifad->ifa_ifp : NULL);
792 #else /* DARWIN60 || XBSD */
794 /* Returns ifnet which best matches address */
796 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
799 extern struct in_ifaddr *in_ifaddr;
800 struct in_ifaddr *ifa, *ifad = NULL;
804 #if defined(AFS_DARWIN_ENV)
805 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
806 ifa = TAILQ_NEXT(ifa, ia_link)) {
808 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
810 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
811 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
812 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
817 if (match_value < 3) {
822 if (match_value < 2) {
827 } /* if net matches */
828 } /* for all in_ifaddrs */
832 *maskp = ifad->ia_subnetmask;
833 return (ifad ? ifad->ia_ifp : NULL);
835 #endif /* else DARWIN60 || XBSD */
836 #endif /* else AFS_USERSPACE_IP_ADDR */
837 #endif /* !SUN5 && !SGI62 */
840 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
841 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
842 * system specific subdirectories for all systems. But for the moment,
843 * most of it is simple to follow common code.
845 #if !defined(UKERNEL)
846 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
847 /* rxk_NewSocket creates a new socket on the specified port. The port is
848 * in network byte order.
851 rxk_NewSocketHost(afs_uint32 ahost, short aport)
853 register afs_int32 code;
854 #ifdef AFS_DARWIN80_ENV
857 struct socket *newSocket;
859 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
862 struct sockaddr_in myaddr;
863 #ifdef AFS_HPUX110_ENV
864 /* prototype copied from kernel source file streams/str_proto.h */
865 extern MBLKP allocb_wait(int, int);
867 int addrsize = sizeof(struct sockaddr_in);
869 extern struct fileops socketops;
875 AFS_STATCNT(osi_NewSocket);
876 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
877 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
881 #if defined(AFS_HPUX102_ENV)
882 #if defined(AFS_HPUX110_ENV)
883 /* we need a file associated with the socket so sosend in NetSend
885 /* blocking socket */
886 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
890 fp->f_flag = FREAD | FWRITE;
891 fp->f_type = DTYPE_SOCKET;
892 fp->f_ops = &socketops;
894 fp->f_data = (void *)newSocket;
895 newSocket->so_fp = (void *)fp;
897 #else /* AFS_HPUX110_ENV */
898 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
899 #endif /* else AFS_HPUX110_ENV */
900 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
901 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
902 #elif defined(AFS_FBSD50_ENV)
903 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
904 afs_osi_credp, curthread);
905 #elif defined(AFS_FBSD40_ENV)
906 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
907 #elif defined(AFS_DARWIN80_ENV)
908 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
910 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
911 #endif /* AFS_HPUX102_ENV */
915 memset(&myaddr, 0, sizeof myaddr);
916 myaddr.sin_family = AF_INET;
917 myaddr.sin_port = aport;
918 myaddr.sin_addr.s_addr = ahost;
919 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
920 myaddr.sin_len = sizeof(myaddr);
923 #ifdef AFS_HPUX110_ENV
924 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
929 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
931 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
933 code = sobind(newSocket, bindnam, addrsize);
936 #if !defined(AFS_HPUX1122_ENV)
943 #else /* AFS_HPUX110_ENV */
944 #if defined(AFS_DARWIN80_ENV)
949 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
950 &buflen, sizeof(buflen));
951 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
952 &buflen, sizeof(buflen));
956 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
961 code = soreserve(newSocket, 50000, 50000);
963 code = soreserve(newSocket, 32766, 32766);
965 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
968 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
969 #if defined(AFS_FBSD50_ENV)
970 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
971 #elif defined(AFS_FBSD40_ENV)
972 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
974 code = sobind(newSocket, (struct sockaddr *)&myaddr);
977 dpf(("sobind fails (%d)\n", (int)code));
982 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
984 nam = m_getclr(M_WAIT, MT_SONAME);
985 #else /* AFS_OSF_ENV */
986 nam = m_get(M_WAIT, MT_SONAME);
989 #if defined(KERNEL_HAVE_UERROR)
994 nam->m_len = sizeof(myaddr);
995 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
997 BHV_PDATA(&bhv) = (void *)newSocket;
998 code = sobind(&bhv, nam);
1001 code = sobind(newSocket, nam);
1004 dpf(("sobind fails (%d)\n", (int)code));
1006 #ifndef AFS_SGI65_ENV
1011 #endif /* else AFS_DARWIN_ENV */
1012 #endif /* else AFS_HPUX110_ENV */
1015 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1016 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1018 return (osi_socket *)newSocket;
1022 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1023 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1025 return (osi_socket *)0;
1029 rxk_NewSocket(short aport)
1031 return rxk_NewSocketHost(0, aport);
1034 /* free socket allocated by rxk_NewSocket */
1036 rxk_FreeSocket(register struct socket *asocket)
1038 AFS_STATCNT(osi_FreeSocket);
1039 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1040 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1042 #ifdef AFS_HPUX110_ENV
1043 if (asocket->so_fp) {
1044 struct file *fp = asocket->so_fp;
1045 #if !defined(AFS_HPUX1123_ENV)
1046 /* 11.23 still has falloc, but not FPENTRYFREE !
1047 * so for now if we shutdown, we will waist a file
1050 asocket->so_fp = NULL;
1053 #endif /* AFS_HPUX110_ENV */
1055 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1056 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1060 #endif /* !SUN5 && !LINUX20 */
1062 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1064 * Run RX event daemon every second (5 times faster than rest of systems)
1067 afs_rxevent_daemon(void)
1073 #ifdef RX_ENABLE_LOCKS
1075 #endif /* RX_ENABLE_LOCKS */
1077 rxevent_RaiseEvents(&temp);
1079 #ifdef RX_ENABLE_LOCKS
1081 #endif /* RX_ENABLE_LOCKS */
1082 #ifdef RX_KERNEL_TRACE
1083 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1084 "before afs_osi_Wait()");
1086 afs_osi_Wait(500, NULL, 0);
1087 #ifdef RX_KERNEL_TRACE
1088 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1089 "after afs_osi_Wait()");
1091 if (afs_termState == AFSOP_STOP_RXEVENT) {
1092 #ifdef RXK_LISTENER_ENV
1093 afs_termState = AFSOP_STOP_RXK_LISTENER;
1095 #ifdef AFS_SUN510_ENV
1096 afs_termState = AFSOP_STOP_NETIF;
1098 afs_termState = AFSOP_STOP_COMPLETE;
1101 osi_rxWakeup(&afs_termState);
1108 #ifdef RXK_LISTENER_ENV
1110 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1112 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1115 struct sockaddr_in from;
1118 register afs_int32 tlen;
1119 afs_int32 savelen; /* was using rlen but had aliasing problems */
1120 rx_computelen(p, tlen);
1121 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1123 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1124 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1125 * it once in order to avoid races. */
1128 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1136 /* add some padding to the last iovec, it's just to make sure that the
1137 * read doesn't return more data than we expect, and is done to get around
1138 * our problems caused by the lack of a length field in the rx header. */
1139 savelen = p->wirevec[p->niovecs - 1].iov_len;
1140 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1142 nbytes = tlen + sizeof(afs_int32);
1143 #ifdef RX_KERNEL_TRACE
1144 if (ICL_SETACTIVE(afs_iclSetp)) {
1146 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1147 "before osi_NetRecive()");
1151 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1153 #ifdef RX_KERNEL_TRACE
1154 if (ICL_SETACTIVE(afs_iclSetp)) {
1156 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1157 "after osi_NetRecive()");
1161 /* restore the vec to its correct state */
1162 p->wirevec[p->niovecs - 1].iov_len = savelen;
1165 p->length = nbytes - RX_HEADER_SIZE;;
1166 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1168 MUTEX_ENTER(&rx_stats_mutex);
1169 rx_stats.bogusPacketOnRead++;
1170 rx_stats.bogusHost = from.sin_addr.s_addr;
1171 MUTEX_EXIT(&rx_stats_mutex);
1172 dpf(("B: bogus packet from [%x,%d] nb=%d",
1173 from.sin_addr.s_addr, from.sin_port, nbytes));
1177 /* Extract packet header. */
1178 rxi_DecodePacketHeader(p);
1180 *host = from.sin_addr.s_addr;
1181 *port = from.sin_port;
1182 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1183 MUTEX_ENTER(&rx_stats_mutex);
1184 rx_stats.packetsRead[p->header.type - 1]++;
1185 MUTEX_EXIT(&rx_stats_mutex);
1188 /* Free any empty packet buffers at the end of this packet */
1189 rxi_TrimDataBufs(p, 1);
1199 * Listen for packets on socket. This thread is typically started after
1200 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1201 * the start state is set before proceeding.
1203 * Note that this thread is outside the AFS global lock for much of
1206 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1207 * we want here. So we need to either block all signals (including SIGKILL
1208 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1209 * OS's socket receive routine returns as a result of a signal.
1211 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1212 #ifdef AFS_LINUX20_ENV
1213 struct task_struct *rxk_ListenerTask;
1218 * Run the listener as a kernel thread.
1224 void rxk_ListenerProc(void);
1226 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1227 minclsyspri) == NULL)
1228 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1232 rxk_ListenerProc(void)
1233 #else /* AFS_SUN5_ENV */
1236 #endif /* AFS_SUN5_ENV */
1238 struct rx_packet *rxp = NULL;
1242 #ifdef AFS_LINUX20_ENV
1243 rxk_ListenerPid = current->pid;
1244 rxk_ListenerTask = current;
1247 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1248 #endif /* AFS_SUN5_ENV */
1250 rxk_ListenerPid = curproc->p_pid;
1251 #endif /* AFS_FBSD_ENV */
1252 #ifdef AFS_DARWIN80_ENV
1253 rxk_ListenerPid = proc_selfpid();
1254 #elif defined(AFS_DARWIN_ENV)
1255 rxk_ListenerPid = current_proc()->p_pid;
1257 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1259 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1260 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1262 rxi_RestoreDataBufs(rxp);
1264 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1266 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1268 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1269 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1273 #ifdef RX_ENABLE_LOCKS
1275 #endif /* RX_ENABLE_LOCKS */
1276 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1277 #ifdef AFS_SUN510_ENV
1278 afs_termState = AFSOP_STOP_NETIF;
1280 afs_termState = AFSOP_STOP_COMPLETE;
1282 osi_rxWakeup(&afs_termState);
1284 rxk_ListenerPid = 0;
1285 #ifdef AFS_LINUX20_ENV
1286 rxk_ListenerTask = 0;
1287 osi_rxWakeup(&rxk_ListenerTask);
1289 #if defined(AFS_SUN5_ENV)
1290 osi_rxWakeup(&rxk_ListenerPid);
1294 #endif /* AFS_SUN5_ENV */
1297 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1298 /* The manner of stopping the rx listener thread may vary. Most unix's should
1299 * be able to call soclose.
1302 osi_StopListener(void)
1307 #endif /* RXK_LISTENER_ENV */
1309 #endif /* !NCR && !UKERNEL */