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"
19 #include "rx/rx_kcommon.h"
21 #ifdef AFS_HPUX110_ENV
27 #ifdef AFS_DARWIN60_ENV
28 struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct ifaddr **pifad);
30 struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct in_ifaddr **pifad);
33 #ifndef RXK_LISTENER_ENV
34 int (*rxk_PacketArrivalProc)(register struct rx_packet *ahandle,
35 register struct sockaddr_in *afrom, char *arock,
36 afs_int32 asize); /* set to packet allocation procedure */
37 int (*rxk_GetPacketProc)(char **ahandle, int asize);
40 extern struct interfaceAddr afs_cb_interface;
42 rxk_ports_t rxk_ports;
43 rxk_portRocks_t rxk_portRocks;
47 /* add a port to the monitored list, port # is in network order */
48 static int rxk_AddPort(u_short aport, char * arock)
51 unsigned short *tsp, ts;
54 zslot = -1; /* look for an empty slot simultaneously */
55 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
56 if (((ts = *tsp) == 0) && (zslot == -1))
62 /* otherwise allocate a new port slot */
63 if (zslot < 0) return E2BIG; /* all full */
64 rxk_ports[zslot] = aport;
65 rxk_portRocks[zslot] = arock;
69 /* remove as port from the monitored list, port # is in network order */
70 int rxk_DelPort(u_short aport)
73 register unsigned short *tsp;
75 for(i=0,tsp=rxk_ports;i<MAXRXPORTS;i++,tsp++) {
77 /* found it, adjust ref count and free the port reference if all gone */
82 /* otherwise port not found */
86 void rxk_shutdownPorts(void)
89 for (i=0; i<MAXRXPORTS;i++) {
92 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
93 soclose((struct socket *)rxk_portRocks[i]);
95 rxk_portRocks[i] = NULL;
100 osi_socket rxi_GetUDPSocket(u_short port)
102 struct osi_socket *sockp;
103 sockp = (struct osi_socket *) rxk_NewSocket(port);
104 if (sockp == (struct osi_socket *) 0) return OSI_NULLSOCKET;
105 rxk_AddPort(port, (char *) sockp);
106 return (osi_socket)sockp;
110 void osi_Panic(msg, a1, a2, a3)
114 msg = "Unknown AFS panic";
116 printf(msg, a1, a2, a3);
117 #ifdef AFS_LINUX20_ENV
118 *((char*)0xffffffff) = 42;
125 * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
126 * unsigned long value into the given buffer. Returns 0 on success,
127 * and a value less than 0 on failure. The contents of the buffer is
128 * defined only on success.
131 int osi_utoa(char *buf, size_t len, unsigned long val)
133 long k; /* index of first byte of string value */
135 /* we definitely need room for at least one digit and NUL */
141 /* compute the string form from the high end of the buffer */
144 for (k = len - 2; k >= 0; k--) {
145 buf[k] = val % 10 + '0';
152 /* did we finish converting val to string form? */
158 /* this should never happen */
164 /* this should never happen */
170 /* if necessary, relocate string to beginning of buf[] */
175 * We need to achieve the effect of calling
177 * memmove(buf, &buf[k], len - k);
179 * However, since memmove() is not available in all
180 * kernels, we explicitly do an appropriate copy.
186 while((*dst++ = *src++) != '\0')
194 * osi_AssertFailK() -- used by the osi_Assert() macro.
196 * It essentially does
198 * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
200 * Since the kernel version of osi_Panic() only passes its first
201 * argument to the native panic(), we construct a single string and hand
202 * that to osi_Panic().
204 void osi_AssertFailK(const char *expr, const char *file, int line)
206 static const char msg0[] = "assertion failed: ";
207 static const char msg1[] = ", file: ";
208 static const char msg2[] = ", line: ";
209 static const char msg3[] = "\n";
212 * These buffers add up to 1K, which is a pleasantly nice round
213 * value, but probably not vital.
218 /* check line number conversion */
220 if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
221 osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
226 #define ADDBUF(BUF, STR) \
227 if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) { \
228 strcat(BUF, (char *)(STR)); \
237 ADDBUF(buf, linebuf);
246 /* This is the server process request loop. Kernel server
247 * processes never become listener threads */
248 void rx_ServerProc(void)
252 rxi_MorePackets(rx_maxReceiveWindow+2); /* alloc more packets */
253 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
254 /* threadID is used for making decisions in GetCall. Get it by bumping
255 * number of threads handling incoming calls */
256 threadID = rxi_availProcs++;
258 #ifdef RX_ENABLE_LOCKS
260 #endif /* RX_ENABLE_LOCKS */
261 rxi_ServerProc(threadID, NULL, NULL);
262 #ifdef RX_ENABLE_LOCKS
264 #endif /* RX_ENABLE_LOCKS */
266 #endif /* !UKERNEL */
268 #ifndef RXK_LISTENER_ENV
269 /* asize includes the Rx header */
270 static int MyPacketProc(char **ahandle, int asize)
272 register struct rx_packet *tp;
274 /* If this is larger than we expected, increase rx_maxReceiveDataSize */
275 /* If we can't scrounge enough cbufs, then we have to drop the packet,
276 * but we should set a flag so we magic up some more at our leisure.
279 if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
280 tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
281 if (tp && (tp->length + RX_HEADER_SIZE) < asize ) {
282 if (0 < rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
283 RX_PACKET_CLASS_RECV_CBUF)) {
286 MUTEX_ENTER(&rx_stats_mutex);
287 rx_stats.noPacketBuffersOnRead++;
288 MUTEX_EXIT(&rx_stats_mutex);
293 * XXX if packet is too long for our buffer,
294 * should do this at a higher layer and let other
295 * end know we're losing.
297 MUTEX_ENTER(&rx_stats_mutex);
298 rx_stats.bogusPacketOnRead++;
299 MUTEX_EXIT(&rx_stats_mutex);
300 /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
301 printf("rx: packet dropped: bad ulen=%d\n", asize);
306 /* otherwise we have a packet, set appropriate values */
307 *ahandle = (char *) tp;
311 static int MyArrivalProc(register struct rx_packet *ahandle,
312 register struct sockaddr_in *afrom, char *arock,
315 /* handle basic rx packet */
316 ahandle->length = asize - RX_HEADER_SIZE;
317 rxi_DecodePacketHeader(ahandle);
318 ahandle = rxi_ReceivePacket(ahandle, (struct socket *) arock,
319 afrom->sin_addr.s_addr, afrom->sin_port,
322 /* free the packet if it has been returned */
323 if (ahandle) rxi_FreePacket(ahandle);
326 #endif /* !RXK_LISTENER_ENV */
328 void rxi_StartListener(void)
330 /* if kernel, give name of appropriate procedures */
331 #ifndef RXK_LISTENER_ENV
332 rxk_GetPacketProc = MyPacketProc;
333 rxk_PacketArrivalProc = MyArrivalProc;
338 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
339 to get interesting information. */
340 void rxi_InitPeerParams(register struct rx_peer *pp)
347 #ifdef AFS_USERSPACE_IP_ADDR
348 i = rxi_Findcbi(pp->host);
351 /* pp->timeout.usec = 0; */
352 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
355 /* pp->timeout.usec = 0; */
356 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
359 mtu = ntohl(afs_cb_interface.mtu[i]);
360 /* Diminish the packet size to one based on the MTU given by
362 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
363 rxmtu = mtu - RX_IPUDP_SIZE;
364 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
367 else { /* couldn't find the interface, so assume the worst */
368 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
370 #else /* AFS_USERSPACE_IP_ADDR */
371 #ifdef AFS_DARWIN60_ENV
372 struct ifaddr *ifad = (struct ifaddr *) 0;
374 struct in_ifaddr *ifad = (struct in_ifaddr *) 0;
378 /* At some time we need to iterate through rxi_FindIfnet() to find the
381 ifn = rxi_FindIfnet(pp->host, &ifad);
382 if (ifn == NULL) { /* not local */
384 /* pp->timeout.usec = 0; */
385 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
388 /* pp->timeout.usec = 0; */
389 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
392 #ifdef IFF_POINTOPOINT
393 if (ifn->if_flags & IFF_POINTOPOINT) {
394 /* wish we knew the bit rate and the chunk size, sigh. */
396 pp->ifMTU = RX_PP_PACKET_SIZE;
398 #endif /* IFF_POINTOPOINT */
399 /* Diminish the packet size to one based on the MTU given by
401 if (ifn->if_mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
402 rxmtu = ifn->if_mtu - RX_IPUDP_SIZE;
403 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
406 else { /* couldn't find the interface, so assume the worst */
407 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
409 #endif/* else AFS_USERSPACE_IP_ADDR */
410 #else /* AFS_SUN5_ENV */
411 mtu = rxi_FindIfMTU(pp->host);
415 /* pp->timeout.usec = 0; */
416 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
419 /* pp->timeout.usec = 0; */
420 pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
424 /* Diminish the packet size to one based on the MTU given by
426 if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
427 rxmtu = mtu - RX_IPUDP_SIZE;
428 if (rxmtu < pp->ifMTU) pp->ifMTU = rxmtu;
430 } else { /* couldn't find the interface, so assume the worst */
431 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
433 #endif /* AFS_SUN5_ENV */
434 #else /* ADAPT_MTU */
435 pp->rateFlag = 2; /* start timing after two full packets */
437 pp->ifMTU = OLD_MAX_PACKET_SIZE;
438 #endif /* else ADAPT_MTU */
439 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
440 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
441 pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
442 pp->ifDgramPackets = MIN(rxi_nDgramPackets,
443 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
444 pp->maxDgramPackets = 1;
446 /* Initialize slow start parameters */
447 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
449 pp->nDgramPackets = 1;
454 /* The following code is common to several system types, but not all. The
455 * separate ones are found in the system specific subdirectories.
459 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
460 /* Routine called during the afsd "-shutdown" process to put things back to
463 static struct protosw parent_proto; /* udp proto switch */
465 void shutdown_rxkernel(void)
467 register struct protosw *tpro, *last;
468 last = inetdomain.dom_protoswNPROTOSW;
469 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
470 if (tpro->pr_protocol == IPPROTO_UDP) {
471 /* restore original udp protocol switch */
472 memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
473 memset((void *)&parent_proto, 0, sizeof(parent_proto));
478 printf("shutdown_rxkernel: no udp proto");
480 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
482 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
483 /* Determine what the network interfaces are for this machine. */
485 #define ADDRSPERSITE 16
486 static afs_uint32 myNetAddrs[ADDRSPERSITE];
487 static int myNetMTUs[ADDRSPERSITE];
488 static int numMyNetAddrs = 0;
490 #ifdef AFS_USERSPACE_IP_ADDR
491 int rxi_GetcbiInfo(void)
493 int i, j, different = 0;
496 afs_uint32 addrs[ADDRSPERSITE];
497 int mtus[ADDRSPERSITE];
499 memset((void *)addrs, 0, sizeof(addrs));
500 memset((void *)mtus, 0, sizeof(mtus));
502 for (i=0; i<afs_cb_interface.numberOfInterfaces; i++) {
503 rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
504 ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
505 if (myNetAddrs[i] != ifinaddr) different++;
508 rxmtu = rxi_AdjustIfMTU(rxmtu);
509 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
510 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
511 addrs[i++] = ifinaddr;
512 if ( ( ifinaddr != 0x7f000001 ) && (maxmtu > rx_maxReceiveSize) ) {
513 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
514 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
518 rx_maxJumboRecvSize = RX_HEADER_SIZE +
519 ( rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
520 ((rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE);
521 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
524 for (j=0; j<i; j++) {
525 myNetMTUs[j] = mtus[j];
526 myNetAddrs[j] = addrs[j];
533 /* Returns the afs_cb_interface inxex which best matches address.
534 * If none is found, we return -1.
536 afs_int32 rxi_Findcbi(afs_uint32 addr)
539 afs_uint32 myAddr, thisAddr, netMask, subnetMask;
540 afs_int32 rvalue = -1;
543 if (numMyNetAddrs == 0)
544 (void) rxi_GetcbiInfo();
546 myAddr = ntohl(addr);
548 if ( IN_CLASSA(myAddr) ) netMask = IN_CLASSA_NET;
549 else if ( IN_CLASSB(myAddr) ) netMask = IN_CLASSB_NET;
550 else if ( IN_CLASSC(myAddr) ) netMask = IN_CLASSC_NET;
553 for (j=0; j<afs_cb_interface.numberOfInterfaces; j++) {
554 thisAddr = ntohl(afs_cb_interface.addr_in[j]);
555 subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
556 if ((myAddr & netMask) == (thisAddr & netMask)) {
557 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
558 if (myAddr == thisAddr) {
563 if (match_value < 3) {
568 if (match_value < 2) {
579 #else /* AFS_USERSPACE_IP_ADDR */
581 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
582 #define IFADDR2SA(f) (&((f)->ifa_addr))
583 #else /* AFS_AIX41_ENV */
584 #define IFADDR2SA(f) ((f)->ifa_addr)
587 int rxi_GetIFInfo(void)
592 register struct ifnet *ifn;
593 register int rxmtu, maxmtu;
594 afs_uint32 addrs[ADDRSPERSITE];
595 int mtus[ADDRSPERSITE];
596 struct ifaddr *ifad; /* ifnet points to a if_addrlist of ifaddrs */
599 memset(addrs, 0, sizeof(addrs));
600 memset(mtus, 0, sizeof(mtus));
602 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
603 TAILQ_FOREACH(ifn, &ifnet, if_link) {
604 if (i >= ADDRSPERSITE) break;
605 #elif defined(AFS_OBSD_ENV)
606 for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL; ifn = ifn->if_list.tqe_next) {
608 for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
610 rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
611 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
612 TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
613 if (i >= ADDRSPERSITE) break;
614 #elif defined(AFS_OBSD_ENV)
615 for (ifad = ifn->if_addrlist.tqh_first; ifad != NULL && i < ADDRSPERSITE;
616 ifad = ifad->ifa_list.tqe_next) {
618 for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
619 ifad = ifad->ifa_next) {
621 if (IFADDR2SA(ifad)->sa_family == AF_INET) {
622 ifinaddr = ntohl(((struct sockaddr_in *) IFADDR2SA(ifad))->sin_addr.s_addr);
623 if (myNetAddrs[i] != ifinaddr) {
627 rxmtu = rxi_AdjustIfMTU(rxmtu);
628 maxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags-1) * UDP_HDR_SIZE);
629 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
630 addrs[i++] = ifinaddr;
631 if ( ( ifinaddr != 0x7f000001 ) &&
632 (maxmtu > rx_maxReceiveSize) ) {
633 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxmtu);
634 rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
640 rx_maxJumboRecvSize = RX_HEADER_SIZE
641 + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE
642 + (rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE;
643 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
647 for (j=0; j< i; j++) {
648 myNetMTUs[j] = mtus[j];
649 myNetAddrs[j] = addrs[j];
654 #ifdef AFS_DARWIN60_ENV
655 /* Returns ifnet which best matches address */
657 rxi_FindIfnet(addr, pifad)
659 struct ifaddr **pifad;
661 struct sockaddr_in s;
663 if (numMyNetAddrs == 0)
664 (void) rxi_GetIFInfo();
666 s.sin_family=AF_INET;
667 s.sin_addr.s_addr=addr;
668 *pifad=ifa_ifwithnet((struct sockaddr *)&s);
670 return (*pifad ? (*pifad)->ifa_ifp : NULL );
673 /* Returns ifnet which best matches address */
674 struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct in_ifaddr **pifad)
679 extern struct in_ifaddr *in_ifaddr;
681 struct in_ifaddr *ifa;
682 struct sockaddr_in *sin;
684 if (numMyNetAddrs == 0)
685 (void) rxi_GetIFInfo();
690 ppaddr = ntohl(addr);
693 /* if we're given an address, skip everything until we find it */
695 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
696 *pifad = TAILQ_FIRST(&in_ifaddrhead);
697 #elif defined(AFS_OBSD_ENV)
698 *pifad = in_ifaddr.tqh_first;
703 if (((ppaddr & (*pifad)->ia_subnetmask) == (*pifad)->ia_subnet))
704 match_value = 2; /* don't find matching nets, just subnets */
705 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
706 *pifad = TAILQ_NEXT(*pifad, ia_link);
707 #elif defined(AFS_OBSD_ENV)
708 *pifad = (*pifad)->ia_list.tqe_next;
710 *pifad = (*pifad)->ia_next;
714 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
715 for (ifa = *pifad; ifa; ifa = TAILQ_NEXT(ifa, ia_link) ) {
716 #elif defined(AFS_OBSD_ENV)
717 for (ifa = *pifad; ifa; ifa = ifa->ia_list.tqe_next) {
719 for (ifa = *pifad; ifa; ifa = ifa->ia_next ) {
721 if ((ppaddr & ifa->ia_netmask) == ifa->ia_net) {
722 if ((ppaddr & ifa->ia_subnetmask) == ifa->ia_subnet) {
724 if ( sin->sin_addr.s_addr == ppaddr) { /* ie, ME!!! */
729 if (match_value < 3) {
735 if (match_value < 2) {
740 } /* if net matches */
741 } /* for all in_ifaddrs */
744 return (*pifad ? (*pifad)->ia_ifp : NULL );
747 #endif /* else AFS_USERSPACE_IP_ADDR */
748 #endif /* !SUN5 && !SGI62 */
751 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
752 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
753 * system specific subdirectories for all systems. But for the moment,
754 * most of it is simple to follow common code.
756 #if !defined(UKERNEL)
757 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
758 /* rxk_NewSocket creates a new socket on the specified port. The port is
759 * in network byte order.
761 struct osi_socket *rxk_NewSocket(short aport)
763 register afs_int32 code;
764 struct socket *newSocket;
766 struct sockaddr_in myaddr;
767 #ifdef AFS_HPUX110_ENV
768 /* prototype copied from kernel source file streams/str_proto.h */
769 extern MBLKP allocb_wait(int, int);
771 int addrsize = sizeof(struct sockaddr_in);
777 AFS_STATCNT(osi_NewSocket);
778 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
779 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
781 #if defined(AFS_HPUX102_ENV)
782 #if defined(AFS_HPUX110_ENV)
783 /* blocking socket */
784 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
785 #else /* AFS_HPUX110_ENV */
786 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
787 #endif /* else AFS_HPUX110_ENV */
788 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
789 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
790 #elif defined(AFS_FBSD_ENV)
791 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
793 code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
794 #endif /* AFS_HPUX102_ENV */
797 myaddr.sin_family = AF_INET;
798 myaddr.sin_port = aport;
799 myaddr.sin_addr.s_addr = 0;
800 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
801 myaddr.sin_len = sizeof(myaddr);
804 #ifdef AFS_HPUX110_ENV
805 bindnam = allocb_wait((addrsize+SO_MSGOFFSET+1), BPRI_MED);
810 memcpy((caddr_t)bindnam->b_rptr+SO_MSGOFFSET, (caddr_t)&myaddr, addrsize);
811 bindnam->b_wptr = bindnam->b_rptr + (addrsize+SO_MSGOFFSET+1);
813 code = sobind(newSocket, bindnam, addrsize);
821 #else /* AFS_HPUX110_ENV */
822 code = soreserve(newSocket, 50000, 50000);
824 code = soreserve(newSocket, 32766, 32766);
826 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
828 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
829 #if defined(AFS_FBSD_ENV)
830 code = sobind(newSocket, (struct sockaddr *) &myaddr, curproc);
832 code = sobind(newSocket, (struct sockaddr *) &myaddr);
835 printf("sobind fails (%d)\n", (int) code);
839 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
841 nam = m_getclr(M_WAIT, MT_SONAME);
842 #else /* AFS_OSF_ENV */
843 nam = m_get(M_WAIT, MT_SONAME);
846 #if defined(KERNEL_HAVE_UERROR)
851 nam->m_len = sizeof(myaddr);
852 memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
854 BHV_PDATA(&bhv) = (void*)newSocket;
855 code = sobind(&bhv, nam);
858 code = sobind(newSocket, nam);
861 printf("sobind fails (%d)\n", (int) code);
863 #ifndef AFS_SGI65_ENV
868 #endif /* else AFS_DARWIN_ENV */
869 #endif /* else AFS_HPUX110_ENV */
871 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
872 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
874 return (struct osi_socket *) newSocket;
877 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
878 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
880 return (struct osi_socket *) 0;
884 /* free socket allocated by rxk_NewSocket */
885 int rxk_FreeSocket(register struct socket *asocket)
887 AFS_STATCNT(osi_FreeSocket);
888 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
889 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
892 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
893 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
897 #endif /* !SUN5 && !LINUX20 */
899 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
901 * Run RX event daemon every second (5 times faster than rest of systems)
903 void afs_rxevent_daemon(void)
909 #ifdef RX_ENABLE_LOCKS
911 #endif /* RX_ENABLE_LOCKS */
914 rxevent_RaiseEvents(&temp);
917 #ifdef RX_ENABLE_LOCKS
919 #endif /* RX_ENABLE_LOCKS */
920 #ifdef RX_KERNEL_TRACE
921 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
922 ICL_TYPE_STRING, "before afs_osi_Wait()");
924 afs_osi_Wait(500, NULL, 0);
925 #ifdef RX_KERNEL_TRACE
926 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
927 ICL_TYPE_STRING, "after afs_osi_Wait()");
929 if (afs_termState == AFSOP_STOP_RXEVENT )
931 #ifdef RXK_LISTENER_ENV
932 afs_termState = AFSOP_STOP_RXK_LISTENER;
934 afs_termState = AFSOP_STOP_COMPLETE;
936 osi_rxWakeup(&afs_termState);
943 #ifdef RXK_LISTENER_ENV
945 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
946 int rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
949 struct sockaddr_in from;
952 register afs_int32 tlen;
953 afs_int32 savelen; /* was using rlen but had aliasing problems */
954 rx_computelen(p, tlen);
955 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
957 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
958 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
959 * it once in order to avoid races. */
962 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
970 /* add some padding to the last iovec, it's just to make sure that the
971 * read doesn't return more data than we expect, and is done to get around
972 * our problems caused by the lack of a length field in the rx header. */
973 savelen = p->wirevec[p->niovecs-1].iov_len;
974 p->wirevec[p->niovecs-1].iov_len = savelen + RX_EXTRABUFFERSIZE;
976 nbytes = tlen + sizeof(afs_int32);
977 #ifdef RX_KERNEL_TRACE
979 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
980 ICL_TYPE_STRING, "before osi_NetRecive()");
983 code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs,
986 #ifdef RX_KERNEL_TRACE
988 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
989 ICL_TYPE_STRING, "after osi_NetRecive()");
992 /* restore the vec to its correct state */
993 p->wirevec[p->niovecs-1].iov_len = savelen;
996 p->length = nbytes - RX_HEADER_SIZE;;
997 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
999 rxi_MorePackets(rx_initSendWindow);
1001 MUTEX_ENTER(&rx_stats_mutex);
1002 rx_stats.bogusPacketOnRead++;
1003 rx_stats.bogusHost = from.sin_addr.s_addr;
1004 MUTEX_EXIT(&rx_stats_mutex);
1005 dpf(("B: bogus packet from [%x,%d] nb=%d", from.sin_addr.s_addr,
1006 from.sin_port,nbytes));
1011 /* Extract packet header. */
1012 rxi_DecodePacketHeader(p);
1014 *host = from.sin_addr.s_addr;
1015 *port = from.sin_port;
1016 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1017 MUTEX_ENTER(&rx_stats_mutex);
1018 rx_stats.packetsRead[p->header.type-1]++;
1019 MUTEX_EXIT(&rx_stats_mutex);
1022 /* Free any empty packet buffers at the end of this packet */
1023 rxi_TrimDataBufs(p, 1);
1034 * Listen for packets on socket. This thread is typically started after
1035 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1036 * the start state is set before proceeding.
1038 * Note that this thread is outside the AFS global lock for much of
1041 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1042 * we want here. So we need to either block all signals (including SIGKILL
1043 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1044 * OS's socket receive routine returns as a result of a signal.
1046 int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
1050 * Run the listener as a kernel thread.
1052 void rxk_Listener(void)
1055 void rxk_ListenerProc(void);
1056 if (thread_create(NULL, DEFAULTSTKSZ, rxk_ListenerProc,
1057 0, 0, &p0, TS_RUN, minclsyspri) == NULL)
1058 osi_Panic("rxk_Listener: failed to start listener thread!\n");
1061 void rxk_ListenerProc(void)
1062 #else /* AFS_SUN5_ENV */
1063 void rxk_Listener(void)
1064 #endif /* AFS_SUN5_ENV */
1066 struct rx_packet *rxp = NULL;
1070 #ifdef AFS_LINUX20_ENV
1071 rxk_ListenerPid = current->pid;
1074 rxk_ListenerPid = 1; /* No PID, just a flag that we're alive */
1075 #endif /* AFS_SUN5_ENV */
1077 rxk_ListenerPid = curproc->p_pid;
1078 #endif /* AFS_FBSD_ENV */
1079 #if defined(AFS_DARWIN_ENV)
1080 rxk_ListenerPid = current_proc()->p_pid;
1082 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1084 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1086 while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1088 rxi_RestoreDataBufs(rxp);
1091 rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1093 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1095 if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1097 rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1102 #ifdef RX_ENABLE_LOCKS
1104 #endif /* RX_ENABLE_LOCKS */
1105 if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1106 afs_termState = AFSOP_STOP_COMPLETE;
1107 osi_rxWakeup(&afs_termState);
1109 rxk_ListenerPid = 0;
1110 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1111 osi_rxWakeup(&rxk_ListenerPid);
1115 #endif /* AFS_SUN5_ENV */
1118 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1119 /* The manner of stopping the rx listener thread may vary. Most unix's should
1120 * be able to call soclose.
1122 void osi_StopListener(void)
1127 #endif /* RXK_LISTENER_ENV */
1129 #endif /* !NCR && !UKERNEL */