1da14cebeSEric Cheng /* 2da14cebeSEric Cheng * CDDL HEADER START 3da14cebeSEric Cheng * 4da14cebeSEric Cheng * The contents of this file are subject to the terms of the 5da14cebeSEric Cheng * Common Development and Distribution License (the "License"). 6da14cebeSEric Cheng * You may not use this file except in compliance with the License. 7da14cebeSEric Cheng * 8da14cebeSEric Cheng * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9da14cebeSEric Cheng * or http://www.opensolaris.org/os/licensing. 10da14cebeSEric Cheng * See the License for the specific language governing permissions 11da14cebeSEric Cheng * and limitations under the License. 12da14cebeSEric Cheng * 13da14cebeSEric Cheng * When distributing Covered Code, include this CDDL HEADER in each 14da14cebeSEric Cheng * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15da14cebeSEric Cheng * If applicable, add the following below this CDDL HEADER, with the 16da14cebeSEric Cheng * fields enclosed by brackets "[]" replaced with your own identifying 17da14cebeSEric Cheng * information: Portions Copyright [yyyy] [name of copyright owner] 18da14cebeSEric Cheng * 19da14cebeSEric Cheng * CDDL HEADER END 20da14cebeSEric Cheng */ 21da14cebeSEric Cheng /* 22da000602SGirish Moodalbail * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23da14cebeSEric Cheng * Use is subject to license terms. 24da14cebeSEric Cheng */ 25da14cebeSEric Cheng 26da14cebeSEric Cheng #include <stdio.h> 27da14cebeSEric Cheng #include <locale.h> 28da14cebeSEric Cheng #include <stdarg.h> 29da14cebeSEric Cheng #include <stdlib.h> 30da14cebeSEric Cheng #include <fcntl.h> 31da14cebeSEric Cheng #include <string.h> 32da14cebeSEric Cheng #include <stropts.h> 33da14cebeSEric Cheng #include <errno.h> 34da14cebeSEric Cheng #include <kstat.h> 35da14cebeSEric Cheng #include <strings.h> 36da14cebeSEric Cheng #include <getopt.h> 37da14cebeSEric Cheng #include <unistd.h> 38da14cebeSEric Cheng #include <priv.h> 39da14cebeSEric Cheng #include <netdb.h> 40da14cebeSEric Cheng #include <libintl.h> 41da14cebeSEric Cheng #include <libdlflow.h> 42da14cebeSEric Cheng #include <libdllink.h> 43da14cebeSEric Cheng #include <libdlstat.h> 44da14cebeSEric Cheng #include <sys/types.h> 45da14cebeSEric Cheng #include <sys/socket.h> 46da14cebeSEric Cheng #include <netinet/in.h> 47da14cebeSEric Cheng #include <arpa/inet.h> 48da14cebeSEric Cheng #include <sys/ethernet.h> 49da14cebeSEric Cheng #include <inet/ip.h> 50da14cebeSEric Cheng #include <inet/ip6.h> 51da14cebeSEric Cheng #include <stddef.h> 52da14cebeSEric Cheng 53da14cebeSEric Cheng #define CMD_TYPE_ANY 0xffffffff 54da14cebeSEric Cheng #define STR_UNDEF_VAL "--" 55da14cebeSEric Cheng 56da14cebeSEric Cheng 57da14cebeSEric Cheng /* 58da14cebeSEric Cheng * data structures and routines for printing output. 59da14cebeSEric Cheng */ 60da14cebeSEric Cheng 61da14cebeSEric Cheng typedef struct print_field_s { 62da14cebeSEric Cheng const char *pf_name; 63da14cebeSEric Cheng const char *pf_header; 64da14cebeSEric Cheng uint_t pf_width; 65da14cebeSEric Cheng union { 66da14cebeSEric Cheng uint_t _pf_index; 67da14cebeSEric Cheng size_t _pf_offset; 68da14cebeSEric Cheng }_pf_un; 69da14cebeSEric Cheng #define pf_index _pf_un._pf_index 70da14cebeSEric Cheng #define pf_offset _pf_un._pf_offset; 71da14cebeSEric Cheng uint_t pf_cmdtype; 72da14cebeSEric Cheng } print_field_t; 73da14cebeSEric Cheng 74da14cebeSEric Cheng typedef struct print_state_s { 75da14cebeSEric Cheng print_field_t **ps_fields; 76da14cebeSEric Cheng uint_t ps_nfields; 77da14cebeSEric Cheng boolean_t ps_lastfield; 78da14cebeSEric Cheng uint_t ps_overflow; 79da14cebeSEric Cheng } print_state_t; 80da14cebeSEric Cheng 81da14cebeSEric Cheng typedef struct show_usage_state_s { 82da14cebeSEric Cheng boolean_t us_plot; 83da14cebeSEric Cheng boolean_t us_parseable; 84da14cebeSEric Cheng boolean_t us_printheader; 85da14cebeSEric Cheng boolean_t us_first; 86da14cebeSEric Cheng print_state_t us_print; 87da14cebeSEric Cheng } show_usage_state_t; 88da14cebeSEric Cheng 89da14cebeSEric Cheng typedef char *(*print_callback_t)(print_field_t *, void *); 90da14cebeSEric Cheng static print_field_t **parse_output_fields(char *, print_field_t *, int, 91da14cebeSEric Cheng uint_t, uint_t *); 92da14cebeSEric Cheng 93da14cebeSEric Cheng static void print_header(print_state_t *); 94da14cebeSEric Cheng static void print_field(print_state_t *, print_field_t *, const char *, 95da14cebeSEric Cheng boolean_t); 96da14cebeSEric Cheng 97da14cebeSEric Cheng static void flowadm_print_output(print_state_t *, boolean_t, 98da14cebeSEric Cheng print_callback_t, void *); 99da14cebeSEric Cheng 100da14cebeSEric Cheng /* 101da14cebeSEric Cheng * helper function that, when invoked as flowadm(print_field(pf, buf) 102da14cebeSEric Cheng * prints string which is offset by pf->pf_offset within buf. 103da14cebeSEric Cheng */ 104da14cebeSEric Cheng static char *flowadm_print_field(print_field_t *, void *); 105da14cebeSEric Cheng 106da14cebeSEric Cheng #define MAX_FIELD_LEN 32 107da14cebeSEric Cheng 108da14cebeSEric Cheng typedef void cmdfunc_t(int, char **); 109da14cebeSEric Cheng 110da14cebeSEric Cheng static cmdfunc_t do_add_flow, do_remove_flow, do_init_flow, do_show_flow; 111da14cebeSEric Cheng static cmdfunc_t do_show_flowprop, do_set_flowprop, do_reset_flowprop; 112da14cebeSEric Cheng static cmdfunc_t do_show_usage; 113da14cebeSEric Cheng 114da14cebeSEric Cheng static int show_flow(dladm_flow_attr_t *, void *); 1154ac67f02SAnurag S. Maskey static int show_flows_onelink(dladm_handle_t, datalink_id_t, void *); 116da14cebeSEric Cheng 117da14cebeSEric Cheng static void flow_stats(const char *, datalink_id_t, uint_t); 118da14cebeSEric Cheng static void get_flow_stats(const char *, pktsum_t *); 119da14cebeSEric Cheng static int show_flow_stats(dladm_flow_attr_t *, void *); 1204ac67f02SAnurag S. Maskey static int show_link_flow_stats(dladm_handle_t, datalink_id_t, void *); 121da14cebeSEric Cheng 122da14cebeSEric Cheng static int remove_flow(dladm_flow_attr_t *, void *); 123da14cebeSEric Cheng 124da14cebeSEric Cheng static int show_flowprop(dladm_flow_attr_t *, void *); 125da14cebeSEric Cheng static void show_flowprop_one_flow(void *, const char *); 1264ac67f02SAnurag S. Maskey static int show_flowprop_onelink(dladm_handle_t, datalink_id_t, void *); 127da14cebeSEric Cheng 128da14cebeSEric Cheng static void die(const char *, ...); 129da14cebeSEric Cheng static void die_optdup(int); 130da14cebeSEric Cheng static void die_opterr(int, int); 131da14cebeSEric Cheng static void die_dlerr(dladm_status_t, const char *, ...); 132da14cebeSEric Cheng static void warn(const char *, ...); 133da14cebeSEric Cheng static void warn_dlerr(dladm_status_t, const char *, ...); 134da14cebeSEric Cheng 135da14cebeSEric Cheng typedef struct cmd { 136da14cebeSEric Cheng char *c_name; 137da14cebeSEric Cheng void (*c_fn)(int, char **); 138da14cebeSEric Cheng } cmd_t; 139da14cebeSEric Cheng 140da14cebeSEric Cheng static cmd_t cmds[] = { 141da14cebeSEric Cheng { "add-flow", do_add_flow }, 142da14cebeSEric Cheng { "remove-flow", do_remove_flow }, 143da14cebeSEric Cheng { "show-flowprop", do_show_flowprop }, 144da14cebeSEric Cheng { "set-flowprop", do_set_flowprop }, 145da14cebeSEric Cheng { "reset-flowprop", do_reset_flowprop }, 146da14cebeSEric Cheng { "show-flow", do_show_flow }, 147da14cebeSEric Cheng { "init-flow", do_init_flow }, 148da14cebeSEric Cheng { "show-usage", do_show_usage } 149da14cebeSEric Cheng }; 150da14cebeSEric Cheng 151da14cebeSEric Cheng static const struct option longopts[] = { 152da14cebeSEric Cheng {"link", required_argument, 0, 'l'}, 153da14cebeSEric Cheng {"parseable", no_argument, 0, 'p'}, 154da14cebeSEric Cheng {"statistics", no_argument, 0, 's'}, 155da14cebeSEric Cheng {"interval", required_argument, 0, 'i'}, 156da14cebeSEric Cheng {"temporary", no_argument, 0, 't'}, 157da14cebeSEric Cheng {"root-dir", required_argument, 0, 'R'}, 158da14cebeSEric Cheng { 0, 0, 0, 0 } 159da14cebeSEric Cheng }; 160da14cebeSEric Cheng 161da14cebeSEric Cheng static const struct option prop_longopts[] = { 162da14cebeSEric Cheng {"link", required_argument, 0, 'l'}, 163da14cebeSEric Cheng {"temporary", no_argument, 0, 't'}, 164da14cebeSEric Cheng {"root-dir", required_argument, 0, 'R'}, 165da14cebeSEric Cheng {"prop", required_argument, 0, 'p'}, 166da14cebeSEric Cheng {"attr", required_argument, 0, 'a'}, 167da14cebeSEric Cheng { 0, 0, 0, 0 } 168da14cebeSEric Cheng }; 169da14cebeSEric Cheng 170da14cebeSEric Cheng /* 171da14cebeSEric Cheng * structures for 'flowadm show-flow' 172da14cebeSEric Cheng */ 173da14cebeSEric Cheng 174da14cebeSEric Cheng typedef struct show_flow_state { 175da14cebeSEric Cheng boolean_t fs_firstonly; 176da14cebeSEric Cheng boolean_t fs_donefirst; 177da14cebeSEric Cheng pktsum_t fs_prevstats; 178da14cebeSEric Cheng uint32_t fs_flags; 179da14cebeSEric Cheng dladm_status_t fs_status; 180da14cebeSEric Cheng print_state_t fs_print; 181da14cebeSEric Cheng const char *fs_flow; 182da14cebeSEric Cheng const char *fs_link; 183da14cebeSEric Cheng boolean_t fs_parseable; 184da14cebeSEric Cheng boolean_t fs_printheader; 185da14cebeSEric Cheng boolean_t fs_persist; 186da14cebeSEric Cheng boolean_t fs_stats; 187da14cebeSEric Cheng uint64_t fs_mask; 188da14cebeSEric Cheng } show_flow_state_t; 189da14cebeSEric Cheng 190da14cebeSEric Cheng /* 191da14cebeSEric Cheng * structures for 'flowadm remove-flow' 192da14cebeSEric Cheng */ 193da14cebeSEric Cheng 194da14cebeSEric Cheng typedef struct remove_flow_state { 195da14cebeSEric Cheng boolean_t fs_tempop; 196da14cebeSEric Cheng const char *fs_altroot; 197da14cebeSEric Cheng dladm_status_t fs_status; 198da14cebeSEric Cheng } remove_flow_state_t; 199da14cebeSEric Cheng 200da14cebeSEric Cheng typedef struct flow_args_s { 201da14cebeSEric Cheng const char *fa_link; 202da14cebeSEric Cheng int fa_attrno; /* -1 indicates flow itself */ 203da14cebeSEric Cheng uint64_t fa_mask; 204da14cebeSEric Cheng dladm_flow_attr_t *fa_finfop; 205da14cebeSEric Cheng dladm_status_t *fa_status; 206da14cebeSEric Cheng boolean_t fa_parseable; 207da14cebeSEric Cheng } flow_args_t; 208da14cebeSEric Cheng 209da14cebeSEric Cheng #define PROTO_MAXSTR_LEN 7 210da14cebeSEric Cheng #define PORT_MAXSTR_LEN 6 211da14cebeSEric Cheng #define DSFIELD_MAXSTR_LEN 10 212da14cebeSEric Cheng 213da14cebeSEric Cheng typedef struct flow_fields_buf_s 214da14cebeSEric Cheng { 215da000602SGirish Moodalbail char flow_name[MAXFLOWNAMELEN]; 216da14cebeSEric Cheng char flow_link[MAXLINKNAMELEN]; 217da14cebeSEric Cheng char flow_ipaddr[INET6_ADDRSTRLEN+4]; 218da14cebeSEric Cheng char flow_proto[PROTO_MAXSTR_LEN]; 219da14cebeSEric Cheng char flow_port[PORT_MAXSTR_LEN]; 220da14cebeSEric Cheng char flow_dsfield[DSFIELD_MAXSTR_LEN]; 221da14cebeSEric Cheng } flow_fields_buf_t; 222da14cebeSEric Cheng 223da14cebeSEric Cheng static print_field_t flow_fields[] = { 224da14cebeSEric Cheng /* name, header, field width, index, cmdtype */ 225da14cebeSEric Cheng { "flow", "FLOW", 11, 226da14cebeSEric Cheng offsetof(flow_fields_buf_t, flow_name), CMD_TYPE_ANY}, 227da14cebeSEric Cheng { "link", "LINK", 11, 228da14cebeSEric Cheng offsetof(flow_fields_buf_t, flow_link), CMD_TYPE_ANY}, 229da14cebeSEric Cheng { "ipaddr", "IPADDR", 30, 230da14cebeSEric Cheng offsetof(flow_fields_buf_t, flow_ipaddr), CMD_TYPE_ANY}, 231*ad091ee1SMichael Lim { "proto", "PROTO", 6, 232da14cebeSEric Cheng offsetof(flow_fields_buf_t, flow_proto), CMD_TYPE_ANY}, 233da14cebeSEric Cheng { "port", "PORT", 7, 234da14cebeSEric Cheng offsetof(flow_fields_buf_t, flow_port), CMD_TYPE_ANY}, 235*ad091ee1SMichael Lim { "dsfld", "DSFLD", 9, 236da14cebeSEric Cheng offsetof(flow_fields_buf_t, flow_dsfield), CMD_TYPE_ANY}} 237da14cebeSEric Cheng ; 238da14cebeSEric Cheng 239da14cebeSEric Cheng #define FLOW_MAX_FIELDS (sizeof (flow_fields) / sizeof (print_field_t)) 240da14cebeSEric Cheng 241da14cebeSEric Cheng /* 242da14cebeSEric Cheng * structures for 'flowadm show-flowprop' 243da14cebeSEric Cheng */ 244da14cebeSEric Cheng typedef enum { 245da14cebeSEric Cheng FLOWPROP_FLOW, 246da14cebeSEric Cheng FLOWPROP_PROPERTY, 247da14cebeSEric Cheng FLOWPROP_VALUE, 248da14cebeSEric Cheng FLOWPROP_DEFAULT, 249da14cebeSEric Cheng FLOWPROP_POSSIBLE 250da14cebeSEric Cheng } flowprop_field_index_t; 251da14cebeSEric Cheng 252da14cebeSEric Cheng static print_field_t flowprop_fields[] = { 253da14cebeSEric Cheng /* name, header, fieldwidth, index, cmdtype */ 254da14cebeSEric Cheng { "flow", "FLOW", 12, FLOWPROP_FLOW, CMD_TYPE_ANY}, 255da14cebeSEric Cheng { "property", "PROPERTY", 15, FLOWPROP_PROPERTY, CMD_TYPE_ANY}, 256da14cebeSEric Cheng { "value", "VALUE", 14, FLOWPROP_VALUE, CMD_TYPE_ANY}, 257da14cebeSEric Cheng { "default", "DEFAULT", 14, FLOWPROP_DEFAULT, CMD_TYPE_ANY}, 258da14cebeSEric Cheng { "possible", "POSSIBLE", 20, FLOWPROP_POSSIBLE, CMD_TYPE_ANY}} 259da14cebeSEric Cheng ; 260da14cebeSEric Cheng #define FLOWPROP_MAX_FIELDS \ 261da14cebeSEric Cheng (sizeof (flowprop_fields) / sizeof (print_field_t)) 262da14cebeSEric Cheng 263da14cebeSEric Cheng #define MAX_PROP_LINE 512 264da14cebeSEric Cheng 265da14cebeSEric Cheng typedef struct show_flowprop_state { 266da14cebeSEric Cheng const char *fs_flow; 267da14cebeSEric Cheng datalink_id_t fs_linkid; 268da14cebeSEric Cheng char *fs_line; 269da14cebeSEric Cheng char **fs_propvals; 270da14cebeSEric Cheng dladm_arg_list_t *fs_proplist; 271da14cebeSEric Cheng boolean_t fs_parseable; 272da14cebeSEric Cheng boolean_t fs_persist; 273da14cebeSEric Cheng boolean_t fs_header; 274da14cebeSEric Cheng dladm_status_t fs_status; 275da14cebeSEric Cheng dladm_status_t fs_retstatus; 276da14cebeSEric Cheng print_state_t fs_print; 277da14cebeSEric Cheng } show_flowprop_state_t; 278da14cebeSEric Cheng 279da14cebeSEric Cheng typedef struct set_flowprop_state { 280da14cebeSEric Cheng const char *fs_name; 281da14cebeSEric Cheng boolean_t fs_reset; 282da14cebeSEric Cheng boolean_t fs_temp; 283da14cebeSEric Cheng dladm_status_t fs_status; 284da14cebeSEric Cheng } set_flowprop_state_t; 285da14cebeSEric Cheng 286da14cebeSEric Cheng typedef struct flowprop_args_s { 287da14cebeSEric Cheng show_flowprop_state_t *fs_state; 288da14cebeSEric Cheng char *fs_propname; 289da14cebeSEric Cheng char *fs_flowname; 290da14cebeSEric Cheng } flowprop_args_t; 291da14cebeSEric Cheng 292da14cebeSEric Cheng /* 293da14cebeSEric Cheng * structures for 'flow show-usage' 294da14cebeSEric Cheng */ 295da14cebeSEric Cheng 296da14cebeSEric Cheng typedef struct usage_fields_buf_s { 297da14cebeSEric Cheng char usage_flow[12]; 298da14cebeSEric Cheng char usage_duration[10]; 299da14cebeSEric Cheng char usage_ipackets[9]; 300da14cebeSEric Cheng char usage_rbytes[10]; 301da14cebeSEric Cheng char usage_opackets[9]; 302da14cebeSEric Cheng char usage_obytes[10]; 303da14cebeSEric Cheng char usage_bandwidth[14]; 304da14cebeSEric Cheng } usage_fields_buf_t; 305da14cebeSEric Cheng 306da14cebeSEric Cheng static print_field_t usage_fields[] = { 307da14cebeSEric Cheng /* name, header, field width, offset, cmdtype */ 308da14cebeSEric Cheng { "flow", "FLOW", 12, 309da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_flow), CMD_TYPE_ANY}, 310da14cebeSEric Cheng { "duration", "DURATION", 10, 311da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_duration), CMD_TYPE_ANY}, 312da14cebeSEric Cheng { "ipackets", "IPACKETS", 9, 313da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_ipackets), CMD_TYPE_ANY}, 314da14cebeSEric Cheng { "rbytes", "RBYTES", 10, 315da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_rbytes), CMD_TYPE_ANY}, 316da14cebeSEric Cheng { "opackets", "OPACKETS", 9, 317da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_opackets), CMD_TYPE_ANY}, 318da14cebeSEric Cheng { "obytes", "OBYTES", 10, 319da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_obytes), CMD_TYPE_ANY}, 320da14cebeSEric Cheng { "bandwidth", "BANDWIDTH", 14, 321da14cebeSEric Cheng offsetof(usage_fields_buf_t, usage_bandwidth), CMD_TYPE_ANY}} 322da14cebeSEric Cheng ; 323da14cebeSEric Cheng 324da14cebeSEric Cheng #define USAGE_MAX_FIELDS (sizeof (usage_fields) / sizeof (print_field_t)) 325da14cebeSEric Cheng 326da14cebeSEric Cheng /* 327da14cebeSEric Cheng * structures for 'dladm show-usage link' 328da14cebeSEric Cheng */ 329da14cebeSEric Cheng 330da14cebeSEric Cheng typedef struct usage_l_fields_buf_s { 331da14cebeSEric Cheng char usage_l_flow[12]; 332da14cebeSEric Cheng char usage_l_stime[13]; 333da14cebeSEric Cheng char usage_l_etime[13]; 334da14cebeSEric Cheng char usage_l_rbytes[8]; 335da14cebeSEric Cheng char usage_l_obytes[8]; 336da14cebeSEric Cheng char usage_l_bandwidth[14]; 337da14cebeSEric Cheng } usage_l_fields_buf_t; 338da14cebeSEric Cheng 339da14cebeSEric Cheng static print_field_t usage_l_fields[] = { 340da14cebeSEric Cheng /* name, header, field width, offset, cmdtype */ 341da14cebeSEric Cheng { "flow", "FLOW", 12, 342da14cebeSEric Cheng offsetof(usage_l_fields_buf_t, usage_l_flow), CMD_TYPE_ANY}, 343da14cebeSEric Cheng { "start", "START", 13, 344da14cebeSEric Cheng offsetof(usage_l_fields_buf_t, usage_l_stime), CMD_TYPE_ANY}, 345da14cebeSEric Cheng { "end", "END", 13, 346da14cebeSEric Cheng offsetof(usage_l_fields_buf_t, usage_l_etime), CMD_TYPE_ANY}, 347da14cebeSEric Cheng { "rbytes", "RBYTES", 8, 348da14cebeSEric Cheng offsetof(usage_l_fields_buf_t, usage_l_rbytes), CMD_TYPE_ANY}, 349da14cebeSEric Cheng { "obytes", "OBYTES", 8, 350da14cebeSEric Cheng offsetof(usage_l_fields_buf_t, usage_l_obytes), CMD_TYPE_ANY}, 351da14cebeSEric Cheng { "bandwidth", "BANDWIDTH", 14, 352da14cebeSEric Cheng offsetof(usage_l_fields_buf_t, usage_l_bandwidth), CMD_TYPE_ANY}} 353da14cebeSEric Cheng ; 354da14cebeSEric Cheng 355da14cebeSEric Cheng #define USAGE_L_MAX_FIELDS \ 356da14cebeSEric Cheng (sizeof (usage_l_fields) /sizeof (print_field_t)) 357da14cebeSEric Cheng 358da14cebeSEric Cheng #define PRI_HI 100 359da14cebeSEric Cheng #define PRI_LO 10 360da14cebeSEric Cheng #define PRI_NORM 50 361da14cebeSEric Cheng 362da14cebeSEric Cheng #define FLOWADM_CONF "/etc/dladm/flowadm.conf" 363da14cebeSEric Cheng #define BLANK_LINE(s) ((s[0] == '\0') || (s[0] == '#') || (s[0] == '\n')) 364da14cebeSEric Cheng 365da14cebeSEric Cheng static char *progname; 366da14cebeSEric Cheng 367da14cebeSEric Cheng boolean_t t_arg = B_FALSE; /* changes are persistent */ 368da14cebeSEric Cheng char *altroot = NULL; 369da14cebeSEric Cheng 3704ac67f02SAnurag S. Maskey /* 3714ac67f02SAnurag S. Maskey * Handle to libdladm. Opened in main() before the sub-command 3724ac67f02SAnurag S. Maskey * specific function is called. 3734ac67f02SAnurag S. Maskey */ 3744ac67f02SAnurag S. Maskey static dladm_handle_t handle = NULL; 3754ac67f02SAnurag S. Maskey 376da14cebeSEric Cheng static const char *attr_table[] = 377da14cebeSEric Cheng {"local_ip", "remote_ip", "transport", "local_port", "dsfield"}; 378da14cebeSEric Cheng 379da14cebeSEric Cheng #define NATTR (sizeof (attr_table)/sizeof (char *)) 380da14cebeSEric Cheng 381da14cebeSEric Cheng static void 382da14cebeSEric Cheng usage(void) 383da14cebeSEric Cheng { 384da14cebeSEric Cheng (void) fprintf(stderr, gettext("usage: flowadm <subcommand>" 385da14cebeSEric Cheng " <args>...\n" 3860790b6dcSAnurag S. Maskey " add-flow [-t] -l <link> -a <attr>=<value>[,...]\n" 3870790b6dcSAnurag S. Maskey "\t\t [-p <prop>=<value>,...] <flow>\n" 3880790b6dcSAnurag S. Maskey " remove-flow [-t] {-l <link> | <flow>}\n" 3890790b6dcSAnurag S. Maskey " show-flow [-p] [-s [-i <interval>]] [-l <link>] " 3900790b6dcSAnurag S. Maskey "[<flow>]\n\n" 3910790b6dcSAnurag S. Maskey " set-flowprop [-t] -p <prop>=<value>[,...] <flow>\n" 3920790b6dcSAnurag S. Maskey " reset-flowprop [-t] [-p <prop>,...] <flow>\n" 3930790b6dcSAnurag S. Maskey " show-flowprop [-cP] [-l <link>] [-p <prop>,...] " 3940790b6dcSAnurag S. Maskey "[<flow>]\n\n" 3950790b6dcSAnurag S. Maskey " show-usage [-d|-p -F <format>] " 3960790b6dcSAnurag S. Maskey "[-s <DD/MM/YYYY,HH:MM:SS>]\n" 3970790b6dcSAnurag S. Maskey "\t\t [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<flow>]\n")); 3984ac67f02SAnurag S. Maskey 3994ac67f02SAnurag S. Maskey /* close dladm handle if it was opened */ 4004ac67f02SAnurag S. Maskey if (handle != NULL) 4014ac67f02SAnurag S. Maskey dladm_close(handle); 4024ac67f02SAnurag S. Maskey 403da14cebeSEric Cheng exit(1); 404da14cebeSEric Cheng } 405da14cebeSEric Cheng 406da14cebeSEric Cheng int 407da14cebeSEric Cheng main(int argc, char *argv[]) 408da14cebeSEric Cheng { 409da14cebeSEric Cheng int i, arglen, cmdlen; 410da14cebeSEric Cheng cmd_t *cmdp; 4114ac67f02SAnurag S. Maskey dladm_status_t status; 412da14cebeSEric Cheng 413da14cebeSEric Cheng (void) setlocale(LC_ALL, ""); 414da14cebeSEric Cheng #if !defined(TEXT_DOMAIN) 415da14cebeSEric Cheng #define TEXT_DOMAIN "SYS_TEST" 416da14cebeSEric Cheng #endif 417da14cebeSEric Cheng (void) textdomain(TEXT_DOMAIN); 418da14cebeSEric Cheng 419da14cebeSEric Cheng progname = argv[0]; 420da14cebeSEric Cheng 421da14cebeSEric Cheng if (argc < 2) 422da14cebeSEric Cheng usage(); 423da14cebeSEric Cheng 424da14cebeSEric Cheng for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) { 425da14cebeSEric Cheng cmdp = &cmds[i]; 426da14cebeSEric Cheng arglen = strlen(argv[1]); 427da14cebeSEric Cheng cmdlen = strlen(cmdp->c_name); 428da14cebeSEric Cheng if ((arglen == cmdlen) && (strncmp(argv[1], cmdp->c_name, 429da14cebeSEric Cheng cmdlen) == 0)) { 4304ac67f02SAnurag S. Maskey /* Open the libdladm handle */ 4314ac67f02SAnurag S. Maskey if ((status = dladm_open(&handle)) != DLADM_STATUS_OK) { 4324ac67f02SAnurag S. Maskey die_dlerr(status, 4334ac67f02SAnurag S. Maskey "could not open /dev/dld"); 4344ac67f02SAnurag S. Maskey } 4354ac67f02SAnurag S. Maskey 436da14cebeSEric Cheng cmdp->c_fn(argc - 1, &argv[1]); 4374ac67f02SAnurag S. Maskey 4384ac67f02SAnurag S. Maskey dladm_close(handle); 439*ad091ee1SMichael Lim exit(EXIT_SUCCESS); 440da14cebeSEric Cheng } 441da14cebeSEric Cheng } 442da14cebeSEric Cheng 443da14cebeSEric Cheng (void) fprintf(stderr, gettext("%s: unknown subcommand '%s'\n"), 444da14cebeSEric Cheng progname, argv[1]); 445da14cebeSEric Cheng usage(); 446da14cebeSEric Cheng 447da14cebeSEric Cheng return (0); 448da14cebeSEric Cheng } 449da14cebeSEric Cheng 450da14cebeSEric Cheng static const char * 451da14cebeSEric Cheng match_attr(char *attr) 452da14cebeSEric Cheng { 453da14cebeSEric Cheng int i; 454da14cebeSEric Cheng 455da14cebeSEric Cheng for (i = 0; i < NATTR; i++) { 456da14cebeSEric Cheng if (strlen(attr) == strlen(attr_table[i]) && 457da14cebeSEric Cheng strncmp(attr, attr_table[i], strlen(attr_table[i])) == 0) { 458da14cebeSEric Cheng return (attr); 459da14cebeSEric Cheng } 460da14cebeSEric Cheng } 461da14cebeSEric Cheng return (NULL); 462da14cebeSEric Cheng } 463da14cebeSEric Cheng 464da14cebeSEric Cheng /* ARGSUSED */ 465da14cebeSEric Cheng static void 466da14cebeSEric Cheng do_init_flow(int argc, char *argv[]) 467da14cebeSEric Cheng { 468da14cebeSEric Cheng dladm_status_t status; 469da14cebeSEric Cheng 4704ac67f02SAnurag S. Maskey status = dladm_flow_init(handle); 471da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 472da14cebeSEric Cheng die_dlerr(status, "flows initialization failed"); 473da14cebeSEric Cheng } 474da14cebeSEric Cheng 475da14cebeSEric Cheng /* ARGSUSED */ 476da14cebeSEric Cheng static int 477da14cebeSEric Cheng show_usage_date(dladm_usage_t *usage, void *arg) 478da14cebeSEric Cheng { 479da14cebeSEric Cheng 480da14cebeSEric Cheng time_t stime; 481da14cebeSEric Cheng char timebuf[20]; 482da14cebeSEric Cheng 483da14cebeSEric Cheng stime = usage->du_stime; 484da14cebeSEric Cheng (void) strftime(timebuf, sizeof (timebuf), "%m/%d/%Y", 485da14cebeSEric Cheng localtime(&stime)); 486da14cebeSEric Cheng (void) printf("%s\n", timebuf); 487da14cebeSEric Cheng 488da14cebeSEric Cheng return (DLADM_STATUS_OK); 489da14cebeSEric Cheng } 490da14cebeSEric Cheng 491da14cebeSEric Cheng static int 492da14cebeSEric Cheng show_usage_time(dladm_usage_t *usage, void *arg) 493da14cebeSEric Cheng { 494da14cebeSEric Cheng show_usage_state_t *state = (show_usage_state_t *)arg; 495da14cebeSEric Cheng char buf[DLADM_STRSIZE]; 496da14cebeSEric Cheng usage_l_fields_buf_t ubuf; 497da14cebeSEric Cheng time_t time; 498da14cebeSEric Cheng double bw; 499da14cebeSEric Cheng 500da14cebeSEric Cheng if (state->us_plot) { 501da14cebeSEric Cheng if (!state->us_printheader) { 502da14cebeSEric Cheng if (state->us_first) { 503da14cebeSEric Cheng (void) printf("# Time"); 504da14cebeSEric Cheng state->us_first = B_FALSE; 505da14cebeSEric Cheng } 506da14cebeSEric Cheng (void) printf(" %s", usage->du_name); 507da14cebeSEric Cheng if (usage->du_last) { 508da14cebeSEric Cheng (void) printf("\n"); 509da14cebeSEric Cheng state->us_first = B_TRUE; 510da14cebeSEric Cheng state->us_printheader = B_TRUE; 511da14cebeSEric Cheng } 512da14cebeSEric Cheng } else { 513da14cebeSEric Cheng if (state->us_first) { 514da14cebeSEric Cheng time = usage->du_etime; 515da14cebeSEric Cheng (void) strftime(buf, sizeof (buf), "%T", 516da14cebeSEric Cheng localtime(&time)); 517da14cebeSEric Cheng state->us_first = B_FALSE; 518da14cebeSEric Cheng (void) printf("%s", buf); 519da14cebeSEric Cheng } 520da14cebeSEric Cheng bw = (double)usage->du_bandwidth/1000; 521da14cebeSEric Cheng (void) printf(" %.2f", bw); 522da14cebeSEric Cheng if (usage->du_last) { 523da14cebeSEric Cheng (void) printf("\n"); 524da14cebeSEric Cheng state->us_first = B_TRUE; 525da14cebeSEric Cheng } 526da14cebeSEric Cheng } 527da14cebeSEric Cheng return (DLADM_STATUS_OK); 528da14cebeSEric Cheng } 529da14cebeSEric Cheng 530da14cebeSEric Cheng bzero(&ubuf, sizeof (ubuf)); 531da14cebeSEric Cheng 532da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_flow, sizeof (ubuf.usage_l_flow), "%s", 533da14cebeSEric Cheng usage->du_name); 534da14cebeSEric Cheng time = usage->du_stime; 535da14cebeSEric Cheng (void) strftime(buf, sizeof (buf), "%T", localtime(&time)); 536da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_stime, sizeof (ubuf.usage_l_stime), "%s", 537da14cebeSEric Cheng buf); 538da14cebeSEric Cheng time = usage->du_etime; 539da14cebeSEric Cheng (void) strftime(buf, sizeof (buf), "%T", localtime(&time)); 540da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_etime, sizeof (ubuf.usage_l_etime), "%s", 541da14cebeSEric Cheng buf); 542da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_rbytes, sizeof (ubuf.usage_l_rbytes), 543da14cebeSEric Cheng "%llu", usage->du_rbytes); 544da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_obytes, sizeof (ubuf.usage_l_obytes), 545da14cebeSEric Cheng "%llu", usage->du_obytes); 546da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_bandwidth, sizeof (ubuf.usage_l_bandwidth), 547da14cebeSEric Cheng "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf)); 548da14cebeSEric Cheng 549da14cebeSEric Cheng if (!state->us_parseable && !state->us_printheader) { 550da14cebeSEric Cheng print_header(&state->us_print); 551da14cebeSEric Cheng state->us_printheader = B_TRUE; 552da14cebeSEric Cheng } 553da14cebeSEric Cheng 554da14cebeSEric Cheng flowadm_print_output(&state->us_print, state->us_parseable, 555da14cebeSEric Cheng flowadm_print_field, (void *)&ubuf); 556da14cebeSEric Cheng 557da14cebeSEric Cheng return (DLADM_STATUS_OK); 558da14cebeSEric Cheng } 559da14cebeSEric Cheng 560da14cebeSEric Cheng static int 561da14cebeSEric Cheng show_usage_res(dladm_usage_t *usage, void *arg) 562da14cebeSEric Cheng { 563da14cebeSEric Cheng show_usage_state_t *state = (show_usage_state_t *)arg; 564da14cebeSEric Cheng char buf[DLADM_STRSIZE]; 565da14cebeSEric Cheng usage_fields_buf_t ubuf; 566da14cebeSEric Cheng 567da14cebeSEric Cheng bzero(&ubuf, sizeof (ubuf)); 568da14cebeSEric Cheng 569da14cebeSEric Cheng (void) snprintf(ubuf.usage_flow, sizeof (ubuf.usage_flow), "%s", 570da14cebeSEric Cheng usage->du_name); 571da14cebeSEric Cheng (void) snprintf(ubuf.usage_duration, sizeof (ubuf.usage_duration), 572da14cebeSEric Cheng "%llu", usage->du_duration); 573da14cebeSEric Cheng (void) snprintf(ubuf.usage_ipackets, sizeof (ubuf.usage_ipackets), 574da14cebeSEric Cheng "%llu", usage->du_ipackets); 575da14cebeSEric Cheng (void) snprintf(ubuf.usage_rbytes, sizeof (ubuf.usage_rbytes), 576da14cebeSEric Cheng "%llu", usage->du_rbytes); 577da14cebeSEric Cheng (void) snprintf(ubuf.usage_opackets, sizeof (ubuf.usage_opackets), 578da14cebeSEric Cheng "%llu", usage->du_opackets); 579da14cebeSEric Cheng (void) snprintf(ubuf.usage_obytes, sizeof (ubuf.usage_obytes), 580da14cebeSEric Cheng "%llu", usage->du_obytes); 581da14cebeSEric Cheng (void) snprintf(ubuf.usage_bandwidth, sizeof (ubuf.usage_bandwidth), 582da14cebeSEric Cheng "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf)); 583da14cebeSEric Cheng 584da14cebeSEric Cheng if (!state->us_parseable && !state->us_printheader) { 585da14cebeSEric Cheng print_header(&state->us_print); 586da14cebeSEric Cheng state->us_printheader = B_TRUE; 587da14cebeSEric Cheng } 588da14cebeSEric Cheng 589da14cebeSEric Cheng flowadm_print_output(&state->us_print, state->us_parseable, 590da14cebeSEric Cheng flowadm_print_field, (void *)&ubuf); 591da14cebeSEric Cheng 592da14cebeSEric Cheng return (DLADM_STATUS_OK); 593da14cebeSEric Cheng } 594da14cebeSEric Cheng 595da14cebeSEric Cheng static boolean_t 596da14cebeSEric Cheng valid_formatspec(char *formatspec_str) 597da14cebeSEric Cheng { 598da14cebeSEric Cheng if (strcmp(formatspec_str, "gnuplot") == 0) 599da14cebeSEric Cheng return (B_TRUE); 600da14cebeSEric Cheng return (B_FALSE); 601da14cebeSEric Cheng } 602da14cebeSEric Cheng 603da14cebeSEric Cheng /* ARGSUSED */ 604da14cebeSEric Cheng static void 605da14cebeSEric Cheng do_show_usage(int argc, char *argv[]) 606da14cebeSEric Cheng { 607da14cebeSEric Cheng char *file = NULL; 608da14cebeSEric Cheng int opt; 609da14cebeSEric Cheng dladm_status_t status; 610da14cebeSEric Cheng boolean_t d_arg = B_FALSE; 611da14cebeSEric Cheng boolean_t p_arg = B_FALSE; 612da14cebeSEric Cheng char *stime = NULL; 613da14cebeSEric Cheng char *etime = NULL; 614da14cebeSEric Cheng char *resource = NULL; 615da14cebeSEric Cheng show_usage_state_t state; 616da14cebeSEric Cheng boolean_t o_arg = B_FALSE; 617da14cebeSEric Cheng boolean_t F_arg = B_FALSE; 618da14cebeSEric Cheng char *fields_str = NULL; 619da14cebeSEric Cheng char *formatspec_str = NULL; 620da14cebeSEric Cheng print_field_t **fields; 621da14cebeSEric Cheng uint_t nfields; 622da14cebeSEric Cheng char *all_fields = 623da14cebeSEric Cheng "flow,duration,ipackets,rbytes,opackets,obytes,bandwidth"; 624da14cebeSEric Cheng char *all_l_fields = 625da14cebeSEric Cheng "flow,start,end,rbytes,obytes,bandwidth"; 626da14cebeSEric Cheng 627da14cebeSEric Cheng bzero(&state, sizeof (show_usage_state_t)); 628da14cebeSEric Cheng state.us_parseable = B_FALSE; 629da14cebeSEric Cheng state.us_printheader = B_FALSE; 630da14cebeSEric Cheng state.us_plot = B_FALSE; 631da14cebeSEric Cheng state.us_first = B_TRUE; 632da14cebeSEric Cheng 633da14cebeSEric Cheng while ((opt = getopt(argc, argv, "dps:e:o:f:F:")) != -1) { 634da14cebeSEric Cheng switch (opt) { 635da14cebeSEric Cheng case 'd': 636da14cebeSEric Cheng d_arg = B_TRUE; 637da14cebeSEric Cheng break; 638da14cebeSEric Cheng case 'p': 639da14cebeSEric Cheng state.us_plot = p_arg = B_TRUE; 640da14cebeSEric Cheng break; 641da14cebeSEric Cheng case 'f': 642da14cebeSEric Cheng file = optarg; 643da14cebeSEric Cheng break; 644da14cebeSEric Cheng case 's': 645da14cebeSEric Cheng stime = optarg; 646da14cebeSEric Cheng break; 647da14cebeSEric Cheng case 'e': 648da14cebeSEric Cheng etime = optarg; 649da14cebeSEric Cheng break; 650da14cebeSEric Cheng case 'o': 651da14cebeSEric Cheng o_arg = B_TRUE; 652da14cebeSEric Cheng fields_str = optarg; 653da14cebeSEric Cheng break; 654da14cebeSEric Cheng case 'F': 655da14cebeSEric Cheng F_arg = B_TRUE; 656da14cebeSEric Cheng formatspec_str = optarg; 657da14cebeSEric Cheng break; 658da14cebeSEric Cheng default: 659da14cebeSEric Cheng die_opterr(optopt, opt); 660da14cebeSEric Cheng } 661da14cebeSEric Cheng } 662da14cebeSEric Cheng 663da14cebeSEric Cheng if (file == NULL) 664da14cebeSEric Cheng die("show-usage requires a file"); 665da14cebeSEric Cheng 666da14cebeSEric Cheng if (optind == (argc-1)) { 667da14cebeSEric Cheng resource = argv[optind]; 668da14cebeSEric Cheng } 669da14cebeSEric Cheng 670da14cebeSEric Cheng if (resource == NULL && stime == NULL && etime == NULL) { 671da14cebeSEric Cheng if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) 672da14cebeSEric Cheng fields_str = all_fields; 673da14cebeSEric Cheng fields = parse_output_fields(fields_str, usage_fields, 674da14cebeSEric Cheng USAGE_MAX_FIELDS, CMD_TYPE_ANY, &nfields); 675da14cebeSEric Cheng } else { 676da14cebeSEric Cheng if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) 677da14cebeSEric Cheng fields_str = all_l_fields; 678da14cebeSEric Cheng fields = parse_output_fields(fields_str, usage_l_fields, 679da14cebeSEric Cheng USAGE_L_MAX_FIELDS, CMD_TYPE_ANY, &nfields); 680da14cebeSEric Cheng } 681da14cebeSEric Cheng 682da14cebeSEric Cheng if (fields == NULL) { 683da14cebeSEric Cheng die("invalid fields(s) specified"); 684da14cebeSEric Cheng return; 685da14cebeSEric Cheng } 686da14cebeSEric Cheng state.us_print.ps_fields = fields; 687da14cebeSEric Cheng state.us_print.ps_nfields = nfields; 688da14cebeSEric Cheng 689da14cebeSEric Cheng if (p_arg && d_arg) 690da14cebeSEric Cheng die("plot and date options are incompatible"); 691da14cebeSEric Cheng 692da14cebeSEric Cheng if (p_arg && !F_arg) 693da14cebeSEric Cheng die("specify format speicifier: -F <format>"); 694da14cebeSEric Cheng 695da14cebeSEric Cheng if (F_arg && valid_formatspec(formatspec_str) == B_FALSE) 696da14cebeSEric Cheng die("Format specifier %s not supported", formatspec_str); 697da14cebeSEric Cheng 698da14cebeSEric Cheng if (d_arg) { 699da14cebeSEric Cheng /* Print log dates */ 700da14cebeSEric Cheng status = dladm_usage_dates(show_usage_date, 701da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, resource, &state); 702da14cebeSEric Cheng } else if (resource == NULL && stime == NULL && etime == NULL && 703da14cebeSEric Cheng !p_arg) { 704da14cebeSEric Cheng /* Print summary */ 705da14cebeSEric Cheng status = dladm_usage_summary(show_usage_res, 706da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, &state); 707da14cebeSEric Cheng } else if (resource != NULL) { 708da14cebeSEric Cheng /* Print log entries for named resource */ 709da14cebeSEric Cheng status = dladm_walk_usage_res(show_usage_time, 710da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, resource, stime, etime, &state); 711da14cebeSEric Cheng } else { 712da14cebeSEric Cheng /* Print time and information for each link */ 713da14cebeSEric Cheng status = dladm_walk_usage_time(show_usage_time, 714da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, stime, etime, &state); 715da14cebeSEric Cheng } 716da14cebeSEric Cheng 717da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 718da14cebeSEric Cheng die_dlerr(status, "show-usage"); 719da14cebeSEric Cheng } 720da14cebeSEric Cheng 721da14cebeSEric Cheng static void 722da14cebeSEric Cheng do_add_flow(int argc, char *argv[]) 723da14cebeSEric Cheng { 724da000602SGirish Moodalbail char devname[MAXLINKNAMELEN]; 725da14cebeSEric Cheng char *name = NULL; 726da14cebeSEric Cheng uint_t index; 727da14cebeSEric Cheng datalink_id_t linkid; 728da14cebeSEric Cheng 729da14cebeSEric Cheng char option; 730da14cebeSEric Cheng boolean_t l_arg = B_FALSE; 731da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 732da14cebeSEric Cheng dladm_arg_list_t *attrlist = NULL; 733da14cebeSEric Cheng dladm_status_t status; 734da14cebeSEric Cheng 735da14cebeSEric Cheng while ((option = getopt_long(argc, argv, "tR:l:a:p:", 736da14cebeSEric Cheng prop_longopts, NULL)) != -1) { 737da14cebeSEric Cheng switch (option) { 738da14cebeSEric Cheng case 't': 739da14cebeSEric Cheng t_arg = B_TRUE; 740da14cebeSEric Cheng break; 741da14cebeSEric Cheng case 'R': 742da14cebeSEric Cheng altroot = optarg; 743da14cebeSEric Cheng break; 744da14cebeSEric Cheng case 'l': 745da14cebeSEric Cheng if (strlcpy(devname, optarg, 746da000602SGirish Moodalbail MAXLINKNAMELEN) >= MAXLINKNAMELEN) { 747da14cebeSEric Cheng die("link name too long"); 748da14cebeSEric Cheng } 7494ac67f02SAnurag S. Maskey if (dladm_name2info(handle, devname, &linkid, NULL, 750da14cebeSEric Cheng NULL, NULL) != DLADM_STATUS_OK) 751da14cebeSEric Cheng die("invalid link '%s'", devname); 752da14cebeSEric Cheng l_arg = B_TRUE; 753da14cebeSEric Cheng break; 754da14cebeSEric Cheng case 'a': 755da14cebeSEric Cheng if (dladm_parse_flow_attrs(optarg, &attrlist, B_FALSE) 756da14cebeSEric Cheng != DLADM_STATUS_OK) 757da14cebeSEric Cheng die("invalid flow attribute specified"); 758da14cebeSEric Cheng break; 759da14cebeSEric Cheng case 'p': 760da14cebeSEric Cheng if (dladm_parse_flow_props(optarg, &proplist, B_FALSE) 761da14cebeSEric Cheng != DLADM_STATUS_OK) 762da14cebeSEric Cheng die("invalid flow property specified"); 763da14cebeSEric Cheng break; 764da14cebeSEric Cheng default: 765da14cebeSEric Cheng die_opterr(optopt, option); 766da14cebeSEric Cheng } 767da14cebeSEric Cheng } 768da14cebeSEric Cheng if (!l_arg) { 769da14cebeSEric Cheng die("link is required"); 770da14cebeSEric Cheng } 771da14cebeSEric Cheng 772da14cebeSEric Cheng opterr = 0; 773da14cebeSEric Cheng index = optind; 774da14cebeSEric Cheng 775da14cebeSEric Cheng if ((index != (argc - 1)) || match_attr(argv[index]) != NULL) { 776da14cebeSEric Cheng die("flow name is required"); 777da14cebeSEric Cheng } else { 778da14cebeSEric Cheng /* get flow name; required last argument */ 779da000602SGirish Moodalbail if (strlen(argv[index]) >= MAXFLOWNAMELEN) 780da14cebeSEric Cheng die("flow name too long"); 781da14cebeSEric Cheng name = argv[index]; 782da14cebeSEric Cheng } 783da14cebeSEric Cheng 7844ac67f02SAnurag S. Maskey status = dladm_flow_add(handle, linkid, attrlist, proplist, name, 785da14cebeSEric Cheng t_arg, altroot); 786da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 787da14cebeSEric Cheng die_dlerr(status, "add flow failed"); 788da14cebeSEric Cheng 789da14cebeSEric Cheng dladm_free_attrs(attrlist); 790da14cebeSEric Cheng dladm_free_props(proplist); 791da14cebeSEric Cheng } 792da14cebeSEric Cheng 793da14cebeSEric Cheng static void 794da14cebeSEric Cheng do_remove_flow(int argc, char *argv[]) 795da14cebeSEric Cheng { 796da14cebeSEric Cheng char option; 797da14cebeSEric Cheng char *flowname = NULL; 798da000602SGirish Moodalbail char linkname[MAXLINKNAMELEN]; 799da14cebeSEric Cheng datalink_id_t linkid = DATALINK_ALL_LINKID; 800da14cebeSEric Cheng boolean_t l_arg = B_FALSE; 801da14cebeSEric Cheng remove_flow_state_t state; 802da14cebeSEric Cheng dladm_status_t status; 803da14cebeSEric Cheng 804da14cebeSEric Cheng bzero(&state, sizeof (state)); 805da14cebeSEric Cheng 806da14cebeSEric Cheng opterr = 0; 807da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":tR:l:", 808da14cebeSEric Cheng longopts, NULL)) != -1) { 809da14cebeSEric Cheng switch (option) { 810da14cebeSEric Cheng case 't': 811da14cebeSEric Cheng t_arg = B_TRUE; 812da14cebeSEric Cheng break; 813da14cebeSEric Cheng case 'R': 814da14cebeSEric Cheng altroot = optarg; 815da14cebeSEric Cheng break; 816da14cebeSEric Cheng case 'l': 817da14cebeSEric Cheng if (strlcpy(linkname, optarg, 818da14cebeSEric Cheng MAXLINKNAMELEN) >= MAXLINKNAMELEN) { 819da14cebeSEric Cheng die("link name too long"); 820da14cebeSEric Cheng } 8214ac67f02SAnurag S. Maskey if (dladm_name2info(handle, linkname, &linkid, NULL, 822da14cebeSEric Cheng NULL, NULL) != DLADM_STATUS_OK) { 823da14cebeSEric Cheng die("invalid link '%s'", linkname); 824da14cebeSEric Cheng } 825da14cebeSEric Cheng l_arg = B_TRUE; 826da14cebeSEric Cheng break; 827da14cebeSEric Cheng default: 828da14cebeSEric Cheng die_opterr(optopt, option); 829da14cebeSEric Cheng break; 830da14cebeSEric Cheng } 831da14cebeSEric Cheng } 832da14cebeSEric Cheng 833da14cebeSEric Cheng /* when link not specified get flow name */ 834da14cebeSEric Cheng if (!l_arg) { 835da14cebeSEric Cheng if (optind != (argc-1)) { 836da14cebeSEric Cheng usage(); 837da14cebeSEric Cheng } else { 838da000602SGirish Moodalbail if (strlen(argv[optind]) >= MAXFLOWNAMELEN) 839da14cebeSEric Cheng die("flow name too long"); 840da14cebeSEric Cheng flowname = argv[optind]; 841da14cebeSEric Cheng } 8424ac67f02SAnurag S. Maskey status = dladm_flow_remove(handle, flowname, t_arg, altroot); 843da14cebeSEric Cheng } else { 844da14cebeSEric Cheng /* if link is specified then flow name should not be there */ 845da14cebeSEric Cheng if (optind == argc-1) 846da14cebeSEric Cheng usage(); 847da14cebeSEric Cheng /* walk the link to find flows and remove them */ 848da14cebeSEric Cheng state.fs_tempop = t_arg; 849da14cebeSEric Cheng state.fs_altroot = altroot; 850da14cebeSEric Cheng state.fs_status = DLADM_STATUS_OK; 8514ac67f02SAnurag S. Maskey status = dladm_walk_flow(remove_flow, handle, linkid, &state, 8524ac67f02SAnurag S. Maskey B_FALSE); 853da14cebeSEric Cheng /* 854da14cebeSEric Cheng * check if dladm_walk_flow terminated early and see if the 855da14cebeSEric Cheng * walker function as any status for us 856da14cebeSEric Cheng */ 857da14cebeSEric Cheng if (status == DLADM_STATUS_OK) 858da14cebeSEric Cheng status = state.fs_status; 859da14cebeSEric Cheng } 860da14cebeSEric Cheng 861da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 862da14cebeSEric Cheng die_dlerr(status, "remove flow failed"); 863da14cebeSEric Cheng } 864da14cebeSEric Cheng 865da14cebeSEric Cheng /* 866da14cebeSEric Cheng * Walker function for removing a flow through dladm_walk_flow(); 867da14cebeSEric Cheng */ 868da14cebeSEric Cheng static int 869da14cebeSEric Cheng remove_flow(dladm_flow_attr_t *attr, void *arg) 870da14cebeSEric Cheng { 871da14cebeSEric Cheng remove_flow_state_t *state = (remove_flow_state_t *)arg; 872da14cebeSEric Cheng 8734ac67f02SAnurag S. Maskey state->fs_status = dladm_flow_remove(handle, attr->fa_flowname, 874da14cebeSEric Cheng state->fs_tempop, state->fs_altroot); 875da14cebeSEric Cheng 876da14cebeSEric Cheng if (state->fs_status == DLADM_STATUS_OK) 877da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 878da14cebeSEric Cheng else 879da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 880da14cebeSEric Cheng } 881da14cebeSEric Cheng 882da14cebeSEric Cheng static char * 883da14cebeSEric Cheng flowadm_print_field(print_field_t *pf, void *arg) 884da14cebeSEric Cheng { 885da14cebeSEric Cheng char *value; 886da14cebeSEric Cheng 887da14cebeSEric Cheng value = (char *)arg + pf->pf_offset; 888da14cebeSEric Cheng return (value); 889da14cebeSEric Cheng } 890da14cebeSEric Cheng 891da14cebeSEric Cheng /*ARGSUSED*/ 892da14cebeSEric Cheng static dladm_status_t 893da14cebeSEric Cheng print_flow(show_flow_state_t *state, dladm_flow_attr_t *attr, 894da14cebeSEric Cheng flow_fields_buf_t *fbuf) 895da14cebeSEric Cheng { 896da14cebeSEric Cheng char link[MAXLINKNAMELEN]; 897da14cebeSEric Cheng dladm_status_t status; 898da14cebeSEric Cheng 8994ac67f02SAnurag S. Maskey if ((status = dladm_datalink_id2info(handle, attr->fa_linkid, NULL, 9004ac67f02SAnurag S. Maskey NULL, NULL, link, sizeof (link))) != DLADM_STATUS_OK) { 901da14cebeSEric Cheng return (status); 902da14cebeSEric Cheng } 903da14cebeSEric Cheng 904da14cebeSEric Cheng (void) snprintf(fbuf->flow_name, sizeof (fbuf->flow_name), 905da14cebeSEric Cheng "%s", attr->fa_flowname); 906da14cebeSEric Cheng (void) snprintf(fbuf->flow_link, sizeof (fbuf->flow_link), 907da14cebeSEric Cheng "%s", link); 908da14cebeSEric Cheng 909da14cebeSEric Cheng (void) dladm_flow_attr_ip2str(attr, fbuf->flow_ipaddr, 910da14cebeSEric Cheng sizeof (fbuf->flow_ipaddr)); 911da14cebeSEric Cheng (void) dladm_flow_attr_proto2str(attr, fbuf->flow_proto, 912da14cebeSEric Cheng sizeof (fbuf->flow_proto)); 913da14cebeSEric Cheng (void) dladm_flow_attr_port2str(attr, fbuf->flow_port, 914da14cebeSEric Cheng sizeof (fbuf->flow_port)); 915da14cebeSEric Cheng (void) dladm_flow_attr_dsfield2str(attr, fbuf->flow_dsfield, 916da14cebeSEric Cheng sizeof (fbuf->flow_dsfield)); 917da14cebeSEric Cheng 918da14cebeSEric Cheng return (DLADM_STATUS_OK); 919da14cebeSEric Cheng } 920da14cebeSEric Cheng 921da14cebeSEric Cheng /* 922da14cebeSEric Cheng * Walker function for showing flow attributes through dladm_walk_flow(). 923da14cebeSEric Cheng */ 924da14cebeSEric Cheng static int 925da14cebeSEric Cheng show_flow(dladm_flow_attr_t *attr, void *arg) 926da14cebeSEric Cheng { 927da14cebeSEric Cheng show_flow_state_t *statep = arg; 928da14cebeSEric Cheng dladm_status_t status; 929da14cebeSEric Cheng flow_fields_buf_t fbuf; 930da14cebeSEric Cheng 931da14cebeSEric Cheng /* 932da14cebeSEric Cheng * first get all the flow attributes into fbuf; 933da14cebeSEric Cheng */ 934da14cebeSEric Cheng bzero(&fbuf, sizeof (fbuf)); 935da14cebeSEric Cheng status = print_flow(statep, attr, &fbuf); 936da14cebeSEric Cheng 937da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 938da14cebeSEric Cheng goto done; 939da14cebeSEric Cheng 940da14cebeSEric Cheng if (!statep->fs_parseable && !statep->fs_printheader) { 941da14cebeSEric Cheng print_header(&statep->fs_print); 942da14cebeSEric Cheng statep->fs_printheader = B_TRUE; 943da14cebeSEric Cheng } 944da14cebeSEric Cheng 945da14cebeSEric Cheng flowadm_print_output(&statep->fs_print, statep->fs_parseable, 946da14cebeSEric Cheng flowadm_print_field, (void *)&fbuf); 947da14cebeSEric Cheng 948da14cebeSEric Cheng done: 949da14cebeSEric Cheng statep->fs_status = status; 950da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 951da14cebeSEric Cheng } 952da14cebeSEric Cheng 953da14cebeSEric Cheng static void 954da14cebeSEric Cheng show_one_flow(void *arg, const char *name) 955da14cebeSEric Cheng { 956da14cebeSEric Cheng dladm_flow_attr_t attr; 957da14cebeSEric Cheng 9584ac67f02SAnurag S. Maskey if (dladm_flow_info(handle, name, &attr) != DLADM_STATUS_OK) 959da14cebeSEric Cheng die("invalid flow: '%s'", name); 960da14cebeSEric Cheng else 9614ac67f02SAnurag S. Maskey (void) show_flow(&attr, arg); 962da14cebeSEric Cheng } 963da14cebeSEric Cheng 964da14cebeSEric Cheng /* 965da14cebeSEric Cheng * Wrapper of dladm_walk_flow(show_flow,...) to make it usable to 966da14cebeSEric Cheng * dladm_walk_datalink_id(). Used for showing flow attributes for 967da14cebeSEric Cheng * all flows on all links. 968da14cebeSEric Cheng */ 969da14cebeSEric Cheng static int 9704ac67f02SAnurag S. Maskey show_flows_onelink(dladm_handle_t dh, datalink_id_t linkid, void *arg) 971da14cebeSEric Cheng { 972da14cebeSEric Cheng show_flow_state_t *state = arg; 973da14cebeSEric Cheng 9744ac67f02SAnurag S. Maskey (void) dladm_walk_flow(show_flow, dh, linkid, arg, state->fs_persist); 975da14cebeSEric Cheng 976da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 977da14cebeSEric Cheng } 978da14cebeSEric Cheng 979da14cebeSEric Cheng static void 980da14cebeSEric Cheng get_flow_stats(const char *flowname, pktsum_t *stats) 981da14cebeSEric Cheng { 982da14cebeSEric Cheng kstat_ctl_t *kcp; 983da14cebeSEric Cheng kstat_t *ksp; 984da14cebeSEric Cheng 985da14cebeSEric Cheng bzero(stats, sizeof (*stats)); 986da14cebeSEric Cheng 987da14cebeSEric Cheng if ((kcp = kstat_open()) == NULL) { 988da14cebeSEric Cheng warn("kstat open operation failed"); 989da14cebeSEric Cheng return; 990da14cebeSEric Cheng } 991da14cebeSEric Cheng 992da14cebeSEric Cheng ksp = dladm_kstat_lookup(kcp, NULL, -1, flowname, "flow"); 993da14cebeSEric Cheng 994da14cebeSEric Cheng if (ksp != NULL) 995da14cebeSEric Cheng dladm_get_stats(kcp, ksp, stats); 996da14cebeSEric Cheng 997da14cebeSEric Cheng (void) kstat_close(kcp); 998da14cebeSEric Cheng } 999da14cebeSEric Cheng 1000da14cebeSEric Cheng /* ARGSUSED */ 1001da14cebeSEric Cheng static int 1002da14cebeSEric Cheng show_flow_stats(dladm_flow_attr_t *attr, void *arg) 1003da14cebeSEric Cheng { 1004da14cebeSEric Cheng show_flow_state_t *state = (show_flow_state_t *)arg; 1005da14cebeSEric Cheng const char *name = attr->fa_flowname; 1006da14cebeSEric Cheng pktsum_t stats, diff_stats; 1007da14cebeSEric Cheng 1008da14cebeSEric Cheng if (state->fs_firstonly) { 1009da14cebeSEric Cheng if (state->fs_donefirst) 1010da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 1011da14cebeSEric Cheng state->fs_donefirst = B_TRUE; 1012da14cebeSEric Cheng } else { 1013da14cebeSEric Cheng bzero(&state->fs_prevstats, sizeof (state->fs_prevstats)); 1014da14cebeSEric Cheng } 1015da14cebeSEric Cheng 1016da14cebeSEric Cheng get_flow_stats(name, &stats); 1017da14cebeSEric Cheng dladm_stats_diff(&diff_stats, &stats, &state->fs_prevstats); 1018da14cebeSEric Cheng 1019da14cebeSEric Cheng (void) printf("%-12s", name); 1020da14cebeSEric Cheng (void) printf("%-10llu", diff_stats.ipackets); 1021da14cebeSEric Cheng (void) printf("%-12llu", diff_stats.rbytes); 1022da14cebeSEric Cheng (void) printf("%-8llu", diff_stats.ierrors); 1023da14cebeSEric Cheng (void) printf("%-10llu", diff_stats.opackets); 1024da14cebeSEric Cheng (void) printf("%-12llu", diff_stats.obytes); 1025da14cebeSEric Cheng (void) printf("%-8llu\n", diff_stats.oerrors); 1026da14cebeSEric Cheng 1027da14cebeSEric Cheng state->fs_prevstats = stats; 1028da14cebeSEric Cheng 1029da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1030da14cebeSEric Cheng } 1031da14cebeSEric Cheng 1032da14cebeSEric Cheng /* 1033da14cebeSEric Cheng * Wrapper of dladm_walk_flow(show_flow,...) to make it usable for 1034da14cebeSEric Cheng * dladm_walk_datalink_id(). Used for showing flow stats for 1035da14cebeSEric Cheng * all flows on all links. 1036da14cebeSEric Cheng */ 1037da14cebeSEric Cheng static int 10384ac67f02SAnurag S. Maskey show_link_flow_stats(dladm_handle_t dh, datalink_id_t linkid, void * arg) 1039da14cebeSEric Cheng { 10404ac67f02SAnurag S. Maskey if (dladm_walk_flow(show_flow_stats, dh, linkid, arg, B_FALSE) 1041da14cebeSEric Cheng == DLADM_STATUS_OK) 1042da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1043da14cebeSEric Cheng else 1044da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 1045da14cebeSEric Cheng } 1046da14cebeSEric Cheng 1047da14cebeSEric Cheng /* ARGSUSED */ 1048da14cebeSEric Cheng static void 1049da14cebeSEric Cheng flow_stats(const char *flow, datalink_id_t linkid, uint_t interval) 1050da14cebeSEric Cheng { 1051da14cebeSEric Cheng show_flow_state_t state; 1052da14cebeSEric Cheng dladm_flow_attr_t attr; 1053da14cebeSEric Cheng 10544ac67f02SAnurag S. Maskey if (flow != NULL && 10554ac67f02SAnurag S. Maskey dladm_flow_info(handle, flow, &attr) != DLADM_STATUS_OK) 1056da14cebeSEric Cheng die("invalid flow %s", flow); 1057da14cebeSEric Cheng 1058da14cebeSEric Cheng bzero(&state, sizeof (state)); 1059da14cebeSEric Cheng 1060da14cebeSEric Cheng /* 1061da14cebeSEric Cheng * If an interval is specified, continuously show the stats 1062da14cebeSEric Cheng * for only the first flow. 1063da14cebeSEric Cheng */ 1064da14cebeSEric Cheng state.fs_firstonly = (interval != 0); 1065da14cebeSEric Cheng 1066da14cebeSEric Cheng for (;;) { 1067da14cebeSEric Cheng if (!state.fs_donefirst) 1068da14cebeSEric Cheng (void) printf("%-12s%-10s%-12s%-8s%-10s%-12s%-8s\n", 1069da14cebeSEric Cheng "FLOW", "IPACKETS", "RBYTES", "IERRORS", 1070da14cebeSEric Cheng "OPACKETS", "OBYTES", "OERRORS"); 1071da14cebeSEric Cheng 1072da14cebeSEric Cheng state.fs_donefirst = B_FALSE; 1073da14cebeSEric Cheng 1074da14cebeSEric Cheng /* Show stats for named flow */ 1075da14cebeSEric Cheng if (flow != NULL) { 1076da14cebeSEric Cheng state.fs_flow = flow; 1077da14cebeSEric Cheng (void) show_flow_stats(&attr, &state); 1078da14cebeSEric Cheng 1079da14cebeSEric Cheng /* Show all stats on a link */ 1080da14cebeSEric Cheng } else if (linkid != DATALINK_INVALID_LINKID) { 10814ac67f02SAnurag S. Maskey (void) dladm_walk_flow(show_flow_stats, handle, linkid, 10824ac67f02SAnurag S. Maskey &state, B_FALSE); 1083da14cebeSEric Cheng 1084da14cebeSEric Cheng /* Show all stats by datalink */ 1085da14cebeSEric Cheng } else { 1086da14cebeSEric Cheng (void) dladm_walk_datalink_id(show_link_flow_stats, 10874ac67f02SAnurag S. Maskey handle, &state, DATALINK_CLASS_ALL, 10884ac67f02SAnurag S. Maskey DATALINK_ANY_MEDIATYPE, DLADM_OPT_ACTIVE); 1089da14cebeSEric Cheng } 1090da14cebeSEric Cheng 1091da14cebeSEric Cheng if (interval == 0) 1092da14cebeSEric Cheng break; 1093da14cebeSEric Cheng 1094da14cebeSEric Cheng (void) sleep(interval); 1095da14cebeSEric Cheng } 1096da14cebeSEric Cheng } 1097da14cebeSEric Cheng 1098da14cebeSEric Cheng static void 1099da14cebeSEric Cheng do_show_flow(int argc, char *argv[]) 1100da14cebeSEric Cheng { 1101da000602SGirish Moodalbail char flowname[MAXFLOWNAMELEN]; 1102da000602SGirish Moodalbail char linkname[MAXLINKNAMELEN]; 1103da14cebeSEric Cheng datalink_id_t linkid = DATALINK_ALL_LINKID; 1104da14cebeSEric Cheng int option; 1105da14cebeSEric Cheng boolean_t s_arg = B_FALSE; 1106da14cebeSEric Cheng boolean_t S_arg = B_FALSE; 1107da14cebeSEric Cheng boolean_t i_arg = B_FALSE; 1108da14cebeSEric Cheng boolean_t l_arg = B_FALSE; 1109da14cebeSEric Cheng boolean_t o_arg = B_FALSE; 1110da14cebeSEric Cheng uint32_t interval = 0; 1111da14cebeSEric Cheng char *endp = NULL; 1112da14cebeSEric Cheng show_flow_state_t state; 1113da14cebeSEric Cheng char *fields_str = NULL; 1114da14cebeSEric Cheng print_field_t **fields; 1115da14cebeSEric Cheng uint_t nfields; 1116da14cebeSEric Cheng char *all_fields = 1117*ad091ee1SMichael Lim "flow,link,ipaddr,proto,port,dsfld"; 1118da14cebeSEric Cheng 1119da14cebeSEric Cheng bzero(&state, sizeof (state)); 1120da14cebeSEric Cheng 1121da14cebeSEric Cheng opterr = 0; 1122da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":pPsSi:l:o:", 1123da14cebeSEric Cheng longopts, NULL)) != -1) { 1124da14cebeSEric Cheng switch (option) { 1125da14cebeSEric Cheng case 'p': 1126da14cebeSEric Cheng state.fs_parseable = B_TRUE; 1127da14cebeSEric Cheng break; 1128da14cebeSEric Cheng case 'P': 1129da14cebeSEric Cheng state.fs_persist = B_TRUE; 1130da14cebeSEric Cheng break; 1131da14cebeSEric Cheng case 's': 1132da14cebeSEric Cheng if (s_arg) 1133da14cebeSEric Cheng die_optdup(option); 1134da14cebeSEric Cheng 1135da14cebeSEric Cheng s_arg = B_TRUE; 1136da14cebeSEric Cheng break; 1137da14cebeSEric Cheng case 'S': 1138da14cebeSEric Cheng if (S_arg) 1139da14cebeSEric Cheng die_optdup(option); 1140da14cebeSEric Cheng 1141da14cebeSEric Cheng S_arg = B_TRUE; 1142da14cebeSEric Cheng break; 1143da14cebeSEric Cheng case 'o': 1144da14cebeSEric Cheng if (o_arg) 1145da14cebeSEric Cheng die_optdup(option); 1146da14cebeSEric Cheng 1147da14cebeSEric Cheng o_arg = B_TRUE; 1148da14cebeSEric Cheng fields_str = optarg; 1149da14cebeSEric Cheng break; 1150da14cebeSEric Cheng case 'i': 1151da14cebeSEric Cheng if (i_arg) 1152da14cebeSEric Cheng die_optdup(option); 1153da14cebeSEric Cheng 1154da14cebeSEric Cheng i_arg = B_TRUE; 1155da14cebeSEric Cheng 1156da14cebeSEric Cheng errno = 0; 1157da14cebeSEric Cheng interval = (int)strtol(optarg, &endp, 10); 1158da14cebeSEric Cheng if (errno != 0 || interval == 0 || *endp != '\0') 1159da14cebeSEric Cheng die("invalid interval value" " '%d'\n", 1160da14cebeSEric Cheng interval); 1161da14cebeSEric Cheng break; 1162da14cebeSEric Cheng case 'l': 1163da14cebeSEric Cheng if (strlcpy(linkname, optarg, MAXLINKNAMELEN) 1164da14cebeSEric Cheng >= MAXLINKNAMELEN) 1165da14cebeSEric Cheng die("link name too long\n"); 11664ac67f02SAnurag S. Maskey if (dladm_name2info(handle, linkname, &linkid, NULL, 1167da14cebeSEric Cheng NULL, NULL) != DLADM_STATUS_OK) 1168da14cebeSEric Cheng die("invalid link '%s'", linkname); 1169da14cebeSEric Cheng l_arg = B_TRUE; 1170da14cebeSEric Cheng break; 1171da14cebeSEric Cheng default: 1172da14cebeSEric Cheng die_opterr(optopt, option); 1173da14cebeSEric Cheng break; 1174da14cebeSEric Cheng } 1175da14cebeSEric Cheng } 1176*ad091ee1SMichael Lim if (state.fs_parseable && !o_arg) 1177*ad091ee1SMichael Lim die("-p requires -o"); 1178*ad091ee1SMichael Lim 1179*ad091ee1SMichael Lim if (state.fs_parseable && strcasecmp(fields_str, "all") == 0) 1180*ad091ee1SMichael Lim die("\"-o all\" is invalid with -p"); 1181*ad091ee1SMichael Lim 1182da14cebeSEric Cheng if (i_arg && !(s_arg || S_arg)) 1183da14cebeSEric Cheng die("the -i option can be used only with -s or -S"); 1184da14cebeSEric Cheng 1185da14cebeSEric Cheng if (s_arg && S_arg) 1186da14cebeSEric Cheng die("the -s option cannot be used with -S"); 1187da14cebeSEric Cheng 1188da14cebeSEric Cheng /* get flow name (optional last argument */ 1189da14cebeSEric Cheng if (optind == (argc-1)) { 1190da000602SGirish Moodalbail if (strlcpy(flowname, argv[optind], MAXFLOWNAMELEN) 1191da000602SGirish Moodalbail >= MAXFLOWNAMELEN) 1192da14cebeSEric Cheng die("flow name too long"); 1193da14cebeSEric Cheng state.fs_flow = flowname; 1194da14cebeSEric Cheng } 1195da14cebeSEric Cheng 1196da14cebeSEric Cheng if (s_arg) { 1197da14cebeSEric Cheng flow_stats(state.fs_flow, linkid, interval); 1198da14cebeSEric Cheng return; 1199da14cebeSEric Cheng } 1200da14cebeSEric Cheng 1201da14cebeSEric Cheng if (S_arg) { 12024ac67f02SAnurag S. Maskey dladm_continuous(handle, linkid, state.fs_flow, interval, 12034ac67f02SAnurag S. Maskey FLOW_REPORT); 1204da14cebeSEric Cheng return; 1205da14cebeSEric Cheng } 1206da14cebeSEric Cheng 1207da14cebeSEric Cheng if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) 1208da14cebeSEric Cheng fields_str = all_fields; 1209da14cebeSEric Cheng 1210da14cebeSEric Cheng fields = parse_output_fields(fields_str, flow_fields, FLOW_MAX_FIELDS, 1211da14cebeSEric Cheng CMD_TYPE_ANY, &nfields); 1212da14cebeSEric Cheng 1213da14cebeSEric Cheng if (fields == NULL) { 1214da14cebeSEric Cheng die("invalid fields(s) specified"); 1215da14cebeSEric Cheng return; 1216da14cebeSEric Cheng } 1217da14cebeSEric Cheng 1218da14cebeSEric Cheng state.fs_print.ps_fields = fields; 1219da14cebeSEric Cheng state.fs_print.ps_nfields = nfields; 1220da14cebeSEric Cheng 1221da14cebeSEric Cheng /* Show attributes of one flow */ 1222da14cebeSEric Cheng if (state.fs_flow != NULL) { 1223da14cebeSEric Cheng show_one_flow(&state, state.fs_flow); 1224da14cebeSEric Cheng 1225da14cebeSEric Cheng /* Show attributes of flows on one link */ 1226da14cebeSEric Cheng } else if (l_arg) { 12274ac67f02SAnurag S. Maskey (void) show_flows_onelink(handle, linkid, &state); 1228da14cebeSEric Cheng 1229da14cebeSEric Cheng /* Show attributes of all flows on all links */ 1230da14cebeSEric Cheng } else { 12314ac67f02SAnurag S. Maskey (void) dladm_walk_datalink_id(show_flows_onelink, handle, 12324ac67f02SAnurag S. Maskey &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, 1233da14cebeSEric Cheng DLADM_OPT_ACTIVE); 1234da14cebeSEric Cheng } 1235da14cebeSEric Cheng } 1236da14cebeSEric Cheng 1237da14cebeSEric Cheng static dladm_status_t 1238da14cebeSEric Cheng set_flowprop_persist(const char *flow, const char *prop_name, char **prop_val, 1239da14cebeSEric Cheng uint_t val_cnt, boolean_t reset) 1240da14cebeSEric Cheng { 1241da14cebeSEric Cheng dladm_status_t status; 1242da14cebeSEric Cheng char *errprop; 1243da14cebeSEric Cheng 12444ac67f02SAnurag S. Maskey status = dladm_set_flowprop(handle, flow, prop_name, prop_val, val_cnt, 1245da14cebeSEric Cheng DLADM_OPT_PERSIST, &errprop); 1246da14cebeSEric Cheng 1247da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1248da14cebeSEric Cheng warn_dlerr(status, "cannot persistently %s flow " 1249da14cebeSEric Cheng "property '%s' on '%s'", reset? "reset": "set", 1250da14cebeSEric Cheng errprop, flow); 1251da14cebeSEric Cheng } 1252da14cebeSEric Cheng return (status); 1253da14cebeSEric Cheng } 1254da14cebeSEric Cheng 1255da14cebeSEric Cheng static void 1256da14cebeSEric Cheng set_flowprop(int argc, char **argv, boolean_t reset) 1257da14cebeSEric Cheng { 1258da14cebeSEric Cheng int i, option; 1259da14cebeSEric Cheng char errmsg[DLADM_STRSIZE]; 1260da14cebeSEric Cheng const char *flow = NULL; 1261da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 1262da14cebeSEric Cheng boolean_t temp = B_FALSE; 1263da14cebeSEric Cheng dladm_status_t status = DLADM_STATUS_OK; 1264da14cebeSEric Cheng 1265da14cebeSEric Cheng opterr = 0; 1266da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":p:R:t", 1267da14cebeSEric Cheng prop_longopts, NULL)) != -1) { 1268da14cebeSEric Cheng switch (option) { 1269da14cebeSEric Cheng case 'p': 1270da14cebeSEric Cheng if (dladm_parse_flow_props(optarg, &proplist, reset) 1271da14cebeSEric Cheng != DLADM_STATUS_OK) 1272da14cebeSEric Cheng die("invalid flow property specified"); 1273da14cebeSEric Cheng break; 1274da14cebeSEric Cheng case 't': 1275da14cebeSEric Cheng temp = B_TRUE; 1276da14cebeSEric Cheng break; 1277da14cebeSEric Cheng case 'R': 1278da14cebeSEric Cheng status = dladm_set_rootdir(optarg); 1279da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1280da14cebeSEric Cheng die_dlerr(status, "invalid directory " 1281da14cebeSEric Cheng "specified"); 1282da14cebeSEric Cheng } 1283da14cebeSEric Cheng break; 1284da14cebeSEric Cheng default: 1285da14cebeSEric Cheng die_opterr(optopt, option); 1286da14cebeSEric Cheng break; 1287da14cebeSEric Cheng } 1288da14cebeSEric Cheng } 1289da14cebeSEric Cheng 1290da14cebeSEric Cheng if (optind == (argc - 1)) { 1291da000602SGirish Moodalbail if (strlen(argv[optind]) >= MAXFLOWNAMELEN) 1292da14cebeSEric Cheng die("flow name too long"); 1293da14cebeSEric Cheng flow = argv[optind]; 1294da14cebeSEric Cheng } else if (optind != argc) { 1295da14cebeSEric Cheng usage(); 1296da14cebeSEric Cheng } 1297da14cebeSEric Cheng if (flow == NULL) 1298da14cebeSEric Cheng die("flow name must be specified"); 1299da14cebeSEric Cheng 1300da14cebeSEric Cheng if (proplist == NULL) { 1301da14cebeSEric Cheng char *errprop; 1302da14cebeSEric Cheng 1303da14cebeSEric Cheng if (!reset) 1304da14cebeSEric Cheng die("flow property must be specified"); 1305da14cebeSEric Cheng 13064ac67f02SAnurag S. Maskey status = dladm_set_flowprop(handle, flow, NULL, NULL, 0, 1307da14cebeSEric Cheng DLADM_OPT_ACTIVE, &errprop); 1308da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1309da14cebeSEric Cheng warn_dlerr(status, "cannot reset flow property '%s' " 1310da14cebeSEric Cheng "on '%s'", errprop, flow); 1311da14cebeSEric Cheng } 1312da14cebeSEric Cheng if (!temp) { 1313da14cebeSEric Cheng dladm_status_t s; 1314da14cebeSEric Cheng 1315da14cebeSEric Cheng s = set_flowprop_persist(flow, NULL, NULL, 0, reset); 1316da14cebeSEric Cheng if (s != DLADM_STATUS_OK) 1317da14cebeSEric Cheng status = s; 1318da14cebeSEric Cheng } 1319da14cebeSEric Cheng goto done; 1320da14cebeSEric Cheng } 1321da14cebeSEric Cheng 1322da14cebeSEric Cheng for (i = 0; i < proplist->al_count; i++) { 1323da14cebeSEric Cheng dladm_arg_info_t *aip = &proplist->al_info[i]; 1324da14cebeSEric Cheng char **val; 1325da14cebeSEric Cheng uint_t count; 1326da14cebeSEric Cheng dladm_status_t s; 1327da14cebeSEric Cheng 1328da14cebeSEric Cheng if (reset) { 1329da14cebeSEric Cheng val = NULL; 1330da14cebeSEric Cheng count = 0; 1331da14cebeSEric Cheng } else { 1332da14cebeSEric Cheng val = aip->ai_val; 1333da14cebeSEric Cheng count = aip->ai_count; 1334da14cebeSEric Cheng if (count == 0) { 1335da14cebeSEric Cheng warn("no value specified for '%s'", 1336da14cebeSEric Cheng aip->ai_name); 1337da14cebeSEric Cheng status = DLADM_STATUS_BADARG; 1338da14cebeSEric Cheng continue; 1339da14cebeSEric Cheng } 1340da14cebeSEric Cheng } 13414ac67f02SAnurag S. Maskey s = dladm_set_flowprop(handle, flow, aip->ai_name, val, count, 1342da14cebeSEric Cheng DLADM_OPT_ACTIVE, NULL); 1343da14cebeSEric Cheng if (s == DLADM_STATUS_OK) { 1344da14cebeSEric Cheng if (!temp) { 1345da14cebeSEric Cheng s = set_flowprop_persist(flow, 1346da14cebeSEric Cheng aip->ai_name, val, count, reset); 1347da14cebeSEric Cheng if (s != DLADM_STATUS_OK) 1348da14cebeSEric Cheng status = s; 1349da14cebeSEric Cheng } 1350da14cebeSEric Cheng continue; 1351da14cebeSEric Cheng } 1352da14cebeSEric Cheng status = s; 1353da14cebeSEric Cheng switch (s) { 1354da14cebeSEric Cheng case DLADM_STATUS_NOTFOUND: 1355da14cebeSEric Cheng warn("invalid flow property '%s'", aip->ai_name); 1356da14cebeSEric Cheng break; 1357da14cebeSEric Cheng case DLADM_STATUS_BADVAL: { 1358da14cebeSEric Cheng int j; 1359da14cebeSEric Cheng char *ptr, *lim; 1360da14cebeSEric Cheng char **propvals = NULL; 1361da14cebeSEric Cheng uint_t valcnt = DLADM_MAX_PROP_VALCNT; 1362da14cebeSEric Cheng 1363da14cebeSEric Cheng ptr = malloc((sizeof (char *) + 1364da14cebeSEric Cheng DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT + 1365da14cebeSEric Cheng MAX_PROP_LINE); 1366da14cebeSEric Cheng 1367da14cebeSEric Cheng if (ptr == NULL) 1368da14cebeSEric Cheng die("insufficient memory"); 1369da14cebeSEric Cheng propvals = (char **)(void *)ptr; 1370da14cebeSEric Cheng 1371da14cebeSEric Cheng for (j = 0; j < DLADM_MAX_PROP_VALCNT; j++) { 1372da14cebeSEric Cheng propvals[j] = ptr + sizeof (char *) * 1373da14cebeSEric Cheng DLADM_MAX_PROP_VALCNT + 1374da14cebeSEric Cheng j * DLADM_PROP_VAL_MAX; 1375da14cebeSEric Cheng } 13764ac67f02SAnurag S. Maskey s = dladm_get_flowprop(handle, flow, 13774ac67f02SAnurag S. Maskey DLADM_PROP_VAL_MODIFIABLE, aip->ai_name, propvals, 13784ac67f02SAnurag S. Maskey &valcnt); 1379da14cebeSEric Cheng 1380da14cebeSEric Cheng ptr = errmsg; 1381da14cebeSEric Cheng lim = ptr + DLADM_STRSIZE; 1382da14cebeSEric Cheng *ptr = '\0'; 1383da14cebeSEric Cheng for (j = 0; j < valcnt && s == DLADM_STATUS_OK; j++) { 1384da14cebeSEric Cheng ptr += snprintf(ptr, lim - ptr, "%s,", 1385da14cebeSEric Cheng propvals[j]); 1386da14cebeSEric Cheng if (ptr >= lim) 1387da14cebeSEric Cheng break; 1388da14cebeSEric Cheng } 1389da14cebeSEric Cheng if (ptr > errmsg) { 1390da14cebeSEric Cheng *(ptr - 1) = '\0'; 1391da14cebeSEric Cheng warn("flow property '%s' must be one of: %s", 1392da14cebeSEric Cheng aip->ai_name, errmsg); 1393da14cebeSEric Cheng } else 1394da14cebeSEric Cheng warn("%s is an invalid value for " 1395da14cebeSEric Cheng "flow property %s", *val, aip->ai_name); 1396da14cebeSEric Cheng free(propvals); 1397da14cebeSEric Cheng break; 1398da14cebeSEric Cheng } 1399da14cebeSEric Cheng default: 1400da14cebeSEric Cheng if (reset) { 1401da14cebeSEric Cheng warn_dlerr(status, "cannot reset flow property " 1402da14cebeSEric Cheng "'%s' on '%s'", aip->ai_name, flow); 1403da14cebeSEric Cheng } else { 1404da14cebeSEric Cheng warn_dlerr(status, "cannot set flow property " 1405da14cebeSEric Cheng "'%s' on '%s'", aip->ai_name, flow); 1406da14cebeSEric Cheng } 1407da14cebeSEric Cheng break; 1408da14cebeSEric Cheng } 1409da14cebeSEric Cheng } 1410da14cebeSEric Cheng done: 1411da14cebeSEric Cheng dladm_free_props(proplist); 14124ac67f02SAnurag S. Maskey if (status != DLADM_STATUS_OK) { 14134ac67f02SAnurag S. Maskey dladm_close(handle); 1414*ad091ee1SMichael Lim exit(EXIT_FAILURE); 1415da14cebeSEric Cheng } 14164ac67f02SAnurag S. Maskey } 1417da14cebeSEric Cheng 1418da14cebeSEric Cheng static void 1419da14cebeSEric Cheng do_set_flowprop(int argc, char **argv) 1420da14cebeSEric Cheng { 1421da14cebeSEric Cheng set_flowprop(argc, argv, B_FALSE); 1422da14cebeSEric Cheng } 1423da14cebeSEric Cheng 1424da14cebeSEric Cheng static void 1425da14cebeSEric Cheng do_reset_flowprop(int argc, char **argv) 1426da14cebeSEric Cheng { 1427da14cebeSEric Cheng set_flowprop(argc, argv, B_TRUE); 1428da14cebeSEric Cheng } 1429da14cebeSEric Cheng 1430da14cebeSEric Cheng static void 1431da14cebeSEric Cheng warn(const char *format, ...) 1432da14cebeSEric Cheng { 1433da14cebeSEric Cheng va_list alist; 1434da14cebeSEric Cheng 1435da14cebeSEric Cheng format = gettext(format); 1436da14cebeSEric Cheng (void) fprintf(stderr, "%s: warning: ", progname); 1437da14cebeSEric Cheng 1438da14cebeSEric Cheng va_start(alist, format); 1439da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1440da14cebeSEric Cheng va_end(alist); 1441da14cebeSEric Cheng 1442da14cebeSEric Cheng (void) putchar('\n'); 1443da14cebeSEric Cheng } 1444da14cebeSEric Cheng 1445da14cebeSEric Cheng /* PRINTFLIKE2 */ 1446da14cebeSEric Cheng static void 1447da14cebeSEric Cheng warn_dlerr(dladm_status_t err, const char *format, ...) 1448da14cebeSEric Cheng { 1449da14cebeSEric Cheng va_list alist; 1450da14cebeSEric Cheng char errmsg[DLADM_STRSIZE]; 1451da14cebeSEric Cheng 1452da14cebeSEric Cheng format = gettext(format); 1453da14cebeSEric Cheng (void) fprintf(stderr, gettext("%s: warning: "), progname); 1454da14cebeSEric Cheng 1455da14cebeSEric Cheng va_start(alist, format); 1456da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1457da14cebeSEric Cheng va_end(alist); 1458da14cebeSEric Cheng (void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg)); 1459da14cebeSEric Cheng } 1460da14cebeSEric Cheng 1461da14cebeSEric Cheng /* PRINTFLIKE1 */ 1462da14cebeSEric Cheng static void 1463da14cebeSEric Cheng die(const char *format, ...) 1464da14cebeSEric Cheng { 1465da14cebeSEric Cheng va_list alist; 1466da14cebeSEric Cheng 1467da14cebeSEric Cheng format = gettext(format); 1468da14cebeSEric Cheng (void) fprintf(stderr, "%s: ", progname); 1469da14cebeSEric Cheng 1470da14cebeSEric Cheng va_start(alist, format); 1471da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1472da14cebeSEric Cheng va_end(alist); 1473da14cebeSEric Cheng 1474da14cebeSEric Cheng (void) putchar('\n'); 14754ac67f02SAnurag S. Maskey 14764ac67f02SAnurag S. Maskey /* close dladm handle if it was opened */ 14774ac67f02SAnurag S. Maskey if (handle != NULL) 14784ac67f02SAnurag S. Maskey dladm_close(handle); 14794ac67f02SAnurag S. Maskey 1480da14cebeSEric Cheng exit(EXIT_FAILURE); 1481da14cebeSEric Cheng } 1482da14cebeSEric Cheng 1483da14cebeSEric Cheng static void 1484da14cebeSEric Cheng die_optdup(int opt) 1485da14cebeSEric Cheng { 1486da14cebeSEric Cheng die("the option -%c cannot be specified more than once", opt); 1487da14cebeSEric Cheng } 1488da14cebeSEric Cheng 1489da14cebeSEric Cheng static void 1490da14cebeSEric Cheng die_opterr(int opt, int opterr) 1491da14cebeSEric Cheng { 1492da14cebeSEric Cheng switch (opterr) { 1493da14cebeSEric Cheng case ':': 1494da14cebeSEric Cheng die("option '-%c' requires a value", opt); 1495da14cebeSEric Cheng break; 1496da14cebeSEric Cheng case '?': 1497da14cebeSEric Cheng default: 1498da14cebeSEric Cheng die("unrecognized option '-%c'", opt); 1499da14cebeSEric Cheng break; 1500da14cebeSEric Cheng } 1501da14cebeSEric Cheng } 1502da14cebeSEric Cheng 1503da14cebeSEric Cheng /* PRINTFLIKE2 */ 1504da14cebeSEric Cheng static void 1505da14cebeSEric Cheng die_dlerr(dladm_status_t err, const char *format, ...) 1506da14cebeSEric Cheng { 1507da14cebeSEric Cheng va_list alist; 1508da14cebeSEric Cheng char errmsg[DLADM_STRSIZE]; 1509da14cebeSEric Cheng 1510da14cebeSEric Cheng format = gettext(format); 1511da14cebeSEric Cheng (void) fprintf(stderr, "%s: ", progname); 1512da14cebeSEric Cheng 1513da14cebeSEric Cheng va_start(alist, format); 1514da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1515da14cebeSEric Cheng va_end(alist); 1516da14cebeSEric Cheng (void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg)); 1517da14cebeSEric Cheng 15184ac67f02SAnurag S. Maskey /* close dladm handle if it was opened */ 15194ac67f02SAnurag S. Maskey if (handle != NULL) 15204ac67f02SAnurag S. Maskey dladm_close(handle); 15214ac67f02SAnurag S. Maskey 1522da14cebeSEric Cheng exit(EXIT_FAILURE); 1523da14cebeSEric Cheng } 1524da14cebeSEric Cheng 1525da14cebeSEric Cheng static void 1526da14cebeSEric Cheng print_flowprop(const char *flowname, show_flowprop_state_t *statep, 1527da14cebeSEric Cheng const char *propname, dladm_prop_type_t type, 1528da14cebeSEric Cheng const char *format, char **pptr) 1529da14cebeSEric Cheng { 1530da14cebeSEric Cheng int i; 1531da14cebeSEric Cheng char *ptr, *lim; 1532da14cebeSEric Cheng char buf[DLADM_STRSIZE]; 1533da14cebeSEric Cheng char *unknown = "--", *notsup = ""; 1534da14cebeSEric Cheng char **propvals = statep->fs_propvals; 1535da14cebeSEric Cheng uint_t valcnt = DLADM_MAX_PROP_VALCNT; 1536da14cebeSEric Cheng dladm_status_t status; 1537da14cebeSEric Cheng 15384ac67f02SAnurag S. Maskey status = dladm_get_flowprop(handle, flowname, type, propname, propvals, 1539da14cebeSEric Cheng &valcnt); 1540da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1541da14cebeSEric Cheng if (status == DLADM_STATUS_TEMPONLY) { 1542da14cebeSEric Cheng if (type == DLADM_PROP_VAL_MODIFIABLE && 1543da14cebeSEric Cheng statep->fs_persist) { 1544da14cebeSEric Cheng valcnt = 1; 1545da14cebeSEric Cheng propvals = &unknown; 1546da14cebeSEric Cheng } else { 1547da14cebeSEric Cheng statep->fs_status = status; 1548da14cebeSEric Cheng statep->fs_retstatus = status; 1549da14cebeSEric Cheng return; 1550da14cebeSEric Cheng } 1551da14cebeSEric Cheng } else if (status == DLADM_STATUS_NOTSUP || 1552da14cebeSEric Cheng statep->fs_persist) { 1553da14cebeSEric Cheng valcnt = 1; 1554da14cebeSEric Cheng if (type == DLADM_PROP_VAL_CURRENT) 1555da14cebeSEric Cheng propvals = &unknown; 1556da14cebeSEric Cheng else 1557da14cebeSEric Cheng propvals = ¬sup; 1558da14cebeSEric Cheng } else { 1559da14cebeSEric Cheng if ((statep->fs_proplist != NULL) && 1560da14cebeSEric Cheng statep->fs_status == DLADM_STATUS_OK) { 1561da14cebeSEric Cheng warn("invalid flow property '%s'", propname); 1562da14cebeSEric Cheng } 1563da14cebeSEric Cheng statep->fs_status = status; 1564da14cebeSEric Cheng statep->fs_retstatus = status; 1565da14cebeSEric Cheng return; 1566da14cebeSEric Cheng } 1567da14cebeSEric Cheng } 1568da14cebeSEric Cheng 1569da14cebeSEric Cheng statep->fs_status = DLADM_STATUS_OK; 1570da14cebeSEric Cheng 1571da14cebeSEric Cheng ptr = buf; 1572da14cebeSEric Cheng lim = buf + DLADM_STRSIZE; 1573da14cebeSEric Cheng for (i = 0; i < valcnt; i++) { 1574da14cebeSEric Cheng if (propvals[i][0] == '\0' && !statep->fs_parseable) 1575da14cebeSEric Cheng ptr += snprintf(ptr, lim - ptr, STR_UNDEF_VAL","); 1576da14cebeSEric Cheng else 1577da14cebeSEric Cheng ptr += snprintf(ptr, lim - ptr, "%s,", propvals[i]); 1578da14cebeSEric Cheng if (ptr >= lim) 1579da14cebeSEric Cheng break; 1580da14cebeSEric Cheng } 1581da14cebeSEric Cheng if (valcnt > 0) 1582da14cebeSEric Cheng buf[strlen(buf) - 1] = '\0'; 1583da14cebeSEric Cheng 1584da14cebeSEric Cheng lim = statep->fs_line + MAX_PROP_LINE; 1585da14cebeSEric Cheng if (statep->fs_parseable) { 1586da14cebeSEric Cheng *pptr += snprintf(*pptr, lim - *pptr, 1587da14cebeSEric Cheng "%s", buf); 1588da14cebeSEric Cheng } else { 1589da14cebeSEric Cheng *pptr += snprintf(*pptr, lim - *pptr, format, buf); 1590da14cebeSEric Cheng } 1591da14cebeSEric Cheng } 1592da14cebeSEric Cheng 1593da14cebeSEric Cheng static char * 1594da14cebeSEric Cheng flowprop_callback(print_field_t *pf, void *fs_arg) 1595da14cebeSEric Cheng { 1596da14cebeSEric Cheng flowprop_args_t *arg = fs_arg; 1597da14cebeSEric Cheng char *propname = arg->fs_propname; 1598da14cebeSEric Cheng show_flowprop_state_t *statep = arg->fs_state; 1599da14cebeSEric Cheng char *ptr = statep->fs_line; 1600da14cebeSEric Cheng char *lim = ptr + MAX_PROP_LINE; 1601da14cebeSEric Cheng char *flowname = arg->fs_flowname; 1602da14cebeSEric Cheng 1603da14cebeSEric Cheng switch (pf->pf_index) { 1604da14cebeSEric Cheng case FLOWPROP_FLOW: 1605da14cebeSEric Cheng (void) snprintf(ptr, lim - ptr, "%s", statep->fs_flow); 1606da14cebeSEric Cheng break; 1607da14cebeSEric Cheng case FLOWPROP_PROPERTY: 1608da14cebeSEric Cheng (void) snprintf(ptr, lim - ptr, "%s", propname); 1609da14cebeSEric Cheng break; 1610da14cebeSEric Cheng case FLOWPROP_VALUE: 1611da14cebeSEric Cheng print_flowprop(flowname, statep, propname, 1612da14cebeSEric Cheng statep->fs_persist ? DLADM_PROP_VAL_PERSISTENT : 1613da14cebeSEric Cheng DLADM_PROP_VAL_CURRENT, "%s", &ptr); 1614da14cebeSEric Cheng /* 1615da14cebeSEric Cheng * If we failed to query the flow property, for example, query 1616da14cebeSEric Cheng * the persistent value of a non-persistable flow property, 1617da14cebeSEric Cheng * simply skip the output. 1618da14cebeSEric Cheng */ 1619da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1620da14cebeSEric Cheng goto skip; 1621da14cebeSEric Cheng ptr = statep->fs_line; 1622da14cebeSEric Cheng break; 1623da14cebeSEric Cheng case FLOWPROP_DEFAULT: 1624da14cebeSEric Cheng print_flowprop(flowname, statep, propname, 1625da14cebeSEric Cheng DLADM_PROP_VAL_DEFAULT, "%s", &ptr); 1626da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1627da14cebeSEric Cheng goto skip; 1628da14cebeSEric Cheng ptr = statep->fs_line; 1629da14cebeSEric Cheng break; 1630da14cebeSEric Cheng case FLOWPROP_POSSIBLE: 1631da14cebeSEric Cheng print_flowprop(flowname, statep, propname, 1632da14cebeSEric Cheng DLADM_PROP_VAL_MODIFIABLE, "%s ", &ptr); 1633da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1634da14cebeSEric Cheng goto skip; 1635da14cebeSEric Cheng ptr = statep->fs_line; 1636da14cebeSEric Cheng break; 1637da14cebeSEric Cheng default: 1638da14cebeSEric Cheng die("invalid input"); 1639da14cebeSEric Cheng break; 1640da14cebeSEric Cheng } 1641da14cebeSEric Cheng return (ptr); 1642da14cebeSEric Cheng skip: 1643da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1644da14cebeSEric Cheng return (NULL); 1645da14cebeSEric Cheng else 1646da14cebeSEric Cheng return (""); 1647da14cebeSEric Cheng } 1648da14cebeSEric Cheng 1649da14cebeSEric Cheng static int 1650da14cebeSEric Cheng show_one_flowprop(void *arg, const char *propname) 1651da14cebeSEric Cheng { 1652da14cebeSEric Cheng show_flowprop_state_t *statep = arg; 1653da14cebeSEric Cheng flowprop_args_t fs_arg; 1654da14cebeSEric Cheng 1655da14cebeSEric Cheng bzero(&fs_arg, sizeof (fs_arg)); 1656da14cebeSEric Cheng fs_arg.fs_state = statep; 1657da14cebeSEric Cheng fs_arg.fs_propname = (char *)propname; 1658da14cebeSEric Cheng fs_arg.fs_flowname = (char *)statep->fs_flow; 1659da14cebeSEric Cheng 1660da14cebeSEric Cheng if (statep->fs_header) { 1661da14cebeSEric Cheng statep->fs_header = B_FALSE; 1662da14cebeSEric Cheng if (!statep ->fs_parseable) 1663da14cebeSEric Cheng print_header(&statep->fs_print); 1664da14cebeSEric Cheng } 1665da14cebeSEric Cheng flowadm_print_output(&statep->fs_print, statep->fs_parseable, 1666da14cebeSEric Cheng flowprop_callback, (void *)&fs_arg); 1667da14cebeSEric Cheng 1668da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1669da14cebeSEric Cheng } 1670da14cebeSEric Cheng 1671da14cebeSEric Cheng /* Walker function called by dladm_walk_flow to display flow properties */ 1672da14cebeSEric Cheng static int 1673da14cebeSEric Cheng show_flowprop(dladm_flow_attr_t *attr, void *arg) 1674da14cebeSEric Cheng { 1675da14cebeSEric Cheng show_flowprop_one_flow(arg, attr->fa_flowname); 1676da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1677da14cebeSEric Cheng } 1678da14cebeSEric Cheng 1679da14cebeSEric Cheng /* 1680da14cebeSEric Cheng * Wrapper of dladm_walk_flow(show_walk_fn,...) to make it 1681da14cebeSEric Cheng * usable to dladm_walk_datalink_id() 1682da14cebeSEric Cheng */ 1683da14cebeSEric Cheng static int 16844ac67f02SAnurag S. Maskey show_flowprop_onelink(dladm_handle_t dh, datalink_id_t linkid, void *arg) 1685da14cebeSEric Cheng { 1686da14cebeSEric Cheng char name[MAXLINKNAMELEN]; 1687da14cebeSEric Cheng 16884ac67f02SAnurag S. Maskey if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, name, 16894ac67f02SAnurag S. Maskey sizeof (name)) != DLADM_STATUS_OK) 1690da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 1691da14cebeSEric Cheng 16924ac67f02SAnurag S. Maskey (void) dladm_walk_flow(show_flowprop, dh, linkid, arg, B_FALSE); 1693da14cebeSEric Cheng 1694da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1695da14cebeSEric Cheng } 1696da14cebeSEric Cheng 1697da14cebeSEric Cheng static void 1698da14cebeSEric Cheng do_show_flowprop(int argc, char **argv) 1699da14cebeSEric Cheng { 1700da14cebeSEric Cheng int option; 1701*ad091ee1SMichael Lim boolean_t o_arg = B_FALSE; 1702da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 1703da14cebeSEric Cheng show_flowprop_state_t state; 1704da14cebeSEric Cheng char *fields_str = NULL; 1705da14cebeSEric Cheng print_field_t **fields; 1706da14cebeSEric Cheng uint_t nfields; 1707da14cebeSEric Cheng char *all_fields = 1708da14cebeSEric Cheng "flow,property,value,default,possible"; 1709da14cebeSEric Cheng 1710da14cebeSEric Cheng fields_str = all_fields; 1711da14cebeSEric Cheng opterr = 0; 1712da14cebeSEric Cheng state.fs_propvals = NULL; 1713da14cebeSEric Cheng state.fs_line = NULL; 1714da14cebeSEric Cheng state.fs_parseable = B_FALSE; 1715da14cebeSEric Cheng state.fs_persist = B_FALSE; 1716da14cebeSEric Cheng state.fs_header = B_TRUE; 1717da14cebeSEric Cheng state.fs_retstatus = DLADM_STATUS_OK; 1718da14cebeSEric Cheng state.fs_linkid = DATALINK_INVALID_LINKID; 1719da14cebeSEric Cheng state.fs_flow = NULL; 1720da14cebeSEric Cheng 1721da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":p:cPl:o:", 1722da14cebeSEric Cheng prop_longopts, NULL)) != -1) { 1723da14cebeSEric Cheng switch (option) { 1724da14cebeSEric Cheng case 'p': 1725da14cebeSEric Cheng if (dladm_parse_flow_props(optarg, &proplist, B_TRUE) 1726da14cebeSEric Cheng != DLADM_STATUS_OK) 1727da14cebeSEric Cheng die("invalid flow properties specified"); 1728da14cebeSEric Cheng break; 1729da14cebeSEric Cheng case 'c': 1730da14cebeSEric Cheng state.fs_parseable = B_TRUE; 1731da14cebeSEric Cheng break; 1732da14cebeSEric Cheng case 'P': 1733da14cebeSEric Cheng state.fs_persist = B_TRUE; 1734da14cebeSEric Cheng break; 1735da14cebeSEric Cheng case 'l': 17364ac67f02SAnurag S. Maskey if (dladm_name2info(handle, optarg, &state.fs_linkid, 1737da14cebeSEric Cheng NULL, NULL, NULL) != DLADM_STATUS_OK) 1738da14cebeSEric Cheng die("invalid link '%s'", optarg); 1739da14cebeSEric Cheng break; 1740da14cebeSEric Cheng case 'o': 1741*ad091ee1SMichael Lim o_arg = B_TRUE; 1742da14cebeSEric Cheng if (strcasecmp(optarg, "all") == 0) 1743da14cebeSEric Cheng fields_str = all_fields; 1744da14cebeSEric Cheng else 1745da14cebeSEric Cheng fields_str = optarg; 1746da14cebeSEric Cheng break; 1747da14cebeSEric Cheng default: 1748da14cebeSEric Cheng die_opterr(optopt, option); 1749da14cebeSEric Cheng break; 1750da14cebeSEric Cheng } 1751da14cebeSEric Cheng } 1752da14cebeSEric Cheng 1753*ad091ee1SMichael Lim if (state.fs_parseable && !o_arg) 1754*ad091ee1SMichael Lim die("-p requires -o"); 1755*ad091ee1SMichael Lim 1756*ad091ee1SMichael Lim if (state.fs_parseable && strcasecmp(fields_str, "all") == 0) 1757*ad091ee1SMichael Lim die("\"-o all\" is invalid with -p"); 1758*ad091ee1SMichael Lim 1759da14cebeSEric Cheng if (optind == (argc - 1)) { 1760da000602SGirish Moodalbail if (strlen(argv[optind]) >= MAXFLOWNAMELEN) 1761da14cebeSEric Cheng die("flow name too long"); 1762da14cebeSEric Cheng state.fs_flow = argv[optind]; 1763da14cebeSEric Cheng } else if (optind != argc) { 1764da14cebeSEric Cheng usage(); 1765da14cebeSEric Cheng } 1766da14cebeSEric Cheng bzero(&state.fs_print, sizeof (print_state_t)); 1767da14cebeSEric Cheng state.fs_proplist = proplist; 1768da14cebeSEric Cheng state.fs_status = DLADM_STATUS_OK; 1769da14cebeSEric Cheng 1770da14cebeSEric Cheng fields = parse_output_fields(fields_str, flowprop_fields, 1771da14cebeSEric Cheng FLOWPROP_MAX_FIELDS, CMD_TYPE_ANY, &nfields); 1772da14cebeSEric Cheng 1773da14cebeSEric Cheng if (fields == NULL) { 1774da14cebeSEric Cheng die("invalid field(s) specified"); 1775da14cebeSEric Cheng return; 1776da14cebeSEric Cheng } 1777da14cebeSEric Cheng 1778da14cebeSEric Cheng state.fs_print.ps_fields = fields; 1779da14cebeSEric Cheng state.fs_print.ps_nfields = nfields; 1780da14cebeSEric Cheng 1781da14cebeSEric Cheng /* Show properties for one flow */ 1782da14cebeSEric Cheng if (state.fs_flow != NULL) { 1783da14cebeSEric Cheng show_flowprop_one_flow(&state, state.fs_flow); 1784da14cebeSEric Cheng 1785da14cebeSEric Cheng /* Show properties for all flows on one link */ 1786da14cebeSEric Cheng } else if (state.fs_linkid != DATALINK_INVALID_LINKID) { 17874ac67f02SAnurag S. Maskey (void) show_flowprop_onelink(handle, state.fs_linkid, &state); 1788da14cebeSEric Cheng 1789da14cebeSEric Cheng /* Show properties for all flows on all links */ 1790da14cebeSEric Cheng } else { 17914ac67f02SAnurag S. Maskey (void) dladm_walk_datalink_id(show_flowprop_onelink, handle, 17924ac67f02SAnurag S. Maskey &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, 1793da14cebeSEric Cheng DLADM_OPT_ACTIVE); 1794da14cebeSEric Cheng } 1795da14cebeSEric Cheng 1796da14cebeSEric Cheng dladm_free_props(proplist); 1797da14cebeSEric Cheng } 1798da14cebeSEric Cheng 1799da14cebeSEric Cheng static void 1800da14cebeSEric Cheng show_flowprop_one_flow(void *arg, const char *flow) 1801da14cebeSEric Cheng { 1802da14cebeSEric Cheng int i; 1803da14cebeSEric Cheng char *buf; 1804da14cebeSEric Cheng dladm_status_t status; 1805da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 1806da14cebeSEric Cheng show_flowprop_state_t *statep = arg; 1807da14cebeSEric Cheng dladm_flow_attr_t attr; 1808da14cebeSEric Cheng const char *savep; 1809da14cebeSEric Cheng 1810da14cebeSEric Cheng /* 1811da14cebeSEric Cheng * Do not print flow props for invalid flows. 1812da14cebeSEric Cheng */ 18134ac67f02SAnurag S. Maskey if ((status = dladm_flow_info(handle, flow, &attr)) != 18144ac67f02SAnurag S. Maskey DLADM_STATUS_OK) { 1815da14cebeSEric Cheng die("invalid flow: '%s'", flow); 1816da14cebeSEric Cheng } 1817da14cebeSEric Cheng 1818da14cebeSEric Cheng savep = statep->fs_flow; 1819da14cebeSEric Cheng statep->fs_flow = flow; 1820da14cebeSEric Cheng 1821da14cebeSEric Cheng proplist = statep->fs_proplist; 1822da14cebeSEric Cheng 1823da14cebeSEric Cheng buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) 1824da14cebeSEric Cheng * DLADM_MAX_PROP_VALCNT + MAX_PROP_LINE); 1825da14cebeSEric Cheng if (buf == NULL) 1826da14cebeSEric Cheng die("insufficient memory"); 1827da14cebeSEric Cheng 1828da14cebeSEric Cheng statep->fs_propvals = (char **)(void *)buf; 1829da14cebeSEric Cheng for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) { 1830da14cebeSEric Cheng statep->fs_propvals[i] = buf + 1831da14cebeSEric Cheng sizeof (char *) * DLADM_MAX_PROP_VALCNT + 1832da14cebeSEric Cheng i * DLADM_PROP_VAL_MAX; 1833da14cebeSEric Cheng } 1834da14cebeSEric Cheng statep->fs_line = buf + 1835da14cebeSEric Cheng (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT; 1836da14cebeSEric Cheng 1837da14cebeSEric Cheng /* show only specified flow properties */ 1838da14cebeSEric Cheng if (proplist != NULL) { 1839da14cebeSEric Cheng for (i = 0; i < proplist->al_count; i++) { 1840da14cebeSEric Cheng if (show_one_flowprop(statep, 1841da14cebeSEric Cheng proplist->al_info[i].ai_name) != DLADM_STATUS_OK) 1842da14cebeSEric Cheng break; 1843da14cebeSEric Cheng } 1844da14cebeSEric Cheng 1845da14cebeSEric Cheng /* show all flow properties */ 1846da14cebeSEric Cheng } else { 1847da14cebeSEric Cheng status = dladm_walk_flowprop(show_one_flowprop, flow, statep); 1848da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 1849da14cebeSEric Cheng die_dlerr(status, "show-flowprop"); 1850da14cebeSEric Cheng } 1851da14cebeSEric Cheng free(buf); 1852da14cebeSEric Cheng statep->fs_flow = savep; 1853da14cebeSEric Cheng } 1854da14cebeSEric Cheng 1855da14cebeSEric Cheng typedef struct { 1856da14cebeSEric Cheng char *s_buf; 1857da14cebeSEric Cheng char **s_fields; /* array of pointer to the fields in s_buf */ 1858da14cebeSEric Cheng uint_t s_nfields; /* the number of fields in s_buf */ 1859da14cebeSEric Cheng } split_t; 1860da14cebeSEric Cheng 1861da14cebeSEric Cheng /* 1862da14cebeSEric Cheng * Free the split_t structure pointed to by `sp'. 1863da14cebeSEric Cheng */ 1864da14cebeSEric Cheng static void 1865da14cebeSEric Cheng splitfree(split_t *sp) 1866da14cebeSEric Cheng { 1867da14cebeSEric Cheng free(sp->s_buf); 1868da14cebeSEric Cheng free(sp->s_fields); 1869da14cebeSEric Cheng free(sp); 1870da14cebeSEric Cheng } 1871da14cebeSEric Cheng 1872da14cebeSEric Cheng /* 1873da14cebeSEric Cheng * Split `str' into at most `maxfields' fields, each field at most `maxlen' in 1874da14cebeSEric Cheng * length. Return a pointer to a split_t containing the split fields, or NULL 1875da14cebeSEric Cheng * on failure. 1876da14cebeSEric Cheng */ 1877da14cebeSEric Cheng static split_t * 1878da14cebeSEric Cheng split(const char *str, uint_t maxfields, uint_t maxlen) 1879da14cebeSEric Cheng { 1880da14cebeSEric Cheng char *field, *token, *lasts = NULL; 1881da14cebeSEric Cheng split_t *sp; 1882da14cebeSEric Cheng 1883da14cebeSEric Cheng if (*str == '\0' || maxfields == 0 || maxlen == 0) 1884da14cebeSEric Cheng return (NULL); 1885da14cebeSEric Cheng 1886da14cebeSEric Cheng sp = calloc(sizeof (split_t), 1); 1887da14cebeSEric Cheng if (sp == NULL) 1888da14cebeSEric Cheng return (NULL); 1889da14cebeSEric Cheng 1890da14cebeSEric Cheng sp->s_buf = strdup(str); 1891da14cebeSEric Cheng sp->s_fields = malloc(sizeof (char *) * maxfields); 1892da14cebeSEric Cheng if (sp->s_buf == NULL || sp->s_fields == NULL) 1893da14cebeSEric Cheng goto fail; 1894da14cebeSEric Cheng 1895da14cebeSEric Cheng token = sp->s_buf; 1896da14cebeSEric Cheng while ((field = strtok_r(token, ",", &lasts)) != NULL) { 1897da14cebeSEric Cheng if (sp->s_nfields == maxfields || strlen(field) > maxlen) 1898da14cebeSEric Cheng goto fail; 1899da14cebeSEric Cheng token = NULL; 1900da14cebeSEric Cheng sp->s_fields[sp->s_nfields++] = field; 1901da14cebeSEric Cheng } 1902da14cebeSEric Cheng return (sp); 1903da14cebeSEric Cheng fail: 1904da14cebeSEric Cheng splitfree(sp); 1905da14cebeSEric Cheng return (NULL); 1906da14cebeSEric Cheng } 1907da14cebeSEric Cheng 1908da14cebeSEric Cheng static print_field_t ** 1909da14cebeSEric Cheng parse_output_fields(char *str, print_field_t *template, int max_fields, 1910da14cebeSEric Cheng uint_t cmdtype, uint_t *countp) 1911da14cebeSEric Cheng { 1912da14cebeSEric Cheng split_t *sp; 1913da14cebeSEric Cheng boolean_t good_match = B_FALSE; 1914da14cebeSEric Cheng uint_t i, j; 1915da14cebeSEric Cheng print_field_t **pf = NULL; 1916da14cebeSEric Cheng 1917da14cebeSEric Cheng sp = split(str, max_fields, MAX_FIELD_LEN); 1918da14cebeSEric Cheng 1919da14cebeSEric Cheng if (sp == NULL) 1920da14cebeSEric Cheng return (NULL); 1921da14cebeSEric Cheng 1922da14cebeSEric Cheng pf = malloc(sp->s_nfields * sizeof (print_field_t *)); 1923da14cebeSEric Cheng if (pf == NULL) 1924da14cebeSEric Cheng goto fail; 1925da14cebeSEric Cheng 1926da14cebeSEric Cheng for (i = 0; i < sp->s_nfields; i++) { 1927da14cebeSEric Cheng for (j = 0; j < max_fields; j++) { 1928da14cebeSEric Cheng if (strcasecmp(sp->s_fields[i], 1929da14cebeSEric Cheng template[j].pf_name) == 0) { 1930da14cebeSEric Cheng good_match = template[j]. pf_cmdtype & cmdtype; 1931da14cebeSEric Cheng break; 1932da14cebeSEric Cheng } 1933da14cebeSEric Cheng } 1934da14cebeSEric Cheng if (!good_match) 1935da14cebeSEric Cheng goto fail; 1936da14cebeSEric Cheng 1937da14cebeSEric Cheng good_match = B_FALSE; 1938da14cebeSEric Cheng pf[i] = &template[j]; 1939da14cebeSEric Cheng } 1940da14cebeSEric Cheng *countp = i; 1941da14cebeSEric Cheng splitfree(sp); 1942da14cebeSEric Cheng return (pf); 1943da14cebeSEric Cheng fail: 1944da14cebeSEric Cheng free(pf); 1945da14cebeSEric Cheng splitfree(sp); 1946da14cebeSEric Cheng return (NULL); 1947da14cebeSEric Cheng } 1948da14cebeSEric Cheng 1949da14cebeSEric Cheng static void 1950da14cebeSEric Cheng flowadm_print_output(print_state_t *statep, boolean_t parseable, 1951da14cebeSEric Cheng print_callback_t fn, void *arg) 1952da14cebeSEric Cheng { 1953da14cebeSEric Cheng int i; 1954da14cebeSEric Cheng char *value; 1955da14cebeSEric Cheng print_field_t **pf; 1956da14cebeSEric Cheng 1957da14cebeSEric Cheng pf = statep->ps_fields; 1958da14cebeSEric Cheng for (i = 0; i < statep->ps_nfields; i++) { 1959da14cebeSEric Cheng statep->ps_lastfield = (i + 1 == statep->ps_nfields); 1960da14cebeSEric Cheng value = (*fn)(pf[i], arg); 1961da14cebeSEric Cheng if (value != NULL) 1962da14cebeSEric Cheng print_field(statep, pf[i], value, parseable); 1963da14cebeSEric Cheng } 1964da14cebeSEric Cheng (void) putchar('\n'); 1965da14cebeSEric Cheng } 1966da14cebeSEric Cheng 1967da14cebeSEric Cheng static void 1968da14cebeSEric Cheng print_header(print_state_t *ps) 1969da14cebeSEric Cheng { 1970da14cebeSEric Cheng int i; 1971da14cebeSEric Cheng print_field_t **pf; 1972da14cebeSEric Cheng 1973da14cebeSEric Cheng pf = ps->ps_fields; 1974da14cebeSEric Cheng for (i = 0; i < ps->ps_nfields; i++) { 1975da14cebeSEric Cheng ps->ps_lastfield = (i + 1 == ps->ps_nfields); 1976da14cebeSEric Cheng print_field(ps, pf[i], pf[i]->pf_header, B_FALSE); 1977da14cebeSEric Cheng } 1978da14cebeSEric Cheng (void) putchar('\n'); 1979da14cebeSEric Cheng } 1980da14cebeSEric Cheng 1981da14cebeSEric Cheng static void 1982da14cebeSEric Cheng print_field(print_state_t *statep, print_field_t *pfp, const char *value, 1983da14cebeSEric Cheng boolean_t parseable) 1984da14cebeSEric Cheng { 1985da14cebeSEric Cheng uint_t width = pfp->pf_width; 1986*ad091ee1SMichael Lim uint_t valwidth; 1987da14cebeSEric Cheng uint_t compress; 1988da14cebeSEric Cheng 1989*ad091ee1SMichael Lim /* 1990*ad091ee1SMichael Lim * Parsable fields are separated by ':'. If such a field contains 1991*ad091ee1SMichael Lim * a ':' or '\', this character is prefixed by a '\'. 1992*ad091ee1SMichael Lim */ 1993da14cebeSEric Cheng if (parseable) { 1994*ad091ee1SMichael Lim char c; 1995*ad091ee1SMichael Lim 1996*ad091ee1SMichael Lim if (statep->ps_nfields == 1) { 1997*ad091ee1SMichael Lim (void) printf("%s", value); 1998*ad091ee1SMichael Lim return; 1999*ad091ee1SMichael Lim } 2000*ad091ee1SMichael Lim while ((c = *value++) != '\0') { 2001*ad091ee1SMichael Lim if (c == ':' || c == '\\') 2002*ad091ee1SMichael Lim (void) putchar('\\'); 2003*ad091ee1SMichael Lim (void) putchar(c); 2004*ad091ee1SMichael Lim } 2005*ad091ee1SMichael Lim if (!statep->ps_lastfield) 2006*ad091ee1SMichael Lim (void) putchar(':'); 2007*ad091ee1SMichael Lim return; 2008da14cebeSEric Cheng } else { 2009da14cebeSEric Cheng if (value[0] == '\0') 2010da14cebeSEric Cheng value = STR_UNDEF_VAL; 2011da14cebeSEric Cheng if (statep->ps_lastfield) { 2012da14cebeSEric Cheng (void) printf("%s", value); 2013*ad091ee1SMichael Lim statep->ps_overflow = 0; 2014da14cebeSEric Cheng return; 2015da14cebeSEric Cheng } 2016da14cebeSEric Cheng 2017*ad091ee1SMichael Lim valwidth = strlen(value); 2018da14cebeSEric Cheng if (valwidth > width) { 2019da14cebeSEric Cheng statep->ps_overflow += valwidth - width; 2020da14cebeSEric Cheng } else if (valwidth < width && statep->ps_overflow > 0) { 2021da14cebeSEric Cheng compress = min(statep->ps_overflow, width - valwidth); 2022da14cebeSEric Cheng statep->ps_overflow -= compress; 2023da14cebeSEric Cheng width -= compress; 2024da14cebeSEric Cheng } 2025da14cebeSEric Cheng (void) printf("%-*s", width, value); 2026da14cebeSEric Cheng } 2027da14cebeSEric Cheng 2028da14cebeSEric Cheng if (!statep->ps_lastfield) 2029da14cebeSEric Cheng (void) putchar(' '); 2030da14cebeSEric Cheng } 2031