pkt_sched.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_PKT_SCHED_H
  3. #define __NET_PKT_SCHED_H
  4. #include <linux/jiffies.h>
  5. #include <linux/ktime.h>
  6. #include <linux/if_vlan.h>
  7. #include <linux/netdevice.h>
  8. #include <net/sch_generic.h>
  9. #include <net/net_namespace.h>
  10. #include <uapi/linux/pkt_sched.h>
  11. #define DEFAULT_TX_QUEUE_LEN 1000
  12. #define STAB_SIZE_LOG_MAX 30
  13. struct qdisc_walker {
  14. int stop;
  15. int skip;
  16. int count;
  17. int (*fn)(struct Qdisc *, unsigned long cl, struct qdisc_walker *);
  18. };
  19. #define qdisc_priv(q) \
  20. _Generic(q, \
  21. const struct Qdisc * : (const void *)&q->privdata, \
  22. struct Qdisc * : (void *)&q->privdata)
  23. static inline struct Qdisc *qdisc_from_priv(void *priv)
  24. {
  25. return container_of(priv, struct Qdisc, privdata);
  26. }
  27. /*
  28. Timer resolution MUST BE < 10% of min_schedulable_packet_size/bandwidth
  29. Normal IP packet size ~ 512byte, hence:
  30. 0.5Kbyte/1Mbyte/sec = 0.5msec, so that we need 50usec timer for
  31. 10Mbit ethernet.
  32. 10msec resolution -> <50Kbit/sec.
  33. The result: [34]86 is not good choice for QoS router :-(
  34. The things are not so bad, because we may use artificial
  35. clock evaluated by integration of network data flow
  36. in the most critical places.
  37. */
  38. typedef u64 psched_time_t;
  39. typedef long psched_tdiff_t;
  40. /* Avoid doing 64 bit divide */
  41. #define PSCHED_SHIFT 6
  42. #define PSCHED_TICKS2NS(x) ((s64)(x) << PSCHED_SHIFT)
  43. #define PSCHED_NS2TICKS(x) ((x) >> PSCHED_SHIFT)
  44. #define PSCHED_TICKS_PER_SEC PSCHED_NS2TICKS(NSEC_PER_SEC)
  45. #define PSCHED_PASTPERFECT 0
  46. static inline psched_time_t psched_get_time(void)
  47. {
  48. return PSCHED_NS2TICKS(ktime_get_ns());
  49. }
  50. struct qdisc_watchdog {
  51. struct hrtimer timer;
  52. struct Qdisc *qdisc;
  53. };
  54. void qdisc_watchdog_init_clockid(struct qdisc_watchdog *wd, struct Qdisc *qdisc,
  55. clockid_t clockid);
  56. void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc);
  57. void qdisc_watchdog_schedule_range_ns(struct qdisc_watchdog *wd, u64 expires,
  58. u64 delta_ns);
  59. static inline void qdisc_watchdog_schedule_ns(struct qdisc_watchdog *wd,
  60. u64 expires)
  61. {
  62. return qdisc_watchdog_schedule_range_ns(wd, expires, 0ULL);
  63. }
  64. static inline void qdisc_watchdog_schedule(struct qdisc_watchdog *wd,
  65. psched_time_t expires)
  66. {
  67. qdisc_watchdog_schedule_ns(wd, PSCHED_TICKS2NS(expires));
  68. }
  69. void qdisc_watchdog_cancel(struct qdisc_watchdog *wd);
  70. extern struct Qdisc_ops pfifo_qdisc_ops;
  71. extern struct Qdisc_ops bfifo_qdisc_ops;
  72. extern struct Qdisc_ops pfifo_head_drop_qdisc_ops;
  73. int fifo_set_limit(struct Qdisc *q, unsigned int limit);
  74. struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
  75. unsigned int limit,
  76. struct netlink_ext_ack *extack);
  77. int register_qdisc(struct Qdisc_ops *qops);
  78. void unregister_qdisc(struct Qdisc_ops *qops);
  79. #define NET_SCH_ALIAS_PREFIX "net-sch-"
  80. #define MODULE_ALIAS_NET_SCH(id) MODULE_ALIAS(NET_SCH_ALIAS_PREFIX id)
  81. void qdisc_get_default(char *id, size_t len);
  82. int qdisc_set_default(const char *id);
  83. void qdisc_hash_add(struct Qdisc *q, bool invisible);
  84. void qdisc_hash_del(struct Qdisc *q);
  85. struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle);
  86. struct Qdisc *qdisc_lookup_rcu(struct net_device *dev, u32 handle);
  87. struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r,
  88. struct nlattr *tab,
  89. struct netlink_ext_ack *extack);
  90. void qdisc_put_rtab(struct qdisc_rate_table *tab);
  91. void qdisc_put_stab(struct qdisc_size_table *tab);
  92. void qdisc_warn_nonwc(const char *txt, struct Qdisc *qdisc);
  93. bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
  94. struct net_device *dev, struct netdev_queue *txq,
  95. spinlock_t *root_lock, bool validate);
  96. void __qdisc_run(struct Qdisc *q);
  97. static inline void qdisc_run(struct Qdisc *q)
  98. {
  99. if (qdisc_run_begin(q)) {
  100. __qdisc_run(q);
  101. qdisc_run_end(q);
  102. }
  103. }
  104. extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
  105. /* Calculate maximal size of packet seen by hard_start_xmit
  106. routine of this device.
  107. */
  108. static inline unsigned int psched_mtu(const struct net_device *dev)
  109. {
  110. return READ_ONCE(dev->mtu) + dev->hard_header_len;
  111. }
  112. static inline struct net *qdisc_net(struct Qdisc *q)
  113. {
  114. return dev_net(q->dev_queue->dev);
  115. }
  116. struct tc_query_caps_base {
  117. enum tc_setup_type type;
  118. void *caps;
  119. };
  120. struct tc_cbs_qopt_offload {
  121. u8 enable;
  122. s32 queue;
  123. s32 hicredit;
  124. s32 locredit;
  125. s32 idleslope;
  126. s32 sendslope;
  127. };
  128. struct tc_etf_qopt_offload {
  129. u8 enable;
  130. s32 queue;
  131. };
  132. struct tc_mqprio_caps {
  133. bool validate_queue_counts:1;
  134. };
  135. struct tc_mqprio_qopt_offload {
  136. /* struct tc_mqprio_qopt must always be the first element */
  137. struct tc_mqprio_qopt qopt;
  138. struct netlink_ext_ack *extack;
  139. u16 mode;
  140. u16 shaper;
  141. u32 flags;
  142. u64 min_rate[TC_QOPT_MAX_QUEUE];
  143. u64 max_rate[TC_QOPT_MAX_QUEUE];
  144. unsigned long preemptible_tcs;
  145. };
  146. struct tc_taprio_caps {
  147. bool supports_queue_max_sdu:1;
  148. bool gate_mask_per_txq:1;
  149. /* Device expects lower TXQ numbers to have higher priority over higher
  150. * TXQs, regardless of their TC mapping. DO NOT USE FOR NEW DRIVERS,
  151. * INSTEAD ENFORCE A PROPER TC:TXQ MAPPING COMING FROM USER SPACE.
  152. */
  153. bool broken_mqprio:1;
  154. };
  155. enum tc_taprio_qopt_cmd {
  156. TAPRIO_CMD_REPLACE,
  157. TAPRIO_CMD_DESTROY,
  158. TAPRIO_CMD_STATS,
  159. TAPRIO_CMD_QUEUE_STATS,
  160. };
  161. /**
  162. * struct tc_taprio_qopt_stats - IEEE 802.1Qbv statistics
  163. * @window_drops: Frames that were dropped because they were too large to be
  164. * transmitted in any of the allotted time windows (open gates) for their
  165. * traffic class.
  166. * @tx_overruns: Frames still being transmitted by the MAC after the
  167. * transmission gate associated with their traffic class has closed.
  168. * Equivalent to `12.29.1.1.2 TransmissionOverrun` from 802.1Q-2018.
  169. */
  170. struct tc_taprio_qopt_stats {
  171. u64 window_drops;
  172. u64 tx_overruns;
  173. };
  174. struct tc_taprio_qopt_queue_stats {
  175. int queue;
  176. struct tc_taprio_qopt_stats stats;
  177. };
  178. struct tc_taprio_sched_entry {
  179. u8 command; /* TC_TAPRIO_CMD_* */
  180. /* The gate_mask in the offloading side refers to traffic classes */
  181. u32 gate_mask;
  182. u32 interval;
  183. };
  184. struct tc_taprio_qopt_offload {
  185. enum tc_taprio_qopt_cmd cmd;
  186. union {
  187. /* TAPRIO_CMD_STATS */
  188. struct tc_taprio_qopt_stats stats;
  189. /* TAPRIO_CMD_QUEUE_STATS */
  190. struct tc_taprio_qopt_queue_stats queue_stats;
  191. /* TAPRIO_CMD_REPLACE */
  192. struct {
  193. struct tc_mqprio_qopt_offload mqprio;
  194. struct netlink_ext_ack *extack;
  195. ktime_t base_time;
  196. u64 cycle_time;
  197. u64 cycle_time_extension;
  198. u32 max_sdu[TC_MAX_QUEUE];
  199. size_t num_entries;
  200. struct tc_taprio_sched_entry entries[];
  201. };
  202. };
  203. };
  204. #if IS_ENABLED(CONFIG_NET_SCH_TAPRIO)
  205. /* Reference counting */
  206. struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
  207. *offload);
  208. void taprio_offload_free(struct tc_taprio_qopt_offload *offload);
  209. #else
  210. /* Reference counting */
  211. static inline struct tc_taprio_qopt_offload *
  212. taprio_offload_get(struct tc_taprio_qopt_offload *offload)
  213. {
  214. return NULL;
  215. }
  216. static inline void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
  217. {
  218. }
  219. #endif
  220. /* Ensure skb_mstamp_ns, which might have been populated with the txtime, is
  221. * not mistaken for a software timestamp, because this will otherwise prevent
  222. * the dispatch of hardware timestamps to the socket.
  223. */
  224. static inline void skb_txtime_consumed(struct sk_buff *skb)
  225. {
  226. skb->tstamp = ktime_set(0, 0);
  227. }
  228. static inline bool tc_qdisc_stats_dump(struct Qdisc *sch,
  229. unsigned long cl,
  230. struct qdisc_walker *arg)
  231. {
  232. if (arg->count >= arg->skip && arg->fn(sch, cl, arg) < 0) {
  233. arg->stop = 1;
  234. return false;
  235. }
  236. arg->count++;
  237. return true;
  238. }
  239. #endif