sch_generic.h 34 KB

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