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 /* jaltman - rxi_dataQuota is protected by a mutex everywhere else */
265 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
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++;
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 MUTEX_ENTER(&rx_stats_mutex);
305 rx_stats.noPacketBuffersOnRead++;
306 MUTEX_EXIT(&rx_stats_mutex);
312 * XXX if packet is too long for our buffer,
313 * should do this at a higher layer and let other
314 * end know we're losing.
316 if (rx_stats_active) {
317 MUTEX_ENTER(&rx_stats_mutex);
318 rx_stats.bogusPacketOnRead++;
319 MUTEX_EXIT(&rx_stats_mutex);
321 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
322 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
328 /* otherwise we have a packet, set appropriate values */
334 MyArrivalProc(struct rx_packet *ahandle,
335 struct sockaddr_in *afrom,
336 struct socket *arock,
339 /* handle basic rx packet */
340 ahandle->length = asize - RX_HEADER_SIZE;
341 rxi_DecodePacketHeader(ahandle);
343 rxi_ReceivePacket(ahandle, arock,
344 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
347 /* free the packet if it has been returned */
349 rxi_FreePacket(ahandle);
352 #endif /* !RXK_LISTENER_ENV */
355 rxi_StartListener(void)
357 /* if kernel, give name of appropriate procedures */
358 #ifndef RXK_LISTENER_ENV
359 rxk_GetPacketProc = MyPacketProc;
360 rxk_PacketArrivalProc = MyArrivalProc;
365 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
366 to get interesting information. */
368 rxi_InitPeerParams(struct rx_peer *pp)
373 # ifndef AFS_SUN5_ENV
374 # ifdef AFS_USERSPACE_IP_ADDR
378 i = rxi_Findcbi(pp->host);
381 /* pp->timeout.usec = 0; */
382 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
385 /* pp->timeout.usec = 0; */
386 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
389 mtu = ntohl(afs_cb_interface.mtu[i]);
390 /* Diminish the packet size to one based on the MTU given by
392 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
393 rxmtu = mtu - RX_IPUDP_SIZE;
394 if (rxmtu < pp->ifMTU)
397 } else { /* couldn't find the interface, so assume the worst */
398 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
400 # else /* AFS_USERSPACE_IP_ADDR */
403 # if !defined(AFS_SGI62_ENV)
404 if (numMyNetAddrs == 0)
405 (void)rxi_GetIFInfo();
408 ifn = rxi_FindIfnet(pp->host, NULL);
411 /* pp->timeout.usec = 0; */
412 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
413 # ifdef IFF_POINTOPOINT
414 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
415 /* wish we knew the bit rate and the chunk size, sigh. */
417 pp->ifMTU = RX_PP_PACKET_SIZE;
419 # endif /* IFF_POINTOPOINT */
420 /* Diminish the packet size to one based on the MTU given by
422 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
423 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
424 if (rxmtu < pp->ifMTU)
427 } else { /* couldn't find the interface, so assume the worst */
429 /* pp->timeout.usec = 0; */
430 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
432 # endif /* else AFS_USERSPACE_IP_ADDR */
433 # else /* AFS_SUN5_ENV */
436 mtu = rxi_FindIfMTU(pp->host);
440 /* pp->timeout.usec = 0; */
441 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
444 /* pp->timeout.usec = 0; */
445 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
449 /* Diminish the packet size to one based on the MTU given by
451 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
452 rxmtu = mtu - RX_IPUDP_SIZE;
453 if (rxmtu < pp->ifMTU)
456 } else { /* couldn't find the interface, so assume the worst */
457 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE,rx_MyMaxSendSize);
459 # endif /* AFS_SUN5_ENV */
460 #else /* ADAPT_MTU */
461 pp->rateFlag = 2; /* start timing after two full packets */
463 pp->ifMTU = OLD_MAX_PACKET_SIZE;
464 #endif /* else ADAPT_MTU */
465 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
466 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
467 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
469 MIN(rxi_nDgramPackets,
470 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
471 pp->maxDgramPackets = 1;
473 /* Initialize slow start parameters */
474 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
476 pp->nDgramPackets = 1;
481 /* The following code is common to several system types, but not all. The
482 * separate ones are found in the system specific subdirectories.
486 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
487 /* Routine called during the afsd "-shutdown" process to put things back to
490 static struct protosw parent_proto; /* udp proto switch */
493 shutdown_rxkernel(void)
495 struct protosw *tpro, *last;
496 last = inetdomain.dom_protoswNPROTOSW;
497 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
498 if (tpro->pr_protocol == IPPROTO_UDP) {
499 /* restore original udp protocol switch */
500 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
501 memset((void *)&parent_proto, 0, sizeof(parent_proto));
506 dpf(("shutdown_rxkernel: no udp proto"));
508 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
510 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
511 /* Determine what the network interfaces are for this machine. */
513 #ifdef AFS_USERSPACE_IP_ADDR
517 int i, j, different = 0, num = ADDRSPERSITE;
520 afs_uint32 addrs[ADDRSPERSITE];
521 int mtus[ADDRSPERSITE];
523 memset((void *)addrs, 0, sizeof(addrs));
524 memset((void *)mtus, 0, sizeof(mtus));
526 if (afs_cb_interface.numberOfInterfaces < num)
527 num = afs_cb_interface.numberOfInterfaces;
528 for (i = 0; i < num; i++) {
529 if (!afs_cb_interface.mtu[i])
530 afs_cb_interface.mtu[i] = htonl(1500);
531 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
532 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
533 if (myNetAddrs[i] != ifinaddr)
537 rxmtu = rxi_AdjustIfMTU(rxmtu);
539 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
540 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
541 addrs[i++] = ifinaddr;
542 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
543 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
544 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
548 rx_maxJumboRecvSize =
549 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
550 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
551 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
554 for (j = 0; j < i; j++) {
555 myNetMTUs[j] = mtus[j];
556 myNetAddrs[j] = addrs[j];
563 /* Returns the afs_cb_interface inxex which best matches address.
564 * If none is found, we return -1.
567 rxi_Findcbi(afs_uint32 addr)
570 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
571 afs_int32 rvalue = -1;
574 if (numMyNetAddrs == 0)
575 (void)rxi_GetcbiInfo();
577 myAddr = ntohl(addr);
579 if (IN_CLASSA(myAddr))
580 netMask = IN_CLASSA_NET;
581 else if (IN_CLASSB(myAddr))
582 netMask = IN_CLASSB_NET;
583 else if (IN_CLASSC(myAddr))
584 netMask = IN_CLASSC_NET;
588 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
589 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
590 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
591 if ((myAddr & netMask) == (thisAddr & netMask)) {
592 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
593 if (myAddr == thisAddr) {
598 if (match_value < 3) {
603 if (match_value < 2) {
614 #else /* AFS_USERSPACE_IP_ADDR */
616 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
617 #define IFADDR2SA(f) (&((f)->ifa_addr))
618 #else /* AFS_AIX41_ENV */
619 #define IFADDR2SA(f) ((f)->ifa_addr)
629 afs_uint32 addrs[ADDRSPERSITE];
630 int mtus[ADDRSPERSITE];
632 #if defined(AFS_DARWIN80_ENV)
638 struct sockaddr sout;
639 struct sockaddr_in *sin;
642 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
646 memset(addrs, 0, sizeof(addrs));
647 memset(mtus, 0, sizeof(mtus));
649 #if defined(AFS_DARWIN80_ENV)
650 if (!ifnet_list_get(AF_INET, &ifns, &count)) {
651 for (m = 0; m < count; m++) {
652 if (!ifnet_get_address_list(ifns[m], &ifads)) {
653 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
654 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
655 sin = (struct sockaddr_in *)&sout;
656 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
657 ifinaddr = ntohl(sin->sin_addr.s_addr);
658 if (myNetAddrs[i] != ifinaddr) {
662 rxmtu = rxi_AdjustIfMTU(rxmtu);
664 rxmtu * rxi_nRecvFrags +
665 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
666 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
667 addrs[i++] = ifinaddr;
668 if ((ifinaddr != 0x7f000001) &&
669 (maxmtu > rx_maxReceiveSize)) {
671 MIN(RX_MAX_PACKET_SIZE, maxmtu);
673 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
678 ifnet_free_address_list(ifads);
681 ifnet_list_free(ifns);
684 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
685 TAILQ_FOREACH(ifn, &ifnet, if_link) {
686 if (i >= ADDRSPERSITE)
688 #elif defined(AFS_OBSD_ENV)
689 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
690 ifn = ifn->if_list.tqe_next) {
692 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
694 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
695 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
696 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
697 if (i >= ADDRSPERSITE)
699 #elif defined(AFS_OBSD_ENV)
700 for (ifad = ifn->if_addrlist.tqh_first;
701 ifad != NULL && i < ADDRSPERSITE;
702 ifad = ifad->ifa_list.tqe_next) {
704 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
705 ifad = ifad->ifa_next) {
707 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
709 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
711 if (myNetAddrs[i] != ifinaddr) {
715 rxmtu = rxi_AdjustIfMTU(rxmtu);
717 rxmtu * rxi_nRecvFrags +
718 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
719 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
720 addrs[i++] = ifinaddr;
721 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
722 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
724 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
731 rx_maxJumboRecvSize =
732 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
733 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
734 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
738 for (l = 0; l < i; l++) {
739 myNetMTUs[l] = mtus[l];
740 myNetAddrs[l] = addrs[l];
746 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
747 /* Returns ifnet which best matches address */
749 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
751 struct sockaddr_in s, sr;
754 s.sin_family = AF_INET;
755 s.sin_addr.s_addr = addr;
756 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
759 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
760 *maskp = sr.sin_addr.s_addr;
762 return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
765 #else /* DARWIN60 || XBSD */
767 /* Returns ifnet which best matches address */
769 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
772 extern struct in_ifaddr *in_ifaddr;
773 struct in_ifaddr *ifa, *ifad = NULL;
777 #if defined(AFS_DARWIN_ENV)
778 for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
779 ifa = TAILQ_NEXT(ifa, ia_link)) {
781 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
783 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
784 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
785 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
790 if (match_value < 3) {
795 if (match_value < 2) {
800 } /* if net matches */
801 } /* for all in_ifaddrs */
805 *maskp = ifad->ia_subnetmask;
806 return (ifad ? ifad->ia_ifp : NULL);
808 #endif /* else DARWIN60 || XBSD */
809 #endif /* else AFS_USERSPACE_IP_ADDR */
810 #endif /* !SUN5 && !SGI62 */
813 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
814 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
815 * system specific subdirectories for all systems. But for the moment,
816 * most of it is simple to follow common code.
818 #if !defined(UKERNEL)
819 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
820 /* rxk_NewSocket creates a new socket on the specified port. The port is
821 * in network byte order.
824 rxk_NewSocketHost(afs_uint32 ahost, short aport)
827 #ifdef AFS_DARWIN80_ENV
830 struct socket *newSocket;
832 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
835 struct sockaddr_in myaddr;
836 #ifdef AFS_HPUX110_ENV
837 /* prototype copied from kernel source file streams/str_proto.h */
838 extern MBLKP allocb_wait(int, int);
840 int addrsize = sizeof(struct sockaddr_in);
842 extern struct fileops socketops;
848 AFS_STATCNT(osi_NewSocket);
849 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
850 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
854 #if defined(AFS_HPUX102_ENV)
855 #if defined(AFS_HPUX110_ENV)
856 /* we need a file associated with the socket so sosend in NetSend
858 /* blocking socket */
859 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
863 fp->f_flag = FREAD | FWRITE;
864 fp->f_type = DTYPE_SOCKET;
865 fp->f_ops = &socketops;
867 fp->f_data = (void *)newSocket;
868 newSocket->so_fp = (void *)fp;
870 #else /* AFS_HPUX110_ENV */
871 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
872 #endif /* else AFS_HPUX110_ENV */
873 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
874 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
875 #elif defined(AFS_FBSD50_ENV)
876 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
877 afs_osi_credp, curthread);
878 #elif defined(AFS_FBSD40_ENV)
879 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
880 #elif defined(AFS_DARWIN80_ENV)
881 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
883 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
884 #endif /* AFS_HPUX102_ENV */
888 memset(&myaddr, 0, sizeof myaddr);
889 myaddr.sin_family = AF_INET;
890 myaddr.sin_port = aport;
891 myaddr.sin_addr.s_addr = ahost;
892 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
893 myaddr.sin_len = sizeof(myaddr);
896 #ifdef AFS_HPUX110_ENV
897 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
902 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
904 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
906 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_FBSD50_ENV)
943 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
944 #elif defined(AFS_FBSD40_ENV)
945 code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
947 code = sobind(newSocket, (struct sockaddr *)&myaddr);
950 dpf(("sobind fails (%d)\n", (int)code));
955 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
957 nam = m_getclr(M_WAIT, MT_SONAME);
958 #else /* AFS_OSF_ENV */
959 nam = m_get(M_WAIT, MT_SONAME);
962 #if defined(KERNEL_HAVE_UERROR)
967 nam->m_len = sizeof(myaddr);
968 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
969 #if defined(AFS_SGI65_ENV)
970 BHV_PDATA(&bhv) = (void *)newSocket;
971 code = sobind(&bhv, nam);
973 #elif defined(AFS_OBSD44_ENV)
974 code = sobind(newSocket, nam, osi_curproc());
976 code = sobind(newSocket, nam);
979 dpf(("sobind fails (%d)\n", (int)code));
981 #ifndef AFS_SGI65_ENV
986 #endif /* else AFS_DARWIN_ENV */
987 #endif /* else AFS_HPUX110_ENV */
990 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
991 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
993 return (osi_socket *)newSocket;
997 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
998 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1000 return (osi_socket *)0;
1004 rxk_NewSocket(short aport)
1006 return rxk_NewSocketHost(0, aport);
1009 /* free socket allocated by rxk_NewSocket */
1011 rxk_FreeSocket(struct socket *asocket)
1013 AFS_STATCNT(osi_FreeSocket);
1014 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1015 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1017 #ifdef AFS_HPUX110_ENV
1018 if (asocket->so_fp) {
1019 struct file *fp = asocket->so_fp;
1020 #if !defined(AFS_HPUX1123_ENV)
1021 /* 11.23 still has falloc, but not FPENTRYFREE !
1022 * so for now if we shutdown, we will waist a file
1025 asocket->so_fp = NULL;
1028 #endif /* AFS_HPUX110_ENV */
1030 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1031 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1035 #endif /* !SUN5 && !LINUX20 */
1037 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1038 #if 0/*def AFS_DARWIN80_ENV*/
1039 /* Shutting down should wake us up, as should an earlier event. */
1041 rxi_ReScheduleEvents(void)
1043 /* needed to allow startup */
1044 int glock = ISAFS_GLOCK();
1047 osi_rxWakeup(&afs_termState);
1053 * Run RX event daemon every second (5 times faster than rest of systems)
1056 afs_rxevent_daemon(void)
1062 #ifdef RX_ENABLE_LOCKS
1064 #endif /* RX_ENABLE_LOCKS */
1066 rxevent_RaiseEvents(&temp);
1068 #ifdef RX_ENABLE_LOCKS
1070 #endif /* RX_ENABLE_LOCKS */
1071 #ifdef RX_KERNEL_TRACE
1072 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1073 "before afs_osi_Wait()");
1075 #if 0/*def AFS_DARWIN80_ENV*/
1076 afs_osi_TimedSleep(&afs_termState, ((temp.sec * 1000) +
1077 (temp.usec / 1000)), 0);
1079 afs_osi_Wait(500, NULL, 0);
1081 #ifdef RX_KERNEL_TRACE
1082 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1083 "after afs_osi_Wait()");
1085 if (afs_termState == AFSOP_STOP_RXEVENT) {
1086 #ifdef RXK_LISTENER_ENV
1087 afs_termState = AFSOP_STOP_RXK_LISTENER;
1089 #ifdef AFS_SUN510_ENV
1090 afs_termState = AFSOP_STOP_NETIF;
1092 afs_termState = AFSOP_STOP_COMPLETE;
1095 osi_rxWakeup(&afs_termState);
1102 #ifdef RXK_LISTENER_ENV
1104 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1106 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1109 struct sockaddr_in from;
1113 afs_int32 savelen; /* was using rlen but had aliasing problems */
1114 rx_computelen(p, tlen);
1115 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1117 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1118 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1119 * it once in order to avoid races. */
1122 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1130 /* add some padding to the last iovec, it's just to make sure that the
1131 * read doesn't return more data than we expect, and is done to get around
1132 * our problems caused by the lack of a length field in the rx header. */
1133 savelen = p->wirevec[p->niovecs - 1].iov_len;
1134 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1136 nbytes = tlen + sizeof(afs_int32);
1137 #ifdef RX_KERNEL_TRACE
1138 if (ICL_SETACTIVE(afs_iclSetp)) {
1140 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1141 "before osi_NetRecive()");
1145 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1147 #ifdef RX_KERNEL_TRACE
1148 if (ICL_SETACTIVE(afs_iclSetp)) {
1150 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1151 "after osi_NetRecive()");
1155 /* restore the vec to its correct state */
1156 p->wirevec[p->niovecs - 1].iov_len = savelen;
1159 p->length = nbytes - RX_HEADER_SIZE;;
1160 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1162 if (rx_stats_active) {
1163 MUTEX_ENTER(&rx_stats_mutex);
1164 rx_stats.bogusPacketOnRead++;
1165 rx_stats.bogusHost = from.sin_addr.s_addr;
1166 MUTEX_EXIT(&rx_stats_mutex);
1168 dpf(("B: bogus packet from [%x,%d] nb=%d",
1169 from.sin_addr.s_addr, from.sin_port, nbytes));
1173 /* Extract packet header. */
1174 rxi_DecodePacketHeader(p);
1176 *host = from.sin_addr.s_addr;
1177 *port = from.sin_port;
1178 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1179 if (rx_stats_active) {
1180 MUTEX_ENTER(&rx_stats_mutex);
1181 rx_stats.packetsRead[p->header.type - 1]++;
1182 MUTEX_EXIT(&rx_stats_mutex);
1186 #ifdef RX_TRIMDATABUFS
1187 /* Free any empty packet buffers at the end of this packet */
1188 rxi_TrimDataBufs(p, 1);
1198 * Listen for packets on socket. This thread is typically started after
1199 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1200 * the start state is set before proceeding.
1202 * Note that this thread is outside the AFS global lock for much of
1205 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1206 * we want here. So we need to either block all signals (including SIGKILL
1207 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1208 * OS's socket receive routine returns as a result of a signal.
1210 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1211 #ifdef AFS_LINUX20_ENV
1212 struct task_struct *rxk_ListenerTask;
1217 * Run the listener as a kernel thread.
1223 void rxk_ListenerProc(void);
1225 (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1226 minclsyspri) == NULL)
1227 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1231 rxk_ListenerProc(void)
1232 #else /* AFS_SUN5_ENV */
1235 #endif /* AFS_SUN5_ENV */
1237 struct rx_packet *rxp = NULL;
1241 #ifdef AFS_LINUX20_ENV
1242 rxk_ListenerPid = current->pid;
1243 rxk_ListenerTask = current;
1246 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1247 #endif /* AFS_SUN5_ENV */
1249 rxk_ListenerPid = curproc->p_pid;
1250 #endif /* AFS_FBSD_ENV */
1251 #ifdef AFS_DARWIN80_ENV
1252 rxk_ListenerPid = proc_selfpid();
1253 #elif defined(AFS_DARWIN_ENV)
1254 rxk_ListenerPid = current_proc()->p_pid;
1256 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1258 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1259 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1261 rxi_RestoreDataBufs(rxp);
1263 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1265 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1267 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1268 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1272 #ifdef RX_ENABLE_LOCKS
1274 #endif /* RX_ENABLE_LOCKS */
1275 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1276 #ifdef AFS_SUN510_ENV
1277 afs_termState = AFSOP_STOP_NETIF;
1279 afs_termState = AFSOP_STOP_COMPLETE;
1281 osi_rxWakeup(&afs_termState);
1283 rxk_ListenerPid = 0;
1284 #ifdef AFS_LINUX20_ENV
1285 rxk_ListenerTask = 0;
1286 osi_rxWakeup(&rxk_ListenerTask);
1288 #if defined(AFS_SUN5_ENV)
1289 osi_rxWakeup(&rxk_ListenerPid);
1293 #endif /* AFS_SUN5_ENV */
1296 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1297 /* The manner of stopping the rx listener thread may vary. Most unix's should
1298 * be able to call soclose.
1301 osi_StopListener(void)
1306 #endif /* RXK_LISTENER_ENV */
1307 #endif /* !NCR && !UKERNEL */
1309 #if !defined(AFS_LINUX26_ENV)
1311 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
1312 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1314 osi_Panic(char *msg, ...)
1317 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
1319 msg = "Unknown AFS panic";
1320 printf(msg, a1, a2, a3);
1322 #elif (defined(AFS_DARWIN80_ENV) && !defined(AFS_DARWIN90_ENV)) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1326 msg = "Unknown AFS panic";
1329 vsnprintf(buf, sizeof(buf), msg, ap);
1336 msg = "Unknown AFS panic";
1341 # ifdef AFS_LINUX20_ENV