From 494533187fc40b819e5ff3b7f071f20738dc3491 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Tue, 21 Oct 2014 15:42:22 +0200 Subject: [PATCH 1/4] [core.config] Make `parse_app_arg' a NOOP for tables. --- src/core/config.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/config.lua b/src/core/config.lua index 73e4f45977..29e773150f 100644 --- a/src/core/config.lua +++ b/src/core/config.lua @@ -59,9 +59,11 @@ end -- Return a Lua object for the arg to an app. -- Example: -- parse_app_arg("{ timeout = 5 * 10 }") => { timeout = 50 } -function parse_app_arg (s) - assert(type(s) == 'string') - return loadstring("return " .. s)() +-- parse_app_arg() =>
(NOOP) +function parse_app_arg (arg) + if type(arg) == 'string' then return loadstring("return " .. arg)() + elseif type(arg) == 'table' then return arg + else error(" is not a string or table.") end end function graphviz (c) From edda471ded99fe74c7210f78d35d48cdfd2ce3a6 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Tue, 21 Oct 2014 15:44:21 +0200 Subject: [PATCH 2/4] [META] Use `core.config.parse_app_arg' uniformly. --- src/apps/intel/intel_app.lua | 16 +++++----- src/apps/ipv6/ipv6.lua | 5 ++-- src/apps/keyed_ipv6_tunnel/tunnel.lua | 37 ++++++++++++------------ src/apps/packet_filter/packet_filter.lua | 4 +-- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/apps/intel/intel_app.lua b/src/apps/intel/intel_app.lua index ff7d4cc665..68cbcf64a5 100644 --- a/src/apps/intel/intel_app.lua +++ b/src/apps/intel/intel_app.lua @@ -24,20 +24,20 @@ end -- Create an Intel82599 App for the device with 'pciaddress'. -function Intel82599:new (args) - args = config.parse_app_arg(args) +function Intel82599:new (arg) + local conf = config.parse_app_arg(arg) - if args.vmdq then - if devices[args.pciaddr] == nil then - devices[args.pciaddr] = {pf=intel10g.new_pf(args.pciaddr):open(), vflist={}} + if conf.vmdq then + if devices[conf.pciaddr] == nil then + devices[conf.pciaddr] = {pf=intel10g.new_pf(conf.pciaddr):open(), vflist={}} end - local dev = devices[args.pciaddr] + local dev = devices[conf.pciaddr] local poolnum = firsthole(dev.vflist)-1 local vf = dev.pf:new_vf(poolnum) dev.vflist[poolnum+1] = vf - return setmetatable({dev=vf:open(args)}, Intel82599) + return setmetatable({dev=vf:open(conf)}, Intel82599) else - local dev = intel10g.new_sf(args.pciaddr) + local dev = intel10g.new_sf(conf.pciaddr) :open() :autonegotiate_sfi() :wait_linkup() diff --git a/src/apps/ipv6/ipv6.lua b/src/apps/ipv6/ipv6.lua index fbe6811d4c..a4af857981 100644 --- a/src/apps/ipv6/ipv6.lua +++ b/src/apps/ipv6/ipv6.lua @@ -60,9 +60,8 @@ struct { SimpleIPv6 = {} -function SimpleIPv6:new (confstring) - print("confstring", confstring) - local conf = confstring and loadstring("return " .. confstring)() or {} +function SimpleIPv6:new (arg) + local conf = config and config.parse_app_arg(arg) or {} own_mac = conf.own_mac or "\x52\x54\x00\x12\x34\x57" own_ip = conf.own_ip or "\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" local o = {own_mac = own_mac, own_ip = own_ip} diff --git a/src/apps/keyed_ipv6_tunnel/tunnel.lua b/src/apps/keyed_ipv6_tunnel/tunnel.lua index ddb8c9d063..d5b2590f93 100644 --- a/src/apps/keyed_ipv6_tunnel/tunnel.lua +++ b/src/apps/keyed_ipv6_tunnel/tunnel.lua @@ -102,8 +102,8 @@ end SimpleKeyedTunnel = {} -function SimpleKeyedTunnel:new (confstring) - local config = confstring and loadstring("return " .. confstring)() or {} +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 @@ -114,23 +114,22 @@ function SimpleKeyedTunnel:new (confstring) -- default_gateway_MAC, useful for testing -- hop_limit, override default hop limit 64 assert( - type(config.local_cookie) == "string" - and #config.local_cookie <= 16, + type(conf.local_cookie) == "string" + and #conf.local_cookie <= 16, "local_cookie should be 8 bytes hex string" ) - config.local_cookie = lib.hexundump(config.local_cookie, 8) assert( - type(config.remote_cookie) == "string" - and #config.remote_cookie <= 16, + type(conf.remote_cookie) == "string" + and #conf.remote_cookie <= 16, "remote_cookie should be 8 bytes hex string" ) - config.remote_cookie = lib.hexundump(config.remote_cookie, 8) local header = header_array_ctype(HEADER_SIZE) ffi.copy(header, header_template, HEADER_SIZE) + local local_cookie = lib.hexundump(conf.local_cookie, 8) ffi.copy( header + COOKIE_OFFSET, - config.local_cookie, - #config.local_cookie + local_cookie, + #local_cookie ) -- convert dest, sorce ipv6 addressed to network order binary @@ -146,22 +145,22 @@ function SimpleKeyedTunnel:new (confstring) local remote_address = ffi.cast(paddress_ctype, header + DST_IP_OFFSET) local local_address = ffi.cast(paddress_ctype, header + SRC_IP_OFFSET) - local remote_cookie = ffi.cast(pcookie_ctype, config.remote_cookie) + local remote_cookie = ffi.cast(pcookie_ctype, lib.hexundump(conf.remote_cookie, 8)) - if config.local_session then + if conf.local_session then local psession = ffi.cast(psession_id_ctype, header + SESSION_ID_OFFSET) - psession[0] = lib.htonl(config.local_session) + psession[0] = lib.htonl(conf.local_session) end - if config.default_gateway_MAC then - local mac = assert(macaddress:new(config.default_gateway_MAC)) + if conf.default_gateway_MAC then + local mac = assert(macaddress:new(conf.default_gateway_MAC)) ffi.copy(header + DST_MAC_OFFSET, mac.bytes, 6) end - if config.hop_limit then - assert(type(config.hop_limit) == 'number' and - config.hop_limit <= 255, "invalid hop limit") - header[HOP_LIMIT_OFFSET] = config.hop_limit + if conf.hop_limit then + assert(type(conf.hop_limit) == 'number' and + conf.hop_limit <= 255, "invalid hop limit") + header[HOP_LIMIT_OFFSET] = conf.hop_limit end local o = diff --git a/src/apps/packet_filter/packet_filter.lua b/src/apps/packet_filter/packet_filter.lua index 32a7d6e49d..3ed7dec94e 100644 --- a/src/apps/packet_filter/packet_filter.lua +++ b/src/apps/packet_filter/packet_filter.lua @@ -379,8 +379,8 @@ end PacketFilter = {} -function PacketFilter:new (confstring) - local rules = confstring and loadstring("return " .. confstring)() or {} +function PacketFilter:new (arg) + local rules = arg and config.parse_app_arg(arg) or {} assert(rules) assert(#rules > 0) From 40b6518b8b593f8f3e1d81a6274286fbeba715db Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Tue, 21 Oct 2014 15:48:09 +0200 Subject: [PATCH 3/4] [META] Refrain from using `([[{...}]]):format(...)' constructs and use table literals instead. --- src/apps/intel/intel_app.lua | 57 +++++++++---------- src/apps/keyed_ipv6_tunnel/tunnel.lua | 6 +- src/core/config.lua | 2 +- src/designs/csv/csv | 4 +- src/designs/nfv/nfv | 2 +- src/lib/nfv/config.lua | 24 ++++---- src/test_fixtures/nfvconfig/switch_filter/x | 5 +- src/test_fixtures/nfvconfig/switch_filter/y | 5 +- .../nfvconfig/test_functions/filter.ports | 10 ++-- 9 files changed, 55 insertions(+), 60 deletions(-) diff --git a/src/apps/intel/intel_app.lua b/src/apps/intel/intel_app.lua index 68cbcf64a5..e1f437480d 100644 --- a/src/apps/intel/intel_app.lua +++ b/src/apps/intel/intel_app.lua @@ -210,8 +210,8 @@ function sq_sq(pcidevA, pcidevB) print ('just send a lot of packets through the wire') config.app(c, 'source1', basic_apps.Source) config.app(c, 'source2', basic_apps.Source) - config.app(c, 'nicA', Intel82599, ([[{pciaddr='%s'}]]):format(pcidevA)) - config.app(c, 'nicB', Intel82599, ([[{pciaddr='%s'}]]):format(pcidevB)) + config.app(c, 'nicA', Intel82599, {pciaddr=pcidevA}) + config.app(c, 'nicB', Intel82599, {pciaddr=pcidevB}) config.app(c, 'sink', basic_apps.Sink) config.link(c, 'source1.out -> nicA.rx') config.link(c, 'source2.out -> nicB.rx') @@ -244,23 +244,20 @@ function mq_sq(pcidevA, pcidevB) local c = config.new() config.app(c, 'source_ms', basic_apps.Join) config.app(c, 'repeater_ms', basic_apps.Repeater) - config.app(c, 'nicAs', Intel82599, ([[{ - -- Single App on NIC A - pciaddr = '%s', - macaddr = '52:54:00:01:01:01', - }]]):format(pcidevA)) - config.app(c, 'nicBm0', Intel82599, ([[{ - -- first VF on NIC B - pciaddr = '%s', - vmdq = true, - macaddr = '52:54:00:02:02:02', - }]]):format(pcidevB)) - config.app(c, 'nicBm1', Intel82599, ([[{ - -- second VF on NIC B - pciaddr = '%s', - vmdq = true, - macaddr = '52:54:00:03:03:03', - }]]):format(pcidevB)) + config.app(c, 'nicAs', Intel82599, + {-- Single App on NIC A + pciaddr = pcidevA, + macaddr = '52:54:00:01:01:01'}) + config.app(c, 'nicBm0', Intel82599, + {-- first VF on NIC B + pciaddr = pcidevB, + vmdq = true, + macaddr = '52:54:00:02:02:02'}) + config.app(c, 'nicBm1', Intel82599, + {-- second VF on NIC B + pciaddr = pcidevB, + vmdq = true, + macaddr = '52:54:00:03:03:03'}) print ('-------') print ("Send a bunch of from the SF on NIC A to the VFs on NIC B") print ("half of them go to nicBm0 and nicBm0") @@ -299,18 +296,16 @@ function mq_sw(pcidevA) local c = config.new() config.app(c, 'source_ms', basic_apps.Join) config.app(c, 'repeater_ms', basic_apps.Repeater) - config.app(c, 'nicAm0', Intel82599, ([[{ - -- first VF on NIC A - pciaddr = '%s', - vmdq = true, - macaddr = '52:54:00:01:01:01', - }]]):format(pcidevA)) - config.app(c, 'nicAm1', Intel82599, ([[{ - -- second VF on NIC A - pciaddr = '%s', - vmdq = true, - macaddr = '52:54:00:02:02:02', - }]]):format(pcidevA)) + config.app(c, 'nicAm0', Intel82599, + {-- first VF on NIC A + pciaddr = pcidevA, + vmdq = true, + macaddr = '52:54:00:01:01:01'}) + config.app(c, 'nicAm1', Intel82599, + {-- second VF on NIC A + pciaddr = pcidevA, + vmdq = true, + macaddr = '52:54:00:02:02:02'}) print ('-------') print ("Send a bunch of packets from Am0") print ("half of them go to nicAm1 and half go nowhere") diff --git a/src/apps/keyed_ipv6_tunnel/tunnel.lua b/src/apps/keyed_ipv6_tunnel/tunnel.lua index d5b2590f93..1054e3488b 100644 --- a/src/apps/keyed_ipv6_tunnel/tunnel.lua +++ b/src/apps/keyed_ipv6_tunnel/tunnel.lua @@ -273,15 +273,13 @@ function selftest () local input_file = "apps/keyed_ipv6_tunnel/selftest.cap.input" local output_file = "apps/keyed_ipv6_tunnel/selftest.cap.output" - local tunnel_config = - [[{ + local tunnel_config = { local_address = "00::2:1", remote_address = "00::2:1", local_cookie = "12345678", remote_cookie = "12345678", default_gateway_MAC = "a1:b2:c3:d4:e5:f6" - } - ]] -- should be symmetric for local "loop-back" test + } -- should be symmetric for local "loop-back" test buffer.preallocate(10000) local c = config.new() diff --git a/src/core/config.lua b/src/core/config.lua index 29e773150f..5e0ecc2064 100644 --- a/src/core/config.lua +++ b/src/core/config.lua @@ -19,7 +19,7 @@ end -- class is the Lua object with a class:new(arg) method to create the app. -- arg is the app's configuration (as a string to be passed to new()). -- --- Example: config.app(c, "nic", Intel82599, [[{pciaddr = "0000:00:01.00"}]]) +-- Example: config.app(c, "nic", Intel82599, {pciaddr = "0000:00:01.00"}) function app (config, name, class, arg) arg = arg or "nil" assert(type(name) == "string", "name must be a string") diff --git a/src/designs/csv/csv b/src/designs/csv/csv index f12241191a..a10bd66586 100755 --- a/src/designs/csv/csv +++ b/src/designs/csv/csv @@ -35,8 +35,8 @@ local v6_rules = [[ config.app(c, "pcap", pcap.PcapReader, "apps/packet_filter/samples/v6.pcap") config.app(c, "repeat", basic_apps.Repeater) config.app(c, "limit", rate_limiter.RateLimiter, [[ {rate = 1e8, bucket_capacity = 1e9} ]]) -config.app(c, "nic1", Intel82599, [[{pciaddr = "0000:05:00.1"}]]) -config.app(c, "nic2", Intel82599, [[{pciaddr = "0000:8a:00.1"}]]) +config.app(c, "nic1", Intel82599, {pciaddr = "0000:05:00.1"}) +config.app(c, "nic2", Intel82599, {pciaddr = "0000:8a:00.1"}) config.app(c, "filter", packet_filter.PacketFilter, v6_rules) config.app(c, "tee", basic_apps.Tee) config.app(c, "sink1", basic_apps.Sink) diff --git a/src/designs/nfv/nfv b/src/designs/nfv/nfv index 4927c29ae8..ed7282c5ed 100755 --- a/src/designs/nfv/nfv +++ b/src/designs/nfv/nfv @@ -20,7 +20,7 @@ function run (npackets) pci.unbind_device_from_linux(pciaddr) local c = config.new() config.app(c, "vm", vhost_user.VhostUser, {socket_path=socket, is_server=server}) - config.app(c, "nic", intel_app.Intel82599, ([[{pciaddr='%s'}]]):format(pciaddr)) + config.app(c, "nic", intel_app.Intel82599, {pciaddr=pciaddr}) if not trace then print("No trace file ($NFV_TRACE) configured.") config.link(c, "vm.tx -> nic.rx") diff --git a/src/lib/nfv/config.lua b/src/lib/nfv/config.lua index 1b6c956978..1beaa364b3 100644 --- a/src/lib/nfv/config.lua +++ b/src/lib/nfv/config.lua @@ -25,11 +25,10 @@ function load (file, pciaddr, sockpath) local name = port_id:gsub("-", "_") local NIC = "NIC_"..name local Virtio = "Virtio_"..name - config.app(c, NIC, Intel82599, - ([[{pciaddr = %q, - vmdq=true, - macaddr = %q, - vlan=%d}]]):format(pciaddr, mac_address, vlan)) + config.app(c, NIC, Intel82599, {pciaddr = pciaddr, + vmdq = true, + macaddr = mac_address, + vlan = vlan}) config.app(c, Virtio, VhostUser, {socket_path=sockpath:format(port_id)}) local VM_rx, VM_tx = Virtio..".rx", Virtio..".tx" if t.ingress_filter then @@ -46,14 +45,11 @@ function load (file, pciaddr, sockpath) end if t.tunnel and t.tunnel.type == "L2TPv3" then local Tunnel = "Tunnel_"..name - local conf = (([[{local_address = %q, - remote_address = %q, - local_cookie = %q, - remote_cookie = %q, - local_session = %q,}]]) - :format(t.tunnel.local_ip, t.tunnel.remote_ip, - t.tunnel.local_cookie, t.tunnel.remote_cookie, - t.tunnel.session)) + local conf = {local_address = t.tunnel.local_ip, + remote_address = t.tunnel.remote_ip, + local_cookie = t.tunnel.local_cookie, + remote_cookie = t.tunnel.remote_cookie, + local_session = t.tunnel.session} config.app(c, Tunnel, L2TPv3, conf) -- Setup IPv6 neighbor discovery/solicitation responder. -- This will talk to our local gateway. @@ -73,7 +69,7 @@ function load (file, pciaddr, sockpath) if policing and t.gbps then local QoS = "QoS_"..name local rate = t.gbps * 1000000 / 8 - config.app(c, QoS, RateLimiter, ([[{rate = %d, bucket_capacity = %d}]]):format(rate, rate)) + config.app(c, QoS, RateLimiter, {rate = rate, bucket_capacity = rate}) config.link(c, VM_tx.." -> "..QoS..".rx") VM_tx = QoS..".tx" end diff --git a/src/test_fixtures/nfvconfig/switch_filter/x b/src/test_fixtures/nfvconfig/switch_filter/x index e7db5a30cb..d0da22fe1c 100644 --- a/src/test_fixtures/nfvconfig/switch_filter/x +++ b/src/test_fixtures/nfvconfig/switch_filter/x @@ -2,7 +2,10 @@ return { { vlan = 0, mac_address = "00:90:f5:ef:aa:67", port_id = "id0", - ingress_filter = [[{ { ethertype='ipv6', protocol='udp', dest_port_min=1024, dest_port_max=1025, }, }]], + ingress_filter = { { ethertype='ipv6', + protocol='udp', + dest_port_min=1024, + dest_port_max=1025 } }, gbps = nil, tunnel = nil }, diff --git a/src/test_fixtures/nfvconfig/switch_filter/y b/src/test_fixtures/nfvconfig/switch_filter/y index 471962f2f1..b81f130882 100644 --- a/src/test_fixtures/nfvconfig/switch_filter/y +++ b/src/test_fixtures/nfvconfig/switch_filter/y @@ -2,7 +2,10 @@ return { { vlan = 0, mac_address = "00:90:f5:ef:aa:67", port_id = "id0", - ingress_filter = [[{ { ethertype='ipv4', protocol='tcp', dest_port_min=24, dest_port_max=25, }, }]], + ingress_filter = { { ethertype='ipv4', + protocol='tcp', + dest_port_min=24, + dest_port_max=25 } }, gbps = nil, tunnel = nil }, diff --git a/src/test_fixtures/nfvconfig/test_functions/filter.ports b/src/test_fixtures/nfvconfig/test_functions/filter.ports index 23f4943a0c..0571712e11 100644 --- a/src/test_fixtures/nfvconfig/test_functions/filter.ports +++ b/src/test_fixtures/nfvconfig/test_functions/filter.ports @@ -9,11 +9,11 @@ return { { vlan = 0, mac_address = "52:54:00:00:00:01", port_id = "B", - ingress_filter = [[{ { ethertype='ipv6', - protocol='icmp' }, - { ethertype='ipv6', - protocol='tcp', - dest_port_min=12345 } }]], + ingress_filter = { { ethertype='ipv6', + protocol='icmp' }, + { ethertype='ipv6', + protocol='tcp', + dest_port_min=12345 } }, gbps = nil, tunnel = nil }, From 6514ce3e3a93c4530236cbbdd4bcc86f3c4e0a6c Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 29 Oct 2014 14:31:42 +0100 Subject: [PATCH 4/4] [core.config] Remove type checking of `arg' in `config.app()'. (It's done in `parse_app_arg'.) --- src/core/config.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/core/config.lua b/src/core/config.lua index 5e0ecc2064..f5e77eecea 100644 --- a/src/core/config.lua +++ b/src/core/config.lua @@ -17,15 +17,13 @@ end -- c is a config object. -- name is the name of this app in the network (a string). -- class is the Lua object with a class:new(arg) method to create the app. --- arg is the app's configuration (as a string to be passed to new()). +-- arg is the app's configuration (to be passed to new()). -- -- Example: config.app(c, "nic", Intel82599, {pciaddr = "0000:00:01.00"}) function app (config, name, class, arg) arg = arg or "nil" assert(type(name) == "string", "name must be a string") assert(type(class) == "table", "class must be a table") - assert(type(arg) == "string" or type(arg) == "table", - "arg must be a string or a table") config.apps[name] = { class = class, arg = arg} end