Skip to content

Commit

Permalink
samples/bpf: playing with xdp_rxhash tools, still devel phase
Browse files Browse the repository at this point in the history
Trying out different use-cases with the new XDP rxhash feature
that I'm currently developing for the kernel.

The API is still not fixed.

The kernel patches needed have been posted upstream for feedback here:

 [RFC 0/5] XDP driver feature API and handling change  to xdp_buff
    http://lkml.kernel.org/r/149512205297.14733.15729847433404265933.stgit@firesoul

 [RFC 1/5] samples/bpf: xdp_tx_iptunnel make use of  map_data[]
    http://lkml.kernel.org/r/149512209298.14733.14668513619424960672.stgit@firesoul

 [RFC 2/5] mlx5: fix bug reading rss_hash_type from  CQE
    http://lkml.kernel.org/r/149512209807.14733.12774004120782832472.stgit@firesoul

 [RFC 3/5] net: introduce XDP driver features  interface
    http://lkml.kernel.org/r/149512210317.14733.15039298820296846614.stgit@firesoul

 [RFC 4/5] net: new XDP feature for reading HW rxhash  from drivers
    http://lkml.kernel.org/r/149512210827.14733.13997041998775151648.stgit@firesoul

 [RFC 5/5] mlx5: add XDP rxhash feature for driver  mlx5
    http://lkml.kernel.org/r/149512211338.14733.1908296344857176060.stgit@firesoul

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
  • Loading branch information
netoptimizer committed May 18, 2017
1 parent 58d2eaf commit 9647e1b
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 37 deletions.
74 changes: 62 additions & 12 deletions kernel/samples/bpf/xdp_rxhash_kern.c
Expand Up @@ -15,7 +15,7 @@

#include "bpf_helpers.h"

#define DEBUG 1
//#define DEBUG 1
#ifdef DEBUG
/* Only use this for debug output. Notice output from bpf_trace_printk()
* end-up in /sys/kernel/debug/tracing/trace_pipe (remember use cat)
Expand All @@ -27,7 +27,8 @@
##__VA_ARGS__); \
})
#else
# define bpf_debug(fmt, ...) { } while (0)
//# define bpf_debug(fmt, ...) { } while (0)
# define bpf_debug(fmt, ...)
#endif

struct bpf_map_def SEC("maps") rx_cnt = {
Expand Down Expand Up @@ -76,6 +77,48 @@ void stats_action_verdict(u32 action)
*value += 1;
}

/* Keep stats of hash_type for L3 (e.g IPv4, IPv6) and L4 (e.g UDP, TCP)
*
* Two small array are sufficient, as the supported types are limited.
* The type is stored in a 8-bit value, partitioned with 3-bits for L3
* and 5 bits for L4.
*/
struct bpf_map_def SEC("maps") stats_htype_L3 = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(u32),
.value_size = sizeof(long),
.max_entries = (1 << XDP_HASH_TYPE_L3_BITS),
};

struct bpf_map_def SEC("maps") stats_htype_L4 = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(u32),
.value_size = sizeof(long),
.max_entries = (1 << XDP_HASH_TYPE_L4_BITS),
};

static __always_inline
void stats_hash_type(u32 hash_type)
{
u64 *value;
u32 L3, L4;

if (hash_type > XDP_HASH_TYPE_MASK)
return;

L3 = XDP_HASH_TYPE_L3(hash_type);
value = bpf_map_lookup_elem(&stats_htype_L3, &L3);
if (value)
*value += 1;

/* The L4 value is shifted down to fit within array size */
L4 = XDP_HASH_TYPE_L4(hash_type) >> XDP_HASH_TYPE_L4_SHIFT;
value = bpf_map_lookup_elem(&stats_htype_L4, &L4);
if (value)
*value += 1;
}


SEC("xdp_rxhash")
int xdp_rxhash_prog(struct xdp_md *ctx)
{
Expand All @@ -87,37 +130,44 @@ int xdp_rxhash_prog(struct xdp_md *ctx)
u32 *a2;
struct pattern *pattern;
u32 key = 0;
u64 *touch_mem;
u64 rxhash, h;
u64 *touch_mem, h = 0;
u32 hash, hash_type;
u32 L3, L4;
u32 rxhash, rxhash_type;

/* Validate packet length is minimum Eth header size */
if (eth + 1 > data_end)
return XDP_DROP;

/* Direct reading of xdp_md->rxhash */
rxhash = ctx->rxhash;
/* Separate (direct) reading xdp_md->rxhash_type */
rxhash_type = ctx->rxhash_type;

/* Call helper bpf_xdp_rxhash, 64-bit return value,
* with hash_type in in upper bits.
/* Helper bpf_xdp_rxhash, return 64-bit value with hash_type
* in in upper bits.
*/
h = bpf_xdp_rxhash(ctx, 0, 0, BPF_F_RXHASH_GET);
hash = XDP_HASH(h);
hash_type = XDP_HASH_TYPE(h);
stats_hash_type(hash_type);

//L3 = hash_type & XDP_HASH_TYPE_L3_MASK;
//L4 = hash_type & XDP_HASH_TYPE_L4_MASK;

// or
L3 = XDP_HASH_TYPE_L3(hash_type);
L4 = XDP_HASH_TYPE_L4(hash_type);

bpf_debug("xdp_rxhash: hash1=%llu h:%llu hash:%u\n",
rxhash, h, hash);

bpf_debug("helper: type:%u L3:%u L4:%u\n",
hash_type, L3, L4);
bpf_debug("xdp_rxhash: hash1=%llu h:%llu hash:%u\n", rxhash, h, hash);
bpf_debug("helper: type:%u L3:%u L4:%u\n", hash_type, L3, L4);
bpf_debug("experiment: type:%u rxhash_type_direct:%u\n",
hash_type, rxhash_type);

/* Drop all IPv4 UDP packets without even reading packet data */
if (hash_type == (XDP_HASH_TYPE_L4_UDP + XDP_HASH_TYPE_L3_IPV4)) {
action = XDP_ABORTED; /* Notice in --stats output */
//goto out;
}

touch_mem = bpf_map_lookup_elem(&touch_memory, &key);
if (touch_mem && (*touch_mem == 1)) {
Expand Down

0 comments on commit 9647e1b

Please sign in to comment.