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> | |
aecbe01e | 15 | #include <net/switchdev.h> |
1da177e4 | 16 | #include <linux/if_arp.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
174cd4b1 | 18 | #include <linux/sched/signal.h> |
608b4b95 | 19 | #include <linux/nsproxy.h> |
1da177e4 | 20 | #include <net/sock.h> |
608b4b95 | 21 | #include <net/net_namespace.h> |
1da177e4 | 22 | #include <linux/rtnetlink.h> |
fec5e652 | 23 | #include <linux/vmalloc.h> |
bc3b2d7f | 24 | #include <linux/export.h> |
114cf580 | 25 | #include <linux/jiffies.h> |
9802c8e2 | 26 | #include <linux/pm_runtime.h> |
aa836df9 | 27 | #include <linux/of.h> |
88832a22 | 28 | #include <linux/of_net.h> |
1da177e4 | 29 | |
342709ef PE |
30 | #include "net-sysfs.h" |
31 | ||
8b41d188 | 32 | #ifdef CONFIG_SYSFS |
1da177e4 LT |
33 | static const char fmt_hex[] = "%#x\n"; |
34 | static const char fmt_dec[] = "%d\n"; | |
35 | static const char fmt_ulong[] = "%lu\n"; | |
be1f3c2c | 36 | static const char fmt_u64[] = "%llu\n"; |
1da177e4 | 37 | |
4ec93edb | 38 | static inline int dev_isalive(const struct net_device *dev) |
1da177e4 | 39 | { |
fe9925b5 | 40 | return dev->reg_state <= NETREG_REGISTERED; |
1da177e4 LT |
41 | } |
42 | ||
43 | /* use same locking rules as GIF* ioctl's */ | |
43cb76d9 GKH |
44 | static ssize_t netdev_show(const struct device *dev, |
45 | struct device_attribute *attr, char *buf, | |
1da177e4 LT |
46 | ssize_t (*format)(const struct net_device *, char *)) |
47 | { | |
6b53dafe | 48 | struct net_device *ndev = to_net_dev(dev); |
1da177e4 LT |
49 | ssize_t ret = -EINVAL; |
50 | ||
51 | read_lock(&dev_base_lock); | |
6b53dafe WC |
52 | if (dev_isalive(ndev)) |
53 | ret = (*format)(ndev, buf); | |
1da177e4 LT |
54 | read_unlock(&dev_base_lock); |
55 | ||
56 | return ret; | |
57 | } | |
58 | ||
59 | /* generate a show function for simple field */ | |
60 | #define NETDEVICE_SHOW(field, format_string) \ | |
6b53dafe | 61 | static ssize_t format_##field(const struct net_device *dev, char *buf) \ |
1da177e4 | 62 | { \ |
6b53dafe | 63 | return sprintf(buf, format_string, dev->field); \ |
1da177e4 | 64 | } \ |
6be8aeef | 65 | static ssize_t field##_show(struct device *dev, \ |
43cb76d9 | 66 | struct device_attribute *attr, char *buf) \ |
1da177e4 | 67 | { \ |
43cb76d9 | 68 | return netdev_show(dev, attr, buf, format_##field); \ |
6be8aeef GKH |
69 | } \ |
70 | ||
71 | #define NETDEVICE_SHOW_RO(field, format_string) \ | |
72 | NETDEVICE_SHOW(field, format_string); \ | |
73 | static DEVICE_ATTR_RO(field) | |
1da177e4 | 74 | |
6be8aeef GKH |
75 | #define NETDEVICE_SHOW_RW(field, format_string) \ |
76 | NETDEVICE_SHOW(field, format_string); \ | |
77 | static DEVICE_ATTR_RW(field) | |
1da177e4 LT |
78 | |
79 | /* use same locking and permission rules as SIF* ioctl's */ | |
43cb76d9 | 80 | static ssize_t netdev_store(struct device *dev, struct device_attribute *attr, |
1da177e4 LT |
81 | const char *buf, size_t len, |
82 | int (*set)(struct net_device *, unsigned long)) | |
83 | { | |
5e1fccc0 EB |
84 | struct net_device *netdev = to_net_dev(dev); |
85 | struct net *net = dev_net(netdev); | |
1da177e4 LT |
86 | unsigned long new; |
87 | int ret = -EINVAL; | |
88 | ||
5e1fccc0 | 89 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
90 | return -EPERM; |
91 | ||
e1e420c7 SK |
92 | ret = kstrtoul(buf, 0, &new); |
93 | if (ret) | |
1da177e4 LT |
94 | goto err; |
95 | ||
5a5990d3 | 96 | if (!rtnl_trylock()) |
336ca57c | 97 | return restart_syscall(); |
5a5990d3 | 98 | |
5e1fccc0 | 99 | if (dev_isalive(netdev)) { |
6648c65e | 100 | ret = (*set)(netdev, new); |
101 | if (ret == 0) | |
1da177e4 LT |
102 | ret = len; |
103 | } | |
104 | rtnl_unlock(); | |
105 | err: | |
106 | return ret; | |
107 | } | |
108 | ||
6be8aeef | 109 | NETDEVICE_SHOW_RO(dev_id, fmt_hex); |
3f85944f | 110 | NETDEVICE_SHOW_RO(dev_port, fmt_dec); |
6be8aeef GKH |
111 | NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec); |
112 | NETDEVICE_SHOW_RO(addr_len, fmt_dec); | |
6be8aeef GKH |
113 | NETDEVICE_SHOW_RO(ifindex, fmt_dec); |
114 | NETDEVICE_SHOW_RO(type, fmt_dec); | |
115 | NETDEVICE_SHOW_RO(link_mode, fmt_dec); | |
1da177e4 | 116 | |
a54acb3a ND |
117 | static ssize_t iflink_show(struct device *dev, struct device_attribute *attr, |
118 | char *buf) | |
119 | { | |
120 | struct net_device *ndev = to_net_dev(dev); | |
121 | ||
122 | return sprintf(buf, fmt_dec, dev_get_iflink(ndev)); | |
123 | } | |
124 | static DEVICE_ATTR_RO(iflink); | |
125 | ||
6b53dafe | 126 | static ssize_t format_name_assign_type(const struct net_device *dev, char *buf) |
685343fc | 127 | { |
6b53dafe | 128 | return sprintf(buf, fmt_dec, dev->name_assign_type); |
685343fc TG |
129 | } |
130 | ||
131 | static ssize_t name_assign_type_show(struct device *dev, | |
132 | struct device_attribute *attr, | |
133 | char *buf) | |
134 | { | |
6b53dafe | 135 | struct net_device *ndev = to_net_dev(dev); |
685343fc TG |
136 | ssize_t ret = -EINVAL; |
137 | ||
6b53dafe | 138 | if (ndev->name_assign_type != NET_NAME_UNKNOWN) |
685343fc TG |
139 | ret = netdev_show(dev, attr, buf, format_name_assign_type); |
140 | ||
141 | return ret; | |
142 | } | |
143 | static DEVICE_ATTR_RO(name_assign_type); | |
144 | ||
1da177e4 | 145 | /* use same locking rules as GIFHWADDR ioctl's */ |
6be8aeef | 146 | static ssize_t address_show(struct device *dev, struct device_attribute *attr, |
43cb76d9 | 147 | char *buf) |
1da177e4 | 148 | { |
6b53dafe | 149 | struct net_device *ndev = to_net_dev(dev); |
1da177e4 LT |
150 | ssize_t ret = -EINVAL; |
151 | ||
152 | read_lock(&dev_base_lock); | |
6b53dafe WC |
153 | if (dev_isalive(ndev)) |
154 | ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len); | |
1da177e4 LT |
155 | read_unlock(&dev_base_lock); |
156 | return ret; | |
157 | } | |
6be8aeef | 158 | static DEVICE_ATTR_RO(address); |
1da177e4 | 159 | |
6be8aeef GKH |
160 | static ssize_t broadcast_show(struct device *dev, |
161 | struct device_attribute *attr, char *buf) | |
1da177e4 | 162 | { |
6b53dafe | 163 | struct net_device *ndev = to_net_dev(dev); |
6648c65e | 164 | |
6b53dafe WC |
165 | if (dev_isalive(ndev)) |
166 | return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len); | |
1da177e4 LT |
167 | return -EINVAL; |
168 | } | |
6be8aeef | 169 | static DEVICE_ATTR_RO(broadcast); |
1da177e4 | 170 | |
6b53dafe | 171 | static int change_carrier(struct net_device *dev, unsigned long new_carrier) |
fdae0fde | 172 | { |
6b53dafe | 173 | if (!netif_running(dev)) |
fdae0fde | 174 | return -EINVAL; |
6648c65e | 175 | return dev_change_carrier(dev, (bool)new_carrier); |
fdae0fde JP |
176 | } |
177 | ||
6be8aeef GKH |
178 | static ssize_t carrier_store(struct device *dev, struct device_attribute *attr, |
179 | const char *buf, size_t len) | |
fdae0fde JP |
180 | { |
181 | return netdev_store(dev, attr, buf, len, change_carrier); | |
182 | } | |
183 | ||
6be8aeef | 184 | static ssize_t carrier_show(struct device *dev, |
43cb76d9 | 185 | struct device_attribute *attr, char *buf) |
1da177e4 LT |
186 | { |
187 | struct net_device *netdev = to_net_dev(dev); | |
6648c65e | 188 | |
189 | if (netif_running(netdev)) | |
1da177e4 | 190 | return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev)); |
6648c65e | 191 | |
1da177e4 LT |
192 | return -EINVAL; |
193 | } | |
6be8aeef | 194 | static DEVICE_ATTR_RW(carrier); |
1da177e4 | 195 | |
6be8aeef | 196 | static ssize_t speed_show(struct device *dev, |
d519e17e AG |
197 | struct device_attribute *attr, char *buf) |
198 | { | |
199 | struct net_device *netdev = to_net_dev(dev); | |
200 | int ret = -EINVAL; | |
201 | ||
202 | if (!rtnl_trylock()) | |
203 | return restart_syscall(); | |
204 | ||
8ae6daca | 205 | if (netif_running(netdev)) { |
7cad1bac DD |
206 | struct ethtool_link_ksettings cmd; |
207 | ||
208 | if (!__ethtool_get_link_ksettings(netdev, &cmd)) | |
209 | ret = sprintf(buf, fmt_dec, cmd.base.speed); | |
d519e17e AG |
210 | } |
211 | rtnl_unlock(); | |
212 | return ret; | |
213 | } | |
6be8aeef | 214 | static DEVICE_ATTR_RO(speed); |
d519e17e | 215 | |
6be8aeef | 216 | static ssize_t duplex_show(struct device *dev, |
d519e17e AG |
217 | struct device_attribute *attr, char *buf) |
218 | { | |
219 | struct net_device *netdev = to_net_dev(dev); | |
220 | int ret = -EINVAL; | |
221 | ||
222 | if (!rtnl_trylock()) | |
223 | return restart_syscall(); | |
224 | ||
8ae6daca | 225 | if (netif_running(netdev)) { |
7cad1bac DD |
226 | struct ethtool_link_ksettings cmd; |
227 | ||
228 | if (!__ethtool_get_link_ksettings(netdev, &cmd)) { | |
c6c13965 | 229 | const char *duplex; |
7cad1bac DD |
230 | |
231 | switch (cmd.base.duplex) { | |
c6c13965 NA |
232 | case DUPLEX_HALF: |
233 | duplex = "half"; | |
234 | break; | |
235 | case DUPLEX_FULL: | |
236 | duplex = "full"; | |
237 | break; | |
238 | default: | |
239 | duplex = "unknown"; | |
240 | break; | |
241 | } | |
242 | ret = sprintf(buf, "%s\n", duplex); | |
243 | } | |
d519e17e AG |
244 | } |
245 | rtnl_unlock(); | |
246 | return ret; | |
247 | } | |
6be8aeef | 248 | static DEVICE_ATTR_RO(duplex); |
d519e17e | 249 | |
6be8aeef | 250 | static ssize_t dormant_show(struct device *dev, |
43cb76d9 | 251 | struct device_attribute *attr, char *buf) |
b00055aa SR |
252 | { |
253 | struct net_device *netdev = to_net_dev(dev); | |
254 | ||
255 | if (netif_running(netdev)) | |
256 | return sprintf(buf, fmt_dec, !!netif_dormant(netdev)); | |
257 | ||
258 | return -EINVAL; | |
259 | } | |
6be8aeef | 260 | static DEVICE_ATTR_RO(dormant); |
b00055aa | 261 | |
36cbd3dc | 262 | static const char *const operstates[] = { |
b00055aa SR |
263 | "unknown", |
264 | "notpresent", /* currently unused */ | |
265 | "down", | |
266 | "lowerlayerdown", | |
267 | "testing", /* currently unused */ | |
268 | "dormant", | |
269 | "up" | |
270 | }; | |
271 | ||
6be8aeef | 272 | static ssize_t operstate_show(struct device *dev, |
43cb76d9 | 273 | struct device_attribute *attr, char *buf) |
b00055aa SR |
274 | { |
275 | const struct net_device *netdev = to_net_dev(dev); | |
276 | unsigned char operstate; | |
277 | ||
278 | read_lock(&dev_base_lock); | |
279 | operstate = netdev->operstate; | |
280 | if (!netif_running(netdev)) | |
281 | operstate = IF_OPER_DOWN; | |
282 | read_unlock(&dev_base_lock); | |
283 | ||
e3a5cd9e | 284 | if (operstate >= ARRAY_SIZE(operstates)) |
b00055aa SR |
285 | return -EINVAL; /* should not happen */ |
286 | ||
287 | return sprintf(buf, "%s\n", operstates[operstate]); | |
288 | } | |
6be8aeef | 289 | static DEVICE_ATTR_RO(operstate); |
b00055aa | 290 | |
2d3b479d | 291 | static ssize_t carrier_changes_show(struct device *dev, |
292 | struct device_attribute *attr, | |
293 | char *buf) | |
294 | { | |
295 | struct net_device *netdev = to_net_dev(dev); | |
6648c65e | 296 | |
2d3b479d | 297 | return sprintf(buf, fmt_dec, |
b2d3bcfa DD |
298 | atomic_read(&netdev->carrier_up_count) + |
299 | atomic_read(&netdev->carrier_down_count)); | |
2d3b479d | 300 | } |
301 | static DEVICE_ATTR_RO(carrier_changes); | |
302 | ||
b2d3bcfa DD |
303 | static ssize_t carrier_up_count_show(struct device *dev, |
304 | struct device_attribute *attr, | |
305 | char *buf) | |
306 | { | |
307 | struct net_device *netdev = to_net_dev(dev); | |
308 | ||
309 | return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count)); | |
310 | } | |
311 | static DEVICE_ATTR_RO(carrier_up_count); | |
312 | ||
313 | static ssize_t carrier_down_count_show(struct device *dev, | |
314 | struct device_attribute *attr, | |
315 | char *buf) | |
316 | { | |
317 | struct net_device *netdev = to_net_dev(dev); | |
318 | ||
319 | return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count)); | |
320 | } | |
321 | static DEVICE_ATTR_RO(carrier_down_count); | |
322 | ||
1da177e4 | 323 | /* read-write attributes */ |
1da177e4 | 324 | |
6b53dafe | 325 | static int change_mtu(struct net_device *dev, unsigned long new_mtu) |
1da177e4 | 326 | { |
6648c65e | 327 | return dev_set_mtu(dev, (int)new_mtu); |
1da177e4 LT |
328 | } |
329 | ||
6be8aeef | 330 | static ssize_t mtu_store(struct device *dev, struct device_attribute *attr, |
43cb76d9 | 331 | const char *buf, size_t len) |
1da177e4 | 332 | { |
43cb76d9 | 333 | return netdev_store(dev, attr, buf, len, change_mtu); |
1da177e4 | 334 | } |
6be8aeef | 335 | NETDEVICE_SHOW_RW(mtu, fmt_dec); |
1da177e4 | 336 | |
6b53dafe | 337 | static int change_flags(struct net_device *dev, unsigned long new_flags) |
1da177e4 | 338 | { |
6648c65e | 339 | return dev_change_flags(dev, (unsigned int)new_flags); |
1da177e4 LT |
340 | } |
341 | ||
6be8aeef | 342 | static ssize_t flags_store(struct device *dev, struct device_attribute *attr, |
43cb76d9 | 343 | const char *buf, size_t len) |
1da177e4 | 344 | { |
43cb76d9 | 345 | return netdev_store(dev, attr, buf, len, change_flags); |
1da177e4 | 346 | } |
6be8aeef | 347 | NETDEVICE_SHOW_RW(flags, fmt_hex); |
1da177e4 | 348 | |
6b53dafe | 349 | static int change_tx_queue_len(struct net_device *dev, unsigned long new_len) |
1da177e4 | 350 | { |
0cd29503 AD |
351 | unsigned int orig_len = dev->tx_queue_len; |
352 | int res; | |
353 | ||
354 | if (new_len != (unsigned int)new_len) | |
355 | return -ERANGE; | |
08294a26 JW |
356 | |
357 | if (new_len != orig_len) { | |
358 | dev->tx_queue_len = new_len; | |
359 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
360 | res = notifier_to_errno(res); | |
361 | if (res) { | |
362 | netdev_err(dev, | |
363 | "refused to change device tx_queue_len\n"); | |
364 | dev->tx_queue_len = orig_len; | |
365 | return -EFAULT; | |
366 | } | |
367 | } | |
368 | ||
1da177e4 LT |
369 | return 0; |
370 | } | |
371 | ||
6be8aeef | 372 | static ssize_t tx_queue_len_store(struct device *dev, |
43cb76d9 GKH |
373 | struct device_attribute *attr, |
374 | const char *buf, size_t len) | |
1da177e4 | 375 | { |
5e1fccc0 EB |
376 | if (!capable(CAP_NET_ADMIN)) |
377 | return -EPERM; | |
378 | ||
43cb76d9 | 379 | return netdev_store(dev, attr, buf, len, change_tx_queue_len); |
1da177e4 | 380 | } |
0cd29503 | 381 | NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec); |
1da177e4 | 382 | |
3b47d303 ED |
383 | static int change_gro_flush_timeout(struct net_device *dev, unsigned long val) |
384 | { | |
385 | dev->gro_flush_timeout = val; | |
386 | return 0; | |
387 | } | |
388 | ||
389 | static ssize_t gro_flush_timeout_store(struct device *dev, | |
6648c65e | 390 | struct device_attribute *attr, |
391 | const char *buf, size_t len) | |
3b47d303 ED |
392 | { |
393 | if (!capable(CAP_NET_ADMIN)) | |
394 | return -EPERM; | |
395 | ||
396 | return netdev_store(dev, attr, buf, len, change_gro_flush_timeout); | |
397 | } | |
398 | NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong); | |
399 | ||
6be8aeef | 400 | static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr, |
0b815a1a SH |
401 | const char *buf, size_t len) |
402 | { | |
403 | struct net_device *netdev = to_net_dev(dev); | |
5e1fccc0 | 404 | struct net *net = dev_net(netdev); |
0b815a1a | 405 | size_t count = len; |
c92eb77a | 406 | ssize_t ret = 0; |
0b815a1a | 407 | |
5e1fccc0 | 408 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
0b815a1a SH |
409 | return -EPERM; |
410 | ||
411 | /* ignore trailing newline */ | |
412 | if (len > 0 && buf[len - 1] == '\n') | |
413 | --count; | |
414 | ||
c92eb77a RP |
415 | if (!rtnl_trylock()) |
416 | return restart_syscall(); | |
0b815a1a | 417 | |
c92eb77a RP |
418 | if (dev_isalive(netdev)) { |
419 | ret = dev_set_alias(netdev, buf, count); | |
420 | if (ret < 0) | |
421 | goto err; | |
422 | ret = len; | |
423 | netdev_state_change(netdev); | |
424 | } | |
425 | err: | |
426 | rtnl_unlock(); | |
427 | ||
428 | return ret; | |
0b815a1a SH |
429 | } |
430 | ||
6be8aeef | 431 | static ssize_t ifalias_show(struct device *dev, |
0b815a1a SH |
432 | struct device_attribute *attr, char *buf) |
433 | { | |
434 | const struct net_device *netdev = to_net_dev(dev); | |
6c557001 | 435 | char tmp[IFALIASZ]; |
0b815a1a SH |
436 | ssize_t ret = 0; |
437 | ||
6c557001 FW |
438 | ret = dev_get_alias(netdev, tmp, sizeof(tmp)); |
439 | if (ret > 0) | |
440 | ret = sprintf(buf, "%s\n", tmp); | |
0b815a1a SH |
441 | return ret; |
442 | } | |
6be8aeef | 443 | static DEVICE_ATTR_RW(ifalias); |
a512b92b | 444 | |
6b53dafe | 445 | static int change_group(struct net_device *dev, unsigned long new_group) |
a512b92b | 446 | { |
6648c65e | 447 | dev_set_group(dev, (int)new_group); |
a512b92b VD |
448 | return 0; |
449 | } | |
450 | ||
6be8aeef GKH |
451 | static ssize_t group_store(struct device *dev, struct device_attribute *attr, |
452 | const char *buf, size_t len) | |
a512b92b VD |
453 | { |
454 | return netdev_store(dev, attr, buf, len, change_group); | |
455 | } | |
6be8aeef GKH |
456 | NETDEVICE_SHOW(group, fmt_dec); |
457 | static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store); | |
458 | ||
d746d707 AK |
459 | static int change_proto_down(struct net_device *dev, unsigned long proto_down) |
460 | { | |
6648c65e | 461 | return dev_change_proto_down(dev, (bool)proto_down); |
d746d707 AK |
462 | } |
463 | ||
464 | static ssize_t proto_down_store(struct device *dev, | |
465 | struct device_attribute *attr, | |
466 | const char *buf, size_t len) | |
467 | { | |
468 | return netdev_store(dev, attr, buf, len, change_proto_down); | |
469 | } | |
470 | NETDEVICE_SHOW_RW(proto_down, fmt_dec); | |
471 | ||
cc998ff8 | 472 | static ssize_t phys_port_id_show(struct device *dev, |
ff80e519 JP |
473 | struct device_attribute *attr, char *buf) |
474 | { | |
475 | struct net_device *netdev = to_net_dev(dev); | |
476 | ssize_t ret = -EINVAL; | |
477 | ||
478 | if (!rtnl_trylock()) | |
479 | return restart_syscall(); | |
480 | ||
481 | if (dev_isalive(netdev)) { | |
02637fce | 482 | struct netdev_phys_item_id ppid; |
ff80e519 JP |
483 | |
484 | ret = dev_get_phys_port_id(netdev, &ppid); | |
485 | if (!ret) | |
486 | ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id); | |
487 | } | |
488 | rtnl_unlock(); | |
489 | ||
490 | return ret; | |
491 | } | |
cc998ff8 LT |
492 | static DEVICE_ATTR_RO(phys_port_id); |
493 | ||
db24a904 DA |
494 | static ssize_t phys_port_name_show(struct device *dev, |
495 | struct device_attribute *attr, char *buf) | |
496 | { | |
497 | struct net_device *netdev = to_net_dev(dev); | |
498 | ssize_t ret = -EINVAL; | |
499 | ||
500 | if (!rtnl_trylock()) | |
501 | return restart_syscall(); | |
502 | ||
503 | if (dev_isalive(netdev)) { | |
504 | char name[IFNAMSIZ]; | |
505 | ||
506 | ret = dev_get_phys_port_name(netdev, name, sizeof(name)); | |
507 | if (!ret) | |
508 | ret = sprintf(buf, "%s\n", name); | |
509 | } | |
510 | rtnl_unlock(); | |
511 | ||
512 | return ret; | |
513 | } | |
514 | static DEVICE_ATTR_RO(phys_port_name); | |
515 | ||
aecbe01e JP |
516 | static ssize_t phys_switch_id_show(struct device *dev, |
517 | struct device_attribute *attr, char *buf) | |
518 | { | |
519 | struct net_device *netdev = to_net_dev(dev); | |
520 | ssize_t ret = -EINVAL; | |
521 | ||
522 | if (!rtnl_trylock()) | |
523 | return restart_syscall(); | |
524 | ||
525 | if (dev_isalive(netdev)) { | |
f8e20a9f | 526 | struct switchdev_attr attr = { |
6ff64f6f | 527 | .orig_dev = netdev, |
1f868398 | 528 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
f8e20a9f SF |
529 | .flags = SWITCHDEV_F_NO_RECURSE, |
530 | }; | |
aecbe01e | 531 | |
f8e20a9f | 532 | ret = switchdev_port_attr_get(netdev, &attr); |
aecbe01e | 533 | if (!ret) |
42275bd8 SF |
534 | ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len, |
535 | attr.u.ppid.id); | |
aecbe01e JP |
536 | } |
537 | rtnl_unlock(); | |
538 | ||
539 | return ret; | |
540 | } | |
541 | static DEVICE_ATTR_RO(phys_switch_id); | |
542 | ||
ec6cc599 | 543 | static struct attribute *net_class_attrs[] __ro_after_init = { |
6be8aeef GKH |
544 | &dev_attr_netdev_group.attr, |
545 | &dev_attr_type.attr, | |
546 | &dev_attr_dev_id.attr, | |
3f85944f | 547 | &dev_attr_dev_port.attr, |
6be8aeef GKH |
548 | &dev_attr_iflink.attr, |
549 | &dev_attr_ifindex.attr, | |
685343fc | 550 | &dev_attr_name_assign_type.attr, |
6be8aeef GKH |
551 | &dev_attr_addr_assign_type.attr, |
552 | &dev_attr_addr_len.attr, | |
553 | &dev_attr_link_mode.attr, | |
554 | &dev_attr_address.attr, | |
555 | &dev_attr_broadcast.attr, | |
556 | &dev_attr_speed.attr, | |
557 | &dev_attr_duplex.attr, | |
558 | &dev_attr_dormant.attr, | |
559 | &dev_attr_operstate.attr, | |
2d3b479d | 560 | &dev_attr_carrier_changes.attr, |
6be8aeef GKH |
561 | &dev_attr_ifalias.attr, |
562 | &dev_attr_carrier.attr, | |
563 | &dev_attr_mtu.attr, | |
564 | &dev_attr_flags.attr, | |
565 | &dev_attr_tx_queue_len.attr, | |
3b47d303 | 566 | &dev_attr_gro_flush_timeout.attr, |
cc998ff8 | 567 | &dev_attr_phys_port_id.attr, |
db24a904 | 568 | &dev_attr_phys_port_name.attr, |
aecbe01e | 569 | &dev_attr_phys_switch_id.attr, |
d746d707 | 570 | &dev_attr_proto_down.attr, |
b2d3bcfa DD |
571 | &dev_attr_carrier_up_count.attr, |
572 | &dev_attr_carrier_down_count.attr, | |
6be8aeef | 573 | NULL, |
1da177e4 | 574 | }; |
6be8aeef | 575 | ATTRIBUTE_GROUPS(net_class); |
1da177e4 LT |
576 | |
577 | /* Show a given an attribute in the statistics group */ | |
43cb76d9 GKH |
578 | static ssize_t netstat_show(const struct device *d, |
579 | struct device_attribute *attr, char *buf, | |
1da177e4 LT |
580 | unsigned long offset) |
581 | { | |
43cb76d9 | 582 | struct net_device *dev = to_net_dev(d); |
1da177e4 LT |
583 | ssize_t ret = -EINVAL; |
584 | ||
be1f3c2c | 585 | WARN_ON(offset > sizeof(struct rtnl_link_stats64) || |
6648c65e | 586 | offset % sizeof(u64) != 0); |
1da177e4 LT |
587 | |
588 | read_lock(&dev_base_lock); | |
96e74088 | 589 | if (dev_isalive(dev)) { |
28172739 ED |
590 | struct rtnl_link_stats64 temp; |
591 | const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); | |
592 | ||
6648c65e | 593 | ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset)); |
96e74088 | 594 | } |
1da177e4 LT |
595 | read_unlock(&dev_base_lock); |
596 | return ret; | |
597 | } | |
598 | ||
599 | /* generate a read-only statistics attribute */ | |
600 | #define NETSTAT_ENTRY(name) \ | |
6be8aeef | 601 | static ssize_t name##_show(struct device *d, \ |
6648c65e | 602 | struct device_attribute *attr, char *buf) \ |
1da177e4 | 603 | { \ |
43cb76d9 | 604 | return netstat_show(d, attr, buf, \ |
be1f3c2c | 605 | offsetof(struct rtnl_link_stats64, name)); \ |
1da177e4 | 606 | } \ |
6be8aeef | 607 | static DEVICE_ATTR_RO(name) |
1da177e4 LT |
608 | |
609 | NETSTAT_ENTRY(rx_packets); | |
610 | NETSTAT_ENTRY(tx_packets); | |
611 | NETSTAT_ENTRY(rx_bytes); | |
612 | NETSTAT_ENTRY(tx_bytes); | |
613 | NETSTAT_ENTRY(rx_errors); | |
614 | NETSTAT_ENTRY(tx_errors); | |
615 | NETSTAT_ENTRY(rx_dropped); | |
616 | NETSTAT_ENTRY(tx_dropped); | |
617 | NETSTAT_ENTRY(multicast); | |
618 | NETSTAT_ENTRY(collisions); | |
619 | NETSTAT_ENTRY(rx_length_errors); | |
620 | NETSTAT_ENTRY(rx_over_errors); | |
621 | NETSTAT_ENTRY(rx_crc_errors); | |
622 | NETSTAT_ENTRY(rx_frame_errors); | |
623 | NETSTAT_ENTRY(rx_fifo_errors); | |
624 | NETSTAT_ENTRY(rx_missed_errors); | |
625 | NETSTAT_ENTRY(tx_aborted_errors); | |
626 | NETSTAT_ENTRY(tx_carrier_errors); | |
627 | NETSTAT_ENTRY(tx_fifo_errors); | |
628 | NETSTAT_ENTRY(tx_heartbeat_errors); | |
629 | NETSTAT_ENTRY(tx_window_errors); | |
630 | NETSTAT_ENTRY(rx_compressed); | |
631 | NETSTAT_ENTRY(tx_compressed); | |
6e7333d3 | 632 | NETSTAT_ENTRY(rx_nohandler); |
1da177e4 | 633 | |
ec6cc599 | 634 | static struct attribute *netstat_attrs[] __ro_after_init = { |
43cb76d9 GKH |
635 | &dev_attr_rx_packets.attr, |
636 | &dev_attr_tx_packets.attr, | |
637 | &dev_attr_rx_bytes.attr, | |
638 | &dev_attr_tx_bytes.attr, | |
639 | &dev_attr_rx_errors.attr, | |
640 | &dev_attr_tx_errors.attr, | |
641 | &dev_attr_rx_dropped.attr, | |
642 | &dev_attr_tx_dropped.attr, | |
643 | &dev_attr_multicast.attr, | |
644 | &dev_attr_collisions.attr, | |
645 | &dev_attr_rx_length_errors.attr, | |
646 | &dev_attr_rx_over_errors.attr, | |
647 | &dev_attr_rx_crc_errors.attr, | |
648 | &dev_attr_rx_frame_errors.attr, | |
649 | &dev_attr_rx_fifo_errors.attr, | |
650 | &dev_attr_rx_missed_errors.attr, | |
651 | &dev_attr_tx_aborted_errors.attr, | |
652 | &dev_attr_tx_carrier_errors.attr, | |
653 | &dev_attr_tx_fifo_errors.attr, | |
654 | &dev_attr_tx_heartbeat_errors.attr, | |
655 | &dev_attr_tx_window_errors.attr, | |
656 | &dev_attr_rx_compressed.attr, | |
657 | &dev_attr_tx_compressed.attr, | |
6e7333d3 | 658 | &dev_attr_rx_nohandler.attr, |
1da177e4 LT |
659 | NULL |
660 | }; | |
661 | ||
38ef00cc | 662 | static const struct attribute_group netstat_group = { |
1da177e4 LT |
663 | .name = "statistics", |
664 | .attrs = netstat_attrs, | |
665 | }; | |
38c1a01c JB |
666 | |
667 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) | |
668 | static struct attribute *wireless_attrs[] = { | |
669 | NULL | |
670 | }; | |
671 | ||
38ef00cc | 672 | static const struct attribute_group wireless_group = { |
38c1a01c JB |
673 | .name = "wireless", |
674 | .attrs = wireless_attrs, | |
675 | }; | |
676 | #endif | |
6be8aeef GKH |
677 | |
678 | #else /* CONFIG_SYSFS */ | |
679 | #define net_class_groups NULL | |
d6523ddf | 680 | #endif /* CONFIG_SYSFS */ |
1da177e4 | 681 | |
a953be53 | 682 | #ifdef CONFIG_SYSFS |
6648c65e | 683 | #define to_rx_queue_attr(_attr) \ |
684 | container_of(_attr, struct rx_queue_attribute, attr) | |
0a9627f2 TH |
685 | |
686 | #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj) | |
687 | ||
688 | static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr, | |
689 | char *buf) | |
690 | { | |
667e427b | 691 | const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
0a9627f2 TH |
692 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
693 | ||
694 | if (!attribute->show) | |
695 | return -EIO; | |
696 | ||
718ad681 | 697 | return attribute->show(queue, buf); |
0a9627f2 TH |
698 | } |
699 | ||
700 | static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr, | |
701 | const char *buf, size_t count) | |
702 | { | |
667e427b | 703 | const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr); |
0a9627f2 TH |
704 | struct netdev_rx_queue *queue = to_rx_queue(kobj); |
705 | ||
706 | if (!attribute->store) | |
707 | return -EIO; | |
708 | ||
718ad681 | 709 | return attribute->store(queue, buf, count); |
0a9627f2 TH |
710 | } |
711 | ||
fa50d645 | 712 | static const struct sysfs_ops rx_queue_sysfs_ops = { |
0a9627f2 TH |
713 | .show = rx_queue_attr_show, |
714 | .store = rx_queue_attr_store, | |
715 | }; | |
716 | ||
a953be53 | 717 | #ifdef CONFIG_RPS |
718ad681 | 718 | static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf) |
0a9627f2 TH |
719 | { |
720 | struct rps_map *map; | |
721 | cpumask_var_t mask; | |
f0906827 | 722 | int i, len; |
0a9627f2 TH |
723 | |
724 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) | |
725 | return -ENOMEM; | |
726 | ||
727 | rcu_read_lock(); | |
728 | map = rcu_dereference(queue->rps_map); | |
729 | if (map) | |
730 | for (i = 0; i < map->len; i++) | |
731 | cpumask_set_cpu(map->cpus[i], mask); | |
732 | ||
f0906827 | 733 | len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); |
0a9627f2 | 734 | rcu_read_unlock(); |
0a9627f2 | 735 | free_cpumask_var(mask); |
f0906827 TH |
736 | |
737 | return len < PAGE_SIZE ? len : -EINVAL; | |
0a9627f2 TH |
738 | } |
739 | ||
f5acb907 | 740 | static ssize_t store_rps_map(struct netdev_rx_queue *queue, |
718ad681 | 741 | const char *buf, size_t len) |
0a9627f2 TH |
742 | { |
743 | struct rps_map *old_map, *map; | |
744 | cpumask_var_t mask; | |
745 | int err, cpu, i; | |
da65ad1f | 746 | static DEFINE_MUTEX(rps_map_mutex); |
0a9627f2 TH |
747 | |
748 | if (!capable(CAP_NET_ADMIN)) | |
749 | return -EPERM; | |
750 | ||
751 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
752 | return -ENOMEM; | |
753 | ||
754 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); | |
755 | if (err) { | |
756 | free_cpumask_var(mask); | |
757 | return err; | |
758 | } | |
759 | ||
95c96174 | 760 | map = kzalloc(max_t(unsigned int, |
6648c65e | 761 | RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES), |
762 | GFP_KERNEL); | |
0a9627f2 TH |
763 | if (!map) { |
764 | free_cpumask_var(mask); | |
765 | return -ENOMEM; | |
766 | } | |
767 | ||
768 | i = 0; | |
769 | for_each_cpu_and(cpu, mask, cpu_online_mask) | |
770 | map->cpus[i++] = cpu; | |
771 | ||
6648c65e | 772 | if (i) { |
0a9627f2 | 773 | map->len = i; |
6648c65e | 774 | } else { |
0a9627f2 TH |
775 | kfree(map); |
776 | map = NULL; | |
777 | } | |
778 | ||
da65ad1f | 779 | mutex_lock(&rps_map_mutex); |
6e3f7faf | 780 | old_map = rcu_dereference_protected(queue->rps_map, |
da65ad1f | 781 | mutex_is_locked(&rps_map_mutex)); |
0a9627f2 | 782 | rcu_assign_pointer(queue->rps_map, map); |
0a9627f2 | 783 | |
adc9300e | 784 | if (map) |
c5905afb | 785 | static_key_slow_inc(&rps_needed); |
10e4ea75 | 786 | if (old_map) |
c5905afb | 787 | static_key_slow_dec(&rps_needed); |
10e4ea75 | 788 | |
da65ad1f | 789 | mutex_unlock(&rps_map_mutex); |
10e4ea75 TH |
790 | |
791 | if (old_map) | |
792 | kfree_rcu(old_map, rcu); | |
793 | ||
0a9627f2 TH |
794 | free_cpumask_var(mask); |
795 | return len; | |
796 | } | |
797 | ||
fec5e652 | 798 | static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
fec5e652 TH |
799 | char *buf) |
800 | { | |
801 | struct rps_dev_flow_table *flow_table; | |
60b778ce | 802 | unsigned long val = 0; |
fec5e652 TH |
803 | |
804 | rcu_read_lock(); | |
805 | flow_table = rcu_dereference(queue->rps_flow_table); | |
806 | if (flow_table) | |
60b778ce | 807 | val = (unsigned long)flow_table->mask + 1; |
fec5e652 TH |
808 | rcu_read_unlock(); |
809 | ||
60b778ce | 810 | return sprintf(buf, "%lu\n", val); |
fec5e652 TH |
811 | } |
812 | ||
fec5e652 TH |
813 | static void rps_dev_flow_table_release(struct rcu_head *rcu) |
814 | { | |
815 | struct rps_dev_flow_table *table = container_of(rcu, | |
816 | struct rps_dev_flow_table, rcu); | |
243198d0 | 817 | vfree(table); |
fec5e652 TH |
818 | } |
819 | ||
f5acb907 | 820 | static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue, |
718ad681 | 821 | const char *buf, size_t len) |
fec5e652 | 822 | { |
60b778ce | 823 | unsigned long mask, count; |
fec5e652 TH |
824 | struct rps_dev_flow_table *table, *old_table; |
825 | static DEFINE_SPINLOCK(rps_dev_flow_lock); | |
60b778ce | 826 | int rc; |
fec5e652 TH |
827 | |
828 | if (!capable(CAP_NET_ADMIN)) | |
829 | return -EPERM; | |
830 | ||
60b778ce ED |
831 | rc = kstrtoul(buf, 0, &count); |
832 | if (rc < 0) | |
833 | return rc; | |
fec5e652 TH |
834 | |
835 | if (count) { | |
60b778ce ED |
836 | mask = count - 1; |
837 | /* mask = roundup_pow_of_two(count) - 1; | |
838 | * without overflows... | |
839 | */ | |
840 | while ((mask | (mask >> 1)) != mask) | |
841 | mask |= (mask >> 1); | |
842 | /* On 64 bit arches, must check mask fits in table->mask (u32), | |
8e3bff96 | 843 | * and on 32bit arches, must check |
844 | * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow. | |
60b778ce ED |
845 | */ |
846 | #if BITS_PER_LONG > 32 | |
847 | if (mask > (unsigned long)(u32)mask) | |
a0a129f8 | 848 | return -EINVAL; |
60b778ce ED |
849 | #else |
850 | if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1)) | |
a0a129f8 | 851 | / sizeof(struct rps_dev_flow)) { |
fec5e652 TH |
852 | /* Enforce a limit to prevent overflow */ |
853 | return -EINVAL; | |
854 | } | |
60b778ce ED |
855 | #endif |
856 | table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1)); | |
fec5e652 TH |
857 | if (!table) |
858 | return -ENOMEM; | |
859 | ||
60b778ce ED |
860 | table->mask = mask; |
861 | for (count = 0; count <= mask; count++) | |
862 | table->flows[count].cpu = RPS_NO_CPU; | |
6648c65e | 863 | } else { |
fec5e652 | 864 | table = NULL; |
6648c65e | 865 | } |
fec5e652 TH |
866 | |
867 | spin_lock(&rps_dev_flow_lock); | |
6e3f7faf ED |
868 | old_table = rcu_dereference_protected(queue->rps_flow_table, |
869 | lockdep_is_held(&rps_dev_flow_lock)); | |
fec5e652 TH |
870 | rcu_assign_pointer(queue->rps_flow_table, table); |
871 | spin_unlock(&rps_dev_flow_lock); | |
872 | ||
873 | if (old_table) | |
874 | call_rcu(&old_table->rcu, rps_dev_flow_table_release); | |
875 | ||
876 | return len; | |
877 | } | |
878 | ||
667e427b | 879 | static struct rx_queue_attribute rps_cpus_attribute __ro_after_init |
880 | = __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map); | |
0a9627f2 | 881 | |
667e427b | 882 | static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init |
883 | = __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR, | |
884 | show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt); | |
a953be53 | 885 | #endif /* CONFIG_RPS */ |
fec5e652 | 886 | |
667e427b | 887 | static struct attribute *rx_queue_default_attrs[] __ro_after_init = { |
a953be53 | 888 | #ifdef CONFIG_RPS |
0a9627f2 | 889 | &rps_cpus_attribute.attr, |
fec5e652 | 890 | &rps_dev_flow_table_cnt_attribute.attr, |
a953be53 | 891 | #endif |
0a9627f2 TH |
892 | NULL |
893 | }; | |
894 | ||
895 | static void rx_queue_release(struct kobject *kobj) | |
896 | { | |
897 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
a953be53 | 898 | #ifdef CONFIG_RPS |
6e3f7faf ED |
899 | struct rps_map *map; |
900 | struct rps_dev_flow_table *flow_table; | |
0a9627f2 | 901 | |
33d480ce | 902 | map = rcu_dereference_protected(queue->rps_map, 1); |
9ea19481 JF |
903 | if (map) { |
904 | RCU_INIT_POINTER(queue->rps_map, NULL); | |
f6f80238 | 905 | kfree_rcu(map, rcu); |
9ea19481 | 906 | } |
6e3f7faf | 907 | |
33d480ce | 908 | flow_table = rcu_dereference_protected(queue->rps_flow_table, 1); |
9ea19481 JF |
909 | if (flow_table) { |
910 | RCU_INIT_POINTER(queue->rps_flow_table, NULL); | |
6e3f7faf | 911 | call_rcu(&flow_table->rcu, rps_dev_flow_table_release); |
9ea19481 | 912 | } |
a953be53 | 913 | #endif |
0a9627f2 | 914 | |
9ea19481 | 915 | memset(kobj, 0, sizeof(*kobj)); |
fe822240 | 916 | dev_put(queue->dev); |
0a9627f2 TH |
917 | } |
918 | ||
82ef3d5d WC |
919 | static const void *rx_queue_namespace(struct kobject *kobj) |
920 | { | |
921 | struct netdev_rx_queue *queue = to_rx_queue(kobj); | |
922 | struct device *dev = &queue->dev->dev; | |
923 | const void *ns = NULL; | |
924 | ||
925 | if (dev->class && dev->class->ns_type) | |
926 | ns = dev->class->namespace(dev); | |
927 | ||
928 | return ns; | |
929 | } | |
930 | ||
667e427b | 931 | static struct kobj_type rx_queue_ktype __ro_after_init = { |
0a9627f2 TH |
932 | .sysfs_ops = &rx_queue_sysfs_ops, |
933 | .release = rx_queue_release, | |
934 | .default_attrs = rx_queue_default_attrs, | |
82ef3d5d | 935 | .namespace = rx_queue_namespace |
0a9627f2 TH |
936 | }; |
937 | ||
6b53dafe | 938 | static int rx_queue_add_kobject(struct net_device *dev, int index) |
0a9627f2 | 939 | { |
6b53dafe | 940 | struct netdev_rx_queue *queue = dev->_rx + index; |
0a9627f2 TH |
941 | struct kobject *kobj = &queue->kobj; |
942 | int error = 0; | |
943 | ||
6b53dafe | 944 | kobj->kset = dev->queues_kset; |
0a9627f2 | 945 | error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL, |
6648c65e | 946 | "rx-%u", index); |
a953be53 | 947 | if (error) |
d0d66837 | 948 | return error; |
a953be53 | 949 | |
6b53dafe WC |
950 | if (dev->sysfs_rx_queue_group) { |
951 | error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group); | |
d0d66837 | 952 | if (error) { |
953 | kobject_put(kobj); | |
954 | return error; | |
955 | } | |
0a9627f2 TH |
956 | } |
957 | ||
958 | kobject_uevent(kobj, KOBJ_ADD); | |
fe822240 | 959 | dev_hold(queue->dev); |
0a9627f2 TH |
960 | |
961 | return error; | |
962 | } | |
80dd6eac | 963 | #endif /* CONFIG_SYSFS */ |
0a9627f2 | 964 | |
62fe0b40 | 965 | int |
6b53dafe | 966 | net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) |
0a9627f2 | 967 | { |
a953be53 | 968 | #ifdef CONFIG_SYSFS |
0a9627f2 TH |
969 | int i; |
970 | int error = 0; | |
971 | ||
a953be53 | 972 | #ifndef CONFIG_RPS |
6b53dafe | 973 | if (!dev->sysfs_rx_queue_group) |
a953be53 MD |
974 | return 0; |
975 | #endif | |
62fe0b40 | 976 | for (i = old_num; i < new_num; i++) { |
6b53dafe | 977 | error = rx_queue_add_kobject(dev, i); |
62fe0b40 BH |
978 | if (error) { |
979 | new_num = old_num; | |
0a9627f2 | 980 | break; |
62fe0b40 | 981 | } |
0a9627f2 TH |
982 | } |
983 | ||
a953be53 | 984 | while (--i >= new_num) { |
002d8a1a AV |
985 | struct kobject *kobj = &dev->_rx[i].kobj; |
986 | ||
273c28bc | 987 | if (!refcount_read(&dev_net(dev)->count)) |
002d8a1a | 988 | kobj->uevent_suppress = 1; |
6b53dafe | 989 | if (dev->sysfs_rx_queue_group) |
002d8a1a AV |
990 | sysfs_remove_group(kobj, dev->sysfs_rx_queue_group); |
991 | kobject_put(kobj); | |
a953be53 | 992 | } |
0a9627f2 TH |
993 | |
994 | return error; | |
bf264145 TH |
995 | #else |
996 | return 0; | |
997 | #endif | |
0a9627f2 TH |
998 | } |
999 | ||
ccf5ff69 | 1000 | #ifdef CONFIG_SYSFS |
1d24eb48 TH |
1001 | /* |
1002 | * netdev_queue sysfs structures and functions. | |
1003 | */ | |
1004 | struct netdev_queue_attribute { | |
1005 | struct attribute attr; | |
718ad681 | 1006 | ssize_t (*show)(struct netdev_queue *queue, char *buf); |
1d24eb48 | 1007 | ssize_t (*store)(struct netdev_queue *queue, |
718ad681 | 1008 | const char *buf, size_t len); |
1d24eb48 | 1009 | }; |
6648c65e | 1010 | #define to_netdev_queue_attr(_attr) \ |
1011 | container_of(_attr, struct netdev_queue_attribute, attr) | |
1d24eb48 TH |
1012 | |
1013 | #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj) | |
1014 | ||
1015 | static ssize_t netdev_queue_attr_show(struct kobject *kobj, | |
1016 | struct attribute *attr, char *buf) | |
1017 | { | |
667e427b | 1018 | const struct netdev_queue_attribute *attribute |
1019 | = to_netdev_queue_attr(attr); | |
1d24eb48 TH |
1020 | struct netdev_queue *queue = to_netdev_queue(kobj); |
1021 | ||
1022 | if (!attribute->show) | |
1023 | return -EIO; | |
1024 | ||
718ad681 | 1025 | return attribute->show(queue, buf); |
1d24eb48 TH |
1026 | } |
1027 | ||
1028 | static ssize_t netdev_queue_attr_store(struct kobject *kobj, | |
1029 | struct attribute *attr, | |
1030 | const char *buf, size_t count) | |
1031 | { | |
667e427b | 1032 | const struct netdev_queue_attribute *attribute |
1033 | = to_netdev_queue_attr(attr); | |
1d24eb48 TH |
1034 | struct netdev_queue *queue = to_netdev_queue(kobj); |
1035 | ||
1036 | if (!attribute->store) | |
1037 | return -EIO; | |
1038 | ||
718ad681 | 1039 | return attribute->store(queue, buf, count); |
1d24eb48 TH |
1040 | } |
1041 | ||
1042 | static const struct sysfs_ops netdev_queue_sysfs_ops = { | |
1043 | .show = netdev_queue_attr_show, | |
1044 | .store = netdev_queue_attr_store, | |
1045 | }; | |
1046 | ||
2b9c7581 | 1047 | static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf) |
ccf5ff69 | 1048 | { |
1049 | unsigned long trans_timeout; | |
1050 | ||
1051 | spin_lock_irq(&queue->_xmit_lock); | |
1052 | trans_timeout = queue->trans_timeout; | |
1053 | spin_unlock_irq(&queue->_xmit_lock); | |
1054 | ||
1055 | return sprintf(buf, "%lu", trans_timeout); | |
1056 | } | |
1057 | ||
c4047f53 | 1058 | static unsigned int get_netdev_queue_index(struct netdev_queue *queue) |
822b3b2e JF |
1059 | { |
1060 | struct net_device *dev = queue->dev; | |
c4047f53 | 1061 | unsigned int i; |
822b3b2e | 1062 | |
c4047f53 | 1063 | i = queue - dev->_tx; |
822b3b2e JF |
1064 | BUG_ON(i >= dev->num_tx_queues); |
1065 | ||
1066 | return i; | |
1067 | } | |
1068 | ||
2b9c7581 | 1069 | static ssize_t traffic_class_show(struct netdev_queue *queue, |
8d059b0f AD |
1070 | char *buf) |
1071 | { | |
1072 | struct net_device *dev = queue->dev; | |
1073 | int index = get_netdev_queue_index(queue); | |
1074 | int tc = netdev_txq_to_tc(dev, index); | |
1075 | ||
1076 | if (tc < 0) | |
1077 | return -EINVAL; | |
1078 | ||
1079 | return sprintf(buf, "%u\n", tc); | |
1080 | } | |
1081 | ||
1082 | #ifdef CONFIG_XPS | |
2b9c7581 | 1083 | static ssize_t tx_maxrate_show(struct netdev_queue *queue, |
822b3b2e JF |
1084 | char *buf) |
1085 | { | |
1086 | return sprintf(buf, "%lu\n", queue->tx_maxrate); | |
1087 | } | |
1088 | ||
2b9c7581 | 1089 | static ssize_t tx_maxrate_store(struct netdev_queue *queue, |
1090 | const char *buf, size_t len) | |
822b3b2e JF |
1091 | { |
1092 | struct net_device *dev = queue->dev; | |
1093 | int err, index = get_netdev_queue_index(queue); | |
1094 | u32 rate = 0; | |
1095 | ||
1096 | err = kstrtou32(buf, 10, &rate); | |
1097 | if (err < 0) | |
1098 | return err; | |
1099 | ||
1100 | if (!rtnl_trylock()) | |
1101 | return restart_syscall(); | |
1102 | ||
1103 | err = -EOPNOTSUPP; | |
1104 | if (dev->netdev_ops->ndo_set_tx_maxrate) | |
1105 | err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate); | |
1106 | ||
1107 | rtnl_unlock(); | |
1108 | if (!err) { | |
1109 | queue->tx_maxrate = rate; | |
1110 | return len; | |
1111 | } | |
1112 | return err; | |
1113 | } | |
1114 | ||
2b9c7581 | 1115 | static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init |
1116 | = __ATTR_RW(tx_maxrate); | |
822b3b2e JF |
1117 | #endif |
1118 | ||
2b9c7581 | 1119 | static struct netdev_queue_attribute queue_trans_timeout __ro_after_init |
1120 | = __ATTR_RO(tx_timeout); | |
ccf5ff69 | 1121 | |
2b9c7581 | 1122 | static struct netdev_queue_attribute queue_traffic_class __ro_after_init |
1123 | = __ATTR_RO(traffic_class); | |
8d059b0f | 1124 | |
114cf580 TH |
1125 | #ifdef CONFIG_BQL |
1126 | /* | |
1127 | * Byte queue limits sysfs structures and functions. | |
1128 | */ | |
1129 | static ssize_t bql_show(char *buf, unsigned int value) | |
1130 | { | |
1131 | return sprintf(buf, "%u\n", value); | |
1132 | } | |
1133 | ||
1134 | static ssize_t bql_set(const char *buf, const size_t count, | |
1135 | unsigned int *pvalue) | |
1136 | { | |
1137 | unsigned int value; | |
1138 | int err; | |
1139 | ||
6648c65e | 1140 | if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) { |
114cf580 | 1141 | value = DQL_MAX_LIMIT; |
6648c65e | 1142 | } else { |
114cf580 TH |
1143 | err = kstrtouint(buf, 10, &value); |
1144 | if (err < 0) | |
1145 | return err; | |
1146 | if (value > DQL_MAX_LIMIT) | |
1147 | return -EINVAL; | |
1148 | } | |
1149 | ||
1150 | *pvalue = value; | |
1151 | ||
1152 | return count; | |
1153 | } | |
1154 | ||
1155 | static ssize_t bql_show_hold_time(struct netdev_queue *queue, | |
114cf580 TH |
1156 | char *buf) |
1157 | { | |
1158 | struct dql *dql = &queue->dql; | |
1159 | ||
1160 | return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time)); | |
1161 | } | |
1162 | ||
1163 | static ssize_t bql_set_hold_time(struct netdev_queue *queue, | |
114cf580 TH |
1164 | const char *buf, size_t len) |
1165 | { | |
1166 | struct dql *dql = &queue->dql; | |
95c96174 | 1167 | unsigned int value; |
114cf580 TH |
1168 | int err; |
1169 | ||
1170 | err = kstrtouint(buf, 10, &value); | |
1171 | if (err < 0) | |
1172 | return err; | |
1173 | ||
1174 | dql->slack_hold_time = msecs_to_jiffies(value); | |
1175 | ||
1176 | return len; | |
1177 | } | |
1178 | ||
170c658a | 1179 | static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init |
1180 | = __ATTR(hold_time, S_IRUGO | S_IWUSR, | |
1181 | bql_show_hold_time, bql_set_hold_time); | |
114cf580 TH |
1182 | |
1183 | static ssize_t bql_show_inflight(struct netdev_queue *queue, | |
114cf580 TH |
1184 | char *buf) |
1185 | { | |
1186 | struct dql *dql = &queue->dql; | |
1187 | ||
1188 | return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed); | |
1189 | } | |
1190 | ||
170c658a | 1191 | static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init = |
795d9a25 | 1192 | __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL); |
114cf580 TH |
1193 | |
1194 | #define BQL_ATTR(NAME, FIELD) \ | |
1195 | static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \ | |
114cf580 TH |
1196 | char *buf) \ |
1197 | { \ | |
1198 | return bql_show(buf, queue->dql.FIELD); \ | |
1199 | } \ | |
1200 | \ | |
1201 | static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \ | |
114cf580 TH |
1202 | const char *buf, size_t len) \ |
1203 | { \ | |
1204 | return bql_set(buf, len, &queue->dql.FIELD); \ | |
1205 | } \ | |
1206 | \ | |
170c658a | 1207 | static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \ |
1208 | = __ATTR(NAME, S_IRUGO | S_IWUSR, \ | |
1209 | bql_show_ ## NAME, bql_set_ ## NAME) | |
114cf580 | 1210 | |
170c658a | 1211 | BQL_ATTR(limit, limit); |
1212 | BQL_ATTR(limit_max, max_limit); | |
1213 | BQL_ATTR(limit_min, min_limit); | |
114cf580 | 1214 | |
170c658a | 1215 | static struct attribute *dql_attrs[] __ro_after_init = { |
114cf580 TH |
1216 | &bql_limit_attribute.attr, |
1217 | &bql_limit_max_attribute.attr, | |
1218 | &bql_limit_min_attribute.attr, | |
1219 | &bql_hold_time_attribute.attr, | |
1220 | &bql_inflight_attribute.attr, | |
1221 | NULL | |
1222 | }; | |
1223 | ||
38ef00cc | 1224 | static const struct attribute_group dql_group = { |
114cf580 TH |
1225 | .name = "byte_queue_limits", |
1226 | .attrs = dql_attrs, | |
1227 | }; | |
1228 | #endif /* CONFIG_BQL */ | |
1229 | ||
ccf5ff69 | 1230 | #ifdef CONFIG_XPS |
2b9c7581 | 1231 | static ssize_t xps_cpus_show(struct netdev_queue *queue, |
1232 | char *buf) | |
1d24eb48 TH |
1233 | { |
1234 | struct net_device *dev = queue->dev; | |
184c449f | 1235 | int cpu, len, num_tc = 1, tc = 0; |
1d24eb48 TH |
1236 | struct xps_dev_maps *dev_maps; |
1237 | cpumask_var_t mask; | |
1238 | unsigned long index; | |
1d24eb48 TH |
1239 | |
1240 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) | |
1241 | return -ENOMEM; | |
1242 | ||
1243 | index = get_netdev_queue_index(queue); | |
1244 | ||
184c449f AD |
1245 | if (dev->num_tc) { |
1246 | num_tc = dev->num_tc; | |
1247 | tc = netdev_txq_to_tc(dev, index); | |
1248 | if (tc < 0) | |
1249 | return -EINVAL; | |
1250 | } | |
1251 | ||
1d24eb48 TH |
1252 | rcu_read_lock(); |
1253 | dev_maps = rcu_dereference(dev->xps_maps); | |
1254 | if (dev_maps) { | |
184c449f AD |
1255 | for_each_possible_cpu(cpu) { |
1256 | int i, tci = cpu * num_tc + tc; | |
1257 | struct xps_map *map; | |
1258 | ||
1259 | map = rcu_dereference(dev_maps->cpu_map[tci]); | |
1260 | if (!map) | |
1261 | continue; | |
1262 | ||
1263 | for (i = map->len; i--;) { | |
1264 | if (map->queues[i] == index) { | |
1265 | cpumask_set_cpu(cpu, mask); | |
1266 | break; | |
1d24eb48 TH |
1267 | } |
1268 | } | |
1269 | } | |
1270 | } | |
1271 | rcu_read_unlock(); | |
1272 | ||
f0906827 | 1273 | len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); |
1d24eb48 | 1274 | free_cpumask_var(mask); |
f0906827 | 1275 | return len < PAGE_SIZE ? len : -EINVAL; |
1d24eb48 TH |
1276 | } |
1277 | ||
2b9c7581 | 1278 | static ssize_t xps_cpus_store(struct netdev_queue *queue, |
1279 | const char *buf, size_t len) | |
1d24eb48 TH |
1280 | { |
1281 | struct net_device *dev = queue->dev; | |
1d24eb48 | 1282 | unsigned long index; |
537c00de AD |
1283 | cpumask_var_t mask; |
1284 | int err; | |
1d24eb48 TH |
1285 | |
1286 | if (!capable(CAP_NET_ADMIN)) | |
1287 | return -EPERM; | |
1288 | ||
1289 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
1290 | return -ENOMEM; | |
1291 | ||
1292 | index = get_netdev_queue_index(queue); | |
1293 | ||
1294 | err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits); | |
1295 | if (err) { | |
1296 | free_cpumask_var(mask); | |
1297 | return err; | |
1298 | } | |
1299 | ||
537c00de | 1300 | err = netif_set_xps_queue(dev, mask, index); |
1d24eb48 TH |
1301 | |
1302 | free_cpumask_var(mask); | |
1d24eb48 | 1303 | |
537c00de | 1304 | return err ? : len; |
1d24eb48 TH |
1305 | } |
1306 | ||
2b9c7581 | 1307 | static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init |
1308 | = __ATTR_RW(xps_cpus); | |
ccf5ff69 | 1309 | #endif /* CONFIG_XPS */ |
1d24eb48 | 1310 | |
2b9c7581 | 1311 | static struct attribute *netdev_queue_default_attrs[] __ro_after_init = { |
ccf5ff69 | 1312 | &queue_trans_timeout.attr, |
8d059b0f | 1313 | &queue_traffic_class.attr, |
ccf5ff69 | 1314 | #ifdef CONFIG_XPS |
1d24eb48 | 1315 | &xps_cpus_attribute.attr, |
822b3b2e | 1316 | &queue_tx_maxrate.attr, |
ccf5ff69 | 1317 | #endif |
1d24eb48 TH |
1318 | NULL |
1319 | }; | |
1320 | ||
1321 | static void netdev_queue_release(struct kobject *kobj) | |
1322 | { | |
1323 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
1d24eb48 | 1324 | |
1d24eb48 TH |
1325 | memset(kobj, 0, sizeof(*kobj)); |
1326 | dev_put(queue->dev); | |
1327 | } | |
1328 | ||
82ef3d5d WC |
1329 | static const void *netdev_queue_namespace(struct kobject *kobj) |
1330 | { | |
1331 | struct netdev_queue *queue = to_netdev_queue(kobj); | |
1332 | struct device *dev = &queue->dev->dev; | |
1333 | const void *ns = NULL; | |
1334 | ||
1335 | if (dev->class && dev->class->ns_type) | |
1336 | ns = dev->class->namespace(dev); | |
1337 | ||
1338 | return ns; | |
1339 | } | |
1340 | ||
2b9c7581 | 1341 | static struct kobj_type netdev_queue_ktype __ro_after_init = { |
1d24eb48 TH |
1342 | .sysfs_ops = &netdev_queue_sysfs_ops, |
1343 | .release = netdev_queue_release, | |
1344 | .default_attrs = netdev_queue_default_attrs, | |
82ef3d5d | 1345 | .namespace = netdev_queue_namespace, |
1d24eb48 TH |
1346 | }; |
1347 | ||
6b53dafe | 1348 | static int netdev_queue_add_kobject(struct net_device *dev, int index) |
1d24eb48 | 1349 | { |
6b53dafe | 1350 | struct netdev_queue *queue = dev->_tx + index; |
1d24eb48 TH |
1351 | struct kobject *kobj = &queue->kobj; |
1352 | int error = 0; | |
1353 | ||
6b53dafe | 1354 | kobj->kset = dev->queues_kset; |
1d24eb48 | 1355 | error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL, |
6648c65e | 1356 | "tx-%u", index); |
114cf580 | 1357 | if (error) |
d0d66837 | 1358 | return error; |
114cf580 TH |
1359 | |
1360 | #ifdef CONFIG_BQL | |
1361 | error = sysfs_create_group(kobj, &dql_group); | |
d0d66837 | 1362 | if (error) { |
1363 | kobject_put(kobj); | |
1364 | return error; | |
1365 | } | |
114cf580 | 1366 | #endif |
1d24eb48 TH |
1367 | |
1368 | kobject_uevent(kobj, KOBJ_ADD); | |
1369 | dev_hold(queue->dev); | |
1370 | ||
114cf580 | 1371 | return 0; |
1d24eb48 | 1372 | } |
ccf5ff69 | 1373 | #endif /* CONFIG_SYSFS */ |
1d24eb48 TH |
1374 | |
1375 | int | |
6b53dafe | 1376 | netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) |
1d24eb48 | 1377 | { |
ccf5ff69 | 1378 | #ifdef CONFIG_SYSFS |
1d24eb48 TH |
1379 | int i; |
1380 | int error = 0; | |
1381 | ||
1382 | for (i = old_num; i < new_num; i++) { | |
6b53dafe | 1383 | error = netdev_queue_add_kobject(dev, i); |
1d24eb48 TH |
1384 | if (error) { |
1385 | new_num = old_num; | |
1386 | break; | |
1387 | } | |
1388 | } | |
1389 | ||
114cf580 | 1390 | while (--i >= new_num) { |
6b53dafe | 1391 | struct netdev_queue *queue = dev->_tx + i; |
114cf580 | 1392 | |
273c28bc | 1393 | if (!refcount_read(&dev_net(dev)->count)) |
002d8a1a | 1394 | queue->kobj.uevent_suppress = 1; |
114cf580 TH |
1395 | #ifdef CONFIG_BQL |
1396 | sysfs_remove_group(&queue->kobj, &dql_group); | |
1397 | #endif | |
1398 | kobject_put(&queue->kobj); | |
1399 | } | |
1d24eb48 TH |
1400 | |
1401 | return error; | |
bf264145 TH |
1402 | #else |
1403 | return 0; | |
ccf5ff69 | 1404 | #endif /* CONFIG_SYSFS */ |
1d24eb48 TH |
1405 | } |
1406 | ||
6b53dafe | 1407 | static int register_queue_kobjects(struct net_device *dev) |
1d24eb48 | 1408 | { |
bf264145 | 1409 | int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; |
1d24eb48 | 1410 | |
ccf5ff69 | 1411 | #ifdef CONFIG_SYSFS |
6b53dafe | 1412 | dev->queues_kset = kset_create_and_add("queues", |
6648c65e | 1413 | NULL, &dev->dev.kobj); |
6b53dafe | 1414 | if (!dev->queues_kset) |
62fe0b40 | 1415 | return -ENOMEM; |
6b53dafe | 1416 | real_rx = dev->real_num_rx_queues; |
bf264145 | 1417 | #endif |
6b53dafe | 1418 | real_tx = dev->real_num_tx_queues; |
1d24eb48 | 1419 | |
6b53dafe | 1420 | error = net_rx_queue_update_kobjects(dev, 0, real_rx); |
1d24eb48 TH |
1421 | if (error) |
1422 | goto error; | |
bf264145 | 1423 | rxq = real_rx; |
1d24eb48 | 1424 | |
6b53dafe | 1425 | error = netdev_queue_update_kobjects(dev, 0, real_tx); |
1d24eb48 TH |
1426 | if (error) |
1427 | goto error; | |
bf264145 | 1428 | txq = real_tx; |
1d24eb48 TH |
1429 | |
1430 | return 0; | |
1431 | ||
1432 | error: | |
6b53dafe WC |
1433 | netdev_queue_update_kobjects(dev, txq, 0); |
1434 | net_rx_queue_update_kobjects(dev, rxq, 0); | |
1d24eb48 | 1435 | return error; |
62fe0b40 | 1436 | } |
0a9627f2 | 1437 | |
6b53dafe | 1438 | static void remove_queue_kobjects(struct net_device *dev) |
62fe0b40 | 1439 | { |
bf264145 TH |
1440 | int real_rx = 0, real_tx = 0; |
1441 | ||
a953be53 | 1442 | #ifdef CONFIG_SYSFS |
6b53dafe | 1443 | real_rx = dev->real_num_rx_queues; |
bf264145 | 1444 | #endif |
6b53dafe | 1445 | real_tx = dev->real_num_tx_queues; |
bf264145 | 1446 | |
6b53dafe WC |
1447 | net_rx_queue_update_kobjects(dev, real_rx, 0); |
1448 | netdev_queue_update_kobjects(dev, real_tx, 0); | |
ccf5ff69 | 1449 | #ifdef CONFIG_SYSFS |
6b53dafe | 1450 | kset_unregister(dev->queues_kset); |
bf264145 | 1451 | #endif |
0a9627f2 | 1452 | } |
608b4b95 | 1453 | |
7dc5dbc8 EB |
1454 | static bool net_current_may_mount(void) |
1455 | { | |
1456 | struct net *net = current->nsproxy->net_ns; | |
1457 | ||
1458 | return ns_capable(net->user_ns, CAP_SYS_ADMIN); | |
1459 | } | |
1460 | ||
a685e089 | 1461 | static void *net_grab_current_ns(void) |
608b4b95 | 1462 | { |
a685e089 AV |
1463 | struct net *ns = current->nsproxy->net_ns; |
1464 | #ifdef CONFIG_NET_NS | |
1465 | if (ns) | |
c122e14d | 1466 | refcount_inc(&ns->passive); |
a685e089 AV |
1467 | #endif |
1468 | return ns; | |
608b4b95 EB |
1469 | } |
1470 | ||
1471 | static const void *net_initial_ns(void) | |
1472 | { | |
1473 | return &init_net; | |
1474 | } | |
1475 | ||
1476 | static const void *net_netlink_ns(struct sock *sk) | |
1477 | { | |
1478 | return sock_net(sk); | |
1479 | } | |
1480 | ||
737aec57 | 1481 | const struct kobj_ns_type_operations net_ns_type_operations = { |
608b4b95 | 1482 | .type = KOBJ_NS_TYPE_NET, |
7dc5dbc8 | 1483 | .current_may_mount = net_current_may_mount, |
a685e089 | 1484 | .grab_current_ns = net_grab_current_ns, |
608b4b95 EB |
1485 | .netlink_ns = net_netlink_ns, |
1486 | .initial_ns = net_initial_ns, | |
a685e089 | 1487 | .drop_ns = net_drop_ns, |
608b4b95 | 1488 | }; |
04600794 | 1489 | EXPORT_SYMBOL_GPL(net_ns_type_operations); |
608b4b95 | 1490 | |
7eff2e7a | 1491 | static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) |
1da177e4 | 1492 | { |
43cb76d9 | 1493 | struct net_device *dev = to_net_dev(d); |
7eff2e7a | 1494 | int retval; |
1da177e4 | 1495 | |
312c004d | 1496 | /* pass interface to uevent. */ |
7eff2e7a | 1497 | retval = add_uevent_var(env, "INTERFACE=%s", dev->name); |
bf62456e ER |
1498 | if (retval) |
1499 | goto exit; | |
ca2f37db JT |
1500 | |
1501 | /* pass ifindex to uevent. | |
1502 | * ifindex is useful as it won't change (interface name may change) | |
6648c65e | 1503 | * and is what RtNetlink uses natively. |
1504 | */ | |
7eff2e7a | 1505 | retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex); |
1da177e4 | 1506 | |
bf62456e | 1507 | exit: |
bf62456e | 1508 | return retval; |
1da177e4 | 1509 | } |
1da177e4 LT |
1510 | |
1511 | /* | |
4ec93edb | 1512 | * netdev_release -- destroy and free a dead device. |
43cb76d9 | 1513 | * Called when last reference to device kobject is gone. |
1da177e4 | 1514 | */ |
43cb76d9 | 1515 | static void netdev_release(struct device *d) |
1da177e4 | 1516 | { |
43cb76d9 | 1517 | struct net_device *dev = to_net_dev(d); |
1da177e4 LT |
1518 | |
1519 | BUG_ON(dev->reg_state != NETREG_RELEASED); | |
1520 | ||
6c557001 FW |
1521 | /* no need to wait for rcu grace period: |
1522 | * device is dead and about to be freed. | |
1523 | */ | |
1524 | kfree(rcu_access_pointer(dev->ifalias)); | |
74d332c1 | 1525 | netdev_freemem(dev); |
1da177e4 LT |
1526 | } |
1527 | ||
608b4b95 EB |
1528 | static const void *net_namespace(struct device *d) |
1529 | { | |
5c29482d GT |
1530 | struct net_device *dev = to_net_dev(d); |
1531 | ||
608b4b95 EB |
1532 | return dev_net(dev); |
1533 | } | |
1534 | ||
e6d473e6 | 1535 | static struct class net_class __ro_after_init = { |
1da177e4 | 1536 | .name = "net", |
43cb76d9 | 1537 | .dev_release = netdev_release, |
6be8aeef | 1538 | .dev_groups = net_class_groups, |
43cb76d9 | 1539 | .dev_uevent = netdev_uevent, |
608b4b95 EB |
1540 | .ns_type = &net_ns_type_operations, |
1541 | .namespace = net_namespace, | |
1da177e4 LT |
1542 | }; |
1543 | ||
aa836df9 FF |
1544 | #ifdef CONFIG_OF_NET |
1545 | static int of_dev_node_match(struct device *dev, const void *data) | |
1546 | { | |
1547 | int ret = 0; | |
1548 | ||
1549 | if (dev->parent) | |
1550 | ret = dev->parent->of_node == data; | |
1551 | ||
1552 | return ret == 0 ? dev->of_node == data : ret; | |
1553 | } | |
1554 | ||
9861f720 RK |
1555 | /* |
1556 | * of_find_net_device_by_node - lookup the net device for the device node | |
1557 | * @np: OF device node | |
1558 | * | |
1559 | * Looks up the net_device structure corresponding with the device node. | |
1560 | * If successful, returns a pointer to the net_device with the embedded | |
1561 | * struct device refcount incremented by one, or NULL on failure. The | |
1562 | * refcount must be dropped when done with the net_device. | |
1563 | */ | |
aa836df9 FF |
1564 | struct net_device *of_find_net_device_by_node(struct device_node *np) |
1565 | { | |
1566 | struct device *dev; | |
1567 | ||
1568 | dev = class_find_device(&net_class, NULL, np, of_dev_node_match); | |
1569 | if (!dev) | |
1570 | return NULL; | |
1571 | ||
1572 | return to_net_dev(dev); | |
1573 | } | |
1574 | EXPORT_SYMBOL(of_find_net_device_by_node); | |
1575 | #endif | |
1576 | ||
9093bbb2 SH |
1577 | /* Delete sysfs entries but hold kobject reference until after all |
1578 | * netdev references are gone. | |
1579 | */ | |
6b53dafe | 1580 | void netdev_unregister_kobject(struct net_device *ndev) |
1da177e4 | 1581 | { |
6648c65e | 1582 | struct device *dev = &ndev->dev; |
9093bbb2 | 1583 | |
273c28bc | 1584 | if (!refcount_read(&dev_net(ndev)->count)) |
002d8a1a AV |
1585 | dev_set_uevent_suppress(dev, 1); |
1586 | ||
9093bbb2 | 1587 | kobject_get(&dev->kobj); |
3891845e | 1588 | |
6b53dafe | 1589 | remove_queue_kobjects(ndev); |
0a9627f2 | 1590 | |
9802c8e2 ML |
1591 | pm_runtime_set_memalloc_noio(dev, false); |
1592 | ||
9093bbb2 | 1593 | device_del(dev); |
1da177e4 LT |
1594 | } |
1595 | ||
1596 | /* Create sysfs entries for network device. */ | |
6b53dafe | 1597 | int netdev_register_kobject(struct net_device *ndev) |
1da177e4 | 1598 | { |
6648c65e | 1599 | struct device *dev = &ndev->dev; |
6b53dafe | 1600 | const struct attribute_group **groups = ndev->sysfs_groups; |
0a9627f2 | 1601 | int error = 0; |
1da177e4 | 1602 | |
a1b3f594 | 1603 | device_initialize(dev); |
43cb76d9 | 1604 | dev->class = &net_class; |
6b53dafe | 1605 | dev->platform_data = ndev; |
43cb76d9 | 1606 | dev->groups = groups; |
1da177e4 | 1607 | |
6b53dafe | 1608 | dev_set_name(dev, "%s", ndev->name); |
1da177e4 | 1609 | |
8b41d188 | 1610 | #ifdef CONFIG_SYSFS |
0c509a6c EB |
1611 | /* Allow for a device specific group */ |
1612 | if (*groups) | |
1613 | groups++; | |
1da177e4 | 1614 | |
0c509a6c | 1615 | *groups++ = &netstat_group; |
38c1a01c JB |
1616 | |
1617 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211) | |
6b53dafe | 1618 | if (ndev->ieee80211_ptr) |
38c1a01c JB |
1619 | *groups++ = &wireless_group; |
1620 | #if IS_ENABLED(CONFIG_WIRELESS_EXT) | |
6b53dafe | 1621 | else if (ndev->wireless_handlers) |
38c1a01c JB |
1622 | *groups++ = &wireless_group; |
1623 | #endif | |
1624 | #endif | |
8b41d188 | 1625 | #endif /* CONFIG_SYSFS */ |
1da177e4 | 1626 | |
0a9627f2 TH |
1627 | error = device_add(dev); |
1628 | if (error) | |
1629 | return error; | |
1630 | ||
6b53dafe | 1631 | error = register_queue_kobjects(ndev); |
0a9627f2 TH |
1632 | if (error) { |
1633 | device_del(dev); | |
1634 | return error; | |
1635 | } | |
1636 | ||
9802c8e2 ML |
1637 | pm_runtime_set_memalloc_noio(dev, true); |
1638 | ||
0a9627f2 | 1639 | return error; |
1da177e4 LT |
1640 | } |
1641 | ||
b793dc5c | 1642 | int netdev_class_create_file_ns(const struct class_attribute *class_attr, |
58292cbe | 1643 | const void *ns) |
b8a9787e | 1644 | { |
58292cbe | 1645 | return class_create_file_ns(&net_class, class_attr, ns); |
b8a9787e | 1646 | } |
58292cbe | 1647 | EXPORT_SYMBOL(netdev_class_create_file_ns); |
b8a9787e | 1648 | |
b793dc5c | 1649 | void netdev_class_remove_file_ns(const struct class_attribute *class_attr, |
58292cbe | 1650 | const void *ns) |
b8a9787e | 1651 | { |
58292cbe | 1652 | class_remove_file_ns(&net_class, class_attr, ns); |
b8a9787e | 1653 | } |
58292cbe | 1654 | EXPORT_SYMBOL(netdev_class_remove_file_ns); |
b8a9787e | 1655 | |
a48d4bb0 | 1656 | int __init netdev_kobject_init(void) |
1da177e4 | 1657 | { |
608b4b95 | 1658 | kobj_ns_type_register(&net_ns_type_operations); |
1da177e4 LT |
1659 | return class_register(&net_class); |
1660 | } |