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