2022-03-31 03:16:58

by chenchacha

[permalink] [raw]
Subject: [PATCH v2 3/4] ipmi: msghandler: Get the number of users and messages through sysfs

The administrator reads the user_count and msg_count file
corresponding the ipmi device, ipmi traverse all intf, and count
the number of users and messages.

Signed-off-by: Chen Guanqiao <[email protected]>
---
drivers/char/ipmi/ipmi_msghandler.c | 71 +++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)

diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c
index 80ab88702c5f..a188dc7e7135 100644
--- a/drivers/char/ipmi/ipmi_msghandler.c
+++ b/drivers/char/ipmi/ipmi_msghandler.c
@@ -922,6 +922,19 @@ unsigned int ipmi_addr_length(int addr_type)
}
EXPORT_SYMBOL(ipmi_addr_length);

+static unsigned int intf_user_count(struct ipmi_smi *intf)
+{
+ struct ipmi_user *user;
+ int index, count = 0;
+
+ index = srcu_read_lock(&intf->users_srcu);
+ list_for_each_entry_rcu(user, &intf->users, link)
+ count++;
+ srcu_read_unlock(&intf->users_srcu, index);
+
+ return count;
+}
+
static void intf_msg_count(struct ipmi_smi *intf,
unsigned int *hp_count, unsigned int *count)
{
@@ -2760,6 +2773,42 @@ static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc,
return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
}

+static unsigned int get_user_count(void)
+{
+ struct ipmi_smi *intf;
+ int index;
+ unsigned int count = 0;
+
+ index = srcu_read_lock(&ipmi_interfaces_srcu);
+ list_for_each_entry_rcu(intf, &ipmi_interfaces, link)
+ count += intf_user_count(intf);
+ srcu_read_unlock(&ipmi_interfaces_srcu, index);
+
+ return count;
+}
+
+static void get_msg_count(unsigned int *hp_count, unsigned int *count)
+{
+ struct ipmi_smi *intf;
+ int index;
+
+ *hp_count = 0;
+ *count = 0;
+
+ index = srcu_read_lock(&ipmi_interfaces_srcu);
+ list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
+ int hp_msg_count = 0, msg_count = 0;
+
+ rcu_read_lock();
+ intf_msg_count(intf, &hp_msg_count, &msg_count);
+ rcu_read_unlock();
+
+ *hp_count += hp_msg_count;
+ *count += msg_count;
+ }
+ srcu_read_unlock(&ipmi_interfaces_srcu, index);
+}
+
static ssize_t device_id_show(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -2929,6 +2978,26 @@ static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
}
static DEVICE_ATTR_RO(guid);

+static ssize_t user_count_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ unsigned int user_count = get_user_count();
+
+ return snprintf(buf, 20, "%u\n", user_count);
+}
+static DEVICE_ATTR_ADMIN_RO(user_count);
+
+static ssize_t msg_count_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ unsigned int hp_count, count;
+
+ get_msg_count(&hp_count, &count);
+
+ return snprintf(buf, 40, "hp msg:%u, msg:%u\n", hp_count, count);
+}
+static DEVICE_ATTR_ADMIN_RO(msg_count);
+
static struct attribute *bmc_dev_attrs[] = {
&dev_attr_device_id.attr,
&dev_attr_provides_device_sdrs.attr,
@@ -2940,6 +3009,8 @@ static struct attribute *bmc_dev_attrs[] = {
&dev_attr_product_id.attr,
&dev_attr_aux_firmware_revision.attr,
&dev_attr_guid.attr,
+ &dev_attr_user_count.attr,
+ &dev_attr_msg_count.attr,
NULL
};

--
2.25.1