2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * rx_kcommon.c - Common kernel RX code for all system types.
14 #include <afsconfig.h>
15 #include "afs/param.h"
18 #include "rx/rx_kcommon.h"
19 #include "rx_atomic.h"
22 #ifdef AFS_HPUX110_ENV
28 #ifndef RXK_LISTENER_ENV
29 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
30 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
33 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
34 extern struct interfaceAddr afs_cb_interface;
36 rxk_ports_t rxk_ports;
37 rxk_portRocks_t rxk_portRocks;
41 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
42 #define ADDRSPERSITE 16
43 static afs_uint32 myNetAddrs[ADDRSPERSITE];
44 static int myNetMTUs[ADDRSPERSITE];
45 static int numMyNetAddrs = 0;
48 #if defined(AFS_DARWIN80_ENV)
49 #define sobind sock_bind
50 #define soclose sock_close
53 /* add a port to the monitored list, port # is in network order */
55 rxk_AddPort(u_short aport, char *arock)
58 unsigned short *tsp, ts;
61 zslot = -1; /* look for an empty slot simultaneously */
62 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
63 if (((ts = *tsp) == 0) && (zslot == -1))
69 /* otherwise allocate a new port slot */
71 return E2BIG; /* all full */
72 rxk_ports[zslot] = aport;
73 rxk_portRocks[zslot] = arock;
77 /* remove as port from the monitored list, port # is in network order */
79 rxk_DelPort(u_short aport)
84 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
86 /* found it, adjust ref count and free the port reference if all gone */
91 /* otherwise port not found */
96 rxk_shutdownPorts(void)
99 for (i = 0; i < MAXRXPORTS; i++) {
102 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
103 soclose((struct socket *)rxk_portRocks[i]);
105 rxk_portRocks[i] = NULL;
111 rxi_GetHostUDPSocket(u_int host, u_short port)
114 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
115 if (sockp == (osi_socket *)0)
116 return OSI_NULLSOCKET;
117 rxk_AddPort(port, (char *)sockp);
118 return (osi_socket) sockp;
122 rxi_GetUDPSocket(u_short port)
124 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
128 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
129 * unsigned long value into the given buffer. Returns 0 on success,
130 * and a value less than 0 on failure. The contents of the buffer is
131 * defined only on success.
135 osi_utoa(char *buf, size_t len, unsigned long val)
137 long k; /* index of first byte of string value */
139 /* we definitely need room for at least one digit and NUL */
145 /* compute the string form from the high end of the buffer */
148 for (k = len - 2; k >= 0; k--) {
149 buf[k] = val % 10 + '0';
156 /* did we finish converting val to string form? */
162 /* this should never happen */
168 /* this should never happen */
174 /* if necessary, relocate string to beginning of buf[] */
179 * We need to achieve the effect of calling
181 * memmove(buf, &buf[k], len - k);
183 * However, since memmove() is not available in all
184 * kernels, we explicitly do an appropriate copy.
190 while ((*dst++ = *src++) != '\0')
197 #ifndef AFS_LINUX26_ENV
199 * osi_AssertFailK() -- used by the osi_Assert() macro.
201 * It essentially does
203 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
205 * Since the kernel version of osi_Panic() only passes its first
206 * argument to the native panic(), we construct a single string and hand
207 * that to osi_Panic().
210 osi_AssertFailK(const char *expr, const char *file, int line)
212 static const char msg0[] = "assertion failed: ";
213 static const char msg1[] = ", file: ";
214 static const char msg2[] = ", line: ";
215 static const char msg3[] = "\n";
218 * These buffers add up to 1K, which is a pleasantly nice round
219 * value, but probably not vital.
224 /* check line number conversion */
226 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
227 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
232 #define ADDBUF(BUF, STR) \
233 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
234 strcat(BUF, (char *)(STR)); \
243 ADDBUF(buf, linebuf);
248 osi_Panic("%s", buf);
252 osi_AssertFailK(const char *expr, const char *file, int line)
254 printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
259 /* This is the server process request loop. Kernel server
260 * processes never become listener threads */
262 rx_ServerProc(void *unused)
266 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
267 MUTEX_ENTER(&rx_quota_mutex);
268 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
269 /* threadID is used for making decisions in GetCall. Get it by bumping
270 * number of threads handling incoming calls */
271 threadID = rxi_availProcs++;
272 MUTEX_EXIT(&rx_quota_mutex);
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 */
284 #endif /* !UKERNEL */
286 #ifndef RXK_LISTENER_ENV
287 /* asize includes the Rx header */
289 MyPacketProc(struct rx_packet **ahandle, int asize)
291 struct rx_packet *tp;
293 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
294 /* If we can't scrounge enough cbufs, then we have to drop the packet,
295 * but we should set a flag so we magic up some more at our leisure.
298 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
299 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
300 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
302 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
303 RX_PACKET_CLASS_RECV_CBUF)) {
306 if (rx_stats_active) {
307 MUTEX_ENTER(&rx_stats_mutex);
308 rx_stats.noPacketBuffersOnRead++;
309 MUTEX_EXIT(&rx_stats_mutex);
315 * XXX if packet is too long for our buffer,
316 * should do this at a higher layer and let other
317 * end know we're losing.
319 if (rx_stats_active) {
320 MUTEX_ENTER(&rx_stats_mutex);
321 rx_stats.bogusPacketOnRead++;
322 MUTEX_EXIT(&rx_stats_mutex);
324 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
325 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
331 /* otherwise we have a packet, set appropriate values */
337 MyArrivalProc(struct rx_packet *ahandle,
338 struct sockaddr_in *afrom,
339 struct socket *arock,
342 /* handle basic rx packet */
343 ahandle->length = asize - RX_HEADER_SIZE;
344 rxi_DecodePacketHeader(ahandle);
346 rxi_ReceivePacket(ahandle, arock,
347 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
350 /* free the packet if it has been returned */
352 rxi_FreePacket(ahandle);
355 #endif /* !RXK_LISTENER_ENV */
358 rxi_StartListener(void)
360 /* if kernel, give name of appropriate procedures */
361 #ifndef RXK_LISTENER_ENV
362 rxk_GetPacketProc = MyPacketProc;
363 rxk_PacketArrivalProc = MyArrivalProc;
368 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
369 to get interesting information. */
371 rxi_InitPeerParams(struct rx_peer *pp)
376 # ifndef AFS_SUN5_ENV
377 # ifdef AFS_USERSPACE_IP_ADDR
381 i = rxi_Findcbi(pp->host);
384 /* pp->timeout.usec = 0; */
385 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
388 /* pp->timeout.usec = 0; */
389 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)
399 # else /* AFS_USERSPACE_IP_ADDR */
402 # if !defined(AFS_SGI62_ENV)
403 if (numMyNetAddrs == 0)
404 (void)rxi_GetIFInfo();
407 ifn = rxi_FindIfnet(pp->host, NULL);
410 /* pp->timeout.usec = 0; */
411 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
412 # ifdef IFF_POINTOPOINT
413 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
414 /* wish we knew the bit rate and the chunk size, sigh. */
416 pp->ifMTU = RX_PP_PACKET_SIZE;
418 # endif /* IFF_POINTOPOINT */
419 /* Diminish the packet size to one based on the MTU given by
421 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
422 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
423 if (rxmtu < pp->ifMTU)
426 } else { /* couldn't find the interface, so assume the worst */
428 /* pp->timeout.usec = 0; */
429 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
431 # endif /* else AFS_USERSPACE_IP_ADDR */
432 # else /* AFS_SUN5_ENV */
435 mtu = rxi_FindIfMTU(pp->host);
439 /* pp->timeout.usec = 0; */
440 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
443 /* pp->timeout.usec = 0; */
444 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
446 /* Diminish the packet size to one based on the MTU given by
448 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
449 rxmtu = mtu - RX_IPUDP_SIZE;
450 if (rxmtu < pp->ifMTU)
454 # endif /* AFS_SUN5_ENV */
455 #else /* ADAPT_MTU */
456 pp->rateFlag = 2; /* start timing after two full packets */
458 pp->ifMTU = OLD_MAX_PACKET_SIZE;
459 #endif /* else ADAPT_MTU */
460 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
461 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
462 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
464 MIN(rxi_nDgramPackets,
465 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
466 pp->maxDgramPackets = 1;
468 /* Initialize slow start parameters */
469 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
471 pp->nDgramPackets = 1;
476 /* The following code is common to several system types, but not all. The
477 * separate ones are found in the system specific subdirectories.
481 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
482 /* Routine called during the afsd "-shutdown" process to put things back to
485 static struct protosw parent_proto; /* udp proto switch */
488 shutdown_rxkernel(void)
490 struct protosw *tpro, *last;
491 last = inetdomain.dom_protoswNPROTOSW;
492 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
493 if (tpro->pr_protocol == IPPROTO_UDP) {
494 /* restore original udp protocol switch */
495 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
496 memset((void *)&parent_proto, 0, sizeof(parent_proto));
501 dpf(("shutdown_rxkernel: no udp proto\n"));
503 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
505 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
506 /* Determine what the network interfaces are for this machine. */
508 #ifdef AFS_USERSPACE_IP_ADDR
512 int i, j, different = 0, num = ADDRSPERSITE;
515 afs_uint32 addrs[ADDRSPERSITE];
516 int mtus[ADDRSPERSITE];
518 memset((void *)addrs, 0, sizeof(addrs));
519 memset((void *)mtus, 0, sizeof(mtus));
521 if (afs_cb_interface.numberOfInterfaces < num)
522 num = afs_cb_interface.numberOfInterfaces;
523 for (i = 0; i < num; i++) {
524 if (!afs_cb_interface.mtu[i])
525 afs_cb_interface.mtu[i] = htonl(1500);
526 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
527 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
528 if (myNetAddrs[i] != ifinaddr)
532 rxmtu = rxi_AdjustIfMTU(rxmtu);
534 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
535 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
536 addrs[i++] = ifinaddr;
537 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
538 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
539 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
543 rx_maxJumboRecvSize =
544 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
545 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
546 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
549 for (j = 0; j < i; j++) {
550 myNetMTUs[j] = mtus[j];
551 myNetAddrs[j] = addrs[j];
558 /* Returns the afs_cb_interface inxex which best matches address.
559 * If none is found, we return -1.
562 rxi_Findcbi(afs_uint32 addr)
565 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
566 afs_int32 rvalue = -1;
569 if (numMyNetAddrs == 0)
570 (void)rxi_GetcbiInfo();
572 myAddr = ntohl(addr);
574 if (IN_CLASSA(myAddr))
575 netMask = IN_CLASSA_NET;
576 else if (IN_CLASSB(myAddr))
577 netMask = IN_CLASSB_NET;
578 else if (IN_CLASSC(myAddr))
579 netMask = IN_CLASSC_NET;
583 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
584 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
585 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
586 if ((myAddr & netMask) == (thisAddr & netMask)) {
587 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
588 if (myAddr == thisAddr) {
593 if (match_value < 3) {
598 if (match_value < 2) {
609 #else /* AFS_USERSPACE_IP_ADDR */
611 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
612 #define IFADDR2SA(f) (&((f)->ifa_addr))
613 #else /* AFS_AIX41_ENV */
614 #define IFADDR2SA(f) ((f)->ifa_addr)
624 afs_uint32 addrs[ADDRSPERSITE];
625 int mtus[ADDRSPERSITE];
627 #if defined(AFS_DARWIN80_ENV)
633 struct sockaddr sout;
634 struct sockaddr_in *sin;
637 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
641 memset(addrs, 0, sizeof(addrs));
642 memset(mtus, 0, sizeof(mtus));
644 #if defined(AFS_DARWIN80_ENV)
645 if (!ifnet_list_get(AF_INET, &ifns, &count)) {
646 for (m = 0; m < count; m++) {
647 if (!ifnet_get_address_list(ifns[m], &ifads)) {
648 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
649 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
650 sin = (struct sockaddr_in *)&sout;
651 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
652 ifinaddr = ntohl(sin->sin_addr.s_addr);
653 if (myNetAddrs[i] != ifinaddr) {
657 rxmtu = rxi_AdjustIfMTU(rxmtu);
659 rxmtu * rxi_nRecvFrags +
660 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
661 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
662 addrs[i++] = ifinaddr;
663 if (!rx_IsLoopbackAddr(ifinaddr) &&
664 (maxmtu > rx_maxReceiveSize)) {
666 MIN(RX_MAX_PACKET_SIZE, maxmtu);
668 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
673 ifnet_free_address_list(ifads);
676 ifnet_list_free(ifns);
679 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
680 TAILQ_FOREACH(ifn, &ifnet, if_link) {
681 if (i >= ADDRSPERSITE)
683 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
684 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
685 ifn = ifn->if_list.tqe_next) {
687 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
689 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
690 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
691 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
692 if (i >= ADDRSPERSITE)
694 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
695 for (ifad = ifn->if_addrlist.tqh_first;
696 ifad != NULL && i < ADDRSPERSITE;
697 ifad = ifad->ifa_list.tqe_next) {
699 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
700 ifad = ifad->ifa_next) {
702 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
704 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
706 if (myNetAddrs[i] != ifinaddr) {
710 rxmtu = rxi_AdjustIfMTU(rxmtu);
712 rxmtu * rxi_nRecvFrags +
713 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
714 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
715 addrs[i++] = ifinaddr;
716 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
717 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
719 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
726 rx_maxJumboRecvSize =
727 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
728 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
729 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
733 for (l = 0; l < i; l++) {
734 myNetMTUs[l] = mtus[l];
735 myNetAddrs[l] = addrs[l];
741 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
742 /* Returns ifnet which best matches address */
744 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
746 struct sockaddr_in s, sr;
749 s.sin_family = AF_INET;
750 s.sin_addr.s_addr = addr;
751 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
754 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
755 *maskp = sr.sin_addr.s_addr;
757 return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
760 #else /* DARWIN || XBSD */
762 /* Returns ifnet which best matches address */
764 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
767 extern struct in_ifaddr *in_ifaddr;
768 struct in_ifaddr *ifa, *ifad = NULL;
772 #if defined(AFS_DARWIN_ENV)
773 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
774 ifa = TAILQ_NEXT(ifa, ia_link)) {
776 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
778 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
779 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
780 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
785 if (match_value < 3) {
790 if (match_value < 2) {
795 } /* if net matches */
796 } /* for all in_ifaddrs */
800 *maskp = ifad->ia_subnetmask;
801 return (ifad ? ifad->ia_ifp : NULL);
803 #endif /* else DARWIN || XBSD */
804 #endif /* else AFS_USERSPACE_IP_ADDR */
805 #endif /* !SUN5 && !SGI62 */
808 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
809 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
810 * system specific subdirectories for all systems. But for the moment,
811 * most of it is simple to follow common code.
813 #if !defined(UKERNEL)
814 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
815 /* rxk_NewSocket creates a new socket on the specified port. The port is
816 * in network byte order.
819 rxk_NewSocketHost(afs_uint32 ahost, short aport)
822 #ifdef AFS_DARWIN80_ENV
825 struct socket *newSocket;
827 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
830 struct sockaddr_in myaddr;
831 #ifdef AFS_HPUX110_ENV
832 /* prototype copied from kernel source file streams/str_proto.h */
833 extern MBLKP allocb_wait(int, int);
835 int addrsize = sizeof(struct sockaddr_in);
837 extern struct fileops socketops;
843 AFS_STATCNT(osi_NewSocket);
844 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
845 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
849 #if defined(AFS_HPUX102_ENV)
850 #if defined(AFS_HPUX110_ENV)
851 /* we need a file associated with the socket so sosend in NetSend
853 /* blocking socket */
854 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
858 fp->f_flag = FREAD | FWRITE;
859 fp->f_type = DTYPE_SOCKET;
860 fp->f_ops = &socketops;
862 fp->f_data = (void *)newSocket;
863 newSocket->so_fp = (void *)fp;
865 #else /* AFS_HPUX110_ENV */
866 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
867 #endif /* else AFS_HPUX110_ENV */
868 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
869 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
870 #elif defined(AFS_FBSD_ENV)
871 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
872 afs_osi_credp, curthread);
873 #elif defined(AFS_DARWIN80_ENV)
874 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
875 #elif defined(AFS_NBSD50_ENV)
876 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
877 #elif defined(AFS_NBSD40_ENV)
878 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
880 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
881 #endif /* AFS_HPUX102_ENV */
885 memset(&myaddr, 0, sizeof myaddr);
886 myaddr.sin_family = AF_INET;
887 myaddr.sin_port = aport;
888 myaddr.sin_addr.s_addr = ahost;
889 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
890 myaddr.sin_len = sizeof(myaddr);
893 #ifdef AFS_HPUX110_ENV
894 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
899 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
901 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
902 #if defined(AFS_NBSD40_ENV)
903 code = sobind(newSocket, bindnam, addrsize, osi_curproc());
905 code = sobind(newSocket, bindnam, addrsize);
909 #if !defined(AFS_HPUX1122_ENV)
916 #else /* AFS_HPUX110_ENV */
917 #if defined(AFS_DARWIN80_ENV)
922 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
923 &buflen, sizeof(buflen));
924 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
925 &buflen, sizeof(buflen));
929 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
934 code = soreserve(newSocket, 50000, 50000);
936 code = soreserve(newSocket, 32766, 32766);
938 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
941 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
942 #if defined(AFS_FBSD_ENV)
943 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
945 code = sobind(newSocket, (struct sockaddr *)&myaddr);
948 dpf(("sobind fails (%d)\n", (int)code));
952 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
954 nam = m_getclr(M_WAIT, MT_SONAME);
955 #else /* AFS_OSF_ENV */
956 nam = m_get(M_WAIT, MT_SONAME);
959 #if defined(KERNEL_HAVE_UERROR)
964 nam->m_len = sizeof(myaddr);
965 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
966 #if defined(AFS_SGI65_ENV)
967 BHV_PDATA(&bhv) = (void *)newSocket;
968 code = sobind(&bhv, nam);
970 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
971 code = sobind(newSocket, nam, osi_curproc());
973 code = sobind(newSocket, nam);
976 dpf(("sobind fails (%d)\n", (int)code));
978 #ifndef AFS_SGI65_ENV
983 #endif /* else AFS_DARWIN_ENV */
984 #endif /* else AFS_HPUX110_ENV */
987 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
988 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
990 return (osi_socket *)newSocket;
994 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
995 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
997 return (osi_socket *)0;
1001 rxk_NewSocket(short aport)
1003 return rxk_NewSocketHost(0, aport);
1006 /* free socket allocated by rxk_NewSocket */
1008 rxk_FreeSocket(struct socket *asocket)
1010 AFS_STATCNT(osi_FreeSocket);
1011 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1012 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1014 #ifdef AFS_HPUX110_ENV
1015 if (asocket->so_fp) {
1016 struct file *fp = asocket->so_fp;
1017 #if !defined(AFS_HPUX1123_ENV)
1018 /* 11.23 still has falloc, but not FPENTRYFREE !
1019 * so for now if we shutdown, we will waist a file
1022 asocket->so_fp = NULL;
1025 #endif /* AFS_HPUX110_ENV */
1027 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1028 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1032 #endif /* !SUN5 && !LINUX20 */
1034 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1035 #ifdef AFS_DARWIN80_ENV
1036 /* Shutting down should wake us up, as should an earlier event. */
1038 rxi_ReScheduleEvents(void)
1040 /* needed to allow startup */
1041 int glock = ISAFS_GLOCK();
1044 osi_rxWakeup(&afs_termState);
1050 * Run RX event daemon every second (5 times faster than rest of systems)
1053 afs_rxevent_daemon(void)
1059 #ifdef RX_ENABLE_LOCKS
1061 #endif /* RX_ENABLE_LOCKS */
1063 rxevent_RaiseEvents(&temp);
1065 #ifdef RX_ENABLE_LOCKS
1067 #endif /* RX_ENABLE_LOCKS */
1068 #ifdef RX_KERNEL_TRACE
1069 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1070 "before afs_osi_Wait()");
1072 #ifdef AFS_DARWIN80_ENV
1073 afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1074 (temp.usec / 1000))), 0);
1076 afs_osi_Wait(500, NULL, 0);
1078 #ifdef RX_KERNEL_TRACE
1079 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1080 "after afs_osi_Wait()");
1082 if (afs_termState == AFSOP_STOP_RXEVENT) {
1083 #ifdef RXK_LISTENER_ENV
1084 afs_termState = AFSOP_STOP_RXK_LISTENER;
1086 #ifdef AFS_SUN510_ENV
1087 afs_termState = AFSOP_STOP_NETIF;
1089 afs_termState = AFSOP_STOP_COMPLETE;
1092 osi_rxWakeup(&afs_termState);
1099 #ifdef RXK_LISTENER_ENV
1101 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1103 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1106 struct sockaddr_in from;
1110 afs_int32 savelen; /* was using rlen but had aliasing problems */
1111 rx_computelen(p, tlen);
1112 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1114 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1115 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1116 * it once in order to avoid races. */
1119 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1127 /* add some padding to the last iovec, it's just to make sure that the
1128 * read doesn't return more data than we expect, and is done to get around
1129 * our problems caused by the lack of a length field in the rx header. */
1130 savelen = p->wirevec[p->niovecs - 1].iov_len;
1131 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1133 nbytes = tlen + sizeof(afs_int32);
1134 #ifdef RX_KERNEL_TRACE
1135 if (ICL_SETACTIVE(afs_iclSetp)) {
1137 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1138 "before osi_NetRecive()");
1142 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1144 #ifdef RX_KERNEL_TRACE
1145 if (ICL_SETACTIVE(afs_iclSetp)) {
1147 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1148 "after osi_NetRecive()");
1152 /* restore the vec to its correct state */
1153 p->wirevec[p->niovecs - 1].iov_len = savelen;
1156 p->length = nbytes - RX_HEADER_SIZE;;
1157 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1159 if (rx_stats_active) {
1160 MUTEX_ENTER(&rx_stats_mutex);
1161 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1162 rx_stats.bogusHost = from.sin_addr.s_addr;
1163 MUTEX_EXIT(&rx_stats_mutex);
1165 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1166 from.sin_addr.s_addr, from.sin_port, nbytes));
1170 /* Extract packet header. */
1171 rxi_DecodePacketHeader(p);
1173 *host = from.sin_addr.s_addr;
1174 *port = from.sin_port;
1175 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1176 if (rx_stats_active) {
1177 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1181 #ifdef RX_TRIMDATABUFS
1182 /* Free any empty packet buffers at the end of this packet */
1183 rxi_TrimDataBufs(p, 1);
1193 * Listen for packets on socket. This thread is typically started after
1194 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1195 * the start state is set before proceeding.
1197 * Note that this thread is outside the AFS global lock for much of
1200 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1201 * we want here. So we need to either block all signals (including SIGKILL
1202 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1203 * OS's socket receive routine returns as a result of a signal.
1205 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1206 #ifdef AFS_LINUX20_ENV
1207 struct task_struct *rxk_ListenerTask;
1212 * Run the listener as a kernel thread.
1218 void rxk_ListenerProc(void);
1220 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1221 minclsyspri) == NULL)
1222 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1226 rxk_ListenerProc(void)
1227 #else /* AFS_SUN5_ENV */
1230 #endif /* AFS_SUN5_ENV */
1232 struct rx_packet *rxp = NULL;
1236 #ifdef AFS_LINUX20_ENV
1237 rxk_ListenerPid = current->pid;
1238 rxk_ListenerTask = current;
1241 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1242 #endif /* AFS_SUN5_ENV */
1244 rxk_ListenerPid = curproc->p_pid;
1245 #endif /* AFS_FBSD_ENV */
1246 #ifdef AFS_DARWIN80_ENV
1247 rxk_ListenerPid = proc_selfpid();
1248 #elif defined(AFS_DARWIN_ENV)
1249 rxk_ListenerPid = current_proc()->p_pid;
1251 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1253 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1254 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1255 /* See if a check for additional packets was issued */
1259 rxi_RestoreDataBufs(rxp);
1261 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1263 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1265 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1266 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1270 #ifdef RX_ENABLE_LOCKS
1272 #endif /* RX_ENABLE_LOCKS */
1273 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1274 #ifdef AFS_SUN510_ENV
1275 afs_termState = AFSOP_STOP_NETIF;
1277 afs_termState = AFSOP_STOP_COMPLETE;
1279 osi_rxWakeup(&afs_termState);
1281 rxk_ListenerPid = 0;
1282 #ifdef AFS_LINUX20_ENV
1283 rxk_ListenerTask = 0;
1284 osi_rxWakeup(&rxk_ListenerTask);
1286 #if defined(AFS_SUN5_ENV)
1287 osi_rxWakeup(&rxk_ListenerPid);
1291 #endif /* AFS_SUN5_ENV */
1294 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1295 /* The manner of stopping the rx listener thread may vary. Most unix's should
1296 * be able to call soclose.
1299 osi_StopListener(void)
1304 #endif /* RXK_LISTENER_ENV */
1305 #endif /* !NCR && !UKERNEL */
1307 #if !defined(AFS_LINUX26_ENV)
1309 #if defined(AFS_AIX_ENV)
1310 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1312 osi_Panic(char *msg, ...)
1317 msg = "Unknown AFS panic";
1319 * we should probably use the errsave facility here. it is not
1323 printf(msg, a1, a2, a3);
1325 #elif defined(AFS_SGI_ENV)
1328 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1330 cmn_err(CE_PANIC, "Unknown AFS panic");
1333 icmn_err(CE_PANIC, msg, ap);
1336 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1340 msg = "Unknown AFS panic";
1343 vsnprintf(buf, sizeof(buf), msg, ap);
1350 msg = "Unknown AFS panic";
1355 # ifdef AFS_LINUX20_ENV