4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2016 Nexenta Systems, Inc.
47 #include <auth_attr.h>
48 #include <auth_list.h>
50 #include <libdevinfo.h>
53 #include <libdllink.h>
54 #include <libdlstat.h>
55 #include <libdlaggr.h>
56 #include <libdlwlan.h>
57 #include <libdlvlan.h>
58 #include <libdlvnic.h>
60 #include <libdlether.h>
61 #include <libdliptun.h>
63 #include <libdlbridge.h>
64 #include <libinetutil.h>
65 #include <libvrrpadm.h>
66 #include <libdlvnic.h>
67 #include <sys/types.h>
68 #include <sys/socket.h>
69 #include <sys/ib/ib_types.h>
70 #include <sys/processor.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
73 #include <net/if_types.h>
80 #define BUFLEN(lim, ptr) (((lim) > (ptr)) ? ((lim) - (ptr)) : 0)
81 #define MAXLINELEN 1024
82 #define SMF_UPGRADE_FILE "/var/svc/profile/upgrade"
83 #define SMF_UPGRADEDATALINK_FILE "/var/svc/profile/upgrade_datalink"
84 #define SMF_DLADM_UPGRADE_MSG " # added by dladm(8)"
85 #define DLADM_DEFAULT_COL 80
88 * used by the wifi show-* commands to set up ofmt_field_t structures.
90 #define WIFI_CMD_SCAN 0x00000001
91 #define WIFI_CMD_SHOW 0x00000002
92 #define WIFI_CMD_ALL (WIFI_CMD_SCAN | WIFI_CMD_SHOW)
94 /* No larger than pktsum_t */
95 typedef struct brsum_s
{
104 /* No larger than pktsum_t */
105 typedef struct brlsum_s
{
115 typedef struct show_state
{
116 boolean_t ls_firstonly
;
117 boolean_t ls_donefirst
;
118 pktsum_t ls_prevstats
;
120 dladm_status_t ls_status
;
121 ofmt_handle_t ls_ofmt
;
122 boolean_t ls_parsable
;
127 typedef struct show_grp_state
{
128 pktsum_t gs_prevstats
[MAXPORT
];
130 dladm_status_t gs_status
;
131 boolean_t gs_parsable
;
133 boolean_t gs_extended
;
135 boolean_t gs_firstonly
;
136 boolean_t gs_donefirst
;
137 ofmt_handle_t gs_ofmt
;
140 typedef struct show_vnic_state
{
141 datalink_id_t vs_vnic_id
;
142 datalink_id_t vs_link_id
;
143 char vs_vnic
[MAXLINKNAMELEN
];
144 char vs_link
[MAXLINKNAMELEN
];
145 boolean_t vs_parsable
;
147 boolean_t vs_firstonly
;
148 boolean_t vs_donefirst
;
150 boolean_t vs_printstats
;
151 pktsum_t vs_totalstats
;
152 pktsum_t vs_prevstats
[MAXVNIC
];
153 boolean_t vs_etherstub
;
154 dladm_status_t vs_status
;
156 ofmt_handle_t vs_ofmt
;
159 typedef struct show_part_state
{
160 datalink_id_t ps_over_id
;
161 char ps_part
[MAXLINKNAMELEN
];
162 boolean_t ps_parsable
;
164 dladm_status_t ps_status
;
166 ofmt_handle_t ps_ofmt
;
169 typedef struct show_ib_state
{
170 datalink_id_t is_link_id
;
171 char is_link
[MAXLINKNAMELEN
];
172 boolean_t is_parsable
;
173 dladm_status_t is_status
;
175 ofmt_handle_t is_ofmt
;
178 typedef struct show_usage_state_s
{
180 boolean_t us_parsable
;
181 boolean_t us_printheader
;
183 boolean_t us_showall
;
184 ofmt_handle_t us_ofmt
;
185 } show_usage_state_t
;
188 * callback functions for printing output and error diagnostics.
190 static ofmt_cb_t print_default_cb
, print_link_stats_cb
, print_linkprop_cb
;
191 static ofmt_cb_t print_lacp_cb
, print_phys_one_mac_cb
;
192 static ofmt_cb_t print_xaggr_cb
, print_aggr_stats_cb
;
193 static ofmt_cb_t print_phys_one_hwgrp_cb
, print_wlan_attr_cb
;
194 static ofmt_cb_t print_wifi_status_cb
, print_link_attr_cb
;
196 typedef void cmdfunc_t(int, char **, const char *);
198 static cmdfunc_t do_show_link
, do_show_wifi
, do_show_phys
;
199 static cmdfunc_t do_create_aggr
, do_delete_aggr
, do_add_aggr
, do_remove_aggr
;
200 static cmdfunc_t do_modify_aggr
, do_show_aggr
, do_up_aggr
;
201 static cmdfunc_t do_scan_wifi
, do_connect_wifi
, do_disconnect_wifi
;
202 static cmdfunc_t do_show_linkprop
, do_set_linkprop
, do_reset_linkprop
;
203 static cmdfunc_t do_create_secobj
, do_delete_secobj
, do_show_secobj
;
204 static cmdfunc_t do_init_linkprop
, do_init_secobj
;
205 static cmdfunc_t do_create_vlan
, do_delete_vlan
, do_up_vlan
, do_show_vlan
;
206 static cmdfunc_t do_rename_link
, do_delete_phys
, do_init_phys
;
207 static cmdfunc_t do_show_linkmap
;
208 static cmdfunc_t do_show_ether
;
209 static cmdfunc_t do_create_vnic
, do_delete_vnic
, do_show_vnic
;
210 static cmdfunc_t do_up_vnic
;
211 static cmdfunc_t do_create_part
, do_delete_part
, do_show_part
, do_show_ib
;
212 static cmdfunc_t do_up_part
;
213 static cmdfunc_t do_create_etherstub
, do_delete_etherstub
, do_show_etherstub
;
214 static cmdfunc_t do_create_simnet
, do_modify_simnet
;
215 static cmdfunc_t do_delete_simnet
, do_show_simnet
, do_up_simnet
;
216 static cmdfunc_t do_show_usage
;
217 static cmdfunc_t do_create_bridge
, do_modify_bridge
, do_delete_bridge
;
218 static cmdfunc_t do_add_bridge
, do_remove_bridge
, do_show_bridge
;
219 static cmdfunc_t do_create_iptun
, do_modify_iptun
, do_delete_iptun
;
220 static cmdfunc_t do_show_iptun
, do_up_iptun
, do_down_iptun
;
222 static void do_up_vnic_common(int, char **, const char *, boolean_t
);
224 static int show_part(dladm_handle_t
, datalink_id_t
, void *);
226 static void altroot_cmd(char *, int, char **);
227 static int show_linkprop_onelink(dladm_handle_t
, datalink_id_t
, void *);
229 static void link_stats(datalink_id_t
, uint_t
, char *, show_state_t
*);
230 static void aggr_stats(datalink_id_t
, show_grp_state_t
*, uint_t
);
231 static void vnic_stats(show_vnic_state_t
*, uint32_t);
233 static int get_one_kstat(const char *, const char *, uint8_t,
235 static void get_mac_stats(const char *, pktsum_t
*);
236 static void get_link_stats(const char *, pktsum_t
*);
237 static uint64_t get_ifspeed(const char *, boolean_t
);
238 static const char *get_linkstate(const char *, boolean_t
, char *);
239 static const char *get_linkduplex(const char *, boolean_t
, char *);
241 static iptun_type_t
iptun_gettypebyname(char *);
242 static const char *iptun_gettypebyvalue(iptun_type_t
);
243 static dladm_status_t
print_iptun(dladm_handle_t
, datalink_id_t
,
245 static int print_iptun_walker(dladm_handle_t
, datalink_id_t
, void *);
247 static int show_etherprop(dladm_handle_t
, datalink_id_t
, void *);
248 static void show_ether_xprop(void *, dladm_ether_info_t
*);
249 static boolean_t
link_is_ether(const char *, datalink_id_t
*);
251 static boolean_t
str2int(const char *, int *);
252 static void die(const char *, ...);
253 static void die_optdup(int);
254 static void die_opterr(int, int, const char *);
255 static void die_dlerr(dladm_status_t
, const char *, ...);
256 static void warn(const char *, ...);
257 static void warn_dlerr(dladm_status_t
, const char *, ...);
265 static cmd_t cmds
[] = {
266 { "rename-link", do_rename_link
,
267 " rename-link <oldlink> <newlink>" },
268 { "show-link", do_show_link
,
269 " show-link [-pP] [-o <field>,..] [-s [-i <interval>]] "
271 { "create-aggr", do_create_aggr
,
272 " create-aggr [-t] [-P <policy>] [-L <mode>] [-T <time>] "
274 "\t\t -l <link> [-l <link>...] <link>" },
275 { "delete-aggr", do_delete_aggr
,
276 " delete-aggr [-t] <link>" },
277 { "add-aggr", do_add_aggr
,
278 " add-aggr [-t] -l <link> [-l <link>...] <link>" },
279 { "remove-aggr", do_remove_aggr
,
280 " remove-aggr [-t] -l <link> [-l <link>...] <link>" },
281 { "modify-aggr", do_modify_aggr
,
282 " modify-aggr [-t] [-P <policy>] [-L <mode>] [-T <time>] "
285 { "show-aggr", do_show_aggr
,
286 " show-aggr [-pPLx] [-o <field>,..] [-s [-i <interval>]] "
288 { "up-aggr", do_up_aggr
, NULL
},
289 { "scan-wifi", do_scan_wifi
,
290 " scan-wifi [-p] [-o <field>,...] [<link>]" },
291 { "connect-wifi", do_connect_wifi
,
292 " connect-wifi [-e <essid>] [-i <bssid>] [-k <key>,...] "
294 "\t\t [-a open|shared] [-b bss|ibss] [-c] [-m a|b|g] "
297 { "disconnect-wifi", do_disconnect_wifi
,
298 " disconnect-wifi [-a] [<link>]" },
299 { "show-wifi", do_show_wifi
,
300 " show-wifi [-p] [-o <field>,...] [<link>]\n" },
301 { "set-linkprop", do_set_linkprop
,
302 " set-linkprop [-t] -p <prop>=<value>[,...] <name>" },
303 { "reset-linkprop", do_reset_linkprop
,
304 " reset-linkprop [-t] [-p <prop>,...] <name>" },
305 { "show-linkprop", do_show_linkprop
,
306 " show-linkprop [-cP] [-o <field>,...] [-p <prop>,...] "
308 { "show-ether", do_show_ether
,
309 " show-ether [-px][-o <field>,...] <link>\n" },
310 { "create-secobj", do_create_secobj
,
311 " create-secobj [-t] [-f <file>] -c <class> <secobj>" },
312 { "delete-secobj", do_delete_secobj
,
313 " delete-secobj [-t] <secobj>[,...]" },
314 { "show-secobj", do_show_secobj
,
315 " show-secobj [-pP] [-o <field>,...] [<secobj>,...]\n" },
316 { "init-linkprop", do_init_linkprop
, NULL
},
317 { "init-secobj", do_init_secobj
, NULL
},
318 { "create-vlan", do_create_vlan
,
319 " create-vlan [-ft] -l <link> -v <vid> [link]" },
320 { "delete-vlan", do_delete_vlan
,
321 " delete-vlan [-t] <link>" },
322 { "show-vlan", do_show_vlan
,
323 " show-vlan [-pP] [-o <field>,..] [<link>]\n" },
324 { "up-vlan", do_up_vlan
, NULL
},
325 { "create-iptun", do_create_iptun
,
326 " create-iptun [-t] -T <type> "
327 "[-a {local|remote}=<addr>,...] <link>]" },
328 { "delete-iptun", do_delete_iptun
,
329 " delete-iptun [-t] <link>" },
330 { "modify-iptun", do_modify_iptun
,
331 " modify-iptun [-t] -a {local|remote}=<addr>,... <link>" },
332 { "show-iptun", do_show_iptun
,
333 " show-iptun [-pP] [-o <field>,..] [<link>]\n" },
334 { "up-iptun", do_up_iptun
, NULL
},
335 { "down-iptun", do_down_iptun
, NULL
},
336 { "delete-phys", do_delete_phys
,
337 " delete-phys <link>" },
338 { "show-phys", do_show_phys
,
339 " show-phys [-m | -H | -P] [[-p] [-o <field>[,...]] "
341 { "init-phys", do_init_phys
, NULL
},
342 { "show-linkmap", do_show_linkmap
, NULL
},
343 { "create-vnic", do_create_vnic
,
344 " create-vnic [-t] -l <link> [-m <value> | auto |\n"
345 "\t\t {factory [-n <slot-id>]} | {random [-r <prefix>]} |\n"
346 "\t\t {vrrp -V <vrid> -A {inet | inet6}} [-v <vid> [-f]]\n"
347 "\t\t [-p <prop>=<value>[,...]] <vnic-link>" },
348 { "delete-vnic", do_delete_vnic
,
349 " delete-vnic [-t] <vnic-link>" },
350 { "show-vnic", do_show_vnic
,
351 " show-vnic [-pP] [-l <link>] [-s [-i <interval>]] "
353 { "up-vnic", do_up_vnic
, NULL
},
354 { "create-part", do_create_part
,
355 " create-part [-t] [-f] -l <link> [-P <pkey>]\n"
356 "\t\t [-R <root-dir>] <part-link>" },
357 { "delete-part", do_delete_part
,
358 " delete-part [-t] [-R <root-dir>] <part-link>"},
359 { "show-part", do_show_part
,
360 " show-part [-pP] [-o <field>,...][-l <linkover>]\n"
361 "\t\t [<part-link>]" },
362 { "show-ib", do_show_ib
,
363 " show-ib [-p] [-o <field>,...] [<link>]\n" },
364 { "up-part", do_up_part
, NULL
},
365 { "create-etherstub", do_create_etherstub
,
366 " create-etherstub [-t] <link>" },
367 { "delete-etherstub", do_delete_etherstub
,
368 " delete-etherstub [-t] <link>" },
369 { "show-etherstub", do_show_etherstub
,
370 " show-etherstub [-t] [<link>]\n" },
371 { "create-simnet", do_create_simnet
, NULL
},
372 { "modify-simnet", do_modify_simnet
, NULL
},
373 { "delete-simnet", do_delete_simnet
, NULL
},
374 { "show-simnet", do_show_simnet
, NULL
},
375 { "up-simnet", do_up_simnet
, NULL
},
376 { "create-bridge", do_create_bridge
,
377 " create-bridge [-R <root-dir>] [-P <protect>] "
379 "\t\t [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
380 "\t\t [-f <force-protocol>] [-l <link>]... <bridge>" },
381 { "modify-bridge", do_modify_bridge
,
382 " modify-bridge [-R <root-dir>] [-P <protect>] "
384 "\t\t [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
385 "\t\t [-f <force-protocol>] <bridge>" },
386 { "delete-bridge", do_delete_bridge
,
387 " delete-bridge [-R <root-dir>] <bridge>" },
388 { "add-bridge", do_add_bridge
,
389 " add-bridge [-R <root-dir>] -l <link> [-l <link>]... "
391 { "remove-bridge", do_remove_bridge
,
392 " remove-bridge [-R <root-dir>] -l <link> [-l <link>]... "
394 { "show-bridge", do_show_bridge
,
395 " show-bridge [-p] [-o <field>,...] [-s [-i <interval>]] "
397 " show-bridge -l [-p] [-o <field>,...] [-s [-i <interval>]]"
399 " show-bridge -f [-p] [-o <field>,...] [-s [-i <interval>]]"
401 " show-bridge -t [-p] [-o <field>,...] [-s [-i <interval>]]"
403 { "show-usage", do_show_usage
,
404 " show-usage [-a] [-d | -F <format>] "
405 "[-s <DD/MM/YYYY,HH:MM:SS>]\n"
406 "\t\t [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<link>]" }
409 static const struct option lopts
[] = {
410 {"vlan-id", required_argument
, 0, 'v'},
411 {"output", required_argument
, 0, 'o'},
412 {"dev", required_argument
, 0, 'd'},
413 {"policy", required_argument
, 0, 'P'},
414 {"lacp-mode", required_argument
, 0, 'L'},
415 {"lacp-timer", required_argument
, 0, 'T'},
416 {"unicast", required_argument
, 0, 'u'},
417 {"temporary", no_argument
, 0, 't'},
418 {"root-dir", required_argument
, 0, 'R'},
419 {"link", required_argument
, 0, 'l'},
420 {"forcible", no_argument
, 0, 'f'},
421 {"bw-limit", required_argument
, 0, 'b'},
422 {"mac-address", required_argument
, 0, 'm'},
423 {"slot", required_argument
, 0, 'n'},
427 static const struct option show_lopts
[] = {
428 {"statistics", no_argument
, 0, 's'},
429 {"continuous", no_argument
, 0, 'S'},
430 {"interval", required_argument
, 0, 'i'},
431 {"parsable", no_argument
, 0, 'p'},
432 {"parseable", no_argument
, 0, 'p'},
433 {"extended", no_argument
, 0, 'x'},
434 {"output", required_argument
, 0, 'o'},
435 {"persistent", no_argument
, 0, 'P'},
436 {"lacp", no_argument
, 0, 'L'},
440 static const struct option iptun_lopts
[] = {
441 {"output", required_argument
, 0, 'o'},
442 {"tunnel-type", required_argument
, 0, 'T'},
443 {"address", required_argument
, 0, 'a'},
444 {"root-dir", required_argument
, 0, 'R'},
445 {"parsable", no_argument
, 0, 'p'},
446 {"parseable", no_argument
, 0, 'p'},
447 {"persistent", no_argument
, 0, 'P'},
451 static char * const iptun_addropts
[] = {
452 #define IPTUN_LOCAL 0
454 #define IPTUN_REMOTE 1
458 static const struct {
459 const char *type_name
;
460 iptun_type_t type_value
;
462 {"ipv4", IPTUN_TYPE_IPV4
},
463 {"ipv6", IPTUN_TYPE_IPV6
},
464 {"6to4", IPTUN_TYPE_6TO4
},
468 static const struct option prop_longopts
[] = {
469 {"temporary", no_argument
, 0, 't' },
470 {"output", required_argument
, 0, 'o' },
471 {"root-dir", required_argument
, 0, 'R' },
472 {"prop", required_argument
, 0, 'p' },
473 {"parsable", no_argument
, 0, 'c' },
474 {"parseable", no_argument
, 0, 'c' },
475 {"persistent", no_argument
, 0, 'P' },
479 static const struct option wifi_longopts
[] = {
480 {"parsable", no_argument
, 0, 'p' },
481 {"parseable", no_argument
, 0, 'p' },
482 {"output", required_argument
, 0, 'o' },
483 {"essid", required_argument
, 0, 'e' },
484 {"bsstype", required_argument
, 0, 'b' },
485 {"mode", required_argument
, 0, 'm' },
486 {"key", required_argument
, 0, 'k' },
487 {"sec", required_argument
, 0, 's' },
488 {"auth", required_argument
, 0, 'a' },
489 {"create-ibss", required_argument
, 0, 'c' },
490 {"timeout", required_argument
, 0, 'T' },
491 {"all-links", no_argument
, 0, 'a' },
492 {"temporary", no_argument
, 0, 't' },
493 {"root-dir", required_argument
, 0, 'R' },
494 {"persistent", no_argument
, 0, 'P' },
495 {"file", required_argument
, 0, 'f' },
499 static const struct option showeth_lopts
[] = {
500 {"parsable", no_argument
, 0, 'p' },
501 {"parseable", no_argument
, 0, 'p' },
502 {"extended", no_argument
, 0, 'x' },
503 {"output", required_argument
, 0, 'o' },
507 static const struct option vnic_lopts
[] = {
508 {"temporary", no_argument
, 0, 't' },
509 {"root-dir", required_argument
, 0, 'R' },
510 {"dev", required_argument
, 0, 'd' },
511 {"mac-address", required_argument
, 0, 'm' },
512 {"cpus", required_argument
, 0, 'c' },
513 {"bw-limit", required_argument
, 0, 'b' },
514 {"slot", required_argument
, 0, 'n' },
515 {"mac-prefix", required_argument
, 0, 'r' },
516 {"vrid", required_argument
, 0, 'V' },
517 {"address-family", required_argument
, 0, 'A' },
521 static const struct option part_lopts
[] = {
522 {"temporary", no_argument
, 0, 't' },
523 {"pkey", required_argument
, 0, 'P' },
524 {"link", required_argument
, 0, 'l' },
525 {"force", no_argument
, 0, 'f' },
526 {"root-dir", required_argument
, 0, 'R' },
527 {"prop", required_argument
, 0, 'p' },
531 static const struct option show_part_lopts
[] = {
532 {"parsable", no_argument
, 0, 'p' },
533 {"parseable", no_argument
, 0, 'p' },
534 {"link", required_argument
, 0, 'l' },
535 {"persistent", no_argument
, 0, 'P' },
536 {"output", required_argument
, 0, 'o' },
540 static const struct option etherstub_lopts
[] = {
541 {"temporary", no_argument
, 0, 't' },
542 {"root-dir", required_argument
, 0, 'R' },
546 static const struct option usage_opts
[] = {
547 {"file", required_argument
, 0, 'f' },
548 {"format", required_argument
, 0, 'F' },
549 {"start", required_argument
, 0, 's' },
550 {"stop", required_argument
, 0, 'e' },
554 static const struct option simnet_lopts
[] = {
555 {"temporary", no_argument
, 0, 't' },
556 {"root-dir", required_argument
, 0, 'R' },
557 {"media", required_argument
, 0, 'm' },
558 {"peer", required_argument
, 0, 'p' },
562 static const struct option bridge_lopts
[] = {
563 { "protect", required_argument
, 0, 'P' },
564 { "root-dir", required_argument
, 0, 'R' },
565 { "forward-delay", required_argument
, 0, 'd' },
566 { "force-protocol", required_argument
, 0, 'f' },
567 { "hello-time", required_argument
, 0, 'h' },
568 { "link", required_argument
, 0, 'l' },
569 { "max-age", required_argument
, 0, 'm' },
570 { "priority", required_argument
, 0, 'p' },
574 static const struct option bridge_show_lopts
[] = {
575 { "forwarding", no_argument
, 0, 'f' },
576 { "interval", required_argument
, 0, 'i' },
577 { "link", no_argument
, 0, 'l' },
578 { "output", required_argument
, 0, 'o' },
579 { "parsable", no_argument
, 0, 'p' },
580 { "parseable", no_argument
, 0, 'p' },
581 { "statistics", no_argument
, 0, 's' },
582 { "trill", no_argument
, 0, 't' },
587 * structures for 'dladm show-ether'
589 static const char *ptype
[] = {LEI_ATTR_NAMES
};
591 typedef struct ether_fields_buf_s
599 char eth_rem_fault
[16];
600 } ether_fields_buf_t
;
602 static const ofmt_field_t ether_fields
[] = {
603 /* name, field width, offset callback */
605 offsetof(ether_fields_buf_t
, eth_link
), print_default_cb
},
607 offsetof(ether_fields_buf_t
, eth_ptype
), print_default_cb
},
609 offsetof(ether_fields_buf_t
, eth_state
),
612 offsetof(ether_fields_buf_t
, eth_autoneg
), print_default_cb
},
613 { "SPEED-DUPLEX", 32,
614 offsetof(ether_fields_buf_t
, eth_spdx
), print_default_cb
},
616 offsetof(ether_fields_buf_t
, eth_pause
), print_default_cb
},
618 offsetof(ether_fields_buf_t
, eth_rem_fault
), print_default_cb
},
623 typedef struct print_ether_state
{
625 boolean_t es_parsable
;
627 boolean_t es_extended
;
628 ofmt_handle_t es_ofmt
;
629 } print_ether_state_t
;
632 * structures for 'dladm show-link -s' (print statistics)
642 } link_s_field_index_t
;
644 static const ofmt_field_t link_s_fields
[] = {
645 /* name, field width, index, callback */
646 { "LINK", 15, LINK_S_LINK
, print_link_stats_cb
},
647 { "IPACKETS", 10, LINK_S_IPKTS
, print_link_stats_cb
},
648 { "RBYTES", 8, LINK_S_RBYTES
, print_link_stats_cb
},
649 { "IERRORS", 10, LINK_S_IERRORS
, print_link_stats_cb
},
650 { "OPACKETS", 12, LINK_S_OPKTS
, print_link_stats_cb
},
651 { "OBYTES", 12, LINK_S_OBYTES
, print_link_stats_cb
},
652 { "OERRORS", 8, LINK_S_OERRORS
, print_link_stats_cb
}}
655 typedef struct link_args_s
{
657 pktsum_t
*link_s_psum
;
661 * buffer used by print functions for show-{link,phys,vlan} commands.
663 typedef struct link_fields_buf_s
{
664 char link_name
[MAXLINKNAMELEN
];
665 char link_class
[DLADM_STRSIZE
];
667 char link_state
[DLADM_STRSIZE
];
668 char link_bridge
[MAXLINKNAMELEN
* MAXPORT
];
669 char link_over
[MAXLINKNAMELEN
* MAXPORT
];
670 char link_phys_state
[DLADM_STRSIZE
];
671 char link_phys_media
[DLADM_STRSIZE
];
672 char link_phys_speed
[DLADM_STRSIZE
];
673 char link_phys_duplex
[DLPI_LINKNAME_MAX
];
674 char link_phys_device
[DLPI_LINKNAME_MAX
];
676 char link_vlan_vid
[6];
680 * structures for 'dladm show-link'
682 static const ofmt_field_t link_fields
[] = {
683 /* name, field width, index, callback */
685 offsetof(link_fields_buf_t
, link_name
), print_default_cb
},
687 offsetof(link_fields_buf_t
, link_class
), print_default_cb
},
689 offsetof(link_fields_buf_t
, link_mtu
), print_default_cb
},
691 offsetof(link_fields_buf_t
, link_state
), print_default_cb
},
693 offsetof(link_fields_buf_t
, link_bridge
), print_default_cb
},
695 offsetof(link_fields_buf_t
, link_over
), print_default_cb
},
700 * structures for 'dladm show-aggr'
702 typedef struct laggr_fields_buf_s
{
703 char laggr_name
[DLPI_LINKNAME_MAX
];
704 char laggr_policy
[9];
705 char laggr_addrpolicy
[ETHERADDRL
* 3 + 3];
706 char laggr_lacpactivity
[14];
707 char laggr_lacptimer
[DLADM_STRSIZE
];
709 } laggr_fields_buf_t
;
711 typedef struct laggr_args_s
{
712 int laggr_lport
; /* -1 indicates the aggr itself */
713 const char *laggr_link
;
714 dladm_aggr_grp_attr_t
*laggr_ginfop
;
715 dladm_status_t
*laggr_status
;
716 pktsum_t
*laggr_pktsumtot
; /* -s only */
717 pktsum_t
*laggr_diffstats
; /* -s only */
718 boolean_t laggr_parsable
;
721 static const ofmt_field_t laggr_fields
[] = {
722 /* name, field width, offset, callback */
724 offsetof(laggr_fields_buf_t
, laggr_name
), print_default_cb
},
726 offsetof(laggr_fields_buf_t
, laggr_policy
), print_default_cb
},
727 { "ADDRPOLICY", ETHERADDRL
* 3 + 3,
728 offsetof(laggr_fields_buf_t
, laggr_addrpolicy
), print_default_cb
},
729 { "LACPACTIVITY", 14,
730 offsetof(laggr_fields_buf_t
, laggr_lacpactivity
), print_default_cb
},
732 offsetof(laggr_fields_buf_t
, laggr_lacptimer
), print_default_cb
},
734 offsetof(laggr_fields_buf_t
, laggr_flags
), print_default_cb
},
739 * structures for 'dladm show-aggr -x'.
749 } aggr_x_field_index_t
;
751 static const ofmt_field_t aggr_x_fields
[] = {
752 /* name, field width, index callback */
753 { "LINK", 12, AGGR_X_LINK
, print_xaggr_cb
},
754 { "PORT", 15, AGGR_X_PORT
, print_xaggr_cb
},
755 { "SPEED", 5, AGGR_X_SPEED
, print_xaggr_cb
},
756 { "DUPLEX", 10, AGGR_X_DUPLEX
, print_xaggr_cb
},
757 { "STATE", 10, AGGR_X_STATE
, print_xaggr_cb
},
758 { "ADDRESS", 19, AGGR_X_ADDRESS
, print_xaggr_cb
},
759 { "PORTSTATE", 16, AGGR_X_PORTSTATE
, print_xaggr_cb
},
764 * structures for 'dladm show-aggr -s'.
775 } aggr_s_field_index_t
;
777 static const ofmt_field_t aggr_s_fields
[] = {
778 { "LINK", 12, AGGR_S_LINK
, print_aggr_stats_cb
},
779 { "PORT", 10, AGGR_S_PORT
, print_aggr_stats_cb
},
780 { "IPACKETS", 8, AGGR_S_IPKTS
, print_aggr_stats_cb
},
781 { "RBYTES", 8, AGGR_S_RBYTES
, print_aggr_stats_cb
},
782 { "OPACKETS", 8, AGGR_S_OPKTS
, print_aggr_stats_cb
},
783 { "OBYTES", 8, AGGR_S_OBYTES
, print_aggr_stats_cb
},
784 { "IPKTDIST", 9, AGGR_S_IPKTDIST
, print_aggr_stats_cb
},
785 { "OPKTDIST", 15, AGGR_S_OPKTDIST
, print_aggr_stats_cb
},
790 * structures for 'dladm show-aggr -L'.
801 } aggr_l_field_index_t
;
803 static const ofmt_field_t aggr_l_fields
[] = {
804 /* name, field width, index */
805 { "LINK", 12, AGGR_L_LINK
, print_lacp_cb
},
806 { "PORT", 13, AGGR_L_PORT
, print_lacp_cb
},
807 { "AGGREGATABLE", 13, AGGR_L_AGGREGATABLE
, print_lacp_cb
},
808 { "SYNC", 5, AGGR_L_SYNC
, print_lacp_cb
},
809 { "COLL", 5, AGGR_L_COLL
, print_lacp_cb
},
810 { "DIST", 5, AGGR_L_DIST
, print_lacp_cb
},
811 { "DEFAULTED", 10, AGGR_L_DEFAULTED
, print_lacp_cb
},
812 { "EXPIRED", 15, AGGR_L_EXPIRED
, print_lacp_cb
},
817 * structures for 'dladm show-phys'
820 static const ofmt_field_t phys_fields
[] = {
821 /* name, field width, offset */
823 offsetof(link_fields_buf_t
, link_name
), print_default_cb
},
825 offsetof(link_fields_buf_t
, link_phys_media
), print_default_cb
},
827 offsetof(link_fields_buf_t
, link_phys_state
), print_default_cb
},
829 offsetof(link_fields_buf_t
, link_phys_speed
), print_default_cb
},
831 offsetof(link_fields_buf_t
, link_phys_duplex
), print_default_cb
},
833 offsetof(link_fields_buf_t
, link_phys_device
), print_default_cb
},
835 offsetof(link_fields_buf_t
, link_flags
), print_default_cb
},
840 * structures for 'dladm show-phys -m'
849 } phys_m_field_index_t
;
851 static const ofmt_field_t phys_m_fields
[] = {
852 /* name, field width, offset */
853 { "LINK", 13, PHYS_M_LINK
, print_phys_one_mac_cb
},
854 { "SLOT", 9, PHYS_M_SLOT
, print_phys_one_mac_cb
},
855 { "ADDRESS", 19, PHYS_M_ADDRESS
, print_phys_one_mac_cb
},
856 { "INUSE", 5, PHYS_M_INUSE
, print_phys_one_mac_cb
},
857 { "CLIENT", 13, PHYS_M_CLIENT
, print_phys_one_mac_cb
},
862 * structures for 'dladm show-phys -H'
870 } phys_h_field_index_t
;
872 #define RINGSTRLEN 21
874 static const ofmt_field_t phys_h_fields
[] = {
875 { "LINK", 13, PHYS_H_LINK
, print_phys_one_hwgrp_cb
},
876 { "RINGTYPE", 9, PHYS_H_RINGTYPE
, print_phys_one_hwgrp_cb
},
877 { "RINGS", RINGSTRLEN
, PHYS_H_RINGS
, print_phys_one_hwgrp_cb
},
878 { "CLIENTS", 24, PHYS_H_CLIENTS
, print_phys_one_hwgrp_cb
},
883 * structures for 'dladm show-vlan'
885 static const ofmt_field_t vlan_fields
[] = {
887 offsetof(link_fields_buf_t
, link_name
), print_default_cb
},
889 offsetof(link_fields_buf_t
, link_vlan_vid
), print_default_cb
},
891 offsetof(link_fields_buf_t
, link_over
), print_default_cb
},
893 offsetof(link_fields_buf_t
, link_flags
), print_default_cb
},
898 * structures common to 'dladm scan-wifi' and 'dladm show-wifi'
899 * callback will be determined in parse_wifi_fields.
901 static ofmt_field_t wifi_common_fields
[] = {
902 { "LINK", 11, 0, NULL
},
903 { "ESSID", 20, DLADM_WLAN_ATTR_ESSID
, NULL
},
904 { "BSSID", 18, DLADM_WLAN_ATTR_BSSID
, NULL
},
905 { "IBSSID", 18, DLADM_WLAN_ATTR_BSSID
, NULL
},
906 { "MODE", 7, DLADM_WLAN_ATTR_MODE
, NULL
},
907 { "SPEED", 7, DLADM_WLAN_ATTR_SPEED
, NULL
},
908 { "BSSTYPE", 9, DLADM_WLAN_ATTR_BSSTYPE
, NULL
},
909 { "SEC", 7, DLADM_WLAN_ATTR_SECMODE
, NULL
},
910 { "STRENGTH", 11, DLADM_WLAN_ATTR_STRENGTH
, NULL
},
911 { NULL
, 0, 0, NULL
}};
914 * the 'show-wifi' command supports all the fields in wifi_common_fields
915 * plus the AUTH and STATUS fields.
917 static ofmt_field_t wifi_show_fields
[A_CNT(wifi_common_fields
) + 2] = {
918 { "AUTH", 9, DLADM_WLAN_ATTR_AUTH
, NULL
},
919 { "STATUS", 18, DLADM_WLAN_LINKATTR_STATUS
, print_wifi_status_cb
},
920 /* copy wifi_common_fields here */
923 static char *all_scan_wifi_fields
=
924 "link,essid,bssid,sec,strength,mode,speed,bsstype";
925 static char *all_show_wifi_fields
=
926 "link,status,essid,sec,strength,mode,speed,auth,bssid,bsstype";
927 static char *def_scan_wifi_fields
=
928 "link,essid,bssid,sec,strength,mode,speed";
929 static char *def_show_wifi_fields
=
930 "link,status,essid,sec,strength,mode,speed";
933 * structures for 'dladm show-linkprop'
942 } linkprop_field_index_t
;
944 static const ofmt_field_t linkprop_fields
[] = {
945 /* name, field width, index */
946 { "LINK", 13, LINKPROP_LINK
, print_linkprop_cb
},
947 { "PROPERTY", 16, LINKPROP_PROPERTY
, print_linkprop_cb
},
948 { "PERM", 5, LINKPROP_PERM
, print_linkprop_cb
},
949 { "VALUE", 15, LINKPROP_VALUE
, print_linkprop_cb
},
950 { "DEFAULT", 15, LINKPROP_DEFAULT
, print_linkprop_cb
},
951 { "POSSIBLE", 20, LINKPROP_POSSIBLE
, print_linkprop_cb
},
955 #define MAX_PROP_LINE 512
957 typedef struct show_linkprop_state
{
958 char ls_link
[MAXLINKNAMELEN
];
961 dladm_arg_list_t
*ls_proplist
;
962 boolean_t ls_parsable
;
963 boolean_t ls_persist
;
965 dladm_status_t ls_status
;
966 dladm_status_t ls_retstatus
;
967 ofmt_handle_t ls_ofmt
;
968 } show_linkprop_state_t
;
970 typedef struct set_linkprop_state
{
974 dladm_status_t ls_status
;
975 } set_linkprop_state_t
;
977 typedef struct linkprop_args_s
{
978 show_linkprop_state_t
*ls_state
;
980 datalink_id_t ls_linkid
;
984 * structures for 'dladm show-secobj'
986 typedef struct secobj_fields_buf_s
{
987 char ss_obj_name
[DLADM_SECOBJ_VAL_MAX
];
990 } secobj_fields_buf_t
;
992 static const ofmt_field_t secobj_fields
[] = {
994 offsetof(secobj_fields_buf_t
, ss_obj_name
), print_default_cb
},
996 offsetof(secobj_fields_buf_t
, ss_class
), print_default_cb
},
998 offsetof(secobj_fields_buf_t
, ss_val
), print_default_cb
},
1003 * structures for 'dladm show-vnic'
1005 typedef struct vnic_fields_buf_s
1007 char vnic_link
[DLPI_LINKNAME_MAX
];
1008 char vnic_over
[DLPI_LINKNAME_MAX
];
1010 char vnic_macaddr
[18];
1011 char vnic_macaddrtype
[19];
1013 } vnic_fields_buf_t
;
1015 static const ofmt_field_t vnic_fields
[] = {
1017 offsetof(vnic_fields_buf_t
, vnic_link
), print_default_cb
},
1019 offsetof(vnic_fields_buf_t
, vnic_over
), print_default_cb
},
1021 offsetof(vnic_fields_buf_t
, vnic_speed
), print_default_cb
},
1023 offsetof(vnic_fields_buf_t
, vnic_macaddr
), print_default_cb
},
1024 { "MACADDRTYPE", 20,
1025 offsetof(vnic_fields_buf_t
, vnic_macaddrtype
), print_default_cb
},
1027 offsetof(vnic_fields_buf_t
, vnic_vid
), print_default_cb
},
1028 { NULL
, 0, 0, NULL
}}
1032 * structures for 'dladm show-ib'
1034 typedef struct ib_fields_buf_s
1036 char ib_link
[DLPI_LINKNAME_MAX
];
1037 char ib_hcaguid
[17];
1038 char ib_portguid
[17];
1041 char ib_pkeys
[MAXPKEYSTRSZ
];
1044 static const ofmt_field_t ib_fields
[] = {
1046 offsetof(ib_fields_buf_t
, ib_link
), print_default_cb
},
1047 { "HCAGUID", IBGUIDSTRLEN
,
1048 offsetof(ib_fields_buf_t
, ib_hcaguid
), print_default_cb
},
1049 { "PORTGUID", IBGUIDSTRLEN
,
1050 offsetof(ib_fields_buf_t
, ib_portguid
), print_default_cb
},
1051 { "PORT", IBPORTSTRLEN
,
1052 offsetof(ib_fields_buf_t
, ib_portnum
), print_default_cb
},
1054 offsetof(ib_fields_buf_t
, ib_state
), print_default_cb
},
1056 offsetof(ib_fields_buf_t
, ib_pkeys
), print_default_cb
},
1057 { NULL
, 0, 0, NULL
}};
1060 * structures for 'dladm show-part'
1062 typedef struct part_fields_buf_s
1064 char part_link
[DLPI_LINKNAME_MAX
];
1066 char part_over
[DLPI_LINKNAME_MAX
];
1069 } part_fields_buf_t
;
1071 static const ofmt_field_t part_fields
[] = {
1073 offsetof(part_fields_buf_t
, part_link
), print_default_cb
},
1074 { "PKEY", MAXPKEYLEN
,
1075 offsetof(part_fields_buf_t
, part_pkey
), print_default_cb
},
1077 offsetof(part_fields_buf_t
, part_over
), print_default_cb
},
1079 offsetof(part_fields_buf_t
, part_state
), print_default_cb
},
1081 offsetof(part_fields_buf_t
, part_flags
), print_default_cb
},
1082 { NULL
, 0, 0, NULL
}};
1085 * structures for 'dladm show-simnet'
1087 typedef struct simnet_fields_buf_s
1089 char simnet_name
[DLPI_LINKNAME_MAX
];
1090 char simnet_media
[DLADM_STRSIZE
];
1091 char simnet_macaddr
[18];
1092 char simnet_otherlink
[DLPI_LINKNAME_MAX
];
1093 } simnet_fields_buf_t
;
1095 static const ofmt_field_t simnet_fields
[] = {
1097 offsetof(simnet_fields_buf_t
, simnet_name
), print_default_cb
},
1099 offsetof(simnet_fields_buf_t
, simnet_media
), print_default_cb
},
1101 offsetof(simnet_fields_buf_t
, simnet_macaddr
), print_default_cb
},
1103 offsetof(simnet_fields_buf_t
, simnet_otherlink
), print_default_cb
},
1104 { NULL
, 0, 0, NULL
}}
1108 * structures for 'dladm show-usage'
1111 typedef struct usage_fields_buf_s
{
1112 char usage_link
[12];
1113 char usage_duration
[10];
1114 char usage_ipackets
[9];
1115 char usage_rbytes
[10];
1116 char usage_opackets
[9];
1117 char usage_obytes
[10];
1118 char usage_bandwidth
[14];
1119 } usage_fields_buf_t
;
1121 static const ofmt_field_t usage_fields
[] = {
1123 offsetof(usage_fields_buf_t
, usage_link
), print_default_cb
},
1125 offsetof(usage_fields_buf_t
, usage_duration
), print_default_cb
},
1127 offsetof(usage_fields_buf_t
, usage_ipackets
), print_default_cb
},
1129 offsetof(usage_fields_buf_t
, usage_rbytes
), print_default_cb
},
1131 offsetof(usage_fields_buf_t
, usage_opackets
), print_default_cb
},
1133 offsetof(usage_fields_buf_t
, usage_obytes
), print_default_cb
},
1135 offsetof(usage_fields_buf_t
, usage_bandwidth
), print_default_cb
},
1136 { NULL
, 0, 0, NULL
}}
1141 * structures for 'dladm show-usage link'
1144 typedef struct usage_l_fields_buf_s
{
1145 char usage_l_link
[12];
1146 char usage_l_stime
[13];
1147 char usage_l_etime
[13];
1148 char usage_l_rbytes
[8];
1149 char usage_l_obytes
[8];
1150 char usage_l_bandwidth
[14];
1151 } usage_l_fields_buf_t
;
1153 static const ofmt_field_t usage_l_fields
[] = {
1154 /* name, field width, offset */
1156 offsetof(usage_l_fields_buf_t
, usage_l_link
), print_default_cb
},
1158 offsetof(usage_l_fields_buf_t
, usage_l_stime
), print_default_cb
},
1160 offsetof(usage_l_fields_buf_t
, usage_l_etime
), print_default_cb
},
1162 offsetof(usage_l_fields_buf_t
, usage_l_rbytes
), print_default_cb
},
1164 offsetof(usage_l_fields_buf_t
, usage_l_obytes
), print_default_cb
},
1166 offsetof(usage_l_fields_buf_t
, usage_l_bandwidth
), print_default_cb
},
1167 { NULL
, 0, 0, NULL
}}
1170 /* IPTUN_*FLAG_INDEX values are indices into iptun_flags below. */
1171 enum { IPTUN_SFLAG_INDEX
, IPTUN_IFLAG_INDEX
, IPTUN_NUM_FLAGS
};
1174 * structures for 'dladm show-iptun'
1176 typedef struct iptun_fields_buf_s
{
1177 char iptun_name
[MAXLINKNAMELEN
];
1179 char iptun_laddr
[NI_MAXHOST
];
1180 char iptun_raddr
[NI_MAXHOST
];
1181 char iptun_flags
[IPTUN_NUM_FLAGS
+ 1];
1182 } iptun_fields_buf_t
;
1184 static const ofmt_field_t iptun_fields
[] = {
1186 offsetof(iptun_fields_buf_t
, iptun_name
), print_default_cb
},
1188 offsetof(iptun_fields_buf_t
, iptun_type
), print_default_cb
},
1190 offsetof(iptun_fields_buf_t
, iptun_flags
), print_default_cb
},
1192 offsetof(iptun_fields_buf_t
, iptun_laddr
), print_default_cb
},
1194 offsetof(iptun_fields_buf_t
, iptun_raddr
), print_default_cb
},
1199 * structures for 'dladm show-bridge'. These are based on sections 14.8.1.1.3
1200 * and 14.8.1.2.2 of IEEE 802.1D-2004.
1202 typedef struct bridge_fields_buf_s
{
1203 char bridge_name
[MAXLINKNAMELEN
]; /* 14.4.1.2.3(b) */
1204 char bridge_protect
[7]; /* stp or trill */
1205 char bridge_address
[24]; /* 17.18.3, 7.12.5, 14.4.1.2.3(a) */
1206 char bridge_priority
[7]; /* 17.18.3 9.2.5 - only upper 4 bits */
1207 char bridge_bmaxage
[7]; /* 17.18.4 configured */
1208 char bridge_bhellotime
[7]; /* 17.18.4 configured */
1209 char bridge_bfwddelay
[7]; /* 17.18.4 configured */
1210 char bridge_forceproto
[3]; /* 17.13.4 configured */
1211 char bridge_tctime
[12]; /* 14.8.1.1.3(b) */
1212 char bridge_tccount
[12]; /* 17.17.8 */
1213 char bridge_tchange
[12]; /* 17.17.8 */
1214 char bridge_desroot
[24]; /* 17.18.6 priority "/" MAC */
1215 char bridge_rootcost
[12]; /* 17.18.6 */
1216 char bridge_rootport
[12]; /* 17.18.6 */
1217 char bridge_maxage
[7]; /* 17.18.7 for root */
1218 char bridge_hellotime
[7]; /* 17.13.6 for root */
1219 char bridge_fwddelay
[7]; /* 17.13.5 for root */
1220 char bridge_holdtime
[12]; /* 17.13.12 for root */
1221 } bridge_fields_buf_t
;
1223 static ofmt_field_t bridge_fields
[] = {
1224 /* name, field width, offset, callback */
1226 offsetof(bridge_fields_buf_t
, bridge_name
), print_default_cb
},
1228 offsetof(bridge_fields_buf_t
, bridge_protect
), print_default_cb
},
1230 offsetof(bridge_fields_buf_t
, bridge_address
), print_default_cb
},
1232 offsetof(bridge_fields_buf_t
, bridge_priority
), print_default_cb
},
1234 offsetof(bridge_fields_buf_t
, bridge_bmaxage
), print_default_cb
},
1236 offsetof(bridge_fields_buf_t
, bridge_bhellotime
), print_default_cb
},
1238 offsetof(bridge_fields_buf_t
, bridge_bfwddelay
), print_default_cb
},
1240 offsetof(bridge_fields_buf_t
, bridge_forceproto
), print_default_cb
},
1242 offsetof(bridge_fields_buf_t
, bridge_tctime
), print_default_cb
},
1244 offsetof(bridge_fields_buf_t
, bridge_tccount
), print_default_cb
},
1246 offsetof(bridge_fields_buf_t
, bridge_tchange
), print_default_cb
},
1248 offsetof(bridge_fields_buf_t
, bridge_desroot
), print_default_cb
},
1250 offsetof(bridge_fields_buf_t
, bridge_rootcost
), print_default_cb
},
1252 offsetof(bridge_fields_buf_t
, bridge_rootport
), print_default_cb
},
1254 offsetof(bridge_fields_buf_t
, bridge_maxage
), print_default_cb
},
1256 offsetof(bridge_fields_buf_t
, bridge_hellotime
), print_default_cb
},
1258 offsetof(bridge_fields_buf_t
, bridge_fwddelay
), print_default_cb
},
1260 offsetof(bridge_fields_buf_t
, bridge_holdtime
), print_default_cb
},
1261 { NULL
, 0, 0, NULL
}};
1264 * structures for 'dladm show-bridge -l'. These are based on 14.4.1.2.3 and
1265 * 14.8.2.1.3 of IEEE 802.1D-2004.
1267 typedef struct bridge_link_fields_buf_s
{
1268 char bridgel_link
[MAXLINKNAMELEN
];
1269 char bridgel_index
[7]; /* 14.4.1.2.3(d1) */
1270 char bridgel_state
[11]; /* 14.8.2.1.3(b) */
1271 char bridgel_uptime
[7]; /* 14.8.2.1.3(a) */
1272 char bridgel_opercost
[7] /* 14.8.2.1.3(d) */;
1273 char bridgel_operp2p
[4]; /* 14.8.2.1.3(p) */
1274 char bridgel_operedge
[4]; /* 14.8.2.1.3(k) */
1275 char bridgel_desroot
[23]; /* 14.8.2.1.3(e) */
1276 char bridgel_descost
[12]; /* 14.8.2.1.3(f) */
1277 char bridgel_desbridge
[23]; /* 14.8.2.1.3(g) */
1278 char bridgel_desport
[7]; /* 14.8.2.1.3(h) */
1279 char bridgel_tcack
[4]; /* 14.8.2.1.3(i) */
1280 } bridge_link_fields_buf_t
;
1282 static ofmt_field_t bridge_link_fields
[] = {
1283 /* name, field width, offset, callback */
1285 offsetof(bridge_link_fields_buf_t
, bridgel_link
), print_default_cb
},
1287 offsetof(bridge_link_fields_buf_t
, bridgel_index
), print_default_cb
},
1289 offsetof(bridge_link_fields_buf_t
, bridgel_state
), print_default_cb
},
1291 offsetof(bridge_link_fields_buf_t
, bridgel_uptime
), print_default_cb
},
1293 offsetof(bridge_link_fields_buf_t
, bridgel_opercost
), print_default_cb
},
1295 offsetof(bridge_link_fields_buf_t
, bridgel_operp2p
), print_default_cb
},
1297 offsetof(bridge_link_fields_buf_t
, bridgel_operedge
), print_default_cb
},
1299 offsetof(bridge_link_fields_buf_t
, bridgel_desroot
), print_default_cb
},
1301 offsetof(bridge_link_fields_buf_t
, bridgel_descost
), print_default_cb
},
1303 offsetof(bridge_link_fields_buf_t
, bridgel_desbridge
), print_default_cb
},
1305 offsetof(bridge_link_fields_buf_t
, bridgel_desport
), print_default_cb
},
1307 offsetof(bridge_link_fields_buf_t
, bridgel_tcack
), print_default_cb
},
1308 { NULL
, 0, 0, NULL
}};
1311 * structures for 'dladm show-bridge -s'. These are not based on IEEE
1314 #define ULONG_DIG (((sizeof (ulong_t) * NBBY) * 3 / 10) + 1)
1315 #define UINT64_DIG (((sizeof (uint64_t) * NBBY) * 3 / 10) + 1)
1316 typedef struct bridge_statfields_buf_s
{
1317 char bridges_name
[MAXLINKNAMELEN
];
1318 char bridges_drops
[UINT64_DIG
];
1319 char bridges_forwards
[UINT64_DIG
];
1320 char bridges_mbcast
[UINT64_DIG
];
1321 char bridges_unknown
[UINT64_DIG
];
1322 char bridges_recv
[UINT64_DIG
];
1323 char bridges_sent
[UINT64_DIG
];
1324 } bridge_statfields_buf_t
;
1326 static ofmt_field_t bridge_statfields
[] = {
1327 /* name, field width, offset, callback */
1329 offsetof(bridge_statfields_buf_t
, bridges_name
), print_default_cb
},
1331 offsetof(bridge_statfields_buf_t
, bridges_drops
), print_default_cb
},
1333 offsetof(bridge_statfields_buf_t
, bridges_forwards
), print_default_cb
},
1335 offsetof(bridge_statfields_buf_t
, bridges_mbcast
), print_default_cb
},
1337 offsetof(bridge_statfields_buf_t
, bridges_unknown
), print_default_cb
},
1339 offsetof(bridge_statfields_buf_t
, bridges_recv
), print_default_cb
},
1341 offsetof(bridge_statfields_buf_t
, bridges_sent
), print_default_cb
},
1342 { NULL
, 0, 0, NULL
}};
1345 * structures for 'dladm show-bridge -s -l'. These are based in part on
1346 * section 14.6.1.1.3 of IEEE 802.1D-2004.
1348 typedef struct bridge_link_statfields_buf_s
{
1349 char bridgels_link
[MAXLINKNAMELEN
];
1350 char bridgels_cfgbpdu
[ULONG_DIG
];
1351 char bridgels_tcnbpdu
[ULONG_DIG
];
1352 char bridgels_rstpbpdu
[ULONG_DIG
];
1353 char bridgels_txbpdu
[ULONG_DIG
];
1354 char bridgels_drops
[UINT64_DIG
]; /* 14.6.1.1.3(d) */
1355 char bridgels_recv
[UINT64_DIG
]; /* 14.6.1.1.3(a) */
1356 char bridgels_xmit
[UINT64_DIG
]; /* 14.6.1.1.3(c) */
1357 } bridge_link_statfields_buf_t
;
1359 static ofmt_field_t bridge_link_statfields
[] = {
1360 /* name, field width, offset, callback */
1362 offsetof(bridge_link_statfields_buf_t
, bridgels_link
), print_default_cb
},
1364 offsetof(bridge_link_statfields_buf_t
, bridgels_cfgbpdu
),
1367 offsetof(bridge_link_statfields_buf_t
, bridgels_tcnbpdu
),
1370 offsetof(bridge_link_statfields_buf_t
, bridgels_rstpbpdu
),
1373 offsetof(bridge_link_statfields_buf_t
, bridgels_txbpdu
), print_default_cb
},
1375 offsetof(bridge_link_statfields_buf_t
, bridgels_drops
), print_default_cb
},
1377 offsetof(bridge_link_statfields_buf_t
, bridgels_recv
), print_default_cb
},
1379 offsetof(bridge_link_statfields_buf_t
, bridgels_xmit
), print_default_cb
},
1380 { NULL
, 0, 0, NULL
}};
1383 * structures for 'dladm show-bridge -f'. These are based in part on
1384 * section 14.7.6.3.3 of IEEE 802.1D-2004.
1386 typedef struct bridge_fwd_fields_buf_s
{
1387 char bridgef_dest
[18]; /* 14.7.6.3.3(a) */
1388 char bridgef_age
[8];
1389 char bridgef_flags
[6];
1390 char bridgef_output
[MAXLINKNAMELEN
]; /* 14.7.6.3.3(c) */
1391 } bridge_fwd_fields_buf_t
;
1393 static ofmt_field_t bridge_fwd_fields
[] = {
1394 /* name, field width, offset, callback */
1396 offsetof(bridge_fwd_fields_buf_t
, bridgef_dest
), print_default_cb
},
1398 offsetof(bridge_fwd_fields_buf_t
, bridgef_age
), print_default_cb
},
1400 offsetof(bridge_fwd_fields_buf_t
, bridgef_flags
), print_default_cb
},
1402 offsetof(bridge_fwd_fields_buf_t
, bridgef_output
), print_default_cb
},
1403 { NULL
, 0, 0, NULL
}};
1406 * structures for 'dladm show-bridge -t'.
1408 typedef struct bridge_trill_fields_buf_s
{
1409 char bridget_nick
[6];
1410 char bridget_flags
[6];
1411 char bridget_link
[MAXLINKNAMELEN
];
1412 char bridget_nexthop
[18];
1413 } bridge_trill_fields_buf_t
;
1415 static ofmt_field_t bridge_trill_fields
[] = {
1416 /* name, field width, offset, callback */
1418 offsetof(bridge_trill_fields_buf_t
, bridget_nick
), print_default_cb
},
1420 offsetof(bridge_trill_fields_buf_t
, bridget_flags
), print_default_cb
},
1422 offsetof(bridge_trill_fields_buf_t
, bridget_link
), print_default_cb
},
1424 offsetof(bridge_trill_fields_buf_t
, bridget_nexthop
), print_default_cb
},
1425 { NULL
, 0, 0, NULL
}};
1427 static char *progname
;
1428 static sig_atomic_t signalled
;
1431 * Handle to libdladm. Opened in main() before the sub-command
1432 * specific function is called.
1434 static dladm_handle_t handle
= NULL
;
1436 #define DLADM_ETHERSTUB_NAME "etherstub"
1437 #define DLADM_IS_ETHERSTUB(id) (id == DATALINK_INVALID_LINKID)
1444 (void) fprintf(stderr
, gettext("usage: dladm <subcommand> <args> ..."
1446 for (i
= 0; i
< sizeof (cmds
) / sizeof (cmds
[0]); i
++) {
1448 if (cmdp
->c_usage
!= NULL
)
1449 (void) fprintf(stderr
, "%s\n", gettext(cmdp
->c_usage
));
1452 /* close dladm handle if it was opened */
1454 dladm_close(handle
);
1460 main(int argc
, char *argv
[])
1464 dladm_status_t status
;
1466 (void) setlocale(LC_ALL
, "");
1467 #if !defined(TEXT_DOMAIN)
1468 #define TEXT_DOMAIN "SYS_TEST"
1470 (void) textdomain(TEXT_DOMAIN
);
1474 /* Open the libdladm handle */
1475 if ((status
= dladm_open(&handle
)) != DLADM_STATUS_OK
) {
1477 "could not open /dev/dld");
1481 char *args
= "show-link";
1482 do_show_link(1, &args
, args
);
1483 dladm_close(handle
);
1487 for (i
= 0; i
< sizeof (cmds
) / sizeof (cmds
[0]); i
++) {
1489 if (strcmp(argv
[1], cmdp
->c_name
) == 0) {
1490 cmdp
->c_fn(argc
- 1, &argv
[1], cmdp
->c_usage
);
1491 dladm_close(handle
);
1492 return (EXIT_SUCCESS
);
1496 (void) fprintf(stderr
, gettext("%s: unknown subcommand '%s'\n"),
1499 return (EXIT_FAILURE
);
1504 show_usage_date(dladm_usage_t
*usage
, void *arg
)
1506 show_usage_state_t
*state
= (show_usage_state_t
*)arg
;
1509 dladm_status_t status
;
1513 * Only show usage information for existing links unless '-a'
1516 if (!state
->us_showall
) {
1517 if ((status
= dladm_name2info(handle
, usage
->du_name
,
1518 NULL
, &flags
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
1521 if ((flags
& DLADM_OPT_ACTIVE
) == 0)
1522 return (DLADM_STATUS_LINKINVAL
);
1525 stime
= usage
->du_stime
;
1526 (void) strftime(timebuf
, sizeof (timebuf
), "%m/%d/%Y",
1528 (void) printf("%s\n", timebuf
);
1530 return (DLADM_STATUS_OK
);
1534 show_usage_time(dladm_usage_t
*usage
, void *arg
)
1536 show_usage_state_t
*state
= (show_usage_state_t
*)arg
;
1537 char buf
[DLADM_STRSIZE
];
1538 usage_l_fields_buf_t ubuf
;
1541 dladm_status_t status
;
1545 * Only show usage information for existing links unless '-a'
1548 if (!state
->us_showall
) {
1549 if ((status
= dladm_name2info(handle
, usage
->du_name
,
1550 NULL
, &flags
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
1553 if ((flags
& DLADM_OPT_ACTIVE
) == 0)
1554 return (DLADM_STATUS_LINKINVAL
);
1557 if (state
->us_plot
) {
1558 if (!state
->us_printheader
) {
1559 if (state
->us_first
) {
1560 (void) printf("# Time");
1561 state
->us_first
= B_FALSE
;
1563 (void) printf(" %s", usage
->du_name
);
1564 if (usage
->du_last
) {
1565 (void) printf("\n");
1566 state
->us_first
= B_TRUE
;
1567 state
->us_printheader
= B_TRUE
;
1570 if (state
->us_first
) {
1571 time
= usage
->du_etime
;
1572 (void) strftime(buf
, sizeof (buf
), "%T",
1574 state
->us_first
= B_FALSE
;
1575 (void) printf("%s", buf
);
1577 bw
= (double)usage
->du_bandwidth
/1000;
1578 (void) printf(" %.2f", bw
);
1579 if (usage
->du_last
) {
1580 (void) printf("\n");
1581 state
->us_first
= B_TRUE
;
1584 return (DLADM_STATUS_OK
);
1587 bzero(&ubuf
, sizeof (ubuf
));
1589 (void) snprintf(ubuf
.usage_l_link
, sizeof (ubuf
.usage_l_link
), "%s",
1591 time
= usage
->du_stime
;
1592 (void) strftime(buf
, sizeof (buf
), "%T", localtime(&time
));
1593 (void) snprintf(ubuf
.usage_l_stime
, sizeof (ubuf
.usage_l_stime
), "%s",
1595 time
= usage
->du_etime
;
1596 (void) strftime(buf
, sizeof (buf
), "%T", localtime(&time
));
1597 (void) snprintf(ubuf
.usage_l_etime
, sizeof (ubuf
.usage_l_etime
), "%s",
1599 (void) snprintf(ubuf
.usage_l_rbytes
, sizeof (ubuf
.usage_l_rbytes
),
1600 "%llu", usage
->du_rbytes
);
1601 (void) snprintf(ubuf
.usage_l_obytes
, sizeof (ubuf
.usage_l_obytes
),
1602 "%llu", usage
->du_obytes
);
1603 (void) snprintf(ubuf
.usage_l_bandwidth
, sizeof (ubuf
.usage_l_bandwidth
),
1604 "%s Mbps", dladm_bw2str(usage
->du_bandwidth
, buf
));
1606 ofmt_print(state
->us_ofmt
, &ubuf
);
1607 return (DLADM_STATUS_OK
);
1611 show_usage_res(dladm_usage_t
*usage
, void *arg
)
1613 show_usage_state_t
*state
= (show_usage_state_t
*)arg
;
1614 char buf
[DLADM_STRSIZE
];
1615 usage_fields_buf_t ubuf
;
1616 dladm_status_t status
;
1620 * Only show usage information for existing links unless '-a'
1623 if (!state
->us_showall
) {
1624 if ((status
= dladm_name2info(handle
, usage
->du_name
,
1625 NULL
, &flags
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
1628 if ((flags
& DLADM_OPT_ACTIVE
) == 0)
1629 return (DLADM_STATUS_LINKINVAL
);
1632 bzero(&ubuf
, sizeof (ubuf
));
1634 (void) snprintf(ubuf
.usage_link
, sizeof (ubuf
.usage_link
), "%s",
1636 (void) snprintf(ubuf
.usage_duration
, sizeof (ubuf
.usage_duration
),
1637 "%llu", usage
->du_duration
);
1638 (void) snprintf(ubuf
.usage_ipackets
, sizeof (ubuf
.usage_ipackets
),
1639 "%llu", usage
->du_ipackets
);
1640 (void) snprintf(ubuf
.usage_rbytes
, sizeof (ubuf
.usage_rbytes
),
1641 "%llu", usage
->du_rbytes
);
1642 (void) snprintf(ubuf
.usage_opackets
, sizeof (ubuf
.usage_opackets
),
1643 "%llu", usage
->du_opackets
);
1644 (void) snprintf(ubuf
.usage_obytes
, sizeof (ubuf
.usage_obytes
),
1645 "%llu", usage
->du_obytes
);
1646 (void) snprintf(ubuf
.usage_bandwidth
, sizeof (ubuf
.usage_bandwidth
),
1647 "%s Mbps", dladm_bw2str(usage
->du_bandwidth
, buf
));
1649 ofmt_print(state
->us_ofmt
, &ubuf
);
1651 return (DLADM_STATUS_OK
);
1655 valid_formatspec(char *formatspec_str
)
1657 if (strcmp(formatspec_str
, "gnuplot") == 0)
1665 do_show_usage(int argc
, char *argv
[], const char *use
)
1669 dladm_status_t status
;
1670 boolean_t d_arg
= B_FALSE
;
1673 char *resource
= NULL
;
1674 show_usage_state_t state
;
1675 boolean_t o_arg
= B_FALSE
;
1676 boolean_t F_arg
= B_FALSE
;
1677 char *fields_str
= NULL
;
1678 char *formatspec_str
= NULL
;
1679 char *all_l_fields
=
1680 "link,start,end,rbytes,obytes,bandwidth";
1682 ofmt_status_t oferr
;
1683 uint_t ofmtflags
= 0;
1685 bzero(&state
, sizeof (show_usage_state_t
));
1686 state
.us_parsable
= B_FALSE
;
1687 state
.us_printheader
= B_FALSE
;
1688 state
.us_plot
= B_FALSE
;
1689 state
.us_first
= B_TRUE
;
1691 while ((opt
= getopt_long(argc
, argv
, "das:e:o:f:F:",
1692 usage_opts
, NULL
)) != -1) {
1698 state
.us_showall
= B_TRUE
;
1711 fields_str
= optarg
;
1714 state
.us_plot
= F_arg
= B_TRUE
;
1715 formatspec_str
= optarg
;
1718 die_opterr(optopt
, opt
, use
);
1724 die("show-usage requires a file");
1726 if (optind
== (argc
-1)) {
1729 resource
= argv
[optind
];
1730 if (!state
.us_showall
&&
1731 (((status
= dladm_name2info(handle
, resource
, NULL
, &flags
,
1732 NULL
, NULL
)) != DLADM_STATUS_OK
) ||
1733 ((flags
& DLADM_OPT_ACTIVE
) == 0))) {
1734 die("invalid link: '%s'", resource
);
1739 die("incompatible -d and -F options");
1741 if (F_arg
&& valid_formatspec(formatspec_str
) == B_FALSE
)
1742 die("Format specifier %s not supported", formatspec_str
);
1744 if (state
.us_parsable
)
1745 ofmtflags
|= OFMT_PARSABLE
;
1747 if (resource
== NULL
&& stime
== NULL
&& etime
== NULL
) {
1748 oferr
= ofmt_open(fields_str
, usage_fields
, ofmtflags
, 0,
1751 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0))
1752 fields_str
= all_l_fields
;
1753 oferr
= ofmt_open(fields_str
, usage_l_fields
, ofmtflags
, 0,
1757 ofmt_check(oferr
, state
.us_parsable
, ofmt
, die
, warn
);
1758 state
.us_ofmt
= ofmt
;
1761 /* Print log dates */
1762 status
= dladm_usage_dates(show_usage_date
,
1763 DLADM_LOGTYPE_LINK
, file
, resource
, &state
);
1764 } else if (resource
== NULL
&& stime
== NULL
&& etime
== NULL
&&
1767 status
= dladm_usage_summary(show_usage_res
,
1768 DLADM_LOGTYPE_LINK
, file
, &state
);
1769 } else if (resource
!= NULL
) {
1770 /* Print log entries for named resource */
1771 status
= dladm_walk_usage_res(show_usage_time
,
1772 DLADM_LOGTYPE_LINK
, file
, resource
, stime
, etime
, &state
);
1774 /* Print time and information for each link */
1775 status
= dladm_walk_usage_time(show_usage_time
,
1776 DLADM_LOGTYPE_LINK
, file
, stime
, etime
, &state
);
1779 if (status
!= DLADM_STATUS_OK
)
1780 die_dlerr(status
, "show-usage");
1785 do_create_aggr(int argc
, char *argv
[], const char *use
)
1789 uint32_t policy
= AGGR_POLICY_L4
;
1790 aggr_lacp_mode_t lacp_mode
= AGGR_LACP_OFF
;
1791 aggr_lacp_timer_t lacp_timer
= AGGR_LACP_TIMER_SHORT
;
1792 dladm_aggr_port_attr_db_t port
[MAXPORT
];
1793 uint_t n
, ndev
, nlink
;
1794 uint8_t mac_addr
[ETHERADDRL
];
1795 boolean_t mac_addr_fixed
= B_FALSE
;
1796 boolean_t P_arg
= B_FALSE
;
1797 boolean_t l_arg
= B_FALSE
;
1798 boolean_t u_arg
= B_FALSE
;
1799 boolean_t T_arg
= B_FALSE
;
1800 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
1801 char *altroot
= NULL
;
1802 char name
[MAXLINKNAMELEN
];
1803 char *devs
[MAXPORT
];
1804 char *links
[MAXPORT
];
1805 dladm_status_t status
;
1806 dladm_status_t pstatus
;
1807 char propstr
[DLADM_STRSIZE
];
1808 dladm_arg_list_t
*proplist
= NULL
;
1810 datalink_id_t linkid
;
1812 ndev
= nlink
= opterr
= 0;
1813 bzero(propstr
, DLADM_STRSIZE
);
1815 while ((option
= getopt_long(argc
, argv
, ":d:l:L:P:R:tfu:T:p:",
1816 lopts
, NULL
)) != -1) {
1819 if (ndev
+ nlink
>= MAXPORT
)
1820 die("too many ports specified");
1822 devs
[ndev
++] = optarg
;
1829 if (!dladm_aggr_str2policy(optarg
, &policy
))
1830 die("invalid policy '%s'", optarg
);
1837 if (!dladm_aggr_str2macaddr(optarg
, &mac_addr_fixed
,
1839 die("invalid MAC address '%s'", optarg
);
1842 if (isdigit(optarg
[strlen(optarg
) - 1])) {
1845 * Ended with digit, possibly a link name.
1847 if (ndev
+ nlink
>= MAXPORT
)
1848 die("too many ports specified");
1850 links
[nlink
++] = optarg
;
1859 if (!dladm_aggr_str2lacpmode(optarg
, &lacp_mode
))
1860 die("invalid LACP mode '%s'", optarg
);
1867 if (!dladm_aggr_str2lacptimer(optarg
, &lacp_timer
))
1868 die("invalid LACP timer value '%s'", optarg
);
1871 flags
&= ~DLADM_OPT_PERSIST
;
1874 flags
|= DLADM_OPT_FORCE
;
1880 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
1881 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
1883 die("property list too long '%s'", propstr
);
1887 die_opterr(optopt
, option
, use
);
1892 if (ndev
+ nlink
== 0)
1895 /* get key value or the aggregation name (required last argument) */
1896 if (optind
!= (argc
-1))
1899 if (!str2int(argv
[optind
], &key
)) {
1900 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >=
1902 die("link name too long '%s'", argv
[optind
]);
1905 if (!dladm_valid_linkname(name
))
1906 die("invalid link name '%s'", argv
[optind
]);
1908 (void) snprintf(name
, MAXLINKNAMELEN
, "aggr%d", key
);
1911 if (altroot
!= NULL
)
1912 altroot_cmd(altroot
, argc
, argv
);
1914 for (n
= 0; n
< ndev
; n
++) {
1915 if ((status
= dladm_dev2linkid(handle
, devs
[n
],
1916 &port
[n
].lp_linkid
)) != DLADM_STATUS_OK
) {
1917 die_dlerr(status
, "invalid dev name '%s'", devs
[n
]);
1921 for (n
= 0; n
< nlink
; n
++) {
1922 if ((status
= dladm_name2info(handle
, links
[n
],
1923 &port
[ndev
+ n
].lp_linkid
, NULL
, NULL
, NULL
)) !=
1925 die_dlerr(status
, "invalid link name '%s'", links
[n
]);
1929 status
= dladm_aggr_create(handle
, name
, key
, ndev
+ nlink
, port
,
1930 policy
, mac_addr_fixed
, (const uchar_t
*)mac_addr
, lacp_mode
,
1932 if (status
!= DLADM_STATUS_OK
)
1935 if (dladm_parse_link_props(propstr
, &proplist
, B_FALSE
)
1937 die("invalid aggregation property");
1939 if (proplist
== NULL
)
1942 status
= dladm_name2info(handle
, name
, &linkid
, NULL
, NULL
, NULL
);
1943 if (status
!= DLADM_STATUS_OK
)
1946 for (i
= 0; i
< proplist
->al_count
; i
++) {
1947 dladm_arg_info_t
*aip
= &proplist
->al_info
[i
];
1949 pstatus
= dladm_set_linkprop(handle
, linkid
, aip
->ai_name
,
1950 aip
->ai_val
, aip
->ai_count
, flags
);
1952 if (pstatus
!= DLADM_STATUS_OK
) {
1954 "aggr creation succeeded but "
1955 "could not set property '%s'", aip
->ai_name
);
1959 dladm_free_props(proplist
);
1960 if (status
!= DLADM_STATUS_OK
) {
1961 if (status
== DLADM_STATUS_NONOTIF
) {
1962 die("not all links have link up/down detection; must "
1963 "use -f (see dladm(8))");
1965 die_dlerr(status
, "create operation failed");
1971 * arg is either the key or the aggr name. Validate it and convert it to
1972 * the linkid if altroot is NULL.
1974 static dladm_status_t
1975 i_dladm_aggr_get_linkid(const char *altroot
, const char *arg
,
1976 datalink_id_t
*linkidp
, uint32_t flags
)
1980 dladm_status_t status
;
1982 if (!str2int(arg
, &key
))
1985 if (aggr
== NULL
&& key
== 0)
1986 return (DLADM_STATUS_LINKINVAL
);
1988 if (altroot
!= NULL
)
1989 return (DLADM_STATUS_OK
);
1992 status
= dladm_name2info(handle
, aggr
, linkidp
, NULL
, NULL
,
1995 status
= dladm_key2linkid(handle
, key
, linkidp
, flags
);
2002 do_delete_aggr(int argc
, char *argv
[], const char *use
)
2005 char *altroot
= NULL
;
2006 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
2007 dladm_status_t status
;
2008 datalink_id_t linkid
;
2011 while ((option
= getopt_long(argc
, argv
, ":R:t", lopts
, NULL
)) != -1) {
2014 flags
&= ~DLADM_OPT_PERSIST
;
2020 die_opterr(optopt
, option
, use
);
2025 /* get key value or the aggregation name (required last argument) */
2026 if (optind
!= (argc
-1))
2029 status
= i_dladm_aggr_get_linkid(altroot
, argv
[optind
], &linkid
, flags
);
2030 if (status
!= DLADM_STATUS_OK
)
2033 if (altroot
!= NULL
)
2034 altroot_cmd(altroot
, argc
, argv
);
2036 status
= dladm_aggr_delete(handle
, linkid
, flags
);
2038 if (status
!= DLADM_STATUS_OK
)
2039 die_dlerr(status
, "delete operation failed");
2043 do_add_aggr(int argc
, char *argv
[], const char *use
)
2046 uint_t n
, ndev
, nlink
;
2047 char *altroot
= NULL
;
2048 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
2049 datalink_id_t linkid
;
2050 dladm_status_t status
;
2051 dladm_aggr_port_attr_db_t port
[MAXPORT
];
2052 char *devs
[MAXPORT
];
2053 char *links
[MAXPORT
];
2055 ndev
= nlink
= opterr
= 0;
2056 while ((option
= getopt_long(argc
, argv
, ":d:l:R:tf", lopts
,
2060 if (ndev
+ nlink
>= MAXPORT
)
2061 die("too many ports specified");
2063 devs
[ndev
++] = optarg
;
2066 if (ndev
+ nlink
>= MAXPORT
)
2067 die("too many ports specified");
2069 links
[nlink
++] = optarg
;
2072 flags
&= ~DLADM_OPT_PERSIST
;
2075 flags
|= DLADM_OPT_FORCE
;
2081 die_opterr(optopt
, option
, use
);
2086 if (ndev
+ nlink
== 0)
2089 /* get key value or the aggregation name (required last argument) */
2090 if (optind
!= (argc
-1))
2093 if ((status
= i_dladm_aggr_get_linkid(altroot
, argv
[optind
], &linkid
,
2094 flags
& (DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
))) !=
2099 if (altroot
!= NULL
)
2100 altroot_cmd(altroot
, argc
, argv
);
2102 for (n
= 0; n
< ndev
; n
++) {
2103 if ((status
= dladm_dev2linkid(handle
, devs
[n
],
2104 &(port
[n
].lp_linkid
))) != DLADM_STATUS_OK
) {
2105 die_dlerr(status
, "invalid <dev> '%s'", devs
[n
]);
2109 for (n
= 0; n
< nlink
; n
++) {
2110 if ((status
= dladm_name2info(handle
, links
[n
],
2111 &port
[n
+ ndev
].lp_linkid
, NULL
, NULL
, NULL
)) !=
2113 die_dlerr(status
, "invalid <link> '%s'", links
[n
]);
2117 status
= dladm_aggr_add(handle
, linkid
, ndev
+ nlink
, port
, flags
);
2119 if (status
!= DLADM_STATUS_OK
) {
2121 * checking DLADM_STATUS_NOTSUP is a temporary workaround
2122 * and should be removed once 6399681 is fixed.
2124 if (status
== DLADM_STATUS_NOTSUP
) {
2125 die("add operation failed: link capabilities don't "
2127 } else if (status
== DLADM_STATUS_NONOTIF
) {
2128 die("not all links have link up/down detection; must "
2129 "use -f (see dladm(8))");
2131 die_dlerr(status
, "add operation failed");
2137 do_remove_aggr(int argc
, char *argv
[], const char *use
)
2140 dladm_aggr_port_attr_db_t port
[MAXPORT
];
2141 uint_t n
, ndev
, nlink
;
2142 char *devs
[MAXPORT
];
2143 char *links
[MAXPORT
];
2144 char *altroot
= NULL
;
2146 datalink_id_t linkid
;
2147 dladm_status_t status
;
2149 flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
2150 ndev
= nlink
= opterr
= 0;
2151 while ((option
= getopt_long(argc
, argv
, ":d:l:R:t",
2152 lopts
, NULL
)) != -1) {
2155 if (ndev
+ nlink
>= MAXPORT
)
2156 die("too many ports specified");
2158 devs
[ndev
++] = optarg
;
2161 if (ndev
+ nlink
>= MAXPORT
)
2162 die("too many ports specified");
2164 links
[nlink
++] = optarg
;
2167 flags
&= ~DLADM_OPT_PERSIST
;
2173 die_opterr(optopt
, option
, use
);
2178 if (ndev
+ nlink
== 0)
2181 /* get key value or the aggregation name (required last argument) */
2182 if (optind
!= (argc
-1))
2185 status
= i_dladm_aggr_get_linkid(altroot
, argv
[optind
], &linkid
, flags
);
2186 if (status
!= DLADM_STATUS_OK
)
2189 if (altroot
!= NULL
)
2190 altroot_cmd(altroot
, argc
, argv
);
2192 for (n
= 0; n
< ndev
; n
++) {
2193 if ((status
= dladm_dev2linkid(handle
, devs
[n
],
2194 &(port
[n
].lp_linkid
))) != DLADM_STATUS_OK
) {
2195 die_dlerr(status
, "invalid <dev> '%s'", devs
[n
]);
2199 for (n
= 0; n
< nlink
; n
++) {
2200 if ((status
= dladm_name2info(handle
, links
[n
],
2201 &port
[n
+ ndev
].lp_linkid
, NULL
, NULL
, NULL
)) !=
2203 die_dlerr(status
, "invalid <link> '%s'", links
[n
]);
2207 status
= dladm_aggr_remove(handle
, linkid
, ndev
+ nlink
, port
, flags
);
2209 if (status
!= DLADM_STATUS_OK
)
2210 die_dlerr(status
, "remove operation failed");
2214 do_modify_aggr(int argc
, char *argv
[], const char *use
)
2217 uint32_t policy
= AGGR_POLICY_L4
;
2218 aggr_lacp_mode_t lacp_mode
= AGGR_LACP_OFF
;
2219 aggr_lacp_timer_t lacp_timer
= AGGR_LACP_TIMER_SHORT
;
2220 uint8_t mac_addr
[ETHERADDRL
];
2221 boolean_t mac_addr_fixed
= B_FALSE
;
2222 uint8_t modify_mask
= 0;
2223 char *altroot
= NULL
;
2224 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
2225 datalink_id_t linkid
;
2226 dladm_status_t status
;
2229 while ((option
= getopt_long(argc
, argv
, ":L:l:P:R:tu:T:", lopts
,
2233 if (modify_mask
& DLADM_AGGR_MODIFY_POLICY
)
2236 modify_mask
|= DLADM_AGGR_MODIFY_POLICY
;
2238 if (!dladm_aggr_str2policy(optarg
, &policy
))
2239 die("invalid policy '%s'", optarg
);
2242 if (modify_mask
& DLADM_AGGR_MODIFY_MAC
)
2245 modify_mask
|= DLADM_AGGR_MODIFY_MAC
;
2247 if (!dladm_aggr_str2macaddr(optarg
, &mac_addr_fixed
,
2249 die("invalid MAC address '%s'", optarg
);
2253 if (modify_mask
& DLADM_AGGR_MODIFY_LACP_MODE
)
2256 modify_mask
|= DLADM_AGGR_MODIFY_LACP_MODE
;
2258 if (!dladm_aggr_str2lacpmode(optarg
, &lacp_mode
))
2259 die("invalid LACP mode '%s'", optarg
);
2262 if (modify_mask
& DLADM_AGGR_MODIFY_LACP_TIMER
)
2265 modify_mask
|= DLADM_AGGR_MODIFY_LACP_TIMER
;
2267 if (!dladm_aggr_str2lacptimer(optarg
, &lacp_timer
))
2268 die("invalid LACP timer value '%s'", optarg
);
2271 flags
&= ~DLADM_OPT_PERSIST
;
2277 die_opterr(optopt
, option
, use
);
2282 if (modify_mask
== 0)
2283 die("at least one of the -PulT options must be specified");
2285 /* get key value or the aggregation name (required last argument) */
2286 if (optind
!= (argc
-1))
2289 status
= i_dladm_aggr_get_linkid(altroot
, argv
[optind
], &linkid
, flags
);
2290 if (status
!= DLADM_STATUS_OK
)
2293 if (altroot
!= NULL
)
2294 altroot_cmd(altroot
, argc
, argv
);
2296 status
= dladm_aggr_modify(handle
, linkid
, modify_mask
, policy
,
2297 mac_addr_fixed
, (const uchar_t
*)mac_addr
, lacp_mode
, lacp_timer
,
2301 if (status
!= DLADM_STATUS_OK
)
2302 die_dlerr(status
, "modify operation failed");
2307 do_up_aggr(int argc
, char *argv
[], const char *use
)
2309 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
2310 dladm_status_t status
;
2313 * get the key or the name of the aggregation (optional last argument)
2316 if ((status
= i_dladm_aggr_get_linkid(NULL
, argv
[1], &linkid
,
2317 DLADM_OPT_PERSIST
)) != DLADM_STATUS_OK
)
2319 } else if (argc
> 2) {
2323 status
= dladm_aggr_up(handle
, linkid
);
2325 if (status
!= DLADM_STATUS_OK
) {
2328 "could not bring up aggregation '%s'", argv
[1]);
2330 die_dlerr(status
, "could not bring aggregations up");
2336 do_create_vlan(int argc
, char *argv
[], const char *use
)
2339 char drv
[DLPI_LINKNAME_MAX
];
2341 datalink_id_t linkid
;
2342 datalink_id_t dev_linkid
;
2345 uint32_t flags
= (DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
);
2346 char *altroot
= NULL
;
2347 char vlan
[MAXLINKNAMELEN
];
2348 char propstr
[DLADM_STRSIZE
];
2349 dladm_arg_list_t
*proplist
= NULL
;
2350 dladm_status_t status
;
2353 bzero(propstr
, DLADM_STRSIZE
);
2355 while ((option
= getopt_long(argc
, argv
, ":tfR:l:v:p:",
2356 lopts
, NULL
)) != -1) {
2362 if (!str2int(optarg
, &vid
) || vid
< 1 || vid
> 4094)
2363 die("invalid VLAN identifier '%s'", optarg
);
2373 flags
&= ~DLADM_OPT_PERSIST
;
2379 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
2380 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
2382 die("property list too long '%s'", propstr
);
2385 flags
|= DLADM_OPT_FORCE
;
2388 die_opterr(optopt
, option
, use
);
2393 /* get vlan name if there is any */
2394 if ((vid
== 0) || (link
== NULL
) || (argc
- optind
> 1))
2397 if (optind
== (argc
- 1)) {
2398 if (strlcpy(vlan
, argv
[optind
], MAXLINKNAMELEN
) >=
2400 die("vlan name too long '%s'", argv
[optind
]);
2403 if ((dlpi_parselink(link
, drv
, &ppa
) != DLPI_SUCCESS
) ||
2405 (dlpi_makelink(vlan
, drv
, vid
* 1000 + ppa
) !=
2407 die("invalid link name '%s'", link
);
2411 if (altroot
!= NULL
)
2412 altroot_cmd(altroot
, argc
, argv
);
2414 if (dladm_name2info(handle
, link
, &dev_linkid
, NULL
, NULL
, NULL
) !=
2416 die("invalid link name '%s'", link
);
2419 if (dladm_parse_link_props(propstr
, &proplist
, B_FALSE
)
2421 die("invalid vlan property");
2423 status
= dladm_vlan_create(handle
, vlan
, dev_linkid
, vid
, proplist
,
2426 case DLADM_STATUS_OK
:
2429 case DLADM_STATUS_NOTSUP
:
2430 die("VLAN over '%s' may require lowered MTU; must use -f (see "
2434 case DLADM_STATUS_LINKBUSY
:
2435 die("VLAN over '%s' may not use default_tag ID "
2436 "(see dladm(8))", link
);
2440 die_dlerr(status
, "create operation failed");
2445 do_delete_vlan(int argc
, char *argv
[], const char *use
)
2448 uint32_t flags
= (DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
);
2449 char *altroot
= NULL
;
2450 datalink_id_t linkid
;
2451 dladm_status_t status
;
2454 while ((option
= getopt_long(argc
, argv
, ":R:t", lopts
, NULL
)) != -1) {
2457 flags
&= ~DLADM_OPT_PERSIST
;
2463 die_opterr(optopt
, option
, use
);
2468 /* get VLAN link name (required last argument) */
2469 if (optind
!= (argc
- 1))
2472 if (altroot
!= NULL
)
2473 altroot_cmd(altroot
, argc
, argv
);
2475 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
2477 if (status
!= DLADM_STATUS_OK
)
2480 status
= dladm_vlan_delete(handle
, linkid
, flags
);
2482 if (status
!= DLADM_STATUS_OK
)
2483 die_dlerr(status
, "delete operation failed");
2488 do_up_vlan(int argc
, char *argv
[], const char *use
)
2490 do_up_vnic_common(argc
, argv
, use
, B_TRUE
);
2494 do_rename_link(int argc
, char *argv
[], const char *use
)
2497 char *link1
, *link2
;
2498 char *altroot
= NULL
;
2499 dladm_status_t status
;
2502 while ((option
= getopt_long(argc
, argv
, ":R:", lopts
, NULL
)) != -1) {
2508 die_opterr(optopt
, option
, use
);
2513 /* get link1 and link2 name (required the last 2 arguments) */
2514 if (optind
!= (argc
- 2))
2517 if (altroot
!= NULL
)
2518 altroot_cmd(altroot
, argc
, argv
);
2520 link1
= argv
[optind
++];
2521 link2
= argv
[optind
];
2522 if ((status
= dladm_rename_link(handle
, link1
, link2
)) !=
2524 die_dlerr(status
, "rename operation failed");
2529 do_delete_phys(int argc
, char *argv
[], const char *use
)
2531 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
2532 dladm_status_t status
;
2534 /* get link name (required the last argument) */
2539 if ((status
= dladm_name2info(handle
, argv
[1], &linkid
, NULL
,
2540 NULL
, NULL
)) != DLADM_STATUS_OK
)
2541 die_dlerr(status
, "cannot delete '%s'", argv
[1]);
2544 if ((status
= dladm_phys_delete(handle
, linkid
)) != DLADM_STATUS_OK
) {
2546 die_dlerr(status
, "cannot delete '%s'", argv
[1]);
2548 die_dlerr(status
, "delete operation failed");
2554 i_dladm_walk_linkmap(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
2556 char name
[MAXLINKNAMELEN
];
2557 char mediabuf
[DLADM_STRSIZE
];
2558 char classbuf
[DLADM_STRSIZE
];
2559 datalink_class_t
class;
2563 if (dladm_datalink_id2info(dh
, linkid
, &flags
, &class, &media
, name
,
2564 MAXLINKNAMELEN
) == DLADM_STATUS_OK
) {
2565 (void) dladm_class2str(class, classbuf
);
2566 (void) dladm_media2str(media
, mediabuf
);
2567 (void) printf("%-12s%8d %-12s%-20s %6d\n", name
,
2568 linkid
, classbuf
, mediabuf
, flags
);
2570 return (DLADM_WALK_CONTINUE
);
2575 do_show_linkmap(int argc
, char *argv
[], const char *use
)
2578 die("invalid arguments");
2580 (void) printf("%-12s%8s %-12s%-20s %6s\n", "NAME", "LINKID",
2581 "CLASS", "MEDIA", "FLAGS");
2583 (void) dladm_walk_datalink_id(i_dladm_walk_linkmap
, handle
, NULL
,
2584 DATALINK_CLASS_ALL
, DATALINK_ANY_MEDIATYPE
,
2585 DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
);
2589 * Delete inactive physical links.
2593 purge_phys(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
2595 datalink_class_t
class;
2598 if (dladm_datalink_id2info(dh
, linkid
, &flags
, &class, NULL
, NULL
, 0)
2599 != DLADM_STATUS_OK
) {
2600 return (DLADM_WALK_CONTINUE
);
2603 if (class == DATALINK_CLASS_PHYS
&& !(flags
& DLADM_OPT_ACTIVE
))
2604 (void) dladm_phys_delete(dh
, linkid
);
2606 return (DLADM_WALK_CONTINUE
);
2611 do_init_phys(int argc
, char *argv
[], const char *use
)
2619 * Force all the devices to attach, therefore all the network physical
2620 * devices can be known to the dlmgmtd daemon.
2622 if ((devtree
= di_init("/", DINFOFORCE
| DINFOSUBTREE
)) != DI_NODE_NIL
)
2625 (void) dladm_walk_datalink_id(purge_phys
, handle
, NULL
,
2626 DATALINK_CLASS_PHYS
, DATALINK_ANY_MEDIATYPE
, DLADM_OPT_PERSIST
);
2630 * Print the active topology information.
2633 print_link_topology(show_state_t
*state
, datalink_id_t linkid
,
2634 datalink_class_t
class, link_fields_buf_t
*lbuf
)
2636 uint32_t flags
= state
->ls_flags
;
2637 dladm_status_t status
;
2638 char tmpbuf
[MAXLINKNAMELEN
];
2640 lbuf
->link_over
[0] = '\0';
2641 lbuf
->link_bridge
[0] = '\0';
2644 case DATALINK_CLASS_AGGR
:
2645 case DATALINK_CLASS_PHYS
:
2646 case DATALINK_CLASS_ETHERSTUB
:
2647 status
= dladm_bridge_getlink(handle
, linkid
, lbuf
->link_bridge
,
2648 sizeof (lbuf
->link_bridge
));
2649 if (status
!= DLADM_STATUS_OK
&&
2650 status
!= DLADM_STATUS_NOTFOUND
)
2651 (void) strcpy(lbuf
->link_bridge
, "?");
2656 case DATALINK_CLASS_VLAN
: {
2657 dladm_vlan_attr_t vinfo
;
2659 if (dladm_vlan_info(handle
, linkid
, &vinfo
, flags
) !=
2661 (void) strcpy(lbuf
->link_over
, "?");
2664 if (dladm_datalink_id2info(handle
, vinfo
.dv_linkid
, NULL
, NULL
,
2665 NULL
, lbuf
->link_over
, sizeof (lbuf
->link_over
)) !=
2667 (void) strcpy(lbuf
->link_over
, "?");
2670 case DATALINK_CLASS_AGGR
: {
2671 dladm_aggr_grp_attr_t ginfo
;
2674 if (dladm_aggr_info(handle
, linkid
, &ginfo
, flags
) !=
2675 DLADM_STATUS_OK
|| ginfo
.lg_nports
== 0) {
2676 (void) strcpy(lbuf
->link_over
, "?");
2679 for (i
= 0; i
< ginfo
.lg_nports
; i
++) {
2680 if (dladm_datalink_id2info(handle
,
2681 ginfo
.lg_ports
[i
].lp_linkid
, NULL
, NULL
, NULL
,
2682 tmpbuf
, sizeof (tmpbuf
)) != DLADM_STATUS_OK
) {
2683 (void) strcpy(lbuf
->link_over
, "?");
2686 (void) strlcat(lbuf
->link_over
, tmpbuf
,
2687 sizeof (lbuf
->link_over
));
2688 if (i
!= (ginfo
.lg_nports
- 1)) {
2689 (void) strlcat(lbuf
->link_over
, ",",
2690 sizeof (lbuf
->link_over
));
2693 free(ginfo
.lg_ports
);
2696 case DATALINK_CLASS_VNIC
: {
2697 dladm_vnic_attr_t vinfo
;
2699 if (dladm_vnic_info(handle
, linkid
, &vinfo
, flags
) !=
2701 (void) strcpy(lbuf
->link_over
, "?");
2704 if (dladm_datalink_id2info(handle
, vinfo
.va_link_id
, NULL
, NULL
,
2705 NULL
, lbuf
->link_over
, sizeof (lbuf
->link_over
)) !=
2707 (void) strcpy(lbuf
->link_over
, "?");
2711 case DATALINK_CLASS_PART
: {
2712 dladm_part_attr_t pinfo
;
2714 if (dladm_part_info(handle
, linkid
, &pinfo
, flags
) !=
2716 (void) strcpy(lbuf
->link_over
, "?");
2719 if (dladm_datalink_id2info(handle
, pinfo
.dia_physlinkid
, NULL
,
2720 NULL
, NULL
, lbuf
->link_over
, sizeof (lbuf
->link_over
)) !=
2722 (void) strcpy(lbuf
->link_over
, "?");
2726 case DATALINK_CLASS_BRIDGE
: {
2730 if (dladm_datalink_id2info(handle
, linkid
, NULL
, NULL
,
2731 NULL
, tmpbuf
, sizeof (tmpbuf
)) != DLADM_STATUS_OK
) {
2732 (void) strcpy(lbuf
->link_over
, "?");
2735 if (tmpbuf
[0] != '\0')
2736 tmpbuf
[strlen(tmpbuf
) - 1] = '\0';
2737 dlp
= dladm_bridge_get_portlist(tmpbuf
, &nports
);
2739 (void) strcpy(lbuf
->link_over
, "?");
2742 for (i
= 0; i
< nports
; i
++) {
2743 if (dladm_datalink_id2info(handle
, dlp
[i
], NULL
,
2744 NULL
, NULL
, tmpbuf
, sizeof (tmpbuf
)) !=
2746 (void) strcpy(lbuf
->link_over
, "?");
2749 (void) strlcat(lbuf
->link_over
, tmpbuf
,
2750 sizeof (lbuf
->link_over
));
2751 if (i
!= nports
- 1) {
2752 (void) strlcat(lbuf
->link_over
, ",",
2753 sizeof (lbuf
->link_over
));
2756 dladm_bridge_free_portlist(dlp
);
2760 case DATALINK_CLASS_SIMNET
: {
2761 dladm_simnet_attr_t slinfo
;
2763 if (dladm_simnet_info(handle
, linkid
, &slinfo
, flags
) !=
2765 (void) strcpy(lbuf
->link_over
, "?");
2768 if (slinfo
.sna_peer_link_id
!= DATALINK_INVALID_LINKID
) {
2769 if (dladm_datalink_id2info(handle
,
2770 slinfo
.sna_peer_link_id
, NULL
, NULL
, NULL
,
2771 lbuf
->link_over
, sizeof (lbuf
->link_over
)) !=
2773 (void) strcpy(lbuf
->link_over
, "?");
2780 static dladm_status_t
2781 print_link(show_state_t
*state
, datalink_id_t linkid
, link_fields_buf_t
*lbuf
)
2783 char link
[MAXLINKNAMELEN
];
2784 datalink_class_t
class;
2787 dladm_status_t status
;
2789 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
2790 NULL
, link
, sizeof (link
))) != DLADM_STATUS_OK
) {
2794 if (!(state
->ls_flags
& flags
)) {
2795 status
= DLADM_STATUS_NOTFOUND
;
2799 if (state
->ls_flags
== DLADM_OPT_ACTIVE
) {
2800 dladm_attr_t dlattr
;
2802 if (class == DATALINK_CLASS_PHYS
) {
2803 dladm_phys_attr_t dpa
;
2807 if ((status
= dladm_phys_info(handle
, linkid
, &dpa
,
2808 DLADM_OPT_ACTIVE
)) != DLADM_STATUS_OK
) {
2812 if (!dpa
.dp_novanity
)
2816 * This is a physical link that does not have
2817 * vanity naming support.
2819 if (dlpi_open(dpa
.dp_dev
, &dh
, DLPI_DEVONLY
) !=
2821 status
= DLADM_STATUS_NOTFOUND
;
2825 if (dlpi_info(dh
, &dlinfo
, 0) != DLPI_SUCCESS
) {
2827 status
= DLADM_STATUS_BADARG
;
2832 mtu
= dlinfo
.di_max_sdu
;
2835 status
= dladm_info(handle
, linkid
, &dlattr
);
2836 if (status
!= DLADM_STATUS_OK
)
2838 mtu
= dlattr
.da_max_sdu
;
2842 (void) snprintf(lbuf
->link_name
, sizeof (lbuf
->link_name
),
2844 (void) dladm_class2str(class, lbuf
->link_class
);
2845 if (state
->ls_flags
== DLADM_OPT_ACTIVE
) {
2846 (void) snprintf(lbuf
->link_mtu
, sizeof (lbuf
->link_mtu
),
2848 (void) get_linkstate(link
, B_TRUE
, lbuf
->link_state
);
2851 print_link_topology(state
, linkid
, class, lbuf
);
2858 show_link(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
2860 show_state_t
*state
= (show_state_t
*)arg
;
2861 dladm_status_t status
;
2862 link_fields_buf_t lbuf
;
2865 * first get all the link attributes into lbuf;
2867 bzero(&lbuf
, sizeof (link_fields_buf_t
));
2868 if ((status
= print_link(state
, linkid
, &lbuf
)) == DLADM_STATUS_OK
)
2869 ofmt_print(state
->ls_ofmt
, &lbuf
);
2870 state
->ls_status
= status
;
2871 return (DLADM_WALK_CONTINUE
);
2875 print_link_stats_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
2877 link_args_t
*largs
= ofarg
->ofmt_cbarg
;
2878 pktsum_t
*diff_stats
= largs
->link_s_psum
;
2880 switch (ofarg
->ofmt_id
) {
2882 (void) snprintf(buf
, bufsize
, "%s", largs
->link_s_link
);
2885 (void) snprintf(buf
, bufsize
, "%llu", diff_stats
->ipackets
);
2888 (void) snprintf(buf
, bufsize
, "%llu", diff_stats
->rbytes
);
2890 case LINK_S_IERRORS
:
2891 (void) snprintf(buf
, bufsize
, "%u", diff_stats
->ierrors
);
2894 (void) snprintf(buf
, bufsize
, "%llu", diff_stats
->opackets
);
2897 (void) snprintf(buf
, bufsize
, "%llu", diff_stats
->obytes
);
2899 case LINK_S_OERRORS
:
2900 (void) snprintf(buf
, bufsize
, "%u", diff_stats
->oerrors
);
2903 die("invalid input");
2910 show_link_stats(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
2912 char link
[DLPI_LINKNAME_MAX
];
2913 datalink_class_t
class;
2914 show_state_t
*state
= arg
;
2915 pktsum_t stats
, diff_stats
;
2916 dladm_phys_attr_t dpa
;
2919 if (state
->ls_firstonly
) {
2920 if (state
->ls_donefirst
)
2921 return (DLADM_WALK_CONTINUE
);
2922 state
->ls_donefirst
= B_TRUE
;
2924 bzero(&state
->ls_prevstats
, sizeof (state
->ls_prevstats
));
2927 if (dladm_datalink_id2info(dh
, linkid
, NULL
, &class, NULL
, link
,
2928 DLPI_LINKNAME_MAX
) != DLADM_STATUS_OK
) {
2929 return (DLADM_WALK_CONTINUE
);
2932 if (class == DATALINK_CLASS_PHYS
) {
2933 if (dladm_phys_info(dh
, linkid
, &dpa
, DLADM_OPT_ACTIVE
) !=
2935 return (DLADM_WALK_CONTINUE
);
2937 if (dpa
.dp_novanity
)
2938 get_mac_stats(dpa
.dp_dev
, &stats
);
2940 get_link_stats(link
, &stats
);
2942 get_link_stats(link
, &stats
);
2944 dladm_stats_diff(&diff_stats
, &stats
, &state
->ls_prevstats
);
2946 largs
.link_s_link
= link
;
2947 largs
.link_s_psum
= &diff_stats
;
2948 ofmt_print(state
->ls_ofmt
, &largs
);
2950 state
->ls_prevstats
= stats
;
2951 return (DLADM_WALK_CONTINUE
);
2955 static dladm_status_t
2956 print_aggr_info(show_grp_state_t
*state
, const char *link
,
2957 dladm_aggr_grp_attr_t
*ginfop
)
2959 char addr_str
[ETHERADDRL
* 3];
2960 laggr_fields_buf_t lbuf
;
2962 (void) snprintf(lbuf
.laggr_name
, sizeof (lbuf
.laggr_name
),
2965 (void) dladm_aggr_policy2str(ginfop
->lg_policy
,
2968 if (ginfop
->lg_mac_fixed
) {
2969 (void) dladm_aggr_macaddr2str(ginfop
->lg_mac
, addr_str
);
2970 (void) snprintf(lbuf
.laggr_addrpolicy
,
2971 sizeof (lbuf
.laggr_addrpolicy
), "fixed (%s)", addr_str
);
2973 (void) snprintf(lbuf
.laggr_addrpolicy
,
2974 sizeof (lbuf
.laggr_addrpolicy
), "auto");
2977 (void) dladm_aggr_lacpmode2str(ginfop
->lg_lacp_mode
,
2978 lbuf
.laggr_lacpactivity
);
2979 (void) dladm_aggr_lacptimer2str(ginfop
->lg_lacp_timer
,
2980 lbuf
.laggr_lacptimer
);
2981 (void) snprintf(lbuf
.laggr_flags
, sizeof (lbuf
.laggr_flags
), "%c----",
2982 ginfop
->lg_force
? 'f' : '-');
2984 ofmt_print(state
->gs_ofmt
, &lbuf
);
2986 return (DLADM_STATUS_OK
);
2990 print_xaggr_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
2992 const laggr_args_t
*l
= ofarg
->ofmt_cbarg
;
2993 boolean_t is_port
= (l
->laggr_lport
>= 0);
2994 char tmpbuf
[DLADM_STRSIZE
];
2995 const char *objname
;
2996 dladm_aggr_port_attr_t
*portp
;
2997 dladm_phys_attr_t dpa
;
3000 portp
= &(l
->laggr_ginfop
->lg_ports
[l
->laggr_lport
]);
3001 if (dladm_phys_info(handle
, portp
->lp_linkid
, &dpa
,
3002 DLADM_OPT_ACTIVE
) != DLADM_STATUS_OK
)
3005 objname
= dpa
.dp_dev
;
3007 objname
= l
->laggr_link
;
3010 switch (ofarg
->ofmt_id
) {
3012 (void) snprintf(buf
, bufsize
, "%s",
3013 (is_port
&& !l
->laggr_parsable
? " " : l
->laggr_link
));
3017 if (dladm_datalink_id2info(handle
, portp
->lp_linkid
,
3018 NULL
, NULL
, NULL
, buf
, bufsize
) != DLADM_STATUS_OK
)
3019 (void) sprintf(buf
, "?");
3024 (void) snprintf(buf
, bufsize
, "%uMb",
3025 (uint_t
)((get_ifspeed(objname
, !is_port
)) / 1000000ull));
3029 (void) get_linkduplex(objname
, !is_port
, tmpbuf
);
3030 (void) strlcpy(buf
, tmpbuf
, bufsize
);
3034 (void) get_linkstate(objname
, !is_port
, tmpbuf
);
3035 (void) strlcpy(buf
, tmpbuf
, bufsize
);
3037 case AGGR_X_ADDRESS
:
3038 (void) dladm_aggr_macaddr2str(
3039 (is_port
? portp
->lp_mac
: l
->laggr_ginfop
->lg_mac
),
3041 (void) strlcpy(buf
, tmpbuf
, bufsize
);
3043 case AGGR_X_PORTSTATE
:
3045 (void) dladm_aggr_portstate2str(portp
->lp_state
,
3047 (void) strlcpy(buf
, tmpbuf
, bufsize
);
3052 *(l
->laggr_status
) = DLADM_STATUS_OK
;
3056 static dladm_status_t
3057 print_aggr_extended(show_grp_state_t
*state
, const char *link
,
3058 dladm_aggr_grp_attr_t
*ginfop
)
3061 dladm_status_t status
;
3064 largs
.laggr_lport
= -1;
3065 largs
.laggr_link
= link
;
3066 largs
.laggr_ginfop
= ginfop
;
3067 largs
.laggr_status
= &status
;
3068 largs
.laggr_parsable
= state
->gs_parsable
;
3070 ofmt_print(state
->gs_ofmt
, &largs
);
3072 if (status
!= DLADM_STATUS_OK
)
3075 for (i
= 0; i
< ginfop
->lg_nports
; i
++) {
3076 largs
.laggr_lport
= i
;
3077 ofmt_print(state
->gs_ofmt
, &largs
);
3078 if (status
!= DLADM_STATUS_OK
)
3082 status
= DLADM_STATUS_OK
;
3088 print_lacp_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
3090 const laggr_args_t
*l
= ofarg
->ofmt_cbarg
;
3092 boolean_t is_port
= (l
->laggr_lport
>= 0);
3093 dladm_aggr_port_attr_t
*portp
;
3094 aggr_lacp_state_t
*lstate
;
3097 return (B_FALSE
); /* cannot happen! */
3099 portnum
= l
->laggr_lport
;
3100 portp
= &(l
->laggr_ginfop
->lg_ports
[portnum
]);
3101 lstate
= &(portp
->lp_lacp_state
);
3103 switch (ofarg
->ofmt_id
) {
3105 (void) snprintf(buf
, bufsize
, "%s",
3106 (portnum
> 0 ? "" : l
->laggr_link
));
3110 if (dladm_datalink_id2info(handle
, portp
->lp_linkid
, NULL
, NULL
,
3111 NULL
, buf
, bufsize
) != DLADM_STATUS_OK
)
3112 (void) sprintf(buf
, "?");
3115 case AGGR_L_AGGREGATABLE
:
3116 (void) snprintf(buf
, bufsize
, "%s",
3117 (lstate
->bit
.aggregation
? "yes" : "no"));
3121 (void) snprintf(buf
, bufsize
, "%s",
3122 (lstate
->bit
.sync
? "yes" : "no"));
3126 (void) snprintf(buf
, bufsize
, "%s",
3127 (lstate
->bit
.collecting
? "yes" : "no"));
3131 (void) snprintf(buf
, bufsize
, "%s",
3132 (lstate
->bit
.distributing
? "yes" : "no"));
3135 case AGGR_L_DEFAULTED
:
3136 (void) snprintf(buf
, bufsize
, "%s",
3137 (lstate
->bit
.defaulted
? "yes" : "no"));
3140 case AGGR_L_EXPIRED
:
3141 (void) snprintf(buf
, bufsize
, "%s",
3142 (lstate
->bit
.expired
? "yes" : "no"));
3146 *(l
->laggr_status
) = DLADM_STATUS_OK
;
3150 static dladm_status_t
3151 print_aggr_lacp(show_grp_state_t
*state
, const char *link
,
3152 dladm_aggr_grp_attr_t
*ginfop
)
3155 dladm_status_t status
;
3158 largs
.laggr_link
= link
;
3159 largs
.laggr_ginfop
= ginfop
;
3160 largs
.laggr_status
= &status
;
3162 for (i
= 0; i
< ginfop
->lg_nports
; i
++) {
3163 largs
.laggr_lport
= i
;
3164 ofmt_print(state
->gs_ofmt
, &largs
);
3165 if (status
!= DLADM_STATUS_OK
)
3169 status
= DLADM_STATUS_OK
;
3175 print_aggr_stats_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
3177 const laggr_args_t
*l
= ofarg
->ofmt_cbarg
;
3179 boolean_t is_port
= (l
->laggr_lport
>= 0);
3180 dladm_aggr_port_attr_t
*portp
;
3181 dladm_status_t
*stat
, status
;
3182 pktsum_t
*diff_stats
;
3184 stat
= l
->laggr_status
;
3185 *stat
= DLADM_STATUS_OK
;
3188 portnum
= l
->laggr_lport
;
3189 portp
= &(l
->laggr_ginfop
->lg_ports
[portnum
]);
3191 if ((status
= dladm_datalink_id2info(handle
,
3192 portp
->lp_linkid
, NULL
, NULL
, NULL
, buf
, bufsize
)) !=
3196 diff_stats
= l
->laggr_diffstats
;
3199 switch (ofarg
->ofmt_id
) {
3201 (void) snprintf(buf
, bufsize
, "%s",
3202 (is_port
? "" : l
->laggr_link
));
3206 * if (is_port), buf has port name. Otherwise we print
3213 (void) snprintf(buf
, bufsize
, "%llu",
3214 diff_stats
->ipackets
);
3216 (void) snprintf(buf
, bufsize
, "%llu",
3217 l
->laggr_pktsumtot
->ipackets
);
3223 (void) snprintf(buf
, bufsize
, "%llu",
3224 diff_stats
->rbytes
);
3226 (void) snprintf(buf
, bufsize
, "%llu",
3227 l
->laggr_pktsumtot
->rbytes
);
3233 (void) snprintf(buf
, bufsize
, "%llu",
3234 diff_stats
->opackets
);
3236 (void) snprintf(buf
, bufsize
, "%llu",
3237 l
->laggr_pktsumtot
->opackets
);
3242 (void) snprintf(buf
, bufsize
, "%llu",
3243 diff_stats
->obytes
);
3245 (void) snprintf(buf
, bufsize
, "%llu",
3246 l
->laggr_pktsumtot
->obytes
);
3250 case AGGR_S_IPKTDIST
:
3252 (void) snprintf(buf
, bufsize
, "%-6.1f",
3253 (double)diff_stats
->ipackets
/
3254 (double)l
->laggr_pktsumtot
->ipackets
* 100);
3257 case AGGR_S_OPKTDIST
:
3259 (void) snprintf(buf
, bufsize
, "%-6.1f",
3260 (double)diff_stats
->opackets
/
3261 (double)l
->laggr_pktsumtot
->opackets
* 100);
3272 static dladm_status_t
3273 print_aggr_stats(show_grp_state_t
*state
, const char *link
,
3274 dladm_aggr_grp_attr_t
*ginfop
)
3276 dladm_phys_attr_t dpa
;
3277 dladm_aggr_port_attr_t
*portp
;
3278 pktsum_t pktsumtot
, *port_stat
;
3279 dladm_status_t status
;
3283 /* sum the ports statistics */
3284 bzero(&pktsumtot
, sizeof (pktsumtot
));
3286 /* Allocate memory to keep stats of each port */
3287 port_stat
= malloc(ginfop
->lg_nports
* sizeof (pktsum_t
));
3288 if (port_stat
== NULL
) {
3289 /* Bail out; no memory */
3290 return (DLADM_STATUS_NOMEM
);
3294 for (i
= 0; i
< ginfop
->lg_nports
; i
++) {
3296 portp
= &(ginfop
->lg_ports
[i
]);
3297 if ((status
= dladm_phys_info(handle
, portp
->lp_linkid
, &dpa
,
3298 DLADM_OPT_ACTIVE
)) != DLADM_STATUS_OK
) {
3302 get_mac_stats(dpa
.dp_dev
, &port_stat
[i
]);
3305 * Let's re-use gs_prevstats[] to store the difference of the
3306 * counters since last use. We will store the new stats from
3307 * port_stat[] once we have the stats displayed.
3310 dladm_stats_diff(&state
->gs_prevstats
[i
], &port_stat
[i
],
3311 &state
->gs_prevstats
[i
]);
3312 dladm_stats_total(&pktsumtot
, &pktsumtot
,
3313 &state
->gs_prevstats
[i
]);
3316 largs
.laggr_lport
= -1;
3317 largs
.laggr_link
= link
;
3318 largs
.laggr_ginfop
= ginfop
;
3319 largs
.laggr_status
= &status
;
3320 largs
.laggr_pktsumtot
= &pktsumtot
;
3322 ofmt_print(state
->gs_ofmt
, &largs
);
3324 if (status
!= DLADM_STATUS_OK
)
3327 for (i
= 0; i
< ginfop
->lg_nports
; i
++) {
3328 largs
.laggr_lport
= i
;
3329 largs
.laggr_diffstats
= &state
->gs_prevstats
[i
];
3330 ofmt_print(state
->gs_ofmt
, &largs
);
3331 if (status
!= DLADM_STATUS_OK
)
3335 status
= DLADM_STATUS_OK
;
3336 for (i
= 0; i
< ginfop
->lg_nports
; i
++)
3337 state
->gs_prevstats
[i
] = port_stat
[i
];
3344 static dladm_status_t
3345 print_aggr(show_grp_state_t
*state
, datalink_id_t linkid
)
3347 char link
[MAXLINKNAMELEN
];
3348 dladm_aggr_grp_attr_t ginfo
;
3350 dladm_status_t status
;
3352 bzero(&ginfo
, sizeof (dladm_aggr_grp_attr_t
));
3353 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, NULL
,
3354 NULL
, link
, MAXLINKNAMELEN
)) != DLADM_STATUS_OK
) {
3358 if (!(state
->gs_flags
& flags
))
3359 return (DLADM_STATUS_NOTFOUND
);
3361 status
= dladm_aggr_info(handle
, linkid
, &ginfo
, state
->gs_flags
);
3362 if (status
!= DLADM_STATUS_OK
)
3366 status
= print_aggr_lacp(state
, link
, &ginfo
);
3367 else if (state
->gs_extended
)
3368 status
= print_aggr_extended(state
, link
, &ginfo
);
3369 else if (state
->gs_stats
)
3370 status
= print_aggr_stats(state
, link
, &ginfo
);
3372 status
= print_aggr_info(state
, link
, &ginfo
);
3375 free(ginfo
.lg_ports
);
3381 show_aggr(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
3383 show_grp_state_t
*state
= arg
;
3385 state
->gs_status
= print_aggr(state
, linkid
);
3386 return (DLADM_WALK_CONTINUE
);
3390 do_show_link(int argc
, char *argv
[], const char *use
)
3393 boolean_t s_arg
= B_FALSE
;
3394 boolean_t i_arg
= B_FALSE
;
3395 uint32_t flags
= DLADM_OPT_ACTIVE
;
3396 boolean_t p_arg
= B_FALSE
;
3397 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
3398 char linkname
[MAXLINKNAMELEN
];
3399 uint32_t interval
= 0;
3401 dladm_status_t status
;
3402 boolean_t o_arg
= B_FALSE
;
3403 char *fields_str
= NULL
;
3404 char *all_active_fields
= "link,class,mtu,state,bridge,over";
3405 char *all_inactive_fields
= "link,class,bridge,over";
3406 char *allstat_fields
=
3407 "link,ipackets,rbytes,ierrors,opackets,obytes,oerrors";
3409 ofmt_status_t oferr
;
3410 uint_t ofmtflags
= 0;
3412 bzero(&state
, sizeof (state
));
3415 while ((option
= getopt_long(argc
, argv
, ":pPsi:o:",
3416 show_lopts
, NULL
)) != -1) {
3431 if (flags
!= DLADM_OPT_ACTIVE
)
3434 flags
= DLADM_OPT_PERSIST
;
3438 fields_str
= optarg
;
3445 if (!dladm_str2interval(optarg
, &interval
))
3446 die("invalid interval value '%s'", optarg
);
3449 die_opterr(optopt
, option
, use
);
3454 if (i_arg
&& !s_arg
)
3455 die("the option -i can be used only with -s");
3457 if (s_arg
&& flags
!= DLADM_OPT_ACTIVE
)
3458 die("the option -P cannot be used with -s");
3460 /* get link name (optional last argument) */
3461 if (optind
== (argc
-1)) {
3464 if (strlcpy(linkname
, argv
[optind
], MAXLINKNAMELEN
) >=
3466 die("link name too long");
3467 if ((status
= dladm_name2info(handle
, linkname
, &linkid
, &f
,
3468 NULL
, NULL
)) != DLADM_STATUS_OK
) {
3469 die_dlerr(status
, "link %s is not valid", linkname
);
3473 die_dlerr(DLADM_STATUS_BADARG
, "link %s is %s",
3474 argv
[optind
], flags
== DLADM_OPT_PERSIST
?
3475 "a temporary link" : "temporarily removed");
3477 } else if (optind
!= argc
) {
3481 if (p_arg
&& !o_arg
)
3482 die("-p requires -o");
3484 if (p_arg
&& strcasecmp(fields_str
, "all") == 0)
3485 die("\"-o all\" is invalid with -p");
3487 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0)) {
3489 fields_str
= allstat_fields
;
3490 else if (flags
& DLADM_OPT_ACTIVE
)
3491 fields_str
= all_active_fields
;
3493 fields_str
= all_inactive_fields
;
3496 state
.ls_parsable
= p_arg
;
3497 state
.ls_flags
= flags
;
3498 state
.ls_donefirst
= B_FALSE
;
3501 link_stats(linkid
, interval
, fields_str
, &state
);
3504 if (state
.ls_parsable
)
3505 ofmtflags
|= OFMT_PARSABLE
;
3507 ofmtflags
|= OFMT_WRAP
;
3509 oferr
= ofmt_open(fields_str
, link_fields
, ofmtflags
, 0, &ofmt
);
3510 ofmt_check(oferr
, state
.ls_parsable
, ofmt
, die
, warn
);
3511 state
.ls_ofmt
= ofmt
;
3513 if (linkid
== DATALINK_ALL_LINKID
) {
3514 (void) dladm_walk_datalink_id(show_link
, handle
, &state
,
3515 DATALINK_CLASS_ALL
, DATALINK_ANY_MEDIATYPE
, flags
);
3517 (void) show_link(handle
, linkid
, &state
);
3518 if (state
.ls_status
!= DLADM_STATUS_OK
) {
3519 die_dlerr(state
.ls_status
, "failed to show link %s",
3527 do_show_aggr(int argc
, char *argv
[], const char *use
)
3529 boolean_t L_arg
= B_FALSE
;
3530 boolean_t s_arg
= B_FALSE
;
3531 boolean_t i_arg
= B_FALSE
;
3532 boolean_t p_arg
= B_FALSE
;
3533 boolean_t x_arg
= B_FALSE
;
3534 show_grp_state_t state
;
3535 uint32_t flags
= DLADM_OPT_ACTIVE
;
3536 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
3538 uint32_t interval
= 0;
3540 dladm_status_t status
;
3541 boolean_t o_arg
= B_FALSE
;
3542 char *fields_str
= NULL
;
3544 "link,policy,addrpolicy,lacpactivity,lacptimer,flags";
3545 char *all_lacp_fields
=
3546 "link,port,aggregatable,sync,coll,dist,defaulted,expired";
3547 char *all_stats_fields
=
3548 "link,port,ipackets,rbytes,opackets,obytes,ipktdist,opktdist";
3549 char *all_extended_fields
=
3550 "link,port,speed,duplex,state,address,portstate";
3551 const ofmt_field_t
*pf
;
3553 ofmt_status_t oferr
;
3554 uint_t ofmtflags
= 0;
3557 while ((option
= getopt_long(argc
, argv
, ":LpPxsi:o:",
3558 show_lopts
, NULL
)) != -1) {
3579 if (flags
!= DLADM_OPT_ACTIVE
)
3582 flags
= DLADM_OPT_PERSIST
;
3592 fields_str
= optarg
;
3599 if (!dladm_str2interval(optarg
, &interval
))
3600 die("invalid interval value '%s'", optarg
);
3603 die_opterr(optopt
, option
, use
);
3608 if (p_arg
&& !o_arg
)
3609 die("-p requires -o");
3611 if (p_arg
&& strcasecmp(fields_str
, "all") == 0)
3612 die("\"-o all\" is invalid with -p");
3614 if (i_arg
&& !s_arg
)
3615 die("the option -i can be used only with -s");
3617 if (s_arg
&& (L_arg
|| p_arg
|| x_arg
|| flags
!= DLADM_OPT_ACTIVE
)) {
3618 die("the option -%c cannot be used with -s",
3619 L_arg
? 'L' : (p_arg
? 'p' : (x_arg
? 'x' : 'P')));
3622 if (L_arg
&& flags
!= DLADM_OPT_ACTIVE
)
3623 die("the option -P cannot be used with -L");
3625 if (x_arg
&& (L_arg
|| flags
!= DLADM_OPT_ACTIVE
))
3626 die("the option -%c cannot be used with -x", L_arg
? 'L' : 'P');
3628 /* get aggregation key or aggrname (optional last argument) */
3629 if (optind
== (argc
-1)) {
3630 if (!str2int(argv
[optind
], &key
)) {
3631 status
= dladm_name2info(handle
, argv
[optind
],
3632 &linkid
, NULL
, NULL
, NULL
);
3634 status
= dladm_key2linkid(handle
, (uint16_t)key
,
3635 &linkid
, DLADM_OPT_ACTIVE
);
3638 if (status
!= DLADM_STATUS_OK
)
3639 die("non-existent aggregation '%s'", argv
[optind
]);
3641 } else if (optind
!= argc
) {
3645 bzero(&state
, sizeof (state
));
3646 state
.gs_lacp
= L_arg
;
3647 state
.gs_stats
= s_arg
;
3648 state
.gs_flags
= flags
;
3649 state
.gs_parsable
= p_arg
;
3650 state
.gs_extended
= x_arg
;
3652 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0)) {
3654 fields_str
= all_lacp_fields
;
3655 else if (state
.gs_stats
)
3656 fields_str
= all_stats_fields
;
3657 else if (state
.gs_extended
)
3658 fields_str
= all_extended_fields
;
3660 fields_str
= all_fields
;
3663 if (state
.gs_lacp
) {
3665 } else if (state
.gs_stats
) {
3667 } else if (state
.gs_extended
) {
3673 if (state
.gs_parsable
)
3674 ofmtflags
|= OFMT_PARSABLE
;
3675 oferr
= ofmt_open(fields_str
, pf
, ofmtflags
, 0, &ofmt
);
3676 ofmt_check(oferr
, state
.gs_parsable
, ofmt
, die
, warn
);
3677 state
.gs_ofmt
= ofmt
;
3680 aggr_stats(linkid
, &state
, interval
);
3685 if (linkid
== DATALINK_ALL_LINKID
) {
3686 (void) dladm_walk_datalink_id(show_aggr
, handle
, &state
,
3687 DATALINK_CLASS_AGGR
, DATALINK_ANY_MEDIATYPE
, flags
);
3689 (void) show_aggr(handle
, linkid
, &state
);
3690 if (state
.gs_status
!= DLADM_STATUS_OK
) {
3691 die_dlerr(state
.gs_status
, "failed to show aggr %s",
3698 static dladm_status_t
3699 print_phys_default(show_state_t
*state
, datalink_id_t linkid
,
3700 const char *link
, uint32_t flags
, uint32_t media
)
3702 dladm_phys_attr_t dpa
;
3703 dladm_status_t status
;
3704 link_fields_buf_t pattr
;
3706 status
= dladm_phys_info(handle
, linkid
, &dpa
, state
->ls_flags
);
3707 if (status
!= DLADM_STATUS_OK
)
3710 bzero(&pattr
, sizeof (pattr
));
3711 (void) snprintf(pattr
.link_phys_device
,
3712 sizeof (pattr
.link_phys_device
), "%s", dpa
.dp_dev
);
3713 (void) dladm_media2str(media
, pattr
.link_phys_media
);
3714 if (state
->ls_flags
== DLADM_OPT_ACTIVE
) {
3717 if (!dpa
.dp_novanity
) {
3718 (void) strlcpy(pattr
.link_name
, link
,
3719 sizeof (pattr
.link_name
));
3723 * This is a physical link that does not have
3724 * vanity naming support.
3726 (void) strlcpy(pattr
.link_name
, dpa
.dp_dev
,
3727 sizeof (pattr
.link_name
));
3731 (void) get_linkstate(pattr
.link_name
, islink
,
3732 pattr
.link_phys_state
);
3733 (void) snprintf(pattr
.link_phys_speed
,
3734 sizeof (pattr
.link_phys_speed
), "%u",
3735 (uint_t
)((get_ifspeed(pattr
.link_name
,
3736 islink
)) / 1000000ull));
3737 (void) get_linkduplex(pattr
.link_name
, islink
,
3738 pattr
.link_phys_duplex
);
3740 (void) snprintf(pattr
.link_name
, sizeof (pattr
.link_name
),
3742 (void) snprintf(pattr
.link_flags
, sizeof (pattr
.link_flags
),
3743 "%c----", flags
& DLADM_OPT_ACTIVE
? '-' : 'r');
3746 ofmt_print(state
->ls_ofmt
, &pattr
);
3753 show_state_t
*ms_state
;
3755 dladm_macaddr_attr_t
*ms_mac_attr
;
3756 } print_phys_mac_state_t
;
3759 * callback for ofmt_print()
3762 print_phys_one_mac_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
3764 print_phys_mac_state_t
*mac_state
= ofarg
->ofmt_cbarg
;
3765 dladm_macaddr_attr_t
*attr
= mac_state
->ms_mac_attr
;
3766 boolean_t is_primary
= (attr
->ma_slot
== 0);
3767 boolean_t is_parsable
= mac_state
->ms_state
->ls_parsable
;
3769 switch (ofarg
->ofmt_id
) {
3771 (void) snprintf(buf
, bufsize
, "%s",
3772 (is_primary
|| is_parsable
) ? mac_state
->ms_link
: " ");
3776 (void) snprintf(buf
, bufsize
, gettext("primary"));
3778 (void) snprintf(buf
, bufsize
, "%d", attr
->ma_slot
);
3780 case PHYS_M_ADDRESS
:
3781 (void) dladm_aggr_macaddr2str(attr
->ma_addr
, buf
);
3784 (void) snprintf(buf
, bufsize
, "%s",
3785 attr
->ma_flags
& DLADM_MACADDR_USED
? gettext("yes") :
3790 * CR 6678526: resolve link id to actual link name if
3793 (void) snprintf(buf
, bufsize
, "%s", attr
->ma_client_name
);
3801 show_state_t
*hs_state
;
3803 dladm_hwgrp_attr_t
*hs_grp_attr
;
3804 } print_phys_hwgrp_state_t
;
3807 print_phys_one_hwgrp_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
3810 boolean_t first
= B_TRUE
;
3813 char ringstr
[RINGSTRLEN
];
3814 char ringsubstr
[RINGSTRLEN
];
3816 print_phys_hwgrp_state_t
*hg_state
= ofarg
->ofmt_cbarg
;
3817 dladm_hwgrp_attr_t
*attr
= hg_state
->hs_grp_attr
;
3819 switch (ofarg
->ofmt_id
) {
3821 (void) snprintf(buf
, bufsize
, "%s", attr
->hg_link_name
);
3823 case PHYS_H_RINGTYPE
:
3824 (void) snprintf(buf
, bufsize
, "%s",
3825 attr
->hg_grp_type
== DLADM_HWGRP_TYPE_RX
? "RX" : "TX");
3829 for (i
= 0; i
< attr
->hg_n_rings
; i
++) {
3830 uint_t index
= attr
->hg_rings
[i
];
3835 } else if (index
== end
+ 1) {
3867 (void) strlcat(ringstr
, ringsubstr
,
3877 (void) snprintf(buf
, bufsize
, "%d",
3880 (void) snprintf(buf
, bufsize
, "%d-%d",
3885 (void) snprintf(ringsubstr
, RINGSTRLEN
,
3888 (void) snprintf(ringsubstr
, RINGSTRLEN
,
3889 ",%d-%d", start
, end
);
3891 (void) strlcat(ringstr
, ringsubstr
, RINGSTRLEN
);
3892 (void) snprintf(buf
, bufsize
, "%s", ringstr
);
3896 case PHYS_H_CLIENTS
:
3897 if (attr
->hg_client_names
[0] == '\0') {
3898 (void) snprintf(buf
, bufsize
, "--");
3900 (void) snprintf(buf
, bufsize
, "%s ",
3901 attr
->hg_client_names
);
3910 * callback for dladm_walk_macaddr, invoked for each MAC address slot
3913 print_phys_mac_callback(void *arg
, dladm_macaddr_attr_t
*attr
)
3915 print_phys_mac_state_t
*mac_state
= arg
;
3916 show_state_t
*state
= mac_state
->ms_state
;
3918 mac_state
->ms_mac_attr
= attr
;
3919 ofmt_print(state
->ls_ofmt
, mac_state
);
3925 * invoked by show-phys -m for each physical data-link
3927 static dladm_status_t
3928 print_phys_mac(show_state_t
*state
, datalink_id_t linkid
, char *link
)
3930 print_phys_mac_state_t mac_state
;
3932 mac_state
.ms_state
= state
;
3933 mac_state
.ms_link
= link
;
3935 return (dladm_walk_macaddr(handle
, linkid
, &mac_state
,
3936 print_phys_mac_callback
));
3940 * callback for dladm_walk_hwgrp, invoked for each MAC hwgrp
3943 print_phys_hwgrp_callback(void *arg
, dladm_hwgrp_attr_t
*attr
)
3945 print_phys_hwgrp_state_t
*hwgrp_state
= arg
;
3946 show_state_t
*state
= hwgrp_state
->hs_state
;
3948 hwgrp_state
->hs_grp_attr
= attr
;
3949 ofmt_print(state
->ls_ofmt
, hwgrp_state
);
3954 /* invoked by show-phys -H for each physical data-link */
3955 static dladm_status_t
3956 print_phys_hwgrp(show_state_t
*state
, datalink_id_t linkid
, char *link
)
3958 print_phys_hwgrp_state_t hwgrp_state
;
3960 hwgrp_state
.hs_state
= state
;
3961 hwgrp_state
.hs_link
= link
;
3962 return (dladm_walk_hwgrp(handle
, linkid
, &hwgrp_state
,
3963 print_phys_hwgrp_callback
));
3967 * Parse the "local=<laddr>,remote=<raddr>" sub-options for the -a option of
3968 * *-iptun subcommands.
3971 iptun_process_addrarg(char *addrarg
, iptun_params_t
*params
)
3975 while (*addrarg
!= '\0') {
3976 switch (getsubopt(&addrarg
, iptun_addropts
, &addrval
)) {
3978 if (addrval
== NULL
)
3979 die("tunnel source address value is missing");
3980 params
->iptun_param_flags
|= IPTUN_PARAM_LADDR
;
3981 if (strlcpy(params
->iptun_param_laddr
, addrval
,
3982 sizeof (params
->iptun_param_laddr
)) >=
3983 sizeof (params
->iptun_param_laddr
))
3984 die("tunnel source address is too long");
3987 if (addrval
== NULL
)
3988 die("tunnel destination address value "
3990 params
->iptun_param_flags
|= IPTUN_PARAM_RADDR
;
3991 if (strlcpy(params
->iptun_param_raddr
, addrval
,
3992 sizeof (params
->iptun_param_raddr
)) >=
3993 sizeof (params
->iptun_param_raddr
))
3994 die("tunnel destination address is too long");
3997 die("invalid address type: %s", addrval
);
4004 * Convenience routine to process iptun-create/modify/delete subcommand
4008 iptun_process_args(int argc
, char *argv
[], const char *opts
,
4009 iptun_params_t
*params
, uint32_t *flags
, char *name
, const char *use
)
4012 char *altroot
= NULL
;
4015 bzero(params
, sizeof (*params
));
4016 *flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
4019 while ((option
= getopt_long(argc
, argv
, opts
, iptun_lopts
, NULL
)) !=
4023 iptun_process_addrarg(optarg
, params
);
4029 *flags
&= ~DLADM_OPT_PERSIST
;
4032 params
->iptun_param_type
= iptun_gettypebyname(optarg
);
4033 if (params
->iptun_param_type
== IPTUN_TYPE_UNKNOWN
)
4034 die("unknown tunnel type: %s", optarg
);
4035 params
->iptun_param_flags
|= IPTUN_PARAM_TYPE
;
4038 die_opterr(optopt
, option
, use
);
4043 /* Get the required tunnel name argument. */
4044 if (argc
- optind
!= 1)
4047 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
4048 die("tunnel name is too long");
4050 if (altroot
!= NULL
)
4051 altroot_cmd(altroot
, argc
, argv
);
4055 do_create_iptun(int argc
, char *argv
[], const char *use
)
4057 iptun_params_t params
;
4058 dladm_status_t status
;
4060 char name
[MAXLINKNAMELEN
];
4062 iptun_process_args(argc
, argv
, ":a:R:tT:", ¶ms
, &flags
, name
,
4065 status
= dladm_iptun_create(handle
, name
, ¶ms
, flags
);
4066 if (status
!= DLADM_STATUS_OK
)
4067 die_dlerr(status
, "could not create tunnel");
4071 do_delete_iptun(int argc
, char *argv
[], const char *use
)
4074 datalink_id_t linkid
;
4075 dladm_status_t status
;
4076 char name
[MAXLINKNAMELEN
];
4078 iptun_process_args(argc
, argv
, ":R:t", NULL
, &flags
, name
, use
);
4080 status
= dladm_name2info(handle
, name
, &linkid
, NULL
, NULL
, NULL
);
4081 if (status
!= DLADM_STATUS_OK
)
4082 die_dlerr(status
, "could not delete tunnel");
4083 status
= dladm_iptun_delete(handle
, linkid
, flags
);
4084 if (status
!= DLADM_STATUS_OK
)
4085 die_dlerr(status
, "could not delete tunnel");
4089 do_modify_iptun(int argc
, char *argv
[], const char *use
)
4091 iptun_params_t params
;
4093 dladm_status_t status
;
4094 char name
[MAXLINKNAMELEN
];
4096 iptun_process_args(argc
, argv
, ":a:R:t", ¶ms
, &flags
, name
, use
);
4098 if ((status
= dladm_name2info(handle
, name
, ¶ms
.iptun_param_linkid
,
4099 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
)
4100 die_dlerr(status
, "could not modify tunnel");
4101 status
= dladm_iptun_modify(handle
, ¶ms
, flags
);
4102 if (status
!= DLADM_STATUS_OK
)
4103 die_dlerr(status
, "could not modify tunnel");
4107 do_show_iptun(int argc
, char *argv
[], const char *use
)
4110 datalink_id_t linkid
;
4111 uint32_t flags
= DLADM_OPT_ACTIVE
;
4113 dladm_status_t status
;
4114 const char *fields_str
= NULL
;
4117 ofmt_status_t oferr
;
4118 uint_t ofmtflags
= 0;
4120 bzero(&state
, sizeof (state
));
4122 while ((option
= getopt_long(argc
, argv
, ":pPo:",
4123 iptun_lopts
, NULL
)) != -1) {
4126 fields_str
= optarg
;
4129 state
.ls_parsable
= B_TRUE
;
4130 ofmtflags
= OFMT_PARSABLE
;
4133 flags
= DLADM_OPT_PERSIST
;
4136 die_opterr(optopt
, option
, use
);
4142 * Get the optional tunnel name argument. If there is one, it must
4143 * be the last thing remaining on the command-line.
4145 if (argc
- optind
> 1)
4147 if (argc
- optind
== 1)
4148 name
= argv
[optind
];
4150 oferr
= ofmt_open(fields_str
, iptun_fields
, ofmtflags
,
4151 DLADM_DEFAULT_COL
, &ofmt
);
4152 ofmt_check(oferr
, state
.ls_parsable
, ofmt
, die
, warn
);
4154 state
.ls_ofmt
= ofmt
;
4155 state
.ls_flags
= flags
;
4158 (void) dladm_walk_datalink_id(print_iptun_walker
, handle
,
4159 &state
, DATALINK_CLASS_IPTUN
, DATALINK_ANY_MEDIATYPE
,
4161 status
= state
.ls_status
;
4163 if ((status
= dladm_name2info(handle
, name
, &linkid
, NULL
, NULL
,
4164 NULL
)) == DLADM_STATUS_OK
)
4165 status
= print_iptun(handle
, linkid
, &state
);
4168 if (status
!= DLADM_STATUS_OK
)
4169 die_dlerr(status
, "unable to obtain tunnel status");
4174 do_up_iptun(int argc
, char *argv
[], const char *use
)
4176 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4177 dladm_status_t status
= DLADM_STATUS_OK
;
4180 * Get the optional tunnel name argument. If there is one, it must
4181 * be the last thing remaining on the command-line.
4183 if (argc
- optind
> 1)
4185 if (argc
- optind
== 1) {
4186 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
4189 if (status
== DLADM_STATUS_OK
)
4190 status
= dladm_iptun_up(handle
, linkid
);
4191 if (status
!= DLADM_STATUS_OK
)
4192 die_dlerr(status
, "unable to configure IP tunnel links");
4197 do_down_iptun(int argc
, char *argv
[], const char *use
)
4199 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4200 dladm_status_t status
= DLADM_STATUS_OK
;
4203 * Get the optional tunnel name argument. If there is one, it must
4204 * be the last thing remaining on the command-line.
4206 if (argc
- optind
> 1)
4208 if (argc
- optind
== 1) {
4209 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
4212 if (status
== DLADM_STATUS_OK
)
4213 status
= dladm_iptun_down(handle
, linkid
);
4214 if (status
!= DLADM_STATUS_OK
)
4215 die_dlerr(status
, "unable to bring down IP tunnel links");
4219 iptun_gettypebyname(char *typestr
)
4223 for (i
= 0; iptun_types
[i
].type_name
!= NULL
; i
++) {
4224 if (strncmp(iptun_types
[i
].type_name
, typestr
,
4225 strlen(iptun_types
[i
].type_name
)) == 0) {
4226 return (iptun_types
[i
].type_value
);
4229 return (IPTUN_TYPE_UNKNOWN
);
4233 iptun_gettypebyvalue(iptun_type_t type
)
4237 for (i
= 0; iptun_types
[i
].type_name
!= NULL
; i
++) {
4238 if (iptun_types
[i
].type_value
== type
)
4239 return (iptun_types
[i
].type_name
);
4244 static dladm_status_t
4245 print_iptun(dladm_handle_t dh
, datalink_id_t linkid
, show_state_t
*state
)
4247 dladm_status_t status
;
4248 iptun_params_t params
;
4249 iptun_fields_buf_t lbuf
;
4253 params
.iptun_param_linkid
= linkid
;
4254 status
= dladm_iptun_getparams(dh
, ¶ms
, state
->ls_flags
);
4255 if (status
!= DLADM_STATUS_OK
)
4259 status
= dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
,
4260 lbuf
.iptun_name
, sizeof (lbuf
.iptun_name
));
4261 if (status
!= DLADM_STATUS_OK
)
4265 (void) strlcpy(lbuf
.iptun_type
,
4266 iptun_gettypebyvalue(params
.iptun_param_type
),
4267 sizeof (lbuf
.iptun_type
));
4270 (void) memset(lbuf
.iptun_flags
, '-', IPTUN_NUM_FLAGS
);
4271 lbuf
.iptun_flags
[IPTUN_NUM_FLAGS
] = '\0';
4272 if (params
.iptun_param_flags
& IPTUN_PARAM_IPSECPOL
)
4273 lbuf
.iptun_flags
[IPTUN_SFLAG_INDEX
] = 's';
4274 if (params
.iptun_param_flags
& IPTUN_PARAM_IMPLICIT
)
4275 lbuf
.iptun_flags
[IPTUN_IFLAG_INDEX
] = 'i';
4278 if (params
.iptun_param_flags
& IPTUN_PARAM_LADDR
)
4279 laddr
= params
.iptun_param_laddr
;
4281 laddr
= (state
->ls_parsable
) ? "" : "--";
4282 (void) strlcpy(lbuf
.iptun_laddr
, laddr
, sizeof (lbuf
.iptun_laddr
));
4285 if (params
.iptun_param_flags
& IPTUN_PARAM_RADDR
)
4286 raddr
= params
.iptun_param_raddr
;
4288 raddr
= (state
->ls_parsable
) ? "" : "--";
4289 (void) strlcpy(lbuf
.iptun_raddr
, raddr
, sizeof (lbuf
.iptun_raddr
));
4291 ofmt_print(state
->ls_ofmt
, &lbuf
);
4293 return (DLADM_STATUS_OK
);
4297 print_iptun_walker(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
4299 ((show_state_t
*)arg
)->ls_status
= print_iptun(dh
, linkid
, arg
);
4300 return (DLADM_WALK_CONTINUE
);
4303 static dladm_status_t
4304 print_phys(show_state_t
*state
, datalink_id_t linkid
)
4306 char link
[MAXLINKNAMELEN
];
4308 dladm_status_t status
;
4309 datalink_class_t
class;
4312 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
4313 &media
, link
, MAXLINKNAMELEN
)) != DLADM_STATUS_OK
) {
4317 if (class != DATALINK_CLASS_PHYS
) {
4318 status
= DLADM_STATUS_BADARG
;
4322 if (!(state
->ls_flags
& flags
)) {
4323 status
= DLADM_STATUS_NOTFOUND
;
4328 status
= print_phys_mac(state
, linkid
, link
);
4329 else if (state
->ls_hwgrp
)
4330 status
= print_phys_hwgrp(state
, linkid
, link
);
4332 status
= print_phys_default(state
, linkid
, link
, flags
, media
);
4340 show_phys(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
4342 show_state_t
*state
= arg
;
4344 state
->ls_status
= print_phys(state
, linkid
);
4345 return (DLADM_WALK_CONTINUE
);
4349 * Print the active topology information.
4351 static dladm_status_t
4352 print_vlan(show_state_t
*state
, datalink_id_t linkid
, link_fields_buf_t
*l
)
4354 dladm_vlan_attr_t vinfo
;
4356 dladm_status_t status
;
4358 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, NULL
, NULL
,
4359 l
->link_name
, sizeof (l
->link_name
))) != DLADM_STATUS_OK
) {
4363 if (!(state
->ls_flags
& flags
)) {
4364 status
= DLADM_STATUS_NOTFOUND
;
4368 if ((status
= dladm_vlan_info(handle
, linkid
, &vinfo
,
4369 state
->ls_flags
)) != DLADM_STATUS_OK
||
4370 (status
= dladm_datalink_id2info(handle
, vinfo
.dv_linkid
, NULL
,
4371 NULL
, NULL
, l
->link_over
, sizeof (l
->link_over
))) !=
4376 (void) snprintf(l
->link_vlan_vid
, sizeof (l
->link_vlan_vid
), "%d",
4378 (void) snprintf(l
->link_flags
, sizeof (l
->link_flags
), "%c----",
4379 vinfo
.dv_force
? 'f' : '-');
4387 show_vlan(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
4389 show_state_t
*state
= arg
;
4390 dladm_status_t status
;
4391 link_fields_buf_t lbuf
;
4393 bzero(&lbuf
, sizeof (link_fields_buf_t
));
4394 status
= print_vlan(state
, linkid
, &lbuf
);
4395 if (status
!= DLADM_STATUS_OK
)
4398 ofmt_print(state
->ls_ofmt
, &lbuf
);
4401 state
->ls_status
= status
;
4402 return (DLADM_WALK_CONTINUE
);
4406 do_show_phys(int argc
, char *argv
[], const char *use
)
4409 uint32_t flags
= DLADM_OPT_ACTIVE
;
4410 boolean_t p_arg
= B_FALSE
;
4411 boolean_t o_arg
= B_FALSE
;
4412 boolean_t m_arg
= B_FALSE
;
4413 boolean_t H_arg
= B_FALSE
;
4414 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4416 dladm_status_t status
;
4417 char *fields_str
= NULL
;
4418 char *all_active_fields
=
4419 "link,media,state,speed,duplex,device";
4420 char *all_inactive_fields
= "link,device,media,flags";
4421 char *all_mac_fields
= "link,slot,address,inuse,client";
4422 char *all_hwgrp_fields
= "link,ringtype,rings,clients";
4423 const ofmt_field_t
*pf
;
4425 ofmt_status_t oferr
;
4426 uint_t ofmtflags
= 0;
4428 bzero(&state
, sizeof (state
));
4430 while ((option
= getopt_long(argc
, argv
, ":pPo:mH",
4431 show_lopts
, NULL
)) != -1) {
4440 if (flags
!= DLADM_OPT_ACTIVE
)
4443 flags
= DLADM_OPT_PERSIST
;
4447 fields_str
= optarg
;
4456 die_opterr(optopt
, option
, use
);
4461 if (p_arg
&& !o_arg
)
4462 die("-p requires -o");
4465 die("-m cannot combine with -H");
4467 if (p_arg
&& strcasecmp(fields_str
, "all") == 0)
4468 die("\"-o all\" is invalid with -p");
4470 /* get link name (optional last argument) */
4471 if (optind
== (argc
-1)) {
4472 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
4473 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
4474 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
4476 } else if (optind
!= argc
) {
4480 state
.ls_parsable
= p_arg
;
4481 state
.ls_flags
= flags
;
4482 state
.ls_donefirst
= B_FALSE
;
4483 state
.ls_mac
= m_arg
;
4484 state
.ls_hwgrp
= H_arg
;
4486 if (m_arg
&& !(flags
& DLADM_OPT_ACTIVE
)) {
4488 * We can only display the factory MAC addresses of
4489 * active data-links.
4491 die("-m not compatible with -P");
4494 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0)) {
4496 fields_str
= all_mac_fields
;
4497 else if (state
.ls_hwgrp
)
4498 fields_str
= all_hwgrp_fields
;
4499 else if (state
.ls_flags
& DLADM_OPT_ACTIVE
) {
4500 fields_str
= all_active_fields
;
4502 fields_str
= all_inactive_fields
;
4508 } else if (state
.ls_hwgrp
) {
4514 if (state
.ls_parsable
)
4515 ofmtflags
|= OFMT_PARSABLE
;
4516 oferr
= ofmt_open(fields_str
, pf
, ofmtflags
, 0, &ofmt
);
4517 ofmt_check(oferr
, state
.ls_parsable
, ofmt
, die
, warn
);
4518 state
.ls_ofmt
= ofmt
;
4520 if (linkid
== DATALINK_ALL_LINKID
) {
4521 (void) dladm_walk_datalink_id(show_phys
, handle
, &state
,
4522 DATALINK_CLASS_PHYS
, DATALINK_ANY_MEDIATYPE
, flags
);
4524 (void) show_phys(handle
, linkid
, &state
);
4525 if (state
.ls_status
!= DLADM_STATUS_OK
) {
4526 die_dlerr(state
.ls_status
,
4527 "failed to show physical link %s", argv
[optind
]);
4534 do_show_vlan(int argc
, char *argv
[], const char *use
)
4537 uint32_t flags
= DLADM_OPT_ACTIVE
;
4538 boolean_t p_arg
= B_FALSE
;
4539 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4541 dladm_status_t status
;
4542 boolean_t o_arg
= B_FALSE
;
4543 char *fields_str
= NULL
;
4545 ofmt_status_t oferr
;
4546 uint_t ofmtflags
= 0;
4548 bzero(&state
, sizeof (state
));
4551 while ((option
= getopt_long(argc
, argv
, ":pPo:",
4552 show_lopts
, NULL
)) != -1) {
4561 if (flags
!= DLADM_OPT_ACTIVE
)
4564 flags
= DLADM_OPT_PERSIST
;
4568 fields_str
= optarg
;
4571 die_opterr(optopt
, option
, use
);
4576 /* get link name (optional last argument) */
4577 if (optind
== (argc
-1)) {
4578 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
4579 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
4580 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
4582 } else if (optind
!= argc
) {
4586 state
.ls_parsable
= p_arg
;
4587 state
.ls_flags
= flags
;
4588 state
.ls_donefirst
= B_FALSE
;
4590 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0))
4593 if (state
.ls_parsable
)
4594 ofmtflags
|= OFMT_PARSABLE
;
4595 oferr
= ofmt_open(fields_str
, vlan_fields
, ofmtflags
, 0, &ofmt
);
4596 ofmt_check(oferr
, state
.ls_parsable
, ofmt
, die
, warn
);
4597 state
.ls_ofmt
= ofmt
;
4599 if (linkid
== DATALINK_ALL_LINKID
) {
4600 (void) dladm_walk_datalink_id(show_vlan
, handle
, &state
,
4601 DATALINK_CLASS_VLAN
, DATALINK_ANY_MEDIATYPE
, flags
);
4603 (void) show_vlan(handle
, linkid
, &state
);
4604 if (state
.ls_status
!= DLADM_STATUS_OK
) {
4605 die_dlerr(state
.ls_status
, "failed to show vlan %s",
4613 do_create_vnic(int argc
, char *argv
[], const char *use
)
4615 datalink_id_t linkid
, dev_linkid
;
4616 char devname
[MAXLINKNAMELEN
];
4617 char name
[MAXLINKNAMELEN
];
4618 boolean_t l_arg
= B_FALSE
;
4619 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
4620 char *altroot
= NULL
;
4623 dladm_status_t status
;
4624 vnic_mac_addr_type_t mac_addr_type
= VNIC_MAC_ADDR_TYPE_UNKNOWN
;
4625 uchar_t
*mac_addr
= NULL
;
4627 uint_t maclen
= 0, mac_prefix_len
= 0;
4628 char propstr
[DLADM_STRSIZE
];
4629 dladm_arg_list_t
*proplist
= NULL
;
4632 vrid_t vrid
= VRRP_VRID_NONE
;
4635 bzero(propstr
, DLADM_STRSIZE
);
4637 while ((option
= getopt_long(argc
, argv
, ":tfR:l:m:n:p:r:v:V:A:H",
4638 vnic_lopts
, NULL
)) != -1) {
4641 flags
&= ~DLADM_OPT_PERSIST
;
4647 if (strlcpy(devname
, optarg
, MAXLINKNAMELEN
) >=
4649 die("link name too long");
4653 if (mac_addr_type
!= VNIC_MAC_ADDR_TYPE_UNKNOWN
)
4654 die("cannot specify -m option twice");
4656 if (strcmp(optarg
, "fixed") == 0) {
4658 * A fixed MAC address must be specified
4659 * by its value, not by the keyword 'fixed'.
4661 die("'fixed' is not a valid MAC address");
4663 if (dladm_vnic_str2macaddrtype(optarg
,
4664 &mac_addr_type
) != DLADM_STATUS_OK
) {
4665 mac_addr_type
= VNIC_MAC_ADDR_TYPE_FIXED
;
4666 /* MAC address specified by value */
4667 mac_addr
= _link_aton(optarg
, (int *)&maclen
);
4668 if (mac_addr
== NULL
) {
4669 if (maclen
== (uint_t
)-1)
4670 die("invalid MAC address");
4672 die("out of memory");
4678 mac_slot
= (int)strtol(optarg
, &endp
, 10);
4679 if (errno
!= 0 || *endp
!= '\0')
4680 die("invalid slot number");
4683 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
4684 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
4686 die("property list too long '%s'", propstr
);
4689 mac_addr
= _link_aton(optarg
, (int *)&mac_prefix_len
);
4690 if (mac_addr
== NULL
) {
4691 if (mac_prefix_len
== (uint_t
)-1)
4692 die("invalid MAC address");
4694 die("out of memory");
4698 if (!str2int(optarg
, (int *)&vrid
) ||
4699 vrid
< VRRP_VRID_MIN
|| vrid
> VRRP_VRID_MAX
) {
4700 die("invalid VRRP identifier '%s'", optarg
);
4705 if (strcmp(optarg
, "inet") == 0)
4707 else if (strcmp(optarg
, "inet6") == 0)
4710 die("invalid address family '%s'", optarg
);
4716 if (!str2int(optarg
, &vid
) || vid
< 1 || vid
> 4094)
4717 die("invalid VLAN identifier '%s'", optarg
);
4721 flags
|= DLADM_OPT_FORCE
;
4724 die_opterr(optopt
, option
, use
);
4728 if (mac_addr_type
== VNIC_MAC_ADDR_TYPE_UNKNOWN
)
4729 mac_addr_type
= VNIC_MAC_ADDR_TYPE_AUTO
;
4732 * 'f' - force, flag can be specified only with 'v' - vlan.
4734 if ((flags
& DLADM_OPT_FORCE
) != 0 && vid
== 0)
4735 die("-f option can only be used with -v");
4737 if (mac_prefix_len
!= 0 && mac_addr_type
!= VNIC_MAC_ADDR_TYPE_RANDOM
&&
4738 mac_addr_type
!= VNIC_MAC_ADDR_TYPE_FIXED
)
4741 if (mac_addr_type
== VNIC_MAC_ADDR_TYPE_VRID
) {
4742 if (vrid
== VRRP_VRID_NONE
|| af
== AF_UNSPEC
||
4743 mac_addr
!= NULL
|| maclen
!= 0 || mac_slot
!= -1 ||
4744 mac_prefix_len
!= 0) {
4747 } else if ((af
!= AF_UNSPEC
|| vrid
!= VRRP_VRID_NONE
)) {
4751 /* check required options */
4755 if (mac_slot
!= -1 && mac_addr_type
!= VNIC_MAC_ADDR_TYPE_FACTORY
)
4758 /* the VNIC id is the required operand */
4759 if (optind
!= (argc
- 1))
4762 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
4763 die("link name too long '%s'", argv
[optind
]);
4765 if (!dladm_valid_linkname(name
))
4766 die("invalid link name '%s'", argv
[optind
]);
4768 if (altroot
!= NULL
)
4769 altroot_cmd(altroot
, argc
, argv
);
4771 if (dladm_name2info(handle
, devname
, &dev_linkid
, NULL
, NULL
, NULL
) !=
4773 die("invalid link name '%s'", devname
);
4775 if (dladm_parse_link_props(propstr
, &proplist
, B_FALSE
)
4777 die("invalid vnic property");
4779 status
= dladm_vnic_create(handle
, name
, dev_linkid
, mac_addr_type
,
4780 mac_addr
, maclen
, &mac_slot
, mac_prefix_len
, vid
, vrid
, af
,
4781 &linkid
, proplist
, flags
);
4783 case DLADM_STATUS_OK
:
4786 case DLADM_STATUS_LINKBUSY
:
4787 die("VLAN over '%s' may not use default_tag ID "
4788 "(see dladm(8))", devname
);
4792 die_dlerr(status
, "vnic creation over %s failed", devname
);
4795 dladm_free_props(proplist
);
4800 do_etherstub_check(const char *name
, datalink_id_t linkid
, boolean_t etherstub
,
4803 boolean_t is_etherstub
;
4804 dladm_vnic_attr_t attr
;
4806 if (dladm_vnic_info(handle
, linkid
, &attr
, flags
) != DLADM_STATUS_OK
) {
4808 * Let the delete continue anyway.
4812 is_etherstub
= (attr
.va_link_id
== DATALINK_INVALID_LINKID
);
4813 if (is_etherstub
!= etherstub
) {
4814 die("'%s' is not %s", name
,
4815 (is_etherstub
? "a vnic" : "an etherstub"));
4820 do_delete_vnic_common(int argc
, char *argv
[], const char *use
,
4821 boolean_t etherstub
)
4824 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
4825 datalink_id_t linkid
;
4826 char *altroot
= NULL
;
4827 dladm_status_t status
;
4830 while ((option
= getopt_long(argc
, argv
, ":R:t", lopts
,
4834 flags
&= ~DLADM_OPT_PERSIST
;
4840 die_opterr(optopt
, option
, use
);
4844 /* get vnic name (required last argument) */
4845 if (optind
!= (argc
- 1))
4848 if (altroot
!= NULL
)
4849 altroot_cmd(altroot
, argc
, argv
);
4851 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
4853 if (status
!= DLADM_STATUS_OK
)
4854 die("invalid link name '%s'", argv
[optind
]);
4856 if ((flags
& DLADM_OPT_ACTIVE
) != 0) {
4857 do_etherstub_check(argv
[optind
], linkid
, etherstub
,
4860 if ((flags
& DLADM_OPT_PERSIST
) != 0) {
4861 do_etherstub_check(argv
[optind
], linkid
, etherstub
,
4865 status
= dladm_vnic_delete(handle
, linkid
, flags
);
4866 if (status
!= DLADM_STATUS_OK
)
4867 die_dlerr(status
, "vnic deletion failed");
4871 do_delete_vnic(int argc
, char *argv
[], const char *use
)
4873 do_delete_vnic_common(argc
, argv
, use
, B_FALSE
);
4878 do_up_vnic_common(int argc
, char *argv
[], const char *use
, boolean_t vlan
)
4880 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4881 dladm_status_t status
;
4884 type
= vlan
? "vlan" : "vnic";
4887 * get the id or the name of the vnic/vlan (optional last argument)
4890 status
= dladm_name2info(handle
, argv
[1], &linkid
, NULL
, NULL
,
4892 if (status
!= DLADM_STATUS_OK
)
4895 } else if (argc
> 2) {
4900 status
= dladm_vlan_up(handle
, linkid
);
4902 status
= dladm_vnic_up(handle
, linkid
, 0);
4905 if (status
!= DLADM_STATUS_OK
) {
4908 "could not bring up %s '%s'", type
, argv
[1]);
4910 die_dlerr(status
, "could not bring %ss up", type
);
4916 do_up_vnic(int argc
, char *argv
[], const char *use
)
4918 do_up_vnic_common(argc
, argv
, use
, B_FALSE
);
4922 dump_vnics_head(const char *dev
)
4925 (void) printf("%s", dev
);
4927 (void) printf("\tipackets rbytes opackets obytes ");
4930 (void) printf("%%ipkts %%opkts\n");
4932 (void) printf("\n");
4936 dump_vnic_stat(const char *name
, datalink_id_t vnic_id
,
4937 show_vnic_state_t
*state
, pktsum_t
*vnic_stats
, pktsum_t
*tot_stats
)
4939 pktsum_t diff_stats
;
4940 pktsum_t
*old_stats
= &state
->vs_prevstats
[vnic_id
];
4942 dladm_stats_diff(&diff_stats
, vnic_stats
, old_stats
);
4944 (void) printf("%s", name
);
4946 (void) printf("\t%-10llu", diff_stats
.ipackets
);
4947 (void) printf("%-12llu", diff_stats
.rbytes
);
4948 (void) printf("%-10llu", diff_stats
.opackets
);
4949 (void) printf("%-12llu", diff_stats
.obytes
);
4952 if (tot_stats
->ipackets
== 0) {
4953 (void) printf("\t-");
4955 (void) printf("\t%-6.1f", (double)diff_stats
.ipackets
/
4956 (double)tot_stats
->ipackets
* 100);
4958 if (tot_stats
->opackets
== 0) {
4959 (void) printf("\t-");
4961 (void) printf("\t%-6.1f", (double)diff_stats
.opackets
/
4962 (double)tot_stats
->opackets
* 100);
4965 (void) printf("\n");
4967 *old_stats
= *vnic_stats
;
4971 * Called from the walker dladm_vnic_walk_sys() for each vnic to display
4972 * vnic information or statistics.
4974 static dladm_status_t
4975 print_vnic(show_vnic_state_t
*state
, datalink_id_t linkid
)
4977 dladm_vnic_attr_t attr
, *vnic
= &attr
;
4978 dladm_status_t status
;
4979 boolean_t is_etherstub
;
4980 char devname
[MAXLINKNAMELEN
];
4981 char vnic_name
[MAXLINKNAMELEN
];
4982 char mstr
[MAXMACADDRLEN
* 3];
4983 vnic_fields_buf_t vbuf
;
4985 if ((status
= dladm_vnic_info(handle
, linkid
, vnic
, state
->vs_flags
)) !=
4989 is_etherstub
= (vnic
->va_link_id
== DATALINK_INVALID_LINKID
);
4990 if (state
->vs_etherstub
!= is_etherstub
) {
4992 * Want all etherstub but it's not one, or want
4993 * non-etherstub and it's one.
4995 return (DLADM_STATUS_OK
);
4998 if (state
->vs_link_id
!= DATALINK_ALL_LINKID
) {
4999 if (state
->vs_link_id
!= vnic
->va_link_id
)
5000 return (DLADM_STATUS_OK
);
5003 if (dladm_datalink_id2info(handle
, linkid
, NULL
, NULL
,
5004 NULL
, vnic_name
, sizeof (vnic_name
)) != DLADM_STATUS_OK
)
5005 return (DLADM_STATUS_BADARG
);
5007 bzero(devname
, sizeof (devname
));
5008 if (!is_etherstub
&&
5009 dladm_datalink_id2info(handle
, vnic
->va_link_id
, NULL
, NULL
,
5010 NULL
, devname
, sizeof (devname
)) != DLADM_STATUS_OK
)
5011 (void) sprintf(devname
, "?");
5013 state
->vs_found
= B_TRUE
;
5014 if (state
->vs_stats
) {
5015 /* print vnic statistics */
5016 pktsum_t vnic_stats
;
5018 if (state
->vs_firstonly
) {
5019 if (state
->vs_donefirst
)
5021 state
->vs_donefirst
= B_TRUE
;
5024 if (!state
->vs_printstats
) {
5026 * get vnic statistics and add to the sum for the
5029 get_link_stats(vnic_name
, &vnic_stats
);
5030 dladm_stats_total(&state
->vs_totalstats
, &vnic_stats
,
5031 &state
->vs_prevstats
[vnic
->va_vnic_id
]);
5033 /* get and print vnic statistics */
5034 get_link_stats(vnic_name
, &vnic_stats
);
5035 dump_vnic_stat(vnic_name
, linkid
, state
, &vnic_stats
,
5036 &state
->vs_totalstats
);
5038 return (DLADM_STATUS_OK
);
5040 (void) snprintf(vbuf
.vnic_link
, sizeof (vbuf
.vnic_link
),
5043 if (!is_etherstub
) {
5045 (void) snprintf(vbuf
.vnic_over
, sizeof (vbuf
.vnic_over
),
5047 (void) snprintf(vbuf
.vnic_speed
,
5048 sizeof (vbuf
.vnic_speed
), "%u",
5049 (uint_t
)((get_ifspeed(vnic_name
, B_TRUE
))
5052 switch (vnic
->va_mac_addr_type
) {
5053 case VNIC_MAC_ADDR_TYPE_FIXED
:
5054 case VNIC_MAC_ADDR_TYPE_PRIMARY
:
5055 (void) snprintf(vbuf
.vnic_macaddrtype
,
5056 sizeof (vbuf
.vnic_macaddrtype
),
5059 case VNIC_MAC_ADDR_TYPE_RANDOM
:
5060 (void) snprintf(vbuf
.vnic_macaddrtype
,
5061 sizeof (vbuf
.vnic_macaddrtype
),
5064 case VNIC_MAC_ADDR_TYPE_FACTORY
:
5065 (void) snprintf(vbuf
.vnic_macaddrtype
,
5066 sizeof (vbuf
.vnic_macaddrtype
),
5067 gettext("factory, slot %d"),
5070 case VNIC_MAC_ADDR_TYPE_VRID
:
5071 (void) snprintf(vbuf
.vnic_macaddrtype
,
5072 sizeof (vbuf
.vnic_macaddrtype
),
5073 gettext("vrrp, %d/%s"),
5074 vnic
->va_vrid
, vnic
->va_af
== AF_INET
?
5079 if (strlen(vbuf
.vnic_macaddrtype
) > 0) {
5080 (void) snprintf(vbuf
.vnic_macaddr
,
5081 sizeof (vbuf
.vnic_macaddr
), "%s",
5082 dladm_aggr_macaddr2str(vnic
->va_mac_addr
,
5086 (void) snprintf(vbuf
.vnic_vid
, sizeof (vbuf
.vnic_vid
),
5087 "%d", vnic
->va_vid
);
5090 ofmt_print(state
->vs_ofmt
, &vbuf
);
5092 return (DLADM_STATUS_OK
);
5098 show_vnic(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
5100 show_vnic_state_t
*state
= arg
;
5102 state
->vs_status
= print_vnic(state
, linkid
);
5103 return (DLADM_WALK_CONTINUE
);
5107 do_show_vnic_common(int argc
, char *argv
[], const char *use
,
5108 boolean_t etherstub
)
5111 boolean_t s_arg
= B_FALSE
;
5112 boolean_t i_arg
= B_FALSE
;
5113 boolean_t l_arg
= B_FALSE
;
5114 uint32_t interval
= 0, flags
= DLADM_OPT_ACTIVE
;
5115 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
5116 datalink_id_t dev_linkid
= DATALINK_ALL_LINKID
;
5117 show_vnic_state_t state
;
5118 dladm_status_t status
;
5119 boolean_t o_arg
= B_FALSE
;
5120 char *fields_str
= NULL
;
5121 const ofmt_field_t
*pf
;
5122 char *all_e_fields
= "link";
5124 ofmt_status_t oferr
;
5125 uint_t ofmtflags
= 0;
5127 bzero(&state
, sizeof (state
));
5129 while ((option
= getopt_long(argc
, argv
, ":pPl:si:o:", lopts
,
5133 state
.vs_parsable
= B_TRUE
;
5136 flags
= DLADM_OPT_PERSIST
;
5140 die("option not supported for this command");
5142 if (strlcpy(state
.vs_link
, optarg
, MAXLINKNAMELEN
) >=
5144 die("link name too long");
5150 die("the option -s cannot be specified "
5157 die("the option -i cannot be specified "
5161 if (!dladm_str2interval(optarg
, &interval
))
5162 die("invalid interval value '%s'", optarg
);
5166 fields_str
= optarg
;
5169 die_opterr(optopt
, option
, use
);
5173 if (i_arg
&& !s_arg
)
5174 die("the option -i can be used only with -s");
5176 /* get vnic ID (optional last argument) */
5177 if (optind
== (argc
- 1)) {
5178 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
5180 if (status
!= DLADM_STATUS_OK
) {
5181 die_dlerr(status
, "invalid vnic name '%s'",
5184 (void) strlcpy(state
.vs_vnic
, argv
[optind
], MAXLINKNAMELEN
);
5185 } else if (optind
!= argc
) {
5190 status
= dladm_name2info(handle
, state
.vs_link
, &dev_linkid
,
5192 if (status
!= DLADM_STATUS_OK
) {
5193 die_dlerr(status
, "invalid link name '%s'",
5198 state
.vs_vnic_id
= linkid
;
5199 state
.vs_link_id
= dev_linkid
;
5200 state
.vs_etherstub
= etherstub
;
5201 state
.vs_found
= B_FALSE
;
5202 state
.vs_flags
= flags
;
5204 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0)) {
5206 fields_str
= all_e_fields
;
5210 if (state
.vs_parsable
)
5211 ofmtflags
|= OFMT_PARSABLE
;
5212 oferr
= ofmt_open(fields_str
, pf
, ofmtflags
, 0, &ofmt
);
5213 ofmt_check(oferr
, state
.vs_parsable
, ofmt
, die
, warn
);
5214 state
.vs_ofmt
= ofmt
;
5217 /* Display vnic statistics */
5218 vnic_stats(&state
, interval
);
5223 /* Display vnic information */
5224 state
.vs_donefirst
= B_FALSE
;
5226 if (linkid
== DATALINK_ALL_LINKID
) {
5227 (void) dladm_walk_datalink_id(show_vnic
, handle
, &state
,
5228 DATALINK_CLASS_VNIC
| DATALINK_CLASS_ETHERSTUB
,
5229 DATALINK_ANY_MEDIATYPE
, flags
);
5231 (void) show_vnic(handle
, linkid
, &state
);
5232 if (state
.vs_status
!= DLADM_STATUS_OK
) {
5234 die_dlerr(state
.vs_status
, "failed to show vnic '%s'",
5242 do_show_vnic(int argc
, char *argv
[], const char *use
)
5244 do_show_vnic_common(argc
, argv
, use
, B_FALSE
);
5248 do_create_etherstub(int argc
, char *argv
[], const char *use
)
5251 char *altroot
= NULL
;
5253 dladm_status_t status
;
5254 char name
[MAXLINKNAMELEN
];
5255 uchar_t mac_addr
[ETHERADDRL
];
5258 bzero(mac_addr
, sizeof (mac_addr
));
5259 flags
= DLADM_OPT_ANCHOR
| DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5262 while ((option
= getopt_long(argc
, argv
, "tR:",
5263 etherstub_lopts
, NULL
)) != -1) {
5266 flags
&= ~DLADM_OPT_PERSIST
;
5272 die_opterr(optopt
, option
, use
);
5276 /* the etherstub id is the required operand */
5277 if (optind
!= (argc
- 1))
5280 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
5281 die("link name too long '%s'", argv
[optind
]);
5283 if (!dladm_valid_linkname(name
))
5284 die("invalid link name '%s'", argv
[optind
]);
5286 if (altroot
!= NULL
)
5287 altroot_cmd(altroot
, argc
, argv
);
5289 status
= dladm_vnic_create(handle
, name
, DATALINK_INVALID_LINKID
,
5290 VNIC_MAC_ADDR_TYPE_AUTO
, mac_addr
, ETHERADDRL
, NULL
, 0, 0,
5291 VRRP_VRID_NONE
, AF_UNSPEC
, NULL
, NULL
, flags
);
5292 if (status
!= DLADM_STATUS_OK
)
5293 die_dlerr(status
, "etherstub creation failed");
5297 do_delete_etherstub(int argc
, char *argv
[], const char *use
)
5299 do_delete_vnic_common(argc
, argv
, use
, B_TRUE
);
5304 do_show_etherstub(int argc
, char *argv
[], const char *use
)
5306 do_show_vnic_common(argc
, argv
, use
, B_TRUE
);
5311 do_up_simnet(int argc
, char *argv
[], const char *use
)
5313 (void) dladm_simnet_up(handle
, DATALINK_ALL_LINKID
, 0);
5317 do_create_simnet(int argc
, char *argv
[], const char *use
)
5320 char *altroot
= NULL
;
5322 uint32_t mtype
= DL_ETHER
;
5324 dladm_status_t status
;
5325 char name
[MAXLINKNAMELEN
];
5328 flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5331 while ((option
= getopt_long(argc
, argv
, ":tR:m:",
5332 simnet_lopts
, NULL
)) != -1) {
5335 flags
&= ~DLADM_OPT_PERSIST
;
5344 die_opterr(optopt
, option
, use
);
5348 /* the simnet id is the required operand */
5349 if (optind
!= (argc
- 1))
5352 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
5353 die("link name too long '%s'", argv
[optind
]);
5355 if (!dladm_valid_linkname(name
))
5356 die("invalid link name '%s'", name
);
5358 if (media
!= NULL
) {
5359 mtype
= dladm_str2media(media
);
5360 if (mtype
!= DL_ETHER
&& mtype
!= DL_WIFI
)
5361 die("media type '%s' is not supported", media
);
5364 if (altroot
!= NULL
)
5365 altroot_cmd(altroot
, argc
, argv
);
5367 status
= dladm_simnet_create(handle
, name
, mtype
, flags
);
5368 if (status
!= DLADM_STATUS_OK
)
5369 die_dlerr(status
, "simnet creation failed");
5373 do_delete_simnet(int argc
, char *argv
[], const char *use
)
5376 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5377 datalink_id_t linkid
;
5378 char *altroot
= NULL
;
5379 dladm_status_t status
;
5380 dladm_simnet_attr_t slinfo
;
5383 while ((option
= getopt_long(argc
, argv
, ":tR:", simnet_lopts
,
5387 flags
&= ~DLADM_OPT_PERSIST
;
5393 die_opterr(optopt
, option
, use
);
5397 /* get simnet name (required last argument) */
5398 if (optind
!= (argc
- 1))
5401 if (!dladm_valid_linkname(argv
[optind
]))
5402 die("invalid link name '%s'", argv
[optind
]);
5404 if (altroot
!= NULL
)
5405 altroot_cmd(altroot
, argc
, argv
);
5407 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
5409 if (status
!= DLADM_STATUS_OK
)
5410 die("simnet '%s' not found", argv
[optind
]);
5412 if ((status
= dladm_simnet_info(handle
, linkid
, &slinfo
,
5413 flags
)) != DLADM_STATUS_OK
)
5414 die_dlerr(status
, "failed to retrieve simnet information");
5416 status
= dladm_simnet_delete(handle
, linkid
, flags
);
5417 if (status
!= DLADM_STATUS_OK
)
5418 die_dlerr(status
, "simnet deletion failed");
5422 do_modify_simnet(int argc
, char *argv
[], const char *use
)
5425 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5426 datalink_id_t linkid
;
5427 datalink_id_t peer_linkid
;
5428 char *altroot
= NULL
;
5429 dladm_status_t status
;
5430 boolean_t p_arg
= B_FALSE
;
5433 while ((option
= getopt_long(argc
, argv
, ":tR:p:", simnet_lopts
,
5437 flags
&= ~DLADM_OPT_PERSIST
;
5446 if (strcasecmp(optarg
, "none") == 0)
5447 peer_linkid
= DATALINK_INVALID_LINKID
;
5448 else if (dladm_name2info(handle
, optarg
, &peer_linkid
,
5449 NULL
, NULL
, NULL
) != DLADM_STATUS_OK
)
5450 die("invalid peer link name '%s'", optarg
);
5453 die_opterr(optopt
, option
, use
);
5457 /* get simnet name (required last argument) */
5458 if (optind
!= (argc
- 1))
5461 /* Nothing to do if no peer link argument */
5465 if (altroot
!= NULL
)
5466 altroot_cmd(altroot
, argc
, argv
);
5468 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
5470 if (status
!= DLADM_STATUS_OK
)
5471 die("invalid link name '%s'", argv
[optind
]);
5473 status
= dladm_simnet_modify(handle
, linkid
, peer_linkid
, flags
);
5474 if (status
!= DLADM_STATUS_OK
)
5475 die_dlerr(status
, "simnet modification failed");
5478 static dladm_status_t
5479 print_simnet(show_state_t
*state
, datalink_id_t linkid
)
5481 dladm_simnet_attr_t slinfo
;
5483 dladm_status_t status
;
5484 simnet_fields_buf_t slbuf
;
5485 char mstr
[ETHERADDRL
* 3];
5487 bzero(&slbuf
, sizeof (slbuf
));
5488 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, NULL
, NULL
,
5489 slbuf
.simnet_name
, sizeof (slbuf
.simnet_name
)))
5493 if (!(state
->ls_flags
& flags
))
5494 return (DLADM_STATUS_NOTFOUND
);
5496 if ((status
= dladm_simnet_info(handle
, linkid
, &slinfo
,
5497 state
->ls_flags
)) != DLADM_STATUS_OK
)
5500 if (slinfo
.sna_peer_link_id
!= DATALINK_INVALID_LINKID
&&
5501 (status
= dladm_datalink_id2info(handle
, slinfo
.sna_peer_link_id
,
5502 NULL
, NULL
, NULL
, slbuf
.simnet_otherlink
,
5503 sizeof (slbuf
.simnet_otherlink
))) !=
5507 if (slinfo
.sna_mac_len
> sizeof (slbuf
.simnet_macaddr
))
5508 return (DLADM_STATUS_BADVAL
);
5510 (void) strlcpy(slbuf
.simnet_macaddr
,
5511 dladm_aggr_macaddr2str(slinfo
.sna_mac_addr
, mstr
),
5512 sizeof (slbuf
.simnet_macaddr
));
5513 (void) dladm_media2str(slinfo
.sna_type
, slbuf
.simnet_media
);
5515 ofmt_print(state
->ls_ofmt
, &slbuf
);
5521 show_simnet(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
5523 show_state_t
*state
= arg
;
5525 state
->ls_status
= print_simnet(state
, linkid
);
5526 return (DLADM_WALK_CONTINUE
);
5530 do_show_simnet(int argc
, char *argv
[], const char *use
)
5533 uint32_t flags
= DLADM_OPT_ACTIVE
;
5534 boolean_t p_arg
= B_FALSE
;
5535 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
5537 dladm_status_t status
;
5538 boolean_t o_arg
= B_FALSE
;
5540 ofmt_status_t oferr
;
5541 char *all_fields
= "link,media,macaddress,otherlink";
5542 char *fields_str
= all_fields
;
5543 uint_t ofmtflags
= 0;
5545 bzero(&state
, sizeof (state
));
5548 while ((option
= getopt_long(argc
, argv
, ":pPo:",
5549 show_lopts
, NULL
)) != -1) {
5556 state
.ls_parsable
= p_arg
;
5559 if (flags
!= DLADM_OPT_ACTIVE
)
5562 flags
= DLADM_OPT_PERSIST
;
5566 fields_str
= optarg
;
5569 die_opterr(optopt
, option
, use
);
5574 if (p_arg
&& !o_arg
)
5575 die("-p requires -o");
5577 if (strcasecmp(fields_str
, "all") == 0) {
5579 die("\"-o all\" is invalid with -p");
5580 fields_str
= all_fields
;
5583 /* get link name (optional last argument) */
5584 if (optind
== (argc
-1)) {
5585 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
5586 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
5587 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
5589 } else if (optind
!= argc
) {
5593 state
.ls_flags
= flags
;
5594 state
.ls_donefirst
= B_FALSE
;
5595 if (state
.ls_parsable
)
5596 ofmtflags
|= OFMT_PARSABLE
;
5597 oferr
= ofmt_open(fields_str
, simnet_fields
, ofmtflags
, 0, &ofmt
);
5598 ofmt_check(oferr
, state
.ls_parsable
, ofmt
, die
, warn
);
5599 state
.ls_ofmt
= ofmt
;
5601 if (linkid
== DATALINK_ALL_LINKID
) {
5602 (void) dladm_walk_datalink_id(show_simnet
, handle
, &state
,
5603 DATALINK_CLASS_SIMNET
, DATALINK_ANY_MEDIATYPE
, flags
);
5605 (void) show_simnet(handle
, linkid
, &state
);
5606 if (state
.ls_status
!= DLADM_STATUS_OK
) {
5608 die_dlerr(state
.ls_status
, "failed to show simnet %s",
5616 link_stats(datalink_id_t linkid
, uint_t interval
, char *fields_str
,
5617 show_state_t
*state
)
5620 ofmt_status_t oferr
;
5621 uint_t ofmtflags
= 0;
5623 if (state
->ls_parsable
)
5624 ofmtflags
|= OFMT_PARSABLE
;
5625 oferr
= ofmt_open(fields_str
, link_s_fields
, ofmtflags
, 0, &ofmt
);
5626 ofmt_check(oferr
, state
->ls_parsable
, ofmt
, die
, warn
);
5627 state
->ls_ofmt
= ofmt
;
5630 * If an interval is specified, continuously show the stats
5631 * only for the first MAC port.
5633 state
->ls_firstonly
= (interval
!= 0);
5636 state
->ls_donefirst
= B_FALSE
;
5637 if (linkid
== DATALINK_ALL_LINKID
) {
5638 (void) dladm_walk_datalink_id(show_link_stats
, handle
,
5639 state
, DATALINK_CLASS_ALL
, DATALINK_ANY_MEDIATYPE
,
5642 (void) show_link_stats(handle
, linkid
, state
);
5648 (void) fflush(stdout
);
5649 (void) sleep(interval
);
5655 aggr_stats(datalink_id_t linkid
, show_grp_state_t
*state
, uint_t interval
)
5658 * If an interval is specified, continuously show the stats
5659 * only for the first group.
5661 state
->gs_firstonly
= (interval
!= 0);
5664 state
->gs_donefirst
= B_FALSE
;
5665 if (linkid
== DATALINK_ALL_LINKID
)
5666 (void) dladm_walk_datalink_id(show_aggr
, handle
, state
,
5667 DATALINK_CLASS_AGGR
, DATALINK_ANY_MEDIATYPE
,
5670 (void) show_aggr(handle
, linkid
, state
);
5675 (void) fflush(stdout
);
5676 (void) sleep(interval
);
5682 vnic_stats(show_vnic_state_t
*sp
, uint32_t interval
)
5684 show_vnic_state_t state
;
5685 boolean_t specific_link
, specific_dev
;
5687 /* Display vnic statistics */
5688 dump_vnics_head(sp
->vs_link
);
5690 bzero(&state
, sizeof (state
));
5691 state
.vs_stats
= B_TRUE
;
5692 state
.vs_vnic_id
= sp
->vs_vnic_id
;
5693 state
.vs_link_id
= sp
->vs_link_id
;
5696 * If an interval is specified, and a vnic ID is not specified,
5697 * continuously show the stats only for the first vnic.
5699 specific_link
= (sp
->vs_vnic_id
!= DATALINK_ALL_LINKID
);
5700 specific_dev
= (sp
->vs_link_id
!= DATALINK_ALL_LINKID
);
5703 /* Get stats for each vnic */
5704 state
.vs_found
= B_FALSE
;
5705 state
.vs_donefirst
= B_FALSE
;
5706 state
.vs_printstats
= B_FALSE
;
5707 state
.vs_flags
= DLADM_OPT_ACTIVE
;
5709 if (!specific_link
) {
5710 (void) dladm_walk_datalink_id(show_vnic
, handle
, &state
,
5711 DATALINK_CLASS_VNIC
, DATALINK_ANY_MEDIATYPE
,
5714 (void) show_vnic(handle
, sp
->vs_vnic_id
, &state
);
5715 if (state
.vs_status
!= DLADM_STATUS_OK
) {
5716 die_dlerr(state
.vs_status
,
5717 "failed to show vnic '%s'", sp
->vs_vnic
);
5721 if (specific_link
&& !state
.vs_found
)
5722 die("non-existent vnic '%s'", sp
->vs_vnic
);
5723 if (specific_dev
&& !state
.vs_found
)
5724 die("device %s has no vnics", sp
->vs_link
);
5727 if ((specific_link
| specific_dev
) && !interval
) {
5728 (void) printf("Total");
5729 (void) printf("\t%-10llu",
5730 state
.vs_totalstats
.ipackets
);
5731 (void) printf("%-12llu",
5732 state
.vs_totalstats
.rbytes
);
5733 (void) printf("%-10llu",
5734 state
.vs_totalstats
.opackets
);
5735 (void) printf("%-12llu\n",
5736 state
.vs_totalstats
.obytes
);
5739 /* Show stats for each vnic */
5740 state
.vs_donefirst
= B_FALSE
;
5741 state
.vs_printstats
= B_TRUE
;
5743 if (!specific_link
) {
5744 (void) dladm_walk_datalink_id(show_vnic
, handle
, &state
,
5745 DATALINK_CLASS_VNIC
, DATALINK_ANY_MEDIATYPE
,
5748 (void) show_vnic(handle
, sp
->vs_vnic_id
, &state
);
5749 if (state
.vs_status
!= DLADM_STATUS_OK
) {
5750 die_dlerr(state
.vs_status
,
5751 "failed to show vnic '%s'", sp
->vs_vnic
);
5758 (void) fflush(stdout
);
5759 (void) sleep(interval
);
5764 get_mac_stats(const char *dev
, pktsum_t
*stats
)
5768 char module
[DLPI_LINKNAME_MAX
];
5772 bzero(stats
, sizeof (*stats
));
5774 if (dlpi_parselink(dev
, module
, &instance
) != DLPI_SUCCESS
)
5777 if ((kcp
= kstat_open()) == NULL
) {
5778 warn("kstat open operation failed");
5782 ksp
= dladm_kstat_lookup(kcp
, module
, instance
, "mac", NULL
);
5784 dladm_get_stats(kcp
, ksp
, stats
);
5786 (void) kstat_close(kcp
);
5791 get_link_stats(const char *link
, pktsum_t
*stats
)
5796 bzero(stats
, sizeof (*stats
));
5798 if ((kcp
= kstat_open()) == NULL
) {
5799 warn("kstat_open operation failed");
5803 ksp
= dladm_kstat_lookup(kcp
, "link", 0, link
, NULL
);
5806 dladm_get_stats(kcp
, ksp
, stats
);
5808 (void) kstat_close(kcp
);
5812 query_kstat(char *module
, int instance
, const char *name
, const char *stat
,
5813 uint8_t type
, void *val
)
5818 if ((kcp
= kstat_open()) == NULL
) {
5819 warn("kstat open operation failed");
5823 if ((ksp
= kstat_lookup(kcp
, module
, instance
, (char *)name
)) == NULL
) {
5825 * The kstat query could fail if the underlying MAC
5826 * driver was already detached.
5831 if (kstat_read(kcp
, ksp
, NULL
) == -1) {
5832 warn("kstat read failed");
5836 if (dladm_kstat_value(ksp
, stat
, type
, val
) < 0)
5839 (void) kstat_close(kcp
);
5843 (void) kstat_close(kcp
);
5848 get_one_kstat(const char *name
, const char *stat
, uint8_t type
,
5849 void *val
, boolean_t islink
)
5851 char module
[DLPI_LINKNAME_MAX
];
5855 return (query_kstat("link", 0, name
, stat
, type
, val
));
5857 if (dlpi_parselink(name
, module
, &instance
) != DLPI_SUCCESS
)
5860 return (query_kstat(module
, instance
, "mac", stat
, type
, val
));
5865 get_ifspeed(const char *name
, boolean_t islink
)
5867 uint64_t ifspeed
= 0;
5869 (void) get_one_kstat(name
, "ifspeed", KSTAT_DATA_UINT64
,
5876 get_linkstate(const char *name
, boolean_t islink
, char *buf
)
5878 link_state_t linkstate
;
5880 if (get_one_kstat(name
, "link_state", KSTAT_DATA_UINT32
,
5881 &linkstate
, islink
) != 0) {
5882 (void) strlcpy(buf
, "?", DLADM_STRSIZE
);
5885 return (dladm_linkstate2str(linkstate
, buf
));
5889 get_linkduplex(const char *name
, boolean_t islink
, char *buf
)
5891 link_duplex_t linkduplex
;
5893 if (get_one_kstat(name
, "link_duplex", KSTAT_DATA_UINT32
,
5894 &linkduplex
, islink
) != 0) {
5895 (void) strlcpy(buf
, "unknown", DLADM_STRSIZE
);
5899 return (dladm_linkduplex2str(linkduplex
, buf
));
5903 parse_wifi_fields(char *str
, ofmt_handle_t
*ofmt
, uint_t cmdtype
,
5906 ofmt_field_t
*template, *of
;
5908 ofmt_status_t oferr
;
5910 if (cmdtype
== WIFI_CMD_SCAN
) {
5911 template = wifi_common_fields
;
5913 str
= def_scan_wifi_fields
;
5914 if (strcasecmp(str
, "all") == 0)
5915 str
= all_scan_wifi_fields
;
5916 fn
= print_wlan_attr_cb
;
5917 } else if (cmdtype
== WIFI_CMD_SHOW
) {
5918 bcopy(wifi_common_fields
, &wifi_show_fields
[2],
5919 sizeof (wifi_common_fields
));
5920 template = wifi_show_fields
;
5922 str
= def_show_wifi_fields
;
5923 if (strcasecmp(str
, "all") == 0)
5924 str
= all_show_wifi_fields
;
5925 fn
= print_link_attr_cb
;
5930 for (of
= template; of
->of_name
!= NULL
; of
++) {
5931 if (of
->of_cb
== NULL
)
5935 oferr
= ofmt_open(str
, template, (parsable
? OFMT_PARSABLE
: 0),
5937 ofmt_check(oferr
, parsable
, *ofmt
, die
, warn
);
5941 typedef struct print_wifi_state
{
5943 boolean_t ws_parsable
;
5944 boolean_t ws_header
;
5945 ofmt_handle_t ws_ofmt
;
5946 } print_wifi_state_t
;
5948 typedef struct wlan_scan_args_s
{
5949 print_wifi_state_t
*ws_state
;
5954 print_wlan_attr_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
5956 wlan_scan_args_t
*w
= ofarg
->ofmt_cbarg
;
5957 print_wifi_state_t
*statep
= w
->ws_state
;
5958 dladm_wlan_attr_t
*attrp
= w
->ws_attr
;
5959 char tmpbuf
[DLADM_STRSIZE
];
5961 if (ofarg
->ofmt_id
== 0) {
5962 (void) strlcpy(buf
, (char *)statep
->ws_link
, bufsize
);
5966 if ((ofarg
->ofmt_id
& attrp
->wa_valid
) == 0)
5969 switch (ofarg
->ofmt_id
) {
5970 case DLADM_WLAN_ATTR_ESSID
:
5971 (void) dladm_wlan_essid2str(&attrp
->wa_essid
, tmpbuf
);
5973 case DLADM_WLAN_ATTR_BSSID
:
5974 (void) dladm_wlan_bssid2str(&attrp
->wa_bssid
, tmpbuf
);
5976 case DLADM_WLAN_ATTR_SECMODE
:
5977 (void) dladm_wlan_secmode2str(&attrp
->wa_secmode
, tmpbuf
);
5979 case DLADM_WLAN_ATTR_STRENGTH
:
5980 (void) dladm_wlan_strength2str(&attrp
->wa_strength
, tmpbuf
);
5982 case DLADM_WLAN_ATTR_MODE
:
5983 (void) dladm_wlan_mode2str(&attrp
->wa_mode
, tmpbuf
);
5985 case DLADM_WLAN_ATTR_SPEED
:
5986 (void) dladm_wlan_speed2str(&attrp
->wa_speed
, tmpbuf
);
5987 (void) strlcat(tmpbuf
, "Mb", sizeof (tmpbuf
));
5989 case DLADM_WLAN_ATTR_AUTH
:
5990 (void) dladm_wlan_auth2str(&attrp
->wa_auth
, tmpbuf
);
5992 case DLADM_WLAN_ATTR_BSSTYPE
:
5993 (void) dladm_wlan_bsstype2str(&attrp
->wa_bsstype
, tmpbuf
);
5996 (void) strlcpy(buf
, tmpbuf
, bufsize
);
6002 print_scan_results(void *arg
, dladm_wlan_attr_t
*attrp
)
6004 print_wifi_state_t
*statep
= arg
;
6005 wlan_scan_args_t warg
;
6007 bzero(&warg
, sizeof (warg
));
6008 warg
.ws_state
= statep
;
6009 warg
.ws_attr
= attrp
;
6010 ofmt_print(statep
->ws_ofmt
, &warg
);
6015 scan_wifi(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6017 print_wifi_state_t
*statep
= arg
;
6018 dladm_status_t status
;
6019 char link
[MAXLINKNAMELEN
];
6021 if ((status
= dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
, link
,
6022 sizeof (link
))) != DLADM_STATUS_OK
) {
6023 return (DLADM_WALK_CONTINUE
);
6026 statep
->ws_link
= link
;
6027 status
= dladm_wlan_scan(dh
, linkid
, statep
, print_scan_results
);
6028 if (status
!= DLADM_STATUS_OK
)
6029 die_dlerr(status
, "cannot scan link '%s'", statep
->ws_link
);
6031 return (DLADM_WALK_CONTINUE
);
6035 print_wifi_status_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
6037 static char tmpbuf
[DLADM_STRSIZE
];
6038 wlan_scan_args_t
*w
= ofarg
->ofmt_cbarg
;
6039 dladm_wlan_linkattr_t
*attrp
= w
->ws_attr
;
6041 if ((ofarg
->ofmt_id
& attrp
->la_valid
) != 0) {
6042 (void) dladm_wlan_linkstatus2str(&attrp
->la_status
, tmpbuf
);
6043 (void) strlcpy(buf
, tmpbuf
, bufsize
);
6049 print_link_attr_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
6051 wlan_scan_args_t
*w
= ofarg
->ofmt_cbarg
, w1
;
6052 print_wifi_state_t
*statep
= w
->ws_state
;
6053 dladm_wlan_linkattr_t
*attrp
= w
->ws_attr
;
6055 bzero(&w1
, sizeof (w1
));
6056 w1
.ws_state
= statep
;
6057 w1
.ws_attr
= &attrp
->la_wlan_attr
;
6058 ofarg
->ofmt_cbarg
= &w1
;
6059 return (print_wlan_attr_cb(ofarg
, buf
, bufsize
));
6063 show_wifi(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6065 print_wifi_state_t
*statep
= arg
;
6066 dladm_wlan_linkattr_t attr
;
6067 dladm_status_t status
;
6068 char link
[MAXLINKNAMELEN
];
6069 wlan_scan_args_t warg
;
6071 if ((status
= dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
, link
,
6072 sizeof (link
))) != DLADM_STATUS_OK
) {
6073 return (DLADM_WALK_CONTINUE
);
6076 /* dladm_wlan_get_linkattr() memsets attr with 0 */
6077 status
= dladm_wlan_get_linkattr(dh
, linkid
, &attr
);
6078 if (status
!= DLADM_STATUS_OK
)
6079 die_dlerr(status
, "cannot get link attributes for %s", link
);
6081 statep
->ws_link
= link
;
6083 bzero(&warg
, sizeof (warg
));
6084 warg
.ws_state
= statep
;
6085 warg
.ws_attr
= &attr
;
6086 ofmt_print(statep
->ws_ofmt
, &warg
);
6087 return (DLADM_WALK_CONTINUE
);
6091 do_display_wifi(int argc
, char **argv
, int cmd
, const char *use
)
6094 char *fields_str
= NULL
;
6095 int (*callback
)(dladm_handle_t
, datalink_id_t
, void *);
6096 print_wifi_state_t state
;
6097 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6098 dladm_status_t status
;
6100 if (cmd
== WIFI_CMD_SCAN
)
6101 callback
= scan_wifi
;
6102 else if (cmd
== WIFI_CMD_SHOW
)
6103 callback
= show_wifi
;
6107 state
.ws_parsable
= B_FALSE
;
6108 state
.ws_header
= B_TRUE
;
6110 while ((option
= getopt_long(argc
, argv
, ":o:p",
6111 wifi_longopts
, NULL
)) != -1) {
6114 fields_str
= optarg
;
6117 state
.ws_parsable
= B_TRUE
;
6120 die_opterr(optopt
, option
, use
);
6124 if (state
.ws_parsable
&& fields_str
== NULL
)
6125 die("-p requires -o");
6127 if (state
.ws_parsable
&& strcasecmp(fields_str
, "all") == 0)
6128 die("\"-o all\" is invalid with -p");
6130 if (optind
== (argc
- 1)) {
6131 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6132 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6133 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6135 } else if (optind
!= argc
) {
6139 if (parse_wifi_fields(fields_str
, &state
.ws_ofmt
, cmd
,
6140 state
.ws_parsable
) < 0)
6141 die("invalid field(s) specified");
6143 if (linkid
== DATALINK_ALL_LINKID
) {
6144 (void) dladm_walk_datalink_id(callback
, handle
, &state
,
6145 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6146 DL_WIFI
, DLADM_OPT_ACTIVE
);
6148 (void) (*callback
)(handle
, linkid
, &state
);
6150 ofmt_close(state
.ws_ofmt
);
6154 do_scan_wifi(int argc
, char **argv
, const char *use
)
6156 do_display_wifi(argc
, argv
, WIFI_CMD_SCAN
, use
);
6160 do_show_wifi(int argc
, char **argv
, const char *use
)
6162 do_display_wifi(argc
, argv
, WIFI_CMD_SHOW
, use
);
6165 typedef struct wlan_count_attr
{
6167 datalink_id_t wc_linkid
;
6168 } wlan_count_attr_t
;
6172 do_count_wlan(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6174 wlan_count_attr_t
*cp
= arg
;
6176 if (cp
->wc_count
== 0)
6177 cp
->wc_linkid
= linkid
;
6179 return (DLADM_WALK_CONTINUE
);
6183 parse_wlan_keys(char *str
, dladm_wlan_key_t
**keys
, uint_t
*key_countp
)
6186 dladm_wlan_key_t
*wk
;
6188 char *field
, *token
, *lasts
= NULL
, c
;
6191 while ((c
= *token
++) != '\0') {
6195 token
= strdup(str
);
6199 wk
= malloc(nfields
* sizeof (dladm_wlan_key_t
));
6204 for (i
= 0; i
< nfields
; i
++) {
6206 dladm_secobj_class_t
class;
6207 dladm_status_t status
;
6209 field
= strtok_r(token
, ",", &lasts
);
6212 (void) strlcpy(wk
[i
].wk_name
, field
,
6213 DLADM_WLAN_MAX_KEYNAME_LEN
);
6216 if ((s
= strrchr(wk
[i
].wk_name
, ':')) != NULL
) {
6217 if (s
[1] == '\0' || s
[2] != '\0' || !isdigit(s
[1]))
6220 wk
[i
].wk_idx
= (uint_t
)(s
[1] - '0');
6223 wk
[i
].wk_len
= DLADM_WLAN_MAX_KEY_LEN
;
6225 status
= dladm_get_secobj(handle
, wk
[i
].wk_name
, &class,
6226 wk
[i
].wk_val
, &wk
[i
].wk_len
, 0);
6227 if (status
!= DLADM_STATUS_OK
) {
6228 if (status
== DLADM_STATUS_NOTFOUND
) {
6229 status
= dladm_get_secobj(handle
, wk
[i
].wk_name
,
6230 &class, wk
[i
].wk_val
, &wk
[i
].wk_len
,
6233 if (status
!= DLADM_STATUS_OK
)
6236 wk
[i
].wk_class
= class;
6249 do_connect_wifi(int argc
, char **argv
, const char *use
)
6252 dladm_wlan_attr_t attr
, *attrp
;
6253 dladm_status_t status
= DLADM_STATUS_OK
;
6254 int timeout
= DLADM_WLAN_CONNECT_TIMEOUT_DEFAULT
;
6255 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6256 dladm_wlan_key_t
*keys
= NULL
;
6257 uint_t key_count
= 0;
6259 dladm_wlan_secmode_t keysecmode
= DLADM_WLAN_SECMODE_NONE
;
6260 char buf
[DLADM_STRSIZE
];
6263 (void) memset(&attr
, 0, sizeof (attr
));
6264 while ((option
= getopt_long(argc
, argv
, ":e:i:a:m:b:s:k:T:c",
6265 wifi_longopts
, NULL
)) != -1) {
6268 status
= dladm_wlan_str2essid(optarg
, &attr
.wa_essid
);
6269 if (status
!= DLADM_STATUS_OK
)
6270 die("invalid ESSID '%s'", optarg
);
6272 attr
.wa_valid
|= DLADM_WLAN_ATTR_ESSID
;
6274 * Try to connect without doing a scan.
6276 flags
|= DLADM_WLAN_CONNECT_NOSCAN
;
6279 status
= dladm_wlan_str2bssid(optarg
, &attr
.wa_bssid
);
6280 if (status
!= DLADM_STATUS_OK
)
6281 die("invalid BSSID %s", optarg
);
6283 attr
.wa_valid
|= DLADM_WLAN_ATTR_BSSID
;
6286 status
= dladm_wlan_str2auth(optarg
, &attr
.wa_auth
);
6287 if (status
!= DLADM_STATUS_OK
)
6288 die("invalid authentication mode '%s'", optarg
);
6290 attr
.wa_valid
|= DLADM_WLAN_ATTR_AUTH
;
6293 status
= dladm_wlan_str2mode(optarg
, &attr
.wa_mode
);
6294 if (status
!= DLADM_STATUS_OK
)
6295 die("invalid mode '%s'", optarg
);
6297 attr
.wa_valid
|= DLADM_WLAN_ATTR_MODE
;
6300 if ((status
= dladm_wlan_str2bsstype(optarg
,
6301 &attr
.wa_bsstype
)) != DLADM_STATUS_OK
) {
6302 die("invalid bsstype '%s'", optarg
);
6305 attr
.wa_valid
|= DLADM_WLAN_ATTR_BSSTYPE
;
6308 if ((status
= dladm_wlan_str2secmode(optarg
,
6309 &attr
.wa_secmode
)) != DLADM_STATUS_OK
) {
6310 die("invalid security mode '%s'", optarg
);
6313 attr
.wa_valid
|= DLADM_WLAN_ATTR_SECMODE
;
6316 if (parse_wlan_keys(optarg
, &keys
, &key_count
) < 0)
6317 die("invalid key(s) '%s'", optarg
);
6319 if (keys
[0].wk_class
== DLADM_SECOBJ_CLASS_WEP
)
6320 keysecmode
= DLADM_WLAN_SECMODE_WEP
;
6322 keysecmode
= DLADM_WLAN_SECMODE_WPA
;
6325 if (strcasecmp(optarg
, "forever") == 0) {
6329 if (!str2int(optarg
, &timeout
) || timeout
< 0)
6330 die("invalid timeout value '%s'", optarg
);
6333 flags
|= DLADM_WLAN_CONNECT_CREATEIBSS
;
6334 flags
|= DLADM_WLAN_CONNECT_CREATEIBSS
;
6337 die_opterr(optopt
, option
, use
);
6342 if (keysecmode
== DLADM_WLAN_SECMODE_NONE
) {
6343 if ((attr
.wa_valid
& DLADM_WLAN_ATTR_SECMODE
) != 0) {
6344 die("key required for security mode '%s'",
6345 dladm_wlan_secmode2str(&attr
.wa_secmode
, buf
));
6348 if ((attr
.wa_valid
& DLADM_WLAN_ATTR_SECMODE
) != 0 &&
6349 attr
.wa_secmode
!= keysecmode
)
6350 die("incompatible -s and -k options");
6351 attr
.wa_valid
|= DLADM_WLAN_ATTR_SECMODE
;
6352 attr
.wa_secmode
= keysecmode
;
6355 if (optind
== (argc
- 1)) {
6356 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6357 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6358 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6360 } else if (optind
!= argc
) {
6364 if (linkid
== DATALINK_ALL_LINKID
) {
6365 wlan_count_attr_t wcattr
;
6367 wcattr
.wc_linkid
= DATALINK_INVALID_LINKID
;
6368 wcattr
.wc_count
= 0;
6369 (void) dladm_walk_datalink_id(do_count_wlan
, handle
, &wcattr
,
6370 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6371 DL_WIFI
, DLADM_OPT_ACTIVE
);
6372 if (wcattr
.wc_count
== 0) {
6373 die("no wifi links are available");
6374 } else if (wcattr
.wc_count
> 1) {
6375 die("link name is required when more than one wifi "
6376 "link is available");
6378 linkid
= wcattr
.wc_linkid
;
6380 attrp
= (attr
.wa_valid
== 0) ? NULL
: &attr
;
6382 if ((status
= dladm_wlan_connect(handle
, linkid
, attrp
, timeout
, keys
,
6383 key_count
, flags
)) != DLADM_STATUS_OK
) {
6384 if ((flags
& DLADM_WLAN_CONNECT_NOSCAN
) != 0) {
6386 * Try again with scanning and filtering.
6388 flags
&= ~DLADM_WLAN_CONNECT_NOSCAN
;
6392 if (status
== DLADM_STATUS_NOTFOUND
) {
6393 if (attr
.wa_valid
== 0) {
6394 die("no wifi networks are available");
6396 die("no wifi networks with the specified "
6397 "criteria are available");
6400 die_dlerr(status
, "cannot connect");
6407 do_all_disconnect_wifi(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6409 dladm_status_t status
;
6411 status
= dladm_wlan_disconnect(dh
, linkid
);
6412 if (status
!= DLADM_STATUS_OK
)
6413 warn_dlerr(status
, "cannot disconnect link");
6415 return (DLADM_WALK_CONTINUE
);
6419 do_disconnect_wifi(int argc
, char **argv
, const char *use
)
6422 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6423 boolean_t all_links
= B_FALSE
;
6424 dladm_status_t status
;
6425 wlan_count_attr_t wcattr
;
6428 while ((option
= getopt_long(argc
, argv
, ":a",
6429 wifi_longopts
, NULL
)) != -1) {
6435 die_opterr(optopt
, option
, use
);
6440 if (optind
== (argc
- 1)) {
6441 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6442 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6443 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6445 } else if (optind
!= argc
) {
6449 if (linkid
== DATALINK_ALL_LINKID
) {
6451 wcattr
.wc_linkid
= linkid
;
6452 wcattr
.wc_count
= 0;
6453 (void) dladm_walk_datalink_id(do_count_wlan
, handle
,
6455 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6456 DL_WIFI
, DLADM_OPT_ACTIVE
);
6457 if (wcattr
.wc_count
== 0) {
6458 die("no wifi links are available");
6459 } else if (wcattr
.wc_count
> 1) {
6460 die("link name is required when more than "
6461 "one wifi link is available");
6463 linkid
= wcattr
.wc_linkid
;
6465 (void) dladm_walk_datalink_id(do_all_disconnect_wifi
,
6467 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6468 DL_WIFI
, DLADM_OPT_ACTIVE
);
6472 status
= dladm_wlan_disconnect(handle
, linkid
);
6473 if (status
!= DLADM_STATUS_OK
)
6474 die_dlerr(status
, "cannot disconnect");
6478 print_linkprop(datalink_id_t linkid
, show_linkprop_state_t
*statep
,
6479 const char *propname
, dladm_prop_type_t type
, const char *format
,
6484 char buf
[DLADM_STRSIZE
];
6485 char *unknown
= "--", *notsup
= "";
6486 char **propvals
= statep
->ls_propvals
;
6487 uint_t valcnt
= DLADM_MAX_PROP_VALCNT
;
6488 dladm_status_t status
;
6490 status
= dladm_get_linkprop(handle
, linkid
, type
, propname
, propvals
,
6492 if (status
!= DLADM_STATUS_OK
) {
6493 if (status
== DLADM_STATUS_TEMPONLY
) {
6494 if (type
== DLADM_PROP_VAL_MODIFIABLE
&&
6495 statep
->ls_persist
) {
6497 propvals
= &unknown
;
6499 statep
->ls_status
= status
;
6500 statep
->ls_retstatus
= status
;
6503 } else if (status
== DLADM_STATUS_NOTSUP
||
6504 statep
->ls_persist
) {
6506 if (type
== DLADM_PROP_VAL_CURRENT
||
6507 type
== DLADM_PROP_VAL_PERM
)
6508 propvals
= &unknown
;
6511 } else if (status
== DLADM_STATUS_NOTDEFINED
) {
6512 propvals
= ¬sup
; /* STR_UNDEF_VAL */
6514 if (statep
->ls_proplist
&&
6515 statep
->ls_status
== DLADM_STATUS_OK
) {
6517 "cannot get link property '%s' for %s",
6518 propname
, statep
->ls_link
);
6520 statep
->ls_status
= status
;
6521 statep
->ls_retstatus
= status
;
6526 statep
->ls_status
= DLADM_STATUS_OK
;
6530 lim
= buf
+ DLADM_STRSIZE
;
6531 for (i
= 0; i
< valcnt
; i
++) {
6532 if (propvals
[i
][0] == '\0' && !statep
->ls_parsable
)
6533 ptr
+= snprintf(ptr
, lim
- ptr
, "--,");
6535 ptr
+= snprintf(ptr
, lim
- ptr
, "%s,", propvals
[i
]);
6540 buf
[strlen(buf
) - 1] = '\0';
6542 lim
= statep
->ls_line
+ MAX_PROP_LINE
;
6543 if (statep
->ls_parsable
) {
6544 *pptr
+= snprintf(*pptr
, lim
- *pptr
,
6547 *pptr
+= snprintf(*pptr
, lim
- *pptr
, format
, buf
);
6552 print_linkprop_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
6554 linkprop_args_t
*arg
= ofarg
->ofmt_cbarg
;
6555 char *propname
= arg
->ls_propname
;
6556 show_linkprop_state_t
*statep
= arg
->ls_state
;
6557 char *ptr
= statep
->ls_line
;
6558 char *lim
= ptr
+ MAX_PROP_LINE
;
6559 datalink_id_t linkid
= arg
->ls_linkid
;
6561 switch (ofarg
->ofmt_id
) {
6563 (void) snprintf(ptr
, lim
- ptr
, "%s", statep
->ls_link
);
6565 case LINKPROP_PROPERTY
:
6566 (void) snprintf(ptr
, lim
- ptr
, "%s", propname
);
6568 case LINKPROP_VALUE
:
6569 print_linkprop(linkid
, statep
, propname
,
6570 statep
->ls_persist
? DLADM_PROP_VAL_PERSISTENT
:
6571 DLADM_PROP_VAL_CURRENT
, "%s", &ptr
);
6573 * If we failed to query the link property, for example, query
6574 * the persistent value of a non-persistable link property,
6575 * simply skip the output.
6577 if (statep
->ls_status
!= DLADM_STATUS_OK
) {
6579 * Ignore the temponly error when we skip printing
6580 * link properties to avoid returning failure on exit.
6582 if (statep
->ls_retstatus
== DLADM_STATUS_TEMPONLY
)
6583 statep
->ls_retstatus
= DLADM_STATUS_OK
;
6586 ptr
= statep
->ls_line
;
6589 print_linkprop(linkid
, statep
, propname
,
6590 DLADM_PROP_VAL_PERM
, "%s", &ptr
);
6591 if (statep
->ls_status
!= DLADM_STATUS_OK
)
6593 ptr
= statep
->ls_line
;
6595 case LINKPROP_DEFAULT
:
6596 print_linkprop(linkid
, statep
, propname
,
6597 DLADM_PROP_VAL_DEFAULT
, "%s", &ptr
);
6598 if (statep
->ls_status
!= DLADM_STATUS_OK
)
6600 ptr
= statep
->ls_line
;
6602 case LINKPROP_POSSIBLE
:
6603 print_linkprop(linkid
, statep
, propname
,
6604 DLADM_PROP_VAL_MODIFIABLE
, "%s ", &ptr
);
6605 if (statep
->ls_status
!= DLADM_STATUS_OK
)
6607 ptr
= statep
->ls_line
;
6610 die("invalid input");
6613 (void) strlcpy(buf
, ptr
, bufsize
);
6616 return ((statep
->ls_status
== DLADM_STATUS_OK
) ?
6621 linkprop_is_supported(datalink_id_t linkid
, const char *propname
,
6622 show_linkprop_state_t
*statep
)
6624 dladm_status_t status
;
6625 uint_t valcnt
= DLADM_MAX_PROP_VALCNT
;
6627 /* if used with -p flag, always print output */
6628 if (statep
->ls_proplist
!= NULL
)
6631 status
= dladm_get_linkprop(handle
, linkid
, DLADM_PROP_VAL_DEFAULT
,
6632 propname
, statep
->ls_propvals
, &valcnt
);
6634 if (status
== DLADM_STATUS_OK
)
6638 * A system wide default value is not available for the
6639 * property. Check if current value can be retrieved.
6641 status
= dladm_get_linkprop(handle
, linkid
, DLADM_PROP_VAL_CURRENT
,
6642 propname
, statep
->ls_propvals
, &valcnt
);
6644 return (status
== DLADM_STATUS_OK
);
6649 show_linkprop(dladm_handle_t dh
, datalink_id_t linkid
, const char *propname
,
6652 show_linkprop_state_t
*statep
= arg
;
6653 linkprop_args_t ls_arg
;
6655 bzero(&ls_arg
, sizeof (ls_arg
));
6656 ls_arg
.ls_state
= statep
;
6657 ls_arg
.ls_propname
= (char *)propname
;
6658 ls_arg
.ls_linkid
= linkid
;
6661 * This will need to be fixed when kernel interfaces are added
6662 * to enable walking of all known private properties. For now,
6663 * we are limited to walking persistent private properties only.
6665 if ((propname
[0] == '_') && !statep
->ls_persist
&&
6666 (statep
->ls_proplist
== NULL
))
6667 return (DLADM_WALK_CONTINUE
);
6668 if (!statep
->ls_parsable
&&
6669 !linkprop_is_supported(linkid
, propname
, statep
))
6670 return (DLADM_WALK_CONTINUE
);
6672 ofmt_print(statep
->ls_ofmt
, &ls_arg
);
6674 return (DLADM_WALK_CONTINUE
);
6678 do_show_linkprop(int argc
, char **argv
, const char *use
)
6681 char propstr
[DLADM_STRSIZE
];
6682 dladm_arg_list_t
*proplist
= NULL
;
6683 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6684 show_linkprop_state_t state
;
6685 uint32_t flags
= DLADM_OPT_ACTIVE
;
6686 dladm_status_t status
;
6687 char *fields_str
= NULL
;
6689 ofmt_status_t oferr
;
6690 uint_t ofmtflags
= 0;
6692 bzero(propstr
, DLADM_STRSIZE
);
6694 state
.ls_propvals
= NULL
;
6695 state
.ls_line
= NULL
;
6696 state
.ls_parsable
= B_FALSE
;
6697 state
.ls_persist
= B_FALSE
;
6698 state
.ls_header
= B_TRUE
;
6699 state
.ls_retstatus
= DLADM_STATUS_OK
;
6701 while ((option
= getopt_long(argc
, argv
, ":p:cPo:",
6702 prop_longopts
, NULL
)) != -1) {
6705 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
6706 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
6708 die("property list too long '%s'", propstr
);
6711 state
.ls_parsable
= B_TRUE
;
6714 state
.ls_persist
= B_TRUE
;
6715 flags
= DLADM_OPT_PERSIST
;
6718 fields_str
= optarg
;
6721 die_opterr(optopt
, option
, use
);
6726 if (optind
== (argc
- 1)) {
6727 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6728 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6729 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6731 } else if (optind
!= argc
) {
6735 if (dladm_parse_link_props(propstr
, &proplist
, B_TRUE
)
6737 die("invalid link properties specified");
6738 state
.ls_proplist
= proplist
;
6739 state
.ls_status
= DLADM_STATUS_OK
;
6741 if (state
.ls_parsable
)
6742 ofmtflags
|= OFMT_PARSABLE
;
6744 ofmtflags
|= OFMT_WRAP
;
6746 oferr
= ofmt_open(fields_str
, linkprop_fields
, ofmtflags
, 0, &ofmt
);
6747 ofmt_check(oferr
, state
.ls_parsable
, ofmt
, die
, warn
);
6748 state
.ls_ofmt
= ofmt
;
6750 if (linkid
== DATALINK_ALL_LINKID
) {
6751 (void) dladm_walk_datalink_id(show_linkprop_onelink
, handle
,
6752 &state
, DATALINK_CLASS_ALL
, DATALINK_ANY_MEDIATYPE
, flags
);
6754 (void) show_linkprop_onelink(handle
, linkid
, &state
);
6757 dladm_free_props(proplist
);
6759 if (state
.ls_retstatus
!= DLADM_STATUS_OK
) {
6760 dladm_close(handle
);
6766 show_linkprop_onelink(dladm_handle_t hdl
, datalink_id_t linkid
, void *arg
)
6771 dladm_arg_list_t
*proplist
= NULL
;
6772 show_linkprop_state_t
*statep
= arg
;
6773 dlpi_handle_t dh
= NULL
;
6775 statep
->ls_status
= DLADM_STATUS_OK
;
6777 if (dladm_datalink_id2info(hdl
, linkid
, &flags
, NULL
, NULL
,
6778 statep
->ls_link
, MAXLINKNAMELEN
) != DLADM_STATUS_OK
) {
6779 statep
->ls_status
= DLADM_STATUS_NOTFOUND
;
6780 return (DLADM_WALK_CONTINUE
);
6783 if ((statep
->ls_persist
&& !(flags
& DLADM_OPT_PERSIST
)) ||
6784 (!statep
->ls_persist
&& !(flags
& DLADM_OPT_ACTIVE
))) {
6785 statep
->ls_status
= DLADM_STATUS_BADARG
;
6786 return (DLADM_WALK_CONTINUE
);
6789 proplist
= statep
->ls_proplist
;
6792 * When some WiFi links are opened for the first time, their hardware
6793 * automatically scans for APs and does other slow operations. Thus,
6794 * if there are no open links, the retrieval of link properties
6795 * (below) will proceed slowly unless we hold the link open.
6797 * Note that failure of dlpi_open() does not necessarily mean invalid
6798 * link properties, because dlpi_open() may fail because of incorrect
6799 * autopush configuration. Therefore, we ingore the return value of
6802 if (!statep
->ls_persist
)
6803 (void) dlpi_open(statep
->ls_link
, &dh
, 0);
6805 buf
= malloc((sizeof (char *) + DLADM_PROP_VAL_MAX
) *
6806 DLADM_MAX_PROP_VALCNT
+ MAX_PROP_LINE
);
6808 die("insufficient memory");
6810 statep
->ls_propvals
= (char **)(void *)buf
;
6811 for (i
= 0; i
< DLADM_MAX_PROP_VALCNT
; i
++) {
6812 statep
->ls_propvals
[i
] = buf
+
6813 sizeof (char *) * DLADM_MAX_PROP_VALCNT
+
6814 i
* DLADM_PROP_VAL_MAX
;
6816 statep
->ls_line
= buf
+
6817 (sizeof (char *) + DLADM_PROP_VAL_MAX
) * DLADM_MAX_PROP_VALCNT
;
6819 if (proplist
!= NULL
) {
6820 for (i
= 0; i
< proplist
->al_count
; i
++) {
6821 (void) show_linkprop(hdl
, linkid
,
6822 proplist
->al_info
[i
].ai_name
, statep
);
6825 (void) dladm_walk_linkprop(hdl
, linkid
, statep
,
6831 return (DLADM_WALK_CONTINUE
);
6835 reset_one_linkprop(dladm_handle_t dh
, datalink_id_t linkid
,
6836 const char *propname
, void *arg
)
6838 set_linkprop_state_t
*statep
= arg
;
6839 dladm_status_t status
;
6841 status
= dladm_set_linkprop(dh
, linkid
, propname
, NULL
, 0,
6842 DLADM_OPT_ACTIVE
| (statep
->ls_temp
? 0 : DLADM_OPT_PERSIST
));
6843 if (status
!= DLADM_STATUS_OK
&&
6844 status
!= DLADM_STATUS_PROPRDONLY
&&
6845 status
!= DLADM_STATUS_NOTSUP
) {
6846 warn_dlerr(status
, "cannot reset link property '%s' on '%s'",
6847 propname
, statep
->ls_name
);
6848 statep
->ls_status
= status
;
6851 return (DLADM_WALK_CONTINUE
);
6855 set_linkprop(int argc
, char **argv
, boolean_t reset
, const char *use
)
6858 char errmsg
[DLADM_STRSIZE
];
6859 char *altroot
= NULL
;
6860 datalink_id_t linkid
;
6861 boolean_t temp
= B_FALSE
;
6862 dladm_status_t status
= DLADM_STATUS_OK
;
6863 char propstr
[DLADM_STRSIZE
];
6864 dladm_arg_list_t
*proplist
= NULL
;
6867 bzero(propstr
, DLADM_STRSIZE
);
6869 while ((option
= getopt_long(argc
, argv
, ":p:R:t",
6870 prop_longopts
, NULL
)) != -1) {
6873 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
6874 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
6876 die("property list too long '%s'", propstr
);
6885 die_opterr(optopt
, option
, use
);
6890 /* get link name (required last argument) */
6891 if (optind
!= (argc
- 1))
6894 if (dladm_parse_link_props(propstr
, &proplist
, reset
) !=
6896 die("invalid link properties specified");
6898 if (proplist
== NULL
&& !reset
)
6899 die("link property must be specified");
6901 if (altroot
!= NULL
) {
6902 dladm_free_props(proplist
);
6903 altroot_cmd(altroot
, argc
, argv
);
6906 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
6908 if (status
!= DLADM_STATUS_OK
)
6909 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6911 if (proplist
== NULL
) {
6912 set_linkprop_state_t state
;
6914 state
.ls_name
= argv
[optind
];
6915 state
.ls_reset
= reset
;
6916 state
.ls_temp
= temp
;
6917 state
.ls_status
= DLADM_STATUS_OK
;
6919 (void) dladm_walk_linkprop(handle
, linkid
, &state
,
6920 reset_one_linkprop
);
6922 status
= state
.ls_status
;
6926 for (i
= 0; i
< proplist
->al_count
; i
++) {
6927 dladm_arg_info_t
*aip
= &proplist
->al_info
[i
];
6936 count
= aip
->ai_count
;
6938 warn("no value specified for '%s'",
6940 status
= DLADM_STATUS_BADARG
;
6944 status
= dladm_set_linkprop(handle
, linkid
, aip
->ai_name
, val
,
6945 count
, DLADM_OPT_ACTIVE
| (temp
? 0 : DLADM_OPT_PERSIST
));
6947 case DLADM_STATUS_OK
:
6949 case DLADM_STATUS_NOTFOUND
:
6950 warn("invalid link property '%s'", aip
->ai_name
);
6952 case DLADM_STATUS_BADVAL
: {
6955 char **propvals
= NULL
;
6956 uint_t valcnt
= DLADM_MAX_PROP_VALCNT
;
6959 ptr
= malloc((sizeof (char *) +
6960 DLADM_PROP_VAL_MAX
) * DLADM_MAX_PROP_VALCNT
+
6963 propvals
= (char **)(void *)ptr
;
6964 if (propvals
== NULL
)
6965 die("insufficient memory");
6967 for (j
= 0; j
< DLADM_MAX_PROP_VALCNT
; j
++) {
6968 propvals
[j
] = ptr
+ sizeof (char *) *
6969 DLADM_MAX_PROP_VALCNT
+
6970 j
* DLADM_PROP_VAL_MAX
;
6972 s
= dladm_get_linkprop(handle
, linkid
,
6973 DLADM_PROP_VAL_MODIFIABLE
, aip
->ai_name
, propvals
,
6976 if (s
!= DLADM_STATUS_OK
) {
6977 warn_dlerr(status
, "cannot set link property "
6978 "'%s' on '%s'", aip
->ai_name
, argv
[optind
]);
6984 lim
= ptr
+ DLADM_STRSIZE
;
6986 for (j
= 0; j
< valcnt
; j
++) {
6987 ptr
+= snprintf(ptr
, lim
- ptr
, "%s,",
6994 warn("link property '%s' must be one of: %s",
6995 aip
->ai_name
, errmsg
);
6997 warn("invalid link property '%s'", *val
);
7003 warn_dlerr(status
, "cannot reset link property "
7004 "'%s' on '%s'", aip
->ai_name
, argv
[optind
]);
7006 warn_dlerr(status
, "cannot set link property "
7007 "'%s' on '%s'", aip
->ai_name
, argv
[optind
]);
7013 dladm_free_props(proplist
);
7014 if (status
!= DLADM_STATUS_OK
) {
7015 dladm_close(handle
);
7021 do_set_linkprop(int argc
, char **argv
, const char *use
)
7023 set_linkprop(argc
, argv
, B_FALSE
, use
);
7027 do_reset_linkprop(int argc
, char **argv
, const char *use
)
7029 set_linkprop(argc
, argv
, B_TRUE
, use
);
7033 convert_secobj(char *buf
, uint_t len
, uint8_t *obj_val
, uint_t
*obj_lenp
,
7034 dladm_secobj_class_t
class)
7038 if (class == DLADM_SECOBJ_CLASS_WPA
) {
7039 if (len
< 8 || len
> 63)
7041 (void) memcpy(obj_val
, buf
, len
);
7046 if (class == DLADM_SECOBJ_CLASS_WEP
) {
7048 case 5: /* ASCII key sizes */
7050 (void) memcpy(obj_val
, buf
, len
);
7053 case 10: /* Hex key sizes, not preceded by 0x */
7055 error
= hexascii_to_octet(buf
, len
, obj_val
, obj_lenp
);
7057 case 12: /* Hex key sizes, preceded by 0x */
7059 if (strncmp(buf
, "0x", 2) != 0)
7061 error
= hexascii_to_octet(buf
+ 2, len
- 2,
7080 get_secobj_from_tty(uint_t
try, const char *objname
, char *buf
)
7084 struct termios stored
, current
;
7085 void (*sigfunc
)(int);
7088 * Turn off echo -- but before we do so, defer SIGINT handling
7089 * so that a ^C doesn't leave the terminal corrupted.
7091 sigfunc
= signal(SIGINT
, defersig
);
7092 (void) fflush(stdin
);
7093 (void) tcgetattr(0, &stored
);
7095 current
.c_lflag
&= ~(ICANON
|ECHO
);
7096 current
.c_cc
[VTIME
] = 0;
7097 current
.c_cc
[VMIN
] = 1;
7098 (void) tcsetattr(0, TCSANOW
, ¤t
);
7101 (void) printf(gettext("provide value for '%s': "), objname
);
7103 (void) printf(gettext("confirm value for '%s': "), objname
);
7105 (void) fflush(stdout
);
7106 while (signalled
== 0) {
7108 if (c
== '\n' || c
== '\r') {
7111 (void) putchar('\n');
7116 if (len
>= DLADM_SECOBJ_VAL_MAX
- 1)
7118 (void) putchar('*');
7121 (void) putchar('\n');
7122 (void) fflush(stdin
);
7125 * Restore terminal setting and handle deferred signals.
7127 (void) tcsetattr(0, TCSANOW
, &stored
);
7129 (void) signal(SIGINT
, sigfunc
);
7131 (void) kill(getpid(), signalled
);
7137 get_secobj_val(char *obj_name
, uint8_t *obj_val
, uint_t
*obj_lenp
,
7138 dladm_secobj_class_t
class, FILE *filep
)
7142 char buf
[DLADM_SECOBJ_VAL_MAX
], buf2
[DLADM_SECOBJ_VAL_MAX
];
7144 if (filep
== NULL
) {
7145 len
= get_secobj_from_tty(1, obj_name
, buf
);
7146 rval
= convert_secobj(buf
, len
, obj_val
, obj_lenp
, class);
7148 len2
= get_secobj_from_tty(2, obj_name
, buf2
);
7149 if (len
!= len2
|| memcmp(buf
, buf2
, len
) != 0)
7155 if (fgets(buf
, sizeof (buf
), filep
) == NULL
)
7157 if (isspace(buf
[0]))
7161 if (buf
[len
- 1] == '\n') {
7162 buf
[len
- 1] = '\0';
7167 (void) fclose(filep
);
7169 return (convert_secobj(buf
, len
, obj_val
, obj_lenp
, class));
7173 check_auth(const char *auth
)
7177 if ((pw
= getpwuid(getuid())) == NULL
)
7180 return (chkauthattr(auth
, pw
->pw_name
) != 0);
7184 do_create_secobj(int argc
, char **argv
, const char *use
)
7188 char *obj_name
= NULL
;
7189 char *class_name
= NULL
;
7190 uint8_t obj_val
[DLADM_SECOBJ_VAL_MAX
];
7192 boolean_t success
, temp
= B_FALSE
;
7193 dladm_status_t status
;
7194 dladm_secobj_class_t
class = -1;
7198 (void) memset(obj_val
, 0, DLADM_SECOBJ_VAL_MAX
);
7199 while ((option
= getopt_long(argc
, argv
, ":f:c:R:t",
7200 wifi_longopts
, NULL
)) != -1) {
7204 (void) seteuid(getuid());
7205 filep
= fopen(optarg
, "r");
7206 if (filep
== NULL
) {
7207 die("cannot open %s: %s", optarg
,
7210 (void) seteuid(euid
);
7213 class_name
= optarg
;
7214 status
= dladm_str2secobjclass(optarg
, &class);
7215 if (status
!= DLADM_STATUS_OK
) {
7216 die("invalid secure object class '%s', "
7217 "valid values are: wep, wpa", optarg
);
7224 status
= dladm_set_rootdir(optarg
);
7225 if (status
!= DLADM_STATUS_OK
) {
7226 die_dlerr(status
, "invalid directory "
7231 die_opterr(optopt
, option
, use
);
7236 if (optind
== (argc
- 1))
7237 obj_name
= argv
[optind
];
7238 else if (optind
!= argc
)
7242 die("secure object class required");
7244 if (obj_name
== NULL
)
7245 die("secure object name required");
7247 if (!dladm_valid_secobj_name(obj_name
))
7248 die("invalid secure object name '%s'", obj_name
);
7250 success
= check_auth(LINK_SEC_AUTH
);
7252 die("authorization '%s' is required", LINK_SEC_AUTH
);
7254 rval
= get_secobj_val(obj_name
, obj_val
, &obj_len
, class, filep
);
7258 die("invalid secure object class");
7261 die("invalid secure object value");
7264 die("verification failed");
7267 die("invalid secure object: %s", strerror(rval
));
7272 status
= dladm_set_secobj(handle
, obj_name
, class, obj_val
, obj_len
,
7273 DLADM_OPT_CREATE
| DLADM_OPT_ACTIVE
);
7274 if (status
!= DLADM_STATUS_OK
) {
7275 die_dlerr(status
, "could not create secure object '%s'",
7281 status
= dladm_set_secobj(handle
, obj_name
, class, obj_val
, obj_len
,
7283 if (status
!= DLADM_STATUS_OK
) {
7284 warn_dlerr(status
, "could not persistently create secure "
7285 "object '%s'", obj_name
);
7290 do_delete_secobj(int argc
, char **argv
, const char *use
)
7293 boolean_t temp
= B_FALSE
;
7295 dladm_status_t status
, pstatus
;
7297 char *field
, *token
, *lasts
= NULL
, c
;
7300 status
= pstatus
= DLADM_STATUS_OK
;
7301 while ((option
= getopt_long(argc
, argv
, ":R:t",
7302 wifi_longopts
, NULL
)) != -1) {
7308 status
= dladm_set_rootdir(optarg
);
7309 if (status
!= DLADM_STATUS_OK
) {
7310 die_dlerr(status
, "invalid directory "
7315 die_opterr(optopt
, option
, use
);
7320 if (optind
!= (argc
- 1))
7321 die("secure object name required");
7323 token
= argv
[optind
];
7324 while ((c
= *token
++) != '\0') {
7328 token
= strdup(argv
[optind
]);
7332 success
= check_auth(LINK_SEC_AUTH
);
7334 die("authorization '%s' is required", LINK_SEC_AUTH
);
7336 for (i
= 0; i
< nfields
; i
++) {
7338 field
= strtok_r(token
, ",", &lasts
);
7340 status
= dladm_unset_secobj(handle
, field
, DLADM_OPT_ACTIVE
);
7342 pstatus
= dladm_unset_secobj(handle
, field
,
7345 pstatus
= DLADM_STATUS_OK
;
7348 if (status
!= DLADM_STATUS_OK
) {
7349 warn_dlerr(status
, "could not delete secure object "
7352 if (pstatus
!= DLADM_STATUS_OK
) {
7353 warn_dlerr(pstatus
, "could not persistently delete "
7354 "secure object '%s'", field
);
7359 if (status
!= DLADM_STATUS_OK
|| pstatus
!= DLADM_STATUS_OK
) {
7360 dladm_close(handle
);
7365 typedef struct show_secobj_state
{
7366 boolean_t ss_persist
;
7367 boolean_t ss_parsable
;
7368 boolean_t ss_header
;
7369 ofmt_handle_t ss_ofmt
;
7370 } show_secobj_state_t
;
7374 show_secobj(dladm_handle_t dh
, void *arg
, const char *obj_name
)
7376 uint_t obj_len
= DLADM_SECOBJ_VAL_MAX
;
7377 uint8_t obj_val
[DLADM_SECOBJ_VAL_MAX
];
7378 char buf
[DLADM_STRSIZE
];
7380 dladm_secobj_class_t
class;
7381 show_secobj_state_t
*statep
= arg
;
7382 dladm_status_t status
;
7383 secobj_fields_buf_t sbuf
;
7385 bzero(&sbuf
, sizeof (secobj_fields_buf_t
));
7386 if (statep
->ss_persist
)
7387 flags
|= DLADM_OPT_PERSIST
;
7389 status
= dladm_get_secobj(dh
, obj_name
, &class, obj_val
, &obj_len
,
7391 if (status
!= DLADM_STATUS_OK
)
7392 die_dlerr(status
, "cannot get secure object '%s'", obj_name
);
7394 (void) snprintf(sbuf
.ss_obj_name
, sizeof (sbuf
.ss_obj_name
),
7396 (void) dladm_secobjclass2str(class, buf
);
7397 (void) snprintf(sbuf
.ss_class
, sizeof (sbuf
.ss_class
), "%s", buf
);
7398 if (getuid() == 0) {
7399 char val
[DLADM_SECOBJ_VAL_MAX
* 2];
7400 uint_t len
= sizeof (val
);
7402 if (octet_to_hexascii(obj_val
, obj_len
, val
, &len
) == 0)
7403 (void) snprintf(sbuf
.ss_val
,
7404 sizeof (sbuf
.ss_val
), "%s", val
);
7406 ofmt_print(statep
->ss_ofmt
, &sbuf
);
7411 do_show_secobj(int argc
, char **argv
, const char *use
)
7414 show_secobj_state_t state
;
7415 dladm_status_t status
;
7416 boolean_t o_arg
= B_FALSE
;
7419 char *fields_str
= NULL
;
7420 char *def_fields
= "object,class";
7421 char *all_fields
= "object,class,value";
7422 char *field
, *token
, *lasts
= NULL
, c
;
7424 ofmt_status_t oferr
;
7425 uint_t ofmtflags
= 0;
7428 bzero(&state
, sizeof (state
));
7429 state
.ss_parsable
= B_FALSE
;
7430 fields_str
= def_fields
;
7431 state
.ss_persist
= B_FALSE
;
7432 state
.ss_parsable
= B_FALSE
;
7433 state
.ss_header
= B_TRUE
;
7434 while ((option
= getopt_long(argc
, argv
, ":pPo:",
7435 wifi_longopts
, NULL
)) != -1) {
7438 state
.ss_parsable
= B_TRUE
;
7441 state
.ss_persist
= B_TRUE
;
7445 if (strcasecmp(optarg
, "all") == 0)
7446 fields_str
= all_fields
;
7448 fields_str
= optarg
;
7451 die_opterr(optopt
, option
, use
);
7456 if (state
.ss_parsable
&& !o_arg
)
7457 die("option -c requires -o");
7459 if (state
.ss_parsable
&& fields_str
== all_fields
)
7460 die("\"-o all\" is invalid with -p");
7462 if (state
.ss_parsable
)
7463 ofmtflags
|= OFMT_PARSABLE
;
7464 oferr
= ofmt_open(fields_str
, secobj_fields
, ofmtflags
, 0, &ofmt
);
7465 ofmt_check(oferr
, state
.ss_parsable
, ofmt
, die
, warn
);
7466 state
.ss_ofmt
= ofmt
;
7468 flags
= state
.ss_persist
? DLADM_OPT_PERSIST
: 0;
7470 if (optind
== (argc
- 1)) {
7471 uint_t obj_fields
= 1;
7473 token
= argv
[optind
];
7475 die("secure object name required");
7476 while ((c
= *token
++) != '\0') {
7480 token
= strdup(argv
[optind
]);
7483 for (i
= 0; i
< obj_fields
; i
++) {
7484 field
= strtok_r(token
, ",", &lasts
);
7486 if (!show_secobj(handle
, &state
, field
))
7492 } else if (optind
!= argc
)
7495 status
= dladm_walk_secobj(handle
, &state
, show_secobj
, flags
);
7497 if (status
!= DLADM_STATUS_OK
)
7498 die_dlerr(status
, "show-secobj");
7504 i_dladm_init_linkprop(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
7506 (void) dladm_init_linkprop(dh
, linkid
, B_TRUE
);
7507 return (DLADM_WALK_CONTINUE
);
7512 do_init_linkprop(int argc
, char **argv
, const char *use
)
7515 dladm_status_t status
;
7516 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
7517 datalink_media_t media
= DATALINK_ANY_MEDIATYPE
;
7518 uint_t any_media
= B_TRUE
;
7521 while ((option
= getopt(argc
, argv
, ":w")) != -1) {
7525 any_media
= B_FALSE
;
7529 * Because init-linkprop is not a public command,
7530 * print the usage instead.
7537 if (optind
== (argc
- 1)) {
7538 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
7539 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
)
7540 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
7541 } else if (optind
!= argc
) {
7545 if (linkid
== DATALINK_ALL_LINKID
) {
7547 * linkprops of links of other classes have been initialized as
7548 * part of the dladm up-xxx operation.
7550 (void) dladm_walk_datalink_id(i_dladm_init_linkprop
, handle
,
7551 NULL
, DATALINK_CLASS_PHYS
, media
, DLADM_OPT_PERSIST
);
7553 (void) dladm_init_linkprop(handle
, linkid
, any_media
);
7558 do_show_ether(int argc
, char **argv
, const char *use
)
7561 datalink_id_t linkid
;
7562 print_ether_state_t state
;
7563 char *fields_str
= NULL
;
7565 ofmt_status_t oferr
;
7566 uint_t ofmtflags
= 0;
7568 bzero(&state
, sizeof (state
));
7569 state
.es_link
= NULL
;
7570 state
.es_parsable
= B_FALSE
;
7572 while ((option
= getopt_long(argc
, argv
, "o:px",
7573 showeth_lopts
, NULL
)) != -1) {
7576 state
.es_extended
= B_TRUE
;
7579 state
.es_parsable
= B_TRUE
;
7582 fields_str
= optarg
;
7585 die_opterr(optopt
, option
, use
);
7590 if (optind
== (argc
- 1))
7591 state
.es_link
= argv
[optind
];
7593 if (state
.es_parsable
)
7594 ofmtflags
|= OFMT_PARSABLE
;
7595 oferr
= ofmt_open(fields_str
, ether_fields
, ofmtflags
,
7596 DLADM_DEFAULT_COL
, &ofmt
);
7597 ofmt_check(oferr
, state
.es_parsable
, ofmt
, die
, warn
);
7598 state
.es_ofmt
= ofmt
;
7600 if (state
.es_link
== NULL
) {
7601 (void) dladm_walk_datalink_id(show_etherprop
, handle
, &state
,
7602 DATALINK_CLASS_PHYS
, DL_ETHER
, DLADM_OPT_ACTIVE
);
7604 if (!link_is_ether(state
.es_link
, &linkid
))
7605 die("invalid link specified");
7606 (void) show_etherprop(handle
, linkid
, &state
);
7612 show_etherprop(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
7614 print_ether_state_t
*statep
= arg
;
7615 ether_fields_buf_t ebuf
;
7616 dladm_ether_info_t eattr
;
7617 dladm_status_t status
;
7619 bzero(&ebuf
, sizeof (ether_fields_buf_t
));
7620 if (dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
,
7621 ebuf
.eth_link
, sizeof (ebuf
.eth_link
)) != DLADM_STATUS_OK
) {
7622 return (DLADM_WALK_CONTINUE
);
7625 status
= dladm_ether_info(dh
, linkid
, &eattr
);
7626 if (status
!= DLADM_STATUS_OK
)
7629 (void) strlcpy(ebuf
.eth_ptype
, "current", sizeof (ebuf
.eth_ptype
));
7631 (void) dladm_ether_autoneg2str(ebuf
.eth_autoneg
,
7632 sizeof (ebuf
.eth_autoneg
), &eattr
, CURRENT
);
7633 (void) dladm_ether_pause2str(ebuf
.eth_pause
,
7634 sizeof (ebuf
.eth_pause
), &eattr
, CURRENT
);
7635 (void) dladm_ether_spdx2str(ebuf
.eth_spdx
,
7636 sizeof (ebuf
.eth_spdx
), &eattr
, CURRENT
);
7637 (void) strlcpy(ebuf
.eth_state
,
7638 dladm_linkstate2str(eattr
.lei_state
, ebuf
.eth_state
),
7639 sizeof (ebuf
.eth_state
));
7640 (void) strlcpy(ebuf
.eth_rem_fault
,
7641 (eattr
.lei_attr
[CURRENT
].le_fault
? "fault" : "none"),
7642 sizeof (ebuf
.eth_rem_fault
));
7644 ofmt_print(statep
->es_ofmt
, &ebuf
);
7646 if (statep
->es_extended
)
7647 show_ether_xprop(arg
, &eattr
);
7650 dladm_ether_info_done(&eattr
);
7651 return (DLADM_WALK_CONTINUE
);
7656 do_init_secobj(int argc
, char **argv
, const char *use
)
7658 dladm_status_t status
;
7660 status
= dladm_init_secobj(handle
);
7661 if (status
!= DLADM_STATUS_OK
)
7662 die_dlerr(status
, "secure object initialization failed");
7666 brCreate
, brAdd
, brModify
7670 create_modify_add_bridge(int argc
, char **argv
, const char *use
,
7671 enum bridge_func func
)
7675 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
7676 char *altroot
= NULL
;
7677 char *links
[MAXPORT
];
7678 datalink_id_t linkids
[MAXPORT
];
7679 dladm_status_t status
;
7681 UID_STP_CFG_T cfg
, cfg_old
;
7682 dladm_bridge_prot_t brprot
= DLADM_BRIDGE_PROT_UNKNOWN
;
7683 dladm_bridge_prot_t brprot_old
;
7685 /* Set up the default configuration values */
7687 cfg
.bridge_priority
= DEF_BR_PRIO
;
7688 cfg
.max_age
= DEF_BR_MAXAGE
;
7689 cfg
.hello_time
= DEF_BR_HELLOT
;
7690 cfg
.forward_delay
= DEF_BR_FWDELAY
;
7691 cfg
.force_version
= DEF_FORCE_VERS
;
7694 while ((option
= getopt_long(argc
, argv
, ":P:R:d:f:h:l:m:p:",
7695 bridge_lopts
, NULL
)) != -1) {
7699 die_opterr(optopt
, option
, use
);
7700 status
= dladm_bridge_str2prot(optarg
, &brprot
);
7701 if (status
!= DLADM_STATUS_OK
)
7702 die_dlerr(status
, "protection %s", optarg
);
7709 die_opterr(optopt
, option
, use
);
7710 if (cfg
.field_mask
& BR_CFG_DELAY
)
7711 die("forwarding delay set more than once");
7712 if (!str2int(optarg
, &cfg
.forward_delay
) ||
7713 cfg
.forward_delay
< MIN_BR_FWDELAY
||
7714 cfg
.forward_delay
> MAX_BR_FWDELAY
)
7715 die("incorrect forwarding delay");
7716 cfg
.field_mask
|= BR_CFG_DELAY
;
7720 die_opterr(optopt
, option
, use
);
7721 if (cfg
.field_mask
& BR_CFG_FORCE_VER
)
7722 die("force protocol set more than once");
7723 if (!str2int(optarg
, &cfg
.force_version
) ||
7724 cfg
.force_version
< 0)
7725 die("incorrect force protocol");
7726 cfg
.field_mask
|= BR_CFG_FORCE_VER
;
7730 die_opterr(optopt
, option
, use
);
7731 if (cfg
.field_mask
& BR_CFG_HELLO
)
7732 die("hello time set more than once");
7733 if (!str2int(optarg
, &cfg
.hello_time
) ||
7734 cfg
.hello_time
< MIN_BR_HELLOT
||
7735 cfg
.hello_time
> MAX_BR_HELLOT
)
7736 die("incorrect hello time");
7737 cfg
.field_mask
|= BR_CFG_HELLO
;
7740 if (func
== brModify
)
7741 die_opterr(optopt
, option
, use
);
7742 if (nlink
>= MAXPORT
)
7743 die("too many links specified");
7744 links
[nlink
++] = optarg
;
7748 die_opterr(optopt
, option
, use
);
7749 if (cfg
.field_mask
& BR_CFG_AGE
)
7750 die("max age set more than once");
7751 if (!str2int(optarg
, &cfg
.max_age
) ||
7752 cfg
.max_age
< MIN_BR_MAXAGE
||
7753 cfg
.max_age
> MAX_BR_MAXAGE
)
7754 die("incorrect max age");
7755 cfg
.field_mask
|= BR_CFG_AGE
;
7759 die_opterr(optopt
, option
, use
);
7760 if (cfg
.field_mask
& BR_CFG_PRIO
)
7761 die("priority set more than once");
7762 if (!str2int(optarg
, &cfg
.bridge_priority
) ||
7763 cfg
.bridge_priority
< MIN_BR_PRIO
||
7764 cfg
.bridge_priority
> MAX_BR_PRIO
)
7765 die("incorrect priority");
7766 cfg
.bridge_priority
&= 0xF000;
7767 cfg
.field_mask
|= BR_CFG_PRIO
;
7770 die_opterr(optopt
, option
, use
);
7775 /* get the bridge name (required last argument) */
7776 if (optind
!= (argc
-1))
7779 bridge
= argv
[optind
];
7780 if (!dladm_valid_bridgename(bridge
))
7781 die("invalid bridge name '%s'", bridge
);
7784 * Get the current properties, if any, and merge in with changes. This
7785 * is necessary (even with the field_mask feature) so that the
7786 * value-checking macros will produce the right results with proposed
7787 * changes to existing configuration. We only need it for those
7788 * parameters, though.
7790 (void) dladm_bridge_get_properties(bridge
, &cfg_old
, &brprot_old
);
7791 if (brprot
== DLADM_BRIDGE_PROT_UNKNOWN
)
7792 brprot
= brprot_old
;
7793 if (!(cfg
.field_mask
& BR_CFG_AGE
))
7794 cfg
.max_age
= cfg_old
.max_age
;
7795 if (!(cfg
.field_mask
& BR_CFG_HELLO
))
7796 cfg
.hello_time
= cfg_old
.hello_time
;
7797 if (!(cfg
.field_mask
& BR_CFG_DELAY
))
7798 cfg
.forward_delay
= cfg_old
.forward_delay
;
7800 if (!CHECK_BRIDGE_CONFIG(cfg
)) {
7801 warn("illegal forward delay / max age / hello time "
7803 if (NO_MAXAGE(cfg
)) {
7804 die("no max age possible: need forward delay >= %d or "
7805 "hello time <= %d", MIN_FWDELAY_NOM(cfg
),
7806 MAX_HELLOTIME_NOM(cfg
));
7807 } else if (SMALL_MAXAGE(cfg
)) {
7808 if (CAPPED_MAXAGE(cfg
))
7809 die("max age too small: need age >= %d and "
7810 "<= %d or hello time <= %d",
7811 MIN_MAXAGE(cfg
), MAX_MAXAGE(cfg
),
7812 MAX_HELLOTIME(cfg
));
7814 die("max age too small: need age >= %d or "
7816 MIN_MAXAGE(cfg
), MAX_HELLOTIME(cfg
));
7817 } else if (FLOORED_MAXAGE(cfg
)) {
7818 die("max age too large: need age >= %d and <= %d or "
7819 "forward delay >= %d",
7820 MIN_MAXAGE(cfg
), MAX_MAXAGE(cfg
),
7823 die("max age too large: need age <= %d or forward "
7825 MAX_MAXAGE(cfg
), MIN_FWDELAY(cfg
));
7829 if (altroot
!= NULL
)
7830 altroot_cmd(altroot
, argc
, argv
);
7832 for (n
= 0; n
< nlink
; n
++) {
7833 datalink_class_t
class;
7835 char pointless
[DLADM_STRSIZE
];
7837 if (dladm_name2info(handle
, links
[n
], &linkids
[n
], NULL
, &class,
7838 &media
) != DLADM_STATUS_OK
)
7839 die("invalid link name '%s'", links
[n
]);
7840 if (class & ~(DATALINK_CLASS_PHYS
| DATALINK_CLASS_AGGR
|
7841 DATALINK_CLASS_ETHERSTUB
| DATALINK_CLASS_SIMNET
))
7842 die("%s %s cannot be bridged",
7843 dladm_class2str(class, pointless
), links
[n
]);
7844 if (media
!= DL_ETHER
&& media
!= DL_100VG
&&
7845 media
!= DL_ETH_CSMA
&& media
!= DL_100BT
)
7846 die("%s interface %s cannot be bridged",
7847 dladm_media2str(media
, pointless
), links
[n
]);
7850 if (func
== brCreate
)
7851 flags
|= DLADM_OPT_CREATE
;
7853 if (func
!= brAdd
) {
7854 status
= dladm_bridge_configure(handle
, bridge
, &cfg
, brprot
,
7856 if (status
!= DLADM_STATUS_OK
)
7857 die_dlerr(status
, "create operation failed");
7860 status
= DLADM_STATUS_OK
;
7861 for (n
= 0; n
< nlink
; n
++) {
7862 status
= dladm_bridge_setlink(handle
, linkids
[n
], bridge
);
7863 if (status
!= DLADM_STATUS_OK
)
7869 * We were successful. If we're creating a new bridge, then
7870 * there's just one more step: enabling. If we're modifying or
7871 * just adding links, then we're done.
7873 if (func
!= brCreate
||
7874 (status
= dladm_bridge_enable(bridge
)) == DLADM_STATUS_OK
)
7878 /* clean up the partial configuration */
7879 for (i
= 0; i
< n
; i
++)
7880 (void) dladm_bridge_setlink(handle
, linkids
[i
], "");
7882 /* if failure for brCreate, then delete the bridge */
7883 if (func
== brCreate
)
7884 (void) dladm_bridge_delete(handle
, bridge
, flags
);
7887 die_dlerr(status
, "unable to add link %s to bridge %s",
7890 die_dlerr(status
, "unable to enable bridge %s", bridge
);
7894 do_create_bridge(int argc
, char **argv
, const char *use
)
7896 create_modify_add_bridge(argc
, argv
, use
, brCreate
);
7900 do_modify_bridge(int argc
, char **argv
, const char *use
)
7902 create_modify_add_bridge(argc
, argv
, use
, brModify
);
7906 do_add_bridge(int argc
, char **argv
, const char *use
)
7908 create_modify_add_bridge(argc
, argv
, use
, brAdd
);
7912 do_delete_bridge(int argc
, char **argv
, const char *use
)
7915 char *altroot
= NULL
;
7916 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
7917 dladm_status_t status
;
7920 while ((option
= getopt_long(argc
, argv
, ":R:", bridge_lopts
, NULL
)) !=
7927 die_opterr(optopt
, option
, use
);
7932 /* get the bridge name (required last argument) */
7933 if (optind
!= (argc
-1))
7936 if (altroot
!= NULL
)
7937 altroot_cmd(altroot
, argc
, argv
);
7939 status
= dladm_bridge_delete(handle
, argv
[optind
], flags
);
7940 if (status
!= DLADM_STATUS_OK
)
7941 die_dlerr(status
, "delete operation failed");
7945 do_remove_bridge(int argc
, char **argv
, const char *use
)
7949 char *links
[MAXPORT
];
7950 datalink_id_t linkids
[MAXPORT
];
7951 char *altroot
= NULL
;
7952 dladm_status_t status
;
7953 boolean_t removed_one
;
7956 while ((option
= getopt_long(argc
, argv
, ":R:l:", bridge_lopts
,
7963 if (nlink
>= MAXPORT
)
7964 die("too many links specified");
7965 links
[nlink
++] = optarg
;
7968 die_opterr(optopt
, option
, use
);
7976 /* get the bridge name (required last argument) */
7977 if (optind
!= (argc
-1))
7980 if (altroot
!= NULL
)
7981 altroot_cmd(altroot
, argc
, argv
);
7983 for (n
= 0; n
< nlink
; n
++) {
7984 char bridge
[MAXLINKNAMELEN
];
7986 if (dladm_name2info(handle
, links
[n
], &linkids
[n
], NULL
, NULL
,
7987 NULL
) != DLADM_STATUS_OK
)
7988 die("invalid link name '%s'", links
[n
]);
7989 status
= dladm_bridge_getlink(handle
, linkids
[n
], bridge
,
7991 if (status
!= DLADM_STATUS_OK
&&
7992 status
!= DLADM_STATUS_NOTFOUND
) {
7993 die_dlerr(status
, "cannot get bridge status on %s",
7996 if (status
== DLADM_STATUS_NOTFOUND
||
7997 strcmp(bridge
, argv
[optind
]) != 0)
7998 die("link %s is not on bridge %s", links
[n
],
8002 removed_one
= B_FALSE
;
8003 for (n
= 0; n
< nlink
; n
++) {
8004 status
= dladm_bridge_setlink(handle
, linkids
[n
], "");
8005 if (status
== DLADM_STATUS_OK
) {
8006 removed_one
= B_TRUE
;
8009 "cannot remove link %s from bridge %s",
8010 links
[n
], argv
[optind
]);
8014 die("unable to remove any links from bridge %s", argv
[optind
]);
8018 fmt_int(char *buf
, size_t buflen
, int value
, int runvalue
,
8019 boolean_t printstar
)
8021 (void) snprintf(buf
, buflen
, "%d", value
);
8022 if (value
!= runvalue
&& printstar
)
8023 (void) strlcat(buf
, "*", buflen
);
8027 fmt_bridge_id(char *buf
, size_t buflen
, UID_BRIDGE_ID_T
*bid
)
8029 (void) snprintf(buf
, buflen
, "%u/%x:%x:%x:%x:%x:%x", bid
->prio
,
8030 bid
->addr
[0], bid
->addr
[1], bid
->addr
[2], bid
->addr
[3],
8031 bid
->addr
[4], bid
->addr
[5]);
8034 static dladm_status_t
8035 print_bridge(show_state_t
*state
, datalink_id_t linkid
,
8036 bridge_fields_buf_t
*bbuf
)
8038 char link
[MAXLINKNAMELEN
];
8039 datalink_class_t
class;
8041 dladm_status_t status
;
8042 UID_STP_CFG_T smfcfg
, runcfg
;
8043 UID_STP_STATE_T stpstate
;
8044 dladm_bridge_prot_t smfprot
, runprot
;
8046 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
8047 NULL
, link
, sizeof (link
))) != DLADM_STATUS_OK
)
8050 if (!(state
->ls_flags
& flags
))
8051 return (DLADM_STATUS_NOTFOUND
);
8053 /* Convert observability node name back to bridge name */
8054 if (!dladm_observe_to_bridge(link
))
8055 return (DLADM_STATUS_NOTFOUND
);
8056 (void) strlcpy(bbuf
->bridge_name
, link
, sizeof (bbuf
->bridge_name
));
8059 * If the running value differs from the one in SMF, and parsable
8060 * output is not requested, then we show the running value with an
8063 (void) dladm_bridge_get_properties(bbuf
->bridge_name
, &smfcfg
,
8065 (void) dladm_bridge_run_properties(bbuf
->bridge_name
, &runcfg
,
8067 (void) snprintf(bbuf
->bridge_protect
, sizeof (bbuf
->bridge_protect
),
8068 "%s%s", state
->ls_parsable
|| smfprot
== runprot
? "" : "*",
8069 dladm_bridge_prot2str(runprot
));
8070 fmt_int(bbuf
->bridge_priority
, sizeof (bbuf
->bridge_priority
),
8071 smfcfg
.bridge_priority
, runcfg
.bridge_priority
,
8072 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_AGE
));
8073 fmt_int(bbuf
->bridge_bmaxage
, sizeof (bbuf
->bridge_bmaxage
),
8074 smfcfg
.max_age
, runcfg
.max_age
,
8075 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_AGE
));
8076 fmt_int(bbuf
->bridge_bhellotime
,
8077 sizeof (bbuf
->bridge_bhellotime
), smfcfg
.hello_time
,
8079 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_HELLO
));
8080 fmt_int(bbuf
->bridge_bfwddelay
, sizeof (bbuf
->bridge_bfwddelay
),
8081 smfcfg
.forward_delay
, runcfg
.forward_delay
,
8082 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_DELAY
));
8083 fmt_int(bbuf
->bridge_forceproto
, sizeof (bbuf
->bridge_forceproto
),
8084 smfcfg
.force_version
, runcfg
.force_version
,
8085 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_FORCE_VER
));
8086 fmt_int(bbuf
->bridge_holdtime
, sizeof (bbuf
->bridge_holdtime
),
8087 smfcfg
.hold_time
, runcfg
.hold_time
,
8088 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_HOLD_TIME
));
8090 if (dladm_bridge_state(bbuf
->bridge_name
, &stpstate
) ==
8092 fmt_bridge_id(bbuf
->bridge_address
,
8093 sizeof (bbuf
->bridge_address
), &stpstate
.bridge_id
);
8094 (void) snprintf(bbuf
->bridge_tctime
,
8095 sizeof (bbuf
->bridge_tctime
), "%lu",
8096 stpstate
.timeSince_Topo_Change
);
8097 (void) snprintf(bbuf
->bridge_tccount
,
8098 sizeof (bbuf
->bridge_tccount
), "%lu",
8099 stpstate
.Topo_Change_Count
);
8100 (void) snprintf(bbuf
->bridge_tchange
,
8101 sizeof (bbuf
->bridge_tchange
), "%u", stpstate
.Topo_Change
);
8102 fmt_bridge_id(bbuf
->bridge_desroot
,
8103 sizeof (bbuf
->bridge_desroot
), &stpstate
.designated_root
);
8104 (void) snprintf(bbuf
->bridge_rootcost
,
8105 sizeof (bbuf
->bridge_rootcost
), "%lu",
8106 stpstate
.root_path_cost
);
8107 (void) snprintf(bbuf
->bridge_rootport
,
8108 sizeof (bbuf
->bridge_rootport
), "%u", stpstate
.root_port
);
8109 (void) snprintf(bbuf
->bridge_maxage
,
8110 sizeof (bbuf
->bridge_maxage
), "%d", stpstate
.max_age
);
8111 (void) snprintf(bbuf
->bridge_hellotime
,
8112 sizeof (bbuf
->bridge_hellotime
), "%d", stpstate
.hello_time
);
8113 (void) snprintf(bbuf
->bridge_fwddelay
,
8114 sizeof (bbuf
->bridge_fwddelay
), "%d",
8115 stpstate
.forward_delay
);
8117 return (DLADM_STATUS_OK
);
8120 static dladm_status_t
8121 print_bridge_stats(show_state_t
*state
, datalink_id_t linkid
,
8122 bridge_statfields_buf_t
*bbuf
)
8124 char link
[MAXLINKNAMELEN
];
8125 datalink_class_t
class;
8127 dladm_status_t status
;
8130 brsum_t
*brsum
= (brsum_t
*)&state
->ls_prevstats
;
8133 /* This is a compile-time assertion; optimizer normally fixes this */
8134 extern void brsum_t_is_too_large(void);
8136 if (sizeof (*brsum
) > sizeof (state
->ls_prevstats
))
8137 brsum_t_is_too_large();
8139 if (state
->ls_firstonly
) {
8140 if (state
->ls_donefirst
)
8141 return (DLADM_WALK_CONTINUE
);
8142 state
->ls_donefirst
= B_TRUE
;
8144 bzero(brsum
, sizeof (*brsum
));
8146 bzero(&newval
, sizeof (newval
));
8148 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
8149 NULL
, link
, sizeof (link
))) != DLADM_STATUS_OK
)
8152 if (!(state
->ls_flags
& flags
))
8153 return (DLADM_STATUS_NOTFOUND
);
8155 if ((kcp
= kstat_open()) == NULL
) {
8156 warn("kstat open operation failed");
8157 return (DLADM_STATUS_OK
);
8159 if ((ksp
= kstat_lookup(kcp
, "bridge", 0, link
)) != NULL
&&
8160 kstat_read(kcp
, ksp
, NULL
) != -1) {
8161 if (dladm_kstat_value(ksp
, "drops", KSTAT_DATA_UINT64
,
8162 &newval
.drops
) == DLADM_STATUS_OK
) {
8163 (void) snprintf(bbuf
->bridges_drops
,
8164 sizeof (bbuf
->bridges_drops
), "%llu",
8165 newval
.drops
- brsum
->drops
);
8167 if (dladm_kstat_value(ksp
, "forward_direct", KSTAT_DATA_UINT64
,
8168 &newval
.forward_dir
) == DLADM_STATUS_OK
) {
8169 (void) snprintf(bbuf
->bridges_forwards
,
8170 sizeof (bbuf
->bridges_forwards
), "%llu",
8171 newval
.forward_dir
- brsum
->forward_dir
);
8173 if (dladm_kstat_value(ksp
, "forward_mbcast", KSTAT_DATA_UINT64
,
8174 &newval
.forward_mb
) == DLADM_STATUS_OK
) {
8175 (void) snprintf(bbuf
->bridges_mbcast
,
8176 sizeof (bbuf
->bridges_mbcast
), "%llu",
8177 newval
.forward_mb
- brsum
->forward_mb
);
8179 if (dladm_kstat_value(ksp
, "forward_unknown", KSTAT_DATA_UINT64
,
8180 &newval
.forward_unk
) == DLADM_STATUS_OK
) {
8181 (void) snprintf(bbuf
->bridges_unknown
,
8182 sizeof (bbuf
->bridges_unknown
), "%llu",
8183 newval
.forward_unk
- brsum
->forward_unk
);
8185 if (dladm_kstat_value(ksp
, "recv", KSTAT_DATA_UINT64
,
8186 &newval
.recv
) == DLADM_STATUS_OK
) {
8187 (void) snprintf(bbuf
->bridges_recv
,
8188 sizeof (bbuf
->bridges_recv
), "%llu",
8189 newval
.recv
- brsum
->recv
);
8191 if (dladm_kstat_value(ksp
, "sent", KSTAT_DATA_UINT64
,
8192 &newval
.sent
) == DLADM_STATUS_OK
) {
8193 (void) snprintf(bbuf
->bridges_sent
,
8194 sizeof (bbuf
->bridges_sent
), "%llu",
8195 newval
.sent
- brsum
->sent
);
8198 (void) kstat_close(kcp
);
8200 /* Convert observability node name back to bridge name */
8201 if (!dladm_observe_to_bridge(link
))
8202 return (DLADM_STATUS_NOTFOUND
);
8203 (void) strlcpy(bbuf
->bridges_name
, link
, sizeof (bbuf
->bridges_name
));
8207 return (DLADM_STATUS_OK
);
8211 * This structure carries around extra state information for the show-bridge
8212 * command and allows us to use common support functions.
8216 boolean_t show_stats
;
8222 show_bridge(dladm_handle_t handle
, datalink_id_t linkid
, void *arg
)
8224 show_brstate_t
*brstate
= arg
;
8227 if (brstate
->show_stats
) {
8228 bridge_statfields_buf_t bbuf
;
8230 bzero(&bbuf
, sizeof (bbuf
));
8231 brstate
->state
.ls_status
= print_bridge_stats(&brstate
->state
,
8235 bridge_fields_buf_t bbuf
;
8237 bzero(&bbuf
, sizeof (bbuf
));
8238 brstate
->state
.ls_status
= print_bridge(&brstate
->state
, linkid
,
8242 if (brstate
->state
.ls_status
== DLADM_STATUS_OK
)
8243 ofmt_print(brstate
->state
.ls_ofmt
, buf
);
8244 return (DLADM_WALK_CONTINUE
);
8248 fmt_bool(char *buf
, size_t buflen
, int val
)
8250 (void) strlcpy(buf
, val
? "yes" : "no", buflen
);
8253 static dladm_status_t
8254 print_bridge_link(show_state_t
*state
, datalink_id_t linkid
,
8255 bridge_link_fields_buf_t
*bbuf
)
8257 datalink_class_t
class;
8259 dladm_status_t status
;
8260 UID_STP_PORT_STATE_T stpstate
;
8262 status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class, NULL
,
8263 bbuf
->bridgel_link
, sizeof (bbuf
->bridgel_link
));
8264 if (status
!= DLADM_STATUS_OK
)
8267 if (!(state
->ls_flags
& flags
))
8268 return (DLADM_STATUS_NOTFOUND
);
8270 if (dladm_bridge_link_state(handle
, linkid
, &stpstate
) ==
8272 (void) snprintf(bbuf
->bridgel_index
,
8273 sizeof (bbuf
->bridgel_index
), "%u", stpstate
.port_no
);
8274 if (dlsym(RTLD_PROBE
, "STP_IN_state2str")) {
8275 (void) strlcpy(bbuf
->bridgel_state
,
8276 STP_IN_state2str(stpstate
.state
),
8277 sizeof (bbuf
->bridgel_state
));
8279 (void) snprintf(bbuf
->bridgel_state
,
8280 sizeof (bbuf
->bridgel_state
), "%u",
8283 (void) snprintf(bbuf
->bridgel_uptime
,
8284 sizeof (bbuf
->bridgel_uptime
), "%lu", stpstate
.uptime
);
8285 (void) snprintf(bbuf
->bridgel_opercost
,
8286 sizeof (bbuf
->bridgel_opercost
), "%lu",
8287 stpstate
.oper_port_path_cost
);
8288 fmt_bool(bbuf
->bridgel_operp2p
, sizeof (bbuf
->bridgel_operp2p
),
8289 stpstate
.oper_point2point
);
8290 fmt_bool(bbuf
->bridgel_operedge
,
8291 sizeof (bbuf
->bridgel_operedge
), stpstate
.oper_edge
);
8292 fmt_bridge_id(bbuf
->bridgel_desroot
,
8293 sizeof (bbuf
->bridgel_desroot
), &stpstate
.designated_root
);
8294 (void) snprintf(bbuf
->bridgel_descost
,
8295 sizeof (bbuf
->bridgel_descost
), "%lu",
8296 stpstate
.designated_cost
);
8297 fmt_bridge_id(bbuf
->bridgel_desbridge
,
8298 sizeof (bbuf
->bridgel_desbridge
),
8299 &stpstate
.designated_bridge
);
8300 (void) snprintf(bbuf
->bridgel_desport
,
8301 sizeof (bbuf
->bridgel_desport
), "%u",
8302 stpstate
.designated_port
);
8303 fmt_bool(bbuf
->bridgel_tcack
, sizeof (bbuf
->bridgel_tcack
),
8304 stpstate
.top_change_ack
);
8306 return (DLADM_STATUS_OK
);
8309 static dladm_status_t
8310 print_bridge_link_stats(show_state_t
*state
, datalink_id_t linkid
,
8311 bridge_link_statfields_buf_t
*bbuf
)
8313 datalink_class_t
class;
8315 dladm_status_t status
;
8316 UID_STP_PORT_STATE_T stpstate
;
8319 char bridge
[MAXLINKNAMELEN
];
8320 char kstatname
[MAXLINKNAMELEN
*2 + 1];
8321 brlsum_t
*brlsum
= (brlsum_t
*)&state
->ls_prevstats
;
8324 /* This is a compile-time assertion; optimizer normally fixes this */
8325 extern void brlsum_t_is_too_large(void);
8327 if (sizeof (*brlsum
) > sizeof (state
->ls_prevstats
))
8328 brlsum_t_is_too_large();
8330 if (state
->ls_firstonly
) {
8331 if (state
->ls_donefirst
)
8332 return (DLADM_WALK_CONTINUE
);
8333 state
->ls_donefirst
= B_TRUE
;
8335 bzero(brlsum
, sizeof (*brlsum
));
8337 bzero(&newval
, sizeof (newval
));
8339 status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class, NULL
,
8340 bbuf
->bridgels_link
, sizeof (bbuf
->bridgels_link
));
8341 if (status
!= DLADM_STATUS_OK
)
8344 if (!(state
->ls_flags
& flags
))
8345 return (DLADM_STATUS_NOTFOUND
);
8347 if (dladm_bridge_link_state(handle
, linkid
, &stpstate
) ==
8349 newval
.cfgbpdu
= stpstate
.rx_cfg_bpdu_cnt
;
8350 newval
.tcnbpdu
= stpstate
.rx_tcn_bpdu_cnt
;
8351 newval
.rstpbpdu
= stpstate
.rx_rstp_bpdu_cnt
;
8352 newval
.txbpdu
= stpstate
.txCount
;
8354 (void) snprintf(bbuf
->bridgels_cfgbpdu
,
8355 sizeof (bbuf
->bridgels_cfgbpdu
), "%lu",
8356 newval
.cfgbpdu
- brlsum
->cfgbpdu
);
8357 (void) snprintf(bbuf
->bridgels_tcnbpdu
,
8358 sizeof (bbuf
->bridgels_tcnbpdu
), "%lu",
8359 newval
.tcnbpdu
- brlsum
->tcnbpdu
);
8360 (void) snprintf(bbuf
->bridgels_rstpbpdu
,
8361 sizeof (bbuf
->bridgels_rstpbpdu
), "%lu",
8362 newval
.rstpbpdu
- brlsum
->rstpbpdu
);
8363 (void) snprintf(bbuf
->bridgels_txbpdu
,
8364 sizeof (bbuf
->bridgels_txbpdu
), "%lu",
8365 newval
.txbpdu
- brlsum
->txbpdu
);
8368 if ((status
= dladm_bridge_getlink(handle
, linkid
, bridge
,
8369 sizeof (bridge
))) != DLADM_STATUS_OK
)
8371 (void) snprintf(kstatname
, sizeof (kstatname
), "%s0-%s", bridge
,
8372 bbuf
->bridgels_link
);
8373 if ((kcp
= kstat_open()) == NULL
) {
8374 warn("kstat open operation failed");
8377 if ((ksp
= kstat_lookup(kcp
, "bridge", 0, kstatname
)) != NULL
&&
8378 kstat_read(kcp
, ksp
, NULL
) != -1) {
8379 if (dladm_kstat_value(ksp
, "drops", KSTAT_DATA_UINT64
,
8380 &newval
.drops
) != -1) {
8381 (void) snprintf(bbuf
->bridgels_drops
,
8382 sizeof (bbuf
->bridgels_drops
), "%llu",
8383 newval
.drops
- brlsum
->drops
);
8385 if (dladm_kstat_value(ksp
, "recv", KSTAT_DATA_UINT64
,
8386 &newval
.recv
) != -1) {
8387 (void) snprintf(bbuf
->bridgels_recv
,
8388 sizeof (bbuf
->bridgels_recv
), "%llu",
8389 newval
.recv
- brlsum
->recv
);
8391 if (dladm_kstat_value(ksp
, "xmit", KSTAT_DATA_UINT64
,
8392 &newval
.xmit
) != -1) {
8393 (void) snprintf(bbuf
->bridgels_xmit
,
8394 sizeof (bbuf
->bridgels_xmit
), "%llu",
8395 newval
.xmit
- brlsum
->xmit
);
8398 (void) kstat_close(kcp
);
8406 show_bridge_link(datalink_id_t linkid
, show_brstate_t
*brstate
)
8410 if (brstate
->show_stats
) {
8411 bridge_link_statfields_buf_t bbuf
;
8413 bzero(&bbuf
, sizeof (bbuf
));
8414 brstate
->state
.ls_status
= print_bridge_link_stats(
8415 &brstate
->state
, linkid
, &bbuf
);
8418 bridge_link_fields_buf_t bbuf
;
8420 bzero(&bbuf
, sizeof (bbuf
));
8421 brstate
->state
.ls_status
= print_bridge_link(&brstate
->state
,
8425 if (brstate
->state
.ls_status
== DLADM_STATUS_OK
)
8426 ofmt_print(brstate
->state
.ls_ofmt
, buf
);
8431 show_bridge_link_walk(dladm_handle_t handle
, datalink_id_t linkid
, void *arg
)
8433 show_brstate_t
*brstate
= arg
;
8434 char bridge
[MAXLINKNAMELEN
];
8436 if (dladm_bridge_getlink(handle
, linkid
, bridge
, sizeof (bridge
)) ==
8437 DLADM_STATUS_OK
&& strcmp(bridge
, brstate
->bridge
) == 0) {
8438 show_bridge_link(linkid
, brstate
);
8440 return (DLADM_WALK_CONTINUE
);
8444 show_bridge_fwd(dladm_handle_t handle
, bridge_listfwd_t
*blf
,
8445 show_state_t
*state
)
8447 bridge_fwd_fields_buf_t bbuf
;
8449 bzero(&bbuf
, sizeof (bbuf
));
8450 (void) snprintf(bbuf
.bridgef_dest
, sizeof (bbuf
.bridgef_dest
),
8451 "%s", ether_ntoa((struct ether_addr
*)blf
->blf_dest
));
8452 if (blf
->blf_is_local
) {
8453 (void) strlcpy(bbuf
.bridgef_flags
, "L",
8454 sizeof (bbuf
.bridgef_flags
));
8456 (void) snprintf(bbuf
.bridgef_age
, sizeof (bbuf
.bridgef_age
),
8457 "%2d.%03d", blf
->blf_ms_age
/ 1000, blf
->blf_ms_age
% 1000);
8458 if (blf
->blf_trill_nick
!= 0) {
8459 (void) snprintf(bbuf
.bridgef_output
,
8460 sizeof (bbuf
.bridgef_output
), "%u",
8461 blf
->blf_trill_nick
);
8464 if (blf
->blf_linkid
!= DATALINK_INVALID_LINKID
&&
8465 blf
->blf_trill_nick
== 0) {
8466 state
->ls_status
= dladm_datalink_id2info(handle
,
8467 blf
->blf_linkid
, NULL
, NULL
, NULL
, bbuf
.bridgef_output
,
8468 sizeof (bbuf
.bridgef_output
));
8470 if (state
->ls_status
== DLADM_STATUS_OK
)
8471 ofmt_print(state
->ls_ofmt
, &bbuf
);
8475 show_bridge_trillnick(trill_listnick_t
*tln
, show_state_t
*state
)
8477 bridge_trill_fields_buf_t bbuf
;
8479 bzero(&bbuf
, sizeof (bbuf
));
8480 (void) snprintf(bbuf
.bridget_nick
, sizeof (bbuf
.bridget_nick
),
8481 "%u", tln
->tln_nick
);
8482 if (tln
->tln_ours
) {
8483 (void) strlcpy(bbuf
.bridget_flags
, "L",
8484 sizeof (bbuf
.bridget_flags
));
8486 state
->ls_status
= dladm_datalink_id2info(handle
,
8487 tln
->tln_linkid
, NULL
, NULL
, NULL
, bbuf
.bridget_link
,
8488 sizeof (bbuf
.bridget_link
));
8489 (void) snprintf(bbuf
.bridget_nexthop
,
8490 sizeof (bbuf
.bridget_nexthop
), "%s",
8491 ether_ntoa((struct ether_addr
*)tln
->tln_nexthop
));
8493 if (state
->ls_status
== DLADM_STATUS_OK
)
8494 ofmt_print(state
->ls_ofmt
, &bbuf
);
8498 do_show_bridge(int argc
, char **argv
, const char *use
)
8502 bridgeMode
, linkMode
, fwdMode
, trillMode
8503 } op_mode
= bridgeMode
;
8504 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
8505 boolean_t parsable
= B_FALSE
;
8506 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
8508 show_brstate_t brstate
;
8509 dladm_status_t status
;
8510 char *fields_str
= NULL
;
8511 /* default: bridge-related data */
8512 char *all_fields
= "bridge,protect,address,priority,bmaxage,"
8513 "bhellotime,bfwddelay,forceproto,tctime,tccount,tchange,"
8514 "desroot,rootcost,rootport,maxage,hellotime,fwddelay,holdtime";
8515 char *default_fields
= "bridge,protect,address,priority,"
8517 char *all_statfields
= "bridge,drops,forwards,mbcast,"
8518 "unknown,recv,sent";
8519 char *default_statfields
= "bridge,drops,forwards,mbcast,"
8521 /* -l: link-related data */
8522 char *all_link_fields
= "link,index,state,uptime,opercost,"
8523 "operp2p,operedge,desroot,descost,desbridge,desport,tcack";
8524 char *default_link_fields
= "link,state,uptime,desroot";
8525 char *all_link_statfields
= "link,cfgbpdu,tcnbpdu,rstpbpdu,"
8526 "txbpdu,drops,recv,xmit";
8527 char *default_link_statfields
= "link,drops,recv,xmit";
8528 /* -f: bridge forwarding table related data */
8529 char *default_fwd_fields
= "dest,age,flags,output";
8530 /* -t: TRILL nickname table related data */
8531 char *default_trill_fields
= "nick,flags,link,nexthop";
8534 ofmt_field_t
*field_arr
;
8536 ofmt_status_t oferr
;
8537 uint_t ofmtflags
= 0;
8539 bzero(&brstate
, sizeof (brstate
));
8542 while ((option
= getopt_long(argc
, argv
, ":fi:lo:pst",
8543 bridge_show_lopts
, NULL
)) != -1) {
8546 if (op_mode
!= bridgeMode
&& op_mode
!= fwdMode
)
8547 die("-f is incompatible with -l or -t");
8553 if (!str2int(optarg
, &interval
) || interval
== 0)
8554 die("invalid interval value '%s'", optarg
);
8557 if (op_mode
!= bridgeMode
&& op_mode
!= linkMode
)
8558 die("-l is incompatible with -f or -t");
8562 fields_str
= optarg
;
8570 if (brstate
.show_stats
)
8572 brstate
.show_stats
= B_TRUE
;
8575 if (op_mode
!= bridgeMode
&& op_mode
!= trillMode
)
8576 die("-t is incompatible with -f or -l");
8577 op_mode
= trillMode
;
8580 die_opterr(optopt
, option
, use
);
8585 if (interval
!= 0 && !brstate
.show_stats
)
8586 die("the -i option can be used only with -s");
8588 if ((op_mode
== fwdMode
|| op_mode
== trillMode
) && brstate
.show_stats
)
8589 die("the -f/-t and -s options cannot be used together");
8591 /* get the bridge name (optional last argument) */
8592 if (optind
== (argc
-1)) {
8593 char lname
[MAXLINKNAMELEN
];
8595 datalink_class_t
class;
8597 brstate
.bridge
= argv
[optind
];
8598 (void) snprintf(lname
, sizeof (lname
), "%s0", brstate
.bridge
);
8599 if ((status
= dladm_name2info(handle
, lname
, &linkid
, &lnkflg
,
8600 &class, NULL
)) != DLADM_STATUS_OK
) {
8601 die_dlerr(status
, "bridge %s is not valid",
8605 if (class != DATALINK_CLASS_BRIDGE
)
8606 die("%s is not a bridge", brstate
.bridge
);
8608 if (!(lnkflg
& flags
)) {
8609 die_dlerr(DLADM_STATUS_BADARG
,
8610 "bridge %s is temporarily removed", brstate
.bridge
);
8612 } else if (optind
!= argc
) {
8614 } else if (op_mode
!= bridgeMode
) {
8615 die("bridge name required for -l, -f, or -t");
8619 brstate
.state
.ls_parsable
= parsable
;
8620 brstate
.state
.ls_flags
= flags
;
8621 brstate
.state
.ls_firstonly
= (interval
!= 0);
8625 if (brstate
.show_stats
) {
8626 default_str
= default_statfields
;
8627 all_str
= all_statfields
;
8628 field_arr
= bridge_statfields
;
8630 default_str
= default_fields
;
8631 all_str
= all_fields
;
8632 field_arr
= bridge_fields
;
8637 if (brstate
.show_stats
) {
8638 default_str
= default_link_statfields
;
8639 all_str
= all_link_statfields
;
8640 field_arr
= bridge_link_statfields
;
8642 default_str
= default_link_fields
;
8643 all_str
= all_link_fields
;
8644 field_arr
= bridge_link_fields
;
8649 default_str
= all_str
= default_fwd_fields
;
8650 field_arr
= bridge_fwd_fields
;
8654 default_str
= all_str
= default_trill_fields
;
8655 field_arr
= bridge_trill_fields
;
8659 if (fields_str
== NULL
)
8660 fields_str
= default_str
;
8661 else if (strcasecmp(fields_str
, "all") == 0)
8662 fields_str
= all_str
;
8665 ofmtflags
|= OFMT_PARSABLE
;
8666 oferr
= ofmt_open(fields_str
, field_arr
, ofmtflags
, 0, &ofmt
);
8667 ofmt_check(oferr
, brstate
.state
.ls_parsable
, ofmt
, die
, warn
);
8668 brstate
.state
.ls_ofmt
= ofmt
;
8671 brstate
.state
.ls_donefirst
= B_FALSE
;
8674 if (linkid
== DATALINK_ALL_LINKID
) {
8675 (void) dladm_walk_datalink_id(show_bridge
,
8676 handle
, &brstate
, DATALINK_CLASS_BRIDGE
,
8677 DATALINK_ANY_MEDIATYPE
, flags
);
8679 (void) show_bridge(handle
, linkid
, &brstate
);
8680 if (brstate
.state
.ls_status
!=
8682 die_dlerr(brstate
.state
.ls_status
,
8683 "failed to show bridge %s",
8693 dlp
= dladm_bridge_get_portlist(brstate
.bridge
,
8696 for (i
= 0; i
< nlinks
; i
++)
8697 show_bridge_link(dlp
[i
], &brstate
);
8698 dladm_bridge_free_portlist(dlp
);
8699 } else if (errno
== ENOENT
) {
8700 /* bridge not running; iterate on libdladm */
8701 (void) dladm_walk_datalink_id(
8702 show_bridge_link_walk
, handle
,
8703 &brstate
, DATALINK_CLASS_PHYS
|
8704 DATALINK_CLASS_AGGR
|
8705 DATALINK_CLASS_ETHERSTUB
,
8706 DATALINK_ANY_MEDIATYPE
, flags
);
8708 die("unable to get port list for bridge %s: %s",
8709 brstate
.bridge
, strerror(errno
));
8715 bridge_listfwd_t
*blf
;
8718 blf
= dladm_bridge_get_fwdtable(handle
, brstate
.bridge
,
8721 die("unable to get forwarding entries for "
8722 "bridge %s", brstate
.bridge
);
8724 for (i
= 0; i
< nfwd
; i
++)
8725 show_bridge_fwd(handle
, blf
+ i
,
8727 dladm_bridge_free_fwdtable(blf
);
8733 trill_listnick_t
*tln
;
8736 tln
= dladm_bridge_get_trillnick(brstate
.bridge
,
8739 if (errno
== ENOENT
)
8740 die("bridge %s is not running TRILL",
8743 die("unable to get TRILL nickname "
8744 "entries for bridge %s",
8747 for (i
= 0; i
< nnick
; i
++)
8748 show_bridge_trillnick(tln
+ i
,
8750 dladm_bridge_free_trillnick(tln
);
8757 (void) sleep(interval
);
8762 * "-R" option support. It is used for live upgrading. Append dladm commands
8763 * to a upgrade script which will be run when the alternative root boots up:
8765 * - If the /etc/dladm/datalink.conf file exists on the alternative root,
8766 * append dladm commands to the <altroot>/var/svc/profile/upgrade_datalink
8767 * script. This script will be run as part of the network/physical service.
8768 * We cannot defer this to /var/svc/profile/upgrade because then the
8769 * configuration will not be able to take effect before network/physical
8770 * plumbs various interfaces.
8772 * - If the /etc/dladm/datalink.conf file does not exist on the alternative
8773 * root, append dladm commands to the <altroot>/var/svc/profile/upgrade script,
8774 * which will be run in the manifest-import service.
8776 * Note that the SMF team is considering to move the manifest-import service
8777 * to be run at the very begining of boot. Once that is done, the need for
8778 * the /var/svc/profile/upgrade_datalink script will not exist any more.
8781 altroot_cmd(char *altroot
, int argc
, char *argv
[])
8783 char path
[MAXPATHLEN
];
8789 * Check for the existence of the /etc/dladm/datalink.conf
8790 * configuration file, and determine the name of script file.
8792 (void) snprintf(path
, MAXPATHLEN
, "/%s/etc/dladm/datalink.conf",
8794 if (stat(path
, &stbuf
) < 0) {
8795 (void) snprintf(path
, MAXPATHLEN
, "/%s/%s", altroot
,
8798 (void) snprintf(path
, MAXPATHLEN
, "/%s/%s", altroot
,
8799 SMF_UPGRADEDATALINK_FILE
);
8802 if ((fp
= fopen(path
, "a+")) == NULL
)
8803 die("operation not supported on %s", altroot
);
8805 (void) fprintf(fp
, "/sbin/dladm ");
8806 for (i
= 0; i
< argc
; i
++) {
8808 * Directly write to the file if it is not the "-R <altroot>"
8809 * option. In which case, skip it.
8811 if (strcmp(argv
[i
], "-R") != 0)
8812 (void) fprintf(fp
, "%s ", argv
[i
]);
8816 (void) fprintf(fp
, "%s\n", SMF_DLADM_UPGRADE_MSG
);
8818 dladm_close(handle
);
8823 * Convert the string to an integer. Note that the string must not have any
8824 * trailing non-integer characters.
8827 str2int(const char *str
, int *valp
)
8833 val
= strtol(str
, &endp
, 10);
8834 if (errno
!= 0 || *endp
!= '\0')
8843 warn(const char *format
, ...)
8847 format
= gettext(format
);
8848 (void) fprintf(stderr
, "%s: warning: ", progname
);
8850 va_start(alist
, format
);
8851 (void) vfprintf(stderr
, format
, alist
);
8854 (void) putc('\n', stderr
);
8859 warn_dlerr(dladm_status_t err
, const char *format
, ...)
8862 char errmsg
[DLADM_STRSIZE
];
8864 format
= gettext(format
);
8865 (void) fprintf(stderr
, gettext("%s: warning: "), progname
);
8867 va_start(alist
, format
);
8868 (void) vfprintf(stderr
, format
, alist
);
8870 (void) fprintf(stderr
, ": %s\n", dladm_status2str(err
, errmsg
));
8874 * Also closes the dladm handle if it is not NULL.
8878 die_dlerr(dladm_status_t err
, const char *format
, ...)
8881 char errmsg
[DLADM_STRSIZE
];
8883 format
= gettext(format
);
8884 (void) fprintf(stderr
, "%s: ", progname
);
8886 va_start(alist
, format
);
8887 (void) vfprintf(stderr
, format
, alist
);
8889 (void) fprintf(stderr
, ": %s\n", dladm_status2str(err
, errmsg
));
8891 /* close dladm handle if it was opened */
8893 dladm_close(handle
);
8900 die(const char *format
, ...)
8904 format
= gettext(format
);
8905 (void) fprintf(stderr
, "%s: ", progname
);
8907 va_start(alist
, format
);
8908 (void) vfprintf(stderr
, format
, alist
);
8911 (void) putc('\n', stderr
);
8913 /* close dladm handle if it was opened */
8915 dladm_close(handle
);
8923 die("the option -%c cannot be specified more than once", opt
);
8927 die_opterr(int opt
, int opterr
, const char *usage
)
8931 die("option '-%c' requires a value\nusage: %s", opt
,
8936 die("unrecognized option '-%c'\nusage: %s", opt
,
8943 show_ether_xprop(void *arg
, dladm_ether_info_t
*eattr
)
8945 print_ether_state_t
*statep
= arg
;
8946 ether_fields_buf_t ebuf
;
8949 for (i
= CAPABLE
; i
<= PEERADV
; i
++) {
8950 bzero(&ebuf
, sizeof (ebuf
));
8951 (void) strlcpy(ebuf
.eth_ptype
, ptype
[i
],
8952 sizeof (ebuf
.eth_ptype
));
8953 (void) dladm_ether_autoneg2str(ebuf
.eth_autoneg
,
8954 sizeof (ebuf
.eth_autoneg
), eattr
, i
);
8955 (void) dladm_ether_spdx2str(ebuf
.eth_spdx
,
8956 sizeof (ebuf
.eth_spdx
), eattr
, i
);
8957 (void) dladm_ether_pause2str(ebuf
.eth_pause
,
8958 sizeof (ebuf
.eth_pause
), eattr
, i
);
8959 (void) strlcpy(ebuf
.eth_rem_fault
,
8960 (eattr
->lei_attr
[i
].le_fault
? "fault" : "none"),
8961 sizeof (ebuf
.eth_rem_fault
));
8962 ofmt_print(statep
->es_ofmt
, &ebuf
);
8968 link_is_ether(const char *link
, datalink_id_t
*linkid
)
8971 datalink_class_t
class;
8973 if (dladm_name2info(handle
, link
, linkid
, NULL
, &class, &media
) ==
8975 if (class == DATALINK_CLASS_PHYS
&& media
== DL_ETHER
)
8982 * default output callback function that, when invoked,
8983 * prints string which is offset by ofmt_arg->ofmt_id within buf.
8986 print_default_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
8990 value
= (char *)ofarg
->ofmt_cbarg
+ ofarg
->ofmt_id
;
8991 (void) strlcpy(buf
, value
, bufsize
);
8996 * Called from the walker dladm_walk_datalink_id() for each IB partition to
8997 * display IB partition specific information.
8999 static dladm_status_t
9000 print_part(show_part_state_t
*state
, datalink_id_t linkid
)
9002 dladm_part_attr_t attr
;
9003 dladm_status_t status
;
9005 char part_over
[MAXLINKNAMELEN
];
9006 char part_name
[MAXLINKNAMELEN
];
9007 part_fields_buf_t pbuf
;
9008 boolean_t force_in_conf
= B_FALSE
;
9011 * Get the information about the IB partition from the partition
9012 * datlink ID 'linkid'.
9014 if ((status
= dladm_part_info(handle
, linkid
, &attr
, state
->ps_flags
))
9019 * If an IB Phys link name was provided on the command line we have
9020 * the Phys link's datalink ID in the ps_over_id field of the state
9021 * structure. Proceed only if the IB partition represented by 'linkid'
9022 * was created over Phys link denoted by ps_over_id. The
9023 * 'dia_physlinkid' field of dladm_part_attr_t represents the IB Phys
9024 * link over which the partition was created.
9026 if (state
->ps_over_id
!= DATALINK_ALL_LINKID
)
9027 if (state
->ps_over_id
!= attr
.dia_physlinkid
)
9028 return (DLADM_STATUS_OK
);
9031 * The linkid argument passed to this function is the datalink ID
9032 * of the IB Partition. Get the partitions name from this linkid.
9034 if (dladm_datalink_id2info(handle
, linkid
, NULL
, NULL
,
9035 NULL
, part_name
, sizeof (part_name
)) != DLADM_STATUS_OK
)
9036 return (DLADM_STATUS_BADARG
);
9038 bzero(part_over
, sizeof (part_over
));
9041 * The 'dia_physlinkid' field contains the datalink ID of the IB Phys
9042 * link over which the partition was created. Use this linkid to get the
9045 if (dladm_datalink_id2info(handle
, attr
.dia_physlinkid
, NULL
, NULL
,
9046 NULL
, part_over
, sizeof (part_over
)) != DLADM_STATUS_OK
)
9047 (void) sprintf(part_over
, "?");
9048 state
->ps_found
= B_TRUE
;
9051 * Read the FFORCE field from this datalink's persistent configuration
9052 * database line to determine if this datalink was created forcibly.
9053 * If this datalink is a temporary datalink, then it will not have an
9054 * entry in the persistent configuration, so check if force create flag
9055 * is set in the partition attributes.
9057 * We need this two level check since persistent partitions brought up
9058 * by up-part during boot will have force create flag always set, since
9059 * we want up-part to always succeed even if the port is currently down
9060 * or P_Key is not yet available in the subnet.
9062 if ((status
= dladm_getsnap_conf(handle
, linkid
, &conf
)) ==
9064 (void) dladm_get_conf_field(handle
, conf
, FFORCE
,
9065 &force_in_conf
, sizeof (boolean_t
));
9066 dladm_destroy_conf(handle
, conf
);
9067 } else if (status
== DLADM_STATUS_NOTFOUND
) {
9069 * for a temp link the force create flag will determine
9070 * whether it was created with force flag.
9072 force_in_conf
= ((attr
.dia_flags
& DLADM_PART_FORCE_CREATE
)
9076 (void) snprintf(pbuf
.part_link
, sizeof (pbuf
.part_link
),
9079 (void) snprintf(pbuf
.part_over
, sizeof (pbuf
.part_over
),
9082 (void) snprintf(pbuf
.part_pkey
, sizeof (pbuf
.part_pkey
),
9083 "%X", attr
.dia_pkey
);
9085 (void) get_linkstate(pbuf
.part_link
, B_TRUE
, pbuf
.part_state
);
9087 (void) snprintf(pbuf
.part_flags
, sizeof (pbuf
.part_flags
),
9088 "%c----", force_in_conf
? 'f' : '-');
9090 ofmt_print(state
->ps_ofmt
, &pbuf
);
9092 return (DLADM_STATUS_OK
);
9097 show_part(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
9099 ((show_part_state_t
*)arg
)->ps_status
= print_part(arg
, linkid
);
9100 return (DLADM_WALK_CONTINUE
);
9104 * Show the information about the IB partition objects.
9107 do_show_part(int argc
, char *argv
[], const char *use
)
9110 boolean_t l_arg
= B_FALSE
;
9111 uint32_t flags
= DLADM_OPT_ACTIVE
;
9112 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
9113 datalink_id_t over_linkid
= DATALINK_ALL_LINKID
;
9114 char over_link
[MAXLINKNAMELEN
];
9115 show_part_state_t state
;
9116 dladm_status_t status
;
9117 boolean_t o_arg
= B_FALSE
;
9118 char *fields_str
= NULL
;
9120 ofmt_status_t oferr
;
9121 uint_t ofmtflags
= 0;
9123 bzero(&state
, sizeof (state
));
9125 while ((option
= getopt_long(argc
, argv
, ":pPl:o:", show_part_lopts
,
9129 state
.ps_parsable
= B_TRUE
;
9132 flags
= DLADM_OPT_PERSIST
;
9136 * The data link ID of the IB Phys link. When this
9137 * argument is provided we list only the partition
9138 * objects created over this IB Phys link.
9140 if (strlcpy(over_link
, optarg
, MAXLINKNAMELEN
) >=
9142 die("link name too long");
9148 fields_str
= optarg
;
9151 die_opterr(optopt
, option
, use
);
9156 * Get the partition ID (optional last argument).
9158 if (optind
== (argc
- 1)) {
9159 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
9161 if (status
!= DLADM_STATUS_OK
) {
9162 die_dlerr(status
, "invalid partition link name '%s'",
9165 (void) strlcpy(state
.ps_part
, argv
[optind
], MAXLINKNAMELEN
);
9166 } else if (optind
!= argc
) {
9170 if (state
.ps_parsable
&& !o_arg
)
9171 die("-p requires -o");
9174 * If an IB Phys link name was provided as an argument, then get its
9178 status
= dladm_name2info(handle
, over_link
, &over_linkid
, NULL
,
9180 if (status
!= DLADM_STATUS_OK
) {
9181 die_dlerr(status
, "invalid link name '%s'", over_link
);
9185 state
.ps_over_id
= over_linkid
; /* IB Phys link ID */
9186 state
.ps_found
= B_FALSE
;
9187 state
.ps_flags
= flags
;
9189 if (state
.ps_parsable
)
9190 ofmtflags
|= OFMT_PARSABLE
;
9191 oferr
= ofmt_open(fields_str
, part_fields
, ofmtflags
, 0, &ofmt
);
9192 ofmt_check(oferr
, state
.ps_parsable
, ofmt
, die
, warn
);
9193 state
.ps_ofmt
= ofmt
;
9196 * If a specific IB partition name was not provided as an argument,
9197 * walk all the datalinks and display the information for all
9198 * IB partitions. If IB Phys link was provided limit it to only
9199 * IB partitions created over that IB Phys link.
9201 if (linkid
== DATALINK_ALL_LINKID
) {
9202 (void) dladm_walk_datalink_id(show_part
, handle
, &state
,
9203 DATALINK_CLASS_PART
, DATALINK_ANY_MEDIATYPE
, flags
);
9205 (void) show_part(handle
, linkid
, &state
);
9206 if (state
.ps_status
!= DLADM_STATUS_OK
) {
9208 die_dlerr(state
.ps_status
, "failed to show IB partition"
9209 " '%s'", state
.ps_part
);
9217 * Called from the walker dladm_walk_datalink_id() for each IB Phys link to
9218 * display IB specific information for these Phys links.
9220 static dladm_status_t
9221 print_ib(show_ib_state_t
*state
, datalink_id_t phys_linkid
)
9223 dladm_ib_attr_t attr
;
9224 dladm_status_t status
;
9225 char linkname
[MAXLINKNAMELEN
];
9226 char pkeystr
[MAXPKEYLEN
];
9228 ib_fields_buf_t ibuf
;
9230 bzero(&attr
, sizeof (attr
));
9233 * Get the attributes of the IB Phys link from active/Persistent config
9234 * based on the flag passed.
9236 if ((status
= dladm_ib_info(handle
, phys_linkid
, &attr
,
9237 state
->is_flags
)) != DLADM_STATUS_OK
)
9240 if ((state
->is_link_id
!= DATALINK_ALL_LINKID
) && (state
->is_link_id
9241 != attr
.dia_physlinkid
)) {
9242 dladm_free_ib_info(&attr
);
9243 return (DLADM_STATUS_OK
);
9247 * Get the data link name for the phys_linkid. If we are doing show-ib
9248 * for all IB Phys links, we have only the datalink IDs not the
9251 if (dladm_datalink_id2info(handle
, phys_linkid
, NULL
, NULL
, NULL
,
9252 linkname
, MAXLINKNAMELEN
) != DLADM_STATUS_OK
)
9255 (void) snprintf(ibuf
.ib_link
, sizeof (ibuf
.ib_link
),
9258 (void) snprintf(ibuf
.ib_portnum
, sizeof (ibuf
.ib_portnum
),
9259 "%d", attr
.dia_portnum
);
9261 (void) snprintf(ibuf
.ib_hcaguid
, sizeof (ibuf
.ib_hcaguid
),
9262 "%llX", attr
.dia_hca_guid
);
9264 (void) snprintf(ibuf
.ib_portguid
, sizeof (ibuf
.ib_portguid
),
9265 "%llX", attr
.dia_port_guid
);
9267 (void) get_linkstate(linkname
, B_TRUE
, ibuf
.ib_state
);
9270 * Create a comma separated list of pkeys from the pkey table returned
9271 * by the IP over IB driver instance.
9273 bzero(ibuf
.ib_pkeys
, attr
.dia_port_pkey_tbl_sz
* sizeof (ib_pkey_t
));
9274 for (i
= 0; i
< attr
.dia_port_pkey_tbl_sz
; i
++) {
9275 if (attr
.dia_port_pkeys
[i
] != IB_PKEY_INVALID_FULL
&&
9276 attr
.dia_port_pkeys
[i
] != IB_PKEY_INVALID_LIMITED
) {
9278 (void) snprintf(pkeystr
, MAXPKEYLEN
, "%X",
9279 attr
.dia_port_pkeys
[i
]);
9281 (void) snprintf(pkeystr
, MAXPKEYLEN
, ",%X",
9282 attr
.dia_port_pkeys
[i
]);
9283 (void) strlcat(ibuf
.ib_pkeys
, pkeystr
, MAXPKEYSTRSZ
);
9287 dladm_free_ib_info(&attr
);
9289 ofmt_print(state
->is_ofmt
, &ibuf
);
9291 return (DLADM_STATUS_OK
);
9296 show_ib(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
9298 ((show_ib_state_t
*)arg
)->is_status
= print_ib(arg
, linkid
);
9299 return (DLADM_WALK_CONTINUE
);
9303 * Show the properties of one/all IB Phys links. This is different from
9304 * show-phys command since this will display IB specific information about the
9305 * Phys link like, HCA GUID, PORT GUID, PKEYS active for this port etc.
9308 do_show_ib(int argc
, char *argv
[], const char *use
)
9311 uint32_t flags
= DLADM_OPT_ACTIVE
;
9312 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
9313 show_ib_state_t state
;
9314 dladm_status_t status
;
9315 boolean_t o_arg
= B_FALSE
;
9316 char *fields_str
= NULL
;
9318 ofmt_status_t oferr
;
9319 uint_t ofmtflags
= 0;
9321 bzero(&state
, sizeof (state
));
9323 while ((option
= getopt_long(argc
, argv
, ":po:", show_lopts
,
9327 state
.is_parsable
= B_TRUE
;
9331 fields_str
= optarg
;
9334 die_opterr(optopt
, option
, use
);
9338 /* get IB Phys link ID (optional last argument) */
9339 if (optind
== (argc
- 1)) {
9340 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
9342 if (status
!= DLADM_STATUS_OK
) {
9343 die_dlerr(status
, "invalid IB port name '%s'",
9346 (void) strlcpy(state
.is_link
, argv
[optind
], MAXLINKNAMELEN
);
9347 } else if (optind
!= argc
) {
9351 if (state
.is_parsable
&& !o_arg
)
9352 die("-p requires -o");
9355 * linkid is the data link ID of the IB Phys link. By default it will
9356 * be DATALINK_ALL_LINKID.
9358 state
.is_link_id
= linkid
;
9359 state
.is_flags
= flags
;
9361 if (state
.is_parsable
)
9362 ofmtflags
|= OFMT_PARSABLE
;
9363 oferr
= ofmt_open(fields_str
, ib_fields
, ofmtflags
, 0, &ofmt
);
9364 ofmt_check(oferr
, state
.is_parsable
, ofmt
, die
, warn
);
9365 state
.is_ofmt
= ofmt
;
9368 * If we are going to display the information for all IB Phys links
9369 * then we'll walk through all the datalinks for datalinks of Phys
9370 * class and media type IB.
9372 if (linkid
== DATALINK_ALL_LINKID
) {
9373 (void) dladm_walk_datalink_id(show_ib
, handle
, &state
,
9374 DATALINK_CLASS_PHYS
, DL_IB
, flags
);
9377 * We need to display the information only for the IB phys link
9378 * linkid. Call show_ib for this link.
9380 (void) show_ib(handle
, linkid
, &state
);
9381 if (state
.is_status
!= DLADM_STATUS_OK
) {
9383 die_dlerr(state
.is_status
, "failed to show IB Phys link"
9384 " '%s'", state
.is_link
);
9391 * Create an IP over Infiniband partition object over an IB Phys link. The IB
9392 * Phys link is associated with an Infiniband HCA port. The IB partition object
9393 * is created over a port, pkey combination. This partition object represents
9394 * an instance of IP over IB interface.
9398 do_create_part(int argc
, char *argv
[], const char *use
)
9401 int flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
9404 char *altroot
= NULL
;
9405 datalink_id_t physlinkid
= 0;
9406 datalink_id_t partlinkid
= 0;
9407 unsigned long opt_pkey
;
9410 char propstr
[DLADM_STRSIZE
];
9411 dladm_arg_list_t
*proplist
= NULL
;
9414 while ((option
= getopt_long(argc
, argv
, ":tfl:P:R:p:",
9415 part_lopts
, NULL
)) != -1) {
9419 * Create a temporary IB partition object. This
9420 * instance is not entered into the persistent database
9421 * so it will not be recreated automatically on a
9424 flags
&= ~DLADM_OPT_PERSIST
;
9428 * The IB phys link over which the partition object will
9437 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
9438 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
9440 die("property list too long '%s'", propstr
);
9444 * The P_Key for the port, pkey tuple of the partition
9445 * object. This P_Key should exist in the IB subnet.
9446 * The partition creation for a non-existent P_Key will
9447 * fail unless the -f option is used.
9449 * The P_Key is expected to be a hexadecimal number.
9451 opt_pkey
= strtoul(optarg
, &endp
, 16);
9452 if (errno
== ERANGE
|| opt_pkey
> USHRT_MAX
||
9454 die("Invalid pkey");
9456 pkey
= (ib_pkey_t
)opt_pkey
;
9459 flags
|= DLADM_OPT_FORCE
;
9462 die_opterr(optopt
, option
, use
);
9467 /* check required options */
9471 /* the partition name is a required operand */
9472 if (optind
!= (argc
- 1))
9475 pname
= argv
[argc
- 1];
9478 * Verify that the partition object's name is in the valid link name
9481 if (!dladm_valid_linkname(pname
))
9482 die("Invalid link name '%s'", pname
);
9484 /* pkey is a mandatory argument */
9488 if (altroot
!= NULL
)
9489 altroot_cmd(altroot
, argc
, argv
);
9492 * Get the data link id of the IB Phys link over which we will be
9493 * creating partition object.
9495 if (dladm_name2info(handle
, l_arg
,
9496 &physlinkid
, NULL
, NULL
, NULL
) != DLADM_STATUS_OK
)
9497 die("invalid link name '%s'", l_arg
);
9500 * parse the property list provided with -p option.
9502 if (dladm_parse_link_props(propstr
, &proplist
, B_FALSE
)
9504 die("invalid IB partition property");
9507 * Call the library routine to create the partition object.
9509 status
= dladm_part_create(handle
, physlinkid
, pkey
, flags
, pname
,
9510 &partlinkid
, proplist
);
9511 if (status
!= DLADM_STATUS_OK
)
9513 "partition %x creation over %s failed", pkey
, l_arg
);
9517 * Delete an IP over Infiniband partition object. The partition object should
9518 * be unplumbed before attempting the delete.
9521 do_delete_part(int argc
, char *argv
[], const char *use
)
9523 int option
, flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
9525 char *altroot
= NULL
;
9526 datalink_id_t partid
;
9529 while ((option
= getopt_long(argc
, argv
, "R:t", part_lopts
,
9533 flags
&= ~DLADM_OPT_PERSIST
;
9539 die_opterr(optopt
, option
, use
);
9543 /* get partition name (required last argument) */
9544 if (optind
!= (argc
- 1))
9547 if (altroot
!= NULL
)
9548 altroot_cmd(altroot
, argc
, argv
);
9551 * Get the data link id of the partition object given the partition
9554 status
= dladm_name2info(handle
, argv
[optind
], &partid
, NULL
, NULL
,
9556 if (status
!= DLADM_STATUS_OK
)
9557 die("invalid link name '%s'", argv
[optind
]);
9560 * Call the library routine to delete the IB partition. This will
9561 * result in the IB partition object and all its resources getting
9564 status
= dladm_part_delete(handle
, partid
, flags
);
9565 if (status
!= DLADM_STATUS_OK
)
9566 die_dlerr(status
, "%s: partition deletion failed",
9571 * Bring up all or one IB partition already present in the persistent database
9572 * but not active yet.
9574 * This sub-command is used during the system boot up to bring up all IB
9575 * partitions present in the persistent database. This is similar to a
9576 * create partition except that, the partitions are always created even if the
9577 * HCA port is down or P_Key is not present in the IB subnet. This is similar
9578 * to using the 'force' option while creating the partition except that the 'f'
9579 * flag will be set in the flags field only if the create-part for this command
9580 * was called with '-f' option.
9584 do_up_part(int argc
, char *argv
[], const char *use
)
9586 datalink_id_t partid
= DATALINK_ALL_LINKID
;
9587 dladm_status_t status
;
9590 * If a partition name was passed as an argument, get its data link
9591 * id. By default we'll attempt to bring up all IB partition data
9595 status
= dladm_name2info(handle
, argv
[argc
- 1], &partid
, NULL
,
9597 if (status
!= DLADM_STATUS_OK
)
9599 } else if (argc
> 2) {
9603 (void) dladm_part_up(handle
, partid
, 0);