Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net-sysfs.c - network device class and attributes | |
3 | * | |
4 | * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org> | |
4ec93edb | 5 | * |
1da177e4 LT |
6 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
4fc268d2 | 12 | #include <linux/capability.h> |
1da177e4 LT |
13 | #include <linux/kernel.h> |
14 | #include <linux/netdevice.h> | |
15 | #include <linux/if_arp.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
608b4b95 | 17 | #include <linux/nsproxy.h> |
1da177e4 | 18 | #include <net/sock.h> |
608b4b95 | 19 | #include <net/net_namespace.h> |
1da177e4 | 20 | #include <linux/rtnetlink.h> |
fec5e652 | 21 | #include <linux/vmalloc.h> |
bc3b2d7f | 22 | #include <linux/export.h> |
114cf580 | 23 | #include <linux/jiffies.h> |
1da177e4 | 24 | |
342709ef PE |
25 | #include "net-sysfs.h" |
26 | ||
8b41d188 | 27 | #ifdef CONFIG_SYSFS |
1da177e4 | 28 | static const char fmt_hex[] = "%#x\n"; |
d1102b59 | 29 | static const char fmt_long_hex[] = "%#lx\n"; |
1da177e4 | 30 | static const char fmt_dec[] = "%d\n"; |
8ae6daca | 31 | static const char fmt_udec[] = "%u\n"; |
1da177e4 | 32 | static const char fmt_ulong[] = "%lu\n"; |
be1f3c2c | 33 | static const char fmt_u64[] = "%llu\n"; |
1da177e4 | 34 | |
4ec93edb | 35 | static inline int dev_isalive(const struct net_device *dev) |
1da177e4 | 36 | { |
fe9925b5 | 37 | return dev->reg_state <= NETREG_REGISTERED; |
1da177e4 LT |
38 | } |
39 | ||
40 | /* use same locking rules as GIF* ioctl's */ | |
43cb76d9 GKH |
41 | static ssize_t netdev_show(const struct device *dev, |
42 | struct device_attribute *attr, char *buf, | |
1da177e4 LT |
43 | ssize_t (*format)(const struct net_device *, char *)) |
44 | { | |
43cb76d9 | 45 | struct net_device *net = to_net_dev(dev); |
1da177e4 LT |
46 | ssize_t ret = -EINVAL; |
47 | ||
48 | read_lock(&dev_base_lock); | |
49 | if (dev_isalive(net)) | |
50 | ret = (*format)(net, buf); | |
51 | read_unlock(&dev_base_lock); | |
52 | ||
53 | return ret; | |
54 | } | |
55 | ||
56 | /* generate a show function for simple field */ | |
57 | #define NETDEVICE_SHOW(field, format_string) \ | |
58 | static ssize_t format_##field(const struct net_device *net, char *buf) \ | |
59 | { \ | |
60 | return sprintf(buf, format_string, net->field); \ | |
61 | } \ | |
43cb76d9 GKH |
62 | static ssize_t show_##field(struct device *dev, \ |
63 | struct device_attribute *attr, char *buf) \ | |
1da177e4 | 64 | { \ |
43cb76d9 | 65 | return netdev_show(dev, attr, buf, format_##field); \ |
1da177e4 LT |
66 | } |
67 | ||
68 | ||
69 | /* use same locking and permission rules as SIF* ioctl's */ | |
43cb76d9 | 70 | static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, |
1da177e4 LT |
71 | const char *buf, size_t len, |
72 | int (*set)(struct net_device *, unsigned long)) | |
73 | { | |
5e1fccc0 EB |
74 | struct net_device *netdev = to_net_dev(dev); |
75 | struct net *net = dev_net(netdev); | |
1da177e4 LT |
76 | unsigned long new; |
77 | int ret = -EINVAL; | |
78 | ||
5e1fccc0 | 79 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
80 | return -EPERM; |
81 | ||
e1e420c7 SK |
82 | ret = kstrtoul(buf, 0, &new); |
83 | if (ret) | |
1da177e4 LT |
84 | goto err; |
85 | ||
5a5990d3 | 86 | if (!rtnl_trylock()) |
336ca57c | 87 | return restart_syscall(); |
5a5990d3 | 88 | |
5e1fccc0 EB |
89 | if (dev_isalive(netdev)) { |
90 | if ((ret = (*set)(netdev, new)) == 0) | |
1da177e4 LT |
91 | ret = len; |
92 | } | |
93 | rtnl_unlock(); | |
94 | err: | |
95 | return ret; | |
96 | } | |
97 | ||
9d29672c | 98 | NETDEVICE_SHOW(dev_id, fmt_hex); |
c1f79426 | 99 | NETDEVICE_SHOW(addr_assign_type, fmt_dec); |
fd586bac KS |
100 | NETDEVICE_SHOW(addr_len, fmt_dec); |
101 | NETDEVICE_SHOW(iflink, fmt_dec); | |
102 | NETDEVICE_SHOW(ifindex, fmt_dec); | |
fd586bac | 103 | NETDEVICE_SHOW(type, fmt_dec); |
b00055aa | 104 | NETDEVICE_SHOW(link_mode, fmt_dec); |
1da177e4 LT |
105 | |
106 | /* use same locking rules as GIFHWADDR ioctl's */ | |
43cb76d9 GKH |
107 | static ssize_t show_address(struct device *dev, struct device_attribute *attr, |
108 | char *buf) | |
1da177e4 LT |
109 | { |
110 | struct net_device *net = to_net_dev(dev); | |
111 | ssize_t ret = -EINVAL; | |
112 | ||
113 | read_lock(&dev_base_lock); | |
114 | if (dev_isalive(net)) | |
7ffc49a6 | 115 | ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len); |
1da177e4 LT |
116 | read_unlock(&dev_base_lock); |
117 | return ret; | |
118 | } | |
119 | ||
43cb76d9 GKH |
120 | static ssize_t show_broadcast(struct device *dev, |
121 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
122 | { |
123 | struct net_device *net = to_net_dev(dev); | |
124 | if (dev_isalive(net)) | |
7ffc49a6 | 125 | return sysfs_format_mac(buf, net->broadcast, net->addr_len); |
1da177e4 LT |
126 | return -EINVAL; |
127 | } | |
128 | ||
fdae0fde JP |
129 | static int change_carrier(struct net_device *net, unsigned long new_carrier) |
130 | { | |
131 | if (!netif_running(net)) | |
132 | return -EINVAL; | |
133 | return dev_change_carrier(net, (bool) new_carrier); | |
134 | } | |
135 | ||
136 | static ssize_t store_carrier(struct device *dev, struct device_attribute *attr, | |
137 | const char *buf, size_t len) | |
138 | { | |
139 | return netdev_store(dev, attr, buf, len, change_carrier); | |
140 | } | |
141 | ||
43cb76d9 GKH |
142 | static ssize_t show_carrier(struct device *dev, |
143 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
144 | { |
145 | struct net_device *netdev = to_net_dev(dev); | |
146 | if (netif_running(netdev)) { | |
147 | return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); | |
148 | } | |
149 | return -EINVAL; | |
150 | } | |
151 | ||
d519e17e AG |
152 | static ssize_t show_speed(struct device *dev, |
153 | struct device_attribute *attr, char *buf) | |
154 | { | |
155 | struct net_device *netdev = to_net_dev(dev); | |
156 | int ret = -EINVAL; | |
157 | ||
158 | if (!rtnl_trylock()) | |
159 | return restart_syscall(); | |
160 | ||
8ae6daca DD |
161 | if (netif_running(netdev)) { |
162 | struct ethtool_cmd cmd; | |
4bc71cb9 | 163 | if (!__ethtool_get_settings(netdev, &cmd)) |
8ae6daca | 164 | ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd)); |
d519e17e AG |
165 | } |
166 | rtnl_unlock(); | |
167 | return ret; | |
168 | } | |
169 | ||
170 | static ssize_t show_duplex(struct device *dev, | |
171 | struct device_attribute *attr, char *buf) | |
172 | { | |
173 | struct net_device *netdev = to_net_dev(dev); | |
174 | int ret = -EINVAL; | |
175 | ||
176 | if (!rtnl_trylock()) | |
177 | return restart_syscall(); | |
178 | ||
8ae6daca DD |
179 | if (netif_running(netdev)) { |
180 | struct ethtool_cmd cmd; | |
c6c13965 NA |
181 | if (!__ethtool_get_settings(netdev, &cmd)) { |
182 | const char *duplex; | |
183 | switch (cmd.duplex) { | |
184 | case DUPLEX_HALF: | |
185 | duplex = "half"; | |
186 | break; | |
187 | case DUPLEX_FULL: | |
188 | duplex = "full"; | |
189 | break; | |
190 | default: | |
191 | duplex = "unknown"; | |
192 | break; | |
193 | } | |
194 | ret = sprintf(buf, "%s\n", duplex); | |
195 | } | |
d519e17e AG |
196 | } |
197 | rtnl_unlock(); | |
198 | return ret; | |
199 | } | |
200 | ||
43cb76d9 GKH |
201 | static ssize_t show_dormant(struct device *dev, |
202 | struct device_attribute *attr, char *buf) | |
b00055aa SR |
203 | { |
204 | struct net_device *netdev = to_net_dev(dev); | |
205 | ||
206 | if (netif_running(netdev)) | |
207 | return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); | |
208 | ||
209 | return -EINVAL; | |
210 | } | |
211 | ||
36cbd3dc | 212 | static const char *const operstates[] = { |
b00055aa SR |
213 | "unknown", |
214 | "notpresent", /* currently unused */ | |
215 | "down", | |
216 | "lowerlayerdown", | |
217 | "testing", /* currently unused */ | |
218 | "dormant", | |
219 | "up" | |
220 | }; | |
221 | ||
43cb76d9 GKH |
222 | static ssize_t show_operstate(struct device *dev, |
223 | struct device_attribute *attr, char *buf) | |
b00055aa SR |
224 | { |
225 | const struct net_device *netdev = to_net_dev(dev); | |
226 | unsigned char operstate; | |
227 | ||
228 | read_lock(&dev_base_lock); | |
229 | operstate = netdev->operstate; | |
230 | if (!netif_running(netdev)) | |
231 | operstate = IF_OPER_DOWN; | |
232 | read_unlock(&dev_base_lock); | |
233 | ||
e3a5cd9e | 234 | if (operstate >= ARRAY_SIZE(operstates)) |
b00055aa SR |
235 | return -EINVAL; /* should not happen */ |
236 | ||
237 | return sprintf(buf, "%s\n", operstates[operstate]); | |
238 | } | |
239 | ||
1da177e4 LT |
240 | /* read-write attributes */ |
241 | NETDEVICE_SHOW(mtu, fmt_dec); | |
242 | ||
243 | static int change_mtu(struct net_device *net, unsigned long new_mtu) | |
244 | { | |
245 | return dev_set_mtu(net, (int) new_mtu); | |
246 | } | |
247 | ||
43cb76d9 GKH |
248 | static ssize_t store_mtu(struct device *dev, struct device_attribute *attr, |
249 | const char *buf, size_t len) | |
1da177e4 | 250 | { |
43cb76d9 | 251 | return netdev_store(dev, attr, buf, len, change_mtu); |
1da177e4 LT |
252 | } |
253 | ||
1da177e4 LT |
254 | NETDEVICE_SHOW(flags, fmt_hex); |
255 | ||
256 | static int change_flags(struct net_device *net, unsigned long new_flags) | |
257 | { | |
95c96174 | 258 | return dev_change_flags(net, (unsigned int) new_flags); |
1da177e4 LT |
259 | } |
260 | ||
43cb76d9 GKH |
261 | static ssize_t store_flags(struct device *dev, struct device_attribute *attr, |
262 | const char *buf, size_t len) | |
1da177e4 | 263 | { |
43cb76d9 | 264 | return netdev_store(dev, attr, buf, len, change_flags); |
1da177e4 LT |
265 | } |
266 | ||
1da177e4 LT |
267 | NETDEVICE_SHOW(tx_queue_len, fmt_ulong); |
268 | ||
269 | static int change_tx_queue_len(struct net_device *net, unsigned long new_len) | |
270 | { | |
271 | net->tx_queue_len = new_len; | |
272 | return 0; | |
273 | } | |
274 | ||
43cb76d9 GKH |
275 | static ssize_t store_tx_queue_len(struct device *dev, |
276 | struct device_attribute *attr, | |
277 | const char *buf, size_t len) | |
1da177e4 | 278 | { |
5e1fccc0 EB |
279 | if (!capable(CAP_NET_ADMIN)) |
280 | return -EPERM; | |
281 | ||
43cb76d9 | 282 | return netdev_store(dev, attr, buf, len, change_tx_queue_len); |
1da177e4 LT |
283 | } |
284 | ||
0b815a1a SH |
285 | static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr, |
286 | const char *buf, size_t len) | |
287 | { | |
288 | struct net_device *netdev = to_net_dev(dev); | |
5e1fccc0 | 289 | struct net *net = dev_net(netdev); |
0b815a1a SH |
290 | size_t count = len; |
291 | ssize_t ret; | |
292 | ||
5e1fccc0 | 293 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
0b815a1a SH |
294 | return -EPERM; |
295 | ||
296 | /* ignore trailing newline */ | |
297 | if (len > 0 && buf[len - 1] == '\n') | |
298 | --count; | |
299 | ||
336ca57c EB |
300 | if (!rtnl_trylock()) |
301 | return restart_syscall(); | |
0b815a1a SH |
302 | ret = dev_set_alias(netdev, buf, count); |
303 | rtnl_unlock(); | |
304 | ||
305 | return ret < 0 ? ret : len; | |
306 | } | |
307 | ||
308 | static ssize_t show_ifalias(struct device *dev, | |
309 | struct device_attribute *attr, char *buf) | |
310 | { | |
311 | const struct net_device *netdev = to_net_dev(dev); | |
312 | ssize_t ret = 0; | |
313 | ||
336ca57c EB |
314 | if (!rtnl_trylock()) |
315 | return restart_syscall(); | |
0b815a1a SH |
316 | if (netdev->ifalias) |
317 | ret = sprintf(buf, "%s\n", netdev->ifalias); | |
318 | rtnl_unlock(); | |
319 | return ret; | |
320 | } | |
321 | ||
a512b92b VD |
322 | NETDEVICE_SHOW(group, fmt_dec); |
323 | ||
324 | static int change_group(struct net_device *net, unsigned long new_group) | |
325 | { | |
326 | dev_set_group(net, (int) new_group); | |
327 | return 0; | |
328 | } | |
329 | ||
330 | static ssize_t store_group(struct device *dev, struct device_attribute *attr, | |
331 | const char *buf, size_t len) | |
332 | { | |
333 | return netdev_store(dev, attr, buf, len, change_group); | |
334 | } | |
335 | ||
43cb76d9 | 336 | static struct device_attribute net_class_attributes[] = { |
c1f79426 | 337 | __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL), |
fd586bac | 338 | __ATTR(addr_len, S_IRUGO, show_addr_len, NULL), |
9d29672c | 339 | __ATTR(dev_id, S_IRUGO, show_dev_id, NULL), |
0b815a1a | 340 | __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias), |
fd586bac KS |
341 | __ATTR(iflink, S_IRUGO, show_iflink, NULL), |
342 | __ATTR(ifindex, S_IRUGO, show_ifindex, NULL), | |
fd586bac | 343 | __ATTR(type, S_IRUGO, show_type, NULL), |
b00055aa | 344 | __ATTR(link_mode, S_IRUGO, show_link_mode, NULL), |
fd586bac KS |
345 | __ATTR(address, S_IRUGO, show_address, NULL), |
346 | __ATTR(broadcast, S_IRUGO, show_broadcast, NULL), | |
fdae0fde | 347 | __ATTR(carrier, S_IRUGO | S_IWUSR, show_carrier, store_carrier), |
d519e17e AG |
348 | __ATTR(speed, S_IRUGO, show_speed, NULL), |
349 | __ATTR(duplex, S_IRUGO, show_duplex, NULL), | |
b00055aa SR |
350 | __ATTR(dormant, S_IRUGO, show_dormant, NULL), |
351 | __ATTR(operstate, S_IRUGO, show_operstate, NULL), | |
fd586bac KS |
352 | __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu), |
353 | __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags), | |
354 | __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len, | |
355 | store_tx_queue_len), | |
b6644cb7 | 356 | __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group), |
fd586bac | 357 | {} |
1da177e4 LT |
358 | }; |
359 | ||
360 | /* Show a given an attribute in the statistics group */ | |
43cb76d9 GKH |
361 | static ssize_t netstat_show(const struct device *d, |
362 | struct device_attribute *attr, char *buf, | |
1da177e4 LT |
363 | unsigned long offset) |
364 | { | |
43cb76d9 | 365 | struct net_device *dev = to_net_dev(d); |
1da177e4 LT |
366 | ssize_t ret = -EINVAL; |
367 | ||
be1f3c2c BH |
368 | WARN_ON(offset > sizeof(struct rtnl_link_stats64) || |
369 | offset % sizeof(u64) != 0); | |
1da177e4 LT |
370 | |
371 | read_lock(&dev_base_lock); | |
96e74088 | 372 | if (dev_isalive(dev)) { |
28172739 ED |
373 | struct rtnl_link_stats64 temp; |
374 | const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); | |
375 | ||
be1f3c2c | 376 | ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset)); |
96e74088 | 377 | } |
1da177e4 LT |
378 | read_unlock(&dev_base_lock); |
379 | return ret; | |
380 | } | |
381 | ||
382 | /* generate a read-only statistics attribute */ | |
383 | #define NETSTAT_ENTRY(name) \ | |
43cb76d9 GKH |
384 | static ssize_t show_##name(struct device *d, \ |
385 | struct device_attribute *attr, char *buf) \ | |
1da177e4 | 386 | { \ |
43cb76d9 | 387 | return netstat_show(d, attr, buf, \ |
be1f3c2c | 388 | offsetof(struct rtnl_link_stats64, name)); \ |
1da177e4 | 389 | } \ |
43cb76d9 | 390 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
1da177e4 LT |
391 | |
392 | NETSTAT_ENTRY(rx_packets); | |
393 | NETSTAT_ENTRY(tx_packets); | |
394 | NETSTAT_ENTRY(rx_bytes); | |
395 | NETSTAT_ENTRY(tx_bytes); | |
396 | NETSTAT_ENTRY(rx_errors); | |
397 | NETSTAT_ENTRY(tx_errors); | |
398 | NETSTAT_ENTRY(rx_dropped); | |
399 | NETSTAT_ENTRY(tx_dropped); | |
400 | NETSTAT_ENTRY(multicast); | |
401 | NETSTAT_ENTRY(collisions); | |
402 | NETSTAT_ENTRY(rx_length_errors); | |
403 | NETSTAT_ENTRY(rx_over_errors); | |
404 | NETSTAT_ENTRY(rx_crc_errors); | |
405 | NETSTAT_ENTRY(rx_frame_errors); | |
406 | NETSTAT_ENTRY(rx_fifo_errors); | |
407 | NETSTAT_ENTRY(rx_missed_errors); | |
408 | NETSTAT_ENTRY(tx_aborted_errors); | |
409 | NETSTAT_ENTRY(tx_carrier_errors); | |
410 | NETSTAT_ENTRY(tx_fifo_errors); | |
411 | NETSTAT_ENTRY(tx_heartbeat_errors); | |
412 | NETSTAT_ENTRY(tx_window_errors); | |
413 | NETSTAT_ENTRY(rx_compressed); | |
414 | NETSTAT_ENTRY(tx_compressed); | |
415 | ||
416 | static struct attribute *netstat_attrs[] = { | |
43cb76d9 GKH |
417 | &dev_attr_rx_packets.attr, |
418 | &dev_attr_tx_packets.attr, | |
419 | &dev_attr_rx_bytes.attr, | |
420 | &dev_attr_tx_bytes.attr, | |
421 | &dev_attr_rx_errors.attr, | |
422 | &dev_attr_tx_errors.attr, | |
423 | &dev_attr_rx_dropped.attr, | |
424 | &dev_attr_tx_dropped.attr, | |
425 | &dev_attr_multicast.attr, | |
426 | &dev_attr_collisions.attr, | |
427 | &dev_attr_rx_length_errors.attr, | |
428 | &dev_attr_rx_over_errors.attr, | |
429 | &dev_attr_rx_crc_errors.attr, | |
430 | &dev_attr_rx_frame_errors.attr, | |
431 | &dev_attr_rx_fifo_errors.attr, | |
432 | &dev_attr_rx_missed_errors.attr, | |
433 | &dev_attr_tx_aborted_errors.attr, | |
434 | &dev_attr_tx_carrier_errors.attr, | |
435 | &dev_attr_tx_fifo_errors.attr, | |
436 | &dev_attr_tx_heartbeat_errors.attr, | |
437 | &dev_attr_tx_window_errors.attr, | |
438 | &dev_attr_rx_compressed.attr, | |
439 | &dev_attr_tx_compressed.attr, | |
1da177e4 LT |
440 | NULL |
441 | }; | |
442 | ||
443 | ||
444 | static struct attribute_group netstat_group = { | |
445 | .name = "statistics", | |
446 | .attrs = netstat_attrs, | |
447 | }; | |
38c1a01c JB |
448 | |
449 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) | |
450 | static struct attribute *wireless_attrs[] = { | |
451 | NULL | |
452 | }; | |
453 | ||
454 | static struct attribute_group wireless_group = { | |
455 | .name = "wireless", | |
456 | .attrs = wireless_attrs, | |
457 | }; | |
458 | #endif | |
d6523ddf | 459 | #endif /* CONFIG_SYSFS */ |
1da177e4 | 460 | |
30bde1f5 | 461 | #ifdef CONFIG_RPS |
0a9627f2 TH |
462 | /* |
463 | * RX queue sysfs structures and functions. | |
464 | */ | |
465 | struct rx_queue_attribute { | |
466 | struct attribute attr; | |
467 | ssize_t (*show)(struct netdev_rx_queue *queue, | |
468 | struct rx_queue_attribute *attr, char *buf); | |
469 | ssize_t (*store)(struct netdev_rx_queue *queue, | |
470 | struct rx_queue_attribute *attr, const char *buf, size_t len); | |
471 | }; | |
472 | #define to_rx_queue_attr(_attr) container_of(_attr, \ | |
473 | struct rx_queue_attribute, attr) | |
474 | ||
475 | #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) | |
476 | ||
477 | static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, | |
478 | char *buf) | |
479 | { | |
480 | struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); | |
481 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
482 | ||
483 | if (!attribute->show) | |
484 | return -EIO; | |
485 | ||
486 | return attribute->show(queue, attribute, buf); | |
487 | } | |
488 | ||
489 | static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, | |
490 | const char *buf, size_t count) | |
491 | { | |
492 | struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); | |
493 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
494 | ||
495 | if (!attribute->store) | |
496 | return -EIO; | |
497 | ||
498 | return attribute->store(queue, attribute, buf, count); | |
499 | } | |
500 | ||
fa50d645 | 501 | static const struct sysfs_ops rx_queue_sysfs_ops = { |
0a9627f2 TH |
502 | .show = rx_queue_attr_show, |
503 | .store = rx_queue_attr_store, | |
504 | }; | |
505 | ||
506 | static ssize_t show_rps_map(struct netdev_rx_queue *queue, | |
507 | struct rx_queue_attribute *attribute, char *buf) | |
508 | { | |
509 | struct rps_map *map; | |
510 | cpumask_var_t mask; | |
511 | size_t len = 0; | |
512 | int i; | |
513 | ||
514 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) | |
515 | return -ENOMEM; | |
516 | ||
517 | rcu_read_lock(); | |
518 | map = rcu_dereference(queue->rps_map); | |
519 | if (map) | |
520 | for (i = 0; i < map->len; i++) | |
521 | cpumask_set_cpu(map->cpus[i], mask); | |
522 | ||
523 | len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); | |
524 | if (PAGE_SIZE - len < 3) { | |
525 | rcu_read_unlock(); | |
526 | free_cpumask_var(mask); | |
527 | return -EINVAL; | |
528 | } | |
529 | rcu_read_unlock(); | |
530 | ||
531 | free_cpumask_var(mask); | |
532 | len += sprintf(buf + len, "\n"); | |
533 | return len; | |
534 | } | |
535 | ||
f5acb907 | 536 | static ssize_t store_rps_map(struct netdev_rx_queue *queue, |
0a9627f2 TH |
537 | struct rx_queue_attribute *attribute, |
538 | const char *buf, size_t len) | |
539 | { | |
540 | struct rps_map *old_map, *map; | |
541 | cpumask_var_t mask; | |
542 | int err, cpu, i; | |
543 | static DEFINE_SPINLOCK(rps_map_lock); | |
544 | ||
545 | if (!capable(CAP_NET_ADMIN)) | |
546 | return -EPERM; | |
547 | ||
548 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
549 | return -ENOMEM; | |
550 | ||
551 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); | |
552 | if (err) { | |
553 | free_cpumask_var(mask); | |
554 | return err; | |
555 | } | |
556 | ||
95c96174 | 557 | map = kzalloc(max_t(unsigned int, |
0a9627f2 TH |
558 | RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), |
559 | GFP_KERNEL); | |
560 | if (!map) { | |
561 | free_cpumask_var(mask); | |
562 | return -ENOMEM; | |
563 | } | |
564 | ||
565 | i = 0; | |
566 | for_each_cpu_and(cpu, mask, cpu_online_mask) | |
567 | map->cpus[i++] = cpu; | |
568 | ||
569 | if (i) | |
570 | map->len = i; | |
571 | else { | |
572 | kfree(map); | |
573 | map = NULL; | |
574 | } | |
575 | ||
576 | spin_lock(&rps_map_lock); | |
6e3f7faf ED |
577 | old_map = rcu_dereference_protected(queue->rps_map, |
578 | lockdep_is_held(&rps_map_lock)); | |
0a9627f2 TH |
579 | rcu_assign_pointer(queue->rps_map, map); |
580 | spin_unlock(&rps_map_lock); | |
581 | ||
adc9300e | 582 | if (map) |
c5905afb | 583 | static_key_slow_inc(&rps_needed); |
adc9300e | 584 | if (old_map) { |
f6f80238 | 585 | kfree_rcu(old_map, rcu); |
c5905afb | 586 | static_key_slow_dec(&rps_needed); |
adc9300e | 587 | } |
0a9627f2 TH |
588 | free_cpumask_var(mask); |
589 | return len; | |
590 | } | |
591 | ||
fec5e652 TH |
592 | static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
593 | struct rx_queue_attribute *attr, | |
594 | char *buf) | |
595 | { | |
596 | struct rps_dev_flow_table *flow_table; | |
60b778ce | 597 | unsigned long val = 0; |
fec5e652 TH |
598 | |
599 | rcu_read_lock(); | |
600 | flow_table = rcu_dereference(queue->rps_flow_table); | |
601 | if (flow_table) | |
60b778ce | 602 | val = (unsigned long)flow_table->mask + 1; |
fec5e652 TH |
603 | rcu_read_unlock(); |
604 | ||
60b778ce | 605 | return sprintf(buf, "%lu\n", val); |
fec5e652 TH |
606 | } |
607 | ||
608 | static void rps_dev_flow_table_release_work(struct work_struct *work) | |
609 | { | |
610 | struct rps_dev_flow_table *table = container_of(work, | |
611 | struct rps_dev_flow_table, free_work); | |
612 | ||
613 | vfree(table); | |
614 | } | |
615 | ||
616 | static void rps_dev_flow_table_release(struct rcu_head *rcu) | |
617 | { | |
618 | struct rps_dev_flow_table *table = container_of(rcu, | |
619 | struct rps_dev_flow_table, rcu); | |
620 | ||
621 | INIT_WORK(&table->free_work, rps_dev_flow_table_release_work); | |
622 | schedule_work(&table->free_work); | |
623 | } | |
624 | ||
f5acb907 | 625 | static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
fec5e652 TH |
626 | struct rx_queue_attribute *attr, |
627 | const char *buf, size_t len) | |
628 | { | |
60b778ce | 629 | unsigned long mask, count; |
fec5e652 TH |
630 | struct rps_dev_flow_table *table, *old_table; |
631 | static DEFINE_SPINLOCK(rps_dev_flow_lock); | |
60b778ce | 632 | int rc; |
fec5e652 TH |
633 | |
634 | if (!capable(CAP_NET_ADMIN)) | |
635 | return -EPERM; | |
636 | ||
60b778ce ED |
637 | rc = kstrtoul(buf, 0, &count); |
638 | if (rc < 0) | |
639 | return rc; | |
fec5e652 TH |
640 | |
641 | if (count) { | |
60b778ce ED |
642 | mask = count - 1; |
643 | /* mask = roundup_pow_of_two(count) - 1; | |
644 | * without overflows... | |
645 | */ | |
646 | while ((mask | (mask >> 1)) != mask) | |
647 | mask |= (mask >> 1); | |
648 | /* On 64 bit arches, must check mask fits in table->mask (u32), | |
649 | * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1) | |
650 | * doesnt overflow. | |
651 | */ | |
652 | #if BITS_PER_LONG > 32 | |
653 | if (mask > (unsigned long)(u32)mask) | |
a0a129f8 | 654 | return -EINVAL; |
60b778ce ED |
655 | #else |
656 | if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) | |
a0a129f8 | 657 | / sizeof(struct rps_dev_flow)) { |
fec5e652 TH |
658 | /* Enforce a limit to prevent overflow */ |
659 | return -EINVAL; | |
660 | } | |
60b778ce ED |
661 | #endif |
662 | table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); | |
fec5e652 TH |
663 | if (!table) |
664 | return -ENOMEM; | |
665 | ||
60b778ce ED |
666 | table->mask = mask; |
667 | for (count = 0; count <= mask; count++) | |
668 | table->flows[count].cpu = RPS_NO_CPU; | |
fec5e652 TH |
669 | } else |
670 | table = NULL; | |
671 | ||
672 | spin_lock(&rps_dev_flow_lock); | |
6e3f7faf ED |
673 | old_table = rcu_dereference_protected(queue->rps_flow_table, |
674 | lockdep_is_held(&rps_dev_flow_lock)); | |
fec5e652 TH |
675 | rcu_assign_pointer(queue->rps_flow_table, table); |
676 | spin_unlock(&rps_dev_flow_lock); | |
677 | ||
678 | if (old_table) | |
679 | call_rcu(&old_table->rcu, rps_dev_flow_table_release); | |
680 | ||
681 | return len; | |
682 | } | |
683 | ||
0a9627f2 TH |
684 | static struct rx_queue_attribute rps_cpus_attribute = |
685 | __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); | |
686 | ||
fec5e652 TH |
687 | |
688 | static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute = | |
689 | __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, | |
690 | show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); | |
691 | ||
0a9627f2 TH |
692 | static struct attribute *rx_queue_default_attrs[] = { |
693 | &rps_cpus_attribute.attr, | |
fec5e652 | 694 | &rps_dev_flow_table_cnt_attribute.attr, |
0a9627f2 TH |
695 | NULL |
696 | }; | |
697 | ||
698 | static void rx_queue_release(struct kobject *kobj) | |
699 | { | |
700 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
6e3f7faf ED |
701 | struct rps_map *map; |
702 | struct rps_dev_flow_table *flow_table; | |
0a9627f2 | 703 | |
fec5e652 | 704 | |
33d480ce | 705 | map = rcu_dereference_protected(queue->rps_map, 1); |
9ea19481 JF |
706 | if (map) { |
707 | RCU_INIT_POINTER(queue->rps_map, NULL); | |
f6f80238 | 708 | kfree_rcu(map, rcu); |
9ea19481 | 709 | } |
6e3f7faf | 710 | |
33d480ce | 711 | flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); |
9ea19481 JF |
712 | if (flow_table) { |
713 | RCU_INIT_POINTER(queue->rps_flow_table, NULL); | |
6e3f7faf | 714 | call_rcu(&flow_table->rcu, rps_dev_flow_table_release); |
9ea19481 | 715 | } |
0a9627f2 | 716 | |
9ea19481 | 717 | memset(kobj, 0, sizeof(*kobj)); |
fe822240 | 718 | dev_put(queue->dev); |
0a9627f2 TH |
719 | } |
720 | ||
721 | static struct kobj_type rx_queue_ktype = { | |
722 | .sysfs_ops = &rx_queue_sysfs_ops, | |
723 | .release = rx_queue_release, | |
724 | .default_attrs = rx_queue_default_attrs, | |
725 | }; | |
726 | ||
727 | static int rx_queue_add_kobject(struct net_device *net, int index) | |
728 | { | |
729 | struct netdev_rx_queue *queue = net->_rx + index; | |
730 | struct kobject *kobj = &queue->kobj; | |
731 | int error = 0; | |
732 | ||
733 | kobj->kset = net->queues_kset; | |
734 | error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, | |
735 | "rx-%u", index); | |
736 | if (error) { | |
737 | kobject_put(kobj); | |
738 | return error; | |
739 | } | |
740 | ||
741 | kobject_uevent(kobj, KOBJ_ADD); | |
fe822240 | 742 | dev_hold(queue->dev); |
0a9627f2 TH |
743 | |
744 | return error; | |
745 | } | |
bf264145 | 746 | #endif /* CONFIG_RPS */ |
0a9627f2 | 747 | |
62fe0b40 BH |
748 | int |
749 | net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num) | |
0a9627f2 | 750 | { |
bf264145 | 751 | #ifdef CONFIG_RPS |
0a9627f2 TH |
752 | int i; |
753 | int error = 0; | |
754 | ||
62fe0b40 | 755 | for (i = old_num; i < new_num; i++) { |
0a9627f2 | 756 | error = rx_queue_add_kobject(net, i); |
62fe0b40 BH |
757 | if (error) { |
758 | new_num = old_num; | |
0a9627f2 | 759 | break; |
62fe0b40 | 760 | } |
0a9627f2 TH |
761 | } |
762 | ||
62fe0b40 BH |
763 | while (--i >= new_num) |
764 | kobject_put(&net->_rx[i].kobj); | |
0a9627f2 TH |
765 | |
766 | return error; | |
bf264145 TH |
767 | #else |
768 | return 0; | |
769 | #endif | |
0a9627f2 TH |
770 | } |
771 | ||
ccf5ff69 | 772 | #ifdef CONFIG_SYSFS |
1d24eb48 TH |
773 | /* |
774 | * netdev_queue sysfs structures and functions. | |
775 | */ | |
776 | struct netdev_queue_attribute { | |
777 | struct attribute attr; | |
778 | ssize_t (*show)(struct netdev_queue *queue, | |
779 | struct netdev_queue_attribute *attr, char *buf); | |
780 | ssize_t (*store)(struct netdev_queue *queue, | |
781 | struct netdev_queue_attribute *attr, const char *buf, size_t len); | |
782 | }; | |
783 | #define to_netdev_queue_attr(_attr) container_of(_attr, \ | |
784 | struct netdev_queue_attribute, attr) | |
785 | ||
786 | #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) | |
787 | ||
788 | static ssize_t netdev_queue_attr_show(struct kobject *kobj, | |
789 | struct attribute *attr, char *buf) | |
790 | { | |
791 | struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); | |
792 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
793 | ||
794 | if (!attribute->show) | |
795 | return -EIO; | |
796 | ||
797 | return attribute->show(queue, attribute, buf); | |
798 | } | |
799 | ||
800 | static ssize_t netdev_queue_attr_store(struct kobject *kobj, | |
801 | struct attribute *attr, | |
802 | const char *buf, size_t count) | |
803 | { | |
804 | struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr); | |
805 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
806 | ||
807 | if (!attribute->store) | |
808 | return -EIO; | |
809 | ||
810 | return attribute->store(queue, attribute, buf, count); | |
811 | } | |
812 | ||
813 | static const struct sysfs_ops netdev_queue_sysfs_ops = { | |
814 | .show = netdev_queue_attr_show, | |
815 | .store = netdev_queue_attr_store, | |
816 | }; | |
817 | ||
ccf5ff69 | 818 | static ssize_t show_trans_timeout(struct netdev_queue *queue, |
819 | struct netdev_queue_attribute *attribute, | |
820 | char *buf) | |
821 | { | |
822 | unsigned long trans_timeout; | |
823 | ||
824 | spin_lock_irq(&queue->_xmit_lock); | |
825 | trans_timeout = queue->trans_timeout; | |
826 | spin_unlock_irq(&queue->_xmit_lock); | |
827 | ||
828 | return sprintf(buf, "%lu", trans_timeout); | |
829 | } | |
830 | ||
831 | static struct netdev_queue_attribute queue_trans_timeout = | |
832 | __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL); | |
833 | ||
114cf580 TH |
834 | #ifdef CONFIG_BQL |
835 | /* | |
836 | * Byte queue limits sysfs structures and functions. | |
837 | */ | |
838 | static ssize_t bql_show(char *buf, unsigned int value) | |
839 | { | |
840 | return sprintf(buf, "%u\n", value); | |
841 | } | |
842 | ||
843 | static ssize_t bql_set(const char *buf, const size_t count, | |
844 | unsigned int *pvalue) | |
845 | { | |
846 | unsigned int value; | |
847 | int err; | |
848 | ||
849 | if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) | |
850 | value = DQL_MAX_LIMIT; | |
851 | else { | |
852 | err = kstrtouint(buf, 10, &value); | |
853 | if (err < 0) | |
854 | return err; | |
855 | if (value > DQL_MAX_LIMIT) | |
856 | return -EINVAL; | |
857 | } | |
858 | ||
859 | *pvalue = value; | |
860 | ||
861 | return count; | |
862 | } | |
863 | ||
864 | static ssize_t bql_show_hold_time(struct netdev_queue *queue, | |
865 | struct netdev_queue_attribute *attr, | |
866 | char *buf) | |
867 | { | |
868 | struct dql *dql = &queue->dql; | |
869 | ||
870 | return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); | |
871 | } | |
872 | ||
873 | static ssize_t bql_set_hold_time(struct netdev_queue *queue, | |
874 | struct netdev_queue_attribute *attribute, | |
875 | const char *buf, size_t len) | |
876 | { | |
877 | struct dql *dql = &queue->dql; | |
95c96174 | 878 | unsigned int value; |
114cf580 TH |
879 | int err; |
880 | ||
881 | err = kstrtouint(buf, 10, &value); | |
882 | if (err < 0) | |
883 | return err; | |
884 | ||
885 | dql->slack_hold_time = msecs_to_jiffies(value); | |
886 | ||
887 | return len; | |
888 | } | |
889 | ||
890 | static struct netdev_queue_attribute bql_hold_time_attribute = | |
891 | __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time, | |
892 | bql_set_hold_time); | |
893 | ||
894 | static ssize_t bql_show_inflight(struct netdev_queue *queue, | |
895 | struct netdev_queue_attribute *attr, | |
896 | char *buf) | |
897 | { | |
898 | struct dql *dql = &queue->dql; | |
899 | ||
900 | return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); | |
901 | } | |
902 | ||
903 | static struct netdev_queue_attribute bql_inflight_attribute = | |
795d9a25 | 904 | __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL); |
114cf580 TH |
905 | |
906 | #define BQL_ATTR(NAME, FIELD) \ | |
907 | static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ | |
908 | struct netdev_queue_attribute *attr, \ | |
909 | char *buf) \ | |
910 | { \ | |
911 | return bql_show(buf, queue->dql.FIELD); \ | |
912 | } \ | |
913 | \ | |
914 | static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ | |
915 | struct netdev_queue_attribute *attr, \ | |
916 | const char *buf, size_t len) \ | |
917 | { \ | |
918 | return bql_set(buf, len, &queue->dql.FIELD); \ | |
919 | } \ | |
920 | \ | |
921 | static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \ | |
922 | __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \ | |
923 | bql_set_ ## NAME); | |
924 | ||
925 | BQL_ATTR(limit, limit) | |
926 | BQL_ATTR(limit_max, max_limit) | |
927 | BQL_ATTR(limit_min, min_limit) | |
928 | ||
929 | static struct attribute *dql_attrs[] = { | |
930 | &bql_limit_attribute.attr, | |
931 | &bql_limit_max_attribute.attr, | |
932 | &bql_limit_min_attribute.attr, | |
933 | &bql_hold_time_attribute.attr, | |
934 | &bql_inflight_attribute.attr, | |
935 | NULL | |
936 | }; | |
937 | ||
938 | static struct attribute_group dql_group = { | |
939 | .name = "byte_queue_limits", | |
940 | .attrs = dql_attrs, | |
941 | }; | |
942 | #endif /* CONFIG_BQL */ | |
943 | ||
ccf5ff69 | 944 | #ifdef CONFIG_XPS |
1d24eb48 | 945 | static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue) |
0a9627f2 | 946 | { |
1d24eb48 TH |
947 | struct net_device *dev = queue->dev; |
948 | int i; | |
949 | ||
950 | for (i = 0; i < dev->num_tx_queues; i++) | |
951 | if (queue == &dev->_tx[i]) | |
952 | break; | |
953 | ||
954 | BUG_ON(i >= dev->num_tx_queues); | |
955 | ||
956 | return i; | |
957 | } | |
958 | ||
959 | ||
960 | static ssize_t show_xps_map(struct netdev_queue *queue, | |
961 | struct netdev_queue_attribute *attribute, char *buf) | |
962 | { | |
963 | struct net_device *dev = queue->dev; | |
964 | struct xps_dev_maps *dev_maps; | |
965 | cpumask_var_t mask; | |
966 | unsigned long index; | |
967 | size_t len = 0; | |
968 | int i; | |
969 | ||
970 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) | |
971 | return -ENOMEM; | |
972 | ||
973 | index = get_netdev_queue_index(queue); | |
974 | ||
975 | rcu_read_lock(); | |
976 | dev_maps = rcu_dereference(dev->xps_maps); | |
977 | if (dev_maps) { | |
978 | for_each_possible_cpu(i) { | |
979 | struct xps_map *map = | |
980 | rcu_dereference(dev_maps->cpu_map[i]); | |
981 | if (map) { | |
982 | int j; | |
983 | for (j = 0; j < map->len; j++) { | |
984 | if (map->queues[j] == index) { | |
985 | cpumask_set_cpu(i, mask); | |
986 | break; | |
987 | } | |
988 | } | |
989 | } | |
990 | } | |
991 | } | |
992 | rcu_read_unlock(); | |
993 | ||
994 | len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask); | |
995 | if (PAGE_SIZE - len < 3) { | |
996 | free_cpumask_var(mask); | |
997 | return -EINVAL; | |
998 | } | |
999 | ||
1000 | free_cpumask_var(mask); | |
1001 | len += sprintf(buf + len, "\n"); | |
1002 | return len; | |
1003 | } | |
1004 | ||
1d24eb48 TH |
1005 | static ssize_t store_xps_map(struct netdev_queue *queue, |
1006 | struct netdev_queue_attribute *attribute, | |
1007 | const char *buf, size_t len) | |
1008 | { | |
1009 | struct net_device *dev = queue->dev; | |
1d24eb48 | 1010 | unsigned long index; |
537c00de AD |
1011 | cpumask_var_t mask; |
1012 | int err; | |
1d24eb48 TH |
1013 | |
1014 | if (!capable(CAP_NET_ADMIN)) | |
1015 | return -EPERM; | |
1016 | ||
1017 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
1018 | return -ENOMEM; | |
1019 | ||
1020 | index = get_netdev_queue_index(queue); | |
1021 | ||
1022 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); | |
1023 | if (err) { | |
1024 | free_cpumask_var(mask); | |
1025 | return err; | |
1026 | } | |
1027 | ||
537c00de | 1028 | err = netif_set_xps_queue(dev, mask, index); |
1d24eb48 TH |
1029 | |
1030 | free_cpumask_var(mask); | |
1d24eb48 | 1031 | |
537c00de | 1032 | return err ? : len; |
1d24eb48 TH |
1033 | } |
1034 | ||
1035 | static struct netdev_queue_attribute xps_cpus_attribute = | |
1036 | __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map); | |
ccf5ff69 | 1037 | #endif /* CONFIG_XPS */ |
1d24eb48 TH |
1038 | |
1039 | static struct attribute *netdev_queue_default_attrs[] = { | |
ccf5ff69 | 1040 | &queue_trans_timeout.attr, |
1041 | #ifdef CONFIG_XPS | |
1d24eb48 | 1042 | &xps_cpus_attribute.attr, |
ccf5ff69 | 1043 | #endif |
1d24eb48 TH |
1044 | NULL |
1045 | }; | |
1046 | ||
1047 | static void netdev_queue_release(struct kobject *kobj) | |
1048 | { | |
1049 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
1d24eb48 | 1050 | |
1d24eb48 TH |
1051 | memset(kobj, 0, sizeof(*kobj)); |
1052 | dev_put(queue->dev); | |
1053 | } | |
1054 | ||
1055 | static struct kobj_type netdev_queue_ktype = { | |
1056 | .sysfs_ops = &netdev_queue_sysfs_ops, | |
1057 | .release = netdev_queue_release, | |
1058 | .default_attrs = netdev_queue_default_attrs, | |
1059 | }; | |
1060 | ||
1061 | static int netdev_queue_add_kobject(struct net_device *net, int index) | |
1062 | { | |
1063 | struct netdev_queue *queue = net->_tx + index; | |
1064 | struct kobject *kobj = &queue->kobj; | |
1065 | int error = 0; | |
1066 | ||
1067 | kobj->kset = net->queues_kset; | |
1068 | error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, | |
1069 | "tx-%u", index); | |
114cf580 TH |
1070 | if (error) |
1071 | goto exit; | |
1072 | ||
1073 | #ifdef CONFIG_BQL | |
1074 | error = sysfs_create_group(kobj, &dql_group); | |
1075 | if (error) | |
1076 | goto exit; | |
1077 | #endif | |
1d24eb48 TH |
1078 | |
1079 | kobject_uevent(kobj, KOBJ_ADD); | |
1080 | dev_hold(queue->dev); | |
1081 | ||
114cf580 TH |
1082 | return 0; |
1083 | exit: | |
1084 | kobject_put(kobj); | |
1d24eb48 TH |
1085 | return error; |
1086 | } | |
ccf5ff69 | 1087 | #endif /* CONFIG_SYSFS */ |
1d24eb48 TH |
1088 | |
1089 | int | |
1090 | netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num) | |
1091 | { | |
ccf5ff69 | 1092 | #ifdef CONFIG_SYSFS |
1d24eb48 TH |
1093 | int i; |
1094 | int error = 0; | |
1095 | ||
1096 | for (i = old_num; i < new_num; i++) { | |
1097 | error = netdev_queue_add_kobject(net, i); | |
1098 | if (error) { | |
1099 | new_num = old_num; | |
1100 | break; | |
1101 | } | |
1102 | } | |
1103 | ||
114cf580 TH |
1104 | while (--i >= new_num) { |
1105 | struct netdev_queue *queue = net->_tx + i; | |
1106 | ||
1107 | #ifdef CONFIG_BQL | |
1108 | sysfs_remove_group(&queue->kobj, &dql_group); | |
1109 | #endif | |
1110 | kobject_put(&queue->kobj); | |
1111 | } | |
1d24eb48 TH |
1112 | |
1113 | return error; | |
bf264145 TH |
1114 | #else |
1115 | return 0; | |
ccf5ff69 | 1116 | #endif /* CONFIG_SYSFS */ |
1d24eb48 TH |
1117 | } |
1118 | ||
1119 | static int register_queue_kobjects(struct net_device *net) | |
1120 | { | |
bf264145 | 1121 | int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; |
1d24eb48 | 1122 | |
ccf5ff69 | 1123 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
1124 | net->queues_kset = kset_create_and_add("queues", |
1125 | NULL, &net->dev.kobj); | |
1126 | if (!net->queues_kset) | |
1127 | return -ENOMEM; | |
bf264145 TH |
1128 | #endif |
1129 | ||
1130 | #ifdef CONFIG_RPS | |
1131 | real_rx = net->real_num_rx_queues; | |
1132 | #endif | |
1133 | real_tx = net->real_num_tx_queues; | |
1d24eb48 | 1134 | |
bf264145 | 1135 | error = net_rx_queue_update_kobjects(net, 0, real_rx); |
1d24eb48 TH |
1136 | if (error) |
1137 | goto error; | |
bf264145 | 1138 | rxq = real_rx; |
1d24eb48 | 1139 | |
bf264145 | 1140 | error = netdev_queue_update_kobjects(net, 0, real_tx); |
1d24eb48 TH |
1141 | if (error) |
1142 | goto error; | |
bf264145 | 1143 | txq = real_tx; |
1d24eb48 TH |
1144 | |
1145 | return 0; | |
1146 | ||
1147 | error: | |
1148 | netdev_queue_update_kobjects(net, txq, 0); | |
1149 | net_rx_queue_update_kobjects(net, rxq, 0); | |
1150 | return error; | |
62fe0b40 | 1151 | } |
0a9627f2 | 1152 | |
1d24eb48 | 1153 | static void remove_queue_kobjects(struct net_device *net) |
62fe0b40 | 1154 | { |
bf264145 TH |
1155 | int real_rx = 0, real_tx = 0; |
1156 | ||
1157 | #ifdef CONFIG_RPS | |
1158 | real_rx = net->real_num_rx_queues; | |
1159 | #endif | |
1160 | real_tx = net->real_num_tx_queues; | |
1161 | ||
1162 | net_rx_queue_update_kobjects(net, real_rx, 0); | |
1163 | netdev_queue_update_kobjects(net, real_tx, 0); | |
ccf5ff69 | 1164 | #ifdef CONFIG_SYSFS |
0a9627f2 | 1165 | kset_unregister(net->queues_kset); |
bf264145 | 1166 | #endif |
0a9627f2 | 1167 | } |
608b4b95 | 1168 | |
a685e089 | 1169 | static void *net_grab_current_ns(void) |
608b4b95 | 1170 | { |
a685e089 AV |
1171 | struct net *ns = current->nsproxy->net_ns; |
1172 | #ifdef CONFIG_NET_NS | |
1173 | if (ns) | |
1174 | atomic_inc(&ns->passive); | |
1175 | #endif | |
1176 | return ns; | |
608b4b95 EB |
1177 | } |
1178 | ||
1179 | static const void *net_initial_ns(void) | |
1180 | { | |
1181 | return &init_net; | |
1182 | } | |
1183 | ||
1184 | static const void *net_netlink_ns(struct sock *sk) | |
1185 | { | |
1186 | return sock_net(sk); | |
1187 | } | |
1188 | ||
04600794 | 1189 | struct kobj_ns_type_operations net_ns_type_operations = { |
608b4b95 | 1190 | .type = KOBJ_NS_TYPE_NET, |
a685e089 | 1191 | .grab_current_ns = net_grab_current_ns, |
608b4b95 EB |
1192 | .netlink_ns = net_netlink_ns, |
1193 | .initial_ns = net_initial_ns, | |
a685e089 | 1194 | .drop_ns = net_drop_ns, |
608b4b95 | 1195 | }; |
04600794 | 1196 | EXPORT_SYMBOL_GPL(net_ns_type_operations); |
608b4b95 | 1197 | |
7eff2e7a | 1198 | static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) |
1da177e4 | 1199 | { |
43cb76d9 | 1200 | struct net_device *dev = to_net_dev(d); |
7eff2e7a | 1201 | int retval; |
1da177e4 | 1202 | |
312c004d | 1203 | /* pass interface to uevent. */ |
7eff2e7a | 1204 | retval = add_uevent_var(env, "INTERFACE=%s", dev->name); |
bf62456e ER |
1205 | if (retval) |
1206 | goto exit; | |
ca2f37db JT |
1207 | |
1208 | /* pass ifindex to uevent. | |
1209 | * ifindex is useful as it won't change (interface name may change) | |
1210 | * and is what RtNetlink uses natively. */ | |
7eff2e7a | 1211 | retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); |
1da177e4 | 1212 | |
bf62456e | 1213 | exit: |
bf62456e | 1214 | return retval; |
1da177e4 | 1215 | } |
1da177e4 LT |
1216 | |
1217 | /* | |
4ec93edb | 1218 | * netdev_release -- destroy and free a dead device. |
43cb76d9 | 1219 | * Called when last reference to device kobject is gone. |
1da177e4 | 1220 | */ |
43cb76d9 | 1221 | static void netdev_release(struct device *d) |
1da177e4 | 1222 | { |
43cb76d9 | 1223 | struct net_device *dev = to_net_dev(d); |
1da177e4 LT |
1224 | |
1225 | BUG_ON(dev->reg_state != NETREG_RELEASED); | |
1226 | ||
0b815a1a | 1227 | kfree(dev->ifalias); |
1da177e4 LT |
1228 | kfree((char *)dev - dev->padded); |
1229 | } | |
1230 | ||
608b4b95 EB |
1231 | static const void *net_namespace(struct device *d) |
1232 | { | |
1233 | struct net_device *dev; | |
1234 | dev = container_of(d, struct net_device, dev); | |
1235 | return dev_net(dev); | |
1236 | } | |
1237 | ||
1da177e4 LT |
1238 | static struct class net_class = { |
1239 | .name = "net", | |
43cb76d9 | 1240 | .dev_release = netdev_release, |
8b41d188 | 1241 | #ifdef CONFIG_SYSFS |
43cb76d9 | 1242 | .dev_attrs = net_class_attributes, |
8b41d188 | 1243 | #endif /* CONFIG_SYSFS */ |
43cb76d9 | 1244 | .dev_uevent = netdev_uevent, |
608b4b95 EB |
1245 | .ns_type = &net_ns_type_operations, |
1246 | .namespace = net_namespace, | |
1da177e4 LT |
1247 | }; |
1248 | ||
9093bbb2 SH |
1249 | /* Delete sysfs entries but hold kobject reference until after all |
1250 | * netdev references are gone. | |
1251 | */ | |
8b41d188 | 1252 | void netdev_unregister_kobject(struct net_device * net) |
1da177e4 | 1253 | { |
9093bbb2 SH |
1254 | struct device *dev = &(net->dev); |
1255 | ||
1256 | kobject_get(&dev->kobj); | |
3891845e | 1257 | |
1d24eb48 | 1258 | remove_queue_kobjects(net); |
0a9627f2 | 1259 | |
9093bbb2 | 1260 | device_del(dev); |
1da177e4 LT |
1261 | } |
1262 | ||
1263 | /* Create sysfs entries for network device. */ | |
8b41d188 | 1264 | int netdev_register_kobject(struct net_device *net) |
1da177e4 | 1265 | { |
43cb76d9 | 1266 | struct device *dev = &(net->dev); |
a4dbd674 | 1267 | const struct attribute_group **groups = net->sysfs_groups; |
0a9627f2 | 1268 | int error = 0; |
1da177e4 | 1269 | |
a1b3f594 | 1270 | device_initialize(dev); |
43cb76d9 GKH |
1271 | dev->class = &net_class; |
1272 | dev->platform_data = net; | |
1273 | dev->groups = groups; | |
1da177e4 | 1274 | |
a2205472 | 1275 | dev_set_name(dev, "%s", net->name); |
1da177e4 | 1276 | |
8b41d188 | 1277 | #ifdef CONFIG_SYSFS |
0c509a6c EB |
1278 | /* Allow for a device specific group */ |
1279 | if (*groups) | |
1280 | groups++; | |
1da177e4 | 1281 | |
0c509a6c | 1282 | *groups++ = &netstat_group; |
38c1a01c JB |
1283 | |
1284 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) | |
1285 | if (net->ieee80211_ptr) | |
1286 | *groups++ = &wireless_group; | |
1287 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) | |
1288 | else if (net->wireless_handlers) | |
1289 | *groups++ = &wireless_group; | |
1290 | #endif | |
1291 | #endif | |
8b41d188 | 1292 | #endif /* CONFIG_SYSFS */ |
1da177e4 | 1293 | |
0a9627f2 TH |
1294 | error = device_add(dev); |
1295 | if (error) | |
1296 | return error; | |
1297 | ||
1d24eb48 | 1298 | error = register_queue_kobjects(net); |
0a9627f2 TH |
1299 | if (error) { |
1300 | device_del(dev); | |
1301 | return error; | |
1302 | } | |
1303 | ||
1304 | return error; | |
1da177e4 LT |
1305 | } |
1306 | ||
b8a9787e JV |
1307 | int netdev_class_create_file(struct class_attribute *class_attr) |
1308 | { | |
1309 | return class_create_file(&net_class, class_attr); | |
1310 | } | |
9e34a5b5 | 1311 | EXPORT_SYMBOL(netdev_class_create_file); |
b8a9787e JV |
1312 | |
1313 | void netdev_class_remove_file(struct class_attribute *class_attr) | |
1314 | { | |
1315 | class_remove_file(&net_class, class_attr); | |
1316 | } | |
b8a9787e JV |
1317 | EXPORT_SYMBOL(netdev_class_remove_file); |
1318 | ||
8b41d188 | 1319 | int netdev_kobject_init(void) |
1da177e4 | 1320 | { |
608b4b95 | 1321 | kobj_ns_type_register(&net_ns_type_operations); |
1da177e4 LT |
1322 | return class_register(&net_class); |
1323 | } |