1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#include <linux/uaccess.h>
72#include <linux/bitops.h>
73#include <linux/capability.h>
74#include <linux/cpu.h>
75#include <linux/types.h>
76#include <linux/kernel.h>
77#include <linux/hash.h>
78#include <linux/slab.h>
79#include <linux/sched.h>
80#include <linux/sched/mm.h>
81#include <linux/mutex.h>
82#include <linux/rwsem.h>
83#include <linux/string.h>
84#include <linux/mm.h>
85#include <linux/socket.h>
86#include <linux/sockios.h>
87#include <linux/errno.h>
88#include <linux/interrupt.h>
89#include <linux/if_ether.h>
90#include <linux/netdevice.h>
91#include <linux/etherdevice.h>
92#include <linux/ethtool.h>
93#include <linux/skbuff.h>
94#include <linux/kthread.h>
95#include <linux/bpf.h>
96#include <linux/bpf_trace.h>
97#include <net/net_namespace.h>
98#include <net/sock.h>
99#include <net/busy_poll.h>
100#include <linux/rtnetlink.h>
101#include <linux/stat.h>
102#include <net/dsa.h>
103#include <net/dst.h>
104#include <net/dst_metadata.h>
105#include <net/gro.h>
106#include <net/pkt_sched.h>
107#include <net/pkt_cls.h>
108#include <net/checksum.h>
109#include <net/xfrm.h>
110#include <linux/highmem.h>
111#include <linux/init.h>
112#include <linux/module.h>
113#include <linux/netpoll.h>
114#include <linux/rcupdate.h>
115#include <linux/delay.h>
116#include <net/iw_handler.h>
117#include <asm/current.h>
118#include <linux/audit.h>
119#include <linux/dmaengine.h>
120#include <linux/err.h>
121#include <linux/ctype.h>
122#include <linux/if_arp.h>
123#include <linux/if_vlan.h>
124#include <linux/ip.h>
125#include <net/ip.h>
126#include <net/mpls.h>
127#include <linux/ipv6.h>
128#include <linux/in.h>
129#include <linux/jhash.h>
130#include <linux/random.h>
131#include <trace/events/napi.h>
132#include <trace/events/net.h>
133#include <trace/events/skb.h>
134#include <trace/events/qdisc.h>
135#include <linux/inetdevice.h>
136#include <linux/cpu_rmap.h>
137#include <linux/static_key.h>
138#include <linux/hashtable.h>
139#include <linux/vmalloc.h>
140#include <linux/if_macvlan.h>
141#include <linux/errqueue.h>
142#include <linux/hrtimer.h>
143#include <linux/netfilter_netdev.h>
144#include <linux/crash_dump.h>
145#include <linux/sctp.h>
146#include <net/udp_tunnel.h>
147#include <linux/net_namespace.h>
148#include <linux/indirect_call_wrapper.h>
149#include <net/devlink.h>
150#include <linux/pm_runtime.h>
151#include <linux/prandom.h>
152#include <linux/once_lite.h>
153
154#include "net-sysfs.h"
155
156
157static DEFINE_SPINLOCK(ptype_lock);
158struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
159struct list_head ptype_all __read_mostly;
160
161static int netif_rx_internal(struct sk_buff *skb);
162static int call_netdevice_notifiers_info(unsigned long val,
163 struct netdev_notifier_info *info);
164static int call_netdevice_notifiers_extack(unsigned long val,
165 struct net_device *dev,
166 struct netlink_ext_ack *extack);
167static struct napi_struct *napi_by_id(unsigned int napi_id);
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188DEFINE_RWLOCK(dev_base_lock);
189EXPORT_SYMBOL(dev_base_lock);
190
191static DEFINE_MUTEX(ifalias_mutex);
192
193
194static DEFINE_SPINLOCK(napi_hash_lock);
195
196static unsigned int napi_gen_id = NR_CPUS;
197static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
198
199static DECLARE_RWSEM(devnet_rename_sem);
200
201static inline void dev_base_seq_inc(struct net *net)
202{
203 while (++net->dev_base_seq == 0)
204 ;
205}
206
207static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
208{
209 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
210
211 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
212}
213
214static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
215{
216 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
217}
218
219static inline void rps_lock(struct softnet_data *sd)
220{
221#ifdef CONFIG_RPS
222 spin_lock(&sd->input_pkt_queue.lock);
223#endif
224}
225
226static inline void rps_unlock(struct softnet_data *sd)
227{
228#ifdef CONFIG_RPS
229 spin_unlock(&sd->input_pkt_queue.lock);
230#endif
231}
232
233static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
234 const char *name)
235{
236 struct netdev_name_node *name_node;
237
238 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
239 if (!name_node)
240 return NULL;
241 INIT_HLIST_NODE(&name_node->hlist);
242 name_node->dev = dev;
243 name_node->name = name;
244 return name_node;
245}
246
247static struct netdev_name_node *
248netdev_name_node_head_alloc(struct net_device *dev)
249{
250 struct netdev_name_node *name_node;
251
252 name_node = netdev_name_node_alloc(dev, dev->name);
253 if (!name_node)
254 return NULL;
255 INIT_LIST_HEAD(&name_node->list);
256 return name_node;
257}
258
259static void netdev_name_node_free(struct netdev_name_node *name_node)
260{
261 kfree(name_node);
262}
263
264static void netdev_name_node_add(struct net *net,
265 struct netdev_name_node *name_node)
266{
267 hlist_add_head_rcu(&name_node->hlist,
268 dev_name_hash(net, name_node->name));
269}
270
271static void netdev_name_node_del(struct netdev_name_node *name_node)
272{
273 hlist_del_rcu(&name_node->hlist);
274}
275
276static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
277 const char *name)
278{
279 struct hlist_head *head = dev_name_hash(net, name);
280 struct netdev_name_node *name_node;
281
282 hlist_for_each_entry(name_node, head, hlist)
283 if (!strcmp(name_node->name, name))
284 return name_node;
285 return NULL;
286}
287
288static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
289 const char *name)
290{
291 struct hlist_head *head = dev_name_hash(net, name);
292 struct netdev_name_node *name_node;
293
294 hlist_for_each_entry_rcu(name_node, head, hlist)
295 if (!strcmp(name_node->name, name))
296 return name_node;
297 return NULL;
298}
299
300bool netdev_name_in_use(struct net *net, const char *name)
301{
302 return netdev_name_node_lookup(net, name);
303}
304EXPORT_SYMBOL(netdev_name_in_use);
305
306int netdev_name_node_alt_create(struct net_device *dev, const char *name)
307{
308 struct netdev_name_node *name_node;
309 struct net *net = dev_net(dev);
310
311 name_node = netdev_name_node_lookup(net, name);
312 if (name_node)
313 return -EEXIST;
314 name_node = netdev_name_node_alloc(dev, name);
315 if (!name_node)
316 return -ENOMEM;
317 netdev_name_node_add(net, name_node);
318
319 list_add_tail(&name_node->list, &dev->name_node->list);
320
321 return 0;
322}
323EXPORT_SYMBOL(netdev_name_node_alt_create);
324
325static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
326{
327 list_del(&name_node->list);
328 netdev_name_node_del(name_node);
329 kfree(name_node->name);
330 netdev_name_node_free(name_node);
331}
332
333int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
334{
335 struct netdev_name_node *name_node;
336 struct net *net = dev_net(dev);
337
338 name_node = netdev_name_node_lookup(net, name);
339 if (!name_node)
340 return -ENOENT;
341
342
343
344 if (name_node == dev->name_node || name_node->dev != dev)
345 return -EINVAL;
346
347 __netdev_name_node_alt_destroy(name_node);
348
349 return 0;
350}
351EXPORT_SYMBOL(netdev_name_node_alt_destroy);
352
353static void netdev_name_node_alt_flush(struct net_device *dev)
354{
355 struct netdev_name_node *name_node, *tmp;
356
357 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
358 __netdev_name_node_alt_destroy(name_node);
359}
360
361
362static void list_netdevice(struct net_device *dev)
363{
364 struct net *net = dev_net(dev);
365
366 ASSERT_RTNL();
367
368 write_lock(&dev_base_lock);
369 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
370 netdev_name_node_add(net, dev->name_node);
371 hlist_add_head_rcu(&dev->index_hlist,
372 dev_index_hash(net, dev->ifindex));
373 write_unlock(&dev_base_lock);
374
375 dev_base_seq_inc(net);
376}
377
378
379
380
381static void unlist_netdevice(struct net_device *dev)
382{
383 ASSERT_RTNL();
384
385
386 write_lock(&dev_base_lock);
387 list_del_rcu(&dev->dev_list);
388 netdev_name_node_del(dev->name_node);
389 hlist_del_rcu(&dev->index_hlist);
390 write_unlock(&dev_base_lock);
391
392 dev_base_seq_inc(dev_net(dev));
393}
394
395
396
397
398
399static RAW_NOTIFIER_HEAD(netdev_chain);
400
401
402
403
404
405
406DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
407EXPORT_PER_CPU_SYMBOL(softnet_data);
408
409#ifdef CONFIG_LOCKDEP
410
411
412
413
414static const unsigned short netdev_lock_type[] = {
415 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
416 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
417 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
418 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
419 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
420 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
421 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
422 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
423 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
424 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
425 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
426 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
427 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
428 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
429 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
430
431static const char *const netdev_lock_name[] = {
432 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
433 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
434 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
435 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
436 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
437 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
438 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
439 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
440 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
441 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
442 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
443 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
444 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
445 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
446 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
447
448static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
449static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
450
451static inline unsigned short netdev_lock_pos(unsigned short dev_type)
452{
453 int i;
454
455 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
456 if (netdev_lock_type[i] == dev_type)
457 return i;
458
459 return ARRAY_SIZE(netdev_lock_type) - 1;
460}
461
462static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
463 unsigned short dev_type)
464{
465 int i;
466
467 i = netdev_lock_pos(dev_type);
468 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
469 netdev_lock_name[i]);
470}
471
472static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
473{
474 int i;
475
476 i = netdev_lock_pos(dev->type);
477 lockdep_set_class_and_name(&dev->addr_list_lock,
478 &netdev_addr_lock_key[i],
479 netdev_lock_name[i]);
480}
481#else
482static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
483 unsigned short dev_type)
484{
485}
486
487static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
488{
489}
490#endif
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515static inline struct list_head *ptype_head(const struct packet_type *pt)
516{
517 if (pt->type == htons(ETH_P_ALL))
518 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
519 else
520 return pt->dev ? &pt->dev->ptype_specific :
521 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
522}
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537void dev_add_pack(struct packet_type *pt)
538{
539 struct list_head *head = ptype_head(pt);
540
541 spin_lock(&ptype_lock);
542 list_add_rcu(&pt->list, head);
543 spin_unlock(&ptype_lock);
544}
545EXPORT_SYMBOL(dev_add_pack);
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560void __dev_remove_pack(struct packet_type *pt)
561{
562 struct list_head *head = ptype_head(pt);
563 struct packet_type *pt1;
564
565 spin_lock(&ptype_lock);
566
567 list_for_each_entry(pt1, head, list) {
568 if (pt == pt1) {
569 list_del_rcu(&pt->list);
570 goto out;
571 }
572 }
573
574 pr_warn("dev_remove_pack: %p not found\n", pt);
575out:
576 spin_unlock(&ptype_lock);
577}
578EXPORT_SYMBOL(__dev_remove_pack);
579
580
581
582
583
584
585
586
587
588
589
590
591
592void dev_remove_pack(struct packet_type *pt)
593{
594 __dev_remove_pack(pt);
595
596 synchronize_net();
597}
598EXPORT_SYMBOL(dev_remove_pack);
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615int dev_get_iflink(const struct net_device *dev)
616{
617 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
618 return dev->netdev_ops->ndo_get_iflink(dev);
619
620 return dev->ifindex;
621}
622EXPORT_SYMBOL(dev_get_iflink);
623
624
625
626
627
628
629
630
631
632
633int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
634{
635 struct ip_tunnel_info *info;
636
637 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
638 return -EINVAL;
639
640 info = skb_tunnel_info_unclone(skb);
641 if (!info)
642 return -ENOMEM;
643 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
644 return -EINVAL;
645
646 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
647}
648EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
649
650static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
651{
652 int k = stack->num_paths++;
653
654 if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX))
655 return NULL;
656
657 return &stack->path[k];
658}
659
660int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
661 struct net_device_path_stack *stack)
662{
663 const struct net_device *last_dev;
664 struct net_device_path_ctx ctx = {
665 .dev = dev,
666 .daddr = daddr,
667 };
668 struct net_device_path *path;
669 int ret = 0;
670
671 stack->num_paths = 0;
672 while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
673 last_dev = ctx.dev;
674 path = dev_fwd_path(stack);
675 if (!path)
676 return -1;
677
678 memset(path, 0, sizeof(struct net_device_path));
679 ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
680 if (ret < 0)
681 return -1;
682
683 if (WARN_ON_ONCE(last_dev == ctx.dev))
684 return -1;
685 }
686 path = dev_fwd_path(stack);
687 if (!path)
688 return -1;
689 path->type = DEV_PATH_ETHERNET;
690 path->dev = ctx.dev;
691
692 return ret;
693}
694EXPORT_SYMBOL_GPL(dev_fill_forward_path);
695
696
697
698
699
700
701
702
703
704
705
706
707
708struct net_device *__dev_get_by_name(struct net *net, const char *name)
709{
710 struct netdev_name_node *node_name;
711
712 node_name = netdev_name_node_lookup(net, name);
713 return node_name ? node_name->dev : NULL;
714}
715EXPORT_SYMBOL(__dev_get_by_name);
716
717
718
719
720
721
722
723
724
725
726
727
728
729struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
730{
731 struct netdev_name_node *node_name;
732
733 node_name = netdev_name_node_lookup_rcu(net, name);
734 return node_name ? node_name->dev : NULL;
735}
736EXPORT_SYMBOL(dev_get_by_name_rcu);
737
738
739
740
741
742
743
744
745
746
747
748
749
750struct net_device *dev_get_by_name(struct net *net, const char *name)
751{
752 struct net_device *dev;
753
754 rcu_read_lock();
755 dev = dev_get_by_name_rcu(net, name);
756 dev_hold(dev);
757 rcu_read_unlock();
758 return dev;
759}
760EXPORT_SYMBOL(dev_get_by_name);
761
762
763
764
765
766
767
768
769
770
771
772
773
774struct net_device *__dev_get_by_index(struct net *net, int ifindex)
775{
776 struct net_device *dev;
777 struct hlist_head *head = dev_index_hash(net, ifindex);
778
779 hlist_for_each_entry(dev, head, index_hlist)
780 if (dev->ifindex == ifindex)
781 return dev;
782
783 return NULL;
784}
785EXPORT_SYMBOL(__dev_get_by_index);
786
787
788
789
790
791
792
793
794
795
796
797
798struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
799{
800 struct net_device *dev;
801 struct hlist_head *head = dev_index_hash(net, ifindex);
802
803 hlist_for_each_entry_rcu(dev, head, index_hlist)
804 if (dev->ifindex == ifindex)
805 return dev;
806
807 return NULL;
808}
809EXPORT_SYMBOL(dev_get_by_index_rcu);
810
811
812
813
814
815
816
817
818
819
820
821
822
823struct net_device *dev_get_by_index(struct net *net, int ifindex)
824{
825 struct net_device *dev;
826
827 rcu_read_lock();
828 dev = dev_get_by_index_rcu(net, ifindex);
829 dev_hold(dev);
830 rcu_read_unlock();
831 return dev;
832}
833EXPORT_SYMBOL(dev_get_by_index);
834
835
836
837
838
839
840
841
842
843
844
845struct net_device *dev_get_by_napi_id(unsigned int napi_id)
846{
847 struct napi_struct *napi;
848
849 WARN_ON_ONCE(!rcu_read_lock_held());
850
851 if (napi_id < MIN_NAPI_ID)
852 return NULL;
853
854 napi = napi_by_id(napi_id);
855
856 return napi ? napi->dev : NULL;
857}
858EXPORT_SYMBOL(dev_get_by_napi_id);
859
860
861
862
863
864
865
866int netdev_get_name(struct net *net, char *name, int ifindex)
867{
868 struct net_device *dev;
869 int ret;
870
871 down_read(&devnet_rename_sem);
872 rcu_read_lock();
873
874 dev = dev_get_by_index_rcu(net, ifindex);
875 if (!dev) {
876 ret = -ENODEV;
877 goto out;
878 }
879
880 strcpy(name, dev->name);
881
882 ret = 0;
883out:
884 rcu_read_unlock();
885 up_read(&devnet_rename_sem);
886 return ret;
887}
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
904 const char *ha)
905{
906 struct net_device *dev;
907
908 for_each_netdev_rcu(net, dev)
909 if (dev->type == type &&
910 !memcmp(dev->dev_addr, ha, dev->addr_len))
911 return dev;
912
913 return NULL;
914}
915EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
916
917struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
918{
919 struct net_device *dev, *ret = NULL;
920
921 rcu_read_lock();
922 for_each_netdev_rcu(net, dev)
923 if (dev->type == type) {
924 dev_hold(dev);
925 ret = dev;
926 break;
927 }
928 rcu_read_unlock();
929 return ret;
930}
931EXPORT_SYMBOL(dev_getfirstbyhwtype);
932
933
934
935
936
937
938
939
940
941
942
943
944struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
945 unsigned short mask)
946{
947 struct net_device *dev, *ret;
948
949 ASSERT_RTNL();
950
951 ret = NULL;
952 for_each_netdev(net, dev) {
953 if (((dev->flags ^ if_flags) & mask) == 0) {
954 ret = dev;
955 break;
956 }
957 }
958 return ret;
959}
960EXPORT_SYMBOL(__dev_get_by_flags);
961
962
963
964
965
966
967
968
969
970bool dev_valid_name(const char *name)
971{
972 if (*name == '\0')
973 return false;
974 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
975 return false;
976 if (!strcmp(name, ".") || !strcmp(name, ".."))
977 return false;
978
979 while (*name) {
980 if (*name == '/' || *name == ':' || isspace(*name))
981 return false;
982 name++;
983 }
984 return true;
985}
986EXPORT_SYMBOL(dev_valid_name);
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1004{
1005 int i = 0;
1006 const char *p;
1007 const int max_netdevices = 8*PAGE_SIZE;
1008 unsigned long *inuse;
1009 struct net_device *d;
1010
1011 if (!dev_valid_name(name))
1012 return -EINVAL;
1013
1014 p = strchr(name, '%');
1015 if (p) {
1016
1017
1018
1019
1020
1021 if (p[1] != 'd' || strchr(p + 2, '%'))
1022 return -EINVAL;
1023
1024
1025 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1026 if (!inuse)
1027 return -ENOMEM;
1028
1029 for_each_netdev(net, d) {
1030 struct netdev_name_node *name_node;
1031 list_for_each_entry(name_node, &d->name_node->list, list) {
1032 if (!sscanf(name_node->name, name, &i))
1033 continue;
1034 if (i < 0 || i >= max_netdevices)
1035 continue;
1036
1037
1038 snprintf(buf, IFNAMSIZ, name, i);
1039 if (!strncmp(buf, name_node->name, IFNAMSIZ))
1040 set_bit(i, inuse);
1041 }
1042 if (!sscanf(d->name, name, &i))
1043 continue;
1044 if (i < 0 || i >= max_netdevices)
1045 continue;
1046
1047
1048 snprintf(buf, IFNAMSIZ, name, i);
1049 if (!strncmp(buf, d->name, IFNAMSIZ))
1050 set_bit(i, inuse);
1051 }
1052
1053 i = find_first_zero_bit(inuse, max_netdevices);
1054 free_page((unsigned long) inuse);
1055 }
1056
1057 snprintf(buf, IFNAMSIZ, name, i);
1058 if (!netdev_name_in_use(net, buf))
1059 return i;
1060
1061
1062
1063
1064
1065 return -ENFILE;
1066}
1067
1068static int dev_alloc_name_ns(struct net *net,
1069 struct net_device *dev,
1070 const char *name)
1071{
1072 char buf[IFNAMSIZ];
1073 int ret;
1074
1075 BUG_ON(!net);
1076 ret = __dev_alloc_name(net, name, buf);
1077 if (ret >= 0)
1078 strlcpy(dev->name, buf, IFNAMSIZ);
1079 return ret;
1080}
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096int dev_alloc_name(struct net_device *dev, const char *name)
1097{
1098 return dev_alloc_name_ns(dev_net(dev), dev, name);
1099}
1100EXPORT_SYMBOL(dev_alloc_name);
1101
1102static int dev_get_valid_name(struct net *net, struct net_device *dev,
1103 const char *name)
1104{
1105 BUG_ON(!net);
1106
1107 if (!dev_valid_name(name))
1108 return -EINVAL;
1109
1110 if (strchr(name, '%'))
1111 return dev_alloc_name_ns(net, dev, name);
1112 else if (netdev_name_in_use(net, name))
1113 return -EEXIST;
1114 else if (dev->name != name)
1115 strlcpy(dev->name, name, IFNAMSIZ);
1116
1117 return 0;
1118}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128int dev_change_name(struct net_device *dev, const char *newname)
1129{
1130 unsigned char old_assign_type;
1131 char oldname[IFNAMSIZ];
1132 int err = 0;
1133 int ret;
1134 struct net *net;
1135
1136 ASSERT_RTNL();
1137 BUG_ON(!dev_net(dev));
1138
1139 net = dev_net(dev);
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 if (dev->flags & IFF_UP &&
1154 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
1155 return -EBUSY;
1156
1157 down_write(&devnet_rename_sem);
1158
1159 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1160 up_write(&devnet_rename_sem);
1161 return 0;
1162 }
1163
1164 memcpy(oldname, dev->name, IFNAMSIZ);
1165
1166 err = dev_get_valid_name(net, dev, newname);
1167 if (err < 0) {
1168 up_write(&devnet_rename_sem);
1169 return err;
1170 }
1171
1172 if (oldname[0] && !strchr(oldname, '%'))
1173 netdev_info(dev, "renamed from %s\n", oldname);
1174
1175 old_assign_type = dev->name_assign_type;
1176 dev->name_assign_type = NET_NAME_RENAMED;
1177
1178rollback:
1179 ret = device_rename(&dev->dev, dev->name);
1180 if (ret) {
1181 memcpy(dev->name, oldname, IFNAMSIZ);
1182 dev->name_assign_type = old_assign_type;
1183 up_write(&devnet_rename_sem);
1184 return ret;
1185 }
1186
1187 up_write(&devnet_rename_sem);
1188
1189 netdev_adjacent_rename_links(dev, oldname);
1190
1191 write_lock(&dev_base_lock);
1192 netdev_name_node_del(dev->name_node);
1193 write_unlock(&dev_base_lock);
1194
1195 synchronize_rcu();
1196
1197 write_lock(&dev_base_lock);
1198 netdev_name_node_add(net, dev->name_node);
1199 write_unlock(&dev_base_lock);
1200
1201 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1202 ret = notifier_to_errno(ret);
1203
1204 if (ret) {
1205
1206 if (err >= 0) {
1207 err = ret;
1208 down_write(&devnet_rename_sem);
1209 memcpy(dev->name, oldname, IFNAMSIZ);
1210 memcpy(oldname, newname, IFNAMSIZ);
1211 dev->name_assign_type = old_assign_type;
1212 old_assign_type = NET_NAME_RENAMED;
1213 goto rollback;
1214 } else {
1215 netdev_err(dev, "name change rollback failed: %d\n",
1216 ret);
1217 }
1218 }
1219
1220 return err;
1221}
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1232{
1233 struct dev_ifalias *new_alias = NULL;
1234
1235 if (len >= IFALIASZ)
1236 return -EINVAL;
1237
1238 if (len) {
1239 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1240 if (!new_alias)
1241 return -ENOMEM;
1242
1243 memcpy(new_alias->ifalias, alias, len);
1244 new_alias->ifalias[len] = 0;
1245 }
1246
1247 mutex_lock(&ifalias_mutex);
1248 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1249 mutex_is_locked(&ifalias_mutex));
1250 mutex_unlock(&ifalias_mutex);
1251
1252 if (new_alias)
1253 kfree_rcu(new_alias, rcuhead);
1254
1255 return len;
1256}
1257EXPORT_SYMBOL(dev_set_alias);
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1269{
1270 const struct dev_ifalias *alias;
1271 int ret = 0;
1272
1273 rcu_read_lock();
1274 alias = rcu_dereference(dev->ifalias);
1275 if (alias)
1276 ret = snprintf(name, len, "%s", alias->ifalias);
1277 rcu_read_unlock();
1278
1279 return ret;
1280}
1281
1282
1283
1284
1285
1286
1287
1288void netdev_features_change(struct net_device *dev)
1289{
1290 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1291}
1292EXPORT_SYMBOL(netdev_features_change);
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302void netdev_state_change(struct net_device *dev)
1303{
1304 if (dev->flags & IFF_UP) {
1305 struct netdev_notifier_change_info change_info = {
1306 .info.dev = dev,
1307 };
1308
1309 call_netdevice_notifiers_info(NETDEV_CHANGE,
1310 &change_info.info);
1311 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1312 }
1313}
1314EXPORT_SYMBOL(netdev_state_change);
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327void __netdev_notify_peers(struct net_device *dev)
1328{
1329 ASSERT_RTNL();
1330 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1331 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1332}
1333EXPORT_SYMBOL(__netdev_notify_peers);
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345void netdev_notify_peers(struct net_device *dev)
1346{
1347 rtnl_lock();
1348 __netdev_notify_peers(dev);
1349 rtnl_unlock();
1350}
1351EXPORT_SYMBOL(netdev_notify_peers);
1352
1353static int napi_threaded_poll(void *data);
1354
1355static int napi_kthread_create(struct napi_struct *n)
1356{
1357 int err = 0;
1358
1359
1360
1361
1362
1363 n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d",
1364 n->dev->name, n->napi_id);
1365 if (IS_ERR(n->thread)) {
1366 err = PTR_ERR(n->thread);
1367 pr_err("kthread_run failed with err %d\n", err);
1368 n->thread = NULL;
1369 }
1370
1371 return err;
1372}
1373
1374static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1375{
1376 const struct net_device_ops *ops = dev->netdev_ops;
1377 int ret;
1378
1379 ASSERT_RTNL();
1380 dev_addr_check(dev);
1381
1382 if (!netif_device_present(dev)) {
1383
1384 if (dev->dev.parent)
1385 pm_runtime_resume(dev->dev.parent);
1386 if (!netif_device_present(dev))
1387 return -ENODEV;
1388 }
1389
1390
1391
1392
1393
1394 netpoll_poll_disable(dev);
1395
1396 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
1397 ret = notifier_to_errno(ret);
1398 if (ret)
1399 return ret;
1400
1401 set_bit(__LINK_STATE_START, &dev->state);
1402
1403 if (ops->ndo_validate_addr)
1404 ret = ops->ndo_validate_addr(dev);
1405
1406 if (!ret && ops->ndo_open)
1407 ret = ops->ndo_open(dev);
1408
1409 netpoll_poll_enable(dev);
1410
1411 if (ret)
1412 clear_bit(__LINK_STATE_START, &dev->state);
1413 else {
1414 dev->flags |= IFF_UP;
1415 dev_set_rx_mode(dev);
1416 dev_activate(dev);
1417 add_device_randomness(dev->dev_addr, dev->addr_len);
1418 }
1419
1420 return ret;
1421}
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1437{
1438 int ret;
1439
1440 if (dev->flags & IFF_UP)
1441 return 0;
1442
1443 ret = __dev_open(dev, extack);
1444 if (ret < 0)
1445 return ret;
1446
1447 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1448 call_netdevice_notifiers(NETDEV_UP, dev);
1449
1450 return ret;
1451}
1452EXPORT_SYMBOL(dev_open);
1453
1454static void __dev_close_many(struct list_head *head)
1455{
1456 struct net_device *dev;
1457
1458 ASSERT_RTNL();
1459 might_sleep();
1460
1461 list_for_each_entry(dev, head, close_list) {
1462
1463 netpoll_poll_disable(dev);
1464
1465 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1466
1467 clear_bit(__LINK_STATE_START, &dev->state);
1468
1469
1470
1471
1472
1473
1474
1475 smp_mb__after_atomic();
1476 }
1477
1478 dev_deactivate_many(head);
1479
1480 list_for_each_entry(dev, head, close_list) {
1481 const struct net_device_ops *ops = dev->netdev_ops;
1482
1483
1484
1485
1486
1487
1488
1489
1490 if (ops->ndo_stop)
1491 ops->ndo_stop(dev);
1492
1493 dev->flags &= ~IFF_UP;
1494 netpoll_poll_enable(dev);
1495 }
1496}
1497
1498static void __dev_close(struct net_device *dev)
1499{
1500 LIST_HEAD(single);
1501
1502 list_add(&dev->close_list, &single);
1503 __dev_close_many(&single);
1504 list_del(&single);
1505}
1506
1507void dev_close_many(struct list_head *head, bool unlink)
1508{
1509 struct net_device *dev, *tmp;
1510
1511
1512 list_for_each_entry_safe(dev, tmp, head, close_list)
1513 if (!(dev->flags & IFF_UP))
1514 list_del_init(&dev->close_list);
1515
1516 __dev_close_many(head);
1517
1518 list_for_each_entry_safe(dev, tmp, head, close_list) {
1519 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1520 call_netdevice_notifiers(NETDEV_DOWN, dev);
1521 if (unlink)
1522 list_del_init(&dev->close_list);
1523 }
1524}
1525EXPORT_SYMBOL(dev_close_many);
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536void dev_close(struct net_device *dev)
1537{
1538 if (dev->flags & IFF_UP) {
1539 LIST_HEAD(single);
1540
1541 list_add(&dev->close_list, &single);
1542 dev_close_many(&single, true);
1543 list_del(&single);
1544 }
1545}
1546EXPORT_SYMBOL(dev_close);
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557void dev_disable_lro(struct net_device *dev)
1558{
1559 struct net_device *lower_dev;
1560 struct list_head *iter;
1561
1562 dev->wanted_features &= ~NETIF_F_LRO;
1563 netdev_update_features(dev);
1564
1565 if (unlikely(dev->features & NETIF_F_LRO))
1566 netdev_WARN(dev, "failed to disable LRO!\n");
1567
1568 netdev_for_each_lower_dev(dev, lower_dev, iter)
1569 dev_disable_lro(lower_dev);
1570}
1571EXPORT_SYMBOL(dev_disable_lro);
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581static void dev_disable_gro_hw(struct net_device *dev)
1582{
1583 dev->wanted_features &= ~NETIF_F_GRO_HW;
1584 netdev_update_features(dev);
1585
1586 if (unlikely(dev->features & NETIF_F_GRO_HW))
1587 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1588}
1589
1590const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1591{
1592#define N(val) \
1593 case NETDEV_##val: \
1594 return "NETDEV_" __stringify(val);
1595 switch (cmd) {
1596 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1597 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1598 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1599 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1600 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1601 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1602 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
1603 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1604 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
1605 N(PRE_CHANGEADDR)
1606 }
1607#undef N
1608 return "UNKNOWN_NETDEV_EVENT";
1609}
1610EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1611
1612static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1613 struct net_device *dev)
1614{
1615 struct netdev_notifier_info info = {
1616 .dev = dev,
1617 };
1618
1619 return nb->notifier_call(nb, val, &info);
1620}
1621
1622static int call_netdevice_register_notifiers(struct notifier_block *nb,
1623 struct net_device *dev)
1624{
1625 int err;
1626
1627 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1628 err = notifier_to_errno(err);
1629 if (err)
1630 return err;
1631
1632 if (!(dev->flags & IFF_UP))
1633 return 0;
1634
1635 call_netdevice_notifier(nb, NETDEV_UP, dev);
1636 return 0;
1637}
1638
1639static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1640 struct net_device *dev)
1641{
1642 if (dev->flags & IFF_UP) {
1643 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1644 dev);
1645 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1646 }
1647 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1648}
1649
1650static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1651 struct net *net)
1652{
1653 struct net_device *dev;
1654 int err;
1655
1656 for_each_netdev(net, dev) {
1657 err = call_netdevice_register_notifiers(nb, dev);
1658 if (err)
1659 goto rollback;
1660 }
1661 return 0;
1662
1663rollback:
1664 for_each_netdev_continue_reverse(net, dev)
1665 call_netdevice_unregister_notifiers(nb, dev);
1666 return err;
1667}
1668
1669static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1670 struct net *net)
1671{
1672 struct net_device *dev;
1673
1674 for_each_netdev(net, dev)
1675 call_netdevice_unregister_notifiers(nb, dev);
1676}
1677
1678static int dev_boot_phase = 1;
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694int register_netdevice_notifier(struct notifier_block *nb)
1695{
1696 struct net *net;
1697 int err;
1698
1699
1700 down_write(&pernet_ops_rwsem);
1701 rtnl_lock();
1702 err = raw_notifier_chain_register(&netdev_chain, nb);
1703 if (err)
1704 goto unlock;
1705 if (dev_boot_phase)
1706 goto unlock;
1707 for_each_net(net) {
1708 err = call_netdevice_register_net_notifiers(nb, net);
1709 if (err)
1710 goto rollback;
1711 }
1712
1713unlock:
1714 rtnl_unlock();
1715 up_write(&pernet_ops_rwsem);
1716 return err;
1717
1718rollback:
1719 for_each_net_continue_reverse(net)
1720 call_netdevice_unregister_net_notifiers(nb, net);
1721
1722 raw_notifier_chain_unregister(&netdev_chain, nb);
1723 goto unlock;
1724}
1725EXPORT_SYMBOL(register_netdevice_notifier);
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741int unregister_netdevice_notifier(struct notifier_block *nb)
1742{
1743 struct net *net;
1744 int err;
1745
1746
1747 down_write(&pernet_ops_rwsem);
1748 rtnl_lock();
1749 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1750 if (err)
1751 goto unlock;
1752
1753 for_each_net(net)
1754 call_netdevice_unregister_net_notifiers(nb, net);
1755
1756unlock:
1757 rtnl_unlock();
1758 up_write(&pernet_ops_rwsem);
1759 return err;
1760}
1761EXPORT_SYMBOL(unregister_netdevice_notifier);
1762
1763static int __register_netdevice_notifier_net(struct net *net,
1764 struct notifier_block *nb,
1765 bool ignore_call_fail)
1766{
1767 int err;
1768
1769 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1770 if (err)
1771 return err;
1772 if (dev_boot_phase)
1773 return 0;
1774
1775 err = call_netdevice_register_net_notifiers(nb, net);
1776 if (err && !ignore_call_fail)
1777 goto chain_unregister;
1778
1779 return 0;
1780
1781chain_unregister:
1782 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1783 return err;
1784}
1785
1786static int __unregister_netdevice_notifier_net(struct net *net,
1787 struct notifier_block *nb)
1788{
1789 int err;
1790
1791 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1792 if (err)
1793 return err;
1794
1795 call_netdevice_unregister_net_notifiers(nb, net);
1796 return 0;
1797}
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1815{
1816 int err;
1817
1818 rtnl_lock();
1819 err = __register_netdevice_notifier_net(net, nb, false);
1820 rtnl_unlock();
1821 return err;
1822}
1823EXPORT_SYMBOL(register_netdevice_notifier_net);
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841int unregister_netdevice_notifier_net(struct net *net,
1842 struct notifier_block *nb)
1843{
1844 int err;
1845
1846 rtnl_lock();
1847 err = __unregister_netdevice_notifier_net(net, nb);
1848 rtnl_unlock();
1849 return err;
1850}
1851EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1852
1853int register_netdevice_notifier_dev_net(struct net_device *dev,
1854 struct notifier_block *nb,
1855 struct netdev_net_notifier *nn)
1856{
1857 int err;
1858
1859 rtnl_lock();
1860 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1861 if (!err) {
1862 nn->nb = nb;
1863 list_add(&nn->list, &dev->net_notifier_list);
1864 }
1865 rtnl_unlock();
1866 return err;
1867}
1868EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1869
1870int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1871 struct notifier_block *nb,
1872 struct netdev_net_notifier *nn)
1873{
1874 int err;
1875
1876 rtnl_lock();
1877 list_del(&nn->list);
1878 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
1879 rtnl_unlock();
1880 return err;
1881}
1882EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1883
1884static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1885 struct net *net)
1886{
1887 struct netdev_net_notifier *nn;
1888
1889 list_for_each_entry(nn, &dev->net_notifier_list, list) {
1890 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
1891 __register_netdevice_notifier_net(net, nn->nb, true);
1892 }
1893}
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904static int call_netdevice_notifiers_info(unsigned long val,
1905 struct netdev_notifier_info *info)
1906{
1907 struct net *net = dev_net(info->dev);
1908 int ret;
1909
1910 ASSERT_RTNL();
1911
1912
1913
1914
1915
1916 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1917 if (ret & NOTIFY_STOP_MASK)
1918 return ret;
1919 return raw_notifier_call_chain(&netdev_chain, val, info);
1920}
1921
1922static int call_netdevice_notifiers_extack(unsigned long val,
1923 struct net_device *dev,
1924 struct netlink_ext_ack *extack)
1925{
1926 struct netdev_notifier_info info = {
1927 .dev = dev,
1928 .extack = extack,
1929 };
1930
1931 return call_netdevice_notifiers_info(val, &info);
1932}
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1944{
1945 return call_netdevice_notifiers_extack(val, dev, NULL);
1946}
1947EXPORT_SYMBOL(call_netdevice_notifiers);
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958static int call_netdevice_notifiers_mtu(unsigned long val,
1959 struct net_device *dev, u32 arg)
1960{
1961 struct netdev_notifier_info_ext info = {
1962 .info.dev = dev,
1963 .ext.mtu = arg,
1964 };
1965
1966 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1967
1968 return call_netdevice_notifiers_info(val, &info.info);
1969}
1970
1971#ifdef CONFIG_NET_INGRESS
1972static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
1973
1974void net_inc_ingress_queue(void)
1975{
1976 static_branch_inc(&ingress_needed_key);
1977}
1978EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1979
1980void net_dec_ingress_queue(void)
1981{
1982 static_branch_dec(&ingress_needed_key);
1983}
1984EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1985#endif
1986
1987#ifdef CONFIG_NET_EGRESS
1988static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
1989
1990void net_inc_egress_queue(void)
1991{
1992 static_branch_inc(&egress_needed_key);
1993}
1994EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1995
1996void net_dec_egress_queue(void)
1997{
1998 static_branch_dec(&egress_needed_key);
1999}
2000EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2001#endif
2002
2003static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
2004#ifdef CONFIG_JUMP_LABEL
2005static atomic_t netstamp_needed_deferred;
2006static atomic_t netstamp_wanted;
2007static void netstamp_clear(struct work_struct *work)
2008{
2009 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
2010 int wanted;
2011
2012 wanted = atomic_add_return(deferred, &netstamp_wanted);
2013 if (wanted > 0)
2014 static_branch_enable(&netstamp_needed_key);
2015 else
2016 static_branch_disable(&netstamp_needed_key);
2017}
2018static DECLARE_WORK(netstamp_work, netstamp_clear);
2019#endif
2020
2021void net_enable_timestamp(void)
2022{
2023#ifdef CONFIG_JUMP_LABEL
2024 int wanted;
2025
2026 while (1) {
2027 wanted = atomic_read(&netstamp_wanted);
2028 if (wanted <= 0)
2029 break;
2030 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2031 return;
2032 }
2033 atomic_inc(&netstamp_needed_deferred);
2034 schedule_work(&netstamp_work);
2035#else
2036 static_branch_inc(&netstamp_needed_key);
2037#endif
2038}
2039EXPORT_SYMBOL(net_enable_timestamp);
2040
2041void net_disable_timestamp(void)
2042{
2043#ifdef CONFIG_JUMP_LABEL
2044 int wanted;
2045
2046 while (1) {
2047 wanted = atomic_read(&netstamp_wanted);
2048 if (wanted <= 1)
2049 break;
2050 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2051 return;
2052 }
2053 atomic_dec(&netstamp_needed_deferred);
2054 schedule_work(&netstamp_work);
2055#else
2056 static_branch_dec(&netstamp_needed_key);
2057#endif
2058}
2059EXPORT_SYMBOL(net_disable_timestamp);
2060
2061static inline void net_timestamp_set(struct sk_buff *skb)
2062{
2063 skb->tstamp = 0;
2064 if (static_branch_unlikely(&netstamp_needed_key))
2065 __net_timestamp(skb);
2066}
2067
2068#define net_timestamp_check(COND, SKB) \
2069 if (static_branch_unlikely(&netstamp_needed_key)) { \
2070 if ((COND) && !(SKB)->tstamp) \
2071 __net_timestamp(SKB); \
2072 } \
2073
2074bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
2075{
2076 return __is_skb_forwardable(dev, skb, true);
2077}
2078EXPORT_SYMBOL_GPL(is_skb_forwardable);
2079
2080static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb,
2081 bool check_mtu)
2082{
2083 int ret = ____dev_forward_skb(dev, skb, check_mtu);
2084
2085 if (likely(!ret)) {
2086 skb->protocol = eth_type_trans(skb, dev);
2087 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
2088 }
2089
2090 return ret;
2091}
2092
2093int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2094{
2095 return __dev_forward_skb2(dev, skb, true);
2096}
2097EXPORT_SYMBOL_GPL(__dev_forward_skb);
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2118{
2119 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
2120}
2121EXPORT_SYMBOL_GPL(dev_forward_skb);
2122
2123int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb)
2124{
2125 return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb);
2126}
2127
2128static inline int deliver_skb(struct sk_buff *skb,
2129 struct packet_type *pt_prev,
2130 struct net_device *orig_dev)
2131{
2132 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
2133 return -ENOMEM;
2134 refcount_inc(&skb->users);
2135 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2136}
2137
2138static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2139 struct packet_type **pt,
2140 struct net_device *orig_dev,
2141 __be16 type,
2142 struct list_head *ptype_list)
2143{
2144 struct packet_type *ptype, *pt_prev = *pt;
2145
2146 list_for_each_entry_rcu(ptype, ptype_list, list) {
2147 if (ptype->type != type)
2148 continue;
2149 if (pt_prev)
2150 deliver_skb(skb, pt_prev, orig_dev);
2151 pt_prev = ptype;
2152 }
2153 *pt = pt_prev;
2154}
2155
2156static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2157{
2158 if (!ptype->af_packet_priv || !skb->sk)
2159 return false;
2160
2161 if (ptype->id_match)
2162 return ptype->id_match(ptype, skb->sk);
2163 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2164 return true;
2165
2166 return false;
2167}
2168
2169
2170
2171
2172
2173
2174bool dev_nit_active(struct net_device *dev)
2175{
2176 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2177}
2178EXPORT_SYMBOL_GPL(dev_nit_active);
2179
2180
2181
2182
2183
2184
2185void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
2186{
2187 struct packet_type *ptype;
2188 struct sk_buff *skb2 = NULL;
2189 struct packet_type *pt_prev = NULL;
2190 struct list_head *ptype_list = &ptype_all;
2191
2192 rcu_read_lock();
2193again:
2194 list_for_each_entry_rcu(ptype, ptype_list, list) {
2195 if (ptype->ignore_outgoing)
2196 continue;
2197
2198
2199
2200
2201 if (skb_loop_sk(ptype, skb))
2202 continue;
2203
2204 if (pt_prev) {
2205 deliver_skb(skb2, pt_prev, skb->dev);
2206 pt_prev = ptype;
2207 continue;
2208 }
2209
2210
2211 skb2 = skb_clone(skb, GFP_ATOMIC);
2212 if (!skb2)
2213 goto out_unlock;
2214
2215 net_timestamp_set(skb2);
2216
2217
2218
2219
2220
2221 skb_reset_mac_header(skb2);
2222
2223 if (skb_network_header(skb2) < skb2->data ||
2224 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2225 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2226 ntohs(skb2->protocol),
2227 dev->name);
2228 skb_reset_network_header(skb2);
2229 }
2230
2231 skb2->transport_header = skb2->network_header;
2232 skb2->pkt_type = PACKET_OUTGOING;
2233 pt_prev = ptype;
2234 }
2235
2236 if (ptype_list == &ptype_all) {
2237 ptype_list = &dev->ptype_all;
2238 goto again;
2239 }
2240out_unlock:
2241 if (pt_prev) {
2242 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2243 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2244 else
2245 kfree_skb(skb2);
2246 }
2247 rcu_read_unlock();
2248}
2249EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264static void netif_setup_tc(struct net_device *dev, unsigned int txq)
2265{
2266 int i;
2267 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2268
2269
2270 if (tc->offset + tc->count > txq) {
2271 netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
2272 dev->num_tc = 0;
2273 return;
2274 }
2275
2276
2277 for (i = 1; i < TC_BITMASK + 1; i++) {
2278 int q = netdev_get_prio_tc_map(dev, i);
2279
2280 tc = &dev->tc_to_txq[q];
2281 if (tc->offset + tc->count > txq) {
2282 netdev_warn(dev, "Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2283 i, q);
2284 netdev_set_prio_tc_map(dev, i, 0);
2285 }
2286 }
2287}
2288
2289int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2290{
2291 if (dev->num_tc) {
2292 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2293 int i;
2294
2295
2296 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2297 if ((txq - tc->offset) < tc->count)
2298 return i;
2299 }
2300
2301
2302 return -1;
2303 }
2304
2305 return 0;
2306}
2307EXPORT_SYMBOL(netdev_txq_to_tc);
2308
2309#ifdef CONFIG_XPS
2310static struct static_key xps_needed __read_mostly;
2311static struct static_key xps_rxqs_needed __read_mostly;
2312static DEFINE_MUTEX(xps_map_mutex);
2313#define xmap_dereference(P) \
2314 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2315
2316static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2317 struct xps_dev_maps *old_maps, int tci, u16 index)
2318{
2319 struct xps_map *map = NULL;
2320 int pos;
2321
2322 if (dev_maps)
2323 map = xmap_dereference(dev_maps->attr_map[tci]);
2324 if (!map)
2325 return false;
2326
2327 for (pos = map->len; pos--;) {
2328 if (map->queues[pos] != index)
2329 continue;
2330
2331 if (map->len > 1) {
2332 map->queues[pos] = map->queues[--map->len];
2333 break;
2334 }
2335
2336 if (old_maps)
2337 RCU_INIT_POINTER(old_maps->attr_map[tci], NULL);
2338 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
2339 kfree_rcu(map, rcu);
2340 return false;
2341 }
2342
2343 return true;
2344}
2345
2346static bool remove_xps_queue_cpu(struct net_device *dev,
2347 struct xps_dev_maps *dev_maps,
2348 int cpu, u16 offset, u16 count)
2349{
2350 int num_tc = dev_maps->num_tc;
2351 bool active = false;
2352 int tci;
2353
2354 for (tci = cpu * num_tc; num_tc--; tci++) {
2355 int i, j;
2356
2357 for (i = count, j = offset; i--; j++) {
2358 if (!remove_xps_queue(dev_maps, NULL, tci, j))
2359 break;
2360 }
2361
2362 active |= i < 0;
2363 }
2364
2365 return active;
2366}
2367
2368static void reset_xps_maps(struct net_device *dev,
2369 struct xps_dev_maps *dev_maps,
2370 enum xps_map_type type)
2371{
2372 static_key_slow_dec_cpuslocked(&xps_needed);
2373 if (type == XPS_RXQS)
2374 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2375
2376 RCU_INIT_POINTER(dev->xps_maps[type], NULL);
2377
2378 kfree_rcu(dev_maps, rcu);
2379}
2380
2381static void clean_xps_maps(struct net_device *dev, enum xps_map_type type,
2382 u16 offset, u16 count)
2383{
2384 struct xps_dev_maps *dev_maps;
2385 bool active = false;
2386 int i, j;
2387
2388 dev_maps = xmap_dereference(dev->xps_maps[type]);
2389 if (!dev_maps)
2390 return;
2391
2392 for (j = 0; j < dev_maps->nr_ids; j++)
2393 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count);
2394 if (!active)
2395 reset_xps_maps(dev, dev_maps, type);
2396
2397 if (type == XPS_CPUS) {
2398 for (i = offset + (count - 1); count--; i--)
2399 netdev_queue_numa_node_write(
2400 netdev_get_tx_queue(dev, i), NUMA_NO_NODE);
2401 }
2402}
2403
2404static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2405 u16 count)
2406{
2407 if (!static_key_false(&xps_needed))
2408 return;
2409
2410 cpus_read_lock();
2411 mutex_lock(&xps_map_mutex);
2412
2413 if (static_key_false(&xps_rxqs_needed))
2414 clean_xps_maps(dev, XPS_RXQS, offset, count);
2415
2416 clean_xps_maps(dev, XPS_CPUS, offset, count);
2417
2418 mutex_unlock(&xps_map_mutex);
2419 cpus_read_unlock();
2420}
2421
2422static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2423{
2424 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2425}
2426
2427static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2428 u16 index, bool is_rxqs_map)
2429{
2430 struct xps_map *new_map;
2431 int alloc_len = XPS_MIN_MAP_ALLOC;
2432 int i, pos;
2433
2434 for (pos = 0; map && pos < map->len; pos++) {
2435 if (map->queues[pos] != index)
2436 continue;
2437 return map;
2438 }
2439
2440
2441 if (map) {
2442 if (pos < map->alloc_len)
2443 return map;
2444
2445 alloc_len = map->alloc_len * 2;
2446 }
2447
2448
2449
2450
2451 if (is_rxqs_map)
2452 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2453 else
2454 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2455 cpu_to_node(attr_index));
2456 if (!new_map)
2457 return NULL;
2458
2459 for (i = 0; i < pos; i++)
2460 new_map->queues[i] = map->queues[i];
2461 new_map->alloc_len = alloc_len;
2462 new_map->len = pos;
2463
2464 return new_map;
2465}
2466
2467
2468static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps,
2469 struct xps_dev_maps *new_dev_maps, int index,
2470 int tc, bool skip_tc)
2471{
2472 int i, tci = index * dev_maps->num_tc;
2473 struct xps_map *map;
2474
2475
2476 for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2477 if (i == tc && skip_tc)
2478 continue;
2479
2480
2481 map = xmap_dereference(dev_maps->attr_map[tci]);
2482 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2483 }
2484}
2485
2486
2487int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2488 u16 index, enum xps_map_type type)
2489{
2490 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL;
2491 const unsigned long *online_mask = NULL;
2492 bool active = false, copy = false;
2493 int i, j, tci, numa_node_id = -2;
2494 int maps_sz, num_tc = 1, tc = 0;
2495 struct xps_map *map, *new_map;
2496 unsigned int nr_ids;
2497
2498 if (dev->num_tc) {
2499
2500 num_tc = dev->num_tc;
2501 if (num_tc < 0)
2502 return -EINVAL;
2503
2504
2505 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2506
2507 tc = netdev_txq_to_tc(dev, index);
2508 if (tc < 0)
2509 return -EINVAL;
2510 }
2511
2512 mutex_lock(&xps_map_mutex);
2513
2514 dev_maps = xmap_dereference(dev->xps_maps[type]);
2515 if (type == XPS_RXQS) {
2516 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2517 nr_ids = dev->num_rx_queues;
2518 } else {
2519 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2520 if (num_possible_cpus() > 1)
2521 online_mask = cpumask_bits(cpu_online_mask);
2522 nr_ids = nr_cpu_ids;
2523 }
2524
2525 if (maps_sz < L1_CACHE_BYTES)
2526 maps_sz = L1_CACHE_BYTES;
2527
2528
2529
2530
2531
2532
2533 if (dev_maps &&
2534 dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids)
2535 copy = true;
2536
2537
2538 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2539 j < nr_ids;) {
2540 if (!new_dev_maps) {
2541 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2542 if (!new_dev_maps) {
2543 mutex_unlock(&xps_map_mutex);
2544 return -ENOMEM;
2545 }
2546
2547 new_dev_maps->nr_ids = nr_ids;
2548 new_dev_maps->num_tc = num_tc;
2549 }
2550
2551 tci = j * num_tc + tc;
2552 map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL;
2553
2554 map = expand_xps_map(map, j, index, type == XPS_RXQS);
2555 if (!map)
2556 goto error;
2557
2558 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2559 }
2560
2561 if (!new_dev_maps)
2562 goto out_no_new_maps;
2563
2564 if (!dev_maps) {
2565
2566 static_key_slow_inc_cpuslocked(&xps_needed);
2567 if (type == XPS_RXQS)
2568 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2569 }
2570
2571 for (j = 0; j < nr_ids; j++) {
2572 bool skip_tc = false;
2573
2574 tci = j * num_tc + tc;
2575 if (netif_attr_test_mask(j, mask, nr_ids) &&
2576 netif_attr_test_online(j, online_mask, nr_ids)) {
2577
2578 int pos = 0;
2579
2580 skip_tc = true;
2581
2582 map = xmap_dereference(new_dev_maps->attr_map[tci]);
2583 while ((pos < map->len) && (map->queues[pos] != index))
2584 pos++;
2585
2586 if (pos == map->len)
2587 map->queues[map->len++] = index;
2588#ifdef CONFIG_NUMA
2589 if (type == XPS_CPUS) {
2590 if (numa_node_id == -2)
2591 numa_node_id = cpu_to_node(j);
2592 else if (numa_node_id != cpu_to_node(j))
2593 numa_node_id = -1;
2594 }
2595#endif
2596 }
2597
2598 if (copy)
2599 xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc,
2600 skip_tc);
2601 }
2602
2603 rcu_assign_pointer(dev->xps_maps[type], new_dev_maps);
2604
2605
2606 if (!dev_maps)
2607 goto out_no_old_maps;
2608
2609 for (j = 0; j < dev_maps->nr_ids; j++) {
2610 for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) {
2611 map = xmap_dereference(dev_maps->attr_map[tci]);
2612 if (!map)
2613 continue;
2614
2615 if (copy) {
2616 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2617 if (map == new_map)
2618 continue;
2619 }
2620
2621 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
2622 kfree_rcu(map, rcu);
2623 }
2624 }
2625
2626 old_dev_maps = dev_maps;
2627
2628out_no_old_maps:
2629 dev_maps = new_dev_maps;
2630 active = true;
2631
2632out_no_new_maps:
2633 if (type == XPS_CPUS)
2634
2635 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2636 (numa_node_id >= 0) ?
2637 numa_node_id : NUMA_NO_NODE);
2638
2639 if (!dev_maps)
2640 goto out_no_maps;
2641
2642
2643 for (j = 0; j < dev_maps->nr_ids; j++) {
2644 tci = j * dev_maps->num_tc;
2645
2646 for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2647 if (i == tc &&
2648 netif_attr_test_mask(j, mask, dev_maps->nr_ids) &&
2649 netif_attr_test_online(j, online_mask, dev_maps->nr_ids))
2650 continue;
2651
2652 active |= remove_xps_queue(dev_maps,
2653 copy ? old_dev_maps : NULL,
2654 tci, index);
2655 }
2656 }
2657
2658 if (old_dev_maps)
2659 kfree_rcu(old_dev_maps, rcu);
2660
2661
2662 if (!active)
2663 reset_xps_maps(dev, dev_maps, type);
2664
2665out_no_maps:
2666 mutex_unlock(&xps_map_mutex);
2667
2668 return 0;
2669error:
2670
2671 for (j = 0; j < nr_ids; j++) {
2672 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2673 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2674 map = copy ?
2675 xmap_dereference(dev_maps->attr_map[tci]) :
2676 NULL;
2677 if (new_map && new_map != map)
2678 kfree(new_map);
2679 }
2680 }
2681
2682 mutex_unlock(&xps_map_mutex);
2683
2684 kfree(new_dev_maps);
2685 return -ENOMEM;
2686}
2687EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
2688
2689int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2690 u16 index)
2691{
2692 int ret;
2693
2694 cpus_read_lock();
2695 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS);
2696 cpus_read_unlock();
2697
2698 return ret;
2699}
2700EXPORT_SYMBOL(netif_set_xps_queue);
2701
2702#endif
2703static void netdev_unbind_all_sb_channels(struct net_device *dev)
2704{
2705 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2706
2707
2708 while (txq-- != &dev->_tx[0]) {
2709 if (txq->sb_dev)
2710 netdev_unbind_sb_channel(dev, txq->sb_dev);
2711 }
2712}
2713
2714void netdev_reset_tc(struct net_device *dev)
2715{
2716#ifdef CONFIG_XPS
2717 netif_reset_xps_queues_gt(dev, 0);
2718#endif
2719 netdev_unbind_all_sb_channels(dev);
2720
2721
2722 dev->num_tc = 0;
2723 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2724 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2725}
2726EXPORT_SYMBOL(netdev_reset_tc);
2727
2728int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2729{
2730 if (tc >= dev->num_tc)
2731 return -EINVAL;
2732
2733#ifdef CONFIG_XPS
2734 netif_reset_xps_queues(dev, offset, count);
2735#endif
2736 dev->tc_to_txq[tc].count = count;
2737 dev->tc_to_txq[tc].offset = offset;
2738 return 0;
2739}
2740EXPORT_SYMBOL(netdev_set_tc_queue);
2741
2742int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2743{
2744 if (num_tc > TC_MAX_QUEUE)
2745 return -EINVAL;
2746
2747#ifdef CONFIG_XPS
2748 netif_reset_xps_queues_gt(dev, 0);
2749#endif
2750 netdev_unbind_all_sb_channels(dev);
2751
2752 dev->num_tc = num_tc;
2753 return 0;
2754}
2755EXPORT_SYMBOL(netdev_set_num_tc);
2756
2757void netdev_unbind_sb_channel(struct net_device *dev,
2758 struct net_device *sb_dev)
2759{
2760 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2761
2762#ifdef CONFIG_XPS
2763 netif_reset_xps_queues_gt(sb_dev, 0);
2764#endif
2765 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2766 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2767
2768 while (txq-- != &dev->_tx[0]) {
2769 if (txq->sb_dev == sb_dev)
2770 txq->sb_dev = NULL;
2771 }
2772}
2773EXPORT_SYMBOL(netdev_unbind_sb_channel);
2774
2775int netdev_bind_sb_channel_queue(struct net_device *dev,
2776 struct net_device *sb_dev,
2777 u8 tc, u16 count, u16 offset)
2778{
2779
2780 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2781 return -EINVAL;
2782
2783
2784 if ((offset + count) > dev->real_num_tx_queues)
2785 return -EINVAL;
2786
2787
2788 sb_dev->tc_to_txq[tc].count = count;
2789 sb_dev->tc_to_txq[tc].offset = offset;
2790
2791
2792
2793
2794 while (count--)
2795 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2796
2797 return 0;
2798}
2799EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2800
2801int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2802{
2803
2804 if (netif_is_multiqueue(dev))
2805 return -ENODEV;
2806
2807
2808
2809
2810
2811
2812 if (channel > S16_MAX)
2813 return -EINVAL;
2814
2815 dev->num_tc = -channel;
2816
2817 return 0;
2818}
2819EXPORT_SYMBOL(netdev_set_sb_channel);
2820
2821
2822
2823
2824
2825int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
2826{
2827 bool disabling;
2828 int rc;
2829
2830 disabling = txq < dev->real_num_tx_queues;
2831
2832 if (txq < 1 || txq > dev->num_tx_queues)
2833 return -EINVAL;
2834
2835 if (dev->reg_state == NETREG_REGISTERED ||
2836 dev->reg_state == NETREG_UNREGISTERING) {
2837 ASSERT_RTNL();
2838
2839 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2840 txq);
2841 if (rc)
2842 return rc;
2843
2844 if (dev->num_tc)
2845 netif_setup_tc(dev, txq);
2846
2847 dev_qdisc_change_real_num_tx(dev, txq);
2848
2849 dev->real_num_tx_queues = txq;
2850
2851 if (disabling) {
2852 synchronize_net();
2853 qdisc_reset_all_tx_gt(dev, txq);
2854#ifdef CONFIG_XPS
2855 netif_reset_xps_queues_gt(dev, txq);
2856#endif
2857 }
2858 } else {
2859 dev->real_num_tx_queues = txq;
2860 }
2861
2862 return 0;
2863}
2864EXPORT_SYMBOL(netif_set_real_num_tx_queues);
2865
2866#ifdef CONFIG_SYSFS
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2878{
2879 int rc;
2880
2881 if (rxq < 1 || rxq > dev->num_rx_queues)
2882 return -EINVAL;
2883
2884 if (dev->reg_state == NETREG_REGISTERED) {
2885 ASSERT_RTNL();
2886
2887 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2888 rxq);
2889 if (rc)
2890 return rc;
2891 }
2892
2893 dev->real_num_rx_queues = rxq;
2894 return 0;
2895}
2896EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2897#endif
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908int netif_set_real_num_queues(struct net_device *dev,
2909 unsigned int txq, unsigned int rxq)
2910{
2911 unsigned int old_rxq = dev->real_num_rx_queues;
2912 int err;
2913
2914 if (txq < 1 || txq > dev->num_tx_queues ||
2915 rxq < 1 || rxq > dev->num_rx_queues)
2916 return -EINVAL;
2917
2918
2919
2920
2921 if (rxq > dev->real_num_rx_queues) {
2922 err = netif_set_real_num_rx_queues(dev, rxq);
2923 if (err)
2924 return err;
2925 }
2926 if (txq > dev->real_num_tx_queues) {
2927 err = netif_set_real_num_tx_queues(dev, txq);
2928 if (err)
2929 goto undo_rx;
2930 }
2931 if (rxq < dev->real_num_rx_queues)
2932 WARN_ON(netif_set_real_num_rx_queues(dev, rxq));
2933 if (txq < dev->real_num_tx_queues)
2934 WARN_ON(netif_set_real_num_tx_queues(dev, txq));
2935
2936 return 0;
2937undo_rx:
2938 WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq));
2939 return err;
2940}
2941EXPORT_SYMBOL(netif_set_real_num_queues);
2942
2943
2944
2945
2946
2947
2948
2949int netif_get_num_default_rss_queues(void)
2950{
2951 return is_kdump_kernel() ?
2952 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
2953}
2954EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2955
2956static void __netif_reschedule(struct Qdisc *q)
2957{
2958 struct softnet_data *sd;
2959 unsigned long flags;
2960
2961 local_irq_save(flags);
2962 sd = this_cpu_ptr(&softnet_data);
2963 q->next_sched = NULL;
2964 *sd->output_queue_tailp = q;
2965 sd->output_queue_tailp = &q->next_sched;
2966 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2967 local_irq_restore(flags);
2968}
2969
2970void __netif_schedule(struct Qdisc *q)
2971{
2972 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2973 __netif_reschedule(q);
2974}
2975EXPORT_SYMBOL(__netif_schedule);
2976
2977struct dev_kfree_skb_cb {
2978 enum skb_free_reason reason;
2979};
2980
2981static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
2982{
2983 return (struct dev_kfree_skb_cb *)skb->cb;
2984}
2985
2986void netif_schedule_queue(struct netdev_queue *txq)
2987{
2988 rcu_read_lock();
2989 if (!netif_xmit_stopped(txq)) {
2990 struct Qdisc *q = rcu_dereference(txq->qdisc);
2991
2992 __netif_schedule(q);
2993 }
2994 rcu_read_unlock();
2995}
2996EXPORT_SYMBOL(netif_schedule_queue);
2997
2998void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2999{
3000 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3001 struct Qdisc *q;
3002
3003 rcu_read_lock();
3004 q = rcu_dereference(dev_queue->qdisc);
3005 __netif_schedule(q);
3006 rcu_read_unlock();
3007 }
3008}
3009EXPORT_SYMBOL(netif_tx_wake_queue);
3010
3011void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
3012{
3013 unsigned long flags;
3014
3015 if (unlikely(!skb))
3016 return;
3017
3018 if (likely(refcount_read(&skb->users) == 1)) {
3019 smp_rmb();
3020 refcount_set(&skb->users, 0);
3021 } else if (likely(!refcount_dec_and_test(&skb->users))) {
3022 return;
3023 }
3024 get_kfree_skb_cb(skb)->reason = reason;
3025 local_irq_save(flags);
3026 skb->next = __this_cpu_read(softnet_data.completion_queue);
3027 __this_cpu_write(softnet_data.completion_queue, skb);
3028 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3029 local_irq_restore(flags);
3030}
3031EXPORT_SYMBOL(__dev_kfree_skb_irq);
3032
3033void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
3034{
3035 if (in_hardirq() || irqs_disabled())
3036 __dev_kfree_skb_irq(skb, reason);
3037 else
3038 dev_kfree_skb(skb);
3039}
3040EXPORT_SYMBOL(__dev_kfree_skb_any);
3041
3042
3043
3044
3045
3046
3047
3048
3049void netif_device_detach(struct net_device *dev)
3050{
3051 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3052 netif_running(dev)) {
3053 netif_tx_stop_all_queues(dev);
3054 }
3055}
3056EXPORT_SYMBOL(netif_device_detach);
3057
3058
3059
3060
3061
3062
3063
3064void netif_device_attach(struct net_device *dev)
3065{
3066 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3067 netif_running(dev)) {
3068 netif_tx_wake_all_queues(dev);
3069 __netdev_watchdog_up(dev);
3070 }
3071}
3072EXPORT_SYMBOL(netif_device_attach);
3073
3074
3075
3076
3077
3078static u16 skb_tx_hash(const struct net_device *dev,
3079 const struct net_device *sb_dev,
3080 struct sk_buff *skb)
3081{
3082 u32 hash;
3083 u16 qoffset = 0;
3084 u16 qcount = dev->real_num_tx_queues;
3085
3086 if (dev->num_tc) {
3087 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3088
3089 qoffset = sb_dev->tc_to_txq[tc].offset;
3090 qcount = sb_dev->tc_to_txq[tc].count;
3091 if (unlikely(!qcount)) {
3092 net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n",
3093 sb_dev->name, qoffset, tc);
3094 qoffset = 0;
3095 qcount = dev->real_num_tx_queues;
3096 }
3097 }
3098
3099 if (skb_rx_queue_recorded(skb)) {
3100 hash = skb_get_rx_queue(skb);
3101 if (hash >= qoffset)
3102 hash -= qoffset;
3103 while (unlikely(hash >= qcount))
3104 hash -= qcount;
3105 return hash + qoffset;
3106 }
3107
3108 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3109}
3110
3111static void skb_warn_bad_offload(const struct sk_buff *skb)
3112{
3113 static const netdev_features_t null_features;
3114 struct net_device *dev = skb->dev;
3115 const char *name = "";
3116
3117 if (!net_ratelimit())
3118 return;
3119
3120 if (dev) {
3121 if (dev->dev.parent)
3122 name = dev_driver_string(dev->dev.parent);
3123 else
3124 name = netdev_name(dev);
3125 }
3126 skb_dump(KERN_WARNING, skb, false);
3127 WARN(1, "%s: caps=(%pNF, %pNF)\n",
3128 name, dev ? &dev->features : &null_features,
3129 skb->sk ? &skb->sk->sk_route_caps : &null_features);
3130}
3131
3132
3133
3134
3135
3136int skb_checksum_help(struct sk_buff *skb)
3137{
3138 __wsum csum;
3139 int ret = 0, offset;
3140
3141 if (skb->ip_summed == CHECKSUM_COMPLETE)
3142 goto out_set_summed;
3143
3144 if (unlikely(skb_is_gso(skb))) {
3145 skb_warn_bad_offload(skb);
3146 return -EINVAL;
3147 }
3148
3149
3150
3151
3152 if (skb_has_shared_frag(skb)) {
3153 ret = __skb_linearize(skb);
3154 if (ret)
3155 goto out;
3156 }
3157
3158 offset = skb_checksum_start_offset(skb);
3159 BUG_ON(offset >= skb_headlen(skb));
3160 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3161
3162 offset += skb->csum_offset;
3163 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3164
3165 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3166 if (ret)
3167 goto out;
3168
3169 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
3170out_set_summed:
3171 skb->ip_summed = CHECKSUM_NONE;
3172out:
3173 return ret;
3174}
3175EXPORT_SYMBOL(skb_checksum_help);
3176
3177int skb_crc32c_csum_help(struct sk_buff *skb)
3178{
3179 __le32 crc32c_csum;
3180 int ret = 0, offset, start;
3181
3182 if (skb->ip_summed != CHECKSUM_PARTIAL)
3183 goto out;
3184
3185 if (unlikely(skb_is_gso(skb)))
3186 goto out;
3187
3188
3189
3190
3191 if (unlikely(skb_has_shared_frag(skb))) {
3192 ret = __skb_linearize(skb);
3193 if (ret)
3194 goto out;
3195 }
3196 start = skb_checksum_start_offset(skb);
3197 offset = start + offsetof(struct sctphdr, checksum);
3198 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3199 ret = -EINVAL;
3200 goto out;
3201 }
3202
3203 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3204 if (ret)
3205 goto out;
3206
3207 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3208 skb->len - start, ~(__u32)0,
3209 crc32c_csum_stub));
3210 *(__le32 *)(skb->data + offset) = crc32c_csum;
3211 skb->ip_summed = CHECKSUM_NONE;
3212 skb->csum_not_inet = 0;
3213out:
3214 return ret;
3215}
3216
3217__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
3218{
3219 __be16 type = skb->protocol;
3220
3221
3222 if (type == htons(ETH_P_TEB)) {
3223 struct ethhdr *eth;
3224
3225 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3226 return 0;
3227
3228 eth = (struct ethhdr *)skb->data;
3229 type = eth->h_proto;
3230 }
3231
3232 return __vlan_get_protocol(skb, type, depth);
3233}
3234
3235
3236
3237static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3238{
3239 if (tx_path)
3240 return skb->ip_summed != CHECKSUM_PARTIAL &&
3241 skb->ip_summed != CHECKSUM_UNNECESSARY;
3242
3243 return skb->ip_summed == CHECKSUM_NONE;
3244}
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3260 netdev_features_t features, bool tx_path)
3261{
3262 struct sk_buff *segs;
3263
3264 if (unlikely(skb_needs_check(skb, tx_path))) {
3265 int err;
3266
3267
3268 err = skb_cow_head(skb, 0);
3269 if (err < 0)
3270 return ERR_PTR(err);
3271 }
3272
3273
3274
3275
3276
3277 if (features & NETIF_F_GSO_PARTIAL) {
3278 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3279 struct net_device *dev = skb->dev;
3280
3281 partial_features |= dev->features & dev->gso_partial_features;
3282 if (!skb_gso_ok(skb, features | partial_features))
3283 features &= ~NETIF_F_GSO_PARTIAL;
3284 }
3285
3286 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
3287 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3288
3289 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
3290 SKB_GSO_CB(skb)->encap_level = 0;
3291
3292 skb_reset_mac_header(skb);
3293 skb_reset_mac_len(skb);
3294
3295 segs = skb_mac_gso_segment(skb, features);
3296
3297 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
3298 skb_warn_bad_offload(skb);
3299
3300 return segs;
3301}
3302EXPORT_SYMBOL(__skb_gso_segment);
3303
3304
3305#ifdef CONFIG_BUG
3306static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
3307{
3308 netdev_err(dev, "hw csum failure\n");
3309 skb_dump(KERN_ERR, skb, true);
3310 dump_stack();
3311}
3312
3313void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
3314{
3315 DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb);
3316}
3317EXPORT_SYMBOL(netdev_rx_csum_fault);
3318#endif
3319
3320
3321static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
3322{
3323#ifdef CONFIG_HIGHMEM
3324 int i;
3325
3326 if (!(dev->features & NETIF_F_HIGHDMA)) {
3327 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3328 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3329
3330 if (PageHighMem(skb_frag_page(frag)))
3331 return 1;
3332 }
3333 }
3334#endif
3335 return 0;
3336}
3337
3338
3339
3340
3341#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3342static netdev_features_t net_mpls_features(struct sk_buff *skb,
3343 netdev_features_t features,
3344 __be16 type)
3345{
3346 if (eth_p_mpls(type))
3347 features &= skb->dev->mpls_features;
3348
3349 return features;
3350}
3351#else
3352static netdev_features_t net_mpls_features(struct sk_buff *skb,
3353 netdev_features_t features,
3354 __be16 type)
3355{
3356 return features;
3357}
3358#endif
3359
3360static netdev_features_t harmonize_features(struct sk_buff *skb,
3361 netdev_features_t features)
3362{
3363 __be16 type;
3364
3365 type = skb_network_protocol(skb, NULL);
3366 features = net_mpls_features(skb, features, type);
3367
3368 if (skb->ip_summed != CHECKSUM_NONE &&
3369 !can_checksum_protocol(features, type)) {
3370 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3371 }
3372 if (illegal_highdma(skb->dev, skb))
3373 features &= ~NETIF_F_SG;
3374
3375 return features;
3376}
3377
3378netdev_features_t passthru_features_check(struct sk_buff *skb,
3379 struct net_device *dev,
3380 netdev_features_t features)
3381{
3382 return features;
3383}
3384EXPORT_SYMBOL(passthru_features_check);
3385
3386static netdev_features_t dflt_features_check(struct sk_buff *skb,
3387 struct net_device *dev,
3388 netdev_features_t features)
3389{
3390 return vlan_features_check(skb, features);
3391}
3392
3393static netdev_features_t gso_features_check(const struct sk_buff *skb,
3394 struct net_device *dev,
3395 netdev_features_t features)
3396{
3397 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3398
3399 if (gso_segs > READ_ONCE(dev->gso_max_segs))
3400 return features & ~NETIF_F_GSO_MASK;
3401
3402 if (!skb_shinfo(skb)->gso_type) {
3403 skb_warn_bad_offload(skb);
3404 return features & ~NETIF_F_GSO_MASK;
3405 }
3406
3407
3408
3409
3410
3411
3412
3413 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3414 features &= ~dev->gso_partial_features;
3415
3416
3417
3418
3419 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3420 struct iphdr *iph = skb->encapsulation ?
3421 inner_ip_hdr(skb) : ip_hdr(skb);
3422
3423 if (!(iph->frag_off & htons(IP_DF)))
3424 features &= ~NETIF_F_TSO_MANGLEID;
3425 }
3426
3427 return features;
3428}
3429
3430netdev_features_t netif_skb_features(struct sk_buff *skb)
3431{
3432 struct net_device *dev = skb->dev;
3433 netdev_features_t features = dev->features;
3434
3435 if (skb_is_gso(skb))
3436 features = gso_features_check(skb, dev, features);
3437
3438
3439
3440
3441
3442 if (skb->encapsulation)
3443 features &= dev->hw_enc_features;
3444
3445 if (skb_vlan_tagged(skb))
3446 features = netdev_intersect_features(features,
3447 dev->vlan_features |
3448 NETIF_F_HW_VLAN_CTAG_TX |
3449 NETIF_F_HW_VLAN_STAG_TX);
3450
3451 if (dev->netdev_ops->ndo_features_check)
3452 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3453 features);
3454 else
3455 features &= dflt_features_check(skb, dev, features);
3456
3457 return harmonize_features(skb, features);
3458}
3459EXPORT_SYMBOL(netif_skb_features);
3460
3461static int xmit_one(struct sk_buff *skb, struct net_device *dev,
3462 struct netdev_queue *txq, bool more)
3463{
3464 unsigned int len;
3465 int rc;
3466
3467 if (dev_nit_active(dev))
3468 dev_queue_xmit_nit(skb, dev);
3469
3470 len = skb->len;
3471 PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies);
3472 trace_net_dev_start_xmit(skb, dev);
3473 rc = netdev_start_xmit(skb, dev, txq, more);
3474 trace_net_dev_xmit(skb, rc, dev, len);
3475
3476 return rc;
3477}
3478
3479struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3480 struct netdev_queue *txq, int *ret)
3481{
3482 struct sk_buff *skb = first;
3483 int rc = NETDEV_TX_OK;
3484
3485 while (skb) {
3486 struct sk_buff *next = skb->next;
3487
3488 skb_mark_not_on_list(skb);
3489 rc = xmit_one(skb, dev, txq, next != NULL);
3490 if (unlikely(!dev_xmit_complete(rc))) {
3491 skb->next = next;
3492 goto out;
3493 }
3494
3495 skb = next;
3496 if (netif_tx_queue_stopped(txq) && skb) {
3497 rc = NETDEV_TX_BUSY;
3498 break;
3499 }
3500 }
3501
3502out:
3503 *ret = rc;
3504 return skb;
3505}
3506
3507static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3508 netdev_features_t features)
3509{
3510 if (skb_vlan_tag_present(skb) &&
3511 !vlan_hw_offload_capable(features, skb->vlan_proto))
3512 skb = __vlan_hwaccel_push_inside(skb);
3513 return skb;
3514}
3515
3516int skb_csum_hwoffload_help(struct sk_buff *skb,
3517 const netdev_features_t features)
3518{
3519 if (unlikely(skb_csum_is_sctp(skb)))
3520 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3521 skb_crc32c_csum_help(skb);
3522
3523 if (features & NETIF_F_HW_CSUM)
3524 return 0;
3525
3526 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3527 switch (skb->csum_offset) {
3528 case offsetof(struct tcphdr, check):
3529 case offsetof(struct udphdr, check):
3530 return 0;
3531 }
3532 }
3533
3534 return skb_checksum_help(skb);
3535}
3536EXPORT_SYMBOL(skb_csum_hwoffload_help);
3537
3538static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
3539{
3540 netdev_features_t features;
3541
3542 features = netif_skb_features(skb);
3543 skb = validate_xmit_vlan(skb, features);
3544 if (unlikely(!skb))
3545 goto out_null;
3546
3547 skb = sk_validate_xmit_skb(skb, dev);
3548 if (unlikely(!skb))
3549 goto out_null;
3550
3551 if (netif_needs_gso(skb, features)) {
3552 struct sk_buff *segs;
3553
3554 segs = skb_gso_segment(skb, features);
3555 if (IS_ERR(segs)) {
3556 goto out_kfree_skb;
3557 } else if (segs) {
3558 consume_skb(skb);
3559 skb = segs;
3560 }
3561 } else {
3562 if (skb_needs_linearize(skb, features) &&
3563 __skb_linearize(skb))
3564 goto out_kfree_skb;
3565
3566
3567
3568
3569
3570 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3571 if (skb->encapsulation)
3572 skb_set_inner_transport_header(skb,
3573 skb_checksum_start_offset(skb));
3574 else
3575 skb_set_transport_header(skb,
3576 skb_checksum_start_offset(skb));
3577 if (skb_csum_hwoffload_help(skb, features))
3578 goto out_kfree_skb;
3579 }
3580 }
3581
3582 skb = validate_xmit_xfrm(skb, features, again);
3583
3584 return skb;
3585
3586out_kfree_skb:
3587 kfree_skb(skb);
3588out_null:
3589 atomic_long_inc(&dev->tx_dropped);
3590 return NULL;
3591}
3592
3593struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
3594{
3595 struct sk_buff *next, *head = NULL, *tail;
3596
3597 for (; skb != NULL; skb = next) {
3598 next = skb->next;
3599 skb_mark_not_on_list(skb);
3600
3601
3602 skb->prev = skb;
3603
3604 skb = validate_xmit_skb(skb, dev, again);
3605 if (!skb)
3606 continue;
3607
3608 if (!head)
3609 head = skb;
3610 else
3611 tail->next = skb;
3612
3613
3614
3615 tail = skb->prev;
3616 }
3617 return head;
3618}
3619EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
3620
3621static void qdisc_pkt_len_init(struct sk_buff *skb)
3622{
3623 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3624
3625 qdisc_skb_cb(skb)->pkt_len = skb->len;
3626
3627
3628
3629
3630 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
3631 unsigned int hdr_len;
3632 u16 gso_segs = shinfo->gso_segs;
3633
3634
3635 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3636
3637
3638 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3639 const struct tcphdr *th;
3640 struct tcphdr _tcphdr;
3641
3642 th = skb_header_pointer(skb, skb_transport_offset(skb),
3643 sizeof(_tcphdr), &_tcphdr);
3644 if (likely(th))
3645 hdr_len += __tcp_hdrlen(th);
3646 } else {
3647 struct udphdr _udphdr;
3648
3649 if (skb_header_pointer(skb, skb_transport_offset(skb),
3650 sizeof(_udphdr), &_udphdr))
3651 hdr_len += sizeof(struct udphdr);
3652 }
3653
3654 if (shinfo->gso_type & SKB_GSO_DODGY)
3655 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3656 shinfo->gso_size);
3657
3658 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
3659 }
3660}
3661
3662static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q,
3663 struct sk_buff **to_free,
3664 struct netdev_queue *txq)
3665{
3666 int rc;
3667
3668 rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK;
3669 if (rc == NET_XMIT_SUCCESS)
3670 trace_qdisc_enqueue(q, txq, skb);
3671 return rc;
3672}
3673
3674static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3675 struct net_device *dev,
3676 struct netdev_queue *txq)
3677{
3678 spinlock_t *root_lock = qdisc_lock(q);
3679 struct sk_buff *to_free = NULL;
3680 bool contended;
3681 int rc;
3682
3683 qdisc_calculate_pkt_len(skb, q);
3684
3685 if (q->flags & TCQ_F_NOLOCK) {
3686 if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) &&
3687 qdisc_run_begin(q)) {
3688
3689
3690
3691 if (unlikely(!nolock_qdisc_is_empty(q))) {
3692 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
3693 __qdisc_run(q);
3694 qdisc_run_end(q);
3695
3696 goto no_lock_out;
3697 }
3698
3699 qdisc_bstats_cpu_update(q, skb);
3700 if (sch_direct_xmit(skb, q, dev, txq, NULL, true) &&
3701 !nolock_qdisc_is_empty(q))
3702 __qdisc_run(q);
3703
3704 qdisc_run_end(q);
3705 return NET_XMIT_SUCCESS;
3706 }
3707
3708 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
3709 qdisc_run(q);
3710
3711no_lock_out:
3712 if (unlikely(to_free))
3713 kfree_skb_list(to_free);
3714 return rc;
3715 }
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727 contended = qdisc_is_running(q) || IS_ENABLED(CONFIG_PREEMPT_RT);
3728 if (unlikely(contended))
3729 spin_lock(&q->busylock);
3730
3731 spin_lock(root_lock);
3732 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3733 __qdisc_drop(skb, &to_free);
3734 rc = NET_XMIT_DROP;
3735 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
3736 qdisc_run_begin(q)) {
3737
3738
3739
3740
3741
3742
3743 qdisc_bstats_update(q, skb);
3744
3745 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
3746 if (unlikely(contended)) {
3747 spin_unlock(&q->busylock);
3748 contended = false;
3749 }
3750 __qdisc_run(q);
3751 }
3752
3753 qdisc_run_end(q);
3754 rc = NET_XMIT_SUCCESS;
3755 } else {
3756 rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
3757 if (qdisc_run_begin(q)) {
3758 if (unlikely(contended)) {
3759 spin_unlock(&q->busylock);
3760 contended = false;
3761 }
3762 __qdisc_run(q);
3763 qdisc_run_end(q);
3764 }
3765 }
3766 spin_unlock(root_lock);
3767 if (unlikely(to_free))
3768 kfree_skb_list(to_free);
3769 if (unlikely(contended))
3770 spin_unlock(&q->busylock);
3771 return rc;
3772}
3773
3774#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
3775static void skb_update_prio(struct sk_buff *skb)
3776{
3777 const struct netprio_map *map;
3778 const struct sock *sk;
3779 unsigned int prioidx;
3780
3781 if (skb->priority)
3782 return;
3783 map = rcu_dereference_bh(skb->dev->priomap);
3784 if (!map)
3785 return;
3786 sk = skb_to_full_sk(skb);
3787 if (!sk)
3788 return;
3789
3790 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3791
3792 if (prioidx < map->priomap_len)
3793 skb->priority = map->priomap[prioidx];
3794}
3795#else
3796#define skb_update_prio(skb)
3797#endif
3798
3799
3800
3801
3802
3803
3804
3805int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
3806{
3807 skb_reset_mac_header(skb);
3808 __skb_pull(skb, skb_network_offset(skb));
3809 skb->pkt_type = PACKET_LOOPBACK;
3810 if (skb->ip_summed == CHECKSUM_NONE)
3811 skb->ip_summed = CHECKSUM_UNNECESSARY;
3812 WARN_ON(!skb_dst(skb));
3813 skb_dst_force(skb);
3814 netif_rx_ni(skb);
3815 return 0;
3816}
3817EXPORT_SYMBOL(dev_loopback_xmit);
3818
3819#ifdef CONFIG_NET_EGRESS
3820static struct sk_buff *
3821sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3822{
3823#ifdef CONFIG_NET_CLS_ACT
3824 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
3825 struct tcf_result cl_res;
3826
3827 if (!miniq)
3828 return skb;
3829
3830
3831 tc_skb_cb(skb)->mru = 0;
3832 tc_skb_cb(skb)->post_ct = false;
3833 mini_qdisc_bstats_cpu_update(miniq, skb);
3834
3835 switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) {
3836 case TC_ACT_OK:
3837 case TC_ACT_RECLASSIFY:
3838 skb->tc_index = TC_H_MIN(cl_res.classid);
3839 break;
3840 case TC_ACT_SHOT:
3841 mini_qdisc_qstats_cpu_drop(miniq);
3842 *ret = NET_XMIT_DROP;
3843 kfree_skb(skb);
3844 return NULL;
3845 case TC_ACT_STOLEN:
3846 case TC_ACT_QUEUED:
3847 case TC_ACT_TRAP:
3848 *ret = NET_XMIT_SUCCESS;
3849 consume_skb(skb);
3850 return NULL;
3851 case TC_ACT_REDIRECT:
3852
3853 skb_do_redirect(skb);
3854 *ret = NET_XMIT_SUCCESS;
3855 return NULL;
3856 default:
3857 break;
3858 }
3859#endif
3860
3861 return skb;
3862}
3863#endif
3864
3865#ifdef CONFIG_XPS
3866static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3867 struct xps_dev_maps *dev_maps, unsigned int tci)
3868{
3869 int tc = netdev_get_prio_tc_map(dev, skb->priority);
3870 struct xps_map *map;
3871 int queue_index = -1;
3872
3873 if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids)
3874 return queue_index;
3875
3876 tci *= dev_maps->num_tc;
3877 tci += tc;
3878
3879 map = rcu_dereference(dev_maps->attr_map[tci]);
3880 if (map) {
3881 if (map->len == 1)
3882 queue_index = map->queues[0];
3883 else
3884 queue_index = map->queues[reciprocal_scale(
3885 skb_get_hash(skb), map->len)];
3886 if (unlikely(queue_index >= dev->real_num_tx_queues))
3887 queue_index = -1;
3888 }
3889 return queue_index;
3890}
3891#endif
3892
3893static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3894 struct sk_buff *skb)
3895{
3896#ifdef CONFIG_XPS
3897 struct xps_dev_maps *dev_maps;
3898 struct sock *sk = skb->sk;
3899 int queue_index = -1;
3900
3901 if (!static_key_false(&xps_needed))
3902 return -1;
3903
3904 rcu_read_lock();
3905 if (!static_key_false(&xps_rxqs_needed))
3906 goto get_cpus_map;
3907
3908 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]);
3909 if (dev_maps) {
3910 int tci = sk_rx_queue_get(sk);
3911
3912 if (tci >= 0)
3913 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3914 tci);
3915 }
3916
3917get_cpus_map:
3918 if (queue_index < 0) {
3919 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]);
3920 if (dev_maps) {
3921 unsigned int tci = skb->sender_cpu - 1;
3922
3923 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3924 tci);
3925 }
3926 }
3927 rcu_read_unlock();
3928
3929 return queue_index;
3930#else
3931 return -1;
3932#endif
3933}
3934
3935u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
3936 struct net_device *sb_dev)
3937{
3938 return 0;
3939}
3940EXPORT_SYMBOL(dev_pick_tx_zero);
3941
3942u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
3943 struct net_device *sb_dev)
3944{
3945 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3946}
3947EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3948
3949u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3950 struct net_device *sb_dev)
3951{
3952 struct sock *sk = skb->sk;
3953 int queue_index = sk_tx_queue_get(sk);
3954
3955 sb_dev = sb_dev ? : dev;
3956
3957 if (queue_index < 0 || skb->ooo_okay ||
3958 queue_index >= dev->real_num_tx_queues) {
3959 int new_index = get_xps_queue(dev, sb_dev, skb);
3960
3961 if (new_index < 0)
3962 new_index = skb_tx_hash(dev, sb_dev, skb);
3963
3964 if (queue_index != new_index && sk &&
3965 sk_fullsock(sk) &&
3966 rcu_access_pointer(sk->sk_dst_cache))
3967 sk_tx_queue_set(sk, new_index);
3968
3969 queue_index = new_index;
3970 }
3971
3972 return queue_index;
3973}
3974EXPORT_SYMBOL(netdev_pick_tx);
3975
3976struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
3977 struct sk_buff *skb,
3978 struct net_device *sb_dev)
3979{
3980 int queue_index = 0;
3981
3982#ifdef CONFIG_XPS
3983 u32 sender_cpu = skb->sender_cpu - 1;
3984
3985 if (sender_cpu >= (u32)NR_CPUS)
3986 skb->sender_cpu = raw_smp_processor_id() + 1;
3987#endif
3988
3989 if (dev->real_num_tx_queues != 1) {
3990 const struct net_device_ops *ops = dev->netdev_ops;
3991
3992 if (ops->ndo_select_queue)
3993 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
3994 else
3995 queue_index = netdev_pick_tx(dev, skb, sb_dev);
3996
3997 queue_index = netdev_cap_txqueue(dev, queue_index);
3998 }
3999
4000 skb_set_queue_mapping(skb, queue_index);
4001 return netdev_get_tx_queue(dev, queue_index);
4002}
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
4031{
4032 struct net_device *dev = skb->dev;
4033 struct netdev_queue *txq;
4034 struct Qdisc *q;
4035 int rc = -ENOMEM;
4036 bool again = false;
4037
4038 skb_reset_mac_header(skb);
4039
4040 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4041 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED);
4042
4043
4044
4045
4046 rcu_read_lock_bh();
4047
4048 skb_update_prio(skb);
4049
4050 qdisc_pkt_len_init(skb);
4051#ifdef CONFIG_NET_CLS_ACT
4052 skb->tc_at_ingress = 0;
4053#endif
4054#ifdef CONFIG_NET_EGRESS
4055 if (static_branch_unlikely(&egress_needed_key)) {
4056 if (nf_hook_egress_active()) {
4057 skb = nf_hook_egress(skb, &rc, dev);
4058 if (!skb)
4059 goto out;
4060 }
4061 nf_skip_egress(skb, true);
4062 skb = sch_handle_egress(skb, &rc, dev);
4063 if (!skb)
4064 goto out;
4065 nf_skip_egress(skb, false);
4066 }
4067#endif
4068
4069
4070
4071 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4072 skb_dst_drop(skb);
4073 else
4074 skb_dst_force(skb);
4075
4076 txq = netdev_core_pick_tx(dev, skb, sb_dev);
4077 q = rcu_dereference_bh(txq->qdisc);
4078
4079 trace_net_dev_queue(skb);
4080 if (q->enqueue) {
4081 rc = __dev_xmit_skb(skb, q, dev, txq);
4082 goto out;
4083 }
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097 if (dev->flags & IFF_UP) {
4098 int cpu = smp_processor_id();
4099
4100
4101
4102
4103 if (READ_ONCE(txq->xmit_lock_owner) != cpu) {
4104 if (dev_xmit_recursion())
4105 goto recursion_alert;
4106
4107 skb = validate_xmit_skb(skb, dev, &again);
4108 if (!skb)
4109 goto out;
4110
4111 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
4112 HARD_TX_LOCK(dev, txq, cpu);
4113
4114 if (!netif_xmit_stopped(txq)) {
4115 dev_xmit_recursion_inc();
4116 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
4117 dev_xmit_recursion_dec();
4118 if (dev_xmit_complete(rc)) {
4119 HARD_TX_UNLOCK(dev, txq);
4120 goto out;
4121 }
4122 }
4123 HARD_TX_UNLOCK(dev, txq);
4124 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4125 dev->name);
4126 } else {
4127
4128
4129
4130recursion_alert:
4131 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4132 dev->name);
4133 }
4134 }
4135
4136 rc = -ENETDOWN;
4137 rcu_read_unlock_bh();
4138
4139 atomic_long_inc(&dev->tx_dropped);
4140 kfree_skb_list(skb);
4141 return rc;
4142out:
4143 rcu_read_unlock_bh();
4144 return rc;
4145}
4146
4147int dev_queue_xmit(struct sk_buff *skb)
4148{
4149 return __dev_queue_xmit(skb, NULL);
4150}
4151EXPORT_SYMBOL(dev_queue_xmit);
4152
4153int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
4154{
4155 return __dev_queue_xmit(skb, sb_dev);
4156}
4157EXPORT_SYMBOL(dev_queue_xmit_accel);
4158
4159int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4160{
4161 struct net_device *dev = skb->dev;
4162 struct sk_buff *orig_skb = skb;
4163 struct netdev_queue *txq;
4164 int ret = NETDEV_TX_BUSY;
4165 bool again = false;
4166
4167 if (unlikely(!netif_running(dev) ||
4168 !netif_carrier_ok(dev)))
4169 goto drop;
4170
4171 skb = validate_xmit_skb_list(skb, dev, &again);
4172 if (skb != orig_skb)
4173 goto drop;
4174
4175 skb_set_queue_mapping(skb, queue_id);
4176 txq = skb_get_tx_queue(dev, skb);
4177 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
4178
4179 local_bh_disable();
4180
4181 dev_xmit_recursion_inc();
4182 HARD_TX_LOCK(dev, txq, smp_processor_id());
4183 if (!netif_xmit_frozen_or_drv_stopped(txq))
4184 ret = netdev_start_xmit(skb, dev, txq, false);
4185 HARD_TX_UNLOCK(dev, txq);
4186 dev_xmit_recursion_dec();
4187
4188 local_bh_enable();
4189 return ret;
4190drop:
4191 atomic_long_inc(&dev->tx_dropped);
4192 kfree_skb_list(skb);
4193 return NET_XMIT_DROP;
4194}
4195EXPORT_SYMBOL(__dev_direct_xmit);
4196
4197
4198
4199
4200
4201int netdev_max_backlog __read_mostly = 1000;
4202EXPORT_SYMBOL(netdev_max_backlog);
4203
4204int netdev_tstamp_prequeue __read_mostly = 1;
4205int netdev_budget __read_mostly = 300;
4206
4207unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
4208int weight_p __read_mostly = 64;
4209int dev_weight_rx_bias __read_mostly = 1;
4210int dev_weight_tx_bias __read_mostly = 1;
4211int dev_rx_weight __read_mostly = 64;
4212int dev_tx_weight __read_mostly = 64;
4213
4214
4215static inline void ____napi_schedule(struct softnet_data *sd,
4216 struct napi_struct *napi)
4217{
4218 struct task_struct *thread;
4219
4220 if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
4221
4222
4223
4224
4225
4226
4227 thread = READ_ONCE(napi->thread);
4228 if (thread) {
4229
4230
4231
4232
4233
4234 if (READ_ONCE(thread->__state) != TASK_INTERRUPTIBLE)
4235 set_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
4236 wake_up_process(thread);
4237 return;
4238 }
4239 }
4240
4241 list_add_tail(&napi->poll_list, &sd->poll_list);
4242 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4243}
4244
4245#ifdef CONFIG_RPS
4246
4247
4248struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
4249EXPORT_SYMBOL(rps_sock_flow_table);
4250u32 rps_cpu_mask __read_mostly;
4251EXPORT_SYMBOL(rps_cpu_mask);
4252
4253struct static_key_false rps_needed __read_mostly;
4254EXPORT_SYMBOL(rps_needed);
4255struct static_key_false rfs_needed __read_mostly;
4256EXPORT_SYMBOL(rfs_needed);
4257
4258static struct rps_dev_flow *
4259set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4260 struct rps_dev_flow *rflow, u16 next_cpu)
4261{
4262 if (next_cpu < nr_cpu_ids) {
4263#ifdef CONFIG_RFS_ACCEL
4264 struct netdev_rx_queue *rxqueue;
4265 struct rps_dev_flow_table *flow_table;
4266 struct rps_dev_flow *old_rflow;
4267 u32 flow_id;
4268 u16 rxq_index;
4269 int rc;
4270
4271
4272 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4273 !(dev->features & NETIF_F_NTUPLE))
4274 goto out;
4275 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4276 if (rxq_index == skb_get_rx_queue(skb))
4277 goto out;
4278
4279 rxqueue = dev->_rx + rxq_index;
4280 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4281 if (!flow_table)
4282 goto out;
4283 flow_id = skb_get_hash(skb) & flow_table->mask;
4284 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4285 rxq_index, flow_id);
4286 if (rc < 0)
4287 goto out;
4288 old_rflow = rflow;
4289 rflow = &flow_table->flows[flow_id];
4290 rflow->filter = rc;
4291 if (old_rflow->filter == rflow->filter)
4292 old_rflow->filter = RPS_NO_FILTER;
4293 out:
4294#endif
4295 rflow->last_qtail =
4296 per_cpu(softnet_data, next_cpu).input_queue_head;
4297 }
4298
4299 rflow->cpu = next_cpu;
4300 return rflow;
4301}
4302
4303
4304
4305
4306
4307
4308static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4309 struct rps_dev_flow **rflowp)
4310{
4311 const struct rps_sock_flow_table *sock_flow_table;
4312 struct netdev_rx_queue *rxqueue = dev->_rx;
4313 struct rps_dev_flow_table *flow_table;
4314 struct rps_map *map;
4315 int cpu = -1;
4316 u32 tcpu;
4317 u32 hash;
4318
4319 if (skb_rx_queue_recorded(skb)) {
4320 u16 index = skb_get_rx_queue(skb);
4321
4322 if (unlikely(index >= dev->real_num_rx_queues)) {
4323 WARN_ONCE(dev->real_num_rx_queues > 1,
4324 "%s received packet on queue %u, but number "
4325 "of RX queues is %u\n",
4326 dev->name, index, dev->real_num_rx_queues);
4327 goto done;
4328 }
4329 rxqueue += index;
4330 }
4331
4332
4333
4334 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4335 map = rcu_dereference(rxqueue->rps_map);
4336 if (!flow_table && !map)
4337 goto done;
4338
4339 skb_reset_network_header(skb);
4340 hash = skb_get_hash(skb);
4341 if (!hash)
4342 goto done;
4343
4344 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4345 if (flow_table && sock_flow_table) {
4346 struct rps_dev_flow *rflow;
4347 u32 next_cpu;
4348 u32 ident;
4349
4350
4351 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4352 if ((ident ^ hash) & ~rps_cpu_mask)
4353 goto try_rps;
4354
4355 next_cpu = ident & rps_cpu_mask;
4356
4357
4358
4359
4360 rflow = &flow_table->flows[hash & flow_table->mask];
4361 tcpu = rflow->cpu;
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374 if (unlikely(tcpu != next_cpu) &&
4375 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
4376 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
4377 rflow->last_qtail)) >= 0)) {
4378 tcpu = next_cpu;
4379 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
4380 }
4381
4382 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
4383 *rflowp = rflow;
4384 cpu = tcpu;
4385 goto done;
4386 }
4387 }
4388
4389try_rps:
4390
4391 if (map) {
4392 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
4393 if (cpu_online(tcpu)) {
4394 cpu = tcpu;
4395 goto done;
4396 }
4397 }
4398
4399done:
4400 return cpu;
4401}
4402
4403#ifdef CONFIG_RFS_ACCEL
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4417 u32 flow_id, u16 filter_id)
4418{
4419 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4420 struct rps_dev_flow_table *flow_table;
4421 struct rps_dev_flow *rflow;
4422 bool expire = true;
4423 unsigned int cpu;
4424
4425 rcu_read_lock();
4426 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4427 if (flow_table && flow_id <= flow_table->mask) {
4428 rflow = &flow_table->flows[flow_id];
4429 cpu = READ_ONCE(rflow->cpu);
4430 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
4431 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4432 rflow->last_qtail) <
4433 (int)(10 * flow_table->mask)))
4434 expire = false;
4435 }
4436 rcu_read_unlock();
4437 return expire;
4438}
4439EXPORT_SYMBOL(rps_may_expire_flow);
4440
4441#endif
4442
4443
4444static void rps_trigger_softirq(void *data)
4445{
4446 struct softnet_data *sd = data;
4447
4448 ____napi_schedule(sd, &sd->backlog);
4449 sd->received_rps++;
4450}
4451
4452#endif
4453
4454
4455
4456
4457
4458
4459static int rps_ipi_queued(struct softnet_data *sd)
4460{
4461#ifdef CONFIG_RPS
4462 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
4463
4464 if (sd != mysd) {
4465 sd->rps_ipi_next = mysd->rps_ipi_list;
4466 mysd->rps_ipi_list = sd;
4467
4468 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4469 return 1;
4470 }
4471#endif
4472 return 0;
4473}
4474
4475#ifdef CONFIG_NET_FLOW_LIMIT
4476int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4477#endif
4478
4479static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4480{
4481#ifdef CONFIG_NET_FLOW_LIMIT
4482 struct sd_flow_limit *fl;
4483 struct softnet_data *sd;
4484 unsigned int old_flow, new_flow;
4485
4486 if (qlen < (netdev_max_backlog >> 1))
4487 return false;
4488
4489 sd = this_cpu_ptr(&softnet_data);
4490
4491 rcu_read_lock();
4492 fl = rcu_dereference(sd->flow_limit);
4493 if (fl) {
4494 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
4495 old_flow = fl->history[fl->history_head];
4496 fl->history[fl->history_head] = new_flow;
4497
4498 fl->history_head++;
4499 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4500
4501 if (likely(fl->buckets[old_flow]))
4502 fl->buckets[old_flow]--;
4503
4504 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4505 fl->count++;
4506 rcu_read_unlock();
4507 return true;
4508 }
4509 }
4510 rcu_read_unlock();
4511#endif
4512 return false;
4513}
4514
4515
4516
4517
4518
4519static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4520 unsigned int *qtail)
4521{
4522 struct softnet_data *sd;
4523 unsigned long flags;
4524 unsigned int qlen;
4525
4526 sd = &per_cpu(softnet_data, cpu);
4527
4528 local_irq_save(flags);
4529
4530 rps_lock(sd);
4531 if (!netif_running(skb->dev))
4532 goto drop;
4533 qlen = skb_queue_len(&sd->input_pkt_queue);
4534 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
4535 if (qlen) {
4536enqueue:
4537 __skb_queue_tail(&sd->input_pkt_queue, skb);
4538 input_queue_tail_incr_save(sd, qtail);
4539 rps_unlock(sd);
4540 local_irq_restore(flags);
4541 return NET_RX_SUCCESS;
4542 }
4543
4544
4545
4546
4547 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
4548 if (!rps_ipi_queued(sd))
4549 ____napi_schedule(sd, &sd->backlog);
4550 }
4551 goto enqueue;
4552 }
4553
4554drop:
4555 sd->dropped++;
4556 rps_unlock(sd);
4557
4558 local_irq_restore(flags);
4559
4560 atomic_long_inc(&skb->dev->rx_dropped);
4561 kfree_skb(skb);
4562 return NET_RX_DROP;
4563}
4564
4565static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4566{
4567 struct net_device *dev = skb->dev;
4568 struct netdev_rx_queue *rxqueue;
4569
4570 rxqueue = dev->_rx;
4571
4572 if (skb_rx_queue_recorded(skb)) {
4573 u16 index = skb_get_rx_queue(skb);
4574
4575 if (unlikely(index >= dev->real_num_rx_queues)) {
4576 WARN_ONCE(dev->real_num_rx_queues > 1,
4577 "%s received packet on queue %u, but number "
4578 "of RX queues is %u\n",
4579 dev->name, index, dev->real_num_rx_queues);
4580
4581 return rxqueue;
4582 }
4583 rxqueue += index;
4584 }
4585 return rxqueue;
4586}
4587
4588u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
4589 struct bpf_prog *xdp_prog)
4590{
4591 void *orig_data, *orig_data_end, *hard_start;
4592 struct netdev_rx_queue *rxqueue;
4593 bool orig_bcast, orig_host;
4594 u32 mac_len, frame_sz;
4595 __be16 orig_eth_type;
4596 struct ethhdr *eth;
4597 u32 metalen, act;
4598 int off;
4599
4600
4601
4602
4603 mac_len = skb->data - skb_mac_header(skb);
4604 hard_start = skb->data - skb_headroom(skb);
4605
4606
4607 frame_sz = (void *)skb_end_pointer(skb) - hard_start;
4608 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4609
4610 rxqueue = netif_get_rxqueue(skb);
4611 xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
4612 xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
4613 skb_headlen(skb) + mac_len, true);
4614
4615 orig_data_end = xdp->data_end;
4616 orig_data = xdp->data;
4617 eth = (struct ethhdr *)xdp->data;
4618 orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr);
4619 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4620 orig_eth_type = eth->h_proto;
4621
4622 act = bpf_prog_run_xdp(xdp_prog, xdp);
4623
4624
4625 off = xdp->data - orig_data;
4626 if (off) {
4627 if (off > 0)
4628 __skb_pull(skb, off);
4629 else if (off < 0)
4630 __skb_push(skb, -off);
4631
4632 skb->mac_header += off;
4633 skb_reset_network_header(skb);
4634 }
4635
4636
4637 off = xdp->data_end - orig_data_end;
4638 if (off != 0) {
4639 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
4640 skb->len += off;
4641 }
4642
4643
4644 eth = (struct ethhdr *)xdp->data;
4645 if ((orig_eth_type != eth->h_proto) ||
4646 (orig_host != ether_addr_equal_64bits(eth->h_dest,
4647 skb->dev->dev_addr)) ||
4648 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4649 __skb_push(skb, ETH_HLEN);
4650 skb->pkt_type = PACKET_HOST;
4651 skb->protocol = eth_type_trans(skb, skb->dev);
4652 }
4653
4654
4655
4656
4657
4658
4659
4660
4661 switch (act) {
4662 case XDP_REDIRECT:
4663 case XDP_TX:
4664 __skb_push(skb, mac_len);
4665 break;
4666 case XDP_PASS:
4667 metalen = xdp->data - xdp->data_meta;
4668 if (metalen)
4669 skb_metadata_set(skb, metalen);
4670 break;
4671 }
4672
4673 return act;
4674}
4675
4676static u32 netif_receive_generic_xdp(struct sk_buff *skb,
4677 struct xdp_buff *xdp,
4678 struct bpf_prog *xdp_prog)
4679{
4680 u32 act = XDP_DROP;
4681
4682
4683
4684
4685 if (skb_is_redirected(skb))
4686 return XDP_PASS;
4687
4688
4689
4690
4691
4692 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
4693 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4694 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4695 int troom = skb->tail + skb->data_len - skb->end;
4696
4697
4698
4699
4700 if (pskb_expand_head(skb,
4701 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4702 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4703 goto do_drop;
4704 if (skb_linearize(skb))
4705 goto do_drop;
4706 }
4707
4708 act = bpf_prog_run_generic_xdp(skb, xdp, xdp_prog);
4709 switch (act) {
4710 case XDP_REDIRECT:
4711 case XDP_TX:
4712 case XDP_PASS:
4713 break;
4714 default:
4715 bpf_warn_invalid_xdp_action(skb->dev, xdp_prog, act);
4716 fallthrough;
4717 case XDP_ABORTED:
4718 trace_xdp_exception(skb->dev, xdp_prog, act);
4719 fallthrough;
4720 case XDP_DROP:
4721 do_drop:
4722 kfree_skb(skb);
4723 break;
4724 }
4725
4726 return act;
4727}
4728
4729
4730
4731
4732void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
4733{
4734 struct net_device *dev = skb->dev;
4735 struct netdev_queue *txq;
4736 bool free_skb = true;
4737 int cpu, rc;
4738
4739 txq = netdev_core_pick_tx(dev, skb, NULL);
4740 cpu = smp_processor_id();
4741 HARD_TX_LOCK(dev, txq, cpu);
4742 if (!netif_xmit_stopped(txq)) {
4743 rc = netdev_start_xmit(skb, dev, txq, 0);
4744 if (dev_xmit_complete(rc))
4745 free_skb = false;
4746 }
4747 HARD_TX_UNLOCK(dev, txq);
4748 if (free_skb) {
4749 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4750 kfree_skb(skb);
4751 }
4752}
4753
4754static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
4755
4756int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
4757{
4758 if (xdp_prog) {
4759 struct xdp_buff xdp;
4760 u32 act;
4761 int err;
4762
4763 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
4764 if (act != XDP_PASS) {
4765 switch (act) {
4766 case XDP_REDIRECT:
4767 err = xdp_do_generic_redirect(skb->dev, skb,
4768 &xdp, xdp_prog);
4769 if (err)
4770 goto out_redir;
4771 break;
4772 case XDP_TX:
4773 generic_xdp_tx(skb, xdp_prog);
4774 break;
4775 }
4776 return XDP_DROP;
4777 }
4778 }
4779 return XDP_PASS;
4780out_redir:
4781 kfree_skb(skb);
4782 return XDP_DROP;
4783}
4784EXPORT_SYMBOL_GPL(do_xdp_generic);
4785
4786static int netif_rx_internal(struct sk_buff *skb)
4787{
4788 int ret;
4789
4790 net_timestamp_check(netdev_tstamp_prequeue, skb);
4791
4792 trace_netif_rx(skb);
4793
4794#ifdef CONFIG_RPS
4795 if (static_branch_unlikely(&rps_needed)) {
4796 struct rps_dev_flow voidflow, *rflow = &voidflow;
4797 int cpu;
4798
4799 preempt_disable();
4800 rcu_read_lock();
4801
4802 cpu = get_rps_cpu(skb->dev, skb, &rflow);
4803 if (cpu < 0)
4804 cpu = smp_processor_id();
4805
4806 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4807
4808 rcu_read_unlock();
4809 preempt_enable();
4810 } else
4811#endif
4812 {
4813 unsigned int qtail;
4814
4815 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4816 put_cpu();
4817 }
4818 return ret;
4819}
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836int netif_rx(struct sk_buff *skb)
4837{
4838 int ret;
4839
4840 trace_netif_rx_entry(skb);
4841
4842 ret = netif_rx_internal(skb);
4843 trace_netif_rx_exit(ret);
4844
4845 return ret;
4846}
4847EXPORT_SYMBOL(netif_rx);
4848
4849int netif_rx_ni(struct sk_buff *skb)
4850{
4851 int err;
4852
4853 trace_netif_rx_ni_entry(skb);
4854
4855 preempt_disable();
4856 err = netif_rx_internal(skb);
4857 if (local_softirq_pending())
4858 do_softirq();
4859 preempt_enable();
4860 trace_netif_rx_ni_exit(err);
4861
4862 return err;
4863}
4864EXPORT_SYMBOL(netif_rx_ni);
4865
4866int netif_rx_any_context(struct sk_buff *skb)
4867{
4868
4869
4870
4871
4872
4873
4874 if (in_interrupt())
4875 return netif_rx(skb);
4876 else
4877 return netif_rx_ni(skb);
4878}
4879EXPORT_SYMBOL(netif_rx_any_context);
4880
4881static __latent_entropy void net_tx_action(struct softirq_action *h)
4882{
4883 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
4884
4885 if (sd->completion_queue) {
4886 struct sk_buff *clist;
4887
4888 local_irq_disable();
4889 clist = sd->completion_queue;
4890 sd->completion_queue = NULL;
4891 local_irq_enable();
4892
4893 while (clist) {
4894 struct sk_buff *skb = clist;
4895
4896 clist = clist->next;
4897
4898 WARN_ON(refcount_read(&skb->users));
4899 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4900 trace_consume_skb(skb);
4901 else
4902 trace_kfree_skb(skb, net_tx_action,
4903 SKB_DROP_REASON_NOT_SPECIFIED);
4904
4905 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4906 __kfree_skb(skb);
4907 else
4908 __kfree_skb_defer(skb);
4909 }
4910 }
4911
4912 if (sd->output_queue) {
4913 struct Qdisc *head;
4914
4915 local_irq_disable();
4916 head = sd->output_queue;
4917 sd->output_queue = NULL;
4918 sd->output_queue_tailp = &sd->output_queue;
4919 local_irq_enable();
4920
4921 rcu_read_lock();
4922
4923 while (head) {
4924 struct Qdisc *q = head;
4925 spinlock_t *root_lock = NULL;
4926
4927 head = head->next_sched;
4928
4929
4930
4931
4932 smp_mb__before_atomic();
4933
4934 if (!(q->flags & TCQ_F_NOLOCK)) {
4935 root_lock = qdisc_lock(q);
4936 spin_lock(root_lock);
4937 } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
4938 &q->state))) {
4939
4940
4941
4942
4943
4944
4945
4946
4947 clear_bit(__QDISC_STATE_SCHED, &q->state);
4948 continue;
4949 }
4950
4951 clear_bit(__QDISC_STATE_SCHED, &q->state);
4952 qdisc_run(q);
4953 if (root_lock)
4954 spin_unlock(root_lock);
4955 }
4956
4957 rcu_read_unlock();
4958 }
4959
4960 xfrm_dev_backlog(sd);
4961}
4962
4963#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
4964
4965int (*br_fdb_test_addr_hook)(struct net_device *dev,
4966 unsigned char *addr) __read_mostly;
4967EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
4968#endif
4969
4970static inline struct sk_buff *
4971sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4972 struct net_device *orig_dev, bool *another)
4973{
4974#ifdef CONFIG_NET_CLS_ACT
4975 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
4976 struct tcf_result cl_res;
4977
4978
4979
4980
4981
4982
4983 if (!miniq)
4984 return skb;
4985
4986 if (*pt_prev) {
4987 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4988 *pt_prev = NULL;
4989 }
4990
4991 qdisc_skb_cb(skb)->pkt_len = skb->len;
4992 tc_skb_cb(skb)->mru = 0;
4993 tc_skb_cb(skb)->post_ct = false;
4994 skb->tc_at_ingress = 1;
4995 mini_qdisc_bstats_cpu_update(miniq, skb);
4996
4997 switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) {
4998 case TC_ACT_OK:
4999 case TC_ACT_RECLASSIFY:
5000 skb->tc_index = TC_H_MIN(cl_res.classid);
5001 break;
5002 case TC_ACT_SHOT:
5003 mini_qdisc_qstats_cpu_drop(miniq);
5004 kfree_skb(skb);
5005 return NULL;
5006 case TC_ACT_STOLEN:
5007 case TC_ACT_QUEUED:
5008 case TC_ACT_TRAP:
5009 consume_skb(skb);
5010 return NULL;
5011 case TC_ACT_REDIRECT:
5012
5013
5014
5015
5016 __skb_push(skb, skb->mac_len);
5017 if (skb_do_redirect(skb) == -EAGAIN) {
5018 __skb_pull(skb, skb->mac_len);
5019 *another = true;
5020 break;
5021 }
5022 return NULL;
5023 case TC_ACT_CONSUMED:
5024 return NULL;
5025 default:
5026 break;
5027 }
5028#endif
5029 return skb;
5030}
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041bool netdev_is_rx_handler_busy(struct net_device *dev)
5042{
5043 ASSERT_RTNL();
5044 return dev && rtnl_dereference(dev->rx_handler);
5045}
5046EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062int netdev_rx_handler_register(struct net_device *dev,
5063 rx_handler_func_t *rx_handler,
5064 void *rx_handler_data)
5065{
5066 if (netdev_is_rx_handler_busy(dev))
5067 return -EBUSY;
5068
5069 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5070 return -EINVAL;
5071
5072
5073 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
5074 rcu_assign_pointer(dev->rx_handler, rx_handler);
5075
5076 return 0;
5077}
5078EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088void netdev_rx_handler_unregister(struct net_device *dev)
5089{
5090
5091 ASSERT_RTNL();
5092 RCU_INIT_POINTER(dev->rx_handler, NULL);
5093
5094
5095
5096
5097 synchronize_net();
5098 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
5099}
5100EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5101
5102
5103
5104
5105
5106static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5107{
5108 switch (skb->protocol) {
5109 case htons(ETH_P_ARP):
5110 case htons(ETH_P_IP):
5111 case htons(ETH_P_IPV6):
5112 case htons(ETH_P_8021Q):
5113 case htons(ETH_P_8021AD):
5114 return true;
5115 default:
5116 return false;
5117 }
5118}
5119
5120static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5121 int *ret, struct net_device *orig_dev)
5122{
5123 if (nf_hook_ingress_active(skb)) {
5124 int ingress_retval;
5125
5126 if (*pt_prev) {
5127 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5128 *pt_prev = NULL;
5129 }
5130
5131 rcu_read_lock();
5132 ingress_retval = nf_hook_ingress(skb);
5133 rcu_read_unlock();
5134 return ingress_retval;
5135 }
5136 return 0;
5137}
5138
5139static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
5140 struct packet_type **ppt_prev)
5141{
5142 struct packet_type *ptype, *pt_prev;
5143 rx_handler_func_t *rx_handler;
5144 struct sk_buff *skb = *pskb;
5145 struct net_device *orig_dev;
5146 bool deliver_exact = false;
5147 int ret = NET_RX_DROP;
5148 __be16 type;
5149
5150 net_timestamp_check(!netdev_tstamp_prequeue, skb);
5151
5152 trace_netif_receive_skb(skb);
5153
5154 orig_dev = skb->dev;
5155
5156 skb_reset_network_header(skb);
5157 if (!skb_transport_header_was_set(skb))
5158 skb_reset_transport_header(skb);
5159 skb_reset_mac_len(skb);
5160
5161 pt_prev = NULL;
5162
5163another_round:
5164 skb->skb_iif = skb->dev->ifindex;
5165
5166 __this_cpu_inc(softnet_data.processed);
5167
5168 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5169 int ret2;
5170
5171 migrate_disable();
5172 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5173 migrate_enable();
5174
5175 if (ret2 != XDP_PASS) {
5176 ret = NET_RX_DROP;
5177 goto out;
5178 }
5179 }
5180
5181 if (eth_type_vlan(skb->protocol)) {
5182 skb = skb_vlan_untag(skb);
5183 if (unlikely(!skb))
5184 goto out;
5185 }
5186
5187 if (skb_skip_tc_classify(skb))
5188 goto skip_classify;
5189
5190 if (pfmemalloc)
5191 goto skip_taps;
5192
5193 list_for_each_entry_rcu(ptype, &ptype_all, list) {
5194 if (pt_prev)
5195 ret = deliver_skb(skb, pt_prev, orig_dev);
5196 pt_prev = ptype;
5197 }
5198
5199 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5200 if (pt_prev)
5201 ret = deliver_skb(skb, pt_prev, orig_dev);
5202 pt_prev = ptype;
5203 }
5204
5205skip_taps:
5206#ifdef CONFIG_NET_INGRESS
5207 if (static_branch_unlikely(&ingress_needed_key)) {
5208 bool another = false;
5209
5210 nf_skip_egress(skb, true);
5211 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5212 &another);
5213 if (another)
5214 goto another_round;
5215 if (!skb)
5216 goto out;
5217
5218 nf_skip_egress(skb, false);
5219 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
5220 goto out;
5221 }
5222#endif
5223 skb_reset_redirect(skb);
5224skip_classify:
5225 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
5226 goto drop;
5227
5228 if (skb_vlan_tag_present(skb)) {
5229 if (pt_prev) {
5230 ret = deliver_skb(skb, pt_prev, orig_dev);
5231 pt_prev = NULL;
5232 }
5233 if (vlan_do_receive(&skb))
5234 goto another_round;
5235 else if (unlikely(!skb))
5236 goto out;
5237 }
5238
5239 rx_handler = rcu_dereference(skb->dev->rx_handler);
5240 if (rx_handler) {
5241 if (pt_prev) {
5242 ret = deliver_skb(skb, pt_prev, orig_dev);
5243 pt_prev = NULL;
5244 }
5245 switch (rx_handler(&skb)) {
5246 case RX_HANDLER_CONSUMED:
5247 ret = NET_RX_SUCCESS;
5248 goto out;
5249 case RX_HANDLER_ANOTHER:
5250 goto another_round;
5251 case RX_HANDLER_EXACT:
5252 deliver_exact = true;
5253 break;
5254 case RX_HANDLER_PASS:
5255 break;
5256 default:
5257 BUG();
5258 }
5259 }
5260
5261 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
5262check_vlan_id:
5263 if (skb_vlan_tag_get_id(skb)) {
5264
5265
5266
5267 skb->pkt_type = PACKET_OTHERHOST;
5268 } else if (eth_type_vlan(skb->protocol)) {
5269
5270
5271
5272
5273 __vlan_hwaccel_clear_tag(skb);
5274 skb = skb_vlan_untag(skb);
5275 if (unlikely(!skb))
5276 goto out;
5277 if (vlan_do_receive(&skb))
5278
5279
5280
5281 goto another_round;
5282 else if (unlikely(!skb))
5283 goto out;
5284 else
5285
5286
5287
5288
5289 goto check_vlan_id;
5290 }
5291
5292
5293
5294
5295 __vlan_hwaccel_clear_tag(skb);
5296 }
5297
5298 type = skb->protocol;
5299
5300
5301 if (likely(!deliver_exact)) {
5302 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5303 &ptype_base[ntohs(type) &
5304 PTYPE_HASH_MASK]);
5305 }
5306
5307 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5308 &orig_dev->ptype_specific);
5309
5310 if (unlikely(skb->dev != orig_dev)) {
5311 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5312 &skb->dev->ptype_specific);
5313 }
5314
5315 if (pt_prev) {
5316 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
5317 goto drop;
5318 *ppt_prev = pt_prev;
5319 } else {
5320drop:
5321 if (!deliver_exact)
5322 atomic_long_inc(&skb->dev->rx_dropped);
5323 else
5324 atomic_long_inc(&skb->dev->rx_nohandler);
5325 kfree_skb(skb);
5326
5327
5328
5329 ret = NET_RX_DROP;
5330 }
5331
5332out:
5333
5334
5335
5336
5337
5338
5339 *pskb = skb;
5340 return ret;
5341}
5342
5343static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5344{
5345 struct net_device *orig_dev = skb->dev;
5346 struct packet_type *pt_prev = NULL;
5347 int ret;
5348
5349 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
5350 if (pt_prev)
5351 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5352 skb->dev, pt_prev, orig_dev);
5353 return ret;
5354}
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371int netif_receive_skb_core(struct sk_buff *skb)
5372{
5373 int ret;
5374
5375 rcu_read_lock();
5376 ret = __netif_receive_skb_one_core(skb, false);
5377 rcu_read_unlock();
5378
5379 return ret;
5380}
5381EXPORT_SYMBOL(netif_receive_skb_core);
5382
5383static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5384 struct packet_type *pt_prev,
5385 struct net_device *orig_dev)
5386{
5387 struct sk_buff *skb, *next;
5388
5389 if (!pt_prev)
5390 return;
5391 if (list_empty(head))
5392 return;
5393 if (pt_prev->list_func != NULL)
5394 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5395 ip_list_rcv, head, pt_prev, orig_dev);
5396 else
5397 list_for_each_entry_safe(skb, next, head, list) {
5398 skb_list_del_init(skb);
5399 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
5400 }
5401}
5402
5403static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5404{
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415 struct packet_type *pt_curr = NULL;
5416
5417 struct net_device *od_curr = NULL;
5418 struct list_head sublist;
5419 struct sk_buff *skb, *next;
5420
5421 INIT_LIST_HEAD(&sublist);
5422 list_for_each_entry_safe(skb, next, head, list) {
5423 struct net_device *orig_dev = skb->dev;
5424 struct packet_type *pt_prev = NULL;
5425
5426 skb_list_del_init(skb);
5427 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
5428 if (!pt_prev)
5429 continue;
5430 if (pt_curr != pt_prev || od_curr != orig_dev) {
5431
5432 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5433
5434 INIT_LIST_HEAD(&sublist);
5435 pt_curr = pt_prev;
5436 od_curr = orig_dev;
5437 }
5438 list_add_tail(&skb->list, &sublist);
5439 }
5440
5441
5442 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5443}
5444
5445static int __netif_receive_skb(struct sk_buff *skb)
5446{
5447 int ret;
5448
5449 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
5450 unsigned int noreclaim_flag;
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461 noreclaim_flag = memalloc_noreclaim_save();
5462 ret = __netif_receive_skb_one_core(skb, true);
5463 memalloc_noreclaim_restore(noreclaim_flag);
5464 } else
5465 ret = __netif_receive_skb_one_core(skb, false);
5466
5467 return ret;
5468}
5469
5470static void __netif_receive_skb_list(struct list_head *head)
5471{
5472 unsigned long noreclaim_flag = 0;
5473 struct sk_buff *skb, *next;
5474 bool pfmemalloc = false;
5475
5476 list_for_each_entry_safe(skb, next, head, list) {
5477 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5478 struct list_head sublist;
5479
5480
5481 list_cut_before(&sublist, head, &skb->list);
5482 if (!list_empty(&sublist))
5483 __netif_receive_skb_list_core(&sublist, pfmemalloc);
5484 pfmemalloc = !pfmemalloc;
5485
5486 if (pfmemalloc)
5487 noreclaim_flag = memalloc_noreclaim_save();
5488 else
5489 memalloc_noreclaim_restore(noreclaim_flag);
5490 }
5491 }
5492
5493 if (!list_empty(head))
5494 __netif_receive_skb_list_core(head, pfmemalloc);
5495
5496 if (pfmemalloc)
5497 memalloc_noreclaim_restore(noreclaim_flag);
5498}
5499
5500static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
5501{
5502 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
5503 struct bpf_prog *new = xdp->prog;
5504 int ret = 0;
5505
5506 switch (xdp->command) {
5507 case XDP_SETUP_PROG:
5508 rcu_assign_pointer(dev->xdp_prog, new);
5509 if (old)
5510 bpf_prog_put(old);
5511
5512 if (old && !new) {
5513 static_branch_dec(&generic_xdp_needed_key);
5514 } else if (new && !old) {
5515 static_branch_inc(&generic_xdp_needed_key);
5516 dev_disable_lro(dev);
5517 dev_disable_gro_hw(dev);
5518 }
5519 break;
5520
5521 default:
5522 ret = -EINVAL;
5523 break;
5524 }
5525
5526 return ret;
5527}
5528
5529static int netif_receive_skb_internal(struct sk_buff *skb)
5530{
5531 int ret;
5532
5533 net_timestamp_check(netdev_tstamp_prequeue, skb);
5534
5535 if (skb_defer_rx_timestamp(skb))
5536 return NET_RX_SUCCESS;
5537
5538 rcu_read_lock();
5539#ifdef CONFIG_RPS
5540 if (static_branch_unlikely(&rps_needed)) {
5541 struct rps_dev_flow voidflow, *rflow = &voidflow;
5542 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5543
5544 if (cpu >= 0) {
5545 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5546 rcu_read_unlock();
5547 return ret;
5548 }
5549 }
5550#endif
5551 ret = __netif_receive_skb(skb);
5552 rcu_read_unlock();
5553 return ret;
5554}
5555
5556void netif_receive_skb_list_internal(struct list_head *head)
5557{
5558 struct sk_buff *skb, *next;
5559 struct list_head sublist;
5560
5561 INIT_LIST_HEAD(&sublist);
5562 list_for_each_entry_safe(skb, next, head, list) {
5563 net_timestamp_check(netdev_tstamp_prequeue, skb);
5564 skb_list_del_init(skb);
5565 if (!skb_defer_rx_timestamp(skb))
5566 list_add_tail(&skb->list, &sublist);
5567 }
5568 list_splice_init(&sublist, head);
5569
5570 rcu_read_lock();
5571#ifdef CONFIG_RPS
5572 if (static_branch_unlikely(&rps_needed)) {
5573 list_for_each_entry_safe(skb, next, head, list) {
5574 struct rps_dev_flow voidflow, *rflow = &voidflow;
5575 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5576
5577 if (cpu >= 0) {
5578
5579 skb_list_del_init(skb);
5580 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5581 }
5582 }
5583 }
5584#endif
5585 __netif_receive_skb_list(head);
5586 rcu_read_unlock();
5587}
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604int netif_receive_skb(struct sk_buff *skb)
5605{
5606 int ret;
5607
5608 trace_netif_receive_skb_entry(skb);
5609
5610 ret = netif_receive_skb_internal(skb);
5611 trace_netif_receive_skb_exit(ret);
5612
5613 return ret;
5614}
5615EXPORT_SYMBOL(netif_receive_skb);
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627void netif_receive_skb_list(struct list_head *head)
5628{
5629 struct sk_buff *skb;
5630
5631 if (list_empty(head))
5632 return;
5633 if (trace_netif_receive_skb_list_entry_enabled()) {
5634 list_for_each_entry(skb, head, list)
5635 trace_netif_receive_skb_list_entry(skb);
5636 }
5637 netif_receive_skb_list_internal(head);
5638 trace_netif_receive_skb_list_exit(0);
5639}
5640EXPORT_SYMBOL(netif_receive_skb_list);
5641
5642static DEFINE_PER_CPU(struct work_struct, flush_works);
5643
5644
5645static void flush_backlog(struct work_struct *work)
5646{
5647 struct sk_buff *skb, *tmp;
5648 struct softnet_data *sd;
5649
5650 local_bh_disable();
5651 sd = this_cpu_ptr(&softnet_data);
5652
5653 local_irq_disable();
5654 rps_lock(sd);
5655 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
5656 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5657 __skb_unlink(skb, &sd->input_pkt_queue);
5658 dev_kfree_skb_irq(skb);
5659 input_queue_head_incr(sd);
5660 }
5661 }
5662 rps_unlock(sd);
5663 local_irq_enable();
5664
5665 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
5666 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5667 __skb_unlink(skb, &sd->process_queue);
5668 kfree_skb(skb);
5669 input_queue_head_incr(sd);
5670 }
5671 }
5672 local_bh_enable();
5673}
5674
5675static bool flush_required(int cpu)
5676{
5677#if IS_ENABLED(CONFIG_RPS)
5678 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5679 bool do_flush;
5680
5681 local_irq_disable();
5682 rps_lock(sd);
5683
5684
5685
5686
5687 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5688 !skb_queue_empty_lockless(&sd->process_queue);
5689 rps_unlock(sd);
5690 local_irq_enable();
5691
5692 return do_flush;
5693#endif
5694
5695
5696
5697
5698
5699 return true;
5700}
5701
5702static void flush_all_backlogs(void)
5703{
5704 static cpumask_t flush_cpus;
5705 unsigned int cpu;
5706
5707
5708
5709
5710
5711 ASSERT_RTNL();
5712
5713 cpus_read_lock();
5714
5715 cpumask_clear(&flush_cpus);
5716 for_each_online_cpu(cpu) {
5717 if (flush_required(cpu)) {
5718 queue_work_on(cpu, system_highpri_wq,
5719 per_cpu_ptr(&flush_works, cpu));
5720 cpumask_set_cpu(cpu, &flush_cpus);
5721 }
5722 }
5723
5724
5725
5726
5727
5728 for_each_cpu(cpu, &flush_cpus)
5729 flush_work(per_cpu_ptr(&flush_works, cpu));
5730
5731 cpus_read_unlock();
5732}
5733
5734static void net_rps_send_ipi(struct softnet_data *remsd)
5735{
5736#ifdef CONFIG_RPS
5737 while (remsd) {
5738 struct softnet_data *next = remsd->rps_ipi_next;
5739
5740 if (cpu_online(remsd->cpu))
5741 smp_call_function_single_async(remsd->cpu, &remsd->csd);
5742 remsd = next;
5743 }
5744#endif
5745}
5746
5747
5748
5749
5750
5751static void net_rps_action_and_irq_enable(struct softnet_data *sd)
5752{
5753#ifdef CONFIG_RPS
5754 struct softnet_data *remsd = sd->rps_ipi_list;
5755
5756 if (remsd) {
5757 sd->rps_ipi_list = NULL;
5758
5759 local_irq_enable();
5760
5761
5762 net_rps_send_ipi(remsd);
5763 } else
5764#endif
5765 local_irq_enable();
5766}
5767
5768static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5769{
5770#ifdef CONFIG_RPS
5771 return sd->rps_ipi_list != NULL;
5772#else
5773 return false;
5774#endif
5775}
5776
5777static int process_backlog(struct napi_struct *napi, int quota)
5778{
5779 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
5780 bool again = true;
5781 int work = 0;
5782
5783
5784
5785
5786 if (sd_has_rps_ipi_waiting(sd)) {
5787 local_irq_disable();
5788 net_rps_action_and_irq_enable(sd);
5789 }
5790
5791 napi->weight = dev_rx_weight;
5792 while (again) {
5793 struct sk_buff *skb;
5794
5795 while ((skb = __skb_dequeue(&sd->process_queue))) {
5796 rcu_read_lock();
5797 __netif_receive_skb(skb);
5798 rcu_read_unlock();
5799 input_queue_head_incr(sd);
5800 if (++work >= quota)
5801 return work;
5802
5803 }
5804
5805 local_irq_disable();
5806 rps_lock(sd);
5807 if (skb_queue_empty(&sd->input_pkt_queue)) {
5808
5809
5810
5811
5812
5813
5814
5815
5816 napi->state = 0;
5817 again = false;
5818 } else {
5819 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5820 &sd->process_queue);
5821 }
5822 rps_unlock(sd);
5823 local_irq_enable();
5824 }
5825
5826 return work;
5827}
5828
5829
5830
5831
5832
5833
5834
5835
5836void __napi_schedule(struct napi_struct *n)
5837{
5838 unsigned long flags;
5839
5840 local_irq_save(flags);
5841 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5842 local_irq_restore(flags);
5843}
5844EXPORT_SYMBOL(__napi_schedule);
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855bool napi_schedule_prep(struct napi_struct *n)
5856{
5857 unsigned long val, new;
5858
5859 do {
5860 val = READ_ONCE(n->state);
5861 if (unlikely(val & NAPIF_STATE_DISABLE))
5862 return false;
5863 new = val | NAPIF_STATE_SCHED;
5864
5865
5866
5867
5868
5869
5870
5871 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5872 NAPIF_STATE_MISSED;
5873 } while (cmpxchg(&n->state, val, new) != val);
5874
5875 return !(val & NAPIF_STATE_SCHED);
5876}
5877EXPORT_SYMBOL(napi_schedule_prep);
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889void __napi_schedule_irqoff(struct napi_struct *n)
5890{
5891 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
5892 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5893 else
5894 __napi_schedule(n);
5895}
5896EXPORT_SYMBOL(__napi_schedule_irqoff);
5897
5898bool napi_complete_done(struct napi_struct *n, int work_done)
5899{
5900 unsigned long flags, val, new, timeout = 0;
5901 bool ret = true;
5902
5903
5904
5905
5906
5907
5908
5909 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
5910 NAPIF_STATE_IN_BUSY_POLL)))
5911 return false;
5912
5913 if (work_done) {
5914 if (n->gro_bitmask)
5915 timeout = READ_ONCE(n->dev->gro_flush_timeout);
5916 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
5917 }
5918 if (n->defer_hard_irqs_count > 0) {
5919 n->defer_hard_irqs_count--;
5920 timeout = READ_ONCE(n->dev->gro_flush_timeout);
5921 if (timeout)
5922 ret = false;
5923 }
5924 if (n->gro_bitmask) {
5925
5926
5927
5928
5929 napi_gro_flush(n, !!timeout);
5930 }
5931
5932 gro_normal_list(n);
5933
5934 if (unlikely(!list_empty(&n->poll_list))) {
5935
5936 local_irq_save(flags);
5937 list_del_init(&n->poll_list);
5938 local_irq_restore(flags);
5939 }
5940
5941 do {
5942 val = READ_ONCE(n->state);
5943
5944 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
5945
5946 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
5947 NAPIF_STATE_SCHED_THREADED |
5948 NAPIF_STATE_PREFER_BUSY_POLL);
5949
5950
5951
5952
5953
5954 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
5955 NAPIF_STATE_SCHED;
5956 } while (cmpxchg(&n->state, val, new) != val);
5957
5958 if (unlikely(val & NAPIF_STATE_MISSED)) {
5959 __napi_schedule(n);
5960 return false;
5961 }
5962
5963 if (timeout)
5964 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5965 HRTIMER_MODE_REL_PINNED);
5966 return ret;
5967}
5968EXPORT_SYMBOL(napi_complete_done);
5969
5970
5971static struct napi_struct *napi_by_id(unsigned int napi_id)
5972{
5973 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
5974 struct napi_struct *napi;
5975
5976 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
5977 if (napi->napi_id == napi_id)
5978 return napi;
5979
5980 return NULL;
5981}
5982
5983#if defined(CONFIG_NET_RX_BUSY_POLL)
5984
5985static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
5986{
5987 if (!skip_schedule) {
5988 gro_normal_list(napi);
5989 __napi_schedule(napi);
5990 return;
5991 }
5992
5993 if (napi->gro_bitmask) {
5994
5995
5996
5997 napi_gro_flush(napi, HZ >= 1000);
5998 }
5999
6000 gro_normal_list(napi);
6001 clear_bit(NAPI_STATE_SCHED, &napi->state);
6002}
6003
6004static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll,
6005 u16 budget)
6006{
6007 bool skip_schedule = false;
6008 unsigned long timeout;
6009 int rc;
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020 clear_bit(NAPI_STATE_MISSED, &napi->state);
6021 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6022
6023 local_bh_disable();
6024
6025 if (prefer_busy_poll) {
6026 napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
6027 timeout = READ_ONCE(napi->dev->gro_flush_timeout);
6028 if (napi->defer_hard_irqs_count && timeout) {
6029 hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
6030 skip_schedule = true;
6031 }
6032 }
6033
6034
6035
6036
6037 rc = napi->poll(napi, budget);
6038
6039
6040
6041
6042 trace_napi_poll(napi, rc, budget);
6043 netpoll_poll_unlock(have_poll_lock);
6044 if (rc == budget)
6045 __busy_poll_stop(napi, skip_schedule);
6046 local_bh_enable();
6047}
6048
6049void napi_busy_loop(unsigned int napi_id,
6050 bool (*loop_end)(void *, unsigned long),
6051 void *loop_end_arg, bool prefer_busy_poll, u16 budget)
6052{
6053 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
6054 int (*napi_poll)(struct napi_struct *napi, int budget);
6055 void *have_poll_lock = NULL;
6056 struct napi_struct *napi;
6057
6058restart:
6059 napi_poll = NULL;
6060
6061 rcu_read_lock();
6062
6063 napi = napi_by_id(napi_id);
6064 if (!napi)
6065 goto out;
6066
6067 preempt_disable();
6068 for (;;) {
6069 int work = 0;
6070
6071 local_bh_disable();
6072 if (!napi_poll) {
6073 unsigned long val = READ_ONCE(napi->state);
6074
6075
6076
6077
6078 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6079 NAPIF_STATE_IN_BUSY_POLL)) {
6080 if (prefer_busy_poll)
6081 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
6082 goto count;
6083 }
6084 if (cmpxchg(&napi->state, val,
6085 val | NAPIF_STATE_IN_BUSY_POLL |
6086 NAPIF_STATE_SCHED) != val) {
6087 if (prefer_busy_poll)
6088 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
6089 goto count;
6090 }
6091 have_poll_lock = netpoll_poll_lock(napi);
6092 napi_poll = napi->poll;
6093 }
6094 work = napi_poll(napi, budget);
6095 trace_napi_poll(napi, work, budget);
6096 gro_normal_list(napi);
6097count:
6098 if (work > 0)
6099 __NET_ADD_STATS(dev_net(napi->dev),
6100 LINUX_MIB_BUSYPOLLRXPACKETS, work);
6101 local_bh_enable();
6102
6103 if (!loop_end || loop_end(loop_end_arg, start_time))
6104 break;
6105
6106 if (unlikely(need_resched())) {
6107 if (napi_poll)
6108 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
6109 preempt_enable();
6110 rcu_read_unlock();
6111 cond_resched();
6112 if (loop_end(loop_end_arg, start_time))
6113 return;
6114 goto restart;
6115 }
6116 cpu_relax();
6117 }
6118 if (napi_poll)
6119 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
6120 preempt_enable();
6121out:
6122 rcu_read_unlock();
6123}
6124EXPORT_SYMBOL(napi_busy_loop);
6125
6126#endif
6127
6128static void napi_hash_add(struct napi_struct *napi)
6129{
6130 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
6131 return;
6132
6133 spin_lock(&napi_hash_lock);
6134
6135
6136 do {
6137 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6138 napi_gen_id = MIN_NAPI_ID;
6139 } while (napi_by_id(napi_gen_id));
6140 napi->napi_id = napi_gen_id;
6141
6142 hlist_add_head_rcu(&napi->napi_hash_node,
6143 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
6144
6145 spin_unlock(&napi_hash_lock);
6146}
6147
6148
6149
6150
6151static void napi_hash_del(struct napi_struct *napi)
6152{
6153 spin_lock(&napi_hash_lock);
6154
6155 hlist_del_init_rcu(&napi->napi_hash_node);
6156
6157 spin_unlock(&napi_hash_lock);
6158}
6159
6160static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6161{
6162 struct napi_struct *napi;
6163
6164 napi = container_of(timer, struct napi_struct, timer);
6165
6166
6167
6168
6169 if (!napi_disable_pending(napi) &&
6170 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
6171 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
6172 __napi_schedule_irqoff(napi);
6173 }
6174
6175 return HRTIMER_NORESTART;
6176}
6177
6178static void init_gro_hash(struct napi_struct *napi)
6179{
6180 int i;
6181
6182 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6183 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6184 napi->gro_hash[i].count = 0;
6185 }
6186 napi->gro_bitmask = 0;
6187}
6188
6189int dev_set_threaded(struct net_device *dev, bool threaded)
6190{
6191 struct napi_struct *napi;
6192 int err = 0;
6193
6194 if (dev->threaded == threaded)
6195 return 0;
6196
6197 if (threaded) {
6198 list_for_each_entry(napi, &dev->napi_list, dev_list) {
6199 if (!napi->thread) {
6200 err = napi_kthread_create(napi);
6201 if (err) {
6202 threaded = false;
6203 break;
6204 }
6205 }
6206 }
6207 }
6208
6209 dev->threaded = threaded;
6210
6211
6212
6213
6214 smp_mb__before_atomic();
6215
6216
6217
6218
6219
6220
6221
6222 list_for_each_entry(napi, &dev->napi_list, dev_list) {
6223 if (threaded)
6224 set_bit(NAPI_STATE_THREADED, &napi->state);
6225 else
6226 clear_bit(NAPI_STATE_THREADED, &napi->state);
6227 }
6228
6229 return err;
6230}
6231EXPORT_SYMBOL(dev_set_threaded);
6232
6233void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6234 int (*poll)(struct napi_struct *, int), int weight)
6235{
6236 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6237 return;
6238
6239 INIT_LIST_HEAD(&napi->poll_list);
6240 INIT_HLIST_NODE(&napi->napi_hash_node);
6241 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6242 napi->timer.function = napi_watchdog;
6243 init_gro_hash(napi);
6244 napi->skb = NULL;
6245 INIT_LIST_HEAD(&napi->rx_list);
6246 napi->rx_count = 0;
6247 napi->poll = poll;
6248 if (weight > NAPI_POLL_WEIGHT)
6249 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6250 weight);
6251 napi->weight = weight;
6252 napi->dev = dev;
6253#ifdef CONFIG_NETPOLL
6254 napi->poll_owner = -1;
6255#endif
6256 set_bit(NAPI_STATE_SCHED, &napi->state);
6257 set_bit(NAPI_STATE_NPSVC, &napi->state);
6258 list_add_rcu(&napi->dev_list, &dev->napi_list);
6259 napi_hash_add(napi);
6260
6261
6262
6263
6264 if (dev->threaded && napi_kthread_create(napi))
6265 dev->threaded = 0;
6266}
6267EXPORT_SYMBOL(netif_napi_add);
6268
6269void napi_disable(struct napi_struct *n)
6270{
6271 unsigned long val, new;
6272
6273 might_sleep();
6274 set_bit(NAPI_STATE_DISABLE, &n->state);
6275
6276 for ( ; ; ) {
6277 val = READ_ONCE(n->state);
6278 if (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
6279 usleep_range(20, 200);
6280 continue;
6281 }
6282
6283 new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
6284 new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL);
6285
6286 if (cmpxchg(&n->state, val, new) == val)
6287 break;
6288 }
6289
6290 hrtimer_cancel(&n->timer);
6291
6292 clear_bit(NAPI_STATE_DISABLE, &n->state);
6293}
6294EXPORT_SYMBOL(napi_disable);
6295
6296
6297
6298
6299
6300
6301
6302
6303void napi_enable(struct napi_struct *n)
6304{
6305 unsigned long val, new;
6306
6307 do {
6308 val = READ_ONCE(n->state);
6309 BUG_ON(!test_bit(NAPI_STATE_SCHED, &val));
6310
6311 new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC);
6312 if (n->dev->threaded && n->thread)
6313 new |= NAPIF_STATE_THREADED;
6314 } while (cmpxchg(&n->state, val, new) != val);
6315}
6316EXPORT_SYMBOL(napi_enable);
6317
6318static void flush_gro_hash(struct napi_struct *napi)
6319{
6320 int i;
6321
6322 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6323 struct sk_buff *skb, *n;
6324
6325 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
6326 kfree_skb(skb);
6327 napi->gro_hash[i].count = 0;
6328 }
6329}
6330
6331
6332void __netif_napi_del(struct napi_struct *napi)
6333{
6334 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6335 return;
6336
6337 napi_hash_del(napi);
6338 list_del_rcu(&napi->dev_list);
6339 napi_free_frags(napi);
6340
6341 flush_gro_hash(napi);
6342 napi->gro_bitmask = 0;
6343
6344 if (napi->thread) {
6345 kthread_stop(napi->thread);
6346 napi->thread = NULL;
6347 }
6348}
6349EXPORT_SYMBOL(__netif_napi_del);
6350
6351static int __napi_poll(struct napi_struct *n, bool *repoll)
6352{
6353 int work, weight;
6354
6355 weight = n->weight;
6356
6357
6358
6359
6360
6361
6362
6363 work = 0;
6364 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6365 work = n->poll(n, weight);
6366 trace_napi_poll(n, work, weight);
6367 }
6368
6369 if (unlikely(work > weight))
6370 netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6371 n->poll, work, weight);
6372
6373 if (likely(work < weight))
6374 return work;
6375
6376
6377
6378
6379
6380
6381 if (unlikely(napi_disable_pending(n))) {
6382 napi_complete(n);
6383 return work;
6384 }
6385
6386
6387
6388
6389 if (napi_prefer_busy_poll(n)) {
6390 if (napi_complete_done(n, work)) {
6391
6392
6393
6394 napi_schedule(n);
6395 }
6396 return work;
6397 }
6398
6399 if (n->gro_bitmask) {
6400
6401
6402
6403 napi_gro_flush(n, HZ >= 1000);
6404 }
6405
6406 gro_normal_list(n);
6407
6408
6409
6410
6411 if (unlikely(!list_empty(&n->poll_list))) {
6412 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6413 n->dev ? n->dev->name : "backlog");
6414 return work;
6415 }
6416
6417 *repoll = true;
6418
6419 return work;
6420}
6421
6422static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6423{
6424 bool do_repoll = false;
6425 void *have;
6426 int work;
6427
6428 list_del_init(&n->poll_list);
6429
6430 have = netpoll_poll_lock(n);
6431
6432 work = __napi_poll(n, &do_repoll);
6433
6434 if (do_repoll)
6435 list_add_tail(&n->poll_list, repoll);
6436
6437 netpoll_poll_unlock(have);
6438
6439 return work;
6440}
6441
6442static int napi_thread_wait(struct napi_struct *napi)
6443{
6444 bool woken = false;
6445
6446 set_current_state(TASK_INTERRUPTIBLE);
6447
6448 while (!kthread_should_stop()) {
6449
6450
6451
6452
6453
6454 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) {
6455 WARN_ON(!list_empty(&napi->poll_list));
6456 __set_current_state(TASK_RUNNING);
6457 return 0;
6458 }
6459
6460 schedule();
6461
6462 woken = true;
6463 set_current_state(TASK_INTERRUPTIBLE);
6464 }
6465 __set_current_state(TASK_RUNNING);
6466
6467 return -1;
6468}
6469
6470static int napi_threaded_poll(void *data)
6471{
6472 struct napi_struct *napi = data;
6473 void *have;
6474
6475 while (!napi_thread_wait(napi)) {
6476 for (;;) {
6477 bool repoll = false;
6478
6479 local_bh_disable();
6480
6481 have = netpoll_poll_lock(napi);
6482 __napi_poll(napi, &repoll);
6483 netpoll_poll_unlock(have);
6484
6485 local_bh_enable();
6486
6487 if (!repoll)
6488 break;
6489
6490 cond_resched();
6491 }
6492 }
6493 return 0;
6494}
6495
6496static __latent_entropy void net_rx_action(struct softirq_action *h)
6497{
6498 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
6499 unsigned long time_limit = jiffies +
6500 usecs_to_jiffies(netdev_budget_usecs);
6501 int budget = netdev_budget;
6502 LIST_HEAD(list);
6503 LIST_HEAD(repoll);
6504
6505 local_irq_disable();
6506 list_splice_init(&sd->poll_list, &list);
6507 local_irq_enable();
6508
6509 for (;;) {
6510 struct napi_struct *n;
6511
6512 if (list_empty(&list)) {
6513 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
6514 return;
6515 break;
6516 }
6517
6518 n = list_first_entry(&list, struct napi_struct, poll_list);
6519 budget -= napi_poll(n, &repoll);
6520
6521
6522
6523
6524
6525 if (unlikely(budget <= 0 ||
6526 time_after_eq(jiffies, time_limit))) {
6527 sd->time_squeeze++;
6528 break;
6529 }
6530 }
6531
6532 local_irq_disable();
6533
6534 list_splice_tail_init(&sd->poll_list, &list);
6535 list_splice_tail(&repoll, &list);
6536 list_splice(&list, &sd->poll_list);
6537 if (!list_empty(&sd->poll_list))
6538 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6539
6540 net_rps_action_and_irq_enable(sd);
6541}
6542
6543struct netdev_adjacent {
6544 struct net_device *dev;
6545 netdevice_tracker dev_tracker;
6546
6547
6548 bool master;
6549
6550
6551 bool ignore;
6552
6553
6554 u16 ref_nr;
6555
6556
6557 void *private;
6558
6559 struct list_head list;
6560 struct rcu_head rcu;
6561};
6562
6563static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
6564 struct list_head *adj_list)
6565{
6566 struct netdev_adjacent *adj;
6567
6568 list_for_each_entry(adj, adj_list, list) {
6569 if (adj->dev == adj_dev)
6570 return adj;
6571 }
6572 return NULL;
6573}
6574
6575static int ____netdev_has_upper_dev(struct net_device *upper_dev,
6576 struct netdev_nested_priv *priv)
6577{
6578 struct net_device *dev = (struct net_device *)priv->data;
6579
6580 return upper_dev == dev;
6581}
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592bool netdev_has_upper_dev(struct net_device *dev,
6593 struct net_device *upper_dev)
6594{
6595 struct netdev_nested_priv priv = {
6596 .data = (void *)upper_dev,
6597 };
6598
6599 ASSERT_RTNL();
6600
6601 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
6602 &priv);
6603}
6604EXPORT_SYMBOL(netdev_has_upper_dev);
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6617 struct net_device *upper_dev)
6618{
6619 struct netdev_nested_priv priv = {
6620 .data = (void *)upper_dev,
6621 };
6622
6623 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
6624 &priv);
6625}
6626EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6627
6628
6629
6630
6631
6632
6633
6634
6635bool netdev_has_any_upper_dev(struct net_device *dev)
6636{
6637 ASSERT_RTNL();
6638
6639 return !list_empty(&dev->adj_list.upper);
6640}
6641EXPORT_SYMBOL(netdev_has_any_upper_dev);
6642
6643
6644
6645
6646
6647
6648
6649
6650struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6651{
6652 struct netdev_adjacent *upper;
6653
6654 ASSERT_RTNL();
6655
6656 if (list_empty(&dev->adj_list.upper))
6657 return NULL;
6658
6659 upper = list_first_entry(&dev->adj_list.upper,
6660 struct netdev_adjacent, list);
6661 if (likely(upper->master))
6662 return upper->dev;
6663 return NULL;
6664}
6665EXPORT_SYMBOL(netdev_master_upper_dev_get);
6666
6667static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6668{
6669 struct netdev_adjacent *upper;
6670
6671 ASSERT_RTNL();
6672
6673 if (list_empty(&dev->adj_list.upper))
6674 return NULL;
6675
6676 upper = list_first_entry(&dev->adj_list.upper,
6677 struct netdev_adjacent, list);
6678 if (likely(upper->master) && !upper->ignore)
6679 return upper->dev;
6680 return NULL;
6681}
6682
6683
6684
6685
6686
6687
6688
6689
6690static bool netdev_has_any_lower_dev(struct net_device *dev)
6691{
6692 ASSERT_RTNL();
6693
6694 return !list_empty(&dev->adj_list.lower);
6695}
6696
6697void *netdev_adjacent_get_private(struct list_head *adj_list)
6698{
6699 struct netdev_adjacent *adj;
6700
6701 adj = list_entry(adj_list, struct netdev_adjacent, list);
6702
6703 return adj->private;
6704}
6705EXPORT_SYMBOL(netdev_adjacent_get_private);
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6716 struct list_head **iter)
6717{
6718 struct netdev_adjacent *upper;
6719
6720 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6721
6722 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6723
6724 if (&upper->list == &dev->adj_list.upper)
6725 return NULL;
6726
6727 *iter = &upper->list;
6728
6729 return upper->dev;
6730}
6731EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6732
6733static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6734 struct list_head **iter,
6735 bool *ignore)
6736{
6737 struct netdev_adjacent *upper;
6738
6739 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6740
6741 if (&upper->list == &dev->adj_list.upper)
6742 return NULL;
6743
6744 *iter = &upper->list;
6745 *ignore = upper->ignore;
6746
6747 return upper->dev;
6748}
6749
6750static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6751 struct list_head **iter)
6752{
6753 struct netdev_adjacent *upper;
6754
6755 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6756
6757 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6758
6759 if (&upper->list == &dev->adj_list.upper)
6760 return NULL;
6761
6762 *iter = &upper->list;
6763
6764 return upper->dev;
6765}
6766
6767static int __netdev_walk_all_upper_dev(struct net_device *dev,
6768 int (*fn)(struct net_device *dev,
6769 struct netdev_nested_priv *priv),
6770 struct netdev_nested_priv *priv)
6771{
6772 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6773 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6774 int ret, cur = 0;
6775 bool ignore;
6776
6777 now = dev;
6778 iter = &dev->adj_list.upper;
6779
6780 while (1) {
6781 if (now != dev) {
6782 ret = fn(now, priv);
6783 if (ret)
6784 return ret;
6785 }
6786
6787 next = NULL;
6788 while (1) {
6789 udev = __netdev_next_upper_dev(now, &iter, &ignore);
6790 if (!udev)
6791 break;
6792 if (ignore)
6793 continue;
6794
6795 next = udev;
6796 niter = &udev->adj_list.upper;
6797 dev_stack[cur] = now;
6798 iter_stack[cur++] = iter;
6799 break;
6800 }
6801
6802 if (!next) {
6803 if (!cur)
6804 return 0;
6805 next = dev_stack[--cur];
6806 niter = iter_stack[cur];
6807 }
6808
6809 now = next;
6810 iter = niter;
6811 }
6812
6813 return 0;
6814}
6815
6816int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6817 int (*fn)(struct net_device *dev,
6818 struct netdev_nested_priv *priv),
6819 struct netdev_nested_priv *priv)
6820{
6821 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6822 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6823 int ret, cur = 0;
6824
6825 now = dev;
6826 iter = &dev->adj_list.upper;
6827
6828 while (1) {
6829 if (now != dev) {
6830 ret = fn(now, priv);
6831 if (ret)
6832 return ret;
6833 }
6834
6835 next = NULL;
6836 while (1) {
6837 udev = netdev_next_upper_dev_rcu(now, &iter);
6838 if (!udev)
6839 break;
6840
6841 next = udev;
6842 niter = &udev->adj_list.upper;
6843 dev_stack[cur] = now;
6844 iter_stack[cur++] = iter;
6845 break;
6846 }
6847
6848 if (!next) {
6849 if (!cur)
6850 return 0;
6851 next = dev_stack[--cur];
6852 niter = iter_stack[cur];
6853 }
6854
6855 now = next;
6856 iter = niter;
6857 }
6858
6859 return 0;
6860}
6861EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6862
6863static bool __netdev_has_upper_dev(struct net_device *dev,
6864 struct net_device *upper_dev)
6865{
6866 struct netdev_nested_priv priv = {
6867 .flags = 0,
6868 .data = (void *)upper_dev,
6869 };
6870
6871 ASSERT_RTNL();
6872
6873 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
6874 &priv);
6875}
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888void *netdev_lower_get_next_private(struct net_device *dev,
6889 struct list_head **iter)
6890{
6891 struct netdev_adjacent *lower;
6892
6893 lower = list_entry(*iter, struct netdev_adjacent, list);
6894
6895 if (&lower->list == &dev->adj_list.lower)
6896 return NULL;
6897
6898 *iter = lower->list.next;
6899
6900 return lower->private;
6901}
6902EXPORT_SYMBOL(netdev_lower_get_next_private);
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914void *netdev_lower_get_next_private_rcu(struct net_device *dev,
6915 struct list_head **iter)
6916{
6917 struct netdev_adjacent *lower;
6918
6919 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
6920
6921 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6922
6923 if (&lower->list == &dev->adj_list.lower)
6924 return NULL;
6925
6926 *iter = &lower->list;
6927
6928 return lower->private;
6929}
6930EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
6944{
6945 struct netdev_adjacent *lower;
6946
6947 lower = list_entry(*iter, struct netdev_adjacent, list);
6948
6949 if (&lower->list == &dev->adj_list.lower)
6950 return NULL;
6951
6952 *iter = lower->list.next;
6953
6954 return lower->dev;
6955}
6956EXPORT_SYMBOL(netdev_lower_get_next);
6957
6958static struct net_device *netdev_next_lower_dev(struct net_device *dev,
6959 struct list_head **iter)
6960{
6961 struct netdev_adjacent *lower;
6962
6963 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
6964
6965 if (&lower->list == &dev->adj_list.lower)
6966 return NULL;
6967
6968 *iter = &lower->list;
6969
6970 return lower->dev;
6971}
6972
6973static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
6974 struct list_head **iter,
6975 bool *ignore)
6976{
6977 struct netdev_adjacent *lower;
6978
6979 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
6980
6981 if (&lower->list == &dev->adj_list.lower)
6982 return NULL;
6983
6984 *iter = &lower->list;
6985 *ignore = lower->ignore;
6986
6987 return lower->dev;
6988}
6989
6990int netdev_walk_all_lower_dev(struct net_device *dev,
6991 int (*fn)(struct net_device *dev,
6992 struct netdev_nested_priv *priv),
6993 struct netdev_nested_priv *priv)
6994{
6995 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6996 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6997 int ret, cur = 0;
6998
6999 now = dev;
7000 iter = &dev->adj_list.lower;
7001
7002 while (1) {
7003 if (now != dev) {
7004 ret = fn(now, priv);
7005 if (ret)
7006 return ret;
7007 }
7008
7009 next = NULL;
7010 while (1) {
7011 ldev = netdev_next_lower_dev(now, &iter);
7012 if (!ldev)
7013 break;
7014
7015 next = ldev;
7016 niter = &ldev->adj_list.lower;
7017 dev_stack[cur] = now;
7018 iter_stack[cur++] = iter;
7019 break;
7020 }
7021
7022 if (!next) {
7023 if (!cur)
7024 return 0;
7025 next = dev_stack[--cur];
7026 niter = iter_stack[cur];
7027 }
7028
7029 now = next;
7030 iter = niter;
7031 }
7032
7033 return 0;
7034}
7035EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7036
7037static int __netdev_walk_all_lower_dev(struct net_device *dev,
7038 int (*fn)(struct net_device *dev,
7039 struct netdev_nested_priv *priv),
7040 struct netdev_nested_priv *priv)
7041{
7042 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7043 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7044 int ret, cur = 0;
7045 bool ignore;
7046
7047 now = dev;
7048 iter = &dev->adj_list.lower;
7049
7050 while (1) {
7051 if (now != dev) {
7052 ret = fn(now, priv);
7053 if (ret)
7054 return ret;
7055 }
7056
7057 next = NULL;
7058 while (1) {
7059 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7060 if (!ldev)
7061 break;
7062 if (ignore)
7063 continue;
7064
7065 next = ldev;
7066 niter = &ldev->adj_list.lower;
7067 dev_stack[cur] = now;
7068 iter_stack[cur++] = iter;
7069 break;
7070 }
7071
7072 if (!next) {
7073 if (!cur)
7074 return 0;
7075 next = dev_stack[--cur];
7076 niter = iter_stack[cur];
7077 }
7078
7079 now = next;
7080 iter = niter;
7081 }
7082
7083 return 0;
7084}
7085
7086struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7087 struct list_head **iter)
7088{
7089 struct netdev_adjacent *lower;
7090
7091 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7092 if (&lower->list == &dev->adj_list.lower)
7093 return NULL;
7094
7095 *iter = &lower->list;
7096
7097 return lower->dev;
7098}
7099EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
7100
7101static u8 __netdev_upper_depth(struct net_device *dev)
7102{
7103 struct net_device *udev;
7104 struct list_head *iter;
7105 u8 max_depth = 0;
7106 bool ignore;
7107
7108 for (iter = &dev->adj_list.upper,
7109 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
7110 udev;
7111 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7112 if (ignore)
7113 continue;
7114 if (max_depth < udev->upper_level)
7115 max_depth = udev->upper_level;
7116 }
7117
7118 return max_depth;
7119}
7120
7121static u8 __netdev_lower_depth(struct net_device *dev)
7122{
7123 struct net_device *ldev;
7124 struct list_head *iter;
7125 u8 max_depth = 0;
7126 bool ignore;
7127
7128 for (iter = &dev->adj_list.lower,
7129 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
7130 ldev;
7131 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7132 if (ignore)
7133 continue;
7134 if (max_depth < ldev->lower_level)
7135 max_depth = ldev->lower_level;
7136 }
7137
7138 return max_depth;
7139}
7140
7141static int __netdev_update_upper_level(struct net_device *dev,
7142 struct netdev_nested_priv *__unused)
7143{
7144 dev->upper_level = __netdev_upper_depth(dev) + 1;
7145 return 0;
7146}
7147
7148static int __netdev_update_lower_level(struct net_device *dev,
7149 struct netdev_nested_priv *priv)
7150{
7151 dev->lower_level = __netdev_lower_depth(dev) + 1;
7152
7153#ifdef CONFIG_LOCKDEP
7154 if (!priv)
7155 return 0;
7156
7157 if (priv->flags & NESTED_SYNC_IMM)
7158 dev->nested_level = dev->lower_level - 1;
7159 if (priv->flags & NESTED_SYNC_TODO)
7160 net_unlink_todo(dev);
7161#endif
7162 return 0;
7163}
7164
7165int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7166 int (*fn)(struct net_device *dev,
7167 struct netdev_nested_priv *priv),
7168 struct netdev_nested_priv *priv)
7169{
7170 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7171 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7172 int ret, cur = 0;
7173
7174 now = dev;
7175 iter = &dev->adj_list.lower;
7176
7177 while (1) {
7178 if (now != dev) {
7179 ret = fn(now, priv);
7180 if (ret)
7181 return ret;
7182 }
7183
7184 next = NULL;
7185 while (1) {
7186 ldev = netdev_next_lower_dev_rcu(now, &iter);
7187 if (!ldev)
7188 break;
7189
7190 next = ldev;
7191 niter = &ldev->adj_list.lower;
7192 dev_stack[cur] = now;
7193 iter_stack[cur++] = iter;
7194 break;
7195 }
7196
7197 if (!next) {
7198 if (!cur)
7199 return 0;
7200 next = dev_stack[--cur];
7201 niter = iter_stack[cur];
7202 }
7203
7204 now = next;
7205 iter = niter;
7206 }
7207
7208 return 0;
7209}
7210EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7222{
7223 struct netdev_adjacent *lower;
7224
7225 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7226 struct netdev_adjacent, list);
7227 if (lower)
7228 return lower->private;
7229 return NULL;
7230}
7231EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7232
7233
7234
7235
7236
7237
7238
7239
7240struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7241{
7242 struct netdev_adjacent *upper;
7243
7244 upper = list_first_or_null_rcu(&dev->adj_list.upper,
7245 struct netdev_adjacent, list);
7246 if (upper && likely(upper->master))
7247 return upper->dev;
7248 return NULL;
7249}
7250EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7251
7252static int netdev_adjacent_sysfs_add(struct net_device *dev,
7253 struct net_device *adj_dev,
7254 struct list_head *dev_list)
7255{
7256 char linkname[IFNAMSIZ+7];
7257
7258 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7259 "upper_%s" : "lower_%s", adj_dev->name);
7260 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7261 linkname);
7262}
7263static void netdev_adjacent_sysfs_del(struct net_device *dev,
7264 char *name,
7265 struct list_head *dev_list)
7266{
7267 char linkname[IFNAMSIZ+7];
7268
7269 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7270 "upper_%s" : "lower_%s", name);
7271 sysfs_remove_link(&(dev->dev.kobj), linkname);
7272}
7273
7274static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7275 struct net_device *adj_dev,
7276 struct list_head *dev_list)
7277{
7278 return (dev_list == &dev->adj_list.upper ||
7279 dev_list == &dev->adj_list.lower) &&
7280 net_eq(dev_net(dev), dev_net(adj_dev));
7281}
7282
7283static int __netdev_adjacent_dev_insert(struct net_device *dev,
7284 struct net_device *adj_dev,
7285 struct list_head *dev_list,
7286 void *private, bool master)
7287{
7288 struct netdev_adjacent *adj;
7289 int ret;
7290
7291 adj = __netdev_find_adj(adj_dev, dev_list);
7292
7293 if (adj) {
7294 adj->ref_nr += 1;
7295 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7296 dev->name, adj_dev->name, adj->ref_nr);
7297
7298 return 0;
7299 }
7300
7301 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7302 if (!adj)
7303 return -ENOMEM;
7304
7305 adj->dev = adj_dev;
7306 adj->master = master;
7307 adj->ref_nr = 1;
7308 adj->private = private;
7309 adj->ignore = false;
7310 dev_hold_track(adj_dev, &adj->dev_tracker, GFP_KERNEL);
7311
7312 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7313 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
7314
7315 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
7316 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
7317 if (ret)
7318 goto free_adj;
7319 }
7320
7321
7322 if (master) {
7323 ret = sysfs_create_link(&(dev->dev.kobj),
7324 &(adj_dev->dev.kobj), "master");
7325 if (ret)
7326 goto remove_symlinks;
7327
7328 list_add_rcu(&adj->list, dev_list);
7329 } else {
7330 list_add_tail_rcu(&adj->list, dev_list);
7331 }
7332
7333 return 0;
7334
7335remove_symlinks:
7336 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
7337 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
7338free_adj:
7339 dev_put_track(adj_dev, &adj->dev_tracker);
7340 kfree(adj);
7341
7342 return ret;
7343}
7344
7345static void __netdev_adjacent_dev_remove(struct net_device *dev,
7346 struct net_device *adj_dev,
7347 u16 ref_nr,
7348 struct list_head *dev_list)
7349{
7350 struct netdev_adjacent *adj;
7351
7352 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7353 dev->name, adj_dev->name, ref_nr);
7354
7355 adj = __netdev_find_adj(adj_dev, dev_list);
7356
7357 if (!adj) {
7358 pr_err("Adjacency does not exist for device %s from %s\n",
7359 dev->name, adj_dev->name);
7360 WARN_ON(1);
7361 return;
7362 }
7363
7364 if (adj->ref_nr > ref_nr) {
7365 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7366 dev->name, adj_dev->name, ref_nr,
7367 adj->ref_nr - ref_nr);
7368 adj->ref_nr -= ref_nr;
7369 return;
7370 }
7371
7372 if (adj->master)
7373 sysfs_remove_link(&(dev->dev.kobj), "master");
7374
7375 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
7376 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
7377
7378 list_del_rcu(&adj->list);
7379 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
7380 adj_dev->name, dev->name, adj_dev->name);
7381 dev_put_track(adj_dev, &adj->dev_tracker);
7382 kfree_rcu(adj, rcu);
7383}
7384
7385static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7386 struct net_device *upper_dev,
7387 struct list_head *up_list,
7388 struct list_head *down_list,
7389 void *private, bool master)
7390{
7391 int ret;
7392
7393 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
7394 private, master);
7395 if (ret)
7396 return ret;
7397
7398 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
7399 private, false);
7400 if (ret) {
7401 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
7402 return ret;
7403 }
7404
7405 return 0;
7406}
7407
7408static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7409 struct net_device *upper_dev,
7410 u16 ref_nr,
7411 struct list_head *up_list,
7412 struct list_head *down_list)
7413{
7414 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7415 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
7416}
7417
7418static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7419 struct net_device *upper_dev,
7420 void *private, bool master)
7421{
7422 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7423 &dev->adj_list.upper,
7424 &upper_dev->adj_list.lower,
7425 private, master);
7426}
7427
7428static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7429 struct net_device *upper_dev)
7430{
7431 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
7432 &dev->adj_list.upper,
7433 &upper_dev->adj_list.lower);
7434}
7435
7436static int __netdev_upper_dev_link(struct net_device *dev,
7437 struct net_device *upper_dev, bool master,
7438 void *upper_priv, void *upper_info,
7439 struct netdev_nested_priv *priv,
7440 struct netlink_ext_ack *extack)
7441{
7442 struct netdev_notifier_changeupper_info changeupper_info = {
7443 .info = {
7444 .dev = dev,
7445 .extack = extack,
7446 },
7447 .upper_dev = upper_dev,
7448 .master = master,
7449 .linking = true,
7450 .upper_info = upper_info,
7451 };
7452 struct net_device *master_dev;
7453 int ret = 0;
7454
7455 ASSERT_RTNL();
7456
7457 if (dev == upper_dev)
7458 return -EBUSY;
7459
7460
7461 if (__netdev_has_upper_dev(upper_dev, dev))
7462 return -EBUSY;
7463
7464 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7465 return -EMLINK;
7466
7467 if (!master) {
7468 if (__netdev_has_upper_dev(dev, upper_dev))
7469 return -EEXIST;
7470 } else {
7471 master_dev = __netdev_master_upper_dev_get(dev);
7472 if (master_dev)
7473 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7474 }
7475
7476 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
7477 &changeupper_info.info);
7478 ret = notifier_to_errno(ret);
7479 if (ret)
7480 return ret;
7481
7482 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
7483 master);
7484 if (ret)
7485 return ret;
7486
7487 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
7488 &changeupper_info.info);
7489 ret = notifier_to_errno(ret);
7490 if (ret)
7491 goto rollback;
7492
7493 __netdev_update_upper_level(dev, NULL);
7494 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
7495
7496 __netdev_update_lower_level(upper_dev, priv);
7497 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7498 priv);
7499
7500 return 0;
7501
7502rollback:
7503 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
7504
7505 return ret;
7506}
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519int netdev_upper_dev_link(struct net_device *dev,
7520 struct net_device *upper_dev,
7521 struct netlink_ext_ack *extack)
7522{
7523 struct netdev_nested_priv priv = {
7524 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7525 .data = NULL,
7526 };
7527
7528 return __netdev_upper_dev_link(dev, upper_dev, false,
7529 NULL, NULL, &priv, extack);
7530}
7531EXPORT_SYMBOL(netdev_upper_dev_link);
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547int netdev_master_upper_dev_link(struct net_device *dev,
7548 struct net_device *upper_dev,
7549 void *upper_priv, void *upper_info,
7550 struct netlink_ext_ack *extack)
7551{
7552 struct netdev_nested_priv priv = {
7553 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7554 .data = NULL,
7555 };
7556
7557 return __netdev_upper_dev_link(dev, upper_dev, true,
7558 upper_priv, upper_info, &priv, extack);
7559}
7560EXPORT_SYMBOL(netdev_master_upper_dev_link);
7561
7562static void __netdev_upper_dev_unlink(struct net_device *dev,
7563 struct net_device *upper_dev,
7564 struct netdev_nested_priv *priv)
7565{
7566 struct netdev_notifier_changeupper_info changeupper_info = {
7567 .info = {
7568 .dev = dev,
7569 },
7570 .upper_dev = upper_dev,
7571 .linking = false,
7572 };
7573
7574 ASSERT_RTNL();
7575
7576 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
7577
7578 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
7579 &changeupper_info.info);
7580
7581 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
7582
7583 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
7584 &changeupper_info.info);
7585
7586 __netdev_update_upper_level(dev, NULL);
7587 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
7588
7589 __netdev_update_lower_level(upper_dev, priv);
7590 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7591 priv);
7592}
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602void netdev_upper_dev_unlink(struct net_device *dev,
7603 struct net_device *upper_dev)
7604{
7605 struct netdev_nested_priv priv = {
7606 .flags = NESTED_SYNC_TODO,
7607 .data = NULL,
7608 };
7609
7610 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
7611}
7612EXPORT_SYMBOL(netdev_upper_dev_unlink);
7613
7614static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7615 struct net_device *lower_dev,
7616 bool val)
7617{
7618 struct netdev_adjacent *adj;
7619
7620 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7621 if (adj)
7622 adj->ignore = val;
7623
7624 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7625 if (adj)
7626 adj->ignore = val;
7627}
7628
7629static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7630 struct net_device *lower_dev)
7631{
7632 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7633}
7634
7635static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7636 struct net_device *lower_dev)
7637{
7638 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7639}
7640
7641int netdev_adjacent_change_prepare(struct net_device *old_dev,
7642 struct net_device *new_dev,
7643 struct net_device *dev,
7644 struct netlink_ext_ack *extack)
7645{
7646 struct netdev_nested_priv priv = {
7647 .flags = 0,
7648 .data = NULL,
7649 };
7650 int err;
7651
7652 if (!new_dev)
7653 return 0;
7654
7655 if (old_dev && new_dev != old_dev)
7656 netdev_adjacent_dev_disable(dev, old_dev);
7657 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
7658 extack);
7659 if (err) {
7660 if (old_dev && new_dev != old_dev)
7661 netdev_adjacent_dev_enable(dev, old_dev);
7662 return err;
7663 }
7664
7665 return 0;
7666}
7667EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7668
7669void netdev_adjacent_change_commit(struct net_device *old_dev,
7670 struct net_device *new_dev,
7671 struct net_device *dev)
7672{
7673 struct netdev_nested_priv priv = {
7674 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7675 .data = NULL,
7676 };
7677
7678 if (!new_dev || !old_dev)
7679 return;
7680
7681 if (new_dev == old_dev)
7682 return;
7683
7684 netdev_adjacent_dev_enable(dev, old_dev);
7685 __netdev_upper_dev_unlink(old_dev, dev, &priv);
7686}
7687EXPORT_SYMBOL(netdev_adjacent_change_commit);
7688
7689void netdev_adjacent_change_abort(struct net_device *old_dev,
7690 struct net_device *new_dev,
7691 struct net_device *dev)
7692{
7693 struct netdev_nested_priv priv = {
7694 .flags = 0,
7695 .data = NULL,
7696 };
7697
7698 if (!new_dev)
7699 return;
7700
7701 if (old_dev && new_dev != old_dev)
7702 netdev_adjacent_dev_enable(dev, old_dev);
7703
7704 __netdev_upper_dev_unlink(new_dev, dev, &priv);
7705}
7706EXPORT_SYMBOL(netdev_adjacent_change_abort);
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716void netdev_bonding_info_change(struct net_device *dev,
7717 struct netdev_bonding_info *bonding_info)
7718{
7719 struct netdev_notifier_bonding_info info = {
7720 .info.dev = dev,
7721 };
7722
7723 memcpy(&info.bonding_info, bonding_info,
7724 sizeof(struct netdev_bonding_info));
7725 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
7726 &info.info);
7727}
7728EXPORT_SYMBOL(netdev_bonding_info_change);
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741struct net_device *netdev_get_xmit_slave(struct net_device *dev,
7742 struct sk_buff *skb,
7743 bool all_slaves)
7744{
7745 const struct net_device_ops *ops = dev->netdev_ops;
7746
7747 if (!ops->ndo_get_xmit_slave)
7748 return NULL;
7749 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
7750}
7751EXPORT_SYMBOL(netdev_get_xmit_slave);
7752
7753static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev,
7754 struct sock *sk)
7755{
7756 const struct net_device_ops *ops = dev->netdev_ops;
7757
7758 if (!ops->ndo_sk_get_lower_dev)
7759 return NULL;
7760 return ops->ndo_sk_get_lower_dev(dev, sk);
7761}
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
7772 struct sock *sk)
7773{
7774 struct net_device *lower;
7775
7776 lower = netdev_sk_get_lower_dev(dev, sk);
7777 while (lower) {
7778 dev = lower;
7779 lower = netdev_sk_get_lower_dev(dev, sk);
7780 }
7781
7782 return dev;
7783}
7784EXPORT_SYMBOL(netdev_sk_get_lowest_dev);
7785
7786static void netdev_adjacent_add_links(struct net_device *dev)
7787{
7788 struct netdev_adjacent *iter;
7789
7790 struct net *net = dev_net(dev);
7791
7792 list_for_each_entry(iter, &dev->adj_list.upper, list) {
7793 if (!net_eq(net, dev_net(iter->dev)))
7794 continue;
7795 netdev_adjacent_sysfs_add(iter->dev, dev,
7796 &iter->dev->adj_list.lower);
7797 netdev_adjacent_sysfs_add(dev, iter->dev,
7798 &dev->adj_list.upper);
7799 }
7800
7801 list_for_each_entry(iter, &dev->adj_list.lower, list) {
7802 if (!net_eq(net, dev_net(iter->dev)))
7803 continue;
7804 netdev_adjacent_sysfs_add(iter->dev, dev,
7805 &iter->dev->adj_list.upper);
7806 netdev_adjacent_sysfs_add(dev, iter->dev,
7807 &dev->adj_list.lower);
7808 }
7809}
7810
7811static void netdev_adjacent_del_links(struct net_device *dev)
7812{
7813 struct netdev_adjacent *iter;
7814
7815 struct net *net = dev_net(dev);
7816
7817 list_for_each_entry(iter, &dev->adj_list.upper, list) {
7818 if (!net_eq(net, dev_net(iter->dev)))
7819 continue;
7820 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7821 &iter->dev->adj_list.lower);
7822 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7823 &dev->adj_list.upper);
7824 }
7825
7826 list_for_each_entry(iter, &dev->adj_list.lower, list) {
7827 if (!net_eq(net, dev_net(iter->dev)))
7828 continue;
7829 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7830 &iter->dev->adj_list.upper);
7831 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7832 &dev->adj_list.lower);
7833 }
7834}
7835
7836void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
7837{
7838 struct netdev_adjacent *iter;
7839
7840 struct net *net = dev_net(dev);
7841
7842 list_for_each_entry(iter, &dev->adj_list.upper, list) {
7843 if (!net_eq(net, dev_net(iter->dev)))
7844 continue;
7845 netdev_adjacent_sysfs_del(iter->dev, oldname,
7846 &iter->dev->adj_list.lower);
7847 netdev_adjacent_sysfs_add(iter->dev, dev,
7848 &iter->dev->adj_list.lower);
7849 }
7850
7851 list_for_each_entry(iter, &dev->adj_list.lower, list) {
7852 if (!net_eq(net, dev_net(iter->dev)))
7853 continue;
7854 netdev_adjacent_sysfs_del(iter->dev, oldname,
7855 &iter->dev->adj_list.upper);
7856 netdev_adjacent_sysfs_add(iter->dev, dev,
7857 &iter->dev->adj_list.upper);
7858 }
7859}
7860
7861void *netdev_lower_dev_get_private(struct net_device *dev,
7862 struct net_device *lower_dev)
7863{
7864 struct netdev_adjacent *lower;
7865
7866 if (!lower_dev)
7867 return NULL;
7868 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
7869 if (!lower)
7870 return NULL;
7871
7872 return lower->private;
7873}
7874EXPORT_SYMBOL(netdev_lower_dev_get_private);
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885void netdev_lower_state_changed(struct net_device *lower_dev,
7886 void *lower_state_info)
7887{
7888 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7889 .info.dev = lower_dev,
7890 };
7891
7892 ASSERT_RTNL();
7893 changelowerstate_info.lower_state_info = lower_state_info;
7894 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
7895 &changelowerstate_info.info);
7896}
7897EXPORT_SYMBOL(netdev_lower_state_changed);
7898
7899static void dev_change_rx_flags(struct net_device *dev, int flags)
7900{
7901 const struct net_device_ops *ops = dev->netdev_ops;
7902
7903 if (ops->ndo_change_rx_flags)
7904 ops->ndo_change_rx_flags(dev, flags);
7905}
7906
7907static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
7908{
7909 unsigned int old_flags = dev->flags;
7910 kuid_t uid;
7911 kgid_t gid;
7912
7913 ASSERT_RTNL();
7914
7915 dev->flags |= IFF_PROMISC;
7916 dev->promiscuity += inc;
7917 if (dev->promiscuity == 0) {
7918
7919
7920
7921
7922 if (inc < 0)
7923 dev->flags &= ~IFF_PROMISC;
7924 else {
7925 dev->promiscuity -= inc;
7926 netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n");
7927 return -EOVERFLOW;
7928 }
7929 }
7930 if (dev->flags != old_flags) {
7931 pr_info("device %s %s promiscuous mode\n",
7932 dev->name,
7933 dev->flags & IFF_PROMISC ? "entered" : "left");
7934 if (audit_enabled) {
7935 current_uid_gid(&uid, &gid);
7936 audit_log(audit_context(), GFP_ATOMIC,
7937 AUDIT_ANOM_PROMISCUOUS,
7938 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7939 dev->name, (dev->flags & IFF_PROMISC),
7940 (old_flags & IFF_PROMISC),
7941 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7942 from_kuid(&init_user_ns, uid),
7943 from_kgid(&init_user_ns, gid),
7944 audit_get_sessionid(current));
7945 }
7946
7947 dev_change_rx_flags(dev, IFF_PROMISC);
7948 }
7949 if (notify)
7950 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
7951 return 0;
7952}
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965int dev_set_promiscuity(struct net_device *dev, int inc)
7966{
7967 unsigned int old_flags = dev->flags;
7968 int err;
7969
7970 err = __dev_set_promiscuity(dev, inc, true);
7971 if (err < 0)
7972 return err;
7973 if (dev->flags != old_flags)
7974 dev_set_rx_mode(dev);
7975 return err;
7976}
7977EXPORT_SYMBOL(dev_set_promiscuity);
7978
7979static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
7980{
7981 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
7982
7983 ASSERT_RTNL();
7984
7985 dev->flags |= IFF_ALLMULTI;
7986 dev->allmulti += inc;
7987 if (dev->allmulti == 0) {
7988
7989
7990
7991
7992 if (inc < 0)
7993 dev->flags &= ~IFF_ALLMULTI;
7994 else {
7995 dev->allmulti -= inc;
7996 netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n");
7997 return -EOVERFLOW;
7998 }
7999 }
8000 if (dev->flags ^ old_flags) {
8001 dev_change_rx_flags(dev, IFF_ALLMULTI);
8002 dev_set_rx_mode(dev);
8003 if (notify)
8004 __dev_notify_flags(dev, old_flags,
8005 dev->gflags ^ old_gflags);
8006 }
8007 return 0;
8008}
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023int dev_set_allmulti(struct net_device *dev, int inc)
8024{
8025 return __dev_set_allmulti(dev, inc, true);
8026}
8027EXPORT_SYMBOL(dev_set_allmulti);
8028
8029
8030
8031
8032
8033
8034
8035void __dev_set_rx_mode(struct net_device *dev)
8036{
8037 const struct net_device_ops *ops = dev->netdev_ops;
8038
8039
8040 if (!(dev->flags&IFF_UP))
8041 return;
8042
8043 if (!netif_device_present(dev))
8044 return;
8045
8046 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
8047
8048
8049
8050 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
8051 __dev_set_promiscuity(dev, 1, false);
8052 dev->uc_promisc = true;
8053 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
8054 __dev_set_promiscuity(dev, -1, false);
8055 dev->uc_promisc = false;
8056 }
8057 }
8058
8059 if (ops->ndo_set_rx_mode)
8060 ops->ndo_set_rx_mode(dev);
8061}
8062
8063void dev_set_rx_mode(struct net_device *dev)
8064{
8065 netif_addr_lock_bh(dev);
8066 __dev_set_rx_mode(dev);
8067 netif_addr_unlock_bh(dev);
8068}
8069
8070
8071
8072
8073
8074
8075
8076unsigned int dev_get_flags(const struct net_device *dev)
8077{
8078 unsigned int flags;
8079
8080 flags = (dev->flags & ~(IFF_PROMISC |
8081 IFF_ALLMULTI |
8082 IFF_RUNNING |
8083 IFF_LOWER_UP |
8084 IFF_DORMANT)) |
8085 (dev->gflags & (IFF_PROMISC |
8086 IFF_ALLMULTI));
8087
8088 if (netif_running(dev)) {
8089 if (netif_oper_up(dev))
8090 flags |= IFF_RUNNING;
8091 if (netif_carrier_ok(dev))
8092 flags |= IFF_LOWER_UP;
8093 if (netif_dormant(dev))
8094 flags |= IFF_DORMANT;
8095 }
8096
8097 return flags;
8098}
8099EXPORT_SYMBOL(dev_get_flags);
8100
8101int __dev_change_flags(struct net_device *dev, unsigned int flags,
8102 struct netlink_ext_ack *extack)
8103{
8104 unsigned int old_flags = dev->flags;
8105 int ret;
8106
8107 ASSERT_RTNL();
8108
8109
8110
8111
8112
8113 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8114 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8115 IFF_AUTOMEDIA)) |
8116 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8117 IFF_ALLMULTI));
8118
8119
8120
8121
8122
8123 if ((old_flags ^ flags) & IFF_MULTICAST)
8124 dev_change_rx_flags(dev, IFF_MULTICAST);
8125
8126 dev_set_rx_mode(dev);
8127
8128
8129
8130
8131
8132
8133
8134 ret = 0;
8135 if ((old_flags ^ flags) & IFF_UP) {
8136 if (old_flags & IFF_UP)
8137 __dev_close(dev);
8138 else
8139 ret = __dev_open(dev, extack);
8140 }
8141
8142 if ((flags ^ dev->gflags) & IFF_PROMISC) {
8143 int inc = (flags & IFF_PROMISC) ? 1 : -1;
8144 unsigned int old_flags = dev->flags;
8145
8146 dev->gflags ^= IFF_PROMISC;
8147
8148 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8149 if (dev->flags != old_flags)
8150 dev_set_rx_mode(dev);
8151 }
8152
8153
8154
8155
8156
8157 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
8158 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8159
8160 dev->gflags ^= IFF_ALLMULTI;
8161 __dev_set_allmulti(dev, inc, false);
8162 }
8163
8164 return ret;
8165}
8166
8167void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8168 unsigned int gchanges)
8169{
8170 unsigned int changes = dev->flags ^ old_flags;
8171
8172 if (gchanges)
8173 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
8174
8175 if (changes & IFF_UP) {
8176 if (dev->flags & IFF_UP)
8177 call_netdevice_notifiers(NETDEV_UP, dev);
8178 else
8179 call_netdevice_notifiers(NETDEV_DOWN, dev);
8180 }
8181
8182 if (dev->flags & IFF_UP &&
8183 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
8184 struct netdev_notifier_change_info change_info = {
8185 .info = {
8186 .dev = dev,
8187 },
8188 .flags_changed = changes,
8189 };
8190
8191 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
8192 }
8193}
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204int dev_change_flags(struct net_device *dev, unsigned int flags,
8205 struct netlink_ext_ack *extack)
8206{
8207 int ret;
8208 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
8209
8210 ret = __dev_change_flags(dev, flags, extack);
8211 if (ret < 0)
8212 return ret;
8213
8214 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
8215 __dev_notify_flags(dev, old_flags, changes);
8216 return ret;
8217}
8218EXPORT_SYMBOL(dev_change_flags);
8219
8220int __dev_set_mtu(struct net_device *dev, int new_mtu)
8221{
8222 const struct net_device_ops *ops = dev->netdev_ops;
8223
8224 if (ops->ndo_change_mtu)
8225 return ops->ndo_change_mtu(dev, new_mtu);
8226
8227
8228 WRITE_ONCE(dev->mtu, new_mtu);
8229 return 0;
8230}
8231EXPORT_SYMBOL(__dev_set_mtu);
8232
8233int dev_validate_mtu(struct net_device *dev, int new_mtu,
8234 struct netlink_ext_ack *extack)
8235{
8236
8237 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8238 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8239 return -EINVAL;
8240 }
8241
8242 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8243 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8244 return -EINVAL;
8245 }
8246 return 0;
8247}
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8258 struct netlink_ext_ack *extack)
8259{
8260 int err, orig_mtu;
8261
8262 if (new_mtu == dev->mtu)
8263 return 0;
8264
8265 err = dev_validate_mtu(dev, new_mtu, extack);
8266 if (err)
8267 return err;
8268
8269 if (!netif_device_present(dev))
8270 return -ENODEV;
8271
8272 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8273 err = notifier_to_errno(err);
8274 if (err)
8275 return err;
8276
8277 orig_mtu = dev->mtu;
8278 err = __dev_set_mtu(dev, new_mtu);
8279
8280 if (!err) {
8281 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8282 orig_mtu);
8283 err = notifier_to_errno(err);
8284 if (err) {
8285
8286
8287
8288 __dev_set_mtu(dev, orig_mtu);
8289 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8290 new_mtu);
8291 }
8292 }
8293 return err;
8294}
8295
8296int dev_set_mtu(struct net_device *dev, int new_mtu)
8297{
8298 struct netlink_ext_ack extack;
8299 int err;
8300
8301 memset(&extack, 0, sizeof(extack));
8302 err = dev_set_mtu_ext(dev, new_mtu, &extack);
8303 if (err && extack._msg)
8304 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8305 return err;
8306}
8307EXPORT_SYMBOL(dev_set_mtu);
8308
8309
8310
8311
8312
8313
8314int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8315{
8316 unsigned int orig_len = dev->tx_queue_len;
8317 int res;
8318
8319 if (new_len != (unsigned int)new_len)
8320 return -ERANGE;
8321
8322 if (new_len != orig_len) {
8323 dev->tx_queue_len = new_len;
8324 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8325 res = notifier_to_errno(res);
8326 if (res)
8327 goto err_rollback;
8328 res = dev_qdisc_change_tx_queue_len(dev);
8329 if (res)
8330 goto err_rollback;
8331 }
8332
8333 return 0;
8334
8335err_rollback:
8336 netdev_err(dev, "refused to change device tx_queue_len\n");
8337 dev->tx_queue_len = orig_len;
8338 return res;
8339}
8340
8341
8342
8343
8344
8345
8346void dev_set_group(struct net_device *dev, int new_group)
8347{
8348 dev->group = new_group;
8349}
8350EXPORT_SYMBOL(dev_set_group);
8351
8352
8353
8354
8355
8356
8357
8358int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8359 struct netlink_ext_ack *extack)
8360{
8361 struct netdev_notifier_pre_changeaddr_info info = {
8362 .info.dev = dev,
8363 .info.extack = extack,
8364 .dev_addr = addr,
8365 };
8366 int rc;
8367
8368 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8369 return notifier_to_errno(rc);
8370}
8371EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8382 struct netlink_ext_ack *extack)
8383{
8384 const struct net_device_ops *ops = dev->netdev_ops;
8385 int err;
8386
8387 if (!ops->ndo_set_mac_address)
8388 return -EOPNOTSUPP;
8389 if (sa->sa_family != dev->type)
8390 return -EINVAL;
8391 if (!netif_device_present(dev))
8392 return -ENODEV;
8393 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8394 if (err)
8395 return err;
8396 err = ops->ndo_set_mac_address(dev, sa);
8397 if (err)
8398 return err;
8399 dev->addr_assign_type = NET_ADDR_SET;
8400 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
8401 add_device_randomness(dev->dev_addr, dev->addr_len);
8402 return 0;
8403}
8404EXPORT_SYMBOL(dev_set_mac_address);
8405
8406static DECLARE_RWSEM(dev_addr_sem);
8407
8408int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
8409 struct netlink_ext_ack *extack)
8410{
8411 int ret;
8412
8413 down_write(&dev_addr_sem);
8414 ret = dev_set_mac_address(dev, sa, extack);
8415 up_write(&dev_addr_sem);
8416 return ret;
8417}
8418EXPORT_SYMBOL(dev_set_mac_address_user);
8419
8420int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
8421{
8422 size_t size = sizeof(sa->sa_data);
8423 struct net_device *dev;
8424 int ret = 0;
8425
8426 down_read(&dev_addr_sem);
8427 rcu_read_lock();
8428
8429 dev = dev_get_by_name_rcu(net, dev_name);
8430 if (!dev) {
8431 ret = -ENODEV;
8432 goto unlock;
8433 }
8434 if (!dev->addr_len)
8435 memset(sa->sa_data, 0, size);
8436 else
8437 memcpy(sa->sa_data, dev->dev_addr,
8438 min_t(size_t, size, dev->addr_len));
8439 sa->sa_family = dev->type;
8440
8441unlock:
8442 rcu_read_unlock();
8443 up_read(&dev_addr_sem);
8444 return ret;
8445}
8446EXPORT_SYMBOL(dev_get_mac_address);
8447
8448
8449
8450
8451
8452
8453
8454
8455int dev_change_carrier(struct net_device *dev, bool new_carrier)
8456{
8457 const struct net_device_ops *ops = dev->netdev_ops;
8458
8459 if (!ops->ndo_change_carrier)
8460 return -EOPNOTSUPP;
8461 if (!netif_device_present(dev))
8462 return -ENODEV;
8463 return ops->ndo_change_carrier(dev, new_carrier);
8464}
8465EXPORT_SYMBOL(dev_change_carrier);
8466
8467
8468
8469
8470
8471
8472
8473
8474int dev_get_phys_port_id(struct net_device *dev,
8475 struct netdev_phys_item_id *ppid)
8476{
8477 const struct net_device_ops *ops = dev->netdev_ops;
8478
8479 if (!ops->ndo_get_phys_port_id)
8480 return -EOPNOTSUPP;
8481 return ops->ndo_get_phys_port_id(dev, ppid);
8482}
8483EXPORT_SYMBOL(dev_get_phys_port_id);
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493int dev_get_phys_port_name(struct net_device *dev,
8494 char *name, size_t len)
8495{
8496 const struct net_device_ops *ops = dev->netdev_ops;
8497 int err;
8498
8499 if (ops->ndo_get_phys_port_name) {
8500 err = ops->ndo_get_phys_port_name(dev, name, len);
8501 if (err != -EOPNOTSUPP)
8502 return err;
8503 }
8504 return devlink_compat_phys_port_name_get(dev, name, len);
8505}
8506EXPORT_SYMBOL(dev_get_phys_port_name);
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516int dev_get_port_parent_id(struct net_device *dev,
8517 struct netdev_phys_item_id *ppid,
8518 bool recurse)
8519{
8520 const struct net_device_ops *ops = dev->netdev_ops;
8521 struct netdev_phys_item_id first = { };
8522 struct net_device *lower_dev;
8523 struct list_head *iter;
8524 int err;
8525
8526 if (ops->ndo_get_port_parent_id) {
8527 err = ops->ndo_get_port_parent_id(dev, ppid);
8528 if (err != -EOPNOTSUPP)
8529 return err;
8530 }
8531
8532 err = devlink_compat_switch_id_get(dev, ppid);
8533 if (!recurse || err != -EOPNOTSUPP)
8534 return err;
8535
8536 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8537 err = dev_get_port_parent_id(lower_dev, ppid, true);
8538 if (err)
8539 break;
8540 if (!first.id_len)
8541 first = *ppid;
8542 else if (memcmp(&first, ppid, sizeof(*ppid)))
8543 return -EOPNOTSUPP;
8544 }
8545
8546 return err;
8547}
8548EXPORT_SYMBOL(dev_get_port_parent_id);
8549
8550
8551
8552
8553
8554
8555
8556bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8557{
8558 struct netdev_phys_item_id a_id = { };
8559 struct netdev_phys_item_id b_id = { };
8560
8561 if (dev_get_port_parent_id(a, &a_id, true) ||
8562 dev_get_port_parent_id(b, &b_id, true))
8563 return false;
8564
8565 return netdev_phys_item_id_same(&a_id, &b_id);
8566}
8567EXPORT_SYMBOL(netdev_port_same_parent_id);
8568
8569
8570
8571
8572
8573
8574
8575int dev_change_proto_down(struct net_device *dev, bool proto_down)
8576{
8577 if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN))
8578 return -EOPNOTSUPP;
8579 if (!netif_device_present(dev))
8580 return -ENODEV;
8581 if (proto_down)
8582 netif_carrier_off(dev);
8583 else
8584 netif_carrier_on(dev);
8585 dev->proto_down = proto_down;
8586 return 0;
8587}
8588EXPORT_SYMBOL(dev_change_proto_down);
8589
8590
8591
8592
8593
8594
8595
8596
8597void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
8598 u32 value)
8599{
8600 int b;
8601
8602 if (!mask) {
8603 dev->proto_down_reason = value;
8604 } else {
8605 for_each_set_bit(b, &mask, 32) {
8606 if (value & (1 << b))
8607 dev->proto_down_reason |= BIT(b);
8608 else
8609 dev->proto_down_reason &= ~BIT(b);
8610 }
8611 }
8612}
8613EXPORT_SYMBOL(dev_change_proto_down_reason);
8614
8615struct bpf_xdp_link {
8616 struct bpf_link link;
8617 struct net_device *dev;
8618 int flags;
8619};
8620
8621static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
8622{
8623 if (flags & XDP_FLAGS_HW_MODE)
8624 return XDP_MODE_HW;
8625 if (flags & XDP_FLAGS_DRV_MODE)
8626 return XDP_MODE_DRV;
8627 if (flags & XDP_FLAGS_SKB_MODE)
8628 return XDP_MODE_SKB;
8629 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
8630}
8631
8632static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
8633{
8634 switch (mode) {
8635 case XDP_MODE_SKB:
8636 return generic_xdp_install;
8637 case XDP_MODE_DRV:
8638 case XDP_MODE_HW:
8639 return dev->netdev_ops->ndo_bpf;
8640 default:
8641 return NULL;
8642 }
8643}
8644
8645static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
8646 enum bpf_xdp_mode mode)
8647{
8648 return dev->xdp_state[mode].link;
8649}
8650
8651static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
8652 enum bpf_xdp_mode mode)
8653{
8654 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
8655
8656 if (link)
8657 return link->link.prog;
8658 return dev->xdp_state[mode].prog;
8659}
8660
8661u8 dev_xdp_prog_count(struct net_device *dev)
8662{
8663 u8 count = 0;
8664 int i;
8665
8666 for (i = 0; i < __MAX_XDP_MODE; i++)
8667 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
8668 count++;
8669 return count;
8670}
8671EXPORT_SYMBOL_GPL(dev_xdp_prog_count);
8672
8673u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
8674{
8675 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
8676
8677 return prog ? prog->aux->id : 0;
8678}
8679
8680static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
8681 struct bpf_xdp_link *link)
8682{
8683 dev->xdp_state[mode].link = link;
8684 dev->xdp_state[mode].prog = NULL;
8685}
8686
8687static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
8688 struct bpf_prog *prog)
8689{
8690 dev->xdp_state[mode].link = NULL;
8691 dev->xdp_state[mode].prog = prog;
8692}
8693
8694static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
8695 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
8696 u32 flags, struct bpf_prog *prog)
8697{
8698 struct netdev_bpf xdp;
8699 int err;
8700
8701 memset(&xdp, 0, sizeof(xdp));
8702 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
8703 xdp.extack = extack;
8704 xdp.flags = flags;
8705 xdp.prog = prog;
8706
8707
8708
8709
8710
8711
8712
8713 if (prog)
8714 bpf_prog_inc(prog);
8715 err = bpf_op(dev, &xdp);
8716 if (err) {
8717 if (prog)
8718 bpf_prog_put(prog);
8719 return err;
8720 }
8721
8722 if (mode != XDP_MODE_HW)
8723 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
8724
8725 return 0;
8726}
8727
8728static void dev_xdp_uninstall(struct net_device *dev)
8729{
8730 struct bpf_xdp_link *link;
8731 struct bpf_prog *prog;
8732 enum bpf_xdp_mode mode;
8733 bpf_op_t bpf_op;
8734
8735 ASSERT_RTNL();
8736
8737 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
8738 prog = dev_xdp_prog(dev, mode);
8739 if (!prog)
8740 continue;
8741
8742 bpf_op = dev_xdp_bpf_op(dev, mode);
8743 if (!bpf_op)
8744 continue;
8745
8746 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
8747
8748
8749 link = dev_xdp_link(dev, mode);
8750 if (link)
8751 link->dev = NULL;
8752 else
8753 bpf_prog_put(prog);
8754
8755 dev_xdp_set_link(dev, mode, NULL);
8756 }
8757}
8758
8759static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
8760 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
8761 struct bpf_prog *old_prog, u32 flags)
8762{
8763 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
8764 struct bpf_prog *cur_prog;
8765 struct net_device *upper;
8766 struct list_head *iter;
8767 enum bpf_xdp_mode mode;
8768 bpf_op_t bpf_op;
8769 int err;
8770
8771 ASSERT_RTNL();
8772
8773
8774 if (link && (new_prog || old_prog))
8775 return -EINVAL;
8776
8777 if (link && (flags & ~XDP_FLAGS_MODES)) {
8778 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
8779 return -EINVAL;
8780 }
8781
8782 if (num_modes > 1) {
8783 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
8784 return -EINVAL;
8785 }
8786
8787 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
8788 NL_SET_ERR_MSG(extack,
8789 "More than one program loaded, unset mode is ambiguous");
8790 return -EINVAL;
8791 }
8792
8793 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
8794 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
8795 return -EINVAL;
8796 }
8797
8798 mode = dev_xdp_mode(dev, flags);
8799
8800 if (dev_xdp_link(dev, mode)) {
8801 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
8802 return -EBUSY;
8803 }
8804
8805
8806 netdev_for_each_upper_dev_rcu(dev, upper, iter) {
8807 if (dev_xdp_prog_count(upper) > 0) {
8808 NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program");
8809 return -EEXIST;
8810 }
8811 }
8812
8813 cur_prog = dev_xdp_prog(dev, mode);
8814
8815 if (link && cur_prog) {
8816 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
8817 return -EBUSY;
8818 }
8819 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
8820 NL_SET_ERR_MSG(extack, "Active program does not match expected");
8821 return -EEXIST;
8822 }
8823
8824
8825 if (link)
8826 new_prog = link->link.prog;
8827
8828 if (new_prog) {
8829 bool offload = mode == XDP_MODE_HW;
8830 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
8831 ? XDP_MODE_DRV : XDP_MODE_SKB;
8832
8833 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
8834 NL_SET_ERR_MSG(extack, "XDP program already attached");
8835 return -EBUSY;
8836 }
8837 if (!offload && dev_xdp_prog(dev, other_mode)) {
8838 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
8839 return -EEXIST;
8840 }
8841 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
8842 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
8843 return -EINVAL;
8844 }
8845 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
8846 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
8847 return -EINVAL;
8848 }
8849 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
8850 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
8851 return -EINVAL;
8852 }
8853 }
8854
8855
8856 if (new_prog != cur_prog) {
8857 bpf_op = dev_xdp_bpf_op(dev, mode);
8858 if (!bpf_op) {
8859 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
8860 return -EOPNOTSUPP;
8861 }
8862
8863 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
8864 if (err)
8865 return err;
8866 }
8867
8868 if (link)
8869 dev_xdp_set_link(dev, mode, link);
8870 else
8871 dev_xdp_set_prog(dev, mode, new_prog);
8872 if (cur_prog)
8873 bpf_prog_put(cur_prog);
8874
8875 return 0;
8876}
8877
8878static int dev_xdp_attach_link(struct net_device *dev,
8879 struct netlink_ext_ack *extack,
8880 struct bpf_xdp_link *link)
8881{
8882 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
8883}
8884
8885static int dev_xdp_detach_link(struct net_device *dev,
8886 struct netlink_ext_ack *extack,
8887 struct bpf_xdp_link *link)
8888{
8889 enum bpf_xdp_mode mode;
8890 bpf_op_t bpf_op;
8891
8892 ASSERT_RTNL();
8893
8894 mode = dev_xdp_mode(dev, link->flags);
8895 if (dev_xdp_link(dev, mode) != link)
8896 return -EINVAL;
8897
8898 bpf_op = dev_xdp_bpf_op(dev, mode);
8899 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
8900 dev_xdp_set_link(dev, mode, NULL);
8901 return 0;
8902}
8903
8904static void bpf_xdp_link_release(struct bpf_link *link)
8905{
8906 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8907
8908 rtnl_lock();
8909
8910
8911
8912
8913 if (xdp_link->dev) {
8914 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
8915 xdp_link->dev = NULL;
8916 }
8917
8918 rtnl_unlock();
8919}
8920
8921static int bpf_xdp_link_detach(struct bpf_link *link)
8922{
8923 bpf_xdp_link_release(link);
8924 return 0;
8925}
8926
8927static void bpf_xdp_link_dealloc(struct bpf_link *link)
8928{
8929 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8930
8931 kfree(xdp_link);
8932}
8933
8934static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
8935 struct seq_file *seq)
8936{
8937 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8938 u32 ifindex = 0;
8939
8940 rtnl_lock();
8941 if (xdp_link->dev)
8942 ifindex = xdp_link->dev->ifindex;
8943 rtnl_unlock();
8944
8945 seq_printf(seq, "ifindex:\t%u\n", ifindex);
8946}
8947
8948static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
8949 struct bpf_link_info *info)
8950{
8951 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8952 u32 ifindex = 0;
8953
8954 rtnl_lock();
8955 if (xdp_link->dev)
8956 ifindex = xdp_link->dev->ifindex;
8957 rtnl_unlock();
8958
8959 info->xdp.ifindex = ifindex;
8960 return 0;
8961}
8962
8963static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
8964 struct bpf_prog *old_prog)
8965{
8966 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
8967 enum bpf_xdp_mode mode;
8968 bpf_op_t bpf_op;
8969 int err = 0;
8970
8971 rtnl_lock();
8972
8973
8974 if (!xdp_link->dev) {
8975 err = -ENOLINK;
8976 goto out_unlock;
8977 }
8978
8979 if (old_prog && link->prog != old_prog) {
8980 err = -EPERM;
8981 goto out_unlock;
8982 }
8983 old_prog = link->prog;
8984 if (old_prog->type != new_prog->type ||
8985 old_prog->expected_attach_type != new_prog->expected_attach_type) {
8986 err = -EINVAL;
8987 goto out_unlock;
8988 }
8989
8990 if (old_prog == new_prog) {
8991
8992 bpf_prog_put(new_prog);
8993 goto out_unlock;
8994 }
8995
8996 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
8997 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
8998 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
8999 xdp_link->flags, new_prog);
9000 if (err)
9001 goto out_unlock;
9002
9003 old_prog = xchg(&link->prog, new_prog);
9004 bpf_prog_put(old_prog);
9005
9006out_unlock:
9007 rtnl_unlock();
9008 return err;
9009}
9010
9011static const struct bpf_link_ops bpf_xdp_link_lops = {
9012 .release = bpf_xdp_link_release,
9013 .dealloc = bpf_xdp_link_dealloc,
9014 .detach = bpf_xdp_link_detach,
9015 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9016 .fill_link_info = bpf_xdp_link_fill_link_info,
9017 .update_prog = bpf_xdp_link_update,
9018};
9019
9020int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9021{
9022 struct net *net = current->nsproxy->net_ns;
9023 struct bpf_link_primer link_primer;
9024 struct bpf_xdp_link *link;
9025 struct net_device *dev;
9026 int err, fd;
9027
9028 rtnl_lock();
9029 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
9030 if (!dev) {
9031 rtnl_unlock();
9032 return -EINVAL;
9033 }
9034
9035 link = kzalloc(sizeof(*link), GFP_USER);
9036 if (!link) {
9037 err = -ENOMEM;
9038 goto unlock;
9039 }
9040
9041 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9042 link->dev = dev;
9043 link->flags = attr->link_create.flags;
9044
9045 err = bpf_link_prime(&link->link, &link_primer);
9046 if (err) {
9047 kfree(link);
9048 goto unlock;
9049 }
9050
9051 err = dev_xdp_attach_link(dev, NULL, link);
9052 rtnl_unlock();
9053
9054 if (err) {
9055 link->dev = NULL;
9056 bpf_link_cleanup(&link_primer);
9057 goto out_put_dev;
9058 }
9059
9060 fd = bpf_link_settle(&link_primer);
9061
9062 dev_put(dev);
9063 return fd;
9064
9065unlock:
9066 rtnl_unlock();
9067
9068out_put_dev:
9069 dev_put(dev);
9070 return err;
9071}
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
9084 int fd, int expected_fd, u32 flags)
9085{
9086 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
9087 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
9088 int err;
9089
9090 ASSERT_RTNL();
9091
9092 if (fd >= 0) {
9093 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9094 mode != XDP_MODE_SKB);
9095 if (IS_ERR(new_prog))
9096 return PTR_ERR(new_prog);
9097 }
9098
9099 if (expected_fd >= 0) {
9100 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9101 mode != XDP_MODE_SKB);
9102 if (IS_ERR(old_prog)) {
9103 err = PTR_ERR(old_prog);
9104 old_prog = NULL;
9105 goto err_out;
9106 }
9107 }
9108
9109 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
9110
9111err_out:
9112 if (err && new_prog)
9113 bpf_prog_put(new_prog);
9114 if (old_prog)
9115 bpf_prog_put(old_prog);
9116 return err;
9117}
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127static int dev_new_index(struct net *net)
9128{
9129 int ifindex = net->ifindex;
9130
9131 for (;;) {
9132 if (++ifindex <= 0)
9133 ifindex = 1;
9134 if (!__dev_get_by_index(net, ifindex))
9135 return net->ifindex = ifindex;
9136 }
9137}
9138
9139
9140static LIST_HEAD(net_todo_list);
9141DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
9142
9143static void net_set_todo(struct net_device *dev)
9144{
9145 list_add_tail(&dev->todo_list, &net_todo_list);
9146 dev_net(dev)->dev_unreg_count++;
9147}
9148
9149static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9150 struct net_device *upper, netdev_features_t features)
9151{
9152 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9153 netdev_features_t feature;
9154 int feature_bit;
9155
9156 for_each_netdev_feature(upper_disables, feature_bit) {
9157 feature = __NETIF_F_BIT(feature_bit);
9158 if (!(upper->wanted_features & feature)
9159 && (features & feature)) {
9160 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9161 &feature, upper->name);
9162 features &= ~feature;
9163 }
9164 }
9165
9166 return features;
9167}
9168
9169static void netdev_sync_lower_features(struct net_device *upper,
9170 struct net_device *lower, netdev_features_t features)
9171{
9172 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9173 netdev_features_t feature;
9174 int feature_bit;
9175
9176 for_each_netdev_feature(upper_disables, feature_bit) {
9177 feature = __NETIF_F_BIT(feature_bit);
9178 if (!(features & feature) && (lower->features & feature)) {
9179 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9180 &feature, lower->name);
9181 lower->wanted_features &= ~feature;
9182 __netdev_update_features(lower);
9183
9184 if (unlikely(lower->features & feature))
9185 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9186 &feature, lower->name);
9187 else
9188 netdev_features_change(lower);
9189 }
9190 }
9191}
9192
9193static netdev_features_t netdev_fix_features(struct net_device *dev,
9194 netdev_features_t features)
9195{
9196
9197 if ((features & NETIF_F_HW_CSUM) &&
9198 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
9199 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
9200 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9201 }
9202
9203
9204 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
9205 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
9206 features &= ~NETIF_F_ALL_TSO;
9207 }
9208
9209 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9210 !(features & NETIF_F_IP_CSUM)) {
9211 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9212 features &= ~NETIF_F_TSO;
9213 features &= ~NETIF_F_TSO_ECN;
9214 }
9215
9216 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9217 !(features & NETIF_F_IPV6_CSUM)) {
9218 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9219 features &= ~NETIF_F_TSO6;
9220 }
9221
9222
9223 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9224 features &= ~NETIF_F_TSO_MANGLEID;
9225
9226
9227 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9228 features &= ~NETIF_F_TSO_ECN;
9229
9230
9231 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
9232 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
9233 features &= ~NETIF_F_GSO;
9234 }
9235
9236
9237 if ((features & dev->gso_partial_features) &&
9238 !(features & NETIF_F_GSO_PARTIAL)) {
9239 netdev_dbg(dev,
9240 "Dropping partially supported GSO features since no GSO partial.\n");
9241 features &= ~dev->gso_partial_features;
9242 }
9243
9244 if (!(features & NETIF_F_RXCSUM)) {
9245
9246
9247
9248
9249
9250 if (features & NETIF_F_GRO_HW) {
9251 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9252 features &= ~NETIF_F_GRO_HW;
9253 }
9254 }
9255
9256
9257 if (features & NETIF_F_RXFCS) {
9258 if (features & NETIF_F_LRO) {
9259 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9260 features &= ~NETIF_F_LRO;
9261 }
9262
9263 if (features & NETIF_F_GRO_HW) {
9264 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9265 features &= ~NETIF_F_GRO_HW;
9266 }
9267 }
9268
9269 if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) {
9270 netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n");
9271 features &= ~NETIF_F_LRO;
9272 }
9273
9274 if (features & NETIF_F_HW_TLS_TX) {
9275 bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) ==
9276 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
9277 bool hw_csum = features & NETIF_F_HW_CSUM;
9278
9279 if (!ip_csum && !hw_csum) {
9280 netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
9281 features &= ~NETIF_F_HW_TLS_TX;
9282 }
9283 }
9284
9285 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
9286 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
9287 features &= ~NETIF_F_HW_TLS_RX;
9288 }
9289
9290 return features;
9291}
9292
9293int __netdev_update_features(struct net_device *dev)
9294{
9295 struct net_device *upper, *lower;
9296 netdev_features_t features;
9297 struct list_head *iter;
9298 int err = -1;
9299
9300 ASSERT_RTNL();
9301
9302 features = netdev_get_wanted_features(dev);
9303
9304 if (dev->netdev_ops->ndo_fix_features)
9305 features = dev->netdev_ops->ndo_fix_features(dev, features);
9306
9307
9308 features = netdev_fix_features(dev, features);
9309
9310
9311 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9312 features = netdev_sync_upper_features(dev, upper, features);
9313
9314 if (dev->features == features)
9315 goto sync_lower;
9316
9317 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9318 &dev->features, &features);
9319
9320 if (dev->netdev_ops->ndo_set_features)
9321 err = dev->netdev_ops->ndo_set_features(dev, features);
9322 else
9323 err = 0;
9324
9325 if (unlikely(err < 0)) {
9326 netdev_err(dev,
9327 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9328 err, &features, &dev->features);
9329
9330
9331
9332 return -1;
9333 }
9334
9335sync_lower:
9336
9337
9338
9339 netdev_for_each_lower_dev(dev, lower, iter)
9340 netdev_sync_lower_features(dev, lower, features);
9341
9342 if (!err) {
9343 netdev_features_t diff = features ^ dev->features;
9344
9345 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9346
9347
9348
9349
9350
9351
9352
9353 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9354 dev->features = features;
9355 udp_tunnel_get_rx_info(dev);
9356 } else {
9357 udp_tunnel_drop_rx_info(dev);
9358 }
9359 }
9360
9361 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9362 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9363 dev->features = features;
9364 err |= vlan_get_rx_ctag_filter_info(dev);
9365 } else {
9366 vlan_drop_rx_ctag_filter_info(dev);
9367 }
9368 }
9369
9370 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9371 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9372 dev->features = features;
9373 err |= vlan_get_rx_stag_filter_info(dev);
9374 } else {
9375 vlan_drop_rx_stag_filter_info(dev);
9376 }
9377 }
9378
9379 dev->features = features;
9380 }
9381
9382 return err < 0 ? 0 : 1;
9383}
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393void netdev_update_features(struct net_device *dev)
9394{
9395 if (__netdev_update_features(dev))
9396 netdev_features_change(dev);
9397}
9398EXPORT_SYMBOL(netdev_update_features);
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410void netdev_change_features(struct net_device *dev)
9411{
9412 __netdev_update_features(dev);
9413 netdev_features_change(dev);
9414}
9415EXPORT_SYMBOL(netdev_change_features);
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9427 struct net_device *dev)
9428{
9429 if (rootdev->operstate == IF_OPER_DORMANT)
9430 netif_dormant_on(dev);
9431 else
9432 netif_dormant_off(dev);
9433
9434 if (rootdev->operstate == IF_OPER_TESTING)
9435 netif_testing_on(dev);
9436 else
9437 netif_testing_off(dev);
9438
9439 if (netif_carrier_ok(rootdev))
9440 netif_carrier_on(dev);
9441 else
9442 netif_carrier_off(dev);
9443}
9444EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9445
9446static int netif_alloc_rx_queues(struct net_device *dev)
9447{
9448 unsigned int i, count = dev->num_rx_queues;
9449 struct netdev_rx_queue *rx;
9450 size_t sz = count * sizeof(*rx);
9451 int err = 0;
9452
9453 BUG_ON(count < 1);
9454
9455 rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
9456 if (!rx)
9457 return -ENOMEM;
9458
9459 dev->_rx = rx;
9460
9461 for (i = 0; i < count; i++) {
9462 rx[i].dev = dev;
9463
9464
9465 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
9466 if (err < 0)
9467 goto err_rxq_info;
9468 }
9469 return 0;
9470
9471err_rxq_info:
9472
9473 while (i--)
9474 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
9475 kvfree(dev->_rx);
9476 dev->_rx = NULL;
9477 return err;
9478}
9479
9480static void netif_free_rx_queues(struct net_device *dev)
9481{
9482 unsigned int i, count = dev->num_rx_queues;
9483
9484
9485 if (!dev->_rx)
9486 return;
9487
9488 for (i = 0; i < count; i++)
9489 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9490
9491 kvfree(dev->_rx);
9492}
9493
9494static void netdev_init_one_queue(struct net_device *dev,
9495 struct netdev_queue *queue, void *_unused)
9496{
9497
9498 spin_lock_init(&queue->_xmit_lock);
9499 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
9500 queue->xmit_lock_owner = -1;
9501 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
9502 queue->dev = dev;
9503#ifdef CONFIG_BQL
9504 dql_init(&queue->dql, HZ);
9505#endif
9506}
9507
9508static void netif_free_tx_queues(struct net_device *dev)
9509{
9510 kvfree(dev->_tx);
9511}
9512
9513static int netif_alloc_netdev_queues(struct net_device *dev)
9514{
9515 unsigned int count = dev->num_tx_queues;
9516 struct netdev_queue *tx;
9517 size_t sz = count * sizeof(*tx);
9518
9519 if (count < 1 || count > 0xffff)
9520 return -EINVAL;
9521
9522 tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
9523 if (!tx)
9524 return -ENOMEM;
9525
9526 dev->_tx = tx;
9527
9528 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9529 spin_lock_init(&dev->tx_global_lock);
9530
9531 return 0;
9532}
9533
9534void netif_tx_stop_all_queues(struct net_device *dev)
9535{
9536 unsigned int i;
9537
9538 for (i = 0; i < dev->num_tx_queues; i++) {
9539 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
9540
9541 netif_tx_stop_queue(txq);
9542 }
9543}
9544EXPORT_SYMBOL(netif_tx_stop_all_queues);
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563int register_netdevice(struct net_device *dev)
9564{
9565 int ret;
9566 struct net *net = dev_net(dev);
9567
9568 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9569 NETDEV_FEATURE_COUNT);
9570 BUG_ON(dev_boot_phase);
9571 ASSERT_RTNL();
9572
9573 might_sleep();
9574
9575
9576 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
9577 BUG_ON(!net);
9578
9579 ret = ethtool_check_ops(dev->ethtool_ops);
9580 if (ret)
9581 return ret;
9582
9583 spin_lock_init(&dev->addr_list_lock);
9584 netdev_set_addr_lockdep_class(dev);
9585
9586 ret = dev_get_valid_name(net, dev, dev->name);
9587 if (ret < 0)
9588 goto out;
9589
9590 ret = -ENOMEM;
9591 dev->name_node = netdev_name_node_head_alloc(dev);
9592 if (!dev->name_node)
9593 goto out;
9594
9595
9596 if (dev->netdev_ops->ndo_init) {
9597 ret = dev->netdev_ops->ndo_init(dev);
9598 if (ret) {
9599 if (ret > 0)
9600 ret = -EIO;
9601 goto err_free_name;
9602 }
9603 }
9604
9605 if (((dev->hw_features | dev->features) &
9606 NETIF_F_HW_VLAN_CTAG_FILTER) &&
9607 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9608 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9609 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9610 ret = -EINVAL;
9611 goto err_uninit;
9612 }
9613
9614 ret = -EBUSY;
9615 if (!dev->ifindex)
9616 dev->ifindex = dev_new_index(net);
9617 else if (__dev_get_by_index(net, dev->ifindex))
9618 goto err_uninit;
9619
9620
9621
9622
9623 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
9624 dev->features |= NETIF_F_SOFT_FEATURES;
9625
9626 if (dev->udp_tunnel_nic_info) {
9627 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9628 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9629 }
9630
9631 dev->wanted_features = dev->features & dev->hw_features;
9632
9633 if (!(dev->flags & IFF_LOOPBACK))
9634 dev->hw_features |= NETIF_F_NOCACHE_COPY;
9635
9636
9637
9638
9639
9640
9641 if (dev->hw_features & NETIF_F_TSO)
9642 dev->hw_features |= NETIF_F_TSO_MANGLEID;
9643 if (dev->vlan_features & NETIF_F_TSO)
9644 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9645 if (dev->mpls_features & NETIF_F_TSO)
9646 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9647 if (dev->hw_enc_features & NETIF_F_TSO)
9648 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
9649
9650
9651
9652 dev->vlan_features |= NETIF_F_HIGHDMA;
9653
9654
9655
9656 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
9657
9658
9659
9660 dev->mpls_features |= NETIF_F_SG;
9661
9662 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9663 ret = notifier_to_errno(ret);
9664 if (ret)
9665 goto err_uninit;
9666
9667 ret = netdev_register_kobject(dev);
9668 if (ret) {
9669 dev->reg_state = NETREG_UNREGISTERED;
9670 goto err_uninit;
9671 }
9672 dev->reg_state = NETREG_REGISTERED;
9673
9674 __netdev_update_features(dev);
9675
9676
9677
9678
9679
9680
9681 set_bit(__LINK_STATE_PRESENT, &dev->state);
9682
9683 linkwatch_init_dev(dev);
9684
9685 dev_init_scheduler(dev);
9686 dev_hold(dev);
9687 list_netdevice(dev);
9688 add_device_randomness(dev->dev_addr, dev->addr_len);
9689
9690
9691
9692
9693
9694 if (dev->addr_assign_type == NET_ADDR_PERM)
9695 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
9696
9697
9698 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
9699 ret = notifier_to_errno(ret);
9700 if (ret) {
9701
9702 dev->needs_free_netdev = false;
9703 unregister_netdevice_queue(dev, NULL);
9704 goto out;
9705 }
9706
9707
9708
9709
9710 if (!dev->rtnl_link_ops ||
9711 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
9712 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
9713
9714out:
9715 return ret;
9716
9717err_uninit:
9718 if (dev->netdev_ops->ndo_uninit)
9719 dev->netdev_ops->ndo_uninit(dev);
9720 if (dev->priv_destructor)
9721 dev->priv_destructor(dev);
9722err_free_name:
9723 netdev_name_node_free(dev->name_node);
9724 goto out;
9725}
9726EXPORT_SYMBOL(register_netdevice);
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738int init_dummy_netdev(struct net_device *dev)
9739{
9740
9741
9742
9743
9744
9745 memset(dev, 0, sizeof(struct net_device));
9746
9747
9748
9749
9750 dev->reg_state = NETREG_DUMMY;
9751
9752
9753 INIT_LIST_HEAD(&dev->napi_list);
9754
9755
9756 set_bit(__LINK_STATE_PRESENT, &dev->state);
9757 set_bit(__LINK_STATE_START, &dev->state);
9758
9759
9760 dev_net_set(dev, &init_net);
9761
9762
9763
9764
9765
9766
9767 return 0;
9768}
9769EXPORT_SYMBOL_GPL(init_dummy_netdev);
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785int register_netdev(struct net_device *dev)
9786{
9787 int err;
9788
9789 if (rtnl_lock_killable())
9790 return -EINTR;
9791 err = register_netdevice(dev);
9792 rtnl_unlock();
9793 return err;
9794}
9795EXPORT_SYMBOL(register_netdev);
9796
9797int netdev_refcnt_read(const struct net_device *dev)
9798{
9799#ifdef CONFIG_PCPU_DEV_REFCNT
9800 int i, refcnt = 0;
9801
9802 for_each_possible_cpu(i)
9803 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
9804 return refcnt;
9805#else
9806 return refcount_read(&dev->dev_refcnt);
9807#endif
9808}
9809EXPORT_SYMBOL(netdev_refcnt_read);
9810
9811int netdev_unregister_timeout_secs __read_mostly = 10;
9812
9813#define WAIT_REFS_MIN_MSECS 1
9814#define WAIT_REFS_MAX_MSECS 250
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827static void netdev_wait_allrefs(struct net_device *dev)
9828{
9829 unsigned long rebroadcast_time, warning_time;
9830 int wait = 0, refcnt;
9831
9832 linkwatch_forget_dev(dev);
9833
9834 rebroadcast_time = warning_time = jiffies;
9835 refcnt = netdev_refcnt_read(dev);
9836
9837 while (refcnt != 1) {
9838 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
9839 rtnl_lock();
9840
9841
9842 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
9843
9844 __rtnl_unlock();
9845 rcu_barrier();
9846 rtnl_lock();
9847
9848 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
9849 &dev->state)) {
9850
9851
9852
9853
9854
9855
9856 linkwatch_run_queue();
9857 }
9858
9859 __rtnl_unlock();
9860
9861 rebroadcast_time = jiffies;
9862 }
9863
9864 if (!wait) {
9865 rcu_barrier();
9866 wait = WAIT_REFS_MIN_MSECS;
9867 } else {
9868 msleep(wait);
9869 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
9870 }
9871
9872 refcnt = netdev_refcnt_read(dev);
9873
9874 if (refcnt != 1 &&
9875 time_after(jiffies, warning_time +
9876 netdev_unregister_timeout_secs * HZ)) {
9877 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9878 dev->name, refcnt);
9879 ref_tracker_dir_print(&dev->refcnt_tracker, 10);
9880 warning_time = jiffies;
9881 }
9882 }
9883}
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909void netdev_run_todo(void)
9910{
9911 struct list_head list;
9912#ifdef CONFIG_LOCKDEP
9913 struct list_head unlink_list;
9914
9915 list_replace_init(&net_unlink_list, &unlink_list);
9916
9917 while (!list_empty(&unlink_list)) {
9918 struct net_device *dev = list_first_entry(&unlink_list,
9919 struct net_device,
9920 unlink_list);
9921 list_del_init(&dev->unlink_list);
9922 dev->nested_level = dev->lower_level - 1;
9923 }
9924#endif
9925
9926
9927 list_replace_init(&net_todo_list, &list);
9928
9929 __rtnl_unlock();
9930
9931
9932
9933 if (!list_empty(&list))
9934 rcu_barrier();
9935
9936 while (!list_empty(&list)) {
9937 struct net_device *dev
9938 = list_first_entry(&list, struct net_device, todo_list);
9939 list_del(&dev->todo_list);
9940
9941 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
9942 pr_err("network todo '%s' but state %d\n",
9943 dev->name, dev->reg_state);
9944 dump_stack();
9945 continue;
9946 }
9947
9948 dev->reg_state = NETREG_UNREGISTERED;
9949
9950 netdev_wait_allrefs(dev);
9951
9952
9953 BUG_ON(netdev_refcnt_read(dev) != 1);
9954 BUG_ON(!list_empty(&dev->ptype_all));
9955 BUG_ON(!list_empty(&dev->ptype_specific));
9956 WARN_ON(rcu_access_pointer(dev->ip_ptr));
9957 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
9958#if IS_ENABLED(CONFIG_DECNET)
9959 WARN_ON(dev->dn_ptr);
9960#endif
9961 if (dev->priv_destructor)
9962 dev->priv_destructor(dev);
9963 if (dev->needs_free_netdev)
9964 free_netdev(dev);
9965
9966
9967 rtnl_lock();
9968 dev_net(dev)->dev_unreg_count--;
9969 __rtnl_unlock();
9970 wake_up(&netdev_unregistering_wq);
9971
9972
9973 kobject_put(&dev->dev.kobj);
9974 }
9975}
9976
9977
9978
9979
9980
9981
9982void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
9983 const struct net_device_stats *netdev_stats)
9984{
9985#if BITS_PER_LONG == 64
9986 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
9987 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
9988
9989 memset((char *)stats64 + sizeof(*netdev_stats), 0,
9990 sizeof(*stats64) - sizeof(*netdev_stats));
9991#else
9992 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
9993 const unsigned long *src = (const unsigned long *)netdev_stats;
9994 u64 *dst = (u64 *)stats64;
9995
9996 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
9997 for (i = 0; i < n; i++)
9998 dst[i] = src[i];
9999
10000 memset((char *)stats64 + n * sizeof(u64), 0,
10001 sizeof(*stats64) - n * sizeof(u64));
10002#endif
10003}
10004EXPORT_SYMBOL(netdev_stats_to_stats64);
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10017 struct rtnl_link_stats64 *storage)
10018{
10019 const struct net_device_ops *ops = dev->netdev_ops;
10020
10021 if (ops->ndo_get_stats64) {
10022 memset(storage, 0, sizeof(*storage));
10023 ops->ndo_get_stats64(dev, storage);
10024 } else if (ops->ndo_get_stats) {
10025 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
10026 } else {
10027 netdev_stats_to_stats64(storage, &dev->stats);
10028 }
10029 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10030 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10031 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
10032 return storage;
10033}
10034EXPORT_SYMBOL(dev_get_stats);
10035
10036
10037
10038
10039
10040
10041
10042
10043void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10044 const struct pcpu_sw_netstats __percpu *netstats)
10045{
10046 int cpu;
10047
10048 for_each_possible_cpu(cpu) {
10049 const struct pcpu_sw_netstats *stats;
10050 struct pcpu_sw_netstats tmp;
10051 unsigned int start;
10052
10053 stats = per_cpu_ptr(netstats, cpu);
10054 do {
10055 start = u64_stats_fetch_begin_irq(&stats->syncp);
10056 tmp.rx_packets = stats->rx_packets;
10057 tmp.rx_bytes = stats->rx_bytes;
10058 tmp.tx_packets = stats->tx_packets;
10059 tmp.tx_bytes = stats->tx_bytes;
10060 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10061
10062 s->rx_packets += tmp.rx_packets;
10063 s->rx_bytes += tmp.rx_bytes;
10064 s->tx_packets += tmp.tx_packets;
10065 s->tx_bytes += tmp.tx_bytes;
10066 }
10067}
10068EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
10079{
10080 netdev_stats_to_stats64(s, &dev->stats);
10081 dev_fetch_sw_netstats(s, dev->tstats);
10082}
10083EXPORT_SYMBOL_GPL(dev_get_tstats64);
10084
10085struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
10086{
10087 struct netdev_queue *queue = dev_ingress_queue(dev);
10088
10089#ifdef CONFIG_NET_CLS_ACT
10090 if (queue)
10091 return queue;
10092 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10093 if (!queue)
10094 return NULL;
10095 netdev_init_one_queue(dev, queue, NULL);
10096 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
10097 queue->qdisc_sleeping = &noop_qdisc;
10098 rcu_assign_pointer(dev->ingress_queue, queue);
10099#endif
10100 return queue;
10101}
10102
10103static const struct ethtool_ops default_ethtool_ops;
10104
10105void netdev_set_default_ethtool_ops(struct net_device *dev,
10106 const struct ethtool_ops *ops)
10107{
10108 if (dev->ethtool_ops == &default_ethtool_ops)
10109 dev->ethtool_ops = ops;
10110}
10111EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10112
10113void netdev_freemem(struct net_device *dev)
10114{
10115 char *addr = (char *)dev - dev->padded;
10116
10117 kvfree(addr);
10118}
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
10134 unsigned char name_assign_type,
10135 void (*setup)(struct net_device *),
10136 unsigned int txqs, unsigned int rxqs)
10137{
10138 struct net_device *dev;
10139 unsigned int alloc_size;
10140 struct net_device *p;
10141
10142 BUG_ON(strlen(name) >= sizeof(dev->name));
10143
10144 if (txqs < 1) {
10145 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
10146 return NULL;
10147 }
10148
10149 if (rxqs < 1) {
10150 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
10151 return NULL;
10152 }
10153
10154 alloc_size = sizeof(struct net_device);
10155 if (sizeof_priv) {
10156
10157 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
10158 alloc_size += sizeof_priv;
10159 }
10160
10161 alloc_size += NETDEV_ALIGN - 1;
10162
10163 p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
10164 if (!p)
10165 return NULL;
10166
10167 dev = PTR_ALIGN(p, NETDEV_ALIGN);
10168 dev->padded = (char *)dev - (char *)p;
10169
10170 ref_tracker_dir_init(&dev->refcnt_tracker, 128);
10171#ifdef CONFIG_PCPU_DEV_REFCNT
10172 dev->pcpu_refcnt = alloc_percpu(int);
10173 if (!dev->pcpu_refcnt)
10174 goto free_dev;
10175 dev_hold(dev);
10176#else
10177 refcount_set(&dev->dev_refcnt, 1);
10178#endif
10179
10180 if (dev_addr_init(dev))
10181 goto free_pcpu;
10182
10183 dev_mc_init(dev);
10184 dev_uc_init(dev);
10185
10186 dev_net_set(dev, &init_net);
10187
10188 dev->gso_max_size = GSO_MAX_SIZE;
10189 dev->gso_max_segs = GSO_MAX_SEGS;
10190 dev->gro_max_size = GRO_MAX_SIZE;
10191 dev->upper_level = 1;
10192 dev->lower_level = 1;
10193#ifdef CONFIG_LOCKDEP
10194 dev->nested_level = 0;
10195 INIT_LIST_HEAD(&dev->unlink_list);
10196#endif
10197
10198 INIT_LIST_HEAD(&dev->napi_list);
10199 INIT_LIST_HEAD(&dev->unreg_list);
10200 INIT_LIST_HEAD(&dev->close_list);
10201 INIT_LIST_HEAD(&dev->link_watch_list);
10202 INIT_LIST_HEAD(&dev->adj_list.upper);
10203 INIT_LIST_HEAD(&dev->adj_list.lower);
10204 INIT_LIST_HEAD(&dev->ptype_all);
10205 INIT_LIST_HEAD(&dev->ptype_specific);
10206 INIT_LIST_HEAD(&dev->net_notifier_list);
10207#ifdef CONFIG_NET_SCHED
10208 hash_init(dev->qdisc_hash);
10209#endif
10210 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
10211 setup(dev);
10212
10213 if (!dev->tx_queue_len) {
10214 dev->priv_flags |= IFF_NO_QUEUE;
10215 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
10216 }
10217
10218 dev->num_tx_queues = txqs;
10219 dev->real_num_tx_queues = txqs;
10220 if (netif_alloc_netdev_queues(dev))
10221 goto free_all;
10222
10223 dev->num_rx_queues = rxqs;
10224 dev->real_num_rx_queues = rxqs;
10225 if (netif_alloc_rx_queues(dev))
10226 goto free_all;
10227
10228 strcpy(dev->name, name);
10229 dev->name_assign_type = name_assign_type;
10230 dev->group = INIT_NETDEV_GROUP;
10231 if (!dev->ethtool_ops)
10232 dev->ethtool_ops = &default_ethtool_ops;
10233
10234 nf_hook_netdev_init(dev);
10235
10236 return dev;
10237
10238free_all:
10239 free_netdev(dev);
10240 return NULL;
10241
10242free_pcpu:
10243#ifdef CONFIG_PCPU_DEV_REFCNT
10244 free_percpu(dev->pcpu_refcnt);
10245free_dev:
10246#endif
10247 netdev_freemem(dev);
10248 return NULL;
10249}
10250EXPORT_SYMBOL(alloc_netdev_mqs);
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261void free_netdev(struct net_device *dev)
10262{
10263 struct napi_struct *p, *n;
10264
10265 might_sleep();
10266
10267
10268
10269
10270
10271 if (dev->reg_state == NETREG_UNREGISTERING) {
10272 ASSERT_RTNL();
10273 dev->needs_free_netdev = true;
10274 return;
10275 }
10276
10277 netif_free_tx_queues(dev);
10278 netif_free_rx_queues(dev);
10279
10280 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
10281
10282
10283 dev_addr_flush(dev);
10284
10285 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10286 netif_napi_del(p);
10287
10288 ref_tracker_dir_exit(&dev->refcnt_tracker);
10289#ifdef CONFIG_PCPU_DEV_REFCNT
10290 free_percpu(dev->pcpu_refcnt);
10291 dev->pcpu_refcnt = NULL;
10292#endif
10293 free_percpu(dev->xdp_bulkq);
10294 dev->xdp_bulkq = NULL;
10295
10296
10297 if (dev->reg_state == NETREG_UNINITIALIZED) {
10298 netdev_freemem(dev);
10299 return;
10300 }
10301
10302 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10303 dev->reg_state = NETREG_RELEASED;
10304
10305
10306 put_device(&dev->dev);
10307}
10308EXPORT_SYMBOL(free_netdev);
10309
10310
10311
10312
10313
10314
10315
10316void synchronize_net(void)
10317{
10318 might_sleep();
10319 if (rtnl_is_locked())
10320 synchronize_rcu_expedited();
10321 else
10322 synchronize_rcu();
10323}
10324EXPORT_SYMBOL(synchronize_net);
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
10340{
10341 ASSERT_RTNL();
10342
10343 if (head) {
10344 list_move_tail(&dev->unreg_list, head);
10345 } else {
10346 LIST_HEAD(single);
10347
10348 list_add(&dev->unreg_list, &single);
10349 unregister_netdevice_many(&single);
10350 }
10351}
10352EXPORT_SYMBOL(unregister_netdevice_queue);
10353
10354
10355
10356
10357
10358
10359
10360
10361void unregister_netdevice_many(struct list_head *head)
10362{
10363 struct net_device *dev, *tmp;
10364 LIST_HEAD(close_head);
10365
10366 BUG_ON(dev_boot_phase);
10367 ASSERT_RTNL();
10368
10369 if (list_empty(head))
10370 return;
10371
10372 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
10373
10374
10375
10376
10377 if (dev->reg_state == NETREG_UNINITIALIZED) {
10378 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
10379 dev->name, dev);
10380
10381 WARN_ON(1);
10382 list_del(&dev->unreg_list);
10383 continue;
10384 }
10385 dev->dismantle = true;
10386 BUG_ON(dev->reg_state != NETREG_REGISTERED);
10387 }
10388
10389
10390 list_for_each_entry(dev, head, unreg_list)
10391 list_add_tail(&dev->close_list, &close_head);
10392 dev_close_many(&close_head, true);
10393
10394 list_for_each_entry(dev, head, unreg_list) {
10395
10396 unlist_netdevice(dev);
10397
10398 dev->reg_state = NETREG_UNREGISTERING;
10399 }
10400 flush_all_backlogs();
10401
10402 synchronize_net();
10403
10404 list_for_each_entry(dev, head, unreg_list) {
10405 struct sk_buff *skb = NULL;
10406
10407
10408 dev_shutdown(dev);
10409
10410 dev_xdp_uninstall(dev);
10411
10412
10413
10414
10415 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
10416
10417 if (!dev->rtnl_link_ops ||
10418 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
10419 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
10420 GFP_KERNEL, NULL, 0);
10421
10422
10423
10424
10425 dev_uc_flush(dev);
10426 dev_mc_flush(dev);
10427
10428 netdev_name_node_alt_flush(dev);
10429 netdev_name_node_free(dev->name_node);
10430
10431 if (dev->netdev_ops->ndo_uninit)
10432 dev->netdev_ops->ndo_uninit(dev);
10433
10434 if (skb)
10435 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
10436
10437
10438 WARN_ON(netdev_has_any_upper_dev(dev));
10439 WARN_ON(netdev_has_any_lower_dev(dev));
10440
10441
10442 netdev_unregister_kobject(dev);
10443#ifdef CONFIG_XPS
10444
10445 netif_reset_xps_queues_gt(dev, 0);
10446#endif
10447 }
10448
10449 synchronize_net();
10450
10451 list_for_each_entry(dev, head, unreg_list) {
10452 dev_put(dev);
10453 net_set_todo(dev);
10454 }
10455
10456 list_del(head);
10457}
10458EXPORT_SYMBOL(unregister_netdevice_many);
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471void unregister_netdev(struct net_device *dev)
10472{
10473 rtnl_lock();
10474 unregister_netdevice(dev);
10475 rtnl_unlock();
10476}
10477EXPORT_SYMBOL(unregister_netdev);
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495int __dev_change_net_namespace(struct net_device *dev, struct net *net,
10496 const char *pat, int new_ifindex)
10497{
10498 struct net *net_old = dev_net(dev);
10499 int err, new_nsid;
10500
10501 ASSERT_RTNL();
10502
10503
10504 err = -EINVAL;
10505 if (dev->features & NETIF_F_NETNS_LOCAL)
10506 goto out;
10507
10508
10509 if (dev->reg_state != NETREG_REGISTERED)
10510 goto out;
10511
10512
10513 err = 0;
10514 if (net_eq(net_old, net))
10515 goto out;
10516
10517
10518
10519
10520 err = -EEXIST;
10521 if (netdev_name_in_use(net, dev->name)) {
10522
10523 if (!pat)
10524 goto out;
10525 err = dev_get_valid_name(net, dev, pat);
10526 if (err < 0)
10527 goto out;
10528 }
10529
10530
10531 err = -EBUSY;
10532 if (new_ifindex && __dev_get_by_index(net, new_ifindex))
10533 goto out;
10534
10535
10536
10537
10538
10539
10540 dev_close(dev);
10541
10542
10543 unlist_netdevice(dev);
10544
10545 synchronize_net();
10546
10547
10548 dev_shutdown(dev);
10549
10550
10551
10552
10553
10554
10555
10556
10557 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
10558 rcu_barrier();
10559
10560 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
10561
10562 if (!new_ifindex) {
10563 if (__dev_get_by_index(net, dev->ifindex))
10564 new_ifindex = dev_new_index(net);
10565 else
10566 new_ifindex = dev->ifindex;
10567 }
10568
10569 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10570 new_ifindex);
10571
10572
10573
10574
10575 dev_uc_flush(dev);
10576 dev_mc_flush(dev);
10577
10578
10579 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
10580 netdev_adjacent_del_links(dev);
10581
10582
10583 move_netdevice_notifiers_dev_net(dev, net);
10584
10585
10586 dev_net_set(dev, net);
10587 dev->ifindex = new_ifindex;
10588
10589
10590 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
10591 netdev_adjacent_add_links(dev);
10592
10593
10594 err = device_rename(&dev->dev, dev->name);
10595 WARN_ON(err);
10596
10597
10598
10599
10600 err = netdev_change_owner(dev, net_old, net);
10601 WARN_ON(err);
10602
10603
10604 list_netdevice(dev);
10605
10606
10607 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10608
10609
10610
10611
10612
10613 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
10614
10615 synchronize_net();
10616 err = 0;
10617out:
10618 return err;
10619}
10620EXPORT_SYMBOL_GPL(__dev_change_net_namespace);
10621
10622static int dev_cpu_dead(unsigned int oldcpu)
10623{
10624 struct sk_buff **list_skb;
10625 struct sk_buff *skb;
10626 unsigned int cpu;
10627 struct softnet_data *sd, *oldsd, *remsd = NULL;
10628
10629 local_irq_disable();
10630 cpu = smp_processor_id();
10631 sd = &per_cpu(softnet_data, cpu);
10632 oldsd = &per_cpu(softnet_data, oldcpu);
10633
10634
10635 list_skb = &sd->completion_queue;
10636 while (*list_skb)
10637 list_skb = &(*list_skb)->next;
10638
10639 *list_skb = oldsd->completion_queue;
10640 oldsd->completion_queue = NULL;
10641
10642
10643 if (oldsd->output_queue) {
10644 *sd->output_queue_tailp = oldsd->output_queue;
10645 sd->output_queue_tailp = oldsd->output_queue_tailp;
10646 oldsd->output_queue = NULL;
10647 oldsd->output_queue_tailp = &oldsd->output_queue;
10648 }
10649
10650
10651
10652
10653 while (!list_empty(&oldsd->poll_list)) {
10654 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10655 struct napi_struct,
10656 poll_list);
10657
10658 list_del_init(&napi->poll_list);
10659 if (napi->poll == process_backlog)
10660 napi->state = 0;
10661 else
10662 ____napi_schedule(sd, napi);
10663 }
10664
10665 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10666 local_irq_enable();
10667
10668#ifdef CONFIG_RPS
10669 remsd = oldsd->rps_ipi_list;
10670 oldsd->rps_ipi_list = NULL;
10671#endif
10672
10673 net_rps_send_ipi(remsd);
10674
10675
10676 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
10677 netif_rx_ni(skb);
10678 input_queue_head_incr(oldsd);
10679 }
10680 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
10681 netif_rx_ni(skb);
10682 input_queue_head_incr(oldsd);
10683 }
10684
10685 return 0;
10686}
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698netdev_features_t netdev_increment_features(netdev_features_t all,
10699 netdev_features_t one, netdev_features_t mask)
10700{
10701 if (mask & NETIF_F_HW_CSUM)
10702 mask |= NETIF_F_CSUM_MASK;
10703 mask |= NETIF_F_VLAN_CHALLENGED;
10704
10705 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
10706 all &= one | ~NETIF_F_ALL_FOR_ALL;
10707
10708
10709 if (all & NETIF_F_HW_CSUM)
10710 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
10711
10712 return all;
10713}
10714EXPORT_SYMBOL(netdev_increment_features);
10715
10716static struct hlist_head * __net_init netdev_create_hash(void)
10717{
10718 int i;
10719 struct hlist_head *hash;
10720
10721 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
10722 if (hash != NULL)
10723 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10724 INIT_HLIST_HEAD(&hash[i]);
10725
10726 return hash;
10727}
10728
10729
10730static int __net_init netdev_init(struct net *net)
10731{
10732 BUILD_BUG_ON(GRO_HASH_BUCKETS >
10733 8 * sizeof_field(struct napi_struct, gro_bitmask));
10734
10735 if (net != &init_net)
10736 INIT_LIST_HEAD(&net->dev_base_head);
10737
10738 net->dev_name_head = netdev_create_hash();
10739 if (net->dev_name_head == NULL)
10740 goto err_name;
10741
10742 net->dev_index_head = netdev_create_hash();
10743 if (net->dev_index_head == NULL)
10744 goto err_idx;
10745
10746 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10747
10748 return 0;
10749
10750err_idx:
10751 kfree(net->dev_name_head);
10752err_name:
10753 return -ENOMEM;
10754}
10755
10756
10757
10758
10759
10760
10761
10762const char *netdev_drivername(const struct net_device *dev)
10763{
10764 const struct device_driver *driver;
10765 const struct device *parent;
10766 const char *empty = "";
10767
10768 parent = dev->dev.parent;
10769 if (!parent)
10770 return empty;
10771
10772 driver = parent->driver;
10773 if (driver && driver->name)
10774 return driver->name;
10775 return empty;
10776}
10777
10778static void __netdev_printk(const char *level, const struct net_device *dev,
10779 struct va_format *vaf)
10780{
10781 if (dev && dev->dev.parent) {
10782 dev_printk_emit(level[1] - '0',
10783 dev->dev.parent,
10784 "%s %s %s%s: %pV",
10785 dev_driver_string(dev->dev.parent),
10786 dev_name(dev->dev.parent),
10787 netdev_name(dev), netdev_reg_state(dev),
10788 vaf);
10789 } else if (dev) {
10790 printk("%s%s%s: %pV",
10791 level, netdev_name(dev), netdev_reg_state(dev), vaf);
10792 } else {
10793 printk("%s(NULL net_device): %pV", level, vaf);
10794 }
10795}
10796
10797void netdev_printk(const char *level, const struct net_device *dev,
10798 const char *format, ...)
10799{
10800 struct va_format vaf;
10801 va_list args;
10802
10803 va_start(args, format);
10804
10805 vaf.fmt = format;
10806 vaf.va = &args;
10807
10808 __netdev_printk(level, dev, &vaf);
10809
10810 va_end(args);
10811}
10812EXPORT_SYMBOL(netdev_printk);
10813
10814#define define_netdev_printk_level(func, level) \
10815void func(const struct net_device *dev, const char *fmt, ...) \
10816{ \
10817 struct va_format vaf; \
10818 va_list args; \
10819 \
10820 va_start(args, fmt); \
10821 \
10822 vaf.fmt = fmt; \
10823 vaf.va = &args; \
10824 \
10825 __netdev_printk(level, dev, &vaf); \
10826 \
10827 va_end(args); \
10828} \
10829EXPORT_SYMBOL(func);
10830
10831define_netdev_printk_level(netdev_emerg, KERN_EMERG);
10832define_netdev_printk_level(netdev_alert, KERN_ALERT);
10833define_netdev_printk_level(netdev_crit, KERN_CRIT);
10834define_netdev_printk_level(netdev_err, KERN_ERR);
10835define_netdev_printk_level(netdev_warn, KERN_WARNING);
10836define_netdev_printk_level(netdev_notice, KERN_NOTICE);
10837define_netdev_printk_level(netdev_info, KERN_INFO);
10838
10839static void __net_exit netdev_exit(struct net *net)
10840{
10841 kfree(net->dev_name_head);
10842 kfree(net->dev_index_head);
10843 if (net != &init_net)
10844 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
10845}
10846
10847static struct pernet_operations __net_initdata netdev_net_ops = {
10848 .init = netdev_init,
10849 .exit = netdev_exit,
10850};
10851
10852static void __net_exit default_device_exit(struct net *net)
10853{
10854 struct net_device *dev, *aux;
10855
10856
10857
10858
10859 rtnl_lock();
10860 for_each_netdev_safe(net, dev, aux) {
10861 int err;
10862 char fb_name[IFNAMSIZ];
10863
10864
10865 if (dev->features & NETIF_F_NETNS_LOCAL)
10866 continue;
10867
10868
10869 if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
10870 continue;
10871
10872
10873 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
10874 if (netdev_name_in_use(&init_net, fb_name))
10875 snprintf(fb_name, IFNAMSIZ, "dev%%d");
10876 err = dev_change_net_namespace(dev, &init_net, fb_name);
10877 if (err) {
10878 pr_emerg("%s: failed to move %s to init_net: %d\n",
10879 __func__, dev->name, err);
10880 BUG();
10881 }
10882 }
10883 rtnl_unlock();
10884}
10885
10886static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
10887{
10888
10889
10890
10891 struct net *net;
10892 bool unregistering;
10893 DEFINE_WAIT_FUNC(wait, woken_wake_function);
10894
10895 add_wait_queue(&netdev_unregistering_wq, &wait);
10896 for (;;) {
10897 unregistering = false;
10898 rtnl_lock();
10899 list_for_each_entry(net, net_list, exit_list) {
10900 if (net->dev_unreg_count > 0) {
10901 unregistering = true;
10902 break;
10903 }
10904 }
10905 if (!unregistering)
10906 break;
10907 __rtnl_unlock();
10908
10909 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
10910 }
10911 remove_wait_queue(&netdev_unregistering_wq, &wait);
10912}
10913
10914static void __net_exit default_device_exit_batch(struct list_head *net_list)
10915{
10916
10917
10918
10919
10920
10921 struct net_device *dev;
10922 struct net *net;
10923 LIST_HEAD(dev_kill_list);
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936 rtnl_lock_unregistering(net_list);
10937 list_for_each_entry(net, net_list, exit_list) {
10938 for_each_netdev_reverse(net, dev) {
10939 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
10940 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
10941 else
10942 unregister_netdevice_queue(dev, &dev_kill_list);
10943 }
10944 }
10945 unregister_netdevice_many(&dev_kill_list);
10946 rtnl_unlock();
10947}
10948
10949static struct pernet_operations __net_initdata default_device_ops = {
10950 .exit = default_device_exit,
10951 .exit_batch = default_device_exit_batch,
10952};
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965static int __init net_dev_init(void)
10966{
10967 int i, rc = -ENOMEM;
10968
10969 BUG_ON(!dev_boot_phase);
10970
10971 if (dev_proc_init())
10972 goto out;
10973
10974 if (netdev_kobject_init())
10975 goto out;
10976
10977 INIT_LIST_HEAD(&ptype_all);
10978 for (i = 0; i < PTYPE_HASH_SIZE; i++)
10979 INIT_LIST_HEAD(&ptype_base[i]);
10980
10981 if (register_pernet_subsys(&netdev_net_ops))
10982 goto out;
10983
10984
10985
10986
10987
10988 for_each_possible_cpu(i) {
10989 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
10990 struct softnet_data *sd = &per_cpu(softnet_data, i);
10991
10992 INIT_WORK(flush, flush_backlog);
10993
10994 skb_queue_head_init(&sd->input_pkt_queue);
10995 skb_queue_head_init(&sd->process_queue);
10996#ifdef CONFIG_XFRM_OFFLOAD
10997 skb_queue_head_init(&sd->xfrm_backlog);
10998#endif
10999 INIT_LIST_HEAD(&sd->poll_list);
11000 sd->output_queue_tailp = &sd->output_queue;
11001#ifdef CONFIG_RPS
11002 INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
11003 sd->cpu = i;
11004#endif
11005
11006 init_gro_hash(&sd->backlog);
11007 sd->backlog.poll = process_backlog;
11008 sd->backlog.weight = weight_p;
11009 }
11010
11011 dev_boot_phase = 0;
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022 if (register_pernet_device(&loopback_net_ops))
11023 goto out;
11024
11025 if (register_pernet_device(&default_device_ops))
11026 goto out;
11027
11028 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11029 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
11030
11031 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11032 NULL, dev_cpu_dead);
11033 WARN_ON(rc < 0);
11034 rc = 0;
11035out:
11036 return rc;
11037}
11038
11039subsys_initcall(net_dev_init);
11040