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