1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
16 #define MIN_MAX_RECONN_ATT -1
17 #define MAX_MAX_RECONN_ATT 9999
19 static void rtrs_clt_path_release(struct kobject
*kobj
)
21 struct rtrs_clt_path
*clt_path
;
23 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
28 static struct kobj_type ktype_sess
= {
29 .sysfs_ops
= &kobj_sysfs_ops
,
30 .release
= rtrs_clt_path_release
33 static void rtrs_clt_path_stats_release(struct kobject
*kobj
)
35 struct rtrs_clt_stats
*stats
;
37 stats
= container_of(kobj
, struct rtrs_clt_stats
, kobj_stats
);
39 free_percpu(stats
->pcpu_stats
);
44 static struct kobj_type ktype_stats
= {
45 .sysfs_ops
= &kobj_sysfs_ops
,
46 .release
= rtrs_clt_path_stats_release
,
49 static ssize_t
max_reconnect_attempts_show(struct device
*dev
,
50 struct device_attribute
*attr
,
53 struct rtrs_clt_sess
*clt
= container_of(dev
, struct rtrs_clt_sess
,
56 return sysfs_emit(page
, "%d\n",
57 rtrs_clt_get_max_reconnect_attempts(clt
));
60 static ssize_t
max_reconnect_attempts_store(struct device
*dev
,
61 struct device_attribute
*attr
,
67 struct rtrs_clt_sess
*clt
= container_of(dev
, struct rtrs_clt_sess
,
70 ret
= kstrtoint(buf
, 10, &value
);
72 rtrs_err(clt
, "%s: failed to convert string '%s' to int\n",
73 attr
->attr
.name
, buf
);
76 if (value
> MAX_MAX_RECONN_ATT
||
77 value
< MIN_MAX_RECONN_ATT
) {
79 "%s: invalid range (provided: '%s', accepted: min: %d, max: %d)\n",
80 attr
->attr
.name
, buf
, MIN_MAX_RECONN_ATT
,
84 rtrs_clt_set_max_reconnect_attempts(clt
, value
);
89 static DEVICE_ATTR_RW(max_reconnect_attempts
);
91 static ssize_t
mpath_policy_show(struct device
*dev
,
92 struct device_attribute
*attr
,
95 struct rtrs_clt_sess
*clt
;
97 clt
= container_of(dev
, struct rtrs_clt_sess
, dev
);
99 switch (clt
->mp_policy
) {
101 return sysfs_emit(page
, "round-robin (RR: %d)\n",
103 case MP_POLICY_MIN_INFLIGHT
:
104 return sysfs_emit(page
, "min-inflight (MI: %d)\n",
106 case MP_POLICY_MIN_LATENCY
:
107 return sysfs_emit(page
, "min-latency (ML: %d)\n",
110 return sysfs_emit(page
, "Unknown (%d)\n", clt
->mp_policy
);
114 static ssize_t
mpath_policy_store(struct device
*dev
,
115 struct device_attribute
*attr
,
119 struct rtrs_clt_sess
*clt
;
124 clt
= container_of(dev
, struct rtrs_clt_sess
, dev
);
126 ret
= kstrtoint(buf
, 10, &value
);
127 if (!ret
&& (value
== MP_POLICY_RR
||
128 value
== MP_POLICY_MIN_INFLIGHT
||
129 value
== MP_POLICY_MIN_LATENCY
)) {
130 clt
->mp_policy
= value
;
134 /* distinguish "mi" and "min-latency" with length */
135 len
= strnlen(buf
, NAME_MAX
);
136 if (len
&& buf
[len
- 1] == '\n')
139 if (!strncasecmp(buf
, "round-robin", 11) ||
140 (len
== 2 && !strncasecmp(buf
, "rr", 2)))
141 clt
->mp_policy
= MP_POLICY_RR
;
142 else if (!strncasecmp(buf
, "min-inflight", 12) ||
143 (len
== 2 && !strncasecmp(buf
, "mi", 2)))
144 clt
->mp_policy
= MP_POLICY_MIN_INFLIGHT
;
145 else if (!strncasecmp(buf
, "min-latency", 11) ||
146 (len
== 2 && !strncasecmp(buf
, "ml", 2)))
147 clt
->mp_policy
= MP_POLICY_MIN_LATENCY
;
154 static DEVICE_ATTR_RW(mpath_policy
);
156 static ssize_t
add_path_show(struct device
*dev
,
157 struct device_attribute
*attr
, char *page
)
159 return sysfs_emit(page
,
160 "Usage: echo [<source addr>@]<destination addr> > %s\n\n*addr ::= [ ip:<ipv4|ipv6> | gid:<gid> ]\n",
164 static ssize_t
add_path_store(struct device
*dev
,
165 struct device_attribute
*attr
,
166 const char *buf
, size_t count
)
168 struct sockaddr_storage srcaddr
, dstaddr
;
169 struct rtrs_addr addr
= {
173 struct rtrs_clt_sess
*clt
;
178 clt
= container_of(dev
, struct rtrs_clt_sess
, dev
);
180 nl
= strchr(buf
, '\n');
185 err
= rtrs_addr_to_sockaddr(buf
, len
, clt
->port
, &addr
);
189 err
= rtrs_clt_create_path_from_sysfs(clt
, &addr
);
196 static DEVICE_ATTR_RW(add_path
);
198 static ssize_t
rtrs_clt_state_show(struct kobject
*kobj
,
199 struct kobj_attribute
*attr
, char *page
)
201 struct rtrs_clt_path
*clt_path
;
203 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
204 if (clt_path
->state
== RTRS_CLT_CONNECTED
)
205 return sysfs_emit(page
, "connected\n");
207 return sysfs_emit(page
, "disconnected\n");
210 static struct kobj_attribute rtrs_clt_state_attr
=
211 __ATTR(state
, 0444, rtrs_clt_state_show
, NULL
);
213 static ssize_t
rtrs_clt_reconnect_show(struct kobject
*kobj
,
214 struct kobj_attribute
*attr
, char *buf
)
216 return sysfs_emit(buf
, "Usage: echo 1 > %s\n", attr
->attr
.name
);
219 static ssize_t
rtrs_clt_reconnect_store(struct kobject
*kobj
,
220 struct kobj_attribute
*attr
,
221 const char *buf
, size_t count
)
223 struct rtrs_clt_path
*clt_path
;
226 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
227 if (!sysfs_streq(buf
, "1")) {
228 rtrs_err(clt_path
->clt
, "%s: unknown value: '%s'\n",
229 attr
->attr
.name
, buf
);
232 ret
= rtrs_clt_reconnect_from_sysfs(clt_path
);
239 static struct kobj_attribute rtrs_clt_reconnect_attr
=
240 __ATTR(reconnect
, 0644, rtrs_clt_reconnect_show
,
241 rtrs_clt_reconnect_store
);
243 static ssize_t
rtrs_clt_disconnect_show(struct kobject
*kobj
,
244 struct kobj_attribute
*attr
, char *buf
)
246 return sysfs_emit(buf
, "Usage: echo 1 > %s\n", attr
->attr
.name
);
249 static ssize_t
rtrs_clt_disconnect_store(struct kobject
*kobj
,
250 struct kobj_attribute
*attr
,
251 const char *buf
, size_t count
)
253 struct rtrs_clt_path
*clt_path
;
255 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
256 if (!sysfs_streq(buf
, "1")) {
257 rtrs_err(clt_path
->clt
, "%s: unknown value: '%s'\n",
258 attr
->attr
.name
, buf
);
261 rtrs_clt_close_conns(clt_path
, true);
266 static struct kobj_attribute rtrs_clt_disconnect_attr
=
267 __ATTR(disconnect
, 0644, rtrs_clt_disconnect_show
,
268 rtrs_clt_disconnect_store
);
270 static ssize_t
rtrs_clt_remove_path_show(struct kobject
*kobj
,
271 struct kobj_attribute
*attr
, char *buf
)
273 return sysfs_emit(buf
, "Usage: echo 1 > %s\n", attr
->attr
.name
);
276 static ssize_t
rtrs_clt_remove_path_store(struct kobject
*kobj
,
277 struct kobj_attribute
*attr
,
278 const char *buf
, size_t count
)
280 struct rtrs_clt_path
*clt_path
;
283 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
284 if (!sysfs_streq(buf
, "1")) {
285 rtrs_err(clt_path
->clt
, "%s: unknown value: '%s'\n",
286 attr
->attr
.name
, buf
);
289 ret
= rtrs_clt_remove_path_from_sysfs(clt_path
, &attr
->attr
);
296 static struct kobj_attribute rtrs_clt_remove_path_attr
=
297 __ATTR(remove_path
, 0644, rtrs_clt_remove_path_show
,
298 rtrs_clt_remove_path_store
);
300 STAT_ATTR(struct rtrs_clt_stats
, cpu_migration_from
,
301 rtrs_clt_stats_migration_from_cnt_to_str
,
302 rtrs_clt_reset_cpu_migr_stats
);
304 STAT_ATTR(struct rtrs_clt_stats
, cpu_migration_to
,
305 rtrs_clt_stats_migration_to_cnt_to_str
,
306 rtrs_clt_reset_cpu_migr_stats
);
308 STAT_ATTR(struct rtrs_clt_stats
, reconnects
,
309 rtrs_clt_stats_reconnects_to_str
,
310 rtrs_clt_reset_reconnects_stat
);
312 STAT_ATTR(struct rtrs_clt_stats
, rdma
,
313 rtrs_clt_stats_rdma_to_str
,
314 rtrs_clt_reset_rdma_stats
);
316 STAT_ATTR(struct rtrs_clt_stats
, reset_all
,
317 rtrs_clt_reset_all_help
,
318 rtrs_clt_reset_all_stats
);
320 static struct attribute
*rtrs_clt_stats_attrs
[] = {
321 &cpu_migration_from_attr
.attr
,
322 &cpu_migration_to_attr
.attr
,
323 &reconnects_attr
.attr
,
325 &reset_all_attr
.attr
,
329 static const struct attribute_group rtrs_clt_stats_attr_group
= {
330 .attrs
= rtrs_clt_stats_attrs
,
333 static ssize_t
rtrs_clt_hca_port_show(struct kobject
*kobj
,
334 struct kobj_attribute
*attr
,
337 struct rtrs_clt_path
*clt_path
;
339 clt_path
= container_of(kobj
, typeof(*clt_path
), kobj
);
341 return sysfs_emit(page
, "%u\n", clt_path
->hca_port
);
344 static struct kobj_attribute rtrs_clt_hca_port_attr
=
345 __ATTR(hca_port
, 0444, rtrs_clt_hca_port_show
, NULL
);
347 static ssize_t
rtrs_clt_hca_name_show(struct kobject
*kobj
,
348 struct kobj_attribute
*attr
,
351 struct rtrs_clt_path
*clt_path
;
353 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
355 return sysfs_emit(page
, "%s\n", clt_path
->hca_name
);
358 static struct kobj_attribute rtrs_clt_hca_name_attr
=
359 __ATTR(hca_name
, 0444, rtrs_clt_hca_name_show
, NULL
);
361 static ssize_t
rtrs_clt_cur_latency_show(struct kobject
*kobj
,
362 struct kobj_attribute
*attr
,
365 struct rtrs_clt_path
*clt_path
;
367 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
369 return sysfs_emit(page
, "%lld ns\n",
370 ktime_to_ns(clt_path
->s
.hb_cur_latency
));
373 static struct kobj_attribute rtrs_clt_cur_latency_attr
=
374 __ATTR(cur_latency
, 0444, rtrs_clt_cur_latency_show
, NULL
);
376 static ssize_t
rtrs_clt_src_addr_show(struct kobject
*kobj
,
377 struct kobj_attribute
*attr
,
380 struct rtrs_clt_path
*clt_path
;
383 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
384 len
= sockaddr_to_str((struct sockaddr
*)&clt_path
->s
.src_addr
, page
,
386 len
+= sysfs_emit_at(page
, len
, "\n");
390 static struct kobj_attribute rtrs_clt_src_addr_attr
=
391 __ATTR(src_addr
, 0444, rtrs_clt_src_addr_show
, NULL
);
393 static ssize_t
rtrs_clt_dst_addr_show(struct kobject
*kobj
,
394 struct kobj_attribute
*attr
,
397 struct rtrs_clt_path
*clt_path
;
400 clt_path
= container_of(kobj
, struct rtrs_clt_path
, kobj
);
401 len
= sockaddr_to_str((struct sockaddr
*)&clt_path
->s
.dst_addr
, page
,
403 len
+= sysfs_emit_at(page
, len
, "\n");
407 static struct kobj_attribute rtrs_clt_dst_addr_attr
=
408 __ATTR(dst_addr
, 0444, rtrs_clt_dst_addr_show
, NULL
);
410 static struct attribute
*rtrs_clt_path_attrs
[] = {
411 &rtrs_clt_hca_name_attr
.attr
,
412 &rtrs_clt_hca_port_attr
.attr
,
413 &rtrs_clt_src_addr_attr
.attr
,
414 &rtrs_clt_dst_addr_attr
.attr
,
415 &rtrs_clt_state_attr
.attr
,
416 &rtrs_clt_reconnect_attr
.attr
,
417 &rtrs_clt_disconnect_attr
.attr
,
418 &rtrs_clt_remove_path_attr
.attr
,
419 &rtrs_clt_cur_latency_attr
.attr
,
423 static const struct attribute_group rtrs_clt_path_attr_group
= {
424 .attrs
= rtrs_clt_path_attrs
,
427 int rtrs_clt_create_path_files(struct rtrs_clt_path
*clt_path
)
429 struct rtrs_clt_sess
*clt
= clt_path
->clt
;
432 struct rtrs_addr path
= {
433 .src
= &clt_path
->s
.src_addr
,
434 .dst
= &clt_path
->s
.dst_addr
,
437 rtrs_addr_to_str(&path
, str
, sizeof(str
));
438 err
= kobject_init_and_add(&clt_path
->kobj
, &ktype_sess
,
442 pr_err("kobject_init_and_add: %d\n", err
);
443 kobject_put(&clt_path
->kobj
);
446 err
= sysfs_create_group(&clt_path
->kobj
, &rtrs_clt_path_attr_group
);
448 pr_err("sysfs_create_group(): %d\n", err
);
451 err
= kobject_init_and_add(&clt_path
->stats
->kobj_stats
, &ktype_stats
,
452 &clt_path
->kobj
, "stats");
454 pr_err("kobject_init_and_add: %d\n", err
);
455 kobject_put(&clt_path
->stats
->kobj_stats
);
459 err
= sysfs_create_group(&clt_path
->stats
->kobj_stats
,
460 &rtrs_clt_stats_attr_group
);
462 pr_err("failed to create stats sysfs group, err: %d\n", err
);
469 kobject_del(&clt_path
->stats
->kobj_stats
);
470 kobject_put(&clt_path
->stats
->kobj_stats
);
472 sysfs_remove_group(&clt_path
->kobj
, &rtrs_clt_path_attr_group
);
474 kobject_del(&clt_path
->kobj
);
475 kobject_put(&clt_path
->kobj
);
480 void rtrs_clt_destroy_path_files(struct rtrs_clt_path
*clt_path
,
481 const struct attribute
*sysfs_self
)
483 kobject_del(&clt_path
->stats
->kobj_stats
);
484 kobject_put(&clt_path
->stats
->kobj_stats
);
486 sysfs_remove_file_self(&clt_path
->kobj
, sysfs_self
);
487 kobject_del(&clt_path
->kobj
);
490 static struct attribute
*rtrs_clt_attrs
[] = {
491 &dev_attr_max_reconnect_attempts
.attr
,
492 &dev_attr_mpath_policy
.attr
,
493 &dev_attr_add_path
.attr
,
497 static const struct attribute_group rtrs_clt_attr_group
= {
498 .attrs
= rtrs_clt_attrs
,
501 int rtrs_clt_create_sysfs_root_files(struct rtrs_clt_sess
*clt
)
503 return sysfs_create_group(&clt
->dev
.kobj
, &rtrs_clt_attr_group
);
506 void rtrs_clt_destroy_sysfs_root(struct rtrs_clt_sess
*clt
)
508 sysfs_remove_group(&clt
->dev
.kobj
, &rtrs_clt_attr_group
);
510 if (clt
->kobj_paths
) {
511 kobject_del(clt
->kobj_paths
);
512 kobject_put(clt
->kobj_paths
);