- Make these files compilable on user land.

- Update copyrights and fix style(9).
This commit is contained in:
Jung-uk Kim 2008-08-18 18:59:33 +00:00
parent 651eea9aa8
commit 3bfea8682f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=181846
4 changed files with 112 additions and 19 deletions

View File

@ -32,16 +32,20 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#ifdef _KERNEL
#include "opt_bpf.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <net/if.h>
#else
#include <stdlib.h>
#endif
#include <sys/types.h>
#include <net/bpf.h>
#include <net/bpf_jitter.h>
@ -53,7 +57,7 @@ bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, int *);
* emit routine to update the jump table
*/
static void
emit_length(bpf_bin_stream *stream, u_int value, u_int len)
emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len)
{
(stream->refs)[stream->bpf_pc] += len;
@ -108,8 +112,12 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
return (NULL);
/* Allocate the reference table for the jumps */
#ifdef _KERNEL
stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int),
M_BPFJIT, M_NOWAIT);
#else
stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int));
#endif
if (stream.refs == NULL)
return (NULL);
@ -141,7 +149,11 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
switch (ins->code) {
default:
#ifdef _KERNEL
return (NULL);
#else
abort();
#endif
case BPF_RET|BPF_K:
MOVid(ins->k, EAX);
@ -450,11 +462,19 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
if (pass == 2)
break;
#ifdef _KERNEL
stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL) {
free(stream.refs, M_BPFJIT);
return (NULL);
}
#else
stream.ibuf = (char *)malloc(stream.cur_ip);
if (stream.ibuf == NULL) {
free(stream.refs);
return (NULL);
}
#endif
/*
* modify the reference table to contain the offsets and
@ -475,7 +495,11 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
* the reference table is needed only during compilation,
* now we can free it
*/
#ifdef _KERNEL
free(stream.refs, M_BPFJIT);
#else
free(stream.refs);
#endif
return ((bpf_filter_func)stream.ibuf);
}

View File

@ -32,16 +32,20 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#ifdef _KERNEL
#include "opt_bpf.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <net/if.h>
#else
#include <stdlib.h>
#endif
#include <sys/types.h>
#include <net/bpf.h>
#include <net/bpf_jitter.h>
@ -53,7 +57,7 @@ bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, int *);
* emit routine to update the jump table
*/
static void
emit_length(bpf_bin_stream *stream, u_int value, u_int len)
emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len)
{
(stream->refs)[stream->bpf_pc] += len;
@ -108,8 +112,12 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
return (NULL);
/* Allocate the reference table for the jumps */
#ifdef _KERNEL
stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int),
M_BPFJIT, M_NOWAIT);
#else
stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int));
#endif
if (stream.refs == NULL)
return (NULL);
@ -144,7 +152,11 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
switch (ins->code) {
default:
#ifdef _KERNEL
return (NULL);
#else
abort();
#endif
case BPF_RET|BPF_K:
MOVid(ins->k, EAX);
@ -473,11 +485,19 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
if (pass == 2)
break;
#ifdef _KERNEL
stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL) {
free(stream.refs, M_BPFJIT);
return (NULL);
}
#else
stream.ibuf = (char *)malloc(stream.cur_ip);
if (stream.ibuf == NULL) {
free(stream.refs);
return (NULL);
}
#endif
/*
* modify the reference table to contain the offsets and
@ -498,7 +518,11 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
* the reference table is needed only during compilation,
* now we can free it
*/
#ifdef _KERNEL
free(stream.refs, M_BPFJIT);
#else
free(stream.refs);
#endif
return ((bpf_filter_func)stream.ibuf);
}

View File

@ -1,6 +1,6 @@
/*-
* Copyright (c) 2002 - 2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (c) 2005 Jung-uk Kim <jkim@FreeBSD.org>
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,6 +32,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#ifdef _KERNEL
#include "opt_bpf.h"
#include <sys/param.h>
@ -39,14 +40,19 @@ __FBSDID("$FreeBSD$");
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/sysctl.h>
#else
#include <stdlib.h>
#include <sys/types.h>
#endif
#include <net/bpf.h>
#include <net/bpf_jitter.h>
MALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler");
bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, int *);
#ifdef _KERNEL
MALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler");
SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW, 0, "BPF JIT compiler");
int bpf_jitter_enable = 1;
SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,
@ -58,27 +64,27 @@ bpf_jitter(struct bpf_insn *fp, int nins)
bpf_jit_filter *filter;
/* Allocate the filter structure */
filter = (struct bpf_jit_filter *)malloc(sizeof(struct bpf_jit_filter),
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter),
M_BPFJIT, M_NOWAIT);
if (filter == NULL)
return NULL;
return (NULL);
/* Allocate the filter's memory */
filter->mem = (int *)malloc(BPF_MEMWORDS * sizeof(int),
M_BPFJIT, M_NOWAIT);
if (filter->mem == NULL) {
free(filter, M_BPFJIT);
return NULL;
return (NULL);
}
/* Create the binary */
if ((filter->func = bpf_jit_compile(fp, nins, filter->mem)) == NULL) {
free(filter->mem, M_BPFJIT);
free(filter, M_BPFJIT);
return NULL;
return (NULL);
}
return filter;
return (filter);
}
void
@ -89,3 +95,40 @@ bpf_destroy_jit_filter(bpf_jit_filter *filter)
free(filter->func, M_BPFJIT);
free(filter, M_BPFJIT);
}
#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);
/* Allocate the filter's memory */
filter->mem = (int *)malloc(BPF_MEMWORDS * sizeof(int));
if (filter->mem == NULL) {
free(filter);
return (NULL);
}
/* Create the binary */
if ((filter->func = bpf_jit_compile(fp, nins, filter->mem)) == NULL) {
free(filter->mem);
free(filter);
return (NULL);
}
return (filter);
}
void
bpf_destroy_jit_filter(bpf_jit_filter *filter)
{
free(filter->mem);
free(filter->func);
free(filter);
}
#endif

View File

@ -1,6 +1,6 @@
/*-
* Copyright (c) 2002 - 2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (c) 2005 Jung-uk Kim <jkim@FreeBSD.org>
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -34,7 +34,9 @@
#ifndef _NET_BPF_JITTER_H_
#define _NET_BPF_JITTER_H_
#ifdef _KERNEL
MALLOC_DECLARE(M_BPFJIT);
#endif
extern int bpf_jitter_enable;