sch_generic.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 <net/gen_stats.h>
  15. #include <net/rtnetlink.h>
  16. struct Qdisc_ops;
  17. struct qdisc_walker;
  18. struct tcf_walker;
  19. struct module;
  20. typedef int tc_setup_cb_t(enum tc_setup_type type,
  21. void *type_data, void *cb_priv);
  22. struct qdisc_rate_table {
  23. struct tc_ratespec rate;
  24. u32 data[256];
  25. struct qdisc_rate_table *next;
  26. int refcnt;
  27. };
  28. enum qdisc_state_t {
  29. __QDISC_STATE_SCHED,
  30. __QDISC_STATE_DEACTIVATED,
  31. };
  32. struct qdisc_size_table {
  33. struct rcu_head rcu;
  34. struct list_head list;
  35. struct tc_sizespec szopts;
  36. int refcnt;
  37. u16 data[];
  38. };
  39. /* similar to sk_buff_head, but skb->prev pointer is undefined. */
  40. struct qdisc_skb_head {
  41. struct sk_buff *head;
  42. struct sk_buff *tail;
  43. union {
  44. u32 qlen;
  45. atomic_t atomic_qlen;
  46. };
  47. spinlock_t lock;
  48. };
  49. struct Qdisc {
  50. int (*enqueue)(struct sk_buff *skb,
  51. struct Qdisc *sch,
  52. struct sk_buff **to_free);
  53. struct sk_buff * (*dequeue)(struct Qdisc *sch);
  54. unsigned int flags;
  55. #define TCQ_F_BUILTIN 1
  56. #define TCQ_F_INGRESS 2
  57. #define TCQ_F_CAN_BYPASS 4
  58. #define TCQ_F_MQROOT 8
  59. #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
  60. * q->dev_queue : It can test
  61. * netif_xmit_frozen_or_stopped() before
  62. * dequeueing next packet.
  63. * Its true for MQ/MQPRIO slaves, or non
  64. * multiqueue device.
  65. */
  66. #define TCQ_F_WARN_NONWC (1 << 16)
  67. #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
  68. #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
  69. * qdisc_tree_decrease_qlen() should stop.
  70. */
  71. #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
  72. #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
  73. #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
  74. u32 limit;
  75. const struct Qdisc_ops *ops;
  76. struct qdisc_size_table __rcu *stab;
  77. struct hlist_node hash;
  78. u32 handle;
  79. u32 parent;
  80. struct netdev_queue *dev_queue;
  81. struct net_rate_estimator __rcu *rate_est;
  82. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  83. struct gnet_stats_queue __percpu *cpu_qstats;
  84. int padded;
  85. refcount_t refcnt;
  86. /*
  87. * For performance sake on SMP, we put highly modified fields at the end
  88. */
  89. struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
  90. struct qdisc_skb_head q;
  91. struct gnet_stats_basic_packed bstats;
  92. seqcount_t running;
  93. struct gnet_stats_queue qstats;
  94. unsigned long state;
  95. struct Qdisc *next_sched;
  96. struct sk_buff_head skb_bad_txq;
  97. spinlock_t busylock ____cacheline_aligned_in_smp;
  98. spinlock_t seqlock;
  99. };
  100. static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
  101. {
  102. if (qdisc->flags & TCQ_F_BUILTIN)
  103. return;
  104. refcount_inc(&qdisc->refcnt);
  105. }
  106. static inline bool qdisc_is_running(struct Qdisc *qdisc)
  107. {
  108. if (qdisc->flags & TCQ_F_NOLOCK)
  109. return spin_is_locked(&qdisc->seqlock);
  110. return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
  111. }
  112. static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  113. {
  114. if (qdisc->flags & TCQ_F_NOLOCK) {
  115. if (!spin_trylock(&qdisc->seqlock))
  116. return false;
  117. } else if (qdisc_is_running(qdisc)) {
  118. return false;
  119. }
  120. /* Variant of write_seqcount_begin() telling lockdep a trylock
  121. * was attempted.
  122. */
  123. raw_write_seqcount_begin(&qdisc->running);
  124. seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
  125. return true;
  126. }
  127. static inline void qdisc_run_end(struct Qdisc *qdisc)
  128. {
  129. write_seqcount_end(&qdisc->running);
  130. if (qdisc->flags & TCQ_F_NOLOCK)
  131. spin_unlock(&qdisc->seqlock);
  132. }
  133. static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
  134. {
  135. return qdisc->flags & TCQ_F_ONETXQUEUE;
  136. }
  137. static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
  138. {
  139. #ifdef CONFIG_BQL
  140. /* Non-BQL migrated drivers will return 0, too. */
  141. return dql_avail(&txq->dql);
  142. #else
  143. return 0;
  144. #endif
  145. }
  146. struct Qdisc_class_ops {
  147. /* Child qdisc manipulation */
  148. struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
  149. int (*graft)(struct Qdisc *, unsigned long cl,
  150. struct Qdisc *, struct Qdisc **,
  151. struct netlink_ext_ack *extack);
  152. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  153. void (*qlen_notify)(struct Qdisc *, unsigned long);
  154. /* Class manipulation routines */
  155. unsigned long (*find)(struct Qdisc *, u32 classid);
  156. int (*change)(struct Qdisc *, u32, u32,
  157. struct nlattr **, unsigned long *,
  158. struct netlink_ext_ack *);
  159. int (*delete)(struct Qdisc *, unsigned long);
  160. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  161. /* Filter manipulation */
  162. struct tcf_block * (*tcf_block)(struct Qdisc *sch,
  163. unsigned long arg,
  164. struct netlink_ext_ack *extack);
  165. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  166. u32 classid);
  167. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  168. /* rtnetlink specific */
  169. int (*dump)(struct Qdisc *, unsigned long,
  170. struct sk_buff *skb, struct tcmsg*);
  171. int (*dump_stats)(struct Qdisc *, unsigned long,
  172. struct gnet_dump *);
  173. };
  174. struct Qdisc_ops {
  175. struct Qdisc_ops *next;
  176. const struct Qdisc_class_ops *cl_ops;
  177. char id[IFNAMSIZ];
  178. int priv_size;
  179. unsigned int static_flags;
  180. int (*enqueue)(struct sk_buff *skb,
  181. struct Qdisc *sch,
  182. struct sk_buff **to_free);
  183. struct sk_buff * (*dequeue)(struct Qdisc *);
  184. struct sk_buff * (*peek)(struct Qdisc *);
  185. int (*init)(struct Qdisc *sch, struct nlattr *arg,
  186. struct netlink_ext_ack *extack);
  187. void (*reset)(struct Qdisc *);
  188. void (*destroy)(struct Qdisc *);
  189. int (*change)(struct Qdisc *sch,
  190. struct nlattr *arg,
  191. struct netlink_ext_ack *extack);
  192. void (*attach)(struct Qdisc *sch);
  193. int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
  194. int (*dump)(struct Qdisc *, struct sk_buff *);
  195. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  196. void (*ingress_block_set)(struct Qdisc *sch,
  197. u32 block_index);
  198. void (*egress_block_set)(struct Qdisc *sch,
  199. u32 block_index);
  200. u32 (*ingress_block_get)(struct Qdisc *sch);
  201. u32 (*egress_block_get)(struct Qdisc *sch);
  202. struct module *owner;
  203. };
  204. struct tcf_result {
  205. union {
  206. struct {
  207. unsigned long class;
  208. u32 classid;
  209. };
  210. const struct tcf_proto *goto_tp;
  211. /* used by the TC_ACT_REINSERT action */
  212. struct {
  213. bool ingress;
  214. struct gnet_stats_queue *qstats;
  215. };
  216. };
  217. };
  218. struct tcf_chain;
  219. struct tcf_proto_ops {
  220. struct list_head head;
  221. char kind[IFNAMSIZ];
  222. int (*classify)(struct sk_buff *,
  223. const struct tcf_proto *,
  224. struct tcf_result *);
  225. int (*init)(struct tcf_proto*);
  226. void (*destroy)(struct tcf_proto *tp,
  227. struct netlink_ext_ack *extack);
  228. void* (*get)(struct tcf_proto*, u32 handle);
  229. int (*change)(struct net *net, struct sk_buff *,
  230. struct tcf_proto*, unsigned long,
  231. u32 handle, struct nlattr **,
  232. void **, bool,
  233. struct netlink_ext_ack *);
  234. int (*delete)(struct tcf_proto *tp, void *arg,
  235. bool *last,
  236. struct netlink_ext_ack *);
  237. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  238. int (*reoffload)(struct tcf_proto *tp, bool add,
  239. tc_setup_cb_t *cb, void *cb_priv,
  240. struct netlink_ext_ack *extack);
  241. void (*bind_class)(void *, u32, unsigned long,
  242. void *, unsigned long);
  243. void * (*tmplt_create)(struct net *net,
  244. struct tcf_chain *chain,
  245. struct nlattr **tca,
  246. struct netlink_ext_ack *extack);
  247. void (*tmplt_destroy)(void *tmplt_priv);
  248. /* rtnetlink specific */
  249. int (*dump)(struct net*, struct tcf_proto*, void *,
  250. struct sk_buff *skb, struct tcmsg*);
  251. int (*tmplt_dump)(struct sk_buff *skb,
  252. struct net *net,
  253. void *tmplt_priv);
  254. struct module *owner;
  255. };
  256. struct tcf_proto {
  257. /* Fast access part */
  258. struct tcf_proto __rcu *next;
  259. void __rcu *root;
  260. /* called under RCU BH lock*/
  261. int (*classify)(struct sk_buff *,
  262. const struct tcf_proto *,
  263. struct tcf_result *);
  264. __be16 protocol;
  265. /* All the rest */
  266. u32 prio;
  267. void *data;
  268. const struct tcf_proto_ops *ops;
  269. struct tcf_chain *chain;
  270. struct rcu_head rcu;
  271. };
  272. struct qdisc_skb_cb {
  273. unsigned int pkt_len;
  274. u16 slave_dev_queue_mapping;
  275. u16 tc_classid;
  276. #define QDISC_CB_PRIV_LEN 20
  277. unsigned char data[QDISC_CB_PRIV_LEN];
  278. };
  279. typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
  280. struct tcf_chain {
  281. struct tcf_proto __rcu *filter_chain;
  282. struct list_head list;
  283. struct tcf_block *block;
  284. u32 index; /* chain index */
  285. unsigned int refcnt;
  286. unsigned int action_refcnt;
  287. bool explicitly_created;
  288. const struct tcf_proto_ops *tmplt_ops;
  289. void *tmplt_priv;
  290. };
  291. struct tcf_block {
  292. struct list_head chain_list;
  293. u32 index; /* block index for shared blocks */
  294. unsigned int refcnt;
  295. struct net *net;
  296. struct Qdisc *q;
  297. struct list_head cb_list;
  298. struct list_head owner_list;
  299. bool keep_dst;
  300. unsigned int offloadcnt; /* Number of oddloaded filters */
  301. unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
  302. struct {
  303. struct tcf_chain *chain;
  304. struct list_head filter_chain_list;
  305. } chain0;
  306. };
  307. static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
  308. {
  309. if (*flags & TCA_CLS_FLAGS_IN_HW)
  310. return;
  311. *flags |= TCA_CLS_FLAGS_IN_HW;
  312. block->offloadcnt++;
  313. }
  314. static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
  315. {
  316. if (!(*flags & TCA_CLS_FLAGS_IN_HW))
  317. return;
  318. *flags &= ~TCA_CLS_FLAGS_IN_HW;
  319. block->offloadcnt--;
  320. }
  321. static inline void
  322. tc_cls_offload_cnt_update(struct tcf_block *block, unsigned int *cnt,
  323. u32 *flags, bool add)
  324. {
  325. if (add) {
  326. if (!*cnt)
  327. tcf_block_offload_inc(block, flags);
  328. (*cnt)++;
  329. } else {
  330. (*cnt)--;
  331. if (!*cnt)
  332. tcf_block_offload_dec(block, flags);
  333. }
  334. }
  335. static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
  336. {
  337. struct qdisc_skb_cb *qcb;
  338. BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
  339. BUILD_BUG_ON(sizeof(qcb->data) < sz);
  340. }
  341. static inline int qdisc_qlen(const struct Qdisc *q)
  342. {
  343. return q->q.qlen;
  344. }
  345. static inline u32 qdisc_qlen_sum(const struct Qdisc *q)
  346. {
  347. u32 qlen = q->qstats.qlen;
  348. if (q->flags & TCQ_F_NOLOCK)
  349. qlen += atomic_read(&q->q.atomic_qlen);
  350. else
  351. qlen += q->q.qlen;
  352. return qlen;
  353. }
  354. static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
  355. {
  356. return (struct qdisc_skb_cb *)skb->cb;
  357. }
  358. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  359. {
  360. return &qdisc->q.lock;
  361. }
  362. static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
  363. {
  364. struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
  365. return q;
  366. }
  367. static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
  368. {
  369. return rcu_dereference_bh(qdisc->dev_queue->qdisc);
  370. }
  371. static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
  372. {
  373. return qdisc->dev_queue->qdisc_sleeping;
  374. }
  375. /* The qdisc root lock is a mechanism by which to top level
  376. * of a qdisc tree can be locked from any qdisc node in the
  377. * forest. This allows changing the configuration of some
  378. * aspect of the qdisc tree while blocking out asynchronous
  379. * qdisc access in the packet processing paths.
  380. *
  381. * It is only legal to do this when the root will not change
  382. * on us. Otherwise we'll potentially lock the wrong qdisc
  383. * root. This is enforced by holding the RTNL semaphore, which
  384. * all users of this lock accessor must do.
  385. */
  386. static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
  387. {
  388. struct Qdisc *root = qdisc_root(qdisc);
  389. ASSERT_RTNL();
  390. return qdisc_lock(root);
  391. }
  392. static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
  393. {
  394. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  395. ASSERT_RTNL();
  396. return qdisc_lock(root);
  397. }
  398. static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  399. {
  400. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  401. ASSERT_RTNL();
  402. return &root->running;
  403. }
  404. static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
  405. {
  406. return qdisc->dev_queue->dev;
  407. }
  408. static inline void sch_tree_lock(const struct Qdisc *q)
  409. {
  410. spin_lock_bh(qdisc_root_sleeping_lock(q));
  411. }
  412. static inline void sch_tree_unlock(const struct Qdisc *q)
  413. {
  414. spin_unlock_bh(qdisc_root_sleeping_lock(q));
  415. }
  416. extern struct Qdisc noop_qdisc;
  417. extern struct Qdisc_ops noop_qdisc_ops;
  418. extern struct Qdisc_ops pfifo_fast_ops;
  419. extern struct Qdisc_ops mq_qdisc_ops;
  420. extern struct Qdisc_ops noqueue_qdisc_ops;
  421. extern const struct Qdisc_ops *default_qdisc_ops;
  422. static inline const struct Qdisc_ops *
  423. get_default_qdisc_ops(const struct net_device *dev, int ntx)
  424. {
  425. return ntx < dev->real_num_tx_queues ?
  426. default_qdisc_ops : &pfifo_fast_ops;
  427. }
  428. struct Qdisc_class_common {
  429. u32 classid;
  430. struct hlist_node hnode;
  431. };
  432. struct Qdisc_class_hash {
  433. struct hlist_head *hash;
  434. unsigned int hashsize;
  435. unsigned int hashmask;
  436. unsigned int hashelems;
  437. };
  438. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  439. {
  440. id ^= id >> 8;
  441. id ^= id >> 4;
  442. return id & mask;
  443. }
  444. static inline struct Qdisc_class_common *
  445. qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
  446. {
  447. struct Qdisc_class_common *cl;
  448. unsigned int h;
  449. if (!id)
  450. return NULL;
  451. h = qdisc_class_hash(id, hash->hashmask);
  452. hlist_for_each_entry(cl, &hash->hash[h], hnode) {
  453. if (cl->classid == id)
  454. return cl;
  455. }
  456. return NULL;
  457. }
  458. static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
  459. {
  460. u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
  461. return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
  462. }
  463. int qdisc_class_hash_init(struct Qdisc_class_hash *);
  464. void qdisc_class_hash_insert(struct Qdisc_class_hash *,
  465. struct Qdisc_class_common *);
  466. void qdisc_class_hash_remove(struct Qdisc_class_hash *,
  467. struct Qdisc_class_common *);
  468. void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  469. void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  470. int dev_qdisc_change_tx_queue_len(struct net_device *dev);
  471. void dev_init_scheduler(struct net_device *dev);
  472. void dev_shutdown(struct net_device *dev);
  473. void dev_activate(struct net_device *dev);
  474. void dev_deactivate(struct net_device *dev);
  475. void dev_deactivate_many(struct list_head *head);
  476. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  477. struct Qdisc *qdisc);
  478. void qdisc_reset(struct Qdisc *qdisc);
  479. void qdisc_destroy(struct Qdisc *qdisc);
  480. void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
  481. unsigned int len);
  482. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  483. const struct Qdisc_ops *ops,
  484. struct netlink_ext_ack *extack);
  485. void qdisc_free(struct Qdisc *qdisc);
  486. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  487. const struct Qdisc_ops *ops, u32 parentid,
  488. struct netlink_ext_ack *extack);
  489. void __qdisc_calculate_pkt_len(struct sk_buff *skb,
  490. const struct qdisc_size_table *stab);
  491. int skb_do_redirect(struct sk_buff *);
  492. static inline void skb_reset_tc(struct sk_buff *skb)
  493. {
  494. #ifdef CONFIG_NET_CLS_ACT
  495. skb->tc_redirected = 0;
  496. #endif
  497. }
  498. static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
  499. {
  500. #ifdef CONFIG_NET_CLS_ACT
  501. return skb->tc_redirected;
  502. #else
  503. return false;
  504. #endif
  505. }
  506. static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
  507. {
  508. #ifdef CONFIG_NET_CLS_ACT
  509. return skb->tc_at_ingress;
  510. #else
  511. return false;
  512. #endif
  513. }
  514. static inline bool skb_skip_tc_classify(struct sk_buff *skb)
  515. {
  516. #ifdef CONFIG_NET_CLS_ACT
  517. if (skb->tc_skip_classify) {
  518. skb->tc_skip_classify = 0;
  519. return true;
  520. }
  521. #endif
  522. return false;
  523. }
  524. /* Reset all TX qdiscs greater than index of a device. */
  525. static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
  526. {
  527. struct Qdisc *qdisc;
  528. for (; i < dev->num_tx_queues; i++) {
  529. qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
  530. if (qdisc) {
  531. spin_lock_bh(qdisc_lock(qdisc));
  532. qdisc_reset(qdisc);
  533. spin_unlock_bh(qdisc_lock(qdisc));
  534. }
  535. }
  536. }
  537. static inline void qdisc_reset_all_tx(struct net_device *dev)
  538. {
  539. qdisc_reset_all_tx_gt(dev, 0);
  540. }
  541. /* Are all TX queues of the device empty? */
  542. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  543. {
  544. unsigned int i;
  545. rcu_read_lock();
  546. for (i = 0; i < dev->num_tx_queues; i++) {
  547. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  548. const struct Qdisc *q = rcu_dereference(txq->qdisc);
  549. if (q->q.qlen) {
  550. rcu_read_unlock();
  551. return false;
  552. }
  553. }
  554. rcu_read_unlock();
  555. return true;
  556. }
  557. /* Are any of the TX qdiscs changing? */
  558. static inline bool qdisc_tx_changing(const struct net_device *dev)
  559. {
  560. unsigned int i;
  561. for (i = 0; i < dev->num_tx_queues; i++) {
  562. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  563. if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
  564. return true;
  565. }
  566. return false;
  567. }
  568. /* Is the device using the noop qdisc on all queues? */
  569. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  570. {
  571. unsigned int i;
  572. for (i = 0; i < dev->num_tx_queues; i++) {
  573. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  574. if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
  575. return false;
  576. }
  577. return true;
  578. }
  579. static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
  580. {
  581. return qdisc_skb_cb(skb)->pkt_len;
  582. }
  583. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  584. enum net_xmit_qdisc_t {
  585. __NET_XMIT_STOLEN = 0x00010000,
  586. __NET_XMIT_BYPASS = 0x00020000,
  587. };
  588. #ifdef CONFIG_NET_CLS_ACT
  589. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  590. #else
  591. #define net_xmit_drop_count(e) (1)
  592. #endif
  593. static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
  594. const struct Qdisc *sch)
  595. {
  596. #ifdef CONFIG_NET_SCHED
  597. struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
  598. if (stab)
  599. __qdisc_calculate_pkt_len(skb, stab);
  600. #endif
  601. }
  602. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  603. struct sk_buff **to_free)
  604. {
  605. qdisc_calculate_pkt_len(skb, sch);
  606. return sch->enqueue(skb, sch, to_free);
  607. }
  608. static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
  609. {
  610. return q->flags & TCQ_F_CPUSTATS;
  611. }
  612. static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
  613. __u64 bytes, __u32 packets)
  614. {
  615. bstats->bytes += bytes;
  616. bstats->packets += packets;
  617. }
  618. static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
  619. const struct sk_buff *skb)
  620. {
  621. _bstats_update(bstats,
  622. qdisc_pkt_len(skb),
  623. skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
  624. }
  625. static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
  626. __u64 bytes, __u32 packets)
  627. {
  628. u64_stats_update_begin(&bstats->syncp);
  629. _bstats_update(&bstats->bstats, bytes, packets);
  630. u64_stats_update_end(&bstats->syncp);
  631. }
  632. static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
  633. const struct sk_buff *skb)
  634. {
  635. u64_stats_update_begin(&bstats->syncp);
  636. bstats_update(&bstats->bstats, skb);
  637. u64_stats_update_end(&bstats->syncp);
  638. }
  639. static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
  640. const struct sk_buff *skb)
  641. {
  642. bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
  643. }
  644. static inline void qdisc_bstats_update(struct Qdisc *sch,
  645. const struct sk_buff *skb)
  646. {
  647. bstats_update(&sch->bstats, skb);
  648. }
  649. static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
  650. const struct sk_buff *skb)
  651. {
  652. sch->qstats.backlog -= qdisc_pkt_len(skb);
  653. }
  654. static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
  655. const struct sk_buff *skb)
  656. {
  657. this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
  658. }
  659. static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
  660. const struct sk_buff *skb)
  661. {
  662. sch->qstats.backlog += qdisc_pkt_len(skb);
  663. }
  664. static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
  665. const struct sk_buff *skb)
  666. {
  667. this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
  668. }
  669. static inline void qdisc_qstats_atomic_qlen_inc(struct Qdisc *sch)
  670. {
  671. atomic_inc(&sch->q.atomic_qlen);
  672. }
  673. static inline void qdisc_qstats_atomic_qlen_dec(struct Qdisc *sch)
  674. {
  675. atomic_dec(&sch->q.atomic_qlen);
  676. }
  677. static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
  678. {
  679. this_cpu_inc(sch->cpu_qstats->requeues);
  680. }
  681. static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
  682. {
  683. sch->qstats.drops += count;
  684. }
  685. static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
  686. {
  687. qstats->drops++;
  688. }
  689. static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
  690. {
  691. qstats->overlimits++;
  692. }
  693. static inline void qdisc_qstats_drop(struct Qdisc *sch)
  694. {
  695. qstats_drop_inc(&sch->qstats);
  696. }
  697. static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
  698. {
  699. this_cpu_inc(sch->cpu_qstats->drops);
  700. }
  701. static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
  702. {
  703. sch->qstats.overlimits++;
  704. }
  705. static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
  706. {
  707. qh->head = NULL;
  708. qh->tail = NULL;
  709. qh->qlen = 0;
  710. }
  711. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  712. struct qdisc_skb_head *qh)
  713. {
  714. struct sk_buff *last = qh->tail;
  715. if (last) {
  716. skb->next = NULL;
  717. last->next = skb;
  718. qh->tail = skb;
  719. } else {
  720. qh->tail = skb;
  721. qh->head = skb;
  722. }
  723. qh->qlen++;
  724. qdisc_qstats_backlog_inc(sch, skb);
  725. return NET_XMIT_SUCCESS;
  726. }
  727. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  728. {
  729. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  730. }
  731. static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
  732. {
  733. struct sk_buff *skb = qh->head;
  734. if (likely(skb != NULL)) {
  735. qh->head = skb->next;
  736. qh->qlen--;
  737. if (qh->head == NULL)
  738. qh->tail = NULL;
  739. skb->next = NULL;
  740. }
  741. return skb;
  742. }
  743. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  744. {
  745. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  746. if (likely(skb != NULL)) {
  747. qdisc_qstats_backlog_dec(sch, skb);
  748. qdisc_bstats_update(sch, skb);
  749. }
  750. return skb;
  751. }
  752. /* Instead of calling kfree_skb() while root qdisc lock is held,
  753. * queue the skb for future freeing at end of __dev_xmit_skb()
  754. */
  755. static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
  756. {
  757. skb->next = *to_free;
  758. *to_free = skb;
  759. }
  760. static inline void __qdisc_drop_all(struct sk_buff *skb,
  761. struct sk_buff **to_free)
  762. {
  763. if (skb->prev)
  764. skb->prev->next = *to_free;
  765. else
  766. skb->next = *to_free;
  767. *to_free = skb;
  768. }
  769. static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
  770. struct qdisc_skb_head *qh,
  771. struct sk_buff **to_free)
  772. {
  773. struct sk_buff *skb = __qdisc_dequeue_head(qh);
  774. if (likely(skb != NULL)) {
  775. unsigned int len = qdisc_pkt_len(skb);
  776. qdisc_qstats_backlog_dec(sch, skb);
  777. __qdisc_drop(skb, to_free);
  778. return len;
  779. }
  780. return 0;
  781. }
  782. static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
  783. struct sk_buff **to_free)
  784. {
  785. return __qdisc_queue_drop_head(sch, &sch->q, to_free);
  786. }
  787. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  788. {
  789. const struct qdisc_skb_head *qh = &sch->q;
  790. return qh->head;
  791. }
  792. /* generic pseudo peek method for non-work-conserving qdisc */
  793. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  794. {
  795. struct sk_buff *skb = skb_peek(&sch->gso_skb);
  796. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  797. if (!skb) {
  798. skb = sch->dequeue(sch);
  799. if (skb) {
  800. __skb_queue_head(&sch->gso_skb, skb);
  801. /* it's still part of the queue */
  802. qdisc_qstats_backlog_inc(sch, skb);
  803. sch->q.qlen++;
  804. }
  805. }
  806. return skb;
  807. }
  808. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  809. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  810. {
  811. struct sk_buff *skb = skb_peek(&sch->gso_skb);
  812. if (skb) {
  813. skb = __skb_dequeue(&sch->gso_skb);
  814. qdisc_qstats_backlog_dec(sch, skb);
  815. sch->q.qlen--;
  816. } else {
  817. skb = sch->dequeue(sch);
  818. }
  819. return skb;
  820. }
  821. static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
  822. {
  823. /*
  824. * We do not know the backlog in bytes of this list, it
  825. * is up to the caller to correct it
  826. */
  827. ASSERT_RTNL();
  828. if (qh->qlen) {
  829. rtnl_kfree_skbs(qh->head, qh->tail);
  830. qh->head = NULL;
  831. qh->tail = NULL;
  832. qh->qlen = 0;
  833. }
  834. }
  835. static inline void qdisc_reset_queue(struct Qdisc *sch)
  836. {
  837. __qdisc_reset_queue(&sch->q);
  838. sch->qstats.backlog = 0;
  839. }
  840. static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
  841. struct Qdisc **pold)
  842. {
  843. struct Qdisc *old;
  844. sch_tree_lock(sch);
  845. old = *pold;
  846. *pold = new;
  847. if (old != NULL) {
  848. unsigned int qlen = old->q.qlen;
  849. unsigned int backlog = old->qstats.backlog;
  850. qdisc_reset(old);
  851. qdisc_tree_reduce_backlog(old, qlen, backlog);
  852. }
  853. sch_tree_unlock(sch);
  854. return old;
  855. }
  856. static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  857. {
  858. rtnl_kfree_skbs(skb, skb);
  859. qdisc_qstats_drop(sch);
  860. }
  861. static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
  862. struct sk_buff **to_free)
  863. {
  864. __qdisc_drop(skb, to_free);
  865. qdisc_qstats_cpu_drop(sch);
  866. return NET_XMIT_DROP;
  867. }
  868. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
  869. struct sk_buff **to_free)
  870. {
  871. __qdisc_drop(skb, to_free);
  872. qdisc_qstats_drop(sch);
  873. return NET_XMIT_DROP;
  874. }
  875. static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
  876. struct sk_buff **to_free)
  877. {
  878. __qdisc_drop_all(skb, to_free);
  879. qdisc_qstats_drop(sch);
  880. return NET_XMIT_DROP;
  881. }
  882. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  883. long it will take to send a packet given its size.
  884. */
  885. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  886. {
  887. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  888. if (slot < 0)
  889. slot = 0;
  890. slot >>= rtab->rate.cell_log;
  891. if (slot > 255)
  892. return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
  893. return rtab->data[slot];
  894. }
  895. struct psched_ratecfg {
  896. u64 rate_bytes_ps; /* bytes per second */
  897. u32 mult;
  898. u16 overhead;
  899. u8 linklayer;
  900. u8 shift;
  901. };
  902. static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
  903. unsigned int len)
  904. {
  905. len += r->overhead;
  906. if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
  907. return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
  908. return ((u64)len * r->mult) >> r->shift;
  909. }
  910. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  911. const struct tc_ratespec *conf,
  912. u64 rate64);
  913. static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
  914. const struct psched_ratecfg *r)
  915. {
  916. memset(res, 0, sizeof(*res));
  917. /* legacy struct tc_ratespec has a 32bit @rate field
  918. * Qdisc using 64bit rate should add new attributes
  919. * in order to maintain compatibility.
  920. */
  921. res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
  922. res->overhead = r->overhead;
  923. res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
  924. }
  925. /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
  926. * The fast path only needs to access filter list and to update stats
  927. */
  928. struct mini_Qdisc {
  929. struct tcf_proto *filter_list;
  930. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  931. struct gnet_stats_queue __percpu *cpu_qstats;
  932. struct rcu_head rcu;
  933. };
  934. static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
  935. const struct sk_buff *skb)
  936. {
  937. bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
  938. }
  939. static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
  940. {
  941. this_cpu_inc(miniq->cpu_qstats->drops);
  942. }
  943. struct mini_Qdisc_pair {
  944. struct mini_Qdisc miniq1;
  945. struct mini_Qdisc miniq2;
  946. struct mini_Qdisc __rcu **p_miniq;
  947. };
  948. void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
  949. struct tcf_proto *tp_head);
  950. void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
  951. struct mini_Qdisc __rcu **p_miniq);
  952. static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
  953. {
  954. struct gnet_stats_queue *stats = res->qstats;
  955. int ret;
  956. if (res->ingress)
  957. ret = netif_receive_skb(skb);
  958. else
  959. ret = dev_queue_xmit(skb);
  960. if (ret && stats)
  961. qstats_overlimit_inc(res->qstats);
  962. }
  963. #endif