Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v3.15
 
  1/*	Sysfs attributes of bond slaves
  2 *
  3 *      Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com>
  4 *
  5 *	This program is free software; you can redistribute it and/or
  6 *	modify it under the terms of the GNU General Public License
  7 *	as published by the Free Software Foundation; either version
  8 *	2 of the License, or (at your option) any later version.
  9 */
 10
 11#include <linux/capability.h>
 12#include <linux/kernel.h>
 13#include <linux/netdevice.h>
 14
 15#include "bonding.h"
 16
 17struct slave_attribute {
 18	struct attribute attr;
 19	ssize_t (*show)(struct slave *, char *);
 20};
 21
 22#define SLAVE_ATTR(_name, _mode, _show)				\
 23const struct slave_attribute slave_attr_##_name = {		\
 24	.attr = {.name = __stringify(_name),			\
 25		 .mode = _mode },				\
 26	.show	= _show,					\
 27};
 28#define SLAVE_ATTR_RO(_name) \
 29	SLAVE_ATTR(_name, S_IRUGO, _name##_show)
 30
 31static ssize_t state_show(struct slave *slave, char *buf)
 32{
 33	switch (bond_slave_state(slave)) {
 34	case BOND_STATE_ACTIVE:
 35		return sprintf(buf, "active\n");
 36	case BOND_STATE_BACKUP:
 37		return sprintf(buf, "backup\n");
 38	default:
 39		return sprintf(buf, "UNKONWN\n");
 40	}
 41}
 42static SLAVE_ATTR_RO(state);
 43
 44static ssize_t mii_status_show(struct slave *slave, char *buf)
 45{
 46	return sprintf(buf, "%s\n", bond_slave_link_status(slave->link));
 47}
 48static SLAVE_ATTR_RO(mii_status);
 49
 50static ssize_t link_failure_count_show(struct slave *slave, char *buf)
 51{
 52	return sprintf(buf, "%d\n", slave->link_failure_count);
 53}
 54static SLAVE_ATTR_RO(link_failure_count);
 55
 56static ssize_t perm_hwaddr_show(struct slave *slave, char *buf)
 57{
 58	return sprintf(buf, "%pM\n", slave->perm_hwaddr);
 
 
 59}
 60static SLAVE_ATTR_RO(perm_hwaddr);
 61
 62static ssize_t queue_id_show(struct slave *slave, char *buf)
 63{
 64	return sprintf(buf, "%d\n", slave->queue_id);
 65}
 66static SLAVE_ATTR_RO(queue_id);
 67
 68static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf)
 69{
 70	const struct aggregator *agg;
 71
 72	if (slave->bond->params.mode == BOND_MODE_8023AD) {
 73		agg = SLAVE_AD_INFO(slave).port.aggregator;
 74		if (agg)
 75			return sprintf(buf, "%d\n",
 76				       agg->aggregator_identifier);
 77	}
 78
 79	return sprintf(buf, "N/A\n");
 80}
 81static SLAVE_ATTR_RO(ad_aggregator_id);
 82
 83static const struct slave_attribute *slave_attrs[] = {
 84	&slave_attr_state,
 85	&slave_attr_mii_status,
 86	&slave_attr_link_failure_count,
 87	&slave_attr_perm_hwaddr,
 88	&slave_attr_queue_id,
 89	&slave_attr_ad_aggregator_id,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 90	NULL
 91};
 92
 93#define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
 94#define to_slave(obj)	container_of(obj, struct slave, kobj)
 95
 96static ssize_t slave_show(struct kobject *kobj,
 97			  struct attribute *attr, char *buf)
 98{
 99	struct slave_attribute *slave_attr = to_slave_attr(attr);
100	struct slave *slave = to_slave(kobj);
101
102	return slave_attr->show(slave, buf);
103}
104
105static const struct sysfs_ops slave_sysfs_ops = {
106	.show = slave_show,
107};
108
109static struct kobj_type slave_ktype = {
110#ifdef CONFIG_SYSFS
111	.sysfs_ops = &slave_sysfs_ops,
112#endif
113};
114
115int bond_sysfs_slave_add(struct slave *slave)
116{
117	const struct slave_attribute **a;
118	int err;
119
120	err = kobject_init_and_add(&slave->kobj, &slave_ktype,
121				   &(slave->dev->dev.kobj), "bonding_slave");
122	if (err)
123		return err;
124
125	for (a = slave_attrs; *a; ++a) {
126		err = sysfs_create_file(&slave->kobj, &((*a)->attr));
127		if (err) {
128			kobject_del(&slave->kobj);
129			return err;
130		}
131	}
132
133	return 0;
134}
135
136void bond_sysfs_slave_del(struct slave *slave)
137{
138	const struct slave_attribute **a;
139
140	for (a = slave_attrs; *a; ++a)
141		sysfs_remove_file(&slave->kobj, &((*a)->attr));
142
143	kobject_del(&slave->kobj);
144}
v6.13.7
  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
 13struct slave_attribute {
 14	struct attribute attr;
 15	ssize_t (*show)(struct slave *, char *);
 16};
 17
 18#define SLAVE_ATTR_RO(_name)					\
 19const struct slave_attribute slave_attr_##_name = __ATTR_RO(_name)
 
 
 
 
 
 
 20
 21static 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}
 32static SLAVE_ATTR_RO(state);
 33
 34static 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}
 38static SLAVE_ATTR_RO(mii_status);
 39
 40static ssize_t link_failure_count_show(struct slave *slave, char *buf)
 41{
 42	return sysfs_emit(buf, "%d\n", slave->link_failure_count);
 43}
 44static SLAVE_ATTR_RO(link_failure_count);
 45
 46static 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}
 52static SLAVE_ATTR_RO(perm_hwaddr);
 53
 54static ssize_t queue_id_show(struct slave *slave, char *buf)
 55{
 56	return sysfs_emit(buf, "%d\n", READ_ONCE(slave->queue_id));
 57}
 58static SLAVE_ATTR_RO(queue_id);
 59
 60static 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		agg = SLAVE_AD_INFO(slave)->port.aggregator;
 66		if (agg)
 67			return sysfs_emit(buf, "%d\n",
 68					  agg->aggregator_identifier);
 69	}
 70
 71	return sysfs_emit(buf, "N/A\n");
 72}
 73static SLAVE_ATTR_RO(ad_aggregator_id);
 74
 75static ssize_t ad_actor_oper_port_state_show(struct slave *slave, char *buf)
 76{
 77	const struct port *ad_port;
 78
 79	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
 80		ad_port = &SLAVE_AD_INFO(slave)->port;
 81		if (ad_port->aggregator)
 82			return sysfs_emit(buf, "%u\n",
 83				       ad_port->actor_oper_port_state);
 84	}
 85
 86	return sysfs_emit(buf, "N/A\n");
 87}
 88static SLAVE_ATTR_RO(ad_actor_oper_port_state);
 89
 90static ssize_t ad_partner_oper_port_state_show(struct slave *slave, char *buf)
 91{
 92	const struct port *ad_port;
 93
 94	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
 95		ad_port = &SLAVE_AD_INFO(slave)->port;
 96		if (ad_port->aggregator)
 97			return sysfs_emit(buf, "%u\n",
 98				       ad_port->partner_oper.port_state);
 99	}
100
101	return sysfs_emit(buf, "N/A\n");
102}
103static SLAVE_ATTR_RO(ad_partner_oper_port_state);
104
105static const struct attribute *slave_attrs[] = {
106	&slave_attr_state.attr,
107	&slave_attr_mii_status.attr,
108	&slave_attr_link_failure_count.attr,
109	&slave_attr_perm_hwaddr.attr,
110	&slave_attr_queue_id.attr,
111	&slave_attr_ad_aggregator_id.attr,
112	&slave_attr_ad_actor_oper_port_state.attr,
113	&slave_attr_ad_partner_oper_port_state.attr,
114	NULL
115};
116
117#define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
 
118
119static ssize_t slave_show(struct kobject *kobj,
120			  struct attribute *attr, char *buf)
121{
122	struct slave_attribute *slave_attr = to_slave_attr(attr);
123	struct slave *slave = to_slave(kobj);
124
125	return slave_attr->show(slave, buf);
126}
127
128const struct sysfs_ops slave_sysfs_ops = {
129	.show = slave_show,
130};
131
 
 
 
 
 
 
132int bond_sysfs_slave_add(struct slave *slave)
133{
134	return sysfs_create_files(&slave->kobj, slave_attrs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135}
136
137void bond_sysfs_slave_del(struct slave *slave)
138{
139	sysfs_remove_files(&slave->kobj, slave_attrs);
 
 
 
 
 
140}