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_SGI62_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)
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_LINUX26_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
);
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 # if !defined(AFS_SGI62_ENV)
393 if (numMyNetAddrs
== 0)
394 (void)rxi_GetIFInfo();
397 ifn
= rxi_FindIfnet(pp
->host
, NULL
);
399 rx_rto_setPeerTimeoutSecs(pp
, 2);
400 pp
->ifMTU
= MIN(RX_MAX_PACKET_SIZE
, rx_MyMaxSendSize
);
401 # ifdef IFF_POINTOPOINT
402 if (rx_ifnet_flags(ifn
) & IFF_POINTOPOINT
) {
403 /* wish we knew the bit rate and the chunk size, sigh. */
404 rx_rto_setPeerTimeoutSecs(pp
, 4);
405 pp
->ifMTU
= RX_PP_PACKET_SIZE
;
407 # endif /* IFF_POINTOPOINT */
408 /* Diminish the packet size to one based on the MTU given by
410 if (rx_ifnet_mtu(ifn
) > (RX_IPUDP_SIZE
+ RX_HEADER_SIZE
)) {
411 rxmtu
= rx_ifnet_mtu(ifn
) - RX_IPUDP_SIZE
;
412 if (rxmtu
< pp
->ifMTU
)
415 } else { /* couldn't find the interface, so assume the worst */
416 rx_rto_setPeerTimeoutSecs(pp
, 3);
417 pp
->ifMTU
= MIN(RX_REMOTE_PACKET_SIZE
, rx_MyMaxSendSize
);
419 # endif /* else AFS_USERSPACE_IP_ADDR */
420 #else /* AFS_SUN5_ENV */
423 mtu
= rxi_FindIfMTU(pp
->host
);
426 rx_rto_setPeerTimeoutSecs(pp
, 3);
427 pp
->ifMTU
= MIN(RX_REMOTE_PACKET_SIZE
, rx_MyMaxSendSize
);
429 rx_rto_setPeerTimeoutSecs(pp
, 2);
430 pp
->ifMTU
= MIN(RX_MAX_PACKET_SIZE
, rx_MyMaxSendSize
);
432 /* Diminish the packet size to one based on the MTU given by
434 if (mtu
> (RX_IPUDP_SIZE
+ RX_HEADER_SIZE
)) {
435 rxmtu
= mtu
- RX_IPUDP_SIZE
;
436 if (rxmtu
< pp
->ifMTU
)
440 #endif /* AFS_SUN5_ENV */
441 pp
->ifMTU
= rxi_AdjustIfMTU(pp
->ifMTU
);
442 pp
->maxMTU
= OLD_MAX_PACKET_SIZE
; /* for compatibility with old guys */
443 pp
->natMTU
= MIN(pp
->ifMTU
, OLD_MAX_PACKET_SIZE
);
445 MIN(rxi_nDgramPackets
,
446 rxi_AdjustDgramPackets(rxi_nSendFrags
, pp
->ifMTU
));
447 pp
->maxDgramPackets
= 1;
449 /* Initialize slow start parameters */
450 pp
->MTU
= MIN(pp
->natMTU
, pp
->maxMTU
);
452 pp
->nDgramPackets
= 1;
457 /* The following code is common to several system types, but not all. The
458 * separate ones are found in the system specific subdirectories.
462 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
463 /* Routine called during the afsd "-shutdown" process to put things back to
466 static struct protosw parent_proto
; /* udp proto switch */
469 shutdown_rxkernel(void)
471 struct protosw
*tpro
, *last
;
472 last
= inetdomain
.dom_protoswNPROTOSW
;
473 for (tpro
= inetdomain
.dom_protosw
; tpro
< last
; tpro
++)
474 if (tpro
->pr_protocol
== IPPROTO_UDP
) {
475 /* restore original udp protocol switch */
476 memcpy((void *)tpro
, (void *)&parent_proto
, sizeof(parent_proto
));
477 memset((void *)&parent_proto
, 0, sizeof(parent_proto
));
482 dpf(("shutdown_rxkernel: no udp proto\n"));
484 #endif /* !AIX && !SUN && !NCR && !UKERNEL */
486 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
487 /* Determine what the network interfaces are for this machine. */
489 #ifdef AFS_USERSPACE_IP_ADDR
493 int i
, j
, different
= 0, num
= ADDRSPERSITE
;
496 afs_uint32 addrs
[ADDRSPERSITE
];
497 int mtus
[ADDRSPERSITE
];
499 memset((void *)addrs
, 0, sizeof(addrs
));
500 memset((void *)mtus
, 0, sizeof(mtus
));
502 if (afs_cb_interface
.numberOfInterfaces
< num
)
503 num
= afs_cb_interface
.numberOfInterfaces
;
504 for (i
= 0; i
< num
; i
++) {
505 if (!afs_cb_interface
.mtu
[i
])
506 afs_cb_interface
.mtu
[i
] = htonl(1500);
507 rxmtu
= (ntohl(afs_cb_interface
.mtu
[i
]) - RX_IPUDP_SIZE
);
508 ifinaddr
= ntohl(afs_cb_interface
.addr_in
[i
]);
509 if (myNetAddrs
[i
] != ifinaddr
)
513 rxmtu
= rxi_AdjustIfMTU(rxmtu
);
515 rxmtu
* rxi_nRecvFrags
+ ((rxi_nRecvFrags
- 1) * UDP_HDR_SIZE
);
516 maxmtu
= rxi_AdjustMaxMTU(rxmtu
, maxmtu
);
517 addrs
[i
++] = ifinaddr
;
518 if (!rx_IsLoopbackAddr(ifinaddr
) && (maxmtu
> rx_maxReceiveSize
)) {
519 rx_maxReceiveSize
= MIN(RX_MAX_PACKET_SIZE
, maxmtu
);
520 rx_maxReceiveSize
= MIN(rx_maxReceiveSize
, rx_maxReceiveSizeUser
);
524 rx_maxJumboRecvSize
=
525 RX_HEADER_SIZE
+ (rxi_nDgramPackets
* RX_JUMBOBUFFERSIZE
) +
526 ((rxi_nDgramPackets
- 1) * RX_JUMBOHEADERSIZE
);
527 rx_maxJumboRecvSize
= MAX(rx_maxJumboRecvSize
, rx_maxReceiveSize
);
530 for (j
= 0; j
< i
; j
++) {
531 myNetMTUs
[j
] = mtus
[j
];
532 myNetAddrs
[j
] = addrs
[j
];
539 /* Returns the afs_cb_interface inxex which best matches address.
540 * If none is found, we return -1.
543 rxi_Findcbi(afs_uint32 addr
)
546 afs_uint32 myAddr
, thisAddr
, netMask
, subnetMask
;
547 afs_int32 rvalue
= -1;
550 if (numMyNetAddrs
== 0)
551 (void)rxi_GetcbiInfo();
553 myAddr
= ntohl(addr
);
555 if (IN_CLASSA(myAddr
))
556 netMask
= IN_CLASSA_NET
;
557 else if (IN_CLASSB(myAddr
))
558 netMask
= IN_CLASSB_NET
;
559 else if (IN_CLASSC(myAddr
))
560 netMask
= IN_CLASSC_NET
;
564 for (j
= 0; j
< afs_cb_interface
.numberOfInterfaces
; j
++) {
565 thisAddr
= ntohl(afs_cb_interface
.addr_in
[j
]);
566 subnetMask
= ntohl(afs_cb_interface
.subnetmask
[j
]);
567 if ((myAddr
& netMask
) == (thisAddr
& netMask
)) {
568 if ((myAddr
& subnetMask
) == (thisAddr
& subnetMask
)) {
569 if (myAddr
== thisAddr
) {
574 if (match_value
< 3) {
579 if (match_value
< 2) {
590 #else /* AFS_USERSPACE_IP_ADDR */
592 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
593 #define IFADDR2SA(f) (&((f)->ifa_addr))
594 #else /* AFS_AIX41_ENV */
595 #define IFADDR2SA(f) ((f)->ifa_addr)
605 afs_uint32 addrs
[ADDRSPERSITE
];
606 int mtus
[ADDRSPERSITE
];
608 #if defined(AFS_DARWIN80_ENV)
614 struct sockaddr sout
;
615 struct sockaddr_in
*sin
;
618 rx_ifaddr_t ifad
; /* ifnet points to a if_addrlist of ifaddrs */
622 memset(addrs
, 0, sizeof(addrs
));
623 memset(mtus
, 0, sizeof(mtus
));
625 #if defined(AFS_DARWIN80_ENV)
626 if (!ifnet_list_get(AF_INET
, &ifns
, &count
)) {
627 for (m
= 0; m
< count
; m
++) {
628 if (!ifnet_get_address_list(ifns
[m
], &ifads
)) {
629 for (j
= 0; ifads
[j
] != NULL
&& cnt
< ADDRSPERSITE
; j
++) {
630 if ((t
= ifaddr_address(ifads
[j
], &sout
, sizeof(struct sockaddr
))) == 0) {
631 sin
= (struct sockaddr_in
*)&sout
;
632 rxmtu
= rx_ifnet_mtu(rx_ifaddr_ifnet(ifads
[j
])) - RX_IPUDP_SIZE
;
633 ifinaddr
= ntohl(sin
->sin_addr
.s_addr
);
634 if (myNetAddrs
[i
] != ifinaddr
) {
638 rxmtu
= rxi_AdjustIfMTU(rxmtu
);
640 rxmtu
* rxi_nRecvFrags
+
641 ((rxi_nRecvFrags
- 1) * UDP_HDR_SIZE
);
642 maxmtu
= rxi_AdjustMaxMTU(rxmtu
, maxmtu
);
643 addrs
[i
++] = ifinaddr
;
644 if (!rx_IsLoopbackAddr(ifinaddr
) &&
645 (maxmtu
> rx_maxReceiveSize
)) {
647 MIN(RX_MAX_PACKET_SIZE
, maxmtu
);
649 MIN(rx_maxReceiveSize
, rx_maxReceiveSizeUser
);
654 ifnet_free_address_list(ifads
);
657 ifnet_list_free(ifns
);
660 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
661 #if defined(AFS_FBSD80_ENV)
662 TAILQ_FOREACH(ifn
, &V_ifnet
, if_link
) {
664 TAILQ_FOREACH(ifn
, &ifnet
, if_link
) {
666 if (i
>= ADDRSPERSITE
)
668 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
669 for (ifn
= ifnet
.tqh_first
; i
< ADDRSPERSITE
&& ifn
!= NULL
;
670 ifn
= ifn
->if_list
.tqe_next
) {
672 for (ifn
= ifnet
; ifn
!= NULL
&& i
< ADDRSPERSITE
; ifn
= ifn
->if_next
) {
674 rxmtu
= (ifn
->if_mtu
- RX_IPUDP_SIZE
);
675 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
676 TAILQ_FOREACH(ifad
, &ifn
->if_addrhead
, ifa_link
) {
677 if (i
>= ADDRSPERSITE
)
679 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
680 for (ifad
= ifn
->if_addrlist
.tqh_first
;
681 ifad
!= NULL
&& i
< ADDRSPERSITE
;
682 ifad
= ifad
->ifa_list
.tqe_next
) {
684 for (ifad
= ifn
->if_addrlist
; ifad
!= NULL
&& i
< ADDRSPERSITE
;
685 ifad
= ifad
->ifa_next
) {
687 if (IFADDR2SA(ifad
)->sa_family
== AF_INET
) {
689 ntohl(((struct sockaddr_in
*)IFADDR2SA(ifad
))->sin_addr
.
691 if (myNetAddrs
[i
] != ifinaddr
) {
695 rxmtu
= rxi_AdjustIfMTU(rxmtu
);
697 rxmtu
* rxi_nRecvFrags
+
698 ((rxi_nRecvFrags
- 1) * UDP_HDR_SIZE
);
699 maxmtu
= rxi_AdjustMaxMTU(rxmtu
, maxmtu
);
700 addrs
[i
++] = ifinaddr
;
701 if (!rx_IsLoopbackAddr(ifinaddr
) && (maxmtu
> rx_maxReceiveSize
)) {
702 rx_maxReceiveSize
= MIN(RX_MAX_PACKET_SIZE
, maxmtu
);
704 MIN(rx_maxReceiveSize
, rx_maxReceiveSizeUser
);
711 rx_maxJumboRecvSize
=
712 RX_HEADER_SIZE
+ rxi_nDgramPackets
* RX_JUMBOBUFFERSIZE
+
713 (rxi_nDgramPackets
- 1) * RX_JUMBOHEADERSIZE
;
714 rx_maxJumboRecvSize
= MAX(rx_maxJumboRecvSize
, rx_maxReceiveSize
);
718 for (l
= 0; l
< i
; l
++) {
719 myNetMTUs
[l
] = mtus
[l
];
720 myNetAddrs
[l
] = addrs
[l
];
726 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
727 /* Returns ifnet which best matches address */
729 rxi_FindIfnet(afs_uint32 addr
, afs_uint32
* maskp
)
731 struct sockaddr_in s
, sr
;
734 s
.sin_family
= AF_INET
;
735 s
.sin_addr
.s_addr
= addr
;
736 ifad
= rx_ifaddr_withnet((struct sockaddr
*)&s
);
739 rx_ifaddr_netmask(ifad
, (struct sockaddr
*)&sr
, sizeof(sr
));
740 *maskp
= sr
.sin_addr
.s_addr
;
742 return (ifad
? rx_ifaddr_ifnet(ifad
) : NULL
);
745 #else /* DARWIN || XBSD */
747 /* Returns ifnet which best matches address */
749 rxi_FindIfnet(afs_uint32 addr
, afs_uint32
* maskp
)
752 extern struct in_ifaddr
*in_ifaddr
;
753 struct in_ifaddr
*ifa
, *ifad
= NULL
;
757 for (ifa
= in_ifaddr
; ifa
; ifa
= ifa
->ia_next
) {
758 if ((addr
& ifa
->ia_netmask
) == ifa
->ia_net
) {
759 if ((addr
& ifa
->ia_subnetmask
) == ifa
->ia_subnet
) {
760 if (IA_SIN(ifa
)->sin_addr
.s_addr
== addr
) { /* ie, ME!!! */
765 if (match_value
< 3) {
770 if (match_value
< 2) {
775 } /* if net matches */
776 } /* for all in_ifaddrs */
780 *maskp
= ifad
->ia_subnetmask
;
781 return (ifad
? ifad
->ia_ifp
: NULL
);
783 #endif /* else DARWIN || XBSD */
784 #endif /* else AFS_USERSPACE_IP_ADDR */
785 #endif /* !SUN5 && !SGI62 */
788 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
789 * afs_osinet.c. One could argue that rxi_NewSocket could go into the
790 * system specific subdirectories for all systems. But for the moment,
791 * most of it is simple to follow common code.
793 #if !defined(UKERNEL)
794 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
795 /* rxk_NewSocket creates a new socket on the specified port. The port is
796 * in network byte order.
799 rxk_NewSocketHost(afs_uint32 ahost
, short aport
)
802 #ifdef AFS_DARWIN80_ENV
805 struct socket
*newSocket
;
807 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
810 struct sockaddr_in myaddr
;
811 #ifdef AFS_HPUX110_ENV
812 /* prototype copied from kernel source file streams/str_proto.h */
813 extern MBLKP
allocb_wait(int, int);
815 int addrsize
= sizeof(struct sockaddr_in
);
817 extern struct fileops socketops
;
823 AFS_STATCNT(osi_NewSocket
);
824 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
825 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
829 #if defined(AFS_HPUX102_ENV)
830 #if defined(AFS_HPUX110_ENV)
831 /* we need a file associated with the socket so sosend in NetSend
833 /* blocking socket */
834 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, 0, 0);
838 fp
->f_flag
= FREAD
| FWRITE
;
839 fp
->f_type
= DTYPE_SOCKET
;
840 fp
->f_ops
= &socketops
;
842 fp
->f_data
= (void *)newSocket
;
843 newSocket
->so_fp
= (void *)fp
;
845 #else /* AFS_HPUX110_ENV */
846 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, 0, SS_NOWAIT
);
847 #endif /* else AFS_HPUX110_ENV */
848 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
849 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, IPPROTO_UDP
);
850 #elif defined(AFS_FBSD_ENV)
851 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, IPPROTO_UDP
,
852 afs_osi_credp
, curthread
);
853 #elif defined(AFS_DARWIN80_ENV)
854 #ifdef RXK_LISTENER_ENV
855 code
= sock_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, NULL
, NULL
, &newSocket
);
857 code
= sock_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, rx_upcall
, NULL
, &newSocket
);
859 #elif defined(AFS_NBSD50_ENV)
860 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, 0, osi_curproc(), NULL
);
861 #elif defined(AFS_NBSD40_ENV)
862 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, 0, osi_curproc());
864 code
= socreate(AF_INET
, &newSocket
, SOCK_DGRAM
, 0);
865 #endif /* AFS_HPUX102_ENV */
869 memset(&myaddr
, 0, sizeof myaddr
);
870 myaddr
.sin_family
= AF_INET
;
871 myaddr
.sin_port
= aport
;
872 myaddr
.sin_addr
.s_addr
= ahost
;
873 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
874 myaddr
.sin_len
= sizeof(myaddr
);
877 #ifdef AFS_HPUX110_ENV
878 bindnam
= allocb_wait((addrsize
+ SO_MSGOFFSET
+ 1), BPRI_MED
);
883 memcpy((caddr_t
) bindnam
->b_rptr
+ SO_MSGOFFSET
, (caddr_t
) & myaddr
,
885 bindnam
->b_wptr
= bindnam
->b_rptr
+ (addrsize
+ SO_MSGOFFSET
+ 1);
886 code
= sobind(newSocket
, bindnam
, addrsize
);
889 #if !defined(AFS_HPUX1122_ENV)
896 #else /* AFS_HPUX110_ENV */
897 #if defined(AFS_DARWIN80_ENV)
902 code
= sock_setsockopt(newSocket
, SOL_SOCKET
, SO_SNDBUF
,
903 &buflen
, sizeof(buflen
));
904 code2
= sock_setsockopt(newSocket
, SOL_SOCKET
, SO_RCVBUF
,
905 &buflen
, sizeof(buflen
));
909 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
914 #if defined(AFS_NBSD_ENV)
917 code
= soreserve(newSocket
, 50000, 50000);
919 code
= soreserve(newSocket
, 32766, 32766);
921 osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
923 #if defined(AFS_NBSD_ENV)
927 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
928 #if defined(AFS_FBSD_ENV)
929 code
= sobind(newSocket
, (struct sockaddr
*)&myaddr
, curthread
);
931 code
= sobind(newSocket
, (struct sockaddr
*)&myaddr
);
934 dpf(("sobind fails (%d)\n", (int)code
));
938 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
940 nam
= m_getclr(M_WAIT
, MT_SONAME
);
941 #else /* AFS_OSF_ENV */
942 nam
= m_get(M_WAIT
, MT_SONAME
);
945 #if defined(KERNEL_HAVE_UERROR)
950 nam
->m_len
= sizeof(myaddr
);
951 memcpy(mtod(nam
, caddr_t
), &myaddr
, sizeof(myaddr
));
952 #if defined(AFS_SGI65_ENV)
953 BHV_PDATA(&bhv
) = (void *)newSocket
;
954 code
= sobind(&bhv
, nam
);
956 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
957 code
= sobind(newSocket
, nam
, osi_curproc());
959 code
= sobind(newSocket
, nam
);
962 dpf(("sobind fails (%d)\n", (int)code
));
964 #ifndef AFS_SGI65_ENV
969 #endif /* else AFS_DARWIN_ENV */
970 #endif /* else AFS_HPUX110_ENV */
973 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
974 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
976 return (osi_socket
*)newSocket
;
980 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
981 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
983 return (osi_socket
*)0;
987 rxk_NewSocket(short aport
)
989 return rxk_NewSocketHost(0, aport
);
992 /* free socket allocated by rxk_NewSocket */
994 rxk_FreeSocket(struct socket
*asocket
)
996 AFS_STATCNT(osi_FreeSocket
);
997 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
998 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1000 #ifdef AFS_HPUX110_ENV
1001 if (asocket
->so_fp
) {
1002 struct file
*fp
= asocket
->so_fp
;
1003 #if !defined(AFS_HPUX1123_ENV)
1004 /* 11.23 still has falloc, but not FPENTRYFREE !
1005 * so for now if we shutdown, we will waist a file
1008 asocket
->so_fp
= NULL
;
1011 #endif /* AFS_HPUX110_ENV */
1013 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1014 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1018 #endif /* !SUN5 && !LINUX20 */
1020 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV) || defined(RXK_UPCALL_ENV)
1021 #ifdef RXK_TIMEDSLEEP_ENV
1022 /* Shutting down should wake us up, as should an earlier event. */
1024 rxi_ReScheduleEvents(void)
1026 /* needed to allow startup */
1027 int glock
= ISAFS_GLOCK();
1030 osi_rxWakeup(&afs_termState
);
1036 * Run RX event daemon every second (5 times faster than rest of systems)
1039 afs_rxevent_daemon(void)
1045 #ifdef RX_ENABLE_LOCKS
1047 #endif /* RX_ENABLE_LOCKS */
1049 rxevent_RaiseEvents(&temp
);
1051 #ifdef RX_ENABLE_LOCKS
1053 #endif /* RX_ENABLE_LOCKS */
1054 #ifdef RX_KERNEL_TRACE
1055 afs_Trace1(afs_iclSetp
, CM_TRACE_TIMESTAMP
, ICL_TYPE_STRING
,
1056 "before afs_osi_Wait()");
1058 #ifdef RXK_TIMEDSLEEP_ENV
1059 afs_osi_TimedSleep(&afs_termState
, MAX(500, ((temp
.sec
* 1000) +
1060 (temp
.usec
/ 1000))), 0);
1062 afs_osi_Wait(500, NULL
, 0);
1064 #ifdef RX_KERNEL_TRACE
1065 afs_Trace1(afs_iclSetp
, CM_TRACE_TIMESTAMP
, ICL_TYPE_STRING
,
1066 "after afs_osi_Wait()");
1068 if (afs_termState
== AFSOP_STOP_RXEVENT
) {
1069 #ifdef RXK_LISTENER_ENV
1070 afs_termState
= AFSOP_STOP_RXK_LISTENER
;
1071 #elif defined(AFS_SUN510_ENV) || defined(RXK_UPCALL_ENV)
1072 afs_termState
= AFSOP_STOP_NETIF
;
1074 afs_termState
= AFSOP_STOP_COMPLETE
;
1076 osi_rxWakeup(&afs_termState
);
1083 #ifdef RXK_LISTENER_ENV
1085 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1087 rxk_ReadPacket(osi_socket so
, struct rx_packet
*p
, int *host
, int *port
)
1090 struct sockaddr_in from
;
1094 afs_int32 savelen
; /* was using rlen but had aliasing problems */
1095 rx_computelen(p
, tlen
);
1096 rx_SetDataSize(p
, tlen
); /* this is the size of the user data area */
1098 tlen
+= RX_HEADER_SIZE
; /* now this is the size of the entire packet */
1099 rlen
= rx_maxJumboRecvSize
; /* this is what I am advertising. Only check
1100 * it once in order to avoid races. */
1103 tlen
= rxi_AllocDataBuf(p
, tlen
, RX_PACKET_CLASS_RECV_CBUF
);
1111 /* add some padding to the last iovec, it's just to make sure that the
1112 * read doesn't return more data than we expect, and is done to get around
1113 * our problems caused by the lack of a length field in the rx header. */
1114 savelen
= p
->wirevec
[p
->niovecs
- 1].iov_len
;
1115 p
->wirevec
[p
->niovecs
- 1].iov_len
= savelen
+ RX_EXTRABUFFERSIZE
;
1117 nbytes
= tlen
+ sizeof(afs_int32
);
1118 #ifdef RX_KERNEL_TRACE
1119 if (ICL_SETACTIVE(afs_iclSetp
)) {
1121 afs_Trace1(afs_iclSetp
, CM_TRACE_TIMESTAMP
, ICL_TYPE_STRING
,
1122 "before osi_NetRecive()");
1126 code
= osi_NetReceive(rx_socket
, &from
, p
->wirevec
, p
->niovecs
, &nbytes
);
1128 #ifdef RX_KERNEL_TRACE
1129 if (ICL_SETACTIVE(afs_iclSetp
)) {
1131 afs_Trace1(afs_iclSetp
, CM_TRACE_TIMESTAMP
, ICL_TYPE_STRING
,
1132 "after osi_NetRecive()");
1136 /* restore the vec to its correct state */
1137 p
->wirevec
[p
->niovecs
- 1].iov_len
= savelen
;
1140 p
->length
= nbytes
- RX_HEADER_SIZE
;;
1141 if ((nbytes
> tlen
) || (p
->length
& 0x8000)) { /* Bogus packet */
1143 if (rx_stats_active
) {
1144 MUTEX_ENTER(&rx_stats_mutex
);
1145 rx_atomic_inc(&rx_stats
.bogusPacketOnRead
);
1146 rx_stats
.bogusHost
= from
.sin_addr
.s_addr
;
1147 MUTEX_EXIT(&rx_stats_mutex
);
1149 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1150 from
.sin_addr
.s_addr
, from
.sin_port
, nbytes
));
1154 /* Extract packet header. */
1155 rxi_DecodePacketHeader(p
);
1157 *host
= from
.sin_addr
.s_addr
;
1158 *port
= from
.sin_port
;
1159 if (p
->header
.type
> 0 && p
->header
.type
< RX_N_PACKET_TYPES
) {
1160 if (rx_stats_active
) {
1161 rx_atomic_inc(&rx_stats
.packetsRead
[p
->header
.type
- 1]);
1165 #ifdef RX_TRIMDATABUFS
1166 /* Free any empty packet buffers at the end of this packet */
1167 rxi_TrimDataBufs(p
, 1);
1177 * Listen for packets on socket. This thread is typically started after
1178 * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1179 * the start state is set before proceeding.
1181 * Note that this thread is outside the AFS global lock for much of
1184 * In many OS's, the socket receive code sleeps interruptibly. That's not what
1185 * we want here. So we need to either block all signals (including SIGKILL
1186 * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1187 * OS's socket receive routine returns as a result of a signal.
1189 int rxk_ListenerPid
; /* Used to signal process to wakeup at shutdown */
1190 #ifdef AFS_LINUX20_ENV
1191 struct task_struct
*rxk_ListenerTask
;
1197 struct rx_packet
*rxp
= NULL
;
1201 #ifdef AFS_LINUX20_ENV
1202 rxk_ListenerPid
= current
->pid
;
1203 rxk_ListenerTask
= current
;
1204 allow_signal(SIGKILL
); /* Allowed, but blocked until shutdown */
1207 rxk_ListenerPid
= 1; /* No PID, just a flag that we're alive */
1208 #endif /* AFS_SUN5_ENV */
1210 rxk_ListenerPid
= curproc
->p_pid
;
1211 #endif /* AFS_FBSD_ENV */
1212 #ifdef AFS_DARWIN80_ENV
1213 rxk_ListenerPid
= proc_selfpid();
1214 #elif defined(AFS_DARWIN_ENV)
1215 rxk_ListenerPid
= current_proc()->p_pid
;
1217 #ifdef RX_ENABLE_LOCKS
1219 #endif /* RX_ENABLE_LOCKS */
1220 while (afs_termState
!= AFSOP_STOP_RXK_LISTENER
) {
1221 /* See if a check for additional packets was issued */
1225 rxi_RestoreDataBufs(rxp
);
1227 rxp
= rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE
);
1229 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1231 if (!(code
= rxk_ReadPacket(rx_socket
, rxp
, &host
, &port
))) {
1232 rxp
= rxi_ReceivePacket(rxp
, rx_socket
, host
, port
, 0, 0);
1236 #ifdef RX_ENABLE_LOCKS
1238 #endif /* RX_ENABLE_LOCKS */
1239 if (afs_termState
== AFSOP_STOP_RXK_LISTENER
) {
1240 #ifdef AFS_SUN510_ENV
1241 afs_termState
= AFSOP_STOP_NETIF
;
1243 afs_termState
= AFSOP_STOP_COMPLETE
;
1245 osi_rxWakeup(&afs_termState
);
1247 rxk_ListenerPid
= 0;
1248 #ifdef AFS_LINUX20_ENV
1249 rxk_ListenerTask
= 0;
1250 osi_rxWakeup(&rxk_ListenerTask
);
1252 #if defined(AFS_SUN5_ENV) || defined(AFS_FBSD_ENV)
1253 osi_rxWakeup(&rxk_ListenerPid
);
1257 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1258 /* The manner of stopping the rx listener thread may vary. Most unix's should
1259 * be able to call soclose.
1262 osi_StopListener(void)
1267 #endif /* RXK_LISTENER_ENV */
1268 #endif /* !NCR && !UKERNEL */
1270 #if !defined(AFS_LINUX26_ENV)
1272 #if defined(AFS_AIX_ENV)
1273 osi_Panic(char *msg
, void *a1
, void *a2
, void *a3
)
1275 osi_Panic(char *msg
, ...)
1280 msg
= "Unknown AFS panic";
1282 * we should probably use the errsave facility here. it is not
1286 printf(msg
, a1
, a2
, a3
);
1288 #elif defined(AFS_SGI_ENV)
1291 /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1293 cmn_err(CE_PANIC
, "Unknown AFS panic");
1296 icmn_err(CE_PANIC
, msg
, ap
);
1299 #elif defined(AFS_DARWIN80_ENV) || defined(AFS_LINUX22_ENV) || defined(AFS_FBSD_ENV) || defined(UKERNEL)
1303 msg
= "Unknown AFS panic";
1306 vsnprintf(buf
, sizeof(buf
), msg
, ap
);
1313 msg
= "Unknown AFS panic";
1318 # ifdef AFS_LINUX20_ENV