Skip to content

Commit 1c3b4cc

Browse files
liuhangbinkernel-patches-bot
authored andcommitted
xdp: add a new helper for dev map multicast support
This patch is for xdp multicast support, which has been discussed before[0], The goal is to be able to implement an OVS-like data plane in XDP, i.e., a software switch that can forward XDP frames to multiple ports. To achieve this, an application needs to specify a group of interfaces to forward a packet to. It is also common to want to exclude one or more physical interfaces from the forwarding operation - e.g., to forward a packet to all interfaces in the multicast group except the interface it arrived on. While this could be done simply by adding more groups, this quickly leads to a combinatorial explosion in the number of groups an application has to maintain. To avoid the combinatorial explosion, we propose to include the ability to specify an "exclude group" as part of the forwarding operation. This needs to be a group (instead of just a single port index), because a physical interface can be part of a logical grouping, such as a bond device. Thus, the logical forwarding operation becomes a "set difference" operation, i.e. "forward to all ports in group A that are not also in group B". This series implements such an operation using device maps to represent the groups. This means that the XDP program specifies two device maps, one containing the list of netdevs to redirect to, and the other containing the exclude list. To achieve this, a new helper bpf_redirect_map_multi() is implemented to accept two maps, the forwarding map and exclude map. The forwarding map could be DEVMAP or DEVMAP_HASH, but the exclude map *must* be DEVMAP_HASH to get better performace. If user don't want to use exclude map and just want simply stop redirecting back to ingress device, they can use flag BPF_F_EXCLUDE_INGRESS. As both bpf_xdp_redirect_map() and this new helpers are using struct bpf_redirect_info, a new field ex_map is added and tgt_value is set to NULL in the new helper to make a difference with bpf_xdp_redirect_map(). At last, keep the general data path in net/core/filter.c, the native data path in kernel/bpf/devmap.c so we can use direct calls to get better performace. [0] https://xdp-project.net/#Handling-multicast Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> v16-v18: no update v15: a) Update bpf_redirect_map_multi() helper description that ex_map must be keyed by ifindex. b) remove variable last_one in dev_map_enqueue_multi() as it's pointless. c) add a comment about why we don't use READ/WRITE_ONCE() for ex_map. v14: no update, only rebase the code v13: pass xdp_prog through bq_enqueue v12: rebase the code based on Jespoer's devmap xdp_prog patch v11: Fix bpf_redirect_map_multi() helper description typo. Add loop limit for devmap_get_next_obj() and dev_map_redirect_multi(). v10: Update helper bpf_xdp_redirect_map_multi() - No need to check map pointer as we will do the check in verifier. v9: Update helper bpf_xdp_redirect_map_multi() - Use ARG_CONST_MAP_PTR_OR_NULL for helper arg2 v8: Update function dev_in_exclude_map(): - remove duplicate ex_map map_type check in - lookup the element in dev map by obj dev index directly instead of looping all the map v7: a) Fix helper flag check b) Limit the *ex_map* to use DEVMAP_HASH only and update function dev_in_exclude_map() to get better performance. v6: converted helper return types from int to long v5: a) Check devmap_get_next_key() return value. b) Pass through flags to __bpf_tx_xdp_map() instead of bool value. c) In function dev_map_enqueue_multi(), consume xdpf for the last obj instead of the first on. d) Update helper description and code comments to explain that we use NULL target value to distinguish multicast and unicast forwarding. e) Update memory model, memory id and frame_sz in xdpf_clone(). v4: Fix bpf_xdp_redirect_map_multi_proto arg2_type typo v3: Based on Toke's suggestion, do the following update a) Update bpf_redirect_map_multi() description in bpf.h. b) Fix exclude_ifindex checking order in dev_in_exclude_map(). c) Fix one more xdpf clone in dev_map_enqueue_multi(). d) Go find next one in dev_map_enqueue_multi() if the interface is not able to forward instead of abort the whole loop. e) Remove READ_ONCE/WRITE_ONCE for ex_map. v2: Add new syscall bpf_xdp_redirect_map_multi() which could accept include/exclude maps directly.
1 parent 7622a89 commit 1c3b4cc

File tree

9 files changed

+360
-5
lines changed

9 files changed

+360
-5
lines changed

include/linux/bpf.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,11 @@ int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
14411441
struct net_device *dev_rx);
14421442
int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
14431443
struct net_device *dev_rx);
1444+
bool dev_in_exclude_map(struct bpf_dtab_netdev *obj, struct bpf_map *map,
1445+
int exclude_ifindex);
1446+
int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1447+
struct bpf_map *map, struct bpf_map *ex_map,
1448+
u32 flags);
14441449
int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
14451450
struct bpf_prog *xdp_prog);
14461451
bool dev_map_can_have_prog(struct bpf_map *map);
@@ -1609,6 +1614,21 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
16091614
return 0;
16101615
}
16111616

1617+
static inline
1618+
bool dev_in_exclude_map(struct bpf_dtab_netdev *obj, struct bpf_map *map,
1619+
int exclude_ifindex)
1620+
{
1621+
return false;
1622+
}
1623+
1624+
static inline
1625+
int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1626+
struct bpf_map *map, struct bpf_map *ex_map,
1627+
u32 flags)
1628+
{
1629+
return 0;
1630+
}
1631+
16121632
struct sk_buff;
16131633

16141634
static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,

include/linux/filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ struct bpf_redirect_info {
637637
u32 tgt_index;
638638
void *tgt_value;
639639
struct bpf_map *map;
640+
struct bpf_map *ex_map;
640641
u32 kern_flags;
641642
struct bpf_nh_params nh;
642643
};

include/net/xdp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
170170
struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
171171
struct net_device *dev);
172172
int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp);
173+
struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
173174

174175
static inline
175176
void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)

include/uapi/linux/bpf.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3836,6 +3836,28 @@ union bpf_attr {
38363836
* Return
38373837
* A pointer to a struct socket on success or NULL if the file is
38383838
* not a socket.
3839+
*
3840+
* long bpf_redirect_map_multi(struct bpf_map *map, struct bpf_map *ex_map, u64 flags)
3841+
* Description
3842+
* This is a multicast implementation for XDP redirect. It will
3843+
* redirect the packet to ALL the interfaces in *map*, but
3844+
* exclude the interfaces in *ex_map*.
3845+
*
3846+
* The forwarding *map* could be either BPF_MAP_TYPE_DEVMAP or
3847+
* BPF_MAP_TYPE_DEVMAP_HASH. To get better performance, the
3848+
* *ex_map* is limited to BPF_MAP_TYPE_DEVMAP_HASH and must be
3849+
* keyed by ifindex for the helper to work.
3850+
*
3851+
* Currently the *flags* only supports *BPF_F_EXCLUDE_INGRESS*,
3852+
* which additionally excludes the current ingress device.
3853+
*
3854+
* See also bpf_redirect_map() as a unicast implementation,
3855+
* which supports redirecting packet to a specific ifindex
3856+
* in the map. As both helpers use struct bpf_redirect_info
3857+
* to store the redirect info, we will use a a NULL tgt_value
3858+
* to distinguish multicast and unicast redirecting.
3859+
* Return
3860+
* **XDP_REDIRECT** on success, or **XDP_ABORTED** on error.
38393861
*/
38403862
#define __BPF_FUNC_MAPPER(FN) \
38413863
FN(unspec), \
@@ -4001,6 +4023,7 @@ union bpf_attr {
40014023
FN(ktime_get_coarse_ns), \
40024024
FN(ima_inode_hash), \
40034025
FN(sock_from_file), \
4026+
FN(redirect_map_multi), \
40044027
/* */
40054028

40064029
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4177,6 +4200,11 @@ enum {
41774200
BPF_F_BPRM_SECUREEXEC = (1ULL << 0),
41784201
};
41794202

4203+
/* BPF_FUNC_redirect_map_multi flags. */
4204+
enum {
4205+
BPF_F_EXCLUDE_INGRESS = (1ULL << 0),
4206+
};
4207+
41804208
#define __bpf_md_ptr(type, name) \
41814209
union { \
41824210
type name; \

kernel/bpf/devmap.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,134 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
519519
return __xdp_enqueue(dev, xdp, dev_rx, dst->xdp_prog);
520520
}
521521

522+
/* Use direct call in fast path instead of map->ops->map_get_next_key() */
523+
static int devmap_get_next_key(struct bpf_map *map, void *key, void *next_key)
524+
{
525+
526+
switch (map->map_type) {
527+
case BPF_MAP_TYPE_DEVMAP:
528+
return dev_map_get_next_key(map, key, next_key);
529+
case BPF_MAP_TYPE_DEVMAP_HASH:
530+
return dev_map_hash_get_next_key(map, key, next_key);
531+
default:
532+
break;
533+
}
534+
535+
return -ENOENT;
536+
}
537+
538+
bool dev_in_exclude_map(struct bpf_dtab_netdev *obj, struct bpf_map *map,
539+
int exclude_ifindex)
540+
{
541+
if (obj->dev->ifindex == exclude_ifindex)
542+
return true;
543+
544+
if (!map)
545+
return false;
546+
547+
return __dev_map_hash_lookup_elem(map, obj->dev->ifindex) != NULL;
548+
}
549+
550+
static struct bpf_dtab_netdev *devmap_get_next_obj(struct xdp_buff *xdp, struct bpf_map *map,
551+
struct bpf_map *ex_map, u32 *key,
552+
u32 *next_key, int ex_ifindex)
553+
{
554+
struct bpf_dtab_netdev *obj;
555+
struct net_device *dev;
556+
u32 *tmp_key = key;
557+
u32 index;
558+
int err;
559+
560+
err = devmap_get_next_key(map, tmp_key, next_key);
561+
if (err)
562+
return NULL;
563+
564+
/* When using dev map hash, we could restart the hashtab traversal
565+
* in case the key has been updated/removed in the mean time.
566+
* So we may end up potentially looping due to traversal restarts
567+
* from first elem.
568+
*
569+
* Let's use map's max_entries to limit the loop number.
570+
*/
571+
for (index = 0; index < map->max_entries; index++) {
572+
switch (map->map_type) {
573+
case BPF_MAP_TYPE_DEVMAP:
574+
obj = __dev_map_lookup_elem(map, *next_key);
575+
break;
576+
case BPF_MAP_TYPE_DEVMAP_HASH:
577+
obj = __dev_map_hash_lookup_elem(map, *next_key);
578+
break;
579+
default:
580+
break;
581+
}
582+
583+
if (!obj || dev_in_exclude_map(obj, ex_map, ex_ifindex))
584+
goto find_next;
585+
586+
dev = obj->dev;
587+
588+
if (!dev->netdev_ops->ndo_xdp_xmit)
589+
goto find_next;
590+
591+
err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
592+
if (unlikely(err))
593+
goto find_next;
594+
595+
return obj;
596+
597+
find_next:
598+
tmp_key = next_key;
599+
err = devmap_get_next_key(map, tmp_key, next_key);
600+
if (err)
601+
break;
602+
}
603+
604+
return NULL;
605+
}
606+
607+
int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
608+
struct bpf_map *map, struct bpf_map *ex_map,
609+
u32 flags)
610+
{
611+
struct bpf_dtab_netdev *obj = NULL, *next_obj = NULL;
612+
struct xdp_frame *xdpf, *nxdpf;
613+
int ex_ifindex;
614+
u32 key, next_key;
615+
616+
ex_ifindex = flags & BPF_F_EXCLUDE_INGRESS ? dev_rx->ifindex : 0;
617+
618+
/* Find first available obj */
619+
obj = devmap_get_next_obj(xdp, map, ex_map, NULL, &key, ex_ifindex);
620+
if (!obj)
621+
return 0;
622+
623+
xdpf = xdp_convert_buff_to_frame(xdp);
624+
if (unlikely(!xdpf))
625+
return -EOVERFLOW;
626+
627+
for (;;) {
628+
/* Check if we still have one more available obj */
629+
next_obj = devmap_get_next_obj(xdp, map, ex_map, &key,
630+
&next_key, ex_ifindex);
631+
if (!next_obj) {
632+
bq_enqueue(obj->dev, xdpf, dev_rx, obj->xdp_prog);
633+
return 0;
634+
}
635+
636+
nxdpf = xdpf_clone(xdpf);
637+
if (unlikely(!nxdpf)) {
638+
xdp_return_frame_rx_napi(xdpf);
639+
return -ENOMEM;
640+
}
641+
642+
bq_enqueue(obj->dev, nxdpf, dev_rx, obj->xdp_prog);
643+
644+
/* Deal with next obj */
645+
obj = next_obj;
646+
key = next_key;
647+
}
648+
}
649+
522650
int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
523651
struct bpf_prog *xdp_prog)
524652
{

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4469,6 +4469,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
44694469
case BPF_MAP_TYPE_DEVMAP:
44704470
case BPF_MAP_TYPE_DEVMAP_HASH:
44714471
if (func_id != BPF_FUNC_redirect_map &&
4472+
func_id != BPF_FUNC_redirect_map_multi &&
44724473
func_id != BPF_FUNC_map_lookup_elem)
44734474
goto error;
44744475
break;
@@ -4573,6 +4574,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
45734574
map->map_type != BPF_MAP_TYPE_XSKMAP)
45744575
goto error;
45754576
break;
4577+
case BPF_FUNC_redirect_map_multi:
4578+
if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
4579+
map->map_type != BPF_MAP_TYPE_DEVMAP_HASH)
4580+
goto error;
4581+
break;
45764582
case BPF_FUNC_sk_redirect_map:
45774583
case BPF_FUNC_msg_redirect_map:
45784584
case BPF_FUNC_sock_map_update:

0 commit comments

Comments
 (0)