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