#ifdef _KERNEL
#include <sys/types.h>
#include <sys/endian.h>
#endif
#define NPF_BPFCOP
#include "npf_impl.h"
#include "npf_test.h"
static bool lverbose = false;
static int
test_bpf_code(void *code, size_t size)
{
uint32_t memstore[BPF_MEMWORDS];
bpf_args_t bc_args;
npf_cache_t *npc;
struct mbuf *m;
int ret, jret;
void *jcode;
m = mbuf_get_pkt(AF_INET, IPPROTO_TCP,
"192.168.2.100", "10.0.0.1", 15000, 80);
npc = get_cached_pkt(m, NULL, NPF_RULE_LAYER_3);
#ifdef _NPF_STANDALONE
bc_args.pkt = (const uint8_t *)nbuf_dataptr(npc->npc_nbuf);
#else
bc_args.pkt = (const uint8_t *)m;
#endif
bc_args.buflen = m_length(m);
bc_args.wirelen = bc_args.buflen;
bc_args.mem = memstore;
bc_args.arg = npc;
ret = npf_bpf_filter(&bc_args, code, NULL);
jcode = npf_bpf_compile(code, size);
if (jcode) {
jret = npf_bpf_filter(&bc_args, NULL, jcode);
assert(ret == jret); (void)jret;
bpf_jit_freecode(jcode);
} else if (lverbose) {
printf("JIT-compilation failed\n");
}
put_cached_pkt(npc);
return ret;
}
static uint32_t
npf_bpfcop_run(unsigned reg)
{
struct bpf_insn insns_npf_bpfcop[] = {
BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
BPF_STMT(BPF_LD+BPF_W+BPF_MEM, reg),
BPF_STMT(BPF_RET+BPF_A, 0),
};
return test_bpf_code(&insns_npf_bpfcop, sizeof(insns_npf_bpfcop));
}
static bool
npf_bpfcop_test(void)
{
struct bpf_insn insns_ipver[] = {
BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
BPF_STMT(BPF_RET+BPF_A, 0),
};
CHECK_TRUE(test_bpf_code(&insns_ipver, sizeof(insns_ipver)) == IPVERSION);
CHECK_TRUE(npf_bpfcop_run(BPF_MW_IPVER) == IPVERSION);
CHECK_TRUE(npf_bpfcop_run(BPF_MW_L4OFF) == sizeof(struct ip));
CHECK_TRUE(npf_bpfcop_run(BPF_MW_L4PROTO) == IPPROTO_TCP);
return true;
}
bool
npf_bpf_test(bool verbose)
{
lverbose = verbose;
return npf_bpfcop_test();
}