Skip to content

Commit

Permalink
expand synth to generate random payloads and include a packet id, add…
Browse files Browse the repository at this point in the history
… apps.test.npackets an app that only sends n packets from input to output
  • Loading branch information
petebristow committed Feb 15, 2019
1 parent 31c8d78 commit 4a04933
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/apps/test/README.md
Expand Up @@ -69,3 +69,29 @@ Source and destination MAC addresses in human readable from. The default is

An array of numbers designating the packet payload sizes. The default is
`{64}`.

— Key **random_payload**
Generate a random payload for each packet in `sizes`

— Key **packet_id**
Insert the packet number (32bit uint) directly after the ethertype. The packet
number starts at 0 and is sequential on each output link.

## Npackets (apps.test.npackets)

The `Npackets` app allows are most N packets to flow through it. Any further
packets are never dequeued from input.

DIAGRAM: Npackets
+-----------+
input -> | Npackets | -> output
+-----------+

### Configuration

The `Npackets` app accepts a table as its configuration argument. The following
keys are defined:

— Key **npackets**
The number of packets to forward, further packets are never dequeued from
input.
41 changes: 41 additions & 0 deletions src/apps/test/npackets.lua
@@ -0,0 +1,41 @@
module(...,package.seeall)

local ffi = require("ffi")
local C = ffi.C
Npackets = {
config = {
npackets = { default = 1000000 }
}
}

function Npackets:new (conf)
return setmetatable({n = conf.npackets}, { __index = Npackets})
end

function Npackets:push ()
while not link.empty(self.input.input) and
self.n > 0 do
link.transmit(self.output.output, link.receive(self.input.input))
self.n = self.n - 1
end
end

function selftest()
local synth = require("apps.test.synth")
local basic_apps = require("apps.basic.basic_apps")
local counter = require("core.counter")

local c = config.new()
config.app(c, "src", synth.Synth)
config.app(c, "n", Npackets, { npackets = 100 })
config.app(c, "sink", basic_apps.Sink)
config.link(c, "src.output -> n.input")
config.link(c, "n.output -> sink.input")
engine.configure(c)
assert(engine.app_table.n.n == 100)
engine.main({duration=1})
assert(engine.app_table.n.n == 0)
assert(counter.read(engine.link_table['src.output -> n.input'].stats.txpackets) > 100)
local cnt = counter.read(engine.link_table['n.output -> sink.input'].stats.rxpackets)
assert(100 == cnt)
end
18 changes: 17 additions & 1 deletion src/apps/test/synth.lua
Expand Up @@ -12,6 +12,8 @@ Synth = {
sizes = {default={64}},
src = {default='00:00:00:00:00:00'},
dst = {default='00:00:00:00:00:00'},
random_payload = { default = false },
packet_id = { default = false },
}
}

Expand All @@ -23,6 +25,14 @@ function Synth:new (conf)
assert(payload_size >= 0 and payload_size <= 1536,
"Invalid payload size: "..payload_size)
local data = ffi.new("char[?]", payload_size)
if conf.random_payload then
-- dstmac + srcmac + type
local off = 6 + 6 + 2
ffi.fill(data, payload_size)
ffi.copy(data + off,
lib.random_bytes(payload_size - off), payload_size - off)
end

local dgram = datagram:new(packet.from_pointer(data, payload_size))
local ether = ethernet:new({ src = ethernet:pton(conf.src),
dst = ethernet:pton(conf.dst),
Expand All @@ -38,7 +48,13 @@ function Synth:pull ()
local n = 0
while n < engine.pull_npackets do
for _, p in ipairs(self.packets) do
transmit(o, packet.clone(p))
local c = packet.clone(p)
if self.packet_id then
-- 14 == sizeof(dstmac srcmac type)
ffi.cast("uint32_t *", clone.data+14)[0] = lib.htonl(self.pktid)
self.pktid = self.pktid + 1
end
transmit(o, c)
n = n + 1
end
end
Expand Down

0 comments on commit 4a04933

Please sign in to comment.