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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Implements auxiliary routines declared in pcp_utils.h to facilitate
* finding the appropriate communication transport & device path for a
* given service. This supports the transition from the legacy service channel
* transport (glvc) to the logical domain channel (vldc) transport native
* to platforms running Logical Domains (LDoms).
*/
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <libdevinfo.h>
#include "pcp_utils.h"
typedef enum { false = 0, true = 1 } bool_t;
#define SERVICE_PREFIX "SUNW,sun4v-"
#define DEVICES_DIR "/devices"
#define GLVC ":glvc"
#define VCHAN "virtual-channel@"
#define VCHAN_C "virtual-channel-client@"
/*
* The mechanism to relate a service to a device path is different for
* vldc and glvc, due to the way the device pathnames are encoded:
* Sample service: sunvts
* Service Name: SUNW,sun4v-sunvts
* GLVC device path:
* "/devices/virtual-devices@100/sunvts@a:glvc"
* VLDC device path:
* "/devices/virtual-devices@100/channel-devices@200/virtual-channel@3:sunvts"
*
* As VLDC is the communication mechanism used in an LDoms environment, it is
* the preferred channel, and its existence is checked for first.
*/
/*
* Extract from dev_path the "service name" portion.
* For vldc, the service corresponds to the minor name of the device path
* (e.g. virtual-channel@3:sunvts for sunvts). If service is non-NULL, it must
* match the extracted service name for the function to succeed.
* The service name is returned in match (if non-NULL), and the function
* itself returns true on success; false on failure.
*/
static bool_t
get_vldc_svc_name(char *dev_path, char *service, char **match)
{
bool_t ret = false;
char *pathname = strdup(dev_path);
char *devname, *s;
if (NULL == pathname)
return (false);
devname = basename(pathname);
s = strrchr(devname, ':');
if (s++ == NULL) {
goto end;
}
if ((strncmp(devname, VCHAN, strlen(VCHAN)) == 0) ||
(strncmp(devname, VCHAN_C, strlen(VCHAN_C)) == 0)) {
/*
* If in addition, a service string is specified to
* be matched, do a comparison
*/
if (service != NULL) {
if (strcmp(s, service) == 0) {
if (match)
*match = strdup(s);
ret = true;
goto end;
} else {
ret = false;
goto end;
}
} else if (match) {
*match = strdup(s);
}
ret = true;
goto end;
}
end:
free(pathname);
return (ret);
}
/*
* Extract from dev_path the "service name" portion.
* For glvc, the service corresponds to the node name of the device path
* (e.g. sunvts@a:glvc for sunvts). If service is non-NULL, it must
* match the extracted service name for the function to succeed.
* The service name is returned in match (if non-NULL), and the function
* itself returns true on success; false on failure.
*/
static bool_t
get_glvc_svc_name(char *dev_path, char *service, char **match)
{
bool_t ret = true;
char *pathname = strdup(dev_path);
char *devname, *substr, *t;
int len;
if (NULL == pathname)
return (false);
devname = basename(pathname);
substr = strstr(devname, GLVC);
if (!((substr != NULL) && (strcmp(substr, GLVC) == 0))) {
ret = false;
goto end;
}
if ((t = strrchr(devname, '@')) == NULL) {
ret = false;
goto end;
}
len = t - devname;
/*
* If a service string is specified, check if there
* is a match
*/
if ((service != NULL) && (strncmp(devname, service, len) != 0))
ret = false;
if ((ret == true) && (match != NULL)) {
*match = calloc(len + 1, 1);
if (*match)
(void) strncpy(*match, devname, len);
}
end:
free(pathname);
return (ret);
}
/*
* This routine accepts either a prefixed service name or a legacy full
* pathname (which might not even exist in the filesystem), and in either case
* returns a canonical service name. If the parameter is neither a service
* name (i.e. with a "SUNW,sun4v-" prefix), nor a path to a legacy glvc or
* vldc device, NULL is returned.
*/
char *
platsvc_extract_svc_name(char *devname)
{
char *sname = NULL;
char *vldc_path, *glvc_path;
/*
* First check whether a service name
*/
if (strncmp(devname, SERVICE_PREFIX, strlen(SERVICE_PREFIX)) == 0) {
sname = strdup(devname + strlen(SERVICE_PREFIX));
return (sname);
}
/*
* Not a service name, check if it's a valid pathname
*/
if (!(devname[0] == '/' || devname[0] == '.')) {
return (NULL);
}
/*
* Ideally, we should only check for a valid glvc pathname,
* requiring all vldc access to be only via service names. But
* to prevent a flag day with code that's already passing in
* vldc full pathnames (e.g. sunMC), we allow them here.
*/
if (get_vldc_svc_name(devname, NULL, &vldc_path) == true) {
return (vldc_path);
} else if (get_glvc_svc_name(devname, NULL, &glvc_path) == true) {
return (glvc_path);
}
return (NULL);
}
/*
* Walk all "service" device nodes to find the one with the
* matching glvc minor name
*/
static char *
svc_name_to_glvc_dev_path(char *service)
{
di_node_t root_node, service_node;
char *glvc_path;
char *minor_name;
di_minor_t minor;
char *dev_path = NULL;
if (service == NULL)
return (NULL);
/* Ensure that the 'glvc' driver is loaded */
root_node = di_init_driver("glvc", DINFOCPYALL);
if (root_node == DI_NODE_NIL) {
return (dev_path);
}
service_node = di_drv_first_node("glvc", root_node);
while (service_node != DI_NODE_NIL) {
/* Make sure node name matches service name */
if (strcmp(service, di_node_name(service_node)) == 0) {
/* Walk minor nodes */
minor = di_minor_next(service_node, DI_NODE_NIL);
while (minor != DI_NODE_NIL) {
glvc_path = di_devfs_minor_path(minor);
minor_name = di_minor_name(minor);
if (strcmp(minor_name, "glvc") == 0) {
dev_path = malloc(strlen(glvc_path) +
strlen(DEVICES_DIR) + 1);
(void) strcpy(dev_path, DEVICES_DIR);
(void) strcat(dev_path, glvc_path);
di_devfs_path_free(glvc_path);
break;
}
di_devfs_path_free(glvc_path);
minor = di_minor_next(service_node, minor);
}
}
if (dev_path != NULL)
break;
service_node = di_drv_next_node(service_node);
}
di_fini(root_node);
return (dev_path);
}
/*
* Walk all vldc device nodes to find the one with the
* matching minor name
*/
static char *
svc_name_to_vldc_dev_path(char *service)
{
di_node_t root_node, vldc_node;
char *vldc_path;
char *minor_name;
di_minor_t minor;
char *dev_path = NULL;
/* Ensure that the 'vldc' driver is loaded */
root_node = di_init_driver("vldc", DINFOCPYALL);
if (root_node == DI_NODE_NIL) {
return (dev_path);
}
vldc_node = di_drv_first_node("vldc", root_node);
while (vldc_node != DI_NODE_NIL) {
/* Walk minor nodes */
minor = di_minor_next(vldc_node, DI_NODE_NIL);
while (minor != DI_NODE_NIL) {
vldc_path = di_devfs_minor_path(minor);
minor_name = di_minor_name(minor);
if (strcmp(minor_name, service) == 0) {
dev_path = malloc(strlen(vldc_path) +
strlen(DEVICES_DIR) + 1);
(void) strcpy(dev_path, DEVICES_DIR);
(void) strcat(dev_path, vldc_path);
di_devfs_path_free(vldc_path);
break;
}
di_devfs_path_free(vldc_path);
minor = di_minor_next(vldc_node, minor);
}
if (dev_path != NULL)
break;
vldc_node = di_drv_next_node(vldc_node);
}
di_fini(root_node);
return (dev_path);
}
/*
* Given a service name or a full legacy pathname, return
* the full pathname to the appropriate vldc or glvc device.
*/
char *
platsvc_name_to_path(char *svc_or_path, pcp_xport_t *type)
{
char *pathn_p;
char *service;
if ((service = platsvc_extract_svc_name(svc_or_path)) == NULL)
return (NULL);
/*
* First lookup vldc nodes
*/
pathn_p = svc_name_to_vldc_dev_path(service);
if (pathn_p != NULL) {
*type = VLDC_STREAMING;
} else {
/*
* If no vldc, try to find a glvc node
*/
pathn_p = svc_name_to_glvc_dev_path(service);
if (pathn_p != NULL) {
*type = GLVC_NON_STREAM;
}
}
free(service);
return (pathn_p);
}
|