blob: 0e69180c3771378e462d51b39ed10fee42bc1dfc [file] [log] [blame]
Jack Wang215378b2020-05-11 15:51:14 +02001// 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
19static 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
28static struct kobj_type ktype_sess = {
29 .sysfs_ops = &kobj_sysfs_ops,
30 .release = rtrs_clt_sess_release
31};
32
33static 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
44static struct kobj_type ktype_stats = {
45 .sysfs_ops = &kobj_sysfs_ops,
46 .release = rtrs_clt_sess_stats_release,
47};
48
49static 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
Joe Perches1c7fd722020-10-07 19:36:24 -070055 return sysfs_emit(page, "%d\n",
56 rtrs_clt_get_max_reconnect_attempts(clt));
Jack Wang215378b2020-05-11 15:51:14 +020057}
58
59static 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
87static DEVICE_ATTR_RW(max_reconnect_attempts);
88
89static 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:
Joe Perches1c7fd722020-10-07 19:36:24 -070099 return sysfs_emit(page, "round-robin (RR: %d)\n",
100 clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200101 case MP_POLICY_MIN_INFLIGHT:
Joe Perches1c7fd722020-10-07 19:36:24 -0700102 return sysfs_emit(page, "min-inflight (MI: %d)\n",
103 clt->mp_policy);
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200104 case MP_POLICY_MIN_LATENCY:
105 return sysfs_emit(page, "min-latency (ML: %d)\n",
106 clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200107 default:
Joe Perches1c7fd722020-10-07 19:36:24 -0700108 return sysfs_emit(page, "Unknown (%d)\n", clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200109 }
110}
111
112static 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;
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200120 size_t len = 0;
Jack Wang215378b2020-05-11 15:51:14 +0200121
122 clt = container_of(dev, struct rtrs_clt, dev);
123
124 ret = kstrtoint(buf, 10, &value);
125 if (!ret && (value == MP_POLICY_RR ||
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200126 value == MP_POLICY_MIN_INFLIGHT ||
127 value == MP_POLICY_MIN_LATENCY)) {
Jack Wang215378b2020-05-11 15:51:14 +0200128 clt->mp_policy = value;
129 return count;
130 }
131
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200132 /* distinguish "mi" and "min-latency" with length */
133 len = strnlen(buf, NAME_MAX);
134 if (buf[len - 1] == '\n')
135 len--;
136
Jack Wang215378b2020-05-11 15:51:14 +0200137 if (!strncasecmp(buf, "round-robin", 11) ||
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200138 (len == 2 && !strncasecmp(buf, "rr", 2)))
Jack Wang215378b2020-05-11 15:51:14 +0200139 clt->mp_policy = MP_POLICY_RR;
140 else if (!strncasecmp(buf, "min-inflight", 12) ||
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200141 (len == 2 && !strncasecmp(buf, "mi", 2)))
Jack Wang215378b2020-05-11 15:51:14 +0200142 clt->mp_policy = MP_POLICY_MIN_INFLIGHT;
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200143 else if (!strncasecmp(buf, "min-latency", 11) ||
144 (len == 2 && !strncasecmp(buf, "ml", 2)))
145 clt->mp_policy = MP_POLICY_MIN_LATENCY;
Jack Wang215378b2020-05-11 15:51:14 +0200146 else
147 return -EINVAL;
148
149 return count;
150}
151
152static DEVICE_ATTR_RW(mpath_policy);
153
154static ssize_t add_path_show(struct device *dev,
155 struct device_attribute *attr, char *page)
156{
Joe Perches1c7fd722020-10-07 19:36:24 -0700157 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);
Jack Wang215378b2020-05-11 15:51:14 +0200161}
162
163static 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
195static DEVICE_ATTR_RW(add_path);
196
197static 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)
Joe Perches3c6bff32020-10-07 19:36:25 -0700204 return sysfs_emit(page, "connected\n");
Jack Wang215378b2020-05-11 15:51:14 +0200205
Joe Perches3c6bff32020-10-07 19:36:25 -0700206 return sysfs_emit(page, "disconnected\n");
Jack Wang215378b2020-05-11 15:51:14 +0200207}
208
209static struct kobj_attribute rtrs_clt_state_attr =
210 __ATTR(state, 0444, rtrs_clt_state_show, NULL);
211
212static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700213 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200214{
Joe Perches3c6bff32020-10-07 19:36:25 -0700215 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200216}
217
218static 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
238static struct kobj_attribute rtrs_clt_reconnect_attr =
239 __ATTR(reconnect, 0644, rtrs_clt_reconnect_show,
240 rtrs_clt_reconnect_store);
241
242static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700243 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200244{
Joe Perches3c6bff32020-10-07 19:36:25 -0700245 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200246}
247
248static 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;
Jack Wang215378b2020-05-11 15:51:14 +0200253
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 }
Guoqing Jiangb0c633c2021-05-28 13:30:07 +0200260 rtrs_clt_close_conns(sess, true);
Jack Wang215378b2020-05-11 15:51:14 +0200261
262 return count;
263}
264
265static struct kobj_attribute rtrs_clt_disconnect_attr =
266 __ATTR(disconnect, 0644, rtrs_clt_disconnect_show,
267 rtrs_clt_disconnect_store);
268
269static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700270 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200271{
Joe Perches3c6bff32020-10-07 19:36:25 -0700272 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200273}
274
275static 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
295static 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
Md Haris Iqbal3f3fe682021-09-22 14:53:33 +0200299STAT_ATTR(struct rtrs_clt_stats, cpu_migration_from,
300 rtrs_clt_stats_migration_from_cnt_to_str,
301 rtrs_clt_reset_cpu_migr_stats);
302
303STAT_ATTR(struct rtrs_clt_stats, cpu_migration_to,
304 rtrs_clt_stats_migration_to_cnt_to_str,
Jack Wang215378b2020-05-11 15:51:14 +0200305 rtrs_clt_reset_cpu_migr_stats);
306
307STAT_ATTR(struct rtrs_clt_stats, reconnects,
308 rtrs_clt_stats_reconnects_to_str,
309 rtrs_clt_reset_reconnects_stat);
310
311STAT_ATTR(struct rtrs_clt_stats, rdma,
312 rtrs_clt_stats_rdma_to_str,
313 rtrs_clt_reset_rdma_stats);
314
315STAT_ATTR(struct rtrs_clt_stats, reset_all,
316 rtrs_clt_reset_all_help,
317 rtrs_clt_reset_all_stats);
318
319static struct attribute *rtrs_clt_stats_attrs[] = {
Md Haris Iqbal3f3fe682021-09-22 14:53:33 +0200320 &cpu_migration_from_attr.attr,
321 &cpu_migration_to_attr.attr,
Jack Wang215378b2020-05-11 15:51:14 +0200322 &reconnects_attr.attr,
323 &rdma_attr.attr,
324 &reset_all_attr.attr,
325 NULL
326};
327
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200328static const struct attribute_group rtrs_clt_stats_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200329 .attrs = rtrs_clt_stats_attrs,
330};
331
332static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
333 struct kobj_attribute *attr,
334 char *page)
335{
336 struct rtrs_clt_sess *sess;
337
338 sess = container_of(kobj, typeof(*sess), kobj);
339
Joe Perches3c6bff32020-10-07 19:36:25 -0700340 return sysfs_emit(page, "%u\n", sess->hca_port);
Jack Wang215378b2020-05-11 15:51:14 +0200341}
342
343static struct kobj_attribute rtrs_clt_hca_port_attr =
344 __ATTR(hca_port, 0444, rtrs_clt_hca_port_show, NULL);
345
346static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
347 struct kobj_attribute *attr,
348 char *page)
349{
350 struct rtrs_clt_sess *sess;
351
352 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
353
Joe Perches3c6bff32020-10-07 19:36:25 -0700354 return sysfs_emit(page, "%s\n", sess->hca_name);
Jack Wang215378b2020-05-11 15:51:14 +0200355}
356
357static struct kobj_attribute rtrs_clt_hca_name_attr =
358 __ATTR(hca_name, 0444, rtrs_clt_hca_name_show, NULL);
359
Gioh Kimcc853922021-04-07 13:34:42 +0200360static ssize_t rtrs_clt_cur_latency_show(struct kobject *kobj,
361 struct kobj_attribute *attr,
362 char *page)
363{
364 struct rtrs_clt_sess *sess;
365
366 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
367
368 return sysfs_emit(page, "%lld ns\n",
369 ktime_to_ns(sess->s.hb_cur_latency));
370}
371
372static struct kobj_attribute rtrs_clt_cur_latency_attr =
373 __ATTR(cur_latency, 0444, rtrs_clt_cur_latency_show, NULL);
374
Jack Wang215378b2020-05-11 15:51:14 +0200375static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
376 struct kobj_attribute *attr,
377 char *page)
378{
379 struct rtrs_clt_sess *sess;
Joe Perchese28bf1f2020-10-07 19:36:27 -0700380 int len;
Jack Wang215378b2020-05-11 15:51:14 +0200381
382 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
Joe Perchese28bf1f2020-10-07 19:36:27 -0700383 len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr, page,
384 PAGE_SIZE);
385 len += sysfs_emit_at(page, len, "\n");
386 return len;
Jack Wang215378b2020-05-11 15:51:14 +0200387}
388
389static struct kobj_attribute rtrs_clt_src_addr_attr =
390 __ATTR(src_addr, 0444, rtrs_clt_src_addr_show, NULL);
391
392static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
393 struct kobj_attribute *attr,
394 char *page)
395{
396 struct rtrs_clt_sess *sess;
Joe Perchese28bf1f2020-10-07 19:36:27 -0700397 int len;
Jack Wang215378b2020-05-11 15:51:14 +0200398
399 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
Joe Perchese28bf1f2020-10-07 19:36:27 -0700400 len = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr, page,
401 PAGE_SIZE);
402 len += sysfs_emit_at(page, len, "\n");
403 return len;
Jack Wang215378b2020-05-11 15:51:14 +0200404}
405
406static struct kobj_attribute rtrs_clt_dst_addr_attr =
407 __ATTR(dst_addr, 0444, rtrs_clt_dst_addr_show, NULL);
408
409static struct attribute *rtrs_clt_sess_attrs[] = {
410 &rtrs_clt_hca_name_attr.attr,
411 &rtrs_clt_hca_port_attr.attr,
412 &rtrs_clt_src_addr_attr.attr,
413 &rtrs_clt_dst_addr_attr.attr,
414 &rtrs_clt_state_attr.attr,
415 &rtrs_clt_reconnect_attr.attr,
416 &rtrs_clt_disconnect_attr.attr,
417 &rtrs_clt_remove_path_attr.attr,
Gioh Kimcc853922021-04-07 13:34:42 +0200418 &rtrs_clt_cur_latency_attr.attr,
Jack Wang215378b2020-05-11 15:51:14 +0200419 NULL,
420};
421
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200422static const struct attribute_group rtrs_clt_sess_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200423 .attrs = rtrs_clt_sess_attrs,
424};
425
426int rtrs_clt_create_sess_files(struct rtrs_clt_sess *sess)
427{
428 struct rtrs_clt *clt = sess->clt;
429 char str[NAME_MAX];
Gioh Kim8e864992021-03-25 16:32:59 +0100430 int err;
431 struct rtrs_addr path = {
432 .src = &sess->s.src_addr,
433 .dst = &sess->s.dst_addr,
434 };
Jack Wang215378b2020-05-11 15:51:14 +0200435
Gioh Kim8e864992021-03-25 16:32:59 +0100436 rtrs_addr_to_str(&path, str, sizeof(str));
Jack Wang215378b2020-05-11 15:51:14 +0200437 err = kobject_init_and_add(&sess->kobj, &ktype_sess, clt->kobj_paths,
438 "%s", str);
439 if (err) {
440 pr_err("kobject_init_and_add: %d\n", err);
Guoqing Jiang424774c2020-12-17 15:19:03 +0100441 kobject_put(&sess->kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200442 return err;
443 }
444 err = sysfs_create_group(&sess->kobj, &rtrs_clt_sess_attr_group);
445 if (err) {
446 pr_err("sysfs_create_group(): %d\n", err);
447 goto put_kobj;
448 }
449 err = kobject_init_and_add(&sess->stats->kobj_stats, &ktype_stats,
450 &sess->kobj, "stats");
451 if (err) {
452 pr_err("kobject_init_and_add: %d\n", err);
Guoqing Jiang424774c2020-12-17 15:19:03 +0100453 kobject_put(&sess->stats->kobj_stats);
Jack Wang215378b2020-05-11 15:51:14 +0200454 goto remove_group;
455 }
456
457 err = sysfs_create_group(&sess->stats->kobj_stats,
458 &rtrs_clt_stats_attr_group);
459 if (err) {
460 pr_err("failed to create stats sysfs group, err: %d\n", err);
461 goto put_kobj_stats;
462 }
463
464 return 0;
465
466put_kobj_stats:
467 kobject_del(&sess->stats->kobj_stats);
468 kobject_put(&sess->stats->kobj_stats);
469remove_group:
470 sysfs_remove_group(&sess->kobj, &rtrs_clt_sess_attr_group);
471put_kobj:
472 kobject_del(&sess->kobj);
473 kobject_put(&sess->kobj);
474
475 return err;
476}
477
478void rtrs_clt_destroy_sess_files(struct rtrs_clt_sess *sess,
479 const struct attribute *sysfs_self)
480{
481 kobject_del(&sess->stats->kobj_stats);
482 kobject_put(&sess->stats->kobj_stats);
483 if (sysfs_self)
484 sysfs_remove_file_self(&sess->kobj, sysfs_self);
485 kobject_del(&sess->kobj);
486}
487
488static struct attribute *rtrs_clt_attrs[] = {
489 &dev_attr_max_reconnect_attempts.attr,
490 &dev_attr_mpath_policy.attr,
491 &dev_attr_add_path.attr,
492 NULL,
493};
494
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200495static const struct attribute_group rtrs_clt_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200496 .attrs = rtrs_clt_attrs,
497};
498
499int rtrs_clt_create_sysfs_root_files(struct rtrs_clt *clt)
500{
501 return sysfs_create_group(&clt->dev.kobj, &rtrs_clt_attr_group);
502}
503
Guoqing Jiang7b47b272020-12-17 15:19:04 +0100504void rtrs_clt_destroy_sysfs_root(struct rtrs_clt *clt)
Jack Wang215378b2020-05-11 15:51:14 +0200505{
Guoqing Jiang7b47b272020-12-17 15:19:04 +0100506 sysfs_remove_group(&clt->dev.kobj, &rtrs_clt_attr_group);
507
Jack Wang215378b2020-05-11 15:51:14 +0200508 if (clt->kobj_paths) {
509 kobject_del(clt->kobj_paths);
510 kobject_put(clt->kobj_paths);
511 }
512}