pkt_cls.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_PKT_CLS_H
  3. #define __NET_PKT_CLS_H
  4. #include <linux/pkt_cls.h>
  5. #include <linux/workqueue.h>
  6. #include <net/sch_generic.h>
  7. #include <net/act_api.h>
  8. /* TC action not accessible from user space */
  9. #define TC_ACT_REINSERT (TC_ACT_VALUE_MAX + 1)
  10. /* Basic packet classifier frontend definitions. */
  11. struct tcf_walker {
  12. int stop;
  13. int skip;
  14. int count;
  15. unsigned long cookie;
  16. int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
  17. };
  18. int register_tcf_proto_ops(struct tcf_proto_ops *ops);
  19. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
  20. enum tcf_block_binder_type {
  21. TCF_BLOCK_BINDER_TYPE_UNSPEC,
  22. TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
  23. TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
  24. };
  25. struct tcf_block_ext_info {
  26. enum tcf_block_binder_type binder_type;
  27. tcf_chain_head_change_t *chain_head_change;
  28. void *chain_head_change_priv;
  29. u32 block_index;
  30. };
  31. struct tcf_block_cb;
  32. bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
  33. #ifdef CONFIG_NET_CLS
  34. struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
  35. u32 chain_index);
  36. void tcf_chain_put_by_act(struct tcf_chain *chain);
  37. void tcf_block_netif_keep_dst(struct tcf_block *block);
  38. int tcf_block_get(struct tcf_block **p_block,
  39. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  40. struct netlink_ext_ack *extack);
  41. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  42. struct tcf_block_ext_info *ei,
  43. struct netlink_ext_ack *extack);
  44. void tcf_block_put(struct tcf_block *block);
  45. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  46. struct tcf_block_ext_info *ei);
  47. static inline bool tcf_block_shared(struct tcf_block *block)
  48. {
  49. return block->index;
  50. }
  51. static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
  52. {
  53. WARN_ON(tcf_block_shared(block));
  54. return block->q;
  55. }
  56. static inline struct net_device *tcf_block_dev(struct tcf_block *block)
  57. {
  58. return tcf_block_q(block)->dev_queue->dev;
  59. }
  60. void *tcf_block_cb_priv(struct tcf_block_cb *block_cb);
  61. struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
  62. tc_setup_cb_t *cb, void *cb_ident);
  63. void tcf_block_cb_incref(struct tcf_block_cb *block_cb);
  64. unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb);
  65. struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
  66. tc_setup_cb_t *cb, void *cb_ident,
  67. void *cb_priv,
  68. struct netlink_ext_ack *extack);
  69. int tcf_block_cb_register(struct tcf_block *block,
  70. tc_setup_cb_t *cb, void *cb_ident,
  71. void *cb_priv, struct netlink_ext_ack *extack);
  72. void __tcf_block_cb_unregister(struct tcf_block *block,
  73. struct tcf_block_cb *block_cb);
  74. void tcf_block_cb_unregister(struct tcf_block *block,
  75. tc_setup_cb_t *cb, void *cb_ident);
  76. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  77. struct tcf_result *res, bool compat_mode);
  78. #else
  79. static inline
  80. int tcf_block_get(struct tcf_block **p_block,
  81. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  82. struct netlink_ext_ack *extack)
  83. {
  84. return 0;
  85. }
  86. static inline
  87. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  88. struct tcf_block_ext_info *ei,
  89. struct netlink_ext_ack *extack)
  90. {
  91. return 0;
  92. }
  93. static inline void tcf_block_put(struct tcf_block *block)
  94. {
  95. }
  96. static inline
  97. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  98. struct tcf_block_ext_info *ei)
  99. {
  100. }
  101. static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
  102. {
  103. return NULL;
  104. }
  105. static inline struct net_device *tcf_block_dev(struct tcf_block *block)
  106. {
  107. return NULL;
  108. }
  109. static inline
  110. int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb,
  111. void *cb_priv)
  112. {
  113. return 0;
  114. }
  115. static inline
  116. void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb,
  117. void *cb_priv)
  118. {
  119. }
  120. static inline
  121. void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
  122. {
  123. return NULL;
  124. }
  125. static inline
  126. struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
  127. tc_setup_cb_t *cb, void *cb_ident)
  128. {
  129. return NULL;
  130. }
  131. static inline
  132. void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
  133. {
  134. }
  135. static inline
  136. unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
  137. {
  138. return 0;
  139. }
  140. static inline
  141. struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
  142. tc_setup_cb_t *cb, void *cb_ident,
  143. void *cb_priv,
  144. struct netlink_ext_ack *extack)
  145. {
  146. return NULL;
  147. }
  148. static inline
  149. int tcf_block_cb_register(struct tcf_block *block,
  150. tc_setup_cb_t *cb, void *cb_ident,
  151. void *cb_priv, struct netlink_ext_ack *extack)
  152. {
  153. return 0;
  154. }
  155. static inline
  156. void __tcf_block_cb_unregister(struct tcf_block *block,
  157. struct tcf_block_cb *block_cb)
  158. {
  159. }
  160. static inline
  161. void tcf_block_cb_unregister(struct tcf_block *block,
  162. tc_setup_cb_t *cb, void *cb_ident)
  163. {
  164. }
  165. static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  166. struct tcf_result *res, bool compat_mode)
  167. {
  168. return TC_ACT_UNSPEC;
  169. }
  170. #endif
  171. static inline unsigned long
  172. __cls_set_class(unsigned long *clp, unsigned long cl)
  173. {
  174. return xchg(clp, cl);
  175. }
  176. static inline void
  177. __tcf_bind_filter(struct Qdisc *q, struct tcf_result *r, unsigned long base)
  178. {
  179. unsigned long cl;
  180. cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
  181. cl = __cls_set_class(&r->class, cl);
  182. if (cl)
  183. q->ops->cl_ops->unbind_tcf(q, cl);
  184. }
  185. static inline void
  186. tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
  187. {
  188. struct Qdisc *q = tp->chain->block->q;
  189. /* Check q as it is not set for shared blocks. In that case,
  190. * setting class is not supported.
  191. */
  192. if (!q)
  193. return;
  194. sch_tree_lock(q);
  195. __tcf_bind_filter(q, r, base);
  196. sch_tree_unlock(q);
  197. }
  198. static inline void
  199. __tcf_unbind_filter(struct Qdisc *q, struct tcf_result *r)
  200. {
  201. unsigned long cl;
  202. if ((cl = __cls_set_class(&r->class, 0)) != 0)
  203. q->ops->cl_ops->unbind_tcf(q, cl);
  204. }
  205. static inline void
  206. tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
  207. {
  208. struct Qdisc *q = tp->chain->block->q;
  209. if (!q)
  210. return;
  211. __tcf_unbind_filter(q, r);
  212. }
  213. struct tcf_exts {
  214. #ifdef CONFIG_NET_CLS_ACT
  215. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  216. int nr_actions;
  217. struct tc_action **actions;
  218. struct net *net;
  219. #endif
  220. /* Map to export classifier specific extension TLV types to the
  221. * generic extensions API. Unsupported extensions must be set to 0.
  222. */
  223. int action;
  224. int police;
  225. };
  226. static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
  227. {
  228. #ifdef CONFIG_NET_CLS_ACT
  229. exts->type = 0;
  230. exts->nr_actions = 0;
  231. exts->net = NULL;
  232. exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
  233. GFP_KERNEL);
  234. if (!exts->actions)
  235. return -ENOMEM;
  236. #endif
  237. exts->action = action;
  238. exts->police = police;
  239. return 0;
  240. }
  241. /* Return false if the netns is being destroyed in cleanup_net(). Callers
  242. * need to do cleanup synchronously in this case, otherwise may race with
  243. * tc_action_net_exit(). Return true for other cases.
  244. */
  245. static inline bool tcf_exts_get_net(struct tcf_exts *exts)
  246. {
  247. #ifdef CONFIG_NET_CLS_ACT
  248. exts->net = maybe_get_net(exts->net);
  249. return exts->net != NULL;
  250. #else
  251. return true;
  252. #endif
  253. }
  254. static inline void tcf_exts_put_net(struct tcf_exts *exts)
  255. {
  256. #ifdef CONFIG_NET_CLS_ACT
  257. if (exts->net)
  258. put_net(exts->net);
  259. #endif
  260. }
  261. #ifdef CONFIG_NET_CLS_ACT
  262. #define tcf_exts_for_each_action(i, a, exts) \
  263. for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
  264. #else
  265. #define tcf_exts_for_each_action(i, a, exts) \
  266. for (; 0; (void)(i), (void)(a), (void)(exts))
  267. #endif
  268. static inline void
  269. tcf_exts_stats_update(const struct tcf_exts *exts,
  270. u64 bytes, u64 packets, u64 lastuse)
  271. {
  272. #ifdef CONFIG_NET_CLS_ACT
  273. int i;
  274. preempt_disable();
  275. for (i = 0; i < exts->nr_actions; i++) {
  276. struct tc_action *a = exts->actions[i];
  277. tcf_action_stats_update(a, bytes, packets, lastuse);
  278. }
  279. preempt_enable();
  280. #endif
  281. }
  282. /**
  283. * tcf_exts_has_actions - check if at least one action is present
  284. * @exts: tc filter extensions handle
  285. *
  286. * Returns true if at least one action is present.
  287. */
  288. static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
  289. {
  290. #ifdef CONFIG_NET_CLS_ACT
  291. return exts->nr_actions;
  292. #else
  293. return false;
  294. #endif
  295. }
  296. /**
  297. * tcf_exts_has_one_action - check if exactly one action is present
  298. * @exts: tc filter extensions handle
  299. *
  300. * Returns true if exactly one action is present.
  301. */
  302. static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
  303. {
  304. #ifdef CONFIG_NET_CLS_ACT
  305. return exts->nr_actions == 1;
  306. #else
  307. return false;
  308. #endif
  309. }
  310. static inline struct tc_action *tcf_exts_first_action(struct tcf_exts *exts)
  311. {
  312. #ifdef CONFIG_NET_CLS_ACT
  313. return exts->actions[0];
  314. #else
  315. return NULL;
  316. #endif
  317. }
  318. /**
  319. * tcf_exts_exec - execute tc filter extensions
  320. * @skb: socket buffer
  321. * @exts: tc filter extensions handle
  322. * @res: desired result
  323. *
  324. * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
  325. * a negative number if the filter must be considered unmatched or
  326. * a positive action code (TC_ACT_*) which must be returned to the
  327. * underlying layer.
  328. */
  329. static inline int
  330. tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
  331. struct tcf_result *res)
  332. {
  333. #ifdef CONFIG_NET_CLS_ACT
  334. return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
  335. #endif
  336. return TC_ACT_OK;
  337. }
  338. int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
  339. struct nlattr **tb, struct nlattr *rate_tlv,
  340. struct tcf_exts *exts, bool ovr,
  341. struct netlink_ext_ack *extack);
  342. void tcf_exts_destroy(struct tcf_exts *exts);
  343. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
  344. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
  345. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
  346. /**
  347. * struct tcf_pkt_info - packet information
  348. */
  349. struct tcf_pkt_info {
  350. unsigned char * ptr;
  351. int nexthdr;
  352. };
  353. #ifdef CONFIG_NET_EMATCH
  354. struct tcf_ematch_ops;
  355. /**
  356. * struct tcf_ematch - extended match (ematch)
  357. *
  358. * @matchid: identifier to allow userspace to reidentify a match
  359. * @flags: flags specifying attributes and the relation to other matches
  360. * @ops: the operations lookup table of the corresponding ematch module
  361. * @datalen: length of the ematch specific configuration data
  362. * @data: ematch specific data
  363. */
  364. struct tcf_ematch {
  365. struct tcf_ematch_ops * ops;
  366. unsigned long data;
  367. unsigned int datalen;
  368. u16 matchid;
  369. u16 flags;
  370. struct net *net;
  371. };
  372. static inline int tcf_em_is_container(struct tcf_ematch *em)
  373. {
  374. return !em->ops;
  375. }
  376. static inline int tcf_em_is_simple(struct tcf_ematch *em)
  377. {
  378. return em->flags & TCF_EM_SIMPLE;
  379. }
  380. static inline int tcf_em_is_inverted(struct tcf_ematch *em)
  381. {
  382. return em->flags & TCF_EM_INVERT;
  383. }
  384. static inline int tcf_em_last_match(struct tcf_ematch *em)
  385. {
  386. return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
  387. }
  388. static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
  389. {
  390. if (tcf_em_last_match(em))
  391. return 1;
  392. if (result == 0 && em->flags & TCF_EM_REL_AND)
  393. return 1;
  394. if (result != 0 && em->flags & TCF_EM_REL_OR)
  395. return 1;
  396. return 0;
  397. }
  398. /**
  399. * struct tcf_ematch_tree - ematch tree handle
  400. *
  401. * @hdr: ematch tree header supplied by userspace
  402. * @matches: array of ematches
  403. */
  404. struct tcf_ematch_tree {
  405. struct tcf_ematch_tree_hdr hdr;
  406. struct tcf_ematch * matches;
  407. };
  408. /**
  409. * struct tcf_ematch_ops - ematch module operations
  410. *
  411. * @kind: identifier (kind) of this ematch module
  412. * @datalen: length of expected configuration data (optional)
  413. * @change: called during validation (optional)
  414. * @match: called during ematch tree evaluation, must return 1/0
  415. * @destroy: called during destroyage (optional)
  416. * @dump: called during dumping process (optional)
  417. * @owner: owner, must be set to THIS_MODULE
  418. * @link: link to previous/next ematch module (internal use)
  419. */
  420. struct tcf_ematch_ops {
  421. int kind;
  422. int datalen;
  423. int (*change)(struct net *net, void *,
  424. int, struct tcf_ematch *);
  425. int (*match)(struct sk_buff *, struct tcf_ematch *,
  426. struct tcf_pkt_info *);
  427. void (*destroy)(struct tcf_ematch *);
  428. int (*dump)(struct sk_buff *, struct tcf_ematch *);
  429. struct module *owner;
  430. struct list_head link;
  431. };
  432. int tcf_em_register(struct tcf_ematch_ops *);
  433. void tcf_em_unregister(struct tcf_ematch_ops *);
  434. int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
  435. struct tcf_ematch_tree *);
  436. void tcf_em_tree_destroy(struct tcf_ematch_tree *);
  437. int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
  438. int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
  439. struct tcf_pkt_info *);
  440. /**
  441. * tcf_em_tree_match - evaulate an ematch tree
  442. *
  443. * @skb: socket buffer of the packet in question
  444. * @tree: ematch tree to be used for evaluation
  445. * @info: packet information examined by classifier
  446. *
  447. * This function matches @skb against the ematch tree in @tree by going
  448. * through all ematches respecting their logic relations returning
  449. * as soon as the result is obvious.
  450. *
  451. * Returns 1 if the ematch tree as-one matches, no ematches are configured
  452. * or ematch is not enabled in the kernel, otherwise 0 is returned.
  453. */
  454. static inline int tcf_em_tree_match(struct sk_buff *skb,
  455. struct tcf_ematch_tree *tree,
  456. struct tcf_pkt_info *info)
  457. {
  458. if (tree->hdr.nmatches)
  459. return __tcf_em_tree_match(skb, tree, info);
  460. else
  461. return 1;
  462. }
  463. #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
  464. #else /* CONFIG_NET_EMATCH */
  465. struct tcf_ematch_tree {
  466. };
  467. #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
  468. #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
  469. #define tcf_em_tree_dump(skb, t, tlv) (0)
  470. #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
  471. #endif /* CONFIG_NET_EMATCH */
  472. static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
  473. {
  474. switch (layer) {
  475. case TCF_LAYER_LINK:
  476. return skb_mac_header(skb);
  477. case TCF_LAYER_NETWORK:
  478. return skb_network_header(skb);
  479. case TCF_LAYER_TRANSPORT:
  480. return skb_transport_header(skb);
  481. }
  482. return NULL;
  483. }
  484. static inline int tcf_valid_offset(const struct sk_buff *skb,
  485. const unsigned char *ptr, const int len)
  486. {
  487. return likely((ptr + len) <= skb_tail_pointer(skb) &&
  488. ptr >= skb->head &&
  489. (ptr <= (ptr + len)));
  490. }
  491. #ifdef CONFIG_NET_CLS_IND
  492. #include <net/net_namespace.h>
  493. static inline int
  494. tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
  495. struct netlink_ext_ack *extack)
  496. {
  497. char indev[IFNAMSIZ];
  498. struct net_device *dev;
  499. if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
  500. NL_SET_ERR_MSG(extack, "Interface name too long");
  501. return -EINVAL;
  502. }
  503. dev = __dev_get_by_name(net, indev);
  504. if (!dev)
  505. return -ENODEV;
  506. return dev->ifindex;
  507. }
  508. static inline bool
  509. tcf_match_indev(struct sk_buff *skb, int ifindex)
  510. {
  511. if (!ifindex)
  512. return true;
  513. if (!skb->skb_iif)
  514. return false;
  515. return ifindex == skb->skb_iif;
  516. }
  517. #endif /* CONFIG_NET_CLS_IND */
  518. int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
  519. enum tc_setup_type type, void *type_data, bool err_stop);
  520. enum tc_block_command {
  521. TC_BLOCK_BIND,
  522. TC_BLOCK_UNBIND,
  523. };
  524. struct tc_block_offload {
  525. enum tc_block_command command;
  526. enum tcf_block_binder_type binder_type;
  527. struct tcf_block *block;
  528. struct netlink_ext_ack *extack;
  529. };
  530. struct tc_cls_common_offload {
  531. u32 chain_index;
  532. __be16 protocol;
  533. u32 prio;
  534. struct netlink_ext_ack *extack;
  535. };
  536. struct tc_cls_u32_knode {
  537. struct tcf_exts *exts;
  538. struct tc_u32_sel *sel;
  539. u32 handle;
  540. u32 val;
  541. u32 mask;
  542. u32 link_handle;
  543. u8 fshift;
  544. };
  545. struct tc_cls_u32_hnode {
  546. u32 handle;
  547. u32 prio;
  548. unsigned int divisor;
  549. };
  550. enum tc_clsu32_command {
  551. TC_CLSU32_NEW_KNODE,
  552. TC_CLSU32_REPLACE_KNODE,
  553. TC_CLSU32_DELETE_KNODE,
  554. TC_CLSU32_NEW_HNODE,
  555. TC_CLSU32_REPLACE_HNODE,
  556. TC_CLSU32_DELETE_HNODE,
  557. };
  558. struct tc_cls_u32_offload {
  559. struct tc_cls_common_offload common;
  560. /* knode values */
  561. enum tc_clsu32_command command;
  562. union {
  563. struct tc_cls_u32_knode knode;
  564. struct tc_cls_u32_hnode hnode;
  565. };
  566. };
  567. static inline bool tc_can_offload(const struct net_device *dev)
  568. {
  569. return dev->features & NETIF_F_HW_TC;
  570. }
  571. static inline bool tc_can_offload_extack(const struct net_device *dev,
  572. struct netlink_ext_ack *extack)
  573. {
  574. bool can = tc_can_offload(dev);
  575. if (!can)
  576. NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
  577. return can;
  578. }
  579. static inline bool
  580. tc_cls_can_offload_and_chain0(const struct net_device *dev,
  581. struct tc_cls_common_offload *common)
  582. {
  583. if (!tc_can_offload_extack(dev, common->extack))
  584. return false;
  585. if (common->chain_index) {
  586. NL_SET_ERR_MSG(common->extack,
  587. "Driver supports only offload of chain 0");
  588. return false;
  589. }
  590. return true;
  591. }
  592. static inline bool tc_skip_hw(u32 flags)
  593. {
  594. return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
  595. }
  596. static inline bool tc_skip_sw(u32 flags)
  597. {
  598. return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
  599. }
  600. /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
  601. static inline bool tc_flags_valid(u32 flags)
  602. {
  603. if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
  604. TCA_CLS_FLAGS_VERBOSE))
  605. return false;
  606. flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
  607. if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
  608. return false;
  609. return true;
  610. }
  611. static inline bool tc_in_hw(u32 flags)
  612. {
  613. return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
  614. }
  615. static inline void
  616. tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
  617. const struct tcf_proto *tp, u32 flags,
  618. struct netlink_ext_ack *extack)
  619. {
  620. cls_common->chain_index = tp->chain->index;
  621. cls_common->protocol = tp->protocol;
  622. cls_common->prio = tp->prio;
  623. if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
  624. cls_common->extack = extack;
  625. }
  626. enum tc_fl_command {
  627. TC_CLSFLOWER_REPLACE,
  628. TC_CLSFLOWER_DESTROY,
  629. TC_CLSFLOWER_STATS,
  630. TC_CLSFLOWER_TMPLT_CREATE,
  631. TC_CLSFLOWER_TMPLT_DESTROY,
  632. };
  633. struct tc_cls_flower_offload {
  634. struct tc_cls_common_offload common;
  635. enum tc_fl_command command;
  636. unsigned long cookie;
  637. struct flow_dissector *dissector;
  638. struct fl_flow_key *mask;
  639. struct fl_flow_key *key;
  640. struct tcf_exts *exts;
  641. u32 classid;
  642. };
  643. enum tc_matchall_command {
  644. TC_CLSMATCHALL_REPLACE,
  645. TC_CLSMATCHALL_DESTROY,
  646. };
  647. struct tc_cls_matchall_offload {
  648. struct tc_cls_common_offload common;
  649. enum tc_matchall_command command;
  650. struct tcf_exts *exts;
  651. unsigned long cookie;
  652. };
  653. enum tc_clsbpf_command {
  654. TC_CLSBPF_OFFLOAD,
  655. TC_CLSBPF_STATS,
  656. };
  657. struct tc_cls_bpf_offload {
  658. struct tc_cls_common_offload common;
  659. enum tc_clsbpf_command command;
  660. struct tcf_exts *exts;
  661. struct bpf_prog *prog;
  662. struct bpf_prog *oldprog;
  663. const char *name;
  664. bool exts_integrated;
  665. };
  666. struct tc_mqprio_qopt_offload {
  667. /* struct tc_mqprio_qopt must always be the first element */
  668. struct tc_mqprio_qopt qopt;
  669. u16 mode;
  670. u16 shaper;
  671. u32 flags;
  672. u64 min_rate[TC_QOPT_MAX_QUEUE];
  673. u64 max_rate[TC_QOPT_MAX_QUEUE];
  674. };
  675. /* This structure holds cookie structure that is passed from user
  676. * to the kernel for actions and classifiers
  677. */
  678. struct tc_cookie {
  679. u8 *data;
  680. u32 len;
  681. struct rcu_head rcu;
  682. };
  683. struct tc_qopt_offload_stats {
  684. struct gnet_stats_basic_packed *bstats;
  685. struct gnet_stats_queue *qstats;
  686. };
  687. enum tc_mq_command {
  688. TC_MQ_CREATE,
  689. TC_MQ_DESTROY,
  690. TC_MQ_STATS,
  691. };
  692. struct tc_mq_qopt_offload {
  693. enum tc_mq_command command;
  694. u32 handle;
  695. struct tc_qopt_offload_stats stats;
  696. };
  697. enum tc_red_command {
  698. TC_RED_REPLACE,
  699. TC_RED_DESTROY,
  700. TC_RED_STATS,
  701. TC_RED_XSTATS,
  702. };
  703. struct tc_red_qopt_offload_params {
  704. u32 min;
  705. u32 max;
  706. u32 probability;
  707. bool is_ecn;
  708. struct gnet_stats_queue *qstats;
  709. };
  710. struct tc_red_qopt_offload {
  711. enum tc_red_command command;
  712. u32 handle;
  713. u32 parent;
  714. union {
  715. struct tc_red_qopt_offload_params set;
  716. struct tc_qopt_offload_stats stats;
  717. struct red_stats *xstats;
  718. };
  719. };
  720. enum tc_prio_command {
  721. TC_PRIO_REPLACE,
  722. TC_PRIO_DESTROY,
  723. TC_PRIO_STATS,
  724. TC_PRIO_GRAFT,
  725. };
  726. struct tc_prio_qopt_offload_params {
  727. int bands;
  728. u8 priomap[TC_PRIO_MAX + 1];
  729. /* In case that a prio qdisc is offloaded and now is changed to a
  730. * non-offloadedable config, it needs to update the backlog & qlen
  731. * values to negate the HW backlog & qlen values (and only them).
  732. */
  733. struct gnet_stats_queue *qstats;
  734. };
  735. struct tc_prio_qopt_offload_graft_params {
  736. u8 band;
  737. u32 child_handle;
  738. };
  739. struct tc_prio_qopt_offload {
  740. enum tc_prio_command command;
  741. u32 handle;
  742. u32 parent;
  743. union {
  744. struct tc_prio_qopt_offload_params replace_params;
  745. struct tc_qopt_offload_stats stats;
  746. struct tc_prio_qopt_offload_graft_params graft_params;
  747. };
  748. };
  749. #endif