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);
140 #elif defined(AFS_DARWIN80_ENV) && !defined(AFS_DARWIN90_ENV)
144 msg = "Unknown AFS panic";
147 vsnprintf(buf, sizeof(buf), msg, ap);
154 msg = "Unknown AFS panic";
159 # ifdef AFS_LINUX20_ENV
168 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
169 * unsigned long value into the given buffer. Returns 0 on success,
170 * and a value less than 0 on failure. The contents of the buffer is
171 * defined only on success.
175 osi_utoa(char *buf, size_t len, unsigned long val)
177 long k; /* index of first byte of string value */
179 /* we definitely need room for at least one digit and NUL */
185 /* compute the string form from the high end of the buffer */
188 for (k = len - 2; k >= 0; k--) {
189 buf[k] = val % 10 + '0';
196 /* did we finish converting val to string form? */
202 /* this should never happen */
208 /* this should never happen */
214 /* if necessary, relocate string to beginning of buf[] */
219 * We need to achieve the effect of calling
221 * memmove(buf, &buf[k], len - k);
223 * However, since memmove() is not available in all
224 * kernels, we explicitly do an appropriate copy.
230 while ((*dst++ = *src++) != '\0')
238 * osi_AssertFailK() -- used by the osi_Assert() macro.
240 * It essentially does
242 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
244 * Since the kernel version of osi_Panic() only passes its first
245 * argument to the native panic(), we construct a single string and hand
246 * that to osi_Panic().
249 osi_AssertFailK(const char *expr, const char *file, int line)
251 static const char msg0[] = "assertion failed: ";
252 static const char msg1[] = ", file: ";
253 static const char msg2[] = ", line: ";
254 static const char msg3[] = "\n";
257 * These buffers add up to 1K, which is a pleasantly nice round
258 * value, but probably not vital.
263 /* check line number conversion */
265 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
266 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
271 #define ADDBUF(BUF, STR) \
272 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
273 strcat(BUF, (char *)(STR)); \
282 ADDBUF(buf, linebuf);
291 osi_AssertFailK(const char *expr, const char *file, int line)
293 printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
298 /* This is the server process request loop. Kernel server
299 * processes never become listener threads */
301 rx_ServerProc(void *unused)
305 /* jaltman - rxi_dataQuota is protected by a mutex everywhere else */
306 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
307 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
308 /* threadID is used for making decisions in GetCall. Get it by bumping
309 * number of threads handling incoming calls */
310 threadID = rxi_availProcs++;
312 #ifdef RX_ENABLE_LOCKS
314 #endif /* RX_ENABLE_LOCKS */
315 rxi_ServerProc(threadID, NULL, NULL);
316 #ifdef RX_ENABLE_LOCKS
318 #endif /* RX_ENABLE_LOCKS */
322 #endif /* !UKERNEL */
324 #ifndef RXK_LISTENER_ENV
325 /* asize includes the Rx header */
327 MyPacketProc(struct rx_packet **ahandle, int asize)
329 struct rx_packet *tp;
331 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
332 /* If we can't scrounge enough cbufs, then we have to drop the packet,
333 * but we should set a flag so we magic up some more at our leisure.
336 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
337 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
338 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
340 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
341 RX_PACKET_CLASS_RECV_CBUF)) {
344 if (rx_stats_active) {
345 MUTEX_ENTER(&rx_stats_mutex);
346 rx_stats.noPacketBuffersOnRead++;
347 MUTEX_EXIT(&rx_stats_mutex);
353 * XXX if packet is too long for our buffer,
354 * should do this at a higher layer and let other
355 * end know we're losing.
357 if (rx_stats_active) {
358 MUTEX_ENTER(&rx_stats_mutex);
359 rx_stats.bogusPacketOnRead++;
360 MUTEX_EXIT(&rx_stats_mutex);
362 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
363 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
369 /* otherwise we have a packet, set appropriate values */
375 MyArrivalProc(struct rx_packet *ahandle,
376 struct sockaddr_in *afrom,
377 struct socket *arock,
380 /* handle basic rx packet */
381 ahandle->length = asize - RX_HEADER_SIZE;
382 rxi_DecodePacketHeader(ahandle);
384 rxi_ReceivePacket(ahandle, arock,
385 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
388 /* free the packet if it has been returned */
390 rxi_FreePacket(ahandle);
393 #endif /* !RXK_LISTENER_ENV */
396 rxi_StartListener(void)
398 /* if kernel, give name of appropriate procedures */
399 #ifndef RXK_LISTENER_ENV
400 rxk_GetPacketProc = MyPacketProc;
401 rxk_PacketArrivalProc = MyArrivalProc;
406 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
407 to get interesting information. */
409 rxi_InitPeerParams(register struct rx_peer *pp)
416 #ifdef AFS_USERSPACE_IP_ADDR
417 i = rxi_Findcbi(pp->host);
420 /* pp->timeout.usec = 0; */
421 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
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 = RX_REMOTE_PACKET_SIZE;
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 (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 (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
462 rxmtu = 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 = RX_REMOTE_PACKET_SIZE;
471 #endif /* else AFS_USERSPACE_IP_ADDR */
472 #else /* AFS_SUN5_ENV */
473 mtu = rxi_FindIfMTU(pp->host);
477 /* pp->timeout.usec = 0; */
478 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
481 /* pp->timeout.usec = 0; */
482 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
486 /* Diminish the packet size to one based on the MTU given by
488 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
489 rxmtu = mtu - RX_IPUDP_SIZE;
490 if (rxmtu < pp->ifMTU)
493 } else { /* couldn't find the interface, so assume the worst */
494 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
496 #endif /* AFS_SUN5_ENV */
497 #else /* ADAPT_MTU */
498 pp->rateFlag = 2; /* start timing after two full packets */
500 pp->ifMTU = OLD_MAX_PACKET_SIZE;
501 #endif /* else ADAPT_MTU */
502 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
503 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
504 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
506 MIN(rxi_nDgramPackets,
507 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
508 pp->maxDgramPackets = 1;
510 /* Initialize slow start parameters */
511 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
513 pp->nDgramPackets = 1;
518 /* The following code is common to several system types, but not all. The
519 * separate ones are found in the system specific subdirectories.
523 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
524 /* Routine called during the afsd "-shutdown" process to put things back to
527 static struct protosw parent_proto; /* udp proto switch */
530 shutdown_rxkernel(void)
532 register struct protosw *tpro, *last;
533 last = inetdomain.dom_protoswNPROTOSW;
534 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
535 if (tpro->pr_protocol == IPPROTO_UDP) {
536 /* restore original udp protocol switch */
537 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
538 memset((void *)&parent_proto, 0, sizeof(parent_proto));
543 dpf(("shutdown_rxkernel: no udp proto"));
545 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
547 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
548 /* Determine what the network interfaces are for this machine. */
550 #ifdef AFS_USERSPACE_IP_ADDR
554 int i, j, different = 0, num = ADDRSPERSITE;
557 afs_uint32 addrs[ADDRSPERSITE];
558 int mtus[ADDRSPERSITE];
560 memset((void *)addrs, 0, sizeof(addrs));
561 memset((void *)mtus, 0, sizeof(mtus));
563 if (afs_cb_interface.numberOfInterfaces < num)
564 num = afs_cb_interface.numberOfInterfaces;
565 for (i = 0; i < num; i++) {
566 if (!afs_cb_interface.mtu[i])
567 afs_cb_interface.mtu[i] = htonl(1500);
568 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
569 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
570 if (myNetAddrs[i] != ifinaddr)
574 rxmtu = rxi_AdjustIfMTU(rxmtu);
576 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
577 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
578 addrs[i++] = ifinaddr;
579 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
580 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
581 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
585 rx_maxJumboRecvSize =
586 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
587 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
588 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
591 for (j = 0; j < i; j++) {
592 myNetMTUs[j] = mtus[j];
593 myNetAddrs[j] = addrs[j];
600 /* Returns the afs_cb_interface inxex which best matches address.
601 * If none is found, we return -1.
604 rxi_Findcbi(afs_uint32 addr)
607 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
608 afs_int32 rvalue = -1;
611 if (numMyNetAddrs == 0)
612 (void)rxi_GetcbiInfo();
614 myAddr = ntohl(addr);
616 if (IN_CLASSA(myAddr))
617 netMask = IN_CLASSA_NET;
618 else if (IN_CLASSB(myAddr))
619 netMask = IN_CLASSB_NET;
620 else if (IN_CLASSC(myAddr))
621 netMask = IN_CLASSC_NET;
625 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
626 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
627 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
628 if ((myAddr & netMask) == (thisAddr & netMask)) {
629 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
630 if (myAddr == thisAddr) {
635 if (match_value < 3) {
640 if (match_value < 2) {
651 #else /* AFS_USERSPACE_IP_ADDR */
653 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
654 #define IFADDR2SA(f) (&((f)->ifa_addr))
655 #else /* AFS_AIX41_ENV */
656 #define IFADDR2SA(f) ((f)->ifa_addr)
665 register int rxmtu, maxmtu;
666 afs_uint32 addrs[ADDRSPERSITE];
667 int mtus[ADDRSPERSITE];
669 #if defined(AFS_DARWIN80_ENV)
675 struct sockaddr sout;
676 struct sockaddr_in *sin;
679 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
680 register struct ifnet *ifn;
683 memset(addrs, 0, sizeof(addrs));
684 memset(mtus, 0, sizeof(mtus));
686 #if defined(AFS_DARWIN80_ENV)
687 if (!ifnet_list_get(AF_INET, &ifn, &count)) {
688 for (m = 0; m < count; m++) {
689 if (!ifnet_get_address_list(ifn[m], &ifads)) {
690 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
691 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
692 sin = (struct sockaddr_in *)&sout;
693 rxmtu = ifnet_mtu(ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
694 ifinaddr = ntohl(sin->sin_addr.s_addr);
695 if (myNetAddrs[i] != ifinaddr) {
699 rxmtu = rxi_AdjustIfMTU(rxmtu);
701 rxmtu * rxi_nRecvFrags +
702 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
703 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
704 addrs[i++] = ifinaddr;
705 if ((ifinaddr != 0x7f000001) &&
706 (maxmtu > rx_maxReceiveSize)) {
708 MIN(RX_MAX_PACKET_SIZE, maxmtu);
710 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
715 ifnet_free_address_list(ifads);
718 ifnet_list_free(ifn);
721 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
722 TAILQ_FOREACH(ifn, &ifnet, if_link) {
723 if (i >= ADDRSPERSITE)
725 #elif defined(AFS_OBSD_ENV)
726 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
727 ifn = ifn->if_list.tqe_next) {
729 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
731 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
732 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
733 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
734 if (i >= ADDRSPERSITE)
736 #elif defined(AFS_OBSD_ENV)
737 for (ifad = ifn->if_addrlist.tqh_first;
738 ifad != NULL && i < ADDRSPERSITE;
739 ifad = ifad->ifa_list.tqe_next) {
741 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
742 ifad = ifad->ifa_next) {
744 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
746 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
748 if (myNetAddrs[i] != ifinaddr) {
752 rxmtu = rxi_AdjustIfMTU(rxmtu);
754 rxmtu * rxi_nRecvFrags +
755 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
756 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
757 addrs[i++] = ifinaddr;
758 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
759 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
761 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
768 rx_maxJumboRecvSize =
769 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
770 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
771 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
775 for (l = 0; l < i; l++) {
776 myNetMTUs[l] = mtus[l];
777 myNetAddrs[l] = addrs[l];
783 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
784 /* Returns ifnet which best matches address */
785 #ifdef AFS_DARWIN80_ENV
790 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
792 struct sockaddr_in s, sr;
793 #ifdef AFS_DARWIN80_ENV
799 s.sin_family = AF_INET;
800 s.sin_addr.s_addr = addr;
801 #ifdef AFS_DARWIN80_ENV
802 ifad = ifaddr_withnet((struct sockaddr *)&s);
804 ifad = ifa_ifwithnet((struct sockaddr *)&s);
807 #ifdef AFS_DARWIN80_ENV
809 ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
810 *maskp = sr.sin_addr.s_addr;
812 return (ifad ? ifaddr_ifnet(ifad) : NULL);
815 *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
816 return (ifad ? ifad->ifa_ifp : NULL);
820 #else /* DARWIN60 || XBSD */
822 /* Returns ifnet which best matches address */
824 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
827 extern struct in_ifaddr *in_ifaddr;
828 struct in_ifaddr *ifa, *ifad = NULL;
832 #if defined(AFS_DARWIN_ENV)
833 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
834 ifa = TAILQ_NEXT(ifa, ia_link)) {
836 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
838 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
839 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
840 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
845 if (match_value < 3) {
850 if (match_value < 2) {
855 } /* if net matches */
856 } /* for all in_ifaddrs */
860 *maskp = ifad->ia_subnetmask;
861 return (ifad ? ifad->ia_ifp : NULL);
863 #endif /* else DARWIN60 || XBSD */
864 #endif /* else AFS_USERSPACE_IP_ADDR */
865 #endif /* !SUN5 && !SGI62 */
868 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
869 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
870 * system specific subdirectories for all systems. But for the moment,
871 * most of it is simple to follow common code.
873 #if !defined(UKERNEL)
874 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
875 /* rxk_NewSocket creates a new socket on the specified port. The port is
876 * in network byte order.
879 rxk_NewSocketHost(afs_uint32 ahost, short aport)
881 register afs_int32 code;
882 #ifdef AFS_DARWIN80_ENV
885 struct socket *newSocket;
887 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
890 struct sockaddr_in myaddr;
891 #ifdef AFS_HPUX110_ENV
892 /* prototype copied from kernel source file streams/str_proto.h */
893 extern MBLKP allocb_wait(int, int);
895 int addrsize = sizeof(struct sockaddr_in);
897 extern struct fileops socketops;
903 AFS_STATCNT(osi_NewSocket);
904 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
905 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
909 #if defined(AFS_HPUX102_ENV)
910 #if defined(AFS_HPUX110_ENV)
911 /* we need a file associated with the socket so sosend in NetSend
913 /* blocking socket */
914 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
918 fp->f_flag = FREAD | FWRITE;
919 fp->f_type = DTYPE_SOCKET;
920 fp->f_ops = &socketops;
922 fp->f_data = (void *)newSocket;
923 newSocket->so_fp = (void *)fp;
925 #else /* AFS_HPUX110_ENV */
926 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
927 #endif /* else AFS_HPUX110_ENV */
928 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
929 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
930 #elif defined(AFS_FBSD50_ENV)
931 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
932 afs_osi_credp, curthread);
933 #elif defined(AFS_FBSD40_ENV)
934 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
935 #elif defined(AFS_DARWIN80_ENV)
936 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
938 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
939 #endif /* AFS_HPUX102_ENV */
943 memset(&myaddr, 0, sizeof myaddr);
944 myaddr.sin_family = AF_INET;
945 myaddr.sin_port = aport;
946 myaddr.sin_addr.s_addr = ahost;
947 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
948 myaddr.sin_len = sizeof(myaddr);
951 #ifdef AFS_HPUX110_ENV
952 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
957 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
959 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
961 code = sobind(newSocket, bindnam, addrsize);
964 #if !defined(AFS_HPUX1122_ENV)
971 #else /* AFS_HPUX110_ENV */
972 #if defined(AFS_DARWIN80_ENV)
977 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
978 &buflen, sizeof(buflen));
979 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
980 &buflen, sizeof(buflen));
984 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
989 code = soreserve(newSocket, 50000, 50000);
991 code = soreserve(newSocket, 32766, 32766);
993 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
996 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
997 #if defined(AFS_FBSD50_ENV)
998 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
999 #elif defined(AFS_FBSD40_ENV)
1000 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
1002 code = sobind(newSocket, (struct sockaddr *)&myaddr);
1005 dpf(("sobind fails (%d)\n", (int)code));
1010 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
1012 nam = m_getclr(M_WAIT, MT_SONAME);
1013 #else /* AFS_OSF_ENV */
1014 nam = m_get(M_WAIT, MT_SONAME);
1017 #if defined(KERNEL_HAVE_UERROR)
1022 nam->m_len = sizeof(myaddr);
1023 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
1024 #if defined(AFS_SGI65_ENV)
1025 BHV_PDATA(&bhv) = (void *)newSocket;
1026 code = sobind(&bhv, nam);
1028 #elif defined(AFS_OBSD44_ENV)
1029 code = sobind(newSocket, nam, osi_curproc());
1031 code = sobind(newSocket, nam);
1034 dpf(("sobind fails (%d)\n", (int)code));
1036 #ifndef AFS_SGI65_ENV
1041 #endif /* else AFS_DARWIN_ENV */
1042 #endif /* else AFS_HPUX110_ENV */
1045 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1046 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1048 return (osi_socket *)newSocket;
1052 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1053 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1055 return (osi_socket *)0;
1059 rxk_NewSocket(short aport)
1061 return rxk_NewSocketHost(0, aport);
1064 /* free socket allocated by rxk_NewSocket */
1066 rxk_FreeSocket(register struct socket *asocket)
1068 AFS_STATCNT(osi_FreeSocket);
1069 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1070 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1072 #ifdef AFS_HPUX110_ENV
1073 if (asocket->so_fp) {
1074 struct file *fp = asocket->so_fp;
1075 #if !defined(AFS_HPUX1123_ENV)
1076 /* 11.23 still has falloc, but not FPENTRYFREE !
1077 * so for now if we shutdown, we will waist a file
1080 asocket->so_fp = NULL;
1083 #endif /* AFS_HPUX110_ENV */
1085 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1086 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1090 #endif /* !SUN5 && !LINUX20 */
1092 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1094 * Run RX event daemon every second (5 times faster than rest of systems)
1097 afs_rxevent_daemon(void)
1103 #ifdef RX_ENABLE_LOCKS
1105 #endif /* RX_ENABLE_LOCKS */
1107 rxevent_RaiseEvents(&temp);
1109 #ifdef RX_ENABLE_LOCKS
1111 #endif /* RX_ENABLE_LOCKS */
1112 #ifdef RX_KERNEL_TRACE
1113 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1114 "before afs_osi_Wait()");
1116 afs_osi_Wait(500, NULL, 0);
1117 #ifdef RX_KERNEL_TRACE
1118 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1119 "after afs_osi_Wait()");
1121 if (afs_termState == AFSOP_STOP_RXEVENT) {
1122 #ifdef RXK_LISTENER_ENV
1123 afs_termState = AFSOP_STOP_RXK_LISTENER;
1125 #ifdef AFS_SUN510_ENV
1126 afs_termState = AFSOP_STOP_NETIF;
1128 afs_termState = AFSOP_STOP_COMPLETE;
1131 osi_rxWakeup(&afs_termState);
1138 #ifdef RXK_LISTENER_ENV
1140 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1142 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1145 struct sockaddr_in from;
1148 register afs_int32 tlen;
1149 afs_int32 savelen; /* was using rlen but had aliasing problems */
1150 rx_computelen(p, tlen);
1151 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1153 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1154 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1155 * it once in order to avoid races. */
1158 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1166 /* add some padding to the last iovec, it's just to make sure that the
1167 * read doesn't return more data than we expect, and is done to get around
1168 * our problems caused by the lack of a length field in the rx header. */
1169 savelen = p->wirevec[p->niovecs - 1].iov_len;
1170 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1172 nbytes = tlen + sizeof(afs_int32);
1173 #ifdef RX_KERNEL_TRACE
1174 if (ICL_SETACTIVE(afs_iclSetp)) {
1176 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1177 "before osi_NetRecive()");
1181 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1183 #ifdef RX_KERNEL_TRACE
1184 if (ICL_SETACTIVE(afs_iclSetp)) {
1186 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1187 "after osi_NetRecive()");
1191 /* restore the vec to its correct state */
1192 p->wirevec[p->niovecs - 1].iov_len = savelen;
1195 p->length = nbytes - RX_HEADER_SIZE;;
1196 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1198 if (rx_stats_active) {
1199 MUTEX_ENTER(&rx_stats_mutex);
1200 rx_stats.bogusPacketOnRead++;
1201 rx_stats.bogusHost = from.sin_addr.s_addr;
1202 MUTEX_EXIT(&rx_stats_mutex);
1204 dpf(("B: bogus packet from [%x,%d] nb=%d",
1205 from.sin_addr.s_addr, from.sin_port, nbytes));
1209 /* Extract packet header. */
1210 rxi_DecodePacketHeader(p);
1212 *host = from.sin_addr.s_addr;
1213 *port = from.sin_port;
1214 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1215 if (rx_stats_active) {
1216 MUTEX_ENTER(&rx_stats_mutex);
1217 rx_stats.packetsRead[p->header.type - 1]++;
1218 MUTEX_EXIT(&rx_stats_mutex);
1222 /* Free any empty packet buffers at the end of this packet */
1223 rxi_TrimDataBufs(p, 1);
1233 * Listen for packets on socket. This thread is typically started after
1234 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1235 * the start state is set before proceeding.
1237 * Note that this thread is outside the AFS global lock for much of
1240 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1241 * we want here. So we need to either block all signals (including SIGKILL
1242 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1243 * OS's socket receive routine returns as a result of a signal.
1245 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1246 #ifdef AFS_LINUX20_ENV
1247 struct task_struct *rxk_ListenerTask;
1252 * Run the listener as a kernel thread.
1258 void rxk_ListenerProc(void);
1260 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1261 minclsyspri) == NULL)
1262 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1266 rxk_ListenerProc(void)
1267 #else /* AFS_SUN5_ENV */
1270 #endif /* AFS_SUN5_ENV */
1272 struct rx_packet *rxp = NULL;
1276 #ifdef AFS_LINUX20_ENV
1277 rxk_ListenerPid = current->pid;
1278 rxk_ListenerTask = current;
1281 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1282 #endif /* AFS_SUN5_ENV */
1284 rxk_ListenerPid = curproc->p_pid;
1285 #endif /* AFS_FBSD_ENV */
1286 #ifdef AFS_DARWIN80_ENV
1287 rxk_ListenerPid = proc_selfpid();
1288 #elif defined(AFS_DARWIN_ENV)
1289 rxk_ListenerPid = current_proc()->p_pid;
1291 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1293 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1294 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1296 rxi_RestoreDataBufs(rxp);
1298 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1300 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1302 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1303 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1307 #ifdef RX_ENABLE_LOCKS
1309 #endif /* RX_ENABLE_LOCKS */
1310 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1311 #ifdef AFS_SUN510_ENV
1312 afs_termState = AFSOP_STOP_NETIF;
1314 afs_termState = AFSOP_STOP_COMPLETE;
1316 osi_rxWakeup(&afs_termState);
1318 rxk_ListenerPid = 0;
1319 #ifdef AFS_LINUX20_ENV
1320 rxk_ListenerTask = 0;
1321 osi_rxWakeup(&rxk_ListenerTask);
1323 #if defined(AFS_SUN5_ENV)
1324 osi_rxWakeup(&rxk_ListenerPid);
1328 #endif /* AFS_SUN5_ENV */
1331 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1332 /* The manner of stopping the rx listener thread may vary. Most unix's should
1333 * be able to call soclose.
1336 osi_StopListener(void)
1341 #endif /* RXK_LISTENER_ENV */
1343 #endif /* !NCR && !UKERNEL */