1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Sysfs attributes of bond slaves
4 * Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com>
7 #include <linux/capability.h>
8 #include <linux/kernel.h>
9 #include <linux/netdevice.h>
11 #include <net/bonding.h>
13 struct slave_attribute
{
14 struct attribute attr
;
15 ssize_t (*show
)(struct slave
*, char *);
18 #define SLAVE_ATTR(_name, _mode, _show) \
19 const struct slave_attribute slave_attr_##_name = { \
20 .attr = {.name = __stringify(_name), \
24 #define SLAVE_ATTR_RO(_name) \
25 SLAVE_ATTR(_name, 0444, _name##_show)
27 static ssize_t
state_show(struct slave
*slave
, char *buf
)
29 switch (bond_slave_state(slave
)) {
30 case BOND_STATE_ACTIVE
:
31 return sprintf(buf
, "active\n");
32 case BOND_STATE_BACKUP
:
33 return sprintf(buf
, "backup\n");
35 return sprintf(buf
, "UNKNOWN\n");
38 static SLAVE_ATTR_RO(state
);
40 static ssize_t
mii_status_show(struct slave
*slave
, char *buf
)
42 return sprintf(buf
, "%s\n", bond_slave_link_status(slave
->link
));
44 static SLAVE_ATTR_RO(mii_status
);
46 static ssize_t
link_failure_count_show(struct slave
*slave
, char *buf
)
48 return sprintf(buf
, "%d\n", slave
->link_failure_count
);
50 static SLAVE_ATTR_RO(link_failure_count
);
52 static ssize_t
perm_hwaddr_show(struct slave
*slave
, char *buf
)
54 return sprintf(buf
, "%*phC\n",
58 static SLAVE_ATTR_RO(perm_hwaddr
);
60 static ssize_t
queue_id_show(struct slave
*slave
, char *buf
)
62 return sprintf(buf
, "%d\n", slave
->queue_id
);
64 static SLAVE_ATTR_RO(queue_id
);
66 static ssize_t
ad_aggregator_id_show(struct slave
*slave
, char *buf
)
68 const struct aggregator
*agg
;
70 if (BOND_MODE(slave
->bond
) == BOND_MODE_8023AD
) {
71 agg
= SLAVE_AD_INFO(slave
)->port
.aggregator
;
73 return sprintf(buf
, "%d\n",
74 agg
->aggregator_identifier
);
77 return sprintf(buf
, "N/A\n");
79 static SLAVE_ATTR_RO(ad_aggregator_id
);
81 static ssize_t
ad_actor_oper_port_state_show(struct slave
*slave
, char *buf
)
83 const struct port
*ad_port
;
85 if (BOND_MODE(slave
->bond
) == BOND_MODE_8023AD
) {
86 ad_port
= &SLAVE_AD_INFO(slave
)->port
;
87 if (ad_port
->aggregator
)
88 return sprintf(buf
, "%u\n",
89 ad_port
->actor_oper_port_state
);
92 return sprintf(buf
, "N/A\n");
94 static SLAVE_ATTR_RO(ad_actor_oper_port_state
);
96 static ssize_t
ad_partner_oper_port_state_show(struct slave
*slave
, char *buf
)
98 const struct port
*ad_port
;
100 if (BOND_MODE(slave
->bond
) == BOND_MODE_8023AD
) {
101 ad_port
= &SLAVE_AD_INFO(slave
)->port
;
102 if (ad_port
->aggregator
)
103 return sprintf(buf
, "%u\n",
104 ad_port
->partner_oper
.port_state
);
107 return sprintf(buf
, "N/A\n");
109 static SLAVE_ATTR_RO(ad_partner_oper_port_state
);
111 static const struct slave_attribute
*slave_attrs
[] = {
113 &slave_attr_mii_status
,
114 &slave_attr_link_failure_count
,
115 &slave_attr_perm_hwaddr
,
116 &slave_attr_queue_id
,
117 &slave_attr_ad_aggregator_id
,
118 &slave_attr_ad_actor_oper_port_state
,
119 &slave_attr_ad_partner_oper_port_state
,
123 #define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
124 #define to_slave(obj) container_of(obj, struct slave, kobj)
126 static ssize_t
slave_show(struct kobject
*kobj
,
127 struct attribute
*attr
, char *buf
)
129 struct slave_attribute
*slave_attr
= to_slave_attr(attr
);
130 struct slave
*slave
= to_slave(kobj
);
132 return slave_attr
->show(slave
, buf
);
135 static const struct sysfs_ops slave_sysfs_ops
= {
139 static struct kobj_type slave_ktype
= {
141 .sysfs_ops
= &slave_sysfs_ops
,
145 int bond_sysfs_slave_add(struct slave
*slave
)
147 const struct slave_attribute
**a
;
150 err
= kobject_init_and_add(&slave
->kobj
, &slave_ktype
,
151 &(slave
->dev
->dev
.kobj
), "bonding_slave");
155 for (a
= slave_attrs
; *a
; ++a
) {
156 err
= sysfs_create_file(&slave
->kobj
, &((*a
)->attr
));
158 kobject_put(&slave
->kobj
);
166 void bond_sysfs_slave_del(struct slave
*slave
)
168 const struct slave_attribute
**a
;
170 for (a
= slave_attrs
; *a
; ++a
)
171 sysfs_remove_file(&slave
->kobj
, &((*a
)->attr
));
173 kobject_put(&slave
->kobj
);