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