Skip to content

Commit

Permalink
bpf,test: add tests for vxlan helper functions
Browse files Browse the repository at this point in the history
Add unit tests for new vxlan helper functions in tunnel.h

Signed-off-by: Louis DeLosSantos <louis.delos@isovalent.com>
  • Loading branch information
ldelossa committed Mar 1, 2024
1 parent e096734 commit f2f6671
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions bpf/tests/tunnel_vxlan_helpers_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright Authors of Cilium */

#include "common.h"
#include "bpf/ctx/skb.h"
#include "pktgen.h"

#define TUNNEL_PROTOCOL TUNNEL_PROTOCOL_VXLAN
#define TUNNEL_PORT 8472
#define TUNNEL_PORT_BAD 0
#define VXLAN_VNI 0xDEADBE

#include "node_config.h"
#include "lib/common.h"

static __always_inline int
mk_packet(struct __ctx_buff *ctx) {
struct pktgen builder;
struct udphdr *l4;
struct vxlanhdr *vx;
void *data;

pktgen__init(&builder, ctx);

l4 = pktgen__push_ipv4_udp_packet(&builder,
(__u8 *)mac_one,
(__u8 *)mac_two,
v4_node_one,
v4_node_two,
666,
bpf_htons(TUNNEL_PORT));
if (!l4)
return TEST_ERROR;

vx = pktgen__push_default_vxlanhdr(&builder);
if (!vx)
return TEST_ERROR;

vx->vx_vni = bpf_htonl(VXLAN_VNI << 8);

/* we won't sniff into the encap'd packet, so just use the default */
data = pktgen__push_data(&builder, default_data, sizeof(default_data));
if (!data)
return TEST_ERROR;

pktgen__finish(&builder);

return 0;
}

PKTGEN("tc", "tunnel_skb_is_vxlan_v4_success")
static __always_inline int
pktgen_vxlan_mock_check1(struct __ctx_buff *ctx) {
return mk_packet(ctx);
}

CHECK("tc", "tunnel_skb_is_vxlan_v4_success")
int check1(struct __ctx_buff *ctx)
{
test_init();

void *data, *data_end = NULL;
struct iphdr *ipv4 = NULL;

assert(revalidate_data(ctx, &data, &data_end, &ipv4));
assert(tunnel_skb_is_vxlan_v4(data, data_end, ipv4, TUNNEL_PORT));

test_finish();
}

PKTGEN("tc", "tunnel_skb_is_vxlan_v4_failure")
static __always_inline int
pktgen_vxlan_mock_check2(struct __ctx_buff *ctx) {
return mk_packet(ctx);
}

CHECK("tc", "tunnel_skb_is_vxlan_v4_failure")
int check2(struct __ctx_buff *ctx)
{
test_init();

void *data, *data_end = NULL;
struct iphdr *ipv4 = NULL;

assert(revalidate_data(ctx, &data, &data_end, &ipv4));
assert(!tunnel_skb_is_vxlan_v4(data, data_end, ipv4, TUNNEL_PORT_BAD));

test_finish();
}

PKTGEN("tc", "tunnel_vxlan_get_vni_success")
static __always_inline int
pktgen_vxlan_mock_check3(struct __ctx_buff *ctx) {
return mk_packet(ctx);
}

CHECK("tc", "tunnel_vxlan_get_vni_success")
int check3(struct __ctx_buff *ctx)
{
test_init();

void *data, *data_end = NULL;
struct iphdr *ipv4 = NULL;

assert(revalidate_data(ctx, &data, &data_end, &ipv4));
assert(tunnel_vxlan_get_vni(data, data_end, ipv4) == VXLAN_VNI);

test_finish();
}

0 comments on commit f2f6671

Please sign in to comment.