1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ 3 4 #include <linux/skbuff.h> 5 #include <net/devlink.h> 6 #include <net/pkt_cls.h> 7 8 #include "cmsg.h" 9 #include "main.h" 10 #include "../nfpcore/nfp_cpp.h" 11 #include "../nfpcore/nfp_nsp.h" 12 #include "../nfp_app.h" 13 #include "../nfp_main.h" 14 #include "../nfp_net.h" 15 #include "../nfp_port.h" 16 17 #define NFP_FLOWER_SUPPORTED_TCPFLAGS \ 18 (TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST | \ 19 TCPHDR_PSH | TCPHDR_URG) 20 21 #define NFP_FLOWER_SUPPORTED_CTLFLAGS \ 22 (FLOW_DIS_IS_FRAGMENT | \ 23 FLOW_DIS_FIRST_FRAG) 24 25 #define NFP_FLOWER_WHITELIST_DISSECTOR \ 26 (BIT(FLOW_DISSECTOR_KEY_CONTROL) | \ 27 BIT(FLOW_DISSECTOR_KEY_BASIC) | \ 28 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | \ 29 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | \ 30 BIT(FLOW_DISSECTOR_KEY_TCP) | \ 31 BIT(FLOW_DISSECTOR_KEY_PORTS) | \ 32 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | \ 33 BIT(FLOW_DISSECTOR_KEY_VLAN) | \ 34 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \ 35 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \ 36 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \ 37 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \ 38 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \ 39 BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) | \ 40 BIT(FLOW_DISSECTOR_KEY_ENC_IP) | \ 41 BIT(FLOW_DISSECTOR_KEY_MPLS) | \ 42 BIT(FLOW_DISSECTOR_KEY_IP)) 43 44 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR \ 45 (BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \ 46 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \ 47 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \ 48 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \ 49 BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) | \ 50 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \ 51 BIT(FLOW_DISSECTOR_KEY_ENC_IP)) 52 53 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R \ 54 (BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \ 55 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) 56 57 #define NFP_FLOWER_MERGE_FIELDS \ 58 (NFP_FLOWER_LAYER_PORT | \ 59 NFP_FLOWER_LAYER_MAC | \ 60 NFP_FLOWER_LAYER_TP | \ 61 NFP_FLOWER_LAYER_IPV4 | \ 62 NFP_FLOWER_LAYER_IPV6) 63 64 struct nfp_flower_merge_check { 65 union { 66 struct { 67 __be16 tci; 68 struct nfp_flower_mac_mpls l2; 69 struct nfp_flower_tp_ports l4; 70 union { 71 struct nfp_flower_ipv4 ipv4; 72 struct nfp_flower_ipv6 ipv6; 73 }; 74 }; 75 unsigned long vals[8]; 76 }; 77 }; 78 79 static int 80 nfp_flower_xmit_flow(struct nfp_app *app, struct nfp_fl_payload *nfp_flow, 81 u8 mtype) 82 { 83 u32 meta_len, key_len, mask_len, act_len, tot_len; 84 struct sk_buff *skb; 85 unsigned char *msg; 86 87 meta_len = sizeof(struct nfp_fl_rule_metadata); 88 key_len = nfp_flow->meta.key_len; 89 mask_len = nfp_flow->meta.mask_len; 90 act_len = nfp_flow->meta.act_len; 91 92 tot_len = meta_len + key_len + mask_len + act_len; 93 94 /* Convert to long words as firmware expects 95 * lengths in units of NFP_FL_LW_SIZ. 96 */ 97 nfp_flow->meta.key_len >>= NFP_FL_LW_SIZ; 98 nfp_flow->meta.mask_len >>= NFP_FL_LW_SIZ; 99 nfp_flow->meta.act_len >>= NFP_FL_LW_SIZ; 100 101 skb = nfp_flower_cmsg_alloc(app, tot_len, mtype, GFP_KERNEL); 102 if (!skb) 103 return -ENOMEM; 104 105 msg = nfp_flower_cmsg_get_data(skb); 106 memcpy(msg, &nfp_flow->meta, meta_len); 107 memcpy(&msg[meta_len], nfp_flow->unmasked_data, key_len); 108 memcpy(&msg[meta_len + key_len], nfp_flow->mask_data, mask_len); 109 memcpy(&msg[meta_len + key_len + mask_len], 110 nfp_flow->action_data, act_len); 111 112 /* Convert back to bytes as software expects 113 * lengths in units of bytes. 114 */ 115 nfp_flow->meta.key_len <<= NFP_FL_LW_SIZ; 116 nfp_flow->meta.mask_len <<= NFP_FL_LW_SIZ; 117 nfp_flow->meta.act_len <<= NFP_FL_LW_SIZ; 118 119 nfp_ctrl_tx(app->ctrl, skb); 120 121 return 0; 122 } 123 124 static bool nfp_flower_check_higher_than_mac(struct tc_cls_flower_offload *f) 125 { 126 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f); 127 128 return flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS) || 129 flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS) || 130 flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS) || 131 flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP); 132 } 133 134 static bool nfp_flower_check_higher_than_l3(struct tc_cls_flower_offload *f) 135 { 136 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f); 137 138 return flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS) || 139 flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP); 140 } 141 142 static int 143 nfp_flower_calc_opt_layer(struct flow_dissector_key_enc_opts *enc_opts, 144 u32 *key_layer_two, int *key_size, 145 struct netlink_ext_ack *extack) 146 { 147 if (enc_opts->len > NFP_FL_MAX_GENEVE_OPT_KEY) { 148 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: geneve options exceed maximum length"); 149 return -EOPNOTSUPP; 150 } 151 152 if (enc_opts->len > 0) { 153 *key_layer_two |= NFP_FLOWER_LAYER2_GENEVE_OP; 154 *key_size += sizeof(struct nfp_flower_geneve_options); 155 } 156 157 return 0; 158 } 159 160 static int 161 nfp_flower_calc_udp_tun_layer(struct flow_dissector_key_ports *enc_ports, 162 struct flow_dissector_key_enc_opts *enc_op, 163 u32 *key_layer_two, u8 *key_layer, int *key_size, 164 struct nfp_flower_priv *priv, 165 enum nfp_flower_tun_type *tun_type, 166 struct netlink_ext_ack *extack) 167 { 168 int err; 169 170 switch (enc_ports->dst) { 171 case htons(IANA_VXLAN_UDP_PORT): 172 *tun_type = NFP_FL_TUNNEL_VXLAN; 173 *key_layer |= NFP_FLOWER_LAYER_VXLAN; 174 *key_size += sizeof(struct nfp_flower_ipv4_udp_tun); 175 176 if (enc_op) { 177 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: encap options not supported on vxlan tunnels"); 178 return -EOPNOTSUPP; 179 } 180 break; 181 case htons(GENEVE_UDP_PORT): 182 if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE)) { 183 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support geneve offload"); 184 return -EOPNOTSUPP; 185 } 186 *tun_type = NFP_FL_TUNNEL_GENEVE; 187 *key_layer |= NFP_FLOWER_LAYER_EXT_META; 188 *key_size += sizeof(struct nfp_flower_ext_meta); 189 *key_layer_two |= NFP_FLOWER_LAYER2_GENEVE; 190 *key_size += sizeof(struct nfp_flower_ipv4_udp_tun); 191 192 if (!enc_op) 193 break; 194 if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE_OPT)) { 195 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support geneve option offload"); 196 return -EOPNOTSUPP; 197 } 198 err = nfp_flower_calc_opt_layer(enc_op, key_layer_two, 199 key_size, extack); 200 if (err) 201 return err; 202 break; 203 default: 204 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: tunnel type unknown"); 205 return -EOPNOTSUPP; 206 } 207 208 return 0; 209 } 210 211 static int 212 nfp_flower_calculate_key_layers(struct nfp_app *app, 213 struct net_device *netdev, 214 struct nfp_fl_key_ls *ret_key_ls, 215 struct tc_cls_flower_offload *flow, 216 enum nfp_flower_tun_type *tun_type, 217 struct netlink_ext_ack *extack) 218 { 219 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(flow); 220 struct flow_dissector *dissector = rule->match.dissector; 221 struct flow_match_basic basic = { NULL, NULL}; 222 struct nfp_flower_priv *priv = app->priv; 223 u32 key_layer_two; 224 u8 key_layer; 225 int key_size; 226 int err; 227 228 if (dissector->used_keys & ~NFP_FLOWER_WHITELIST_DISSECTOR) { 229 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match not supported"); 230 return -EOPNOTSUPP; 231 } 232 233 /* If any tun dissector is used then the required set must be used. */ 234 if (dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR && 235 (dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R) 236 != NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R) { 237 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: tunnel match not supported"); 238 return -EOPNOTSUPP; 239 } 240 241 key_layer_two = 0; 242 key_layer = NFP_FLOWER_LAYER_PORT; 243 key_size = sizeof(struct nfp_flower_meta_tci) + 244 sizeof(struct nfp_flower_in_port); 245 246 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS) || 247 flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_MPLS)) { 248 key_layer |= NFP_FLOWER_LAYER_MAC; 249 key_size += sizeof(struct nfp_flower_mac_mpls); 250 } 251 252 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) { 253 struct flow_match_vlan vlan; 254 255 flow_rule_match_vlan(rule, &vlan); 256 if (!(priv->flower_ext_feats & NFP_FL_FEATS_VLAN_PCP) && 257 vlan.key->vlan_priority) { 258 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support VLAN PCP offload"); 259 return -EOPNOTSUPP; 260 } 261 } 262 263 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL)) { 264 struct flow_match_enc_opts enc_op = { NULL, NULL }; 265 struct flow_match_ipv4_addrs ipv4_addrs; 266 struct flow_match_control enc_ctl; 267 struct flow_match_ports enc_ports; 268 269 flow_rule_match_enc_control(rule, &enc_ctl); 270 271 if (enc_ctl.mask->addr_type != 0xffff) { 272 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: wildcarded protocols on tunnels are not supported"); 273 return -EOPNOTSUPP; 274 } 275 if (enc_ctl.key->addr_type != FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 276 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: only IPv4 tunnels are supported"); 277 return -EOPNOTSUPP; 278 } 279 280 /* These fields are already verified as used. */ 281 flow_rule_match_enc_ipv4_addrs(rule, &ipv4_addrs); 282 if (ipv4_addrs.mask->dst != cpu_to_be32(~0)) { 283 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: only an exact match IPv4 destination address is supported"); 284 return -EOPNOTSUPP; 285 } 286 287 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) 288 flow_rule_match_enc_opts(rule, &enc_op); 289 290 291 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS)) { 292 /* check if GRE, which has no enc_ports */ 293 if (netif_is_gretap(netdev)) { 294 *tun_type = NFP_FL_TUNNEL_GRE; 295 key_layer |= NFP_FLOWER_LAYER_EXT_META; 296 key_size += sizeof(struct nfp_flower_ext_meta); 297 key_layer_two |= NFP_FLOWER_LAYER2_GRE; 298 key_size += 299 sizeof(struct nfp_flower_ipv4_gre_tun); 300 301 if (enc_op.key) { 302 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: encap options not supported on GRE tunnels"); 303 return -EOPNOTSUPP; 304 } 305 } else { 306 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: an exact match on L4 destination port is required for non-GRE tunnels"); 307 return -EOPNOTSUPP; 308 } 309 } else { 310 flow_rule_match_enc_ports(rule, &enc_ports); 311 if (enc_ports.mask->dst != cpu_to_be16(~0)) { 312 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: only an exact match L4 destination port is supported"); 313 return -EOPNOTSUPP; 314 } 315 316 err = nfp_flower_calc_udp_tun_layer(enc_ports.key, 317 enc_op.key, 318 &key_layer_two, 319 &key_layer, 320 &key_size, priv, 321 tun_type, extack); 322 if (err) 323 return err; 324 325 /* Ensure the ingress netdev matches the expected 326 * tun type. 327 */ 328 if (!nfp_fl_netdev_is_tunnel_type(netdev, *tun_type)) { 329 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: ingress netdev does not match the expected tunnel type"); 330 return -EOPNOTSUPP; 331 } 332 } 333 } 334 335 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) 336 flow_rule_match_basic(rule, &basic); 337 338 if (basic.mask && basic.mask->n_proto) { 339 /* Ethernet type is present in the key. */ 340 switch (basic.key->n_proto) { 341 case cpu_to_be16(ETH_P_IP): 342 key_layer |= NFP_FLOWER_LAYER_IPV4; 343 key_size += sizeof(struct nfp_flower_ipv4); 344 break; 345 346 case cpu_to_be16(ETH_P_IPV6): 347 key_layer |= NFP_FLOWER_LAYER_IPV6; 348 key_size += sizeof(struct nfp_flower_ipv6); 349 break; 350 351 /* Currently we do not offload ARP 352 * because we rely on it to get to the host. 353 */ 354 case cpu_to_be16(ETH_P_ARP): 355 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: ARP not supported"); 356 return -EOPNOTSUPP; 357 358 case cpu_to_be16(ETH_P_MPLS_UC): 359 case cpu_to_be16(ETH_P_MPLS_MC): 360 if (!(key_layer & NFP_FLOWER_LAYER_MAC)) { 361 key_layer |= NFP_FLOWER_LAYER_MAC; 362 key_size += sizeof(struct nfp_flower_mac_mpls); 363 } 364 break; 365 366 /* Will be included in layer 2. */ 367 case cpu_to_be16(ETH_P_8021Q): 368 break; 369 370 default: 371 /* Other ethtype - we need check the masks for the 372 * remainder of the key to ensure we can offload. 373 */ 374 if (nfp_flower_check_higher_than_mac(flow)) { 375 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: non IPv4/IPv6 offload with L3/L4 matches not supported"); 376 return -EOPNOTSUPP; 377 } 378 break; 379 } 380 } 381 382 if (basic.mask && basic.mask->ip_proto) { 383 switch (basic.key->ip_proto) { 384 case IPPROTO_TCP: 385 case IPPROTO_UDP: 386 case IPPROTO_SCTP: 387 case IPPROTO_ICMP: 388 case IPPROTO_ICMPV6: 389 key_layer |= NFP_FLOWER_LAYER_TP; 390 key_size += sizeof(struct nfp_flower_tp_ports); 391 break; 392 default: 393 /* Other ip proto - we need check the masks for the 394 * remainder of the key to ensure we can offload. 395 */ 396 if (nfp_flower_check_higher_than_l3(flow)) { 397 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: unknown IP protocol with L4 matches not supported"); 398 return -EOPNOTSUPP; 399 } 400 break; 401 } 402 } 403 404 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_TCP)) { 405 struct flow_match_tcp tcp; 406 u32 tcp_flags; 407 408 flow_rule_match_tcp(rule, &tcp); 409 tcp_flags = be16_to_cpu(tcp.key->flags); 410 411 if (tcp_flags & ~NFP_FLOWER_SUPPORTED_TCPFLAGS) { 412 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: no match support for selected TCP flags"); 413 return -EOPNOTSUPP; 414 } 415 416 /* We only support PSH and URG flags when either 417 * FIN, SYN or RST is present as well. 418 */ 419 if ((tcp_flags & (TCPHDR_PSH | TCPHDR_URG)) && 420 !(tcp_flags & (TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST))) { 421 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: PSH and URG is only supported when used with FIN, SYN or RST"); 422 return -EOPNOTSUPP; 423 } 424 425 /* We need to store TCP flags in the either the IPv4 or IPv6 key 426 * space, thus we need to ensure we include a IPv4/IPv6 key 427 * layer if we have not done so already. 428 */ 429 if (!basic.key) { 430 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on TCP flags requires a match on L3 protocol"); 431 return -EOPNOTSUPP; 432 } 433 434 if (!(key_layer & NFP_FLOWER_LAYER_IPV4) && 435 !(key_layer & NFP_FLOWER_LAYER_IPV6)) { 436 switch (basic.key->n_proto) { 437 case cpu_to_be16(ETH_P_IP): 438 key_layer |= NFP_FLOWER_LAYER_IPV4; 439 key_size += sizeof(struct nfp_flower_ipv4); 440 break; 441 442 case cpu_to_be16(ETH_P_IPV6): 443 key_layer |= NFP_FLOWER_LAYER_IPV6; 444 key_size += sizeof(struct nfp_flower_ipv6); 445 break; 446 447 default: 448 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on TCP flags requires a match on IPv4/IPv6"); 449 return -EOPNOTSUPP; 450 } 451 } 452 } 453 454 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) { 455 struct flow_match_control ctl; 456 457 flow_rule_match_control(rule, &ctl); 458 if (ctl.key->flags & ~NFP_FLOWER_SUPPORTED_CTLFLAGS) { 459 NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on unknown control flag"); 460 return -EOPNOTSUPP; 461 } 462 } 463 464 ret_key_ls->key_layer = key_layer; 465 ret_key_ls->key_layer_two = key_layer_two; 466 ret_key_ls->key_size = key_size; 467 468 return 0; 469 } 470 471 static struct nfp_fl_payload * 472 nfp_flower_allocate_new(struct nfp_fl_key_ls *key_layer) 473 { 474 struct nfp_fl_payload *flow_pay; 475 476 flow_pay = kmalloc(sizeof(*flow_pay), GFP_KERNEL); 477 if (!flow_pay) 478 return NULL; 479 480 flow_pay->meta.key_len = key_layer->key_size; 481 flow_pay->unmasked_data = kmalloc(key_layer->key_size, GFP_KERNEL); 482 if (!flow_pay->unmasked_data) 483 goto err_free_flow; 484 485 flow_pay->meta.mask_len = key_layer->key_size; 486 flow_pay->mask_data = kmalloc(key_layer->key_size, GFP_KERNEL); 487 if (!flow_pay->mask_data) 488 goto err_free_unmasked; 489 490 flow_pay->action_data = kmalloc(NFP_FL_MAX_A_SIZ, GFP_KERNEL); 491 if (!flow_pay->action_data) 492 goto err_free_mask; 493 494 flow_pay->nfp_tun_ipv4_addr = 0; 495 flow_pay->meta.flags = 0; 496 INIT_LIST_HEAD(&flow_pay->linked_flows); 497 flow_pay->in_hw = false; 498 499 return flow_pay; 500 501 err_free_mask: 502 kfree(flow_pay->mask_data); 503 err_free_unmasked: 504 kfree(flow_pay->unmasked_data); 505 err_free_flow: 506 kfree(flow_pay); 507 return NULL; 508 } 509 510 static int 511 nfp_flower_update_merge_with_actions(struct nfp_fl_payload *flow, 512 struct nfp_flower_merge_check *merge, 513 u8 *last_act_id, int *act_out) 514 { 515 struct nfp_fl_set_ipv6_tc_hl_fl *ipv6_tc_hl_fl; 516 struct nfp_fl_set_ip4_ttl_tos *ipv4_ttl_tos; 517 struct nfp_fl_set_ip4_addrs *ipv4_add; 518 struct nfp_fl_set_ipv6_addr *ipv6_add; 519 struct nfp_fl_push_vlan *push_vlan; 520 struct nfp_fl_set_tport *tport; 521 struct nfp_fl_set_eth *eth; 522 struct nfp_fl_act_head *a; 523 unsigned int act_off = 0; 524 u8 act_id = 0; 525 u8 *ports; 526 int i; 527 528 while (act_off < flow->meta.act_len) { 529 a = (struct nfp_fl_act_head *)&flow->action_data[act_off]; 530 act_id = a->jump_id; 531 532 switch (act_id) { 533 case NFP_FL_ACTION_OPCODE_OUTPUT: 534 if (act_out) 535 (*act_out)++; 536 break; 537 case NFP_FL_ACTION_OPCODE_PUSH_VLAN: 538 push_vlan = (struct nfp_fl_push_vlan *)a; 539 if (push_vlan->vlan_tci) 540 merge->tci = cpu_to_be16(0xffff); 541 break; 542 case NFP_FL_ACTION_OPCODE_POP_VLAN: 543 merge->tci = cpu_to_be16(0); 544 break; 545 case NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL: 546 /* New tunnel header means l2 to l4 can be matched. */ 547 eth_broadcast_addr(&merge->l2.mac_dst[0]); 548 eth_broadcast_addr(&merge->l2.mac_src[0]); 549 memset(&merge->l4, 0xff, 550 sizeof(struct nfp_flower_tp_ports)); 551 memset(&merge->ipv4, 0xff, 552 sizeof(struct nfp_flower_ipv4)); 553 break; 554 case NFP_FL_ACTION_OPCODE_SET_ETHERNET: 555 eth = (struct nfp_fl_set_eth *)a; 556 for (i = 0; i < ETH_ALEN; i++) 557 merge->l2.mac_dst[i] |= eth->eth_addr_mask[i]; 558 for (i = 0; i < ETH_ALEN; i++) 559 merge->l2.mac_src[i] |= 560 eth->eth_addr_mask[ETH_ALEN + i]; 561 break; 562 case NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS: 563 ipv4_add = (struct nfp_fl_set_ip4_addrs *)a; 564 merge->ipv4.ipv4_src |= ipv4_add->ipv4_src_mask; 565 merge->ipv4.ipv4_dst |= ipv4_add->ipv4_dst_mask; 566 break; 567 case NFP_FL_ACTION_OPCODE_SET_IPV4_TTL_TOS: 568 ipv4_ttl_tos = (struct nfp_fl_set_ip4_ttl_tos *)a; 569 merge->ipv4.ip_ext.ttl |= ipv4_ttl_tos->ipv4_ttl_mask; 570 merge->ipv4.ip_ext.tos |= ipv4_ttl_tos->ipv4_tos_mask; 571 break; 572 case NFP_FL_ACTION_OPCODE_SET_IPV6_SRC: 573 ipv6_add = (struct nfp_fl_set_ipv6_addr *)a; 574 for (i = 0; i < 4; i++) 575 merge->ipv6.ipv6_src.in6_u.u6_addr32[i] |= 576 ipv6_add->ipv6[i].mask; 577 break; 578 case NFP_FL_ACTION_OPCODE_SET_IPV6_DST: 579 ipv6_add = (struct nfp_fl_set_ipv6_addr *)a; 580 for (i = 0; i < 4; i++) 581 merge->ipv6.ipv6_dst.in6_u.u6_addr32[i] |= 582 ipv6_add->ipv6[i].mask; 583 break; 584 case NFP_FL_ACTION_OPCODE_SET_IPV6_TC_HL_FL: 585 ipv6_tc_hl_fl = (struct nfp_fl_set_ipv6_tc_hl_fl *)a; 586 merge->ipv6.ip_ext.ttl |= 587 ipv6_tc_hl_fl->ipv6_hop_limit_mask; 588 merge->ipv6.ip_ext.tos |= ipv6_tc_hl_fl->ipv6_tc_mask; 589 merge->ipv6.ipv6_flow_label_exthdr |= 590 ipv6_tc_hl_fl->ipv6_label_mask; 591 break; 592 case NFP_FL_ACTION_OPCODE_SET_UDP: 593 case NFP_FL_ACTION_OPCODE_SET_TCP: 594 tport = (struct nfp_fl_set_tport *)a; 595 ports = (u8 *)&merge->l4.port_src; 596 for (i = 0; i < 4; i++) 597 ports[i] |= tport->tp_port_mask[i]; 598 break; 599 case NFP_FL_ACTION_OPCODE_PRE_TUNNEL: 600 case NFP_FL_ACTION_OPCODE_PRE_LAG: 601 case NFP_FL_ACTION_OPCODE_PUSH_GENEVE: 602 break; 603 default: 604 return -EOPNOTSUPP; 605 } 606 607 act_off += a->len_lw << NFP_FL_LW_SIZ; 608 } 609 610 if (last_act_id) 611 *last_act_id = act_id; 612 613 return 0; 614 } 615 616 static int 617 nfp_flower_populate_merge_match(struct nfp_fl_payload *flow, 618 struct nfp_flower_merge_check *merge, 619 bool extra_fields) 620 { 621 struct nfp_flower_meta_tci *meta_tci; 622 u8 *mask = flow->mask_data; 623 u8 key_layer, match_size; 624 625 memset(merge, 0, sizeof(struct nfp_flower_merge_check)); 626 627 meta_tci = (struct nfp_flower_meta_tci *)mask; 628 key_layer = meta_tci->nfp_flow_key_layer; 629 630 if (key_layer & ~NFP_FLOWER_MERGE_FIELDS && !extra_fields) 631 return -EOPNOTSUPP; 632 633 merge->tci = meta_tci->tci; 634 mask += sizeof(struct nfp_flower_meta_tci); 635 636 if (key_layer & NFP_FLOWER_LAYER_EXT_META) 637 mask += sizeof(struct nfp_flower_ext_meta); 638 639 mask += sizeof(struct nfp_flower_in_port); 640 641 if (key_layer & NFP_FLOWER_LAYER_MAC) { 642 match_size = sizeof(struct nfp_flower_mac_mpls); 643 memcpy(&merge->l2, mask, match_size); 644 mask += match_size; 645 } 646 647 if (key_layer & NFP_FLOWER_LAYER_TP) { 648 match_size = sizeof(struct nfp_flower_tp_ports); 649 memcpy(&merge->l4, mask, match_size); 650 mask += match_size; 651 } 652 653 if (key_layer & NFP_FLOWER_LAYER_IPV4) { 654 match_size = sizeof(struct nfp_flower_ipv4); 655 memcpy(&merge->ipv4, mask, match_size); 656 } 657 658 if (key_layer & NFP_FLOWER_LAYER_IPV6) { 659 match_size = sizeof(struct nfp_flower_ipv6); 660 memcpy(&merge->ipv6, mask, match_size); 661 } 662 663 return 0; 664 } 665 666 static int 667 nfp_flower_can_merge(struct nfp_fl_payload *sub_flow1, 668 struct nfp_fl_payload *sub_flow2) 669 { 670 /* Two flows can be merged if sub_flow2 only matches on bits that are 671 * either matched by sub_flow1 or set by a sub_flow1 action. This 672 * ensures that every packet that hits sub_flow1 and recirculates is 673 * guaranteed to hit sub_flow2. 674 */ 675 struct nfp_flower_merge_check sub_flow1_merge, sub_flow2_merge; 676 int err, act_out = 0; 677 u8 last_act_id = 0; 678 679 err = nfp_flower_populate_merge_match(sub_flow1, &sub_flow1_merge, 680 true); 681 if (err) 682 return err; 683 684 err = nfp_flower_populate_merge_match(sub_flow2, &sub_flow2_merge, 685 false); 686 if (err) 687 return err; 688 689 err = nfp_flower_update_merge_with_actions(sub_flow1, &sub_flow1_merge, 690 &last_act_id, &act_out); 691 if (err) 692 return err; 693 694 /* Must only be 1 output action and it must be the last in sequence. */ 695 if (act_out != 1 || last_act_id != NFP_FL_ACTION_OPCODE_OUTPUT) 696 return -EOPNOTSUPP; 697 698 /* Reject merge if sub_flow2 matches on something that is not matched 699 * on or set in an action by sub_flow1. 700 */ 701 err = bitmap_andnot(sub_flow2_merge.vals, sub_flow2_merge.vals, 702 sub_flow1_merge.vals, 703 sizeof(struct nfp_flower_merge_check) * 8); 704 if (err) 705 return -EINVAL; 706 707 return 0; 708 } 709 710 static unsigned int 711 nfp_flower_copy_pre_actions(char *act_dst, char *act_src, int len, 712 bool *tunnel_act) 713 { 714 unsigned int act_off = 0, act_len; 715 struct nfp_fl_act_head *a; 716 u8 act_id = 0; 717 718 while (act_off < len) { 719 a = (struct nfp_fl_act_head *)&act_src[act_off]; 720 act_len = a->len_lw << NFP_FL_LW_SIZ; 721 act_id = a->jump_id; 722 723 switch (act_id) { 724 case NFP_FL_ACTION_OPCODE_PRE_TUNNEL: 725 if (tunnel_act) 726 *tunnel_act = true; 727 /* fall through */ 728 case NFP_FL_ACTION_OPCODE_PRE_LAG: 729 memcpy(act_dst + act_off, act_src + act_off, act_len); 730 break; 731 default: 732 return act_off; 733 } 734 735 act_off += act_len; 736 } 737 738 return act_off; 739 } 740 741 static int nfp_fl_verify_post_tun_acts(char *acts, int len) 742 { 743 struct nfp_fl_act_head *a; 744 unsigned int act_off = 0; 745 746 while (act_off < len) { 747 a = (struct nfp_fl_act_head *)&acts[act_off]; 748 if (a->jump_id != NFP_FL_ACTION_OPCODE_OUTPUT) 749 return -EOPNOTSUPP; 750 751 act_off += a->len_lw << NFP_FL_LW_SIZ; 752 } 753 754 return 0; 755 } 756 757 static int 758 nfp_flower_merge_action(struct nfp_fl_payload *sub_flow1, 759 struct nfp_fl_payload *sub_flow2, 760 struct nfp_fl_payload *merge_flow) 761 { 762 unsigned int sub1_act_len, sub2_act_len, pre_off1, pre_off2; 763 bool tunnel_act = false; 764 char *merge_act; 765 int err; 766 767 /* The last action of sub_flow1 must be output - do not merge this. */ 768 sub1_act_len = sub_flow1->meta.act_len - sizeof(struct nfp_fl_output); 769 sub2_act_len = sub_flow2->meta.act_len; 770 771 if (!sub2_act_len) 772 return -EINVAL; 773 774 if (sub1_act_len + sub2_act_len > NFP_FL_MAX_A_SIZ) 775 return -EINVAL; 776 777 /* A shortcut can only be applied if there is a single action. */ 778 if (sub1_act_len) 779 merge_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL); 780 else 781 merge_flow->meta.shortcut = sub_flow2->meta.shortcut; 782 783 merge_flow->meta.act_len = sub1_act_len + sub2_act_len; 784 merge_act = merge_flow->action_data; 785 786 /* Copy any pre-actions to the start of merge flow action list. */ 787 pre_off1 = nfp_flower_copy_pre_actions(merge_act, 788 sub_flow1->action_data, 789 sub1_act_len, &tunnel_act); 790 merge_act += pre_off1; 791 sub1_act_len -= pre_off1; 792 pre_off2 = nfp_flower_copy_pre_actions(merge_act, 793 sub_flow2->action_data, 794 sub2_act_len, NULL); 795 merge_act += pre_off2; 796 sub2_act_len -= pre_off2; 797 798 /* FW does a tunnel push when egressing, therefore, if sub_flow 1 pushes 799 * a tunnel, sub_flow 2 can only have output actions for a valid merge. 800 */ 801 if (tunnel_act) { 802 char *post_tun_acts = &sub_flow2->action_data[pre_off2]; 803 804 err = nfp_fl_verify_post_tun_acts(post_tun_acts, sub2_act_len); 805 if (err) 806 return err; 807 } 808 809 /* Copy remaining actions from sub_flows 1 and 2. */ 810 memcpy(merge_act, sub_flow1->action_data + pre_off1, sub1_act_len); 811 merge_act += sub1_act_len; 812 memcpy(merge_act, sub_flow2->action_data + pre_off2, sub2_act_len); 813 814 return 0; 815 } 816 817 /* Flow link code should only be accessed under RTNL. */ 818 static void nfp_flower_unlink_flow(struct nfp_fl_payload_link *link) 819 { 820 list_del(&link->merge_flow.list); 821 list_del(&link->sub_flow.list); 822 kfree(link); 823 } 824 825 static void nfp_flower_unlink_flows(struct nfp_fl_payload *merge_flow, 826 struct nfp_fl_payload *sub_flow) 827 { 828 struct nfp_fl_payload_link *link; 829 830 list_for_each_entry(link, &merge_flow->linked_flows, merge_flow.list) 831 if (link->sub_flow.flow == sub_flow) { 832 nfp_flower_unlink_flow(link); 833 return; 834 } 835 } 836 837 static int nfp_flower_link_flows(struct nfp_fl_payload *merge_flow, 838 struct nfp_fl_payload *sub_flow) 839 { 840 struct nfp_fl_payload_link *link; 841 842 link = kmalloc(sizeof(*link), GFP_KERNEL); 843 if (!link) 844 return -ENOMEM; 845 846 link->merge_flow.flow = merge_flow; 847 list_add_tail(&link->merge_flow.list, &merge_flow->linked_flows); 848 link->sub_flow.flow = sub_flow; 849 list_add_tail(&link->sub_flow.list, &sub_flow->linked_flows); 850 851 return 0; 852 } 853 854 /** 855 * nfp_flower_merge_offloaded_flows() - Merge 2 existing flows to single flow. 856 * @app: Pointer to the APP handle 857 * @sub_flow1: Initial flow matched to produce merge hint 858 * @sub_flow2: Post recirculation flow matched in merge hint 859 * 860 * Combines 2 flows (if valid) to a single flow, removing the initial from hw 861 * and offloading the new, merged flow. 862 * 863 * Return: negative value on error, 0 in success. 864 */ 865 int nfp_flower_merge_offloaded_flows(struct nfp_app *app, 866 struct nfp_fl_payload *sub_flow1, 867 struct nfp_fl_payload *sub_flow2) 868 { 869 struct tc_cls_flower_offload merge_tc_off; 870 struct nfp_flower_priv *priv = app->priv; 871 struct netlink_ext_ack *extack = NULL; 872 struct nfp_fl_payload *merge_flow; 873 struct nfp_fl_key_ls merge_key_ls; 874 int err; 875 876 ASSERT_RTNL(); 877 878 extack = merge_tc_off.common.extack; 879 if (sub_flow1 == sub_flow2 || 880 nfp_flower_is_merge_flow(sub_flow1) || 881 nfp_flower_is_merge_flow(sub_flow2)) 882 return -EINVAL; 883 884 err = nfp_flower_can_merge(sub_flow1, sub_flow2); 885 if (err) 886 return err; 887 888 merge_key_ls.key_size = sub_flow1->meta.key_len; 889 890 merge_flow = nfp_flower_allocate_new(&merge_key_ls); 891 if (!merge_flow) 892 return -ENOMEM; 893 894 merge_flow->tc_flower_cookie = (unsigned long)merge_flow; 895 merge_flow->ingress_dev = sub_flow1->ingress_dev; 896 897 memcpy(merge_flow->unmasked_data, sub_flow1->unmasked_data, 898 sub_flow1->meta.key_len); 899 memcpy(merge_flow->mask_data, sub_flow1->mask_data, 900 sub_flow1->meta.mask_len); 901 902 err = nfp_flower_merge_action(sub_flow1, sub_flow2, merge_flow); 903 if (err) 904 goto err_destroy_merge_flow; 905 906 err = nfp_flower_link_flows(merge_flow, sub_flow1); 907 if (err) 908 goto err_destroy_merge_flow; 909 910 err = nfp_flower_link_flows(merge_flow, sub_flow2); 911 if (err) 912 goto err_unlink_sub_flow1; 913 914 merge_tc_off.cookie = merge_flow->tc_flower_cookie; 915 err = nfp_compile_flow_metadata(app, &merge_tc_off, merge_flow, 916 merge_flow->ingress_dev, extack); 917 if (err) 918 goto err_unlink_sub_flow2; 919 920 err = rhashtable_insert_fast(&priv->flow_table, &merge_flow->fl_node, 921 nfp_flower_table_params); 922 if (err) 923 goto err_release_metadata; 924 925 err = nfp_flower_xmit_flow(app, merge_flow, 926 NFP_FLOWER_CMSG_TYPE_FLOW_MOD); 927 if (err) 928 goto err_remove_rhash; 929 930 merge_flow->in_hw = true; 931 sub_flow1->in_hw = false; 932 933 return 0; 934 935 err_remove_rhash: 936 WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table, 937 &merge_flow->fl_node, 938 nfp_flower_table_params)); 939 err_release_metadata: 940 nfp_modify_flow_metadata(app, merge_flow); 941 err_unlink_sub_flow2: 942 nfp_flower_unlink_flows(merge_flow, sub_flow2); 943 err_unlink_sub_flow1: 944 nfp_flower_unlink_flows(merge_flow, sub_flow1); 945 err_destroy_merge_flow: 946 kfree(merge_flow->action_data); 947 kfree(merge_flow->mask_data); 948 kfree(merge_flow->unmasked_data); 949 kfree(merge_flow); 950 return err; 951 } 952 953 /** 954 * nfp_flower_add_offload() - Adds a new flow to hardware. 955 * @app: Pointer to the APP handle 956 * @netdev: netdev structure. 957 * @flow: TC flower classifier offload structure. 958 * 959 * Adds a new flow to the repeated hash structure and action payload. 960 * 961 * Return: negative value on error, 0 if configured successfully. 962 */ 963 static int 964 nfp_flower_add_offload(struct nfp_app *app, struct net_device *netdev, 965 struct tc_cls_flower_offload *flow) 966 { 967 enum nfp_flower_tun_type tun_type = NFP_FL_TUNNEL_NONE; 968 struct nfp_flower_priv *priv = app->priv; 969 struct netlink_ext_ack *extack = NULL; 970 struct nfp_fl_payload *flow_pay; 971 struct nfp_fl_key_ls *key_layer; 972 struct nfp_port *port = NULL; 973 int err; 974 975 extack = flow->common.extack; 976 if (nfp_netdev_is_nfp_repr(netdev)) 977 port = nfp_port_from_netdev(netdev); 978 979 key_layer = kmalloc(sizeof(*key_layer), GFP_KERNEL); 980 if (!key_layer) 981 return -ENOMEM; 982 983 err = nfp_flower_calculate_key_layers(app, netdev, key_layer, flow, 984 &tun_type, extack); 985 if (err) 986 goto err_free_key_ls; 987 988 flow_pay = nfp_flower_allocate_new(key_layer); 989 if (!flow_pay) { 990 err = -ENOMEM; 991 goto err_free_key_ls; 992 } 993 994 err = nfp_flower_compile_flow_match(app, flow, key_layer, netdev, 995 flow_pay, tun_type, extack); 996 if (err) 997 goto err_destroy_flow; 998 999 err = nfp_flower_compile_action(app, flow, netdev, flow_pay, extack); 1000 if (err) 1001 goto err_destroy_flow; 1002 1003 err = nfp_compile_flow_metadata(app, flow, flow_pay, netdev, extack); 1004 if (err) 1005 goto err_destroy_flow; 1006 1007 flow_pay->tc_flower_cookie = flow->cookie; 1008 err = rhashtable_insert_fast(&priv->flow_table, &flow_pay->fl_node, 1009 nfp_flower_table_params); 1010 if (err) { 1011 NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot insert flow into tables for offloads"); 1012 goto err_release_metadata; 1013 } 1014 1015 err = nfp_flower_xmit_flow(app, flow_pay, 1016 NFP_FLOWER_CMSG_TYPE_FLOW_ADD); 1017 if (err) 1018 goto err_remove_rhash; 1019 1020 if (port) 1021 port->tc_offload_cnt++; 1022 1023 flow_pay->in_hw = true; 1024 1025 /* Deallocate flow payload when flower rule has been destroyed. */ 1026 kfree(key_layer); 1027 1028 return 0; 1029 1030 err_remove_rhash: 1031 WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table, 1032 &flow_pay->fl_node, 1033 nfp_flower_table_params)); 1034 err_release_metadata: 1035 nfp_modify_flow_metadata(app, flow_pay); 1036 err_destroy_flow: 1037 kfree(flow_pay->action_data); 1038 kfree(flow_pay->mask_data); 1039 kfree(flow_pay->unmasked_data); 1040 kfree(flow_pay); 1041 err_free_key_ls: 1042 kfree(key_layer); 1043 return err; 1044 } 1045 1046 static void 1047 nfp_flower_remove_merge_flow(struct nfp_app *app, 1048 struct nfp_fl_payload *del_sub_flow, 1049 struct nfp_fl_payload *merge_flow) 1050 { 1051 struct nfp_flower_priv *priv = app->priv; 1052 struct nfp_fl_payload_link *link, *temp; 1053 struct nfp_fl_payload *origin; 1054 bool mod = false; 1055 int err; 1056 1057 link = list_first_entry(&merge_flow->linked_flows, 1058 struct nfp_fl_payload_link, merge_flow.list); 1059 origin = link->sub_flow.flow; 1060 1061 /* Re-add rule the merge had overwritten if it has not been deleted. */ 1062 if (origin != del_sub_flow) 1063 mod = true; 1064 1065 err = nfp_modify_flow_metadata(app, merge_flow); 1066 if (err) { 1067 nfp_flower_cmsg_warn(app, "Metadata fail for merge flow delete.\n"); 1068 goto err_free_links; 1069 } 1070 1071 if (!mod) { 1072 err = nfp_flower_xmit_flow(app, merge_flow, 1073 NFP_FLOWER_CMSG_TYPE_FLOW_DEL); 1074 if (err) { 1075 nfp_flower_cmsg_warn(app, "Failed to delete merged flow.\n"); 1076 goto err_free_links; 1077 } 1078 } else { 1079 __nfp_modify_flow_metadata(priv, origin); 1080 err = nfp_flower_xmit_flow(app, origin, 1081 NFP_FLOWER_CMSG_TYPE_FLOW_MOD); 1082 if (err) 1083 nfp_flower_cmsg_warn(app, "Failed to revert merge flow.\n"); 1084 origin->in_hw = true; 1085 } 1086 1087 err_free_links: 1088 /* Clean any links connected with the merged flow. */ 1089 list_for_each_entry_safe(link, temp, &merge_flow->linked_flows, 1090 merge_flow.list) 1091 nfp_flower_unlink_flow(link); 1092 1093 kfree(merge_flow->action_data); 1094 kfree(merge_flow->mask_data); 1095 kfree(merge_flow->unmasked_data); 1096 WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table, 1097 &merge_flow->fl_node, 1098 nfp_flower_table_params)); 1099 kfree_rcu(merge_flow, rcu); 1100 } 1101 1102 static void 1103 nfp_flower_del_linked_merge_flows(struct nfp_app *app, 1104 struct nfp_fl_payload *sub_flow) 1105 { 1106 struct nfp_fl_payload_link *link, *temp; 1107 1108 /* Remove any merge flow formed from the deleted sub_flow. */ 1109 list_for_each_entry_safe(link, temp, &sub_flow->linked_flows, 1110 sub_flow.list) 1111 nfp_flower_remove_merge_flow(app, sub_flow, 1112 link->merge_flow.flow); 1113 } 1114 1115 /** 1116 * nfp_flower_del_offload() - Removes a flow from hardware. 1117 * @app: Pointer to the APP handle 1118 * @netdev: netdev structure. 1119 * @flow: TC flower classifier offload structure 1120 * 1121 * Removes a flow from the repeated hash structure and clears the 1122 * action payload. Any flows merged from this are also deleted. 1123 * 1124 * Return: negative value on error, 0 if removed successfully. 1125 */ 1126 static int 1127 nfp_flower_del_offload(struct nfp_app *app, struct net_device *netdev, 1128 struct tc_cls_flower_offload *flow) 1129 { 1130 struct nfp_flower_priv *priv = app->priv; 1131 struct netlink_ext_ack *extack = NULL; 1132 struct nfp_fl_payload *nfp_flow; 1133 struct nfp_port *port = NULL; 1134 int err; 1135 1136 extack = flow->common.extack; 1137 if (nfp_netdev_is_nfp_repr(netdev)) 1138 port = nfp_port_from_netdev(netdev); 1139 1140 nfp_flow = nfp_flower_search_fl_table(app, flow->cookie, netdev); 1141 if (!nfp_flow) { 1142 NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot remove flow that does not exist"); 1143 return -ENOENT; 1144 } 1145 1146 err = nfp_modify_flow_metadata(app, nfp_flow); 1147 if (err) 1148 goto err_free_merge_flow; 1149 1150 if (nfp_flow->nfp_tun_ipv4_addr) 1151 nfp_tunnel_del_ipv4_off(app, nfp_flow->nfp_tun_ipv4_addr); 1152 1153 if (!nfp_flow->in_hw) { 1154 err = 0; 1155 goto err_free_merge_flow; 1156 } 1157 1158 err = nfp_flower_xmit_flow(app, nfp_flow, 1159 NFP_FLOWER_CMSG_TYPE_FLOW_DEL); 1160 /* Fall through on error. */ 1161 1162 err_free_merge_flow: 1163 nfp_flower_del_linked_merge_flows(app, nfp_flow); 1164 if (port) 1165 port->tc_offload_cnt--; 1166 kfree(nfp_flow->action_data); 1167 kfree(nfp_flow->mask_data); 1168 kfree(nfp_flow->unmasked_data); 1169 WARN_ON_ONCE(rhashtable_remove_fast(&priv->flow_table, 1170 &nfp_flow->fl_node, 1171 nfp_flower_table_params)); 1172 kfree_rcu(nfp_flow, rcu); 1173 return err; 1174 } 1175 1176 static void 1177 __nfp_flower_update_merge_stats(struct nfp_app *app, 1178 struct nfp_fl_payload *merge_flow) 1179 { 1180 struct nfp_flower_priv *priv = app->priv; 1181 struct nfp_fl_payload_link *link; 1182 struct nfp_fl_payload *sub_flow; 1183 u64 pkts, bytes, used; 1184 u32 ctx_id; 1185 1186 ctx_id = be32_to_cpu(merge_flow->meta.host_ctx_id); 1187 pkts = priv->stats[ctx_id].pkts; 1188 /* Do not cycle subflows if no stats to distribute. */ 1189 if (!pkts) 1190 return; 1191 bytes = priv->stats[ctx_id].bytes; 1192 used = priv->stats[ctx_id].used; 1193 1194 /* Reset stats for the merge flow. */ 1195 priv->stats[ctx_id].pkts = 0; 1196 priv->stats[ctx_id].bytes = 0; 1197 1198 /* The merge flow has received stats updates from firmware. 1199 * Distribute these stats to all subflows that form the merge. 1200 * The stats will collected from TC via the subflows. 1201 */ 1202 list_for_each_entry(link, &merge_flow->linked_flows, merge_flow.list) { 1203 sub_flow = link->sub_flow.flow; 1204 ctx_id = be32_to_cpu(sub_flow->meta.host_ctx_id); 1205 priv->stats[ctx_id].pkts += pkts; 1206 priv->stats[ctx_id].bytes += bytes; 1207 max_t(u64, priv->stats[ctx_id].used, used); 1208 } 1209 } 1210 1211 static void 1212 nfp_flower_update_merge_stats(struct nfp_app *app, 1213 struct nfp_fl_payload *sub_flow) 1214 { 1215 struct nfp_fl_payload_link *link; 1216 1217 /* Get merge flows that the subflow forms to distribute their stats. */ 1218 list_for_each_entry(link, &sub_flow->linked_flows, sub_flow.list) 1219 __nfp_flower_update_merge_stats(app, link->merge_flow.flow); 1220 } 1221 1222 /** 1223 * nfp_flower_get_stats() - Populates flow stats obtained from hardware. 1224 * @app: Pointer to the APP handle 1225 * @netdev: Netdev structure. 1226 * @flow: TC flower classifier offload structure 1227 * 1228 * Populates a flow statistics structure which which corresponds to a 1229 * specific flow. 1230 * 1231 * Return: negative value on error, 0 if stats populated successfully. 1232 */ 1233 static int 1234 nfp_flower_get_stats(struct nfp_app *app, struct net_device *netdev, 1235 struct tc_cls_flower_offload *flow) 1236 { 1237 struct nfp_flower_priv *priv = app->priv; 1238 struct netlink_ext_ack *extack = NULL; 1239 struct nfp_fl_payload *nfp_flow; 1240 u32 ctx_id; 1241 1242 extack = flow->common.extack; 1243 nfp_flow = nfp_flower_search_fl_table(app, flow->cookie, netdev); 1244 if (!nfp_flow) { 1245 NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot dump stats for flow that does not exist"); 1246 return -EINVAL; 1247 } 1248 1249 ctx_id = be32_to_cpu(nfp_flow->meta.host_ctx_id); 1250 1251 spin_lock_bh(&priv->stats_lock); 1252 /* If request is for a sub_flow, update stats from merged flows. */ 1253 if (!list_empty(&nfp_flow->linked_flows)) 1254 nfp_flower_update_merge_stats(app, nfp_flow); 1255 1256 flow_stats_update(&flow->stats, priv->stats[ctx_id].bytes, 1257 priv->stats[ctx_id].pkts, priv->stats[ctx_id].used); 1258 1259 priv->stats[ctx_id].pkts = 0; 1260 priv->stats[ctx_id].bytes = 0; 1261 spin_unlock_bh(&priv->stats_lock); 1262 1263 return 0; 1264 } 1265 1266 static int 1267 nfp_flower_repr_offload(struct nfp_app *app, struct net_device *netdev, 1268 struct tc_cls_flower_offload *flower) 1269 { 1270 if (!eth_proto_is_802_3(flower->common.protocol)) 1271 return -EOPNOTSUPP; 1272 1273 switch (flower->command) { 1274 case TC_CLSFLOWER_REPLACE: 1275 return nfp_flower_add_offload(app, netdev, flower); 1276 case TC_CLSFLOWER_DESTROY: 1277 return nfp_flower_del_offload(app, netdev, flower); 1278 case TC_CLSFLOWER_STATS: 1279 return nfp_flower_get_stats(app, netdev, flower); 1280 default: 1281 return -EOPNOTSUPP; 1282 } 1283 } 1284 1285 static int nfp_flower_setup_tc_block_cb(enum tc_setup_type type, 1286 void *type_data, void *cb_priv) 1287 { 1288 struct nfp_repr *repr = cb_priv; 1289 1290 if (!tc_cls_can_offload_and_chain0(repr->netdev, type_data)) 1291 return -EOPNOTSUPP; 1292 1293 switch (type) { 1294 case TC_SETUP_CLSFLOWER: 1295 return nfp_flower_repr_offload(repr->app, repr->netdev, 1296 type_data); 1297 case TC_SETUP_CLSMATCHALL: 1298 return nfp_flower_setup_qos_offload(repr->app, repr->netdev, 1299 type_data); 1300 default: 1301 return -EOPNOTSUPP; 1302 } 1303 } 1304 1305 static LIST_HEAD(nfp_block_cb_list); 1306 1307 static int nfp_flower_setup_tc_block(struct net_device *netdev, 1308 struct flow_block_offload *f) 1309 { 1310 struct nfp_repr *repr = netdev_priv(netdev); 1311 struct nfp_flower_repr_priv *repr_priv; 1312 struct flow_block_cb *block_cb; 1313 1314 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 1315 return -EOPNOTSUPP; 1316 1317 repr_priv = repr->app_priv; 1318 repr_priv->block_shared = f->block_shared; 1319 f->driver_block_list = &nfp_block_cb_list; 1320 1321 switch (f->command) { 1322 case FLOW_BLOCK_BIND: 1323 block_cb = flow_block_cb_alloc(f->net, 1324 nfp_flower_setup_tc_block_cb, 1325 repr, repr, NULL); 1326 if (IS_ERR(block_cb)) 1327 return PTR_ERR(block_cb); 1328 1329 flow_block_cb_add(block_cb, f); 1330 list_add_tail(&block_cb->driver_list, &nfp_block_cb_list); 1331 return 0; 1332 case FLOW_BLOCK_UNBIND: 1333 block_cb = flow_block_cb_lookup(f, nfp_flower_setup_tc_block_cb, 1334 repr); 1335 if (!block_cb) 1336 return -ENOENT; 1337 1338 flow_block_cb_remove(block_cb, f); 1339 list_del(&block_cb->driver_list); 1340 return 0; 1341 default: 1342 return -EOPNOTSUPP; 1343 } 1344 } 1345 1346 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev, 1347 enum tc_setup_type type, void *type_data) 1348 { 1349 switch (type) { 1350 case TC_SETUP_BLOCK: 1351 return nfp_flower_setup_tc_block(netdev, type_data); 1352 default: 1353 return -EOPNOTSUPP; 1354 } 1355 } 1356 1357 struct nfp_flower_indr_block_cb_priv { 1358 struct net_device *netdev; 1359 struct nfp_app *app; 1360 struct list_head list; 1361 }; 1362 1363 static struct nfp_flower_indr_block_cb_priv * 1364 nfp_flower_indr_block_cb_priv_lookup(struct nfp_app *app, 1365 struct net_device *netdev) 1366 { 1367 struct nfp_flower_indr_block_cb_priv *cb_priv; 1368 struct nfp_flower_priv *priv = app->priv; 1369 1370 /* All callback list access should be protected by RTNL. */ 1371 ASSERT_RTNL(); 1372 1373 list_for_each_entry(cb_priv, &priv->indr_block_cb_priv, list) 1374 if (cb_priv->netdev == netdev) 1375 return cb_priv; 1376 1377 return NULL; 1378 } 1379 1380 static int nfp_flower_setup_indr_block_cb(enum tc_setup_type type, 1381 void *type_data, void *cb_priv) 1382 { 1383 struct nfp_flower_indr_block_cb_priv *priv = cb_priv; 1384 struct tc_cls_flower_offload *flower = type_data; 1385 1386 if (flower->common.chain_index) 1387 return -EOPNOTSUPP; 1388 1389 switch (type) { 1390 case TC_SETUP_CLSFLOWER: 1391 return nfp_flower_repr_offload(priv->app, priv->netdev, 1392 type_data); 1393 default: 1394 return -EOPNOTSUPP; 1395 } 1396 } 1397 1398 static void nfp_flower_setup_indr_tc_release(void *cb_priv) 1399 { 1400 struct nfp_flower_indr_block_cb_priv *priv = cb_priv; 1401 1402 list_del(&priv->list); 1403 kfree(priv); 1404 } 1405 1406 static int 1407 nfp_flower_setup_indr_tc_block(struct net_device *netdev, struct nfp_app *app, 1408 struct flow_block_offload *f) 1409 { 1410 struct nfp_flower_indr_block_cb_priv *cb_priv; 1411 struct nfp_flower_priv *priv = app->priv; 1412 struct flow_block_cb *block_cb; 1413 1414 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS && 1415 !(f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS && 1416 nfp_flower_internal_port_can_offload(app, netdev))) 1417 return -EOPNOTSUPP; 1418 1419 switch (f->command) { 1420 case FLOW_BLOCK_BIND: 1421 cb_priv = kmalloc(sizeof(*cb_priv), GFP_KERNEL); 1422 if (!cb_priv) 1423 return -ENOMEM; 1424 1425 cb_priv->netdev = netdev; 1426 cb_priv->app = app; 1427 list_add(&cb_priv->list, &priv->indr_block_cb_priv); 1428 1429 block_cb = flow_block_cb_alloc(f->net, 1430 nfp_flower_setup_indr_block_cb, 1431 cb_priv, cb_priv, 1432 nfp_flower_setup_indr_tc_release); 1433 if (IS_ERR(block_cb)) { 1434 list_del(&cb_priv->list); 1435 kfree(cb_priv); 1436 return PTR_ERR(block_cb); 1437 } 1438 1439 flow_block_cb_add(block_cb, f); 1440 list_add_tail(&block_cb->driver_list, &nfp_block_cb_list); 1441 return 0; 1442 case FLOW_BLOCK_UNBIND: 1443 cb_priv = nfp_flower_indr_block_cb_priv_lookup(app, netdev); 1444 if (!cb_priv) 1445 return -ENOENT; 1446 1447 block_cb = flow_block_cb_lookup(f, 1448 nfp_flower_setup_indr_block_cb, 1449 cb_priv); 1450 if (!block_cb) 1451 return -ENOENT; 1452 1453 flow_block_cb_remove(block_cb, f); 1454 list_del(&block_cb->driver_list); 1455 return 0; 1456 default: 1457 return -EOPNOTSUPP; 1458 } 1459 return 0; 1460 } 1461 1462 static int 1463 nfp_flower_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv, 1464 enum tc_setup_type type, void *type_data) 1465 { 1466 switch (type) { 1467 case TC_SETUP_BLOCK: 1468 return nfp_flower_setup_indr_tc_block(netdev, cb_priv, 1469 type_data); 1470 default: 1471 return -EOPNOTSUPP; 1472 } 1473 } 1474 1475 int nfp_flower_reg_indir_block_handler(struct nfp_app *app, 1476 struct net_device *netdev, 1477 unsigned long event) 1478 { 1479 int err; 1480 1481 if (!nfp_fl_is_netdev_to_offload(netdev)) 1482 return NOTIFY_OK; 1483 1484 if (event == NETDEV_REGISTER) { 1485 err = __tc_indr_block_cb_register(netdev, app, 1486 nfp_flower_indr_setup_tc_cb, 1487 app); 1488 if (err) 1489 nfp_flower_cmsg_warn(app, 1490 "Indirect block reg failed - %s\n", 1491 netdev->name); 1492 } else if (event == NETDEV_UNREGISTER) { 1493 __tc_indr_block_cb_unregister(netdev, 1494 nfp_flower_indr_setup_tc_cb, app); 1495 } 1496 1497 return NOTIFY_OK; 1498 } 1499