xref: /linux/net/core/dev.h (revision 7110f24f9e33979fd704f7a4a595a9d3e9bdacb7)
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 #if IS_ENABLED(CONFIG_NET_SHAPER)
41 void net_shaper_flush_netdev(struct net_device *dev);
42 void net_shaper_set_real_num_tx_queues(struct net_device *dev,
43 				       unsigned int txq);
44 #else
45 static inline void net_shaper_flush_netdev(struct net_device *dev) {}
46 static inline void net_shaper_set_real_num_tx_queues(struct net_device *dev,
47 						     unsigned int txq) {}
48 #endif
49 
50 /* sysctls not referred to from outside net/core/ */
51 extern int		netdev_unregister_timeout_secs;
52 extern int		weight_p;
53 extern int		dev_weight_rx_bias;
54 extern int		dev_weight_tx_bias;
55 
56 extern struct rw_semaphore dev_addr_sem;
57 
58 /* rtnl helpers */
59 extern struct list_head net_todo_list;
60 void netdev_run_todo(void);
61 
62 /* netdev management, shared between various uAPI entry points */
63 struct netdev_name_node {
64 	struct hlist_node hlist;
65 	struct list_head list;
66 	struct net_device *dev;
67 	const char *name;
68 	struct rcu_head rcu;
69 };
70 
71 int netdev_get_name(struct net *net, char *name, int ifindex);
72 int dev_change_name(struct net_device *dev, const char *newname);
73 
74 #define netdev_for_each_altname(dev, namenode)				\
75 	list_for_each_entry((namenode), &(dev)->name_node->list, list)
76 #define netdev_for_each_altname_safe(dev, namenode, next)		\
77 	list_for_each_entry_safe((namenode), (next), &(dev)->name_node->list, \
78 				 list)
79 
80 int netdev_name_node_alt_create(struct net_device *dev, const char *name);
81 int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);
82 
83 int dev_validate_mtu(struct net_device *dev, int mtu,
84 		     struct netlink_ext_ack *extack);
85 int dev_set_mtu_ext(struct net_device *dev, int mtu,
86 		    struct netlink_ext_ack *extack);
87 
88 int dev_get_phys_port_id(struct net_device *dev,
89 			 struct netdev_phys_item_id *ppid);
90 int dev_get_phys_port_name(struct net_device *dev,
91 			   char *name, size_t len);
92 
93 int dev_change_proto_down(struct net_device *dev, bool proto_down);
94 void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
95 				  u32 value);
96 
97 typedef int (*bpf_op_t)(struct net_device *dev, struct netdev_bpf *bpf);
98 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
99 		      int fd, int expected_fd, u32 flags);
100 
101 int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
102 void dev_set_group(struct net_device *dev, int new_group);
103 int dev_change_carrier(struct net_device *dev, bool new_carrier);
104 
105 void __dev_set_rx_mode(struct net_device *dev);
106 
107 void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
108 			unsigned int gchanges, u32 portid,
109 			const struct nlmsghdr *nlh);
110 
111 void unregister_netdevice_many_notify(struct list_head *head,
112 				      u32 portid, const struct nlmsghdr *nlh);
113 
114 static inline void netif_set_gso_max_size(struct net_device *dev,
115 					  unsigned int size)
116 {
117 	/* dev->gso_max_size is read locklessly from sk_setup_caps() */
118 	WRITE_ONCE(dev->gso_max_size, size);
119 	if (size <= GSO_LEGACY_MAX_SIZE)
120 		WRITE_ONCE(dev->gso_ipv4_max_size, size);
121 }
122 
123 static inline void netif_set_gso_max_segs(struct net_device *dev,
124 					  unsigned int segs)
125 {
126 	/* dev->gso_max_segs is read locklessly from sk_setup_caps() */
127 	WRITE_ONCE(dev->gso_max_segs, segs);
128 }
129 
130 static inline void netif_set_gro_max_size(struct net_device *dev,
131 					  unsigned int size)
132 {
133 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
134 	WRITE_ONCE(dev->gro_max_size, size);
135 	if (size <= GRO_LEGACY_MAX_SIZE)
136 		WRITE_ONCE(dev->gro_ipv4_max_size, size);
137 }
138 
139 static inline void netif_set_gso_ipv4_max_size(struct net_device *dev,
140 					       unsigned int size)
141 {
142 	/* dev->gso_ipv4_max_size is read locklessly from sk_setup_caps() */
143 	WRITE_ONCE(dev->gso_ipv4_max_size, size);
144 }
145 
146 static inline void netif_set_gro_ipv4_max_size(struct net_device *dev,
147 					       unsigned int size)
148 {
149 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
150 	WRITE_ONCE(dev->gro_ipv4_max_size, size);
151 }
152 
153 /**
154  * napi_get_defer_hard_irqs - get the NAPI's defer_hard_irqs
155  * @n: napi struct to get the defer_hard_irqs field from
156  *
157  * Return: the per-NAPI value of the defar_hard_irqs field.
158  */
159 static inline u32 napi_get_defer_hard_irqs(const struct napi_struct *n)
160 {
161 	return READ_ONCE(n->defer_hard_irqs);
162 }
163 
164 /**
165  * napi_set_defer_hard_irqs - set the defer_hard_irqs for a napi
166  * @n: napi_struct to set the defer_hard_irqs field
167  * @defer: the value the field should be set to
168  */
169 static inline void napi_set_defer_hard_irqs(struct napi_struct *n, u32 defer)
170 {
171 	WRITE_ONCE(n->defer_hard_irqs, defer);
172 }
173 
174 /**
175  * netdev_set_defer_hard_irqs - set defer_hard_irqs for all NAPIs of a netdev
176  * @netdev: the net_device for which all NAPIs will have defer_hard_irqs set
177  * @defer: the defer_hard_irqs value to set
178  */
179 static inline void netdev_set_defer_hard_irqs(struct net_device *netdev,
180 					      u32 defer)
181 {
182 	unsigned int count = max(netdev->num_rx_queues,
183 				 netdev->num_tx_queues);
184 	struct napi_struct *napi;
185 	int i;
186 
187 	WRITE_ONCE(netdev->napi_defer_hard_irqs, defer);
188 	list_for_each_entry(napi, &netdev->napi_list, dev_list)
189 		napi_set_defer_hard_irqs(napi, defer);
190 
191 	for (i = 0; i < count; i++)
192 		netdev->napi_config[i].defer_hard_irqs = defer;
193 }
194 
195 /**
196  * napi_get_gro_flush_timeout - get the gro_flush_timeout
197  * @n: napi struct to get the gro_flush_timeout from
198  *
199  * Return: the per-NAPI value of the gro_flush_timeout field.
200  */
201 static inline unsigned long
202 napi_get_gro_flush_timeout(const struct napi_struct *n)
203 {
204 	return READ_ONCE(n->gro_flush_timeout);
205 }
206 
207 /**
208  * napi_set_gro_flush_timeout - set the gro_flush_timeout for a napi
209  * @n: napi struct to set the gro_flush_timeout
210  * @timeout: timeout value to set
211  *
212  * napi_set_gro_flush_timeout sets the per-NAPI gro_flush_timeout
213  */
214 static inline void napi_set_gro_flush_timeout(struct napi_struct *n,
215 					      unsigned long timeout)
216 {
217 	WRITE_ONCE(n->gro_flush_timeout, timeout);
218 }
219 
220 /**
221  * netdev_set_gro_flush_timeout - set gro_flush_timeout of a netdev's NAPIs
222  * @netdev: the net_device for which all NAPIs will have gro_flush_timeout set
223  * @timeout: the timeout value to set
224  */
225 static inline void netdev_set_gro_flush_timeout(struct net_device *netdev,
226 						unsigned long timeout)
227 {
228 	unsigned int count = max(netdev->num_rx_queues,
229 				 netdev->num_tx_queues);
230 	struct napi_struct *napi;
231 	int i;
232 
233 	WRITE_ONCE(netdev->gro_flush_timeout, timeout);
234 	list_for_each_entry(napi, &netdev->napi_list, dev_list)
235 		napi_set_gro_flush_timeout(napi, timeout);
236 
237 	for (i = 0; i < count; i++)
238 		netdev->napi_config[i].gro_flush_timeout = timeout;
239 }
240 
241 /**
242  * napi_get_irq_suspend_timeout - get the irq_suspend_timeout
243  * @n: napi struct to get the irq_suspend_timeout from
244  *
245  * Return: the per-NAPI value of the irq_suspend_timeout field.
246  */
247 static inline unsigned long
248 napi_get_irq_suspend_timeout(const struct napi_struct *n)
249 {
250 	return READ_ONCE(n->irq_suspend_timeout);
251 }
252 
253 /**
254  * napi_set_irq_suspend_timeout - set the irq_suspend_timeout for a napi
255  * @n: napi struct to set the irq_suspend_timeout
256  * @timeout: timeout value to set
257  *
258  * napi_set_irq_suspend_timeout sets the per-NAPI irq_suspend_timeout
259  */
260 static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,
261 						unsigned long timeout)
262 {
263 	WRITE_ONCE(n->irq_suspend_timeout, timeout);
264 }
265 
266 int rps_cpumask_housekeeping(struct cpumask *mask);
267 
268 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
269 void xdp_do_check_flushed(struct napi_struct *napi);
270 #else
271 static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
272 #endif
273 
274 void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu);
275 
276 #define XMIT_RECURSION_LIMIT	8
277 
278 #ifndef CONFIG_PREEMPT_RT
279 static inline bool dev_xmit_recursion(void)
280 {
281 	return unlikely(__this_cpu_read(softnet_data.xmit.recursion) >
282 			XMIT_RECURSION_LIMIT);
283 }
284 
285 static inline void dev_xmit_recursion_inc(void)
286 {
287 	__this_cpu_inc(softnet_data.xmit.recursion);
288 }
289 
290 static inline void dev_xmit_recursion_dec(void)
291 {
292 	__this_cpu_dec(softnet_data.xmit.recursion);
293 }
294 #else
295 static inline bool dev_xmit_recursion(void)
296 {
297 	return unlikely(current->net_xmit.recursion > XMIT_RECURSION_LIMIT);
298 }
299 
300 static inline void dev_xmit_recursion_inc(void)
301 {
302 	current->net_xmit.recursion++;
303 }
304 
305 static inline void dev_xmit_recursion_dec(void)
306 {
307 	current->net_xmit.recursion--;
308 }
309 #endif
310 
311 int dev_set_hwtstamp_phylib(struct net_device *dev,
312 			    struct kernel_hwtstamp_config *cfg,
313 			    struct netlink_ext_ack *extack);
314 
315 #endif
316