1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2023, Advanced Micro Devices, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include "tc_encap_actions.h"
14 #include <net/vxlan.h>
15 #include <net/geneve.h>
16 #include <net/netevent.h>
19 static const struct rhashtable_params efx_neigh_ht_params
= {
20 .key_len
= offsetof(struct efx_neigh_binder
, ha
),
22 .head_offset
= offsetof(struct efx_neigh_binder
, linkage
),
25 static const struct rhashtable_params efx_tc_encap_ht_params
= {
26 .key_len
= offsetofend(struct efx_tc_encap_action
, key
),
28 .head_offset
= offsetof(struct efx_tc_encap_action
, linkage
),
31 static void efx_tc_encap_free(void *ptr
, void *__unused
)
33 struct efx_tc_encap_action
*enc
= ptr
;
35 WARN_ON(refcount_read(&enc
->ref
));
39 static void efx_neigh_free(void *ptr
, void *__unused
)
41 struct efx_neigh_binder
*neigh
= ptr
;
43 WARN_ON(refcount_read(&neigh
->ref
));
44 WARN_ON(!list_empty(&neigh
->users
));
45 put_net_track(neigh
->net
, &neigh
->ns_tracker
);
46 netdev_put(neigh
->egdev
, &neigh
->dev_tracker
);
50 int efx_tc_init_encap_actions(struct efx_nic
*efx
)
54 rc
= rhashtable_init(&efx
->tc
->neigh_ht
, &efx_neigh_ht_params
);
57 rc
= rhashtable_init(&efx
->tc
->encap_ht
, &efx_tc_encap_ht_params
);
62 rhashtable_destroy(&efx
->tc
->neigh_ht
);
67 /* Only call this in init failure teardown.
68 * Normal exit should fini instead as there may be entries in the table.
70 void efx_tc_destroy_encap_actions(struct efx_nic
*efx
)
72 rhashtable_destroy(&efx
->tc
->encap_ht
);
73 rhashtable_destroy(&efx
->tc
->neigh_ht
);
76 void efx_tc_fini_encap_actions(struct efx_nic
*efx
)
78 rhashtable_free_and_destroy(&efx
->tc
->encap_ht
, efx_tc_encap_free
, NULL
);
79 rhashtable_free_and_destroy(&efx
->tc
->neigh_ht
, efx_neigh_free
, NULL
);
82 static void efx_neigh_update(struct work_struct
*work
);
84 static int efx_bind_neigh(struct efx_nic
*efx
,
85 struct efx_tc_encap_action
*encap
, struct net
*net
,
86 struct netlink_ext_ack
*extack
)
88 struct efx_neigh_binder
*neigh
, *old
;
89 struct flowi6 flow6
= {};
90 struct flowi4 flow4
= {};
93 /* GCC stupidly thinks that only values explicitly listed in the enum
94 * definition can _possibly_ be sensible case values, so without this
95 * cast it complains about the IPv6 versions.
97 switch ((int)encap
->type
) {
98 case EFX_ENCAP_TYPE_VXLAN
:
99 case EFX_ENCAP_TYPE_GENEVE
:
100 flow4
.flowi4_proto
= IPPROTO_UDP
;
101 flow4
.fl4_dport
= encap
->key
.tp_dst
;
102 flow4
.flowi4_tos
= encap
->key
.tos
;
103 flow4
.daddr
= encap
->key
.u
.ipv4
.dst
;
104 flow4
.saddr
= encap
->key
.u
.ipv4
.src
;
106 case EFX_ENCAP_TYPE_VXLAN
| EFX_ENCAP_FLAG_IPV6
:
107 case EFX_ENCAP_TYPE_GENEVE
| EFX_ENCAP_FLAG_IPV6
:
108 flow6
.flowi6_proto
= IPPROTO_UDP
;
109 flow6
.fl6_dport
= encap
->key
.tp_dst
;
110 flow6
.flowlabel
= ip6_make_flowinfo(encap
->key
.tos
,
112 flow6
.daddr
= encap
->key
.u
.ipv6
.dst
;
113 flow6
.saddr
= encap
->key
.u
.ipv6
.src
;
116 NL_SET_ERR_MSG_FMT_MOD(extack
, "Unsupported encap type %d",
121 neigh
= kzalloc(sizeof(*neigh
), GFP_KERNEL_ACCOUNT
);
124 neigh
->net
= get_net_track(net
, &neigh
->ns_tracker
, GFP_KERNEL_ACCOUNT
);
125 neigh
->dst_ip
= flow4
.daddr
;
126 neigh
->dst_ip6
= flow6
.daddr
;
128 old
= rhashtable_lookup_get_insert_fast(&efx
->tc
->neigh_ht
,
130 efx_neigh_ht_params
);
132 /* don't need our new entry */
133 put_net_track(neigh
->net
, &neigh
->ns_tracker
);
135 if (IS_ERR(old
)) /* oh dear, it's actually an error */
137 if (!refcount_inc_not_zero(&old
->ref
))
139 /* existing entry found, ref taken */
142 /* New entry. We need to initiate a lookup */
146 if (encap
->type
& EFX_ENCAP_FLAG_IPV6
) {
147 #if IS_ENABLED(CONFIG_IPV6)
148 struct dst_entry
*dst
;
150 dst
= ipv6_stub
->ipv6_dst_lookup_flow(net
, NULL
, &flow6
,
152 rc
= PTR_ERR_OR_ZERO(dst
);
154 NL_SET_ERR_MSG_MOD(extack
, "Failed to lookup route for IPv6 encap");
157 neigh
->egdev
= dst
->dev
;
158 netdev_hold(neigh
->egdev
, &neigh
->dev_tracker
,
160 neigh
->ttl
= ip6_dst_hoplimit(dst
);
161 n
= dst_neigh_lookup(dst
, &flow6
.daddr
);
164 /* We shouldn't ever get here, because if IPv6 isn't
165 * enabled how did someone create an IPv6 tunnel_key?
168 NL_SET_ERR_MSG_MOD(extack
, "No IPv6 support (neigh bind)");
172 rt
= ip_route_output_key(net
, &flow4
);
173 if (IS_ERR_OR_NULL(rt
)) {
174 rc
= PTR_ERR_OR_ZERO(rt
);
177 NL_SET_ERR_MSG_MOD(extack
, "Failed to lookup route for encap");
180 neigh
->egdev
= rt
->dst
.dev
;
181 netdev_hold(neigh
->egdev
, &neigh
->dev_tracker
,
183 neigh
->ttl
= ip4_dst_hoplimit(&rt
->dst
);
184 n
= dst_neigh_lookup(&rt
->dst
, &flow4
.daddr
);
189 NL_SET_ERR_MSG_MOD(extack
, "Failed to lookup neighbour for encap");
190 netdev_put(neigh
->egdev
, &neigh
->dev_tracker
);
193 refcount_set(&neigh
->ref
, 1);
194 INIT_LIST_HEAD(&neigh
->users
);
195 read_lock_bh(&n
->lock
);
196 ether_addr_copy(neigh
->ha
, n
->ha
);
197 neigh
->n_valid
= n
->nud_state
& NUD_VALID
;
198 read_unlock_bh(&n
->lock
);
199 rwlock_init(&neigh
->lock
);
200 INIT_WORK(&neigh
->work
, efx_neigh_update
);
202 neigh
->used
= jiffies
;
204 /* Prod ARP to find us a neighbour */
205 neigh_event_send(n
, NULL
);
208 /* Add us to this neigh */
209 encap
->neigh
= neigh
;
210 list_add_tail(&encap
->list
, &neigh
->users
);
214 /* cleanup common to several error paths */
215 rhashtable_remove_fast(&efx
->tc
->neigh_ht
, &neigh
->linkage
,
216 efx_neigh_ht_params
);
218 put_net_track(net
, &neigh
->ns_tracker
);
223 static void efx_free_neigh(struct efx_neigh_binder
*neigh
)
225 struct efx_nic
*efx
= neigh
->efx
;
227 rhashtable_remove_fast(&efx
->tc
->neigh_ht
, &neigh
->linkage
,
228 efx_neigh_ht_params
);
230 netdev_put(neigh
->egdev
, &neigh
->dev_tracker
);
231 put_net_track(neigh
->net
, &neigh
->ns_tracker
);
235 static void efx_release_neigh(struct efx_nic
*efx
,
236 struct efx_tc_encap_action
*encap
)
238 struct efx_neigh_binder
*neigh
= encap
->neigh
;
242 list_del(&encap
->list
);
244 if (!refcount_dec_and_test(&neigh
->ref
))
245 return; /* still in use */
246 efx_free_neigh(neigh
);
249 static void efx_gen_tun_header_eth(struct efx_tc_encap_action
*encap
, u16 proto
)
251 struct efx_neigh_binder
*neigh
= encap
->neigh
;
254 encap
->encap_hdr_len
= sizeof(*eth
);
255 eth
= (struct ethhdr
*)encap
->encap_hdr
;
257 if (encap
->neigh
->n_valid
)
258 ether_addr_copy(eth
->h_dest
, neigh
->ha
);
260 eth_zero_addr(eth
->h_dest
);
261 ether_addr_copy(eth
->h_source
, neigh
->egdev
->dev_addr
);
262 eth
->h_proto
= htons(proto
);
265 static void efx_gen_tun_header_ipv4(struct efx_tc_encap_action
*encap
, u8 ipproto
, u8 len
)
267 struct efx_neigh_binder
*neigh
= encap
->neigh
;
268 struct ip_tunnel_key
*key
= &encap
->key
;
271 ip
= (struct iphdr
*)(encap
->encap_hdr
+ encap
->encap_hdr_len
);
272 encap
->encap_hdr_len
+= sizeof(*ip
);
274 ip
->daddr
= key
->u
.ipv4
.dst
;
275 ip
->saddr
= key
->u
.ipv4
.src
;
276 ip
->ttl
= neigh
->ttl
;
277 ip
->protocol
= ipproto
;
280 ip
->tot_len
= cpu_to_be16(ip
->ihl
* 4 + len
);
285 static void efx_gen_tun_header_ipv6(struct efx_tc_encap_action
*encap
, u8 ipproto
, u8 len
)
287 struct efx_neigh_binder
*neigh
= encap
->neigh
;
288 struct ip_tunnel_key
*key
= &encap
->key
;
291 ip
= (struct ipv6hdr
*)(encap
->encap_hdr
+ encap
->encap_hdr_len
);
292 encap
->encap_hdr_len
+= sizeof(*ip
);
294 ip6_flow_hdr(ip
, key
->tos
, key
->label
);
295 ip
->daddr
= key
->u
.ipv6
.dst
;
296 ip
->saddr
= key
->u
.ipv6
.src
;
297 ip
->hop_limit
= neigh
->ttl
;
298 ip
->nexthdr
= ipproto
;
300 ip
->payload_len
= cpu_to_be16(len
);
304 static void efx_gen_tun_header_udp(struct efx_tc_encap_action
*encap
, u8 len
)
306 struct ip_tunnel_key
*key
= &encap
->key
;
309 udp
= (struct udphdr
*)(encap
->encap_hdr
+ encap
->encap_hdr_len
);
310 encap
->encap_hdr_len
+= sizeof(*udp
);
312 udp
->dest
= key
->tp_dst
;
313 udp
->len
= cpu_to_be16(sizeof(*udp
) + len
);
316 static void efx_gen_tun_header_vxlan(struct efx_tc_encap_action
*encap
)
318 struct ip_tunnel_key
*key
= &encap
->key
;
319 struct vxlanhdr
*vxlan
;
321 vxlan
= (struct vxlanhdr
*)(encap
->encap_hdr
+ encap
->encap_hdr_len
);
322 encap
->encap_hdr_len
+= sizeof(*vxlan
);
324 vxlan
->vx_flags
= VXLAN_HF_VNI
;
325 vxlan
->vx_vni
= vxlan_vni_field(tunnel_id_to_key32(key
->tun_id
));
328 static void efx_gen_tun_header_geneve(struct efx_tc_encap_action
*encap
)
330 struct ip_tunnel_key
*key
= &encap
->key
;
331 struct genevehdr
*geneve
;
334 geneve
= (struct genevehdr
*)(encap
->encap_hdr
+ encap
->encap_hdr_len
);
335 encap
->encap_hdr_len
+= sizeof(*geneve
);
337 geneve
->proto_type
= htons(ETH_P_TEB
);
338 /* convert tun_id to host-endian so we can use host arithmetic to
339 * extract individual bytes.
341 vni
= ntohl(tunnel_id_to_key32(key
->tun_id
));
342 geneve
->vni
[0] = vni
>> 16;
343 geneve
->vni
[1] = vni
>> 8;
344 geneve
->vni
[2] = vni
;
347 #define vxlan_header_l4_len (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
348 #define vxlan4_header_len (sizeof(struct ethhdr) + sizeof(struct iphdr) + vxlan_header_l4_len)
349 static void efx_gen_vxlan_header_ipv4(struct efx_tc_encap_action
*encap
)
351 BUILD_BUG_ON(sizeof(encap
->encap_hdr
) < vxlan4_header_len
);
352 efx_gen_tun_header_eth(encap
, ETH_P_IP
);
353 efx_gen_tun_header_ipv4(encap
, IPPROTO_UDP
, vxlan_header_l4_len
);
354 efx_gen_tun_header_udp(encap
, sizeof(struct vxlanhdr
));
355 efx_gen_tun_header_vxlan(encap
);
358 #define geneve_header_l4_len (sizeof(struct udphdr) + sizeof(struct genevehdr))
359 #define geneve4_header_len (sizeof(struct ethhdr) + sizeof(struct iphdr) + geneve_header_l4_len)
360 static void efx_gen_geneve_header_ipv4(struct efx_tc_encap_action
*encap
)
362 BUILD_BUG_ON(sizeof(encap
->encap_hdr
) < geneve4_header_len
);
363 efx_gen_tun_header_eth(encap
, ETH_P_IP
);
364 efx_gen_tun_header_ipv4(encap
, IPPROTO_UDP
, geneve_header_l4_len
);
365 efx_gen_tun_header_udp(encap
, sizeof(struct genevehdr
));
366 efx_gen_tun_header_geneve(encap
);
370 #define vxlan6_header_len (sizeof(struct ethhdr) + sizeof(struct ipv6hdr) + vxlan_header_l4_len)
371 static void efx_gen_vxlan_header_ipv6(struct efx_tc_encap_action
*encap
)
373 BUILD_BUG_ON(sizeof(encap
->encap_hdr
) < vxlan6_header_len
);
374 efx_gen_tun_header_eth(encap
, ETH_P_IPV6
);
375 efx_gen_tun_header_ipv6(encap
, IPPROTO_UDP
, vxlan_header_l4_len
);
376 efx_gen_tun_header_udp(encap
, sizeof(struct vxlanhdr
));
377 efx_gen_tun_header_vxlan(encap
);
380 #define geneve6_header_len (sizeof(struct ethhdr) + sizeof(struct ipv6hdr) + geneve_header_l4_len)
381 static void efx_gen_geneve_header_ipv6(struct efx_tc_encap_action
*encap
)
383 BUILD_BUG_ON(sizeof(encap
->encap_hdr
) < geneve6_header_len
);
384 efx_gen_tun_header_eth(encap
, ETH_P_IPV6
);
385 efx_gen_tun_header_ipv6(encap
, IPPROTO_UDP
, geneve_header_l4_len
);
386 efx_gen_tun_header_udp(encap
, sizeof(struct genevehdr
));
387 efx_gen_tun_header_geneve(encap
);
391 static void efx_gen_encap_header(struct efx_nic
*efx
,
392 struct efx_tc_encap_action
*encap
)
394 encap
->n_valid
= encap
->neigh
->n_valid
;
396 /* GCC stupidly thinks that only values explicitly listed in the enum
397 * definition can _possibly_ be sensible case values, so without this
398 * cast it complains about the IPv6 versions.
400 switch ((int)encap
->type
) {
401 case EFX_ENCAP_TYPE_VXLAN
:
402 efx_gen_vxlan_header_ipv4(encap
);
404 case EFX_ENCAP_TYPE_GENEVE
:
405 efx_gen_geneve_header_ipv4(encap
);
408 case EFX_ENCAP_TYPE_VXLAN
| EFX_ENCAP_FLAG_IPV6
:
409 efx_gen_vxlan_header_ipv6(encap
);
411 case EFX_ENCAP_TYPE_GENEVE
| EFX_ENCAP_FLAG_IPV6
:
412 efx_gen_geneve_header_ipv6(encap
);
416 /* unhandled encap type, can't happen */
418 netif_err(efx
, drv
, efx
->net_dev
,
419 "Bogus encap type %d, can't generate\n",
422 /* Use fallback action. */
423 encap
->n_valid
= false;
428 static void efx_tc_update_encap(struct efx_nic
*efx
,
429 struct efx_tc_encap_action
*encap
)
431 struct efx_tc_action_set_list
*acts
, *fallback
;
432 struct efx_tc_flow_rule
*rule
;
433 struct efx_tc_action_set
*act
;
436 if (encap
->n_valid
) {
437 /* Make sure no rules are using this encap while we change it */
438 list_for_each_entry(act
, &encap
->users
, encap_user
) {
440 if (WARN_ON(!acts
)) /* can't happen */
442 rule
= container_of(acts
, struct efx_tc_flow_rule
, acts
);
444 fallback
= rule
->fallback
;
445 else /* fallback fallback: deliver to PF */
446 fallback
= &efx
->tc
->facts
.pf
;
447 rc
= efx_mae_update_rule(efx
, fallback
->fw_id
,
450 netif_err(efx
, drv
, efx
->net_dev
,
451 "Failed to update (f) rule %08x rc %d\n",
454 netif_dbg(efx
, drv
, efx
->net_dev
, "Updated (f) rule %08x\n",
459 /* Make sure we don't leak arbitrary bytes on the wire;
460 * set an all-0s ethernet header. A successful call to
461 * efx_gen_encap_header() will overwrite this.
463 memset(encap
->encap_hdr
, 0, sizeof(encap
->encap_hdr
));
464 encap
->encap_hdr_len
= ETH_HLEN
;
467 read_lock_bh(&encap
->neigh
->lock
);
468 efx_gen_encap_header(efx
, encap
);
469 read_unlock_bh(&encap
->neigh
->lock
);
471 encap
->n_valid
= false;
474 rc
= efx_mae_update_encap_md(efx
, encap
);
476 netif_err(efx
, drv
, efx
->net_dev
,
477 "Failed to update encap hdr %08x rc %d\n",
481 netif_dbg(efx
, drv
, efx
->net_dev
, "Updated encap hdr %08x\n",
485 /* Update rule users: use the action if they are now ready */
486 list_for_each_entry(act
, &encap
->users
, encap_user
) {
488 if (WARN_ON(!acts
)) /* can't happen */
490 rule
= container_of(acts
, struct efx_tc_flow_rule
, acts
);
491 if (!efx_tc_check_ready(efx
, rule
))
493 rc
= efx_mae_update_rule(efx
, acts
->fw_id
, rule
->fw_id
);
495 netif_err(efx
, drv
, efx
->net_dev
,
496 "Failed to update rule %08x rc %d\n",
499 netif_dbg(efx
, drv
, efx
->net_dev
, "Updated rule %08x\n",
504 static void efx_neigh_update(struct work_struct
*work
)
506 struct efx_neigh_binder
*neigh
= container_of(work
, struct efx_neigh_binder
, work
);
507 struct efx_tc_encap_action
*encap
;
508 struct efx_nic
*efx
= neigh
->efx
;
510 mutex_lock(&efx
->tc
->mutex
);
511 list_for_each_entry(encap
, &neigh
->users
, list
)
512 efx_tc_update_encap(neigh
->efx
, encap
);
513 /* release ref taken in efx_neigh_event() */
514 if (refcount_dec_and_test(&neigh
->ref
))
515 efx_free_neigh(neigh
);
516 mutex_unlock(&efx
->tc
->mutex
);
519 static int efx_neigh_event(struct efx_nic
*efx
, struct neighbour
*n
)
521 struct efx_neigh_binder keys
= {NULL
}, *neigh
;
522 bool n_valid
, ipv6
= false;
526 if (WARN_ON(!efx
->tc
))
529 if (n
->tbl
== &arp_tbl
) {
530 keysize
= sizeof(keys
.dst_ip
);
531 #if IS_ENABLED(CONFIG_IPV6)
532 } else if (n
->tbl
== ipv6_stub
->nd_tbl
) {
534 keysize
= sizeof(keys
.dst_ip6
);
540 netif_warn(efx
, drv
, efx
->net_dev
, "neigh_event with no parms!\n");
543 keys
.net
= read_pnet(&n
->parms
->net
);
544 if (n
->tbl
->key_len
!= keysize
) {
545 netif_warn(efx
, drv
, efx
->net_dev
, "neigh_event with bad key_len %u\n",
549 read_lock_bh(&n
->lock
); /* Get a consistent view */
550 memcpy(ha
, n
->ha
, ETH_ALEN
);
551 n_valid
= (n
->nud_state
& NUD_VALID
) && !n
->dead
;
552 read_unlock_bh(&n
->lock
);
554 memcpy(&keys
.dst_ip6
, n
->primary_key
, n
->tbl
->key_len
);
556 memcpy(&keys
.dst_ip
, n
->primary_key
, n
->tbl
->key_len
);
558 neigh
= rhashtable_lookup_fast(&efx
->tc
->neigh_ht
, &keys
,
559 efx_neigh_ht_params
);
560 if (!neigh
|| neigh
->dying
)
561 /* We're not interested in this neighbour */
563 write_lock_bh(&neigh
->lock
);
564 if (n_valid
== neigh
->n_valid
&& !memcmp(ha
, neigh
->ha
, ETH_ALEN
)) {
565 write_unlock_bh(&neigh
->lock
);
566 /* Nothing has changed; no work to do */
569 neigh
->n_valid
= n_valid
;
570 memcpy(neigh
->ha
, ha
, ETH_ALEN
);
571 write_unlock_bh(&neigh
->lock
);
572 if (refcount_inc_not_zero(&neigh
->ref
)) {
574 if (!schedule_work(&neigh
->work
))
575 /* failed to schedule, release the ref we just took */
576 if (refcount_dec_and_test(&neigh
->ref
))
577 efx_free_neigh(neigh
);
585 bool efx_tc_check_ready(struct efx_nic
*efx
, struct efx_tc_flow_rule
*rule
)
587 struct efx_tc_action_set
*act
;
589 /* Encap actions can only be offloaded if they have valid
590 * neighbour info for the outer Ethernet header.
592 list_for_each_entry(act
, &rule
->acts
.list
, list
)
593 if (act
->encap_md
&& !act
->encap_md
->n_valid
)
598 struct efx_tc_encap_action
*efx_tc_flower_create_encap_md(
599 struct efx_nic
*efx
, const struct ip_tunnel_info
*info
,
600 struct net_device
*egdev
, struct netlink_ext_ack
*extack
)
602 enum efx_encap_type type
= efx_tc_indr_netdev_type(egdev
);
603 struct efx_tc_encap_action
*encap
, *old
;
604 struct efx_rep
*to_efv
;
607 if (type
== EFX_ENCAP_TYPE_NONE
) {
608 /* dest is not an encap device */
609 NL_SET_ERR_MSG_MOD(extack
, "Not a (supported) tunnel device but tunnel_key is set");
610 return ERR_PTR(-EOPNOTSUPP
);
612 rc
= efx_mae_check_encap_type_supported(efx
, type
);
614 NL_SET_ERR_MSG_MOD(extack
, "Firmware reports no support for this tunnel type");
617 /* No support yet for Geneve options */
618 if (info
->options_len
) {
619 NL_SET_ERR_MSG_MOD(extack
, "Unsupported tunnel options");
620 return ERR_PTR(-EOPNOTSUPP
);
622 switch (info
->mode
) {
623 case IP_TUNNEL_INFO_TX
:
625 case IP_TUNNEL_INFO_TX
| IP_TUNNEL_INFO_IPV6
:
626 type
|= EFX_ENCAP_FLAG_IPV6
;
629 NL_SET_ERR_MSG_FMT_MOD(extack
, "Unsupported tunnel mode %u",
631 return ERR_PTR(-EOPNOTSUPP
);
633 encap
= kzalloc(sizeof(*encap
), GFP_KERNEL_ACCOUNT
);
635 return ERR_PTR(-ENOMEM
);
637 encap
->key
= info
->key
;
638 INIT_LIST_HEAD(&encap
->users
);
639 old
= rhashtable_lookup_get_insert_fast(&efx
->tc
->encap_ht
,
641 efx_tc_encap_ht_params
);
643 /* don't need our new entry */
645 if (IS_ERR(old
)) /* oh dear, it's actually an error */
646 return ERR_CAST(old
);
647 if (!refcount_inc_not_zero(&old
->ref
))
648 return ERR_PTR(-EAGAIN
);
649 /* existing entry found, ref taken */
653 rc
= efx_bind_neigh(efx
, encap
, dev_net(egdev
), extack
);
656 to_efv
= efx_tc_flower_lookup_efv(efx
, encap
->neigh
->egdev
);
657 if (IS_ERR(to_efv
)) {
658 /* neigh->egdev isn't ours */
659 NL_SET_ERR_MSG_MOD(extack
, "Tunnel egress device not on switch");
660 rc
= PTR_ERR(to_efv
);
663 rc
= efx_tc_flower_external_mport(efx
, to_efv
);
665 NL_SET_ERR_MSG_MOD(extack
, "Failed to identify tunnel egress m-port");
668 encap
->dest_mport
= rc
;
669 read_lock_bh(&encap
->neigh
->lock
);
670 efx_gen_encap_header(efx
, encap
);
671 read_unlock_bh(&encap
->neigh
->lock
);
673 rc
= efx_mae_allocate_encap_md(efx
, encap
);
675 NL_SET_ERR_MSG_MOD(extack
, "Failed to write tunnel header to hw");
680 refcount_set(&encap
->ref
, 1);
683 efx_release_neigh(efx
, encap
);
685 rhashtable_remove_fast(&efx
->tc
->encap_ht
, &encap
->linkage
,
686 efx_tc_encap_ht_params
);
691 void efx_tc_flower_release_encap_md(struct efx_nic
*efx
,
692 struct efx_tc_encap_action
*encap
)
694 if (!refcount_dec_and_test(&encap
->ref
))
695 return; /* still in use */
696 efx_release_neigh(efx
, encap
);
697 rhashtable_remove_fast(&efx
->tc
->encap_ht
, &encap
->linkage
,
698 efx_tc_encap_ht_params
);
699 efx_mae_free_encap_md(efx
, encap
);
703 static void efx_tc_remove_neigh_users(struct efx_nic
*efx
, struct efx_neigh_binder
*neigh
)
705 struct efx_tc_encap_action
*encap
, *next
;
707 list_for_each_entry_safe(encap
, next
, &neigh
->users
, list
) {
708 /* Should cause neigh usage count to fall to zero, freeing it */
709 efx_release_neigh(efx
, encap
);
710 /* The encap has lost its neigh, so it's now unready */
711 efx_tc_update_encap(efx
, encap
);
715 void efx_tc_unregister_egdev(struct efx_nic
*efx
, struct net_device
*net_dev
)
717 struct efx_neigh_binder
*neigh
;
718 struct rhashtable_iter walk
;
720 mutex_lock(&efx
->tc
->mutex
);
721 rhashtable_walk_enter(&efx
->tc
->neigh_ht
, &walk
);
722 rhashtable_walk_start(&walk
);
723 while ((neigh
= rhashtable_walk_next(&walk
)) != NULL
) {
726 if (neigh
->egdev
!= net_dev
)
729 rhashtable_walk_stop(&walk
);
730 synchronize_rcu(); /* Make sure any updates see dying flag */
731 efx_tc_remove_neigh_users(efx
, neigh
); /* might sleep */
732 rhashtable_walk_start(&walk
);
734 rhashtable_walk_stop(&walk
);
735 rhashtable_walk_exit(&walk
);
736 mutex_unlock(&efx
->tc
->mutex
);
739 int efx_tc_netevent_event(struct efx_nic
*efx
, unsigned long event
,
742 if (efx
->type
->is_vf
)
746 case NETEVENT_NEIGH_UPDATE
:
747 return efx_neigh_event(efx
, ptr
);