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
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "HBAPort.h"
#include "Exceptions.h"
#include "Trace.h"
#include <iostream>
#include <iomanip>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <dirent.h>
#include <libdevinfo.h>
using namespace std;
/**
* Standard definition for general topology lookup (See T11 FC-FS)
*/
const int HBAPort::RNID_GENERAL_TOPOLOGY_DATA_FORMAT = 0xDF;
const uint8_t HBAPort::HBA_NPIV_PORT_MAX = UCHAR_MAX;
/**
* @memo Construct a new deafult HBA Port
*/
HBAPort::HBAPort() {
}
/**
* @memo Compare two HBA ports for equality
* @return TRUE if both ports are the same
* @return FALSE if the ports are different
*
* @doc Comparison is based on Node WWN, Port WWN and path
*/
bool HBAPort::operator==(HBAPort &comp) {
return (this->getPortWWN() == comp.getPortWWN() &&
this->getNodeWWN() == comp.getNodeWWN() &&
this->getPath() == comp.getPath());
}
/**
* @memo Validate that the port is still present in the system
* @exception UnavailableException if the port is not present
*
* @doc If the port is still present on the system, the routine
* will return normally. If the port is not present
* an exception will be thrown.
*/
void HBAPort::validatePresent() {
Trace log("HBAPort::validatePresent");
string path = getPath();
struct stat sbuf;
if (stat(path.c_str(), &sbuf) == -1) {
if (errno == ENOENT) {
throw UnavailableException();
} else {
log.debug("Unable to stat %s: %s", path.c_str(),
strerror(errno));
throw InternalError();
}
}
}
/*
* structure for di_devlink_walk
*/
typedef struct walk_devlink {
char *path;
size_t len;
char **linkpp;
} walk_devlink_t;
/**
* @memo callback funtion for di_devlink_walk
* @postcondition Find matching /dev link for the given path argument.
* @param devlink element and callback function argument.
*
* @doc The input path is expected to not have "/devices".
*/
extern "C" int
get_devlink(di_devlink_t devlink, void *arg) {
Trace log("get_devlink");
walk_devlink_t *warg = (walk_devlink_t *)arg;
/*
* When path is specified, it doesn't have minor
* name. Therefore, the ../.. prefixes needs to be stripped.
*/
if (warg->path) {
// di_devlink_content contains /devices
char *content = (char *)di_devlink_content(devlink);
char *start = strstr(content, "/devices");
if (start == NULL ||
strncmp(start, warg->path, warg->len) != 0 ||
// make it sure the device path has minor name
start[warg->len] != ':')
return (DI_WALK_CONTINUE);
}
*(warg->linkpp) = strdup(di_devlink_path(devlink));
return (DI_WALK_TERMINATE);
}
/**
* @memo Convert /devices paths to /dev sym-link paths.
* @postcondition The mapping buffer OSDeviceName paths will be
* converted to short names.
* @param mappings The target mappings data to convert to
* short names
*
* @doc If no link
* is found, the long path is left as is.
* Note: The NumberOfEntries field MUST not be greater than the size
* of the array passed in.
*/
void HBAPort::convertToShortNames(PHBA_FCPTARGETMAPPINGV2 mappings) {
Trace log("HBAPort::convertToShortNames");
di_devlink_handle_t hdl;
walk_devlink_t warg;
char *minor_path, *devlinkp;
if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
log.internalError("di_devlink_init failed. Errno:%d", errno);
// no need to check further, just return here.
return;
}
for (int j = 0; j < mappings->NumberOfEntries; j++) {
if (strchr(mappings->entry[j].ScsiId.OSDeviceName, ':')) {
// search link for minor node
minor_path = mappings->entry[j].ScsiId.OSDeviceName;
if (strstr(minor_path, "/devices") != NULL) {
minor_path = mappings->entry[j].ScsiId.OSDeviceName +
strlen("/devices");
} else {
minor_path = mappings->entry[j].ScsiId.OSDeviceName;
}
warg.path = NULL;
} else {
minor_path = NULL;
if (strstr(mappings->entry[j].ScsiId.OSDeviceName,
"/devices") != NULL) {
warg.len = strlen (mappings->entry[j].ScsiId.OSDeviceName) -
strlen ("/devices");
warg.path = mappings->entry[j].ScsiId.OSDeviceName +
strlen ("/devices");
} else {
warg.len = strlen(mappings->entry[j].ScsiId.OSDeviceName);
warg.path = mappings->entry[j].ScsiId.OSDeviceName;
}
}
devlinkp = NULL;
warg.linkpp = &devlinkp;
(void) di_devlink_walk(hdl, NULL, minor_path, DI_PRIMARY_LINK,
(void *)&warg, get_devlink);
if (devlinkp != NULL) {
snprintf(mappings->entry[j].ScsiId.OSDeviceName,
sizeof (mappings->entry[j].ScsiId.OSDeviceName),
"%s", devlinkp);
free(devlinkp);
} // else leave OSDeviceName alone.
}
di_devlink_fini(&hdl);
}
/*
* Finds controller path for a give device path.
*
* Return vale: controller path.
*/
string HBAPort::lookupControllerPath(string path) {
Trace log("lookupControllerPath");
DIR *dp;
char buf[MAXPATHLEN];
char node[MAXPATHLEN];
struct dirent **dirpp, *dirp;
const char dir[] = "/dev/cfg";
ssize_t count;
uchar_t *dir_buf = new uchar_t[sizeof (struct dirent) + MAXPATHLEN];
if ((dp = opendir(dir)) == NULL) {
string tmp = "Unable to open ";
tmp += dir;
tmp += "to find controller number.";
delete (dir_buf);
throw IOError(tmp);
}
dirp = (struct dirent *) dir_buf;
dirpp = &dirp;
while ((readdir_r(dp, dirp, dirpp)) == 0 && dirp != NULL) {
if (strcmp(dirp->d_name, ".") == 0 ||
strcmp(dirp->d_name, "..") == 0) {
continue;
}
sprintf(node, "%s/%s", dir, dirp->d_name);
if ((count = readlink(node,buf,sizeof(buf)))) {
buf[count] = '\0';
if (strstr(buf, path.c_str())) {
string cfg_path = dir;
cfg_path += "/";
cfg_path += dirp->d_name;
closedir(dp);
delete (dir_buf);
return (cfg_path);
}
}
}
closedir(dp);
delete (dir_buf);
throw InternalError("Unable to find controller path");
}
void HBAPort::addPort(HBANPIVPort *port) {
Trace log("HBAPort::addPort");
lock();
// support hba with up to UCHAR_MAX number of ports.
if (npivportsByIndex.size() + 1 > HBA_NPIV_PORT_MAX) {
unlock();
throw InternalError("HBA NPIV Port count exceeds max number of ports");
}
try {
npivportsByWWN[port->getPortWWN()] = port;
npivportsByIndex.insert(npivportsByIndex.end(), port);
unlock();
} catch (...) {
unlock();
throw;
}
}
HBANPIVPort* HBAPort::getPort(uint64_t wwn) {
Trace log("HBAPort::getPort");
HBANPIVPort *port = NULL;
lock();
try {
if (npivportsByWWN.find(wwn) == npivportsByWWN.end()) {
throw IllegalWWNException();
}
port = npivportsByWWN[wwn];
unlock();
return (port);
} catch (...) {
unlock();
throw;
}
}
HBANPIVPort* HBAPort::getPortByIndex(int index) {
Trace log("HBAPort::getPortByIndex");
lock();
try {
if (index >= npivportsByIndex.size() || index < 0) {
throw IllegalIndexException();
}
HBANPIVPort *tmp = npivportsByIndex[index];
unlock();
return (tmp);
} catch (...) {
unlock();
throw;
}
}
|