bpf_jitter.h revision 199492
1126475Sgrehan/*-
2126475Sgrehan * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3126475Sgrehan * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
4126475Sgrehan * All rights reserved.
5126475Sgrehan *
6126475Sgrehan * Redistribution and use in source and binary forms, with or without
7126475Sgrehan * modification, are permitted provided that the following conditions
8126475Sgrehan * are met:
9126475Sgrehan *
10126475Sgrehan * 1. Redistributions of source code must retain the above copyright
11126475Sgrehan * notice, this list of conditions and the following disclaimer.
12126475Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
13126475Sgrehan * notice, this list of conditions and the following disclaimer in the
14126475Sgrehan * documentation and/or other materials provided with the distribution.
15126475Sgrehan * 3. Neither the name of the Politecnico di Torino nor the names of its
16126475Sgrehan * contributors may be used to endorse or promote products derived from
17126475Sgrehan * this software without specific prior written permission.
18126475Sgrehan *
19126475Sgrehan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20126475Sgrehan * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21126475Sgrehan * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22126475Sgrehan * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23126475Sgrehan * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24126475Sgrehan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25126475Sgrehan * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26126475Sgrehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27126475Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28126475Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29126475Sgrehan * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30126475Sgrehan *
31126475Sgrehan * $FreeBSD: head/sys/net/bpf_jitter.h 199492 2009-11-18 19:26:17Z jkim $
32126475Sgrehan */
33126475Sgrehan
34126475Sgrehan#ifndef _NET_BPF_JITTER_H_
35126475Sgrehan#define _NET_BPF_JITTER_H_
36126475Sgrehan
37126475Sgrehan#ifdef _KERNEL
38126475SgrehanMALLOC_DECLARE(M_BPFJIT);
39126475Sgrehan#else
40126475Sgrehan#define	BPF_JIT_MAXSIZE		PAGE_SIZE
41126475Sgrehan#endif
42126475Sgrehan
43126475Sgrehanextern int bpf_jitter_enable;
44126475Sgrehan
45126475Sgrehan/*
46126475Sgrehan * Prototype of a filtering function created by the jitter.
47126475Sgrehan *
48126475Sgrehan * The syntax and the meaning of the parameters is analogous to the one of
49126475Sgrehan * bpf_filter(). Notice that the filter is not among the parameters because
50126475Sgrehan * it is hardwired in the function.
51126475Sgrehan */
52126475Sgrehantypedef u_int (*bpf_filter_func)(u_char *, u_int, u_int);
53126475Sgrehan
54126475Sgrehan/* Structure describing a native filtering program created by the jitter. */
55126475Sgrehantypedef struct bpf_jit_filter {
56126475Sgrehan	/* The native filtering binary, in the form of a bpf_filter_func. */
57126475Sgrehan	bpf_filter_func	func;
58126475Sgrehan
59126475Sgrehan	int		mem[BPF_MEMWORDS];	/* Scratch memory */
60126475Sgrehan} bpf_jit_filter;
61126475Sgrehan
62126475Sgrehan/*
63126475Sgrehan * BPF jitter, builds a machine function from a BPF program.
64126475Sgrehan *
65126475Sgrehan * param fp	The BPF pseudo-assembly filter that will be translated
66126475Sgrehan *		into native code.
67126475Sgrehan * param nins	Number of instructions of the input filter.
68126475Sgrehan * return	The bpf_jit_filter structure containing the native filtering
69126475Sgrehan *		binary.
70126475Sgrehan *
71126475Sgrehan * bpf_jitter allocates the buffers for the new native filter and
72126475Sgrehan * then translates the program pointed by fp calling bpf_jit_compile().
73126475Sgrehan */
74126475Sgrehanbpf_jit_filter	*bpf_jitter(struct bpf_insn *fp, int nins);
75126475Sgrehan
76126475Sgrehan/*
77126475Sgrehan * Deletes a filtering function that was previously created by bpf_jitter().
78126475Sgrehan *
79126475Sgrehan * param filter	The filter to destroy.
80126475Sgrehan *
81126475Sgrehan * This function frees the variuos buffers (code, memory, etc.) associated
82126475Sgrehan * with a filtering function.
83126475Sgrehan */
84126475Sgrehanvoid		bpf_destroy_jit_filter(bpf_jit_filter *filter);
85126475Sgrehan
86126475Sgrehan#endif	/* _NET_BPF_JITTER_H_ */
87126475Sgrehan