1#include <linux/proc_fs.h>
2#include <linux/export.h>
3#include <net/net_namespace.h>
4#include <net/netns/generic.h>
5#include <net/bonding.h>
6
7#include "bonding_priv.h"
8
9static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
10	__acquires(RCU)
11{
12	struct bonding *bond = seq->private;
13	struct list_head *iter;
14	struct slave *slave;
15	loff_t off = 0;
16
17	rcu_read_lock();
18
19	if (*pos == 0)
20		return SEQ_START_TOKEN;
21
22	bond_for_each_slave_rcu(bond, slave, iter)
23		if (++off == *pos)
24			return slave;
25
26	return NULL;
27}
28
29static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
30{
31	struct bonding *bond = seq->private;
32	struct list_head *iter;
33	struct slave *slave;
34	bool found = false;
35
36	++*pos;
37	if (v == SEQ_START_TOKEN)
38		return bond_first_slave_rcu(bond);
39
40	bond_for_each_slave_rcu(bond, slave, iter) {
41		if (found)
42			return slave;
43		if (slave == v)
44			found = true;
45	}
46
47	return NULL;
48}
49
50static void bond_info_seq_stop(struct seq_file *seq, void *v)
51	__releases(RCU)
52{
53	rcu_read_unlock();
54}
55
56static void bond_info_show_master(struct seq_file *seq)
57{
58	struct bonding *bond = seq->private;
59	const struct bond_opt_value *optval;
60	struct slave *curr, *primary;
61	int i;
62
63	curr = rcu_dereference(bond->curr_active_slave);
64
65	seq_printf(seq, "Bonding Mode: %s",
66		   bond_mode_name(BOND_MODE(bond)));
67
68	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
69	    bond->params.fail_over_mac) {
70		optval = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
71					  bond->params.fail_over_mac);
72		seq_printf(seq, " (fail_over_mac %s)", optval->string);
73	}
74
75	seq_printf(seq, "\n");
76
77	if (bond_mode_uses_xmit_hash(bond)) {
78		optval = bond_opt_get_val(BOND_OPT_XMIT_HASH,
79					  bond->params.xmit_policy);
80		seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
81			   optval->string, bond->params.xmit_policy);
82	}
83
84	if (bond_uses_primary(bond)) {
85		primary = rcu_dereference(bond->primary_slave);
86		seq_printf(seq, "Primary Slave: %s",
87			   primary ? primary->dev->name : "None");
88		if (primary) {
89			optval = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
90						  bond->params.primary_reselect);
91			seq_printf(seq, " (primary_reselect %s)",
92				   optval->string);
93		}
94
95		seq_printf(seq, "\nCurrently Active Slave: %s\n",
96			   (curr) ? curr->dev->name : "None");
97	}
98
99	seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
100		   "up" : "down");
101	seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
102	seq_printf(seq, "Up Delay (ms): %d\n",
103		   bond->params.updelay * bond->params.miimon);
104	seq_printf(seq, "Down Delay (ms): %d\n",
105		   bond->params.downdelay * bond->params.miimon);
106
107
108	/* ARP information */
109	if (bond->params.arp_interval > 0) {
110		int printed = 0;
111		seq_printf(seq, "ARP Polling Interval (ms): %d\n",
112				bond->params.arp_interval);
113
114		seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
115
116		for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
117			if (!bond->params.arp_targets[i])
118				break;
119			if (printed)
120				seq_printf(seq, ",");
121			seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
122			printed = 1;
123		}
124		seq_printf(seq, "\n");
125	}
126
127	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
128		struct ad_info ad_info;
129
130		seq_puts(seq, "\n802.3ad info\n");
131		seq_printf(seq, "LACP rate: %s\n",
132			   (bond->params.lacp_fast) ? "fast" : "slow");
133		seq_printf(seq, "Min links: %d\n", bond->params.min_links);
134		optval = bond_opt_get_val(BOND_OPT_AD_SELECT,
135					  bond->params.ad_select);
136		seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
137			   optval->string);
138		if (capable(CAP_NET_ADMIN)) {
139			seq_printf(seq, "System priority: %d\n",
140				   BOND_AD_INFO(bond).system.sys_priority);
141			seq_printf(seq, "System MAC address: %pM\n",
142				   &BOND_AD_INFO(bond).system.sys_mac_addr);
143
144			if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
145				seq_printf(seq,
146					   "bond %s has no active aggregator\n",
147					   bond->dev->name);
148			} else {
149				seq_printf(seq, "Active Aggregator Info:\n");
150
151				seq_printf(seq, "\tAggregator ID: %d\n",
152					   ad_info.aggregator_id);
153				seq_printf(seq, "\tNumber of ports: %d\n",
154					   ad_info.ports);
155				seq_printf(seq, "\tActor Key: %d\n",
156					   ad_info.actor_key);
157				seq_printf(seq, "\tPartner Key: %d\n",
158					   ad_info.partner_key);
159				seq_printf(seq, "\tPartner Mac Address: %pM\n",
160					   ad_info.partner_system);
161			}
162		}
163	}
164}
165
166static void bond_info_show_slave(struct seq_file *seq,
167				 const struct slave *slave)
168{
169	struct bonding *bond = seq->private;
170
171	seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
172	seq_printf(seq, "MII Status: %s\n", bond_slave_link_status(slave->link));
173	if (slave->speed == SPEED_UNKNOWN)
174		seq_printf(seq, "Speed: %s\n", "Unknown");
175	else
176		seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
177
178	if (slave->duplex == DUPLEX_UNKNOWN)
179		seq_printf(seq, "Duplex: %s\n", "Unknown");
180	else
181		seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
182
183	seq_printf(seq, "Link Failure Count: %u\n",
184		   slave->link_failure_count);
185
186	seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
187	seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
188
189	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
190		const struct port *port = &SLAVE_AD_INFO(slave)->port;
191		const struct aggregator *agg = port->aggregator;
192
193		if (agg) {
194			seq_printf(seq, "Aggregator ID: %d\n",
195				   agg->aggregator_identifier);
196			seq_printf(seq, "Actor Churn State: %s\n",
197				   bond_3ad_churn_desc(port->sm_churn_actor_state));
198			seq_printf(seq, "Partner Churn State: %s\n",
199				   bond_3ad_churn_desc(port->sm_churn_partner_state));
200			seq_printf(seq, "Actor Churned Count: %d\n",
201				   port->churn_actor_count);
202			seq_printf(seq, "Partner Churned Count: %d\n",
203				   port->churn_partner_count);
204
205			if (capable(CAP_NET_ADMIN)) {
206				seq_puts(seq, "details actor lacp pdu:\n");
207				seq_printf(seq, "    system priority: %d\n",
208					   port->actor_system_priority);
209				seq_printf(seq, "    system mac address: %pM\n",
210					   &port->actor_system);
211				seq_printf(seq, "    port key: %d\n",
212					   port->actor_oper_port_key);
213				seq_printf(seq, "    port priority: %d\n",
214					   port->actor_port_priority);
215				seq_printf(seq, "    port number: %d\n",
216					   port->actor_port_number);
217				seq_printf(seq, "    port state: %d\n",
218					   port->actor_oper_port_state);
219
220				seq_puts(seq, "details partner lacp pdu:\n");
221				seq_printf(seq, "    system priority: %d\n",
222					   port->partner_oper.system_priority);
223				seq_printf(seq, "    system mac address: %pM\n",
224					   &port->partner_oper.system);
225				seq_printf(seq, "    oper key: %d\n",
226					   port->partner_oper.key);
227				seq_printf(seq, "    port priority: %d\n",
228					   port->partner_oper.port_priority);
229				seq_printf(seq, "    port number: %d\n",
230					   port->partner_oper.port_number);
231				seq_printf(seq, "    port state: %d\n",
232					   port->partner_oper.port_state);
233			}
234		} else {
235			seq_puts(seq, "Aggregator ID: N/A\n");
236		}
237	}
238}
239
240static int bond_info_seq_show(struct seq_file *seq, void *v)
241{
242	if (v == SEQ_START_TOKEN) {
243		seq_printf(seq, "%s\n", bond_version);
244		bond_info_show_master(seq);
245	} else
246		bond_info_show_slave(seq, v);
247
248	return 0;
249}
250
251static const struct seq_operations bond_info_seq_ops = {
252	.start = bond_info_seq_start,
253	.next  = bond_info_seq_next,
254	.stop  = bond_info_seq_stop,
255	.show  = bond_info_seq_show,
256};
257
258static int bond_info_open(struct inode *inode, struct file *file)
259{
260	struct seq_file *seq;
261	int res;
262
263	res = seq_open(file, &bond_info_seq_ops);
264	if (!res) {
265		/* recover the pointer buried in proc_dir_entry data */
266		seq = file->private_data;
267		seq->private = PDE_DATA(inode);
268	}
269
270	return res;
271}
272
273static const struct file_operations bond_info_fops = {
274	.owner   = THIS_MODULE,
275	.open    = bond_info_open,
276	.read    = seq_read,
277	.llseek  = seq_lseek,
278	.release = seq_release,
279};
280
281void bond_create_proc_entry(struct bonding *bond)
282{
283	struct net_device *bond_dev = bond->dev;
284	struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
285
286	if (bn->proc_dir) {
287		bond->proc_entry = proc_create_data(bond_dev->name,
288						    S_IRUGO, bn->proc_dir,
289						    &bond_info_fops, bond);
290		if (bond->proc_entry == NULL)
291			netdev_warn(bond_dev, "Cannot create /proc/net/%s/%s\n",
292				    DRV_NAME, bond_dev->name);
293		else
294			memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
295	}
296}
297
298void bond_remove_proc_entry(struct bonding *bond)
299{
300	struct net_device *bond_dev = bond->dev;
301	struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
302
303	if (bn->proc_dir && bond->proc_entry) {
304		remove_proc_entry(bond->proc_file_name, bn->proc_dir);
305		memset(bond->proc_file_name, 0, IFNAMSIZ);
306		bond->proc_entry = NULL;
307	}
308}
309
310/* Create the bonding directory under /proc/net, if doesn't exist yet.
311 * Caller must hold rtnl_lock.
312 */
313void __net_init bond_create_proc_dir(struct bond_net *bn)
314{
315	if (!bn->proc_dir) {
316		bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
317		if (!bn->proc_dir)
318			pr_warn("Warning: Cannot create /proc/net/%s\n",
319				DRV_NAME);
320	}
321}
322
323/* Destroy the bonding directory under /proc/net, if empty.
324 * Caller must hold rtnl_lock.
325 */
326void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
327{
328	if (bn->proc_dir) {
329		remove_proc_entry(DRV_NAME, bn->net->proc_net);
330		bn->proc_dir = NULL;
331	}
332}
333