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 
254 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
255 	if (!sysfs_streq(buf, "1")) {
256 		rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
257 			  attr->attr.name, buf);
258 		return -EINVAL;
259 	}
260 	rtrs_clt_close_conns(sess, true);
261 
262 	return count;
263 }
264 
265 static struct kobj_attribute rtrs_clt_disconnect_attr =
266 	__ATTR(disconnect, 0644, rtrs_clt_disconnect_show,
267 	       rtrs_clt_disconnect_store);
268 
269 static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
270 					 struct kobj_attribute *attr, char *buf)
271 {
272 	return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
273 }
274 
275 static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
276 					   struct kobj_attribute *attr,
277 					   const char *buf, size_t count)
278 {
279 	struct rtrs_clt_sess *sess;
280 	int ret;
281 
282 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
283 	if (!sysfs_streq(buf, "1")) {
284 		rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
285 			  attr->attr.name, buf);
286 		return -EINVAL;
287 	}
288 	ret = rtrs_clt_remove_path_from_sysfs(sess, &attr->attr);
289 	if (ret)
290 		return ret;
291 
292 	return count;
293 }
294 
295 static struct kobj_attribute rtrs_clt_remove_path_attr =
296 	__ATTR(remove_path, 0644, rtrs_clt_remove_path_show,
297 	       rtrs_clt_remove_path_store);
298 
299 STAT_ATTR(struct rtrs_clt_stats, cpu_migration,
300 	  rtrs_clt_stats_migration_cnt_to_str,
301 	  rtrs_clt_reset_cpu_migr_stats);
302 
303 STAT_ATTR(struct rtrs_clt_stats, reconnects,
304 	  rtrs_clt_stats_reconnects_to_str,
305 	  rtrs_clt_reset_reconnects_stat);
306 
307 STAT_ATTR(struct rtrs_clt_stats, rdma,
308 	  rtrs_clt_stats_rdma_to_str,
309 	  rtrs_clt_reset_rdma_stats);
310 
311 STAT_ATTR(struct rtrs_clt_stats, reset_all,
312 	  rtrs_clt_reset_all_help,
313 	  rtrs_clt_reset_all_stats);
314 
315 static struct attribute *rtrs_clt_stats_attrs[] = {
316 	&cpu_migration_attr.attr,
317 	&reconnects_attr.attr,
318 	&rdma_attr.attr,
319 	&reset_all_attr.attr,
320 	NULL
321 };
322 
323 static const struct attribute_group rtrs_clt_stats_attr_group = {
324 	.attrs = rtrs_clt_stats_attrs,
325 };
326 
327 static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
328 				       struct kobj_attribute *attr,
329 				       char *page)
330 {
331 	struct rtrs_clt_sess *sess;
332 
333 	sess = container_of(kobj, typeof(*sess), kobj);
334 
335 	return sysfs_emit(page, "%u\n", sess->hca_port);
336 }
337 
338 static struct kobj_attribute rtrs_clt_hca_port_attr =
339 	__ATTR(hca_port, 0444, rtrs_clt_hca_port_show, NULL);
340 
341 static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
342 				       struct kobj_attribute *attr,
343 				       char *page)
344 {
345 	struct rtrs_clt_sess *sess;
346 
347 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
348 
349 	return sysfs_emit(page, "%s\n", sess->hca_name);
350 }
351 
352 static struct kobj_attribute rtrs_clt_hca_name_attr =
353 	__ATTR(hca_name, 0444, rtrs_clt_hca_name_show, NULL);
354 
355 static ssize_t rtrs_clt_cur_latency_show(struct kobject *kobj,
356 				    struct kobj_attribute *attr,
357 				    char *page)
358 {
359 	struct rtrs_clt_sess *sess;
360 
361 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
362 
363 	return sysfs_emit(page, "%lld ns\n",
364 			  ktime_to_ns(sess->s.hb_cur_latency));
365 }
366 
367 static struct kobj_attribute rtrs_clt_cur_latency_attr =
368 	__ATTR(cur_latency, 0444, rtrs_clt_cur_latency_show, NULL);
369 
370 static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
371 				       struct kobj_attribute *attr,
372 				       char *page)
373 {
374 	struct rtrs_clt_sess *sess;
375 	int len;
376 
377 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
378 	len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr, page,
379 			      PAGE_SIZE);
380 	len += sysfs_emit_at(page, len, "\n");
381 	return len;
382 }
383 
384 static struct kobj_attribute rtrs_clt_src_addr_attr =
385 	__ATTR(src_addr, 0444, rtrs_clt_src_addr_show, NULL);
386 
387 static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
388 				       struct kobj_attribute *attr,
389 				       char *page)
390 {
391 	struct rtrs_clt_sess *sess;
392 	int len;
393 
394 	sess = container_of(kobj, struct rtrs_clt_sess, kobj);
395 	len = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr, page,
396 			      PAGE_SIZE);
397 	len += sysfs_emit_at(page, len, "\n");
398 	return len;
399 }
400 
401 static struct kobj_attribute rtrs_clt_dst_addr_attr =
402 	__ATTR(dst_addr, 0444, rtrs_clt_dst_addr_show, NULL);
403 
404 static struct attribute *rtrs_clt_sess_attrs[] = {
405 	&rtrs_clt_hca_name_attr.attr,
406 	&rtrs_clt_hca_port_attr.attr,
407 	&rtrs_clt_src_addr_attr.attr,
408 	&rtrs_clt_dst_addr_attr.attr,
409 	&rtrs_clt_state_attr.attr,
410 	&rtrs_clt_reconnect_attr.attr,
411 	&rtrs_clt_disconnect_attr.attr,
412 	&rtrs_clt_remove_path_attr.attr,
413 	&rtrs_clt_cur_latency_attr.attr,
414 	NULL,
415 };
416 
417 static const struct attribute_group rtrs_clt_sess_attr_group = {
418 	.attrs = rtrs_clt_sess_attrs,
419 };
420 
421 int rtrs_clt_create_sess_files(struct rtrs_clt_sess *sess)
422 {
423 	struct rtrs_clt *clt = sess->clt;
424 	char str[NAME_MAX];
425 	int err;
426 	struct rtrs_addr path = {
427 		.src = &sess->s.src_addr,
428 		.dst = &sess->s.dst_addr,
429 	};
430 
431 	rtrs_addr_to_str(&path, str, sizeof(str));
432 	err = kobject_init_and_add(&sess->kobj, &ktype_sess, clt->kobj_paths,
433 				   "%s", str);
434 	if (err) {
435 		pr_err("kobject_init_and_add: %d\n", err);
436 		kobject_put(&sess->kobj);
437 		return err;
438 	}
439 	err = sysfs_create_group(&sess->kobj, &rtrs_clt_sess_attr_group);
440 	if (err) {
441 		pr_err("sysfs_create_group(): %d\n", err);
442 		goto put_kobj;
443 	}
444 	err = kobject_init_and_add(&sess->stats->kobj_stats, &ktype_stats,
445 				   &sess->kobj, "stats");
446 	if (err) {
447 		pr_err("kobject_init_and_add: %d\n", err);
448 		kobject_put(&sess->stats->kobj_stats);
449 		goto remove_group;
450 	}
451 
452 	err = sysfs_create_group(&sess->stats->kobj_stats,
453 				 &rtrs_clt_stats_attr_group);
454 	if (err) {
455 		pr_err("failed to create stats sysfs group, err: %d\n", err);
456 		goto put_kobj_stats;
457 	}
458 
459 	return 0;
460 
461 put_kobj_stats:
462 	kobject_del(&sess->stats->kobj_stats);
463 	kobject_put(&sess->stats->kobj_stats);
464 remove_group:
465 	sysfs_remove_group(&sess->kobj, &rtrs_clt_sess_attr_group);
466 put_kobj:
467 	kobject_del(&sess->kobj);
468 	kobject_put(&sess->kobj);
469 
470 	return err;
471 }
472 
473 void rtrs_clt_destroy_sess_files(struct rtrs_clt_sess *sess,
474 				  const struct attribute *sysfs_self)
475 {
476 	kobject_del(&sess->stats->kobj_stats);
477 	kobject_put(&sess->stats->kobj_stats);
478 	if (sysfs_self)
479 		sysfs_remove_file_self(&sess->kobj, sysfs_self);
480 	kobject_del(&sess->kobj);
481 }
482 
483 static struct attribute *rtrs_clt_attrs[] = {
484 	&dev_attr_max_reconnect_attempts.attr,
485 	&dev_attr_mpath_policy.attr,
486 	&dev_attr_add_path.attr,
487 	NULL,
488 };
489 
490 static const struct attribute_group rtrs_clt_attr_group = {
491 	.attrs = rtrs_clt_attrs,
492 };
493 
494 int rtrs_clt_create_sysfs_root_files(struct rtrs_clt *clt)
495 {
496 	return sysfs_create_group(&clt->dev.kobj, &rtrs_clt_attr_group);
497 }
498 
499 void rtrs_clt_destroy_sysfs_root(struct rtrs_clt *clt)
500 {
501 	sysfs_remove_group(&clt->dev.kobj, &rtrs_clt_attr_group);
502 
503 	if (clt->kobj_paths) {
504 		kobject_del(clt->kobj_paths);
505 		kobject_put(clt->kobj_paths);
506 	}
507 }
508