17c478bd9Sstevel@tonic-gate /*
2*e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1987 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley. The name of the
167c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include "mpd_defs.h"
247c478bd9Sstevel@tonic-gate #include "mpd_tables.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Probe types for probe()
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #define	PROBE_UNI	0x1234		/* Unicast probe packet */
307c478bd9Sstevel@tonic-gate #define	PROBE_MULTI	0x5678		/* Multicast probe packet */
317c478bd9Sstevel@tonic-gate #define	PROBE_RTT	0x9abc		/* RTT only probe packet */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define	MSEC_PERMIN	(60 * MILLISEC)	/* Number of milliseconds in a minute */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Format of probe / probe response packets. This is an ICMP Echo request
377c478bd9Sstevel@tonic-gate  * or ICMP Echo reply. Packet format is same for both IPv4 and IPv6
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate struct pr_icmp
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	uint8_t  pr_icmp_type;		/* type field */
427c478bd9Sstevel@tonic-gate 	uint8_t  pr_icmp_code;		/* code field */
437c478bd9Sstevel@tonic-gate 	uint16_t pr_icmp_cksum;		/* checksum field */
447c478bd9Sstevel@tonic-gate 	uint16_t pr_icmp_id;		/* Identification */
457c478bd9Sstevel@tonic-gate 	uint16_t pr_icmp_seq;		/* sequence number */
46*e11c3f44Smeem 	uint64_t pr_icmp_timestamp;	/* Time stamp (in ns) */
477c478bd9Sstevel@tonic-gate 	uint32_t pr_icmp_mtype;		/* Message type */
487c478bd9Sstevel@tonic-gate };
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static struct in6_addr all_nodes_mcast_v6 = { { 0xff, 0x2, 0x0, 0x0,
517c478bd9Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
527c478bd9Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
537c478bd9Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x1 } };
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static struct in_addr all_nodes_mcast_v4 = { { { 0xe0, 0x0, 0x0, 0x1 } } };
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static hrtime_t	last_fdt_bumpup_time;	/* When FDT was bumped up last */
587c478bd9Sstevel@tonic-gate 
59*e11c3f44Smeem static void		*find_ancillary(struct msghdr *msg, int cmsg_level,
60*e11c3f44Smeem     int cmsg_type);
61*e11c3f44Smeem static void		pi_set_crtt(struct target *tg, int64_t m,
627c478bd9Sstevel@tonic-gate     boolean_t is_probe_uni);
637c478bd9Sstevel@tonic-gate static void		incoming_echo_reply(struct phyint_instance *pii,
64*e11c3f44Smeem     struct pr_icmp *reply, struct in6_addr fromaddr, struct timeval *recv_tvp);
657c478bd9Sstevel@tonic-gate static void		incoming_rtt_reply(struct phyint_instance *pii,
667c478bd9Sstevel@tonic-gate     struct pr_icmp *reply, struct in6_addr fromaddr);
677c478bd9Sstevel@tonic-gate static void		incoming_mcast_reply(struct phyint_instance *pii,
687c478bd9Sstevel@tonic-gate     struct pr_icmp *reply, struct in6_addr fromaddr);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static boolean_t	check_pg_crtt_improved(struct phyint_group *pg);
717c478bd9Sstevel@tonic-gate static boolean_t	check_pii_crtt_improved(struct phyint_instance *pii);
727c478bd9Sstevel@tonic-gate static boolean_t	check_exception_target(struct phyint_instance *pii,
737c478bd9Sstevel@tonic-gate     struct target *target);
747c478bd9Sstevel@tonic-gate static void		probe_fail_info(struct phyint_instance *pii,
757c478bd9Sstevel@tonic-gate     struct target *cur_tg, struct probe_fail_count *pfinfo);
767c478bd9Sstevel@tonic-gate static void		probe_success_info(struct phyint_instance *pii,
777c478bd9Sstevel@tonic-gate     struct target *cur_tg, struct probe_success_count *psinfo);
787c478bd9Sstevel@tonic-gate static boolean_t	phyint_repaired(struct phyint *pi);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static boolean_t	highest_ack_tg(uint16_t seq, struct target *tg);
817c478bd9Sstevel@tonic-gate static int 		in_cksum(ushort_t *addr, int len);
827c478bd9Sstevel@tonic-gate static void		reset_snxt_basetimes(void);
83*e11c3f44Smeem static int		ns2ms(int64_t ns);
84*e11c3f44Smeem static int64_t		tv2ns(struct timeval *);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * CRTT - Conservative Round Trip Time Estimate
887c478bd9Sstevel@tonic-gate  * Probe success - A matching probe reply received before CRTT ms has elapsed
897c478bd9Sstevel@tonic-gate  *	after sending the probe.
907c478bd9Sstevel@tonic-gate  * Probe failure - No probe reply received and more than CRTT ms has elapsed
917c478bd9Sstevel@tonic-gate  *	after sending the probe.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * TLS - Time last success. Most recent probe ack received at this time.
947c478bd9Sstevel@tonic-gate  * TFF - Time first fail. The time of the earliest probe failure in
957c478bd9Sstevel@tonic-gate  *	a consecutive series of probe failures.
967c478bd9Sstevel@tonic-gate  * NUM_PROBE_REPAIRS  - Number of consecutive successful probes required
977c478bd9Sstevel@tonic-gate  * 	before declaring phyint repair.
987c478bd9Sstevel@tonic-gate  * NUM_PROBE_FAILS - Number of consecutive probe failures required to
997c478bd9Sstevel@tonic-gate  *	declare a phyint failure.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  * 			Phyint state diagram
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * The state of a phyint that is capable of being probed, is completely
104*e11c3f44Smeem  * specified by the 3-tuple <pi_state, pg_state, I>.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * A phyint starts in either PI_RUNNING or PI_FAILED, depending on the state
1077c478bd9Sstevel@tonic-gate  * of the link (according to the driver).  If the phyint is also configured
1087c478bd9Sstevel@tonic-gate  * with a test address (the common case) and probe targets, then a phyint must
1097c478bd9Sstevel@tonic-gate  * also successfully be able to send and receive probes in order to remain in
1107c478bd9Sstevel@tonic-gate  * the PI_RUNNING state (otherwise, it transitions to PI_FAILED).
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * Further, if a PI_RUNNING phyint is configured with a test address but is
1137c478bd9Sstevel@tonic-gate  * unable to find any probe targets, it will transition to the PI_NOTARGETS
1147c478bd9Sstevel@tonic-gate  * state, which indicates that the link is apparently functional but that
1157c478bd9Sstevel@tonic-gate  * in.mpathd is unable to send probes to verify functionality (in this case,
1167c478bd9Sstevel@tonic-gate  * in.mpathd makes the optimistic assumption that the interface is working
117*e11c3f44Smeem  * correctly and thus does not mark the interface FAILED, but reports it as
118*e11c3f44Smeem  * IPMP_IF_UNKNOWN through the async events and query interfaces).
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * At any point, a phyint may be administratively marked offline via if_mpadm.
1217c478bd9Sstevel@tonic-gate  * In this case, the interface always transitions to PI_OFFLINE, regardless
1227c478bd9Sstevel@tonic-gate  * of its previous state.  When the interface is later brought back online,
1237c478bd9Sstevel@tonic-gate  * in.mpathd acts as if the interface is new (and thus it transitions to
1247c478bd9Sstevel@tonic-gate  * PI_RUNNING or PI_FAILED based on the status of the link and the result of
1257c478bd9Sstevel@tonic-gate  * its probes, if probes are sent).
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * pi_state -  PI_RUNNING or PI_FAILED
1287c478bd9Sstevel@tonic-gate  *	PI_RUNNING: The failure detection logic says the phyint is good.
1297c478bd9Sstevel@tonic-gate  *	PI_FAILED: The failure detection logic says the phyint has failed.
1307c478bd9Sstevel@tonic-gate  *
131*e11c3f44Smeem  * pg_state  - PG_OK, PG_DEGRADED, or PG_FAILED.
132*e11c3f44Smeem  *	PG_OK: All interfaces in the group are OK.
133*e11c3f44Smeem  *	PG_DEGRADED: Some interfaces in the group are unusable.
134*e11c3f44Smeem  *	PG_FAILED: All interfaces in the group are unusable.
135*e11c3f44Smeem  *
1367c478bd9Sstevel@tonic-gate  *	In the case of router targets, we assume that the current list of
1377c478bd9Sstevel@tonic-gate  *	targets obtained from the routing table, is still valid, so the
1387c478bd9Sstevel@tonic-gate  *	phyint stat is PI_FAILED. In the case of host targets, we delete the
1397c478bd9Sstevel@tonic-gate  *	list of targets, and multicast to the all hosts, to reconstruct the
1407c478bd9Sstevel@tonic-gate  *	target list. So the phyints are in the PI_NOTARGETS state.
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * I -	value of (pi_flags & IFF_INACTIVE)
143*e11c3f44Smeem  *	IFF_INACTIVE: This phyint will not send or receive packets.
144*e11c3f44Smeem  *	Usually, inactive is tied to standby interfaces that are not yet
145*e11c3f44Smeem  *	needed (e.g., no non-standby interfaces in the group have failed).
146*e11c3f44Smeem  *	When failback has been disabled (FAILBACK=no configured), phyint can
147*e11c3f44Smeem  *	also be a non-STANDBY. In this case IFF_INACTIVE is set when phyint
148*e11c3f44Smeem  *	subsequently recovers after a failure.
1497c478bd9Sstevel@tonic-gate  *
150*e11c3f44Smeem  * Not all 9 possible combinations of the above 3-tuple are possible.
1517c478bd9Sstevel@tonic-gate  *
152*e11c3f44Smeem  * I is tracked by IP. pi_state is tracked by mpathd.
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  *			pi_state state machine
1557c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------------------------
1567c478bd9Sstevel@tonic-gate  *	Event			State			New State
1577c478bd9Sstevel@tonic-gate  *				Action:
1587c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------------------------
159*e11c3f44Smeem  *	IP interface failure	(PI_RUNNING, I == 0) -> (PI_FAILED, I == 0)
1607c478bd9Sstevel@tonic-gate  *	detection		: set IFF_FAILED on this phyint
1617c478bd9Sstevel@tonic-gate  *
162*e11c3f44Smeem  *	IP interface failure	(PI_RUNNING, I == 1) -> (PI_FAILED, I == 0)
1637c478bd9Sstevel@tonic-gate  *	detection		: set IFF_FAILED on this phyint
1647c478bd9Sstevel@tonic-gate  *
165*e11c3f44Smeem  *	IP interface repair 	(PI_FAILED, I == 0, FAILBACK=yes)
16649df4566Sethindra  *	detection				     -> (PI_RUNNING, I == 0)
1677c478bd9Sstevel@tonic-gate  *				: clear IFF_FAILED on this phyint
1687c478bd9Sstevel@tonic-gate  *
169*e11c3f44Smeem  *	IP interface repair 	(PI_FAILED, I == 0, FAILBACK=no)
17049df4566Sethindra  *	detection				     ->	(PI_RUNNING, I == 1)
17149df4566Sethindra  *				: clear IFF_FAILED on this phyint
17249df4566Sethindra  *				: if failback is disabled set I == 1
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  *	Group failure		(perform on all phyints in the group)
1757c478bd9Sstevel@tonic-gate  *	detection 		PI_RUNNING		PI_FAILED
1767c478bd9Sstevel@tonic-gate  *	(Router targets)	: set IFF_FAILED
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  *	Group failure		(perform on all phyints in the group)
1797c478bd9Sstevel@tonic-gate  *	detection 		PI_RUNNING		PI_NOTARGETS
1807c478bd9Sstevel@tonic-gate  *	(Host targets)		: set IFF_FAILED
1817c478bd9Sstevel@tonic-gate  *				: delete the target list on all phyints
1827c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------------------------
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate struct probes_missed probes_missed;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Compose and transmit an ICMP ECHO REQUEST packet.  The IP header
1897c478bd9Sstevel@tonic-gate  * will be added on by the kernel.  The id field identifies this phyint.
1907c478bd9Sstevel@tonic-gate  * and the sequence number is an increasing (modulo 2^^16) integer. The data
1917c478bd9Sstevel@tonic-gate  * portion holds the time value when the packet is sent. On echo this is
1927c478bd9Sstevel@tonic-gate  * extracted to compute the round-trip time. Three different types of
1937c478bd9Sstevel@tonic-gate  * probe packets are used.
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  * PROBE_UNI: This type is used to do failure detection / failure recovery
1967c478bd9Sstevel@tonic-gate  *	and RTT calculation. PROBE_UNI probes are spaced apart in time,
1977c478bd9Sstevel@tonic-gate  *	not less than the current CRTT. pii_probes[] stores data
1987c478bd9Sstevel@tonic-gate  *	about these probes. These packets consume sequence number space.
1997c478bd9Sstevel@tonic-gate  *
200*e11c3f44Smeem  * PROBE_RTT: This type is used to make only rtt measurements. Normally these
2017c478bd9Sstevel@tonic-gate  * 	are not used. Under heavy network load, the rtt may go up very high,
2027c478bd9Sstevel@tonic-gate  *	due to a spike, or may appear to go high, due to extreme scheduling
2037c478bd9Sstevel@tonic-gate  * 	delays. Once the network stress is removed, mpathd takes long time to
2047c478bd9Sstevel@tonic-gate  *	recover, because the probe_interval is already high, and it takes
2057c478bd9Sstevel@tonic-gate  *	a long time to send out sufficient number of probes to bring down the
2067c478bd9Sstevel@tonic-gate  *	rtt. To avoid this problem, PROBE_RTT probes are sent out every
2077c478bd9Sstevel@tonic-gate  *	user_probe_interval ms. and will cause only rtt updates. These packets
2087c478bd9Sstevel@tonic-gate  *	do not consume sequence number space nor is information about these
2097c478bd9Sstevel@tonic-gate  *	packets stored in the pii_probes[]
2107c478bd9Sstevel@tonic-gate  *
2117c478bd9Sstevel@tonic-gate  * PROBE_MULTI: This type is only used to construct a list of targets, when
2127c478bd9Sstevel@tonic-gate  *	no targets are known. The packet is multicast to the all hosts addr.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate static void
215*e11c3f44Smeem probe(struct phyint_instance *pii, uint_t probe_type, hrtime_t start_hrtime)
2167c478bd9Sstevel@tonic-gate {
217*e11c3f44Smeem 	hrtime_t sent_hrtime;
218*e11c3f44Smeem 	struct timeval sent_tv;
2197c478bd9Sstevel@tonic-gate 	struct pr_icmp probe_pkt;	/* Probe packet */
220*e11c3f44Smeem 	struct sockaddr_storage targ;	/* target address */
221*e11c3f44Smeem 	uint_t	targaddrlen;		/* targed address length */
2227c478bd9Sstevel@tonic-gate 	int	pr_ndx;			/* probe index in pii->pii_probes[] */
2237c478bd9Sstevel@tonic-gate 	boolean_t sent = _B_TRUE;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (debug & D_TARGET) {
226*e11c3f44Smeem 		logdebug("probe(%s %s %d %lld)\n", AF_STR(pii->pii_af),
227*e11c3f44Smeem 		    pii->pii_name, probe_type, start_hrtime);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	assert(pii->pii_probe_sock != -1);
2317c478bd9Sstevel@tonic-gate 	assert(probe_type == PROBE_UNI || probe_type == PROBE_MULTI ||
2327c478bd9Sstevel@tonic-gate 	    probe_type == PROBE_RTT);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	probe_pkt.pr_icmp_type = (pii->pii_af == AF_INET) ?
2357c478bd9Sstevel@tonic-gate 	    ICMP_ECHO_REQUEST : ICMP6_ECHO_REQUEST;
2367c478bd9Sstevel@tonic-gate 	probe_pkt.pr_icmp_code = 0;
2377c478bd9Sstevel@tonic-gate 	probe_pkt.pr_icmp_cksum = 0;
2387c478bd9Sstevel@tonic-gate 	probe_pkt.pr_icmp_seq = htons(pii->pii_snxt);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * Since there is no need to do arithmetic on the icmpid,
2427c478bd9Sstevel@tonic-gate 	 * (only equality check is done) pii_icmpid is stored in
2437c478bd9Sstevel@tonic-gate 	 * network byte order at initialization itself.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	probe_pkt.pr_icmp_id = pii->pii_icmpid;
246*e11c3f44Smeem 	probe_pkt.pr_icmp_timestamp = htonll(start_hrtime);
2477c478bd9Sstevel@tonic-gate 	probe_pkt.pr_icmp_mtype = htonl(probe_type);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * If probe_type is PROBE_MULTI, this packet will be multicast to
2517c478bd9Sstevel@tonic-gate 	 * the all hosts address. Otherwise it is unicast to the next target.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	assert(probe_type == PROBE_MULTI || ((pii->pii_target_next != NULL) &&
2547c478bd9Sstevel@tonic-gate 	    pii->pii_rtt_target_next != NULL));
2557c478bd9Sstevel@tonic-gate 
256*e11c3f44Smeem 	bzero(&targ, sizeof (targ));
257*e11c3f44Smeem 	targ.ss_family = pii->pii_af;
258*e11c3f44Smeem 
2597c478bd9Sstevel@tonic-gate 	if (pii->pii_af == AF_INET6) {
260*e11c3f44Smeem 		struct in6_addr *addr6;
261*e11c3f44Smeem 
262*e11c3f44Smeem 		addr6 = &((struct sockaddr_in6 *)&targ)->sin6_addr;
263*e11c3f44Smeem 		targaddrlen = sizeof (struct sockaddr_in6);
2647c478bd9Sstevel@tonic-gate 		if (probe_type == PROBE_MULTI) {
265*e11c3f44Smeem 			*addr6 = all_nodes_mcast_v6;
2667c478bd9Sstevel@tonic-gate 		} else if (probe_type == PROBE_UNI) {
267*e11c3f44Smeem 			*addr6 = pii->pii_target_next->tg_address;
268*e11c3f44Smeem 		} else { /* type is PROBE_RTT */
269*e11c3f44Smeem 			*addr6 = pii->pii_rtt_target_next->tg_address;
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 	} else {
272*e11c3f44Smeem 		struct in_addr *addr4;
273*e11c3f44Smeem 
274*e11c3f44Smeem 		addr4 = &((struct sockaddr_in *)&targ)->sin_addr;
275*e11c3f44Smeem 		targaddrlen = sizeof (struct sockaddr_in);
2767c478bd9Sstevel@tonic-gate 		if (probe_type == PROBE_MULTI) {
277*e11c3f44Smeem 			*addr4 = all_nodes_mcast_v4;
2787c478bd9Sstevel@tonic-gate 		} else if (probe_type == PROBE_UNI) {
2797c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(
280*e11c3f44Smeem 			    &pii->pii_target_next->tg_address, addr4);
281*e11c3f44Smeem 		} else { /* type is PROBE_RTT */
2827c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(
283*e11c3f44Smeem 			    &pii->pii_rtt_target_next->tg_address, addr4);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * Compute the IPv4 icmp checksum. Does not cover the IP header.
2887c478bd9Sstevel@tonic-gate 		 */
2897c478bd9Sstevel@tonic-gate 		probe_pkt.pr_icmp_cksum =
2907c478bd9Sstevel@tonic-gate 		    in_cksum((ushort_t *)&probe_pkt, (int)sizeof (probe_pkt));
291*e11c3f44Smeem 	}
292*e11c3f44Smeem 
293*e11c3f44Smeem 	/*
294*e11c3f44Smeem 	 * Use the current time as the time we sent.  Not atomic, but the best
295*e11c3f44Smeem 	 * we can do from here.
296*e11c3f44Smeem 	 */
297*e11c3f44Smeem 	sent_hrtime = gethrtime();
298*e11c3f44Smeem 	(void) gettimeofday(&sent_tv, NULL);
299*e11c3f44Smeem 	if (sendto(pii->pii_probe_sock, &probe_pkt, sizeof (probe_pkt), 0,
300*e11c3f44Smeem 	    (struct sockaddr *)&targ, targaddrlen) != sizeof (probe_pkt)) {
301*e11c3f44Smeem 		logperror_pii(pii, "probe: probe sendto");
302*e11c3f44Smeem 		sent = _B_FALSE;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * If this is a PROBE_UNI probe packet being unicast to a target, then
3077c478bd9Sstevel@tonic-gate 	 * update our tables. We will need this info in processing the probe
3087c478bd9Sstevel@tonic-gate 	 * response. PROBE_MULTI and PROBE_RTT packets are not used for
3097c478bd9Sstevel@tonic-gate 	 * the purpose of failure or recovery detection. PROBE_MULTI packets
3107c478bd9Sstevel@tonic-gate 	 * are only used to construct a list of targets. PROBE_RTT packets are
3117c478bd9Sstevel@tonic-gate 	 * used only for updating the rtt and not for failure detection.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if (probe_type == PROBE_UNI && sent) {
3147c478bd9Sstevel@tonic-gate 		pr_ndx = pii->pii_probe_next;
3157c478bd9Sstevel@tonic-gate 		assert(pr_ndx >= 0 && pr_ndx < PROBE_STATS_COUNT);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		/* Collect statistics, before we reuse the last slot. */
3187c478bd9Sstevel@tonic-gate 		if (pii->pii_probes[pr_ndx].pr_status == PR_LOST)
3197c478bd9Sstevel@tonic-gate 			pii->pii_cum_stats.lost++;
3207c478bd9Sstevel@tonic-gate 		else if (pii->pii_probes[pr_ndx].pr_status == PR_ACKED)
3217c478bd9Sstevel@tonic-gate 			pii->pii_cum_stats.acked++;
3227c478bd9Sstevel@tonic-gate 		pii->pii_cum_stats.sent++;
3237c478bd9Sstevel@tonic-gate 
324*e11c3f44Smeem 		pii->pii_probes[pr_ndx].pr_id = pii->pii_snxt;
325*e11c3f44Smeem 		pii->pii_probes[pr_ndx].pr_tv_sent = sent_tv;
326*e11c3f44Smeem 		pii->pii_probes[pr_ndx].pr_hrtime_sent = sent_hrtime;
327*e11c3f44Smeem 		pii->pii_probes[pr_ndx].pr_hrtime_start = start_hrtime;
3287c478bd9Sstevel@tonic-gate 		pii->pii_probes[pr_ndx].pr_target = pii->pii_target_next;
329*e11c3f44Smeem 		probe_chstate(&pii->pii_probes[pr_ndx], pii, PR_UNACKED);
330*e11c3f44Smeem 
3317c478bd9Sstevel@tonic-gate 		pii->pii_probe_next = PROBE_INDEX_NEXT(pii->pii_probe_next);
3327c478bd9Sstevel@tonic-gate 		pii->pii_target_next = target_next(pii->pii_target_next);
3337c478bd9Sstevel@tonic-gate 		assert(pii->pii_target_next != NULL);
3347c478bd9Sstevel@tonic-gate 		/*
3357c478bd9Sstevel@tonic-gate 		 * If we have a single variable to denote the next target to
3367c478bd9Sstevel@tonic-gate 		 * probe for both rtt probes and failure detection probes, we
3377c478bd9Sstevel@tonic-gate 		 * could end up with a situation where the failure detection
3387c478bd9Sstevel@tonic-gate 		 * probe targets become disjoint from the rtt probe targets.
3397c478bd9Sstevel@tonic-gate 		 * Eg. if 2 targets and the actual fdt is double the user
3407c478bd9Sstevel@tonic-gate 		 * specified fdt. So we have 2 variables. In this scheme
3417c478bd9Sstevel@tonic-gate 		 * we also reset pii_rtt_target_next for every fdt probe,
3427c478bd9Sstevel@tonic-gate 		 * though that may not be necessary.
3437c478bd9Sstevel@tonic-gate 		 */
3447c478bd9Sstevel@tonic-gate 		pii->pii_rtt_target_next = pii->pii_target_next;
3457c478bd9Sstevel@tonic-gate 		pii->pii_snxt++;
3467c478bd9Sstevel@tonic-gate 	} else if (probe_type == PROBE_RTT) {
3477c478bd9Sstevel@tonic-gate 		pii->pii_rtt_target_next =
3487c478bd9Sstevel@tonic-gate 		    target_next(pii->pii_rtt_target_next);
3497c478bd9Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next != NULL);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Incoming IPv4 data from wire, is received here. Called from main.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate void
3577c478bd9Sstevel@tonic-gate in_data(struct phyint_instance *pii)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	struct	sockaddr_in 	from;
3607c478bd9Sstevel@tonic-gate 	struct	in6_addr	fromaddr;
361*e11c3f44Smeem 	static uint64_t in_packet[(IP_MAXPACKET + 1)/8];
362*e11c3f44Smeem 	static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
3637c478bd9Sstevel@tonic-gate 	struct ip *ip;
3647c478bd9Sstevel@tonic-gate 	int 	iphlen;
3657c478bd9Sstevel@tonic-gate 	int 	len;
3667c478bd9Sstevel@tonic-gate 	char 	abuf[INET_ADDRSTRLEN];
367*e11c3f44Smeem 	struct msghdr msg;
368*e11c3f44Smeem 	struct iovec iov;
369*e11c3f44Smeem 	struct pr_icmp *reply;
370*e11c3f44Smeem 	struct timeval *recv_tvp;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
3737c478bd9Sstevel@tonic-gate 		logdebug("in_data(%s %s)\n",
3747c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
377*e11c3f44Smeem 	iov.iov_base = (char *)in_packet;
378*e11c3f44Smeem 	iov.iov_len = sizeof (in_packet);
379*e11c3f44Smeem 	msg.msg_iov = &iov;
380*e11c3f44Smeem 	msg.msg_iovlen = 1;
381*e11c3f44Smeem 	msg.msg_name = (struct sockaddr *)&from;
382*e11c3f44Smeem 	msg.msg_namelen = sizeof (from);
383*e11c3f44Smeem 	msg.msg_control = ancillary_data;
384*e11c3f44Smeem 	msg.msg_controllen = sizeof (ancillary_data);
385*e11c3f44Smeem 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * Poll has already told us that a message is waiting,
3887c478bd9Sstevel@tonic-gate 	 * on this socket. Read it now. We should not block.
3897c478bd9Sstevel@tonic-gate 	 */
390*e11c3f44Smeem 	if ((len = recvmsg(pii->pii_probe_sock, &msg, 0)) < 0) {
391*e11c3f44Smeem 		logperror_pii(pii, "in_data: recvmsg");
3927c478bd9Sstevel@tonic-gate 		return;
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/*
396*e11c3f44Smeem 	 * If the datalink has indicated the link is down, don't go
3977c478bd9Sstevel@tonic-gate 	 * any further.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if (LINK_DOWN(pii->pii_phyint))
4007c478bd9Sstevel@tonic-gate 		return;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/* Get the printable address for error reporting */
4037c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, &from.sin_addr, abuf, sizeof (abuf));
4047c478bd9Sstevel@tonic-gate 
405*e11c3f44Smeem 	/* Ignore packets > 64k or control buffers that don't fit */
406*e11c3f44Smeem 	if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
407*e11c3f44Smeem 		if (debug & D_PKTBAD) {
408*e11c3f44Smeem 			logdebug("Truncated message: msg_flags 0x%x from %s\n",
409*e11c3f44Smeem 			    msg.msg_flags, abuf);
410*e11c3f44Smeem 		}
411*e11c3f44Smeem 		return;
412*e11c3f44Smeem 	}
413*e11c3f44Smeem 
4147c478bd9Sstevel@tonic-gate 	/* Make sure packet contains at least minimum ICMP header */
4157c478bd9Sstevel@tonic-gate 	ip = (struct ip *)in_packet;
4167c478bd9Sstevel@tonic-gate 	iphlen = ip->ip_hl << 2;
4177c478bd9Sstevel@tonic-gate 	if (len < iphlen + ICMP_MINLEN) {
4187c478bd9Sstevel@tonic-gate 		if (debug & D_PKTBAD) {
4197c478bd9Sstevel@tonic-gate 			logdebug("in_data: packet too short (%d bytes)"
4207c478bd9Sstevel@tonic-gate 			    " from %s\n", len, abuf);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		return;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Subtract the IP hdr length, 'len' will be length of the probe
4277c478bd9Sstevel@tonic-gate 	 * reply, starting from the icmp hdr.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	len -= iphlen;
4307c478bd9Sstevel@tonic-gate 	/* LINTED */
4317c478bd9Sstevel@tonic-gate 	reply = (struct pr_icmp *)((char *)in_packet + iphlen);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/* Probe replies are icmp echo replies. Ignore anything else */
4347c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_type != ICMP_ECHO_REPLY)
4357c478bd9Sstevel@tonic-gate 		return;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * The icmp id should match what we sent, which is stored
4397c478bd9Sstevel@tonic-gate 	 * in pi_icmpid. The icmp code for reply must be 0.
4407c478bd9Sstevel@tonic-gate 	 * The reply content must be a struct pr_icmp
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_id != pii->pii_icmpid) {
4437c478bd9Sstevel@tonic-gate 		/* Not in response to our probe */
4447c478bd9Sstevel@tonic-gate 		return;
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_code != 0) {
4487c478bd9Sstevel@tonic-gate 		logtrace("probe reply code %d from %s on %s\n",
4497c478bd9Sstevel@tonic-gate 		    reply->pr_icmp_code, abuf, pii->pii_name);
4507c478bd9Sstevel@tonic-gate 		return;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (len < sizeof (struct pr_icmp)) {
4547c478bd9Sstevel@tonic-gate 		logtrace("probe reply too short: %d bytes from %s on %s\n",
4557c478bd9Sstevel@tonic-gate 		    len, abuf, pii->pii_name);
4567c478bd9Sstevel@tonic-gate 		return;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
459*e11c3f44Smeem 	recv_tvp = find_ancillary(&msg, SOL_SOCKET, SCM_TIMESTAMP);
460*e11c3f44Smeem 	if (recv_tvp == NULL) {
461*e11c3f44Smeem 		logtrace("message without timestamp from %s on %s\n",
462*e11c3f44Smeem 		    abuf, pii->pii_name);
463*e11c3f44Smeem 		return;
464*e11c3f44Smeem 	}
465*e11c3f44Smeem 
4667c478bd9Sstevel@tonic-gate 	IN6_INADDR_TO_V4MAPPED(&from.sin_addr, &fromaddr);
4677c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_mtype == htonl(PROBE_UNI))
4687c478bd9Sstevel@tonic-gate 		/* Unicast probe reply */
469*e11c3f44Smeem 		incoming_echo_reply(pii, reply, fromaddr, recv_tvp);
4707c478bd9Sstevel@tonic-gate 	else if (reply->pr_icmp_mtype == htonl(PROBE_MULTI)) {
4717c478bd9Sstevel@tonic-gate 		/* Multicast reply */
4727c478bd9Sstevel@tonic-gate 		incoming_mcast_reply(pii, reply, fromaddr);
4737c478bd9Sstevel@tonic-gate 	} else if (reply->pr_icmp_mtype == htonl(PROBE_RTT)) {
4747c478bd9Sstevel@tonic-gate 		incoming_rtt_reply(pii, reply, fromaddr);
4757c478bd9Sstevel@tonic-gate 	} else {
4767c478bd9Sstevel@tonic-gate 		/* Probably not in response to our probe */
4777c478bd9Sstevel@tonic-gate 		logtrace("probe reply type: %d from %s on %s\n",
4787c478bd9Sstevel@tonic-gate 		    reply->pr_icmp_mtype, abuf, pii->pii_name);
4797c478bd9Sstevel@tonic-gate 		return;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Incoming IPv6 data from wire is received here. Called from main.
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate void
4877c478bd9Sstevel@tonic-gate in6_data(struct phyint_instance *pii)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 from;
4907c478bd9Sstevel@tonic-gate 	static uint64_t in_packet[(IP_MAXPACKET + 1)/8];
4917c478bd9Sstevel@tonic-gate 	static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
4927c478bd9Sstevel@tonic-gate 	int len;
4937c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
4947c478bd9Sstevel@tonic-gate 	struct msghdr msg;
4957c478bd9Sstevel@tonic-gate 	struct iovec iov;
496*e11c3f44Smeem 	void	*opt;
4977c478bd9Sstevel@tonic-gate 	struct	pr_icmp *reply;
498*e11c3f44Smeem 	struct	timeval *recv_tvp;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
5017c478bd9Sstevel@tonic-gate 		logdebug("in6_data(%s %s)\n",
5027c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	iov.iov_base = (char *)in_packet;
5067c478bd9Sstevel@tonic-gate 	iov.iov_len = sizeof (in_packet);
5077c478bd9Sstevel@tonic-gate 	msg.msg_iov = &iov;
5087c478bd9Sstevel@tonic-gate 	msg.msg_iovlen = 1;
5097c478bd9Sstevel@tonic-gate 	msg.msg_name = (struct sockaddr *)&from;
5107c478bd9Sstevel@tonic-gate 	msg.msg_namelen = sizeof (from);
5117c478bd9Sstevel@tonic-gate 	msg.msg_control = ancillary_data;
5127c478bd9Sstevel@tonic-gate 	msg.msg_controllen = sizeof (ancillary_data);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if ((len = recvmsg(pii->pii_probe_sock, &msg, 0)) < 0) {
515*e11c3f44Smeem 		logperror_pii(pii, "in6_data: recvmsg");
5167c478bd9Sstevel@tonic-gate 		return;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
520*e11c3f44Smeem 	 * If the datalink has indicated that the link is down, don't go
5217c478bd9Sstevel@tonic-gate 	 * any further.
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	if (LINK_DOWN(pii->pii_phyint))
5247c478bd9Sstevel@tonic-gate 		return;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/* Get the printable address for error reporting */
5277c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, &from.sin6_addr, abuf, sizeof (abuf));
5287c478bd9Sstevel@tonic-gate 	if (len < ICMP_MINLEN) {
5297c478bd9Sstevel@tonic-gate 		if (debug & D_PKTBAD) {
5307c478bd9Sstevel@tonic-gate 			logdebug("Truncated message: msg_flags 0x%x from %s\n",
5317c478bd9Sstevel@tonic-gate 			    msg.msg_flags, abuf);
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		return;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	/* Ignore packets > 64k or control buffers that don't fit */
5367c478bd9Sstevel@tonic-gate 	if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
5377c478bd9Sstevel@tonic-gate 		if (debug & D_PKTBAD) {
5387c478bd9Sstevel@tonic-gate 			logdebug("Truncated message: msg_flags 0x%x from %s\n",
5397c478bd9Sstevel@tonic-gate 			    msg.msg_flags, abuf);
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 		return;
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	reply = (struct pr_icmp *)in_packet;
5457c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_type != ICMP6_ECHO_REPLY)
5467c478bd9Sstevel@tonic-gate 		return;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_id != pii->pii_icmpid) {
5497c478bd9Sstevel@tonic-gate 		/* Not in response to our probe */
5507c478bd9Sstevel@tonic-gate 		return;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * The kernel has already verified the the ICMP checksum.
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	if (!IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
5577c478bd9Sstevel@tonic-gate 		logtrace("ICMPv6 echo reply source address not linklocal from "
5587c478bd9Sstevel@tonic-gate 		    "%s on %s\n", abuf, pii->pii_name);
5597c478bd9Sstevel@tonic-gate 		return;
5607c478bd9Sstevel@tonic-gate 	}
561*e11c3f44Smeem 	opt = find_ancillary(&msg, IPPROTO_IPV6, IPV6_RTHDR);
5627c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
5637c478bd9Sstevel@tonic-gate 		/* Can't allow routing headers in probe replies  */
5647c478bd9Sstevel@tonic-gate 		logtrace("message with routing header from %s on %s\n",
5657c478bd9Sstevel@tonic-gate 		    abuf, pii->pii_name);
5667c478bd9Sstevel@tonic-gate 		return;
5677c478bd9Sstevel@tonic-gate 	}
568*e11c3f44Smeem 
5697c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_code != 0) {
5707c478bd9Sstevel@tonic-gate 		logtrace("probe reply code: %d from %s on %s\n",
5717c478bd9Sstevel@tonic-gate 		    reply->pr_icmp_code, abuf, pii->pii_name);
5727c478bd9Sstevel@tonic-gate 		return;
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 	if (len < (sizeof (struct pr_icmp))) {
5757c478bd9Sstevel@tonic-gate 		logtrace("probe reply too short: %d bytes from %s on %s\n",
5767c478bd9Sstevel@tonic-gate 		    len, abuf, pii->pii_name);
5777c478bd9Sstevel@tonic-gate 		return;
5787c478bd9Sstevel@tonic-gate 	}
579*e11c3f44Smeem 
580*e11c3f44Smeem 	recv_tvp = find_ancillary(&msg, SOL_SOCKET, SCM_TIMESTAMP);
581*e11c3f44Smeem 	if (recv_tvp == NULL) {
582*e11c3f44Smeem 		logtrace("message without timestamp from %s on %s\n",
583*e11c3f44Smeem 		    abuf, pii->pii_name);
584*e11c3f44Smeem 		return;
585*e11c3f44Smeem 	}
586*e11c3f44Smeem 
5877c478bd9Sstevel@tonic-gate 	if (reply->pr_icmp_mtype == htonl(PROBE_UNI)) {
588*e11c3f44Smeem 		incoming_echo_reply(pii, reply, from.sin6_addr, recv_tvp);
5897c478bd9Sstevel@tonic-gate 	} else if (reply->pr_icmp_mtype == htonl(PROBE_MULTI)) {
5907c478bd9Sstevel@tonic-gate 		incoming_mcast_reply(pii, reply, from.sin6_addr);
5917c478bd9Sstevel@tonic-gate 	} else if (reply->pr_icmp_mtype == htonl(PROBE_RTT)) {
5927c478bd9Sstevel@tonic-gate 		incoming_rtt_reply(pii, reply, from.sin6_addr);
5937c478bd9Sstevel@tonic-gate 	} else  {
5947c478bd9Sstevel@tonic-gate 		/* Probably not in response to our probe */
5957c478bd9Sstevel@tonic-gate 		logtrace("probe reply type: %d from %s on %s\n",
5967c478bd9Sstevel@tonic-gate 		    reply->pr_icmp_mtype, abuf, pii->pii_name);
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * Process the incoming rtt reply, in response to our rtt probe.
6027c478bd9Sstevel@tonic-gate  * Common for both IPv4 and IPv6. Unlike incoming_echo_reply() we don't
6037c478bd9Sstevel@tonic-gate  * have any stored information about the probe we sent. So we don't log
6047c478bd9Sstevel@tonic-gate  * any errors if we receive bad replies.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate static void
6077c478bd9Sstevel@tonic-gate incoming_rtt_reply(struct phyint_instance *pii, struct pr_icmp *reply,
6087c478bd9Sstevel@tonic-gate     struct in6_addr fromaddr)
6097c478bd9Sstevel@tonic-gate {
610*e11c3f44Smeem 	int64_t	m;		/* rtt measurement in ns */
6117c478bd9Sstevel@tonic-gate 	char	abuf[INET6_ADDRSTRLEN];
6127c478bd9Sstevel@tonic-gate 	struct	target	*target;
6137c478bd9Sstevel@tonic-gate 	struct 	phyint_group *pg;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/* Get the printable address for error reporting */
6167c478bd9Sstevel@tonic-gate 	(void) pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf));
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
6197c478bd9Sstevel@tonic-gate 		logdebug("incoming_rtt_reply: %s %s %s\n",
6207c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name, abuf);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/* Do we know this target ? */
6247c478bd9Sstevel@tonic-gate 	target = target_lookup(pii, fromaddr);
6257c478bd9Sstevel@tonic-gate 	if (target == NULL)
6267c478bd9Sstevel@tonic-gate 		return;
6277c478bd9Sstevel@tonic-gate 
628*e11c3f44Smeem 	m = (int64_t)(gethrtime() - ntohll(reply->pr_icmp_timestamp));
6297c478bd9Sstevel@tonic-gate 	/* Invalid rtt. It has wrapped around */
6307c478bd9Sstevel@tonic-gate 	if (m < 0)
6317c478bd9Sstevel@tonic-gate 		return;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * Don't update rtt until we see NUM_PROBE_REPAIRS probe responses
6357c478bd9Sstevel@tonic-gate 	 * The initial few responses after the interface is repaired may
6367c478bd9Sstevel@tonic-gate 	 * contain high rtt's because they could have been queued up waiting
6377c478bd9Sstevel@tonic-gate 	 * for ARP/NDP resolution on a failed interface.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	pg = pii->pii_phyint->pi_group;
6407c478bd9Sstevel@tonic-gate 	if ((pii->pii_state != PI_RUNNING) || GROUP_FAILED(pg))
6417c478bd9Sstevel@tonic-gate 		return;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * Update rtt only if the new rtt is lower than the current rtt.
6457c478bd9Sstevel@tonic-gate 	 * (specified by the 3rd parameter to pi_set_crtt).
6467c478bd9Sstevel@tonic-gate 	 * If a spike has caused the current probe_interval to be >
6477c478bd9Sstevel@tonic-gate 	 * user_probe_interval, then this mechanism is used to bring down
6487c478bd9Sstevel@tonic-gate 	 * the rtt rapidly once the network stress is removed.
6497c478bd9Sstevel@tonic-gate 	 * If the new rtt is higher than the current rtt, we don't want to
6507c478bd9Sstevel@tonic-gate 	 * update the rtt. We are having more than 1 outstanding probe and
6517c478bd9Sstevel@tonic-gate 	 * the increase in rtt we are seeing is being unnecessarily weighted
6527c478bd9Sstevel@tonic-gate 	 * many times. The regular rtt update will be handled by
6537c478bd9Sstevel@tonic-gate 	 * incoming_echo_reply() and will take care of any rtt increase.
6547c478bd9Sstevel@tonic-gate 	 */
6557c478bd9Sstevel@tonic-gate 	pi_set_crtt(target, m, _B_FALSE);
6567c478bd9Sstevel@tonic-gate 	if ((target->tg_crtt < (pg->pg_probeint / LOWER_FDT_TRIGGER)) &&
6577c478bd9Sstevel@tonic-gate 	    (user_failure_detection_time < pg->pg_fdt) &&
6587c478bd9Sstevel@tonic-gate 	    (last_fdt_bumpup_time + MIN_SETTLING_TIME < gethrtime())) {
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * If the crtt has now dropped by a factor of LOWER_FT_TRIGGER,
6617c478bd9Sstevel@tonic-gate 		 * investigate if we can improve the failure detection time to
6627c478bd9Sstevel@tonic-gate 		 * meet whatever the user specified.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		if (check_pg_crtt_improved(pg)) {
6657c478bd9Sstevel@tonic-gate 			pg->pg_fdt = MAX(pg->pg_fdt / NEXT_FDT_MULTIPLE,
6667c478bd9Sstevel@tonic-gate 			    user_failure_detection_time);
6677c478bd9Sstevel@tonic-gate 			pg->pg_probeint = pg->pg_fdt / (NUM_PROBE_FAILS + 2);
6687c478bd9Sstevel@tonic-gate 			if (pii->pii_phyint->pi_group != phyint_anongroup) {
6697c478bd9Sstevel@tonic-gate 				logerr("Improved failure detection time %d ms "
6707c478bd9Sstevel@tonic-gate 				    "on (%s %s) for group \"%s\"\n",
6717c478bd9Sstevel@tonic-gate 				    pg->pg_fdt, AF_STR(pii->pii_af),
6727c478bd9Sstevel@tonic-gate 				    pii->pii_name,
6737c478bd9Sstevel@tonic-gate 				    pii->pii_phyint->pi_group->pg_name);
6747c478bd9Sstevel@tonic-gate 			}
6757c478bd9Sstevel@tonic-gate 			if (user_failure_detection_time == pg->pg_fdt) {
6767c478bd9Sstevel@tonic-gate 				/* Avoid any truncation or rounding errors */
6777c478bd9Sstevel@tonic-gate 				pg->pg_probeint = user_probe_interval;
6787c478bd9Sstevel@tonic-gate 				/*
6797c478bd9Sstevel@tonic-gate 				 * No more rtt probes will be sent. The actual
6807c478bd9Sstevel@tonic-gate 				 * fdt has dropped to the user specified value.
6817c478bd9Sstevel@tonic-gate 				 * pii_fd_snxt_basetime and pii_snxt_basetime
6827c478bd9Sstevel@tonic-gate 				 * will be in sync henceforth.
6837c478bd9Sstevel@tonic-gate 				 */
6847c478bd9Sstevel@tonic-gate 				reset_snxt_basetimes();
6857c478bd9Sstevel@tonic-gate 			}
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * Process the incoming echo reply, in response to our unicast probe.
6927c478bd9Sstevel@tonic-gate  * Common for both IPv4 and IPv6
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate static void
6957c478bd9Sstevel@tonic-gate incoming_echo_reply(struct phyint_instance *pii, struct pr_icmp *reply,
696*e11c3f44Smeem     struct in6_addr fromaddr, struct timeval *recv_tvp)
6977c478bd9Sstevel@tonic-gate {
698*e11c3f44Smeem 	int64_t	m;		/* rtt measurement in ns */
699*e11c3f44Smeem 	hrtime_t cur_hrtime;	/* in ns from some arbitrary point */
7007c478bd9Sstevel@tonic-gate 	char	abuf[INET6_ADDRSTRLEN];
7017c478bd9Sstevel@tonic-gate 	int	pr_ndx;
7027c478bd9Sstevel@tonic-gate 	struct	target	*target;
7037c478bd9Sstevel@tonic-gate 	boolean_t exception;
704*e11c3f44Smeem 	uint64_t pr_icmp_timestamp;
7057c478bd9Sstevel@tonic-gate 	uint16_t pr_icmp_seq;
706*e11c3f44Smeem 	struct	probe_stats *pr_statp;
7077c478bd9Sstevel@tonic-gate 	struct 	phyint_group *pg = pii->pii_phyint->pi_group;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/* Get the printable address for error reporting */
7107c478bd9Sstevel@tonic-gate 	(void) pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf));
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
713*e11c3f44Smeem 		logdebug("incoming_echo_reply: %s %s %s seq %u recv_tvp %lld\n",
7147c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name, abuf,
715*e11c3f44Smeem 		    ntohs(reply->pr_icmp_seq), tv2ns(recv_tvp));
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
718*e11c3f44Smeem 	pr_icmp_timestamp = ntohll(reply->pr_icmp_timestamp);
719*e11c3f44Smeem 	pr_icmp_seq = ntohs(reply->pr_icmp_seq);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/* Reject out of window probe replies */
7227c478bd9Sstevel@tonic-gate 	if (SEQ_GE(pr_icmp_seq, pii->pii_snxt) ||
7237c478bd9Sstevel@tonic-gate 	    SEQ_LT(pr_icmp_seq, pii->pii_snxt - PROBE_STATS_COUNT)) {
7247c478bd9Sstevel@tonic-gate 		logtrace("out of window probe seq %u snxt %u on %s from %s\n",
7257c478bd9Sstevel@tonic-gate 		    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
7267c478bd9Sstevel@tonic-gate 		pii->pii_cum_stats.unknown++;
7277c478bd9Sstevel@tonic-gate 		return;
7287c478bd9Sstevel@tonic-gate 	}
729*e11c3f44Smeem 
730*e11c3f44Smeem 	cur_hrtime = gethrtime();
731*e11c3f44Smeem 	m = (int64_t)(cur_hrtime - pr_icmp_timestamp);
7327c478bd9Sstevel@tonic-gate 	if (m < 0) {
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * This is a ridiculously high value of rtt. rtt has wrapped
7357c478bd9Sstevel@tonic-gate 		 * around. Log a message, and ignore the rtt.
7367c478bd9Sstevel@tonic-gate 		 */
737*e11c3f44Smeem 		logerr("incoming_echo_reply: rtt wraparound cur_hrtime %lld "
738*e11c3f44Smeem 		    "reply timestamp %lld\n", cur_hrtime, pr_icmp_timestamp);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
7427c478bd9Sstevel@tonic-gate 	 * Get the probe index pr_ndx corresponding to the received icmp seq.
7437c478bd9Sstevel@tonic-gate 	 * number in our pii->pii_probes[] array. The icmp sequence number
7447c478bd9Sstevel@tonic-gate 	 * pii_snxt corresponds to the probe index pii->pii_probe_next
7457c478bd9Sstevel@tonic-gate 	 */
7467c478bd9Sstevel@tonic-gate 	pr_ndx = MOD_SUB(pii->pii_probe_next,
7477c478bd9Sstevel@tonic-gate 	    (uint16_t)(pii->pii_snxt - pr_icmp_seq), PROBE_STATS_COUNT);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	assert(PR_STATUS_VALID(pii->pii_probes[pr_ndx].pr_status));
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	target = pii->pii_probes[pr_ndx].pr_target;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * Perform sanity checks, whether this probe reply that we
7557c478bd9Sstevel@tonic-gate 	 * have received is genuine
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	if (target != NULL) {
7587c478bd9Sstevel@tonic-gate 		/*
7597c478bd9Sstevel@tonic-gate 		 * Compare the src. addr of the received ICMP or ICMPv6
7607c478bd9Sstevel@tonic-gate 		 * probe reply with the target address in our tables.
7617c478bd9Sstevel@tonic-gate 		 */
7627c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&target->tg_address, &fromaddr)) {
7637c478bd9Sstevel@tonic-gate 			/*
7647c478bd9Sstevel@tonic-gate 			 * We don't have any record of having sent a probe to
7657c478bd9Sstevel@tonic-gate 			 * this target. This is a fake probe reply. Log an error
7667c478bd9Sstevel@tonic-gate 			 */
7677c478bd9Sstevel@tonic-gate 			logtrace("probe status %d Fake probe reply seq %u "
7687c478bd9Sstevel@tonic-gate 			    "snxt %u on %s from %s\n",
7697c478bd9Sstevel@tonic-gate 			    pii->pii_probes[pr_ndx].pr_status,
7707c478bd9Sstevel@tonic-gate 			    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
7717c478bd9Sstevel@tonic-gate 			pii->pii_cum_stats.unknown++;
7727c478bd9Sstevel@tonic-gate 			return;
7737c478bd9Sstevel@tonic-gate 		} else if (pii->pii_probes[pr_ndx].pr_status == PR_ACKED) {
7747c478bd9Sstevel@tonic-gate 			/*
7757c478bd9Sstevel@tonic-gate 			 * The address matches, but our tables indicate that
7767c478bd9Sstevel@tonic-gate 			 * this probe reply has been acked already. So this
7777c478bd9Sstevel@tonic-gate 			 * is a duplicate probe reply. Log an error
7787c478bd9Sstevel@tonic-gate 			 */
7797c478bd9Sstevel@tonic-gate 			logtrace("probe status %d Duplicate probe reply seq %u "
7807c478bd9Sstevel@tonic-gate 			    "snxt %u on %s from %s\n",
7817c478bd9Sstevel@tonic-gate 			    pii->pii_probes[pr_ndx].pr_status,
7827c478bd9Sstevel@tonic-gate 			    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
7837c478bd9Sstevel@tonic-gate 			pii->pii_cum_stats.unknown++;
7847c478bd9Sstevel@tonic-gate 			return;
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 	} else {
7877c478bd9Sstevel@tonic-gate 		/*
7887c478bd9Sstevel@tonic-gate 		 * Target must not be NULL in the PR_UNACKED state
7897c478bd9Sstevel@tonic-gate 		 */
7907c478bd9Sstevel@tonic-gate 		assert(pii->pii_probes[pr_ndx].pr_status != PR_UNACKED);
7917c478bd9Sstevel@tonic-gate 		if (pii->pii_probes[pr_ndx].pr_status == PR_UNUSED) {
7927c478bd9Sstevel@tonic-gate 			/*
7937c478bd9Sstevel@tonic-gate 			 * The probe stats slot is unused. So we didn't
7947c478bd9Sstevel@tonic-gate 			 * send out any probe to this target. This is a fake.
7957c478bd9Sstevel@tonic-gate 			 * Log an error.
7967c478bd9Sstevel@tonic-gate 			 */
7977c478bd9Sstevel@tonic-gate 			logtrace("probe status %d Fake probe reply seq %u "
7987c478bd9Sstevel@tonic-gate 			    "snxt %u on %s from %s\n",
7997c478bd9Sstevel@tonic-gate 			    pii->pii_probes[pr_ndx].pr_status,
8007c478bd9Sstevel@tonic-gate 			    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 		pii->pii_cum_stats.unknown++;
8037c478bd9Sstevel@tonic-gate 		return;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/*
8077c478bd9Sstevel@tonic-gate 	 * If the rtt does not appear to be right, don't update the
8087c478bd9Sstevel@tonic-gate 	 * rtt stats. This can happen if the system dropped into the
8097c478bd9Sstevel@tonic-gate 	 * debugger, or the system was hung or too busy for a
8107c478bd9Sstevel@tonic-gate 	 * substantial time that we didn't get a chance to run.
8117c478bd9Sstevel@tonic-gate 	 */
812*e11c3f44Smeem 	if ((m < 0) || (ns2ms(m) > PROBE_STATS_COUNT * pg->pg_probeint)) {
8137c478bd9Sstevel@tonic-gate 		/*
814*e11c3f44Smeem 		 * If the probe corresponding to this received response
815*e11c3f44Smeem 		 * was truly sent 'm' ns. ago, then this response must
8167c478bd9Sstevel@tonic-gate 		 * have been rejected by the sequence number checks. The
8177c478bd9Sstevel@tonic-gate 		 * fact that it has passed the sequence number checks
8187c478bd9Sstevel@tonic-gate 		 * means that the measured rtt is wrong. We were probably
8197c478bd9Sstevel@tonic-gate 		 * scheduled long after the packet was received.
8207c478bd9Sstevel@tonic-gate 		 */
8217c478bd9Sstevel@tonic-gate 		goto out;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Don't update rtt until we see NUM_PROBE_REPAIRS probe responses
8267c478bd9Sstevel@tonic-gate 	 * The initial few responses after the interface is repaired may
8277c478bd9Sstevel@tonic-gate 	 * contain high rtt's because they could have been queued up waiting
8287c478bd9Sstevel@tonic-gate 	 * for ARP/NDP resolution on a failed interface.
8297c478bd9Sstevel@tonic-gate 	 */
8307c478bd9Sstevel@tonic-gate 	if ((pii->pii_state != PI_RUNNING) || GROUP_FAILED(pg))
8317c478bd9Sstevel@tonic-gate 		goto out;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/*
8347c478bd9Sstevel@tonic-gate 	 * Don't update the Conservative Round Trip Time estimate for this
8357c478bd9Sstevel@tonic-gate 	 * (phint, target) pair if this is the not the highest ack seq seen
8367c478bd9Sstevel@tonic-gate 	 * thus far on this target.
8377c478bd9Sstevel@tonic-gate 	 */
8387c478bd9Sstevel@tonic-gate 	if (!highest_ack_tg(pr_icmp_seq, target))
8397c478bd9Sstevel@tonic-gate 		goto out;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	/*
8427c478bd9Sstevel@tonic-gate 	 * Always update the rtt. This is a failure detection probe
8437c478bd9Sstevel@tonic-gate 	 * and we want to measure both increase / decrease in rtt.
8447c478bd9Sstevel@tonic-gate 	 */
8457c478bd9Sstevel@tonic-gate 	pi_set_crtt(target, m, _B_TRUE);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/*
8487c478bd9Sstevel@tonic-gate 	 * If the crtt exceeds the average time between probes,
8497c478bd9Sstevel@tonic-gate 	 * investigate if this slow target is an exception. If so we
8507c478bd9Sstevel@tonic-gate 	 * can avoid this target and still meet the failure detection
8517c478bd9Sstevel@tonic-gate 	 * time. Otherwise we can't meet the failure detection time.
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	if (target->tg_crtt > pg->pg_probeint) {
8547c478bd9Sstevel@tonic-gate 		exception = check_exception_target(pii, target);
8557c478bd9Sstevel@tonic-gate 		if (exception) {
8567c478bd9Sstevel@tonic-gate 			/*
8577c478bd9Sstevel@tonic-gate 			 * This target is exceptionally slow. Don't use it
8587c478bd9Sstevel@tonic-gate 			 * for future probes. check_exception_target() has
8597c478bd9Sstevel@tonic-gate 			 * made sure that we have at least MIN_PROBE_TARGETS
8607c478bd9Sstevel@tonic-gate 			 * other active targets
8617c478bd9Sstevel@tonic-gate 			 */
8627c478bd9Sstevel@tonic-gate 			if (pii->pii_targets_are_routers) {
8637c478bd9Sstevel@tonic-gate 				/*
8647c478bd9Sstevel@tonic-gate 				 * This is a slow router, mark it as slow
8657c478bd9Sstevel@tonic-gate 				 * and don't use it for further probes. We
8667c478bd9Sstevel@tonic-gate 				 * don't delete it, since it will be populated
8677c478bd9Sstevel@tonic-gate 				 * again when we do a router scan. Hence we
8687c478bd9Sstevel@tonic-gate 				 * need to maintain extra state (unlike the
8697c478bd9Sstevel@tonic-gate 				 * host case below).  Mark it as TG_SLOW.
8707c478bd9Sstevel@tonic-gate 				 */
8717c478bd9Sstevel@tonic-gate 				if (target->tg_status == TG_ACTIVE)
8727c478bd9Sstevel@tonic-gate 					pii->pii_ntargets--;
8737c478bd9Sstevel@tonic-gate 				target->tg_status = TG_SLOW;
8747c478bd9Sstevel@tonic-gate 				target->tg_latime = gethrtime();
8757c478bd9Sstevel@tonic-gate 				target->tg_rtt_sa = -1;
8767c478bd9Sstevel@tonic-gate 				target->tg_crtt = 0;
8777c478bd9Sstevel@tonic-gate 				target->tg_rtt_sd = 0;
8787c478bd9Sstevel@tonic-gate 				if (pii->pii_target_next == target) {
8797c478bd9Sstevel@tonic-gate 					pii->pii_target_next =
8807c478bd9Sstevel@tonic-gate 					    target_next(target);
8817c478bd9Sstevel@tonic-gate 				}
8827c478bd9Sstevel@tonic-gate 			} else {
8837c478bd9Sstevel@tonic-gate 				/*
8847c478bd9Sstevel@tonic-gate 				 * the slow target is not a router, we can
8857c478bd9Sstevel@tonic-gate 				 * just delete it. Send an icmp multicast and
8867c478bd9Sstevel@tonic-gate 				 * pick the fastest responder that is not
8877c478bd9Sstevel@tonic-gate 				 * already an active target. target_delete()
8887c478bd9Sstevel@tonic-gate 				 * adjusts pii->pii_target_next
8897c478bd9Sstevel@tonic-gate 				 */
8907c478bd9Sstevel@tonic-gate 				target_delete(target);
891*e11c3f44Smeem 				probe(pii, PROBE_MULTI, cur_hrtime);
8927c478bd9Sstevel@tonic-gate 			}
8937c478bd9Sstevel@tonic-gate 		} else {
8947c478bd9Sstevel@tonic-gate 			/*
8957c478bd9Sstevel@tonic-gate 			 * We can't meet the failure detection time.
8967c478bd9Sstevel@tonic-gate 			 * Log a message, and update the detection time to
8977c478bd9Sstevel@tonic-gate 			 * whatever we can achieve.
8987c478bd9Sstevel@tonic-gate 			 */
8997c478bd9Sstevel@tonic-gate 			pg->pg_probeint = target->tg_crtt * NEXT_FDT_MULTIPLE;
9007c478bd9Sstevel@tonic-gate 			pg->pg_fdt = pg->pg_probeint * (NUM_PROBE_FAILS + 2);
9017c478bd9Sstevel@tonic-gate 			last_fdt_bumpup_time = gethrtime();
9027c478bd9Sstevel@tonic-gate 			if (pg != phyint_anongroup) {
9037c478bd9Sstevel@tonic-gate 				logerr("Cannot meet requested failure detection"
9047c478bd9Sstevel@tonic-gate 				    " time of %d ms on (%s %s) new failure"
9057c478bd9Sstevel@tonic-gate 				    " detection time for group \"%s\" is %d"
9067c478bd9Sstevel@tonic-gate 				    " ms\n", user_failure_detection_time,
9077c478bd9Sstevel@tonic-gate 				    AF_STR(pii->pii_af), pii->pii_name,
9087c478bd9Sstevel@tonic-gate 				    pg->pg_name, pg->pg_fdt);
9097c478bd9Sstevel@tonic-gate 			}
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 	} else if ((target->tg_crtt < (pg->pg_probeint / LOWER_FDT_TRIGGER)) &&
9127c478bd9Sstevel@tonic-gate 	    (user_failure_detection_time < pg->pg_fdt) &&
9137c478bd9Sstevel@tonic-gate 	    (last_fdt_bumpup_time + MIN_SETTLING_TIME < gethrtime())) {
9147c478bd9Sstevel@tonic-gate 		/*
9157c478bd9Sstevel@tonic-gate 		 * If the crtt has now dropped by a factor of LOWER_FDT_TRIGGER
9167c478bd9Sstevel@tonic-gate 		 * investigate if we can improve the failure detection time to
9177c478bd9Sstevel@tonic-gate 		 * meet whatever the user specified.
9187c478bd9Sstevel@tonic-gate 		 */
9197c478bd9Sstevel@tonic-gate 		if (check_pg_crtt_improved(pg)) {
9207c478bd9Sstevel@tonic-gate 			pg->pg_fdt = MAX(pg->pg_fdt / NEXT_FDT_MULTIPLE,
9217c478bd9Sstevel@tonic-gate 			    user_failure_detection_time);
9227c478bd9Sstevel@tonic-gate 			pg->pg_probeint = pg->pg_fdt / (NUM_PROBE_FAILS + 2);
9237c478bd9Sstevel@tonic-gate 			if (pg != phyint_anongroup) {
9247c478bd9Sstevel@tonic-gate 				logerr("Improved failure detection time %d ms "
9257c478bd9Sstevel@tonic-gate 				    "on (%s %s) for group \"%s\"\n", pg->pg_fdt,
9267c478bd9Sstevel@tonic-gate 				    AF_STR(pii->pii_af), pii->pii_name,
9277c478bd9Sstevel@tonic-gate 				    pg->pg_name);
9287c478bd9Sstevel@tonic-gate 			}
9297c478bd9Sstevel@tonic-gate 			if (user_failure_detection_time == pg->pg_fdt) {
9307c478bd9Sstevel@tonic-gate 				/* Avoid any truncation or rounding errors */
9317c478bd9Sstevel@tonic-gate 				pg->pg_probeint = user_probe_interval;
9327c478bd9Sstevel@tonic-gate 				/*
9337c478bd9Sstevel@tonic-gate 				 * No more rtt probes will be sent. The actual
9347c478bd9Sstevel@tonic-gate 				 * fdt has dropped to the user specified value.
9357c478bd9Sstevel@tonic-gate 				 * pii_fd_snxt_basetime and pii_snxt_basetime
9367c478bd9Sstevel@tonic-gate 				 * will be in sync henceforth.
9377c478bd9Sstevel@tonic-gate 				 */
9387c478bd9Sstevel@tonic-gate 				reset_snxt_basetimes();
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate out:
943*e11c3f44Smeem 	pr_statp = &pii->pii_probes[pr_ndx];
944*e11c3f44Smeem 	pr_statp->pr_hrtime_ackproc = cur_hrtime;
945*e11c3f44Smeem 	pr_statp->pr_hrtime_ackrecv = pr_statp->pr_hrtime_sent +
946*e11c3f44Smeem 	    (tv2ns(recv_tvp) - tv2ns(&pr_statp->pr_tv_sent));
947*e11c3f44Smeem 
948*e11c3f44Smeem 	probe_chstate(pr_statp, pii, PR_ACKED);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	/*
9517c478bd9Sstevel@tonic-gate 	 * Update pii->pii_rack, i.e. the sequence number of the last received
9527c478bd9Sstevel@tonic-gate 	 * probe response, based on the echo reply we have received now, if
9537c478bd9Sstevel@tonic-gate 	 * either of the following conditions are satisfied.
9547c478bd9Sstevel@tonic-gate 	 * a. pii_rack is outside the current receive window of
9557c478bd9Sstevel@tonic-gate 	 *    [pii->pii_snxt - PROBE_STATS_COUNT, pii->pii_snxt).
9567c478bd9Sstevel@tonic-gate 	 *    This means we have not received probe responses for a
9577c478bd9Sstevel@tonic-gate 	 *    long time, and the sequence number has wrapped around.
9587c478bd9Sstevel@tonic-gate 	 * b. pii_rack is within the current receive window and this echo
9597c478bd9Sstevel@tonic-gate 	 *    reply corresponds to the highest sequence number we have seen
9607c478bd9Sstevel@tonic-gate 	 *    so far.
9617c478bd9Sstevel@tonic-gate 	 */
9627c478bd9Sstevel@tonic-gate 	if (SEQ_GE(pii->pii_rack, pii->pii_snxt) ||
9637c478bd9Sstevel@tonic-gate 	    SEQ_LT(pii->pii_rack, pii->pii_snxt - PROBE_STATS_COUNT) ||
9647c478bd9Sstevel@tonic-gate 	    SEQ_GT(pr_icmp_seq, pii->pii_rack)) {
9657c478bd9Sstevel@tonic-gate 		pii->pii_rack = pr_icmp_seq;
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate /*
9707c478bd9Sstevel@tonic-gate  * Returns true if seq is the highest unacknowledged seq for target tg
9717c478bd9Sstevel@tonic-gate  * else returns false
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate static boolean_t
9747c478bd9Sstevel@tonic-gate highest_ack_tg(uint16_t seq, struct target *tg)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	struct phyint_instance *pii;
9777c478bd9Sstevel@tonic-gate 	int	 pr_ndx;
9787c478bd9Sstevel@tonic-gate 	uint16_t pr_seq;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	pii = tg->tg_phyint_inst;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * Get the seq number of the most recent probe sent so far,
9847c478bd9Sstevel@tonic-gate 	 * and also get the corresponding probe index in the probe stats
9857c478bd9Sstevel@tonic-gate 	 * array.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
9887c478bd9Sstevel@tonic-gate 	pr_seq = pii->pii_snxt;
9897c478bd9Sstevel@tonic-gate 	pr_seq--;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	/*
9927c478bd9Sstevel@tonic-gate 	 * Start from the most recent probe and walk back, trying to find
9937c478bd9Sstevel@tonic-gate 	 * an acked probe corresponding to target tg.
9947c478bd9Sstevel@tonic-gate 	 */
9957c478bd9Sstevel@tonic-gate 	for (; pr_ndx != pii->pii_probe_next;
9967c478bd9Sstevel@tonic-gate 	    pr_ndx = PROBE_INDEX_PREV(pr_ndx), pr_seq--) {
9977c478bd9Sstevel@tonic-gate 		if (pii->pii_probes[pr_ndx].pr_target == tg &&
9987c478bd9Sstevel@tonic-gate 		    pii->pii_probes[pr_ndx].pr_status == PR_ACKED) {
9997c478bd9Sstevel@tonic-gate 			if (SEQ_GT(pr_seq, seq))
10007c478bd9Sstevel@tonic-gate 				return (_B_FALSE);
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	return (_B_TRUE);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate /*
10077c478bd9Sstevel@tonic-gate  * Check whether the crtt for the group has improved by a factor of
10087c478bd9Sstevel@tonic-gate  * LOWER_FDT_TRIGGER.  Small crtt improvements are ignored to avoid failure
10097c478bd9Sstevel@tonic-gate  * detection time flapping in the face of small crtt changes.
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate static boolean_t
10127c478bd9Sstevel@tonic-gate check_pg_crtt_improved(struct phyint_group *pg)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	struct	phyint *pi;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE)
10177c478bd9Sstevel@tonic-gate 		logdebug("check_pg_crtt_improved()\n");
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/*
10207c478bd9Sstevel@tonic-gate 	 * The crtt for the group is only improved if each phyint_instance
10217c478bd9Sstevel@tonic-gate 	 * for both ipv4 and ipv6 is improved.
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
10247c478bd9Sstevel@tonic-gate 		if (!check_pii_crtt_improved(pi->pi_v4) ||
10257c478bd9Sstevel@tonic-gate 		    !check_pii_crtt_improved(pi->pi_v6))
10267c478bd9Sstevel@tonic-gate 			return (_B_FALSE);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	return (_B_TRUE);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate  * Check whether the crtt has improved substantially on this phyint_instance.
10347c478bd9Sstevel@tonic-gate  * Returns _B_TRUE if there's no crtt information available, because pii
10357c478bd9Sstevel@tonic-gate  * is NULL or the phyint_instance is not capable of probing.
10367c478bd9Sstevel@tonic-gate  */
10377c478bd9Sstevel@tonic-gate boolean_t
10387c478bd9Sstevel@tonic-gate check_pii_crtt_improved(struct phyint_instance *pii) {
10397c478bd9Sstevel@tonic-gate 	struct 	target *tg;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	if (pii == NULL)
10427c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (!PROBE_CAPABLE(pii) ||
10457c478bd9Sstevel@tonic-gate 	    pii->pii_phyint->pi_state == PI_FAILED)
10467c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
10497c478bd9Sstevel@tonic-gate 		if (tg->tg_status != TG_ACTIVE)
10507c478bd9Sstevel@tonic-gate 			continue;
10517c478bd9Sstevel@tonic-gate 		if (tg->tg_crtt > (pii->pii_phyint->pi_group->pg_probeint /
10527c478bd9Sstevel@tonic-gate 		    LOWER_FDT_TRIGGER)) {
10537c478bd9Sstevel@tonic-gate 			return (_B_FALSE);
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	return (_B_TRUE);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * This target responds very slowly to probes. The target's crtt exceeds
10627c478bd9Sstevel@tonic-gate  * the probe interval of its group. Compare against other targets
10637c478bd9Sstevel@tonic-gate  * and determine if this target is an exception, if so return true, else false
10647c478bd9Sstevel@tonic-gate  */
10657c478bd9Sstevel@tonic-gate static boolean_t
10667c478bd9Sstevel@tonic-gate check_exception_target(struct phyint_instance *pii, struct target *target)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate 	struct	target *tg;
10697c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
10727c478bd9Sstevel@tonic-gate 		logdebug("check_exception_target(%s %s target %s)\n",
10737c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
10747c478bd9Sstevel@tonic-gate 		    pr_addr(pii->pii_af, target->tg_address,
107528f13c35Srk 		    abuf, sizeof (abuf)));
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/*
10797c478bd9Sstevel@tonic-gate 	 * We should have at least MIN_PROBE_TARGETS + 1 good targets now,
10807c478bd9Sstevel@tonic-gate 	 * to make a good judgement. Otherwise don't drop this target.
10817c478bd9Sstevel@tonic-gate 	 */
10827c478bd9Sstevel@tonic-gate 	if (pii->pii_ntargets <  MIN_PROBE_TARGETS + 1)
10837c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/*
10867c478bd9Sstevel@tonic-gate 	 * Determine whether only this particular target is slow.
10877c478bd9Sstevel@tonic-gate 	 * We know that this target's crtt exceeds the group's probe interval.
10887c478bd9Sstevel@tonic-gate 	 * If all other active targets have a
10897c478bd9Sstevel@tonic-gate 	 * crtt < (this group's probe interval) / EXCEPTION_FACTOR,
10907c478bd9Sstevel@tonic-gate 	 * then this target is considered slow.
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
10937c478bd9Sstevel@tonic-gate 		if (tg != target && tg->tg_status == TG_ACTIVE) {
10947c478bd9Sstevel@tonic-gate 			if (tg->tg_crtt >
10957c478bd9Sstevel@tonic-gate 			    pii->pii_phyint->pi_group->pg_probeint /
10967c478bd9Sstevel@tonic-gate 			    EXCEPTION_FACTOR) {
10977c478bd9Sstevel@tonic-gate 				return (_B_FALSE);
10987c478bd9Sstevel@tonic-gate 			}
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	return (_B_TRUE);
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate /*
11067c478bd9Sstevel@tonic-gate  * Update the target list. The icmp all hosts multicast has given us
11077c478bd9Sstevel@tonic-gate  * some host to which we can send probes. If we already have sufficient
11087c478bd9Sstevel@tonic-gate  * targets, discard it.
11097c478bd9Sstevel@tonic-gate  */
11107c478bd9Sstevel@tonic-gate static void
11117c478bd9Sstevel@tonic-gate incoming_mcast_reply(struct phyint_instance *pii, struct pr_icmp *reply,
11127c478bd9Sstevel@tonic-gate     struct in6_addr fromaddr)
11137c478bd9Sstevel@tonic-gate /* ARGSUSED */
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	int af;
11167c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
11177c478bd9Sstevel@tonic-gate 	struct phyint *pi;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
11207c478bd9Sstevel@tonic-gate 		logdebug("incoming_mcast_reply(%s %s %s)\n",
11217c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
11227c478bd9Sstevel@tonic-gate 		    pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf)));
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/*
11267c478bd9Sstevel@tonic-gate 	 * Using host targets is a fallback mechanism. If we have
11277c478bd9Sstevel@tonic-gate 	 * found a router, don't add this host target. If we already
11287c478bd9Sstevel@tonic-gate 	 * know MAX_PROBE_TARGETS, don't add another target.
11297c478bd9Sstevel@tonic-gate 	 */
11307c478bd9Sstevel@tonic-gate 	assert(pii->pii_ntargets <= MAX_PROBE_TARGETS);
11317c478bd9Sstevel@tonic-gate 	if (pii->pii_targets != NULL) {
11327c478bd9Sstevel@tonic-gate 		if (pii->pii_targets_are_routers ||
11337c478bd9Sstevel@tonic-gate 		    (pii->pii_ntargets == MAX_PROBE_TARGETS)) {
11347c478bd9Sstevel@tonic-gate 			return;
11357c478bd9Sstevel@tonic-gate 		}
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&fromaddr) ||
11397c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED_ANY(&fromaddr)) {
11407c478bd9Sstevel@tonic-gate 		/*
11417c478bd9Sstevel@tonic-gate 		 * Guard against response from 0.0.0.0
11427c478bd9Sstevel@tonic-gate 		 * and ::. Log a trace message
11437c478bd9Sstevel@tonic-gate 		 */
11447c478bd9Sstevel@tonic-gate 		logtrace("probe response from %s on %s\n",
11457c478bd9Sstevel@tonic-gate 		    pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf)),
11467c478bd9Sstevel@tonic-gate 		    pii->pii_name);
11477c478bd9Sstevel@tonic-gate 		return;
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/*
11517c478bd9Sstevel@tonic-gate 	 * This address is one of our own, so reject this address as a
11527c478bd9Sstevel@tonic-gate 	 * valid probe target.
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	af = pii->pii_af;
115587e66ffcSrk 	if (own_address(fromaddr))
11567c478bd9Sstevel@tonic-gate 		return;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/*
11597c478bd9Sstevel@tonic-gate 	 * If the phyint is part a named group, then add the address to all
11607c478bd9Sstevel@tonic-gate 	 * members of the group.  Otherwise, add the address only to the
11617c478bd9Sstevel@tonic-gate 	 * phyint itself, since other phyints in the anongroup may not be on
11627c478bd9Sstevel@tonic-gate 	 * the same subnet.
11637c478bd9Sstevel@tonic-gate 	 */
11647c478bd9Sstevel@tonic-gate 	pi = pii->pii_phyint;
11657c478bd9Sstevel@tonic-gate 	if (pi->pi_group == phyint_anongroup) {
11667c478bd9Sstevel@tonic-gate 		target_add(pii, fromaddr, _B_FALSE);
11677c478bd9Sstevel@tonic-gate 	} else {
11687c478bd9Sstevel@tonic-gate 		pi = pi->pi_group->pg_phyint;
11697c478bd9Sstevel@tonic-gate 		for (; pi != NULL; pi = pi->pi_pgnext)
11707c478bd9Sstevel@tonic-gate 			target_add(PHYINT_INSTANCE(pi, af), fromaddr, _B_FALSE);
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate /*
11757c478bd9Sstevel@tonic-gate  * Compute CRTT given an existing scaled average, scaled deviation estimate
11767c478bd9Sstevel@tonic-gate  * and a new rtt time.  The formula is from Jacobson and Karels'
11777c478bd9Sstevel@tonic-gate  * "Congestion Avoidance and Control" in SIGCOMM '88.  The variable names
11787c478bd9Sstevel@tonic-gate  * are the same as those in Appendix A.2 of that paper.
11797c478bd9Sstevel@tonic-gate  *
11807c478bd9Sstevel@tonic-gate  * m = new measurement
11817c478bd9Sstevel@tonic-gate  * sa = scaled RTT average (8 * average estimates)
11827c478bd9Sstevel@tonic-gate  * sv = scaled mean deviation (mdev) of RTT (4 * deviation estimates).
11837c478bd9Sstevel@tonic-gate  * crtt = Conservative round trip time. Used to determine whether probe
11847c478bd9Sstevel@tonic-gate  * has timed out.
11857c478bd9Sstevel@tonic-gate  *
11867c478bd9Sstevel@tonic-gate  * New scaled average and deviation are passed back via sap and svp
11877c478bd9Sstevel@tonic-gate  */
1188*e11c3f44Smeem static int64_t
1189*e11c3f44Smeem compute_crtt(int64_t *sap, int64_t *svp, int64_t m)
11907c478bd9Sstevel@tonic-gate {
1191*e11c3f44Smeem 	int64_t sa = *sap;
1192*e11c3f44Smeem 	int64_t sv = *svp;
1193*e11c3f44Smeem 	int64_t crtt;
1194*e11c3f44Smeem 	int64_t saved_m = m;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	assert(*sap >= -1);
11977c478bd9Sstevel@tonic-gate 	assert(*svp >= 0);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	if (sa != -1) {
12007c478bd9Sstevel@tonic-gate 		/*
12017c478bd9Sstevel@tonic-gate 		 * Update average estimator:
12027c478bd9Sstevel@tonic-gate 		 *	new rtt = old rtt + 1/8 Error
12037c478bd9Sstevel@tonic-gate 		 *	    where Error = m - old rtt
12047c478bd9Sstevel@tonic-gate 		 *	i.e. 8 * new rtt = 8 * old rtt + Error
12057c478bd9Sstevel@tonic-gate 		 *	i.e. new sa =  old sa + Error
12067c478bd9Sstevel@tonic-gate 		 */
12077c478bd9Sstevel@tonic-gate 		m -= sa >> 3;		/* m is now Error in estimate. */
12087c478bd9Sstevel@tonic-gate 		if ((sa += m) < 0) {
12097c478bd9Sstevel@tonic-gate 			/* Don't allow the smoothed average to be negative. */
12107c478bd9Sstevel@tonic-gate 			sa = 0;
12117c478bd9Sstevel@tonic-gate 		}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		/*
12147c478bd9Sstevel@tonic-gate 		 * Update deviation estimator:
12157c478bd9Sstevel@tonic-gate 		 *	new mdev =  old mdev + 1/4 (abs(Error) - old mdev)
12167c478bd9Sstevel@tonic-gate 		 *	i.e. 4 * new mdev = 4 * old mdev +
12177c478bd9Sstevel@tonic-gate 		 *		(abs(Error) - old mdev)
12187c478bd9Sstevel@tonic-gate 		 * 	i.e. new sv = old sv + (abs(Error) - old mdev)
12197c478bd9Sstevel@tonic-gate 		 */
12207c478bd9Sstevel@tonic-gate 		if (m < 0)
12217c478bd9Sstevel@tonic-gate 			m = -m;
12227c478bd9Sstevel@tonic-gate 		m -= sv >> 2;
12237c478bd9Sstevel@tonic-gate 		sv += m;
12247c478bd9Sstevel@tonic-gate 	} else {
12257c478bd9Sstevel@tonic-gate 		/* Initialization. This is the first response received. */
12267c478bd9Sstevel@tonic-gate 		sa = (m << 3);
12277c478bd9Sstevel@tonic-gate 		sv = (m << 1);
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	crtt = (sa >> 3) + sv;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE) {
1233*e11c3f44Smeem 		logerr("compute_crtt: m = %lld sa = %lld, sv = %lld -> "
1234*e11c3f44Smeem 		    "crtt = %lld\n", saved_m, sa, sv, crtt);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	*sap = sa;
12387c478bd9Sstevel@tonic-gate 	*svp = sv;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/*
12417c478bd9Sstevel@tonic-gate 	 * CRTT = average estimates  + 4 * deviation estimates
12427c478bd9Sstevel@tonic-gate 	 *	= sa / 8 + sv
12437c478bd9Sstevel@tonic-gate 	 */
12447c478bd9Sstevel@tonic-gate 	return (crtt);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate static void
1248*e11c3f44Smeem pi_set_crtt(struct target *tg, int64_t m, boolean_t is_probe_uni)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate 	struct phyint_instance *pii = tg->tg_phyint_inst;
12517c478bd9Sstevel@tonic-gate 	int probe_interval = pii->pii_phyint->pi_group->pg_probeint;
1252*e11c3f44Smeem 	int64_t sa = tg->tg_rtt_sa;
1253*e11c3f44Smeem 	int64_t sv = tg->tg_rtt_sd;
12547c478bd9Sstevel@tonic-gate 	int new_crtt;
12557c478bd9Sstevel@tonic-gate 	int i;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if (debug & D_PROBE)
1258*e11c3f44Smeem 		logdebug("pi_set_crtt: target -  m %lld\n", m);
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/* store the round trip time, in case we need to defer computation */
12617c478bd9Sstevel@tonic-gate 	tg->tg_deferred[tg->tg_num_deferred] = m;
12627c478bd9Sstevel@tonic-gate 
1263*e11c3f44Smeem 	new_crtt = ns2ms(compute_crtt(&sa, &sv, m));
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/*
12667c478bd9Sstevel@tonic-gate 	 * If this probe's round trip time would singlehandedly cause an
12677c478bd9Sstevel@tonic-gate 	 * increase in the group's probe interval consider it suspect.
12687c478bd9Sstevel@tonic-gate 	 */
12697c478bd9Sstevel@tonic-gate 	if ((new_crtt > probe_interval) && is_probe_uni) {
12707c478bd9Sstevel@tonic-gate 		if (debug & D_PROBE) {
12717c478bd9Sstevel@tonic-gate 			logdebug("Received a suspect probe on %s, new_crtt ="
12727c478bd9Sstevel@tonic-gate 			    " %d, probe_interval = %d, num_deferred = %d\n",
12737c478bd9Sstevel@tonic-gate 			    pii->pii_probe_logint->li_name, new_crtt,
12747c478bd9Sstevel@tonic-gate 			    probe_interval, tg->tg_num_deferred);
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		/*
12787c478bd9Sstevel@tonic-gate 		 * If we've deferred as many rtts as we plan on deferring, then
12797c478bd9Sstevel@tonic-gate 		 * assume the link really did slow down and process all queued
12807c478bd9Sstevel@tonic-gate 		 * rtts
12817c478bd9Sstevel@tonic-gate 		 */
12827c478bd9Sstevel@tonic-gate 		if (tg->tg_num_deferred == MAXDEFERREDRTT) {
12837c478bd9Sstevel@tonic-gate 			if (debug & D_PROBE) {
12847c478bd9Sstevel@tonic-gate 				logdebug("Received MAXDEFERREDRTT probes which "
12857c478bd9Sstevel@tonic-gate 				    "would cause an increased probe_interval.  "
12867c478bd9Sstevel@tonic-gate 				    "Integrating queued rtt data points.\n");
12877c478bd9Sstevel@tonic-gate 			}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 			for (i = 0; i <= tg->tg_num_deferred; i++) {
1290*e11c3f44Smeem 				tg->tg_crtt = ns2ms(compute_crtt(&tg->tg_rtt_sa,
1291*e11c3f44Smeem 				    &tg->tg_rtt_sd, tg->tg_deferred[i]));
12927c478bd9Sstevel@tonic-gate 			}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 			tg->tg_num_deferred = 0;
12957c478bd9Sstevel@tonic-gate 		} else {
12967c478bd9Sstevel@tonic-gate 			tg->tg_num_deferred++;
12977c478bd9Sstevel@tonic-gate 		}
12987c478bd9Sstevel@tonic-gate 		return;
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/*
13027c478bd9Sstevel@tonic-gate 	 * If this is a normal probe, or an RTT probe that would lead to a
13037c478bd9Sstevel@tonic-gate 	 * reduced CRTT, then update our CRTT data.  Further, if this was
13047c478bd9Sstevel@tonic-gate 	 * a normal probe, pitch any deferred probes since our probes are
13057c478bd9Sstevel@tonic-gate 	 * again being answered within our CRTT estimates.
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	if (is_probe_uni || new_crtt < tg->tg_crtt) {
13087c478bd9Sstevel@tonic-gate 		tg->tg_rtt_sa = sa;
13097c478bd9Sstevel@tonic-gate 		tg->tg_rtt_sd = sv;
13107c478bd9Sstevel@tonic-gate 		tg->tg_crtt = new_crtt;
13117c478bd9Sstevel@tonic-gate 		if (is_probe_uni)
13127c478bd9Sstevel@tonic-gate 			tg->tg_num_deferred = 0;
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate /*
13177c478bd9Sstevel@tonic-gate  * Return a pointer to the specified option buffer.
13187c478bd9Sstevel@tonic-gate  * If not found return NULL.
13197c478bd9Sstevel@tonic-gate  */
13207c478bd9Sstevel@tonic-gate static void *
1321*e11c3f44Smeem find_ancillary(struct msghdr *msg, int cmsg_level, int cmsg_type)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
13267c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
1327*e11c3f44Smeem 		if (cmsg->cmsg_level == cmsg_level &&
13287c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == cmsg_type) {
13297c478bd9Sstevel@tonic-gate 			return (CMSG_DATA(cmsg));
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 	return (NULL);
13337c478bd9Sstevel@tonic-gate }
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate /*
1336*e11c3f44Smeem  * Try to activate another INACTIVE interface in the same group as `pi'.
1337*e11c3f44Smeem  * Prefer STANDBY INACTIVE to just INACTIVE.
13387c478bd9Sstevel@tonic-gate  */
13397c478bd9Sstevel@tonic-gate void
1340*e11c3f44Smeem phyint_activate_another(struct phyint *pi)
13417c478bd9Sstevel@tonic-gate {
1342*e11c3f44Smeem 	struct phyint *pi2;
1343*e11c3f44Smeem 	struct phyint *inactivepi = NULL;
13447c478bd9Sstevel@tonic-gate 
1345*e11c3f44Smeem 	if (pi->pi_group == phyint_anongroup)
1346*e11c3f44Smeem 		return;
13477c478bd9Sstevel@tonic-gate 
1348*e11c3f44Smeem 	for (pi2 = pi->pi_group->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
1349*e11c3f44Smeem 		if (pi == pi2 || pi2->pi_state != PI_RUNNING ||
1350*e11c3f44Smeem 		    !(pi2->pi_flags & IFF_INACTIVE))
1351*e11c3f44Smeem 			continue;
1352*e11c3f44Smeem 
1353*e11c3f44Smeem 		inactivepi = pi2;
1354*e11c3f44Smeem 		if (pi2->pi_flags & IFF_STANDBY)
1355*e11c3f44Smeem 			break;
1356*e11c3f44Smeem 	}
1357*e11c3f44Smeem 
1358*e11c3f44Smeem 	if (inactivepi != NULL)
1359*e11c3f44Smeem 		(void) change_pif_flags(inactivepi, 0, IFF_INACTIVE);
1360*e11c3f44Smeem }
1361*e11c3f44Smeem 
1362*e11c3f44Smeem /*
1363*e11c3f44Smeem  * Transition a phyint back to PI_RUNNING (from PI_FAILED or PI_OFFLINE).  The
1364*e11c3f44Smeem  * caller must ensure that the transition is appropriate.  Clears IFF_OFFLINE
1365*e11c3f44Smeem  * or IFF_FAILED, as appropriate.  Also sets IFF_INACTIVE on this or other
1366*e11c3f44Smeem  * interfaces as appropriate (see comment below).  Finally, also updates the
1367*e11c3f44Smeem  * phyint's group state to account for the change.
1368*e11c3f44Smeem  */
1369*e11c3f44Smeem void
1370*e11c3f44Smeem phyint_transition_to_running(struct phyint *pi)
1371*e11c3f44Smeem {
1372*e11c3f44Smeem 	struct phyint *pi2;
1373*e11c3f44Smeem 	struct phyint *actstandbypi = NULL;
1374*e11c3f44Smeem 	uint_t nactive = 0, nnonstandby = 0;
1375*e11c3f44Smeem 	boolean_t onlining = (pi->pi_state == PI_OFFLINE);
1376*e11c3f44Smeem 	uint64_t set, clear;
1377*e11c3f44Smeem 
1378*e11c3f44Smeem 	/*
1379*e11c3f44Smeem 	 * The interface is running again, but should it or another interface
1380*e11c3f44Smeem 	 * in the group end up INACTIVE?  There are three cases:
1381*e11c3f44Smeem 	 *
1382*e11c3f44Smeem 	 * 1. If it's a STANDBY interface, it should be end up INACTIVE if
1383*e11c3f44Smeem 	 *    the group is operating at capacity (i.e., there are at least as
1384*e11c3f44Smeem 	 *    many active interfaces as non-STANDBY interfaces in the group).
1385*e11c3f44Smeem 	 *    No other interfaces should be changed.
1386*e11c3f44Smeem 	 *
1387*e11c3f44Smeem 	 * 2. If it's a non-STANDBY interface and we're onlining it or
1388*e11c3f44Smeem 	 *    FAILBACK is enabled, then it should *not* end up INACTIVE.
1389*e11c3f44Smeem 	 *    Further, if the group is above capacity as a result of this
1390*e11c3f44Smeem 	 *    interface, then an active STANDBY interface in the group should
1391*e11c3f44Smeem 	 *    end up INACTIVE.
1392*e11c3f44Smeem 	 *
1393*e11c3f44Smeem 	 * 3. If it's a non-STANDBY interface, we're repairing it, and
1394*e11c3f44Smeem 	 *    FAILBACK is disabled, then it should end up INACTIVE *unless*
1395*e11c3f44Smeem 	 *    the group was failed (in which case we have no choice but to
1396*e11c3f44Smeem 	 *    use it).  No other interfaces should be changed.
1397*e11c3f44Smeem 	 */
1398*e11c3f44Smeem 	if (pi->pi_group != phyint_anongroup) {
1399*e11c3f44Smeem 		pi2 = pi->pi_group->pg_phyint;
1400*e11c3f44Smeem 		for (; pi2 != NULL; pi2 = pi2->pi_pgnext) {
1401*e11c3f44Smeem 			if (!(pi2->pi_flags & IFF_STANDBY))
1402*e11c3f44Smeem 				nnonstandby++;
1403*e11c3f44Smeem 
1404*e11c3f44Smeem 			if (pi2->pi_state == PI_RUNNING) {
1405*e11c3f44Smeem 				if (!(pi2->pi_flags & IFF_INACTIVE)) {
1406*e11c3f44Smeem 					nactive++;
1407*e11c3f44Smeem 					if (pi2->pi_flags & IFF_STANDBY)
1408*e11c3f44Smeem 						actstandbypi = pi2;
1409*e11c3f44Smeem 				}
14107c478bd9Sstevel@tonic-gate 			}
14117c478bd9Sstevel@tonic-gate 		}
1412*e11c3f44Smeem 	}
14137c478bd9Sstevel@tonic-gate 
1414*e11c3f44Smeem 	set = 0;
1415*e11c3f44Smeem 	clear = (onlining ? IFF_OFFLINE : IFF_FAILED);
14167c478bd9Sstevel@tonic-gate 
1417*e11c3f44Smeem 	if (pi->pi_flags & IFF_STANDBY) {			/* case 1 */
1418*e11c3f44Smeem 		if (nactive >= nnonstandby)
1419*e11c3f44Smeem 			set |= IFF_INACTIVE;
1420*e11c3f44Smeem 		else
1421*e11c3f44Smeem 			clear |= IFF_INACTIVE;
1422*e11c3f44Smeem 	} else if (onlining || failback_enabled) {		/* case 2 */
1423*e11c3f44Smeem 		if (nactive >= nnonstandby && actstandbypi != NULL)
1424*e11c3f44Smeem 			(void) change_pif_flags(actstandbypi, IFF_INACTIVE, 0);
1425*e11c3f44Smeem 	} else if (!GROUP_FAILED(pi->pi_group)) {		/* case 3 */
1426*e11c3f44Smeem 		set |= IFF_INACTIVE;
1427*e11c3f44Smeem 	}
1428*e11c3f44Smeem 	(void) change_pif_flags(pi, set, clear);
1429*e11c3f44Smeem 
1430*e11c3f44Smeem 	phyint_chstate(pi, PI_RUNNING);
1431*e11c3f44Smeem 
1432*e11c3f44Smeem 	/*
1433*e11c3f44Smeem 	 * Update the group state to account for the change.
1434*e11c3f44Smeem 	 */
1435*e11c3f44Smeem 	phyint_group_refresh_state(pi->pi_group);
1436*e11c3f44Smeem }
1437*e11c3f44Smeem 
1438*e11c3f44Smeem /*
1439*e11c3f44Smeem  * See if a previously failed interface has started working again.
1440*e11c3f44Smeem  */
1441*e11c3f44Smeem void
1442*e11c3f44Smeem phyint_check_for_repair(struct phyint *pi)
1443*e11c3f44Smeem {
1444*e11c3f44Smeem 	if (!phyint_repaired(pi))
1445*e11c3f44Smeem 		return;
1446*e11c3f44Smeem 
1447*e11c3f44Smeem 	if (pi->pi_group == phyint_anongroup) {
1448*e11c3f44Smeem 		logerr("IP interface repair detected on %s\n", pi->pi_name);
1449*e11c3f44Smeem 	} else {
1450*e11c3f44Smeem 		logerr("IP interface repair detected on %s of group %s\n",
1451*e11c3f44Smeem 		    pi->pi_name, pi->pi_group->pg_name);
14527c478bd9Sstevel@tonic-gate 	}
1453*e11c3f44Smeem 
1454*e11c3f44Smeem 	/*
1455*e11c3f44Smeem 	 * If the interface is PI_OFFLINE, it can't be made PI_RUNNING yet.
1456*e11c3f44Smeem 	 * So just clear IFF_OFFLINE and defer phyint_transition_to_running()
1457*e11c3f44Smeem 	 * until it is brought back online.
1458*e11c3f44Smeem 	 */
1459*e11c3f44Smeem 	if (pi->pi_state == PI_OFFLINE) {
1460*e11c3f44Smeem 		(void) change_pif_flags(pi, 0, IFF_FAILED);
1461*e11c3f44Smeem 		return;
1462*e11c3f44Smeem 	}
1463*e11c3f44Smeem 
1464*e11c3f44Smeem 	phyint_transition_to_running(pi);	/* calls phyint_chstate() */
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate /*
1468*e11c3f44Smeem  * See if an interface has failed, or if the whole group of interfaces has
1469*e11c3f44Smeem  * failed.
14707c478bd9Sstevel@tonic-gate  */
14717c478bd9Sstevel@tonic-gate static void
14727c478bd9Sstevel@tonic-gate phyint_inst_check_for_failure(struct phyint_instance *pii)
14737c478bd9Sstevel@tonic-gate {
1474*e11c3f44Smeem 	struct phyint	*pi = pii->pii_phyint;
1475*e11c3f44Smeem 	struct phyint	*pi2;
1476*e11c3f44Smeem 	boolean_t	was_active;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	switch (failure_state(pii)) {
14797c478bd9Sstevel@tonic-gate 	case PHYINT_FAILURE:
1480*e11c3f44Smeem 		was_active = ((pi->pi_flags & IFF_INACTIVE) == 0);
1481*e11c3f44Smeem 
1482*e11c3f44Smeem 		(void) change_pif_flags(pi, IFF_FAILED, IFF_INACTIVE);
14837c478bd9Sstevel@tonic-gate 		if (pi->pi_group == phyint_anongroup) {
1484*e11c3f44Smeem 			logerr("IP interface failure detected on %s\n",
1485*e11c3f44Smeem 			    pii->pii_name);
14867c478bd9Sstevel@tonic-gate 		} else {
1487*e11c3f44Smeem 			logerr("IP interface failure detected on %s of group"
1488*e11c3f44Smeem 			    " %s\n", pii->pii_name, pi->pi_group->pg_name);
14897c478bd9Sstevel@tonic-gate 		}
1490*e11c3f44Smeem 
14917c478bd9Sstevel@tonic-gate 		/*
1492*e11c3f44Smeem 		 * If the interface is offline, the state change will be
1493*e11c3f44Smeem 		 * noted when it comes back online.
14947c478bd9Sstevel@tonic-gate 		 */
14957c478bd9Sstevel@tonic-gate 		if (pi->pi_state != PI_OFFLINE) {
1496*e11c3f44Smeem 			/*
1497*e11c3f44Smeem 			 * If the failed interface was active, activate
1498*e11c3f44Smeem 			 * another INACTIVE interface in the group if
1499*e11c3f44Smeem 			 * possible.  (If the interface is PI_OFFLINE,
1500*e11c3f44Smeem 			 * we already activated another.)
1501*e11c3f44Smeem 			 */
1502*e11c3f44Smeem 			if (was_active)
1503*e11c3f44Smeem 				phyint_activate_another(pi);
1504*e11c3f44Smeem 
15057c478bd9Sstevel@tonic-gate 			phyint_chstate(pi, PI_FAILED);
15067c478bd9Sstevel@tonic-gate 			reset_crtt_all(pi);
15077c478bd9Sstevel@tonic-gate 		}
15087c478bd9Sstevel@tonic-gate 		break;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	case GROUP_FAILURE:
1511*e11c3f44Smeem 		pi2 = pi->pi_group->pg_phyint;
1512*e11c3f44Smeem 		for (; pi2 != NULL; pi2 = pi2->pi_pgnext) {
1513*e11c3f44Smeem 			(void) change_pif_flags(pi2, IFF_FAILED, IFF_INACTIVE);
1514*e11c3f44Smeem 			if (pi2->pi_state == PI_OFFLINE) /* see comment above */
15157c478bd9Sstevel@tonic-gate 				continue;
15167c478bd9Sstevel@tonic-gate 
1517*e11c3f44Smeem 			reset_crtt_all(pi2);
15187c478bd9Sstevel@tonic-gate 			/*
1519*e11c3f44Smeem 			 * In the case of host targets, we would have flushed
1520*e11c3f44Smeem 			 * the targets, and gone to PI_NOTARGETS state.
15217c478bd9Sstevel@tonic-gate 			 */
15227c478bd9Sstevel@tonic-gate 			if (pi2->pi_state == PI_RUNNING)
152349df4566Sethindra 				phyint_chstate(pi2, PI_FAILED);
15247c478bd9Sstevel@tonic-gate 		}
15257c478bd9Sstevel@tonic-gate 		break;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	default:
15287c478bd9Sstevel@tonic-gate 		break;
15297c478bd9Sstevel@tonic-gate 	}
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate  * Determines if any timeout event has occurred and returns the number of
15347c478bd9Sstevel@tonic-gate  * milliseconds until the next timeout event for the phyint. Returns
15357c478bd9Sstevel@tonic-gate  * TIMER_INFINITY for "never".
15367c478bd9Sstevel@tonic-gate  */
15377c478bd9Sstevel@tonic-gate uint_t
15387c478bd9Sstevel@tonic-gate phyint_inst_timer(struct phyint_instance *pii)
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate 	int 	pr_ndx;
15417c478bd9Sstevel@tonic-gate 	uint_t	timeout;
15427c478bd9Sstevel@tonic-gate 	struct	target	*cur_tg;
15437c478bd9Sstevel@tonic-gate 	struct	probe_stats *pr_statp;
15447c478bd9Sstevel@tonic-gate 	struct	phyint_instance *pii_other;
15457c478bd9Sstevel@tonic-gate 	struct	phyint *pi;
15467c478bd9Sstevel@tonic-gate 	int	valid_unack_count;
15477c478bd9Sstevel@tonic-gate 	int	i;
15487c478bd9Sstevel@tonic-gate 	int	interval;
15497c478bd9Sstevel@tonic-gate 	uint_t	check_time;
15507c478bd9Sstevel@tonic-gate 	uint_t	cur_time;
15517c478bd9Sstevel@tonic-gate 	hrtime_t cur_hrtime;
15527c478bd9Sstevel@tonic-gate 	int	probe_interval = pii->pii_phyint->pi_group->pg_probeint;
15537c478bd9Sstevel@tonic-gate 
1554*e11c3f44Smeem 	cur_hrtime = gethrtime();
1555*e11c3f44Smeem 	cur_time = ns2ms(cur_hrtime);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	if (debug & D_TIMER) {
15587c478bd9Sstevel@tonic-gate 		logdebug("phyint_inst_timer(%s %s)\n",
15597c478bd9Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	pii_other = phyint_inst_other(pii);
15637c478bd9Sstevel@tonic-gate 	if (!PROBE_ENABLED(pii) && !PROBE_ENABLED(pii_other)) {
15647c478bd9Sstevel@tonic-gate 		/*
15657c478bd9Sstevel@tonic-gate 		 * Check to see if we're here due to link up/down flapping; If
15667c478bd9Sstevel@tonic-gate 		 * enough time has passed, then try to bring the interface
15677c478bd9Sstevel@tonic-gate 		 * back up; otherwise, schedule a timer to bring it back up
15687c478bd9Sstevel@tonic-gate 		 * when enough time *has* elapsed.
15697c478bd9Sstevel@tonic-gate 		 */
15707c478bd9Sstevel@tonic-gate 		pi = pii->pii_phyint;
15717c478bd9Sstevel@tonic-gate 		if (pi->pi_state == PI_FAILED && LINK_UP(pi)) {
15727c478bd9Sstevel@tonic-gate 			check_time = pi->pi_whenup[pi->pi_whendx] + MSEC_PERMIN;
15737c478bd9Sstevel@tonic-gate 			if (check_time > cur_time)
15747c478bd9Sstevel@tonic-gate 				return (check_time - cur_time);
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 			phyint_check_for_repair(pi);
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	/*
158106cdd167Smeem 	 * If probing is not enabled on this phyint instance, don't proceed.
15827c478bd9Sstevel@tonic-gate 	 */
158306cdd167Smeem 	if (!PROBE_ENABLED(pii))
15847c478bd9Sstevel@tonic-gate 		return (TIMER_INFINITY);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	/*
15877c478bd9Sstevel@tonic-gate 	 * If the timer has fired too soon, probably triggered
15887c478bd9Sstevel@tonic-gate 	 * by some other phyint instance, return the remaining
15897c478bd9Sstevel@tonic-gate 	 * time
15907c478bd9Sstevel@tonic-gate 	 */
15917c478bd9Sstevel@tonic-gate 	if (TIME_LT(cur_time, pii->pii_snxt_time))
15927c478bd9Sstevel@tonic-gate 		return (pii->pii_snxt_time - cur_time);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/*
15957c478bd9Sstevel@tonic-gate 	 * If the link is down, don't send any probes for now.
15967c478bd9Sstevel@tonic-gate 	 */
15977c478bd9Sstevel@tonic-gate 	if (LINK_DOWN(pii->pii_phyint))
15987c478bd9Sstevel@tonic-gate 		return (TIMER_INFINITY);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	/*
16017c478bd9Sstevel@tonic-gate 	 * Randomize the next probe time, between MIN_RANDOM_FACTOR
16027c478bd9Sstevel@tonic-gate 	 * and MAX_RANDOM_FACTOR with respect to the base probe time.
16037c478bd9Sstevel@tonic-gate 	 * Base probe time is strictly periodic.
16047c478bd9Sstevel@tonic-gate 	 */
16057c478bd9Sstevel@tonic-gate 	interval = GET_RANDOM(
16067c478bd9Sstevel@tonic-gate 	    (int)(MIN_RANDOM_FACTOR * user_probe_interval),
16077c478bd9Sstevel@tonic-gate 	    (int)(MAX_RANDOM_FACTOR * user_probe_interval));
16087c478bd9Sstevel@tonic-gate 	pii->pii_snxt_time = pii->pii_snxt_basetime + interval;
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	/*
16117c478bd9Sstevel@tonic-gate 	 * Check if the current time > next time to probe. If so, we missed
16127c478bd9Sstevel@tonic-gate 	 * sending 1 or more probes, probably due to heavy system load. At least
16137c478bd9Sstevel@tonic-gate 	 * 'MIN_RANDOM_FACTOR * user_probe_interval' ms has elapsed since we
16147c478bd9Sstevel@tonic-gate 	 * were scheduled. Make adjustments to the times, in multiples of
16157c478bd9Sstevel@tonic-gate 	 * user_probe_interval.
16167c478bd9Sstevel@tonic-gate 	 */
16177c478bd9Sstevel@tonic-gate 	if (TIME_GT(cur_time, pii->pii_snxt_time)) {
16187c478bd9Sstevel@tonic-gate 		int n;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 		n = (cur_time - pii->pii_snxt_time) / user_probe_interval;
16217c478bd9Sstevel@tonic-gate 		pii->pii_snxt_time 	+= (n + 1) * user_probe_interval;
16227c478bd9Sstevel@tonic-gate 		pii->pii_snxt_basetime 	+= (n + 1) * user_probe_interval;
16237c478bd9Sstevel@tonic-gate 		logtrace("missed sending %d probes cur_time %u snxt_time %u"
16247c478bd9Sstevel@tonic-gate 		    " snxt_basetime %u\n", n + 1, cur_time, pii->pii_snxt_time,
16257c478bd9Sstevel@tonic-gate 		    pii->pii_snxt_basetime);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		/* Collect statistics about missed probes */
16287c478bd9Sstevel@tonic-gate 		probes_missed.pm_nprobes += n + 1;
16297c478bd9Sstevel@tonic-gate 		probes_missed.pm_ntimes++;
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 	pii->pii_snxt_basetime += user_probe_interval;
16327c478bd9Sstevel@tonic-gate 	interval = pii->pii_snxt_time - cur_time;
16337c478bd9Sstevel@tonic-gate 	if (debug & D_TARGET) {
16347c478bd9Sstevel@tonic-gate 		logdebug("cur_time %u snxt_time %u snxt_basetime %u"
16357c478bd9Sstevel@tonic-gate 		    " interval %u\n", cur_time, pii->pii_snxt_time,
16367c478bd9Sstevel@tonic-gate 		    pii->pii_snxt_basetime, interval);
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * If no targets are known, we need to send an ICMP multicast. The
16417c478bd9Sstevel@tonic-gate 	 * probe type is PROBE_MULTI.  We'll check back in 'interval' msec
16427c478bd9Sstevel@tonic-gate 	 * to see if we found a target.
16437c478bd9Sstevel@tonic-gate 	 */
16447c478bd9Sstevel@tonic-gate 	if (pii->pii_target_next == NULL) {
16457c478bd9Sstevel@tonic-gate 		assert(pii->pii_ntargets == 0);
16467c478bd9Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
16477c478bd9Sstevel@tonic-gate 		probe(pii, PROBE_MULTI, cur_time);
16487c478bd9Sstevel@tonic-gate 		return (interval);
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	if ((user_probe_interval != probe_interval) &&
16527c478bd9Sstevel@tonic-gate 	    TIME_LT(pii->pii_snxt_time, pii->pii_fd_snxt_basetime)) {
16537c478bd9Sstevel@tonic-gate 		/*
16547c478bd9Sstevel@tonic-gate 		 * the failure detection (fd) probe timer has not yet fired.
16557c478bd9Sstevel@tonic-gate 		 * Need to send only an rtt probe. The probe type is PROBE_RTT.
16567c478bd9Sstevel@tonic-gate 		 */
1657*e11c3f44Smeem 		probe(pii, PROBE_RTT, cur_hrtime);
16587c478bd9Sstevel@tonic-gate 		return (interval);
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 	/*
16617c478bd9Sstevel@tonic-gate 	 * the fd probe timer has fired. Need to do all failure
16627c478bd9Sstevel@tonic-gate 	 * detection / recovery calculations, and then send an fd probe
16637c478bd9Sstevel@tonic-gate 	 * of type PROBE_UNI.
16647c478bd9Sstevel@tonic-gate 	 */
16657c478bd9Sstevel@tonic-gate 	if (user_probe_interval == probe_interval) {
16667c478bd9Sstevel@tonic-gate 		/*
16677c478bd9Sstevel@tonic-gate 		 * We could have missed some probes, and then adjusted
16687c478bd9Sstevel@tonic-gate 		 * pii_snxt_basetime above. Otherwise we could have
16697c478bd9Sstevel@tonic-gate 		 * blindly added probe_interval to pii_fd_snxt_basetime.
16707c478bd9Sstevel@tonic-gate 		 */
16717c478bd9Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
16727c478bd9Sstevel@tonic-gate 	} else {
16737c478bd9Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime += probe_interval;
16747c478bd9Sstevel@tonic-gate 		if (TIME_GT(cur_time, pii->pii_fd_snxt_basetime)) {
16757c478bd9Sstevel@tonic-gate 			int n;
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 			n = (cur_time - pii->pii_fd_snxt_basetime) /
16787c478bd9Sstevel@tonic-gate 			    probe_interval;
16797c478bd9Sstevel@tonic-gate 			pii->pii_fd_snxt_basetime += (n + 1) * probe_interval;
16807c478bd9Sstevel@tonic-gate 		}
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	/*
16847c478bd9Sstevel@tonic-gate 	 * We can have at most, the latest 2 probes that we sent, in
16857c478bd9Sstevel@tonic-gate 	 * the PR_UNACKED state. All previous probes sent, are either
16867c478bd9Sstevel@tonic-gate 	 * PR_LOST or PR_ACKED. An unacknowledged probe is considered
1687*e11c3f44Smeem 	 * timed out if the probe's time_start + the CRTT < currenttime.
16887c478bd9Sstevel@tonic-gate 	 * For each of the last 2 probes, examine whether it has timed
16897c478bd9Sstevel@tonic-gate 	 * out. If so, mark it PR_LOST. The probe stats is a circular array.
16907c478bd9Sstevel@tonic-gate 	 */
16917c478bd9Sstevel@tonic-gate 	pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
16927c478bd9Sstevel@tonic-gate 	valid_unack_count = 0;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
16957c478bd9Sstevel@tonic-gate 		pr_statp = &pii->pii_probes[pr_ndx];
16967c478bd9Sstevel@tonic-gate 		cur_tg = pii->pii_probes[pr_ndx].pr_target;
16977c478bd9Sstevel@tonic-gate 		switch (pr_statp->pr_status) {
16987c478bd9Sstevel@tonic-gate 		case PR_ACKED:
16997c478bd9Sstevel@tonic-gate 			/*
17007c478bd9Sstevel@tonic-gate 			 * We received back an ACK, so the switch clearly
17017c478bd9Sstevel@tonic-gate 			 * is not dropping our traffic, and thus we can
17027c478bd9Sstevel@tonic-gate 			 * enable failure detection immediately.
17037c478bd9Sstevel@tonic-gate 			 */
17047c478bd9Sstevel@tonic-gate 			if (pii->pii_fd_hrtime > gethrtime()) {
17057c478bd9Sstevel@tonic-gate 				if (debug & D_PROBE) {
17067c478bd9Sstevel@tonic-gate 					logdebug("successful probe on %s; "
17077c478bd9Sstevel@tonic-gate 					    "ending quiet period\n",
17087c478bd9Sstevel@tonic-gate 					    pii->pii_phyint->pi_name);
17097c478bd9Sstevel@tonic-gate 				}
17107c478bd9Sstevel@tonic-gate 				pii->pii_fd_hrtime = gethrtime();
17117c478bd9Sstevel@tonic-gate 			}
17127c478bd9Sstevel@tonic-gate 			break;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 		case PR_UNACKED:
17157c478bd9Sstevel@tonic-gate 			assert(cur_tg != NULL);
17167c478bd9Sstevel@tonic-gate 			/*
17177c478bd9Sstevel@tonic-gate 			 * The crtt could be zero for some reason,
17187c478bd9Sstevel@tonic-gate 			 * Eg. the phyint could be failed. If the crtt is
17197c478bd9Sstevel@tonic-gate 			 * not available use group's probe interval,
17207c478bd9Sstevel@tonic-gate 			 * which is a worst case estimate.
17217c478bd9Sstevel@tonic-gate 			 */
1722*e11c3f44Smeem 			timeout = ns2ms(pr_statp->pr_hrtime_start);
17237c478bd9Sstevel@tonic-gate 			if (cur_tg->tg_crtt != 0) {
1724*e11c3f44Smeem 				timeout += cur_tg->tg_crtt;
17257c478bd9Sstevel@tonic-gate 			} else {
1726*e11c3f44Smeem 				timeout += probe_interval;
17277c478bd9Sstevel@tonic-gate 			}
17287c478bd9Sstevel@tonic-gate 			if (TIME_LT(timeout, cur_time)) {
17297c478bd9Sstevel@tonic-gate 				pr_statp->pr_time_lost = timeout;
1730*e11c3f44Smeem 				probe_chstate(pr_statp, pii, PR_LOST);
17317c478bd9Sstevel@tonic-gate 			} else if (i == 1) {
17327c478bd9Sstevel@tonic-gate 				/*
17337c478bd9Sstevel@tonic-gate 				 * We are forced to consider this probe
17347c478bd9Sstevel@tonic-gate 				 * lost, as we can have at most 2 unack.
17357c478bd9Sstevel@tonic-gate 				 * probes any time, and we will be sending a
17367c478bd9Sstevel@tonic-gate 				 * probe at the end of this function.
17377c478bd9Sstevel@tonic-gate 				 * Normally, we should not be here, but
17387c478bd9Sstevel@tonic-gate 				 * this can happen if an incoming response
17397c478bd9Sstevel@tonic-gate 				 * that was considered lost has increased
17407c478bd9Sstevel@tonic-gate 				 * the crtt for this target, and also bumped
17417c478bd9Sstevel@tonic-gate 				 * up the FDT. Note that we never cancel or
17427c478bd9Sstevel@tonic-gate 				 * increase the current pii_time_left, so
17437c478bd9Sstevel@tonic-gate 				 * when the timer fires, we find 2 valid
17447c478bd9Sstevel@tonic-gate 				 * unacked probes, and they are yet to timeout
17457c478bd9Sstevel@tonic-gate 				 */
17467c478bd9Sstevel@tonic-gate 				pr_statp->pr_time_lost = cur_time;
1747*e11c3f44Smeem 				probe_chstate(pr_statp, pii, PR_LOST);
17487c478bd9Sstevel@tonic-gate 			} else {
17497c478bd9Sstevel@tonic-gate 				/*
17507c478bd9Sstevel@tonic-gate 				 * Only the most recent probe can enter
17517c478bd9Sstevel@tonic-gate 				 * this 'else' arm. The second most recent
17527c478bd9Sstevel@tonic-gate 				 * probe must take either of the above arms,
17537c478bd9Sstevel@tonic-gate 				 * if it is unacked.
17547c478bd9Sstevel@tonic-gate 				 */
17557c478bd9Sstevel@tonic-gate 				valid_unack_count++;
17567c478bd9Sstevel@tonic-gate 			}
17577c478bd9Sstevel@tonic-gate 			break;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 		pr_ndx = PROBE_INDEX_PREV(pr_ndx);
17607c478bd9Sstevel@tonic-gate 	}
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	/*
17637c478bd9Sstevel@tonic-gate 	 * We send out 1 probe randomly in the interval between one half
17647c478bd9Sstevel@tonic-gate 	 * and one probe interval for the group. Given that the CRTT is always
17657c478bd9Sstevel@tonic-gate 	 * less than the group's probe interval, we can have at most 1
17667c478bd9Sstevel@tonic-gate 	 * unacknowledged probe now.  All previous probes are either lost or
17677c478bd9Sstevel@tonic-gate 	 * acked.
17687c478bd9Sstevel@tonic-gate 	 */
17697c478bd9Sstevel@tonic-gate 	assert(valid_unack_count == 0 || valid_unack_count == 1);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	/*
17727c478bd9Sstevel@tonic-gate 	 * The timer has fired. Take appropriate action depending
17737c478bd9Sstevel@tonic-gate 	 * on the current state of the phyint.
17747c478bd9Sstevel@tonic-gate 	 *
1775*e11c3f44Smeem 	 * PI_RUNNING state 	- Failure detection
1776*e11c3f44Smeem 	 * PI_FAILED state 	- Repair detection
17777c478bd9Sstevel@tonic-gate 	 */
17787c478bd9Sstevel@tonic-gate 	switch (pii->pii_phyint->pi_state) {
17797c478bd9Sstevel@tonic-gate 	case PI_FAILED:
17807c478bd9Sstevel@tonic-gate 		/*
17817c478bd9Sstevel@tonic-gate 		 * If the most recent probe (excluding unacked probes that
17827c478bd9Sstevel@tonic-gate 		 * are yet to time out) has been acked, check whether the
1783*e11c3f44Smeem 		 * phyint is now repaired.
17847c478bd9Sstevel@tonic-gate 		 */
17857c478bd9Sstevel@tonic-gate 		if (pii->pii_rack + valid_unack_count + 1 == pii->pii_snxt) {
17867c478bd9Sstevel@tonic-gate 			phyint_check_for_repair(pii->pii_phyint);
17877c478bd9Sstevel@tonic-gate 		}
17887c478bd9Sstevel@tonic-gate 		break;
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	case PI_RUNNING:
17917c478bd9Sstevel@tonic-gate 		/*
17927c478bd9Sstevel@tonic-gate 		 * It's possible our probes have been lost because of a
17937c478bd9Sstevel@tonic-gate 		 * spanning-tree mandated quiet period on the switch.  If so,
1794*e11c3f44Smeem 		 * ignore the lost probes.
17957c478bd9Sstevel@tonic-gate 		 */
17967c478bd9Sstevel@tonic-gate 		if (pii->pii_fd_hrtime - cur_hrtime > 0)
17977c478bd9Sstevel@tonic-gate 			break;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 		if (pii->pii_rack + valid_unack_count + 1 != pii->pii_snxt) {
18007c478bd9Sstevel@tonic-gate 			/*
18017c478bd9Sstevel@tonic-gate 			 * We have 1 or more failed probes (excluding unacked
18027c478bd9Sstevel@tonic-gate 			 * probes that are yet to time out). Determine if the
1803*e11c3f44Smeem 			 * phyint has failed.
18047c478bd9Sstevel@tonic-gate 			 */
18057c478bd9Sstevel@tonic-gate 			phyint_inst_check_for_failure(pii);
18067c478bd9Sstevel@tonic-gate 		}
18077c478bd9Sstevel@tonic-gate 		break;
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	default:
18107c478bd9Sstevel@tonic-gate 		logerr("phyint_inst_timer: invalid state %d\n",
18117c478bd9Sstevel@tonic-gate 		    pii->pii_phyint->pi_state);
18127c478bd9Sstevel@tonic-gate 		abort();
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	/*
18167c478bd9Sstevel@tonic-gate 	 * Start the next probe. probe() will also set pii->pii_probe_time_left
18177c478bd9Sstevel@tonic-gate 	 * to the group's probe interval. If phyint_failed -> target_flush_hosts
18187c478bd9Sstevel@tonic-gate 	 * was called, the target list may be empty.
18197c478bd9Sstevel@tonic-gate 	 */
18207c478bd9Sstevel@tonic-gate 	if (pii->pii_target_next != NULL) {
1821*e11c3f44Smeem 		probe(pii, PROBE_UNI, cur_hrtime);
18227c478bd9Sstevel@tonic-gate 		/*
18237c478bd9Sstevel@tonic-gate 		 * If we have just the one probe target, and we're not using
18247c478bd9Sstevel@tonic-gate 		 * router targets, try to find another as we presently have
18257c478bd9Sstevel@tonic-gate 		 * no resilience.
18267c478bd9Sstevel@tonic-gate 		 */
18277c478bd9Sstevel@tonic-gate 		if (!pii->pii_targets_are_routers && pii->pii_ntargets == 1)
1828*e11c3f44Smeem 			probe(pii, PROBE_MULTI, cur_hrtime);
18297c478bd9Sstevel@tonic-gate 	} else {
1830*e11c3f44Smeem 		probe(pii, PROBE_MULTI, cur_hrtime);
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 	return (interval);
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate /*
18367c478bd9Sstevel@tonic-gate  * Start the probe timer for an interface instance.
18377c478bd9Sstevel@tonic-gate  */
18387c478bd9Sstevel@tonic-gate void
18397c478bd9Sstevel@tonic-gate start_timer(struct phyint_instance *pii)
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate 	uint32_t interval;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	/*
18447c478bd9Sstevel@tonic-gate 	 * Spread the base probe times (pi_snxt_basetime) across phyints
18457c478bd9Sstevel@tonic-gate 	 * uniformly over the (curtime..curtime + the group's probe_interval).
18467c478bd9Sstevel@tonic-gate 	 * pi_snxt_basetime is strictly periodic with a frequency of
18477c478bd9Sstevel@tonic-gate 	 * the group's probe interval. The actual probe time pi_snxt_time
18487c478bd9Sstevel@tonic-gate 	 * adds some randomness to pi_snxt_basetime and happens in probe().
18497c478bd9Sstevel@tonic-gate 	 * For the 1st probe on each phyint after the timer is started,
18507c478bd9Sstevel@tonic-gate 	 * pi_snxt_time and pi_snxt_basetime are the same.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	interval = GET_RANDOM(0,
18537c478bd9Sstevel@tonic-gate 	    (int)pii->pii_phyint->pi_group->pg_probeint);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	pii->pii_snxt_basetime = getcurrenttime() + interval;
18567c478bd9Sstevel@tonic-gate 	pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
18577c478bd9Sstevel@tonic-gate 	pii->pii_snxt_time = pii->pii_snxt_basetime;
18587c478bd9Sstevel@tonic-gate 	timer_schedule(interval);
18597c478bd9Sstevel@tonic-gate }
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate /*
18627c478bd9Sstevel@tonic-gate  * Restart the probe timer on an interface instance.
18637c478bd9Sstevel@tonic-gate  */
18647c478bd9Sstevel@tonic-gate static void
18657c478bd9Sstevel@tonic-gate restart_timer(struct phyint_instance *pii)
18667c478bd9Sstevel@tonic-gate {
18677c478bd9Sstevel@tonic-gate 	/*
18687c478bd9Sstevel@tonic-gate 	 * We don't need to restart the timer if it was never started in
18697c478bd9Sstevel@tonic-gate 	 * the first place (pii->pii_basetime_inited not set), as the timer
18707c478bd9Sstevel@tonic-gate 	 * won't have gone off yet.
18717c478bd9Sstevel@tonic-gate 	 */
18727c478bd9Sstevel@tonic-gate 	if (pii->pii_basetime_inited != 0) {
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 		if (debug & D_LINKNOTE)
18757c478bd9Sstevel@tonic-gate 			logdebug("restart timer: restarting timer on %s, "
18767c478bd9Sstevel@tonic-gate 			    "address family %s\n", pii->pii_phyint->pi_name,
18777c478bd9Sstevel@tonic-gate 			    AF_STR(pii->pii_af));
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 		start_timer(pii);
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate static void
18847c478bd9Sstevel@tonic-gate process_link_state_down(struct phyint *pi)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate 	logerr("The link has gone down on %s\n", pi->pi_name);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * Clear the probe statistics arrays, we don't want the repair
1890*e11c3f44Smeem 	 * detection logic relying on probes that were successful prior
1891*e11c3f44Smeem 	 * to the link going down.
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	if (PROBE_CAPABLE(pi->pi_v4))
18947c478bd9Sstevel@tonic-gate 		clear_pii_probe_stats(pi->pi_v4);
18957c478bd9Sstevel@tonic-gate 	if (PROBE_CAPABLE(pi->pi_v6))
18967c478bd9Sstevel@tonic-gate 		clear_pii_probe_stats(pi->pi_v6);
18977c478bd9Sstevel@tonic-gate 	/*
18987c478bd9Sstevel@tonic-gate 	 * Check for interface failure.  Although we know the interface
18997c478bd9Sstevel@tonic-gate 	 * has failed, we don't know if all the other interfaces in the
19007c478bd9Sstevel@tonic-gate 	 * group have failed as well.
19017c478bd9Sstevel@tonic-gate 	 */
19027c478bd9Sstevel@tonic-gate 	if ((pi->pi_state == PI_RUNNING) ||
19037c478bd9Sstevel@tonic-gate 	    (pi->pi_state != PI_FAILED && !GROUP_FAILED(pi->pi_group))) {
19047c478bd9Sstevel@tonic-gate 		if (debug & D_LINKNOTE) {
19057c478bd9Sstevel@tonic-gate 			logdebug("process_link_state_down:"
19067c478bd9Sstevel@tonic-gate 			    " checking for failure on %s\n", pi->pi_name);
19077c478bd9Sstevel@tonic-gate 		}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 		if (pi->pi_v4 != NULL)
19107c478bd9Sstevel@tonic-gate 			phyint_inst_check_for_failure(pi->pi_v4);
19117c478bd9Sstevel@tonic-gate 		else if (pi->pi_v6 != NULL)
19127c478bd9Sstevel@tonic-gate 			phyint_inst_check_for_failure(pi->pi_v6);
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate }
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate static void
19177c478bd9Sstevel@tonic-gate process_link_state_up(struct phyint *pi)
19187c478bd9Sstevel@tonic-gate {
19197c478bd9Sstevel@tonic-gate 	logerr("The link has come up on %s\n", pi->pi_name);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	/*
19227c478bd9Sstevel@tonic-gate 	 * We stopped any running timers on each instance when the link
19237c478bd9Sstevel@tonic-gate 	 * went down, so restart them.
19247c478bd9Sstevel@tonic-gate 	 */
19257c478bd9Sstevel@tonic-gate 	if (pi->pi_v4)
19267c478bd9Sstevel@tonic-gate 		restart_timer(pi->pi_v4);
19277c478bd9Sstevel@tonic-gate 	if (pi->pi_v6)
19287c478bd9Sstevel@tonic-gate 		restart_timer(pi->pi_v6);
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	phyint_check_for_repair(pi);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	pi->pi_whenup[pi->pi_whendx++] = getcurrenttime();
19337c478bd9Sstevel@tonic-gate 	if (pi->pi_whendx == LINK_UP_PERMIN)
19347c478bd9Sstevel@tonic-gate 		pi->pi_whendx = 0;
19357c478bd9Sstevel@tonic-gate }
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate /*
19387c478bd9Sstevel@tonic-gate  * Process any changes in link state passed up from the interfaces.
19397c478bd9Sstevel@tonic-gate  */
19407c478bd9Sstevel@tonic-gate void
19417c478bd9Sstevel@tonic-gate process_link_state_changes(void)
19427c478bd9Sstevel@tonic-gate {
19437c478bd9Sstevel@tonic-gate 	struct phyint *pi;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	/* Look for interfaces where the link state has just changed */
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
19487c478bd9Sstevel@tonic-gate 		boolean_t old_link_state_up = LINK_UP(pi);
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 		/*
19517c478bd9Sstevel@tonic-gate 		 * Except when the "phyint" structure is created, this is
19527c478bd9Sstevel@tonic-gate 		 * the only place the link state is updated.  This allows
19537c478bd9Sstevel@tonic-gate 		 * this routine to detect changes in link state, rather
19547c478bd9Sstevel@tonic-gate 		 * than just the current state.
19557c478bd9Sstevel@tonic-gate 		 */
19567c478bd9Sstevel@tonic-gate 		UPDATE_LINK_STATE(pi);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 		if (LINK_DOWN(pi)) {
19597c478bd9Sstevel@tonic-gate 			/*
19607c478bd9Sstevel@tonic-gate 			 * Has link just gone down?
19617c478bd9Sstevel@tonic-gate 			 */
19627c478bd9Sstevel@tonic-gate 			if (old_link_state_up)
19637c478bd9Sstevel@tonic-gate 				process_link_state_down(pi);
19647c478bd9Sstevel@tonic-gate 		} else {
19657c478bd9Sstevel@tonic-gate 			/*
19667c478bd9Sstevel@tonic-gate 			 * Has link just gone back up?
19677c478bd9Sstevel@tonic-gate 			 */
19687c478bd9Sstevel@tonic-gate 			if (!old_link_state_up)
19697c478bd9Sstevel@tonic-gate 				process_link_state_up(pi);
19707c478bd9Sstevel@tonic-gate 		}
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate void
19757c478bd9Sstevel@tonic-gate reset_crtt_all(struct phyint *pi)
19767c478bd9Sstevel@tonic-gate {
19777c478bd9Sstevel@tonic-gate 	struct phyint_instance *pii;
19787c478bd9Sstevel@tonic-gate 	struct target *tg;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	pii = pi->pi_v4;
19817c478bd9Sstevel@tonic-gate 	if (pii != NULL) {
19827c478bd9Sstevel@tonic-gate 		for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
19837c478bd9Sstevel@tonic-gate 			tg->tg_crtt = 0;
19847c478bd9Sstevel@tonic-gate 			tg->tg_rtt_sa = -1;
19857c478bd9Sstevel@tonic-gate 			tg->tg_rtt_sd = 0;
19867c478bd9Sstevel@tonic-gate 		}
19877c478bd9Sstevel@tonic-gate 	}
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	pii = pi->pi_v6;
19907c478bd9Sstevel@tonic-gate 	if (pii != NULL) {
19917c478bd9Sstevel@tonic-gate 		for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
19927c478bd9Sstevel@tonic-gate 			tg->tg_crtt = 0;
19937c478bd9Sstevel@tonic-gate 			tg->tg_rtt_sa = -1;
19947c478bd9Sstevel@tonic-gate 			tg->tg_rtt_sd = 0;
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate /*
20007c478bd9Sstevel@tonic-gate  * Check if the phyint has failed the last NUM_PROBE_FAILS consecutive
20017c478bd9Sstevel@tonic-gate  * probes on both instances IPv4 and IPv6.
20027c478bd9Sstevel@tonic-gate  * If the interface has failed, return the time of the first probe failure
20037c478bd9Sstevel@tonic-gate  * in "tff".
20047c478bd9Sstevel@tonic-gate  */
20057c478bd9Sstevel@tonic-gate static int
20067c478bd9Sstevel@tonic-gate phyint_inst_probe_failure_state(struct phyint_instance *pii, uint_t *tff)
20077c478bd9Sstevel@tonic-gate {
20087c478bd9Sstevel@tonic-gate 	uint_t	pi_tff;
20097c478bd9Sstevel@tonic-gate 	struct	target *cur_tg;
20107c478bd9Sstevel@tonic-gate 	struct	probe_fail_count pfinfo;
20117c478bd9Sstevel@tonic-gate 	struct	phyint_instance *pii_other;
20127c478bd9Sstevel@tonic-gate 	int	pr_ndx;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	/*
20157c478bd9Sstevel@tonic-gate 	 * Get the number of consecutive failed probes on
20167c478bd9Sstevel@tonic-gate 	 * this phyint across all targets. Also get the number
20177c478bd9Sstevel@tonic-gate 	 * of consecutive failed probes on this target only
20187c478bd9Sstevel@tonic-gate 	 */
20197c478bd9Sstevel@tonic-gate 	pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
20207c478bd9Sstevel@tonic-gate 	cur_tg = pii->pii_probes[pr_ndx].pr_target;
20217c478bd9Sstevel@tonic-gate 	probe_fail_info(pii, cur_tg, &pfinfo);
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	/* Get the time of first failure, for later use */
20247c478bd9Sstevel@tonic-gate 	pi_tff = pfinfo.pf_tff;
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	/*
20277c478bd9Sstevel@tonic-gate 	 * If the current target has not responded to the
20287c478bd9Sstevel@tonic-gate 	 * last NUM_PROBE_FAILS probes, and other targets are
20297c478bd9Sstevel@tonic-gate 	 * responding delete this target. Dead gateway detection
20307c478bd9Sstevel@tonic-gate 	 * will eventually remove this target (if router) from the
20317c478bd9Sstevel@tonic-gate 	 * routing tables. If that does not occur, we may end
20327c478bd9Sstevel@tonic-gate 	 * up adding this to our list again.
20337c478bd9Sstevel@tonic-gate 	 */
20347c478bd9Sstevel@tonic-gate 	if (pfinfo.pf_nfail < NUM_PROBE_FAILS &&
20357c478bd9Sstevel@tonic-gate 	    pfinfo.pf_nfail_tg >= NUM_PROBE_FAILS) {
20367c478bd9Sstevel@tonic-gate 		if (pii->pii_targets_are_routers) {
20377c478bd9Sstevel@tonic-gate 			if (cur_tg->tg_status == TG_ACTIVE)
20387c478bd9Sstevel@tonic-gate 				pii->pii_ntargets--;
20397c478bd9Sstevel@tonic-gate 			cur_tg->tg_status = TG_DEAD;
20407c478bd9Sstevel@tonic-gate 			cur_tg->tg_crtt = 0;
20417c478bd9Sstevel@tonic-gate 			cur_tg->tg_rtt_sa = -1;
20427c478bd9Sstevel@tonic-gate 			cur_tg->tg_rtt_sd = 0;
20437c478bd9Sstevel@tonic-gate 			if (pii->pii_target_next == cur_tg)
20447c478bd9Sstevel@tonic-gate 				pii->pii_target_next = target_next(cur_tg);
20457c478bd9Sstevel@tonic-gate 		} else {
20467c478bd9Sstevel@tonic-gate 			target_delete(cur_tg);
2047*e11c3f44Smeem 			probe(pii, PROBE_MULTI, gethrtime());
20487c478bd9Sstevel@tonic-gate 		}
20497c478bd9Sstevel@tonic-gate 		return (PHYINT_OK);
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	/*
20537c478bd9Sstevel@tonic-gate 	 * If the phyint has lost NUM_PROBE_FAILS or more
20547c478bd9Sstevel@tonic-gate 	 * consecutive probes, on both IPv4 and IPv6 protocol
20557c478bd9Sstevel@tonic-gate 	 * instances of the phyint, then trigger failure
20567c478bd9Sstevel@tonic-gate 	 * detection, else return false
20577c478bd9Sstevel@tonic-gate 	 */
20587c478bd9Sstevel@tonic-gate 	if (pfinfo.pf_nfail < NUM_PROBE_FAILS)
20597c478bd9Sstevel@tonic-gate 		return (PHYINT_OK);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	pii_other = phyint_inst_other(pii);
20627c478bd9Sstevel@tonic-gate 	if (PROBE_CAPABLE(pii_other)) {
20637c478bd9Sstevel@tonic-gate 		probe_fail_info(pii_other, NULL, &pfinfo);
20647c478bd9Sstevel@tonic-gate 		if (pfinfo.pf_nfail >= NUM_PROBE_FAILS) {
20657c478bd9Sstevel@tonic-gate 			/*
20667c478bd9Sstevel@tonic-gate 			 * We have NUM_PROBE_FAILS or more failures
20677c478bd9Sstevel@tonic-gate 			 * on both IPv4 and IPv6. Get the earliest
20687c478bd9Sstevel@tonic-gate 			 * time when failure was detected on this
20697c478bd9Sstevel@tonic-gate 			 * phyint across IPv4 and IPv6.
20707c478bd9Sstevel@tonic-gate 			 */
20717c478bd9Sstevel@tonic-gate 			if (TIME_LT(pfinfo.pf_tff, pi_tff))
20727c478bd9Sstevel@tonic-gate 				pi_tff = pfinfo.pf_tff;
20737c478bd9Sstevel@tonic-gate 		} else {
20747c478bd9Sstevel@tonic-gate 			/*
20757c478bd9Sstevel@tonic-gate 			 * This instance has < NUM_PROBE_FAILS failure.
20767c478bd9Sstevel@tonic-gate 			 * So return false
20777c478bd9Sstevel@tonic-gate 			 */
20787c478bd9Sstevel@tonic-gate 			return (PHYINT_OK);
20797c478bd9Sstevel@tonic-gate 		}
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 	*tff = pi_tff;
20827c478bd9Sstevel@tonic-gate 	return (PHYINT_FAILURE);
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate /*
20867c478bd9Sstevel@tonic-gate  * Check if the link has gone down on this phyint, or it has failed the
20877c478bd9Sstevel@tonic-gate  * last NUM_PROBE_FAILS consecutive probes on both instances IPv4 and IPv6.
20887c478bd9Sstevel@tonic-gate  * Also look at other phyints of this group, for group failures.
20897c478bd9Sstevel@tonic-gate  */
20907c478bd9Sstevel@tonic-gate int
20917c478bd9Sstevel@tonic-gate failure_state(struct phyint_instance *pii)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate 	struct	probe_success_count psinfo;
20947c478bd9Sstevel@tonic-gate 	uint_t	pi2_tls;		/* time last success */
20957c478bd9Sstevel@tonic-gate 	uint_t	pi_tff;			/* time first fail */
2096*e11c3f44Smeem 	struct	phyint *pi2;
20977c478bd9Sstevel@tonic-gate 	struct	phyint *pi;
20987c478bd9Sstevel@tonic-gate 	struct	phyint_instance *pii2;
20997c478bd9Sstevel@tonic-gate 	struct  phyint_group *pg;
2100*e11c3f44Smeem 	int	retval;
21017c478bd9Sstevel@tonic-gate 
2102*e11c3f44Smeem 	if (debug & D_FAILREP)
21037c478bd9Sstevel@tonic-gate 		logdebug("phyint_failed(%s)\n", pii->pii_name);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	pi = pii->pii_phyint;
21067c478bd9Sstevel@tonic-gate 	pg = pi->pi_group;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	if (LINK_UP(pi) && phyint_inst_probe_failure_state(pii, &pi_tff) ==
210928f13c35Srk 	    PHYINT_OK)
21107c478bd9Sstevel@tonic-gate 		return (PHYINT_OK);
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	/*
2113*e11c3f44Smeem 	 * At this point, the link is down, or the phyint is suspect, as it
2114*e11c3f44Smeem 	 * has lost NUM_PROBE_FAILS or more probes. If the phyint does not
2115*e11c3f44Smeem 	 * belong to any group, this is a PHYINT_FAILURE.  Otherwise, continue
2116*e11c3f44Smeem 	 * on to determine whether this should be considered a PHYINT_FAILURE
2117*e11c3f44Smeem 	 * or GROUP_FAILURE.
21187c478bd9Sstevel@tonic-gate 	 */
2119*e11c3f44Smeem 	if (pg == phyint_anongroup)
21207c478bd9Sstevel@tonic-gate 		return (PHYINT_FAILURE);
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	/*
21237c478bd9Sstevel@tonic-gate 	 * Need to compare against other phyints of the same group
21247c478bd9Sstevel@tonic-gate 	 * to exclude group failures. If the failure was detected via
21257c478bd9Sstevel@tonic-gate 	 * probing, then if the time of last success (tls) of any
21267c478bd9Sstevel@tonic-gate 	 * phyint is more recent than the time of first fail (tff) of the
21277c478bd9Sstevel@tonic-gate 	 * phyint in question, and the link is up on the phyint,
21287c478bd9Sstevel@tonic-gate 	 * then it is a phyint failure. Otherwise it is a group failure.
21297c478bd9Sstevel@tonic-gate 	 * If failure was detected via a link down notification sent from
21307c478bd9Sstevel@tonic-gate 	 * the driver to IP, we see if any phyints in the group are still
21317c478bd9Sstevel@tonic-gate 	 * running and haven't received a link down notification.  We
21327c478bd9Sstevel@tonic-gate 	 * will usually be processing the link down notification shortly
21337c478bd9Sstevel@tonic-gate 	 * after it was received, so there is no point looking at the tls
21347c478bd9Sstevel@tonic-gate 	 * of other phyints.
21357c478bd9Sstevel@tonic-gate 	 */
2136*e11c3f44Smeem 	retval = GROUP_FAILURE;
21377c478bd9Sstevel@tonic-gate 	for (pi2 = pg->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
21387c478bd9Sstevel@tonic-gate 		/* Exclude ourself from comparison */
21397c478bd9Sstevel@tonic-gate 		if (pi2 == pi)
21407c478bd9Sstevel@tonic-gate 			continue;
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 		if (LINK_DOWN(pi)) {
21437c478bd9Sstevel@tonic-gate 			/*
2144*e11c3f44Smeem 			 * We use FLAGS_TO_LINK_STATE() to test the flags
2145*e11c3f44Smeem 			 * directly, rather then LINK_UP() or LINK_DOWN(), as
2146*e11c3f44Smeem 			 * we may not have got round to processing the link
2147*e11c3f44Smeem 			 * state for the other phyints in the group yet.
21487c478bd9Sstevel@tonic-gate 			 *
2149*e11c3f44Smeem 			 * The check for PI_RUNNING and group failure handles
2150*e11c3f44Smeem 			 * the case when the group begins to recover.
2151*e11c3f44Smeem 			 * PI_RUNNING will be set, and group failure cleared
2152*e11c3f44Smeem 			 * only after receipt of NUM_PROBE_REPAIRS, by which
2153*e11c3f44Smeem 			 * time the other phyints should have received at
2154*e11c3f44Smeem 			 * least 1 packet, and so will not have NUM_PROBE_FAILS.
21557c478bd9Sstevel@tonic-gate 			 */
21567c478bd9Sstevel@tonic-gate 			if ((pi2->pi_state == PI_RUNNING) &&
2157*e11c3f44Smeem 			    !GROUP_FAILED(pg) && FLAGS_TO_LINK_STATE(pi2)) {
2158*e11c3f44Smeem 				retval = PHYINT_FAILURE;
2159*e11c3f44Smeem 				break;
2160*e11c3f44Smeem 			}
2161*e11c3f44Smeem 			continue;
2162*e11c3f44Smeem 		}
2163*e11c3f44Smeem 
2164*e11c3f44Smeem 		if (LINK_DOWN(pi2))
2165*e11c3f44Smeem 			continue;
2166*e11c3f44Smeem 
2167*e11c3f44Smeem 		/*
2168*e11c3f44Smeem 		 * If there's no probe-based failure detection on this
2169*e11c3f44Smeem 		 * interface, and its link is still up, then it's still
2170*e11c3f44Smeem 		 * working and thus the group has not failed.
2171*e11c3f44Smeem 		 */
2172*e11c3f44Smeem 		if (!PROBE_ENABLED(pi2->pi_v4) && !PROBE_ENABLED(pi2->pi_v6)) {
2173*e11c3f44Smeem 			retval = PHYINT_FAILURE;
2174*e11c3f44Smeem 			break;
2175*e11c3f44Smeem 		}
2176*e11c3f44Smeem 
2177*e11c3f44Smeem 		/*
2178*e11c3f44Smeem 		 * Need to compare against both IPv4 and IPv6 instances.
2179*e11c3f44Smeem 		 */
2180*e11c3f44Smeem 		pii2 = pi2->pi_v4;
2181*e11c3f44Smeem 		if (pii2 != NULL) {
2182*e11c3f44Smeem 			probe_success_info(pii2, NULL, &psinfo);
2183*e11c3f44Smeem 			if (psinfo.ps_tls_valid) {
2184*e11c3f44Smeem 				pi2_tls = psinfo.ps_tls;
2185*e11c3f44Smeem 				/*
2186*e11c3f44Smeem 				 * See comment above regarding check
2187*e11c3f44Smeem 				 * for PI_RUNNING and group failure.
2188*e11c3f44Smeem 				 */
2189*e11c3f44Smeem 				if (TIME_GT(pi2_tls, pi_tff) &&
2190*e11c3f44Smeem 				    (pi2->pi_state == PI_RUNNING) &&
2191*e11c3f44Smeem 				    !GROUP_FAILED(pg) &&
2192*e11c3f44Smeem 				    FLAGS_TO_LINK_STATE(pi2)) {
2193*e11c3f44Smeem 					retval = PHYINT_FAILURE;
2194*e11c3f44Smeem 					break;
21957c478bd9Sstevel@tonic-gate 				}
21967c478bd9Sstevel@tonic-gate 			}
2197*e11c3f44Smeem 		}
21987c478bd9Sstevel@tonic-gate 
2199*e11c3f44Smeem 		pii2 = pi2->pi_v6;
2200*e11c3f44Smeem 		if (pii2 != NULL) {
2201*e11c3f44Smeem 			probe_success_info(pii2, NULL, &psinfo);
2202*e11c3f44Smeem 			if (psinfo.ps_tls_valid) {
2203*e11c3f44Smeem 				pi2_tls = psinfo.ps_tls;
2204*e11c3f44Smeem 				/*
2205*e11c3f44Smeem 				 * See comment above regarding check
2206*e11c3f44Smeem 				 * for PI_RUNNING and group failure.
2207*e11c3f44Smeem 				 */
2208*e11c3f44Smeem 				if (TIME_GT(pi2_tls, pi_tff) &&
2209*e11c3f44Smeem 				    (pi2->pi_state == PI_RUNNING) &&
2210*e11c3f44Smeem 				    !GROUP_FAILED(pg) &&
2211*e11c3f44Smeem 				    FLAGS_TO_LINK_STATE(pi2)) {
2212*e11c3f44Smeem 					retval = PHYINT_FAILURE;
2213*e11c3f44Smeem 					break;
22147c478bd9Sstevel@tonic-gate 				}
22157c478bd9Sstevel@tonic-gate 			}
22167c478bd9Sstevel@tonic-gate 		}
22177c478bd9Sstevel@tonic-gate 	}
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	/*
2220*e11c3f44Smeem 	 * Update the group state to account for the changes.
22217c478bd9Sstevel@tonic-gate 	 */
2222*e11c3f44Smeem 	phyint_group_refresh_state(pg);
2223*e11c3f44Smeem 	return (retval);
22247c478bd9Sstevel@tonic-gate }
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate /*
22277c478bd9Sstevel@tonic-gate  * Return the information associated with consecutive probe successes
22287c478bd9Sstevel@tonic-gate  * starting with the most recent probe. At most the last 2 probes can be
22297c478bd9Sstevel@tonic-gate  * in the unacknowledged state. All previous probes have either failed
22307c478bd9Sstevel@tonic-gate  * or succeeded.
22317c478bd9Sstevel@tonic-gate  */
22327c478bd9Sstevel@tonic-gate static void
22337c478bd9Sstevel@tonic-gate probe_success_info(struct phyint_instance *pii, struct target *cur_tg,
22347c478bd9Sstevel@tonic-gate     struct probe_success_count *psinfo)
22357c478bd9Sstevel@tonic-gate {
22367c478bd9Sstevel@tonic-gate 	uint_t	i;
22377c478bd9Sstevel@tonic-gate 	struct probe_stats *pr_statp;
22387c478bd9Sstevel@tonic-gate 	uint_t most_recent;
22397c478bd9Sstevel@tonic-gate 	uint_t second_most_recent;
22407c478bd9Sstevel@tonic-gate 	boolean_t pi_found_failure = _B_FALSE;
22417c478bd9Sstevel@tonic-gate 	boolean_t tg_found_failure = _B_FALSE;
22427c478bd9Sstevel@tonic-gate 	uint_t now;
22437c478bd9Sstevel@tonic-gate 	uint_t timeout;
22447c478bd9Sstevel@tonic-gate 	struct target *tg;
22457c478bd9Sstevel@tonic-gate 
2246*e11c3f44Smeem 	if (debug & D_FAILREP)
22477c478bd9Sstevel@tonic-gate 		logdebug("probe_success_info(%s)\n", pii->pii_name);
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	bzero(psinfo, sizeof (*psinfo));
22507c478bd9Sstevel@tonic-gate 	now = getcurrenttime();
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/*
22537c478bd9Sstevel@tonic-gate 	 * Start with the most recent probe, and count the number
22547c478bd9Sstevel@tonic-gate 	 * of consecutive probe successes. Latch the number of successes
22557c478bd9Sstevel@tonic-gate 	 * on hitting a failure.
22567c478bd9Sstevel@tonic-gate 	 */
22577c478bd9Sstevel@tonic-gate 	most_recent = PROBE_INDEX_PREV(pii->pii_probe_next);
22587c478bd9Sstevel@tonic-gate 	second_most_recent = PROBE_INDEX_PREV(most_recent);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	for (i = most_recent; i != pii->pii_probe_next;
22617c478bd9Sstevel@tonic-gate 	    i = PROBE_INDEX_PREV(i)) {
22627c478bd9Sstevel@tonic-gate 		pr_statp = &pii->pii_probes[i];
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 		switch (pr_statp->pr_status) {
22657c478bd9Sstevel@tonic-gate 		case PR_UNACKED:
22667c478bd9Sstevel@tonic-gate 			/*
22677c478bd9Sstevel@tonic-gate 			 * Only the most recent 2 probes can be unacknowledged
22687c478bd9Sstevel@tonic-gate 			 */
22697c478bd9Sstevel@tonic-gate 			assert(i == most_recent || i == second_most_recent);
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 			tg = pr_statp->pr_target;
22727c478bd9Sstevel@tonic-gate 			assert(tg != NULL);
22737c478bd9Sstevel@tonic-gate 			/*
22747c478bd9Sstevel@tonic-gate 			 * The crtt could be zero for some reason,
22757c478bd9Sstevel@tonic-gate 			 * Eg. the phyint could be failed. If the crtt is
22767c478bd9Sstevel@tonic-gate 			 * not available use the value of the group's probe
22777c478bd9Sstevel@tonic-gate 			 * interval which is a worst case estimate.
22787c478bd9Sstevel@tonic-gate 			 */
2279*e11c3f44Smeem 			timeout = ns2ms(pr_statp->pr_hrtime_start);
22807c478bd9Sstevel@tonic-gate 			if (tg->tg_crtt != 0) {
2281*e11c3f44Smeem 				timeout += tg->tg_crtt;
22827c478bd9Sstevel@tonic-gate 			} else {
2283*e11c3f44Smeem 				timeout +=
22847c478bd9Sstevel@tonic-gate 				    pii->pii_phyint->pi_group->pg_probeint;
22857c478bd9Sstevel@tonic-gate 			}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 			if (TIME_LT(timeout, now)) {
22887c478bd9Sstevel@tonic-gate 				/*
22897c478bd9Sstevel@tonic-gate 				 * We hit a failure. Latch the total number of
22907c478bd9Sstevel@tonic-gate 				 * recent consecutive successes.
22917c478bd9Sstevel@tonic-gate 				 */
22927c478bd9Sstevel@tonic-gate 				pr_statp->pr_time_lost = timeout;
2293*e11c3f44Smeem 				probe_chstate(pr_statp, pii, PR_LOST);
22947c478bd9Sstevel@tonic-gate 				pi_found_failure = _B_TRUE;
22957c478bd9Sstevel@tonic-gate 				if (cur_tg != NULL && tg == cur_tg) {
22967c478bd9Sstevel@tonic-gate 					/*
22977c478bd9Sstevel@tonic-gate 					 * We hit a failure for the desired
22987c478bd9Sstevel@tonic-gate 					 * target. Latch the number of recent
22997c478bd9Sstevel@tonic-gate 					 * consecutive successes for this target
23007c478bd9Sstevel@tonic-gate 					 */
23017c478bd9Sstevel@tonic-gate 					tg_found_failure = _B_TRUE;
23027c478bd9Sstevel@tonic-gate 				}
23037c478bd9Sstevel@tonic-gate 			}
23047c478bd9Sstevel@tonic-gate 			break;
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 		case PR_ACKED:
23077c478bd9Sstevel@tonic-gate 			/*
23087c478bd9Sstevel@tonic-gate 			 * Bump up the count of probe successes, if we
23097c478bd9Sstevel@tonic-gate 			 * have not seen any failure so far.
23107c478bd9Sstevel@tonic-gate 			 */
23117c478bd9Sstevel@tonic-gate 			if (!pi_found_failure)
23127c478bd9Sstevel@tonic-gate 				psinfo->ps_nsucc++;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg &&
23157c478bd9Sstevel@tonic-gate 			    !tg_found_failure) {
23167c478bd9Sstevel@tonic-gate 				psinfo->ps_nsucc_tg++;
23177c478bd9Sstevel@tonic-gate 			}
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 			/*
23207c478bd9Sstevel@tonic-gate 			 * Record the time of last success, if this is
23217c478bd9Sstevel@tonic-gate 			 * the most recent probe success.
23227c478bd9Sstevel@tonic-gate 			 */
23237c478bd9Sstevel@tonic-gate 			if (!psinfo->ps_tls_valid) {
2324*e11c3f44Smeem 				psinfo->ps_tls =
2325*e11c3f44Smeem 				    ns2ms(pr_statp->pr_hrtime_ackproc);
23267c478bd9Sstevel@tonic-gate 				psinfo->ps_tls_valid = _B_TRUE;
23277c478bd9Sstevel@tonic-gate 			}
23287c478bd9Sstevel@tonic-gate 			break;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 		case PR_LOST:
23317c478bd9Sstevel@tonic-gate 			/*
23327c478bd9Sstevel@tonic-gate 			 * We hit a failure. Latch the total number of
23337c478bd9Sstevel@tonic-gate 			 * recent consecutive successes.
23347c478bd9Sstevel@tonic-gate 			 */
23357c478bd9Sstevel@tonic-gate 			pi_found_failure = _B_TRUE;
23367c478bd9Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg) {
23377c478bd9Sstevel@tonic-gate 				/*
23387c478bd9Sstevel@tonic-gate 				 * We hit a failure for the desired target.
23397c478bd9Sstevel@tonic-gate 				 * Latch the number of recent consecutive
23407c478bd9Sstevel@tonic-gate 				 * successes for this target
23417c478bd9Sstevel@tonic-gate 				 */
23427c478bd9Sstevel@tonic-gate 				tg_found_failure = _B_TRUE;
23437c478bd9Sstevel@tonic-gate 			}
23447c478bd9Sstevel@tonic-gate 			break;
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 		default:
23477c478bd9Sstevel@tonic-gate 			return;
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 		}
23507c478bd9Sstevel@tonic-gate 	}
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate /*
23547c478bd9Sstevel@tonic-gate  * Return the information associated with consecutive probe failures
23557c478bd9Sstevel@tonic-gate  * starting with the most recent probe. Only the last 2 probes can be in the
23567c478bd9Sstevel@tonic-gate  * unacknowledged state. All previous probes have either failed or succeeded.
23577c478bd9Sstevel@tonic-gate  */
23587c478bd9Sstevel@tonic-gate static void
23597c478bd9Sstevel@tonic-gate probe_fail_info(struct phyint_instance *pii, struct target *cur_tg,
23607c478bd9Sstevel@tonic-gate     struct probe_fail_count *pfinfo)
23617c478bd9Sstevel@tonic-gate {
23627c478bd9Sstevel@tonic-gate 	int	i;
23637c478bd9Sstevel@tonic-gate 	struct probe_stats *pr_statp;
23647c478bd9Sstevel@tonic-gate 	boolean_t	tg_found_success = _B_FALSE;
23657c478bd9Sstevel@tonic-gate 	boolean_t	pi_found_success = _B_FALSE;
23667c478bd9Sstevel@tonic-gate 	int	most_recent;
23677c478bd9Sstevel@tonic-gate 	int	second_most_recent;
23687c478bd9Sstevel@tonic-gate 	uint_t	now;
23697c478bd9Sstevel@tonic-gate 	uint_t	timeout;
23707c478bd9Sstevel@tonic-gate 	struct	target *tg;
23717c478bd9Sstevel@tonic-gate 
2372*e11c3f44Smeem 	if (debug & D_FAILREP)
23737c478bd9Sstevel@tonic-gate 		logdebug("probe_fail_info(%s)\n", pii->pii_name);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	bzero(pfinfo, sizeof (*pfinfo));
23767c478bd9Sstevel@tonic-gate 	now = getcurrenttime();
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	/*
23797c478bd9Sstevel@tonic-gate 	 * Start with the most recent probe, and count the number
23807c478bd9Sstevel@tonic-gate 	 * of consecutive probe failures. Latch the number of failures
23817c478bd9Sstevel@tonic-gate 	 * on hitting a probe success.
23827c478bd9Sstevel@tonic-gate 	 */
23837c478bd9Sstevel@tonic-gate 	most_recent = PROBE_INDEX_PREV(pii->pii_probe_next);
23847c478bd9Sstevel@tonic-gate 	second_most_recent = PROBE_INDEX_PREV(most_recent);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	for (i = most_recent; i != pii->pii_probe_next;
23877c478bd9Sstevel@tonic-gate 	    i = PROBE_INDEX_PREV(i)) {
23887c478bd9Sstevel@tonic-gate 		pr_statp = &pii->pii_probes[i];
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 		assert(PR_STATUS_VALID(pr_statp->pr_status));
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 		switch (pr_statp->pr_status) {
23937c478bd9Sstevel@tonic-gate 		case PR_UNACKED:
23947c478bd9Sstevel@tonic-gate 			/*
23957c478bd9Sstevel@tonic-gate 			 * Only the most recent 2 probes can be unacknowledged
23967c478bd9Sstevel@tonic-gate 			 */
23977c478bd9Sstevel@tonic-gate 			assert(i == most_recent || i == second_most_recent);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 			tg = pr_statp->pr_target;
24007c478bd9Sstevel@tonic-gate 			/*
24017c478bd9Sstevel@tonic-gate 			 * Target is guaranteed to exist in the unack. state
24027c478bd9Sstevel@tonic-gate 			 */
24037c478bd9Sstevel@tonic-gate 			assert(tg != NULL);
24047c478bd9Sstevel@tonic-gate 			/*
24057c478bd9Sstevel@tonic-gate 			 * The crtt could be zero for some reason,
24067c478bd9Sstevel@tonic-gate 			 * Eg. the phyint could be failed. If the crtt is
24077c478bd9Sstevel@tonic-gate 			 * not available use the group's probe interval,
24087c478bd9Sstevel@tonic-gate 			 * which is a worst case estimate.
24097c478bd9Sstevel@tonic-gate 			 */
2410*e11c3f44Smeem 			timeout = ns2ms(pr_statp->pr_hrtime_start);
24117c478bd9Sstevel@tonic-gate 			if (tg->tg_crtt != 0) {
2412*e11c3f44Smeem 				timeout += tg->tg_crtt;
24137c478bd9Sstevel@tonic-gate 			} else {
2414*e11c3f44Smeem 				timeout +=
24157c478bd9Sstevel@tonic-gate 				    pii->pii_phyint->pi_group->pg_probeint;
24167c478bd9Sstevel@tonic-gate 			}
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 			if (TIME_GT(timeout, now))
24197c478bd9Sstevel@tonic-gate 				break;
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 			pr_statp->pr_time_lost = timeout;
2422*e11c3f44Smeem 			probe_chstate(pr_statp, pii, PR_LOST);
24237c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 		case PR_LOST:
24267c478bd9Sstevel@tonic-gate 			if (!pi_found_success) {
24277c478bd9Sstevel@tonic-gate 				pfinfo->pf_nfail++;
24287c478bd9Sstevel@tonic-gate 				pfinfo->pf_tff = pr_statp->pr_time_lost;
24297c478bd9Sstevel@tonic-gate 			}
24307c478bd9Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg &&
24317c478bd9Sstevel@tonic-gate 			    !tg_found_success)  {
24327c478bd9Sstevel@tonic-gate 				pfinfo->pf_nfail_tg++;
24337c478bd9Sstevel@tonic-gate 			}
24347c478bd9Sstevel@tonic-gate 			break;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		default:
24377c478bd9Sstevel@tonic-gate 			/*
24387c478bd9Sstevel@tonic-gate 			 * We hit a success or unused slot. Latch the
24397c478bd9Sstevel@tonic-gate 			 * total number of recent consecutive failures.
24407c478bd9Sstevel@tonic-gate 			 */
24417c478bd9Sstevel@tonic-gate 			pi_found_success = _B_TRUE;
24427c478bd9Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg) {
24437c478bd9Sstevel@tonic-gate 				/*
24447c478bd9Sstevel@tonic-gate 				 * We hit a success for the desired target.
24457c478bd9Sstevel@tonic-gate 				 * Latch the number of recent consecutive
24467c478bd9Sstevel@tonic-gate 				 * failures for this target
24477c478bd9Sstevel@tonic-gate 				 */
24487c478bd9Sstevel@tonic-gate 				tg_found_success = _B_TRUE;
24497c478bd9Sstevel@tonic-gate 			}
24507c478bd9Sstevel@tonic-gate 		}
24517c478bd9Sstevel@tonic-gate 	}
24527c478bd9Sstevel@tonic-gate }
24537c478bd9Sstevel@tonic-gate 
2454*e11c3f44Smeem /*
2455*e11c3f44Smeem  * Change the state of probe `pr' on phyint_instance `pii' to state `state'.
2456*e11c3f44Smeem  */
2457*e11c3f44Smeem void
2458*e11c3f44Smeem probe_chstate(struct probe_stats *pr, struct phyint_instance *pii, int state)
2459*e11c3f44Smeem {
2460*e11c3f44Smeem 	if (pr->pr_status == state)
2461*e11c3f44Smeem 		return;
2462*e11c3f44Smeem 
2463*e11c3f44Smeem 	pr->pr_status = state;
2464*e11c3f44Smeem 	(void) probe_state_event(pr, pii);
2465*e11c3f44Smeem }
2466*e11c3f44Smeem 
24677c478bd9Sstevel@tonic-gate /*
24687c478bd9Sstevel@tonic-gate  * Check if the phyint has been repaired.  If no test address has been
24697c478bd9Sstevel@tonic-gate  * configured, then consider the interface repaired if the link is up (unless
24707c478bd9Sstevel@tonic-gate  * the link is flapping; see below).  Otherwise, look for proof of probes
24717c478bd9Sstevel@tonic-gate  * being sent and received. If last NUM_PROBE_REPAIRS probes are fine on
24727c478bd9Sstevel@tonic-gate  * either IPv4 or IPv6 instance, the phyint can be considered repaired.
24737c478bd9Sstevel@tonic-gate  */
24747c478bd9Sstevel@tonic-gate static boolean_t
24757c478bd9Sstevel@tonic-gate phyint_repaired(struct phyint *pi)
24767c478bd9Sstevel@tonic-gate {
24777c478bd9Sstevel@tonic-gate 	struct	probe_success_count psinfo;
24787c478bd9Sstevel@tonic-gate 	struct	phyint_instance *pii;
24797c478bd9Sstevel@tonic-gate 	struct	target *cur_tg;
24807c478bd9Sstevel@tonic-gate 	int	pr_ndx;
24817c478bd9Sstevel@tonic-gate 	uint_t	cur_time;
24827c478bd9Sstevel@tonic-gate 
2483*e11c3f44Smeem 	if (debug & D_FAILREP)
24847c478bd9Sstevel@tonic-gate 		logdebug("phyint_repaired(%s)\n", pi->pi_name);
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	if (LINK_DOWN(pi))
24877c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	/*
24907c478bd9Sstevel@tonic-gate 	 * If we don't have any test addresses and the link is up, then
24917c478bd9Sstevel@tonic-gate 	 * consider the interface repaired, unless we've received more than
24927c478bd9Sstevel@tonic-gate 	 * LINK_UP_PERMIN link up notifications in the last minute, in
24937c478bd9Sstevel@tonic-gate 	 * which case we keep the link down until we drop back below
24947c478bd9Sstevel@tonic-gate 	 * the threshold.
24957c478bd9Sstevel@tonic-gate 	 */
24967c478bd9Sstevel@tonic-gate 	if (!PROBE_ENABLED(pi->pi_v4) && !PROBE_ENABLED(pi->pi_v6)) {
24977c478bd9Sstevel@tonic-gate 		cur_time = getcurrenttime();
24987c478bd9Sstevel@tonic-gate 		if ((pi->pi_whenup[pi->pi_whendx] == 0 ||
24997c478bd9Sstevel@tonic-gate 		    (cur_time - pi->pi_whenup[pi->pi_whendx]) > MSEC_PERMIN)) {
25007c478bd9Sstevel@tonic-gate 			pi->pi_lfmsg_printed = 0;
25017c478bd9Sstevel@tonic-gate 			return (_B_TRUE);
25027c478bd9Sstevel@tonic-gate 		}
25037c478bd9Sstevel@tonic-gate 		if (!pi->pi_lfmsg_printed) {
25047c478bd9Sstevel@tonic-gate 			logerr("The link has come up on %s more than %d times "
2505*e11c3f44Smeem 			    "in the last minute; disabling repair until it "
25067c478bd9Sstevel@tonic-gate 			    "stabilizes\n", pi->pi_name, LINK_UP_PERMIN);
25077c478bd9Sstevel@tonic-gate 			pi->pi_lfmsg_printed = 1;
25087c478bd9Sstevel@tonic-gate 		}
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
25117c478bd9Sstevel@tonic-gate 	}
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 	pii = pi->pi_v4;
25147c478bd9Sstevel@tonic-gate 	if (PROBE_CAPABLE(pii)) {
25157c478bd9Sstevel@tonic-gate 		pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
25167c478bd9Sstevel@tonic-gate 		cur_tg = pii->pii_probes[pr_ndx].pr_target;
25177c478bd9Sstevel@tonic-gate 		probe_success_info(pii, cur_tg, &psinfo);
25187c478bd9Sstevel@tonic-gate 		if (psinfo.ps_nsucc >= NUM_PROBE_REPAIRS ||
25197c478bd9Sstevel@tonic-gate 		    psinfo.ps_nsucc_tg >= NUM_PROBE_REPAIRS)
25207c478bd9Sstevel@tonic-gate 			return (_B_TRUE);
25217c478bd9Sstevel@tonic-gate 	}
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	pii = pi->pi_v6;
25247c478bd9Sstevel@tonic-gate 	if (PROBE_CAPABLE(pii)) {
25257c478bd9Sstevel@tonic-gate 		pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
25267c478bd9Sstevel@tonic-gate 		cur_tg = pii->pii_probes[pr_ndx].pr_target;
25277c478bd9Sstevel@tonic-gate 		probe_success_info(pii, cur_tg, &psinfo);
25287c478bd9Sstevel@tonic-gate 		if (psinfo.ps_nsucc >= NUM_PROBE_REPAIRS ||
25297c478bd9Sstevel@tonic-gate 		    psinfo.ps_nsucc_tg >= NUM_PROBE_REPAIRS)
25307c478bd9Sstevel@tonic-gate 			return (_B_TRUE);
25317c478bd9Sstevel@tonic-gate 	}
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
25347c478bd9Sstevel@tonic-gate }
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate /*
25377c478bd9Sstevel@tonic-gate  * Used to set/clear phyint flags, by making a SIOCSLIFFLAGS call.
25387c478bd9Sstevel@tonic-gate  */
25397c478bd9Sstevel@tonic-gate boolean_t
2540*e11c3f44Smeem change_pif_flags(struct phyint *pi, uint64_t set, uint64_t clear)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	int ifsock;
25437c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
254428f13c35Srk 	uint64_t old_flags;
25457c478bd9Sstevel@tonic-gate 
2546*e11c3f44Smeem 	if (debug & D_FAILREP) {
2547*e11c3f44Smeem 		logdebug("change_pif_flags(%s): set %llx clear %llx\n",
2548*e11c3f44Smeem 		    pi->pi_name, set, clear);
25497c478bd9Sstevel@tonic-gate 	}
25507c478bd9Sstevel@tonic-gate 
2551*e11c3f44Smeem 	if (pi->pi_v4 != NULL)
25527c478bd9Sstevel@tonic-gate 		ifsock = ifsock_v4;
2553*e11c3f44Smeem 	else
25547c478bd9Sstevel@tonic-gate 		ifsock = ifsock_v6;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	/*
25577c478bd9Sstevel@tonic-gate 	 * Get the current flags from the kernel, and set/clear the
25587c478bd9Sstevel@tonic-gate 	 * desired phyint flags. Since we set only phyint flags, we can
25597c478bd9Sstevel@tonic-gate 	 * do it on either IPv4 or IPv6 instance.
25607c478bd9Sstevel@tonic-gate 	 */
2561*e11c3f44Smeem 	(void) strlcpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
2562*e11c3f44Smeem 
25637c478bd9Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
25647c478bd9Sstevel@tonic-gate 		if (errno != ENXIO)
2565*e11c3f44Smeem 			logperror("change_pif_flags: ioctl (get flags)");
25667c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
25677c478bd9Sstevel@tonic-gate 	}
256828f13c35Srk 
256928f13c35Srk 	old_flags = lifr.lifr_flags;
2570*e11c3f44Smeem 	lifr.lifr_flags |= set;
2571*e11c3f44Smeem 	lifr.lifr_flags &= ~clear;
257228f13c35Srk 
257328f13c35Srk 	if (old_flags == lifr.lifr_flags) {
257428f13c35Srk 		/* No change in the flags. No need to send ioctl */
257528f13c35Srk 		return (_B_TRUE);
257628f13c35Srk 	}
257728f13c35Srk 
25787c478bd9Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCSLIFFLAGS, (char *)&lifr) < 0) {
25797c478bd9Sstevel@tonic-gate 		if (errno != ENXIO)
2580*e11c3f44Smeem 			logperror("change_pif_flags: ioctl (set flags)");
25817c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
25827c478bd9Sstevel@tonic-gate 	}
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	/*
25857c478bd9Sstevel@tonic-gate 	 * Keep pi_flags in synch. with actual flags. Assumes flags are
25867c478bd9Sstevel@tonic-gate 	 * phyint flags.
25877c478bd9Sstevel@tonic-gate 	 */
2588*e11c3f44Smeem 	pi->pi_flags |= set;
2589*e11c3f44Smeem 	pi->pi_flags &= ~clear;
25907c478bd9Sstevel@tonic-gate 
2591*e11c3f44Smeem 	if (pi->pi_v4 != NULL)
25927c478bd9Sstevel@tonic-gate 		pi->pi_v4->pii_flags = pi->pi_flags;
25937c478bd9Sstevel@tonic-gate 
2594*e11c3f44Smeem 	if (pi->pi_v6 != NULL)
25957c478bd9Sstevel@tonic-gate 		pi->pi_v6->pii_flags = pi->pi_flags;
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	return (_B_TRUE);
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate /*
26017c478bd9Sstevel@tonic-gate  * icmp cksum computation for IPv4.
26027c478bd9Sstevel@tonic-gate  */
26037c478bd9Sstevel@tonic-gate static int
26047c478bd9Sstevel@tonic-gate in_cksum(ushort_t *addr, int len)
26057c478bd9Sstevel@tonic-gate {
26067c478bd9Sstevel@tonic-gate 	register int nleft = len;
26077c478bd9Sstevel@tonic-gate 	register ushort_t *w = addr;
26087c478bd9Sstevel@tonic-gate 	register ushort_t answer;
26097c478bd9Sstevel@tonic-gate 	ushort_t odd_byte = 0;
26107c478bd9Sstevel@tonic-gate 	register int sum = 0;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	/*
26137c478bd9Sstevel@tonic-gate 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
26147c478bd9Sstevel@tonic-gate 	 *  we add sequential 16 bit words to it, and at the end, fold
26157c478bd9Sstevel@tonic-gate 	 *  back all the carry bits from the top 16 bits into the lower
26167c478bd9Sstevel@tonic-gate 	 *  16 bits.
26177c478bd9Sstevel@tonic-gate 	 */
26187c478bd9Sstevel@tonic-gate 	while (nleft > 1)  {
26197c478bd9Sstevel@tonic-gate 		sum += *w++;
26207c478bd9Sstevel@tonic-gate 		nleft -= 2;
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	/* mop up an odd byte, if necessary */
26247c478bd9Sstevel@tonic-gate 	if (nleft == 1) {
26257c478bd9Sstevel@tonic-gate 		*(uchar_t *)(&odd_byte) = *(uchar_t *)w;
26267c478bd9Sstevel@tonic-gate 		sum += odd_byte;
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	/*
26307c478bd9Sstevel@tonic-gate 	 * add back carry outs from top 16 bits to low 16 bits
26317c478bd9Sstevel@tonic-gate 	 */
26327c478bd9Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
26337c478bd9Sstevel@tonic-gate 	sum += (sum >> 16);			/* add carry */
26347c478bd9Sstevel@tonic-gate 	answer = ~sum;				/* truncate to 16 bits */
26357c478bd9Sstevel@tonic-gate 	return (answer);
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate static void
26397c478bd9Sstevel@tonic-gate reset_snxt_basetimes(void)
26407c478bd9Sstevel@tonic-gate {
26417c478bd9Sstevel@tonic-gate 	struct phyint_instance *pii;
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
26447c478bd9Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
26457c478bd9Sstevel@tonic-gate 	}
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate  * Is the address one of our own addresses? Unfortunately,
26507c478bd9Sstevel@tonic-gate  * we cannot check our phyint tables to determine if the address
26517c478bd9Sstevel@tonic-gate  * is our own. This is because, we don't track interfaces that
26527c478bd9Sstevel@tonic-gate  * are not part of any group. We have to either use a 'bind' or
26537c478bd9Sstevel@tonic-gate  * get the complete list of all interfaces using SIOCGLIFCONF,
265487e66ffcSrk  * to do this check. We could also use SIOCTMYADDR.
265587e66ffcSrk  * Bind fails for the local zone address, so we might include local zone
265687e66ffcSrk  * address as target address. If local zone address is a target address
265787e66ffcSrk  * and it is up, it is not possible to detect the interface failure.
265887e66ffcSrk  * SIOCTMYADDR also doesn't consider local zone address as own address.
265987e66ffcSrk  * So, we choose to use SIOCGLIFCONF to collect the local addresses, and they
2660*e11c3f44Smeem  * are stored in `localaddrs'
26617c478bd9Sstevel@tonic-gate  */
26627c478bd9Sstevel@tonic-gate boolean_t
266387e66ffcSrk own_address(struct in6_addr addr)
26647c478bd9Sstevel@tonic-gate {
2665*e11c3f44Smeem 	addrlist_t *addrp;
2666*e11c3f44Smeem 	struct sockaddr_storage ss;
2667*e11c3f44Smeem 	int af = IN6_IS_ADDR_V4MAPPED(&addr) ? AF_INET : AF_INET6;
26687c478bd9Sstevel@tonic-gate 
2669*e11c3f44Smeem 	addr2storage(af, &addr, &ss);
2670*e11c3f44Smeem 	for (addrp = localaddrs; addrp != NULL; addrp = addrp->al_next) {
2671*e11c3f44Smeem 		if (sockaddrcmp(&ss, &addrp->al_addr))
267287e66ffcSrk 			return (_B_TRUE);
26737c478bd9Sstevel@tonic-gate 	}
267487e66ffcSrk 	return (_B_FALSE);
26757c478bd9Sstevel@tonic-gate }
2676*e11c3f44Smeem 
2677*e11c3f44Smeem static int
2678*e11c3f44Smeem ns2ms(int64_t ns)
2679*e11c3f44Smeem {
2680*e11c3f44Smeem 	return (ns / (NANOSEC / MILLISEC));
2681*e11c3f44Smeem }
2682*e11c3f44Smeem 
2683*e11c3f44Smeem static int64_t
2684*e11c3f44Smeem tv2ns(struct timeval *tvp)
2685*e11c3f44Smeem {
2686*e11c3f44Smeem 	return (tvp->tv_sec * NANOSEC + tvp->tv_usec * 1000);
2687*e11c3f44Smeem }
2688