xref: /linux/net/core/dev.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
16264f58cSJakub Kicinski /* SPDX-License-Identifier: GPL-2.0-or-later */
26264f58cSJakub Kicinski #ifndef _NET_CORE_DEV_H
36264f58cSJakub Kicinski #define _NET_CORE_DEV_H
46264f58cSJakub Kicinski 
56264f58cSJakub Kicinski #include <linux/types.h>
6c7d52737SEric Dumazet #include <linux/rwsem.h>
7b9495b56SEric Dumazet #include <linux/netdevice.h>
86264f58cSJakub Kicinski 
96264f58cSJakub Kicinski struct net;
106264f58cSJakub Kicinski struct netlink_ext_ack;
11370ca718SPaolo Abeni struct cpumask;
126264f58cSJakub Kicinski 
136264f58cSJakub Kicinski /* Random bits of netdevice that don't need to be exposed */
146264f58cSJakub Kicinski #define FLOW_LIMIT_HISTORY	(1 << 7)  /* must be ^2 and !overflow buckets */
156264f58cSJakub Kicinski struct sd_flow_limit {
166264f58cSJakub Kicinski 	u64			count;
176264f58cSJakub Kicinski 	unsigned int		num_buckets;
186264f58cSJakub Kicinski 	unsigned int		history_head;
196264f58cSJakub Kicinski 	u16			history[FLOW_LIMIT_HISTORY];
206264f58cSJakub Kicinski 	u8			buckets[];
216264f58cSJakub Kicinski };
226264f58cSJakub Kicinski 
236264f58cSJakub Kicinski extern int netdev_flow_limit_table_len;
246264f58cSJakub Kicinski 
256264f58cSJakub Kicinski #ifdef CONFIG_PROC_FS
266264f58cSJakub Kicinski int __init dev_proc_init(void);
276264f58cSJakub Kicinski #else
286264f58cSJakub Kicinski #define dev_proc_init() 0
296264f58cSJakub Kicinski #endif
306264f58cSJakub Kicinski 
316264f58cSJakub Kicinski void linkwatch_init_dev(struct net_device *dev);
326264f58cSJakub Kicinski void linkwatch_run_queue(void);
336264f58cSJakub Kicinski 
346264f58cSJakub Kicinski void dev_addr_flush(struct net_device *dev);
356264f58cSJakub Kicinski int dev_addr_init(struct net_device *dev);
366264f58cSJakub Kicinski void dev_addr_check(struct net_device *dev);
376264f58cSJakub Kicinski 
386264f58cSJakub Kicinski /* sysctls not referred to from outside net/core/ */
396264f58cSJakub Kicinski extern int		netdev_unregister_timeout_secs;
406264f58cSJakub Kicinski extern int		weight_p;
416264f58cSJakub Kicinski extern int		dev_weight_rx_bias;
426264f58cSJakub Kicinski extern int		dev_weight_tx_bias;
436264f58cSJakub Kicinski 
44c7d52737SEric Dumazet extern struct rw_semaphore dev_addr_sem;
45c7d52737SEric Dumazet 
466264f58cSJakub Kicinski /* rtnl helpers */
476264f58cSJakub Kicinski extern struct list_head net_todo_list;
486264f58cSJakub Kicinski void netdev_run_todo(void);
496264f58cSJakub Kicinski 
506264f58cSJakub Kicinski /* netdev management, shared between various uAPI entry points */
516264f58cSJakub Kicinski struct netdev_name_node {
526264f58cSJakub Kicinski 	struct hlist_node hlist;
536264f58cSJakub Kicinski 	struct list_head list;
546264f58cSJakub Kicinski 	struct net_device *dev;
556264f58cSJakub Kicinski 	const char *name;
56723de3ebSJakub Kicinski 	struct rcu_head rcu;
576264f58cSJakub Kicinski };
586264f58cSJakub Kicinski 
596264f58cSJakub Kicinski int netdev_get_name(struct net *net, char *name, int ifindex);
606264f58cSJakub Kicinski int dev_change_name(struct net_device *dev, const char *newname);
616264f58cSJakub Kicinski 
627663d522SJakub Kicinski #define netdev_for_each_altname(dev, namenode)				\
637663d522SJakub Kicinski 	list_for_each_entry((namenode), &(dev)->name_node->list, list)
64d09486a0SJakub Kicinski #define netdev_for_each_altname_safe(dev, namenode, next)		\
65d09486a0SJakub Kicinski 	list_for_each_entry_safe((namenode), (next), &(dev)->name_node->list, \
66d09486a0SJakub Kicinski 				 list)
677663d522SJakub Kicinski 
686264f58cSJakub Kicinski int netdev_name_node_alt_create(struct net_device *dev, const char *name);
696264f58cSJakub Kicinski int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);
706264f58cSJakub Kicinski 
716264f58cSJakub Kicinski int dev_validate_mtu(struct net_device *dev, int mtu,
726264f58cSJakub Kicinski 		     struct netlink_ext_ack *extack);
736264f58cSJakub Kicinski int dev_set_mtu_ext(struct net_device *dev, int mtu,
746264f58cSJakub Kicinski 		    struct netlink_ext_ack *extack);
756264f58cSJakub Kicinski 
766264f58cSJakub Kicinski int dev_get_phys_port_id(struct net_device *dev,
776264f58cSJakub Kicinski 			 struct netdev_phys_item_id *ppid);
786264f58cSJakub Kicinski int dev_get_phys_port_name(struct net_device *dev,
796264f58cSJakub Kicinski 			   char *name, size_t len);
806264f58cSJakub Kicinski 
816264f58cSJakub Kicinski int dev_change_proto_down(struct net_device *dev, bool proto_down);
826264f58cSJakub Kicinski void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
836264f58cSJakub Kicinski 				  u32 value);
846264f58cSJakub Kicinski 
856264f58cSJakub Kicinski typedef int (*bpf_op_t)(struct net_device *dev, struct netdev_bpf *bpf);
866264f58cSJakub Kicinski int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
876264f58cSJakub Kicinski 		      int fd, int expected_fd, u32 flags);
886264f58cSJakub Kicinski 
896264f58cSJakub Kicinski int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
906264f58cSJakub Kicinski void dev_set_group(struct net_device *dev, int new_group);
916264f58cSJakub Kicinski int dev_change_carrier(struct net_device *dev, bool new_carrier);
926264f58cSJakub Kicinski 
936264f58cSJakub Kicinski void __dev_set_rx_mode(struct net_device *dev);
946264f58cSJakub Kicinski 
951d997f10SHangbin Liu void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
961d997f10SHangbin Liu 			unsigned int gchanges, u32 portid,
971d997f10SHangbin Liu 			const struct nlmsghdr *nlh);
981d997f10SHangbin Liu 
9977f4aa9aSHangbin Liu void unregister_netdevice_many_notify(struct list_head *head,
10077f4aa9aSHangbin Liu 				      u32 portid, const struct nlmsghdr *nlh);
10177f4aa9aSHangbin Liu 
netif_set_gso_max_size(struct net_device * dev,unsigned int size)102744d49daSJakub Kicinski static inline void netif_set_gso_max_size(struct net_device *dev,
103744d49daSJakub Kicinski 					  unsigned int size)
104744d49daSJakub Kicinski {
105744d49daSJakub Kicinski 	/* dev->gso_max_size is read locklessly from sk_setup_caps() */
106744d49daSJakub Kicinski 	WRITE_ONCE(dev->gso_max_size, size);
1079eefedd5SXin Long 	if (size <= GSO_LEGACY_MAX_SIZE)
1089eefedd5SXin Long 		WRITE_ONCE(dev->gso_ipv4_max_size, size);
109744d49daSJakub Kicinski }
110744d49daSJakub Kicinski 
netif_set_gso_max_segs(struct net_device * dev,unsigned int segs)111744d49daSJakub Kicinski static inline void netif_set_gso_max_segs(struct net_device *dev,
112744d49daSJakub Kicinski 					  unsigned int segs)
113744d49daSJakub Kicinski {
114744d49daSJakub Kicinski 	/* dev->gso_max_segs is read locklessly from sk_setup_caps() */
115744d49daSJakub Kicinski 	WRITE_ONCE(dev->gso_max_segs, segs);
116744d49daSJakub Kicinski }
117744d49daSJakub Kicinski 
netif_set_gro_max_size(struct net_device * dev,unsigned int size)118744d49daSJakub Kicinski static inline void netif_set_gro_max_size(struct net_device *dev,
119744d49daSJakub Kicinski 					  unsigned int size)
120744d49daSJakub Kicinski {
121744d49daSJakub Kicinski 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
122744d49daSJakub Kicinski 	WRITE_ONCE(dev->gro_max_size, size);
1239eefedd5SXin Long 	if (size <= GRO_LEGACY_MAX_SIZE)
1249eefedd5SXin Long 		WRITE_ONCE(dev->gro_ipv4_max_size, size);
1259eefedd5SXin Long }
1269eefedd5SXin Long 
netif_set_gso_ipv4_max_size(struct net_device * dev,unsigned int size)1279eefedd5SXin Long static inline void netif_set_gso_ipv4_max_size(struct net_device *dev,
1289eefedd5SXin Long 					       unsigned int size)
1299eefedd5SXin Long {
1309eefedd5SXin Long 	/* dev->gso_ipv4_max_size is read locklessly from sk_setup_caps() */
1319eefedd5SXin Long 	WRITE_ONCE(dev->gso_ipv4_max_size, size);
1329eefedd5SXin Long }
1339eefedd5SXin Long 
netif_set_gro_ipv4_max_size(struct net_device * dev,unsigned int size)1349eefedd5SXin Long static inline void netif_set_gro_ipv4_max_size(struct net_device *dev,
1359eefedd5SXin Long 					       unsigned int size)
1369eefedd5SXin Long {
1379eefedd5SXin Long 	/* This pairs with the READ_ONCE() in skb_gro_receive() */
1389eefedd5SXin Long 	WRITE_ONCE(dev->gro_ipv4_max_size, size);
139744d49daSJakub Kicinski }
140744d49daSJakub Kicinski 
141370ca718SPaolo Abeni int rps_cpumask_housekeeping(struct cpumask *mask);
1429a675ba5SSebastian Andrzej Siewior 
1439a675ba5SSebastian Andrzej Siewior #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
1449a675ba5SSebastian Andrzej Siewior void xdp_do_check_flushed(struct napi_struct *napi);
1459a675ba5SSebastian Andrzej Siewior #else
xdp_do_check_flushed(struct napi_struct * napi)1469a675ba5SSebastian Andrzej Siewior static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
1479a675ba5SSebastian Andrzej Siewior #endif
14827f91aafSAmritha Nambiar 
14927f91aafSAmritha Nambiar struct napi_struct *napi_by_id(unsigned int napi_id);
150b9495b56SEric Dumazet void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu);
151b9495b56SEric Dumazet 
1522fe50a4dSEric Dumazet #define XMIT_RECURSION_LIMIT	8
153*ecefbc09SSebastian Andrzej Siewior 
154*ecefbc09SSebastian Andrzej Siewior #ifndef CONFIG_PREEMPT_RT
dev_xmit_recursion(void)1552fe50a4dSEric Dumazet static inline bool dev_xmit_recursion(void)
1562fe50a4dSEric Dumazet {
1572fe50a4dSEric Dumazet 	return unlikely(__this_cpu_read(softnet_data.xmit.recursion) >
1582fe50a4dSEric Dumazet 			XMIT_RECURSION_LIMIT);
1592fe50a4dSEric Dumazet }
1602fe50a4dSEric Dumazet 
dev_xmit_recursion_inc(void)1612fe50a4dSEric Dumazet static inline void dev_xmit_recursion_inc(void)
1622fe50a4dSEric Dumazet {
1632fe50a4dSEric Dumazet 	__this_cpu_inc(softnet_data.xmit.recursion);
1642fe50a4dSEric Dumazet }
1652fe50a4dSEric Dumazet 
dev_xmit_recursion_dec(void)1662fe50a4dSEric Dumazet static inline void dev_xmit_recursion_dec(void)
1672fe50a4dSEric Dumazet {
1682fe50a4dSEric Dumazet 	__this_cpu_dec(softnet_data.xmit.recursion);
1692fe50a4dSEric Dumazet }
170*ecefbc09SSebastian Andrzej Siewior #else
dev_xmit_recursion(void)171*ecefbc09SSebastian Andrzej Siewior static inline bool dev_xmit_recursion(void)
172*ecefbc09SSebastian Andrzej Siewior {
173*ecefbc09SSebastian Andrzej Siewior 	return unlikely(current->net_xmit.recursion > XMIT_RECURSION_LIMIT);
174*ecefbc09SSebastian Andrzej Siewior }
175*ecefbc09SSebastian Andrzej Siewior 
dev_xmit_recursion_inc(void)176*ecefbc09SSebastian Andrzej Siewior static inline void dev_xmit_recursion_inc(void)
177*ecefbc09SSebastian Andrzej Siewior {
178*ecefbc09SSebastian Andrzej Siewior 	current->net_xmit.recursion++;
179*ecefbc09SSebastian Andrzej Siewior }
180*ecefbc09SSebastian Andrzej Siewior 
dev_xmit_recursion_dec(void)181*ecefbc09SSebastian Andrzej Siewior static inline void dev_xmit_recursion_dec(void)
182*ecefbc09SSebastian Andrzej Siewior {
183*ecefbc09SSebastian Andrzej Siewior 	current->net_xmit.recursion--;
184*ecefbc09SSebastian Andrzej Siewior }
185*ecefbc09SSebastian Andrzej Siewior #endif
1862fe50a4dSEric Dumazet 
187efb45930SKory Maincent int dev_set_hwtstamp_phylib(struct net_device *dev,
188efb45930SKory Maincent 			    struct kernel_hwtstamp_config *cfg,
189efb45930SKory Maincent 			    struct netlink_ext_ack *extack);
190efb45930SKory Maincent 
1916264f58cSJakub Kicinski #endif
192