Skip to content

Commit 915dd73

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>
1 parent f4d31a3 commit 915dd73

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
@@ -1438,6 +1438,11 @@ int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
14381438
struct net_device *dev_rx);
14391439
int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
14401440
struct net_device *dev_rx);
1441+
bool dev_in_exclude_map(struct bpf_dtab_netdev *obj, struct bpf_map *map,
1442+
int exclude_ifindex);
1443+
int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1444+
struct bpf_map *map, struct bpf_map *ex_map,
1445+
u32 flags);
14411446
int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
14421447
struct bpf_prog *xdp_prog);
14431448
bool dev_map_can_have_prog(struct bpf_map *map);
@@ -1606,6 +1611,21 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
16061611
return 0;
16071612
}
16081613

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

16111631
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
@@ -647,6 +647,7 @@ struct bpf_redirect_info {
647647
u32 tgt_index;
648648
void *tgt_value;
649649
struct bpf_map *map;
650+
struct bpf_map *ex_map;
650651
u32 kern_flags;
651652
struct bpf_nh_params nh;
652653
};

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
@@ -3844,6 +3844,28 @@ union bpf_attr {
38443844
* Return
38453845
* A pointer to a struct socket on success or NULL if the file is
38463846
* not a socket.
3847+
*
3848+
* long bpf_redirect_map_multi(struct bpf_map *map, struct bpf_map *ex_map, u64 flags)
3849+
* Description
3850+
* This is a multicast implementation for XDP redirect. It will
3851+
* redirect the packet to ALL the interfaces in *map*, but
3852+
* exclude the interfaces in *ex_map*.
3853+
*
3854+
* The forwarding *map* could be either BPF_MAP_TYPE_DEVMAP or
3855+
* BPF_MAP_TYPE_DEVMAP_HASH. To get better performance, the
3856+
* *ex_map* is limited to BPF_MAP_TYPE_DEVMAP_HASH and must be
3857+
* keyed by ifindex for the helper to work.
3858+
*
3859+
* Currently the *flags* only supports *BPF_F_EXCLUDE_INGRESS*,
3860+
* which additionally excludes the current ingress device.
3861+
*
3862+
* See also bpf_redirect_map() as a unicast implementation,
3863+
* which supports redirecting packet to a specific ifindex
3864+
* in the map. As both helpers use struct bpf_redirect_info
3865+
* to store the redirect info, we will use a a NULL tgt_value
3866+
* to distinguish multicast and unicast redirecting.
3867+
* Return
3868+
* **XDP_REDIRECT** on success, or **XDP_ABORTED** on error.
38473869
*/
38483870
#define __BPF_FUNC_MAPPER(FN) \
38493871
FN(unspec), \
@@ -4009,6 +4031,7 @@ union bpf_attr {
40094031
FN(ktime_get_coarse_ns), \
40104032
FN(ima_inode_hash), \
40114033
FN(sock_from_file), \
4034+
FN(redirect_map_multi), \
40124035
/* */
40134036

40144037
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4185,6 +4208,11 @@ enum {
41854208
BPF_F_BPRM_SECUREEXEC = (1ULL << 0),
41864209
};
41874210

4211+
/* BPF_FUNC_redirect_map_multi flags. */
4212+
enum {
4213+
BPF_F_EXCLUDE_INGRESS = (1ULL << 0),
4214+
};
4215+
41884216
#define __bpf_md_ptr(type, name) \
41894217
union { \
41904218
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
@@ -4799,6 +4799,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
47994799
case BPF_MAP_TYPE_DEVMAP:
48004800
case BPF_MAP_TYPE_DEVMAP_HASH:
48014801
if (func_id != BPF_FUNC_redirect_map &&
4802+
func_id != BPF_FUNC_redirect_map_multi &&
48024803
func_id != BPF_FUNC_map_lookup_elem)
48034804
goto error;
48044805
break;
@@ -4903,6 +4904,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
49034904
map->map_type != BPF_MAP_TYPE_XSKMAP)
49044905
goto error;
49054906
break;
4907+
case BPF_FUNC_redirect_map_multi:
4908+
if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
4909+
map->map_type != BPF_MAP_TYPE_DEVMAP_HASH)
4910+
goto error;
4911+
break;
49064912
case BPF_FUNC_sk_redirect_map:
49074913
case BPF_FUNC_msg_redirect_map:
49084914
case BPF_FUNC_sock_map_update:

0 commit comments

Comments
 (0)