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>
67 #include <bsm/adt_event.h>
68 #include <libdlvnic.h>
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <sys/ib/ib_types.h>
72 #include <sys/processor.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 #include <net/if_types.h>
82 #define BUFLEN(lim, ptr) (((lim) > (ptr)) ? ((lim) - (ptr)) : 0)
83 #define MAXLINELEN 1024
84 #define SMF_UPGRADE_FILE "/var/svc/profile/upgrade"
85 #define SMF_UPGRADEDATALINK_FILE "/var/svc/profile/upgrade_datalink"
86 #define SMF_DLADM_UPGRADE_MSG " # added by dladm(1M)"
87 #define DLADM_DEFAULT_COL 80
90 * used by the wifi show-* commands to set up ofmt_field_t structures.
92 #define WIFI_CMD_SCAN 0x00000001
93 #define WIFI_CMD_SHOW 0x00000002
94 #define WIFI_CMD_ALL (WIFI_CMD_SCAN | WIFI_CMD_SHOW)
96 /* No larger than pktsum_t */
97 typedef struct brsum_s
{
101 uint64_t forward_unk
;
106 /* No larger than pktsum_t */
107 typedef struct brlsum_s
{
117 typedef struct show_state
{
118 boolean_t ls_firstonly
;
119 boolean_t ls_donefirst
;
120 pktsum_t ls_prevstats
;
122 dladm_status_t ls_status
;
123 ofmt_handle_t ls_ofmt
;
124 boolean_t ls_parsable
;
129 typedef struct show_grp_state
{
130 pktsum_t gs_prevstats
[MAXPORT
];
132 dladm_status_t gs_status
;
133 boolean_t gs_parsable
;
135 boolean_t gs_extended
;
137 boolean_t gs_firstonly
;
138 boolean_t gs_donefirst
;
139 ofmt_handle_t gs_ofmt
;
142 typedef struct show_vnic_state
{
143 datalink_id_t vs_vnic_id
;
144 datalink_id_t vs_link_id
;
145 char vs_vnic
[MAXLINKNAMELEN
];
146 char vs_link
[MAXLINKNAMELEN
];
147 boolean_t vs_parsable
;
149 boolean_t vs_firstonly
;
150 boolean_t vs_donefirst
;
152 boolean_t vs_printstats
;
153 pktsum_t vs_totalstats
;
154 pktsum_t vs_prevstats
[MAXVNIC
];
155 boolean_t vs_etherstub
;
156 dladm_status_t vs_status
;
158 ofmt_handle_t vs_ofmt
;
161 typedef struct show_part_state
{
162 datalink_id_t ps_over_id
;
163 char ps_part
[MAXLINKNAMELEN
];
164 boolean_t ps_parsable
;
166 dladm_status_t ps_status
;
168 ofmt_handle_t ps_ofmt
;
171 typedef struct show_ib_state
{
172 datalink_id_t is_link_id
;
173 char is_link
[MAXLINKNAMELEN
];
174 boolean_t is_parsable
;
175 dladm_status_t is_status
;
177 ofmt_handle_t is_ofmt
;
180 typedef struct show_usage_state_s
{
182 boolean_t us_parsable
;
183 boolean_t us_printheader
;
185 boolean_t us_showall
;
186 ofmt_handle_t us_ofmt
;
187 } show_usage_state_t
;
190 * callback functions for printing output and error diagnostics.
192 static ofmt_cb_t print_default_cb
, print_link_stats_cb
, print_linkprop_cb
;
193 static ofmt_cb_t print_lacp_cb
, print_phys_one_mac_cb
;
194 static ofmt_cb_t print_xaggr_cb
, print_aggr_stats_cb
;
195 static ofmt_cb_t print_phys_one_hwgrp_cb
, print_wlan_attr_cb
;
196 static ofmt_cb_t print_wifi_status_cb
, print_link_attr_cb
;
197 static void dladm_ofmt_check(ofmt_status_t
, boolean_t
, ofmt_handle_t
);
199 typedef void cmdfunc_t(int, char **, const char *);
201 static cmdfunc_t do_show_link
, do_show_wifi
, do_show_phys
;
202 static cmdfunc_t do_create_aggr
, do_delete_aggr
, do_add_aggr
, do_remove_aggr
;
203 static cmdfunc_t do_modify_aggr
, do_show_aggr
, do_up_aggr
;
204 static cmdfunc_t do_scan_wifi
, do_connect_wifi
, do_disconnect_wifi
;
205 static cmdfunc_t do_show_linkprop
, do_set_linkprop
, do_reset_linkprop
;
206 static cmdfunc_t do_create_secobj
, do_delete_secobj
, do_show_secobj
;
207 static cmdfunc_t do_init_linkprop
, do_init_secobj
;
208 static cmdfunc_t do_create_vlan
, do_delete_vlan
, do_up_vlan
, do_show_vlan
;
209 static cmdfunc_t do_rename_link
, do_delete_phys
, do_init_phys
;
210 static cmdfunc_t do_show_linkmap
;
211 static cmdfunc_t do_show_ether
;
212 static cmdfunc_t do_create_vnic
, do_delete_vnic
, do_show_vnic
;
213 static cmdfunc_t do_up_vnic
;
214 static cmdfunc_t do_create_part
, do_delete_part
, do_show_part
, do_show_ib
;
215 static cmdfunc_t do_up_part
;
216 static cmdfunc_t do_create_etherstub
, do_delete_etherstub
, do_show_etherstub
;
217 static cmdfunc_t do_create_simnet
, do_modify_simnet
;
218 static cmdfunc_t do_delete_simnet
, do_show_simnet
, do_up_simnet
;
219 static cmdfunc_t do_show_usage
;
220 static cmdfunc_t do_create_bridge
, do_modify_bridge
, do_delete_bridge
;
221 static cmdfunc_t do_add_bridge
, do_remove_bridge
, do_show_bridge
;
222 static cmdfunc_t do_create_iptun
, do_modify_iptun
, do_delete_iptun
;
223 static cmdfunc_t do_show_iptun
, do_up_iptun
, do_down_iptun
;
225 static void do_up_vnic_common(int, char **, const char *, boolean_t
);
227 static int show_part(dladm_handle_t
, datalink_id_t
, void *);
229 static void altroot_cmd(char *, int, char **);
230 static int show_linkprop_onelink(dladm_handle_t
, datalink_id_t
, void *);
232 static void link_stats(datalink_id_t
, uint_t
, char *, show_state_t
*);
233 static void aggr_stats(datalink_id_t
, show_grp_state_t
*, uint_t
);
234 static void vnic_stats(show_vnic_state_t
*, uint32_t);
236 static int get_one_kstat(const char *, const char *, uint8_t,
238 static void get_mac_stats(const char *, pktsum_t
*);
239 static void get_link_stats(const char *, pktsum_t
*);
240 static uint64_t get_ifspeed(const char *, boolean_t
);
241 static const char *get_linkstate(const char *, boolean_t
, char *);
242 static const char *get_linkduplex(const char *, boolean_t
, char *);
244 static iptun_type_t
iptun_gettypebyname(char *);
245 static const char *iptun_gettypebyvalue(iptun_type_t
);
246 static dladm_status_t
print_iptun(dladm_handle_t
, datalink_id_t
,
248 static int print_iptun_walker(dladm_handle_t
, datalink_id_t
, void *);
250 static int show_etherprop(dladm_handle_t
, datalink_id_t
, void *);
251 static void show_ether_xprop(void *, dladm_ether_info_t
*);
252 static boolean_t
link_is_ether(const char *, datalink_id_t
*);
254 static boolean_t
str2int(const char *, int *);
255 static void die(const char *, ...);
256 static void die_optdup(int);
257 static void die_opterr(int, int, const char *);
258 static void die_dlerr(dladm_status_t
, const char *, ...);
259 static void warn(const char *, ...);
260 static void warn_dlerr(dladm_status_t
, const char *, ...);
268 static cmd_t cmds
[] = {
269 { "rename-link", do_rename_link
,
270 " rename-link <oldlink> <newlink>" },
271 { "show-link", do_show_link
,
272 " show-link [-pP] [-o <field>,..] [-s [-i <interval>]] "
274 { "create-aggr", do_create_aggr
,
275 " create-aggr [-t] [-P <policy>] [-L <mode>] [-T <time>] "
277 "\t\t -l <link> [-l <link>...] <link>" },
278 { "delete-aggr", do_delete_aggr
,
279 " delete-aggr [-t] <link>" },
280 { "add-aggr", do_add_aggr
,
281 " add-aggr [-t] -l <link> [-l <link>...] <link>" },
282 { "remove-aggr", do_remove_aggr
,
283 " remove-aggr [-t] -l <link> [-l <link>...] <link>" },
284 { "modify-aggr", do_modify_aggr
,
285 " modify-aggr [-t] [-P <policy>] [-L <mode>] [-T <time>] "
288 { "show-aggr", do_show_aggr
,
289 " show-aggr [-pPLx] [-o <field>,..] [-s [-i <interval>]] "
291 { "up-aggr", do_up_aggr
, NULL
},
292 { "scan-wifi", do_scan_wifi
,
293 " scan-wifi [-p] [-o <field>,...] [<link>]" },
294 { "connect-wifi", do_connect_wifi
,
295 " connect-wifi [-e <essid>] [-i <bssid>] [-k <key>,...] "
297 "\t\t [-a open|shared] [-b bss|ibss] [-c] [-m a|b|g] "
300 { "disconnect-wifi", do_disconnect_wifi
,
301 " disconnect-wifi [-a] [<link>]" },
302 { "show-wifi", do_show_wifi
,
303 " show-wifi [-p] [-o <field>,...] [<link>]\n" },
304 { "set-linkprop", do_set_linkprop
,
305 " set-linkprop [-t] -p <prop>=<value>[,...] <name>" },
306 { "reset-linkprop", do_reset_linkprop
,
307 " reset-linkprop [-t] [-p <prop>,...] <name>" },
308 { "show-linkprop", do_show_linkprop
,
309 " show-linkprop [-cP] [-o <field>,...] [-p <prop>,...] "
311 { "show-ether", do_show_ether
,
312 " show-ether [-px][-o <field>,...] <link>\n" },
313 { "create-secobj", do_create_secobj
,
314 " create-secobj [-t] [-f <file>] -c <class> <secobj>" },
315 { "delete-secobj", do_delete_secobj
,
316 " delete-secobj [-t] <secobj>[,...]" },
317 { "show-secobj", do_show_secobj
,
318 " show-secobj [-pP] [-o <field>,...] [<secobj>,...]\n" },
319 { "init-linkprop", do_init_linkprop
, NULL
},
320 { "init-secobj", do_init_secobj
, NULL
},
321 { "create-vlan", do_create_vlan
,
322 " create-vlan [-ft] -l <link> -v <vid> [link]" },
323 { "delete-vlan", do_delete_vlan
,
324 " delete-vlan [-t] <link>" },
325 { "show-vlan", do_show_vlan
,
326 " show-vlan [-pP] [-o <field>,..] [<link>]\n" },
327 { "up-vlan", do_up_vlan
, NULL
},
328 { "create-iptun", do_create_iptun
,
329 " create-iptun [-t] -T <type> "
330 "[-a {local|remote}=<addr>,...] <link>]" },
331 { "delete-iptun", do_delete_iptun
,
332 " delete-iptun [-t] <link>" },
333 { "modify-iptun", do_modify_iptun
,
334 " modify-iptun [-t] -a {local|remote}=<addr>,... <link>" },
335 { "show-iptun", do_show_iptun
,
336 " show-iptun [-pP] [-o <field>,..] [<link>]\n" },
337 { "up-iptun", do_up_iptun
, NULL
},
338 { "down-iptun", do_down_iptun
, NULL
},
339 { "delete-phys", do_delete_phys
,
340 " delete-phys <link>" },
341 { "show-phys", do_show_phys
,
342 " show-phys [-m | -H | -P] [[-p] [-o <field>[,...]] "
344 { "init-phys", do_init_phys
, NULL
},
345 { "show-linkmap", do_show_linkmap
, NULL
},
346 { "create-vnic", do_create_vnic
,
347 " create-vnic [-t] -l <link> [-m <value> | auto |\n"
348 "\t\t {factory [-n <slot-id>]} | {random [-r <prefix>]} |\n"
349 "\t\t {vrrp -V <vrid> -A {inet | inet6}} [-v <vid> [-f]]\n"
350 "\t\t [-p <prop>=<value>[,...]] <vnic-link>" },
351 { "delete-vnic", do_delete_vnic
,
352 " delete-vnic [-t] <vnic-link>" },
353 { "show-vnic", do_show_vnic
,
354 " show-vnic [-pP] [-l <link>] [-s [-i <interval>]] "
356 { "up-vnic", do_up_vnic
, NULL
},
357 { "create-part", do_create_part
,
358 " create-part [-t] [-f] -l <link> [-P <pkey>]\n"
359 "\t\t [-R <root-dir>] <part-link>" },
360 { "delete-part", do_delete_part
,
361 " delete-part [-t] [-R <root-dir>] <part-link>"},
362 { "show-part", do_show_part
,
363 " show-part [-pP] [-o <field>,...][-l <linkover>]\n"
364 "\t\t [<part-link>]" },
365 { "show-ib", do_show_ib
,
366 " show-ib [-p] [-o <field>,...] [<link>]\n" },
367 { "up-part", do_up_part
, NULL
},
368 { "create-etherstub", do_create_etherstub
,
369 " create-etherstub [-t] <link>" },
370 { "delete-etherstub", do_delete_etherstub
,
371 " delete-etherstub [-t] <link>" },
372 { "show-etherstub", do_show_etherstub
,
373 " show-etherstub [-t] [<link>]\n" },
374 { "create-simnet", do_create_simnet
, NULL
},
375 { "modify-simnet", do_modify_simnet
, NULL
},
376 { "delete-simnet", do_delete_simnet
, NULL
},
377 { "show-simnet", do_show_simnet
, NULL
},
378 { "up-simnet", do_up_simnet
, NULL
},
379 { "create-bridge", do_create_bridge
,
380 " create-bridge [-R <root-dir>] [-P <protect>] "
382 "\t\t [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
383 "\t\t [-f <force-protocol>] [-l <link>]... <bridge>" },
384 { "modify-bridge", do_modify_bridge
,
385 " modify-bridge [-R <root-dir>] [-P <protect>] "
387 "\t\t [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
388 "\t\t [-f <force-protocol>] <bridge>" },
389 { "delete-bridge", do_delete_bridge
,
390 " delete-bridge [-R <root-dir>] <bridge>" },
391 { "add-bridge", do_add_bridge
,
392 " add-bridge [-R <root-dir>] -l <link> [-l <link>]... "
394 { "remove-bridge", do_remove_bridge
,
395 " remove-bridge [-R <root-dir>] -l <link> [-l <link>]... "
397 { "show-bridge", do_show_bridge
,
398 " show-bridge [-p] [-o <field>,...] [-s [-i <interval>]] "
400 " show-bridge -l [-p] [-o <field>,...] [-s [-i <interval>]]"
402 " show-bridge -f [-p] [-o <field>,...] [-s [-i <interval>]]"
404 " show-bridge -t [-p] [-o <field>,...] [-s [-i <interval>]]"
406 { "show-usage", do_show_usage
,
407 " show-usage [-a] [-d | -F <format>] "
408 "[-s <DD/MM/YYYY,HH:MM:SS>]\n"
409 "\t\t [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<link>]" }
412 static const struct option lopts
[] = {
413 {"vlan-id", required_argument
, 0, 'v'},
414 {"output", required_argument
, 0, 'o'},
415 {"dev", required_argument
, 0, 'd'},
416 {"policy", required_argument
, 0, 'P'},
417 {"lacp-mode", required_argument
, 0, 'L'},
418 {"lacp-timer", required_argument
, 0, 'T'},
419 {"unicast", required_argument
, 0, 'u'},
420 {"temporary", no_argument
, 0, 't'},
421 {"root-dir", required_argument
, 0, 'R'},
422 {"link", required_argument
, 0, 'l'},
423 {"forcible", no_argument
, 0, 'f'},
424 {"bw-limit", required_argument
, 0, 'b'},
425 {"mac-address", required_argument
, 0, 'm'},
426 {"slot", required_argument
, 0, 'n'},
430 static const struct option show_lopts
[] = {
431 {"statistics", no_argument
, 0, 's'},
432 {"continuous", no_argument
, 0, 'S'},
433 {"interval", required_argument
, 0, 'i'},
434 {"parsable", no_argument
, 0, 'p'},
435 {"parseable", no_argument
, 0, 'p'},
436 {"extended", no_argument
, 0, 'x'},
437 {"output", required_argument
, 0, 'o'},
438 {"persistent", no_argument
, 0, 'P'},
439 {"lacp", no_argument
, 0, 'L'},
443 static const struct option iptun_lopts
[] = {
444 {"output", required_argument
, 0, 'o'},
445 {"tunnel-type", required_argument
, 0, 'T'},
446 {"address", required_argument
, 0, 'a'},
447 {"root-dir", required_argument
, 0, 'R'},
448 {"parsable", no_argument
, 0, 'p'},
449 {"parseable", no_argument
, 0, 'p'},
450 {"persistent", no_argument
, 0, 'P'},
454 static char * const iptun_addropts
[] = {
455 #define IPTUN_LOCAL 0
457 #define IPTUN_REMOTE 1
461 static const struct {
462 const char *type_name
;
463 iptun_type_t type_value
;
465 {"ipv4", IPTUN_TYPE_IPV4
},
466 {"ipv6", IPTUN_TYPE_IPV6
},
467 {"6to4", IPTUN_TYPE_6TO4
},
471 static const struct option prop_longopts
[] = {
472 {"temporary", no_argument
, 0, 't' },
473 {"output", required_argument
, 0, 'o' },
474 {"root-dir", required_argument
, 0, 'R' },
475 {"prop", required_argument
, 0, 'p' },
476 {"parsable", no_argument
, 0, 'c' },
477 {"parseable", no_argument
, 0, 'c' },
478 {"persistent", no_argument
, 0, 'P' },
482 static const struct option wifi_longopts
[] = {
483 {"parsable", no_argument
, 0, 'p' },
484 {"parseable", no_argument
, 0, 'p' },
485 {"output", required_argument
, 0, 'o' },
486 {"essid", required_argument
, 0, 'e' },
487 {"bsstype", required_argument
, 0, 'b' },
488 {"mode", required_argument
, 0, 'm' },
489 {"key", required_argument
, 0, 'k' },
490 {"sec", required_argument
, 0, 's' },
491 {"auth", required_argument
, 0, 'a' },
492 {"create-ibss", required_argument
, 0, 'c' },
493 {"timeout", required_argument
, 0, 'T' },
494 {"all-links", no_argument
, 0, 'a' },
495 {"temporary", no_argument
, 0, 't' },
496 {"root-dir", required_argument
, 0, 'R' },
497 {"persistent", no_argument
, 0, 'P' },
498 {"file", required_argument
, 0, 'f' },
502 static const struct option showeth_lopts
[] = {
503 {"parsable", no_argument
, 0, 'p' },
504 {"parseable", no_argument
, 0, 'p' },
505 {"extended", no_argument
, 0, 'x' },
506 {"output", required_argument
, 0, 'o' },
510 static const struct option vnic_lopts
[] = {
511 {"temporary", no_argument
, 0, 't' },
512 {"root-dir", required_argument
, 0, 'R' },
513 {"dev", required_argument
, 0, 'd' },
514 {"mac-address", required_argument
, 0, 'm' },
515 {"cpus", required_argument
, 0, 'c' },
516 {"bw-limit", required_argument
, 0, 'b' },
517 {"slot", required_argument
, 0, 'n' },
518 {"mac-prefix", required_argument
, 0, 'r' },
519 {"vrid", required_argument
, 0, 'V' },
520 {"address-family", required_argument
, 0, 'A' },
524 static const struct option part_lopts
[] = {
525 {"temporary", no_argument
, 0, 't' },
526 {"pkey", required_argument
, 0, 'P' },
527 {"link", required_argument
, 0, 'l' },
528 {"force", no_argument
, 0, 'f' },
529 {"root-dir", required_argument
, 0, 'R' },
530 {"prop", required_argument
, 0, 'p' },
534 static const struct option show_part_lopts
[] = {
535 {"parsable", no_argument
, 0, 'p' },
536 {"parseable", no_argument
, 0, 'p' },
537 {"link", required_argument
, 0, 'l' },
538 {"persistent", no_argument
, 0, 'P' },
539 {"output", required_argument
, 0, 'o' },
543 static const struct option etherstub_lopts
[] = {
544 {"temporary", no_argument
, 0, 't' },
545 {"root-dir", required_argument
, 0, 'R' },
549 static const struct option usage_opts
[] = {
550 {"file", required_argument
, 0, 'f' },
551 {"format", required_argument
, 0, 'F' },
552 {"start", required_argument
, 0, 's' },
553 {"stop", required_argument
, 0, 'e' },
557 static const struct option simnet_lopts
[] = {
558 {"temporary", no_argument
, 0, 't' },
559 {"root-dir", required_argument
, 0, 'R' },
560 {"media", required_argument
, 0, 'm' },
561 {"peer", required_argument
, 0, 'p' },
565 static const struct option bridge_lopts
[] = {
566 { "protect", required_argument
, 0, 'P' },
567 { "root-dir", required_argument
, 0, 'R' },
568 { "forward-delay", required_argument
, 0, 'd' },
569 { "force-protocol", required_argument
, 0, 'f' },
570 { "hello-time", required_argument
, 0, 'h' },
571 { "link", required_argument
, 0, 'l' },
572 { "max-age", required_argument
, 0, 'm' },
573 { "priority", required_argument
, 0, 'p' },
577 static const struct option bridge_show_lopts
[] = {
578 { "forwarding", no_argument
, 0, 'f' },
579 { "interval", required_argument
, 0, 'i' },
580 { "link", no_argument
, 0, 'l' },
581 { "output", required_argument
, 0, 'o' },
582 { "parsable", no_argument
, 0, 'p' },
583 { "parseable", no_argument
, 0, 'p' },
584 { "statistics", no_argument
, 0, 's' },
585 { "trill", no_argument
, 0, 't' },
590 * structures for 'dladm show-ether'
592 static const char *ptype
[] = {LEI_ATTR_NAMES
};
594 typedef struct ether_fields_buf_s
602 char eth_rem_fault
[16];
603 } ether_fields_buf_t
;
605 static const ofmt_field_t ether_fields
[] = {
606 /* name, field width, offset callback */
608 offsetof(ether_fields_buf_t
, eth_link
), print_default_cb
},
610 offsetof(ether_fields_buf_t
, eth_ptype
), print_default_cb
},
612 offsetof(ether_fields_buf_t
, eth_state
),
615 offsetof(ether_fields_buf_t
, eth_autoneg
), print_default_cb
},
616 { "SPEED-DUPLEX", 32,
617 offsetof(ether_fields_buf_t
, eth_spdx
), print_default_cb
},
619 offsetof(ether_fields_buf_t
, eth_pause
), print_default_cb
},
621 offsetof(ether_fields_buf_t
, eth_rem_fault
), print_default_cb
},
626 typedef struct print_ether_state
{
628 boolean_t es_parsable
;
630 boolean_t es_extended
;
631 ofmt_handle_t es_ofmt
;
632 } print_ether_state_t
;
635 * structures for 'dladm show-link -s' (print statistics)
645 } link_s_field_index_t
;
647 static const ofmt_field_t link_s_fields
[] = {
648 /* name, field width, index, callback */
649 { "LINK", 15, LINK_S_LINK
, print_link_stats_cb
},
650 { "IPACKETS", 10, LINK_S_IPKTS
, print_link_stats_cb
},
651 { "RBYTES", 8, LINK_S_RBYTES
, print_link_stats_cb
},
652 { "IERRORS", 10, LINK_S_IERRORS
, print_link_stats_cb
},
653 { "OPACKETS", 12, LINK_S_OPKTS
, print_link_stats_cb
},
654 { "OBYTES", 12, LINK_S_OBYTES
, print_link_stats_cb
},
655 { "OERRORS", 8, LINK_S_OERRORS
, print_link_stats_cb
}}
658 typedef struct link_args_s
{
660 pktsum_t
*link_s_psum
;
664 * buffer used by print functions for show-{link,phys,vlan} commands.
666 typedef struct link_fields_buf_s
{
667 char link_name
[MAXLINKNAMELEN
];
668 char link_class
[DLADM_STRSIZE
];
670 char link_state
[DLADM_STRSIZE
];
671 char link_bridge
[MAXLINKNAMELEN
* MAXPORT
];
672 char link_over
[MAXLINKNAMELEN
* MAXPORT
];
673 char link_phys_state
[DLADM_STRSIZE
];
674 char link_phys_media
[DLADM_STRSIZE
];
675 char link_phys_speed
[DLADM_STRSIZE
];
676 char link_phys_duplex
[DLPI_LINKNAME_MAX
];
677 char link_phys_device
[DLPI_LINKNAME_MAX
];
679 char link_vlan_vid
[6];
683 * structures for 'dladm show-link'
685 static const ofmt_field_t link_fields
[] = {
686 /* name, field width, index, callback */
688 offsetof(link_fields_buf_t
, link_name
), print_default_cb
},
690 offsetof(link_fields_buf_t
, link_class
), print_default_cb
},
692 offsetof(link_fields_buf_t
, link_mtu
), print_default_cb
},
694 offsetof(link_fields_buf_t
, link_state
), print_default_cb
},
696 offsetof(link_fields_buf_t
, link_bridge
), print_default_cb
},
698 offsetof(link_fields_buf_t
, link_over
), print_default_cb
},
703 * structures for 'dladm show-aggr'
705 typedef struct laggr_fields_buf_s
{
706 char laggr_name
[DLPI_LINKNAME_MAX
];
707 char laggr_policy
[9];
708 char laggr_addrpolicy
[ETHERADDRL
* 3 + 3];
709 char laggr_lacpactivity
[14];
710 char laggr_lacptimer
[DLADM_STRSIZE
];
712 } laggr_fields_buf_t
;
714 typedef struct laggr_args_s
{
715 int laggr_lport
; /* -1 indicates the aggr itself */
716 const char *laggr_link
;
717 dladm_aggr_grp_attr_t
*laggr_ginfop
;
718 dladm_status_t
*laggr_status
;
719 pktsum_t
*laggr_pktsumtot
; /* -s only */
720 pktsum_t
*laggr_diffstats
; /* -s only */
721 boolean_t laggr_parsable
;
724 static const ofmt_field_t laggr_fields
[] = {
725 /* name, field width, offset, callback */
727 offsetof(laggr_fields_buf_t
, laggr_name
), print_default_cb
},
729 offsetof(laggr_fields_buf_t
, laggr_policy
), print_default_cb
},
730 { "ADDRPOLICY", ETHERADDRL
* 3 + 3,
731 offsetof(laggr_fields_buf_t
, laggr_addrpolicy
), print_default_cb
},
732 { "LACPACTIVITY", 14,
733 offsetof(laggr_fields_buf_t
, laggr_lacpactivity
), print_default_cb
},
735 offsetof(laggr_fields_buf_t
, laggr_lacptimer
), print_default_cb
},
737 offsetof(laggr_fields_buf_t
, laggr_flags
), print_default_cb
},
742 * structures for 'dladm show-aggr -x'.
752 } aggr_x_field_index_t
;
754 static const ofmt_field_t aggr_x_fields
[] = {
755 /* name, field width, index callback */
756 { "LINK", 12, AGGR_X_LINK
, print_xaggr_cb
},
757 { "PORT", 15, AGGR_X_PORT
, print_xaggr_cb
},
758 { "SPEED", 5, AGGR_X_SPEED
, print_xaggr_cb
},
759 { "DUPLEX", 10, AGGR_X_DUPLEX
, print_xaggr_cb
},
760 { "STATE", 10, AGGR_X_STATE
, print_xaggr_cb
},
761 { "ADDRESS", 19, AGGR_X_ADDRESS
, print_xaggr_cb
},
762 { "PORTSTATE", 16, AGGR_X_PORTSTATE
, print_xaggr_cb
},
767 * structures for 'dladm show-aggr -s'.
778 } aggr_s_field_index_t
;
780 static const ofmt_field_t aggr_s_fields
[] = {
781 { "LINK", 12, AGGR_S_LINK
, print_aggr_stats_cb
},
782 { "PORT", 10, AGGR_S_PORT
, print_aggr_stats_cb
},
783 { "IPACKETS", 8, AGGR_S_IPKTS
, print_aggr_stats_cb
},
784 { "RBYTES", 8, AGGR_S_RBYTES
, print_aggr_stats_cb
},
785 { "OPACKETS", 8, AGGR_S_OPKTS
, print_aggr_stats_cb
},
786 { "OBYTES", 8, AGGR_S_OBYTES
, print_aggr_stats_cb
},
787 { "IPKTDIST", 9, AGGR_S_IPKTDIST
, print_aggr_stats_cb
},
788 { "OPKTDIST", 15, AGGR_S_OPKTDIST
, print_aggr_stats_cb
},
793 * structures for 'dladm show-aggr -L'.
804 } aggr_l_field_index_t
;
806 static const ofmt_field_t aggr_l_fields
[] = {
807 /* name, field width, index */
808 { "LINK", 12, AGGR_L_LINK
, print_lacp_cb
},
809 { "PORT", 13, AGGR_L_PORT
, print_lacp_cb
},
810 { "AGGREGATABLE", 13, AGGR_L_AGGREGATABLE
, print_lacp_cb
},
811 { "SYNC", 5, AGGR_L_SYNC
, print_lacp_cb
},
812 { "COLL", 5, AGGR_L_COLL
, print_lacp_cb
},
813 { "DIST", 5, AGGR_L_DIST
, print_lacp_cb
},
814 { "DEFAULTED", 10, AGGR_L_DEFAULTED
, print_lacp_cb
},
815 { "EXPIRED", 15, AGGR_L_EXPIRED
, print_lacp_cb
},
820 * structures for 'dladm show-phys'
823 static const ofmt_field_t phys_fields
[] = {
824 /* name, field width, offset */
826 offsetof(link_fields_buf_t
, link_name
), print_default_cb
},
828 offsetof(link_fields_buf_t
, link_phys_media
), print_default_cb
},
830 offsetof(link_fields_buf_t
, link_phys_state
), print_default_cb
},
832 offsetof(link_fields_buf_t
, link_phys_speed
), print_default_cb
},
834 offsetof(link_fields_buf_t
, link_phys_duplex
), print_default_cb
},
836 offsetof(link_fields_buf_t
, link_phys_device
), print_default_cb
},
838 offsetof(link_fields_buf_t
, link_flags
), print_default_cb
},
843 * structures for 'dladm show-phys -m'
852 } phys_m_field_index_t
;
854 static const ofmt_field_t phys_m_fields
[] = {
855 /* name, field width, offset */
856 { "LINK", 13, PHYS_M_LINK
, print_phys_one_mac_cb
},
857 { "SLOT", 9, PHYS_M_SLOT
, print_phys_one_mac_cb
},
858 { "ADDRESS", 19, PHYS_M_ADDRESS
, print_phys_one_mac_cb
},
859 { "INUSE", 5, PHYS_M_INUSE
, print_phys_one_mac_cb
},
860 { "CLIENT", 13, PHYS_M_CLIENT
, print_phys_one_mac_cb
},
865 * structures for 'dladm show-phys -H'
873 } phys_h_field_index_t
;
875 #define RINGSTRLEN 21
877 static const ofmt_field_t phys_h_fields
[] = {
878 { "LINK", 13, PHYS_H_LINK
, print_phys_one_hwgrp_cb
},
879 { "RINGTYPE", 9, PHYS_H_RINGTYPE
, print_phys_one_hwgrp_cb
},
880 { "RINGS", RINGSTRLEN
, PHYS_H_RINGS
, print_phys_one_hwgrp_cb
},
881 { "CLIENTS", 24, PHYS_H_CLIENTS
, print_phys_one_hwgrp_cb
},
886 * structures for 'dladm show-vlan'
888 static const ofmt_field_t vlan_fields
[] = {
890 offsetof(link_fields_buf_t
, link_name
), print_default_cb
},
892 offsetof(link_fields_buf_t
, link_vlan_vid
), print_default_cb
},
894 offsetof(link_fields_buf_t
, link_over
), print_default_cb
},
896 offsetof(link_fields_buf_t
, link_flags
), print_default_cb
},
901 * structures common to 'dladm scan-wifi' and 'dladm show-wifi'
902 * callback will be determined in parse_wifi_fields.
904 static ofmt_field_t wifi_common_fields
[] = {
905 { "LINK", 11, 0, NULL
},
906 { "ESSID", 20, DLADM_WLAN_ATTR_ESSID
, NULL
},
907 { "BSSID", 18, DLADM_WLAN_ATTR_BSSID
, NULL
},
908 { "IBSSID", 18, DLADM_WLAN_ATTR_BSSID
, NULL
},
909 { "MODE", 7, DLADM_WLAN_ATTR_MODE
, NULL
},
910 { "SPEED", 7, DLADM_WLAN_ATTR_SPEED
, NULL
},
911 { "BSSTYPE", 9, DLADM_WLAN_ATTR_BSSTYPE
, NULL
},
912 { "SEC", 7, DLADM_WLAN_ATTR_SECMODE
, NULL
},
913 { "STRENGTH", 11, DLADM_WLAN_ATTR_STRENGTH
, NULL
},
914 { NULL
, 0, 0, NULL
}};
917 * the 'show-wifi' command supports all the fields in wifi_common_fields
918 * plus the AUTH and STATUS fields.
920 static ofmt_field_t wifi_show_fields
[A_CNT(wifi_common_fields
) + 2] = {
921 { "AUTH", 9, DLADM_WLAN_ATTR_AUTH
, NULL
},
922 { "STATUS", 18, DLADM_WLAN_LINKATTR_STATUS
, print_wifi_status_cb
},
923 /* copy wifi_common_fields here */
926 static char *all_scan_wifi_fields
=
927 "link,essid,bssid,sec,strength,mode,speed,bsstype";
928 static char *all_show_wifi_fields
=
929 "link,status,essid,sec,strength,mode,speed,auth,bssid,bsstype";
930 static char *def_scan_wifi_fields
=
931 "link,essid,bssid,sec,strength,mode,speed";
932 static char *def_show_wifi_fields
=
933 "link,status,essid,sec,strength,mode,speed";
936 * structures for 'dladm show-linkprop'
945 } linkprop_field_index_t
;
947 static const ofmt_field_t linkprop_fields
[] = {
948 /* name, field width, index */
949 { "LINK", 13, LINKPROP_LINK
, print_linkprop_cb
},
950 { "PROPERTY", 16, LINKPROP_PROPERTY
, print_linkprop_cb
},
951 { "PERM", 5, LINKPROP_PERM
, print_linkprop_cb
},
952 { "VALUE", 15, LINKPROP_VALUE
, print_linkprop_cb
},
953 { "DEFAULT", 15, LINKPROP_DEFAULT
, print_linkprop_cb
},
954 { "POSSIBLE", 20, LINKPROP_POSSIBLE
, print_linkprop_cb
},
958 #define MAX_PROP_LINE 512
960 typedef struct show_linkprop_state
{
961 char ls_link
[MAXLINKNAMELEN
];
964 dladm_arg_list_t
*ls_proplist
;
965 boolean_t ls_parsable
;
966 boolean_t ls_persist
;
968 dladm_status_t ls_status
;
969 dladm_status_t ls_retstatus
;
970 ofmt_handle_t ls_ofmt
;
971 } show_linkprop_state_t
;
973 typedef struct set_linkprop_state
{
977 dladm_status_t ls_status
;
978 } set_linkprop_state_t
;
980 typedef struct linkprop_args_s
{
981 show_linkprop_state_t
*ls_state
;
983 datalink_id_t ls_linkid
;
987 * structures for 'dladm show-secobj'
989 typedef struct secobj_fields_buf_s
{
990 char ss_obj_name
[DLADM_SECOBJ_VAL_MAX
];
993 } secobj_fields_buf_t
;
995 static const ofmt_field_t secobj_fields
[] = {
997 offsetof(secobj_fields_buf_t
, ss_obj_name
), print_default_cb
},
999 offsetof(secobj_fields_buf_t
, ss_class
), print_default_cb
},
1001 offsetof(secobj_fields_buf_t
, ss_val
), print_default_cb
},
1002 { NULL
, 0, 0, NULL
}}
1006 * structures for 'dladm show-vnic'
1008 typedef struct vnic_fields_buf_s
1010 char vnic_link
[DLPI_LINKNAME_MAX
];
1011 char vnic_over
[DLPI_LINKNAME_MAX
];
1013 char vnic_macaddr
[18];
1014 char vnic_macaddrtype
[19];
1016 } vnic_fields_buf_t
;
1018 static const ofmt_field_t vnic_fields
[] = {
1020 offsetof(vnic_fields_buf_t
, vnic_link
), print_default_cb
},
1022 offsetof(vnic_fields_buf_t
, vnic_over
), print_default_cb
},
1024 offsetof(vnic_fields_buf_t
, vnic_speed
), print_default_cb
},
1026 offsetof(vnic_fields_buf_t
, vnic_macaddr
), print_default_cb
},
1027 { "MACADDRTYPE", 20,
1028 offsetof(vnic_fields_buf_t
, vnic_macaddrtype
), print_default_cb
},
1030 offsetof(vnic_fields_buf_t
, vnic_vid
), print_default_cb
},
1031 { NULL
, 0, 0, NULL
}}
1035 * structures for 'dladm show-ib'
1037 typedef struct ib_fields_buf_s
1039 char ib_link
[DLPI_LINKNAME_MAX
];
1040 char ib_hcaguid
[17];
1041 char ib_portguid
[17];
1044 char ib_pkeys
[MAXPKEYSTRSZ
];
1047 static const ofmt_field_t ib_fields
[] = {
1049 offsetof(ib_fields_buf_t
, ib_link
), print_default_cb
},
1050 { "HCAGUID", IBGUIDSTRLEN
,
1051 offsetof(ib_fields_buf_t
, ib_hcaguid
), print_default_cb
},
1052 { "PORTGUID", IBGUIDSTRLEN
,
1053 offsetof(ib_fields_buf_t
, ib_portguid
), print_default_cb
},
1054 { "PORT", IBPORTSTRLEN
,
1055 offsetof(ib_fields_buf_t
, ib_portnum
), print_default_cb
},
1057 offsetof(ib_fields_buf_t
, ib_state
), print_default_cb
},
1059 offsetof(ib_fields_buf_t
, ib_pkeys
), print_default_cb
},
1060 { NULL
, 0, 0, NULL
}};
1063 * structures for 'dladm show-part'
1065 typedef struct part_fields_buf_s
1067 char part_link
[DLPI_LINKNAME_MAX
];
1069 char part_over
[DLPI_LINKNAME_MAX
];
1072 } part_fields_buf_t
;
1074 static const ofmt_field_t part_fields
[] = {
1076 offsetof(part_fields_buf_t
, part_link
), print_default_cb
},
1077 { "PKEY", MAXPKEYLEN
,
1078 offsetof(part_fields_buf_t
, part_pkey
), print_default_cb
},
1080 offsetof(part_fields_buf_t
, part_over
), print_default_cb
},
1082 offsetof(part_fields_buf_t
, part_state
), print_default_cb
},
1084 offsetof(part_fields_buf_t
, part_flags
), print_default_cb
},
1085 { NULL
, 0, 0, NULL
}};
1088 * structures for 'dladm show-simnet'
1090 typedef struct simnet_fields_buf_s
1092 char simnet_name
[DLPI_LINKNAME_MAX
];
1093 char simnet_media
[DLADM_STRSIZE
];
1094 char simnet_macaddr
[18];
1095 char simnet_otherlink
[DLPI_LINKNAME_MAX
];
1096 } simnet_fields_buf_t
;
1098 static const ofmt_field_t simnet_fields
[] = {
1100 offsetof(simnet_fields_buf_t
, simnet_name
), print_default_cb
},
1102 offsetof(simnet_fields_buf_t
, simnet_media
), print_default_cb
},
1104 offsetof(simnet_fields_buf_t
, simnet_macaddr
), print_default_cb
},
1106 offsetof(simnet_fields_buf_t
, simnet_otherlink
), print_default_cb
},
1107 { NULL
, 0, 0, NULL
}}
1111 * structures for 'dladm show-usage'
1114 typedef struct usage_fields_buf_s
{
1115 char usage_link
[12];
1116 char usage_duration
[10];
1117 char usage_ipackets
[9];
1118 char usage_rbytes
[10];
1119 char usage_opackets
[9];
1120 char usage_obytes
[10];
1121 char usage_bandwidth
[14];
1122 } usage_fields_buf_t
;
1124 static const ofmt_field_t usage_fields
[] = {
1126 offsetof(usage_fields_buf_t
, usage_link
), print_default_cb
},
1128 offsetof(usage_fields_buf_t
, usage_duration
), print_default_cb
},
1130 offsetof(usage_fields_buf_t
, usage_ipackets
), print_default_cb
},
1132 offsetof(usage_fields_buf_t
, usage_rbytes
), print_default_cb
},
1134 offsetof(usage_fields_buf_t
, usage_opackets
), print_default_cb
},
1136 offsetof(usage_fields_buf_t
, usage_obytes
), print_default_cb
},
1138 offsetof(usage_fields_buf_t
, usage_bandwidth
), print_default_cb
},
1139 { NULL
, 0, 0, NULL
}}
1144 * structures for 'dladm show-usage link'
1147 typedef struct usage_l_fields_buf_s
{
1148 char usage_l_link
[12];
1149 char usage_l_stime
[13];
1150 char usage_l_etime
[13];
1151 char usage_l_rbytes
[8];
1152 char usage_l_obytes
[8];
1153 char usage_l_bandwidth
[14];
1154 } usage_l_fields_buf_t
;
1156 static const ofmt_field_t usage_l_fields
[] = {
1157 /* name, field width, offset */
1159 offsetof(usage_l_fields_buf_t
, usage_l_link
), print_default_cb
},
1161 offsetof(usage_l_fields_buf_t
, usage_l_stime
), print_default_cb
},
1163 offsetof(usage_l_fields_buf_t
, usage_l_etime
), print_default_cb
},
1165 offsetof(usage_l_fields_buf_t
, usage_l_rbytes
), print_default_cb
},
1167 offsetof(usage_l_fields_buf_t
, usage_l_obytes
), print_default_cb
},
1169 offsetof(usage_l_fields_buf_t
, usage_l_bandwidth
), print_default_cb
},
1170 { NULL
, 0, 0, NULL
}}
1173 /* IPTUN_*FLAG_INDEX values are indices into iptun_flags below. */
1174 enum { IPTUN_SFLAG_INDEX
, IPTUN_IFLAG_INDEX
, IPTUN_NUM_FLAGS
};
1177 * structures for 'dladm show-iptun'
1179 typedef struct iptun_fields_buf_s
{
1180 char iptun_name
[MAXLINKNAMELEN
];
1182 char iptun_laddr
[NI_MAXHOST
];
1183 char iptun_raddr
[NI_MAXHOST
];
1184 char iptun_flags
[IPTUN_NUM_FLAGS
+ 1];
1185 } iptun_fields_buf_t
;
1187 static const ofmt_field_t iptun_fields
[] = {
1189 offsetof(iptun_fields_buf_t
, iptun_name
), print_default_cb
},
1191 offsetof(iptun_fields_buf_t
, iptun_type
), print_default_cb
},
1193 offsetof(iptun_fields_buf_t
, iptun_flags
), print_default_cb
},
1195 offsetof(iptun_fields_buf_t
, iptun_laddr
), print_default_cb
},
1197 offsetof(iptun_fields_buf_t
, iptun_raddr
), print_default_cb
},
1202 * structures for 'dladm show-bridge'. These are based on sections 14.8.1.1.3
1203 * and 14.8.1.2.2 of IEEE 802.1D-2004.
1205 typedef struct bridge_fields_buf_s
{
1206 char bridge_name
[MAXLINKNAMELEN
]; /* 14.4.1.2.3(b) */
1207 char bridge_protect
[7]; /* stp or trill */
1208 char bridge_address
[24]; /* 17.18.3, 7.12.5, 14.4.1.2.3(a) */
1209 char bridge_priority
[7]; /* 17.18.3 9.2.5 - only upper 4 bits */
1210 char bridge_bmaxage
[7]; /* 17.18.4 configured */
1211 char bridge_bhellotime
[7]; /* 17.18.4 configured */
1212 char bridge_bfwddelay
[7]; /* 17.18.4 configured */
1213 char bridge_forceproto
[3]; /* 17.13.4 configured */
1214 char bridge_tctime
[12]; /* 14.8.1.1.3(b) */
1215 char bridge_tccount
[12]; /* 17.17.8 */
1216 char bridge_tchange
[12]; /* 17.17.8 */
1217 char bridge_desroot
[24]; /* 17.18.6 priority "/" MAC */
1218 char bridge_rootcost
[12]; /* 17.18.6 */
1219 char bridge_rootport
[12]; /* 17.18.6 */
1220 char bridge_maxage
[7]; /* 17.18.7 for root */
1221 char bridge_hellotime
[7]; /* 17.13.6 for root */
1222 char bridge_fwddelay
[7]; /* 17.13.5 for root */
1223 char bridge_holdtime
[12]; /* 17.13.12 for root */
1224 } bridge_fields_buf_t
;
1226 static ofmt_field_t bridge_fields
[] = {
1227 /* name, field width, offset, callback */
1229 offsetof(bridge_fields_buf_t
, bridge_name
), print_default_cb
},
1231 offsetof(bridge_fields_buf_t
, bridge_protect
), print_default_cb
},
1233 offsetof(bridge_fields_buf_t
, bridge_address
), print_default_cb
},
1235 offsetof(bridge_fields_buf_t
, bridge_priority
), print_default_cb
},
1237 offsetof(bridge_fields_buf_t
, bridge_bmaxage
), print_default_cb
},
1239 offsetof(bridge_fields_buf_t
, bridge_bhellotime
), print_default_cb
},
1241 offsetof(bridge_fields_buf_t
, bridge_bfwddelay
), print_default_cb
},
1243 offsetof(bridge_fields_buf_t
, bridge_forceproto
), print_default_cb
},
1245 offsetof(bridge_fields_buf_t
, bridge_tctime
), print_default_cb
},
1247 offsetof(bridge_fields_buf_t
, bridge_tccount
), print_default_cb
},
1249 offsetof(bridge_fields_buf_t
, bridge_tchange
), print_default_cb
},
1251 offsetof(bridge_fields_buf_t
, bridge_desroot
), print_default_cb
},
1253 offsetof(bridge_fields_buf_t
, bridge_rootcost
), print_default_cb
},
1255 offsetof(bridge_fields_buf_t
, bridge_rootport
), print_default_cb
},
1257 offsetof(bridge_fields_buf_t
, bridge_maxage
), print_default_cb
},
1259 offsetof(bridge_fields_buf_t
, bridge_hellotime
), print_default_cb
},
1261 offsetof(bridge_fields_buf_t
, bridge_fwddelay
), print_default_cb
},
1263 offsetof(bridge_fields_buf_t
, bridge_holdtime
), print_default_cb
},
1264 { NULL
, 0, 0, NULL
}};
1267 * structures for 'dladm show-bridge -l'. These are based on 14.4.1.2.3 and
1268 * 14.8.2.1.3 of IEEE 802.1D-2004.
1270 typedef struct bridge_link_fields_buf_s
{
1271 char bridgel_link
[MAXLINKNAMELEN
];
1272 char bridgel_index
[7]; /* 14.4.1.2.3(d1) */
1273 char bridgel_state
[11]; /* 14.8.2.1.3(b) */
1274 char bridgel_uptime
[7]; /* 14.8.2.1.3(a) */
1275 char bridgel_opercost
[7] /* 14.8.2.1.3(d) */;
1276 char bridgel_operp2p
[4]; /* 14.8.2.1.3(p) */
1277 char bridgel_operedge
[4]; /* 14.8.2.1.3(k) */
1278 char bridgel_desroot
[23]; /* 14.8.2.1.3(e) */
1279 char bridgel_descost
[12]; /* 14.8.2.1.3(f) */
1280 char bridgel_desbridge
[23]; /* 14.8.2.1.3(g) */
1281 char bridgel_desport
[7]; /* 14.8.2.1.3(h) */
1282 char bridgel_tcack
[4]; /* 14.8.2.1.3(i) */
1283 } bridge_link_fields_buf_t
;
1285 static ofmt_field_t bridge_link_fields
[] = {
1286 /* name, field width, offset, callback */
1288 offsetof(bridge_link_fields_buf_t
, bridgel_link
), print_default_cb
},
1290 offsetof(bridge_link_fields_buf_t
, bridgel_index
), print_default_cb
},
1292 offsetof(bridge_link_fields_buf_t
, bridgel_state
), print_default_cb
},
1294 offsetof(bridge_link_fields_buf_t
, bridgel_uptime
), print_default_cb
},
1296 offsetof(bridge_link_fields_buf_t
, bridgel_opercost
), print_default_cb
},
1298 offsetof(bridge_link_fields_buf_t
, bridgel_operp2p
), print_default_cb
},
1300 offsetof(bridge_link_fields_buf_t
, bridgel_operedge
), print_default_cb
},
1302 offsetof(bridge_link_fields_buf_t
, bridgel_desroot
), print_default_cb
},
1304 offsetof(bridge_link_fields_buf_t
, bridgel_descost
), print_default_cb
},
1306 offsetof(bridge_link_fields_buf_t
, bridgel_desbridge
), print_default_cb
},
1308 offsetof(bridge_link_fields_buf_t
, bridgel_desport
), print_default_cb
},
1310 offsetof(bridge_link_fields_buf_t
, bridgel_tcack
), print_default_cb
},
1311 { NULL
, 0, 0, NULL
}};
1314 * structures for 'dladm show-bridge -s'. These are not based on IEEE
1317 #define ULONG_DIG (((sizeof (ulong_t) * NBBY) * 3 / 10) + 1)
1318 #define UINT64_DIG (((sizeof (uint64_t) * NBBY) * 3 / 10) + 1)
1319 typedef struct bridge_statfields_buf_s
{
1320 char bridges_name
[MAXLINKNAMELEN
];
1321 char bridges_drops
[UINT64_DIG
];
1322 char bridges_forwards
[UINT64_DIG
];
1323 char bridges_mbcast
[UINT64_DIG
];
1324 char bridges_unknown
[UINT64_DIG
];
1325 char bridges_recv
[UINT64_DIG
];
1326 char bridges_sent
[UINT64_DIG
];
1327 } bridge_statfields_buf_t
;
1329 static ofmt_field_t bridge_statfields
[] = {
1330 /* name, field width, offset, callback */
1332 offsetof(bridge_statfields_buf_t
, bridges_name
), print_default_cb
},
1334 offsetof(bridge_statfields_buf_t
, bridges_drops
), print_default_cb
},
1336 offsetof(bridge_statfields_buf_t
, bridges_forwards
), print_default_cb
},
1338 offsetof(bridge_statfields_buf_t
, bridges_mbcast
), print_default_cb
},
1340 offsetof(bridge_statfields_buf_t
, bridges_unknown
), print_default_cb
},
1342 offsetof(bridge_statfields_buf_t
, bridges_recv
), print_default_cb
},
1344 offsetof(bridge_statfields_buf_t
, bridges_sent
), print_default_cb
},
1345 { NULL
, 0, 0, NULL
}};
1348 * structures for 'dladm show-bridge -s -l'. These are based in part on
1349 * section 14.6.1.1.3 of IEEE 802.1D-2004.
1351 typedef struct bridge_link_statfields_buf_s
{
1352 char bridgels_link
[MAXLINKNAMELEN
];
1353 char bridgels_cfgbpdu
[ULONG_DIG
];
1354 char bridgels_tcnbpdu
[ULONG_DIG
];
1355 char bridgels_rstpbpdu
[ULONG_DIG
];
1356 char bridgels_txbpdu
[ULONG_DIG
];
1357 char bridgels_drops
[UINT64_DIG
]; /* 14.6.1.1.3(d) */
1358 char bridgels_recv
[UINT64_DIG
]; /* 14.6.1.1.3(a) */
1359 char bridgels_xmit
[UINT64_DIG
]; /* 14.6.1.1.3(c) */
1360 } bridge_link_statfields_buf_t
;
1362 static ofmt_field_t bridge_link_statfields
[] = {
1363 /* name, field width, offset, callback */
1365 offsetof(bridge_link_statfields_buf_t
, bridgels_link
), print_default_cb
},
1367 offsetof(bridge_link_statfields_buf_t
, bridgels_cfgbpdu
),
1370 offsetof(bridge_link_statfields_buf_t
, bridgels_tcnbpdu
),
1373 offsetof(bridge_link_statfields_buf_t
, bridgels_rstpbpdu
),
1376 offsetof(bridge_link_statfields_buf_t
, bridgels_txbpdu
), print_default_cb
},
1378 offsetof(bridge_link_statfields_buf_t
, bridgels_drops
), print_default_cb
},
1380 offsetof(bridge_link_statfields_buf_t
, bridgels_recv
), print_default_cb
},
1382 offsetof(bridge_link_statfields_buf_t
, bridgels_xmit
), print_default_cb
},
1383 { NULL
, 0, 0, NULL
}};
1386 * structures for 'dladm show-bridge -f'. These are based in part on
1387 * section 14.7.6.3.3 of IEEE 802.1D-2004.
1389 typedef struct bridge_fwd_fields_buf_s
{
1390 char bridgef_dest
[18]; /* 14.7.6.3.3(a) */
1391 char bridgef_age
[8];
1392 char bridgef_flags
[6];
1393 char bridgef_output
[MAXLINKNAMELEN
]; /* 14.7.6.3.3(c) */
1394 } bridge_fwd_fields_buf_t
;
1396 static ofmt_field_t bridge_fwd_fields
[] = {
1397 /* name, field width, offset, callback */
1399 offsetof(bridge_fwd_fields_buf_t
, bridgef_dest
), print_default_cb
},
1401 offsetof(bridge_fwd_fields_buf_t
, bridgef_age
), print_default_cb
},
1403 offsetof(bridge_fwd_fields_buf_t
, bridgef_flags
), print_default_cb
},
1405 offsetof(bridge_fwd_fields_buf_t
, bridgef_output
), print_default_cb
},
1406 { NULL
, 0, 0, NULL
}};
1409 * structures for 'dladm show-bridge -t'.
1411 typedef struct bridge_trill_fields_buf_s
{
1412 char bridget_nick
[6];
1413 char bridget_flags
[6];
1414 char bridget_link
[MAXLINKNAMELEN
];
1415 char bridget_nexthop
[18];
1416 } bridge_trill_fields_buf_t
;
1418 static ofmt_field_t bridge_trill_fields
[] = {
1419 /* name, field width, offset, callback */
1421 offsetof(bridge_trill_fields_buf_t
, bridget_nick
), print_default_cb
},
1423 offsetof(bridge_trill_fields_buf_t
, bridget_flags
), print_default_cb
},
1425 offsetof(bridge_trill_fields_buf_t
, bridget_link
), print_default_cb
},
1427 offsetof(bridge_trill_fields_buf_t
, bridget_nexthop
), print_default_cb
},
1428 { NULL
, 0, 0, NULL
}};
1430 static char *progname
;
1431 static sig_atomic_t signalled
;
1434 * Handle to libdladm. Opened in main() before the sub-command
1435 * specific function is called.
1437 static dladm_handle_t handle
= NULL
;
1439 #define DLADM_ETHERSTUB_NAME "etherstub"
1440 #define DLADM_IS_ETHERSTUB(id) (id == DATALINK_INVALID_LINKID)
1447 (void) fprintf(stderr
, gettext("usage: dladm <subcommand> <args> ..."
1449 for (i
= 0; i
< sizeof (cmds
) / sizeof (cmds
[0]); i
++) {
1451 if (cmdp
->c_usage
!= NULL
)
1452 (void) fprintf(stderr
, "%s\n", gettext(cmdp
->c_usage
));
1455 /* close dladm handle if it was opened */
1457 dladm_close(handle
);
1463 main(int argc
, char *argv
[])
1467 dladm_status_t status
;
1469 (void) setlocale(LC_ALL
, "");
1470 #if !defined(TEXT_DOMAIN)
1471 #define TEXT_DOMAIN "SYS_TEST"
1473 (void) textdomain(TEXT_DOMAIN
);
1480 for (i
= 0; i
< sizeof (cmds
) / sizeof (cmds
[0]); i
++) {
1482 if (strcmp(argv
[1], cmdp
->c_name
) == 0) {
1483 /* Open the libdladm handle */
1484 if ((status
= dladm_open(&handle
)) != DLADM_STATUS_OK
) {
1486 "could not open /dev/dld");
1489 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 dladm_ofmt_check(oferr
, state
.us_parsable
, ofmt
);
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(1M))");
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(1M))");
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 "
2431 "dladm(1M))", link
);
2434 case DLADM_STATUS_LINKBUSY
:
2435 die("VLAN over '%s' may not use default_tag ID "
2436 "(see dladm(1M))", 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 dladm_ofmt_check(oferr
, state
.ls_parsable
, ofmt
);
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 dladm_ofmt_check(oferr
, state
.gs_parsable
, ofmt
);
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 (void) snprintf(pattr
.link_phys_device
,
3711 sizeof (pattr
.link_phys_device
), "%s", dpa
.dp_dev
);
3712 (void) dladm_media2str(media
, pattr
.link_phys_media
);
3713 if (state
->ls_flags
== DLADM_OPT_ACTIVE
) {
3716 if (!dpa
.dp_novanity
) {
3717 (void) strlcpy(pattr
.link_name
, link
,
3718 sizeof (pattr
.link_name
));
3722 * This is a physical link that does not have
3723 * vanity naming support.
3725 (void) strlcpy(pattr
.link_name
, dpa
.dp_dev
,
3726 sizeof (pattr
.link_name
));
3730 (void) get_linkstate(pattr
.link_name
, islink
,
3731 pattr
.link_phys_state
);
3732 (void) snprintf(pattr
.link_phys_speed
,
3733 sizeof (pattr
.link_phys_speed
), "%u",
3734 (uint_t
)((get_ifspeed(pattr
.link_name
,
3735 islink
)) / 1000000ull));
3736 (void) get_linkduplex(pattr
.link_name
, islink
,
3737 pattr
.link_phys_duplex
);
3739 (void) snprintf(pattr
.link_name
, sizeof (pattr
.link_name
),
3741 (void) snprintf(pattr
.link_flags
, sizeof (pattr
.link_flags
),
3742 "%c----", flags
& DLADM_OPT_ACTIVE
? '-' : 'r');
3745 ofmt_print(state
->ls_ofmt
, &pattr
);
3752 show_state_t
*ms_state
;
3754 dladm_macaddr_attr_t
*ms_mac_attr
;
3755 } print_phys_mac_state_t
;
3758 * callback for ofmt_print()
3761 print_phys_one_mac_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
3763 print_phys_mac_state_t
*mac_state
= ofarg
->ofmt_cbarg
;
3764 dladm_macaddr_attr_t
*attr
= mac_state
->ms_mac_attr
;
3765 boolean_t is_primary
= (attr
->ma_slot
== 0);
3766 boolean_t is_parsable
= mac_state
->ms_state
->ls_parsable
;
3768 switch (ofarg
->ofmt_id
) {
3770 (void) snprintf(buf
, bufsize
, "%s",
3771 (is_primary
|| is_parsable
) ? mac_state
->ms_link
: " ");
3775 (void) snprintf(buf
, bufsize
, gettext("primary"));
3777 (void) snprintf(buf
, bufsize
, "%d", attr
->ma_slot
);
3779 case PHYS_M_ADDRESS
:
3780 (void) dladm_aggr_macaddr2str(attr
->ma_addr
, buf
);
3783 (void) snprintf(buf
, bufsize
, "%s",
3784 attr
->ma_flags
& DLADM_MACADDR_USED
? gettext("yes") :
3789 * CR 6678526: resolve link id to actual link name if
3792 (void) snprintf(buf
, bufsize
, "%s", attr
->ma_client_name
);
3800 show_state_t
*hs_state
;
3802 dladm_hwgrp_attr_t
*hs_grp_attr
;
3803 } print_phys_hwgrp_state_t
;
3806 print_phys_one_hwgrp_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
3809 boolean_t first
= B_TRUE
;
3812 char ringstr
[RINGSTRLEN
];
3813 char ringsubstr
[RINGSTRLEN
];
3815 print_phys_hwgrp_state_t
*hg_state
= ofarg
->ofmt_cbarg
;
3816 dladm_hwgrp_attr_t
*attr
= hg_state
->hs_grp_attr
;
3818 switch (ofarg
->ofmt_id
) {
3820 (void) snprintf(buf
, bufsize
, "%s", attr
->hg_link_name
);
3822 case PHYS_H_RINGTYPE
:
3823 (void) snprintf(buf
, bufsize
, "%s",
3824 attr
->hg_grp_type
== DLADM_HWGRP_TYPE_RX
? "RX" : "TX");
3828 for (i
= 0; i
< attr
->hg_n_rings
; i
++) {
3829 uint_t index
= attr
->hg_rings
[i
];
3834 } else if (index
== end
+ 1) {
3866 (void) strlcat(ringstr
, ringsubstr
,
3876 (void) snprintf(buf
, bufsize
, "%d",
3879 (void) snprintf(buf
, bufsize
, "%d-%d",
3884 (void) snprintf(ringsubstr
, RINGSTRLEN
,
3887 (void) snprintf(ringsubstr
, RINGSTRLEN
,
3888 ",%d-%d", start
, end
);
3890 (void) strlcat(ringstr
, ringsubstr
, RINGSTRLEN
);
3891 (void) snprintf(buf
, bufsize
, "%s", ringstr
);
3895 case PHYS_H_CLIENTS
:
3896 if (attr
->hg_client_names
[0] == '\0') {
3897 (void) snprintf(buf
, bufsize
, "--");
3899 (void) snprintf(buf
, bufsize
, "%s ",
3900 attr
->hg_client_names
);
3909 * callback for dladm_walk_macaddr, invoked for each MAC address slot
3912 print_phys_mac_callback(void *arg
, dladm_macaddr_attr_t
*attr
)
3914 print_phys_mac_state_t
*mac_state
= arg
;
3915 show_state_t
*state
= mac_state
->ms_state
;
3917 mac_state
->ms_mac_attr
= attr
;
3918 ofmt_print(state
->ls_ofmt
, mac_state
);
3924 * invoked by show-phys -m for each physical data-link
3926 static dladm_status_t
3927 print_phys_mac(show_state_t
*state
, datalink_id_t linkid
, char *link
)
3929 print_phys_mac_state_t mac_state
;
3931 mac_state
.ms_state
= state
;
3932 mac_state
.ms_link
= link
;
3934 return (dladm_walk_macaddr(handle
, linkid
, &mac_state
,
3935 print_phys_mac_callback
));
3939 * callback for dladm_walk_hwgrp, invoked for each MAC hwgrp
3942 print_phys_hwgrp_callback(void *arg
, dladm_hwgrp_attr_t
*attr
)
3944 print_phys_hwgrp_state_t
*hwgrp_state
= arg
;
3945 show_state_t
*state
= hwgrp_state
->hs_state
;
3947 hwgrp_state
->hs_grp_attr
= attr
;
3948 ofmt_print(state
->ls_ofmt
, hwgrp_state
);
3953 /* invoked by show-phys -H for each physical data-link */
3954 static dladm_status_t
3955 print_phys_hwgrp(show_state_t
*state
, datalink_id_t linkid
, char *link
)
3957 print_phys_hwgrp_state_t hwgrp_state
;
3959 hwgrp_state
.hs_state
= state
;
3960 hwgrp_state
.hs_link
= link
;
3961 return (dladm_walk_hwgrp(handle
, linkid
, &hwgrp_state
,
3962 print_phys_hwgrp_callback
));
3966 * Parse the "local=<laddr>,remote=<raddr>" sub-options for the -a option of
3967 * *-iptun subcommands.
3970 iptun_process_addrarg(char *addrarg
, iptun_params_t
*params
)
3974 while (*addrarg
!= '\0') {
3975 switch (getsubopt(&addrarg
, iptun_addropts
, &addrval
)) {
3977 params
->iptun_param_flags
|= IPTUN_PARAM_LADDR
;
3978 if (strlcpy(params
->iptun_param_laddr
, addrval
,
3979 sizeof (params
->iptun_param_laddr
)) >=
3980 sizeof (params
->iptun_param_laddr
))
3981 die("tunnel source address is too long");
3984 params
->iptun_param_flags
|= IPTUN_PARAM_RADDR
;
3985 if (strlcpy(params
->iptun_param_raddr
, addrval
,
3986 sizeof (params
->iptun_param_raddr
)) >=
3987 sizeof (params
->iptun_param_raddr
))
3988 die("tunnel destination address is too long");
3991 die("invalid address type: %s", addrval
);
3998 * Convenience routine to process iptun-create/modify/delete subcommand
4002 iptun_process_args(int argc
, char *argv
[], const char *opts
,
4003 iptun_params_t
*params
, uint32_t *flags
, char *name
, const char *use
)
4006 char *altroot
= NULL
;
4009 bzero(params
, sizeof (*params
));
4010 *flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
4013 while ((option
= getopt_long(argc
, argv
, opts
, iptun_lopts
, NULL
)) !=
4017 iptun_process_addrarg(optarg
, params
);
4023 *flags
&= ~DLADM_OPT_PERSIST
;
4026 params
->iptun_param_type
= iptun_gettypebyname(optarg
);
4027 if (params
->iptun_param_type
== IPTUN_TYPE_UNKNOWN
)
4028 die("unknown tunnel type: %s", optarg
);
4029 params
->iptun_param_flags
|= IPTUN_PARAM_TYPE
;
4032 die_opterr(optopt
, option
, use
);
4037 /* Get the required tunnel name argument. */
4038 if (argc
- optind
!= 1)
4041 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
4042 die("tunnel name is too long");
4044 if (altroot
!= NULL
)
4045 altroot_cmd(altroot
, argc
, argv
);
4049 do_create_iptun(int argc
, char *argv
[], const char *use
)
4051 iptun_params_t params
;
4052 dladm_status_t status
;
4054 char name
[MAXLINKNAMELEN
];
4056 iptun_process_args(argc
, argv
, ":a:R:tT:", ¶ms
, &flags
, name
,
4059 status
= dladm_iptun_create(handle
, name
, ¶ms
, flags
);
4060 if (status
!= DLADM_STATUS_OK
)
4061 die_dlerr(status
, "could not create tunnel");
4065 do_delete_iptun(int argc
, char *argv
[], const char *use
)
4068 datalink_id_t linkid
;
4069 dladm_status_t status
;
4070 char name
[MAXLINKNAMELEN
];
4072 iptun_process_args(argc
, argv
, ":R:t", NULL
, &flags
, name
, use
);
4074 status
= dladm_name2info(handle
, name
, &linkid
, NULL
, NULL
, NULL
);
4075 if (status
!= DLADM_STATUS_OK
)
4076 die_dlerr(status
, "could not delete tunnel");
4077 status
= dladm_iptun_delete(handle
, linkid
, flags
);
4078 if (status
!= DLADM_STATUS_OK
)
4079 die_dlerr(status
, "could not delete tunnel");
4083 do_modify_iptun(int argc
, char *argv
[], const char *use
)
4085 iptun_params_t params
;
4087 dladm_status_t status
;
4088 char name
[MAXLINKNAMELEN
];
4090 iptun_process_args(argc
, argv
, ":a:R:t", ¶ms
, &flags
, name
, use
);
4092 if ((status
= dladm_name2info(handle
, name
, ¶ms
.iptun_param_linkid
,
4093 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
)
4094 die_dlerr(status
, "could not modify tunnel");
4095 status
= dladm_iptun_modify(handle
, ¶ms
, flags
);
4096 if (status
!= DLADM_STATUS_OK
)
4097 die_dlerr(status
, "could not modify tunnel");
4101 do_show_iptun(int argc
, char *argv
[], const char *use
)
4104 datalink_id_t linkid
;
4105 uint32_t flags
= DLADM_OPT_ACTIVE
;
4107 dladm_status_t status
;
4108 const char *fields_str
= NULL
;
4111 ofmt_status_t oferr
;
4112 uint_t ofmtflags
= 0;
4114 bzero(&state
, sizeof (state
));
4116 while ((option
= getopt_long(argc
, argv
, ":pPo:",
4117 iptun_lopts
, NULL
)) != -1) {
4120 fields_str
= optarg
;
4123 state
.ls_parsable
= B_TRUE
;
4124 ofmtflags
= OFMT_PARSABLE
;
4127 flags
= DLADM_OPT_PERSIST
;
4130 die_opterr(optopt
, option
, use
);
4136 * Get the optional tunnel name argument. If there is one, it must
4137 * be the last thing remaining on the command-line.
4139 if (argc
- optind
> 1)
4141 if (argc
- optind
== 1)
4142 name
= argv
[optind
];
4144 oferr
= ofmt_open(fields_str
, iptun_fields
, ofmtflags
,
4145 DLADM_DEFAULT_COL
, &ofmt
);
4146 dladm_ofmt_check(oferr
, state
.ls_parsable
, ofmt
);
4148 state
.ls_ofmt
= ofmt
;
4149 state
.ls_flags
= flags
;
4152 (void) dladm_walk_datalink_id(print_iptun_walker
, handle
,
4153 &state
, DATALINK_CLASS_IPTUN
, DATALINK_ANY_MEDIATYPE
,
4155 status
= state
.ls_status
;
4157 if ((status
= dladm_name2info(handle
, name
, &linkid
, NULL
, NULL
,
4158 NULL
)) == DLADM_STATUS_OK
)
4159 status
= print_iptun(handle
, linkid
, &state
);
4162 if (status
!= DLADM_STATUS_OK
)
4163 die_dlerr(status
, "unable to obtain tunnel status");
4168 do_up_iptun(int argc
, char *argv
[], const char *use
)
4170 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4171 dladm_status_t status
= DLADM_STATUS_OK
;
4174 * Get the optional tunnel name argument. If there is one, it must
4175 * be the last thing remaining on the command-line.
4177 if (argc
- optind
> 1)
4179 if (argc
- optind
== 1) {
4180 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
4183 if (status
== DLADM_STATUS_OK
)
4184 status
= dladm_iptun_up(handle
, linkid
);
4185 if (status
!= DLADM_STATUS_OK
)
4186 die_dlerr(status
, "unable to configure IP tunnel links");
4191 do_down_iptun(int argc
, char *argv
[], const char *use
)
4193 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4194 dladm_status_t status
= DLADM_STATUS_OK
;
4197 * Get the optional tunnel name argument. If there is one, it must
4198 * be the last thing remaining on the command-line.
4200 if (argc
- optind
> 1)
4202 if (argc
- optind
== 1) {
4203 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
4206 if (status
== DLADM_STATUS_OK
)
4207 status
= dladm_iptun_down(handle
, linkid
);
4208 if (status
!= DLADM_STATUS_OK
)
4209 die_dlerr(status
, "unable to bring down IP tunnel links");
4213 iptun_gettypebyname(char *typestr
)
4217 for (i
= 0; iptun_types
[i
].type_name
!= NULL
; i
++) {
4218 if (strncmp(iptun_types
[i
].type_name
, typestr
,
4219 strlen(iptun_types
[i
].type_name
)) == 0) {
4220 return (iptun_types
[i
].type_value
);
4223 return (IPTUN_TYPE_UNKNOWN
);
4227 iptun_gettypebyvalue(iptun_type_t type
)
4231 for (i
= 0; iptun_types
[i
].type_name
!= NULL
; i
++) {
4232 if (iptun_types
[i
].type_value
== type
)
4233 return (iptun_types
[i
].type_name
);
4238 static dladm_status_t
4239 print_iptun(dladm_handle_t dh
, datalink_id_t linkid
, show_state_t
*state
)
4241 dladm_status_t status
;
4242 iptun_params_t params
;
4243 iptun_fields_buf_t lbuf
;
4247 params
.iptun_param_linkid
= linkid
;
4248 status
= dladm_iptun_getparams(dh
, ¶ms
, state
->ls_flags
);
4249 if (status
!= DLADM_STATUS_OK
)
4253 status
= dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
,
4254 lbuf
.iptun_name
, sizeof (lbuf
.iptun_name
));
4255 if (status
!= DLADM_STATUS_OK
)
4259 (void) strlcpy(lbuf
.iptun_type
,
4260 iptun_gettypebyvalue(params
.iptun_param_type
),
4261 sizeof (lbuf
.iptun_type
));
4264 (void) memset(lbuf
.iptun_flags
, '-', IPTUN_NUM_FLAGS
);
4265 lbuf
.iptun_flags
[IPTUN_NUM_FLAGS
] = '\0';
4266 if (params
.iptun_param_flags
& IPTUN_PARAM_IPSECPOL
)
4267 lbuf
.iptun_flags
[IPTUN_SFLAG_INDEX
] = 's';
4268 if (params
.iptun_param_flags
& IPTUN_PARAM_IMPLICIT
)
4269 lbuf
.iptun_flags
[IPTUN_IFLAG_INDEX
] = 'i';
4272 if (params
.iptun_param_flags
& IPTUN_PARAM_LADDR
)
4273 laddr
= params
.iptun_param_laddr
;
4275 laddr
= (state
->ls_parsable
) ? "" : "--";
4276 (void) strlcpy(lbuf
.iptun_laddr
, laddr
, sizeof (lbuf
.iptun_laddr
));
4279 if (params
.iptun_param_flags
& IPTUN_PARAM_RADDR
)
4280 raddr
= params
.iptun_param_raddr
;
4282 raddr
= (state
->ls_parsable
) ? "" : "--";
4283 (void) strlcpy(lbuf
.iptun_raddr
, raddr
, sizeof (lbuf
.iptun_raddr
));
4285 ofmt_print(state
->ls_ofmt
, &lbuf
);
4287 return (DLADM_STATUS_OK
);
4291 print_iptun_walker(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
4293 ((show_state_t
*)arg
)->ls_status
= print_iptun(dh
, linkid
, arg
);
4294 return (DLADM_WALK_CONTINUE
);
4297 static dladm_status_t
4298 print_phys(show_state_t
*state
, datalink_id_t linkid
)
4300 char link
[MAXLINKNAMELEN
];
4302 dladm_status_t status
;
4303 datalink_class_t
class;
4306 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
4307 &media
, link
, MAXLINKNAMELEN
)) != DLADM_STATUS_OK
) {
4311 if (class != DATALINK_CLASS_PHYS
) {
4312 status
= DLADM_STATUS_BADARG
;
4316 if (!(state
->ls_flags
& flags
)) {
4317 status
= DLADM_STATUS_NOTFOUND
;
4322 status
= print_phys_mac(state
, linkid
, link
);
4323 else if (state
->ls_hwgrp
)
4324 status
= print_phys_hwgrp(state
, linkid
, link
);
4326 status
= print_phys_default(state
, linkid
, link
, flags
, media
);
4334 show_phys(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
4336 show_state_t
*state
= arg
;
4338 state
->ls_status
= print_phys(state
, linkid
);
4339 return (DLADM_WALK_CONTINUE
);
4343 * Print the active topology information.
4345 static dladm_status_t
4346 print_vlan(show_state_t
*state
, datalink_id_t linkid
, link_fields_buf_t
*l
)
4348 dladm_vlan_attr_t vinfo
;
4350 dladm_status_t status
;
4352 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, NULL
, NULL
,
4353 l
->link_name
, sizeof (l
->link_name
))) != DLADM_STATUS_OK
) {
4357 if (!(state
->ls_flags
& flags
)) {
4358 status
= DLADM_STATUS_NOTFOUND
;
4362 if ((status
= dladm_vlan_info(handle
, linkid
, &vinfo
,
4363 state
->ls_flags
)) != DLADM_STATUS_OK
||
4364 (status
= dladm_datalink_id2info(handle
, vinfo
.dv_linkid
, NULL
,
4365 NULL
, NULL
, l
->link_over
, sizeof (l
->link_over
))) !=
4370 (void) snprintf(l
->link_vlan_vid
, sizeof (l
->link_vlan_vid
), "%d",
4372 (void) snprintf(l
->link_flags
, sizeof (l
->link_flags
), "%c----",
4373 vinfo
.dv_force
? 'f' : '-');
4381 show_vlan(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
4383 show_state_t
*state
= arg
;
4384 dladm_status_t status
;
4385 link_fields_buf_t lbuf
;
4387 bzero(&lbuf
, sizeof (link_fields_buf_t
));
4388 status
= print_vlan(state
, linkid
, &lbuf
);
4389 if (status
!= DLADM_STATUS_OK
)
4392 ofmt_print(state
->ls_ofmt
, &lbuf
);
4395 state
->ls_status
= status
;
4396 return (DLADM_WALK_CONTINUE
);
4400 do_show_phys(int argc
, char *argv
[], const char *use
)
4403 uint32_t flags
= DLADM_OPT_ACTIVE
;
4404 boolean_t p_arg
= B_FALSE
;
4405 boolean_t o_arg
= B_FALSE
;
4406 boolean_t m_arg
= B_FALSE
;
4407 boolean_t H_arg
= B_FALSE
;
4408 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4410 dladm_status_t status
;
4411 char *fields_str
= NULL
;
4412 char *all_active_fields
=
4413 "link,media,state,speed,duplex,device";
4414 char *all_inactive_fields
= "link,device,media,flags";
4415 char *all_mac_fields
= "link,slot,address,inuse,client";
4416 char *all_hwgrp_fields
= "link,ringtype,rings,clients";
4417 const ofmt_field_t
*pf
;
4419 ofmt_status_t oferr
;
4420 uint_t ofmtflags
= 0;
4422 bzero(&state
, sizeof (state
));
4424 while ((option
= getopt_long(argc
, argv
, ":pPo:mH",
4425 show_lopts
, NULL
)) != -1) {
4434 if (flags
!= DLADM_OPT_ACTIVE
)
4437 flags
= DLADM_OPT_PERSIST
;
4441 fields_str
= optarg
;
4450 die_opterr(optopt
, option
, use
);
4455 if (p_arg
&& !o_arg
)
4456 die("-p requires -o");
4459 die("-m cannot combine with -H");
4461 if (p_arg
&& strcasecmp(fields_str
, "all") == 0)
4462 die("\"-o all\" is invalid with -p");
4464 /* get link name (optional last argument) */
4465 if (optind
== (argc
-1)) {
4466 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
4467 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
4468 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
4470 } else if (optind
!= argc
) {
4474 state
.ls_parsable
= p_arg
;
4475 state
.ls_flags
= flags
;
4476 state
.ls_donefirst
= B_FALSE
;
4477 state
.ls_mac
= m_arg
;
4478 state
.ls_hwgrp
= H_arg
;
4480 if (m_arg
&& !(flags
& DLADM_OPT_ACTIVE
)) {
4482 * We can only display the factory MAC addresses of
4483 * active data-links.
4485 die("-m not compatible with -P");
4488 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0)) {
4490 fields_str
= all_mac_fields
;
4491 else if (state
.ls_hwgrp
)
4492 fields_str
= all_hwgrp_fields
;
4493 else if (state
.ls_flags
& DLADM_OPT_ACTIVE
) {
4494 fields_str
= all_active_fields
;
4496 fields_str
= all_inactive_fields
;
4502 } else if (state
.ls_hwgrp
) {
4508 if (state
.ls_parsable
)
4509 ofmtflags
|= OFMT_PARSABLE
;
4510 oferr
= ofmt_open(fields_str
, pf
, ofmtflags
, 0, &ofmt
);
4511 dladm_ofmt_check(oferr
, state
.ls_parsable
, ofmt
);
4512 state
.ls_ofmt
= ofmt
;
4514 if (linkid
== DATALINK_ALL_LINKID
) {
4515 (void) dladm_walk_datalink_id(show_phys
, handle
, &state
,
4516 DATALINK_CLASS_PHYS
, DATALINK_ANY_MEDIATYPE
, flags
);
4518 (void) show_phys(handle
, linkid
, &state
);
4519 if (state
.ls_status
!= DLADM_STATUS_OK
) {
4520 die_dlerr(state
.ls_status
,
4521 "failed to show physical link %s", argv
[optind
]);
4528 do_show_vlan(int argc
, char *argv
[], const char *use
)
4531 uint32_t flags
= DLADM_OPT_ACTIVE
;
4532 boolean_t p_arg
= B_FALSE
;
4533 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4535 dladm_status_t status
;
4536 boolean_t o_arg
= B_FALSE
;
4537 char *fields_str
= NULL
;
4539 ofmt_status_t oferr
;
4540 uint_t ofmtflags
= 0;
4542 bzero(&state
, sizeof (state
));
4545 while ((option
= getopt_long(argc
, argv
, ":pPo:",
4546 show_lopts
, NULL
)) != -1) {
4555 if (flags
!= DLADM_OPT_ACTIVE
)
4558 flags
= DLADM_OPT_PERSIST
;
4562 fields_str
= optarg
;
4565 die_opterr(optopt
, option
, use
);
4570 /* get link name (optional last argument) */
4571 if (optind
== (argc
-1)) {
4572 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
4573 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
4574 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
4576 } else if (optind
!= argc
) {
4580 state
.ls_parsable
= p_arg
;
4581 state
.ls_flags
= flags
;
4582 state
.ls_donefirst
= B_FALSE
;
4584 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0))
4587 if (state
.ls_parsable
)
4588 ofmtflags
|= OFMT_PARSABLE
;
4589 oferr
= ofmt_open(fields_str
, vlan_fields
, ofmtflags
, 0, &ofmt
);
4590 dladm_ofmt_check(oferr
, state
.ls_parsable
, ofmt
);
4591 state
.ls_ofmt
= ofmt
;
4593 if (linkid
== DATALINK_ALL_LINKID
) {
4594 (void) dladm_walk_datalink_id(show_vlan
, handle
, &state
,
4595 DATALINK_CLASS_VLAN
, DATALINK_ANY_MEDIATYPE
, flags
);
4597 (void) show_vlan(handle
, linkid
, &state
);
4598 if (state
.ls_status
!= DLADM_STATUS_OK
) {
4599 die_dlerr(state
.ls_status
, "failed to show vlan %s",
4607 do_create_vnic(int argc
, char *argv
[], const char *use
)
4609 datalink_id_t linkid
, dev_linkid
;
4610 char devname
[MAXLINKNAMELEN
];
4611 char name
[MAXLINKNAMELEN
];
4612 boolean_t l_arg
= B_FALSE
;
4613 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
4614 char *altroot
= NULL
;
4617 dladm_status_t status
;
4618 vnic_mac_addr_type_t mac_addr_type
= VNIC_MAC_ADDR_TYPE_UNKNOWN
;
4619 uchar_t
*mac_addr
= NULL
;
4621 uint_t maclen
= 0, mac_prefix_len
= 0;
4622 char propstr
[DLADM_STRSIZE
];
4623 dladm_arg_list_t
*proplist
= NULL
;
4626 vrid_t vrid
= VRRP_VRID_NONE
;
4629 bzero(propstr
, DLADM_STRSIZE
);
4631 while ((option
= getopt_long(argc
, argv
, ":tfR:l:m:n:p:r:v:V:A:H",
4632 vnic_lopts
, NULL
)) != -1) {
4635 flags
&= ~DLADM_OPT_PERSIST
;
4641 if (strlcpy(devname
, optarg
, MAXLINKNAMELEN
) >=
4643 die("link name too long");
4647 if (mac_addr_type
!= VNIC_MAC_ADDR_TYPE_UNKNOWN
)
4648 die("cannot specify -m option twice");
4650 if (strcmp(optarg
, "fixed") == 0) {
4652 * A fixed MAC address must be specified
4653 * by its value, not by the keyword 'fixed'.
4655 die("'fixed' is not a valid MAC address");
4657 if (dladm_vnic_str2macaddrtype(optarg
,
4658 &mac_addr_type
) != DLADM_STATUS_OK
) {
4659 mac_addr_type
= VNIC_MAC_ADDR_TYPE_FIXED
;
4660 /* MAC address specified by value */
4661 mac_addr
= _link_aton(optarg
, (int *)&maclen
);
4662 if (mac_addr
== NULL
) {
4663 if (maclen
== (uint_t
)-1)
4664 die("invalid MAC address");
4666 die("out of memory");
4672 mac_slot
= (int)strtol(optarg
, &endp
, 10);
4673 if (errno
!= 0 || *endp
!= '\0')
4674 die("invalid slot number");
4677 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
4678 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
4680 die("property list too long '%s'", propstr
);
4683 mac_addr
= _link_aton(optarg
, (int *)&mac_prefix_len
);
4684 if (mac_addr
== NULL
) {
4685 if (mac_prefix_len
== (uint_t
)-1)
4686 die("invalid MAC address");
4688 die("out of memory");
4692 if (!str2int(optarg
, (int *)&vrid
) ||
4693 vrid
< VRRP_VRID_MIN
|| vrid
> VRRP_VRID_MAX
) {
4694 die("invalid VRRP identifier '%s'", optarg
);
4699 if (strcmp(optarg
, "inet") == 0)
4701 else if (strcmp(optarg
, "inet6") == 0)
4704 die("invalid address family '%s'", optarg
);
4710 if (!str2int(optarg
, &vid
) || vid
< 1 || vid
> 4094)
4711 die("invalid VLAN identifier '%s'", optarg
);
4715 flags
|= DLADM_OPT_FORCE
;
4718 die_opterr(optopt
, option
, use
);
4722 if (mac_addr_type
== VNIC_MAC_ADDR_TYPE_UNKNOWN
)
4723 mac_addr_type
= VNIC_MAC_ADDR_TYPE_AUTO
;
4726 * 'f' - force, flag can be specified only with 'v' - vlan.
4728 if ((flags
& DLADM_OPT_FORCE
) != 0 && vid
== 0)
4729 die("-f option can only be used with -v");
4731 if (mac_prefix_len
!= 0 && mac_addr_type
!= VNIC_MAC_ADDR_TYPE_RANDOM
&&
4732 mac_addr_type
!= VNIC_MAC_ADDR_TYPE_FIXED
)
4735 if (mac_addr_type
== VNIC_MAC_ADDR_TYPE_VRID
) {
4736 if (vrid
== VRRP_VRID_NONE
|| af
== AF_UNSPEC
||
4737 mac_addr
!= NULL
|| maclen
!= 0 || mac_slot
!= -1 ||
4738 mac_prefix_len
!= 0) {
4741 } else if ((af
!= AF_UNSPEC
|| vrid
!= VRRP_VRID_NONE
)) {
4745 /* check required options */
4749 if (mac_slot
!= -1 && mac_addr_type
!= VNIC_MAC_ADDR_TYPE_FACTORY
)
4752 /* the VNIC id is the required operand */
4753 if (optind
!= (argc
- 1))
4756 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
4757 die("link name too long '%s'", argv
[optind
]);
4759 if (!dladm_valid_linkname(name
))
4760 die("invalid link name '%s'", argv
[optind
]);
4762 if (altroot
!= NULL
)
4763 altroot_cmd(altroot
, argc
, argv
);
4765 if (dladm_name2info(handle
, devname
, &dev_linkid
, NULL
, NULL
, NULL
) !=
4767 die("invalid link name '%s'", devname
);
4769 if (dladm_parse_link_props(propstr
, &proplist
, B_FALSE
)
4771 die("invalid vnic property");
4773 status
= dladm_vnic_create(handle
, name
, dev_linkid
, mac_addr_type
,
4774 mac_addr
, maclen
, &mac_slot
, mac_prefix_len
, vid
, vrid
, af
,
4775 &linkid
, proplist
, flags
);
4777 case DLADM_STATUS_OK
:
4780 case DLADM_STATUS_LINKBUSY
:
4781 die("VLAN over '%s' may not use default_tag ID "
4782 "(see dladm(1M))", devname
);
4786 die_dlerr(status
, "vnic creation over %s failed", devname
);
4789 dladm_free_props(proplist
);
4794 do_etherstub_check(const char *name
, datalink_id_t linkid
, boolean_t etherstub
,
4797 boolean_t is_etherstub
;
4798 dladm_vnic_attr_t attr
;
4800 if (dladm_vnic_info(handle
, linkid
, &attr
, flags
) != DLADM_STATUS_OK
) {
4802 * Let the delete continue anyway.
4806 is_etherstub
= (attr
.va_link_id
== DATALINK_INVALID_LINKID
);
4807 if (is_etherstub
!= etherstub
) {
4808 die("'%s' is not %s", name
,
4809 (is_etherstub
? "a vnic" : "an etherstub"));
4814 do_delete_vnic_common(int argc
, char *argv
[], const char *use
,
4815 boolean_t etherstub
)
4818 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
4819 datalink_id_t linkid
;
4820 char *altroot
= NULL
;
4821 dladm_status_t status
;
4824 while ((option
= getopt_long(argc
, argv
, ":R:t", lopts
,
4828 flags
&= ~DLADM_OPT_PERSIST
;
4834 die_opterr(optopt
, option
, use
);
4838 /* get vnic name (required last argument) */
4839 if (optind
!= (argc
- 1))
4842 if (altroot
!= NULL
)
4843 altroot_cmd(altroot
, argc
, argv
);
4845 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
4847 if (status
!= DLADM_STATUS_OK
)
4848 die("invalid link name '%s'", argv
[optind
]);
4850 if ((flags
& DLADM_OPT_ACTIVE
) != 0) {
4851 do_etherstub_check(argv
[optind
], linkid
, etherstub
,
4854 if ((flags
& DLADM_OPT_PERSIST
) != 0) {
4855 do_etherstub_check(argv
[optind
], linkid
, etherstub
,
4859 status
= dladm_vnic_delete(handle
, linkid
, flags
);
4860 if (status
!= DLADM_STATUS_OK
)
4861 die_dlerr(status
, "vnic deletion failed");
4865 do_delete_vnic(int argc
, char *argv
[], const char *use
)
4867 do_delete_vnic_common(argc
, argv
, use
, B_FALSE
);
4872 do_up_vnic_common(int argc
, char *argv
[], const char *use
, boolean_t vlan
)
4874 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
4875 dladm_status_t status
;
4878 type
= vlan
? "vlan" : "vnic";
4881 * get the id or the name of the vnic/vlan (optional last argument)
4884 status
= dladm_name2info(handle
, argv
[1], &linkid
, NULL
, NULL
,
4886 if (status
!= DLADM_STATUS_OK
)
4889 } else if (argc
> 2) {
4894 status
= dladm_vlan_up(handle
, linkid
);
4896 status
= dladm_vnic_up(handle
, linkid
, 0);
4899 if (status
!= DLADM_STATUS_OK
) {
4902 "could not bring up %s '%s'", type
, argv
[1]);
4904 die_dlerr(status
, "could not bring %ss up", type
);
4910 do_up_vnic(int argc
, char *argv
[], const char *use
)
4912 do_up_vnic_common(argc
, argv
, use
, B_FALSE
);
4916 dump_vnics_head(const char *dev
)
4919 (void) printf("%s", dev
);
4921 (void) printf("\tipackets rbytes opackets obytes ");
4924 (void) printf("%%ipkts %%opkts\n");
4926 (void) printf("\n");
4930 dump_vnic_stat(const char *name
, datalink_id_t vnic_id
,
4931 show_vnic_state_t
*state
, pktsum_t
*vnic_stats
, pktsum_t
*tot_stats
)
4933 pktsum_t diff_stats
;
4934 pktsum_t
*old_stats
= &state
->vs_prevstats
[vnic_id
];
4936 dladm_stats_diff(&diff_stats
, vnic_stats
, old_stats
);
4938 (void) printf("%s", name
);
4940 (void) printf("\t%-10llu", diff_stats
.ipackets
);
4941 (void) printf("%-12llu", diff_stats
.rbytes
);
4942 (void) printf("%-10llu", diff_stats
.opackets
);
4943 (void) printf("%-12llu", diff_stats
.obytes
);
4946 if (tot_stats
->ipackets
== 0) {
4947 (void) printf("\t-");
4949 (void) printf("\t%-6.1f", (double)diff_stats
.ipackets
/
4950 (double)tot_stats
->ipackets
* 100);
4952 if (tot_stats
->opackets
== 0) {
4953 (void) printf("\t-");
4955 (void) printf("\t%-6.1f", (double)diff_stats
.opackets
/
4956 (double)tot_stats
->opackets
* 100);
4959 (void) printf("\n");
4961 *old_stats
= *vnic_stats
;
4965 * Called from the walker dladm_vnic_walk_sys() for each vnic to display
4966 * vnic information or statistics.
4968 static dladm_status_t
4969 print_vnic(show_vnic_state_t
*state
, datalink_id_t linkid
)
4971 dladm_vnic_attr_t attr
, *vnic
= &attr
;
4972 dladm_status_t status
;
4973 boolean_t is_etherstub
;
4974 char devname
[MAXLINKNAMELEN
];
4975 char vnic_name
[MAXLINKNAMELEN
];
4976 char mstr
[MAXMACADDRLEN
* 3];
4977 vnic_fields_buf_t vbuf
;
4979 if ((status
= dladm_vnic_info(handle
, linkid
, vnic
, state
->vs_flags
)) !=
4983 is_etherstub
= (vnic
->va_link_id
== DATALINK_INVALID_LINKID
);
4984 if (state
->vs_etherstub
!= is_etherstub
) {
4986 * Want all etherstub but it's not one, or want
4987 * non-etherstub and it's one.
4989 return (DLADM_STATUS_OK
);
4992 if (state
->vs_link_id
!= DATALINK_ALL_LINKID
) {
4993 if (state
->vs_link_id
!= vnic
->va_link_id
)
4994 return (DLADM_STATUS_OK
);
4997 if (dladm_datalink_id2info(handle
, linkid
, NULL
, NULL
,
4998 NULL
, vnic_name
, sizeof (vnic_name
)) != DLADM_STATUS_OK
)
4999 return (DLADM_STATUS_BADARG
);
5001 bzero(devname
, sizeof (devname
));
5002 if (!is_etherstub
&&
5003 dladm_datalink_id2info(handle
, vnic
->va_link_id
, NULL
, NULL
,
5004 NULL
, devname
, sizeof (devname
)) != DLADM_STATUS_OK
)
5005 (void) sprintf(devname
, "?");
5007 state
->vs_found
= B_TRUE
;
5008 if (state
->vs_stats
) {
5009 /* print vnic statistics */
5010 pktsum_t vnic_stats
;
5012 if (state
->vs_firstonly
) {
5013 if (state
->vs_donefirst
)
5015 state
->vs_donefirst
= B_TRUE
;
5018 if (!state
->vs_printstats
) {
5020 * get vnic statistics and add to the sum for the
5023 get_link_stats(vnic_name
, &vnic_stats
);
5024 dladm_stats_total(&state
->vs_totalstats
, &vnic_stats
,
5025 &state
->vs_prevstats
[vnic
->va_vnic_id
]);
5027 /* get and print vnic statistics */
5028 get_link_stats(vnic_name
, &vnic_stats
);
5029 dump_vnic_stat(vnic_name
, linkid
, state
, &vnic_stats
,
5030 &state
->vs_totalstats
);
5032 return (DLADM_STATUS_OK
);
5034 (void) snprintf(vbuf
.vnic_link
, sizeof (vbuf
.vnic_link
),
5037 if (!is_etherstub
) {
5039 (void) snprintf(vbuf
.vnic_over
, sizeof (vbuf
.vnic_over
),
5041 (void) snprintf(vbuf
.vnic_speed
,
5042 sizeof (vbuf
.vnic_speed
), "%u",
5043 (uint_t
)((get_ifspeed(vnic_name
, B_TRUE
))
5046 switch (vnic
->va_mac_addr_type
) {
5047 case VNIC_MAC_ADDR_TYPE_FIXED
:
5048 case VNIC_MAC_ADDR_TYPE_PRIMARY
:
5049 (void) snprintf(vbuf
.vnic_macaddrtype
,
5050 sizeof (vbuf
.vnic_macaddrtype
),
5053 case VNIC_MAC_ADDR_TYPE_RANDOM
:
5054 (void) snprintf(vbuf
.vnic_macaddrtype
,
5055 sizeof (vbuf
.vnic_macaddrtype
),
5058 case VNIC_MAC_ADDR_TYPE_FACTORY
:
5059 (void) snprintf(vbuf
.vnic_macaddrtype
,
5060 sizeof (vbuf
.vnic_macaddrtype
),
5061 gettext("factory, slot %d"),
5064 case VNIC_MAC_ADDR_TYPE_VRID
:
5065 (void) snprintf(vbuf
.vnic_macaddrtype
,
5066 sizeof (vbuf
.vnic_macaddrtype
),
5067 gettext("vrrp, %d/%s"),
5068 vnic
->va_vrid
, vnic
->va_af
== AF_INET
?
5073 if (strlen(vbuf
.vnic_macaddrtype
) > 0) {
5074 (void) snprintf(vbuf
.vnic_macaddr
,
5075 sizeof (vbuf
.vnic_macaddr
), "%s",
5076 dladm_aggr_macaddr2str(vnic
->va_mac_addr
,
5080 (void) snprintf(vbuf
.vnic_vid
, sizeof (vbuf
.vnic_vid
),
5081 "%d", vnic
->va_vid
);
5084 ofmt_print(state
->vs_ofmt
, &vbuf
);
5086 return (DLADM_STATUS_OK
);
5092 show_vnic(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
5094 show_vnic_state_t
*state
= arg
;
5096 state
->vs_status
= print_vnic(state
, linkid
);
5097 return (DLADM_WALK_CONTINUE
);
5101 do_show_vnic_common(int argc
, char *argv
[], const char *use
,
5102 boolean_t etherstub
)
5105 boolean_t s_arg
= B_FALSE
;
5106 boolean_t i_arg
= B_FALSE
;
5107 boolean_t l_arg
= B_FALSE
;
5108 uint32_t interval
= 0, flags
= DLADM_OPT_ACTIVE
;
5109 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
5110 datalink_id_t dev_linkid
= DATALINK_ALL_LINKID
;
5111 show_vnic_state_t state
;
5112 dladm_status_t status
;
5113 boolean_t o_arg
= B_FALSE
;
5114 char *fields_str
= NULL
;
5115 const ofmt_field_t
*pf
;
5116 char *all_e_fields
= "link";
5118 ofmt_status_t oferr
;
5119 uint_t ofmtflags
= 0;
5121 bzero(&state
, sizeof (state
));
5123 while ((option
= getopt_long(argc
, argv
, ":pPl:si:o:", lopts
,
5127 state
.vs_parsable
= B_TRUE
;
5130 flags
= DLADM_OPT_PERSIST
;
5134 die("option not supported for this command");
5136 if (strlcpy(state
.vs_link
, optarg
, MAXLINKNAMELEN
) >=
5138 die("link name too long");
5144 die("the option -s cannot be specified "
5151 die("the option -i cannot be specified "
5155 if (!dladm_str2interval(optarg
, &interval
))
5156 die("invalid interval value '%s'", optarg
);
5160 fields_str
= optarg
;
5163 die_opterr(optopt
, option
, use
);
5167 if (i_arg
&& !s_arg
)
5168 die("the option -i can be used only with -s");
5170 /* get vnic ID (optional last argument) */
5171 if (optind
== (argc
- 1)) {
5172 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
5174 if (status
!= DLADM_STATUS_OK
) {
5175 die_dlerr(status
, "invalid vnic name '%s'",
5178 (void) strlcpy(state
.vs_vnic
, argv
[optind
], MAXLINKNAMELEN
);
5179 } else if (optind
!= argc
) {
5184 status
= dladm_name2info(handle
, state
.vs_link
, &dev_linkid
,
5186 if (status
!= DLADM_STATUS_OK
) {
5187 die_dlerr(status
, "invalid link name '%s'",
5192 state
.vs_vnic_id
= linkid
;
5193 state
.vs_link_id
= dev_linkid
;
5194 state
.vs_etherstub
= etherstub
;
5195 state
.vs_found
= B_FALSE
;
5196 state
.vs_flags
= flags
;
5198 if (!o_arg
|| (o_arg
&& strcasecmp(fields_str
, "all") == 0)) {
5200 fields_str
= all_e_fields
;
5204 if (state
.vs_parsable
)
5205 ofmtflags
|= OFMT_PARSABLE
;
5206 oferr
= ofmt_open(fields_str
, pf
, ofmtflags
, 0, &ofmt
);
5207 dladm_ofmt_check(oferr
, state
.vs_parsable
, ofmt
);
5208 state
.vs_ofmt
= ofmt
;
5211 /* Display vnic statistics */
5212 vnic_stats(&state
, interval
);
5217 /* Display vnic information */
5218 state
.vs_donefirst
= B_FALSE
;
5220 if (linkid
== DATALINK_ALL_LINKID
) {
5221 (void) dladm_walk_datalink_id(show_vnic
, handle
, &state
,
5222 DATALINK_CLASS_VNIC
| DATALINK_CLASS_ETHERSTUB
,
5223 DATALINK_ANY_MEDIATYPE
, flags
);
5225 (void) show_vnic(handle
, linkid
, &state
);
5226 if (state
.vs_status
!= DLADM_STATUS_OK
) {
5228 die_dlerr(state
.vs_status
, "failed to show vnic '%s'",
5236 do_show_vnic(int argc
, char *argv
[], const char *use
)
5238 do_show_vnic_common(argc
, argv
, use
, B_FALSE
);
5242 do_create_etherstub(int argc
, char *argv
[], const char *use
)
5245 char *altroot
= NULL
;
5247 dladm_status_t status
;
5248 char name
[MAXLINKNAMELEN
];
5249 uchar_t mac_addr
[ETHERADDRL
];
5252 bzero(mac_addr
, sizeof (mac_addr
));
5253 flags
= DLADM_OPT_ANCHOR
| DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5256 while ((option
= getopt_long(argc
, argv
, "tR:",
5257 etherstub_lopts
, NULL
)) != -1) {
5260 flags
&= ~DLADM_OPT_PERSIST
;
5266 die_opterr(optopt
, option
, use
);
5270 /* the etherstub id is the required operand */
5271 if (optind
!= (argc
- 1))
5274 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
5275 die("link name too long '%s'", argv
[optind
]);
5277 if (!dladm_valid_linkname(name
))
5278 die("invalid link name '%s'", argv
[optind
]);
5280 if (altroot
!= NULL
)
5281 altroot_cmd(altroot
, argc
, argv
);
5283 status
= dladm_vnic_create(handle
, name
, DATALINK_INVALID_LINKID
,
5284 VNIC_MAC_ADDR_TYPE_AUTO
, mac_addr
, ETHERADDRL
, NULL
, 0, 0,
5285 VRRP_VRID_NONE
, AF_UNSPEC
, NULL
, NULL
, flags
);
5286 if (status
!= DLADM_STATUS_OK
)
5287 die_dlerr(status
, "etherstub creation failed");
5291 do_delete_etherstub(int argc
, char *argv
[], const char *use
)
5293 do_delete_vnic_common(argc
, argv
, use
, B_TRUE
);
5298 do_show_etherstub(int argc
, char *argv
[], const char *use
)
5300 do_show_vnic_common(argc
, argv
, use
, B_TRUE
);
5305 do_up_simnet(int argc
, char *argv
[], const char *use
)
5307 (void) dladm_simnet_up(handle
, DATALINK_ALL_LINKID
, 0);
5311 do_create_simnet(int argc
, char *argv
[], const char *use
)
5314 char *altroot
= NULL
;
5316 uint32_t mtype
= DL_ETHER
;
5318 dladm_status_t status
;
5319 char name
[MAXLINKNAMELEN
];
5322 flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5325 while ((option
= getopt_long(argc
, argv
, ":tR:m:",
5326 simnet_lopts
, NULL
)) != -1) {
5329 flags
&= ~DLADM_OPT_PERSIST
;
5338 die_opterr(optopt
, option
, use
);
5342 /* the simnet id is the required operand */
5343 if (optind
!= (argc
- 1))
5346 if (strlcpy(name
, argv
[optind
], MAXLINKNAMELEN
) >= MAXLINKNAMELEN
)
5347 die("link name too long '%s'", argv
[optind
]);
5349 if (!dladm_valid_linkname(name
))
5350 die("invalid link name '%s'", name
);
5352 if (media
!= NULL
) {
5353 mtype
= dladm_str2media(media
);
5354 if (mtype
!= DL_ETHER
&& mtype
!= DL_WIFI
)
5355 die("media type '%s' is not supported", media
);
5358 if (altroot
!= NULL
)
5359 altroot_cmd(altroot
, argc
, argv
);
5361 status
= dladm_simnet_create(handle
, name
, mtype
, flags
);
5362 if (status
!= DLADM_STATUS_OK
)
5363 die_dlerr(status
, "simnet creation failed");
5367 do_delete_simnet(int argc
, char *argv
[], const char *use
)
5370 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5371 datalink_id_t linkid
;
5372 char *altroot
= NULL
;
5373 dladm_status_t status
;
5374 dladm_simnet_attr_t slinfo
;
5377 while ((option
= getopt_long(argc
, argv
, ":tR:", simnet_lopts
,
5381 flags
&= ~DLADM_OPT_PERSIST
;
5387 die_opterr(optopt
, option
, use
);
5391 /* get simnet name (required last argument) */
5392 if (optind
!= (argc
- 1))
5395 if (!dladm_valid_linkname(argv
[optind
]))
5396 die("invalid link name '%s'", argv
[optind
]);
5398 if (altroot
!= NULL
)
5399 altroot_cmd(altroot
, argc
, argv
);
5401 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
5403 if (status
!= DLADM_STATUS_OK
)
5404 die("simnet '%s' not found", argv
[optind
]);
5406 if ((status
= dladm_simnet_info(handle
, linkid
, &slinfo
,
5407 flags
)) != DLADM_STATUS_OK
)
5408 die_dlerr(status
, "failed to retrieve simnet information");
5410 status
= dladm_simnet_delete(handle
, linkid
, flags
);
5411 if (status
!= DLADM_STATUS_OK
)
5412 die_dlerr(status
, "simnet deletion failed");
5416 do_modify_simnet(int argc
, char *argv
[], const char *use
)
5419 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
5420 datalink_id_t linkid
;
5421 datalink_id_t peer_linkid
;
5422 char *altroot
= NULL
;
5423 dladm_status_t status
;
5424 boolean_t p_arg
= B_FALSE
;
5427 while ((option
= getopt_long(argc
, argv
, ":tR:p:", simnet_lopts
,
5431 flags
&= ~DLADM_OPT_PERSIST
;
5440 if (strcasecmp(optarg
, "none") == 0)
5441 peer_linkid
= DATALINK_INVALID_LINKID
;
5442 else if (dladm_name2info(handle
, optarg
, &peer_linkid
,
5443 NULL
, NULL
, NULL
) != DLADM_STATUS_OK
)
5444 die("invalid peer link name '%s'", optarg
);
5447 die_opterr(optopt
, option
, use
);
5451 /* get simnet name (required last argument) */
5452 if (optind
!= (argc
- 1))
5455 /* Nothing to do if no peer link argument */
5459 if (altroot
!= NULL
)
5460 altroot_cmd(altroot
, argc
, argv
);
5462 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
5464 if (status
!= DLADM_STATUS_OK
)
5465 die("invalid link name '%s'", argv
[optind
]);
5467 status
= dladm_simnet_modify(handle
, linkid
, peer_linkid
, flags
);
5468 if (status
!= DLADM_STATUS_OK
)
5469 die_dlerr(status
, "simnet modification failed");
5472 static dladm_status_t
5473 print_simnet(show_state_t
*state
, datalink_id_t linkid
)
5475 dladm_simnet_attr_t slinfo
;
5477 dladm_status_t status
;
5478 simnet_fields_buf_t slbuf
;
5479 char mstr
[ETHERADDRL
* 3];
5481 bzero(&slbuf
, sizeof (slbuf
));
5482 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, NULL
, NULL
,
5483 slbuf
.simnet_name
, sizeof (slbuf
.simnet_name
)))
5487 if (!(state
->ls_flags
& flags
))
5488 return (DLADM_STATUS_NOTFOUND
);
5490 if ((status
= dladm_simnet_info(handle
, linkid
, &slinfo
,
5491 state
->ls_flags
)) != DLADM_STATUS_OK
)
5494 if (slinfo
.sna_peer_link_id
!= DATALINK_INVALID_LINKID
&&
5495 (status
= dladm_datalink_id2info(handle
, slinfo
.sna_peer_link_id
,
5496 NULL
, NULL
, NULL
, slbuf
.simnet_otherlink
,
5497 sizeof (slbuf
.simnet_otherlink
))) !=
5501 if (slinfo
.sna_mac_len
> sizeof (slbuf
.simnet_macaddr
))
5502 return (DLADM_STATUS_BADVAL
);
5504 (void) strlcpy(slbuf
.simnet_macaddr
,
5505 dladm_aggr_macaddr2str(slinfo
.sna_mac_addr
, mstr
),
5506 sizeof (slbuf
.simnet_macaddr
));
5507 (void) dladm_media2str(slinfo
.sna_type
, slbuf
.simnet_media
);
5509 ofmt_print(state
->ls_ofmt
, &slbuf
);
5515 show_simnet(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
5517 show_state_t
*state
= arg
;
5519 state
->ls_status
= print_simnet(state
, linkid
);
5520 return (DLADM_WALK_CONTINUE
);
5524 do_show_simnet(int argc
, char *argv
[], const char *use
)
5527 uint32_t flags
= DLADM_OPT_ACTIVE
;
5528 boolean_t p_arg
= B_FALSE
;
5529 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
5531 dladm_status_t status
;
5532 boolean_t o_arg
= B_FALSE
;
5534 ofmt_status_t oferr
;
5535 char *all_fields
= "link,media,macaddress,otherlink";
5536 char *fields_str
= all_fields
;
5537 uint_t ofmtflags
= 0;
5539 bzero(&state
, sizeof (state
));
5542 while ((option
= getopt_long(argc
, argv
, ":pPo:",
5543 show_lopts
, NULL
)) != -1) {
5550 state
.ls_parsable
= p_arg
;
5553 if (flags
!= DLADM_OPT_ACTIVE
)
5556 flags
= DLADM_OPT_PERSIST
;
5560 fields_str
= optarg
;
5563 die_opterr(optopt
, option
, use
);
5568 if (p_arg
&& !o_arg
)
5569 die("-p requires -o");
5571 if (strcasecmp(fields_str
, "all") == 0) {
5573 die("\"-o all\" is invalid with -p");
5574 fields_str
= all_fields
;
5577 /* get link name (optional last argument) */
5578 if (optind
== (argc
-1)) {
5579 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
5580 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
5581 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
5583 } else if (optind
!= argc
) {
5587 state
.ls_flags
= flags
;
5588 state
.ls_donefirst
= B_FALSE
;
5589 if (state
.ls_parsable
)
5590 ofmtflags
|= OFMT_PARSABLE
;
5591 oferr
= ofmt_open(fields_str
, simnet_fields
, ofmtflags
, 0, &ofmt
);
5592 dladm_ofmt_check(oferr
, state
.ls_parsable
, ofmt
);
5593 state
.ls_ofmt
= ofmt
;
5595 if (linkid
== DATALINK_ALL_LINKID
) {
5596 (void) dladm_walk_datalink_id(show_simnet
, handle
, &state
,
5597 DATALINK_CLASS_SIMNET
, DATALINK_ANY_MEDIATYPE
, flags
);
5599 (void) show_simnet(handle
, linkid
, &state
);
5600 if (state
.ls_status
!= DLADM_STATUS_OK
) {
5602 die_dlerr(state
.ls_status
, "failed to show simnet %s",
5610 link_stats(datalink_id_t linkid
, uint_t interval
, char *fields_str
,
5611 show_state_t
*state
)
5614 ofmt_status_t oferr
;
5615 uint_t ofmtflags
= 0;
5617 if (state
->ls_parsable
)
5618 ofmtflags
|= OFMT_PARSABLE
;
5619 oferr
= ofmt_open(fields_str
, link_s_fields
, ofmtflags
, 0, &ofmt
);
5620 dladm_ofmt_check(oferr
, state
->ls_parsable
, ofmt
);
5621 state
->ls_ofmt
= ofmt
;
5624 * If an interval is specified, continuously show the stats
5625 * only for the first MAC port.
5627 state
->ls_firstonly
= (interval
!= 0);
5630 state
->ls_donefirst
= B_FALSE
;
5631 if (linkid
== DATALINK_ALL_LINKID
) {
5632 (void) dladm_walk_datalink_id(show_link_stats
, handle
,
5633 state
, DATALINK_CLASS_ALL
, DATALINK_ANY_MEDIATYPE
,
5636 (void) show_link_stats(handle
, linkid
, state
);
5642 (void) fflush(stdout
);
5643 (void) sleep(interval
);
5649 aggr_stats(datalink_id_t linkid
, show_grp_state_t
*state
, uint_t interval
)
5652 * If an interval is specified, continuously show the stats
5653 * only for the first group.
5655 state
->gs_firstonly
= (interval
!= 0);
5658 state
->gs_donefirst
= B_FALSE
;
5659 if (linkid
== DATALINK_ALL_LINKID
)
5660 (void) dladm_walk_datalink_id(show_aggr
, handle
, state
,
5661 DATALINK_CLASS_AGGR
, DATALINK_ANY_MEDIATYPE
,
5664 (void) show_aggr(handle
, linkid
, state
);
5669 (void) fflush(stdout
);
5670 (void) sleep(interval
);
5676 vnic_stats(show_vnic_state_t
*sp
, uint32_t interval
)
5678 show_vnic_state_t state
;
5679 boolean_t specific_link
, specific_dev
;
5681 /* Display vnic statistics */
5682 dump_vnics_head(sp
->vs_link
);
5684 bzero(&state
, sizeof (state
));
5685 state
.vs_stats
= B_TRUE
;
5686 state
.vs_vnic_id
= sp
->vs_vnic_id
;
5687 state
.vs_link_id
= sp
->vs_link_id
;
5690 * If an interval is specified, and a vnic ID is not specified,
5691 * continuously show the stats only for the first vnic.
5693 specific_link
= (sp
->vs_vnic_id
!= DATALINK_ALL_LINKID
);
5694 specific_dev
= (sp
->vs_link_id
!= DATALINK_ALL_LINKID
);
5697 /* Get stats for each vnic */
5698 state
.vs_found
= B_FALSE
;
5699 state
.vs_donefirst
= B_FALSE
;
5700 state
.vs_printstats
= B_FALSE
;
5701 state
.vs_flags
= DLADM_OPT_ACTIVE
;
5703 if (!specific_link
) {
5704 (void) dladm_walk_datalink_id(show_vnic
, handle
, &state
,
5705 DATALINK_CLASS_VNIC
, DATALINK_ANY_MEDIATYPE
,
5708 (void) show_vnic(handle
, sp
->vs_vnic_id
, &state
);
5709 if (state
.vs_status
!= DLADM_STATUS_OK
) {
5710 die_dlerr(state
.vs_status
,
5711 "failed to show vnic '%s'", sp
->vs_vnic
);
5715 if (specific_link
&& !state
.vs_found
)
5716 die("non-existent vnic '%s'", sp
->vs_vnic
);
5717 if (specific_dev
&& !state
.vs_found
)
5718 die("device %s has no vnics", sp
->vs_link
);
5721 if ((specific_link
| specific_dev
) && !interval
) {
5722 (void) printf("Total");
5723 (void) printf("\t%-10llu",
5724 state
.vs_totalstats
.ipackets
);
5725 (void) printf("%-12llu",
5726 state
.vs_totalstats
.rbytes
);
5727 (void) printf("%-10llu",
5728 state
.vs_totalstats
.opackets
);
5729 (void) printf("%-12llu\n",
5730 state
.vs_totalstats
.obytes
);
5733 /* Show stats for each vnic */
5734 state
.vs_donefirst
= B_FALSE
;
5735 state
.vs_printstats
= B_TRUE
;
5737 if (!specific_link
) {
5738 (void) dladm_walk_datalink_id(show_vnic
, handle
, &state
,
5739 DATALINK_CLASS_VNIC
, DATALINK_ANY_MEDIATYPE
,
5742 (void) show_vnic(handle
, sp
->vs_vnic_id
, &state
);
5743 if (state
.vs_status
!= DLADM_STATUS_OK
) {
5744 die_dlerr(state
.vs_status
,
5745 "failed to show vnic '%s'", sp
->vs_vnic
);
5752 (void) fflush(stdout
);
5753 (void) sleep(interval
);
5758 get_mac_stats(const char *dev
, pktsum_t
*stats
)
5762 char module
[DLPI_LINKNAME_MAX
];
5766 bzero(stats
, sizeof (*stats
));
5768 if (dlpi_parselink(dev
, module
, &instance
) != DLPI_SUCCESS
)
5771 if ((kcp
= kstat_open()) == NULL
) {
5772 warn("kstat open operation failed");
5776 ksp
= dladm_kstat_lookup(kcp
, module
, instance
, "mac", NULL
);
5778 dladm_get_stats(kcp
, ksp
, stats
);
5780 (void) kstat_close(kcp
);
5785 get_link_stats(const char *link
, pktsum_t
*stats
)
5790 bzero(stats
, sizeof (*stats
));
5792 if ((kcp
= kstat_open()) == NULL
) {
5793 warn("kstat_open operation failed");
5797 ksp
= dladm_kstat_lookup(kcp
, "link", 0, link
, NULL
);
5800 dladm_get_stats(kcp
, ksp
, stats
);
5802 (void) kstat_close(kcp
);
5806 query_kstat(char *module
, int instance
, const char *name
, const char *stat
,
5807 uint8_t type
, void *val
)
5812 if ((kcp
= kstat_open()) == NULL
) {
5813 warn("kstat open operation failed");
5817 if ((ksp
= kstat_lookup(kcp
, module
, instance
, (char *)name
)) == NULL
) {
5819 * The kstat query could fail if the underlying MAC
5820 * driver was already detached.
5825 if (kstat_read(kcp
, ksp
, NULL
) == -1) {
5826 warn("kstat read failed");
5830 if (dladm_kstat_value(ksp
, stat
, type
, val
) < 0)
5833 (void) kstat_close(kcp
);
5837 (void) kstat_close(kcp
);
5842 get_one_kstat(const char *name
, const char *stat
, uint8_t type
,
5843 void *val
, boolean_t islink
)
5845 char module
[DLPI_LINKNAME_MAX
];
5849 return (query_kstat("link", 0, name
, stat
, type
, val
));
5851 if (dlpi_parselink(name
, module
, &instance
) != DLPI_SUCCESS
)
5854 return (query_kstat(module
, instance
, "mac", stat
, type
, val
));
5859 get_ifspeed(const char *name
, boolean_t islink
)
5861 uint64_t ifspeed
= 0;
5863 (void) get_one_kstat(name
, "ifspeed", KSTAT_DATA_UINT64
,
5870 get_linkstate(const char *name
, boolean_t islink
, char *buf
)
5872 link_state_t linkstate
;
5874 if (get_one_kstat(name
, "link_state", KSTAT_DATA_UINT32
,
5875 &linkstate
, islink
) != 0) {
5876 (void) strlcpy(buf
, "?", DLADM_STRSIZE
);
5879 return (dladm_linkstate2str(linkstate
, buf
));
5883 get_linkduplex(const char *name
, boolean_t islink
, char *buf
)
5885 link_duplex_t linkduplex
;
5887 if (get_one_kstat(name
, "link_duplex", KSTAT_DATA_UINT32
,
5888 &linkduplex
, islink
) != 0) {
5889 (void) strlcpy(buf
, "unknown", DLADM_STRSIZE
);
5893 return (dladm_linkduplex2str(linkduplex
, buf
));
5897 parse_wifi_fields(char *str
, ofmt_handle_t
*ofmt
, uint_t cmdtype
,
5900 ofmt_field_t
*template, *of
;
5902 ofmt_status_t oferr
;
5904 if (cmdtype
== WIFI_CMD_SCAN
) {
5905 template = wifi_common_fields
;
5907 str
= def_scan_wifi_fields
;
5908 if (strcasecmp(str
, "all") == 0)
5909 str
= all_scan_wifi_fields
;
5910 fn
= print_wlan_attr_cb
;
5911 } else if (cmdtype
== WIFI_CMD_SHOW
) {
5912 bcopy(wifi_common_fields
, &wifi_show_fields
[2],
5913 sizeof (wifi_common_fields
));
5914 template = wifi_show_fields
;
5916 str
= def_show_wifi_fields
;
5917 if (strcasecmp(str
, "all") == 0)
5918 str
= all_show_wifi_fields
;
5919 fn
= print_link_attr_cb
;
5924 for (of
= template; of
->of_name
!= NULL
; of
++) {
5925 if (of
->of_cb
== NULL
)
5929 oferr
= ofmt_open(str
, template, (parsable
? OFMT_PARSABLE
: 0),
5931 dladm_ofmt_check(oferr
, parsable
, *ofmt
);
5935 typedef struct print_wifi_state
{
5937 boolean_t ws_parsable
;
5938 boolean_t ws_header
;
5939 ofmt_handle_t ws_ofmt
;
5940 } print_wifi_state_t
;
5942 typedef struct wlan_scan_args_s
{
5943 print_wifi_state_t
*ws_state
;
5948 print_wlan_attr_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
5950 wlan_scan_args_t
*w
= ofarg
->ofmt_cbarg
;
5951 print_wifi_state_t
*statep
= w
->ws_state
;
5952 dladm_wlan_attr_t
*attrp
= w
->ws_attr
;
5953 char tmpbuf
[DLADM_STRSIZE
];
5955 if (ofarg
->ofmt_id
== 0) {
5956 (void) strlcpy(buf
, (char *)statep
->ws_link
, bufsize
);
5960 if ((ofarg
->ofmt_id
& attrp
->wa_valid
) == 0)
5963 switch (ofarg
->ofmt_id
) {
5964 case DLADM_WLAN_ATTR_ESSID
:
5965 (void) dladm_wlan_essid2str(&attrp
->wa_essid
, tmpbuf
);
5967 case DLADM_WLAN_ATTR_BSSID
:
5968 (void) dladm_wlan_bssid2str(&attrp
->wa_bssid
, tmpbuf
);
5970 case DLADM_WLAN_ATTR_SECMODE
:
5971 (void) dladm_wlan_secmode2str(&attrp
->wa_secmode
, tmpbuf
);
5973 case DLADM_WLAN_ATTR_STRENGTH
:
5974 (void) dladm_wlan_strength2str(&attrp
->wa_strength
, tmpbuf
);
5976 case DLADM_WLAN_ATTR_MODE
:
5977 (void) dladm_wlan_mode2str(&attrp
->wa_mode
, tmpbuf
);
5979 case DLADM_WLAN_ATTR_SPEED
:
5980 (void) dladm_wlan_speed2str(&attrp
->wa_speed
, tmpbuf
);
5981 (void) strlcat(tmpbuf
, "Mb", sizeof (tmpbuf
));
5983 case DLADM_WLAN_ATTR_AUTH
:
5984 (void) dladm_wlan_auth2str(&attrp
->wa_auth
, tmpbuf
);
5986 case DLADM_WLAN_ATTR_BSSTYPE
:
5987 (void) dladm_wlan_bsstype2str(&attrp
->wa_bsstype
, tmpbuf
);
5990 (void) strlcpy(buf
, tmpbuf
, bufsize
);
5996 print_scan_results(void *arg
, dladm_wlan_attr_t
*attrp
)
5998 print_wifi_state_t
*statep
= arg
;
5999 wlan_scan_args_t warg
;
6001 bzero(&warg
, sizeof (warg
));
6002 warg
.ws_state
= statep
;
6003 warg
.ws_attr
= attrp
;
6004 ofmt_print(statep
->ws_ofmt
, &warg
);
6009 scan_wifi(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6011 print_wifi_state_t
*statep
= arg
;
6012 dladm_status_t status
;
6013 char link
[MAXLINKNAMELEN
];
6015 if ((status
= dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
, link
,
6016 sizeof (link
))) != DLADM_STATUS_OK
) {
6017 return (DLADM_WALK_CONTINUE
);
6020 statep
->ws_link
= link
;
6021 status
= dladm_wlan_scan(dh
, linkid
, statep
, print_scan_results
);
6022 if (status
!= DLADM_STATUS_OK
)
6023 die_dlerr(status
, "cannot scan link '%s'", statep
->ws_link
);
6025 return (DLADM_WALK_CONTINUE
);
6029 print_wifi_status_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
6031 static char tmpbuf
[DLADM_STRSIZE
];
6032 wlan_scan_args_t
*w
= ofarg
->ofmt_cbarg
;
6033 dladm_wlan_linkattr_t
*attrp
= w
->ws_attr
;
6035 if ((ofarg
->ofmt_id
& attrp
->la_valid
) != 0) {
6036 (void) dladm_wlan_linkstatus2str(&attrp
->la_status
, tmpbuf
);
6037 (void) strlcpy(buf
, tmpbuf
, bufsize
);
6043 print_link_attr_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
6045 wlan_scan_args_t
*w
= ofarg
->ofmt_cbarg
, w1
;
6046 print_wifi_state_t
*statep
= w
->ws_state
;
6047 dladm_wlan_linkattr_t
*attrp
= w
->ws_attr
;
6049 bzero(&w1
, sizeof (w1
));
6050 w1
.ws_state
= statep
;
6051 w1
.ws_attr
= &attrp
->la_wlan_attr
;
6052 ofarg
->ofmt_cbarg
= &w1
;
6053 return (print_wlan_attr_cb(ofarg
, buf
, bufsize
));
6057 show_wifi(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6059 print_wifi_state_t
*statep
= arg
;
6060 dladm_wlan_linkattr_t attr
;
6061 dladm_status_t status
;
6062 char link
[MAXLINKNAMELEN
];
6063 wlan_scan_args_t warg
;
6065 if ((status
= dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
, link
,
6066 sizeof (link
))) != DLADM_STATUS_OK
) {
6067 return (DLADM_WALK_CONTINUE
);
6070 /* dladm_wlan_get_linkattr() memsets attr with 0 */
6071 status
= dladm_wlan_get_linkattr(dh
, linkid
, &attr
);
6072 if (status
!= DLADM_STATUS_OK
)
6073 die_dlerr(status
, "cannot get link attributes for %s", link
);
6075 statep
->ws_link
= link
;
6077 bzero(&warg
, sizeof (warg
));
6078 warg
.ws_state
= statep
;
6079 warg
.ws_attr
= &attr
;
6080 ofmt_print(statep
->ws_ofmt
, &warg
);
6081 return (DLADM_WALK_CONTINUE
);
6085 do_display_wifi(int argc
, char **argv
, int cmd
, const char *use
)
6088 char *fields_str
= NULL
;
6089 int (*callback
)(dladm_handle_t
, datalink_id_t
, void *);
6090 print_wifi_state_t state
;
6091 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6092 dladm_status_t status
;
6094 if (cmd
== WIFI_CMD_SCAN
)
6095 callback
= scan_wifi
;
6096 else if (cmd
== WIFI_CMD_SHOW
)
6097 callback
= show_wifi
;
6101 state
.ws_parsable
= B_FALSE
;
6102 state
.ws_header
= B_TRUE
;
6104 while ((option
= getopt_long(argc
, argv
, ":o:p",
6105 wifi_longopts
, NULL
)) != -1) {
6108 fields_str
= optarg
;
6111 state
.ws_parsable
= B_TRUE
;
6114 die_opterr(optopt
, option
, use
);
6118 if (state
.ws_parsable
&& fields_str
== NULL
)
6119 die("-p requires -o");
6121 if (state
.ws_parsable
&& strcasecmp(fields_str
, "all") == 0)
6122 die("\"-o all\" is invalid with -p");
6124 if (optind
== (argc
- 1)) {
6125 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6126 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6127 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6129 } else if (optind
!= argc
) {
6133 if (parse_wifi_fields(fields_str
, &state
.ws_ofmt
, cmd
,
6134 state
.ws_parsable
) < 0)
6135 die("invalid field(s) specified");
6137 if (linkid
== DATALINK_ALL_LINKID
) {
6138 (void) dladm_walk_datalink_id(callback
, handle
, &state
,
6139 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6140 DL_WIFI
, DLADM_OPT_ACTIVE
);
6142 (void) (*callback
)(handle
, linkid
, &state
);
6144 ofmt_close(state
.ws_ofmt
);
6148 do_scan_wifi(int argc
, char **argv
, const char *use
)
6150 do_display_wifi(argc
, argv
, WIFI_CMD_SCAN
, use
);
6154 do_show_wifi(int argc
, char **argv
, const char *use
)
6156 do_display_wifi(argc
, argv
, WIFI_CMD_SHOW
, use
);
6159 typedef struct wlan_count_attr
{
6161 datalink_id_t wc_linkid
;
6162 } wlan_count_attr_t
;
6166 do_count_wlan(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6168 wlan_count_attr_t
*cp
= arg
;
6170 if (cp
->wc_count
== 0)
6171 cp
->wc_linkid
= linkid
;
6173 return (DLADM_WALK_CONTINUE
);
6177 parse_wlan_keys(char *str
, dladm_wlan_key_t
**keys
, uint_t
*key_countp
)
6180 dladm_wlan_key_t
*wk
;
6182 char *field
, *token
, *lasts
= NULL
, c
;
6185 while ((c
= *token
++) != NULL
) {
6189 token
= strdup(str
);
6193 wk
= malloc(nfields
* sizeof (dladm_wlan_key_t
));
6198 for (i
= 0; i
< nfields
; i
++) {
6200 dladm_secobj_class_t
class;
6201 dladm_status_t status
;
6203 field
= strtok_r(token
, ",", &lasts
);
6206 (void) strlcpy(wk
[i
].wk_name
, field
,
6207 DLADM_WLAN_MAX_KEYNAME_LEN
);
6210 if ((s
= strrchr(wk
[i
].wk_name
, ':')) != NULL
) {
6211 if (s
[1] == '\0' || s
[2] != '\0' || !isdigit(s
[1]))
6214 wk
[i
].wk_idx
= (uint_t
)(s
[1] - '0');
6217 wk
[i
].wk_len
= DLADM_WLAN_MAX_KEY_LEN
;
6219 status
= dladm_get_secobj(handle
, wk
[i
].wk_name
, &class,
6220 wk
[i
].wk_val
, &wk
[i
].wk_len
, 0);
6221 if (status
!= DLADM_STATUS_OK
) {
6222 if (status
== DLADM_STATUS_NOTFOUND
) {
6223 status
= dladm_get_secobj(handle
, wk
[i
].wk_name
,
6224 &class, wk
[i
].wk_val
, &wk
[i
].wk_len
,
6227 if (status
!= DLADM_STATUS_OK
)
6230 wk
[i
].wk_class
= class;
6243 do_connect_wifi(int argc
, char **argv
, const char *use
)
6246 dladm_wlan_attr_t attr
, *attrp
;
6247 dladm_status_t status
= DLADM_STATUS_OK
;
6248 int timeout
= DLADM_WLAN_CONNECT_TIMEOUT_DEFAULT
;
6249 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6250 dladm_wlan_key_t
*keys
= NULL
;
6251 uint_t key_count
= 0;
6253 dladm_wlan_secmode_t keysecmode
= DLADM_WLAN_SECMODE_NONE
;
6254 char buf
[DLADM_STRSIZE
];
6257 (void) memset(&attr
, 0, sizeof (attr
));
6258 while ((option
= getopt_long(argc
, argv
, ":e:i:a:m:b:s:k:T:c",
6259 wifi_longopts
, NULL
)) != -1) {
6262 status
= dladm_wlan_str2essid(optarg
, &attr
.wa_essid
);
6263 if (status
!= DLADM_STATUS_OK
)
6264 die("invalid ESSID '%s'", optarg
);
6266 attr
.wa_valid
|= DLADM_WLAN_ATTR_ESSID
;
6268 * Try to connect without doing a scan.
6270 flags
|= DLADM_WLAN_CONNECT_NOSCAN
;
6273 status
= dladm_wlan_str2bssid(optarg
, &attr
.wa_bssid
);
6274 if (status
!= DLADM_STATUS_OK
)
6275 die("invalid BSSID %s", optarg
);
6277 attr
.wa_valid
|= DLADM_WLAN_ATTR_BSSID
;
6280 status
= dladm_wlan_str2auth(optarg
, &attr
.wa_auth
);
6281 if (status
!= DLADM_STATUS_OK
)
6282 die("invalid authentication mode '%s'", optarg
);
6284 attr
.wa_valid
|= DLADM_WLAN_ATTR_AUTH
;
6287 status
= dladm_wlan_str2mode(optarg
, &attr
.wa_mode
);
6288 if (status
!= DLADM_STATUS_OK
)
6289 die("invalid mode '%s'", optarg
);
6291 attr
.wa_valid
|= DLADM_WLAN_ATTR_MODE
;
6294 if ((status
= dladm_wlan_str2bsstype(optarg
,
6295 &attr
.wa_bsstype
)) != DLADM_STATUS_OK
) {
6296 die("invalid bsstype '%s'", optarg
);
6299 attr
.wa_valid
|= DLADM_WLAN_ATTR_BSSTYPE
;
6302 if ((status
= dladm_wlan_str2secmode(optarg
,
6303 &attr
.wa_secmode
)) != DLADM_STATUS_OK
) {
6304 die("invalid security mode '%s'", optarg
);
6307 attr
.wa_valid
|= DLADM_WLAN_ATTR_SECMODE
;
6310 if (parse_wlan_keys(optarg
, &keys
, &key_count
) < 0)
6311 die("invalid key(s) '%s'", optarg
);
6313 if (keys
[0].wk_class
== DLADM_SECOBJ_CLASS_WEP
)
6314 keysecmode
= DLADM_WLAN_SECMODE_WEP
;
6316 keysecmode
= DLADM_WLAN_SECMODE_WPA
;
6319 if (strcasecmp(optarg
, "forever") == 0) {
6323 if (!str2int(optarg
, &timeout
) || timeout
< 0)
6324 die("invalid timeout value '%s'", optarg
);
6327 flags
|= DLADM_WLAN_CONNECT_CREATEIBSS
;
6328 flags
|= DLADM_WLAN_CONNECT_CREATEIBSS
;
6331 die_opterr(optopt
, option
, use
);
6336 if (keysecmode
== DLADM_WLAN_SECMODE_NONE
) {
6337 if ((attr
.wa_valid
& DLADM_WLAN_ATTR_SECMODE
) != 0) {
6338 die("key required for security mode '%s'",
6339 dladm_wlan_secmode2str(&attr
.wa_secmode
, buf
));
6342 if ((attr
.wa_valid
& DLADM_WLAN_ATTR_SECMODE
) != 0 &&
6343 attr
.wa_secmode
!= keysecmode
)
6344 die("incompatible -s and -k options");
6345 attr
.wa_valid
|= DLADM_WLAN_ATTR_SECMODE
;
6346 attr
.wa_secmode
= keysecmode
;
6349 if (optind
== (argc
- 1)) {
6350 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6351 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6352 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6354 } else if (optind
!= argc
) {
6358 if (linkid
== DATALINK_ALL_LINKID
) {
6359 wlan_count_attr_t wcattr
;
6361 wcattr
.wc_linkid
= DATALINK_INVALID_LINKID
;
6362 wcattr
.wc_count
= 0;
6363 (void) dladm_walk_datalink_id(do_count_wlan
, handle
, &wcattr
,
6364 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6365 DL_WIFI
, DLADM_OPT_ACTIVE
);
6366 if (wcattr
.wc_count
== 0) {
6367 die("no wifi links are available");
6368 } else if (wcattr
.wc_count
> 1) {
6369 die("link name is required when more than one wifi "
6370 "link is available");
6372 linkid
= wcattr
.wc_linkid
;
6374 attrp
= (attr
.wa_valid
== 0) ? NULL
: &attr
;
6376 if ((status
= dladm_wlan_connect(handle
, linkid
, attrp
, timeout
, keys
,
6377 key_count
, flags
)) != DLADM_STATUS_OK
) {
6378 if ((flags
& DLADM_WLAN_CONNECT_NOSCAN
) != 0) {
6380 * Try again with scanning and filtering.
6382 flags
&= ~DLADM_WLAN_CONNECT_NOSCAN
;
6386 if (status
== DLADM_STATUS_NOTFOUND
) {
6387 if (attr
.wa_valid
== 0) {
6388 die("no wifi networks are available");
6390 die("no wifi networks with the specified "
6391 "criteria are available");
6394 die_dlerr(status
, "cannot connect");
6401 do_all_disconnect_wifi(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
6403 dladm_status_t status
;
6405 status
= dladm_wlan_disconnect(dh
, linkid
);
6406 if (status
!= DLADM_STATUS_OK
)
6407 warn_dlerr(status
, "cannot disconnect link");
6409 return (DLADM_WALK_CONTINUE
);
6413 do_disconnect_wifi(int argc
, char **argv
, const char *use
)
6416 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6417 boolean_t all_links
= B_FALSE
;
6418 dladm_status_t status
;
6419 wlan_count_attr_t wcattr
;
6422 while ((option
= getopt_long(argc
, argv
, ":a",
6423 wifi_longopts
, NULL
)) != -1) {
6429 die_opterr(optopt
, option
, use
);
6434 if (optind
== (argc
- 1)) {
6435 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6436 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6437 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6439 } else if (optind
!= argc
) {
6443 if (linkid
== DATALINK_ALL_LINKID
) {
6445 wcattr
.wc_linkid
= linkid
;
6446 wcattr
.wc_count
= 0;
6447 (void) dladm_walk_datalink_id(do_count_wlan
, handle
,
6449 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6450 DL_WIFI
, DLADM_OPT_ACTIVE
);
6451 if (wcattr
.wc_count
== 0) {
6452 die("no wifi links are available");
6453 } else if (wcattr
.wc_count
> 1) {
6454 die("link name is required when more than "
6455 "one wifi link is available");
6457 linkid
= wcattr
.wc_linkid
;
6459 (void) dladm_walk_datalink_id(do_all_disconnect_wifi
,
6461 DATALINK_CLASS_PHYS
| DATALINK_CLASS_SIMNET
,
6462 DL_WIFI
, DLADM_OPT_ACTIVE
);
6466 status
= dladm_wlan_disconnect(handle
, linkid
);
6467 if (status
!= DLADM_STATUS_OK
)
6468 die_dlerr(status
, "cannot disconnect");
6472 print_linkprop(datalink_id_t linkid
, show_linkprop_state_t
*statep
,
6473 const char *propname
, dladm_prop_type_t type
, const char *format
,
6478 char buf
[DLADM_STRSIZE
];
6479 char *unknown
= "--", *notsup
= "";
6480 char **propvals
= statep
->ls_propvals
;
6481 uint_t valcnt
= DLADM_MAX_PROP_VALCNT
;
6482 dladm_status_t status
;
6484 status
= dladm_get_linkprop(handle
, linkid
, type
, propname
, propvals
,
6486 if (status
!= DLADM_STATUS_OK
) {
6487 if (status
== DLADM_STATUS_TEMPONLY
) {
6488 if (type
== DLADM_PROP_VAL_MODIFIABLE
&&
6489 statep
->ls_persist
) {
6491 propvals
= &unknown
;
6493 statep
->ls_status
= status
;
6494 statep
->ls_retstatus
= status
;
6497 } else if (status
== DLADM_STATUS_NOTSUP
||
6498 statep
->ls_persist
) {
6500 if (type
== DLADM_PROP_VAL_CURRENT
||
6501 type
== DLADM_PROP_VAL_PERM
)
6502 propvals
= &unknown
;
6505 } else if (status
== DLADM_STATUS_NOTDEFINED
) {
6506 propvals
= ¬sup
; /* STR_UNDEF_VAL */
6508 if (statep
->ls_proplist
&&
6509 statep
->ls_status
== DLADM_STATUS_OK
) {
6511 "cannot get link property '%s' for %s",
6512 propname
, statep
->ls_link
);
6514 statep
->ls_status
= status
;
6515 statep
->ls_retstatus
= status
;
6520 statep
->ls_status
= DLADM_STATUS_OK
;
6524 lim
= buf
+ DLADM_STRSIZE
;
6525 for (i
= 0; i
< valcnt
; i
++) {
6526 if (propvals
[i
][0] == '\0' && !statep
->ls_parsable
)
6527 ptr
+= snprintf(ptr
, lim
- ptr
, "--,");
6529 ptr
+= snprintf(ptr
, lim
- ptr
, "%s,", propvals
[i
]);
6534 buf
[strlen(buf
) - 1] = '\0';
6536 lim
= statep
->ls_line
+ MAX_PROP_LINE
;
6537 if (statep
->ls_parsable
) {
6538 *pptr
+= snprintf(*pptr
, lim
- *pptr
,
6541 *pptr
+= snprintf(*pptr
, lim
- *pptr
, format
, buf
);
6546 print_linkprop_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
6548 linkprop_args_t
*arg
= ofarg
->ofmt_cbarg
;
6549 char *propname
= arg
->ls_propname
;
6550 show_linkprop_state_t
*statep
= arg
->ls_state
;
6551 char *ptr
= statep
->ls_line
;
6552 char *lim
= ptr
+ MAX_PROP_LINE
;
6553 datalink_id_t linkid
= arg
->ls_linkid
;
6555 switch (ofarg
->ofmt_id
) {
6557 (void) snprintf(ptr
, lim
- ptr
, "%s", statep
->ls_link
);
6559 case LINKPROP_PROPERTY
:
6560 (void) snprintf(ptr
, lim
- ptr
, "%s", propname
);
6562 case LINKPROP_VALUE
:
6563 print_linkprop(linkid
, statep
, propname
,
6564 statep
->ls_persist
? DLADM_PROP_VAL_PERSISTENT
:
6565 DLADM_PROP_VAL_CURRENT
, "%s", &ptr
);
6567 * If we failed to query the link property, for example, query
6568 * the persistent value of a non-persistable link property,
6569 * simply skip the output.
6571 if (statep
->ls_status
!= DLADM_STATUS_OK
) {
6573 * Ignore the temponly error when we skip printing
6574 * link properties to avoid returning failure on exit.
6576 if (statep
->ls_retstatus
== DLADM_STATUS_TEMPONLY
)
6577 statep
->ls_retstatus
= DLADM_STATUS_OK
;
6580 ptr
= statep
->ls_line
;
6583 print_linkprop(linkid
, statep
, propname
,
6584 DLADM_PROP_VAL_PERM
, "%s", &ptr
);
6585 if (statep
->ls_status
!= DLADM_STATUS_OK
)
6587 ptr
= statep
->ls_line
;
6589 case LINKPROP_DEFAULT
:
6590 print_linkprop(linkid
, statep
, propname
,
6591 DLADM_PROP_VAL_DEFAULT
, "%s", &ptr
);
6592 if (statep
->ls_status
!= DLADM_STATUS_OK
)
6594 ptr
= statep
->ls_line
;
6596 case LINKPROP_POSSIBLE
:
6597 print_linkprop(linkid
, statep
, propname
,
6598 DLADM_PROP_VAL_MODIFIABLE
, "%s ", &ptr
);
6599 if (statep
->ls_status
!= DLADM_STATUS_OK
)
6601 ptr
= statep
->ls_line
;
6604 die("invalid input");
6607 (void) strlcpy(buf
, ptr
, bufsize
);
6610 return ((statep
->ls_status
== DLADM_STATUS_OK
) ?
6615 linkprop_is_supported(datalink_id_t linkid
, const char *propname
,
6616 show_linkprop_state_t
*statep
)
6618 dladm_status_t status
;
6619 uint_t valcnt
= DLADM_MAX_PROP_VALCNT
;
6621 /* if used with -p flag, always print output */
6622 if (statep
->ls_proplist
!= NULL
)
6625 status
= dladm_get_linkprop(handle
, linkid
, DLADM_PROP_VAL_DEFAULT
,
6626 propname
, statep
->ls_propvals
, &valcnt
);
6628 if (status
== DLADM_STATUS_OK
)
6632 * A system wide default value is not available for the
6633 * property. Check if current value can be retrieved.
6635 status
= dladm_get_linkprop(handle
, linkid
, DLADM_PROP_VAL_CURRENT
,
6636 propname
, statep
->ls_propvals
, &valcnt
);
6638 return (status
== DLADM_STATUS_OK
);
6643 show_linkprop(dladm_handle_t dh
, datalink_id_t linkid
, const char *propname
,
6646 show_linkprop_state_t
*statep
= arg
;
6647 linkprop_args_t ls_arg
;
6649 bzero(&ls_arg
, sizeof (ls_arg
));
6650 ls_arg
.ls_state
= statep
;
6651 ls_arg
.ls_propname
= (char *)propname
;
6652 ls_arg
.ls_linkid
= linkid
;
6655 * This will need to be fixed when kernel interfaces are added
6656 * to enable walking of all known private properties. For now,
6657 * we are limited to walking persistent private properties only.
6659 if ((propname
[0] == '_') && !statep
->ls_persist
&&
6660 (statep
->ls_proplist
== NULL
))
6661 return (DLADM_WALK_CONTINUE
);
6662 if (!statep
->ls_parsable
&&
6663 !linkprop_is_supported(linkid
, propname
, statep
))
6664 return (DLADM_WALK_CONTINUE
);
6666 ofmt_print(statep
->ls_ofmt
, &ls_arg
);
6668 return (DLADM_WALK_CONTINUE
);
6672 do_show_linkprop(int argc
, char **argv
, const char *use
)
6675 char propstr
[DLADM_STRSIZE
];
6676 dladm_arg_list_t
*proplist
= NULL
;
6677 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
6678 show_linkprop_state_t state
;
6679 uint32_t flags
= DLADM_OPT_ACTIVE
;
6680 dladm_status_t status
;
6681 char *fields_str
= NULL
;
6683 ofmt_status_t oferr
;
6684 uint_t ofmtflags
= 0;
6686 bzero(propstr
, DLADM_STRSIZE
);
6688 state
.ls_propvals
= NULL
;
6689 state
.ls_line
= NULL
;
6690 state
.ls_parsable
= B_FALSE
;
6691 state
.ls_persist
= B_FALSE
;
6692 state
.ls_header
= B_TRUE
;
6693 state
.ls_retstatus
= DLADM_STATUS_OK
;
6695 while ((option
= getopt_long(argc
, argv
, ":p:cPo:",
6696 prop_longopts
, NULL
)) != -1) {
6699 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
6700 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
6702 die("property list too long '%s'", propstr
);
6705 state
.ls_parsable
= B_TRUE
;
6708 state
.ls_persist
= B_TRUE
;
6709 flags
= DLADM_OPT_PERSIST
;
6712 fields_str
= optarg
;
6715 die_opterr(optopt
, option
, use
);
6720 if (optind
== (argc
- 1)) {
6721 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
6722 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
) {
6723 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6725 } else if (optind
!= argc
) {
6729 if (dladm_parse_link_props(propstr
, &proplist
, B_TRUE
)
6731 die("invalid link properties specified");
6732 state
.ls_proplist
= proplist
;
6733 state
.ls_status
= DLADM_STATUS_OK
;
6735 if (state
.ls_parsable
)
6736 ofmtflags
|= OFMT_PARSABLE
;
6738 ofmtflags
|= OFMT_WRAP
;
6740 oferr
= ofmt_open(fields_str
, linkprop_fields
, ofmtflags
, 0, &ofmt
);
6741 dladm_ofmt_check(oferr
, state
.ls_parsable
, ofmt
);
6742 state
.ls_ofmt
= ofmt
;
6744 if (linkid
== DATALINK_ALL_LINKID
) {
6745 (void) dladm_walk_datalink_id(show_linkprop_onelink
, handle
,
6746 &state
, DATALINK_CLASS_ALL
, DATALINK_ANY_MEDIATYPE
, flags
);
6748 (void) show_linkprop_onelink(handle
, linkid
, &state
);
6751 dladm_free_props(proplist
);
6753 if (state
.ls_retstatus
!= DLADM_STATUS_OK
) {
6754 dladm_close(handle
);
6760 show_linkprop_onelink(dladm_handle_t hdl
, datalink_id_t linkid
, void *arg
)
6765 dladm_arg_list_t
*proplist
= NULL
;
6766 show_linkprop_state_t
*statep
= arg
;
6767 dlpi_handle_t dh
= NULL
;
6769 statep
->ls_status
= DLADM_STATUS_OK
;
6771 if (dladm_datalink_id2info(hdl
, linkid
, &flags
, NULL
, NULL
,
6772 statep
->ls_link
, MAXLINKNAMELEN
) != DLADM_STATUS_OK
) {
6773 statep
->ls_status
= DLADM_STATUS_NOTFOUND
;
6774 return (DLADM_WALK_CONTINUE
);
6777 if ((statep
->ls_persist
&& !(flags
& DLADM_OPT_PERSIST
)) ||
6778 (!statep
->ls_persist
&& !(flags
& DLADM_OPT_ACTIVE
))) {
6779 statep
->ls_status
= DLADM_STATUS_BADARG
;
6780 return (DLADM_WALK_CONTINUE
);
6783 proplist
= statep
->ls_proplist
;
6786 * When some WiFi links are opened for the first time, their hardware
6787 * automatically scans for APs and does other slow operations. Thus,
6788 * if there are no open links, the retrieval of link properties
6789 * (below) will proceed slowly unless we hold the link open.
6791 * Note that failure of dlpi_open() does not necessarily mean invalid
6792 * link properties, because dlpi_open() may fail because of incorrect
6793 * autopush configuration. Therefore, we ingore the return value of
6796 if (!statep
->ls_persist
)
6797 (void) dlpi_open(statep
->ls_link
, &dh
, 0);
6799 buf
= malloc((sizeof (char *) + DLADM_PROP_VAL_MAX
) *
6800 DLADM_MAX_PROP_VALCNT
+ MAX_PROP_LINE
);
6802 die("insufficient memory");
6804 statep
->ls_propvals
= (char **)(void *)buf
;
6805 for (i
= 0; i
< DLADM_MAX_PROP_VALCNT
; i
++) {
6806 statep
->ls_propvals
[i
] = buf
+
6807 sizeof (char *) * DLADM_MAX_PROP_VALCNT
+
6808 i
* DLADM_PROP_VAL_MAX
;
6810 statep
->ls_line
= buf
+
6811 (sizeof (char *) + DLADM_PROP_VAL_MAX
) * DLADM_MAX_PROP_VALCNT
;
6813 if (proplist
!= NULL
) {
6814 for (i
= 0; i
< proplist
->al_count
; i
++) {
6815 (void) show_linkprop(hdl
, linkid
,
6816 proplist
->al_info
[i
].ai_name
, statep
);
6819 (void) dladm_walk_linkprop(hdl
, linkid
, statep
,
6825 return (DLADM_WALK_CONTINUE
);
6829 reset_one_linkprop(dladm_handle_t dh
, datalink_id_t linkid
,
6830 const char *propname
, void *arg
)
6832 set_linkprop_state_t
*statep
= arg
;
6833 dladm_status_t status
;
6835 status
= dladm_set_linkprop(dh
, linkid
, propname
, NULL
, 0,
6836 DLADM_OPT_ACTIVE
| (statep
->ls_temp
? 0 : DLADM_OPT_PERSIST
));
6837 if (status
!= DLADM_STATUS_OK
&&
6838 status
!= DLADM_STATUS_PROPRDONLY
&&
6839 status
!= DLADM_STATUS_NOTSUP
) {
6840 warn_dlerr(status
, "cannot reset link property '%s' on '%s'",
6841 propname
, statep
->ls_name
);
6842 statep
->ls_status
= status
;
6845 return (DLADM_WALK_CONTINUE
);
6849 set_linkprop(int argc
, char **argv
, boolean_t reset
, const char *use
)
6852 char errmsg
[DLADM_STRSIZE
];
6853 char *altroot
= NULL
;
6854 datalink_id_t linkid
;
6855 boolean_t temp
= B_FALSE
;
6856 dladm_status_t status
= DLADM_STATUS_OK
;
6857 char propstr
[DLADM_STRSIZE
];
6858 dladm_arg_list_t
*proplist
= NULL
;
6861 bzero(propstr
, DLADM_STRSIZE
);
6863 while ((option
= getopt_long(argc
, argv
, ":p:R:t",
6864 prop_longopts
, NULL
)) != -1) {
6867 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
6868 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
6870 die("property list too long '%s'", propstr
);
6879 die_opterr(optopt
, option
, use
);
6884 /* get link name (required last argument) */
6885 if (optind
!= (argc
- 1))
6888 if (dladm_parse_link_props(propstr
, &proplist
, reset
) !=
6890 die("invalid link properties specified");
6892 if (proplist
== NULL
&& !reset
)
6893 die("link property must be specified");
6895 if (altroot
!= NULL
) {
6896 dladm_free_props(proplist
);
6897 altroot_cmd(altroot
, argc
, argv
);
6900 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
, NULL
,
6902 if (status
!= DLADM_STATUS_OK
)
6903 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
6905 if (proplist
== NULL
) {
6906 set_linkprop_state_t state
;
6908 state
.ls_name
= argv
[optind
];
6909 state
.ls_reset
= reset
;
6910 state
.ls_temp
= temp
;
6911 state
.ls_status
= DLADM_STATUS_OK
;
6913 (void) dladm_walk_linkprop(handle
, linkid
, &state
,
6914 reset_one_linkprop
);
6916 status
= state
.ls_status
;
6920 for (i
= 0; i
< proplist
->al_count
; i
++) {
6921 dladm_arg_info_t
*aip
= &proplist
->al_info
[i
];
6930 count
= aip
->ai_count
;
6932 warn("no value specified for '%s'",
6934 status
= DLADM_STATUS_BADARG
;
6938 status
= dladm_set_linkprop(handle
, linkid
, aip
->ai_name
, val
,
6939 count
, DLADM_OPT_ACTIVE
| (temp
? 0 : DLADM_OPT_PERSIST
));
6941 case DLADM_STATUS_OK
:
6943 case DLADM_STATUS_NOTFOUND
:
6944 warn("invalid link property '%s'", aip
->ai_name
);
6946 case DLADM_STATUS_BADVAL
: {
6949 char **propvals
= NULL
;
6950 uint_t valcnt
= DLADM_MAX_PROP_VALCNT
;
6953 ptr
= malloc((sizeof (char *) +
6954 DLADM_PROP_VAL_MAX
) * DLADM_MAX_PROP_VALCNT
+
6957 propvals
= (char **)(void *)ptr
;
6958 if (propvals
== NULL
)
6959 die("insufficient memory");
6961 for (j
= 0; j
< DLADM_MAX_PROP_VALCNT
; j
++) {
6962 propvals
[j
] = ptr
+ sizeof (char *) *
6963 DLADM_MAX_PROP_VALCNT
+
6964 j
* DLADM_PROP_VAL_MAX
;
6966 s
= dladm_get_linkprop(handle
, linkid
,
6967 DLADM_PROP_VAL_MODIFIABLE
, aip
->ai_name
, propvals
,
6970 if (s
!= DLADM_STATUS_OK
) {
6971 warn_dlerr(status
, "cannot set link property "
6972 "'%s' on '%s'", aip
->ai_name
, argv
[optind
]);
6978 lim
= ptr
+ DLADM_STRSIZE
;
6980 for (j
= 0; j
< valcnt
; j
++) {
6981 ptr
+= snprintf(ptr
, lim
- ptr
, "%s,",
6988 warn("link property '%s' must be one of: %s",
6989 aip
->ai_name
, errmsg
);
6991 warn("invalid link property '%s'", *val
);
6997 warn_dlerr(status
, "cannot reset link property "
6998 "'%s' on '%s'", aip
->ai_name
, argv
[optind
]);
7000 warn_dlerr(status
, "cannot set link property "
7001 "'%s' on '%s'", aip
->ai_name
, argv
[optind
]);
7007 dladm_free_props(proplist
);
7008 if (status
!= DLADM_STATUS_OK
) {
7009 dladm_close(handle
);
7015 do_set_linkprop(int argc
, char **argv
, const char *use
)
7017 set_linkprop(argc
, argv
, B_FALSE
, use
);
7021 do_reset_linkprop(int argc
, char **argv
, const char *use
)
7023 set_linkprop(argc
, argv
, B_TRUE
, use
);
7027 convert_secobj(char *buf
, uint_t len
, uint8_t *obj_val
, uint_t
*obj_lenp
,
7028 dladm_secobj_class_t
class)
7032 if (class == DLADM_SECOBJ_CLASS_WPA
) {
7033 if (len
< 8 || len
> 63)
7035 (void) memcpy(obj_val
, buf
, len
);
7040 if (class == DLADM_SECOBJ_CLASS_WEP
) {
7042 case 5: /* ASCII key sizes */
7044 (void) memcpy(obj_val
, buf
, len
);
7047 case 10: /* Hex key sizes, not preceded by 0x */
7049 error
= hexascii_to_octet(buf
, len
, obj_val
, obj_lenp
);
7051 case 12: /* Hex key sizes, preceded by 0x */
7053 if (strncmp(buf
, "0x", 2) != 0)
7055 error
= hexascii_to_octet(buf
+ 2, len
- 2,
7074 get_secobj_from_tty(uint_t
try, const char *objname
, char *buf
)
7078 struct termios stored
, current
;
7079 void (*sigfunc
)(int);
7082 * Turn off echo -- but before we do so, defer SIGINT handling
7083 * so that a ^C doesn't leave the terminal corrupted.
7085 sigfunc
= signal(SIGINT
, defersig
);
7086 (void) fflush(stdin
);
7087 (void) tcgetattr(0, &stored
);
7089 current
.c_lflag
&= ~(ICANON
|ECHO
);
7090 current
.c_cc
[VTIME
] = 0;
7091 current
.c_cc
[VMIN
] = 1;
7092 (void) tcsetattr(0, TCSANOW
, ¤t
);
7095 (void) printf(gettext("provide value for '%s': "), objname
);
7097 (void) printf(gettext("confirm value for '%s': "), objname
);
7099 (void) fflush(stdout
);
7100 while (signalled
== 0) {
7102 if (c
== '\n' || c
== '\r') {
7105 (void) putchar('\n');
7110 if (len
>= DLADM_SECOBJ_VAL_MAX
- 1)
7112 (void) putchar('*');
7115 (void) putchar('\n');
7116 (void) fflush(stdin
);
7119 * Restore terminal setting and handle deferred signals.
7121 (void) tcsetattr(0, TCSANOW
, &stored
);
7123 (void) signal(SIGINT
, sigfunc
);
7125 (void) kill(getpid(), signalled
);
7131 get_secobj_val(char *obj_name
, uint8_t *obj_val
, uint_t
*obj_lenp
,
7132 dladm_secobj_class_t
class, FILE *filep
)
7136 char buf
[DLADM_SECOBJ_VAL_MAX
], buf2
[DLADM_SECOBJ_VAL_MAX
];
7138 if (filep
== NULL
) {
7139 len
= get_secobj_from_tty(1, obj_name
, buf
);
7140 rval
= convert_secobj(buf
, len
, obj_val
, obj_lenp
, class);
7142 len2
= get_secobj_from_tty(2, obj_name
, buf2
);
7143 if (len
!= len2
|| memcmp(buf
, buf2
, len
) != 0)
7149 if (fgets(buf
, sizeof (buf
), filep
) == NULL
)
7151 if (isspace(buf
[0]))
7155 if (buf
[len
- 1] == '\n') {
7156 buf
[len
- 1] = '\0';
7161 (void) fclose(filep
);
7163 return (convert_secobj(buf
, len
, obj_val
, obj_lenp
, class));
7167 check_auth(const char *auth
)
7171 if ((pw
= getpwuid(getuid())) == NULL
)
7174 return (chkauthattr(auth
, pw
->pw_name
) != 0);
7178 audit_secobj(char *auth
, char *class, char *obj
,
7179 boolean_t success
, boolean_t create
)
7181 adt_session_data_t
*ah
;
7182 adt_event_data_t
*event
;
7187 flag
= ADT_dladm_create_secobj
;
7188 errstr
= "ADT_dladm_create_secobj";
7190 flag
= ADT_dladm_delete_secobj
;
7191 errstr
= "ADT_dladm_delete_secobj";
7194 if (adt_start_session(&ah
, NULL
, ADT_USE_PROC_DATA
) != 0)
7195 die("adt_start_session: %s", strerror(errno
));
7197 if ((event
= adt_alloc_event(ah
, flag
)) == NULL
)
7198 die("adt_alloc_event (%s): %s", errstr
, strerror(errno
));
7200 /* fill in audit info */
7202 event
->adt_dladm_create_secobj
.auth_used
= auth
;
7203 event
->adt_dladm_create_secobj
.obj_class
= class;
7204 event
->adt_dladm_create_secobj
.obj_name
= obj
;
7206 event
->adt_dladm_delete_secobj
.auth_used
= auth
;
7207 event
->adt_dladm_delete_secobj
.obj_class
= class;
7208 event
->adt_dladm_delete_secobj
.obj_name
= obj
;
7212 if (adt_put_event(event
, ADT_SUCCESS
, ADT_SUCCESS
) != 0) {
7213 die("adt_put_event (%s, success): %s", errstr
,
7217 if (adt_put_event(event
, ADT_FAILURE
,
7218 ADT_FAIL_VALUE_AUTH
) != 0) {
7219 die("adt_put_event: (%s, failure): %s", errstr
,
7224 adt_free_event(event
);
7225 (void) adt_end_session(ah
);
7229 do_create_secobj(int argc
, char **argv
, const char *use
)
7233 char *obj_name
= NULL
;
7234 char *class_name
= NULL
;
7235 uint8_t obj_val
[DLADM_SECOBJ_VAL_MAX
];
7237 boolean_t success
, temp
= B_FALSE
;
7238 dladm_status_t status
;
7239 dladm_secobj_class_t
class = -1;
7243 (void) memset(obj_val
, 0, DLADM_SECOBJ_VAL_MAX
);
7244 while ((option
= getopt_long(argc
, argv
, ":f:c:R:t",
7245 wifi_longopts
, NULL
)) != -1) {
7249 (void) seteuid(getuid());
7250 filep
= fopen(optarg
, "r");
7251 if (filep
== NULL
) {
7252 die("cannot open %s: %s", optarg
,
7255 (void) seteuid(euid
);
7258 class_name
= optarg
;
7259 status
= dladm_str2secobjclass(optarg
, &class);
7260 if (status
!= DLADM_STATUS_OK
) {
7261 die("invalid secure object class '%s', "
7262 "valid values are: wep, wpa", optarg
);
7269 status
= dladm_set_rootdir(optarg
);
7270 if (status
!= DLADM_STATUS_OK
) {
7271 die_dlerr(status
, "invalid directory "
7276 die_opterr(optopt
, option
, use
);
7281 if (optind
== (argc
- 1))
7282 obj_name
= argv
[optind
];
7283 else if (optind
!= argc
)
7287 die("secure object class required");
7289 if (obj_name
== NULL
)
7290 die("secure object name required");
7292 if (!dladm_valid_secobj_name(obj_name
))
7293 die("invalid secure object name '%s'", obj_name
);
7295 success
= check_auth(LINK_SEC_AUTH
);
7296 audit_secobj(LINK_SEC_AUTH
, class_name
, obj_name
, success
, B_TRUE
);
7298 die("authorization '%s' is required", LINK_SEC_AUTH
);
7300 rval
= get_secobj_val(obj_name
, obj_val
, &obj_len
, class, filep
);
7304 die("invalid secure object class");
7307 die("invalid secure object value");
7310 die("verification failed");
7313 die("invalid secure object: %s", strerror(rval
));
7318 status
= dladm_set_secobj(handle
, obj_name
, class, obj_val
, obj_len
,
7319 DLADM_OPT_CREATE
| DLADM_OPT_ACTIVE
);
7320 if (status
!= DLADM_STATUS_OK
) {
7321 die_dlerr(status
, "could not create secure object '%s'",
7327 status
= dladm_set_secobj(handle
, obj_name
, class, obj_val
, obj_len
,
7329 if (status
!= DLADM_STATUS_OK
) {
7330 warn_dlerr(status
, "could not persistently create secure "
7331 "object '%s'", obj_name
);
7336 do_delete_secobj(int argc
, char **argv
, const char *use
)
7339 boolean_t temp
= B_FALSE
;
7341 dladm_status_t status
, pstatus
;
7343 char *field
, *token
, *lasts
= NULL
, c
;
7346 status
= pstatus
= DLADM_STATUS_OK
;
7347 while ((option
= getopt_long(argc
, argv
, ":R:t",
7348 wifi_longopts
, NULL
)) != -1) {
7354 status
= dladm_set_rootdir(optarg
);
7355 if (status
!= DLADM_STATUS_OK
) {
7356 die_dlerr(status
, "invalid directory "
7361 die_opterr(optopt
, option
, use
);
7366 if (optind
!= (argc
- 1))
7367 die("secure object name required");
7369 token
= argv
[optind
];
7370 while ((c
= *token
++) != NULL
) {
7374 token
= strdup(argv
[optind
]);
7378 success
= check_auth(LINK_SEC_AUTH
);
7379 audit_secobj(LINK_SEC_AUTH
, "unknown", argv
[optind
], success
, B_FALSE
);
7381 die("authorization '%s' is required", LINK_SEC_AUTH
);
7383 for (i
= 0; i
< nfields
; i
++) {
7385 field
= strtok_r(token
, ",", &lasts
);
7387 status
= dladm_unset_secobj(handle
, field
, DLADM_OPT_ACTIVE
);
7389 pstatus
= dladm_unset_secobj(handle
, field
,
7392 pstatus
= DLADM_STATUS_OK
;
7395 if (status
!= DLADM_STATUS_OK
) {
7396 warn_dlerr(status
, "could not delete secure object "
7399 if (pstatus
!= DLADM_STATUS_OK
) {
7400 warn_dlerr(pstatus
, "could not persistently delete "
7401 "secure object '%s'", field
);
7406 if (status
!= DLADM_STATUS_OK
|| pstatus
!= DLADM_STATUS_OK
) {
7407 dladm_close(handle
);
7412 typedef struct show_secobj_state
{
7413 boolean_t ss_persist
;
7414 boolean_t ss_parsable
;
7415 boolean_t ss_header
;
7416 ofmt_handle_t ss_ofmt
;
7417 } show_secobj_state_t
;
7421 show_secobj(dladm_handle_t dh
, void *arg
, const char *obj_name
)
7423 uint_t obj_len
= DLADM_SECOBJ_VAL_MAX
;
7424 uint8_t obj_val
[DLADM_SECOBJ_VAL_MAX
];
7425 char buf
[DLADM_STRSIZE
];
7427 dladm_secobj_class_t
class;
7428 show_secobj_state_t
*statep
= arg
;
7429 dladm_status_t status
;
7430 secobj_fields_buf_t sbuf
;
7432 bzero(&sbuf
, sizeof (secobj_fields_buf_t
));
7433 if (statep
->ss_persist
)
7434 flags
|= DLADM_OPT_PERSIST
;
7436 status
= dladm_get_secobj(dh
, obj_name
, &class, obj_val
, &obj_len
,
7438 if (status
!= DLADM_STATUS_OK
)
7439 die_dlerr(status
, "cannot get secure object '%s'", obj_name
);
7441 (void) snprintf(sbuf
.ss_obj_name
, sizeof (sbuf
.ss_obj_name
),
7443 (void) dladm_secobjclass2str(class, buf
);
7444 (void) snprintf(sbuf
.ss_class
, sizeof (sbuf
.ss_class
), "%s", buf
);
7445 if (getuid() == 0) {
7446 char val
[DLADM_SECOBJ_VAL_MAX
* 2];
7447 uint_t len
= sizeof (val
);
7449 if (octet_to_hexascii(obj_val
, obj_len
, val
, &len
) == 0)
7450 (void) snprintf(sbuf
.ss_val
,
7451 sizeof (sbuf
.ss_val
), "%s", val
);
7453 ofmt_print(statep
->ss_ofmt
, &sbuf
);
7458 do_show_secobj(int argc
, char **argv
, const char *use
)
7461 show_secobj_state_t state
;
7462 dladm_status_t status
;
7463 boolean_t o_arg
= B_FALSE
;
7466 char *fields_str
= NULL
;
7467 char *def_fields
= "object,class";
7468 char *all_fields
= "object,class,value";
7469 char *field
, *token
, *lasts
= NULL
, c
;
7471 ofmt_status_t oferr
;
7472 uint_t ofmtflags
= 0;
7475 bzero(&state
, sizeof (state
));
7476 state
.ss_parsable
= B_FALSE
;
7477 fields_str
= def_fields
;
7478 state
.ss_persist
= B_FALSE
;
7479 state
.ss_parsable
= B_FALSE
;
7480 state
.ss_header
= B_TRUE
;
7481 while ((option
= getopt_long(argc
, argv
, ":pPo:",
7482 wifi_longopts
, NULL
)) != -1) {
7485 state
.ss_parsable
= B_TRUE
;
7488 state
.ss_persist
= B_TRUE
;
7492 if (strcasecmp(optarg
, "all") == 0)
7493 fields_str
= all_fields
;
7495 fields_str
= optarg
;
7498 die_opterr(optopt
, option
, use
);
7503 if (state
.ss_parsable
&& !o_arg
)
7504 die("option -c requires -o");
7506 if (state
.ss_parsable
&& fields_str
== all_fields
)
7507 die("\"-o all\" is invalid with -p");
7509 if (state
.ss_parsable
)
7510 ofmtflags
|= OFMT_PARSABLE
;
7511 oferr
= ofmt_open(fields_str
, secobj_fields
, ofmtflags
, 0, &ofmt
);
7512 dladm_ofmt_check(oferr
, state
.ss_parsable
, ofmt
);
7513 state
.ss_ofmt
= ofmt
;
7515 flags
= state
.ss_persist
? DLADM_OPT_PERSIST
: 0;
7517 if (optind
== (argc
- 1)) {
7518 uint_t obj_fields
= 1;
7520 token
= argv
[optind
];
7522 die("secure object name required");
7523 while ((c
= *token
++) != NULL
) {
7527 token
= strdup(argv
[optind
]);
7530 for (i
= 0; i
< obj_fields
; i
++) {
7531 field
= strtok_r(token
, ",", &lasts
);
7533 if (!show_secobj(handle
, &state
, field
))
7539 } else if (optind
!= argc
)
7542 status
= dladm_walk_secobj(handle
, &state
, show_secobj
, flags
);
7544 if (status
!= DLADM_STATUS_OK
)
7545 die_dlerr(status
, "show-secobj");
7551 i_dladm_init_linkprop(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
7553 (void) dladm_init_linkprop(dh
, linkid
, B_TRUE
);
7554 return (DLADM_WALK_CONTINUE
);
7559 do_init_linkprop(int argc
, char **argv
, const char *use
)
7562 dladm_status_t status
;
7563 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
7564 datalink_media_t media
= DATALINK_ANY_MEDIATYPE
;
7565 uint_t any_media
= B_TRUE
;
7568 while ((option
= getopt(argc
, argv
, ":w")) != -1) {
7572 any_media
= B_FALSE
;
7576 * Because init-linkprop is not a public command,
7577 * print the usage instead.
7584 if (optind
== (argc
- 1)) {
7585 if ((status
= dladm_name2info(handle
, argv
[optind
], &linkid
,
7586 NULL
, NULL
, NULL
)) != DLADM_STATUS_OK
)
7587 die_dlerr(status
, "link %s is not valid", argv
[optind
]);
7588 } else if (optind
!= argc
) {
7592 if (linkid
== DATALINK_ALL_LINKID
) {
7594 * linkprops of links of other classes have been initialized as
7595 * part of the dladm up-xxx operation.
7597 (void) dladm_walk_datalink_id(i_dladm_init_linkprop
, handle
,
7598 NULL
, DATALINK_CLASS_PHYS
, media
, DLADM_OPT_PERSIST
);
7600 (void) dladm_init_linkprop(handle
, linkid
, any_media
);
7605 do_show_ether(int argc
, char **argv
, const char *use
)
7608 datalink_id_t linkid
;
7609 print_ether_state_t state
;
7610 char *fields_str
= NULL
;
7612 ofmt_status_t oferr
;
7613 uint_t ofmtflags
= 0;
7615 bzero(&state
, sizeof (state
));
7616 state
.es_link
= NULL
;
7617 state
.es_parsable
= B_FALSE
;
7619 while ((option
= getopt_long(argc
, argv
, "o:px",
7620 showeth_lopts
, NULL
)) != -1) {
7623 state
.es_extended
= B_TRUE
;
7626 state
.es_parsable
= B_TRUE
;
7629 fields_str
= optarg
;
7632 die_opterr(optopt
, option
, use
);
7637 if (optind
== (argc
- 1))
7638 state
.es_link
= argv
[optind
];
7640 if (state
.es_parsable
)
7641 ofmtflags
|= OFMT_PARSABLE
;
7642 oferr
= ofmt_open(fields_str
, ether_fields
, ofmtflags
,
7643 DLADM_DEFAULT_COL
, &ofmt
);
7644 dladm_ofmt_check(oferr
, state
.es_parsable
, ofmt
);
7645 state
.es_ofmt
= ofmt
;
7647 if (state
.es_link
== NULL
) {
7648 (void) dladm_walk_datalink_id(show_etherprop
, handle
, &state
,
7649 DATALINK_CLASS_PHYS
, DL_ETHER
, DLADM_OPT_ACTIVE
);
7651 if (!link_is_ether(state
.es_link
, &linkid
))
7652 die("invalid link specified");
7653 (void) show_etherprop(handle
, linkid
, &state
);
7659 show_etherprop(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
7661 print_ether_state_t
*statep
= arg
;
7662 ether_fields_buf_t ebuf
;
7663 dladm_ether_info_t eattr
;
7664 dladm_status_t status
;
7666 bzero(&ebuf
, sizeof (ether_fields_buf_t
));
7667 if (dladm_datalink_id2info(dh
, linkid
, NULL
, NULL
, NULL
,
7668 ebuf
.eth_link
, sizeof (ebuf
.eth_link
)) != DLADM_STATUS_OK
) {
7669 return (DLADM_WALK_CONTINUE
);
7672 status
= dladm_ether_info(dh
, linkid
, &eattr
);
7673 if (status
!= DLADM_STATUS_OK
)
7676 (void) strlcpy(ebuf
.eth_ptype
, "current", sizeof (ebuf
.eth_ptype
));
7678 (void) dladm_ether_autoneg2str(ebuf
.eth_autoneg
,
7679 sizeof (ebuf
.eth_autoneg
), &eattr
, CURRENT
);
7680 (void) dladm_ether_pause2str(ebuf
.eth_pause
,
7681 sizeof (ebuf
.eth_pause
), &eattr
, CURRENT
);
7682 (void) dladm_ether_spdx2str(ebuf
.eth_spdx
,
7683 sizeof (ebuf
.eth_spdx
), &eattr
, CURRENT
);
7684 (void) strlcpy(ebuf
.eth_state
,
7685 dladm_linkstate2str(eattr
.lei_state
, ebuf
.eth_state
),
7686 sizeof (ebuf
.eth_state
));
7687 (void) strlcpy(ebuf
.eth_rem_fault
,
7688 (eattr
.lei_attr
[CURRENT
].le_fault
? "fault" : "none"),
7689 sizeof (ebuf
.eth_rem_fault
));
7691 ofmt_print(statep
->es_ofmt
, &ebuf
);
7693 if (statep
->es_extended
)
7694 show_ether_xprop(arg
, &eattr
);
7697 dladm_ether_info_done(&eattr
);
7698 return (DLADM_WALK_CONTINUE
);
7703 do_init_secobj(int argc
, char **argv
, const char *use
)
7705 dladm_status_t status
;
7707 status
= dladm_init_secobj(handle
);
7708 if (status
!= DLADM_STATUS_OK
)
7709 die_dlerr(status
, "secure object initialization failed");
7713 brCreate
, brAdd
, brModify
7717 create_modify_add_bridge(int argc
, char **argv
, const char *use
,
7718 enum bridge_func func
)
7722 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
7723 char *altroot
= NULL
;
7724 char *links
[MAXPORT
];
7725 datalink_id_t linkids
[MAXPORT
];
7726 dladm_status_t status
;
7728 UID_STP_CFG_T cfg
, cfg_old
;
7729 dladm_bridge_prot_t brprot
= DLADM_BRIDGE_PROT_UNKNOWN
;
7730 dladm_bridge_prot_t brprot_old
;
7732 /* Set up the default configuration values */
7734 cfg
.bridge_priority
= DEF_BR_PRIO
;
7735 cfg
.max_age
= DEF_BR_MAXAGE
;
7736 cfg
.hello_time
= DEF_BR_HELLOT
;
7737 cfg
.forward_delay
= DEF_BR_FWDELAY
;
7738 cfg
.force_version
= DEF_FORCE_VERS
;
7741 while ((option
= getopt_long(argc
, argv
, ":P:R:d:f:h:l:m:p:",
7742 bridge_lopts
, NULL
)) != -1) {
7746 die_opterr(optopt
, option
, use
);
7747 status
= dladm_bridge_str2prot(optarg
, &brprot
);
7748 if (status
!= DLADM_STATUS_OK
)
7749 die_dlerr(status
, "protection %s", optarg
);
7756 die_opterr(optopt
, option
, use
);
7757 if (cfg
.field_mask
& BR_CFG_DELAY
)
7758 die("forwarding delay set more than once");
7759 if (!str2int(optarg
, &cfg
.forward_delay
) ||
7760 cfg
.forward_delay
< MIN_BR_FWDELAY
||
7761 cfg
.forward_delay
> MAX_BR_FWDELAY
)
7762 die("incorrect forwarding delay");
7763 cfg
.field_mask
|= BR_CFG_DELAY
;
7767 die_opterr(optopt
, option
, use
);
7768 if (cfg
.field_mask
& BR_CFG_FORCE_VER
)
7769 die("force protocol set more than once");
7770 if (!str2int(optarg
, &cfg
.force_version
) ||
7771 cfg
.force_version
< 0)
7772 die("incorrect force protocol");
7773 cfg
.field_mask
|= BR_CFG_FORCE_VER
;
7777 die_opterr(optopt
, option
, use
);
7778 if (cfg
.field_mask
& BR_CFG_HELLO
)
7779 die("hello time set more than once");
7780 if (!str2int(optarg
, &cfg
.hello_time
) ||
7781 cfg
.hello_time
< MIN_BR_HELLOT
||
7782 cfg
.hello_time
> MAX_BR_HELLOT
)
7783 die("incorrect hello time");
7784 cfg
.field_mask
|= BR_CFG_HELLO
;
7787 if (func
== brModify
)
7788 die_opterr(optopt
, option
, use
);
7789 if (nlink
>= MAXPORT
)
7790 die("too many links specified");
7791 links
[nlink
++] = optarg
;
7795 die_opterr(optopt
, option
, use
);
7796 if (cfg
.field_mask
& BR_CFG_AGE
)
7797 die("max age set more than once");
7798 if (!str2int(optarg
, &cfg
.max_age
) ||
7799 cfg
.max_age
< MIN_BR_MAXAGE
||
7800 cfg
.max_age
> MAX_BR_MAXAGE
)
7801 die("incorrect max age");
7802 cfg
.field_mask
|= BR_CFG_AGE
;
7806 die_opterr(optopt
, option
, use
);
7807 if (cfg
.field_mask
& BR_CFG_PRIO
)
7808 die("priority set more than once");
7809 if (!str2int(optarg
, &cfg
.bridge_priority
) ||
7810 cfg
.bridge_priority
< MIN_BR_PRIO
||
7811 cfg
.bridge_priority
> MAX_BR_PRIO
)
7812 die("incorrect priority");
7813 cfg
.bridge_priority
&= 0xF000;
7814 cfg
.field_mask
|= BR_CFG_PRIO
;
7817 die_opterr(optopt
, option
, use
);
7822 /* get the bridge name (required last argument) */
7823 if (optind
!= (argc
-1))
7826 bridge
= argv
[optind
];
7827 if (!dladm_valid_bridgename(bridge
))
7828 die("invalid bridge name '%s'", bridge
);
7831 * Get the current properties, if any, and merge in with changes. This
7832 * is necessary (even with the field_mask feature) so that the
7833 * value-checking macros will produce the right results with proposed
7834 * changes to existing configuration. We only need it for those
7835 * parameters, though.
7837 (void) dladm_bridge_get_properties(bridge
, &cfg_old
, &brprot_old
);
7838 if (brprot
== DLADM_BRIDGE_PROT_UNKNOWN
)
7839 brprot
= brprot_old
;
7840 if (!(cfg
.field_mask
& BR_CFG_AGE
))
7841 cfg
.max_age
= cfg_old
.max_age
;
7842 if (!(cfg
.field_mask
& BR_CFG_HELLO
))
7843 cfg
.hello_time
= cfg_old
.hello_time
;
7844 if (!(cfg
.field_mask
& BR_CFG_DELAY
))
7845 cfg
.forward_delay
= cfg_old
.forward_delay
;
7847 if (!CHECK_BRIDGE_CONFIG(cfg
)) {
7848 warn("illegal forward delay / max age / hello time "
7850 if (NO_MAXAGE(cfg
)) {
7851 die("no max age possible: need forward delay >= %d or "
7852 "hello time <= %d", MIN_FWDELAY_NOM(cfg
),
7853 MAX_HELLOTIME_NOM(cfg
));
7854 } else if (SMALL_MAXAGE(cfg
)) {
7855 if (CAPPED_MAXAGE(cfg
))
7856 die("max age too small: need age >= %d and "
7857 "<= %d or hello time <= %d",
7858 MIN_MAXAGE(cfg
), MAX_MAXAGE(cfg
),
7859 MAX_HELLOTIME(cfg
));
7861 die("max age too small: need age >= %d or "
7863 MIN_MAXAGE(cfg
), MAX_HELLOTIME(cfg
));
7864 } else if (FLOORED_MAXAGE(cfg
)) {
7865 die("max age too large: need age >= %d and <= %d or "
7866 "forward delay >= %d",
7867 MIN_MAXAGE(cfg
), MAX_MAXAGE(cfg
),
7870 die("max age too large: need age <= %d or forward "
7872 MAX_MAXAGE(cfg
), MIN_FWDELAY(cfg
));
7876 if (altroot
!= NULL
)
7877 altroot_cmd(altroot
, argc
, argv
);
7879 for (n
= 0; n
< nlink
; n
++) {
7880 datalink_class_t
class;
7882 char pointless
[DLADM_STRSIZE
];
7884 if (dladm_name2info(handle
, links
[n
], &linkids
[n
], NULL
, &class,
7885 &media
) != DLADM_STATUS_OK
)
7886 die("invalid link name '%s'", links
[n
]);
7887 if (class & ~(DATALINK_CLASS_PHYS
| DATALINK_CLASS_AGGR
|
7888 DATALINK_CLASS_ETHERSTUB
| DATALINK_CLASS_SIMNET
))
7889 die("%s %s cannot be bridged",
7890 dladm_class2str(class, pointless
), links
[n
]);
7891 if (media
!= DL_ETHER
&& media
!= DL_100VG
&&
7892 media
!= DL_ETH_CSMA
&& media
!= DL_100BT
)
7893 die("%s interface %s cannot be bridged",
7894 dladm_media2str(media
, pointless
), links
[n
]);
7897 if (func
== brCreate
)
7898 flags
|= DLADM_OPT_CREATE
;
7900 if (func
!= brAdd
) {
7901 status
= dladm_bridge_configure(handle
, bridge
, &cfg
, brprot
,
7903 if (status
!= DLADM_STATUS_OK
)
7904 die_dlerr(status
, "create operation failed");
7907 status
= DLADM_STATUS_OK
;
7908 for (n
= 0; n
< nlink
; n
++) {
7909 status
= dladm_bridge_setlink(handle
, linkids
[n
], bridge
);
7910 if (status
!= DLADM_STATUS_OK
)
7916 * We were successful. If we're creating a new bridge, then
7917 * there's just one more step: enabling. If we're modifying or
7918 * just adding links, then we're done.
7920 if (func
!= brCreate
||
7921 (status
= dladm_bridge_enable(bridge
)) == DLADM_STATUS_OK
)
7925 /* clean up the partial configuration */
7926 for (i
= 0; i
< n
; i
++)
7927 (void) dladm_bridge_setlink(handle
, linkids
[i
], "");
7929 /* if failure for brCreate, then delete the bridge */
7930 if (func
== brCreate
)
7931 (void) dladm_bridge_delete(handle
, bridge
, flags
);
7934 die_dlerr(status
, "unable to add link %s to bridge %s",
7937 die_dlerr(status
, "unable to enable bridge %s", bridge
);
7941 do_create_bridge(int argc
, char **argv
, const char *use
)
7943 create_modify_add_bridge(argc
, argv
, use
, brCreate
);
7947 do_modify_bridge(int argc
, char **argv
, const char *use
)
7949 create_modify_add_bridge(argc
, argv
, use
, brModify
);
7953 do_add_bridge(int argc
, char **argv
, const char *use
)
7955 create_modify_add_bridge(argc
, argv
, use
, brAdd
);
7959 do_delete_bridge(int argc
, char **argv
, const char *use
)
7962 char *altroot
= NULL
;
7963 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
7964 dladm_status_t status
;
7967 while ((option
= getopt_long(argc
, argv
, ":R:", bridge_lopts
, NULL
)) !=
7974 die_opterr(optopt
, option
, use
);
7979 /* get the bridge name (required last argument) */
7980 if (optind
!= (argc
-1))
7983 if (altroot
!= NULL
)
7984 altroot_cmd(altroot
, argc
, argv
);
7986 status
= dladm_bridge_delete(handle
, argv
[optind
], flags
);
7987 if (status
!= DLADM_STATUS_OK
)
7988 die_dlerr(status
, "delete operation failed");
7992 do_remove_bridge(int argc
, char **argv
, const char *use
)
7996 char *links
[MAXPORT
];
7997 datalink_id_t linkids
[MAXPORT
];
7998 char *altroot
= NULL
;
7999 dladm_status_t status
;
8000 boolean_t removed_one
;
8003 while ((option
= getopt_long(argc
, argv
, ":R:l:", bridge_lopts
,
8010 if (nlink
>= MAXPORT
)
8011 die("too many links specified");
8012 links
[nlink
++] = optarg
;
8015 die_opterr(optopt
, option
, use
);
8023 /* get the bridge name (required last argument) */
8024 if (optind
!= (argc
-1))
8027 if (altroot
!= NULL
)
8028 altroot_cmd(altroot
, argc
, argv
);
8030 for (n
= 0; n
< nlink
; n
++) {
8031 char bridge
[MAXLINKNAMELEN
];
8033 if (dladm_name2info(handle
, links
[n
], &linkids
[n
], NULL
, NULL
,
8034 NULL
) != DLADM_STATUS_OK
)
8035 die("invalid link name '%s'", links
[n
]);
8036 status
= dladm_bridge_getlink(handle
, linkids
[n
], bridge
,
8038 if (status
!= DLADM_STATUS_OK
&&
8039 status
!= DLADM_STATUS_NOTFOUND
) {
8040 die_dlerr(status
, "cannot get bridge status on %s",
8043 if (status
== DLADM_STATUS_NOTFOUND
||
8044 strcmp(bridge
, argv
[optind
]) != 0)
8045 die("link %s is not on bridge %s", links
[n
],
8049 removed_one
= B_FALSE
;
8050 for (n
= 0; n
< nlink
; n
++) {
8051 status
= dladm_bridge_setlink(handle
, linkids
[n
], "");
8052 if (status
== DLADM_STATUS_OK
) {
8053 removed_one
= B_TRUE
;
8056 "cannot remove link %s from bridge %s",
8057 links
[n
], argv
[optind
]);
8061 die("unable to remove any links from bridge %s", argv
[optind
]);
8065 fmt_int(char *buf
, size_t buflen
, int value
, int runvalue
,
8066 boolean_t printstar
)
8068 (void) snprintf(buf
, buflen
, "%d", value
);
8069 if (value
!= runvalue
&& printstar
)
8070 (void) strlcat(buf
, "*", buflen
);
8074 fmt_bridge_id(char *buf
, size_t buflen
, UID_BRIDGE_ID_T
*bid
)
8076 (void) snprintf(buf
, buflen
, "%u/%x:%x:%x:%x:%x:%x", bid
->prio
,
8077 bid
->addr
[0], bid
->addr
[1], bid
->addr
[2], bid
->addr
[3],
8078 bid
->addr
[4], bid
->addr
[5]);
8081 static dladm_status_t
8082 print_bridge(show_state_t
*state
, datalink_id_t linkid
,
8083 bridge_fields_buf_t
*bbuf
)
8085 char link
[MAXLINKNAMELEN
];
8086 datalink_class_t
class;
8088 dladm_status_t status
;
8089 UID_STP_CFG_T smfcfg
, runcfg
;
8090 UID_STP_STATE_T stpstate
;
8091 dladm_bridge_prot_t smfprot
, runprot
;
8093 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
8094 NULL
, link
, sizeof (link
))) != DLADM_STATUS_OK
)
8097 if (!(state
->ls_flags
& flags
))
8098 return (DLADM_STATUS_NOTFOUND
);
8100 /* Convert observability node name back to bridge name */
8101 if (!dladm_observe_to_bridge(link
))
8102 return (DLADM_STATUS_NOTFOUND
);
8103 (void) strlcpy(bbuf
->bridge_name
, link
, sizeof (bbuf
->bridge_name
));
8106 * If the running value differs from the one in SMF, and parsable
8107 * output is not requested, then we show the running value with an
8110 (void) dladm_bridge_get_properties(bbuf
->bridge_name
, &smfcfg
,
8112 (void) dladm_bridge_run_properties(bbuf
->bridge_name
, &runcfg
,
8114 (void) snprintf(bbuf
->bridge_protect
, sizeof (bbuf
->bridge_protect
),
8115 "%s%s", state
->ls_parsable
|| smfprot
== runprot
? "" : "*",
8116 dladm_bridge_prot2str(runprot
));
8117 fmt_int(bbuf
->bridge_priority
, sizeof (bbuf
->bridge_priority
),
8118 smfcfg
.bridge_priority
, runcfg
.bridge_priority
,
8119 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_AGE
));
8120 fmt_int(bbuf
->bridge_bmaxage
, sizeof (bbuf
->bridge_bmaxage
),
8121 smfcfg
.max_age
, runcfg
.max_age
,
8122 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_AGE
));
8123 fmt_int(bbuf
->bridge_bhellotime
,
8124 sizeof (bbuf
->bridge_bhellotime
), smfcfg
.hello_time
,
8126 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_HELLO
));
8127 fmt_int(bbuf
->bridge_bfwddelay
, sizeof (bbuf
->bridge_bfwddelay
),
8128 smfcfg
.forward_delay
, runcfg
.forward_delay
,
8129 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_DELAY
));
8130 fmt_int(bbuf
->bridge_forceproto
, sizeof (bbuf
->bridge_forceproto
),
8131 smfcfg
.force_version
, runcfg
.force_version
,
8132 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_FORCE_VER
));
8133 fmt_int(bbuf
->bridge_holdtime
, sizeof (bbuf
->bridge_holdtime
),
8134 smfcfg
.hold_time
, runcfg
.hold_time
,
8135 !state
->ls_parsable
&& (runcfg
.field_mask
& BR_CFG_HOLD_TIME
));
8137 if (dladm_bridge_state(bbuf
->bridge_name
, &stpstate
) ==
8139 fmt_bridge_id(bbuf
->bridge_address
,
8140 sizeof (bbuf
->bridge_address
), &stpstate
.bridge_id
);
8141 (void) snprintf(bbuf
->bridge_tctime
,
8142 sizeof (bbuf
->bridge_tctime
), "%lu",
8143 stpstate
.timeSince_Topo_Change
);
8144 (void) snprintf(bbuf
->bridge_tccount
,
8145 sizeof (bbuf
->bridge_tccount
), "%lu",
8146 stpstate
.Topo_Change_Count
);
8147 (void) snprintf(bbuf
->bridge_tchange
,
8148 sizeof (bbuf
->bridge_tchange
), "%u", stpstate
.Topo_Change
);
8149 fmt_bridge_id(bbuf
->bridge_desroot
,
8150 sizeof (bbuf
->bridge_desroot
), &stpstate
.designated_root
);
8151 (void) snprintf(bbuf
->bridge_rootcost
,
8152 sizeof (bbuf
->bridge_rootcost
), "%lu",
8153 stpstate
.root_path_cost
);
8154 (void) snprintf(bbuf
->bridge_rootport
,
8155 sizeof (bbuf
->bridge_rootport
), "%u", stpstate
.root_port
);
8156 (void) snprintf(bbuf
->bridge_maxage
,
8157 sizeof (bbuf
->bridge_maxage
), "%d", stpstate
.max_age
);
8158 (void) snprintf(bbuf
->bridge_hellotime
,
8159 sizeof (bbuf
->bridge_hellotime
), "%d", stpstate
.hello_time
);
8160 (void) snprintf(bbuf
->bridge_fwddelay
,
8161 sizeof (bbuf
->bridge_fwddelay
), "%d",
8162 stpstate
.forward_delay
);
8164 return (DLADM_STATUS_OK
);
8167 static dladm_status_t
8168 print_bridge_stats(show_state_t
*state
, datalink_id_t linkid
,
8169 bridge_statfields_buf_t
*bbuf
)
8171 char link
[MAXLINKNAMELEN
];
8172 datalink_class_t
class;
8174 dladm_status_t status
;
8177 brsum_t
*brsum
= (brsum_t
*)&state
->ls_prevstats
;
8181 /* This is a compile-time assertion; optimizer normally fixes this */
8182 extern void brsum_t_is_too_large(void);
8184 if (sizeof (*brsum
) > sizeof (state
->ls_prevstats
))
8185 brsum_t_is_too_large();
8188 if (state
->ls_firstonly
) {
8189 if (state
->ls_donefirst
)
8190 return (DLADM_WALK_CONTINUE
);
8191 state
->ls_donefirst
= B_TRUE
;
8193 bzero(brsum
, sizeof (*brsum
));
8195 bzero(&newval
, sizeof (newval
));
8197 if ((status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class,
8198 NULL
, link
, sizeof (link
))) != DLADM_STATUS_OK
)
8201 if (!(state
->ls_flags
& flags
))
8202 return (DLADM_STATUS_NOTFOUND
);
8204 if ((kcp
= kstat_open()) == NULL
) {
8205 warn("kstat open operation failed");
8206 return (DLADM_STATUS_OK
);
8208 if ((ksp
= kstat_lookup(kcp
, "bridge", 0, link
)) != NULL
&&
8209 kstat_read(kcp
, ksp
, NULL
) != -1) {
8210 if (dladm_kstat_value(ksp
, "drops", KSTAT_DATA_UINT64
,
8211 &newval
.drops
) == DLADM_STATUS_OK
) {
8212 (void) snprintf(bbuf
->bridges_drops
,
8213 sizeof (bbuf
->bridges_drops
), "%llu",
8214 newval
.drops
- brsum
->drops
);
8216 if (dladm_kstat_value(ksp
, "forward_direct", KSTAT_DATA_UINT64
,
8217 &newval
.forward_dir
) == DLADM_STATUS_OK
) {
8218 (void) snprintf(bbuf
->bridges_forwards
,
8219 sizeof (bbuf
->bridges_forwards
), "%llu",
8220 newval
.forward_dir
- brsum
->forward_dir
);
8222 if (dladm_kstat_value(ksp
, "forward_mbcast", KSTAT_DATA_UINT64
,
8223 &newval
.forward_mb
) == DLADM_STATUS_OK
) {
8224 (void) snprintf(bbuf
->bridges_mbcast
,
8225 sizeof (bbuf
->bridges_mbcast
), "%llu",
8226 newval
.forward_mb
- brsum
->forward_mb
);
8228 if (dladm_kstat_value(ksp
, "forward_unknown", KSTAT_DATA_UINT64
,
8229 &newval
.forward_unk
) == DLADM_STATUS_OK
) {
8230 (void) snprintf(bbuf
->bridges_unknown
,
8231 sizeof (bbuf
->bridges_unknown
), "%llu",
8232 newval
.forward_unk
- brsum
->forward_unk
);
8234 if (dladm_kstat_value(ksp
, "recv", KSTAT_DATA_UINT64
,
8235 &newval
.recv
) == DLADM_STATUS_OK
) {
8236 (void) snprintf(bbuf
->bridges_recv
,
8237 sizeof (bbuf
->bridges_recv
), "%llu",
8238 newval
.recv
- brsum
->recv
);
8240 if (dladm_kstat_value(ksp
, "sent", KSTAT_DATA_UINT64
,
8241 &newval
.sent
) == DLADM_STATUS_OK
) {
8242 (void) snprintf(bbuf
->bridges_sent
,
8243 sizeof (bbuf
->bridges_sent
), "%llu",
8244 newval
.sent
- brsum
->sent
);
8247 (void) kstat_close(kcp
);
8249 /* Convert observability node name back to bridge name */
8250 if (!dladm_observe_to_bridge(link
))
8251 return (DLADM_STATUS_NOTFOUND
);
8252 (void) strlcpy(bbuf
->bridges_name
, link
, sizeof (bbuf
->bridges_name
));
8256 return (DLADM_STATUS_OK
);
8260 * This structure carries around extra state information for the show-bridge
8261 * command and allows us to use common support functions.
8265 boolean_t show_stats
;
8271 show_bridge(dladm_handle_t handle
, datalink_id_t linkid
, void *arg
)
8273 show_brstate_t
*brstate
= arg
;
8276 if (brstate
->show_stats
) {
8277 bridge_statfields_buf_t bbuf
;
8279 bzero(&bbuf
, sizeof (bbuf
));
8280 brstate
->state
.ls_status
= print_bridge_stats(&brstate
->state
,
8284 bridge_fields_buf_t bbuf
;
8286 bzero(&bbuf
, sizeof (bbuf
));
8287 brstate
->state
.ls_status
= print_bridge(&brstate
->state
, linkid
,
8291 if (brstate
->state
.ls_status
== DLADM_STATUS_OK
)
8292 ofmt_print(brstate
->state
.ls_ofmt
, buf
);
8293 return (DLADM_WALK_CONTINUE
);
8297 fmt_bool(char *buf
, size_t buflen
, int val
)
8299 (void) strlcpy(buf
, val
? "yes" : "no", buflen
);
8302 static dladm_status_t
8303 print_bridge_link(show_state_t
*state
, datalink_id_t linkid
,
8304 bridge_link_fields_buf_t
*bbuf
)
8306 datalink_class_t
class;
8308 dladm_status_t status
;
8309 UID_STP_PORT_STATE_T stpstate
;
8311 status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class, NULL
,
8312 bbuf
->bridgel_link
, sizeof (bbuf
->bridgel_link
));
8313 if (status
!= DLADM_STATUS_OK
)
8316 if (!(state
->ls_flags
& flags
))
8317 return (DLADM_STATUS_NOTFOUND
);
8319 if (dladm_bridge_link_state(handle
, linkid
, &stpstate
) ==
8321 (void) snprintf(bbuf
->bridgel_index
,
8322 sizeof (bbuf
->bridgel_index
), "%u", stpstate
.port_no
);
8323 if (dlsym(RTLD_PROBE
, "STP_IN_state2str")) {
8324 (void) strlcpy(bbuf
->bridgel_state
,
8325 STP_IN_state2str(stpstate
.state
),
8326 sizeof (bbuf
->bridgel_state
));
8328 (void) snprintf(bbuf
->bridgel_state
,
8329 sizeof (bbuf
->bridgel_state
), "%u",
8332 (void) snprintf(bbuf
->bridgel_uptime
,
8333 sizeof (bbuf
->bridgel_uptime
), "%lu", stpstate
.uptime
);
8334 (void) snprintf(bbuf
->bridgel_opercost
,
8335 sizeof (bbuf
->bridgel_opercost
), "%lu",
8336 stpstate
.oper_port_path_cost
);
8337 fmt_bool(bbuf
->bridgel_operp2p
, sizeof (bbuf
->bridgel_operp2p
),
8338 stpstate
.oper_point2point
);
8339 fmt_bool(bbuf
->bridgel_operedge
,
8340 sizeof (bbuf
->bridgel_operedge
), stpstate
.oper_edge
);
8341 fmt_bridge_id(bbuf
->bridgel_desroot
,
8342 sizeof (bbuf
->bridgel_desroot
), &stpstate
.designated_root
);
8343 (void) snprintf(bbuf
->bridgel_descost
,
8344 sizeof (bbuf
->bridgel_descost
), "%lu",
8345 stpstate
.designated_cost
);
8346 fmt_bridge_id(bbuf
->bridgel_desbridge
,
8347 sizeof (bbuf
->bridgel_desbridge
),
8348 &stpstate
.designated_bridge
);
8349 (void) snprintf(bbuf
->bridgel_desport
,
8350 sizeof (bbuf
->bridgel_desport
), "%u",
8351 stpstate
.designated_port
);
8352 fmt_bool(bbuf
->bridgel_tcack
, sizeof (bbuf
->bridgel_tcack
),
8353 stpstate
.top_change_ack
);
8355 return (DLADM_STATUS_OK
);
8358 static dladm_status_t
8359 print_bridge_link_stats(show_state_t
*state
, datalink_id_t linkid
,
8360 bridge_link_statfields_buf_t
*bbuf
)
8362 datalink_class_t
class;
8364 dladm_status_t status
;
8365 UID_STP_PORT_STATE_T stpstate
;
8368 char bridge
[MAXLINKNAMELEN
];
8369 char kstatname
[MAXLINKNAMELEN
*2 + 1];
8370 brlsum_t
*brlsum
= (brlsum_t
*)&state
->ls_prevstats
;
8374 /* This is a compile-time assertion; optimizer normally fixes this */
8375 extern void brlsum_t_is_too_large(void);
8377 if (sizeof (*brlsum
) > sizeof (state
->ls_prevstats
))
8378 brlsum_t_is_too_large();
8381 if (state
->ls_firstonly
) {
8382 if (state
->ls_donefirst
)
8383 return (DLADM_WALK_CONTINUE
);
8384 state
->ls_donefirst
= B_TRUE
;
8386 bzero(brlsum
, sizeof (*brlsum
));
8388 bzero(&newval
, sizeof (newval
));
8390 status
= dladm_datalink_id2info(handle
, linkid
, &flags
, &class, NULL
,
8391 bbuf
->bridgels_link
, sizeof (bbuf
->bridgels_link
));
8392 if (status
!= DLADM_STATUS_OK
)
8395 if (!(state
->ls_flags
& flags
))
8396 return (DLADM_STATUS_NOTFOUND
);
8398 if (dladm_bridge_link_state(handle
, linkid
, &stpstate
) ==
8400 newval
.cfgbpdu
= stpstate
.rx_cfg_bpdu_cnt
;
8401 newval
.tcnbpdu
= stpstate
.rx_tcn_bpdu_cnt
;
8402 newval
.rstpbpdu
= stpstate
.rx_rstp_bpdu_cnt
;
8403 newval
.txbpdu
= stpstate
.txCount
;
8405 (void) snprintf(bbuf
->bridgels_cfgbpdu
,
8406 sizeof (bbuf
->bridgels_cfgbpdu
), "%lu",
8407 newval
.cfgbpdu
- brlsum
->cfgbpdu
);
8408 (void) snprintf(bbuf
->bridgels_tcnbpdu
,
8409 sizeof (bbuf
->bridgels_tcnbpdu
), "%lu",
8410 newval
.tcnbpdu
- brlsum
->tcnbpdu
);
8411 (void) snprintf(bbuf
->bridgels_rstpbpdu
,
8412 sizeof (bbuf
->bridgels_rstpbpdu
), "%lu",
8413 newval
.rstpbpdu
- brlsum
->rstpbpdu
);
8414 (void) snprintf(bbuf
->bridgels_txbpdu
,
8415 sizeof (bbuf
->bridgels_txbpdu
), "%lu",
8416 newval
.txbpdu
- brlsum
->txbpdu
);
8419 if ((status
= dladm_bridge_getlink(handle
, linkid
, bridge
,
8420 sizeof (bridge
))) != DLADM_STATUS_OK
)
8422 (void) snprintf(kstatname
, sizeof (kstatname
), "%s0-%s", bridge
,
8423 bbuf
->bridgels_link
);
8424 if ((kcp
= kstat_open()) == NULL
) {
8425 warn("kstat open operation failed");
8428 if ((ksp
= kstat_lookup(kcp
, "bridge", 0, kstatname
)) != NULL
&&
8429 kstat_read(kcp
, ksp
, NULL
) != -1) {
8430 if (dladm_kstat_value(ksp
, "drops", KSTAT_DATA_UINT64
,
8431 &newval
.drops
) != -1) {
8432 (void) snprintf(bbuf
->bridgels_drops
,
8433 sizeof (bbuf
->bridgels_drops
), "%llu",
8434 newval
.drops
- brlsum
->drops
);
8436 if (dladm_kstat_value(ksp
, "recv", KSTAT_DATA_UINT64
,
8437 &newval
.recv
) != -1) {
8438 (void) snprintf(bbuf
->bridgels_recv
,
8439 sizeof (bbuf
->bridgels_recv
), "%llu",
8440 newval
.recv
- brlsum
->recv
);
8442 if (dladm_kstat_value(ksp
, "xmit", KSTAT_DATA_UINT64
,
8443 &newval
.xmit
) != -1) {
8444 (void) snprintf(bbuf
->bridgels_xmit
,
8445 sizeof (bbuf
->bridgels_xmit
), "%llu",
8446 newval
.xmit
- brlsum
->xmit
);
8449 (void) kstat_close(kcp
);
8457 show_bridge_link(datalink_id_t linkid
, show_brstate_t
*brstate
)
8461 if (brstate
->show_stats
) {
8462 bridge_link_statfields_buf_t bbuf
;
8464 bzero(&bbuf
, sizeof (bbuf
));
8465 brstate
->state
.ls_status
= print_bridge_link_stats(
8466 &brstate
->state
, linkid
, &bbuf
);
8469 bridge_link_fields_buf_t bbuf
;
8471 bzero(&bbuf
, sizeof (bbuf
));
8472 brstate
->state
.ls_status
= print_bridge_link(&brstate
->state
,
8476 if (brstate
->state
.ls_status
== DLADM_STATUS_OK
)
8477 ofmt_print(brstate
->state
.ls_ofmt
, buf
);
8482 show_bridge_link_walk(dladm_handle_t handle
, datalink_id_t linkid
, void *arg
)
8484 show_brstate_t
*brstate
= arg
;
8485 char bridge
[MAXLINKNAMELEN
];
8487 if (dladm_bridge_getlink(handle
, linkid
, bridge
, sizeof (bridge
)) ==
8488 DLADM_STATUS_OK
&& strcmp(bridge
, brstate
->bridge
) == 0) {
8489 show_bridge_link(linkid
, brstate
);
8491 return (DLADM_WALK_CONTINUE
);
8495 show_bridge_fwd(dladm_handle_t handle
, bridge_listfwd_t
*blf
,
8496 show_state_t
*state
)
8498 bridge_fwd_fields_buf_t bbuf
;
8500 bzero(&bbuf
, sizeof (bbuf
));
8501 (void) snprintf(bbuf
.bridgef_dest
, sizeof (bbuf
.bridgef_dest
),
8502 "%s", ether_ntoa((struct ether_addr
*)blf
->blf_dest
));
8503 if (blf
->blf_is_local
) {
8504 (void) strlcpy(bbuf
.bridgef_flags
, "L",
8505 sizeof (bbuf
.bridgef_flags
));
8507 (void) snprintf(bbuf
.bridgef_age
, sizeof (bbuf
.bridgef_age
),
8508 "%2d.%03d", blf
->blf_ms_age
/ 1000, blf
->blf_ms_age
% 1000);
8509 if (blf
->blf_trill_nick
!= 0) {
8510 (void) snprintf(bbuf
.bridgef_output
,
8511 sizeof (bbuf
.bridgef_output
), "%u",
8512 blf
->blf_trill_nick
);
8515 if (blf
->blf_linkid
!= DATALINK_INVALID_LINKID
&&
8516 blf
->blf_trill_nick
== 0) {
8517 state
->ls_status
= dladm_datalink_id2info(handle
,
8518 blf
->blf_linkid
, NULL
, NULL
, NULL
, bbuf
.bridgef_output
,
8519 sizeof (bbuf
.bridgef_output
));
8521 if (state
->ls_status
== DLADM_STATUS_OK
)
8522 ofmt_print(state
->ls_ofmt
, &bbuf
);
8526 show_bridge_trillnick(trill_listnick_t
*tln
, show_state_t
*state
)
8528 bridge_trill_fields_buf_t bbuf
;
8530 bzero(&bbuf
, sizeof (bbuf
));
8531 (void) snprintf(bbuf
.bridget_nick
, sizeof (bbuf
.bridget_nick
),
8532 "%u", tln
->tln_nick
);
8533 if (tln
->tln_ours
) {
8534 (void) strlcpy(bbuf
.bridget_flags
, "L",
8535 sizeof (bbuf
.bridget_flags
));
8537 state
->ls_status
= dladm_datalink_id2info(handle
,
8538 tln
->tln_linkid
, NULL
, NULL
, NULL
, bbuf
.bridget_link
,
8539 sizeof (bbuf
.bridget_link
));
8540 (void) snprintf(bbuf
.bridget_nexthop
,
8541 sizeof (bbuf
.bridget_nexthop
), "%s",
8542 ether_ntoa((struct ether_addr
*)tln
->tln_nexthop
));
8544 if (state
->ls_status
== DLADM_STATUS_OK
)
8545 ofmt_print(state
->ls_ofmt
, &bbuf
);
8549 do_show_bridge(int argc
, char **argv
, const char *use
)
8553 bridgeMode
, linkMode
, fwdMode
, trillMode
8554 } op_mode
= bridgeMode
;
8555 uint32_t flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
8556 boolean_t parsable
= B_FALSE
;
8557 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
8559 show_brstate_t brstate
;
8560 dladm_status_t status
;
8561 char *fields_str
= NULL
;
8562 /* default: bridge-related data */
8563 char *all_fields
= "bridge,protect,address,priority,bmaxage,"
8564 "bhellotime,bfwddelay,forceproto,tctime,tccount,tchange,"
8565 "desroot,rootcost,rootport,maxage,hellotime,fwddelay,holdtime";
8566 char *default_fields
= "bridge,protect,address,priority,"
8568 char *all_statfields
= "bridge,drops,forwards,mbcast,"
8569 "unknown,recv,sent";
8570 char *default_statfields
= "bridge,drops,forwards,mbcast,"
8572 /* -l: link-related data */
8573 char *all_link_fields
= "link,index,state,uptime,opercost,"
8574 "operp2p,operedge,desroot,descost,desbridge,desport,tcack";
8575 char *default_link_fields
= "link,state,uptime,desroot";
8576 char *all_link_statfields
= "link,cfgbpdu,tcnbpdu,rstpbpdu,"
8577 "txbpdu,drops,recv,xmit";
8578 char *default_link_statfields
= "link,drops,recv,xmit";
8579 /* -f: bridge forwarding table related data */
8580 char *default_fwd_fields
= "dest,age,flags,output";
8581 /* -t: TRILL nickname table related data */
8582 char *default_trill_fields
= "nick,flags,link,nexthop";
8585 ofmt_field_t
*field_arr
;
8587 ofmt_status_t oferr
;
8588 uint_t ofmtflags
= 0;
8590 bzero(&brstate
, sizeof (brstate
));
8593 while ((option
= getopt_long(argc
, argv
, ":fi:lo:pst",
8594 bridge_show_lopts
, NULL
)) != -1) {
8597 if (op_mode
!= bridgeMode
&& op_mode
!= fwdMode
)
8598 die("-f is incompatible with -l or -t");
8604 if (!str2int(optarg
, &interval
) || interval
== 0)
8605 die("invalid interval value '%s'", optarg
);
8608 if (op_mode
!= bridgeMode
&& op_mode
!= linkMode
)
8609 die("-l is incompatible with -f or -t");
8613 fields_str
= optarg
;
8621 if (brstate
.show_stats
)
8623 brstate
.show_stats
= B_TRUE
;
8626 if (op_mode
!= bridgeMode
&& op_mode
!= trillMode
)
8627 die("-t is incompatible with -f or -l");
8628 op_mode
= trillMode
;
8631 die_opterr(optopt
, option
, use
);
8636 if (interval
!= 0 && !brstate
.show_stats
)
8637 die("the -i option can be used only with -s");
8639 if ((op_mode
== fwdMode
|| op_mode
== trillMode
) && brstate
.show_stats
)
8640 die("the -f/-t and -s options cannot be used together");
8642 /* get the bridge name (optional last argument) */
8643 if (optind
== (argc
-1)) {
8644 char lname
[MAXLINKNAMELEN
];
8646 datalink_class_t
class;
8648 brstate
.bridge
= argv
[optind
];
8649 (void) snprintf(lname
, sizeof (lname
), "%s0", brstate
.bridge
);
8650 if ((status
= dladm_name2info(handle
, lname
, &linkid
, &lnkflg
,
8651 &class, NULL
)) != DLADM_STATUS_OK
) {
8652 die_dlerr(status
, "bridge %s is not valid",
8656 if (class != DATALINK_CLASS_BRIDGE
)
8657 die("%s is not a bridge", brstate
.bridge
);
8659 if (!(lnkflg
& flags
)) {
8660 die_dlerr(DLADM_STATUS_BADARG
,
8661 "bridge %s is temporarily removed", brstate
.bridge
);
8663 } else if (optind
!= argc
) {
8665 } else if (op_mode
!= bridgeMode
) {
8666 die("bridge name required for -l, -f, or -t");
8670 brstate
.state
.ls_parsable
= parsable
;
8671 brstate
.state
.ls_flags
= flags
;
8672 brstate
.state
.ls_firstonly
= (interval
!= 0);
8676 if (brstate
.show_stats
) {
8677 default_str
= default_statfields
;
8678 all_str
= all_statfields
;
8679 field_arr
= bridge_statfields
;
8681 default_str
= default_fields
;
8682 all_str
= all_fields
;
8683 field_arr
= bridge_fields
;
8688 if (brstate
.show_stats
) {
8689 default_str
= default_link_statfields
;
8690 all_str
= all_link_statfields
;
8691 field_arr
= bridge_link_statfields
;
8693 default_str
= default_link_fields
;
8694 all_str
= all_link_fields
;
8695 field_arr
= bridge_link_fields
;
8700 default_str
= all_str
= default_fwd_fields
;
8701 field_arr
= bridge_fwd_fields
;
8705 default_str
= all_str
= default_trill_fields
;
8706 field_arr
= bridge_trill_fields
;
8710 if (fields_str
== NULL
)
8711 fields_str
= default_str
;
8712 else if (strcasecmp(fields_str
, "all") == 0)
8713 fields_str
= all_str
;
8716 ofmtflags
|= OFMT_PARSABLE
;
8717 oferr
= ofmt_open(fields_str
, field_arr
, ofmtflags
, 0, &ofmt
);
8718 dladm_ofmt_check(oferr
, brstate
.state
.ls_parsable
, ofmt
);
8719 brstate
.state
.ls_ofmt
= ofmt
;
8722 brstate
.state
.ls_donefirst
= B_FALSE
;
8725 if (linkid
== DATALINK_ALL_LINKID
) {
8726 (void) dladm_walk_datalink_id(show_bridge
,
8727 handle
, &brstate
, DATALINK_CLASS_BRIDGE
,
8728 DATALINK_ANY_MEDIATYPE
, flags
);
8730 (void) show_bridge(handle
, linkid
, &brstate
);
8731 if (brstate
.state
.ls_status
!=
8733 die_dlerr(brstate
.state
.ls_status
,
8734 "failed to show bridge %s",
8744 dlp
= dladm_bridge_get_portlist(brstate
.bridge
,
8747 for (i
= 0; i
< nlinks
; i
++)
8748 show_bridge_link(dlp
[i
], &brstate
);
8749 dladm_bridge_free_portlist(dlp
);
8750 } else if (errno
== ENOENT
) {
8751 /* bridge not running; iterate on libdladm */
8752 (void) dladm_walk_datalink_id(
8753 show_bridge_link_walk
, handle
,
8754 &brstate
, DATALINK_CLASS_PHYS
|
8755 DATALINK_CLASS_AGGR
|
8756 DATALINK_CLASS_ETHERSTUB
,
8757 DATALINK_ANY_MEDIATYPE
, flags
);
8759 die("unable to get port list for bridge %s: %s",
8760 brstate
.bridge
, strerror(errno
));
8766 bridge_listfwd_t
*blf
;
8769 blf
= dladm_bridge_get_fwdtable(handle
, brstate
.bridge
,
8772 die("unable to get forwarding entries for "
8773 "bridge %s", brstate
.bridge
);
8775 for (i
= 0; i
< nfwd
; i
++)
8776 show_bridge_fwd(handle
, blf
+ i
,
8778 dladm_bridge_free_fwdtable(blf
);
8784 trill_listnick_t
*tln
;
8787 tln
= dladm_bridge_get_trillnick(brstate
.bridge
,
8790 if (errno
== ENOENT
)
8791 die("bridge %s is not running TRILL",
8794 die("unable to get TRILL nickname "
8795 "entries for bridge %s",
8798 for (i
= 0; i
< nnick
; i
++)
8799 show_bridge_trillnick(tln
+ i
,
8801 dladm_bridge_free_trillnick(tln
);
8808 (void) sleep(interval
);
8813 * "-R" option support. It is used for live upgrading. Append dladm commands
8814 * to a upgrade script which will be run when the alternative root boots up:
8816 * - If the /etc/dladm/datalink.conf file exists on the alternative root,
8817 * append dladm commands to the <altroot>/var/svc/profile/upgrade_datalink
8818 * script. This script will be run as part of the network/physical service.
8819 * We cannot defer this to /var/svc/profile/upgrade because then the
8820 * configuration will not be able to take effect before network/physical
8821 * plumbs various interfaces.
8823 * - If the /etc/dladm/datalink.conf file does not exist on the alternative
8824 * root, append dladm commands to the <altroot>/var/svc/profile/upgrade script,
8825 * which will be run in the manifest-import service.
8827 * Note that the SMF team is considering to move the manifest-import service
8828 * to be run at the very begining of boot. Once that is done, the need for
8829 * the /var/svc/profile/upgrade_datalink script will not exist any more.
8832 altroot_cmd(char *altroot
, int argc
, char *argv
[])
8834 char path
[MAXPATHLEN
];
8840 * Check for the existence of the /etc/dladm/datalink.conf
8841 * configuration file, and determine the name of script file.
8843 (void) snprintf(path
, MAXPATHLEN
, "/%s/etc/dladm/datalink.conf",
8845 if (stat(path
, &stbuf
) < 0) {
8846 (void) snprintf(path
, MAXPATHLEN
, "/%s/%s", altroot
,
8849 (void) snprintf(path
, MAXPATHLEN
, "/%s/%s", altroot
,
8850 SMF_UPGRADEDATALINK_FILE
);
8853 if ((fp
= fopen(path
, "a+")) == NULL
)
8854 die("operation not supported on %s", altroot
);
8856 (void) fprintf(fp
, "/sbin/dladm ");
8857 for (i
= 0; i
< argc
; i
++) {
8859 * Directly write to the file if it is not the "-R <altroot>"
8860 * option. In which case, skip it.
8862 if (strcmp(argv
[i
], "-R") != 0)
8863 (void) fprintf(fp
, "%s ", argv
[i
]);
8867 (void) fprintf(fp
, "%s\n", SMF_DLADM_UPGRADE_MSG
);
8869 dladm_close(handle
);
8874 * Convert the string to an integer. Note that the string must not have any
8875 * trailing non-integer characters.
8878 str2int(const char *str
, int *valp
)
8884 val
= strtol(str
, &endp
, 10);
8885 if (errno
!= 0 || *endp
!= '\0')
8894 warn(const char *format
, ...)
8898 format
= gettext(format
);
8899 (void) fprintf(stderr
, "%s: warning: ", progname
);
8901 va_start(alist
, format
);
8902 (void) vfprintf(stderr
, format
, alist
);
8905 (void) putc('\n', stderr
);
8910 warn_dlerr(dladm_status_t err
, const char *format
, ...)
8913 char errmsg
[DLADM_STRSIZE
];
8915 format
= gettext(format
);
8916 (void) fprintf(stderr
, gettext("%s: warning: "), progname
);
8918 va_start(alist
, format
);
8919 (void) vfprintf(stderr
, format
, alist
);
8921 (void) fprintf(stderr
, ": %s\n", dladm_status2str(err
, errmsg
));
8925 * Also closes the dladm handle if it is not NULL.
8929 die_dlerr(dladm_status_t err
, const char *format
, ...)
8932 char errmsg
[DLADM_STRSIZE
];
8934 format
= gettext(format
);
8935 (void) fprintf(stderr
, "%s: ", progname
);
8937 va_start(alist
, format
);
8938 (void) vfprintf(stderr
, format
, alist
);
8940 (void) fprintf(stderr
, ": %s\n", dladm_status2str(err
, errmsg
));
8942 /* close dladm handle if it was opened */
8944 dladm_close(handle
);
8951 die(const char *format
, ...)
8955 format
= gettext(format
);
8956 (void) fprintf(stderr
, "%s: ", progname
);
8958 va_start(alist
, format
);
8959 (void) vfprintf(stderr
, format
, alist
);
8962 (void) putc('\n', stderr
);
8964 /* close dladm handle if it was opened */
8966 dladm_close(handle
);
8974 die("the option -%c cannot be specified more than once", opt
);
8978 die_opterr(int opt
, int opterr
, const char *usage
)
8982 die("option '-%c' requires a value\nusage: %s", opt
,
8987 die("unrecognized option '-%c'\nusage: %s", opt
,
8994 show_ether_xprop(void *arg
, dladm_ether_info_t
*eattr
)
8996 print_ether_state_t
*statep
= arg
;
8997 ether_fields_buf_t ebuf
;
9000 for (i
= CAPABLE
; i
<= PEERADV
; i
++) {
9001 bzero(&ebuf
, sizeof (ebuf
));
9002 (void) strlcpy(ebuf
.eth_ptype
, ptype
[i
],
9003 sizeof (ebuf
.eth_ptype
));
9004 (void) dladm_ether_autoneg2str(ebuf
.eth_autoneg
,
9005 sizeof (ebuf
.eth_autoneg
), eattr
, i
);
9006 (void) dladm_ether_spdx2str(ebuf
.eth_spdx
,
9007 sizeof (ebuf
.eth_spdx
), eattr
, i
);
9008 (void) dladm_ether_pause2str(ebuf
.eth_pause
,
9009 sizeof (ebuf
.eth_pause
), eattr
, i
);
9010 (void) strlcpy(ebuf
.eth_rem_fault
,
9011 (eattr
->lei_attr
[i
].le_fault
? "fault" : "none"),
9012 sizeof (ebuf
.eth_rem_fault
));
9013 ofmt_print(statep
->es_ofmt
, &ebuf
);
9019 link_is_ether(const char *link
, datalink_id_t
*linkid
)
9022 datalink_class_t
class;
9024 if (dladm_name2info(handle
, link
, linkid
, NULL
, &class, &media
) ==
9026 if (class == DATALINK_CLASS_PHYS
&& media
== DL_ETHER
)
9033 * default output callback function that, when invoked,
9034 * prints string which is offset by ofmt_arg->ofmt_id within buf.
9037 print_default_cb(ofmt_arg_t
*ofarg
, char *buf
, uint_t bufsize
)
9041 value
= (char *)ofarg
->ofmt_cbarg
+ ofarg
->ofmt_id
;
9042 (void) strlcpy(buf
, value
, bufsize
);
9047 dladm_ofmt_check(ofmt_status_t oferr
, boolean_t parsable
,
9050 char buf
[OFMT_BUFSIZE
];
9052 if (oferr
== OFMT_SUCCESS
)
9054 (void) ofmt_strerror(ofmt
, oferr
, buf
, sizeof (buf
));
9056 * All errors are considered fatal in parsable mode.
9057 * NOMEM errors are always fatal, regardless of mode.
9058 * For other errors, we print diagnostics in human-readable
9059 * mode and processs what we can.
9061 if (parsable
|| oferr
== OFMT_ENOFIELDS
) {
9070 * Called from the walker dladm_walk_datalink_id() for each IB partition to
9071 * display IB partition specific information.
9073 static dladm_status_t
9074 print_part(show_part_state_t
*state
, datalink_id_t linkid
)
9076 dladm_part_attr_t attr
;
9077 dladm_status_t status
;
9079 char part_over
[MAXLINKNAMELEN
];
9080 char part_name
[MAXLINKNAMELEN
];
9081 part_fields_buf_t pbuf
;
9082 boolean_t force_in_conf
= B_FALSE
;
9085 * Get the information about the IB partition from the partition
9086 * datlink ID 'linkid'.
9088 if ((status
= dladm_part_info(handle
, linkid
, &attr
, state
->ps_flags
))
9093 * If an IB Phys link name was provided on the command line we have
9094 * the Phys link's datalink ID in the ps_over_id field of the state
9095 * structure. Proceed only if the IB partition represented by 'linkid'
9096 * was created over Phys link denoted by ps_over_id. The
9097 * 'dia_physlinkid' field of dladm_part_attr_t represents the IB Phys
9098 * link over which the partition was created.
9100 if (state
->ps_over_id
!= DATALINK_ALL_LINKID
)
9101 if (state
->ps_over_id
!= attr
.dia_physlinkid
)
9102 return (DLADM_STATUS_OK
);
9105 * The linkid argument passed to this function is the datalink ID
9106 * of the IB Partition. Get the partitions name from this linkid.
9108 if (dladm_datalink_id2info(handle
, linkid
, NULL
, NULL
,
9109 NULL
, part_name
, sizeof (part_name
)) != DLADM_STATUS_OK
)
9110 return (DLADM_STATUS_BADARG
);
9112 bzero(part_over
, sizeof (part_over
));
9115 * The 'dia_physlinkid' field contains the datalink ID of the IB Phys
9116 * link over which the partition was created. Use this linkid to get the
9119 if (dladm_datalink_id2info(handle
, attr
.dia_physlinkid
, NULL
, NULL
,
9120 NULL
, part_over
, sizeof (part_over
)) != DLADM_STATUS_OK
)
9121 (void) sprintf(part_over
, "?");
9122 state
->ps_found
= B_TRUE
;
9125 * Read the FFORCE field from this datalink's persistent configuration
9126 * database line to determine if this datalink was created forcibly.
9127 * If this datalink is a temporary datalink, then it will not have an
9128 * entry in the persistent configuration, so check if force create flag
9129 * is set in the partition attributes.
9131 * We need this two level check since persistent partitions brought up
9132 * by up-part during boot will have force create flag always set, since
9133 * we want up-part to always succeed even if the port is currently down
9134 * or P_Key is not yet available in the subnet.
9136 if ((status
= dladm_getsnap_conf(handle
, linkid
, &conf
)) ==
9138 (void) dladm_get_conf_field(handle
, conf
, FFORCE
,
9139 &force_in_conf
, sizeof (boolean_t
));
9140 dladm_destroy_conf(handle
, conf
);
9141 } else if (status
== DLADM_STATUS_NOTFOUND
) {
9143 * for a temp link the force create flag will determine
9144 * whether it was created with force flag.
9146 force_in_conf
= ((attr
.dia_flags
& DLADM_PART_FORCE_CREATE
)
9150 (void) snprintf(pbuf
.part_link
, sizeof (pbuf
.part_link
),
9153 (void) snprintf(pbuf
.part_over
, sizeof (pbuf
.part_over
),
9156 (void) snprintf(pbuf
.part_pkey
, sizeof (pbuf
.part_pkey
),
9157 "%X", attr
.dia_pkey
);
9159 (void) get_linkstate(pbuf
.part_link
, B_TRUE
, pbuf
.part_state
);
9161 (void) snprintf(pbuf
.part_flags
, sizeof (pbuf
.part_flags
),
9162 "%c----", force_in_conf
? 'f' : '-');
9164 ofmt_print(state
->ps_ofmt
, &pbuf
);
9166 return (DLADM_STATUS_OK
);
9171 show_part(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
9173 ((show_part_state_t
*)arg
)->ps_status
= print_part(arg
, linkid
);
9174 return (DLADM_WALK_CONTINUE
);
9178 * Show the information about the IB partition objects.
9181 do_show_part(int argc
, char *argv
[], const char *use
)
9184 boolean_t l_arg
= B_FALSE
;
9185 uint32_t flags
= DLADM_OPT_ACTIVE
;
9186 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
9187 datalink_id_t over_linkid
= DATALINK_ALL_LINKID
;
9188 char over_link
[MAXLINKNAMELEN
];
9189 show_part_state_t state
;
9190 dladm_status_t status
;
9191 boolean_t o_arg
= B_FALSE
;
9192 char *fields_str
= NULL
;
9194 ofmt_status_t oferr
;
9195 uint_t ofmtflags
= 0;
9197 bzero(&state
, sizeof (state
));
9199 while ((option
= getopt_long(argc
, argv
, ":pPl:o:", show_part_lopts
,
9203 state
.ps_parsable
= B_TRUE
;
9206 flags
= DLADM_OPT_PERSIST
;
9210 * The data link ID of the IB Phys link. When this
9211 * argument is provided we list only the partition
9212 * objects created over this IB Phys link.
9214 if (strlcpy(over_link
, optarg
, MAXLINKNAMELEN
) >=
9216 die("link name too long");
9222 fields_str
= optarg
;
9225 die_opterr(optopt
, option
, use
);
9230 * Get the partition ID (optional last argument).
9232 if (optind
== (argc
- 1)) {
9233 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
9235 if (status
!= DLADM_STATUS_OK
) {
9236 die_dlerr(status
, "invalid partition link name '%s'",
9239 (void) strlcpy(state
.ps_part
, argv
[optind
], MAXLINKNAMELEN
);
9240 } else if (optind
!= argc
) {
9244 if (state
.ps_parsable
&& !o_arg
)
9245 die("-p requires -o");
9248 * If an IB Phys link name was provided as an argument, then get its
9252 status
= dladm_name2info(handle
, over_link
, &over_linkid
, NULL
,
9254 if (status
!= DLADM_STATUS_OK
) {
9255 die_dlerr(status
, "invalid link name '%s'", over_link
);
9259 state
.ps_over_id
= over_linkid
; /* IB Phys link ID */
9260 state
.ps_found
= B_FALSE
;
9261 state
.ps_flags
= flags
;
9263 if (state
.ps_parsable
)
9264 ofmtflags
|= OFMT_PARSABLE
;
9265 oferr
= ofmt_open(fields_str
, part_fields
, ofmtflags
, 0, &ofmt
);
9266 dladm_ofmt_check(oferr
, state
.ps_parsable
, ofmt
);
9267 state
.ps_ofmt
= ofmt
;
9270 * If a specific IB partition name was not provided as an argument,
9271 * walk all the datalinks and display the information for all
9272 * IB partitions. If IB Phys link was provided limit it to only
9273 * IB partitions created over that IB Phys link.
9275 if (linkid
== DATALINK_ALL_LINKID
) {
9276 (void) dladm_walk_datalink_id(show_part
, handle
, &state
,
9277 DATALINK_CLASS_PART
, DATALINK_ANY_MEDIATYPE
, flags
);
9279 (void) show_part(handle
, linkid
, &state
);
9280 if (state
.ps_status
!= DLADM_STATUS_OK
) {
9282 die_dlerr(state
.ps_status
, "failed to show IB partition"
9283 " '%s'", state
.ps_part
);
9291 * Called from the walker dladm_walk_datalink_id() for each IB Phys link to
9292 * display IB specific information for these Phys links.
9294 static dladm_status_t
9295 print_ib(show_ib_state_t
*state
, datalink_id_t phys_linkid
)
9297 dladm_ib_attr_t attr
;
9298 dladm_status_t status
;
9299 char linkname
[MAXLINKNAMELEN
];
9300 char pkeystr
[MAXPKEYLEN
];
9302 ib_fields_buf_t ibuf
;
9304 bzero(&attr
, sizeof (attr
));
9307 * Get the attributes of the IB Phys link from active/Persistent config
9308 * based on the flag passed.
9310 if ((status
= dladm_ib_info(handle
, phys_linkid
, &attr
,
9311 state
->is_flags
)) != DLADM_STATUS_OK
)
9314 if ((state
->is_link_id
!= DATALINK_ALL_LINKID
) && (state
->is_link_id
9315 != attr
.dia_physlinkid
)) {
9316 dladm_free_ib_info(&attr
);
9317 return (DLADM_STATUS_OK
);
9321 * Get the data link name for the phys_linkid. If we are doing show-ib
9322 * for all IB Phys links, we have only the datalink IDs not the
9325 if (dladm_datalink_id2info(handle
, phys_linkid
, NULL
, NULL
, NULL
,
9326 linkname
, MAXLINKNAMELEN
) != DLADM_STATUS_OK
)
9329 (void) snprintf(ibuf
.ib_link
, sizeof (ibuf
.ib_link
),
9332 (void) snprintf(ibuf
.ib_portnum
, sizeof (ibuf
.ib_portnum
),
9333 "%d", attr
.dia_portnum
);
9335 (void) snprintf(ibuf
.ib_hcaguid
, sizeof (ibuf
.ib_hcaguid
),
9336 "%llX", attr
.dia_hca_guid
);
9338 (void) snprintf(ibuf
.ib_portguid
, sizeof (ibuf
.ib_portguid
),
9339 "%llX", attr
.dia_port_guid
);
9341 (void) get_linkstate(linkname
, B_TRUE
, ibuf
.ib_state
);
9344 * Create a comma separated list of pkeys from the pkey table returned
9345 * by the IP over IB driver instance.
9347 bzero(ibuf
.ib_pkeys
, attr
.dia_port_pkey_tbl_sz
* sizeof (ib_pkey_t
));
9348 for (i
= 0; i
< attr
.dia_port_pkey_tbl_sz
; i
++) {
9349 if (attr
.dia_port_pkeys
[i
] != IB_PKEY_INVALID_FULL
&&
9350 attr
.dia_port_pkeys
[i
] != IB_PKEY_INVALID_LIMITED
) {
9352 (void) snprintf(pkeystr
, MAXPKEYLEN
, "%X",
9353 attr
.dia_port_pkeys
[i
]);
9355 (void) snprintf(pkeystr
, MAXPKEYLEN
, ",%X",
9356 attr
.dia_port_pkeys
[i
]);
9357 (void) strlcat(ibuf
.ib_pkeys
, pkeystr
, MAXPKEYSTRSZ
);
9361 dladm_free_ib_info(&attr
);
9363 ofmt_print(state
->is_ofmt
, &ibuf
);
9365 return (DLADM_STATUS_OK
);
9370 show_ib(dladm_handle_t dh
, datalink_id_t linkid
, void *arg
)
9372 ((show_ib_state_t
*)arg
)->is_status
= print_ib(arg
, linkid
);
9373 return (DLADM_WALK_CONTINUE
);
9377 * Show the properties of one/all IB Phys links. This is different from
9378 * show-phys command since this will display IB specific information about the
9379 * Phys link like, HCA GUID, PORT GUID, PKEYS active for this port etc.
9382 do_show_ib(int argc
, char *argv
[], const char *use
)
9385 uint32_t flags
= DLADM_OPT_ACTIVE
;
9386 datalink_id_t linkid
= DATALINK_ALL_LINKID
;
9387 show_ib_state_t state
;
9388 dladm_status_t status
;
9389 boolean_t o_arg
= B_FALSE
;
9390 char *fields_str
= NULL
;
9392 ofmt_status_t oferr
;
9393 uint_t ofmtflags
= 0;
9395 bzero(&state
, sizeof (state
));
9397 while ((option
= getopt_long(argc
, argv
, ":po:", show_lopts
,
9401 state
.is_parsable
= B_TRUE
;
9405 fields_str
= optarg
;
9408 die_opterr(optopt
, option
, use
);
9412 /* get IB Phys link ID (optional last argument) */
9413 if (optind
== (argc
- 1)) {
9414 status
= dladm_name2info(handle
, argv
[optind
], &linkid
, NULL
,
9416 if (status
!= DLADM_STATUS_OK
) {
9417 die_dlerr(status
, "invalid IB port name '%s'",
9420 (void) strlcpy(state
.is_link
, argv
[optind
], MAXLINKNAMELEN
);
9421 } else if (optind
!= argc
) {
9425 if (state
.is_parsable
&& !o_arg
)
9426 die("-p requires -o");
9429 * linkid is the data link ID of the IB Phys link. By default it will
9430 * be DATALINK_ALL_LINKID.
9432 state
.is_link_id
= linkid
;
9433 state
.is_flags
= flags
;
9435 if (state
.is_parsable
)
9436 ofmtflags
|= OFMT_PARSABLE
;
9437 oferr
= ofmt_open(fields_str
, ib_fields
, ofmtflags
, 0, &ofmt
);
9438 dladm_ofmt_check(oferr
, state
.is_parsable
, ofmt
);
9439 state
.is_ofmt
= ofmt
;
9442 * If we are going to display the information for all IB Phys links
9443 * then we'll walk through all the datalinks for datalinks of Phys
9444 * class and media type IB.
9446 if (linkid
== DATALINK_ALL_LINKID
) {
9447 (void) dladm_walk_datalink_id(show_ib
, handle
, &state
,
9448 DATALINK_CLASS_PHYS
, DL_IB
, flags
);
9451 * We need to display the information only for the IB phys link
9452 * linkid. Call show_ib for this link.
9454 (void) show_ib(handle
, linkid
, &state
);
9455 if (state
.is_status
!= DLADM_STATUS_OK
) {
9457 die_dlerr(state
.is_status
, "failed to show IB Phys link"
9458 " '%s'", state
.is_link
);
9465 * Create an IP over Infiniband partition object over an IB Phys link. The IB
9466 * Phys link is associated with an Infiniband HCA port. The IB partition object
9467 * is created over a port, pkey combination. This partition object represents
9468 * an instance of IP over IB interface.
9472 do_create_part(int argc
, char *argv
[], const char *use
)
9475 int flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
9478 char *altroot
= NULL
;
9479 datalink_id_t physlinkid
= 0;
9480 datalink_id_t partlinkid
= 0;
9481 unsigned long opt_pkey
;
9484 char propstr
[DLADM_STRSIZE
];
9485 dladm_arg_list_t
*proplist
= NULL
;
9488 while ((option
= getopt_long(argc
, argv
, ":tfl:P:R:p:",
9489 part_lopts
, NULL
)) != -1) {
9493 * Create a temporary IB partition object. This
9494 * instance is not entered into the persistent database
9495 * so it will not be recreated automatically on a
9498 flags
&= ~DLADM_OPT_PERSIST
;
9502 * The IB phys link over which the partition object will
9511 (void) strlcat(propstr
, optarg
, DLADM_STRSIZE
);
9512 if (strlcat(propstr
, ",", DLADM_STRSIZE
) >=
9514 die("property list too long '%s'", propstr
);
9518 * The P_Key for the port, pkey tuple of the partition
9519 * object. This P_Key should exist in the IB subnet.
9520 * The partition creation for a non-existent P_Key will
9521 * fail unless the -f option is used.
9523 * The P_Key is expected to be a hexadecimal number.
9525 opt_pkey
= strtoul(optarg
, &endp
, 16);
9526 if (errno
== ERANGE
|| opt_pkey
> USHRT_MAX
||
9528 die("Invalid pkey");
9530 pkey
= (ib_pkey_t
)opt_pkey
;
9533 flags
|= DLADM_OPT_FORCE
;
9536 die_opterr(optopt
, option
, use
);
9541 /* check required options */
9545 /* the partition name is a required operand */
9546 if (optind
!= (argc
- 1))
9549 pname
= argv
[argc
- 1];
9552 * Verify that the partition object's name is in the valid link name
9555 if (!dladm_valid_linkname(pname
))
9556 die("Invalid link name '%s'", pname
);
9558 /* pkey is a mandatory argument */
9562 if (altroot
!= NULL
)
9563 altroot_cmd(altroot
, argc
, argv
);
9566 * Get the data link id of the IB Phys link over which we will be
9567 * creating partition object.
9569 if (dladm_name2info(handle
, l_arg
,
9570 &physlinkid
, NULL
, NULL
, NULL
) != DLADM_STATUS_OK
)
9571 die("invalid link name '%s'", l_arg
);
9574 * parse the property list provided with -p option.
9576 if (dladm_parse_link_props(propstr
, &proplist
, B_FALSE
)
9578 die("invalid IB partition property");
9581 * Call the library routine to create the partition object.
9583 status
= dladm_part_create(handle
, physlinkid
, pkey
, flags
, pname
,
9584 &partlinkid
, proplist
);
9585 if (status
!= DLADM_STATUS_OK
)
9587 "partition %x creation over %s failed", pkey
, l_arg
);
9591 * Delete an IP over Infiniband partition object. The partition object should
9592 * be unplumbed before attempting the delete.
9595 do_delete_part(int argc
, char *argv
[], const char *use
)
9597 int option
, flags
= DLADM_OPT_ACTIVE
| DLADM_OPT_PERSIST
;
9599 char *altroot
= NULL
;
9600 datalink_id_t partid
;
9603 while ((option
= getopt_long(argc
, argv
, "R:t", part_lopts
,
9607 flags
&= ~DLADM_OPT_PERSIST
;
9613 die_opterr(optopt
, option
, use
);
9617 /* get partition name (required last argument) */
9618 if (optind
!= (argc
- 1))
9621 if (altroot
!= NULL
)
9622 altroot_cmd(altroot
, argc
, argv
);
9625 * Get the data link id of the partition object given the partition
9628 status
= dladm_name2info(handle
, argv
[optind
], &partid
, NULL
, NULL
,
9630 if (status
!= DLADM_STATUS_OK
)
9631 die("invalid link name '%s'", argv
[optind
]);
9634 * Call the library routine to delete the IB partition. This will
9635 * result in the IB partition object and all its resources getting
9638 status
= dladm_part_delete(handle
, partid
, flags
);
9639 if (status
!= DLADM_STATUS_OK
)
9640 die_dlerr(status
, "%s: partition deletion failed",
9645 * Bring up all or one IB partition already present in the persistent database
9646 * but not active yet.
9648 * This sub-command is used during the system boot up to bring up all IB
9649 * partitions present in the persistent database. This is similar to a
9650 * create partition except that, the partitions are always created even if the
9651 * HCA port is down or P_Key is not present in the IB subnet. This is similar
9652 * to using the 'force' option while creating the partition except that the 'f'
9653 * flag will be set in the flags field only if the create-part for this command
9654 * was called with '-f' option.
9658 do_up_part(int argc
, char *argv
[], const char *use
)
9660 datalink_id_t partid
= DATALINK_ALL_LINKID
;
9661 dladm_status_t status
;
9664 * If a partition name was passed as an argument, get its data link
9665 * id. By default we'll attempt to bring up all IB partition data
9669 status
= dladm_name2info(handle
, argv
[argc
- 1], &partid
, NULL
,
9671 if (status
!= DLADM_STATUS_OK
)
9673 } else if (argc
> 2) {
9677 (void) dladm_part_up(handle
, partid
, 0);