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