pkt_cls.h 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. if (!skb_transport_header_was_set(skb))
  465. break;
  466. return skb_transport_header(skb);
  467. }
  468. return NULL;
  469. }
  470. static inline int tcf_valid_offset(const struct sk_buff *skb,
  471. const unsigned char *ptr, const int len)
  472. {
  473. return likely((ptr + len) <= skb_tail_pointer(skb) &&
  474. ptr >= skb->head &&
  475. (ptr <= (ptr + len)));
  476. }
  477. static inline int
  478. tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
  479. struct netlink_ext_ack *extack)
  480. {
  481. char indev[IFNAMSIZ];
  482. struct net_device *dev;
  483. if (nla_strscpy(indev, indev_tlv, IFNAMSIZ) < 0) {
  484. NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
  485. "Interface name too long");
  486. return -EINVAL;
  487. }
  488. dev = __dev_get_by_name(net, indev);
  489. if (!dev) {
  490. NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
  491. "Network device not found");
  492. return -ENODEV;
  493. }
  494. return dev->ifindex;
  495. }
  496. static inline bool
  497. tcf_match_indev(struct sk_buff *skb, int ifindex)
  498. {
  499. if (!ifindex)
  500. return true;
  501. if (!skb->skb_iif)
  502. return false;
  503. return ifindex == skb->skb_iif;
  504. }
  505. int tc_setup_offload_action(struct flow_action *flow_action,
  506. const struct tcf_exts *exts,
  507. struct netlink_ext_ack *extack);
  508. void tc_cleanup_offload_action(struct flow_action *flow_action);
  509. int tc_setup_action(struct flow_action *flow_action,
  510. struct tc_action *actions[],
  511. u32 miss_cookie_base,
  512. struct netlink_ext_ack *extack);
  513. int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
  514. void *type_data, bool err_stop, bool rtnl_held);
  515. int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
  516. enum tc_setup_type type, void *type_data, bool err_stop,
  517. u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
  518. int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
  519. enum tc_setup_type type, void *type_data, bool err_stop,
  520. u32 *old_flags, unsigned int *old_in_hw_count,
  521. u32 *new_flags, unsigned int *new_in_hw_count,
  522. bool rtnl_held);
  523. int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
  524. enum tc_setup_type type, void *type_data, bool err_stop,
  525. u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
  526. int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
  527. bool add, flow_setup_cb_t *cb,
  528. enum tc_setup_type type, void *type_data,
  529. void *cb_priv, u32 *flags, unsigned int *in_hw_count);
  530. unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
  531. #ifdef CONFIG_NET_CLS_ACT
  532. int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
  533. enum flow_block_binder_type binder_type,
  534. struct nlattr *block_index_attr,
  535. struct netlink_ext_ack *extack);
  536. void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch);
  537. int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
  538. struct netlink_ext_ack *extack);
  539. struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
  540. struct sk_buff **to_free, int *ret);
  541. int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe);
  542. #else
  543. static inline int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
  544. enum flow_block_binder_type binder_type,
  545. struct nlattr *block_index_attr,
  546. struct netlink_ext_ack *extack)
  547. {
  548. return 0;
  549. }
  550. static inline void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
  551. {
  552. }
  553. static inline int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
  554. struct netlink_ext_ack *extack)
  555. {
  556. return 0;
  557. }
  558. static inline struct sk_buff *
  559. tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
  560. struct sk_buff **to_free, int *ret)
  561. {
  562. return skb;
  563. }
  564. static inline int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
  565. {
  566. return 0;
  567. }
  568. #endif
  569. struct tc_cls_u32_knode {
  570. struct tcf_exts *exts;
  571. struct tcf_result *res;
  572. struct tc_u32_sel *sel;
  573. u32 handle;
  574. u32 val;
  575. u32 mask;
  576. u32 link_handle;
  577. u8 fshift;
  578. };
  579. struct tc_cls_u32_hnode {
  580. u32 handle;
  581. u32 prio;
  582. unsigned int divisor;
  583. };
  584. enum tc_clsu32_command {
  585. TC_CLSU32_NEW_KNODE,
  586. TC_CLSU32_REPLACE_KNODE,
  587. TC_CLSU32_DELETE_KNODE,
  588. TC_CLSU32_NEW_HNODE,
  589. TC_CLSU32_REPLACE_HNODE,
  590. TC_CLSU32_DELETE_HNODE,
  591. };
  592. struct tc_cls_u32_offload {
  593. struct flow_cls_common_offload common;
  594. /* knode values */
  595. enum tc_clsu32_command command;
  596. union {
  597. struct tc_cls_u32_knode knode;
  598. struct tc_cls_u32_hnode hnode;
  599. };
  600. };
  601. static inline bool tc_can_offload(const struct net_device *dev)
  602. {
  603. return dev->features & NETIF_F_HW_TC;
  604. }
  605. static inline bool tc_can_offload_extack(const struct net_device *dev,
  606. struct netlink_ext_ack *extack)
  607. {
  608. bool can = tc_can_offload(dev);
  609. if (!can)
  610. NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
  611. return can;
  612. }
  613. static inline bool
  614. tc_cls_can_offload_and_chain0(const struct net_device *dev,
  615. struct flow_cls_common_offload *common)
  616. {
  617. if (!tc_can_offload_extack(dev, common->extack))
  618. return false;
  619. if (common->chain_index) {
  620. NL_SET_ERR_MSG(common->extack,
  621. "Driver supports only offload of chain 0");
  622. return false;
  623. }
  624. return true;
  625. }
  626. static inline bool tc_skip_hw(u32 flags)
  627. {
  628. return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
  629. }
  630. static inline bool tc_skip_sw(u32 flags)
  631. {
  632. return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
  633. }
  634. /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
  635. static inline bool tc_flags_valid(u32 flags)
  636. {
  637. if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
  638. TCA_CLS_FLAGS_VERBOSE))
  639. return false;
  640. flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
  641. if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
  642. return false;
  643. return true;
  644. }
  645. static inline bool tc_in_hw(u32 flags)
  646. {
  647. return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
  648. }
  649. static inline void
  650. tc_cls_common_offload_init(struct flow_cls_common_offload *cls_common,
  651. const struct tcf_proto *tp, u32 flags,
  652. struct netlink_ext_ack *extack)
  653. {
  654. cls_common->chain_index = tp->chain->index;
  655. cls_common->protocol = tp->protocol;
  656. cls_common->prio = tp->prio >> 16;
  657. cls_common->skip_sw = tc_skip_sw(flags);
  658. if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
  659. cls_common->extack = extack;
  660. }
  661. static inline void tcf_proto_update_usesw(struct tcf_proto *tp, u32 flags)
  662. {
  663. if (tp->usesw)
  664. return;
  665. if (tc_skip_sw(flags) && tc_in_hw(flags))
  666. return;
  667. tp->usesw = true;
  668. }
  669. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  670. static inline struct tc_skb_ext *tc_skb_ext_alloc(struct sk_buff *skb)
  671. {
  672. struct tc_skb_ext *tc_skb_ext = skb_ext_add(skb, TC_SKB_EXT);
  673. if (tc_skb_ext)
  674. memset(tc_skb_ext, 0, sizeof(*tc_skb_ext));
  675. return tc_skb_ext;
  676. }
  677. #endif
  678. enum tc_matchall_command {
  679. TC_CLSMATCHALL_REPLACE,
  680. TC_CLSMATCHALL_DESTROY,
  681. TC_CLSMATCHALL_STATS,
  682. };
  683. struct tc_cls_matchall_offload {
  684. struct flow_cls_common_offload common;
  685. enum tc_matchall_command command;
  686. struct flow_rule *rule;
  687. struct flow_stats stats;
  688. bool use_act_stats;
  689. unsigned long cookie;
  690. };
  691. enum tc_clsbpf_command {
  692. TC_CLSBPF_OFFLOAD,
  693. TC_CLSBPF_STATS,
  694. };
  695. struct tc_cls_bpf_offload {
  696. struct flow_cls_common_offload common;
  697. enum tc_clsbpf_command command;
  698. struct tcf_exts *exts;
  699. struct bpf_prog *prog;
  700. struct bpf_prog *oldprog;
  701. const char *name;
  702. bool exts_integrated;
  703. };
  704. /* This structure holds cookie structure that is passed from user
  705. * to the kernel for actions and classifiers
  706. */
  707. struct tc_cookie {
  708. u8 *data;
  709. u32 len;
  710. struct rcu_head rcu;
  711. };
  712. struct tc_qopt_offload_stats {
  713. struct gnet_stats_basic_sync *bstats;
  714. struct gnet_stats_queue *qstats;
  715. };
  716. enum tc_mq_command {
  717. TC_MQ_CREATE,
  718. TC_MQ_DESTROY,
  719. TC_MQ_STATS,
  720. TC_MQ_GRAFT,
  721. };
  722. struct tc_mq_opt_offload_graft_params {
  723. unsigned long queue;
  724. u32 child_handle;
  725. };
  726. struct tc_mq_qopt_offload {
  727. enum tc_mq_command command;
  728. u32 handle;
  729. union {
  730. struct tc_qopt_offload_stats stats;
  731. struct tc_mq_opt_offload_graft_params graft_params;
  732. };
  733. };
  734. enum tc_htb_command {
  735. /* Root */
  736. TC_HTB_CREATE, /* Initialize HTB offload. */
  737. TC_HTB_DESTROY, /* Destroy HTB offload. */
  738. /* Classes */
  739. /* Allocate qid and create leaf. */
  740. TC_HTB_LEAF_ALLOC_QUEUE,
  741. /* Convert leaf to inner, preserve and return qid, create new leaf. */
  742. TC_HTB_LEAF_TO_INNER,
  743. /* Delete leaf, while siblings remain. */
  744. TC_HTB_LEAF_DEL,
  745. /* Delete leaf, convert parent to leaf, preserving qid. */
  746. TC_HTB_LEAF_DEL_LAST,
  747. /* TC_HTB_LEAF_DEL_LAST, but delete driver data on hardware errors. */
  748. TC_HTB_LEAF_DEL_LAST_FORCE,
  749. /* Modify parameters of a node. */
  750. TC_HTB_NODE_MODIFY,
  751. /* Class qdisc */
  752. TC_HTB_LEAF_QUERY_QUEUE, /* Query qid by classid. */
  753. };
  754. struct tc_htb_qopt_offload {
  755. struct netlink_ext_ack *extack;
  756. enum tc_htb_command command;
  757. u32 parent_classid;
  758. u16 classid;
  759. u16 qid;
  760. u32 quantum;
  761. u64 rate;
  762. u64 ceil;
  763. u8 prio;
  764. };
  765. #define TC_HTB_CLASSID_ROOT U32_MAX
  766. enum tc_red_command {
  767. TC_RED_REPLACE,
  768. TC_RED_DESTROY,
  769. TC_RED_STATS,
  770. TC_RED_XSTATS,
  771. TC_RED_GRAFT,
  772. };
  773. struct tc_red_qopt_offload_params {
  774. u32 min;
  775. u32 max;
  776. u32 probability;
  777. u32 limit;
  778. bool is_ecn;
  779. bool is_harddrop;
  780. bool is_nodrop;
  781. struct gnet_stats_queue *qstats;
  782. };
  783. struct tc_red_qopt_offload {
  784. enum tc_red_command command;
  785. u32 handle;
  786. u32 parent;
  787. union {
  788. struct tc_red_qopt_offload_params set;
  789. struct tc_qopt_offload_stats stats;
  790. struct red_stats *xstats;
  791. u32 child_handle;
  792. };
  793. };
  794. enum tc_gred_command {
  795. TC_GRED_REPLACE,
  796. TC_GRED_DESTROY,
  797. TC_GRED_STATS,
  798. };
  799. struct tc_gred_vq_qopt_offload_params {
  800. bool present;
  801. u32 limit;
  802. u32 prio;
  803. u32 min;
  804. u32 max;
  805. bool is_ecn;
  806. bool is_harddrop;
  807. u32 probability;
  808. /* Only need backlog, see struct tc_prio_qopt_offload_params */
  809. u32 *backlog;
  810. };
  811. struct tc_gred_qopt_offload_params {
  812. bool grio_on;
  813. bool wred_on;
  814. unsigned int dp_cnt;
  815. unsigned int dp_def;
  816. struct gnet_stats_queue *qstats;
  817. struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
  818. };
  819. struct tc_gred_qopt_offload_stats {
  820. struct gnet_stats_basic_sync bstats[MAX_DPs];
  821. struct gnet_stats_queue qstats[MAX_DPs];
  822. struct red_stats *xstats[MAX_DPs];
  823. };
  824. struct tc_gred_qopt_offload {
  825. enum tc_gred_command command;
  826. u32 handle;
  827. u32 parent;
  828. union {
  829. struct tc_gred_qopt_offload_params set;
  830. struct tc_gred_qopt_offload_stats stats;
  831. };
  832. };
  833. enum tc_prio_command {
  834. TC_PRIO_REPLACE,
  835. TC_PRIO_DESTROY,
  836. TC_PRIO_STATS,
  837. TC_PRIO_GRAFT,
  838. };
  839. struct tc_prio_qopt_offload_params {
  840. int bands;
  841. u8 priomap[TC_PRIO_MAX + 1];
  842. /* At the point of un-offloading the Qdisc, the reported backlog and
  843. * qlen need to be reduced by the portion that is in HW.
  844. */
  845. struct gnet_stats_queue *qstats;
  846. };
  847. struct tc_prio_qopt_offload_graft_params {
  848. u8 band;
  849. u32 child_handle;
  850. };
  851. struct tc_prio_qopt_offload {
  852. enum tc_prio_command command;
  853. u32 handle;
  854. u32 parent;
  855. union {
  856. struct tc_prio_qopt_offload_params replace_params;
  857. struct tc_qopt_offload_stats stats;
  858. struct tc_prio_qopt_offload_graft_params graft_params;
  859. };
  860. };
  861. enum tc_root_command {
  862. TC_ROOT_GRAFT,
  863. };
  864. struct tc_root_qopt_offload {
  865. enum tc_root_command command;
  866. u32 handle;
  867. bool ingress;
  868. };
  869. enum tc_ets_command {
  870. TC_ETS_REPLACE,
  871. TC_ETS_DESTROY,
  872. TC_ETS_STATS,
  873. TC_ETS_GRAFT,
  874. };
  875. struct tc_ets_qopt_offload_replace_params {
  876. unsigned int bands;
  877. u8 priomap[TC_PRIO_MAX + 1];
  878. unsigned int quanta[TCQ_ETS_MAX_BANDS]; /* 0 for strict bands. */
  879. unsigned int weights[TCQ_ETS_MAX_BANDS];
  880. struct gnet_stats_queue *qstats;
  881. };
  882. struct tc_ets_qopt_offload_graft_params {
  883. u8 band;
  884. u32 child_handle;
  885. };
  886. struct tc_ets_qopt_offload {
  887. enum tc_ets_command command;
  888. u32 handle;
  889. u32 parent;
  890. union {
  891. struct tc_ets_qopt_offload_replace_params replace_params;
  892. struct tc_qopt_offload_stats stats;
  893. struct tc_ets_qopt_offload_graft_params graft_params;
  894. };
  895. };
  896. enum tc_tbf_command {
  897. TC_TBF_REPLACE,
  898. TC_TBF_DESTROY,
  899. TC_TBF_STATS,
  900. TC_TBF_GRAFT,
  901. };
  902. struct tc_tbf_qopt_offload_replace_params {
  903. struct psched_ratecfg rate;
  904. u32 max_size;
  905. struct gnet_stats_queue *qstats;
  906. };
  907. struct tc_tbf_qopt_offload {
  908. enum tc_tbf_command command;
  909. u32 handle;
  910. u32 parent;
  911. union {
  912. struct tc_tbf_qopt_offload_replace_params replace_params;
  913. struct tc_qopt_offload_stats stats;
  914. u32 child_handle;
  915. };
  916. };
  917. enum tc_fifo_command {
  918. TC_FIFO_REPLACE,
  919. TC_FIFO_DESTROY,
  920. TC_FIFO_STATS,
  921. };
  922. struct tc_fifo_qopt_offload {
  923. enum tc_fifo_command command;
  924. u32 handle;
  925. u32 parent;
  926. union {
  927. struct tc_qopt_offload_stats stats;
  928. };
  929. };
  930. #ifdef CONFIG_NET_CLS_ACT
  931. DECLARE_STATIC_KEY_FALSE(tc_skb_ext_tc);
  932. void tc_skb_ext_tc_enable(void);
  933. void tc_skb_ext_tc_disable(void);
  934. #define tc_skb_ext_tc_enabled() static_branch_unlikely(&tc_skb_ext_tc)
  935. #else /* CONFIG_NET_CLS_ACT */
  936. static inline void tc_skb_ext_tc_enable(void) { }
  937. static inline void tc_skb_ext_tc_disable(void) { }
  938. #define tc_skb_ext_tc_enabled() false
  939. #endif
  940. #endif