[BACK]Return to h_bpf.h CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / tests / net / bpf

File: [cvs.NetBSD.org] / src / tests / net / bpf / h_bpf.h (download)

Revision 1.2, Tue Jul 8 21:44:26 2014 UTC (6 years, 9 months ago) by alnsn
Branch: MAIN
CVS Tags: tls-maxphys-base, tls-earlyentropy-base, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, is-mlppp-base, is-mlppp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, HEAD
Branch point for: tls-maxphys, tls-earlyentropy
Changes since 1.1: +34 -6 lines

Clone libbpfjit tests to check kernel implementation of bpfjit.
Old content of t_bpfjit.c will be moved to t_mbuf.c shortly.
Change packet buffers to unsigned char[] type.

/*	$NetBSD: h_bpf.h,v 1.2 2014/07/08 21:44:26 alnsn Exp $	*/

/*-
 * Copyright (c) 2014 Alexander Nasonov.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _TESTS_NET_BPF_H_BPF_H_
#define _TESTS_NET_BPF_H_BPF_H_

#include <sys/param.h>
#include <sys/mbuf.h>

#include <net/bpf.h>
#include <net/bpfjit.h>

#include <rump/rump.h>
#include <rump/rump_syscalls.h>

#include <stdint.h>
#include <string.h>

/* XXX These declarations don't look kosher. */
int rumpns_bpf_validate(const struct bpf_insn *, int);
unsigned int rumpns_bpf_filter_ext(const bpf_ctx_t *,
    const struct bpf_insn *, bpf_args_t *);
bpfjit_func_t rumpns_bpfjit_generate_code(const bpf_ctx_t *,
    const struct bpf_insn *, size_t);
void rumpns_bpfjit_free_code(bpfjit_func_t);

/*
 * Init mbuf chain with one or two chunks. The first chunk holds
 * [pkt, pkt + split] bytes, the second chunk (if it's not empty)
 * holds (pkt + split, pkt + pktsize) bytes.
 * The function returns (const uint8_t *)mb1.
 */
static inline const uint8_t *
init_mchain2(struct mbuf *mb1, struct mbuf *mb2,
    unsigned char pkt[], size_t pktsize, size_t split)
{

	(void)memset(mb1, 0, sizeof(*mb1));
	mb1->m_data = (char *)pkt;
	mb1->m_next = (split < pktsize) ? mb2 : NULL;
	mb1->m_len = (split < pktsize) ? split : pktsize;

	if (split < pktsize) {
		(void)memset(mb2, 0, sizeof(*mb2));
		mb2->m_next = NULL;
		mb2->m_data = (char *)&pkt[split];
		mb2->m_len = pktsize - split;
	}

	return (const uint8_t*)mb1;
}

/*
 * Compile and run a filter program.
 */
static inline unsigned int
exec_prog(struct bpf_insn *insns, size_t insn_count,
    unsigned char pkt[], size_t pktsize)
{
	bpfjit_func_t fn;
	bpf_args_t args;
	unsigned int res;

	args.pkt = (const uint8_t *)pkt;
	args.buflen = pktsize;
	args.wirelen = pktsize;

	rump_schedule();
	fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
	rump_unschedule();

	res = fn(NULL, &args);

	rump_schedule();
	rumpns_bpfjit_free_code(fn);
	rump_unschedule();

	return res;
}

/*
 * Interpret a filter program with mbuf chain passed to bpf_filter_ext().
 */
static inline unsigned int
interp_prog_mchain2(struct bpf_insn *insns,
    unsigned char pkt[], size_t pktsize, size_t split)
{
	uint32_t mem[BPF_MEMWORDS];
	struct mbuf mb1, mb2;
	bpf_args_t args;
	unsigned int res;

	args.pkt = init_mchain2(&mb1, &mb2, pkt, pktsize, split);
	args.buflen = 0;
	args.wirelen = pktsize;
	args.mem = mem;

	rump_schedule();
	res = rumpns_bpf_filter_ext(NULL, insns, &args);
	rump_unschedule();

	return res;
}

/*
 * Compile and run a filter program with mbuf chain passed to compiled function.
 */
static inline unsigned int
exec_prog_mchain2(struct bpf_insn *insns, size_t insn_count,
    unsigned char pkt[], size_t pktsize, size_t split)
{
	bpfjit_func_t fn;
	struct mbuf mb1, mb2;
	bpf_args_t args;
	unsigned int res;

	args.pkt = init_mchain2(&mb1, &mb2, pkt, pktsize, split);
	args.buflen = 0;
	args.wirelen = pktsize;

	rump_schedule();
	fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
	rump_unschedule();

	res = fn(NULL, &args);

	rump_schedule();
	rumpns_bpfjit_free_code(fn);
	rump_unschedule();

	return res;
}

static inline bool
prog_validate(struct bpf_insn *insns, size_t insn_count)
{
	bool res;

	rump_schedule();
	res = rumpns_bpf_validate(insns, insn_count);
	rump_unschedule();

	return res;
}

#endif /* _TESTS_NET_BPF_H_BPF_H_ */