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);
128 osi_Panic(msg, a1, a2, a3)
132 msg = "Unknown AFS panic";
134 printf(msg, a1, a2, a3);
135 #ifdef AFS_LINUX24_ENV
143 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
144 * unsigned long value into the given buffer. Returns 0 on success,
145 * and a value less than 0 on failure. The contents of the buffer is
146 * defined only on success.
150 osi_utoa(char *buf, size_t len, unsigned long val)
152 long k; /* index of first byte of string value */
154 /* we definitely need room for at least one digit and NUL */
160 /* compute the string form from the high end of the buffer */
163 for (k = len - 2; k >= 0; k--) {
164 buf[k] = val % 10 + '0';
171 /* did we finish converting val to string form? */
177 /* this should never happen */
183 /* this should never happen */
189 /* if necessary, relocate string to beginning of buf[] */
194 * We need to achieve the effect of calling
196 * memmove(buf, &buf[k], len - k);
198 * However, since memmove() is not available in all
199 * kernels, we explicitly do an appropriate copy.
205 while ((*dst++ = *src++) != '\0')
213 * osi_AssertFailK() -- used by the osi_Assert() macro.
215 * It essentially does
217 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
219 * Since the kernel version of osi_Panic() only passes its first
220 * argument to the native panic(), we construct a single string and hand
221 * that to osi_Panic().
224 osi_AssertFailK(const char *expr, const char *file, int line)
226 static const char msg0[] = "assertion failed: ";
227 static const char msg1[] = ", file: ";
228 static const char msg2[] = ", line: ";
229 static const char msg3[] = "\n";
232 * These buffers add up to 1K, which is a pleasantly nice round
233 * value, but probably not vital.
238 /* check line number conversion */
240 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
241 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
246 #define ADDBUF(BUF, STR) \
247 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
248 strcat(BUF, (char *)(STR)); \
257 ADDBUF(buf, linebuf);
266 /* This is the server process request loop. Kernel server
267 * processes never become listener threads */
273 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
274 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
275 /* threadID is used for making decisions in GetCall. Get it by bumping
276 * number of threads handling incoming calls */
277 threadID = rxi_availProcs++;
279 #ifdef RX_ENABLE_LOCKS
281 #endif /* RX_ENABLE_LOCKS */
282 rxi_ServerProc(threadID, NULL, NULL);
283 #ifdef RX_ENABLE_LOCKS
285 #endif /* RX_ENABLE_LOCKS */
287 #endif /* !UKERNEL */
289 #ifndef RXK_LISTENER_ENV
290 /* asize includes the Rx header */
292 MyPacketProc(struct rx_packet **ahandle, int asize)
294 struct rx_packet *tp;
296 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
297 /* If we can't scrounge enough cbufs, then we have to drop the packet,
298 * but we should set a flag so we magic up some more at our leisure.
301 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
302 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
303 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
305 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
306 RX_PACKET_CLASS_RECV_CBUF)) {
309 MUTEX_ENTER(&rx_stats_mutex);
310 rx_stats.noPacketBuffersOnRead++;
311 MUTEX_EXIT(&rx_stats_mutex);
316 * XXX if packet is too long for our buffer,
317 * should do this at a higher layer and let other
318 * end know we're losing.
320 MUTEX_ENTER(&rx_stats_mutex);
321 rx_stats.bogusPacketOnRead++;
322 MUTEX_EXIT(&rx_stats_mutex);
323 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
324 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
330 /* otherwise we have a packet, set appropriate values */
336 MyArrivalProc(struct rx_packet *ahandle,
337 struct sockaddr_in *afrom,
338 struct socket *arock,
341 /* handle basic rx packet */
342 ahandle->length = asize - RX_HEADER_SIZE;
343 rxi_DecodePacketHeader(ahandle);
345 rxi_ReceivePacket(ahandle, arock,
346 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
349 /* free the packet if it has been returned */
351 rxi_FreePacket(ahandle);
354 #endif /* !RXK_LISTENER_ENV */
357 rxi_StartListener(void)
359 /* if kernel, give name of appropriate procedures */
360 #ifndef RXK_LISTENER_ENV
361 rxk_GetPacketProc = MyPacketProc;
362 rxk_PacketArrivalProc = MyArrivalProc;
367 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
368 to get interesting information. */
370 rxi_InitPeerParams(register struct rx_peer *pp)
377 #ifdef AFS_USERSPACE_IP_ADDR
378 i = rxi_Findcbi(pp->host);
381 /* pp->timeout.usec = 0; */
382 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
385 /* pp->timeout.usec = 0; */
386 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
389 mtu = ntohl(afs_cb_interface.mtu[i]);
390 /* Diminish the packet size to one based on the MTU given by
392 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
393 rxmtu = mtu - RX_IPUDP_SIZE;
394 if (rxmtu < pp->ifMTU)
397 } else { /* couldn't find the interface, so assume the worst */
398 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
400 #else /* AFS_USERSPACE_IP_ADDR */
403 #if !defined(AFS_SGI62_ENV)
404 if (numMyNetAddrs == 0)
405 (void)rxi_GetIFInfo();
408 ifn = rxi_FindIfnet(pp->host, NULL);
411 /* pp->timeout.usec = 0; */
412 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
413 #ifdef IFF_POINTOPOINT
414 if (ifnet_flags(ifn) & IFF_POINTOPOINT) {
415 /* wish we knew the bit rate and the chunk size, sigh. */
417 pp->ifMTU = RX_PP_PACKET_SIZE;
419 #endif /* IFF_POINTOPOINT */
420 /* Diminish the packet size to one based on the MTU given by
422 if (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
423 rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
424 if (rxmtu < pp->ifMTU)
427 } else { /* couldn't find the interface, so assume the worst */
429 /* pp->timeout.usec = 0; */
430 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
432 #endif /* else AFS_USERSPACE_IP_ADDR */
433 #else /* AFS_SUN5_ENV */
434 mtu = rxi_FindIfMTU(pp->host);
438 /* pp->timeout.usec = 0; */
439 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
442 /* pp->timeout.usec = 0; */
443 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
447 /* Diminish the packet size to one based on the MTU given by
449 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
450 rxmtu = mtu - RX_IPUDP_SIZE;
451 if (rxmtu < pp->ifMTU)
454 } else { /* couldn't find the interface, so assume the worst */
455 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
457 #endif /* AFS_SUN5_ENV */
458 #else /* ADAPT_MTU */
459 pp->rateFlag = 2; /* start timing after two full packets */
461 pp->ifMTU = OLD_MAX_PACKET_SIZE;
462 #endif /* else ADAPT_MTU */
463 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
464 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
465 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
467 MIN(rxi_nDgramPackets,
468 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
469 pp->maxDgramPackets = 1;
471 /* Initialize slow start parameters */
472 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
474 pp->nDgramPackets = 1;
479 /* The following code is common to several system types, but not all. The
480 * separate ones are found in the system specific subdirectories.
484 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
485 /* Routine called during the afsd "-shutdown" process to put things back to
488 static struct protosw parent_proto; /* udp proto switch */
491 shutdown_rxkernel(void)
493 register struct protosw *tpro, *last;
494 last = inetdomain.dom_protoswNPROTOSW;
495 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
496 if (tpro->pr_protocol == IPPROTO_UDP) {
497 /* restore original udp protocol switch */
498 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
499 memset((void *)&parent_proto, 0, sizeof(parent_proto));
504 dpf(("shutdown_rxkernel: no udp proto"));
506 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
508 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
509 /* Determine what the network interfaces are for this machine. */
511 #ifdef AFS_USERSPACE_IP_ADDR
515 int i, j, different = 0;
518 afs_uint32 addrs[ADDRSPERSITE];
519 int mtus[ADDRSPERSITE];
521 memset((void *)addrs, 0, sizeof(addrs));
522 memset((void *)mtus, 0, sizeof(mtus));
524 for (i = 0; i < afs_cb_interface.numberOfInterfaces; i++) {
525 if (!afs_cb_interface.mtu[i])
526 afs_cb_interface.mtu[i] = htonl(1500);
527 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
528 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
529 if (myNetAddrs[i] != ifinaddr)
533 rxmtu = rxi_AdjustIfMTU(rxmtu);
535 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
536 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
537 addrs[i++] = ifinaddr;
538 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
539 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
540 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
544 rx_maxJumboRecvSize =
545 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
546 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
547 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
550 for (j = 0; j < i; j++) {
551 myNetMTUs[j] = mtus[j];
552 myNetAddrs[j] = addrs[j];
559 /* Returns the afs_cb_interface inxex which best matches address.
560 * If none is found, we return -1.
563 rxi_Findcbi(afs_uint32 addr)
566 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
567 afs_int32 rvalue = -1;
570 if (numMyNetAddrs == 0)
571 (void)rxi_GetcbiInfo();
573 myAddr = ntohl(addr);
575 if (IN_CLASSA(myAddr))
576 netMask = IN_CLASSA_NET;
577 else if (IN_CLASSB(myAddr))
578 netMask = IN_CLASSB_NET;
579 else if (IN_CLASSC(myAddr))
580 netMask = IN_CLASSC_NET;
584 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
585 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
586 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
587 if ((myAddr & netMask) == (thisAddr & netMask)) {
588 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
589 if (myAddr == thisAddr) {
594 if (match_value < 3) {
599 if (match_value < 2) {
610 #else /* AFS_USERSPACE_IP_ADDR */
612 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
613 #define IFADDR2SA(f) (&((f)->ifa_addr))
614 #else /* AFS_AIX41_ENV */
615 #define IFADDR2SA(f) ((f)->ifa_addr)
624 register int rxmtu, maxmtu;
625 afs_uint32 addrs[ADDRSPERSITE];
626 int mtus[ADDRSPERSITE];
628 #if defined(AFS_DARWIN80_ENV)
631 ifaddr_t *ifads, ifad;
632 register ifnet_t ifn;
633 struct sockaddr sout;
634 struct sockaddr_in *sin;
636 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
637 register struct ifnet *ifn;
640 memset(addrs, 0, sizeof(addrs));
641 memset(mtus, 0, sizeof(mtus));
643 #if defined(AFS_DARWIN80_ENV)
644 t = ifnet_get_address_list_family(NULL, &ifads, AF_INET);
646 rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
647 while((ifads[cnt] != NULL) && cnt < ADDRSPERSITE) {
648 t = ifaddr_address(ifads[cnt], &sout, sizeof(sout));
649 sin = (struct sockaddr_in *)&sout;
650 ifinaddr = ntohl(sin->sin_addr.s_addr);
651 if (myNetAddrs[i] != ifinaddr) {
655 rxmtu = rxi_AdjustIfMTU(rxmtu);
657 rxmtu * rxi_nRecvFrags +
658 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
659 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
660 addrs[i++] = ifinaddr;
661 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
662 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
664 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
668 ifnet_free_address_list(ifads);
671 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
672 TAILQ_FOREACH(ifn, &ifnet, if_link) {
673 if (i >= ADDRSPERSITE)
675 #elif defined(AFS_OBSD_ENV)
676 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
677 ifn = ifn->if_list.tqe_next) {
679 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
681 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
682 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
683 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
684 if (i >= ADDRSPERSITE)
686 #elif defined(AFS_OBSD_ENV)
687 for (ifad = ifn->if_addrlist.tqh_first;
688 ifad != NULL && i < ADDRSPERSITE;
689 ifad = ifad->ifa_list.tqe_next) {
691 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
692 ifad = ifad->ifa_next) {
694 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
696 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
698 if (myNetAddrs[i] != ifinaddr) {
702 rxmtu = rxi_AdjustIfMTU(rxmtu);
704 rxmtu * rxi_nRecvFrags +
705 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
706 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
707 addrs[i++] = ifinaddr;
708 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
709 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
711 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
718 rx_maxJumboRecvSize =
719 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
720 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
721 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
725 for (j = 0; j < i; j++) {
726 myNetMTUs[j] = mtus[j];
727 myNetAddrs[j] = addrs[j];
733 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
734 /* Returns ifnet which best matches address */
735 #ifdef AFS_DARWIN80_ENV
740 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
742 struct sockaddr_in s, sr;
743 #ifdef AFS_DARWIN80_ENV
749 s.sin_family = AF_INET;
750 s.sin_addr.s_addr = addr;
751 #ifdef AFS_DARWIN80_ENV
752 ifad = ifaddr_withnet((struct sockaddr *)&s);
754 ifad = ifa_ifwithnet((struct sockaddr *)&s);
757 #ifdef AFS_DARWIN80_ENV
759 ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
760 *maskp = sr.sin_addr.s_addr;
762 return (ifad ? ifaddr_ifnet(ifad) : NULL);
765 *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
766 return (ifad ? ifad->ifa_ifp : NULL);
770 #else /* DARWIN60 || XBSD */
772 /* Returns ifnet which best matches address */
774 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
777 extern struct in_ifaddr *in_ifaddr;
778 struct in_ifaddr *ifa, *ifad = NULL;
782 #if defined(AFS_DARWIN_ENV)
783 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
784 ifa = TAILQ_NEXT(ifa, ia_link)) {
786 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
788 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
789 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
790 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
795 if (match_value < 3) {
800 if (match_value < 2) {
805 } /* if net matches */
806 } /* for all in_ifaddrs */
810 *maskp = ifad->ia_subnetmask;
811 return (ifad ? ifad->ia_ifp : NULL);
813 #endif /* else DARWIN60 || XBSD */
814 #endif /* else AFS_USERSPACE_IP_ADDR */
815 #endif /* !SUN5 && !SGI62 */
818 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
819 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
820 * system specific subdirectories for all systems. But for the moment,
821 * most of it is simple to follow common code.
823 #if !defined(UKERNEL)
824 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
825 /* rxk_NewSocket creates a new socket on the specified port. The port is
826 * in network byte order.
829 rxk_NewSocketHost(afs_uint32 ahost, short aport)
831 register afs_int32 code;
832 #ifdef AFS_DARWIN80_ENV
835 struct socket *newSocket;
837 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
840 struct sockaddr_in myaddr;
841 #ifdef AFS_HPUX110_ENV
842 /* prototype copied from kernel source file streams/str_proto.h */
843 extern MBLKP allocb_wait(int, int);
845 int addrsize = sizeof(struct sockaddr_in);
847 extern struct fileops socketops;
853 AFS_STATCNT(osi_NewSocket);
854 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
855 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
859 #if defined(AFS_HPUX102_ENV)
860 #if defined(AFS_HPUX110_ENV)
861 /* we need a file associated with the socket so sosend in NetSend
863 /* blocking socket */
864 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
868 fp->f_flag = FREAD | FWRITE;
869 fp->f_type = DTYPE_SOCKET;
870 fp->f_ops = &socketops;
872 fp->f_data = (void *)newSocket;
873 newSocket->so_fp = (void *)fp;
875 #else /* AFS_HPUX110_ENV */
876 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
877 #endif /* else AFS_HPUX110_ENV */
878 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
879 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
880 #elif defined(AFS_FBSD50_ENV)
881 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
882 afs_osi_credp, curthread);
883 #elif defined(AFS_FBSD40_ENV)
884 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
885 #elif defined(AFS_DARWIN80_ENV)
886 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
888 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
889 #endif /* AFS_HPUX102_ENV */
893 memset(&myaddr, 0, sizeof myaddr);
894 myaddr.sin_family = AF_INET;
895 myaddr.sin_port = aport;
896 myaddr.sin_addr.s_addr = ahost;
897 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
898 myaddr.sin_len = sizeof(myaddr);
901 #ifdef AFS_HPUX110_ENV
902 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
907 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
909 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
911 code = sobind(newSocket, bindnam, addrsize);
914 #if !defined(AFS_HPUX1122_ENV)
921 #else /* AFS_HPUX110_ENV */
922 #if defined(AFS_DARWIN80_ENV)
927 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
928 &buflen, sizeof(buflen));
929 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
930 &buflen, sizeof(buflen));
934 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
939 code = soreserve(newSocket, 50000, 50000);
941 code = soreserve(newSocket, 32766, 32766);
943 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
946 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
947 #if defined(AFS_FBSD50_ENV)
948 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
949 #elif defined(AFS_FBSD40_ENV)
950 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
952 code = sobind(newSocket, (struct sockaddr *)&myaddr);
955 dpf(("sobind fails (%d)\n", (int)code));
960 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
962 nam = m_getclr(M_WAIT, MT_SONAME);
963 #else /* AFS_OSF_ENV */
964 nam = m_get(M_WAIT, MT_SONAME);
967 #if defined(KERNEL_HAVE_UERROR)
972 nam->m_len = sizeof(myaddr);
973 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
975 BHV_PDATA(&bhv) = (void *)newSocket;
976 code = sobind(&bhv, nam);
979 code = sobind(newSocket, nam);
982 dpf(("sobind fails (%d)\n", (int)code));
984 #ifndef AFS_SGI65_ENV
989 #endif /* else AFS_DARWIN_ENV */
990 #endif /* else AFS_HPUX110_ENV */
993 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
994 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
996 return (osi_socket *)newSocket;
1000 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1001 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1003 return (osi_socket *)0;
1007 rxk_NewSocket(short aport)
1009 return rxk_NewSocketHost(0, aport);
1012 /* free socket allocated by rxk_NewSocket */
1014 rxk_FreeSocket(register struct socket *asocket)
1016 AFS_STATCNT(osi_FreeSocket);
1017 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1018 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1020 #ifdef AFS_HPUX110_ENV
1021 if (asocket->so_fp) {
1022 struct file *fp = asocket->so_fp;
1023 #if !defined(AFS_HPUX1123_ENV)
1024 /* 11.23 still has falloc, but not FPENTRYFREE !
1025 * so for now if we shutdown, we will waist a file
1028 asocket->so_fp = NULL;
1031 #endif /* AFS_HPUX110_ENV */
1033 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1034 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1038 #endif /* !SUN5 && !LINUX20 */
1040 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1042 * Run RX event daemon every second (5 times faster than rest of systems)
1045 afs_rxevent_daemon(void)
1051 #ifdef RX_ENABLE_LOCKS
1053 #endif /* RX_ENABLE_LOCKS */
1055 rxevent_RaiseEvents(&temp);
1057 #ifdef RX_ENABLE_LOCKS
1059 #endif /* RX_ENABLE_LOCKS */
1060 #ifdef RX_KERNEL_TRACE
1061 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1062 "before afs_osi_Wait()");
1064 afs_osi_Wait(500, NULL, 0);
1065 #ifdef RX_KERNEL_TRACE
1066 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1067 "after afs_osi_Wait()");
1069 if (afs_termState == AFSOP_STOP_RXEVENT) {
1070 #ifdef RXK_LISTENER_ENV
1071 afs_termState = AFSOP_STOP_RXK_LISTENER;
1073 afs_termState = AFSOP_STOP_COMPLETE;
1075 osi_rxWakeup(&afs_termState);
1082 #ifdef RXK_LISTENER_ENV
1084 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1086 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1089 struct sockaddr_in from;
1092 register afs_int32 tlen;
1093 afs_int32 savelen; /* was using rlen but had aliasing problems */
1094 rx_computelen(p, tlen);
1095 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1097 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1098 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1099 * it once in order to avoid races. */
1102 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1110 /* add some padding to the last iovec, it's just to make sure that the
1111 * read doesn't return more data than we expect, and is done to get around
1112 * our problems caused by the lack of a length field in the rx header. */
1113 savelen = p->wirevec[p->niovecs - 1].iov_len;
1114 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1116 nbytes = tlen + sizeof(afs_int32);
1117 #ifdef RX_KERNEL_TRACE
1118 if (ICL_SETACTIVE(afs_iclSetp)) {
1120 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1121 "before osi_NetRecive()");
1125 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1127 #ifdef RX_KERNEL_TRACE
1128 if (ICL_SETACTIVE(afs_iclSetp)) {
1130 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1131 "after osi_NetRecive()");
1135 /* restore the vec to its correct state */
1136 p->wirevec[p->niovecs - 1].iov_len = savelen;
1139 p->length = nbytes - RX_HEADER_SIZE;;
1140 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1142 rxi_MorePackets(rx_initSendWindow);
1144 MUTEX_ENTER(&rx_stats_mutex);
1145 rx_stats.bogusPacketOnRead++;
1146 rx_stats.bogusHost = from.sin_addr.s_addr;
1147 MUTEX_EXIT(&rx_stats_mutex);
1148 dpf(("B: bogus packet from [%x,%d] nb=%d",
1149 from.sin_addr.s_addr, from.sin_port, nbytes));
1153 /* Extract packet header. */
1154 rxi_DecodePacketHeader(p);
1156 *host = from.sin_addr.s_addr;
1157 *port = from.sin_port;
1158 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1159 MUTEX_ENTER(&rx_stats_mutex);
1160 rx_stats.packetsRead[p->header.type - 1]++;
1161 MUTEX_EXIT(&rx_stats_mutex);
1164 /* Free any empty packet buffers at the end of this packet */
1165 rxi_TrimDataBufs(p, 1);
1175 * Listen for packets on socket. This thread is typically started after
1176 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1177 * the start state is set before proceeding.
1179 * Note that this thread is outside the AFS global lock for much of
1182 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1183 * we want here. So we need to either block all signals (including SIGKILL
1184 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1185 * OS's socket receive routine returns as a result of a signal.
1187 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1191 * Run the listener as a kernel thread.
1197 void rxk_ListenerProc(void);
1199 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1200 minclsyspri) == NULL)
1201 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1205 rxk_ListenerProc(void)
1206 #else /* AFS_SUN5_ENV */
1209 #endif /* AFS_SUN5_ENV */
1211 struct rx_packet *rxp = NULL;
1215 #ifdef AFS_LINUX20_ENV
1216 rxk_ListenerPid = current->pid;
1219 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1220 #endif /* AFS_SUN5_ENV */
1222 rxk_ListenerPid = curproc->p_pid;
1223 #endif /* AFS_FBSD_ENV */
1224 #ifdef AFS_DARWIN80_ENV
1225 rxk_ListenerPid = proc_selfpid();
1226 #elif defined(AFS_DARWIN_ENV)
1227 rxk_ListenerPid = current_proc()->p_pid;
1229 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1231 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1232 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1234 rxi_RestoreDataBufs(rxp);
1236 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1238 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1240 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1241 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1245 #ifdef RX_ENABLE_LOCKS
1247 #endif /* RX_ENABLE_LOCKS */
1248 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1249 afs_termState = AFSOP_STOP_COMPLETE;
1250 osi_rxWakeup(&afs_termState);
1252 rxk_ListenerPid = 0;
1253 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1254 osi_rxWakeup(&rxk_ListenerPid);
1258 #endif /* AFS_SUN5_ENV */
1261 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1262 /* The manner of stopping the rx listener thread may vary. Most unix's should
1263 * be able to call soclose.
1266 osi_StopListener(void)
1271 #endif /* RXK_LISTENER_ENV */
1273 #endif /* !NCR && !UKERNEL */