Linux Audio

Check our new training course

Loading...
v4.17
 
  1/*
  2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
  3 *
  4 * This program is free software; you can redistribute it and/or modify it
  5 * under the terms of the GNU General Public License as published by the
  6 * Free Software Foundation; either version 2 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful, but
 10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 11 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12 * for more details.
 13 *
 14 * 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 * The full GNU General Public License is included in this distribution in the
 18 * file called LICENSE.
 19 *
 20 */
 21
 22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 23
 24#include <linux/kernel.h>
 25#include <linux/module.h>
 26#include <linux/device.h>
 27#include <linux/sched/signal.h>
 28#include <linux/fs.h>
 29#include <linux/types.h>
 30#include <linux/string.h>
 31#include <linux/netdevice.h>
 32#include <linux/inetdevice.h>
 33#include <linux/in.h>
 34#include <linux/sysfs.h>
 35#include <linux/ctype.h>
 36#include <linux/inet.h>
 37#include <linux/rtnetlink.h>
 38#include <linux/etherdevice.h>
 39#include <net/net_namespace.h>
 40#include <net/netns/generic.h>
 41#include <linux/nsproxy.h>
 42
 43#include <net/bonding.h>
 44
 45#define to_bond(cd)	((struct bonding *)(netdev_priv(to_net_dev(cd))))
 46
 47/* "show" function for the bond_masters attribute.
 48 * The class parameter is ignored.
 49 */
 50static ssize_t bonding_show_bonds(struct class *cls,
 51				  struct class_attribute *attr,
 52				  char *buf)
 53{
 54	struct bond_net *bn =
 55		container_of(attr, struct bond_net, class_attr_bonding_masters);
 56	int res = 0;
 57	struct bonding *bond;
 58
 59	rtnl_lock();
 60
 61	list_for_each_entry(bond, &bn->dev_list, bond_list) {
 62		if (res > (PAGE_SIZE - IFNAMSIZ)) {
 63			/* not enough space for another interface name */
 64			if ((PAGE_SIZE - res) > 10)
 65				res = PAGE_SIZE - 10;
 66			res += sprintf(buf + res, "++more++ ");
 67			break;
 68		}
 69		res += sprintf(buf + res, "%s ", bond->dev->name);
 70	}
 71	if (res)
 72		buf[res-1] = '\n'; /* eat the leftover space */
 73
 74	rtnl_unlock();
 75	return res;
 76}
 77
 78static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
 79{
 80	struct bonding *bond;
 81
 82	list_for_each_entry(bond, &bn->dev_list, bond_list) {
 83		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
 84			return bond->dev;
 85	}
 86	return NULL;
 87}
 88
 89/* "store" function for the bond_masters attribute.  This is what
 90 * creates and deletes entire bonds.
 91 *
 92 * The class parameter is ignored.
 93 */
 94static ssize_t bonding_store_bonds(struct class *cls,
 95				   struct class_attribute *attr,
 96				   const char *buffer, size_t count)
 97{
 98	struct bond_net *bn =
 99		container_of(attr, struct bond_net, class_attr_bonding_masters);
100	char command[IFNAMSIZ + 1] = {0, };
101	char *ifname;
102	int rv, res = count;
103
104	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
105	ifname = command + 1;
106	if ((strlen(command) <= 1) ||
107	    !dev_valid_name(ifname))
108		goto err_no_cmd;
109
110	if (command[0] == '+') {
111		pr_info("%s is being created...\n", ifname);
112		rv = bond_create(bn->net, ifname);
113		if (rv) {
114			if (rv == -EEXIST)
115				pr_info("%s already exists\n", ifname);
116			else
117				pr_info("%s creation failed\n", ifname);
118			res = rv;
119		}
120	} else if (command[0] == '-') {
121		struct net_device *bond_dev;
122
123		rtnl_lock();
124		bond_dev = bond_get_by_name(bn, ifname);
125		if (bond_dev) {
126			pr_info("%s is being deleted...\n", ifname);
127			unregister_netdevice(bond_dev);
128		} else {
129			pr_err("unable to delete non-existent %s\n", ifname);
130			res = -ENODEV;
131		}
132		rtnl_unlock();
133	} else
134		goto err_no_cmd;
135
136	/* Always return either count or an error.  If you return 0, you'll
137	 * get called forever, which is bad.
138	 */
139	return res;
140
141err_no_cmd:
142	pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
143	return -EPERM;
144}
145
146/* class attribute for bond_masters file.  This ends up in /sys/class/net */
147static const struct class_attribute class_attr_bonding_masters = {
148	.attr = {
149		.name = "bonding_masters",
150		.mode = 0644,
151	},
152	.show = bonding_show_bonds,
153	.store = bonding_store_bonds,
154};
155
156/* Generic "store" method for bonding sysfs option setting */
157static ssize_t bonding_sysfs_store_option(struct device *d,
158					  struct device_attribute *attr,
159					  const char *buffer, size_t count)
160{
161	struct bonding *bond = to_bond(d);
162	const struct bond_option *opt;
 
163	int ret;
164
165	opt = bond_opt_get_by_name(attr->attr.name);
166	if (WARN_ON(!opt))
167		return -ENOENT;
168	ret = bond_opt_tryset_rtnl(bond, opt->id, (char *)buffer);
 
 
 
169	if (!ret)
170		ret = count;
 
171
172	return ret;
173}
174
175/* Show the slaves in the current bond. */
176static ssize_t bonding_show_slaves(struct device *d,
177				   struct device_attribute *attr, char *buf)
178{
179	struct bonding *bond = to_bond(d);
180	struct list_head *iter;
181	struct slave *slave;
182	int res = 0;
183
184	if (!rtnl_trylock())
185		return restart_syscall();
186
187	bond_for_each_slave(bond, slave, iter) {
188		if (res > (PAGE_SIZE - IFNAMSIZ)) {
189			/* not enough space for another interface name */
190			if ((PAGE_SIZE - res) > 10)
191				res = PAGE_SIZE - 10;
192			res += sprintf(buf + res, "++more++ ");
193			break;
194		}
195		res += sprintf(buf + res, "%s ", slave->dev->name);
196	}
197
198	rtnl_unlock();
199
200	if (res)
201		buf[res-1] = '\n'; /* eat the leftover space */
202
203	return res;
204}
205static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
206		   bonding_sysfs_store_option);
207
208/* Show the bonding mode. */
209static ssize_t bonding_show_mode(struct device *d,
210				 struct device_attribute *attr, char *buf)
211{
212	struct bonding *bond = to_bond(d);
213	const struct bond_opt_value *val;
214
215	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
216
217	return sprintf(buf, "%s %d\n", val->string, BOND_MODE(bond));
218}
219static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
220
221/* Show the bonding transmit hash method. */
222static ssize_t bonding_show_xmit_hash(struct device *d,
223				      struct device_attribute *attr,
224				      char *buf)
225{
226	struct bonding *bond = to_bond(d);
227	const struct bond_opt_value *val;
228
229	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
230
231	return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
232}
233static DEVICE_ATTR(xmit_hash_policy, 0644,
234		   bonding_show_xmit_hash, bonding_sysfs_store_option);
235
236/* Show arp_validate. */
237static ssize_t bonding_show_arp_validate(struct device *d,
238					 struct device_attribute *attr,
239					 char *buf)
240{
241	struct bonding *bond = to_bond(d);
242	const struct bond_opt_value *val;
243
244	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
245			       bond->params.arp_validate);
246
247	return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
248}
249static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
250		   bonding_sysfs_store_option);
251
252/* Show arp_all_targets. */
253static ssize_t bonding_show_arp_all_targets(struct device *d,
254					 struct device_attribute *attr,
255					 char *buf)
256{
257	struct bonding *bond = to_bond(d);
258	const struct bond_opt_value *val;
259
260	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
261			       bond->params.arp_all_targets);
262	return sprintf(buf, "%s %d\n",
263		       val->string, bond->params.arp_all_targets);
264}
265static DEVICE_ATTR(arp_all_targets, 0644,
266		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
267
268/* Show fail_over_mac. */
269static ssize_t bonding_show_fail_over_mac(struct device *d,
270					  struct device_attribute *attr,
271					  char *buf)
272{
273	struct bonding *bond = to_bond(d);
274	const struct bond_opt_value *val;
275
276	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
277			       bond->params.fail_over_mac);
278
279	return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
280}
281static DEVICE_ATTR(fail_over_mac, 0644,
282		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
283
284/* Show the arp timer interval. */
285static ssize_t bonding_show_arp_interval(struct device *d,
286					 struct device_attribute *attr,
287					 char *buf)
288{
289	struct bonding *bond = to_bond(d);
290
291	return sprintf(buf, "%d\n", bond->params.arp_interval);
292}
293static DEVICE_ATTR(arp_interval, 0644,
294		   bonding_show_arp_interval, bonding_sysfs_store_option);
295
296/* Show the arp targets. */
297static ssize_t bonding_show_arp_targets(struct device *d,
298					struct device_attribute *attr,
299					char *buf)
300{
301	struct bonding *bond = to_bond(d);
302	int i, res = 0;
303
304	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
305		if (bond->params.arp_targets[i])
306			res += sprintf(buf + res, "%pI4 ",
307				       &bond->params.arp_targets[i]);
308	}
309	if (res)
310		buf[res-1] = '\n'; /* eat the leftover space */
311
312	return res;
313}
314static DEVICE_ATTR(arp_ip_target, 0644,
315		   bonding_show_arp_targets, bonding_sysfs_store_option);
316
317/* Show the up and down delays. */
318static ssize_t bonding_show_downdelay(struct device *d,
319				      struct device_attribute *attr,
320				      char *buf)
321{
322	struct bonding *bond = to_bond(d);
323
324	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
325}
326static DEVICE_ATTR(downdelay, 0644,
327		   bonding_show_downdelay, bonding_sysfs_store_option);
328
329static ssize_t bonding_show_updelay(struct device *d,
330				    struct device_attribute *attr,
331				    char *buf)
332{
333	struct bonding *bond = to_bond(d);
334
335	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
336
337}
338static DEVICE_ATTR(updelay, 0644,
339		   bonding_show_updelay, bonding_sysfs_store_option);
340
 
 
 
 
 
 
 
 
 
 
 
 
341/* Show the LACP interval. */
342static ssize_t bonding_show_lacp(struct device *d,
343				 struct device_attribute *attr,
344				 char *buf)
345{
346	struct bonding *bond = to_bond(d);
347	const struct bond_opt_value *val;
348
349	val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
350
351	return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
352}
353static DEVICE_ATTR(lacp_rate, 0644,
354		   bonding_show_lacp, bonding_sysfs_store_option);
355
356static ssize_t bonding_show_min_links(struct device *d,
357				      struct device_attribute *attr,
358				      char *buf)
359{
360	struct bonding *bond = to_bond(d);
361
362	return sprintf(buf, "%u\n", bond->params.min_links);
363}
364static DEVICE_ATTR(min_links, 0644,
365		   bonding_show_min_links, bonding_sysfs_store_option);
366
367static ssize_t bonding_show_ad_select(struct device *d,
368				      struct device_attribute *attr,
369				      char *buf)
370{
371	struct bonding *bond = to_bond(d);
372	const struct bond_opt_value *val;
373
374	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
375
376	return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
377}
378static DEVICE_ATTR(ad_select, 0644,
379		   bonding_show_ad_select, bonding_sysfs_store_option);
380
381/* Show the number of peer notifications to send after a failover event. */
382static ssize_t bonding_show_num_peer_notif(struct device *d,
383					   struct device_attribute *attr,
384					   char *buf)
385{
386	struct bonding *bond = to_bond(d);
387	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
388}
389static DEVICE_ATTR(num_grat_arp, 0644,
390		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
391static DEVICE_ATTR(num_unsol_na, 0644,
392		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
393
394/* Show the MII monitor interval. */
395static ssize_t bonding_show_miimon(struct device *d,
396				   struct device_attribute *attr,
397				   char *buf)
398{
399	struct bonding *bond = to_bond(d);
400
401	return sprintf(buf, "%d\n", bond->params.miimon);
402}
403static DEVICE_ATTR(miimon, 0644,
404		   bonding_show_miimon, bonding_sysfs_store_option);
405
406/* Show the primary slave. */
407static ssize_t bonding_show_primary(struct device *d,
408				    struct device_attribute *attr,
409				    char *buf)
410{
411	struct bonding *bond = to_bond(d);
412	struct slave *primary;
413	int count = 0;
414
415	rcu_read_lock();
416	primary = rcu_dereference(bond->primary_slave);
417	if (primary)
418		count = sprintf(buf, "%s\n", primary->dev->name);
419	rcu_read_unlock();
420
421	return count;
422}
423static DEVICE_ATTR(primary, 0644,
424		   bonding_show_primary, bonding_sysfs_store_option);
425
426/* Show the primary_reselect flag. */
427static ssize_t bonding_show_primary_reselect(struct device *d,
428					     struct device_attribute *attr,
429					     char *buf)
430{
431	struct bonding *bond = to_bond(d);
432	const struct bond_opt_value *val;
433
434	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
435			       bond->params.primary_reselect);
436
437	return sprintf(buf, "%s %d\n",
438		       val->string, bond->params.primary_reselect);
439}
440static DEVICE_ATTR(primary_reselect, 0644,
441		   bonding_show_primary_reselect, bonding_sysfs_store_option);
442
443/* Show the use_carrier flag. */
444static ssize_t bonding_show_carrier(struct device *d,
445				    struct device_attribute *attr,
446				    char *buf)
447{
448	struct bonding *bond = to_bond(d);
449
450	return sprintf(buf, "%d\n", bond->params.use_carrier);
451}
452static DEVICE_ATTR(use_carrier, 0644,
453		   bonding_show_carrier, bonding_sysfs_store_option);
454
455
456/* Show currently active_slave. */
457static ssize_t bonding_show_active_slave(struct device *d,
458					 struct device_attribute *attr,
459					 char *buf)
460{
461	struct bonding *bond = to_bond(d);
462	struct net_device *slave_dev;
463	int count = 0;
464
465	rcu_read_lock();
466	slave_dev = bond_option_active_slave_get_rcu(bond);
467	if (slave_dev)
468		count = sprintf(buf, "%s\n", slave_dev->name);
469	rcu_read_unlock();
470
471	return count;
472}
473static DEVICE_ATTR(active_slave, 0644,
474		   bonding_show_active_slave, bonding_sysfs_store_option);
475
476/* Show link status of the bond interface. */
477static ssize_t bonding_show_mii_status(struct device *d,
478				       struct device_attribute *attr,
479				       char *buf)
480{
481	struct bonding *bond = to_bond(d);
482	bool active = netif_carrier_ok(bond->dev);
483
484	return sprintf(buf, "%s\n", active ? "up" : "down");
485}
486static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
487
488/* Show current 802.3ad aggregator ID. */
489static ssize_t bonding_show_ad_aggregator(struct device *d,
490					  struct device_attribute *attr,
491					  char *buf)
492{
493	int count = 0;
494	struct bonding *bond = to_bond(d);
495
496	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
497		struct ad_info ad_info;
498		count = sprintf(buf, "%d\n",
499				bond_3ad_get_active_agg_info(bond, &ad_info)
500				?  0 : ad_info.aggregator_id);
501	}
502
503	return count;
504}
505static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
506
507
508/* Show number of active 802.3ad ports. */
509static ssize_t bonding_show_ad_num_ports(struct device *d,
510					 struct device_attribute *attr,
511					 char *buf)
512{
513	int count = 0;
514	struct bonding *bond = to_bond(d);
515
516	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
517		struct ad_info ad_info;
518		count = sprintf(buf, "%d\n",
519				bond_3ad_get_active_agg_info(bond, &ad_info)
520				?  0 : ad_info.ports);
521	}
522
523	return count;
524}
525static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
526
527
528/* Show current 802.3ad actor key. */
529static ssize_t bonding_show_ad_actor_key(struct device *d,
530					 struct device_attribute *attr,
531					 char *buf)
532{
533	int count = 0;
534	struct bonding *bond = to_bond(d);
535
536	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
537		struct ad_info ad_info;
538		count = sprintf(buf, "%d\n",
539				bond_3ad_get_active_agg_info(bond, &ad_info)
540				?  0 : ad_info.actor_key);
541	}
542
543	return count;
544}
545static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
546
547
548/* Show current 802.3ad partner key. */
549static ssize_t bonding_show_ad_partner_key(struct device *d,
550					   struct device_attribute *attr,
551					   char *buf)
552{
553	int count = 0;
554	struct bonding *bond = to_bond(d);
555
556	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
557		struct ad_info ad_info;
558		count = sprintf(buf, "%d\n",
559				bond_3ad_get_active_agg_info(bond, &ad_info)
560				?  0 : ad_info.partner_key);
561	}
562
563	return count;
564}
565static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
566
567
568/* Show current 802.3ad partner mac. */
569static ssize_t bonding_show_ad_partner_mac(struct device *d,
570					   struct device_attribute *attr,
571					   char *buf)
572{
573	int count = 0;
574	struct bonding *bond = to_bond(d);
575
576	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
577		struct ad_info ad_info;
578		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
579			count = sprintf(buf, "%pM\n", ad_info.partner_system);
580	}
581
582	return count;
583}
584static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
585
586/* Show the queue_ids of the slaves in the current bond. */
587static ssize_t bonding_show_queue_id(struct device *d,
588				     struct device_attribute *attr,
589				     char *buf)
590{
591	struct bonding *bond = to_bond(d);
592	struct list_head *iter;
593	struct slave *slave;
594	int res = 0;
595
596	if (!rtnl_trylock())
597		return restart_syscall();
598
599	bond_for_each_slave(bond, slave, iter) {
600		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
601			/* not enough space for another interface_name:queue_id pair */
602			if ((PAGE_SIZE - res) > 10)
603				res = PAGE_SIZE - 10;
604			res += sprintf(buf + res, "++more++ ");
605			break;
606		}
607		res += sprintf(buf + res, "%s:%d ",
608			       slave->dev->name, slave->queue_id);
609	}
610	if (res)
611		buf[res-1] = '\n'; /* eat the leftover space */
612
613	rtnl_unlock();
614
615	return res;
616}
617static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
618		   bonding_sysfs_store_option);
619
620
621/* Show the all_slaves_active flag. */
622static ssize_t bonding_show_slaves_active(struct device *d,
623					  struct device_attribute *attr,
624					  char *buf)
625{
626	struct bonding *bond = to_bond(d);
627
628	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
629}
630static DEVICE_ATTR(all_slaves_active, 0644,
631		   bonding_show_slaves_active, bonding_sysfs_store_option);
632
633/* Show the number of IGMP membership reports to send on link failure */
634static ssize_t bonding_show_resend_igmp(struct device *d,
635					struct device_attribute *attr,
636					char *buf)
637{
638	struct bonding *bond = to_bond(d);
639
640	return sprintf(buf, "%d\n", bond->params.resend_igmp);
641}
642static DEVICE_ATTR(resend_igmp, 0644,
643		   bonding_show_resend_igmp, bonding_sysfs_store_option);
644
645
646static ssize_t bonding_show_lp_interval(struct device *d,
647					struct device_attribute *attr,
648					char *buf)
649{
650	struct bonding *bond = to_bond(d);
651
652	return sprintf(buf, "%d\n", bond->params.lp_interval);
653}
654static DEVICE_ATTR(lp_interval, 0644,
655		   bonding_show_lp_interval, bonding_sysfs_store_option);
656
657static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
658					   struct device_attribute *attr,
659					   char *buf)
660{
661	struct bonding *bond = to_bond(d);
662	return sprintf(buf, "%d\n", bond->params.tlb_dynamic_lb);
663}
664static DEVICE_ATTR(tlb_dynamic_lb, 0644,
665		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
666
667static ssize_t bonding_show_packets_per_slave(struct device *d,
668					      struct device_attribute *attr,
669					      char *buf)
670{
671	struct bonding *bond = to_bond(d);
672	unsigned int packets_per_slave = bond->params.packets_per_slave;
673
674	return sprintf(buf, "%u\n", packets_per_slave);
675}
676static DEVICE_ATTR(packets_per_slave, 0644,
677		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
678
679static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
680					      struct device_attribute *attr,
681					      char *buf)
682{
683	struct bonding *bond = to_bond(d);
684
685	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
686		return sprintf(buf, "%hu\n", bond->params.ad_actor_sys_prio);
687
688	return 0;
689}
690static DEVICE_ATTR(ad_actor_sys_prio, 0644,
691		   bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
692
693static ssize_t bonding_show_ad_actor_system(struct device *d,
694					    struct device_attribute *attr,
695					    char *buf)
696{
697	struct bonding *bond = to_bond(d);
698
699	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
700		return sprintf(buf, "%pM\n", bond->params.ad_actor_system);
701
702	return 0;
703}
704
705static DEVICE_ATTR(ad_actor_system, 0644,
706		   bonding_show_ad_actor_system, bonding_sysfs_store_option);
707
708static ssize_t bonding_show_ad_user_port_key(struct device *d,
709					     struct device_attribute *attr,
710					     char *buf)
711{
712	struct bonding *bond = to_bond(d);
713
714	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
715		return sprintf(buf, "%hu\n", bond->params.ad_user_port_key);
716
717	return 0;
718}
719static DEVICE_ATTR(ad_user_port_key, 0644,
720		   bonding_show_ad_user_port_key, bonding_sysfs_store_option);
721
722static struct attribute *per_bond_attrs[] = {
723	&dev_attr_slaves.attr,
724	&dev_attr_mode.attr,
725	&dev_attr_fail_over_mac.attr,
726	&dev_attr_arp_validate.attr,
727	&dev_attr_arp_all_targets.attr,
728	&dev_attr_arp_interval.attr,
729	&dev_attr_arp_ip_target.attr,
730	&dev_attr_downdelay.attr,
731	&dev_attr_updelay.attr,
 
732	&dev_attr_lacp_rate.attr,
733	&dev_attr_ad_select.attr,
734	&dev_attr_xmit_hash_policy.attr,
735	&dev_attr_num_grat_arp.attr,
736	&dev_attr_num_unsol_na.attr,
737	&dev_attr_miimon.attr,
738	&dev_attr_primary.attr,
739	&dev_attr_primary_reselect.attr,
740	&dev_attr_use_carrier.attr,
741	&dev_attr_active_slave.attr,
742	&dev_attr_mii_status.attr,
743	&dev_attr_ad_aggregator.attr,
744	&dev_attr_ad_num_ports.attr,
745	&dev_attr_ad_actor_key.attr,
746	&dev_attr_ad_partner_key.attr,
747	&dev_attr_ad_partner_mac.attr,
748	&dev_attr_queue_id.attr,
749	&dev_attr_all_slaves_active.attr,
750	&dev_attr_resend_igmp.attr,
751	&dev_attr_min_links.attr,
752	&dev_attr_lp_interval.attr,
753	&dev_attr_packets_per_slave.attr,
754	&dev_attr_tlb_dynamic_lb.attr,
755	&dev_attr_ad_actor_sys_prio.attr,
756	&dev_attr_ad_actor_system.attr,
757	&dev_attr_ad_user_port_key.attr,
758	NULL,
759};
760
761static const struct attribute_group bonding_group = {
762	.name = "bonding",
763	.attrs = per_bond_attrs,
764};
765
766/* Initialize sysfs.  This sets up the bonding_masters file in
767 * /sys/class/net.
768 */
769int bond_create_sysfs(struct bond_net *bn)
770{
771	int ret;
772
773	bn->class_attr_bonding_masters = class_attr_bonding_masters;
774	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
775
776	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
777					  bn->net);
778	/* Permit multiple loads of the module by ignoring failures to
779	 * create the bonding_masters sysfs file.  Bonding devices
780	 * created by second or subsequent loads of the module will
781	 * not be listed in, or controllable by, bonding_masters, but
782	 * will have the usual "bonding" sysfs directory.
783	 *
784	 * This is done to preserve backwards compatibility for
785	 * initscripts/sysconfig, which load bonding multiple times to
786	 * configure multiple bonding devices.
787	 */
788	if (ret == -EEXIST) {
789		/* Is someone being kinky and naming a device bonding_master? */
790		if (__dev_get_by_name(bn->net,
791				      class_attr_bonding_masters.attr.name))
792			pr_err("network device named %s already exists in sysfs\n",
793			       class_attr_bonding_masters.attr.name);
794		ret = 0;
795	}
796
797	return ret;
798
799}
800
801/* Remove /sys/class/net/bonding_masters. */
802void bond_destroy_sysfs(struct bond_net *bn)
803{
804	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
805}
806
807/* Initialize sysfs for each bond.  This sets up and registers
808 * the 'bondctl' directory for each individual bond under /sys/class/net.
809 */
810void bond_prepare_sysfs_group(struct bonding *bond)
811{
812	bond->dev->sysfs_groups[0] = &bonding_group;
813}
814
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 */
  5
  6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7
  8#include <linux/kernel.h>
  9#include <linux/module.h>
 10#include <linux/device.h>
 11#include <linux/sched/signal.h>
 12#include <linux/fs.h>
 13#include <linux/types.h>
 14#include <linux/string.h>
 15#include <linux/netdevice.h>
 16#include <linux/inetdevice.h>
 17#include <linux/in.h>
 18#include <linux/sysfs.h>
 19#include <linux/ctype.h>
 20#include <linux/inet.h>
 21#include <linux/rtnetlink.h>
 22#include <linux/etherdevice.h>
 23#include <net/net_namespace.h>
 24#include <net/netns/generic.h>
 25#include <linux/nsproxy.h>
 26
 27#include <net/bonding.h>
 28
 29#define to_bond(cd)	((struct bonding *)(netdev_priv(to_net_dev(cd))))
 30
 31/* "show" function for the bond_masters attribute.
 32 * The class parameter is ignored.
 33 */
 34static ssize_t bonding_show_bonds(struct class *cls,
 35				  struct class_attribute *attr,
 36				  char *buf)
 37{
 38	struct bond_net *bn =
 39		container_of(attr, struct bond_net, class_attr_bonding_masters);
 40	int res = 0;
 41	struct bonding *bond;
 42
 43	rtnl_lock();
 44
 45	list_for_each_entry(bond, &bn->dev_list, bond_list) {
 46		if (res > (PAGE_SIZE - IFNAMSIZ)) {
 47			/* not enough space for another interface name */
 48			if ((PAGE_SIZE - res) > 10)
 49				res = PAGE_SIZE - 10;
 50			res += sprintf(buf + res, "++more++ ");
 51			break;
 52		}
 53		res += sprintf(buf + res, "%s ", bond->dev->name);
 54	}
 55	if (res)
 56		buf[res-1] = '\n'; /* eat the leftover space */
 57
 58	rtnl_unlock();
 59	return res;
 60}
 61
 62static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
 63{
 64	struct bonding *bond;
 65
 66	list_for_each_entry(bond, &bn->dev_list, bond_list) {
 67		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
 68			return bond->dev;
 69	}
 70	return NULL;
 71}
 72
 73/* "store" function for the bond_masters attribute.  This is what
 74 * creates and deletes entire bonds.
 75 *
 76 * The class parameter is ignored.
 77 */
 78static ssize_t bonding_store_bonds(struct class *cls,
 79				   struct class_attribute *attr,
 80				   const char *buffer, size_t count)
 81{
 82	struct bond_net *bn =
 83		container_of(attr, struct bond_net, class_attr_bonding_masters);
 84	char command[IFNAMSIZ + 1] = {0, };
 85	char *ifname;
 86	int rv, res = count;
 87
 88	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
 89	ifname = command + 1;
 90	if ((strlen(command) <= 1) ||
 91	    !dev_valid_name(ifname))
 92		goto err_no_cmd;
 93
 94	if (command[0] == '+') {
 95		pr_info("%s is being created...\n", ifname);
 96		rv = bond_create(bn->net, ifname);
 97		if (rv) {
 98			if (rv == -EEXIST)
 99				pr_info("%s already exists\n", ifname);
100			else
101				pr_info("%s creation failed\n", ifname);
102			res = rv;
103		}
104	} else if (command[0] == '-') {
105		struct net_device *bond_dev;
106
107		rtnl_lock();
108		bond_dev = bond_get_by_name(bn, ifname);
109		if (bond_dev) {
110			pr_info("%s is being deleted...\n", ifname);
111			unregister_netdevice(bond_dev);
112		} else {
113			pr_err("unable to delete non-existent %s\n", ifname);
114			res = -ENODEV;
115		}
116		rtnl_unlock();
117	} else
118		goto err_no_cmd;
119
120	/* Always return either count or an error.  If you return 0, you'll
121	 * get called forever, which is bad.
122	 */
123	return res;
124
125err_no_cmd:
126	pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
127	return -EPERM;
128}
129
130/* class attribute for bond_masters file.  This ends up in /sys/class/net */
131static const struct class_attribute class_attr_bonding_masters = {
132	.attr = {
133		.name = "bonding_masters",
134		.mode = 0644,
135	},
136	.show = bonding_show_bonds,
137	.store = bonding_store_bonds,
138};
139
140/* Generic "store" method for bonding sysfs option setting */
141static ssize_t bonding_sysfs_store_option(struct device *d,
142					  struct device_attribute *attr,
143					  const char *buffer, size_t count)
144{
145	struct bonding *bond = to_bond(d);
146	const struct bond_option *opt;
147	char *buffer_clone;
148	int ret;
149
150	opt = bond_opt_get_by_name(attr->attr.name);
151	if (WARN_ON(!opt))
152		return -ENOENT;
153	buffer_clone = kstrndup(buffer, count, GFP_KERNEL);
154	if (!buffer_clone)
155		return -ENOMEM;
156	ret = bond_opt_tryset_rtnl(bond, opt->id, buffer_clone);
157	if (!ret)
158		ret = count;
159	kfree(buffer_clone);
160
161	return ret;
162}
163
164/* Show the slaves in the current bond. */
165static ssize_t bonding_show_slaves(struct device *d,
166				   struct device_attribute *attr, char *buf)
167{
168	struct bonding *bond = to_bond(d);
169	struct list_head *iter;
170	struct slave *slave;
171	int res = 0;
172
173	if (!rtnl_trylock())
174		return restart_syscall();
175
176	bond_for_each_slave(bond, slave, iter) {
177		if (res > (PAGE_SIZE - IFNAMSIZ)) {
178			/* not enough space for another interface name */
179			if ((PAGE_SIZE - res) > 10)
180				res = PAGE_SIZE - 10;
181			res += sprintf(buf + res, "++more++ ");
182			break;
183		}
184		res += sprintf(buf + res, "%s ", slave->dev->name);
185	}
186
187	rtnl_unlock();
188
189	if (res)
190		buf[res-1] = '\n'; /* eat the leftover space */
191
192	return res;
193}
194static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
195		   bonding_sysfs_store_option);
196
197/* Show the bonding mode. */
198static ssize_t bonding_show_mode(struct device *d,
199				 struct device_attribute *attr, char *buf)
200{
201	struct bonding *bond = to_bond(d);
202	const struct bond_opt_value *val;
203
204	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
205
206	return sprintf(buf, "%s %d\n", val->string, BOND_MODE(bond));
207}
208static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
209
210/* Show the bonding transmit hash method. */
211static ssize_t bonding_show_xmit_hash(struct device *d,
212				      struct device_attribute *attr,
213				      char *buf)
214{
215	struct bonding *bond = to_bond(d);
216	const struct bond_opt_value *val;
217
218	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
219
220	return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
221}
222static DEVICE_ATTR(xmit_hash_policy, 0644,
223		   bonding_show_xmit_hash, bonding_sysfs_store_option);
224
225/* Show arp_validate. */
226static ssize_t bonding_show_arp_validate(struct device *d,
227					 struct device_attribute *attr,
228					 char *buf)
229{
230	struct bonding *bond = to_bond(d);
231	const struct bond_opt_value *val;
232
233	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
234			       bond->params.arp_validate);
235
236	return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
237}
238static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
239		   bonding_sysfs_store_option);
240
241/* Show arp_all_targets. */
242static ssize_t bonding_show_arp_all_targets(struct device *d,
243					 struct device_attribute *attr,
244					 char *buf)
245{
246	struct bonding *bond = to_bond(d);
247	const struct bond_opt_value *val;
248
249	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
250			       bond->params.arp_all_targets);
251	return sprintf(buf, "%s %d\n",
252		       val->string, bond->params.arp_all_targets);
253}
254static DEVICE_ATTR(arp_all_targets, 0644,
255		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
256
257/* Show fail_over_mac. */
258static ssize_t bonding_show_fail_over_mac(struct device *d,
259					  struct device_attribute *attr,
260					  char *buf)
261{
262	struct bonding *bond = to_bond(d);
263	const struct bond_opt_value *val;
264
265	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
266			       bond->params.fail_over_mac);
267
268	return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
269}
270static DEVICE_ATTR(fail_over_mac, 0644,
271		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
272
273/* Show the arp timer interval. */
274static ssize_t bonding_show_arp_interval(struct device *d,
275					 struct device_attribute *attr,
276					 char *buf)
277{
278	struct bonding *bond = to_bond(d);
279
280	return sprintf(buf, "%d\n", bond->params.arp_interval);
281}
282static DEVICE_ATTR(arp_interval, 0644,
283		   bonding_show_arp_interval, bonding_sysfs_store_option);
284
285/* Show the arp targets. */
286static ssize_t bonding_show_arp_targets(struct device *d,
287					struct device_attribute *attr,
288					char *buf)
289{
290	struct bonding *bond = to_bond(d);
291	int i, res = 0;
292
293	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
294		if (bond->params.arp_targets[i])
295			res += sprintf(buf + res, "%pI4 ",
296				       &bond->params.arp_targets[i]);
297	}
298	if (res)
299		buf[res-1] = '\n'; /* eat the leftover space */
300
301	return res;
302}
303static DEVICE_ATTR(arp_ip_target, 0644,
304		   bonding_show_arp_targets, bonding_sysfs_store_option);
305
306/* Show the up and down delays. */
307static ssize_t bonding_show_downdelay(struct device *d,
308				      struct device_attribute *attr,
309				      char *buf)
310{
311	struct bonding *bond = to_bond(d);
312
313	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
314}
315static DEVICE_ATTR(downdelay, 0644,
316		   bonding_show_downdelay, bonding_sysfs_store_option);
317
318static ssize_t bonding_show_updelay(struct device *d,
319				    struct device_attribute *attr,
320				    char *buf)
321{
322	struct bonding *bond = to_bond(d);
323
324	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
325
326}
327static DEVICE_ATTR(updelay, 0644,
328		   bonding_show_updelay, bonding_sysfs_store_option);
329
330static ssize_t bonding_show_peer_notif_delay(struct device *d,
331					     struct device_attribute *attr,
332					     char *buf)
333{
334	struct bonding *bond = to_bond(d);
335
336	return sprintf(buf, "%d\n",
337		       bond->params.peer_notif_delay * bond->params.miimon);
338}
339static DEVICE_ATTR(peer_notif_delay, 0644,
340		   bonding_show_peer_notif_delay, bonding_sysfs_store_option);
341
342/* Show the LACP interval. */
343static ssize_t bonding_show_lacp(struct device *d,
344				 struct device_attribute *attr,
345				 char *buf)
346{
347	struct bonding *bond = to_bond(d);
348	const struct bond_opt_value *val;
349
350	val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
351
352	return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
353}
354static DEVICE_ATTR(lacp_rate, 0644,
355		   bonding_show_lacp, bonding_sysfs_store_option);
356
357static ssize_t bonding_show_min_links(struct device *d,
358				      struct device_attribute *attr,
359				      char *buf)
360{
361	struct bonding *bond = to_bond(d);
362
363	return sprintf(buf, "%u\n", bond->params.min_links);
364}
365static DEVICE_ATTR(min_links, 0644,
366		   bonding_show_min_links, bonding_sysfs_store_option);
367
368static ssize_t bonding_show_ad_select(struct device *d,
369				      struct device_attribute *attr,
370				      char *buf)
371{
372	struct bonding *bond = to_bond(d);
373	const struct bond_opt_value *val;
374
375	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
376
377	return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
378}
379static DEVICE_ATTR(ad_select, 0644,
380		   bonding_show_ad_select, bonding_sysfs_store_option);
381
382/* Show the number of peer notifications to send after a failover event. */
383static ssize_t bonding_show_num_peer_notif(struct device *d,
384					   struct device_attribute *attr,
385					   char *buf)
386{
387	struct bonding *bond = to_bond(d);
388	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
389}
390static DEVICE_ATTR(num_grat_arp, 0644,
391		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
392static DEVICE_ATTR(num_unsol_na, 0644,
393		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
394
395/* Show the MII monitor interval. */
396static ssize_t bonding_show_miimon(struct device *d,
397				   struct device_attribute *attr,
398				   char *buf)
399{
400	struct bonding *bond = to_bond(d);
401
402	return sprintf(buf, "%d\n", bond->params.miimon);
403}
404static DEVICE_ATTR(miimon, 0644,
405		   bonding_show_miimon, bonding_sysfs_store_option);
406
407/* Show the primary slave. */
408static ssize_t bonding_show_primary(struct device *d,
409				    struct device_attribute *attr,
410				    char *buf)
411{
412	struct bonding *bond = to_bond(d);
413	struct slave *primary;
414	int count = 0;
415
416	rcu_read_lock();
417	primary = rcu_dereference(bond->primary_slave);
418	if (primary)
419		count = sprintf(buf, "%s\n", primary->dev->name);
420	rcu_read_unlock();
421
422	return count;
423}
424static DEVICE_ATTR(primary, 0644,
425		   bonding_show_primary, bonding_sysfs_store_option);
426
427/* Show the primary_reselect flag. */
428static ssize_t bonding_show_primary_reselect(struct device *d,
429					     struct device_attribute *attr,
430					     char *buf)
431{
432	struct bonding *bond = to_bond(d);
433	const struct bond_opt_value *val;
434
435	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
436			       bond->params.primary_reselect);
437
438	return sprintf(buf, "%s %d\n",
439		       val->string, bond->params.primary_reselect);
440}
441static DEVICE_ATTR(primary_reselect, 0644,
442		   bonding_show_primary_reselect, bonding_sysfs_store_option);
443
444/* Show the use_carrier flag. */
445static ssize_t bonding_show_carrier(struct device *d,
446				    struct device_attribute *attr,
447				    char *buf)
448{
449	struct bonding *bond = to_bond(d);
450
451	return sprintf(buf, "%d\n", bond->params.use_carrier);
452}
453static DEVICE_ATTR(use_carrier, 0644,
454		   bonding_show_carrier, bonding_sysfs_store_option);
455
456
457/* Show currently active_slave. */
458static ssize_t bonding_show_active_slave(struct device *d,
459					 struct device_attribute *attr,
460					 char *buf)
461{
462	struct bonding *bond = to_bond(d);
463	struct net_device *slave_dev;
464	int count = 0;
465
466	rcu_read_lock();
467	slave_dev = bond_option_active_slave_get_rcu(bond);
468	if (slave_dev)
469		count = sprintf(buf, "%s\n", slave_dev->name);
470	rcu_read_unlock();
471
472	return count;
473}
474static DEVICE_ATTR(active_slave, 0644,
475		   bonding_show_active_slave, bonding_sysfs_store_option);
476
477/* Show link status of the bond interface. */
478static ssize_t bonding_show_mii_status(struct device *d,
479				       struct device_attribute *attr,
480				       char *buf)
481{
482	struct bonding *bond = to_bond(d);
483	bool active = netif_carrier_ok(bond->dev);
484
485	return sprintf(buf, "%s\n", active ? "up" : "down");
486}
487static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
488
489/* Show current 802.3ad aggregator ID. */
490static ssize_t bonding_show_ad_aggregator(struct device *d,
491					  struct device_attribute *attr,
492					  char *buf)
493{
494	int count = 0;
495	struct bonding *bond = to_bond(d);
496
497	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
498		struct ad_info ad_info;
499		count = sprintf(buf, "%d\n",
500				bond_3ad_get_active_agg_info(bond, &ad_info)
501				?  0 : ad_info.aggregator_id);
502	}
503
504	return count;
505}
506static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
507
508
509/* Show number of active 802.3ad ports. */
510static ssize_t bonding_show_ad_num_ports(struct device *d,
511					 struct device_attribute *attr,
512					 char *buf)
513{
514	int count = 0;
515	struct bonding *bond = to_bond(d);
516
517	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
518		struct ad_info ad_info;
519		count = sprintf(buf, "%d\n",
520				bond_3ad_get_active_agg_info(bond, &ad_info)
521				?  0 : ad_info.ports);
522	}
523
524	return count;
525}
526static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
527
528
529/* Show current 802.3ad actor key. */
530static ssize_t bonding_show_ad_actor_key(struct device *d,
531					 struct device_attribute *attr,
532					 char *buf)
533{
534	int count = 0;
535	struct bonding *bond = to_bond(d);
536
537	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
538		struct ad_info ad_info;
539		count = sprintf(buf, "%d\n",
540				bond_3ad_get_active_agg_info(bond, &ad_info)
541				?  0 : ad_info.actor_key);
542	}
543
544	return count;
545}
546static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
547
548
549/* Show current 802.3ad partner key. */
550static ssize_t bonding_show_ad_partner_key(struct device *d,
551					   struct device_attribute *attr,
552					   char *buf)
553{
554	int count = 0;
555	struct bonding *bond = to_bond(d);
556
557	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
558		struct ad_info ad_info;
559		count = sprintf(buf, "%d\n",
560				bond_3ad_get_active_agg_info(bond, &ad_info)
561				?  0 : ad_info.partner_key);
562	}
563
564	return count;
565}
566static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
567
568
569/* Show current 802.3ad partner mac. */
570static ssize_t bonding_show_ad_partner_mac(struct device *d,
571					   struct device_attribute *attr,
572					   char *buf)
573{
574	int count = 0;
575	struct bonding *bond = to_bond(d);
576
577	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
578		struct ad_info ad_info;
579		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
580			count = sprintf(buf, "%pM\n", ad_info.partner_system);
581	}
582
583	return count;
584}
585static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
586
587/* Show the queue_ids of the slaves in the current bond. */
588static ssize_t bonding_show_queue_id(struct device *d,
589				     struct device_attribute *attr,
590				     char *buf)
591{
592	struct bonding *bond = to_bond(d);
593	struct list_head *iter;
594	struct slave *slave;
595	int res = 0;
596
597	if (!rtnl_trylock())
598		return restart_syscall();
599
600	bond_for_each_slave(bond, slave, iter) {
601		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
602			/* not enough space for another interface_name:queue_id pair */
603			if ((PAGE_SIZE - res) > 10)
604				res = PAGE_SIZE - 10;
605			res += sprintf(buf + res, "++more++ ");
606			break;
607		}
608		res += sprintf(buf + res, "%s:%d ",
609			       slave->dev->name, slave->queue_id);
610	}
611	if (res)
612		buf[res-1] = '\n'; /* eat the leftover space */
613
614	rtnl_unlock();
615
616	return res;
617}
618static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
619		   bonding_sysfs_store_option);
620
621
622/* Show the all_slaves_active flag. */
623static ssize_t bonding_show_slaves_active(struct device *d,
624					  struct device_attribute *attr,
625					  char *buf)
626{
627	struct bonding *bond = to_bond(d);
628
629	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
630}
631static DEVICE_ATTR(all_slaves_active, 0644,
632		   bonding_show_slaves_active, bonding_sysfs_store_option);
633
634/* Show the number of IGMP membership reports to send on link failure */
635static ssize_t bonding_show_resend_igmp(struct device *d,
636					struct device_attribute *attr,
637					char *buf)
638{
639	struct bonding *bond = to_bond(d);
640
641	return sprintf(buf, "%d\n", bond->params.resend_igmp);
642}
643static DEVICE_ATTR(resend_igmp, 0644,
644		   bonding_show_resend_igmp, bonding_sysfs_store_option);
645
646
647static ssize_t bonding_show_lp_interval(struct device *d,
648					struct device_attribute *attr,
649					char *buf)
650{
651	struct bonding *bond = to_bond(d);
652
653	return sprintf(buf, "%d\n", bond->params.lp_interval);
654}
655static DEVICE_ATTR(lp_interval, 0644,
656		   bonding_show_lp_interval, bonding_sysfs_store_option);
657
658static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
659					   struct device_attribute *attr,
660					   char *buf)
661{
662	struct bonding *bond = to_bond(d);
663	return sprintf(buf, "%d\n", bond->params.tlb_dynamic_lb);
664}
665static DEVICE_ATTR(tlb_dynamic_lb, 0644,
666		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
667
668static ssize_t bonding_show_packets_per_slave(struct device *d,
669					      struct device_attribute *attr,
670					      char *buf)
671{
672	struct bonding *bond = to_bond(d);
673	unsigned int packets_per_slave = bond->params.packets_per_slave;
674
675	return sprintf(buf, "%u\n", packets_per_slave);
676}
677static DEVICE_ATTR(packets_per_slave, 0644,
678		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
679
680static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
681					      struct device_attribute *attr,
682					      char *buf)
683{
684	struct bonding *bond = to_bond(d);
685
686	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
687		return sprintf(buf, "%hu\n", bond->params.ad_actor_sys_prio);
688
689	return 0;
690}
691static DEVICE_ATTR(ad_actor_sys_prio, 0644,
692		   bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
693
694static ssize_t bonding_show_ad_actor_system(struct device *d,
695					    struct device_attribute *attr,
696					    char *buf)
697{
698	struct bonding *bond = to_bond(d);
699
700	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
701		return sprintf(buf, "%pM\n", bond->params.ad_actor_system);
702
703	return 0;
704}
705
706static DEVICE_ATTR(ad_actor_system, 0644,
707		   bonding_show_ad_actor_system, bonding_sysfs_store_option);
708
709static ssize_t bonding_show_ad_user_port_key(struct device *d,
710					     struct device_attribute *attr,
711					     char *buf)
712{
713	struct bonding *bond = to_bond(d);
714
715	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
716		return sprintf(buf, "%hu\n", bond->params.ad_user_port_key);
717
718	return 0;
719}
720static DEVICE_ATTR(ad_user_port_key, 0644,
721		   bonding_show_ad_user_port_key, bonding_sysfs_store_option);
722
723static struct attribute *per_bond_attrs[] = {
724	&dev_attr_slaves.attr,
725	&dev_attr_mode.attr,
726	&dev_attr_fail_over_mac.attr,
727	&dev_attr_arp_validate.attr,
728	&dev_attr_arp_all_targets.attr,
729	&dev_attr_arp_interval.attr,
730	&dev_attr_arp_ip_target.attr,
731	&dev_attr_downdelay.attr,
732	&dev_attr_updelay.attr,
733	&dev_attr_peer_notif_delay.attr,
734	&dev_attr_lacp_rate.attr,
735	&dev_attr_ad_select.attr,
736	&dev_attr_xmit_hash_policy.attr,
737	&dev_attr_num_grat_arp.attr,
738	&dev_attr_num_unsol_na.attr,
739	&dev_attr_miimon.attr,
740	&dev_attr_primary.attr,
741	&dev_attr_primary_reselect.attr,
742	&dev_attr_use_carrier.attr,
743	&dev_attr_active_slave.attr,
744	&dev_attr_mii_status.attr,
745	&dev_attr_ad_aggregator.attr,
746	&dev_attr_ad_num_ports.attr,
747	&dev_attr_ad_actor_key.attr,
748	&dev_attr_ad_partner_key.attr,
749	&dev_attr_ad_partner_mac.attr,
750	&dev_attr_queue_id.attr,
751	&dev_attr_all_slaves_active.attr,
752	&dev_attr_resend_igmp.attr,
753	&dev_attr_min_links.attr,
754	&dev_attr_lp_interval.attr,
755	&dev_attr_packets_per_slave.attr,
756	&dev_attr_tlb_dynamic_lb.attr,
757	&dev_attr_ad_actor_sys_prio.attr,
758	&dev_attr_ad_actor_system.attr,
759	&dev_attr_ad_user_port_key.attr,
760	NULL,
761};
762
763static const struct attribute_group bonding_group = {
764	.name = "bonding",
765	.attrs = per_bond_attrs,
766};
767
768/* Initialize sysfs.  This sets up the bonding_masters file in
769 * /sys/class/net.
770 */
771int bond_create_sysfs(struct bond_net *bn)
772{
773	int ret;
774
775	bn->class_attr_bonding_masters = class_attr_bonding_masters;
776	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
777
778	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
779					  bn->net);
780	/* Permit multiple loads of the module by ignoring failures to
781	 * create the bonding_masters sysfs file.  Bonding devices
782	 * created by second or subsequent loads of the module will
783	 * not be listed in, or controllable by, bonding_masters, but
784	 * will have the usual "bonding" sysfs directory.
785	 *
786	 * This is done to preserve backwards compatibility for
787	 * initscripts/sysconfig, which load bonding multiple times to
788	 * configure multiple bonding devices.
789	 */
790	if (ret == -EEXIST) {
791		/* Is someone being kinky and naming a device bonding_master? */
792		if (__dev_get_by_name(bn->net,
793				      class_attr_bonding_masters.attr.name))
794			pr_err("network device named %s already exists in sysfs\n",
795			       class_attr_bonding_masters.attr.name);
796		ret = 0;
797	}
798
799	return ret;
800
801}
802
803/* Remove /sys/class/net/bonding_masters. */
804void bond_destroy_sysfs(struct bond_net *bn)
805{
806	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
807}
808
809/* Initialize sysfs for each bond.  This sets up and registers
810 * the 'bondctl' directory for each individual bond under /sys/class/net.
811 */
812void bond_prepare_sysfs_group(struct bonding *bond)
813{
814	bond->dev->sysfs_groups[0] = &bonding_group;
815}
816