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_packet.h"
20 #include "rx_atomic.h"
23 #ifdef AFS_HPUX110_ENV
29 #ifndef RXK_LISTENER_ENV
30 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
31 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
34 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
35 extern struct interfaceAddr afs_cb_interface;
37 rxk_ports_t rxk_ports;
38 rxk_portRocks_t rxk_portRocks;
42 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
43 #define ADDRSPERSITE 16
44 static afs_uint32 myNetAddrs[ADDRSPERSITE];
45 static int myNetMTUs[ADDRSPERSITE];
46 static int numMyNetAddrs = 0;
49 #if defined(AFS_DARWIN80_ENV)
50 #define sobind sock_bind
51 #define soclose sock_close
54 /* add a port to the monitored list, port # is in network order */
56 rxk_AddPort(u_short aport, char *arock)
59 unsigned short *tsp, ts;
62 zslot = -1; /* look for an empty slot simultaneously */
63 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
64 if (((ts = *tsp) == 0) && (zslot == -1))
70 /* otherwise allocate a new port slot */
72 return E2BIG; /* all full */
73 rxk_ports[zslot] = aport;
74 rxk_portRocks[zslot] = arock;
78 /* remove as port from the monitored list, port # is in network order */
80 rxk_DelPort(u_short aport)
85 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
87 /* found it, adjust ref count and free the port reference if all gone */
92 /* otherwise port not found */
97 rxk_shutdownPorts(void)
100 for (i = 0; i < MAXRXPORTS; i++) {
103 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
104 soclose((struct socket *)rxk_portRocks[i]);
106 rxk_portRocks[i] = NULL;
112 rxi_GetHostUDPSocket(u_int host, u_short port)
115 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
116 if (sockp == (osi_socket *)0)
117 return OSI_NULLSOCKET;
118 rxk_AddPort(port, (char *)sockp);
119 return (osi_socket) sockp;
123 rxi_GetUDPSocket(u_short port)
125 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
129 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
130 * unsigned long value into the given buffer. Returns 0 on success,
131 * and a value less than 0 on failure. The contents of the buffer is
132 * defined only on success.
136 osi_utoa(char *buf, size_t len, unsigned long val)
138 long k; /* index of first byte of string value */
140 /* we definitely need room for at least one digit and NUL */
146 /* compute the string form from the high end of the buffer */
149 for (k = len - 2; k >= 0; k--) {
150 buf[k] = val % 10 + '0';
157 /* did we finish converting val to string form? */
163 /* this should never happen */
169 /* this should never happen */
175 /* if necessary, relocate string to beginning of buf[] */
180 * We need to achieve the effect of calling
182 * memmove(buf, &buf[k], len - k);
184 * However, since memmove() is not available in all
185 * kernels, we explicitly do an appropriate copy.
191 while ((*dst++ = *src++) != '\0')
198 #ifndef AFS_LINUX26_ENV
200 * osi_AssertFailK() -- used by the osi_Assert() macro.
202 * It essentially does
204 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
206 * Since the kernel version of osi_Panic() only passes its first
207 * argument to the native panic(), we construct a single string and hand
208 * that to osi_Panic().
211 osi_AssertFailK(const char *expr, const char *file, int line)
213 static const char msg0[] = "assertion failed: ";
214 static const char msg1[] = ", file: ";
215 static const char msg2[] = ", line: ";
216 static const char msg3[] = "\n";
219 * These buffers add up to 1K, which is a pleasantly nice round
220 * value, but probably not vital.
225 /* check line number conversion */
227 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
228 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
233 #define ADDBUF(BUF, STR) \
234 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
235 strcat(BUF, (char *)(STR)); \
244 ADDBUF(buf, linebuf);
249 osi_Panic("%s", buf);
254 /* This is the server process request loop. Kernel server
255 * processes never become listener threads */
257 rx_ServerProc(void *unused)
261 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
262 MUTEX_ENTER(&rx_quota_mutex);
263 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
264 /* threadID is used for making decisions in GetCall. Get it by bumping
265 * number of threads handling incoming calls */
266 threadID = rxi_availProcs++;
267 MUTEX_EXIT(&rx_quota_mutex);
269 #ifdef RX_ENABLE_LOCKS
271 #endif /* RX_ENABLE_LOCKS */
272 rxi_ServerProc(threadID, NULL, NULL);
273 #ifdef RX_ENABLE_LOCKS
275 #endif /* RX_ENABLE_LOCKS */
279 #endif /* !UKERNEL */
281 #ifndef RXK_LISTENER_ENV
282 /* asize includes the Rx header */
284 MyPacketProc(struct rx_packet **ahandle, int asize)
286 struct rx_packet *tp;
288 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
289 /* If we can't scrounge enough cbufs, then we have to drop the packet,
290 * but we should set a flag so we magic up some more at our leisure.
293 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
294 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
295 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
297 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
298 RX_PACKET_CLASS_RECV_CBUF)) {
301 if (rx_stats_active) {
302 rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
308 * XXX if packet is too long for our buffer,
309 * should do this at a higher layer and let other
310 * end know we're losing.
312 if (rx_stats_active) {
313 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
315 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
316 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
322 /* otherwise we have a packet, set appropriate values */
328 MyArrivalProc(struct rx_packet *ahandle,
329 struct sockaddr_in *afrom,
330 struct socket *arock,
333 /* handle basic rx packet */
334 ahandle->length = asize - RX_HEADER_SIZE;
335 rxi_DecodePacketHeader(ahandle);
337 rxi_ReceivePacket(ahandle, arock,
338 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
341 /* free the packet if it has been returned */
343 rxi_FreePacket(ahandle);
346 #endif /* !RXK_LISTENER_ENV */
349 rxi_StartListener(void)
351 #if !defined(RXK_LISTENER_ENV) && !defined(RXK_UPCALL_ENV)
352 /* if kernel, give name of appropriate procedures */
353 rxk_GetPacketProc = MyPacketProc;
354 rxk_PacketArrivalProc = MyArrivalProc;
359 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
360 to get interesting information. */
362 rxi_InitPeerParams(struct rx_peer *pp)
367 # ifndef AFS_SUN5_ENV
368 # ifdef AFS_USERSPACE_IP_ADDR
372 i = rxi_Findcbi(pp->host);
374 rx_rto_setPeerTimeoutSecs(pp, 3);
375 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
377 rx_rto_setPeerTimeoutSecs(pp, 2);
378 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
379 mtu = ntohl(afs_cb_interface.mtu[i]);
380 /* Diminish the packet size to one based on the MTU given by
382 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
383 rxmtu = mtu - RX_IPUDP_SIZE;
384 if (rxmtu < pp->ifMTU)
388 # else /* AFS_USERSPACE_IP_ADDR */
391 # if !defined(AFS_SGI62_ENV)
392 if (numMyNetAddrs == 0)
393 (void)rxi_GetIFInfo();
396 ifn = rxi_FindIfnet(pp->host, NULL);
398 rx_rto_setPeerTimeoutSecs(pp, 2);
399 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
400 # ifdef IFF_POINTOPOINT
401 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
402 /* wish we knew the bit rate and the chunk size, sigh. */
403 rx_rto_setPeerTimeoutSecs(pp, 4);
404 pp->ifMTU = RX_PP_PACKET_SIZE;
406 # endif /* IFF_POINTOPOINT */
407 /* Diminish the packet size to one based on the MTU given by
409 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
410 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
411 if (rxmtu < pp->ifMTU)
414 } else { /* couldn't find the interface, so assume the worst */
415 rx_rto_setPeerTimeoutSecs(pp, 3);
416 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
418 # endif /* else AFS_USERSPACE_IP_ADDR */
419 # else /* AFS_SUN5_ENV */
422 mtu = rxi_FindIfMTU(pp->host);
425 rx_rto_setPeerTimeoutSecs(pp, 3);
426 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
428 rx_rto_setPeerTimeoutSecs(pp, 2);
429 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
431 /* Diminish the packet size to one based on the MTU given by
433 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
434 rxmtu = mtu - RX_IPUDP_SIZE;
435 if (rxmtu < pp->ifMTU)
439 # endif /* AFS_SUN5_ENV */
440 #else /* ADAPT_MTU */
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 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
761 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
762 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
763 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
768 if (match_value < 3) {
773 if (match_value < 2) {
778 } /* if net matches */
779 } /* for all in_ifaddrs */
783 *maskp = ifad->ia_subnetmask;
784 return (ifad ? ifad->ia_ifp : NULL);
786 #endif /* else DARWIN || XBSD */
787 #endif /* else AFS_USERSPACE_IP_ADDR */
788 #endif /* !SUN5 && !SGI62 */
791 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
792 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
793 * system specific subdirectories for all systems. But for the moment,
794 * most of it is simple to follow common code.
796 #if !defined(UKERNEL)
797 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
798 /* rxk_NewSocket creates a new socket on the specified port. The port is
799 * in network byte order.
802 rxk_NewSocketHost(afs_uint32 ahost, short aport)
805 #ifdef AFS_DARWIN80_ENV
808 struct socket *newSocket;
810 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
813 struct sockaddr_in myaddr;
814 #ifdef AFS_HPUX110_ENV
815 /* prototype copied from kernel source file streams/str_proto.h */
816 extern MBLKP allocb_wait(int, int);
818 int addrsize = sizeof(struct sockaddr_in);
820 extern struct fileops socketops;
826 AFS_STATCNT(osi_NewSocket);
827 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
828 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
832 #if defined(AFS_HPUX102_ENV)
833 #if defined(AFS_HPUX110_ENV)
834 /* we need a file associated with the socket so sosend in NetSend
836 /* blocking socket */
837 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
841 fp->f_flag = FREAD | FWRITE;
842 fp->f_type = DTYPE_SOCKET;
843 fp->f_ops = &socketops;
845 fp->f_data = (void *)newSocket;
846 newSocket->so_fp = (void *)fp;
848 #else /* AFS_HPUX110_ENV */
849 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
850 #endif /* else AFS_HPUX110_ENV */
851 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
852 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
853 #elif defined(AFS_FBSD_ENV)
854 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
855 afs_osi_credp, curthread);
856 #elif defined(AFS_DARWIN80_ENV)
857 #ifdef RXK_LISTENER_ENV
858 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
860 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, rx_upcall, NULL, &newSocket);
862 #elif defined(AFS_NBSD50_ENV)
863 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
864 #elif defined(AFS_NBSD40_ENV)
865 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
867 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
868 #endif /* AFS_HPUX102_ENV */
872 memset(&myaddr, 0, sizeof myaddr);
873 myaddr.sin_family = AF_INET;
874 myaddr.sin_port = aport;
875 myaddr.sin_addr.s_addr = ahost;
876 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
877 myaddr.sin_len = sizeof(myaddr);
880 #ifdef AFS_HPUX110_ENV
881 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
886 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
888 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
889 #if defined(AFS_NBSD40_ENV)
890 code = sobind(newSocket, bindnam, addrsize, osi_curproc());
892 code = sobind(newSocket, bindnam, addrsize);
896 #if !defined(AFS_HPUX1122_ENV)
903 #else /* AFS_HPUX110_ENV */
904 #if defined(AFS_DARWIN80_ENV)
909 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
910 &buflen, sizeof(buflen));
911 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
912 &buflen, sizeof(buflen));
916 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
921 #if defined(AFS_NBSD_ENV)
924 code = soreserve(newSocket, 50000, 50000);
926 code = soreserve(newSocket, 32766, 32766);
928 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
930 #if defined(AFS_NBSD_ENV)
934 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
935 #if defined(AFS_FBSD_ENV)
936 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
938 code = sobind(newSocket, (struct sockaddr *)&myaddr);
941 dpf(("sobind fails (%d)\n", (int)code));
945 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
947 nam = m_getclr(M_WAIT, MT_SONAME);
948 #else /* AFS_OSF_ENV */
949 nam = m_get(M_WAIT, MT_SONAME);
952 #if defined(KERNEL_HAVE_UERROR)
957 nam->m_len = sizeof(myaddr);
958 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
959 #if defined(AFS_SGI65_ENV)
960 BHV_PDATA(&bhv) = (void *)newSocket;
961 code = sobind(&bhv, nam);
963 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
964 code = sobind(newSocket, nam, osi_curproc());
966 code = sobind(newSocket, nam);
969 dpf(("sobind fails (%d)\n", (int)code));
971 #ifndef AFS_SGI65_ENV
976 #endif /* else AFS_DARWIN_ENV */
977 #endif /* else AFS_HPUX110_ENV */
980 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
981 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
983 return (osi_socket *)newSocket;
987 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
988 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
990 return (osi_socket *)0;
994 rxk_NewSocket(short aport)
996 return rxk_NewSocketHost(0, aport);
999 /* free socket allocated by rxk_NewSocket */
1001 rxk_FreeSocket(struct socket *asocket)
1003 AFS_STATCNT(osi_FreeSocket);
1004 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1005 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1007 #ifdef AFS_HPUX110_ENV
1008 if (asocket->so_fp) {
1009 struct file *fp = asocket->so_fp;
1010 #if !defined(AFS_HPUX1123_ENV)
1011 /* 11.23 still has falloc, but not FPENTRYFREE !
1012 * so for now if we shutdown, we will waist a file
1015 asocket->so_fp = NULL;
1018 #endif /* AFS_HPUX110_ENV */
1020 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1021 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1025 #endif /* !SUN5 && !LINUX20 */
1027 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV) || defined(RXK_UPCALL_ENV)
1028 #ifdef RXK_TIMEDSLEEP_ENV
1029 /* Shutting down should wake us up, as should an earlier event. */
1031 rxi_ReScheduleEvents(void)
1033 /* needed to allow startup */
1034 int glock = ISAFS_GLOCK();
1037 osi_rxWakeup(&afs_termState);
1043 * Run RX event daemon every second (5 times faster than rest of systems)
1046 afs_rxevent_daemon(void)
1052 #ifdef RX_ENABLE_LOCKS
1054 #endif /* RX_ENABLE_LOCKS */
1056 rxevent_RaiseEvents(&temp);
1058 #ifdef RX_ENABLE_LOCKS
1060 #endif /* RX_ENABLE_LOCKS */
1061 #ifdef RX_KERNEL_TRACE
1062 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1063 "before afs_osi_Wait()");
1065 #ifdef RXK_TIMEDSLEEP_ENV
1066 afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1067 (temp.usec / 1000))), 0);
1069 afs_osi_Wait(500, NULL, 0);
1071 #ifdef RX_KERNEL_TRACE
1072 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1073 "after afs_osi_Wait()");
1075 if (afs_termState == AFSOP_STOP_RXEVENT) {
1076 #ifdef RXK_LISTENER_ENV
1077 afs_termState = AFSOP_STOP_RXK_LISTENER;
1078 #elif defined(AFS_SUN510_ENV) || defined(RXK_UPCALL_ENV)
1079 afs_termState = AFSOP_STOP_NETIF;
1081 afs_termState = AFSOP_STOP_COMPLETE;
1083 osi_rxWakeup(&afs_termState);
1090 #ifdef RXK_LISTENER_ENV
1092 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1094 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1097 struct sockaddr_in from;
1101 afs_int32 savelen; /* was using rlen but had aliasing problems */
1102 rx_computelen(p, tlen);
1103 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1105 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1106 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1107 * it once in order to avoid races. */
1110 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1118 /* add some padding to the last iovec, it's just to make sure that the
1119 * read doesn't return more data than we expect, and is done to get around
1120 * our problems caused by the lack of a length field in the rx header. */
1121 savelen = p->wirevec[p->niovecs - 1].iov_len;
1122 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1124 nbytes = tlen + sizeof(afs_int32);
1125 #ifdef RX_KERNEL_TRACE
1126 if (ICL_SETACTIVE(afs_iclSetp)) {
1128 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1129 "before osi_NetRecive()");
1133 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1135 #ifdef RX_KERNEL_TRACE
1136 if (ICL_SETACTIVE(afs_iclSetp)) {
1138 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1139 "after osi_NetRecive()");
1143 /* restore the vec to its correct state */
1144 p->wirevec[p->niovecs - 1].iov_len = savelen;
1147 p->length = nbytes - RX_HEADER_SIZE;;
1148 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1150 if (rx_stats_active) {
1151 MUTEX_ENTER(&rx_stats_mutex);
1152 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1153 rx_stats.bogusHost = from.sin_addr.s_addr;
1154 MUTEX_EXIT(&rx_stats_mutex);
1156 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1157 from.sin_addr.s_addr, from.sin_port, nbytes));
1161 /* Extract packet header. */
1162 rxi_DecodePacketHeader(p);
1164 *host = from.sin_addr.s_addr;
1165 *port = from.sin_port;
1166 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1167 if (rx_stats_active) {
1168 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1172 #ifdef RX_TRIMDATABUFS
1173 /* Free any empty packet buffers at the end of this packet */
1174 rxi_TrimDataBufs(p, 1);
1184 * Listen for packets on socket. This thread is typically started after
1185 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1186 * the start state is set before proceeding.
1188 * Note that this thread is outside the AFS global lock for much of
1191 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1192 * we want here. So we need to either block all signals (including SIGKILL
1193 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1194 * OS's socket receive routine returns as a result of a signal.
1196 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1197 #ifdef AFS_LINUX20_ENV
1198 struct task_struct *rxk_ListenerTask;
1204 struct rx_packet *rxp = NULL;
1208 #ifdef AFS_LINUX20_ENV
1209 rxk_ListenerPid = current->pid;
1210 rxk_ListenerTask = current;
1213 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1214 #endif /* AFS_SUN5_ENV */
1216 rxk_ListenerPid = curproc->p_pid;
1217 #endif /* AFS_FBSD_ENV */
1218 #ifdef AFS_DARWIN80_ENV
1219 rxk_ListenerPid = proc_selfpid();
1220 #elif defined(AFS_DARWIN_ENV)
1221 rxk_ListenerPid = current_proc()->p_pid;
1223 #ifdef RX_ENABLE_LOCKS
1225 #endif /* RX_ENABLE_LOCKS */
1226 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1227 /* See if a check for additional packets was issued */
1231 rxi_RestoreDataBufs(rxp);
1233 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1235 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1237 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1238 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1242 #ifdef RX_ENABLE_LOCKS
1244 #endif /* RX_ENABLE_LOCKS */
1245 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1246 #ifdef AFS_SUN510_ENV
1247 afs_termState = AFSOP_STOP_NETIF;
1249 afs_termState = AFSOP_STOP_COMPLETE;
1251 osi_rxWakeup(&afs_termState);
1253 rxk_ListenerPid = 0;
1254 #ifdef AFS_LINUX20_ENV
1255 rxk_ListenerTask = 0;
1256 osi_rxWakeup(&rxk_ListenerTask);
1258 #if defined(AFS_SUN5_ENV) || defined(AFS_FBSD_ENV)
1259 osi_rxWakeup(&rxk_ListenerPid);
1263 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1264 /* The manner of stopping the rx listener thread may vary. Most unix's should
1265 * be able to call soclose.
1268 osi_StopListener(void)
1273 #endif /* RXK_LISTENER_ENV */
1274 #endif /* !NCR && !UKERNEL */
1276 #if !defined(AFS_LINUX26_ENV)
1278 #if defined(AFS_AIX_ENV)
1279 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1281 osi_Panic(char *msg, ...)
1286 msg = "Unknown AFS panic";
1288 * we should probably use the errsave facility here. it is not
1292 printf(msg, a1, a2, a3);
1294 #elif defined(AFS_SGI_ENV)
1297 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1299 cmn_err(CE_PANIC, "Unknown AFS panic");
1302 icmn_err(CE_PANIC, msg, ap);
1305 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1309 msg = "Unknown AFS panic";
1312 vsnprintf(buf, sizeof(buf), msg, ap);
1319 msg = "Unknown AFS panic";
1324 # ifdef AFS_LINUX20_ENV