bond_sysfs.c 21.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
15
 * with this program; if not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 */
J
Joe Perches 已提交
21 22 23

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

24 25 26
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
27
#include <linux/sched.h>
28 29 30 31 32 33 34 35 36 37
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/in.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/inet.h>
#include <linux/rtnetlink.h>
38
#include <linux/etherdevice.h>
39
#include <net/net_namespace.h>
40 41
#include <net/netns/generic.h>
#include <linux/nsproxy.h>
42 43

#include "bonding.h"
44

S
Stephen Hemminger 已提交
45
#define to_dev(obj)	container_of(obj, struct device, kobj)
46
#define to_bond(cd)	((struct bonding *)(netdev_priv(to_net_dev(cd))))
47

48
/* "show" function for the bond_masters attribute.
49 50
 * The class parameter is ignored.
 */
51 52 53
static ssize_t bonding_show_bonds(struct class *cls,
				  struct class_attribute *attr,
				  char *buf)
54
{
55 56
	struct bond_net *bn =
		container_of(attr, struct bond_net, class_attr_bonding_masters);
57 58 59
	int res = 0;
	struct bonding *bond;

60
	rtnl_lock();
61

62
	list_for_each_entry(bond, &bn->dev_list, bond_list) {
63 64 65 66
		if (res > (PAGE_SIZE - IFNAMSIZ)) {
			/* not enough space for another interface name */
			if ((PAGE_SIZE - res) > 10)
				res = PAGE_SIZE - 10;
67
			res += sprintf(buf + res, "++more++ ");
68 69
			break;
		}
70
		res += sprintf(buf + res, "%s ", bond->dev->name);
71
	}
72 73
	if (res)
		buf[res-1] = '\n'; /* eat the leftover space */
74 75

	rtnl_unlock();
76 77 78
	return res;
}

79
static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
80 81 82
{
	struct bonding *bond;

83
	list_for_each_entry(bond, &bn->dev_list, bond_list) {
84 85 86 87 88 89
		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
			return bond->dev;
	}
	return NULL;
}

90
/* "store" function for the bond_masters attribute.  This is what
91 92 93 94
 * creates and deletes entire bonds.
 *
 * The class parameter is ignored.
 */
S
Stephen Hemminger 已提交
95
static ssize_t bonding_store_bonds(struct class *cls,
96
				   struct class_attribute *attr,
S
Stephen Hemminger 已提交
97
				   const char *buffer, size_t count)
98
{
99 100
	struct bond_net *bn =
		container_of(attr, struct bond_net, class_attr_bonding_masters);
101 102
	char command[IFNAMSIZ + 1] = {0, };
	char *ifname;
103
	int rv, res = count;
104 105 106 107 108 109 110 111

	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
	ifname = command + 1;
	if ((strlen(command) <= 1) ||
	    !dev_valid_name(ifname))
		goto err_no_cmd;

	if (command[0] == '+') {
J
Joe Perches 已提交
112
		pr_info("%s is being created...\n", ifname);
113
		rv = bond_create(bn->net, ifname);
114
		if (rv) {
115
			if (rv == -EEXIST)
J
Joe Perches 已提交
116
				pr_info("%s already exists\n", ifname);
117
			else
J
Joe Perches 已提交
118
				pr_info("%s creation failed\n", ifname);
119
			res = rv;
120
		}
121 122
	} else if (command[0] == '-') {
		struct net_device *bond_dev;
123

124
		rtnl_lock();
125
		bond_dev = bond_get_by_name(bn, ifname);
126
		if (bond_dev) {
J
Joe Perches 已提交
127
			pr_info("%s is being deleted...\n", ifname);
128 129
			unregister_netdevice(bond_dev);
		} else {
J
Joe Perches 已提交
130
			pr_err("unable to delete non-existent %s\n", ifname);
131 132 133 134 135
			res = -ENODEV;
		}
		rtnl_unlock();
	} else
		goto err_no_cmd;
136

137 138 139 140
	/* Always return either count or an error.  If you return 0, you'll
	 * get called forever, which is bad.
	 */
	return res;
141 142

err_no_cmd:
J
Joe Perches 已提交
143
	pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
144
	return -EPERM;
145
}
146

147
/* class attribute for bond_masters file.  This ends up in /sys/class/net */
148 149 150 151 152 153 154 155
static const struct class_attribute class_attr_bonding_masters = {
	.attr = {
		.name = "bonding_masters",
		.mode = S_IWUSR | S_IRUGO,
	},
	.show = bonding_show_bonds,
	.store = bonding_store_bonds,
};
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* Generic "store" method for bonding sysfs option setting */
static ssize_t bonding_sysfs_store_option(struct device *d,
					  struct device_attribute *attr,
					  const char *buffer, size_t count)
{
	struct bonding *bond = to_bond(d);
	const struct bond_option *opt;
	int ret;

	opt = bond_opt_get_by_name(attr->attr.name);
	if (WARN_ON(!opt))
		return -ENOENT;
	ret = bond_opt_tryset_rtnl(bond, opt->id, (char *)buffer);
	if (!ret)
		ret = count;

	return ret;
}

/* Show the slaves in the current bond. */
177 178
static ssize_t bonding_show_slaves(struct device *d,
				   struct device_attribute *attr, char *buf)
179
{
180
	struct bonding *bond = to_bond(d);
181
	struct list_head *iter;
182 183
	struct slave *slave;
	int res = 0;
184

185 186 187
	if (!rtnl_trylock())
		return restart_syscall();

188
	bond_for_each_slave(bond, slave, iter) {
189 190 191 192
		if (res > (PAGE_SIZE - IFNAMSIZ)) {
			/* not enough space for another interface name */
			if ((PAGE_SIZE - res) > 10)
				res = PAGE_SIZE - 10;
193
			res += sprintf(buf + res, "++more++ ");
194 195 196 197
			break;
		}
		res += sprintf(buf + res, "%s ", slave->dev->name);
	}
198 199 200

	rtnl_unlock();

201 202
	if (res)
		buf[res-1] = '\n'; /* eat the leftover space */
203

204 205
	return res;
}
S
Stephen Hemminger 已提交
206
static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
207
		   bonding_sysfs_store_option);
208

209
/* Show the bonding mode. */
210 211
static ssize_t bonding_show_mode(struct device *d,
				 struct device_attribute *attr, char *buf)
212
{
213
	struct bonding *bond = to_bond(d);
214
	const struct bond_opt_value *val;
215

216
	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
217

218
	return sprintf(buf, "%s %d\n", val->string, BOND_MODE(bond));
219
}
S
Stephen Hemminger 已提交
220
static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
221
		   bonding_show_mode, bonding_sysfs_store_option);
222

223
/* Show the bonding transmit hash method. */
224 225 226
static ssize_t bonding_show_xmit_hash(struct device *d,
				      struct device_attribute *attr,
				      char *buf)
227
{
228
	struct bonding *bond = to_bond(d);
229
	const struct bond_opt_value *val;
230 231

	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
232

233
	return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
234
}
S
Stephen Hemminger 已提交
235
static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
236
		   bonding_show_xmit_hash, bonding_sysfs_store_option);
237

238
/* Show arp_validate. */
239 240 241
static ssize_t bonding_show_arp_validate(struct device *d,
					 struct device_attribute *attr,
					 char *buf)
242
{
243
	struct bonding *bond = to_bond(d);
244
	const struct bond_opt_value *val;
245 246 247

	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
			       bond->params.arp_validate);
248

249
	return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
250
}
S
Stephen Hemminger 已提交
251
static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
252 253 254
		   bonding_sysfs_store_option);

/* Show arp_all_targets. */
255 256 257 258 259
static ssize_t bonding_show_arp_all_targets(struct device *d,
					 struct device_attribute *attr,
					 char *buf)
{
	struct bonding *bond = to_bond(d);
260
	const struct bond_opt_value *val;
261

262 263 264 265
	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
			       bond->params.arp_all_targets);
	return sprintf(buf, "%s %d\n",
		       val->string, bond->params.arp_all_targets);
266 267
}
static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
268
		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
269

270
/* Show fail_over_mac. */
S
Stephen Hemminger 已提交
271 272 273
static ssize_t bonding_show_fail_over_mac(struct device *d,
					  struct device_attribute *attr,
					  char *buf)
274 275
{
	struct bonding *bond = to_bond(d);
276
	const struct bond_opt_value *val;
277

278 279 280 281
	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
			       bond->params.fail_over_mac);

	return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
282
}
S
Stephen Hemminger 已提交
283
static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
284
		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
285

286
/* Show the arp timer interval. */
287 288 289
static ssize_t bonding_show_arp_interval(struct device *d,
					 struct device_attribute *attr,
					 char *buf)
290
{
291
	struct bonding *bond = to_bond(d);
292

293
	return sprintf(buf, "%d\n", bond->params.arp_interval);
294
}
S
Stephen Hemminger 已提交
295
static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
296
		   bonding_show_arp_interval, bonding_sysfs_store_option);
297

298
/* Show the arp targets. */
299 300 301
static ssize_t bonding_show_arp_targets(struct device *d,
					struct device_attribute *attr,
					char *buf)
302
{
303
	struct bonding *bond = to_bond(d);
304
	int i, res = 0;
305 306 307

	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
		if (bond->params.arp_targets[i])
H
Harvey Harrison 已提交
308 309
			res += sprintf(buf + res, "%pI4 ",
				       &bond->params.arp_targets[i]);
310
	}
311 312
	if (res)
		buf[res-1] = '\n'; /* eat the leftover space */
313

314 315
	return res;
}
316 317
static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR,
		   bonding_show_arp_targets, bonding_sysfs_store_option);
318

319
/* Show the up and down delays. */
320 321 322
static ssize_t bonding_show_downdelay(struct device *d,
				      struct device_attribute *attr,
				      char *buf)
323
{
324
	struct bonding *bond = to_bond(d);
325

326
	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
327
}
S
Stephen Hemminger 已提交
328
static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
329
		   bonding_show_downdelay, bonding_sysfs_store_option);
330

331 332 333
static ssize_t bonding_show_updelay(struct device *d,
				    struct device_attribute *attr,
				    char *buf)
334
{
335
	struct bonding *bond = to_bond(d);
336

337
	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
338 339

}
S
Stephen Hemminger 已提交
340
static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
341
		   bonding_show_updelay, bonding_sysfs_store_option);
342

343
/* Show the LACP interval. */
344 345 346
static ssize_t bonding_show_lacp(struct device *d,
				 struct device_attribute *attr,
				 char *buf)
347
{
348
	struct bonding *bond = to_bond(d);
349
	const struct bond_opt_value *val;
350

351 352 353
	val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);

	return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
354
}
S
Stephen Hemminger 已提交
355
static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
356
		   bonding_show_lacp, bonding_sysfs_store_option);
357

358 359 360 361 362 363
static ssize_t bonding_show_min_links(struct device *d,
				      struct device_attribute *attr,
				      char *buf)
{
	struct bonding *bond = to_bond(d);

364
	return sprintf(buf, "%u\n", bond->params.min_links);
365 366
}
static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
367
		   bonding_show_min_links, bonding_sysfs_store_option);
368

369 370 371 372 373
static ssize_t bonding_show_ad_select(struct device *d,
				      struct device_attribute *attr,
				      char *buf)
{
	struct bonding *bond = to_bond(d);
374
	const struct bond_opt_value *val;
375

376 377 378
	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);

	return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
379
}
S
Stephen Hemminger 已提交
380
static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
381
		   bonding_show_ad_select, bonding_sysfs_store_option);
382

383
/* Show and set the number of peer notifications to send after a failover event. */
384 385 386 387 388 389 390 391 392 393 394 395 396
static ssize_t bonding_show_num_peer_notif(struct device *d,
					   struct device_attribute *attr,
					   char *buf)
{
	struct bonding *bond = to_bond(d);
	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
}

static ssize_t bonding_store_num_peer_notif(struct device *d,
					    struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct bonding *bond = to_bond(d);
397 398
	int ret;

399
	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_NUM_PEER_NOTIF, (char *)buf);
400 401 402 403
	if (!ret)
		ret = count;

	return ret;
404 405 406 407 408 409
}
static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
		   bonding_show_num_peer_notif, bonding_store_num_peer_notif);
static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
		   bonding_show_num_peer_notif, bonding_store_num_peer_notif);

410
/* Show the MII monitor interval. */
411 412 413
static ssize_t bonding_show_miimon(struct device *d,
				   struct device_attribute *attr,
				   char *buf)
414
{
415
	struct bonding *bond = to_bond(d);
416

417
	return sprintf(buf, "%d\n", bond->params.miimon);
418
}
S
Stephen Hemminger 已提交
419
static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
420
		   bonding_show_miimon, bonding_sysfs_store_option);
421

422
/* Show the primary slave. */
423 424 425
static ssize_t bonding_show_primary(struct device *d,
				    struct device_attribute *attr,
				    char *buf)
426
{
427
	struct bonding *bond = to_bond(d);
428 429
	struct slave *primary;
	int count = 0;
430

431 432 433 434 435
	rcu_read_lock();
	primary = rcu_dereference(bond->primary_slave);
	if (primary)
		count = sprintf(buf, "%s\n", primary->dev->name);
	rcu_read_unlock();
436 437 438

	return count;
}
S
Stephen Hemminger 已提交
439
static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
440
		   bonding_show_primary, bonding_sysfs_store_option);
441

442
/* Show the primary_reselect flag. */
443 444 445 446 447
static ssize_t bonding_show_primary_reselect(struct device *d,
					     struct device_attribute *attr,
					     char *buf)
{
	struct bonding *bond = to_bond(d);
448
	const struct bond_opt_value *val;
449 450 451

	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
			       bond->params.primary_reselect);
452 453

	return sprintf(buf, "%s %d\n",
454
		       val->string, bond->params.primary_reselect);
455 456
}
static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
457
		   bonding_show_primary_reselect, bonding_sysfs_store_option);
458

459
/* Show the use_carrier flag. */
460 461 462
static ssize_t bonding_show_carrier(struct device *d,
				    struct device_attribute *attr,
				    char *buf)
463
{
464
	struct bonding *bond = to_bond(d);
465

466
	return sprintf(buf, "%d\n", bond->params.use_carrier);
467
}
S
Stephen Hemminger 已提交
468
static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
469
		   bonding_show_carrier, bonding_sysfs_store_option);
470 471


472
/* Show currently active_slave. */
473 474 475
static ssize_t bonding_show_active_slave(struct device *d,
					 struct device_attribute *attr,
					 char *buf)
476
{
477
	struct bonding *bond = to_bond(d);
478
	struct net_device *slave_dev;
479
	int count = 0;
480

481
	rcu_read_lock();
482 483 484
	slave_dev = bond_option_active_slave_get_rcu(bond);
	if (slave_dev)
		count = sprintf(buf, "%s\n", slave_dev->name);
485 486
	rcu_read_unlock();

487 488
	return count;
}
S
Stephen Hemminger 已提交
489
static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
490
		   bonding_show_active_slave, bonding_sysfs_store_option);
491

492
/* Show link status of the bond interface. */
493 494 495
static ssize_t bonding_show_mii_status(struct device *d,
				       struct device_attribute *attr,
				       char *buf)
496
{
497
	struct bonding *bond = to_bond(d);
498
	bool active = !!rcu_access_pointer(bond->curr_active_slave);
499

500
	return sprintf(buf, "%s\n", active ? "up" : "down");
501
}
502
static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
503

504
/* Show current 802.3ad aggregator ID. */
505 506 507
static ssize_t bonding_show_ad_aggregator(struct device *d,
					  struct device_attribute *attr,
					  char *buf)
508 509
{
	int count = 0;
510
	struct bonding *bond = to_bond(d);
511

512
	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
513
		struct ad_info ad_info;
S
Stephen Hemminger 已提交
514
		count = sprintf(buf, "%d\n",
515
				bond_3ad_get_active_agg_info(bond, &ad_info)
S
Stephen Hemminger 已提交
516
				?  0 : ad_info.aggregator_id);
517 518 519 520
	}

	return count;
}
521
static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
522 523


524
/* Show number of active 802.3ad ports. */
525 526 527
static ssize_t bonding_show_ad_num_ports(struct device *d,
					 struct device_attribute *attr,
					 char *buf)
528 529
{
	int count = 0;
530
	struct bonding *bond = to_bond(d);
531

532
	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
533
		struct ad_info ad_info;
S
Stephen Hemminger 已提交
534
		count = sprintf(buf, "%d\n",
535
				bond_3ad_get_active_agg_info(bond, &ad_info)
S
Stephen Hemminger 已提交
536
				?  0 : ad_info.ports);
537 538 539 540
	}

	return count;
}
541
static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
542 543


544
/* Show current 802.3ad actor key. */
545 546 547
static ssize_t bonding_show_ad_actor_key(struct device *d,
					 struct device_attribute *attr,
					 char *buf)
548 549
{
	int count = 0;
550
	struct bonding *bond = to_bond(d);
551

552
	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
553
		struct ad_info ad_info;
S
Stephen Hemminger 已提交
554
		count = sprintf(buf, "%d\n",
555
				bond_3ad_get_active_agg_info(bond, &ad_info)
S
Stephen Hemminger 已提交
556
				?  0 : ad_info.actor_key);
557 558 559 560
	}

	return count;
}
561
static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
562 563


564
/* Show current 802.3ad partner key. */
565 566 567
static ssize_t bonding_show_ad_partner_key(struct device *d,
					   struct device_attribute *attr,
					   char *buf)
568 569
{
	int count = 0;
570
	struct bonding *bond = to_bond(d);
571

572
	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
573
		struct ad_info ad_info;
S
Stephen Hemminger 已提交
574
		count = sprintf(buf, "%d\n",
575
				bond_3ad_get_active_agg_info(bond, &ad_info)
S
Stephen Hemminger 已提交
576
				?  0 : ad_info.partner_key);
577 578 579 580
	}

	return count;
}
581
static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
582 583


584
/* Show current 802.3ad partner mac. */
585 586 587
static ssize_t bonding_show_ad_partner_mac(struct device *d,
					   struct device_attribute *attr,
					   char *buf)
588 589
{
	int count = 0;
590
	struct bonding *bond = to_bond(d);
591

592
	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
593
		struct ad_info ad_info;
S
Stephen Hemminger 已提交
594
		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
J
Johannes Berg 已提交
595
			count = sprintf(buf, "%pM\n", ad_info.partner_system);
596 597 598 599
	}

	return count;
}
600
static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
601

602
/* Show the queue_ids of the slaves in the current bond. */
603 604 605 606 607
static ssize_t bonding_show_queue_id(struct device *d,
				     struct device_attribute *attr,
				     char *buf)
{
	struct bonding *bond = to_bond(d);
608
	struct list_head *iter;
609 610
	struct slave *slave;
	int res = 0;
611 612 613 614

	if (!rtnl_trylock())
		return restart_syscall();

615
	bond_for_each_slave(bond, slave, iter) {
616 617
		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
			/* not enough space for another interface_name:queue_id pair */
618 619 620 621 622 623 624 625 626 627
			if ((PAGE_SIZE - res) > 10)
				res = PAGE_SIZE - 10;
			res += sprintf(buf + res, "++more++ ");
			break;
		}
		res += sprintf(buf + res, "%s:%d ",
			       slave->dev->name, slave->queue_id);
	}
	if (res)
		buf[res-1] = '\n'; /* eat the leftover space */
628

629
	rtnl_unlock();
630

631 632 633
	return res;
}
static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
634
		   bonding_sysfs_store_option);
635 636


637
/* Show the all_slaves_active flag. */
638 639 640 641 642 643 644 645 646
static ssize_t bonding_show_slaves_active(struct device *d,
					  struct device_attribute *attr,
					  char *buf)
{
	struct bonding *bond = to_bond(d);

	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
}
static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
647
		   bonding_show_slaves_active, bonding_sysfs_store_option);
648

649
/* Show the number of IGMP membership reports to send on link failure */
650
static ssize_t bonding_show_resend_igmp(struct device *d,
651 652
					struct device_attribute *attr,
					char *buf)
653 654 655 656 657 658
{
	struct bonding *bond = to_bond(d);

	return sprintf(buf, "%d\n", bond->params.resend_igmp);
}
static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
659
		   bonding_show_resend_igmp, bonding_sysfs_store_option);
660

661 662 663 664 665 666

static ssize_t bonding_show_lp_interval(struct device *d,
					struct device_attribute *attr,
					char *buf)
{
	struct bonding *bond = to_bond(d);
667

668
	return sprintf(buf, "%d\n", bond->params.lp_interval);
669 670
}
static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
671
		   bonding_show_lp_interval, bonding_sysfs_store_option);
672

673 674 675 676 677 678 679 680
static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
					   struct device_attribute *attr,
					   char *buf)
{
	struct bonding *bond = to_bond(d);
	return sprintf(buf, "%d\n", bond->params.tlb_dynamic_lb);
}
static DEVICE_ATTR(tlb_dynamic_lb, S_IRUGO | S_IWUSR,
681
		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
682

683 684 685 686 687
static ssize_t bonding_show_packets_per_slave(struct device *d,
					      struct device_attribute *attr,
					      char *buf)
{
	struct bonding *bond = to_bond(d);
688
	unsigned int packets_per_slave = bond->params.packets_per_slave;
689

690
	return sprintf(buf, "%u\n", packets_per_slave);
691 692
}
static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
693
		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
694

695
static struct attribute *per_bond_attrs[] = {
696 697
	&dev_attr_slaves.attr,
	&dev_attr_mode.attr,
698
	&dev_attr_fail_over_mac.attr,
699
	&dev_attr_arp_validate.attr,
700
	&dev_attr_arp_all_targets.attr,
701 702 703 704 705
	&dev_attr_arp_interval.attr,
	&dev_attr_arp_ip_target.attr,
	&dev_attr_downdelay.attr,
	&dev_attr_updelay.attr,
	&dev_attr_lacp_rate.attr,
706
	&dev_attr_ad_select.attr,
707
	&dev_attr_xmit_hash_policy.attr,
708 709
	&dev_attr_num_grat_arp.attr,
	&dev_attr_num_unsol_na.attr,
710 711
	&dev_attr_miimon.attr,
	&dev_attr_primary.attr,
712
	&dev_attr_primary_reselect.attr,
713 714 715 716 717 718 719 720
	&dev_attr_use_carrier.attr,
	&dev_attr_active_slave.attr,
	&dev_attr_mii_status.attr,
	&dev_attr_ad_aggregator.attr,
	&dev_attr_ad_num_ports.attr,
	&dev_attr_ad_actor_key.attr,
	&dev_attr_ad_partner_key.attr,
	&dev_attr_ad_partner_mac.attr,
721
	&dev_attr_queue_id.attr,
722
	&dev_attr_all_slaves_active.attr,
723
	&dev_attr_resend_igmp.attr,
724
	&dev_attr_min_links.attr,
725
	&dev_attr_lp_interval.attr,
726
	&dev_attr_packets_per_slave.attr,
727
	&dev_attr_tlb_dynamic_lb.attr,
728 729 730 731 732 733 734 735
	NULL,
};

static struct attribute_group bonding_group = {
	.name = "bonding",
	.attrs = per_bond_attrs,
};

736
/* Initialize sysfs.  This sets up the bonding_masters file in
737 738
 * /sys/class/net.
 */
739
int bond_create_sysfs(struct bond_net *bn)
740
{
741
	int ret;
742

743
	bn->class_attr_bonding_masters = class_attr_bonding_masters;
744
	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
745

746 747
	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
					  bn->net);
748
	/* Permit multiple loads of the module by ignoring failures to
749 750 751 752 753 754 755 756 757 758
	 * create the bonding_masters sysfs file.  Bonding devices
	 * created by second or subsequent loads of the module will
	 * not be listed in, or controllable by, bonding_masters, but
	 * will have the usual "bonding" sysfs directory.
	 *
	 * This is done to preserve backwards compatibility for
	 * initscripts/sysconfig, which load bonding multiple times to
	 * configure multiple bonding devices.
	 */
	if (ret == -EEXIST) {
759
		/* Is someone being kinky and naming a device bonding_master? */
760
		if (__dev_get_by_name(bn->net,
761
				      class_attr_bonding_masters.attr.name))
J
Joe Perches 已提交
762
			pr_err("network device named %s already exists in sysfs\n",
763
			       class_attr_bonding_masters.attr.name);
764
		ret = 0;
765
	}
766 767 768 769 770

	return ret;

}

771
/* Remove /sys/class/net/bonding_masters. */
772
void bond_destroy_sysfs(struct bond_net *bn)
773
{
774
	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
775 776
}

777
/* Initialize sysfs for each bond.  This sets up and registers
778 779
 * the 'bondctl' directory for each individual bond under /sys/class/net.
 */
780
void bond_prepare_sysfs_group(struct bonding *bond)
781
{
782
	bond->dev->sysfs_groups[0] = &bonding_group;
783 784
}