Created
March 18, 2019 14:27
-
-
Save isaac-ped/11d4136a3acd06e602839950c467782c to your computer and use it in GitHub Desktop.
Moongen script to capture packets at high rate with accurate timestamps
This file contains hidden or 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
-- Capture packets on a port with high rate and accurate timestamps | |
local lm = require "libmoon" | |
local device = require "device" | |
local log = require "log" | |
local memory = require "memory" | |
local stats = require "stats" | |
local pcap = require "pcap" | |
function configure(parser) | |
parser:argument("dev_in", "Device on which to capture from"):args(1):convert(tonumber) | |
parser:argument("output", "Pcap file to output logged packets to"); | |
parser:option("-t --threads", "Number of threads per forwarding direction using RSS."):args(1):convert(tonumber):default(1) | |
parser:option("-s --snap-len", "Truncate packets to this size."):convert(tonumber):target("snapLen") | |
local args = parser:parse() | |
return args | |
end | |
function master(args) | |
local dev_in = device.config { | |
port = args.dev_in, | |
txQueues = args.threads, | |
rssQueues = args.threads, | |
dropEnable = false, | |
rxDescs = 8192 | |
} | |
device.waitForLinks() | |
stats.startStatsTask{rxDevices = {dev_in}} | |
for i = 1, args.threads do | |
lm.startTask("capture", dev_in:getRxQueue(i - 1), args, i) | |
end | |
lm.waitForTasks() | |
end | |
function capture(rxQueue, args, threadId) | |
local snapLen = args.snapLen | |
local captureCtr | |
local output = args.output | |
if args.threads > 1 then | |
if output:match("%.pcap$") then | |
output = output:gsub("%.pcap$", "") | |
end | |
output = output .. "-thread-" .. threadId .. ".pcap" | |
else | |
if not output:match("%.pcap$") then | |
output = output .. ".pcap" | |
end | |
end | |
local writer = pcap:newWriter(output) | |
local captureCtr = stats:newPktRxCounter("Capture return, thread #" .. threadId) | |
local bufs = memory.bufArray() | |
while lm.running() do | |
local count = rxQueue:recvWithTimestamps(bufs) | |
for i = 1, count do | |
local buf = bufs[i] | |
local rxts = tonumber(bufs[i].udata64) * 1e-9; | |
writer:writeBuf(rxts, buf, snapLen) | |
captureCtr:countPacket(buf) | |
end | |
bufs:free(count) | |
end | |
captureCtr:finalize() | |
log:info("Flushing rcv only buffers") | |
writer:close() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment