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
|
/*
* 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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <libintl.h>
#include <libscf.h>
#include <libnvpair.h>
#include <libstmf.h>
#include <libsrpt.h>
#include "srpt_common.h"
#define SRPT_PROV_NAME "srpt"
/*
* Function: srpt_GetConfig()
*
* Parameters:
* cfg Current SRPT configuration in nvlist form
* token Configuration generation number. Use this token
* if updating the configuration with srpt_SetConfig.
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
*/
int
srpt_GetConfig(nvlist_t **cfg, uint64_t *token)
{
int ret = 0;
nvlist_t *cfg_nv = NULL;
uint64_t stmf_token = 0;
nvlist_t *hcanv = NULL;
if (!cfg) {
return (EINVAL);
}
*cfg = NULL;
ret = stmfGetProviderDataProt(SRPT_PROV_NAME, &cfg_nv,
STMF_PORT_PROVIDER_TYPE, &stmf_token);
if (ret == STMF_STATUS_SUCCESS) {
ret = 0;
} else if (ret == STMF_ERROR_NOT_FOUND) {
/* Not initialized yet */
ret = nvlist_alloc(&cfg_nv, NV_UNIQUE_NAME, 0);
if (ret != 0) {
return (ret);
}
/* create the HCA list */
ret = nvlist_alloc(&hcanv, NV_UNIQUE_NAME, 0);
if (ret == 0) {
ret = nvlist_add_nvlist(cfg_nv, SRPT_PROP_HCALIST,
hcanv);
if (ret != 0) {
nvlist_free(hcanv);
}
}
if (ret != 0) {
nvlist_free(cfg_nv);
cfg_nv = NULL;
}
} else if (ret == STMF_ERROR_NOMEM) {
ret = ENOMEM;
} else {
ret = EINVAL;
}
*cfg = cfg_nv;
*token = stmf_token;
return (ret);
}
/*
* Function: srpt_SetConfig()
*
* Parameters:
* cfg SRPT configuration in nvlist form
* token Configuration generation number from srpt_GetConfig.
* Use this token to ensure the configuration hasn't been
* updated by another user since the time it was fetched.
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
* ECANCELED Configuration updated by another user
*/
int
srpt_SetConfig(nvlist_t *cfg, uint64_t token)
{
int ret = 0;
ret = stmfSetProviderDataProt(SRPT_PROV_NAME, cfg,
STMF_PORT_PROVIDER_TYPE, &token);
if (ret == STMF_STATUS_SUCCESS) {
ret = 0;
} else if (ret == STMF_ERROR_NOMEM) {
ret = ENOMEM;
} else if (ret == STMF_ERROR_PROV_DATA_STALE) {
ret = ECANCELED; /* could be a better errno */
} else {
ret = EINVAL;
}
return (ret);
}
/*
* Function: srpt_GetDefaultState()
*
* Parameters:
* enabled If B_TRUE, indicates that targets will be created for all
* discovered HCAs that have not been specifically disabled.
* If B_FALSE, targets will not be created unless the HCA has
* been specifically enabled. See also srpt_SetDefaultState().
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
*/
int
srpt_GetDefaultState(boolean_t *enabled)
{
int ret;
nvlist_t *cfgnv;
uint64_t token;
boolean_t val = B_TRUE;
if (enabled == NULL) {
return (EINVAL);
}
ret = srpt_GetConfig(&cfgnv, &token);
if (ret != 0) {
return (ret);
}
if (cfgnv != NULL) {
ret = nvlist_lookup_boolean_value(cfgnv,
SRPT_PROP_DEFAULT_ENABLED, &val);
if (ret == ENOENT) {
ret = 0;
}
}
*enabled = val;
return (ret);
}
/*
* Function: srpt_SetDefaultState()
*
* Parameters:
* enabled If B_TRUE, indicates that targets will be created for all
* discovered HCAs that have not been specifically disabled.
* If B_FALSE, targets will not be created unless the HCA has
* been specifically enabled. See also srpt_SetDefaultState().
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
*/
int
srpt_SetDefaultState(boolean_t enabled)
{
int ret;
nvlist_t *cfgnv;
uint64_t token;
ret = srpt_GetConfig(&cfgnv, &token);
if (ret != 0) {
return (ret);
}
if (cfgnv == NULL) {
ret = nvlist_alloc(&cfgnv, NV_UNIQUE_NAME, 0);
if (ret != 0) {
return (ret);
}
}
ret = nvlist_add_boolean_value(cfgnv, SRPT_PROP_DEFAULT_ENABLED,
enabled);
if (ret == 0) {
ret = srpt_SetConfig(cfgnv, token);
}
nvlist_free(cfgnv);
return (ret);
}
/*
* Function: srpt_SetTargetState()
*
* Parameters:
* hca_guid HCA GUID. See description of srpt_NormalizeGuid
* enabled If B_TRUE, indicates that a target will be created for
* this HCA when the SRPT SMF service is enabled. If B_FALSE,
* a target will not be created
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
*/
int
srpt_SetTargetState(char *hca_guid, boolean_t enabled)
{
int ret;
nvlist_t *cfgnv;
uint64_t token;
nvlist_t *hcalist;
nvlist_t *hcanv;
char guid[32];
uint64_t hcaguid;
if (hca_guid == NULL) {
return (EINVAL);
}
ret = srpt_NormalizeGuid(hca_guid, guid, sizeof (guid), &hcaguid);
if (ret != 0) {
return (ret);
}
ret = srpt_GetConfig(&cfgnv, &token);
if (ret != 0) {
return (ret);
}
/* get the list of HCAs */
ret = nvlist_lookup_nvlist(cfgnv, SRPT_PROP_HCALIST, &hcalist);
if (ret != 0) {
nvlist_free(cfgnv);
return (ret);
}
ret = nvlist_lookup_nvlist(hcalist, guid, &hcanv);
if (ret == ENOENT) {
/* no entry yet */
ret = nvlist_alloc(&hcanv, NV_UNIQUE_NAME, 0);
if (ret == 0) {
ret = nvlist_add_uint64(hcanv, SRPT_PROP_GUID, hcaguid);
}
}
if (ret == 0) {
ret = nvlist_add_boolean_value(hcanv, SRPT_PROP_ENABLED,
enabled);
}
if (ret == 0) {
ret = nvlist_add_nvlist(hcalist, guid, hcanv);
}
if (ret == 0) {
ret = srpt_SetConfig(cfgnv, token);
}
nvlist_free(cfgnv);
return (ret);
}
/*
* Function: srpt_GetTargetState()
*
* Parameters:
* hca_guid HCA GUID. See description of srpt_NormalizeGuid
* enabled If B_TRUE, indicates that a target will be created for
* this HCA when the SRPT SMF service is enabled. If B_FALSE,
* a target will not be created
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
*/
int
srpt_GetTargetState(char *hca_guid, boolean_t *enabled)
{
int ret;
nvlist_t *cfgnv;
uint64_t token;
nvlist_t *hcalist;
nvlist_t *hcanv;
boolean_t defaultState = B_TRUE;
char guid[32];
if (hca_guid == NULL) {
return (EINVAL);
}
ret = srpt_NormalizeGuid(hca_guid, guid, sizeof (guid), NULL);
if (ret != 0) {
return (ret);
}
ret = srpt_GetConfig(&cfgnv, &token);
if (ret != 0) {
return (ret);
}
/* get the list of HCAs */
ret = nvlist_lookup_nvlist(cfgnv, SRPT_PROP_HCALIST, &hcalist);
if (ret != 0) {
nvlist_free(cfgnv);
return (ret);
}
/*
* Find the default, for the likely case that this HCA isn't
* explicitly set.
*/
(void) nvlist_lookup_boolean_value(cfgnv, SRPT_PROP_DEFAULT_ENABLED,
&defaultState);
ret = nvlist_lookup_nvlist(hcalist, guid, &hcanv);
if (ret == 0) {
ret = nvlist_lookup_boolean_value(hcanv, SRPT_PROP_ENABLED,
enabled);
}
if (ret == ENOENT) {
/* not explicitly set, use the default */
*enabled = defaultState;
ret = 0;
}
nvlist_free(cfgnv);
return (ret);
}
/*
* Function: srpt_ResetTarget()
*
* Clears the HCA-specific configuration. Target creation will revert to
* the default.
*
* Parameters:
* hca_guid HCA GUID. See description of srpt_NormalizeGuid
*
* Return Values:
* 0 Success
* ENOMEM Could not allocate resources
* EINVAL Invalid parameter
*/
int
srpt_ResetTarget(char *hca_guid)
{
int ret;
nvlist_t *cfgnv;
nvlist_t *hcalist;
uint64_t token;
char guid[32];
if (hca_guid == NULL) {
return (EINVAL);
}
ret = srpt_NormalizeGuid(hca_guid, guid, sizeof (guid), NULL);
if (ret != 0) {
return (ret);
}
ret = srpt_GetConfig(&cfgnv, &token);
if (ret != 0) {
return (ret);
}
/* get the list of HCAs */
ret = nvlist_lookup_nvlist(cfgnv, SRPT_PROP_HCALIST, &hcalist);
if (ret != 0) {
nvlist_free(cfgnv);
return (ret);
}
/* don't set config if we don't actually change anything */
if (nvlist_exists(hcalist, guid)) {
(void) nvlist_remove_all(hcalist, guid);
if (ret == 0) {
ret = srpt_SetConfig(cfgnv, token);
}
}
nvlist_free(cfgnv);
return (ret);
}
/*
* srpt_NormalizeGuid()
*
* Parameters:
* in HCA GUID. Must be in one of the following forms:
* 3BA000100CD18 - base hex form
* 0003BA000100CD18 - base hex form with leading zeroes
* hca:3BA000100CD18 - form from cfgadm and/or /dev/cfg
* eui.0003BA000100CD18 - EUI form
*
* buf Buffer to hold normalized guid string. Must be at least
* 17 chars long.
* buflen Length of provided buffer
* int_guid Optional. If not NULL, the integer form of the GUID will also
* be returned.
* Return Values:
* 0 Success
* EINVAL Invalid HCA GUID or invalid parameter.
*/
int
srpt_NormalizeGuid(char *in, char *buf, size_t buflen, uint64_t *int_guid)
{
uint64_t guid;
char *bufp = in;
char *end = NULL;
if ((in == NULL) || (buf == NULL)) {
return (EINVAL);
}
if (strncasecmp(bufp, "eui.", 4) == 0) {
/* EUI form */
bufp += 4;
} else if (strncasecmp(bufp, "hca:", 4) == 0) {
/* cfgadm and /dev/hca form */
bufp += 4;
}
/*
* strtoull() does not return EINVAL as documented. Lucky
* for us, neither 0 nor ULLONG_MAX will be valid. Trap on
* those and fail.
*/
guid = strtoull(bufp, &end, 16);
if ((guid == 0) || (guid == ULLONG_MAX) ||
((end != NULL) && (strlen(end) > 0))) {
return (EINVAL);
}
#if 0
(void) snprintf(buf, buflen, "%llX", guid);
#endif
SRPT_FORMAT_HCAKEY(buf, buflen, guid);
if (int_guid) {
*int_guid = guid;
}
return (0);
}
|