2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * rx_kcommon.c - Common kernel RX code for all system types.
14 #include <afsconfig.h>
15 #include <afs/param.h>
18 #include "rx/rx_kcommon.h"
19 #include "rx_atomic.h"
20 #include "rx_packet.h"
21 #include "rx_internal.h"
25 #ifdef AFS_HPUX110_ENV
31 #ifndef RXK_LISTENER_ENV
32 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize); /* set to packet allocation procedure */
33 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
36 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
37 extern struct interfaceAddr afs_cb_interface;
39 rxk_ports_t rxk_ports;
40 rxk_portRocks_t rxk_portRocks;
44 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV)
45 # define ADDRSPERSITE 16
46 static afs_uint32 myNetAddrs[ADDRSPERSITE];
47 static int myNetMTUs[ADDRSPERSITE];
48 static int numMyNetAddrs = 0;
51 #if defined(AFS_DARWIN80_ENV)
52 # define sobind sock_bind
53 # define soclose sock_close
56 /* add a port to the monitored list, port # is in network order */
58 rxk_AddPort(u_short aport, char *arock)
61 unsigned short *tsp, ts;
64 zslot = -1; /* look for an empty slot simultaneously */
65 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
66 if (((ts = *tsp) == 0) && (zslot == -1))
72 /* otherwise allocate a new port slot */
74 return E2BIG; /* all full */
75 rxk_ports[zslot] = aport;
76 rxk_portRocks[zslot] = arock;
80 /* remove as port from the monitored list, port # is in network order */
82 rxk_DelPort(u_short aport)
87 for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
89 /* found it, adjust ref count and free the port reference if all gone */
94 /* otherwise port not found */
99 rxk_shutdownPorts(void)
102 for (i = 0; i < MAXRXPORTS; i++) {
105 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV) && ! defined(AFS_SOCKPROXY_ENV)
106 soclose((struct socket *)rxk_portRocks[i]);
108 rxk_portRocks[i] = NULL;
114 rxi_GetHostUDPSocket(u_int host, u_short port)
117 sockp = (osi_socket *)rxk_NewSocketHost(host, port);
118 if (sockp == (osi_socket *)0)
119 return OSI_NULLSOCKET;
120 rxk_AddPort(port, (char *)sockp);
121 return (osi_socket) sockp;
125 rxi_GetUDPSocket(u_short port)
127 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
131 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
132 * unsigned long value into the given buffer. Returns 0 on success,
133 * and a value less than 0 on failure. The contents of the buffer is
134 * defined only on success.
138 osi_utoa(char *buf, size_t len, unsigned long val)
140 long k; /* index of first byte of string value */
142 /* we definitely need room for at least one digit and NUL */
148 /* compute the string form from the high end of the buffer */
151 for (k = len - 2; k >= 0; k--) {
152 buf[k] = val % 10 + '0';
159 /* did we finish converting val to string form? */
165 /* this should never happen */
171 /* this should never happen */
177 /* if necessary, relocate string to beginning of buf[] */
182 * We need to achieve the effect of calling
184 * memmove(buf, &buf[k], len - k);
186 * However, since memmove() is not available in all
187 * kernels, we explicitly do an appropriate copy.
193 while ((*dst++ = *src++) != '\0')
200 #ifndef AFS_LINUX_ENV
202 * osi_AssertFailK() -- used by the osi_Assert() macro.
204 * It essentially does
206 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
208 * Since the kernel version of osi_Panic() only passes its first
209 * argument to the native panic(), we construct a single string and hand
210 * that to osi_Panic().
213 osi_AssertFailK(const char *expr, const char *file, int line)
215 static const char msg0[] = "assertion failed: ";
216 static const char msg1[] = ", file: ";
217 static const char msg2[] = ", line: ";
218 static const char msg3[] = "\n";
221 * These buffers add up to 1K, which is a pleasantly nice round
222 * value, but probably not vital.
227 /* check line number conversion */
229 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
230 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
235 # define ADDBUF(BUF, STR) \
236 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
237 strcat(BUF, (char *)(STR)); \
246 ADDBUF(buf, linebuf);
251 osi_Panic("%s", buf);
253 #endif /* !AFS_LINUX_ENV */
256 /* This is the server process request loop. Kernel server
257 * processes never become listener threads */
259 rx_ServerProc(void *unused)
263 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
264 MUTEX_ENTER(&rx_quota_mutex);
265 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
266 /* threadID is used for making decisions in GetCall. Get it by bumping
267 * number of threads handling incoming calls */
268 threadID = rxi_availProcs++;
269 MUTEX_EXIT(&rx_quota_mutex);
271 # ifdef RX_ENABLE_LOCKS
273 # endif /* RX_ENABLE_LOCKS */
274 rxi_ServerProc(threadID, NULL, NULL);
275 # ifdef RX_ENABLE_LOCKS
277 # endif /* RX_ENABLE_LOCKS */
281 #endif /* !UKERNEL */
283 #ifndef RXK_LISTENER_ENV
284 /* asize includes the Rx header */
286 MyPacketProc(struct rx_packet **ahandle, int asize)
288 struct rx_packet *tp;
290 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
291 /* If we can't scrounge enough cbufs, then we have to drop the packet,
292 * but we should set a flag so we magic up some more at our leisure.
295 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
296 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
297 if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
299 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
300 RX_PACKET_CLASS_RECV_CBUF)) {
303 if (rx_stats_active) {
304 rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
310 * XXX if packet is too long for our buffer,
311 * should do this at a higher layer and let other
312 * end know we're losing.
314 if (rx_stats_active) {
315 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
317 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
318 dpf(("rx: packet dropped: bad ulen=%d\n", asize));
324 /* otherwise we have a packet, set appropriate values */
330 MyArrivalProc(struct rx_packet *ahandle,
331 struct sockaddr_in *afrom,
332 struct socket *arock,
335 /* handle basic rx packet */
336 ahandle->length = asize - RX_HEADER_SIZE;
337 rxi_DecodePacketHeader(ahandle);
339 rxi_ReceivePacket(ahandle, arock,
340 afrom->sin_addr.s_addr, afrom->sin_port, NULL,
343 /* free the packet if it has been returned */
345 rxi_FreePacket(ahandle);
348 #endif /* !RXK_LISTENER_ENV */
351 rxi_StartListener(void)
353 #if !defined(RXK_LISTENER_ENV) && !defined(RXK_UPCALL_ENV)
354 /* if kernel, give name of appropriate procedures */
355 rxk_GetPacketProc = MyPacketProc;
356 rxk_PacketArrivalProc = MyArrivalProc;
361 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
362 to get interesting information. */
364 rxi_InitPeerParams(struct rx_peer *pp)
369 # ifdef AFS_USERSPACE_IP_ADDR
373 i = rxi_Findcbi(pp->host);
375 rx_rto_setPeerTimeoutSecs(pp, 3);
376 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
378 rx_rto_setPeerTimeoutSecs(pp, 2);
379 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
380 mtu = ntohl(afs_cb_interface.mtu[i]);
381 /* Diminish the packet size to one based on the MTU given by
383 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
384 rxmtu = mtu - RX_IPUDP_SIZE;
385 if (rxmtu < pp->ifMTU)
389 # else /* AFS_USERSPACE_IP_ADDR */
392 RX_NET_EPOCH_ENTER();
394 # if !defined(AFS_SGI_ENV)
395 if (numMyNetAddrs == 0)
396 (void)rxi_GetIFInfo();
399 ifn = rxi_FindIfnet(pp->host, NULL);
401 rx_rto_setPeerTimeoutSecs(pp, 2);
402 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
403 # ifdef IFF_POINTOPOINT
404 if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
405 /* wish we knew the bit rate and the chunk size, sigh. */
406 rx_rto_setPeerTimeoutSecs(pp, 4);
407 pp->ifMTU = RX_PP_PACKET_SIZE;
409 # endif /* IFF_POINTOPOINT */
410 /* Diminish the packet size to one based on the MTU given by
412 if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
413 rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
414 if (rxmtu < pp->ifMTU)
417 } else { /* couldn't find the interface, so assume the worst */
418 rx_rto_setPeerTimeoutSecs(pp, 3);
419 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
424 # endif /* else AFS_USERSPACE_IP_ADDR */
425 #else /* AFS_SUN5_ENV */
428 mtu = rxi_FindIfMTU(pp->host);
431 rx_rto_setPeerTimeoutSecs(pp, 3);
432 pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
434 rx_rto_setPeerTimeoutSecs(pp, 2);
435 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
437 /* Diminish the packet size to one based on the MTU given by
439 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
440 rxmtu = mtu - RX_IPUDP_SIZE;
441 if (rxmtu < pp->ifMTU)
445 #endif /* AFS_SUN5_ENV */
446 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
447 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
448 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
450 MIN(rxi_nDgramPackets,
451 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
452 pp->maxDgramPackets = 1;
454 /* Initialize slow start parameters */
455 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
457 pp->nDgramPackets = 1;
462 /* The following code is common to several system types, but not all. The
463 * separate ones are found in the system specific subdirectories.
467 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
468 /* Routine called during the afsd "-shutdown" process to put things back to
471 static struct protosw parent_proto; /* udp proto switch */
474 shutdown_rxkernel(void)
476 struct protosw *tpro, *last;
477 last = inetdomain.dom_protoswNPROTOSW;
478 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
479 if (tpro->pr_protocol == IPPROTO_UDP) {
480 /* restore original udp protocol switch */
481 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
482 memset((void *)&parent_proto, 0, sizeof(parent_proto));
487 dpf(("shutdown_rxkernel: no udp proto\n"));
489 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
491 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV)
492 /* Determine what the network interfaces are for this machine. */
494 # ifdef AFS_USERSPACE_IP_ADDR
498 int i, j, different = 0, num = ADDRSPERSITE;
501 afs_uint32 addrs[ADDRSPERSITE];
502 int mtus[ADDRSPERSITE];
504 memset((void *)addrs, 0, sizeof(addrs));
505 memset((void *)mtus, 0, sizeof(mtus));
507 if (afs_cb_interface.numberOfInterfaces < num)
508 num = afs_cb_interface.numberOfInterfaces;
509 for (i = 0; i < num; i++) {
510 if (!afs_cb_interface.mtu[i])
511 afs_cb_interface.mtu[i] = htonl(1500);
512 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
513 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
514 if (myNetAddrs[i] != ifinaddr)
518 rxmtu = rxi_AdjustIfMTU(rxmtu);
520 rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
521 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
522 addrs[i++] = ifinaddr;
523 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
524 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
525 rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
529 rx_maxJumboRecvSize =
530 RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
531 ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
532 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
535 for (j = 0; j < i; j++) {
536 myNetMTUs[j] = mtus[j];
537 myNetAddrs[j] = addrs[j];
544 /* Returns the afs_cb_interface inxex which best matches address.
545 * If none is found, we return -1.
548 rxi_Findcbi(afs_uint32 addr)
551 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
552 afs_int32 rvalue = -1;
555 if (numMyNetAddrs == 0)
556 (void)rxi_GetcbiInfo();
558 myAddr = ntohl(addr);
560 if (IN_CLASSA(myAddr))
561 netMask = IN_CLASSA_NET;
562 else if (IN_CLASSB(myAddr))
563 netMask = IN_CLASSB_NET;
564 else if (IN_CLASSC(myAddr))
565 netMask = IN_CLASSC_NET;
569 for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
570 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
571 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
572 if ((myAddr & netMask) == (thisAddr & netMask)) {
573 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
574 if (myAddr == thisAddr) {
579 if (match_value < 3) {
584 if (match_value < 2) {
595 # else /* AFS_USERSPACE_IP_ADDR */
597 # if !defined(AFS_AIX41_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
598 # define IFADDR2SA(f) (&((f)->ifa_addr))
600 # define IFADDR2SA(f) ((f)->ifa_addr)
610 afs_uint32 addrs[ADDRSPERSITE];
611 int mtus[ADDRSPERSITE];
613 # if defined(AFS_DARWIN80_ENV)
619 struct sockaddr sout;
620 struct sockaddr_in *sin;
623 rx_ifaddr_t ifad; /* ifnet points to a if_addrlist of ifaddrs */
627 memset(addrs, 0, sizeof(addrs));
628 memset(mtus, 0, sizeof(mtus));
630 # if defined(AFS_DARWIN80_ENV)
631 if (!ifnet_list_get(AF_INET, &ifns, &count)) {
632 for (m = 0; m < count; m++) {
633 if (!ifnet_get_address_list(ifns[m], &ifads)) {
634 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
635 if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
636 sin = (struct sockaddr_in *)&sout;
637 rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
638 ifinaddr = ntohl(sin->sin_addr.s_addr);
639 if (myNetAddrs[i] != ifinaddr) {
643 rxmtu = rxi_AdjustIfMTU(rxmtu);
645 rxmtu * rxi_nRecvFrags +
646 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
647 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
648 addrs[i++] = ifinaddr;
649 if (!rx_IsLoopbackAddr(ifinaddr) &&
650 (maxmtu > rx_maxReceiveSize)) {
652 MIN(RX_MAX_PACKET_SIZE, maxmtu);
654 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
659 ifnet_free_address_list(ifads);
662 ifnet_list_free(ifns);
664 # else /* AFS_DARWIN80_ENV */
665 # if defined(AFS_DARWIN_ENV)
666 TAILQ_FOREACH(ifn, &ifnet, if_link) {
667 if (i >= ADDRSPERSITE)
669 # elif defined(AFS_FBSD_ENV)
670 CURVNET_SET(rx_socket->so_vnet);
672 AFS_FBSD_NET_FOREACH(ifn, &V_ifnet, if_link) {
673 if (i >= ADDRSPERSITE)
675 # elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
676 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
677 ifn = ifn->if_list.tqe_next) {
679 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
681 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
682 # if defined(AFS_DARWIN_ENV)
683 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
684 if (i >= ADDRSPERSITE)
686 # elif defined(AFS_FBSD_ENV)
688 AFS_FBSD_NET_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
689 if (i >= ADDRSPERSITE)
691 # elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
692 for (ifad = ifn->if_addrlist.tqh_first;
693 ifad != NULL && i < ADDRSPERSITE;
694 ifad = ifad->ifa_list.tqe_next) {
696 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
697 ifad = ifad->ifa_next) {
699 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
701 ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
703 if (myNetAddrs[i] != ifinaddr) {
707 rxmtu = rxi_AdjustIfMTU(rxmtu);
709 rxmtu * rxi_nRecvFrags +
710 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
711 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
712 addrs[i++] = ifinaddr;
713 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
714 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
716 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
721 if_addr_runlock(ifn);
724 # endif /* !AFS_DARWIN80_ENV */
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];
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;
757 CURVNET_SET(rx_socket->so_vnet);
760 s.sin_family = AF_INET;
761 s.sin_addr.s_addr = addr;
762 ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
765 rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
766 *maskp = sr.sin_addr.s_addr;
769 ret = (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
778 # else /* DARWIN || XBSD */
780 /* Returns ifnet which best matches address */
782 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
785 extern struct in_ifaddr *in_ifaddr;
786 struct in_ifaddr *ifa, *ifad = NULL;
790 for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
791 if ((addr & ifa->ia_netmask) == ifa->ia_net) {
792 if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
793 if (IA_SIN(ifa)->sin_addr.s_addr == addr) { /* ie, ME!!! */
798 if (match_value < 3) {
803 if (match_value < 2) {
808 } /* if net matches */
809 } /* for all in_ifaddrs */
813 *maskp = ifad->ia_subnetmask;
814 return (ifad ? ifad->ia_ifp : NULL);
816 # endif /* else DARWIN || XBSD */
817 # endif /* else AFS_USERSPACE_IP_ADDR */
818 #endif /* !SUN5 && !SGI */
821 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
822 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
823 * system specific subdirectories for all systems. But for the moment,
824 * most of it is simple to follow common code.
826 #if !defined(UKERNEL)
827 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX_ENV) && !defined(AFS_SOCKPROXY_ENV)
828 /* rxk_NewSocket creates a new socket on the specified port. The port is
829 * in network byte order.
832 rxk_NewSocketHost(afs_uint32 ahost, short aport)
835 # ifdef AFS_DARWIN80_ENV
838 struct socket *newSocket;
840 # if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
843 struct sockaddr_in myaddr;
844 # ifdef AFS_HPUX110_ENV
845 /* prototype copied from kernel source file streams/str_proto.h */
846 extern MBLKP allocb_wait(int, int);
848 int addrsize = sizeof(struct sockaddr_in);
850 extern struct fileops socketops;
856 AFS_STATCNT(osi_NewSocket);
857 # if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
858 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
862 # if defined(AFS_HPUX102_ENV)
863 # if defined(AFS_HPUX110_ENV)
864 /* we need a file associated with the socket so sosend in NetSend
866 /* blocking socket */
867 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
871 fp->f_flag = FREAD | FWRITE;
872 fp->f_type = DTYPE_SOCKET;
873 fp->f_ops = &socketops;
875 fp->f_data = (void *)newSocket;
876 newSocket->so_fp = (void *)fp;
878 # else /* AFS_HPUX110_ENV */
879 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
880 # endif /* else AFS_HPUX110_ENV */
881 # elif defined(AFS_SGI_ENV) || defined(AFS_OBSD_ENV)
882 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
883 # elif defined(AFS_FBSD_ENV)
884 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
885 afs_osi_credp, curthread);
886 # elif defined(AFS_DARWIN80_ENV)
887 # ifdef RXK_LISTENER_ENV
888 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
890 code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, rx_upcall, NULL, &newSocket);
892 # elif defined(AFS_NBSD50_ENV)
893 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
894 # elif defined(AFS_NBSD40_ENV)
895 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
897 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
898 # endif /* AFS_HPUX102_ENV */
902 memset(&myaddr, 0, sizeof myaddr);
903 myaddr.sin_family = AF_INET;
904 myaddr.sin_port = aport;
905 myaddr.sin_addr.s_addr = ahost;
906 # ifdef STRUCT_SOCKADDR_HAS_SA_LEN
907 myaddr.sin_len = sizeof(myaddr);
910 # ifdef AFS_HPUX110_ENV
911 bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
916 memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
918 bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
919 code = sobind(newSocket, bindnam, addrsize);
922 # if !defined(AFS_HPUX1122_ENV)
929 # else /* AFS_HPUX110_ENV */
930 # if defined(AFS_DARWIN80_ENV)
935 code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
936 &buflen, sizeof(buflen));
937 code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
938 &buflen, sizeof(buflen));
942 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
947 # if defined(AFS_NBSD_ENV)
950 code = soreserve(newSocket, 50000, 50000);
952 code = soreserve(newSocket, 32766, 32766);
954 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
956 # if defined(AFS_NBSD_ENV)
960 # if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
961 # if defined(AFS_FBSD_ENV)
962 code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
964 code = sobind(newSocket, (struct sockaddr *)&myaddr);
967 dpf(("sobind fails (%d)\n", (int)code));
971 # else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
972 nam = m_get(M_WAIT, MT_SONAME);
974 # if defined(KERNEL_HAVE_UERROR)
979 nam->m_len = sizeof(myaddr);
980 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
981 # if defined(AFS_SGI_ENV)
982 BHV_PDATA(&bhv) = (void *)newSocket;
983 code = sobind(&bhv, nam);
985 # elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
986 code = sobind(newSocket, nam, osi_curproc());
988 code = sobind(newSocket, nam);
991 dpf(("sobind fails (%d)\n", (int)code));
998 # endif /* else defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
999 # endif /* else AFS_HPUX110_ENV */
1002 # if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1003 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1005 return (osi_socket *)newSocket;
1009 # if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1010 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1012 return (osi_socket *)0;
1016 rxk_NewSocket(short aport)
1018 return rxk_NewSocketHost(0, aport);
1021 /* free socket allocated by rxk_NewSocket */
1023 rxk_FreeSocket(struct socket *asocket)
1025 AFS_STATCNT(osi_FreeSocket);
1026 # if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1027 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1029 # ifdef AFS_HPUX110_ENV
1030 if (asocket->so_fp) {
1031 struct file *fp = asocket->so_fp;
1032 # if !defined(AFS_HPUX1123_ENV)
1033 /* 11.23 still has falloc, but not FPENTRYFREE !
1034 * so for now if we shutdown, we will waist a file
1037 asocket->so_fp = NULL;
1040 # endif /* AFS_HPUX110_ENV */
1042 # if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1043 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1047 # endif /* !AFS_SUN5_ENV && !AFS_LINUX_ENV && !AFS_SOCKPROXY_ENV */
1049 # if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV) || defined(RXK_UPCALL_ENV)
1050 # ifdef RXK_TIMEDSLEEP_ENV
1051 /* Shutting down should wake us up, as should an earlier event. */
1053 rxi_ReScheduleEvents(void)
1055 /* needed to allow startup */
1056 int glock = ISAFS_GLOCK();
1059 osi_rxWakeup(&afs_termState);
1065 * Run RX event daemon every second (5 times faster than rest of systems)
1068 afs_rxevent_daemon(void)
1074 # ifdef RX_ENABLE_LOCKS
1076 # endif /* RX_ENABLE_LOCKS */
1078 rxevent_RaiseEvents(&temp);
1080 # ifdef RX_ENABLE_LOCKS
1082 # endif /* RX_ENABLE_LOCKS */
1083 # ifdef RX_KERNEL_TRACE
1084 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1085 "before afs_osi_Wait()");
1087 # ifdef RXK_TIMEDSLEEP_ENV
1088 afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1089 (temp.usec / 1000))), 0);
1091 afs_osi_Wait(500, NULL, 0);
1093 # ifdef RX_KERNEL_TRACE
1094 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1095 "after afs_osi_Wait()");
1097 if (afs_termState == AFSOP_STOP_RXEVENT) {
1098 # ifdef RXK_LISTENER_ENV
1099 afs_termState = AFSOP_STOP_RXK_LISTENER;
1100 # elif defined(AFS_SOCKPROXY_ENV)
1101 afs_termState = AFSOP_STOP_SOCKPROXY;
1102 # elif defined(AFS_SUN510_ENV) || defined(RXK_UPCALL_ENV)
1103 afs_termState = AFSOP_STOP_NETIF;
1105 afs_termState = AFSOP_STOP_COMPLETE;
1107 osi_rxWakeup(&afs_termState);
1112 # endif /* RXK_LISTENER_ENV || AFS_SUN5_ENV || RXK_UPCALL_ENV */
1114 # ifdef RXK_LISTENER_ENV
1116 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1118 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1121 struct sockaddr_in from;
1125 afs_int32 savelen; /* was using rlen but had aliasing problems */
1126 rx_computelen(p, tlen);
1127 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
1129 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
1130 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
1131 * it once in order to avoid races. */
1134 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1142 /* add some padding to the last iovec, it's just to make sure that the
1143 * read doesn't return more data than we expect, and is done to get around
1144 * our problems caused by the lack of a length field in the rx header. */
1145 savelen = p->wirevec[p->niovecs - 1].iov_len;
1146 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1148 nbytes = tlen + sizeof(afs_int32);
1149 # ifdef RX_KERNEL_TRACE
1150 if (ICL_SETACTIVE(afs_iclSetp)) {
1152 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1153 "before osi_NetRecive()");
1157 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1159 # ifdef RX_KERNEL_TRACE
1160 if (ICL_SETACTIVE(afs_iclSetp)) {
1162 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1163 "after osi_NetRecive()");
1167 /* restore the vec to its correct state */
1168 p->wirevec[p->niovecs - 1].iov_len = savelen;
1171 p->length = nbytes - RX_HEADER_SIZE;;
1172 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
1174 if (rx_stats_active) {
1175 MUTEX_ENTER(&rx_stats_mutex);
1176 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1177 rx_stats.bogusHost = from.sin_addr.s_addr;
1178 MUTEX_EXIT(&rx_stats_mutex);
1180 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1181 from.sin_addr.s_addr, from.sin_port, nbytes));
1185 /* Extract packet header. */
1186 rxi_DecodePacketHeader(p);
1188 *host = from.sin_addr.s_addr;
1189 *port = from.sin_port;
1190 if (p->header.type > 0 && p->header.type <= RX_N_PACKET_TYPES) {
1191 if (rx_stats_active) {
1192 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1196 # ifdef RX_TRIMDATABUFS
1197 /* Free any empty packet buffers at the end of this packet */
1198 rxi_TrimDataBufs(p, 1);
1208 * Listen for packets on socket. This thread is typically started after
1209 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1210 * the start state is set before proceeding.
1212 * Note that this thread is outside the AFS global lock for much of
1215 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1216 * we want here. So we need to either block all signals (including SIGKILL
1217 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1218 * OS's socket receive routine returns as a result of a signal.
1220 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1221 # ifdef AFS_LINUX_ENV
1222 struct task_struct *rxk_ListenerTask;
1228 struct rx_packet *rxp = NULL;
1232 # ifdef AFS_LINUX_ENV
1233 rxk_ListenerPid = current->pid;
1234 rxk_ListenerTask = current;
1235 allow_signal(SIGKILL); /* Allowed, but blocked until shutdown */
1237 # ifdef AFS_SUN5_ENV
1238 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1239 # endif /* AFS_SUN5_ENV */
1240 # ifdef AFS_XBSD_ENV
1241 rxk_ListenerPid = curproc->p_pid;
1242 # endif /* AFS_FBSD_ENV */
1243 # ifdef AFS_DARWIN80_ENV
1244 rxk_ListenerPid = proc_selfpid();
1245 # elif defined(AFS_DARWIN_ENV)
1246 rxk_ListenerPid = current_proc()->p_pid;
1248 # ifdef RX_ENABLE_LOCKS
1250 # endif /* RX_ENABLE_LOCKS */
1251 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1252 /* See if a check for additional packets was issued */
1256 rxi_RestoreDataBufs(rxp);
1258 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1260 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1262 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1263 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1267 # ifdef RX_ENABLE_LOCKS
1269 # endif /* RX_ENABLE_LOCKS */
1270 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1271 # ifdef AFS_SUN510_ENV
1272 afs_termState = AFSOP_STOP_NETIF;
1274 afs_termState = AFSOP_STOP_COMPLETE;
1276 osi_rxWakeup(&afs_termState);
1278 rxk_ListenerPid = 0;
1279 # ifdef AFS_LINUX_ENV
1280 rxk_ListenerTask = 0;
1281 osi_rxWakeup(&rxk_ListenerTask);
1283 # if defined(AFS_SUN5_ENV) || defined(AFS_FBSD_ENV)
1284 osi_rxWakeup(&rxk_ListenerPid);
1288 # if !defined(AFS_LINUX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1289 /* The manner of stopping the rx listener thread may vary. Most unix's should
1290 * be able to call soclose.
1293 osi_StopListener(void)
1298 # endif /* RXK_LISTENER_ENV */
1299 #endif /* !UKERNEL */
1302 osi_Msg(const char *fmt, ...)
1306 #if defined(AFS_LINUX_ENV)
1314 #if !defined(AFS_LINUX_ENV)
1316 # if defined(AFS_AIX_ENV)
1317 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1319 osi_Panic(char *msg, ...)
1324 msg = "Unknown AFS panic";
1326 * we should probably use the errsave facility here. it is not
1330 printf(msg, a1, a2, a3);
1332 # elif defined(AFS_SGI_ENV)
1335 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1337 cmn_err(CE_PANIC, "Unknown AFS panic");
1340 icmn_err(CE_PANIC, msg, ap);
1343 # elif defined(AFS_DARWIN80_ENV) || defined(AFS_LINUX_ENV) || defined(AFS_FBSD_ENV) || defined(UKERNEL)
1347 msg = "Unknown AFS panic";
1350 vsnprintf(buf, sizeof(buf), msg, ap);
1354 # else /* DARWIN80 || LINUX || FBSD || UKERNEL */
1357 msg = "Unknown AFS panic";
1362 # ifdef AFS_LINUX_ENV
1367 # endif /* else DARWIN80 || LINUX || FBSD || UKERNEL */
1370 #endif /* !AFS_LINUX_ENV */