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