Skip to content

Commit

Permalink
core: Use the array part of app.input and app.output tables
Browse files Browse the repository at this point in the history
Previously the links for apps were accessible only by name
e.g. input.rx or output.tx. Now they are available both by name and by
number e.g. input[1] or output[2]. The exact order is not fixed.

This makes it easy to write apps that don't care what their links are
called. If there is only one link then it can be accessed as input[1].
If there are many links they can be accessed with an ipairs() loop.

The relink() method of apps is also removed. The only way that it was
actually used was to emulate this behavior in a more complex way. That
code is also removed now from basic_apps and snabbmark.

This may resolve the question: what should an app name its input and
output links in cases where it does not actually matter? Now instead
of having to choose an arbitrary name like 'rx' or 'input' or 'left'
or 'west' the app can simply say that it requires one link and then
access that by number.

So code like this:

    local rx = input.rx
    assert(rx, "rx link not connected")

could instead be written as:

    assert(#input == 1, "one input link expected")
    local rx = input[1]

which would be backwards compatible and allow the link to have any
name. That would solve the existing problem of apps expecting
inconsistent names.

(This does not apply to cases where apps actually need to have
multiple links that are distinguished by their names e.g. a tunnel app
that will encapsulate packets from one input and decapsulate those
from another.)
  • Loading branch information
lukego committed May 10, 2015
1 parent b69ad9c commit c4a7885
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 41 deletions.
7 changes: 0 additions & 7 deletions src/README.md.src
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ For example: Move packets from input ports to output ports or to a
network adapter.


— Method **myapp:relink**

*Optional*. React to a changes in input/output links (`app.input` and
`app.output`). This method is called after a link reconfiguration but
before the next packets are processed.


— Method **myapp:reconfig** *arg*

*Optional*. Reconfigure the app with a new *arg*. If this method is not
Expand Down
46 changes: 17 additions & 29 deletions src/apps/basic/basic_apps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,9 @@ local transmit, receive = link.transmit, link.receive
local ffi = require("ffi")
local C = ffi.C

Basic = {}

function Basic:relink ()
self.inputi, self.outputi = {}, {}
for _,l in pairs(self.output) do
table.insert(self.outputi, l)
end
for _,l in pairs(self.input) do
table.insert(self.inputi, l)
end
end

--- # `Source` app: generate synthetic packets

Source = setmetatable({zone = "Source"}, {__index = Basic})
Source = {}

function Source:new(size)
size = tonumber(size) or 60
Expand All @@ -34,7 +22,7 @@ function Source:new(size)
end

function Source:pull ()
for _, o in ipairs(self.outputi) do
for _, o in ipairs(self.output) do
for i = 1, link.nwritable(o) do
transmit(o, packet.clone(self.packet))
end
Expand All @@ -47,14 +35,14 @@ end

--- # `Join` app: Merge multiple inputs onto one output

Join = setmetatable({zone = "Join"}, {__index = Basic})
Join = {}

function Join:new()
return setmetatable({}, {__index=Join})
end

function Join:push ()
for _, inport in ipairs(self.inputi) do
for _, inport in ipairs(self.input) do
for n = 1,math.min(link.nreadable(inport), link.nwritable(self.output.out)) do
transmit(self.output.out, receive(inport))
end
Expand All @@ -65,15 +53,15 @@ end

-- For each input port, push packets onto outputs. When one output
-- becomes full then continue with the next.
Split = setmetatable({zone = "Split"}, {__index = Basic})
Split = {}

function Split:new ()
return setmetatable({}, {__index=Split})
end

function Split:push ()
for _, i in ipairs(self.inputi) do
for _, o in ipairs(self.outputi) do
for _, i in ipairs(self.input) do
for _, o in ipairs(self.output) do
for _ = 1, math.min(link.nreadable(i), link.nwritable(o)) do
transmit(o, receive(i))
end
Expand All @@ -83,14 +71,14 @@ end

--- ### `Sink` app: Receive and discard packets

Sink = setmetatable({zone = "Sink"}, {__index = Basic})
Sink = {}

function Sink:new ()
return setmetatable({}, {__index=Sink})
end

function Sink:push ()
for _, i in ipairs(self.inputi) do
for _, i in ipairs(self.input) do
for _ = 1, link.nreadable(i) do
local p = receive(i)
packet.free(p)
Expand All @@ -100,26 +88,26 @@ end

--- ### `Tee` app: Send inputs to all outputs

Tee = setmetatable({zone = "Tee"}, {__index = Basic})
Tee = {}

function Tee:new ()
return setmetatable({}, {__index=Tee})
end

function Tee:push ()
noutputs = #self.outputi
noutputs = #self.output
if noutputs > 0 then
local maxoutput = link.max
for _, o in ipairs(self.outputi) do
for _, o in ipairs(self.output) do
maxoutput = math.min(maxoutput, link.nwritable(o))
end
for _, i in ipairs(self.inputi) do
for _, i in ipairs(self.input) do
for _ = 1, math.min(link.nreadable(i), maxoutput) do
local p = receive(i)
maxoutput = maxoutput - 1
do local outputi = self.outputi
for k = 1, #outputi do
transmit(outputi[k], k == #outputi and p or packet.clone(p))
do local output = self.output
for k = 1, #output do
transmit(output[k], k == #output and p or packet.clone(p))
end
end
end
Expand All @@ -129,7 +117,7 @@ end

--- ### `Repeater` app: Send all received packets in a loop

Repeater = setmetatable({zone = "Repeater"}, {__index = Basic})
Repeater = {}

function Repeater:new ()
return setmetatable({index = 1, packets = {}},
Expand Down
5 changes: 2 additions & 3 deletions src/core/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,13 @@ function apply_config_actions (actions, conf)
link.receiving_app = app_name_to_index[ta]
-- Add link to apps
new_app_table[fa].output[fl] = link
table.insert(new_app_table[fa].output, link)
new_app_table[ta].input[tl] = link
table.insert(new_app_table[ta].input, link)
-- Remember link
new_link_table[linkspec] = link
table.insert(new_link_array, link)
end
for _, app in ipairs(new_app_array) do
if app.relink then app:relink() end
end
-- commit changes
app_table, link_table = new_app_table, new_link_table
app_array, link_array = new_app_array, new_link_array
Expand Down
4 changes: 2 additions & 2 deletions src/program/snabbmark/snabbmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ function get_sf_devices()
return sf_devices
end

Source = setmetatable({zone = "Source"}, {__index = basic_apps.Basic});
Source = {}

function Source:new(size)
return setmetatable({}, {__index=Source})
end

function Source:pull()
for _, o in ipairs(self.outputi) do
for _, o in ipairs(self.output) do
for i = 1, link.nwritable(o) do
local p = packet.allocate()
ffi.copy(p.data, self.to_mac_address, 6)
Expand Down

0 comments on commit c4a7885

Please sign in to comment.