1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
4 #include <linux/etherdevice.h>
5 #include <linux/inetdevice.h>
6 #include <net/netevent.h>
8 #include <net/dst_metadata.h>
13 #include "../nfp_net_repr.h"
14 #include "../nfp_net.h"
16 #define NFP_FL_MAX_ROUTES 32
18 #define NFP_TUN_PRE_TUN_RULE_LIMIT 32
19 #define NFP_TUN_PRE_TUN_RULE_DEL 0x1
20 #define NFP_TUN_PRE_TUN_IDX_BIT 0x8
23 * struct nfp_tun_pre_run_rule - rule matched before decap
24 * @flags: options for the rule offset
25 * @port_idx: index of destination MAC address for the rule
26 * @vlan_tci: VLAN info associated with MAC
27 * @host_ctx_id: stats context of rule to update
29 struct nfp_tun_pre_tun_rule {
37 * struct nfp_tun_active_tuns - periodic message of active tunnels
38 * @seq: sequence number of the message
39 * @count: number of tunnels report in message
40 * @flags: options part of the request
41 * @tun_info.ipv4: dest IPv4 address of active route
42 * @tun_info.egress_port: port the encapsulated packet egressed
43 * @tun_info.extra: reserved for future use
44 * @tun_info: tunnels that have sent traffic in reported period
46 struct nfp_tun_active_tuns {
50 struct route_ip_info {
58 * struct nfp_tun_active_tuns_v6 - periodic message of active IPv6 tunnels
59 * @seq: sequence number of the message
60 * @count: number of tunnels report in message
61 * @flags: options part of the request
62 * @tun_info.ipv6: dest IPv6 address of active route
63 * @tun_info.egress_port: port the encapsulated packet egressed
64 * @tun_info: tunnels that have sent traffic in reported period
66 struct nfp_tun_active_tuns_v6 {
70 struct route_ip_info_v6 {
77 * struct nfp_tun_neigh - neighbour/route entry on the NFP
78 * @dst_ipv4: destination IPv4 address
79 * @src_ipv4: source IPv4 address
80 * @dst_addr: destination MAC address
81 * @src_addr: source MAC address
82 * @port_id: NFP port to output packet on - associated with source IPv4
84 struct nfp_tun_neigh {
87 u8 dst_addr[ETH_ALEN];
88 u8 src_addr[ETH_ALEN];
93 * struct nfp_tun_neigh_v6 - neighbour/route entry on the NFP
94 * @dst_ipv6: destination IPv6 address
95 * @src_ipv6: source IPv6 address
96 * @dst_addr: destination MAC address
97 * @src_addr: source MAC address
98 * @port_id: NFP port to output packet on - associated with source IPv6
100 struct nfp_tun_neigh_v6 {
101 struct in6_addr dst_ipv6;
102 struct in6_addr src_ipv6;
103 u8 dst_addr[ETH_ALEN];
104 u8 src_addr[ETH_ALEN];
109 * struct nfp_tun_req_route_ipv4 - NFP requests a route/neighbour lookup
110 * @ingress_port: ingress port of packet that signalled request
111 * @ipv4_addr: destination ipv4 address for route
112 * @reserved: reserved for future use
114 struct nfp_tun_req_route_ipv4 {
121 * struct nfp_tun_req_route_ipv6 - NFP requests an IPv6 route/neighbour lookup
122 * @ingress_port: ingress port of packet that signalled request
123 * @ipv6_addr: destination ipv6 address for route
125 struct nfp_tun_req_route_ipv6 {
127 struct in6_addr ipv6_addr;
131 * struct nfp_offloaded_route - routes that are offloaded to the NFP
132 * @list: list pointer
133 * @ip_add: destination of route - can be IPv4 or IPv6
135 struct nfp_offloaded_route {
136 struct list_head list;
140 #define NFP_FL_IPV4_ADDRS_MAX 32
143 * struct nfp_tun_ipv4_addr - set the IP address list on the NFP
144 * @count: number of IPs populated in the array
145 * @ipv4_addr: array of IPV4_ADDRS_MAX 32 bit IPv4 addresses
147 struct nfp_tun_ipv4_addr {
149 __be32 ipv4_addr[NFP_FL_IPV4_ADDRS_MAX];
153 * struct nfp_ipv4_addr_entry - cached IPv4 addresses
154 * @ipv4_addr: IP address
155 * @ref_count: number of rules currently using this IP
156 * @list: list pointer
158 struct nfp_ipv4_addr_entry {
161 struct list_head list;
164 #define NFP_FL_IPV6_ADDRS_MAX 4
167 * struct nfp_tun_ipv6_addr - set the IP address list on the NFP
168 * @count: number of IPs populated in the array
169 * @ipv6_addr: array of IPV6_ADDRS_MAX 128 bit IPv6 addresses
171 struct nfp_tun_ipv6_addr {
173 struct in6_addr ipv6_addr[NFP_FL_IPV6_ADDRS_MAX];
176 #define NFP_TUN_MAC_OFFLOAD_DEL_FLAG 0x2
179 * struct nfp_tun_mac_addr_offload - configure MAC address of tunnel EP on NFP
180 * @flags: MAC address offload options
181 * @count: number of MAC addresses in the message (should be 1)
182 * @index: index of MAC address in the lookup table
183 * @addr: interface MAC address
185 struct nfp_tun_mac_addr_offload {
192 enum nfp_flower_mac_offload_cmd {
193 NFP_TUNNEL_MAC_OFFLOAD_ADD = 0,
194 NFP_TUNNEL_MAC_OFFLOAD_DEL = 1,
195 NFP_TUNNEL_MAC_OFFLOAD_MOD = 2,
198 #define NFP_MAX_MAC_INDEX 0xff
201 * struct nfp_tun_offloaded_mac - hashtable entry for an offloaded MAC
202 * @ht_node: Hashtable entry
203 * @addr: Offloaded MAC address
204 * @index: Offloaded index for given MAC address
205 * @ref_count: Number of devs using this MAC address
206 * @repr_list: List of reprs sharing this MAC address
207 * @bridge_count: Number of bridge/internal devs with MAC
209 struct nfp_tun_offloaded_mac {
210 struct rhash_head ht_node;
214 struct list_head repr_list;
218 static const struct rhashtable_params offloaded_macs_params = {
219 .key_offset = offsetof(struct nfp_tun_offloaded_mac, addr),
220 .head_offset = offsetof(struct nfp_tun_offloaded_mac, ht_node),
222 .automatic_shrinking = true,
225 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)
227 struct nfp_tun_active_tuns *payload;
228 struct net_device *netdev;
229 int count, i, pay_len;
234 payload = nfp_flower_cmsg_get_data(skb);
235 count = be32_to_cpu(payload->count);
236 if (count > NFP_FL_MAX_ROUTES) {
237 nfp_flower_cmsg_warn(app, "Tunnel keep-alive request exceeds max routes.\n");
241 pay_len = nfp_flower_cmsg_get_data_len(skb);
242 if (pay_len != struct_size(payload, tun_info, count)) {
243 nfp_flower_cmsg_warn(app, "Corruption in tunnel keep-alive message.\n");
248 for (i = 0; i < count; i++) {
249 ipv4_addr = payload->tun_info[i].ipv4;
250 port = be32_to_cpu(payload->tun_info[i].egress_port);
251 netdev = nfp_app_dev_get(app, port, NULL);
255 n = neigh_lookup(&arp_tbl, &ipv4_addr, netdev);
259 /* Update the used timestamp of neighbour */
260 neigh_event_send(n, NULL);
266 void nfp_tunnel_keep_alive_v6(struct nfp_app *app, struct sk_buff *skb)
268 #if IS_ENABLED(CONFIG_IPV6)
269 struct nfp_tun_active_tuns_v6 *payload;
270 struct net_device *netdev;
271 int count, i, pay_len;
276 payload = nfp_flower_cmsg_get_data(skb);
277 count = be32_to_cpu(payload->count);
278 if (count > NFP_FL_IPV6_ADDRS_MAX) {
279 nfp_flower_cmsg_warn(app, "IPv6 tunnel keep-alive request exceeds max routes.\n");
283 pay_len = nfp_flower_cmsg_get_data_len(skb);
284 if (pay_len != struct_size(payload, tun_info, count)) {
285 nfp_flower_cmsg_warn(app, "Corruption in tunnel keep-alive message.\n");
290 for (i = 0; i < count; i++) {
291 ipv6_add = &payload->tun_info[i].ipv6;
292 port = be32_to_cpu(payload->tun_info[i].egress_port);
293 netdev = nfp_app_dev_get(app, port, NULL);
297 n = neigh_lookup(&nd_tbl, ipv6_add, netdev);
301 /* Update the used timestamp of neighbour */
302 neigh_event_send(n, NULL);
310 nfp_flower_xmit_tun_conf(struct nfp_app *app, u8 mtype, u16 plen, void *pdata,
316 skb = nfp_flower_cmsg_alloc(app, plen, mtype, flag);
320 msg = nfp_flower_cmsg_get_data(skb);
321 memcpy(msg, pdata, nfp_flower_cmsg_get_data_len(skb));
323 nfp_ctrl_tx(app->ctrl, skb);
328 __nfp_tun_has_route(struct list_head *route_list, spinlock_t *list_lock,
329 void *add, int add_len)
331 struct nfp_offloaded_route *entry;
333 spin_lock_bh(list_lock);
334 list_for_each_entry(entry, route_list, list)
335 if (!memcmp(entry->ip_add, add, add_len)) {
336 spin_unlock_bh(list_lock);
339 spin_unlock_bh(list_lock);
344 __nfp_tun_add_route_to_cache(struct list_head *route_list,
345 spinlock_t *list_lock, void *add, int add_len)
347 struct nfp_offloaded_route *entry;
349 spin_lock_bh(list_lock);
350 list_for_each_entry(entry, route_list, list)
351 if (!memcmp(entry->ip_add, add, add_len)) {
352 spin_unlock_bh(list_lock);
356 entry = kmalloc(sizeof(*entry) + add_len, GFP_ATOMIC);
358 spin_unlock_bh(list_lock);
362 memcpy(entry->ip_add, add, add_len);
363 list_add_tail(&entry->list, route_list);
364 spin_unlock_bh(list_lock);
370 __nfp_tun_del_route_from_cache(struct list_head *route_list,
371 spinlock_t *list_lock, void *add, int add_len)
373 struct nfp_offloaded_route *entry;
375 spin_lock_bh(list_lock);
376 list_for_each_entry(entry, route_list, list)
377 if (!memcmp(entry->ip_add, add, add_len)) {
378 list_del(&entry->list);
382 spin_unlock_bh(list_lock);
385 static bool nfp_tun_has_route_v4(struct nfp_app *app, __be32 *ipv4_addr)
387 struct nfp_flower_priv *priv = app->priv;
389 return __nfp_tun_has_route(&priv->tun.neigh_off_list_v4,
390 &priv->tun.neigh_off_lock_v4, ipv4_addr,
395 nfp_tun_has_route_v6(struct nfp_app *app, struct in6_addr *ipv6_addr)
397 struct nfp_flower_priv *priv = app->priv;
399 return __nfp_tun_has_route(&priv->tun.neigh_off_list_v6,
400 &priv->tun.neigh_off_lock_v6, ipv6_addr,
405 nfp_tun_add_route_to_cache_v4(struct nfp_app *app, __be32 *ipv4_addr)
407 struct nfp_flower_priv *priv = app->priv;
409 __nfp_tun_add_route_to_cache(&priv->tun.neigh_off_list_v4,
410 &priv->tun.neigh_off_lock_v4, ipv4_addr,
415 nfp_tun_add_route_to_cache_v6(struct nfp_app *app, struct in6_addr *ipv6_addr)
417 struct nfp_flower_priv *priv = app->priv;
419 __nfp_tun_add_route_to_cache(&priv->tun.neigh_off_list_v6,
420 &priv->tun.neigh_off_lock_v6, ipv6_addr,
425 nfp_tun_del_route_from_cache_v4(struct nfp_app *app, __be32 *ipv4_addr)
427 struct nfp_flower_priv *priv = app->priv;
429 __nfp_tun_del_route_from_cache(&priv->tun.neigh_off_list_v4,
430 &priv->tun.neigh_off_lock_v4, ipv4_addr,
435 nfp_tun_del_route_from_cache_v6(struct nfp_app *app, struct in6_addr *ipv6_addr)
437 struct nfp_flower_priv *priv = app->priv;
439 __nfp_tun_del_route_from_cache(&priv->tun.neigh_off_list_v6,
440 &priv->tun.neigh_off_lock_v6, ipv6_addr,
445 nfp_tun_write_neigh_v4(struct net_device *netdev, struct nfp_app *app,
446 struct flowi4 *flow, struct neighbour *neigh, gfp_t flag)
448 struct nfp_tun_neigh payload;
451 port_id = nfp_flower_get_port_id_from_netdev(app, netdev);
455 memset(&payload, 0, sizeof(struct nfp_tun_neigh));
456 payload.dst_ipv4 = flow->daddr;
458 /* If entry has expired send dst IP with all other fields 0. */
459 if (!(neigh->nud_state & NUD_VALID) || neigh->dead) {
460 nfp_tun_del_route_from_cache_v4(app, &payload.dst_ipv4);
461 /* Trigger ARP to verify invalid neighbour state. */
462 neigh_event_send(neigh, NULL);
466 /* Have a valid neighbour so populate rest of entry. */
467 payload.src_ipv4 = flow->saddr;
468 ether_addr_copy(payload.src_addr, netdev->dev_addr);
469 neigh_ha_snapshot(payload.dst_addr, neigh, netdev);
470 payload.port_id = cpu_to_be32(port_id);
471 /* Add destination of new route to NFP cache. */
472 nfp_tun_add_route_to_cache_v4(app, &payload.dst_ipv4);
475 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_NEIGH,
476 sizeof(struct nfp_tun_neigh),
477 (unsigned char *)&payload, flag);
481 nfp_tun_write_neigh_v6(struct net_device *netdev, struct nfp_app *app,
482 struct flowi6 *flow, struct neighbour *neigh, gfp_t flag)
484 struct nfp_tun_neigh_v6 payload;
487 port_id = nfp_flower_get_port_id_from_netdev(app, netdev);
491 memset(&payload, 0, sizeof(struct nfp_tun_neigh_v6));
492 payload.dst_ipv6 = flow->daddr;
494 /* If entry has expired send dst IP with all other fields 0. */
495 if (!(neigh->nud_state & NUD_VALID) || neigh->dead) {
496 nfp_tun_del_route_from_cache_v6(app, &payload.dst_ipv6);
497 /* Trigger probe to verify invalid neighbour state. */
498 neigh_event_send(neigh, NULL);
502 /* Have a valid neighbour so populate rest of entry. */
503 payload.src_ipv6 = flow->saddr;
504 ether_addr_copy(payload.src_addr, netdev->dev_addr);
505 neigh_ha_snapshot(payload.dst_addr, neigh, netdev);
506 payload.port_id = cpu_to_be32(port_id);
507 /* Add destination of new route to NFP cache. */
508 nfp_tun_add_route_to_cache_v6(app, &payload.dst_ipv6);
511 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6,
512 sizeof(struct nfp_tun_neigh_v6),
513 (unsigned char *)&payload, flag);
517 nfp_tun_neigh_event_handler(struct notifier_block *nb, unsigned long event,
520 struct nfp_flower_priv *app_priv;
521 struct netevent_redirect *redir;
522 struct flowi4 flow4 = {};
523 struct flowi6 flow6 = {};
531 case NETEVENT_REDIRECT:
532 redir = (struct netevent_redirect *)ptr;
535 case NETEVENT_NEIGH_UPDATE:
536 n = (struct neighbour *)ptr;
542 if (n->tbl->family == AF_INET6)
546 flow6.daddr = *(struct in6_addr *)n->primary_key;
548 flow4.daddr = *(__be32 *)n->primary_key;
550 app_priv = container_of(nb, struct nfp_flower_priv, tun.neigh_nb);
553 if (!nfp_netdev_is_nfp_repr(n->dev) &&
554 !nfp_flower_internal_port_can_offload(app, n->dev))
557 /* Only concerned with changes to routes already added to NFP. */
558 if ((ipv6 && !nfp_tun_has_route_v6(app, &flow6.daddr)) ||
559 (!ipv6 && !nfp_tun_has_route_v4(app, &flow4.daddr)))
562 #if IS_ENABLED(CONFIG_INET)
564 #if IS_ENABLED(CONFIG_IPV6)
565 struct dst_entry *dst;
567 dst = ipv6_stub->ipv6_dst_lookup_flow(dev_net(n->dev), NULL,
573 flow6.flowi6_proto = IPPROTO_UDP;
574 nfp_tun_write_neigh_v6(n->dev, app, &flow6, n, GFP_ATOMIC);
577 #endif /* CONFIG_IPV6 */
579 /* Do a route lookup to populate flow data. */
580 rt = ip_route_output_key(dev_net(n->dev), &flow4);
581 err = PTR_ERR_OR_ZERO(rt);
587 flow4.flowi4_proto = IPPROTO_UDP;
588 nfp_tun_write_neigh_v4(n->dev, app, &flow4, n, GFP_ATOMIC);
592 #endif /* CONFIG_INET */
597 void nfp_tunnel_request_route_v4(struct nfp_app *app, struct sk_buff *skb)
599 struct nfp_tun_req_route_ipv4 *payload;
600 struct net_device *netdev;
601 struct flowi4 flow = {};
606 payload = nfp_flower_cmsg_get_data(skb);
609 netdev = nfp_app_dev_get(app, be32_to_cpu(payload->ingress_port), NULL);
611 goto fail_rcu_unlock;
613 flow.daddr = payload->ipv4_addr;
614 flow.flowi4_proto = IPPROTO_UDP;
616 #if IS_ENABLED(CONFIG_INET)
617 /* Do a route lookup on same namespace as ingress port. */
618 rt = ip_route_output_key(dev_net(netdev), &flow);
619 err = PTR_ERR_OR_ZERO(rt);
621 goto fail_rcu_unlock;
623 goto fail_rcu_unlock;
626 /* Get the neighbour entry for the lookup */
627 n = dst_neigh_lookup(&rt->dst, &flow.daddr);
630 goto fail_rcu_unlock;
631 nfp_tun_write_neigh_v4(n->dev, app, &flow, n, GFP_ATOMIC);
638 nfp_flower_cmsg_warn(app, "Requested route not found.\n");
641 void nfp_tunnel_request_route_v6(struct nfp_app *app, struct sk_buff *skb)
643 struct nfp_tun_req_route_ipv6 *payload;
644 struct net_device *netdev;
645 struct flowi6 flow = {};
646 struct dst_entry *dst;
649 payload = nfp_flower_cmsg_get_data(skb);
652 netdev = nfp_app_dev_get(app, be32_to_cpu(payload->ingress_port), NULL);
654 goto fail_rcu_unlock;
656 flow.daddr = payload->ipv6_addr;
657 flow.flowi6_proto = IPPROTO_UDP;
659 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
660 dst = ipv6_stub->ipv6_dst_lookup_flow(dev_net(netdev), NULL, &flow,
663 goto fail_rcu_unlock;
665 goto fail_rcu_unlock;
668 n = dst_neigh_lookup(dst, &flow.daddr);
671 goto fail_rcu_unlock;
673 nfp_tun_write_neigh_v6(n->dev, app, &flow, n, GFP_ATOMIC);
680 nfp_flower_cmsg_warn(app, "Requested IPv6 route not found.\n");
683 static void nfp_tun_write_ipv4_list(struct nfp_app *app)
685 struct nfp_flower_priv *priv = app->priv;
686 struct nfp_ipv4_addr_entry *entry;
687 struct nfp_tun_ipv4_addr payload;
688 struct list_head *ptr, *storage;
691 memset(&payload, 0, sizeof(struct nfp_tun_ipv4_addr));
692 mutex_lock(&priv->tun.ipv4_off_lock);
694 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) {
695 if (count >= NFP_FL_IPV4_ADDRS_MAX) {
696 mutex_unlock(&priv->tun.ipv4_off_lock);
697 nfp_flower_cmsg_warn(app, "IPv4 offload exceeds limit.\n");
700 entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list);
701 payload.ipv4_addr[count++] = entry->ipv4_addr;
703 payload.count = cpu_to_be32(count);
704 mutex_unlock(&priv->tun.ipv4_off_lock);
706 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_IPS,
707 sizeof(struct nfp_tun_ipv4_addr),
708 &payload, GFP_KERNEL);
711 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4)
713 struct nfp_flower_priv *priv = app->priv;
714 struct nfp_ipv4_addr_entry *entry;
715 struct list_head *ptr, *storage;
717 mutex_lock(&priv->tun.ipv4_off_lock);
718 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) {
719 entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list);
720 if (entry->ipv4_addr == ipv4) {
722 mutex_unlock(&priv->tun.ipv4_off_lock);
727 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
729 mutex_unlock(&priv->tun.ipv4_off_lock);
730 nfp_flower_cmsg_warn(app, "Mem error when offloading IP address.\n");
733 entry->ipv4_addr = ipv4;
734 entry->ref_count = 1;
735 list_add_tail(&entry->list, &priv->tun.ipv4_off_list);
736 mutex_unlock(&priv->tun.ipv4_off_lock);
738 nfp_tun_write_ipv4_list(app);
741 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4)
743 struct nfp_flower_priv *priv = app->priv;
744 struct nfp_ipv4_addr_entry *entry;
745 struct list_head *ptr, *storage;
747 mutex_lock(&priv->tun.ipv4_off_lock);
748 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) {
749 entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list);
750 if (entry->ipv4_addr == ipv4) {
752 if (!entry->ref_count) {
753 list_del(&entry->list);
759 mutex_unlock(&priv->tun.ipv4_off_lock);
761 nfp_tun_write_ipv4_list(app);
764 static void nfp_tun_write_ipv6_list(struct nfp_app *app)
766 struct nfp_flower_priv *priv = app->priv;
767 struct nfp_ipv6_addr_entry *entry;
768 struct nfp_tun_ipv6_addr payload;
771 memset(&payload, 0, sizeof(struct nfp_tun_ipv6_addr));
772 mutex_lock(&priv->tun.ipv6_off_lock);
773 list_for_each_entry(entry, &priv->tun.ipv6_off_list, list) {
774 if (count >= NFP_FL_IPV6_ADDRS_MAX) {
775 nfp_flower_cmsg_warn(app, "Too many IPv6 tunnel endpoint addresses, some cannot be offloaded.\n");
778 payload.ipv6_addr[count++] = entry->ipv6_addr;
780 mutex_unlock(&priv->tun.ipv6_off_lock);
781 payload.count = cpu_to_be32(count);
783 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_IPS_V6,
784 sizeof(struct nfp_tun_ipv6_addr),
785 &payload, GFP_KERNEL);
788 struct nfp_ipv6_addr_entry *
789 nfp_tunnel_add_ipv6_off(struct nfp_app *app, struct in6_addr *ipv6)
791 struct nfp_flower_priv *priv = app->priv;
792 struct nfp_ipv6_addr_entry *entry;
794 mutex_lock(&priv->tun.ipv6_off_lock);
795 list_for_each_entry(entry, &priv->tun.ipv6_off_list, list)
796 if (!memcmp(&entry->ipv6_addr, ipv6, sizeof(*ipv6))) {
798 mutex_unlock(&priv->tun.ipv6_off_lock);
802 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
804 mutex_unlock(&priv->tun.ipv6_off_lock);
805 nfp_flower_cmsg_warn(app, "Mem error when offloading IP address.\n");
808 entry->ipv6_addr = *ipv6;
809 entry->ref_count = 1;
810 list_add_tail(&entry->list, &priv->tun.ipv6_off_list);
811 mutex_unlock(&priv->tun.ipv6_off_lock);
813 nfp_tun_write_ipv6_list(app);
819 nfp_tunnel_put_ipv6_off(struct nfp_app *app, struct nfp_ipv6_addr_entry *entry)
821 struct nfp_flower_priv *priv = app->priv;
824 mutex_lock(&priv->tun.ipv6_off_lock);
825 if (!--entry->ref_count) {
826 list_del(&entry->list);
830 mutex_unlock(&priv->tun.ipv6_off_lock);
833 nfp_tun_write_ipv6_list(app);
837 __nfp_tunnel_offload_mac(struct nfp_app *app, u8 *mac, u16 idx, bool del)
839 struct nfp_tun_mac_addr_offload payload;
841 memset(&payload, 0, sizeof(payload));
844 payload.flags = cpu_to_be16(NFP_TUN_MAC_OFFLOAD_DEL_FLAG);
846 /* FW supports multiple MACs per cmsg but restrict to single. */
847 payload.count = cpu_to_be16(1);
848 payload.index = cpu_to_be16(idx);
849 ether_addr_copy(payload.addr, mac);
851 return nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_MAC,
852 sizeof(struct nfp_tun_mac_addr_offload),
853 &payload, GFP_KERNEL);
856 static bool nfp_tunnel_port_is_phy_repr(int port)
858 if (FIELD_GET(NFP_FLOWER_CMSG_PORT_TYPE, port) ==
859 NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT)
865 static u16 nfp_tunnel_get_mac_idx_from_phy_port_id(int port)
867 return port << 8 | NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT;
870 static u16 nfp_tunnel_get_global_mac_idx_from_ida(int id)
872 return id << 8 | NFP_FLOWER_CMSG_PORT_TYPE_OTHER_PORT;
875 static int nfp_tunnel_get_ida_from_global_mac_idx(u16 nfp_mac_idx)
877 return nfp_mac_idx >> 8;
880 static bool nfp_tunnel_is_mac_idx_global(u16 nfp_mac_idx)
882 return (nfp_mac_idx & 0xff) == NFP_FLOWER_CMSG_PORT_TYPE_OTHER_PORT;
885 static struct nfp_tun_offloaded_mac *
886 nfp_tunnel_lookup_offloaded_macs(struct nfp_app *app, u8 *mac)
888 struct nfp_flower_priv *priv = app->priv;
890 return rhashtable_lookup_fast(&priv->tun.offloaded_macs, mac,
891 offloaded_macs_params);
895 nfp_tunnel_offloaded_macs_inc_ref_and_link(struct nfp_tun_offloaded_mac *entry,
896 struct net_device *netdev, bool mod)
898 if (nfp_netdev_is_nfp_repr(netdev)) {
899 struct nfp_flower_repr_priv *repr_priv;
900 struct nfp_repr *repr;
902 repr = netdev_priv(netdev);
903 repr_priv = repr->app_priv;
905 /* If modifing MAC, remove repr from old list first. */
907 list_del(&repr_priv->mac_list);
909 list_add_tail(&repr_priv->mac_list, &entry->repr_list);
910 } else if (nfp_flower_is_supported_bridge(netdev)) {
911 entry->bridge_count++;
918 nfp_tunnel_add_shared_mac(struct nfp_app *app, struct net_device *netdev,
921 struct nfp_flower_priv *priv = app->priv;
922 int ida_idx = NFP_MAX_MAC_INDEX, err;
923 struct nfp_tun_offloaded_mac *entry;
926 entry = nfp_tunnel_lookup_offloaded_macs(app, netdev->dev_addr);
927 if (entry && nfp_tunnel_is_mac_idx_global(entry->index)) {
928 if (entry->bridge_count ||
929 !nfp_flower_is_supported_bridge(netdev)) {
930 nfp_tunnel_offloaded_macs_inc_ref_and_link(entry,
935 /* MAC is global but matches need to go to pre_tun table. */
936 nfp_mac_idx = entry->index | NFP_TUN_PRE_TUN_IDX_BIT;
940 /* Assign a global index if non-repr or MAC is now shared. */
941 if (entry || !port) {
942 ida_idx = ida_simple_get(&priv->tun.mac_off_ids, 0,
943 NFP_MAX_MAC_INDEX, GFP_KERNEL);
948 nfp_tunnel_get_global_mac_idx_from_ida(ida_idx);
950 if (nfp_flower_is_supported_bridge(netdev))
951 nfp_mac_idx |= NFP_TUN_PRE_TUN_IDX_BIT;
955 nfp_tunnel_get_mac_idx_from_phy_port_id(port);
960 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
966 ether_addr_copy(entry->addr, netdev->dev_addr);
967 INIT_LIST_HEAD(&entry->repr_list);
969 if (rhashtable_insert_fast(&priv->tun.offloaded_macs,
971 offloaded_macs_params)) {
977 err = __nfp_tunnel_offload_mac(app, netdev->dev_addr,
980 /* If not shared then free. */
981 if (!entry->ref_count)
982 goto err_remove_hash;
986 entry->index = nfp_mac_idx;
987 nfp_tunnel_offloaded_macs_inc_ref_and_link(entry, netdev, mod);
992 rhashtable_remove_fast(&priv->tun.offloaded_macs, &entry->ht_node,
993 offloaded_macs_params);
997 if (ida_idx != NFP_MAX_MAC_INDEX)
998 ida_simple_remove(&priv->tun.mac_off_ids, ida_idx);
1004 nfp_tunnel_del_shared_mac(struct nfp_app *app, struct net_device *netdev,
1007 struct nfp_flower_priv *priv = app->priv;
1008 struct nfp_flower_repr_priv *repr_priv;
1009 struct nfp_tun_offloaded_mac *entry;
1010 struct nfp_repr *repr;
1013 entry = nfp_tunnel_lookup_offloaded_macs(app, mac);
1018 /* If del is part of a mod then mac_list is still in use elsewheree. */
1019 if (nfp_netdev_is_nfp_repr(netdev) && !mod) {
1020 repr = netdev_priv(netdev);
1021 repr_priv = repr->app_priv;
1022 list_del(&repr_priv->mac_list);
1025 if (nfp_flower_is_supported_bridge(netdev)) {
1026 entry->bridge_count--;
1028 if (!entry->bridge_count && entry->ref_count) {
1031 nfp_mac_idx = entry->index & ~NFP_TUN_PRE_TUN_IDX_BIT;
1032 if (__nfp_tunnel_offload_mac(app, mac, nfp_mac_idx,
1034 nfp_flower_cmsg_warn(app, "MAC offload index revert failed on %s.\n",
1035 netdev_name(netdev));
1039 entry->index = nfp_mac_idx;
1044 /* If MAC is now used by 1 repr set the offloaded MAC index to port. */
1045 if (entry->ref_count == 1 && list_is_singular(&entry->repr_list)) {
1049 repr_priv = list_first_entry(&entry->repr_list,
1050 struct nfp_flower_repr_priv,
1052 repr = repr_priv->nfp_repr;
1053 port = nfp_repr_get_port_id(repr->netdev);
1054 nfp_mac_idx = nfp_tunnel_get_mac_idx_from_phy_port_id(port);
1055 err = __nfp_tunnel_offload_mac(app, mac, nfp_mac_idx, false);
1057 nfp_flower_cmsg_warn(app, "MAC offload index revert failed on %s.\n",
1058 netdev_name(netdev));
1062 ida_idx = nfp_tunnel_get_ida_from_global_mac_idx(entry->index);
1063 ida_simple_remove(&priv->tun.mac_off_ids, ida_idx);
1064 entry->index = nfp_mac_idx;
1068 if (entry->ref_count)
1071 WARN_ON_ONCE(rhashtable_remove_fast(&priv->tun.offloaded_macs,
1073 offloaded_macs_params));
1074 /* If MAC has global ID then extract and free the ida entry. */
1075 if (nfp_tunnel_is_mac_idx_global(entry->index)) {
1076 ida_idx = nfp_tunnel_get_ida_from_global_mac_idx(entry->index);
1077 ida_simple_remove(&priv->tun.mac_off_ids, ida_idx);
1082 return __nfp_tunnel_offload_mac(app, mac, 0, true);
1086 nfp_tunnel_offload_mac(struct nfp_app *app, struct net_device *netdev,
1087 enum nfp_flower_mac_offload_cmd cmd)
1089 struct nfp_flower_non_repr_priv *nr_priv = NULL;
1090 bool non_repr = false, *mac_offloaded;
1094 if (nfp_netdev_is_nfp_repr(netdev)) {
1095 struct nfp_flower_repr_priv *repr_priv;
1096 struct nfp_repr *repr;
1098 repr = netdev_priv(netdev);
1099 if (repr->app != app)
1102 repr_priv = repr->app_priv;
1103 if (repr_priv->on_bridge)
1106 mac_offloaded = &repr_priv->mac_offloaded;
1107 off_mac = &repr_priv->offloaded_mac_addr[0];
1108 port = nfp_repr_get_port_id(netdev);
1109 if (!nfp_tunnel_port_is_phy_repr(port))
1111 } else if (nfp_fl_is_netdev_to_offload(netdev)) {
1112 nr_priv = nfp_flower_non_repr_priv_get(app, netdev);
1116 mac_offloaded = &nr_priv->mac_offloaded;
1117 off_mac = &nr_priv->offloaded_mac_addr[0];
1123 if (!is_valid_ether_addr(netdev->dev_addr)) {
1125 goto err_put_non_repr_priv;
1128 if (cmd == NFP_TUNNEL_MAC_OFFLOAD_MOD && !*mac_offloaded)
1129 cmd = NFP_TUNNEL_MAC_OFFLOAD_ADD;
1132 case NFP_TUNNEL_MAC_OFFLOAD_ADD:
1133 err = nfp_tunnel_add_shared_mac(app, netdev, port, false);
1135 goto err_put_non_repr_priv;
1138 __nfp_flower_non_repr_priv_get(nr_priv);
1140 *mac_offloaded = true;
1141 ether_addr_copy(off_mac, netdev->dev_addr);
1143 case NFP_TUNNEL_MAC_OFFLOAD_DEL:
1144 /* Only attempt delete if add was successful. */
1145 if (!*mac_offloaded)
1149 __nfp_flower_non_repr_priv_put(nr_priv);
1151 *mac_offloaded = false;
1153 err = nfp_tunnel_del_shared_mac(app, netdev, netdev->dev_addr,
1156 goto err_put_non_repr_priv;
1159 case NFP_TUNNEL_MAC_OFFLOAD_MOD:
1160 /* Ignore if changing to the same address. */
1161 if (ether_addr_equal(netdev->dev_addr, off_mac))
1164 err = nfp_tunnel_add_shared_mac(app, netdev, port, true);
1166 goto err_put_non_repr_priv;
1168 /* Delete the previous MAC address. */
1169 err = nfp_tunnel_del_shared_mac(app, netdev, off_mac, true);
1171 nfp_flower_cmsg_warn(app, "Failed to remove offload of replaced MAC addr on %s.\n",
1172 netdev_name(netdev));
1174 ether_addr_copy(off_mac, netdev->dev_addr);
1178 goto err_put_non_repr_priv;
1182 __nfp_flower_non_repr_priv_put(nr_priv);
1186 err_put_non_repr_priv:
1188 __nfp_flower_non_repr_priv_put(nr_priv);
1193 int nfp_tunnel_mac_event_handler(struct nfp_app *app,
1194 struct net_device *netdev,
1195 unsigned long event, void *ptr)
1199 if (event == NETDEV_DOWN) {
1200 err = nfp_tunnel_offload_mac(app, netdev,
1201 NFP_TUNNEL_MAC_OFFLOAD_DEL);
1203 nfp_flower_cmsg_warn(app, "Failed to delete offload MAC on %s.\n",
1204 netdev_name(netdev));
1205 } else if (event == NETDEV_UP) {
1206 err = nfp_tunnel_offload_mac(app, netdev,
1207 NFP_TUNNEL_MAC_OFFLOAD_ADD);
1209 nfp_flower_cmsg_warn(app, "Failed to offload MAC on %s.\n",
1210 netdev_name(netdev));
1211 } else if (event == NETDEV_CHANGEADDR) {
1212 /* Only offload addr change if netdev is already up. */
1213 if (!(netdev->flags & IFF_UP))
1216 err = nfp_tunnel_offload_mac(app, netdev,
1217 NFP_TUNNEL_MAC_OFFLOAD_MOD);
1219 nfp_flower_cmsg_warn(app, "Failed to offload MAC change on %s.\n",
1220 netdev_name(netdev));
1221 } else if (event == NETDEV_CHANGEUPPER) {
1222 /* If a repr is attached to a bridge then tunnel packets
1223 * entering the physical port are directed through the bridge
1224 * datapath and cannot be directly detunneled. Therefore,
1225 * associated offloaded MACs and indexes should not be used
1226 * by fw for detunneling.
1228 struct netdev_notifier_changeupper_info *info = ptr;
1229 struct net_device *upper = info->upper_dev;
1230 struct nfp_flower_repr_priv *repr_priv;
1231 struct nfp_repr *repr;
1233 if (!nfp_netdev_is_nfp_repr(netdev) ||
1234 !nfp_flower_is_supported_bridge(upper))
1237 repr = netdev_priv(netdev);
1238 if (repr->app != app)
1241 repr_priv = repr->app_priv;
1243 if (info->linking) {
1244 if (nfp_tunnel_offload_mac(app, netdev,
1245 NFP_TUNNEL_MAC_OFFLOAD_DEL))
1246 nfp_flower_cmsg_warn(app, "Failed to delete offloaded MAC on %s.\n",
1247 netdev_name(netdev));
1248 repr_priv->on_bridge = true;
1250 repr_priv->on_bridge = false;
1252 if (!(netdev->flags & IFF_UP))
1255 if (nfp_tunnel_offload_mac(app, netdev,
1256 NFP_TUNNEL_MAC_OFFLOAD_ADD))
1257 nfp_flower_cmsg_warn(app, "Failed to offload MAC on %s.\n",
1258 netdev_name(netdev));
1264 int nfp_flower_xmit_pre_tun_flow(struct nfp_app *app,
1265 struct nfp_fl_payload *flow)
1267 struct nfp_flower_priv *app_priv = app->priv;
1268 struct nfp_tun_offloaded_mac *mac_entry;
1269 struct nfp_tun_pre_tun_rule payload;
1270 struct net_device *internal_dev;
1273 if (app_priv->pre_tun_rule_cnt == NFP_TUN_PRE_TUN_RULE_LIMIT)
1276 memset(&payload, 0, sizeof(struct nfp_tun_pre_tun_rule));
1278 internal_dev = flow->pre_tun_rule.dev;
1279 payload.vlan_tci = flow->pre_tun_rule.vlan_tci;
1280 payload.host_ctx_id = flow->meta.host_ctx_id;
1282 /* Lookup MAC index for the pre-tunnel rule egress device.
1283 * Note that because the device is always an internal port, it will
1284 * have a constant global index so does not need to be tracked.
1286 mac_entry = nfp_tunnel_lookup_offloaded_macs(app,
1287 internal_dev->dev_addr);
1291 payload.port_idx = cpu_to_be16(mac_entry->index);
1293 /* Copy mac id and vlan to flow - dev may not exist at delete time. */
1294 flow->pre_tun_rule.vlan_tci = payload.vlan_tci;
1295 flow->pre_tun_rule.port_idx = payload.port_idx;
1297 err = nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_PRE_TUN_RULE,
1298 sizeof(struct nfp_tun_pre_tun_rule),
1299 (unsigned char *)&payload, GFP_KERNEL);
1303 app_priv->pre_tun_rule_cnt++;
1308 int nfp_flower_xmit_pre_tun_del_flow(struct nfp_app *app,
1309 struct nfp_fl_payload *flow)
1311 struct nfp_flower_priv *app_priv = app->priv;
1312 struct nfp_tun_pre_tun_rule payload;
1316 memset(&payload, 0, sizeof(struct nfp_tun_pre_tun_rule));
1318 tmp_flags |= NFP_TUN_PRE_TUN_RULE_DEL;
1319 payload.flags = cpu_to_be32(tmp_flags);
1320 payload.vlan_tci = flow->pre_tun_rule.vlan_tci;
1321 payload.port_idx = flow->pre_tun_rule.port_idx;
1323 err = nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_PRE_TUN_RULE,
1324 sizeof(struct nfp_tun_pre_tun_rule),
1325 (unsigned char *)&payload, GFP_KERNEL);
1329 app_priv->pre_tun_rule_cnt--;
1334 int nfp_tunnel_config_start(struct nfp_app *app)
1336 struct nfp_flower_priv *priv = app->priv;
1339 /* Initialise rhash for MAC offload tracking. */
1340 err = rhashtable_init(&priv->tun.offloaded_macs,
1341 &offloaded_macs_params);
1345 ida_init(&priv->tun.mac_off_ids);
1347 /* Initialise priv data for IPv4/v6 offloading. */
1348 mutex_init(&priv->tun.ipv4_off_lock);
1349 INIT_LIST_HEAD(&priv->tun.ipv4_off_list);
1350 mutex_init(&priv->tun.ipv6_off_lock);
1351 INIT_LIST_HEAD(&priv->tun.ipv6_off_list);
1353 /* Initialise priv data for neighbour offloading. */
1354 spin_lock_init(&priv->tun.neigh_off_lock_v4);
1355 INIT_LIST_HEAD(&priv->tun.neigh_off_list_v4);
1356 spin_lock_init(&priv->tun.neigh_off_lock_v6);
1357 INIT_LIST_HEAD(&priv->tun.neigh_off_list_v6);
1358 priv->tun.neigh_nb.notifier_call = nfp_tun_neigh_event_handler;
1360 err = register_netevent_notifier(&priv->tun.neigh_nb);
1362 rhashtable_free_and_destroy(&priv->tun.offloaded_macs,
1363 nfp_check_rhashtable_empty, NULL);
1370 void nfp_tunnel_config_stop(struct nfp_app *app)
1372 struct nfp_offloaded_route *route_entry, *temp;
1373 struct nfp_flower_priv *priv = app->priv;
1374 struct nfp_ipv4_addr_entry *ip_entry;
1375 struct nfp_tun_neigh_v6 ipv6_route;
1376 struct nfp_tun_neigh ipv4_route;
1377 struct list_head *ptr, *storage;
1379 unregister_netevent_notifier(&priv->tun.neigh_nb);
1381 ida_destroy(&priv->tun.mac_off_ids);
1383 /* Free any memory that may be occupied by ipv4 list. */
1384 list_for_each_safe(ptr, storage, &priv->tun.ipv4_off_list) {
1385 ip_entry = list_entry(ptr, struct nfp_ipv4_addr_entry, list);
1386 list_del(&ip_entry->list);
1390 mutex_destroy(&priv->tun.ipv6_off_lock);
1392 /* Free memory in the route list and remove entries from fw cache. */
1393 list_for_each_entry_safe(route_entry, temp,
1394 &priv->tun.neigh_off_list_v4, list) {
1395 memset(&ipv4_route, 0, sizeof(ipv4_route));
1396 memcpy(&ipv4_route.dst_ipv4, &route_entry->ip_add,
1397 sizeof(ipv4_route.dst_ipv4));
1398 list_del(&route_entry->list);
1401 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_NEIGH,
1402 sizeof(struct nfp_tun_neigh),
1403 (unsigned char *)&ipv4_route,
1407 list_for_each_entry_safe(route_entry, temp,
1408 &priv->tun.neigh_off_list_v6, list) {
1409 memset(&ipv6_route, 0, sizeof(ipv6_route));
1410 memcpy(&ipv6_route.dst_ipv6, &route_entry->ip_add,
1411 sizeof(ipv6_route.dst_ipv6));
1412 list_del(&route_entry->list);
1415 nfp_flower_xmit_tun_conf(app, NFP_FLOWER_CMSG_TYPE_TUN_NEIGH_V6,
1416 sizeof(struct nfp_tun_neigh),
1417 (unsigned char *)&ipv6_route,
1421 /* Destroy rhash. Entries should be cleaned on netdev notifier unreg. */
1422 rhashtable_free_and_destroy(&priv->tun.offloaded_macs,
1423 nfp_check_rhashtable_empty, NULL);