General style cleanup, no functional change.

This commit is contained in:
jkim 2009-11-20 21:12:40 +00:00
parent e66ae1c3f9
commit 2d93b6e424
3 changed files with 72 additions and 99 deletions

View File

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <net/if.h> #include <net/if.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/param.h> #include <sys/param.h>
#endif #endif
@ -104,38 +105,38 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
u_int i, pass; u_int i, pass;
/* /*
* NOTE: do not modify the name of this variable, as it's used by * NOTE: Do not modify the name of this variable, as it's used by
* the macros to emit code. * the macros to emit code.
*/ */
emit_func emitm; emit_func emitm;
/* Allocate the reference table for the jumps */ /* Allocate the reference table for the jumps. */
#ifdef _KERNEL #ifdef _KERNEL
stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT); stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
M_NOWAIT | M_ZERO);
#else #else
stream.refs = malloc((nins + 1) * sizeof(u_int)); stream.refs = malloc((nins + 1) * sizeof(u_int));
#endif #endif
if (stream.refs == NULL) if (stream.refs == NULL)
return (NULL); return (NULL);
#ifndef _KERNEL
/* Reset the reference table */ memset(stream.refs, 0, (nins + 1) * sizeof(u_int));
for (i = 0; i < nins + 1; i++) #endif
stream.refs[i] = 0;
stream.cur_ip = 0; stream.cur_ip = 0;
stream.bpf_pc = 0; stream.bpf_pc = 0;
stream.ibuf = NULL;
/* /*
* the first pass will emit the lengths of the instructions * The first pass will emit the lengths of the instructions
* to create the reference table * to create the reference table.
*/ */
emitm = emit_length; emitm = emit_length;
pass = 0; for (pass = 0; pass < 2; pass++) {
for (;;) {
ins = prog; ins = prog;
/* create the procedure header */ /* Create the procedure header. */
PUSH(RBP); PUSH(RBP);
MOVrq(RSP, RBP); MOVrq(RSP, RBP);
SUBib(BPF_MEMWORDS * sizeof(uint32_t), RSP); SUBib(BPF_MEMWORDS * sizeof(uint32_t), RSP);
@ -470,25 +471,16 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
ins++; ins++;
} }
pass++; if (pass > 0)
if (pass >= 2) { continue;
#ifndef _KERNEL
if (mprotect(stream.ibuf, stream.cur_ip,
PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, stream.cur_ip);
stream.ibuf = NULL;
}
#endif
*size = stream.cur_ip;
break;
}
*size = stream.cur_ip;
#ifdef _KERNEL #ifdef _KERNEL
stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL) if (stream.ibuf == NULL)
break; break;
#else #else
stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE, stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
MAP_ANON, -1, 0); MAP_ANON, -1, 0);
if (stream.ibuf == MAP_FAILED) { if (stream.ibuf == MAP_FAILED) {
stream.ibuf = NULL; stream.ibuf = NULL;
@ -497,28 +489,33 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
#endif #endif
/* /*
* modify the reference table to contain the offsets and * Modify the reference table to contain the offsets and
* not the lengths of the instructions * not the lengths of the instructions.
*/ */
for (i = 1; i < nins + 1; i++) for (i = 1; i < nins + 1; i++)
stream.refs[i] += stream.refs[i - 1]; stream.refs[i] += stream.refs[i - 1];
/* Reset the counters */ /* Reset the counters. */
stream.cur_ip = 0; stream.cur_ip = 0;
stream.bpf_pc = 0; stream.bpf_pc = 0;
/* the second pass creates the actual code */ /* The second pass creates the actual code. */
emitm = emit_code; emitm = emit_code;
} }
/* /*
* the reference table is needed only during compilation, * The reference table is needed only during compilation,
* now we can free it * now we can free it.
*/ */
#ifdef _KERNEL #ifdef _KERNEL
free(stream.refs, M_BPFJIT); free(stream.refs, M_BPFJIT);
#else #else
free(stream.refs); free(stream.refs);
if (stream.ibuf != NULL &&
mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, *size);
stream.ibuf = NULL;
}
#endif #endif
return ((bpf_filter_func)stream.ibuf); return ((bpf_filter_func)stream.ibuf);

View File

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <net/if.h> #include <net/if.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/param.h> #include <sys/param.h>
#endif #endif
@ -104,38 +105,38 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
u_int i, pass; u_int i, pass;
/* /*
* NOTE: do not modify the name of this variable, as it's used by * NOTE: Do not modify the name of this variable, as it's used by
* the macros to emit code. * the macros to emit code.
*/ */
emit_func emitm; emit_func emitm;
/* Allocate the reference table for the jumps */ /* Allocate the reference table for the jumps. */
#ifdef _KERNEL #ifdef _KERNEL
stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT); stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
M_NOWAIT | M_ZERO);
#else #else
stream.refs = malloc((nins + 1) * sizeof(u_int)); stream.refs = malloc((nins + 1) * sizeof(u_int));
#endif #endif
if (stream.refs == NULL) if (stream.refs == NULL)
return (NULL); return (NULL);
#ifndef _KERNEL
/* Reset the reference table */ memset(stream.refs, 0, (nins + 1) * sizeof(u_int));
for (i = 0; i < nins + 1; i++) #endif
stream.refs[i] = 0;
stream.cur_ip = 0; stream.cur_ip = 0;
stream.bpf_pc = 0; stream.bpf_pc = 0;
stream.ibuf = NULL;
/* /*
* the first pass will emit the lengths of the instructions * The first pass will emit the lengths of the instructions
* to create the reference table * to create the reference table.
*/ */
emitm = emit_length; emitm = emit_length;
pass = 0; for (pass = 0; pass < 2; pass++) {
for (;;) {
ins = prog; ins = prog;
/* create the procedure header */ /* Create the procedure header. */
PUSH(EBP); PUSH(EBP);
MOVrd(ESP, EBP); MOVrd(ESP, EBP);
SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP); SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP);
@ -503,25 +504,16 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
ins++; ins++;
} }
pass++; if (pass > 0)
if (pass >= 2) { continue;
#ifndef _KERNEL
if (mprotect(stream.ibuf, stream.cur_ip,
PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, stream.cur_ip);
stream.ibuf = NULL;
}
#endif
*size = stream.cur_ip;
break;
}
*size = stream.cur_ip;
#ifdef _KERNEL #ifdef _KERNEL
stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL) if (stream.ibuf == NULL)
break; break;
#else #else
stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE, stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
MAP_ANON, -1, 0); MAP_ANON, -1, 0);
if (stream.ibuf == MAP_FAILED) { if (stream.ibuf == MAP_FAILED) {
stream.ibuf = NULL; stream.ibuf = NULL;
@ -530,28 +522,33 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
#endif #endif
/* /*
* modify the reference table to contain the offsets and * Modify the reference table to contain the offsets and
* not the lengths of the instructions * not the lengths of the instructions.
*/ */
for (i = 1; i < nins + 1; i++) for (i = 1; i < nins + 1; i++)
stream.refs[i] += stream.refs[i - 1]; stream.refs[i] += stream.refs[i - 1];
/* Reset the counters */ /* Reset the counters. */
stream.cur_ip = 0; stream.cur_ip = 0;
stream.bpf_pc = 0; stream.bpf_pc = 0;
/* the second pass creates the actual code */ /* The second pass creates the actual code. */
emitm = emit_code; emitm = emit_code;
} }
/* /*
* the reference table is needed only during compilation, * The reference table is needed only during compilation,
* now we can free it * now we can free it.
*/ */
#ifdef _KERNEL #ifdef _KERNEL
free(stream.refs, M_BPFJIT); free(stream.refs, M_BPFJIT);
#else #else
free(stream.refs); free(stream.refs);
if (stream.ibuf != NULL &&
mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, *size);
stream.ibuf = NULL;
}
#endif #endif
return ((bpf_filter_func)stream.ibuf); return ((bpf_filter_func)stream.ibuf);

View File

@ -42,7 +42,6 @@ __FBSDID("$FreeBSD$");
#include <sys/sysctl.h> #include <sys/sysctl.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/types.h> #include <sys/types.h>
@ -62,27 +61,36 @@ SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW, 0, "BPF JIT compiler");
int bpf_jitter_enable = 1; int bpf_jitter_enable = 1;
SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW, SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,
&bpf_jitter_enable, 0, "enable BPF JIT compiler"); &bpf_jitter_enable, 0, "enable BPF JIT compiler");
#endif
bpf_jit_filter * bpf_jit_filter *
bpf_jitter(struct bpf_insn *fp, int nins) bpf_jitter(struct bpf_insn *fp, int nins)
{ {
bpf_jit_filter *filter; bpf_jit_filter *filter;
/* Allocate the filter structure */ /* Allocate the filter structure. */
#ifdef _KERNEL
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter), filter = (struct bpf_jit_filter *)malloc(sizeof(*filter),
M_BPFJIT, M_NOWAIT); M_BPFJIT, M_NOWAIT);
#else
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));
#endif
if (filter == NULL) if (filter == NULL)
return (NULL); return (NULL);
/* No filter means accept all */ /* No filter means accept all. */
if (fp == NULL || nins == 0) { if (fp == NULL || nins == 0) {
filter->func = bpf_jit_accept_all; filter->func = bpf_jit_accept_all;
return (filter); return (filter);
} }
/* Create the binary */ /* Create the binary. */
if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) { if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {
#ifdef _KERNEL
free(filter, M_BPFJIT); free(filter, M_BPFJIT);
#else
free(filter);
#endif
return (NULL); return (NULL);
} }
@ -93,45 +101,16 @@ void
bpf_destroy_jit_filter(bpf_jit_filter *filter) bpf_destroy_jit_filter(bpf_jit_filter *filter)
{ {
#ifdef _KERNEL
if (filter->func != bpf_jit_accept_all) if (filter->func != bpf_jit_accept_all)
free(filter->func, M_BPFJIT); free(filter->func, M_BPFJIT);
free(filter, M_BPFJIT); free(filter, M_BPFJIT);
}
#else #else
bpf_jit_filter *
bpf_jitter(struct bpf_insn *fp, int nins)
{
bpf_jit_filter *filter;
/* Allocate the filter structure */
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));
if (filter == NULL)
return (NULL);
/* No filter means accept all */
if (fp == NULL || nins == 0) {
filter->func = bpf_jit_accept_all;
return (filter);
}
/* Create the binary */
if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {
free(filter);
return (NULL);
}
return (filter);
}
void
bpf_destroy_jit_filter(bpf_jit_filter *filter)
{
if (filter->func != bpf_jit_accept_all) if (filter->func != bpf_jit_accept_all)
munmap(filter->func, filter->size); munmap(filter->func, filter->size);
free(filter); free(filter);
}
#endif #endif
}
static u_int static u_int
bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen, bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen,