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> 52*8002d411SSowmini Varadhan #include <ofmt.h> 53da14cebeSEric Cheng 54da14cebeSEric Cheng typedef struct show_usage_state_s { 55da14cebeSEric Cheng boolean_t us_plot; 56*8002d411SSowmini Varadhan boolean_t us_parsable; 57da14cebeSEric Cheng boolean_t us_printheader; 58da14cebeSEric Cheng boolean_t us_first; 59ae6aa22aSVenugopal Iyer boolean_t us_showall; 60*8002d411SSowmini Varadhan ofmt_handle_t us_ofmt; 61da14cebeSEric Cheng } show_usage_state_t; 62da14cebeSEric Cheng 63ae6aa22aSVenugopal Iyer typedef struct show_flow_state { 64ae6aa22aSVenugopal Iyer boolean_t fs_firstonly; 65ae6aa22aSVenugopal Iyer boolean_t fs_donefirst; 66ae6aa22aSVenugopal Iyer pktsum_t fs_prevstats; 67ae6aa22aSVenugopal Iyer uint32_t fs_flags; 68ae6aa22aSVenugopal Iyer dladm_status_t fs_status; 69*8002d411SSowmini Varadhan ofmt_handle_t fs_ofmt; 70ae6aa22aSVenugopal Iyer const char *fs_flow; 71ae6aa22aSVenugopal Iyer const char *fs_link; 72*8002d411SSowmini Varadhan boolean_t fs_parsable; 73ae6aa22aSVenugopal Iyer boolean_t fs_persist; 74ae6aa22aSVenugopal Iyer boolean_t fs_stats; 75ae6aa22aSVenugopal Iyer uint64_t fs_mask; 76ae6aa22aSVenugopal Iyer } show_flow_state_t; 77ae6aa22aSVenugopal Iyer 78da14cebeSEric Cheng typedef void cmdfunc_t(int, char **); 79da14cebeSEric Cheng 80da14cebeSEric Cheng static cmdfunc_t do_add_flow, do_remove_flow, do_init_flow, do_show_flow; 81da14cebeSEric Cheng static cmdfunc_t do_show_flowprop, do_set_flowprop, do_reset_flowprop; 82da14cebeSEric Cheng static cmdfunc_t do_show_usage; 83da14cebeSEric Cheng 84da14cebeSEric Cheng static int show_flow(dladm_flow_attr_t *, void *); 854ac67f02SAnurag S. Maskey static int show_flows_onelink(dladm_handle_t, datalink_id_t, void *); 86da14cebeSEric Cheng 87ae6aa22aSVenugopal Iyer static void flow_stats(const char *, datalink_id_t, uint_t, char *, 88ae6aa22aSVenugopal Iyer show_flow_state_t *); 89da14cebeSEric Cheng static void get_flow_stats(const char *, pktsum_t *); 90da14cebeSEric Cheng static int show_flow_stats(dladm_flow_attr_t *, void *); 914ac67f02SAnurag S. Maskey static int show_link_flow_stats(dladm_handle_t, datalink_id_t, void *); 92da14cebeSEric Cheng 93da14cebeSEric Cheng static int remove_flow(dladm_flow_attr_t *, void *); 94da14cebeSEric Cheng 95da14cebeSEric Cheng static int show_flowprop(dladm_flow_attr_t *, void *); 96da14cebeSEric Cheng static void show_flowprop_one_flow(void *, const char *); 974ac67f02SAnurag S. Maskey static int show_flowprop_onelink(dladm_handle_t, datalink_id_t, void *); 98da14cebeSEric Cheng 99da14cebeSEric Cheng static void die(const char *, ...); 100da14cebeSEric Cheng static void die_optdup(int); 101da14cebeSEric Cheng static void die_opterr(int, int); 102da14cebeSEric Cheng static void die_dlerr(dladm_status_t, const char *, ...); 103da14cebeSEric Cheng static void warn(const char *, ...); 104da14cebeSEric Cheng static void warn_dlerr(dladm_status_t, const char *, ...); 105da14cebeSEric Cheng 106*8002d411SSowmini Varadhan /* callback functions for printing output */ 107*8002d411SSowmini Varadhan static ofmt_cb_t print_flowprop_cb, print_default_cb, print_flow_stats_cb; 108*8002d411SSowmini Varadhan static void flowadm_ofmt_check(ofmt_status_t, boolean_t, ofmt_handle_t); 109*8002d411SSowmini Varadhan 110da14cebeSEric Cheng typedef struct cmd { 111da14cebeSEric Cheng char *c_name; 112da14cebeSEric Cheng void (*c_fn)(int, char **); 113da14cebeSEric Cheng } cmd_t; 114da14cebeSEric Cheng 115da14cebeSEric Cheng static cmd_t cmds[] = { 116da14cebeSEric Cheng { "add-flow", do_add_flow }, 117da14cebeSEric Cheng { "remove-flow", do_remove_flow }, 118da14cebeSEric Cheng { "show-flowprop", do_show_flowprop }, 119da14cebeSEric Cheng { "set-flowprop", do_set_flowprop }, 120da14cebeSEric Cheng { "reset-flowprop", do_reset_flowprop }, 121da14cebeSEric Cheng { "show-flow", do_show_flow }, 122da14cebeSEric Cheng { "init-flow", do_init_flow }, 123da14cebeSEric Cheng { "show-usage", do_show_usage } 124da14cebeSEric Cheng }; 125da14cebeSEric Cheng 126da14cebeSEric Cheng static const struct option longopts[] = { 127da14cebeSEric Cheng {"link", required_argument, 0, 'l'}, 128*8002d411SSowmini Varadhan {"parsable", no_argument, 0, 'p'}, 129da14cebeSEric Cheng {"parseable", no_argument, 0, 'p'}, 130da14cebeSEric Cheng {"statistics", no_argument, 0, 's'}, 131da14cebeSEric Cheng {"interval", required_argument, 0, 'i'}, 132da14cebeSEric Cheng {"temporary", no_argument, 0, 't'}, 133da14cebeSEric Cheng {"root-dir", required_argument, 0, 'R'}, 134da14cebeSEric Cheng { 0, 0, 0, 0 } 135da14cebeSEric Cheng }; 136da14cebeSEric Cheng 137da14cebeSEric Cheng static const struct option prop_longopts[] = { 138da14cebeSEric Cheng {"link", required_argument, 0, 'l'}, 139da14cebeSEric Cheng {"temporary", no_argument, 0, 't'}, 140da14cebeSEric Cheng {"root-dir", required_argument, 0, 'R'}, 141da14cebeSEric Cheng {"prop", required_argument, 0, 'p'}, 142da14cebeSEric Cheng {"attr", required_argument, 0, 'a'}, 143da14cebeSEric Cheng { 0, 0, 0, 0 } 144da14cebeSEric Cheng }; 145da14cebeSEric Cheng 146da14cebeSEric Cheng /* 147da14cebeSEric Cheng * structures for 'flowadm remove-flow' 148da14cebeSEric Cheng */ 149da14cebeSEric Cheng typedef struct remove_flow_state { 150da14cebeSEric Cheng boolean_t fs_tempop; 151da14cebeSEric Cheng const char *fs_altroot; 152da14cebeSEric Cheng dladm_status_t fs_status; 153da14cebeSEric Cheng } remove_flow_state_t; 154da14cebeSEric Cheng 155da14cebeSEric Cheng #define PROTO_MAXSTR_LEN 7 156da14cebeSEric Cheng #define PORT_MAXSTR_LEN 6 157da14cebeSEric Cheng #define DSFIELD_MAXSTR_LEN 10 158*8002d411SSowmini Varadhan #define NULL_OFMT {NULL, 0, 0, NULL} 159da14cebeSEric Cheng 160da14cebeSEric Cheng typedef struct flow_fields_buf_s 161da14cebeSEric Cheng { 162da000602SGirish Moodalbail char flow_name[MAXFLOWNAMELEN]; 163da14cebeSEric Cheng char flow_link[MAXLINKNAMELEN]; 164da14cebeSEric Cheng char flow_ipaddr[INET6_ADDRSTRLEN+4]; 165da14cebeSEric Cheng char flow_proto[PROTO_MAXSTR_LEN]; 166da14cebeSEric Cheng char flow_port[PORT_MAXSTR_LEN]; 167da14cebeSEric Cheng char flow_dsfield[DSFIELD_MAXSTR_LEN]; 168da14cebeSEric Cheng } flow_fields_buf_t; 169da14cebeSEric Cheng 170*8002d411SSowmini Varadhan static ofmt_field_t flow_fields[] = { 171*8002d411SSowmini Varadhan /* name, field width, index */ 172*8002d411SSowmini Varadhan { "FLOW", 12, 173*8002d411SSowmini Varadhan offsetof(flow_fields_buf_t, flow_name), print_default_cb}, 174*8002d411SSowmini Varadhan { "LINK", 12, 175*8002d411SSowmini Varadhan offsetof(flow_fields_buf_t, flow_link), print_default_cb}, 176*8002d411SSowmini Varadhan { "IPADDR", 31, 177*8002d411SSowmini Varadhan offsetof(flow_fields_buf_t, flow_ipaddr), print_default_cb}, 178*8002d411SSowmini Varadhan { "PROTO", 7, 179*8002d411SSowmini Varadhan offsetof(flow_fields_buf_t, flow_proto), print_default_cb}, 180*8002d411SSowmini Varadhan { "PORT", 8, 181*8002d411SSowmini Varadhan offsetof(flow_fields_buf_t, flow_port), print_default_cb}, 182*8002d411SSowmini Varadhan { "DSFLD", 10, 183*8002d411SSowmini Varadhan offsetof(flow_fields_buf_t, flow_dsfield), print_default_cb}, 184*8002d411SSowmini Varadhan NULL_OFMT} 185da14cebeSEric Cheng ; 186da14cebeSEric Cheng 187da14cebeSEric Cheng /* 188da14cebeSEric Cheng * structures for 'flowadm show-flowprop' 189da14cebeSEric Cheng */ 190da14cebeSEric Cheng typedef enum { 191da14cebeSEric Cheng FLOWPROP_FLOW, 192da14cebeSEric Cheng FLOWPROP_PROPERTY, 193da14cebeSEric Cheng FLOWPROP_VALUE, 194da14cebeSEric Cheng FLOWPROP_DEFAULT, 195da14cebeSEric Cheng FLOWPROP_POSSIBLE 196da14cebeSEric Cheng } flowprop_field_index_t; 197da14cebeSEric Cheng 198*8002d411SSowmini Varadhan static ofmt_field_t flowprop_fields[] = { 199*8002d411SSowmini Varadhan /* name, fieldwidth, index, callback */ 200*8002d411SSowmini Varadhan { "FLOW", 13, FLOWPROP_FLOW, print_flowprop_cb}, 201*8002d411SSowmini Varadhan { "PROPERTY", 16, FLOWPROP_PROPERTY, print_flowprop_cb}, 202*8002d411SSowmini Varadhan { "VALUE", 15, FLOWPROP_VALUE, print_flowprop_cb}, 203*8002d411SSowmini Varadhan { "DEFAULT", 15, FLOWPROP_DEFAULT, print_flowprop_cb}, 204*8002d411SSowmini Varadhan { "POSSIBLE", 21, FLOWPROP_POSSIBLE, print_flowprop_cb}, 205*8002d411SSowmini Varadhan NULL_OFMT} 206da14cebeSEric Cheng ; 207da14cebeSEric Cheng 208da14cebeSEric Cheng #define MAX_PROP_LINE 512 209da14cebeSEric Cheng 210da14cebeSEric Cheng typedef struct show_flowprop_state { 211da14cebeSEric Cheng const char *fs_flow; 212da14cebeSEric Cheng datalink_id_t fs_linkid; 213da14cebeSEric Cheng char *fs_line; 214da14cebeSEric Cheng char **fs_propvals; 215da14cebeSEric Cheng dladm_arg_list_t *fs_proplist; 216*8002d411SSowmini Varadhan boolean_t fs_parsable; 217da14cebeSEric Cheng boolean_t fs_persist; 218da14cebeSEric Cheng boolean_t fs_header; 219da14cebeSEric Cheng dladm_status_t fs_status; 220da14cebeSEric Cheng dladm_status_t fs_retstatus; 221*8002d411SSowmini Varadhan ofmt_handle_t fs_ofmt; 222da14cebeSEric Cheng } show_flowprop_state_t; 223da14cebeSEric Cheng 224da14cebeSEric Cheng typedef struct set_flowprop_state { 225da14cebeSEric Cheng const char *fs_name; 226da14cebeSEric Cheng boolean_t fs_reset; 227da14cebeSEric Cheng boolean_t fs_temp; 228da14cebeSEric Cheng dladm_status_t fs_status; 229da14cebeSEric Cheng } set_flowprop_state_t; 230da14cebeSEric Cheng 231da14cebeSEric Cheng typedef struct flowprop_args_s { 232da14cebeSEric Cheng show_flowprop_state_t *fs_state; 233da14cebeSEric Cheng char *fs_propname; 234da14cebeSEric Cheng char *fs_flowname; 235da14cebeSEric Cheng } flowprop_args_t; 236ae6aa22aSVenugopal Iyer /* 237ae6aa22aSVenugopal Iyer * structures for 'flowadm show-flow -s' (print statistics) 238ae6aa22aSVenugopal Iyer */ 239ae6aa22aSVenugopal Iyer typedef enum { 240ae6aa22aSVenugopal Iyer FLOW_S_FLOW, 241ae6aa22aSVenugopal Iyer FLOW_S_IPKTS, 242ae6aa22aSVenugopal Iyer FLOW_S_RBYTES, 243ae6aa22aSVenugopal Iyer FLOW_S_IERRORS, 244ae6aa22aSVenugopal Iyer FLOW_S_OPKTS, 245ae6aa22aSVenugopal Iyer FLOW_S_OBYTES, 246ae6aa22aSVenugopal Iyer FLOW_S_OERRORS 247ae6aa22aSVenugopal Iyer } flow_s_field_index_t; 248ae6aa22aSVenugopal Iyer 249*8002d411SSowmini Varadhan static ofmt_field_t flow_s_fields[] = { 250*8002d411SSowmini Varadhan /* name, field width, index, callback */ 251*8002d411SSowmini Varadhan { "FLOW", 15, FLOW_S_FLOW, print_flow_stats_cb}, 252*8002d411SSowmini Varadhan { "IPACKETS", 10, FLOW_S_IPKTS, print_flow_stats_cb}, 253*8002d411SSowmini Varadhan { "RBYTES", 8, FLOW_S_RBYTES, print_flow_stats_cb}, 254*8002d411SSowmini Varadhan { "IERRORS", 10, FLOW_S_IERRORS, print_flow_stats_cb}, 255*8002d411SSowmini Varadhan { "OPACKETS", 12, FLOW_S_OPKTS, print_flow_stats_cb}, 256*8002d411SSowmini Varadhan { "OBYTES", 12, FLOW_S_OBYTES, print_flow_stats_cb}, 257*8002d411SSowmini Varadhan { "OERRORS", 8, FLOW_S_OERRORS, print_flow_stats_cb}, 258*8002d411SSowmini Varadhan NULL_OFMT} 259ae6aa22aSVenugopal Iyer ; 260ae6aa22aSVenugopal Iyer 261ae6aa22aSVenugopal Iyer typedef struct flow_args_s { 262ae6aa22aSVenugopal Iyer char *flow_s_flow; 263ae6aa22aSVenugopal Iyer pktsum_t *flow_s_psum; 264ae6aa22aSVenugopal Iyer } flow_args_t; 265da14cebeSEric Cheng 266da14cebeSEric Cheng /* 267ae6aa22aSVenugopal Iyer * structures for 'flowadm show-usage' 268da14cebeSEric Cheng */ 269da14cebeSEric Cheng typedef struct usage_fields_buf_s { 270da14cebeSEric Cheng char usage_flow[12]; 271da14cebeSEric Cheng char usage_duration[10]; 272da14cebeSEric Cheng char usage_ipackets[9]; 273da14cebeSEric Cheng char usage_rbytes[10]; 274da14cebeSEric Cheng char usage_opackets[9]; 275da14cebeSEric Cheng char usage_obytes[10]; 276da14cebeSEric Cheng char usage_bandwidth[14]; 277da14cebeSEric Cheng } usage_fields_buf_t; 278da14cebeSEric Cheng 279*8002d411SSowmini Varadhan static ofmt_field_t usage_fields[] = { 280*8002d411SSowmini Varadhan /* name, field width, offset */ 281*8002d411SSowmini Varadhan { "FLOW", 13, 282*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_flow), print_default_cb}, 283*8002d411SSowmini Varadhan { "DURATION", 11, 284*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_duration), print_default_cb}, 285*8002d411SSowmini Varadhan { "IPACKETS", 10, 286*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_ipackets), print_default_cb}, 287*8002d411SSowmini Varadhan { "RBYTES", 11, 288*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_rbytes), print_default_cb}, 289*8002d411SSowmini Varadhan { "OPACKETS", 10, 290*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_opackets), print_default_cb}, 291*8002d411SSowmini Varadhan { "OBYTES", 11, 292*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_obytes), print_default_cb}, 293*8002d411SSowmini Varadhan { "BANDWIDTH", 15, 294*8002d411SSowmini Varadhan offsetof(usage_fields_buf_t, usage_bandwidth), print_default_cb}, 295*8002d411SSowmini Varadhan NULL_OFMT} 296da14cebeSEric Cheng ; 297da14cebeSEric Cheng 298da14cebeSEric Cheng /* 299da14cebeSEric Cheng * structures for 'dladm show-usage link' 300da14cebeSEric Cheng */ 301da14cebeSEric Cheng 302da14cebeSEric Cheng typedef struct usage_l_fields_buf_s { 303da14cebeSEric Cheng char usage_l_flow[12]; 304da14cebeSEric Cheng char usage_l_stime[13]; 305da14cebeSEric Cheng char usage_l_etime[13]; 306da14cebeSEric Cheng char usage_l_rbytes[8]; 307da14cebeSEric Cheng char usage_l_obytes[8]; 308da14cebeSEric Cheng char usage_l_bandwidth[14]; 309da14cebeSEric Cheng } usage_l_fields_buf_t; 310da14cebeSEric Cheng 311*8002d411SSowmini Varadhan static ofmt_field_t usage_l_fields[] = { 312*8002d411SSowmini Varadhan /* name, field width, offset */ 313*8002d411SSowmini Varadhan { "FLOW", 13, 314*8002d411SSowmini Varadhan offsetof(usage_l_fields_buf_t, usage_l_flow), print_default_cb}, 315*8002d411SSowmini Varadhan { "START", 14, 316*8002d411SSowmini Varadhan offsetof(usage_l_fields_buf_t, usage_l_stime), print_default_cb}, 317*8002d411SSowmini Varadhan { "END", 14, 318*8002d411SSowmini Varadhan offsetof(usage_l_fields_buf_t, usage_l_etime), print_default_cb}, 319*8002d411SSowmini Varadhan { "RBYTES", 9, 320*8002d411SSowmini Varadhan offsetof(usage_l_fields_buf_t, usage_l_rbytes), print_default_cb}, 321*8002d411SSowmini Varadhan { "OBYTES", 9, 322*8002d411SSowmini Varadhan offsetof(usage_l_fields_buf_t, usage_l_obytes), print_default_cb}, 323*8002d411SSowmini Varadhan { "BANDWIDTH", 15, 324*8002d411SSowmini Varadhan offsetof(usage_l_fields_buf_t, usage_l_bandwidth), print_default_cb}, 325*8002d411SSowmini Varadhan NULL_OFMT} 326da14cebeSEric Cheng ; 327da14cebeSEric Cheng 328da14cebeSEric Cheng #define PRI_HI 100 329da14cebeSEric Cheng #define PRI_LO 10 330da14cebeSEric Cheng #define PRI_NORM 50 331da14cebeSEric Cheng 332da14cebeSEric Cheng #define FLOWADM_CONF "/etc/dladm/flowadm.conf" 333da14cebeSEric Cheng #define BLANK_LINE(s) ((s[0] == '\0') || (s[0] == '#') || (s[0] == '\n')) 334da14cebeSEric Cheng 335da14cebeSEric Cheng static char *progname; 336da14cebeSEric Cheng 337da14cebeSEric Cheng boolean_t t_arg = B_FALSE; /* changes are persistent */ 338da14cebeSEric Cheng char *altroot = NULL; 339da14cebeSEric Cheng 3404ac67f02SAnurag S. Maskey /* 3414ac67f02SAnurag S. Maskey * Handle to libdladm. Opened in main() before the sub-command 3424ac67f02SAnurag S. Maskey * specific function is called. 3434ac67f02SAnurag S. Maskey */ 3444ac67f02SAnurag S. Maskey static dladm_handle_t handle = NULL; 3454ac67f02SAnurag S. Maskey 346da14cebeSEric Cheng static const char *attr_table[] = 347da14cebeSEric Cheng {"local_ip", "remote_ip", "transport", "local_port", "dsfield"}; 348da14cebeSEric Cheng 349da14cebeSEric Cheng #define NATTR (sizeof (attr_table)/sizeof (char *)) 350da14cebeSEric Cheng 351da14cebeSEric Cheng static void 352da14cebeSEric Cheng usage(void) 353da14cebeSEric Cheng { 354da14cebeSEric Cheng (void) fprintf(stderr, gettext("usage: flowadm <subcommand>" 355da14cebeSEric Cheng " <args>...\n" 3560790b6dcSAnurag S. Maskey " add-flow [-t] -l <link> -a <attr>=<value>[,...]\n" 3570790b6dcSAnurag S. Maskey "\t\t [-p <prop>=<value>,...] <flow>\n" 3580790b6dcSAnurag S. Maskey " remove-flow [-t] {-l <link> | <flow>}\n" 3590790b6dcSAnurag S. Maskey " show-flow [-p] [-s [-i <interval>]] [-l <link>] " 3600790b6dcSAnurag S. Maskey "[<flow>]\n\n" 3610790b6dcSAnurag S. Maskey " set-flowprop [-t] -p <prop>=<value>[,...] <flow>\n" 3620790b6dcSAnurag S. Maskey " reset-flowprop [-t] [-p <prop>,...] <flow>\n" 3630790b6dcSAnurag S. Maskey " show-flowprop [-cP] [-l <link>] [-p <prop>,...] " 3640790b6dcSAnurag S. Maskey "[<flow>]\n\n" 365ae6aa22aSVenugopal Iyer " show-usage [-a] [-d | -F <format>] " 3660790b6dcSAnurag S. Maskey "[-s <DD/MM/YYYY,HH:MM:SS>]\n" 3670790b6dcSAnurag S. Maskey "\t\t [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<flow>]\n")); 3684ac67f02SAnurag S. Maskey 3694ac67f02SAnurag S. Maskey /* close dladm handle if it was opened */ 3704ac67f02SAnurag S. Maskey if (handle != NULL) 3714ac67f02SAnurag S. Maskey dladm_close(handle); 3724ac67f02SAnurag S. Maskey 373da14cebeSEric Cheng exit(1); 374da14cebeSEric Cheng } 375da14cebeSEric Cheng 376da14cebeSEric Cheng int 377da14cebeSEric Cheng main(int argc, char *argv[]) 378da14cebeSEric Cheng { 379da14cebeSEric Cheng int i, arglen, cmdlen; 380da14cebeSEric Cheng cmd_t *cmdp; 3814ac67f02SAnurag S. Maskey dladm_status_t status; 382da14cebeSEric Cheng 383da14cebeSEric Cheng (void) setlocale(LC_ALL, ""); 384da14cebeSEric Cheng #if !defined(TEXT_DOMAIN) 385da14cebeSEric Cheng #define TEXT_DOMAIN "SYS_TEST" 386da14cebeSEric Cheng #endif 387da14cebeSEric Cheng (void) textdomain(TEXT_DOMAIN); 388da14cebeSEric Cheng 389da14cebeSEric Cheng progname = argv[0]; 390da14cebeSEric Cheng 391da14cebeSEric Cheng if (argc < 2) 392da14cebeSEric Cheng usage(); 393da14cebeSEric Cheng 394da14cebeSEric Cheng for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) { 395da14cebeSEric Cheng cmdp = &cmds[i]; 396da14cebeSEric Cheng arglen = strlen(argv[1]); 397da14cebeSEric Cheng cmdlen = strlen(cmdp->c_name); 398da14cebeSEric Cheng if ((arglen == cmdlen) && (strncmp(argv[1], cmdp->c_name, 399da14cebeSEric Cheng cmdlen) == 0)) { 4004ac67f02SAnurag S. Maskey /* Open the libdladm handle */ 4014ac67f02SAnurag S. Maskey if ((status = dladm_open(&handle)) != DLADM_STATUS_OK) { 4024ac67f02SAnurag S. Maskey die_dlerr(status, 4034ac67f02SAnurag S. Maskey "could not open /dev/dld"); 4044ac67f02SAnurag S. Maskey } 4054ac67f02SAnurag S. Maskey 406da14cebeSEric Cheng cmdp->c_fn(argc - 1, &argv[1]); 4074ac67f02SAnurag S. Maskey 4084ac67f02SAnurag S. Maskey dladm_close(handle); 409ad091ee1SMichael Lim exit(EXIT_SUCCESS); 410da14cebeSEric Cheng } 411da14cebeSEric Cheng } 412da14cebeSEric Cheng 413da14cebeSEric Cheng (void) fprintf(stderr, gettext("%s: unknown subcommand '%s'\n"), 414da14cebeSEric Cheng progname, argv[1]); 415da14cebeSEric Cheng usage(); 416da14cebeSEric Cheng 417da14cebeSEric Cheng return (0); 418da14cebeSEric Cheng } 419da14cebeSEric Cheng 420da14cebeSEric Cheng static const char * 421da14cebeSEric Cheng match_attr(char *attr) 422da14cebeSEric Cheng { 423da14cebeSEric Cheng int i; 424da14cebeSEric Cheng 425da14cebeSEric Cheng for (i = 0; i < NATTR; i++) { 426da14cebeSEric Cheng if (strlen(attr) == strlen(attr_table[i]) && 427da14cebeSEric Cheng strncmp(attr, attr_table[i], strlen(attr_table[i])) == 0) { 428da14cebeSEric Cheng return (attr); 429da14cebeSEric Cheng } 430da14cebeSEric Cheng } 431da14cebeSEric Cheng return (NULL); 432da14cebeSEric Cheng } 433da14cebeSEric Cheng 434da14cebeSEric Cheng /* ARGSUSED */ 435da14cebeSEric Cheng static void 436da14cebeSEric Cheng do_init_flow(int argc, char *argv[]) 437da14cebeSEric Cheng { 438da14cebeSEric Cheng dladm_status_t status; 439da14cebeSEric Cheng 4404ac67f02SAnurag S. Maskey status = dladm_flow_init(handle); 441da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 442da14cebeSEric Cheng die_dlerr(status, "flows initialization failed"); 443da14cebeSEric Cheng } 444da14cebeSEric Cheng 445da14cebeSEric Cheng /* ARGSUSED */ 446da14cebeSEric Cheng static int 447da14cebeSEric Cheng show_usage_date(dladm_usage_t *usage, void *arg) 448da14cebeSEric Cheng { 449ae6aa22aSVenugopal Iyer show_usage_state_t *state = (show_usage_state_t *)arg; 450da14cebeSEric Cheng time_t stime; 451da14cebeSEric Cheng char timebuf[20]; 452ae6aa22aSVenugopal Iyer dladm_flow_attr_t attr; 453ae6aa22aSVenugopal Iyer dladm_status_t status; 454ae6aa22aSVenugopal Iyer 455ae6aa22aSVenugopal Iyer /* 456ae6aa22aSVenugopal Iyer * Only show usage information for existing flows unless '-a' 457ae6aa22aSVenugopal Iyer * is specified. 458ae6aa22aSVenugopal Iyer */ 459ae6aa22aSVenugopal Iyer if (!state->us_showall && ((status = dladm_flow_info(handle, 460ae6aa22aSVenugopal Iyer usage->du_name, &attr)) != DLADM_STATUS_OK)) { 461ae6aa22aSVenugopal Iyer return (status); 462ae6aa22aSVenugopal Iyer } 463da14cebeSEric Cheng 464da14cebeSEric Cheng stime = usage->du_stime; 465da14cebeSEric Cheng (void) strftime(timebuf, sizeof (timebuf), "%m/%d/%Y", 466da14cebeSEric Cheng localtime(&stime)); 467da14cebeSEric Cheng (void) printf("%s\n", timebuf); 468da14cebeSEric Cheng 469da14cebeSEric Cheng return (DLADM_STATUS_OK); 470da14cebeSEric Cheng } 471da14cebeSEric Cheng 472da14cebeSEric Cheng static int 473da14cebeSEric Cheng show_usage_time(dladm_usage_t *usage, void *arg) 474da14cebeSEric Cheng { 475da14cebeSEric Cheng show_usage_state_t *state = (show_usage_state_t *)arg; 476da14cebeSEric Cheng char buf[DLADM_STRSIZE]; 477da14cebeSEric Cheng usage_l_fields_buf_t ubuf; 478da14cebeSEric Cheng time_t time; 479da14cebeSEric Cheng double bw; 480ae6aa22aSVenugopal Iyer dladm_flow_attr_t attr; 481ae6aa22aSVenugopal Iyer dladm_status_t status; 482ae6aa22aSVenugopal Iyer 483ae6aa22aSVenugopal Iyer /* 484ae6aa22aSVenugopal Iyer * Only show usage information for existing flows unless '-a' 485ae6aa22aSVenugopal Iyer * is specified. 486ae6aa22aSVenugopal Iyer */ 487ae6aa22aSVenugopal Iyer if (!state->us_showall && ((status = dladm_flow_info(handle, 488ae6aa22aSVenugopal Iyer usage->du_name, &attr)) != DLADM_STATUS_OK)) { 489ae6aa22aSVenugopal Iyer return (status); 490ae6aa22aSVenugopal Iyer } 491da14cebeSEric Cheng 492da14cebeSEric Cheng if (state->us_plot) { 493da14cebeSEric Cheng if (!state->us_printheader) { 494da14cebeSEric Cheng if (state->us_first) { 495da14cebeSEric Cheng (void) printf("# Time"); 496da14cebeSEric Cheng state->us_first = B_FALSE; 497da14cebeSEric Cheng } 498da14cebeSEric Cheng (void) printf(" %s", usage->du_name); 499da14cebeSEric Cheng if (usage->du_last) { 500da14cebeSEric Cheng (void) printf("\n"); 501da14cebeSEric Cheng state->us_first = B_TRUE; 502da14cebeSEric Cheng state->us_printheader = B_TRUE; 503da14cebeSEric Cheng } 504da14cebeSEric Cheng } else { 505da14cebeSEric Cheng if (state->us_first) { 506da14cebeSEric Cheng time = usage->du_etime; 507da14cebeSEric Cheng (void) strftime(buf, sizeof (buf), "%T", 508da14cebeSEric Cheng localtime(&time)); 509da14cebeSEric Cheng state->us_first = B_FALSE; 510da14cebeSEric Cheng (void) printf("%s", buf); 511da14cebeSEric Cheng } 512da14cebeSEric Cheng bw = (double)usage->du_bandwidth/1000; 513da14cebeSEric Cheng (void) printf(" %.2f", bw); 514da14cebeSEric Cheng if (usage->du_last) { 515da14cebeSEric Cheng (void) printf("\n"); 516da14cebeSEric Cheng state->us_first = B_TRUE; 517da14cebeSEric Cheng } 518da14cebeSEric Cheng } 519da14cebeSEric Cheng return (DLADM_STATUS_OK); 520da14cebeSEric Cheng } 521da14cebeSEric Cheng 522da14cebeSEric Cheng bzero(&ubuf, sizeof (ubuf)); 523da14cebeSEric Cheng 524da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_flow, sizeof (ubuf.usage_l_flow), "%s", 525da14cebeSEric Cheng usage->du_name); 526da14cebeSEric Cheng time = usage->du_stime; 527da14cebeSEric Cheng (void) strftime(buf, sizeof (buf), "%T", localtime(&time)); 528da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_stime, sizeof (ubuf.usage_l_stime), "%s", 529da14cebeSEric Cheng buf); 530da14cebeSEric Cheng time = usage->du_etime; 531da14cebeSEric Cheng (void) strftime(buf, sizeof (buf), "%T", localtime(&time)); 532da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_etime, sizeof (ubuf.usage_l_etime), "%s", 533da14cebeSEric Cheng buf); 534da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_rbytes, sizeof (ubuf.usage_l_rbytes), 535da14cebeSEric Cheng "%llu", usage->du_rbytes); 536da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_obytes, sizeof (ubuf.usage_l_obytes), 537da14cebeSEric Cheng "%llu", usage->du_obytes); 538da14cebeSEric Cheng (void) snprintf(ubuf.usage_l_bandwidth, sizeof (ubuf.usage_l_bandwidth), 539da14cebeSEric Cheng "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf)); 540da14cebeSEric Cheng 541*8002d411SSowmini Varadhan ofmt_print(state->us_ofmt, (void *)&ubuf); 542da14cebeSEric Cheng return (DLADM_STATUS_OK); 543da14cebeSEric Cheng } 544da14cebeSEric Cheng 545da14cebeSEric Cheng static int 546da14cebeSEric Cheng show_usage_res(dladm_usage_t *usage, void *arg) 547da14cebeSEric Cheng { 548da14cebeSEric Cheng show_usage_state_t *state = (show_usage_state_t *)arg; 549da14cebeSEric Cheng char buf[DLADM_STRSIZE]; 550da14cebeSEric Cheng usage_fields_buf_t ubuf; 551ae6aa22aSVenugopal Iyer dladm_flow_attr_t attr; 552ae6aa22aSVenugopal Iyer dladm_status_t status; 553ae6aa22aSVenugopal Iyer 554ae6aa22aSVenugopal Iyer /* 555ae6aa22aSVenugopal Iyer * Only show usage information for existing flows unless '-a' 556ae6aa22aSVenugopal Iyer * is specified. 557ae6aa22aSVenugopal Iyer */ 558ae6aa22aSVenugopal Iyer if (!state->us_showall && ((status = dladm_flow_info(handle, 559ae6aa22aSVenugopal Iyer usage->du_name, &attr)) != DLADM_STATUS_OK)) { 560ae6aa22aSVenugopal Iyer return (status); 561ae6aa22aSVenugopal Iyer } 562da14cebeSEric Cheng 563da14cebeSEric Cheng bzero(&ubuf, sizeof (ubuf)); 564da14cebeSEric Cheng 565da14cebeSEric Cheng (void) snprintf(ubuf.usage_flow, sizeof (ubuf.usage_flow), "%s", 566da14cebeSEric Cheng usage->du_name); 567da14cebeSEric Cheng (void) snprintf(ubuf.usage_duration, sizeof (ubuf.usage_duration), 568da14cebeSEric Cheng "%llu", usage->du_duration); 569da14cebeSEric Cheng (void) snprintf(ubuf.usage_ipackets, sizeof (ubuf.usage_ipackets), 570da14cebeSEric Cheng "%llu", usage->du_ipackets); 571da14cebeSEric Cheng (void) snprintf(ubuf.usage_rbytes, sizeof (ubuf.usage_rbytes), 572da14cebeSEric Cheng "%llu", usage->du_rbytes); 573da14cebeSEric Cheng (void) snprintf(ubuf.usage_opackets, sizeof (ubuf.usage_opackets), 574da14cebeSEric Cheng "%llu", usage->du_opackets); 575da14cebeSEric Cheng (void) snprintf(ubuf.usage_obytes, sizeof (ubuf.usage_obytes), 576da14cebeSEric Cheng "%llu", usage->du_obytes); 577da14cebeSEric Cheng (void) snprintf(ubuf.usage_bandwidth, sizeof (ubuf.usage_bandwidth), 578da14cebeSEric Cheng "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf)); 579da14cebeSEric Cheng 580*8002d411SSowmini Varadhan ofmt_print(state->us_ofmt, (void *)&ubuf); 581da14cebeSEric Cheng 582da14cebeSEric Cheng return (DLADM_STATUS_OK); 583da14cebeSEric Cheng } 584da14cebeSEric Cheng 585da14cebeSEric Cheng static boolean_t 586da14cebeSEric Cheng valid_formatspec(char *formatspec_str) 587da14cebeSEric Cheng { 588da14cebeSEric Cheng if (strcmp(formatspec_str, "gnuplot") == 0) 589da14cebeSEric Cheng return (B_TRUE); 590da14cebeSEric Cheng return (B_FALSE); 591da14cebeSEric Cheng } 592da14cebeSEric Cheng 593da14cebeSEric Cheng /* ARGSUSED */ 594da14cebeSEric Cheng static void 595da14cebeSEric Cheng do_show_usage(int argc, char *argv[]) 596da14cebeSEric Cheng { 597da14cebeSEric Cheng char *file = NULL; 598da14cebeSEric Cheng int opt; 599da14cebeSEric Cheng dladm_status_t status; 600da14cebeSEric Cheng boolean_t d_arg = B_FALSE; 601da14cebeSEric Cheng char *stime = NULL; 602da14cebeSEric Cheng char *etime = NULL; 603da14cebeSEric Cheng char *resource = NULL; 604da14cebeSEric Cheng show_usage_state_t state; 605da14cebeSEric Cheng boolean_t o_arg = B_FALSE; 606da14cebeSEric Cheng boolean_t F_arg = B_FALSE; 607da14cebeSEric Cheng char *fields_str = NULL; 608da14cebeSEric Cheng char *formatspec_str = NULL; 609da14cebeSEric Cheng char *all_fields = 610da14cebeSEric Cheng "flow,duration,ipackets,rbytes,opackets,obytes,bandwidth"; 611da14cebeSEric Cheng char *all_l_fields = 612da14cebeSEric Cheng "flow,start,end,rbytes,obytes,bandwidth"; 613*8002d411SSowmini Varadhan ofmt_handle_t ofmt; 614*8002d411SSowmini Varadhan ofmt_status_t oferr; 615*8002d411SSowmini Varadhan uint_t ofmtflags = 0; 616da14cebeSEric Cheng 617da14cebeSEric Cheng bzero(&state, sizeof (show_usage_state_t)); 618*8002d411SSowmini Varadhan state.us_parsable = B_FALSE; 619da14cebeSEric Cheng state.us_printheader = B_FALSE; 620da14cebeSEric Cheng state.us_plot = B_FALSE; 621da14cebeSEric Cheng state.us_first = B_TRUE; 622da14cebeSEric Cheng 623ae6aa22aSVenugopal Iyer while ((opt = getopt(argc, argv, "das:e:o:f:F:")) != -1) { 624da14cebeSEric Cheng switch (opt) { 625da14cebeSEric Cheng case 'd': 626da14cebeSEric Cheng d_arg = B_TRUE; 627da14cebeSEric Cheng break; 628ae6aa22aSVenugopal Iyer case 'a': 629ae6aa22aSVenugopal Iyer state.us_showall = B_TRUE; 630da14cebeSEric Cheng break; 631da14cebeSEric Cheng case 'f': 632da14cebeSEric Cheng file = optarg; 633da14cebeSEric Cheng break; 634da14cebeSEric Cheng case 's': 635da14cebeSEric Cheng stime = optarg; 636da14cebeSEric Cheng break; 637da14cebeSEric Cheng case 'e': 638da14cebeSEric Cheng etime = optarg; 639da14cebeSEric Cheng break; 640da14cebeSEric Cheng case 'o': 641da14cebeSEric Cheng o_arg = B_TRUE; 642da14cebeSEric Cheng fields_str = optarg; 643da14cebeSEric Cheng break; 644da14cebeSEric Cheng case 'F': 645ae6aa22aSVenugopal Iyer state.us_plot = F_arg = B_TRUE; 646da14cebeSEric Cheng formatspec_str = optarg; 647da14cebeSEric Cheng break; 648da14cebeSEric Cheng default: 649da14cebeSEric Cheng die_opterr(optopt, opt); 650da14cebeSEric Cheng } 651da14cebeSEric Cheng } 652da14cebeSEric Cheng 653da14cebeSEric Cheng if (file == NULL) 654da14cebeSEric Cheng die("show-usage requires a file"); 655da14cebeSEric Cheng 656da14cebeSEric Cheng if (optind == (argc-1)) { 657ae6aa22aSVenugopal Iyer dladm_flow_attr_t attr; 658ae6aa22aSVenugopal Iyer 659ae6aa22aSVenugopal Iyer if (!state.us_showall && 660ae6aa22aSVenugopal Iyer dladm_flow_info(handle, resource, &attr) != 661ae6aa22aSVenugopal Iyer DLADM_STATUS_OK) { 662ae6aa22aSVenugopal Iyer die("invalid flow: '%s'", resource); 663ae6aa22aSVenugopal Iyer } 664da14cebeSEric Cheng resource = argv[optind]; 665da14cebeSEric Cheng } 666da14cebeSEric Cheng 667*8002d411SSowmini Varadhan if (state.us_parsable) 668*8002d411SSowmini Varadhan ofmtflags |= OFMT_PARSABLE; 669da14cebeSEric Cheng if (resource == NULL && stime == NULL && etime == NULL) { 670da14cebeSEric Cheng if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) 671da14cebeSEric Cheng fields_str = all_fields; 672*8002d411SSowmini Varadhan oferr = ofmt_open(fields_str, usage_fields, ofmtflags, 673*8002d411SSowmini Varadhan 0, &ofmt); 674da14cebeSEric Cheng } else { 675da14cebeSEric Cheng if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) 676da14cebeSEric Cheng fields_str = all_l_fields; 677*8002d411SSowmini Varadhan oferr = ofmt_open(fields_str, usage_l_fields, ofmtflags, 678*8002d411SSowmini Varadhan 0, &ofmt); 679da14cebeSEric Cheng } 680da14cebeSEric Cheng 681*8002d411SSowmini Varadhan flowadm_ofmt_check(oferr, state.us_parsable, ofmt); 682*8002d411SSowmini Varadhan state.us_ofmt = ofmt; 683da14cebeSEric Cheng 684ae6aa22aSVenugopal Iyer if (F_arg && d_arg) 685ae6aa22aSVenugopal Iyer die("incompatible -d and -F options"); 686da14cebeSEric Cheng 687da14cebeSEric Cheng if (F_arg && valid_formatspec(formatspec_str) == B_FALSE) 688da14cebeSEric Cheng die("Format specifier %s not supported", formatspec_str); 689da14cebeSEric Cheng 690da14cebeSEric Cheng if (d_arg) { 691da14cebeSEric Cheng /* Print log dates */ 692da14cebeSEric Cheng status = dladm_usage_dates(show_usage_date, 693da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, resource, &state); 694da14cebeSEric Cheng } else if (resource == NULL && stime == NULL && etime == NULL && 695ae6aa22aSVenugopal Iyer !F_arg) { 696da14cebeSEric Cheng /* Print summary */ 697da14cebeSEric Cheng status = dladm_usage_summary(show_usage_res, 698da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, &state); 699da14cebeSEric Cheng } else if (resource != NULL) { 700da14cebeSEric Cheng /* Print log entries for named resource */ 701da14cebeSEric Cheng status = dladm_walk_usage_res(show_usage_time, 702da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, resource, stime, etime, &state); 703da14cebeSEric Cheng } else { 704da14cebeSEric Cheng /* Print time and information for each link */ 705da14cebeSEric Cheng status = dladm_walk_usage_time(show_usage_time, 706da14cebeSEric Cheng DLADM_LOGTYPE_FLOW, file, stime, etime, &state); 707da14cebeSEric Cheng } 708da14cebeSEric Cheng 709*8002d411SSowmini Varadhan ofmt_close(ofmt); 710da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 711da14cebeSEric Cheng die_dlerr(status, "show-usage"); 712da14cebeSEric Cheng } 713da14cebeSEric Cheng 714da14cebeSEric Cheng static void 715da14cebeSEric Cheng do_add_flow(int argc, char *argv[]) 716da14cebeSEric Cheng { 717da000602SGirish Moodalbail char devname[MAXLINKNAMELEN]; 718da14cebeSEric Cheng char *name = NULL; 719da14cebeSEric Cheng uint_t index; 720da14cebeSEric Cheng datalink_id_t linkid; 721da14cebeSEric Cheng 722da14cebeSEric Cheng char option; 723da14cebeSEric Cheng boolean_t l_arg = B_FALSE; 72463a6526dSMichael Lim char propstr[DLADM_STRSIZE]; 72563a6526dSMichael Lim char attrstr[DLADM_STRSIZE]; 726da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 727da14cebeSEric Cheng dladm_arg_list_t *attrlist = NULL; 728da14cebeSEric Cheng dladm_status_t status; 729da14cebeSEric Cheng 73063a6526dSMichael Lim bzero(propstr, DLADM_STRSIZE); 73163a6526dSMichael Lim bzero(attrstr, DLADM_STRSIZE); 73263a6526dSMichael Lim 733da14cebeSEric Cheng while ((option = getopt_long(argc, argv, "tR:l:a:p:", 734da14cebeSEric Cheng prop_longopts, NULL)) != -1) { 735da14cebeSEric Cheng switch (option) { 736da14cebeSEric Cheng case 't': 737da14cebeSEric Cheng t_arg = B_TRUE; 738da14cebeSEric Cheng break; 739da14cebeSEric Cheng case 'R': 740da14cebeSEric Cheng altroot = optarg; 741da14cebeSEric Cheng break; 742da14cebeSEric Cheng case 'l': 743da14cebeSEric Cheng if (strlcpy(devname, optarg, 744da000602SGirish Moodalbail MAXLINKNAMELEN) >= MAXLINKNAMELEN) { 745da14cebeSEric Cheng die("link name too long"); 746da14cebeSEric Cheng } 7474ac67f02SAnurag S. Maskey if (dladm_name2info(handle, devname, &linkid, NULL, 748da14cebeSEric Cheng NULL, NULL) != DLADM_STATUS_OK) 749da14cebeSEric Cheng die("invalid link '%s'", devname); 750da14cebeSEric Cheng l_arg = B_TRUE; 751da14cebeSEric Cheng break; 752da14cebeSEric Cheng case 'a': 75363a6526dSMichael Lim (void) strlcat(attrstr, optarg, DLADM_STRSIZE); 75463a6526dSMichael Lim if (strlcat(attrstr, ",", DLADM_STRSIZE) >= 75563a6526dSMichael Lim DLADM_STRSIZE) 75663a6526dSMichael Lim die("attribute list too long '%s'", attrstr); 757da14cebeSEric Cheng break; 758da14cebeSEric Cheng case 'p': 75963a6526dSMichael Lim (void) strlcat(propstr, optarg, DLADM_STRSIZE); 76063a6526dSMichael Lim if (strlcat(propstr, ",", DLADM_STRSIZE) >= 76163a6526dSMichael Lim DLADM_STRSIZE) 76263a6526dSMichael Lim die("property list too long '%s'", propstr); 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 78463a6526dSMichael Lim if (dladm_parse_flow_attrs(attrstr, &attrlist, B_FALSE) 78563a6526dSMichael Lim != DLADM_STATUS_OK) 78663a6526dSMichael Lim die("invalid flow attribute specified"); 78763a6526dSMichael Lim if (dladm_parse_flow_props(propstr, &proplist, B_FALSE) 78863a6526dSMichael Lim != DLADM_STATUS_OK) 78963a6526dSMichael Lim die("invalid flow property specified"); 79063a6526dSMichael Lim 7914ac67f02SAnurag S. Maskey status = dladm_flow_add(handle, linkid, attrlist, proplist, name, 792da14cebeSEric Cheng t_arg, altroot); 793da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 794da14cebeSEric Cheng die_dlerr(status, "add flow failed"); 795da14cebeSEric Cheng 796da14cebeSEric Cheng dladm_free_attrs(attrlist); 797da14cebeSEric Cheng dladm_free_props(proplist); 798da14cebeSEric Cheng } 799da14cebeSEric Cheng 800da14cebeSEric Cheng static void 801da14cebeSEric Cheng do_remove_flow(int argc, char *argv[]) 802da14cebeSEric Cheng { 803da14cebeSEric Cheng char option; 804da14cebeSEric Cheng char *flowname = NULL; 805da000602SGirish Moodalbail char linkname[MAXLINKNAMELEN]; 806da14cebeSEric Cheng datalink_id_t linkid = DATALINK_ALL_LINKID; 807da14cebeSEric Cheng boolean_t l_arg = B_FALSE; 808da14cebeSEric Cheng remove_flow_state_t state; 809da14cebeSEric Cheng dladm_status_t status; 810da14cebeSEric Cheng 811da14cebeSEric Cheng bzero(&state, sizeof (state)); 812da14cebeSEric Cheng 813da14cebeSEric Cheng opterr = 0; 814da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":tR:l:", 815da14cebeSEric Cheng longopts, NULL)) != -1) { 816da14cebeSEric Cheng switch (option) { 817da14cebeSEric Cheng case 't': 818da14cebeSEric Cheng t_arg = B_TRUE; 819da14cebeSEric Cheng break; 820da14cebeSEric Cheng case 'R': 821da14cebeSEric Cheng altroot = optarg; 822da14cebeSEric Cheng break; 823da14cebeSEric Cheng case 'l': 824da14cebeSEric Cheng if (strlcpy(linkname, optarg, 825da14cebeSEric Cheng MAXLINKNAMELEN) >= MAXLINKNAMELEN) { 826da14cebeSEric Cheng die("link name too long"); 827da14cebeSEric Cheng } 8284ac67f02SAnurag S. Maskey if (dladm_name2info(handle, linkname, &linkid, NULL, 829da14cebeSEric Cheng NULL, NULL) != DLADM_STATUS_OK) { 830da14cebeSEric Cheng die("invalid link '%s'", linkname); 831da14cebeSEric Cheng } 832da14cebeSEric Cheng l_arg = B_TRUE; 833da14cebeSEric Cheng break; 834da14cebeSEric Cheng default: 835da14cebeSEric Cheng die_opterr(optopt, option); 836da14cebeSEric Cheng break; 837da14cebeSEric Cheng } 838da14cebeSEric Cheng } 839da14cebeSEric Cheng 840da14cebeSEric Cheng /* when link not specified get flow name */ 841da14cebeSEric Cheng if (!l_arg) { 842da14cebeSEric Cheng if (optind != (argc-1)) { 843da14cebeSEric Cheng usage(); 844da14cebeSEric Cheng } else { 845da000602SGirish Moodalbail if (strlen(argv[optind]) >= MAXFLOWNAMELEN) 846da14cebeSEric Cheng die("flow name too long"); 847da14cebeSEric Cheng flowname = argv[optind]; 848da14cebeSEric Cheng } 8494ac67f02SAnurag S. Maskey status = dladm_flow_remove(handle, flowname, t_arg, altroot); 850da14cebeSEric Cheng } else { 851da14cebeSEric Cheng /* if link is specified then flow name should not be there */ 852da14cebeSEric Cheng if (optind == argc-1) 853da14cebeSEric Cheng usage(); 854da14cebeSEric Cheng /* walk the link to find flows and remove them */ 855da14cebeSEric Cheng state.fs_tempop = t_arg; 856da14cebeSEric Cheng state.fs_altroot = altroot; 857da14cebeSEric Cheng state.fs_status = DLADM_STATUS_OK; 8584ac67f02SAnurag S. Maskey status = dladm_walk_flow(remove_flow, handle, linkid, &state, 8594ac67f02SAnurag S. Maskey B_FALSE); 860da14cebeSEric Cheng /* 861da14cebeSEric Cheng * check if dladm_walk_flow terminated early and see if the 862da14cebeSEric Cheng * walker function as any status for us 863da14cebeSEric Cheng */ 864da14cebeSEric Cheng if (status == DLADM_STATUS_OK) 865da14cebeSEric Cheng status = state.fs_status; 866da14cebeSEric Cheng } 867da14cebeSEric Cheng 868da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 869da14cebeSEric Cheng die_dlerr(status, "remove flow failed"); 870da14cebeSEric Cheng } 871da14cebeSEric Cheng 872da14cebeSEric Cheng /* 873da14cebeSEric Cheng * Walker function for removing a flow through dladm_walk_flow(); 874da14cebeSEric Cheng */ 875da14cebeSEric Cheng static int 876da14cebeSEric Cheng remove_flow(dladm_flow_attr_t *attr, void *arg) 877da14cebeSEric Cheng { 878da14cebeSEric Cheng remove_flow_state_t *state = (remove_flow_state_t *)arg; 879da14cebeSEric Cheng 8804ac67f02SAnurag S. Maskey state->fs_status = dladm_flow_remove(handle, attr->fa_flowname, 881da14cebeSEric Cheng state->fs_tempop, state->fs_altroot); 882da14cebeSEric Cheng 883da14cebeSEric Cheng if (state->fs_status == DLADM_STATUS_OK) 884da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 885da14cebeSEric Cheng else 886da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 887da14cebeSEric Cheng } 888da14cebeSEric Cheng 889da14cebeSEric Cheng /*ARGSUSED*/ 890da14cebeSEric Cheng static dladm_status_t 891da14cebeSEric Cheng print_flow(show_flow_state_t *state, dladm_flow_attr_t *attr, 892da14cebeSEric Cheng flow_fields_buf_t *fbuf) 893da14cebeSEric Cheng { 894da14cebeSEric Cheng char link[MAXLINKNAMELEN]; 895da14cebeSEric Cheng dladm_status_t status; 896da14cebeSEric Cheng 8974ac67f02SAnurag S. Maskey if ((status = dladm_datalink_id2info(handle, attr->fa_linkid, NULL, 8984ac67f02SAnurag S. Maskey NULL, NULL, link, sizeof (link))) != DLADM_STATUS_OK) { 899da14cebeSEric Cheng return (status); 900da14cebeSEric Cheng } 901da14cebeSEric Cheng 902da14cebeSEric Cheng (void) snprintf(fbuf->flow_name, sizeof (fbuf->flow_name), 903da14cebeSEric Cheng "%s", attr->fa_flowname); 904da14cebeSEric Cheng (void) snprintf(fbuf->flow_link, sizeof (fbuf->flow_link), 905da14cebeSEric Cheng "%s", link); 906da14cebeSEric Cheng 907da14cebeSEric Cheng (void) dladm_flow_attr_ip2str(attr, fbuf->flow_ipaddr, 908da14cebeSEric Cheng sizeof (fbuf->flow_ipaddr)); 909da14cebeSEric Cheng (void) dladm_flow_attr_proto2str(attr, fbuf->flow_proto, 910da14cebeSEric Cheng sizeof (fbuf->flow_proto)); 911da14cebeSEric Cheng (void) dladm_flow_attr_port2str(attr, fbuf->flow_port, 912da14cebeSEric Cheng sizeof (fbuf->flow_port)); 913da14cebeSEric Cheng (void) dladm_flow_attr_dsfield2str(attr, fbuf->flow_dsfield, 914da14cebeSEric Cheng sizeof (fbuf->flow_dsfield)); 915da14cebeSEric Cheng 916da14cebeSEric Cheng return (DLADM_STATUS_OK); 917da14cebeSEric Cheng } 918da14cebeSEric Cheng 919da14cebeSEric Cheng /* 920da14cebeSEric Cheng * Walker function for showing flow attributes through dladm_walk_flow(). 921da14cebeSEric Cheng */ 922da14cebeSEric Cheng static int 923da14cebeSEric Cheng show_flow(dladm_flow_attr_t *attr, void *arg) 924da14cebeSEric Cheng { 925da14cebeSEric Cheng show_flow_state_t *statep = arg; 926da14cebeSEric Cheng dladm_status_t status; 927da14cebeSEric Cheng flow_fields_buf_t fbuf; 928da14cebeSEric Cheng 929da14cebeSEric Cheng /* 930da14cebeSEric Cheng * first get all the flow attributes into fbuf; 931da14cebeSEric Cheng */ 932da14cebeSEric Cheng bzero(&fbuf, sizeof (fbuf)); 933da14cebeSEric Cheng status = print_flow(statep, attr, &fbuf); 934da14cebeSEric Cheng 935da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 936da14cebeSEric Cheng goto done; 937da14cebeSEric Cheng 938*8002d411SSowmini Varadhan ofmt_print(statep->fs_ofmt, (void *)&fbuf); 939da14cebeSEric Cheng 940da14cebeSEric Cheng done: 941da14cebeSEric Cheng statep->fs_status = status; 942da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 943da14cebeSEric Cheng } 944da14cebeSEric Cheng 945da14cebeSEric Cheng static void 946da14cebeSEric Cheng show_one_flow(void *arg, const char *name) 947da14cebeSEric Cheng { 948da14cebeSEric Cheng dladm_flow_attr_t attr; 949da14cebeSEric Cheng 9504ac67f02SAnurag S. Maskey if (dladm_flow_info(handle, name, &attr) != DLADM_STATUS_OK) 951da14cebeSEric Cheng die("invalid flow: '%s'", name); 952da14cebeSEric Cheng else 9534ac67f02SAnurag S. Maskey (void) show_flow(&attr, arg); 954da14cebeSEric Cheng } 955da14cebeSEric Cheng 956da14cebeSEric Cheng /* 957da14cebeSEric Cheng * Wrapper of dladm_walk_flow(show_flow,...) to make it usable to 958da14cebeSEric Cheng * dladm_walk_datalink_id(). Used for showing flow attributes for 959da14cebeSEric Cheng * all flows on all links. 960da14cebeSEric Cheng */ 961da14cebeSEric Cheng static int 9624ac67f02SAnurag S. Maskey show_flows_onelink(dladm_handle_t dh, datalink_id_t linkid, void *arg) 963da14cebeSEric Cheng { 964da14cebeSEric Cheng show_flow_state_t *state = arg; 965da14cebeSEric Cheng 9664ac67f02SAnurag S. Maskey (void) dladm_walk_flow(show_flow, dh, linkid, arg, state->fs_persist); 967da14cebeSEric Cheng 968da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 969da14cebeSEric Cheng } 970da14cebeSEric Cheng 971da14cebeSEric Cheng static void 972da14cebeSEric Cheng get_flow_stats(const char *flowname, pktsum_t *stats) 973da14cebeSEric Cheng { 974da14cebeSEric Cheng kstat_ctl_t *kcp; 975da14cebeSEric Cheng kstat_t *ksp; 976da14cebeSEric Cheng 977da14cebeSEric Cheng bzero(stats, sizeof (*stats)); 978da14cebeSEric Cheng 979da14cebeSEric Cheng if ((kcp = kstat_open()) == NULL) { 980da14cebeSEric Cheng warn("kstat open operation failed"); 981da14cebeSEric Cheng return; 982da14cebeSEric Cheng } 983da14cebeSEric Cheng 984da14cebeSEric Cheng ksp = dladm_kstat_lookup(kcp, NULL, -1, flowname, "flow"); 985da14cebeSEric Cheng 986da14cebeSEric Cheng if (ksp != NULL) 987da14cebeSEric Cheng dladm_get_stats(kcp, ksp, stats); 988da14cebeSEric Cheng 989da14cebeSEric Cheng (void) kstat_close(kcp); 990da14cebeSEric Cheng } 991da14cebeSEric Cheng 992*8002d411SSowmini Varadhan static boolean_t 993*8002d411SSowmini Varadhan print_flow_stats_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize) 994ae6aa22aSVenugopal Iyer { 995*8002d411SSowmini Varadhan flow_args_t *fargs = of_arg->ofmt_cbarg; 996ae6aa22aSVenugopal Iyer pktsum_t *diff_stats = fargs->flow_s_psum; 997ae6aa22aSVenugopal Iyer 998*8002d411SSowmini Varadhan switch (of_arg->ofmt_id) { 999ae6aa22aSVenugopal Iyer case FLOW_S_FLOW: 1000*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%s", fargs->flow_s_flow); 1001ae6aa22aSVenugopal Iyer break; 1002ae6aa22aSVenugopal Iyer case FLOW_S_IPKTS: 1003*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%llu", 1004ae6aa22aSVenugopal Iyer diff_stats->ipackets); 1005ae6aa22aSVenugopal Iyer break; 1006ae6aa22aSVenugopal Iyer case FLOW_S_RBYTES: 1007*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%llu", 1008ae6aa22aSVenugopal Iyer diff_stats->rbytes); 1009ae6aa22aSVenugopal Iyer break; 1010ae6aa22aSVenugopal Iyer case FLOW_S_IERRORS: 1011*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%u", 1012ae6aa22aSVenugopal Iyer diff_stats->ierrors); 1013ae6aa22aSVenugopal Iyer break; 1014ae6aa22aSVenugopal Iyer case FLOW_S_OPKTS: 1015*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%llu", 1016ae6aa22aSVenugopal Iyer diff_stats->opackets); 1017ae6aa22aSVenugopal Iyer break; 1018ae6aa22aSVenugopal Iyer case FLOW_S_OBYTES: 1019*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%llu", 1020ae6aa22aSVenugopal Iyer diff_stats->obytes); 1021ae6aa22aSVenugopal Iyer break; 1022ae6aa22aSVenugopal Iyer case FLOW_S_OERRORS: 1023*8002d411SSowmini Varadhan (void) snprintf(buf, bufsize, "%u", 1024ae6aa22aSVenugopal Iyer diff_stats->oerrors); 1025ae6aa22aSVenugopal Iyer break; 1026ae6aa22aSVenugopal Iyer default: 1027ae6aa22aSVenugopal Iyer die("invalid input"); 1028ae6aa22aSVenugopal Iyer break; 1029ae6aa22aSVenugopal Iyer } 1030*8002d411SSowmini Varadhan return (B_TRUE); 1031ae6aa22aSVenugopal Iyer } 1032*8002d411SSowmini Varadhan 1033da14cebeSEric Cheng /* ARGSUSED */ 1034da14cebeSEric Cheng static int 1035da14cebeSEric Cheng show_flow_stats(dladm_flow_attr_t *attr, void *arg) 1036da14cebeSEric Cheng { 1037da14cebeSEric Cheng show_flow_state_t *state = (show_flow_state_t *)arg; 1038ae6aa22aSVenugopal Iyer char *name = attr->fa_flowname; 1039da14cebeSEric Cheng pktsum_t stats, diff_stats; 1040ae6aa22aSVenugopal Iyer flow_args_t fargs; 1041da14cebeSEric Cheng 1042da14cebeSEric Cheng if (state->fs_firstonly) { 1043da14cebeSEric Cheng if (state->fs_donefirst) 1044da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 1045da14cebeSEric Cheng state->fs_donefirst = B_TRUE; 1046da14cebeSEric Cheng } else { 1047da14cebeSEric Cheng bzero(&state->fs_prevstats, sizeof (state->fs_prevstats)); 1048da14cebeSEric Cheng } 1049da14cebeSEric Cheng 1050da14cebeSEric Cheng get_flow_stats(name, &stats); 1051da14cebeSEric Cheng dladm_stats_diff(&diff_stats, &stats, &state->fs_prevstats); 1052da14cebeSEric Cheng 1053ae6aa22aSVenugopal Iyer fargs.flow_s_flow = name; 1054ae6aa22aSVenugopal Iyer fargs.flow_s_psum = &diff_stats; 1055*8002d411SSowmini Varadhan ofmt_print(state->fs_ofmt, (void *)&fargs); 1056da14cebeSEric Cheng state->fs_prevstats = stats; 1057da14cebeSEric Cheng 1058da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1059da14cebeSEric Cheng } 1060da14cebeSEric Cheng 1061da14cebeSEric Cheng /* 1062da14cebeSEric Cheng * Wrapper of dladm_walk_flow(show_flow,...) to make it usable for 1063da14cebeSEric Cheng * dladm_walk_datalink_id(). Used for showing flow stats for 1064da14cebeSEric Cheng * all flows on all links. 1065da14cebeSEric Cheng */ 1066da14cebeSEric Cheng static int 10674ac67f02SAnurag S. Maskey show_link_flow_stats(dladm_handle_t dh, datalink_id_t linkid, void * arg) 1068da14cebeSEric Cheng { 10694ac67f02SAnurag S. Maskey if (dladm_walk_flow(show_flow_stats, dh, linkid, arg, B_FALSE) 1070da14cebeSEric Cheng == DLADM_STATUS_OK) 1071da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1072da14cebeSEric Cheng else 1073da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 1074da14cebeSEric Cheng } 1075da14cebeSEric Cheng 1076da14cebeSEric Cheng /* ARGSUSED */ 1077da14cebeSEric Cheng static void 1078ae6aa22aSVenugopal Iyer flow_stats(const char *flow, datalink_id_t linkid, uint_t interval, 1079ae6aa22aSVenugopal Iyer char *fields_str, show_flow_state_t *state) 1080da14cebeSEric Cheng { 1081da14cebeSEric Cheng dladm_flow_attr_t attr; 1082*8002d411SSowmini Varadhan ofmt_handle_t ofmt; 1083*8002d411SSowmini Varadhan ofmt_status_t oferr; 1084*8002d411SSowmini Varadhan uint_t ofmtflags = 0; 1085ae6aa22aSVenugopal Iyer 1086*8002d411SSowmini Varadhan oferr = ofmt_open(fields_str, flow_s_fields, ofmtflags, 0, &ofmt); 1087*8002d411SSowmini Varadhan flowadm_ofmt_check(oferr, state->fs_parsable, ofmt); 1088*8002d411SSowmini Varadhan state->fs_ofmt = ofmt; 1089da14cebeSEric Cheng 10904ac67f02SAnurag S. Maskey if (flow != NULL && 10914ac67f02SAnurag S. Maskey dladm_flow_info(handle, flow, &attr) != DLADM_STATUS_OK) 1092da14cebeSEric Cheng die("invalid flow %s", flow); 1093da14cebeSEric Cheng 1094da14cebeSEric Cheng /* 1095da14cebeSEric Cheng * If an interval is specified, continuously show the stats 1096da14cebeSEric Cheng * for only the first flow. 1097da14cebeSEric Cheng */ 1098ae6aa22aSVenugopal Iyer state->fs_firstonly = (interval != 0); 1099da14cebeSEric Cheng 1100da14cebeSEric Cheng for (;;) { 1101ae6aa22aSVenugopal Iyer state->fs_donefirst = B_FALSE; 1102da14cebeSEric Cheng 1103da14cebeSEric Cheng /* Show stats for named flow */ 1104da14cebeSEric Cheng if (flow != NULL) { 1105ae6aa22aSVenugopal Iyer state->fs_flow = flow; 1106ae6aa22aSVenugopal Iyer (void) show_flow_stats(&attr, state); 1107da14cebeSEric Cheng 1108da14cebeSEric Cheng /* Show all stats on a link */ 1109da14cebeSEric Cheng } else if (linkid != DATALINK_INVALID_LINKID) { 11104ac67f02SAnurag S. Maskey (void) dladm_walk_flow(show_flow_stats, handle, linkid, 1111ae6aa22aSVenugopal Iyer state, B_FALSE); 1112da14cebeSEric Cheng 1113da14cebeSEric Cheng /* Show all stats by datalink */ 1114da14cebeSEric Cheng } else { 1115da14cebeSEric Cheng (void) dladm_walk_datalink_id(show_link_flow_stats, 1116ae6aa22aSVenugopal Iyer handle, state, DATALINK_CLASS_ALL, 11174ac67f02SAnurag S. Maskey DATALINK_ANY_MEDIATYPE, DLADM_OPT_ACTIVE); 1118da14cebeSEric Cheng } 1119da14cebeSEric Cheng 1120da14cebeSEric Cheng if (interval == 0) 1121da14cebeSEric Cheng break; 1122da14cebeSEric Cheng 1123da14cebeSEric Cheng (void) sleep(interval); 1124da14cebeSEric Cheng } 1125*8002d411SSowmini Varadhan ofmt_close(ofmt); 1126da14cebeSEric Cheng } 1127da14cebeSEric Cheng 1128da14cebeSEric Cheng static void 1129da14cebeSEric Cheng do_show_flow(int argc, char *argv[]) 1130da14cebeSEric Cheng { 1131da000602SGirish Moodalbail char flowname[MAXFLOWNAMELEN]; 1132da000602SGirish Moodalbail char linkname[MAXLINKNAMELEN]; 1133da14cebeSEric Cheng datalink_id_t linkid = DATALINK_ALL_LINKID; 1134da14cebeSEric Cheng int option; 1135da14cebeSEric Cheng boolean_t s_arg = B_FALSE; 1136da14cebeSEric Cheng boolean_t S_arg = B_FALSE; 1137da14cebeSEric Cheng boolean_t i_arg = B_FALSE; 1138da14cebeSEric Cheng boolean_t l_arg = B_FALSE; 1139da14cebeSEric Cheng boolean_t o_arg = B_FALSE; 1140da14cebeSEric Cheng uint32_t interval = 0; 1141da14cebeSEric Cheng show_flow_state_t state; 1142da14cebeSEric Cheng char *fields_str = NULL; 1143*8002d411SSowmini Varadhan ofmt_handle_t ofmt; 1144*8002d411SSowmini Varadhan ofmt_status_t oferr; 1145*8002d411SSowmini Varadhan uint_t ofmtflags = 0; 1146da14cebeSEric Cheng 1147da14cebeSEric Cheng bzero(&state, sizeof (state)); 1148da14cebeSEric Cheng 1149da14cebeSEric Cheng opterr = 0; 1150da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":pPsSi:l:o:", 1151da14cebeSEric Cheng longopts, NULL)) != -1) { 1152da14cebeSEric Cheng switch (option) { 1153da14cebeSEric Cheng case 'p': 1154*8002d411SSowmini Varadhan state.fs_parsable = B_TRUE; 1155*8002d411SSowmini Varadhan ofmtflags |= OFMT_PARSABLE; 1156da14cebeSEric Cheng break; 1157da14cebeSEric Cheng case 'P': 1158da14cebeSEric Cheng state.fs_persist = B_TRUE; 1159da14cebeSEric Cheng break; 1160da14cebeSEric Cheng case 's': 1161da14cebeSEric Cheng if (s_arg) 1162da14cebeSEric Cheng die_optdup(option); 1163da14cebeSEric Cheng 1164da14cebeSEric Cheng s_arg = B_TRUE; 1165da14cebeSEric Cheng break; 1166da14cebeSEric Cheng case 'S': 1167da14cebeSEric Cheng if (S_arg) 1168da14cebeSEric Cheng die_optdup(option); 1169da14cebeSEric Cheng 1170da14cebeSEric Cheng S_arg = B_TRUE; 1171da14cebeSEric Cheng break; 1172da14cebeSEric Cheng case 'o': 1173da14cebeSEric Cheng if (o_arg) 1174da14cebeSEric Cheng die_optdup(option); 1175da14cebeSEric Cheng 1176da14cebeSEric Cheng o_arg = B_TRUE; 1177da14cebeSEric Cheng fields_str = optarg; 1178da14cebeSEric Cheng break; 1179da14cebeSEric Cheng case 'i': 1180da14cebeSEric Cheng if (i_arg) 1181da14cebeSEric Cheng die_optdup(option); 1182da14cebeSEric Cheng 1183da14cebeSEric Cheng i_arg = B_TRUE; 1184da14cebeSEric Cheng 118563a6526dSMichael Lim if (!dladm_str2interval(optarg, &interval)) 118663a6526dSMichael Lim die("invalid interval value '%s'", optarg); 1187da14cebeSEric Cheng break; 1188da14cebeSEric Cheng case 'l': 1189da14cebeSEric Cheng if (strlcpy(linkname, optarg, MAXLINKNAMELEN) 1190da14cebeSEric Cheng >= MAXLINKNAMELEN) 1191da14cebeSEric Cheng die("link name too long\n"); 11924ac67f02SAnurag S. Maskey if (dladm_name2info(handle, linkname, &linkid, NULL, 1193da14cebeSEric Cheng NULL, NULL) != DLADM_STATUS_OK) 1194da14cebeSEric Cheng die("invalid link '%s'", linkname); 1195da14cebeSEric Cheng l_arg = B_TRUE; 1196da14cebeSEric Cheng break; 1197da14cebeSEric Cheng default: 1198da14cebeSEric Cheng die_opterr(optopt, option); 1199da14cebeSEric Cheng break; 1200da14cebeSEric Cheng } 1201da14cebeSEric Cheng } 1202da14cebeSEric Cheng if (i_arg && !(s_arg || S_arg)) 1203da14cebeSEric Cheng die("the -i option can be used only with -s or -S"); 1204da14cebeSEric Cheng 1205da14cebeSEric Cheng if (s_arg && S_arg) 1206da14cebeSEric Cheng die("the -s option cannot be used with -S"); 1207da14cebeSEric Cheng 1208da14cebeSEric Cheng /* get flow name (optional last argument */ 1209da14cebeSEric Cheng if (optind == (argc-1)) { 1210da000602SGirish Moodalbail if (strlcpy(flowname, argv[optind], MAXFLOWNAMELEN) 1211da000602SGirish Moodalbail >= MAXFLOWNAMELEN) 1212da14cebeSEric Cheng die("flow name too long"); 1213da14cebeSEric Cheng state.fs_flow = flowname; 1214da14cebeSEric Cheng } 1215da14cebeSEric Cheng 1216da14cebeSEric Cheng if (S_arg) { 12174ac67f02SAnurag S. Maskey dladm_continuous(handle, linkid, state.fs_flow, interval, 12184ac67f02SAnurag S. Maskey FLOW_REPORT); 1219da14cebeSEric Cheng return; 1220da14cebeSEric Cheng } 1221da14cebeSEric Cheng 1222ae6aa22aSVenugopal Iyer if (s_arg) { 1223ae6aa22aSVenugopal Iyer flow_stats(state.fs_flow, linkid, interval, fields_str, &state); 1224ae6aa22aSVenugopal Iyer return; 1225ae6aa22aSVenugopal Iyer } 1226da14cebeSEric Cheng 1227*8002d411SSowmini Varadhan oferr = ofmt_open(fields_str, flow_fields, ofmtflags, 0, &ofmt); 1228*8002d411SSowmini Varadhan flowadm_ofmt_check(oferr, state.fs_parsable, ofmt); 1229*8002d411SSowmini Varadhan state.fs_ofmt = ofmt; 1230da14cebeSEric Cheng 1231da14cebeSEric Cheng /* Show attributes of one flow */ 1232da14cebeSEric Cheng if (state.fs_flow != NULL) { 1233da14cebeSEric Cheng show_one_flow(&state, state.fs_flow); 1234da14cebeSEric Cheng 1235da14cebeSEric Cheng /* Show attributes of flows on one link */ 1236da14cebeSEric Cheng } else if (l_arg) { 12374ac67f02SAnurag S. Maskey (void) show_flows_onelink(handle, linkid, &state); 1238da14cebeSEric Cheng 1239da14cebeSEric Cheng /* Show attributes of all flows on all links */ 1240da14cebeSEric Cheng } else { 12414ac67f02SAnurag S. Maskey (void) dladm_walk_datalink_id(show_flows_onelink, handle, 12424ac67f02SAnurag S. Maskey &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, 1243da14cebeSEric Cheng DLADM_OPT_ACTIVE); 1244da14cebeSEric Cheng } 1245*8002d411SSowmini Varadhan ofmt_close(ofmt); 1246da14cebeSEric Cheng } 1247da14cebeSEric Cheng 1248da14cebeSEric Cheng static dladm_status_t 1249da14cebeSEric Cheng set_flowprop_persist(const char *flow, const char *prop_name, char **prop_val, 1250da14cebeSEric Cheng uint_t val_cnt, boolean_t reset) 1251da14cebeSEric Cheng { 1252da14cebeSEric Cheng dladm_status_t status; 1253da14cebeSEric Cheng char *errprop; 1254da14cebeSEric Cheng 12554ac67f02SAnurag S. Maskey status = dladm_set_flowprop(handle, flow, prop_name, prop_val, val_cnt, 1256da14cebeSEric Cheng DLADM_OPT_PERSIST, &errprop); 1257da14cebeSEric Cheng 1258da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1259da14cebeSEric Cheng warn_dlerr(status, "cannot persistently %s flow " 1260da14cebeSEric Cheng "property '%s' on '%s'", reset? "reset": "set", 1261da14cebeSEric Cheng errprop, flow); 1262da14cebeSEric Cheng } 1263da14cebeSEric Cheng return (status); 1264da14cebeSEric Cheng } 1265da14cebeSEric Cheng 1266da14cebeSEric Cheng static void 1267da14cebeSEric Cheng set_flowprop(int argc, char **argv, boolean_t reset) 1268da14cebeSEric Cheng { 1269da14cebeSEric Cheng int i, option; 1270da14cebeSEric Cheng char errmsg[DLADM_STRSIZE]; 1271da14cebeSEric Cheng const char *flow = NULL; 127263a6526dSMichael Lim char propstr[DLADM_STRSIZE]; 1273da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 1274da14cebeSEric Cheng boolean_t temp = B_FALSE; 1275da14cebeSEric Cheng dladm_status_t status = DLADM_STATUS_OK; 1276da14cebeSEric Cheng 1277da14cebeSEric Cheng opterr = 0; 127863a6526dSMichael Lim bzero(propstr, DLADM_STRSIZE); 127963a6526dSMichael Lim 1280da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":p:R:t", 1281da14cebeSEric Cheng prop_longopts, NULL)) != -1) { 1282da14cebeSEric Cheng switch (option) { 1283da14cebeSEric Cheng case 'p': 128463a6526dSMichael Lim (void) strlcat(propstr, optarg, DLADM_STRSIZE); 128563a6526dSMichael Lim if (strlcat(propstr, ",", DLADM_STRSIZE) >= 128663a6526dSMichael Lim DLADM_STRSIZE) 128763a6526dSMichael Lim die("property list too long '%s'", propstr); 1288da14cebeSEric Cheng break; 1289da14cebeSEric Cheng case 't': 1290da14cebeSEric Cheng temp = B_TRUE; 1291da14cebeSEric Cheng break; 1292da14cebeSEric Cheng case 'R': 1293da14cebeSEric Cheng status = dladm_set_rootdir(optarg); 1294da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1295da14cebeSEric Cheng die_dlerr(status, "invalid directory " 1296da14cebeSEric Cheng "specified"); 1297da14cebeSEric Cheng } 1298da14cebeSEric Cheng break; 1299da14cebeSEric Cheng default: 1300da14cebeSEric Cheng die_opterr(optopt, option); 1301da14cebeSEric Cheng break; 1302da14cebeSEric Cheng } 1303da14cebeSEric Cheng } 1304da14cebeSEric Cheng 1305da14cebeSEric Cheng if (optind == (argc - 1)) { 1306da000602SGirish Moodalbail if (strlen(argv[optind]) >= MAXFLOWNAMELEN) 1307da14cebeSEric Cheng die("flow name too long"); 1308da14cebeSEric Cheng flow = argv[optind]; 1309da14cebeSEric Cheng } else if (optind != argc) { 1310da14cebeSEric Cheng usage(); 1311da14cebeSEric Cheng } 1312da14cebeSEric Cheng if (flow == NULL) 1313da14cebeSEric Cheng die("flow name must be specified"); 1314da14cebeSEric Cheng 131563a6526dSMichael Lim if (dladm_parse_flow_props(propstr, &proplist, reset) 131663a6526dSMichael Lim != DLADM_STATUS_OK) 131763a6526dSMichael Lim die("invalid flow property specified"); 131863a6526dSMichael Lim 1319da14cebeSEric Cheng if (proplist == NULL) { 1320da14cebeSEric Cheng char *errprop; 1321da14cebeSEric Cheng 1322da14cebeSEric Cheng if (!reset) 1323da14cebeSEric Cheng die("flow property must be specified"); 1324da14cebeSEric Cheng 13254ac67f02SAnurag S. Maskey status = dladm_set_flowprop(handle, flow, NULL, NULL, 0, 1326da14cebeSEric Cheng DLADM_OPT_ACTIVE, &errprop); 1327da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1328da14cebeSEric Cheng warn_dlerr(status, "cannot reset flow property '%s' " 1329da14cebeSEric Cheng "on '%s'", errprop, flow); 1330da14cebeSEric Cheng } 1331da14cebeSEric Cheng if (!temp) { 1332da14cebeSEric Cheng dladm_status_t s; 1333da14cebeSEric Cheng 1334da14cebeSEric Cheng s = set_flowprop_persist(flow, NULL, NULL, 0, reset); 1335da14cebeSEric Cheng if (s != DLADM_STATUS_OK) 1336da14cebeSEric Cheng status = s; 1337da14cebeSEric Cheng } 1338da14cebeSEric Cheng goto done; 1339da14cebeSEric Cheng } 1340da14cebeSEric Cheng 1341da14cebeSEric Cheng for (i = 0; i < proplist->al_count; i++) { 1342da14cebeSEric Cheng dladm_arg_info_t *aip = &proplist->al_info[i]; 1343da14cebeSEric Cheng char **val; 1344da14cebeSEric Cheng uint_t count; 1345da14cebeSEric Cheng dladm_status_t s; 1346da14cebeSEric Cheng 1347da14cebeSEric Cheng if (reset) { 1348da14cebeSEric Cheng val = NULL; 1349da14cebeSEric Cheng count = 0; 1350da14cebeSEric Cheng } else { 1351da14cebeSEric Cheng val = aip->ai_val; 1352da14cebeSEric Cheng count = aip->ai_count; 1353da14cebeSEric Cheng if (count == 0) { 1354da14cebeSEric Cheng warn("no value specified for '%s'", 1355da14cebeSEric Cheng aip->ai_name); 1356da14cebeSEric Cheng status = DLADM_STATUS_BADARG; 1357da14cebeSEric Cheng continue; 1358da14cebeSEric Cheng } 1359da14cebeSEric Cheng } 13604ac67f02SAnurag S. Maskey s = dladm_set_flowprop(handle, flow, aip->ai_name, val, count, 1361da14cebeSEric Cheng DLADM_OPT_ACTIVE, NULL); 1362da14cebeSEric Cheng if (s == DLADM_STATUS_OK) { 1363da14cebeSEric Cheng if (!temp) { 1364da14cebeSEric Cheng s = set_flowprop_persist(flow, 1365da14cebeSEric Cheng aip->ai_name, val, count, reset); 1366da14cebeSEric Cheng if (s != DLADM_STATUS_OK) 1367da14cebeSEric Cheng status = s; 1368da14cebeSEric Cheng } 1369da14cebeSEric Cheng continue; 1370da14cebeSEric Cheng } 1371da14cebeSEric Cheng status = s; 1372da14cebeSEric Cheng switch (s) { 1373da14cebeSEric Cheng case DLADM_STATUS_NOTFOUND: 1374da14cebeSEric Cheng warn("invalid flow property '%s'", aip->ai_name); 1375da14cebeSEric Cheng break; 1376da14cebeSEric Cheng case DLADM_STATUS_BADVAL: { 1377da14cebeSEric Cheng int j; 1378da14cebeSEric Cheng char *ptr, *lim; 1379da14cebeSEric Cheng char **propvals = NULL; 1380da14cebeSEric Cheng uint_t valcnt = DLADM_MAX_PROP_VALCNT; 1381da14cebeSEric Cheng 1382da14cebeSEric Cheng ptr = malloc((sizeof (char *) + 1383da14cebeSEric Cheng DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT + 1384da14cebeSEric Cheng MAX_PROP_LINE); 1385da14cebeSEric Cheng 1386da14cebeSEric Cheng if (ptr == NULL) 1387da14cebeSEric Cheng die("insufficient memory"); 1388da14cebeSEric Cheng propvals = (char **)(void *)ptr; 1389da14cebeSEric Cheng 1390da14cebeSEric Cheng for (j = 0; j < DLADM_MAX_PROP_VALCNT; j++) { 1391da14cebeSEric Cheng propvals[j] = ptr + sizeof (char *) * 1392da14cebeSEric Cheng DLADM_MAX_PROP_VALCNT + 1393da14cebeSEric Cheng j * DLADM_PROP_VAL_MAX; 1394da14cebeSEric Cheng } 13954ac67f02SAnurag S. Maskey s = dladm_get_flowprop(handle, flow, 13964ac67f02SAnurag S. Maskey DLADM_PROP_VAL_MODIFIABLE, aip->ai_name, propvals, 13974ac67f02SAnurag S. Maskey &valcnt); 1398da14cebeSEric Cheng 1399da14cebeSEric Cheng ptr = errmsg; 1400da14cebeSEric Cheng lim = ptr + DLADM_STRSIZE; 1401da14cebeSEric Cheng *ptr = '\0'; 1402da14cebeSEric Cheng for (j = 0; j < valcnt && s == DLADM_STATUS_OK; j++) { 1403da14cebeSEric Cheng ptr += snprintf(ptr, lim - ptr, "%s,", 1404da14cebeSEric Cheng propvals[j]); 1405da14cebeSEric Cheng if (ptr >= lim) 1406da14cebeSEric Cheng break; 1407da14cebeSEric Cheng } 1408da14cebeSEric Cheng if (ptr > errmsg) { 1409da14cebeSEric Cheng *(ptr - 1) = '\0'; 1410da14cebeSEric Cheng warn("flow property '%s' must be one of: %s", 1411da14cebeSEric Cheng aip->ai_name, errmsg); 1412da14cebeSEric Cheng } else 1413da14cebeSEric Cheng warn("%s is an invalid value for " 1414da14cebeSEric Cheng "flow property %s", *val, aip->ai_name); 1415da14cebeSEric Cheng free(propvals); 1416da14cebeSEric Cheng break; 1417da14cebeSEric Cheng } 1418da14cebeSEric Cheng default: 1419da14cebeSEric Cheng if (reset) { 1420da14cebeSEric Cheng warn_dlerr(status, "cannot reset flow property " 1421da14cebeSEric Cheng "'%s' on '%s'", aip->ai_name, flow); 1422da14cebeSEric Cheng } else { 1423da14cebeSEric Cheng warn_dlerr(status, "cannot set flow property " 1424da14cebeSEric Cheng "'%s' on '%s'", aip->ai_name, flow); 1425da14cebeSEric Cheng } 1426da14cebeSEric Cheng break; 1427da14cebeSEric Cheng } 1428da14cebeSEric Cheng } 1429da14cebeSEric Cheng done: 1430da14cebeSEric Cheng dladm_free_props(proplist); 14314ac67f02SAnurag S. Maskey if (status != DLADM_STATUS_OK) { 14324ac67f02SAnurag S. Maskey dladm_close(handle); 1433ad091ee1SMichael Lim exit(EXIT_FAILURE); 1434da14cebeSEric Cheng } 14354ac67f02SAnurag S. Maskey } 1436da14cebeSEric Cheng 1437da14cebeSEric Cheng static void 1438da14cebeSEric Cheng do_set_flowprop(int argc, char **argv) 1439da14cebeSEric Cheng { 1440da14cebeSEric Cheng set_flowprop(argc, argv, B_FALSE); 1441da14cebeSEric Cheng } 1442da14cebeSEric Cheng 1443da14cebeSEric Cheng static void 1444da14cebeSEric Cheng do_reset_flowprop(int argc, char **argv) 1445da14cebeSEric Cheng { 1446da14cebeSEric Cheng set_flowprop(argc, argv, B_TRUE); 1447da14cebeSEric Cheng } 1448da14cebeSEric Cheng 1449da14cebeSEric Cheng static void 1450da14cebeSEric Cheng warn(const char *format, ...) 1451da14cebeSEric Cheng { 1452da14cebeSEric Cheng va_list alist; 1453da14cebeSEric Cheng 1454da14cebeSEric Cheng format = gettext(format); 1455da14cebeSEric Cheng (void) fprintf(stderr, "%s: warning: ", progname); 1456da14cebeSEric Cheng 1457da14cebeSEric Cheng va_start(alist, format); 1458da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1459da14cebeSEric Cheng va_end(alist); 1460da14cebeSEric Cheng 1461da14cebeSEric Cheng (void) putchar('\n'); 1462da14cebeSEric Cheng } 1463da14cebeSEric Cheng 1464da14cebeSEric Cheng /* PRINTFLIKE2 */ 1465da14cebeSEric Cheng static void 1466da14cebeSEric Cheng warn_dlerr(dladm_status_t err, const char *format, ...) 1467da14cebeSEric Cheng { 1468da14cebeSEric Cheng va_list alist; 1469da14cebeSEric Cheng char errmsg[DLADM_STRSIZE]; 1470da14cebeSEric Cheng 1471da14cebeSEric Cheng format = gettext(format); 1472da14cebeSEric Cheng (void) fprintf(stderr, gettext("%s: warning: "), progname); 1473da14cebeSEric Cheng 1474da14cebeSEric Cheng va_start(alist, format); 1475da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1476da14cebeSEric Cheng va_end(alist); 1477da14cebeSEric Cheng (void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg)); 1478da14cebeSEric Cheng } 1479da14cebeSEric Cheng 1480da14cebeSEric Cheng /* PRINTFLIKE1 */ 1481da14cebeSEric Cheng static void 1482da14cebeSEric Cheng die(const char *format, ...) 1483da14cebeSEric Cheng { 1484da14cebeSEric Cheng va_list alist; 1485da14cebeSEric Cheng 1486da14cebeSEric Cheng format = gettext(format); 1487da14cebeSEric Cheng (void) fprintf(stderr, "%s: ", progname); 1488da14cebeSEric Cheng 1489da14cebeSEric Cheng va_start(alist, format); 1490da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1491da14cebeSEric Cheng va_end(alist); 1492da14cebeSEric Cheng 1493da14cebeSEric Cheng (void) putchar('\n'); 14944ac67f02SAnurag S. Maskey 14954ac67f02SAnurag S. Maskey /* close dladm handle if it was opened */ 14964ac67f02SAnurag S. Maskey if (handle != NULL) 14974ac67f02SAnurag S. Maskey dladm_close(handle); 14984ac67f02SAnurag S. Maskey 1499da14cebeSEric Cheng exit(EXIT_FAILURE); 1500da14cebeSEric Cheng } 1501da14cebeSEric Cheng 1502da14cebeSEric Cheng static void 1503da14cebeSEric Cheng die_optdup(int opt) 1504da14cebeSEric Cheng { 1505da14cebeSEric Cheng die("the option -%c cannot be specified more than once", opt); 1506da14cebeSEric Cheng } 1507da14cebeSEric Cheng 1508da14cebeSEric Cheng static void 1509da14cebeSEric Cheng die_opterr(int opt, int opterr) 1510da14cebeSEric Cheng { 1511da14cebeSEric Cheng switch (opterr) { 1512da14cebeSEric Cheng case ':': 1513da14cebeSEric Cheng die("option '-%c' requires a value", opt); 1514da14cebeSEric Cheng break; 1515da14cebeSEric Cheng case '?': 1516da14cebeSEric Cheng default: 1517da14cebeSEric Cheng die("unrecognized option '-%c'", opt); 1518da14cebeSEric Cheng break; 1519da14cebeSEric Cheng } 1520da14cebeSEric Cheng } 1521da14cebeSEric Cheng 1522da14cebeSEric Cheng /* PRINTFLIKE2 */ 1523da14cebeSEric Cheng static void 1524da14cebeSEric Cheng die_dlerr(dladm_status_t err, const char *format, ...) 1525da14cebeSEric Cheng { 1526da14cebeSEric Cheng va_list alist; 1527da14cebeSEric Cheng char errmsg[DLADM_STRSIZE]; 1528da14cebeSEric Cheng 1529da14cebeSEric Cheng format = gettext(format); 1530da14cebeSEric Cheng (void) fprintf(stderr, "%s: ", progname); 1531da14cebeSEric Cheng 1532da14cebeSEric Cheng va_start(alist, format); 1533da14cebeSEric Cheng (void) vfprintf(stderr, format, alist); 1534da14cebeSEric Cheng va_end(alist); 1535da14cebeSEric Cheng (void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg)); 1536da14cebeSEric Cheng 15374ac67f02SAnurag S. Maskey /* close dladm handle if it was opened */ 15384ac67f02SAnurag S. Maskey if (handle != NULL) 15394ac67f02SAnurag S. Maskey dladm_close(handle); 15404ac67f02SAnurag S. Maskey 1541da14cebeSEric Cheng exit(EXIT_FAILURE); 1542da14cebeSEric Cheng } 1543da14cebeSEric Cheng 1544da14cebeSEric Cheng static void 1545da14cebeSEric Cheng print_flowprop(const char *flowname, show_flowprop_state_t *statep, 1546da14cebeSEric Cheng const char *propname, dladm_prop_type_t type, 1547da14cebeSEric Cheng const char *format, char **pptr) 1548da14cebeSEric Cheng { 1549da14cebeSEric Cheng int i; 1550da14cebeSEric Cheng char *ptr, *lim; 1551da14cebeSEric Cheng char buf[DLADM_STRSIZE]; 1552da14cebeSEric Cheng char *unknown = "--", *notsup = ""; 1553da14cebeSEric Cheng char **propvals = statep->fs_propvals; 1554da14cebeSEric Cheng uint_t valcnt = DLADM_MAX_PROP_VALCNT; 1555da14cebeSEric Cheng dladm_status_t status; 1556da14cebeSEric Cheng 15574ac67f02SAnurag S. Maskey status = dladm_get_flowprop(handle, flowname, type, propname, propvals, 1558da14cebeSEric Cheng &valcnt); 1559da14cebeSEric Cheng if (status != DLADM_STATUS_OK) { 1560da14cebeSEric Cheng if (status == DLADM_STATUS_TEMPONLY) { 1561da14cebeSEric Cheng if (type == DLADM_PROP_VAL_MODIFIABLE && 1562da14cebeSEric Cheng statep->fs_persist) { 1563da14cebeSEric Cheng valcnt = 1; 1564da14cebeSEric Cheng propvals = &unknown; 1565da14cebeSEric Cheng } else { 1566da14cebeSEric Cheng statep->fs_status = status; 1567da14cebeSEric Cheng statep->fs_retstatus = status; 1568da14cebeSEric Cheng return; 1569da14cebeSEric Cheng } 1570da14cebeSEric Cheng } else if (status == DLADM_STATUS_NOTSUP || 1571da14cebeSEric Cheng statep->fs_persist) { 1572da14cebeSEric Cheng valcnt = 1; 1573da14cebeSEric Cheng if (type == DLADM_PROP_VAL_CURRENT) 1574da14cebeSEric Cheng propvals = &unknown; 1575da14cebeSEric Cheng else 1576da14cebeSEric Cheng propvals = ¬sup; 1577da14cebeSEric Cheng } else { 1578da14cebeSEric Cheng if ((statep->fs_proplist != NULL) && 1579da14cebeSEric Cheng statep->fs_status == DLADM_STATUS_OK) { 1580da14cebeSEric Cheng warn("invalid flow property '%s'", propname); 1581da14cebeSEric Cheng } 1582da14cebeSEric Cheng statep->fs_status = status; 1583da14cebeSEric Cheng statep->fs_retstatus = status; 1584da14cebeSEric Cheng return; 1585da14cebeSEric Cheng } 1586da14cebeSEric Cheng } 1587da14cebeSEric Cheng 1588da14cebeSEric Cheng statep->fs_status = DLADM_STATUS_OK; 1589da14cebeSEric Cheng 1590da14cebeSEric Cheng ptr = buf; 1591da14cebeSEric Cheng lim = buf + DLADM_STRSIZE; 1592da14cebeSEric Cheng for (i = 0; i < valcnt; i++) { 1593*8002d411SSowmini Varadhan if (propvals[i][0] == '\0' && !statep->fs_parsable) 1594*8002d411SSowmini Varadhan ptr += snprintf(ptr, lim - ptr, "--,"); 1595da14cebeSEric Cheng else 1596da14cebeSEric Cheng ptr += snprintf(ptr, lim - ptr, "%s,", propvals[i]); 1597da14cebeSEric Cheng if (ptr >= lim) 1598da14cebeSEric Cheng break; 1599da14cebeSEric Cheng } 1600da14cebeSEric Cheng if (valcnt > 0) 1601da14cebeSEric Cheng buf[strlen(buf) - 1] = '\0'; 1602da14cebeSEric Cheng 1603da14cebeSEric Cheng lim = statep->fs_line + MAX_PROP_LINE; 1604*8002d411SSowmini Varadhan if (statep->fs_parsable) { 1605da14cebeSEric Cheng *pptr += snprintf(*pptr, lim - *pptr, 1606da14cebeSEric Cheng "%s", buf); 1607da14cebeSEric Cheng } else { 1608da14cebeSEric Cheng *pptr += snprintf(*pptr, lim - *pptr, format, buf); 1609da14cebeSEric Cheng } 1610da14cebeSEric Cheng } 1611da14cebeSEric Cheng 1612*8002d411SSowmini Varadhan static boolean_t 1613*8002d411SSowmini Varadhan print_flowprop_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize) 1614da14cebeSEric Cheng { 1615*8002d411SSowmini Varadhan flowprop_args_t *arg = of_arg->ofmt_cbarg; 1616da14cebeSEric Cheng char *propname = arg->fs_propname; 1617da14cebeSEric Cheng show_flowprop_state_t *statep = arg->fs_state; 1618da14cebeSEric Cheng char *ptr = statep->fs_line; 1619da14cebeSEric Cheng char *lim = ptr + MAX_PROP_LINE; 1620da14cebeSEric Cheng char *flowname = arg->fs_flowname; 1621da14cebeSEric Cheng 1622*8002d411SSowmini Varadhan switch (of_arg->ofmt_id) { 1623da14cebeSEric Cheng case FLOWPROP_FLOW: 1624da14cebeSEric Cheng (void) snprintf(ptr, lim - ptr, "%s", statep->fs_flow); 1625da14cebeSEric Cheng break; 1626da14cebeSEric Cheng case FLOWPROP_PROPERTY: 1627da14cebeSEric Cheng (void) snprintf(ptr, lim - ptr, "%s", propname); 1628da14cebeSEric Cheng break; 1629da14cebeSEric Cheng case FLOWPROP_VALUE: 1630da14cebeSEric Cheng print_flowprop(flowname, statep, propname, 1631da14cebeSEric Cheng statep->fs_persist ? DLADM_PROP_VAL_PERSISTENT : 1632da14cebeSEric Cheng DLADM_PROP_VAL_CURRENT, "%s", &ptr); 1633da14cebeSEric Cheng /* 1634da14cebeSEric Cheng * If we failed to query the flow property, for example, query 1635da14cebeSEric Cheng * the persistent value of a non-persistable flow property, 1636da14cebeSEric Cheng * simply skip the output. 1637da14cebeSEric Cheng */ 1638da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1639da14cebeSEric Cheng goto skip; 1640da14cebeSEric Cheng ptr = statep->fs_line; 1641da14cebeSEric Cheng break; 1642da14cebeSEric Cheng case FLOWPROP_DEFAULT: 1643da14cebeSEric Cheng print_flowprop(flowname, statep, propname, 1644da14cebeSEric Cheng DLADM_PROP_VAL_DEFAULT, "%s", &ptr); 1645da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1646da14cebeSEric Cheng goto skip; 1647da14cebeSEric Cheng ptr = statep->fs_line; 1648da14cebeSEric Cheng break; 1649da14cebeSEric Cheng case FLOWPROP_POSSIBLE: 1650da14cebeSEric Cheng print_flowprop(flowname, statep, propname, 1651da14cebeSEric Cheng DLADM_PROP_VAL_MODIFIABLE, "%s ", &ptr); 1652da14cebeSEric Cheng if (statep->fs_status != DLADM_STATUS_OK) 1653da14cebeSEric Cheng goto skip; 1654da14cebeSEric Cheng ptr = statep->fs_line; 1655da14cebeSEric Cheng break; 1656da14cebeSEric Cheng default: 1657da14cebeSEric Cheng die("invalid input"); 1658da14cebeSEric Cheng break; 1659da14cebeSEric Cheng } 1660*8002d411SSowmini Varadhan (void) strlcpy(buf, ptr, bufsize); 1661*8002d411SSowmini Varadhan return (B_TRUE); 1662da14cebeSEric Cheng skip: 1663*8002d411SSowmini Varadhan buf[0] = '\0'; 1664*8002d411SSowmini Varadhan return ((statep->fs_status == DLADM_STATUS_OK) ? 1665*8002d411SSowmini Varadhan B_TRUE : B_FALSE); 1666da14cebeSEric Cheng } 1667da14cebeSEric Cheng 1668da14cebeSEric Cheng static int 1669da14cebeSEric Cheng show_one_flowprop(void *arg, const char *propname) 1670da14cebeSEric Cheng { 1671da14cebeSEric Cheng show_flowprop_state_t *statep = arg; 1672da14cebeSEric Cheng flowprop_args_t fs_arg; 1673da14cebeSEric Cheng 1674da14cebeSEric Cheng bzero(&fs_arg, sizeof (fs_arg)); 1675da14cebeSEric Cheng fs_arg.fs_state = statep; 1676da14cebeSEric Cheng fs_arg.fs_propname = (char *)propname; 1677da14cebeSEric Cheng fs_arg.fs_flowname = (char *)statep->fs_flow; 1678da14cebeSEric Cheng 1679*8002d411SSowmini Varadhan ofmt_print(statep->fs_ofmt, (void *)&fs_arg); 1680da14cebeSEric Cheng 1681da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1682da14cebeSEric Cheng } 1683da14cebeSEric Cheng 1684da14cebeSEric Cheng /* Walker function called by dladm_walk_flow to display flow properties */ 1685da14cebeSEric Cheng static int 1686da14cebeSEric Cheng show_flowprop(dladm_flow_attr_t *attr, void *arg) 1687da14cebeSEric Cheng { 1688da14cebeSEric Cheng show_flowprop_one_flow(arg, attr->fa_flowname); 1689da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1690da14cebeSEric Cheng } 1691da14cebeSEric Cheng 1692da14cebeSEric Cheng /* 1693da14cebeSEric Cheng * Wrapper of dladm_walk_flow(show_walk_fn,...) to make it 1694da14cebeSEric Cheng * usable to dladm_walk_datalink_id() 1695da14cebeSEric Cheng */ 1696da14cebeSEric Cheng static int 16974ac67f02SAnurag S. Maskey show_flowprop_onelink(dladm_handle_t dh, datalink_id_t linkid, void *arg) 1698da14cebeSEric Cheng { 1699da14cebeSEric Cheng char name[MAXLINKNAMELEN]; 1700da14cebeSEric Cheng 17014ac67f02SAnurag S. Maskey if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, name, 17024ac67f02SAnurag S. Maskey sizeof (name)) != DLADM_STATUS_OK) 1703da14cebeSEric Cheng return (DLADM_WALK_TERMINATE); 1704da14cebeSEric Cheng 17054ac67f02SAnurag S. Maskey (void) dladm_walk_flow(show_flowprop, dh, linkid, arg, B_FALSE); 1706da14cebeSEric Cheng 1707da14cebeSEric Cheng return (DLADM_WALK_CONTINUE); 1708da14cebeSEric Cheng } 1709da14cebeSEric Cheng 1710da14cebeSEric Cheng static void 1711da14cebeSEric Cheng do_show_flowprop(int argc, char **argv) 1712da14cebeSEric Cheng { 1713da14cebeSEric Cheng int option; 1714da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 1715da14cebeSEric Cheng show_flowprop_state_t state; 1716da14cebeSEric Cheng char *fields_str = NULL; 1717*8002d411SSowmini Varadhan ofmt_handle_t ofmt; 1718*8002d411SSowmini Varadhan ofmt_status_t oferr; 1719*8002d411SSowmini Varadhan uint_t ofmtflags = 0; 1720da14cebeSEric Cheng 1721da14cebeSEric Cheng opterr = 0; 1722da14cebeSEric Cheng state.fs_propvals = NULL; 1723da14cebeSEric Cheng state.fs_line = NULL; 1724*8002d411SSowmini Varadhan state.fs_parsable = B_FALSE; 1725da14cebeSEric Cheng state.fs_persist = B_FALSE; 1726da14cebeSEric Cheng state.fs_header = B_TRUE; 1727da14cebeSEric Cheng state.fs_retstatus = DLADM_STATUS_OK; 1728da14cebeSEric Cheng state.fs_linkid = DATALINK_INVALID_LINKID; 1729da14cebeSEric Cheng state.fs_flow = NULL; 1730da14cebeSEric Cheng 1731da14cebeSEric Cheng while ((option = getopt_long(argc, argv, ":p:cPl:o:", 1732da14cebeSEric Cheng prop_longopts, NULL)) != -1) { 1733da14cebeSEric Cheng switch (option) { 1734da14cebeSEric Cheng case 'p': 1735da14cebeSEric Cheng if (dladm_parse_flow_props(optarg, &proplist, B_TRUE) 1736da14cebeSEric Cheng != DLADM_STATUS_OK) 1737da14cebeSEric Cheng die("invalid flow properties specified"); 1738da14cebeSEric Cheng break; 1739da14cebeSEric Cheng case 'c': 1740*8002d411SSowmini Varadhan state.fs_parsable = B_TRUE; 1741*8002d411SSowmini Varadhan ofmtflags |= OFMT_PARSABLE; 1742da14cebeSEric Cheng break; 1743da14cebeSEric Cheng case 'P': 1744da14cebeSEric Cheng state.fs_persist = B_TRUE; 1745da14cebeSEric Cheng break; 1746da14cebeSEric Cheng case 'l': 17474ac67f02SAnurag S. Maskey if (dladm_name2info(handle, optarg, &state.fs_linkid, 1748da14cebeSEric Cheng NULL, NULL, NULL) != DLADM_STATUS_OK) 1749da14cebeSEric Cheng die("invalid link '%s'", optarg); 1750da14cebeSEric Cheng break; 1751da14cebeSEric Cheng case 'o': 1752da14cebeSEric Cheng fields_str = optarg; 1753da14cebeSEric Cheng break; 1754da14cebeSEric Cheng default: 1755da14cebeSEric Cheng die_opterr(optopt, option); 1756da14cebeSEric Cheng break; 1757da14cebeSEric Cheng } 1758da14cebeSEric Cheng } 1759da14cebeSEric Cheng 1760da14cebeSEric Cheng if (optind == (argc - 1)) { 1761da000602SGirish Moodalbail if (strlen(argv[optind]) >= MAXFLOWNAMELEN) 1762da14cebeSEric Cheng die("flow name too long"); 1763da14cebeSEric Cheng state.fs_flow = argv[optind]; 1764da14cebeSEric Cheng } else if (optind != argc) { 1765da14cebeSEric Cheng usage(); 1766da14cebeSEric Cheng } 1767da14cebeSEric Cheng state.fs_proplist = proplist; 1768da14cebeSEric Cheng state.fs_status = DLADM_STATUS_OK; 1769da14cebeSEric Cheng 1770*8002d411SSowmini Varadhan oferr = ofmt_open(fields_str, flowprop_fields, ofmtflags, 0, &ofmt); 1771*8002d411SSowmini Varadhan flowadm_ofmt_check(oferr, state.fs_parsable, ofmt); 1772*8002d411SSowmini Varadhan state.fs_ofmt = ofmt; 1773da14cebeSEric Cheng 1774da14cebeSEric Cheng /* Show properties for one flow */ 1775da14cebeSEric Cheng if (state.fs_flow != NULL) { 1776da14cebeSEric Cheng show_flowprop_one_flow(&state, state.fs_flow); 1777da14cebeSEric Cheng 1778da14cebeSEric Cheng /* Show properties for all flows on one link */ 1779da14cebeSEric Cheng } else if (state.fs_linkid != DATALINK_INVALID_LINKID) { 17804ac67f02SAnurag S. Maskey (void) show_flowprop_onelink(handle, state.fs_linkid, &state); 1781da14cebeSEric Cheng 1782da14cebeSEric Cheng /* Show properties for all flows on all links */ 1783da14cebeSEric Cheng } else { 17844ac67f02SAnurag S. Maskey (void) dladm_walk_datalink_id(show_flowprop_onelink, handle, 17854ac67f02SAnurag S. Maskey &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, 1786da14cebeSEric Cheng DLADM_OPT_ACTIVE); 1787da14cebeSEric Cheng } 1788da14cebeSEric Cheng 1789da14cebeSEric Cheng dladm_free_props(proplist); 1790*8002d411SSowmini Varadhan ofmt_close(ofmt); 1791da14cebeSEric Cheng } 1792da14cebeSEric Cheng 1793da14cebeSEric Cheng static void 1794da14cebeSEric Cheng show_flowprop_one_flow(void *arg, const char *flow) 1795da14cebeSEric Cheng { 1796da14cebeSEric Cheng int i; 1797da14cebeSEric Cheng char *buf; 1798da14cebeSEric Cheng dladm_status_t status; 1799da14cebeSEric Cheng dladm_arg_list_t *proplist = NULL; 1800da14cebeSEric Cheng show_flowprop_state_t *statep = arg; 1801da14cebeSEric Cheng dladm_flow_attr_t attr; 1802da14cebeSEric Cheng const char *savep; 1803da14cebeSEric Cheng 1804da14cebeSEric Cheng /* 1805da14cebeSEric Cheng * Do not print flow props for invalid flows. 1806da14cebeSEric Cheng */ 18074ac67f02SAnurag S. Maskey if ((status = dladm_flow_info(handle, flow, &attr)) != 18084ac67f02SAnurag S. Maskey DLADM_STATUS_OK) { 1809da14cebeSEric Cheng die("invalid flow: '%s'", flow); 1810da14cebeSEric Cheng } 1811da14cebeSEric Cheng 1812da14cebeSEric Cheng savep = statep->fs_flow; 1813da14cebeSEric Cheng statep->fs_flow = flow; 1814da14cebeSEric Cheng 1815da14cebeSEric Cheng proplist = statep->fs_proplist; 1816da14cebeSEric Cheng 1817da14cebeSEric Cheng buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) 1818da14cebeSEric Cheng * DLADM_MAX_PROP_VALCNT + MAX_PROP_LINE); 1819da14cebeSEric Cheng if (buf == NULL) 1820da14cebeSEric Cheng die("insufficient memory"); 1821da14cebeSEric Cheng 1822da14cebeSEric Cheng statep->fs_propvals = (char **)(void *)buf; 1823da14cebeSEric Cheng for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) { 1824da14cebeSEric Cheng statep->fs_propvals[i] = buf + 1825da14cebeSEric Cheng sizeof (char *) * DLADM_MAX_PROP_VALCNT + 1826da14cebeSEric Cheng i * DLADM_PROP_VAL_MAX; 1827da14cebeSEric Cheng } 1828da14cebeSEric Cheng statep->fs_line = buf + 1829da14cebeSEric Cheng (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT; 1830da14cebeSEric Cheng 1831da14cebeSEric Cheng /* show only specified flow properties */ 1832da14cebeSEric Cheng if (proplist != NULL) { 1833da14cebeSEric Cheng for (i = 0; i < proplist->al_count; i++) { 1834da14cebeSEric Cheng if (show_one_flowprop(statep, 1835da14cebeSEric Cheng proplist->al_info[i].ai_name) != DLADM_STATUS_OK) 1836da14cebeSEric Cheng break; 1837da14cebeSEric Cheng } 1838da14cebeSEric Cheng 1839da14cebeSEric Cheng /* show all flow properties */ 1840da14cebeSEric Cheng } else { 1841da14cebeSEric Cheng status = dladm_walk_flowprop(show_one_flowprop, flow, statep); 1842da14cebeSEric Cheng if (status != DLADM_STATUS_OK) 1843da14cebeSEric Cheng die_dlerr(status, "show-flowprop"); 1844da14cebeSEric Cheng } 1845da14cebeSEric Cheng free(buf); 1846da14cebeSEric Cheng statep->fs_flow = savep; 1847da14cebeSEric Cheng } 1848da14cebeSEric Cheng 1849da14cebeSEric Cheng /* 1850*8002d411SSowmini Varadhan * default output callback function that, when invoked from dladm_print_output, 1851*8002d411SSowmini Varadhan * prints string which is offset by of_arg->ofmt_id within buf. 1852da14cebeSEric Cheng */ 1853*8002d411SSowmini Varadhan static boolean_t 1854*8002d411SSowmini Varadhan print_default_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize) 1855da14cebeSEric Cheng { 1856da14cebeSEric Cheng char *value; 1857da14cebeSEric Cheng 1858*8002d411SSowmini Varadhan value = (char *)of_arg->ofmt_cbarg + of_arg->ofmt_id; 1859*8002d411SSowmini Varadhan (void) strlcpy(buf, value, bufsize); 1860*8002d411SSowmini Varadhan return (B_TRUE); 1861da14cebeSEric Cheng } 1862da14cebeSEric Cheng 1863da14cebeSEric Cheng static void 1864*8002d411SSowmini Varadhan flowadm_ofmt_check(ofmt_status_t oferr, boolean_t parsable, 1865*8002d411SSowmini Varadhan ofmt_handle_t ofmt) 1866da14cebeSEric Cheng { 1867*8002d411SSowmini Varadhan char buf[OFMT_BUFSIZE]; 1868da14cebeSEric Cheng 1869*8002d411SSowmini Varadhan if (oferr == OFMT_SUCCESS) 1870*8002d411SSowmini Varadhan return; 1871*8002d411SSowmini Varadhan (void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf)); 1872ad091ee1SMichael Lim /* 1873*8002d411SSowmini Varadhan * All errors are considered fatal in parsable mode. 1874*8002d411SSowmini Varadhan * NOMEM errors are always fatal, regardless of mode. 1875*8002d411SSowmini Varadhan * For other errors, we print diagnostics in human-readable 1876*8002d411SSowmini Varadhan * mode and processs what we can. 1877ad091ee1SMichael Lim */ 1878*8002d411SSowmini Varadhan if (parsable || oferr == OFMT_ENOFIELDS) { 1879*8002d411SSowmini Varadhan ofmt_close(ofmt); 1880*8002d411SSowmini Varadhan die(buf); 1881da14cebeSEric Cheng } else { 1882*8002d411SSowmini Varadhan warn(buf); 1883da14cebeSEric Cheng } 1884da14cebeSEric Cheng } 1885