Skip to content

Commit

Permalink
xdp-forward: Introduce xdp-fwd-flowtable bpf sample
Browse files Browse the repository at this point in the history
Introduce xdp-fwd-flowtable sample in order to perform XDP_REDIRECT
between net_devices inserted in a netfilter flowtable.
xdp-fwd-flowtable relies on bpf_xdp_flow_lookup kfunc in order to
perform the lookup of a given flowtable entry based on a fib tuple of
incoming traffic. At the moment we are able to offload just TCP or UDP
netfilter flowtable entries to the xdp layer. The user is supposed to
configure the flowtable separately.

Signed-off-by: Lorenzo Bianconi <[email protected]>
  • Loading branch information
LorenzoBianconi authored and tohojo committed Oct 11, 2024
1 parent f8fffe4 commit 9db1ee6
Show file tree
Hide file tree
Showing 4 changed files with 728 additions and 1 deletion.
4 changes: 4 additions & 0 deletions headers/linux/hlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

struct list_head;

struct rhash_head {
struct rhash_head *next;
};

#define HLIST_POISON_POINTER_DELTA 0
#define HLIST_POISON1 ((void *) 0x100 + HLIST_POISON_POINTER_DELTA)
#define HLIST_POISON2 ((void *) 0x200 + HLIST_POISON_POINTER_DELTA)
Expand Down
114 changes: 114 additions & 0 deletions headers/linux/netfilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#ifndef _LINUX_NETFILTER_H
#define _LINUX_NETFILTER_H

#include <stdbool.h>
#include <linux/types.h>
#include <bpf/bpf_helpers.h>
#include <xdp/parsing_helpers.h>

#include "hlist.h"

struct flow_ports {
__be16 source, dest;
};

enum ip_conntrack_dir {
IP_CT_DIR_ORIGINAL,
IP_CT_DIR_REPLY,
IP_CT_DIR_MAX
};

enum flow_offload_tuple_dir {
FLOW_OFFLOAD_DIR_ORIGINAL = IP_CT_DIR_ORIGINAL,
FLOW_OFFLOAD_DIR_REPLY = IP_CT_DIR_REPLY,
FLOW_OFFLOAD_DIR_MAX = IP_CT_DIR_MAX,
};

enum flow_offload_type {
NF_FLOW_OFFLOAD_UNSPEC,
NF_FLOW_OFFLOAD_ROUTE,
};

enum nf_flow_flags {
NF_FLOW_SNAT,
NF_FLOW_DNAT,
NF_FLOW_TEARDOWN,
NF_FLOW_HW,
NF_FLOW_HW_DYING,
NF_FLOW_HW_DEAD,
NF_FLOW_HW_PENDING,
NF_FLOW_HW_BIDIRECTIONAL,
NF_FLOW_HW_ESTABLISHED,
};

enum flow_offload_xmit_type {
FLOW_OFFLOAD_XMIT_UNSPEC,
FLOW_OFFLOAD_XMIT_NEIGH,
FLOW_OFFLOAD_XMIT_XFRM,
FLOW_OFFLOAD_XMIT_DIRECT,
FLOW_OFFLOAD_XMIT_TC,
};

#define NF_FLOW_TABLE_ENCAP_MAX 2
struct flow_offload_tuple {
union {
struct in_addr src_v4;
struct in6_addr src_v6;
};
union {
struct in_addr dst_v4;
struct in6_addr dst_v6;
};
struct {
__be16 src_port;
__be16 dst_port;
};

int iifidx;

__u8 l3proto;
__u8 l4proto;
struct {
__u16 id;
__be16 proto;
} encap[NF_FLOW_TABLE_ENCAP_MAX];

/* All members above are keys for lookups, see flow_offload_hash(). */
struct { } __hash;

__u8 dir:2,
xmit_type:3,
encap_num:2,
in_vlan_ingress:2;
__u16 mtu;
union {
struct {
struct dst_entry *dst_cache;
__u32 dst_cookie;
};
struct {
__u32 ifidx;
__u32 hw_ifidx;
__u8 h_source[ETH_ALEN];
__u8 h_dest[ETH_ALEN];
} out;
struct {
__u32 iifidx;
} tc;
};
};

struct flow_offload_tuple_rhash {
struct rhash_head node;
struct flow_offload_tuple tuple;
};

struct flow_offload {
struct flow_offload_tuple_rhash tuplehash[FLOW_OFFLOAD_DIR_MAX];
struct nf_conn *ct;
unsigned long flags;
__u16 type;
__u32 timeout;
};

#endif /* _LINUX_NETFILTER_H */
2 changes: 1 addition & 1 deletion xdp-forward/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0

XDP_TARGETS := xdp_forward.bpf
XDP_TARGETS := xdp_forward.bpf xdp_flowtable.bpf
BPF_SKEL_TARGETS := $(XDP_TARGETS)

XDP_OBJ_INSTALL :=
Expand Down
Loading

0 comments on commit 9db1ee6

Please sign in to comment.