Print this page
OS-406
OS-249
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libdladm/common/libdlvnic.c
+++ new/usr/src/lib/libdladm/common/libdlvnic.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
|
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 + * Copyright (c) 2011, Joyent Inc. All rights reserved.
23 24 */
24 25
25 26 #include <stdio.h>
26 27 #include <sys/types.h>
27 28 #include <sys/stat.h>
28 29 #include <string.h>
29 30 #include <fcntl.h>
30 31 #include <unistd.h>
31 32 #include <stropts.h>
32 33 #include <stdlib.h>
33 34 #include <errno.h>
34 35 #include <strings.h>
35 36 #include <libintl.h>
36 37 #include <net/if_types.h>
37 38 #include <net/if_dl.h>
38 39 #include <sys/dld.h>
39 40 #include <libdladm_impl.h>
40 41 #include <libvrrpadm.h>
41 42 #include <libdllink.h>
42 43 #include <libdlbridge.h>
43 44 #include <libdlvnic.h>
44 45
45 46 /*
46 47 * VNIC administration library.
47 48 */
48 49
49 50 /*
50 51 * Default random MAC address prefix (locally administered).
51 52 */
52 53 static char dladm_vnic_def_prefix[] = {0x02, 0x08, 0x20};
53 54
54 55 static dladm_status_t dladm_vnic_persist_conf(dladm_handle_t,
55 56 const char *name, dladm_vnic_attr_t *,
56 57 datalink_class_t);
57 58 static const char *dladm_vnic_macaddr2str(const uchar_t *, char *);
58 59 static dladm_status_t dladm_vnic_str2macaddr(const char *, uchar_t *);
59 60
60 61 /*
61 62 * Convert a diagnostic returned by the kernel into a dladm_status_t.
62 63 */
63 64 static dladm_status_t
64 65 dladm_vnic_diag2status(vnic_ioc_diag_t ioc_diag)
65 66 {
66 67 switch (ioc_diag) {
67 68 case VNIC_IOC_DIAG_NONE:
68 69 return (DLADM_STATUS_OK);
69 70 case VNIC_IOC_DIAG_MACADDRLEN_INVALID:
70 71 return (DLADM_STATUS_INVALIDMACADDRLEN);
71 72 case VNIC_IOC_DIAG_MACADDR_NIC:
72 73 return (DLADM_STATUS_INVALIDMACADDRNIC);
73 74 case VNIC_IOC_DIAG_MACADDR_INUSE:
74 75 return (DLADM_STATUS_INVALIDMACADDRINUSE);
75 76 case VNIC_IOC_DIAG_MACFACTORYSLOTINVALID:
76 77 return (DLADM_STATUS_MACFACTORYSLOTINVALID);
77 78 case VNIC_IOC_DIAG_MACFACTORYSLOTUSED:
78 79 return (DLADM_STATUS_MACFACTORYSLOTUSED);
79 80 case VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED:
80 81 return (DLADM_STATUS_MACFACTORYSLOTALLUSED);
81 82 case VNIC_IOC_DIAG_MACFACTORYNOTSUP:
82 83 return (DLADM_STATUS_MACFACTORYNOTSUP);
83 84 case VNIC_IOC_DIAG_MACPREFIX_INVALID:
84 85 return (DLADM_STATUS_INVALIDMACPREFIX);
85 86 case VNIC_IOC_DIAG_MACPREFIXLEN_INVALID:
86 87 return (DLADM_STATUS_INVALIDMACPREFIXLEN);
87 88 case VNIC_IOC_DIAG_MACMARGIN_INVALID:
88 89 return (DLADM_STATUS_INVALID_MACMARGIN);
89 90 case VNIC_IOC_DIAG_NO_HWRINGS:
90 91 return (DLADM_STATUS_NO_HWRINGS);
91 92 case VNIC_IOC_DIAG_MACADDR_INVALID:
92 93 return (DLADM_STATUS_INVALIDMACADDR);
93 94 case VNIC_IOC_DIAG_MACMTU_INVALID:
94 95 return (DLADM_STATUS_INVALID_MTU);
95 96 default:
96 97 return (DLADM_STATUS_FAILED);
97 98 }
98 99 }
99 100
100 101 /*
101 102 * Send a create command to the VNIC driver.
102 103 */
103 104 dladm_status_t
104 105 i_dladm_vnic_create_sys(dladm_handle_t handle, dladm_vnic_attr_t *attr)
105 106 {
106 107 int rc;
107 108 vnic_ioc_create_t ioc;
108 109 dladm_status_t status = DLADM_STATUS_OK;
109 110
110 111 bzero(&ioc, sizeof (ioc));
111 112 ioc.vc_vnic_id = attr->va_vnic_id;
112 113 ioc.vc_link_id = attr->va_link_id;
113 114 ioc.vc_mac_addr_type = attr->va_mac_addr_type;
114 115 ioc.vc_mac_len = attr->va_mac_len;
115 116 ioc.vc_mac_slot = attr->va_mac_slot;
116 117 ioc.vc_mac_prefix_len = attr->va_mac_prefix_len;
117 118 ioc.vc_vid = attr->va_vid;
118 119 ioc.vc_vrid = attr->va_vrid;
119 120 ioc.vc_af = attr->va_af;
120 121 ioc.vc_flags = attr->va_force ? VNIC_IOC_CREATE_FORCE : 0;
121 122
122 123 if (attr->va_mac_len > 0 || ioc.vc_mac_prefix_len > 0)
123 124 bcopy(attr->va_mac_addr, ioc.vc_mac_addr, MAXMACADDRLEN);
124 125 bcopy(&attr->va_resource_props, &ioc.vc_resource_props,
125 126 sizeof (mac_resource_props_t));
126 127 if (attr->va_link_id == DATALINK_INVALID_LINKID)
127 128 ioc.vc_flags |= VNIC_IOC_CREATE_ANCHOR;
128 129
129 130 rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_CREATE, &ioc);
130 131 if (rc < 0)
131 132 status = dladm_errno2status(errno);
132 133
133 134 if (status != DLADM_STATUS_OK) {
134 135 if (ioc.vc_diag != VNIC_IOC_DIAG_NONE)
135 136 status = dladm_vnic_diag2status(ioc.vc_diag);
136 137 }
137 138 if (status != DLADM_STATUS_OK)
138 139 return (status);
139 140
140 141 attr->va_mac_addr_type = ioc.vc_mac_addr_type;
141 142 switch (ioc.vc_mac_addr_type) {
142 143 case VNIC_MAC_ADDR_TYPE_FACTORY:
143 144 attr->va_mac_slot = ioc.vc_mac_slot;
144 145 break;
145 146 case VNIC_MAC_ADDR_TYPE_RANDOM:
146 147 bcopy(ioc.vc_mac_addr, attr->va_mac_addr, MAXMACADDRLEN);
147 148 attr->va_mac_len = ioc.vc_mac_len;
148 149 break;
149 150 }
150 151 return (status);
151 152 }
152 153
153 154 /*
154 155 * Get the configuration information of the given VNIC.
155 156 */
156 157 static dladm_status_t
157 158 i_dladm_vnic_info_active(dladm_handle_t handle, datalink_id_t linkid,
158 159 dladm_vnic_attr_t *attrp)
159 160 {
160 161 vnic_ioc_info_t ioc;
161 162 vnic_info_t *vnic;
162 163 int rc;
163 164 dladm_status_t status = DLADM_STATUS_OK;
164 165
165 166 bzero(&ioc, sizeof (ioc));
166 167 vnic = &ioc.vi_info;
167 168 vnic->vn_vnic_id = linkid;
168 169
169 170 rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_INFO, &ioc);
170 171 if (rc != 0) {
171 172 status = dladm_errno2status(errno);
172 173 goto bail;
173 174 }
174 175
175 176 attrp->va_vnic_id = vnic->vn_vnic_id;
176 177 attrp->va_link_id = vnic->vn_link_id;
177 178 attrp->va_mac_addr_type = vnic->vn_mac_addr_type;
178 179 bcopy(vnic->vn_mac_addr, attrp->va_mac_addr, MAXMACADDRLEN);
179 180 attrp->va_mac_len = vnic->vn_mac_len;
180 181 attrp->va_mac_slot = vnic->vn_mac_slot;
181 182 attrp->va_mac_prefix_len = vnic->vn_mac_prefix_len;
182 183 attrp->va_vid = vnic->vn_vid;
183 184 attrp->va_vrid = vnic->vn_vrid;
184 185 attrp->va_af = vnic->vn_af;
185 186 attrp->va_force = vnic->vn_force;
186 187
187 188 bail:
188 189 return (status);
189 190 }
190 191
191 192 static dladm_status_t
192 193 i_dladm_vnic_info_persist(dladm_handle_t handle, datalink_id_t linkid,
193 194 dladm_vnic_attr_t *attrp)
194 195 {
195 196 dladm_conf_t conf;
196 197 dladm_status_t status;
197 198 char macstr[ETHERADDRL * 3];
198 199 char linkover[MAXLINKNAMELEN];
199 200 uint64_t u64;
200 201 datalink_class_t class;
201 202
202 203 attrp->va_vnic_id = linkid;
203 204 if ((status = dladm_getsnap_conf(handle, linkid, &conf)) !=
204 205 DLADM_STATUS_OK)
205 206 return (status);
206 207
207 208 status = dladm_get_conf_field(handle, conf, FLINKOVER, linkover,
208 209 sizeof (linkover));
209 210 if (status != DLADM_STATUS_OK) {
210 211 /*
211 212 * This isn't an error, etherstubs don't have a FLINKOVER
212 213 * property.
213 214 */
214 215 attrp->va_link_id = DATALINK_INVALID_LINKID;
215 216 } else {
216 217 if ((status = dladm_name2info(handle, linkover,
217 218 &attrp->va_link_id, NULL, NULL, NULL)) != DLADM_STATUS_OK)
218 219 goto done;
219 220 }
220 221
221 222 if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class,
222 223 NULL, NULL, 0)) != DLADM_STATUS_OK)
223 224 goto done;
224 225
225 226 if (class == DATALINK_CLASS_VLAN) {
226 227 if (attrp->va_link_id == DATALINK_INVALID_LINKID) {
227 228 status = DLADM_STATUS_BADARG;
228 229 goto done;
229 230 }
230 231 attrp->va_mac_addr_type = VNIC_MAC_ADDR_TYPE_PRIMARY;
231 232 attrp->va_mac_len = 0;
232 233 } else {
233 234 status = dladm_get_conf_field(handle, conf, FMADDRTYPE, &u64,
234 235 sizeof (u64));
235 236 if (status != DLADM_STATUS_OK)
236 237 goto done;
237 238
238 239 attrp->va_mac_addr_type = (vnic_mac_addr_type_t)u64;
239 240
240 241 if ((status = dladm_get_conf_field(handle, conf, FVRID,
241 242 &u64, sizeof (u64))) != DLADM_STATUS_OK) {
242 243 attrp->va_vrid = VRRP_VRID_NONE;
243 244 } else {
244 245 attrp->va_vrid = (vrid_t)u64;
245 246 }
246 247
247 248 if ((status = dladm_get_conf_field(handle, conf, FVRAF,
248 249 &u64, sizeof (u64))) != DLADM_STATUS_OK) {
249 250 attrp->va_af = AF_UNSPEC;
250 251 } else {
251 252 attrp->va_af = (int)u64;
252 253 }
253 254
254 255 status = dladm_get_conf_field(handle, conf, FMADDRLEN, &u64,
255 256 sizeof (u64));
256 257 attrp->va_mac_len = ((status == DLADM_STATUS_OK) ?
257 258 (uint_t)u64 : ETHERADDRL);
258 259
259 260 status = dladm_get_conf_field(handle, conf, FMADDRSLOT, &u64,
260 261 sizeof (u64));
261 262 attrp->va_mac_slot = ((status == DLADM_STATUS_OK) ?
262 263 (int)u64 : -1);
263 264
264 265 status = dladm_get_conf_field(handle, conf, FMADDRPREFIXLEN,
265 266 &u64, sizeof (u64));
266 267 attrp->va_mac_prefix_len = ((status == DLADM_STATUS_OK) ?
267 268 (uint_t)u64 : sizeof (dladm_vnic_def_prefix));
268 269
269 270 status = dladm_get_conf_field(handle, conf, FMACADDR, macstr,
270 271 sizeof (macstr));
271 272 if (status != DLADM_STATUS_OK)
272 273 goto done;
273 274
274 275 status = dladm_vnic_str2macaddr(macstr, attrp->va_mac_addr);
275 276 if (status != DLADM_STATUS_OK)
276 277 goto done;
277 278 }
278 279
279 280 status = dladm_get_conf_field(handle, conf, FVLANID, &u64,
280 281 sizeof (u64));
281 282 attrp->va_vid = ((status == DLADM_STATUS_OK) ? (uint16_t)u64 : 0);
282 283
283 284 status = DLADM_STATUS_OK;
284 285 done:
285 286 dladm_destroy_conf(handle, conf);
286 287 return (status);
287 288 }
288 289
289 290 dladm_status_t
290 291 dladm_vnic_info(dladm_handle_t handle, datalink_id_t linkid,
291 292 dladm_vnic_attr_t *attrp, uint32_t flags)
292 293 {
293 294 if (flags == DLADM_OPT_ACTIVE)
294 295 return (i_dladm_vnic_info_active(handle, linkid, attrp));
295 296 else if (flags == DLADM_OPT_PERSIST)
296 297 return (i_dladm_vnic_info_persist(handle, linkid, attrp));
297 298 else
298 299 return (DLADM_STATUS_BADARG);
299 300 }
300 301
301 302 /*
302 303 * Remove a VNIC from the kernel.
303 304 */
304 305 dladm_status_t
305 306 i_dladm_vnic_delete_sys(dladm_handle_t handle, datalink_id_t linkid)
306 307 {
307 308 vnic_ioc_delete_t ioc;
308 309 dladm_status_t status = DLADM_STATUS_OK;
309 310 int rc;
310 311
311 312 ioc.vd_vnic_id = linkid;
312 313
313 314 rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_DELETE, &ioc);
314 315 if (rc < 0)
315 316 status = dladm_errno2status(errno);
316 317
317 318 return (status);
318 319 }
319 320
320 321 /*
321 322 * Convert between MAC address types and their string representations.
322 323 */
323 324
324 325 typedef struct dladm_vnic_addr_type_s {
325 326 const char *va_str;
326 327 vnic_mac_addr_type_t va_type;
327 328 } dladm_vnic_addr_type_t;
328 329
329 330 static dladm_vnic_addr_type_t addr_types[] = {
330 331 {"fixed", VNIC_MAC_ADDR_TYPE_FIXED},
331 332 {"random", VNIC_MAC_ADDR_TYPE_RANDOM},
332 333 {"factory", VNIC_MAC_ADDR_TYPE_FACTORY},
333 334 {"auto", VNIC_MAC_ADDR_TYPE_AUTO},
334 335 {"fixed", VNIC_MAC_ADDR_TYPE_PRIMARY},
335 336 {"vrrp", VNIC_MAC_ADDR_TYPE_VRID}
336 337 };
337 338
338 339 #define NADDR_TYPES (sizeof (addr_types) / sizeof (dladm_vnic_addr_type_t))
339 340
340 341 static const char *
341 342 dladm_vnic_macaddrtype2str(vnic_mac_addr_type_t type)
342 343 {
343 344 int i;
344 345
345 346 for (i = 0; i < NADDR_TYPES; i++) {
346 347 if (type == addr_types[i].va_type)
347 348 return (addr_types[i].va_str);
348 349 }
349 350 return (NULL);
350 351 }
351 352
352 353 dladm_status_t
353 354 dladm_vnic_str2macaddrtype(const char *str, vnic_mac_addr_type_t *val)
354 355 {
355 356 int i;
356 357 dladm_vnic_addr_type_t *type;
357 358
358 359 for (i = 0; i < NADDR_TYPES; i++) {
359 360 type = &addr_types[i];
360 361 if (strncmp(str, type->va_str, strlen(type->va_str)) == 0) {
361 362 *val = type->va_type;
362 363 return (DLADM_STATUS_OK);
363 364 }
364 365 }
365 366 return (DLADM_STATUS_BADARG);
366 367 }
367 368
368 369 /*
369 370 * Based on the VRRP specification, the virtual router MAC address associated
370 371 * with a virtual router is an IEEE 802 MAC address in the following format:
371 372 *
372 373 * IPv4 case: 00-00-5E-00-01-{VRID} (in hex in internet standard bit-order)
373 374 *
374 375 * IPv6 case: 00-00-5E-00-02-{VRID} (in hex in internet standard bit-order)
375 376 */
376 377 static dladm_status_t
377 378 i_dladm_vnic_vrrp_mac(vrid_t vrid, int af, uint8_t *mac, uint_t maclen)
378 379 {
379 380 if (maclen < ETHERADDRL || vrid < VRRP_VRID_MIN ||
380 381 vrid > VRRP_VRID_MAX || (af != AF_INET && af != AF_INET6)) {
381 382 return (DLADM_STATUS_BADARG);
382 383 }
383 384
384 385 mac[0] = mac[1] = mac[3] = 0x0;
385 386 mac[2] = 0x5e;
386 387 mac[4] = (af == AF_INET) ? 0x01 : 0x02;
387 388 mac[5] = vrid;
388 389 return (DLADM_STATUS_OK);
389 390 }
390 391
391 392 /*
392 393 * Create a new VNIC / VLAN. Update the configuration file and bring it up.
393 394 * The "vrid" and "af" arguments are only required if the mac_addr_type is
394 395 * VNIC_MAC_ADDR_TYPE_VRID. In that case, the MAC address will be caculated
395 396 * based on the above algorithm.
396 397 */
397 398 dladm_status_t
398 399 dladm_vnic_create(dladm_handle_t handle, const char *vnic, datalink_id_t linkid,
399 400 vnic_mac_addr_type_t mac_addr_type, uchar_t *mac_addr, uint_t mac_len,
400 401 int *mac_slot, uint_t mac_prefix_len, uint16_t vid, vrid_t vrid,
401 402 int af, datalink_id_t *vnic_id_out, dladm_arg_list_t *proplist,
402 403 uint32_t flags)
403 404 {
404 405 dladm_vnic_attr_t attr;
405 406 datalink_id_t vnic_id;
406 407 datalink_class_t class;
407 408 uint32_t media = DL_ETHER;
408 409 char name[MAXLINKNAMELEN];
409 410 uchar_t tmp_addr[MAXMACADDRLEN];
410 411 dladm_status_t status;
411 412 boolean_t is_vlan;
412 413 boolean_t is_etherstub;
413 414 int i;
414 415 boolean_t vnic_created = B_FALSE;
415 416 boolean_t conf_set = B_FALSE;
416 417
417 418 /*
418 419 * Sanity test arguments.
419 420 */
420 421 if ((flags & DLADM_OPT_ACTIVE) == 0)
421 422 return (DLADM_STATUS_NOTSUP);
422 423
423 424 is_vlan = ((flags & DLADM_OPT_VLAN) != 0);
424 425 if (is_vlan && ((vid < 1 || vid > 4094)))
425 426 return (DLADM_STATUS_VIDINVAL);
426 427
427 428 is_etherstub = (linkid == DATALINK_INVALID_LINKID);
428 429
429 430 if (!dladm_vnic_macaddrtype2str(mac_addr_type))
430 431 return (DLADM_STATUS_INVALIDMACADDRTYPE);
431 432
432 433 if ((flags & DLADM_OPT_ANCHOR) == 0) {
433 434 if ((status = dladm_datalink_id2info(handle, linkid, NULL,
434 435 &class, &media, NULL, 0)) != DLADM_STATUS_OK)
435 436 return (status);
436 437
437 438 if (class == DATALINK_CLASS_VNIC ||
438 439 class == DATALINK_CLASS_VLAN)
439 440 return (DLADM_STATUS_BADARG);
440 441 } else {
441 442 /* it's an anchor VNIC */
442 443 if (linkid != DATALINK_INVALID_LINKID || vid != 0)
443 444 return (DLADM_STATUS_BADARG);
444 445 }
445 446
446 447 /*
447 448 * Only VRRP VNIC need VRID and address family specified.
448 449 */
449 450 if (mac_addr_type != VNIC_MAC_ADDR_TYPE_VRID &&
450 451 (af != AF_UNSPEC || vrid != VRRP_VRID_NONE)) {
451 452 return (DLADM_STATUS_BADARG);
452 453 }
453 454
454 455 /*
455 456 * If a random address might be generated, but no prefix
456 457 * was specified by the caller, use the default MAC address
457 458 * prefix.
458 459 */
459 460 if ((mac_addr_type == VNIC_MAC_ADDR_TYPE_RANDOM ||
460 461 mac_addr_type == VNIC_MAC_ADDR_TYPE_AUTO) &&
461 462 mac_prefix_len == 0) {
462 463 mac_prefix_len = sizeof (dladm_vnic_def_prefix);
463 464 mac_addr = tmp_addr;
464 465 bcopy(dladm_vnic_def_prefix, mac_addr, mac_prefix_len);
465 466 }
466 467
467 468 /*
468 469 * If this is a VRRP VNIC, generate its MAC address using the given
469 470 * VRID and address family.
470 471 */
471 472 if (mac_addr_type == VNIC_MAC_ADDR_TYPE_VRID) {
472 473 /*
473 474 * VRRP VNICs must be created over ethernet data-links.
474 475 */
475 476 if (vrid < VRRP_VRID_MIN || vrid > VRRP_VRID_MAX ||
476 477 (af != AF_INET && af != AF_INET6) || mac_addr != NULL ||
477 478 mac_len != 0 || mac_prefix_len != 0 ||
478 479 (mac_slot != NULL && *mac_slot != -1) || is_etherstub ||
479 480 media != DL_ETHER) {
480 481 return (DLADM_STATUS_BADARG);
481 482 }
482 483 mac_len = ETHERADDRL;
483 484 mac_addr = tmp_addr;
484 485 status = i_dladm_vnic_vrrp_mac(vrid, af, mac_addr, mac_len);
485 486 if (status != DLADM_STATUS_OK)
486 487 return (status);
487 488 }
488 489
489 490 if (mac_len > MAXMACADDRLEN)
490 491 return (DLADM_STATUS_INVALIDMACADDRLEN);
491 492
492 493 if (vnic == NULL) {
493 494 flags |= DLADM_OPT_PREFIX;
494 495 (void) strlcpy(name, "vnic", sizeof (name));
495 496 } else {
496 497 (void) strlcpy(name, vnic, sizeof (name));
497 498 }
498 499
499 500 class = is_vlan ? DATALINK_CLASS_VLAN :
500 501 (is_etherstub ? DATALINK_CLASS_ETHERSTUB : DATALINK_CLASS_VNIC);
501 502 if ((status = dladm_create_datalink_id(handle, name, class,
502 503 media, flags, &vnic_id)) != DLADM_STATUS_OK)
503 504 return (status);
504 505
505 506 if ((flags & DLADM_OPT_PREFIX) != 0) {
506 507 (void) snprintf(name + 4, sizeof (name), "%llu", vnic_id);
507 508 flags &= ~DLADM_OPT_PREFIX;
508 509 }
509 510
510 511 bzero(&attr, sizeof (attr));
511 512
512 513 /* Extract resource_ctl and cpu_list from proplist */
513 514 if (proplist != NULL) {
514 515 status = dladm_link_proplist_extract(handle, proplist,
515 516 &attr.va_resource_props, 0);
516 517 if (status != DLADM_STATUS_OK)
517 518 goto done;
518 519 }
519 520
520 521 attr.va_vnic_id = vnic_id;
521 522 attr.va_link_id = linkid;
522 523 attr.va_mac_addr_type = mac_addr_type;
523 524 attr.va_mac_len = mac_len;
524 525 if (mac_slot != NULL)
525 526 attr.va_mac_slot = *mac_slot;
526 527 if (mac_len > 0)
527 528 bcopy(mac_addr, attr.va_mac_addr, mac_len);
528 529 else if (mac_prefix_len > 0)
529 530 bcopy(mac_addr, attr.va_mac_addr, mac_prefix_len);
530 531 attr.va_mac_prefix_len = mac_prefix_len;
531 532 attr.va_vid = vid;
|
↓ open down ↓ |
499 lines elided |
↑ open up ↑ |
532 533 attr.va_vrid = vrid;
533 534 attr.va_af = af;
534 535 attr.va_force = (flags & DLADM_OPT_FORCE) != 0;
535 536
536 537 status = i_dladm_vnic_create_sys(handle, &attr);
537 538 if (status != DLADM_STATUS_OK)
538 539 goto done;
539 540 vnic_created = B_TRUE;
540 541
541 542 /* Save vnic configuration and its properties */
542 - if (!(flags & DLADM_OPT_PERSIST))
543 - goto done;
543 + if (flags & DLADM_OPT_PERSIST) {
544 + status = dladm_vnic_persist_conf(handle, name, &attr, class);
545 + if (status == DLADM_STATUS_OK)
546 + conf_set = B_TRUE;
547 + }
544 548
545 - status = dladm_vnic_persist_conf(handle, name, &attr, class);
546 - if (status != DLADM_STATUS_OK)
547 - goto done;
548 - conf_set = B_TRUE;
549 +done:
550 + if (status == DLADM_STATUS_OK && proplist != NULL) {
551 + uint32_t flg;
549 552
550 - if (proplist != NULL) {
553 + flg = (flags & DLADM_OPT_PERSIST) ?
554 + DLADM_OPT_PERSIST : DLADM_OPT_ACTIVE;
555 +
551 556 for (i = 0; i < proplist->al_count; i++) {
552 557 dladm_arg_info_t *aip = &proplist->al_info[i];
553 558
559 + if (strcmp(aip->ai_name, "zone") == 0 &&
560 + flags & DLADM_OPT_TRANSIENT)
561 + flg |= DLADM_OPT_TRANSIENT;
562 + else
563 + flg &= ~DLADM_OPT_TRANSIENT;
564 +
554 565 status = dladm_set_linkprop(handle, vnic_id,
555 - aip->ai_name, aip->ai_val, aip->ai_count,
556 - DLADM_OPT_PERSIST);
566 + aip->ai_name, aip->ai_val, aip->ai_count, flg);
557 567 if (status != DLADM_STATUS_OK)
558 568 break;
559 569 }
560 570 }
561 571
562 -done:
563 572 if (status != DLADM_STATUS_OK) {
564 573 if (conf_set)
565 574 (void) dladm_remove_conf(handle, vnic_id);
566 575 if (vnic_created)
567 576 (void) i_dladm_vnic_delete_sys(handle, vnic_id);
568 577 (void) dladm_destroy_datalink_id(handle, vnic_id, flags);
569 578 } else {
570 579 if (vnic_id_out != NULL)
571 580 *vnic_id_out = vnic_id;
572 581 if (mac_slot != NULL)
573 582 *mac_slot = attr.va_mac_slot;
574 583 }
575 584
576 585 if (is_vlan) {
577 586 dladm_status_t stat2;
578 587
579 588 stat2 = dladm_bridge_refresh(handle, linkid);
580 589 if (status == DLADM_STATUS_OK && stat2 != DLADM_STATUS_OK)
581 590 status = stat2;
582 591 }
583 592 return (status);
584 593 }
585 594
586 595 /*
587 596 * Delete a VNIC / VLAN.
588 597 */
589 598 dladm_status_t
590 599 dladm_vnic_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
591 600 {
592 601 dladm_status_t status;
593 602 datalink_class_t class;
594 603
595 604 if (flags == 0)
596 605 return (DLADM_STATUS_BADARG);
597 606
598 607 if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL, 0)
599 608 != DLADM_STATUS_OK))
600 609 return (DLADM_STATUS_BADARG);
601 610
602 611 if ((flags & DLADM_OPT_VLAN) != 0) {
603 612 if (class != DATALINK_CLASS_VLAN)
604 613 return (DLADM_STATUS_BADARG);
605 614 } else {
606 615 if (class != DATALINK_CLASS_VNIC &&
607 616 class != DATALINK_CLASS_ETHERSTUB)
608 617 return (DLADM_STATUS_BADARG);
609 618 }
610 619
611 620 if ((flags & DLADM_OPT_ACTIVE) != 0) {
612 621 status = i_dladm_vnic_delete_sys(handle, linkid);
613 622 if (status == DLADM_STATUS_OK) {
614 623 (void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0,
615 624 DLADM_OPT_ACTIVE);
616 625 (void) dladm_destroy_datalink_id(handle, linkid,
617 626 DLADM_OPT_ACTIVE);
618 627 } else if (status != DLADM_STATUS_NOTFOUND ||
619 628 !(flags & DLADM_OPT_PERSIST)) {
620 629 return (status);
621 630 }
622 631 }
623 632 if ((flags & DLADM_OPT_PERSIST) != 0) {
624 633 (void) dladm_remove_conf(handle, linkid);
625 634 (void) dladm_destroy_datalink_id(handle, linkid,
626 635 DLADM_OPT_PERSIST);
627 636 }
628 637 return (dladm_bridge_refresh(handle, linkid));
629 638 }
630 639
631 640 static const char *
632 641 dladm_vnic_macaddr2str(const uchar_t *mac, char *buf)
633 642 {
634 643 static char unknown_mac[] = {0, 0, 0, 0, 0, 0};
635 644
636 645 if (buf == NULL)
637 646 return (NULL);
638 647
639 648 if (bcmp(unknown_mac, mac, ETHERADDRL) == 0)
640 649 (void) strlcpy(buf, "unknown", DLADM_STRSIZE);
641 650 else
642 651 return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER));
643 652
644 653 return (buf);
645 654 }
646 655
647 656 static dladm_status_t
648 657 dladm_vnic_str2macaddr(const char *str, uchar_t *buf)
649 658 {
650 659 int len = 0;
651 660 uchar_t *b = _link_aton(str, &len);
652 661
653 662 if (b == NULL || len >= MAXMACADDRLEN)
654 663 return (DLADM_STATUS_BADARG);
655 664
656 665 bcopy(b, buf, len);
657 666 free(b);
658 667 return (DLADM_STATUS_OK);
659 668 }
660 669
661 670
662 671 static dladm_status_t
663 672 dladm_vnic_persist_conf(dladm_handle_t handle, const char *name,
664 673 dladm_vnic_attr_t *attrp, datalink_class_t class)
665 674 {
666 675 dladm_conf_t conf;
667 676 dladm_status_t status;
668 677 char macstr[ETHERADDRL * 3];
669 678 char linkover[MAXLINKNAMELEN];
670 679 uint64_t u64;
671 680
672 681 if ((status = dladm_create_conf(handle, name, attrp->va_vnic_id,
673 682 class, DL_ETHER, &conf)) != DLADM_STATUS_OK)
674 683 return (status);
675 684
676 685 if (attrp->va_link_id != DATALINK_INVALID_LINKID) {
677 686 status = dladm_datalink_id2info(handle, attrp->va_link_id, NULL,
678 687 NULL, NULL, linkover, sizeof (linkover));
679 688 if (status != DLADM_STATUS_OK)
680 689 goto done;
681 690 status = dladm_set_conf_field(handle, conf, FLINKOVER,
682 691 DLADM_TYPE_STR, linkover);
683 692 if (status != DLADM_STATUS_OK)
684 693 goto done;
685 694 }
686 695
687 696 if (class != DATALINK_CLASS_VLAN) {
688 697 u64 = attrp->va_mac_addr_type;
689 698 status = dladm_set_conf_field(handle, conf, FMADDRTYPE,
690 699 DLADM_TYPE_UINT64, &u64);
691 700 if (status != DLADM_STATUS_OK)
692 701 goto done;
693 702
694 703 u64 = attrp->va_vrid;
695 704 status = dladm_set_conf_field(handle, conf, FVRID,
696 705 DLADM_TYPE_UINT64, &u64);
697 706 if (status != DLADM_STATUS_OK)
698 707 goto done;
699 708
700 709 u64 = attrp->va_af;
701 710 status = dladm_set_conf_field(handle, conf, FVRAF,
702 711 DLADM_TYPE_UINT64, &u64);
703 712 if (status != DLADM_STATUS_OK)
704 713 goto done;
705 714
706 715 if (attrp->va_mac_len != ETHERADDRL) {
707 716 u64 = attrp->va_mac_len;
708 717 status = dladm_set_conf_field(handle, conf, FMADDRLEN,
709 718 DLADM_TYPE_UINT64, &u64);
710 719 if (status != DLADM_STATUS_OK)
711 720 goto done;
712 721 }
713 722
714 723 if (attrp->va_mac_slot != -1) {
715 724 u64 = attrp->va_mac_slot;
716 725 status = dladm_set_conf_field(handle, conf,
717 726 FMADDRSLOT, DLADM_TYPE_UINT64, &u64);
718 727 if (status != DLADM_STATUS_OK)
719 728 goto done;
720 729 }
721 730
722 731 if (attrp->va_mac_prefix_len !=
723 732 sizeof (dladm_vnic_def_prefix)) {
724 733 u64 = attrp->va_mac_prefix_len;
725 734 status = dladm_set_conf_field(handle, conf,
726 735 FMADDRPREFIXLEN, DLADM_TYPE_UINT64, &u64);
727 736 if (status != DLADM_STATUS_OK)
728 737 goto done;
729 738 }
730 739
731 740 (void) dladm_vnic_macaddr2str(attrp->va_mac_addr, macstr);
732 741 status = dladm_set_conf_field(handle, conf, FMACADDR,
733 742 DLADM_TYPE_STR, macstr);
734 743 if (status != DLADM_STATUS_OK)
735 744 goto done;
736 745 }
737 746
738 747 if (attrp->va_vid != 0) {
739 748 u64 = attrp->va_vid;
740 749 status = dladm_set_conf_field(handle, conf, FVLANID,
741 750 DLADM_TYPE_UINT64, &u64);
742 751 if (status != DLADM_STATUS_OK)
743 752 goto done;
744 753 }
745 754
746 755 /*
747 756 * Commit the link configuration.
748 757 */
749 758 status = dladm_write_conf(handle, conf);
750 759
751 760 done:
752 761 dladm_destroy_conf(handle, conf);
753 762 return (status);
754 763 }
755 764
756 765 typedef struct dladm_vnic_up_arg_s {
757 766 uint32_t flags;
758 767 dladm_status_t status;
759 768 } dladm_vnic_up_arg_t;
760 769
761 770 static int
762 771 i_dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, void *arg)
763 772 {
764 773 dladm_status_t *statusp = &(((dladm_vnic_up_arg_t *)arg)->status);
765 774 dladm_vnic_attr_t attr;
766 775 dladm_status_t status;
767 776 dladm_arg_list_t *proplist;
768 777
769 778 bzero(&attr, sizeof (attr));
770 779
771 780 status = dladm_vnic_info(handle, linkid, &attr, DLADM_OPT_PERSIST);
772 781 if (status != DLADM_STATUS_OK)
773 782 goto done;
774 783
775 784 /* Get all properties for this vnic */
776 785 status = dladm_link_get_proplist(handle, linkid, &proplist);
777 786 if (status != DLADM_STATUS_OK)
778 787 goto done;
779 788
780 789 if (proplist != NULL) {
781 790 status = dladm_link_proplist_extract(handle, proplist,
782 791 &attr.va_resource_props, DLADM_OPT_BOOT);
783 792 }
784 793
785 794 status = i_dladm_vnic_create_sys(handle, &attr);
786 795 if (status == DLADM_STATUS_OK) {
787 796 status = dladm_up_datalink_id(handle, linkid);
788 797 if (status != DLADM_STATUS_OK)
789 798 (void) i_dladm_vnic_delete_sys(handle, linkid);
790 799 }
791 800
792 801 done:
793 802 *statusp = status;
794 803 return (DLADM_WALK_CONTINUE);
795 804 }
796 805
797 806 dladm_status_t
798 807 dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
799 808 {
800 809 dladm_vnic_up_arg_t vnic_arg;
801 810 datalink_class_t class;
802 811
803 812 class = ((flags & DLADM_OPT_VLAN) != 0) ? DATALINK_CLASS_VLAN :
804 813 (DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB);
805 814
806 815 if (linkid == DATALINK_ALL_LINKID) {
807 816 (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle,
808 817 &vnic_arg, class, DATALINK_ANY_MEDIATYPE,
809 818 DLADM_OPT_PERSIST);
810 819 return (DLADM_STATUS_OK);
811 820 } else {
812 821 (void) i_dladm_vnic_up(handle, linkid, &vnic_arg);
813 822 return (vnic_arg.status);
814 823 }
815 824 }
|
↓ open down ↓ |
243 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX