xref: /titanic_54/usr/src/cmd/flowadm/flowadm.c (revision c3affd820c380dd131cbf6584013c11487d84089)
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>
528002d411SSowmini Varadhan #include <ofmt.h>
53da14cebeSEric Cheng 
54da14cebeSEric Cheng typedef struct show_usage_state_s {
55da14cebeSEric Cheng 	boolean_t	us_plot;
568002d411SSowmini Varadhan 	boolean_t	us_parsable;
57da14cebeSEric Cheng 	boolean_t	us_printheader;
58da14cebeSEric Cheng 	boolean_t	us_first;
59ae6aa22aSVenugopal Iyer 	boolean_t	us_showall;
608002d411SSowmini 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;
698002d411SSowmini Varadhan 	ofmt_handle_t		fs_ofmt;
70ae6aa22aSVenugopal Iyer 	const char		*fs_flow;
71ae6aa22aSVenugopal Iyer 	const char		*fs_link;
728002d411SSowmini 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 
84*c3affd82SMichael Lim static int	show_flow(dladm_handle_t, 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 *);
90*c3affd82SMichael Lim static int	show_flow_stats(dladm_handle_t, dladm_flow_attr_t *, void *);
914ac67f02SAnurag S. Maskey static int	show_link_flow_stats(dladm_handle_t, datalink_id_t, void *);
92da14cebeSEric Cheng 
93*c3affd82SMichael Lim static int	remove_flow(dladm_handle_t, dladm_flow_attr_t *, void *);
94da14cebeSEric Cheng 
95*c3affd82SMichael Lim static int	show_flowprop(dladm_handle_t, 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 
1068002d411SSowmini Varadhan /* callback functions for printing output */
1078002d411SSowmini Varadhan static ofmt_cb_t print_flowprop_cb, print_default_cb, print_flow_stats_cb;
1088002d411SSowmini Varadhan static void flowadm_ofmt_check(ofmt_status_t, boolean_t, ofmt_handle_t);
1098002d411SSowmini 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'},
1288002d411SSowmini 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
1588002d411SSowmini 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 
1708002d411SSowmini Varadhan static ofmt_field_t flow_fields[] = {
1718002d411SSowmini Varadhan /* name,	field width,	index */
1728002d411SSowmini Varadhan {  "FLOW",	12,
1738002d411SSowmini Varadhan 	offsetof(flow_fields_buf_t, flow_name), print_default_cb},
1748002d411SSowmini Varadhan {  "LINK",	12,
1758002d411SSowmini Varadhan 	offsetof(flow_fields_buf_t, flow_link), print_default_cb},
1768002d411SSowmini Varadhan {  "IPADDR",	31,
1778002d411SSowmini Varadhan 	offsetof(flow_fields_buf_t, flow_ipaddr), print_default_cb},
1788002d411SSowmini Varadhan {  "PROTO",	7,
1798002d411SSowmini Varadhan 	offsetof(flow_fields_buf_t, flow_proto), print_default_cb},
1808002d411SSowmini Varadhan {  "PORT",	8,
1818002d411SSowmini Varadhan 	offsetof(flow_fields_buf_t, flow_port), print_default_cb},
1828002d411SSowmini Varadhan {  "DSFLD",	10,
1838002d411SSowmini Varadhan 	offsetof(flow_fields_buf_t, flow_dsfield), print_default_cb},
1848002d411SSowmini 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 
1988002d411SSowmini Varadhan static ofmt_field_t flowprop_fields[] = {
1998002d411SSowmini Varadhan /* name,	fieldwidth,	index, 		callback */
2008002d411SSowmini Varadhan { "FLOW",	13,	FLOWPROP_FLOW,		print_flowprop_cb},
2018002d411SSowmini Varadhan { "PROPERTY",	16,	FLOWPROP_PROPERTY,	print_flowprop_cb},
2028002d411SSowmini Varadhan { "VALUE",	15,	FLOWPROP_VALUE,		print_flowprop_cb},
2038002d411SSowmini Varadhan { "DEFAULT",	15,	FLOWPROP_DEFAULT,	print_flowprop_cb},
2048002d411SSowmini Varadhan { "POSSIBLE",	21,	FLOWPROP_POSSIBLE,	print_flowprop_cb},
2058002d411SSowmini 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;
2168002d411SSowmini 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;
2218002d411SSowmini 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 
2498002d411SSowmini Varadhan static ofmt_field_t flow_s_fields[] = {
2508002d411SSowmini Varadhan /* name,	field width,	index,		callback */
2518002d411SSowmini Varadhan { "FLOW",	15,	FLOW_S_FLOW,	print_flow_stats_cb},
2528002d411SSowmini Varadhan { "IPACKETS",	10,	FLOW_S_IPKTS,	print_flow_stats_cb},
2538002d411SSowmini Varadhan { "RBYTES",	8,	FLOW_S_RBYTES,	print_flow_stats_cb},
2548002d411SSowmini Varadhan { "IERRORS",	10,	FLOW_S_IERRORS,	print_flow_stats_cb},
2558002d411SSowmini Varadhan { "OPACKETS",	12,	FLOW_S_OPKTS,	print_flow_stats_cb},
2568002d411SSowmini Varadhan { "OBYTES",	12,	FLOW_S_OBYTES,	print_flow_stats_cb},
2578002d411SSowmini Varadhan { "OERRORS",	8,	FLOW_S_OERRORS,	print_flow_stats_cb},
2588002d411SSowmini 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 
2798002d411SSowmini Varadhan static ofmt_field_t usage_fields[] = {
2808002d411SSowmini Varadhan /* name,	field width,	offset */
2818002d411SSowmini Varadhan { "FLOW",	13,
2828002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_flow), print_default_cb},
2838002d411SSowmini Varadhan { "DURATION",	11,
2848002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_duration), print_default_cb},
2858002d411SSowmini Varadhan { "IPACKETS",	10,
2868002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_ipackets), print_default_cb},
2878002d411SSowmini Varadhan { "RBYTES",	11,
2888002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_rbytes), print_default_cb},
2898002d411SSowmini Varadhan { "OPACKETS",	10,
2908002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_opackets), print_default_cb},
2918002d411SSowmini Varadhan { "OBYTES",	11,
2928002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_obytes), print_default_cb},
2938002d411SSowmini Varadhan { "BANDWIDTH",	15,
2948002d411SSowmini Varadhan 	offsetof(usage_fields_buf_t, usage_bandwidth), print_default_cb},
2958002d411SSowmini 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 
3118002d411SSowmini Varadhan static ofmt_field_t usage_l_fields[] = {
3128002d411SSowmini Varadhan /* name,	field width,	offset */
3138002d411SSowmini Varadhan { "FLOW",	13,
3148002d411SSowmini Varadhan 	offsetof(usage_l_fields_buf_t, usage_l_flow), print_default_cb},
3158002d411SSowmini Varadhan { "START",	14,
3168002d411SSowmini Varadhan 	offsetof(usage_l_fields_buf_t, usage_l_stime), print_default_cb},
3178002d411SSowmini Varadhan { "END",	14,
3188002d411SSowmini Varadhan 	offsetof(usage_l_fields_buf_t, usage_l_etime), print_default_cb},
3198002d411SSowmini Varadhan { "RBYTES",	9,
3208002d411SSowmini Varadhan 	offsetof(usage_l_fields_buf_t, usage_l_rbytes), print_default_cb},
3218002d411SSowmini Varadhan { "OBYTES",	9,
3228002d411SSowmini Varadhan 	offsetof(usage_l_fields_buf_t, usage_l_obytes), print_default_cb},
3238002d411SSowmini Varadhan { "BANDWIDTH",	15,
3248002d411SSowmini Varadhan 	offsetof(usage_l_fields_buf_t, usage_l_bandwidth), print_default_cb},
3258002d411SSowmini 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 
5418002d411SSowmini 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 
5808002d411SSowmini 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";
6138002d411SSowmini Varadhan 	ofmt_handle_t		ofmt;
6148002d411SSowmini Varadhan 	ofmt_status_t		oferr;
6158002d411SSowmini Varadhan 	uint_t			ofmtflags = 0;
616da14cebeSEric Cheng 
617da14cebeSEric Cheng 	bzero(&state, sizeof (show_usage_state_t));
6188002d411SSowmini 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 
6678002d411SSowmini Varadhan 	if (state.us_parsable)
6688002d411SSowmini 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;
6728002d411SSowmini Varadhan 		oferr = ofmt_open(fields_str, usage_fields, ofmtflags,
6738002d411SSowmini 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;
6778002d411SSowmini Varadhan 		oferr = ofmt_open(fields_str, usage_l_fields, ofmtflags,
6788002d411SSowmini Varadhan 		    0, &ofmt);
679da14cebeSEric Cheng 	}
680da14cebeSEric Cheng 
6818002d411SSowmini Varadhan 	flowadm_ofmt_check(oferr, state.us_parsable, ofmt);
6828002d411SSowmini 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 
7098002d411SSowmini 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  */
875*c3affd82SMichael Lim /*ARGSUSED*/
876da14cebeSEric Cheng static int
877*c3affd82SMichael Lim remove_flow(dladm_handle_t handle, dladm_flow_attr_t *attr, void *arg)
878da14cebeSEric Cheng {
879da14cebeSEric Cheng 	remove_flow_state_t	*state = (remove_flow_state_t *)arg;
880da14cebeSEric Cheng 
8814ac67f02SAnurag S. Maskey 	state->fs_status = dladm_flow_remove(handle, attr->fa_flowname,
882da14cebeSEric Cheng 	    state->fs_tempop, state->fs_altroot);
883da14cebeSEric Cheng 
884da14cebeSEric Cheng 	if (state->fs_status == DLADM_STATUS_OK)
885da14cebeSEric Cheng 		return (DLADM_WALK_CONTINUE);
886da14cebeSEric Cheng 	else
887da14cebeSEric Cheng 		return (DLADM_WALK_TERMINATE);
888da14cebeSEric Cheng }
889da14cebeSEric Cheng 
890da14cebeSEric Cheng /*ARGSUSED*/
891da14cebeSEric Cheng static dladm_status_t
892da14cebeSEric Cheng print_flow(show_flow_state_t *state, dladm_flow_attr_t *attr,
893da14cebeSEric Cheng     flow_fields_buf_t *fbuf)
894da14cebeSEric Cheng {
895da14cebeSEric Cheng 	char		link[MAXLINKNAMELEN];
896da14cebeSEric Cheng 	dladm_status_t	status;
897da14cebeSEric Cheng 
8984ac67f02SAnurag S. Maskey 	if ((status = dladm_datalink_id2info(handle, attr->fa_linkid, NULL,
8994ac67f02SAnurag S. Maskey 	    NULL, NULL, link, sizeof (link))) != DLADM_STATUS_OK) {
900da14cebeSEric Cheng 		return (status);
901da14cebeSEric Cheng 	}
902da14cebeSEric Cheng 
903da14cebeSEric Cheng 	(void) snprintf(fbuf->flow_name, sizeof (fbuf->flow_name),
904da14cebeSEric Cheng 	    "%s", attr->fa_flowname);
905da14cebeSEric Cheng 	(void) snprintf(fbuf->flow_link, sizeof (fbuf->flow_link),
906da14cebeSEric Cheng 	    "%s", link);
907da14cebeSEric Cheng 
908da14cebeSEric Cheng 	(void) dladm_flow_attr_ip2str(attr, fbuf->flow_ipaddr,
909da14cebeSEric Cheng 	    sizeof (fbuf->flow_ipaddr));
910da14cebeSEric Cheng 	(void) dladm_flow_attr_proto2str(attr, fbuf->flow_proto,
911da14cebeSEric Cheng 	    sizeof (fbuf->flow_proto));
912da14cebeSEric Cheng 	(void) dladm_flow_attr_port2str(attr, fbuf->flow_port,
913da14cebeSEric Cheng 	    sizeof (fbuf->flow_port));
914da14cebeSEric Cheng 	(void) dladm_flow_attr_dsfield2str(attr, fbuf->flow_dsfield,
915da14cebeSEric Cheng 	    sizeof (fbuf->flow_dsfield));
916da14cebeSEric Cheng 
917da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
918da14cebeSEric Cheng }
919da14cebeSEric Cheng 
920da14cebeSEric Cheng /*
921da14cebeSEric Cheng  * Walker function for showing flow attributes through dladm_walk_flow().
922da14cebeSEric Cheng  */
923*c3affd82SMichael Lim /*ARGSUSED*/
924da14cebeSEric Cheng static int
925*c3affd82SMichael Lim show_flow(dladm_handle_t handle, 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 
9408002d411SSowmini Varadhan 	ofmt_print(statep->fs_ofmt, (void *)&fbuf);
941da14cebeSEric Cheng 
942da14cebeSEric Cheng done:
943da14cebeSEric Cheng 	statep->fs_status = status;
944da14cebeSEric Cheng 	return (DLADM_WALK_CONTINUE);
945da14cebeSEric Cheng }
946da14cebeSEric Cheng 
947da14cebeSEric Cheng static void
948da14cebeSEric Cheng show_one_flow(void *arg, const char *name)
949da14cebeSEric Cheng {
950da14cebeSEric Cheng 	dladm_flow_attr_t	attr;
951da14cebeSEric Cheng 
9524ac67f02SAnurag S. Maskey 	if (dladm_flow_info(handle, name, &attr) != DLADM_STATUS_OK)
953da14cebeSEric Cheng 		die("invalid flow: '%s'", name);
954da14cebeSEric Cheng 	else
955*c3affd82SMichael Lim 		(void) show_flow(handle, &attr, arg);
956da14cebeSEric Cheng }
957da14cebeSEric Cheng 
958da14cebeSEric Cheng /*
959da14cebeSEric Cheng  * Wrapper of dladm_walk_flow(show_flow,...) to make it usable to
960da14cebeSEric Cheng  * dladm_walk_datalink_id(). Used for showing flow attributes for
961da14cebeSEric Cheng  * all flows on all links.
962da14cebeSEric Cheng  */
963da14cebeSEric Cheng static int
9644ac67f02SAnurag S. Maskey show_flows_onelink(dladm_handle_t dh, datalink_id_t linkid, void *arg)
965da14cebeSEric Cheng {
966da14cebeSEric Cheng 	show_flow_state_t *state = arg;
967da14cebeSEric Cheng 
9684ac67f02SAnurag S. Maskey 	(void) dladm_walk_flow(show_flow, dh, linkid, arg, state->fs_persist);
969da14cebeSEric Cheng 
970da14cebeSEric Cheng 	return (DLADM_WALK_CONTINUE);
971da14cebeSEric Cheng }
972da14cebeSEric Cheng 
973da14cebeSEric Cheng static void
974da14cebeSEric Cheng get_flow_stats(const char *flowname, pktsum_t *stats)
975da14cebeSEric Cheng {
976da14cebeSEric Cheng 	kstat_ctl_t	*kcp;
977da14cebeSEric Cheng 	kstat_t		*ksp;
978da14cebeSEric Cheng 
979da14cebeSEric Cheng 	bzero(stats, sizeof (*stats));
980da14cebeSEric Cheng 
981da14cebeSEric Cheng 	if ((kcp = kstat_open()) == NULL) {
982da14cebeSEric Cheng 		warn("kstat open operation failed");
983da14cebeSEric Cheng 		return;
984da14cebeSEric Cheng 	}
985da14cebeSEric Cheng 
986da14cebeSEric Cheng 	ksp = dladm_kstat_lookup(kcp, NULL, -1, flowname, "flow");
987da14cebeSEric Cheng 
988da14cebeSEric Cheng 	if (ksp != NULL)
989da14cebeSEric Cheng 		dladm_get_stats(kcp, ksp, stats);
990da14cebeSEric Cheng 
991da14cebeSEric Cheng 	(void) kstat_close(kcp);
992da14cebeSEric Cheng }
993da14cebeSEric Cheng 
9948002d411SSowmini Varadhan static boolean_t
9958002d411SSowmini Varadhan print_flow_stats_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
996ae6aa22aSVenugopal Iyer {
9978002d411SSowmini Varadhan 	flow_args_t	*fargs = of_arg->ofmt_cbarg;
998ae6aa22aSVenugopal Iyer 	pktsum_t	*diff_stats = fargs->flow_s_psum;
999ae6aa22aSVenugopal Iyer 
10008002d411SSowmini Varadhan 	switch (of_arg->ofmt_id) {
1001ae6aa22aSVenugopal Iyer 	case FLOW_S_FLOW:
10028002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%s", fargs->flow_s_flow);
1003ae6aa22aSVenugopal Iyer 		break;
1004ae6aa22aSVenugopal Iyer 	case FLOW_S_IPKTS:
10058002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%llu",
1006ae6aa22aSVenugopal Iyer 		    diff_stats->ipackets);
1007ae6aa22aSVenugopal Iyer 		break;
1008ae6aa22aSVenugopal Iyer 	case FLOW_S_RBYTES:
10098002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%llu",
1010ae6aa22aSVenugopal Iyer 		    diff_stats->rbytes);
1011ae6aa22aSVenugopal Iyer 		break;
1012ae6aa22aSVenugopal Iyer 	case FLOW_S_IERRORS:
10138002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%u",
1014ae6aa22aSVenugopal Iyer 		    diff_stats->ierrors);
1015ae6aa22aSVenugopal Iyer 		break;
1016ae6aa22aSVenugopal Iyer 	case FLOW_S_OPKTS:
10178002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%llu",
1018ae6aa22aSVenugopal Iyer 		    diff_stats->opackets);
1019ae6aa22aSVenugopal Iyer 		break;
1020ae6aa22aSVenugopal Iyer 	case FLOW_S_OBYTES:
10218002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%llu",
1022ae6aa22aSVenugopal Iyer 		    diff_stats->obytes);
1023ae6aa22aSVenugopal Iyer 		break;
1024ae6aa22aSVenugopal Iyer 	case FLOW_S_OERRORS:
10258002d411SSowmini Varadhan 		(void) snprintf(buf, bufsize, "%u",
1026ae6aa22aSVenugopal Iyer 		    diff_stats->oerrors);
1027ae6aa22aSVenugopal Iyer 		break;
1028ae6aa22aSVenugopal Iyer 	default:
1029ae6aa22aSVenugopal Iyer 		die("invalid input");
1030ae6aa22aSVenugopal Iyer 		break;
1031ae6aa22aSVenugopal Iyer 	}
10328002d411SSowmini Varadhan 	return (B_TRUE);
1033ae6aa22aSVenugopal Iyer }
10348002d411SSowmini Varadhan 
1035da14cebeSEric Cheng /* ARGSUSED */
1036da14cebeSEric Cheng static int
1037*c3affd82SMichael Lim show_flow_stats(dladm_handle_t handle, dladm_flow_attr_t *attr, void *arg)
1038da14cebeSEric Cheng {
1039da14cebeSEric Cheng 	show_flow_state_t	*state = (show_flow_state_t *)arg;
1040ae6aa22aSVenugopal Iyer 	char			*name = attr->fa_flowname;
1041da14cebeSEric Cheng 	pktsum_t		stats, diff_stats;
1042ae6aa22aSVenugopal Iyer 	flow_args_t		fargs;
1043da14cebeSEric Cheng 
1044da14cebeSEric Cheng 	if (state->fs_firstonly) {
1045da14cebeSEric Cheng 		if (state->fs_donefirst)
1046da14cebeSEric Cheng 			return (DLADM_WALK_TERMINATE);
1047da14cebeSEric Cheng 		state->fs_donefirst = B_TRUE;
1048da14cebeSEric Cheng 	} else {
1049da14cebeSEric Cheng 		bzero(&state->fs_prevstats, sizeof (state->fs_prevstats));
1050da14cebeSEric Cheng 	}
1051da14cebeSEric Cheng 
1052da14cebeSEric Cheng 	get_flow_stats(name, &stats);
1053da14cebeSEric Cheng 	dladm_stats_diff(&diff_stats, &stats, &state->fs_prevstats);
1054da14cebeSEric Cheng 
1055ae6aa22aSVenugopal Iyer 	fargs.flow_s_flow = name;
1056ae6aa22aSVenugopal Iyer 	fargs.flow_s_psum = &diff_stats;
10578002d411SSowmini Varadhan 	ofmt_print(state->fs_ofmt, (void *)&fargs);
1058da14cebeSEric Cheng 	state->fs_prevstats = stats;
1059da14cebeSEric Cheng 
1060da14cebeSEric Cheng 	return (DLADM_WALK_CONTINUE);
1061da14cebeSEric Cheng }
1062da14cebeSEric Cheng 
1063da14cebeSEric Cheng /*
1064da14cebeSEric Cheng  * Wrapper of dladm_walk_flow(show_flow,...) to make it usable for
1065da14cebeSEric Cheng  * dladm_walk_datalink_id(). Used for showing flow stats for
1066da14cebeSEric Cheng  * all flows on all links.
1067da14cebeSEric Cheng  */
1068da14cebeSEric Cheng static int
10694ac67f02SAnurag S. Maskey show_link_flow_stats(dladm_handle_t dh, datalink_id_t linkid, void * arg)
1070da14cebeSEric Cheng {
10714ac67f02SAnurag S. Maskey 	if (dladm_walk_flow(show_flow_stats, dh, linkid, arg, B_FALSE)
1072da14cebeSEric Cheng 	    == DLADM_STATUS_OK)
1073da14cebeSEric Cheng 		return (DLADM_WALK_CONTINUE);
1074da14cebeSEric Cheng 	else
1075da14cebeSEric Cheng 		return (DLADM_WALK_TERMINATE);
1076da14cebeSEric Cheng }
1077da14cebeSEric Cheng 
1078da14cebeSEric Cheng /* ARGSUSED */
1079da14cebeSEric Cheng static void
1080ae6aa22aSVenugopal Iyer flow_stats(const char *flow, datalink_id_t linkid,  uint_t interval,
1081ae6aa22aSVenugopal Iyer     char *fields_str, show_flow_state_t *state)
1082da14cebeSEric Cheng {
1083da14cebeSEric Cheng 	dladm_flow_attr_t	attr;
10848002d411SSowmini Varadhan 	ofmt_handle_t		ofmt;
10858002d411SSowmini Varadhan 	ofmt_status_t		oferr;
10868002d411SSowmini Varadhan 	uint_t			ofmtflags = 0;
1087ae6aa22aSVenugopal Iyer 
10888002d411SSowmini Varadhan 	oferr = ofmt_open(fields_str, flow_s_fields, ofmtflags, 0, &ofmt);
10898002d411SSowmini Varadhan 	flowadm_ofmt_check(oferr, state->fs_parsable, ofmt);
10908002d411SSowmini Varadhan 	state->fs_ofmt = ofmt;
1091da14cebeSEric Cheng 
10924ac67f02SAnurag S. Maskey 	if (flow != NULL &&
10934ac67f02SAnurag S. Maskey 	    dladm_flow_info(handle, flow, &attr) != DLADM_STATUS_OK)
1094da14cebeSEric Cheng 		die("invalid flow %s", flow);
1095da14cebeSEric Cheng 
1096da14cebeSEric Cheng 	/*
1097da14cebeSEric Cheng 	 * If an interval is specified, continuously show the stats
1098da14cebeSEric Cheng 	 * for only the first flow.
1099da14cebeSEric Cheng 	 */
1100ae6aa22aSVenugopal Iyer 	state->fs_firstonly = (interval != 0);
1101da14cebeSEric Cheng 
1102da14cebeSEric Cheng 	for (;;) {
1103ae6aa22aSVenugopal Iyer 		state->fs_donefirst = B_FALSE;
1104da14cebeSEric Cheng 
1105da14cebeSEric Cheng 		/* Show stats for named flow */
1106da14cebeSEric Cheng 		if (flow != NULL)  {
1107ae6aa22aSVenugopal Iyer 			state->fs_flow = flow;
1108*c3affd82SMichael Lim 			(void) show_flow_stats(handle, &attr, state);
1109da14cebeSEric Cheng 
1110da14cebeSEric Cheng 		/* Show all stats on a link */
1111da14cebeSEric Cheng 		} else if (linkid != DATALINK_INVALID_LINKID) {
11124ac67f02SAnurag S. Maskey 			(void) dladm_walk_flow(show_flow_stats, handle, linkid,
1113ae6aa22aSVenugopal Iyer 			    state, B_FALSE);
1114da14cebeSEric Cheng 
1115da14cebeSEric Cheng 		/* Show all stats by datalink */
1116da14cebeSEric Cheng 		} else {
1117da14cebeSEric Cheng 			(void) dladm_walk_datalink_id(show_link_flow_stats,
1118ae6aa22aSVenugopal Iyer 			    handle, state, DATALINK_CLASS_ALL,
11194ac67f02SAnurag S. Maskey 			    DATALINK_ANY_MEDIATYPE, DLADM_OPT_ACTIVE);
1120da14cebeSEric Cheng 		}
1121da14cebeSEric Cheng 
1122da14cebeSEric Cheng 		if (interval == 0)
1123da14cebeSEric Cheng 			break;
1124da14cebeSEric Cheng 
1125*c3affd82SMichael Lim 		(void) fflush(stdout);
1126da14cebeSEric Cheng 		(void) sleep(interval);
1127da14cebeSEric Cheng 	}
11288002d411SSowmini Varadhan 	ofmt_close(ofmt);
1129da14cebeSEric Cheng }
1130da14cebeSEric Cheng 
1131da14cebeSEric Cheng static void
1132da14cebeSEric Cheng do_show_flow(int argc, char *argv[])
1133da14cebeSEric Cheng {
1134da000602SGirish Moodalbail 	char			flowname[MAXFLOWNAMELEN];
1135da000602SGirish Moodalbail 	char			linkname[MAXLINKNAMELEN];
1136da14cebeSEric Cheng 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
1137da14cebeSEric Cheng 	int			option;
1138da14cebeSEric Cheng 	boolean_t		s_arg = B_FALSE;
1139da14cebeSEric Cheng 	boolean_t		S_arg = B_FALSE;
1140da14cebeSEric Cheng 	boolean_t		i_arg = B_FALSE;
1141da14cebeSEric Cheng 	boolean_t		l_arg = B_FALSE;
1142da14cebeSEric Cheng 	boolean_t		o_arg = B_FALSE;
1143da14cebeSEric Cheng 	uint32_t		interval = 0;
1144da14cebeSEric Cheng 	show_flow_state_t	state;
1145da14cebeSEric Cheng 	char			*fields_str = NULL;
11468002d411SSowmini Varadhan 	ofmt_handle_t		ofmt;
11478002d411SSowmini Varadhan 	ofmt_status_t		oferr;
11488002d411SSowmini Varadhan 	uint_t			ofmtflags = 0;
1149da14cebeSEric Cheng 
1150da14cebeSEric Cheng 	bzero(&state, sizeof (state));
1151da14cebeSEric Cheng 
1152da14cebeSEric Cheng 	opterr = 0;
1153da14cebeSEric Cheng 	while ((option = getopt_long(argc, argv, ":pPsSi:l:o:",
1154da14cebeSEric Cheng 	    longopts, NULL)) != -1) {
1155da14cebeSEric Cheng 		switch (option) {
1156da14cebeSEric Cheng 		case 'p':
11578002d411SSowmini Varadhan 			state.fs_parsable = B_TRUE;
11588002d411SSowmini Varadhan 			ofmtflags |= OFMT_PARSABLE;
1159da14cebeSEric Cheng 			break;
1160da14cebeSEric Cheng 		case 'P':
1161da14cebeSEric Cheng 			state.fs_persist = B_TRUE;
1162da14cebeSEric Cheng 			break;
1163da14cebeSEric Cheng 		case 's':
1164da14cebeSEric Cheng 			if (s_arg)
1165da14cebeSEric Cheng 				die_optdup(option);
1166da14cebeSEric Cheng 
1167da14cebeSEric Cheng 			s_arg = B_TRUE;
1168da14cebeSEric Cheng 			break;
1169da14cebeSEric Cheng 		case 'S':
1170da14cebeSEric Cheng 			if (S_arg)
1171da14cebeSEric Cheng 				die_optdup(option);
1172da14cebeSEric Cheng 
1173da14cebeSEric Cheng 			S_arg = B_TRUE;
1174da14cebeSEric Cheng 			break;
1175da14cebeSEric Cheng 		case 'o':
1176da14cebeSEric Cheng 			if (o_arg)
1177da14cebeSEric Cheng 				die_optdup(option);
1178da14cebeSEric Cheng 
1179da14cebeSEric Cheng 			o_arg = B_TRUE;
1180da14cebeSEric Cheng 			fields_str = optarg;
1181da14cebeSEric Cheng 			break;
1182da14cebeSEric Cheng 		case 'i':
1183da14cebeSEric Cheng 			if (i_arg)
1184da14cebeSEric Cheng 				die_optdup(option);
1185da14cebeSEric Cheng 
1186da14cebeSEric Cheng 			i_arg = B_TRUE;
1187da14cebeSEric Cheng 
118863a6526dSMichael Lim 			if (!dladm_str2interval(optarg, &interval))
118963a6526dSMichael Lim 				die("invalid interval value '%s'", optarg);
1190da14cebeSEric Cheng 			break;
1191da14cebeSEric Cheng 		case 'l':
1192da14cebeSEric Cheng 			if (strlcpy(linkname, optarg, MAXLINKNAMELEN)
1193da14cebeSEric Cheng 			    >= MAXLINKNAMELEN)
1194da14cebeSEric Cheng 				die("link name too long\n");
11954ac67f02SAnurag S. Maskey 			if (dladm_name2info(handle, linkname, &linkid, NULL,
1196da14cebeSEric Cheng 			    NULL, NULL) != DLADM_STATUS_OK)
1197da14cebeSEric Cheng 				die("invalid link '%s'", linkname);
1198da14cebeSEric Cheng 			l_arg = B_TRUE;
1199da14cebeSEric Cheng 			break;
1200da14cebeSEric Cheng 		default:
1201da14cebeSEric Cheng 			die_opterr(optopt, option);
1202da14cebeSEric Cheng 			break;
1203da14cebeSEric Cheng 		}
1204da14cebeSEric Cheng 	}
1205da14cebeSEric Cheng 	if (i_arg && !(s_arg || S_arg))
1206da14cebeSEric Cheng 		die("the -i option can be used only with -s or -S");
1207da14cebeSEric Cheng 
1208da14cebeSEric Cheng 	if (s_arg && S_arg)
1209da14cebeSEric Cheng 		die("the -s option cannot be used with -S");
1210da14cebeSEric Cheng 
1211da14cebeSEric Cheng 	/* get flow name (optional last argument */
1212da14cebeSEric Cheng 	if (optind == (argc-1)) {
1213da000602SGirish Moodalbail 		if (strlcpy(flowname, argv[optind], MAXFLOWNAMELEN)
1214da000602SGirish Moodalbail 		    >= MAXFLOWNAMELEN)
1215da14cebeSEric Cheng 			die("flow name too long");
1216da14cebeSEric Cheng 		state.fs_flow = flowname;
1217da14cebeSEric Cheng 	}
1218da14cebeSEric Cheng 
1219da14cebeSEric Cheng 	if (S_arg) {
12204ac67f02SAnurag S. Maskey 		dladm_continuous(handle, linkid, state.fs_flow, interval,
12214ac67f02SAnurag S. Maskey 		    FLOW_REPORT);
1222da14cebeSEric Cheng 		return;
1223da14cebeSEric Cheng 	}
1224da14cebeSEric Cheng 
1225ae6aa22aSVenugopal Iyer 	if (s_arg) {
1226ae6aa22aSVenugopal Iyer 		flow_stats(state.fs_flow, linkid, interval, fields_str, &state);
1227ae6aa22aSVenugopal Iyer 		return;
1228ae6aa22aSVenugopal Iyer 	}
1229da14cebeSEric Cheng 
12308002d411SSowmini Varadhan 	oferr = ofmt_open(fields_str, flow_fields, ofmtflags, 0, &ofmt);
12318002d411SSowmini Varadhan 	flowadm_ofmt_check(oferr, state.fs_parsable, ofmt);
12328002d411SSowmini Varadhan 	state.fs_ofmt = ofmt;
1233da14cebeSEric Cheng 
1234da14cebeSEric Cheng 	/* Show attributes of one flow */
1235da14cebeSEric Cheng 	if (state.fs_flow != NULL) {
1236da14cebeSEric Cheng 		show_one_flow(&state, state.fs_flow);
1237da14cebeSEric Cheng 
1238da14cebeSEric Cheng 	/* Show attributes of flows on one link */
1239da14cebeSEric Cheng 	} else if (l_arg) {
12404ac67f02SAnurag S. Maskey 		(void) show_flows_onelink(handle, linkid, &state);
1241da14cebeSEric Cheng 
1242da14cebeSEric Cheng 	/* Show attributes of all flows on all links */
1243da14cebeSEric Cheng 	} else {
12444ac67f02SAnurag S. Maskey 		(void) dladm_walk_datalink_id(show_flows_onelink, handle,
12454ac67f02SAnurag S. Maskey 		    &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
1246da14cebeSEric Cheng 		    DLADM_OPT_ACTIVE);
1247da14cebeSEric Cheng 	}
12488002d411SSowmini Varadhan 	ofmt_close(ofmt);
1249da14cebeSEric Cheng }
1250da14cebeSEric Cheng 
1251da14cebeSEric Cheng static dladm_status_t
1252da14cebeSEric Cheng set_flowprop_persist(const char *flow, const char *prop_name, char **prop_val,
1253da14cebeSEric Cheng     uint_t val_cnt, boolean_t reset)
1254da14cebeSEric Cheng {
1255da14cebeSEric Cheng 	dladm_status_t	status;
1256da14cebeSEric Cheng 	char		*errprop;
1257da14cebeSEric Cheng 
12584ac67f02SAnurag S. Maskey 	status = dladm_set_flowprop(handle, flow, prop_name, prop_val, val_cnt,
1259da14cebeSEric Cheng 	    DLADM_OPT_PERSIST, &errprop);
1260da14cebeSEric Cheng 
1261da14cebeSEric Cheng 	if (status != DLADM_STATUS_OK) {
1262da14cebeSEric Cheng 		warn_dlerr(status, "cannot persistently %s flow "
1263da14cebeSEric Cheng 		    "property '%s' on '%s'", reset? "reset": "set",
1264da14cebeSEric Cheng 		    errprop, flow);
1265da14cebeSEric Cheng 	}
1266da14cebeSEric Cheng 	return (status);
1267da14cebeSEric Cheng }
1268da14cebeSEric Cheng 
1269da14cebeSEric Cheng static void
1270da14cebeSEric Cheng set_flowprop(int argc, char **argv, boolean_t reset)
1271da14cebeSEric Cheng {
1272da14cebeSEric Cheng 	int			i, option;
1273da14cebeSEric Cheng 	char			errmsg[DLADM_STRSIZE];
1274da14cebeSEric Cheng 	const char		*flow = NULL;
127563a6526dSMichael Lim 	char			propstr[DLADM_STRSIZE];
1276da14cebeSEric Cheng 	dladm_arg_list_t	*proplist = NULL;
1277da14cebeSEric Cheng 	boolean_t		temp = B_FALSE;
1278da14cebeSEric Cheng 	dladm_status_t		status = DLADM_STATUS_OK;
1279da14cebeSEric Cheng 
1280da14cebeSEric Cheng 	opterr = 0;
128163a6526dSMichael Lim 	bzero(propstr, DLADM_STRSIZE);
128263a6526dSMichael Lim 
1283da14cebeSEric Cheng 	while ((option = getopt_long(argc, argv, ":p:R:t",
1284da14cebeSEric Cheng 	    prop_longopts, NULL)) != -1) {
1285da14cebeSEric Cheng 		switch (option) {
1286da14cebeSEric Cheng 		case 'p':
128763a6526dSMichael Lim 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
128863a6526dSMichael Lim 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
128963a6526dSMichael Lim 			    DLADM_STRSIZE)
129063a6526dSMichael Lim 				die("property list too long '%s'", propstr);
1291da14cebeSEric Cheng 			break;
1292da14cebeSEric Cheng 		case 't':
1293da14cebeSEric Cheng 			temp = B_TRUE;
1294da14cebeSEric Cheng 			break;
1295da14cebeSEric Cheng 		case 'R':
1296da14cebeSEric Cheng 			status = dladm_set_rootdir(optarg);
1297da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK) {
1298da14cebeSEric Cheng 				die_dlerr(status, "invalid directory "
1299da14cebeSEric Cheng 				    "specified");
1300da14cebeSEric Cheng 			}
1301da14cebeSEric Cheng 			break;
1302da14cebeSEric Cheng 		default:
1303da14cebeSEric Cheng 			die_opterr(optopt, option);
1304da14cebeSEric Cheng 			break;
1305da14cebeSEric Cheng 		}
1306da14cebeSEric Cheng 	}
1307da14cebeSEric Cheng 
1308da14cebeSEric Cheng 	if (optind == (argc - 1)) {
1309da000602SGirish Moodalbail 		if (strlen(argv[optind]) >= MAXFLOWNAMELEN)
1310da14cebeSEric Cheng 			die("flow name too long");
1311da14cebeSEric Cheng 		flow = argv[optind];
1312da14cebeSEric Cheng 	} else if (optind != argc) {
1313da14cebeSEric Cheng 		usage();
1314da14cebeSEric Cheng 	}
1315da14cebeSEric Cheng 	if (flow == NULL)
1316da14cebeSEric Cheng 		die("flow name must be specified");
1317da14cebeSEric Cheng 
131863a6526dSMichael Lim 	if (dladm_parse_flow_props(propstr, &proplist, reset)
131963a6526dSMichael Lim 	    != DLADM_STATUS_OK)
132063a6526dSMichael Lim 		die("invalid flow property specified");
132163a6526dSMichael Lim 
1322da14cebeSEric Cheng 	if (proplist == NULL) {
1323da14cebeSEric Cheng 		char *errprop;
1324da14cebeSEric Cheng 
1325da14cebeSEric Cheng 		if (!reset)
1326da14cebeSEric Cheng 			die("flow property must be specified");
1327da14cebeSEric Cheng 
13284ac67f02SAnurag S. Maskey 		status = dladm_set_flowprop(handle, flow, NULL, NULL, 0,
1329da14cebeSEric Cheng 		    DLADM_OPT_ACTIVE, &errprop);
1330da14cebeSEric Cheng 		if (status != DLADM_STATUS_OK) {
1331da14cebeSEric Cheng 			warn_dlerr(status, "cannot reset flow property '%s' "
1332da14cebeSEric Cheng 			    "on '%s'", errprop, flow);
1333da14cebeSEric Cheng 		}
1334da14cebeSEric Cheng 		if (!temp) {
1335da14cebeSEric Cheng 			dladm_status_t	s;
1336da14cebeSEric Cheng 
1337da14cebeSEric Cheng 			s = set_flowprop_persist(flow, NULL, NULL, 0, reset);
1338da14cebeSEric Cheng 			if (s != DLADM_STATUS_OK)
1339da14cebeSEric Cheng 				status = s;
1340da14cebeSEric Cheng 		}
1341da14cebeSEric Cheng 		goto done;
1342da14cebeSEric Cheng 	}
1343da14cebeSEric Cheng 
1344da14cebeSEric Cheng 	for (i = 0; i < proplist->al_count; i++) {
1345da14cebeSEric Cheng 		dladm_arg_info_t	*aip = &proplist->al_info[i];
1346da14cebeSEric Cheng 		char		**val;
1347da14cebeSEric Cheng 		uint_t		count;
1348da14cebeSEric Cheng 		dladm_status_t	s;
1349da14cebeSEric Cheng 
1350da14cebeSEric Cheng 		if (reset) {
1351da14cebeSEric Cheng 			val = NULL;
1352da14cebeSEric Cheng 			count = 0;
1353da14cebeSEric Cheng 		} else {
1354da14cebeSEric Cheng 			val = aip->ai_val;
1355da14cebeSEric Cheng 			count = aip->ai_count;
1356da14cebeSEric Cheng 			if (count == 0) {
1357da14cebeSEric Cheng 				warn("no value specified for '%s'",
1358da14cebeSEric Cheng 				    aip->ai_name);
1359da14cebeSEric Cheng 				status = DLADM_STATUS_BADARG;
1360da14cebeSEric Cheng 				continue;
1361da14cebeSEric Cheng 			}
1362da14cebeSEric Cheng 		}
13634ac67f02SAnurag S. Maskey 		s = dladm_set_flowprop(handle, flow, aip->ai_name, val, count,
1364da14cebeSEric Cheng 		    DLADM_OPT_ACTIVE, NULL);
1365da14cebeSEric Cheng 		if (s == DLADM_STATUS_OK) {
1366da14cebeSEric Cheng 			if (!temp) {
1367da14cebeSEric Cheng 				s = set_flowprop_persist(flow,
1368da14cebeSEric Cheng 				    aip->ai_name, val, count, reset);
1369da14cebeSEric Cheng 				if (s != DLADM_STATUS_OK)
1370da14cebeSEric Cheng 					status = s;
1371da14cebeSEric Cheng 			}
1372da14cebeSEric Cheng 			continue;
1373da14cebeSEric Cheng 		}
1374da14cebeSEric Cheng 		status = s;
1375da14cebeSEric Cheng 		switch (s) {
1376da14cebeSEric Cheng 		case DLADM_STATUS_NOTFOUND:
1377da14cebeSEric Cheng 			warn("invalid flow property '%s'", aip->ai_name);
1378da14cebeSEric Cheng 			break;
1379da14cebeSEric Cheng 		case DLADM_STATUS_BADVAL: {
1380da14cebeSEric Cheng 			int		j;
1381da14cebeSEric Cheng 			char		*ptr, *lim;
1382da14cebeSEric Cheng 			char		**propvals = NULL;
1383da14cebeSEric Cheng 			uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
1384da14cebeSEric Cheng 
1385da14cebeSEric Cheng 			ptr = malloc((sizeof (char *) +
1386da14cebeSEric Cheng 			    DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT +
1387da14cebeSEric Cheng 			    MAX_PROP_LINE);
1388da14cebeSEric Cheng 
1389da14cebeSEric Cheng 			if (ptr == NULL)
1390da14cebeSEric Cheng 				die("insufficient memory");
1391da14cebeSEric Cheng 			propvals = (char **)(void *)ptr;
1392da14cebeSEric Cheng 
1393da14cebeSEric Cheng 			for (j = 0; j < DLADM_MAX_PROP_VALCNT; j++) {
1394da14cebeSEric Cheng 				propvals[j] = ptr + sizeof (char *) *
1395da14cebeSEric Cheng 				    DLADM_MAX_PROP_VALCNT +
1396da14cebeSEric Cheng 				    j * DLADM_PROP_VAL_MAX;
1397da14cebeSEric Cheng 			}
13984ac67f02SAnurag S. Maskey 			s = dladm_get_flowprop(handle, flow,
13994ac67f02SAnurag S. Maskey 			    DLADM_PROP_VAL_MODIFIABLE, aip->ai_name, propvals,
14004ac67f02SAnurag S. Maskey 			    &valcnt);
1401da14cebeSEric Cheng 
1402da14cebeSEric Cheng 			ptr = errmsg;
1403da14cebeSEric Cheng 			lim = ptr + DLADM_STRSIZE;
1404da14cebeSEric Cheng 			*ptr = '\0';
1405da14cebeSEric Cheng 			for (j = 0; j < valcnt && s == DLADM_STATUS_OK; j++) {
1406da14cebeSEric Cheng 				ptr += snprintf(ptr, lim - ptr, "%s,",
1407da14cebeSEric Cheng 				    propvals[j]);
1408da14cebeSEric Cheng 				if (ptr >= lim)
1409da14cebeSEric Cheng 					break;
1410da14cebeSEric Cheng 			}
1411da14cebeSEric Cheng 			if (ptr > errmsg) {
1412da14cebeSEric Cheng 				*(ptr - 1) = '\0';
1413da14cebeSEric Cheng 				warn("flow property '%s' must be one of: %s",
1414da14cebeSEric Cheng 				    aip->ai_name, errmsg);
1415da14cebeSEric Cheng 			} else
1416da14cebeSEric Cheng 				warn("%s is an invalid value for "
1417da14cebeSEric Cheng 				    "flow property %s", *val, aip->ai_name);
1418da14cebeSEric Cheng 			free(propvals);
1419da14cebeSEric Cheng 			break;
1420da14cebeSEric Cheng 		}
1421da14cebeSEric Cheng 		default:
1422da14cebeSEric Cheng 			if (reset) {
1423da14cebeSEric Cheng 				warn_dlerr(status, "cannot reset flow property "
1424da14cebeSEric Cheng 				    "'%s' on '%s'", aip->ai_name, flow);
1425da14cebeSEric Cheng 			} else {
1426da14cebeSEric Cheng 				warn_dlerr(status, "cannot set flow property "
1427da14cebeSEric Cheng 				    "'%s' on '%s'", aip->ai_name, flow);
1428da14cebeSEric Cheng 			}
1429da14cebeSEric Cheng 			break;
1430da14cebeSEric Cheng 		}
1431da14cebeSEric Cheng 	}
1432da14cebeSEric Cheng done:
1433da14cebeSEric Cheng 	dladm_free_props(proplist);
14344ac67f02SAnurag S. Maskey 	if (status != DLADM_STATUS_OK) {
14354ac67f02SAnurag S. Maskey 		dladm_close(handle);
1436ad091ee1SMichael Lim 		exit(EXIT_FAILURE);
1437da14cebeSEric Cheng 	}
14384ac67f02SAnurag S. Maskey }
1439da14cebeSEric Cheng 
1440da14cebeSEric Cheng static void
1441da14cebeSEric Cheng do_set_flowprop(int argc, char **argv)
1442da14cebeSEric Cheng {
1443da14cebeSEric Cheng 	set_flowprop(argc, argv, B_FALSE);
1444da14cebeSEric Cheng }
1445da14cebeSEric Cheng 
1446da14cebeSEric Cheng static void
1447da14cebeSEric Cheng do_reset_flowprop(int argc, char **argv)
1448da14cebeSEric Cheng {
1449da14cebeSEric Cheng 	set_flowprop(argc, argv, B_TRUE);
1450da14cebeSEric Cheng }
1451da14cebeSEric Cheng 
1452da14cebeSEric Cheng static void
1453da14cebeSEric Cheng warn(const char *format, ...)
1454da14cebeSEric Cheng {
1455da14cebeSEric Cheng 	va_list alist;
1456da14cebeSEric Cheng 
1457da14cebeSEric Cheng 	format = gettext(format);
1458da14cebeSEric Cheng 	(void) fprintf(stderr, "%s: warning: ", progname);
1459da14cebeSEric Cheng 
1460da14cebeSEric Cheng 	va_start(alist, format);
1461da14cebeSEric Cheng 	(void) vfprintf(stderr, format, alist);
1462da14cebeSEric Cheng 	va_end(alist);
1463da14cebeSEric Cheng 
1464da14cebeSEric Cheng 	(void) putchar('\n');
1465da14cebeSEric Cheng }
1466da14cebeSEric Cheng 
1467da14cebeSEric Cheng /* PRINTFLIKE2 */
1468da14cebeSEric Cheng static void
1469da14cebeSEric Cheng warn_dlerr(dladm_status_t err, const char *format, ...)
1470da14cebeSEric Cheng {
1471da14cebeSEric Cheng 	va_list alist;
1472da14cebeSEric Cheng 	char    errmsg[DLADM_STRSIZE];
1473da14cebeSEric Cheng 
1474da14cebeSEric Cheng 	format = gettext(format);
1475da14cebeSEric Cheng 	(void) fprintf(stderr, gettext("%s: warning: "), progname);
1476da14cebeSEric Cheng 
1477da14cebeSEric Cheng 	va_start(alist, format);
1478da14cebeSEric Cheng 	(void) vfprintf(stderr, format, alist);
1479da14cebeSEric Cheng 	va_end(alist);
1480da14cebeSEric Cheng 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
1481da14cebeSEric Cheng }
1482da14cebeSEric Cheng 
1483da14cebeSEric Cheng /* PRINTFLIKE1 */
1484da14cebeSEric Cheng static void
1485da14cebeSEric Cheng die(const char *format, ...)
1486da14cebeSEric Cheng {
1487da14cebeSEric Cheng 	va_list alist;
1488da14cebeSEric Cheng 
1489da14cebeSEric Cheng 	format = gettext(format);
1490da14cebeSEric Cheng 	(void) fprintf(stderr, "%s: ", progname);
1491da14cebeSEric Cheng 
1492da14cebeSEric Cheng 	va_start(alist, format);
1493da14cebeSEric Cheng 	(void) vfprintf(stderr, format, alist);
1494da14cebeSEric Cheng 	va_end(alist);
1495da14cebeSEric Cheng 
1496da14cebeSEric Cheng 	(void) putchar('\n');
14974ac67f02SAnurag S. Maskey 
14984ac67f02SAnurag S. Maskey 	/* close dladm handle if it was opened */
14994ac67f02SAnurag S. Maskey 	if (handle != NULL)
15004ac67f02SAnurag S. Maskey 		dladm_close(handle);
15014ac67f02SAnurag S. Maskey 
1502da14cebeSEric Cheng 	exit(EXIT_FAILURE);
1503da14cebeSEric Cheng }
1504da14cebeSEric Cheng 
1505da14cebeSEric Cheng static void
1506da14cebeSEric Cheng die_optdup(int opt)
1507da14cebeSEric Cheng {
1508da14cebeSEric Cheng 	die("the option -%c cannot be specified more than once", opt);
1509da14cebeSEric Cheng }
1510da14cebeSEric Cheng 
1511da14cebeSEric Cheng static void
1512da14cebeSEric Cheng die_opterr(int opt, int opterr)
1513da14cebeSEric Cheng {
1514da14cebeSEric Cheng 	switch (opterr) {
1515da14cebeSEric Cheng 	case ':':
1516da14cebeSEric Cheng 		die("option '-%c' requires a value", opt);
1517da14cebeSEric Cheng 		break;
1518da14cebeSEric Cheng 	case '?':
1519da14cebeSEric Cheng 	default:
1520da14cebeSEric Cheng 		die("unrecognized option '-%c'", opt);
1521da14cebeSEric Cheng 		break;
1522da14cebeSEric Cheng 	}
1523da14cebeSEric Cheng }
1524da14cebeSEric Cheng 
1525da14cebeSEric Cheng /* PRINTFLIKE2 */
1526da14cebeSEric Cheng static void
1527da14cebeSEric Cheng die_dlerr(dladm_status_t err, const char *format, ...)
1528da14cebeSEric Cheng {
1529da14cebeSEric Cheng 	va_list alist;
1530da14cebeSEric Cheng 	char	errmsg[DLADM_STRSIZE];
1531da14cebeSEric Cheng 
1532da14cebeSEric Cheng 	format = gettext(format);
1533da14cebeSEric Cheng 	(void) fprintf(stderr, "%s: ", progname);
1534da14cebeSEric Cheng 
1535da14cebeSEric Cheng 	va_start(alist, format);
1536da14cebeSEric Cheng 	(void) vfprintf(stderr, format, alist);
1537da14cebeSEric Cheng 	va_end(alist);
1538da14cebeSEric Cheng 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
1539da14cebeSEric Cheng 
15404ac67f02SAnurag S. Maskey 	/* close dladm handle if it was opened */
15414ac67f02SAnurag S. Maskey 	if (handle != NULL)
15424ac67f02SAnurag S. Maskey 		dladm_close(handle);
15434ac67f02SAnurag S. Maskey 
1544da14cebeSEric Cheng 	exit(EXIT_FAILURE);
1545da14cebeSEric Cheng }
1546da14cebeSEric Cheng 
1547da14cebeSEric Cheng static void
1548da14cebeSEric Cheng print_flowprop(const char *flowname, show_flowprop_state_t *statep,
1549da14cebeSEric Cheng     const char *propname, dladm_prop_type_t type,
1550da14cebeSEric Cheng     const char *format, char **pptr)
1551da14cebeSEric Cheng {
1552da14cebeSEric Cheng 	int		i;
1553da14cebeSEric Cheng 	char		*ptr, *lim;
1554da14cebeSEric Cheng 	char		buf[DLADM_STRSIZE];
1555da14cebeSEric Cheng 	char		*unknown = "--", *notsup = "";
1556da14cebeSEric Cheng 	char		**propvals = statep->fs_propvals;
1557da14cebeSEric Cheng 	uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
1558da14cebeSEric Cheng 	dladm_status_t	status;
1559da14cebeSEric Cheng 
15604ac67f02SAnurag S. Maskey 	status = dladm_get_flowprop(handle, flowname, type, propname, propvals,
1561da14cebeSEric Cheng 	    &valcnt);
1562da14cebeSEric Cheng 	if (status != DLADM_STATUS_OK) {
1563da14cebeSEric Cheng 		if (status == DLADM_STATUS_TEMPONLY) {
1564da14cebeSEric Cheng 			if (type == DLADM_PROP_VAL_MODIFIABLE &&
1565da14cebeSEric Cheng 			    statep->fs_persist) {
1566da14cebeSEric Cheng 				valcnt = 1;
1567da14cebeSEric Cheng 				propvals = &unknown;
1568da14cebeSEric Cheng 			} else {
1569da14cebeSEric Cheng 				statep->fs_status = status;
1570da14cebeSEric Cheng 				statep->fs_retstatus = status;
1571da14cebeSEric Cheng 				return;
1572da14cebeSEric Cheng 			}
1573da14cebeSEric Cheng 		} else if (status == DLADM_STATUS_NOTSUP ||
1574da14cebeSEric Cheng 		    statep->fs_persist) {
1575da14cebeSEric Cheng 			valcnt = 1;
1576da14cebeSEric Cheng 			if (type == DLADM_PROP_VAL_CURRENT)
1577da14cebeSEric Cheng 				propvals = &unknown;
1578da14cebeSEric Cheng 			else
1579da14cebeSEric Cheng 				propvals = &notsup;
1580da14cebeSEric Cheng 		} else {
1581da14cebeSEric Cheng 			if ((statep->fs_proplist != NULL) &&
1582da14cebeSEric Cheng 			    statep->fs_status == DLADM_STATUS_OK) {
1583da14cebeSEric Cheng 				warn("invalid flow property '%s'", propname);
1584da14cebeSEric Cheng 			}
1585da14cebeSEric Cheng 			statep->fs_status = status;
1586da14cebeSEric Cheng 			statep->fs_retstatus = status;
1587da14cebeSEric Cheng 			return;
1588da14cebeSEric Cheng 		}
1589da14cebeSEric Cheng 	}
1590da14cebeSEric Cheng 
1591da14cebeSEric Cheng 	statep->fs_status = DLADM_STATUS_OK;
1592da14cebeSEric Cheng 
1593da14cebeSEric Cheng 	ptr = buf;
1594da14cebeSEric Cheng 	lim = buf + DLADM_STRSIZE;
1595da14cebeSEric Cheng 	for (i = 0; i < valcnt; i++) {
15968002d411SSowmini Varadhan 		if (propvals[i][0] == '\0' && !statep->fs_parsable)
15978002d411SSowmini Varadhan 			ptr += snprintf(ptr, lim - ptr, "--,");
1598da14cebeSEric Cheng 		else
1599da14cebeSEric Cheng 			ptr += snprintf(ptr, lim - ptr, "%s,", propvals[i]);
1600da14cebeSEric Cheng 		if (ptr >= lim)
1601da14cebeSEric Cheng 			break;
1602da14cebeSEric Cheng 	}
1603da14cebeSEric Cheng 	if (valcnt > 0)
1604da14cebeSEric Cheng 		buf[strlen(buf) - 1] = '\0';
1605da14cebeSEric Cheng 
1606da14cebeSEric Cheng 	lim = statep->fs_line + MAX_PROP_LINE;
16078002d411SSowmini Varadhan 	if (statep->fs_parsable) {
1608da14cebeSEric Cheng 		*pptr += snprintf(*pptr, lim - *pptr,
1609da14cebeSEric Cheng 		    "%s", buf);
1610da14cebeSEric Cheng 	} else {
1611da14cebeSEric Cheng 		*pptr += snprintf(*pptr, lim - *pptr, format, buf);
1612da14cebeSEric Cheng 	}
1613da14cebeSEric Cheng }
1614da14cebeSEric Cheng 
16158002d411SSowmini Varadhan static boolean_t
16168002d411SSowmini Varadhan print_flowprop_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
1617da14cebeSEric Cheng {
16188002d411SSowmini Varadhan 	flowprop_args_t		*arg = of_arg->ofmt_cbarg;
1619da14cebeSEric Cheng 	char 			*propname = arg->fs_propname;
1620da14cebeSEric Cheng 	show_flowprop_state_t	*statep = arg->fs_state;
1621da14cebeSEric Cheng 	char			*ptr = statep->fs_line;
1622da14cebeSEric Cheng 	char			*lim = ptr + MAX_PROP_LINE;
1623da14cebeSEric Cheng 	char			*flowname = arg->fs_flowname;
1624da14cebeSEric Cheng 
16258002d411SSowmini Varadhan 	switch (of_arg->ofmt_id) {
1626da14cebeSEric Cheng 	case FLOWPROP_FLOW:
1627da14cebeSEric Cheng 		(void) snprintf(ptr, lim - ptr, "%s", statep->fs_flow);
1628da14cebeSEric Cheng 		break;
1629da14cebeSEric Cheng 	case FLOWPROP_PROPERTY:
1630da14cebeSEric Cheng 		(void) snprintf(ptr, lim - ptr, "%s", propname);
1631da14cebeSEric Cheng 		break;
1632da14cebeSEric Cheng 	case FLOWPROP_VALUE:
1633da14cebeSEric Cheng 		print_flowprop(flowname, statep, propname,
1634da14cebeSEric Cheng 		    statep->fs_persist ? DLADM_PROP_VAL_PERSISTENT :
1635da14cebeSEric Cheng 		    DLADM_PROP_VAL_CURRENT, "%s", &ptr);
1636da14cebeSEric Cheng 		/*
1637da14cebeSEric Cheng 		 * If we failed to query the flow property, for example, query
1638da14cebeSEric Cheng 		 * the persistent value of a non-persistable flow property,
1639da14cebeSEric Cheng 		 * simply skip the output.
1640da14cebeSEric Cheng 		 */
1641da14cebeSEric Cheng 		if (statep->fs_status != DLADM_STATUS_OK)
1642da14cebeSEric Cheng 			goto skip;
1643da14cebeSEric Cheng 		ptr = statep->fs_line;
1644da14cebeSEric Cheng 		break;
1645da14cebeSEric Cheng 	case FLOWPROP_DEFAULT:
1646da14cebeSEric Cheng 		print_flowprop(flowname, statep, propname,
1647da14cebeSEric Cheng 		    DLADM_PROP_VAL_DEFAULT, "%s", &ptr);
1648da14cebeSEric Cheng 		if (statep->fs_status != DLADM_STATUS_OK)
1649da14cebeSEric Cheng 			goto skip;
1650da14cebeSEric Cheng 		ptr = statep->fs_line;
1651da14cebeSEric Cheng 		break;
1652da14cebeSEric Cheng 	case FLOWPROP_POSSIBLE:
1653da14cebeSEric Cheng 		print_flowprop(flowname, statep, propname,
1654da14cebeSEric Cheng 		    DLADM_PROP_VAL_MODIFIABLE, "%s ", &ptr);
1655da14cebeSEric Cheng 		if (statep->fs_status != DLADM_STATUS_OK)
1656da14cebeSEric Cheng 			goto skip;
1657da14cebeSEric Cheng 		ptr = statep->fs_line;
1658da14cebeSEric Cheng 		break;
1659da14cebeSEric Cheng 	default:
1660da14cebeSEric Cheng 		die("invalid input");
1661da14cebeSEric Cheng 		break;
1662da14cebeSEric Cheng 	}
16638002d411SSowmini Varadhan 	(void) strlcpy(buf, ptr, bufsize);
16648002d411SSowmini Varadhan 	return (B_TRUE);
1665da14cebeSEric Cheng skip:
16668002d411SSowmini Varadhan 	buf[0] = '\0';
16678002d411SSowmini Varadhan 	return ((statep->fs_status == DLADM_STATUS_OK) ?
16688002d411SSowmini Varadhan 	    B_TRUE : B_FALSE);
1669da14cebeSEric Cheng }
1670da14cebeSEric Cheng 
1671da14cebeSEric Cheng static int
1672da14cebeSEric Cheng show_one_flowprop(void *arg, const char *propname)
1673da14cebeSEric Cheng {
1674da14cebeSEric Cheng 	show_flowprop_state_t	*statep = arg;
1675da14cebeSEric Cheng 	flowprop_args_t		fs_arg;
1676da14cebeSEric Cheng 
1677da14cebeSEric Cheng 	bzero(&fs_arg, sizeof (fs_arg));
1678da14cebeSEric Cheng 	fs_arg.fs_state = statep;
1679da14cebeSEric Cheng 	fs_arg.fs_propname = (char *)propname;
1680da14cebeSEric Cheng 	fs_arg.fs_flowname = (char *)statep->fs_flow;
1681da14cebeSEric Cheng 
16828002d411SSowmini Varadhan 	ofmt_print(statep->fs_ofmt, (void *)&fs_arg);
1683da14cebeSEric Cheng 
1684da14cebeSEric Cheng 	return (DLADM_WALK_CONTINUE);
1685da14cebeSEric Cheng }
1686da14cebeSEric Cheng 
1687*c3affd82SMichael Lim /*ARGSUSED*/
1688da14cebeSEric Cheng /* Walker function called by dladm_walk_flow to display flow properties */
1689da14cebeSEric Cheng static int
1690*c3affd82SMichael Lim show_flowprop(dladm_handle_t handle, dladm_flow_attr_t *attr, void *arg)
1691da14cebeSEric Cheng {
1692da14cebeSEric Cheng 	show_flowprop_one_flow(arg, attr->fa_flowname);
1693da14cebeSEric Cheng 	return (DLADM_WALK_CONTINUE);
1694da14cebeSEric Cheng }
1695da14cebeSEric Cheng 
1696da14cebeSEric Cheng /*
1697da14cebeSEric Cheng  * Wrapper of dladm_walk_flow(show_walk_fn,...) to make it
1698da14cebeSEric Cheng  * usable to dladm_walk_datalink_id()
1699da14cebeSEric Cheng  */
1700da14cebeSEric Cheng static int
17014ac67f02SAnurag S. Maskey show_flowprop_onelink(dladm_handle_t dh, datalink_id_t linkid, void *arg)
1702da14cebeSEric Cheng {
1703da14cebeSEric Cheng 	char	name[MAXLINKNAMELEN];
1704da14cebeSEric Cheng 
17054ac67f02SAnurag S. Maskey 	if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, name,
17064ac67f02SAnurag S. Maskey 	    sizeof (name)) != DLADM_STATUS_OK)
1707da14cebeSEric Cheng 		return (DLADM_WALK_TERMINATE);
1708da14cebeSEric Cheng 
17094ac67f02SAnurag S. Maskey 	(void) dladm_walk_flow(show_flowprop, dh, linkid, arg, B_FALSE);
1710da14cebeSEric Cheng 
1711da14cebeSEric Cheng 	return (DLADM_WALK_CONTINUE);
1712da14cebeSEric Cheng }
1713da14cebeSEric Cheng 
1714da14cebeSEric Cheng static void
1715da14cebeSEric Cheng do_show_flowprop(int argc, char **argv)
1716da14cebeSEric Cheng {
1717da14cebeSEric Cheng 	int			option;
1718da14cebeSEric Cheng 	dladm_arg_list_t	*proplist = NULL;
1719da14cebeSEric Cheng 	show_flowprop_state_t	state;
1720da14cebeSEric Cheng 	char			*fields_str = NULL;
17218002d411SSowmini Varadhan 	ofmt_handle_t		ofmt;
17228002d411SSowmini Varadhan 	ofmt_status_t		oferr;
17238002d411SSowmini Varadhan 	uint_t			ofmtflags = 0;
1724da14cebeSEric Cheng 
1725da14cebeSEric Cheng 	opterr = 0;
1726da14cebeSEric Cheng 	state.fs_propvals = NULL;
1727da14cebeSEric Cheng 	state.fs_line = NULL;
17288002d411SSowmini Varadhan 	state.fs_parsable = B_FALSE;
1729da14cebeSEric Cheng 	state.fs_persist = B_FALSE;
1730da14cebeSEric Cheng 	state.fs_header = B_TRUE;
1731da14cebeSEric Cheng 	state.fs_retstatus = DLADM_STATUS_OK;
1732da14cebeSEric Cheng 	state.fs_linkid = DATALINK_INVALID_LINKID;
1733da14cebeSEric Cheng 	state.fs_flow = NULL;
1734da14cebeSEric Cheng 
1735da14cebeSEric Cheng 	while ((option = getopt_long(argc, argv, ":p:cPl:o:",
1736da14cebeSEric Cheng 	    prop_longopts, NULL)) != -1) {
1737da14cebeSEric Cheng 		switch (option) {
1738da14cebeSEric Cheng 		case 'p':
1739da14cebeSEric Cheng 			if (dladm_parse_flow_props(optarg, &proplist, B_TRUE)
1740da14cebeSEric Cheng 			    != DLADM_STATUS_OK)
1741da14cebeSEric Cheng 				die("invalid flow properties specified");
1742da14cebeSEric Cheng 			break;
1743da14cebeSEric Cheng 		case 'c':
17448002d411SSowmini Varadhan 			state.fs_parsable = B_TRUE;
17458002d411SSowmini Varadhan 			ofmtflags |= OFMT_PARSABLE;
1746da14cebeSEric Cheng 			break;
1747da14cebeSEric Cheng 		case 'P':
1748da14cebeSEric Cheng 			state.fs_persist = B_TRUE;
1749da14cebeSEric Cheng 			break;
1750da14cebeSEric Cheng 		case 'l':
17514ac67f02SAnurag S. Maskey 			if (dladm_name2info(handle, optarg, &state.fs_linkid,
1752da14cebeSEric Cheng 			    NULL, NULL, NULL) != DLADM_STATUS_OK)
1753da14cebeSEric Cheng 				die("invalid link '%s'", optarg);
1754da14cebeSEric Cheng 			break;
1755da14cebeSEric Cheng 		case 'o':
1756da14cebeSEric Cheng 			fields_str = optarg;
1757da14cebeSEric Cheng 			break;
1758da14cebeSEric Cheng 		default:
1759da14cebeSEric Cheng 			die_opterr(optopt, option);
1760da14cebeSEric Cheng 			break;
1761da14cebeSEric Cheng 		}
1762da14cebeSEric Cheng 	}
1763da14cebeSEric Cheng 
1764da14cebeSEric Cheng 	if (optind == (argc - 1)) {
1765da000602SGirish Moodalbail 		if (strlen(argv[optind]) >= MAXFLOWNAMELEN)
1766da14cebeSEric Cheng 			die("flow name too long");
1767da14cebeSEric Cheng 		state.fs_flow = argv[optind];
1768da14cebeSEric Cheng 	} else if (optind != argc) {
1769da14cebeSEric Cheng 		usage();
1770da14cebeSEric Cheng 	}
1771da14cebeSEric Cheng 	state.fs_proplist = proplist;
1772da14cebeSEric Cheng 	state.fs_status = DLADM_STATUS_OK;
1773da14cebeSEric Cheng 
17748002d411SSowmini Varadhan 	oferr = ofmt_open(fields_str, flowprop_fields, ofmtflags, 0, &ofmt);
17758002d411SSowmini Varadhan 	flowadm_ofmt_check(oferr, state.fs_parsable, ofmt);
17768002d411SSowmini Varadhan 	state.fs_ofmt = ofmt;
1777da14cebeSEric Cheng 
1778da14cebeSEric Cheng 	/* Show properties for one flow */
1779da14cebeSEric Cheng 	if (state.fs_flow != NULL) {
1780da14cebeSEric Cheng 		show_flowprop_one_flow(&state, state.fs_flow);
1781da14cebeSEric Cheng 
1782da14cebeSEric Cheng 	/* Show properties for all flows on one link */
1783da14cebeSEric Cheng 	} else if (state.fs_linkid != DATALINK_INVALID_LINKID) {
17844ac67f02SAnurag S. Maskey 		(void) show_flowprop_onelink(handle, state.fs_linkid, &state);
1785da14cebeSEric Cheng 
1786da14cebeSEric Cheng 	/* Show properties for all flows on all links */
1787da14cebeSEric Cheng 	} else {
17884ac67f02SAnurag S. Maskey 		(void) dladm_walk_datalink_id(show_flowprop_onelink, handle,
17894ac67f02SAnurag S. Maskey 		    &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
1790da14cebeSEric Cheng 		    DLADM_OPT_ACTIVE);
1791da14cebeSEric Cheng 	}
1792da14cebeSEric Cheng 
1793da14cebeSEric Cheng 	dladm_free_props(proplist);
17948002d411SSowmini Varadhan 	ofmt_close(ofmt);
1795da14cebeSEric Cheng }
1796da14cebeSEric Cheng 
1797da14cebeSEric Cheng static void
1798da14cebeSEric Cheng show_flowprop_one_flow(void *arg, const char *flow)
1799da14cebeSEric Cheng {
1800da14cebeSEric Cheng 	int			i;
1801da14cebeSEric Cheng 	char			*buf;
1802da14cebeSEric Cheng 	dladm_status_t		status;
1803da14cebeSEric Cheng 	dladm_arg_list_t	*proplist = NULL;
1804da14cebeSEric Cheng 	show_flowprop_state_t	*statep = arg;
1805da14cebeSEric Cheng 	dladm_flow_attr_t	attr;
1806da14cebeSEric Cheng 	const char		*savep;
1807da14cebeSEric Cheng 
1808da14cebeSEric Cheng 	/*
1809da14cebeSEric Cheng 	 * Do not print flow props for invalid flows.
1810da14cebeSEric Cheng 	 */
18114ac67f02SAnurag S. Maskey 	if ((status = dladm_flow_info(handle, flow, &attr)) !=
18124ac67f02SAnurag S. Maskey 	    DLADM_STATUS_OK) {
1813da14cebeSEric Cheng 		die("invalid flow: '%s'", flow);
1814da14cebeSEric Cheng 	}
1815da14cebeSEric Cheng 
1816da14cebeSEric Cheng 	savep = statep->fs_flow;
1817da14cebeSEric Cheng 	statep->fs_flow = flow;
1818da14cebeSEric Cheng 
1819da14cebeSEric Cheng 	proplist = statep->fs_proplist;
1820da14cebeSEric Cheng 
1821da14cebeSEric Cheng 	buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX)
1822da14cebeSEric Cheng 	    * DLADM_MAX_PROP_VALCNT + MAX_PROP_LINE);
1823da14cebeSEric Cheng 	if (buf == NULL)
1824da14cebeSEric Cheng 		die("insufficient memory");
1825da14cebeSEric Cheng 
1826da14cebeSEric Cheng 	statep->fs_propvals = (char **)(void *)buf;
1827da14cebeSEric Cheng 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
1828da14cebeSEric Cheng 		statep->fs_propvals[i] = buf +
1829da14cebeSEric Cheng 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
1830da14cebeSEric Cheng 		    i * DLADM_PROP_VAL_MAX;
1831da14cebeSEric Cheng 	}
1832da14cebeSEric Cheng 	statep->fs_line = buf +
1833da14cebeSEric Cheng 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
1834da14cebeSEric Cheng 
1835da14cebeSEric Cheng 	/* show only specified flow properties */
1836da14cebeSEric Cheng 	if (proplist != NULL) {
1837da14cebeSEric Cheng 		for (i = 0; i < proplist->al_count; i++) {
1838da14cebeSEric Cheng 			if (show_one_flowprop(statep,
1839da14cebeSEric Cheng 			    proplist->al_info[i].ai_name) != DLADM_STATUS_OK)
1840da14cebeSEric Cheng 				break;
1841da14cebeSEric Cheng 		}
1842da14cebeSEric Cheng 
1843da14cebeSEric Cheng 	/* show all flow properties */
1844da14cebeSEric Cheng 	} else {
1845da14cebeSEric Cheng 		status = dladm_walk_flowprop(show_one_flowprop, flow, statep);
1846da14cebeSEric Cheng 		if (status != DLADM_STATUS_OK)
1847da14cebeSEric Cheng 			die_dlerr(status, "show-flowprop");
1848da14cebeSEric Cheng 	}
1849da14cebeSEric Cheng 	free(buf);
1850da14cebeSEric Cheng 	statep->fs_flow = savep;
1851da14cebeSEric Cheng }
1852da14cebeSEric Cheng 
1853da14cebeSEric Cheng /*
18548002d411SSowmini Varadhan  * default output callback function that, when invoked from dladm_print_output,
18558002d411SSowmini Varadhan  * prints string which is offset by of_arg->ofmt_id within buf.
1856da14cebeSEric Cheng  */
18578002d411SSowmini Varadhan static boolean_t
18588002d411SSowmini Varadhan print_default_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
1859da14cebeSEric Cheng {
1860da14cebeSEric Cheng 	char *value;
1861da14cebeSEric Cheng 
18628002d411SSowmini Varadhan 	value = (char *)of_arg->ofmt_cbarg + of_arg->ofmt_id;
18638002d411SSowmini Varadhan 	(void) strlcpy(buf, value, bufsize);
18648002d411SSowmini Varadhan 	return (B_TRUE);
1865da14cebeSEric Cheng }
1866da14cebeSEric Cheng 
1867da14cebeSEric Cheng static void
18688002d411SSowmini Varadhan flowadm_ofmt_check(ofmt_status_t oferr, boolean_t parsable,
18698002d411SSowmini Varadhan     ofmt_handle_t ofmt)
1870da14cebeSEric Cheng {
18718002d411SSowmini Varadhan 	char buf[OFMT_BUFSIZE];
1872da14cebeSEric Cheng 
18738002d411SSowmini Varadhan 	if (oferr == OFMT_SUCCESS)
18748002d411SSowmini Varadhan 		return;
18758002d411SSowmini Varadhan 	(void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf));
1876ad091ee1SMichael Lim 	/*
18778002d411SSowmini Varadhan 	 * All errors are considered fatal in parsable mode.
18788002d411SSowmini Varadhan 	 * NOMEM errors are always fatal, regardless of mode.
18798002d411SSowmini Varadhan 	 * For other errors, we print diagnostics in human-readable
18808002d411SSowmini Varadhan 	 * mode and processs what we can.
1881ad091ee1SMichael Lim 	 */
18828002d411SSowmini Varadhan 	if (parsable || oferr == OFMT_ENOFIELDS) {
18838002d411SSowmini Varadhan 		ofmt_close(ofmt);
18848002d411SSowmini Varadhan 		die(buf);
1885da14cebeSEric Cheng 	} else {
18868002d411SSowmini Varadhan 		warn(buf);
1887da14cebeSEric Cheng 	}
1888da14cebeSEric Cheng }
1889