2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * rx_kcommon.c - Common kernel RX code for all system types.
14 #include <afsconfig.h>
15 #include "afs/param.h"
20 #include "rx/rx_kcommon.h"
22 #ifdef AFS_HPUX110_ENV
28 #ifndef RXK_LISTENER_ENV
29 int (*rxk_PacketArrivalProc) (register struct rx_packet * ahandle, register struct sockaddr_in * afrom, char *arock, afs_int32 asize); /* set to packet allocation procedure */
30 int (*rxk_GetPacketProc) (char **ahandle, int asize);
33 struct 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 /* add a port to the monitored list, port # is in network order */
50 rxk_AddPort(u_short aport, char *arock)
53 unsigned short *tsp, ts;
56 zslot = -1; /* look for an empty slot simultaneously */
57 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
58 if (((ts = *tsp) == 0) && (zslot == -1))
64 /* otherwise allocate a new port slot */
66 return E2BIG; /* all full */
67 rxk_ports[zslot] = aport;
68 rxk_portRocks[zslot] = arock;
72 /* remove as port from the monitored list, port # is in network order */
74 rxk_DelPort(u_short aport)
77 register unsigned short *tsp;
79 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
81 /* found it, adjust ref count and free the port reference if all gone */
86 /* otherwise port not found */
91 rxk_shutdownPorts(void)
94 for (i = 0; i < MAXRXPORTS; i++) {
97 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
98 soclose((struct socket *)rxk_portRocks[i]);
100 rxk_portRocks[i] = NULL;
106 rxi_GetHostUDPSocket(u_int host, u_short port)
108 struct osi_socket *sockp;
109 sockp = (struct osi_socket *)rxk_NewSocketHost(host, port);
110 if (sockp == (struct osi_socket *)0)
111 return OSI_NULLSOCKET;
112 rxk_AddPort(port, (char *)sockp);
113 return (osi_socket) sockp;
117 rxi_GetUDPSocket(u_short port)
119 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
123 osi_Panic(msg, a1, a2, a3)
127 msg = "Unknown AFS panic";
129 printf(msg, a1, a2, a3);
130 #ifdef AFS_LINUX20_ENV
131 *((char *)0xffffffff) = 42;
138 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
139 * unsigned long value into the given buffer. Returns 0 on success,
140 * and a value less than 0 on failure. The contents of the buffer is
141 * defined only on success.
145 osi_utoa(char *buf, size_t len, unsigned long val)
147 long k; /* index of first byte of string value */
149 /* we definitely need room for at least one digit and NUL */
155 /* compute the string form from the high end of the buffer */
158 for (k = len - 2; k >= 0; k--) {
159 buf[k] = val % 10 + '0';
166 /* did we finish converting val to string form? */
172 /* this should never happen */
178 /* this should never happen */
184 /* if necessary, relocate string to beginning of buf[] */
189 * We need to achieve the effect of calling
191 * memmove(buf, &buf[k], len - k);
193 * However, since memmove() is not available in all
194 * kernels, we explicitly do an appropriate copy.
200 while ((*dst++ = *src++) != '\0')
208 * osi_AssertFailK() -- used by the osi_Assert() macro.
210 * It essentially does
212 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
214 * Since the kernel version of osi_Panic() only passes its first
215 * argument to the native panic(), we construct a single string and hand
216 * that to osi_Panic().
219 osi_AssertFailK(const char *expr, const char *file, int line)
221 static const char msg0[] = "assertion failed: ";
222 static const char msg1[] = ", file: ";
223 static const char msg2[] = ", line: ";
224 static const char msg3[] = "\n";
227 * These buffers add up to 1K, which is a pleasantly nice round
228 * value, but probably not vital.
233 /* check line number conversion */
235 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
236 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
241 #define ADDBUF(BUF, STR) \
242 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
243 strcat(BUF, (char *)(STR)); \
252 ADDBUF(buf, linebuf);
261 /* This is the server process request loop. Kernel server
262 * processes never become listener threads */
268 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
269 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
270 /* threadID is used for making decisions in GetCall. Get it by bumping
271 * number of threads handling incoming calls */
272 threadID = rxi_availProcs++;
274 #ifdef RX_ENABLE_LOCKS
276 #endif /* RX_ENABLE_LOCKS */
277 rxi_ServerProc(threadID, NULL, NULL);
278 #ifdef RX_ENABLE_LOCKS
280 #endif /* RX_ENABLE_LOCKS */
282 #endif /* !UKERNEL */
284 #ifndef RXK_LISTENER_ENV
285 /* asize includes the Rx header */
287 MyPacketProc(char **ahandle, int asize)
289 register 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 MUTEX_ENTER(&rx_stats_mutex);
305 rx_stats.noPacketBuffersOnRead++;
306 MUTEX_EXIT(&rx_stats_mutex);
311 * XXX if packet is too long for our buffer,
312 * should do this at a higher layer and let other
313 * end know we're losing.
315 MUTEX_ENTER(&rx_stats_mutex);
316 rx_stats.bogusPacketOnRead++;
317 MUTEX_EXIT(&rx_stats_mutex);
318 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
319 printf("rx: packet dropped: bad ulen=%d\n", asize);
325 /* otherwise we have a packet, set appropriate values */
326 *ahandle = (char *)tp;
331 MyArrivalProc(register struct rx_packet *ahandle,
332 register struct sockaddr_in *afrom, char *arock,
335 /* handle basic rx packet */
336 ahandle->length = asize - RX_HEADER_SIZE;
337 rxi_DecodePacketHeader(ahandle);
339 rxi_ReceivePacket(ahandle, (struct socket *)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 kernel, give name of appropriate procedures */
354 #ifndef RXK_LISTENER_ENV
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(register struct rx_peer *pp)
371 #ifdef AFS_USERSPACE_IP_ADDR
372 i = rxi_Findcbi(pp->host);
375 /* pp->timeout.usec = 0; */
376 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
379 /* pp->timeout.usec = 0; */
380 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
383 mtu = ntohl(afs_cb_interface.mtu[i]);
384 /* Diminish the packet size to one based on the MTU given by
386 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
387 rxmtu = mtu - RX_IPUDP_SIZE;
388 if (rxmtu < pp->ifMTU)
391 } else { /* couldn't find the interface, so assume the worst */
392 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
394 #else /* AFS_USERSPACE_IP_ADDR */
397 #if !defined(AFS_SGI62_ENV)
398 if (numMyNetAddrs == 0)
399 (void)rxi_GetIFInfo();
402 ifn = rxi_FindIfnet(pp->host, NULL);
405 /* pp->timeout.usec = 0; */
406 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
407 #ifdef IFF_POINTOPOINT
408 if (ifn->if_flags & IFF_POINTOPOINT) {
409 /* wish we knew the bit rate and the chunk size, sigh. */
411 pp->ifMTU = RX_PP_PACKET_SIZE;
413 #endif /* IFF_POINTOPOINT */
414 /* Diminish the packet size to one based on the MTU given by
416 if (ifn->if_mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
417 rxmtu = ifn->if_mtu - RX_IPUDP_SIZE;
418 if (rxmtu < pp->ifMTU)
421 } else { /* couldn't find the interface, so assume the worst */
423 /* pp->timeout.usec = 0; */
424 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
426 #endif /* else AFS_USERSPACE_IP_ADDR */
427 #else /* AFS_SUN5_ENV */
428 mtu = rxi_FindIfMTU(pp->host);
432 /* pp->timeout.usec = 0; */
433 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
436 /* pp->timeout.usec = 0; */
437 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
441 /* Diminish the packet size to one based on the MTU given by
443 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
444 rxmtu = mtu - RX_IPUDP_SIZE;
445 if (rxmtu < pp->ifMTU)
448 } else { /* couldn't find the interface, so assume the worst */
449 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
451 #endif /* AFS_SUN5_ENV */
452 #else /* ADAPT_MTU */
453 pp->rateFlag = 2; /* start timing after two full packets */
455 pp->ifMTU = OLD_MAX_PACKET_SIZE;
456 #endif /* else ADAPT_MTU */
457 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
458 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
459 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
461 MIN(rxi_nDgramPackets,
462 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
463 pp->maxDgramPackets = 1;
465 /* Initialize slow start parameters */
466 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
468 pp->nDgramPackets = 1;
473 /* The following code is common to several system types, but not all. The
474 * separate ones are found in the system specific subdirectories.
478 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
479 /* Routine called during the afsd "-shutdown" process to put things back to
482 static struct protosw parent_proto; /* udp proto switch */
485 shutdown_rxkernel(void)
487 register struct protosw *tpro, *last;
488 last = inetdomain.dom_protoswNPROTOSW;
489 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
490 if (tpro->pr_protocol == IPPROTO_UDP) {
491 /* restore original udp protocol switch */
492 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
493 memset((void *)&parent_proto, 0, sizeof(parent_proto));
498 printf("shutdown_rxkernel: no udp proto");
500 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
502 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
503 /* Determine what the network interfaces are for this machine. */
505 #ifdef AFS_USERSPACE_IP_ADDR
509 int i, j, different = 0;
512 afs_uint32 addrs[ADDRSPERSITE];
513 int mtus[ADDRSPERSITE];
515 memset((void *)addrs, 0, sizeof(addrs));
516 memset((void *)mtus, 0, sizeof(mtus));
518 for (i = 0; i < afs_cb_interface.numberOfInterfaces; i++) {
519 if (!afs_cb_interface.mtu[i])
520 afs_cb_interface.mtu[i] = htonl(1500);
521 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
522 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
523 if (myNetAddrs[i] != ifinaddr)
527 rxmtu = rxi_AdjustIfMTU(rxmtu);
529 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
530 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
531 addrs[i++] = ifinaddr;
532 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
533 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
534 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
538 rx_maxJumboRecvSize =
539 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
540 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
541 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
544 for (j = 0; j < i; j++) {
545 myNetMTUs[j] = mtus[j];
546 myNetAddrs[j] = addrs[j];
553 /* Returns the afs_cb_interface inxex which best matches address.
554 * If none is found, we return -1.
557 rxi_Findcbi(afs_uint32 addr)
560 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
561 afs_int32 rvalue = -1;
564 if (numMyNetAddrs == 0)
565 (void)rxi_GetcbiInfo();
567 myAddr = ntohl(addr);
569 if (IN_CLASSA(myAddr))
570 netMask = IN_CLASSA_NET;
571 else if (IN_CLASSB(myAddr))
572 netMask = IN_CLASSB_NET;
573 else if (IN_CLASSC(myAddr))
574 netMask = IN_CLASSC_NET;
578 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
579 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
580 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
581 if ((myAddr & netMask) == (thisAddr & netMask)) {
582 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
583 if (myAddr == thisAddr) {
588 if (match_value < 3) {
593 if (match_value < 2) {
604 #else /* AFS_USERSPACE_IP_ADDR */
606 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
607 #define IFADDR2SA(f) (&((f)->ifa_addr))
608 #else /* AFS_AIX41_ENV */
609 #define IFADDR2SA(f) ((f)->ifa_addr)
618 register struct ifnet *ifn;
619 register int rxmtu, maxmtu;
620 afs_uint32 addrs[ADDRSPERSITE];
621 int mtus[ADDRSPERSITE];
622 struct ifaddr *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_DARWIN_ENV) || defined(AFS_FBSD_ENV)
629 TAILQ_FOREACH(ifn, &ifnet, if_link) {
630 if (i >= ADDRSPERSITE)
632 #elif defined(AFS_OBSD_ENV)
633 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
634 ifn = ifn->if_list.tqe_next) {
636 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
638 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
639 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
640 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
641 if (i >= ADDRSPERSITE)
643 #elif defined(AFS_OBSD_ENV)
644 for (ifad = ifn->if_addrlist.tqh_first;
645 ifad != NULL && i < ADDRSPERSITE;
646 ifad = ifad->ifa_list.tqe_next) {
648 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
649 ifad = ifad->ifa_next) {
651 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
653 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
655 if (myNetAddrs[i] != ifinaddr) {
659 rxmtu = rxi_AdjustIfMTU(rxmtu);
661 rxmtu * rxi_nRecvFrags +
662 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
663 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
664 addrs[i++] = ifinaddr;
665 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
666 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
668 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
674 rx_maxJumboRecvSize =
675 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
676 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
677 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
681 for (j = 0; j < i; j++) {
682 myNetMTUs[j] = mtus[j];
683 myNetAddrs[j] = addrs[j];
689 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
690 /* Returns ifnet which best matches address */
692 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
694 struct sockaddr_in s;
697 s.sin_family = AF_INET;
698 s.sin_addr.s_addr = addr;
699 ifad = ifa_ifwithnet((struct sockaddr *)&s);
702 *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
703 return (ifad ? ifad->ifa_ifp : NULL);
706 #else /* DARWIN60 || XBSD */
708 /* Returns ifnet which best matches address */
710 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
713 extern struct in_ifaddr *in_ifaddr;
714 struct in_ifaddr *ifa, *ifad = NULL;
718 #if defined(AFS_DARWIN_ENV)
719 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
720 ifa = TAILQ_NEXT(ifa, ia_link)) {
722 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
724 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
725 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
726 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
731 if (match_value < 3) {
736 if (match_value < 2) {
741 } /* if net matches */
742 } /* for all in_ifaddrs */
746 *maskp = ifad->ia_subnetmask;
747 return (ifad ? ifad->ia_ifp : NULL);
749 #endif /* else DARWIN60 || XBSD */
750 #endif /* else AFS_USERSPACE_IP_ADDR */
751 #endif /* !SUN5 && !SGI62 */
754 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
755 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
756 * system specific subdirectories for all systems. But for the moment,
757 * most of it is simple to follow common code.
759 #if !defined(UKERNEL)
760 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
761 /* rxk_NewSocket creates a new socket on the specified port. The port is
762 * in network byte order.
765 rxk_NewSocketHost(afs_uint32 ahost, short aport)
767 register afs_int32 code;
768 struct socket *newSocket;
769 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
772 struct sockaddr_in myaddr;
773 #ifdef AFS_HPUX110_ENV
774 /* prototype copied from kernel source file streams/str_proto.h */
775 extern MBLKP allocb_wait(int, int);
777 int addrsize = sizeof(struct sockaddr_in);
779 extern struct fileops socketops;
785 AFS_STATCNT(osi_NewSocket);
786 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
787 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
791 #if defined(AFS_HPUX102_ENV)
792 #if defined(AFS_HPUX110_ENV)
793 /* we need a file associated with the socket so sosend in NetSend
795 /* blocking socket */
796 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
799 fp->f_flag = FREAD | FWRITE;
800 fp->f_type = DTYPE_SOCKET;
801 fp->f_ops = &socketops;
803 fp->f_data = (void *) newSocket;
804 newSocket->so_fp = (void *)fp;
806 #else /* AFS_HPUX110_ENV */
807 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
808 #endif /* else AFS_HPUX110_ENV */
809 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
810 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
811 #elif defined(AFS_FBSD50_ENV)
812 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
813 afs_osi_credp, curthread);
814 #elif defined(AFS_FBSD40_ENV)
815 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
817 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
818 #endif /* AFS_HPUX102_ENV */
822 memset(&myaddr, 0, sizeof myaddr);
823 myaddr.sin_family = AF_INET;
824 myaddr.sin_port = aport;
825 myaddr.sin_addr.s_addr = ahost;
826 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
827 myaddr.sin_len = sizeof(myaddr);
830 #ifdef AFS_HPUX110_ENV
831 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
836 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
838 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
840 code = sobind(newSocket, bindnam, addrsize);
843 #if !defined(AFS_HPUX1122_ENV)
850 #else /* AFS_HPUX110_ENV */
851 code = soreserve(newSocket, 50000, 50000);
853 code = soreserve(newSocket, 32766, 32766);
855 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
857 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
858 #if defined(AFS_FBSD50_ENV)
859 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
860 #elif defined(AFS_FBSD40_ENV)
861 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
863 code = sobind(newSocket, (struct sockaddr *)&myaddr);
866 printf("sobind fails (%d)\n", (int)code);
871 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
873 nam = m_getclr(M_WAIT, MT_SONAME);
874 #else /* AFS_OSF_ENV */
875 nam = m_get(M_WAIT, MT_SONAME);
878 #if defined(KERNEL_HAVE_UERROR)
883 nam->m_len = sizeof(myaddr);
884 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
886 BHV_PDATA(&bhv) = (void *)newSocket;
887 code = sobind(&bhv, nam);
890 code = sobind(newSocket, nam);
893 printf("sobind fails (%d)\n", (int)code);
895 #ifndef AFS_SGI65_ENV
900 #endif /* else AFS_DARWIN_ENV */
901 #endif /* else AFS_HPUX110_ENV */
904 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
905 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
907 return (struct osi_socket *)newSocket;
911 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
912 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
914 return (struct osi_socket *)0;
918 rxk_NewSocket(short aport)
920 return rxk_NewSocketHost(0, aport);
923 /* free socket allocated by rxk_NewSocket */
925 rxk_FreeSocket(register struct socket *asocket)
927 AFS_STATCNT(osi_FreeSocket);
928 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
929 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
931 #ifdef AFS_HPUX110_ENV
932 if (asocket->so_fp) {
933 struct file * fp = asocket->so_fp;
934 #if !defined(AFS_HPUX1123_ENV)
935 /* 11.23 still has falloc, but not FPENTRYFREE !
936 so for now if we shutdown, we will waist a file
939 asocket->so_fp = NULL;
942 #endif /* AFS_HPUX110_ENV */
944 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
945 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
949 #endif /* !SUN5 && !LINUX20 */
951 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
953 * Run RX event daemon every second (5 times faster than rest of systems)
956 afs_rxevent_daemon(void)
962 #ifdef RX_ENABLE_LOCKS
964 #endif /* RX_ENABLE_LOCKS */
967 rxevent_RaiseEvents(&temp);
970 #ifdef RX_ENABLE_LOCKS
972 #endif /* RX_ENABLE_LOCKS */
973 #ifdef RX_KERNEL_TRACE
974 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
975 "before afs_osi_Wait()");
977 afs_osi_Wait(500, NULL, 0);
978 #ifdef RX_KERNEL_TRACE
979 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
980 "after afs_osi_Wait()");
982 if (afs_termState == AFSOP_STOP_RXEVENT) {
983 #ifdef RXK_LISTENER_ENV
984 afs_termState = AFSOP_STOP_RXK_LISTENER;
986 afs_termState = AFSOP_STOP_COMPLETE;
988 osi_rxWakeup(&afs_termState);
995 #ifdef RXK_LISTENER_ENV
997 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
999 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1002 struct sockaddr_in from;
1005 register afs_int32 tlen;
1006 afs_int32 savelen; /* was using rlen but had aliasing problems */
1007 rx_computelen(p, tlen);
1008 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1010 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1011 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1012 * it once in order to avoid races. */
1015 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1023 /* add some padding to the last iovec, it's just to make sure that the
1024 * read doesn't return more data than we expect, and is done to get around
1025 * our problems caused by the lack of a length field in the rx header. */
1026 savelen = p->wirevec[p->niovecs - 1].iov_len;
1027 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1029 nbytes = tlen + sizeof(afs_int32);
1030 #ifdef RX_KERNEL_TRACE
1031 if (ICL_SETACTIVE(afs_iclSetp)) {
1033 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1034 "before osi_NetRecive()");
1038 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1040 #ifdef RX_KERNEL_TRACE
1041 if (ICL_SETACTIVE(afs_iclSetp)) {
1043 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1044 "after osi_NetRecive()");
1048 /* restore the vec to its correct state */
1049 p->wirevec[p->niovecs - 1].iov_len = savelen;
1052 p->length = nbytes - RX_HEADER_SIZE;;
1053 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1055 rxi_MorePackets(rx_initSendWindow);
1057 MUTEX_ENTER(&rx_stats_mutex);
1058 rx_stats.bogusPacketOnRead++;
1059 rx_stats.bogusHost = from.sin_addr.s_addr;
1060 MUTEX_EXIT(&rx_stats_mutex);
1061 dpf(("B: bogus packet from [%x,%d] nb=%d",
1062 from.sin_addr.s_addr, from.sin_port, nbytes));
1066 /* Extract packet header. */
1067 rxi_DecodePacketHeader(p);
1069 *host = from.sin_addr.s_addr;
1070 *port = from.sin_port;
1071 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1072 MUTEX_ENTER(&rx_stats_mutex);
1073 rx_stats.packetsRead[p->header.type - 1]++;
1074 MUTEX_EXIT(&rx_stats_mutex);
1077 /* Free any empty packet buffers at the end of this packet */
1078 rxi_TrimDataBufs(p, 1);
1088 * Listen for packets on socket. This thread is typically started after
1089 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1090 * the start state is set before proceeding.
1092 * Note that this thread is outside the AFS global lock for much of
1095 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1096 * we want here. So we need to either block all signals (including SIGKILL
1097 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1098 * OS's socket receive routine returns as a result of a signal.
1100 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1104 * Run the listener as a kernel thread.
1110 void rxk_ListenerProc(void);
1112 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1113 minclsyspri) == NULL)
1114 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1118 rxk_ListenerProc(void)
1119 #else /* AFS_SUN5_ENV */
1122 #endif /* AFS_SUN5_ENV */
1124 struct rx_packet *rxp = NULL;
1128 #ifdef AFS_LINUX20_ENV
1129 rxk_ListenerPid = current->pid;
1132 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1133 #endif /* AFS_SUN5_ENV */
1135 rxk_ListenerPid = curproc->p_pid;
1136 #endif /* AFS_FBSD_ENV */
1137 #if defined(AFS_DARWIN_ENV)
1138 rxk_ListenerPid = current_proc()->p_pid;
1140 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1142 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1144 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1146 rxi_RestoreDataBufs(rxp);
1148 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1150 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1152 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1154 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1159 #ifdef RX_ENABLE_LOCKS
1161 #endif /* RX_ENABLE_LOCKS */
1162 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1163 afs_termState = AFSOP_STOP_COMPLETE;
1164 osi_rxWakeup(&afs_termState);
1166 rxk_ListenerPid = 0;
1167 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1168 osi_rxWakeup(&rxk_ListenerPid);
1172 #endif /* AFS_SUN5_ENV */
1175 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1176 /* The manner of stopping the rx listener thread may vary. Most unix's should
1177 * be able to call soclose.
1180 osi_StopListener(void)
1185 #endif /* RXK_LISTENER_ENV */
1187 #endif /* !NCR && !UKERNEL */