Skip to content

Commit

Permalink
Adds the draft of the XDP scheduler testing tool
Browse files Browse the repository at this point in the history
This commit contains the XDP scheduling framework. It consists of a
testing program called xdq-tester used to test schedulers using the XDP
and DEQUEUE hooks. It uses trace files written in Lua that the
xdq-tester program uses to check the XDP schedulers for correctness.

The FIFO, SPRIO, and WFQ are fully functional in this commit. The SPRIO
and WFQ have an API to set the weights from the Lua scripts.

This commit's FQ-CoDel contains sparse flow handling and testing, but
not the CoDel part.

Signed-off-by: Frey Alfredsson <[email protected]>
  • Loading branch information
freysteinn committed Oct 14, 2022
1 parent daefd11 commit f31e548
Show file tree
Hide file tree
Showing 16 changed files with 2,637 additions and 0 deletions.
10 changes: 10 additions & 0 deletions xdq-tester/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)

USER_TARGETS := xdq-tester
BPF_TARGETS := $(patsubst %.c,%,$(wildcard *.bpf.c))

USER_LIBS = -llua -ldl -lm

LIB_DIR = ../lib

include $(LIB_DIR)/common.mk
115 changes: 115 additions & 0 deletions xdq-tester/bpf_local_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

#ifndef BPF_LOCAL_HELPERS_H_
#define BPF_LOCAL_HELPERS_H_

#include "bpf_shared_data.h"

#define EEXIST 17 /* File exists */

#define BPF_MAP_TYPE_PIFO_GENERIC 31
#define BPF_MAP_TYPE_PIFO_XDP 32

/*
* bpf_packet_dequeue
*
* Dequeue the packet at the head of the PIFO in *map* and return a pointer
* to the packet (or NULL if the PIFO is empty).
*
* Returns
* On success, a pointer to the packet, or NULL if the PIFO is empty. The
* packet pointer must be freed using *bpf_packet_drop()* or returning
* the packet pointer. The *rank* pointer will be set to the rank of
* the dequeued packet on success, or a negative error code on error.
*/
static long (*bpf_packet_dequeue)(void *ctx, void *map, __u64 flags, __u64 *rank) = (void *) 208;;
static long (*bpf_packet_drop)(void *ctx, void *pkt) = (void *) 209;

struct parsing_context {
void *data; // Start of eth hdr
void *data_end; // End of safe acessible area
struct hdr_cursor nh; // Position to parse next
__u32 pkt_len; // Full packet length (headers+data)
};

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static __always_inline void *
bpf_map_lookup_or_try_init(void *map, const void *key, const void *init)
{
void *val;
long err;

val = bpf_map_lookup_elem(map, key);
if (val)
return val;

err = bpf_map_update_elem(map, key, init, BPF_NOEXIST);
if (err && err != -EEXIST)
return NULL;

return bpf_map_lookup_elem(map, key);
}

static __always_inline int bpf_max(__u64 left, __u64 right)
{
return right > left ? right : left;
}


/*
* Maps an IPv4 address into an IPv6 address according to RFC 4291 sec 2.5.5.2
*/
static void map_ipv4_to_ipv6(struct in6_addr *ipv6, __be32 ipv4)
{
__builtin_memset(&ipv6->in6_u.u6_addr8[0], 0x00, 10);
__builtin_memset(&ipv6->in6_u.u6_addr8[10], 0xff, 2);
ipv6->in6_u.u6_addr32[3] = ipv4;
}

/*
* Five-tuple helpers
*/

/* This function currently only supports UDP packets */
static __always_inline int parse_packet(struct parsing_context *pctx, struct packet_info *p_info)
{
/* Parse Ethernet and IP/IPv6 headers */
p_info->eth_type = parse_ethhdr(&pctx->nh, pctx->data_end, &p_info->eth);
if (p_info->eth_type == bpf_htons(ETH_P_IP)) {
p_info->ip_type = parse_iphdr(&pctx->nh, pctx->data_end, &p_info->iph);
if (p_info->ip_type < 0)
goto err;
p_info->nt.ipv = 4;
map_ipv4_to_ipv6(&p_info->nt.saddr.ip, p_info->iph->saddr);
map_ipv4_to_ipv6(&p_info->nt.daddr.ip, p_info->iph->daddr);
} else if (p_info->eth_type == bpf_htons(ETH_P_IPV6)) {
p_info->ip_type = parse_ip6hdr(&pctx->nh, pctx->data_end, &p_info->ip6h);
if (p_info->ip_type < 0)
goto err;
p_info->nt.ipv = 6;
p_info->nt.saddr.ip = p_info->ip6h->saddr;
p_info->nt.daddr.ip = p_info->ip6h->daddr;
} else {
goto err;
}

/* Parse UDP header */
if (p_info->ip_type != IPPROTO_UDP)
goto err;
if (parse_udphdr(&pctx->nh, pctx->data_end, &p_info->udph) < 0)
goto err;

p_info->nt.proto = IPPROTO_UDP;
p_info->nt.saddr.port = p_info->udph->source;
p_info->nt.daddr.port = p_info->udph->dest;

return 0;
err:
bpf_printk("Failed to parse UDP packet");
return -1;
}

#pragma GCC diagnostic pop

#endif // BPF_LOCAL_HELPERS_H_
49 changes: 49 additions & 0 deletions xdq-tester/bpf_shared_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef BPF_SHARED_DATA_H_
#define BPF_SHARED_DATA_H_

struct flow_address {
struct in6_addr ip;
__u16 port;
__u16 reserved;
};

struct network_tuple {
struct flow_address saddr;
struct flow_address daddr;
__u16 proto;
__u8 ipv;
__u8 reserved;
};

struct flow_state {
__u32 pkts;
__u32 root_finish_bytes;
__u32 finish_bytes;
__u16 root_weight;
__u16 weight;
__u32 persistent;
__u64 root_priority;
};

struct fq_codel_flow_state {
__u32 pkts;
__u32 finish_bytes;
__u32 total_bytes;
__u32 grace_period;
};

struct packet_info {
struct ethhdr *eth;
union {
struct iphdr *iph;
struct ipv6hdr *ip6h;
};
union {
struct udphdr *udph;
};
struct network_tuple nt;
int eth_type;
int ip_type;
};

#endif // BPF_SHARED_DATA_H_
25 changes: 25 additions & 0 deletions xdq-tester/fifo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- SPDX-License-Identifier: GPL-2.0
-- Copyright (c) 2022 Freysteinn Alfredsson <[email protected]>

-- FIFO scheduler
config.bpf.file = "./sched_fifo.bpf.o"

-- Setup flows
packet_flow1 = Udp:new()
packet_flow1.udp.dest = 8080

packet_flow2 = Udp:new()
packet_flow2.udp.dest = 8081

packet_flow3 = Udp:new()
packet_flow3.udp.dest = 8082


-- Test scheduler
enqueue(packet_flow1)
enqueue(packet_flow2)
enqueue(packet_flow3)

dequeue_cmp(packet_flow1)
dequeue_cmp(packet_flow2)
dequeue_cmp(packet_flow3)
170 changes: 170 additions & 0 deletions xdq-tester/fq_codel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
-- SPDX-License-Identifier: GPL-2.0
-- Copyright (c) 2022 Freysteinn Alfredsson <[email protected]>

-- Fair Queuing with Controlled Delay (FQ_CoDel)
config.bpf.file = "./sched_fq_codel.bpf.o"


-- Setup flows
-- We use this flow to test sparse flow handling
packet_sparse_flow_tester = Udp:new()
packet_sparse_flow_tester.udp.dest = 8000

-- The background stream flow increments the time bytes
-- so that we can test our sparse flow tester when time has passed
packet_flow_background_stream = Udp:new()
packet_flow_background_stream.udp.dest = 8001
-- Make the packet the size of full a quantom (1522 - 62)
packet_flow_background_stream.udp.payload = create_payload(1460)


-- Test scheduler

--
-- 1. Sparse flow tests
--
-- In our implementation of FQ-CoDel, the time_bytes variable is the only thing
-- that connects sparse flows. Therefore, we can test all possible scenarios
-- using only two flows. One background flow that we only use to advance time.
-- And the flow that we use for testing.

function make_sparse(flow)
-- The background flow needs two packets to be a stream:
-- * The first packet will be sparse.
-- * The second packet exceeds the sparse quantom.
flow.udp.payload = create_payload(1460)
enqueue(flow) -- Sparse
enqueue(flow) -- Stream
dequeue_cmp(flow) -- Dequeue sparse
dequeue_cmp(flow) -- Dequeue sparse
-- Note that the type_bytes has not advanced at this point but will after the
-- next dequeued packet.
end

-- 1.1 Test when a sparse flow ends while sparse
function fq_codel_sparse_test1()
-- This test does the following:
-- 1. Creates a sparse flow with a couple of packets.
-- 2. Advance time_bytes and expire the sparse flow.
-- 3. Creates a new sparse flow with a couple of packets.
-- 4. Advance time_bytes and expire the new sparse flow.
-- In steps two and four the test confirms that the sparse flows
-- were still sparse.
make_sparse(packet_flow_background_stream)

-- Prime the background stream so it can update the time_bytes variable later.
enqueue(packet_flow_background_stream) -- Prime for updating time_bytes
enqueue(packet_flow_background_stream) -- Make sure the flow is not recycled after update

-- Make the packet the size of half a quantom (1522/2 - 62)
-- The flow will cease being a sparse flow after two packets.
packet_sparse_flow_tester.udp.payload = create_payload(699)

-- The sparse flow gets a full quantom of packets.
enqueue(packet_sparse_flow_tester) -- Sparse 1
enqueue(packet_sparse_flow_tester) -- Sparse 2

-- Remove all sparse packets.
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue sparse
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue sparse

-- Advance time_bytes
dequeue_cmp(packet_flow_background_stream) -- Advances time_bytes one quantom
-- Our FQ-CoDel algorithm should have expired the sparse_flow_tester
-- flow at this point, but not the background stream.

-- Test that the sparse_flow_tester is indeed expired.
enqueue(packet_sparse_flow_tester) -- Add sparse packet with a higher priority
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue the sparse packet
dequeue_cmp(packet_flow_background_stream) -- Advances time_bytes one quantom
-- Our FQ-CoDel algorithm should have expired both the sparse_flow_tester
-- flow and the background stream at this point.
end

-- 1.2 Test a sparse flow when the time_bytes advances while the flow is sparse
function fq_codel_sparse_test2()
-- This test does the following:
-- 1. Creates a sparse flow with a couple of packets.
-- 2. Advances time_bytes by a half a quantom
-- 3. Adds a couple of packets to the sparse flow.
-- In steps one and three the test confirms that the sparse flow
-- is still sparse.
make_sparse(packet_flow_background_stream)

-- Make the packet the size of half a quantom (1522/2 - 62)
packet_flow_background_stream.udp.payload = create_payload(699)

-- Make each packet 50 bytes for our sparse flow
packet_sparse_flow_tester.udp.payload = create_payload(38)

-- Keep in mind that the last background packet ends at a full quantom. Therefore,
-- if we want to update the time_bytes by a half a quantom, we will need to enqueue
-- and deqeueu a half a quantom packet.
enqueue(packet_flow_background_stream) -- Used to advance time_bytes by half a quantom
enqueue(packet_flow_background_stream) -- Used to advance time_bytes by half a quantom
enqueue(packet_flow_background_stream) -- Make sure the flow is not recycled after update
dequeue_cmp(packet_flow_background_stream) -- Advances time_bytes by a half a quantom

-- Confirm that the sparse flow has a higher priority than the background stream.
enqueue(packet_sparse_flow_tester) -- Add a sparse packet
enqueue(packet_sparse_flow_tester) -- Add a sparse packet
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue the sparse packet
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue the sparse packet

dequeue_cmp(packet_flow_background_stream) -- Advances time_bytes by a half a quantom

-- Confirm that the sparse flow has a higher priority than the stream.
enqueue(packet_sparse_flow_tester) -- Add a sparse packet
enqueue(packet_sparse_flow_tester) -- Add a sparse packet
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue the sparse packet
dequeue_cmp(packet_sparse_flow_tester) -- Dequeue the sparse packet

-- Recycle both flows.
dequeue_cmp(packet_flow_background_stream) -- Recycle both flows
end

-- 1.3 Test a flow that becomes a stream.
function fq_codel_sparse_test3()
-- This test does the following:
-- 1. Creates a sparse flow and adds a full quantom to it.
-- 2. Adds packets to the flow to make it a stream.
-- 3. Advances time_bytes by a half a quantom.
-- 4. Adds packets to the stream
-- In steps two and four the test confirms that the flow is a stream.
make_sparse(packet_flow_background_stream)

-- Make the packet the size of half a quantom (1522/2 - 62)
packet_sparse_flow_tester.udp.payload = create_payload(699)

-- Make the packet the size of half a quantom (1522/2 - 62)
packet_flow_background_stream.udp.payload = create_payload(699)

-- Keep in mind that the last background packet ends at a full quantom. Therefore,
-- if we want to update the time_bytes by a half a quantom, we will need to enqueue
-- and deqeueu a half a quantom packet.
enqueue(packet_flow_background_stream) -- Used to advance time_bytes by half a quantom
enqueue(packet_flow_background_stream) -- Used to advance time_bytes by half a quantom
enqueue(packet_flow_background_stream) -- Make sure the flow is not recycled after update
dequeue_cmp(packet_flow_background_stream) -- Advances time_bytes by a half a quantom

-- Make the sparse_flow_tester flow a stream.
enqueue(packet_sparse_flow_tester) -- Add sparse packet
enqueue(packet_sparse_flow_tester) -- Add sparse packet
enqueue(packet_sparse_flow_tester) -- Make the flow a stream
enqueue(packet_sparse_flow_tester) -- Add stream packet

-- Dequeue the sparse flow packets.
dequeue_cmp(packet_sparse_flow_tester)
dequeue_cmp(packet_sparse_flow_tester)

-- Confirm that both flows are streams with equal priority.
dequeue_cmp(packet_sparse_flow_tester)
dequeue_cmp(packet_flow_background_stream)
dequeue_cmp(packet_sparse_flow_tester)
dequeue_cmp(packet_flow_background_stream)
end

-- Run tests
fq_codel_sparse_test1()
fq_codel_sparse_test2()
fq_codel_sparse_test3()
Loading

0 comments on commit f31e548

Please sign in to comment.