Linux Audio

Check our new training course

Loading...
v3.15
 
   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.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 "bonding.h"
  44
  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/*
  49 * "show" function for the bond_masters attribute.
  50 * The class parameter is ignored.
  51 */
  52static ssize_t bonding_show_bonds(struct class *cls,
  53				  struct class_attribute *attr,
  54				  char *buf)
  55{
  56	struct bond_net *bn =
  57		container_of(attr, struct bond_net, class_attr_bonding_masters);
  58	int res = 0;
  59	struct bonding *bond;
 
  60
  61	rtnl_lock();
  62
  63	list_for_each_entry(bond, &bn->dev_list, bond_list) {
  64		if (res > (PAGE_SIZE - IFNAMSIZ)) {
  65			/* not enough space for another interface name */
  66			if ((PAGE_SIZE - res) > 10)
  67				res = PAGE_SIZE - 10;
  68			res += sprintf(buf + res, "++more++ ");
  69			break;
  70		}
  71		res += sprintf(buf + res, "%s ", bond->dev->name);
  72	}
  73	if (res)
  74		buf[res-1] = '\n'; /* eat the leftover space */
  75
  76	rtnl_unlock();
  77	return res;
  78}
  79
  80static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
  81{
  82	struct bonding *bond;
  83
  84	list_for_each_entry(bond, &bn->dev_list, bond_list) {
  85		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
  86			return bond->dev;
  87	}
  88	return NULL;
  89}
  90
  91/*
  92 * "store" function for the bond_masters attribute.  This is what
  93 * creates and deletes entire bonds.
  94 *
  95 * The class parameter is ignored.
  96 *
  97 */
  98
  99static ssize_t bonding_store_bonds(struct class *cls,
 100				   struct class_attribute *attr,
 101				   const char *buffer, size_t count)
 102{
 103	struct bond_net *bn =
 104		container_of(attr, struct bond_net, class_attr_bonding_masters);
 105	char command[IFNAMSIZ + 1] = {0, };
 106	char *ifname;
 107	int rv, res = count;
 108
 109	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
 110	ifname = command + 1;
 111	if ((strlen(command) <= 1) ||
 112	    !dev_valid_name(ifname))
 113		goto err_no_cmd;
 114
 115	if (command[0] == '+') {
 116		pr_info("%s is being created...\n", ifname);
 117		rv = bond_create(bn->net, ifname);
 118		if (rv) {
 119			if (rv == -EEXIST)
 120				pr_info("%s already exists\n", ifname);
 121			else
 122				pr_info("%s creation failed\n", ifname);
 123			res = rv;
 124		}
 125	} else if (command[0] == '-') {
 126		struct net_device *bond_dev;
 127
 128		rtnl_lock();
 129		bond_dev = bond_get_by_name(bn, ifname);
 130		if (bond_dev) {
 131			pr_info("%s is being deleted...\n", ifname);
 132			unregister_netdevice(bond_dev);
 133		} else {
 134			pr_err("unable to delete non-existent %s\n", ifname);
 135			res = -ENODEV;
 136		}
 137		rtnl_unlock();
 138	} else
 139		goto err_no_cmd;
 140
 141	/* Always return either count or an error.  If you return 0, you'll
 142	 * get called forever, which is bad.
 143	 */
 144	return res;
 145
 146err_no_cmd:
 147	pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
 148	return -EPERM;
 149}
 150
 151/* class attribute for bond_masters file.  This ends up in /sys/class/net */
 152static const struct class_attribute class_attr_bonding_masters = {
 153	.attr = {
 154		.name = "bonding_masters",
 155		.mode = S_IWUSR | S_IRUGO,
 156	},
 157	.show = bonding_show_bonds,
 158	.store = bonding_store_bonds,
 159};
 160
 161/*
 162 * Show the slaves in the current bond.
 163 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 164static ssize_t bonding_show_slaves(struct device *d,
 165				   struct device_attribute *attr, char *buf)
 166{
 167	struct bonding *bond = to_bond(d);
 168	struct list_head *iter;
 169	struct slave *slave;
 170	int res = 0;
 171
 172	if (!rtnl_trylock())
 173		return restart_syscall();
 174
 175	bond_for_each_slave(bond, slave, iter) {
 176		if (res > (PAGE_SIZE - IFNAMSIZ)) {
 177			/* not enough space for another interface name */
 178			if ((PAGE_SIZE - res) > 10)
 179				res = PAGE_SIZE - 10;
 180			res += sprintf(buf + res, "++more++ ");
 181			break;
 182		}
 183		res += sprintf(buf + res, "%s ", slave->dev->name);
 184	}
 185
 186	rtnl_unlock();
 187
 188	if (res)
 189		buf[res-1] = '\n'; /* eat the leftover space */
 190
 191	return res;
 192}
 
 
 193
 194/*
 195 * Set the slaves in the current bond.
 196 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
 197 * All hard work should be done there.
 198 */
 199static ssize_t bonding_store_slaves(struct device *d,
 200				    struct device_attribute *attr,
 201				    const char *buffer, size_t count)
 202{
 203	struct bonding *bond = to_bond(d);
 204	int ret;
 205
 206	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_SLAVES, (char *)buffer);
 207	if (!ret)
 208		ret = count;
 209
 210	return ret;
 211}
 212static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
 213		   bonding_store_slaves);
 214
 215/*
 216 * Show and set the bonding mode.  The bond interface must be down to
 217 * change the mode.
 218 */
 219static ssize_t bonding_show_mode(struct device *d,
 220				 struct device_attribute *attr, char *buf)
 221{
 222	struct bonding *bond = to_bond(d);
 223	const struct bond_opt_value *val;
 224
 225	val = bond_opt_get_val(BOND_OPT_MODE, bond->params.mode);
 226
 227	return sprintf(buf, "%s %d\n", val->string, bond->params.mode);
 228}
 229
 230static ssize_t bonding_store_mode(struct device *d,
 231				  struct device_attribute *attr,
 232				  const char *buf, size_t count)
 233{
 234	struct bonding *bond = to_bond(d);
 235	int ret;
 236
 237	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MODE, (char *)buf);
 238	if (!ret)
 239		ret = count;
 240
 241	return ret;
 242}
 243static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
 244		   bonding_show_mode, bonding_store_mode);
 245
 246/*
 247 * Show and set the bonding transmit hash method.
 248 */
 249static ssize_t bonding_show_xmit_hash(struct device *d,
 250				      struct device_attribute *attr,
 251				      char *buf)
 252{
 253	struct bonding *bond = to_bond(d);
 254	const struct bond_opt_value *val;
 255
 256	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
 257
 258	return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
 259}
 
 
 260
 261static ssize_t bonding_store_xmit_hash(struct device *d,
 262				       struct device_attribute *attr,
 263				       const char *buf, size_t count)
 264{
 265	struct bonding *bond = to_bond(d);
 266	int ret;
 267
 268	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_XMIT_HASH, (char *)buf);
 269	if (!ret)
 270		ret = count;
 271
 272	return ret;
 273}
 274static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
 275		   bonding_show_xmit_hash, bonding_store_xmit_hash);
 276
 277/*
 278 * Show and set arp_validate.
 279 */
 280static ssize_t bonding_show_arp_validate(struct device *d,
 281					 struct device_attribute *attr,
 282					 char *buf)
 283{
 284	struct bonding *bond = to_bond(d);
 285	const struct bond_opt_value *val;
 286
 287	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
 288			       bond->params.arp_validate);
 289
 290	return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
 291}
 
 
 292
 293static ssize_t bonding_store_arp_validate(struct device *d,
 294					  struct device_attribute *attr,
 295					  const char *buf, size_t count)
 296{
 297	struct bonding *bond = to_bond(d);
 298	int ret;
 299
 300	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_VALIDATE, (char *)buf);
 301	if (!ret)
 302		ret = count;
 303
 304	return ret;
 305}
 306
 307static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
 308		   bonding_store_arp_validate);
 309/*
 310 * Show and set arp_all_targets.
 311 */
 312static ssize_t bonding_show_arp_all_targets(struct device *d,
 313					 struct device_attribute *attr,
 314					 char *buf)
 315{
 316	struct bonding *bond = to_bond(d);
 317	const struct bond_opt_value *val;
 318
 319	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
 320			       bond->params.arp_all_targets);
 321	return sprintf(buf, "%s %d\n",
 322		       val->string, bond->params.arp_all_targets);
 323}
 
 
 324
 325static ssize_t bonding_store_arp_all_targets(struct device *d,
 326					  struct device_attribute *attr,
 327					  const char *buf, size_t count)
 328{
 329	struct bonding *bond = to_bond(d);
 330	int ret;
 331
 332	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_ALL_TARGETS, (char *)buf);
 333	if (!ret)
 334		ret = count;
 335
 336	return ret;
 337}
 338
 339static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
 340		   bonding_show_arp_all_targets, bonding_store_arp_all_targets);
 341
 342/*
 343 * Show and store fail_over_mac.  User only allowed to change the
 344 * value when there are no slaves.
 345 */
 346static ssize_t bonding_show_fail_over_mac(struct device *d,
 347					  struct device_attribute *attr,
 348					  char *buf)
 349{
 350	struct bonding *bond = to_bond(d);
 351	const struct bond_opt_value *val;
 352
 353	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
 354			       bond->params.fail_over_mac);
 355
 356	return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
 357}
 
 
 358
 359static ssize_t bonding_store_fail_over_mac(struct device *d,
 360					   struct device_attribute *attr,
 361					   const char *buf, size_t count)
 362{
 363	struct bonding *bond = to_bond(d);
 364	int ret;
 365
 366	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_FAIL_OVER_MAC, (char *)buf);
 367	if (!ret)
 368		ret = count;
 369
 370	return ret;
 371}
 372
 373static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
 374		   bonding_show_fail_over_mac, bonding_store_fail_over_mac);
 375
 376/*
 377 * Show and set the arp timer interval.  There are two tricky bits
 378 * here.  First, if ARP monitoring is activated, then we must disable
 379 * MII monitoring.  Second, if the ARP timer isn't running, we must
 380 * start it.
 381 */
 382static ssize_t bonding_show_arp_interval(struct device *d,
 383					 struct device_attribute *attr,
 384					 char *buf)
 385{
 386	struct bonding *bond = to_bond(d);
 387
 388	return sprintf(buf, "%d\n", bond->params.arp_interval);
 389}
 390
 391static ssize_t bonding_store_arp_interval(struct device *d,
 392					  struct device_attribute *attr,
 393					  const char *buf, size_t count)
 394{
 395	struct bonding *bond = to_bond(d);
 396	int ret;
 397
 398	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_INTERVAL, (char *)buf);
 399	if (!ret)
 400		ret = count;
 401
 402	return ret;
 403}
 404static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
 405		   bonding_show_arp_interval, bonding_store_arp_interval);
 406
 407/*
 408 * Show and set the arp targets.
 409 */
 410static ssize_t bonding_show_arp_targets(struct device *d,
 411					struct device_attribute *attr,
 412					char *buf)
 413{
 414	struct bonding *bond = to_bond(d);
 415	int i, res = 0;
 416
 417	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
 418		if (bond->params.arp_targets[i])
 419			res += sprintf(buf + res, "%pI4 ",
 420				       &bond->params.arp_targets[i]);
 421	}
 422	if (res)
 423		buf[res-1] = '\n'; /* eat the leftover space */
 424
 425	return res;
 426}
 
 
 427
 428static ssize_t bonding_store_arp_targets(struct device *d,
 429					 struct device_attribute *attr,
 430					 const char *buf, size_t count)
 
 431{
 432	struct bonding *bond = to_bond(d);
 433	int ret;
 434
 435	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_TARGETS, (char *)buf);
 436	if (!ret)
 437		ret = count;
 438
 439	return ret;
 440}
 441static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
 
 442
 443/*
 444 * Show and set the up and down delays.  These must be multiples of the
 445 * MII monitoring value, and are stored internally as the multiplier.
 446 * Thus, we must translate to MS for the real world.
 447 */
 448static ssize_t bonding_show_downdelay(struct device *d,
 449				      struct device_attribute *attr,
 450				      char *buf)
 451{
 452	struct bonding *bond = to_bond(d);
 453
 454	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
 455}
 456
 457static ssize_t bonding_store_downdelay(struct device *d,
 458				       struct device_attribute *attr,
 459				       const char *buf, size_t count)
 460{
 461	struct bonding *bond = to_bond(d);
 462	int ret;
 463
 464	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_DOWNDELAY, (char *)buf);
 465	if (!ret)
 466		ret = count;
 467
 468	return ret;
 469}
 470static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
 471		   bonding_show_downdelay, bonding_store_downdelay);
 472
 473static ssize_t bonding_show_updelay(struct device *d,
 474				    struct device_attribute *attr,
 475				    char *buf)
 476{
 477	struct bonding *bond = to_bond(d);
 478
 479	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
 480
 481}
 
 
 482
 483static ssize_t bonding_store_updelay(struct device *d,
 484				     struct device_attribute *attr,
 485				     const char *buf, size_t count)
 486{
 487	struct bonding *bond = to_bond(d);
 488	int ret;
 489
 490	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_UPDELAY, (char *)buf);
 491	if (!ret)
 492		ret = count;
 493
 494	return ret;
 495}
 496static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
 497		   bonding_show_updelay, bonding_store_updelay);
 498
 499/*
 500 * Show and set the LACP interval.  Interface must be down, and the mode
 501 * must be set to 802.3ad mode.
 502 */
 503static ssize_t bonding_show_lacp(struct device *d,
 504				 struct device_attribute *attr,
 505				 char *buf)
 506{
 507	struct bonding *bond = to_bond(d);
 508	const struct bond_opt_value *val;
 509
 510	val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
 511
 512	return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
 513}
 
 
 514
 515static ssize_t bonding_store_lacp(struct device *d,
 516				  struct device_attribute *attr,
 517				  const char *buf, size_t count)
 518{
 519	struct bonding *bond = to_bond(d);
 520	int ret;
 521
 522	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_LACP_RATE, (char *)buf);
 523	if (!ret)
 524		ret = count;
 525
 526	return ret;
 527}
 528static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
 529		   bonding_show_lacp, bonding_store_lacp);
 530
 531static ssize_t bonding_show_min_links(struct device *d,
 532				      struct device_attribute *attr,
 533				      char *buf)
 534{
 535	struct bonding *bond = to_bond(d);
 536
 537	return sprintf(buf, "%u\n", bond->params.min_links);
 538}
 539
 540static ssize_t bonding_store_min_links(struct device *d,
 541				       struct device_attribute *attr,
 542				       const char *buf, size_t count)
 543{
 544	struct bonding *bond = to_bond(d);
 545	int ret;
 546
 547	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MINLINKS, (char *)buf);
 548	if (!ret)
 549		ret = count;
 550
 551	return ret;
 552}
 553static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
 554		   bonding_show_min_links, bonding_store_min_links);
 555
 556static ssize_t bonding_show_ad_select(struct device *d,
 557				      struct device_attribute *attr,
 558				      char *buf)
 559{
 560	struct bonding *bond = to_bond(d);
 561	const struct bond_opt_value *val;
 562
 563	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
 564
 565	return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
 566}
 
 
 567
 568
 569static ssize_t bonding_store_ad_select(struct device *d,
 570				       struct device_attribute *attr,
 571				       const char *buf, size_t count)
 572{
 573	struct bonding *bond = to_bond(d);
 574	int ret;
 575
 576	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_AD_SELECT, (char *)buf);
 577	if (!ret)
 578		ret = count;
 579
 580	return ret;
 581}
 582static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
 583		   bonding_show_ad_select, bonding_store_ad_select);
 584
 585/*
 586 * Show and set the number of peer notifications to send after a failover event.
 587 */
 588static ssize_t bonding_show_num_peer_notif(struct device *d,
 589					   struct device_attribute *attr,
 590					   char *buf)
 591{
 592	struct bonding *bond = to_bond(d);
 593	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
 594}
 595
 596static ssize_t bonding_store_num_peer_notif(struct device *d,
 597					    struct device_attribute *attr,
 598					    const char *buf, size_t count)
 599{
 600	struct bonding *bond = to_bond(d);
 601	int ret;
 602
 603	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_NUM_PEER_NOTIF, (char *)buf);
 604	if (!ret)
 605		ret = count;
 606
 607	return ret;
 608}
 609static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
 610		   bonding_show_num_peer_notif, bonding_store_num_peer_notif);
 611static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
 612		   bonding_show_num_peer_notif, bonding_store_num_peer_notif);
 613
 614/*
 615 * Show and set the MII monitor interval.  There are two tricky bits
 616 * here.  First, if MII monitoring is activated, then we must disable
 617 * ARP monitoring.  Second, if the timer isn't running, we must
 618 * start it.
 619 */
 620static ssize_t bonding_show_miimon(struct device *d,
 621				   struct device_attribute *attr,
 622				   char *buf)
 623{
 624	struct bonding *bond = to_bond(d);
 625
 626	return sprintf(buf, "%d\n", bond->params.miimon);
 627}
 628
 629static ssize_t bonding_store_miimon(struct device *d,
 630				    struct device_attribute *attr,
 631				    const char *buf, size_t count)
 632{
 633	struct bonding *bond = to_bond(d);
 634	int ret;
 635
 636	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MIIMON, (char *)buf);
 637	if (!ret)
 638		ret = count;
 639
 640	return ret;
 641}
 642static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
 643		   bonding_show_miimon, bonding_store_miimon);
 644
 645/*
 646 * Show and set the primary slave.  The store function is much
 647 * simpler than bonding_store_slaves function because it only needs to
 648 * handle one interface name.
 649 * The bond must be a mode that supports a primary for this be
 650 * set.
 651 */
 652static ssize_t bonding_show_primary(struct device *d,
 653				    struct device_attribute *attr,
 654				    char *buf)
 655{
 656	int count = 0;
 657	struct bonding *bond = to_bond(d);
 
 
 658
 659	if (bond->primary_slave)
 660		count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
 
 
 
 661
 662	return count;
 663}
 
 
 664
 665static ssize_t bonding_store_primary(struct device *d,
 666				     struct device_attribute *attr,
 667				     const char *buf, size_t count)
 668{
 669	struct bonding *bond = to_bond(d);
 670	int ret;
 671
 672	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PRIMARY, (char *)buf);
 673	if (!ret)
 674		ret = count;
 675
 676	return ret;
 677}
 678static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
 679		   bonding_show_primary, bonding_store_primary);
 680
 681/*
 682 * Show and set the primary_reselect flag.
 683 */
 684static ssize_t bonding_show_primary_reselect(struct device *d,
 685					     struct device_attribute *attr,
 686					     char *buf)
 687{
 688	struct bonding *bond = to_bond(d);
 689	const struct bond_opt_value *val;
 690
 691	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
 692			       bond->params.primary_reselect);
 693
 694	return sprintf(buf, "%s %d\n",
 695		       val->string, bond->params.primary_reselect);
 696}
 
 
 697
 698static ssize_t bonding_store_primary_reselect(struct device *d,
 699					      struct device_attribute *attr,
 700					      const char *buf, size_t count)
 701{
 702	struct bonding *bond = to_bond(d);
 703	int ret;
 704
 705	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PRIMARY_RESELECT,
 706				   (char *)buf);
 707	if (!ret)
 708		ret = count;
 709
 710	return ret;
 711}
 712static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
 713		   bonding_show_primary_reselect,
 714		   bonding_store_primary_reselect);
 715
 716/*
 717 * Show and set the use_carrier flag.
 718 */
 719static ssize_t bonding_show_carrier(struct device *d,
 720				    struct device_attribute *attr,
 721				    char *buf)
 722{
 723	struct bonding *bond = to_bond(d);
 724
 725	return sprintf(buf, "%d\n", bond->params.use_carrier);
 726}
 
 
 727
 728static ssize_t bonding_store_carrier(struct device *d,
 729				     struct device_attribute *attr,
 730				     const char *buf, size_t count)
 731{
 732	struct bonding *bond = to_bond(d);
 733	int ret;
 734
 735	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_USE_CARRIER, (char *)buf);
 736	if (!ret)
 737		ret = count;
 738
 739	return ret;
 740}
 741static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
 742		   bonding_show_carrier, bonding_store_carrier);
 743
 744
 745/*
 746 * Show and set currently active_slave.
 747 */
 748static ssize_t bonding_show_active_slave(struct device *d,
 749					 struct device_attribute *attr,
 750					 char *buf)
 751{
 752	struct bonding *bond = to_bond(d);
 753	struct net_device *slave_dev;
 754	int count = 0;
 755
 756	rcu_read_lock();
 757	slave_dev = bond_option_active_slave_get_rcu(bond);
 758	if (slave_dev)
 759		count = sprintf(buf, "%s\n", slave_dev->name);
 760	rcu_read_unlock();
 761
 762	return count;
 763}
 
 
 764
 765static ssize_t bonding_store_active_slave(struct device *d,
 766					  struct device_attribute *attr,
 767					  const char *buf, size_t count)
 768{
 769	struct bonding *bond = to_bond(d);
 770	int ret;
 771
 772	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ACTIVE_SLAVE, (char *)buf);
 773	if (!ret)
 774		ret = count;
 775
 776	return ret;
 777}
 778static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
 779		   bonding_show_active_slave, bonding_store_active_slave);
 780
 781
 782/*
 783 * Show link status of the bond interface.
 784 */
 785static ssize_t bonding_show_mii_status(struct device *d,
 786				       struct device_attribute *attr,
 787				       char *buf)
 788{
 789	struct bonding *bond = to_bond(d);
 
 790
 791	return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
 792}
 793static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
 794
 795/*
 796 * Show current 802.3ad aggregator ID.
 797 */
 798static ssize_t bonding_show_ad_aggregator(struct device *d,
 799					  struct device_attribute *attr,
 800					  char *buf)
 801{
 802	int count = 0;
 803	struct bonding *bond = to_bond(d);
 804
 805	if (bond->params.mode == BOND_MODE_8023AD) {
 806		struct ad_info ad_info;
 807		count = sprintf(buf, "%d\n",
 808				bond_3ad_get_active_agg_info(bond, &ad_info)
 809				?  0 : ad_info.aggregator_id);
 
 810	}
 811
 812	return count;
 813}
 814static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
 815
 816
 817/*
 818 * Show number of active 802.3ad ports.
 819 */
 820static ssize_t bonding_show_ad_num_ports(struct device *d,
 821					 struct device_attribute *attr,
 822					 char *buf)
 823{
 824	int count = 0;
 825	struct bonding *bond = to_bond(d);
 826
 827	if (bond->params.mode == BOND_MODE_8023AD) {
 828		struct ad_info ad_info;
 829		count = sprintf(buf, "%d\n",
 830				bond_3ad_get_active_agg_info(bond, &ad_info)
 831				?  0 : ad_info.ports);
 
 832	}
 833
 834	return count;
 835}
 836static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
 837
 838
 839/*
 840 * Show current 802.3ad actor key.
 841 */
 842static ssize_t bonding_show_ad_actor_key(struct device *d,
 843					 struct device_attribute *attr,
 844					 char *buf)
 845{
 846	int count = 0;
 847	struct bonding *bond = to_bond(d);
 848
 849	if (bond->params.mode == BOND_MODE_8023AD) {
 850		struct ad_info ad_info;
 851		count = sprintf(buf, "%d\n",
 852				bond_3ad_get_active_agg_info(bond, &ad_info)
 853				?  0 : ad_info.actor_key);
 
 854	}
 855
 856	return count;
 857}
 858static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
 859
 860
 861/*
 862 * Show current 802.3ad partner key.
 863 */
 864static ssize_t bonding_show_ad_partner_key(struct device *d,
 865					   struct device_attribute *attr,
 866					   char *buf)
 867{
 868	int count = 0;
 869	struct bonding *bond = to_bond(d);
 870
 871	if (bond->params.mode == BOND_MODE_8023AD) {
 872		struct ad_info ad_info;
 873		count = sprintf(buf, "%d\n",
 874				bond_3ad_get_active_agg_info(bond, &ad_info)
 875				?  0 : ad_info.partner_key);
 
 876	}
 877
 878	return count;
 879}
 880static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
 881
 882
 883/*
 884 * Show current 802.3ad partner mac.
 885 */
 886static ssize_t bonding_show_ad_partner_mac(struct device *d,
 887					   struct device_attribute *attr,
 888					   char *buf)
 889{
 890	int count = 0;
 891	struct bonding *bond = to_bond(d);
 892
 893	if (bond->params.mode == BOND_MODE_8023AD) {
 894		struct ad_info ad_info;
 
 895		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
 896			count = sprintf(buf, "%pM\n", ad_info.partner_system);
 897	}
 898
 899	return count;
 900}
 901static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
 902
 903/*
 904 * Show the queue_ids of the slaves in the current bond.
 905 */
 906static ssize_t bonding_show_queue_id(struct device *d,
 907				     struct device_attribute *attr,
 908				     char *buf)
 909{
 910	struct bonding *bond = to_bond(d);
 911	struct list_head *iter;
 912	struct slave *slave;
 913	int res = 0;
 914
 915	if (!rtnl_trylock())
 916		return restart_syscall();
 917
 918	bond_for_each_slave(bond, slave, iter) {
 919		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
 920			/* not enough space for another interface_name:queue_id pair */
 921			if ((PAGE_SIZE - res) > 10)
 922				res = PAGE_SIZE - 10;
 923			res += sprintf(buf + res, "++more++ ");
 924			break;
 925		}
 926		res += sprintf(buf + res, "%s:%d ",
 927			       slave->dev->name, slave->queue_id);
 
 928	}
 929	if (res)
 930		buf[res-1] = '\n'; /* eat the leftover space */
 931
 932	rtnl_unlock();
 933
 934	return res;
 935}
 936
 937/*
 938 * Set the queue_ids of the  slaves in the current bond.  The bond
 939 * interface must be enslaved for this to work.
 940 */
 941static ssize_t bonding_store_queue_id(struct device *d,
 942				      struct device_attribute *attr,
 943				      const char *buffer, size_t count)
 944{
 945	struct bonding *bond = to_bond(d);
 946	int ret;
 947
 948	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_QUEUE_ID, (char *)buffer);
 949	if (!ret)
 950		ret = count;
 951
 952	return ret;
 953}
 954static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
 955		   bonding_store_queue_id);
 956
 957
 958/*
 959 * Show and set the all_slaves_active flag.
 960 */
 961static ssize_t bonding_show_slaves_active(struct device *d,
 962					  struct device_attribute *attr,
 963					  char *buf)
 964{
 965	struct bonding *bond = to_bond(d);
 966
 967	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
 968}
 
 
 969
 970static ssize_t bonding_store_slaves_active(struct device *d,
 971					   struct device_attribute *attr,
 972					   const char *buf, size_t count)
 
 973{
 974	struct bonding *bond = to_bond(d);
 975	int ret;
 976
 977	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ALL_SLAVES_ACTIVE,
 978				   (char *)buf);
 979	if (!ret)
 980		ret = count;
 981
 982	return ret;
 983}
 984static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
 985		   bonding_show_slaves_active, bonding_store_slaves_active);
 986
 987/*
 988 * Show and set the number of IGMP membership reports to send on link failure
 989 */
 990static ssize_t bonding_show_resend_igmp(struct device *d,
 991					struct device_attribute *attr,
 992					char *buf)
 993{
 994	struct bonding *bond = to_bond(d);
 995
 996	return sprintf(buf, "%d\n", bond->params.resend_igmp);
 997}
 
 
 998
 999static ssize_t bonding_store_resend_igmp(struct device *d,
1000					 struct device_attribute *attr,
1001					 const char *buf, size_t count)
1002{
1003	struct bonding *bond = to_bond(d);
1004	int ret;
1005
1006	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_RESEND_IGMP, (char *)buf);
1007	if (!ret)
1008		ret = count;
1009
1010	return ret;
1011}
 
 
1012
1013static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1014		   bonding_show_resend_igmp, bonding_store_resend_igmp);
1015
1016
1017static ssize_t bonding_show_lp_interval(struct device *d,
1018					struct device_attribute *attr,
1019					char *buf)
1020{
1021	struct bonding *bond = to_bond(d);
1022	return sprintf(buf, "%d\n", bond->params.lp_interval);
 
 
1023}
 
 
1024
1025static ssize_t bonding_store_lp_interval(struct device *d,
1026					 struct device_attribute *attr,
1027					 const char *buf, size_t count)
1028{
1029	struct bonding *bond = to_bond(d);
1030	int ret;
1031
1032	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_LP_INTERVAL, (char *)buf);
1033	if (!ret)
1034		ret = count;
1035
1036	return ret;
1037}
 
 
1038
1039static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1040		   bonding_show_lp_interval, bonding_store_lp_interval);
1041
1042static ssize_t bonding_show_packets_per_slave(struct device *d,
1043					      struct device_attribute *attr,
1044					      char *buf)
1045{
1046	struct bonding *bond = to_bond(d);
1047	unsigned int packets_per_slave = bond->params.packets_per_slave;
1048	return sprintf(buf, "%u\n", packets_per_slave);
 
 
 
1049}
1050
1051static ssize_t bonding_store_packets_per_slave(struct device *d,
1052					       struct device_attribute *attr,
1053					       const char *buf, size_t count)
 
 
 
1054{
1055	struct bonding *bond = to_bond(d);
1056	int ret;
1057
1058	ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PACKETS_PER_SLAVE,
1059				   (char *)buf);
1060	if (!ret)
1061		ret = count;
1062
1063	return ret;
1064}
1065
1066static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1067		   bonding_show_packets_per_slave,
1068		   bonding_store_packets_per_slave);
1069
1070static struct attribute *per_bond_attrs[] = {
1071	&dev_attr_slaves.attr,
1072	&dev_attr_mode.attr,
1073	&dev_attr_fail_over_mac.attr,
1074	&dev_attr_arp_validate.attr,
1075	&dev_attr_arp_all_targets.attr,
1076	&dev_attr_arp_interval.attr,
1077	&dev_attr_arp_ip_target.attr,
1078	&dev_attr_downdelay.attr,
1079	&dev_attr_updelay.attr,
 
 
1080	&dev_attr_lacp_rate.attr,
1081	&dev_attr_ad_select.attr,
1082	&dev_attr_xmit_hash_policy.attr,
1083	&dev_attr_num_grat_arp.attr,
1084	&dev_attr_num_unsol_na.attr,
1085	&dev_attr_miimon.attr,
1086	&dev_attr_primary.attr,
1087	&dev_attr_primary_reselect.attr,
1088	&dev_attr_use_carrier.attr,
1089	&dev_attr_active_slave.attr,
1090	&dev_attr_mii_status.attr,
1091	&dev_attr_ad_aggregator.attr,
1092	&dev_attr_ad_num_ports.attr,
1093	&dev_attr_ad_actor_key.attr,
1094	&dev_attr_ad_partner_key.attr,
1095	&dev_attr_ad_partner_mac.attr,
1096	&dev_attr_queue_id.attr,
1097	&dev_attr_all_slaves_active.attr,
1098	&dev_attr_resend_igmp.attr,
1099	&dev_attr_min_links.attr,
1100	&dev_attr_lp_interval.attr,
1101	&dev_attr_packets_per_slave.attr,
 
 
 
 
 
1102	NULL,
1103};
1104
1105static struct attribute_group bonding_group = {
1106	.name = "bonding",
1107	.attrs = per_bond_attrs,
1108};
1109
1110/*
1111 * Initialize sysfs.  This sets up the bonding_masters file in
1112 * /sys/class/net.
1113 */
1114int bond_create_sysfs(struct bond_net *bn)
1115{
1116	int ret;
1117
1118	bn->class_attr_bonding_masters = class_attr_bonding_masters;
1119	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1120
1121	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1122					  bn->net);
1123	/*
1124	 * Permit multiple loads of the module by ignoring failures to
1125	 * create the bonding_masters sysfs file.  Bonding devices
1126	 * created by second or subsequent loads of the module will
1127	 * not be listed in, or controllable by, bonding_masters, but
1128	 * will have the usual "bonding" sysfs directory.
1129	 *
1130	 * This is done to preserve backwards compatibility for
1131	 * initscripts/sysconfig, which load bonding multiple times to
1132	 * configure multiple bonding devices.
1133	 */
1134	if (ret == -EEXIST) {
1135		/* Is someone being kinky and naming a device bonding_master? */
1136		if (__dev_get_by_name(bn->net,
1137				      class_attr_bonding_masters.attr.name))
1138			pr_err("network device named %s already exists in sysfs\n",
1139			       class_attr_bonding_masters.attr.name);
1140		ret = 0;
1141	}
1142
1143	return ret;
1144
1145}
1146
1147/*
1148 * Remove /sys/class/net/bonding_masters.
1149 */
1150void bond_destroy_sysfs(struct bond_net *bn)
1151{
1152	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1153}
1154
1155/*
1156 * Initialize sysfs for each bond.  This sets up and registers
1157 * the 'bondctl' directory for each individual bond under /sys/class/net.
1158 */
1159void bond_prepare_sysfs_group(struct bonding *bond)
1160{
1161	bond->dev->sysfs_groups[0] = &bonding_group;
1162}
1163
v6.13.7
  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(const struct class *cls,
 35				  const struct class_attribute *attr,
 36				  char *buf)
 37{
 38	const struct bond_net *bn =
 39		container_of_const(attr, struct bond_net, class_attr_bonding_masters);
 
 40	struct bonding *bond;
 41	int res = 0;
 42
 43	rcu_read_lock();
 44
 45	list_for_each_entry_rcu(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 += sysfs_emit_at(buf, res, "++more++ ");
 51			break;
 52		}
 53		res += sysfs_emit_at(buf, res, "%s ", bond->dev->name);
 54	}
 55	if (res)
 56		buf[res-1] = '\n'; /* eat the leftover space */
 57
 58	rcu_read_unlock();
 59	return res;
 60}
 61
 62static struct net_device *bond_get_by_name(const 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(const struct class *cls,
 79				   const struct class_attribute *attr,
 
 80				   const char *buffer, size_t count)
 81{
 82	const struct bond_net *bn =
 83		container_of_const(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	rcu_read_lock();
 
174
175	bond_for_each_slave_rcu(bond, slave, iter) {
176		if (res > (PAGE_SIZE - IFNAMSIZ)) {
177			/* not enough space for another interface name */
178			if ((PAGE_SIZE - res) > 10)
179				res = PAGE_SIZE - 10;
180			res += sysfs_emit_at(buf, res, "++more++ ");
181			break;
182		}
183		res += sysfs_emit_at(buf, res, "%s ", slave->dev->name);
184	}
185
186	rcu_read_unlock();
187
188	if (res)
189		buf[res-1] = '\n'; /* eat the leftover space */
190
191	return res;
192}
193static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
194		   bonding_sysfs_store_option);
195
196/* Show the bonding mode. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197static ssize_t bonding_show_mode(struct device *d,
198				 struct device_attribute *attr, char *buf)
199{
200	struct bonding *bond = to_bond(d);
201	const struct bond_opt_value *val;
202
203	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
205	return sysfs_emit(buf, "%s %d\n", val->string, BOND_MODE(bond));
206}
207static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
 
208
209/* Show the bonding transmit hash method. */
 
 
210static ssize_t bonding_show_xmit_hash(struct device *d,
211				      struct device_attribute *attr,
212				      char *buf)
213{
214	struct bonding *bond = to_bond(d);
215	const struct bond_opt_value *val;
216
217	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
218
219	return sysfs_emit(buf, "%s %d\n", val->string, bond->params.xmit_policy);
220}
221static DEVICE_ATTR(xmit_hash_policy, 0644,
222		   bonding_show_xmit_hash, bonding_sysfs_store_option);
223
224/* Show arp_validate. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225static ssize_t bonding_show_arp_validate(struct device *d,
226					 struct device_attribute *attr,
227					 char *buf)
228{
229	struct bonding *bond = to_bond(d);
230	const struct bond_opt_value *val;
231
232	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
233			       bond->params.arp_validate);
234
235	return sysfs_emit(buf, "%s %d\n", val->string, bond->params.arp_validate);
236}
237static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
238		   bonding_sysfs_store_option);
239
240/* Show arp_all_targets. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241static ssize_t bonding_show_arp_all_targets(struct device *d,
242					 struct device_attribute *attr,
243					 char *buf)
244{
245	struct bonding *bond = to_bond(d);
246	const struct bond_opt_value *val;
247
248	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
249			       bond->params.arp_all_targets);
250	return sysfs_emit(buf, "%s %d\n",
251		       val->string, bond->params.arp_all_targets);
252}
253static DEVICE_ATTR(arp_all_targets, 0644,
254		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
255
256/* Show fail_over_mac. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257static ssize_t bonding_show_fail_over_mac(struct device *d,
258					  struct device_attribute *attr,
259					  char *buf)
260{
261	struct bonding *bond = to_bond(d);
262	const struct bond_opt_value *val;
263
264	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
265			       bond->params.fail_over_mac);
266
267	return sysfs_emit(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
268}
269static DEVICE_ATTR(fail_over_mac, 0644,
270		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
271
272/* Show the arp timer interval. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273static ssize_t bonding_show_arp_interval(struct device *d,
274					 struct device_attribute *attr,
275					 char *buf)
276{
277	struct bonding *bond = to_bond(d);
278
279	return sysfs_emit(buf, "%d\n", bond->params.arp_interval);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280}
281static DEVICE_ATTR(arp_interval, 0644,
282		   bonding_show_arp_interval, bonding_sysfs_store_option);
283
284/* Show the arp targets. */
 
 
285static ssize_t bonding_show_arp_targets(struct device *d,
286					struct device_attribute *attr,
287					char *buf)
288{
289	struct bonding *bond = to_bond(d);
290	int i, res = 0;
291
292	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
293		if (bond->params.arp_targets[i])
294			res += sysfs_emit_at(buf, res, "%pI4 ",
295					     &bond->params.arp_targets[i]);
296	}
297	if (res)
298		buf[res-1] = '\n'; /* eat the leftover space */
299
300	return res;
301}
302static DEVICE_ATTR(arp_ip_target, 0644,
303		   bonding_show_arp_targets, bonding_sysfs_store_option);
304
305/* Show the arp missed max. */
306static ssize_t bonding_show_missed_max(struct device *d,
307				       struct device_attribute *attr,
308				       char *buf)
309{
310	struct bonding *bond = to_bond(d);
 
311
312	return sysfs_emit(buf, "%u\n", bond->params.missed_max);
 
 
 
 
313}
314static DEVICE_ATTR(arp_missed_max, 0644,
315		   bonding_show_missed_max, 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 sysfs_emit(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 sysfs_emit(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
341static ssize_t bonding_show_peer_notif_delay(struct device *d,
342					     struct device_attribute *attr,
343					     char *buf)
344{
345	struct bonding *bond = to_bond(d);
 
346
347	return sysfs_emit(buf, "%d\n",
348			  bond->params.peer_notif_delay * bond->params.miimon);
 
 
 
349}
350static DEVICE_ATTR(peer_notif_delay, 0644,
351		   bonding_show_peer_notif_delay, bonding_sysfs_store_option);
352
353/* Show the LACP activity and interval. */
354static ssize_t bonding_show_lacp_active(struct device *d,
355					struct device_attribute *attr,
356					char *buf)
 
 
 
357{
358	struct bonding *bond = to_bond(d);
359	const struct bond_opt_value *val;
360
361	val = bond_opt_get_val(BOND_OPT_LACP_ACTIVE, bond->params.lacp_active);
362
363	return sysfs_emit(buf, "%s %d\n", val->string, bond->params.lacp_active);
364}
365static DEVICE_ATTR(lacp_active, 0644,
366		   bonding_show_lacp_active, bonding_sysfs_store_option);
367
368static ssize_t bonding_show_lacp_rate(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_LACP_RATE, bond->params.lacp_fast);
 
 
376
377	return sysfs_emit(buf, "%s %d\n", val->string, bond->params.lacp_fast);
378}
379static DEVICE_ATTR(lacp_rate, 0644,
380		   bonding_show_lacp_rate, bonding_sysfs_store_option);
381
382static ssize_t bonding_show_min_links(struct device *d,
383				      struct device_attribute *attr,
384				      char *buf)
385{
386	struct bonding *bond = to_bond(d);
387
388	return sysfs_emit(buf, "%u\n", bond->params.min_links);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389}
390static DEVICE_ATTR(min_links, 0644,
391		   bonding_show_min_links, bonding_sysfs_store_option);
392
393static ssize_t bonding_show_ad_select(struct device *d,
394				      struct device_attribute *attr,
395				      char *buf)
396{
397	struct bonding *bond = to_bond(d);
398	const struct bond_opt_value *val;
399
400	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
401
402	return sysfs_emit(buf, "%s %d\n", val->string, bond->params.ad_select);
403}
404static DEVICE_ATTR(ad_select, 0644,
405		   bonding_show_ad_select, bonding_sysfs_store_option);
406
407/* Show the number of peer notifications to send after a failover event. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408static ssize_t bonding_show_num_peer_notif(struct device *d,
409					   struct device_attribute *attr,
410					   char *buf)
411{
412	struct bonding *bond = to_bond(d);
 
 
413
414	return sysfs_emit(buf, "%d\n", bond->params.num_peer_notif);
 
 
 
 
 
 
 
 
 
 
 
415}
416static DEVICE_ATTR(num_grat_arp, 0644,
417		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
418static DEVICE_ATTR(num_unsol_na, 0644,
419		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
420
421/* Show the MII monitor interval. */
 
 
 
 
 
422static ssize_t bonding_show_miimon(struct device *d,
423				   struct device_attribute *attr,
424				   char *buf)
425{
426	struct bonding *bond = to_bond(d);
427
428	return sysfs_emit(buf, "%d\n", bond->params.miimon);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429}
430static DEVICE_ATTR(miimon, 0644,
431		   bonding_show_miimon, bonding_sysfs_store_option);
432
433/* Show the primary slave. */
 
 
 
 
 
 
434static ssize_t bonding_show_primary(struct device *d,
435				    struct device_attribute *attr,
436				    char *buf)
437{
 
438	struct bonding *bond = to_bond(d);
439	struct slave *primary;
440	int count = 0;
441
442	rcu_read_lock();
443	primary = rcu_dereference(bond->primary_slave);
444	if (primary)
445		count = sysfs_emit(buf, "%s\n", primary->dev->name);
446	rcu_read_unlock();
447
448	return count;
449}
450static DEVICE_ATTR(primary, 0644,
451		   bonding_show_primary, bonding_sysfs_store_option);
452
453/* Show the primary_reselect flag. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454static ssize_t bonding_show_primary_reselect(struct device *d,
455					     struct device_attribute *attr,
456					     char *buf)
457{
458	struct bonding *bond = to_bond(d);
459	const struct bond_opt_value *val;
460
461	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
462			       bond->params.primary_reselect);
463
464	return sysfs_emit(buf, "%s %d\n",
465			  val->string, bond->params.primary_reselect);
466}
467static DEVICE_ATTR(primary_reselect, 0644,
468		   bonding_show_primary_reselect, bonding_sysfs_store_option);
469
470/* Show the use_carrier flag. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471static ssize_t bonding_show_carrier(struct device *d,
472				    struct device_attribute *attr,
473				    char *buf)
474{
475	struct bonding *bond = to_bond(d);
476
477	return sysfs_emit(buf, "%d\n", bond->params.use_carrier);
478}
479static DEVICE_ATTR(use_carrier, 0644,
480		   bonding_show_carrier, bonding_sysfs_store_option);
481
 
 
 
 
 
 
 
 
 
 
482
483/* Show currently active_slave. */
 
 
 
 
 
 
 
 
484static ssize_t bonding_show_active_slave(struct device *d,
485					 struct device_attribute *attr,
486					 char *buf)
487{
488	struct bonding *bond = to_bond(d);
489	struct net_device *slave_dev;
490	int count = 0;
491
492	rcu_read_lock();
493	slave_dev = bond_option_active_slave_get_rcu(bond);
494	if (slave_dev)
495		count = sysfs_emit(buf, "%s\n", slave_dev->name);
496	rcu_read_unlock();
497
498	return count;
499}
500static DEVICE_ATTR(active_slave, 0644,
501		   bonding_show_active_slave, bonding_sysfs_store_option);
502
503/* Show link status of the bond interface. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504static ssize_t bonding_show_mii_status(struct device *d,
505				       struct device_attribute *attr,
506				       char *buf)
507{
508	struct bonding *bond = to_bond(d);
509	bool active = netif_carrier_ok(bond->dev);
510
511	return sysfs_emit(buf, "%s\n", active ? "up" : "down");
512}
513static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
514
515/* Show current 802.3ad aggregator ID. */
 
 
516static ssize_t bonding_show_ad_aggregator(struct device *d,
517					  struct device_attribute *attr,
518					  char *buf)
519{
520	int count = 0;
521	struct bonding *bond = to_bond(d);
522
523	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
524		struct ad_info ad_info;
525
526		count = sysfs_emit(buf, "%d\n",
527				   bond_3ad_get_active_agg_info(bond, &ad_info)
528				   ?  0 : ad_info.aggregator_id);
529	}
530
531	return count;
532}
533static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
534
535
536/* Show number of active 802.3ad ports. */
 
 
537static ssize_t bonding_show_ad_num_ports(struct device *d,
538					 struct device_attribute *attr,
539					 char *buf)
540{
541	int count = 0;
542	struct bonding *bond = to_bond(d);
543
544	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
545		struct ad_info ad_info;
546
547		count = sysfs_emit(buf, "%d\n",
548				   bond_3ad_get_active_agg_info(bond, &ad_info)
549				   ?  0 : ad_info.ports);
550	}
551
552	return count;
553}
554static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
555
556
557/* Show current 802.3ad actor key. */
 
 
558static ssize_t bonding_show_ad_actor_key(struct device *d,
559					 struct device_attribute *attr,
560					 char *buf)
561{
562	int count = 0;
563	struct bonding *bond = to_bond(d);
564
565	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
566		struct ad_info ad_info;
567
568		count = sysfs_emit(buf, "%d\n",
569				   bond_3ad_get_active_agg_info(bond, &ad_info)
570				   ?  0 : ad_info.actor_key);
571	}
572
573	return count;
574}
575static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
576
577
578/* Show current 802.3ad partner key. */
 
 
579static ssize_t bonding_show_ad_partner_key(struct device *d,
580					   struct device_attribute *attr,
581					   char *buf)
582{
583	int count = 0;
584	struct bonding *bond = to_bond(d);
585
586	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
587		struct ad_info ad_info;
588
589		count = sysfs_emit(buf, "%d\n",
590				   bond_3ad_get_active_agg_info(bond, &ad_info)
591				   ?  0 : ad_info.partner_key);
592	}
593
594	return count;
595}
596static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
597
598
599/* Show current 802.3ad partner mac. */
 
 
600static ssize_t bonding_show_ad_partner_mac(struct device *d,
601					   struct device_attribute *attr,
602					   char *buf)
603{
604	int count = 0;
605	struct bonding *bond = to_bond(d);
606
607	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
608		struct ad_info ad_info;
609
610		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
611			count = sysfs_emit(buf, "%pM\n", ad_info.partner_system);
612	}
613
614	return count;
615}
616static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
617
618/* Show the queue_ids of the slaves in the current bond. */
 
 
619static ssize_t bonding_show_queue_id(struct device *d,
620				     struct device_attribute *attr,
621				     char *buf)
622{
623	struct bonding *bond = to_bond(d);
624	struct list_head *iter;
625	struct slave *slave;
626	int res = 0;
627
628	rcu_read_lock();
 
629
630	bond_for_each_slave_rcu(bond, slave, iter) {
631		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
632			/* not enough space for another interface_name:queue_id pair */
633			if ((PAGE_SIZE - res) > 10)
634				res = PAGE_SIZE - 10;
635			res += sysfs_emit_at(buf, res, "++more++ ");
636			break;
637		}
638		res += sysfs_emit_at(buf, res, "%s:%d ",
639				     slave->dev->name,
640				     READ_ONCE(slave->queue_id));
641	}
642	if (res)
643		buf[res-1] = '\n'; /* eat the leftover space */
644
645	rcu_read_unlock();
646
647	return res;
648}
649static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
650		   bonding_sysfs_store_option);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651
652
653/* Show the all_slaves_active flag. */
 
 
654static ssize_t bonding_show_slaves_active(struct device *d,
655					  struct device_attribute *attr,
656					  char *buf)
657{
658	struct bonding *bond = to_bond(d);
659
660	return sysfs_emit(buf, "%d\n", bond->params.all_slaves_active);
661}
662static DEVICE_ATTR(all_slaves_active, 0644,
663		   bonding_show_slaves_active, bonding_sysfs_store_option);
664
665/* Show the number of IGMP membership reports to send on link failure */
666static ssize_t bonding_show_resend_igmp(struct device *d,
667					struct device_attribute *attr,
668					char *buf)
669{
670	struct bonding *bond = to_bond(d);
 
 
 
 
 
 
671
672	return sysfs_emit(buf, "%d\n", bond->params.resend_igmp);
673}
674static DEVICE_ATTR(resend_igmp, 0644,
675		   bonding_show_resend_igmp, bonding_sysfs_store_option);
676
677
678static ssize_t bonding_show_lp_interval(struct device *d,
 
 
679					struct device_attribute *attr,
680					char *buf)
681{
682	struct bonding *bond = to_bond(d);
683
684	return sysfs_emit(buf, "%d\n", bond->params.lp_interval);
685}
686static DEVICE_ATTR(lp_interval, 0644,
687		   bonding_show_lp_interval, bonding_sysfs_store_option);
688
689static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
690					   struct device_attribute *attr,
691					   char *buf)
692{
693	struct bonding *bond = to_bond(d);
 
 
 
 
 
694
695	return sysfs_emit(buf, "%d\n", bond->params.tlb_dynamic_lb);
696}
697static DEVICE_ATTR(tlb_dynamic_lb, 0644,
698		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
699
700static ssize_t bonding_show_packets_per_slave(struct device *d,
701					      struct device_attribute *attr,
702					      char *buf)
 
 
 
 
703{
704	struct bonding *bond = to_bond(d);
705	unsigned int packets_per_slave = bond->params.packets_per_slave;
706
707	return sysfs_emit(buf, "%u\n", packets_per_slave);
708}
709static DEVICE_ATTR(packets_per_slave, 0644,
710		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
711
712static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
713					      struct device_attribute *attr,
714					      char *buf)
715{
716	struct bonding *bond = to_bond(d);
 
717
718	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
719		return sysfs_emit(buf, "%hu\n", bond->params.ad_actor_sys_prio);
 
720
721	return 0;
722}
723static DEVICE_ATTR(ad_actor_sys_prio, 0644,
724		   bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
725
726static ssize_t bonding_show_ad_actor_system(struct device *d,
727					    struct device_attribute *attr,
728					    char *buf)
 
 
 
729{
730	struct bonding *bond = to_bond(d);
731
732	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
733		return sysfs_emit(buf, "%pM\n", bond->params.ad_actor_system);
734
735	return 0;
736}
737
738static DEVICE_ATTR(ad_actor_system, 0644,
739		   bonding_show_ad_actor_system, bonding_sysfs_store_option);
740
741static ssize_t bonding_show_ad_user_port_key(struct device *d,
742					     struct device_attribute *attr,
743					     char *buf)
744{
745	struct bonding *bond = to_bond(d);
 
746
747	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
748		return sysfs_emit(buf, "%hu\n", bond->params.ad_user_port_key);
 
 
749
750	return 0;
751}
752static DEVICE_ATTR(ad_user_port_key, 0644,
753		   bonding_show_ad_user_port_key, bonding_sysfs_store_option);
 
 
754
755static struct attribute *per_bond_attrs[] = {
756	&dev_attr_slaves.attr,
757	&dev_attr_mode.attr,
758	&dev_attr_fail_over_mac.attr,
759	&dev_attr_arp_validate.attr,
760	&dev_attr_arp_all_targets.attr,
761	&dev_attr_arp_interval.attr,
762	&dev_attr_arp_ip_target.attr,
763	&dev_attr_downdelay.attr,
764	&dev_attr_updelay.attr,
765	&dev_attr_peer_notif_delay.attr,
766	&dev_attr_lacp_active.attr,
767	&dev_attr_lacp_rate.attr,
768	&dev_attr_ad_select.attr,
769	&dev_attr_xmit_hash_policy.attr,
770	&dev_attr_num_grat_arp.attr,
771	&dev_attr_num_unsol_na.attr,
772	&dev_attr_miimon.attr,
773	&dev_attr_primary.attr,
774	&dev_attr_primary_reselect.attr,
775	&dev_attr_use_carrier.attr,
776	&dev_attr_active_slave.attr,
777	&dev_attr_mii_status.attr,
778	&dev_attr_ad_aggregator.attr,
779	&dev_attr_ad_num_ports.attr,
780	&dev_attr_ad_actor_key.attr,
781	&dev_attr_ad_partner_key.attr,
782	&dev_attr_ad_partner_mac.attr,
783	&dev_attr_queue_id.attr,
784	&dev_attr_all_slaves_active.attr,
785	&dev_attr_resend_igmp.attr,
786	&dev_attr_min_links.attr,
787	&dev_attr_lp_interval.attr,
788	&dev_attr_packets_per_slave.attr,
789	&dev_attr_tlb_dynamic_lb.attr,
790	&dev_attr_ad_actor_sys_prio.attr,
791	&dev_attr_ad_actor_system.attr,
792	&dev_attr_ad_user_port_key.attr,
793	&dev_attr_arp_missed_max.attr,
794	NULL,
795};
796
797static const struct attribute_group bonding_group = {
798	.name = "bonding",
799	.attrs = per_bond_attrs,
800};
801
802/* Initialize sysfs.  This sets up the bonding_masters file in
 
803 * /sys/class/net.
804 */
805int __net_init bond_create_sysfs(struct bond_net *bn)
806{
807	int ret;
808
809	bn->class_attr_bonding_masters = class_attr_bonding_masters;
810	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
811
812	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
813					  bn->net);
814	/* Permit multiple loads of the module by ignoring failures to
 
815	 * create the bonding_masters sysfs file.  Bonding devices
816	 * created by second or subsequent loads of the module will
817	 * not be listed in, or controllable by, bonding_masters, but
818	 * will have the usual "bonding" sysfs directory.
819	 *
820	 * This is done to preserve backwards compatibility for
821	 * initscripts/sysconfig, which load bonding multiple times to
822	 * configure multiple bonding devices.
823	 */
824	if (ret == -EEXIST) {
825		/* Is someone being kinky and naming a device bonding_master? */
826		if (netdev_name_in_use(bn->net,
827				       class_attr_bonding_masters.attr.name))
828			pr_err("network device named %s already exists in sysfs\n",
829			       class_attr_bonding_masters.attr.name);
830		ret = 0;
831	}
832
833	return ret;
834
835}
836
837/* Remove /sys/class/net/bonding_masters. */
838void __net_exit bond_destroy_sysfs(struct bond_net *bn)
 
 
839{
840	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
841}
842
843/* Initialize sysfs for each bond.  This sets up and registers
 
844 * the 'bondctl' directory for each individual bond under /sys/class/net.
845 */
846void bond_prepare_sysfs_group(struct bonding *bond)
847{
848	bond->dev->sysfs_groups[0] = &bonding_group;
849}
850