1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/inetdevice.h> 6 #include <net/netevent.h> 7 #include <linux/idr.h> 8 #include <net/dst_metadata.h> 9 #include <net/arp.h> 10 11 #include "cmsg.h" 12 #include "main.h" 13 #include "../nfp_net_repr.h" 14 #include "../nfp_net.h" 15 16 #define NFP_FL_MAX_ROUTES 32 17 18 #define NFP_TUN_PRE_TUN_RULE_LIMIT 32 19 #define NFP_TUN_PRE_TUN_RULE_DEL BIT(0) 20 #define NFP_TUN_PRE_TUN_IDX_BIT BIT(3) 21 #define NFP_TUN_PRE_TUN_IPV6_BIT BIT(7) 22 23 /** 24 * struct nfp_tun_pre_tun_rule - rule matched before decap 25 * @flags: options for the rule offset 26 * @port_idx: index of destination MAC address for the rule 27 * @vlan_tci: VLAN info associated with MAC 28 * @host_ctx_id: stats context of rule to update 29 */ 30 struct nfp_tun_pre_tun_rule { 31 __be32 flags; 32 __be16 port_idx; 33 __be16 vlan_tci; 34 __be32 host_ctx_id; 35 }; 36 37 /** 38 * struct nfp_tun_active_tuns - periodic message of active tunnels 39 * @seq: sequence number of the message 40 * @count: number of tunnels report in message 41 * @flags: options part of the request 42 * @tun_info.ipv4: dest IPv4 address of active route 43 * @tun_info.egress_port: port the encapsulated packet egressed 44 * @tun_info.extra: reserved for future use 45 * @tun_info: tunnels that have sent traffic in reported period 46 */ 47 struct nfp_tun_active_tuns { 48 __be32 seq; 49 __be32 count; 50 __be32 flags; 51 struct route_ip_info { 52 __be32 ipv4; 53 __be32 egress_port; 54 __be32 extra[2]; 55 } tun_info[]; 56 }; 57 58 /** 59 * struct nfp_tun_active_tuns_v6 - periodic message of active IPv6 tunnels 60 * @seq: sequence number of the message 61 * @count: number of tunnels report in message 62 * @flags: options part of the request 63 * @tun_info.ipv6: dest IPv6 address of active route 64 * @tun_info.egress_port: port the encapsulated packet egressed 65 * @tun_info.extra: reserved for future use 66 * @tun_info: tunnels that have sent traffic in reported period 67 */ 68 struct nfp_tun_active_tuns_v6 { 69 __be32 seq; 70 __be32 count; 71 __be32 flags; 72 struct route_ip_info_v6 { 73 struct in6_addr ipv6; 74 __be32 egress_port; 75 __be32 extra[2]; 76 } tun_info[]; 77 }; 78 79 /** 80 * struct nfp_tun_req_route_ipv4 - NFP requests a route/neighbour lookup 81 * @ingress_port: ingress port of packet that signalled request 82 * @ipv4_addr: destination ipv4 address for route 83 * @reserved: reserved for future use 84 */ 85 struct nfp_tun_req_route_ipv4 { 86 __be32 ingress_port; 87 __be32 ipv4_addr; 88 __be32 reserved[2]; 89 }; 90 91 /** 92 * struct nfp_tun_req_route_ipv6 - NFP requests an IPv6 route/neighbour lookup 93 * @ingress_port: ingress port of packet that signalled request 94 * @ipv6_addr: destination ipv6 address for route 95 */ 96 struct nfp_tun_req_route_ipv6 { 97 __be32 ingress_port; 98 struct in6_addr ipv6_addr; 99 }; 100 101 /** 102 * struct nfp_offloaded_route - routes that are offloaded to the NFP 103 * @list: list pointer 104 * @ip_add: destination of route - can be IPv4 or IPv6 105 */ 106 struct nfp_offloaded_route { 107 struct list_head list; 108 u8 ip_add[]; 109 }; 110 111 #define NFP_FL_IPV4_ADDRS_MAX 32 112 113 /** 114 * struct nfp_tun_ipv4_addr - set the IP address list on the NFP 115 * @count: number of IPs populated in the array 116 * @ipv4_addr: array of IPV4_ADDRS_MAX 32 bit IPv4 addresses 117 */ 118 struct nfp_tun_ipv4_addr { 119 __be32 count; 120 __be32 ipv4_addr[NFP_FL_IPV4_ADDRS_MAX]; 121 }; 122 123 /** 124 * struct nfp_ipv4_addr_entry - cached IPv4 addresses 125 * @ipv4_addr: IP address 126 * @ref_count: number of rules currently using this IP 127 * @list: list pointer 128 */ 129 struct nfp_ipv4_addr_entry { 130 __be32 ipv4_addr; 131 int ref_count; 132 struct list_head list; 133 }; 134 135 #define NFP_FL_IPV6_ADDRS_MAX 4 136 137 /** 138 * struct nfp_tun_ipv6_addr - set the IP address list on the NFP 139 * @count: number of IPs populated in the array 140 * @ipv6_addr: array of IPV6_ADDRS_MAX 128 bit IPv6 addresses 141 */ 142 struct nfp_tun_ipv6_addr { 143 __be32 count; 144 struct in6_addr ipv6_addr[NFP_FL_IPV6_ADDRS_MAX]; 145 }; 146 147 #define NFP_TUN_MAC_OFFLOAD_DEL_FLAG 0x2 148 149 /** 150 * struct nfp_tun_mac_addr_offload - configure MAC address of tunnel EP on NFP 151 * @flags: MAC address offload options 152 * @count: number of MAC addresses in the message (should be 1) 153 * @index: index of MAC address in the lookup table 154 * @addr: interface MAC address 155 */ 156 struct nfp_tun_mac_addr_offload { 157 __be16 flags; 158 __be16 count; 159 __be16 index; 160 u8 addr[ETH_ALEN]; 161 }; 162 163 enum nfp_flower_mac_offload_cmd { 164 NFP_TUNNEL_MAC_OFFLOAD_ADD = 0, 165 NFP_TUNNEL_MAC_OFFLOAD_DEL = 1, 166 NFP_TUNNEL_MAC_OFFLOAD_MOD = 2, 167 }; 168 169 #define NFP_MAX_MAC_INDEX 0xff 170 171 /** 172 * struct nfp_tun_offloaded_mac - hashtable entry for an offloaded MAC 173 * @ht_node: Hashtable entry 174 * @addr: Offloaded MAC address 175 * @index: Offloaded index for given MAC address 176 * @ref_count: Number of devs using this MAC address 177 * @repr_list: List of reprs sharing this MAC address 178 * @bridge_count: Number of bridge/internal devs with MAC 179 */ 180 struct nfp_tun_offloaded_mac { 181 struct rhash_head ht_node; 182 u8 addr[ETH_ALEN]; 183 u16 index; 184 int ref_count; 185 struct list_head repr_list; 186 int bridge_count; 187 }; 188 189 static const struct rhashtable_params offloaded_macs_params = { 190 .key_offset = offsetof(struct nfp_tun_offloaded_mac, addr), 191 .head_offset = offsetof(struct nfp_tun_offloaded_mac, ht_node), 192 .key_len = ETH_ALEN, 193 .automatic_shrinking = true, 194 }; 195 196 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb) 197 { 198 struct nfp_tun_active_tuns *payload; 199 struct net_device *netdev; 200 int count, i, pay_len; 201 struct neighbour *n; 202 __be32 ipv4_addr; 203 u32 port; 204 205 payload = nfp_flower_cmsg_get_data(skb); 206 count = be32_to_cpu(payload->count); 207 if (count > NFP_FL_MAX_ROUTES) { 208 nfp_flower_cmsg_warn(app, "Tunnel keep-alive request exceeds max routes.\n"); 209 return; 210 } 211 212 pay_len = nfp_flower_cmsg_get_data_len(skb); 213 if (pay_len != struct_size(payload, tun_info, count)) { 214 nfp_flower_cmsg_warn(app, "Corruption in tunnel keep-alive message.\n"); 215 return; 216 } 217 218 rcu_read_lock(); 219 for (i = 0; i < count; i++) { 220 ipv4_addr = payload->tun_info[i].ipv4; 221 port = be32_to_cpu(payload->tun_info[i].egress_port); 222 netdev = nfp_app_dev_get(app, port, NULL); 223 if (!netdev) 224 continue; 225 226 n = neigh_lookup(&arp_tbl, &ipv4_addr, netdev); 227 if (!n) 228 continue; 229 230 /* Update the used timestamp of neighbour */ 231 neigh_event_send(n, NULL); 232 neigh_release(n); 233 } 234 rcu_read_unlock(); 235 } 236 237 void nfp_tunnel_keep_alive_v6(struct nfp_app *app, struct sk_buff *skb) 238 { 239 #if IS_ENABLED(CONFIG_IPV6) 240 struct nfp_tun_active_tuns_v6 *payload; 241 struct net_device *netdev; 242 int count, i, pay_len; 243 struct neighbour *n; 244 void *ipv6_add; 245 u32 port; 246 247 payload = nfp_flower_cmsg_get_data(skb); 248 count = be32_to_cpu(payload->count); 249 if (count > NFP_FL_IPV6_ADDRS_MAX) { 250 nfp_flower_cmsg_warn(app, "IPv6 tunnel keep-alive request exceeds max routes.\n"); 251 return; 252 } 253 254 pay_len = nfp_flower_cmsg_get_data_len(skb); 255 if (pay_len != struct_size(payload, tun_info, count)) { 256 nfp_flower_cmsg_warn(app, "Corruption in tunnel keep-alive message.\n"); 257 return; 258 } 259 260 rcu_read_lock(); 261 for (i = 0; i < count; i++) { 262 ipv6_add = &payload->tun_info[i].ipv6; 263 port = be32_to_cpu(payload->tun_info[i].egress_port); 264 netdev = nfp_app_dev_get(app, port, NULL); 265 if (!netdev) 266 continue; 267 268 n = neigh_lookup(&nd_tbl, ipv6_add, netdev); 269 if (!n) 270 continue; 271 272 /* Update the used timestamp of neighbour */ 273 neigh_event_send(n, NULL); 274 neigh_release(n); 275 } 276 rcu_read_unlock(); 277 #endif 278 } 279 280 static int 281 nfp_flower_xmit_tun_conf(struct nfp_app *app, u8 mtype, u16 plen, void *pdata, 282 gfp_t flag) 283 { 284 struct nfp_flower_priv *priv = app->priv; 285 struct sk_buff *skb; 286 unsigned char *msg; 287 288 if (!(priv->flower_ext_feats & NFP_FL_FEATS_DECAP_V2) && 289 (mtype == NFP_FLOWER_CMSG_TYPE_TUN_NEIGH || 290 mtype == NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6)) 291 plen -= sizeof(struct nfp_tun_neigh_ext); 292 293 if (!(priv->flower_ext_feats & NFP_FL_FEATS_TUNNEL_NEIGH_LAG) && 294 (mtype == NFP_FLOWER_CMSG_TYPE_TUN_NEIGH || 295 mtype == NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6)) 296 plen -= sizeof(struct nfp_tun_neigh_lag); 297 298 skb = nfp_flower_cmsg_alloc(app, plen, mtype, flag); 299 if (!skb) 300 return -ENOMEM; 301 302 msg = nfp_flower_cmsg_get_data(skb); 303 memcpy(msg, pdata, nfp_flower_cmsg_get_data_len(skb)); 304 305 nfp_ctrl_tx(app->ctrl, skb); 306 return 0; 307 } 308 309 static void 310 nfp_tun_mutual_link(struct nfp_predt_entry *predt, 311 struct nfp_neigh_entry *neigh) 312 { 313 struct nfp_fl_payload *flow_pay = predt->flow_pay; 314 struct nfp_tun_neigh_ext *ext; 315 struct nfp_tun_neigh *common; 316 317 if (flow_pay->pre_tun_rule.is_ipv6 != neigh->is_ipv6) 318 return; 319 320 /* In the case of bonding it is possible that there might already 321 * be a flow linked (as the MAC address gets shared). If a flow 322 * is already linked just return. 323 */ 324 if (neigh->flow) 325 return; 326 327 common = neigh->is_ipv6 ? 328 &((struct nfp_tun_neigh_v6 *)neigh->payload)->common : 329 &((struct nfp_tun_neigh_v4 *)neigh->payload)->common; 330 ext = neigh->is_ipv6 ? 331 &((struct nfp_tun_neigh_v6 *)neigh->payload)->ext : 332 &((struct nfp_tun_neigh_v4 *)neigh->payload)->ext; 333 334 if (memcmp(flow_pay->pre_tun_rule.loc_mac, 335 common->src_addr, ETH_ALEN) || 336 memcmp(flow_pay->pre_tun_rule.rem_mac, 337 common->dst_addr, ETH_ALEN)) 338 return; 339 340 list_add(&neigh->list_head, &predt->nn_list); 341 neigh->flow = predt; 342 ext->host_ctx = flow_pay->meta.host_ctx_id; 343 ext->vlan_tci = flow_pay->pre_tun_rule.vlan_tci; 344 ext->vlan_tpid = flow_pay->pre_tun_rule.vlan_tpid; 345 } 346 347 static void 348 nfp_tun_link_predt_entries(struct nfp_app *app, 349 struct nfp_neigh_entry *nn_entry) 350 { 351 struct nfp_flower_priv *priv = app->priv; 352 struct nfp_predt_entry *predt, *tmp; 353 354 list_for_each_entry_safe(predt, tmp, &priv->predt_list, list_head) { 355 nfp_tun_mutual_link(predt, nn_entry); 356 } 357 } 358 359 void nfp_tun_link_and_update_nn_entries(struct nfp_app *app, 360 struct nfp_predt_entry *predt) 361 { 362 struct nfp_flower_priv *priv = app->priv; 363 struct nfp_neigh_entry *nn_entry; 364 struct rhashtable_iter iter; 365 size_t neigh_size; 366 u8 type; 367 368 rhashtable_walk_enter(&priv->neigh_table, &iter); 369 rhashtable_walk_start(&iter); 370 while ((nn_entry = rhashtable_walk_next(&iter)) != NULL) { 371 if (IS_ERR(nn_entry)) 372 continue; 373 nfp_tun_mutual_link(predt, nn_entry); 374 neigh_size = nn_entry->is_ipv6 ? 375 sizeof(struct nfp_tun_neigh_v6) : 376 sizeof(struct nfp_tun_neigh_v4); 377 type = nn_entry->is_ipv6 ? NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6 : 378 NFP_FLOWER_CMSG_TYPE_TUN_NEIGH; 379 nfp_flower_xmit_tun_conf(app, type, neigh_size, 380 nn_entry->payload, 381 GFP_ATOMIC); 382 } 383 rhashtable_walk_stop(&iter); 384 rhashtable_walk_exit(&iter); 385 } 386 387 static void nfp_tun_cleanup_nn_entries(struct nfp_app *app) 388 { 389 struct nfp_flower_priv *priv = app->priv; 390 struct nfp_neigh_entry *neigh; 391 struct nfp_tun_neigh_ext *ext; 392 struct rhashtable_iter iter; 393 size_t neigh_size; 394 u8 type; 395 396 rhashtable_walk_enter(&priv->neigh_table, &iter); 397 rhashtable_walk_start(&iter); 398 while ((neigh = rhashtable_walk_next(&iter)) != NULL) { 399 if (IS_ERR(neigh)) 400 continue; 401 ext = neigh->is_ipv6 ? 402 &((struct nfp_tun_neigh_v6 *)neigh->payload)->ext : 403 &((struct nfp_tun_neigh_v4 *)neigh->payload)->ext; 404 ext->host_ctx = cpu_to_be32(U32_MAX); 405 ext->vlan_tpid = cpu_to_be16(U16_MAX); 406 ext->vlan_tci = cpu_to_be16(U16_MAX); 407 408 neigh_size = neigh->is_ipv6 ? 409 sizeof(struct nfp_tun_neigh_v6) : 410 sizeof(struct nfp_tun_neigh_v4); 411 type = neigh->is_ipv6 ? NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6 : 412 NFP_FLOWER_CMSG_TYPE_TUN_NEIGH; 413 nfp_flower_xmit_tun_conf(app, type, neigh_size, neigh->payload, 414 GFP_ATOMIC); 415 416 rhashtable_remove_fast(&priv->neigh_table, &neigh->ht_node, 417 neigh_table_params); 418 if (neigh->flow) 419 list_del(&neigh->list_head); 420 kfree(neigh); 421 } 422 rhashtable_walk_stop(&iter); 423 rhashtable_walk_exit(&iter); 424 } 425 426 void nfp_tun_unlink_and_update_nn_entries(struct nfp_app *app, 427 struct nfp_predt_entry *predt) 428 { 429 struct nfp_neigh_entry *neigh, *tmp; 430 struct nfp_tun_neigh_ext *ext; 431 size_t neigh_size; 432 u8 type; 433 434 list_for_each_entry_safe(neigh, tmp, &predt->nn_list, list_head) { 435 ext = neigh->is_ipv6 ? 436 &((struct nfp_tun_neigh_v6 *)neigh->payload)->ext : 437 &((struct nfp_tun_neigh_v4 *)neigh->payload)->ext; 438 neigh->flow = NULL; 439 ext->host_ctx = cpu_to_be32(U32_MAX); 440 ext->vlan_tpid = cpu_to_be16(U16_MAX); 441 ext->vlan_tci = cpu_to_be16(U16_MAX); 442 list_del(&neigh->list_head); 443 neigh_size = neigh->is_ipv6 ? 444 sizeof(struct nfp_tun_neigh_v6) : 445 sizeof(struct nfp_tun_neigh_v4); 446 type = neigh->is_ipv6 ? NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6 : 447 NFP_FLOWER_CMSG_TYPE_TUN_NEIGH; 448 nfp_flower_xmit_tun_conf(app, type, neigh_size, neigh->payload, 449 GFP_ATOMIC); 450 } 451 } 452 453 static void 454 nfp_tun_write_neigh(struct net_device *netdev, struct nfp_app *app, 455 void *flow, struct neighbour *neigh, bool is_ipv6, 456 bool override) 457 { 458 bool neigh_invalid = !(neigh->nud_state & NUD_VALID) || neigh->dead; 459 size_t neigh_size = is_ipv6 ? sizeof(struct nfp_tun_neigh_v6) : 460 sizeof(struct nfp_tun_neigh_v4); 461 unsigned long cookie = (unsigned long)neigh; 462 struct nfp_flower_priv *priv = app->priv; 463 struct nfp_neigh_entry *nn_entry; 464 u32 port_id; 465 u8 mtype; 466 467 port_id = nfp_flower_get_port_id_from_netdev(app, netdev); 468 if (!port_id) 469 return; 470 471 spin_lock_bh(&priv->predt_lock); 472 nn_entry = rhashtable_lookup_fast(&priv->neigh_table, &cookie, 473 neigh_table_params); 474 if (!nn_entry && !neigh_invalid) { 475 struct nfp_tun_neigh_ext *ext; 476 struct nfp_tun_neigh_lag *lag; 477 struct nfp_tun_neigh *common; 478 479 nn_entry = kzalloc(sizeof(*nn_entry) + neigh_size, 480 GFP_ATOMIC); 481 if (!nn_entry) 482 goto err; 483 484 nn_entry->payload = (char *)&nn_entry[1]; 485 nn_entry->neigh_cookie = cookie; 486 nn_entry->is_ipv6 = is_ipv6; 487 nn_entry->flow = NULL; 488 if (is_ipv6) { 489 struct flowi6 *flowi6 = (struct flowi6 *)flow; 490 struct nfp_tun_neigh_v6 *payload; 491 492 payload = (struct nfp_tun_neigh_v6 *)nn_entry->payload; 493 payload->src_ipv6 = flowi6->saddr; 494 payload->dst_ipv6 = flowi6->daddr; 495 common = &payload->common; 496 ext = &payload->ext; 497 lag = &payload->lag; 498 mtype = NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6; 499 } else { 500 struct flowi4 *flowi4 = (struct flowi4 *)flow; 501 struct nfp_tun_neigh_v4 *payload; 502 503 payload = (struct nfp_tun_neigh_v4 *)nn_entry->payload; 504 payload->src_ipv4 = flowi4->saddr; 505 payload->dst_ipv4 = flowi4->daddr; 506 common = &payload->common; 507 ext = &payload->ext; 508 lag = &payload->lag; 509 mtype = NFP_FLOWER_CMSG_TYPE_TUN_NEIGH; 510 } 511 ext->host_ctx = cpu_to_be32(U32_MAX); 512 ext->vlan_tpid = cpu_to_be16(U16_MAX); 513 ext->vlan_tci = cpu_to_be16(U16_MAX); 514 ether_addr_copy(common->src_addr, netdev->dev_addr); 515 neigh_ha_snapshot(common->dst_addr, neigh, netdev); 516 517 if ((port_id & NFP_FL_LAG_OUT) == NFP_FL_LAG_OUT) 518 nfp_flower_lag_get_info_from_netdev(app, netdev, lag); 519 common->port_id = cpu_to_be32(port_id); 520 521 if (rhashtable_insert_fast(&priv->neigh_table, 522 &nn_entry->ht_node, 523 neigh_table_params)) 524 goto err; 525 526 nfp_tun_link_predt_entries(app, nn_entry); 527 nfp_flower_xmit_tun_conf(app, mtype, neigh_size, 528 nn_entry->payload, 529 GFP_ATOMIC); 530 } else if (nn_entry && neigh_invalid) { 531 if (is_ipv6) { 532 struct flowi6 *flowi6 = (struct flowi6 *)flow; 533 struct nfp_tun_neigh_v6 *payload; 534 535 payload = (struct nfp_tun_neigh_v6 *)nn_entry->payload; 536 memset(payload, 0, sizeof(struct nfp_tun_neigh_v6)); 537 payload->dst_ipv6 = flowi6->daddr; 538 mtype = NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6; 539 } else { 540 struct flowi4 *flowi4 = (struct flowi4 *)flow; 541 struct nfp_tun_neigh_v4 *payload; 542 543 payload = (struct nfp_tun_neigh_v4 *)nn_entry->payload; 544 memset(payload, 0, sizeof(struct nfp_tun_neigh_v4)); 545 payload->dst_ipv4 = flowi4->daddr; 546 mtype = NFP_FLOWER_CMSG_TYPE_TUN_NEIGH; 547 } 548 /* Trigger ARP to verify invalid neighbour state. */ 549 neigh_event_send(neigh, NULL); 550 rhashtable_remove_fast(&priv->neigh_table, 551 &nn_entry->ht_node, 552 neigh_table_params); 553 554 nfp_flower_xmit_tun_conf(app, mtype, neigh_size, 555 nn_entry->payload, 556 GFP_ATOMIC); 557 558 if (nn_entry->flow) 559 list_del(&nn_entry->list_head); 560 kfree(nn_entry); 561 } else if (nn_entry && !neigh_invalid) { 562 struct nfp_tun_neigh *common; 563 u8 dst_addr[ETH_ALEN]; 564 bool is_mac_change; 565 566 if (is_ipv6) { 567 struct nfp_tun_neigh_v6 *payload; 568 569 payload = (struct nfp_tun_neigh_v6 *)nn_entry->payload; 570 common = &payload->common; 571 mtype = NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6; 572 } else { 573 struct nfp_tun_neigh_v4 *payload; 574 575 payload = (struct nfp_tun_neigh_v4 *)nn_entry->payload; 576 common = &payload->common; 577 mtype = NFP_FLOWER_CMSG_TYPE_TUN_NEIGH; 578 } 579 580 ether_addr_copy(dst_addr, common->dst_addr); 581 neigh_ha_snapshot(common->dst_addr, neigh, netdev); 582 is_mac_change = !ether_addr_equal(dst_addr, common->dst_addr); 583 if (override || is_mac_change) { 584 if (is_mac_change && nn_entry->flow) { 585 list_del(&nn_entry->list_head); 586 nn_entry->flow = NULL; 587 } 588 nfp_tun_link_predt_entries(app, nn_entry); 589 nfp_flower_xmit_tun_conf(app, mtype, neigh_size, 590 nn_entry->payload, 591 GFP_ATOMIC); 592 } 593 } 594 595 spin_unlock_bh(&priv->predt_lock); 596 return; 597 598 err: 599 kfree(nn_entry); 600 spin_unlock_bh(&priv->predt_lock); 601 nfp_flower_cmsg_warn(app, "Neighbour configuration failed.\n"); 602 } 603 604 static int 605 nfp_tun_neigh_event_handler(struct notifier_block *nb, unsigned long event, 606 void *ptr) 607 { 608 struct nfp_flower_priv *app_priv; 609 struct netevent_redirect *redir; 610 struct neighbour *n; 611 struct nfp_app *app; 612 bool neigh_invalid; 613 int err; 614 615 switch (event) { 616 case NETEVENT_REDIRECT: 617 redir = (struct netevent_redirect *)ptr; 618 n = redir->neigh; 619 break; 620 case NETEVENT_NEIGH_UPDATE: 621 n = (struct neighbour *)ptr; 622 break; 623 default: 624 return NOTIFY_DONE; 625 } 626 627 neigh_invalid = !(n->nud_state & NUD_VALID) || n->dead; 628 629 app_priv = container_of(nb, struct nfp_flower_priv, tun.neigh_nb); 630 app = app_priv->app; 631 632 if (!nfp_flower_get_port_id_from_netdev(app, n->dev)) 633 return NOTIFY_DONE; 634 635 #if IS_ENABLED(CONFIG_INET) 636 if (n->tbl->family == AF_INET6) { 637 #if IS_ENABLED(CONFIG_IPV6) 638 struct flowi6 flow6 = {}; 639 640 flow6.daddr = *(struct in6_addr *)n->primary_key; 641 if (!neigh_invalid) { 642 struct dst_entry *dst; 643 /* Use ipv6_dst_lookup_flow to populate flow6->saddr 644 * and other fields. This information is only needed 645 * for new entries, lookup can be skipped when an entry 646 * gets invalidated - as only the daddr is needed for 647 * deleting. 648 */ 649 dst = ip6_dst_lookup_flow(dev_net(n->dev), NULL, 650 &flow6, NULL); 651 if (IS_ERR(dst)) 652 return NOTIFY_DONE; 653 654 dst_release(dst); 655 } 656 nfp_tun_write_neigh(n->dev, app, &flow6, n, true, false); 657 #else 658 return NOTIFY_DONE; 659 #endif /* CONFIG_IPV6 */ 660 } else { 661 struct flowi4 flow4 = {}; 662 663 flow4.daddr = *(__be32 *)n->primary_key; 664 if (!neigh_invalid) { 665 struct rtable *rt; 666 /* Use ip_route_output_key to populate flow4->saddr and 667 * other fields. This information is only needed for 668 * new entries, lookup can be skipped when an entry 669 * gets invalidated - as only the daddr is needed for 670 * deleting. 671 */ 672 rt = ip_route_output_key(dev_net(n->dev), &flow4); 673 err = PTR_ERR_OR_ZERO(rt); 674 if (err) 675 return NOTIFY_DONE; 676 677 ip_rt_put(rt); 678 } 679 nfp_tun_write_neigh(n->dev, app, &flow4, n, false, false); 680 } 681 #else 682 return NOTIFY_DONE; 683 #endif /* CONFIG_INET */ 684 685 return NOTIFY_OK; 686 } 687 688 void nfp_tunnel_request_route_v4(struct nfp_app *app, struct sk_buff *skb) 689 { 690 struct nfp_tun_req_route_ipv4 *payload; 691 struct net_device *netdev; 692 struct flowi4 flow = {}; 693 struct neighbour *n; 694 struct rtable *rt; 695 int err; 696 697 payload = nfp_flower_cmsg_get_data(skb); 698 699 rcu_read_lock(); 700 netdev = nfp_app_dev_get(app, be32_to_cpu(payload->ingress_port), NULL); 701 if (!netdev) 702 goto fail_rcu_unlock; 703 704 flow.daddr = payload->ipv4_addr; 705 flow.flowi4_proto = IPPROTO_UDP; 706 707 #if IS_ENABLED(CONFIG_INET) 708 /* Do a route lookup on same namespace as ingress port. */ 709 rt = ip_route_output_key(dev_net(netdev), &flow); 710 err = PTR_ERR_OR_ZERO(rt); 711 if (err) 712 goto fail_rcu_unlock; 713 #else 714 goto fail_rcu_unlock; 715 #endif 716 717 /* Get the neighbour entry for the lookup */ 718 n = dst_neigh_lookup(&rt->dst, &flow.daddr); 719 ip_rt_put(rt); 720 if (!n) 721 goto fail_rcu_unlock; 722 nfp_tun_write_neigh(n->dev, app, &flow, n, false, true); 723 neigh_release(n); 724 rcu_read_unlock(); 725 return; 726 727 fail_rcu_unlock: 728 rcu_read_unlock(); 729 nfp_flower_cmsg_warn(app, "Requested route not found.\n"); 730 } 731 732 void nfp_tunnel_request_route_v6(struct nfp_app *app, struct sk_buff *skb) 733 { 734 struct nfp_tun_req_route_ipv6 *payload; 735 struct net_device *netdev; 736 struct flowi6 flow = {}; 737 struct dst_entry *dst; 738 struct neighbour *n; 739 740 payload = nfp_flower_cmsg_get_data(skb); 741 742 rcu_read_lock(); 743 netdev = nfp_app_dev_get(app, be32_to_cpu(payload->ingress_port), NULL); 744 if (!netdev) 745 goto fail_rcu_unlock; 746 747 flow.daddr = payload->ipv6_addr; 748 flow.flowi6_proto = IPPROTO_UDP; 749 750 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6) 751 dst = ipv6_stub->ipv6_dst_lookup_flow(dev_net(netdev), NULL, &flow, 752 NULL); 753 if (IS_ERR(dst)) 754 goto fail_rcu_unlock; 755 #else 756 goto fail_rcu_unlock; 757 #endif 758 759 n = dst_neigh_lookup(dst, &flow.daddr); 760 dst_release(dst); 761 if (!n) 762 goto fail_rcu_unlock; 763 764 nfp_tun_write_neigh(n->dev, app, &flow, n, true, true); 765 neigh_release(n); 766 rcu_read_unlock(); 767 return; 768 769 fail_rcu_unlock: 770 rcu_read_unlock(); 771 nfp_flower_cmsg_warn(app, "Requested IPv6 route not found.\n"); 772 } 773 774 static void nfp_tun_write_ipv4_list(struct nfp_app *app) 775 { 776 struct nfp_flower_priv *priv = app->priv; 777 struct nfp_ipv4_addr_entry *entry; 778 struct nfp_tun_ipv4_addr payload; 779 struct list_head *ptr, *storage; 780 int count; 781 782 memset(&payload, 0, sizeof(struct nfp_tun_ipv4_addr)); 783 mutex_lock(&priv->tun.ipv4_off_lock); 784 count = 0; 785 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) { 786 if (count >= NFP_FL_IPV4_ADDRS_MAX) { 787 mutex_unlock(&priv->tun.ipv4_off_lock); 788 nfp_flower_cmsg_warn(app, "IPv4 offload exceeds limit.\n"); 789 return; 790 } 791 entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list); 792 payload.ipv4_addr[count++] = entry->ipv4_addr; 793 } 794 payload.count = cpu_to_be32(count); 795 mutex_unlock(&priv->tun.ipv4_off_lock); 796 797 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_IPS, 798 sizeof(struct nfp_tun_ipv4_addr), 799 &payload, GFP_KERNEL); 800 } 801 802 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4) 803 { 804 struct nfp_flower_priv *priv = app->priv; 805 struct nfp_ipv4_addr_entry *entry; 806 struct list_head *ptr, *storage; 807 808 mutex_lock(&priv->tun.ipv4_off_lock); 809 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) { 810 entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list); 811 if (entry->ipv4_addr == ipv4) { 812 entry->ref_count++; 813 mutex_unlock(&priv->tun.ipv4_off_lock); 814 return; 815 } 816 } 817 818 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 819 if (!entry) { 820 mutex_unlock(&priv->tun.ipv4_off_lock); 821 nfp_flower_cmsg_warn(app, "Mem error when offloading IP address.\n"); 822 return; 823 } 824 entry->ipv4_addr = ipv4; 825 entry->ref_count = 1; 826 list_add_tail(&entry->list, &priv->tun.ipv4_off_list); 827 mutex_unlock(&priv->tun.ipv4_off_lock); 828 829 nfp_tun_write_ipv4_list(app); 830 } 831 832 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4) 833 { 834 struct nfp_flower_priv *priv = app->priv; 835 struct nfp_ipv4_addr_entry *entry; 836 struct list_head *ptr, *storage; 837 838 mutex_lock(&priv->tun.ipv4_off_lock); 839 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) { 840 entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list); 841 if (entry->ipv4_addr == ipv4) { 842 entry->ref_count--; 843 if (!entry->ref_count) { 844 list_del(&entry->list); 845 kfree(entry); 846 } 847 break; 848 } 849 } 850 mutex_unlock(&priv->tun.ipv4_off_lock); 851 852 nfp_tun_write_ipv4_list(app); 853 } 854 855 static void nfp_tun_write_ipv6_list(struct nfp_app *app) 856 { 857 struct nfp_flower_priv *priv = app->priv; 858 struct nfp_ipv6_addr_entry *entry; 859 struct nfp_tun_ipv6_addr payload; 860 int count = 0; 861 862 memset(&payload, 0, sizeof(struct nfp_tun_ipv6_addr)); 863 mutex_lock(&priv->tun.ipv6_off_lock); 864 list_for_each_entry(entry, &priv->tun.ipv6_off_list, list) { 865 if (count >= NFP_FL_IPV6_ADDRS_MAX) { 866 nfp_flower_cmsg_warn(app, "Too many IPv6 tunnel endpoint addresses, some cannot be offloaded.\n"); 867 break; 868 } 869 payload.ipv6_addr[count++] = entry->ipv6_addr; 870 } 871 mutex_unlock(&priv->tun.ipv6_off_lock); 872 payload.count = cpu_to_be32(count); 873 874 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_IPS_V6, 875 sizeof(struct nfp_tun_ipv6_addr), 876 &payload, GFP_KERNEL); 877 } 878 879 struct nfp_ipv6_addr_entry * 880 nfp_tunnel_add_ipv6_off(struct nfp_app *app, struct in6_addr *ipv6) 881 { 882 struct nfp_flower_priv *priv = app->priv; 883 struct nfp_ipv6_addr_entry *entry; 884 885 mutex_lock(&priv->tun.ipv6_off_lock); 886 list_for_each_entry(entry, &priv->tun.ipv6_off_list, list) 887 if (!memcmp(&entry->ipv6_addr, ipv6, sizeof(*ipv6))) { 888 entry->ref_count++; 889 mutex_unlock(&priv->tun.ipv6_off_lock); 890 return entry; 891 } 892 893 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 894 if (!entry) { 895 mutex_unlock(&priv->tun.ipv6_off_lock); 896 nfp_flower_cmsg_warn(app, "Mem error when offloading IP address.\n"); 897 return NULL; 898 } 899 entry->ipv6_addr = *ipv6; 900 entry->ref_count = 1; 901 list_add_tail(&entry->list, &priv->tun.ipv6_off_list); 902 mutex_unlock(&priv->tun.ipv6_off_lock); 903 904 nfp_tun_write_ipv6_list(app); 905 906 return entry; 907 } 908 909 void 910 nfp_tunnel_put_ipv6_off(struct nfp_app *app, struct nfp_ipv6_addr_entry *entry) 911 { 912 struct nfp_flower_priv *priv = app->priv; 913 bool freed = false; 914 915 mutex_lock(&priv->tun.ipv6_off_lock); 916 if (!--entry->ref_count) { 917 list_del(&entry->list); 918 kfree(entry); 919 freed = true; 920 } 921 mutex_unlock(&priv->tun.ipv6_off_lock); 922 923 if (freed) 924 nfp_tun_write_ipv6_list(app); 925 } 926 927 static int 928 __nfp_tunnel_offload_mac(struct nfp_app *app, const u8 *mac, u16 idx, bool del) 929 { 930 struct nfp_tun_mac_addr_offload payload; 931 932 memset(&payload, 0, sizeof(payload)); 933 934 if (del) 935 payload.flags = cpu_to_be16(NFP_TUN_MAC_OFFLOAD_DEL_FLAG); 936 937 /* FW supports multiple MACs per cmsg but restrict to single. */ 938 payload.count = cpu_to_be16(1); 939 payload.index = cpu_to_be16(idx); 940 ether_addr_copy(payload.addr, mac); 941 942 return nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_MAC, 943 sizeof(struct nfp_tun_mac_addr_offload), 944 &payload, GFP_KERNEL); 945 } 946 947 static bool nfp_tunnel_port_is_phy_repr(int port) 948 { 949 if (FIELD_GET(NFP_FLOWER_CMSG_PORT_TYPE, port) == 950 NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT) 951 return true; 952 953 return false; 954 } 955 956 static u16 nfp_tunnel_get_mac_idx_from_phy_port_id(int port) 957 { 958 return port << 8 | NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT; 959 } 960 961 static u16 nfp_tunnel_get_global_mac_idx_from_ida(int id) 962 { 963 return id << 8 | NFP_FLOWER_CMSG_PORT_TYPE_OTHER_PORT; 964 } 965 966 static int nfp_tunnel_get_ida_from_global_mac_idx(u16 nfp_mac_idx) 967 { 968 return nfp_mac_idx >> 8; 969 } 970 971 static bool nfp_tunnel_is_mac_idx_global(u16 nfp_mac_idx) 972 { 973 return (nfp_mac_idx & 0xff) == NFP_FLOWER_CMSG_PORT_TYPE_OTHER_PORT; 974 } 975 976 static struct nfp_tun_offloaded_mac * 977 nfp_tunnel_lookup_offloaded_macs(struct nfp_app *app, const u8 *mac) 978 { 979 struct nfp_flower_priv *priv = app->priv; 980 981 return rhashtable_lookup_fast(&priv->tun.offloaded_macs, mac, 982 offloaded_macs_params); 983 } 984 985 static void 986 nfp_tunnel_offloaded_macs_inc_ref_and_link(struct nfp_tun_offloaded_mac *entry, 987 struct net_device *netdev, bool mod) 988 { 989 if (nfp_netdev_is_nfp_repr(netdev)) { 990 struct nfp_flower_repr_priv *repr_priv; 991 struct nfp_repr *repr; 992 993 repr = netdev_priv(netdev); 994 repr_priv = repr->app_priv; 995 996 /* If modifing MAC, remove repr from old list first. */ 997 if (mod) 998 list_del(&repr_priv->mac_list); 999 1000 list_add_tail(&repr_priv->mac_list, &entry->repr_list); 1001 } else if (nfp_flower_is_supported_bridge(netdev)) { 1002 entry->bridge_count++; 1003 } 1004 1005 entry->ref_count++; 1006 } 1007 1008 static int 1009 nfp_tunnel_add_shared_mac(struct nfp_app *app, struct net_device *netdev, 1010 int port, bool mod) 1011 { 1012 struct nfp_flower_priv *priv = app->priv; 1013 struct nfp_tun_offloaded_mac *entry; 1014 int ida_idx = -1, err; 1015 u16 nfp_mac_idx = 0; 1016 1017 entry = nfp_tunnel_lookup_offloaded_macs(app, netdev->dev_addr); 1018 if (entry && nfp_tunnel_is_mac_idx_global(entry->index)) { 1019 if (entry->bridge_count || 1020 !nfp_flower_is_supported_bridge(netdev)) { 1021 nfp_tunnel_offloaded_macs_inc_ref_and_link(entry, 1022 netdev, mod); 1023 return 0; 1024 } 1025 1026 /* MAC is global but matches need to go to pre_tun table. */ 1027 nfp_mac_idx = entry->index | NFP_TUN_PRE_TUN_IDX_BIT; 1028 } 1029 1030 if (!nfp_mac_idx) { 1031 /* Assign a global index if non-repr or MAC is now shared. */ 1032 if (entry || !port) { 1033 ida_idx = ida_alloc_max(&priv->tun.mac_off_ids, 1034 NFP_MAX_MAC_INDEX, GFP_KERNEL); 1035 if (ida_idx < 0) 1036 return ida_idx; 1037 1038 nfp_mac_idx = 1039 nfp_tunnel_get_global_mac_idx_from_ida(ida_idx); 1040 1041 if (nfp_flower_is_supported_bridge(netdev)) 1042 nfp_mac_idx |= NFP_TUN_PRE_TUN_IDX_BIT; 1043 1044 } else { 1045 nfp_mac_idx = 1046 nfp_tunnel_get_mac_idx_from_phy_port_id(port); 1047 } 1048 } 1049 1050 if (!entry) { 1051 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 1052 if (!entry) { 1053 err = -ENOMEM; 1054 goto err_free_ida; 1055 } 1056 1057 ether_addr_copy(entry->addr, netdev->dev_addr); 1058 INIT_LIST_HEAD(&entry->repr_list); 1059 1060 if (rhashtable_insert_fast(&priv->tun.offloaded_macs, 1061 &entry->ht_node, 1062 offloaded_macs_params)) { 1063 err = -ENOMEM; 1064 goto err_free_entry; 1065 } 1066 } 1067 1068 err = __nfp_tunnel_offload_mac(app, netdev->dev_addr, 1069 nfp_mac_idx, false); 1070 if (err) { 1071 /* If not shared then free. */ 1072 if (!entry->ref_count) 1073 goto err_remove_hash; 1074 goto err_free_ida; 1075 } 1076 1077 entry->index = nfp_mac_idx; 1078 nfp_tunnel_offloaded_macs_inc_ref_and_link(entry, netdev, mod); 1079 1080 return 0; 1081 1082 err_remove_hash: 1083 rhashtable_remove_fast(&priv->tun.offloaded_macs, &entry->ht_node, 1084 offloaded_macs_params); 1085 err_free_entry: 1086 kfree(entry); 1087 err_free_ida: 1088 if (ida_idx != -1) 1089 ida_free(&priv->tun.mac_off_ids, ida_idx); 1090 1091 return err; 1092 } 1093 1094 static int 1095 nfp_tunnel_del_shared_mac(struct nfp_app *app, struct net_device *netdev, 1096 const u8 *mac, bool mod) 1097 { 1098 struct nfp_flower_priv *priv = app->priv; 1099 struct nfp_flower_repr_priv *repr_priv; 1100 struct nfp_tun_offloaded_mac *entry; 1101 struct nfp_repr *repr; 1102 u16 nfp_mac_idx; 1103 int ida_idx; 1104 1105 entry = nfp_tunnel_lookup_offloaded_macs(app, mac); 1106 if (!entry) 1107 return 0; 1108 1109 entry->ref_count--; 1110 /* If del is part of a mod then mac_list is still in use elsewhere. */ 1111 if (nfp_netdev_is_nfp_repr(netdev) && !mod) { 1112 repr = netdev_priv(netdev); 1113 repr_priv = repr->app_priv; 1114 list_del(&repr_priv->mac_list); 1115 } 1116 1117 if (nfp_flower_is_supported_bridge(netdev)) { 1118 entry->bridge_count--; 1119 1120 if (!entry->bridge_count && entry->ref_count) { 1121 nfp_mac_idx = entry->index & ~NFP_TUN_PRE_TUN_IDX_BIT; 1122 if (__nfp_tunnel_offload_mac(app, mac, nfp_mac_idx, 1123 false)) { 1124 nfp_flower_cmsg_warn(app, "MAC offload index revert failed on %s.\n", 1125 netdev_name(netdev)); 1126 return 0; 1127 } 1128 1129 entry->index = nfp_mac_idx; 1130 return 0; 1131 } 1132 } 1133 1134 /* If MAC is now used by 1 repr set the offloaded MAC index to port. */ 1135 if (entry->ref_count == 1 && list_is_singular(&entry->repr_list)) { 1136 int port, err; 1137 1138 repr_priv = list_first_entry(&entry->repr_list, 1139 struct nfp_flower_repr_priv, 1140 mac_list); 1141 repr = repr_priv->nfp_repr; 1142 port = nfp_repr_get_port_id(repr->netdev); 1143 nfp_mac_idx = nfp_tunnel_get_mac_idx_from_phy_port_id(port); 1144 err = __nfp_tunnel_offload_mac(app, mac, nfp_mac_idx, false); 1145 if (err) { 1146 nfp_flower_cmsg_warn(app, "MAC offload index revert failed on %s.\n", 1147 netdev_name(netdev)); 1148 return 0; 1149 } 1150 1151 ida_idx = nfp_tunnel_get_ida_from_global_mac_idx(entry->index); 1152 ida_free(&priv->tun.mac_off_ids, ida_idx); 1153 entry->index = nfp_mac_idx; 1154 return 0; 1155 } 1156 1157 if (entry->ref_count) 1158 return 0; 1159 1160 WARN_ON_ONCE(rhashtable_remove_fast(&priv->tun.offloaded_macs, 1161 &entry->ht_node, 1162 offloaded_macs_params)); 1163 1164 if (nfp_flower_is_supported_bridge(netdev)) 1165 nfp_mac_idx = entry->index & ~NFP_TUN_PRE_TUN_IDX_BIT; 1166 else 1167 nfp_mac_idx = entry->index; 1168 1169 /* If MAC has global ID then extract and free the ida entry. */ 1170 if (nfp_tunnel_is_mac_idx_global(nfp_mac_idx)) { 1171 ida_idx = nfp_tunnel_get_ida_from_global_mac_idx(entry->index); 1172 ida_free(&priv->tun.mac_off_ids, ida_idx); 1173 } 1174 1175 kfree(entry); 1176 1177 return __nfp_tunnel_offload_mac(app, mac, 0, true); 1178 } 1179 1180 static int 1181 nfp_tunnel_offload_mac(struct nfp_app *app, struct net_device *netdev, 1182 enum nfp_flower_mac_offload_cmd cmd) 1183 { 1184 struct nfp_flower_non_repr_priv *nr_priv = NULL; 1185 bool non_repr = false, *mac_offloaded; 1186 u8 *off_mac = NULL; 1187 int err, port = 0; 1188 1189 if (nfp_netdev_is_nfp_repr(netdev)) { 1190 struct nfp_flower_repr_priv *repr_priv; 1191 struct nfp_repr *repr; 1192 1193 repr = netdev_priv(netdev); 1194 if (repr->app != app) 1195 return 0; 1196 1197 repr_priv = repr->app_priv; 1198 if (repr_priv->on_bridge) 1199 return 0; 1200 1201 mac_offloaded = &repr_priv->mac_offloaded; 1202 off_mac = &repr_priv->offloaded_mac_addr[0]; 1203 port = nfp_repr_get_port_id(netdev); 1204 if (!nfp_tunnel_port_is_phy_repr(port)) 1205 return 0; 1206 } else if (nfp_fl_is_netdev_to_offload(netdev)) { 1207 nr_priv = nfp_flower_non_repr_priv_get(app, netdev); 1208 if (!nr_priv) 1209 return -ENOMEM; 1210 1211 mac_offloaded = &nr_priv->mac_offloaded; 1212 off_mac = &nr_priv->offloaded_mac_addr[0]; 1213 non_repr = true; 1214 } else { 1215 return 0; 1216 } 1217 1218 if (!is_valid_ether_addr(netdev->dev_addr)) { 1219 err = -EINVAL; 1220 goto err_put_non_repr_priv; 1221 } 1222 1223 if (cmd == NFP_TUNNEL_MAC_OFFLOAD_MOD && !*mac_offloaded) 1224 cmd = NFP_TUNNEL_MAC_OFFLOAD_ADD; 1225 1226 switch (cmd) { 1227 case NFP_TUNNEL_MAC_OFFLOAD_ADD: 1228 err = nfp_tunnel_add_shared_mac(app, netdev, port, false); 1229 if (err) 1230 goto err_put_non_repr_priv; 1231 1232 if (non_repr) 1233 __nfp_flower_non_repr_priv_get(nr_priv); 1234 1235 *mac_offloaded = true; 1236 ether_addr_copy(off_mac, netdev->dev_addr); 1237 break; 1238 case NFP_TUNNEL_MAC_OFFLOAD_DEL: 1239 /* Only attempt delete if add was successful. */ 1240 if (!*mac_offloaded) 1241 break; 1242 1243 if (non_repr) 1244 __nfp_flower_non_repr_priv_put(nr_priv); 1245 1246 *mac_offloaded = false; 1247 1248 err = nfp_tunnel_del_shared_mac(app, netdev, netdev->dev_addr, 1249 false); 1250 if (err) 1251 goto err_put_non_repr_priv; 1252 1253 break; 1254 case NFP_TUNNEL_MAC_OFFLOAD_MOD: 1255 /* Ignore if changing to the same address. */ 1256 if (ether_addr_equal(netdev->dev_addr, off_mac)) 1257 break; 1258 1259 err = nfp_tunnel_add_shared_mac(app, netdev, port, true); 1260 if (err) 1261 goto err_put_non_repr_priv; 1262 1263 /* Delete the previous MAC address. */ 1264 err = nfp_tunnel_del_shared_mac(app, netdev, off_mac, true); 1265 if (err) 1266 nfp_flower_cmsg_warn(app, "Failed to remove offload of replaced MAC addr on %s.\n", 1267 netdev_name(netdev)); 1268 1269 ether_addr_copy(off_mac, netdev->dev_addr); 1270 break; 1271 default: 1272 err = -EINVAL; 1273 goto err_put_non_repr_priv; 1274 } 1275 1276 if (non_repr) 1277 __nfp_flower_non_repr_priv_put(nr_priv); 1278 1279 return 0; 1280 1281 err_put_non_repr_priv: 1282 if (non_repr) 1283 __nfp_flower_non_repr_priv_put(nr_priv); 1284 1285 return err; 1286 } 1287 1288 int nfp_tunnel_mac_event_handler(struct nfp_app *app, 1289 struct net_device *netdev, 1290 unsigned long event, void *ptr) 1291 { 1292 int err; 1293 1294 if (event == NETDEV_DOWN) { 1295 err = nfp_tunnel_offload_mac(app, netdev, 1296 NFP_TUNNEL_MAC_OFFLOAD_DEL); 1297 if (err) 1298 nfp_flower_cmsg_warn(app, "Failed to delete offload MAC on %s.\n", 1299 netdev_name(netdev)); 1300 } else if (event == NETDEV_UP) { 1301 err = nfp_tunnel_offload_mac(app, netdev, 1302 NFP_TUNNEL_MAC_OFFLOAD_ADD); 1303 if (err) 1304 nfp_flower_cmsg_warn(app, "Failed to offload MAC on %s.\n", 1305 netdev_name(netdev)); 1306 } else if (event == NETDEV_CHANGEADDR) { 1307 /* Only offload addr change if netdev is already up. */ 1308 if (!(netdev->flags & IFF_UP)) 1309 return NOTIFY_OK; 1310 1311 err = nfp_tunnel_offload_mac(app, netdev, 1312 NFP_TUNNEL_MAC_OFFLOAD_MOD); 1313 if (err) 1314 nfp_flower_cmsg_warn(app, "Failed to offload MAC change on %s.\n", 1315 netdev_name(netdev)); 1316 } else if (event == NETDEV_CHANGEUPPER) { 1317 /* If a repr is attached to a bridge then tunnel packets 1318 * entering the physical port are directed through the bridge 1319 * datapath and cannot be directly detunneled. Therefore, 1320 * associated offloaded MACs and indexes should not be used 1321 * by fw for detunneling. 1322 */ 1323 struct netdev_notifier_changeupper_info *info = ptr; 1324 struct net_device *upper = info->upper_dev; 1325 struct nfp_flower_repr_priv *repr_priv; 1326 struct nfp_repr *repr; 1327 1328 if (!nfp_netdev_is_nfp_repr(netdev) || 1329 !nfp_flower_is_supported_bridge(upper)) 1330 return NOTIFY_OK; 1331 1332 repr = netdev_priv(netdev); 1333 if (repr->app != app) 1334 return NOTIFY_OK; 1335 1336 repr_priv = repr->app_priv; 1337 1338 if (info->linking) { 1339 if (nfp_tunnel_offload_mac(app, netdev, 1340 NFP_TUNNEL_MAC_OFFLOAD_DEL)) 1341 nfp_flower_cmsg_warn(app, "Failed to delete offloaded MAC on %s.\n", 1342 netdev_name(netdev)); 1343 repr_priv->on_bridge = true; 1344 } else { 1345 repr_priv->on_bridge = false; 1346 1347 if (!(netdev->flags & IFF_UP)) 1348 return NOTIFY_OK; 1349 1350 if (nfp_tunnel_offload_mac(app, netdev, 1351 NFP_TUNNEL_MAC_OFFLOAD_ADD)) 1352 nfp_flower_cmsg_warn(app, "Failed to offload MAC on %s.\n", 1353 netdev_name(netdev)); 1354 } 1355 } 1356 return NOTIFY_OK; 1357 } 1358 1359 int nfp_flower_xmit_pre_tun_flow(struct nfp_app *app, 1360 struct nfp_fl_payload *flow) 1361 { 1362 struct nfp_flower_priv *app_priv = app->priv; 1363 struct nfp_tun_offloaded_mac *mac_entry; 1364 struct nfp_flower_meta_tci *key_meta; 1365 struct nfp_tun_pre_tun_rule payload; 1366 struct net_device *internal_dev; 1367 int err; 1368 1369 if (app_priv->pre_tun_rule_cnt == NFP_TUN_PRE_TUN_RULE_LIMIT) 1370 return -ENOSPC; 1371 1372 memset(&payload, 0, sizeof(struct nfp_tun_pre_tun_rule)); 1373 1374 internal_dev = flow->pre_tun_rule.dev; 1375 payload.vlan_tci = flow->pre_tun_rule.vlan_tci; 1376 payload.host_ctx_id = flow->meta.host_ctx_id; 1377 1378 /* Lookup MAC index for the pre-tunnel rule egress device. 1379 * Note that because the device is always an internal port, it will 1380 * have a constant global index so does not need to be tracked. 1381 */ 1382 mac_entry = nfp_tunnel_lookup_offloaded_macs(app, 1383 internal_dev->dev_addr); 1384 if (!mac_entry) 1385 return -ENOENT; 1386 1387 /* Set/clear IPV6 bit. cpu_to_be16() swap will lead to MSB being 1388 * set/clear for port_idx. 1389 */ 1390 key_meta = (struct nfp_flower_meta_tci *)flow->unmasked_data; 1391 if (key_meta->nfp_flow_key_layer & NFP_FLOWER_LAYER_IPV6) 1392 mac_entry->index |= NFP_TUN_PRE_TUN_IPV6_BIT; 1393 else 1394 mac_entry->index &= ~NFP_TUN_PRE_TUN_IPV6_BIT; 1395 1396 payload.port_idx = cpu_to_be16(mac_entry->index); 1397 1398 /* Copy mac id and vlan to flow - dev may not exist at delete time. */ 1399 flow->pre_tun_rule.vlan_tci = payload.vlan_tci; 1400 flow->pre_tun_rule.port_idx = payload.port_idx; 1401 1402 err = nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_PRE_TUN_RULE, 1403 sizeof(struct nfp_tun_pre_tun_rule), 1404 (unsigned char *)&payload, GFP_KERNEL); 1405 if (err) 1406 return err; 1407 1408 app_priv->pre_tun_rule_cnt++; 1409 1410 return 0; 1411 } 1412 1413 int nfp_flower_xmit_pre_tun_del_flow(struct nfp_app *app, 1414 struct nfp_fl_payload *flow) 1415 { 1416 struct nfp_flower_priv *app_priv = app->priv; 1417 struct nfp_tun_pre_tun_rule payload; 1418 u32 tmp_flags = 0; 1419 int err; 1420 1421 memset(&payload, 0, sizeof(struct nfp_tun_pre_tun_rule)); 1422 1423 tmp_flags |= NFP_TUN_PRE_TUN_RULE_DEL; 1424 payload.flags = cpu_to_be32(tmp_flags); 1425 payload.vlan_tci = flow->pre_tun_rule.vlan_tci; 1426 payload.port_idx = flow->pre_tun_rule.port_idx; 1427 1428 err = nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_PRE_TUN_RULE, 1429 sizeof(struct nfp_tun_pre_tun_rule), 1430 (unsigned char *)&payload, GFP_KERNEL); 1431 if (err) 1432 return err; 1433 1434 app_priv->pre_tun_rule_cnt--; 1435 1436 return 0; 1437 } 1438 1439 int nfp_tunnel_config_start(struct nfp_app *app) 1440 { 1441 struct nfp_flower_priv *priv = app->priv; 1442 int err; 1443 1444 /* Initialise rhash for MAC offload tracking. */ 1445 err = rhashtable_init(&priv->tun.offloaded_macs, 1446 &offloaded_macs_params); 1447 if (err) 1448 return err; 1449 1450 ida_init(&priv->tun.mac_off_ids); 1451 1452 /* Initialise priv data for IPv4/v6 offloading. */ 1453 mutex_init(&priv->tun.ipv4_off_lock); 1454 INIT_LIST_HEAD(&priv->tun.ipv4_off_list); 1455 mutex_init(&priv->tun.ipv6_off_lock); 1456 INIT_LIST_HEAD(&priv->tun.ipv6_off_list); 1457 1458 /* Initialise priv data for neighbour offloading. */ 1459 priv->tun.neigh_nb.notifier_call = nfp_tun_neigh_event_handler; 1460 1461 err = register_netevent_notifier(&priv->tun.neigh_nb); 1462 if (err) { 1463 rhashtable_free_and_destroy(&priv->tun.offloaded_macs, 1464 nfp_check_rhashtable_empty, NULL); 1465 return err; 1466 } 1467 1468 return 0; 1469 } 1470 1471 void nfp_tunnel_config_stop(struct nfp_app *app) 1472 { 1473 struct nfp_flower_priv *priv = app->priv; 1474 struct nfp_ipv4_addr_entry *ip_entry; 1475 struct list_head *ptr, *storage; 1476 1477 unregister_netevent_notifier(&priv->tun.neigh_nb); 1478 1479 ida_destroy(&priv->tun.mac_off_ids); 1480 1481 /* Free any memory that may be occupied by ipv4 list. */ 1482 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) { 1483 ip_entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list); 1484 list_del(&ip_entry->list); 1485 kfree(ip_entry); 1486 } 1487 1488 mutex_destroy(&priv->tun.ipv6_off_lock); 1489 1490 /* Destroy rhash. Entries should be cleaned on netdev notifier unreg. */ 1491 rhashtable_free_and_destroy(&priv->tun.offloaded_macs, 1492 nfp_check_rhashtable_empty, NULL); 1493 1494 nfp_tun_cleanup_nn_entries(app); 1495 } 1496