1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Sysfs attributes of bond slaves 3 * 4 * Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com> 5 */ 6 7 #include <linux/capability.h> 8 #include <linux/kernel.h> 9 #include <linux/netdevice.h> 10 11 #include <net/bonding.h> 12 13 struct slave_attribute { 14 struct attribute attr; 15 ssize_t (*show)(struct slave *, char *); 16 }; 17 18 #define SLAVE_ATTR_RO(_name) \ 19 const struct slave_attribute slave_attr_##_name = __ATTR_RO(_name) 20 21 static ssize_t state_show(struct slave *slave, char *buf) 22 { 23 switch (bond_slave_state(slave)) { 24 case BOND_STATE_ACTIVE: 25 return sysfs_emit(buf, "active\n"); 26 case BOND_STATE_BACKUP: 27 return sysfs_emit(buf, "backup\n"); 28 default: 29 return sysfs_emit(buf, "UNKNOWN\n"); 30 } 31 } 32 static SLAVE_ATTR_RO(state); 33 34 static ssize_t mii_status_show(struct slave *slave, char *buf) 35 { 36 return sysfs_emit(buf, "%s\n", bond_slave_link_status(slave->link)); 37 } 38 static SLAVE_ATTR_RO(mii_status); 39 40 static ssize_t link_failure_count_show(struct slave *slave, char *buf) 41 { 42 return sysfs_emit(buf, "%d\n", slave->link_failure_count); 43 } 44 static SLAVE_ATTR_RO(link_failure_count); 45 46 static ssize_t perm_hwaddr_show(struct slave *slave, char *buf) 47 { 48 return sysfs_emit(buf, "%*phC\n", 49 slave->dev->addr_len, 50 slave->perm_hwaddr); 51 } 52 static SLAVE_ATTR_RO(perm_hwaddr); 53 54 static ssize_t queue_id_show(struct slave *slave, char *buf) 55 { 56 return sysfs_emit(buf, "%d\n", READ_ONCE(slave->queue_id)); 57 } 58 static SLAVE_ATTR_RO(queue_id); 59 60 static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf) 61 { 62 const struct aggregator *agg; 63 64 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) { 65 rcu_read_lock(); 66 agg = rcu_dereference(SLAVE_AD_INFO(slave)->port.aggregator); 67 if (agg) { 68 ssize_t res = sysfs_emit(buf, "%d\n", 69 agg->aggregator_identifier); 70 rcu_read_unlock(); 71 return res; 72 } 73 rcu_read_unlock(); 74 } 75 76 return sysfs_emit(buf, "N/A\n"); 77 } 78 static SLAVE_ATTR_RO(ad_aggregator_id); 79 80 static ssize_t ad_actor_oper_port_state_show(struct slave *slave, char *buf) 81 { 82 const struct port *ad_port; 83 84 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) { 85 ad_port = &SLAVE_AD_INFO(slave)->port; 86 if (rcu_access_pointer(ad_port->aggregator)) 87 return sysfs_emit(buf, "%u\n", 88 ad_port->actor_oper_port_state); 89 } 90 91 return sysfs_emit(buf, "N/A\n"); 92 } 93 static SLAVE_ATTR_RO(ad_actor_oper_port_state); 94 95 static ssize_t ad_partner_oper_port_state_show(struct slave *slave, char *buf) 96 { 97 const struct port *ad_port; 98 99 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) { 100 ad_port = &SLAVE_AD_INFO(slave)->port; 101 if (rcu_access_pointer(ad_port->aggregator)) 102 return sysfs_emit(buf, "%u\n", 103 ad_port->partner_oper.port_state); 104 } 105 106 return sysfs_emit(buf, "N/A\n"); 107 } 108 static SLAVE_ATTR_RO(ad_partner_oper_port_state); 109 110 static const struct attribute *slave_attrs[] = { 111 &slave_attr_state.attr, 112 &slave_attr_mii_status.attr, 113 &slave_attr_link_failure_count.attr, 114 &slave_attr_perm_hwaddr.attr, 115 &slave_attr_queue_id.attr, 116 &slave_attr_ad_aggregator_id.attr, 117 &slave_attr_ad_actor_oper_port_state.attr, 118 &slave_attr_ad_partner_oper_port_state.attr, 119 NULL 120 }; 121 122 #define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr) 123 124 static ssize_t slave_show(struct kobject *kobj, 125 struct attribute *attr, char *buf) 126 { 127 struct slave_attribute *slave_attr = to_slave_attr(attr); 128 struct slave *slave = to_slave(kobj); 129 130 return slave_attr->show(slave, buf); 131 } 132 133 const struct sysfs_ops slave_sysfs_ops = { 134 .show = slave_show, 135 }; 136 137 int bond_sysfs_slave_add(struct slave *slave) 138 { 139 return sysfs_create_files(&slave->kobj, slave_attrs); 140 } 141 142 void bond_sysfs_slave_del(struct slave *slave) 143 { 144 sysfs_remove_files(&slave->kobj, slave_attrs); 145 } 146