Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * net-sysfs.c - network device class and attributes | |
4 | * | |
5 | * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org> | |
1da177e4 LT |
6 | */ |
7 | ||
4fc268d2 | 8 | #include <linux/capability.h> |
1da177e4 LT |
9 | #include <linux/kernel.h> |
10 | #include <linux/netdevice.h> | |
11 | #include <linux/if_arp.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
174cd4b1 | 13 | #include <linux/sched/signal.h> |
07bbecb3 | 14 | #include <linux/sched/isolation.h> |
608b4b95 | 15 | #include <linux/nsproxy.h> |
1da177e4 | 16 | #include <net/sock.h> |
608b4b95 | 17 | #include <net/net_namespace.h> |
1da177e4 | 18 | #include <linux/rtnetlink.h> |
fec5e652 | 19 | #include <linux/vmalloc.h> |
bc3b2d7f | 20 | #include <linux/export.h> |
114cf580 | 21 | #include <linux/jiffies.h> |
9802c8e2 | 22 | #include <linux/pm_runtime.h> |
aa836df9 | 23 | #include <linux/of.h> |
88832a22 | 24 | #include <linux/of_net.h> |
4d99f660 | 25 | #include <linux/cpu.h> |
1da177e4 | 26 | |
342709ef PE |
27 | #include "net-sysfs.h" |
28 | ||
8b41d188 | 29 | #ifdef CONFIG_SYSFS |
1da177e4 LT |
30 | static const char fmt_hex[] = "%#x\n"; |
31 | static const char fmt_dec[] = "%d\n"; | |
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 | { | |
6b53dafe | 45 | struct net_device *ndev = to_net_dev(dev); |
1da177e4 LT |
46 | ssize_t ret = -EINVAL; |
47 | ||
48 | read_lock(&dev_base_lock); | |
6b53dafe WC |
49 | if (dev_isalive(ndev)) |
50 | ret = (*format)(ndev, buf); | |
1da177e4 LT |
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) \ | |
6b53dafe | 58 | static ssize_t format_##field(const struct net_device *dev, char *buf) \ |
1da177e4 | 59 | { \ |
6b53dafe | 60 | return sprintf(buf, format_string, dev->field); \ |
1da177e4 | 61 | } \ |
6be8aeef | 62 | static ssize_t field##_show(struct device *dev, \ |
43cb76d9 | 63 | struct device_attribute *attr, char *buf) \ |
1da177e4 | 64 | { \ |
43cb76d9 | 65 | return netdev_show(dev, attr, buf, format_##field); \ |
6be8aeef GKH |
66 | } \ |
67 | ||
68 | #define NETDEVICE_SHOW_RO(field, format_string) \ | |
69 | NETDEVICE_SHOW(field, format_string); \ | |
70 | static DEVICE_ATTR_RO(field) | |
1da177e4 | 71 | |
6be8aeef GKH |
72 | #define NETDEVICE_SHOW_RW(field, format_string) \ |
73 | NETDEVICE_SHOW(field, format_string); \ | |
74 | static DEVICE_ATTR_RW(field) | |
1da177e4 LT |
75 | |
76 | /* use same locking and permission rules as SIF* ioctl's */ | |
43cb76d9 | 77 | static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, |
1da177e4 LT |
78 | const char *buf, size_t len, |
79 | int (*set)(struct net_device *, unsigned long)) | |
80 | { | |
5e1fccc0 EB |
81 | struct net_device *netdev = to_net_dev(dev); |
82 | struct net *net = dev_net(netdev); | |
1da177e4 | 83 | unsigned long new; |
5f0224a6 | 84 | int ret; |
1da177e4 | 85 | |
5e1fccc0 | 86 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
87 | return -EPERM; |
88 | ||
e1e420c7 SK |
89 | ret = kstrtoul(buf, 0, &new); |
90 | if (ret) | |
1da177e4 LT |
91 | goto err; |
92 | ||
5a5990d3 | 93 | if (!rtnl_trylock()) |
336ca57c | 94 | return restart_syscall(); |
5a5990d3 | 95 | |
5e1fccc0 | 96 | if (dev_isalive(netdev)) { |
6648c65e | 97 | ret = (*set)(netdev, new); |
98 | if (ret == 0) | |
1da177e4 LT |
99 | ret = len; |
100 | } | |
101 | rtnl_unlock(); | |
102 | err: | |
103 | return ret; | |
104 | } | |
105 | ||
6be8aeef | 106 | NETDEVICE_SHOW_RO(dev_id, fmt_hex); |
3f85944f | 107 | NETDEVICE_SHOW_RO(dev_port, fmt_dec); |
6be8aeef GKH |
108 | NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec); |
109 | NETDEVICE_SHOW_RO(addr_len, fmt_dec); | |
6be8aeef GKH |
110 | NETDEVICE_SHOW_RO(ifindex, fmt_dec); |
111 | NETDEVICE_SHOW_RO(type, fmt_dec); | |
112 | NETDEVICE_SHOW_RO(link_mode, fmt_dec); | |
1da177e4 | 113 | |
a54acb3a ND |
114 | static ssize_t iflink_show(struct device *dev, struct device_attribute *attr, |
115 | char *buf) | |
116 | { | |
117 | struct net_device *ndev = to_net_dev(dev); | |
118 | ||
119 | return sprintf(buf, fmt_dec, dev_get_iflink(ndev)); | |
120 | } | |
121 | static DEVICE_ATTR_RO(iflink); | |
122 | ||
6b53dafe | 123 | static ssize_t format_name_assign_type(const struct net_device *dev, char *buf) |
685343fc | 124 | { |
6b53dafe | 125 | return sprintf(buf, fmt_dec, dev->name_assign_type); |
685343fc TG |
126 | } |
127 | ||
128 | static ssize_t name_assign_type_show(struct device *dev, | |
129 | struct device_attribute *attr, | |
130 | char *buf) | |
131 | { | |
6b53dafe | 132 | struct net_device *ndev = to_net_dev(dev); |
685343fc TG |
133 | ssize_t ret = -EINVAL; |
134 | ||
6b53dafe | 135 | if (ndev->name_assign_type != NET_NAME_UNKNOWN) |
685343fc TG |
136 | ret = netdev_show(dev, attr, buf, format_name_assign_type); |
137 | ||
138 | return ret; | |
139 | } | |
140 | static DEVICE_ATTR_RO(name_assign_type); | |
141 | ||
1da177e4 | 142 | /* use same locking rules as GIFHWADDR ioctl's */ |
6be8aeef | 143 | static ssize_t address_show(struct device *dev, struct device_attribute *attr, |
43cb76d9 | 144 | char *buf) |
1da177e4 | 145 | { |
6b53dafe | 146 | struct net_device *ndev = to_net_dev(dev); |
1da177e4 LT |
147 | ssize_t ret = -EINVAL; |
148 | ||
149 | read_lock(&dev_base_lock); | |
6b53dafe WC |
150 | if (dev_isalive(ndev)) |
151 | ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len); | |
1da177e4 LT |
152 | read_unlock(&dev_base_lock); |
153 | return ret; | |
154 | } | |
6be8aeef | 155 | static DEVICE_ATTR_RO(address); |
1da177e4 | 156 | |
6be8aeef GKH |
157 | static ssize_t broadcast_show(struct device *dev, |
158 | struct device_attribute *attr, char *buf) | |
1da177e4 | 159 | { |
6b53dafe | 160 | struct net_device *ndev = to_net_dev(dev); |
6648c65e | 161 | |
6b53dafe WC |
162 | if (dev_isalive(ndev)) |
163 | return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len); | |
1da177e4 LT |
164 | return -EINVAL; |
165 | } | |
6be8aeef | 166 | static DEVICE_ATTR_RO(broadcast); |
1da177e4 | 167 | |
6b53dafe | 168 | static int change_carrier(struct net_device *dev, unsigned long new_carrier) |
fdae0fde | 169 | { |
6b53dafe | 170 | if (!netif_running(dev)) |
fdae0fde | 171 | return -EINVAL; |
6648c65e | 172 | return dev_change_carrier(dev, (bool)new_carrier); |
fdae0fde JP |
173 | } |
174 | ||
6be8aeef GKH |
175 | static ssize_t carrier_store(struct device *dev, struct device_attribute *attr, |
176 | const char *buf, size_t len) | |
fdae0fde JP |
177 | { |
178 | return netdev_store(dev, attr, buf, len, change_carrier); | |
179 | } | |
180 | ||
6be8aeef | 181 | static ssize_t carrier_show(struct device *dev, |
43cb76d9 | 182 | struct device_attribute *attr, char *buf) |
1da177e4 LT |
183 | { |
184 | struct net_device *netdev = to_net_dev(dev); | |
6648c65e | 185 | |
186 | if (netif_running(netdev)) | |
1da177e4 | 187 | return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); |
6648c65e | 188 | |
1da177e4 LT |
189 | return -EINVAL; |
190 | } | |
6be8aeef | 191 | static DEVICE_ATTR_RW(carrier); |
1da177e4 | 192 | |
6be8aeef | 193 | static ssize_t speed_show(struct device *dev, |
d519e17e AG |
194 | struct device_attribute *attr, char *buf) |
195 | { | |
196 | struct net_device *netdev = to_net_dev(dev); | |
197 | int ret = -EINVAL; | |
198 | ||
199 | if (!rtnl_trylock()) | |
200 | return restart_syscall(); | |
201 | ||
8ae6daca | 202 | if (netif_running(netdev)) { |
7cad1bac DD |
203 | struct ethtool_link_ksettings cmd; |
204 | ||
205 | if (!__ethtool_get_link_ksettings(netdev, &cmd)) | |
206 | ret = sprintf(buf, fmt_dec, cmd.base.speed); | |
d519e17e AG |
207 | } |
208 | rtnl_unlock(); | |
209 | return ret; | |
210 | } | |
6be8aeef | 211 | static DEVICE_ATTR_RO(speed); |
d519e17e | 212 | |
6be8aeef | 213 | static ssize_t duplex_show(struct device *dev, |
d519e17e AG |
214 | struct device_attribute *attr, char *buf) |
215 | { | |
216 | struct net_device *netdev = to_net_dev(dev); | |
217 | int ret = -EINVAL; | |
218 | ||
219 | if (!rtnl_trylock()) | |
220 | return restart_syscall(); | |
221 | ||
8ae6daca | 222 | if (netif_running(netdev)) { |
7cad1bac DD |
223 | struct ethtool_link_ksettings cmd; |
224 | ||
225 | if (!__ethtool_get_link_ksettings(netdev, &cmd)) { | |
c6c13965 | 226 | const char *duplex; |
7cad1bac DD |
227 | |
228 | switch (cmd.base.duplex) { | |
c6c13965 NA |
229 | case DUPLEX_HALF: |
230 | duplex = "half"; | |
231 | break; | |
232 | case DUPLEX_FULL: | |
233 | duplex = "full"; | |
234 | break; | |
235 | default: | |
236 | duplex = "unknown"; | |
237 | break; | |
238 | } | |
239 | ret = sprintf(buf, "%s\n", duplex); | |
240 | } | |
d519e17e AG |
241 | } |
242 | rtnl_unlock(); | |
243 | return ret; | |
244 | } | |
6be8aeef | 245 | static DEVICE_ATTR_RO(duplex); |
d519e17e | 246 | |
db30a577 AL |
247 | static ssize_t testing_show(struct device *dev, |
248 | struct device_attribute *attr, char *buf) | |
249 | { | |
250 | struct net_device *netdev = to_net_dev(dev); | |
251 | ||
252 | if (netif_running(netdev)) | |
253 | return sprintf(buf, fmt_dec, !!netif_testing(netdev)); | |
254 | ||
255 | return -EINVAL; | |
256 | } | |
257 | static DEVICE_ATTR_RO(testing); | |
258 | ||
6be8aeef | 259 | static ssize_t dormant_show(struct device *dev, |
43cb76d9 | 260 | struct device_attribute *attr, char *buf) |
b00055aa SR |
261 | { |
262 | struct net_device *netdev = to_net_dev(dev); | |
263 | ||
264 | if (netif_running(netdev)) | |
265 | return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); | |
266 | ||
267 | return -EINVAL; | |
268 | } | |
6be8aeef | 269 | static DEVICE_ATTR_RO(dormant); |
b00055aa | 270 | |
36cbd3dc | 271 | static const char *const operstates[] = { |
b00055aa SR |
272 | "unknown", |
273 | "notpresent", /* currently unused */ | |
274 | "down", | |
275 | "lowerlayerdown", | |
db30a577 | 276 | "testing", |
b00055aa SR |
277 | "dormant", |
278 | "up" | |
279 | }; | |
280 | ||
6be8aeef | 281 | static ssize_t operstate_show(struct device *dev, |
43cb76d9 | 282 | struct device_attribute *attr, char *buf) |
b00055aa SR |
283 | { |
284 | const struct net_device *netdev = to_net_dev(dev); | |
285 | unsigned char operstate; | |
286 | ||
287 | read_lock(&dev_base_lock); | |
288 | operstate = netdev->operstate; | |
289 | if (!netif_running(netdev)) | |
290 | operstate = IF_OPER_DOWN; | |
291 | read_unlock(&dev_base_lock); | |
292 | ||
e3a5cd9e | 293 | if (operstate >= ARRAY_SIZE(operstates)) |
b00055aa SR |
294 | return -EINVAL; /* should not happen */ |
295 | ||
296 | return sprintf(buf, "%s\n", operstates[operstate]); | |
297 | } | |
6be8aeef | 298 | static DEVICE_ATTR_RO(operstate); |
b00055aa | 299 | |
2d3b479d | 300 | static ssize_t carrier_changes_show(struct device *dev, |
301 | struct device_attribute *attr, | |
302 | char *buf) | |
303 | { | |
304 | struct net_device *netdev = to_net_dev(dev); | |
6648c65e | 305 | |
2d3b479d | 306 | return sprintf(buf, fmt_dec, |
b2d3bcfa DD |
307 | atomic_read(&netdev->carrier_up_count) + |
308 | atomic_read(&netdev->carrier_down_count)); | |
2d3b479d | 309 | } |
310 | static DEVICE_ATTR_RO(carrier_changes); | |
311 | ||
b2d3bcfa DD |
312 | static ssize_t carrier_up_count_show(struct device *dev, |
313 | struct device_attribute *attr, | |
314 | char *buf) | |
315 | { | |
316 | struct net_device *netdev = to_net_dev(dev); | |
317 | ||
318 | return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count)); | |
319 | } | |
320 | static DEVICE_ATTR_RO(carrier_up_count); | |
321 | ||
322 | static ssize_t carrier_down_count_show(struct device *dev, | |
323 | struct device_attribute *attr, | |
324 | char *buf) | |
325 | { | |
326 | struct net_device *netdev = to_net_dev(dev); | |
327 | ||
328 | return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count)); | |
329 | } | |
330 | static DEVICE_ATTR_RO(carrier_down_count); | |
331 | ||
1da177e4 | 332 | /* read-write attributes */ |
1da177e4 | 333 | |
6b53dafe | 334 | static int change_mtu(struct net_device *dev, unsigned long new_mtu) |
1da177e4 | 335 | { |
6648c65e | 336 | return dev_set_mtu(dev, (int)new_mtu); |
1da177e4 LT |
337 | } |
338 | ||
6be8aeef | 339 | static ssize_t mtu_store(struct device *dev, struct device_attribute *attr, |
43cb76d9 | 340 | const char *buf, size_t len) |
1da177e4 | 341 | { |
43cb76d9 | 342 | return netdev_store(dev, attr, buf, len, change_mtu); |
1da177e4 | 343 | } |
6be8aeef | 344 | NETDEVICE_SHOW_RW(mtu, fmt_dec); |
1da177e4 | 345 | |
6b53dafe | 346 | static int change_flags(struct net_device *dev, unsigned long new_flags) |
1da177e4 | 347 | { |
567c5e13 | 348 | return dev_change_flags(dev, (unsigned int)new_flags, NULL); |
1da177e4 LT |
349 | } |
350 | ||
6be8aeef | 351 | static ssize_t flags_store(struct device *dev, struct device_attribute *attr, |
43cb76d9 | 352 | const char *buf, size_t len) |
1da177e4 | 353 | { |
43cb76d9 | 354 | return netdev_store(dev, attr, buf, len, change_flags); |
1da177e4 | 355 | } |
6be8aeef | 356 | NETDEVICE_SHOW_RW(flags, fmt_hex); |
1da177e4 | 357 | |
6be8aeef | 358 | static ssize_t tx_queue_len_store(struct device *dev, |
43cb76d9 GKH |
359 | struct device_attribute *attr, |
360 | const char *buf, size_t len) | |
1da177e4 | 361 | { |
5e1fccc0 EB |
362 | if (!capable(CAP_NET_ADMIN)) |
363 | return -EPERM; | |
364 | ||
6a643ddb | 365 | return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len); |
1da177e4 | 366 | } |
0cd29503 | 367 | NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec); |
1da177e4 | 368 | |
3b47d303 ED |
369 | static int change_gro_flush_timeout(struct net_device *dev, unsigned long val) |
370 | { | |
7e417a66 | 371 | WRITE_ONCE(dev->gro_flush_timeout, val); |
3b47d303 ED |
372 | return 0; |
373 | } | |
374 | ||
375 | static ssize_t gro_flush_timeout_store(struct device *dev, | |
6648c65e | 376 | struct device_attribute *attr, |
377 | const char *buf, size_t len) | |
3b47d303 ED |
378 | { |
379 | if (!capable(CAP_NET_ADMIN)) | |
380 | return -EPERM; | |
381 | ||
382 | return netdev_store(dev, attr, buf, len, change_gro_flush_timeout); | |
383 | } | |
384 | NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong); | |
385 | ||
6f8b12d6 ED |
386 | static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val) |
387 | { | |
7e417a66 | 388 | WRITE_ONCE(dev->napi_defer_hard_irqs, val); |
6f8b12d6 ED |
389 | return 0; |
390 | } | |
391 | ||
392 | static ssize_t napi_defer_hard_irqs_store(struct device *dev, | |
393 | struct device_attribute *attr, | |
394 | const char *buf, size_t len) | |
395 | { | |
396 | if (!capable(CAP_NET_ADMIN)) | |
397 | return -EPERM; | |
398 | ||
399 | return netdev_store(dev, attr, buf, len, change_napi_defer_hard_irqs); | |
400 | } | |
401 | NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_dec); | |
402 | ||
6be8aeef | 403 | static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr, |
0b815a1a SH |
404 | const char *buf, size_t len) |
405 | { | |
406 | struct net_device *netdev = to_net_dev(dev); | |
5e1fccc0 | 407 | struct net *net = dev_net(netdev); |
0b815a1a | 408 | size_t count = len; |
c92eb77a | 409 | ssize_t ret = 0; |
0b815a1a | 410 | |
5e1fccc0 | 411 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
0b815a1a SH |
412 | return -EPERM; |
413 | ||
414 | /* ignore trailing newline */ | |
415 | if (len > 0 && buf[len - 1] == '\n') | |
416 | --count; | |
417 | ||
c92eb77a RP |
418 | if (!rtnl_trylock()) |
419 | return restart_syscall(); | |
0b815a1a | 420 | |
c92eb77a RP |
421 | if (dev_isalive(netdev)) { |
422 | ret = dev_set_alias(netdev, buf, count); | |
423 | if (ret < 0) | |
424 | goto err; | |
425 | ret = len; | |
426 | netdev_state_change(netdev); | |
427 | } | |
428 | err: | |
429 | rtnl_unlock(); | |
430 | ||
431 | return ret; | |
0b815a1a SH |
432 | } |
433 | ||
6be8aeef | 434 | static ssize_t ifalias_show(struct device *dev, |
0b815a1a SH |
435 | struct device_attribute *attr, char *buf) |
436 | { | |
437 | const struct net_device *netdev = to_net_dev(dev); | |
6c557001 | 438 | char tmp[IFALIASZ]; |
0b815a1a SH |
439 | ssize_t ret = 0; |
440 | ||
6c557001 FW |
441 | ret = dev_get_alias(netdev, tmp, sizeof(tmp)); |
442 | if (ret > 0) | |
443 | ret = sprintf(buf, "%s\n", tmp); | |
0b815a1a SH |
444 | return ret; |
445 | } | |
6be8aeef | 446 | static DEVICE_ATTR_RW(ifalias); |
a512b92b | 447 | |
6b53dafe | 448 | static int change_group(struct net_device *dev, unsigned long new_group) |
a512b92b | 449 | { |
6648c65e | 450 | dev_set_group(dev, (int)new_group); |
a512b92b VD |
451 | return 0; |
452 | } | |
453 | ||
6be8aeef GKH |
454 | static ssize_t group_store(struct device *dev, struct device_attribute *attr, |
455 | const char *buf, size_t len) | |
a512b92b VD |
456 | { |
457 | return netdev_store(dev, attr, buf, len, change_group); | |
458 | } | |
6be8aeef | 459 | NETDEVICE_SHOW(group, fmt_dec); |
d6444062 | 460 | static DEVICE_ATTR(netdev_group, 0644, group_show, group_store); |
6be8aeef | 461 | |
d746d707 AK |
462 | static int change_proto_down(struct net_device *dev, unsigned long proto_down) |
463 | { | |
6648c65e | 464 | return dev_change_proto_down(dev, (bool)proto_down); |
d746d707 AK |
465 | } |
466 | ||
467 | static ssize_t proto_down_store(struct device *dev, | |
468 | struct device_attribute *attr, | |
469 | const char *buf, size_t len) | |
470 | { | |
471 | return netdev_store(dev, attr, buf, len, change_proto_down); | |
472 | } | |
473 | NETDEVICE_SHOW_RW(proto_down, fmt_dec); | |
474 | ||
cc998ff8 | 475 | static ssize_t phys_port_id_show(struct device *dev, |
ff80e519 JP |
476 | struct device_attribute *attr, char *buf) |
477 | { | |
478 | struct net_device *netdev = to_net_dev(dev); | |
479 | ssize_t ret = -EINVAL; | |
480 | ||
481 | if (!rtnl_trylock()) | |
482 | return restart_syscall(); | |
483 | ||
484 | if (dev_isalive(netdev)) { | |
02637fce | 485 | struct netdev_phys_item_id ppid; |
ff80e519 JP |
486 | |
487 | ret = dev_get_phys_port_id(netdev, &ppid); | |
488 | if (!ret) | |
489 | ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id); | |
490 | } | |
491 | rtnl_unlock(); | |
492 | ||
493 | return ret; | |
494 | } | |
cc998ff8 LT |
495 | static DEVICE_ATTR_RO(phys_port_id); |
496 | ||
db24a904 DA |
497 | static ssize_t phys_port_name_show(struct device *dev, |
498 | struct device_attribute *attr, char *buf) | |
499 | { | |
500 | struct net_device *netdev = to_net_dev(dev); | |
501 | ssize_t ret = -EINVAL; | |
502 | ||
503 | if (!rtnl_trylock()) | |
504 | return restart_syscall(); | |
505 | ||
506 | if (dev_isalive(netdev)) { | |
507 | char name[IFNAMSIZ]; | |
508 | ||
509 | ret = dev_get_phys_port_name(netdev, name, sizeof(name)); | |
510 | if (!ret) | |
511 | ret = sprintf(buf, "%s\n", name); | |
512 | } | |
513 | rtnl_unlock(); | |
514 | ||
515 | return ret; | |
516 | } | |
517 | static DEVICE_ATTR_RO(phys_port_name); | |
518 | ||
aecbe01e JP |
519 | static ssize_t phys_switch_id_show(struct device *dev, |
520 | struct device_attribute *attr, char *buf) | |
521 | { | |
522 | struct net_device *netdev = to_net_dev(dev); | |
523 | ssize_t ret = -EINVAL; | |
524 | ||
525 | if (!rtnl_trylock()) | |
526 | return restart_syscall(); | |
527 | ||
528 | if (dev_isalive(netdev)) { | |
bccb3025 FF |
529 | struct netdev_phys_item_id ppid = { }; |
530 | ||
531 | ret = dev_get_port_parent_id(netdev, &ppid, false); | |
aecbe01e | 532 | if (!ret) |
bccb3025 | 533 | ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id); |
aecbe01e JP |
534 | } |
535 | rtnl_unlock(); | |
536 | ||
537 | return ret; | |
538 | } | |
539 | static DEVICE_ATTR_RO(phys_switch_id); | |
540 | ||
5fdd2f0e WW |
541 | static ssize_t threaded_show(struct device *dev, |
542 | struct device_attribute *attr, char *buf) | |
543 | { | |
544 | struct net_device *netdev = to_net_dev(dev); | |
545 | ssize_t ret = -EINVAL; | |
546 | ||
547 | if (!rtnl_trylock()) | |
548 | return restart_syscall(); | |
549 | ||
550 | if (dev_isalive(netdev)) | |
551 | ret = sprintf(buf, fmt_dec, netdev->threaded); | |
552 | ||
553 | rtnl_unlock(); | |
554 | return ret; | |
555 | } | |
556 | ||
557 | static int modify_napi_threaded(struct net_device *dev, unsigned long val) | |
558 | { | |
559 | int ret; | |
560 | ||
561 | if (list_empty(&dev->napi_list)) | |
562 | return -EOPNOTSUPP; | |
563 | ||
564 | if (val != 0 && val != 1) | |
565 | return -EOPNOTSUPP; | |
566 | ||
567 | ret = dev_set_threaded(dev, val); | |
568 | ||
569 | return ret; | |
570 | } | |
571 | ||
572 | static ssize_t threaded_store(struct device *dev, | |
573 | struct device_attribute *attr, | |
574 | const char *buf, size_t len) | |
575 | { | |
576 | return netdev_store(dev, attr, buf, len, modify_napi_threaded); | |
577 | } | |
578 | static DEVICE_ATTR_RW(threaded); | |
579 | ||
ec6cc599 | 580 | static struct attribute *net_class_attrs[] __ro_after_init = { |
6be8aeef GKH |
581 | &dev_attr_netdev_group.attr, |
582 | &dev_attr_type.attr, | |
583 | &dev_attr_dev_id.attr, | |
3f85944f | 584 | &dev_attr_dev_port.attr, |
6be8aeef GKH |
585 | &dev_attr_iflink.attr, |
586 | &dev_attr_ifindex.attr, | |
685343fc | 587 | &dev_attr_name_assign_type.attr, |
6be8aeef GKH |
588 | &dev_attr_addr_assign_type.attr, |
589 | &dev_attr_addr_len.attr, | |
590 | &dev_attr_link_mode.attr, | |
591 | &dev_attr_address.attr, | |
592 | &dev_attr_broadcast.attr, | |
593 | &dev_attr_speed.attr, | |
594 | &dev_attr_duplex.attr, | |
595 | &dev_attr_dormant.attr, | |
db30a577 | 596 | &dev_attr_testing.attr, |
6be8aeef | 597 | &dev_attr_operstate.attr, |
2d3b479d | 598 | &dev_attr_carrier_changes.attr, |
6be8aeef GKH |
599 | &dev_attr_ifalias.attr, |
600 | &dev_attr_carrier.attr, | |
601 | &dev_attr_mtu.attr, | |
602 | &dev_attr_flags.attr, | |
603 | &dev_attr_tx_queue_len.attr, | |
3b47d303 | 604 | &dev_attr_gro_flush_timeout.attr, |
6f8b12d6 | 605 | &dev_attr_napi_defer_hard_irqs.attr, |
cc998ff8 | 606 | &dev_attr_phys_port_id.attr, |
db24a904 | 607 | &dev_attr_phys_port_name.attr, |
aecbe01e | 608 | &dev_attr_phys_switch_id.attr, |
d746d707 | 609 | &dev_attr_proto_down.attr, |
b2d3bcfa DD |
610 | &dev_attr_carrier_up_count.attr, |
611 | &dev_attr_carrier_down_count.attr, | |
5fdd2f0e | 612 | &dev_attr_threaded.attr, |
6be8aeef | 613 | NULL, |
1da177e4 | 614 | }; |
6be8aeef | 615 | ATTRIBUTE_GROUPS(net_class); |
1da177e4 LT |
616 | |
617 | /* Show a given an attribute in the statistics group */ | |
43cb76d9 GKH |
618 | static ssize_t netstat_show(const struct device *d, |
619 | struct device_attribute *attr, char *buf, | |
1da177e4 LT |
620 | unsigned long offset) |
621 | { | |
43cb76d9 | 622 | struct net_device *dev = to_net_dev(d); |
1da177e4 LT |
623 | ssize_t ret = -EINVAL; |
624 | ||
be1f3c2c | 625 | WARN_ON(offset > sizeof(struct rtnl_link_stats64) || |
6648c65e | 626 | offset % sizeof(u64) != 0); |
1da177e4 LT |
627 | |
628 | read_lock(&dev_base_lock); | |
96e74088 | 629 | if (dev_isalive(dev)) { |
28172739 ED |
630 | struct rtnl_link_stats64 temp; |
631 | const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); | |
632 | ||
6648c65e | 633 | ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset)); |
96e74088 | 634 | } |
1da177e4 LT |
635 | read_unlock(&dev_base_lock); |
636 | return ret; | |
637 | } | |
638 | ||
639 | /* generate a read-only statistics attribute */ | |
640 | #define NETSTAT_ENTRY(name) \ | |
6be8aeef | 641 | static ssize_t name##_show(struct device *d, \ |
6648c65e | 642 | struct device_attribute *attr, char *buf) \ |
1da177e4 | 643 | { \ |
43cb76d9 | 644 | return netstat_show(d, attr, buf, \ |
be1f3c2c | 645 | offsetof(struct rtnl_link_stats64, name)); \ |
1da177e4 | 646 | } \ |
6be8aeef | 647 | static DEVICE_ATTR_RO(name) |
1da177e4 LT |
648 | |
649 | NETSTAT_ENTRY(rx_packets); | |
650 | NETSTAT_ENTRY(tx_packets); | |
651 | NETSTAT_ENTRY(rx_bytes); | |
652 | NETSTAT_ENTRY(tx_bytes); | |
653 | NETSTAT_ENTRY(rx_errors); | |
654 | NETSTAT_ENTRY(tx_errors); | |
655 | NETSTAT_ENTRY(rx_dropped); | |
656 | NETSTAT_ENTRY(tx_dropped); | |
657 | NETSTAT_ENTRY(multicast); | |
658 | NETSTAT_ENTRY(collisions); | |
659 | NETSTAT_ENTRY(rx_length_errors); | |
660 | NETSTAT_ENTRY(rx_over_errors); | |
661 | NETSTAT_ENTRY(rx_crc_errors); | |
662 | NETSTAT_ENTRY(rx_frame_errors); | |
663 | NETSTAT_ENTRY(rx_fifo_errors); | |
664 | NETSTAT_ENTRY(rx_missed_errors); | |
665 | NETSTAT_ENTRY(tx_aborted_errors); | |
666 | NETSTAT_ENTRY(tx_carrier_errors); | |
667 | NETSTAT_ENTRY(tx_fifo_errors); | |
668 | NETSTAT_ENTRY(tx_heartbeat_errors); | |
669 | NETSTAT_ENTRY(tx_window_errors); | |
670 | NETSTAT_ENTRY(rx_compressed); | |
671 | NETSTAT_ENTRY(tx_compressed); | |
6e7333d3 | 672 | NETSTAT_ENTRY(rx_nohandler); |
1da177e4 | 673 | |
ec6cc599 | 674 | static struct attribute *netstat_attrs[] __ro_after_init = { |
43cb76d9 GKH |
675 | &dev_attr_rx_packets.attr, |
676 | &dev_attr_tx_packets.attr, | |
677 | &dev_attr_rx_bytes.attr, | |
678 | &dev_attr_tx_bytes.attr, | |
679 | &dev_attr_rx_errors.attr, | |
680 | &dev_attr_tx_errors.attr, | |
681 | &dev_attr_rx_dropped.attr, | |
682 | &dev_attr_tx_dropped.attr, | |
683 | &dev_attr_multicast.attr, | |
684 | &dev_attr_collisions.attr, | |
685 | &dev_attr_rx_length_errors.attr, | |
686 | &dev_attr_rx_over_errors.attr, | |
687 | &dev_attr_rx_crc_errors.attr, | |
688 | &dev_attr_rx_frame_errors.attr, | |
689 | &dev_attr_rx_fifo_errors.attr, | |
690 | &dev_attr_rx_missed_errors.attr, | |
691 | &dev_attr_tx_aborted_errors.attr, | |
692 | &dev_attr_tx_carrier_errors.attr, | |
693 | &dev_attr_tx_fifo_errors.attr, | |
694 | &dev_attr_tx_heartbeat_errors.attr, | |
695 | &dev_attr_tx_window_errors.attr, | |
696 | &dev_attr_rx_compressed.attr, | |
697 | &dev_attr_tx_compressed.attr, | |
6e7333d3 | 698 | &dev_attr_rx_nohandler.attr, |
1da177e4 LT |
699 | NULL |
700 | }; | |
701 | ||
38ef00cc | 702 | static const struct attribute_group netstat_group = { |
1da177e4 LT |
703 | .name = "statistics", |
704 | .attrs = netstat_attrs, | |
705 | }; | |
38c1a01c JB |
706 | |
707 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) | |
708 | static struct attribute *wireless_attrs[] = { | |
709 | NULL | |
710 | }; | |
711 | ||
38ef00cc | 712 | static const struct attribute_group wireless_group = { |
38c1a01c JB |
713 | .name = "wireless", |
714 | .attrs = wireless_attrs, | |
715 | }; | |
716 | #endif | |
6be8aeef GKH |
717 | |
718 | #else /* CONFIG_SYSFS */ | |
719 | #define net_class_groups NULL | |
d6523ddf | 720 | #endif /* CONFIG_SYSFS */ |
1da177e4 | 721 | |
a953be53 | 722 | #ifdef CONFIG_SYSFS |
6648c65e | 723 | #define to_rx_queue_attr(_attr) \ |
724 | container_of(_attr, struct rx_queue_attribute, attr) | |
0a9627f2 TH |
725 | |
726 | #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) | |
727 | ||
728 | static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, | |
729 | char *buf) | |
730 | { | |
667e427b | 731 | const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
0a9627f2 TH |
732 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
733 | ||
734 | if (!attribute->show) | |
735 | return -EIO; | |
736 | ||
718ad681 | 737 | return attribute->show(queue, buf); |
0a9627f2 TH |
738 | } |
739 | ||
740 | static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, | |
741 | const char *buf, size_t count) | |
742 | { | |
667e427b | 743 | const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
0a9627f2 TH |
744 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
745 | ||
746 | if (!attribute->store) | |
747 | return -EIO; | |
748 | ||
718ad681 | 749 | return attribute->store(queue, buf, count); |
0a9627f2 TH |
750 | } |
751 | ||
fa50d645 | 752 | static const struct sysfs_ops rx_queue_sysfs_ops = { |
0a9627f2 TH |
753 | .show = rx_queue_attr_show, |
754 | .store = rx_queue_attr_store, | |
755 | }; | |
756 | ||
a953be53 | 757 | #ifdef CONFIG_RPS |
718ad681 | 758 | static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf) |
0a9627f2 TH |
759 | { |
760 | struct rps_map *map; | |
761 | cpumask_var_t mask; | |
f0906827 | 762 | int i, len; |
0a9627f2 TH |
763 | |
764 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) | |
765 | return -ENOMEM; | |
766 | ||
767 | rcu_read_lock(); | |
768 | map = rcu_dereference(queue->rps_map); | |
769 | if (map) | |
770 | for (i = 0; i < map->len; i++) | |
771 | cpumask_set_cpu(map->cpus[i], mask); | |
772 | ||
f0906827 | 773 | len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); |
0a9627f2 | 774 | rcu_read_unlock(); |
0a9627f2 | 775 | free_cpumask_var(mask); |
f0906827 TH |
776 | |
777 | return len < PAGE_SIZE ? len : -EINVAL; | |
0a9627f2 TH |
778 | } |
779 | ||
f5acb907 | 780 | static ssize_t store_rps_map(struct netdev_rx_queue *queue, |
718ad681 | 781 | const char *buf, size_t len) |
0a9627f2 TH |
782 | { |
783 | struct rps_map *old_map, *map; | |
784 | cpumask_var_t mask; | |
07bbecb3 | 785 | int err, cpu, i, hk_flags; |
da65ad1f | 786 | static DEFINE_MUTEX(rps_map_mutex); |
0a9627f2 TH |
787 | |
788 | if (!capable(CAP_NET_ADMIN)) | |
789 | return -EPERM; | |
790 | ||
791 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
792 | return -ENOMEM; | |
793 | ||
794 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); | |
795 | if (err) { | |
796 | free_cpumask_var(mask); | |
797 | return err; | |
798 | } | |
799 | ||
2e0d8fef ED |
800 | if (!cpumask_empty(mask)) { |
801 | hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ; | |
802 | cpumask_and(mask, mask, housekeeping_cpumask(hk_flags)); | |
803 | if (cpumask_empty(mask)) { | |
804 | free_cpumask_var(mask); | |
805 | return -EINVAL; | |
806 | } | |
07bbecb3 AB |
807 | } |
808 | ||
95c96174 | 809 | map = kzalloc(max_t(unsigned int, |
6648c65e | 810 | RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), |
811 | GFP_KERNEL); | |
0a9627f2 TH |
812 | if (!map) { |
813 | free_cpumask_var(mask); | |
814 | return -ENOMEM; | |
815 | } | |
816 | ||
817 | i = 0; | |
818 | for_each_cpu_and(cpu, mask, cpu_online_mask) | |
819 | map->cpus[i++] = cpu; | |
820 | ||
6648c65e | 821 | if (i) { |
0a9627f2 | 822 | map->len = i; |
6648c65e | 823 | } else { |
0a9627f2 TH |
824 | kfree(map); |
825 | map = NULL; | |
826 | } | |
827 | ||
da65ad1f | 828 | mutex_lock(&rps_map_mutex); |
6e3f7faf | 829 | old_map = rcu_dereference_protected(queue->rps_map, |
da65ad1f | 830 | mutex_is_locked(&rps_map_mutex)); |
0a9627f2 | 831 | rcu_assign_pointer(queue->rps_map, map); |
0a9627f2 | 832 | |
adc9300e | 833 | if (map) |
dc05360f | 834 | static_branch_inc(&rps_needed); |
10e4ea75 | 835 | if (old_map) |
dc05360f | 836 | static_branch_dec(&rps_needed); |
10e4ea75 | 837 | |
da65ad1f | 838 | mutex_unlock(&rps_map_mutex); |
10e4ea75 TH |
839 | |
840 | if (old_map) | |
841 | kfree_rcu(old_map, rcu); | |
842 | ||
0a9627f2 TH |
843 | free_cpumask_var(mask); |
844 | return len; | |
845 | } | |
846 | ||
fec5e652 | 847 | static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
fec5e652 TH |
848 | char *buf) |
849 | { | |
850 | struct rps_dev_flow_table *flow_table; | |
60b778ce | 851 | unsigned long val = 0; |
fec5e652 TH |
852 | |
853 | rcu_read_lock(); | |
854 | flow_table = rcu_dereference(queue->rps_flow_table); | |
855 | if (flow_table) | |
60b778ce | 856 | val = (unsigned long)flow_table->mask + 1; |
fec5e652 TH |
857 | rcu_read_unlock(); |
858 | ||
60b778ce | 859 | return sprintf(buf, "%lu\n", val); |
fec5e652 TH |
860 | } |
861 | ||
fec5e652 TH |
862 | static void rps_dev_flow_table_release(struct rcu_head *rcu) |
863 | { | |
864 | struct rps_dev_flow_table *table = container_of(rcu, | |
865 | struct rps_dev_flow_table, rcu); | |
243198d0 | 866 | vfree(table); |
fec5e652 TH |
867 | } |
868 | ||
f5acb907 | 869 | static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
718ad681 | 870 | const char *buf, size_t len) |
fec5e652 | 871 | { |
60b778ce | 872 | unsigned long mask, count; |
fec5e652 TH |
873 | struct rps_dev_flow_table *table, *old_table; |
874 | static DEFINE_SPINLOCK(rps_dev_flow_lock); | |
60b778ce | 875 | int rc; |
fec5e652 TH |
876 | |
877 | if (!capable(CAP_NET_ADMIN)) | |
878 | return -EPERM; | |
879 | ||
60b778ce ED |
880 | rc = kstrtoul(buf, 0, &count); |
881 | if (rc < 0) | |
882 | return rc; | |
fec5e652 TH |
883 | |
884 | if (count) { | |
60b778ce ED |
885 | mask = count - 1; |
886 | /* mask = roundup_pow_of_two(count) - 1; | |
887 | * without overflows... | |
888 | */ | |
889 | while ((mask | (mask >> 1)) != mask) | |
890 | mask |= (mask >> 1); | |
891 | /* On 64 bit arches, must check mask fits in table->mask (u32), | |
8e3bff96 | 892 | * and on 32bit arches, must check |
893 | * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow. | |
60b778ce ED |
894 | */ |
895 | #if BITS_PER_LONG > 32 | |
896 | if (mask > (unsigned long)(u32)mask) | |
a0a129f8 | 897 | return -EINVAL; |
60b778ce ED |
898 | #else |
899 | if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) | |
a0a129f8 | 900 | / sizeof(struct rps_dev_flow)) { |
fec5e652 TH |
901 | /* Enforce a limit to prevent overflow */ |
902 | return -EINVAL; | |
903 | } | |
60b778ce ED |
904 | #endif |
905 | table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); | |
fec5e652 TH |
906 | if (!table) |
907 | return -ENOMEM; | |
908 | ||
60b778ce ED |
909 | table->mask = mask; |
910 | for (count = 0; count <= mask; count++) | |
911 | table->flows[count].cpu = RPS_NO_CPU; | |
6648c65e | 912 | } else { |
fec5e652 | 913 | table = NULL; |
6648c65e | 914 | } |
fec5e652 TH |
915 | |
916 | spin_lock(&rps_dev_flow_lock); | |
6e3f7faf ED |
917 | old_table = rcu_dereference_protected(queue->rps_flow_table, |
918 | lockdep_is_held(&rps_dev_flow_lock)); | |
fec5e652 TH |
919 | rcu_assign_pointer(queue->rps_flow_table, table); |
920 | spin_unlock(&rps_dev_flow_lock); | |
921 | ||
922 | if (old_table) | |
923 | call_rcu(&old_table->rcu, rps_dev_flow_table_release); | |
924 | ||
925 | return len; | |
926 | } | |
927 | ||
667e427b | 928 | static struct rx_queue_attribute rps_cpus_attribute __ro_after_init |
d6444062 | 929 | = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map); |
0a9627f2 | 930 | |
667e427b | 931 | static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init |
d6444062 | 932 | = __ATTR(rps_flow_cnt, 0644, |
667e427b | 933 | show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); |
a953be53 | 934 | #endif /* CONFIG_RPS */ |
fec5e652 | 935 | |
667e427b | 936 | static struct attribute *rx_queue_default_attrs[] __ro_after_init = { |
a953be53 | 937 | #ifdef CONFIG_RPS |
0a9627f2 | 938 | &rps_cpus_attribute.attr, |
fec5e652 | 939 | &rps_dev_flow_table_cnt_attribute.attr, |
a953be53 | 940 | #endif |
0a9627f2 TH |
941 | NULL |
942 | }; | |
be0d6926 | 943 | ATTRIBUTE_GROUPS(rx_queue_default); |
0a9627f2 TH |
944 | |
945 | static void rx_queue_release(struct kobject *kobj) | |
946 | { | |
947 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
a953be53 | 948 | #ifdef CONFIG_RPS |
6e3f7faf ED |
949 | struct rps_map *map; |
950 | struct rps_dev_flow_table *flow_table; | |
0a9627f2 | 951 | |
33d480ce | 952 | map = rcu_dereference_protected(queue->rps_map, 1); |
9ea19481 JF |
953 | if (map) { |
954 | RCU_INIT_POINTER(queue->rps_map, NULL); | |
f6f80238 | 955 | kfree_rcu(map, rcu); |
9ea19481 | 956 | } |
6e3f7faf | 957 | |
33d480ce | 958 | flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); |
9ea19481 JF |
959 | if (flow_table) { |
960 | RCU_INIT_POINTER(queue->rps_flow_table, NULL); | |
6e3f7faf | 961 | call_rcu(&flow_table->rcu, rps_dev_flow_table_release); |
9ea19481 | 962 | } |
a953be53 | 963 | #endif |
0a9627f2 | 964 | |
9ea19481 | 965 | memset(kobj, 0, sizeof(*kobj)); |
fe822240 | 966 | dev_put(queue->dev); |
0a9627f2 TH |
967 | } |
968 | ||
82ef3d5d WC |
969 | static const void *rx_queue_namespace(struct kobject *kobj) |
970 | { | |
971 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
972 | struct device *dev = &queue->dev->dev; | |
973 | const void *ns = NULL; | |
974 | ||
975 | if (dev->class && dev->class->ns_type) | |
976 | ns = dev->class->namespace(dev); | |
977 | ||
978 | return ns; | |
979 | } | |
980 | ||
b0e37c0d DT |
981 | static void rx_queue_get_ownership(struct kobject *kobj, |
982 | kuid_t *uid, kgid_t *gid) | |
983 | { | |
984 | const struct net *net = rx_queue_namespace(kobj); | |
985 | ||
986 | net_ns_get_ownership(net, uid, gid); | |
987 | } | |
988 | ||
667e427b | 989 | static struct kobj_type rx_queue_ktype __ro_after_init = { |
0a9627f2 TH |
990 | .sysfs_ops = &rx_queue_sysfs_ops, |
991 | .release = rx_queue_release, | |
be0d6926 | 992 | .default_groups = rx_queue_default_groups, |
b0e37c0d DT |
993 | .namespace = rx_queue_namespace, |
994 | .get_ownership = rx_queue_get_ownership, | |
0a9627f2 TH |
995 | }; |
996 | ||
6b53dafe | 997 | static int rx_queue_add_kobject(struct net_device *dev, int index) |
0a9627f2 | 998 | { |
6b53dafe | 999 | struct netdev_rx_queue *queue = dev->_rx + index; |
0a9627f2 TH |
1000 | struct kobject *kobj = &queue->kobj; |
1001 | int error = 0; | |
1002 | ||
ddd9b5e3 JH |
1003 | /* Kobject_put later will trigger rx_queue_release call which |
1004 | * decreases dev refcount: Take that reference here | |
1005 | */ | |
1006 | dev_hold(queue->dev); | |
1007 | ||
6b53dafe | 1008 | kobj->kset = dev->queues_kset; |
0a9627f2 | 1009 | error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, |
6648c65e | 1010 | "rx-%u", index); |
a953be53 | 1011 | if (error) |
b8eb7183 | 1012 | goto err; |
a953be53 | 1013 | |
6b53dafe WC |
1014 | if (dev->sysfs_rx_queue_group) { |
1015 | error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group); | |
b8eb7183 JH |
1016 | if (error) |
1017 | goto err; | |
0a9627f2 TH |
1018 | } |
1019 | ||
1020 | kobject_uevent(kobj, KOBJ_ADD); | |
1021 | ||
1022 | return error; | |
b8eb7183 JH |
1023 | |
1024 | err: | |
1025 | kobject_put(kobj); | |
1026 | return error; | |
0a9627f2 | 1027 | } |
d755407d CB |
1028 | |
1029 | static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid, | |
1030 | kgid_t kgid) | |
1031 | { | |
1032 | struct netdev_rx_queue *queue = dev->_rx + index; | |
1033 | struct kobject *kobj = &queue->kobj; | |
1034 | int error; | |
1035 | ||
1036 | error = sysfs_change_owner(kobj, kuid, kgid); | |
1037 | if (error) | |
1038 | return error; | |
1039 | ||
1040 | if (dev->sysfs_rx_queue_group) | |
1041 | error = sysfs_group_change_owner( | |
1042 | kobj, dev->sysfs_rx_queue_group, kuid, kgid); | |
1043 | ||
1044 | return error; | |
1045 | } | |
80dd6eac | 1046 | #endif /* CONFIG_SYSFS */ |
0a9627f2 | 1047 | |
62fe0b40 | 1048 | int |
6b53dafe | 1049 | net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) |
0a9627f2 | 1050 | { |
a953be53 | 1051 | #ifdef CONFIG_SYSFS |
0a9627f2 TH |
1052 | int i; |
1053 | int error = 0; | |
1054 | ||
a953be53 | 1055 | #ifndef CONFIG_RPS |
6b53dafe | 1056 | if (!dev->sysfs_rx_queue_group) |
a953be53 MD |
1057 | return 0; |
1058 | #endif | |
62fe0b40 | 1059 | for (i = old_num; i < new_num; i++) { |
6b53dafe | 1060 | error = rx_queue_add_kobject(dev, i); |
62fe0b40 BH |
1061 | if (error) { |
1062 | new_num = old_num; | |
0a9627f2 | 1063 | break; |
62fe0b40 | 1064 | } |
0a9627f2 TH |
1065 | } |
1066 | ||
a953be53 | 1067 | while (--i >= new_num) { |
002d8a1a AV |
1068 | struct kobject *kobj = &dev->_rx[i].kobj; |
1069 | ||
8b8f3e66 | 1070 | if (!refcount_read(&dev_net(dev)->ns.count)) |
002d8a1a | 1071 | kobj->uevent_suppress = 1; |
6b53dafe | 1072 | if (dev->sysfs_rx_queue_group) |
002d8a1a AV |
1073 | sysfs_remove_group(kobj, dev->sysfs_rx_queue_group); |
1074 | kobject_put(kobj); | |
a953be53 | 1075 | } |
0a9627f2 TH |
1076 | |
1077 | return error; | |
bf264145 TH |
1078 | #else |
1079 | return 0; | |
1080 | #endif | |
0a9627f2 TH |
1081 | } |
1082 | ||
d755407d CB |
1083 | static int net_rx_queue_change_owner(struct net_device *dev, int num, |
1084 | kuid_t kuid, kgid_t kgid) | |
1085 | { | |
1086 | #ifdef CONFIG_SYSFS | |
1087 | int error = 0; | |
1088 | int i; | |
1089 | ||
1090 | #ifndef CONFIG_RPS | |
1091 | if (!dev->sysfs_rx_queue_group) | |
1092 | return 0; | |
1093 | #endif | |
1094 | for (i = 0; i < num; i++) { | |
1095 | error = rx_queue_change_owner(dev, i, kuid, kgid); | |
1096 | if (error) | |
1097 | break; | |
1098 | } | |
1099 | ||
1100 | return error; | |
1101 | #else | |
1102 | return 0; | |
1103 | #endif | |
1104 | } | |
1105 | ||
ccf5ff69 | 1106 | #ifdef CONFIG_SYSFS |
1d24eb48 TH |
1107 | /* |
1108 | * netdev_queue sysfs structures and functions. | |
1109 | */ | |
1110 | struct netdev_queue_attribute { | |
1111 | struct attribute attr; | |
718ad681 | 1112 | ssize_t (*show)(struct netdev_queue *queue, char *buf); |
1d24eb48 | 1113 | ssize_t (*store)(struct netdev_queue *queue, |
718ad681 | 1114 | const char *buf, size_t len); |
1d24eb48 | 1115 | }; |
6648c65e | 1116 | #define to_netdev_queue_attr(_attr) \ |
1117 | container_of(_attr, struct netdev_queue_attribute, attr) | |
1d24eb48 TH |
1118 | |
1119 | #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) | |
1120 | ||
1121 | static ssize_t netdev_queue_attr_show(struct kobject *kobj, | |
1122 | struct attribute *attr, char *buf) | |
1123 | { | |
667e427b | 1124 | const struct netdev_queue_attribute *attribute |
1125 | = to_netdev_queue_attr(attr); | |
1d24eb48 TH |
1126 | struct netdev_queue *queue = to_netdev_queue(kobj); |
1127 | ||
1128 | if (!attribute->show) | |
1129 | return -EIO; | |
1130 | ||
718ad681 | 1131 | return attribute->show(queue, buf); |
1d24eb48 TH |
1132 | } |
1133 | ||
1134 | static ssize_t netdev_queue_attr_store(struct kobject *kobj, | |
1135 | struct attribute *attr, | |
1136 | const char *buf, size_t count) | |
1137 | { | |
667e427b | 1138 | const struct netdev_queue_attribute *attribute |
1139 | = to_netdev_queue_attr(attr); | |
1d24eb48 TH |
1140 | struct netdev_queue *queue = to_netdev_queue(kobj); |
1141 | ||
1142 | if (!attribute->store) | |
1143 | return -EIO; | |
1144 | ||
718ad681 | 1145 | return attribute->store(queue, buf, count); |
1d24eb48 TH |
1146 | } |
1147 | ||
1148 | static const struct sysfs_ops netdev_queue_sysfs_ops = { | |
1149 | .show = netdev_queue_attr_show, | |
1150 | .store = netdev_queue_attr_store, | |
1151 | }; | |
1152 | ||
2b9c7581 | 1153 | static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf) |
ccf5ff69 | 1154 | { |
1155 | unsigned long trans_timeout; | |
1156 | ||
1157 | spin_lock_irq(&queue->_xmit_lock); | |
1158 | trans_timeout = queue->trans_timeout; | |
1159 | spin_unlock_irq(&queue->_xmit_lock); | |
1160 | ||
9bb5fbea | 1161 | return sprintf(buf, fmt_ulong, trans_timeout); |
ccf5ff69 | 1162 | } |
1163 | ||
c4047f53 | 1164 | static unsigned int get_netdev_queue_index(struct netdev_queue *queue) |
822b3b2e JF |
1165 | { |
1166 | struct net_device *dev = queue->dev; | |
c4047f53 | 1167 | unsigned int i; |
822b3b2e | 1168 | |
c4047f53 | 1169 | i = queue - dev->_tx; |
822b3b2e JF |
1170 | BUG_ON(i >= dev->num_tx_queues); |
1171 | ||
1172 | return i; | |
1173 | } | |
1174 | ||
2b9c7581 | 1175 | static ssize_t traffic_class_show(struct netdev_queue *queue, |
8d059b0f AD |
1176 | char *buf) |
1177 | { | |
1178 | struct net_device *dev = queue->dev; | |
b2f17564 | 1179 | int num_tc, tc; |
d7be9775 | 1180 | int index; |
8d059b0f | 1181 | |
d7be9775 AD |
1182 | if (!netif_is_multiqueue(dev)) |
1183 | return -ENOENT; | |
1184 | ||
b2f17564 AD |
1185 | if (!rtnl_trylock()) |
1186 | return restart_syscall(); | |
1187 | ||
d7be9775 | 1188 | index = get_netdev_queue_index(queue); |
ffcfe25b AD |
1189 | |
1190 | /* If queue belongs to subordinate dev use its TC mapping */ | |
1191 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
1192 | ||
b2f17564 | 1193 | num_tc = dev->num_tc; |
d7be9775 | 1194 | tc = netdev_txq_to_tc(dev, index); |
b2f17564 AD |
1195 | |
1196 | rtnl_unlock(); | |
1197 | ||
8d059b0f AD |
1198 | if (tc < 0) |
1199 | return -EINVAL; | |
1200 | ||
ffcfe25b AD |
1201 | /* We can report the traffic class one of two ways: |
1202 | * Subordinate device traffic classes are reported with the traffic | |
1203 | * class first, and then the subordinate class so for example TC0 on | |
1204 | * subordinate device 2 will be reported as "0-2". If the queue | |
1205 | * belongs to the root device it will be reported with just the | |
1206 | * traffic class, so just "0" for TC 0 for example. | |
1207 | */ | |
b2f17564 AD |
1208 | return num_tc < 0 ? sprintf(buf, "%d%d\n", tc, num_tc) : |
1209 | sprintf(buf, "%d\n", tc); | |
8d059b0f AD |
1210 | } |
1211 | ||
1212 | #ifdef CONFIG_XPS | |
2b9c7581 | 1213 | static ssize_t tx_maxrate_show(struct netdev_queue *queue, |
822b3b2e JF |
1214 | char *buf) |
1215 | { | |
1216 | return sprintf(buf, "%lu\n", queue->tx_maxrate); | |
1217 | } | |
1218 | ||
2b9c7581 | 1219 | static ssize_t tx_maxrate_store(struct netdev_queue *queue, |
1220 | const char *buf, size_t len) | |
822b3b2e JF |
1221 | { |
1222 | struct net_device *dev = queue->dev; | |
1223 | int err, index = get_netdev_queue_index(queue); | |
1224 | u32 rate = 0; | |
1225 | ||
3033fced TH |
1226 | if (!capable(CAP_NET_ADMIN)) |
1227 | return -EPERM; | |
1228 | ||
822b3b2e JF |
1229 | err = kstrtou32(buf, 10, &rate); |
1230 | if (err < 0) | |
1231 | return err; | |
1232 | ||
1233 | if (!rtnl_trylock()) | |
1234 | return restart_syscall(); | |
1235 | ||
1236 | err = -EOPNOTSUPP; | |
1237 | if (dev->netdev_ops->ndo_set_tx_maxrate) | |
1238 | err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate); | |
1239 | ||
1240 | rtnl_unlock(); | |
1241 | if (!err) { | |
1242 | queue->tx_maxrate = rate; | |
1243 | return len; | |
1244 | } | |
1245 | return err; | |
1246 | } | |
1247 | ||
2b9c7581 | 1248 | static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init |
1249 | = __ATTR_RW(tx_maxrate); | |
822b3b2e JF |
1250 | #endif |
1251 | ||
2b9c7581 | 1252 | static struct netdev_queue_attribute queue_trans_timeout __ro_after_init |
1253 | = __ATTR_RO(tx_timeout); | |
ccf5ff69 | 1254 | |
2b9c7581 | 1255 | static struct netdev_queue_attribute queue_traffic_class __ro_after_init |
1256 | = __ATTR_RO(traffic_class); | |
8d059b0f | 1257 | |
114cf580 TH |
1258 | #ifdef CONFIG_BQL |
1259 | /* | |
1260 | * Byte queue limits sysfs structures and functions. | |
1261 | */ | |
1262 | static ssize_t bql_show(char *buf, unsigned int value) | |
1263 | { | |
1264 | return sprintf(buf, "%u\n", value); | |
1265 | } | |
1266 | ||
1267 | static ssize_t bql_set(const char *buf, const size_t count, | |
1268 | unsigned int *pvalue) | |
1269 | { | |
1270 | unsigned int value; | |
1271 | int err; | |
1272 | ||
6648c65e | 1273 | if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) { |
114cf580 | 1274 | value = DQL_MAX_LIMIT; |
6648c65e | 1275 | } else { |
114cf580 TH |
1276 | err = kstrtouint(buf, 10, &value); |
1277 | if (err < 0) | |
1278 | return err; | |
1279 | if (value > DQL_MAX_LIMIT) | |
1280 | return -EINVAL; | |
1281 | } | |
1282 | ||
1283 | *pvalue = value; | |
1284 | ||
1285 | return count; | |
1286 | } | |
1287 | ||
1288 | static ssize_t bql_show_hold_time(struct netdev_queue *queue, | |
114cf580 TH |
1289 | char *buf) |
1290 | { | |
1291 | struct dql *dql = &queue->dql; | |
1292 | ||
1293 | return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); | |
1294 | } | |
1295 | ||
1296 | static ssize_t bql_set_hold_time(struct netdev_queue *queue, | |
114cf580 TH |
1297 | const char *buf, size_t len) |
1298 | { | |
1299 | struct dql *dql = &queue->dql; | |
95c96174 | 1300 | unsigned int value; |
114cf580 TH |
1301 | int err; |
1302 | ||
1303 | err = kstrtouint(buf, 10, &value); | |
1304 | if (err < 0) | |
1305 | return err; | |
1306 | ||
1307 | dql->slack_hold_time = msecs_to_jiffies(value); | |
1308 | ||
1309 | return len; | |
1310 | } | |
1311 | ||
170c658a | 1312 | static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init |
d6444062 | 1313 | = __ATTR(hold_time, 0644, |
170c658a | 1314 | bql_show_hold_time, bql_set_hold_time); |
114cf580 TH |
1315 | |
1316 | static ssize_t bql_show_inflight(struct netdev_queue *queue, | |
114cf580 TH |
1317 | char *buf) |
1318 | { | |
1319 | struct dql *dql = &queue->dql; | |
1320 | ||
1321 | return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); | |
1322 | } | |
1323 | ||
170c658a | 1324 | static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init = |
d6444062 | 1325 | __ATTR(inflight, 0444, bql_show_inflight, NULL); |
114cf580 TH |
1326 | |
1327 | #define BQL_ATTR(NAME, FIELD) \ | |
1328 | static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ | |
114cf580 TH |
1329 | char *buf) \ |
1330 | { \ | |
1331 | return bql_show(buf, queue->dql.FIELD); \ | |
1332 | } \ | |
1333 | \ | |
1334 | static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ | |
114cf580 TH |
1335 | const char *buf, size_t len) \ |
1336 | { \ | |
1337 | return bql_set(buf, len, &queue->dql.FIELD); \ | |
1338 | } \ | |
1339 | \ | |
170c658a | 1340 | static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \ |
d6444062 | 1341 | = __ATTR(NAME, 0644, \ |
170c658a | 1342 | bql_show_ ## NAME, bql_set_ ## NAME) |
114cf580 | 1343 | |
170c658a | 1344 | BQL_ATTR(limit, limit); |
1345 | BQL_ATTR(limit_max, max_limit); | |
1346 | BQL_ATTR(limit_min, min_limit); | |
114cf580 | 1347 | |
170c658a | 1348 | static struct attribute *dql_attrs[] __ro_after_init = { |
114cf580 TH |
1349 | &bql_limit_attribute.attr, |
1350 | &bql_limit_max_attribute.attr, | |
1351 | &bql_limit_min_attribute.attr, | |
1352 | &bql_hold_time_attribute.attr, | |
1353 | &bql_inflight_attribute.attr, | |
1354 | NULL | |
1355 | }; | |
1356 | ||
38ef00cc | 1357 | static const struct attribute_group dql_group = { |
114cf580 TH |
1358 | .name = "byte_queue_limits", |
1359 | .attrs = dql_attrs, | |
1360 | }; | |
1361 | #endif /* CONFIG_BQL */ | |
1362 | ||
ccf5ff69 | 1363 | #ifdef CONFIG_XPS |
2b9c7581 | 1364 | static ssize_t xps_cpus_show(struct netdev_queue *queue, |
1365 | char *buf) | |
1d24eb48 TH |
1366 | { |
1367 | struct net_device *dev = queue->dev; | |
1368 | struct xps_dev_maps *dev_maps; | |
255c04a8 | 1369 | int j, len, ret, tc = 0; |
d9a063d2 AT |
1370 | unsigned long *mask; |
1371 | unsigned int index; | |
1d24eb48 | 1372 | |
d7be9775 AD |
1373 | if (!netif_is_multiqueue(dev)) |
1374 | return -ENOENT; | |
1375 | ||
1d24eb48 TH |
1376 | index = get_netdev_queue_index(queue); |
1377 | ||
fb250385 AT |
1378 | if (!rtnl_trylock()) |
1379 | return restart_syscall(); | |
1380 | ||
255c04a8 AT |
1381 | /* If queue belongs to subordinate dev use its map */ |
1382 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
ffcfe25b | 1383 | |
255c04a8 AT |
1384 | tc = netdev_txq_to_tc(dev, index); |
1385 | if (tc < 0) { | |
1386 | ret = -EINVAL; | |
1387 | goto err_rtnl_unlock; | |
184c449f AD |
1388 | } |
1389 | ||
ea4fe7e8 AT |
1390 | mask = bitmap_zalloc(nr_cpu_ids, GFP_KERNEL); |
1391 | if (!mask) { | |
fb250385 AT |
1392 | ret = -ENOMEM; |
1393 | goto err_rtnl_unlock; | |
1394 | } | |
664088f8 | 1395 | |
1d24eb48 | 1396 | rcu_read_lock(); |
80d19669 | 1397 | dev_maps = rcu_dereference(dev->xps_cpus_map); |
255c04a8 | 1398 | if (!dev_maps || tc >= dev_maps->num_tc) |
73f5e52b AT |
1399 | goto out_no_maps; |
1400 | ||
1401 | for (j = -1; j = netif_attrmask_next(j, NULL, nr_cpu_ids), | |
1402 | j < nr_cpu_ids;) { | |
255c04a8 | 1403 | int i, tci = j * dev_maps->num_tc + tc; |
73f5e52b AT |
1404 | struct xps_map *map; |
1405 | ||
1406 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
1407 | if (!map) | |
1408 | continue; | |
1409 | ||
1410 | for (i = map->len; i--;) { | |
1411 | if (map->queues[i] == index) { | |
1412 | set_bit(j, mask); | |
1413 | break; | |
1d24eb48 TH |
1414 | } |
1415 | } | |
1416 | } | |
73f5e52b | 1417 | out_no_maps: |
1d24eb48 TH |
1418 | rcu_read_unlock(); |
1419 | ||
fb250385 AT |
1420 | rtnl_unlock(); |
1421 | ||
ea4fe7e8 AT |
1422 | len = bitmap_print_to_pagebuf(false, buf, mask, nr_cpu_ids); |
1423 | bitmap_free(mask); | |
f0906827 | 1424 | return len < PAGE_SIZE ? len : -EINVAL; |
fb250385 AT |
1425 | |
1426 | err_rtnl_unlock: | |
1427 | rtnl_unlock(); | |
1428 | return ret; | |
1d24eb48 TH |
1429 | } |
1430 | ||
2b9c7581 | 1431 | static ssize_t xps_cpus_store(struct netdev_queue *queue, |
1432 | const char *buf, size_t len) | |
1d24eb48 TH |
1433 | { |
1434 | struct net_device *dev = queue->dev; | |
d9a063d2 | 1435 | unsigned int index; |
537c00de AD |
1436 | cpumask_var_t mask; |
1437 | int err; | |
1d24eb48 | 1438 | |
d7be9775 AD |
1439 | if (!netif_is_multiqueue(dev)) |
1440 | return -ENOENT; | |
1441 | ||
1d24eb48 TH |
1442 | if (!capable(CAP_NET_ADMIN)) |
1443 | return -EPERM; | |
1444 | ||
1445 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
1446 | return -ENOMEM; | |
1447 | ||
1448 | index = get_netdev_queue_index(queue); | |
1449 | ||
1450 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); | |
1451 | if (err) { | |
1452 | free_cpumask_var(mask); | |
1453 | return err; | |
1454 | } | |
1455 | ||
1ad58225 AT |
1456 | if (!rtnl_trylock()) { |
1457 | free_cpumask_var(mask); | |
1458 | return restart_syscall(); | |
1459 | } | |
1460 | ||
537c00de | 1461 | err = netif_set_xps_queue(dev, mask, index); |
1ad58225 | 1462 | rtnl_unlock(); |
1d24eb48 TH |
1463 | |
1464 | free_cpumask_var(mask); | |
1d24eb48 | 1465 | |
537c00de | 1466 | return err ? : len; |
1d24eb48 TH |
1467 | } |
1468 | ||
2b9c7581 | 1469 | static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init |
1470 | = __ATTR_RW(xps_cpus); | |
8af2c06f AN |
1471 | |
1472 | static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf) | |
1473 | { | |
1474 | struct net_device *dev = queue->dev; | |
1475 | struct xps_dev_maps *dev_maps; | |
255c04a8 | 1476 | int j, len, ret, tc = 0; |
d9a063d2 AT |
1477 | unsigned long *mask; |
1478 | unsigned int index; | |
8af2c06f AN |
1479 | |
1480 | index = get_netdev_queue_index(queue); | |
1481 | ||
4ae2bb81 AT |
1482 | if (!rtnl_trylock()) |
1483 | return restart_syscall(); | |
1484 | ||
255c04a8 AT |
1485 | tc = netdev_txq_to_tc(dev, index); |
1486 | if (tc < 0) { | |
1487 | ret = -EINVAL; | |
1488 | goto err_rtnl_unlock; | |
8af2c06f | 1489 | } |
255c04a8 | 1490 | |
29ca1c5a | 1491 | mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL); |
4ae2bb81 AT |
1492 | if (!mask) { |
1493 | ret = -ENOMEM; | |
1494 | goto err_rtnl_unlock; | |
1495 | } | |
8af2c06f AN |
1496 | |
1497 | rcu_read_lock(); | |
1498 | dev_maps = rcu_dereference(dev->xps_rxqs_map); | |
255c04a8 | 1499 | if (!dev_maps || tc >= dev_maps->num_tc) |
8af2c06f AN |
1500 | goto out_no_maps; |
1501 | ||
1502 | for (j = -1; j = netif_attrmask_next(j, NULL, dev->num_rx_queues), | |
1503 | j < dev->num_rx_queues;) { | |
255c04a8 | 1504 | int i, tci = j * dev_maps->num_tc + tc; |
8af2c06f AN |
1505 | struct xps_map *map; |
1506 | ||
1507 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
1508 | if (!map) | |
1509 | continue; | |
1510 | ||
1511 | for (i = map->len; i--;) { | |
1512 | if (map->queues[i] == index) { | |
1513 | set_bit(j, mask); | |
1514 | break; | |
1515 | } | |
1516 | } | |
1517 | } | |
1518 | out_no_maps: | |
1519 | rcu_read_unlock(); | |
1520 | ||
4ae2bb81 AT |
1521 | rtnl_unlock(); |
1522 | ||
8af2c06f | 1523 | len = bitmap_print_to_pagebuf(false, buf, mask, dev->num_rx_queues); |
29ca1c5a | 1524 | bitmap_free(mask); |
8af2c06f AN |
1525 | |
1526 | return len < PAGE_SIZE ? len : -EINVAL; | |
4ae2bb81 AT |
1527 | |
1528 | err_rtnl_unlock: | |
1529 | rtnl_unlock(); | |
1530 | return ret; | |
8af2c06f AN |
1531 | } |
1532 | ||
1533 | static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf, | |
1534 | size_t len) | |
1535 | { | |
1536 | struct net_device *dev = queue->dev; | |
1537 | struct net *net = dev_net(dev); | |
d9a063d2 AT |
1538 | unsigned long *mask; |
1539 | unsigned int index; | |
8af2c06f AN |
1540 | int err; |
1541 | ||
1542 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) | |
1543 | return -EPERM; | |
1544 | ||
29ca1c5a | 1545 | mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL); |
8af2c06f AN |
1546 | if (!mask) |
1547 | return -ENOMEM; | |
1548 | ||
1549 | index = get_netdev_queue_index(queue); | |
1550 | ||
1551 | err = bitmap_parse(buf, len, mask, dev->num_rx_queues); | |
1552 | if (err) { | |
29ca1c5a | 1553 | bitmap_free(mask); |
8af2c06f AN |
1554 | return err; |
1555 | } | |
1556 | ||
2d57b4f1 AT |
1557 | if (!rtnl_trylock()) { |
1558 | bitmap_free(mask); | |
1559 | return restart_syscall(); | |
1560 | } | |
1561 | ||
4d99f660 | 1562 | cpus_read_lock(); |
8af2c06f | 1563 | err = __netif_set_xps_queue(dev, mask, index, true); |
4d99f660 AV |
1564 | cpus_read_unlock(); |
1565 | ||
2d57b4f1 AT |
1566 | rtnl_unlock(); |
1567 | ||
29ca1c5a | 1568 | bitmap_free(mask); |
8af2c06f AN |
1569 | return err ? : len; |
1570 | } | |
1571 | ||
1572 | static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init | |
1573 | = __ATTR_RW(xps_rxqs); | |
ccf5ff69 | 1574 | #endif /* CONFIG_XPS */ |
1d24eb48 | 1575 | |
2b9c7581 | 1576 | static struct attribute *netdev_queue_default_attrs[] __ro_after_init = { |
ccf5ff69 | 1577 | &queue_trans_timeout.attr, |
8d059b0f | 1578 | &queue_traffic_class.attr, |
ccf5ff69 | 1579 | #ifdef CONFIG_XPS |
1d24eb48 | 1580 | &xps_cpus_attribute.attr, |
8af2c06f | 1581 | &xps_rxqs_attribute.attr, |
822b3b2e | 1582 | &queue_tx_maxrate.attr, |
ccf5ff69 | 1583 | #endif |
1d24eb48 TH |
1584 | NULL |
1585 | }; | |
be0d6926 | 1586 | ATTRIBUTE_GROUPS(netdev_queue_default); |
1d24eb48 TH |
1587 | |
1588 | static void netdev_queue_release(struct kobject *kobj) | |
1589 | { | |
1590 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
1d24eb48 | 1591 | |
1d24eb48 TH |
1592 | memset(kobj, 0, sizeof(*kobj)); |
1593 | dev_put(queue->dev); | |
1594 | } | |
1595 | ||
82ef3d5d WC |
1596 | static const void *netdev_queue_namespace(struct kobject *kobj) |
1597 | { | |
1598 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
1599 | struct device *dev = &queue->dev->dev; | |
1600 | const void *ns = NULL; | |
1601 | ||
1602 | if (dev->class && dev->class->ns_type) | |
1603 | ns = dev->class->namespace(dev); | |
1604 | ||
1605 | return ns; | |
1606 | } | |
1607 | ||
b0e37c0d DT |
1608 | static void netdev_queue_get_ownership(struct kobject *kobj, |
1609 | kuid_t *uid, kgid_t *gid) | |
1610 | { | |
1611 | const struct net *net = netdev_queue_namespace(kobj); | |
1612 | ||
1613 | net_ns_get_ownership(net, uid, gid); | |
1614 | } | |
1615 | ||
2b9c7581 | 1616 | static struct kobj_type netdev_queue_ktype __ro_after_init = { |
1d24eb48 TH |
1617 | .sysfs_ops = &netdev_queue_sysfs_ops, |
1618 | .release = netdev_queue_release, | |
be0d6926 | 1619 | .default_groups = netdev_queue_default_groups, |
82ef3d5d | 1620 | .namespace = netdev_queue_namespace, |
b0e37c0d | 1621 | .get_ownership = netdev_queue_get_ownership, |
1d24eb48 TH |
1622 | }; |
1623 | ||
6b53dafe | 1624 | static int netdev_queue_add_kobject(struct net_device *dev, int index) |
1d24eb48 | 1625 | { |
6b53dafe | 1626 | struct netdev_queue *queue = dev->_tx + index; |
1d24eb48 TH |
1627 | struct kobject *kobj = &queue->kobj; |
1628 | int error = 0; | |
1629 | ||
e0b60903 JH |
1630 | /* Kobject_put later will trigger netdev_queue_release call |
1631 | * which decreases dev refcount: Take that reference here | |
1632 | */ | |
1633 | dev_hold(queue->dev); | |
1634 | ||
6b53dafe | 1635 | kobj->kset = dev->queues_kset; |
1d24eb48 | 1636 | error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, |
6648c65e | 1637 | "tx-%u", index); |
114cf580 | 1638 | if (error) |
b8eb7183 | 1639 | goto err; |
114cf580 TH |
1640 | |
1641 | #ifdef CONFIG_BQL | |
1642 | error = sysfs_create_group(kobj, &dql_group); | |
b8eb7183 JH |
1643 | if (error) |
1644 | goto err; | |
114cf580 | 1645 | #endif |
1d24eb48 TH |
1646 | |
1647 | kobject_uevent(kobj, KOBJ_ADD); | |
48a322b6 | 1648 | return 0; |
1d24eb48 | 1649 | |
b8eb7183 JH |
1650 | err: |
1651 | kobject_put(kobj); | |
1652 | return error; | |
1d24eb48 | 1653 | } |
d755407d CB |
1654 | |
1655 | static int tx_queue_change_owner(struct net_device *ndev, int index, | |
1656 | kuid_t kuid, kgid_t kgid) | |
1657 | { | |
1658 | struct netdev_queue *queue = ndev->_tx + index; | |
1659 | struct kobject *kobj = &queue->kobj; | |
1660 | int error; | |
1661 | ||
1662 | error = sysfs_change_owner(kobj, kuid, kgid); | |
1663 | if (error) | |
1664 | return error; | |
1665 | ||
1666 | #ifdef CONFIG_BQL | |
1667 | error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid); | |
1668 | #endif | |
1669 | return error; | |
1670 | } | |
ccf5ff69 | 1671 | #endif /* CONFIG_SYSFS */ |
1d24eb48 TH |
1672 | |
1673 | int | |
6b53dafe | 1674 | netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) |
1d24eb48 | 1675 | { |
ccf5ff69 | 1676 | #ifdef CONFIG_SYSFS |
1d24eb48 TH |
1677 | int i; |
1678 | int error = 0; | |
1679 | ||
1680 | for (i = old_num; i < new_num; i++) { | |
6b53dafe | 1681 | error = netdev_queue_add_kobject(dev, i); |
1d24eb48 TH |
1682 | if (error) { |
1683 | new_num = old_num; | |
1684 | break; | |
1685 | } | |
1686 | } | |
1687 | ||
114cf580 | 1688 | while (--i >= new_num) { |
6b53dafe | 1689 | struct netdev_queue *queue = dev->_tx + i; |
114cf580 | 1690 | |
8b8f3e66 | 1691 | if (!refcount_read(&dev_net(dev)->ns.count)) |
002d8a1a | 1692 | queue->kobj.uevent_suppress = 1; |
114cf580 TH |
1693 | #ifdef CONFIG_BQL |
1694 | sysfs_remove_group(&queue->kobj, &dql_group); | |
1695 | #endif | |
1696 | kobject_put(&queue->kobj); | |
1697 | } | |
1d24eb48 TH |
1698 | |
1699 | return error; | |
bf264145 TH |
1700 | #else |
1701 | return 0; | |
ccf5ff69 | 1702 | #endif /* CONFIG_SYSFS */ |
1d24eb48 TH |
1703 | } |
1704 | ||
d755407d CB |
1705 | static int net_tx_queue_change_owner(struct net_device *dev, int num, |
1706 | kuid_t kuid, kgid_t kgid) | |
1707 | { | |
1708 | #ifdef CONFIG_SYSFS | |
1709 | int error = 0; | |
1710 | int i; | |
1711 | ||
1712 | for (i = 0; i < num; i++) { | |
1713 | error = tx_queue_change_owner(dev, i, kuid, kgid); | |
1714 | if (error) | |
1715 | break; | |
1716 | } | |
1717 | ||
1718 | return error; | |
1719 | #else | |
1720 | return 0; | |
1721 | #endif /* CONFIG_SYSFS */ | |
1722 | } | |
1723 | ||
6b53dafe | 1724 | static int register_queue_kobjects(struct net_device *dev) |
1d24eb48 | 1725 | { |
bf264145 | 1726 | int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; |
1d24eb48 | 1727 | |
ccf5ff69 | 1728 | #ifdef CONFIG_SYSFS |
6b53dafe | 1729 | dev->queues_kset = kset_create_and_add("queues", |
6648c65e | 1730 | NULL, &dev->dev.kobj); |
6b53dafe | 1731 | if (!dev->queues_kset) |
62fe0b40 | 1732 | return -ENOMEM; |
6b53dafe | 1733 | real_rx = dev->real_num_rx_queues; |
bf264145 | 1734 | #endif |
6b53dafe | 1735 | real_tx = dev->real_num_tx_queues; |
1d24eb48 | 1736 | |
6b53dafe | 1737 | error = net_rx_queue_update_kobjects(dev, 0, real_rx); |
1d24eb48 TH |
1738 | if (error) |
1739 | goto error; | |
bf264145 | 1740 | rxq = real_rx; |
1d24eb48 | 1741 | |
6b53dafe | 1742 | error = netdev_queue_update_kobjects(dev, 0, real_tx); |
1d24eb48 TH |
1743 | if (error) |
1744 | goto error; | |
bf264145 | 1745 | txq = real_tx; |
1d24eb48 TH |
1746 | |
1747 | return 0; | |
1748 | ||
1749 | error: | |
6b53dafe WC |
1750 | netdev_queue_update_kobjects(dev, txq, 0); |
1751 | net_rx_queue_update_kobjects(dev, rxq, 0); | |
895a5e96 Y |
1752 | #ifdef CONFIG_SYSFS |
1753 | kset_unregister(dev->queues_kset); | |
1754 | #endif | |
1d24eb48 | 1755 | return error; |
62fe0b40 | 1756 | } |
0a9627f2 | 1757 | |
d755407d CB |
1758 | static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid) |
1759 | { | |
1760 | int error = 0, real_rx = 0, real_tx = 0; | |
1761 | ||
1762 | #ifdef CONFIG_SYSFS | |
1763 | if (ndev->queues_kset) { | |
1764 | error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid); | |
1765 | if (error) | |
1766 | return error; | |
1767 | } | |
1768 | real_rx = ndev->real_num_rx_queues; | |
1769 | #endif | |
1770 | real_tx = ndev->real_num_tx_queues; | |
1771 | ||
1772 | error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid); | |
1773 | if (error) | |
1774 | return error; | |
1775 | ||
1776 | error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid); | |
1777 | if (error) | |
1778 | return error; | |
1779 | ||
1780 | return 0; | |
1781 | } | |
1782 | ||
6b53dafe | 1783 | static void remove_queue_kobjects(struct net_device *dev) |
62fe0b40 | 1784 | { |
bf264145 TH |
1785 | int real_rx = 0, real_tx = 0; |
1786 | ||
a953be53 | 1787 | #ifdef CONFIG_SYSFS |
6b53dafe | 1788 | real_rx = dev->real_num_rx_queues; |
bf264145 | 1789 | #endif |
6b53dafe | 1790 | real_tx = dev->real_num_tx_queues; |
bf264145 | 1791 | |
6b53dafe WC |
1792 | net_rx_queue_update_kobjects(dev, real_rx, 0); |
1793 | netdev_queue_update_kobjects(dev, real_tx, 0); | |
ccf5ff69 | 1794 | #ifdef CONFIG_SYSFS |
6b53dafe | 1795 | kset_unregister(dev->queues_kset); |
bf264145 | 1796 | #endif |
0a9627f2 | 1797 | } |
608b4b95 | 1798 | |
7dc5dbc8 EB |
1799 | static bool net_current_may_mount(void) |
1800 | { | |
1801 | struct net *net = current->nsproxy->net_ns; | |
1802 | ||
1803 | return ns_capable(net->user_ns, CAP_SYS_ADMIN); | |
1804 | } | |
1805 | ||
a685e089 | 1806 | static void *net_grab_current_ns(void) |
608b4b95 | 1807 | { |
a685e089 AV |
1808 | struct net *ns = current->nsproxy->net_ns; |
1809 | #ifdef CONFIG_NET_NS | |
1810 | if (ns) | |
c122e14d | 1811 | refcount_inc(&ns->passive); |
a685e089 AV |
1812 | #endif |
1813 | return ns; | |
608b4b95 EB |
1814 | } |
1815 | ||
1816 | static const void *net_initial_ns(void) | |
1817 | { | |
1818 | return &init_net; | |
1819 | } | |
1820 | ||
1821 | static const void *net_netlink_ns(struct sock *sk) | |
1822 | { | |
1823 | return sock_net(sk); | |
1824 | } | |
1825 | ||
737aec57 | 1826 | const struct kobj_ns_type_operations net_ns_type_operations = { |
608b4b95 | 1827 | .type = KOBJ_NS_TYPE_NET, |
7dc5dbc8 | 1828 | .current_may_mount = net_current_may_mount, |
a685e089 | 1829 | .grab_current_ns = net_grab_current_ns, |
608b4b95 EB |
1830 | .netlink_ns = net_netlink_ns, |
1831 | .initial_ns = net_initial_ns, | |
a685e089 | 1832 | .drop_ns = net_drop_ns, |
608b4b95 | 1833 | }; |
04600794 | 1834 | EXPORT_SYMBOL_GPL(net_ns_type_operations); |
608b4b95 | 1835 | |
7eff2e7a | 1836 | static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) |
1da177e4 | 1837 | { |
43cb76d9 | 1838 | struct net_device *dev = to_net_dev(d); |
7eff2e7a | 1839 | int retval; |
1da177e4 | 1840 | |
312c004d | 1841 | /* pass interface to uevent. */ |
7eff2e7a | 1842 | retval = add_uevent_var(env, "INTERFACE=%s", dev->name); |
bf62456e ER |
1843 | if (retval) |
1844 | goto exit; | |
ca2f37db JT |
1845 | |
1846 | /* pass ifindex to uevent. | |
1847 | * ifindex is useful as it won't change (interface name may change) | |
6648c65e | 1848 | * and is what RtNetlink uses natively. |
1849 | */ | |
7eff2e7a | 1850 | retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); |
1da177e4 | 1851 | |
bf62456e | 1852 | exit: |
bf62456e | 1853 | return retval; |
1da177e4 | 1854 | } |
1da177e4 LT |
1855 | |
1856 | /* | |
4ec93edb | 1857 | * netdev_release -- destroy and free a dead device. |
43cb76d9 | 1858 | * Called when last reference to device kobject is gone. |
1da177e4 | 1859 | */ |
43cb76d9 | 1860 | static void netdev_release(struct device *d) |
1da177e4 | 1861 | { |
43cb76d9 | 1862 | struct net_device *dev = to_net_dev(d); |
1da177e4 LT |
1863 | |
1864 | BUG_ON(dev->reg_state != NETREG_RELEASED); | |
1865 | ||
6c557001 FW |
1866 | /* no need to wait for rcu grace period: |
1867 | * device is dead and about to be freed. | |
1868 | */ | |
1869 | kfree(rcu_access_pointer(dev->ifalias)); | |
74d332c1 | 1870 | netdev_freemem(dev); |
1da177e4 LT |
1871 | } |
1872 | ||
608b4b95 EB |
1873 | static const void *net_namespace(struct device *d) |
1874 | { | |
5c29482d GT |
1875 | struct net_device *dev = to_net_dev(d); |
1876 | ||
608b4b95 EB |
1877 | return dev_net(dev); |
1878 | } | |
1879 | ||
b0e37c0d DT |
1880 | static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid) |
1881 | { | |
1882 | struct net_device *dev = to_net_dev(d); | |
1883 | const struct net *net = dev_net(dev); | |
1884 | ||
1885 | net_ns_get_ownership(net, uid, gid); | |
1886 | } | |
1887 | ||
e6d473e6 | 1888 | static struct class net_class __ro_after_init = { |
1da177e4 | 1889 | .name = "net", |
43cb76d9 | 1890 | .dev_release = netdev_release, |
6be8aeef | 1891 | .dev_groups = net_class_groups, |
43cb76d9 | 1892 | .dev_uevent = netdev_uevent, |
608b4b95 EB |
1893 | .ns_type = &net_ns_type_operations, |
1894 | .namespace = net_namespace, | |
b0e37c0d | 1895 | .get_ownership = net_get_ownership, |
1da177e4 LT |
1896 | }; |
1897 | ||
aa836df9 FF |
1898 | #ifdef CONFIG_OF_NET |
1899 | static int of_dev_node_match(struct device *dev, const void *data) | |
1900 | { | |
2e186a2c TW |
1901 | for (; dev; dev = dev->parent) { |
1902 | if (dev->of_node == data) | |
1903 | return 1; | |
1904 | } | |
aa836df9 | 1905 | |
2e186a2c | 1906 | return 0; |
aa836df9 FF |
1907 | } |
1908 | ||
9861f720 RK |
1909 | /* |
1910 | * of_find_net_device_by_node - lookup the net device for the device node | |
1911 | * @np: OF device node | |
1912 | * | |
1913 | * Looks up the net_device structure corresponding with the device node. | |
1914 | * If successful, returns a pointer to the net_device with the embedded | |
1915 | * struct device refcount incremented by one, or NULL on failure. The | |
1916 | * refcount must be dropped when done with the net_device. | |
1917 | */ | |
aa836df9 FF |
1918 | struct net_device *of_find_net_device_by_node(struct device_node *np) |
1919 | { | |
1920 | struct device *dev; | |
1921 | ||
1922 | dev = class_find_device(&net_class, NULL, np, of_dev_node_match); | |
1923 | if (!dev) | |
1924 | return NULL; | |
1925 | ||
1926 | return to_net_dev(dev); | |
1927 | } | |
1928 | EXPORT_SYMBOL(of_find_net_device_by_node); | |
1929 | #endif | |
1930 | ||
9093bbb2 SH |
1931 | /* Delete sysfs entries but hold kobject reference until after all |
1932 | * netdev references are gone. | |
1933 | */ | |
6b53dafe | 1934 | void netdev_unregister_kobject(struct net_device *ndev) |
1da177e4 | 1935 | { |
6648c65e | 1936 | struct device *dev = &ndev->dev; |
9093bbb2 | 1937 | |
8b8f3e66 | 1938 | if (!refcount_read(&dev_net(ndev)->ns.count)) |
002d8a1a AV |
1939 | dev_set_uevent_suppress(dev, 1); |
1940 | ||
9093bbb2 | 1941 | kobject_get(&dev->kobj); |
3891845e | 1942 | |
6b53dafe | 1943 | remove_queue_kobjects(ndev); |
0a9627f2 | 1944 | |
9802c8e2 ML |
1945 | pm_runtime_set_memalloc_noio(dev, false); |
1946 | ||
9093bbb2 | 1947 | device_del(dev); |
1da177e4 LT |
1948 | } |
1949 | ||
1950 | /* Create sysfs entries for network device. */ | |
6b53dafe | 1951 | int netdev_register_kobject(struct net_device *ndev) |
1da177e4 | 1952 | { |
6648c65e | 1953 | struct device *dev = &ndev->dev; |
6b53dafe | 1954 | const struct attribute_group **groups = ndev->sysfs_groups; |
0a9627f2 | 1955 | int error = 0; |
1da177e4 | 1956 | |
a1b3f594 | 1957 | device_initialize(dev); |
43cb76d9 | 1958 | dev->class = &net_class; |
6b53dafe | 1959 | dev->platform_data = ndev; |
43cb76d9 | 1960 | dev->groups = groups; |
1da177e4 | 1961 | |
6b53dafe | 1962 | dev_set_name(dev, "%s", ndev->name); |
1da177e4 | 1963 | |
8b41d188 | 1964 | #ifdef CONFIG_SYSFS |
0c509a6c EB |
1965 | /* Allow for a device specific group */ |
1966 | if (*groups) | |
1967 | groups++; | |
1da177e4 | 1968 | |
0c509a6c | 1969 | *groups++ = &netstat_group; |
38c1a01c JB |
1970 | |
1971 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) | |
6b53dafe | 1972 | if (ndev->ieee80211_ptr) |
38c1a01c JB |
1973 | *groups++ = &wireless_group; |
1974 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) | |
6b53dafe | 1975 | else if (ndev->wireless_handlers) |
38c1a01c JB |
1976 | *groups++ = &wireless_group; |
1977 | #endif | |
1978 | #endif | |
8b41d188 | 1979 | #endif /* CONFIG_SYSFS */ |
1da177e4 | 1980 | |
0a9627f2 TH |
1981 | error = device_add(dev); |
1982 | if (error) | |
8ed633b9 | 1983 | return error; |
0a9627f2 | 1984 | |
6b53dafe | 1985 | error = register_queue_kobjects(ndev); |
8ed633b9 WH |
1986 | if (error) { |
1987 | device_del(dev); | |
1988 | return error; | |
1989 | } | |
0a9627f2 | 1990 | |
9802c8e2 ML |
1991 | pm_runtime_set_memalloc_noio(dev, true); |
1992 | ||
0a9627f2 | 1993 | return error; |
1da177e4 LT |
1994 | } |
1995 | ||
e6dee9f3 CB |
1996 | /* Change owner for sysfs entries when moving network devices across network |
1997 | * namespaces owned by different user namespaces. | |
1998 | */ | |
1999 | int netdev_change_owner(struct net_device *ndev, const struct net *net_old, | |
2000 | const struct net *net_new) | |
2001 | { | |
2002 | struct device *dev = &ndev->dev; | |
2003 | kuid_t old_uid, new_uid; | |
2004 | kgid_t old_gid, new_gid; | |
2005 | int error; | |
2006 | ||
2007 | net_ns_get_ownership(net_old, &old_uid, &old_gid); | |
2008 | net_ns_get_ownership(net_new, &new_uid, &new_gid); | |
2009 | ||
2010 | /* The network namespace was changed but the owning user namespace is | |
2011 | * identical so there's no need to change the owner of sysfs entries. | |
2012 | */ | |
2013 | if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid)) | |
2014 | return 0; | |
2015 | ||
2016 | error = device_change_owner(dev, new_uid, new_gid); | |
2017 | if (error) | |
2018 | return error; | |
2019 | ||
d755407d CB |
2020 | error = queue_change_owner(ndev, new_uid, new_gid); |
2021 | if (error) | |
2022 | return error; | |
2023 | ||
e6dee9f3 CB |
2024 | return 0; |
2025 | } | |
2026 | ||
b793dc5c | 2027 | int netdev_class_create_file_ns(const struct class_attribute *class_attr, |
58292cbe | 2028 | const void *ns) |
b8a9787e | 2029 | { |
58292cbe | 2030 | return class_create_file_ns(&net_class, class_attr, ns); |
b8a9787e | 2031 | } |
58292cbe | 2032 | EXPORT_SYMBOL(netdev_class_create_file_ns); |
b8a9787e | 2033 | |
b793dc5c | 2034 | void netdev_class_remove_file_ns(const struct class_attribute *class_attr, |
58292cbe | 2035 | const void *ns) |
b8a9787e | 2036 | { |
58292cbe | 2037 | class_remove_file_ns(&net_class, class_attr, ns); |
b8a9787e | 2038 | } |
58292cbe | 2039 | EXPORT_SYMBOL(netdev_class_remove_file_ns); |
b8a9787e | 2040 | |
a48d4bb0 | 2041 | int __init netdev_kobject_init(void) |
1da177e4 | 2042 | { |
608b4b95 | 2043 | kobj_ns_type_register(&net_ns_type_operations); |
1da177e4 LT |
2044 | return class_register(&net_class); |
2045 | } |