xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/in.mpathd/mpd_probe.c (revision 10290:ecc1578c5db2)
1*10290SPeter.Memishian@Sun.COM 
20Sstevel@tonic-gate /*
38485SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
40Sstevel@tonic-gate  * Use is subject to license terms.
50Sstevel@tonic-gate  */
60Sstevel@tonic-gate 
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  * Copyright (c) 1987 Regents of the University of California.
90Sstevel@tonic-gate  * All rights reserved.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
120Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
130Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
140Sstevel@tonic-gate  * advertising materials, and other materials related to such
150Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
160Sstevel@tonic-gate  * by the University of California, Berkeley. The name of the
170Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
180Sstevel@tonic-gate  * from this software without specific prior written permission.
190Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
200Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
210Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
220Sstevel@tonic-gate  */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "mpd_defs.h"
250Sstevel@tonic-gate #include "mpd_tables.h"
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Probe types for probe()
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate #define	PROBE_UNI	0x1234		/* Unicast probe packet */
310Sstevel@tonic-gate #define	PROBE_MULTI	0x5678		/* Multicast probe packet */
320Sstevel@tonic-gate #define	PROBE_RTT	0x9abc		/* RTT only probe packet */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #define	MSEC_PERMIN	(60 * MILLISEC)	/* Number of milliseconds in a minute */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * Format of probe / probe response packets. This is an ICMP Echo request
380Sstevel@tonic-gate  * or ICMP Echo reply. Packet format is same for both IPv4 and IPv6
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate struct pr_icmp
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	uint8_t  pr_icmp_type;		/* type field */
430Sstevel@tonic-gate 	uint8_t  pr_icmp_code;		/* code field */
440Sstevel@tonic-gate 	uint16_t pr_icmp_cksum;		/* checksum field */
450Sstevel@tonic-gate 	uint16_t pr_icmp_id;		/* Identification */
460Sstevel@tonic-gate 	uint16_t pr_icmp_seq;		/* sequence number */
478485SPeter.Memishian@Sun.COM 	uint64_t pr_icmp_timestamp;	/* Time stamp (in ns) */
480Sstevel@tonic-gate 	uint32_t pr_icmp_mtype;		/* Message type */
490Sstevel@tonic-gate };
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static struct in6_addr all_nodes_mcast_v6 = { { 0xff, 0x2, 0x0, 0x0,
520Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
530Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
540Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x1 } };
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static struct in_addr all_nodes_mcast_v4 = { { { 0xe0, 0x0, 0x0, 0x1 } } };
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static hrtime_t	last_fdt_bumpup_time;	/* When FDT was bumped up last */
590Sstevel@tonic-gate 
608485SPeter.Memishian@Sun.COM static void		*find_ancillary(struct msghdr *msg, int cmsg_level,
618485SPeter.Memishian@Sun.COM     int cmsg_type);
628485SPeter.Memishian@Sun.COM static void		pi_set_crtt(struct target *tg, int64_t m,
630Sstevel@tonic-gate     boolean_t is_probe_uni);
640Sstevel@tonic-gate static void		incoming_echo_reply(struct phyint_instance *pii,
658485SPeter.Memishian@Sun.COM     struct pr_icmp *reply, struct in6_addr fromaddr, struct timeval *recv_tvp);
660Sstevel@tonic-gate static void		incoming_rtt_reply(struct phyint_instance *pii,
670Sstevel@tonic-gate     struct pr_icmp *reply, struct in6_addr fromaddr);
680Sstevel@tonic-gate static void		incoming_mcast_reply(struct phyint_instance *pii,
690Sstevel@tonic-gate     struct pr_icmp *reply, struct in6_addr fromaddr);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static boolean_t	check_pg_crtt_improved(struct phyint_group *pg);
720Sstevel@tonic-gate static boolean_t	check_pii_crtt_improved(struct phyint_instance *pii);
730Sstevel@tonic-gate static boolean_t	check_exception_target(struct phyint_instance *pii,
740Sstevel@tonic-gate     struct target *target);
750Sstevel@tonic-gate static void		probe_fail_info(struct phyint_instance *pii,
760Sstevel@tonic-gate     struct target *cur_tg, struct probe_fail_count *pfinfo);
770Sstevel@tonic-gate static void		probe_success_info(struct phyint_instance *pii,
780Sstevel@tonic-gate     struct target *cur_tg, struct probe_success_count *psinfo);
790Sstevel@tonic-gate static boolean_t	phyint_repaired(struct phyint *pi);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static boolean_t	highest_ack_tg(uint16_t seq, struct target *tg);
820Sstevel@tonic-gate static int 		in_cksum(ushort_t *addr, int len);
830Sstevel@tonic-gate static void		reset_snxt_basetimes(void);
848485SPeter.Memishian@Sun.COM static int		ns2ms(int64_t ns);
858485SPeter.Memishian@Sun.COM static int64_t		tv2ns(struct timeval *);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * CRTT - Conservative Round Trip Time Estimate
890Sstevel@tonic-gate  * Probe success - A matching probe reply received before CRTT ms has elapsed
900Sstevel@tonic-gate  *	after sending the probe.
910Sstevel@tonic-gate  * Probe failure - No probe reply received and more than CRTT ms has elapsed
920Sstevel@tonic-gate  *	after sending the probe.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * TLS - Time last success. Most recent probe ack received at this time.
950Sstevel@tonic-gate  * TFF - Time first fail. The time of the earliest probe failure in
960Sstevel@tonic-gate  *	a consecutive series of probe failures.
970Sstevel@tonic-gate  * NUM_PROBE_REPAIRS  - Number of consecutive successful probes required
980Sstevel@tonic-gate  * 	before declaring phyint repair.
990Sstevel@tonic-gate  * NUM_PROBE_FAILS - Number of consecutive probe failures required to
1000Sstevel@tonic-gate  *	declare a phyint failure.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * 			Phyint state diagram
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * The state of a phyint that is capable of being probed, is completely
1058485SPeter.Memishian@Sun.COM  * specified by the 3-tuple <pi_state, pg_state, I>.
1060Sstevel@tonic-gate  *
107*10290SPeter.Memishian@Sun.COM  * A phyint starts in either PI_RUNNING or PI_OFFLINE, depending on whether
108*10290SPeter.Memishian@Sun.COM  * IFF_OFFLINE is set.  If the phyint is also configured with a test address
109*10290SPeter.Memishian@Sun.COM  * (the common case) and probe targets, then a phyint must also successfully
110*10290SPeter.Memishian@Sun.COM  * be able to send and receive probes in order to remain in the PI_RUNNING
111*10290SPeter.Memishian@Sun.COM  * state (otherwise, it transitions to PI_FAILED).
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * Further, if a PI_RUNNING phyint is configured with a test address but is
1140Sstevel@tonic-gate  * unable to find any probe targets, it will transition to the PI_NOTARGETS
1150Sstevel@tonic-gate  * state, which indicates that the link is apparently functional but that
1160Sstevel@tonic-gate  * in.mpathd is unable to send probes to verify functionality (in this case,
1170Sstevel@tonic-gate  * in.mpathd makes the optimistic assumption that the interface is working
1188485SPeter.Memishian@Sun.COM  * correctly and thus does not mark the interface FAILED, but reports it as
1198485SPeter.Memishian@Sun.COM  * IPMP_IF_UNKNOWN through the async events and query interfaces).
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * At any point, a phyint may be administratively marked offline via if_mpadm.
1220Sstevel@tonic-gate  * In this case, the interface always transitions to PI_OFFLINE, regardless
1230Sstevel@tonic-gate  * of its previous state.  When the interface is later brought back online,
1240Sstevel@tonic-gate  * in.mpathd acts as if the interface is new (and thus it transitions to
1250Sstevel@tonic-gate  * PI_RUNNING or PI_FAILED based on the status of the link and the result of
1260Sstevel@tonic-gate  * its probes, if probes are sent).
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  * pi_state -  PI_RUNNING or PI_FAILED
1290Sstevel@tonic-gate  *	PI_RUNNING: The failure detection logic says the phyint is good.
1300Sstevel@tonic-gate  *	PI_FAILED: The failure detection logic says the phyint has failed.
1310Sstevel@tonic-gate  *
1328485SPeter.Memishian@Sun.COM  * pg_state  - PG_OK, PG_DEGRADED, or PG_FAILED.
1338485SPeter.Memishian@Sun.COM  *	PG_OK: All interfaces in the group are OK.
1348485SPeter.Memishian@Sun.COM  *	PG_DEGRADED: Some interfaces in the group are unusable.
1358485SPeter.Memishian@Sun.COM  *	PG_FAILED: All interfaces in the group are unusable.
1368485SPeter.Memishian@Sun.COM  *
1370Sstevel@tonic-gate  *	In the case of router targets, we assume that the current list of
1380Sstevel@tonic-gate  *	targets obtained from the routing table, is still valid, so the
1390Sstevel@tonic-gate  *	phyint stat is PI_FAILED. In the case of host targets, we delete the
1400Sstevel@tonic-gate  *	list of targets, and multicast to the all hosts, to reconstruct the
1410Sstevel@tonic-gate  *	target list. So the phyints are in the PI_NOTARGETS state.
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  * I -	value of (pi_flags & IFF_INACTIVE)
1448485SPeter.Memishian@Sun.COM  *	IFF_INACTIVE: This phyint will not send or receive packets.
1458485SPeter.Memishian@Sun.COM  *	Usually, inactive is tied to standby interfaces that are not yet
1468485SPeter.Memishian@Sun.COM  *	needed (e.g., no non-standby interfaces in the group have failed).
1478485SPeter.Memishian@Sun.COM  *	When failback has been disabled (FAILBACK=no configured), phyint can
1488485SPeter.Memishian@Sun.COM  *	also be a non-STANDBY. In this case IFF_INACTIVE is set when phyint
1498485SPeter.Memishian@Sun.COM  *	subsequently recovers after a failure.
1500Sstevel@tonic-gate  *
1518485SPeter.Memishian@Sun.COM  * Not all 9 possible combinations of the above 3-tuple are possible.
1520Sstevel@tonic-gate  *
1538485SPeter.Memishian@Sun.COM  * I is tracked by IP. pi_state is tracked by mpathd.
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  *			pi_state state machine
1560Sstevel@tonic-gate  * ---------------------------------------------------------------------------
1570Sstevel@tonic-gate  *	Event			State			New State
1580Sstevel@tonic-gate  *				Action:
1590Sstevel@tonic-gate  * ---------------------------------------------------------------------------
1608485SPeter.Memishian@Sun.COM  *	IP interface failure	(PI_RUNNING, I == 0) -> (PI_FAILED, I == 0)
1610Sstevel@tonic-gate  *	detection		: set IFF_FAILED on this phyint
1620Sstevel@tonic-gate  *
1638485SPeter.Memishian@Sun.COM  *	IP interface failure	(PI_RUNNING, I == 1) -> (PI_FAILED, I == 0)
1640Sstevel@tonic-gate  *	detection		: set IFF_FAILED on this phyint
1650Sstevel@tonic-gate  *
1668485SPeter.Memishian@Sun.COM  *	IP interface repair 	(PI_FAILED, I == 0, FAILBACK=yes)
167704Sethindra  *	detection				     -> (PI_RUNNING, I == 0)
1680Sstevel@tonic-gate  *				: clear IFF_FAILED on this phyint
1690Sstevel@tonic-gate  *
1708485SPeter.Memishian@Sun.COM  *	IP interface repair 	(PI_FAILED, I == 0, FAILBACK=no)
171704Sethindra  *	detection				     ->	(PI_RUNNING, I == 1)
172704Sethindra  *				: clear IFF_FAILED on this phyint
173704Sethindra  *				: if failback is disabled set I == 1
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  *	Group failure		(perform on all phyints in the group)
1760Sstevel@tonic-gate  *	detection 		PI_RUNNING		PI_FAILED
1770Sstevel@tonic-gate  *	(Router targets)	: set IFF_FAILED
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  *	Group failure		(perform on all phyints in the group)
1800Sstevel@tonic-gate  *	detection 		PI_RUNNING		PI_NOTARGETS
1810Sstevel@tonic-gate  *	(Host targets)		: set IFF_FAILED
1820Sstevel@tonic-gate  *				: delete the target list on all phyints
1830Sstevel@tonic-gate  * ---------------------------------------------------------------------------
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate struct probes_missed probes_missed;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Compose and transmit an ICMP ECHO REQUEST packet.  The IP header
1900Sstevel@tonic-gate  * will be added on by the kernel.  The id field identifies this phyint.
1910Sstevel@tonic-gate  * and the sequence number is an increasing (modulo 2^^16) integer. The data
1920Sstevel@tonic-gate  * portion holds the time value when the packet is sent. On echo this is
1930Sstevel@tonic-gate  * extracted to compute the round-trip time. Three different types of
1940Sstevel@tonic-gate  * probe packets are used.
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  * PROBE_UNI: This type is used to do failure detection / failure recovery
1970Sstevel@tonic-gate  *	and RTT calculation. PROBE_UNI probes are spaced apart in time,
1980Sstevel@tonic-gate  *	not less than the current CRTT. pii_probes[] stores data
1990Sstevel@tonic-gate  *	about these probes. These packets consume sequence number space.
2000Sstevel@tonic-gate  *
2018485SPeter.Memishian@Sun.COM  * PROBE_RTT: This type is used to make only rtt measurements. Normally these
2020Sstevel@tonic-gate  * 	are not used. Under heavy network load, the rtt may go up very high,
2030Sstevel@tonic-gate  *	due to a spike, or may appear to go high, due to extreme scheduling
2040Sstevel@tonic-gate  * 	delays. Once the network stress is removed, mpathd takes long time to
2050Sstevel@tonic-gate  *	recover, because the probe_interval is already high, and it takes
2060Sstevel@tonic-gate  *	a long time to send out sufficient number of probes to bring down the
2070Sstevel@tonic-gate  *	rtt. To avoid this problem, PROBE_RTT probes are sent out every
2080Sstevel@tonic-gate  *	user_probe_interval ms. and will cause only rtt updates. These packets
2090Sstevel@tonic-gate  *	do not consume sequence number space nor is information about these
2100Sstevel@tonic-gate  *	packets stored in the pii_probes[]
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  * PROBE_MULTI: This type is only used to construct a list of targets, when
2130Sstevel@tonic-gate  *	no targets are known. The packet is multicast to the all hosts addr.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate static void
2168485SPeter.Memishian@Sun.COM probe(struct phyint_instance *pii, uint_t probe_type, hrtime_t start_hrtime)
2170Sstevel@tonic-gate {
2188485SPeter.Memishian@Sun.COM 	hrtime_t sent_hrtime;
2198485SPeter.Memishian@Sun.COM 	struct timeval sent_tv;
2200Sstevel@tonic-gate 	struct pr_icmp probe_pkt;	/* Probe packet */
2218485SPeter.Memishian@Sun.COM 	struct sockaddr_storage targ;	/* target address */
2228485SPeter.Memishian@Sun.COM 	uint_t	targaddrlen;		/* targed address length */
2230Sstevel@tonic-gate 	int	pr_ndx;			/* probe index in pii->pii_probes[] */
2240Sstevel@tonic-gate 	boolean_t sent = _B_TRUE;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (debug & D_TARGET) {
2278485SPeter.Memishian@Sun.COM 		logdebug("probe(%s %s %d %lld)\n", AF_STR(pii->pii_af),
2288485SPeter.Memishian@Sun.COM 		    pii->pii_name, probe_type, start_hrtime);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	assert(pii->pii_probe_sock != -1);
2320Sstevel@tonic-gate 	assert(probe_type == PROBE_UNI || probe_type == PROBE_MULTI ||
2330Sstevel@tonic-gate 	    probe_type == PROBE_RTT);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	probe_pkt.pr_icmp_type = (pii->pii_af == AF_INET) ?
2360Sstevel@tonic-gate 	    ICMP_ECHO_REQUEST : ICMP6_ECHO_REQUEST;
2370Sstevel@tonic-gate 	probe_pkt.pr_icmp_code = 0;
2380Sstevel@tonic-gate 	probe_pkt.pr_icmp_cksum = 0;
2390Sstevel@tonic-gate 	probe_pkt.pr_icmp_seq = htons(pii->pii_snxt);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/*
2420Sstevel@tonic-gate 	 * Since there is no need to do arithmetic on the icmpid,
2430Sstevel@tonic-gate 	 * (only equality check is done) pii_icmpid is stored in
2440Sstevel@tonic-gate 	 * network byte order at initialization itself.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	probe_pkt.pr_icmp_id = pii->pii_icmpid;
2478485SPeter.Memishian@Sun.COM 	probe_pkt.pr_icmp_timestamp = htonll(start_hrtime);
2480Sstevel@tonic-gate 	probe_pkt.pr_icmp_mtype = htonl(probe_type);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * If probe_type is PROBE_MULTI, this packet will be multicast to
2520Sstevel@tonic-gate 	 * the all hosts address. Otherwise it is unicast to the next target.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	assert(probe_type == PROBE_MULTI || ((pii->pii_target_next != NULL) &&
2550Sstevel@tonic-gate 	    pii->pii_rtt_target_next != NULL));
2560Sstevel@tonic-gate 
2578485SPeter.Memishian@Sun.COM 	bzero(&targ, sizeof (targ));
2588485SPeter.Memishian@Sun.COM 	targ.ss_family = pii->pii_af;
2598485SPeter.Memishian@Sun.COM 
2600Sstevel@tonic-gate 	if (pii->pii_af == AF_INET6) {
2618485SPeter.Memishian@Sun.COM 		struct in6_addr *addr6;
2628485SPeter.Memishian@Sun.COM 
2638485SPeter.Memishian@Sun.COM 		addr6 = &((struct sockaddr_in6 *)&targ)->sin6_addr;
2648485SPeter.Memishian@Sun.COM 		targaddrlen = sizeof (struct sockaddr_in6);
2650Sstevel@tonic-gate 		if (probe_type == PROBE_MULTI) {
2668485SPeter.Memishian@Sun.COM 			*addr6 = all_nodes_mcast_v6;
2670Sstevel@tonic-gate 		} else if (probe_type == PROBE_UNI) {
2688485SPeter.Memishian@Sun.COM 			*addr6 = pii->pii_target_next->tg_address;
2698485SPeter.Memishian@Sun.COM 		} else { /* type is PROBE_RTT */
2708485SPeter.Memishian@Sun.COM 			*addr6 = pii->pii_rtt_target_next->tg_address;
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 	} else {
2738485SPeter.Memishian@Sun.COM 		struct in_addr *addr4;
2748485SPeter.Memishian@Sun.COM 
2758485SPeter.Memishian@Sun.COM 		addr4 = &((struct sockaddr_in *)&targ)->sin_addr;
2768485SPeter.Memishian@Sun.COM 		targaddrlen = sizeof (struct sockaddr_in);
2770Sstevel@tonic-gate 		if (probe_type == PROBE_MULTI) {
2788485SPeter.Memishian@Sun.COM 			*addr4 = all_nodes_mcast_v4;
2790Sstevel@tonic-gate 		} else if (probe_type == PROBE_UNI) {
2800Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(
2818485SPeter.Memishian@Sun.COM 			    &pii->pii_target_next->tg_address, addr4);
2828485SPeter.Memishian@Sun.COM 		} else { /* type is PROBE_RTT */
2830Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(
2848485SPeter.Memishian@Sun.COM 			    &pii->pii_rtt_target_next->tg_address, addr4);
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		/*
2880Sstevel@tonic-gate 		 * Compute the IPv4 icmp checksum. Does not cover the IP header.
2890Sstevel@tonic-gate 		 */
2900Sstevel@tonic-gate 		probe_pkt.pr_icmp_cksum =
2910Sstevel@tonic-gate 		    in_cksum((ushort_t *)&probe_pkt, (int)sizeof (probe_pkt));
2928485SPeter.Memishian@Sun.COM 	}
2938485SPeter.Memishian@Sun.COM 
2948485SPeter.Memishian@Sun.COM 	/*
2958485SPeter.Memishian@Sun.COM 	 * Use the current time as the time we sent.  Not atomic, but the best
2968485SPeter.Memishian@Sun.COM 	 * we can do from here.
2978485SPeter.Memishian@Sun.COM 	 */
2988485SPeter.Memishian@Sun.COM 	sent_hrtime = gethrtime();
2998485SPeter.Memishian@Sun.COM 	(void) gettimeofday(&sent_tv, NULL);
3008485SPeter.Memishian@Sun.COM 	if (sendto(pii->pii_probe_sock, &probe_pkt, sizeof (probe_pkt), 0,
3018485SPeter.Memishian@Sun.COM 	    (struct sockaddr *)&targ, targaddrlen) != sizeof (probe_pkt)) {
3028485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "probe: probe sendto");
3038485SPeter.Memishian@Sun.COM 		sent = _B_FALSE;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * If this is a PROBE_UNI probe packet being unicast to a target, then
3080Sstevel@tonic-gate 	 * update our tables. We will need this info in processing the probe
3090Sstevel@tonic-gate 	 * response. PROBE_MULTI and PROBE_RTT packets are not used for
3100Sstevel@tonic-gate 	 * the purpose of failure or recovery detection. PROBE_MULTI packets
3110Sstevel@tonic-gate 	 * are only used to construct a list of targets. PROBE_RTT packets are
3120Sstevel@tonic-gate 	 * used only for updating the rtt and not for failure detection.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	if (probe_type == PROBE_UNI && sent) {
3150Sstevel@tonic-gate 		pr_ndx = pii->pii_probe_next;
3160Sstevel@tonic-gate 		assert(pr_ndx >= 0 && pr_ndx < PROBE_STATS_COUNT);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		/* Collect statistics, before we reuse the last slot. */
3190Sstevel@tonic-gate 		if (pii->pii_probes[pr_ndx].pr_status == PR_LOST)
3200Sstevel@tonic-gate 			pii->pii_cum_stats.lost++;
3210Sstevel@tonic-gate 		else if (pii->pii_probes[pr_ndx].pr_status == PR_ACKED)
3220Sstevel@tonic-gate 			pii->pii_cum_stats.acked++;
3230Sstevel@tonic-gate 		pii->pii_cum_stats.sent++;
3240Sstevel@tonic-gate 
3258485SPeter.Memishian@Sun.COM 		pii->pii_probes[pr_ndx].pr_id = pii->pii_snxt;
3268485SPeter.Memishian@Sun.COM 		pii->pii_probes[pr_ndx].pr_tv_sent = sent_tv;
3278485SPeter.Memishian@Sun.COM 		pii->pii_probes[pr_ndx].pr_hrtime_sent = sent_hrtime;
3288485SPeter.Memishian@Sun.COM 		pii->pii_probes[pr_ndx].pr_hrtime_start = start_hrtime;
3290Sstevel@tonic-gate 		pii->pii_probes[pr_ndx].pr_target = pii->pii_target_next;
3308485SPeter.Memishian@Sun.COM 		probe_chstate(&pii->pii_probes[pr_ndx], pii, PR_UNACKED);
3318485SPeter.Memishian@Sun.COM 
3320Sstevel@tonic-gate 		pii->pii_probe_next = PROBE_INDEX_NEXT(pii->pii_probe_next);
3330Sstevel@tonic-gate 		pii->pii_target_next = target_next(pii->pii_target_next);
3340Sstevel@tonic-gate 		assert(pii->pii_target_next != NULL);
3350Sstevel@tonic-gate 		/*
3360Sstevel@tonic-gate 		 * If we have a single variable to denote the next target to
3370Sstevel@tonic-gate 		 * probe for both rtt probes and failure detection probes, we
3380Sstevel@tonic-gate 		 * could end up with a situation where the failure detection
3390Sstevel@tonic-gate 		 * probe targets become disjoint from the rtt probe targets.
3400Sstevel@tonic-gate 		 * Eg. if 2 targets and the actual fdt is double the user
3410Sstevel@tonic-gate 		 * specified fdt. So we have 2 variables. In this scheme
3420Sstevel@tonic-gate 		 * we also reset pii_rtt_target_next for every fdt probe,
3430Sstevel@tonic-gate 		 * though that may not be necessary.
3440Sstevel@tonic-gate 		 */
3450Sstevel@tonic-gate 		pii->pii_rtt_target_next = pii->pii_target_next;
3460Sstevel@tonic-gate 		pii->pii_snxt++;
3470Sstevel@tonic-gate 	} else if (probe_type == PROBE_RTT) {
3480Sstevel@tonic-gate 		pii->pii_rtt_target_next =
3490Sstevel@tonic-gate 		    target_next(pii->pii_rtt_target_next);
3500Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next != NULL);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * Incoming IPv4 data from wire, is received here. Called from main.
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate void
3580Sstevel@tonic-gate in_data(struct phyint_instance *pii)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	struct	sockaddr_in 	from;
3610Sstevel@tonic-gate 	struct	in6_addr	fromaddr;
3628485SPeter.Memishian@Sun.COM 	static uint64_t in_packet[(IP_MAXPACKET + 1)/8];
3638485SPeter.Memishian@Sun.COM 	static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
3640Sstevel@tonic-gate 	struct ip *ip;
3650Sstevel@tonic-gate 	int 	iphlen;
3660Sstevel@tonic-gate 	int 	len;
3670Sstevel@tonic-gate 	char 	abuf[INET_ADDRSTRLEN];
3688485SPeter.Memishian@Sun.COM 	struct msghdr msg;
3698485SPeter.Memishian@Sun.COM 	struct iovec iov;
3708485SPeter.Memishian@Sun.COM 	struct pr_icmp *reply;
3718485SPeter.Memishian@Sun.COM 	struct timeval *recv_tvp;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if (debug & D_PROBE) {
3740Sstevel@tonic-gate 		logdebug("in_data(%s %s)\n",
3750Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3788485SPeter.Memishian@Sun.COM 	iov.iov_base = (char *)in_packet;
3798485SPeter.Memishian@Sun.COM 	iov.iov_len = sizeof (in_packet);
3808485SPeter.Memishian@Sun.COM 	msg.msg_iov = &iov;
3818485SPeter.Memishian@Sun.COM 	msg.msg_iovlen = 1;
3828485SPeter.Memishian@Sun.COM 	msg.msg_name = (struct sockaddr *)&from;
3838485SPeter.Memishian@Sun.COM 	msg.msg_namelen = sizeof (from);
3848485SPeter.Memishian@Sun.COM 	msg.msg_control = ancillary_data;
3858485SPeter.Memishian@Sun.COM 	msg.msg_controllen = sizeof (ancillary_data);
3868485SPeter.Memishian@Sun.COM 
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * Poll has already told us that a message is waiting,
3890Sstevel@tonic-gate 	 * on this socket. Read it now. We should not block.
3900Sstevel@tonic-gate 	 */
3918485SPeter.Memishian@Sun.COM 	if ((len = recvmsg(pii->pii_probe_sock, &msg, 0)) < 0) {
3928485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "in_data: recvmsg");
3930Sstevel@tonic-gate 		return;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/*
3978485SPeter.Memishian@Sun.COM 	 * If the datalink has indicated the link is down, don't go
3980Sstevel@tonic-gate 	 * any further.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	if (LINK_DOWN(pii->pii_phyint))
4010Sstevel@tonic-gate 		return;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/* Get the printable address for error reporting */
4040Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, &from.sin_addr, abuf, sizeof (abuf));
4050Sstevel@tonic-gate 
4068485SPeter.Memishian@Sun.COM 	/* Ignore packets > 64k or control buffers that don't fit */
4078485SPeter.Memishian@Sun.COM 	if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
4088485SPeter.Memishian@Sun.COM 		if (debug & D_PKTBAD) {
4098485SPeter.Memishian@Sun.COM 			logdebug("Truncated message: msg_flags 0x%x from %s\n",
4108485SPeter.Memishian@Sun.COM 			    msg.msg_flags, abuf);
4118485SPeter.Memishian@Sun.COM 		}
4128485SPeter.Memishian@Sun.COM 		return;
4138485SPeter.Memishian@Sun.COM 	}
4148485SPeter.Memishian@Sun.COM 
4150Sstevel@tonic-gate 	/* Make sure packet contains at least minimum ICMP header */
4160Sstevel@tonic-gate 	ip = (struct ip *)in_packet;
4170Sstevel@tonic-gate 	iphlen = ip->ip_hl << 2;
4180Sstevel@tonic-gate 	if (len < iphlen + ICMP_MINLEN) {
4190Sstevel@tonic-gate 		if (debug & D_PKTBAD) {
4200Sstevel@tonic-gate 			logdebug("in_data: packet too short (%d bytes)"
4210Sstevel@tonic-gate 			    " from %s\n", len, abuf);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 		return;
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	/*
4270Sstevel@tonic-gate 	 * Subtract the IP hdr length, 'len' will be length of the probe
4280Sstevel@tonic-gate 	 * reply, starting from the icmp hdr.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	len -= iphlen;
4310Sstevel@tonic-gate 	/* LINTED */
4320Sstevel@tonic-gate 	reply = (struct pr_icmp *)((char *)in_packet + iphlen);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/* Probe replies are icmp echo replies. Ignore anything else */
4350Sstevel@tonic-gate 	if (reply->pr_icmp_type != ICMP_ECHO_REPLY)
4360Sstevel@tonic-gate 		return;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * The icmp id should match what we sent, which is stored
4400Sstevel@tonic-gate 	 * in pi_icmpid. The icmp code for reply must be 0.
4410Sstevel@tonic-gate 	 * The reply content must be a struct pr_icmp
4420Sstevel@tonic-gate 	 */
4430Sstevel@tonic-gate 	if (reply->pr_icmp_id != pii->pii_icmpid) {
4440Sstevel@tonic-gate 		/* Not in response to our probe */
4450Sstevel@tonic-gate 		return;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (reply->pr_icmp_code != 0) {
4490Sstevel@tonic-gate 		logtrace("probe reply code %d from %s on %s\n",
4500Sstevel@tonic-gate 		    reply->pr_icmp_code, abuf, pii->pii_name);
4510Sstevel@tonic-gate 		return;
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (len < sizeof (struct pr_icmp)) {
4550Sstevel@tonic-gate 		logtrace("probe reply too short: %d bytes from %s on %s\n",
4560Sstevel@tonic-gate 		    len, abuf, pii->pii_name);
4570Sstevel@tonic-gate 		return;
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4608485SPeter.Memishian@Sun.COM 	recv_tvp = find_ancillary(&msg, SOL_SOCKET, SCM_TIMESTAMP);
4618485SPeter.Memishian@Sun.COM 	if (recv_tvp == NULL) {
4628485SPeter.Memishian@Sun.COM 		logtrace("message without timestamp from %s on %s\n",
4638485SPeter.Memishian@Sun.COM 		    abuf, pii->pii_name);
4648485SPeter.Memishian@Sun.COM 		return;
4658485SPeter.Memishian@Sun.COM 	}
4668485SPeter.Memishian@Sun.COM 
4670Sstevel@tonic-gate 	IN6_INADDR_TO_V4MAPPED(&from.sin_addr, &fromaddr);
4680Sstevel@tonic-gate 	if (reply->pr_icmp_mtype == htonl(PROBE_UNI))
4690Sstevel@tonic-gate 		/* Unicast probe reply */
4708485SPeter.Memishian@Sun.COM 		incoming_echo_reply(pii, reply, fromaddr, recv_tvp);
4710Sstevel@tonic-gate 	else if (reply->pr_icmp_mtype == htonl(PROBE_MULTI)) {
4720Sstevel@tonic-gate 		/* Multicast reply */
4730Sstevel@tonic-gate 		incoming_mcast_reply(pii, reply, fromaddr);
4740Sstevel@tonic-gate 	} else if (reply->pr_icmp_mtype == htonl(PROBE_RTT)) {
4750Sstevel@tonic-gate 		incoming_rtt_reply(pii, reply, fromaddr);
4760Sstevel@tonic-gate 	} else {
4770Sstevel@tonic-gate 		/* Probably not in response to our probe */
4780Sstevel@tonic-gate 		logtrace("probe reply type: %d from %s on %s\n",
4790Sstevel@tonic-gate 		    reply->pr_icmp_mtype, abuf, pii->pii_name);
4800Sstevel@tonic-gate 		return;
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * Incoming IPv6 data from wire is received here. Called from main.
4860Sstevel@tonic-gate  */
4870Sstevel@tonic-gate void
4880Sstevel@tonic-gate in6_data(struct phyint_instance *pii)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	struct sockaddr_in6 from;
4910Sstevel@tonic-gate 	static uint64_t in_packet[(IP_MAXPACKET + 1)/8];
4920Sstevel@tonic-gate 	static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
4930Sstevel@tonic-gate 	int len;
4940Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
4950Sstevel@tonic-gate 	struct msghdr msg;
4960Sstevel@tonic-gate 	struct iovec iov;
4978485SPeter.Memishian@Sun.COM 	void	*opt;
4980Sstevel@tonic-gate 	struct	pr_icmp *reply;
4998485SPeter.Memishian@Sun.COM 	struct	timeval *recv_tvp;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if (debug & D_PROBE) {
5020Sstevel@tonic-gate 		logdebug("in6_data(%s %s)\n",
5030Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	iov.iov_base = (char *)in_packet;
5070Sstevel@tonic-gate 	iov.iov_len = sizeof (in_packet);
5080Sstevel@tonic-gate 	msg.msg_iov = &iov;
5090Sstevel@tonic-gate 	msg.msg_iovlen = 1;
5100Sstevel@tonic-gate 	msg.msg_name = (struct sockaddr *)&from;
5110Sstevel@tonic-gate 	msg.msg_namelen = sizeof (from);
5120Sstevel@tonic-gate 	msg.msg_control = ancillary_data;
5130Sstevel@tonic-gate 	msg.msg_controllen = sizeof (ancillary_data);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if ((len = recvmsg(pii->pii_probe_sock, &msg, 0)) < 0) {
5168485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "in6_data: recvmsg");
5170Sstevel@tonic-gate 		return;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5218485SPeter.Memishian@Sun.COM 	 * If the datalink has indicated that the link is down, don't go
5220Sstevel@tonic-gate 	 * any further.
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	if (LINK_DOWN(pii->pii_phyint))
5250Sstevel@tonic-gate 		return;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/* Get the printable address for error reporting */
5280Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, &from.sin6_addr, abuf, sizeof (abuf));
5290Sstevel@tonic-gate 	if (len < ICMP_MINLEN) {
5300Sstevel@tonic-gate 		if (debug & D_PKTBAD) {
5310Sstevel@tonic-gate 			logdebug("Truncated message: msg_flags 0x%x from %s\n",
5320Sstevel@tonic-gate 			    msg.msg_flags, abuf);
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 		return;
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	/* Ignore packets > 64k or control buffers that don't fit */
5370Sstevel@tonic-gate 	if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
5380Sstevel@tonic-gate 		if (debug & D_PKTBAD) {
5390Sstevel@tonic-gate 			logdebug("Truncated message: msg_flags 0x%x from %s\n",
5400Sstevel@tonic-gate 			    msg.msg_flags, abuf);
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 		return;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	reply = (struct pr_icmp *)in_packet;
5460Sstevel@tonic-gate 	if (reply->pr_icmp_type != ICMP6_ECHO_REPLY)
5470Sstevel@tonic-gate 		return;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (reply->pr_icmp_id != pii->pii_icmpid) {
5500Sstevel@tonic-gate 		/* Not in response to our probe */
5510Sstevel@tonic-gate 		return;
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * The kernel has already verified the the ICMP checksum.
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	if (!IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
5580Sstevel@tonic-gate 		logtrace("ICMPv6 echo reply source address not linklocal from "
5590Sstevel@tonic-gate 		    "%s on %s\n", abuf, pii->pii_name);
5600Sstevel@tonic-gate 		return;
5610Sstevel@tonic-gate 	}
5628485SPeter.Memishian@Sun.COM 	opt = find_ancillary(&msg, IPPROTO_IPV6, IPV6_RTHDR);
5630Sstevel@tonic-gate 	if (opt != NULL) {
5640Sstevel@tonic-gate 		/* Can't allow routing headers in probe replies  */
5650Sstevel@tonic-gate 		logtrace("message with routing header from %s on %s\n",
5660Sstevel@tonic-gate 		    abuf, pii->pii_name);
5670Sstevel@tonic-gate 		return;
5680Sstevel@tonic-gate 	}
5698485SPeter.Memishian@Sun.COM 
5700Sstevel@tonic-gate 	if (reply->pr_icmp_code != 0) {
5710Sstevel@tonic-gate 		logtrace("probe reply code: %d from %s on %s\n",
5720Sstevel@tonic-gate 		    reply->pr_icmp_code, abuf, pii->pii_name);
5730Sstevel@tonic-gate 		return;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 	if (len < (sizeof (struct pr_icmp))) {
5760Sstevel@tonic-gate 		logtrace("probe reply too short: %d bytes from %s on %s\n",
5770Sstevel@tonic-gate 		    len, abuf, pii->pii_name);
5780Sstevel@tonic-gate 		return;
5790Sstevel@tonic-gate 	}
5808485SPeter.Memishian@Sun.COM 
5818485SPeter.Memishian@Sun.COM 	recv_tvp = find_ancillary(&msg, SOL_SOCKET, SCM_TIMESTAMP);
5828485SPeter.Memishian@Sun.COM 	if (recv_tvp == NULL) {
5838485SPeter.Memishian@Sun.COM 		logtrace("message without timestamp from %s on %s\n",
5848485SPeter.Memishian@Sun.COM 		    abuf, pii->pii_name);
5858485SPeter.Memishian@Sun.COM 		return;
5868485SPeter.Memishian@Sun.COM 	}
5878485SPeter.Memishian@Sun.COM 
5880Sstevel@tonic-gate 	if (reply->pr_icmp_mtype == htonl(PROBE_UNI)) {
5898485SPeter.Memishian@Sun.COM 		incoming_echo_reply(pii, reply, from.sin6_addr, recv_tvp);
5900Sstevel@tonic-gate 	} else if (reply->pr_icmp_mtype == htonl(PROBE_MULTI)) {
5910Sstevel@tonic-gate 		incoming_mcast_reply(pii, reply, from.sin6_addr);
5920Sstevel@tonic-gate 	} else if (reply->pr_icmp_mtype == htonl(PROBE_RTT)) {
5930Sstevel@tonic-gate 		incoming_rtt_reply(pii, reply, from.sin6_addr);
5940Sstevel@tonic-gate 	} else  {
5950Sstevel@tonic-gate 		/* Probably not in response to our probe */
5960Sstevel@tonic-gate 		logtrace("probe reply type: %d from %s on %s\n",
5970Sstevel@tonic-gate 		    reply->pr_icmp_mtype, abuf, pii->pii_name);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * Process the incoming rtt reply, in response to our rtt probe.
6030Sstevel@tonic-gate  * Common for both IPv4 and IPv6. Unlike incoming_echo_reply() we don't
6040Sstevel@tonic-gate  * have any stored information about the probe we sent. So we don't log
6050Sstevel@tonic-gate  * any errors if we receive bad replies.
6060Sstevel@tonic-gate  */
6070Sstevel@tonic-gate static void
6080Sstevel@tonic-gate incoming_rtt_reply(struct phyint_instance *pii, struct pr_icmp *reply,
6090Sstevel@tonic-gate     struct in6_addr fromaddr)
6100Sstevel@tonic-gate {
6118485SPeter.Memishian@Sun.COM 	int64_t	m;		/* rtt measurement in ns */
6120Sstevel@tonic-gate 	char	abuf[INET6_ADDRSTRLEN];
6130Sstevel@tonic-gate 	struct	target	*target;
6140Sstevel@tonic-gate 	struct 	phyint_group *pg;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/* Get the printable address for error reporting */
6170Sstevel@tonic-gate 	(void) pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf));
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (debug & D_PROBE) {
6200Sstevel@tonic-gate 		logdebug("incoming_rtt_reply: %s %s %s\n",
6210Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name, abuf);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* Do we know this target ? */
6250Sstevel@tonic-gate 	target = target_lookup(pii, fromaddr);
6260Sstevel@tonic-gate 	if (target == NULL)
6270Sstevel@tonic-gate 		return;
6280Sstevel@tonic-gate 
6298485SPeter.Memishian@Sun.COM 	m = (int64_t)(gethrtime() - ntohll(reply->pr_icmp_timestamp));
6300Sstevel@tonic-gate 	/* Invalid rtt. It has wrapped around */
6310Sstevel@tonic-gate 	if (m < 0)
6320Sstevel@tonic-gate 		return;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * Don't update rtt until we see NUM_PROBE_REPAIRS probe responses
6360Sstevel@tonic-gate 	 * The initial few responses after the interface is repaired may
6370Sstevel@tonic-gate 	 * contain high rtt's because they could have been queued up waiting
6380Sstevel@tonic-gate 	 * for ARP/NDP resolution on a failed interface.
6390Sstevel@tonic-gate 	 */
6400Sstevel@tonic-gate 	pg = pii->pii_phyint->pi_group;
6410Sstevel@tonic-gate 	if ((pii->pii_state != PI_RUNNING) || GROUP_FAILED(pg))
6420Sstevel@tonic-gate 		return;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/*
6450Sstevel@tonic-gate 	 * Update rtt only if the new rtt is lower than the current rtt.
6460Sstevel@tonic-gate 	 * (specified by the 3rd parameter to pi_set_crtt).
6470Sstevel@tonic-gate 	 * If a spike has caused the current probe_interval to be >
6480Sstevel@tonic-gate 	 * user_probe_interval, then this mechanism is used to bring down
6490Sstevel@tonic-gate 	 * the rtt rapidly once the network stress is removed.
6500Sstevel@tonic-gate 	 * If the new rtt is higher than the current rtt, we don't want to
6510Sstevel@tonic-gate 	 * update the rtt. We are having more than 1 outstanding probe and
6520Sstevel@tonic-gate 	 * the increase in rtt we are seeing is being unnecessarily weighted
6530Sstevel@tonic-gate 	 * many times. The regular rtt update will be handled by
6540Sstevel@tonic-gate 	 * incoming_echo_reply() and will take care of any rtt increase.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	pi_set_crtt(target, m, _B_FALSE);
6570Sstevel@tonic-gate 	if ((target->tg_crtt < (pg->pg_probeint / LOWER_FDT_TRIGGER)) &&
6580Sstevel@tonic-gate 	    (user_failure_detection_time < pg->pg_fdt) &&
6590Sstevel@tonic-gate 	    (last_fdt_bumpup_time + MIN_SETTLING_TIME < gethrtime())) {
6600Sstevel@tonic-gate 		/*
6610Sstevel@tonic-gate 		 * If the crtt has now dropped by a factor of LOWER_FT_TRIGGER,
6620Sstevel@tonic-gate 		 * investigate if we can improve the failure detection time to
6630Sstevel@tonic-gate 		 * meet whatever the user specified.
6640Sstevel@tonic-gate 		 */
6650Sstevel@tonic-gate 		if (check_pg_crtt_improved(pg)) {
6660Sstevel@tonic-gate 			pg->pg_fdt = MAX(pg->pg_fdt / NEXT_FDT_MULTIPLE,
6670Sstevel@tonic-gate 			    user_failure_detection_time);
6680Sstevel@tonic-gate 			pg->pg_probeint = pg->pg_fdt / (NUM_PROBE_FAILS + 2);
6690Sstevel@tonic-gate 			if (pii->pii_phyint->pi_group != phyint_anongroup) {
6700Sstevel@tonic-gate 				logerr("Improved failure detection time %d ms "
6710Sstevel@tonic-gate 				    "on (%s %s) for group \"%s\"\n",
6720Sstevel@tonic-gate 				    pg->pg_fdt, AF_STR(pii->pii_af),
6730Sstevel@tonic-gate 				    pii->pii_name,
6740Sstevel@tonic-gate 				    pii->pii_phyint->pi_group->pg_name);
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 			if (user_failure_detection_time == pg->pg_fdt) {
6770Sstevel@tonic-gate 				/* Avoid any truncation or rounding errors */
6780Sstevel@tonic-gate 				pg->pg_probeint = user_probe_interval;
6790Sstevel@tonic-gate 				/*
6800Sstevel@tonic-gate 				 * No more rtt probes will be sent. The actual
6810Sstevel@tonic-gate 				 * fdt has dropped to the user specified value.
6820Sstevel@tonic-gate 				 * pii_fd_snxt_basetime and pii_snxt_basetime
6830Sstevel@tonic-gate 				 * will be in sync henceforth.
6840Sstevel@tonic-gate 				 */
6850Sstevel@tonic-gate 				reset_snxt_basetimes();
6860Sstevel@tonic-gate 			}
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate  * Process the incoming echo reply, in response to our unicast probe.
6930Sstevel@tonic-gate  * Common for both IPv4 and IPv6
6940Sstevel@tonic-gate  */
6950Sstevel@tonic-gate static void
6960Sstevel@tonic-gate incoming_echo_reply(struct phyint_instance *pii, struct pr_icmp *reply,
6978485SPeter.Memishian@Sun.COM     struct in6_addr fromaddr, struct timeval *recv_tvp)
6980Sstevel@tonic-gate {
6998485SPeter.Memishian@Sun.COM 	int64_t	m;		/* rtt measurement in ns */
7008485SPeter.Memishian@Sun.COM 	hrtime_t cur_hrtime;	/* in ns from some arbitrary point */
7010Sstevel@tonic-gate 	char	abuf[INET6_ADDRSTRLEN];
7020Sstevel@tonic-gate 	int	pr_ndx;
7030Sstevel@tonic-gate 	struct	target	*target;
7040Sstevel@tonic-gate 	boolean_t exception;
7058485SPeter.Memishian@Sun.COM 	uint64_t pr_icmp_timestamp;
7060Sstevel@tonic-gate 	uint16_t pr_icmp_seq;
7078485SPeter.Memishian@Sun.COM 	struct	probe_stats *pr_statp;
7080Sstevel@tonic-gate 	struct 	phyint_group *pg = pii->pii_phyint->pi_group;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* Get the printable address for error reporting */
7110Sstevel@tonic-gate 	(void) pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf));
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (debug & D_PROBE) {
7148485SPeter.Memishian@Sun.COM 		logdebug("incoming_echo_reply: %s %s %s seq %u recv_tvp %lld\n",
7150Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name, abuf,
7168485SPeter.Memishian@Sun.COM 		    ntohs(reply->pr_icmp_seq), tv2ns(recv_tvp));
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7198485SPeter.Memishian@Sun.COM 	pr_icmp_timestamp = ntohll(reply->pr_icmp_timestamp);
7208485SPeter.Memishian@Sun.COM 	pr_icmp_seq = ntohs(reply->pr_icmp_seq);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/* Reject out of window probe replies */
7230Sstevel@tonic-gate 	if (SEQ_GE(pr_icmp_seq, pii->pii_snxt) ||
7240Sstevel@tonic-gate 	    SEQ_LT(pr_icmp_seq, pii->pii_snxt - PROBE_STATS_COUNT)) {
7250Sstevel@tonic-gate 		logtrace("out of window probe seq %u snxt %u on %s from %s\n",
7260Sstevel@tonic-gate 		    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
7270Sstevel@tonic-gate 		pii->pii_cum_stats.unknown++;
7280Sstevel@tonic-gate 		return;
7290Sstevel@tonic-gate 	}
7308485SPeter.Memishian@Sun.COM 
7318485SPeter.Memishian@Sun.COM 	cur_hrtime = gethrtime();
7328485SPeter.Memishian@Sun.COM 	m = (int64_t)(cur_hrtime - pr_icmp_timestamp);
7330Sstevel@tonic-gate 	if (m < 0) {
7340Sstevel@tonic-gate 		/*
7350Sstevel@tonic-gate 		 * This is a ridiculously high value of rtt. rtt has wrapped
7360Sstevel@tonic-gate 		 * around. Log a message, and ignore the rtt.
7370Sstevel@tonic-gate 		 */
7388485SPeter.Memishian@Sun.COM 		logerr("incoming_echo_reply: rtt wraparound cur_hrtime %lld "
7398485SPeter.Memishian@Sun.COM 		    "reply timestamp %lld\n", cur_hrtime, pr_icmp_timestamp);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/*
7430Sstevel@tonic-gate 	 * Get the probe index pr_ndx corresponding to the received icmp seq.
7440Sstevel@tonic-gate 	 * number in our pii->pii_probes[] array. The icmp sequence number
7450Sstevel@tonic-gate 	 * pii_snxt corresponds to the probe index pii->pii_probe_next
7460Sstevel@tonic-gate 	 */
7470Sstevel@tonic-gate 	pr_ndx = MOD_SUB(pii->pii_probe_next,
7480Sstevel@tonic-gate 	    (uint16_t)(pii->pii_snxt - pr_icmp_seq), PROBE_STATS_COUNT);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	assert(PR_STATUS_VALID(pii->pii_probes[pr_ndx].pr_status));
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	target = pii->pii_probes[pr_ndx].pr_target;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	/*
7550Sstevel@tonic-gate 	 * Perform sanity checks, whether this probe reply that we
7560Sstevel@tonic-gate 	 * have received is genuine
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	if (target != NULL) {
7590Sstevel@tonic-gate 		/*
7600Sstevel@tonic-gate 		 * Compare the src. addr of the received ICMP or ICMPv6
7610Sstevel@tonic-gate 		 * probe reply with the target address in our tables.
7620Sstevel@tonic-gate 		 */
7630Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&target->tg_address, &fromaddr)) {
7640Sstevel@tonic-gate 			/*
7650Sstevel@tonic-gate 			 * We don't have any record of having sent a probe to
7660Sstevel@tonic-gate 			 * this target. This is a fake probe reply. Log an error
7670Sstevel@tonic-gate 			 */
7680Sstevel@tonic-gate 			logtrace("probe status %d Fake probe reply seq %u "
7690Sstevel@tonic-gate 			    "snxt %u on %s from %s\n",
7700Sstevel@tonic-gate 			    pii->pii_probes[pr_ndx].pr_status,
7710Sstevel@tonic-gate 			    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
7720Sstevel@tonic-gate 			pii->pii_cum_stats.unknown++;
7730Sstevel@tonic-gate 			return;
7740Sstevel@tonic-gate 		} else if (pii->pii_probes[pr_ndx].pr_status == PR_ACKED) {
7750Sstevel@tonic-gate 			/*
7760Sstevel@tonic-gate 			 * The address matches, but our tables indicate that
7770Sstevel@tonic-gate 			 * this probe reply has been acked already. So this
7780Sstevel@tonic-gate 			 * is a duplicate probe reply. Log an error
7790Sstevel@tonic-gate 			 */
7800Sstevel@tonic-gate 			logtrace("probe status %d Duplicate probe reply seq %u "
7810Sstevel@tonic-gate 			    "snxt %u on %s from %s\n",
7820Sstevel@tonic-gate 			    pii->pii_probes[pr_ndx].pr_status,
7830Sstevel@tonic-gate 			    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
7840Sstevel@tonic-gate 			pii->pii_cum_stats.unknown++;
7850Sstevel@tonic-gate 			return;
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 	} else {
7880Sstevel@tonic-gate 		/*
7890Sstevel@tonic-gate 		 * Target must not be NULL in the PR_UNACKED state
7900Sstevel@tonic-gate 		 */
7910Sstevel@tonic-gate 		assert(pii->pii_probes[pr_ndx].pr_status != PR_UNACKED);
7920Sstevel@tonic-gate 		if (pii->pii_probes[pr_ndx].pr_status == PR_UNUSED) {
7930Sstevel@tonic-gate 			/*
7940Sstevel@tonic-gate 			 * The probe stats slot is unused. So we didn't
7950Sstevel@tonic-gate 			 * send out any probe to this target. This is a fake.
7960Sstevel@tonic-gate 			 * Log an error.
7970Sstevel@tonic-gate 			 */
7980Sstevel@tonic-gate 			logtrace("probe status %d Fake probe reply seq %u "
7990Sstevel@tonic-gate 			    "snxt %u on %s from %s\n",
8000Sstevel@tonic-gate 			    pii->pii_probes[pr_ndx].pr_status,
8010Sstevel@tonic-gate 			    pr_icmp_seq, pii->pii_snxt, pii->pii_name, abuf);
8020Sstevel@tonic-gate 		}
8030Sstevel@tonic-gate 		pii->pii_cum_stats.unknown++;
8040Sstevel@tonic-gate 		return;
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/*
8080Sstevel@tonic-gate 	 * If the rtt does not appear to be right, don't update the
8090Sstevel@tonic-gate 	 * rtt stats. This can happen if the system dropped into the
8100Sstevel@tonic-gate 	 * debugger, or the system was hung or too busy for a
8110Sstevel@tonic-gate 	 * substantial time that we didn't get a chance to run.
8120Sstevel@tonic-gate 	 */
8138485SPeter.Memishian@Sun.COM 	if ((m < 0) || (ns2ms(m) > PROBE_STATS_COUNT * pg->pg_probeint)) {
8140Sstevel@tonic-gate 		/*
8158485SPeter.Memishian@Sun.COM 		 * If the probe corresponding to this received response
8168485SPeter.Memishian@Sun.COM 		 * was truly sent 'm' ns. ago, then this response must
8170Sstevel@tonic-gate 		 * have been rejected by the sequence number checks. The
8180Sstevel@tonic-gate 		 * fact that it has passed the sequence number checks
8190Sstevel@tonic-gate 		 * means that the measured rtt is wrong. We were probably
8200Sstevel@tonic-gate 		 * scheduled long after the packet was received.
8210Sstevel@tonic-gate 		 */
8220Sstevel@tonic-gate 		goto out;
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * Don't update rtt until we see NUM_PROBE_REPAIRS probe responses
8270Sstevel@tonic-gate 	 * The initial few responses after the interface is repaired may
8280Sstevel@tonic-gate 	 * contain high rtt's because they could have been queued up waiting
8290Sstevel@tonic-gate 	 * for ARP/NDP resolution on a failed interface.
8300Sstevel@tonic-gate 	 */
8310Sstevel@tonic-gate 	if ((pii->pii_state != PI_RUNNING) || GROUP_FAILED(pg))
8320Sstevel@tonic-gate 		goto out;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	/*
8350Sstevel@tonic-gate 	 * Don't update the Conservative Round Trip Time estimate for this
8360Sstevel@tonic-gate 	 * (phint, target) pair if this is the not the highest ack seq seen
8370Sstevel@tonic-gate 	 * thus far on this target.
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	if (!highest_ack_tg(pr_icmp_seq, target))
8400Sstevel@tonic-gate 		goto out;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/*
8430Sstevel@tonic-gate 	 * Always update the rtt. This is a failure detection probe
8440Sstevel@tonic-gate 	 * and we want to measure both increase / decrease in rtt.
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	pi_set_crtt(target, m, _B_TRUE);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/*
8490Sstevel@tonic-gate 	 * If the crtt exceeds the average time between probes,
8500Sstevel@tonic-gate 	 * investigate if this slow target is an exception. If so we
8510Sstevel@tonic-gate 	 * can avoid this target and still meet the failure detection
8520Sstevel@tonic-gate 	 * time. Otherwise we can't meet the failure detection time.
8530Sstevel@tonic-gate 	 */
8540Sstevel@tonic-gate 	if (target->tg_crtt > pg->pg_probeint) {
8550Sstevel@tonic-gate 		exception = check_exception_target(pii, target);
8560Sstevel@tonic-gate 		if (exception) {
8570Sstevel@tonic-gate 			/*
8580Sstevel@tonic-gate 			 * This target is exceptionally slow. Don't use it
8590Sstevel@tonic-gate 			 * for future probes. check_exception_target() has
8600Sstevel@tonic-gate 			 * made sure that we have at least MIN_PROBE_TARGETS
8610Sstevel@tonic-gate 			 * other active targets
8620Sstevel@tonic-gate 			 */
8630Sstevel@tonic-gate 			if (pii->pii_targets_are_routers) {
8640Sstevel@tonic-gate 				/*
8650Sstevel@tonic-gate 				 * This is a slow router, mark it as slow
8660Sstevel@tonic-gate 				 * and don't use it for further probes. We
8670Sstevel@tonic-gate 				 * don't delete it, since it will be populated
8680Sstevel@tonic-gate 				 * again when we do a router scan. Hence we
8690Sstevel@tonic-gate 				 * need to maintain extra state (unlike the
8700Sstevel@tonic-gate 				 * host case below).  Mark it as TG_SLOW.
8710Sstevel@tonic-gate 				 */
8720Sstevel@tonic-gate 				if (target->tg_status == TG_ACTIVE)
8730Sstevel@tonic-gate 					pii->pii_ntargets--;
8740Sstevel@tonic-gate 				target->tg_status = TG_SLOW;
8750Sstevel@tonic-gate 				target->tg_latime = gethrtime();
8760Sstevel@tonic-gate 				target->tg_rtt_sa = -1;
8770Sstevel@tonic-gate 				target->tg_crtt = 0;
8780Sstevel@tonic-gate 				target->tg_rtt_sd = 0;
8790Sstevel@tonic-gate 				if (pii->pii_target_next == target) {
8800Sstevel@tonic-gate 					pii->pii_target_next =
8810Sstevel@tonic-gate 					    target_next(target);
8820Sstevel@tonic-gate 				}
8830Sstevel@tonic-gate 			} else {
8840Sstevel@tonic-gate 				/*
8850Sstevel@tonic-gate 				 * the slow target is not a router, we can
8860Sstevel@tonic-gate 				 * just delete it. Send an icmp multicast and
8870Sstevel@tonic-gate 				 * pick the fastest responder that is not
8880Sstevel@tonic-gate 				 * already an active target. target_delete()
8890Sstevel@tonic-gate 				 * adjusts pii->pii_target_next
8900Sstevel@tonic-gate 				 */
8910Sstevel@tonic-gate 				target_delete(target);
8928485SPeter.Memishian@Sun.COM 				probe(pii, PROBE_MULTI, cur_hrtime);
8930Sstevel@tonic-gate 			}
8940Sstevel@tonic-gate 		} else {
8950Sstevel@tonic-gate 			/*
8960Sstevel@tonic-gate 			 * We can't meet the failure detection time.
8970Sstevel@tonic-gate 			 * Log a message, and update the detection time to
8980Sstevel@tonic-gate 			 * whatever we can achieve.
8990Sstevel@tonic-gate 			 */
9000Sstevel@tonic-gate 			pg->pg_probeint = target->tg_crtt * NEXT_FDT_MULTIPLE;
9010Sstevel@tonic-gate 			pg->pg_fdt = pg->pg_probeint * (NUM_PROBE_FAILS + 2);
9020Sstevel@tonic-gate 			last_fdt_bumpup_time = gethrtime();
9030Sstevel@tonic-gate 			if (pg != phyint_anongroup) {
9040Sstevel@tonic-gate 				logerr("Cannot meet requested failure detection"
9050Sstevel@tonic-gate 				    " time of %d ms on (%s %s) new failure"
9060Sstevel@tonic-gate 				    " detection time for group \"%s\" is %d"
9070Sstevel@tonic-gate 				    " ms\n", user_failure_detection_time,
9080Sstevel@tonic-gate 				    AF_STR(pii->pii_af), pii->pii_name,
9090Sstevel@tonic-gate 				    pg->pg_name, pg->pg_fdt);
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 	} else if ((target->tg_crtt < (pg->pg_probeint / LOWER_FDT_TRIGGER)) &&
9130Sstevel@tonic-gate 	    (user_failure_detection_time < pg->pg_fdt) &&
9140Sstevel@tonic-gate 	    (last_fdt_bumpup_time + MIN_SETTLING_TIME < gethrtime())) {
9150Sstevel@tonic-gate 		/*
9160Sstevel@tonic-gate 		 * If the crtt has now dropped by a factor of LOWER_FDT_TRIGGER
9170Sstevel@tonic-gate 		 * investigate if we can improve the failure detection time to
9180Sstevel@tonic-gate 		 * meet whatever the user specified.
9190Sstevel@tonic-gate 		 */
9200Sstevel@tonic-gate 		if (check_pg_crtt_improved(pg)) {
9210Sstevel@tonic-gate 			pg->pg_fdt = MAX(pg->pg_fdt / NEXT_FDT_MULTIPLE,
9220Sstevel@tonic-gate 			    user_failure_detection_time);
9230Sstevel@tonic-gate 			pg->pg_probeint = pg->pg_fdt / (NUM_PROBE_FAILS + 2);
9240Sstevel@tonic-gate 			if (pg != phyint_anongroup) {
9250Sstevel@tonic-gate 				logerr("Improved failure detection time %d ms "
9260Sstevel@tonic-gate 				    "on (%s %s) for group \"%s\"\n", pg->pg_fdt,
9270Sstevel@tonic-gate 				    AF_STR(pii->pii_af), pii->pii_name,
9280Sstevel@tonic-gate 				    pg->pg_name);
9290Sstevel@tonic-gate 			}
9300Sstevel@tonic-gate 			if (user_failure_detection_time == pg->pg_fdt) {
9310Sstevel@tonic-gate 				/* Avoid any truncation or rounding errors */
9320Sstevel@tonic-gate 				pg->pg_probeint = user_probe_interval;
9330Sstevel@tonic-gate 				/*
9340Sstevel@tonic-gate 				 * No more rtt probes will be sent. The actual
9350Sstevel@tonic-gate 				 * fdt has dropped to the user specified value.
9360Sstevel@tonic-gate 				 * pii_fd_snxt_basetime and pii_snxt_basetime
9370Sstevel@tonic-gate 				 * will be in sync henceforth.
9380Sstevel@tonic-gate 				 */
9390Sstevel@tonic-gate 				reset_snxt_basetimes();
9400Sstevel@tonic-gate 			}
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate out:
9448485SPeter.Memishian@Sun.COM 	pr_statp = &pii->pii_probes[pr_ndx];
9458485SPeter.Memishian@Sun.COM 	pr_statp->pr_hrtime_ackproc = cur_hrtime;
9468485SPeter.Memishian@Sun.COM 	pr_statp->pr_hrtime_ackrecv = pr_statp->pr_hrtime_sent +
9478485SPeter.Memishian@Sun.COM 	    (tv2ns(recv_tvp) - tv2ns(&pr_statp->pr_tv_sent));
9488485SPeter.Memishian@Sun.COM 
9498485SPeter.Memishian@Sun.COM 	probe_chstate(pr_statp, pii, PR_ACKED);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/*
9520Sstevel@tonic-gate 	 * Update pii->pii_rack, i.e. the sequence number of the last received
9530Sstevel@tonic-gate 	 * probe response, based on the echo reply we have received now, if
9540Sstevel@tonic-gate 	 * either of the following conditions are satisfied.
9550Sstevel@tonic-gate 	 * a. pii_rack is outside the current receive window of
9560Sstevel@tonic-gate 	 *    [pii->pii_snxt - PROBE_STATS_COUNT, pii->pii_snxt).
9570Sstevel@tonic-gate 	 *    This means we have not received probe responses for a
9580Sstevel@tonic-gate 	 *    long time, and the sequence number has wrapped around.
9590Sstevel@tonic-gate 	 * b. pii_rack is within the current receive window and this echo
9600Sstevel@tonic-gate 	 *    reply corresponds to the highest sequence number we have seen
9610Sstevel@tonic-gate 	 *    so far.
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	if (SEQ_GE(pii->pii_rack, pii->pii_snxt) ||
9640Sstevel@tonic-gate 	    SEQ_LT(pii->pii_rack, pii->pii_snxt - PROBE_STATS_COUNT) ||
9650Sstevel@tonic-gate 	    SEQ_GT(pr_icmp_seq, pii->pii_rack)) {
9660Sstevel@tonic-gate 		pii->pii_rack = pr_icmp_seq;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * Returns true if seq is the highest unacknowledged seq for target tg
9720Sstevel@tonic-gate  * else returns false
9730Sstevel@tonic-gate  */
9740Sstevel@tonic-gate static boolean_t
9750Sstevel@tonic-gate highest_ack_tg(uint16_t seq, struct target *tg)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	struct phyint_instance *pii;
9780Sstevel@tonic-gate 	int	 pr_ndx;
9790Sstevel@tonic-gate 	uint16_t pr_seq;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	pii = tg->tg_phyint_inst;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9840Sstevel@tonic-gate 	 * Get the seq number of the most recent probe sent so far,
9850Sstevel@tonic-gate 	 * and also get the corresponding probe index in the probe stats
9860Sstevel@tonic-gate 	 * array.
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
9890Sstevel@tonic-gate 	pr_seq = pii->pii_snxt;
9900Sstevel@tonic-gate 	pr_seq--;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	/*
9930Sstevel@tonic-gate 	 * Start from the most recent probe and walk back, trying to find
9940Sstevel@tonic-gate 	 * an acked probe corresponding to target tg.
9950Sstevel@tonic-gate 	 */
9960Sstevel@tonic-gate 	for (; pr_ndx != pii->pii_probe_next;
9970Sstevel@tonic-gate 	    pr_ndx = PROBE_INDEX_PREV(pr_ndx), pr_seq--) {
9980Sstevel@tonic-gate 		if (pii->pii_probes[pr_ndx].pr_target == tg &&
9990Sstevel@tonic-gate 		    pii->pii_probes[pr_ndx].pr_status == PR_ACKED) {
10000Sstevel@tonic-gate 			if (SEQ_GT(pr_seq, seq))
10010Sstevel@tonic-gate 				return (_B_FALSE);
10020Sstevel@tonic-gate 		}
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	return (_B_TRUE);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate /*
10080Sstevel@tonic-gate  * Check whether the crtt for the group has improved by a factor of
10090Sstevel@tonic-gate  * LOWER_FDT_TRIGGER.  Small crtt improvements are ignored to avoid failure
10100Sstevel@tonic-gate  * detection time flapping in the face of small crtt changes.
10110Sstevel@tonic-gate  */
10120Sstevel@tonic-gate static boolean_t
10130Sstevel@tonic-gate check_pg_crtt_improved(struct phyint_group *pg)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate 	struct	phyint *pi;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	if (debug & D_PROBE)
10180Sstevel@tonic-gate 		logdebug("check_pg_crtt_improved()\n");
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * The crtt for the group is only improved if each phyint_instance
10220Sstevel@tonic-gate 	 * for both ipv4 and ipv6 is improved.
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
10250Sstevel@tonic-gate 		if (!check_pii_crtt_improved(pi->pi_v4) ||
10260Sstevel@tonic-gate 		    !check_pii_crtt_improved(pi->pi_v6))
10270Sstevel@tonic-gate 			return (_B_FALSE);
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	return (_B_TRUE);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate /*
10340Sstevel@tonic-gate  * Check whether the crtt has improved substantially on this phyint_instance.
10350Sstevel@tonic-gate  * Returns _B_TRUE if there's no crtt information available, because pii
10360Sstevel@tonic-gate  * is NULL or the phyint_instance is not capable of probing.
10370Sstevel@tonic-gate  */
10380Sstevel@tonic-gate boolean_t
10390Sstevel@tonic-gate check_pii_crtt_improved(struct phyint_instance *pii) {
10400Sstevel@tonic-gate 	struct 	target *tg;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	if (pii == NULL)
10430Sstevel@tonic-gate 		return (_B_TRUE);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	if (!PROBE_CAPABLE(pii) ||
10460Sstevel@tonic-gate 	    pii->pii_phyint->pi_state == PI_FAILED)
10470Sstevel@tonic-gate 		return (_B_TRUE);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
10500Sstevel@tonic-gate 		if (tg->tg_status != TG_ACTIVE)
10510Sstevel@tonic-gate 			continue;
10520Sstevel@tonic-gate 		if (tg->tg_crtt > (pii->pii_phyint->pi_group->pg_probeint /
10530Sstevel@tonic-gate 		    LOWER_FDT_TRIGGER)) {
10540Sstevel@tonic-gate 			return (_B_FALSE);
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	return (_B_TRUE);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate  * This target responds very slowly to probes. The target's crtt exceeds
10630Sstevel@tonic-gate  * the probe interval of its group. Compare against other targets
10640Sstevel@tonic-gate  * and determine if this target is an exception, if so return true, else false
10650Sstevel@tonic-gate  */
10660Sstevel@tonic-gate static boolean_t
10670Sstevel@tonic-gate check_exception_target(struct phyint_instance *pii, struct target *target)
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate 	struct	target *tg;
10700Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	if (debug & D_PROBE) {
10730Sstevel@tonic-gate 		logdebug("check_exception_target(%s %s target %s)\n",
10740Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
10750Sstevel@tonic-gate 		    pr_addr(pii->pii_af, target->tg_address,
10764929Srk129064 		    abuf, sizeof (abuf)));
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	/*
10800Sstevel@tonic-gate 	 * We should have at least MIN_PROBE_TARGETS + 1 good targets now,
10810Sstevel@tonic-gate 	 * to make a good judgement. Otherwise don't drop this target.
10820Sstevel@tonic-gate 	 */
10830Sstevel@tonic-gate 	if (pii->pii_ntargets <  MIN_PROBE_TARGETS + 1)
10840Sstevel@tonic-gate 		return (_B_FALSE);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/*
10870Sstevel@tonic-gate 	 * Determine whether only this particular target is slow.
10880Sstevel@tonic-gate 	 * We know that this target's crtt exceeds the group's probe interval.
10890Sstevel@tonic-gate 	 * If all other active targets have a
10900Sstevel@tonic-gate 	 * crtt < (this group's probe interval) / EXCEPTION_FACTOR,
10910Sstevel@tonic-gate 	 * then this target is considered slow.
10920Sstevel@tonic-gate 	 */
10930Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
10940Sstevel@tonic-gate 		if (tg != target && tg->tg_status == TG_ACTIVE) {
10950Sstevel@tonic-gate 			if (tg->tg_crtt >
10960Sstevel@tonic-gate 			    pii->pii_phyint->pi_group->pg_probeint /
10970Sstevel@tonic-gate 			    EXCEPTION_FACTOR) {
10980Sstevel@tonic-gate 				return (_B_FALSE);
10990Sstevel@tonic-gate 			}
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	return (_B_TRUE);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate  * Update the target list. The icmp all hosts multicast has given us
11080Sstevel@tonic-gate  * some host to which we can send probes. If we already have sufficient
11090Sstevel@tonic-gate  * targets, discard it.
11100Sstevel@tonic-gate  */
11110Sstevel@tonic-gate static void
11120Sstevel@tonic-gate incoming_mcast_reply(struct phyint_instance *pii, struct pr_icmp *reply,
11130Sstevel@tonic-gate     struct in6_addr fromaddr)
11140Sstevel@tonic-gate /* ARGSUSED */
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate 	int af;
11170Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
11180Sstevel@tonic-gate 	struct phyint *pi;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	if (debug & D_PROBE) {
11210Sstevel@tonic-gate 		logdebug("incoming_mcast_reply(%s %s %s)\n",
11220Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
11230Sstevel@tonic-gate 		    pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf)));
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	/*
11270Sstevel@tonic-gate 	 * Using host targets is a fallback mechanism. If we have
11280Sstevel@tonic-gate 	 * found a router, don't add this host target. If we already
11290Sstevel@tonic-gate 	 * know MAX_PROBE_TARGETS, don't add another target.
11300Sstevel@tonic-gate 	 */
11310Sstevel@tonic-gate 	assert(pii->pii_ntargets <= MAX_PROBE_TARGETS);
11320Sstevel@tonic-gate 	if (pii->pii_targets != NULL) {
11330Sstevel@tonic-gate 		if (pii->pii_targets_are_routers ||
11340Sstevel@tonic-gate 		    (pii->pii_ntargets == MAX_PROBE_TARGETS)) {
11350Sstevel@tonic-gate 			return;
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&fromaddr) ||
11400Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED_ANY(&fromaddr)) {
11410Sstevel@tonic-gate 		/*
11420Sstevel@tonic-gate 		 * Guard against response from 0.0.0.0
11430Sstevel@tonic-gate 		 * and ::. Log a trace message
11440Sstevel@tonic-gate 		 */
11450Sstevel@tonic-gate 		logtrace("probe response from %s on %s\n",
11460Sstevel@tonic-gate 		    pr_addr(pii->pii_af, fromaddr, abuf, sizeof (abuf)),
11470Sstevel@tonic-gate 		    pii->pii_name);
11480Sstevel@tonic-gate 		return;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/*
11520Sstevel@tonic-gate 	 * This address is one of our own, so reject this address as a
11530Sstevel@tonic-gate 	 * valid probe target.
11540Sstevel@tonic-gate 	 */
11550Sstevel@tonic-gate 	af = pii->pii_af;
11562250Srk129064 	if (own_address(fromaddr))
11570Sstevel@tonic-gate 		return;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	/*
11600Sstevel@tonic-gate 	 * If the phyint is part a named group, then add the address to all
11610Sstevel@tonic-gate 	 * members of the group.  Otherwise, add the address only to the
11620Sstevel@tonic-gate 	 * phyint itself, since other phyints in the anongroup may not be on
11630Sstevel@tonic-gate 	 * the same subnet.
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	pi = pii->pii_phyint;
11660Sstevel@tonic-gate 	if (pi->pi_group == phyint_anongroup) {
11670Sstevel@tonic-gate 		target_add(pii, fromaddr, _B_FALSE);
11680Sstevel@tonic-gate 	} else {
11690Sstevel@tonic-gate 		pi = pi->pi_group->pg_phyint;
11700Sstevel@tonic-gate 		for (; pi != NULL; pi = pi->pi_pgnext)
11710Sstevel@tonic-gate 			target_add(PHYINT_INSTANCE(pi, af), fromaddr, _B_FALSE);
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate  * Compute CRTT given an existing scaled average, scaled deviation estimate
11770Sstevel@tonic-gate  * and a new rtt time.  The formula is from Jacobson and Karels'
11780Sstevel@tonic-gate  * "Congestion Avoidance and Control" in SIGCOMM '88.  The variable names
11790Sstevel@tonic-gate  * are the same as those in Appendix A.2 of that paper.
11800Sstevel@tonic-gate  *
11810Sstevel@tonic-gate  * m = new measurement
11820Sstevel@tonic-gate  * sa = scaled RTT average (8 * average estimates)
11830Sstevel@tonic-gate  * sv = scaled mean deviation (mdev) of RTT (4 * deviation estimates).
11840Sstevel@tonic-gate  * crtt = Conservative round trip time. Used to determine whether probe
11850Sstevel@tonic-gate  * has timed out.
11860Sstevel@tonic-gate  *
11870Sstevel@tonic-gate  * New scaled average and deviation are passed back via sap and svp
11880Sstevel@tonic-gate  */
11898485SPeter.Memishian@Sun.COM static int64_t
11908485SPeter.Memishian@Sun.COM compute_crtt(int64_t *sap, int64_t *svp, int64_t m)
11910Sstevel@tonic-gate {
11928485SPeter.Memishian@Sun.COM 	int64_t sa = *sap;
11938485SPeter.Memishian@Sun.COM 	int64_t sv = *svp;
11948485SPeter.Memishian@Sun.COM 	int64_t crtt;
11958485SPeter.Memishian@Sun.COM 	int64_t saved_m = m;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	assert(*sap >= -1);
11980Sstevel@tonic-gate 	assert(*svp >= 0);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (sa != -1) {
12010Sstevel@tonic-gate 		/*
12020Sstevel@tonic-gate 		 * Update average estimator:
12030Sstevel@tonic-gate 		 *	new rtt = old rtt + 1/8 Error
12040Sstevel@tonic-gate 		 *	    where Error = m - old rtt
12050Sstevel@tonic-gate 		 *	i.e. 8 * new rtt = 8 * old rtt + Error
12060Sstevel@tonic-gate 		 *	i.e. new sa =  old sa + Error
12070Sstevel@tonic-gate 		 */
12080Sstevel@tonic-gate 		m -= sa >> 3;		/* m is now Error in estimate. */
12090Sstevel@tonic-gate 		if ((sa += m) < 0) {
12100Sstevel@tonic-gate 			/* Don't allow the smoothed average to be negative. */
12110Sstevel@tonic-gate 			sa = 0;
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		/*
12150Sstevel@tonic-gate 		 * Update deviation estimator:
12160Sstevel@tonic-gate 		 *	new mdev =  old mdev + 1/4 (abs(Error) - old mdev)
12170Sstevel@tonic-gate 		 *	i.e. 4 * new mdev = 4 * old mdev +
12180Sstevel@tonic-gate 		 *		(abs(Error) - old mdev)
12190Sstevel@tonic-gate 		 * 	i.e. new sv = old sv + (abs(Error) - old mdev)
12200Sstevel@tonic-gate 		 */
12210Sstevel@tonic-gate 		if (m < 0)
12220Sstevel@tonic-gate 			m = -m;
12230Sstevel@tonic-gate 		m -= sv >> 2;
12240Sstevel@tonic-gate 		sv += m;
12250Sstevel@tonic-gate 	} else {
12260Sstevel@tonic-gate 		/* Initialization. This is the first response received. */
12270Sstevel@tonic-gate 		sa = (m << 3);
12280Sstevel@tonic-gate 		sv = (m << 1);
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	crtt = (sa >> 3) + sv;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (debug & D_PROBE) {
12348485SPeter.Memishian@Sun.COM 		logerr("compute_crtt: m = %lld sa = %lld, sv = %lld -> "
12358485SPeter.Memishian@Sun.COM 		    "crtt = %lld\n", saved_m, sa, sv, crtt);
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	*sap = sa;
12390Sstevel@tonic-gate 	*svp = sv;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	/*
12420Sstevel@tonic-gate 	 * CRTT = average estimates  + 4 * deviation estimates
12430Sstevel@tonic-gate 	 *	= sa / 8 + sv
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	return (crtt);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate static void
12498485SPeter.Memishian@Sun.COM pi_set_crtt(struct target *tg, int64_t m, boolean_t is_probe_uni)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	struct phyint_instance *pii = tg->tg_phyint_inst;
12520Sstevel@tonic-gate 	int probe_interval = pii->pii_phyint->pi_group->pg_probeint;
12538485SPeter.Memishian@Sun.COM 	int64_t sa = tg->tg_rtt_sa;
12548485SPeter.Memishian@Sun.COM 	int64_t sv = tg->tg_rtt_sd;
12550Sstevel@tonic-gate 	int new_crtt;
12560Sstevel@tonic-gate 	int i;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (debug & D_PROBE)
12598485SPeter.Memishian@Sun.COM 		logdebug("pi_set_crtt: target -  m %lld\n", m);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	/* store the round trip time, in case we need to defer computation */
12620Sstevel@tonic-gate 	tg->tg_deferred[tg->tg_num_deferred] = m;
12630Sstevel@tonic-gate 
12648485SPeter.Memishian@Sun.COM 	new_crtt = ns2ms(compute_crtt(&sa, &sv, m));
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	/*
12670Sstevel@tonic-gate 	 * If this probe's round trip time would singlehandedly cause an
12680Sstevel@tonic-gate 	 * increase in the group's probe interval consider it suspect.
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 	if ((new_crtt > probe_interval) && is_probe_uni) {
12710Sstevel@tonic-gate 		if (debug & D_PROBE) {
12720Sstevel@tonic-gate 			logdebug("Received a suspect probe on %s, new_crtt ="
12730Sstevel@tonic-gate 			    " %d, probe_interval = %d, num_deferred = %d\n",
12740Sstevel@tonic-gate 			    pii->pii_probe_logint->li_name, new_crtt,
12750Sstevel@tonic-gate 			    probe_interval, tg->tg_num_deferred);
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		/*
12790Sstevel@tonic-gate 		 * If we've deferred as many rtts as we plan on deferring, then
12800Sstevel@tonic-gate 		 * assume the link really did slow down and process all queued
12810Sstevel@tonic-gate 		 * rtts
12820Sstevel@tonic-gate 		 */
12830Sstevel@tonic-gate 		if (tg->tg_num_deferred == MAXDEFERREDRTT) {
12840Sstevel@tonic-gate 			if (debug & D_PROBE) {
12850Sstevel@tonic-gate 				logdebug("Received MAXDEFERREDRTT probes which "
12860Sstevel@tonic-gate 				    "would cause an increased probe_interval.  "
12870Sstevel@tonic-gate 				    "Integrating queued rtt data points.\n");
12880Sstevel@tonic-gate 			}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			for (i = 0; i <= tg->tg_num_deferred; i++) {
12918485SPeter.Memishian@Sun.COM 				tg->tg_crtt = ns2ms(compute_crtt(&tg->tg_rtt_sa,
12928485SPeter.Memishian@Sun.COM 				    &tg->tg_rtt_sd, tg->tg_deferred[i]));
12930Sstevel@tonic-gate 			}
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 			tg->tg_num_deferred = 0;
12960Sstevel@tonic-gate 		} else {
12970Sstevel@tonic-gate 			tg->tg_num_deferred++;
12980Sstevel@tonic-gate 		}
12990Sstevel@tonic-gate 		return;
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
13030Sstevel@tonic-gate 	 * If this is a normal probe, or an RTT probe that would lead to a
13040Sstevel@tonic-gate 	 * reduced CRTT, then update our CRTT data.  Further, if this was
13050Sstevel@tonic-gate 	 * a normal probe, pitch any deferred probes since our probes are
13060Sstevel@tonic-gate 	 * again being answered within our CRTT estimates.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	if (is_probe_uni || new_crtt < tg->tg_crtt) {
13090Sstevel@tonic-gate 		tg->tg_rtt_sa = sa;
13100Sstevel@tonic-gate 		tg->tg_rtt_sd = sv;
13110Sstevel@tonic-gate 		tg->tg_crtt = new_crtt;
13120Sstevel@tonic-gate 		if (is_probe_uni)
13130Sstevel@tonic-gate 			tg->tg_num_deferred = 0;
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate /*
13180Sstevel@tonic-gate  * Return a pointer to the specified option buffer.
13190Sstevel@tonic-gate  * If not found return NULL.
13200Sstevel@tonic-gate  */
13210Sstevel@tonic-gate static void *
13228485SPeter.Memishian@Sun.COM find_ancillary(struct msghdr *msg, int cmsg_level, int cmsg_type)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 	struct cmsghdr *cmsg;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
13270Sstevel@tonic-gate 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
13288485SPeter.Memishian@Sun.COM 		if (cmsg->cmsg_level == cmsg_level &&
13290Sstevel@tonic-gate 		    cmsg->cmsg_type == cmsg_type) {
13300Sstevel@tonic-gate 			return (CMSG_DATA(cmsg));
13310Sstevel@tonic-gate 		}
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 	return (NULL);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate /*
13378485SPeter.Memishian@Sun.COM  * Try to activate another INACTIVE interface in the same group as `pi'.
13388485SPeter.Memishian@Sun.COM  * Prefer STANDBY INACTIVE to just INACTIVE.
13398485SPeter.Memishian@Sun.COM  */
13408485SPeter.Memishian@Sun.COM void
13418485SPeter.Memishian@Sun.COM phyint_activate_another(struct phyint *pi)
13428485SPeter.Memishian@Sun.COM {
13438485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
13448485SPeter.Memishian@Sun.COM 	struct phyint *inactivepi = NULL;
13458485SPeter.Memishian@Sun.COM 
13468485SPeter.Memishian@Sun.COM 	if (pi->pi_group == phyint_anongroup)
13478485SPeter.Memishian@Sun.COM 		return;
13488485SPeter.Memishian@Sun.COM 
13498485SPeter.Memishian@Sun.COM 	for (pi2 = pi->pi_group->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
13508485SPeter.Memishian@Sun.COM 		if (pi == pi2 || pi2->pi_state != PI_RUNNING ||
13518485SPeter.Memishian@Sun.COM 		    !(pi2->pi_flags & IFF_INACTIVE))
13528485SPeter.Memishian@Sun.COM 			continue;
13538485SPeter.Memishian@Sun.COM 
13548485SPeter.Memishian@Sun.COM 		inactivepi = pi2;
13558485SPeter.Memishian@Sun.COM 		if (pi2->pi_flags & IFF_STANDBY)
13568485SPeter.Memishian@Sun.COM 			break;
13578485SPeter.Memishian@Sun.COM 	}
13588485SPeter.Memishian@Sun.COM 
13598485SPeter.Memishian@Sun.COM 	if (inactivepi != NULL)
13608485SPeter.Memishian@Sun.COM 		(void) change_pif_flags(inactivepi, 0, IFF_INACTIVE);
13618485SPeter.Memishian@Sun.COM }
13628485SPeter.Memishian@Sun.COM 
13638485SPeter.Memishian@Sun.COM /*
1364*10290SPeter.Memishian@Sun.COM  * Transition a phyint to PI_RUNNING.  The caller must ensure that the
1365*10290SPeter.Memishian@Sun.COM  * transition is appropriate.  Clears IFF_OFFLINE or IFF_FAILED if
1366*10290SPeter.Memishian@Sun.COM  * appropriate.  Also sets IFF_INACTIVE on this or other interfaces as
1367*10290SPeter.Memishian@Sun.COM  * appropriate (see comment below).  Finally, also updates the phyint's group
1368*10290SPeter.Memishian@Sun.COM  * state to account for the change.
13698485SPeter.Memishian@Sun.COM  */
13708485SPeter.Memishian@Sun.COM void
13718485SPeter.Memishian@Sun.COM phyint_transition_to_running(struct phyint *pi)
13728485SPeter.Memishian@Sun.COM {
13738485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
13748485SPeter.Memishian@Sun.COM 	struct phyint *actstandbypi = NULL;
13758485SPeter.Memishian@Sun.COM 	uint_t nactive = 0, nnonstandby = 0;
13768485SPeter.Memishian@Sun.COM 	boolean_t onlining = (pi->pi_state == PI_OFFLINE);
1377*10290SPeter.Memishian@Sun.COM 	boolean_t initial = (pi->pi_state == PI_INIT);
13788485SPeter.Memishian@Sun.COM 	uint64_t set, clear;
13798485SPeter.Memishian@Sun.COM 
13808485SPeter.Memishian@Sun.COM 	/*
13818485SPeter.Memishian@Sun.COM 	 * The interface is running again, but should it or another interface
13828485SPeter.Memishian@Sun.COM 	 * in the group end up INACTIVE?  There are three cases:
13838485SPeter.Memishian@Sun.COM 	 *
13848485SPeter.Memishian@Sun.COM 	 * 1. If it's a STANDBY interface, it should be end up INACTIVE if
13858485SPeter.Memishian@Sun.COM 	 *    the group is operating at capacity (i.e., there are at least as
13868485SPeter.Memishian@Sun.COM 	 *    many active interfaces as non-STANDBY interfaces in the group).
13878485SPeter.Memishian@Sun.COM 	 *    No other interfaces should be changed.
13888485SPeter.Memishian@Sun.COM 	 *
13898485SPeter.Memishian@Sun.COM 	 * 2. If it's a non-STANDBY interface and we're onlining it or
13908485SPeter.Memishian@Sun.COM 	 *    FAILBACK is enabled, then it should *not* end up INACTIVE.
13918485SPeter.Memishian@Sun.COM 	 *    Further, if the group is above capacity as a result of this
13928485SPeter.Memishian@Sun.COM 	 *    interface, then an active STANDBY interface in the group should
13938485SPeter.Memishian@Sun.COM 	 *    end up INACTIVE.
13948485SPeter.Memishian@Sun.COM 	 *
13958485SPeter.Memishian@Sun.COM 	 * 3. If it's a non-STANDBY interface, we're repairing it, and
13968485SPeter.Memishian@Sun.COM 	 *    FAILBACK is disabled, then it should end up INACTIVE *unless*
13978485SPeter.Memishian@Sun.COM 	 *    the group was failed (in which case we have no choice but to
13988485SPeter.Memishian@Sun.COM 	 *    use it).  No other interfaces should be changed.
13998485SPeter.Memishian@Sun.COM 	 */
14008485SPeter.Memishian@Sun.COM 	if (pi->pi_group != phyint_anongroup) {
14018485SPeter.Memishian@Sun.COM 		pi2 = pi->pi_group->pg_phyint;
14028485SPeter.Memishian@Sun.COM 		for (; pi2 != NULL; pi2 = pi2->pi_pgnext) {
14038485SPeter.Memishian@Sun.COM 			if (!(pi2->pi_flags & IFF_STANDBY))
14048485SPeter.Memishian@Sun.COM 				nnonstandby++;
14058485SPeter.Memishian@Sun.COM 
14068485SPeter.Memishian@Sun.COM 			if (pi2->pi_state == PI_RUNNING) {
14078485SPeter.Memishian@Sun.COM 				if (!(pi2->pi_flags & IFF_INACTIVE)) {
14088485SPeter.Memishian@Sun.COM 					nactive++;
14098485SPeter.Memishian@Sun.COM 					if (pi2->pi_flags & IFF_STANDBY)
14108485SPeter.Memishian@Sun.COM 						actstandbypi = pi2;
14118485SPeter.Memishian@Sun.COM 				}
14128485SPeter.Memishian@Sun.COM 			}
14138485SPeter.Memishian@Sun.COM 		}
14148485SPeter.Memishian@Sun.COM 	}
14158485SPeter.Memishian@Sun.COM 
14168485SPeter.Memishian@Sun.COM 	set = 0;
14178485SPeter.Memishian@Sun.COM 	clear = (onlining ? IFF_OFFLINE : IFF_FAILED);
14188485SPeter.Memishian@Sun.COM 
14198485SPeter.Memishian@Sun.COM 	if (pi->pi_flags & IFF_STANDBY) {			/* case 1 */
14208485SPeter.Memishian@Sun.COM 		if (nactive >= nnonstandby)
14218485SPeter.Memishian@Sun.COM 			set |= IFF_INACTIVE;
14228485SPeter.Memishian@Sun.COM 		else
14238485SPeter.Memishian@Sun.COM 			clear |= IFF_INACTIVE;
14248485SPeter.Memishian@Sun.COM 	} else if (onlining || failback_enabled) {		/* case 2 */
14258485SPeter.Memishian@Sun.COM 		if (nactive >= nnonstandby && actstandbypi != NULL)
14268485SPeter.Memishian@Sun.COM 			(void) change_pif_flags(actstandbypi, IFF_INACTIVE, 0);
1427*10290SPeter.Memishian@Sun.COM 	} else if (!initial && !GROUP_FAILED(pi->pi_group)) {	/* case 3 */
14288485SPeter.Memishian@Sun.COM 		set |= IFF_INACTIVE;
14298485SPeter.Memishian@Sun.COM 	}
14308485SPeter.Memishian@Sun.COM 	(void) change_pif_flags(pi, set, clear);
14318485SPeter.Memishian@Sun.COM 
14328485SPeter.Memishian@Sun.COM 	phyint_chstate(pi, PI_RUNNING);
14338485SPeter.Memishian@Sun.COM 
14348485SPeter.Memishian@Sun.COM 	/*
14358485SPeter.Memishian@Sun.COM 	 * Update the group state to account for the change.
14368485SPeter.Memishian@Sun.COM 	 */
14378485SPeter.Memishian@Sun.COM 	phyint_group_refresh_state(pi->pi_group);
14388485SPeter.Memishian@Sun.COM }
14398485SPeter.Memishian@Sun.COM 
14408485SPeter.Memishian@Sun.COM /*
14410Sstevel@tonic-gate  * See if a previously failed interface has started working again.
14420Sstevel@tonic-gate  */
14430Sstevel@tonic-gate void
14440Sstevel@tonic-gate phyint_check_for_repair(struct phyint *pi)
14450Sstevel@tonic-gate {
14468485SPeter.Memishian@Sun.COM 	if (!phyint_repaired(pi))
14478485SPeter.Memishian@Sun.COM 		return;
14480Sstevel@tonic-gate 
14498485SPeter.Memishian@Sun.COM 	if (pi->pi_group == phyint_anongroup) {
14508485SPeter.Memishian@Sun.COM 		logerr("IP interface repair detected on %s\n", pi->pi_name);
14518485SPeter.Memishian@Sun.COM 	} else {
14528485SPeter.Memishian@Sun.COM 		logerr("IP interface repair detected on %s of group %s\n",
14538485SPeter.Memishian@Sun.COM 		    pi->pi_name, pi->pi_group->pg_name);
14548485SPeter.Memishian@Sun.COM 	}
14550Sstevel@tonic-gate 
14568485SPeter.Memishian@Sun.COM 	/*
14578485SPeter.Memishian@Sun.COM 	 * If the interface is PI_OFFLINE, it can't be made PI_RUNNING yet.
14588485SPeter.Memishian@Sun.COM 	 * So just clear IFF_OFFLINE and defer phyint_transition_to_running()
14598485SPeter.Memishian@Sun.COM 	 * until it is brought back online.
14608485SPeter.Memishian@Sun.COM 	 */
14618485SPeter.Memishian@Sun.COM 	if (pi->pi_state == PI_OFFLINE) {
14628485SPeter.Memishian@Sun.COM 		(void) change_pif_flags(pi, 0, IFF_FAILED);
14638485SPeter.Memishian@Sun.COM 		return;
14648485SPeter.Memishian@Sun.COM 	}
14650Sstevel@tonic-gate 
14668485SPeter.Memishian@Sun.COM 	phyint_transition_to_running(pi);	/* calls phyint_chstate() */
14670Sstevel@tonic-gate }
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate /*
14708485SPeter.Memishian@Sun.COM  * See if an interface has failed, or if the whole group of interfaces has
14718485SPeter.Memishian@Sun.COM  * failed.
14720Sstevel@tonic-gate  */
14730Sstevel@tonic-gate static void
14740Sstevel@tonic-gate phyint_inst_check_for_failure(struct phyint_instance *pii)
14750Sstevel@tonic-gate {
14768485SPeter.Memishian@Sun.COM 	struct phyint	*pi = pii->pii_phyint;
14778485SPeter.Memishian@Sun.COM 	struct phyint	*pi2;
14788485SPeter.Memishian@Sun.COM 	boolean_t	was_active;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	switch (failure_state(pii)) {
14810Sstevel@tonic-gate 	case PHYINT_FAILURE:
14828485SPeter.Memishian@Sun.COM 		was_active = ((pi->pi_flags & IFF_INACTIVE) == 0);
14838485SPeter.Memishian@Sun.COM 
14848485SPeter.Memishian@Sun.COM 		(void) change_pif_flags(pi, IFF_FAILED, IFF_INACTIVE);
14850Sstevel@tonic-gate 		if (pi->pi_group == phyint_anongroup) {
14868485SPeter.Memishian@Sun.COM 			logerr("IP interface failure detected on %s\n",
14878485SPeter.Memishian@Sun.COM 			    pii->pii_name);
14880Sstevel@tonic-gate 		} else {
14898485SPeter.Memishian@Sun.COM 			logerr("IP interface failure detected on %s of group"
14908485SPeter.Memishian@Sun.COM 			    " %s\n", pii->pii_name, pi->pi_group->pg_name);
14910Sstevel@tonic-gate 		}
14928485SPeter.Memishian@Sun.COM 
14930Sstevel@tonic-gate 		/*
14948700SPeter.Memishian@Sun.COM 		 * If the failed interface was active, activate another
14958700SPeter.Memishian@Sun.COM 		 * INACTIVE interface in the group if possible.
14968700SPeter.Memishian@Sun.COM 		 */
14978700SPeter.Memishian@Sun.COM 		if (was_active)
14988700SPeter.Memishian@Sun.COM 			phyint_activate_another(pi);
14998700SPeter.Memishian@Sun.COM 
15008700SPeter.Memishian@Sun.COM 		/*
15018485SPeter.Memishian@Sun.COM 		 * If the interface is offline, the state change will be
15028485SPeter.Memishian@Sun.COM 		 * noted when it comes back online.
15030Sstevel@tonic-gate 		 */
15040Sstevel@tonic-gate 		if (pi->pi_state != PI_OFFLINE) {
15050Sstevel@tonic-gate 			phyint_chstate(pi, PI_FAILED);
15060Sstevel@tonic-gate 			reset_crtt_all(pi);
15070Sstevel@tonic-gate 		}
15080Sstevel@tonic-gate 		break;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	case GROUP_FAILURE:
15118485SPeter.Memishian@Sun.COM 		pi2 = pi->pi_group->pg_phyint;
15128485SPeter.Memishian@Sun.COM 		for (; pi2 != NULL; pi2 = pi2->pi_pgnext) {
15138485SPeter.Memishian@Sun.COM 			(void) change_pif_flags(pi2, IFF_FAILED, IFF_INACTIVE);
15148485SPeter.Memishian@Sun.COM 			if (pi2->pi_state == PI_OFFLINE) /* see comment above */
15150Sstevel@tonic-gate 				continue;
15168485SPeter.Memishian@Sun.COM 
15170Sstevel@tonic-gate 			reset_crtt_all(pi2);
15180Sstevel@tonic-gate 			/*
15198485SPeter.Memishian@Sun.COM 			 * In the case of host targets, we would have flushed
15208485SPeter.Memishian@Sun.COM 			 * the targets, and gone to PI_NOTARGETS state.
15210Sstevel@tonic-gate 			 */
15220Sstevel@tonic-gate 			if (pi2->pi_state == PI_RUNNING)
1523704Sethindra 				phyint_chstate(pi2, PI_FAILED);
15240Sstevel@tonic-gate 		}
15250Sstevel@tonic-gate 		break;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	default:
15280Sstevel@tonic-gate 		break;
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate  * Determines if any timeout event has occurred and returns the number of
15340Sstevel@tonic-gate  * milliseconds until the next timeout event for the phyint. Returns
15350Sstevel@tonic-gate  * TIMER_INFINITY for "never".
15360Sstevel@tonic-gate  */
15370Sstevel@tonic-gate uint_t
15380Sstevel@tonic-gate phyint_inst_timer(struct phyint_instance *pii)
15390Sstevel@tonic-gate {
15400Sstevel@tonic-gate 	int 	pr_ndx;
15410Sstevel@tonic-gate 	uint_t	timeout;
15420Sstevel@tonic-gate 	struct	target	*cur_tg;
15430Sstevel@tonic-gate 	struct	probe_stats *pr_statp;
15440Sstevel@tonic-gate 	struct	phyint_instance *pii_other;
15450Sstevel@tonic-gate 	struct	phyint *pi;
15460Sstevel@tonic-gate 	int	valid_unack_count;
15470Sstevel@tonic-gate 	int	i;
15480Sstevel@tonic-gate 	int	interval;
15490Sstevel@tonic-gate 	uint_t	check_time;
15500Sstevel@tonic-gate 	uint_t	cur_time;
15510Sstevel@tonic-gate 	hrtime_t cur_hrtime;
15520Sstevel@tonic-gate 	int	probe_interval = pii->pii_phyint->pi_group->pg_probeint;
15530Sstevel@tonic-gate 
15548485SPeter.Memishian@Sun.COM 	cur_hrtime = gethrtime();
15558485SPeter.Memishian@Sun.COM 	cur_time = ns2ms(cur_hrtime);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (debug & D_TIMER) {
15580Sstevel@tonic-gate 		logdebug("phyint_inst_timer(%s %s)\n",
15590Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	pii_other = phyint_inst_other(pii);
15630Sstevel@tonic-gate 	if (!PROBE_ENABLED(pii) && !PROBE_ENABLED(pii_other)) {
15640Sstevel@tonic-gate 		/*
15650Sstevel@tonic-gate 		 * Check to see if we're here due to link up/down flapping; If
15660Sstevel@tonic-gate 		 * enough time has passed, then try to bring the interface
15670Sstevel@tonic-gate 		 * back up; otherwise, schedule a timer to bring it back up
15680Sstevel@tonic-gate 		 * when enough time *has* elapsed.
15690Sstevel@tonic-gate 		 */
15700Sstevel@tonic-gate 		pi = pii->pii_phyint;
15710Sstevel@tonic-gate 		if (pi->pi_state == PI_FAILED && LINK_UP(pi)) {
15720Sstevel@tonic-gate 			check_time = pi->pi_whenup[pi->pi_whendx] + MSEC_PERMIN;
15730Sstevel@tonic-gate 			if (check_time > cur_time)
15740Sstevel@tonic-gate 				return (check_time - cur_time);
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 			phyint_check_for_repair(pi);
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/*
15812496Smeem 	 * If probing is not enabled on this phyint instance, don't proceed.
15820Sstevel@tonic-gate 	 */
15832496Smeem 	if (!PROBE_ENABLED(pii))
15840Sstevel@tonic-gate 		return (TIMER_INFINITY);
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * If the timer has fired too soon, probably triggered
15880Sstevel@tonic-gate 	 * by some other phyint instance, return the remaining
15890Sstevel@tonic-gate 	 * time
15900Sstevel@tonic-gate 	 */
15910Sstevel@tonic-gate 	if (TIME_LT(cur_time, pii->pii_snxt_time))
15920Sstevel@tonic-gate 		return (pii->pii_snxt_time - cur_time);
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	/*
15950Sstevel@tonic-gate 	 * If the link is down, don't send any probes for now.
15960Sstevel@tonic-gate 	 */
15970Sstevel@tonic-gate 	if (LINK_DOWN(pii->pii_phyint))
15980Sstevel@tonic-gate 		return (TIMER_INFINITY);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	/*
16010Sstevel@tonic-gate 	 * Randomize the next probe time, between MIN_RANDOM_FACTOR
16020Sstevel@tonic-gate 	 * and MAX_RANDOM_FACTOR with respect to the base probe time.
16030Sstevel@tonic-gate 	 * Base probe time is strictly periodic.
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	interval = GET_RANDOM(
16060Sstevel@tonic-gate 	    (int)(MIN_RANDOM_FACTOR * user_probe_interval),
16070Sstevel@tonic-gate 	    (int)(MAX_RANDOM_FACTOR * user_probe_interval));
16080Sstevel@tonic-gate 	pii->pii_snxt_time = pii->pii_snxt_basetime + interval;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	/*
16110Sstevel@tonic-gate 	 * Check if the current time > next time to probe. If so, we missed
16120Sstevel@tonic-gate 	 * sending 1 or more probes, probably due to heavy system load. At least
16130Sstevel@tonic-gate 	 * 'MIN_RANDOM_FACTOR * user_probe_interval' ms has elapsed since we
16140Sstevel@tonic-gate 	 * were scheduled. Make adjustments to the times, in multiples of
16150Sstevel@tonic-gate 	 * user_probe_interval.
16160Sstevel@tonic-gate 	 */
16170Sstevel@tonic-gate 	if (TIME_GT(cur_time, pii->pii_snxt_time)) {
16180Sstevel@tonic-gate 		int n;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 		n = (cur_time - pii->pii_snxt_time) / user_probe_interval;
16210Sstevel@tonic-gate 		pii->pii_snxt_time 	+= (n + 1) * user_probe_interval;
16220Sstevel@tonic-gate 		pii->pii_snxt_basetime 	+= (n + 1) * user_probe_interval;
16230Sstevel@tonic-gate 		logtrace("missed sending %d probes cur_time %u snxt_time %u"
16240Sstevel@tonic-gate 		    " snxt_basetime %u\n", n + 1, cur_time, pii->pii_snxt_time,
16250Sstevel@tonic-gate 		    pii->pii_snxt_basetime);
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 		/* Collect statistics about missed probes */
16280Sstevel@tonic-gate 		probes_missed.pm_nprobes += n + 1;
16290Sstevel@tonic-gate 		probes_missed.pm_ntimes++;
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 	pii->pii_snxt_basetime += user_probe_interval;
16320Sstevel@tonic-gate 	interval = pii->pii_snxt_time - cur_time;
16330Sstevel@tonic-gate 	if (debug & D_TARGET) {
16340Sstevel@tonic-gate 		logdebug("cur_time %u snxt_time %u snxt_basetime %u"
16350Sstevel@tonic-gate 		    " interval %u\n", cur_time, pii->pii_snxt_time,
16360Sstevel@tonic-gate 		    pii->pii_snxt_basetime, interval);
16370Sstevel@tonic-gate 	}
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	/*
16400Sstevel@tonic-gate 	 * If no targets are known, we need to send an ICMP multicast. The
16410Sstevel@tonic-gate 	 * probe type is PROBE_MULTI.  We'll check back in 'interval' msec
16420Sstevel@tonic-gate 	 * to see if we found a target.
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	if (pii->pii_target_next == NULL) {
16450Sstevel@tonic-gate 		assert(pii->pii_ntargets == 0);
16460Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
16470Sstevel@tonic-gate 		probe(pii, PROBE_MULTI, cur_time);
16480Sstevel@tonic-gate 		return (interval);
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	if ((user_probe_interval != probe_interval) &&
16520Sstevel@tonic-gate 	    TIME_LT(pii->pii_snxt_time, pii->pii_fd_snxt_basetime)) {
16530Sstevel@tonic-gate 		/*
16540Sstevel@tonic-gate 		 * the failure detection (fd) probe timer has not yet fired.
16550Sstevel@tonic-gate 		 * Need to send only an rtt probe. The probe type is PROBE_RTT.
16560Sstevel@tonic-gate 		 */
16578485SPeter.Memishian@Sun.COM 		probe(pii, PROBE_RTT, cur_hrtime);
16580Sstevel@tonic-gate 		return (interval);
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 	/*
16610Sstevel@tonic-gate 	 * the fd probe timer has fired. Need to do all failure
16620Sstevel@tonic-gate 	 * detection / recovery calculations, and then send an fd probe
16630Sstevel@tonic-gate 	 * of type PROBE_UNI.
16640Sstevel@tonic-gate 	 */
16650Sstevel@tonic-gate 	if (user_probe_interval == probe_interval) {
16660Sstevel@tonic-gate 		/*
16670Sstevel@tonic-gate 		 * We could have missed some probes, and then adjusted
16680Sstevel@tonic-gate 		 * pii_snxt_basetime above. Otherwise we could have
16690Sstevel@tonic-gate 		 * blindly added probe_interval to pii_fd_snxt_basetime.
16700Sstevel@tonic-gate 		 */
16710Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
16720Sstevel@tonic-gate 	} else {
16730Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime += probe_interval;
16740Sstevel@tonic-gate 		if (TIME_GT(cur_time, pii->pii_fd_snxt_basetime)) {
16750Sstevel@tonic-gate 			int n;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 			n = (cur_time - pii->pii_fd_snxt_basetime) /
16780Sstevel@tonic-gate 			    probe_interval;
16790Sstevel@tonic-gate 			pii->pii_fd_snxt_basetime += (n + 1) * probe_interval;
16800Sstevel@tonic-gate 		}
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/*
16840Sstevel@tonic-gate 	 * We can have at most, the latest 2 probes that we sent, in
16850Sstevel@tonic-gate 	 * the PR_UNACKED state. All previous probes sent, are either
16860Sstevel@tonic-gate 	 * PR_LOST or PR_ACKED. An unacknowledged probe is considered
16878485SPeter.Memishian@Sun.COM 	 * timed out if the probe's time_start + the CRTT < currenttime.
16880Sstevel@tonic-gate 	 * For each of the last 2 probes, examine whether it has timed
16890Sstevel@tonic-gate 	 * out. If so, mark it PR_LOST. The probe stats is a circular array.
16900Sstevel@tonic-gate 	 */
16910Sstevel@tonic-gate 	pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
16920Sstevel@tonic-gate 	valid_unack_count = 0;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
16950Sstevel@tonic-gate 		pr_statp = &pii->pii_probes[pr_ndx];
16960Sstevel@tonic-gate 		cur_tg = pii->pii_probes[pr_ndx].pr_target;
16970Sstevel@tonic-gate 		switch (pr_statp->pr_status) {
16980Sstevel@tonic-gate 		case PR_ACKED:
16990Sstevel@tonic-gate 			/*
17000Sstevel@tonic-gate 			 * We received back an ACK, so the switch clearly
17010Sstevel@tonic-gate 			 * is not dropping our traffic, and thus we can
17020Sstevel@tonic-gate 			 * enable failure detection immediately.
17030Sstevel@tonic-gate 			 */
17040Sstevel@tonic-gate 			if (pii->pii_fd_hrtime > gethrtime()) {
17050Sstevel@tonic-gate 				if (debug & D_PROBE) {
17060Sstevel@tonic-gate 					logdebug("successful probe on %s; "
17070Sstevel@tonic-gate 					    "ending quiet period\n",
17080Sstevel@tonic-gate 					    pii->pii_phyint->pi_name);
17090Sstevel@tonic-gate 				}
17100Sstevel@tonic-gate 				pii->pii_fd_hrtime = gethrtime();
17110Sstevel@tonic-gate 			}
17120Sstevel@tonic-gate 			break;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 		case PR_UNACKED:
17150Sstevel@tonic-gate 			assert(cur_tg != NULL);
17160Sstevel@tonic-gate 			/*
17170Sstevel@tonic-gate 			 * The crtt could be zero for some reason,
17180Sstevel@tonic-gate 			 * Eg. the phyint could be failed. If the crtt is
17190Sstevel@tonic-gate 			 * not available use group's probe interval,
17200Sstevel@tonic-gate 			 * which is a worst case estimate.
17210Sstevel@tonic-gate 			 */
17228485SPeter.Memishian@Sun.COM 			timeout = ns2ms(pr_statp->pr_hrtime_start);
17230Sstevel@tonic-gate 			if (cur_tg->tg_crtt != 0) {
17248485SPeter.Memishian@Sun.COM 				timeout += cur_tg->tg_crtt;
17250Sstevel@tonic-gate 			} else {
17268485SPeter.Memishian@Sun.COM 				timeout += probe_interval;
17270Sstevel@tonic-gate 			}
17280Sstevel@tonic-gate 			if (TIME_LT(timeout, cur_time)) {
17290Sstevel@tonic-gate 				pr_statp->pr_time_lost = timeout;
17308485SPeter.Memishian@Sun.COM 				probe_chstate(pr_statp, pii, PR_LOST);
17310Sstevel@tonic-gate 			} else if (i == 1) {
17320Sstevel@tonic-gate 				/*
17330Sstevel@tonic-gate 				 * We are forced to consider this probe
17340Sstevel@tonic-gate 				 * lost, as we can have at most 2 unack.
17350Sstevel@tonic-gate 				 * probes any time, and we will be sending a
17360Sstevel@tonic-gate 				 * probe at the end of this function.
17370Sstevel@tonic-gate 				 * Normally, we should not be here, but
17380Sstevel@tonic-gate 				 * this can happen if an incoming response
17390Sstevel@tonic-gate 				 * that was considered lost has increased
17400Sstevel@tonic-gate 				 * the crtt for this target, and also bumped
17410Sstevel@tonic-gate 				 * up the FDT. Note that we never cancel or
17420Sstevel@tonic-gate 				 * increase the current pii_time_left, so
17430Sstevel@tonic-gate 				 * when the timer fires, we find 2 valid
17440Sstevel@tonic-gate 				 * unacked probes, and they are yet to timeout
17450Sstevel@tonic-gate 				 */
17460Sstevel@tonic-gate 				pr_statp->pr_time_lost = cur_time;
17478485SPeter.Memishian@Sun.COM 				probe_chstate(pr_statp, pii, PR_LOST);
17480Sstevel@tonic-gate 			} else {
17490Sstevel@tonic-gate 				/*
17500Sstevel@tonic-gate 				 * Only the most recent probe can enter
17510Sstevel@tonic-gate 				 * this 'else' arm. The second most recent
17520Sstevel@tonic-gate 				 * probe must take either of the above arms,
17530Sstevel@tonic-gate 				 * if it is unacked.
17540Sstevel@tonic-gate 				 */
17550Sstevel@tonic-gate 				valid_unack_count++;
17560Sstevel@tonic-gate 			}
17570Sstevel@tonic-gate 			break;
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 		pr_ndx = PROBE_INDEX_PREV(pr_ndx);
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/*
17630Sstevel@tonic-gate 	 * We send out 1 probe randomly in the interval between one half
17640Sstevel@tonic-gate 	 * and one probe interval for the group. Given that the CRTT is always
17650Sstevel@tonic-gate 	 * less than the group's probe interval, we can have at most 1
17660Sstevel@tonic-gate 	 * unacknowledged probe now.  All previous probes are either lost or
17670Sstevel@tonic-gate 	 * acked.
17680Sstevel@tonic-gate 	 */
17690Sstevel@tonic-gate 	assert(valid_unack_count == 0 || valid_unack_count == 1);
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	/*
17720Sstevel@tonic-gate 	 * The timer has fired. Take appropriate action depending
17730Sstevel@tonic-gate 	 * on the current state of the phyint.
17740Sstevel@tonic-gate 	 *
17758485SPeter.Memishian@Sun.COM 	 * PI_RUNNING state 	- Failure detection
17768485SPeter.Memishian@Sun.COM 	 * PI_FAILED state 	- Repair detection
17770Sstevel@tonic-gate 	 */
17780Sstevel@tonic-gate 	switch (pii->pii_phyint->pi_state) {
17790Sstevel@tonic-gate 	case PI_FAILED:
17800Sstevel@tonic-gate 		/*
17810Sstevel@tonic-gate 		 * If the most recent probe (excluding unacked probes that
17820Sstevel@tonic-gate 		 * are yet to time out) has been acked, check whether the
17838485SPeter.Memishian@Sun.COM 		 * phyint is now repaired.
17840Sstevel@tonic-gate 		 */
17850Sstevel@tonic-gate 		if (pii->pii_rack + valid_unack_count + 1 == pii->pii_snxt) {
17860Sstevel@tonic-gate 			phyint_check_for_repair(pii->pii_phyint);
17870Sstevel@tonic-gate 		}
17880Sstevel@tonic-gate 		break;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	case PI_RUNNING:
17910Sstevel@tonic-gate 		/*
17920Sstevel@tonic-gate 		 * It's possible our probes have been lost because of a
17930Sstevel@tonic-gate 		 * spanning-tree mandated quiet period on the switch.  If so,
17948485SPeter.Memishian@Sun.COM 		 * ignore the lost probes.
17950Sstevel@tonic-gate 		 */
17960Sstevel@tonic-gate 		if (pii->pii_fd_hrtime - cur_hrtime > 0)
17970Sstevel@tonic-gate 			break;
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 		if (pii->pii_rack + valid_unack_count + 1 != pii->pii_snxt) {
18000Sstevel@tonic-gate 			/*
18010Sstevel@tonic-gate 			 * We have 1 or more failed probes (excluding unacked
18020Sstevel@tonic-gate 			 * probes that are yet to time out). Determine if the
18038485SPeter.Memishian@Sun.COM 			 * phyint has failed.
18040Sstevel@tonic-gate 			 */
18050Sstevel@tonic-gate 			phyint_inst_check_for_failure(pii);
18060Sstevel@tonic-gate 		}
18070Sstevel@tonic-gate 		break;
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	default:
18100Sstevel@tonic-gate 		logerr("phyint_inst_timer: invalid state %d\n",
18110Sstevel@tonic-gate 		    pii->pii_phyint->pi_state);
18120Sstevel@tonic-gate 		abort();
18130Sstevel@tonic-gate 	}
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	/*
18160Sstevel@tonic-gate 	 * Start the next probe. probe() will also set pii->pii_probe_time_left
18170Sstevel@tonic-gate 	 * to the group's probe interval. If phyint_failed -> target_flush_hosts
18180Sstevel@tonic-gate 	 * was called, the target list may be empty.
18190Sstevel@tonic-gate 	 */
18200Sstevel@tonic-gate 	if (pii->pii_target_next != NULL) {
18218485SPeter.Memishian@Sun.COM 		probe(pii, PROBE_UNI, cur_hrtime);
18220Sstevel@tonic-gate 		/*
18230Sstevel@tonic-gate 		 * If we have just the one probe target, and we're not using
18240Sstevel@tonic-gate 		 * router targets, try to find another as we presently have
18250Sstevel@tonic-gate 		 * no resilience.
18260Sstevel@tonic-gate 		 */
18270Sstevel@tonic-gate 		if (!pii->pii_targets_are_routers && pii->pii_ntargets == 1)
18288485SPeter.Memishian@Sun.COM 			probe(pii, PROBE_MULTI, cur_hrtime);
18290Sstevel@tonic-gate 	} else {
18308485SPeter.Memishian@Sun.COM 		probe(pii, PROBE_MULTI, cur_hrtime);
18310Sstevel@tonic-gate 	}
18320Sstevel@tonic-gate 	return (interval);
18330Sstevel@tonic-gate }
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate /*
18360Sstevel@tonic-gate  * Start the probe timer for an interface instance.
18370Sstevel@tonic-gate  */
18380Sstevel@tonic-gate void
18390Sstevel@tonic-gate start_timer(struct phyint_instance *pii)
18400Sstevel@tonic-gate {
18410Sstevel@tonic-gate 	uint32_t interval;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	/*
18440Sstevel@tonic-gate 	 * Spread the base probe times (pi_snxt_basetime) across phyints
18450Sstevel@tonic-gate 	 * uniformly over the (curtime..curtime + the group's probe_interval).
18460Sstevel@tonic-gate 	 * pi_snxt_basetime is strictly periodic with a frequency of
18470Sstevel@tonic-gate 	 * the group's probe interval. The actual probe time pi_snxt_time
18480Sstevel@tonic-gate 	 * adds some randomness to pi_snxt_basetime and happens in probe().
18490Sstevel@tonic-gate 	 * For the 1st probe on each phyint after the timer is started,
18500Sstevel@tonic-gate 	 * pi_snxt_time and pi_snxt_basetime are the same.
18510Sstevel@tonic-gate 	 */
18520Sstevel@tonic-gate 	interval = GET_RANDOM(0,
18530Sstevel@tonic-gate 	    (int)pii->pii_phyint->pi_group->pg_probeint);
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	pii->pii_snxt_basetime = getcurrenttime() + interval;
18560Sstevel@tonic-gate 	pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
18570Sstevel@tonic-gate 	pii->pii_snxt_time = pii->pii_snxt_basetime;
18580Sstevel@tonic-gate 	timer_schedule(interval);
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate /*
18620Sstevel@tonic-gate  * Restart the probe timer on an interface instance.
18630Sstevel@tonic-gate  */
18640Sstevel@tonic-gate static void
18650Sstevel@tonic-gate restart_timer(struct phyint_instance *pii)
18660Sstevel@tonic-gate {
18670Sstevel@tonic-gate 	/*
18680Sstevel@tonic-gate 	 * We don't need to restart the timer if it was never started in
18690Sstevel@tonic-gate 	 * the first place (pii->pii_basetime_inited not set), as the timer
18700Sstevel@tonic-gate 	 * won't have gone off yet.
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	if (pii->pii_basetime_inited != 0) {
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 		if (debug & D_LINKNOTE)
18750Sstevel@tonic-gate 			logdebug("restart timer: restarting timer on %s, "
18760Sstevel@tonic-gate 			    "address family %s\n", pii->pii_phyint->pi_name,
18770Sstevel@tonic-gate 			    AF_STR(pii->pii_af));
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 		start_timer(pii);
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate }
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate static void
18840Sstevel@tonic-gate process_link_state_down(struct phyint *pi)
18850Sstevel@tonic-gate {
18860Sstevel@tonic-gate 	logerr("The link has gone down on %s\n", pi->pi_name);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	/*
18890Sstevel@tonic-gate 	 * Clear the probe statistics arrays, we don't want the repair
18908485SPeter.Memishian@Sun.COM 	 * detection logic relying on probes that were successful prior
18918485SPeter.Memishian@Sun.COM 	 * to the link going down.
18920Sstevel@tonic-gate 	 */
18930Sstevel@tonic-gate 	if (PROBE_CAPABLE(pi->pi_v4))
18940Sstevel@tonic-gate 		clear_pii_probe_stats(pi->pi_v4);
18950Sstevel@tonic-gate 	if (PROBE_CAPABLE(pi->pi_v6))
18960Sstevel@tonic-gate 		clear_pii_probe_stats(pi->pi_v6);
18970Sstevel@tonic-gate 	/*
18980Sstevel@tonic-gate 	 * Check for interface failure.  Although we know the interface
18990Sstevel@tonic-gate 	 * has failed, we don't know if all the other interfaces in the
19000Sstevel@tonic-gate 	 * group have failed as well.
19010Sstevel@tonic-gate 	 */
19020Sstevel@tonic-gate 	if ((pi->pi_state == PI_RUNNING) ||
19030Sstevel@tonic-gate 	    (pi->pi_state != PI_FAILED && !GROUP_FAILED(pi->pi_group))) {
19040Sstevel@tonic-gate 		if (debug & D_LINKNOTE) {
19050Sstevel@tonic-gate 			logdebug("process_link_state_down:"
19060Sstevel@tonic-gate 			    " checking for failure on %s\n", pi->pi_name);
19070Sstevel@tonic-gate 		}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 		if (pi->pi_v4 != NULL)
19100Sstevel@tonic-gate 			phyint_inst_check_for_failure(pi->pi_v4);
19110Sstevel@tonic-gate 		else if (pi->pi_v6 != NULL)
19120Sstevel@tonic-gate 			phyint_inst_check_for_failure(pi->pi_v6);
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate static void
19170Sstevel@tonic-gate process_link_state_up(struct phyint *pi)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	logerr("The link has come up on %s\n", pi->pi_name);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	/*
19220Sstevel@tonic-gate 	 * We stopped any running timers on each instance when the link
19230Sstevel@tonic-gate 	 * went down, so restart them.
19240Sstevel@tonic-gate 	 */
19250Sstevel@tonic-gate 	if (pi->pi_v4)
19260Sstevel@tonic-gate 		restart_timer(pi->pi_v4);
19270Sstevel@tonic-gate 	if (pi->pi_v6)
19280Sstevel@tonic-gate 		restart_timer(pi->pi_v6);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	phyint_check_for_repair(pi);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	pi->pi_whenup[pi->pi_whendx++] = getcurrenttime();
19330Sstevel@tonic-gate 	if (pi->pi_whendx == LINK_UP_PERMIN)
19340Sstevel@tonic-gate 		pi->pi_whendx = 0;
19350Sstevel@tonic-gate }
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate /*
19380Sstevel@tonic-gate  * Process any changes in link state passed up from the interfaces.
19390Sstevel@tonic-gate  */
19400Sstevel@tonic-gate void
19410Sstevel@tonic-gate process_link_state_changes(void)
19420Sstevel@tonic-gate {
19430Sstevel@tonic-gate 	struct phyint *pi;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	/* Look for interfaces where the link state has just changed */
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
19480Sstevel@tonic-gate 		boolean_t old_link_state_up = LINK_UP(pi);
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		/*
19510Sstevel@tonic-gate 		 * Except when the "phyint" structure is created, this is
19520Sstevel@tonic-gate 		 * the only place the link state is updated.  This allows
19530Sstevel@tonic-gate 		 * this routine to detect changes in link state, rather
19540Sstevel@tonic-gate 		 * than just the current state.
19550Sstevel@tonic-gate 		 */
19560Sstevel@tonic-gate 		UPDATE_LINK_STATE(pi);
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 		if (LINK_DOWN(pi)) {
19590Sstevel@tonic-gate 			/*
19600Sstevel@tonic-gate 			 * Has link just gone down?
19610Sstevel@tonic-gate 			 */
19620Sstevel@tonic-gate 			if (old_link_state_up)
19630Sstevel@tonic-gate 				process_link_state_down(pi);
19640Sstevel@tonic-gate 		} else {
19650Sstevel@tonic-gate 			/*
19660Sstevel@tonic-gate 			 * Has link just gone back up?
19670Sstevel@tonic-gate 			 */
19680Sstevel@tonic-gate 			if (!old_link_state_up)
19690Sstevel@tonic-gate 				process_link_state_up(pi);
19700Sstevel@tonic-gate 		}
19710Sstevel@tonic-gate 	}
19720Sstevel@tonic-gate }
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate void
19750Sstevel@tonic-gate reset_crtt_all(struct phyint *pi)
19760Sstevel@tonic-gate {
19770Sstevel@tonic-gate 	struct phyint_instance *pii;
19780Sstevel@tonic-gate 	struct target *tg;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	pii = pi->pi_v4;
19810Sstevel@tonic-gate 	if (pii != NULL) {
19820Sstevel@tonic-gate 		for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
19830Sstevel@tonic-gate 			tg->tg_crtt = 0;
19840Sstevel@tonic-gate 			tg->tg_rtt_sa = -1;
19850Sstevel@tonic-gate 			tg->tg_rtt_sd = 0;
19860Sstevel@tonic-gate 		}
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	pii = pi->pi_v6;
19900Sstevel@tonic-gate 	if (pii != NULL) {
19910Sstevel@tonic-gate 		for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
19920Sstevel@tonic-gate 			tg->tg_crtt = 0;
19930Sstevel@tonic-gate 			tg->tg_rtt_sa = -1;
19940Sstevel@tonic-gate 			tg->tg_rtt_sd = 0;
19950Sstevel@tonic-gate 		}
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate }
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate /*
20000Sstevel@tonic-gate  * Check if the phyint has failed the last NUM_PROBE_FAILS consecutive
20010Sstevel@tonic-gate  * probes on both instances IPv4 and IPv6.
20020Sstevel@tonic-gate  * If the interface has failed, return the time of the first probe failure
20030Sstevel@tonic-gate  * in "tff".
20040Sstevel@tonic-gate  */
20050Sstevel@tonic-gate static int
20060Sstevel@tonic-gate phyint_inst_probe_failure_state(struct phyint_instance *pii, uint_t *tff)
20070Sstevel@tonic-gate {
20080Sstevel@tonic-gate 	uint_t	pi_tff;
20090Sstevel@tonic-gate 	struct	target *cur_tg;
20100Sstevel@tonic-gate 	struct	probe_fail_count pfinfo;
20110Sstevel@tonic-gate 	struct	phyint_instance *pii_other;
20120Sstevel@tonic-gate 	int	pr_ndx;
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	/*
20150Sstevel@tonic-gate 	 * Get the number of consecutive failed probes on
20160Sstevel@tonic-gate 	 * this phyint across all targets. Also get the number
20170Sstevel@tonic-gate 	 * of consecutive failed probes on this target only
20180Sstevel@tonic-gate 	 */
20190Sstevel@tonic-gate 	pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
20200Sstevel@tonic-gate 	cur_tg = pii->pii_probes[pr_ndx].pr_target;
20210Sstevel@tonic-gate 	probe_fail_info(pii, cur_tg, &pfinfo);
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	/* Get the time of first failure, for later use */
20240Sstevel@tonic-gate 	pi_tff = pfinfo.pf_tff;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	/*
20270Sstevel@tonic-gate 	 * If the current target has not responded to the
20280Sstevel@tonic-gate 	 * last NUM_PROBE_FAILS probes, and other targets are
20290Sstevel@tonic-gate 	 * responding delete this target. Dead gateway detection
20300Sstevel@tonic-gate 	 * will eventually remove this target (if router) from the
20310Sstevel@tonic-gate 	 * routing tables. If that does not occur, we may end
20320Sstevel@tonic-gate 	 * up adding this to our list again.
20330Sstevel@tonic-gate 	 */
20340Sstevel@tonic-gate 	if (pfinfo.pf_nfail < NUM_PROBE_FAILS &&
20350Sstevel@tonic-gate 	    pfinfo.pf_nfail_tg >= NUM_PROBE_FAILS) {
20360Sstevel@tonic-gate 		if (pii->pii_targets_are_routers) {
20370Sstevel@tonic-gate 			if (cur_tg->tg_status == TG_ACTIVE)
20380Sstevel@tonic-gate 				pii->pii_ntargets--;
20390Sstevel@tonic-gate 			cur_tg->tg_status = TG_DEAD;
20400Sstevel@tonic-gate 			cur_tg->tg_crtt = 0;
20410Sstevel@tonic-gate 			cur_tg->tg_rtt_sa = -1;
20420Sstevel@tonic-gate 			cur_tg->tg_rtt_sd = 0;
20430Sstevel@tonic-gate 			if (pii->pii_target_next == cur_tg)
20440Sstevel@tonic-gate 				pii->pii_target_next = target_next(cur_tg);
20450Sstevel@tonic-gate 		} else {
20460Sstevel@tonic-gate 			target_delete(cur_tg);
20478485SPeter.Memishian@Sun.COM 			probe(pii, PROBE_MULTI, gethrtime());
20480Sstevel@tonic-gate 		}
20490Sstevel@tonic-gate 		return (PHYINT_OK);
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 	/*
20530Sstevel@tonic-gate 	 * If the phyint has lost NUM_PROBE_FAILS or more
20540Sstevel@tonic-gate 	 * consecutive probes, on both IPv4 and IPv6 protocol
20550Sstevel@tonic-gate 	 * instances of the phyint, then trigger failure
20560Sstevel@tonic-gate 	 * detection, else return false
20570Sstevel@tonic-gate 	 */
20580Sstevel@tonic-gate 	if (pfinfo.pf_nfail < NUM_PROBE_FAILS)
20590Sstevel@tonic-gate 		return (PHYINT_OK);
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	pii_other = phyint_inst_other(pii);
20620Sstevel@tonic-gate 	if (PROBE_CAPABLE(pii_other)) {
20630Sstevel@tonic-gate 		probe_fail_info(pii_other, NULL, &pfinfo);
20640Sstevel@tonic-gate 		if (pfinfo.pf_nfail >= NUM_PROBE_FAILS) {
20650Sstevel@tonic-gate 			/*
20660Sstevel@tonic-gate 			 * We have NUM_PROBE_FAILS or more failures
20670Sstevel@tonic-gate 			 * on both IPv4 and IPv6. Get the earliest
20680Sstevel@tonic-gate 			 * time when failure was detected on this
20690Sstevel@tonic-gate 			 * phyint across IPv4 and IPv6.
20700Sstevel@tonic-gate 			 */
20710Sstevel@tonic-gate 			if (TIME_LT(pfinfo.pf_tff, pi_tff))
20720Sstevel@tonic-gate 				pi_tff = pfinfo.pf_tff;
20730Sstevel@tonic-gate 		} else {
20740Sstevel@tonic-gate 			/*
20750Sstevel@tonic-gate 			 * This instance has < NUM_PROBE_FAILS failure.
20760Sstevel@tonic-gate 			 * So return false
20770Sstevel@tonic-gate 			 */
20780Sstevel@tonic-gate 			return (PHYINT_OK);
20790Sstevel@tonic-gate 		}
20800Sstevel@tonic-gate 	}
20810Sstevel@tonic-gate 	*tff = pi_tff;
20820Sstevel@tonic-gate 	return (PHYINT_FAILURE);
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate /*
20860Sstevel@tonic-gate  * Check if the link has gone down on this phyint, or it has failed the
20870Sstevel@tonic-gate  * last NUM_PROBE_FAILS consecutive probes on both instances IPv4 and IPv6.
20880Sstevel@tonic-gate  * Also look at other phyints of this group, for group failures.
20890Sstevel@tonic-gate  */
20900Sstevel@tonic-gate int
20910Sstevel@tonic-gate failure_state(struct phyint_instance *pii)
20920Sstevel@tonic-gate {
20930Sstevel@tonic-gate 	struct	probe_success_count psinfo;
20940Sstevel@tonic-gate 	uint_t	pi2_tls;		/* time last success */
20950Sstevel@tonic-gate 	uint_t	pi_tff;			/* time first fail */
20968485SPeter.Memishian@Sun.COM 	struct	phyint *pi2;
20970Sstevel@tonic-gate 	struct	phyint *pi;
20980Sstevel@tonic-gate 	struct	phyint_instance *pii2;
20990Sstevel@tonic-gate 	struct  phyint_group *pg;
21008485SPeter.Memishian@Sun.COM 	int	retval;
21010Sstevel@tonic-gate 
21028485SPeter.Memishian@Sun.COM 	if (debug & D_FAILREP)
21030Sstevel@tonic-gate 		logdebug("phyint_failed(%s)\n", pii->pii_name);
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	pi = pii->pii_phyint;
21060Sstevel@tonic-gate 	pg = pi->pi_group;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	if (LINK_UP(pi) && phyint_inst_probe_failure_state(pii, &pi_tff) ==
21094929Srk129064 	    PHYINT_OK)
21100Sstevel@tonic-gate 		return (PHYINT_OK);
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	/*
21138485SPeter.Memishian@Sun.COM 	 * At this point, the link is down, or the phyint is suspect, as it
21148485SPeter.Memishian@Sun.COM 	 * has lost NUM_PROBE_FAILS or more probes. If the phyint does not
21158485SPeter.Memishian@Sun.COM 	 * belong to any group, this is a PHYINT_FAILURE.  Otherwise, continue
21168485SPeter.Memishian@Sun.COM 	 * on to determine whether this should be considered a PHYINT_FAILURE
21178485SPeter.Memishian@Sun.COM 	 * or GROUP_FAILURE.
21180Sstevel@tonic-gate 	 */
21198485SPeter.Memishian@Sun.COM 	if (pg == phyint_anongroup)
21200Sstevel@tonic-gate 		return (PHYINT_FAILURE);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	/*
21230Sstevel@tonic-gate 	 * Need to compare against other phyints of the same group
21240Sstevel@tonic-gate 	 * to exclude group failures. If the failure was detected via
21250Sstevel@tonic-gate 	 * probing, then if the time of last success (tls) of any
21260Sstevel@tonic-gate 	 * phyint is more recent than the time of first fail (tff) of the
21270Sstevel@tonic-gate 	 * phyint in question, and the link is up on the phyint,
21280Sstevel@tonic-gate 	 * then it is a phyint failure. Otherwise it is a group failure.
21290Sstevel@tonic-gate 	 * If failure was detected via a link down notification sent from
21300Sstevel@tonic-gate 	 * the driver to IP, we see if any phyints in the group are still
21310Sstevel@tonic-gate 	 * running and haven't received a link down notification.  We
21320Sstevel@tonic-gate 	 * will usually be processing the link down notification shortly
21330Sstevel@tonic-gate 	 * after it was received, so there is no point looking at the tls
21340Sstevel@tonic-gate 	 * of other phyints.
21350Sstevel@tonic-gate 	 */
21368485SPeter.Memishian@Sun.COM 	retval = GROUP_FAILURE;
21370Sstevel@tonic-gate 	for (pi2 = pg->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
21380Sstevel@tonic-gate 		/* Exclude ourself from comparison */
21390Sstevel@tonic-gate 		if (pi2 == pi)
21400Sstevel@tonic-gate 			continue;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		if (LINK_DOWN(pi)) {
21430Sstevel@tonic-gate 			/*
21448485SPeter.Memishian@Sun.COM 			 * We use FLAGS_TO_LINK_STATE() to test the flags
21458485SPeter.Memishian@Sun.COM 			 * directly, rather then LINK_UP() or LINK_DOWN(), as
21468485SPeter.Memishian@Sun.COM 			 * we may not have got round to processing the link
21478485SPeter.Memishian@Sun.COM 			 * state for the other phyints in the group yet.
21480Sstevel@tonic-gate 			 *
21498485SPeter.Memishian@Sun.COM 			 * The check for PI_RUNNING and group failure handles
21508485SPeter.Memishian@Sun.COM 			 * the case when the group begins to recover.
21518485SPeter.Memishian@Sun.COM 			 * PI_RUNNING will be set, and group failure cleared
21528485SPeter.Memishian@Sun.COM 			 * only after receipt of NUM_PROBE_REPAIRS, by which
21538485SPeter.Memishian@Sun.COM 			 * time the other phyints should have received at
21548485SPeter.Memishian@Sun.COM 			 * least 1 packet, and so will not have NUM_PROBE_FAILS.
21550Sstevel@tonic-gate 			 */
21560Sstevel@tonic-gate 			if ((pi2->pi_state == PI_RUNNING) &&
21578485SPeter.Memishian@Sun.COM 			    !GROUP_FAILED(pg) && FLAGS_TO_LINK_STATE(pi2)) {
21588485SPeter.Memishian@Sun.COM 				retval = PHYINT_FAILURE;
21598485SPeter.Memishian@Sun.COM 				break;
21608485SPeter.Memishian@Sun.COM 			}
21618485SPeter.Memishian@Sun.COM 			continue;
21628485SPeter.Memishian@Sun.COM 		}
21638485SPeter.Memishian@Sun.COM 
21648485SPeter.Memishian@Sun.COM 		if (LINK_DOWN(pi2))
21658485SPeter.Memishian@Sun.COM 			continue;
21668485SPeter.Memishian@Sun.COM 
21678485SPeter.Memishian@Sun.COM 		/*
21688485SPeter.Memishian@Sun.COM 		 * If there's no probe-based failure detection on this
21698485SPeter.Memishian@Sun.COM 		 * interface, and its link is still up, then it's still
21708485SPeter.Memishian@Sun.COM 		 * working and thus the group has not failed.
21718485SPeter.Memishian@Sun.COM 		 */
21728485SPeter.Memishian@Sun.COM 		if (!PROBE_ENABLED(pi2->pi_v4) && !PROBE_ENABLED(pi2->pi_v6)) {
21738485SPeter.Memishian@Sun.COM 			retval = PHYINT_FAILURE;
21748485SPeter.Memishian@Sun.COM 			break;
21758485SPeter.Memishian@Sun.COM 		}
21768485SPeter.Memishian@Sun.COM 
21778485SPeter.Memishian@Sun.COM 		/*
21788485SPeter.Memishian@Sun.COM 		 * Need to compare against both IPv4 and IPv6 instances.
21798485SPeter.Memishian@Sun.COM 		 */
21808485SPeter.Memishian@Sun.COM 		pii2 = pi2->pi_v4;
21818485SPeter.Memishian@Sun.COM 		if (pii2 != NULL) {
21828485SPeter.Memishian@Sun.COM 			probe_success_info(pii2, NULL, &psinfo);
21838485SPeter.Memishian@Sun.COM 			if (psinfo.ps_tls_valid) {
21848485SPeter.Memishian@Sun.COM 				pi2_tls = psinfo.ps_tls;
21858485SPeter.Memishian@Sun.COM 				/*
21868485SPeter.Memishian@Sun.COM 				 * See comment above regarding check
21878485SPeter.Memishian@Sun.COM 				 * for PI_RUNNING and group failure.
21888485SPeter.Memishian@Sun.COM 				 */
21898485SPeter.Memishian@Sun.COM 				if (TIME_GT(pi2_tls, pi_tff) &&
21908485SPeter.Memishian@Sun.COM 				    (pi2->pi_state == PI_RUNNING) &&
21918485SPeter.Memishian@Sun.COM 				    !GROUP_FAILED(pg) &&
21928485SPeter.Memishian@Sun.COM 				    FLAGS_TO_LINK_STATE(pi2)) {
21938485SPeter.Memishian@Sun.COM 					retval = PHYINT_FAILURE;
21948485SPeter.Memishian@Sun.COM 					break;
21950Sstevel@tonic-gate 				}
21960Sstevel@tonic-gate 			}
21978485SPeter.Memishian@Sun.COM 		}
21980Sstevel@tonic-gate 
21998485SPeter.Memishian@Sun.COM 		pii2 = pi2->pi_v6;
22008485SPeter.Memishian@Sun.COM 		if (pii2 != NULL) {
22018485SPeter.Memishian@Sun.COM 			probe_success_info(pii2, NULL, &psinfo);
22028485SPeter.Memishian@Sun.COM 			if (psinfo.ps_tls_valid) {
22038485SPeter.Memishian@Sun.COM 				pi2_tls = psinfo.ps_tls;
22048485SPeter.Memishian@Sun.COM 				/*
22058485SPeter.Memishian@Sun.COM 				 * See comment above regarding check
22068485SPeter.Memishian@Sun.COM 				 * for PI_RUNNING and group failure.
22078485SPeter.Memishian@Sun.COM 				 */
22088485SPeter.Memishian@Sun.COM 				if (TIME_GT(pi2_tls, pi_tff) &&
22098485SPeter.Memishian@Sun.COM 				    (pi2->pi_state == PI_RUNNING) &&
22108485SPeter.Memishian@Sun.COM 				    !GROUP_FAILED(pg) &&
22118485SPeter.Memishian@Sun.COM 				    FLAGS_TO_LINK_STATE(pi2)) {
22128485SPeter.Memishian@Sun.COM 					retval = PHYINT_FAILURE;
22138485SPeter.Memishian@Sun.COM 					break;
22140Sstevel@tonic-gate 				}
22150Sstevel@tonic-gate 			}
22160Sstevel@tonic-gate 		}
22170Sstevel@tonic-gate 	}
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 	/*
22208485SPeter.Memishian@Sun.COM 	 * Update the group state to account for the changes.
22210Sstevel@tonic-gate 	 */
22228485SPeter.Memishian@Sun.COM 	phyint_group_refresh_state(pg);
22238485SPeter.Memishian@Sun.COM 	return (retval);
22240Sstevel@tonic-gate }
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate /*
22270Sstevel@tonic-gate  * Return the information associated with consecutive probe successes
22280Sstevel@tonic-gate  * starting with the most recent probe. At most the last 2 probes can be
22290Sstevel@tonic-gate  * in the unacknowledged state. All previous probes have either failed
22300Sstevel@tonic-gate  * or succeeded.
22310Sstevel@tonic-gate  */
22320Sstevel@tonic-gate static void
22330Sstevel@tonic-gate probe_success_info(struct phyint_instance *pii, struct target *cur_tg,
22340Sstevel@tonic-gate     struct probe_success_count *psinfo)
22350Sstevel@tonic-gate {
22360Sstevel@tonic-gate 	uint_t	i;
22370Sstevel@tonic-gate 	struct probe_stats *pr_statp;
22380Sstevel@tonic-gate 	uint_t most_recent;
22390Sstevel@tonic-gate 	uint_t second_most_recent;
22400Sstevel@tonic-gate 	boolean_t pi_found_failure = _B_FALSE;
22410Sstevel@tonic-gate 	boolean_t tg_found_failure = _B_FALSE;
22420Sstevel@tonic-gate 	uint_t now;
22430Sstevel@tonic-gate 	uint_t timeout;
22440Sstevel@tonic-gate 	struct target *tg;
22450Sstevel@tonic-gate 
22468485SPeter.Memishian@Sun.COM 	if (debug & D_FAILREP)
22470Sstevel@tonic-gate 		logdebug("probe_success_info(%s)\n", pii->pii_name);
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	bzero(psinfo, sizeof (*psinfo));
22500Sstevel@tonic-gate 	now = getcurrenttime();
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	/*
22530Sstevel@tonic-gate 	 * Start with the most recent probe, and count the number
22540Sstevel@tonic-gate 	 * of consecutive probe successes. Latch the number of successes
22550Sstevel@tonic-gate 	 * on hitting a failure.
22560Sstevel@tonic-gate 	 */
22570Sstevel@tonic-gate 	most_recent = PROBE_INDEX_PREV(pii->pii_probe_next);
22580Sstevel@tonic-gate 	second_most_recent = PROBE_INDEX_PREV(most_recent);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	for (i = most_recent; i != pii->pii_probe_next;
22610Sstevel@tonic-gate 	    i = PROBE_INDEX_PREV(i)) {
22620Sstevel@tonic-gate 		pr_statp = &pii->pii_probes[i];
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 		switch (pr_statp->pr_status) {
22650Sstevel@tonic-gate 		case PR_UNACKED:
22660Sstevel@tonic-gate 			/*
22670Sstevel@tonic-gate 			 * Only the most recent 2 probes can be unacknowledged
22680Sstevel@tonic-gate 			 */
22690Sstevel@tonic-gate 			assert(i == most_recent || i == second_most_recent);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 			tg = pr_statp->pr_target;
22720Sstevel@tonic-gate 			assert(tg != NULL);
22730Sstevel@tonic-gate 			/*
22740Sstevel@tonic-gate 			 * The crtt could be zero for some reason,
22750Sstevel@tonic-gate 			 * Eg. the phyint could be failed. If the crtt is
22760Sstevel@tonic-gate 			 * not available use the value of the group's probe
22770Sstevel@tonic-gate 			 * interval which is a worst case estimate.
22780Sstevel@tonic-gate 			 */
22798485SPeter.Memishian@Sun.COM 			timeout = ns2ms(pr_statp->pr_hrtime_start);
22800Sstevel@tonic-gate 			if (tg->tg_crtt != 0) {
22818485SPeter.Memishian@Sun.COM 				timeout += tg->tg_crtt;
22820Sstevel@tonic-gate 			} else {
22838485SPeter.Memishian@Sun.COM 				timeout +=
22840Sstevel@tonic-gate 				    pii->pii_phyint->pi_group->pg_probeint;
22850Sstevel@tonic-gate 			}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 			if (TIME_LT(timeout, now)) {
22880Sstevel@tonic-gate 				/*
22890Sstevel@tonic-gate 				 * We hit a failure. Latch the total number of
22900Sstevel@tonic-gate 				 * recent consecutive successes.
22910Sstevel@tonic-gate 				 */
22920Sstevel@tonic-gate 				pr_statp->pr_time_lost = timeout;
22938485SPeter.Memishian@Sun.COM 				probe_chstate(pr_statp, pii, PR_LOST);
22940Sstevel@tonic-gate 				pi_found_failure = _B_TRUE;
22950Sstevel@tonic-gate 				if (cur_tg != NULL && tg == cur_tg) {
22960Sstevel@tonic-gate 					/*
22970Sstevel@tonic-gate 					 * We hit a failure for the desired
22980Sstevel@tonic-gate 					 * target. Latch the number of recent
22990Sstevel@tonic-gate 					 * consecutive successes for this target
23000Sstevel@tonic-gate 					 */
23010Sstevel@tonic-gate 					tg_found_failure = _B_TRUE;
23020Sstevel@tonic-gate 				}
23030Sstevel@tonic-gate 			}
23040Sstevel@tonic-gate 			break;
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 		case PR_ACKED:
23070Sstevel@tonic-gate 			/*
23080Sstevel@tonic-gate 			 * Bump up the count of probe successes, if we
23090Sstevel@tonic-gate 			 * have not seen any failure so far.
23100Sstevel@tonic-gate 			 */
23110Sstevel@tonic-gate 			if (!pi_found_failure)
23120Sstevel@tonic-gate 				psinfo->ps_nsucc++;
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg &&
23150Sstevel@tonic-gate 			    !tg_found_failure) {
23160Sstevel@tonic-gate 				psinfo->ps_nsucc_tg++;
23170Sstevel@tonic-gate 			}
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 			/*
23200Sstevel@tonic-gate 			 * Record the time of last success, if this is
23210Sstevel@tonic-gate 			 * the most recent probe success.
23220Sstevel@tonic-gate 			 */
23230Sstevel@tonic-gate 			if (!psinfo->ps_tls_valid) {
23248485SPeter.Memishian@Sun.COM 				psinfo->ps_tls =
23258485SPeter.Memishian@Sun.COM 				    ns2ms(pr_statp->pr_hrtime_ackproc);
23260Sstevel@tonic-gate 				psinfo->ps_tls_valid = _B_TRUE;
23270Sstevel@tonic-gate 			}
23280Sstevel@tonic-gate 			break;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 		case PR_LOST:
23310Sstevel@tonic-gate 			/*
23320Sstevel@tonic-gate 			 * We hit a failure. Latch the total number of
23330Sstevel@tonic-gate 			 * recent consecutive successes.
23340Sstevel@tonic-gate 			 */
23350Sstevel@tonic-gate 			pi_found_failure = _B_TRUE;
23360Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg) {
23370Sstevel@tonic-gate 				/*
23380Sstevel@tonic-gate 				 * We hit a failure for the desired target.
23390Sstevel@tonic-gate 				 * Latch the number of recent consecutive
23400Sstevel@tonic-gate 				 * successes for this target
23410Sstevel@tonic-gate 				 */
23420Sstevel@tonic-gate 				tg_found_failure = _B_TRUE;
23430Sstevel@tonic-gate 			}
23440Sstevel@tonic-gate 			break;
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 		default:
23470Sstevel@tonic-gate 			return;
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 		}
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate }
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate /*
23540Sstevel@tonic-gate  * Return the information associated with consecutive probe failures
23550Sstevel@tonic-gate  * starting with the most recent probe. Only the last 2 probes can be in the
23560Sstevel@tonic-gate  * unacknowledged state. All previous probes have either failed or succeeded.
23570Sstevel@tonic-gate  */
23580Sstevel@tonic-gate static void
23590Sstevel@tonic-gate probe_fail_info(struct phyint_instance *pii, struct target *cur_tg,
23600Sstevel@tonic-gate     struct probe_fail_count *pfinfo)
23610Sstevel@tonic-gate {
23620Sstevel@tonic-gate 	int	i;
23630Sstevel@tonic-gate 	struct probe_stats *pr_statp;
23640Sstevel@tonic-gate 	boolean_t	tg_found_success = _B_FALSE;
23650Sstevel@tonic-gate 	boolean_t	pi_found_success = _B_FALSE;
23660Sstevel@tonic-gate 	int	most_recent;
23670Sstevel@tonic-gate 	int	second_most_recent;
23680Sstevel@tonic-gate 	uint_t	now;
23690Sstevel@tonic-gate 	uint_t	timeout;
23700Sstevel@tonic-gate 	struct	target *tg;
23710Sstevel@tonic-gate 
23728485SPeter.Memishian@Sun.COM 	if (debug & D_FAILREP)
23730Sstevel@tonic-gate 		logdebug("probe_fail_info(%s)\n", pii->pii_name);
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	bzero(pfinfo, sizeof (*pfinfo));
23760Sstevel@tonic-gate 	now = getcurrenttime();
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	/*
23790Sstevel@tonic-gate 	 * Start with the most recent probe, and count the number
23800Sstevel@tonic-gate 	 * of consecutive probe failures. Latch the number of failures
23810Sstevel@tonic-gate 	 * on hitting a probe success.
23820Sstevel@tonic-gate 	 */
23830Sstevel@tonic-gate 	most_recent = PROBE_INDEX_PREV(pii->pii_probe_next);
23840Sstevel@tonic-gate 	second_most_recent = PROBE_INDEX_PREV(most_recent);
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	for (i = most_recent; i != pii->pii_probe_next;
23870Sstevel@tonic-gate 	    i = PROBE_INDEX_PREV(i)) {
23880Sstevel@tonic-gate 		pr_statp = &pii->pii_probes[i];
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 		assert(PR_STATUS_VALID(pr_statp->pr_status));
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 		switch (pr_statp->pr_status) {
23930Sstevel@tonic-gate 		case PR_UNACKED:
23940Sstevel@tonic-gate 			/*
23950Sstevel@tonic-gate 			 * Only the most recent 2 probes can be unacknowledged
23960Sstevel@tonic-gate 			 */
23970Sstevel@tonic-gate 			assert(i == most_recent || i == second_most_recent);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 			tg = pr_statp->pr_target;
24000Sstevel@tonic-gate 			/*
24010Sstevel@tonic-gate 			 * Target is guaranteed to exist in the unack. state
24020Sstevel@tonic-gate 			 */
24030Sstevel@tonic-gate 			assert(tg != NULL);
24040Sstevel@tonic-gate 			/*
24050Sstevel@tonic-gate 			 * The crtt could be zero for some reason,
24060Sstevel@tonic-gate 			 * Eg. the phyint could be failed. If the crtt is
24070Sstevel@tonic-gate 			 * not available use the group's probe interval,
24080Sstevel@tonic-gate 			 * which is a worst case estimate.
24090Sstevel@tonic-gate 			 */
24108485SPeter.Memishian@Sun.COM 			timeout = ns2ms(pr_statp->pr_hrtime_start);
24110Sstevel@tonic-gate 			if (tg->tg_crtt != 0) {
24128485SPeter.Memishian@Sun.COM 				timeout += tg->tg_crtt;
24130Sstevel@tonic-gate 			} else {
24148485SPeter.Memishian@Sun.COM 				timeout +=
24150Sstevel@tonic-gate 				    pii->pii_phyint->pi_group->pg_probeint;
24160Sstevel@tonic-gate 			}
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 			if (TIME_GT(timeout, now))
24190Sstevel@tonic-gate 				break;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 			pr_statp->pr_time_lost = timeout;
24228485SPeter.Memishian@Sun.COM 			probe_chstate(pr_statp, pii, PR_LOST);
24230Sstevel@tonic-gate 			/* FALLTHRU */
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 		case PR_LOST:
24260Sstevel@tonic-gate 			if (!pi_found_success) {
24270Sstevel@tonic-gate 				pfinfo->pf_nfail++;
24280Sstevel@tonic-gate 				pfinfo->pf_tff = pr_statp->pr_time_lost;
24290Sstevel@tonic-gate 			}
24300Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg &&
24310Sstevel@tonic-gate 			    !tg_found_success)  {
24320Sstevel@tonic-gate 				pfinfo->pf_nfail_tg++;
24330Sstevel@tonic-gate 			}
24340Sstevel@tonic-gate 			break;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 		default:
24370Sstevel@tonic-gate 			/*
24380Sstevel@tonic-gate 			 * We hit a success or unused slot. Latch the
24390Sstevel@tonic-gate 			 * total number of recent consecutive failures.
24400Sstevel@tonic-gate 			 */
24410Sstevel@tonic-gate 			pi_found_success = _B_TRUE;
24420Sstevel@tonic-gate 			if (cur_tg != NULL && pr_statp->pr_target == cur_tg) {
24430Sstevel@tonic-gate 				/*
24440Sstevel@tonic-gate 				 * We hit a success for the desired target.
24450Sstevel@tonic-gate 				 * Latch the number of recent consecutive
24460Sstevel@tonic-gate 				 * failures for this target
24470Sstevel@tonic-gate 				 */
24480Sstevel@tonic-gate 				tg_found_success = _B_TRUE;
24490Sstevel@tonic-gate 			}
24500Sstevel@tonic-gate 		}
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate }
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate /*
24558485SPeter.Memishian@Sun.COM  * Change the state of probe `pr' on phyint_instance `pii' to state `state'.
24568485SPeter.Memishian@Sun.COM  */
24578485SPeter.Memishian@Sun.COM void
24588485SPeter.Memishian@Sun.COM probe_chstate(struct probe_stats *pr, struct phyint_instance *pii, int state)
24598485SPeter.Memishian@Sun.COM {
24608485SPeter.Memishian@Sun.COM 	if (pr->pr_status == state)
24618485SPeter.Memishian@Sun.COM 		return;
24628485SPeter.Memishian@Sun.COM 
24638485SPeter.Memishian@Sun.COM 	pr->pr_status = state;
24648485SPeter.Memishian@Sun.COM 	(void) probe_state_event(pr, pii);
24658485SPeter.Memishian@Sun.COM }
24668485SPeter.Memishian@Sun.COM 
24678485SPeter.Memishian@Sun.COM /*
24680Sstevel@tonic-gate  * Check if the phyint has been repaired.  If no test address has been
24690Sstevel@tonic-gate  * configured, then consider the interface repaired if the link is up (unless
24700Sstevel@tonic-gate  * the link is flapping; see below).  Otherwise, look for proof of probes
24710Sstevel@tonic-gate  * being sent and received. If last NUM_PROBE_REPAIRS probes are fine on
24720Sstevel@tonic-gate  * either IPv4 or IPv6 instance, the phyint can be considered repaired.
24730Sstevel@tonic-gate  */
24740Sstevel@tonic-gate static boolean_t
24750Sstevel@tonic-gate phyint_repaired(struct phyint *pi)
24760Sstevel@tonic-gate {
24770Sstevel@tonic-gate 	struct	probe_success_count psinfo;
24780Sstevel@tonic-gate 	struct	phyint_instance *pii;
24790Sstevel@tonic-gate 	struct	target *cur_tg;
24800Sstevel@tonic-gate 	int	pr_ndx;
24810Sstevel@tonic-gate 	uint_t	cur_time;
24820Sstevel@tonic-gate 
24838485SPeter.Memishian@Sun.COM 	if (debug & D_FAILREP)
24840Sstevel@tonic-gate 		logdebug("phyint_repaired(%s)\n", pi->pi_name);
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	if (LINK_DOWN(pi))
24870Sstevel@tonic-gate 		return (_B_FALSE);
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	/*
24900Sstevel@tonic-gate 	 * If we don't have any test addresses and the link is up, then
24910Sstevel@tonic-gate 	 * consider the interface repaired, unless we've received more than
24920Sstevel@tonic-gate 	 * LINK_UP_PERMIN link up notifications in the last minute, in
24930Sstevel@tonic-gate 	 * which case we keep the link down until we drop back below
24940Sstevel@tonic-gate 	 * the threshold.
24950Sstevel@tonic-gate 	 */
24960Sstevel@tonic-gate 	if (!PROBE_ENABLED(pi->pi_v4) && !PROBE_ENABLED(pi->pi_v6)) {
24970Sstevel@tonic-gate 		cur_time = getcurrenttime();
24980Sstevel@tonic-gate 		if ((pi->pi_whenup[pi->pi_whendx] == 0 ||
24990Sstevel@tonic-gate 		    (cur_time - pi->pi_whenup[pi->pi_whendx]) > MSEC_PERMIN)) {
25000Sstevel@tonic-gate 			pi->pi_lfmsg_printed = 0;
25010Sstevel@tonic-gate 			return (_B_TRUE);
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 		if (!pi->pi_lfmsg_printed) {
25040Sstevel@tonic-gate 			logerr("The link has come up on %s more than %d times "
25058485SPeter.Memishian@Sun.COM 			    "in the last minute; disabling repair until it "
25060Sstevel@tonic-gate 			    "stabilizes\n", pi->pi_name, LINK_UP_PERMIN);
25070Sstevel@tonic-gate 			pi->pi_lfmsg_printed = 1;
25080Sstevel@tonic-gate 		}
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 		return (_B_FALSE);
25110Sstevel@tonic-gate 	}
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 	pii = pi->pi_v4;
25140Sstevel@tonic-gate 	if (PROBE_CAPABLE(pii)) {
25150Sstevel@tonic-gate 		pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
25160Sstevel@tonic-gate 		cur_tg = pii->pii_probes[pr_ndx].pr_target;
25170Sstevel@tonic-gate 		probe_success_info(pii, cur_tg, &psinfo);
25180Sstevel@tonic-gate 		if (psinfo.ps_nsucc >= NUM_PROBE_REPAIRS ||
25190Sstevel@tonic-gate 		    psinfo.ps_nsucc_tg >= NUM_PROBE_REPAIRS)
25200Sstevel@tonic-gate 			return (_B_TRUE);
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	pii = pi->pi_v6;
25240Sstevel@tonic-gate 	if (PROBE_CAPABLE(pii)) {
25250Sstevel@tonic-gate 		pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next);
25260Sstevel@tonic-gate 		cur_tg = pii->pii_probes[pr_ndx].pr_target;
25270Sstevel@tonic-gate 		probe_success_info(pii, cur_tg, &psinfo);
25280Sstevel@tonic-gate 		if (psinfo.ps_nsucc >= NUM_PROBE_REPAIRS ||
25290Sstevel@tonic-gate 		    psinfo.ps_nsucc_tg >= NUM_PROBE_REPAIRS)
25300Sstevel@tonic-gate 			return (_B_TRUE);
25310Sstevel@tonic-gate 	}
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	return (_B_FALSE);
25340Sstevel@tonic-gate }
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate /*
25370Sstevel@tonic-gate  * Used to set/clear phyint flags, by making a SIOCSLIFFLAGS call.
25380Sstevel@tonic-gate  */
25390Sstevel@tonic-gate boolean_t
25408485SPeter.Memishian@Sun.COM change_pif_flags(struct phyint *pi, uint64_t set, uint64_t clear)
25410Sstevel@tonic-gate {
25420Sstevel@tonic-gate 	int ifsock;
25430Sstevel@tonic-gate 	struct lifreq lifr;
25444929Srk129064 	uint64_t old_flags;
25450Sstevel@tonic-gate 
25468485SPeter.Memishian@Sun.COM 	if (debug & D_FAILREP) {
25478485SPeter.Memishian@Sun.COM 		logdebug("change_pif_flags(%s): set %llx clear %llx\n",
25488485SPeter.Memishian@Sun.COM 		    pi->pi_name, set, clear);
25490Sstevel@tonic-gate 	}
25500Sstevel@tonic-gate 
25518485SPeter.Memishian@Sun.COM 	if (pi->pi_v4 != NULL)
25520Sstevel@tonic-gate 		ifsock = ifsock_v4;
25538485SPeter.Memishian@Sun.COM 	else
25540Sstevel@tonic-gate 		ifsock = ifsock_v6;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	/*
25570Sstevel@tonic-gate 	 * Get the current flags from the kernel, and set/clear the
25580Sstevel@tonic-gate 	 * desired phyint flags. Since we set only phyint flags, we can
25590Sstevel@tonic-gate 	 * do it on either IPv4 or IPv6 instance.
25600Sstevel@tonic-gate 	 */
25618485SPeter.Memishian@Sun.COM 	(void) strlcpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
25628485SPeter.Memishian@Sun.COM 
25630Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
25640Sstevel@tonic-gate 		if (errno != ENXIO)
25658485SPeter.Memishian@Sun.COM 			logperror("change_pif_flags: ioctl (get flags)");
25660Sstevel@tonic-gate 		return (_B_FALSE);
25670Sstevel@tonic-gate 	}
25684929Srk129064 
25694929Srk129064 	old_flags = lifr.lifr_flags;
25708485SPeter.Memishian@Sun.COM 	lifr.lifr_flags |= set;
25718485SPeter.Memishian@Sun.COM 	lifr.lifr_flags &= ~clear;
25724929Srk129064 
25734929Srk129064 	if (old_flags == lifr.lifr_flags) {
25744929Srk129064 		/* No change in the flags. No need to send ioctl */
25754929Srk129064 		return (_B_TRUE);
25764929Srk129064 	}
25774929Srk129064 
25780Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCSLIFFLAGS, (char *)&lifr) < 0) {
25790Sstevel@tonic-gate 		if (errno != ENXIO)
25808485SPeter.Memishian@Sun.COM 			logperror("change_pif_flags: ioctl (set flags)");
25810Sstevel@tonic-gate 		return (_B_FALSE);
25820Sstevel@tonic-gate 	}
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 	/*
25850Sstevel@tonic-gate 	 * Keep pi_flags in synch. with actual flags. Assumes flags are
25860Sstevel@tonic-gate 	 * phyint flags.
25870Sstevel@tonic-gate 	 */
25888485SPeter.Memishian@Sun.COM 	pi->pi_flags |= set;
25898485SPeter.Memishian@Sun.COM 	pi->pi_flags &= ~clear;
25900Sstevel@tonic-gate 
25918485SPeter.Memishian@Sun.COM 	if (pi->pi_v4 != NULL)
25920Sstevel@tonic-gate 		pi->pi_v4->pii_flags = pi->pi_flags;
25930Sstevel@tonic-gate 
25948485SPeter.Memishian@Sun.COM 	if (pi->pi_v6 != NULL)
25950Sstevel@tonic-gate 		pi->pi_v6->pii_flags = pi->pi_flags;
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	return (_B_TRUE);
25980Sstevel@tonic-gate }
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate /*
26010Sstevel@tonic-gate  * icmp cksum computation for IPv4.
26020Sstevel@tonic-gate  */
26030Sstevel@tonic-gate static int
26040Sstevel@tonic-gate in_cksum(ushort_t *addr, int len)
26050Sstevel@tonic-gate {
26060Sstevel@tonic-gate 	register int nleft = len;
26070Sstevel@tonic-gate 	register ushort_t *w = addr;
26080Sstevel@tonic-gate 	register ushort_t answer;
26090Sstevel@tonic-gate 	ushort_t odd_byte = 0;
26100Sstevel@tonic-gate 	register int sum = 0;
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	/*
26130Sstevel@tonic-gate 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
26140Sstevel@tonic-gate 	 *  we add sequential 16 bit words to it, and at the end, fold
26150Sstevel@tonic-gate 	 *  back all the carry bits from the top 16 bits into the lower
26160Sstevel@tonic-gate 	 *  16 bits.
26170Sstevel@tonic-gate 	 */
26180Sstevel@tonic-gate 	while (nleft > 1)  {
26190Sstevel@tonic-gate 		sum += *w++;
26200Sstevel@tonic-gate 		nleft -= 2;
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 	/* mop up an odd byte, if necessary */
26240Sstevel@tonic-gate 	if (nleft == 1) {
26250Sstevel@tonic-gate 		*(uchar_t *)(&odd_byte) = *(uchar_t *)w;
26260Sstevel@tonic-gate 		sum += odd_byte;
26270Sstevel@tonic-gate 	}
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 	/*
26300Sstevel@tonic-gate 	 * add back carry outs from top 16 bits to low 16 bits
26310Sstevel@tonic-gate 	 */
26320Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
26330Sstevel@tonic-gate 	sum += (sum >> 16);			/* add carry */
26340Sstevel@tonic-gate 	answer = ~sum;				/* truncate to 16 bits */
26350Sstevel@tonic-gate 	return (answer);
26360Sstevel@tonic-gate }
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate static void
26390Sstevel@tonic-gate reset_snxt_basetimes(void)
26400Sstevel@tonic-gate {
26410Sstevel@tonic-gate 	struct phyint_instance *pii;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
26440Sstevel@tonic-gate 		pii->pii_fd_snxt_basetime = pii->pii_snxt_basetime;
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate }
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate /*
26490Sstevel@tonic-gate  * Is the address one of our own addresses? Unfortunately,
26500Sstevel@tonic-gate  * we cannot check our phyint tables to determine if the address
26510Sstevel@tonic-gate  * is our own. This is because, we don't track interfaces that
26520Sstevel@tonic-gate  * are not part of any group. We have to either use a 'bind' or
26530Sstevel@tonic-gate  * get the complete list of all interfaces using SIOCGLIFCONF,
26542250Srk129064  * to do this check. We could also use SIOCTMYADDR.
26552250Srk129064  * Bind fails for the local zone address, so we might include local zone
26562250Srk129064  * address as target address. If local zone address is a target address
26572250Srk129064  * and it is up, it is not possible to detect the interface failure.
26582250Srk129064  * SIOCTMYADDR also doesn't consider local zone address as own address.
26592250Srk129064  * So, we choose to use SIOCGLIFCONF to collect the local addresses, and they
26608485SPeter.Memishian@Sun.COM  * are stored in `localaddrs'
26610Sstevel@tonic-gate  */
26622250Srk129064 boolean_t
26632250Srk129064 own_address(struct in6_addr addr)
26642250Srk129064 {
26658485SPeter.Memishian@Sun.COM 	addrlist_t *addrp;
26668485SPeter.Memishian@Sun.COM 	struct sockaddr_storage ss;
26678485SPeter.Memishian@Sun.COM 	int af = IN6_IS_ADDR_V4MAPPED(&addr) ? AF_INET : AF_INET6;
26682250Srk129064 
26698485SPeter.Memishian@Sun.COM 	addr2storage(af, &addr, &ss);
26708485SPeter.Memishian@Sun.COM 	for (addrp = localaddrs; addrp != NULL; addrp = addrp->al_next) {
26718485SPeter.Memishian@Sun.COM 		if (sockaddrcmp(&ss, &addrp->al_addr))
26722250Srk129064 			return (_B_TRUE);
26730Sstevel@tonic-gate 	}
26742250Srk129064 	return (_B_FALSE);
26750Sstevel@tonic-gate }
26768485SPeter.Memishian@Sun.COM 
26778485SPeter.Memishian@Sun.COM static int
26788485SPeter.Memishian@Sun.COM ns2ms(int64_t ns)
26798485SPeter.Memishian@Sun.COM {
26808485SPeter.Memishian@Sun.COM 	return (ns / (NANOSEC / MILLISEC));
26818485SPeter.Memishian@Sun.COM }
26828485SPeter.Memishian@Sun.COM 
26838485SPeter.Memishian@Sun.COM static int64_t
26848485SPeter.Memishian@Sun.COM tv2ns(struct timeval *tvp)
26858485SPeter.Memishian@Sun.COM {
26868485SPeter.Memishian@Sun.COM 	return (tvp->tv_sec * NANOSEC + tvp->tv_usec * 1000);
26878485SPeter.Memishian@Sun.COM }
2688