xref: /openbmc/linux/drivers/net/bonding/bond_sysfs_slave.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
207699f9aSsfeldma@cumulusnetworks.com /*	Sysfs attributes of bond slaves
307699f9aSsfeldma@cumulusnetworks.com  *
407699f9aSsfeldma@cumulusnetworks.com  *      Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com>
507699f9aSsfeldma@cumulusnetworks.com  */
607699f9aSsfeldma@cumulusnetworks.com 
707699f9aSsfeldma@cumulusnetworks.com #include <linux/capability.h>
807699f9aSsfeldma@cumulusnetworks.com #include <linux/kernel.h>
907699f9aSsfeldma@cumulusnetworks.com #include <linux/netdevice.h>
1007699f9aSsfeldma@cumulusnetworks.com 
111ef8019bSDavid S. Miller #include <net/bonding.h>
1207699f9aSsfeldma@cumulusnetworks.com 
1307699f9aSsfeldma@cumulusnetworks.com struct slave_attribute {
1407699f9aSsfeldma@cumulusnetworks.com 	struct attribute attr;
1507699f9aSsfeldma@cumulusnetworks.com 	ssize_t (*show)(struct slave *, char *);
1607699f9aSsfeldma@cumulusnetworks.com };
1707699f9aSsfeldma@cumulusnetworks.com 
1807699f9aSsfeldma@cumulusnetworks.com #define SLAVE_ATTR_RO(_name)					\
19669b258aSLianjie Zhang const struct slave_attribute slave_attr_##_name = __ATTR_RO(_name)
2007699f9aSsfeldma@cumulusnetworks.com 
state_show(struct slave * slave,char * buf)2107699f9aSsfeldma@cumulusnetworks.com static ssize_t state_show(struct slave *slave, char *buf)
2207699f9aSsfeldma@cumulusnetworks.com {
2307699f9aSsfeldma@cumulusnetworks.com 	switch (bond_slave_state(slave)) {
2407699f9aSsfeldma@cumulusnetworks.com 	case BOND_STATE_ACTIVE:
25*96e07181SWang Yufen 		return sysfs_emit(buf, "active\n");
2607699f9aSsfeldma@cumulusnetworks.com 	case BOND_STATE_BACKUP:
27*96e07181SWang Yufen 		return sysfs_emit(buf, "backup\n");
2807699f9aSsfeldma@cumulusnetworks.com 	default:
29*96e07181SWang Yufen 		return sysfs_emit(buf, "UNKNOWN\n");
3007699f9aSsfeldma@cumulusnetworks.com 	}
3107699f9aSsfeldma@cumulusnetworks.com }
3207699f9aSsfeldma@cumulusnetworks.com static SLAVE_ATTR_RO(state);
3307699f9aSsfeldma@cumulusnetworks.com 
mii_status_show(struct slave * slave,char * buf)3407699f9aSsfeldma@cumulusnetworks.com static ssize_t mii_status_show(struct slave *slave, char *buf)
3507699f9aSsfeldma@cumulusnetworks.com {
36*96e07181SWang Yufen 	return sysfs_emit(buf, "%s\n", bond_slave_link_status(slave->link));
3707699f9aSsfeldma@cumulusnetworks.com }
3807699f9aSsfeldma@cumulusnetworks.com static SLAVE_ATTR_RO(mii_status);
3907699f9aSsfeldma@cumulusnetworks.com 
link_failure_count_show(struct slave * slave,char * buf)4007699f9aSsfeldma@cumulusnetworks.com static ssize_t link_failure_count_show(struct slave *slave, char *buf)
4107699f9aSsfeldma@cumulusnetworks.com {
42*96e07181SWang Yufen 	return sysfs_emit(buf, "%d\n", slave->link_failure_count);
4307699f9aSsfeldma@cumulusnetworks.com }
4407699f9aSsfeldma@cumulusnetworks.com static SLAVE_ATTR_RO(link_failure_count);
4507699f9aSsfeldma@cumulusnetworks.com 
perm_hwaddr_show(struct slave * slave,char * buf)4607699f9aSsfeldma@cumulusnetworks.com static ssize_t perm_hwaddr_show(struct slave *slave, char *buf)
4707699f9aSsfeldma@cumulusnetworks.com {
48*96e07181SWang Yufen 	return sysfs_emit(buf, "%*phC\n",
4918bebc6dSKonstantin Khorenko 		       slave->dev->addr_len,
5018bebc6dSKonstantin Khorenko 		       slave->perm_hwaddr);
5107699f9aSsfeldma@cumulusnetworks.com }
5207699f9aSsfeldma@cumulusnetworks.com static SLAVE_ATTR_RO(perm_hwaddr);
5307699f9aSsfeldma@cumulusnetworks.com 
queue_id_show(struct slave * slave,char * buf)5407699f9aSsfeldma@cumulusnetworks.com static ssize_t queue_id_show(struct slave *slave, char *buf)
5507699f9aSsfeldma@cumulusnetworks.com {
56*96e07181SWang Yufen 	return sysfs_emit(buf, "%d\n", slave->queue_id);
5707699f9aSsfeldma@cumulusnetworks.com }
5807699f9aSsfeldma@cumulusnetworks.com static SLAVE_ATTR_RO(queue_id);
5907699f9aSsfeldma@cumulusnetworks.com 
ad_aggregator_id_show(struct slave * slave,char * buf)6007699f9aSsfeldma@cumulusnetworks.com static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf)
6107699f9aSsfeldma@cumulusnetworks.com {
6207699f9aSsfeldma@cumulusnetworks.com 	const struct aggregator *agg;
6307699f9aSsfeldma@cumulusnetworks.com 
6401844098SVeaceslav Falico 	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
653fdddd85Sdingtianhong 		agg = SLAVE_AD_INFO(slave)->port.aggregator;
6607699f9aSsfeldma@cumulusnetworks.com 		if (agg)
67*96e07181SWang Yufen 			return sysfs_emit(buf, "%d\n",
6807699f9aSsfeldma@cumulusnetworks.com 					  agg->aggregator_identifier);
6907699f9aSsfeldma@cumulusnetworks.com 	}
7007699f9aSsfeldma@cumulusnetworks.com 
71*96e07181SWang Yufen 	return sysfs_emit(buf, "N/A\n");
7207699f9aSsfeldma@cumulusnetworks.com }
7307699f9aSsfeldma@cumulusnetworks.com static SLAVE_ATTR_RO(ad_aggregator_id);
7407699f9aSsfeldma@cumulusnetworks.com 
ad_actor_oper_port_state_show(struct slave * slave,char * buf)75254cb6dbSNikolay Aleksandrov static ssize_t ad_actor_oper_port_state_show(struct slave *slave, char *buf)
76254cb6dbSNikolay Aleksandrov {
77254cb6dbSNikolay Aleksandrov 	const struct port *ad_port;
78254cb6dbSNikolay Aleksandrov 
79254cb6dbSNikolay Aleksandrov 	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
80254cb6dbSNikolay Aleksandrov 		ad_port = &SLAVE_AD_INFO(slave)->port;
81254cb6dbSNikolay Aleksandrov 		if (ad_port->aggregator)
82*96e07181SWang Yufen 			return sysfs_emit(buf, "%u\n",
83254cb6dbSNikolay Aleksandrov 				       ad_port->actor_oper_port_state);
84254cb6dbSNikolay Aleksandrov 	}
85254cb6dbSNikolay Aleksandrov 
86*96e07181SWang Yufen 	return sysfs_emit(buf, "N/A\n");
87254cb6dbSNikolay Aleksandrov }
88254cb6dbSNikolay Aleksandrov static SLAVE_ATTR_RO(ad_actor_oper_port_state);
89254cb6dbSNikolay Aleksandrov 
ad_partner_oper_port_state_show(struct slave * slave,char * buf)9046ea297eSNikolay Aleksandrov static ssize_t ad_partner_oper_port_state_show(struct slave *slave, char *buf)
9146ea297eSNikolay Aleksandrov {
9246ea297eSNikolay Aleksandrov 	const struct port *ad_port;
9346ea297eSNikolay Aleksandrov 
9446ea297eSNikolay Aleksandrov 	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
9546ea297eSNikolay Aleksandrov 		ad_port = &SLAVE_AD_INFO(slave)->port;
9646ea297eSNikolay Aleksandrov 		if (ad_port->aggregator)
97*96e07181SWang Yufen 			return sysfs_emit(buf, "%u\n",
9846ea297eSNikolay Aleksandrov 				       ad_port->partner_oper.port_state);
9946ea297eSNikolay Aleksandrov 	}
10046ea297eSNikolay Aleksandrov 
101*96e07181SWang Yufen 	return sysfs_emit(buf, "N/A\n");
10246ea297eSNikolay Aleksandrov }
10346ea297eSNikolay Aleksandrov static SLAVE_ATTR_RO(ad_partner_oper_port_state);
10446ea297eSNikolay Aleksandrov 
105b93c6a91SHuang Guobin static const struct attribute *slave_attrs[] = {
106b93c6a91SHuang Guobin 	&slave_attr_state.attr,
107b93c6a91SHuang Guobin 	&slave_attr_mii_status.attr,
108b93c6a91SHuang Guobin 	&slave_attr_link_failure_count.attr,
109b93c6a91SHuang Guobin 	&slave_attr_perm_hwaddr.attr,
110b93c6a91SHuang Guobin 	&slave_attr_queue_id.attr,
111b93c6a91SHuang Guobin 	&slave_attr_ad_aggregator_id.attr,
112b93c6a91SHuang Guobin 	&slave_attr_ad_actor_oper_port_state.attr,
113b93c6a91SHuang Guobin 	&slave_attr_ad_partner_oper_port_state.attr,
11407699f9aSsfeldma@cumulusnetworks.com 	NULL
11507699f9aSsfeldma@cumulusnetworks.com };
11607699f9aSsfeldma@cumulusnetworks.com 
11707699f9aSsfeldma@cumulusnetworks.com #define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
11807699f9aSsfeldma@cumulusnetworks.com 
slave_show(struct kobject * kobj,struct attribute * attr,char * buf)11907699f9aSsfeldma@cumulusnetworks.com static ssize_t slave_show(struct kobject *kobj,
12007699f9aSsfeldma@cumulusnetworks.com 			  struct attribute *attr, char *buf)
12107699f9aSsfeldma@cumulusnetworks.com {
12207699f9aSsfeldma@cumulusnetworks.com 	struct slave_attribute *slave_attr = to_slave_attr(attr);
12307699f9aSsfeldma@cumulusnetworks.com 	struct slave *slave = to_slave(kobj);
12407699f9aSsfeldma@cumulusnetworks.com 
12507699f9aSsfeldma@cumulusnetworks.com 	return slave_attr->show(slave, buf);
12607699f9aSsfeldma@cumulusnetworks.com }
12707699f9aSsfeldma@cumulusnetworks.com 
128b9ad3e9fSJamie Iles const struct sysfs_ops slave_sysfs_ops = {
12907699f9aSsfeldma@cumulusnetworks.com 	.show = slave_show,
13007699f9aSsfeldma@cumulusnetworks.com };
13107699f9aSsfeldma@cumulusnetworks.com 
bond_sysfs_slave_add(struct slave * slave)13207699f9aSsfeldma@cumulusnetworks.com int bond_sysfs_slave_add(struct slave *slave)
13307699f9aSsfeldma@cumulusnetworks.com {
134b93c6a91SHuang Guobin 	return sysfs_create_files(&slave->kobj, slave_attrs);
13507699f9aSsfeldma@cumulusnetworks.com }
13607699f9aSsfeldma@cumulusnetworks.com 
bond_sysfs_slave_del(struct slave * slave)13707699f9aSsfeldma@cumulusnetworks.com void bond_sysfs_slave_del(struct slave *slave)
13807699f9aSsfeldma@cumulusnetworks.com {
139b93c6a91SHuang Guobin 	sysfs_remove_files(&slave->kobj, slave_attrs);
14007699f9aSsfeldma@cumulusnetworks.com }
141