xref: /titanic_41/usr/src/lib/libdladm/common/libdllink.c (revision a31148363f598def767ac48c5d82e1572e44b935)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <strings.h>
33 #include <sys/stat.h>
34 #include <sys/dld.h>
35 #include <sys/vlan.h>
36 #include <zone.h>
37 #include <librcm.h>
38 #include <libdlpi.h>
39 #include <libdevinfo.h>
40 #include <libdlaggr.h>
41 #include <libdlvlan.h>
42 #include <libdlvnic.h>
43 #include <libdllink.h>
44 #include <libdlmgmt.h>
45 #include <libdladm_impl.h>
46 #include <libinetutil.h>
47 
48 /*
49  * Return the attributes of the specified datalink from the DLD driver.
50  */
51 static dladm_status_t
52 i_dladm_info(dladm_handle_t handle, const datalink_id_t linkid,
53     dladm_attr_t *dap)
54 {
55 	dld_ioc_attr_t	dia;
56 
57 	dia.dia_linkid = linkid;
58 
59 	if (ioctl(dladm_dld_fd(handle), DLDIOC_ATTR, &dia) < 0)
60 		return (dladm_errno2status(errno));
61 
62 	dap->da_max_sdu = dia.dia_max_sdu;
63 
64 	return (DLADM_STATUS_OK);
65 }
66 
67 static dladm_status_t
68 dladm_usagelog(dladm_handle_t handle, dladm_logtype_t type,
69     dld_ioc_usagelog_t *log_info)
70 {
71 	if (type == DLADM_LOGTYPE_FLOW)
72 		log_info->ul_type = MAC_LOGTYPE_FLOW;
73 	else
74 		log_info->ul_type = MAC_LOGTYPE_LINK;
75 
76 	if (ioctl(dladm_dld_fd(handle), DLDIOC_USAGELOG, log_info) < 0)
77 		return (DLADM_STATUS_IOERR);
78 
79 	return (DLADM_STATUS_OK);
80 }
81 
82 dladm_status_t
83 dladm_start_usagelog(dladm_handle_t handle, dladm_logtype_t type,
84     uint_t interval)
85 {
86 	dld_ioc_usagelog_t	log_info;
87 
88 	log_info.ul_onoff = B_TRUE;
89 	log_info.ul_interval = interval;
90 
91 	return (dladm_usagelog(handle, type, &log_info));
92 }
93 
94 dladm_status_t
95 dladm_stop_usagelog(dladm_handle_t handle, dladm_logtype_t type)
96 {
97 	dld_ioc_usagelog_t	log_info;
98 
99 	log_info.ul_onoff = B_FALSE;
100 	log_info.ul_interval = 0;
101 
102 	return (dladm_usagelog(handle, type, &log_info));
103 }
104 
105 struct i_dladm_walk_arg {
106 	dladm_walkcb_t *fn;
107 	void *arg;
108 };
109 
110 static int
111 i_dladm_walk(dladm_handle_t handle, datalink_id_t linkid, void *arg)
112 {
113 	struct i_dladm_walk_arg *walk_arg = arg;
114 	char link[MAXLINKNAMELEN];
115 
116 	if (dladm_datalink_id2info(handle, linkid, NULL, NULL, NULL, link,
117 	    sizeof (link)) == DLADM_STATUS_OK) {
118 		return (walk_arg->fn(link, walk_arg->arg));
119 	}
120 
121 	return (DLADM_WALK_CONTINUE);
122 }
123 
124 /*
125  * Walk all datalinks.
126  */
127 dladm_status_t
128 dladm_walk(dladm_walkcb_t *fn, dladm_handle_t handle, void *arg,
129     datalink_class_t class, datalink_media_t dmedia, uint32_t flags)
130 {
131 	struct i_dladm_walk_arg walk_arg;
132 
133 	walk_arg.fn = fn;
134 	walk_arg.arg = arg;
135 	return (dladm_walk_datalink_id(i_dladm_walk, handle, &walk_arg,
136 	    class, dmedia, flags));
137 }
138 
139 #define	MAXGRPPERLINK	64
140 
141 int
142 dladm_walk_hwgrp(dladm_handle_t handle, datalink_id_t linkid, void *arg,
143     boolean_t (*fn)(void *, dladm_hwgrp_attr_t *))
144 {
145 	int		bufsize, ret;
146 	int		nhwgrp = MAXGRPPERLINK;
147 	dld_ioc_hwgrpget_t *iomp = NULL;
148 
149 	bufsize = sizeof (dld_ioc_hwgrpget_t) +
150 	    nhwgrp * sizeof (dld_hwgrpinfo_t);
151 
152 	if ((iomp = (dld_ioc_hwgrpget_t *)calloc(1, bufsize)) == NULL)
153 		return (-1);
154 
155 	iomp->dih_size = nhwgrp * sizeof (dld_hwgrpinfo_t);
156 	iomp->dih_linkid = linkid;
157 
158 	ret = ioctl(dladm_dld_fd(handle), DLDIOC_GETHWGRP, iomp);
159 	if (ret == 0) {
160 		int			i;
161 		int			j;
162 		dld_hwgrpinfo_t 	*dhip;
163 		dladm_hwgrp_attr_t	attr;
164 
165 		dhip = (dld_hwgrpinfo_t *)(iomp + 1);
166 		for (i = 0; i < iomp->dih_n_groups; i++) {
167 			bzero(&attr, sizeof (attr));
168 
169 			(void) strlcpy(attr.hg_link_name,
170 			    dhip->dhi_link_name, sizeof (attr.hg_link_name));
171 			attr.hg_grp_num = dhip->dhi_grp_num;
172 			attr.hg_grp_type = dhip->dhi_grp_type;
173 			attr.hg_n_rings = dhip->dhi_n_rings;
174 			for (j = 0; j < dhip->dhi_n_rings; j++)
175 				attr.hg_rings[j] = dhip->dhi_rings[j];
176 			dladm_sort_index_list(attr.hg_rings, attr.hg_n_rings);
177 			attr.hg_n_clnts = dhip->dhi_n_clnts;
178 			(void) strlcpy(attr.hg_client_names,
179 			    dhip->dhi_clnts, sizeof (attr.hg_client_names));
180 
181 			if (!(*fn)(arg, &attr))
182 				break;
183 			dhip++;
184 		}
185 	}
186 	free(iomp);
187 	return (ret);
188 }
189 
190 /*
191  * Invoke the specified callback for each MAC address entry defined on
192  * the specified device.
193  */
194 int
195 dladm_walk_macaddr(dladm_handle_t handle, datalink_id_t linkid, void *arg,
196     boolean_t (*fn)(void *, dladm_macaddr_attr_t *))
197 {
198 	int		bufsize, ret;
199 	int		nmacaddr = 1024;
200 	dld_ioc_macaddrget_t *iomp = NULL;
201 
202 	bufsize = sizeof (dld_ioc_macaddrget_t) +
203 	    nmacaddr * sizeof (dld_macaddrinfo_t);
204 
205 	if ((iomp = (dld_ioc_macaddrget_t *)calloc(1, bufsize)) == NULL)
206 		return (-1);
207 
208 	iomp->dig_size = nmacaddr * sizeof (dld_macaddrinfo_t);
209 	iomp->dig_linkid = linkid;
210 
211 	ret = ioctl(dladm_dld_fd(handle), DLDIOC_MACADDRGET, iomp);
212 	if (ret == 0) {
213 		int i;
214 		dld_macaddrinfo_t *dmip;
215 		dladm_macaddr_attr_t attr;
216 
217 		dmip = (dld_macaddrinfo_t *)(iomp + 1);
218 		for (i = 0; i < iomp->dig_count; i++) {
219 			bzero(&attr, sizeof (attr));
220 
221 			attr.ma_slot = dmip->dmi_slot;
222 			attr.ma_flags = 0;
223 			if (dmip->dmi_flags & DLDIOCMACADDR_USED)
224 				attr.ma_flags |= DLADM_MACADDR_USED;
225 			bcopy(dmip->dmi_addr, attr.ma_addr,
226 			    dmip->dmi_addrlen);
227 			attr.ma_addrlen = dmip->dmi_addrlen;
228 			(void) strlcpy(attr.ma_client_name,
229 			    dmip->dmi_client_name, MAXNAMELEN);
230 			attr.ma_client_linkid = dmip->dma_client_linkid;
231 
232 			if (!(*fn)(arg, &attr))
233 				break;
234 			dmip++;
235 		}
236 	}
237 	free(iomp);
238 	return (ret);
239 }
240 
241 /*
242  * These routines are used by administration tools such as dladm(1M) to
243  * iterate through the list of MAC interfaces
244  */
245 
246 typedef struct dladm_mac_dev {
247 	char			dm_name[MAXNAMELEN];
248 	struct dladm_mac_dev    *dm_next;
249 } dladm_mac_dev_t;
250 
251 typedef struct macadm_walk {
252 	dladm_mac_dev_t	 *dmd_dev_list;
253 } dladm_mac_walk_t;
254 
255 /*
256  * Local callback invoked for each DDI_NT_NET node.
257  */
258 /* ARGSUSED */
259 static int
260 i_dladm_mac_walk(di_node_t node, di_minor_t minor, void *arg)
261 {
262 	dladm_mac_walk_t	*dmwp = arg;
263 	dladm_mac_dev_t		*dmdp = dmwp->dmd_dev_list;
264 	dladm_mac_dev_t		**last_dmdp = &dmwp->dmd_dev_list;
265 	char			mac[MAXNAMELEN];
266 
267 	(void) snprintf(mac, MAXNAMELEN, "%s%d",
268 	    di_driver_name(node), di_instance(node));
269 
270 	/*
271 	 * Skip aggregations.
272 	 */
273 	if (strcmp("aggr", di_driver_name(node)) == 0)
274 		return (DI_WALK_CONTINUE);
275 
276 	/*
277 	 * Skip softmacs.
278 	 */
279 	if (strcmp("softmac", di_driver_name(node)) == 0)
280 		return (DI_WALK_CONTINUE);
281 
282 	while (dmdp) {
283 		/*
284 		 * Skip duplicates.
285 		 */
286 		if (strcmp(dmdp->dm_name, mac) == 0)
287 			return (DI_WALK_CONTINUE);
288 
289 		last_dmdp = &dmdp->dm_next;
290 		dmdp = dmdp->dm_next;
291 	}
292 
293 	if ((dmdp = malloc(sizeof (*dmdp))) == NULL)
294 		return (DI_WALK_CONTINUE);
295 
296 	(void) strlcpy(dmdp->dm_name, mac, MAXNAMELEN);
297 	dmdp->dm_next = NULL;
298 	*last_dmdp = dmdp;
299 
300 	return (DI_WALK_CONTINUE);
301 }
302 
303 /*
304  * Invoke the specified callback for each DDI_NT_NET node.
305  */
306 dladm_status_t
307 dladm_mac_walk(int (*fn)(const char *, void *arg), void *arg)
308 {
309 	di_node_t		root;
310 	dladm_mac_walk_t	dmw;
311 	dladm_mac_dev_t		*dmdp, *next;
312 	boolean_t		done = B_FALSE;
313 
314 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL)
315 		return (dladm_errno2status(errno));
316 
317 	dmw.dmd_dev_list = NULL;
318 
319 	(void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, &dmw,
320 	    i_dladm_mac_walk);
321 
322 	di_fini(root);
323 
324 	dmdp = dmw.dmd_dev_list;
325 	for (dmdp = dmw.dmd_dev_list; dmdp != NULL; dmdp = next) {
326 		next = dmdp->dm_next;
327 		if (!done &&
328 		    ((*fn)(dmdp->dm_name, arg) == DLADM_WALK_TERMINATE)) {
329 			done = B_TRUE;
330 		}
331 		free(dmdp);
332 	}
333 
334 	return (DLADM_STATUS_OK);
335 }
336 
337 /*
338  * Get the current attributes of the specified datalink.
339  */
340 dladm_status_t
341 dladm_info(dladm_handle_t handle, datalink_id_t linkid, dladm_attr_t *dap)
342 {
343 	return (i_dladm_info(handle, linkid, dap));
344 }
345 
346 const char *
347 dladm_linkstate2str(link_state_t state, char *buf)
348 {
349 	const char	*s;
350 
351 	switch (state) {
352 	case LINK_STATE_UP:
353 		s = "up";
354 		break;
355 	case LINK_STATE_DOWN:
356 		s = "down";
357 		break;
358 	default:
359 		s = "unknown";
360 		break;
361 	}
362 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
363 	return (buf);
364 }
365 
366 const char *
367 dladm_linkduplex2str(link_duplex_t duplex, char *buf)
368 {
369 	const char	*s;
370 
371 	switch (duplex) {
372 	case LINK_DUPLEX_FULL:
373 		s = "full";
374 		break;
375 	case LINK_DUPLEX_HALF:
376 		s = "half";
377 		break;
378 	default:
379 		s = "unknown";
380 		break;
381 	}
382 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
383 	return (buf);
384 }
385 
386 /*
387  * Case 1: rename an existing link1 to a link2 that does not exist.
388  * Result: <linkid1, link2>
389  */
390 static dladm_status_t
391 i_dladm_rename_link_c1(dladm_handle_t handle, datalink_id_t linkid1,
392     const char *link1, const char *link2, uint32_t flags)
393 {
394 	dld_ioc_rename_t	dir;
395 	dladm_status_t		status = DLADM_STATUS_OK;
396 
397 	/*
398 	 * Link is currently available. Check to see whether anything is
399 	 * holding this link to prevent a rename operation.
400 	 */
401 	if (flags & DLADM_OPT_ACTIVE) {
402 		dir.dir_linkid1 = linkid1;
403 		dir.dir_linkid2 = DATALINK_INVALID_LINKID;
404 		(void) strlcpy(dir.dir_link, link2, MAXLINKNAMELEN);
405 
406 		if (ioctl(dladm_dld_fd(handle), DLDIOC_RENAME, &dir) < 0) {
407 			status = dladm_errno2status(errno);
408 			return (status);
409 		}
410 	}
411 
412 	status = dladm_remap_datalink_id(handle, linkid1, link2);
413 	if (status != DLADM_STATUS_OK && (flags & DLADM_OPT_ACTIVE)) {
414 		(void) strlcpy(dir.dir_link, link1, MAXLINKNAMELEN);
415 		(void) ioctl(dladm_dld_fd(handle), DLDIOC_RENAME, &dir);
416 	}
417 	return (status);
418 }
419 
420 typedef struct link_hold_arg_s {
421 	datalink_id_t	linkid;
422 	datalink_id_t	holder;
423 	uint32_t	flags;
424 } link_hold_arg_t;
425 
426 static int
427 i_dladm_aggr_link_hold(dladm_handle_t handle, datalink_id_t aggrid, void *arg)
428 {
429 	link_hold_arg_t		*hold_arg = arg;
430 	dladm_aggr_grp_attr_t	ginfo;
431 	dladm_status_t		status;
432 	int			i;
433 
434 	status = dladm_aggr_info(handle, aggrid, &ginfo, hold_arg->flags);
435 	if (status != DLADM_STATUS_OK)
436 		return (DLADM_WALK_CONTINUE);
437 
438 	for (i = 0; i < ginfo.lg_nports; i++) {
439 		if (ginfo.lg_ports[i].lp_linkid == hold_arg->linkid) {
440 			hold_arg->holder = aggrid;
441 			return (DLADM_WALK_TERMINATE);
442 		}
443 	}
444 	return (DLADM_WALK_CONTINUE);
445 }
446 
447 static int
448 i_dladm_vlan_link_hold(dladm_handle_t handle, datalink_id_t vlanid, void *arg)
449 {
450 	link_hold_arg_t		*hold_arg = arg;
451 	dladm_vlan_attr_t	vinfo;
452 	dladm_status_t		status;
453 
454 	status = dladm_vlan_info(handle, vlanid, &vinfo, hold_arg->flags);
455 	if (status != DLADM_STATUS_OK)
456 		return (DLADM_WALK_CONTINUE);
457 
458 	if (vinfo.dv_linkid == hold_arg->linkid) {
459 		hold_arg->holder = vlanid;
460 		return (DLADM_WALK_TERMINATE);
461 	}
462 	return (DLADM_WALK_CONTINUE);
463 }
464 
465 /*
466  * Case 2: rename an available physical link link1 to a REMOVED physical link
467  *     link2.  As a result, link1 directly inherits all datalinks configured
468  *     over link2 (linkid2).
469  * Result: <linkid2, link2, link1_phymaj, link1_phyinst, link1_devname,
470  *     link2_other_attr>
471  */
472 static dladm_status_t
473 i_dladm_rename_link_c2(dladm_handle_t handle, datalink_id_t linkid1,
474     datalink_id_t linkid2)
475 {
476 	rcm_handle_t		*rcm_hdl = NULL;
477 	nvlist_t		*nvl = NULL;
478 	link_hold_arg_t		arg;
479 	dld_ioc_rename_t	dir;
480 	dladm_conf_t		conf1, conf2;
481 	char			devname[MAXLINKNAMELEN];
482 	uint64_t		phymaj, phyinst;
483 	dladm_status_t		status = DLADM_STATUS_OK;
484 
485 	/*
486 	 * First check if linkid1 is associated with any persistent
487 	 * aggregations or VLANs. If yes, return BUSY.
488 	 */
489 	arg.linkid = linkid1;
490 	arg.holder = DATALINK_INVALID_LINKID;
491 	arg.flags = DLADM_OPT_PERSIST;
492 	(void) dladm_walk_datalink_id(i_dladm_aggr_link_hold, handle, &arg,
493 	    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, DLADM_OPT_PERSIST);
494 	if (arg.holder != DATALINK_INVALID_LINKID)
495 		return (DLADM_STATUS_LINKBUSY);
496 
497 	arg.flags = DLADM_OPT_PERSIST;
498 	(void) dladm_walk_datalink_id(i_dladm_vlan_link_hold, handle, &arg,
499 	    DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE, DLADM_OPT_PERSIST);
500 	if (arg.holder != DATALINK_INVALID_LINKID)
501 		return (DLADM_STATUS_LINKBUSY);
502 
503 	/*
504 	 * Send DLDIOC_RENAME to request to rename link1's linkid to
505 	 * be linkid2. This will check whether link1 is used by any
506 	 * aggregations or VLANs, or is held by any application. If yes,
507 	 * return failure.
508 	 */
509 	dir.dir_linkid1 = linkid1;
510 	dir.dir_linkid2 = linkid2;
511 	if (ioctl(dladm_dld_fd(handle), DLDIOC_RENAME, &dir) < 0)
512 		status = dladm_errno2status(errno);
513 
514 	if (status != DLADM_STATUS_OK) {
515 		return (status);
516 	}
517 
518 	/*
519 	 * Now change the phymaj, phyinst and devname associated with linkid1
520 	 * to be associated with linkid2. Before doing that, the old active
521 	 * linkprop of linkid1 should be deleted.
522 	 */
523 	(void) dladm_set_linkprop(handle, linkid1, NULL, NULL, 0,
524 	    DLADM_OPT_ACTIVE);
525 
526 	if (((status = dladm_read_conf(handle, linkid1, &conf1)) !=
527 	    DLADM_STATUS_OK) ||
528 	    ((status = dladm_get_conf_field(handle, conf1, FDEVNAME, devname,
529 	    MAXLINKNAMELEN)) != DLADM_STATUS_OK) ||
530 	    ((status = dladm_get_conf_field(handle, conf1, FPHYMAJ, &phymaj,
531 	    sizeof (uint64_t))) != DLADM_STATUS_OK) ||
532 	    ((status = dladm_get_conf_field(handle, conf1, FPHYINST, &phyinst,
533 	    sizeof (uint64_t))) != DLADM_STATUS_OK) ||
534 	    ((status = dladm_read_conf(handle, linkid2, &conf2)) !=
535 	    DLADM_STATUS_OK)) {
536 		dir.dir_linkid1 = linkid2;
537 		dir.dir_linkid2 = linkid1;
538 		(void) dladm_init_linkprop(handle, linkid1, B_FALSE);
539 		(void) ioctl(dladm_dld_fd(handle), DLDIOC_RENAME, &dir);
540 		return (status);
541 	}
542 
543 	dladm_destroy_conf(handle, conf1);
544 	(void) dladm_set_conf_field(handle, conf2, FDEVNAME, DLADM_TYPE_STR,
545 	    devname);
546 	(void) dladm_set_conf_field(handle, conf2, FPHYMAJ, DLADM_TYPE_UINT64,
547 	    &phymaj);
548 	(void) dladm_set_conf_field(handle, conf2, FPHYINST,
549 	    DLADM_TYPE_UINT64, &phyinst);
550 	(void) dladm_write_conf(handle, conf2);
551 	dladm_destroy_conf(handle, conf2);
552 
553 	/*
554 	 * Delete link1 and mark link2 up.
555 	 */
556 	(void) dladm_remove_conf(handle, linkid1);
557 	(void) dladm_destroy_datalink_id(handle, linkid1, DLADM_OPT_ACTIVE |
558 	    DLADM_OPT_PERSIST);
559 	(void) dladm_up_datalink_id(handle, linkid2);
560 
561 	/*
562 	 * Now generate the RCM_RESOURCE_LINK_NEW sysevent which can be
563 	 * consumed by the RCM framework to restore all the datalink and
564 	 * IP configuration.
565 	 */
566 	status = DLADM_STATUS_FAILED;
567 	if ((nvlist_alloc(&nvl, 0, 0) != 0) ||
568 	    (nvlist_add_uint64(nvl, RCM_NV_LINKID, linkid2) != 0)) {
569 		goto done;
570 	}
571 
572 	if (rcm_alloc_handle(NULL, 0, NULL, &rcm_hdl) != RCM_SUCCESS)
573 		goto done;
574 
575 	if (rcm_notify_event(rcm_hdl, RCM_RESOURCE_LINK_NEW, 0, nvl, NULL) ==
576 	    RCM_SUCCESS) {
577 		status = DLADM_STATUS_OK;
578 	}
579 
580 done:
581 	if (rcm_hdl != NULL)
582 		(void) rcm_free_handle(rcm_hdl);
583 	if (nvl != NULL)
584 		nvlist_free(nvl);
585 	return (status);
586 }
587 
588 /*
589  * case 3: rename a non-existent link to a REMOVED physical link.
590  * Set the removed physical link's device name to link1, so that
591  * when link1 attaches, it inherits all the link configuration of
592  * the removed physical link.
593  */
594 static dladm_status_t
595 i_dladm_rename_link_c3(dladm_handle_t handle, const char *link1,
596     datalink_id_t linkid2)
597 {
598 	dladm_conf_t	conf;
599 	dladm_status_t	status;
600 
601 	if (!dladm_valid_linkname(link1))
602 		return (DLADM_STATUS_LINKINVAL);
603 
604 	status = dladm_read_conf(handle, linkid2, &conf);
605 	if (status != DLADM_STATUS_OK)
606 		goto done;
607 
608 	if ((status = dladm_set_conf_field(handle, conf, FDEVNAME,
609 	    DLADM_TYPE_STR, link1)) == DLADM_STATUS_OK) {
610 		status = dladm_write_conf(handle, conf);
611 	}
612 
613 	dladm_destroy_conf(handle, conf);
614 
615 done:
616 	return (status);
617 }
618 
619 dladm_status_t
620 dladm_rename_link(dladm_handle_t handle, const char *link1, const char *link2)
621 {
622 	datalink_id_t		linkid1 = DATALINK_INVALID_LINKID;
623 	datalink_id_t		linkid2 = DATALINK_INVALID_LINKID;
624 	uint32_t		flags1, flags2;
625 	datalink_class_t	class1, class2;
626 	uint32_t		media1, media2;
627 	boolean_t		remphy2 = B_FALSE;
628 	dladm_status_t  	status;
629 
630 	(void) dladm_name2info(handle, link1, &linkid1, &flags1, &class1,
631 	    &media1);
632 	if ((dladm_name2info(handle, link2, &linkid2, &flags2, &class2,
633 	    &media2) == DLADM_STATUS_OK) && (class2 == DATALINK_CLASS_PHYS) &&
634 	    (flags2 == DLADM_OPT_PERSIST)) {
635 		/*
636 		 * see whether link2 is a removed physical link.
637 		 */
638 		remphy2 = B_TRUE;
639 	}
640 
641 	if (linkid1 != DATALINK_INVALID_LINKID) {
642 		if (linkid2 == DATALINK_INVALID_LINKID) {
643 			/*
644 			 * case 1: rename an existing link to a link that
645 			 * does not exist.
646 			 */
647 			status = i_dladm_rename_link_c1(handle, linkid1, link1,
648 			    link2, flags1);
649 		} else if (remphy2) {
650 			/*
651 			 * case 2: rename an available link to a REMOVED
652 			 * physical link. Return failure if link1 is not
653 			 * an active physical link.
654 			 */
655 			if ((class1 != class2) || (media1 != media2) ||
656 			    !(flags1 & DLADM_OPT_ACTIVE)) {
657 				status = DLADM_STATUS_BADARG;
658 			} else {
659 				status = i_dladm_rename_link_c2(handle, linkid1,
660 				    linkid2);
661 			}
662 		} else {
663 			status = DLADM_STATUS_EXIST;
664 		}
665 	} else if (remphy2) {
666 		status = i_dladm_rename_link_c3(handle, link1, linkid2);
667 	} else {
668 		status = DLADM_STATUS_NOTFOUND;
669 	}
670 	return (status);
671 }
672 
673 typedef struct consumer_del_phys_arg_s {
674 	datalink_id_t	linkid;
675 } consumer_del_phys_arg_t;
676 
677 static int
678 i_dladm_vlan_link_del(dladm_handle_t handle, datalink_id_t vlanid, void *arg)
679 {
680 	consumer_del_phys_arg_t	*del_arg = arg;
681 	dladm_vlan_attr_t	vinfo;
682 	dladm_status_t		status;
683 
684 	status = dladm_vlan_info(handle, vlanid, &vinfo, DLADM_OPT_PERSIST);
685 	if (status != DLADM_STATUS_OK)
686 		return (DLADM_WALK_CONTINUE);
687 
688 	if (vinfo.dv_linkid == del_arg->linkid)
689 		(void) dladm_vlan_delete(handle, vlanid, DLADM_OPT_PERSIST);
690 	return (DLADM_WALK_CONTINUE);
691 }
692 
693 static int
694 i_dladm_aggr_link_del(dladm_handle_t handle, datalink_id_t aggrid, void *arg)
695 {
696 	consumer_del_phys_arg_t		*del_arg = arg;
697 	dladm_aggr_grp_attr_t		ginfo;
698 	dladm_status_t			status;
699 	dladm_aggr_port_attr_db_t	port[1];
700 	int				i;
701 
702 	status = dladm_aggr_info(handle, aggrid, &ginfo, DLADM_OPT_PERSIST);
703 	if (status != DLADM_STATUS_OK)
704 		return (DLADM_WALK_CONTINUE);
705 
706 	for (i = 0; i < ginfo.lg_nports; i++)
707 		if (ginfo.lg_ports[i].lp_linkid == del_arg->linkid)
708 			break;
709 
710 	if (i != ginfo.lg_nports) {
711 		if (ginfo.lg_nports == 1 && i == 0) {
712 			consumer_del_phys_arg_t	aggr_del_arg;
713 
714 			/*
715 			 * First delete all the VLANs on this aggregation, then
716 			 * delete the aggregation itself.
717 			 */
718 			aggr_del_arg.linkid = aggrid;
719 			(void) dladm_walk_datalink_id(i_dladm_vlan_link_del,
720 			    handle, &aggr_del_arg, DATALINK_CLASS_VLAN,
721 			    DATALINK_ANY_MEDIATYPE, DLADM_OPT_PERSIST);
722 			(void) dladm_aggr_delete(handle, aggrid,
723 			    DLADM_OPT_PERSIST);
724 		} else {
725 			port[0].lp_linkid = del_arg->linkid;
726 			(void) dladm_aggr_remove(handle, aggrid, 1, port,
727 			    DLADM_OPT_PERSIST);
728 		}
729 	}
730 	return (DLADM_WALK_CONTINUE);
731 }
732 
733 typedef struct del_phys_arg_s {
734 	dladm_status_t	rval;
735 } del_phys_arg_t;
736 
737 static int
738 i_dladm_phys_delete(dladm_handle_t handle, datalink_id_t linkid, void *arg)
739 {
740 	uint32_t		flags;
741 	datalink_class_t	class;
742 	uint32_t		media;
743 	dladm_status_t		status = DLADM_STATUS_OK;
744 	del_phys_arg_t		*del_phys_arg = arg;
745 	consumer_del_phys_arg_t	del_arg;
746 
747 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
748 	    &media, NULL, 0)) != DLADM_STATUS_OK) {
749 		goto done;
750 	}
751 
752 	/*
753 	 * see whether this link is a removed physical link.
754 	 */
755 	if ((class != DATALINK_CLASS_PHYS) || !(flags & DLADM_OPT_PERSIST) ||
756 	    (flags & DLADM_OPT_ACTIVE)) {
757 		status = DLADM_STATUS_BADARG;
758 		goto done;
759 	}
760 
761 	if (media == DL_ETHER) {
762 		del_arg.linkid = linkid;
763 		(void) dladm_walk_datalink_id(i_dladm_aggr_link_del, handle,
764 		    &del_arg, DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE,
765 		    DLADM_OPT_PERSIST);
766 		(void) dladm_walk_datalink_id(i_dladm_vlan_link_del, handle,
767 		    &del_arg, DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE,
768 		    DLADM_OPT_PERSIST);
769 	}
770 
771 	(void) dladm_remove_conf(handle, linkid);
772 	(void) dladm_destroy_datalink_id(handle, linkid, DLADM_OPT_PERSIST);
773 done:
774 	del_phys_arg->rval = status;
775 	return (DLADM_WALK_CONTINUE);
776 }
777 
778 dladm_status_t
779 dladm_phys_delete(dladm_handle_t handle, datalink_id_t linkid)
780 {
781 	del_phys_arg_t	arg = {DLADM_STATUS_OK};
782 
783 	if (linkid == DATALINK_ALL_LINKID) {
784 		(void) dladm_walk_datalink_id(i_dladm_phys_delete, handle, &arg,
785 		    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE,
786 		    DLADM_OPT_PERSIST);
787 		return (DLADM_STATUS_OK);
788 	} else {
789 		(void) i_dladm_phys_delete(handle, linkid, &arg);
790 		return (arg.rval);
791 	}
792 }
793 
794 dladm_status_t
795 dladm_phys_info(dladm_handle_t handle, datalink_id_t linkid,
796     dladm_phys_attr_t *dpap, uint32_t flags)
797 {
798 	dladm_status_t	status;
799 
800 	assert(flags == DLADM_OPT_ACTIVE || flags == DLADM_OPT_PERSIST);
801 
802 	switch (flags) {
803 	case DLADM_OPT_PERSIST: {
804 		dladm_conf_t	conf;
805 
806 		status = dladm_read_conf(handle, linkid, &conf);
807 		if (status != DLADM_STATUS_OK)
808 			return (status);
809 
810 		status = dladm_get_conf_field(handle, conf, FDEVNAME,
811 		    dpap->dp_dev, MAXLINKNAMELEN);
812 		dladm_destroy_conf(handle, conf);
813 		return (status);
814 	}
815 	case DLADM_OPT_ACTIVE: {
816 		dld_ioc_phys_attr_t	dip;
817 
818 		dip.dip_linkid = linkid;
819 		if (ioctl(dladm_dld_fd(handle), DLDIOC_PHYS_ATTR, &dip) < 0) {
820 			status = dladm_errno2status(errno);
821 			return (status);
822 		}
823 		dpap->dp_novanity = dip.dip_novanity;
824 		(void) strlcpy(dpap->dp_dev, dip.dip_dev, MAXLINKNAMELEN);
825 		return (DLADM_STATUS_OK);
826 	}
827 	default:
828 		return (DLADM_STATUS_BADARG);
829 	}
830 }
831 
832 typedef struct i_walk_dev_state_s {
833 	const char *devname;
834 	datalink_id_t linkid;
835 	boolean_t found;
836 } i_walk_dev_state_t;
837 
838 int
839 i_dladm_walk_dev2linkid(dladm_handle_t handle, datalink_id_t linkid, void *arg)
840 {
841 	dladm_phys_attr_t dpa;
842 	dladm_status_t status;
843 	i_walk_dev_state_t *statep = arg;
844 
845 	status = dladm_phys_info(handle, linkid, &dpa, DLADM_OPT_PERSIST);
846 	if ((status == DLADM_STATUS_OK) &&
847 	    (strcmp(statep->devname, dpa.dp_dev) == 0)) {
848 		statep->found = B_TRUE;
849 		statep->linkid = linkid;
850 		return (DLADM_WALK_TERMINATE);
851 	}
852 	return (DLADM_WALK_CONTINUE);
853 }
854 
855 /*
856  * Get the linkid from the physical device name.
857  */
858 dladm_status_t
859 dladm_dev2linkid(dladm_handle_t handle, const char *devname,
860     datalink_id_t *linkidp)
861 {
862 	i_walk_dev_state_t state;
863 
864 	state.found = B_FALSE;
865 	state.devname = devname;
866 
867 	(void) dladm_walk_datalink_id(i_dladm_walk_dev2linkid, handle, &state,
868 	    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE, DLADM_OPT_PERSIST);
869 	if (state.found == B_TRUE) {
870 		*linkidp = state.linkid;
871 		return (DLADM_STATUS_OK);
872 	} else {
873 		return (dladm_errno2status(ENOENT));
874 	}
875 }
876 
877 static int
878 parse_devname(const char *devname, char *driver, uint_t *ppa, size_t maxlen)
879 {
880 	char	*cp, *tp;
881 	int	len;
882 
883 	/*
884 	 * device name length must not be 0, and it must end with digit.
885 	 */
886 	if (((len = strlen(devname)) == 0) || !isdigit(devname[len - 1]))
887 		return (EINVAL);
888 
889 	(void) strlcpy(driver, devname, maxlen);
890 	cp = (char *)&driver[len - 1];
891 
892 	for (tp = cp; isdigit(*tp); tp--) {
893 		if (tp <= driver)
894 			return (EINVAL);
895 	}
896 
897 	*ppa = atoi(tp + 1);
898 	*(tp + 1) = '\0';
899 	return (0);
900 }
901 
902 dladm_status_t
903 dladm_linkid2legacyname(dladm_handle_t handle, datalink_id_t linkid, char *dev,
904     size_t len)
905 {
906 	char			devname[MAXLINKNAMELEN];
907 	uint16_t		vid = VLAN_ID_NONE;
908 	datalink_class_t	class;
909 	dladm_status_t		status;
910 
911 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, NULL,
912 	    NULL, 0);
913 	if (status != DLADM_STATUS_OK)
914 		goto done;
915 
916 	/*
917 	 * If this is a VLAN, we must first determine the class and linkid of
918 	 * the link the VLAN has been created over.
919 	 */
920 	if (class == DATALINK_CLASS_VLAN) {
921 		dladm_vlan_attr_t	dva;
922 
923 		status = dladm_vlan_info(handle, linkid, &dva,
924 		    DLADM_OPT_ACTIVE);
925 		if (status != DLADM_STATUS_OK)
926 			goto done;
927 		linkid = dva.dv_linkid;
928 		vid = dva.dv_vid;
929 
930 		if ((status = dladm_datalink_id2info(handle, linkid, NULL,
931 		    &class, NULL, NULL, 0)) != DLADM_STATUS_OK) {
932 			goto done;
933 		}
934 	}
935 
936 	switch (class) {
937 	case DATALINK_CLASS_AGGR: {
938 		dladm_aggr_grp_attr_t	dga;
939 
940 		status = dladm_aggr_info(handle, linkid, &dga,
941 		    DLADM_OPT_ACTIVE);
942 		if (status != DLADM_STATUS_OK)
943 			goto done;
944 
945 		if (dga.lg_key == 0) {
946 			/*
947 			 * If the key was not specified when the aggregation
948 			 * is created, we cannot guess its /dev node name.
949 			 */
950 			status = DLADM_STATUS_BADARG;
951 			goto done;
952 		}
953 		(void) snprintf(devname, MAXLINKNAMELEN, "aggr%d", dga.lg_key);
954 		break;
955 	}
956 	case DATALINK_CLASS_PHYS: {
957 		dladm_phys_attr_t	dpa;
958 
959 		status = dladm_phys_info(handle, linkid, &dpa,
960 		    DLADM_OPT_PERSIST);
961 		if (status != DLADM_STATUS_OK)
962 			goto done;
963 
964 		(void) strlcpy(devname, dpa.dp_dev, MAXLINKNAMELEN);
965 		break;
966 	}
967 	default:
968 		status = DLADM_STATUS_BADARG;
969 		goto done;
970 	}
971 
972 	if (vid != VLAN_ID_NONE) {
973 		char		drv[MAXNAMELEN];
974 		uint_t		ppa;
975 
976 		if (parse_devname(devname, drv, &ppa, MAXNAMELEN) != 0) {
977 			status = DLADM_STATUS_BADARG;
978 			goto done;
979 		}
980 		if (snprintf(dev, len, "%s%d", drv, vid * 1000 + ppa) >= len)
981 			status = DLADM_STATUS_TOOSMALL;
982 	} else {
983 		if (strlcpy(dev, devname, len) >= len)
984 			status = DLADM_STATUS_TOOSMALL;
985 	}
986 
987 done:
988 	return (status);
989 }
990 
991 dladm_status_t
992 dladm_parselink(const char *dev, char *provider, uint_t *ppa)
993 {
994 	ifspec_t	ifsp;
995 
996 	if (dev == NULL || !ifparse_ifspec(dev, &ifsp))
997 		return (DLADM_STATUS_LINKINVAL);
998 
999 	if (provider != NULL)
1000 		(void) strlcpy(provider, ifsp.ifsp_devnm, DLPI_LINKNAME_MAX);
1001 
1002 	if (ppa != NULL)
1003 		*ppa = ifsp.ifsp_ppa;
1004 
1005 	return (DLADM_STATUS_OK);
1006 }
1007