1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _NET_CORE_DEV_H
3 #define _NET_CORE_DEV_H
4 
5 #include <linux/types.h>
6 #include <linux/rwsem.h>
7 #include <linux/netdevice.h>
8 
9 struct net;
10 struct netlink_ext_ack;
11 struct cpumask;
12 
13 /* Random bits of netdevice that don't need to be exposed */
14 #define FLOW_LIMIT_HISTORY	(1 << 7)  /* must be ^2 and !overflow buckets */
15 struct sd_flow_limit {
16 	u64			count;
17 	unsigned int		num_buckets;
18 	unsigned int		history_head;
19 	u16			history[FLOW_LIMIT_HISTORY];
20 	u8			buckets[];
21 };
22 
23 extern int netdev_flow_limit_table_len;
24 
25 struct napi_struct *netdev_napi_by_id(struct net *net, unsigned int napi_id);
26 
27 #ifdef CONFIG_PROC_FS
28 int __init dev_proc_init(void);
29 #else
30 #define dev_proc_init() 0
31 #endif
32 
33 void linkwatch_init_dev(struct net_device *dev);
34 void linkwatch_run_queue(void);
35 
36 void dev_addr_flush(struct net_device *dev);
37 int dev_addr_init(struct net_device *dev);
38 void dev_addr_check(struct net_device *dev);
39 
40 /* sysctls not referred to from outside net/core/ */
41 extern int		netdev_unregister_timeout_secs;
42 extern int		weight_p;
43 extern int		dev_weight_rx_bias;
44 extern int		dev_weight_tx_bias;
45 
46 extern struct rw_semaphore dev_addr_sem;
47 
48 /* rtnl helpers */
49 extern struct list_head net_todo_list;
50 void netdev_run_todo(void);
51 
52 /* netdev management, shared between various uAPI entry points */
53 struct netdev_name_node {
54 	struct hlist_node hlist;
55 	struct list_head list;
56 	struct net_device *dev;
57 	const char *name;
58 	struct rcu_head rcu;
59 };
60 
61 int netdev_get_name(struct net *net, char *name, int ifindex);
62 int dev_change_name(struct net_device *dev, const char *newname);
63 
64 #define netdev_for_each_altname(dev, namenode)				\
65 	list_for_each_entry((namenode), &(dev)->name_node->list, list)
66 #define netdev_for_each_altname_safe(dev, namenode, next)		\
67 	list_for_each_entry_safe((namenode), (next), &(dev)->name_node->list, \
68 				 list)
69 
70 int netdev_name_node_alt_create(struct net_device *dev, const char *name);
71 int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);
72 
73 int dev_validate_mtu(struct net_device *dev, int mtu,
74 		     struct netlink_ext_ack *extack);
75 int dev_set_mtu_ext(struct net_device *dev, int mtu,
76 		    struct netlink_ext_ack *extack);
77 
78 int dev_get_phys_port_id(struct net_device *dev,
79 			 struct netdev_phys_item_id *ppid);
80 int dev_get_phys_port_name(struct net_device *dev,
81 			   char *name, size_t len);
82 
83 int dev_change_proto_down(struct net_device *dev, bool proto_down);
84 void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
85 				  u32 value);
86 
87 typedef int (*bpf_op_t)(struct net_device *dev, struct netdev_bpf *bpf);
88 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
89 		      int fd, int expected_fd, u32 flags);
90 
91 int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
92 void dev_set_group(struct net_device *dev, int new_group);
93 int dev_change_carrier(struct net_device *dev, bool new_carrier);
94 
95 void __dev_set_rx_mode(struct net_device *dev);
96 
97 void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
98 			unsigned int gchanges, u32 portid,
99 			const struct nlmsghdr *nlh);
100 
101 void unregister_netdevice_many_notify(struct list_head *head,
102 				      u32 portid, const struct nlmsghdr *nlh);
103 
netif_set_gso_max_size(struct net_device * dev,unsigned int size)104 static inline void netif_set_gso_max_size(struct net_device *dev,
105 					  unsigned int size)
106 {
107 	/* dev->gso_max_size is read locklessly from sk_setup_caps() */
108 	WRITE_ONCE(dev->gso_max_size, size);
109 	if (size <= GSO_LEGACY_MAX_SIZE)
110 		WRITE_ONCE(dev->gso_ipv4_max_size, size);
111 }
112 
netif_set_gso_max_segs(struct net_device * dev,unsigned int segs)113 static inline void netif_set_gso_max_segs(struct net_device *dev,
114 					  unsigned int segs)
115 {
116 	/* dev->gso_max_segs is read locklessly from sk_setup_caps() */
117 	WRITE_ONCE(dev->gso_max_segs, segs);
118 }
119 
netif_set_gro_max_size(struct net_device * dev,unsigned int size)120 static inline void netif_set_gro_max_size(struct net_device *dev,
121 					  unsigned int size)
122 {
123 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
124 	WRITE_ONCE(dev->gro_max_size, size);
125 	if (size <= GRO_LEGACY_MAX_SIZE)
126 		WRITE_ONCE(dev->gro_ipv4_max_size, size);
127 }
128 
netif_set_gso_ipv4_max_size(struct net_device * dev,unsigned int size)129 static inline void netif_set_gso_ipv4_max_size(struct net_device *dev,
130 					       unsigned int size)
131 {
132 	/* dev->gso_ipv4_max_size is read locklessly from sk_setup_caps() */
133 	WRITE_ONCE(dev->gso_ipv4_max_size, size);
134 }
135 
netif_set_gro_ipv4_max_size(struct net_device * dev,unsigned int size)136 static inline void netif_set_gro_ipv4_max_size(struct net_device *dev,
137 					       unsigned int size)
138 {
139 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
140 	WRITE_ONCE(dev->gro_ipv4_max_size, size);
141 }
142 
143 int rps_cpumask_housekeeping(struct cpumask *mask);
144 
145 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
146 void xdp_do_check_flushed(struct napi_struct *napi);
147 #else
xdp_do_check_flushed(struct napi_struct * napi)148 static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
149 #endif
150 
151 void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu);
152 
153 #define XMIT_RECURSION_LIMIT	8
154 
155 #ifndef CONFIG_PREEMPT_RT
dev_xmit_recursion(void)156 static inline bool dev_xmit_recursion(void)
157 {
158 	return unlikely(__this_cpu_read(softnet_data.xmit.recursion) >
159 			XMIT_RECURSION_LIMIT);
160 }
161 
dev_xmit_recursion_inc(void)162 static inline void dev_xmit_recursion_inc(void)
163 {
164 	__this_cpu_inc(softnet_data.xmit.recursion);
165 }
166 
dev_xmit_recursion_dec(void)167 static inline void dev_xmit_recursion_dec(void)
168 {
169 	__this_cpu_dec(softnet_data.xmit.recursion);
170 }
171 #else
dev_xmit_recursion(void)172 static inline bool dev_xmit_recursion(void)
173 {
174 	return unlikely(current->net_xmit.recursion > XMIT_RECURSION_LIMIT);
175 }
176 
dev_xmit_recursion_inc(void)177 static inline void dev_xmit_recursion_inc(void)
178 {
179 	current->net_xmit.recursion++;
180 }
181 
dev_xmit_recursion_dec(void)182 static inline void dev_xmit_recursion_dec(void)
183 {
184 	current->net_xmit.recursion--;
185 }
186 #endif
187 
188 int dev_set_hwtstamp_phylib(struct net_device *dev,
189 			    struct kernel_hwtstamp_config *cfg,
190 			    struct netlink_ext_ack *extack);
191 
192 #endif
193