1 /*******************************************************************************
2  * Modern ConfigFS group context specific iSCSI statistics based on original
3  * iscsi_target_mib.c code
4  *
5  * Copyright (c) 2011 Rising Tide Systems
6  *
7  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8  *
9  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  ******************************************************************************/
21 
22 #include <linux/configfs.h>
23 #include <linux/export.h>
24 #include <scsi/iscsi_proto.h>
25 #include <target/target_core_base.h>
26 #include <target/configfs_macros.h>
27 
28 #include "iscsi_target_core.h"
29 #include "iscsi_target_parameters.h"
30 #include "iscsi_target_device.h"
31 #include "iscsi_target_tpg.h"
32 #include "iscsi_target_util.h"
33 #include "iscsi_target_stat.h"
34 
35 #ifndef INITIAL_JIFFIES
36 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
37 #endif
38 
39 /* Instance Attributes Table */
40 #define ISCSI_INST_NUM_NODES		1
41 #define ISCSI_INST_DESCR		"Storage Engine Target"
42 #define ISCSI_INST_LAST_FAILURE_TYPE	0
43 #define ISCSI_DISCONTINUITY_TIME	0
44 
45 #define ISCSI_NODE_INDEX		1
46 
47 #define ISPRINT(a)   ((a >= ' ') && (a <= '~'))
48 
49 /****************************************************************************
50  * iSCSI MIB Tables
51  ****************************************************************************/
52 /*
53  * Instance Attributes Table
54  */
55 CONFIGFS_EATTR_STRUCT(iscsi_stat_instance, iscsi_wwn_stat_grps);
56 #define ISCSI_STAT_INSTANCE_ATTR(_name, _mode)			\
57 static struct iscsi_stat_instance_attribute			\
58 			iscsi_stat_instance_##_name =		\
59 	__CONFIGFS_EATTR(_name, _mode,				\
60 	iscsi_stat_instance_show_attr_##_name,			\
61 	iscsi_stat_instance_store_attr_##_name);
62 
63 #define ISCSI_STAT_INSTANCE_ATTR_RO(_name)			\
64 static struct iscsi_stat_instance_attribute			\
65 			iscsi_stat_instance_##_name =		\
66 	__CONFIGFS_EATTR_RO(_name,				\
67 	iscsi_stat_instance_show_attr_##_name);
68 
69 static ssize_t iscsi_stat_instance_show_attr_inst(
70 	struct iscsi_wwn_stat_grps *igrps, char *page)
71 {
72 	struct iscsi_tiqn *tiqn = container_of(igrps,
73 				struct iscsi_tiqn, tiqn_stat_grps);
74 
75 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_index);
76 }
77 ISCSI_STAT_INSTANCE_ATTR_RO(inst);
78 
79 static ssize_t iscsi_stat_instance_show_attr_min_ver(
80 	struct iscsi_wwn_stat_grps *igrps, char *page)
81 {
82 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_DRAFT20_VERSION);
83 }
84 ISCSI_STAT_INSTANCE_ATTR_RO(min_ver);
85 
86 static ssize_t iscsi_stat_instance_show_attr_max_ver(
87 	struct iscsi_wwn_stat_grps *igrps, char *page)
88 {
89 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_DRAFT20_VERSION);
90 }
91 ISCSI_STAT_INSTANCE_ATTR_RO(max_ver);
92 
93 static ssize_t iscsi_stat_instance_show_attr_portals(
94 	struct iscsi_wwn_stat_grps *igrps, char *page)
95 {
96 	struct iscsi_tiqn *tiqn = container_of(igrps,
97 				struct iscsi_tiqn, tiqn_stat_grps);
98 
99 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_num_tpg_nps);
100 }
101 ISCSI_STAT_INSTANCE_ATTR_RO(portals);
102 
103 static ssize_t iscsi_stat_instance_show_attr_nodes(
104 	struct iscsi_wwn_stat_grps *igrps, char *page)
105 {
106 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_INST_NUM_NODES);
107 }
108 ISCSI_STAT_INSTANCE_ATTR_RO(nodes);
109 
110 static ssize_t iscsi_stat_instance_show_attr_sessions(
111 	struct iscsi_wwn_stat_grps *igrps, char *page)
112 {
113 	struct iscsi_tiqn *tiqn = container_of(igrps,
114 				struct iscsi_tiqn, tiqn_stat_grps);
115 
116 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_nsessions);
117 }
118 ISCSI_STAT_INSTANCE_ATTR_RO(sessions);
119 
120 static ssize_t iscsi_stat_instance_show_attr_fail_sess(
121 	struct iscsi_wwn_stat_grps *igrps, char *page)
122 {
123 	struct iscsi_tiqn *tiqn = container_of(igrps,
124 				struct iscsi_tiqn, tiqn_stat_grps);
125 	struct iscsi_sess_err_stats *sess_err = &tiqn->sess_err_stats;
126 	u32 sess_err_count;
127 
128 	spin_lock_bh(&sess_err->lock);
129 	sess_err_count = (sess_err->digest_errors +
130 			  sess_err->cxn_timeout_errors +
131 			  sess_err->pdu_format_errors);
132 	spin_unlock_bh(&sess_err->lock);
133 
134 	return snprintf(page, PAGE_SIZE, "%u\n", sess_err_count);
135 }
136 ISCSI_STAT_INSTANCE_ATTR_RO(fail_sess);
137 
138 static ssize_t iscsi_stat_instance_show_attr_fail_type(
139 	struct iscsi_wwn_stat_grps *igrps, char *page)
140 {
141 	struct iscsi_tiqn *tiqn = container_of(igrps,
142 				struct iscsi_tiqn, tiqn_stat_grps);
143 	struct iscsi_sess_err_stats *sess_err = &tiqn->sess_err_stats;
144 
145 	return snprintf(page, PAGE_SIZE, "%u\n",
146 			sess_err->last_sess_failure_type);
147 }
148 ISCSI_STAT_INSTANCE_ATTR_RO(fail_type);
149 
150 static ssize_t iscsi_stat_instance_show_attr_fail_rem_name(
151 	struct iscsi_wwn_stat_grps *igrps, char *page)
152 {
153 	struct iscsi_tiqn *tiqn = container_of(igrps,
154 				struct iscsi_tiqn, tiqn_stat_grps);
155 	struct iscsi_sess_err_stats *sess_err = &tiqn->sess_err_stats;
156 
157 	return snprintf(page, PAGE_SIZE, "%s\n",
158 			sess_err->last_sess_fail_rem_name[0] ?
159 			sess_err->last_sess_fail_rem_name : NONE);
160 }
161 ISCSI_STAT_INSTANCE_ATTR_RO(fail_rem_name);
162 
163 static ssize_t iscsi_stat_instance_show_attr_disc_time(
164 	struct iscsi_wwn_stat_grps *igrps, char *page)
165 {
166 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_DISCONTINUITY_TIME);
167 }
168 ISCSI_STAT_INSTANCE_ATTR_RO(disc_time);
169 
170 static ssize_t iscsi_stat_instance_show_attr_description(
171 	struct iscsi_wwn_stat_grps *igrps, char *page)
172 {
173 	return snprintf(page, PAGE_SIZE, "%s\n", ISCSI_INST_DESCR);
174 }
175 ISCSI_STAT_INSTANCE_ATTR_RO(description);
176 
177 static ssize_t iscsi_stat_instance_show_attr_vendor(
178 	struct iscsi_wwn_stat_grps *igrps, char *page)
179 {
180 	return snprintf(page, PAGE_SIZE, "RisingTide Systems iSCSI-Target\n");
181 }
182 ISCSI_STAT_INSTANCE_ATTR_RO(vendor);
183 
184 static ssize_t iscsi_stat_instance_show_attr_version(
185 	struct iscsi_wwn_stat_grps *igrps, char *page)
186 {
187 	return snprintf(page, PAGE_SIZE, "%s\n", ISCSIT_VERSION);
188 }
189 ISCSI_STAT_INSTANCE_ATTR_RO(version);
190 
191 CONFIGFS_EATTR_OPS(iscsi_stat_instance, iscsi_wwn_stat_grps,
192 		iscsi_instance_group);
193 
194 static struct configfs_attribute *iscsi_stat_instance_attrs[] = {
195 	&iscsi_stat_instance_inst.attr,
196 	&iscsi_stat_instance_min_ver.attr,
197 	&iscsi_stat_instance_max_ver.attr,
198 	&iscsi_stat_instance_portals.attr,
199 	&iscsi_stat_instance_nodes.attr,
200 	&iscsi_stat_instance_sessions.attr,
201 	&iscsi_stat_instance_fail_sess.attr,
202 	&iscsi_stat_instance_fail_type.attr,
203 	&iscsi_stat_instance_fail_rem_name.attr,
204 	&iscsi_stat_instance_disc_time.attr,
205 	&iscsi_stat_instance_description.attr,
206 	&iscsi_stat_instance_vendor.attr,
207 	&iscsi_stat_instance_version.attr,
208 	NULL,
209 };
210 
211 static struct configfs_item_operations iscsi_stat_instance_item_ops = {
212 	.show_attribute		= iscsi_stat_instance_attr_show,
213 	.store_attribute	= iscsi_stat_instance_attr_store,
214 };
215 
216 struct config_item_type iscsi_stat_instance_cit = {
217 	.ct_item_ops		= &iscsi_stat_instance_item_ops,
218 	.ct_attrs		= iscsi_stat_instance_attrs,
219 	.ct_owner		= THIS_MODULE,
220 };
221 
222 /*
223  * Instance Session Failure Stats Table
224  */
225 CONFIGFS_EATTR_STRUCT(iscsi_stat_sess_err, iscsi_wwn_stat_grps);
226 #define ISCSI_STAT_SESS_ERR_ATTR(_name, _mode)			\
227 static struct iscsi_stat_sess_err_attribute			\
228 			iscsi_stat_sess_err_##_name =		\
229 	__CONFIGFS_EATTR(_name, _mode,				\
230 	iscsi_stat_sess_err_show_attr_##_name,			\
231 	iscsi_stat_sess_err_store_attr_##_name);
232 
233 #define ISCSI_STAT_SESS_ERR_ATTR_RO(_name)			\
234 static struct iscsi_stat_sess_err_attribute			\
235 			iscsi_stat_sess_err_##_name =		\
236 	__CONFIGFS_EATTR_RO(_name,				\
237 	iscsi_stat_sess_err_show_attr_##_name);
238 
239 static ssize_t iscsi_stat_sess_err_show_attr_inst(
240 	struct iscsi_wwn_stat_grps *igrps, char *page)
241 {
242 	struct iscsi_tiqn *tiqn = container_of(igrps,
243 				struct iscsi_tiqn, tiqn_stat_grps);
244 
245 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_index);
246 }
247 ISCSI_STAT_SESS_ERR_ATTR_RO(inst);
248 
249 static ssize_t iscsi_stat_sess_err_show_attr_digest_errors(
250 	struct iscsi_wwn_stat_grps *igrps, char *page)
251 {
252 	struct iscsi_tiqn *tiqn = container_of(igrps,
253 				struct iscsi_tiqn, tiqn_stat_grps);
254 	struct iscsi_sess_err_stats *sess_err = &tiqn->sess_err_stats;
255 
256 	return snprintf(page, PAGE_SIZE, "%u\n", sess_err->digest_errors);
257 }
258 ISCSI_STAT_SESS_ERR_ATTR_RO(digest_errors);
259 
260 static ssize_t iscsi_stat_sess_err_show_attr_cxn_errors(
261 	struct iscsi_wwn_stat_grps *igrps, char *page)
262 {
263 	struct iscsi_tiqn *tiqn = container_of(igrps,
264 				struct iscsi_tiqn, tiqn_stat_grps);
265 	struct iscsi_sess_err_stats *sess_err = &tiqn->sess_err_stats;
266 
267 	return snprintf(page, PAGE_SIZE, "%u\n", sess_err->cxn_timeout_errors);
268 }
269 ISCSI_STAT_SESS_ERR_ATTR_RO(cxn_errors);
270 
271 static ssize_t iscsi_stat_sess_err_show_attr_format_errors(
272 	struct iscsi_wwn_stat_grps *igrps, char *page)
273 {
274 	struct iscsi_tiqn *tiqn = container_of(igrps,
275 				struct iscsi_tiqn, tiqn_stat_grps);
276 	struct iscsi_sess_err_stats *sess_err = &tiqn->sess_err_stats;
277 
278 	return snprintf(page, PAGE_SIZE, "%u\n", sess_err->pdu_format_errors);
279 }
280 ISCSI_STAT_SESS_ERR_ATTR_RO(format_errors);
281 
282 CONFIGFS_EATTR_OPS(iscsi_stat_sess_err, iscsi_wwn_stat_grps,
283 		iscsi_sess_err_group);
284 
285 static struct configfs_attribute *iscsi_stat_sess_err_attrs[] = {
286 	&iscsi_stat_sess_err_inst.attr,
287 	&iscsi_stat_sess_err_digest_errors.attr,
288 	&iscsi_stat_sess_err_cxn_errors.attr,
289 	&iscsi_stat_sess_err_format_errors.attr,
290 	NULL,
291 };
292 
293 static struct configfs_item_operations iscsi_stat_sess_err_item_ops = {
294 	.show_attribute		= iscsi_stat_sess_err_attr_show,
295 	.store_attribute	= iscsi_stat_sess_err_attr_store,
296 };
297 
298 struct config_item_type iscsi_stat_sess_err_cit = {
299 	.ct_item_ops		= &iscsi_stat_sess_err_item_ops,
300 	.ct_attrs		= iscsi_stat_sess_err_attrs,
301 	.ct_owner		= THIS_MODULE,
302 };
303 
304 /*
305  * Target Attributes Table
306  */
307 CONFIGFS_EATTR_STRUCT(iscsi_stat_tgt_attr, iscsi_wwn_stat_grps);
308 #define ISCSI_STAT_TGT_ATTR(_name, _mode)			\
309 static struct iscsi_stat_tgt_attr_attribute			\
310 			iscsi_stat_tgt_attr_##_name =		\
311 	__CONFIGFS_EATTR(_name, _mode,				\
312 	iscsi_stat_tgt-attr_show_attr_##_name,			\
313 	iscsi_stat_tgt_attr_store_attr_##_name);
314 
315 #define ISCSI_STAT_TGT_ATTR_RO(_name)				\
316 static struct iscsi_stat_tgt_attr_attribute			\
317 			iscsi_stat_tgt_attr_##_name =		\
318 	__CONFIGFS_EATTR_RO(_name,				\
319 	iscsi_stat_tgt_attr_show_attr_##_name);
320 
321 static ssize_t iscsi_stat_tgt_attr_show_attr_inst(
322 	struct iscsi_wwn_stat_grps *igrps, char *page)
323 {
324 	struct iscsi_tiqn *tiqn = container_of(igrps,
325 				struct iscsi_tiqn, tiqn_stat_grps);
326 
327 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_index);
328 }
329 ISCSI_STAT_TGT_ATTR_RO(inst);
330 
331 static ssize_t iscsi_stat_tgt_attr_show_attr_indx(
332 	struct iscsi_wwn_stat_grps *igrps, char *page)
333 {
334 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_NODE_INDEX);
335 }
336 ISCSI_STAT_TGT_ATTR_RO(indx);
337 
338 static ssize_t iscsi_stat_tgt_attr_show_attr_login_fails(
339 	struct iscsi_wwn_stat_grps *igrps, char *page)
340 {
341 	struct iscsi_tiqn *tiqn = container_of(igrps,
342 				struct iscsi_tiqn, tiqn_stat_grps);
343 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
344 	u32 fail_count;
345 
346 	spin_lock(&lstat->lock);
347 	fail_count = (lstat->redirects + lstat->authorize_fails +
348 			lstat->authenticate_fails + lstat->negotiate_fails +
349 			lstat->other_fails);
350 	spin_unlock(&lstat->lock);
351 
352 	return snprintf(page, PAGE_SIZE, "%u\n", fail_count);
353 }
354 ISCSI_STAT_TGT_ATTR_RO(login_fails);
355 
356 static ssize_t iscsi_stat_tgt_attr_show_attr_last_fail_time(
357 	struct iscsi_wwn_stat_grps *igrps, char *page)
358 {
359 	struct iscsi_tiqn *tiqn = container_of(igrps,
360 				struct iscsi_tiqn, tiqn_stat_grps);
361 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
362 	u32 last_fail_time;
363 
364 	spin_lock(&lstat->lock);
365 	last_fail_time = lstat->last_fail_time ?
366 			(u32)(((u32)lstat->last_fail_time -
367 				INITIAL_JIFFIES) * 100 / HZ) : 0;
368 	spin_unlock(&lstat->lock);
369 
370 	return snprintf(page, PAGE_SIZE, "%u\n", last_fail_time);
371 }
372 ISCSI_STAT_TGT_ATTR_RO(last_fail_time);
373 
374 static ssize_t iscsi_stat_tgt_attr_show_attr_last_fail_type(
375 	struct iscsi_wwn_stat_grps *igrps, char *page)
376 {
377 	struct iscsi_tiqn *tiqn = container_of(igrps,
378 				struct iscsi_tiqn, tiqn_stat_grps);
379 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
380 	u32 last_fail_type;
381 
382 	spin_lock(&lstat->lock);
383 	last_fail_type = lstat->last_fail_type;
384 	spin_unlock(&lstat->lock);
385 
386 	return snprintf(page, PAGE_SIZE, "%u\n", last_fail_type);
387 }
388 ISCSI_STAT_TGT_ATTR_RO(last_fail_type);
389 
390 static ssize_t iscsi_stat_tgt_attr_show_attr_fail_intr_name(
391 	struct iscsi_wwn_stat_grps *igrps, char *page)
392 {
393 	struct iscsi_tiqn *tiqn = container_of(igrps,
394 				struct iscsi_tiqn, tiqn_stat_grps);
395 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
396 	unsigned char buf[224];
397 
398 	spin_lock(&lstat->lock);
399 	snprintf(buf, 224, "%s", lstat->last_intr_fail_name[0] ?
400 				lstat->last_intr_fail_name : NONE);
401 	spin_unlock(&lstat->lock);
402 
403 	return snprintf(page, PAGE_SIZE, "%s\n", buf);
404 }
405 ISCSI_STAT_TGT_ATTR_RO(fail_intr_name);
406 
407 static ssize_t iscsi_stat_tgt_attr_show_attr_fail_intr_addr_type(
408 	struct iscsi_wwn_stat_grps *igrps, char *page)
409 {
410 	struct iscsi_tiqn *tiqn = container_of(igrps,
411 			struct iscsi_tiqn, tiqn_stat_grps);
412 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
413 	int ret;
414 
415 	spin_lock(&lstat->lock);
416 	if (lstat->last_intr_fail_ip_family == AF_INET6)
417 		ret = snprintf(page, PAGE_SIZE, "ipv6\n");
418 	else
419 		ret = snprintf(page, PAGE_SIZE, "ipv4\n");
420 	spin_unlock(&lstat->lock);
421 
422 	return ret;
423 }
424 ISCSI_STAT_TGT_ATTR_RO(fail_intr_addr_type);
425 
426 static ssize_t iscsi_stat_tgt_attr_show_attr_fail_intr_addr(
427 	struct iscsi_wwn_stat_grps *igrps, char *page)
428 {
429 	struct iscsi_tiqn *tiqn = container_of(igrps,
430 			struct iscsi_tiqn, tiqn_stat_grps);
431 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
432 	int ret;
433 
434 	spin_lock(&lstat->lock);
435 	ret = snprintf(page, PAGE_SIZE, "%s\n", lstat->last_intr_fail_ip_addr);
436 	spin_unlock(&lstat->lock);
437 
438 	return ret;
439 }
440 ISCSI_STAT_TGT_ATTR_RO(fail_intr_addr);
441 
442 CONFIGFS_EATTR_OPS(iscsi_stat_tgt_attr, iscsi_wwn_stat_grps,
443 		iscsi_tgt_attr_group);
444 
445 static struct configfs_attribute *iscsi_stat_tgt_attr_attrs[] = {
446 	&iscsi_stat_tgt_attr_inst.attr,
447 	&iscsi_stat_tgt_attr_indx.attr,
448 	&iscsi_stat_tgt_attr_login_fails.attr,
449 	&iscsi_stat_tgt_attr_last_fail_time.attr,
450 	&iscsi_stat_tgt_attr_last_fail_type.attr,
451 	&iscsi_stat_tgt_attr_fail_intr_name.attr,
452 	&iscsi_stat_tgt_attr_fail_intr_addr_type.attr,
453 	&iscsi_stat_tgt_attr_fail_intr_addr.attr,
454 	NULL,
455 };
456 
457 static struct configfs_item_operations iscsi_stat_tgt_attr_item_ops = {
458 	.show_attribute		= iscsi_stat_tgt_attr_attr_show,
459 	.store_attribute	= iscsi_stat_tgt_attr_attr_store,
460 };
461 
462 struct config_item_type iscsi_stat_tgt_attr_cit = {
463 	.ct_item_ops		= &iscsi_stat_tgt_attr_item_ops,
464 	.ct_attrs		= iscsi_stat_tgt_attr_attrs,
465 	.ct_owner		= THIS_MODULE,
466 };
467 
468 /*
469  * Target Login Stats Table
470  */
471 CONFIGFS_EATTR_STRUCT(iscsi_stat_login, iscsi_wwn_stat_grps);
472 #define ISCSI_STAT_LOGIN(_name, _mode)				\
473 static struct iscsi_stat_login_attribute			\
474 			iscsi_stat_login_##_name =		\
475 	__CONFIGFS_EATTR(_name, _mode,				\
476 	iscsi_stat_login_show_attr_##_name,			\
477 	iscsi_stat_login_store_attr_##_name);
478 
479 #define ISCSI_STAT_LOGIN_RO(_name)				\
480 static struct iscsi_stat_login_attribute			\
481 			iscsi_stat_login_##_name =		\
482 	__CONFIGFS_EATTR_RO(_name,				\
483 	iscsi_stat_login_show_attr_##_name);
484 
485 static ssize_t iscsi_stat_login_show_attr_inst(
486 	struct iscsi_wwn_stat_grps *igrps, char *page)
487 {
488 	struct iscsi_tiqn *tiqn = container_of(igrps,
489 				struct iscsi_tiqn, tiqn_stat_grps);
490 
491 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_index);
492 }
493 ISCSI_STAT_LOGIN_RO(inst);
494 
495 static ssize_t iscsi_stat_login_show_attr_indx(
496 	struct iscsi_wwn_stat_grps *igrps, char *page)
497 {
498 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_NODE_INDEX);
499 }
500 ISCSI_STAT_LOGIN_RO(indx);
501 
502 static ssize_t iscsi_stat_login_show_attr_accepts(
503 	struct iscsi_wwn_stat_grps *igrps, char *page)
504 {
505 	struct iscsi_tiqn *tiqn = container_of(igrps,
506 				struct iscsi_tiqn, tiqn_stat_grps);
507 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
508 	ssize_t ret;
509 
510 	spin_lock(&lstat->lock);
511 	ret = snprintf(page, PAGE_SIZE, "%u\n", lstat->accepts);
512 	spin_unlock(&lstat->lock);
513 
514 	return ret;
515 }
516 ISCSI_STAT_LOGIN_RO(accepts);
517 
518 static ssize_t iscsi_stat_login_show_attr_other_fails(
519 	struct iscsi_wwn_stat_grps *igrps, char *page)
520 {
521 	struct iscsi_tiqn *tiqn = container_of(igrps,
522 				struct iscsi_tiqn, tiqn_stat_grps);
523 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
524 	ssize_t ret;
525 
526 	spin_lock(&lstat->lock);
527 	ret = snprintf(page, PAGE_SIZE, "%u\n", lstat->other_fails);
528 	spin_unlock(&lstat->lock);
529 
530 	return ret;
531 }
532 ISCSI_STAT_LOGIN_RO(other_fails);
533 
534 static ssize_t iscsi_stat_login_show_attr_redirects(
535 	struct iscsi_wwn_stat_grps *igrps, char *page)
536 {
537 	struct iscsi_tiqn *tiqn = container_of(igrps,
538 				struct iscsi_tiqn, tiqn_stat_grps);
539 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
540 	ssize_t ret;
541 
542 	spin_lock(&lstat->lock);
543 	ret = snprintf(page, PAGE_SIZE, "%u\n", lstat->redirects);
544 	spin_unlock(&lstat->lock);
545 
546 	return ret;
547 }
548 ISCSI_STAT_LOGIN_RO(redirects);
549 
550 static ssize_t iscsi_stat_login_show_attr_authorize_fails(
551 	struct iscsi_wwn_stat_grps *igrps, char *page)
552 {
553 	struct iscsi_tiqn *tiqn = container_of(igrps,
554 				struct iscsi_tiqn, tiqn_stat_grps);
555 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
556 	ssize_t ret;
557 
558 	spin_lock(&lstat->lock);
559 	ret = snprintf(page, PAGE_SIZE, "%u\n", lstat->authorize_fails);
560 	spin_unlock(&lstat->lock);
561 
562 	return ret;
563 }
564 ISCSI_STAT_LOGIN_RO(authorize_fails);
565 
566 static ssize_t iscsi_stat_login_show_attr_authenticate_fails(
567 	struct iscsi_wwn_stat_grps *igrps, char *page)
568 {
569 	struct iscsi_tiqn *tiqn = container_of(igrps,
570 				struct iscsi_tiqn, tiqn_stat_grps);
571 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
572 	ssize_t ret;
573 
574 	spin_lock(&lstat->lock);
575 	ret = snprintf(page, PAGE_SIZE, "%u\n", lstat->authenticate_fails);
576 	spin_unlock(&lstat->lock);
577 
578 	return ret;
579 }
580 ISCSI_STAT_LOGIN_RO(authenticate_fails);
581 
582 static ssize_t iscsi_stat_login_show_attr_negotiate_fails(
583 	struct iscsi_wwn_stat_grps *igrps, char *page)
584 {
585 	struct iscsi_tiqn *tiqn = container_of(igrps,
586 				struct iscsi_tiqn, tiqn_stat_grps);
587 	struct iscsi_login_stats *lstat = &tiqn->login_stats;
588 	ssize_t ret;
589 
590 	spin_lock(&lstat->lock);
591 	ret = snprintf(page, PAGE_SIZE, "%u\n", lstat->negotiate_fails);
592 	spin_unlock(&lstat->lock);
593 
594 	return ret;
595 }
596 ISCSI_STAT_LOGIN_RO(negotiate_fails);
597 
598 CONFIGFS_EATTR_OPS(iscsi_stat_login, iscsi_wwn_stat_grps,
599 		iscsi_login_stats_group);
600 
601 static struct configfs_attribute *iscsi_stat_login_stats_attrs[] = {
602 	&iscsi_stat_login_inst.attr,
603 	&iscsi_stat_login_indx.attr,
604 	&iscsi_stat_login_accepts.attr,
605 	&iscsi_stat_login_other_fails.attr,
606 	&iscsi_stat_login_redirects.attr,
607 	&iscsi_stat_login_authorize_fails.attr,
608 	&iscsi_stat_login_authenticate_fails.attr,
609 	&iscsi_stat_login_negotiate_fails.attr,
610 	NULL,
611 };
612 
613 static struct configfs_item_operations iscsi_stat_login_stats_item_ops = {
614 	.show_attribute		= iscsi_stat_login_attr_show,
615 	.store_attribute	= iscsi_stat_login_attr_store,
616 };
617 
618 struct config_item_type iscsi_stat_login_cit = {
619 	.ct_item_ops		= &iscsi_stat_login_stats_item_ops,
620 	.ct_attrs		= iscsi_stat_login_stats_attrs,
621 	.ct_owner		= THIS_MODULE,
622 };
623 
624 /*
625  * Target Logout Stats Table
626  */
627 
628 CONFIGFS_EATTR_STRUCT(iscsi_stat_logout, iscsi_wwn_stat_grps);
629 #define ISCSI_STAT_LOGOUT(_name, _mode)				\
630 static struct iscsi_stat_logout_attribute			\
631 			iscsi_stat_logout_##_name =		\
632 	__CONFIGFS_EATTR(_name, _mode,				\
633 	iscsi_stat_logout_show_attr_##_name,			\
634 	iscsi_stat_logout_store_attr_##_name);
635 
636 #define ISCSI_STAT_LOGOUT_RO(_name)				\
637 static struct iscsi_stat_logout_attribute			\
638 			iscsi_stat_logout_##_name =		\
639 	__CONFIGFS_EATTR_RO(_name,				\
640 	iscsi_stat_logout_show_attr_##_name);
641 
642 static ssize_t iscsi_stat_logout_show_attr_inst(
643 	struct iscsi_wwn_stat_grps *igrps, char *page)
644 {
645 	struct iscsi_tiqn *tiqn = container_of(igrps,
646 			struct iscsi_tiqn, tiqn_stat_grps);
647 
648 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_index);
649 }
650 ISCSI_STAT_LOGOUT_RO(inst);
651 
652 static ssize_t iscsi_stat_logout_show_attr_indx(
653 	struct iscsi_wwn_stat_grps *igrps, char *page)
654 {
655 	return snprintf(page, PAGE_SIZE, "%u\n", ISCSI_NODE_INDEX);
656 }
657 ISCSI_STAT_LOGOUT_RO(indx);
658 
659 static ssize_t iscsi_stat_logout_show_attr_normal_logouts(
660 	struct iscsi_wwn_stat_grps *igrps, char *page)
661 {
662 	struct iscsi_tiqn *tiqn = container_of(igrps,
663 			struct iscsi_tiqn, tiqn_stat_grps);
664 	struct iscsi_logout_stats *lstats = &tiqn->logout_stats;
665 
666 	return snprintf(page, PAGE_SIZE, "%u\n", lstats->normal_logouts);
667 }
668 ISCSI_STAT_LOGOUT_RO(normal_logouts);
669 
670 static ssize_t iscsi_stat_logout_show_attr_abnormal_logouts(
671 	struct iscsi_wwn_stat_grps *igrps, char *page)
672 {
673 	struct iscsi_tiqn *tiqn = container_of(igrps,
674 			struct iscsi_tiqn, tiqn_stat_grps);
675 	struct iscsi_logout_stats *lstats = &tiqn->logout_stats;
676 
677 	return snprintf(page, PAGE_SIZE, "%u\n", lstats->abnormal_logouts);
678 }
679 ISCSI_STAT_LOGOUT_RO(abnormal_logouts);
680 
681 CONFIGFS_EATTR_OPS(iscsi_stat_logout, iscsi_wwn_stat_grps,
682 		iscsi_logout_stats_group);
683 
684 static struct configfs_attribute *iscsi_stat_logout_stats_attrs[] = {
685 	&iscsi_stat_logout_inst.attr,
686 	&iscsi_stat_logout_indx.attr,
687 	&iscsi_stat_logout_normal_logouts.attr,
688 	&iscsi_stat_logout_abnormal_logouts.attr,
689 	NULL,
690 };
691 
692 static struct configfs_item_operations iscsi_stat_logout_stats_item_ops = {
693 	.show_attribute		= iscsi_stat_logout_attr_show,
694 	.store_attribute	= iscsi_stat_logout_attr_store,
695 };
696 
697 struct config_item_type iscsi_stat_logout_cit = {
698 	.ct_item_ops		= &iscsi_stat_logout_stats_item_ops,
699 	.ct_attrs		= iscsi_stat_logout_stats_attrs,
700 	.ct_owner		= THIS_MODULE,
701 };
702 
703 /*
704  * Session Stats Table
705  */
706 
707 CONFIGFS_EATTR_STRUCT(iscsi_stat_sess, iscsi_node_stat_grps);
708 #define ISCSI_STAT_SESS(_name, _mode)				\
709 static struct iscsi_stat_sess_attribute				\
710 			iscsi_stat_sess_##_name =		\
711 	__CONFIGFS_EATTR(_name, _mode,				\
712 	iscsi_stat_sess_show_attr_##_name,			\
713 	iscsi_stat_sess_store_attr_##_name);
714 
715 #define ISCSI_STAT_SESS_RO(_name)				\
716 static struct iscsi_stat_sess_attribute				\
717 			iscsi_stat_sess_##_name =		\
718 	__CONFIGFS_EATTR_RO(_name,				\
719 	iscsi_stat_sess_show_attr_##_name);
720 
721 static ssize_t iscsi_stat_sess_show_attr_inst(
722 	struct iscsi_node_stat_grps *igrps, char *page)
723 {
724 	struct iscsi_node_acl *acl = container_of(igrps,
725 			struct iscsi_node_acl, node_stat_grps);
726 	struct se_wwn *wwn = acl->se_node_acl.se_tpg->se_tpg_wwn;
727 	struct iscsi_tiqn *tiqn = container_of(wwn,
728 			struct iscsi_tiqn, tiqn_wwn);
729 
730 	return snprintf(page, PAGE_SIZE, "%u\n", tiqn->tiqn_index);
731 }
732 ISCSI_STAT_SESS_RO(inst);
733 
734 static ssize_t iscsi_stat_sess_show_attr_node(
735 	struct iscsi_node_stat_grps *igrps, char *page)
736 {
737 	struct iscsi_node_acl *acl = container_of(igrps,
738 			struct iscsi_node_acl, node_stat_grps);
739 	struct se_node_acl *se_nacl = &acl->se_node_acl;
740 	struct iscsi_session *sess;
741 	struct se_session *se_sess;
742 	ssize_t ret = 0;
743 
744 	spin_lock_bh(&se_nacl->nacl_sess_lock);
745 	se_sess = se_nacl->nacl_sess;
746 	if (se_sess) {
747 		sess = se_sess->fabric_sess_ptr;
748 		if (sess)
749 			ret = snprintf(page, PAGE_SIZE, "%u\n",
750 				sess->sess_ops->SessionType ? 0 : ISCSI_NODE_INDEX);
751 	}
752 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
753 
754 	return ret;
755 }
756 ISCSI_STAT_SESS_RO(node);
757 
758 static ssize_t iscsi_stat_sess_show_attr_indx(
759 	struct iscsi_node_stat_grps *igrps, char *page)
760 {
761 	struct iscsi_node_acl *acl = container_of(igrps,
762 			struct iscsi_node_acl, node_stat_grps);
763 	struct se_node_acl *se_nacl = &acl->se_node_acl;
764 	struct iscsi_session *sess;
765 	struct se_session *se_sess;
766 	ssize_t ret = 0;
767 
768 	spin_lock_bh(&se_nacl->nacl_sess_lock);
769 	se_sess = se_nacl->nacl_sess;
770 	if (se_sess) {
771 		sess = se_sess->fabric_sess_ptr;
772 		if (sess)
773 			ret = snprintf(page, PAGE_SIZE, "%u\n",
774 					sess->session_index);
775 	}
776 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
777 
778 	return ret;
779 }
780 ISCSI_STAT_SESS_RO(indx);
781 
782 static ssize_t iscsi_stat_sess_show_attr_cmd_pdus(
783 	struct iscsi_node_stat_grps *igrps, char *page)
784 {
785 	struct iscsi_node_acl *acl = container_of(igrps,
786 			struct iscsi_node_acl, node_stat_grps);
787 	struct se_node_acl *se_nacl = &acl->se_node_acl;
788 	struct iscsi_session *sess;
789 	struct se_session *se_sess;
790 	ssize_t ret = 0;
791 
792 	spin_lock_bh(&se_nacl->nacl_sess_lock);
793 	se_sess = se_nacl->nacl_sess;
794 	if (se_sess) {
795 		sess = se_sess->fabric_sess_ptr;
796 		if (sess)
797 			ret = snprintf(page, PAGE_SIZE, "%u\n", sess->cmd_pdus);
798 	}
799 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
800 
801 	return ret;
802 }
803 ISCSI_STAT_SESS_RO(cmd_pdus);
804 
805 static ssize_t iscsi_stat_sess_show_attr_rsp_pdus(
806 	struct iscsi_node_stat_grps *igrps, char *page)
807 {
808 	struct iscsi_node_acl *acl = container_of(igrps,
809 			struct iscsi_node_acl, node_stat_grps);
810 	struct se_node_acl *se_nacl = &acl->se_node_acl;
811 	struct iscsi_session *sess;
812 	struct se_session *se_sess;
813 	ssize_t ret = 0;
814 
815 	spin_lock_bh(&se_nacl->nacl_sess_lock);
816 	se_sess = se_nacl->nacl_sess;
817 	if (se_sess) {
818 		sess = se_sess->fabric_sess_ptr;
819 		if (sess)
820 			ret = snprintf(page, PAGE_SIZE, "%u\n", sess->rsp_pdus);
821 	}
822 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
823 
824 	return ret;
825 }
826 ISCSI_STAT_SESS_RO(rsp_pdus);
827 
828 static ssize_t iscsi_stat_sess_show_attr_txdata_octs(
829 	struct iscsi_node_stat_grps *igrps, char *page)
830 {
831 	struct iscsi_node_acl *acl = container_of(igrps,
832 			struct iscsi_node_acl, node_stat_grps);
833 	struct se_node_acl *se_nacl = &acl->se_node_acl;
834 	struct iscsi_session *sess;
835 	struct se_session *se_sess;
836 	ssize_t ret = 0;
837 
838 	spin_lock_bh(&se_nacl->nacl_sess_lock);
839 	se_sess = se_nacl->nacl_sess;
840 	if (se_sess) {
841 		sess = se_sess->fabric_sess_ptr;
842 		if (sess)
843 			ret = snprintf(page, PAGE_SIZE, "%llu\n",
844 				(unsigned long long)sess->tx_data_octets);
845 	}
846 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
847 
848 	return ret;
849 }
850 ISCSI_STAT_SESS_RO(txdata_octs);
851 
852 static ssize_t iscsi_stat_sess_show_attr_rxdata_octs(
853 	struct iscsi_node_stat_grps *igrps, char *page)
854 {
855 	struct iscsi_node_acl *acl = container_of(igrps,
856 			struct iscsi_node_acl, node_stat_grps);
857 	struct se_node_acl *se_nacl = &acl->se_node_acl;
858 	struct iscsi_session *sess;
859 	struct se_session *se_sess;
860 	ssize_t ret = 0;
861 
862 	spin_lock_bh(&se_nacl->nacl_sess_lock);
863 	se_sess = se_nacl->nacl_sess;
864 	if (se_sess) {
865 		sess = se_sess->fabric_sess_ptr;
866 		if (sess)
867 			ret = snprintf(page, PAGE_SIZE, "%llu\n",
868 				(unsigned long long)sess->rx_data_octets);
869 	}
870 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
871 
872 	return ret;
873 }
874 ISCSI_STAT_SESS_RO(rxdata_octs);
875 
876 static ssize_t iscsi_stat_sess_show_attr_conn_digest_errors(
877 	struct iscsi_node_stat_grps *igrps, char *page)
878 {
879 	struct iscsi_node_acl *acl = container_of(igrps,
880 			struct iscsi_node_acl, node_stat_grps);
881 	struct se_node_acl *se_nacl = &acl->se_node_acl;
882 	struct iscsi_session *sess;
883 	struct se_session *se_sess;
884 	ssize_t ret = 0;
885 
886 	spin_lock_bh(&se_nacl->nacl_sess_lock);
887 	se_sess = se_nacl->nacl_sess;
888 	if (se_sess) {
889 		sess = se_sess->fabric_sess_ptr;
890 		if (sess)
891 			ret = snprintf(page, PAGE_SIZE, "%u\n",
892 					sess->conn_digest_errors);
893 	}
894 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
895 
896 	return ret;
897 }
898 ISCSI_STAT_SESS_RO(conn_digest_errors);
899 
900 static ssize_t iscsi_stat_sess_show_attr_conn_timeout_errors(
901 	struct iscsi_node_stat_grps *igrps, char *page)
902 {
903 	struct iscsi_node_acl *acl = container_of(igrps,
904 			struct iscsi_node_acl, node_stat_grps);
905 	struct se_node_acl *se_nacl = &acl->se_node_acl;
906 	struct iscsi_session *sess;
907 	struct se_session *se_sess;
908 	ssize_t ret = 0;
909 
910 	spin_lock_bh(&se_nacl->nacl_sess_lock);
911 	se_sess = se_nacl->nacl_sess;
912 	if (se_sess) {
913 		sess = se_sess->fabric_sess_ptr;
914 		if (sess)
915 			ret = snprintf(page, PAGE_SIZE, "%u\n",
916 					sess->conn_timeout_errors);
917 	}
918 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
919 
920 	return ret;
921 }
922 ISCSI_STAT_SESS_RO(conn_timeout_errors);
923 
924 CONFIGFS_EATTR_OPS(iscsi_stat_sess, iscsi_node_stat_grps,
925 		iscsi_sess_stats_group);
926 
927 static struct configfs_attribute *iscsi_stat_sess_stats_attrs[] = {
928 	&iscsi_stat_sess_inst.attr,
929 	&iscsi_stat_sess_node.attr,
930 	&iscsi_stat_sess_indx.attr,
931 	&iscsi_stat_sess_cmd_pdus.attr,
932 	&iscsi_stat_sess_rsp_pdus.attr,
933 	&iscsi_stat_sess_txdata_octs.attr,
934 	&iscsi_stat_sess_rxdata_octs.attr,
935 	&iscsi_stat_sess_conn_digest_errors.attr,
936 	&iscsi_stat_sess_conn_timeout_errors.attr,
937 	NULL,
938 };
939 
940 static struct configfs_item_operations iscsi_stat_sess_stats_item_ops = {
941 	.show_attribute		= iscsi_stat_sess_attr_show,
942 	.store_attribute	= iscsi_stat_sess_attr_store,
943 };
944 
945 struct config_item_type iscsi_stat_sess_cit = {
946 	.ct_item_ops		= &iscsi_stat_sess_stats_item_ops,
947 	.ct_attrs		= iscsi_stat_sess_stats_attrs,
948 	.ct_owner		= THIS_MODULE,
949 };
950