1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * RDMA Transport Layer
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 #undef pr_fmt
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
11 
12 #include "rtrs-pri.h"
13 #include "rtrs-clt.h"
14 #include "rtrs-log.h"
15 
16 #define MIN_MAX_RECONN_ATT -1
17 #define MAX_MAX_RECONN_ATT 9999
18 
19 static void rtrs_clt_sess_release(struct kobject *kobj)
20 {
21 	struct rtrs_clt_sess *sess;
22 
23 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
24 
25 	free_sess(sess);
26 }
27 
28 static struct kobj_type ktype_sess = {
29 	.sysfs_ops = &kobj_sysfs_ops,
30 	.release = rtrs_clt_sess_release
31 };
32 
33 static void rtrs_clt_sess_stats_release(struct kobject *kobj)
34 {
35 	struct rtrs_clt_stats *stats;
36 
37 	stats = container_of(kobj, struct rtrs_clt_stats, kobj_stats);
38 
39 	free_percpu(stats->pcpu_stats);
40 
41 	kfree(stats);
42 }
43 
44 static struct kobj_type ktype_stats = {
45 	.sysfs_ops = &kobj_sysfs_ops,
46 	.release = rtrs_clt_sess_stats_release,
47 };
48 
49 static ssize_t max_reconnect_attempts_show(struct device *dev,
50 					   struct device_attribute *attr,
51 					   char *page)
52 {
53 	struct rtrs_clt *clt = container_of(dev, struct rtrs_clt, dev);
54 
55 	return sysfs_emit(page, "%d\n",
56 			  rtrs_clt_get_max_reconnect_attempts(clt));
57 }
58 
59 static ssize_t max_reconnect_attempts_store(struct device *dev,
60 					    struct device_attribute *attr,
61 					    const char *buf,
62 					    size_t count)
63 {
64 	int value;
65 	int ret;
66 	struct rtrs_clt *clt  = container_of(dev, struct rtrs_clt, dev);
67 
68 	ret = kstrtoint(buf, 10, &value);
69 	if (ret) {
70 		rtrs_err(clt, "%s: failed to convert string '%s' to int\n",
71 			  attr->attr.name, buf);
72 		return ret;
73 	}
74 	if (value > MAX_MAX_RECONN_ATT ||
75 		     value < MIN_MAX_RECONN_ATT) {
76 		rtrs_err(clt,
77 			  "%s: invalid range (provided: '%s', accepted: min: %d, max: %d)\n",
78 			  attr->attr.name, buf, MIN_MAX_RECONN_ATT,
79 			  MAX_MAX_RECONN_ATT);
80 		return -EINVAL;
81 	}
82 	rtrs_clt_set_max_reconnect_attempts(clt, value);
83 
84 	return count;
85 }
86 
87 static DEVICE_ATTR_RW(max_reconnect_attempts);
88 
89 static ssize_t mpath_policy_show(struct device *dev,
90 				 struct device_attribute *attr,
91 				 char *page)
92 {
93 	struct rtrs_clt *clt;
94 
95 	clt = container_of(dev, struct rtrs_clt, dev);
96 
97 	switch (clt->mp_policy) {
98 	case MP_POLICY_RR:
99 		return sysfs_emit(page, "round-robin (RR: %d)\n",
100 				  clt->mp_policy);
101 	case MP_POLICY_MIN_INFLIGHT:
102 		return sysfs_emit(page, "min-inflight (MI: %d)\n",
103 				  clt->mp_policy);
104 	case MP_POLICY_MIN_LATENCY:
105 		return sysfs_emit(page, "min-latency (ML: %d)\n",
106 				  clt->mp_policy);
107 	default:
108 		return sysfs_emit(page, "Unknown (%d)\n", clt->mp_policy);
109 	}
110 }
111 
112 static ssize_t mpath_policy_store(struct device *dev,
113 				  struct device_attribute *attr,
114 				  const char *buf,
115 				  size_t count)
116 {
117 	struct rtrs_clt *clt;
118 	int value;
119 	int ret;
120 	size_t len = 0;
121 
122 	clt = container_of(dev, struct rtrs_clt, dev);
123 
124 	ret = kstrtoint(buf, 10, &value);
125 	if (!ret && (value == MP_POLICY_RR ||
126 		     value == MP_POLICY_MIN_INFLIGHT ||
127 		     value == MP_POLICY_MIN_LATENCY)) {
128 		clt->mp_policy = value;
129 		return count;
130 	}
131 
132 	/* distinguish "mi" and "min-latency" with length */
133 	len = strnlen(buf, NAME_MAX);
134 	if (buf[len - 1] == '\n')
135 		len--;
136 
137 	if (!strncasecmp(buf, "round-robin", 11) ||
138 	    (len == 2 && !strncasecmp(buf, "rr", 2)))
139 		clt->mp_policy = MP_POLICY_RR;
140 	else if (!strncasecmp(buf, "min-inflight", 12) ||
141 		 (len == 2 && !strncasecmp(buf, "mi", 2)))
142 		clt->mp_policy = MP_POLICY_MIN_INFLIGHT;
143 	else if (!strncasecmp(buf, "min-latency", 11) ||
144 		 (len == 2 && !strncasecmp(buf, "ml", 2)))
145 		clt->mp_policy = MP_POLICY_MIN_LATENCY;
146 	else
147 		return -EINVAL;
148 
149 	return count;
150 }
151 
152 static DEVICE_ATTR_RW(mpath_policy);
153 
154 static ssize_t add_path_show(struct device *dev,
155 			     struct device_attribute *attr, char *page)
156 {
157 	return sysfs_emit(
158 		page,
159 		"Usage: echo [<source addr>@]<destination addr> > %s\n\n*addr ::= [ ip:<ipv4|ipv6> | gid:<gid> ]\n",
160 		attr->attr.name);
161 }
162 
163 static ssize_t add_path_store(struct device *dev,
164 			      struct device_attribute *attr,
165 			      const char *buf, size_t count)
166 {
167 	struct sockaddr_storage srcaddr, dstaddr;
168 	struct rtrs_addr addr = {
169 		.src = &srcaddr,
170 		.dst = &dstaddr
171 	};
172 	struct rtrs_clt *clt;
173 	const char *nl;
174 	size_t len;
175 	int err;
176 
177 	clt = container_of(dev, struct rtrs_clt, dev);
178 
179 	nl = strchr(buf, '\n');
180 	if (nl)
181 		len = nl - buf;
182 	else
183 		len = count;
184 	err = rtrs_addr_to_sockaddr(buf, len, clt->port, &addr);
185 	if (err)
186 		return -EINVAL;
187 
188 	err = rtrs_clt_create_path_from_sysfs(clt, &addr);
189 	if (err)
190 		return err;
191 
192 	return count;
193 }
194 
195 static DEVICE_ATTR_RW(add_path);
196 
197 static ssize_t rtrs_clt_state_show(struct kobject *kobj,
198 				    struct kobj_attribute *attr, char *page)
199 {
200 	struct rtrs_clt_sess *sess;
201 
202 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
203 	if (sess->state == RTRS_CLT_CONNECTED)
204 		return sysfs_emit(page, "connected\n");
205 
206 	return sysfs_emit(page, "disconnected\n");
207 }
208 
209 static struct kobj_attribute rtrs_clt_state_attr =
210 	__ATTR(state, 0444, rtrs_clt_state_show, NULL);
211 
212 static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
213 				       struct kobj_attribute *attr, char *buf)
214 {
215 	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
216 }
217 
218 static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
219 					 struct kobj_attribute *attr,
220 					 const char *buf, size_t count)
221 {
222 	struct rtrs_clt_sess *sess;
223 	int ret;
224 
225 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
226 	if (!sysfs_streq(buf, "1")) {
227 		rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
228 			  attr->attr.name, buf);
229 		return -EINVAL;
230 	}
231 	ret = rtrs_clt_reconnect_from_sysfs(sess);
232 	if (ret)
233 		return ret;
234 
235 	return count;
236 }
237 
238 static struct kobj_attribute rtrs_clt_reconnect_attr =
239 	__ATTR(reconnect, 0644, rtrs_clt_reconnect_show,
240 	       rtrs_clt_reconnect_store);
241 
242 static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
243 					struct kobj_attribute *attr, char *buf)
244 {
245 	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
246 }
247 
248 static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
249 					  struct kobj_attribute *attr,
250 					  const char *buf, size_t count)
251 {
252 	struct rtrs_clt_sess *sess;
253 	int ret;
254 
255 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
256 	if (!sysfs_streq(buf, "1")) {
257 		rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
258 			  attr->attr.name, buf);
259 		return -EINVAL;
260 	}
261 	ret = rtrs_clt_disconnect_from_sysfs(sess);
262 	if (ret)
263 		return ret;
264 
265 	return count;
266 }
267 
268 static struct kobj_attribute rtrs_clt_disconnect_attr =
269 	__ATTR(disconnect, 0644, rtrs_clt_disconnect_show,
270 	       rtrs_clt_disconnect_store);
271 
272 static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
273 					 struct kobj_attribute *attr, char *buf)
274 {
275 	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
276 }
277 
278 static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
279 					   struct kobj_attribute *attr,
280 					   const char *buf, size_t count)
281 {
282 	struct rtrs_clt_sess *sess;
283 	int ret;
284 
285 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
286 	if (!sysfs_streq(buf, "1")) {
287 		rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
288 			  attr->attr.name, buf);
289 		return -EINVAL;
290 	}
291 	ret = rtrs_clt_remove_path_from_sysfs(sess, &attr->attr);
292 	if (ret)
293 		return ret;
294 
295 	return count;
296 }
297 
298 static struct kobj_attribute rtrs_clt_remove_path_attr =
299 	__ATTR(remove_path, 0644, rtrs_clt_remove_path_show,
300 	       rtrs_clt_remove_path_store);
301 
302 STAT_ATTR(struct rtrs_clt_stats, cpu_migration,
303 	  rtrs_clt_stats_migration_cnt_to_str,
304 	  rtrs_clt_reset_cpu_migr_stats);
305 
306 STAT_ATTR(struct rtrs_clt_stats, reconnects,
307 	  rtrs_clt_stats_reconnects_to_str,
308 	  rtrs_clt_reset_reconnects_stat);
309 
310 STAT_ATTR(struct rtrs_clt_stats, rdma,
311 	  rtrs_clt_stats_rdma_to_str,
312 	  rtrs_clt_reset_rdma_stats);
313 
314 STAT_ATTR(struct rtrs_clt_stats, reset_all,
315 	  rtrs_clt_reset_all_help,
316 	  rtrs_clt_reset_all_stats);
317 
318 static struct attribute *rtrs_clt_stats_attrs[] = {
319 	&cpu_migration_attr.attr,
320 	&reconnects_attr.attr,
321 	&rdma_attr.attr,
322 	&reset_all_attr.attr,
323 	NULL
324 };
325 
326 static const struct attribute_group rtrs_clt_stats_attr_group = {
327 	.attrs = rtrs_clt_stats_attrs,
328 };
329 
330 static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
331 				       struct kobj_attribute *attr,
332 				       char *page)
333 {
334 	struct rtrs_clt_sess *sess;
335 
336 	sess = container_of(kobj, typeof(*sess), kobj);
337 
338 	return sysfs_emit(page, "%u\n", sess->hca_port);
339 }
340 
341 static struct kobj_attribute rtrs_clt_hca_port_attr =
342 	__ATTR(hca_port, 0444, rtrs_clt_hca_port_show, NULL);
343 
344 static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
345 				       struct kobj_attribute *attr,
346 				       char *page)
347 {
348 	struct rtrs_clt_sess *sess;
349 
350 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
351 
352 	return sysfs_emit(page, "%s\n", sess->hca_name);
353 }
354 
355 static struct kobj_attribute rtrs_clt_hca_name_attr =
356 	__ATTR(hca_name, 0444, rtrs_clt_hca_name_show, NULL);
357 
358 static ssize_t rtrs_clt_cur_latency_show(struct kobject *kobj,
359 				    struct kobj_attribute *attr,
360 				    char *page)
361 {
362 	struct rtrs_clt_sess *sess;
363 
364 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
365 
366 	return sysfs_emit(page, "%lld ns\n",
367 			  ktime_to_ns(sess->s.hb_cur_latency));
368 }
369 
370 static struct kobj_attribute rtrs_clt_cur_latency_attr =
371 	__ATTR(cur_latency, 0444, rtrs_clt_cur_latency_show, NULL);
372 
373 static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
374 				       struct kobj_attribute *attr,
375 				       char *page)
376 {
377 	struct rtrs_clt_sess *sess;
378 	int len;
379 
380 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
381 	len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr, page,
382 			      PAGE_SIZE);
383 	len += sysfs_emit_at(page, len, "\n");
384 	return len;
385 }
386 
387 static struct kobj_attribute rtrs_clt_src_addr_attr =
388 	__ATTR(src_addr, 0444, rtrs_clt_src_addr_show, NULL);
389 
390 static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
391 				       struct kobj_attribute *attr,
392 				       char *page)
393 {
394 	struct rtrs_clt_sess *sess;
395 	int len;
396 
397 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
398 	len = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr, page,
399 			      PAGE_SIZE);
400 	len += sysfs_emit_at(page, len, "\n");
401 	return len;
402 }
403 
404 static struct kobj_attribute rtrs_clt_dst_addr_attr =
405 	__ATTR(dst_addr, 0444, rtrs_clt_dst_addr_show, NULL);
406 
407 static struct attribute *rtrs_clt_sess_attrs[] = {
408 	&rtrs_clt_hca_name_attr.attr,
409 	&rtrs_clt_hca_port_attr.attr,
410 	&rtrs_clt_src_addr_attr.attr,
411 	&rtrs_clt_dst_addr_attr.attr,
412 	&rtrs_clt_state_attr.attr,
413 	&rtrs_clt_reconnect_attr.attr,
414 	&rtrs_clt_disconnect_attr.attr,
415 	&rtrs_clt_remove_path_attr.attr,
416 	&rtrs_clt_cur_latency_attr.attr,
417 	NULL,
418 };
419 
420 static const struct attribute_group rtrs_clt_sess_attr_group = {
421 	.attrs = rtrs_clt_sess_attrs,
422 };
423 
424 int rtrs_clt_create_sess_files(struct rtrs_clt_sess *sess)
425 {
426 	struct rtrs_clt *clt = sess->clt;
427 	char str[NAME_MAX];
428 	int err;
429 	struct rtrs_addr path = {
430 		.src = &sess->s.src_addr,
431 		.dst = &sess->s.dst_addr,
432 	};
433 
434 	rtrs_addr_to_str(&path, str, sizeof(str));
435 	err = kobject_init_and_add(&sess->kobj, &ktype_sess, clt->kobj_paths,
436 				   "%s", str);
437 	if (err) {
438 		pr_err("kobject_init_and_add: %d\n", err);
439 		kobject_put(&sess->kobj);
440 		return err;
441 	}
442 	err = sysfs_create_group(&sess->kobj, &rtrs_clt_sess_attr_group);
443 	if (err) {
444 		pr_err("sysfs_create_group(): %d\n", err);
445 		goto put_kobj;
446 	}
447 	err = kobject_init_and_add(&sess->stats->kobj_stats, &ktype_stats,
448 				   &sess->kobj, "stats");
449 	if (err) {
450 		pr_err("kobject_init_and_add: %d\n", err);
451 		kobject_put(&sess->stats->kobj_stats);
452 		goto remove_group;
453 	}
454 
455 	err = sysfs_create_group(&sess->stats->kobj_stats,
456 				 &rtrs_clt_stats_attr_group);
457 	if (err) {
458 		pr_err("failed to create stats sysfs group, err: %d\n", err);
459 		goto put_kobj_stats;
460 	}
461 
462 	return 0;
463 
464 put_kobj_stats:
465 	kobject_del(&sess->stats->kobj_stats);
466 	kobject_put(&sess->stats->kobj_stats);
467 remove_group:
468 	sysfs_remove_group(&sess->kobj, &rtrs_clt_sess_attr_group);
469 put_kobj:
470 	kobject_del(&sess->kobj);
471 	kobject_put(&sess->kobj);
472 
473 	return err;
474 }
475 
476 void rtrs_clt_destroy_sess_files(struct rtrs_clt_sess *sess,
477 				  const struct attribute *sysfs_self)
478 {
479 	kobject_del(&sess->stats->kobj_stats);
480 	kobject_put(&sess->stats->kobj_stats);
481 	if (sysfs_self)
482 		sysfs_remove_file_self(&sess->kobj, sysfs_self);
483 	kobject_del(&sess->kobj);
484 }
485 
486 static struct attribute *rtrs_clt_attrs[] = {
487 	&dev_attr_max_reconnect_attempts.attr,
488 	&dev_attr_mpath_policy.attr,
489 	&dev_attr_add_path.attr,
490 	NULL,
491 };
492 
493 static const struct attribute_group rtrs_clt_attr_group = {
494 	.attrs = rtrs_clt_attrs,
495 };
496 
497 int rtrs_clt_create_sysfs_root_files(struct rtrs_clt *clt)
498 {
499 	return sysfs_create_group(&clt->dev.kobj, &rtrs_clt_attr_group);
500 }
501 
502 void rtrs_clt_destroy_sysfs_root(struct rtrs_clt *clt)
503 {
504 	sysfs_remove_group(&clt->dev.kobj, &rtrs_clt_attr_group);
505 
506 	if (clt->kobj_paths) {
507 		kobject_del(clt->kobj_paths);
508 		kobject_put(clt->kobj_paths);
509 	}
510 }
511