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