pkt_cls.h 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. #include <net/net_namespace.h>
  9. /* TC action not accessible from user space */
  10. #define TC_ACT_CONSUMED (TC_ACT_VALUE_MAX + 1)
  11. /* Basic packet classifier frontend definitions. */
  12. struct tcf_walker {
  13. int stop;
  14. int skip;
  15. int count;
  16. bool nonempty;
  17. unsigned long cookie;
  18. int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
  19. };
  20. int register_tcf_proto_ops(struct tcf_proto_ops *ops);
  21. void unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
  22. #define NET_CLS_ALIAS_PREFIX "net-cls-"
  23. #define MODULE_ALIAS_NET_CLS(kind) MODULE_ALIAS(NET_CLS_ALIAS_PREFIX kind)
  24. struct tcf_block_ext_info {
  25. enum flow_block_binder_type binder_type;
  26. tcf_chain_head_change_t *chain_head_change;
  27. void *chain_head_change_priv;
  28. u32 block_index;
  29. };
  30. struct tcf_qevent {
  31. struct tcf_block *block;
  32. struct tcf_block_ext_info info;
  33. struct tcf_proto __rcu *filter_chain;
  34. };
  35. struct tcf_block_cb;
  36. bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
  37. #ifdef CONFIG_NET_CLS
  38. struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
  39. u32 chain_index);
  40. void tcf_chain_put_by_act(struct tcf_chain *chain);
  41. struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
  42. struct tcf_chain *chain);
  43. struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
  44. struct tcf_proto *tp);
  45. void tcf_block_netif_keep_dst(struct tcf_block *block);
  46. int tcf_block_get(struct tcf_block **p_block,
  47. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  48. struct netlink_ext_ack *extack);
  49. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  50. struct tcf_block_ext_info *ei,
  51. struct netlink_ext_ack *extack);
  52. void tcf_block_put(struct tcf_block *block);
  53. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  54. struct tcf_block_ext_info *ei);
  55. int tcf_exts_init_ex(struct tcf_exts *exts, struct net *net, int action,
  56. int police, struct tcf_proto *tp, u32 handle, bool used_action_miss);
  57. static inline bool tcf_block_shared(struct tcf_block *block)
  58. {
  59. return block->index;
  60. }
  61. static inline bool tcf_block_non_null_shared(struct tcf_block *block)
  62. {
  63. return block && block->index;
  64. }
  65. #ifdef CONFIG_NET_CLS_ACT
  66. DECLARE_STATIC_KEY_FALSE(tcf_sw_enabled_key);
  67. static inline bool tcf_block_bypass_sw(struct tcf_block *block)
  68. {
  69. return block && !atomic_read(&block->useswcnt);
  70. }
  71. #endif
  72. static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
  73. {
  74. WARN_ON(tcf_block_shared(block));
  75. return block->q;
  76. }
  77. int tcf_classify(struct sk_buff *skb,
  78. const struct tcf_block *block,
  79. const struct tcf_proto *tp, struct tcf_result *res,
  80. bool compat_mode);
  81. static inline bool tc_cls_stats_dump(struct tcf_proto *tp,
  82. struct tcf_walker *arg,
  83. void *filter)
  84. {
  85. if (arg->count >= arg->skip && arg->fn(tp, filter, arg) < 0) {
  86. arg->stop = 1;
  87. return false;
  88. }
  89. arg->count++;
  90. return true;
  91. }
  92. #else
  93. static inline bool tcf_block_shared(struct tcf_block *block)
  94. {
  95. return false;
  96. }
  97. static inline bool tcf_block_non_null_shared(struct tcf_block *block)
  98. {
  99. return false;
  100. }
  101. static inline
  102. int tcf_block_get(struct tcf_block **p_block,
  103. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  104. struct netlink_ext_ack *extack)
  105. {
  106. return 0;
  107. }
  108. static inline
  109. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  110. struct tcf_block_ext_info *ei,
  111. struct netlink_ext_ack *extack)
  112. {
  113. return 0;
  114. }
  115. static inline void tcf_block_put(struct tcf_block *block)
  116. {
  117. }
  118. static inline
  119. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  120. struct tcf_block_ext_info *ei)
  121. {
  122. }
  123. static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
  124. {
  125. return NULL;
  126. }
  127. static inline int tcf_classify(struct sk_buff *skb,
  128. const struct tcf_block *block,
  129. const struct tcf_proto *tp,
  130. struct tcf_result *res, bool compat_mode)
  131. {
  132. return TC_ACT_UNSPEC;
  133. }
  134. #endif
  135. static inline unsigned long
  136. __cls_set_class(unsigned long *clp, unsigned long cl)
  137. {
  138. return xchg(clp, cl);
  139. }
  140. static inline void
  141. __tcf_bind_filter(struct Qdisc *q, struct tcf_result *r, unsigned long base)
  142. {
  143. unsigned long cl;
  144. cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
  145. cl = __cls_set_class(&r->class, cl);
  146. if (cl)
  147. q->ops->cl_ops->unbind_tcf(q, cl);
  148. }
  149. static inline void
  150. tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
  151. {
  152. struct Qdisc *q = tp->chain->block->q;
  153. /* Check q as it is not set for shared blocks. In that case,
  154. * setting class is not supported.
  155. */
  156. if (!q)
  157. return;
  158. sch_tree_lock(q);
  159. __tcf_bind_filter(q, r, base);
  160. sch_tree_unlock(q);
  161. }
  162. static inline void
  163. __tcf_unbind_filter(struct Qdisc *q, struct tcf_result *r)
  164. {
  165. unsigned long cl;
  166. if ((cl = __cls_set_class(&r->class, 0)) != 0)
  167. q->ops->cl_ops->unbind_tcf(q, cl);
  168. }
  169. static inline void
  170. tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
  171. {
  172. struct Qdisc *q = tp->chain->block->q;
  173. if (!q)
  174. return;
  175. __tcf_unbind_filter(q, r);
  176. }
  177. static inline void tc_cls_bind_class(u32 classid, unsigned long cl,
  178. void *q, struct tcf_result *res,
  179. unsigned long base)
  180. {
  181. if (res->classid == classid) {
  182. if (cl)
  183. __tcf_bind_filter(q, res, base);
  184. else
  185. __tcf_unbind_filter(q, res);
  186. }
  187. }
  188. struct tcf_exts {
  189. #ifdef CONFIG_NET_CLS_ACT
  190. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  191. int nr_actions;
  192. struct tc_action **actions;
  193. struct net *net;
  194. netns_tracker ns_tracker;
  195. struct tcf_exts_miss_cookie_node *miss_cookie_node;
  196. #endif
  197. /* Map to export classifier specific extension TLV types to the
  198. * generic extensions API. Unsupported extensions must be set to 0.
  199. */
  200. int action;
  201. int police;
  202. };
  203. static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
  204. int action, int police)
  205. {
  206. #ifdef CONFIG_NET_CLS
  207. return tcf_exts_init_ex(exts, net, action, police, NULL, 0, false);
  208. #else
  209. return -EOPNOTSUPP;
  210. #endif
  211. }
  212. /* Return false if the netns is being destroyed in cleanup_net(). Callers
  213. * need to do cleanup synchronously in this case, otherwise may race with
  214. * tc_action_net_exit(). Return true for other cases.
  215. */
  216. static inline bool tcf_exts_get_net(struct tcf_exts *exts)
  217. {
  218. #ifdef CONFIG_NET_CLS_ACT
  219. exts->net = maybe_get_net(exts->net);
  220. if (exts->net)
  221. netns_tracker_alloc(exts->net, &exts->ns_tracker, GFP_KERNEL);
  222. return exts->net != NULL;
  223. #else
  224. return true;
  225. #endif
  226. }
  227. static inline void tcf_exts_put_net(struct tcf_exts *exts)
  228. {
  229. #ifdef CONFIG_NET_CLS_ACT
  230. if (exts->net)
  231. put_net_track(exts->net, &exts->ns_tracker);
  232. #endif
  233. }
  234. #ifdef CONFIG_NET_CLS_ACT
  235. #define tcf_exts_for_each_action(i, a, exts) \
  236. for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
  237. #else
  238. #define tcf_exts_for_each_action(i, a, exts) \
  239. for (; 0; (void)(i), (void)(a), (void)(exts))
  240. #endif
  241. #define tcf_act_for_each_action(i, a, actions) \
  242. for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = actions[i]); i++)
  243. static inline bool tc_act_in_hw(struct tc_action *act)
  244. {
  245. return !!act->in_hw_count;
  246. }
  247. static inline void
  248. tcf_exts_hw_stats_update(const struct tcf_exts *exts,
  249. struct flow_stats *stats,
  250. bool use_act_stats)
  251. {
  252. #ifdef CONFIG_NET_CLS_ACT
  253. int i;
  254. for (i = 0; i < exts->nr_actions; i++) {
  255. struct tc_action *a = exts->actions[i];
  256. if (use_act_stats || tc_act_in_hw(a)) {
  257. if (!tcf_action_update_hw_stats(a))
  258. continue;
  259. }
  260. preempt_disable();
  261. tcf_action_stats_update(a, stats->bytes, stats->pkts, stats->drops,
  262. stats->lastused, true);
  263. preempt_enable();
  264. a->used_hw_stats = stats->used_hw_stats;
  265. a->used_hw_stats_valid = stats->used_hw_stats_valid;
  266. }
  267. #endif
  268. }
  269. /**
  270. * tcf_exts_has_actions - check if at least one action is present
  271. * @exts: tc filter extensions handle
  272. *
  273. * Returns true if at least one action is present.
  274. */
  275. static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
  276. {
  277. #ifdef CONFIG_NET_CLS_ACT
  278. return exts->nr_actions;
  279. #else
  280. return false;
  281. #endif
  282. }
  283. /**
  284. * tcf_exts_exec - execute tc filter extensions
  285. * @skb: socket buffer
  286. * @exts: tc filter extensions handle
  287. * @res: desired result
  288. *
  289. * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
  290. * a negative number if the filter must be considered unmatched or
  291. * a positive action code (TC_ACT_*) which must be returned to the
  292. * underlying layer.
  293. */
  294. static inline int
  295. tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
  296. struct tcf_result *res)
  297. {
  298. #ifdef CONFIG_NET_CLS_ACT
  299. return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
  300. #endif
  301. return TC_ACT_OK;
  302. }
  303. static inline int
  304. tcf_exts_exec_ex(struct sk_buff *skb, struct tcf_exts *exts, int act_index,
  305. struct tcf_result *res)
  306. {
  307. #ifdef CONFIG_NET_CLS_ACT
  308. return tcf_action_exec(skb, exts->actions + act_index,
  309. exts->nr_actions - act_index, res);
  310. #else
  311. return TC_ACT_OK;
  312. #endif
  313. }
  314. int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
  315. struct nlattr **tb, struct nlattr *rate_tlv,
  316. struct tcf_exts *exts, u32 flags,
  317. struct netlink_ext_ack *extack);
  318. int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  319. struct nlattr *rate_tlv, struct tcf_exts *exts,
  320. u32 flags, u32 fl_flags, struct netlink_ext_ack *extack);
  321. void tcf_exts_destroy(struct tcf_exts *exts);
  322. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
  323. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
  324. int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts);
  325. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
  326. /**
  327. * struct tcf_pkt_info - packet information
  328. *
  329. * @ptr: start of the pkt data
  330. * @nexthdr: offset of the next header
  331. */
  332. struct tcf_pkt_info {
  333. unsigned char * ptr;
  334. int nexthdr;
  335. };
  336. #ifdef CONFIG_NET_EMATCH
  337. struct tcf_ematch_ops;
  338. /**
  339. * struct tcf_ematch - extended match (ematch)
  340. *
  341. * @matchid: identifier to allow userspace to reidentify a match
  342. * @flags: flags specifying attributes and the relation to other matches
  343. * @ops: the operations lookup table of the corresponding ematch module
  344. * @datalen: length of the ematch specific configuration data
  345. * @data: ematch specific data
  346. * @net: the network namespace
  347. */
  348. struct tcf_ematch {
  349. struct tcf_ematch_ops * ops;
  350. unsigned long data;
  351. unsigned int datalen;
  352. u16 matchid;
  353. u16 flags;
  354. struct net *net;
  355. };
  356. static inline int tcf_em_is_container(struct tcf_ematch *em)
  357. {
  358. return !em->ops;
  359. }
  360. static inline int tcf_em_is_simple(struct tcf_ematch *em)
  361. {
  362. return em->flags & TCF_EM_SIMPLE;
  363. }
  364. static inline int tcf_em_is_inverted(struct tcf_ematch *em)
  365. {
  366. return em->flags & TCF_EM_INVERT;
  367. }
  368. static inline int tcf_em_last_match(struct tcf_ematch *em)
  369. {
  370. return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
  371. }
  372. static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
  373. {
  374. if (tcf_em_last_match(em))
  375. return 1;
  376. if (result == 0 && em->flags & TCF_EM_REL_AND)
  377. return 1;
  378. if (result != 0 && em->flags & TCF_EM_REL_OR)
  379. return 1;
  380. return 0;
  381. }
  382. /**
  383. * struct tcf_ematch_tree - ematch tree handle
  384. *
  385. * @hdr: ematch tree header supplied by userspace
  386. * @matches: array of ematches
  387. */
  388. struct tcf_ematch_tree {
  389. struct tcf_ematch_tree_hdr hdr;
  390. struct tcf_ematch * matches;
  391. };
  392. /**
  393. * struct tcf_ematch_ops - ematch module operations
  394. *
  395. * @kind: identifier (kind) of this ematch module
  396. * @datalen: length of expected configuration data (optional)
  397. * @change: called during validation (optional)
  398. * @match: called during ematch tree evaluation, must return 1/0
  399. * @destroy: called during destroyage (optional)
  400. * @dump: called during dumping process (optional)
  401. * @owner: owner, must be set to THIS_MODULE
  402. * @link: link to previous/next ematch module (internal use)
  403. */
  404. struct tcf_ematch_ops {
  405. int kind;
  406. int datalen;
  407. int (*change)(struct net *net, void *,
  408. int, struct tcf_ematch *);
  409. int (*match)(struct sk_buff *, struct tcf_ematch *,
  410. struct tcf_pkt_info *);
  411. void (*destroy)(struct tcf_ematch *);
  412. int (*dump)(struct sk_buff *, struct tcf_ematch *);
  413. struct module *owner;
  414. struct list_head link;
  415. };
  416. int tcf_em_register(struct tcf_ematch_ops *);
  417. void tcf_em_unregister(struct tcf_ematch_ops *);
  418. int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
  419. struct tcf_ematch_tree *);
  420. void tcf_em_tree_destroy(struct tcf_ematch_tree *);
  421. int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
  422. int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
  423. struct tcf_pkt_info *);
  424. /**
  425. * tcf_em_tree_match - evaluate an ematch tree
  426. *
  427. * @skb: socket buffer of the packet in question
  428. * @tree: ematch tree to be used for evaluation
  429. * @info: packet information examined by classifier
  430. *
  431. * This function matches @skb against the ematch tree in @tree by going
  432. * through all ematches respecting their logic relations returning
  433. * as soon as the result is obvious.
  434. *
  435. * Returns 1 if the ematch tree as-one matches, no ematches are configured
  436. * or ematch is not enabled in the kernel, otherwise 0 is returned.
  437. */
  438. static inline int tcf_em_tree_match(struct sk_buff *skb,
  439. struct tcf_ematch_tree *tree,
  440. struct tcf_pkt_info *info)
  441. {
  442. if (tree->hdr.nmatches)
  443. return __tcf_em_tree_match(skb, tree, info);
  444. else
  445. return 1;
  446. }
  447. #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
  448. #else /* CONFIG_NET_EMATCH */
  449. struct tcf_ematch_tree {
  450. };
  451. #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
  452. #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
  453. #define tcf_em_tree_dump(skb, t, tlv) (0)
  454. #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
  455. #endif /* CONFIG_NET_EMATCH */
  456. static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
  457. {
  458. switch (layer) {
  459. case TCF_LAYER_LINK:
  460. return skb_mac_header(skb);
  461. case TCF_LAYER_NETWORK:
  462. return skb_network_header(skb);
  463. case TCF_LAYER_TRANSPORT:
  464. return skb_transport_header(skb);
  465. }
  466. return NULL;
  467. }
  468. static inline int tcf_valid_offset(const struct sk_buff *skb,
  469. const unsigned char *ptr, const int len)
  470. {
  471. return likely((ptr + len) <= skb_tail_pointer(skb) &&
  472. ptr >= skb->head &&
  473. (ptr <= (ptr + len)));
  474. }
  475. static inline int
  476. tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
  477. struct netlink_ext_ack *extack)
  478. {
  479. char indev[IFNAMSIZ];
  480. struct net_device *dev;
  481. if (nla_strscpy(indev, indev_tlv, IFNAMSIZ) < 0) {
  482. NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
  483. "Interface name too long");
  484. return -EINVAL;
  485. }
  486. dev = __dev_get_by_name(net, indev);
  487. if (!dev) {
  488. NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
  489. "Network device not found");
  490. return -ENODEV;
  491. }
  492. return dev->ifindex;
  493. }
  494. static inline bool
  495. tcf_match_indev(struct sk_buff *skb, int ifindex)
  496. {
  497. if (!ifindex)
  498. return true;
  499. if (!skb->skb_iif)
  500. return false;
  501. return ifindex == skb->skb_iif;
  502. }
  503. int tc_setup_offload_action(struct flow_action *flow_action,
  504. const struct tcf_exts *exts,
  505. struct netlink_ext_ack *extack);
  506. void tc_cleanup_offload_action(struct flow_action *flow_action);
  507. int tc_setup_action(struct flow_action *flow_action,
  508. struct tc_action *actions[],
  509. u32 miss_cookie_base,
  510. struct netlink_ext_ack *extack);
  511. int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
  512. void *type_data, bool err_stop, bool rtnl_held);
  513. int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
  514. enum tc_setup_type type, void *type_data, bool err_stop,
  515. u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
  516. int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
  517. enum tc_setup_type type, void *type_data, bool err_stop,
  518. u32 *old_flags, unsigned int *old_in_hw_count,
  519. u32 *new_flags, unsigned int *new_in_hw_count,
  520. bool rtnl_held);
  521. int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
  522. enum tc_setup_type type, void *type_data, bool err_stop,
  523. u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
  524. int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
  525. bool add, flow_setup_cb_t *cb,
  526. enum tc_setup_type type, void *type_data,
  527. void *cb_priv, u32 *flags, unsigned int *in_hw_count);
  528. unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
  529. #ifdef CONFIG_NET_CLS_ACT
  530. int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
  531. enum flow_block_binder_type binder_type,
  532. struct nlattr *block_index_attr,
  533. struct netlink_ext_ack *extack);
  534. void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch);
  535. int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
  536. struct netlink_ext_ack *extack);
  537. struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
  538. struct sk_buff **to_free, int *ret);
  539. int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe);
  540. #else
  541. static inline int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
  542. enum flow_block_binder_type binder_type,
  543. struct nlattr *block_index_attr,
  544. struct netlink_ext_ack *extack)
  545. {
  546. return 0;
  547. }
  548. static inline void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
  549. {
  550. }
  551. static inline int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
  552. struct netlink_ext_ack *extack)
  553. {
  554. return 0;
  555. }
  556. static inline struct sk_buff *
  557. tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
  558. struct sk_buff **to_free, int *ret)
  559. {
  560. return skb;
  561. }
  562. static inline int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
  563. {
  564. return 0;
  565. }
  566. #endif
  567. struct tc_cls_u32_knode {
  568. struct tcf_exts *exts;
  569. struct tcf_result *res;
  570. struct tc_u32_sel *sel;
  571. u32 handle;
  572. u32 val;
  573. u32 mask;
  574. u32 link_handle;
  575. u8 fshift;
  576. };
  577. struct tc_cls_u32_hnode {
  578. u32 handle;
  579. u32 prio;
  580. unsigned int divisor;
  581. };
  582. enum tc_clsu32_command {
  583. TC_CLSU32_NEW_KNODE,
  584. TC_CLSU32_REPLACE_KNODE,
  585. TC_CLSU32_DELETE_KNODE,
  586. TC_CLSU32_NEW_HNODE,
  587. TC_CLSU32_REPLACE_HNODE,
  588. TC_CLSU32_DELETE_HNODE,
  589. };
  590. struct tc_cls_u32_offload {
  591. struct flow_cls_common_offload common;
  592. /* knode values */
  593. enum tc_clsu32_command command;
  594. union {
  595. struct tc_cls_u32_knode knode;
  596. struct tc_cls_u32_hnode hnode;
  597. };
  598. };
  599. static inline bool tc_can_offload(const struct net_device *dev)
  600. {
  601. return dev->features & NETIF_F_HW_TC;
  602. }
  603. static inline bool tc_can_offload_extack(const struct net_device *dev,
  604. struct netlink_ext_ack *extack)
  605. {
  606. bool can = tc_can_offload(dev);
  607. if (!can)
  608. NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
  609. return can;
  610. }
  611. static inline bool
  612. tc_cls_can_offload_and_chain0(const struct net_device *dev,
  613. struct flow_cls_common_offload *common)
  614. {
  615. if (!tc_can_offload_extack(dev, common->extack))
  616. return false;
  617. if (common->chain_index) {
  618. NL_SET_ERR_MSG(common->extack,
  619. "Driver supports only offload of chain 0");
  620. return false;
  621. }
  622. return true;
  623. }
  624. static inline bool tc_skip_hw(u32 flags)
  625. {
  626. return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
  627. }
  628. static inline bool tc_skip_sw(u32 flags)
  629. {
  630. return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
  631. }
  632. /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
  633. static inline bool tc_flags_valid(u32 flags)
  634. {
  635. if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
  636. TCA_CLS_FLAGS_VERBOSE))
  637. return false;
  638. flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
  639. if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
  640. return false;
  641. return true;
  642. }
  643. static inline bool tc_in_hw(u32 flags)
  644. {
  645. return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
  646. }
  647. static inline void
  648. tc_cls_common_offload_init(struct flow_cls_common_offload *cls_common,
  649. const struct tcf_proto *tp, u32 flags,
  650. struct netlink_ext_ack *extack)
  651. {
  652. cls_common->chain_index = tp->chain->index;
  653. cls_common->protocol = tp->protocol;
  654. cls_common->prio = tp->prio >> 16;
  655. if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
  656. cls_common->extack = extack;
  657. }
  658. static inline void tcf_proto_update_usesw(struct tcf_proto *tp, u32 flags)
  659. {
  660. if (tp->usesw)
  661. return;
  662. if (tc_skip_sw(flags) && tc_in_hw(flags))
  663. return;
  664. tp->usesw = true;
  665. }
  666. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  667. static inline struct tc_skb_ext *tc_skb_ext_alloc(struct sk_buff *skb)
  668. {
  669. struct tc_skb_ext *tc_skb_ext = skb_ext_add(skb, TC_SKB_EXT);
  670. if (tc_skb_ext)
  671. memset(tc_skb_ext, 0, sizeof(*tc_skb_ext));
  672. return tc_skb_ext;
  673. }
  674. #endif
  675. enum tc_matchall_command {
  676. TC_CLSMATCHALL_REPLACE,
  677. TC_CLSMATCHALL_DESTROY,
  678. TC_CLSMATCHALL_STATS,
  679. };
  680. struct tc_cls_matchall_offload {
  681. struct flow_cls_common_offload common;
  682. enum tc_matchall_command command;
  683. struct flow_rule *rule;
  684. struct flow_stats stats;
  685. bool use_act_stats;
  686. unsigned long cookie;
  687. };
  688. enum tc_clsbpf_command {
  689. TC_CLSBPF_OFFLOAD,
  690. TC_CLSBPF_STATS,
  691. };
  692. struct tc_cls_bpf_offload {
  693. struct flow_cls_common_offload common;
  694. enum tc_clsbpf_command command;
  695. struct tcf_exts *exts;
  696. struct bpf_prog *prog;
  697. struct bpf_prog *oldprog;
  698. const char *name;
  699. bool exts_integrated;
  700. };
  701. /* This structure holds cookie structure that is passed from user
  702. * to the kernel for actions and classifiers
  703. */
  704. struct tc_cookie {
  705. u8 *data;
  706. u32 len;
  707. struct rcu_head rcu;
  708. };
  709. struct tc_qopt_offload_stats {
  710. struct gnet_stats_basic_sync *bstats;
  711. struct gnet_stats_queue *qstats;
  712. };
  713. enum tc_mq_command {
  714. TC_MQ_CREATE,
  715. TC_MQ_DESTROY,
  716. TC_MQ_STATS,
  717. TC_MQ_GRAFT,
  718. };
  719. struct tc_mq_opt_offload_graft_params {
  720. unsigned long queue;
  721. u32 child_handle;
  722. };
  723. struct tc_mq_qopt_offload {
  724. enum tc_mq_command command;
  725. u32 handle;
  726. union {
  727. struct tc_qopt_offload_stats stats;
  728. struct tc_mq_opt_offload_graft_params graft_params;
  729. };
  730. };
  731. enum tc_htb_command {
  732. /* Root */
  733. TC_HTB_CREATE, /* Initialize HTB offload. */
  734. TC_HTB_DESTROY, /* Destroy HTB offload. */
  735. /* Classes */
  736. /* Allocate qid and create leaf. */
  737. TC_HTB_LEAF_ALLOC_QUEUE,
  738. /* Convert leaf to inner, preserve and return qid, create new leaf. */
  739. TC_HTB_LEAF_TO_INNER,
  740. /* Delete leaf, while siblings remain. */
  741. TC_HTB_LEAF_DEL,
  742. /* Delete leaf, convert parent to leaf, preserving qid. */
  743. TC_HTB_LEAF_DEL_LAST,
  744. /* TC_HTB_LEAF_DEL_LAST, but delete driver data on hardware errors. */
  745. TC_HTB_LEAF_DEL_LAST_FORCE,
  746. /* Modify parameters of a node. */
  747. TC_HTB_NODE_MODIFY,
  748. /* Class qdisc */
  749. TC_HTB_LEAF_QUERY_QUEUE, /* Query qid by classid. */
  750. };
  751. struct tc_htb_qopt_offload {
  752. struct netlink_ext_ack *extack;
  753. enum tc_htb_command command;
  754. u32 parent_classid;
  755. u16 classid;
  756. u16 qid;
  757. u32 quantum;
  758. u64 rate;
  759. u64 ceil;
  760. u8 prio;
  761. };
  762. #define TC_HTB_CLASSID_ROOT U32_MAX
  763. enum tc_red_command {
  764. TC_RED_REPLACE,
  765. TC_RED_DESTROY,
  766. TC_RED_STATS,
  767. TC_RED_XSTATS,
  768. TC_RED_GRAFT,
  769. };
  770. struct tc_red_qopt_offload_params {
  771. u32 min;
  772. u32 max;
  773. u32 probability;
  774. u32 limit;
  775. bool is_ecn;
  776. bool is_harddrop;
  777. bool is_nodrop;
  778. struct gnet_stats_queue *qstats;
  779. };
  780. struct tc_red_qopt_offload {
  781. enum tc_red_command command;
  782. u32 handle;
  783. u32 parent;
  784. union {
  785. struct tc_red_qopt_offload_params set;
  786. struct tc_qopt_offload_stats stats;
  787. struct red_stats *xstats;
  788. u32 child_handle;
  789. };
  790. };
  791. enum tc_gred_command {
  792. TC_GRED_REPLACE,
  793. TC_GRED_DESTROY,
  794. TC_GRED_STATS,
  795. };
  796. struct tc_gred_vq_qopt_offload_params {
  797. bool present;
  798. u32 limit;
  799. u32 prio;
  800. u32 min;
  801. u32 max;
  802. bool is_ecn;
  803. bool is_harddrop;
  804. u32 probability;
  805. /* Only need backlog, see struct tc_prio_qopt_offload_params */
  806. u32 *backlog;
  807. };
  808. struct tc_gred_qopt_offload_params {
  809. bool grio_on;
  810. bool wred_on;
  811. unsigned int dp_cnt;
  812. unsigned int dp_def;
  813. struct gnet_stats_queue *qstats;
  814. struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
  815. };
  816. struct tc_gred_qopt_offload_stats {
  817. struct gnet_stats_basic_sync bstats[MAX_DPs];
  818. struct gnet_stats_queue qstats[MAX_DPs];
  819. struct red_stats *xstats[MAX_DPs];
  820. };
  821. struct tc_gred_qopt_offload {
  822. enum tc_gred_command command;
  823. u32 handle;
  824. u32 parent;
  825. union {
  826. struct tc_gred_qopt_offload_params set;
  827. struct tc_gred_qopt_offload_stats stats;
  828. };
  829. };
  830. enum tc_prio_command {
  831. TC_PRIO_REPLACE,
  832. TC_PRIO_DESTROY,
  833. TC_PRIO_STATS,
  834. TC_PRIO_GRAFT,
  835. };
  836. struct tc_prio_qopt_offload_params {
  837. int bands;
  838. u8 priomap[TC_PRIO_MAX + 1];
  839. /* At the point of un-offloading the Qdisc, the reported backlog and
  840. * qlen need to be reduced by the portion that is in HW.
  841. */
  842. struct gnet_stats_queue *qstats;
  843. };
  844. struct tc_prio_qopt_offload_graft_params {
  845. u8 band;
  846. u32 child_handle;
  847. };
  848. struct tc_prio_qopt_offload {
  849. enum tc_prio_command command;
  850. u32 handle;
  851. u32 parent;
  852. union {
  853. struct tc_prio_qopt_offload_params replace_params;
  854. struct tc_qopt_offload_stats stats;
  855. struct tc_prio_qopt_offload_graft_params graft_params;
  856. };
  857. };
  858. enum tc_root_command {
  859. TC_ROOT_GRAFT,
  860. };
  861. struct tc_root_qopt_offload {
  862. enum tc_root_command command;
  863. u32 handle;
  864. bool ingress;
  865. };
  866. enum tc_ets_command {
  867. TC_ETS_REPLACE,
  868. TC_ETS_DESTROY,
  869. TC_ETS_STATS,
  870. TC_ETS_GRAFT,
  871. };
  872. struct tc_ets_qopt_offload_replace_params {
  873. unsigned int bands;
  874. u8 priomap[TC_PRIO_MAX + 1];
  875. unsigned int quanta[TCQ_ETS_MAX_BANDS]; /* 0 for strict bands. */
  876. unsigned int weights[TCQ_ETS_MAX_BANDS];
  877. struct gnet_stats_queue *qstats;
  878. };
  879. struct tc_ets_qopt_offload_graft_params {
  880. u8 band;
  881. u32 child_handle;
  882. };
  883. struct tc_ets_qopt_offload {
  884. enum tc_ets_command command;
  885. u32 handle;
  886. u32 parent;
  887. union {
  888. struct tc_ets_qopt_offload_replace_params replace_params;
  889. struct tc_qopt_offload_stats stats;
  890. struct tc_ets_qopt_offload_graft_params graft_params;
  891. };
  892. };
  893. enum tc_tbf_command {
  894. TC_TBF_REPLACE,
  895. TC_TBF_DESTROY,
  896. TC_TBF_STATS,
  897. TC_TBF_GRAFT,
  898. };
  899. struct tc_tbf_qopt_offload_replace_params {
  900. struct psched_ratecfg rate;
  901. u32 max_size;
  902. struct gnet_stats_queue *qstats;
  903. };
  904. struct tc_tbf_qopt_offload {
  905. enum tc_tbf_command command;
  906. u32 handle;
  907. u32 parent;
  908. union {
  909. struct tc_tbf_qopt_offload_replace_params replace_params;
  910. struct tc_qopt_offload_stats stats;
  911. u32 child_handle;
  912. };
  913. };
  914. enum tc_fifo_command {
  915. TC_FIFO_REPLACE,
  916. TC_FIFO_DESTROY,
  917. TC_FIFO_STATS,
  918. };
  919. struct tc_fifo_qopt_offload {
  920. enum tc_fifo_command command;
  921. u32 handle;
  922. u32 parent;
  923. union {
  924. struct tc_qopt_offload_stats stats;
  925. };
  926. };
  927. #ifdef CONFIG_NET_CLS_ACT
  928. DECLARE_STATIC_KEY_FALSE(tc_skb_ext_tc);
  929. void tc_skb_ext_tc_enable(void);
  930. void tc_skb_ext_tc_disable(void);
  931. #define tc_skb_ext_tc_enabled() static_branch_unlikely(&tc_skb_ext_tc)
  932. #else /* CONFIG_NET_CLS_ACT */
  933. static inline void tc_skb_ext_tc_enable(void) { }
  934. static inline void tc_skb_ext_tc_disable(void) { }
  935. #define tc_skb_ext_tc_enabled() false
  936. #endif
  937. #endif