Skip to content

Commit

Permalink
Added script for generating BPF asm and Lua code generated by libpcap…
Browse files Browse the repository at this point in the history
… and pflua.

- Generate BPF.

        $ pflang-compile --bpf "tcp port 80"

- Generate libpcap Lua code.

        $ pflang-compile --lua "tcp port 80"

- Generate pflua Lua code.

        $ pflang-compile --pflua "tcp port 80"

The flags are not exclusive, they can be combined to get several outputs
at the same time.

Some code reorganization within 'tools/' folder.
  • Loading branch information
dpino committed Aug 26, 2014
1 parent 4acab99 commit 84b7c14
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
10 changes: 10 additions & 0 deletions tools/helpers/pflang_to_lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module("pflang_to_lua", package.seeall)

package.path = package.path .. ";../src/?.lua"

local libpcap = require("pf.libpcap")
local bpf = require("pf.bpf")

function compile(expr)
bpf.compile(libpcap.compile(expr, "EN10MB"))
end
File renamed without changes.
120 changes: 120 additions & 0 deletions tools/pflang-compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/luajit

local DEBUG = false;

function usage()
local content = [=[
Usage: pflang-compile [--bpf] [--lua] [--pflua] <pflang-expression>

Options:

--bpf\tPrints out BPF asm code for <pflang-expression>
--lua\tPrints out libcap generated Lua code for <pflang--expression>
--pflua\tPrints out pflua generated Lua code for <pflang--expression>\t(DEFAULT)

]=]
print(content);
os.exit()
end

local function execute(command)
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
return result
end

local function Set(list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end

local function split(str, delim)
local result = { }
local pattern = string.format("([^%s]+)%s()", delim, delim)
while (true) do
line, pos = str:match(pattern, pos)
if line == nil then break end
table.insert(result, line)
end
return result
end

local function text_block_from(pattern, output)
local result = { }
local PRINT = DEBUG
for i, line in ipairs(split(output, "\n")) do
if (line:match(pattern)) then
PRINT = true
end
if (PRINT) then
table.insert(result, line)
end
end
return table.concat(result, "\n")
end

local function text_block_until(pattern, output)
local result = { }
for i, line in ipairs(split(output, "\n")) do
if (line:match(pattern)) then
break
end
table.insert(result, line)
end
return table.concat(result, "\n")
end

local function pflang_to_bpf(filter)
local command = "export PF_VERBOSE=1; luajit -l helpers/pflang_to_lua -e 'pflang_to_lua.compile(\"%s\")'"
local output = execute(command:format(filter))
return text_block_until("^return", output)
end

local function pflang_to_lua(filter)
local command = "export PF_VERBOSE=1; luajit -l helpers/pflang_to_lua -e 'pflang_to_lua.compile(\"%s\")'"
local output = execute(command:format(filter))
return text_block_from("^return", output)
end


local function pflang_to_pflua(filter)
local command = "export set PF_VERBOSE=1; cd ../src; luajit -l pf -e 'pf.compile_filter(\"%s\")'"
local output = execute(command:format(filter))
return text_block_from("^return", output)
end

-- Print help
if (#arg == 0) then
usage()
end

local flags = Set(arg)

-- Print help
if flags["--help"] or flags["-h"] then
usage()
end

-- No code-generation flag defined
if (not(flags["--bpf"] or flags["--lua"] or flags["--pflua"])) then
-- Default action
flags["--pflua"] = true
end

-- Print out everything in pflua mode
if flags["--debug"] or flags["-d"] then
DEBUG = true
end

local filter = arg[#arg]
if flags["--bpf"] then
print(pflang_to_bpf(filter));
end
if flags["--lua"] then
print(pflang_to_lua(filter));
end
if flags["--pflua"] then
print(pflang_to_pflua(filter));
end
6 changes: 3 additions & 3 deletions utils/pflua-asm → tools/pflua-asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Options:
--help | -h Prints this help.
<bpf expression> BPF expression to compile. DEFAULT: "tcp port 80".
See: http://biot.com/capstats/bpf.html
See: http://biot.com/capstats/bpf.html
EOF
}

Expand All @@ -27,13 +27,13 @@ if [ $# == 1 ]; then
fi

# Name of the script that prints out asm for a pred expression
lua_script="pflua_asm.lua"
lua_script="helpers/pflua_asm.lua"

# Linenumber where the 'execute_pred_ensuring_trace' function is defined
lineno=`grep -n 'function execute_pred_ensuring_trace' $lua_script | cut -d : -f 1`

# Iterate through all asm output but print out only the fragment of asm code for the compiled pred expression
output=`luajit -jdump=m -l pflua_asm -e 'pflua_asm.selftest()'`;
output=`luajit -jdump=m -l helpers/pflua_asm -e 'pflua_asm.selftest()'`;
while read -r line; do
# Was printing and found a blank line
if [ $PRINT -eq 1 ] && [ -z "$line" ]; then
Expand Down

0 comments on commit 84b7c14

Please sign in to comment.