blob: b4fa473b788855c0fa30829182a51dc082971bab [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
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010019static void rtrs_clt_path_release(struct kobject *kobj)
Jack Wang215378b2020-05-11 15:51:14 +020020{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010021 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +020022
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010023 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
Jack Wang215378b2020-05-11 15:51:14 +020024
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010025 free_path(clt_path);
Jack Wang215378b2020-05-11 15:51:14 +020026}
27
28static struct kobj_type ktype_sess = {
29 .sysfs_ops = &kobj_sysfs_ops,
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010030 .release = rtrs_clt_path_release
Jack Wang215378b2020-05-11 15:51:14 +020031};
32
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010033static void rtrs_clt_path_stats_release(struct kobject *kobj)
Jack Wang215378b2020-05-11 15:51:14 +020034{
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,
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +010046 .release = rtrs_clt_path_stats_release,
Jack Wang215378b2020-05-11 15:51:14 +020047};
48
49static ssize_t max_reconnect_attempts_show(struct device *dev,
50 struct device_attribute *attr,
51 char *page)
52{
Vaishali Thakkarf3433d72022-01-05 19:07:08 +010053 struct rtrs_clt_sess *clt = container_of(dev, struct rtrs_clt_sess,
54 dev);
Jack Wang215378b2020-05-11 15:51:14 +020055
Joe Perches1c7fd722020-10-07 19:36:24 -070056 return sysfs_emit(page, "%d\n",
57 rtrs_clt_get_max_reconnect_attempts(clt));
Jack Wang215378b2020-05-11 15:51:14 +020058}
59
60static ssize_t max_reconnect_attempts_store(struct device *dev,
61 struct device_attribute *attr,
62 const char *buf,
63 size_t count)
64{
65 int value;
66 int ret;
Vaishali Thakkarf3433d72022-01-05 19:07:08 +010067 struct rtrs_clt_sess *clt = container_of(dev, struct rtrs_clt_sess,
68 dev);
Jack Wang215378b2020-05-11 15:51:14 +020069
70 ret = kstrtoint(buf, 10, &value);
71 if (ret) {
72 rtrs_err(clt, "%s: failed to convert string '%s' to int\n",
73 attr->attr.name, buf);
74 return ret;
75 }
76 if (value > MAX_MAX_RECONN_ATT ||
77 value < MIN_MAX_RECONN_ATT) {
78 rtrs_err(clt,
79 "%s: invalid range (provided: '%s', accepted: min: %d, max: %d)\n",
80 attr->attr.name, buf, MIN_MAX_RECONN_ATT,
81 MAX_MAX_RECONN_ATT);
82 return -EINVAL;
83 }
84 rtrs_clt_set_max_reconnect_attempts(clt, value);
85
86 return count;
87}
88
89static DEVICE_ATTR_RW(max_reconnect_attempts);
90
91static ssize_t mpath_policy_show(struct device *dev,
92 struct device_attribute *attr,
93 char *page)
94{
Vaishali Thakkarf3433d72022-01-05 19:07:08 +010095 struct rtrs_clt_sess *clt;
Jack Wang215378b2020-05-11 15:51:14 +020096
Vaishali Thakkarf3433d72022-01-05 19:07:08 +010097 clt = container_of(dev, struct rtrs_clt_sess, dev);
Jack Wang215378b2020-05-11 15:51:14 +020098
99 switch (clt->mp_policy) {
100 case MP_POLICY_RR:
Joe Perches1c7fd722020-10-07 19:36:24 -0700101 return sysfs_emit(page, "round-robin (RR: %d)\n",
102 clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200103 case MP_POLICY_MIN_INFLIGHT:
Joe Perches1c7fd722020-10-07 19:36:24 -0700104 return sysfs_emit(page, "min-inflight (MI: %d)\n",
105 clt->mp_policy);
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200106 case MP_POLICY_MIN_LATENCY:
107 return sysfs_emit(page, "min-latency (ML: %d)\n",
108 clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200109 default:
Joe Perches1c7fd722020-10-07 19:36:24 -0700110 return sysfs_emit(page, "Unknown (%d)\n", clt->mp_policy);
Jack Wang215378b2020-05-11 15:51:14 +0200111 }
112}
113
114static ssize_t mpath_policy_store(struct device *dev,
115 struct device_attribute *attr,
116 const char *buf,
117 size_t count)
118{
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100119 struct rtrs_clt_sess *clt;
Jack Wang215378b2020-05-11 15:51:14 +0200120 int value;
121 int ret;
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200122 size_t len = 0;
Jack Wang215378b2020-05-11 15:51:14 +0200123
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100124 clt = container_of(dev, struct rtrs_clt_sess, dev);
Jack Wang215378b2020-05-11 15:51:14 +0200125
126 ret = kstrtoint(buf, 10, &value);
127 if (!ret && (value == MP_POLICY_RR ||
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200128 value == MP_POLICY_MIN_INFLIGHT ||
129 value == MP_POLICY_MIN_LATENCY)) {
Jack Wang215378b2020-05-11 15:51:14 +0200130 clt->mp_policy = value;
131 return count;
132 }
133
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200134 /* distinguish "mi" and "min-latency" with length */
135 len = strnlen(buf, NAME_MAX);
136 if (buf[len - 1] == '\n')
137 len--;
138
Jack Wang215378b2020-05-11 15:51:14 +0200139 if (!strncasecmp(buf, "round-robin", 11) ||
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200140 (len == 2 && !strncasecmp(buf, "rr", 2)))
Jack Wang215378b2020-05-11 15:51:14 +0200141 clt->mp_policy = MP_POLICY_RR;
142 else if (!strncasecmp(buf, "min-inflight", 12) ||
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200143 (len == 2 && !strncasecmp(buf, "mi", 2)))
Jack Wang215378b2020-05-11 15:51:14 +0200144 clt->mp_policy = MP_POLICY_MIN_INFLIGHT;
Gioh Kimdc3b66a2021-04-07 13:34:41 +0200145 else if (!strncasecmp(buf, "min-latency", 11) ||
146 (len == 2 && !strncasecmp(buf, "ml", 2)))
147 clt->mp_policy = MP_POLICY_MIN_LATENCY;
Jack Wang215378b2020-05-11 15:51:14 +0200148 else
149 return -EINVAL;
150
151 return count;
152}
153
154static DEVICE_ATTR_RW(mpath_policy);
155
156static ssize_t add_path_show(struct device *dev,
157 struct device_attribute *attr, char *page)
158{
Joe Perches1c7fd722020-10-07 19:36:24 -0700159 return sysfs_emit(
160 page,
161 "Usage: echo [<source addr>@]<destination addr> > %s\n\n*addr ::= [ ip:<ipv4|ipv6> | gid:<gid> ]\n",
162 attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200163}
164
165static ssize_t add_path_store(struct device *dev,
166 struct device_attribute *attr,
167 const char *buf, size_t count)
168{
169 struct sockaddr_storage srcaddr, dstaddr;
170 struct rtrs_addr addr = {
171 .src = &srcaddr,
172 .dst = &dstaddr
173 };
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100174 struct rtrs_clt_sess *clt;
Jack Wang215378b2020-05-11 15:51:14 +0200175 const char *nl;
176 size_t len;
177 int err;
178
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100179 clt = container_of(dev, struct rtrs_clt_sess, dev);
Jack Wang215378b2020-05-11 15:51:14 +0200180
181 nl = strchr(buf, '\n');
182 if (nl)
183 len = nl - buf;
184 else
185 len = count;
186 err = rtrs_addr_to_sockaddr(buf, len, clt->port, &addr);
187 if (err)
188 return -EINVAL;
189
190 err = rtrs_clt_create_path_from_sysfs(clt, &addr);
191 if (err)
192 return err;
193
194 return count;
195}
196
197static DEVICE_ATTR_RW(add_path);
198
199static ssize_t rtrs_clt_state_show(struct kobject *kobj,
200 struct kobj_attribute *attr, char *page)
201{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100202 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +0200203
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100204 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
205 if (clt_path->state == RTRS_CLT_CONNECTED)
Joe Perches3c6bff32020-10-07 19:36:25 -0700206 return sysfs_emit(page, "connected\n");
Jack Wang215378b2020-05-11 15:51:14 +0200207
Joe Perches3c6bff32020-10-07 19:36:25 -0700208 return sysfs_emit(page, "disconnected\n");
Jack Wang215378b2020-05-11 15:51:14 +0200209}
210
211static struct kobj_attribute rtrs_clt_state_attr =
212 __ATTR(state, 0444, rtrs_clt_state_show, NULL);
213
214static ssize_t rtrs_clt_reconnect_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700215 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200216{
Joe Perches3c6bff32020-10-07 19:36:25 -0700217 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200218}
219
220static ssize_t rtrs_clt_reconnect_store(struct kobject *kobj,
221 struct kobj_attribute *attr,
222 const char *buf, size_t count)
223{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100224 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +0200225 int ret;
226
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100227 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200228 if (!sysfs_streq(buf, "1")) {
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100229 rtrs_err(clt_path->clt, "%s: unknown value: '%s'\n",
Jack Wang215378b2020-05-11 15:51:14 +0200230 attr->attr.name, buf);
231 return -EINVAL;
232 }
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100233 ret = rtrs_clt_reconnect_from_sysfs(clt_path);
Jack Wang215378b2020-05-11 15:51:14 +0200234 if (ret)
235 return ret;
236
237 return count;
238}
239
240static struct kobj_attribute rtrs_clt_reconnect_attr =
241 __ATTR(reconnect, 0644, rtrs_clt_reconnect_show,
242 rtrs_clt_reconnect_store);
243
244static ssize_t rtrs_clt_disconnect_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700245 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200246{
Joe Perches3c6bff32020-10-07 19:36:25 -0700247 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200248}
249
250static ssize_t rtrs_clt_disconnect_store(struct kobject *kobj,
251 struct kobj_attribute *attr,
252 const char *buf, size_t count)
253{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100254 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +0200255
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100256 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200257 if (!sysfs_streq(buf, "1")) {
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100258 rtrs_err(clt_path->clt, "%s: unknown value: '%s'\n",
Jack Wang215378b2020-05-11 15:51:14 +0200259 attr->attr.name, buf);
260 return -EINVAL;
261 }
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100262 rtrs_clt_close_conns(clt_path, true);
Jack Wang215378b2020-05-11 15:51:14 +0200263
264 return count;
265}
266
267static struct kobj_attribute rtrs_clt_disconnect_attr =
268 __ATTR(disconnect, 0644, rtrs_clt_disconnect_show,
269 rtrs_clt_disconnect_store);
270
271static ssize_t rtrs_clt_remove_path_show(struct kobject *kobj,
Joe Perches3c6bff32020-10-07 19:36:25 -0700272 struct kobj_attribute *attr, char *buf)
Jack Wang215378b2020-05-11 15:51:14 +0200273{
Joe Perches3c6bff32020-10-07 19:36:25 -0700274 return sysfs_emit(buf, "Usage: echo 1 > %s\n", attr->attr.name);
Jack Wang215378b2020-05-11 15:51:14 +0200275}
276
277static ssize_t rtrs_clt_remove_path_store(struct kobject *kobj,
278 struct kobj_attribute *attr,
279 const char *buf, size_t count)
280{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100281 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +0200282 int ret;
283
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100284 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200285 if (!sysfs_streq(buf, "1")) {
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100286 rtrs_err(clt_path->clt, "%s: unknown value: '%s'\n",
Jack Wang215378b2020-05-11 15:51:14 +0200287 attr->attr.name, buf);
288 return -EINVAL;
289 }
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100290 ret = rtrs_clt_remove_path_from_sysfs(clt_path, &attr->attr);
Jack Wang215378b2020-05-11 15:51:14 +0200291 if (ret)
292 return ret;
293
294 return count;
295}
296
297static struct kobj_attribute rtrs_clt_remove_path_attr =
298 __ATTR(remove_path, 0644, rtrs_clt_remove_path_show,
299 rtrs_clt_remove_path_store);
300
Md Haris Iqbal3f3fe682021-09-22 14:53:33 +0200301STAT_ATTR(struct rtrs_clt_stats, cpu_migration_from,
302 rtrs_clt_stats_migration_from_cnt_to_str,
303 rtrs_clt_reset_cpu_migr_stats);
304
305STAT_ATTR(struct rtrs_clt_stats, cpu_migration_to,
306 rtrs_clt_stats_migration_to_cnt_to_str,
Jack Wang215378b2020-05-11 15:51:14 +0200307 rtrs_clt_reset_cpu_migr_stats);
308
309STAT_ATTR(struct rtrs_clt_stats, reconnects,
310 rtrs_clt_stats_reconnects_to_str,
311 rtrs_clt_reset_reconnects_stat);
312
313STAT_ATTR(struct rtrs_clt_stats, rdma,
314 rtrs_clt_stats_rdma_to_str,
315 rtrs_clt_reset_rdma_stats);
316
317STAT_ATTR(struct rtrs_clt_stats, reset_all,
318 rtrs_clt_reset_all_help,
319 rtrs_clt_reset_all_stats);
320
321static struct attribute *rtrs_clt_stats_attrs[] = {
Md Haris Iqbal3f3fe682021-09-22 14:53:33 +0200322 &cpu_migration_from_attr.attr,
323 &cpu_migration_to_attr.attr,
Jack Wang215378b2020-05-11 15:51:14 +0200324 &reconnects_attr.attr,
325 &rdma_attr.attr,
326 &reset_all_attr.attr,
327 NULL
328};
329
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200330static const struct attribute_group rtrs_clt_stats_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200331 .attrs = rtrs_clt_stats_attrs,
332};
333
334static ssize_t rtrs_clt_hca_port_show(struct kobject *kobj,
335 struct kobj_attribute *attr,
336 char *page)
337{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100338 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +0200339
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100340 clt_path = container_of(kobj, typeof(*clt_path), kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200341
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100342 return sysfs_emit(page, "%u\n", clt_path->hca_port);
Jack Wang215378b2020-05-11 15:51:14 +0200343}
344
345static struct kobj_attribute rtrs_clt_hca_port_attr =
346 __ATTR(hca_port, 0444, rtrs_clt_hca_port_show, NULL);
347
348static ssize_t rtrs_clt_hca_name_show(struct kobject *kobj,
349 struct kobj_attribute *attr,
350 char *page)
351{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100352 struct rtrs_clt_path *clt_path;
Jack Wang215378b2020-05-11 15:51:14 +0200353
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100354 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200355
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100356 return sysfs_emit(page, "%s\n", clt_path->hca_name);
Jack Wang215378b2020-05-11 15:51:14 +0200357}
358
359static struct kobj_attribute rtrs_clt_hca_name_attr =
360 __ATTR(hca_name, 0444, rtrs_clt_hca_name_show, NULL);
361
Gioh Kimcc853922021-04-07 13:34:42 +0200362static ssize_t rtrs_clt_cur_latency_show(struct kobject *kobj,
363 struct kobj_attribute *attr,
364 char *page)
365{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100366 struct rtrs_clt_path *clt_path;
Gioh Kimcc853922021-04-07 13:34:42 +0200367
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100368 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
Gioh Kimcc853922021-04-07 13:34:42 +0200369
370 return sysfs_emit(page, "%lld ns\n",
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100371 ktime_to_ns(clt_path->s.hb_cur_latency));
Gioh Kimcc853922021-04-07 13:34:42 +0200372}
373
374static struct kobj_attribute rtrs_clt_cur_latency_attr =
375 __ATTR(cur_latency, 0444, rtrs_clt_cur_latency_show, NULL);
376
Jack Wang215378b2020-05-11 15:51:14 +0200377static ssize_t rtrs_clt_src_addr_show(struct kobject *kobj,
378 struct kobj_attribute *attr,
379 char *page)
380{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100381 struct rtrs_clt_path *clt_path;
Joe Perchese28bf1f2020-10-07 19:36:27 -0700382 int len;
Jack Wang215378b2020-05-11 15:51:14 +0200383
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100384 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
385 len = sockaddr_to_str((struct sockaddr *)&clt_path->s.src_addr, page,
Joe Perchese28bf1f2020-10-07 19:36:27 -0700386 PAGE_SIZE);
387 len += sysfs_emit_at(page, len, "\n");
388 return len;
Jack Wang215378b2020-05-11 15:51:14 +0200389}
390
391static struct kobj_attribute rtrs_clt_src_addr_attr =
392 __ATTR(src_addr, 0444, rtrs_clt_src_addr_show, NULL);
393
394static ssize_t rtrs_clt_dst_addr_show(struct kobject *kobj,
395 struct kobj_attribute *attr,
396 char *page)
397{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100398 struct rtrs_clt_path *clt_path;
Joe Perchese28bf1f2020-10-07 19:36:27 -0700399 int len;
Jack Wang215378b2020-05-11 15:51:14 +0200400
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100401 clt_path = container_of(kobj, struct rtrs_clt_path, kobj);
402 len = sockaddr_to_str((struct sockaddr *)&clt_path->s.dst_addr, page,
Joe Perchese28bf1f2020-10-07 19:36:27 -0700403 PAGE_SIZE);
404 len += sysfs_emit_at(page, len, "\n");
405 return len;
Jack Wang215378b2020-05-11 15:51:14 +0200406}
407
408static struct kobj_attribute rtrs_clt_dst_addr_attr =
409 __ATTR(dst_addr, 0444, rtrs_clt_dst_addr_show, NULL);
410
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100411static struct attribute *rtrs_clt_path_attrs[] = {
Jack Wang215378b2020-05-11 15:51:14 +0200412 &rtrs_clt_hca_name_attr.attr,
413 &rtrs_clt_hca_port_attr.attr,
414 &rtrs_clt_src_addr_attr.attr,
415 &rtrs_clt_dst_addr_attr.attr,
416 &rtrs_clt_state_attr.attr,
417 &rtrs_clt_reconnect_attr.attr,
418 &rtrs_clt_disconnect_attr.attr,
419 &rtrs_clt_remove_path_attr.attr,
Gioh Kimcc853922021-04-07 13:34:42 +0200420 &rtrs_clt_cur_latency_attr.attr,
Jack Wang215378b2020-05-11 15:51:14 +0200421 NULL,
422};
423
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100424static const struct attribute_group rtrs_clt_path_attr_group = {
425 .attrs = rtrs_clt_path_attrs,
Jack Wang215378b2020-05-11 15:51:14 +0200426};
427
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100428int rtrs_clt_create_path_files(struct rtrs_clt_path *clt_path)
Jack Wang215378b2020-05-11 15:51:14 +0200429{
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100430 struct rtrs_clt_sess *clt = clt_path->clt;
Jack Wang215378b2020-05-11 15:51:14 +0200431 char str[NAME_MAX];
Gioh Kim8e864992021-03-25 16:32:59 +0100432 int err;
433 struct rtrs_addr path = {
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100434 .src = &clt_path->s.src_addr,
435 .dst = &clt_path->s.dst_addr,
Gioh Kim8e864992021-03-25 16:32:59 +0100436 };
Jack Wang215378b2020-05-11 15:51:14 +0200437
Gioh Kim8e864992021-03-25 16:32:59 +0100438 rtrs_addr_to_str(&path, str, sizeof(str));
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100439 err = kobject_init_and_add(&clt_path->kobj, &ktype_sess,
440 clt->kobj_paths,
Jack Wang215378b2020-05-11 15:51:14 +0200441 "%s", str);
442 if (err) {
443 pr_err("kobject_init_and_add: %d\n", err);
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100444 kobject_put(&clt_path->kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200445 return err;
446 }
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100447 err = sysfs_create_group(&clt_path->kobj, &rtrs_clt_path_attr_group);
Jack Wang215378b2020-05-11 15:51:14 +0200448 if (err) {
449 pr_err("sysfs_create_group(): %d\n", err);
450 goto put_kobj;
451 }
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100452 err = kobject_init_and_add(&clt_path->stats->kobj_stats, &ktype_stats,
453 &clt_path->kobj, "stats");
Jack Wang215378b2020-05-11 15:51:14 +0200454 if (err) {
455 pr_err("kobject_init_and_add: %d\n", err);
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100456 kobject_put(&clt_path->stats->kobj_stats);
Jack Wang215378b2020-05-11 15:51:14 +0200457 goto remove_group;
458 }
459
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100460 err = sysfs_create_group(&clt_path->stats->kobj_stats,
Jack Wang215378b2020-05-11 15:51:14 +0200461 &rtrs_clt_stats_attr_group);
462 if (err) {
463 pr_err("failed to create stats sysfs group, err: %d\n", err);
464 goto put_kobj_stats;
465 }
466
467 return 0;
468
469put_kobj_stats:
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100470 kobject_del(&clt_path->stats->kobj_stats);
471 kobject_put(&clt_path->stats->kobj_stats);
Jack Wang215378b2020-05-11 15:51:14 +0200472remove_group:
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100473 sysfs_remove_group(&clt_path->kobj, &rtrs_clt_path_attr_group);
Jack Wang215378b2020-05-11 15:51:14 +0200474put_kobj:
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100475 kobject_del(&clt_path->kobj);
476 kobject_put(&clt_path->kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200477
478 return err;
479}
480
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100481void rtrs_clt_destroy_path_files(struct rtrs_clt_path *clt_path,
Jack Wang215378b2020-05-11 15:51:14 +0200482 const struct attribute *sysfs_self)
483{
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100484 kobject_del(&clt_path->stats->kobj_stats);
485 kobject_put(&clt_path->stats->kobj_stats);
Jack Wang215378b2020-05-11 15:51:14 +0200486 if (sysfs_self)
Vaishali Thakkarcaa84d92022-01-05 19:07:06 +0100487 sysfs_remove_file_self(&clt_path->kobj, sysfs_self);
488 kobject_del(&clt_path->kobj);
Jack Wang215378b2020-05-11 15:51:14 +0200489}
490
491static struct attribute *rtrs_clt_attrs[] = {
492 &dev_attr_max_reconnect_attempts.attr,
493 &dev_attr_mpath_policy.attr,
494 &dev_attr_add_path.attr,
495 NULL,
496};
497
Rikard Falkeborn3c4e9192020-10-01 00:40:04 +0200498static const struct attribute_group rtrs_clt_attr_group = {
Jack Wang215378b2020-05-11 15:51:14 +0200499 .attrs = rtrs_clt_attrs,
500};
501
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100502int rtrs_clt_create_sysfs_root_files(struct rtrs_clt_sess *clt)
Jack Wang215378b2020-05-11 15:51:14 +0200503{
504 return sysfs_create_group(&clt->dev.kobj, &rtrs_clt_attr_group);
505}
506
Vaishali Thakkarf3433d72022-01-05 19:07:08 +0100507void rtrs_clt_destroy_sysfs_root(struct rtrs_clt_sess *clt)
Jack Wang215378b2020-05-11 15:51:14 +0200508{
Guoqing Jiang7b47b272020-12-17 15:19:04 +0100509 sysfs_remove_group(&clt->dev.kobj, &rtrs_clt_attr_group);
510
Jack Wang215378b2020-05-11 15:51:14 +0200511 if (clt->kobj_paths) {
512 kobject_del(clt->kobj_paths);
513 kobject_put(clt->kobj_paths);
514 }
515}