Skip to content

Commit

Permalink
Merge snabbco#1029 branch 'snabbco/kbara-next' into next
Browse files Browse the repository at this point in the history
Resolved selftest conflict in lib.lua.
  • Loading branch information
lukego committed Oct 6, 2016
2 parents ad3a0db + 82292cd commit 89a1f26
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 117 deletions.
34 changes: 31 additions & 3 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,15 @@ Name of the app. *Read-only*.

— Field **myapp.shm**

Can be set to a specification for `core.shm.create_frame` during `new`. When
set, this field will be initialized to a frame of shared memory objects by the
engine.
Can be set to a specification for `core.shm.create_frame`. When set, this field
will be initialized to a frame of shared memory objects by the engine.


— Field **myapp.config**

Can be set to a specification for `core.lib.parse`. When set, the specification
will be used to validate the app’s arg when it is configured using
`config.app`.


— Method **myapp:link**
Expand Down Expand Up @@ -880,6 +886,28 @@ integers *n* respectively. Unsigned.
Network to host byte order conversion functions for 32 and 16 bit
integers *n* respectively. Unsigned.

— Function **lib.parse** *arg*, *config*

Validates *arg* against the specification in *config*, and returns a fresh
table containing the parameters in *arg* and any omitted optional parameters
with their default values. Given *arg*, a table of parameters or `nil`, assert
that from *config* all of the required keys are present, fill in any missing
values for optional keys, and error if any unknown keys are found. *Config* has
the following format:

```
config := { key = {[required=boolean], [default=value]}, ... }
```

Each key is optional unless `required` is set to a true value, and its default
value defaults to `nil`.

Example:

```
lib.parse({foo=42, bar=43}, {foo={required=true}, bar={}, baz={default=44}})
=> {foo=42, bar=43, baz=44}
```


## Main
Expand Down
13 changes: 6 additions & 7 deletions src/apps/bridge/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@
-- ports to limit the scope of flooding.

module(..., package.seeall)
local config = require("core.config")

bridge = subClass(nil)
bridge._name = "base bridge"
bridge.config = {
ports = {required=true},
split_horizon_groups = {},
config = {default={}}
}

function bridge:new (arg)
function bridge:new (conf)
assert(self ~= bridge, "Can't instantiate abstract class "..self:name())
local o = bridge:superClass().new(self)
local conf = arg and config.parse_app_arg(arg) or {}
assert(conf.ports, self._name..": invalid configuration")
o._conf = conf
if not o._conf.config then
o._conf.config = {}
end

-- Create a list of forwarding ports for all ports connected to the
-- bridge, taking split horizon groups into account
Expand Down
11 changes: 4 additions & 7 deletions src/apps/intel/intel10g.lua
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ function M_vf:reconfig(opts)
:set_MAC(opts.macaddr)
:set_mirror(opts.mirror)
:set_VLAN(opts.vlan)
:set_rx_stats(opts.rxcounter or 0)
:set_tx_stats(opts.txcounter or 0)
:set_rx_stats(opts.rxcounter)
:set_tx_stats(opts.txcounter)
:set_tx_rate(opts.rate_limit, opts.priority)
:enable_receive()
:enable_transmit()
Expand All @@ -718,8 +718,8 @@ function M_vf:init (opts)
:set_MAC(opts.macaddr)
:set_mirror(opts.mirror)
:set_VLAN(opts.vlan)
:set_rx_stats(opts.rxcounter or 0)
:set_tx_stats(opts.txcounter or 0)
:set_rx_stats(opts.rxcounter)
:set_tx_stats(opts.txcounter)
:set_tx_rate(opts.rate_limit, opts.priority)
:enable_receive()
:enable_transmit()
Expand Down Expand Up @@ -992,7 +992,6 @@ function M_vf:get_txstats ()
end

function M_vf:set_tx_rate (limit, priority)
limit = tonumber(limit) or 0
self.pf.r.RTTDQSEL(self.poolnum)
if limit >= 10 then
local factor = 10000 / tonumber(limit) -- line rate = 10,000 Mb/s
Expand All @@ -1001,8 +1000,6 @@ function M_vf:set_tx_rate (limit, priority)
else
self.pf.r.RTTBCNRC(0x00)
end

priority = tonumber(priority) or 1.0
self.pf.r.RTTDT1C(bit.band(math.floor(priority * 0x80), 0x3FF))
return self
end
Expand Down
22 changes: 17 additions & 5 deletions src/apps/intel/intel_app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ local register = require("lib.hardware.register")
local macaddress = require("lib.macaddress")
local intel10g = require("apps.intel.intel10g")
local receive, transmit, empty = link.receive, link.transmit, link.empty
Intel82599 = {}

Intel82599 = {
config = {
pciaddr = {required=true},
mtu = {},
macaddr = {},
vlan = {},
vmdq = {},
mirror = {},
rxcounter = {default=0},
txcounter = {default=0},
rate_limit = {default=0},
priority = {default=1.0}
}
}
Intel82599.__index = Intel82599

local C = ffi.C
Expand All @@ -35,8 +49,7 @@ local function firsthole(t)
end

-- Create an Intel82599 App for the device with 'pciaddress'.
function Intel82599:new (arg)
local conf = config.parse_app_arg(arg)
function Intel82599:new (conf)
local self = {}

if conf.vmdq then
Expand Down Expand Up @@ -111,8 +124,7 @@ function Intel82599:stop()
end


function Intel82599:reconfig(arg)
local conf = config.parse_app_arg(arg)
function Intel82599:reconfig (conf)
assert((not not self.dev.pf) == (not not conf.vmdq), "Can't reconfig from VMDQ to single-port or viceversa")

self.dev:reconfig(conf)
Expand Down
15 changes: 8 additions & 7 deletions src/apps/ipsec/esp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ local esp = require("lib.ipsec.esp")
local counter = require("core.counter")
local C = require("ffi").C

AES128gcm = {}

local provided_counters = {
'type', 'dtime', 'txerrors', 'rxerrors'
AES128gcm = {
config = {
spi = {required=true}, key = {required=true}, window_size = {}
},
shm = {
txerrors = {counter}, rxerrors = {counter}
}
}

function AES128gcm:new (arg)
local conf = arg and config.parse_app_arg(arg) or {}
function AES128gcm:new (conf)
local self = {}
self.encrypt = esp.esp_v6_encrypt:new{
mode = "aes-128-gcm",
Expand All @@ -28,7 +30,6 @@ function AES128gcm:new (arg)
keymat = conf.key:sub(1, 32),
salt = conf.key:sub(33, 40),
window_size = conf.replay_window}
self.shm = { txerrors = {counter}, rxerrors = {counter} }
return setmetatable(self, {__index = AES128gcm})
end

Expand Down
35 changes: 19 additions & 16 deletions src/apps/ipv6/nd_light.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ local lib = require("core.lib")

nd_light = subClass(nil)
nd_light._name = "Partial IPv6 neighbor discovery"
nd_light.config = {
local_mac = {required=true},
local_ip = {required=true},
next_hop = {required=true},
delay = {default=1000},
retrans = {}
}
nd_light.shm = {
status = {counter, 2}, -- Link down
rxerrors = {counter},
txerrors = {counter},
txdrop = {counter},
ns_checksum_errors = {counter},
ns_target_address_errors = {counter},
na_duplicate_errors = {counter},
na_target_address_errors = {counter},
nd_protocol_errors = {counter}
}

-- config:
-- local_mac MAC address of the interface attached to "south".
Expand Down Expand Up @@ -79,12 +97,8 @@ local function check_ip_address(ip, desc)
return ip
end

function nd_light:new (arg)
--copy the args to avoid changing the arg table so that it stays reusable.
local conf = arg and lib.deepcopy(config.parse_app_arg(arg)) or {}
function nd_light:new (conf)
local o = nd_light:superClass().new(self)
conf.delay = conf.delay or 1000
assert(conf.local_mac, "nd_light: missing local MAC address")
if type(conf.local_mac) == "string" and string.len(conf.local_mac) ~= 6 then
conf.local_mac = ethernet:pton(conf.local_mac)
else
Expand Down Expand Up @@ -199,17 +213,6 @@ function nd_light:new (arg)
}
o._logger = lib.logger_new({ module = 'nd_light' })

-- Create counters
o.shm = { status = {counter, 2}, -- Link down
rxerrors = {counter},
txerrors = {counter},
txdrop = {counter},
ns_checksum_errors = {counter},
ns_target_address_errors = {counter},
na_duplicate_errors = {counter},
na_target_address_errors = {counter},
nd_protocol_errors = {counter} }

return o
end

Expand Down
45 changes: 25 additions & 20 deletions src/apps/keyed_ipv6_tunnel/tunnel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,30 @@ local function prepare_header_template ()
header_template[SESSION_ID_OFFSET + 3] = 0xFF
end

SimpleKeyedTunnel = {}

function SimpleKeyedTunnel:new (arg)
local conf = arg and config.parse_app_arg(arg) or {}
-- required fields:
-- local_address, string, ipv6 address
-- remote_address, string, ipv6 address
-- local_cookie, 8 bytes hex string
-- remote_cookie, 8 bytes hex string
-- optional fields:
-- local_session, unsigned number, must fit to uint32_t
-- default_gateway_MAC, useful for testing
-- hop_limit, override default hop limit 64
SimpleKeyedTunnel = {
config = {
-- string, ipv6 address
local_address = {required=true},
remote_address = {required=true},
-- 8 bytes hex string
local_cookie = {required=true},
remote_cookie = {required=true},
-- unsigned number, must fit to uint32_t
local_session = {},
-- string, MAC address (for testing)
default_gateway_MAC = {},
-- unsigned integer <= 255
hop_limit = {}
},
shm = { rxerrors = {counter},
length_errors = {counter},
protocol_errors = {counter},
cookie_errors = {counter},
remote_address_errors = {counter},
local_address_errors = {counter} }
}

function SimpleKeyedTunnel:new (conf)
assert(
type(conf.local_cookie) == "string"
and #conf.local_cookie <= 16,
Expand Down Expand Up @@ -169,13 +180,7 @@ function SimpleKeyedTunnel:new (arg)
header = header,
remote_address = remote_address,
local_address = local_address,
remote_cookie = remote_cookie[0],
shm = { rxerrors = {counter},
length_errors = {counter},
protocol_errors = {counter},
cookie_errors = {counter},
remote_address_errors = {counter},
local_address_errors = {counter} }
remote_cookie = remote_cookie[0]
}

return setmetatable(o, {__index = SimpleKeyedTunnel})
Expand Down
19 changes: 12 additions & 7 deletions src/apps/lwaftr/loadgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ local clone = packet.clone

--- ### `RateLimitedRepeater` app: A repeater that can limit flow rate

RateLimitedRepeater = {}

function RateLimitedRepeater:new (arg)
local conf = arg and config.parse_app_arg(arg) or {}
--- By default, limit to 10 Mbps, just to have a default.
conf.rate = conf.rate or (10e6 / 8)
-- By default, allow for 255 standard packets in the queue.
conf.bucket_capacity = conf.bucket_capacity or (255 * 1500)
RateLimitedRepeater = {
config = {
-- rate: by default, limit to 10 Mbps, just to have a default.
rate = {default=10e6/8},
-- bucket_capacity: by default, allow for 255 standard packets in the
-- queue.
bucket_capacity = {default=255*1500},
initial_capacity = {}
}
}

function RateLimitedRepeater:new (conf)
conf.initial_capacity = conf.initial_capacity or conf.bucket_capacity
local o = {
index = 1,
Expand Down
5 changes: 4 additions & 1 deletion src/apps/pcap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ port to a PCAP file.
## Configuration

Both `PcapReader` and `PcapWriter` expect a filename string as their
configuration arguments to read from and write to respectively.
configuration arguments to read from and write to respectively. `PcapWriter`
will alternatively accept an array as its configuration argument, with the
first element being the filename and the second element being a *mode* argument
to `io.open`.
12 changes: 7 additions & 5 deletions src/apps/pcap/pcap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ end

PcapWriter = {}

function PcapWriter:new (filename, arg)
local conf = arg and config.parse_app_arg(arg) or {}
local mode = conf.overwrite and "w+" or "w"
local file, errno = io.open(filename, mode)
if errno then error(errno) end
function PcapWriter:new (filename)
local mode = "w"
if type(filename) == "table" then
mode = filename[2] or mode
filename = filename[1]
end
local file = assert(io.open(filename, "w"))
pcap.write_file_header(file)
return setmetatable({file = file}, {__index = PcapWriter})
end
Expand Down
13 changes: 8 additions & 5 deletions src/apps/rate_limiter/rate_limiter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ local floor, min = math.floor, math.min
-- bucket capacity and content - bytes
-- rate - bytes per second

RateLimiter = {}
RateLimiter = {
config = {
rate = {required=true},
bucket_capacity = {required=true},
initial_capacity = {required=false}
}
}

-- Source produces synthetic packets of such size
local PACKET_SIZE = 60

function RateLimiter:new (arg)
local conf = arg and config.parse_app_arg(arg) or {}
assert(conf.rate)
assert(conf.bucket_capacity)
function RateLimiter:new (conf)
conf.initial_capacity = conf.initial_capacity or conf.bucket_capacity
local o =
{
Expand Down
Loading

0 comments on commit 89a1f26

Please sign in to comment.