1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <libintl.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <libdladm_impl.h>
#include <libdlvnic.h>
/*
* VNIC administration library.
*/
#define VNIC_DEV "/devices/pseudo/vnic@0:" VNIC_CTL_NODE_NAME
/*
* Because by default the id is used as the DLPI device PPA and default
* VLAN PPA's are calculated as ((1000 * vid) + PPA), the largest id
* can't be > 999. We reserve the last 100 VNIC ids for automatic
* VNIC id assignment.
*/
#define DLADM_VNIC_MIN_VNIC_ID 1 /* total range */
#define DLADM_VNIC_MAX_VNIC_ID 999
#define DLADM_VNIC_MIN_VNIC_SPEC_ID 1 /* specified by user */
#define DLADM_VNIC_MAX_VNIC_SPEC_ID 899
#define DLADM_VNIC_MIN_VNIC_AUTO_ID 900 /* picked automatically */
#define DLADM_VNIC_MAX_VNIC_AUTO_ID 999
#define DLADM_VNIC_NUM_VNIC_AUTO_ID (DLADM_VNIC_MAX_VNIC_AUTO_ID - \
DLADM_VNIC_MIN_VNIC_AUTO_ID + 1)
/* Limits on buffer size for VNIC_IOC_INFO request */
#define MIN_INFO_SIZE (4*1024)
#define MAX_INFO_SIZE (128*1024)
/* configuration database entry */
typedef struct dladm_vnic_attr_db {
uint_t vt_vnic_id;
char vt_dev_name[MAXNAMELEN];
vnic_mac_addr_type_t vt_mac_addr_type;
uint_t vt_mac_len;
uchar_t vt_mac_addr[MAXMACADDRLEN];
} dladm_vnic_attr_db_t;
typedef struct dladm_vnic_up {
uint_t vu_vnic_id;
boolean_t vu_found;
int vu_fd;
} dladm_vnic_up_t;
typedef struct dladm_vnic_down {
uint32_t vd_vnic_id;
boolean_t vd_found;
} dladm_vnic_down_t;
typedef struct dladm_vnic_modify {
uint32_t vm_vnic_id;
boolean_t vm_found;
} dladm_vnic_modify_t;
typedef struct dladm_vnic_modify_attr {
vnic_mac_addr_type_t vm_mac_addr_type;
int vm_mac_len;
uchar_t vm_mac_addr[MAXMACADDRLEN];
} dladm_vnic_modify_attr_t;
/*
* Send a create command to the VNIC driver.
*/
static dladm_status_t
i_dladm_vnic_create_sys(int fd, dladm_vnic_attr_db_t *attr)
{
int rc;
vnic_ioc_create_t ioc;
ioc.vc_vnic_id = attr->vt_vnic_id;
bcopy(attr->vt_dev_name, ioc.vc_dev_name, MAXNAMELEN);
ioc.vc_mac_addr_type = attr->vt_mac_addr_type;
ioc.vc_mac_len = attr->vt_mac_len;
bcopy(attr->vt_mac_addr, ioc.vc_mac_addr, attr->vt_mac_len);
rc = i_dladm_ioctl(fd, VNIC_IOC_CREATE, &ioc, sizeof (ioc));
if (rc < 0)
return (dladm_errno2status(errno));
return (DLADM_STATUS_OK);
}
/*
* Invoked to bring up a VNIC.
*/
static dladm_status_t
i_dladm_vnic_up(void *arg, dladm_vnic_attr_db_t *attr)
{
dladm_vnic_up_t *up = (dladm_vnic_up_t *)arg;
dladm_status_t status;
if (up->vu_vnic_id != 0 && up->vu_vnic_id != attr->vt_vnic_id)
return (DLADM_STATUS_OK);
up->vu_found = B_TRUE;
status = i_dladm_vnic_create_sys(up->vu_fd, attr);
if ((status != DLADM_STATUS_OK) && (up->vu_vnic_id != 0))
return (status);
return (DLADM_STATUS_OK);
}
/*
* Send a modify command to the VNIC driver.
*/
static dladm_status_t
i_dladm_vnic_modify_sys(uint_t vnic_id, uint32_t modify_mask,
dladm_vnic_modify_attr_t *attr)
{
int rc;
int fd;
vnic_ioc_modify_t ioc;
ioc.vm_vnic_id = vnic_id;
ioc.vm_modify_mask = 0;
if (modify_mask & DLADM_VNIC_MODIFY_ADDR)
ioc.vm_modify_mask |= VNIC_IOC_MODIFY_ADDR;
ioc.vm_mac_addr_type = attr->vm_mac_addr_type;
ioc.vm_mac_len = attr->vm_mac_len;
bcopy(attr->vm_mac_addr, ioc.vm_mac_addr, MAXMACADDRLEN);
if ((fd = open(VNIC_DEV, O_RDWR)) < 0)
return (dladm_errno2status(errno));
rc = i_dladm_ioctl(fd, VNIC_IOC_MODIFY, &ioc, sizeof (ioc));
(void) close(fd);
if (rc < 0)
return (dladm_errno2status(errno));
return (DLADM_STATUS_OK);
}
/*
* Walk through the vnics defined on the system and for each vnic <vnic>,
* invoke <fn>(<arg>, <vnic>);
*/
dladm_status_t
dladm_vnic_walk_sys(dladm_status_t (*fn)(void *, dladm_vnic_attr_sys_t *),
void *arg)
{
vnic_ioc_info_t *ioc;
vnic_ioc_info_vnic_t *vnic;
dladm_vnic_attr_sys_t attr;
int rc, i, bufsize, fd;
char *where;
dladm_status_t status = DLADM_STATUS_OK;
if ((fd = open(VNIC_DEV, O_RDWR)) == -1)
return (dladm_errno2status(errno));
bufsize = MIN_INFO_SIZE;
ioc = (vnic_ioc_info_t *)calloc(1, bufsize);
if (ioc == NULL) {
(void) close(fd);
return (dladm_errno2status(ENOMEM));
}
tryagain:
rc = i_dladm_ioctl(fd, VNIC_IOC_INFO, ioc, bufsize);
if (rc != 0) {
if (errno == ENOSPC) {
bufsize *= 2;
if (bufsize <= MAX_INFO_SIZE) {
ioc = (vnic_ioc_info_t *)realloc(ioc, bufsize);
if (ioc != NULL) {
bzero(ioc, bufsize);
goto tryagain;
}
}
}
status = dladm_errno2status(errno);
goto bail;
}
/*
* Go through each vnic returned by the vnic driver
*/
where = (char *)(ioc + 1);
for (i = 0; i < ioc->vi_nvnics; i++) {
/* LINTED E_BAD_PTR_CAST_ALIGN */
vnic = (vnic_ioc_info_vnic_t *)where;
attr.va_vnic_id = vnic->vn_vnic_id;
bcopy(vnic->vn_dev_name, attr.va_dev_name,
MAXNAMELEN);
attr.va_mac_addr_type = vnic->vn_mac_addr_type;
bcopy(vnic->vn_mac_addr, attr.va_mac_addr, ETHERADDRL);
attr.va_mac_len = vnic->vn_mac_len;
where = (char *)(vnic + 1);
status = fn(arg, &attr);
if (status != DLADM_STATUS_OK)
goto bail;
}
bail:
free(ioc);
(void) close(fd);
return (status);
}
/*
* Remove a VNIC from the kernel.
*/
static dladm_status_t
i_dladm_vnic_delete_sys(int fd, dladm_vnic_attr_sys_t *attr)
{
vnic_ioc_delete_t ioc;
int rc;
ioc.vd_vnic_id = attr->va_vnic_id;
rc = i_dladm_ioctl(fd, VNIC_IOC_DELETE, &ioc, sizeof (ioc));
if (rc < 0)
return (dladm_errno2status(errno));
return (DLADM_STATUS_OK);
}
/*
* Invoked to bring down a VNIC.
*/
static dladm_status_t
i_dladm_vnic_down(void *arg, dladm_vnic_attr_sys_t *attr)
{
dladm_vnic_down_t *down = (dladm_vnic_down_t *)arg;
int fd;
dladm_status_t status;
if (down->vd_vnic_id != 0 && down->vd_vnic_id != attr->va_vnic_id)
return (DLADM_STATUS_OK);
down->vd_found = B_TRUE;
if ((fd = open(VNIC_DEV, O_RDWR)) < 0)
return (dladm_errno2status(errno));
status = i_dladm_vnic_delete_sys(fd, attr);
if ((status != DLADM_STATUS_OK) && (down->vd_vnic_id != 0)) {
(void) close(fd);
return (status);
}
(void) close(fd);
return (DLADM_STATUS_OK);
}
/*
* Convert between MAC address types and their string representations.
*/
typedef struct dladm_vnic_addr_type_s {
char *va_str;
vnic_mac_addr_type_t va_type;
} dladm_vnic_addr_type_t;
static dladm_vnic_addr_type_t addr_types[] = {
{"fixed", VNIC_MAC_ADDR_TYPE_FIXED},
};
#define NADDR_TYPES (sizeof (addr_types) / sizeof (dladm_vnic_addr_type_t))
/* returns B_TRUE if a matching type was found, B_FALSE otherwise */
boolean_t
dladm_vnic_mac_addr_str_to_type(const char *str, vnic_mac_addr_type_t *val)
{
int i;
dladm_vnic_addr_type_t *type;
for (i = 0; i < NADDR_TYPES; i++) {
type = &addr_types[i];
if (strncmp(str, type->va_str, strlen(type->va_str)) == 0) {
*val = type->va_type;
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* Select a VNIC id automatically.
*/
typedef struct dladm_vnic_auto_state_s {
uint_t as_nslots;
uint_t *as_slots;
} dladm_vnic_auto_state_t;
static dladm_status_t
i_dladm_vnic_create_auto_walker(void *arg, dladm_vnic_attr_sys_t *attr)
{
dladm_vnic_auto_state_t *state = arg;
if (attr->va_vnic_id < DLADM_VNIC_MIN_VNIC_AUTO_ID ||
attr->va_vnic_id > DLADM_VNIC_MAX_VNIC_AUTO_ID)
return (DLADM_STATUS_OK);
state->as_slots[state->as_nslots++] = attr->va_vnic_id;
return (DLADM_STATUS_OK);
}
static int
i_dladm_vnic_compare(const void *p1, const void *p2)
{
uint_t i = *((uint_t *)p1);
uint_t j = *((uint_t *)p2);
if (i > j)
return (1);
if (i < j)
return (-1);
return (0);
}
/*ARGSUSED*/
static dladm_status_t
i_dladm_vnic_get_auto_id(dladm_vnic_attr_db_t *attr, uint32_t *vnic_id_out)
{
dladm_vnic_auto_state_t state;
uint_t vnic_ids[DLADM_VNIC_NUM_VNIC_AUTO_ID];
int i;
uint_t last_id, vnic_id;
dladm_status_t status;
/*
* Build a sorted array containing the existing VNIC ids in the range
* allocated for automatic allocation.
*/
state.as_nslots = 0;
state.as_slots = vnic_ids;
status = dladm_vnic_walk_sys(i_dladm_vnic_create_auto_walker, &state);
if (status != DLADM_STATUS_OK)
return (status);
qsort(vnic_ids, state.as_nslots, sizeof (uint_t),
i_dladm_vnic_compare);
/*
* Find a gap in the sequence of existing VNIC ids.
*/
last_id = DLADM_VNIC_MIN_VNIC_AUTO_ID - 1;
vnic_id = 0;
for (i = 0; i < state.as_nslots; i++) {
if (vnic_ids[i] > (last_id + 1)) {
vnic_id = last_id + 1;
break;
}
last_id = vnic_ids[i];
}
if (vnic_id == 0) {
/*
* Did not find a gap between existing entries, see if we
* can add one.
*/
if (last_id + 1 > DLADM_VNIC_MAX_VNIC_AUTO_ID)
return (DLADM_STATUS_AUTOIDNOAVAILABLEID);
/* still have room for one more VNIC */
vnic_id = last_id + 1;
}
*vnic_id_out = vnic_id;
return (DLADM_STATUS_OK);
}
/*
* Create a new VNIC. Update the configuration file and bring it up.
*/
dladm_status_t
dladm_vnic_create(uint_t vnic_id, char *dev_name,
vnic_mac_addr_type_t mac_addr_type, uchar_t *mac_addr, int mac_len,
uint_t *vnic_id_out, uint32_t flags)
{
dladm_vnic_attr_db_t attr;
int i;
boolean_t tempop = ((flags & DLADM_VNIC_OPT_TEMP) != 0);
boolean_t autoid = ((flags & DLADM_VNIC_OPT_AUTOID) != 0);
dladm_vnic_up_t up;
dladm_status_t status;
/*
* Sanity test arguments.
*/
if (autoid && !tempop)
return (DLADM_STATUS_AUTOIDNOTEMP);
if (!autoid && ((vnic_id < DLADM_VNIC_MIN_VNIC_SPEC_ID) ||
(vnic_id > DLADM_VNIC_MAX_VNIC_SPEC_ID)))
return (DLADM_STATUS_INVALIDID);
if (mac_len > MAXMACADDRLEN)
return (DLADM_STATUS_INVALIDMACADDRLEN);
for (i = 0; i < NADDR_TYPES; i++) {
if (mac_addr_type == addr_types[i].va_type)
break;
}
if (i == NADDR_TYPES)
return (DLADM_STATUS_INVALIDMACADDRTYPE);
/* for now, only temporary creations are supported */
if (!tempop)
return (dladm_errno2status(ENOTSUP));
auto_again:
if (autoid) {
/*
* Find an unused VNIC id.
*/
status = i_dladm_vnic_get_auto_id(&attr, vnic_id_out);
if (status != DLADM_STATUS_OK)
return (status);
vnic_id = *vnic_id_out;
}
bzero(&attr, sizeof (attr));
attr.vt_vnic_id = vnic_id;
(void) strncpy(attr.vt_dev_name, dev_name,
sizeof (attr.vt_dev_name) - 1);
attr.vt_mac_addr_type = mac_addr_type;
attr.vt_mac_len = mac_len;
bcopy(mac_addr, attr.vt_mac_addr, mac_len);
up.vu_vnic_id = vnic_id;
up.vu_found = B_FALSE;
up.vu_fd = open(VNIC_DEV, O_RDWR);
if (up.vu_fd < 0)
return (dladm_errno2status(errno));
status = i_dladm_vnic_up((void *)&up, &attr);
(void) close(up.vu_fd);
if ((status == DLADM_STATUS_EXIST) && autoid)
goto auto_again;
return (status);
}
/*
* Modify the properties of a VNIC.
*/
dladm_status_t
dladm_vnic_modify(uint_t vnic_id, uint32_t modify_mask,
vnic_mac_addr_type_t mac_addr_type, uint_t mac_len, uchar_t *mac_addr,
uint32_t flags)
{
dladm_vnic_modify_attr_t new_attr;
boolean_t tempop = ((flags & DLADM_VNIC_OPT_TEMP) != 0);
if ((vnic_id < DLADM_VNIC_MIN_VNIC_ID) ||
(vnic_id > DLADM_VNIC_MAX_VNIC_ID))
return (DLADM_STATUS_INVALIDID);
/* for now, only temporary creations are supported */
if (!tempop)
return (dladm_errno2status(ENOTSUP));
bzero(&new_attr, sizeof (new_attr));
if (modify_mask & DLADM_VNIC_MODIFY_ADDR) {
new_attr.vm_mac_addr_type = mac_addr_type;
new_attr.vm_mac_len = mac_len;
bcopy(mac_addr, new_attr.vm_mac_addr, MAXMACADDRLEN);
}
/* update the properties of the existing VNIC */
return (i_dladm_vnic_modify_sys(vnic_id, modify_mask, &new_attr));
}
/*
* Delete a VNIC.
*/
dladm_status_t
dladm_vnic_delete(uint_t vnic_id, uint32_t flags)
{
boolean_t tempop = ((flags & DLADM_VNIC_OPT_TEMP) != 0);
dladm_vnic_down_t down;
dladm_vnic_attr_sys_t sys_attr;
if ((vnic_id < DLADM_VNIC_MIN_VNIC_ID) ||
(vnic_id > DLADM_VNIC_MAX_VNIC_ID))
return (DLADM_STATUS_INVALIDID);
/* for now, only temporary deletes are supported */
if (!tempop)
return (dladm_errno2status(ENOTSUP));
down.vd_vnic_id = vnic_id;
down.vd_found = B_FALSE;
sys_attr.va_vnic_id = vnic_id;
return (i_dladm_vnic_down((void *)&down, &sys_attr));
}
|