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"
20 #ifdef AFS_HPUX110_ENV
26 #ifndef RXK_LISTENER_ENV
27 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
28 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
31 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
32 extern struct interfaceAddr afs_cb_interface;
34 rxk_ports_t rxk_ports;
35 rxk_portRocks_t rxk_portRocks;
39 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
40 #define ADDRSPERSITE 16
41 static afs_uint32 myNetAddrs[ADDRSPERSITE];
42 static int myNetMTUs[ADDRSPERSITE];
43 static int numMyNetAddrs = 0;
46 #if defined(AFS_DARWIN80_ENV)
47 #define sobind sock_bind
48 #define soclose sock_close
51 /* add a port to the monitored list, port # is in network order */
53 rxk_AddPort(u_short aport, char *arock)
56 unsigned short *tsp, ts;
59 zslot = -1; /* look for an empty slot simultaneously */
60 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
61 if (((ts = *tsp) == 0) && (zslot == -1))
67 /* otherwise allocate a new port slot */
69 return E2BIG; /* all full */
70 rxk_ports[zslot] = aport;
71 rxk_portRocks[zslot] = arock;
75 /* remove as port from the monitored list, port # is in network order */
77 rxk_DelPort(u_short aport)
82 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
84 /* found it, adjust ref count and free the port reference if all gone */
89 /* otherwise port not found */
94 rxk_shutdownPorts(void)
97 for (i = 0; i < MAXRXPORTS; i++) {
100 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
101 soclose((struct socket *)rxk_portRocks[i]);
103 rxk_portRocks[i] = NULL;
109 rxi_GetHostUDPSocket(u_int host, u_short port)
112 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
113 if (sockp == (osi_socket *)0)
114 return OSI_NULLSOCKET;
115 rxk_AddPort(port, (char *)sockp);
116 return (osi_socket) sockp;
120 rxi_GetUDPSocket(u_short port)
122 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
126 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
127 * unsigned long value into the given buffer. Returns 0 on success,
128 * and a value less than 0 on failure. The contents of the buffer is
129 * defined only on success.
133 osi_utoa(char *buf, size_t len, unsigned long val)
135 long k; /* index of first byte of string value */
137 /* we definitely need room for at least one digit and NUL */
143 /* compute the string form from the high end of the buffer */
146 for (k = len - 2; k >= 0; k--) {
147 buf[k] = val % 10 + '0';
154 /* did we finish converting val to string form? */
160 /* this should never happen */
166 /* this should never happen */
172 /* if necessary, relocate string to beginning of buf[] */
177 * We need to achieve the effect of calling
179 * memmove(buf, &buf[k], len - k);
181 * However, since memmove() is not available in all
182 * kernels, we explicitly do an appropriate copy.
188 while ((*dst++ = *src++) != '\0')
195 #ifndef AFS_LINUX26_ENV
197 * osi_AssertFailK() -- used by the osi_Assert() macro.
199 * It essentially does
201 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
203 * Since the kernel version of osi_Panic() only passes its first
204 * argument to the native panic(), we construct a single string and hand
205 * that to osi_Panic().
208 osi_AssertFailK(const char *expr, const char *file, int line)
210 static const char msg0[] = "assertion failed: ";
211 static const char msg1[] = ", file: ";
212 static const char msg2[] = ", line: ";
213 static const char msg3[] = "\n";
216 * These buffers add up to 1K, which is a pleasantly nice round
217 * value, but probably not vital.
222 /* check line number conversion */
224 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
225 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
230 #define ADDBUF(BUF, STR) \
231 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
232 strcat(BUF, (char *)(STR)); \
241 ADDBUF(buf, linebuf);
246 osi_Panic("%s", buf);
250 osi_AssertFailK(const char *expr, const char *file, int line)
252 printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
257 /* This is the server process request loop. Kernel server
258 * processes never become listener threads */
260 rx_ServerProc(void *unused)
264 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
265 MUTEX_ENTER(&rx_quota_mutex);
266 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
267 /* threadID is used for making decisions in GetCall. Get it by bumping
268 * number of threads handling incoming calls */
269 threadID = rxi_availProcs++;
270 MUTEX_EXIT(&rx_quota_mutex);
272 #ifdef RX_ENABLE_LOCKS
274 #endif /* RX_ENABLE_LOCKS */
275 rxi_ServerProc(threadID, NULL, NULL);
276 #ifdef RX_ENABLE_LOCKS
278 #endif /* RX_ENABLE_LOCKS */
282 #endif /* !UKERNEL */
284 #ifndef RXK_LISTENER_ENV
285 /* asize includes the Rx header */
287 MyPacketProc(struct rx_packet **ahandle, int asize)
289 struct rx_packet *tp;
291 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
292 /* If we can't scrounge enough cbufs, then we have to drop the packet,
293 * but we should set a flag so we magic up some more at our leisure.
296 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
297 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
298 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
300 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
301 RX_PACKET_CLASS_RECV_CBUF)) {
304 if (rx_stats_active) {
305 MUTEX_ENTER(&rx_stats_mutex);
306 rx_stats.noPacketBuffersOnRead++;
307 MUTEX_EXIT(&rx_stats_mutex);
313 * XXX if packet is too long for our buffer,
314 * should do this at a higher layer and let other
315 * end know we're losing.
317 if (rx_stats_active) {
318 MUTEX_ENTER(&rx_stats_mutex);
319 rx_stats.bogusPacketOnRead++;
320 MUTEX_EXIT(&rx_stats_mutex);
322 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
323 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
329 /* otherwise we have a packet, set appropriate values */
335 MyArrivalProc(struct rx_packet *ahandle,
336 struct sockaddr_in *afrom,
337 struct socket *arock,
340 /* handle basic rx packet */
341 ahandle->length = asize - RX_HEADER_SIZE;
342 rxi_DecodePacketHeader(ahandle);
344 rxi_ReceivePacket(ahandle, arock,
345 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
348 /* free the packet if it has been returned */
350 rxi_FreePacket(ahandle);
353 #endif /* !RXK_LISTENER_ENV */
356 rxi_StartListener(void)
358 /* if kernel, give name of appropriate procedures */
359 #ifndef RXK_LISTENER_ENV
360 rxk_GetPacketProc = MyPacketProc;
361 rxk_PacketArrivalProc = MyArrivalProc;
366 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
367 to get interesting information. */
369 rxi_InitPeerParams(struct rx_peer *pp)
374 # ifndef AFS_SUN5_ENV
375 # ifdef AFS_USERSPACE_IP_ADDR
379 i = rxi_Findcbi(pp->host);
382 /* pp->timeout.usec = 0; */
383 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
386 /* pp->timeout.usec = 0; */
387 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
390 mtu = ntohl(afs_cb_interface.mtu[i]);
391 /* Diminish the packet size to one based on the MTU given by
393 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
394 rxmtu = mtu - RX_IPUDP_SIZE;
395 if (rxmtu < pp->ifMTU)
398 } else { /* couldn't find the interface, so assume the worst */
399 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
401 # else /* AFS_USERSPACE_IP_ADDR */
404 # if !defined(AFS_SGI62_ENV)
405 if (numMyNetAddrs == 0)
406 (void)rxi_GetIFInfo();
409 ifn = rxi_FindIfnet(pp->host, NULL);
412 /* pp->timeout.usec = 0; */
413 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
414 # ifdef IFF_POINTOPOINT
415 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
416 /* wish we knew the bit rate and the chunk size, sigh. */
418 pp->ifMTU = RX_PP_PACKET_SIZE;
420 # endif /* IFF_POINTOPOINT */
421 /* Diminish the packet size to one based on the MTU given by
423 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
424 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
425 if (rxmtu < pp->ifMTU)
428 } else { /* couldn't find the interface, so assume the worst */
430 /* pp->timeout.usec = 0; */
431 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
433 # endif /* else AFS_USERSPACE_IP_ADDR */
434 # else /* AFS_SUN5_ENV */
437 mtu = rxi_FindIfMTU(pp->host);
441 /* pp->timeout.usec = 0; */
442 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
445 /* pp->timeout.usec = 0; */
446 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
450 /* Diminish the packet size to one based on the MTU given by
452 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
453 rxmtu = mtu - RX_IPUDP_SIZE;
454 if (rxmtu < pp->ifMTU)
457 } else { /* couldn't find the interface, so assume the worst */
458 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE,rx_MyMaxSendSize);
460 # endif /* AFS_SUN5_ENV */
461 #else /* ADAPT_MTU */
462 pp->rateFlag = 2; /* start timing after two full packets */
464 pp->ifMTU = OLD_MAX_PACKET_SIZE;
465 #endif /* else ADAPT_MTU */
466 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
467 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
468 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
470 MIN(rxi_nDgramPackets,
471 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
472 pp->maxDgramPackets = 1;
474 /* Initialize slow start parameters */
475 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
477 pp->nDgramPackets = 1;
482 /* The following code is common to several system types, but not all. The
483 * separate ones are found in the system specific subdirectories.
487 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
488 /* Routine called during the afsd "-shutdown" process to put things back to
491 static struct protosw parent_proto; /* udp proto switch */
494 shutdown_rxkernel(void)
496 struct protosw *tpro, *last;
497 last = inetdomain.dom_protoswNPROTOSW;
498 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
499 if (tpro->pr_protocol == IPPROTO_UDP) {
500 /* restore original udp protocol switch */
501 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
502 memset((void *)&parent_proto, 0, sizeof(parent_proto));
507 dpf(("shutdown_rxkernel: no udp proto"));
509 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
511 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
512 /* Determine what the network interfaces are for this machine. */
514 #ifdef AFS_USERSPACE_IP_ADDR
518 int i, j, different = 0, num = ADDRSPERSITE;
521 afs_uint32 addrs[ADDRSPERSITE];
522 int mtus[ADDRSPERSITE];
524 memset((void *)addrs, 0, sizeof(addrs));
525 memset((void *)mtus, 0, sizeof(mtus));
527 if (afs_cb_interface.numberOfInterfaces < num)
528 num = afs_cb_interface.numberOfInterfaces;
529 for (i = 0; i < num; i++) {
530 if (!afs_cb_interface.mtu[i])
531 afs_cb_interface.mtu[i] = htonl(1500);
532 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
533 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
534 if (myNetAddrs[i] != ifinaddr)
538 rxmtu = rxi_AdjustIfMTU(rxmtu);
540 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
541 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
542 addrs[i++] = ifinaddr;
543 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
544 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
545 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
549 rx_maxJumboRecvSize =
550 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
551 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
552 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
555 for (j = 0; j < i; j++) {
556 myNetMTUs[j] = mtus[j];
557 myNetAddrs[j] = addrs[j];
564 /* Returns the afs_cb_interface inxex which best matches address.
565 * If none is found, we return -1.
568 rxi_Findcbi(afs_uint32 addr)
571 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
572 afs_int32 rvalue = -1;
575 if (numMyNetAddrs == 0)
576 (void)rxi_GetcbiInfo();
578 myAddr = ntohl(addr);
580 if (IN_CLASSA(myAddr))
581 netMask = IN_CLASSA_NET;
582 else if (IN_CLASSB(myAddr))
583 netMask = IN_CLASSB_NET;
584 else if (IN_CLASSC(myAddr))
585 netMask = IN_CLASSC_NET;
589 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
590 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
591 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
592 if ((myAddr & netMask) == (thisAddr & netMask)) {
593 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
594 if (myAddr == thisAddr) {
599 if (match_value < 3) {
604 if (match_value < 2) {
615 #else /* AFS_USERSPACE_IP_ADDR */
617 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
618 #define IFADDR2SA(f) (&((f)->ifa_addr))
619 #else /* AFS_AIX41_ENV */
620 #define IFADDR2SA(f) ((f)->ifa_addr)
630 afs_uint32 addrs[ADDRSPERSITE];
631 int mtus[ADDRSPERSITE];
633 #if defined(AFS_DARWIN80_ENV)
639 struct sockaddr sout;
640 struct sockaddr_in *sin;
643 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
647 memset(addrs, 0, sizeof(addrs));
648 memset(mtus, 0, sizeof(mtus));
650 #if defined(AFS_DARWIN80_ENV)
651 if (!ifnet_list_get(AF_INET, &ifns, &count)) {
652 for (m = 0; m < count; m++) {
653 if (!ifnet_get_address_list(ifns[m], &ifads)) {
654 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
655 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
656 sin = (struct sockaddr_in *)&sout;
657 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
658 ifinaddr = ntohl(sin->sin_addr.s_addr);
659 if (myNetAddrs[i] != ifinaddr) {
663 rxmtu = rxi_AdjustIfMTU(rxmtu);
665 rxmtu * rxi_nRecvFrags +
666 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
667 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
668 addrs[i++] = ifinaddr;
669 if ((ifinaddr != 0x7f000001) &&
670 (maxmtu > rx_maxReceiveSize)) {
672 MIN(RX_MAX_PACKET_SIZE, maxmtu);
674 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
679 ifnet_free_address_list(ifads);
682 ifnet_list_free(ifns);
685 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
686 TAILQ_FOREACH(ifn, &ifnet, if_link) {
687 if (i >= ADDRSPERSITE)
689 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
690 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
691 ifn = ifn->if_list.tqe_next) {
693 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
695 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
696 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
697 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
698 if (i >= ADDRSPERSITE)
700 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
701 for (ifad = ifn->if_addrlist.tqh_first;
702 ifad != NULL && i < ADDRSPERSITE;
703 ifad = ifad->ifa_list.tqe_next) {
705 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
706 ifad = ifad->ifa_next) {
708 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
710 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
712 if (myNetAddrs[i] != ifinaddr) {
716 rxmtu = rxi_AdjustIfMTU(rxmtu);
718 rxmtu * rxi_nRecvFrags +
719 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
720 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
721 addrs[i++] = ifinaddr;
722 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
723 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
725 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
732 rx_maxJumboRecvSize =
733 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
734 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
735 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
739 for (l = 0; l < i; l++) {
740 myNetMTUs[l] = mtus[l];
741 myNetAddrs[l] = addrs[l];
747 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
748 /* Returns ifnet which best matches address */
750 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
752 struct sockaddr_in s, sr;
755 s.sin_family = AF_INET;
756 s.sin_addr.s_addr = addr;
757 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
760 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
761 *maskp = sr.sin_addr.s_addr;
763 return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
766 #else /* DARWIN || XBSD */
768 /* Returns ifnet which best matches address */
770 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
773 extern struct in_ifaddr *in_ifaddr;
774 struct in_ifaddr *ifa, *ifad = NULL;
778 #if defined(AFS_DARWIN_ENV)
779 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
780 ifa = TAILQ_NEXT(ifa, ia_link)) {
782 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
784 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
785 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
786 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
791 if (match_value < 3) {
796 if (match_value < 2) {
801 } /* if net matches */
802 } /* for all in_ifaddrs */
806 *maskp = ifad->ia_subnetmask;
807 return (ifad ? ifad->ia_ifp : NULL);
809 #endif /* else DARWIN || XBSD */
810 #endif /* else AFS_USERSPACE_IP_ADDR */
811 #endif /* !SUN5 && !SGI62 */
814 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
815 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
816 * system specific subdirectories for all systems. But for the moment,
817 * most of it is simple to follow common code.
819 #if !defined(UKERNEL)
820 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
821 /* rxk_NewSocket creates a new socket on the specified port. The port is
822 * in network byte order.
825 rxk_NewSocketHost(afs_uint32 ahost, short aport)
828 #ifdef AFS_DARWIN80_ENV
831 struct socket *newSocket;
833 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
836 struct sockaddr_in myaddr;
837 #ifdef AFS_HPUX110_ENV
838 /* prototype copied from kernel source file streams/str_proto.h */
839 extern MBLKP allocb_wait(int, int);
841 int addrsize = sizeof(struct sockaddr_in);
843 extern struct fileops socketops;
849 AFS_STATCNT(osi_NewSocket);
850 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
851 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
855 #if defined(AFS_HPUX102_ENV)
856 #if defined(AFS_HPUX110_ENV)
857 /* we need a file associated with the socket so sosend in NetSend
859 /* blocking socket */
860 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
864 fp->f_flag = FREAD | FWRITE;
865 fp->f_type = DTYPE_SOCKET;
866 fp->f_ops = &socketops;
868 fp->f_data = (void *)newSocket;
869 newSocket->so_fp = (void *)fp;
871 #else /* AFS_HPUX110_ENV */
872 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
873 #endif /* else AFS_HPUX110_ENV */
874 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
875 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
876 #elif defined(AFS_FBSD_ENV)
877 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
878 afs_osi_credp, curthread);
879 #elif defined(AFS_DARWIN80_ENV)
880 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
881 #elif defined(AFS_NBSD40_ENV)
882 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
884 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
885 #endif /* AFS_HPUX102_ENV */
889 memset(&myaddr, 0, sizeof myaddr);
890 myaddr.sin_family = AF_INET;
891 myaddr.sin_port = aport;
892 myaddr.sin_addr.s_addr = ahost;
893 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
894 myaddr.sin_len = sizeof(myaddr);
897 #ifdef AFS_HPUX110_ENV
898 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
903 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
905 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
906 #if defined(AFS_NBSD40_ENV)
907 code = sobind(newSocket, bindnam, addrsize, osi_curproc());
909 code = sobind(newSocket, bindnam, addrsize);
913 #if !defined(AFS_HPUX1122_ENV)
920 #else /* AFS_HPUX110_ENV */
921 #if defined(AFS_DARWIN80_ENV)
926 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
927 &buflen, sizeof(buflen));
928 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
929 &buflen, sizeof(buflen));
933 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
938 code = soreserve(newSocket, 50000, 50000);
940 code = soreserve(newSocket, 32766, 32766);
942 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
945 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
946 #if defined(AFS_FBSD_ENV)
947 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
949 code = sobind(newSocket, (struct sockaddr *)&myaddr);
952 dpf(("sobind fails (%d)\n", (int)code));
956 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
958 nam = m_getclr(M_WAIT, MT_SONAME);
959 #else /* AFS_OSF_ENV */
960 nam = m_get(M_WAIT, MT_SONAME);
963 #if defined(KERNEL_HAVE_UERROR)
968 nam->m_len = sizeof(myaddr);
969 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
970 #if defined(AFS_SGI65_ENV)
971 BHV_PDATA(&bhv) = (void *)newSocket;
972 code = sobind(&bhv, nam);
974 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
975 code = sobind(newSocket, nam, osi_curproc());
977 code = sobind(newSocket, nam);
980 dpf(("sobind fails (%d)\n", (int)code));
982 #ifndef AFS_SGI65_ENV
987 #endif /* else AFS_DARWIN_ENV */
988 #endif /* else AFS_HPUX110_ENV */
991 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
992 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
994 return (osi_socket *)newSocket;
998 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
999 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1001 return (osi_socket *)0;
1005 rxk_NewSocket(short aport)
1007 return rxk_NewSocketHost(0, aport);
1010 /* free socket allocated by rxk_NewSocket */
1012 rxk_FreeSocket(struct socket *asocket)
1014 AFS_STATCNT(osi_FreeSocket);
1015 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1016 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1018 #ifdef AFS_HPUX110_ENV
1019 if (asocket->so_fp) {
1020 struct file *fp = asocket->so_fp;
1021 #if !defined(AFS_HPUX1123_ENV)
1022 /* 11.23 still has falloc, but not FPENTRYFREE !
1023 * so for now if we shutdown, we will waist a file
1026 asocket->so_fp = NULL;
1029 #endif /* AFS_HPUX110_ENV */
1031 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1032 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1036 #endif /* !SUN5 && !LINUX20 */
1038 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1039 #ifdef AFS_DARWIN80_ENV
1040 /* Shutting down should wake us up, as should an earlier event. */
1042 rxi_ReScheduleEvents(void)
1044 /* needed to allow startup */
1045 int glock = ISAFS_GLOCK();
1048 osi_rxWakeup(&afs_termState);
1054 * Run RX event daemon every second (5 times faster than rest of systems)
1057 afs_rxevent_daemon(void)
1063 #ifdef RX_ENABLE_LOCKS
1065 #endif /* RX_ENABLE_LOCKS */
1067 rxevent_RaiseEvents(&temp);
1069 #ifdef RX_ENABLE_LOCKS
1071 #endif /* RX_ENABLE_LOCKS */
1072 #ifdef RX_KERNEL_TRACE
1073 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1074 "before afs_osi_Wait()");
1076 #ifdef AFS_DARWIN80_ENV
1077 afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1078 (temp.usec / 1000))), 0);
1080 afs_osi_Wait(500, NULL, 0);
1082 #ifdef RX_KERNEL_TRACE
1083 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1084 "after afs_osi_Wait()");
1086 if (afs_termState == AFSOP_STOP_RXEVENT) {
1087 #ifdef RXK_LISTENER_ENV
1088 afs_termState = AFSOP_STOP_RXK_LISTENER;
1090 #ifdef AFS_SUN510_ENV
1091 afs_termState = AFSOP_STOP_NETIF;
1093 afs_termState = AFSOP_STOP_COMPLETE;
1096 osi_rxWakeup(&afs_termState);
1103 #ifdef RXK_LISTENER_ENV
1105 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1107 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1110 struct sockaddr_in from;
1114 afs_int32 savelen; /* was using rlen but had aliasing problems */
1115 rx_computelen(p, tlen);
1116 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1118 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1119 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1120 * it once in order to avoid races. */
1123 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1131 /* add some padding to the last iovec, it's just to make sure that the
1132 * read doesn't return more data than we expect, and is done to get around
1133 * our problems caused by the lack of a length field in the rx header. */
1134 savelen = p->wirevec[p->niovecs - 1].iov_len;
1135 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1137 nbytes = tlen + sizeof(afs_int32);
1138 #ifdef RX_KERNEL_TRACE
1139 if (ICL_SETACTIVE(afs_iclSetp)) {
1141 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1142 "before osi_NetRecive()");
1146 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1148 #ifdef RX_KERNEL_TRACE
1149 if (ICL_SETACTIVE(afs_iclSetp)) {
1151 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1152 "after osi_NetRecive()");
1156 /* restore the vec to its correct state */
1157 p->wirevec[p->niovecs - 1].iov_len = savelen;
1160 p->length = nbytes - RX_HEADER_SIZE;;
1161 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1163 if (rx_stats_active) {
1164 MUTEX_ENTER(&rx_stats_mutex);
1165 rx_stats.bogusPacketOnRead++;
1166 rx_stats.bogusHost = from.sin_addr.s_addr;
1167 MUTEX_EXIT(&rx_stats_mutex);
1169 dpf(("B: bogus packet from [%x,%d] nb=%d",
1170 from.sin_addr.s_addr, from.sin_port, nbytes));
1174 /* Extract packet header. */
1175 rxi_DecodePacketHeader(p);
1177 *host = from.sin_addr.s_addr;
1178 *port = from.sin_port;
1179 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1180 if (rx_stats_active) {
1181 MUTEX_ENTER(&rx_stats_mutex);
1182 rx_stats.packetsRead[p->header.type - 1]++;
1183 MUTEX_EXIT(&rx_stats_mutex);
1187 #ifdef RX_TRIMDATABUFS
1188 /* Free any empty packet buffers at the end of this packet */
1189 rxi_TrimDataBufs(p, 1);
1199 * Listen for packets on socket. This thread is typically started after
1200 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1201 * the start state is set before proceeding.
1203 * Note that this thread is outside the AFS global lock for much of
1206 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1207 * we want here. So we need to either block all signals (including SIGKILL
1208 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1209 * OS's socket receive routine returns as a result of a signal.
1211 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1212 #ifdef AFS_LINUX20_ENV
1213 struct task_struct *rxk_ListenerTask;
1218 * Run the listener as a kernel thread.
1224 void rxk_ListenerProc(void);
1226 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1227 minclsyspri) == NULL)
1228 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1232 rxk_ListenerProc(void)
1233 #else /* AFS_SUN5_ENV */
1236 #endif /* AFS_SUN5_ENV */
1238 struct rx_packet *rxp = NULL;
1242 #ifdef AFS_LINUX20_ENV
1243 rxk_ListenerPid = current->pid;
1244 rxk_ListenerTask = current;
1247 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1248 #endif /* AFS_SUN5_ENV */
1250 rxk_ListenerPid = curproc->p_pid;
1251 #endif /* AFS_FBSD_ENV */
1252 #ifdef AFS_DARWIN80_ENV
1253 rxk_ListenerPid = proc_selfpid();
1254 #elif defined(AFS_DARWIN_ENV)
1255 rxk_ListenerPid = current_proc()->p_pid;
1257 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1259 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1260 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1261 /* See if a check for additional packets was issued */
1265 rxi_RestoreDataBufs(rxp);
1267 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1269 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1271 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1272 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1276 #ifdef RX_ENABLE_LOCKS
1278 #endif /* RX_ENABLE_LOCKS */
1279 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1280 #ifdef AFS_SUN510_ENV
1281 afs_termState = AFSOP_STOP_NETIF;
1283 afs_termState = AFSOP_STOP_COMPLETE;
1285 osi_rxWakeup(&afs_termState);
1287 rxk_ListenerPid = 0;
1288 #ifdef AFS_LINUX20_ENV
1289 rxk_ListenerTask = 0;
1290 osi_rxWakeup(&rxk_ListenerTask);
1292 #if defined(AFS_SUN5_ENV)
1293 osi_rxWakeup(&rxk_ListenerPid);
1297 #endif /* AFS_SUN5_ENV */
1300 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1301 /* The manner of stopping the rx listener thread may vary. Most unix's should
1302 * be able to call soclose.
1305 osi_StopListener(void)
1310 #endif /* RXK_LISTENER_ENV */
1311 #endif /* !NCR && !UKERNEL */
1313 #if !defined(AFS_LINUX26_ENV)
1315 #if defined(AFS_AIX_ENV)
1316 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1318 osi_Panic(char *msg, ...)
1323 msg = "Unknown AFS panic";
1325 * we should probably use the errsave facility here. it is not
1329 printf(msg, a1, a2, a3);
1331 #elif defined(AFS_SGI_ENV)
1334 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1336 cmn_err(CE_PANIC, "Unknown AFS panic");
1339 icmn_err(CE_PANIC, msg, ap);
1342 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1346 msg = "Unknown AFS panic";
1349 vsnprintf(buf, sizeof(buf), msg, ap);
1356 msg = "Unknown AFS panic";
1361 # ifdef AFS_LINUX20_ENV