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)
412 # ifndef AFS_SUN5_ENV
413 # ifdef AFS_USERSPACE_IP_ADDR
417 i = rxi_Findcbi(pp->host);
420 /* pp->timeout.usec = 0; */
421 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
424 /* pp->timeout.usec = 0; */
425 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
428 mtu = ntohl(afs_cb_interface.mtu[i]);
429 /* Diminish the packet size to one based on the MTU given by
431 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
432 rxmtu = mtu - RX_IPUDP_SIZE;
433 if (rxmtu < pp->ifMTU)
436 } else { /* couldn't find the interface, so assume the worst */
437 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
439 # else /* AFS_USERSPACE_IP_ADDR */
442 # if !defined(AFS_SGI62_ENV)
443 if (numMyNetAddrs == 0)
444 (void)rxi_GetIFInfo();
447 ifn = rxi_FindIfnet(pp->host, NULL);
450 /* pp->timeout.usec = 0; */
451 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
452 # ifdef IFF_POINTOPOINT
453 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
454 /* wish we knew the bit rate and the chunk size, sigh. */
456 pp->ifMTU = RX_PP_PACKET_SIZE;
458 # endif /* IFF_POINTOPOINT */
459 /* Diminish the packet size to one based on the MTU given by
461 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
462 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
463 if (rxmtu < pp->ifMTU)
466 } else { /* couldn't find the interface, so assume the worst */
468 /* pp->timeout.usec = 0; */
469 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
471 # endif /* else AFS_USERSPACE_IP_ADDR */
472 # else /* AFS_SUN5_ENV */
475 mtu = rxi_FindIfMTU(pp->host);
479 /* pp->timeout.usec = 0; */
480 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
483 /* pp->timeout.usec = 0; */
484 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
488 /* Diminish the packet size to one based on the MTU given by
490 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
491 rxmtu = mtu - RX_IPUDP_SIZE;
492 if (rxmtu < pp->ifMTU)
495 } else { /* couldn't find the interface, so assume the worst */
496 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE,rx_MyMaxSendSize);
498 # endif /* AFS_SUN5_ENV */
499 #else /* ADAPT_MTU */
500 pp->rateFlag = 2; /* start timing after two full packets */
502 pp->ifMTU = OLD_MAX_PACKET_SIZE;
503 #endif /* else ADAPT_MTU */
504 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
505 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
506 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
508 MIN(rxi_nDgramPackets,
509 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
510 pp->maxDgramPackets = 1;
512 /* Initialize slow start parameters */
513 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
515 pp->nDgramPackets = 1;
520 /* The following code is common to several system types, but not all. The
521 * separate ones are found in the system specific subdirectories.
525 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
526 /* Routine called during the afsd "-shutdown" process to put things back to
529 static struct protosw parent_proto; /* udp proto switch */
532 shutdown_rxkernel(void)
534 struct protosw *tpro, *last;
535 last = inetdomain.dom_protoswNPROTOSW;
536 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
537 if (tpro->pr_protocol == IPPROTO_UDP) {
538 /* restore original udp protocol switch */
539 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
540 memset((void *)&parent_proto, 0, sizeof(parent_proto));
545 dpf(("shutdown_rxkernel: no udp proto"));
547 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
549 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
550 /* Determine what the network interfaces are for this machine. */
552 #ifdef AFS_USERSPACE_IP_ADDR
556 int i, j, different = 0, num = ADDRSPERSITE;
559 afs_uint32 addrs[ADDRSPERSITE];
560 int mtus[ADDRSPERSITE];
562 memset((void *)addrs, 0, sizeof(addrs));
563 memset((void *)mtus, 0, sizeof(mtus));
565 if (afs_cb_interface.numberOfInterfaces < num)
566 num = afs_cb_interface.numberOfInterfaces;
567 for (i = 0; i < num; i++) {
568 if (!afs_cb_interface.mtu[i])
569 afs_cb_interface.mtu[i] = htonl(1500);
570 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
571 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
572 if (myNetAddrs[i] != ifinaddr)
576 rxmtu = rxi_AdjustIfMTU(rxmtu);
578 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
579 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
580 addrs[i++] = ifinaddr;
581 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
582 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
583 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
587 rx_maxJumboRecvSize =
588 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
589 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
590 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
593 for (j = 0; j < i; j++) {
594 myNetMTUs[j] = mtus[j];
595 myNetAddrs[j] = addrs[j];
602 /* Returns the afs_cb_interface inxex which best matches address.
603 * If none is found, we return -1.
606 rxi_Findcbi(afs_uint32 addr)
609 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
610 afs_int32 rvalue = -1;
613 if (numMyNetAddrs == 0)
614 (void)rxi_GetcbiInfo();
616 myAddr = ntohl(addr);
618 if (IN_CLASSA(myAddr))
619 netMask = IN_CLASSA_NET;
620 else if (IN_CLASSB(myAddr))
621 netMask = IN_CLASSB_NET;
622 else if (IN_CLASSC(myAddr))
623 netMask = IN_CLASSC_NET;
627 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
628 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
629 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
630 if ((myAddr & netMask) == (thisAddr & netMask)) {
631 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
632 if (myAddr == thisAddr) {
637 if (match_value < 3) {
642 if (match_value < 2) {
653 #else /* AFS_USERSPACE_IP_ADDR */
655 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
656 #define IFADDR2SA(f) (&((f)->ifa_addr))
657 #else /* AFS_AIX41_ENV */
658 #define IFADDR2SA(f) ((f)->ifa_addr)
668 afs_uint32 addrs[ADDRSPERSITE];
669 int mtus[ADDRSPERSITE];
671 #if defined(AFS_DARWIN80_ENV)
677 struct sockaddr sout;
678 struct sockaddr_in *sin;
681 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
685 memset(addrs, 0, sizeof(addrs));
686 memset(mtus, 0, sizeof(mtus));
688 #if defined(AFS_DARWIN80_ENV)
689 if (!ifnet_list_get(AF_INET, ifns, &count)) {
690 for (m = 0; m < count; m++) {
691 if (!ifnet_get_address_list(ifns[m], &ifads)) {
692 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
693 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
694 sin = (struct sockaddr_in *)&sout;
695 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
696 ifinaddr = ntohl(sin->sin_addr.s_addr);
697 if (myNetAddrs[i] != ifinaddr) {
701 rxmtu = rxi_AdjustIfMTU(rxmtu);
703 rxmtu * rxi_nRecvFrags +
704 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
705 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
706 addrs[i++] = ifinaddr;
707 if ((ifinaddr != 0x7f000001) &&
708 (maxmtu > rx_maxReceiveSize)) {
710 MIN(RX_MAX_PACKET_SIZE, maxmtu);
712 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
717 ifnet_free_address_list(ifads);
720 ifnet_list_free(ifns);
723 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
724 TAILQ_FOREACH(ifn, &ifnet, if_link) {
725 if (i >= ADDRSPERSITE)
727 #elif defined(AFS_OBSD_ENV)
728 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
729 ifn = ifn->if_list.tqe_next) {
731 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
733 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
734 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
735 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
736 if (i >= ADDRSPERSITE)
738 #elif defined(AFS_OBSD_ENV)
739 for (ifad = ifn->if_addrlist.tqh_first;
740 ifad != NULL && i < ADDRSPERSITE;
741 ifad = ifad->ifa_list.tqe_next) {
743 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
744 ifad = ifad->ifa_next) {
746 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
748 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
750 if (myNetAddrs[i] != ifinaddr) {
754 rxmtu = rxi_AdjustIfMTU(rxmtu);
756 rxmtu * rxi_nRecvFrags +
757 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
758 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
759 addrs[i++] = ifinaddr;
760 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
761 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
763 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
770 rx_maxJumboRecvSize =
771 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
772 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
773 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
777 for (l = 0; l < i; l++) {
778 myNetMTUs[l] = mtus[l];
779 myNetAddrs[l] = addrs[l];
785 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
786 /* Returns ifnet which best matches address */
788 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
790 struct sockaddr_in s, sr;
793 s.sin_family = AF_INET;
794 s.sin_addr.s_addr = addr;
795 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
798 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
799 *maskp = sr.sin_addr.s_addr;
801 return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
804 #else /* DARWIN60 || XBSD */
806 /* Returns ifnet which best matches address */
808 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
811 extern struct in_ifaddr *in_ifaddr;
812 struct in_ifaddr *ifa, *ifad = NULL;
816 #if defined(AFS_DARWIN_ENV)
817 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
818 ifa = TAILQ_NEXT(ifa, ia_link)) {
820 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
822 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
823 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
824 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
829 if (match_value < 3) {
834 if (match_value < 2) {
839 } /* if net matches */
840 } /* for all in_ifaddrs */
844 *maskp = ifad->ia_subnetmask;
845 return (ifad ? ifad->ia_ifp : NULL);
847 #endif /* else DARWIN60 || XBSD */
848 #endif /* else AFS_USERSPACE_IP_ADDR */
849 #endif /* !SUN5 && !SGI62 */
852 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
853 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
854 * system specific subdirectories for all systems. But for the moment,
855 * most of it is simple to follow common code.
857 #if !defined(UKERNEL)
858 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
859 /* rxk_NewSocket creates a new socket on the specified port. The port is
860 * in network byte order.
863 rxk_NewSocketHost(afs_uint32 ahost, short aport)
866 #ifdef AFS_DARWIN80_ENV
869 struct socket *newSocket;
871 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
874 struct sockaddr_in myaddr;
875 #ifdef AFS_HPUX110_ENV
876 /* prototype copied from kernel source file streams/str_proto.h */
877 extern MBLKP allocb_wait(int, int);
879 int addrsize = sizeof(struct sockaddr_in);
881 extern struct fileops socketops;
887 AFS_STATCNT(osi_NewSocket);
888 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
889 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
893 #if defined(AFS_HPUX102_ENV)
894 #if defined(AFS_HPUX110_ENV)
895 /* we need a file associated with the socket so sosend in NetSend
897 /* blocking socket */
898 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
902 fp->f_flag = FREAD | FWRITE;
903 fp->f_type = DTYPE_SOCKET;
904 fp->f_ops = &socketops;
906 fp->f_data = (void *)newSocket;
907 newSocket->so_fp = (void *)fp;
909 #else /* AFS_HPUX110_ENV */
910 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
911 #endif /* else AFS_HPUX110_ENV */
912 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
913 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
914 #elif defined(AFS_FBSD50_ENV)
915 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
916 afs_osi_credp, curthread);
917 #elif defined(AFS_FBSD40_ENV)
918 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
919 #elif defined(AFS_DARWIN80_ENV)
920 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
922 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
923 #endif /* AFS_HPUX102_ENV */
927 memset(&myaddr, 0, sizeof myaddr);
928 myaddr.sin_family = AF_INET;
929 myaddr.sin_port = aport;
930 myaddr.sin_addr.s_addr = ahost;
931 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
932 myaddr.sin_len = sizeof(myaddr);
935 #ifdef AFS_HPUX110_ENV
936 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
941 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
943 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
945 code = sobind(newSocket, bindnam, addrsize);
948 #if !defined(AFS_HPUX1122_ENV)
955 #else /* AFS_HPUX110_ENV */
956 #if defined(AFS_DARWIN80_ENV)
961 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
962 &buflen, sizeof(buflen));
963 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
964 &buflen, sizeof(buflen));
968 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
973 code = soreserve(newSocket, 50000, 50000);
975 code = soreserve(newSocket, 32766, 32766);
977 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
980 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
981 #if defined(AFS_FBSD50_ENV)
982 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
983 #elif defined(AFS_FBSD40_ENV)
984 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
986 code = sobind(newSocket, (struct sockaddr *)&myaddr);
989 dpf(("sobind fails (%d)\n", (int)code));
994 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
996 nam = m_getclr(M_WAIT, MT_SONAME);
997 #else /* AFS_OSF_ENV */
998 nam = m_get(M_WAIT, MT_SONAME);
1001 #if defined(KERNEL_HAVE_UERROR)
1006 nam->m_len = sizeof(myaddr);
1007 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
1008 #if defined(AFS_SGI65_ENV)
1009 BHV_PDATA(&bhv) = (void *)newSocket;
1010 code = sobind(&bhv, nam);
1012 #elif defined(AFS_OBSD44_ENV)
1013 code = sobind(newSocket, nam, osi_curproc());
1015 code = sobind(newSocket, nam);
1018 dpf(("sobind fails (%d)\n", (int)code));
1020 #ifndef AFS_SGI65_ENV
1025 #endif /* else AFS_DARWIN_ENV */
1026 #endif /* else AFS_HPUX110_ENV */
1029 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1030 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1032 return (osi_socket *)newSocket;
1036 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1037 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1039 return (osi_socket *)0;
1043 rxk_NewSocket(short aport)
1045 return rxk_NewSocketHost(0, aport);
1048 /* free socket allocated by rxk_NewSocket */
1050 rxk_FreeSocket(struct socket *asocket)
1052 AFS_STATCNT(osi_FreeSocket);
1053 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1054 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1056 #ifdef AFS_HPUX110_ENV
1057 if (asocket->so_fp) {
1058 struct file *fp = asocket->so_fp;
1059 #if !defined(AFS_HPUX1123_ENV)
1060 /* 11.23 still has falloc, but not FPENTRYFREE !
1061 * so for now if we shutdown, we will waist a file
1064 asocket->so_fp = NULL;
1067 #endif /* AFS_HPUX110_ENV */
1069 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1070 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1074 #endif /* !SUN5 && !LINUX20 */
1076 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1078 * Run RX event daemon every second (5 times faster than rest of systems)
1081 afs_rxevent_daemon(void)
1087 #ifdef RX_ENABLE_LOCKS
1089 #endif /* RX_ENABLE_LOCKS */
1091 rxevent_RaiseEvents(&temp);
1093 #ifdef RX_ENABLE_LOCKS
1095 #endif /* RX_ENABLE_LOCKS */
1096 #ifdef RX_KERNEL_TRACE
1097 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1098 "before afs_osi_Wait()");
1100 afs_osi_Wait(500, NULL, 0);
1101 #ifdef RX_KERNEL_TRACE
1102 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1103 "after afs_osi_Wait()");
1105 if (afs_termState == AFSOP_STOP_RXEVENT) {
1106 #ifdef RXK_LISTENER_ENV
1107 afs_termState = AFSOP_STOP_RXK_LISTENER;
1109 #ifdef AFS_SUN510_ENV
1110 afs_termState = AFSOP_STOP_NETIF;
1112 afs_termState = AFSOP_STOP_COMPLETE;
1115 osi_rxWakeup(&afs_termState);
1122 #ifdef RXK_LISTENER_ENV
1124 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1126 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1129 struct sockaddr_in from;
1133 afs_int32 savelen; /* was using rlen but had aliasing problems */
1134 rx_computelen(p, tlen);
1135 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1137 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1138 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1139 * it once in order to avoid races. */
1142 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1150 /* add some padding to the last iovec, it's just to make sure that the
1151 * read doesn't return more data than we expect, and is done to get around
1152 * our problems caused by the lack of a length field in the rx header. */
1153 savelen = p->wirevec[p->niovecs - 1].iov_len;
1154 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1156 nbytes = tlen + sizeof(afs_int32);
1157 #ifdef RX_KERNEL_TRACE
1158 if (ICL_SETACTIVE(afs_iclSetp)) {
1160 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1161 "before osi_NetRecive()");
1165 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1167 #ifdef RX_KERNEL_TRACE
1168 if (ICL_SETACTIVE(afs_iclSetp)) {
1170 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1171 "after osi_NetRecive()");
1175 /* restore the vec to its correct state */
1176 p->wirevec[p->niovecs - 1].iov_len = savelen;
1179 p->length = nbytes - RX_HEADER_SIZE;;
1180 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1182 if (rx_stats_active) {
1183 MUTEX_ENTER(&rx_stats_mutex);
1184 rx_stats.bogusPacketOnRead++;
1185 rx_stats.bogusHost = from.sin_addr.s_addr;
1186 MUTEX_EXIT(&rx_stats_mutex);
1188 dpf(("B: bogus packet from [%x,%d] nb=%d",
1189 from.sin_addr.s_addr, from.sin_port, nbytes));
1193 /* Extract packet header. */
1194 rxi_DecodePacketHeader(p);
1196 *host = from.sin_addr.s_addr;
1197 *port = from.sin_port;
1198 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1199 if (rx_stats_active) {
1200 MUTEX_ENTER(&rx_stats_mutex);
1201 rx_stats.packetsRead[p->header.type - 1]++;
1202 MUTEX_EXIT(&rx_stats_mutex);
1206 #ifdef RX_TRIMDATABUFS
1207 /* Free any empty packet buffers at the end of this packet */
1208 rxi_TrimDataBufs(p, 1);
1218 * Listen for packets on socket. This thread is typically started after
1219 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1220 * the start state is set before proceeding.
1222 * Note that this thread is outside the AFS global lock for much of
1225 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1226 * we want here. So we need to either block all signals (including SIGKILL
1227 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1228 * OS's socket receive routine returns as a result of a signal.
1230 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1231 #ifdef AFS_LINUX20_ENV
1232 struct task_struct *rxk_ListenerTask;
1237 * Run the listener as a kernel thread.
1243 void rxk_ListenerProc(void);
1245 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1246 minclsyspri) == NULL)
1247 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1251 rxk_ListenerProc(void)
1252 #else /* AFS_SUN5_ENV */
1255 #endif /* AFS_SUN5_ENV */
1257 struct rx_packet *rxp = NULL;
1261 #ifdef AFS_LINUX20_ENV
1262 rxk_ListenerPid = current->pid;
1263 rxk_ListenerTask = current;
1266 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1267 #endif /* AFS_SUN5_ENV */
1269 rxk_ListenerPid = curproc->p_pid;
1270 #endif /* AFS_FBSD_ENV */
1271 #ifdef AFS_DARWIN80_ENV
1272 rxk_ListenerPid = proc_selfpid();
1273 #elif defined(AFS_DARWIN_ENV)
1274 rxk_ListenerPid = current_proc()->p_pid;
1276 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1278 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1279 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1281 rxi_RestoreDataBufs(rxp);
1283 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1285 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1287 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1288 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1292 #ifdef RX_ENABLE_LOCKS
1294 #endif /* RX_ENABLE_LOCKS */
1295 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1296 #ifdef AFS_SUN510_ENV
1297 afs_termState = AFSOP_STOP_NETIF;
1299 afs_termState = AFSOP_STOP_COMPLETE;
1301 osi_rxWakeup(&afs_termState);
1303 rxk_ListenerPid = 0;
1304 #ifdef AFS_LINUX20_ENV
1305 rxk_ListenerTask = 0;
1306 osi_rxWakeup(&rxk_ListenerTask);
1308 #if defined(AFS_SUN5_ENV)
1309 osi_rxWakeup(&rxk_ListenerPid);
1313 #endif /* AFS_SUN5_ENV */
1316 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1317 /* The manner of stopping the rx listener thread may vary. Most unix's should
1318 * be able to call soclose.
1321 osi_StopListener(void)
1326 #endif /* RXK_LISTENER_ENV */
1328 #endif /* !NCR && !UKERNEL */