sch_generic.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_SCHED_GENERIC_H
  3. #define __NET_SCHED_GENERIC_H
  4. #include <linux/netdevice.h>
  5. #include <linux/types.h>
  6. #include <linux/rcupdate.h>
  7. #include <linux/pkt_sched.h>
  8. #include <linux/pkt_cls.h>
  9. #include <linux/percpu.h>
  10. #include <linux/dynamic_queue_limits.h>
  11. #include <linux/list.h>
  12. #include <linux/refcount.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/mutex.h>
  15. #include <linux/rwsem.h>
  16. #include <linux/atomic.h>
  17. #include <linux/hashtable.h>
  18. #include <net/gen_stats.h>
  19. #include <net/rtnetlink.h>
  20. #include <net/flow_offload.h>
  21. #include <linux/xarray.h>
  22. struct Qdisc_ops;
  23. struct qdisc_walker;
  24. struct tcf_walker;
  25. struct module;
  26. struct bpf_flow_keys;
  27. struct qdisc_rate_table {
  28. struct tc_ratespec rate;
  29. u32 data[256];
  30. struct qdisc_rate_table *next;
  31. int refcnt;
  32. };
  33. enum qdisc_state_t {
  34. __QDISC_STATE_SCHED,
  35. __QDISC_STATE_DEACTIVATED,
  36. __QDISC_STATE_MISSED,
  37. __QDISC_STATE_DRAINING,
  38. };
  39. #define QDISC_STATE_MISSED BIT(__QDISC_STATE_MISSED)
  40. #define QDISC_STATE_DRAINING BIT(__QDISC_STATE_DRAINING)
  41. #define QDISC_STATE_NON_EMPTY (QDISC_STATE_MISSED | \
  42. QDISC_STATE_DRAINING)
  43. struct qdisc_size_table {
  44. struct rcu_head rcu;
  45. struct list_head list;
  46. struct tc_sizespec szopts;
  47. int refcnt;
  48. u16 data[];
  49. };
  50. /* similar to sk_buff_head, but skb->prev pointer is undefined. */
  51. struct qdisc_skb_head {
  52. struct sk_buff *head;
  53. struct sk_buff *tail;
  54. __u32 qlen;
  55. spinlock_t lock;
  56. };
  57. struct Qdisc {
  58. int (*enqueue)(struct sk_buff *skb,
  59. struct Qdisc *sch,
  60. struct sk_buff **to_free);
  61. struct sk_buff * (*dequeue)(struct Qdisc *sch);
  62. unsigned int flags;
  63. #define TCQ_F_BUILTIN 1
  64. #define TCQ_F_INGRESS 2
  65. #define TCQ_F_CAN_BYPASS 4
  66. #define TCQ_F_MQROOT 8
  67. #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
  68. * q->dev_queue : It can test
  69. * netif_xmit_frozen_or_stopped() before
  70. * dequeueing next packet.
  71. * Its true for MQ/MQPRIO slaves, or non
  72. * multiqueue device.
  73. */
  74. #define TCQ_F_WARN_NONWC (1 << 16)
  75. #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
  76. #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
  77. * qdisc_tree_decrease_qlen() should stop.
  78. */
  79. #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
  80. #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
  81. #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
  82. #define TCQ_F_DEQUEUE_DROPS 0x400 /* ->dequeue() can drop packets in q->to_free */
  83. u32 limit;
  84. const struct Qdisc_ops *ops;
  85. struct qdisc_size_table __rcu *stab;
  86. struct hlist_node hash;
  87. u32 handle;
  88. u32 parent;
  89. struct netdev_queue *dev_queue;
  90. struct net_rate_estimator __rcu *rate_est;
  91. struct gnet_stats_basic_sync __percpu *cpu_bstats;
  92. struct gnet_stats_queue __percpu *cpu_qstats;
  93. int pad;
  94. refcount_t refcnt;
  95. /* Cache line potentially dirtied in dequeue() or __netif_reschedule(). */
  96. __cacheline_group_begin(Qdisc_read_mostly) ____cacheline_aligned;
  97. struct sk_buff_head gso_skb;
  98. struct Qdisc *next_sched;
  99. struct sk_buff_head skb_bad_txq;
  100. __cacheline_group_end(Qdisc_read_mostly);
  101. /* Fields dirtied in dequeue() fast path. */
  102. __cacheline_group_begin(Qdisc_write) ____cacheline_aligned;
  103. struct qdisc_skb_head q;
  104. unsigned long state;
  105. struct gnet_stats_basic_sync bstats;
  106. bool running; /* must be written under qdisc spinlock */
  107. /* Note : we only change qstats.backlog in fast path. */
  108. struct gnet_stats_queue qstats;
  109. struct sk_buff *to_free;
  110. __cacheline_group_end(Qdisc_write);
  111. atomic_long_t defer_count ____cacheline_aligned_in_smp;
  112. struct llist_head defer_list;
  113. spinlock_t seqlock;
  114. struct rcu_head rcu;
  115. netdevice_tracker dev_tracker;
  116. struct lock_class_key root_lock_key;
  117. /* private data */
  118. long privdata[] ____cacheline_aligned;
  119. };
  120. static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
  121. {
  122. if (qdisc->flags & TCQ_F_BUILTIN)
  123. return;
  124. refcount_inc(&qdisc->refcnt);
  125. }
  126. static inline bool qdisc_refcount_dec_if_one(struct Qdisc *qdisc)
  127. {
  128. if (qdisc->flags & TCQ_F_BUILTIN)
  129. return true;
  130. return refcount_dec_if_one(&qdisc->refcnt);
  131. }
  132. /* Intended to be used by unlocked users, when concurrent qdisc release is
  133. * possible.
  134. */
  135. static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
  136. {
  137. if (qdisc->flags & TCQ_F_BUILTIN)
  138. return qdisc;
  139. if (refcount_inc_not_zero(&qdisc->refcnt))
  140. return qdisc;
  141. return NULL;
  142. }
  143. /* For !TCQ_F_NOLOCK qdisc: callers must either call this within a qdisc
  144. * root_lock section, or provide their own memory barriers -- ordering
  145. * against qdisc_run_begin/end() atomic bit operations.
  146. */
  147. static inline bool qdisc_is_running(struct Qdisc *qdisc)
  148. {
  149. if (qdisc->flags & TCQ_F_NOLOCK)
  150. return spin_is_locked(&qdisc->seqlock);
  151. return READ_ONCE(qdisc->running);
  152. }
  153. static inline bool nolock_qdisc_is_empty(const struct Qdisc *qdisc)
  154. {
  155. return !(READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY);
  156. }
  157. static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
  158. {
  159. return q->flags & TCQ_F_CPUSTATS;
  160. }
  161. static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
  162. {
  163. if (qdisc_is_percpu_stats(qdisc))
  164. return nolock_qdisc_is_empty(qdisc);
  165. return !READ_ONCE(qdisc->q.qlen);
  166. }
  167. /* For !TCQ_F_NOLOCK qdisc, qdisc_run_begin/end() must be invoked with
  168. * the qdisc root lock acquired.
  169. */
  170. static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  171. {
  172. if (qdisc->flags & TCQ_F_NOLOCK) {
  173. if (spin_trylock(&qdisc->seqlock))
  174. return true;
  175. /* No need to insist if the MISSED flag was already set.
  176. * Note that test_and_set_bit() also gives us memory ordering
  177. * guarantees wrt potential earlier enqueue() and below
  178. * spin_trylock(), both of which are necessary to prevent races
  179. */
  180. if (test_and_set_bit(__QDISC_STATE_MISSED, &qdisc->state))
  181. return false;
  182. /* Try to take the lock again to make sure that we will either
  183. * grab it or the CPU that still has it will see MISSED set
  184. * when testing it in qdisc_run_end()
  185. */
  186. return spin_trylock(&qdisc->seqlock);
  187. }
  188. if (READ_ONCE(qdisc->running))
  189. return false;
  190. WRITE_ONCE(qdisc->running, true);
  191. return true;
  192. }
  193. static inline struct sk_buff *qdisc_run_end(struct Qdisc *qdisc)
  194. {
  195. struct sk_buff *to_free = NULL;
  196. if (qdisc->flags & TCQ_F_NOLOCK) {
  197. spin_unlock(&qdisc->seqlock);
  198. /* spin_unlock() only has store-release semantic. The unlock
  199. * and test_bit() ordering is a store-load ordering, so a full
  200. * memory barrier is needed here.
  201. */
  202. smp_mb();
  203. if (unlikely(test_bit(__QDISC_STATE_MISSED,
  204. &qdisc->state)))
  205. __netif_schedule(qdisc);
  206. return NULL;
  207. }
  208. if (qdisc->flags & TCQ_F_DEQUEUE_DROPS) {
  209. to_free = qdisc->to_free;
  210. if (to_free)
  211. qdisc->to_free = NULL;
  212. }
  213. WRITE_ONCE(qdisc->running, false);
  214. return to_free;
  215. }
  216. static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
  217. {
  218. return qdisc->flags & TCQ_F_ONETXQUEUE;
  219. }
  220. static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
  221. {
  222. return netdev_queue_dql_avail(txq);
  223. }
  224. struct Qdisc_class_ops {
  225. unsigned int flags;
  226. /* Child qdisc manipulation */
  227. struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
  228. int (*graft)(struct Qdisc *, unsigned long cl,
  229. struct Qdisc *, struct Qdisc **,
  230. struct netlink_ext_ack *extack);
  231. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  232. void (*qlen_notify)(struct Qdisc *, unsigned long);
  233. /* Class manipulation routines */
  234. unsigned long (*find)(struct Qdisc *, u32 classid);
  235. int (*change)(struct Qdisc *, u32, u32,
  236. struct nlattr **, unsigned long *,
  237. struct netlink_ext_ack *);
  238. int (*delete)(struct Qdisc *, unsigned long,
  239. struct netlink_ext_ack *);
  240. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  241. /* Filter manipulation */
  242. struct tcf_block * (*tcf_block)(struct Qdisc *sch,
  243. unsigned long arg,
  244. struct netlink_ext_ack *extack);
  245. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  246. u32 classid);
  247. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  248. /* rtnetlink specific */
  249. int (*dump)(struct Qdisc *, unsigned long,
  250. struct sk_buff *skb, struct tcmsg*);
  251. int (*dump_stats)(struct Qdisc *, unsigned long,
  252. struct gnet_dump *);
  253. };
  254. /* Qdisc_class_ops flag values */
  255. /* Implements API that doesn't require rtnl lock */
  256. enum qdisc_class_ops_flags {
  257. QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
  258. };
  259. struct Qdisc_ops {
  260. struct Qdisc_ops *next;
  261. const struct Qdisc_class_ops *cl_ops;
  262. char id[IFNAMSIZ];
  263. int priv_size;
  264. unsigned int static_flags;
  265. int (*enqueue)(struct sk_buff *skb,
  266. struct Qdisc *sch,
  267. struct sk_buff **to_free);
  268. struct sk_buff * (*dequeue)(struct Qdisc *);
  269. struct sk_buff * (*peek)(struct Qdisc *);
  270. int (*init)(struct Qdisc *sch, struct nlattr *arg,
  271. struct netlink_ext_ack *extack);
  272. void (*reset)(struct Qdisc *);
  273. void (*destroy)(struct Qdisc *);
  274. int (*change)(struct Qdisc *sch,
  275. struct nlattr *arg,
  276. struct netlink_ext_ack *extack);
  277. void (*attach)(struct Qdisc *sch);
  278. int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
  279. void (*change_real_num_tx)(struct Qdisc *sch,
  280. unsigned int new_real_tx);
  281. int (*dump)(struct Qdisc *, struct sk_buff *);
  282. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  283. void (*ingress_block_set)(struct Qdisc *sch,
  284. u32 block_index);
  285. void (*egress_block_set)(struct Qdisc *sch,
  286. u32 block_index);
  287. u32 (*ingress_block_get)(struct Qdisc *sch);
  288. u32 (*egress_block_get)(struct Qdisc *sch);
  289. struct module *owner;
  290. };
  291. struct tcf_result {
  292. union {
  293. struct {
  294. unsigned long class;
  295. u32 classid;
  296. };
  297. const struct tcf_proto *goto_tp;
  298. };
  299. };
  300. struct tcf_chain;
  301. struct tcf_proto_ops {
  302. struct list_head head;
  303. char kind[IFNAMSIZ];
  304. int (*classify)(struct sk_buff *,
  305. const struct tcf_proto *,
  306. struct tcf_result *);
  307. int (*init)(struct tcf_proto*);
  308. void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
  309. struct netlink_ext_ack *extack);
  310. void* (*get)(struct tcf_proto*, u32 handle);
  311. void (*put)(struct tcf_proto *tp, void *f);
  312. int (*change)(struct net *net, struct sk_buff *,
  313. struct tcf_proto*, unsigned long,
  314. u32 handle, struct nlattr **,
  315. void **, u32,
  316. struct netlink_ext_ack *);
  317. int (*delete)(struct tcf_proto *tp, void *arg,
  318. bool *last, bool rtnl_held,
  319. struct netlink_ext_ack *);
  320. bool (*delete_empty)(struct tcf_proto *tp);
  321. void (*walk)(struct tcf_proto *tp,
  322. struct tcf_walker *arg, bool rtnl_held);
  323. int (*reoffload)(struct tcf_proto *tp, bool add,
  324. flow_setup_cb_t *cb, void *cb_priv,
  325. struct netlink_ext_ack *extack);
  326. void (*hw_add)(struct tcf_proto *tp,
  327. void *type_data);
  328. void (*hw_del)(struct tcf_proto *tp,
  329. void *type_data);
  330. void (*bind_class)(void *, u32, unsigned long,
  331. void *, unsigned long);
  332. void * (*tmplt_create)(struct net *net,
  333. struct tcf_chain *chain,
  334. struct nlattr **tca,
  335. struct netlink_ext_ack *extack);
  336. void (*tmplt_destroy)(void *tmplt_priv);
  337. void (*tmplt_reoffload)(struct tcf_chain *chain,
  338. bool add,
  339. flow_setup_cb_t *cb,
  340. void *cb_priv);
  341. struct tcf_exts * (*get_exts)(const struct tcf_proto *tp,
  342. u32 handle);
  343. /* rtnetlink specific */
  344. int (*dump)(struct net*, struct tcf_proto*, void *,
  345. struct sk_buff *skb, struct tcmsg*,
  346. bool);
  347. int (*terse_dump)(struct net *net,
  348. struct tcf_proto *tp, void *fh,
  349. struct sk_buff *skb,
  350. struct tcmsg *t, bool rtnl_held);
  351. int (*tmplt_dump)(struct sk_buff *skb,
  352. struct net *net,
  353. void *tmplt_priv);
  354. struct module *owner;
  355. int flags;
  356. };
  357. /* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
  358. * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
  359. * conditions can occur when filters are inserted/deleted simultaneously.
  360. */
  361. enum tcf_proto_ops_flags {
  362. TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
  363. };
  364. struct tcf_proto {
  365. /* Fast access part */
  366. struct tcf_proto __rcu *next;
  367. void __rcu *root;
  368. /* called under RCU BH lock*/
  369. int (*classify)(struct sk_buff *,
  370. const struct tcf_proto *,
  371. struct tcf_result *);
  372. __be16 protocol;
  373. /* All the rest */
  374. u32 prio;
  375. void *data;
  376. const struct tcf_proto_ops *ops;
  377. struct tcf_chain *chain;
  378. /* Lock protects tcf_proto shared state and can be used by unlocked
  379. * classifiers to protect their private data.
  380. */
  381. spinlock_t lock;
  382. bool deleting;
  383. bool counted;
  384. bool usesw;
  385. refcount_t refcnt;
  386. struct rcu_head rcu;
  387. struct hlist_node destroy_ht_node;
  388. };
  389. struct qdisc_skb_cb {
  390. unsigned int pkt_len;
  391. u16 pkt_segs;
  392. u16 tc_classid;
  393. #define QDISC_CB_PRIV_LEN 20
  394. unsigned char data[QDISC_CB_PRIV_LEN];
  395. u16 slave_dev_queue_mapping;
  396. u8 post_ct:1;
  397. u8 post_ct_snat:1;
  398. u8 post_ct_dnat:1;
  399. };
  400. typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
  401. struct tcf_chain {
  402. /* Protects filter_chain. */
  403. struct mutex filter_chain_lock;
  404. struct tcf_proto __rcu *filter_chain;
  405. struct list_head list;
  406. struct tcf_block *block;
  407. u32 index; /* chain index */
  408. unsigned int refcnt;
  409. unsigned int action_refcnt;
  410. bool explicitly_created;
  411. bool flushing;
  412. const struct tcf_proto_ops *tmplt_ops;
  413. void *tmplt_priv;
  414. struct rcu_head rcu;
  415. };
  416. struct tcf_block {
  417. struct xarray ports; /* datapath accessible */
  418. /* Lock protects tcf_block and lifetime-management data of chains
  419. * attached to the block (refcnt, action_refcnt, explicitly_created).
  420. */
  421. struct mutex lock;
  422. struct list_head chain_list;
  423. u32 index; /* block index for shared blocks */
  424. u32 classid; /* which class this block belongs to */
  425. refcount_t refcnt;
  426. struct net *net;
  427. struct Qdisc *q;
  428. struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
  429. struct flow_block flow_block;
  430. struct list_head owner_list;
  431. bool keep_dst;
  432. atomic_t useswcnt;
  433. atomic_t offloadcnt; /* Number of oddloaded filters */
  434. unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
  435. unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
  436. struct {
  437. struct tcf_chain *chain;
  438. struct list_head filter_chain_list;
  439. } chain0;
  440. struct rcu_head rcu;
  441. DECLARE_HASHTABLE(proto_destroy_ht, 7);
  442. struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
  443. };
  444. struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index);
  445. static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
  446. {
  447. return lockdep_is_held(&chain->filter_chain_lock);
  448. }
  449. static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
  450. {
  451. return lockdep_is_held(&tp->lock);
  452. }
  453. #define tcf_chain_dereference(p, chain) \
  454. rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
  455. #define tcf_proto_dereference(p, tp) \
  456. rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
  457. static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
  458. {
  459. struct qdisc_skb_cb *qcb;
  460. BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
  461. BUILD_BUG_ON(sizeof(qcb->data) < sz);
  462. }
  463. static inline int qdisc_qlen(const struct Qdisc *q)
  464. {
  465. return q->q.qlen;
  466. }
  467. static inline int qdisc_qlen_sum(const struct Qdisc *q)
  468. {
  469. __u32 qlen = q->qstats.qlen;
  470. int i;
  471. if (qdisc_is_percpu_stats(q)) {
  472. for_each_possible_cpu(i)
  473. qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
  474. } else {
  475. qlen += q->q.qlen;
  476. }
  477. return qlen;
  478. }
  479. static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
  480. {
  481. return (struct qdisc_skb_cb *)skb->cb;
  482. }
  483. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  484. {
  485. return &qdisc->q.lock;
  486. }
  487. static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
  488. {
  489. struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
  490. return q;
  491. }
  492. static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
  493. {
  494. return rcu_dereference_bh(qdisc->dev_queue->qdisc);
  495. }
  496. static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
  497. {
  498. return rcu_dereference_rtnl(qdisc->dev_queue->qdisc_sleeping);
  499. }
  500. static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
  501. {
  502. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  503. ASSERT_RTNL();
  504. return qdisc_lock(root);
  505. }
  506. static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
  507. {
  508. return qdisc->dev_queue->dev;
  509. }
  510. static inline void sch_tree_lock(struct Qdisc *q)
  511. {
  512. if (q->flags & TCQ_F_MQROOT)
  513. spin_lock_bh(qdisc_lock(q));
  514. else
  515. spin_lock_bh(qdisc_root_sleeping_lock(q));
  516. }
  517. static inline void sch_tree_unlock(struct Qdisc *q)
  518. {
  519. if (q->flags & TCQ_F_MQROOT)
  520. spin_unlock_bh(qdisc_lock(q));
  521. else
  522. spin_unlock_bh(qdisc_root_sleeping_lock(q));
  523. }
  524. extern struct Qdisc noop_qdisc;
  525. extern struct Qdisc_ops noop_qdisc_ops;
  526. extern struct Qdisc_ops pfifo_fast_ops;
  527. extern const u8 sch_default_prio2band[TC_PRIO_MAX + 1];
  528. extern struct Qdisc_ops mq_qdisc_ops;
  529. extern struct Qdisc_ops noqueue_qdisc_ops;
  530. extern const struct Qdisc_ops *default_qdisc_ops;
  531. static inline const struct Qdisc_ops *
  532. get_default_qdisc_ops(const struct net_device *dev, int ntx)
  533. {
  534. return ntx < dev->real_num_tx_queues ?
  535. default_qdisc_ops : &pfifo_fast_ops;
  536. }
  537. struct Qdisc_class_common {
  538. u32 classid;
  539. unsigned int filter_cnt;
  540. struct hlist_node hnode;
  541. };
  542. struct Qdisc_class_hash {
  543. struct hlist_head *hash;
  544. unsigned int hashsize;
  545. unsigned int hashmask;
  546. unsigned int hashelems;
  547. };
  548. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  549. {
  550. id ^= id >> 8;
  551. id ^= id >> 4;
  552. return id & mask;
  553. }
  554. static inline struct Qdisc_class_common *
  555. qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
  556. {
  557. struct Qdisc_class_common *cl;
  558. unsigned int h;
  559. if (!id)
  560. return NULL;
  561. h = qdisc_class_hash(id, hash->hashmask);
  562. hlist_for_each_entry(cl, &hash->hash[h], hnode) {
  563. if (cl->classid == id)
  564. return cl;
  565. }
  566. return NULL;
  567. }
  568. static inline bool qdisc_class_in_use(const struct Qdisc_class_common *cl)
  569. {
  570. return cl->filter_cnt > 0;
  571. }
  572. static inline void qdisc_class_get(struct Qdisc_class_common *cl)
  573. {
  574. unsigned int res;
  575. if (check_add_overflow(cl->filter_cnt, 1, &res))
  576. WARN(1, "Qdisc class overflow");
  577. cl->filter_cnt = res;
  578. }
  579. static inline void qdisc_class_put(struct Qdisc_class_common *cl)
  580. {
  581. unsigned int res;
  582. if (check_sub_overflow(cl->filter_cnt, 1, &res))
  583. WARN(1, "Qdisc class underflow");
  584. cl->filter_cnt = res;
  585. }
  586. static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
  587. {
  588. u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
  589. return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
  590. }
  591. int qdisc_class_hash_init(struct Qdisc_class_hash *);
  592. void qdisc_class_hash_insert(struct Qdisc_class_hash *,
  593. struct Qdisc_class_common *);
  594. void qdisc_class_hash_remove(struct Qdisc_class_hash *,
  595. struct Qdisc_class_common *);
  596. void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  597. void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  598. int dev_qdisc_change_tx_queue_len(struct net_device *dev);
  599. void dev_qdisc_change_real_num_tx(struct net_device *dev,
  600. unsigned int new_real_tx);
  601. void dev_init_scheduler(struct net_device *dev);
  602. void dev_shutdown(struct net_device *dev);
  603. void dev_activate(struct net_device *dev);
  604. void dev_deactivate(struct net_device *dev);
  605. void dev_deactivate_many(struct list_head *head);
  606. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  607. struct Qdisc *qdisc);
  608. void qdisc_reset(struct Qdisc *qdisc);
  609. void qdisc_destroy(struct Qdisc *qdisc);
  610. void qdisc_put(struct Qdisc *qdisc);
  611. void qdisc_put_unlocked(struct Qdisc *qdisc);
  612. void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
  613. static inline void dev_reset_queue(struct net_device *dev,
  614. struct netdev_queue *dev_queue,
  615. void *_unused)
  616. {
  617. struct Qdisc *qdisc;
  618. bool nolock;
  619. qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
  620. if (!qdisc)
  621. return;
  622. nolock = qdisc->flags & TCQ_F_NOLOCK;
  623. if (nolock)
  624. spin_lock_bh(&qdisc->seqlock);
  625. spin_lock_bh(qdisc_lock(qdisc));
  626. qdisc_reset(qdisc);
  627. spin_unlock_bh(qdisc_lock(qdisc));
  628. if (nolock) {
  629. clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
  630. clear_bit(__QDISC_STATE_DRAINING, &qdisc->state);
  631. spin_unlock_bh(&qdisc->seqlock);
  632. }
  633. }
  634. #ifdef CONFIG_NET_SCHED
  635. int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
  636. void *type_data);
  637. void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
  638. struct Qdisc *new, struct Qdisc *old,
  639. enum tc_setup_type type, void *type_data,
  640. struct netlink_ext_ack *extack);
  641. #else
  642. static inline int
  643. qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
  644. void *type_data)
  645. {
  646. q->flags &= ~TCQ_F_OFFLOADED;
  647. return 0;
  648. }
  649. static inline void
  650. qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
  651. struct Qdisc *new, struct Qdisc *old,
  652. enum tc_setup_type type, void *type_data,
  653. struct netlink_ext_ack *extack)
  654. {
  655. }
  656. #endif
  657. void qdisc_offload_query_caps(struct net_device *dev,
  658. enum tc_setup_type type,
  659. void *caps, size_t caps_len);
  660. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  661. const struct Qdisc_ops *ops,
  662. struct netlink_ext_ack *extack);
  663. void qdisc_free(struct Qdisc *qdisc);
  664. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  665. const struct Qdisc_ops *ops, u32 parentid,
  666. struct netlink_ext_ack *extack);
  667. void __qdisc_calculate_pkt_len(struct sk_buff *skb,
  668. const struct qdisc_size_table *stab);
  669. int skb_do_redirect(struct sk_buff *);
  670. static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
  671. {
  672. #ifdef CONFIG_NET_XGRESS
  673. return skb->tc_at_ingress;
  674. #else
  675. return false;
  676. #endif
  677. }
  678. static inline bool skb_skip_tc_classify(struct sk_buff *skb)
  679. {
  680. #ifdef CONFIG_NET_CLS_ACT
  681. if (skb->tc_skip_classify) {
  682. skb->tc_skip_classify = 0;
  683. return true;
  684. }
  685. #endif
  686. return false;
  687. }
  688. /* Reset all TX qdiscs greater than index of a device. */
  689. static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
  690. {
  691. struct Qdisc *qdisc;
  692. bool nolock;
  693. for (; i < dev->num_tx_queues; i++) {
  694. qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
  695. if (qdisc) {
  696. nolock = qdisc->flags & TCQ_F_NOLOCK;
  697. if (nolock)
  698. spin_lock_bh(&qdisc->seqlock);
  699. spin_lock_bh(qdisc_lock(qdisc));
  700. qdisc_reset(qdisc);
  701. spin_unlock_bh(qdisc_lock(qdisc));
  702. if (nolock) {
  703. clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
  704. clear_bit(__QDISC_STATE_DRAINING, &qdisc->state);
  705. spin_unlock_bh(&qdisc->seqlock);
  706. }
  707. }
  708. }
  709. }
  710. /* Are all TX queues of the device empty? */
  711. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  712. {
  713. unsigned int i;
  714. rcu_read_lock();
  715. for (i = 0; i < dev->num_tx_queues; i++) {
  716. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  717. const struct Qdisc *q = rcu_dereference(txq->qdisc);
  718. if (!qdisc_is_empty(q)) {
  719. rcu_read_unlock();
  720. return false;
  721. }
  722. }
  723. rcu_read_unlock();
  724. return true;
  725. }
  726. /* Are any of the TX qdiscs changing? */
  727. static inline bool qdisc_tx_changing(const struct net_device *dev)
  728. {
  729. unsigned int i;
  730. for (i = 0; i < dev->num_tx_queues; i++) {
  731. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  732. if (rcu_access_pointer(txq->qdisc) !=
  733. rcu_access_pointer(txq->qdisc_sleeping))
  734. return true;
  735. }
  736. return false;
  737. }
  738. /* "noqueue" qdisc identified by not having any enqueue, see noqueue_init() */
  739. static inline bool qdisc_txq_has_no_queue(const struct netdev_queue *txq)
  740. {
  741. struct Qdisc *qdisc = rcu_access_pointer(txq->qdisc);
  742. return qdisc->enqueue == NULL;
  743. }
  744. /* Is the device using the noop qdisc on all queues? */
  745. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  746. {
  747. unsigned int i;
  748. for (i = 0; i < dev->num_tx_queues; i++) {
  749. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  750. if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
  751. return false;
  752. }
  753. return true;
  754. }
  755. static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
  756. {
  757. return qdisc_skb_cb(skb)->pkt_len;
  758. }
  759. static inline unsigned int qdisc_pkt_segs(const struct sk_buff *skb)
  760. {
  761. u32 pkt_segs = qdisc_skb_cb(skb)->pkt_segs;
  762. DEBUG_NET_WARN_ON_ONCE(pkt_segs !=
  763. (skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1));
  764. return pkt_segs;
  765. }
  766. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  767. enum net_xmit_qdisc_t {
  768. __NET_XMIT_STOLEN = 0x00010000,
  769. __NET_XMIT_BYPASS = 0x00020000,
  770. };
  771. #ifdef CONFIG_NET_CLS_ACT
  772. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  773. #else
  774. #define net_xmit_drop_count(e) (1)
  775. #endif
  776. static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
  777. const struct Qdisc *sch)
  778. {
  779. #ifdef CONFIG_NET_SCHED
  780. struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
  781. if (stab)
  782. __qdisc_calculate_pkt_len(skb, stab);
  783. #endif
  784. }
  785. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  786. struct sk_buff **to_free)
  787. {
  788. return sch->enqueue(skb, sch, to_free);
  789. }
  790. static inline void _bstats_update(struct gnet_stats_basic_sync *bstats,
  791. __u64 bytes, __u64 packets)
  792. {
  793. u64_stats_update_begin(&bstats->syncp);
  794. u64_stats_add(&bstats->bytes, bytes);
  795. u64_stats_add(&bstats->packets, packets);
  796. u64_stats_update_end(&bstats->syncp);
  797. }
  798. static inline void bstats_update(struct gnet_stats_basic_sync *bstats,
  799. const struct sk_buff *skb)
  800. {
  801. _bstats_update(bstats, qdisc_pkt_len(skb), qdisc_pkt_segs(skb));
  802. }
  803. static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
  804. const struct sk_buff *skb)
  805. {
  806. bstats_update(this_cpu_ptr(sch->cpu_bstats), skb);
  807. }
  808. static inline void qdisc_bstats_update(struct Qdisc *sch,
  809. const struct sk_buff *skb)
  810. {
  811. bstats_update(&sch->bstats, skb);
  812. }
  813. static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
  814. const struct sk_buff *skb)
  815. {
  816. sch->qstats.backlog -= qdisc_pkt_len(skb);
  817. }
  818. static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
  819. const struct sk_buff *skb)
  820. {
  821. this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
  822. }
  823. static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
  824. const struct sk_buff *skb)
  825. {
  826. sch->qstats.backlog += qdisc_pkt_len(skb);
  827. }
  828. static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
  829. const struct sk_buff *skb)
  830. {
  831. this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
  832. }
  833. static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
  834. {
  835. this_cpu_inc(sch->cpu_qstats->qlen);
  836. }
  837. static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
  838. {
  839. this_cpu_dec(sch->cpu_qstats->qlen);
  840. }
  841. static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
  842. {
  843. this_cpu_inc(sch->cpu_qstats->requeues);
  844. }
  845. static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
  846. {
  847. sch->qstats.drops += count;
  848. }
  849. static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
  850. {
  851. qstats->drops++;
  852. }
  853. static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
  854. {
  855. qstats->overlimits++;
  856. }
  857. static inline void qdisc_qstats_drop(struct Qdisc *sch)
  858. {
  859. qstats_drop_inc(&sch->qstats);
  860. }
  861. static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
  862. {
  863. this_cpu_inc(sch->cpu_qstats->drops);
  864. }
  865. static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
  866. {
  867. sch->qstats.overlimits++;
  868. }
  869. static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
  870. {
  871. __u32 qlen = qdisc_qlen_sum(sch);
  872. return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
  873. }
  874. static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
  875. __u32 *backlog)
  876. {
  877. struct gnet_stats_queue qstats = { 0 };
  878. gnet_stats_add_queue(&qstats, sch->cpu_qstats, &sch->qstats);
  879. *qlen = qstats.qlen + qdisc_qlen(sch);
  880. *backlog = qstats.backlog;
  881. }
  882. static inline void qdisc_purge_queue(struct Qdisc *sch)
  883. {
  884. __u32 qlen, backlog;
  885. qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
  886. qdisc_reset(sch);
  887. qdisc_tree_reduce_backlog(sch, qlen, backlog);
  888. }
  889. static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
  890. struct qdisc_skb_head *qh)
  891. {
  892. struct sk_buff *last = qh->tail;
  893. if (last) {
  894. skb->next = NULL;
  895. last->next = skb;
  896. qh->tail = skb;
  897. } else {
  898. qh->tail = skb;
  899. qh->head = skb;
  900. }
  901. qh->qlen++;
  902. }
  903. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  904. {
  905. __qdisc_enqueue_tail(skb, &sch->q);
  906. qdisc_qstats_backlog_inc(sch, skb);
  907. return NET_XMIT_SUCCESS;
  908. }
  909. static inline void __qdisc_enqueue_head(struct sk_buff *skb,
  910. struct qdisc_skb_head *qh)
  911. {
  912. skb->next = qh->head;
  913. if (!qh->head)
  914. qh->tail = skb;
  915. qh->head = skb;
  916. qh->qlen++;
  917. }
  918. static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
  919. {
  920. struct sk_buff *skb = qh->head;
  921. if (likely(skb != NULL)) {
  922. qh->head = skb->next;
  923. qh->qlen--;
  924. if (qh->head == NULL)
  925. qh->tail = NULL;
  926. skb->next = NULL;
  927. }
  928. return skb;
  929. }
  930. static inline struct sk_buff *qdisc_dequeue_internal(struct Qdisc *sch, bool direct)
  931. {
  932. struct sk_buff *skb;
  933. skb = __skb_dequeue(&sch->gso_skb);
  934. if (skb) {
  935. sch->q.qlen--;
  936. qdisc_qstats_backlog_dec(sch, skb);
  937. return skb;
  938. }
  939. if (direct) {
  940. skb = __qdisc_dequeue_head(&sch->q);
  941. if (skb)
  942. qdisc_qstats_backlog_dec(sch, skb);
  943. return skb;
  944. } else {
  945. return sch->dequeue(sch);
  946. }
  947. }
  948. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  949. {
  950. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  951. if (likely(skb != NULL)) {
  952. qdisc_qstats_backlog_dec(sch, skb);
  953. qdisc_bstats_update(sch, skb);
  954. }
  955. return skb;
  956. }
  957. struct tc_skb_cb {
  958. struct qdisc_skb_cb qdisc_cb;
  959. u32 drop_reason;
  960. u16 zone; /* Only valid if qdisc_skb_cb(skb)->post_ct = true */
  961. u16 mru;
  962. };
  963. static inline struct tc_skb_cb *tc_skb_cb(const struct sk_buff *skb)
  964. {
  965. struct tc_skb_cb *cb = (struct tc_skb_cb *)skb->cb;
  966. BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb));
  967. return cb;
  968. }
  969. static inline enum skb_drop_reason
  970. tcf_get_drop_reason(const struct sk_buff *skb)
  971. {
  972. return tc_skb_cb(skb)->drop_reason;
  973. }
  974. static inline void tcf_set_drop_reason(const struct sk_buff *skb,
  975. enum skb_drop_reason reason)
  976. {
  977. tc_skb_cb(skb)->drop_reason = reason;
  978. }
  979. static inline void tcf_kfree_skb_list(struct sk_buff *skb)
  980. {
  981. while (unlikely(skb)) {
  982. struct sk_buff *next = skb->next;
  983. prefetch(next);
  984. kfree_skb_reason(skb, tcf_get_drop_reason(skb));
  985. skb = next;
  986. }
  987. }
  988. static inline void qdisc_dequeue_drop(struct Qdisc *q, struct sk_buff *skb,
  989. enum skb_drop_reason reason)
  990. {
  991. DEBUG_NET_WARN_ON_ONCE(!(q->flags & TCQ_F_DEQUEUE_DROPS));
  992. DEBUG_NET_WARN_ON_ONCE(q->flags & TCQ_F_NOLOCK);
  993. tcf_set_drop_reason(skb, reason);
  994. skb->next = q->to_free;
  995. q->to_free = skb;
  996. }
  997. /* Instead of calling kfree_skb() while root qdisc lock is held,
  998. * queue the skb for future freeing at end of __dev_xmit_skb()
  999. */
  1000. static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
  1001. {
  1002. skb->next = *to_free;
  1003. *to_free = skb;
  1004. }
  1005. static inline void __qdisc_drop_all(struct sk_buff *skb,
  1006. struct sk_buff **to_free)
  1007. {
  1008. if (skb->prev)
  1009. skb->prev->next = *to_free;
  1010. else
  1011. skb->next = *to_free;
  1012. *to_free = skb;
  1013. }
  1014. static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
  1015. struct qdisc_skb_head *qh,
  1016. struct sk_buff **to_free)
  1017. {
  1018. struct sk_buff *skb = __qdisc_dequeue_head(qh);
  1019. if (likely(skb != NULL)) {
  1020. unsigned int len = qdisc_pkt_len(skb);
  1021. qdisc_qstats_backlog_dec(sch, skb);
  1022. __qdisc_drop(skb, to_free);
  1023. return len;
  1024. }
  1025. return 0;
  1026. }
  1027. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  1028. {
  1029. const struct qdisc_skb_head *qh = &sch->q;
  1030. return qh->head;
  1031. }
  1032. /* generic pseudo peek method for non-work-conserving qdisc */
  1033. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  1034. {
  1035. struct sk_buff *skb = skb_peek(&sch->gso_skb);
  1036. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  1037. if (!skb) {
  1038. skb = sch->dequeue(sch);
  1039. if (skb) {
  1040. __skb_queue_head(&sch->gso_skb, skb);
  1041. /* it's still part of the queue */
  1042. qdisc_qstats_backlog_inc(sch, skb);
  1043. sch->q.qlen++;
  1044. }
  1045. }
  1046. return skb;
  1047. }
  1048. static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
  1049. struct sk_buff *skb)
  1050. {
  1051. if (qdisc_is_percpu_stats(sch)) {
  1052. qdisc_qstats_cpu_backlog_dec(sch, skb);
  1053. qdisc_bstats_cpu_update(sch, skb);
  1054. qdisc_qstats_cpu_qlen_dec(sch);
  1055. } else {
  1056. qdisc_qstats_backlog_dec(sch, skb);
  1057. qdisc_bstats_update(sch, skb);
  1058. sch->q.qlen--;
  1059. }
  1060. }
  1061. static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
  1062. unsigned int pkt_len)
  1063. {
  1064. if (qdisc_is_percpu_stats(sch)) {
  1065. qdisc_qstats_cpu_qlen_inc(sch);
  1066. this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
  1067. } else {
  1068. sch->qstats.backlog += pkt_len;
  1069. sch->q.qlen++;
  1070. }
  1071. }
  1072. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  1073. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  1074. {
  1075. struct sk_buff *skb = skb_peek(&sch->gso_skb);
  1076. if (skb) {
  1077. skb = __skb_dequeue(&sch->gso_skb);
  1078. if (qdisc_is_percpu_stats(sch)) {
  1079. qdisc_qstats_cpu_backlog_dec(sch, skb);
  1080. qdisc_qstats_cpu_qlen_dec(sch);
  1081. } else {
  1082. qdisc_qstats_backlog_dec(sch, skb);
  1083. sch->q.qlen--;
  1084. }
  1085. } else {
  1086. skb = sch->dequeue(sch);
  1087. }
  1088. return skb;
  1089. }
  1090. static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
  1091. {
  1092. /*
  1093. * We do not know the backlog in bytes of this list, it
  1094. * is up to the caller to correct it
  1095. */
  1096. ASSERT_RTNL();
  1097. if (qh->qlen) {
  1098. rtnl_kfree_skbs(qh->head, qh->tail);
  1099. qh->head = NULL;
  1100. qh->tail = NULL;
  1101. qh->qlen = 0;
  1102. }
  1103. }
  1104. static inline void qdisc_reset_queue(struct Qdisc *sch)
  1105. {
  1106. __qdisc_reset_queue(&sch->q);
  1107. }
  1108. static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
  1109. struct Qdisc **pold)
  1110. {
  1111. struct Qdisc *old;
  1112. sch_tree_lock(sch);
  1113. old = *pold;
  1114. *pold = new;
  1115. if (old != NULL)
  1116. qdisc_purge_queue(old);
  1117. sch_tree_unlock(sch);
  1118. return old;
  1119. }
  1120. static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  1121. {
  1122. rtnl_kfree_skbs(skb, skb);
  1123. qdisc_qstats_drop(sch);
  1124. }
  1125. static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
  1126. struct sk_buff **to_free)
  1127. {
  1128. __qdisc_drop(skb, to_free);
  1129. qdisc_qstats_cpu_drop(sch);
  1130. return NET_XMIT_DROP;
  1131. }
  1132. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
  1133. struct sk_buff **to_free)
  1134. {
  1135. __qdisc_drop(skb, to_free);
  1136. qdisc_qstats_drop(sch);
  1137. return NET_XMIT_DROP;
  1138. }
  1139. static inline int qdisc_drop_reason(struct sk_buff *skb, struct Qdisc *sch,
  1140. struct sk_buff **to_free,
  1141. enum skb_drop_reason reason)
  1142. {
  1143. tcf_set_drop_reason(skb, reason);
  1144. return qdisc_drop(skb, sch, to_free);
  1145. }
  1146. static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
  1147. struct sk_buff **to_free)
  1148. {
  1149. __qdisc_drop_all(skb, to_free);
  1150. qdisc_qstats_drop(sch);
  1151. return NET_XMIT_DROP;
  1152. }
  1153. struct psched_ratecfg {
  1154. u64 rate_bytes_ps; /* bytes per second */
  1155. u32 mult;
  1156. u16 overhead;
  1157. u16 mpu;
  1158. u8 linklayer;
  1159. u8 shift;
  1160. };
  1161. static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
  1162. unsigned int len)
  1163. {
  1164. len += r->overhead;
  1165. if (len < r->mpu)
  1166. len = r->mpu;
  1167. if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
  1168. return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
  1169. return ((u64)len * r->mult) >> r->shift;
  1170. }
  1171. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  1172. const struct tc_ratespec *conf,
  1173. u64 rate64);
  1174. static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
  1175. const struct psched_ratecfg *r)
  1176. {
  1177. memset(res, 0, sizeof(*res));
  1178. /* legacy struct tc_ratespec has a 32bit @rate field
  1179. * Qdisc using 64bit rate should add new attributes
  1180. * in order to maintain compatibility.
  1181. */
  1182. res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
  1183. res->overhead = r->overhead;
  1184. res->mpu = r->mpu;
  1185. res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
  1186. }
  1187. struct psched_pktrate {
  1188. u64 rate_pkts_ps; /* packets per second */
  1189. u32 mult;
  1190. u8 shift;
  1191. };
  1192. static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r,
  1193. unsigned int pkt_num)
  1194. {
  1195. return ((u64)pkt_num * r->mult) >> r->shift;
  1196. }
  1197. void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64);
  1198. /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
  1199. * The fast path only needs to access filter list and to update stats
  1200. */
  1201. struct mini_Qdisc {
  1202. struct tcf_proto *filter_list;
  1203. struct tcf_block *block;
  1204. struct gnet_stats_basic_sync __percpu *cpu_bstats;
  1205. struct gnet_stats_queue __percpu *cpu_qstats;
  1206. unsigned long rcu_state;
  1207. };
  1208. static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
  1209. const struct sk_buff *skb)
  1210. {
  1211. bstats_update(this_cpu_ptr(miniq->cpu_bstats), skb);
  1212. }
  1213. static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
  1214. {
  1215. this_cpu_inc(miniq->cpu_qstats->drops);
  1216. }
  1217. struct mini_Qdisc_pair {
  1218. struct mini_Qdisc miniq1;
  1219. struct mini_Qdisc miniq2;
  1220. struct mini_Qdisc __rcu **p_miniq;
  1221. };
  1222. void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
  1223. struct tcf_proto *tp_head);
  1224. void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
  1225. struct mini_Qdisc __rcu **p_miniq);
  1226. void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
  1227. struct tcf_block *block);
  1228. static inline bool mini_qdisc_pair_inited(struct mini_Qdisc_pair *miniqp)
  1229. {
  1230. return !!miniqp->p_miniq;
  1231. }
  1232. void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx);
  1233. int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
  1234. /* Make sure qdisc is no longer in SCHED state. */
  1235. static inline void qdisc_synchronize(const struct Qdisc *q)
  1236. {
  1237. while (test_bit(__QDISC_STATE_SCHED, &q->state))
  1238. msleep(1);
  1239. }
  1240. #endif