dev.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _NET_CORE_DEV_H
  3. #define _NET_CORE_DEV_H
  4. #include <linux/cleanup.h>
  5. #include <linux/types.h>
  6. #include <linux/rwsem.h>
  7. #include <linux/netdevice.h>
  8. #include <net/netdev_lock.h>
  9. struct net;
  10. struct netlink_ext_ack;
  11. struct netdev_queue_config;
  12. struct cpumask;
  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. struct rcu_head rcu;
  17. unsigned int count;
  18. u8 log_buckets;
  19. unsigned int history_head;
  20. u16 history[FLOW_LIMIT_HISTORY];
  21. u8 buckets[];
  22. };
  23. extern int netdev_flow_limit_table_len;
  24. struct napi_struct *
  25. netdev_napi_by_id_lock(struct net *net, unsigned int napi_id);
  26. struct net_device *dev_get_by_napi_id(unsigned int napi_id);
  27. struct net_device *__netdev_put_lock(struct net_device *dev, struct net *net);
  28. struct net_device *
  29. netdev_xa_find_lock(struct net *net, struct net_device *dev,
  30. unsigned long *index);
  31. DEFINE_FREE(netdev_unlock, struct net_device *, if (_T) netdev_unlock(_T));
  32. #define for_each_netdev_lock_scoped(net, var_name, ifindex) \
  33. for (struct net_device *var_name __free(netdev_unlock) = NULL; \
  34. (var_name = netdev_xa_find_lock(net, var_name, &ifindex)); \
  35. ifindex++)
  36. struct net_device *
  37. netdev_get_by_index_lock_ops_compat(struct net *net, int ifindex);
  38. struct net_device *
  39. netdev_xa_find_lock_ops_compat(struct net *net, struct net_device *dev,
  40. unsigned long *index);
  41. DEFINE_FREE(netdev_unlock_ops_compat, struct net_device *,
  42. if (_T) netdev_unlock_ops_compat(_T));
  43. #define for_each_netdev_lock_ops_compat_scoped(net, var_name, ifindex) \
  44. for (struct net_device *var_name __free(netdev_unlock_ops_compat) = NULL; \
  45. (var_name = netdev_xa_find_lock_ops_compat(net, var_name, \
  46. &ifindex)); \
  47. ifindex++)
  48. #ifdef CONFIG_PROC_FS
  49. int __init dev_proc_init(void);
  50. #else
  51. #define dev_proc_init() 0
  52. #endif
  53. void linkwatch_init_dev(struct net_device *dev);
  54. void linkwatch_run_queue(void);
  55. void dev_addr_flush(struct net_device *dev);
  56. int dev_addr_init(struct net_device *dev);
  57. void dev_addr_check(struct net_device *dev);
  58. #if IS_ENABLED(CONFIG_NET_SHAPER)
  59. void net_shaper_flush_netdev(struct net_device *dev);
  60. void net_shaper_set_real_num_tx_queues(struct net_device *dev,
  61. unsigned int txq);
  62. #else
  63. static inline void net_shaper_flush_netdev(struct net_device *dev) {}
  64. static inline void net_shaper_set_real_num_tx_queues(struct net_device *dev,
  65. unsigned int txq) {}
  66. #endif
  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. extern struct rw_semaphore dev_addr_sem;
  73. /* rtnl helpers */
  74. extern struct list_head net_todo_list;
  75. void netdev_run_todo(void);
  76. int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
  77. struct netdev_queue_config *qcfg,
  78. struct netlink_ext_ack *extack);
  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. int netdev_get_name(struct net *net, char *name, int ifindex);
  88. int netif_change_name(struct net_device *dev, const char *newname);
  89. int dev_change_name(struct net_device *dev, const char *newname);
  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. int netdev_name_node_alt_create(struct net_device *dev, const char *name);
  96. int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);
  97. int dev_validate_mtu(struct net_device *dev, int mtu,
  98. struct netlink_ext_ack *extack);
  99. int netif_set_mtu_ext(struct net_device *dev, int new_mtu,
  100. struct netlink_ext_ack *extack);
  101. int dev_get_phys_port_id(struct net_device *dev,
  102. struct netdev_phys_item_id *ppid);
  103. int dev_get_phys_port_name(struct net_device *dev,
  104. char *name, size_t len);
  105. int netif_change_proto_down(struct net_device *dev, bool proto_down);
  106. int dev_change_proto_down(struct net_device *dev, bool proto_down);
  107. void netdev_change_proto_down_reason_locked(struct net_device *dev,
  108. unsigned long mask, u32 value);
  109. typedef int (*bpf_op_t)(struct net_device *dev, struct netdev_bpf *bpf);
  110. int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
  111. int fd, int expected_fd, u32 flags);
  112. int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
  113. int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len);
  114. void netif_set_group(struct net_device *dev, int new_group);
  115. void dev_set_group(struct net_device *dev, int new_group);
  116. int netif_change_carrier(struct net_device *dev, bool new_carrier);
  117. int dev_change_carrier(struct net_device *dev, bool new_carrier);
  118. void __dev_set_rx_mode(struct net_device *dev);
  119. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
  120. unsigned int gchanges, u32 portid,
  121. const struct nlmsghdr *nlh);
  122. void unregister_netdevice_many_notify(struct list_head *head,
  123. u32 portid, const struct nlmsghdr *nlh);
  124. static inline void netif_set_up(struct net_device *dev, bool value)
  125. {
  126. if (value)
  127. dev->flags |= IFF_UP;
  128. else
  129. dev->flags &= ~IFF_UP;
  130. if (!netdev_need_ops_lock(dev))
  131. netdev_lock(dev);
  132. dev->up = value;
  133. if (!netdev_need_ops_lock(dev))
  134. netdev_unlock(dev);
  135. }
  136. static inline void netif_set_gso_max_size(struct net_device *dev,
  137. unsigned int size)
  138. {
  139. /* dev->gso_max_size is read locklessly from sk_setup_caps() */
  140. WRITE_ONCE(dev->gso_max_size, size);
  141. if (size <= GSO_LEGACY_MAX_SIZE)
  142. WRITE_ONCE(dev->gso_ipv4_max_size, size);
  143. }
  144. static inline void netif_set_gso_max_segs(struct net_device *dev,
  145. unsigned int segs)
  146. {
  147. /* dev->gso_max_segs is read locklessly from sk_setup_caps() */
  148. WRITE_ONCE(dev->gso_max_segs, segs);
  149. }
  150. static inline void netif_set_gro_max_size(struct net_device *dev,
  151. unsigned int size)
  152. {
  153. /* This pairs with the READ_ONCE() in skb_gro_receive() */
  154. WRITE_ONCE(dev->gro_max_size, size);
  155. if (size <= GRO_LEGACY_MAX_SIZE)
  156. WRITE_ONCE(dev->gro_ipv4_max_size, size);
  157. }
  158. static inline void netif_set_gso_ipv4_max_size(struct net_device *dev,
  159. unsigned int size)
  160. {
  161. /* dev->gso_ipv4_max_size is read locklessly from sk_setup_caps() */
  162. WRITE_ONCE(dev->gso_ipv4_max_size, size);
  163. }
  164. static inline void netif_set_gro_ipv4_max_size(struct net_device *dev,
  165. unsigned int size)
  166. {
  167. /* This pairs with the READ_ONCE() in skb_gro_receive() */
  168. WRITE_ONCE(dev->gro_ipv4_max_size, size);
  169. }
  170. /**
  171. * napi_get_defer_hard_irqs - get the NAPI's defer_hard_irqs
  172. * @n: napi struct to get the defer_hard_irqs field from
  173. *
  174. * Return: the per-NAPI value of the defar_hard_irqs field.
  175. */
  176. static inline u32 napi_get_defer_hard_irqs(const struct napi_struct *n)
  177. {
  178. return READ_ONCE(n->defer_hard_irqs);
  179. }
  180. /**
  181. * napi_set_defer_hard_irqs - set the defer_hard_irqs for a napi
  182. * @n: napi_struct to set the defer_hard_irqs field
  183. * @defer: the value the field should be set to
  184. */
  185. static inline void napi_set_defer_hard_irqs(struct napi_struct *n, u32 defer)
  186. {
  187. WRITE_ONCE(n->defer_hard_irqs, defer);
  188. }
  189. /**
  190. * netdev_set_defer_hard_irqs - set defer_hard_irqs for all NAPIs of a netdev
  191. * @netdev: the net_device for which all NAPIs will have defer_hard_irqs set
  192. * @defer: the defer_hard_irqs value to set
  193. */
  194. static inline void netdev_set_defer_hard_irqs(struct net_device *netdev,
  195. u32 defer)
  196. {
  197. unsigned int count = max(netdev->num_rx_queues,
  198. netdev->num_tx_queues);
  199. struct napi_struct *napi;
  200. int i;
  201. WRITE_ONCE(netdev->napi_defer_hard_irqs, defer);
  202. list_for_each_entry(napi, &netdev->napi_list, dev_list)
  203. napi_set_defer_hard_irqs(napi, defer);
  204. for (i = 0; i < count; i++)
  205. netdev->napi_config[i].defer_hard_irqs = defer;
  206. }
  207. /**
  208. * napi_get_gro_flush_timeout - get the gro_flush_timeout
  209. * @n: napi struct to get the gro_flush_timeout from
  210. *
  211. * Return: the per-NAPI value of the gro_flush_timeout field.
  212. */
  213. static inline unsigned long
  214. napi_get_gro_flush_timeout(const struct napi_struct *n)
  215. {
  216. return READ_ONCE(n->gro_flush_timeout);
  217. }
  218. /**
  219. * napi_set_gro_flush_timeout - set the gro_flush_timeout for a napi
  220. * @n: napi struct to set the gro_flush_timeout
  221. * @timeout: timeout value to set
  222. *
  223. * napi_set_gro_flush_timeout sets the per-NAPI gro_flush_timeout
  224. */
  225. static inline void napi_set_gro_flush_timeout(struct napi_struct *n,
  226. unsigned long timeout)
  227. {
  228. WRITE_ONCE(n->gro_flush_timeout, timeout);
  229. }
  230. /**
  231. * netdev_set_gro_flush_timeout - set gro_flush_timeout of a netdev's NAPIs
  232. * @netdev: the net_device for which all NAPIs will have gro_flush_timeout set
  233. * @timeout: the timeout value to set
  234. */
  235. static inline void netdev_set_gro_flush_timeout(struct net_device *netdev,
  236. unsigned long timeout)
  237. {
  238. unsigned int count = max(netdev->num_rx_queues,
  239. netdev->num_tx_queues);
  240. struct napi_struct *napi;
  241. int i;
  242. WRITE_ONCE(netdev->gro_flush_timeout, timeout);
  243. list_for_each_entry(napi, &netdev->napi_list, dev_list)
  244. napi_set_gro_flush_timeout(napi, timeout);
  245. for (i = 0; i < count; i++)
  246. netdev->napi_config[i].gro_flush_timeout = timeout;
  247. }
  248. /**
  249. * napi_get_irq_suspend_timeout - get the irq_suspend_timeout
  250. * @n: napi struct to get the irq_suspend_timeout from
  251. *
  252. * Return: the per-NAPI value of the irq_suspend_timeout field.
  253. */
  254. static inline unsigned long
  255. napi_get_irq_suspend_timeout(const struct napi_struct *n)
  256. {
  257. return READ_ONCE(n->irq_suspend_timeout);
  258. }
  259. /**
  260. * napi_set_irq_suspend_timeout - set the irq_suspend_timeout for a napi
  261. * @n: napi struct to set the irq_suspend_timeout
  262. * @timeout: timeout value to set
  263. *
  264. * napi_set_irq_suspend_timeout sets the per-NAPI irq_suspend_timeout
  265. */
  266. static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,
  267. unsigned long timeout)
  268. {
  269. WRITE_ONCE(n->irq_suspend_timeout, timeout);
  270. }
  271. static inline enum netdev_napi_threaded napi_get_threaded(struct napi_struct *n)
  272. {
  273. if (test_bit(NAPI_STATE_THREADED_BUSY_POLL, &n->state))
  274. return NETDEV_NAPI_THREADED_BUSY_POLL;
  275. if (test_bit(NAPI_STATE_THREADED, &n->state))
  276. return NETDEV_NAPI_THREADED_ENABLED;
  277. return NETDEV_NAPI_THREADED_DISABLED;
  278. }
  279. static inline enum netdev_napi_threaded
  280. napi_get_threaded_config(struct net_device *dev, struct napi_struct *n)
  281. {
  282. if (n->config)
  283. return n->config->threaded;
  284. return dev->threaded;
  285. }
  286. int napi_set_threaded(struct napi_struct *n,
  287. enum netdev_napi_threaded threaded);
  288. int netif_set_threaded(struct net_device *dev,
  289. enum netdev_napi_threaded threaded);
  290. int rps_cpumask_housekeeping(struct cpumask *mask);
  291. #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
  292. void xdp_do_check_flushed(struct napi_struct *napi);
  293. #else
  294. static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
  295. #endif
  296. /* Best effort check that NAPI is not idle (can't be scheduled to run) */
  297. static inline void napi_assert_will_not_race(const struct napi_struct *napi)
  298. {
  299. /* uninitialized instance, can't race */
  300. if (!napi->poll_list.next)
  301. return;
  302. /* SCHED bit is set on disabled instances */
  303. WARN_ON(!test_bit(NAPI_STATE_SCHED, &napi->state));
  304. WARN_ON(READ_ONCE(napi->list_owner) != -1);
  305. }
  306. void kick_defer_list_purge(unsigned int cpu);
  307. int dev_set_hwtstamp_phylib(struct net_device *dev,
  308. struct kernel_hwtstamp_config *cfg,
  309. struct netlink_ext_ack *extack);
  310. int dev_get_hwtstamp_phylib(struct net_device *dev,
  311. struct kernel_hwtstamp_config *cfg);
  312. int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg);
  313. #endif