From: Gioh Kim <gi-oh.kim@xxxxxxxxxxxxxxx> There are common code converting addresses of source machine and destination machine to one string. We already have a struct rtrs_addr to store two addresses. This patch introduces a new function that converts two addresses into one string with struct rtrs_addr. Signed-off-by: Gioh Kim <gi-oh.kim@xxxxxxxxx> Signed-off-by: Jack Wang <jinpu.wang@xxxxxxxxx> --- drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 13 +++++------ drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 13 +++++------ drivers/infiniband/ulp/rtrs/rtrs.c | 24 ++++++++++++++++++++ drivers/infiniband/ulp/rtrs/rtrs.h | 1 + 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c index d168bd08037a..c502dcbae9bb 100644 --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c @@ -396,14 +396,13 @@ int rtrs_clt_create_sess_files(struct rtrs_clt_sess *sess) { struct rtrs_clt *clt = sess->clt; char str[NAME_MAX]; - int err, cnt; - - cnt = sockaddr_to_str((struct sockaddr *)&sess->s.src_addr, - str, sizeof(str)); - cnt += scnprintf(str + cnt, sizeof(str) - cnt, "@"); - sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr, - str + cnt, sizeof(str) - cnt); + int err; + struct rtrs_addr path = { + .src = &sess->s.src_addr, + .dst = &sess->s.dst_addr, + }; + rtrs_addr_to_str(&path, str, sizeof(str)); err = kobject_init_and_add(&sess->kobj, &ktype_sess, clt->kobj_paths, "%s", str); if (err) { diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c index 309693434870..57af9e7c3588 100644 --- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c +++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c @@ -259,14 +259,13 @@ int rtrs_srv_create_sess_files(struct rtrs_srv_sess *sess) struct rtrs_srv *srv = sess->srv; struct rtrs_sess *s = &sess->s; char str[NAME_MAX]; - int err, cnt; - - cnt = sockaddr_to_str((struct sockaddr *)&sess->s.dst_addr, - str, sizeof(str)); - cnt += scnprintf(str + cnt, sizeof(str) - cnt, "@"); - sockaddr_to_str((struct sockaddr *)&sess->s.src_addr, - str + cnt, sizeof(str) - cnt); + int err; + struct rtrs_addr path = { + .src = &sess->s.dst_addr, + .dst = &sess->s.src_addr, + }; + rtrs_addr_to_str(&path, str, sizeof(str)); err = rtrs_srv_create_once_sysfs_root_folders(sess); if (err) return err; diff --git a/drivers/infiniband/ulp/rtrs/rtrs.c b/drivers/infiniband/ulp/rtrs/rtrs.c index 5822cb088eef..1dd772d84e71 100644 --- a/drivers/infiniband/ulp/rtrs/rtrs.c +++ b/drivers/infiniband/ulp/rtrs/rtrs.c @@ -463,6 +463,30 @@ int sockaddr_to_str(const struct sockaddr *addr, char *buf, size_t len) } EXPORT_SYMBOL(sockaddr_to_str); +/** + * rtrs_addr_to_str() - convert rtrs_addr to a string "src@dst" + * @addr: the rtrs_addr structure to be converted + * @str: string containing source and destination addr of a path + * separated by '@' I.e. "ip:1.1.1.1@ip:1.1.1.2" + * "ip:1.1.1.1@ip:1.1.1.2". + * @len: string length + * + * The return value is the number of characters written into buf not + * including the trailing '\0'. + */ +int rtrs_addr_to_str(const struct rtrs_addr *addr, char *buf, size_t len) +{ + int cnt; + + cnt = sockaddr_to_str((struct sockaddr *)addr->src, + buf, len); + cnt += scnprintf(buf + cnt, len - cnt, "@"); + sockaddr_to_str((struct sockaddr *)addr->dst, + buf + cnt, len - cnt); + return cnt; +} +EXPORT_SYMBOL(rtrs_addr_to_str); + /** * rtrs_addr_to_sockaddr() - convert path string "src,dst" or "src@dst" * to sockaddreses diff --git a/drivers/infiniband/ulp/rtrs/rtrs.h b/drivers/infiniband/ulp/rtrs/rtrs.h index a7e9ae579686..966c1e5ad68e 100644 --- a/drivers/infiniband/ulp/rtrs/rtrs.h +++ b/drivers/infiniband/ulp/rtrs/rtrs.h @@ -184,4 +184,5 @@ int rtrs_addr_to_sockaddr(const char *str, size_t len, u16 port, struct rtrs_addr *addr); int sockaddr_to_str(const struct sockaddr *addr, char *buf, size_t len); +int rtrs_addr_to_str(const struct rtrs_addr *addr, char *buf, size_t len); #endif -- 2.25.1