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"
20 #include "rx_packet.h"
21 #include "rx_internal.h"
25 #ifdef AFS_HPUX110_ENV
31 #ifndef RXK_LISTENER_ENV
32 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
33 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
36 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
37 extern struct interfaceAddr afs_cb_interface;
39 rxk_ports_t rxk_ports;
40 rxk_portRocks_t rxk_portRocks;
44 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
45 #define ADDRSPERSITE 16
46 static afs_uint32 myNetAddrs[ADDRSPERSITE];
47 static int myNetMTUs[ADDRSPERSITE];
48 static int numMyNetAddrs = 0;
51 #if defined(AFS_DARWIN80_ENV)
52 #define sobind sock_bind
53 #define soclose sock_close
56 /* add a port to the monitored list, port # is in network order */
58 rxk_AddPort(u_short aport, char *arock)
61 unsigned short *tsp, ts;
64 zslot = -1; /* look for an empty slot simultaneously */
65 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
66 if (((ts = *tsp) == 0) && (zslot == -1))
72 /* otherwise allocate a new port slot */
74 return E2BIG; /* all full */
75 rxk_ports[zslot] = aport;
76 rxk_portRocks[zslot] = arock;
80 /* remove as port from the monitored list, port # is in network order */
82 rxk_DelPort(u_short aport)
87 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
89 /* found it, adjust ref count and free the port reference if all gone */
94 /* otherwise port not found */
99 rxk_shutdownPorts(void)
102 for (i = 0; i < MAXRXPORTS; i++) {
105 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
106 soclose((struct socket *)rxk_portRocks[i]);
108 rxk_portRocks[i] = NULL;
114 rxi_GetHostUDPSocket(u_int host, u_short port)
117 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
118 if (sockp == (osi_socket *)0)
119 return OSI_NULLSOCKET;
120 rxk_AddPort(port, (char *)sockp);
121 return (osi_socket) sockp;
125 rxi_GetUDPSocket(u_short port)
127 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
131 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
132 * unsigned long value into the given buffer. Returns 0 on success,
133 * and a value less than 0 on failure. The contents of the buffer is
134 * defined only on success.
138 osi_utoa(char *buf, size_t len, unsigned long val)
140 long k; /* index of first byte of string value */
142 /* we definitely need room for at least one digit and NUL */
148 /* compute the string form from the high end of the buffer */
151 for (k = len - 2; k >= 0; k--) {
152 buf[k] = val % 10 + '0';
159 /* did we finish converting val to string form? */
165 /* this should never happen */
171 /* this should never happen */
177 /* if necessary, relocate string to beginning of buf[] */
182 * We need to achieve the effect of calling
184 * memmove(buf, &buf[k], len - k);
186 * However, since memmove() is not available in all
187 * kernels, we explicitly do an appropriate copy.
193 while ((*dst++ = *src++) != '\0')
200 #ifndef AFS_LINUX26_ENV
202 * osi_AssertFailK() -- used by the osi_Assert() macro.
204 * It essentially does
206 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
208 * Since the kernel version of osi_Panic() only passes its first
209 * argument to the native panic(), we construct a single string and hand
210 * that to osi_Panic().
213 osi_AssertFailK(const char *expr, const char *file, int line)
215 static const char msg0[] = "assertion failed: ";
216 static const char msg1[] = ", file: ";
217 static const char msg2[] = ", line: ";
218 static const char msg3[] = "\n";
221 * These buffers add up to 1K, which is a pleasantly nice round
222 * value, but probably not vital.
227 /* check line number conversion */
229 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
230 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
235 #define ADDBUF(BUF, STR) \
236 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
237 strcat(BUF, (char *)(STR)); \
246 ADDBUF(buf, linebuf);
251 osi_Panic("%s", buf);
256 /* This is the server process request loop. Kernel server
257 * processes never become listener threads */
259 rx_ServerProc(void *unused)
263 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
264 MUTEX_ENTER(&rx_quota_mutex);
265 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
266 /* threadID is used for making decisions in GetCall. Get it by bumping
267 * number of threads handling incoming calls */
268 threadID = rxi_availProcs++;
269 MUTEX_EXIT(&rx_quota_mutex);
271 #ifdef RX_ENABLE_LOCKS
273 #endif /* RX_ENABLE_LOCKS */
274 rxi_ServerProc(threadID, NULL, NULL);
275 #ifdef RX_ENABLE_LOCKS
277 #endif /* RX_ENABLE_LOCKS */
281 #endif /* !UKERNEL */
283 #ifndef RXK_LISTENER_ENV
284 /* asize includes the Rx header */
286 MyPacketProc(struct rx_packet **ahandle, int asize)
288 struct rx_packet *tp;
290 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
291 /* If we can't scrounge enough cbufs, then we have to drop the packet,
292 * but we should set a flag so we magic up some more at our leisure.
295 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
296 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
297 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
299 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
300 RX_PACKET_CLASS_RECV_CBUF)) {
303 if (rx_stats_active) {
304 rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
310 * XXX if packet is too long for our buffer,
311 * should do this at a higher layer and let other
312 * end know we're losing.
314 if (rx_stats_active) {
315 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
317 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
318 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
324 /* otherwise we have a packet, set appropriate values */
330 MyArrivalProc(struct rx_packet *ahandle,
331 struct sockaddr_in *afrom,
332 struct socket *arock,
335 /* handle basic rx packet */
336 ahandle->length = asize - RX_HEADER_SIZE;
337 rxi_DecodePacketHeader(ahandle);
339 rxi_ReceivePacket(ahandle, arock,
340 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
343 /* free the packet if it has been returned */
345 rxi_FreePacket(ahandle);
348 #endif /* !RXK_LISTENER_ENV */
351 rxi_StartListener(void)
353 #if !defined(RXK_LISTENER_ENV) && !defined(RXK_UPCALL_ENV)
354 /* if kernel, give name of appropriate procedures */
355 rxk_GetPacketProc = MyPacketProc;
356 rxk_PacketArrivalProc = MyArrivalProc;
361 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
362 to get interesting information. */
364 rxi_InitPeerParams(struct rx_peer *pp)
369 # ifndef AFS_SUN5_ENV
370 # ifdef AFS_USERSPACE_IP_ADDR
374 i = rxi_Findcbi(pp->host);
376 rx_rto_setPeerTimeoutSecs(pp, 3);
377 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
379 rx_rto_setPeerTimeoutSecs(pp, 2);
380 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
381 mtu = ntohl(afs_cb_interface.mtu[i]);
382 /* Diminish the packet size to one based on the MTU given by
384 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
385 rxmtu = mtu - RX_IPUDP_SIZE;
386 if (rxmtu < pp->ifMTU)
390 # else /* AFS_USERSPACE_IP_ADDR */
393 # if !defined(AFS_SGI62_ENV)
394 if (numMyNetAddrs == 0)
395 (void)rxi_GetIFInfo();
398 ifn = rxi_FindIfnet(pp->host, NULL);
400 rx_rto_setPeerTimeoutSecs(pp, 2);
401 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
402 # ifdef IFF_POINTOPOINT
403 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
404 /* wish we knew the bit rate and the chunk size, sigh. */
405 rx_rto_setPeerTimeoutSecs(pp, 4);
406 pp->ifMTU = RX_PP_PACKET_SIZE;
408 # endif /* IFF_POINTOPOINT */
409 /* Diminish the packet size to one based on the MTU given by
411 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
412 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
413 if (rxmtu < pp->ifMTU)
416 } else { /* couldn't find the interface, so assume the worst */
417 rx_rto_setPeerTimeoutSecs(pp, 3);
418 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
420 # endif /* else AFS_USERSPACE_IP_ADDR */
421 # else /* AFS_SUN5_ENV */
424 mtu = rxi_FindIfMTU(pp->host);
427 rx_rto_setPeerTimeoutSecs(pp, 3);
428 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
430 rx_rto_setPeerTimeoutSecs(pp, 2);
431 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
433 /* Diminish the packet size to one based on the MTU given by
435 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
436 rxmtu = mtu - RX_IPUDP_SIZE;
437 if (rxmtu < pp->ifMTU)
441 # endif /* AFS_SUN5_ENV */
442 #else /* ADAPT_MTU */
443 rx_rto_setPeerTimeoutSecs(pp, 2);
444 pp->ifMTU = OLD_MAX_PACKET_SIZE;
445 #endif /* else ADAPT_MTU */
446 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
447 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
448 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
450 MIN(rxi_nDgramPackets,
451 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
452 pp->maxDgramPackets = 1;
454 /* Initialize slow start parameters */
455 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
457 pp->nDgramPackets = 1;
462 /* The following code is common to several system types, but not all. The
463 * separate ones are found in the system specific subdirectories.
467 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
468 /* Routine called during the afsd "-shutdown" process to put things back to
471 static struct protosw parent_proto; /* udp proto switch */
474 shutdown_rxkernel(void)
476 struct protosw *tpro, *last;
477 last = inetdomain.dom_protoswNPROTOSW;
478 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
479 if (tpro->pr_protocol == IPPROTO_UDP) {
480 /* restore original udp protocol switch */
481 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
482 memset((void *)&parent_proto, 0, sizeof(parent_proto));
487 dpf(("shutdown_rxkernel: no udp proto\n"));
489 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
491 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
492 /* Determine what the network interfaces are for this machine. */
494 #ifdef AFS_USERSPACE_IP_ADDR
498 int i, j, different = 0, num = ADDRSPERSITE;
501 afs_uint32 addrs[ADDRSPERSITE];
502 int mtus[ADDRSPERSITE];
504 memset((void *)addrs, 0, sizeof(addrs));
505 memset((void *)mtus, 0, sizeof(mtus));
507 if (afs_cb_interface.numberOfInterfaces < num)
508 num = afs_cb_interface.numberOfInterfaces;
509 for (i = 0; i < num; i++) {
510 if (!afs_cb_interface.mtu[i])
511 afs_cb_interface.mtu[i] = htonl(1500);
512 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
513 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
514 if (myNetAddrs[i] != ifinaddr)
518 rxmtu = rxi_AdjustIfMTU(rxmtu);
520 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
521 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
522 addrs[i++] = ifinaddr;
523 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
524 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
525 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
529 rx_maxJumboRecvSize =
530 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
531 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
532 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
535 for (j = 0; j < i; j++) {
536 myNetMTUs[j] = mtus[j];
537 myNetAddrs[j] = addrs[j];
544 /* Returns the afs_cb_interface inxex which best matches address.
545 * If none is found, we return -1.
548 rxi_Findcbi(afs_uint32 addr)
551 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
552 afs_int32 rvalue = -1;
555 if (numMyNetAddrs == 0)
556 (void)rxi_GetcbiInfo();
558 myAddr = ntohl(addr);
560 if (IN_CLASSA(myAddr))
561 netMask = IN_CLASSA_NET;
562 else if (IN_CLASSB(myAddr))
563 netMask = IN_CLASSB_NET;
564 else if (IN_CLASSC(myAddr))
565 netMask = IN_CLASSC_NET;
569 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
570 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
571 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
572 if ((myAddr & netMask) == (thisAddr & netMask)) {
573 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
574 if (myAddr == thisAddr) {
579 if (match_value < 3) {
584 if (match_value < 2) {
595 #else /* AFS_USERSPACE_IP_ADDR */
597 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
598 #define IFADDR2SA(f) (&((f)->ifa_addr))
599 #else /* AFS_AIX41_ENV */
600 #define IFADDR2SA(f) ((f)->ifa_addr)
610 afs_uint32 addrs[ADDRSPERSITE];
611 int mtus[ADDRSPERSITE];
613 #if defined(AFS_DARWIN80_ENV)
619 struct sockaddr sout;
620 struct sockaddr_in *sin;
623 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
627 memset(addrs, 0, sizeof(addrs));
628 memset(mtus, 0, sizeof(mtus));
630 #if defined(AFS_DARWIN80_ENV)
631 if (!ifnet_list_get(AF_INET, &ifns, &count)) {
632 for (m = 0; m < count; m++) {
633 if (!ifnet_get_address_list(ifns[m], &ifads)) {
634 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
635 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
636 sin = (struct sockaddr_in *)&sout;
637 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
638 ifinaddr = ntohl(sin->sin_addr.s_addr);
639 if (myNetAddrs[i] != ifinaddr) {
643 rxmtu = rxi_AdjustIfMTU(rxmtu);
645 rxmtu * rxi_nRecvFrags +
646 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
647 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
648 addrs[i++] = ifinaddr;
649 if (!rx_IsLoopbackAddr(ifinaddr) &&
650 (maxmtu > rx_maxReceiveSize)) {
652 MIN(RX_MAX_PACKET_SIZE, maxmtu);
654 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
659 ifnet_free_address_list(ifads);
662 ifnet_list_free(ifns);
665 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
666 #if defined(AFS_FBSD80_ENV)
667 TAILQ_FOREACH(ifn, &V_ifnet, if_link) {
669 TAILQ_FOREACH(ifn, &ifnet, if_link) {
671 if (i >= ADDRSPERSITE)
673 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
674 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
675 ifn = ifn->if_list.tqe_next) {
677 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
679 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
680 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
681 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
682 if (i >= ADDRSPERSITE)
684 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
685 for (ifad = ifn->if_addrlist.tqh_first;
686 ifad != NULL && i < ADDRSPERSITE;
687 ifad = ifad->ifa_list.tqe_next) {
689 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
690 ifad = ifad->ifa_next) {
692 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
694 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
696 if (myNetAddrs[i] != ifinaddr) {
700 rxmtu = rxi_AdjustIfMTU(rxmtu);
702 rxmtu * rxi_nRecvFrags +
703 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
704 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
705 addrs[i++] = ifinaddr;
706 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
707 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
709 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
716 rx_maxJumboRecvSize =
717 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
718 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
719 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
723 for (l = 0; l < i; l++) {
724 myNetMTUs[l] = mtus[l];
725 myNetAddrs[l] = addrs[l];
731 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
732 /* Returns ifnet which best matches address */
734 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
736 struct sockaddr_in s, sr;
739 s.sin_family = AF_INET;
740 s.sin_addr.s_addr = addr;
741 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
744 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
745 *maskp = sr.sin_addr.s_addr;
747 return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
750 #else /* DARWIN || XBSD */
752 /* Returns ifnet which best matches address */
754 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
757 extern struct in_ifaddr *in_ifaddr;
758 struct in_ifaddr *ifa, *ifad = NULL;
762 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
763 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
764 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
765 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
770 if (match_value < 3) {
775 if (match_value < 2) {
780 } /* if net matches */
781 } /* for all in_ifaddrs */
785 *maskp = ifad->ia_subnetmask;
786 return (ifad ? ifad->ia_ifp : NULL);
788 #endif /* else DARWIN || XBSD */
789 #endif /* else AFS_USERSPACE_IP_ADDR */
790 #endif /* !SUN5 && !SGI62 */
793 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
794 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
795 * system specific subdirectories for all systems. But for the moment,
796 * most of it is simple to follow common code.
798 #if !defined(UKERNEL)
799 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
800 /* rxk_NewSocket creates a new socket on the specified port. The port is
801 * in network byte order.
804 rxk_NewSocketHost(afs_uint32 ahost, short aport)
807 #ifdef AFS_DARWIN80_ENV
810 struct socket *newSocket;
812 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
815 struct sockaddr_in myaddr;
816 #ifdef AFS_HPUX110_ENV
817 /* prototype copied from kernel source file streams/str_proto.h */
818 extern MBLKP allocb_wait(int, int);
820 int addrsize = sizeof(struct sockaddr_in);
822 extern struct fileops socketops;
828 AFS_STATCNT(osi_NewSocket);
829 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
830 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
834 #if defined(AFS_HPUX102_ENV)
835 #if defined(AFS_HPUX110_ENV)
836 /* we need a file associated with the socket so sosend in NetSend
838 /* blocking socket */
839 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
843 fp->f_flag = FREAD | FWRITE;
844 fp->f_type = DTYPE_SOCKET;
845 fp->f_ops = &socketops;
847 fp->f_data = (void *)newSocket;
848 newSocket->so_fp = (void *)fp;
850 #else /* AFS_HPUX110_ENV */
851 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
852 #endif /* else AFS_HPUX110_ENV */
853 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
854 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
855 #elif defined(AFS_FBSD_ENV)
856 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
857 afs_osi_credp, curthread);
858 #elif defined(AFS_DARWIN80_ENV)
859 #ifdef RXK_LISTENER_ENV
860 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
862 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, rx_upcall, NULL, &newSocket);
864 #elif defined(AFS_NBSD50_ENV)
865 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
866 #elif defined(AFS_NBSD40_ENV)
867 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
869 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
870 #endif /* AFS_HPUX102_ENV */
874 memset(&myaddr, 0, sizeof myaddr);
875 myaddr.sin_family = AF_INET;
876 myaddr.sin_port = aport;
877 myaddr.sin_addr.s_addr = ahost;
878 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
879 myaddr.sin_len = sizeof(myaddr);
882 #ifdef AFS_HPUX110_ENV
883 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
888 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
890 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
891 #if defined(AFS_NBSD40_ENV)
892 code = sobind(newSocket, bindnam, addrsize, osi_curproc());
894 code = sobind(newSocket, bindnam, addrsize);
898 #if !defined(AFS_HPUX1122_ENV)
905 #else /* AFS_HPUX110_ENV */
906 #if defined(AFS_DARWIN80_ENV)
911 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
912 &buflen, sizeof(buflen));
913 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
914 &buflen, sizeof(buflen));
918 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
923 #if defined(AFS_NBSD_ENV)
926 code = soreserve(newSocket, 50000, 50000);
928 code = soreserve(newSocket, 32766, 32766);
930 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
932 #if defined(AFS_NBSD_ENV)
936 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
937 #if defined(AFS_FBSD_ENV)
938 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
940 code = sobind(newSocket, (struct sockaddr *)&myaddr);
943 dpf(("sobind fails (%d)\n", (int)code));
947 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
949 nam = m_getclr(M_WAIT, MT_SONAME);
950 #else /* AFS_OSF_ENV */
951 nam = m_get(M_WAIT, MT_SONAME);
954 #if defined(KERNEL_HAVE_UERROR)
959 nam->m_len = sizeof(myaddr);
960 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
961 #if defined(AFS_SGI65_ENV)
962 BHV_PDATA(&bhv) = (void *)newSocket;
963 code = sobind(&bhv, nam);
965 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
966 code = sobind(newSocket, nam, osi_curproc());
968 code = sobind(newSocket, nam);
971 dpf(("sobind fails (%d)\n", (int)code));
973 #ifndef AFS_SGI65_ENV
978 #endif /* else AFS_DARWIN_ENV */
979 #endif /* else AFS_HPUX110_ENV */
982 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
983 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
985 return (osi_socket *)newSocket;
989 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
990 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
992 return (osi_socket *)0;
996 rxk_NewSocket(short aport)
998 return rxk_NewSocketHost(0, aport);
1001 /* free socket allocated by rxk_NewSocket */
1003 rxk_FreeSocket(struct socket *asocket)
1005 AFS_STATCNT(osi_FreeSocket);
1006 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1007 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1009 #ifdef AFS_HPUX110_ENV
1010 if (asocket->so_fp) {
1011 struct file *fp = asocket->so_fp;
1012 #if !defined(AFS_HPUX1123_ENV)
1013 /* 11.23 still has falloc, but not FPENTRYFREE !
1014 * so for now if we shutdown, we will waist a file
1017 asocket->so_fp = NULL;
1020 #endif /* AFS_HPUX110_ENV */
1022 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1023 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1027 #endif /* !SUN5 && !LINUX20 */
1029 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV) || defined(RXK_UPCALL_ENV)
1030 #ifdef RXK_TIMEDSLEEP_ENV
1031 /* Shutting down should wake us up, as should an earlier event. */
1033 rxi_ReScheduleEvents(void)
1035 /* needed to allow startup */
1036 int glock = ISAFS_GLOCK();
1039 osi_rxWakeup(&afs_termState);
1045 * Run RX event daemon every second (5 times faster than rest of systems)
1048 afs_rxevent_daemon(void)
1054 #ifdef RX_ENABLE_LOCKS
1056 #endif /* RX_ENABLE_LOCKS */
1058 rxevent_RaiseEvents(&temp);
1060 #ifdef RX_ENABLE_LOCKS
1062 #endif /* RX_ENABLE_LOCKS */
1063 #ifdef RX_KERNEL_TRACE
1064 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1065 "before afs_osi_Wait()");
1067 #ifdef RXK_TIMEDSLEEP_ENV
1068 afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1069 (temp.usec / 1000))), 0);
1071 afs_osi_Wait(500, NULL, 0);
1073 #ifdef RX_KERNEL_TRACE
1074 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1075 "after afs_osi_Wait()");
1077 if (afs_termState == AFSOP_STOP_RXEVENT) {
1078 #ifdef RXK_LISTENER_ENV
1079 afs_termState = AFSOP_STOP_RXK_LISTENER;
1080 #elif defined(AFS_SUN510_ENV) || defined(RXK_UPCALL_ENV)
1081 afs_termState = AFSOP_STOP_NETIF;
1083 afs_termState = AFSOP_STOP_COMPLETE;
1085 osi_rxWakeup(&afs_termState);
1092 #ifdef RXK_LISTENER_ENV
1094 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1096 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1099 struct sockaddr_in from;
1103 afs_int32 savelen; /* was using rlen but had aliasing problems */
1104 rx_computelen(p, tlen);
1105 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1107 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1108 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1109 * it once in order to avoid races. */
1112 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1120 /* add some padding to the last iovec, it's just to make sure that the
1121 * read doesn't return more data than we expect, and is done to get around
1122 * our problems caused by the lack of a length field in the rx header. */
1123 savelen = p->wirevec[p->niovecs - 1].iov_len;
1124 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1126 nbytes = tlen + sizeof(afs_int32);
1127 #ifdef RX_KERNEL_TRACE
1128 if (ICL_SETACTIVE(afs_iclSetp)) {
1130 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1131 "before osi_NetRecive()");
1135 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1137 #ifdef RX_KERNEL_TRACE
1138 if (ICL_SETACTIVE(afs_iclSetp)) {
1140 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1141 "after osi_NetRecive()");
1145 /* restore the vec to its correct state */
1146 p->wirevec[p->niovecs - 1].iov_len = savelen;
1149 p->length = nbytes - RX_HEADER_SIZE;;
1150 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1152 if (rx_stats_active) {
1153 MUTEX_ENTER(&rx_stats_mutex);
1154 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1155 rx_stats.bogusHost = from.sin_addr.s_addr;
1156 MUTEX_EXIT(&rx_stats_mutex);
1158 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1159 from.sin_addr.s_addr, from.sin_port, nbytes));
1163 /* Extract packet header. */
1164 rxi_DecodePacketHeader(p);
1166 *host = from.sin_addr.s_addr;
1167 *port = from.sin_port;
1168 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1169 if (rx_stats_active) {
1170 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1174 #ifdef RX_TRIMDATABUFS
1175 /* Free any empty packet buffers at the end of this packet */
1176 rxi_TrimDataBufs(p, 1);
1186 * Listen for packets on socket. This thread is typically started after
1187 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1188 * the start state is set before proceeding.
1190 * Note that this thread is outside the AFS global lock for much of
1193 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1194 * we want here. So we need to either block all signals (including SIGKILL
1195 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1196 * OS's socket receive routine returns as a result of a signal.
1198 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1199 #ifdef AFS_LINUX20_ENV
1200 struct task_struct *rxk_ListenerTask;
1206 struct rx_packet *rxp = NULL;
1210 #ifdef AFS_LINUX20_ENV
1211 rxk_ListenerPid = current->pid;
1212 rxk_ListenerTask = current;
1215 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1216 #endif /* AFS_SUN5_ENV */
1218 rxk_ListenerPid = curproc->p_pid;
1219 #endif /* AFS_FBSD_ENV */
1220 #ifdef AFS_DARWIN80_ENV
1221 rxk_ListenerPid = proc_selfpid();
1222 #elif defined(AFS_DARWIN_ENV)
1223 rxk_ListenerPid = current_proc()->p_pid;
1225 #ifdef RX_ENABLE_LOCKS
1227 #endif /* RX_ENABLE_LOCKS */
1228 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1229 /* See if a check for additional packets was issued */
1233 rxi_RestoreDataBufs(rxp);
1235 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1237 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1239 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1240 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1244 #ifdef RX_ENABLE_LOCKS
1246 #endif /* RX_ENABLE_LOCKS */
1247 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1248 #ifdef AFS_SUN510_ENV
1249 afs_termState = AFSOP_STOP_NETIF;
1251 afs_termState = AFSOP_STOP_COMPLETE;
1253 osi_rxWakeup(&afs_termState);
1255 rxk_ListenerPid = 0;
1256 #ifdef AFS_LINUX20_ENV
1257 rxk_ListenerTask = 0;
1258 osi_rxWakeup(&rxk_ListenerTask);
1260 #if defined(AFS_SUN5_ENV) || defined(AFS_FBSD_ENV)
1261 osi_rxWakeup(&rxk_ListenerPid);
1265 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1266 /* The manner of stopping the rx listener thread may vary. Most unix's should
1267 * be able to call soclose.
1270 osi_StopListener(void)
1275 #endif /* RXK_LISTENER_ENV */
1276 #endif /* !NCR && !UKERNEL */
1278 #if !defined(AFS_LINUX26_ENV)
1280 #if defined(AFS_AIX_ENV)
1281 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1283 osi_Panic(char *msg, ...)
1288 msg = "Unknown AFS panic";
1290 * we should probably use the errsave facility here. it is not
1294 printf(msg, a1, a2, a3);
1296 #elif defined(AFS_SGI_ENV)
1299 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1301 cmn_err(CE_PANIC, "Unknown AFS panic");
1304 icmn_err(CE_PANIC, msg, ap);
1307 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1311 msg = "Unknown AFS panic";
1314 vsnprintf(buf, sizeof(buf), msg, ap);
1321 msg = "Unknown AFS panic";
1326 # ifdef AFS_LINUX20_ENV