blob: eb92ec13cb571e26b12085e92d4696e16fe4ebd0 [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);
Jack Wang215378b2020-05-11 15:51:14 +0200104 default:
Joe Perches1c7fd722020-10-07 19:36:24 -0700105 return sysfs_emit(page, "Unknown (%d)\n", clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200106 }
107}
108
109static ssize_t mpath_policy_store(struct device *dev,
110 struct device_attribute *attr,
111 const char *buf,
112 size_t count)
113{
114 struct rtrs_clt *clt;
115 int value;
116 int ret;
117
118 clt = container_of(dev, struct rtrs_clt, dev);
119
120 ret = kstrtoint(buf, 10, &value);
121 if (!ret && (value == MP_POLICY_RR ||
122 value == MP_POLICY_MIN_INFLIGHT)) {
123 clt->mp_policy = value;
124 return count;
125 }
126
127 if (!strncasecmp(buf, "round-robin", 11) ||
128 !strncasecmp(buf, "rr", 2))
129 clt->mp_policy = MP_POLICY_RR;
130 else if (!strncasecmp(buf, "min-inflight", 12) ||
131 !strncasecmp(buf, "mi", 2))
132 clt->mp_policy = MP_POLICY_MIN_INFLIGHT;
133 else
134 return -EINVAL;
135
136 return count;
137}
138
139static DEVICE_ATTR_RW(mpath_policy);
140
141static ssize_t add_path_show(struct device *dev,
142 struct device_attribute *attr, char *page)
143{
Joe Perches1c7fd722020-10-07 19:36:24 -0700144 return sysfs_emit(
145 page,
146 "Usage: echo [<source addr>@]<destination addr> > %s\n\n*addr ::= [ ip:<ipv4|ipv6> | gid:<gid> ]\n",
147 attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200148}
149
150static ssize_t add_path_store(struct device *dev,
151 struct device_attribute *attr,
152 const char *buf, size_t count)
153{
154 struct sockaddr_storage srcaddr, dstaddr;
155 struct rtrs_addr addr = {
156 .src = &srcaddr,
157 .dst = &dstaddr
158 };
159 struct rtrs_clt *clt;
160 const char *nl;
161 size_t len;
162 int err;
163
164 clt = container_of(dev, struct rtrs_clt, dev);
165
166 nl = strchr(buf, '\n');
167 if (nl)
168 len = nl - buf;
169 else
170 len = count;
171 err = rtrs_addr_to_sockaddr(buf, len, clt->port, &addr);
172 if (err)
173 return -EINVAL;
174
175 err = rtrs_clt_create_path_from_sysfs(clt, &addr);
176 if (err)
177 return err;
178
179 return count;
180}
181
182static DEVICE_ATTR_RW(add_path);
183
184static ssize_t rtrs_clt_state_show(struct kobject *kobj,
185 struct kobj_attribute *attr, char *page)
186{
187 struct rtrs_clt_sess *sess;
188
189 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
190 if (sess->state == RTRS_CLT_CONNECTED)
Joe Perches3c6bff32020-10-07 19:36:25 -0700191 return sysfs_emit(page, "connected\n");
Jack Wang215378b2020-05-11 15:51:14 +0200192
Joe Perches3c6bff32020-10-07 19:36:25 -0700193 return sysfs_emit(page, "disconnected\n");
Jack Wang215378b2020-05-11 15:51:14 +0200194}
195
196static struct kobj_attribute rtrs_clt_state_attr =
197 __ATTR(state, 0444, rtrs_clt_state_show, NULL);
198
199static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700200 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200201{
Joe Perches3c6bff32020-10-07 19:36:25 -0700202 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200203}
204
205static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
206 struct kobj_attribute *attr,
207 const char *buf, size_t count)
208{
209 struct rtrs_clt_sess *sess;
210 int ret;
211
212 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
213 if (!sysfs_streq(buf, "1")) {
214 rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
215 attr->attr.name, buf);
216 return -EINVAL;
217 }
218 ret = rtrs_clt_reconnect_from_sysfs(sess);
219 if (ret)
220 return ret;
221
222 return count;
223}
224
225static struct kobj_attribute rtrs_clt_reconnect_attr =
226 __ATTR(reconnect, 0644, rtrs_clt_reconnect_show,
227 rtrs_clt_reconnect_store);
228
229static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700230 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200231{
Joe Perches3c6bff32020-10-07 19:36:25 -0700232 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200233}
234
235static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
236 struct kobj_attribute *attr,
237 const char *buf, size_t count)
238{
239 struct rtrs_clt_sess *sess;
240 int ret;
241
242 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
243 if (!sysfs_streq(buf, "1")) {
244 rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
245 attr->attr.name, buf);
246 return -EINVAL;
247 }
248 ret = rtrs_clt_disconnect_from_sysfs(sess);
249 if (ret)
250 return ret;
251
252 return count;
253}
254
255static struct kobj_attribute rtrs_clt_disconnect_attr =
256 __ATTR(disconnect, 0644, rtrs_clt_disconnect_show,
257 rtrs_clt_disconnect_store);
258
259static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700260 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200261{
Joe Perches3c6bff32020-10-07 19:36:25 -0700262 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200263}
264
265static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
266 struct kobj_attribute *attr,
267 const char *buf, size_t count)
268{
269 struct rtrs_clt_sess *sess;
270 int ret;
271
272 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
273 if (!sysfs_streq(buf, "1")) {
274 rtrs_err(sess->clt, "%s: unknown value: '%s'\n",
275 attr->attr.name, buf);
276 return -EINVAL;
277 }
278 ret = rtrs_clt_remove_path_from_sysfs(sess, &attr->attr);
279 if (ret)
280 return ret;
281
282 return count;
283}
284
285static struct kobj_attribute rtrs_clt_remove_path_attr =
286 __ATTR(remove_path, 0644, rtrs_clt_remove_path_show,
287 rtrs_clt_remove_path_store);
288
289STAT_ATTR(struct rtrs_clt_stats, cpu_migration,
290 rtrs_clt_stats_migration_cnt_to_str,
291 rtrs_clt_reset_cpu_migr_stats);
292
293STAT_ATTR(struct rtrs_clt_stats, reconnects,
294 rtrs_clt_stats_reconnects_to_str,
295 rtrs_clt_reset_reconnects_stat);
296
297STAT_ATTR(struct rtrs_clt_stats, rdma,
298 rtrs_clt_stats_rdma_to_str,
299 rtrs_clt_reset_rdma_stats);
300
301STAT_ATTR(struct rtrs_clt_stats, reset_all,
302 rtrs_clt_reset_all_help,
303 rtrs_clt_reset_all_stats);
304
305static struct attribute *rtrs_clt_stats_attrs[] = {
306 &cpu_migration_attr.attr,
307 &reconnects_attr.attr,
308 &rdma_attr.attr,
309 &reset_all_attr.attr,
310 NULL
311};
312
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200313static const struct attribute_group rtrs_clt_stats_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200314 .attrs = rtrs_clt_stats_attrs,
315};
316
317static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
318 struct kobj_attribute *attr,
319 char *page)
320{
321 struct rtrs_clt_sess *sess;
322
323 sess = container_of(kobj, typeof(*sess), kobj);
324
Joe Perches3c6bff32020-10-07 19:36:25 -0700325 return sysfs_emit(page, "%u\n", sess->hca_port);
Jack Wang215378b2020-05-11 15:51:14 +0200326}
327
328static struct kobj_attribute rtrs_clt_hca_port_attr =
329 __ATTR(hca_port, 0444, rtrs_clt_hca_port_show, NULL);
330
331static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
332 struct kobj_attribute *attr,
333 char *page)
334{
335 struct rtrs_clt_sess *sess;
336
337 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
338
Joe Perches3c6bff32020-10-07 19:36:25 -0700339 return sysfs_emit(page, "%s\n", sess->hca_name);
Jack Wang215378b2020-05-11 15:51:14 +0200340}
341
342static struct kobj_attribute rtrs_clt_hca_name_attr =
343 __ATTR(hca_name, 0444, rtrs_clt_hca_name_show, NULL);
344
345static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
346 struct kobj_attribute *attr,
347 char *page)
348{
349 struct rtrs_clt_sess *sess;
Joe Perchese28bf1f2020-10-07 19:36:27 -0700350 int len;
Jack Wang215378b2020-05-11 15:51:14 +0200351
352 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
Joe Perchese28bf1f2020-10-07 19:36:27 -0700353 len = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr, page,
354 PAGE_SIZE);
355 len += sysfs_emit_at(page, len, "\n");
356 return len;
Jack Wang215378b2020-05-11 15:51:14 +0200357}
358
359static struct kobj_attribute rtrs_clt_src_addr_attr =
360 __ATTR(src_addr, 0444, rtrs_clt_src_addr_show, NULL);
361
362static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
363 struct kobj_attribute *attr,
364 char *page)
365{
366 struct rtrs_clt_sess *sess;
Joe Perchese28bf1f2020-10-07 19:36:27 -0700367 int len;
Jack Wang215378b2020-05-11 15:51:14 +0200368
369 sess = container_of(kobj, struct rtrs_clt_sess, kobj);
Joe Perchese28bf1f2020-10-07 19:36:27 -0700370 len = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr, page,
371 PAGE_SIZE);
372 len += sysfs_emit_at(page, len, "\n");
373 return len;
Jack Wang215378b2020-05-11 15:51:14 +0200374}
375
376static struct kobj_attribute rtrs_clt_dst_addr_attr =
377 __ATTR(dst_addr, 0444, rtrs_clt_dst_addr_show, NULL);
378
379static struct attribute *rtrs_clt_sess_attrs[] = {
380 &rtrs_clt_hca_name_attr.attr,
381 &rtrs_clt_hca_port_attr.attr,
382 &rtrs_clt_src_addr_attr.attr,
383 &rtrs_clt_dst_addr_attr.attr,
384 &rtrs_clt_state_attr.attr,
385 &rtrs_clt_reconnect_attr.attr,
386 &rtrs_clt_disconnect_attr.attr,
387 &rtrs_clt_remove_path_attr.attr,
388 NULL,
389};
390
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200391static const struct attribute_group rtrs_clt_sess_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200392 .attrs = rtrs_clt_sess_attrs,
393};
394
395int rtrs_clt_create_sess_files(struct rtrs_clt_sess *sess)
396{
397 struct rtrs_clt *clt = sess->clt;
398 char str[NAME_MAX];
Gioh Kim8e864992021-03-25 16:32:59 +0100399 int err;
400 struct rtrs_addr path = {
401 .src = &sess->s.src_addr,
402 .dst = &sess->s.dst_addr,
403 };
Jack Wang215378b2020-05-11 15:51:14 +0200404
Gioh Kim8e864992021-03-25 16:32:59 +0100405 rtrs_addr_to_str(&path, str, sizeof(str));
Jack Wang215378b2020-05-11 15:51:14 +0200406 err = kobject_init_and_add(&sess->kobj, &ktype_sess, clt->kobj_paths,
407 "%s", str);
408 if (err) {
409 pr_err("kobject_init_and_add: %d\n", err);
Guoqing Jiang424774c2020-12-17 15:19:03 +0100410 kobject_put(&sess->kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200411 return err;
412 }
413 err = sysfs_create_group(&sess->kobj, &rtrs_clt_sess_attr_group);
414 if (err) {
415 pr_err("sysfs_create_group(): %d\n", err);
416 goto put_kobj;
417 }
418 err = kobject_init_and_add(&sess->stats->kobj_stats, &ktype_stats,
419 &sess->kobj, "stats");
420 if (err) {
421 pr_err("kobject_init_and_add: %d\n", err);
Guoqing Jiang424774c2020-12-17 15:19:03 +0100422 kobject_put(&sess->stats->kobj_stats);
Jack Wang215378b2020-05-11 15:51:14 +0200423 goto remove_group;
424 }
425
426 err = sysfs_create_group(&sess->stats->kobj_stats,
427 &rtrs_clt_stats_attr_group);
428 if (err) {
429 pr_err("failed to create stats sysfs group, err: %d\n", err);
430 goto put_kobj_stats;
431 }
432
433 return 0;
434
435put_kobj_stats:
436 kobject_del(&sess->stats->kobj_stats);
437 kobject_put(&sess->stats->kobj_stats);
438remove_group:
439 sysfs_remove_group(&sess->kobj, &rtrs_clt_sess_attr_group);
440put_kobj:
441 kobject_del(&sess->kobj);
442 kobject_put(&sess->kobj);
443
444 return err;
445}
446
447void rtrs_clt_destroy_sess_files(struct rtrs_clt_sess *sess,
448 const struct attribute *sysfs_self)
449{
450 kobject_del(&sess->stats->kobj_stats);
451 kobject_put(&sess->stats->kobj_stats);
452 if (sysfs_self)
453 sysfs_remove_file_self(&sess->kobj, sysfs_self);
454 kobject_del(&sess->kobj);
455}
456
457static struct attribute *rtrs_clt_attrs[] = {
458 &dev_attr_max_reconnect_attempts.attr,
459 &dev_attr_mpath_policy.attr,
460 &dev_attr_add_path.attr,
461 NULL,
462};
463
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200464static const struct attribute_group rtrs_clt_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200465 .attrs = rtrs_clt_attrs,
466};
467
468int rtrs_clt_create_sysfs_root_files(struct rtrs_clt *clt)
469{
470 return sysfs_create_group(&clt->dev.kobj, &rtrs_clt_attr_group);
471}
472
Guoqing Jiang7b47b272020-12-17 15:19:04 +0100473void rtrs_clt_destroy_sysfs_root(struct rtrs_clt *clt)
Jack Wang215378b2020-05-11 15:51:14 +0200474{
Guoqing Jiang7b47b272020-12-17 15:19:04 +0100475 sysfs_remove_group(&clt->dev.kobj, &rtrs_clt_attr_group);
476
Jack Wang215378b2020-05-11 15:51:14 +0200477 if (clt->kobj_paths) {
478 kobject_del(clt->kobj_paths);
479 kobject_put(clt->kobj_paths);
480 }
481}