Skip to content

Commit d3f5c6a

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, I re-implement a new helper bpf_redirect_map_multi() 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, I add a new ex_map and set tgt_value to NULL in the new helper to make a difference with bpf_xdp_redirect_map(). Also I 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 Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
1 parent 1c47416 commit d3f5c6a

File tree

9 files changed

+356
-5
lines changed

9 files changed

+356
-5
lines changed

include/linux/bpf.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,11 @@ int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
14271427
struct net_device *dev_rx);
14281428
int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
14291429
struct net_device *dev_rx);
1430+
bool dev_in_exclude_map(struct bpf_dtab_netdev *obj, struct bpf_map *map,
1431+
int exclude_ifindex);
1432+
int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1433+
struct bpf_map *map, struct bpf_map *ex_map,
1434+
u32 flags);
14301435
int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
14311436
struct bpf_prog *xdp_prog);
14321437
bool dev_map_can_have_prog(struct bpf_map *map);
@@ -1595,6 +1600,21 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
15951600
return 0;
15961601
}
15971602

1603+
static inline
1604+
bool dev_in_exclude_map(struct bpf_dtab_netdev *obj, struct bpf_map *map,
1605+
int exclude_ifindex)
1606+
{
1607+
return false;
1608+
}
1609+
1610+
static inline
1611+
int dev_map_enqueue_multi(struct xdp_buff *xdp, struct net_device *dev_rx,
1612+
struct bpf_map *map, struct bpf_map *ex_map,
1613+
u32 flags)
1614+
{
1615+
return 0;
1616+
}
1617+
15981618
struct sk_buff;
15991619

16001620
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
@@ -164,6 +164,7 @@ void xdp_warn(const char *msg, const char *func, const int line);
164164
#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
165165

166166
struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
167+
struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
167168

168169
static inline
169170
void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)

include/uapi/linux/bpf.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3836,6 +3836,27 @@ 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. But the *ex_map* must be
3848+
* BPF_MAP_TYPE_DEVMAP_HASH to get better performance.
3849+
*
3850+
* Currently the *flags* only supports *BPF_F_EXCLUDE_INGRESS*,
3851+
* which additionally excludes the current ingress device.
3852+
*
3853+
* See also bpf_redirect_map() as a unicast implementation,
3854+
* which supports redirecting packet to a specific ifindex
3855+
* in the map. As both helpers use struct bpf_redirect_info
3856+
* to store the redirect info, we will use a a NULL tgt_value
3857+
* to distinguish multicast and unicast redirecting.
3858+
* Return
3859+
* **XDP_REDIRECT** on success, or **XDP_ABORTED** on error.
38393860
*/
38403861
#define __BPF_FUNC_MAPPER(FN) \
38413862
FN(unspec), \
@@ -4001,6 +4022,7 @@ union bpf_attr {
40014022
FN(ktime_get_coarse_ns), \
40024023
FN(ima_inode_hash), \
40034024
FN(sock_from_file), \
4025+
FN(redirect_map_multi), \
40044026
/* */
40054027

40064028
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4177,6 +4199,11 @@ enum {
41774199
BPF_F_BPRM_SECUREEXEC = (1ULL << 0),
41784200
};
41794201

4202+
/* BPF_FUNC_redirect_map_multi flags. */
4203+
enum {
4204+
BPF_F_EXCLUDE_INGRESS = (1ULL << 0),
4205+
};
4206+
41804207
#define __bpf_md_ptr(type, name) \
41814208
union { \
41824209
type name; \

kernel/bpf/devmap.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,138 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
526526
return __xdp_enqueue(dev, xdp, dev_rx, dst->xdp_prog);
527527
}
528528

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

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,6 +4461,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
44614461
case BPF_MAP_TYPE_DEVMAP:
44624462
case BPF_MAP_TYPE_DEVMAP_HASH:
44634463
if (func_id != BPF_FUNC_redirect_map &&
4464+
func_id != BPF_FUNC_redirect_map_multi &&
44644465
func_id != BPF_FUNC_map_lookup_elem)
44654466
goto error;
44664467
break;
@@ -4565,6 +4566,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
45654566
map->map_type != BPF_MAP_TYPE_XSKMAP)
45664567
goto error;
45674568
break;
4569+
case BPF_FUNC_redirect_map_multi:
4570+
if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
4571+
map->map_type != BPF_MAP_TYPE_DEVMAP_HASH)
4572+
goto error;
4573+
break;
45684574
case BPF_FUNC_sk_redirect_map:
45694575
case BPF_FUNC_msg_redirect_map:
45704576
case BPF_FUNC_sock_map_update:

0 commit comments

Comments
 (0)