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