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