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"
19 #include "rx_atomic.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)
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_utoa() - write the NUL-terminated ASCII decimal form of the given
129 * unsigned long value into the given buffer. Returns 0 on success,
130 * and a value less than 0 on failure. The contents of the buffer is
131 * defined only on success.
135 osi_utoa(char *buf, size_t len, unsigned long val)
137 long k; /* index of first byte of string value */
139 /* we definitely need room for at least one digit and NUL */
145 /* compute the string form from the high end of the buffer */
148 for (k = len - 2; k >= 0; k--) {
149 buf[k] = val % 10 + '0';
156 /* did we finish converting val to string form? */
162 /* this should never happen */
168 /* this should never happen */
174 /* if necessary, relocate string to beginning of buf[] */
179 * We need to achieve the effect of calling
181 * memmove(buf, &buf[k], len - k);
183 * However, since memmove() is not available in all
184 * kernels, we explicitly do an appropriate copy.
190 while ((*dst++ = *src++) != '\0')
197 #ifndef AFS_LINUX26_ENV
199 * osi_AssertFailK() -- used by the osi_Assert() macro.
201 * It essentially does
203 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
205 * Since the kernel version of osi_Panic() only passes its first
206 * argument to the native panic(), we construct a single string and hand
207 * that to osi_Panic().
210 osi_AssertFailK(const char *expr, const char *file, int line)
212 static const char msg0[] = "assertion failed: ";
213 static const char msg1[] = ", file: ";
214 static const char msg2[] = ", line: ";
215 static const char msg3[] = "\n";
218 * These buffers add up to 1K, which is a pleasantly nice round
219 * value, but probably not vital.
224 /* check line number conversion */
226 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
227 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
232 #define ADDBUF(BUF, STR) \
233 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
234 strcat(BUF, (char *)(STR)); \
243 ADDBUF(buf, linebuf);
248 osi_Panic("%s", buf);
253 /* This is the server process request loop. Kernel server
254 * processes never become listener threads */
256 rx_ServerProc(void *unused)
260 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
261 MUTEX_ENTER(&rx_quota_mutex);
262 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
263 /* threadID is used for making decisions in GetCall. Get it by bumping
264 * number of threads handling incoming calls */
265 threadID = rxi_availProcs++;
266 MUTEX_EXIT(&rx_quota_mutex);
268 #ifdef RX_ENABLE_LOCKS
270 #endif /* RX_ENABLE_LOCKS */
271 rxi_ServerProc(threadID, NULL, NULL);
272 #ifdef RX_ENABLE_LOCKS
274 #endif /* RX_ENABLE_LOCKS */
278 #endif /* !UKERNEL */
280 #ifndef RXK_LISTENER_ENV
281 /* asize includes the Rx header */
283 MyPacketProc(struct rx_packet **ahandle, int asize)
285 struct rx_packet *tp;
287 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
288 /* If we can't scrounge enough cbufs, then we have to drop the packet,
289 * but we should set a flag so we magic up some more at our leisure.
292 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
293 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
294 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
296 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
297 RX_PACKET_CLASS_RECV_CBUF)) {
300 if (rx_stats_active) {
301 rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
307 * XXX if packet is too long for our buffer,
308 * should do this at a higher layer and let other
309 * end know we're losing.
311 if (rx_stats_active) {
312 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
314 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
315 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
321 /* otherwise we have a packet, set appropriate values */
327 MyArrivalProc(struct rx_packet *ahandle,
328 struct sockaddr_in *afrom,
329 struct socket *arock,
332 /* handle basic rx packet */
333 ahandle->length = asize - RX_HEADER_SIZE;
334 rxi_DecodePacketHeader(ahandle);
336 rxi_ReceivePacket(ahandle, arock,
337 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
340 /* free the packet if it has been returned */
342 rxi_FreePacket(ahandle);
345 #endif /* !RXK_LISTENER_ENV */
348 rxi_StartListener(void)
350 #if !defined(RXK_LISTENER_ENV) && !defined(RXK_UPCALL_ENV)
351 /* if kernel, give name of appropriate procedures */
352 rxk_GetPacketProc = MyPacketProc;
353 rxk_PacketArrivalProc = MyArrivalProc;
358 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
359 to get interesting information. */
361 rxi_InitPeerParams(struct rx_peer *pp)
366 # ifndef AFS_SUN5_ENV
367 # ifdef AFS_USERSPACE_IP_ADDR
371 i = rxi_Findcbi(pp->host);
373 rx_rto_setPeerTimeoutSecs(pp, 3);
374 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
376 rx_rto_setPeerTimeoutSecs(pp, 2);
377 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
378 mtu = ntohl(afs_cb_interface.mtu[i]);
379 /* Diminish the packet size to one based on the MTU given by
381 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
382 rxmtu = mtu - RX_IPUDP_SIZE;
383 if (rxmtu < pp->ifMTU)
387 # else /* AFS_USERSPACE_IP_ADDR */
390 # if !defined(AFS_SGI62_ENV)
391 if (numMyNetAddrs == 0)
392 (void)rxi_GetIFInfo();
395 ifn = rxi_FindIfnet(pp->host, NULL);
397 rx_rto_setPeerTimeoutSecs(pp, 2);
398 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
399 # ifdef IFF_POINTOPOINT
400 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
401 /* wish we knew the bit rate and the chunk size, sigh. */
402 rx_rto_setPeerTimeoutSecs(pp, 4);
403 pp->ifMTU = RX_PP_PACKET_SIZE;
405 # endif /* IFF_POINTOPOINT */
406 /* Diminish the packet size to one based on the MTU given by
408 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
409 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
410 if (rxmtu < pp->ifMTU)
413 } else { /* couldn't find the interface, so assume the worst */
414 rx_rto_setPeerTimeoutSecs(pp, 3);
415 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
417 # endif /* else AFS_USERSPACE_IP_ADDR */
418 # else /* AFS_SUN5_ENV */
421 mtu = rxi_FindIfMTU(pp->host);
424 rx_rto_setPeerTimeoutSecs(pp, 3);
425 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
427 rx_rto_setPeerTimeoutSecs(pp, 2);
428 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
430 /* Diminish the packet size to one based on the MTU given by
432 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
433 rxmtu = mtu - RX_IPUDP_SIZE;
434 if (rxmtu < pp->ifMTU)
438 # endif /* AFS_SUN5_ENV */
439 #else /* ADAPT_MTU */
440 pp->rateFlag = 2; /* start timing after two full packets */
441 rx_rto_setPeerTimeoutSecs(pp, 2);
442 pp->ifMTU = OLD_MAX_PACKET_SIZE;
443 #endif /* else ADAPT_MTU */
444 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
445 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
446 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
448 MIN(rxi_nDgramPackets,
449 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
450 pp->maxDgramPackets = 1;
452 /* Initialize slow start parameters */
453 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
455 pp->nDgramPackets = 1;
460 /* The following code is common to several system types, but not all. The
461 * separate ones are found in the system specific subdirectories.
465 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
466 /* Routine called during the afsd "-shutdown" process to put things back to
469 static struct protosw parent_proto; /* udp proto switch */
472 shutdown_rxkernel(void)
474 struct protosw *tpro, *last;
475 last = inetdomain.dom_protoswNPROTOSW;
476 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
477 if (tpro->pr_protocol == IPPROTO_UDP) {
478 /* restore original udp protocol switch */
479 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
480 memset((void *)&parent_proto, 0, sizeof(parent_proto));
485 dpf(("shutdown_rxkernel: no udp proto\n"));
487 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
489 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
490 /* Determine what the network interfaces are for this machine. */
492 #ifdef AFS_USERSPACE_IP_ADDR
496 int i, j, different = 0, num = ADDRSPERSITE;
499 afs_uint32 addrs[ADDRSPERSITE];
500 int mtus[ADDRSPERSITE];
502 memset((void *)addrs, 0, sizeof(addrs));
503 memset((void *)mtus, 0, sizeof(mtus));
505 if (afs_cb_interface.numberOfInterfaces < num)
506 num = afs_cb_interface.numberOfInterfaces;
507 for (i = 0; i < num; i++) {
508 if (!afs_cb_interface.mtu[i])
509 afs_cb_interface.mtu[i] = htonl(1500);
510 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
511 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
512 if (myNetAddrs[i] != ifinaddr)
516 rxmtu = rxi_AdjustIfMTU(rxmtu);
518 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
519 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
520 addrs[i++] = ifinaddr;
521 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
522 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
523 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
527 rx_maxJumboRecvSize =
528 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
529 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
530 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
533 for (j = 0; j < i; j++) {
534 myNetMTUs[j] = mtus[j];
535 myNetAddrs[j] = addrs[j];
542 /* Returns the afs_cb_interface inxex which best matches address.
543 * If none is found, we return -1.
546 rxi_Findcbi(afs_uint32 addr)
549 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
550 afs_int32 rvalue = -1;
553 if (numMyNetAddrs == 0)
554 (void)rxi_GetcbiInfo();
556 myAddr = ntohl(addr);
558 if (IN_CLASSA(myAddr))
559 netMask = IN_CLASSA_NET;
560 else if (IN_CLASSB(myAddr))
561 netMask = IN_CLASSB_NET;
562 else if (IN_CLASSC(myAddr))
563 netMask = IN_CLASSC_NET;
567 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
568 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
569 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
570 if ((myAddr & netMask) == (thisAddr & netMask)) {
571 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
572 if (myAddr == thisAddr) {
577 if (match_value < 3) {
582 if (match_value < 2) {
593 #else /* AFS_USERSPACE_IP_ADDR */
595 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
596 #define IFADDR2SA(f) (&((f)->ifa_addr))
597 #else /* AFS_AIX41_ENV */
598 #define IFADDR2SA(f) ((f)->ifa_addr)
608 afs_uint32 addrs[ADDRSPERSITE];
609 int mtus[ADDRSPERSITE];
611 #if defined(AFS_DARWIN80_ENV)
617 struct sockaddr sout;
618 struct sockaddr_in *sin;
621 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
625 memset(addrs, 0, sizeof(addrs));
626 memset(mtus, 0, sizeof(mtus));
628 #if defined(AFS_DARWIN80_ENV)
629 if (!ifnet_list_get(AF_INET, &ifns, &count)) {
630 for (m = 0; m < count; m++) {
631 if (!ifnet_get_address_list(ifns[m], &ifads)) {
632 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
633 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
634 sin = (struct sockaddr_in *)&sout;
635 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
636 ifinaddr = ntohl(sin->sin_addr.s_addr);
637 if (myNetAddrs[i] != ifinaddr) {
641 rxmtu = rxi_AdjustIfMTU(rxmtu);
643 rxmtu * rxi_nRecvFrags +
644 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
645 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
646 addrs[i++] = ifinaddr;
647 if (!rx_IsLoopbackAddr(ifinaddr) &&
648 (maxmtu > rx_maxReceiveSize)) {
650 MIN(RX_MAX_PACKET_SIZE, maxmtu);
652 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
657 ifnet_free_address_list(ifads);
660 ifnet_list_free(ifns);
663 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
664 #if defined(AFS_FBSD80_ENV)
665 TAILQ_FOREACH(ifn, &V_ifnet, if_link) {
667 TAILQ_FOREACH(ifn, &ifnet, if_link) {
669 if (i >= ADDRSPERSITE)
671 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
672 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
673 ifn = ifn->if_list.tqe_next) {
675 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
677 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
678 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
679 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
680 if (i >= ADDRSPERSITE)
682 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
683 for (ifad = ifn->if_addrlist.tqh_first;
684 ifad != NULL && i < ADDRSPERSITE;
685 ifad = ifad->ifa_list.tqe_next) {
687 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
688 ifad = ifad->ifa_next) {
690 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
692 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
694 if (myNetAddrs[i] != ifinaddr) {
698 rxmtu = rxi_AdjustIfMTU(rxmtu);
700 rxmtu * rxi_nRecvFrags +
701 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
702 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
703 addrs[i++] = ifinaddr;
704 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
705 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
707 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
714 rx_maxJumboRecvSize =
715 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
716 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
717 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
721 for (l = 0; l < i; l++) {
722 myNetMTUs[l] = mtus[l];
723 myNetAddrs[l] = addrs[l];
729 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
730 /* Returns ifnet which best matches address */
732 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
734 struct sockaddr_in s, sr;
737 s.sin_family = AF_INET;
738 s.sin_addr.s_addr = addr;
739 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
742 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
743 *maskp = sr.sin_addr.s_addr;
745 return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
748 #else /* DARWIN || XBSD */
750 /* Returns ifnet which best matches address */
752 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
755 extern struct in_ifaddr *in_ifaddr;
756 struct in_ifaddr *ifa, *ifad = NULL;
760 #if defined(AFS_DARWIN_ENV)
761 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
762 ifa = TAILQ_NEXT(ifa, ia_link)) {
764 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
766 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
767 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
768 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
773 if (match_value < 3) {
778 if (match_value < 2) {
783 } /* if net matches */
784 } /* for all in_ifaddrs */
788 *maskp = ifad->ia_subnetmask;
789 return (ifad ? ifad->ia_ifp : NULL);
791 #endif /* else DARWIN || XBSD */
792 #endif /* else AFS_USERSPACE_IP_ADDR */
793 #endif /* !SUN5 && !SGI62 */
796 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
797 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
798 * system specific subdirectories for all systems. But for the moment,
799 * most of it is simple to follow common code.
801 #if !defined(UKERNEL)
802 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
803 /* rxk_NewSocket creates a new socket on the specified port. The port is
804 * in network byte order.
807 rxk_NewSocketHost(afs_uint32 ahost, short aport)
810 #ifdef AFS_DARWIN80_ENV
813 struct socket *newSocket;
815 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
818 struct sockaddr_in myaddr;
819 #ifdef AFS_HPUX110_ENV
820 /* prototype copied from kernel source file streams/str_proto.h */
821 extern MBLKP allocb_wait(int, int);
823 int addrsize = sizeof(struct sockaddr_in);
825 extern struct fileops socketops;
831 AFS_STATCNT(osi_NewSocket);
832 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
833 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
837 #if defined(AFS_HPUX102_ENV)
838 #if defined(AFS_HPUX110_ENV)
839 /* we need a file associated with the socket so sosend in NetSend
841 /* blocking socket */
842 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
846 fp->f_flag = FREAD | FWRITE;
847 fp->f_type = DTYPE_SOCKET;
848 fp->f_ops = &socketops;
850 fp->f_data = (void *)newSocket;
851 newSocket->so_fp = (void *)fp;
853 #else /* AFS_HPUX110_ENV */
854 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
855 #endif /* else AFS_HPUX110_ENV */
856 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
857 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
858 #elif defined(AFS_FBSD_ENV)
859 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
860 afs_osi_credp, curthread);
861 #elif defined(AFS_DARWIN80_ENV)
862 #ifdef RXK_LISTENER_ENV
863 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
865 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, rx_upcall, NULL, &newSocket);
867 #elif defined(AFS_NBSD50_ENV)
868 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
869 #elif defined(AFS_NBSD40_ENV)
870 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
872 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
873 #endif /* AFS_HPUX102_ENV */
877 memset(&myaddr, 0, sizeof myaddr);
878 myaddr.sin_family = AF_INET;
879 myaddr.sin_port = aport;
880 myaddr.sin_addr.s_addr = ahost;
881 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
882 myaddr.sin_len = sizeof(myaddr);
885 #ifdef AFS_HPUX110_ENV
886 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
891 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
893 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
894 #if defined(AFS_NBSD40_ENV)
895 code = sobind(newSocket, bindnam, addrsize, osi_curproc());
897 code = sobind(newSocket, bindnam, addrsize);
901 #if !defined(AFS_HPUX1122_ENV)
908 #else /* AFS_HPUX110_ENV */
909 #if defined(AFS_DARWIN80_ENV)
914 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
915 &buflen, sizeof(buflen));
916 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
917 &buflen, sizeof(buflen));
921 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
926 #if defined(AFS_NBSD_ENV)
929 code = soreserve(newSocket, 50000, 50000);
931 code = soreserve(newSocket, 32766, 32766);
933 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
935 #if defined(AFS_NBSD_ENV)
939 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
940 #if defined(AFS_FBSD_ENV)
941 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
943 code = sobind(newSocket, (struct sockaddr *)&myaddr);
946 dpf(("sobind fails (%d)\n", (int)code));
950 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
952 nam = m_getclr(M_WAIT, MT_SONAME);
953 #else /* AFS_OSF_ENV */
954 nam = m_get(M_WAIT, MT_SONAME);
957 #if defined(KERNEL_HAVE_UERROR)
962 nam->m_len = sizeof(myaddr);
963 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
964 #if defined(AFS_SGI65_ENV)
965 BHV_PDATA(&bhv) = (void *)newSocket;
966 code = sobind(&bhv, nam);
968 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
969 code = sobind(newSocket, nam, osi_curproc());
971 code = sobind(newSocket, nam);
974 dpf(("sobind fails (%d)\n", (int)code));
976 #ifndef AFS_SGI65_ENV
981 #endif /* else AFS_DARWIN_ENV */
982 #endif /* else AFS_HPUX110_ENV */
985 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
986 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
988 return (osi_socket *)newSocket;
992 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
993 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
995 return (osi_socket *)0;
999 rxk_NewSocket(short aport)
1001 return rxk_NewSocketHost(0, aport);
1004 /* free socket allocated by rxk_NewSocket */
1006 rxk_FreeSocket(struct socket *asocket)
1008 AFS_STATCNT(osi_FreeSocket);
1009 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1010 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1012 #ifdef AFS_HPUX110_ENV
1013 if (asocket->so_fp) {
1014 struct file *fp = asocket->so_fp;
1015 #if !defined(AFS_HPUX1123_ENV)
1016 /* 11.23 still has falloc, but not FPENTRYFREE !
1017 * so for now if we shutdown, we will waist a file
1020 asocket->so_fp = NULL;
1023 #endif /* AFS_HPUX110_ENV */
1025 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1026 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1030 #endif /* !SUN5 && !LINUX20 */
1032 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV) || defined(RXK_UPCALL_ENV)
1033 #ifdef RXK_TIMEDSLEEP_ENV
1034 /* Shutting down should wake us up, as should an earlier event. */
1036 rxi_ReScheduleEvents(void)
1038 /* needed to allow startup */
1039 int glock = ISAFS_GLOCK();
1042 osi_rxWakeup(&afs_termState);
1048 * Run RX event daemon every second (5 times faster than rest of systems)
1051 afs_rxevent_daemon(void)
1057 #ifdef RX_ENABLE_LOCKS
1059 #endif /* RX_ENABLE_LOCKS */
1061 rxevent_RaiseEvents(&temp);
1063 #ifdef RX_ENABLE_LOCKS
1065 #endif /* RX_ENABLE_LOCKS */
1066 #ifdef RX_KERNEL_TRACE
1067 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1068 "before afs_osi_Wait()");
1070 #ifdef RXK_TIMEDSLEEP_ENV
1071 afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1072 (temp.usec / 1000))), 0);
1074 afs_osi_Wait(500, NULL, 0);
1076 #ifdef RX_KERNEL_TRACE
1077 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1078 "after afs_osi_Wait()");
1080 if (afs_termState == AFSOP_STOP_RXEVENT) {
1081 #ifdef RXK_LISTENER_ENV
1082 afs_termState = AFSOP_STOP_RXK_LISTENER;
1083 #elif defined(AFS_SUN510_ENV) || defined(RXK_UPCALL_ENV)
1084 afs_termState = AFSOP_STOP_NETIF;
1086 afs_termState = AFSOP_STOP_COMPLETE;
1088 osi_rxWakeup(&afs_termState);
1095 #ifdef RXK_LISTENER_ENV
1097 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1099 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1102 struct sockaddr_in from;
1106 afs_int32 savelen; /* was using rlen but had aliasing problems */
1107 rx_computelen(p, tlen);
1108 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1110 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1111 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1112 * it once in order to avoid races. */
1115 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1123 /* add some padding to the last iovec, it's just to make sure that the
1124 * read doesn't return more data than we expect, and is done to get around
1125 * our problems caused by the lack of a length field in the rx header. */
1126 savelen = p->wirevec[p->niovecs - 1].iov_len;
1127 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1129 nbytes = tlen + sizeof(afs_int32);
1130 #ifdef RX_KERNEL_TRACE
1131 if (ICL_SETACTIVE(afs_iclSetp)) {
1133 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1134 "before osi_NetRecive()");
1138 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1140 #ifdef RX_KERNEL_TRACE
1141 if (ICL_SETACTIVE(afs_iclSetp)) {
1143 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1144 "after osi_NetRecive()");
1148 /* restore the vec to its correct state */
1149 p->wirevec[p->niovecs - 1].iov_len = savelen;
1152 p->length = nbytes - RX_HEADER_SIZE;;
1153 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1155 if (rx_stats_active) {
1156 MUTEX_ENTER(&rx_stats_mutex);
1157 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1158 rx_stats.bogusHost = from.sin_addr.s_addr;
1159 MUTEX_EXIT(&rx_stats_mutex);
1161 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1162 from.sin_addr.s_addr, from.sin_port, nbytes));
1166 /* Extract packet header. */
1167 rxi_DecodePacketHeader(p);
1169 *host = from.sin_addr.s_addr;
1170 *port = from.sin_port;
1171 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1172 if (rx_stats_active) {
1173 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1177 #ifdef RX_TRIMDATABUFS
1178 /* Free any empty packet buffers at the end of this packet */
1179 rxi_TrimDataBufs(p, 1);
1189 * Listen for packets on socket. This thread is typically started after
1190 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1191 * the start state is set before proceeding.
1193 * Note that this thread is outside the AFS global lock for much of
1196 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1197 * we want here. So we need to either block all signals (including SIGKILL
1198 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1199 * OS's socket receive routine returns as a result of a signal.
1201 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1202 #ifdef AFS_LINUX20_ENV
1203 struct task_struct *rxk_ListenerTask;
1209 struct rx_packet *rxp = NULL;
1213 #ifdef AFS_LINUX20_ENV
1214 rxk_ListenerPid = current->pid;
1215 rxk_ListenerTask = current;
1218 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1219 #endif /* AFS_SUN5_ENV */
1221 rxk_ListenerPid = curproc->p_pid;
1222 #endif /* AFS_FBSD_ENV */
1223 #ifdef AFS_DARWIN80_ENV
1224 rxk_ListenerPid = proc_selfpid();
1225 #elif defined(AFS_DARWIN_ENV)
1226 rxk_ListenerPid = current_proc()->p_pid;
1228 #ifdef RX_ENABLE_LOCKS
1230 #endif /* RX_ENABLE_LOCKS */
1231 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1232 /* See if a check for additional packets was issued */
1236 rxi_RestoreDataBufs(rxp);
1238 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1240 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1242 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1243 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1247 #ifdef RX_ENABLE_LOCKS
1249 #endif /* RX_ENABLE_LOCKS */
1250 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1251 #ifdef AFS_SUN510_ENV
1252 afs_termState = AFSOP_STOP_NETIF;
1254 afs_termState = AFSOP_STOP_COMPLETE;
1256 osi_rxWakeup(&afs_termState);
1258 rxk_ListenerPid = 0;
1259 #ifdef AFS_LINUX20_ENV
1260 rxk_ListenerTask = 0;
1261 osi_rxWakeup(&rxk_ListenerTask);
1263 #if defined(AFS_SUN5_ENV) || defined(AFS_FBSD_ENV)
1264 osi_rxWakeup(&rxk_ListenerPid);
1268 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1269 /* The manner of stopping the rx listener thread may vary. Most unix's should
1270 * be able to call soclose.
1273 osi_StopListener(void)
1278 #endif /* RXK_LISTENER_ENV */
1279 #endif /* !NCR && !UKERNEL */
1281 #if !defined(AFS_LINUX26_ENV)
1283 #if defined(AFS_AIX_ENV)
1284 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1286 osi_Panic(char *msg, ...)
1291 msg = "Unknown AFS panic";
1293 * we should probably use the errsave facility here. it is not
1297 printf(msg, a1, a2, a3);
1299 #elif defined(AFS_SGI_ENV)
1302 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1304 cmn_err(CE_PANIC, "Unknown AFS panic");
1307 icmn_err(CE_PANIC, msg, ap);
1310 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1314 msg = "Unknown AFS panic";
1317 vsnprintf(buf, sizeof(buf), msg, ap);
1324 msg = "Unknown AFS panic";
1329 # ifdef AFS_LINUX20_ENV