Print this page
4420 flowstat usage message is incorrect
4420 flowstat usage message is incorrect
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/flowstat/flowstat.c
+++ new/usr/src/cmd/flowstat/flowstat.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #include <stdio.h>
27 27 #include <locale.h>
28 28 #include <stdarg.h>
29 29 #include <stdlib.h>
30 30 #include <fcntl.h>
31 31 #include <string.h>
32 32 #include <stropts.h>
33 33 #include <errno.h>
34 34 #include <strings.h>
35 35 #include <getopt.h>
36 36 #include <unistd.h>
37 37 #include <priv.h>
38 38 #include <netdb.h>
39 39 #include <libintl.h>
40 40 #include <libdlflow.h>
41 41 #include <libdllink.h>
42 42 #include <libdlstat.h>
43 43 #include <sys/types.h>
44 44 #include <sys/socket.h>
45 45 #include <netinet/in.h>
46 46 #include <arpa/inet.h>
47 47 #include <sys/ethernet.h>
48 48 #include <inet/ip.h>
49 49 #include <inet/ip6.h>
50 50 #include <stddef.h>
51 51 #include <ofmt.h>
52 52
53 53 typedef struct flow_chain_s {
54 54 char fc_flowname[MAXFLOWNAMELEN];
55 55 boolean_t fc_visited;
56 56 flow_stat_t *fc_stat;
57 57 struct flow_chain_s *fc_next;
58 58 } flow_chain_t;
59 59
60 60 typedef struct show_flow_state {
61 61 flow_chain_t *fs_flowchain;
62 62 ofmt_handle_t fs_ofmt;
63 63 char fs_unit;
64 64 boolean_t fs_parsable;
65 65 } show_flow_state_t;
66 66
67 67 typedef struct show_history_state_s {
68 68 boolean_t us_plot;
69 69 boolean_t us_parsable;
70 70 boolean_t us_printheader;
71 71 boolean_t us_first;
72 72 boolean_t us_showall;
73 73 ofmt_handle_t us_ofmt;
74 74 } show_history_state_t;
75 75
76 76 static void do_show_history(int, char **);
77 77
78 78 static int query_flow_stats(dladm_handle_t, dladm_flow_attr_t *, void *);
79 79 static int query_link_flow_stats(dladm_handle_t, datalink_id_t, void *);
80 80
81 81 static void die(const char *, ...);
82 82 static void die_optdup(int);
83 83 static void die_opterr(int, int, const char *);
84 84 static void die_dlerr(dladm_status_t, const char *, ...);
85 85 static void warn(const char *, ...);
86 86
87 87 /* callback functions for printing output */
88 88 static ofmt_cb_t print_default_cb, print_flow_stats_cb;
89 89 static void flowstat_ofmt_check(ofmt_status_t, boolean_t, ofmt_handle_t);
90 90
91 91 #define NULL_OFMT {NULL, 0, 0, NULL}
92 92
93 93 /*
94 94 * structures for flowstat (printing live statistics)
95 95 */
96 96 typedef enum {
97 97 FLOW_S_FLOW,
98 98 FLOW_S_IPKTS,
99 99 FLOW_S_RBYTES,
100 100 FLOW_S_IERRORS,
101 101 FLOW_S_OPKTS,
102 102 FLOW_S_OBYTES,
103 103 FLOW_S_OERRORS
104 104 } flow_s_field_index_t;
105 105
106 106 static ofmt_field_t flow_s_fields[] = {
107 107 /* name, field width, index, callback */
108 108 { "FLOW", 15, FLOW_S_FLOW, print_flow_stats_cb},
109 109 { "IPKTS", 8, FLOW_S_IPKTS, print_flow_stats_cb},
110 110 { "RBYTES", 8, FLOW_S_RBYTES, print_flow_stats_cb},
111 111 { "IERRS", 8, FLOW_S_IERRORS, print_flow_stats_cb},
112 112 { "OPKTS", 8, FLOW_S_OPKTS, print_flow_stats_cb},
113 113 { "OBYTES", 8, FLOW_S_OBYTES, print_flow_stats_cb},
114 114 { "OERRS", 8, FLOW_S_OERRORS, print_flow_stats_cb},
115 115 NULL_OFMT}
116 116 ;
117 117
118 118 typedef struct flow_args_s {
119 119 char *flow_s_flow;
120 120 flow_stat_t *flow_s_stat;
121 121 char flow_s_unit;
122 122 boolean_t flow_s_parsable;
123 123 } flow_args_t;
124 124
125 125 /*
126 126 * structures for 'flowstat -h'
127 127 */
128 128 typedef struct history_fields_buf_s {
129 129 char history_flow[12];
130 130 char history_duration[10];
131 131 char history_ipackets[9];
132 132 char history_rbytes[10];
133 133 char history_opackets[9];
134 134 char history_obytes[10];
135 135 char history_bandwidth[14];
136 136 } history_fields_buf_t;
137 137
138 138 static ofmt_field_t history_fields[] = {
139 139 /* name, field width, offset */
140 140 { "FLOW", 13,
141 141 offsetof(history_fields_buf_t, history_flow), print_default_cb},
142 142 { "DURATION", 11,
143 143 offsetof(history_fields_buf_t, history_duration), print_default_cb},
144 144 { "IPACKETS", 10,
145 145 offsetof(history_fields_buf_t, history_ipackets), print_default_cb},
146 146 { "RBYTES", 11,
147 147 offsetof(history_fields_buf_t, history_rbytes), print_default_cb},
148 148 { "OPACKETS", 10,
149 149 offsetof(history_fields_buf_t, history_opackets), print_default_cb},
150 150 { "OBYTES", 11,
151 151 offsetof(history_fields_buf_t, history_obytes), print_default_cb},
152 152 { "BANDWIDTH", 15,
153 153 offsetof(history_fields_buf_t, history_bandwidth), print_default_cb},
154 154 NULL_OFMT}
155 155 ;
156 156
157 157 typedef struct history_l_fields_buf_s {
158 158 char history_l_flow[12];
159 159 char history_l_stime[13];
160 160 char history_l_etime[13];
161 161 char history_l_rbytes[8];
162 162 char history_l_obytes[8];
163 163 char history_l_bandwidth[14];
164 164 } history_l_fields_buf_t;
165 165
166 166 static ofmt_field_t history_l_fields[] = {
167 167 /* name, field width, offset */
168 168 { "FLOW", 13,
169 169 offsetof(history_l_fields_buf_t, history_l_flow), print_default_cb},
170 170 { "START", 14,
171 171 offsetof(history_l_fields_buf_t, history_l_stime), print_default_cb},
172 172 { "END", 14,
173 173 offsetof(history_l_fields_buf_t, history_l_etime), print_default_cb},
174 174 { "RBYTES", 9,
175 175 offsetof(history_l_fields_buf_t, history_l_rbytes), print_default_cb},
176 176 { "OBYTES", 9,
177 177 offsetof(history_l_fields_buf_t, history_l_obytes), print_default_cb},
178 178 { "BANDWIDTH", 15,
179 179 offsetof(history_l_fields_buf_t, history_l_bandwidth),
180 180 print_default_cb},
181 181 NULL_OFMT}
↓ open down ↓ |
181 lines elided |
↑ open up ↑ |
182 182 ;
183 183
184 184 static char *progname;
185 185
186 186 /*
187 187 * Handle to libdladm. Opened in main() before the sub-command
188 188 * specific function is called.
189 189 */
190 190 static dladm_handle_t handle = NULL;
191 191
192 -const char *usage_ermsg = "flowstat [-r | -t] [-i interval] "
192 +const char *usage_ermsg = "usage: flowstat [-r | -t] [-i interval] "
193 193 "[-l link] [flow]\n"
194 - " flowstat [-S] [-A] [-i interval] [-p] [ -o field[,...]]\n"
195 - " [-u R|K|M|G|T|P] [-l link] [flow]\n"
196 - " flowstat -h [-a] [-d] [-F format]"
197 - " [-s <DD/MM/YYYY,HH:MM:SS>]\n"
198 - " [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> "
199 - "[<flow>]";
194 + " flowstat [-p | -u R|K|M|G|T|P] [ -o field[,...]] "
195 + "[-l link] [flow]\n"
196 + " flowstat [-A] [-l link] [flow]\n"
197 + " flowstat [-S] [-i interval] [-l link] [flow]\n"
198 + " flowstat -h [-a] [-d] [-F format] "
199 + "[-s <DD/MM/YYYY,HH:MM:SS>]\n"
200 + " [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<flow>]";
200 201
201 202 static void
202 203 usage(void)
203 204 {
204 205 (void) fprintf(stderr, "%s\n", gettext(usage_ermsg));
205 206
206 207 /* close dladm handle if it was opened */
207 208 if (handle != NULL)
208 209 dladm_close(handle);
209 210
210 211 exit(1);
211 212 }
212 213
213 214 boolean_t
214 215 flowstat_unit(char *oarg, char *unit)
215 216 {
216 217 if ((strcmp(oarg, "R") == 0) || (strcmp(oarg, "K") == 0) ||
217 218 (strcmp(oarg, "M") == 0) || (strcmp(oarg, "G") == 0) ||
218 219 (strcmp(oarg, "T") == 0) || (strcmp(oarg, "P") == 0)) {
219 220 *unit = oarg[0];
220 221 return (B_TRUE);
221 222 }
222 223
223 224 return (B_FALSE);
224 225 }
225 226
226 227 void
227 228 map_to_units(char *buf, uint_t bufsize, double num, char unit,
228 229 boolean_t parsable)
229 230 {
230 231 if (parsable) {
231 232 (void) snprintf(buf, bufsize, "%.0lf", num);
232 233 return;
233 234 }
234 235
235 236 if (unit == '\0') {
236 237 int index;
237 238
238 239 for (index = 0; (int)(num/1000) != 0; index++, num /= 1000)
239 240 ;
240 241
241 242 switch (index) {
242 243 case 0:
243 244 unit = '\0';
244 245 break;
245 246 case 1:
246 247 unit = 'K';
247 248 break;
248 249 case 2:
249 250 unit = 'M';
250 251 break;
251 252 case 3:
252 253 unit = 'G';
253 254 break;
254 255 case 4:
255 256 unit = 'T';
256 257 break;
257 258 case 5:
258 259 /* Largest unit supported */
259 260 default:
260 261 unit = 'P';
261 262 break;
262 263 }
263 264 } else {
264 265 switch (unit) {
265 266 case 'R':
266 267 /* Already raw numbers */
267 268 unit = '\0';
268 269 break;
269 270 case 'K':
270 271 num /= 1000;
271 272 break;
272 273 case 'M':
273 274 num /= (1000*1000);
274 275 break;
275 276 case 'G':
276 277 num /= (1000*1000*1000);
277 278 break;
278 279 case 'T':
279 280 num /= (1000.0*1000.0*1000.0*1000.0);
280 281 break;
281 282 case 'P':
282 283 /* Largest unit supported */
283 284 default:
284 285 num /= (1000.0*1000.0*1000.0*1000.0*1000.0);
285 286 break;
286 287 }
287 288 }
288 289
289 290 if (unit == '\0')
290 291 (void) snprintf(buf, bufsize, " %7.0lf%c", num, unit);
291 292 else
292 293 (void) snprintf(buf, bufsize, " %6.2lf%c", num, unit);
293 294 }
294 295
295 296 flow_chain_t *
296 297 get_flow_prev_stat(const char *flowname, void *arg)
297 298 {
298 299 show_flow_state_t *state = arg;
299 300 flow_chain_t *flow_curr = NULL;
300 301
301 302 /* Scan prev flowname list and look for entry matching this entry */
302 303 for (flow_curr = state->fs_flowchain; flow_curr;
303 304 flow_curr = flow_curr->fc_next) {
304 305 if (strcmp(flow_curr->fc_flowname, flowname) == 0)
305 306 break;
306 307 }
307 308
308 309 /* New flow, add it */
309 310 if (flow_curr == NULL) {
310 311 flow_curr = (flow_chain_t *)malloc(sizeof (flow_chain_t));
311 312 if (flow_curr == NULL)
312 313 goto done;
313 314 (void) strncpy(flow_curr->fc_flowname, flowname,
314 315 MAXFLOWNAMELEN);
315 316 flow_curr->fc_stat = NULL;
316 317 flow_curr->fc_next = state->fs_flowchain;
317 318 state->fs_flowchain = flow_curr;
318 319 }
319 320 done:
320 321 return (flow_curr);
321 322 }
322 323
323 324 /*
324 325 * Number of flows may change while flowstat -i is executing.
325 326 * Free memory allocated for flows that are no longer there.
326 327 * Prepare for next iteration by marking visited = false for
327 328 * existing stat entries.
328 329 */
329 330 static void
330 331 cleanup_removed_flows(show_flow_state_t *state)
331 332 {
332 333 flow_chain_t *fcurr;
333 334 flow_chain_t *fprev;
334 335 flow_chain_t *tofree;
335 336
336 337 /* Delete all nodes from the list that have fc_visited marked false */
337 338 fcurr = state->fs_flowchain;
338 339 while (fcurr != NULL) {
339 340 if (fcurr->fc_visited) {
340 341 fcurr->fc_visited = B_FALSE;
341 342 fprev = fcurr;
342 343 fcurr = fcurr->fc_next;
343 344 continue;
344 345 }
345 346
346 347 /* Is it head of the list? */
347 348 if (fcurr == state->fs_flowchain)
348 349 state->fs_flowchain = fcurr->fc_next;
349 350 else
350 351 fprev->fc_next = fcurr->fc_next;
351 352
352 353 /* fprev remains the same */
353 354 tofree = fcurr;
354 355 fcurr = fcurr->fc_next;
355 356
356 357 /* Free stats memory for the removed flow */
357 358 dladm_flow_stat_free(tofree->fc_stat);
358 359 free(tofree);
359 360 }
360 361 }
361 362
362 363 static boolean_t
363 364 print_flow_stats_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
364 365 {
365 366 flow_args_t *fargs = of_arg->ofmt_cbarg;
366 367 flow_stat_t *diff_stats = fargs->flow_s_stat;
367 368 char unit = fargs->flow_s_unit;
368 369 boolean_t parsable = fargs->flow_s_parsable;
369 370
370 371 switch (of_arg->ofmt_id) {
371 372 case FLOW_S_FLOW:
372 373 (void) snprintf(buf, bufsize, "%s", fargs->flow_s_flow);
373 374 break;
374 375 case FLOW_S_IPKTS:
375 376 map_to_units(buf, bufsize, diff_stats->fl_ipackets, unit,
376 377 parsable);
377 378 break;
378 379 case FLOW_S_RBYTES:
379 380 map_to_units(buf, bufsize, diff_stats->fl_rbytes, unit,
380 381 parsable);
381 382 break;
382 383 case FLOW_S_IERRORS:
383 384 map_to_units(buf, bufsize, diff_stats->fl_ierrors, unit,
384 385 parsable);
385 386 break;
386 387 case FLOW_S_OPKTS:
387 388 map_to_units(buf, bufsize, diff_stats->fl_opackets, unit,
388 389 parsable);
389 390 break;
390 391 case FLOW_S_OBYTES:
391 392 map_to_units(buf, bufsize, diff_stats->fl_obytes, unit,
392 393 parsable);
393 394 break;
394 395 case FLOW_S_OERRORS:
395 396 map_to_units(buf, bufsize, diff_stats->fl_oerrors, unit,
396 397 parsable);
397 398 break;
398 399 default:
399 400 die("invalid input");
400 401 break;
401 402 }
402 403 return (B_TRUE);
403 404 }
404 405
405 406 /* ARGSUSED */
406 407 static int
407 408 query_flow_stats(dladm_handle_t handle, dladm_flow_attr_t *attr, void *arg)
408 409 {
409 410 show_flow_state_t *state = arg;
410 411 flow_chain_t *flow_node;
411 412 flow_stat_t *curr_stat;
412 413 flow_stat_t *prev_stat;
413 414 flow_stat_t *diff_stat;
414 415 char *flowname = attr->fa_flowname;
415 416 flow_args_t fargs;
416 417
417 418 /* Get previous stats for the flow */
418 419 flow_node = get_flow_prev_stat(flowname, arg);
419 420 if (flow_node == NULL)
420 421 goto done;
421 422
422 423 flow_node->fc_visited = B_TRUE;
423 424 prev_stat = flow_node->fc_stat;
424 425
425 426 /* Query library for current stats */
426 427 curr_stat = dladm_flow_stat_query(flowname);
427 428 if (curr_stat == NULL)
428 429 goto done;
429 430
430 431 /* current stats - prev iteration stats */
431 432 diff_stat = dladm_flow_stat_diff(curr_stat, prev_stat);
432 433
433 434 /* Free prev stats */
434 435 dladm_flow_stat_free(prev_stat);
435 436
436 437 /* Prev <- curr stats */
437 438 flow_node->fc_stat = curr_stat;
438 439
439 440 if (diff_stat == NULL)
440 441 goto done;
441 442
442 443 /* Print stats */
443 444 fargs.flow_s_flow = flowname;
444 445 fargs.flow_s_stat = diff_stat;
445 446 fargs.flow_s_unit = state->fs_unit;
446 447 fargs.flow_s_parsable = state->fs_parsable;
447 448 ofmt_print(state->fs_ofmt, &fargs);
448 449
449 450 /* Free diff stats */
450 451 dladm_flow_stat_free(diff_stat);
451 452 done:
452 453 return (DLADM_WALK_CONTINUE);
453 454 }
454 455
455 456 /*
456 457 * Wrapper of dladm_walk_flow(query_flow_stats,...) to make it usable for
457 458 * dladm_walk_datalink_id(). Used for showing flow stats for
458 459 * all flows on all links.
459 460 */
460 461 static int
461 462 query_link_flow_stats(dladm_handle_t dh, datalink_id_t linkid, void * arg)
462 463 {
463 464 if (dladm_walk_flow(query_flow_stats, dh, linkid, arg, B_FALSE)
464 465 == DLADM_STATUS_OK)
465 466 return (DLADM_WALK_CONTINUE);
466 467 else
467 468 return (DLADM_WALK_TERMINATE);
468 469 }
469 470
470 471 void
471 472 print_all_stats(name_value_stat_entry_t *stat_entry)
472 473 {
473 474 name_value_stat_t *curr_stat;
474 475
475 476 printf("%s\n", stat_entry->nve_header);
476 477
477 478 for (curr_stat = stat_entry->nve_stats; curr_stat != NULL;
478 479 curr_stat = curr_stat->nv_nextstat) {
479 480 printf("\t%15s", curr_stat->nv_statname);
480 481 printf("\t%15llu\n", curr_stat->nv_statval);
481 482 }
482 483 }
483 484
484 485 /* ARGSUSED */
485 486 static int
486 487 dump_one_flow_stats(dladm_handle_t handle, dladm_flow_attr_t *attr, void *arg)
487 488 {
488 489 char *flowname = attr->fa_flowname;
489 490 void *stat;
490 491
491 492 stat = dladm_flow_stat_query_all(flowname);
492 493 if (stat == NULL)
493 494 goto done;
494 495 print_all_stats(stat);
495 496 dladm_flow_stat_query_all_free(stat);
496 497
497 498 done:
498 499 return (DLADM_WALK_CONTINUE);
499 500 }
500 501
501 502 /*
502 503 * Wrapper of dladm_walk_flow(query_flow_stats,...) to make it usable for
503 504 * dladm_walk_datalink_id(). Used for showing flow stats for
504 505 * all flows on all links.
505 506 */
506 507 static int
507 508 dump_link_flow_stats(dladm_handle_t dh, datalink_id_t linkid, void * arg)
508 509 {
509 510 if (dladm_walk_flow(dump_one_flow_stats, dh, linkid, arg, B_FALSE)
510 511 == DLADM_STATUS_OK)
511 512 return (DLADM_WALK_CONTINUE);
512 513 else
513 514 return (DLADM_WALK_TERMINATE);
514 515 }
515 516
516 517 static void
517 518 dump_all_flow_stats(dladm_flow_attr_t *attrp, void *arg, datalink_id_t linkid,
518 519 boolean_t flow_arg)
519 520 {
520 521 /* Show stats for named flow */
521 522 if (flow_arg) {
522 523 (void) dump_one_flow_stats(handle, attrp, arg);
523 524
524 525 /* Show stats for flows on one link */
525 526 } else if (linkid != DATALINK_INVALID_LINKID) {
526 527 (void) dladm_walk_flow(dump_one_flow_stats, handle, linkid,
527 528 arg, B_FALSE);
528 529
529 530 /* Show stats for all flows on all links */
530 531 } else {
531 532 (void) dladm_walk_datalink_id(dump_link_flow_stats,
532 533 handle, arg, DATALINK_CLASS_ALL,
533 534 DATALINK_ANY_MEDIATYPE, DLADM_OPT_ACTIVE);
534 535 }
535 536 }
536 537
537 538 int
538 539 main(int argc, char *argv[])
539 540 {
540 541 dladm_status_t status;
541 542 int option;
542 543 boolean_t r_arg = B_FALSE;
543 544 boolean_t t_arg = B_FALSE;
544 545 boolean_t p_arg = B_FALSE;
545 546 boolean_t i_arg = B_FALSE;
546 547 boolean_t o_arg = B_FALSE;
547 548 boolean_t u_arg = B_FALSE;
548 549 boolean_t A_arg = B_FALSE;
549 550 boolean_t S_arg = B_FALSE;
550 551 boolean_t flow_arg = B_FALSE;
551 552 datalink_id_t linkid = DATALINK_ALL_LINKID;
552 553 char linkname[MAXLINKNAMELEN];
553 554 char flowname[MAXFLOWNAMELEN];
554 555 uint32_t interval = 0;
555 556 char unit = '\0';
556 557 show_flow_state_t state;
557 558 char *fields_str = NULL;
558 559 char *o_fields_str = NULL;
559 560
560 561 char *total_stat_fields =
561 562 "flow,ipkts,rbytes,ierrs,opkts,obytes,oerrs";
562 563 char *rx_stat_fields =
563 564 "flow,ipkts,rbytes,ierrs";
564 565 char *tx_stat_fields =
565 566 "flow,opkts,obytes,oerrs";
566 567
567 568 ofmt_handle_t ofmt;
568 569 ofmt_status_t oferr;
569 570 uint_t ofmtflags = OFMT_RIGHTJUST;
570 571
571 572 dladm_flow_attr_t attr;
572 573
573 574 (void) setlocale(LC_ALL, "");
574 575 #if !defined(TEXT_DOMAIN)
575 576 #define TEXT_DOMAIN "SYS_TEST"
576 577 #endif
577 578 (void) textdomain(TEXT_DOMAIN);
578 579
579 580 progname = argv[0];
580 581
581 582 /* Open the libdladm handle */
582 583 if ((status = dladm_open(&handle)) != DLADM_STATUS_OK)
583 584 die_dlerr(status, "could not open /dev/dld");
584 585
585 586 bzero(&state, sizeof (state));
586 587
587 588 opterr = 0;
588 589 while ((option = getopt_long(argc, argv, ":rtApSi:o:u:l:h",
589 590 NULL, NULL)) != -1) {
590 591 switch (option) {
591 592 case 'r':
592 593 if (r_arg)
593 594 die_optdup(option);
594 595
595 596 r_arg = B_TRUE;
596 597 break;
597 598 case 't':
598 599 if (t_arg)
599 600 die_optdup(option);
600 601
601 602 t_arg = B_TRUE;
602 603 break;
603 604 case 'A':
604 605 if (A_arg)
605 606 die_optdup(option);
606 607
607 608 A_arg = B_TRUE;
608 609 break;
609 610 case 'p':
610 611 if (p_arg)
611 612 die_optdup(option);
612 613
613 614 p_arg = B_TRUE;
614 615 break;
615 616 case 'S':
616 617 if (S_arg)
617 618 die_optdup(option);
618 619 S_arg = B_TRUE;
619 620 break;
620 621 case 'i':
621 622 if (i_arg)
622 623 die_optdup(option);
623 624
624 625 i_arg = B_TRUE;
625 626 if (!dladm_str2interval(optarg, &interval))
626 627 die("invalid interval value '%s'", optarg);
627 628 break;
628 629 case 'o':
629 630 o_arg = B_TRUE;
630 631 o_fields_str = optarg;
631 632 break;
632 633 case 'u':
633 634 if (u_arg)
634 635 die_optdup(option);
635 636
636 637 u_arg = B_TRUE;
637 638 if (!flowstat_unit(optarg, &unit))
638 639 die("invalid unit value '%s',"
639 640 "unit must be R|K|M|G|T|P", optarg);
640 641 break;
641 642 case 'l':
642 643 if (strlcpy(linkname, optarg, MAXLINKNAMELEN)
643 644 >= MAXLINKNAMELEN)
644 645 die("link name too long\n");
645 646 if (dladm_name2info(handle, linkname, &linkid, NULL,
646 647 NULL, NULL) != DLADM_STATUS_OK)
647 648 die("invalid link '%s'", linkname);
648 649 break;
649 650 case 'h':
650 651 if (r_arg || t_arg || p_arg || o_arg || u_arg ||
651 652 i_arg || S_arg || A_arg) {
652 653 die("the option -h is not compatible with "
653 654 "-r, -t, -p, -o, -u, -i, -S, -A");
654 655 }
655 656 do_show_history(argc, argv);
656 657 return (0);
657 658 break;
658 659 default:
659 660 die_opterr(optopt, option, usage_ermsg);
660 661 break;
661 662 }
662 663 }
663 664
664 665 if (r_arg && t_arg)
665 666 die("the option -t and -r are not compatible");
666 667
667 668 if (u_arg && p_arg)
668 669 die("the option -u and -p are not compatible");
669 670
670 671 if (p_arg && !o_arg)
671 672 die("-p requires -o");
672 673
673 674 if (p_arg && strcasecmp(o_fields_str, "all") == 0)
674 675 die("\"-o all\" is invalid with -p");
675 676
676 677 if (S_arg &&
677 678 (r_arg || t_arg || p_arg || o_arg || u_arg))
678 679 die("the option -S is not compatible with "
679 680 "-r, -t, -p, -o, -u");
680 681
681 682 if (A_arg &&
682 683 (r_arg || t_arg || p_arg || o_arg || u_arg || i_arg))
683 684 die("the option -A is not compatible with "
684 685 "-r, -t, -p, -o, -u, -i");
685 686
686 687 /* get flow name (optional last argument) */
687 688 if (optind == (argc-1)) {
688 689 if (strlcpy(flowname, argv[optind], MAXFLOWNAMELEN)
689 690 >= MAXFLOWNAMELEN)
690 691 die("flow name too long");
691 692 flow_arg = B_TRUE;
692 693 } else if (optind != argc) {
693 694 usage();
694 695 }
695 696
696 697 if (S_arg) {
697 698 dladm_continuous(handle, linkid, (flow_arg ? flowname : NULL),
698 699 interval, FLOW_REPORT);
699 700 return (0);
700 701 }
701 702
702 703 if (flow_arg &&
703 704 dladm_flow_info(handle, flowname, &attr) != DLADM_STATUS_OK)
704 705 die("invalid flow %s", flowname);
705 706
706 707 if (A_arg) {
707 708 dump_all_flow_stats(&attr, &state, linkid, flow_arg);
708 709 return (0);
709 710 }
710 711
711 712 state.fs_unit = unit;
712 713 state.fs_parsable = p_arg;
713 714
714 715 if (state.fs_parsable)
715 716 ofmtflags |= OFMT_PARSABLE;
716 717
717 718 if (r_arg)
718 719 fields_str = rx_stat_fields;
719 720 else if (t_arg)
720 721 fields_str = tx_stat_fields;
721 722 else
722 723 fields_str = total_stat_fields;
723 724
724 725 if (o_arg) {
725 726 fields_str = (strcasecmp(o_fields_str, "all") == 0) ?
726 727 fields_str : o_fields_str;
727 728 }
728 729
729 730 oferr = ofmt_open(fields_str, flow_s_fields, ofmtflags, 0, &ofmt);
730 731 flowstat_ofmt_check(oferr, state.fs_parsable, ofmt);
731 732 state.fs_ofmt = ofmt;
732 733
733 734 for (;;) {
734 735 /* Show stats for named flow */
735 736 if (flow_arg) {
736 737 (void) query_flow_stats(handle, &attr, &state);
737 738
738 739 /* Show stats for flows on one link */
739 740 } else if (linkid != DATALINK_INVALID_LINKID) {
740 741 (void) dladm_walk_flow(query_flow_stats, handle, linkid,
741 742 &state, B_FALSE);
742 743
743 744 /* Show stats for all flows on all links */
744 745 } else {
745 746 (void) dladm_walk_datalink_id(query_link_flow_stats,
746 747 handle, &state, DATALINK_CLASS_ALL,
747 748 DATALINK_ANY_MEDIATYPE, DLADM_OPT_ACTIVE);
748 749 }
749 750
750 751 if (interval == 0)
751 752 break;
752 753
753 754 (void) fflush(stdout);
754 755 cleanup_removed_flows(&state);
755 756 (void) sleep(interval);
756 757 }
757 758 ofmt_close(ofmt);
758 759
759 760 dladm_close(handle);
760 761 return (0);
761 762 }
762 763
763 764 /* ARGSUSED */
764 765 static int
765 766 show_history_date(dladm_usage_t *history, void *arg)
766 767 {
767 768 show_history_state_t *state = (show_history_state_t *)arg;
768 769 time_t stime;
769 770 char timebuf[20];
770 771 dladm_flow_attr_t attr;
771 772 dladm_status_t status;
772 773
773 774 /*
774 775 * Only show historical information for existing flows unless '-a'
775 776 * is specified.
776 777 */
777 778 if (!state->us_showall && ((status = dladm_flow_info(handle,
778 779 history->du_name, &attr)) != DLADM_STATUS_OK)) {
779 780 return (status);
780 781 }
781 782
782 783 stime = history->du_stime;
783 784 (void) strftime(timebuf, sizeof (timebuf), "%m/%d/%Y",
784 785 localtime(&stime));
785 786 (void) printf("%s\n", timebuf);
786 787
787 788 return (DLADM_STATUS_OK);
788 789 }
789 790
790 791 static int
791 792 show_history_time(dladm_usage_t *history, void *arg)
792 793 {
793 794 show_history_state_t *state = (show_history_state_t *)arg;
794 795 char buf[DLADM_STRSIZE];
795 796 history_l_fields_buf_t ubuf;
796 797 time_t time;
797 798 double bw;
798 799 dladm_flow_attr_t attr;
799 800 dladm_status_t status;
800 801
801 802 /*
802 803 * Only show historical information for existing flows unless '-a'
803 804 * is specified.
804 805 */
805 806 if (!state->us_showall && ((status = dladm_flow_info(handle,
806 807 history->du_name, &attr)) != DLADM_STATUS_OK)) {
807 808 return (status);
808 809 }
809 810
810 811 if (state->us_plot) {
811 812 if (!state->us_printheader) {
812 813 if (state->us_first) {
813 814 (void) printf("# Time");
814 815 state->us_first = B_FALSE;
815 816 }
816 817 (void) printf(" %s", history->du_name);
817 818 if (history->du_last) {
818 819 (void) printf("\n");
819 820 state->us_first = B_TRUE;
820 821 state->us_printheader = B_TRUE;
821 822 }
822 823 } else {
823 824 if (state->us_first) {
824 825 time = history->du_etime;
825 826 (void) strftime(buf, sizeof (buf), "%T",
826 827 localtime(&time));
827 828 state->us_first = B_FALSE;
828 829 (void) printf("%s", buf);
829 830 }
830 831 bw = (double)history->du_bandwidth/1000;
831 832 (void) printf(" %.2f", bw);
832 833 if (history->du_last) {
833 834 (void) printf("\n");
834 835 state->us_first = B_TRUE;
835 836 }
836 837 }
837 838 return (DLADM_STATUS_OK);
838 839 }
839 840
840 841 bzero(&ubuf, sizeof (ubuf));
841 842
842 843 (void) snprintf(ubuf.history_l_flow, sizeof (ubuf.history_l_flow), "%s",
843 844 history->du_name);
844 845 time = history->du_stime;
845 846 (void) strftime(buf, sizeof (buf), "%T", localtime(&time));
846 847 (void) snprintf(ubuf.history_l_stime, sizeof (ubuf.history_l_stime),
847 848 "%s", buf);
848 849 time = history->du_etime;
849 850 (void) strftime(buf, sizeof (buf), "%T", localtime(&time));
850 851 (void) snprintf(ubuf.history_l_etime, sizeof (ubuf.history_l_etime),
851 852 "%s", buf);
852 853 (void) snprintf(ubuf.history_l_rbytes, sizeof (ubuf.history_l_rbytes),
853 854 "%llu", history->du_rbytes);
854 855 (void) snprintf(ubuf.history_l_obytes, sizeof (ubuf.history_l_obytes),
855 856 "%llu", history->du_obytes);
856 857 (void) snprintf(ubuf.history_l_bandwidth,
857 858 sizeof (ubuf.history_l_bandwidth), "%s Mbps",
858 859 dladm_bw2str(history->du_bandwidth, buf));
859 860
860 861 ofmt_print(state->us_ofmt, (void *)&ubuf);
861 862 return (DLADM_STATUS_OK);
862 863 }
863 864
864 865 static int
865 866 show_history_res(dladm_usage_t *history, void *arg)
866 867 {
867 868 show_history_state_t *state = (show_history_state_t *)arg;
868 869 char buf[DLADM_STRSIZE];
869 870 history_fields_buf_t ubuf;
870 871 dladm_flow_attr_t attr;
871 872 dladm_status_t status;
872 873
873 874 /*
874 875 * Only show historical information for existing flows unless '-a'
875 876 * is specified.
876 877 */
877 878 if (!state->us_showall && ((status = dladm_flow_info(handle,
878 879 history->du_name, &attr)) != DLADM_STATUS_OK)) {
879 880 return (status);
880 881 }
881 882
882 883 bzero(&ubuf, sizeof (ubuf));
883 884
884 885 (void) snprintf(ubuf.history_flow, sizeof (ubuf.history_flow), "%s",
885 886 history->du_name);
886 887 (void) snprintf(ubuf.history_duration, sizeof (ubuf.history_duration),
887 888 "%llu", history->du_duration);
888 889 (void) snprintf(ubuf.history_ipackets, sizeof (ubuf.history_ipackets),
889 890 "%llu", history->du_ipackets);
890 891 (void) snprintf(ubuf.history_rbytes, sizeof (ubuf.history_rbytes),
891 892 "%llu", history->du_rbytes);
892 893 (void) snprintf(ubuf.history_opackets, sizeof (ubuf.history_opackets),
893 894 "%llu", history->du_opackets);
894 895 (void) snprintf(ubuf.history_obytes, sizeof (ubuf.history_obytes),
895 896 "%llu", history->du_obytes);
896 897 (void) snprintf(ubuf.history_bandwidth, sizeof (ubuf.history_bandwidth),
897 898 "%s Mbps", dladm_bw2str(history->du_bandwidth, buf));
898 899
899 900 ofmt_print(state->us_ofmt, (void *)&ubuf);
900 901
901 902 return (DLADM_STATUS_OK);
902 903 }
903 904
904 905 static boolean_t
905 906 valid_formatspec(char *formatspec_str)
906 907 {
907 908 return (strcmp(formatspec_str, "gnuplot") == 0);
908 909 }
909 910
910 911 /* ARGSUSED */
911 912 static void
912 913 do_show_history(int argc, char *argv[])
913 914 {
914 915 char *file = NULL;
915 916 int opt;
916 917 dladm_status_t status;
917 918 boolean_t d_arg = B_FALSE;
918 919 char *stime = NULL;
919 920 char *etime = NULL;
920 921 char *resource = NULL;
921 922 show_history_state_t state;
922 923 boolean_t o_arg = B_FALSE;
923 924 boolean_t F_arg = B_FALSE;
924 925 char *fields_str = NULL;
925 926 char *formatspec_str = NULL;
926 927 char *all_fields =
927 928 "flow,duration,ipackets,rbytes,opackets,obytes,bandwidth";
928 929 char *all_l_fields =
929 930 "flow,start,end,rbytes,obytes,bandwidth";
930 931 ofmt_handle_t ofmt;
931 932 ofmt_status_t oferr;
932 933 uint_t ofmtflags = 0;
933 934
934 935 bzero(&state, sizeof (show_history_state_t));
935 936 state.us_parsable = B_FALSE;
936 937 state.us_printheader = B_FALSE;
937 938 state.us_plot = B_FALSE;
938 939 state.us_first = B_TRUE;
939 940
940 941 while ((opt = getopt(argc, argv, "das:e:o:f:F:")) != -1) {
941 942 switch (opt) {
942 943 case 'd':
943 944 d_arg = B_TRUE;
944 945 break;
945 946 case 'a':
946 947 state.us_showall = B_TRUE;
947 948 break;
948 949 case 'f':
949 950 file = optarg;
950 951 break;
951 952 case 's':
952 953 stime = optarg;
953 954 break;
954 955 case 'e':
955 956 etime = optarg;
956 957 break;
957 958 case 'o':
958 959 o_arg = B_TRUE;
959 960 fields_str = optarg;
960 961 break;
961 962 case 'F':
962 963 state.us_plot = F_arg = B_TRUE;
963 964 formatspec_str = optarg;
964 965 break;
965 966 default:
966 967 die_opterr(optopt, opt, usage_ermsg);
967 968 }
968 969 }
969 970
970 971 if (file == NULL)
971 972 die("-h requires a file");
972 973
973 974 if (optind == (argc-1)) {
974 975 dladm_flow_attr_t attr;
975 976
976 977 resource = argv[optind];
977 978 if (!state.us_showall &&
978 979 dladm_flow_info(handle, resource, &attr) !=
979 980 DLADM_STATUS_OK) {
980 981 die("invalid flow: '%s'", resource);
981 982 }
982 983 }
983 984
984 985 if (state.us_parsable)
985 986 ofmtflags |= OFMT_PARSABLE;
986 987 if (resource == NULL && stime == NULL && etime == NULL) {
987 988 if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0))
988 989 fields_str = all_fields;
989 990 oferr = ofmt_open(fields_str, history_fields, ofmtflags,
990 991 0, &ofmt);
991 992 } else {
992 993 if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0))
993 994 fields_str = all_l_fields;
994 995 oferr = ofmt_open(fields_str, history_l_fields, ofmtflags,
995 996 0, &ofmt);
996 997 }
997 998
998 999 flowstat_ofmt_check(oferr, state.us_parsable, ofmt);
999 1000 state.us_ofmt = ofmt;
1000 1001
1001 1002 if (F_arg && d_arg)
1002 1003 die("incompatible -d and -F options");
1003 1004
1004 1005 if (F_arg && !valid_formatspec(formatspec_str))
1005 1006 die("Format specifier %s not supported", formatspec_str);
1006 1007
1007 1008 if (d_arg) {
1008 1009 /* Print log dates */
1009 1010 status = dladm_usage_dates(show_history_date,
1010 1011 DLADM_LOGTYPE_FLOW, file, resource, &state);
1011 1012 } else if (resource == NULL && stime == NULL && etime == NULL &&
1012 1013 !F_arg) {
1013 1014 /* Print summary */
1014 1015 status = dladm_usage_summary(show_history_res,
1015 1016 DLADM_LOGTYPE_FLOW, file, &state);
1016 1017 } else if (resource != NULL) {
1017 1018 /* Print log entries for named resource */
1018 1019 status = dladm_walk_usage_res(show_history_time,
1019 1020 DLADM_LOGTYPE_FLOW, file, resource, stime, etime, &state);
1020 1021 } else {
1021 1022 /* Print time and information for each flow */
1022 1023 status = dladm_walk_usage_time(show_history_time,
1023 1024 DLADM_LOGTYPE_FLOW, file, stime, etime, &state);
1024 1025 }
1025 1026
1026 1027 ofmt_close(ofmt);
1027 1028 if (status != DLADM_STATUS_OK)
1028 1029 die_dlerr(status, "-h");
1029 1030 dladm_close(handle);
1030 1031 }
1031 1032
1032 1033 static void
1033 1034 warn(const char *format, ...)
1034 1035 {
1035 1036 va_list alist;
1036 1037
1037 1038 format = gettext(format);
1038 1039 (void) fprintf(stderr, "%s: warning: ", progname);
1039 1040
1040 1041 va_start(alist, format);
1041 1042 (void) vfprintf(stderr, format, alist);
1042 1043 va_end(alist);
1043 1044
1044 1045 (void) putc('\n', stderr);
1045 1046 }
1046 1047
1047 1048 /* PRINTFLIKE1 */
1048 1049 static void
1049 1050 die(const char *format, ...)
1050 1051 {
1051 1052 va_list alist;
1052 1053
1053 1054 format = gettext(format);
1054 1055 (void) fprintf(stderr, "%s: ", progname);
1055 1056
1056 1057 va_start(alist, format);
1057 1058 (void) vfprintf(stderr, format, alist);
1058 1059 va_end(alist);
1059 1060
1060 1061 (void) putc('\n', stderr);
1061 1062
1062 1063 /* close dladm handle if it was opened */
1063 1064 if (handle != NULL)
1064 1065 dladm_close(handle);
1065 1066
1066 1067 exit(EXIT_FAILURE);
1067 1068 }
1068 1069
1069 1070 static void
↓ open down ↓ |
860 lines elided |
↑ open up ↑ |
1070 1071 die_optdup(int opt)
1071 1072 {
1072 1073 die("the option -%c cannot be specified more than once", opt);
1073 1074 }
1074 1075
1075 1076 static void
1076 1077 die_opterr(int opt, int opterr, const char *usage)
1077 1078 {
1078 1079 switch (opterr) {
1079 1080 case ':':
1080 - die("option '-%c' requires a value\nusage: %s", opt,
1081 + die("option '-%c' requires a value\n%s", opt,
1081 1082 gettext(usage));
1082 1083 break;
1083 1084 case '?':
1084 1085 default:
1085 - die("unrecognized option '-%c'\nusage: %s", opt,
1086 + die("unrecognized option '-%c'\n%s", opt,
1086 1087 gettext(usage));
1087 1088 break;
1088 1089 }
1089 1090 }
1090 1091
1091 1092 /* PRINTFLIKE2 */
1092 1093 static void
1093 1094 die_dlerr(dladm_status_t err, const char *format, ...)
1094 1095 {
1095 1096 va_list alist;
1096 1097 char errmsg[DLADM_STRSIZE];
1097 1098
1098 1099 format = gettext(format);
1099 1100 (void) fprintf(stderr, "%s: ", progname);
1100 1101
1101 1102 va_start(alist, format);
1102 1103 (void) vfprintf(stderr, format, alist);
1103 1104 va_end(alist);
1104 1105 (void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
1105 1106
1106 1107 /* close dladm handle if it was opened */
1107 1108 if (handle != NULL)
1108 1109 dladm_close(handle);
1109 1110
1110 1111 exit(EXIT_FAILURE);
1111 1112 }
1112 1113
1113 1114
1114 1115 /*
1115 1116 * default output callback function that, when invoked from dladm_print_output,
1116 1117 * prints string which is offset by of_arg->ofmt_id within buf.
1117 1118 */
1118 1119 static boolean_t
1119 1120 print_default_cb(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
1120 1121 {
1121 1122 char *value;
1122 1123
1123 1124 value = (char *)of_arg->ofmt_cbarg + of_arg->ofmt_id;
1124 1125 (void) strlcpy(buf, value, bufsize);
1125 1126 return (B_TRUE);
1126 1127 }
1127 1128
1128 1129 static void
1129 1130 flowstat_ofmt_check(ofmt_status_t oferr, boolean_t parsable,
1130 1131 ofmt_handle_t ofmt)
1131 1132 {
1132 1133 char buf[OFMT_BUFSIZE];
1133 1134
1134 1135 if (oferr == OFMT_SUCCESS)
1135 1136 return;
1136 1137 (void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf));
1137 1138 /*
1138 1139 * All errors are considered fatal in parsable mode.
1139 1140 * NOMEM errors are always fatal, regardless of mode.
1140 1141 * For other errors, we print diagnostics in human-readable
1141 1142 * mode and processs what we can.
1142 1143 */
1143 1144 if (parsable || oferr == OFMT_ENOFIELDS) {
1144 1145 ofmt_close(ofmt);
1145 1146 die(buf);
1146 1147 } else {
1147 1148 warn(buf);
1148 1149 }
1149 1150 }
↓ open down ↓ |
54 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX