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