|
root / base / usr / src / cmd / mdb / common / modules / mac / mac.c
mac.c C 1273 lines 33.6 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2018 Joyent, Inc.
 */

#include <sys/mdb_modapi.h>
#include <sys/types.h>
#include <inet/ip.h>
#include <inet/ip6.h>

#include <sys/mac.h>
#include <sys/mac_provider.h>
#include <sys/mac_client.h>
#include <sys/mac_client_impl.h>
#include <sys/mac_flow_impl.h>
#include <sys/mac_soft_ring.h>
#include <sys/mac_stat.h>

#define	STRSIZE	64
#define	MAC_RX_SRS_SIZE	 (MAX_RINGS_PER_GROUP * sizeof (uintptr_t))

#define	LAYERED_WALKER_FOR_FLOW	"flow_entry_cache"
#define	LAYERED_WALKER_FOR_SRS	"mac_srs_cache"
#define	LAYERED_WALKER_FOR_RING	"mac_ring_cache"
#define	LAYERED_WALKER_FOR_GROUP	"mac_impl_cache"

/* arguments passed to mac_flow dee-command */
#define	MAC_FLOW_NONE	0x01
#define	MAC_FLOW_ATTR	0x02
#define	MAC_FLOW_PROP	0x04
#define	MAC_FLOW_RX	0x08
#define	MAC_FLOW_TX	0x10
#define	MAC_FLOW_USER	0x20
#define	MAC_FLOW_STATS	0x40
#define	MAC_FLOW_MISC	0x80

/* arguments passed to mac_srs dee-command */
#define	MAC_SRS_NONE		0x00
#define	MAC_SRS_RX		0x01
#define	MAC_SRS_TX		0x02
#define	MAC_SRS_STAT		0x04
#define	MAC_SRS_CPU		0x08
#define	MAC_SRS_VERBOSE		0x10
#define	MAC_SRS_INTR		0x20
#define	MAC_SRS_RXSTAT		(MAC_SRS_RX|MAC_SRS_STAT)
#define	MAC_SRS_TXSTAT		(MAC_SRS_TX|MAC_SRS_STAT)
#define	MAC_SRS_RXCPU		(MAC_SRS_RX|MAC_SRS_CPU)
#define	MAC_SRS_TXCPU		(MAC_SRS_TX|MAC_SRS_CPU)
#define	MAC_SRS_RXCPUVERBOSE	(MAC_SRS_RXCPU|MAC_SRS_VERBOSE)
#define	MAC_SRS_TXCPUVERBOSE	(MAC_SRS_TXCPU|MAC_SRS_VERBOSE)
#define	MAC_SRS_RXINTR		(MAC_SRS_RX|MAC_SRS_INTR)
#define	MAC_SRS_TXINTR		(MAC_SRS_TX|MAC_SRS_INTR)

/* arguments passed to mac_group dcmd */
#define	MAC_GROUP_NONE		0x00
#define	MAC_GROUP_RX		0x01
#define	MAC_GROUP_TX		0x02
#define	MAC_GROUP_UNINIT	0x04

static char *
mac_flow_proto2str(uint8_t protocol)
{
	switch (protocol) {
	case IPPROTO_TCP:
		return ("tcp");
	case IPPROTO_UDP:
		return ("udp");
	case IPPROTO_SCTP:
		return ("sctp");
	case IPPROTO_ICMP:
		return ("icmp");
	case IPPROTO_ICMPV6:
		return ("icmpv6");
	default:
		return ("--");
	}
}

static char *
mac_flow_priority2str(mac_priority_level_t prio)
{
	switch (prio) {
	case MPL_LOW:
		return ("low");
	case MPL_MEDIUM:
		return ("medium");
	case MPL_HIGH:
		return ("high");
	case MPL_RESET:
		return ("reset");
	default:
		return ("--");
	}
}

/*
 *  Convert bandwidth in bps to a string in Mbps.
 */
static char *
mac_flow_bw2str(uint64_t bw, char *buf, ssize_t len)
{
	int kbps, mbps;

	kbps = (bw % 1000000)/1000;
	mbps = bw/1000000;
	if ((mbps == 0) && (kbps != 0))
		mdb_snprintf(buf, len, "0.%03u", kbps);
	else
		mdb_snprintf(buf, len, "%5u", mbps);
	return (buf);
}

static void
mac_flow_print_header(uint_t args)
{
	switch (args) {
	case MAC_FLOW_NONE:
		mdb_printf("%?s %-20s %4s %?s %?s %-16s\n",
		    "", "", "LINK", "", "", "MIP");
		mdb_printf("%<u>%?s %-20s %4s %?s %?s %-16s%</u>\n",
		    "ADDR", "FLOW NAME", "ID", "MCIP", "MIP", "NAME");
		break;
	case MAC_FLOW_ATTR:
		mdb_printf("%<u>%?s %-32s %-7s %6s "
		    "%-9s %s%</u>\n",
		    "ADDR", "FLOW NAME", "PROTO", "PORT",
		    "DSFLD:MSK", "IPADDR");
		break;
	case MAC_FLOW_PROP:
		mdb_printf("%<u>%?s %-32s %8s %9s%</u>\n",
		    "ADDR", "FLOW NAME", "MAXBW(M)", "PRIORITY");
		break;
	case MAC_FLOW_MISC:
		mdb_printf("%<u>%?s %-24s %10s %10s "
		    "%20s %4s%</u>\n",
		    "ADDR", "FLOW NAME", "TYPE", "FLAGS",
		    "MATCH_FN", "ZONE");
		break;
	case MAC_FLOW_RX:
		mdb_printf("%?s %-24s %3s %s\n", "", "", "SRS", "RX");
		mdb_printf("%<u>%?s %-24s %3s %s%</u>\n",
		    "ADDR", "FLOW NAME", "CNT", "SRS");
		break;
	case MAC_FLOW_TX:
		mdb_printf("%<u>%?s %-32s %?s %</u>\n",
		    "ADDR", "FLOW NAME", "TX_SRS");
		break;
	case MAC_FLOW_STATS:
		mdb_printf("%<u>%?s %-32s %16s %16s%</u>\n",
		    "ADDR", "FLOW NAME", "RBYTES", "OBYTES");
		break;
	}
}

/*
 * Display selected fields of the flow_entry_t structure
 */
static int
mac_flow_dcmd_output(uintptr_t addr, uint_t flags, uint_t args)
{
	static const mdb_bitmask_t flow_type_bits[] = {
		{"P", FLOW_PRIMARY_MAC, FLOW_PRIMARY_MAC},
		{"V", FLOW_VNIC_MAC, FLOW_VNIC_MAC},
		{"M", FLOW_MCAST, FLOW_MCAST},
		{"O", FLOW_OTHER, FLOW_OTHER},
		{"U", FLOW_USER, FLOW_USER},
		{"V", FLOW_VNIC, FLOW_VNIC},
		{"NS", FLOW_NO_STATS, FLOW_NO_STATS},
		{ NULL, 0, 0 }
	};
#define	FLOW_MAX_TYPE	(sizeof (flow_type_bits) / sizeof (mdb_bitmask_t))

	static const mdb_bitmask_t flow_flag_bits[] = {
		{"Q", FE_QUIESCE, FE_QUIESCE},
		{"W", FE_WAITER, FE_WAITER},
		{"T", FE_FLOW_TAB, FE_FLOW_TAB},
		{"G", FE_G_FLOW_HASH, FE_G_FLOW_HASH},
		{"I", FE_INCIPIENT, FE_INCIPIENT},
		{"C", FE_CONDEMNED, FE_CONDEMNED},
		{"NU", FE_UF_NO_DATAPATH, FE_UF_NO_DATAPATH},
		{"NC", FE_MC_NO_DATAPATH, FE_MC_NO_DATAPATH},
		{ NULL, 0, 0 }
	};
#define	FLOW_MAX_FLAGS	(sizeof (flow_flag_bits) / sizeof (mdb_bitmask_t))
	flow_entry_t		fe;
	mac_client_impl_t	mcip;
	mac_impl_t		mip;

	if (mdb_vread(&fe, sizeof (fe), addr) == -1) {
		mdb_warn("failed to read struct flow_entry_s at %p", addr);
		return (DCMD_ERR);
	}
	if (args & MAC_FLOW_USER) {
		args &= ~MAC_FLOW_USER;
		if (fe.fe_type & FLOW_MCAST) {
			if (DCMD_HDRSPEC(flags))
				mac_flow_print_header(args);
			return (DCMD_OK);
		}
	}
	if (DCMD_HDRSPEC(flags))
		mac_flow_print_header(args);
	bzero(&mcip, sizeof (mcip));
	bzero(&mip, sizeof (mip));
	if (fe.fe_mcip != NULL && mdb_vread(&mcip, sizeof (mcip),
	    (uintptr_t)fe.fe_mcip) == sizeof (mcip)) {
		(void) mdb_vread(&mip, sizeof (mip), (uintptr_t)mcip.mci_mip);
	}
	switch (args) {
	case MAC_FLOW_NONE: {
		mdb_printf("%?p %-20s %4d %?p "
		    "%?p %-16s\n",
		    addr, fe.fe_flow_name, fe.fe_link_id, fe.fe_mcip,
		    mcip.mci_mip, mip.mi_name);
		break;
	}
	case MAC_FLOW_ATTR: {
		struct	in_addr	in4;
		uintptr_t	desc_addr;
		flow_desc_t	fdesc;

		desc_addr = addr + OFFSETOF(flow_entry_t, fe_flow_desc);
		if (mdb_vread(&fdesc, sizeof (fdesc), desc_addr) == -1) {
			mdb_warn("failed to read struct flow_description at %p",
			    desc_addr);
			return (DCMD_ERR);
		}
		mdb_printf("%?p %-32s "
		    "%-7s %6d "
		    "%4d:%-4d ",
		    addr, fe.fe_flow_name,
		    mac_flow_proto2str(fdesc.fd_protocol), fdesc.fd_local_port,
		    fdesc.fd_dsfield, fdesc.fd_dsfield_mask);
		if (fdesc.fd_ipversion == IPV4_VERSION) {
			IN6_V4MAPPED_TO_INADDR(&fdesc.fd_local_addr, &in4);
			mdb_printf("%I", in4.s_addr);
		} else if (fdesc.fd_ipversion == IPV6_VERSION) {
			mdb_printf("%N", &fdesc.fd_local_addr);
		} else {
			mdb_printf("%s", "--");
		}
		mdb_printf("\n");
		break;
	}
	case MAC_FLOW_PROP: {
		uintptr_t	prop_addr;
		char		bwstr[STRSIZE];
		mac_resource_props_t	fprop;

		prop_addr = addr + OFFSETOF(flow_entry_t, fe_resource_props);
		if (mdb_vread(&fprop, sizeof (fprop), prop_addr) == -1) {
			mdb_warn("failed to read struct mac_resoource_props "
			    "at %p", prop_addr);
			return (DCMD_ERR);
		}
		mdb_printf("%?p %-32s "
		    "%8s %9s\n",
		    addr, fe.fe_flow_name,
		    mac_flow_bw2str(fprop.mrp_maxbw, bwstr, STRSIZE),
		    mac_flow_priority2str(fprop.mrp_priority));
		break;
	}
	case MAC_FLOW_MISC: {
		char		flow_flags[2 * FLOW_MAX_FLAGS];
		char		flow_type[2 * FLOW_MAX_TYPE];
		GElf_Sym	sym;
		char		func_name[MDB_SYM_NAMLEN] = "";
		uintptr_t	func, match_addr;

		match_addr = addr + OFFSETOF(flow_entry_t, fe_match);
		(void) mdb_vread(&func, sizeof (func), match_addr);
		(void) mdb_lookup_by_addr(func, MDB_SYM_EXACT, func_name,
		    MDB_SYM_NAMLEN, &sym);
		mdb_snprintf(flow_flags, 2 * FLOW_MAX_FLAGS, "%hb",
		    fe.fe_flags, flow_flag_bits);
		mdb_snprintf(flow_type, 2 * FLOW_MAX_TYPE, "%hb",
		    fe.fe_type, flow_type_bits);
		mdb_printf("%?p %-24s %10s %10s %20s\n",
		    addr, fe.fe_flow_name, flow_type, flow_flags, func_name);
		break;
	}
	case MAC_FLOW_RX: {
		uintptr_t	rxaddr, rx_srs[MAX_RINGS_PER_GROUP] = {0};
		int		i;

		rxaddr = addr + OFFSETOF(flow_entry_t, fe_rx_srs);
		(void) mdb_vread(rx_srs, MAC_RX_SRS_SIZE, rxaddr);
		mdb_printf("%?p %-24s %3d ",
		    addr, fe.fe_flow_name, fe.fe_rx_srs_cnt);
		for (i = 0; i < MAX_RINGS_PER_GROUP; i++) {
			if (rx_srs[i] == 0)
				continue;
			mdb_printf("%p ", rx_srs[i]);
		}
		mdb_printf("\n");
		break;
	}
	case MAC_FLOW_TX: {
		uintptr_t	tx_srs = 0, txaddr;

		txaddr = addr + OFFSETOF(flow_entry_t, fe_tx_srs);
		(void) mdb_vread(&tx_srs, sizeof (uintptr_t), txaddr);
		mdb_printf("%?p %-32s %?p\n",
		    addr, fe.fe_flow_name, fe.fe_tx_srs);
		break;
	}
	case MAC_FLOW_STATS: {
		uint64_t		totibytes = 0;
		uint64_t		totobytes = 0;
		mac_soft_ring_set_t	*mac_srs;
		mac_rx_stats_t		mac_rx_stat;
		mac_tx_stats_t		mac_tx_stat;
		int			i;

		/*
		 * Sum bytes for all Rx SRS.
		 */
		for (i = 0; i < fe.fe_rx_srs_cnt; i++) {
			mac_srs = (mac_soft_ring_set_t *)(fe.fe_rx_srs[i]);
			if (mdb_vread(&mac_rx_stat, sizeof (mac_rx_stats_t),
			    (uintptr_t)&mac_srs->srs_rx.sr_stat) == -1) {
				mdb_warn("failed to read mac_rx_stats_t at %p",
				    &mac_srs->srs_rx.sr_stat);
				return (DCMD_ERR);
			}

			totibytes += mac_rx_stat.mrs_intrbytes +
			    mac_rx_stat.mrs_pollbytes +
			    mac_rx_stat.mrs_lclbytes;
		}

		/*
		 * Sum bytes for Tx SRS.
		 */
		mac_srs = (mac_soft_ring_set_t *)(fe.fe_tx_srs);
		if (mac_srs != NULL) {
			if (mdb_vread(&mac_tx_stat, sizeof (mac_tx_stats_t),
			    (uintptr_t)&mac_srs->srs_tx.st_stat) == -1) {
				mdb_warn("failed to read max_tx_stats_t at %p",
				    &mac_srs->srs_tx.st_stat);
				return (DCMD_ERR);
			}

			totobytes = mac_tx_stat.mts_obytes;
		}

		mdb_printf("%?p %-32s %16llu %16llu\n",
		    addr, fe.fe_flow_name, totibytes, totobytes);

		break;
	}
	}
	return (DCMD_OK);
}

/*
 * Parse the arguments passed to the dcmd and print all or one flow_entry_t
 * structures
 */
static int
mac_flow_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	uint_t	args = 0;

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("mac_flow", "mac_flow", argc, argv) == -1) {
			mdb_warn("failed to walk 'mac_flow'");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}
	if ((mdb_getopts(argc, argv,
	    'a', MDB_OPT_SETBITS, MAC_FLOW_ATTR, &args,
	    'p', MDB_OPT_SETBITS, MAC_FLOW_PROP, &args,
	    'm', MDB_OPT_SETBITS, MAC_FLOW_MISC, &args,
	    'r', MDB_OPT_SETBITS, MAC_FLOW_RX, &args,
	    't', MDB_OPT_SETBITS, MAC_FLOW_TX, &args,
	    's', MDB_OPT_SETBITS, MAC_FLOW_STATS, &args,
	    'u', MDB_OPT_SETBITS, MAC_FLOW_USER, &args,
	    NULL) != argc)) {
		return (DCMD_USAGE);
	}
	if (argc > 2 || (argc == 2 && !(args & MAC_FLOW_USER)))
		return (DCMD_USAGE);
	/*
	 * If no arguments was specified or just "-u" was specified then
	 * we default to printing basic information of flows.
	 */
	if (args == 0 || args == MAC_FLOW_USER)
		args |= MAC_FLOW_NONE;

	return (mac_flow_dcmd_output(addr, flags, args));
}

static void
mac_flow_help(void)
{
	mdb_printf("If an address is specified, then flow_entry structure at "
	    "that address is printed. Otherwise all the flows in the system "
	    "are printed.\n");
	mdb_printf("Options:\n"
	    "\t-u\tdisplay user defined link & vnic flows.\n"
	    "\t-a\tdisplay flow attributes\n"
	    "\t-p\tdisplay flow properties\n"
	    "\t-r\tdisplay rx side information\n"
	    "\t-t\tdisplay tx side information\n"
	    "\t-s\tdisplay flow statistics\n"
	    "\t-m\tdisplay miscellaneous flow information\n\n");
	mdb_printf("%<u>Interpreting Flow type and Flow flags output.%</u>\n");
	mdb_printf("Flow Types:\n");
	mdb_printf("\t  P --> FLOW_PRIMARY_MAC\n");
	mdb_printf("\t  V --> FLOW_VNIC_MAC\n");
	mdb_printf("\t  M --> FLOW_MCAST\n");
	mdb_printf("\t  O --> FLOW_OTHER\n");
	mdb_printf("\t  U --> FLOW_USER\n");
	mdb_printf("\t NS --> FLOW_NO_STATS\n\n");
	mdb_printf("Flow Flags:\n");
	mdb_printf("\t  Q --> FE_QUIESCE\n");
	mdb_printf("\t  W --> FE_WAITER\n");
	mdb_printf("\t  T --> FE_FLOW_TAB\n");
	mdb_printf("\t  G --> FE_G_FLOW_HASH\n");
	mdb_printf("\t  I --> FE_INCIPIENT\n");
	mdb_printf("\t  C --> FE_CONDEMNED\n");
	mdb_printf("\t NU --> FE_UF_NO_DATAPATH\n");
	mdb_printf("\t NC --> FE_MC_NO_DATAPATH\n");
}

/*
 * called once by the debugger when the mac_flow walk begins.
 */
static int
mac_flow_walk_init(mdb_walk_state_t *wsp)
{
	if (mdb_layered_walk(LAYERED_WALKER_FOR_FLOW, wsp) == -1) {
		mdb_warn("failed to walk 'mac_flow'");
		return (WALK_ERR);
	}
	return (WALK_NEXT);
}

/*
 * Common walker step funciton for flow_entry_t, mac_soft_ring_set_t and
 * mac_ring_t.
 *
 * Steps through each flow_entry_t and calls the callback function. If the
 * user executed ::walk mac_flow, it just prints the address or if the user
 * executed ::mac_flow it displays selected fields of flow_entry_t structure
 * by calling "mac_flow_dcmd"
 */
static int
mac_common_walk_step(mdb_walk_state_t *wsp)
{
	int status;

	if (wsp->walk_addr == 0)
		return (WALK_DONE);

	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
	    wsp->walk_cbdata);

	return (status);
}

static char *
mac_srs_txmode2str(mac_tx_srs_mode_t mode)
{
	switch (mode) {
	case SRS_TX_DEFAULT:
		return ("DEF");
	case SRS_TX_SERIALIZE:
		return ("SER");
	case SRS_TX_FANOUT:
		return ("FO");
	case SRS_TX_BW:
		return ("BW");
	case SRS_TX_BW_FANOUT:
		return ("BWFO");
	case SRS_TX_AGGR:
		return ("AG");
	case SRS_TX_BW_AGGR:
		return ("BWAG");
	}
	return ("--");
}

static void
mac_srs_help(void)
{
	mdb_printf("If an address is specified, then mac_soft_ring_set "
	    "structure at that address is printed. Otherwise all the "
	    "SRS in the system are printed.\n");
	mdb_printf("Options:\n"
	    "\t-r\tdisplay recieve side SRS structures\n"
	    "\t-t\tdisplay transmit side SRS structures\n"
	    "\t-s\tdisplay statistics for RX or TX side\n"
	    "\t-c\tdisplay CPU binding for RX or TX side\n"
	    "\t-v\tverbose flag for CPU binding to list cpus\n"
	    "\t-i\tdisplay mac_ring_t and interrupt information\n"
	    "Note: use -r or -t (to specify RX or TX side respectively) along "
	    "with -c or -s\n");
	mdb_printf("\n%<u>Interpreting TX Modes%</u>\n");
	mdb_printf("\t DEF --> Default\n");
	mdb_printf("\t SER --> Serialize\n");
	mdb_printf("\t  FO --> Fanout\n");
	mdb_printf("\t  BW --> Bandwidth\n");
	mdb_printf("\tBWFO --> Bandwidth Fanout\n");
	mdb_printf("\t  AG --> Aggr\n");
	mdb_printf("\tBWAG --> Bandwidth Aggr\n");
}

/*
 * In verbose mode "::mac_srs -rcv or ::mac_srs -tcv", we print the CPUs
 * assigned to a link and CPUS assigned to the soft rings.
 * 'len' is used for formatting the output and represents the number of
 * spaces between CPU list and Fanout CPU list in the output.
 */
static boolean_t
mac_srs_print_cpu(int *i, uint32_t cnt, uint32_t *cpu_list, int *len)
{
	int		num = 0;

	if (*i == 0)
		mdb_printf("(");
	else
		mdb_printf(" ");
	while (*i < cnt) {
		/* We print 6 CPU's at a time to keep display within 80 cols */
		if (((num + 1) % 7) == 0) {
			if (len != NULL)
				*len = 2;
			return (B_FALSE);
		}
		mdb_printf("%02x%c", cpu_list[*i], ((*i == cnt - 1)?')':','));
		++*i;
		++num;
	}
	if (len != NULL)
		*len = (7 - num) * 3;
	return (B_TRUE);
}

static int
mac_srs_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	uint_t			args = MAC_SRS_NONE;
	mac_soft_ring_set_t	srs;
	mac_client_impl_t	mci;

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("mac_srs", "mac_srs", argc, argv) == -1) {
			mdb_warn("failed to walk 'mac_srs'");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}
	if (mdb_getopts(argc, argv,
	    'r', MDB_OPT_SETBITS, MAC_SRS_RX, &args,
	    't', MDB_OPT_SETBITS, MAC_SRS_TX, &args,
	    'c', MDB_OPT_SETBITS, MAC_SRS_CPU, &args,
	    'v', MDB_OPT_SETBITS, MAC_SRS_VERBOSE, &args,
	    'i', MDB_OPT_SETBITS, MAC_SRS_INTR, &args,
	    's', MDB_OPT_SETBITS, MAC_SRS_STAT, &args,
	    NULL) != argc) {
		return (DCMD_USAGE);
	}

	if (argc > 2)
		return (DCMD_USAGE);

	if (mdb_vread(&srs, sizeof (srs), addr) == -1) {
		mdb_warn("failed to read struct mac_soft_ring_set_s at %p",
		    addr);
		return (DCMD_ERR);
	}
	if (mdb_vread(&mci, sizeof (mci), (uintptr_t)srs.srs_mcip) == -1) {
		mdb_warn("failed to read struct mac_client_impl_t at %p "
		    "for SRS %p", srs.srs_mcip, addr);
		return (DCMD_ERR);
	}

	switch (args) {
	case MAC_SRS_RX: {
		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-20s %-8s %-8s %8s "
			    "%8s %3s\n",
			    "", "", "", "", "MBLK",
			    "Q", "SR");
			mdb_printf("%<u>%?s %-20s %-8s %-8s %8s "
			    "%8s %3s%</u>\n",
			    "ADDR", "LINK_NAME", "STATE", "TYPE", "CNT",
			    "BYTES", "CNT");
		}
		if (srs.srs_type & SRST_TX)
			return (DCMD_OK);
		mdb_printf("%?p %-20s %08x %08x "
		    "%8d %8d %3d\n",
		    addr, mci.mci_name, srs.srs_state, srs.srs_type,
		    srs.srs_count, srs.srs_size, srs.srs_soft_ring_count);
		break;
	}
	case MAC_SRS_TX: {
		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-16s %-4s %-8s "
			    "%-8s %8s %8s %3s\n",
			    "", "", "TX", "",
			    "", "MBLK", "Q", "SR");
			mdb_printf("%<u>%?s %-16s %-4s %-8s "
			    "%-8s %8s %8s %3s%</u>\n",
			    "ADDR", "LINK_NAME", "MODE", "STATE",
			    "TYPE", "CNT", "BYTES", "CNT");
		}
		if (!(srs.srs_type & SRST_TX))
			return (DCMD_OK);

		mdb_printf("%?p %-16s %-4s "
		    "%08x %08x %8d %8d %3d\n",
		    addr, mci.mci_name, mac_srs_txmode2str(srs.srs_tx.st_mode),
		    srs.srs_state, srs.srs_type, srs.srs_count, srs.srs_size,
		    srs.srs_tx_ring_count);
		break;
	}
	case MAC_SRS_RXCPU: {
		mac_cpus_t	mc = srs.srs_cpu;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-20s %-4s %-4s "
			    "%-6s %-4s %-7s\n",
			    "", "", "NUM", "POLL",
			    "WORKER", "INTR", "FANOUT");
			mdb_printf("%<u>%?s %-20s %-4s %-4s "
			    "%-6s %-4s %-7s%</u>\n",
			    "ADDR", "LINK_NAME", "CPUS", "CPU",
			    "CPU", "CPU", "CPU_CNT");
		}
		if ((args & MAC_SRS_RX) && (srs.srs_type & SRST_TX))
			return (DCMD_OK);
		mdb_printf("%?p %-20s %-4d %-4d "
		    "%-6d %-4d %-7d\n",
		    addr, mci.mci_name, mc.mc_ncpus, mc.mc_rx_pollid,
		    mc.mc_rx_workerid, mc.mc_rx_intr_cpu, mc.mc_rx_fanout_cnt);
		break;

	}
	case MAC_SRS_TXCPU: {
		mac_cpus_t	mc = srs.srs_cpu;
		mac_soft_ring_t *s_ringp, s_ring;
		boolean_t	first = B_TRUE;
		int		i;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-12s %?s %8s %8s %8s\n",
			    "", "", "SOFT", "WORKER", "INTR", "RETARGETED");
			mdb_printf("%<u>%?s %-12s %?s %8s %8s %8s%</u>\n",
			    "ADDR", "LINK_NAME", "RING", "CPU", "CPU", "CPU");
		}
		if (!(srs.srs_type & SRST_TX))
			return (DCMD_OK);

		mdb_printf("%?p %-12s ", addr, mci.mci_name);

		/*
		 * Case of no soft rings, print the info from
		 * mac_srs_tx_t.
		 */
		if (srs.srs_tx_ring_count == 0) {
			mdb_printf("%?p %8d %8d %8d\n",
			    0, mc.mc_tx_fanout_cpus[0],
			    mc.mc_tx_intr_cpu[0],
			    mc.mc_tx_retargeted_cpu[0]);
			break;
		}

		for (s_ringp = srs.srs_soft_ring_head, i = 0; s_ringp != NULL;
		    s_ringp = s_ring.s_ring_next, i++) {
			(void) mdb_vread(&s_ring, sizeof (s_ring),
			    (uintptr_t)s_ringp);
			if (first) {
				mdb_printf("%?p %8d %8d %8d\n",
				    s_ringp, mc.mc_tx_fanout_cpus[i],
				    mc.mc_tx_intr_cpu[i],
				    mc.mc_tx_retargeted_cpu[i]);
				first = B_FALSE;
				continue;
			}
			mdb_printf("%?s %-12s %?p %8d %8d %8d\n",
			    "", "", s_ringp, mc.mc_tx_fanout_cpus[i],
			    mc.mc_tx_intr_cpu[i], mc.mc_tx_retargeted_cpu[i]);
		}
		break;
	}
	case MAC_SRS_TXINTR: {
		mac_cpus_t	mc = srs.srs_cpu;
		mac_soft_ring_t *s_ringp, s_ring;
		mac_ring_t	*m_ringp, m_ring;
		boolean_t	first = B_TRUE;
		int		i;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-12s %?s %8s %?s %6s %6s\n",
			    "", "", "SOFT", "WORKER", "MAC", "", "INTR");
			mdb_printf("%<u>%?s %-12s %?s %8s %?s %6s %6s%</u>\n",
			    "ADDR", "LINK_NAME", "RING", "CPU", "RING",
			    "SHARED", "CPU");
		}
		if (!(srs.srs_type & SRST_TX))
			return (DCMD_OK);

		mdb_printf("%?p %-12s ", addr, mci.mci_name);

		/*
		 * Case of no soft rings, print the info from
		 * mac_srs_tx_t.
		 */
		if (srs.srs_tx_ring_count == 0) {
			m_ringp = srs.srs_tx.st_arg2;
			if (m_ringp != NULL) {
				(void) mdb_vread(&m_ring, sizeof (m_ring),
				    (uintptr_t)m_ringp);
				mdb_printf("%?p %8d %?p %6d %6d\n",
				    0, mc.mc_tx_fanout_cpus[0], m_ringp,
				    m_ring.mr_info.mri_intr.mi_ddi_shared,
				    mc.mc_tx_retargeted_cpu[0]);
			} else {
				mdb_printf("%?p %8d %?p %6d %6d\n",
				    0, mc.mc_tx_fanout_cpus[0], 0,
				    0, mc.mc_tx_retargeted_cpu[0]);
			}
			break;
		}

		for (s_ringp = srs.srs_soft_ring_head, i = 0; s_ringp != NULL;
		    s_ringp = s_ring.s_ring_next, i++) {
			(void) mdb_vread(&s_ring, sizeof (s_ring),
			    (uintptr_t)s_ringp);
			m_ringp = s_ring.s_ring_tx_arg2;
			(void) mdb_vread(&m_ring, sizeof (m_ring),
			    (uintptr_t)m_ringp);
			if (first) {
				mdb_printf("%?p %8d %?p %6d %6d\n",
				    s_ringp, mc.mc_tx_fanout_cpus[i],
				    m_ringp,
				    m_ring.mr_info.mri_intr.mi_ddi_shared,
				    mc.mc_tx_retargeted_cpu[i]);
				first = B_FALSE;
				continue;
			}
			mdb_printf("%?s %-12s %?p %8d %?p %6d %6d\n",
			    "", "", s_ringp, mc.mc_tx_fanout_cpus[i],
			    m_ringp, m_ring.mr_info.mri_intr.mi_ddi_shared,
			    mc.mc_tx_retargeted_cpu[i]);
		}
		break;
	}
	case MAC_SRS_RXINTR: {
		mac_cpus_t	mc = srs.srs_cpu;
		mac_ring_t	*m_ringp, m_ring;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-12s %?s %8s %6s %6s\n",
			    "", "", "MAC", "", "POLL", "INTR");
			mdb_printf("%<u>%?s %-12s %?s %8s %6s %6s%</u>\n",
			    "ADDR", "LINK_NAME", "RING", "SHARED", "CPU",
			    "CPU");
		}
		if ((args & MAC_SRS_RX) && (srs.srs_type & SRST_TX))
			return (DCMD_OK);

		mdb_printf("%?p %-12s ", addr, mci.mci_name);

		m_ringp = srs.srs_ring;
		if (m_ringp != NULL) {
			(void) mdb_vread(&m_ring, sizeof (m_ring),
			    (uintptr_t)m_ringp);
			mdb_printf("%?p %8d %6d %6d\n",
			    m_ringp, m_ring.mr_info.mri_intr.mi_ddi_shared,
			    mc.mc_rx_pollid, mc.mc_rx_intr_cpu);
		} else {
			mdb_printf("%?p %8d %6d %6d\n",
			    0, 0, mc.mc_rx_pollid, mc.mc_rx_intr_cpu);
		}
		break;
	}
	case MAC_SRS_RXCPUVERBOSE:
	case MAC_SRS_TXCPUVERBOSE: {
		mac_cpus_t	mc = srs.srs_cpu;
		int		cpu_index = 0, fanout_index = 0, len = 0;
		boolean_t	cpu_done = B_FALSE, fanout_done = B_FALSE;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-20s %-20s %-20s\n",
			    "", "", "CPU_COUNT", "FANOUT_CPU_COUNT");
			mdb_printf("%<u>%?s %-20s "
			    "%-20s %-20s%</u>\n",
			    "ADDR", "LINK_NAME",
			    "(CPU_LIST)", "(CPU_LIST)");
		}
		if (((args & MAC_SRS_TX) && !(srs.srs_type & SRST_TX)) ||
		    ((args & MAC_SRS_RX) && (srs.srs_type & SRST_TX)))
			return (DCMD_OK);
		mdb_printf("%?p %-20s %-20d %-20d\n", addr, mci.mci_name,
		    mc.mc_ncpus, mc.mc_rx_fanout_cnt);
		if (mc.mc_ncpus == 0 && mc.mc_rx_fanout_cnt == 0)
			break;
		/* print all cpus and cpus for soft rings */
		while (!cpu_done || !fanout_done) {
			boolean_t old_value = cpu_done;

			if (!cpu_done) {
				mdb_printf("%?s %20s ", "", "");
				cpu_done = mac_srs_print_cpu(&cpu_index,
				    mc.mc_ncpus, mc.mc_cpus, &len);
			}
			if (!fanout_done) {
				if (old_value)
					mdb_printf("%?s %-40s", "", "");
				else
					mdb_printf("%*s", len, "");
				fanout_done = mac_srs_print_cpu(&fanout_index,
				    mc.mc_rx_fanout_cnt,
				    mc.mc_rx_fanout_cpus, NULL);
			}
			mdb_printf("\n");
		}
		break;
	}
	case MAC_SRS_RXSTAT: {
		mac_rx_stats_t *mac_rx_stat = &srs.srs_rx.sr_stat;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-16s %8s %8s "
			    "%8s %8s %8s\n",
			    "", "", "INTR", "POLL",
			    "CHAIN", "CHAIN", "CHAIN");
			mdb_printf("%<u>%?s %-16s %8s %8s "
			    "%8s %8s %8s%</u>\n",
			    "ADDR", "LINK_NAME", "COUNT", "COUNT",
			    "<10", "10-50", ">50");
		}
		if (srs.srs_type & SRST_TX)
			return (DCMD_OK);
		mdb_printf("%?p %-16s %8d "
		    "%8d %8d "
		    "%8d %8d\n",
		    addr, mci.mci_name, mac_rx_stat->mrs_intrcnt,
		    mac_rx_stat->mrs_pollcnt, mac_rx_stat->mrs_chaincntundr10,
		    mac_rx_stat->mrs_chaincnt10to50,
		    mac_rx_stat->mrs_chaincntover50);
		break;
	}
	case MAC_SRS_TXSTAT: {
		mac_tx_stats_t *mac_tx_stat = &srs.srs_tx.st_stat;
		mac_soft_ring_t *s_ringp, s_ring;
		boolean_t	first = B_TRUE;

		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%?s %-20s %?s %8s %8s %8s\n",
			    "", "", "SOFT", "DROP", "BLOCK", "UNBLOCK");
			mdb_printf("%<u>%?s %-20s %?s %8s %8s %8s%</u>\n",
			    "ADDR", "LINK_NAME", "RING", "COUNT", "COUNT",
			    "COUNT");
		}
		if (!(srs.srs_type & SRST_TX))
			return (DCMD_OK);

		mdb_printf("%?p %-20s ", addr, mci.mci_name);

		/*
		 * Case of no soft rings, print the info from
		 * mac_srs_tx_t.
		 */
		if (srs.srs_tx_ring_count == 0) {
			mdb_printf("%?p %8d %8d %8d\n",
			    0, mac_tx_stat->mts_sdrops,
			    mac_tx_stat->mts_blockcnt,
			    mac_tx_stat->mts_unblockcnt);
			break;
		}

		for (s_ringp = srs.srs_soft_ring_head; s_ringp != NULL;
		    s_ringp = s_ring.s_ring_next) {
			(void) mdb_vread(&s_ring, sizeof (s_ring),
			    (uintptr_t)s_ringp);
			mac_tx_stat = &s_ring.s_st_stat;
			if (first) {
				mdb_printf("%?p %8d %8d %8d\n",
				    s_ringp, mac_tx_stat->mts_sdrops,
				    mac_tx_stat->mts_blockcnt,
				    mac_tx_stat->mts_unblockcnt);
				first = B_FALSE;
				continue;
			}
			mdb_printf("%?s %-20s %?p %8d %8d %8d\n",
			    "", "", s_ringp, mac_tx_stat->mts_sdrops,
			    mac_tx_stat->mts_blockcnt,
			    mac_tx_stat->mts_unblockcnt);
		}
		break;
	}
	case MAC_SRS_NONE: {
		if (DCMD_HDRSPEC(flags)) {
			mdb_printf("%<u>%?s %-20s %?s %?s %-3s%</u>\n",
			    "ADDR", "LINK_NAME", "FLENT", "HW RING", "DIR");
		}
		mdb_printf("%?p %-20s %?p %?p "
		    "%-3s ",
		    addr, mci.mci_name, srs.srs_flent, srs.srs_ring,
		    (srs.srs_type & SRST_TX ? "TX" : "RX"));
		break;
	}
	default:
		return (DCMD_USAGE);
	}
	return (DCMD_OK);
}

static int
mac_srs_walk_init(mdb_walk_state_t *wsp)
{
	if (mdb_layered_walk(LAYERED_WALKER_FOR_SRS, wsp) == -1) {
		mdb_warn("failed to walk 'mac_srs'");
		return (WALK_ERR);
	}
	return (WALK_NEXT);
}

static char *
mac_ring_state2str(mac_ring_state_t state)
{
	switch (state) {
	case MR_FREE:
		return ("free");
	case MR_NEWLY_ADDED:
		return ("new");
	case MR_INUSE:
		return ("inuse");
	}
	return ("--");
}

static char *
mac_ring_classify2str(mac_classify_type_t classify)
{
	switch (classify) {
	case MAC_NO_CLASSIFIER:
		return ("no");
	case MAC_SW_CLASSIFIER:
		return ("sw");
	case MAC_HW_CLASSIFIER:
		return ("hw");
	case MAC_PASSTHRU_CLASSIFIER:
		return ("pass");
	}
	return ("--");
}

static int
mac_ring_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	mac_ring_t		ring;
	mac_group_t		group;
	flow_entry_t		flent;
	mac_soft_ring_set_t	srs;

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("mac_ring", "mac_ring", argc, argv) == -1) {
			mdb_warn("failed to walk 'mac_ring'");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}
	if (mdb_vread(&ring, sizeof (ring), addr) == -1) {
		mdb_warn("failed to read struct mac_ring_s at %p", addr);
		return (DCMD_ERR);
	}
	bzero(&flent, sizeof (flent));
	if (mdb_vread(&srs, sizeof (srs), (uintptr_t)ring.mr_srs) != -1) {
		(void) mdb_vread(&flent, sizeof (flent),
		    (uintptr_t)srs.srs_flent);
	}
	(void) mdb_vread(&group, sizeof (group), (uintptr_t)ring.mr_gh);
	if (DCMD_HDRSPEC(flags)) {
		mdb_printf("%<u>%?s %4s %5s %4s %?s "
		    "%5s %?s %?s %s %</u>\n",
		    "ADDR", "TYPE", "STATE", "FLAG", "GROUP",
		    "CLASS", "MIP", "SRS", "FLOW NAME");
	}
	mdb_printf("%?p %-4s "
	    "%5s %04x "
	    "%?p %-5s "
	    "%?p %?p %s\n",
	    addr, ((ring.mr_type == 1)? "RX" : "TX"),
	    mac_ring_state2str(ring.mr_state), ring.mr_flag,
	    ring.mr_gh, mac_ring_classify2str(ring.mr_classify_type),
	    group.mrg_mh, ring.mr_srs, flent.fe_flow_name);
	return (DCMD_OK);
}

static int
mac_ring_walk_init(mdb_walk_state_t *wsp)
{
	if (mdb_layered_walk(LAYERED_WALKER_FOR_RING, wsp) == -1) {
		mdb_warn("failed to walk `mac_ring`");
		return (WALK_ERR);
	}
	return (WALK_NEXT);
}

static void
mac_ring_help(void)
{
	mdb_printf("If an address is specified, then mac_ring_t "
	    "structure at that address is printed. Otherwise all the "
	    "hardware rings in the system are printed.\n");
}

/*
 * To walk groups we have to have our own somewhat-complicated state machine. We
 * basically start by walking the mac_impl_t walker as all groups are stored off
 * of the various mac_impl_t in the system. The tx and rx rings are kept
 * separately. So we'll need to walk through all the rx rings and then all of
 * the tx rings.
 */
static int
mac_group_walk_init(mdb_walk_state_t *wsp)
{
	int ret;

	if (wsp->walk_addr != 0) {
		mdb_warn("non-global walks are not supported\n");
		return (WALK_ERR);
	}

	if ((ret = mdb_layered_walk(LAYERED_WALKER_FOR_GROUP, wsp)) == -1) {
		mdb_warn("couldn't walk '%s'", LAYERED_WALKER_FOR_GROUP);
		return (ret);
	}

	return (WALK_NEXT);
}

static int
mac_group_walk_step(mdb_walk_state_t *wsp)
{
	int ret;
	mac_impl_t mi;
	mac_group_t mg;
	uintptr_t mgp;

	/*
	 * Nothing to do if we can't find the layer above us. But the kmem
	 * walkers are a bit unsporting, they don't actually read in the data
	 * for us.
	 */
	if (wsp->walk_addr == 0)
		return (WALK_DONE);

	if (mdb_vread(&mi, sizeof (mac_impl_t), wsp->walk_addr) == -1) {
		mdb_warn("failed to read mac_impl_t at %p", wsp->walk_addr);
		return (DCMD_ERR);
	}

	/*
	 * First go for rx groups, then tx groups.
	 */
	mgp = (uintptr_t)mi.mi_rx_groups;
	while (mgp != 0) {
		if (mdb_vread(&mg, sizeof (mac_group_t), mgp) == -1) {
			mdb_warn("failed to read mac_group_t at %p", mgp);
			return (WALK_ERR);
		}

		ret = wsp->walk_callback(mgp, &mg, wsp->walk_cbdata);
		if (ret != WALK_NEXT)
			return (ret);
		mgp = (uintptr_t)mg.mrg_next;
	}

	mgp = (uintptr_t)mi.mi_tx_groups;
	while (mgp != 0) {
		if (mdb_vread(&mg, sizeof (mac_group_t), mgp) == -1) {
			mdb_warn("failed to read mac_group_t at %p", mgp);
			return (WALK_ERR);
		}

		ret = wsp->walk_callback(mgp, &mg, wsp->walk_cbdata);
		if (ret != WALK_NEXT)
			return (ret);
		mgp = (uintptr_t)mg.mrg_next;
	}

	return (WALK_NEXT);
}

static int
mac_group_count_clients(mac_group_t *mgp)
{
	int clients = 0;
	uintptr_t mcp = (uintptr_t)mgp->mrg_clients;

	while (mcp != 0) {
		mac_grp_client_t c;

		if (mdb_vread(&c, sizeof (c), mcp) == -1) {
			mdb_warn("failed to read mac_grp_client_t at %p", mcp);
			return (-1);
		}
		clients++;
		mcp = (uintptr_t)c.mgc_next;
	}

	return (clients);
}

static const char *
mac_group_type(mac_group_t *mgp)
{
	const char *ret;

	switch (mgp->mrg_type) {
	case MAC_RING_TYPE_RX:
		ret = "RECEIVE";
		break;
	case MAC_RING_TYPE_TX:
		ret = "TRANSMIT";
		break;
	default:
		ret = "UNKNOWN";
		break;
	}

	return (ret);
}

static const char *
mac_group_state(mac_group_t *mgp)
{
	const char *ret;

	switch (mgp->mrg_state) {
	case MAC_GROUP_STATE_UNINIT:
		ret = "UNINT";
		break;
	case MAC_GROUP_STATE_REGISTERED:
		ret = "REGISTERED";
		break;
	case MAC_GROUP_STATE_RESERVED:
		ret = "RESERVED";
		break;
	case MAC_GROUP_STATE_SHARED:
		ret = "SHARED";
		break;
	default:
		ret = "UNKNOWN";
		break;
	}

	return (ret);
}

static int
mac_group_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	uint_t		args = MAC_SRS_NONE;
	mac_group_t	mg;
	int		clients;

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("mac_group", "mac_group", argc, argv) == -1) {
			mdb_warn("failed to walk 'mac_group'");
			return (DCMD_ERR);
		}

		return (DCMD_OK);
	}

	if (mdb_getopts(argc, argv,
	    'r', MDB_OPT_SETBITS, MAC_GROUP_RX, &args,
	    't', MDB_OPT_SETBITS, MAC_GROUP_TX, &args,
	    'u', MDB_OPT_SETBITS, MAC_GROUP_UNINIT, &args,
	    NULL) != argc)
		return (DCMD_USAGE);

	if (mdb_vread(&mg, sizeof (mac_group_t), addr) == -1) {
		mdb_warn("failed to read mac_group_t at %p", addr);
		return (DCMD_ERR);
	}

	if (DCMD_HDRSPEC(flags) && !(flags & DCMD_PIPE_OUT)) {
		mdb_printf("%<u>%-?s %-8s %-10s %6s %8s %-?s%</u>\n",
		    "ADDR", "TYPE", "STATE", "NRINGS", "NCLIENTS", "RINGS");
	}

	if ((args & MAC_GROUP_RX) != 0 && mg.mrg_type != MAC_RING_TYPE_RX)
		return (DCMD_OK);
	if ((args & MAC_GROUP_TX) != 0 && mg.mrg_type != MAC_RING_TYPE_TX)
		return (DCMD_OK);

	/*
	 * By default, don't show uninitialized groups. They're not very
	 * interesting. They have no rings and no clients.
	 */
	if (mg.mrg_state == MAC_GROUP_STATE_UNINIT &&
	    (args & MAC_GROUP_UNINIT) == 0)
		return (DCMD_OK);

	if (flags & DCMD_PIPE_OUT) {
		mdb_printf("%lr\n", addr);
		return (DCMD_OK);
	}

	clients = mac_group_count_clients(&mg);
	mdb_printf("%?p %-8s %-10s %6d %8d %?p\n", addr, mac_group_type(&mg),
	    mac_group_state(&mg), mg.mrg_cur_count, clients, mg.mrg_rings);

	return (DCMD_OK);
}

/* Supported dee-commands */
static const mdb_dcmd_t dcmds[] = {
	{"mac_flow", "?[-u] [-aprtsm]", "display Flow Entry structures",
	    mac_flow_dcmd, mac_flow_help},
	{"mac_group", "?[-rtu]", "display MAC Ring Groups", mac_group_dcmd,
	    NULL },
	{"mac_srs", "?[ -r[i|s|c[v]] | -t[i|s|c[v]] ]",
	    "display MAC Soft Ring Set" " structures", mac_srs_dcmd,
	    mac_srs_help},
	{"mac_ring", "?", "display MAC ring (hardware) structures",
	    mac_ring_dcmd, mac_ring_help},
	{ NULL }
};

/* Supported walkers */
static const mdb_walker_t walkers[] = {
	{"mac_flow", "walk list of flow entry structures", mac_flow_walk_init,
	    mac_common_walk_step, NULL, NULL},
	{"mac_group", "walk list of ring group structures", mac_group_walk_init,
	    mac_group_walk_step, NULL, NULL},
	{"mac_srs", "walk list of mac soft ring set structures",
	    mac_srs_walk_init, mac_common_walk_step, NULL, NULL},
	{"mac_ring", "walk list of mac ring structures", mac_ring_walk_init,
	    mac_common_walk_step, NULL, NULL},
	{ NULL }
};

static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}