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