1 #ifndef _NET_FLOW_OFFLOAD_H 2 #define _NET_FLOW_OFFLOAD_H 3 4 #include <linux/kernel.h> 5 #include <linux/list.h> 6 #include <linux/netlink.h> 7 #include <net/flow_dissector.h> 8 9 struct flow_match { 10 struct flow_dissector *dissector; 11 void *mask; 12 void *key; 13 }; 14 15 struct flow_match_meta { 16 struct flow_dissector_key_meta *key, *mask; 17 }; 18 19 struct flow_match_basic { 20 struct flow_dissector_key_basic *key, *mask; 21 }; 22 23 struct flow_match_control { 24 struct flow_dissector_key_control *key, *mask; 25 }; 26 27 struct flow_match_eth_addrs { 28 struct flow_dissector_key_eth_addrs *key, *mask; 29 }; 30 31 struct flow_match_vlan { 32 struct flow_dissector_key_vlan *key, *mask; 33 }; 34 35 struct flow_match_ipv4_addrs { 36 struct flow_dissector_key_ipv4_addrs *key, *mask; 37 }; 38 39 struct flow_match_ipv6_addrs { 40 struct flow_dissector_key_ipv6_addrs *key, *mask; 41 }; 42 43 struct flow_match_ip { 44 struct flow_dissector_key_ip *key, *mask; 45 }; 46 47 struct flow_match_ports { 48 struct flow_dissector_key_ports *key, *mask; 49 }; 50 51 struct flow_match_ports_range { 52 struct flow_dissector_key_ports_range *key, *mask; 53 }; 54 55 struct flow_match_icmp { 56 struct flow_dissector_key_icmp *key, *mask; 57 }; 58 59 struct flow_match_tcp { 60 struct flow_dissector_key_tcp *key, *mask; 61 }; 62 63 struct flow_match_mpls { 64 struct flow_dissector_key_mpls *key, *mask; 65 }; 66 67 struct flow_match_enc_keyid { 68 struct flow_dissector_key_keyid *key, *mask; 69 }; 70 71 struct flow_match_enc_opts { 72 struct flow_dissector_key_enc_opts *key, *mask; 73 }; 74 75 struct flow_match_ct { 76 struct flow_dissector_key_ct *key, *mask; 77 }; 78 79 struct flow_match_pppoe { 80 struct flow_dissector_key_pppoe *key, *mask; 81 }; 82 83 struct flow_rule; 84 85 void flow_rule_match_meta(const struct flow_rule *rule, 86 struct flow_match_meta *out); 87 void flow_rule_match_basic(const struct flow_rule *rule, 88 struct flow_match_basic *out); 89 void flow_rule_match_control(const struct flow_rule *rule, 90 struct flow_match_control *out); 91 void flow_rule_match_eth_addrs(const struct flow_rule *rule, 92 struct flow_match_eth_addrs *out); 93 void flow_rule_match_vlan(const struct flow_rule *rule, 94 struct flow_match_vlan *out); 95 void flow_rule_match_cvlan(const struct flow_rule *rule, 96 struct flow_match_vlan *out); 97 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule, 98 struct flow_match_ipv4_addrs *out); 99 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule, 100 struct flow_match_ipv6_addrs *out); 101 void flow_rule_match_ip(const struct flow_rule *rule, 102 struct flow_match_ip *out); 103 void flow_rule_match_ports(const struct flow_rule *rule, 104 struct flow_match_ports *out); 105 void flow_rule_match_ports_range(const struct flow_rule *rule, 106 struct flow_match_ports_range *out); 107 void flow_rule_match_tcp(const struct flow_rule *rule, 108 struct flow_match_tcp *out); 109 void flow_rule_match_icmp(const struct flow_rule *rule, 110 struct flow_match_icmp *out); 111 void flow_rule_match_mpls(const struct flow_rule *rule, 112 struct flow_match_mpls *out); 113 void flow_rule_match_enc_control(const struct flow_rule *rule, 114 struct flow_match_control *out); 115 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule, 116 struct flow_match_ipv4_addrs *out); 117 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule, 118 struct flow_match_ipv6_addrs *out); 119 void flow_rule_match_enc_ip(const struct flow_rule *rule, 120 struct flow_match_ip *out); 121 void flow_rule_match_enc_ports(const struct flow_rule *rule, 122 struct flow_match_ports *out); 123 void flow_rule_match_enc_keyid(const struct flow_rule *rule, 124 struct flow_match_enc_keyid *out); 125 void flow_rule_match_enc_opts(const struct flow_rule *rule, 126 struct flow_match_enc_opts *out); 127 void flow_rule_match_ct(const struct flow_rule *rule, 128 struct flow_match_ct *out); 129 void flow_rule_match_pppoe(const struct flow_rule *rule, 130 struct flow_match_pppoe *out); 131 132 enum flow_action_id { 133 FLOW_ACTION_ACCEPT = 0, 134 FLOW_ACTION_DROP, 135 FLOW_ACTION_TRAP, 136 FLOW_ACTION_GOTO, 137 FLOW_ACTION_REDIRECT, 138 FLOW_ACTION_MIRRED, 139 FLOW_ACTION_REDIRECT_INGRESS, 140 FLOW_ACTION_MIRRED_INGRESS, 141 FLOW_ACTION_VLAN_PUSH, 142 FLOW_ACTION_VLAN_POP, 143 FLOW_ACTION_VLAN_MANGLE, 144 FLOW_ACTION_TUNNEL_ENCAP, 145 FLOW_ACTION_TUNNEL_DECAP, 146 FLOW_ACTION_MANGLE, 147 FLOW_ACTION_ADD, 148 FLOW_ACTION_CSUM, 149 FLOW_ACTION_MARK, 150 FLOW_ACTION_PTYPE, 151 FLOW_ACTION_PRIORITY, 152 FLOW_ACTION_WAKE, 153 FLOW_ACTION_QUEUE, 154 FLOW_ACTION_SAMPLE, 155 FLOW_ACTION_POLICE, 156 FLOW_ACTION_CT, 157 FLOW_ACTION_CT_METADATA, 158 FLOW_ACTION_MPLS_PUSH, 159 FLOW_ACTION_MPLS_POP, 160 FLOW_ACTION_MPLS_MANGLE, 161 FLOW_ACTION_GATE, 162 FLOW_ACTION_PPPOE_PUSH, 163 FLOW_ACTION_JUMP, 164 FLOW_ACTION_PIPE, 165 FLOW_ACTION_VLAN_PUSH_ETH, 166 FLOW_ACTION_VLAN_POP_ETH, 167 FLOW_ACTION_CONTINUE, 168 NUM_FLOW_ACTIONS, 169 }; 170 171 /* This is mirroring enum pedit_header_type definition for easy mapping between 172 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to 173 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver. 174 */ 175 enum flow_action_mangle_base { 176 FLOW_ACT_MANGLE_UNSPEC = 0, 177 FLOW_ACT_MANGLE_HDR_TYPE_ETH, 178 FLOW_ACT_MANGLE_HDR_TYPE_IP4, 179 FLOW_ACT_MANGLE_HDR_TYPE_IP6, 180 FLOW_ACT_MANGLE_HDR_TYPE_TCP, 181 FLOW_ACT_MANGLE_HDR_TYPE_UDP, 182 }; 183 184 enum flow_action_hw_stats_bit { 185 FLOW_ACTION_HW_STATS_IMMEDIATE_BIT, 186 FLOW_ACTION_HW_STATS_DELAYED_BIT, 187 FLOW_ACTION_HW_STATS_DISABLED_BIT, 188 189 FLOW_ACTION_HW_STATS_NUM_BITS 190 }; 191 192 enum flow_action_hw_stats { 193 FLOW_ACTION_HW_STATS_IMMEDIATE = 194 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT), 195 FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT), 196 FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE | 197 FLOW_ACTION_HW_STATS_DELAYED, 198 FLOW_ACTION_HW_STATS_DISABLED = 199 BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT), 200 FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1, 201 }; 202 203 typedef void (*action_destr)(void *priv); 204 205 struct flow_action_cookie { 206 u32 cookie_len; 207 u8 cookie[]; 208 }; 209 210 struct flow_action_cookie *flow_action_cookie_create(void *data, 211 unsigned int len, 212 gfp_t gfp); 213 void flow_action_cookie_destroy(struct flow_action_cookie *cookie); 214 215 struct flow_action_entry { 216 enum flow_action_id id; 217 u32 hw_index; 218 enum flow_action_hw_stats hw_stats; 219 action_destr destructor; 220 void *destructor_priv; 221 union { 222 u32 chain_index; /* FLOW_ACTION_GOTO */ 223 struct net_device *dev; /* FLOW_ACTION_REDIRECT */ 224 struct { /* FLOW_ACTION_VLAN */ 225 u16 vid; 226 __be16 proto; 227 u8 prio; 228 } vlan; 229 struct { /* FLOW_ACTION_VLAN_PUSH_ETH */ 230 unsigned char dst[ETH_ALEN]; 231 unsigned char src[ETH_ALEN]; 232 } vlan_push_eth; 233 struct { /* FLOW_ACTION_MANGLE */ 234 /* FLOW_ACTION_ADD */ 235 enum flow_action_mangle_base htype; 236 u32 offset; 237 u32 mask; 238 u32 val; 239 } mangle; 240 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */ 241 u32 csum_flags; /* FLOW_ACTION_CSUM */ 242 u32 mark; /* FLOW_ACTION_MARK */ 243 u16 ptype; /* FLOW_ACTION_PTYPE */ 244 u32 priority; /* FLOW_ACTION_PRIORITY */ 245 struct { /* FLOW_ACTION_QUEUE */ 246 u32 ctx; 247 u32 index; 248 u8 vf; 249 } queue; 250 struct { /* FLOW_ACTION_SAMPLE */ 251 struct psample_group *psample_group; 252 u32 rate; 253 u32 trunc_size; 254 bool truncate; 255 } sample; 256 struct { /* FLOW_ACTION_POLICE */ 257 u32 burst; 258 u64 rate_bytes_ps; 259 u64 peakrate_bytes_ps; 260 u32 avrate; 261 u16 overhead; 262 u64 burst_pkt; 263 u64 rate_pkt_ps; 264 u32 mtu; 265 struct { 266 enum flow_action_id act_id; 267 u32 extval; 268 } exceed, notexceed; 269 } police; 270 struct { /* FLOW_ACTION_CT */ 271 int action; 272 u16 zone; 273 struct nf_flowtable *flow_table; 274 } ct; 275 struct { 276 unsigned long cookie; 277 u32 mark; 278 u32 labels[4]; 279 bool orig_dir; 280 } ct_metadata; 281 struct { /* FLOW_ACTION_MPLS_PUSH */ 282 u32 label; 283 __be16 proto; 284 u8 tc; 285 u8 bos; 286 u8 ttl; 287 } mpls_push; 288 struct { /* FLOW_ACTION_MPLS_POP */ 289 __be16 proto; 290 } mpls_pop; 291 struct { /* FLOW_ACTION_MPLS_MANGLE */ 292 u32 label; 293 u8 tc; 294 u8 bos; 295 u8 ttl; 296 } mpls_mangle; 297 struct { 298 s32 prio; 299 u64 basetime; 300 u64 cycletime; 301 u64 cycletimeext; 302 u32 num_entries; 303 struct action_gate_entry *entries; 304 } gate; 305 struct { /* FLOW_ACTION_PPPOE_PUSH */ 306 u16 sid; 307 } pppoe; 308 }; 309 struct flow_action_cookie *cookie; /* user defined action cookie */ 310 }; 311 312 struct flow_action { 313 unsigned int num_entries; 314 struct flow_action_entry entries[]; 315 }; 316 317 static inline bool flow_action_has_entries(const struct flow_action *action) 318 { 319 return action->num_entries; 320 } 321 322 /** 323 * flow_offload_has_one_action() - check if exactly one action is present 324 * @action: tc filter flow offload action 325 * 326 * Returns true if exactly one action is present. 327 */ 328 static inline bool flow_offload_has_one_action(const struct flow_action *action) 329 { 330 return action->num_entries == 1; 331 } 332 333 static inline bool flow_action_is_last_entry(const struct flow_action *action, 334 const struct flow_action_entry *entry) 335 { 336 return entry == &action->entries[action->num_entries - 1]; 337 } 338 339 #define flow_action_for_each(__i, __act, __actions) \ 340 for (__i = 0, __act = &(__actions)->entries[0]; \ 341 __i < (__actions)->num_entries; \ 342 __act = &(__actions)->entries[++__i]) 343 344 static inline bool 345 flow_action_mixed_hw_stats_check(const struct flow_action *action, 346 struct netlink_ext_ack *extack) 347 { 348 const struct flow_action_entry *action_entry; 349 u8 last_hw_stats; 350 int i; 351 352 if (flow_offload_has_one_action(action)) 353 return true; 354 355 flow_action_for_each(i, action_entry, action) { 356 if (i && action_entry->hw_stats != last_hw_stats) { 357 NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported"); 358 return false; 359 } 360 last_hw_stats = action_entry->hw_stats; 361 } 362 return true; 363 } 364 365 static inline const struct flow_action_entry * 366 flow_action_first_entry_get(const struct flow_action *action) 367 { 368 WARN_ON(!flow_action_has_entries(action)); 369 return &action->entries[0]; 370 } 371 372 static inline bool 373 __flow_action_hw_stats_check(const struct flow_action *action, 374 struct netlink_ext_ack *extack, 375 bool check_allow_bit, 376 enum flow_action_hw_stats_bit allow_bit) 377 { 378 const struct flow_action_entry *action_entry; 379 380 if (!flow_action_has_entries(action)) 381 return true; 382 if (!flow_action_mixed_hw_stats_check(action, extack)) 383 return false; 384 385 action_entry = flow_action_first_entry_get(action); 386 387 /* Zero is not a legal value for hw_stats, catch anyone passing it */ 388 WARN_ON_ONCE(!action_entry->hw_stats); 389 390 if (!check_allow_bit && 391 ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) { 392 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\""); 393 return false; 394 } else if (check_allow_bit && 395 !(action_entry->hw_stats & BIT(allow_bit))) { 396 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type"); 397 return false; 398 } 399 return true; 400 } 401 402 static inline bool 403 flow_action_hw_stats_check(const struct flow_action *action, 404 struct netlink_ext_ack *extack, 405 enum flow_action_hw_stats_bit allow_bit) 406 { 407 return __flow_action_hw_stats_check(action, extack, true, allow_bit); 408 } 409 410 static inline bool 411 flow_action_basic_hw_stats_check(const struct flow_action *action, 412 struct netlink_ext_ack *extack) 413 { 414 return __flow_action_hw_stats_check(action, extack, false, 0); 415 } 416 417 struct flow_rule { 418 struct flow_match match; 419 struct flow_action action; 420 }; 421 422 struct flow_rule *flow_rule_alloc(unsigned int num_actions); 423 424 static inline bool flow_rule_match_key(const struct flow_rule *rule, 425 enum flow_dissector_key_id key) 426 { 427 return dissector_uses_key(rule->match.dissector, key); 428 } 429 430 struct flow_stats { 431 u64 pkts; 432 u64 bytes; 433 u64 drops; 434 u64 lastused; 435 enum flow_action_hw_stats used_hw_stats; 436 bool used_hw_stats_valid; 437 }; 438 439 static inline void flow_stats_update(struct flow_stats *flow_stats, 440 u64 bytes, u64 pkts, 441 u64 drops, u64 lastused, 442 enum flow_action_hw_stats used_hw_stats) 443 { 444 flow_stats->pkts += pkts; 445 flow_stats->bytes += bytes; 446 flow_stats->drops += drops; 447 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused); 448 449 /* The driver should pass value with a maximum of one bit set. 450 * Passing FLOW_ACTION_HW_STATS_ANY is invalid. 451 */ 452 WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY); 453 flow_stats->used_hw_stats |= used_hw_stats; 454 flow_stats->used_hw_stats_valid = true; 455 } 456 457 enum flow_block_command { 458 FLOW_BLOCK_BIND, 459 FLOW_BLOCK_UNBIND, 460 }; 461 462 enum flow_block_binder_type { 463 FLOW_BLOCK_BINDER_TYPE_UNSPEC, 464 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS, 465 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS, 466 FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP, 467 FLOW_BLOCK_BINDER_TYPE_RED_MARK, 468 }; 469 470 struct flow_block { 471 struct list_head cb_list; 472 }; 473 474 struct netlink_ext_ack; 475 476 struct flow_block_offload { 477 enum flow_block_command command; 478 enum flow_block_binder_type binder_type; 479 bool block_shared; 480 bool unlocked_driver_cb; 481 struct net *net; 482 struct flow_block *block; 483 struct list_head cb_list; 484 struct list_head *driver_block_list; 485 struct netlink_ext_ack *extack; 486 struct Qdisc *sch; 487 struct list_head *cb_list_head; 488 }; 489 490 enum tc_setup_type; 491 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data, 492 void *cb_priv); 493 494 struct flow_block_cb; 495 496 struct flow_block_indr { 497 struct list_head list; 498 struct net_device *dev; 499 struct Qdisc *sch; 500 enum flow_block_binder_type binder_type; 501 void *data; 502 void *cb_priv; 503 void (*cleanup)(struct flow_block_cb *block_cb); 504 }; 505 506 struct flow_block_cb { 507 struct list_head driver_list; 508 struct list_head list; 509 flow_setup_cb_t *cb; 510 void *cb_ident; 511 void *cb_priv; 512 void (*release)(void *cb_priv); 513 struct flow_block_indr indr; 514 unsigned int refcnt; 515 }; 516 517 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb, 518 void *cb_ident, void *cb_priv, 519 void (*release)(void *cb_priv)); 520 struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb, 521 void *cb_ident, void *cb_priv, 522 void (*release)(void *cb_priv), 523 struct flow_block_offload *bo, 524 struct net_device *dev, 525 struct Qdisc *sch, void *data, 526 void *indr_cb_priv, 527 void (*cleanup)(struct flow_block_cb *block_cb)); 528 void flow_block_cb_free(struct flow_block_cb *block_cb); 529 530 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block, 531 flow_setup_cb_t *cb, void *cb_ident); 532 533 void *flow_block_cb_priv(struct flow_block_cb *block_cb); 534 void flow_block_cb_incref(struct flow_block_cb *block_cb); 535 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb); 536 537 static inline void flow_block_cb_add(struct flow_block_cb *block_cb, 538 struct flow_block_offload *offload) 539 { 540 list_add_tail(&block_cb->list, &offload->cb_list); 541 } 542 543 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb, 544 struct flow_block_offload *offload) 545 { 546 list_move(&block_cb->list, &offload->cb_list); 547 } 548 549 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb, 550 struct flow_block_offload *offload) 551 { 552 list_del(&block_cb->indr.list); 553 list_move(&block_cb->list, &offload->cb_list); 554 } 555 556 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident, 557 struct list_head *driver_block_list); 558 559 int flow_block_cb_setup_simple(struct flow_block_offload *f, 560 struct list_head *driver_list, 561 flow_setup_cb_t *cb, 562 void *cb_ident, void *cb_priv, bool ingress_only); 563 564 enum flow_cls_command { 565 FLOW_CLS_REPLACE, 566 FLOW_CLS_DESTROY, 567 FLOW_CLS_STATS, 568 FLOW_CLS_TMPLT_CREATE, 569 FLOW_CLS_TMPLT_DESTROY, 570 }; 571 572 struct flow_cls_common_offload { 573 u32 chain_index; 574 __be16 protocol; 575 u32 prio; 576 struct netlink_ext_ack *extack; 577 }; 578 579 struct flow_cls_offload { 580 struct flow_cls_common_offload common; 581 enum flow_cls_command command; 582 unsigned long cookie; 583 struct flow_rule *rule; 584 struct flow_stats stats; 585 u32 classid; 586 }; 587 588 enum offload_act_command { 589 FLOW_ACT_REPLACE, 590 FLOW_ACT_DESTROY, 591 FLOW_ACT_STATS, 592 }; 593 594 struct flow_offload_action { 595 struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/ 596 enum offload_act_command command; 597 enum flow_action_id id; 598 u32 index; 599 struct flow_stats stats; 600 struct flow_action action; 601 }; 602 603 struct flow_offload_action *offload_action_alloc(unsigned int num_actions); 604 605 static inline struct flow_rule * 606 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd) 607 { 608 return flow_cmd->rule; 609 } 610 611 static inline void flow_block_init(struct flow_block *flow_block) 612 { 613 INIT_LIST_HEAD(&flow_block->cb_list); 614 } 615 616 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv, 617 enum tc_setup_type type, void *type_data, 618 void *data, 619 void (*cleanup)(struct flow_block_cb *block_cb)); 620 621 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv); 622 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv, 623 void (*release)(void *cb_priv)); 624 int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch, 625 enum tc_setup_type type, void *data, 626 struct flow_block_offload *bo, 627 void (*cleanup)(struct flow_block_cb *block_cb)); 628 bool flow_indr_dev_exists(void); 629 630 #endif /* _NET_FLOW_OFFLOAD_H */ 631