pkt_sched.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. static inline void *qdisc_priv(struct Qdisc *q)
  20. {
  21. return &q->privdata;
  22. }
  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. u64 last_expires;
  52. struct hrtimer timer;
  53. struct Qdisc *qdisc;
  54. };
  55. void qdisc_watchdog_init_clockid(struct qdisc_watchdog *wd, struct Qdisc *qdisc,
  56. clockid_t clockid);
  57. void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc);
  58. void qdisc_watchdog_schedule_range_ns(struct qdisc_watchdog *wd, u64 expires,
  59. u64 delta_ns);
  60. static inline void qdisc_watchdog_schedule_ns(struct qdisc_watchdog *wd,
  61. u64 expires)
  62. {
  63. return qdisc_watchdog_schedule_range_ns(wd, expires, 0ULL);
  64. }
  65. static inline void qdisc_watchdog_schedule(struct qdisc_watchdog *wd,
  66. psched_time_t expires)
  67. {
  68. qdisc_watchdog_schedule_ns(wd, PSCHED_TICKS2NS(expires));
  69. }
  70. void qdisc_watchdog_cancel(struct qdisc_watchdog *wd);
  71. extern struct Qdisc_ops pfifo_qdisc_ops;
  72. extern struct Qdisc_ops bfifo_qdisc_ops;
  73. extern struct Qdisc_ops pfifo_head_drop_qdisc_ops;
  74. int fifo_set_limit(struct Qdisc *q, unsigned int limit);
  75. struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
  76. unsigned int limit,
  77. struct netlink_ext_ack *extack);
  78. int register_qdisc(struct Qdisc_ops *qops);
  79. void unregister_qdisc(struct Qdisc_ops *qops);
  80. void qdisc_get_default(char *id, size_t len);
  81. int qdisc_set_default(const char *id);
  82. void qdisc_hash_add(struct Qdisc *q, bool invisible);
  83. void qdisc_hash_del(struct Qdisc *q);
  84. struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle);
  85. struct Qdisc *qdisc_lookup_rcu(struct net_device *dev, u32 handle);
  86. struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r,
  87. struct nlattr *tab,
  88. struct netlink_ext_ack *extack);
  89. void qdisc_put_rtab(struct qdisc_rate_table *tab);
  90. void qdisc_put_stab(struct qdisc_size_table *tab);
  91. void qdisc_warn_nonwc(const char *txt, struct Qdisc *qdisc);
  92. bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
  93. struct net_device *dev, struct netdev_queue *txq,
  94. spinlock_t *root_lock, bool validate);
  95. void __qdisc_run(struct Qdisc *q);
  96. static inline void qdisc_run(struct Qdisc *q)
  97. {
  98. if (qdisc_run_begin(q)) {
  99. __qdisc_run(q);
  100. qdisc_run_end(q);
  101. }
  102. }
  103. extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
  104. /* Calculate maximal size of packet seen by hard_start_xmit
  105. routine of this device.
  106. */
  107. static inline unsigned int psched_mtu(const struct net_device *dev)
  108. {
  109. return READ_ONCE(dev->mtu) + dev->hard_header_len;
  110. }
  111. static inline struct net *qdisc_net(struct Qdisc *q)
  112. {
  113. return dev_net(q->dev_queue->dev);
  114. }
  115. struct tc_query_caps_base {
  116. enum tc_setup_type type;
  117. void *caps;
  118. };
  119. struct tc_cbs_qopt_offload {
  120. u8 enable;
  121. s32 queue;
  122. s32 hicredit;
  123. s32 locredit;
  124. s32 idleslope;
  125. s32 sendslope;
  126. };
  127. struct tc_etf_qopt_offload {
  128. u8 enable;
  129. s32 queue;
  130. };
  131. struct tc_taprio_caps {
  132. bool supports_queue_max_sdu:1;
  133. };
  134. struct tc_taprio_sched_entry {
  135. u8 command; /* TC_TAPRIO_CMD_* */
  136. /* The gate_mask in the offloading side refers to traffic classes */
  137. u32 gate_mask;
  138. u32 interval;
  139. };
  140. struct tc_taprio_qopt_offload {
  141. u8 enable;
  142. ktime_t base_time;
  143. u64 cycle_time;
  144. u64 cycle_time_extension;
  145. u32 max_sdu[TC_MAX_QUEUE];
  146. size_t num_entries;
  147. struct tc_taprio_sched_entry entries[];
  148. };
  149. #if IS_ENABLED(CONFIG_NET_SCH_TAPRIO)
  150. /* Reference counting */
  151. struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
  152. *offload);
  153. void taprio_offload_free(struct tc_taprio_qopt_offload *offload);
  154. #else
  155. /* Reference counting */
  156. static inline struct tc_taprio_qopt_offload *
  157. taprio_offload_get(struct tc_taprio_qopt_offload *offload)
  158. {
  159. return NULL;
  160. }
  161. static inline void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
  162. {
  163. }
  164. #endif
  165. /* Ensure skb_mstamp_ns, which might have been populated with the txtime, is
  166. * not mistaken for a software timestamp, because this will otherwise prevent
  167. * the dispatch of hardware timestamps to the socket.
  168. */
  169. static inline void skb_txtime_consumed(struct sk_buff *skb)
  170. {
  171. skb->tstamp = ktime_set(0, 0);
  172. }
  173. struct tc_skb_cb {
  174. struct qdisc_skb_cb qdisc_cb;
  175. u16 mru;
  176. u8 post_ct:1;
  177. u8 post_ct_snat:1;
  178. u8 post_ct_dnat:1;
  179. u16 zone; /* Only valid if post_ct = true */
  180. };
  181. static inline struct tc_skb_cb *tc_skb_cb(const struct sk_buff *skb)
  182. {
  183. struct tc_skb_cb *cb = (struct tc_skb_cb *)skb->cb;
  184. BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb));
  185. return cb;
  186. }
  187. static inline bool tc_qdisc_stats_dump(struct Qdisc *sch,
  188. unsigned long cl,
  189. struct qdisc_walker *arg)
  190. {
  191. if (arg->count >= arg->skip && arg->fn(sch, cl, arg) < 0) {
  192. arg->stop = 1;
  193. return false;
  194. }
  195. arg->count++;
  196. return true;
  197. }
  198. #endif