Skip to content

Instantly share code, notes, and snippets.

@upa
Last active December 26, 2017 15:08
Show Gist options
  • Save upa/c8494ea50142efc4d7f047e727b11cd9 to your computer and use it in GitHub Desktop.
Save upa/c8494ea50142efc4d7f047e727b11cd9 to your computer and use it in GitHub Desktop.
A MoonGen script: tx udp packets
-- This test does the following:
-- 2. Send UDP packets from NIC 1 to NIC 2
--
local mg = require "moongen"
local memory = require "memory"
local device = require "device"
local ts = require "timestamping"
local filter = require "filter"
local hist = require "histogram"
local stats = require "stats"
local timer = require "timer"
local arp = require "proto.arp"
local log = require "log"
local timer = require "timer"
local ffi = require "ffi"
-- set addresses here
local SRC_MAC = "ec:0d:9a:7d:84:4e"
local DST_MAC = "ec:0d:9a:7d:83:06"
local SRC_IP_BASE = "10.0.0.2"
local DST_IP = "10.0.0.1"
local SRC_PORT = 1234
local DST_PORT = 60000
local C = ffi.C
function configure(parser)
parser:description("Generates UDP traffic.")
parser:argument("txDev", "Device to transmit from."):convert(tonumber)
parser:option("-q --qnum", "Queue num to be used for TX"):default(1):convert(tonumber)
parser:option("-s --size", "Packet size."):default(60):convert(tonumber)
parser:option("-t --timeout", "timeout"):default(0):convert(tonumber)
end
function master(args)
txDev = device.config{port = args.txDev, txQueues = args.qnum}
device.waitForLinks()
for n = 0, args.qnum - 1 do
mg.startTask("loadSlave", txDev:getTxQueue(n), args.size, args.timeout)
end
mg.waitForTasks()
end
local function fillUdpPacket(buf, len)
buf:getUdpPacket():fill{
ethSrc = SRC_MAC,
ethDst = DST_MAC,
ip4Src = SRC_IP,
ip4Dst = DST_IP,
udpSrc = SRC_PORT,
udpDst = DST_PORT,
pktLength = len
}
end
--- Runs on the sending NIC
--- Generates UDP traffic and also fetches the stats
function loadSlave(queue, size, timeout)
log:info(green("Starting up: LoadSlave"))
local timer = timer:new(timeout)
local mempool = memory.createMemPool(function(buf)
fillUdpPacket(buf, size)
end)
local bufs = mempool:bufArray()
local txCtr = stats:newDevTxCounter(queue, "plain")
local baseIP = parseIPAddress(SRC_IP_BASE)
-- send out UDP packets until the user stops the script
while mg.running() do
bufs:alloc(size)
for i, buf in ipairs(bufs) do
local pkt = buf:getUdpPacket()
pkt.ip4.src:set(baseIP)
pkt.udp.src = i + 1234
end
--- UDP checksums are optional, so using just IPv4 checksums would be sufficient here
bufs:offloadUdpChecksums()
queue:send(bufs)
txCtr:update()
if timeout > 0 and timer:expired() then
break
end
end
txCtr:finalize()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment