summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/net/bpf/h_bpf.h
blob: 307bc50d56e7dcd96f8b1bdb6e1f6c7b2a7a554b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*	$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_ */
OpenPOWER on IntegriCloud