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