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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <door.h> 27 #include <errno.h> 28 #include <assert.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <unistd.h> 32 #include <string.h> 33 #include <strings.h> 34 #include <zone.h> 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 #include <sys/aggr.h> 38 #include <sys/mman.h> 39 #include <fcntl.h> 40 #include <libdladm.h> 41 #include <libdladm_impl.h> 42 #include <libdllink.h> 43 #include <libdlmgmt.h> 44 45 /* 46 * Table of data type sizes indexed by dladm_datatype_t. 47 */ 48 static size_t dladm_datatype_size[] = { 49 0, /* DLADM_TYPE_STR, use strnlen() */ 50 sizeof (boolean_t), /* DLADM_TYPE_BOOLEAN */ 51 sizeof (uint64_t) /* DLADM_TYPE_UINT64 */ 52 }; 53 54 static dladm_status_t 55 dladm_door_call(dladm_handle_t handle, void *arg, size_t asize, void *rbuf, 56 size_t rsize) 57 { 58 door_arg_t darg; 59 int door_fd; 60 dladm_status_t status = DLADM_STATUS_OK; 61 62 darg.data_ptr = arg; 63 darg.data_size = asize; 64 darg.desc_ptr = NULL; 65 darg.desc_num = 0; 66 darg.rbuf = rbuf; 67 darg.rsize = rsize; 68 69 /* The door descriptor is opened if it isn't already */ 70 if ((status = dladm_door_fd(handle, &door_fd)) != DLADM_STATUS_OK) 71 return (status); 72 if (door_call(door_fd, &darg) == -1) 73 status = dladm_errno2status(errno); 74 if (status != DLADM_STATUS_OK) 75 return (status); 76 77 if (darg.rbuf != rbuf) { 78 /* 79 * The size of the input rbuf is not big enough so that 80 * the door allocate the rbuf itself. In this case, simply 81 * think something wrong with the door call. 82 */ 83 (void) munmap(darg.rbuf, darg.rsize); 84 return (DLADM_STATUS_TOOSMALL); 85 } 86 if (darg.rsize != rsize) 87 return (DLADM_STATUS_FAILED); 88 89 return (dladm_errno2status(((dlmgmt_retval_t *)rbuf)->lr_err)); 90 } 91 92 /* 93 * Allocate a new linkid with the given name. Return the new linkid. 94 */ 95 dladm_status_t 96 dladm_create_datalink_id(dladm_handle_t handle, const char *link, 97 datalink_class_t class, uint32_t media, uint32_t flags, 98 datalink_id_t *linkidp) 99 { 100 dlmgmt_door_createid_t createid; 101 dlmgmt_createid_retval_t retval; 102 uint32_t dlmgmt_flags; 103 dladm_status_t status; 104 105 if (link == NULL || class == DATALINK_CLASS_ALL || 106 !(flags & (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST)) || 107 linkidp == NULL) { 108 return (DLADM_STATUS_BADARG); 109 } 110 111 dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0; 112 dlmgmt_flags |= (flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0; 113 114 (void) strlcpy(createid.ld_link, link, MAXLINKNAMELEN); 115 createid.ld_class = class; 116 createid.ld_media = media; 117 createid.ld_flags = dlmgmt_flags; 118 createid.ld_cmd = DLMGMT_CMD_CREATE_LINKID; 119 createid.ld_prefix = (flags & DLADM_OPT_PREFIX); 120 121 if ((status = dladm_door_call(handle, &createid, sizeof (createid), 122 &retval, sizeof (retval))) == DLADM_STATUS_OK) { 123 *linkidp = retval.lr_linkid; 124 } 125 return (status); 126 } 127 128 /* 129 * Destroy the given link ID. 130 */ 131 dladm_status_t 132 dladm_destroy_datalink_id(dladm_handle_t handle, datalink_id_t linkid, 133 uint32_t flags) 134 { 135 dlmgmt_door_destroyid_t destroyid; 136 dlmgmt_destroyid_retval_t retval; 137 uint32_t dlmgmt_flags; 138 139 dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0; 140 dlmgmt_flags |= ((flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0); 141 142 destroyid.ld_cmd = DLMGMT_CMD_DESTROY_LINKID; 143 destroyid.ld_linkid = linkid; 144 destroyid.ld_flags = dlmgmt_flags; 145 146 return (dladm_door_call(handle, &destroyid, sizeof (destroyid), &retval, 147 sizeof (retval))); 148 } 149 150 /* 151 * Remap a given link ID to a new name. 152 */ 153 dladm_status_t 154 dladm_remap_datalink_id(dladm_handle_t handle, datalink_id_t linkid, 155 const char *link) 156 { 157 dlmgmt_door_remapid_t remapid; 158 dlmgmt_remapid_retval_t retval; 159 160 remapid.ld_cmd = DLMGMT_CMD_REMAP_LINKID; 161 remapid.ld_linkid = linkid; 162 (void) strlcpy(remapid.ld_link, link, MAXLINKNAMELEN); 163 164 return (dladm_door_call(handle, &remapid, sizeof (remapid), &retval, 165 sizeof (retval))); 166 } 167 168 /* 169 * Make a given link ID active. 170 */ 171 dladm_status_t 172 dladm_up_datalink_id(dladm_handle_t handle, datalink_id_t linkid) 173 { 174 dlmgmt_door_upid_t upid; 175 dlmgmt_upid_retval_t retval; 176 177 upid.ld_cmd = DLMGMT_CMD_UP_LINKID; 178 upid.ld_linkid = linkid; 179 180 return (dladm_door_call(handle, &upid, sizeof (upid), &retval, 181 sizeof (retval))); 182 } 183 184 /* 185 * Create a new link with the given name. Return the new link's handle 186 */ 187 dladm_status_t 188 dladm_create_conf(dladm_handle_t handle, const char *link, datalink_id_t linkid, 189 datalink_class_t class, uint32_t media, dladm_conf_t *confp) 190 { 191 dlmgmt_door_createconf_t createconf; 192 dlmgmt_createconf_retval_t retval; 193 dladm_status_t status; 194 195 if (link == NULL || confp == NULL) 196 return (DLADM_STATUS_BADARG); 197 198 (void) strlcpy(createconf.ld_link, link, MAXLINKNAMELEN); 199 createconf.ld_class = class; 200 createconf.ld_media = media; 201 createconf.ld_linkid = linkid; 202 createconf.ld_cmd = DLMGMT_CMD_CREATECONF; 203 204 if ((status = dladm_door_call(handle, &createconf, sizeof (createconf), 205 &retval, sizeof (retval))) == DLADM_STATUS_OK) { 206 *confp = retval.lr_conf; 207 } 208 return (status); 209 } 210 211 /* 212 * An active physical link reported by the dlmgmtd daemon might not be active 213 * anymore as this link might be removed during system shutdown. Check its 214 * real status by calling dladm_phys_info(). 215 */ 216 dladm_status_t 217 i_dladm_phys_status(dladm_handle_t handle, datalink_id_t linkid, 218 uint32_t *flagsp) 219 { 220 dladm_phys_attr_t dpa; 221 dladm_status_t status; 222 223 assert((*flagsp) & DLMGMT_ACTIVE); 224 225 status = dladm_phys_info(handle, linkid, &dpa, DLADM_OPT_ACTIVE); 226 if (status == DLADM_STATUS_NOTFOUND) { 227 /* 228 * No active status, this link was removed. Update its status 229 * in the daemon and delete all active linkprops. 230 * 231 * Note that the operation could fail. If it does, return 232 * failure now since otherwise dladm_set_linkprop() might 233 * call back to i_dladm_phys_status() recursively. 234 */ 235 if ((status = dladm_destroy_datalink_id(handle, linkid, 236 DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) 237 return (status); 238 239 (void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0, 240 DLADM_OPT_ACTIVE); 241 242 (*flagsp) &= ~DLMGMT_ACTIVE; 243 status = DLADM_STATUS_OK; 244 } 245 return (status); 246 } 247 248 /* 249 * Walk each entry in the data link configuration repository and 250 * call fn on the linkid and arg. 251 */ 252 dladm_status_t 253 dladm_walk_datalink_id(int (*fn)(dladm_handle_t, datalink_id_t, void *), 254 dladm_handle_t handle, void *argp, datalink_class_t class, 255 datalink_media_t dmedia, uint32_t flags) 256 { 257 dlmgmt_door_getnext_t getnext; 258 dlmgmt_getnext_retval_t retval; 259 uint32_t dlmgmt_flags; 260 datalink_id_t linkid = DATALINK_INVALID_LINKID; 261 dladm_status_t status = DLADM_STATUS_OK; 262 263 if (fn == NULL) 264 return (DLADM_STATUS_BADARG); 265 266 dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0; 267 dlmgmt_flags |= ((flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0); 268 269 getnext.ld_cmd = DLMGMT_CMD_GETNEXT; 270 getnext.ld_class = class; 271 getnext.ld_dmedia = dmedia; 272 getnext.ld_flags = dlmgmt_flags; 273 274 do { 275 getnext.ld_linkid = linkid; 276 if ((status = dladm_door_call(handle, &getnext, 277 sizeof (getnext), &retval, sizeof (retval))) != 278 DLADM_STATUS_OK) { 279 /* 280 * done with walking 281 */ 282 break; 283 } 284 285 linkid = retval.lr_linkid; 286 if ((retval.lr_class == DATALINK_CLASS_PHYS) && 287 (retval.lr_flags & DLMGMT_ACTIVE)) { 288 /* 289 * An active physical link reported by the dlmgmtd 290 * daemon might not be active anymore. Check its 291 * real status. 292 */ 293 if (i_dladm_phys_status(handle, linkid, 294 &retval.lr_flags) != DLADM_STATUS_OK) { 295 continue; 296 } 297 298 if (!(dlmgmt_flags & retval.lr_flags)) 299 continue; 300 } 301 302 if (fn(handle, linkid, argp) == DLADM_WALK_TERMINATE) 303 break; 304 } while (linkid != DATALINK_INVALID_LINKID); 305 306 return (status); 307 } 308 309 /* 310 * Get the link properties structure for the given link. 311 */ 312 dladm_status_t 313 dladm_read_conf(dladm_handle_t handle, datalink_id_t linkid, 314 dladm_conf_t *confp) 315 { 316 dlmgmt_door_readconf_t readconf; 317 dlmgmt_readconf_retval_t retval; 318 dladm_status_t status; 319 320 if (linkid == DATALINK_INVALID_LINKID || confp == NULL) 321 return (DLADM_STATUS_BADARG); 322 323 readconf.ld_linkid = linkid; 324 readconf.ld_cmd = DLMGMT_CMD_READCONF; 325 326 if ((status = dladm_door_call(handle, &readconf, sizeof (readconf), 327 &retval, sizeof (retval))) == DLADM_STATUS_OK) { 328 *confp = retval.lr_conf; 329 } 330 return (status); 331 } 332 333 /* 334 * Commit the given link to the data link configuration repository so 335 * that it will persist across reboots. 336 */ 337 dladm_status_t 338 dladm_write_conf(dladm_handle_t handle, dladm_conf_t conf) 339 { 340 dlmgmt_door_writeconf_t writeconf; 341 dlmgmt_writeconf_retval_t retval; 342 343 if (conf == DLADM_INVALID_CONF) 344 return (DLADM_STATUS_BADARG); 345 346 writeconf.ld_cmd = DLMGMT_CMD_WRITECONF; 347 writeconf.ld_conf = conf; 348 349 return (dladm_door_call(handle, &writeconf, sizeof (writeconf), &retval, 350 sizeof (retval))); 351 } 352 353 /* 354 * Given a link ID and a key, get the matching information from 355 * data link configuration repository. 356 */ 357 dladm_status_t 358 dladm_get_conf_field(dladm_handle_t handle, dladm_conf_t conf, const char *attr, 359 void *attrval, size_t attrsz) 360 { 361 dlmgmt_door_getattr_t getattr; 362 dlmgmt_getattr_retval_t retval; 363 dladm_status_t status; 364 365 if (conf == DLADM_INVALID_CONF || attrval == NULL || 366 attrsz == 0 || attr == NULL) { 367 return (DLADM_STATUS_BADARG); 368 } 369 370 getattr.ld_cmd = DLMGMT_CMD_GETATTR; 371 getattr.ld_conf = conf; 372 (void) strlcpy(getattr.ld_attr, attr, MAXLINKATTRLEN); 373 374 if ((status = dladm_door_call(handle, &getattr, sizeof (getattr), 375 &retval, sizeof (retval))) != DLADM_STATUS_OK) { 376 return (status); 377 } 378 379 if (retval.lr_attrsz > attrsz) 380 return (DLADM_STATUS_TOOSMALL); 381 382 bcopy(retval.lr_attrval, attrval, retval.lr_attrsz); 383 return (DLADM_STATUS_OK); 384 } 385 386 /* 387 * Get next property attribute from data link configuration repository. 388 */ 389 dladm_status_t 390 dladm_getnext_conf_linkprop(dladm_handle_t handle, dladm_conf_t conf, 391 const char *last_attr, char *attr, void *attrval, size_t attrsz, 392 size_t *attrszp) 393 { 394 dlmgmt_door_linkprop_getnext_t getnext; 395 dlmgmt_linkprop_getnext_retval_t retval; 396 dladm_status_t status; 397 398 if (conf == DLADM_INVALID_CONF || attrval == NULL || 399 attrsz == 0 || attr == NULL) { 400 return (DLADM_STATUS_BADARG); 401 } 402 403 getnext.ld_cmd = DLMGMT_CMD_LINKPROP_GETNEXT; 404 getnext.ld_conf = conf; 405 (void) strlcpy(getnext.ld_last_attr, last_attr, MAXLINKATTRLEN); 406 407 if ((status = dladm_door_call(handle, &getnext, sizeof (getnext), 408 &retval, sizeof (retval))) != DLADM_STATUS_OK) { 409 return (status); 410 } 411 412 *attrszp = retval.lr_attrsz; 413 if (retval.lr_attrsz > attrsz) { 414 return (DLADM_STATUS_TOOSMALL); 415 } 416 417 (void) strlcpy(attr, retval.lr_attr, MAXLINKATTRLEN); 418 bcopy(retval.lr_attrval, attrval, retval.lr_attrsz); 419 return (DLADM_STATUS_OK); 420 } 421 422 /* 423 * Get the link ID that is associated with the given name. 424 */ 425 dladm_status_t 426 dladm_name2info(dladm_handle_t handle, const char *link, datalink_id_t *linkidp, 427 uint32_t *flagp, datalink_class_t *classp, uint32_t *mediap) 428 { 429 dlmgmt_door_getlinkid_t getlinkid; 430 dlmgmt_getlinkid_retval_t retval; 431 datalink_id_t linkid; 432 dladm_status_t status; 433 434 getlinkid.ld_cmd = DLMGMT_CMD_GETLINKID; 435 (void) strlcpy(getlinkid.ld_link, link, MAXLINKNAMELEN); 436 437 if ((status = dladm_door_call(handle, &getlinkid, sizeof (getlinkid), 438 &retval, sizeof (retval))) != DLADM_STATUS_OK) { 439 return (status); 440 } 441 442 linkid = retval.lr_linkid; 443 if (retval.lr_class == DATALINK_CLASS_PHYS && 444 retval.lr_flags & DLMGMT_ACTIVE) { 445 /* 446 * An active physical link reported by the dlmgmtd daemon 447 * might not be active anymore. Check and set its real status. 448 */ 449 status = i_dladm_phys_status(handle, linkid, &retval.lr_flags); 450 if (status != DLADM_STATUS_OK) 451 return (status); 452 } 453 454 if (linkidp != NULL) 455 *linkidp = linkid; 456 if (flagp != NULL) { 457 *flagp = retval.lr_flags & DLMGMT_ACTIVE ? DLADM_OPT_ACTIVE : 0; 458 *flagp |= (retval.lr_flags & DLMGMT_PERSIST) ? 459 DLADM_OPT_PERSIST : 0; 460 } 461 if (classp != NULL) 462 *classp = retval.lr_class; 463 if (mediap != NULL) 464 *mediap = retval.lr_media; 465 466 return (DLADM_STATUS_OK); 467 } 468 469 /* 470 * Get the link name that is associated with the given id. 471 */ 472 dladm_status_t 473 dladm_datalink_id2info(dladm_handle_t handle, datalink_id_t linkid, 474 uint32_t *flagp, datalink_class_t *classp, uint32_t *mediap, char *link, 475 size_t len) 476 { 477 dlmgmt_door_getname_t getname; 478 dlmgmt_getname_retval_t retval; 479 dladm_status_t status; 480 481 if ((linkid == DATALINK_INVALID_LINKID) || (link != NULL && len == 0) || 482 (link == NULL && len != 0)) { 483 return (DLADM_STATUS_BADARG); 484 } 485 486 getname.ld_cmd = DLMGMT_CMD_GETNAME; 487 getname.ld_linkid = linkid; 488 if ((status = dladm_door_call(handle, &getname, sizeof (getname), 489 &retval, sizeof (retval))) != DLADM_STATUS_OK) { 490 return (status); 491 } 492 493 if (len != 0 && (strlen(retval.lr_link) + 1 > len)) 494 return (DLADM_STATUS_TOOSMALL); 495 496 if (retval.lr_class == DATALINK_CLASS_PHYS && 497 retval.lr_flags & DLMGMT_ACTIVE) { 498 /* 499 * An active physical link reported by the dlmgmtd daemon 500 * might not be active anymore. Check and set its real status. 501 */ 502 status = i_dladm_phys_status(handle, linkid, &retval.lr_flags); 503 if (status != DLADM_STATUS_OK) 504 return (status); 505 } 506 507 if (link != NULL) 508 (void) strlcpy(link, retval.lr_link, len); 509 if (classp != NULL) 510 *classp = retval.lr_class; 511 if (mediap != NULL) 512 *mediap = retval.lr_media; 513 if (flagp != NULL) { 514 *flagp = retval.lr_flags & DLMGMT_ACTIVE ? 515 DLADM_OPT_ACTIVE : 0; 516 *flagp |= (retval.lr_flags & DLMGMT_PERSIST) ? 517 DLADM_OPT_PERSIST : 0; 518 } 519 return (DLADM_STATUS_OK); 520 } 521 522 /* 523 * Set the given attr with the given attrval for the given link. 524 */ 525 dladm_status_t 526 dladm_set_conf_field(dladm_handle_t handle, dladm_conf_t conf, const char *attr, 527 dladm_datatype_t type, const void *attrval) 528 { 529 dlmgmt_door_setattr_t setattr; 530 dlmgmt_setattr_retval_t retval; 531 size_t attrsz; 532 533 if (attr == NULL || attrval == NULL) 534 return (DLADM_STATUS_BADARG); 535 536 if (type == DLADM_TYPE_STR) 537 attrsz = strlen(attrval) + 1; 538 else 539 attrsz = dladm_datatype_size[type]; 540 541 if (attrsz > MAXLINKATTRVALLEN) 542 return (DLADM_STATUS_TOOSMALL); 543 544 setattr.ld_cmd = DLMGMT_CMD_SETATTR; 545 setattr.ld_conf = conf; 546 (void) strlcpy(setattr.ld_attr, attr, MAXLINKATTRLEN); 547 setattr.ld_attrsz = attrsz; 548 setattr.ld_type = type; 549 bcopy(attrval, &setattr.ld_attrval, attrsz); 550 551 return (dladm_door_call(handle, &setattr, sizeof (setattr), &retval, 552 sizeof (retval))); 553 } 554 555 /* 556 * Unset the given attr the given link. 557 */ 558 dladm_status_t 559 dladm_unset_conf_field(dladm_handle_t handle, dladm_conf_t conf, 560 const char *attr) 561 { 562 dlmgmt_door_unsetattr_t unsetattr; 563 dlmgmt_unsetattr_retval_t retval; 564 565 if (attr == NULL) 566 return (DLADM_STATUS_BADARG); 567 568 unsetattr.ld_cmd = DLMGMT_CMD_UNSETATTR; 569 unsetattr.ld_conf = conf; 570 (void) strlcpy(unsetattr.ld_attr, attr, MAXLINKATTRLEN); 571 572 return (dladm_door_call(handle, &unsetattr, sizeof (unsetattr), &retval, 573 sizeof (retval))); 574 } 575 576 /* 577 * Remove the given link ID and its entry from the data link configuration 578 * repository. 579 */ 580 dladm_status_t 581 dladm_remove_conf(dladm_handle_t handle, datalink_id_t linkid) 582 { 583 dlmgmt_door_removeconf_t removeconf; 584 dlmgmt_removeconf_retval_t retval; 585 586 removeconf.ld_cmd = DLMGMT_CMD_REMOVECONF; 587 removeconf.ld_linkid = linkid; 588 589 return (dladm_door_call(handle, &removeconf, sizeof (removeconf), 590 &retval, sizeof (retval))); 591 } 592 593 /* 594 * Free the contents of the link structure. 595 */ 596 void 597 dladm_destroy_conf(dladm_handle_t handle, dladm_conf_t conf) 598 { 599 dlmgmt_door_destroyconf_t destroyconf; 600 dlmgmt_destroyconf_retval_t retval; 601 602 if (conf == DLADM_INVALID_CONF) 603 return; 604 605 destroyconf.ld_cmd = DLMGMT_CMD_DESTROYCONF; 606 destroyconf.ld_conf = conf; 607 608 (void) dladm_door_call(handle, &destroyconf, sizeof (destroyconf), 609 &retval, sizeof (retval)); 610 } 611 612 dladm_status_t 613 dladm_zone_boot(dladm_handle_t handle, zoneid_t zoneid) 614 { 615 dlmgmt_door_zoneboot_t zoneboot; 616 dlmgmt_zoneboot_retval_t retval; 617 618 zoneboot.ld_cmd = DLMGMT_CMD_ZONEBOOT; 619 zoneboot.ld_zoneid = zoneid; 620 return (dladm_door_call(handle, &zoneboot, sizeof (zoneboot), &retval, 621 sizeof (retval))); 622 } 623 624 dladm_status_t 625 dladm_zone_halt(dladm_handle_t handle, zoneid_t zoneid) 626 { 627 dlmgmt_door_zonehalt_t zonehalt; 628 dlmgmt_zonehalt_retval_t retval; 629 630 zonehalt.ld_cmd = DLMGMT_CMD_ZONEHALT; 631 zonehalt.ld_zoneid = zoneid; 632 return (dladm_door_call(handle, &zonehalt, sizeof (zonehalt), &retval, 633 sizeof (retval))); 634 } 635