forked from snabbco/snabb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request snabbco#15 from takikawa/snabbwall-dev
Optimizations for snabbwall & benchmarking script
- Loading branch information
Showing
8 changed files
with
273 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module(..., package.seeall) | ||
|
||
local lib = require("core.lib") | ||
|
||
-- Constants: Ethernet | ||
ETH_TYPE_IPv4 = lib.htons(0x0800) | ||
ETH_TYPE_IPv6 = lib.htons(0x86DD) | ||
ETH_TYPE_VLAN = lib.htons(0x8100) | ||
ETH_TYPE_OFFSET = 12 | ||
ETH_HEADER_SIZE = 14 | ||
|
||
-- Constants: IPv4 | ||
IPv4_VER_IHL_OFFSET = 0 | ||
IPv4_DSCP_ECN_OFFSET = 1 | ||
IPv4_LEN_OFFSET = 2 | ||
IPv4_FRAG_ID_OFFSET = 4 | ||
IPv4_FLAGS_OFFSET = 6 | ||
IPv4_TTL_OFFSET = 8 | ||
IPv4_PROTO_OFFSET = 9 | ||
IPv4_CHECKSUM_OFFSET = 10 | ||
IPv4_SRC_ADDR_OFFSET = 12 | ||
IPv4_DST_ADDR_OFFSET = 16 | ||
|
||
IPv4_PROTO_TCP = 6 -- uint8_t | ||
IPv4_PROTO_UDP = 17 -- uint8_t | ||
|
||
-- Constants: IPv6 | ||
IPv6_MIN_HEADER_SIZE = 40 | ||
IPv6_PLOADLEN_OFFSET = 4 | ||
IPv6_NEXTHDR_OFFSET = 6 | ||
IPv6_HOPLIMIT_OFFSET = 7 | ||
IPv6_SRC_ADDR_OFFSET = 8 | ||
IPv6_DST_ADDR_OFFSET = 24 | ||
|
||
IPv6_NEXTHDR_HOPBYHOP= 0 | ||
IPv6_NEXTHDR_TCP = 6 | ||
IPv6_NEXTHDR_UDP = 17 | ||
IPv6_NEXTHDR_ROUTING = 43 | ||
IPv6_NEXTHDR_FRAGMENT= 44 | ||
IPv6_NEXTHDR_ESP = 50 | ||
IPv6_NEXTHDR_AH = 51 | ||
IPv6_NEXTHDR_ICMPv6 = 58 | ||
IPv6_NEXTHDR_NONE = 59 | ||
IPv6_NEXTHDR_DSTOPTS = 60 | ||
|
||
-- Constants: TCP | ||
TCP_HEADER_SIZE = 20 | ||
TCP_SRC_PORT_OFFSET = 0 | ||
TCP_DST_PORT_OFFSET = 2 | ||
|
||
-- Constants: UDP | ||
UDP_HEADER_SIZE = 8 | ||
UDP_SRC_PORT_OFFSET = 0 | ||
UDP_DST_PORT_OFFSET = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
-- This module creates a wrapper around nDPI's packet processing | ||
-- functions in order to help LuaJIT do its optimizations | ||
-- | ||
-- Specifically, it avoids a struct value return by creating wrappers | ||
-- that pass a struct pointer in instead. LuaJIT can handle that | ||
-- better than a struct return (which is not supported). | ||
|
||
module(..., package.seeall) | ||
|
||
local dasm = require("dasm") | ||
local ffi = require("ffi") | ||
local ndpi = require("ndpi.c").lib | ||
|
||
local debug = false | ||
|
||
|.arch x64 | ||
|.actionlist actions | ||
|
||
-- the definitions here (anchor, assemble, gen) are borrowed from lwaftr | ||
-- (see multi_copy.lua) | ||
__anchor = {} | ||
|
||
local function assemble (name, prototype, generator) | ||
local Dst = dasm.new(actions) | ||
generator(Dst) | ||
local mcode, size = Dst:build() | ||
table.insert(__anchor, mcode) | ||
if debug then | ||
print("mcode dump: "..name) | ||
dasm.dump(mcode, size) | ||
end | ||
return ffi.cast(prototype, mcode) | ||
end | ||
|
||
local function gen_ndppw(orig_f) | ||
local function gen(Dst) | ||
-- pass the first stack argument onto the original function | ||
| mov rax, [rsp+8] | ||
| push rax | ||
|
||
-- call the original function, do stack cleanup | ||
| mov64 rax, orig_f | ||
| call rax | ||
| add rsp, 8 | ||
|
||
-- at this point, rax and rdx have struct | ||
-- fields in them, which we want to write into | ||
-- the struct pointer (2nd stack arg) | ||
| mov rcx, [rsp+16] | ||
| mov [rcx], rax | ||
| mov [rcx+4], rdx | ||
|
||
| ret | ||
end | ||
|
||
return gen | ||
end | ||
|
||
local function gen_nupw(orig_f) | ||
local function gen(Dst) | ||
-- call the original function, aligning on 16 | ||
| sub rsp, 8 | ||
| mov64 rax, orig_f | ||
| call rax | ||
| add rsp, 8 | ||
|
||
-- like above, write into struct | ||
| mov rcx, [rsp+8] | ||
| mov [rcx], rax | ||
| mov [rcx+4], rdx | ||
|
||
| ret | ||
end | ||
|
||
return gen | ||
end | ||
|
||
-- see ljndpi/ndpi/c.lua for the corresponding headers for these functions | ||
-- these have an extra void* argument at the end for the struct pointer | ||
local function make_ndpp_wrapper(f) | ||
local wrap = assemble("ndpi_detection_process_packet_wrapper", | ||
ffi.typeof("void (*)(void*, void*, void*, unsigned short, uint64_t, void*, void*, void*)"), | ||
gen_ndppw(f)) | ||
return function(self, flow, data, len, tick, src, dst) | ||
local proto = ffi.new("ndpi_protocol_t") | ||
wrap(self, flow, data, len, tick, src, dst, proto) | ||
return proto.master_protocol, proto.protocol | ||
end | ||
end | ||
|
||
local function make_ngup_wrapper(f) | ||
local wrap = assemble("ndpi_detection_process_packet_wrapper", | ||
ffi.typeof("void (*)(void*, uint8_t, uint32_t, uint16_t, uint32_t, uint16_t, void*)"), | ||
gen_nupw(f)) | ||
return function(self, prot_n, src_h, src_p, dst_h, dst_p) | ||
local proto = ffi.new("ndpi_protocol_t") | ||
wrap(self, prot_n, src_h, src_p, dst_h, dst_p, proto) | ||
return proto.master_protocol, proto.protocol | ||
end | ||
end | ||
|
||
process_packet = make_ndpp_wrapper(ndpi.ndpi_detection_process_packet) | ||
guess_undetected_protocol = make_ngup_wrapper(ndpi.ndpi_guess_undetected_protocol) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.