xref: /onnv-gate/usr/src/cmd/mdb/common/modules/ip/ip.c (revision 5023:e0c678e511a7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52546Scarlsonj  * Common Development and Distribution License (the "License").
62546Scarlsonj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/stropts.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <sys/avl_impl.h>
330Sstevel@tonic-gate #include <net/if.h>
340Sstevel@tonic-gate #include <net/route.h>
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #include <netinet/ip6.h>
370Sstevel@tonic-gate #include <netinet/udp.h>
380Sstevel@tonic-gate #include <netinet/sctp.h>
390Sstevel@tonic-gate #include <inet/mib2.h>
400Sstevel@tonic-gate #include <inet/common.h>
410Sstevel@tonic-gate #include <inet/ip.h>
420Sstevel@tonic-gate #include <inet/ip_ire.h>
430Sstevel@tonic-gate #include <inet/ip6.h>
440Sstevel@tonic-gate #include <inet/ipclassifier.h>
450Sstevel@tonic-gate #include <inet/mi.h>
460Sstevel@tonic-gate #include <sys/squeue_impl.h>
47*5023Scarlsonj #include <sys/modhash_impl.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
500Sstevel@tonic-gate #include <mdb/mdb_ks.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	ADDR_WIDTH 11
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef struct {
550Sstevel@tonic-gate 	const char *bit_name;	/* name of bit */
560Sstevel@tonic-gate 	const char *bit_descr;	/* description of bit's purpose */
570Sstevel@tonic-gate } bitname_t;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static const bitname_t squeue_states[] = {
600Sstevel@tonic-gate 	{ "SQS_PROC",		"being processed" },
610Sstevel@tonic-gate 	{ "SQS_WORKER",		"... by a worker thread" },
620Sstevel@tonic-gate 	{ "SQS_ENTER",		"... by an squeue_enter() thread" },
630Sstevel@tonic-gate 	{ "SQS_FAST",		"... in fast-path mode" },
640Sstevel@tonic-gate 	{ "SQS_USER", 		"A non interrupt user" },
650Sstevel@tonic-gate 	{ "SQS_BOUND",		"worker thread bound to CPU" },
660Sstevel@tonic-gate 	{ "SQS_PROFILE",	"profiling enabled" },
670Sstevel@tonic-gate 	{ "SQS_REENTER",	"re-entered thred" },
680Sstevel@tonic-gate 	{ NULL }
690Sstevel@tonic-gate };
700Sstevel@tonic-gate 
710Sstevel@tonic-gate typedef struct illif_walk_data {
720Sstevel@tonic-gate 	ill_g_head_t ill_g_heads[MAX_G_HEADS];
730Sstevel@tonic-gate 	int ill_list;
740Sstevel@tonic-gate 	ill_if_t ill_if;
750Sstevel@tonic-gate } illif_walk_data_t;
760Sstevel@tonic-gate 
77*5023Scarlsonj typedef struct th_walk_data {
78*5023Scarlsonj 	uint_t		thw_non_zero_only;
79*5023Scarlsonj 	boolean_t	thw_match;
80*5023Scarlsonj 	uintptr_t	thw_matchkey;
81*5023Scarlsonj 	uintptr_t	thw_ipst;
82*5023Scarlsonj 	clock_t		thw_lbolt;
83*5023Scarlsonj } th_walk_data_t;
84*5023Scarlsonj 
850Sstevel@tonic-gate static int iphdr(uintptr_t, uint_t, int, const mdb_arg_t *);
860Sstevel@tonic-gate static int ip6hdr(uintptr_t, uint_t, int, const mdb_arg_t *);
870Sstevel@tonic-gate 
883448Sdh155122 static int ire_format(uintptr_t addr, const ire_t *irep, uint_t *verbose);
893448Sdh155122 
903448Sdh155122 /*
913448Sdh155122  * Given the kernel address of an ip_stack_t, return the stackid
923448Sdh155122  */
933448Sdh155122 static int
943448Sdh155122 ips_to_stackid(uintptr_t kaddr)
953448Sdh155122 {
963448Sdh155122 	ip_stack_t ipss;
973448Sdh155122 	netstack_t nss;
983448Sdh155122 
993448Sdh155122 	if (mdb_vread(&ipss, sizeof (ipss), kaddr) == -1) {
1003448Sdh155122 		mdb_warn("failed to read ip_stack_t %p", kaddr);
1013448Sdh155122 		return (0);
1023448Sdh155122 	}
1033448Sdh155122 	kaddr = (uintptr_t)ipss.ips_netstack;
1043448Sdh155122 	if (mdb_vread(&nss, sizeof (nss), kaddr) == -1) {
1053448Sdh155122 		mdb_warn("failed to read netstack_t %p", kaddr);
1063448Sdh155122 		return (0);
1073448Sdh155122 	}
1083448Sdh155122 	return (nss.netstack_stackid);
1093448Sdh155122 }
1103448Sdh155122 
1110Sstevel@tonic-gate int
1123448Sdh155122 ip_stacks_walk_init(mdb_walk_state_t *wsp)
1133448Sdh155122 {
1143448Sdh155122 	if (mdb_layered_walk("netstack", wsp) == -1) {
1153448Sdh155122 		mdb_warn("can't walk 'netstack'");
1163448Sdh155122 		return (WALK_ERR);
1173448Sdh155122 	}
1183448Sdh155122 	return (WALK_NEXT);
1193448Sdh155122 }
1203448Sdh155122 
1213448Sdh155122 int
1223448Sdh155122 ip_stacks_walk_step(mdb_walk_state_t *wsp)
1233448Sdh155122 {
1243448Sdh155122 	uintptr_t kaddr;
1253448Sdh155122 	netstack_t nss;
1263448Sdh155122 
1273448Sdh155122 #ifdef DEBUG
1283448Sdh155122 	mdb_printf("DEBUG: ip_stacks_walk_step: addr %p\n", wsp->walk_addr);
1293448Sdh155122 #endif
1303448Sdh155122 	if (mdb_vread(&nss, sizeof (nss), wsp->walk_addr) == -1) {
1313448Sdh155122 		mdb_warn("can't read netstack at %p", wsp->walk_addr);
1323448Sdh155122 		return (WALK_ERR);
1333448Sdh155122 	}
1343448Sdh155122 	kaddr = (uintptr_t)nss.netstack_modules[NS_IP];
1353448Sdh155122 
1363448Sdh155122 #ifdef DEBUG
1373448Sdh155122 	mdb_printf("DEBUG: ip_stacks_walk_step: ip_stack_t at %p\n", kaddr);
1383448Sdh155122 #endif
1393448Sdh155122 	return (wsp->walk_callback(kaddr, wsp->walk_layer, wsp->walk_cbdata));
1403448Sdh155122 }
1413448Sdh155122 
142*5023Scarlsonj int
143*5023Scarlsonj th_hash_walk_init(mdb_walk_state_t *wsp)
144*5023Scarlsonj {
145*5023Scarlsonj 	GElf_Sym sym;
146*5023Scarlsonj 	list_node_t *next;
147*5023Scarlsonj 
148*5023Scarlsonj 	if (wsp->walk_addr == NULL) {
149*5023Scarlsonj 		if (mdb_lookup_by_obj("ip", "ip_thread_list", &sym) == 0) {
150*5023Scarlsonj 			wsp->walk_addr = sym.st_value;
151*5023Scarlsonj 		} else {
152*5023Scarlsonj 			mdb_warn("unable to locate ip_thread_list\n");
153*5023Scarlsonj 			return (WALK_ERR);
154*5023Scarlsonj 		}
155*5023Scarlsonj 	}
156*5023Scarlsonj 
157*5023Scarlsonj 	if (mdb_vread(&next, sizeof (next),
158*5023Scarlsonj 	    wsp->walk_addr + offsetof(list_t, list_head) +
159*5023Scarlsonj 	    offsetof(list_node_t, list_next)) == -1 ||
160*5023Scarlsonj 	    next == NULL) {
161*5023Scarlsonj 		mdb_warn("non-DEBUG image; cannot walk th_hash list\n");
162*5023Scarlsonj 		return (WALK_ERR);
163*5023Scarlsonj 	}
164*5023Scarlsonj 
165*5023Scarlsonj 	if (mdb_layered_walk("list", wsp) == -1) {
166*5023Scarlsonj 		mdb_warn("can't walk 'list'");
167*5023Scarlsonj 		return (WALK_ERR);
168*5023Scarlsonj 	} else {
169*5023Scarlsonj 		return (WALK_NEXT);
170*5023Scarlsonj 	}
171*5023Scarlsonj }
172*5023Scarlsonj 
173*5023Scarlsonj int
174*5023Scarlsonj th_hash_walk_step(mdb_walk_state_t *wsp)
175*5023Scarlsonj {
176*5023Scarlsonj 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
177*5023Scarlsonj 	    wsp->walk_cbdata));
178*5023Scarlsonj }
179*5023Scarlsonj 
1803448Sdh155122 /*
1813448Sdh155122  * Called with walk_addr being the address of ips_ill_g_heads
1823448Sdh155122  */
1833448Sdh155122 int
1843448Sdh155122 illif_stack_walk_init(mdb_walk_state_t *wsp)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	illif_walk_data_t *iw;
1870Sstevel@tonic-gate 
1883448Sdh155122 	if (wsp->walk_addr == NULL) {
1893448Sdh155122 		mdb_warn("illif_stack supports only local walks\n");
1900Sstevel@tonic-gate 		return (WALK_ERR);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	iw = mdb_alloc(sizeof (illif_walk_data_t), UM_SLEEP);
1940Sstevel@tonic-gate 
1953448Sdh155122 	if (mdb_vread(iw->ill_g_heads, MAX_G_HEADS * sizeof (ill_g_head_t),
1963448Sdh155122 	    wsp->walk_addr) == -1) {
1973448Sdh155122 		mdb_warn("failed to read 'ips_ill_g_heads' at %p",
1983448Sdh155122 		    wsp->walk_addr);
1990Sstevel@tonic-gate 		mdb_free(iw, sizeof (illif_walk_data_t));
2000Sstevel@tonic-gate 		return (WALK_ERR);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	iw->ill_list = 0;
2043448Sdh155122 	wsp->walk_addr = (uintptr_t)iw->ill_g_heads[0].ill_g_list_head;
2050Sstevel@tonic-gate 	wsp->walk_data = iw;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	return (WALK_NEXT);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate int
2113448Sdh155122 illif_stack_walk_step(mdb_walk_state_t *wsp)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
2140Sstevel@tonic-gate 	illif_walk_data_t *iw = wsp->walk_data;
2150Sstevel@tonic-gate 	int list = iw->ill_list;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (mdb_vread(&iw->ill_if, sizeof (ill_if_t), addr) == -1) {
2180Sstevel@tonic-gate 		mdb_warn("failed to read ill_if_t at %p", addr);
2190Sstevel@tonic-gate 		return (WALK_ERR);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)iw->ill_if.illif_next;
2230Sstevel@tonic-gate 
2243448Sdh155122 	if (wsp->walk_addr ==
2253448Sdh155122 	    (uintptr_t)iw->ill_g_heads[list].ill_g_list_head) {
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		if (++list >= MAX_G_HEADS)
2280Sstevel@tonic-gate 			return (WALK_DONE);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		iw->ill_list = list;
2313448Sdh155122 		wsp->walk_addr =
2323448Sdh155122 		    (uintptr_t)iw->ill_g_heads[list].ill_g_list_head;
2330Sstevel@tonic-gate 		return (WALK_NEXT);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	return (wsp->walk_callback(addr, iw, wsp->walk_cbdata));
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate void
2403448Sdh155122 illif_stack_walk_fini(mdb_walk_state_t *wsp)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (illif_walk_data_t));
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate typedef struct illif_cbdata {
2460Sstevel@tonic-gate 	uint_t ill_flags;
2470Sstevel@tonic-gate 	uintptr_t ill_addr;
2480Sstevel@tonic-gate 	int ill_printlist;	/* list to be printed (MAX_G_HEADS for all) */
2490Sstevel@tonic-gate 	boolean_t ill_printed;
2500Sstevel@tonic-gate } illif_cbdata_t;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate static int
2530Sstevel@tonic-gate illif_cb(uintptr_t addr, const illif_walk_data_t *iw, illif_cbdata_t *id)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	const char *version;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (id->ill_printlist < MAX_G_HEADS &&
2580Sstevel@tonic-gate 	    id->ill_printlist != iw->ill_list)
2590Sstevel@tonic-gate 		return (WALK_NEXT);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (id->ill_flags & DCMD_ADDRSPEC && id->ill_addr != addr)
2620Sstevel@tonic-gate 		return (WALK_NEXT);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (id->ill_flags & DCMD_PIPE_OUT) {
2650Sstevel@tonic-gate 		mdb_printf("%p\n", addr);
2660Sstevel@tonic-gate 		return (WALK_NEXT);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	switch (iw->ill_list) {
2700Sstevel@tonic-gate 		case IP_V4_G_HEAD:	version = "v4";	break;
2710Sstevel@tonic-gate 		case IP_V6_G_HEAD:	version = "v6";	break;
2720Sstevel@tonic-gate 		default:		version = "??"; break;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	mdb_printf("%?p %2s %?p %10d %?p %s\n",
2760Sstevel@tonic-gate 	    addr, version, addr + offsetof(ill_if_t, illif_avl_by_ppa),
2770Sstevel@tonic-gate 	    iw->ill_if.illif_avl_by_ppa.avl_numnodes,
2780Sstevel@tonic-gate 	    iw->ill_if.illif_ppa_arena, iw->ill_if.illif_name);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	id->ill_printed = TRUE;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	return (WALK_NEXT);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate int
2863448Sdh155122 illif_walk_init(mdb_walk_state_t *wsp)
2873448Sdh155122 {
2883448Sdh155122 	if (mdb_layered_walk("ip_stacks", wsp) == -1) {
2893448Sdh155122 		mdb_warn("can't walk 'ip_stacks'");
2903448Sdh155122 		return (WALK_ERR);
2913448Sdh155122 	}
2923448Sdh155122 
2933448Sdh155122 	return (WALK_NEXT);
2943448Sdh155122 }
2953448Sdh155122 
2963448Sdh155122 int
2973448Sdh155122 illif_walk_step(mdb_walk_state_t *wsp)
2983448Sdh155122 {
2993448Sdh155122 	uintptr_t kaddr;
3003448Sdh155122 
3013448Sdh155122 #ifdef DEBUG
3023448Sdh155122 	mdb_printf("DEBUG: illif_walk_step: addr %p\n", wsp->walk_addr);
3033448Sdh155122 #endif
3043448Sdh155122 
3053448Sdh155122 	kaddr = wsp->walk_addr + OFFSETOF(ip_stack_t, ips_ill_g_heads);
3063448Sdh155122 
3073448Sdh155122 	if (mdb_vread(&kaddr, sizeof (kaddr), kaddr) == -1) {
3083448Sdh155122 		mdb_warn("can't read ips_ip_cache_table at %p", kaddr);
3093448Sdh155122 		return (WALK_ERR);
3103448Sdh155122 	}
3113448Sdh155122 #ifdef DEBUG
3123448Sdh155122 	mdb_printf("DEBUG: illif_walk_step: ips_ill_g_heads %p\n", kaddr);
3133448Sdh155122 #endif
3143448Sdh155122 
3153448Sdh155122 	if (mdb_pwalk("illif_stack", wsp->walk_callback,
316*5023Scarlsonj 	    wsp->walk_cbdata, kaddr) == -1) {
3173448Sdh155122 		mdb_warn("couldn't walk 'illif_stack' for ips_ill_g_heads %p",
3183448Sdh155122 		    kaddr);
3193448Sdh155122 		return (WALK_ERR);
3203448Sdh155122 	}
3213448Sdh155122 	return (WALK_NEXT);
3223448Sdh155122 }
3233448Sdh155122 
3243448Sdh155122 int
3250Sstevel@tonic-gate illif(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	illif_cbdata_t id;
3280Sstevel@tonic-gate 	ill_if_t ill_if;
3290Sstevel@tonic-gate 	const char *opt_P = NULL;
3300Sstevel@tonic-gate 	int printlist = MAX_G_HEADS;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
3330Sstevel@tonic-gate 	    'P', MDB_OPT_STR, &opt_P, NULL) != argc)
3340Sstevel@tonic-gate 		return (DCMD_USAGE);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if (opt_P != NULL) {
3370Sstevel@tonic-gate 		if (strcmp("v4", opt_P) == 0) {
3380Sstevel@tonic-gate 			printlist = IP_V4_G_HEAD;
3390Sstevel@tonic-gate 		} else if (strcmp("v6", opt_P) == 0) {
3400Sstevel@tonic-gate 			printlist = IP_V6_G_HEAD;
3410Sstevel@tonic-gate 		} else {
3420Sstevel@tonic-gate 			mdb_warn("invalid protocol '%s'\n", opt_P);
3430Sstevel@tonic-gate 			return (DCMD_USAGE);
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && (flags & DCMD_PIPE_OUT) == 0) {
3480Sstevel@tonic-gate 		mdb_printf("%<u>%?s %2s %?s %10s %?s %-10s%</u>\n",
3490Sstevel@tonic-gate 		    "ADDR", "IP", "AVLADDR", "NUMNODES", "ARENA", "NAME");
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	id.ill_flags = flags;
3530Sstevel@tonic-gate 	id.ill_addr = addr;
3540Sstevel@tonic-gate 	id.ill_printlist = printlist;
3550Sstevel@tonic-gate 	id.ill_printed = FALSE;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if (mdb_walk("illif", (mdb_walk_cb_t)illif_cb, &id) == -1) {
3580Sstevel@tonic-gate 		mdb_warn("can't walk ill_if_t structures");
3590Sstevel@tonic-gate 		return (DCMD_ERR);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || opt_P != NULL || id.ill_printed)
3630Sstevel@tonic-gate 		return (DCMD_OK);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * If an address is specified and the walk doesn't find it,
3670Sstevel@tonic-gate 	 * print it anyway.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	if (mdb_vread(&ill_if, sizeof (ill_if_t), addr) == -1) {
3700Sstevel@tonic-gate 		mdb_warn("failed to read ill_if_t at %p", addr);
3710Sstevel@tonic-gate 		return (DCMD_ERR);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	mdb_printf("%?p %2s %?p %10d %?p %s\n",
3750Sstevel@tonic-gate 	    addr, "??", addr + offsetof(ill_if_t, illif_avl_by_ppa),
3760Sstevel@tonic-gate 	    ill_if.illif_avl_by_ppa.avl_numnodes,
3770Sstevel@tonic-gate 	    ill_if.illif_ppa_arena, ill_if.illif_name);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	return (DCMD_OK);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate static void
3830Sstevel@tonic-gate illif_help(void)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	mdb_printf("Options:\n");
3860Sstevel@tonic-gate 	mdb_printf("\t-P v4 | v6"
3870Sstevel@tonic-gate 	    "\tfilter interface structures for the specified protocol\n");
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate int
3910Sstevel@tonic-gate ire_walk_init(mdb_walk_state_t *wsp)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	if (mdb_layered_walk("ire_cache", wsp) == -1) {
3940Sstevel@tonic-gate 		mdb_warn("can't walk 'ire_cache'");
3950Sstevel@tonic-gate 		return (WALK_ERR);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	return (WALK_NEXT);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate int
4020Sstevel@tonic-gate ire_walk_step(mdb_walk_state_t *wsp)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	ire_t ire;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (mdb_vread(&ire, sizeof (ire), wsp->walk_addr) == -1) {
4070Sstevel@tonic-gate 		mdb_warn("can't read ire at %p", wsp->walk_addr);
4080Sstevel@tonic-gate 		return (WALK_ERR);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, &ire, wsp->walk_cbdata));
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4143448Sdh155122 int
4153448Sdh155122 ire_ctable_walk_init(mdb_walk_state_t *wsp)
4163448Sdh155122 {
4173448Sdh155122 	if (mdb_layered_walk("ip_stacks", wsp) == -1) {
4183448Sdh155122 		mdb_warn("can't walk 'ip_stacks'");
4193448Sdh155122 		return (WALK_ERR);
4203448Sdh155122 	}
4213448Sdh155122 
4223448Sdh155122 	return (WALK_NEXT);
4233448Sdh155122 }
4243448Sdh155122 
4253448Sdh155122 int
4263448Sdh155122 ire_ctable_walk_step(mdb_walk_state_t *wsp)
4273448Sdh155122 {
4283448Sdh155122 	uintptr_t kaddr;
4293448Sdh155122 	irb_t *irb;
4303448Sdh155122 	int verbose = 0;
4313448Sdh155122 	uint32_t cache_table_size;
4323448Sdh155122 	int i;
4333448Sdh155122 
4343448Sdh155122 #ifdef DEBUG
4353448Sdh155122 	mdb_printf("DEBUG: ire_ctable_walk_step: addr %p\n", wsp->walk_addr);
4363448Sdh155122 #endif
4373448Sdh155122 
4383448Sdh155122 	kaddr = wsp->walk_addr + OFFSETOF(ip_stack_t, ips_ip_cache_table_size);
4393448Sdh155122 
4403448Sdh155122 	if (mdb_vread(&cache_table_size, sizeof (uint32_t), kaddr) == -1) {
4413448Sdh155122 		mdb_warn("can't read ips_ip_cache_table at %p", kaddr);
4423448Sdh155122 		return (WALK_ERR);
4433448Sdh155122 	}
4443448Sdh155122 #ifdef DEBUG
4453448Sdh155122 	mdb_printf("DEBUG: ire_ctable_walk_step: ips_ip_cache_table_size %u\n",
446*5023Scarlsonj 	    cache_table_size);
4473448Sdh155122 #endif
4483448Sdh155122 
4493448Sdh155122 	kaddr = wsp->walk_addr + OFFSETOF(ip_stack_t, ips_ip_cache_table);
4503448Sdh155122 	if (mdb_vread(&kaddr, sizeof (kaddr), kaddr) == -1) {
4513448Sdh155122 		mdb_warn("can't read ips_ip_cache_table at %p", kaddr);
4523448Sdh155122 		return (WALK_ERR);
4533448Sdh155122 	}
4543448Sdh155122 #ifdef DEBUG
4553448Sdh155122 	mdb_printf("DEBUG: ire_ctable_walk_step: ips_ip_cache_table %p\n",
4563448Sdh155122 	    kaddr);
4573448Sdh155122 #endif
4583448Sdh155122 
4593448Sdh155122 	irb = mdb_alloc(sizeof (irb_t) * cache_table_size, UM_SLEEP|UM_GC);
4603448Sdh155122 	if (mdb_vread(irb, sizeof (irb_t) * cache_table_size, kaddr) == -1) {
4613448Sdh155122 		mdb_warn("can't read irb at %p", kaddr);
4623448Sdh155122 		return (WALK_ERR);
4633448Sdh155122 	}
4643448Sdh155122 	for (i = 0; i < cache_table_size; i++) {
4653448Sdh155122 		kaddr = (uintptr_t)irb[i].irb_ire;
4663448Sdh155122 #ifdef DEBUG
4673448Sdh155122 		mdb_printf("DEBUG: ire_ctable_walk_step: %d ire %p\n",
4683448Sdh155122 		    i, kaddr);
4693448Sdh155122 #endif
4703448Sdh155122 
4713448Sdh155122 		if (mdb_pwalk("ire_next", (mdb_walk_cb_t)ire_format, &verbose,
472*5023Scarlsonj 		    kaddr) == -1) {
4733448Sdh155122 			mdb_warn("can't walk 'ire_next' for ire %p", kaddr);
4743448Sdh155122 			return (WALK_ERR);
4753448Sdh155122 		}
4763448Sdh155122 	}
4773448Sdh155122 	return (WALK_NEXT);
4783448Sdh155122 }
4793448Sdh155122 
4803448Sdh155122 /* ARGSUSED */
4813448Sdh155122 int
4823448Sdh155122 ire_next_walk_init(mdb_walk_state_t *wsp)
4833448Sdh155122 {
4843448Sdh155122 #ifdef DEBUG
4853448Sdh155122 	mdb_printf("DEBUG: ire_next_walk_init: addr %p\n", wsp->walk_addr);
4863448Sdh155122 #endif
4873448Sdh155122 	return (WALK_NEXT);
4883448Sdh155122 }
4893448Sdh155122 
4903448Sdh155122 int
4913448Sdh155122 ire_next_walk_step(mdb_walk_state_t *wsp)
4923448Sdh155122 {
4933448Sdh155122 	ire_t ire;
4943448Sdh155122 	int status;
4953448Sdh155122 
4963448Sdh155122 #ifdef DEBUG
4973448Sdh155122 	mdb_printf("DEBUG: ire_next_walk_step: addr %p\n", wsp->walk_addr);
4983448Sdh155122 #endif
4993448Sdh155122 
5003448Sdh155122 	if (wsp->walk_addr == NULL)
5013448Sdh155122 		return (WALK_DONE);
5023448Sdh155122 
5033448Sdh155122 	if (mdb_vread(&ire, sizeof (ire), wsp->walk_addr) == -1) {
5043448Sdh155122 		mdb_warn("can't read ire at %p", wsp->walk_addr);
5053448Sdh155122 		return (WALK_ERR);
5063448Sdh155122 	}
5073448Sdh155122 	status = wsp->walk_callback(wsp->walk_addr, &ire,
5083448Sdh155122 	    wsp->walk_cbdata);
5093448Sdh155122 
5103448Sdh155122 	if (status != WALK_NEXT)
5113448Sdh155122 		return (status);
5123448Sdh155122 
5133448Sdh155122 	wsp->walk_addr = (uintptr_t)ire.ire_next;
5143448Sdh155122 #ifdef DEBUG
5153448Sdh155122 	mdb_printf("DEBUG: ire_ctable_walk_step: next %p\n", wsp->walk_addr);
5163448Sdh155122 #endif
5173448Sdh155122 	return (status);
5183448Sdh155122 }
5193448Sdh155122 
5200Sstevel@tonic-gate static int
5210Sstevel@tonic-gate ire_format(uintptr_t addr, const ire_t *irep, uint_t *verbose)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	static const mdb_bitmask_t tmasks[] = {
5240Sstevel@tonic-gate 		{ "BROADCAST",	IRE_BROADCAST,		IRE_BROADCAST	},
5250Sstevel@tonic-gate 		{ "DEFAULT",	IRE_DEFAULT,		IRE_DEFAULT	},
5260Sstevel@tonic-gate 		{ "LOCAL",	IRE_LOCAL,		IRE_LOCAL	},
5270Sstevel@tonic-gate 		{ "LOOPBACK",	IRE_LOOPBACK,		IRE_LOOPBACK	},
5280Sstevel@tonic-gate 		{ "PREFIX",	IRE_PREFIX,		IRE_PREFIX	},
5290Sstevel@tonic-gate 		{ "CACHE",	IRE_CACHE,		IRE_CACHE	},
5300Sstevel@tonic-gate 		{ "IF_NORESOLVER", IRE_IF_NORESOLVER,	IRE_IF_NORESOLVER },
5310Sstevel@tonic-gate 		{ "IF_RESOLVER", IRE_IF_RESOLVER,	IRE_IF_RESOLVER	},
5320Sstevel@tonic-gate 		{ "HOST",	IRE_HOST,		IRE_HOST	},
5330Sstevel@tonic-gate 		{ "HOST_REDIRECT", IRE_HOST_REDIRECT,	IRE_HOST_REDIRECT },
5340Sstevel@tonic-gate 		{ NULL,		0,			0		}
5350Sstevel@tonic-gate 	};
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	static const mdb_bitmask_t mmasks[] = {
5380Sstevel@tonic-gate 		{ "CONDEMNED",	IRE_MARK_CONDEMNED,	IRE_MARK_CONDEMNED },
5390Sstevel@tonic-gate 		{ "NORECV",	IRE_MARK_NORECV,	IRE_MARK_NORECV	},
5400Sstevel@tonic-gate 		{ "HIDDEN",	IRE_MARK_HIDDEN,	IRE_MARK_HIDDEN	},
5410Sstevel@tonic-gate 		{ "NOADD",	IRE_MARK_NOADD,		IRE_MARK_NOADD	},
5420Sstevel@tonic-gate 		{ "TEMPORARY",	IRE_MARK_TEMPORARY,	IRE_MARK_TEMPORARY },
5430Sstevel@tonic-gate 		{ NULL,		0,			0		}
5440Sstevel@tonic-gate 	};
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	static const mdb_bitmask_t fmasks[] = {
5470Sstevel@tonic-gate 		{ "UP",		RTF_UP,			RTF_UP		},
5480Sstevel@tonic-gate 		{ "GATEWAY",	RTF_GATEWAY,		RTF_GATEWAY	},
5490Sstevel@tonic-gate 		{ "HOST",	RTF_HOST,		RTF_HOST	},
5500Sstevel@tonic-gate 		{ "REJECT",	RTF_REJECT,		RTF_REJECT	},
5510Sstevel@tonic-gate 		{ "DYNAMIC",	RTF_DYNAMIC,		RTF_DYNAMIC	},
5520Sstevel@tonic-gate 		{ "MODIFIED",	RTF_MODIFIED,		RTF_MODIFIED	},
5530Sstevel@tonic-gate 		{ "DONE",	RTF_DONE,		RTF_DONE	},
5540Sstevel@tonic-gate 		{ "MASK",	RTF_MASK,		RTF_MASK	},
5550Sstevel@tonic-gate 		{ "CLONING",	RTF_CLONING,		RTF_CLONING	},
5560Sstevel@tonic-gate 		{ "XRESOLVE",	RTF_XRESOLVE,		RTF_XRESOLVE	},
5570Sstevel@tonic-gate 		{ "LLINFO",	RTF_LLINFO,		RTF_LLINFO	},
5580Sstevel@tonic-gate 		{ "STATIC",	RTF_STATIC,		RTF_STATIC	},
5590Sstevel@tonic-gate 		{ "BLACKHOLE",	RTF_BLACKHOLE,		RTF_BLACKHOLE	},
5600Sstevel@tonic-gate 		{ "PRIVATE",	RTF_PRIVATE,		RTF_PRIVATE	},
5610Sstevel@tonic-gate 		{ "PROTO2",	RTF_PROTO2,		RTF_PROTO2	},
5620Sstevel@tonic-gate 		{ "PROTO1",	RTF_PROTO1,		RTF_PROTO1	},
5630Sstevel@tonic-gate 		{ "MULTIRT",	RTF_MULTIRT,		RTF_MULTIRT	},
5640Sstevel@tonic-gate 		{ "SETSRC",	RTF_SETSRC,		RTF_SETSRC	},
5650Sstevel@tonic-gate 		{ NULL,		0,			0		}
5660Sstevel@tonic-gate 	};
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (irep->ire_ipversion == 6 && *verbose) {
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		mdb_printf("%<b>%?p%</b> %40N <%hb>\n"
5710Sstevel@tonic-gate 		    "%?s %40N <%hb>\n"
5723448Sdh155122 		    "%?s %40d %4d <%hb>\n",
5730Sstevel@tonic-gate 		    addr, &irep->ire_src_addr_v6, irep->ire_type, tmasks,
5740Sstevel@tonic-gate 		    "", &irep->ire_addr_v6, (ushort_t)irep->ire_marks, mmasks,
5753448Sdh155122 		    "", ips_to_stackid((uintptr_t)irep->ire_ipst),
5763448Sdh155122 		    irep->ire_zoneid,
5773448Sdh155122 		    irep->ire_flags, fmasks);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	} else if (irep->ire_ipversion == 6) {
5800Sstevel@tonic-gate 
5813448Sdh155122 		mdb_printf("%?p %30N %30N %5d %4d\n",
5823448Sdh155122 		    addr, &irep->ire_src_addr_v6,
5833448Sdh155122 		    &irep->ire_addr_v6,
5843448Sdh155122 		    ips_to_stackid((uintptr_t)irep->ire_ipst),
5853448Sdh155122 		    irep->ire_zoneid);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	} else if (*verbose) {
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		mdb_printf("%<b>%?p%</b> %40I <%hb>\n"
5900Sstevel@tonic-gate 		    "%?s %40I <%hb>\n"
5910Sstevel@tonic-gate 		    "%?s %40d <%hb>\n",
5920Sstevel@tonic-gate 		    addr, irep->ire_src_addr, irep->ire_type, tmasks,
5930Sstevel@tonic-gate 		    "", irep->ire_addr, (ushort_t)irep->ire_marks, mmasks,
5943448Sdh155122 		    "", ips_to_stackid((uintptr_t)irep->ire_ipst),
5953448Sdh155122 		    irep->ire_zoneid, irep->ire_flags, fmasks);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	} else {
5980Sstevel@tonic-gate 
5993448Sdh155122 		mdb_printf("%?p %30I %30I %5d %4d\n", addr, irep->ire_src_addr,
6003448Sdh155122 		    irep->ire_addr, ips_to_stackid((uintptr_t)irep->ire_ipst),
6013448Sdh155122 		    irep->ire_zoneid);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	return (WALK_NEXT);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate  * There are faster ways to do this.  Given the interactive nature of this
6090Sstevel@tonic-gate  * use I don't think its worth much effort.
6100Sstevel@tonic-gate  */
6110Sstevel@tonic-gate static unsigned short
6120Sstevel@tonic-gate ipcksum(void *p, int len)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	int32_t	sum = 0;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	while (len > 1) {
6170Sstevel@tonic-gate 		/* alignment */
6180Sstevel@tonic-gate 		sum += *(uint16_t *)p;
6190Sstevel@tonic-gate 		p = (char *)p + sizeof (uint16_t);
6200Sstevel@tonic-gate 		if (sum & 0x80000000)
6210Sstevel@tonic-gate 			sum = (sum & 0xFFFF) + (sum >> 16);
6220Sstevel@tonic-gate 		len -= 2;
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if (len)
6260Sstevel@tonic-gate 		sum += (uint16_t)*(unsigned char *)p;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	while (sum >> 16)
6290Sstevel@tonic-gate 		sum = (sum & 0xFFFF) + (sum >> 16);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	return (~sum);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate static const mdb_bitmask_t tcp_flags[] = {
6350Sstevel@tonic-gate 	{ "SYN",	TH_SYN,		TH_SYN	},
6360Sstevel@tonic-gate 	{ "ACK",	TH_ACK,		TH_ACK	},
6370Sstevel@tonic-gate 	{ "FIN",	TH_FIN,		TH_FIN	},
6380Sstevel@tonic-gate 	{ "RST",	TH_RST,		TH_RST	},
6390Sstevel@tonic-gate 	{ "PSH",	TH_PUSH,	TH_PUSH	},
6400Sstevel@tonic-gate 	{ "ECE",	TH_ECE,		TH_ECE	},
6410Sstevel@tonic-gate 	{ "CWR",	TH_CWR,		TH_CWR	},
6420Sstevel@tonic-gate 	{ NULL,		0,		0	}
6430Sstevel@tonic-gate };
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate static void
6460Sstevel@tonic-gate tcphdr_print(struct tcphdr *tcph)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	in_port_t	sport, dport;
6490Sstevel@tonic-gate 	tcp_seq		seq, ack;
6500Sstevel@tonic-gate 	uint16_t	win, urp;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	mdb_printf("%<b>TCP header%</b>\n");
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	mdb_nhconvert(&sport, &tcph->th_sport, sizeof (sport));
6550Sstevel@tonic-gate 	mdb_nhconvert(&dport, &tcph->th_dport, sizeof (dport));
6560Sstevel@tonic-gate 	mdb_nhconvert(&seq, &tcph->th_seq, sizeof (seq));
6570Sstevel@tonic-gate 	mdb_nhconvert(&ack, &tcph->th_ack, sizeof (ack));
6580Sstevel@tonic-gate 	mdb_nhconvert(&win, &tcph->th_win, sizeof (win));
6590Sstevel@tonic-gate 	mdb_nhconvert(&urp, &tcph->th_urp, sizeof (urp));
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	mdb_printf("%<u>%6s %6s %10s %10s %4s %5s %5s %5s %-15s%</u>\n",
6620Sstevel@tonic-gate 	    "SPORT", "DPORT", "SEQ", "ACK", "HLEN", "WIN", "CSUM", "URP",
6630Sstevel@tonic-gate 	    "FLAGS");
6640Sstevel@tonic-gate 	mdb_printf("%6hu %6hu %10u %10u %4d %5hu %5hu %5hu <%b>\n",
6650Sstevel@tonic-gate 	    sport, dport, seq, ack, tcph->th_off << 2, win,
6660Sstevel@tonic-gate 	    tcph->th_sum, urp, tcph->th_flags, tcp_flags);
6670Sstevel@tonic-gate 	mdb_printf("0x%04x 0x%04x 0x%08x 0x%08x\n\n",
6680Sstevel@tonic-gate 	    sport, dport, seq, ack);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate /* ARGSUSED */
6720Sstevel@tonic-gate static int
6730Sstevel@tonic-gate tcphdr(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	struct tcphdr	tcph;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
6780Sstevel@tonic-gate 		return (DCMD_USAGE);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (mdb_vread(&tcph, sizeof (tcph), addr) == -1) {
6810Sstevel@tonic-gate 		mdb_warn("failed to read TCP header at %p", addr);
6820Sstevel@tonic-gate 		return (DCMD_ERR);
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 	tcphdr_print(&tcph);
6850Sstevel@tonic-gate 	return (DCMD_OK);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate static void
6890Sstevel@tonic-gate udphdr_print(struct udphdr *udph)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	in_port_t	sport, dport;
6920Sstevel@tonic-gate 	uint16_t	hlen;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	mdb_printf("%<b>UDP header%</b>\n");
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	mdb_nhconvert(&sport, &udph->uh_sport, sizeof (sport));
6970Sstevel@tonic-gate 	mdb_nhconvert(&dport, &udph->uh_dport, sizeof (dport));
6980Sstevel@tonic-gate 	mdb_nhconvert(&hlen, &udph->uh_ulen, sizeof (hlen));
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	mdb_printf("%<u>%14s %14s %5s %6s%</u>\n",
7010Sstevel@tonic-gate 	    "SPORT", "DPORT", "LEN", "CSUM");
7020Sstevel@tonic-gate 	mdb_printf("%5hu (0x%04x) %5hu (0x%04x) %5hu 0x%04hx\n\n", sport, sport,
7030Sstevel@tonic-gate 	    dport, dport, hlen, udph->uh_sum);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate /* ARGSUSED */
7070Sstevel@tonic-gate static int
7080Sstevel@tonic-gate udphdr(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	struct udphdr	udph;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
7130Sstevel@tonic-gate 		return (DCMD_USAGE);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (mdb_vread(&udph, sizeof (udph), addr) == -1) {
7160Sstevel@tonic-gate 		mdb_warn("failed to read UDP header at %p", addr);
7170Sstevel@tonic-gate 		return (DCMD_ERR);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 	udphdr_print(&udph);
7200Sstevel@tonic-gate 	return (DCMD_OK);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate static void
7240Sstevel@tonic-gate sctphdr_print(sctp_hdr_t *sctph)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	in_port_t sport, dport;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	mdb_printf("%<b>SCTP header%</b>\n");
7290Sstevel@tonic-gate 	mdb_nhconvert(&sport, &sctph->sh_sport, sizeof (sport));
7300Sstevel@tonic-gate 	mdb_nhconvert(&dport, &sctph->sh_dport, sizeof (dport));
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	mdb_printf("%<u>%14s %14s %10s %10s%</u>\n",
7330Sstevel@tonic-gate 	    "SPORT", "DPORT", "VTAG", "CHKSUM");
7340Sstevel@tonic-gate 	mdb_printf("%5hu (0x%04x) %5hu (0x%04x) %10u 0x%08x\n\n", sport, sport,
7350Sstevel@tonic-gate 	    dport, dport, sctph->sh_verf, sctph->sh_chksum);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate /* ARGSUSED */
7390Sstevel@tonic-gate static int
7400Sstevel@tonic-gate sctphdr(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	sctp_hdr_t sctph;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
7450Sstevel@tonic-gate 		return (DCMD_USAGE);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	if (mdb_vread(&sctph, sizeof (sctph), addr) == -1) {
7480Sstevel@tonic-gate 		mdb_warn("failed to read SCTP header at %p", addr);
7490Sstevel@tonic-gate 		return (DCMD_ERR);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	sctphdr_print(&sctph);
7530Sstevel@tonic-gate 	return (DCMD_OK);
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate static int
7570Sstevel@tonic-gate transport_hdr(int proto, uintptr_t addr)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	mdb_printf("\n");
7600Sstevel@tonic-gate 	switch (proto) {
7610Sstevel@tonic-gate 	case IPPROTO_TCP: {
7620Sstevel@tonic-gate 		struct tcphdr tcph;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		if (mdb_vread(&tcph, sizeof (tcph), addr) == -1) {
7650Sstevel@tonic-gate 			mdb_warn("failed to read TCP header at %p", addr);
7660Sstevel@tonic-gate 			return (DCMD_ERR);
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 		tcphdr_print(&tcph);
7690Sstevel@tonic-gate 		break;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	case IPPROTO_UDP:  {
7720Sstevel@tonic-gate 		struct udphdr udph;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		if (mdb_vread(&udph, sizeof (udph), addr) == -1) {
7750Sstevel@tonic-gate 			mdb_warn("failed to read UDP header at %p", addr);
7760Sstevel@tonic-gate 			return (DCMD_ERR);
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 		udphdr_print(&udph);
7790Sstevel@tonic-gate 		break;
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	case IPPROTO_SCTP: {
7820Sstevel@tonic-gate 		sctp_hdr_t sctph;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		if (mdb_vread(&sctph, sizeof (sctph), addr) == -1) {
7850Sstevel@tonic-gate 			mdb_warn("failed to read SCTP header at %p", addr);
7860Sstevel@tonic-gate 			return (DCMD_ERR);
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 		sctphdr_print(&sctph);
7890Sstevel@tonic-gate 		break;
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 	default:
7920Sstevel@tonic-gate 		break;
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	return (DCMD_OK);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate static const mdb_bitmask_t ip_flags[] = {
7990Sstevel@tonic-gate 	{ "DF",	IPH_DF, IPH_DF	},
8000Sstevel@tonic-gate 	{ "MF", IPH_MF,	IPH_MF	},
8010Sstevel@tonic-gate 	{ NULL, 0,	0	}
8020Sstevel@tonic-gate };
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /* ARGSUSED */
8050Sstevel@tonic-gate static int
8060Sstevel@tonic-gate iphdr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	uint_t		verbose = FALSE, force = FALSE;
8090Sstevel@tonic-gate 	ipha_t		iph[1];
8100Sstevel@tonic-gate 	uint16_t	ver, totlen, hdrlen, ipid, off, csum;
8110Sstevel@tonic-gate 	uintptr_t	nxt_proto;
8120Sstevel@tonic-gate 	char		exp_csum[8];
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
8150Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
8160Sstevel@tonic-gate 	    'f', MDB_OPT_SETBITS, TRUE, &force, NULL) != argc)
8170Sstevel@tonic-gate 		return (DCMD_USAGE);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (mdb_vread(iph, sizeof (*iph), addr) == -1) {
8200Sstevel@tonic-gate 		mdb_warn("failed to read IPv4 header at %p", addr);
8210Sstevel@tonic-gate 		return (DCMD_ERR);
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	ver = (iph->ipha_version_and_hdr_length & 0xf0) >> 4;
8250Sstevel@tonic-gate 	if (ver != IPV4_VERSION) {
8260Sstevel@tonic-gate 		if (ver == IPV6_VERSION) {
8270Sstevel@tonic-gate 			return (ip6hdr(addr, flags, argc, argv));
8280Sstevel@tonic-gate 		} else if (!force) {
8290Sstevel@tonic-gate 			mdb_warn("unknown IP version: %d\n", ver);
8300Sstevel@tonic-gate 			return (DCMD_ERR);
8310Sstevel@tonic-gate 		}
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	mdb_printf("%<b>IPv4 header%</b>\n");
8350Sstevel@tonic-gate 	mdb_printf("%-34s %-34s\n"
8360Sstevel@tonic-gate 	    "%<u>%-4s %-4s %-5s %-5s %-6s %-5s %-5s %-6s %-8s %-6s%</u>\n",
8370Sstevel@tonic-gate 	    "SRC", "DST",
8380Sstevel@tonic-gate 	    "HLEN", "TOS", "LEN", "ID", "OFFSET", "TTL", "PROTO", "CHKSUM",
8390Sstevel@tonic-gate 	    "EXP-CSUM", "FLGS");
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	hdrlen = (iph->ipha_version_and_hdr_length & 0x0f) << 2;
8420Sstevel@tonic-gate 	mdb_nhconvert(&totlen, &iph->ipha_length, sizeof (totlen));
8430Sstevel@tonic-gate 	mdb_nhconvert(&ipid, &iph->ipha_ident, sizeof (ipid));
8440Sstevel@tonic-gate 	mdb_nhconvert(&off, &iph->ipha_fragment_offset_and_flags, sizeof (off));
8450Sstevel@tonic-gate 	if (hdrlen == IP_SIMPLE_HDR_LENGTH) {
8460Sstevel@tonic-gate 		if ((csum = ipcksum(iph, sizeof (*iph))) != 0)
8470Sstevel@tonic-gate 			csum = ~(~csum + ~iph->ipha_hdr_checksum);
8480Sstevel@tonic-gate 		else
8490Sstevel@tonic-gate 			csum = iph->ipha_hdr_checksum;
8500Sstevel@tonic-gate 		mdb_snprintf(exp_csum, 8, "%u", csum);
8510Sstevel@tonic-gate 	} else {
8520Sstevel@tonic-gate 		mdb_snprintf(exp_csum, 8, "<n/a>");
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	mdb_printf("%-34I %-34I%\n"
8560Sstevel@tonic-gate 	    "%-4d %-4d %-5hu %-5hu %-6hu %-5hu %-5hu %-6u %-8s <%5hb>\n",
8570Sstevel@tonic-gate 	    iph->ipha_src, iph->ipha_dst,
8580Sstevel@tonic-gate 	    hdrlen, iph->ipha_type_of_service, totlen, ipid,
8590Sstevel@tonic-gate 	    (off << 3) & 0xffff, iph->ipha_ttl, iph->ipha_protocol,
8600Sstevel@tonic-gate 	    iph->ipha_hdr_checksum, exp_csum, off, ip_flags);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	if (verbose) {
8630Sstevel@tonic-gate 		nxt_proto = addr + hdrlen;
8640Sstevel@tonic-gate 		return (transport_hdr(iph->ipha_protocol, nxt_proto));
8650Sstevel@tonic-gate 	} else {
8660Sstevel@tonic-gate 		return (DCMD_OK);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /* ARGSUSED */
8710Sstevel@tonic-gate static int
8720Sstevel@tonic-gate ip6hdr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	uint_t		verbose = FALSE, force = FALSE;
8750Sstevel@tonic-gate 	ip6_t		iph[1];
8760Sstevel@tonic-gate 	int		ver, class, flow;
8770Sstevel@tonic-gate 	uint16_t	plen;
8780Sstevel@tonic-gate 	uintptr_t	nxt_proto;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
8810Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
8820Sstevel@tonic-gate 	    'f', MDB_OPT_SETBITS, TRUE, &force, NULL) != argc)
8830Sstevel@tonic-gate 		return (DCMD_USAGE);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (mdb_vread(iph, sizeof (*iph), addr) == -1) {
8860Sstevel@tonic-gate 		mdb_warn("failed to read IPv6 header at %p", addr);
8870Sstevel@tonic-gate 		return (DCMD_ERR);
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	ver = (iph->ip6_vfc & 0xf0) >> 4;
8910Sstevel@tonic-gate 	if (ver != IPV6_VERSION) {
8920Sstevel@tonic-gate 		if (ver == IPV4_VERSION) {
8930Sstevel@tonic-gate 			return (iphdr(addr, flags, argc, argv));
8940Sstevel@tonic-gate 		} else if (!force) {
8950Sstevel@tonic-gate 			mdb_warn("unknown IP version: %d\n", ver);
8960Sstevel@tonic-gate 			return (DCMD_ERR);
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	mdb_printf("%<b>IPv6 header%</b>\n");
9010Sstevel@tonic-gate 	mdb_printf("%<u>%-26s %-26s %4s %7s %5s %3s %3s%</u>\n",
9020Sstevel@tonic-gate 	    "SRC", "DST", "TCLS", "FLOW-ID", "PLEN", "NXT", "HOP");
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	class = (iph->ip6_vcf & IPV6_FLOWINFO_TCLASS) >> 20;
9050Sstevel@tonic-gate 	mdb_nhconvert(&class, &class, sizeof (class));
9060Sstevel@tonic-gate 	flow = iph->ip6_vcf & IPV6_FLOWINFO_FLOWLABEL;
9070Sstevel@tonic-gate 	mdb_nhconvert(&flow, &flow, sizeof (flow));
9080Sstevel@tonic-gate 	mdb_nhconvert(&plen, &iph->ip6_plen, sizeof (plen));
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	mdb_printf("%-26N %-26N %4d %7d %5hu %3d %3d\n",
9110Sstevel@tonic-gate 	    &iph->ip6_src, &iph->ip6_dst,
9120Sstevel@tonic-gate 	    class, flow, plen, iph->ip6_nxt, iph->ip6_hlim);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	if (verbose) {
9150Sstevel@tonic-gate 		nxt_proto = addr + sizeof (ip6_t);
9160Sstevel@tonic-gate 		return (transport_hdr(iph->ip6_nxt, nxt_proto));
9170Sstevel@tonic-gate 	} else {
9180Sstevel@tonic-gate 		return (DCMD_OK);
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate int
9230Sstevel@tonic-gate ire(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	uint_t verbose = FALSE;
9260Sstevel@tonic-gate 	ire_t ire;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
9290Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
9300Sstevel@tonic-gate 		return (DCMD_USAGE);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		if (verbose) {
9350Sstevel@tonic-gate 			mdb_printf("%?s %40s %-20s%\n"
9360Sstevel@tonic-gate 			    "%?s %40s %-20s%\n"
9373448Sdh155122 			    "%<u>%?s %40s %4s %-20s%</u>\n",
9380Sstevel@tonic-gate 			    "ADDR", "SRC", "TYPE",
9390Sstevel@tonic-gate 			    "", "DST", "MARKS",
9403448Sdh155122 			    "", "STACK", "ZONE", "FLAGS");
9410Sstevel@tonic-gate 		} else {
9423448Sdh155122 			mdb_printf("%<u>%?s %30s %30s %5s %4s%</u>\n",
9433448Sdh155122 			    "ADDR", "SRC", "DST", "STACK", "ZONE");
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
9480Sstevel@tonic-gate 		(void) mdb_vread(&ire, sizeof (ire_t), addr);
9490Sstevel@tonic-gate 		(void) ire_format(addr, &ire, &verbose);
9500Sstevel@tonic-gate 	} else if (mdb_walk("ire", (mdb_walk_cb_t)ire_format, &verbose) == -1) {
9510Sstevel@tonic-gate 		mdb_warn("failed to walk ire table");
9520Sstevel@tonic-gate 		return (DCMD_ERR);
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	return (DCMD_OK);
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate static size_t
9590Sstevel@tonic-gate mi_osize(const queue_t *q)
9600Sstevel@tonic-gate {
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * The code in common/inet/mi.c allocates an extra word to store the
9630Sstevel@tonic-gate 	 * size of the allocation.  An mi_o_s is thus a size_t plus an mi_o_s.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	struct mi_block {
9660Sstevel@tonic-gate 		size_t mi_nbytes;
9670Sstevel@tonic-gate 		struct mi_o_s mi_o;
9680Sstevel@tonic-gate 	} m;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	if (mdb_vread(&m, sizeof (m), (uintptr_t)q->q_ptr -
9710Sstevel@tonic-gate 	    sizeof (m)) == sizeof (m))
9720Sstevel@tonic-gate 		return (m.mi_nbytes - sizeof (m));
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	return (0);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate static void
9780Sstevel@tonic-gate ip_ill_qinfo(const queue_t *q, char *buf, size_t nbytes)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	char name[32];
9810Sstevel@tonic-gate 	ill_t ill;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	if (mdb_vread(&ill, sizeof (ill),
9840Sstevel@tonic-gate 	    (uintptr_t)q->q_ptr) == sizeof (ill) &&
9850Sstevel@tonic-gate 	    mdb_readstr(name, sizeof (name), (uintptr_t)ill.ill_name) > 0)
9860Sstevel@tonic-gate 		(void) mdb_snprintf(buf, nbytes, "if: %s", name);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate void
9900Sstevel@tonic-gate ip_qinfo(const queue_t *q, char *buf, size_t nbytes)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	size_t size = mi_osize(q);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (size == sizeof (ill_t))
9950Sstevel@tonic-gate 		ip_ill_qinfo(q, buf, nbytes);
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate uintptr_t
9990Sstevel@tonic-gate ip_rnext(const queue_t *q)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	size_t size = mi_osize(q);
10020Sstevel@tonic-gate 	ill_t ill;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	if (size == sizeof (ill_t) && mdb_vread(&ill, sizeof (ill),
10050Sstevel@tonic-gate 	    (uintptr_t)q->q_ptr) == sizeof (ill))
10060Sstevel@tonic-gate 		return ((uintptr_t)ill.ill_rq);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	return (NULL);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate uintptr_t
10120Sstevel@tonic-gate ip_wnext(const queue_t *q)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	size_t size = mi_osize(q);
10150Sstevel@tonic-gate 	ill_t ill;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	if (size == sizeof (ill_t) && mdb_vread(&ill, sizeof (ill),
10180Sstevel@tonic-gate 	    (uintptr_t)q->q_ptr) == sizeof (ill))
10190Sstevel@tonic-gate 		return ((uintptr_t)ill.ill_wq);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	return (NULL);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate /*
10250Sstevel@tonic-gate  * Print the core fields in an squeue_t.  With the "-v" argument,
10260Sstevel@tonic-gate  * provide more verbose output.
10270Sstevel@tonic-gate  */
10280Sstevel@tonic-gate static int
10290Sstevel@tonic-gate squeue(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate 	unsigned int	i;
10320Sstevel@tonic-gate 	unsigned int	verbose = FALSE;
10330Sstevel@tonic-gate 	const int	SQUEUE_STATEDELT = (int)(sizeof (uintptr_t) + 9);
10340Sstevel@tonic-gate 	boolean_t	arm;
10350Sstevel@tonic-gate 	squeue_t	squeue;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
10380Sstevel@tonic-gate 		if (mdb_walk_dcmd("genunix`squeue_cache", "ip`squeue",
10390Sstevel@tonic-gate 		    argc, argv) == -1) {
10400Sstevel@tonic-gate 			mdb_warn("failed to walk squeue cache");
10410Sstevel@tonic-gate 			return (DCMD_ERR);
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 		return (DCMD_OK);
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL)
10470Sstevel@tonic-gate 	    != argc)
10480Sstevel@tonic-gate 		return (DCMD_USAGE);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	if (!DCMD_HDRSPEC(flags) && verbose)
10510Sstevel@tonic-gate 		mdb_printf("\n\n");
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) || verbose) {
10540Sstevel@tonic-gate 		mdb_printf("%?s %-5s %-3s %?s %?s %?s\n",
10550Sstevel@tonic-gate 		    "ADDR", "STATE", "CPU",
10560Sstevel@tonic-gate 		    "FIRST", "LAST", "WORKER");
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	if (mdb_vread(&squeue, sizeof (squeue_t), addr) == -1) {
10600Sstevel@tonic-gate 		mdb_warn("cannot read squeue_t at %p", addr);
10610Sstevel@tonic-gate 		return (DCMD_ERR);
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	mdb_printf("%0?p %05x %3d %0?p %0?p %0?p\n",
10650Sstevel@tonic-gate 	    addr, squeue.sq_state, squeue.sq_bind,
10660Sstevel@tonic-gate 	    squeue.sq_first, squeue.sq_last, squeue.sq_worker);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (!verbose)
10690Sstevel@tonic-gate 		return (DCMD_OK);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	arm = B_TRUE;
10720Sstevel@tonic-gate 	for (i = 0; squeue_states[i].bit_name != NULL; i++) {
10730Sstevel@tonic-gate 		if (((squeue.sq_state) & (1 << i)) == 0)
10740Sstevel@tonic-gate 			continue;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 		if (arm) {
10770Sstevel@tonic-gate 			mdb_printf("%*s|\n", SQUEUE_STATEDELT, "");
10780Sstevel@tonic-gate 			mdb_printf("%*s+-->  ", SQUEUE_STATEDELT, "");
10790Sstevel@tonic-gate 			arm = B_FALSE;
10800Sstevel@tonic-gate 		} else
10810Sstevel@tonic-gate 			mdb_printf("%*s      ", SQUEUE_STATEDELT, "");
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 		mdb_printf("%-12s %s\n", squeue_states[i].bit_name,
10840Sstevel@tonic-gate 		    squeue_states[i].bit_descr);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	return (DCMD_OK);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate static void
10910Sstevel@tonic-gate ip_squeue_help(void)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	mdb_printf("Print the core information for a given NCA squeue_t.\n\n");
10940Sstevel@tonic-gate 	mdb_printf("Options:\n");
10950Sstevel@tonic-gate 	mdb_printf("\t-v\tbe verbose (more descriptive)\n");
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
1098*5023Scarlsonj /*
1099*5023Scarlsonj  * This is called by ::th_trace (via a callback) when walking the th_hash
1100*5023Scarlsonj  * list.  It calls modent to find the entries.
1101*5023Scarlsonj  */
1102*5023Scarlsonj /* ARGSUSED */
1103*5023Scarlsonj static int
1104*5023Scarlsonj modent_summary(uintptr_t addr, const void *data, void *private)
1105*5023Scarlsonj {
1106*5023Scarlsonj 	th_walk_data_t *thw = private;
1107*5023Scarlsonj 	const struct mod_hash_entry *mhe = data;
1108*5023Scarlsonj 	th_trace_t th;
1109*5023Scarlsonj 
1110*5023Scarlsonj 	if (mdb_vread(&th, sizeof (th), (uintptr_t)mhe->mhe_val) == -1) {
1111*5023Scarlsonj 		mdb_warn("failed to read th_trace_t %p", mhe->mhe_val);
1112*5023Scarlsonj 		return (WALK_ERR);
1113*5023Scarlsonj 	}
1114*5023Scarlsonj 
1115*5023Scarlsonj 	if (th.th_refcnt == 0 && thw->thw_non_zero_only)
1116*5023Scarlsonj 		return (WALK_NEXT);
1117*5023Scarlsonj 
1118*5023Scarlsonj 	if (!thw->thw_match) {
1119*5023Scarlsonj 		mdb_printf("%?p %?p %?p %8d %?p\n", thw->thw_ipst, mhe->mhe_key,
1120*5023Scarlsonj 		    mhe->mhe_val, th.th_refcnt, th.th_id);
1121*5023Scarlsonj 	} else if (thw->thw_matchkey == (uintptr_t)mhe->mhe_key) {
1122*5023Scarlsonj 		int i, j, k;
1123*5023Scarlsonj 		tr_buf_t *tr;
1124*5023Scarlsonj 
1125*5023Scarlsonj 		mdb_printf("Object %p in IP stack %p:\n", mhe->mhe_key,
1126*5023Scarlsonj 		    thw->thw_ipst);
1127*5023Scarlsonj 		i = th.th_trace_lastref;
1128*5023Scarlsonj 		mdb_printf("\tThread %p refcnt %d:\n", th.th_id,
1129*5023Scarlsonj 		    th.th_refcnt);
1130*5023Scarlsonj 		for (j = TR_BUF_MAX; j > 0; j--) {
1131*5023Scarlsonj 			tr = th.th_trbuf + i;
1132*5023Scarlsonj 			if (tr->tr_depth == 0 || tr->tr_depth > TR_STACK_DEPTH)
1133*5023Scarlsonj 				break;
1134*5023Scarlsonj 			mdb_printf("\t  T%+ld:\n", tr->tr_time -
1135*5023Scarlsonj 			    thw->thw_lbolt);
1136*5023Scarlsonj 			for (k = 0; k < tr->tr_depth; k++)
1137*5023Scarlsonj 				mdb_printf("\t\t%a\n", tr->tr_stack[k]);
1138*5023Scarlsonj 			if (--i < 0)
1139*5023Scarlsonj 				i = TR_BUF_MAX - 1;
1140*5023Scarlsonj 		}
1141*5023Scarlsonj 	}
1142*5023Scarlsonj 	return (WALK_NEXT);
1143*5023Scarlsonj }
1144*5023Scarlsonj 
1145*5023Scarlsonj /*
1146*5023Scarlsonj  * This is called by ::th_trace (via a callback) when walking the th_hash
1147*5023Scarlsonj  * list.  It calls modent to find the entries.
1148*5023Scarlsonj  */
1149*5023Scarlsonj /* ARGSUSED */
1150*5023Scarlsonj static int
1151*5023Scarlsonj th_hash_summary(uintptr_t addr, const void *data, void *private)
1152*5023Scarlsonj {
1153*5023Scarlsonj 	const th_hash_t *thh = data;
1154*5023Scarlsonj 	th_walk_data_t *thw = private;
1155*5023Scarlsonj 
1156*5023Scarlsonj 	thw->thw_ipst = (uintptr_t)thh->thh_ipst;
1157*5023Scarlsonj 	return (mdb_pwalk("modent", modent_summary, private,
1158*5023Scarlsonj 	    (uintptr_t)thh->thh_hash));
1159*5023Scarlsonj }
1160*5023Scarlsonj 
1161*5023Scarlsonj /*
1162*5023Scarlsonj  * Print or summarize the th_trace_t structures.
1163*5023Scarlsonj  */
1164*5023Scarlsonj static int
1165*5023Scarlsonj th_trace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1166*5023Scarlsonj {
1167*5023Scarlsonj 	th_walk_data_t thw;
1168*5023Scarlsonj 
1169*5023Scarlsonj 	(void) memset(&thw, 0, sizeof (thw));
1170*5023Scarlsonj 
1171*5023Scarlsonj 	if (mdb_getopts(argc, argv,
1172*5023Scarlsonj 	    'n', MDB_OPT_SETBITS, TRUE, &thw.thw_non_zero_only,
1173*5023Scarlsonj 	    NULL) != argc)
1174*5023Scarlsonj 		return (DCMD_USAGE);
1175*5023Scarlsonj 
1176*5023Scarlsonj 	if (!(flags & DCMD_ADDRSPEC)) {
1177*5023Scarlsonj 		/*
1178*5023Scarlsonj 		 * No address specified.  Walk all of the th_hash_t in the
1179*5023Scarlsonj 		 * system, and summarize the th_trace_t entries in each.
1180*5023Scarlsonj 		 */
1181*5023Scarlsonj 		mdb_printf("%?s %?s %?s %8s %?s\n",
1182*5023Scarlsonj 		    "IPSTACK", "OBJECT", "TRACE", "REFCNT", "THREAD");
1183*5023Scarlsonj 		thw.thw_match = B_FALSE;
1184*5023Scarlsonj 	} else {
1185*5023Scarlsonj 		thw.thw_match = B_TRUE;
1186*5023Scarlsonj 		thw.thw_matchkey = addr;
1187*5023Scarlsonj 		if (mdb_readvar(&thw.thw_lbolt,
1188*5023Scarlsonj 		    mdb_prop_postmortem ? "panic_lbolt" : "lbolt") == -1) {
1189*5023Scarlsonj 			mdb_warn("failed to read lbolt");
1190*5023Scarlsonj 			return (DCMD_ERR);
1191*5023Scarlsonj 		}
1192*5023Scarlsonj 	}
1193*5023Scarlsonj 	if (mdb_pwalk("th_hash", th_hash_summary, &thw, NULL) == -1) {
1194*5023Scarlsonj 		mdb_warn("can't walk th_hash entries");
1195*5023Scarlsonj 		return (DCMD_ERR);
1196*5023Scarlsonj 	}
1197*5023Scarlsonj 	return (DCMD_OK);
1198*5023Scarlsonj }
1199*5023Scarlsonj 
1200*5023Scarlsonj static void
1201*5023Scarlsonj th_trace_help(void)
1202*5023Scarlsonj {
1203*5023Scarlsonj 	mdb_printf("If given an address of an ill_t, ipif_t, ire_t, or nce_t, "
1204*5023Scarlsonj 	    "print the\n"
1205*5023Scarlsonj 	    "corresponding th_trace_t structure in detail.  Otherwise, if no "
1206*5023Scarlsonj 	    "address is\n"
1207*5023Scarlsonj 	    "given, then summarize all th_trace_t structures.\n\n");
1208*5023Scarlsonj 	mdb_printf("Options:\n"
1209*5023Scarlsonj 	    "\t-n\tdisplay only entries with non-zero th_refcnt\n");
1210*5023Scarlsonj }
1211*5023Scarlsonj 
12120Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
12130Sstevel@tonic-gate 	{ "illif", "?[-P v4 | v6]",
12140Sstevel@tonic-gate 	    "display or filter IP Lower Level InterFace structures", illif,
12150Sstevel@tonic-gate 	    illif_help },
12160Sstevel@tonic-gate 	{ "iphdr", ":[-vf]", "display an IPv4 header", iphdr },
12170Sstevel@tonic-gate 	{ "ip6hdr", ":[-vf]", "display an IPv6 header", ip6hdr },
12180Sstevel@tonic-gate 	{ "ire", "?[-v]", "display Internet Route Entry structures", ire },
12190Sstevel@tonic-gate 	{ "squeue", ":[-v]", "print core squeue_t info", squeue,
12200Sstevel@tonic-gate 	    ip_squeue_help },
12210Sstevel@tonic-gate 	{ "tcphdr", ":", "display a TCP header", tcphdr },
12220Sstevel@tonic-gate 	{ "udphdr", ":", "display an UDP header", udphdr },
12230Sstevel@tonic-gate 	{ "sctphdr", ":", "display an SCTP header", sctphdr },
1224*5023Scarlsonj 	{ "th_trace", "?[-n]", "display th_trace_t structures", th_trace,
1225*5023Scarlsonj 	    th_trace_help },
12260Sstevel@tonic-gate 	{ NULL }
12270Sstevel@tonic-gate };
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
12303448Sdh155122 	{ "illif", "walk list of ill interface types for all stacks",
12313448Sdh155122 		illif_walk_init, illif_walk_step, NULL },
12323448Sdh155122 	{ "illif_stack", "walk list of ill interface types",
12333448Sdh155122 		illif_stack_walk_init, illif_stack_walk_step,
12343448Sdh155122 		illif_stack_walk_fini },
12350Sstevel@tonic-gate 	{ "ire", "walk active ire_t structures",
12360Sstevel@tonic-gate 		ire_walk_init, ire_walk_step, NULL },
12373448Sdh155122 	{ "ire_ctable", "walk ire_t structures in the ctable",
12383448Sdh155122 		ire_ctable_walk_init, ire_ctable_walk_step, NULL },
12393448Sdh155122 	{ "ire_next", "walk ire_t structures in the ctable",
12403448Sdh155122 		ire_next_walk_init, ire_next_walk_step, NULL },
12413448Sdh155122 	{ "ip_stacks", "walk all the ip_stack_t",
12423448Sdh155122 		ip_stacks_walk_init, ip_stacks_walk_step, NULL },
1243*5023Scarlsonj 	{ "th_hash", "walk all the th_hash_t entries",
1244*5023Scarlsonj 		th_hash_walk_init, th_hash_walk_step, NULL },
12450Sstevel@tonic-gate 	{ NULL }
12460Sstevel@tonic-gate };
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate static const mdb_qops_t ip_qops = { ip_qinfo, ip_rnext, ip_wnext };
12490Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate const mdb_modinfo_t *
12520Sstevel@tonic-gate _mdb_init(void)
12530Sstevel@tonic-gate {
12540Sstevel@tonic-gate 	GElf_Sym sym;
12550Sstevel@tonic-gate 
12562546Scarlsonj 	if (mdb_lookup_by_obj("ip", "ipwinit", &sym) == 0)
12570Sstevel@tonic-gate 		mdb_qops_install(&ip_qops, (uintptr_t)sym.st_value);
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	return (&modinfo);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate void
12630Sstevel@tonic-gate _mdb_fini(void)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	GElf_Sym sym;
12660Sstevel@tonic-gate 
12672546Scarlsonj 	if (mdb_lookup_by_obj("ip", "ipwinit", &sym) == 0)
12680Sstevel@tonic-gate 		mdb_qops_remove(&ip_qops, (uintptr_t)sym.st_value);
12690Sstevel@tonic-gate }
1270