Whamcloud - gitweb
LU-9667 lnet: Generic helper functions for sysfs
authorSonia Sharma <sonia.sharma@intel.com>
Thu, 21 Sep 2017 17:21:50 +0000 (10:21 -0700)
committerSonia Sharma <sharmaso@whamcloud.com>
Wed, 29 May 2019 07:08:15 +0000 (03:08 -0400)
Adding generic helper functions for the sysfs
callback functions to use when implemented.
Add the base directory "lnet" to create the
path /sys/fs/lnet/

sysfs show and store callback functions for
lnet and lnd to use the helper functions.

Test-Parameters: trivial
Change-Id: I250eb0e3f3398f0882c2ab66dd4bf7f56871711b
Signed-off-by: Sonia Sharma <sonia.sharma@intel.com>
Reviewed-on: https://review.whamcloud.com/29163
Reviewed-by: James Simmons <uja.ornl@yahoo.com>
Reviewed-by: Amir Shehata <ashehata@whamcloud.com>
Tested-by: Jenkins
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Sonia Sharma <sharmaso@whamcloud.com>
lnet/include/lnet/Makefile.am
lnet/include/lnet/lnet-sysfs.h [new file with mode: 0644]
lnet/lnet/Makefile.in
lnet/lnet/lnet-sysfs.c [new file with mode: 0644]
lnet/lnet/module.c

index 923074e..e9f5fd0 100644 (file)
@@ -2,4 +2,5 @@ EXTRA_DIST = \
        api.h \
        lib-lnet.h \
        lib-types.h \
+       lnet-sysfs.h \
        socklnd.h
diff --git a/lnet/include/lnet/lnet-sysfs.h b/lnet/include/lnet/lnet-sysfs.h
new file mode 100644 (file)
index 0000000..a47c09b
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2017, Intel Corporation.
+ *
+ * Author:
+ *   Sonia Sharma <sonia.sharma@intel.com>
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lnet/include/lnet/lnet-sysfs.h
+ *
+ */
+
+#ifndef __LNET_LNET_SYSFS_H__
+#define __LNET_LNET_SYSFS_H__
+
+#ifndef __KERNEL__
+# error This include is only for kernel use.
+#endif
+
+#include <libcfs/libcfs.h>
+#include <lnet/api.h>
+#include <lnet/lib-types.h>
+#include <uapi/linux/lnet/lnet-dlc.h>
+#include <uapi/linux/lnet/lnet-types.h>
+
+
+extern struct kobject *lnet_kobj;               /* Sysfs lnet kobject */
+
+struct lnet_attr {
+       struct attribute attr;
+       ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
+                       char *buf);
+       ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
+                        const char *buf, size_t len);
+};
+
+#define LNET_ATTR(name, mode, show, store) \
+static struct lnet_attr lnet_attr_##name = __ATTR(name, mode, show, store)
+
+#define LNET_RO_ATTR(name) LNET_ATTR(name, 0444, name##_show, NULL)
+#define LNET_RW_ATTR(name) LNET_ATTR(name, 0644, name##_show, name##_store)
+
+ssize_t lnet_attr_show(struct kobject *kobj, struct attribute *attr,
+                      char *buf);
+
+ssize_t lnet_attr_store(struct kobject *kobj, struct attribute *attr,
+                       const char *buf, size_t len);
+
+extern const struct sysfs_ops lnet_sysfs_ops;
+
+#endif
index ee119ad..5c62479 100644 (file)
@@ -1,6 +1,6 @@
 MODULES := lnet
 
-lnet-objs := api-ni.o config.o nidstrings.o
+lnet-objs := api-ni.o config.o nidstrings.o lnet-sysfs.o
 lnet-objs += lib-me.o lib-msg.o lib-eq.o lib-md.o lib-ptl.o
 lnet-objs += lib-socket.o lib-move.o module.o lo.o
 lnet-objs += router.o router_proc.o acceptor.o peer.o net_fault.o
diff --git a/lnet/lnet/lnet-sysfs.c b/lnet/lnet/lnet-sysfs.c
new file mode 100644 (file)
index 0000000..0f2bab9
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2017, Intel Corporation.
+ *
+ * Author:
+ *   Sonia Sharma <sonia.sharma@intel.com>
+ */
+
+
+#include <lnet/lib-lnet.h>
+#include <lnet/lnet-sysfs.h>
+
+struct kobject *lnet_kobj;
+EXPORT_SYMBOL_GPL(lnet_kobj);
+
+/*
+ * Helper functions for sysfs callbacks implementation
+ */
+ssize_t lnet_attr_show(struct kobject *kobj,
+                      struct attribute *attr, char *buf)
+{
+       struct lnet_attr *a = container_of(attr, struct lnet_attr, attr);
+
+       return a->show ? a->show(kobj, attr, buf) : 0;
+}
+EXPORT_SYMBOL_GPL(lnet_attr_show);
+
+ssize_t lnet_attr_store(struct kobject *kobj, struct attribute *attr,
+                         const char *buf, size_t len)
+{
+       struct lnet_attr *a = container_of(attr, struct lnet_attr, attr);
+
+       return a->store ? a->store(kobj, attr, buf, len) : len;
+}
+EXPORT_SYMBOL_GPL(lnet_attr_store);
+
+const struct sysfs_ops lnet_sysfs_ops = {
+       .show  = lnet_attr_show,
+       .store = lnet_attr_store,
+};
+EXPORT_SYMBOL_GPL(lnet_sysfs_ops);
index 676f734..bfa6e12 100644 (file)
@@ -33,6 +33,7 @@
 #define DEBUG_SUBSYSTEM S_LNET
 
 #include <lnet/lib-lnet.h>
+#include <lnet/lnet-sysfs.h>
 #include <uapi/linux/lnet/lnet-dlc.h>
 
 static int config_on_load = 0;
@@ -237,9 +238,17 @@ static int __init lnet_init(void)
 
        mutex_init(&lnet_config_mutex);
 
+       /* Create sysfs base directories for LNet statistics */
+       lnet_kobj = kobject_create_and_add("lnet", fs_kobj);
+       if (!lnet_kobj) {
+               CERROR("Sysfs kobject for lnet could not be created\n");
+               RETURN(-ENOMEM);
+       }
+
        rc = lnet_lib_init();
        if (rc != 0) {
                CERROR("lnet_lib_init: error %d\n", rc);
+               kobject_put(lnet_kobj);
                RETURN(rc);
        }
 
@@ -265,6 +274,9 @@ static void __exit lnet_exit(void)
        LASSERT(rc == 0);
 
        lnet_lib_exit();
+
+       /* cleanup the base sysfs directories */
+       kobject_put(lnet_kobj);
 }
 
 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");