File: fcrls.c

package info (click to toggle)
fcoe-utils 1.0.32+git20190507.9834b34-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,092 kB
  • sloc: ansic: 11,945; sh: 1,298; makefile: 81
file content (509 lines) | stat: -rw-r--r-- 12,579 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright(c) 2009 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Maintained at www.Open-FCoE.org
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/types.h>
typedef __u8 u8;
typedef __u16 u16;
typedef __u32 u32;
typedef __u64 u64;
#include <linux/bsg.h>
#include <scsi/sg.h>
#include "fc_els.h"
#include "scsi_bsg_fc.h"

#define ntoh24(n) (u32) ((n)[0] << 16 | (n)[1] << 8  | (n)[2])
#define hton24(h) { (h) >> 16 & 0xff, (h) >> 8 & 0xff, (h) & 0xff }

#define SYSFS_FC_RPORTS "/sys/class/fc_remote_ports"

struct rport_info {
	int host_no;
	int channel;
	u32 number;
	u32 port_id;
	bool found;
	bool online;
};

struct fcoe_fc_els_lesb {
	__be32 lesb_link_fail;	/* link failure count */
	__be32 lesb_vlink_fail; /* virtual link failure count */
	__be32 lesb_miss_fka;	/* missing FIP keep-alive count */
	__be32 lesb_symb_err;	/* symbol error during carrier count */
	__be32 lesb_err_block;	/* errored block count */
	__be32 lesb_fcs_error; /* frame check sequence error count */
} __attribute__((__packed__));

union rls_acc {
	struct fc_els_lesb fs3;
	struct fcoe_fc_els_lesb bb5;
} __attribute__((__packed__));

struct rls_rjt {
	u8 er_resv;
	u8 er_reason;
	u8 er_explan;
	u8 er_vendor;
} __attribute__((__packed__));

struct rls_rsp {
	u8 rls_cmd;
	u8 rls_resv[3];
	union rls_acc acc;
	struct rls_rjt rjt;
} __attribute__((__packed__));

static char *rjt_reason[] = {
	[1] = "Invalid command code",
	[2] = "Invalid version level",
	[3] = "Logical error",
	[4] = "Invalid CT_IU size",
	[5] = "Logical busy",
	[7] = "Protocol error",
	[9] = "Unable to perform command request",
	[0xB] = "Command not supported",
	[0xD] = "Server not available",
	[0xE] = "Session could not be established",
	[0xFF] = "Vendor specific",
	[0x100] = "N/A",
};

static char *rjt_explan[] = {
	[0] = "No additional explanation",
	[1] = "Port Identifier not registered",
	[2] = "Port Name not registered",
	[3] = "Node Name not registered",
	[4] = "Class of Service not registered",
	[6] = "Initial Process Associator not registered",
	[7] = "FC-4 Types not registered",
	[8] = "Symbolic Port Name not registered",
	[9] = "Symbolic Node Name not registered",
	[0xA] = "Port Type not registered",
	[0xC] = "Fabric Port Name not registered",
	[0xD] = "Hard Address not registered",
	[0xF] = "FC-4 Features not registered",
	[0x10] = "Access denied",
	[0x11] = "Unacceptable Port Identifier",
	[0x12] = "Data base empty",
	[0x13] = "No object registered in the specified scope",
	[0x14] = "Domain ID not present",
	[0x15] = "Port number not present",
	[0x16] = "No device attached",
	[0x17] = "invalid OX_ID-RX_ID combination",
	[0x19] = "Request already in progress",
	[0x1e] = "N_Port login required",
	[0x29] = "insufficient resources",
	[0x2a] = "unable to supply requested data",
	[0x2c] = "Request not supported",
	[0x2d] = "Invalid payload length",
	[0x44] = "Invalid Port/Node_Name",
	[0x46] = "Login Extension not supported",
	[0x48] = "Authentication required",
	[0x50] = "Periodic Scan Value not allowed",
	[0x51] = "Periodic Scanning not supported",
	[0x60] = "MAC addressing mode not supported",
	[0x61] = "Proposed MAC address incorrectly formed",
	[0xf0] = "Authorization Exception",
	[0xf1] = "Authentication Exception",
	[0xf2] = "Data base full",
	[0xf3] = "Data base empty",
	[0xf4] = "Processing request",
	[0xf5] = "Unable to verify connection",
	[0xf6] = "Devices not in a common zone",
	[0x100] = "N/A",

};

enum commands {
	NONE = 0,
	RLS_PORT,
	RLS_FCID,
	RLS_QUIET,
	RLS_HELP,
};

/* RLS_QUIET */
static bool quiet;

/* : - has arg
 * :: - has optional arg
 * ;  - arg is long opt
 */
static const struct option lopt[] = {
	{ "port", required_argument, NULL, RLS_PORT },
	{ "fcid", required_argument, NULL, RLS_FCID },
	{ "quiet", no_argument, NULL, RLS_QUIET },
	{ "help", no_argument, NULL, RLS_HELP },
	{ NULL, 0, NULL, 0 },
};

static const char *lopt_usage[] = {
	"rport bsg name, e.g., rport-7:0-1.",
	"rport port FC_ID, e.g., 0xce000d.",
	"disable verbose output.",
	"print useage information.",
	NULL,
};

#define bsg_error(format...)				\
	({						\
		fprintf(stderr, "ERROR: " format);	\
	})

#define bsg_debug(format...)				\
	({						\
		if (!quiet)				\
			printf("DEBUG: " format);	\
	})

static char *els_rjt2str(int type, int code)
{
	char **str;

	str = (type == 0) ? rjt_reason : rjt_explan;

	if (code > 0xff)
		code = 0x100;

	if (!str[code])
		code = 0x100;

	return str[code];
}

static int els_print_lesb(struct fcoe_fc_els_lesb *lesb)
{
	printf("RLS request accepted (LS_ACC), dumping status counters:\n"
	       "\tLink Failure Count                   = %u\n"
	       "\tVirtual Link Failure Count           = %u\n"
	       "\tMissed Discovery Advertisement Count = %u\n"
	       "\tSymbol Error During Carrier Count    = %u\n"
	       "\tErrored Block Count                  = %u\n"
	       "\tFrame Check Sequence Error Count     = %u\n",
	       ntohl(lesb->lesb_link_fail),
	       ntohl(lesb->lesb_vlink_fail),
	       ntohl(lesb->lesb_miss_fka),
	       ntohl(lesb->lesb_symb_err),
	       ntohl(lesb->lesb_err_block),
	       ntohl(lesb->lesb_fcs_error));

	return 0;
}

static int els_print_rjt(struct rls_rjt *rjt)
{
	printf("RLS request rejected (LS_RJT), check reason code below:\n"
	       "\tReason Code  = 0x%02x, %s.\n"
	       "\tExplain Code = 0x%02x, %s.\n",
	       rjt->er_reason, els_rjt2str(0, rjt->er_reason),
	       rjt->er_explan, els_rjt2str(1, rjt->er_explan));
	if (rjt->er_reason == ELS_RJT_VENDOR)
		printf("\tVendor Code  = 0x%02x (check with your vendor).\n",
		       rjt->er_vendor);
	return 0;
}

static int bsg_rport_els(int bsg, u8 els_code, void *req, int req_len,
			 void *rsp, int rsp_len)
{
	int rc;
	char sense[96];
	struct fc_bsg_reply *reply = (struct fc_bsg_reply *)sense;
	struct fc_bsg_request cdb = {
		.msgcode 	= FC_BSG_RPT_ELS,
		.rqst_data.r_els = {
			.els_code = els_code,
		}
	};

	struct sg_io_v4 sgio = {
		.guard			= 'Q',
		.protocol		= BSG_PROTOCOL_SCSI,
		.subprotocol		= BSG_SUB_PROTOCOL_SCSI_TRANSPORT,
		.request_len		= sizeof(cdb),
		.request		= (uintptr_t)&cdb,
		.dout_xfer_len		= req_len,
		.dout_xferp		= (uintptr_t)req,
		.din_xfer_len		= rsp_len,
		.din_xferp		= (uintptr_t)rsp,
		.max_response_len	= sizeof(sense),
		.response		= (uintptr_t)&sense,
		.timeout		= 1000,
	};
	memset(sense, 0, sizeof(sense));
	rc = ioctl(bsg, SG_IO, &sgio);
	bsg_debug("ioctl returned %d: bsg_reply result=%d\n",
		  rc, reply->result);
	return rc;
}

static int bsg_rport_els_rls(int bsg, struct rport_info *rpi)
{
	int rc = EOPNOTSUPP;
	struct fc_els_rls rls = {
		.rls_cmd = ELS_RLS,
		.rls_port_id = hton24(rpi->port_id),
	};
	struct rls_rsp rsp;

	memset(&rsp, 0, sizeof(rsp));
	rc = bsg_rport_els(bsg, ELS_RLS, &rls, sizeof(rls), &rsp, sizeof(rsp));
	if (rc) {
		bsg_error("bsg_rport_els(ELS_RLS) failed\n");
		return rc;
	}
	if (rsp.rls_cmd == ELS_LS_ACC)
		return	els_print_lesb(&rsp.acc.bb5);

	if (rsp.rls_cmd == ELS_LS_RJT)
		return els_print_rjt(&rsp.rjt);

	bsg_error("Unknown response!\n");
	return EIO;
}

static int rport_getid(struct rport_info *rpi)
{
	FILE *f;
	char rp_sysfs[256];

	if (rpi->found)
		return 0;
	snprintf(rp_sysfs, sizeof(rp_sysfs), "%s/rport-%d:%d-%d/port_id",
		 SYSFS_FC_RPORTS, rpi->host_no, rpi->channel, rpi->number);
	f = fopen(rp_sysfs, "ro");
	if (!f) {
		bsg_error("failed to fopen(%s)!\n", rp_sysfs);
		return ENODEV;
	}
	if (1 != fscanf(f, "0x%6x", &rpi->port_id)) {
		bsg_error("failed to fscanf(%s)\n", rp_sysfs);
		fclose(f);
		return ENODEV;
	}
	if (rpi->port_id & 0xff000000) {
		bsg_error("rport %s:invalid fcid 0x%x\n", rp_sysfs,
			  rpi->port_id);
		rpi->port_id = 0;
		fclose(f);
		return ENODEV;
	}
	fclose(f);
	return 0;
}

/*
 * parse a string in format of rport-%d:%d-%d, and get the
 * corresponding rport info.
 * rport-%d:%d-%d
 */
static int rport_parse(const char *s, struct rport_info *rpi)
{
	if (!s)
		return EINVAL;
	memset(rpi, 0, sizeof(*rpi));
	if (3 != sscanf(s, "rport-%d:%d-%d", &rpi->host_no, &rpi->channel,
			&rpi->number))
		return ENODEV;
	if (rport_getid(rpi))
		return ENODEV;
	return 0;
}

#define RPORT_ONLINE	"Online"
static int rport_check_state(struct rport_info *rpi)
{
	FILE *f;
	char rp_sysfs[256];
	char rp_state[256];

	rpi->online = false;
	if (!rpi->found)
		return EINVAL;

	snprintf(rp_sysfs, sizeof(rp_sysfs), "%s/rport-%d:%d-%d/port_state",
		 SYSFS_FC_RPORTS, rpi->host_no, rpi->channel, rpi->number);

	f = fopen(rp_sysfs, "ro");
	if (!f) {
		bsg_error("failed to fopen(%s)!\n", rp_sysfs);
		return ENODEV;
	}
	if (!fgets(rp_state, sizeof(rp_state), f)) {
		bsg_error("failed to fgets(%s)!\n", rp_sysfs);
		fclose(f);
		return ENODEV;
	}
	if (strncmp(rp_state, RPORT_ONLINE, strlen(RPORT_ONLINE))) {
		bsg_error("rport 0x%x %s:must be %s\n", rpi->port_id,
			  rp_state, RPORT_ONLINE);
		fclose(f);
		return ENODEV;
	}
	rpi->online = true;
	fclose(f);
	return 0;
}
/* locate rport by fcid */
static int rport_find(struct rport_info *rpi)
{
	int n;
	struct dirent **namelist;
	struct rport_info rpii;

	if (rpi->found)
		return 0;

	if (!rpi->port_id)
		return ENODEV;

	n = scandir(SYSFS_FC_RPORTS, &namelist, 0, alphasort);
	if (n < 0) {
		bsg_error("failed to scandir %s\n", SYSFS_FC_RPORTS);
		return ENODEV;
	}
	while (n--) {
		if ((namelist[n]->d_type != DT_DIR) &&
		    (namelist[n]->d_type != DT_LNK))
			goto free_name;
		if (rport_parse(namelist[n]->d_name, &rpii))
			goto free_name;
		if (rpi->port_id != rpii.port_id)
			goto free_name;
		rpii.found = true;
		memcpy(rpi, &rpii, sizeof(rpii));
		bsg_debug("found rport 0x%06x as rport-%d:%d-%d\n",
			  rpi->port_id, rpi->host_no, rpi->channel,
			  rpi->number);
free_name:
		free(namelist[n]);
	}
	free(namelist);
	return 0;
}

static void bsg_usage(int status)
{
	int i, n;

	if (status)
		bsg_error("Failed! %s (Errno %d)!\n", strerror(status), status);

	n = sizeof(lopt)/sizeof(struct option) - 1;
	printf("Usage: fcrls\n");
	for (i = 0; i < n; i++)
		printf("\t--%s: %s\n", lopt[i].name, lopt_usage[i]);
	exit(status);
}


int main(int argc, char *argv[])
{
	int rc = ENODEV;
	int opt;
	int bsg_dev;
	char *endptr;
	char *bsg_name = NULL;
	struct rport_info rpi;

	rpi.found = false;
	while ((opt = getopt_long(argc, argv, "", lopt, NULL)) != -1) {
		switch (opt) {
		case RLS_PORT:
			if (rport_parse(optarg, &rpi)) {
				bsg_error("%s format incorrect, must be:"
					  "rport-host:channel-number\n",
					  optarg);
				bsg_usage(EINVAL);
			}
			rpi.found = true;
			goto out_rls;
		case RLS_FCID:
			rpi.found = false;
			rpi.port_id = strtoull(optarg, &endptr, 16);
			if (*endptr != '\0') {
				bsg_error("%s has no valid FCID\n", optarg);
				bsg_usage(EINVAL);
			}
			if (rport_find(&rpi)) {
				bsg_error("%s is not a rport\n", optarg);
				bsg_usage(ENODEV);
			}
			goto out_rls;
		case RLS_QUIET:
			quiet = true;
			break;
		case RLS_HELP:
			bsg_usage(0);
			break;
		}
	}
out_rls:
	/* bsg device name */
	if (!rpi.found)
		bsg_usage(ENODEV);

	if (asprintf(&bsg_name, "/dev/bsg/rport-%d:%d-%d",
		     rpi.host_no, rpi.channel, rpi.number) < 0) {
		rc = ENOMEM;
		bsg_error("not enough memory!\n");
		goto out_error;
	}
	/* open bsg device */
	bsg_dev = open(bsg_name, O_RDWR);
	if (bsg_dev < 0) {
		bsg_error("failed to open %s!\n", bsg_name);
		goto out_free;
	}
	/* check port state */
	if (rport_check_state(&rpi) || (!rpi.online)) {
		bsg_error("rport 0x%x is not online!\n", rpi.port_id);
		goto out_close;
	}
	/* send rls */
	rc = bsg_rport_els_rls(bsg_dev, &rpi);
	if (rc) {
		bsg_error("Failed to bsg_rport_els_rls\n");
		goto out_close;
	}
	rc = 0;

out_close:
	close(bsg_dev);
out_free:
	free(bsg_name);
out_error:
	return rc;
}