xref: /linux/net/core/dev.h (revision 65d657d806848add1e1f0632562d7f47d5d5c188)
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/cleanup.h>
6 #include <linux/types.h>
7 #include <linux/rwsem.h>
8 #include <linux/netdevice.h>
9 #include <net/netdev_lock.h>
10 
11 struct net;
12 struct netlink_ext_ack;
13 struct netdev_queue_config;
14 struct cpumask;
15 struct pp_memory_provider_params;
16 
17 /* Random bits of netdevice that don't need to be exposed */
18 #define FLOW_LIMIT_HISTORY	(1 << 7)  /* must be ^2 and !overflow buckets */
19 struct sd_flow_limit {
20 	struct rcu_head		rcu;
21 	unsigned int		count;
22 	u8			log_buckets;
23 	unsigned int		history_head;
24 	u16			history[FLOW_LIMIT_HISTORY];
25 	u8			buckets[];
26 };
27 
28 extern int netdev_flow_limit_table_len;
29 
30 struct napi_struct *
31 netdev_napi_by_id_lock(struct net *net, unsigned int napi_id);
32 struct net_device *dev_get_by_napi_id(unsigned int napi_id);
33 
34 struct net_device *__netdev_put_lock(struct net_device *dev, struct net *net);
35 struct net_device *netdev_put_lock(struct net_device *dev, struct net *net,
36 				   netdevice_tracker *tracker);
37 struct net_device *
38 netdev_xa_find_lock(struct net *net, struct net_device *dev,
39 		    unsigned long *index);
40 
41 DEFINE_FREE(netdev_unlock, struct net_device *, if (_T) netdev_unlock(_T));
42 
43 #define for_each_netdev_lock_scoped(net, var_name, ifindex)		\
44 	for (struct net_device *var_name __free(netdev_unlock) = NULL;	\
45 	     (var_name = netdev_xa_find_lock(net, var_name, &ifindex)); \
46 	     ifindex++)
47 
48 struct net_device *
49 netdev_get_by_index_lock_ops_compat(struct net *net, int ifindex);
50 struct net_device *
51 netdev_xa_find_lock_ops_compat(struct net *net, struct net_device *dev,
52 			       unsigned long *index);
53 
54 DEFINE_FREE(netdev_unlock_ops_compat, struct net_device *,
55 	    if (_T) netdev_unlock_ops_compat(_T));
56 
57 #define for_each_netdev_lock_ops_compat_scoped(net, var_name, ifindex)	\
58 	for (struct net_device *var_name __free(netdev_unlock_ops_compat) = NULL; \
59 	     (var_name = netdev_xa_find_lock_ops_compat(net, var_name,	\
60 							&ifindex));	\
61 	     ifindex++)
62 
63 #ifdef CONFIG_PROC_FS
64 int __init dev_proc_init(void);
65 #else
66 #define dev_proc_init() 0
67 #endif
68 
69 void linkwatch_init_dev(struct net_device *dev);
70 void linkwatch_run_queue(void);
71 
72 void dev_addr_flush(struct net_device *dev);
73 int dev_addr_init(struct net_device *dev);
74 void dev_addr_check(struct net_device *dev);
75 
76 #if IS_ENABLED(CONFIG_NET_SHAPER)
77 void net_shaper_flush_netdev(struct net_device *dev);
78 void net_shaper_set_real_num_tx_queues(struct net_device *dev,
79 				       unsigned int txq);
80 #else
81 static inline void net_shaper_flush_netdev(struct net_device *dev) {}
82 static inline void net_shaper_set_real_num_tx_queues(struct net_device *dev,
83 						     unsigned int txq) {}
84 #endif
85 
86 /* sysctls not referred to from outside net/core/ */
87 extern int		netdev_unregister_timeout_secs;
88 extern int		weight_p;
89 extern int		dev_weight_rx_bias;
90 extern int		dev_weight_tx_bias;
91 
92 extern struct rw_semaphore dev_addr_sem;
93 
94 /* rtnl helpers */
95 extern struct list_head net_todo_list;
96 void netdev_run_todo(void);
97 
98 int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
99 				 struct netdev_queue_config *qcfg,
100 				 struct netlink_ext_ack *extack);
101 
102 bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx);
103 bool netif_rxq_is_leased(struct net_device *dev, unsigned int rxq_idx);
104 
105 void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
106 			      const struct pp_memory_provider_params *p);
107 
108 void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq,
109 			       struct netdev_rx_queue *virt_rxq);
110 
111 /* netdev management, shared between various uAPI entry points */
112 struct netdev_name_node {
113 	struct hlist_node hlist;
114 	struct list_head list;
115 	struct net_device *dev;
116 	const char *name;
117 	struct rcu_head rcu;
118 };
119 
120 int netdev_get_name(struct net *net, char *name, int ifindex);
121 int netif_change_name(struct net_device *dev, const char *newname);
122 int dev_change_name(struct net_device *dev, const char *newname);
123 
124 #define netdev_for_each_altname(dev, namenode)				\
125 	list_for_each_entry((namenode), &(dev)->name_node->list, list)
126 #define netdev_for_each_altname_safe(dev, namenode, next)		\
127 	list_for_each_entry_safe((namenode), (next), &(dev)->name_node->list, \
128 				 list)
129 
130 int netdev_name_node_alt_create(struct net_device *dev, const char *name);
131 int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);
132 
133 int dev_validate_mtu(struct net_device *dev, int mtu,
134 		     struct netlink_ext_ack *extack);
135 int netif_set_mtu_ext(struct net_device *dev, int new_mtu,
136 		      struct netlink_ext_ack *extack);
137 
138 int dev_get_phys_port_id(struct net_device *dev,
139 			 struct netdev_phys_item_id *ppid);
140 int dev_get_phys_port_name(struct net_device *dev,
141 			   char *name, size_t len);
142 
143 int netif_change_proto_down(struct net_device *dev, bool proto_down);
144 int dev_change_proto_down(struct net_device *dev, bool proto_down);
145 void netdev_change_proto_down_reason_locked(struct net_device *dev,
146 					    unsigned long mask, u32 value);
147 
148 typedef int (*bpf_op_t)(struct net_device *dev, struct netdev_bpf *bpf);
149 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
150 		      int fd, int expected_fd, u32 flags);
151 
152 int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
153 int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
154 void netif_set_group(struct net_device *dev, int new_group);
155 void dev_set_group(struct net_device *dev, int new_group);
156 int netif_change_carrier(struct net_device *dev, bool new_carrier);
157 int dev_change_carrier(struct net_device *dev, bool new_carrier);
158 
159 void __dev_set_rx_mode(struct net_device *dev);
160 
161 void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
162 			unsigned int gchanges, u32 portid,
163 			const struct nlmsghdr *nlh);
164 
165 void unregister_netdevice_many_notify(struct list_head *head,
166 				      u32 portid, const struct nlmsghdr *nlh);
167 
168 static inline void netif_set_up(struct net_device *dev, bool value)
169 {
170 	if (value)
171 		dev->flags |= IFF_UP;
172 	else
173 		dev->flags &= ~IFF_UP;
174 
175 	if (!netdev_need_ops_lock(dev))
176 		netdev_lock(dev);
177 	dev->up = value;
178 	if (!netdev_need_ops_lock(dev))
179 		netdev_unlock(dev);
180 }
181 
182 static inline void netif_set_gso_max_size(struct net_device *dev,
183 					  unsigned int size)
184 {
185 	/* dev->gso_max_size is read locklessly from sk_setup_caps() */
186 	WRITE_ONCE(dev->gso_max_size, size);
187 	if (size <= GSO_LEGACY_MAX_SIZE)
188 		WRITE_ONCE(dev->gso_ipv4_max_size, size);
189 }
190 
191 static inline void netif_set_gso_max_segs(struct net_device *dev,
192 					  unsigned int segs)
193 {
194 	/* dev->gso_max_segs is read locklessly from sk_setup_caps() */
195 	WRITE_ONCE(dev->gso_max_segs, segs);
196 }
197 
198 static inline void netif_set_gro_max_size(struct net_device *dev,
199 					  unsigned int size)
200 {
201 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
202 	WRITE_ONCE(dev->gro_max_size, size);
203 	if (size <= GRO_LEGACY_MAX_SIZE)
204 		WRITE_ONCE(dev->gro_ipv4_max_size, size);
205 }
206 
207 static inline void netif_set_gso_ipv4_max_size(struct net_device *dev,
208 					       unsigned int size)
209 {
210 	/* dev->gso_ipv4_max_size is read locklessly from sk_setup_caps() */
211 	WRITE_ONCE(dev->gso_ipv4_max_size, size);
212 }
213 
214 static inline void netif_set_gro_ipv4_max_size(struct net_device *dev,
215 					       unsigned int size)
216 {
217 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
218 	WRITE_ONCE(dev->gro_ipv4_max_size, size);
219 }
220 
221 /**
222  * napi_get_defer_hard_irqs - get the NAPI's defer_hard_irqs
223  * @n: napi struct to get the defer_hard_irqs field from
224  *
225  * Return: the per-NAPI value of the defar_hard_irqs field.
226  */
227 static inline u32 napi_get_defer_hard_irqs(const struct napi_struct *n)
228 {
229 	return READ_ONCE(n->defer_hard_irqs);
230 }
231 
232 /**
233  * napi_set_defer_hard_irqs - set the defer_hard_irqs for a napi
234  * @n: napi_struct to set the defer_hard_irqs field
235  * @defer: the value the field should be set to
236  */
237 static inline void napi_set_defer_hard_irqs(struct napi_struct *n, u32 defer)
238 {
239 	WRITE_ONCE(n->defer_hard_irqs, defer);
240 }
241 
242 /**
243  * netdev_set_defer_hard_irqs - set defer_hard_irqs for all NAPIs of a netdev
244  * @netdev: the net_device for which all NAPIs will have defer_hard_irqs set
245  * @defer: the defer_hard_irqs value to set
246  */
247 static inline void netdev_set_defer_hard_irqs(struct net_device *netdev,
248 					      u32 defer)
249 {
250 	unsigned int count = max(netdev->num_rx_queues,
251 				 netdev->num_tx_queues);
252 	struct napi_struct *napi;
253 	int i;
254 
255 	WRITE_ONCE(netdev->napi_defer_hard_irqs, defer);
256 	list_for_each_entry(napi, &netdev->napi_list, dev_list)
257 		napi_set_defer_hard_irqs(napi, defer);
258 
259 	for (i = 0; i < count; i++)
260 		netdev->napi_config[i].defer_hard_irqs = defer;
261 }
262 
263 /**
264  * napi_get_gro_flush_timeout - get the gro_flush_timeout
265  * @n: napi struct to get the gro_flush_timeout from
266  *
267  * Return: the per-NAPI value of the gro_flush_timeout field.
268  */
269 static inline unsigned long
270 napi_get_gro_flush_timeout(const struct napi_struct *n)
271 {
272 	return READ_ONCE(n->gro_flush_timeout);
273 }
274 
275 /**
276  * napi_set_gro_flush_timeout - set the gro_flush_timeout for a napi
277  * @n: napi struct to set the gro_flush_timeout
278  * @timeout: timeout value to set
279  *
280  * napi_set_gro_flush_timeout sets the per-NAPI gro_flush_timeout
281  */
282 static inline void napi_set_gro_flush_timeout(struct napi_struct *n,
283 					      unsigned long timeout)
284 {
285 	WRITE_ONCE(n->gro_flush_timeout, timeout);
286 }
287 
288 /**
289  * netdev_set_gro_flush_timeout - set gro_flush_timeout of a netdev's NAPIs
290  * @netdev: the net_device for which all NAPIs will have gro_flush_timeout set
291  * @timeout: the timeout value to set
292  */
293 static inline void netdev_set_gro_flush_timeout(struct net_device *netdev,
294 						unsigned long timeout)
295 {
296 	unsigned int count = max(netdev->num_rx_queues,
297 				 netdev->num_tx_queues);
298 	struct napi_struct *napi;
299 	int i;
300 
301 	WRITE_ONCE(netdev->gro_flush_timeout, timeout);
302 	list_for_each_entry(napi, &netdev->napi_list, dev_list)
303 		napi_set_gro_flush_timeout(napi, timeout);
304 
305 	for (i = 0; i < count; i++)
306 		netdev->napi_config[i].gro_flush_timeout = timeout;
307 }
308 
309 /**
310  * napi_get_irq_suspend_timeout - get the irq_suspend_timeout
311  * @n: napi struct to get the irq_suspend_timeout from
312  *
313  * Return: the per-NAPI value of the irq_suspend_timeout field.
314  */
315 static inline unsigned long
316 napi_get_irq_suspend_timeout(const struct napi_struct *n)
317 {
318 	return READ_ONCE(n->irq_suspend_timeout);
319 }
320 
321 /**
322  * napi_set_irq_suspend_timeout - set the irq_suspend_timeout for a napi
323  * @n: napi struct to set the irq_suspend_timeout
324  * @timeout: timeout value to set
325  *
326  * napi_set_irq_suspend_timeout sets the per-NAPI irq_suspend_timeout
327  */
328 static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,
329 						unsigned long timeout)
330 {
331 	WRITE_ONCE(n->irq_suspend_timeout, timeout);
332 }
333 
334 static inline enum netdev_napi_threaded napi_get_threaded(struct napi_struct *n)
335 {
336 	if (test_bit(NAPI_STATE_THREADED_BUSY_POLL, &n->state))
337 		return NETDEV_NAPI_THREADED_BUSY_POLL;
338 
339 	if (test_bit(NAPI_STATE_THREADED, &n->state))
340 		return NETDEV_NAPI_THREADED_ENABLED;
341 
342 	return NETDEV_NAPI_THREADED_DISABLED;
343 }
344 
345 static inline enum netdev_napi_threaded
346 napi_get_threaded_config(struct net_device *dev, struct napi_struct *n)
347 {
348 	if (n->config)
349 		return n->config->threaded;
350 	return dev->threaded;
351 }
352 
353 int napi_set_threaded(struct napi_struct *n,
354 		      enum netdev_napi_threaded threaded);
355 
356 int netif_set_threaded(struct net_device *dev,
357 		       enum netdev_napi_threaded threaded);
358 
359 int rps_cpumask_housekeeping(struct cpumask *mask);
360 
361 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
362 void xdp_do_check_flushed(struct napi_struct *napi);
363 #else
364 static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
365 #endif
366 
367 /* Best effort check that NAPI is not idle (can't be scheduled to run) */
368 static inline void napi_assert_will_not_race(const struct napi_struct *napi)
369 {
370 	/* uninitialized instance, can't race */
371 	if (!napi->poll_list.next)
372 		return;
373 
374 	/* SCHED bit is set on disabled instances */
375 	WARN_ON(!test_bit(NAPI_STATE_SCHED, &napi->state));
376 	WARN_ON(READ_ONCE(napi->list_owner) != -1);
377 }
378 
379 void kick_defer_list_purge(unsigned int cpu);
380 
381 int dev_set_hwtstamp_phylib(struct net_device *dev,
382 			    struct kernel_hwtstamp_config *cfg,
383 			    struct netlink_ext_ack *extack);
384 int dev_get_hwtstamp_phylib(struct net_device *dev,
385 			    struct kernel_hwtstamp_config *cfg);
386 int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg);
387 
388 #endif
389