From d6dd62c4962f5112c615824dd2d5487c2a542b63 Mon Sep 17 00:00:00 2001 From: Javier Guerra Date: Wed, 10 Jun 2015 23:42:36 -0500 Subject: [PATCH] remove mixed tabs/space --- src/apps/basic/basic_apps.lua | 12 +- src/apps/bridge/base.lua | 16 +- src/apps/bridge/flooding.lua | 16 +- src/apps/bridge/learning.lua | 86 +++++------ src/apps/intel/intel10g.lua | 128 ++++++++-------- src/apps/intel/intel_app.lua | 4 +- src/apps/intel/loadgen.lua | 4 +- src/apps/ipv6/nd_light.lua | 84 +++++------ src/apps/ipv6/ns_responder.lua | 20 +-- src/apps/keyed_ipv6_tunnel/tunnel.lua | 6 +- src/apps/pcap/pcap.lua | 2 +- src/apps/socket/raw.lua | 20 +-- src/apps/vhost/vhost_user.lua | 14 +- src/apps/vpn/vpws.lua | 126 ++++++++-------- src/core/app.lua | 44 +++--- src/core/lib.lua | 48 +++--- src/core/timer.lua | 2 +- src/lib/bloom_filter.lua | 52 +++---- src/lib/hardware/register.lua | 6 +- src/lib/hash/base.lua | 46 +++--- src/lib/hash/murmur.lua | 156 ++++++++++---------- src/lib/ipc/shmem/mib.lua | 80 +++++----- src/lib/ipc/shmem/shmem.lua | 118 +++++++-------- src/lib/json.lua | 16 +- src/lib/lua/alt_getopt.lua | 112 +++++++------- src/lib/protocol/datagram.lua | 14 +- src/lib/protocol/ethernet.lua | 6 +- src/lib/protocol/gre.lua | 30 ++-- src/lib/protocol/gre_csum.lua | 8 +- src/lib/protocol/gre_csum_key.lua | 10 +- src/lib/protocol/gre_key.lua | 6 +- src/lib/protocol/header.lua | 8 +- src/lib/protocol/icmp/header.lua | 12 +- src/lib/protocol/icmp/nd/na.lua | 4 +- src/lib/protocol/icmp/nd/ns.lua | 6 +- src/lib/protocol/icmp/nd/options/lladdr.lua | 2 +- src/lib/protocol/icmp/nd/options/tlv.lua | 8 +- src/lib/protocol/ipv4.lua | 2 +- src/lib/protocol/ipv6.lua | 24 +-- src/lib/protocol/keyed_ipv6_tunnel.lua | 38 ++--- src/lib/protocol/tcp.lua | 2 +- src/lib/protocol/udp.lua | 2 +- src/lib/virtio/net_device.lua | 22 +-- src/program/snabbnfv/traffic/traffic.lua | 26 ++-- src/program/snsh/snsh.lua | 26 ++-- 45 files changed, 737 insertions(+), 737 deletions(-) diff --git a/src/apps/basic/basic_apps.lua b/src/apps/basic/basic_apps.lua index 71f3036e8b..65ec85c695 100644 --- a/src/apps/basic/basic_apps.lua +++ b/src/apps/basic/basic_apps.lua @@ -41,7 +41,7 @@ function Join:new() return setmetatable({}, {__index=Join}) end -function Join:push () +function Join:push () 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)) @@ -105,11 +105,11 @@ function Tee:push () for _ = 1, math.min(link.nreadable(i), maxoutput) do local p = receive(i) maxoutput = maxoutput - 1 - do local output = self.output - for k = 1, #output do - transmit(output[k], k == #output and p or packet.clone(p)) - end - end + do local output = self.output + for k = 1, #output do + transmit(output[k], k == #output and p or packet.clone(p)) + end + end end end end diff --git a/src/apps/bridge/base.lua b/src/apps/bridge/base.lua index 5b5286548f..95f3350ced 100644 --- a/src/apps/bridge/base.lua +++ b/src/apps/bridge/base.lua @@ -12,7 +12,7 @@ -- -- config = { ports = { , , ... }, -- split_horizon_groups = { --- = { , , ...}, +-- = { , , ...}, -- ...}, -- config = { } } -- @@ -60,7 +60,7 @@ function bridge:new (arg) local ports = {} local function add_port(port, group) assert(not ports[port], - self:name()..": duplicate definition of port "..port) + self:name()..": duplicate definition of port "..port) ports[port] = group end for _, port in ipairs(conf.ports) do @@ -68,9 +68,9 @@ function bridge:new (arg) end if conf.split_horizon_groups then for group, ports in pairs(conf.split_horizon_groups) do - for _, port in ipairs(ports) do - add_port(port, group) - end + for _, port in ipairs(ports) do + add_port(port, group) + end end end local src_ports, dst_ports = {}, {} @@ -78,9 +78,9 @@ function bridge:new (arg) table.insert(src_ports, sport) dst_ports[sport] = {} for dport, dgroup in pairs(ports) do - if not (sport == dport or (sgroup ~= '' and sgroup == dgroup)) then - table.insert(dst_ports[sport], dport) - end + if not (sport == dport or (sgroup ~= '' and sgroup == dgroup)) then + table.insert(dst_ports[sport], dport) + end end end o._src_ports = src_ports diff --git a/src/apps/bridge/flooding.lua b/src/apps/bridge/flooding.lua index b5ac2175e5..bc06fe181c 100644 --- a/src/apps/bridge/flooding.lua +++ b/src/apps/bridge/flooding.lua @@ -27,14 +27,14 @@ function bridge:push() local src_port = src_ports[i] local l_in = self.input[src_port] while not empty(l_in) do - local ports = dst_ports[src_port] - local p = receive(l_in) - transmit(output[ports[1]], p) - local j = 2 - while ports[j] do - transmit(output[ports[j]], clone(p)) - j = j + 1 - end + local ports = dst_ports[src_port] + local p = receive(l_in) + transmit(output[ports[1]], p) + local j = 2 + while ports[j] do + transmit(output[ports[j]], clone(p)) + j = j + 1 + end end i = i + 1 end diff --git a/src/apps/bridge/learning.lua b/src/apps/bridge/learning.lua index 0950260c64..589bfba8c1 100644 --- a/src/apps/bridge/learning.lua +++ b/src/apps/bridge/learning.lua @@ -2,7 +2,7 @@ -- bridge" using a Bloom filter (provided by lib.bloom_filter) to -- store the set of MAC source addresses of packets arriving on each -- port. --- +-- -- Two Bloom storage cells called mac_table and mac_shadow are -- allocated for each port connected to the bridge. For each packet -- arriving on a port, the MAC source address is stored in both cells. @@ -66,14 +66,14 @@ bridge = subClass(bridge_base) bridge._name = "learning bridge" local default_config = { mac_table_size = 1000, fp_rate = 0.001, - timeout = 60, verbose = false } + timeout = 60, verbose = false } function bridge:new (arg) local o = bridge:superClass().new(self, arg) local conf = o._conf for k, v in pairs(default_config) do if not conf[k] then - conf[k] = v + conf[k] = v end end local bf = bloom:new(conf.mac_table_size, conf.fp_rate) @@ -84,30 +84,30 @@ function bridge:new (arg) o._filters = {} for _, port in ipairs(o._src_ports) do o._filters[port] = { mac_table = bf:cell_new(), - mac_shadow = bf:cell_new(), - mac_address = bf:item_new() - } + mac_shadow = bf:cell_new(), + mac_address = bf:item_new() + } end o._eth_dst = bf:item_new() timer.activate(timer.new("mac_learn_timeout", - function (t) - if conf.verbose then - print("MAC learning timeout") - print("Table usage per port:") - end - for port, filter in pairs(o._filters) do - bf:cell_copy(filter.mac_shadow, filter.mac_table) - bf:cell_clear(filter.mac_shadow) - if conf.verbose then - print(string.format("\t%s: %02.2f%%", port, - 100*bf:cell_usage(filter.mac_table))) - end - end - end, - conf.timeout *1e9, 'repeating') - ) + function (t) + if conf.verbose then + print("MAC learning timeout") + print("Table usage per port:") + end + for port, filter in pairs(o._filters) do + bf:cell_copy(filter.mac_shadow, filter.mac_table) + bf:cell_clear(filter.mac_shadow) + if conf.verbose then + print(string.format("\t%s: %02.2f%%", port, + 100*bf:cell_usage(filter.mac_table))) + end + end + end, + conf.timeout *1e9, 'repeating') + ) -- Caches for various cdata pointer objects to avoid boxing in the -- push() loop @@ -141,7 +141,7 @@ function bridge:push() mem[0] = packet.data(p[0]) local is_mcast = ethernet:is_mcast(mem[0]) if not is_mcast then - bf:store_value(mem, 6, eth_dst) + bf:store_value(mem, 6, eth_dst) end -- Store the source MAC address in the active and shadow @@ -151,32 +151,32 @@ function bridge:push() mem[0] = mem[0] + 6 bf:store_value(mem, 6, mac_address, filter.mac_table) bf:store_item(mac_address, filter.mac_shadow) - + local ports = dst_ports[src_port] local copy = false local j = 1 while ports[j] do - local dst_port = ports[j] - if is_mcast or bf:check_item(eth_dst, filters[dst_port].mac_table) then - if not copy then - transmit(self.output[dst_port], p[0]) - copy = true - else - transmit(self.output[dst_port], clone(p[0])) - end - end - j = j + 1 + local dst_port = ports[j] + if is_mcast or bf:check_item(eth_dst, filters[dst_port].mac_table) then + if not copy then + transmit(self.output[dst_port], p[0]) + copy = true + else + transmit(self.output[dst_port], clone(p[0])) + end + end + j = j + 1 end if not copy then - -- The source MAC address is unknown, flood the packet to - -- all ports - local output = self.output - transmit(output[ports[1]], p[0]) - local j = 2 - while ports[j] do - transmit(output[ports[j]], clone(p[0])) - j = j + 1 - end + -- The source MAC address is unknown, flood the packet to + -- all ports + local output = self.output + transmit(output[ports[1]], p[0]) + local j = 2 + while ports[j] do + transmit(output[ports[j]], clone(p[0])) + j = j + 1 + end end end -- of while not empty(l_in) if self._port_index == self._nsrc_ports then diff --git a/src/apps/intel/intel10g.lua b/src/apps/intel/intel10g.lua index 084f7e18f5..98469e0dcb 100644 --- a/src/apps/intel/intel10g.lua +++ b/src/apps/intel/intel10g.lua @@ -188,7 +188,7 @@ function M_sf:init_snmp () -- of the ifIndex is delegated to the SNMP agent via the name of -- the interface in ifDescr (currently the PCI address). local ifTable = mib:new({ directory = self.snmp.directory or nil, - filename = self.pciaddress }) + filename = self.pciaddress }) self.snmp.ifTable = ifTable -- ifTable ifTable:register('ifDescr', 'OctetStr', self.pciaddress) @@ -200,12 +200,12 @@ function M_sf:init_snmp () -- the receive address register #0 from the EEPROM local ral, rah = self.r.RAL[0](), self.r.RAH[0]() assert(bit.band(rah, bits({ AV = 31 })) == bits({ AV = 31 }), - "MAC address on "..self.pciaddress.." is not valid ") + "MAC address on "..self.pciaddress.." is not valid ") local mac = ffi.new("struct { uint32_t lo; uint16_t hi; }") mac.lo = ral mac.hi = bit.band(rah, 0xFFFF) ifTable:register('ifPhysAddress', { type = 'OctetStr', length = 6 }, - ffi.string(mac, 6)) + ffi.string(mac, 6)) ifTable:register('ifAdminStatus', 'Integer32', 1) -- up ifTable:register('ifOperStatus', 'Integer32', 2) -- down ifTable:register('ifLastChange', 'TimeTicks', 0) @@ -221,7 +221,7 @@ function M_sf:init_snmp () ifTable:register('ifOutErrors', 'Counter32', 0) -- TBD -- ifXTable ifTable:register('ifName', { type = 'OctetStr', length = 255 }, - self.pciaddress) + self.pciaddress) ifTable:register('ifInMulticastPkts', 'Counter32', 0) ifTable:register('ifInBroadcastPkts', 'Counter32', 0) ifTable:register('ifOutMulticastPkts', 'Counter32', 0) @@ -239,7 +239,7 @@ function M_sf:init_snmp () ifTable:register('ifPromiscuousMode', 'Integer32', 2) -- false ifTable:register('ifConnectorPresent', 'Integer32', 1) -- true ifTable:register('ifAlias', { type = 'OctetStr', length = 64 }, - self.pciaddress) -- TBD add description + self.pciaddress) -- TBD add description ifTable:register('ifCounterDiscontinuityTime', 'TimeTicks', 0) -- TBD ifTable:register('_X_ifCounterDiscontinuityTime', 'Counter64', 0) -- TBD @@ -268,66 +268,66 @@ function M_sf:init_snmp () end local function read_registers() for _, k in ipairs(r_keys) do - r[k].v[0] = r[k].r() + r[k].v[0] = r[k].r() end end local t = timer.new("Interface "..self.pciaddress.." status checker", - function(t) - local old = ifTable:get('ifOperStatus') - local new = 1 - if band(self.r.LINKS(), mask) ~= mask then - new = 2 - end - if old ~= new then - print("Interface "..self.pciaddress.. - " status change: "..status[old].. - " => "..status[new]) - ifTable:set('ifOperStatus', new) - ifTable:set('ifLastChange', 0) - ifTable:set('_X_ifLastChange_TicksBase', - C.get_unix_time()) - end - - ifTable:set('ifPromiscuousMode', - (bit.band(self.r.FCTRL(), promisc) ~= 0ULL - and 1) or 2) - -- Update counters - read_registers() - ifTable:set('ifHCInMulticastPkts', r.in_mcast_pkts.v[0]) - ifTable:set('ifInMulticastPkts', r.in_mcast_pkts.v[0]) - ifTable:set('ifHCInBroadcastPkts', r.in_bcast_pkts.v[0]) - ifTable:set('ifInBroadcastPkts', r.in_bcast_pkts.v[0]) - local in_ucast_pkts = r.in_pkts.v[0] - r.in_bcast_pkts.v[0] - - r.in_mcast_pkts.v[0] - ifTable:set('ifHCInUcastPkts', in_ucast_pkts) - ifTable:set('ifInUcastPkts', in_ucast_pkts) - ifTable:set('ifHCInOctets', r.in_octets64.v[0]) - ifTable:set('ifInOctets', r.in_octets64.v[0]) - - ifTable:set('ifHCOutMulticastPkts', r.out_mcast_pkts.v[0]) - ifTable:set('ifOutMulticastPkts', r.out_mcast_pkts.v[0]) - ifTable:set('ifHCOutBroadcastPkts', r.out_bcast_pkts.v[0]) - ifTable:set('ifOutBroadcastPkts', r.out_bcast_pkts.v[0]) - local out_ucast_pkts = r.out_pkts.v[0] - r.out_bcast_pkts.v[0] - - r.out_mcast_pkts.v[0] - ifTable:set('ifHCOutUcastPkts', out_ucast_pkts) - ifTable:set('ifOutUcastPkts', out_ucast_pkts) - ifTable:set('ifHCOutOctets', r.out_octets64.v[0]) - ifTable:set('ifOutOctets', r.out_octets64.v[0]) - - -- The RX receive drop counts are only - -- available through the RX stats register. - -- We only read stats register #0 here. See comment - -- in init_statistics() - ifTable:set('ifInDiscards', self.qs.QPRDC[0]()) - - ifTable:set('ifInErrors', self.s.CRCERRS() + - self.s.ILLERRC() + self.s.ERRBC() + - self.s.RUC() + self.s.RFC() + - self.s.ROC() + self.s.RJC()) - end, - 1e9 * (self.snmp.status_timer or - default.snmp.status_timer), 'repeating') + function(t) + local old = ifTable:get('ifOperStatus') + local new = 1 + if band(self.r.LINKS(), mask) ~= mask then + new = 2 + end + if old ~= new then + print("Interface "..self.pciaddress.. + " status change: "..status[old].. + " => "..status[new]) + ifTable:set('ifOperStatus', new) + ifTable:set('ifLastChange', 0) + ifTable:set('_X_ifLastChange_TicksBase', + C.get_unix_time()) + end + + ifTable:set('ifPromiscuousMode', + (bit.band(self.r.FCTRL(), promisc) ~= 0ULL + and 1) or 2) + -- Update counters + read_registers() + ifTable:set('ifHCInMulticastPkts', r.in_mcast_pkts.v[0]) + ifTable:set('ifInMulticastPkts', r.in_mcast_pkts.v[0]) + ifTable:set('ifHCInBroadcastPkts', r.in_bcast_pkts.v[0]) + ifTable:set('ifInBroadcastPkts', r.in_bcast_pkts.v[0]) + local in_ucast_pkts = r.in_pkts.v[0] - r.in_bcast_pkts.v[0] + - r.in_mcast_pkts.v[0] + ifTable:set('ifHCInUcastPkts', in_ucast_pkts) + ifTable:set('ifInUcastPkts', in_ucast_pkts) + ifTable:set('ifHCInOctets', r.in_octets64.v[0]) + ifTable:set('ifInOctets', r.in_octets64.v[0]) + + ifTable:set('ifHCOutMulticastPkts', r.out_mcast_pkts.v[0]) + ifTable:set('ifOutMulticastPkts', r.out_mcast_pkts.v[0]) + ifTable:set('ifHCOutBroadcastPkts', r.out_bcast_pkts.v[0]) + ifTable:set('ifOutBroadcastPkts', r.out_bcast_pkts.v[0]) + local out_ucast_pkts = r.out_pkts.v[0] - r.out_bcast_pkts.v[0] + - r.out_mcast_pkts.v[0] + ifTable:set('ifHCOutUcastPkts', out_ucast_pkts) + ifTable:set('ifOutUcastPkts', out_ucast_pkts) + ifTable:set('ifHCOutOctets', r.out_octets64.v[0]) + ifTable:set('ifOutOctets', r.out_octets64.v[0]) + + -- The RX receive drop counts are only + -- available through the RX stats register. + -- We only read stats register #0 here. See comment + -- in init_statistics() + ifTable:set('ifInDiscards', self.qs.QPRDC[0]()) + + ifTable:set('ifInErrors', self.s.CRCERRS() + + self.s.ILLERRC() + self.s.ERRBC() + + self.s.RUC() + self.s.RFC() + + self.s.ROC() + self.s.RJC()) + end, + 1e9 * (self.snmp.status_timer or + default.snmp.status_timer), 'repeating') timer.activate(t) return self end @@ -439,8 +439,8 @@ function M_sf:transmit (p) -- agreement on this strategy is reached. -- if p.length > self.mtu then -- if self.snmp then - -- local errors = self.snmp.ifTable:ptr('ifOutDiscards') - -- errors[0] = errors[0] + 1 + -- local errors = self.snmp.ifTable:ptr('ifOutDiscards') + -- errors[0] = errors[0] + 1 -- end -- packet.free(p) -- else diff --git a/src/apps/intel/intel_app.lua b/src/apps/intel/intel_app.lua index 593aa9b927..cea9efecee 100644 --- a/src/apps/intel/intel_app.lua +++ b/src/apps/intel/intel_app.lua @@ -106,8 +106,8 @@ function Intel82599:push () if l == nil then return end while not empty(l) and self.dev:can_transmit() do do local p = receive(l) - self.dev:transmit(p) - --packet.deref(p) + self.dev:transmit(p) + --packet.deref(p) end end self.dev:sync_transmit() diff --git a/src/apps/intel/loadgen.lua b/src/apps/intel/loadgen.lua index e15e78e0a7..df82549320 100644 --- a/src/apps/intel/loadgen.lua +++ b/src/apps/intel/loadgen.lua @@ -49,8 +49,8 @@ function LoadGen:push () if self.input.input then while not link.empty(self.input.input) and can_transmit(self.dev) do do local p = receive(self.input.input) - transmit(self.dev, p) - end + transmit(self.dev, p) + end end end end diff --git a/src/apps/ipv6/nd_light.lua b/src/apps/ipv6/nd_light.lua index 4502a9c089..bee0115670 100644 --- a/src/apps/ipv6/nd_light.lua +++ b/src/apps/ipv6/nd_light.lua @@ -69,8 +69,8 @@ local function check_ip_address(ip, desc) ip = ipv6:pton(ip) else assert(type(ip) == "cdata", - "nd_light: invalid type of "..desc.." IP address, expected cdata, got " - ..type(ip)) + "nd_light: invalid type of "..desc.." IP address, expected cdata, got " + ..type(ip)) end return ip end @@ -85,19 +85,19 @@ function nd_light:new (arg) conf.local_mac = ethernet:pton(conf.local_mac) else assert(type(conf.local_mac) == "cdata", - "nd_light: invalid type for local MAC address, expected cdata, got " - ..type(conf.local_mac)) + "nd_light: invalid type for local MAC address, expected cdata, got " + ..type(conf.local_mac)) end conf.local_ip = check_ip_address(conf.local_ip, "local") conf.next_hop = check_ip_address(conf.next_hop, "next-hop") o._config = conf o._match_ns = function(ns) - return(ns:target_eq(conf.local_ip)) - end + return(ns:target_eq(conf.local_ip)) + end o._match_na = function(na) - return(na:target_eq(conf.next_hop) and na:solicited()) - end + return(na:target_eq(conf.next_hop) and na:solicited()) + end local errmsg o._filter, errmsg = filter:new("icmp6 and ( ip6[40] = 135 or ip6[40] = 136 )") assert(o._filter, errmsg and ffi.string(errmsg)) @@ -108,9 +108,9 @@ function nd_light:new (arg) nh.packet = dgram:packet() local sol_node_mcast = ipv6:solicited_node_mcast(conf.next_hop) local ipv6 = ipv6:new({ next_header = 58, -- ICMP6 - hop_limit = 255, - src = conf.local_ip, - dst = sol_node_mcast }) + hop_limit = 255, + src = conf.local_ip, + dst = sol_node_mcast }) local icmp = icmp:new(135, 0) -- Construct a neighbor solicitation with a source link-layer @@ -128,27 +128,27 @@ function nd_light:new (arg) ipv6:payload_length(icmp:sizeof() + ns:sizeof() + src_lladdr_tlv_len) dgram:push(ipv6) dgram:push(ethernet:new({ src = conf.local_mac, - dst = ethernet:ipv6_mcast(sol_node_mcast), - type = 0x86dd })) + dst = ethernet:ipv6_mcast(sol_node_mcast), + type = 0x86dd })) dgram:free() -- Timer for retransmits of neighbor solicitations nh.timer_cb = function (t) - local nh = o._next_hop - print(string.format("Sending neighbor solicitation for next-hop %s", - ipv6:ntop(conf.next_hop))) - link.transmit(o.output.south, packet.clone(nh.packet)) - nh.nsent = nh.nsent + 1 - if (not o._config.retrans or nh.nsent <= o._config.retrans) + local nh = o._next_hop + print(string.format("Sending neighbor solicitation for next-hop %s", + ipv6:ntop(conf.next_hop))) + link.transmit(o.output.south, packet.clone(nh.packet)) + nh.nsent = nh.nsent + 1 + if (not o._config.retrans or nh.nsent <= o._config.retrans) and not o._eth_header then timer.activate(nh.timer) - end - if o._config.retrans and nh.nsent > o._config.retrans then - error(string.format("ND for next hop %s has failed", - ipv6:ntop(conf.next_hop))) - end - end + end + if o._config.retrans and nh.nsent > o._config.retrans then + error(string.format("ND for next hop %s has failed", + ipv6:ntop(conf.next_hop))) + end + end nh.timer = timer.new("ns retransmit", nh.timer_cb, 1e6 * conf.delay) o._next_hop = nh @@ -159,8 +159,8 @@ function nd_light:new (arg) -- Leave dst address unspecified. It will be set to the source of -- the incoming solicitation ipv6 = ipv6:new({ next_header = 58, -- ICMP6 - hop_limit = 255, - src = conf.local_ip }) + hop_limit = 255, + src = conf.local_ip }) icmp = icmp:new(136, 0) -- Construct a neighbor solicitation with a target link-layer -- option. @@ -237,10 +237,10 @@ local function na (self, dgram, eth, ipv6, icmp) return nil end self._eth_header = ethernet:new({ src = self._config.local_mac, - dst = option[1]:option():addr(), - type = 0x86dd }) + dst = option[1]:option():addr(), + type = 0x86dd }) print(string.format("Resolved next-hop %s to %s", ipv6:ntop(self._config.next_hop), - ethernet:ntop(option[1]:option():addr()))) + ethernet:ntop(option[1]:option():addr()))) return nil end @@ -281,15 +281,15 @@ function nd_light:push () p[0] = link.receive(l_in) local status = from_south(self, p) if status == nil then - -- Discard - packet.free(p[0]) + -- Discard + packet.free(p[0]) elseif status == true then - -- Send NA back south - packet.free(p[0]) - link.transmit(l_reply, packet.clone(self._sna.packet)) + -- Send NA back south + packet.free(p[0]) + link.transmit(l_reply, packet.clone(self._sna.packet)) else - -- Send transit traffic up north - link.transmit(l_out, p[0]) + -- Send transit traffic up north + link.transmit(l_out, p[0]) end end @@ -297,12 +297,12 @@ function nd_light:push () l_out = self.output.south while not link.empty(l_in) and not link.full(l_out) do if not self._eth_header then - -- Drop packets until ND for the next-hop - -- has completed. - packet.free(link.receive(l_in)) + -- Drop packets until ND for the next-hop + -- has completed. + packet.free(link.receive(l_in)) else - local p = cache.p - p[0] = link.receive(l_in) + local p = cache.p + p[0] = link.receive(l_in) if packet.length(p[0]) >= self._eth_header:sizeof() then self._eth_header:copy(packet.data(p[0])) link.transmit(l_out, p[0]) diff --git a/src/apps/ipv6/ns_responder.lua b/src/apps/ipv6/ns_responder.lua index 8c004d08bb..c4dba3797b 100644 --- a/src/apps/ipv6/ns_responder.lua +++ b/src/apps/ipv6/ns_responder.lua @@ -23,8 +23,8 @@ function ns_responder:new(config) local o = ns_responder:superClass().new(self) o._config = config o._match_ns = function(ns) - return(ns:target_eq(config.local_ip)) - end + return(ns:target_eq(config.local_ip)) + end local filter, errmsg = filter:new("icmp6 and ip6[40] = 135") assert(filter, errmsg and ffi.string(errmsg)) o._filter = filter @@ -87,8 +87,8 @@ function ns_responder:push() local l_out = self.output.south if l_in and l_out then while not link.empty(l_in) and not link.full(l_out) do - -- Pass everything on north -> south - link.transmit(l_out, link.receive(l_in)) + -- Pass everything on north -> south + link.transmit(l_out, link.receive(l_in)) end end l_in = self.input.south @@ -98,14 +98,14 @@ function ns_responder:push() local p = link.receive(l_in) local status = process(self, p) if status == nil then - -- Discard - packet.free(p) + -- Discard + packet.free(p) elseif status == true then - -- Send NA back south - link.transmit(l_reply, p) + -- Send NA back south + link.transmit(l_reply, p) else - -- Send transit traffic up north - link.transmit(l_out, p) + -- Send transit traffic up north + link.transmit(l_out, p) end end end diff --git a/src/apps/keyed_ipv6_tunnel/tunnel.lua b/src/apps/keyed_ipv6_tunnel/tunnel.lua index 55279552be..9bf92eafa5 100644 --- a/src/apps/keyed_ipv6_tunnel/tunnel.lua +++ b/src/apps/keyed_ipv6_tunnel/tunnel.lua @@ -149,7 +149,7 @@ function SimpleKeyedTunnel:new (arg) local psession = ffi.cast(psession_id_ctype, header + SESSION_ID_OFFSET) psession[0] = lib.htonl(conf.local_session) end - + if conf.default_gateway_MAC then local mac = assert(macaddress:new(conf.default_gateway_MAC)) ffi.copy(header + DST_MAC_OFFSET, mac.bytes, 6) @@ -157,7 +157,7 @@ function SimpleKeyedTunnel:new (arg) if conf.hop_limit then assert(type(conf.hop_limit) == 'number' and - conf.hop_limit <= 255, "invalid hop limit") + conf.hop_limit <= 255, "invalid hop limit") header[HOP_LIMIT_OFFSET] = conf.hop_limit end @@ -280,7 +280,7 @@ function selftest () print("run simple one second benchmark ...") app.main({duration = 1}) - + if not ok then print("selftest failed") os.exit(1) diff --git a/src/apps/pcap/pcap.lua b/src/apps/pcap/pcap.lua index 270f5bbb8f..579bdeb260 100644 --- a/src/apps/pcap/pcap.lua +++ b/src/apps/pcap/pcap.lua @@ -12,7 +12,7 @@ PcapReader = {} function PcapReader:new (filename) local records = pcap.records(filename) return setmetatable({iterator = records, done = false}, - {__index = PcapReader}) + {__index = PcapReader}) end function PcapReader:pull () diff --git a/src/apps/socket/raw.lua b/src/apps/socket/raw.lua index 78155f21ca..4442af34d3 100644 --- a/src/apps/socket/raw.lua +++ b/src/apps/socket/raw.lua @@ -44,9 +44,9 @@ function selftest () local dst = ethernet:pton("00:00:00:00:00:02") local localhost = ipv6:pton("0:0:0:0:0:0:0:1") dg_tx:push(ipv6:new({src = localhost, - dst = localhost, - next_header = 59, -- no next header - hop_limit = 1})) + dst = localhost, + next_header = 59, -- no next header + hop_limit = 1})) dg_tx:push(ethernet:new({src = src, dst = dst, type = 0x86dd})) local link = require("core.link") @@ -58,13 +58,13 @@ function selftest () lo:pull() local dg_rx = datagram:new(link.receive(lo.output.tx), ethernet) assert(dg_rx:parse({ { ethernet, function(eth) - return(eth:src_eq(src) and eth:dst_eq(dst) - and eth:type() == 0x86dd) - end }, - { ipv6, function(ipv6) - return(ipv6:src_eq(localhost) and - ipv6:dst_eq(localhost)) - end } }), "loopback test failed") + return(eth:src_eq(src) and eth:dst_eq(dst) + and eth:type() == 0x86dd) + end }, + { ipv6, function(ipv6) + return(ipv6:src_eq(localhost) and + ipv6:dst_eq(localhost)) + end } }), "loopback test failed") -- Another useful test would be to feed a pcap file with -- pings to 127.0.0.1 and ::1 into lo and capture/compare diff --git a/src/apps/vhost/vhost_user.lua b/src/apps/vhost/vhost_user.lua index 36d4da2938..460d7c47ef 100644 --- a/src/apps/vhost/vhost_user.lua +++ b/src/apps/vhost/vhost_user.lua @@ -177,24 +177,24 @@ end function VhostUser:update_features (features) local stat = syscall.stat(self.socket_path) local mtime = ("%d.%d"):format(tonumber(stat.st_mtime), - tonumber(stat.st_mtime_nsec)) + tonumber(stat.st_mtime_nsec)) local cachepath = "/tmp/vhost_features_"..string.gsub(self.socket_path, "/", "__") local f = io.open(cachepath, 'r') if f then local file_features, file_mtime = f:read('*a'):match("features:(.*) mtime:(.*)\n") f:close() if file_mtime == mtime then - print(("vhost_user: Read cached features (0x%s) from %s"):format( - bit.tohex(file_features), cachepath)) - return tonumber(file_features) + print(("vhost_user: Read cached features (0x%s) from %s"):format( + bit.tohex(file_features), cachepath)) + return tonumber(file_features) else - print(("vhost_user: Skipped old feature cache in %s"):format(cachepath)) + print(("vhost_user: Skipped old feature cache in %s"):format(cachepath)) end end f = io.open(cachepath, 'w') if f then print(("vhost_user: Caching features (0x%s) in %s"):format( - bit.tohex(features), cachepath)) + bit.tohex(features), cachepath)) f:write(("features:%s mtime:%s\n"):format("0x"..bit.tohex(features), mtime)) f:close() else @@ -252,7 +252,7 @@ function VhostUser:set_vring_addr (msg) if self.dev:ready() then if not self.vhost_ready then - print("vhost_user: Connected and initialized: "..self.socket_path) + print("vhost_user: Connected and initialized: "..self.socket_path) end self.vhost_ready = true end diff --git a/src/apps/vpn/vpws.lua b/src/apps/vpn/vpws.lua index 6f68b2cbb6..d4722839c1 100644 --- a/src/apps/vpn/vpws.lua +++ b/src/apps/vpn/vpws.lua @@ -43,7 +43,7 @@ function vpws:new(arg) o._name = conf.name o._encap = { ipv6 = ipv6:new({ next_header = 47, hop_limit = 64, src = conf.local_vpn_ip, - dst = conf.remote_vpn_ip}), + dst = conf.remote_vpn_ip}), gre = gre:new({ protocol = 0x6558, checksum = conf.checksum, key = conf.label }) } -- Use a dummy value for the destination MAC address in case it is @@ -52,8 +52,8 @@ function vpws:new(arg) -- overwrites the Ethernet header (e.g. the nd_light app) -- accordinly. o._encap.ether = ethernet:new({ src = conf.local_mac, - dst = conf.remote_mac or ethernet:pton('00:00:00:00:00:00'), - type = 0x86dd }) + dst = conf.remote_mac or ethernet:pton('00:00:00:00:00:00'), + type = 0x86dd }) -- Pre-computed size of combined Ethernet and IPv6 header o._eth_ipv6_size = ethernet:sizeof() + ipv6:sizeof() local program = "ip6 and dst host "..ipv6:ntop(conf.local_vpn_ip) .." and ip6 proto 47" @@ -71,59 +71,59 @@ function vpws:push() local l_out = self.output[in_to_out[port_in]] assert(l_out) while not link.full(l_out) and not link.empty(l_in) do - local p = link.receive(l_in) - local datagram = self._dgram:reuse(p, ethernet) - if port_in == 'customer' then - local encap = self._encap - -- Encapsulate Ethernet frame coming in on customer port - -- IPv6 payload length consist of the size of the GRE header plus - -- the size of the original packet - encap.ipv6:payload_length(encap.gre:sizeof() + p.length) - if encap.gre:checksum() then - encap.gre:checksum(datagram:payload()) - end - -- Copy the finished headers into the packet - datagram:push(encap.gre) - datagram:push(encap.ipv6) - datagram:push(encap.ether) - else - -- Check for encapsulated frame coming in on uplink - if self._filter:match(datagram:payload()) then - -- Remove encapsulation to restore the original - -- Ethernet frame - datagram:pop_raw(self._eth_ipv6_size, gre) - local valid = true - local gre = datagram:parse() - if gre then - if not gre:checksum_check(datagram:payload()) then - print(self:name()..": GRE bad checksum") - valid = false - else - local key = gre:key() - if ((self._config.label and key and key == self._config.label) or - not (self._config.label or key)) then - datagram:pop(1) - else - print(self:name()..": GRE key mismatch: local " - ..(self._config.label or 'none')..", remote "..(gre:key() or 'none')) - valid = false - end - end - else - -- Unsupported GRE options or flags - valid = false - end - if not valid then - packet.free(p) - p = nil - end - else - -- Packet doesn't belong to VPN, discard - packet.free(p) - p = nil - end - end - if p then link.transmit(l_out, p) end + local p = link.receive(l_in) + local datagram = self._dgram:reuse(p, ethernet) + if port_in == 'customer' then + local encap = self._encap + -- Encapsulate Ethernet frame coming in on customer port + -- IPv6 payload length consist of the size of the GRE header plus + -- the size of the original packet + encap.ipv6:payload_length(encap.gre:sizeof() + p.length) + if encap.gre:checksum() then + encap.gre:checksum(datagram:payload()) + end + -- Copy the finished headers into the packet + datagram:push(encap.gre) + datagram:push(encap.ipv6) + datagram:push(encap.ether) + else + -- Check for encapsulated frame coming in on uplink + if self._filter:match(datagram:payload()) then + -- Remove encapsulation to restore the original + -- Ethernet frame + datagram:pop_raw(self._eth_ipv6_size, gre) + local valid = true + local gre = datagram:parse() + if gre then + if not gre:checksum_check(datagram:payload()) then + print(self:name()..": GRE bad checksum") + valid = false + else + local key = gre:key() + if ((self._config.label and key and key == self._config.label) or + not (self._config.label or key)) then + datagram:pop(1) + else + print(self:name()..": GRE key mismatch: local " + ..(self._config.label or 'none')..", remote "..(gre:key() or 'none')) + valid = false + end + end + else + -- Unsupported GRE options or flags + valid = false + end + if not valid then + packet.free(p) + p = nil + end + else + -- Packet doesn't belong to VPN, discard + packet.free(p) + p = nil + end + end + if p then link.transmit(l_out, p) end end end end @@ -141,13 +141,13 @@ function selftest() config.app(c, "to_customer", pcap.PcapWriter, "apps/vpn/vpws-selftest-customer.cap.output") config.app(c, "to_uplink", pcap.PcapWriter, "apps/vpn/vpws-selftest-uplink.cap.output") config.app(c, "vpntp", vpws, { name = "vpntp1", - checksum = true, - label = 0x12345678, - local_vpn_ip = local_vpn_ip, - remote_vpn_ip = remote_vpn_ip, - local_ip = local_ip, - local_mac = local_mac, - remote_mac = remote_mac }) + checksum = true, + label = 0x12345678, + local_vpn_ip = local_vpn_ip, + remote_vpn_ip = remote_vpn_ip, + local_ip = local_ip, + local_mac = local_mac, + remote_mac = remote_mac }) config.link(c, "from_uplink.output -> vpntp.uplink") config.link(c, "vpntp.customer -> to_customer.input") config.link(c, "from_customer.output -> vpntp.customer") diff --git a/src/core/app.lua b/src/core/app.lua index c8a967fd47..70397620aa 100644 --- a/src/core/app.lua +++ b/src/core/app.lua @@ -97,7 +97,7 @@ function restart_dead_apps () end -- Configure the running app network to match new_configuration. --- +-- -- Successive calls to configure() will migrate from the old to the -- new app network by making the changes needed. function configure (new_config) @@ -130,7 +130,7 @@ function compute_config_actions (old, new) end for appname in pairs(old.apps) do if not new.apps[appname] then - table.insert(actions['stop'], appname) + table.insert(actions['stop'], appname) end end return actions @@ -185,10 +185,10 @@ function apply_config_actions (actions, conf) -- Dispatch actions in a suitable sequence. for _, action in ipairs({'stop', 'restart', 'keep', 'reconfig', 'start'}) do for _, name in ipairs(actions[action]) do - if log and action ~= 'keep' then - io.write("engine: ", action, " app ", name, "\n") + if log and action ~= 'keep' then + io.write("engine: ", action, " app ", name, "\n") end - ops[action](name) + ops[action](name) end end -- Setup links: create (or reuse) and renumber. @@ -247,10 +247,10 @@ function pace_breathing () nextbreath = math.max(nextbreath + 1/Hz, monotonic_now) else if lastfrees == frees then - sleep = math.min(sleep + 1, maxsleep) - C.usleep(sleep) + sleep = math.min(sleep + 1, maxsleep) + C.usleep(sleep) else - sleep = math.floor(sleep/2) + sleep = math.floor(sleep/2) end lastfrees = frees lastfreebytes = freebytes @@ -268,9 +268,9 @@ function breathe () -- if app.pull then -- zone(app.zone) app:pull() zone() if app.pull and not app.dead then - zone(app.zone) - with_restart(app, app.pull) - zone() + zone(app.zone) + with_restart(app, app.pull) + zone() end end -- Exhale: push work out through the app network @@ -328,12 +328,12 @@ function report_load () local fpb = math.floor(newfrees/newbreaths) local bpp = math.floor(newbytes/newfrees) print(("load: time: %-2.2fs fps: %-9s fpGbps: %-3.3f fpb: %-3s bpp: %-4s sleep: %-4dus"):format( - interval, - lib.comma_value(fps), - fbps / 1e9, - lib.comma_value(fpb), - (bpp ~= bpp) and "-" or tostring(bpp), -- handle NaN - sleep)) + interval, + lib.comma_value(fps), + fbps / 1e9, + lib.comma_value(fpb), + (bpp ~= bpp) and "-" or tostring(bpp), -- handle NaN + sleep)) end lastloadreport = now() reportedfrees = frees @@ -355,8 +355,8 @@ function report_links () for i, name in ipairs(names) do l = link_table[name] print(("%20s sent on %s (loss rate: %d%%)"):format( - lib.comma_value(l.stats.txpackets), - name, loss_rate(l.stats.txdrop, l.stats.txpackets))) + lib.comma_value(l.stats.txpackets), + name, loss_rate(l.stats.txdrop, l.stats.txpackets))) end end @@ -364,10 +364,10 @@ function report_apps () print ("apps report:") for name, app in pairs(app_table) do if app.dead then - print(name, ("[dead: %s]"):format(app.dead.error)) + print(name, ("[dead: %s]"):format(app.dead.error)) elseif app.report then - print(name) - with_restart(app, app.report) + print(name) + with_restart(app, app.report) end end end diff --git a/src/core/lib.lua b/src/core/lib.lua index 9863b03afc..d48f247334 100644 --- a/src/core/lib.lua +++ b/src/core/lib.lua @@ -71,7 +71,7 @@ end function dirname(path) if not path then return path end - + local buf = ffi.new("char[?]", #path+1) ffi.copy(buf, path) local ptr = C.dirname(buf) @@ -80,7 +80,7 @@ end function basename(path) if not path then return path end - + local buf = ffi.new("char[?]", #path+1) ffi.copy(buf, path) local ptr = C.basename(buf) @@ -159,7 +159,7 @@ end function bits (bitset, basevalue) local sum = basevalue or 0 for _,n in pairs(bitset) do - sum = bit.bor(sum, bit.lshift(1, n)) + sum = bit.bor(sum, bit.lshift(1, n)) end return sum end @@ -176,7 +176,7 @@ end -- this would be save, but masking and shifting is guaranteed to be -- portable. Performance could be an issue, though. -local bitfield_endian_conversion = +local bitfield_endian_conversion = { [16] = { ntoh = C.ntohs, hton = C.htons }, [32] = { ntoh = C.ntohl, hton = C.htonl } } @@ -195,9 +195,9 @@ function bitfield(size, struct, member, offset, nbits, value) if value then field = bit.bor(bit.band(field, imask), bit.lshift(value, shift)) if conv then - struct[member] = conv.hton(field) + struct[member] = conv.hton(field) else - struct[member] = field + struct[member] = field end else return bit.rshift(bit.band(field, mask), shift) @@ -260,15 +260,15 @@ function bounds_checked (type, base, offset, size) type = ffi.typeof(type) local tptr = ffi.typeof("$ *", type) local wrap = ffi.metatype(ffi.typeof("struct { $ _ptr; }", tptr), { - __index = function(w, idx) - assert(idx < size) - return w._ptr[idx] - end, - __newindex = function(w, idx, val) - assert(idx < size) - w._ptr[idx] = val - end, - }) + __index = function(w, idx) + assert(idx < size) + return w._ptr[idx] + end, + __newindex = function(w, idx, val) + assert(idx < size) + w._ptr[idx] = val + end, + }) return wrap(ffi.cast(tptr, ffi.cast("uint8_t *", base) + offset)) end @@ -397,10 +397,10 @@ ntohs = htons function dogetopt (args, actions, opts, long_opts) local opts,optind,optarg = getopt.get_ordered_opts(args, opts, long_opts) for i, v in ipairs(opts) do - if actions[v] then - actions[v](optarg[i]) + if actions[v] then + actions[v](optarg[i]) else - error("unimplemented option: " .. v) + error("unimplemented option: " .. v) end end local rest = {} @@ -414,11 +414,11 @@ function have_module (name) return true else for _, searcher in ipairs(package.loaders) do - local loader = searcher(name) - if type(loader) == 'function' then - package.preload[name] = loader - return true - end + local loader = searcher(name) + if type(loader) == 'function' then + package.preload[name] = loader + return true + end end return false end @@ -447,7 +447,7 @@ function selftest () local data = "\x45\x00\x00\x73\x00\x00\x40\x00\x40\x11\xc0\xa8\x00\x01\xc0\xa8\x00\xc7" local cs = csum(data, string.len(data)) assert(cs == 0xb861, "bad checksum: " .. bit.tohex(cs, 4)) - + -- assert(readlink('/etc/rc2.d/S99rc.local') == '../init.d/rc.local', "bad readlink") -- assert(dirname('/etc/rc2.d/S99rc.local') == '/etc/rc2.d', "wrong dirname") -- assert(basename('/etc/rc2.d/S99rc.local') == 'S99rc.local', "wrong basename") diff --git a/src/core/timer.lua b/src/core/timer.lua index b21f2ac63e..d5ac34e15e 100644 --- a/src/core/timer.lua +++ b/src/core/timer.lua @@ -28,7 +28,7 @@ local function call_timers (l) for i=1,#l do local timer = l[i] if debug then - print(string.format("running timer %s at tick %s", timer.name, ticks)) + print(string.format("running timer %s at tick %s", timer.name, ticks)) end timer.fn(timer) if timer.repeating then activate(timer) end diff --git a/src/lib/bloom_filter.lua b/src/lib/bloom_filter.lua index 3f53c5ee8d..260c117918 100644 --- a/src/lib/bloom_filter.lua +++ b/src/lib/bloom_filter.lua @@ -112,7 +112,7 @@ function filter:cell_usage (cell) local width = self._m for i = 0, width-1 do if band(cell[rshift(i, 6)], lshift(1ULL, band(i, 0x3FULL))) ~= 0ULL then - set = set+1 + set = set+1 end end return set/width @@ -194,7 +194,7 @@ function filter:check_value (v, l, cell) for i = 1, self._k do local index = band(h1.u64[0] + i*h2.u64[0], self._mod) if band(cell[rshift(index, 6)], lshift(1ULL, band(index, 0x3FULL))) == 0ULL then - return false + return false end end return true @@ -214,7 +214,7 @@ end local function check_buckets(filter, item, expected) for k, i in ipairs(filter:item_dump(item)) do assert(i == expected[k], "wrong bucket index "..k - .." (expected "..expected[k]..", got "..tostring(i)..")") + .." (expected "..expected[k]..", got "..tostring(i)..")") end end @@ -233,8 +233,8 @@ function selftest() dptr1[0] = data1 dptr2[0] = data2 local expected_buckets = { 149, 986, 1823, 612, 1449, 238, 1075, - 1912, 701, 1538, 327, 1164, 2001, 790, - 1627 } + 1912, 701, 1538, 327, 1164, 2001, 790, + 1627 } f:store_value(dptr1, s1, item, nil) check_buckets(f, item, expected_buckets) @@ -253,7 +253,7 @@ function selftest() data2 = ffi.new("union { uint32_t i; uint8_t b[4]; }") dptr2[0] = data2.b s1, s2 = ffi.sizeof(data1), ffi.sizeof(data2) - + local min, max, step, samples = 50, 150, 10, 20000 data1 = ffi.new("uint32_t [?]", max) data2 = ffi.new("uint32_t [?]", samples) @@ -267,40 +267,40 @@ function selftest() f:cell_clear(cell) local fail = 0 for i = 0, j-1 do - dptr1[0] = ffi.cast("uint8_t *", data1 + i) - f:store_value(dptr1, ffi.sizeof("uint32_t"), item, cell) - assert(f:check_item(item, cell)) - assert(f:check_value(dptr1, s1, cell)) + dptr1[0] = ffi.cast("uint8_t *", data1 + i) + f:store_value(dptr1, ffi.sizeof("uint32_t"), item, cell) + assert(f:check_item(item, cell)) + assert(f:check_value(dptr1, s1, cell)) end local fp = 0 for i = 0, samples-1 do - dptr1[0] = ffi.cast("uint8_t *", data2 + i) - if f:check_value(dptr1, ffi.sizeof("uint32_t"), cell) then - fp = fp+1 - end + dptr1[0] = ffi.cast("uint8_t *", data2 + i) + if f:check_value(dptr1, ffi.sizeof("uint32_t"), cell) then + fp = fp+1 + end end if selftest_config.verbose then - print(string.format("False-positive rate @%d (occupancy %02.2f%%): %.4f, %d", - j, 100*f:cell_usage(cell), fp/samples, fp)) + print(string.format("False-positive rate @%d (occupancy %02.2f%%): %.4f, %d", + j, 100*f:cell_usage(cell), fp/samples, fp)) end if j == 100 then - assert(fp/samples <= 0.001, - "Maximum false-positives rate exceeded, expected 0.1%, got "..fp/samples) + assert(fp/samples <= 0.001, + "Maximum false-positives rate exceeded, expected 0.1%, got "..fp/samples) end end if selftest_config.performance then local function perfloop (iter, desc, call, ...) - jit.flush() - local start = ffi.C.get_time_ns() - for i = 1, iter do - call(...) - end - local stop = ffi.C.get_time_ns() - print(desc..": "..math.floor(iter/(tonumber(stop-start)/1e9)) - .." iterations per second") + jit.flush() + local start = ffi.C.get_time_ns() + for i = 1, iter do + call(...) + end + local stop = ffi.C.get_time_ns() + print(desc..": "..math.floor(iter/(tonumber(stop-start)/1e9)) + .." iterations per second") end print("Bloom filter performance tests") diff --git a/src/lib/hardware/register.lua b/src/lib/hardware/register.lua index 9fde7aeadb..4fe1fc2a30 100644 --- a/src/lib/hardware/register.lua +++ b/src/lib/hardware/register.lua @@ -86,8 +86,8 @@ local mt = { print=Register.printrc}, __call = Register.readrc, __tostring = Register.__tostring}, RC64 = {__index = { read=Register.readrc, reset=Register.reset, - print=Register.printrc}, - __call = Register.readrc, __tostring = Register.__tostring}, + print=Register.printrc}, + __call = Register.readrc, __tostring = Register.__tostring}, } --- Create a register `offset` bytes from `base_ptr`. @@ -102,7 +102,7 @@ function new (name, longname, offset, base_ptr, mode) if mode == 'RC' or mode == 'RC64' then o.acc = ffi.new("uint64_t[1]") if mode == 'RC64' then - o.ptr = ffi.cast("uint64_t*", o.ptr) + o.ptr = ffi.cast("uint64_t*", o.ptr) end end return setmetatable(o, mt) diff --git a/src/lib/hash/base.lua b/src/lib/hash/base.lua index 99741e9984..8666b27170 100644 --- a/src/lib/hash/base.lua +++ b/src/lib/hash/base.lua @@ -21,10 +21,10 @@ -- -- A subclass must implement the method hash(), which must accept at -- least two arguments: --- +-- -- data Pointer to a region of memory where the input is stored -- length Size of input in bytes --- +-- -- The hash() method must store the result in the 'h' instance -- variable. For convenience of the caller, the method must return -- 'h', allowing for direct access like @@ -45,29 +45,29 @@ function hash:new () local h_t if o._size >= 64 and o._size%64 == 0 then h_t = ffi.typeof([[ - union { - uint8_t u8[$]; - int8_t u8[$]; - uint32_t u32[$]; - int32_t i32[$]; - uint64_t u64[$]; - int64_t i64[$]; - } - ]], - o._size/8, o._size/8, - o._size/32, o._size/32, - o._size/64, o._size/64) + union { + uint8_t u8[$]; + int8_t u8[$]; + uint32_t u32[$]; + int32_t i32[$]; + uint64_t u64[$]; + int64_t i64[$]; + } + ]], + o._size/8, o._size/8, + o._size/32, o._size/32, + o._size/64, o._size/64) else h_t = ffi.typeof([[ - union { - uint8_t u8[$]; - int8_t u8[$]; - uint32_t u32[$]; - int32_t i32[$]; - } - ]], - o._size/8, o._size/8, - o._size/32, o._size/32) + union { + uint8_t u8[$]; + int8_t u8[$]; + uint32_t u32[$]; + int32_t i32[$]; + } + ]], + o._size/8, o._size/8, + o._size/32, o._size/32) end o.h = h_t() return o diff --git a/src/lib/hash/murmur.lua b/src/lib/hash/murmur.lua index fcac5cc8d7..4810bc0bb4 100644 --- a/src/lib/hash/murmur.lua +++ b/src/lib/hash/murmur.lua @@ -43,32 +43,32 @@ do local h1 = (seed and seed + 0ULL) or 0ULL if nblocks > 0 then - for i = 0, nblocks-1 do - local k1 = ffi.cast(uint32_ptr_t, data)[i] - k1 = band(k1*c1, max32) - k1 = bxor(lshift(k1, 15), rshift(k1, 17)) - k1 = band(k1*c2, max32) - h1 = bxor(h1, k1) - h1 = bxor(lshift(h1, 13), rshift(h1, 19)) - h1 = band(h1*5 + c5, max32) - end + for i = 0, nblocks-1 do + local k1 = ffi.cast(uint32_ptr_t, data)[i] + k1 = band(k1*c1, max32) + k1 = bxor(lshift(k1, 15), rshift(k1, 17)) + k1 = band(k1*c2, max32) + h1 = bxor(h1, k1) + h1 = bxor(lshift(h1, 13), rshift(h1, 19)) + h1 = band(h1*5 + c5, max32) + end end local tail = ffi.cast(uint8_ptr_t, data) + lshift(nblocks, 2) local k1 = 0ULL local l = band(length, 3) if l > 2 then - k1 = bxor(k1, lshift(tail[2]+0ULL, 16)) + k1 = bxor(k1, lshift(tail[2]+0ULL, 16)) end if l > 1 then - k1 = bxor(k1, lshift(tail[1]+0ULL, 8)) + k1 = bxor(k1, lshift(tail[1]+0ULL, 8)) end if l > 0 then - k1 = bxor(k1, tail[0]+0ULL) - k1 = band(k1*c1, max32) - k1 = bxor(lshift(k1, 15), rshift(k1, 17)) - k1 = band(k1*c2, max32) - h1 = bxor(h1, k1) + k1 = bxor(k1, tail[0]+0ULL) + k1 = band(k1*c1, max32) + k1 = bxor(lshift(k1, 15), rshift(k1, 17)) + k1 = band(k1*c2, max32) + h1 = bxor(h1, k1) end h1 = bxor(h1, length+0ULL) @@ -87,37 +87,37 @@ do local MurmurHash3_x64_128 = murmur.MurmurHash3_x64_128 MurmurHash3_x64_128._name = 'MurmurHash3_x64_128' MurmurHash3_x64_128._size = 128 - + local c1 = 0x87c37b91114253d5ULL local c2 = 0x4cf5ad432745937fULL local c3 = 0xff51afd7ed558ccdULL local c4 = 0xc4ceb9fe1a85ec53ULL local c5 = 0x52dce729ULL local c6 = 0x38495ab5ULL - + function MurmurHash3_x64_128:hash (data, length, seed) local nblocks = rshift(length, 4) local h1 = (seed and seed+0ULL) or 0ULL local h2 = h1 if nblocks > 0 then - for i = 0, nblocks - 1 do - local k1 = ffi.cast(uint64_ptr_t, data)[i*2]*c1 - k1 = bxor(lshift(k1, 31), rshift(k1, 33)) - k1 = k1*c2 - h1 = bxor(h1, k1) - h1 = bxor(lshift(h1, 27), rshift(h1, 37)) - h1 = h1 + h2 - h1 = h1*5ULL + c5 - - local k2 = ffi.cast(uint64_ptr_t, data)[i*2+1]*c2 - k2 = bxor(lshift(k2, 33), rshift(k2, 31)) - k2 = k2*c1 - h2 = bxor(h2, k2) - h2 = bxor(lshift(h2, 31), rshift(h2, 33)) - h2 = h2 + h1 - h2 = h2*5ULL + c6 - end + for i = 0, nblocks - 1 do + local k1 = ffi.cast(uint64_ptr_t, data)[i*2]*c1 + k1 = bxor(lshift(k1, 31), rshift(k1, 33)) + k1 = k1*c2 + h1 = bxor(h1, k1) + h1 = bxor(lshift(h1, 27), rshift(h1, 37)) + h1 = h1 + h2 + h1 = h1*5ULL + c5 + + local k2 = ffi.cast(uint64_ptr_t, data)[i*2+1]*c2 + k2 = bxor(lshift(k2, 33), rshift(k2, 31)) + k2 = k2*c1 + h2 = bxor(h2, k2) + h2 = bxor(lshift(h2, 31), rshift(h2, 33)) + h2 = h2 + h1 + h2 = h2*5ULL + c6 + end end local k1 = 0ULL @@ -126,57 +126,57 @@ do local l = band(length, 15) if l > 14 then - k2 = bxor(k2, lshift(tail[14]+0ULL, 48)) + k2 = bxor(k2, lshift(tail[14]+0ULL, 48)) end if l > 13 then - k2 = bxor(k2, lshift(tail[13]+0ULL, 40)) + k2 = bxor(k2, lshift(tail[13]+0ULL, 40)) end if l > 12 then - k2 = bxor(k2, lshift(tail[12]+0ULL, 32)) + k2 = bxor(k2, lshift(tail[12]+0ULL, 32)) end if l > 11 then - k2 = bxor(k2, lshift(tail[11]+0ULL, 24)) + k2 = bxor(k2, lshift(tail[11]+0ULL, 24)) end if l > 10 then - k2 = bxor(k2, lshift(tail[10]+0ULL, 16)) + k2 = bxor(k2, lshift(tail[10]+0ULL, 16)) end if l > 9 then - k2 = bxor(k2, lshift(tail[9]+0ULL, 8)) + k2 = bxor(k2, lshift(tail[9]+0ULL, 8)) end if l > 8 then - k2 = bxor(k2, tail[8]+0ULL) - k2 = k2*c2 - k2 = bxor(lshift(k2, 33), rshift(k2, 31)) - k2 = k2*c1 - h2 = bxor(h2, k2) + k2 = bxor(k2, tail[8]+0ULL) + k2 = k2*c2 + k2 = bxor(lshift(k2, 33), rshift(k2, 31)) + k2 = k2*c1 + h2 = bxor(h2, k2) end if l > 7 then - k1 = bxor(k1, lshift(tail[7]+0ULL, 56)) + k1 = bxor(k1, lshift(tail[7]+0ULL, 56)) end if l > 6 then - k1 = bxor(k1, lshift(tail[6]+0ULL, 48)) + k1 = bxor(k1, lshift(tail[6]+0ULL, 48)) end if l > 5 then - k1 = bxor(k1, lshift(tail[5]+0ULL, 40)) + k1 = bxor(k1, lshift(tail[5]+0ULL, 40)) end if l > 4 then - k1 = bxor(k1, lshift(tail[4]+0ULL, 32)) + k1 = bxor(k1, lshift(tail[4]+0ULL, 32)) end if l > 3 then - k1 = bxor(k1, lshift(tail[3]+0ULL, 24)) + k1 = bxor(k1, lshift(tail[3]+0ULL, 24)) end if l > 2 then - k1 = bxor(k1, lshift(tail[2]+0ULL, 16)) + k1 = bxor(k1, lshift(tail[2]+0ULL, 16)) end if l > 1 then - k1 = bxor(k1, lshift(tail[1]+0ULL, 8)) + k1 = bxor(k1, lshift(tail[1]+0ULL, 8)) end if l > 0 then - k1 = bxor(k1, tail[0]+0ULL) - k1 = k1*c1 - k1 = bxor(lshift(k1, 31), rshift(k1, 33)) - k1 = k1*c2 - h1 = bxor(h1, k1) + k1 = bxor(k1, tail[0]+0ULL) + k1 = k1*c1 + k1 = bxor(lshift(k1, 31), rshift(k1, 33)) + k1 = k1*c2 + h1 = bxor(h1, k1) end h1 = bxor(h1, length+0ULL) @@ -208,7 +208,7 @@ local function selftest_hash (hash, expected, perf) local key = ffi.new("uint8_t [256]") local hashes = ffi.new("uint8_t[?]", bytes*256) local seed = ffi.new("uint64_t") - + print("Sleftest hash "..hash:name()) for i = 0, 255 do key[i] = i @@ -222,32 +222,32 @@ local function selftest_hash (hash, expected, perf) else error("Failed, expected 0x"..bit.tohex(expected)..", got 0x"..bit.tohex(check)) end - + if perf_enable and perf then print("Performance test with data blocks from "..(perf.min or 1).." to " - ..perf.max.." bytes (iterations per second)") + ..perf.max.." bytes (iterations per second)") assert(perf.max < 1024) local v = ffi.new("uint8_t[1024]") - + for j = perf.min or 1, perf.max do - local start = ffi.C.get_time_ns() - jit.flush() - for i = 1, perf.iter do - v[j-1] = v[j-1]+1 - hash:hash(v, j, 0) - end - local stop = ffi.C.get_time_ns() - local iter_rate = perf.iter/(tonumber(stop-start)/1e9) - print(j, math.floor(iter_rate)) - assert(iter_rate >= perf.expect, string.format("Performance test failed " - .."for %d byte blocks, " - .."expected at least %d " - .." iterations per second, " - .."got %d", j, perf.expect, iter_rate)) + local start = ffi.C.get_time_ns() + jit.flush() + for i = 1, perf.iter do + v[j-1] = v[j-1]+1 + hash:hash(v, j, 0) + end + local stop = ffi.C.get_time_ns() + local iter_rate = perf.iter/(tonumber(stop-start)/1e9) + print(j, math.floor(iter_rate)) + assert(iter_rate >= perf.expect, string.format("Performance test failed " + .."for %d byte blocks, " + .."expected at least %d " + .." iterations per second, " + .."got %d", j, perf.expect, iter_rate)) + end + print("Passed") end - print("Passed") end -end function selftest() selftest_hash(murmur.MurmurHash3_x86_32, 0xB0F57EE3, { max = 8, iter = 1e7, expect = 1e7 }) diff --git a/src/lib/ipc/shmem/mib.lua b/src/lib/ipc/shmem/mib.lua index f2cdcd9441..6abaa01e16 100644 --- a/src/lib/ipc/shmem/mib.lua +++ b/src/lib/ipc/shmem/mib.lua @@ -54,11 +54,11 @@ local function octetstr_t (length) return octetstr_types[length] else local type = ffi.typeof( - [[ - struct { uint16_t length; - uint8_t data[$]; - } __attribute__((packed)) - ]], length) + [[ + struct { uint16_t length; + uint8_t data[$]; + } __attribute__((packed)) + ]], length) octetstr_types[length] = type return type end @@ -69,14 +69,14 @@ end -- them in the context of the shmem system. The default 'OctetStr' -- supports octet strings up to 255 bytes. local types = { Integer32 = int32_t, - Unsigned32 = uint32_t, - OctetStr = octetstr_t(255), - Counter32 = uint32_t, - Counter64 = uint64_t, - Gauge32 = uint32_t, - TimeTicks = uint32_t, - Bits = octetstr_t(16), - } + Unsigned32 = uint32_t, + OctetStr = octetstr_t(255), + Counter32 = uint32_t, + Counter64 = uint64_t, + Gauge32 = uint32_t, + TimeTicks = uint32_t, + Bits = octetstr_t(16), + } -- The 'type' argument of the constructor must either be a string that -- identifies one of the supported types @@ -120,11 +120,11 @@ function mib:register (name, type, value) assert(type.type) smi_type = type.type if type.type == 'OctetStr' then - assert(type.length and type.length <= 65535) - ctype = octetstr_t(type.length) + assert(type.length and type.length <= 65535) + ctype = octetstr_t(type.length) else - -- Accept all other legal types - type = type.type + -- Accept all other legal types + type = type.type end end ctype = ctype or types[type] @@ -153,24 +153,24 @@ function mib:set (name, value) if value ~= nil then local obj = self._objs[name] if obj and obj.smi_type == 'OctetStr' then - local length = math.min(string.len(value), obj.length - 2) - local octet = mib:superClass().get(self, name) - octet.length = length - ffi.copy(octet.data, value, length) + local length = math.min(string.len(value), obj.length - 2) + local octet = mib:superClass().get(self, name) + octet.length = length + ffi.copy(octet.data, value, length) elseif obj and obj.smi_type == 'Bits' then - local octet = mib:superClass().get(self, name) - octet.length = 16 - ffi.fill(octet.data, 16) - local i = 1 - while value[i] do - local bit_n = value[i] - local byte = rshift(bit_n, 3) - octet.data[byte] = bor(octet.data[byte], - lshift(1, 7-band(bit_n, 7))) - i = i+1 - end + local octet = mib:superClass().get(self, name) + octet.length = 16 + ffi.fill(octet.data, 16) + local i = 1 + while value[i] do + local bit_n = value[i] + local byte = rshift(bit_n, 3) + octet.data[byte] = bor(octet.data[byte], + lshift(1, 7-band(bit_n, 7))) + i = i+1 + end else - mib:superClass().set(self, name, value) + mib:superClass().set(self, name, value) end end end @@ -196,13 +196,13 @@ function mib:get (name, ...) local result = ... or {} local index = 1 for i = 0, 15 do - local byte = octet.data[i] - for j = 0, 7 do - if band(byte, lshift(1, 7-j)) ~= 0 then - result[index] = i*8+j - index = index+1 - end - end + local byte = octet.data[i] + for j = 0, 7 do + if band(byte, lshift(1, 7-j)) ~= 0 then + result[index] = i*8+j + index = index+1 + end + end end result[index] = nil return result diff --git a/src/lib/ipc/shmem/shmem.lua b/src/lib/ipc/shmem/shmem.lua index 968521fd80..fd94d262d9 100644 --- a/src/lib/ipc/shmem/shmem.lua +++ b/src/lib/ipc/shmem/shmem.lua @@ -184,7 +184,7 @@ local defaults = { ---- Class methods local function init (self, options, data_mode, index_mode) - assert(options and options.filename) + assert(options and options.filename) local o = shmem:superClass().new(self) local dir = options.directory or defaults.directory if dir ~= '' then @@ -228,10 +228,10 @@ end -- description is stored in two tables by name and by handle. local function add_obj(self, name, offset, ctype, length, fields) local obj = { offset = offset, - ctype = ctype, - ctype_ptr = ffi.typeof("$*", ctype), - length = length, - fields = fields } + ctype = ctype, + ctype_ptr = ffi.typeof("$*", ctype), + length = length, + fields = fields } self._objs[name] = obj local handle = self._nobjs+1 self._nobjs = handle @@ -259,51 +259,51 @@ function shmem:attach (options) local next, s = preamble:split(':') local namespace, version = next(s), next(s) assert(namespace and namespace == o._namespace, - "Namespace mismatch: expected "..o._namespace - ..", got "..namespace) - assert(version and tonumber(version) <= o._version, - "Version mismatch: expected <= "..o._version - ..", got "..version) - assert(next(s) == nil) - local handle = 0 - while true do - local spec = o._index_fh:read('*line') - if spec == nil then break end - local name, length, ctype - local fields = {} - if (o._fs) then - local next, s = spec:split(o._fs) - name, length = next(s), next(s) - assert(name, length) - length = tonumber(length) - if o._ctype then - ctype = o._ctype - assert(ffi.sizeof(ctype == length)) - else - ctype = ffi.typeof("uint8_t [$]", length) - end - -- Read any additional fields - for f in next, s do - table.insert(fields, f) - print("add field", f) - end - -- Bail out if additional fields are present but the class - -- does not support an extended index. - assert(self._extended or #fields == 0, - "Found extra fields in standard index") - else - name = spec - ctype = o._ctype - assert(ctype) - length = ffi.sizeof(ctype) + "Namespace mismatch: expected "..o._namespace + ..", got "..namespace) + assert(version and tonumber(version) <= o._version, + "Version mismatch: expected <= "..o._version + ..", got "..version) + assert(next(s) == nil) + local handle = 0 + while true do + local spec = o._index_fh:read('*line') + if spec == nil then break end + local name, length, ctype + local fields = {} + if (o._fs) then + local next, s = spec:split(o._fs) + name, length = next(s), next(s) + assert(name, length) + length = tonumber(length) + if o._ctype then + ctype = o._ctype + assert(ffi.sizeof(ctype == length)) + else + ctype = ffi.typeof("uint8_t [$]", length) + end + -- Read any additional fields + for f in next, s do + table.insert(fields, f) + print("add field", f) + end + -- Bail out if additional fields are present but the class + -- does not support an extended index. + assert(self._extended or #fields == 0, + "Found extra fields in standard index") + else + name = spec + ctype = o._ctype + assert(ctype) + length = ffi.sizeof(ctype) + end + add_obj(o, name, o._size, ctype, length, fields) + end + o._base = C.shmem_attach(o._data_fh, o._size) + assert(o._base ~= nil, "mmap failed") + o._attach = true + return o end - add_obj(o, name, o._size, ctype, length, fields) - end - o._base = C.shmem_attach(o._data_fh, o._size) - assert(o._base ~= nil, "mmap failed") - o._attach = true - return o -end ---- Instance methods @@ -348,32 +348,32 @@ end function shmem:_register (name, ctype, value, fields) assert(name and ctype) assert(self._extended or fields == nil, - "Attempting to add extra fields to standard index") + "Attempting to add extra fields to standard index") local handle if self._attach then local obj = get_obj(self, name) assert(ffi.sizeof(ctype) == obj.length, - "invalid length of overriding ctype") + "invalid length of overriding ctype") obj.ctype = ctype obj.ctype_ptr = ffi.typeof("$*", ctype) handle = self._n_to_h[name] else assert(not self._objs[name], "object already exists: "..name) assert(self._fs == '' or not string.find(name, self._fs), - "illegal object name: "..name) + "illegal object name: "..name) local length = ffi.sizeof(ctype) local old_size = self._size handle = add_obj(self, name, self._size, ctype, length, fields) self._base = C.shmem_grow(self._data_fh, self._base, - old_size, self._size) + old_size, self._size) assert(self._base ~= nil, "mmap failed") local line = name if self._fs and self._fs ~= '' then - line = line..self._fs..length - if fields then - assert(type(fields) == 'table') - line = line..self._fs..table.concat(fields, self._fs) - end + line = line..self._fs..length + if fields then + assert(type(fields) == 'table') + line = line..self._fs..table.concat(fields, self._fs) + end end assert(self._index_fh:write(line, '\n')) assert(self._index_fh:flush()) @@ -414,7 +414,7 @@ function shmem:dictionary() return table, self._h_to_n, self._n_to_h end --- Set a named object to the given value. +-- Set a named object to the given value. function shmem:set (name, value) if value ~= nil then local obj = get_obj(self, name) diff --git a/src/lib/json.lua b/src/lib/json.lua index 1af70b25a0..86cbf85e43 100644 --- a/src/lib/json.lua +++ b/src/lib/json.lua @@ -77,7 +77,7 @@ end -- @param startPos The starting position for the scan. -- @return table, int The scanned array as a table, and the position of the next character to scan. function decode_scanArray(s,startPos) - local array = {} -- The return value + local array = {} -- The return value local stringLen = string.len(s) base.assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s ) startPos = startPos + 1 @@ -106,14 +106,14 @@ function decode_scanComment(s, startPos) base.assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos) local endPos = string.find(s,'*/',startPos+2) base.assert(endPos~=nil, "Unterminated comment in string at " .. startPos) - return endPos+2 + return endPos+2 end --- Scans for given constants: true, false or null -- Returns the appropriate Lua type, and the position of the next character to read. -- @param s The string being scanned. -- @param startPos The position in the string at which to start scanning. --- @return object, int The object (true, false or nil) and the position at which the next character should be +-- @return object, int The object (true, false or nil) and the position at which the next character should be -- scanned. function decode_scanConstant(s, startPos) local consts = { ["true"] = true, ["false"] = false, ["null"] = nil } @@ -141,8 +141,8 @@ function decode_scanNumber(s,startPos) local stringLen = string.len(s) local acceptableChars = "+-0123456789.e" while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true) - and endPos<=stringLen - ) do + and endPos<=stringLen + ) do endPos = endPos + 1 end local stringValue = 'return ' .. string.sub(s,startPos, endPos-1) @@ -184,7 +184,7 @@ function decode_scanObject(s,startPos) base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) value, startPos = decode(s,startPos) object[key]=value - until false -- infinite loop while key-value pairs are found + until false -- infinite loop while key-value pairs are found end --- Scans a JSON string from the opening inverted comma or single quote to the @@ -206,7 +206,7 @@ function decode_scanString(s,startPos) repeat local curChar = string.sub(s,endPos,endPos) -- Character escaping is only used to escape the string delimiters - if not escaped then + if not escaped then if curChar==[[\]] then escaped = true else @@ -222,7 +222,7 @@ function decode_scanString(s,startPos) local stringValue = 'return ' .. string.sub(s, startPos, endPos-1) local stringEval = base.loadstring(stringValue) base.assert(stringEval, 'Failed to load string [ ' .. stringValue .. '] in JSON4Lua.decode_scanString at position ' .. startPos .. ' : ' .. endPos) - return stringEval(), endPos + return stringEval(), endPos end --- Scans a JSON string skipping all whitespace from the current start position. diff --git a/src/lib/lua/alt_getopt.lua b/src/lib/lua/alt_getopt.lua index 2387005cb8..b61039b730 100644 --- a/src/lib/lua/alt_getopt.lua +++ b/src/lib/lua/alt_getopt.lua @@ -42,7 +42,7 @@ end local function err_unknown_opt (opt) exit_with_error ("Unknown option `-" .. - (#opt > 1 and "-" or "") .. opt .. "'\n", 1) + (#opt > 1 and "-" or "") .. opt .. "'\n", 1) end local function canonize (options, opt) @@ -54,7 +54,7 @@ local function canonize (options, opt) opt = options [opt] if not options [opt] then - err_unknown_opt (opt) + err_unknown_opt (opt) end end @@ -76,72 +76,72 @@ function get_ordered_opts (arg, sh_opts, long_opts) local a = arg [i] if a == "--" then - i = i + 1 - break + i = i + 1 + break elseif a == "-" then - break + break elseif a:sub (1, 2) == "--" then - local pos = a:find ("=", 1, true) + local pos = a:find ("=", 1, true) - if pos then - local opt = a:sub (3, pos-1) + if pos then + local opt = a:sub (3, pos-1) - opt = canonize (options, opt) + opt = canonize (options, opt) - if options [opt] == 0 then - exit_with_error ("Bad usage of option `" .. a .. "'\n", 1) - end + if options [opt] == 0 then + exit_with_error ("Bad usage of option `" .. a .. "'\n", 1) + end - optarg [count] = a:sub (pos+1) - opts [count] = opt - else - local opt = a:sub (3) + optarg [count] = a:sub (pos+1) + opts [count] = opt + else + local opt = a:sub (3) - opt = canonize (options, opt) + opt = canonize (options, opt) - if options [opt] == 0 then - opts [count] = opt - else - if i == #arg then - exit_with_error ("Missed value for option `" .. a .. "'\n", 1) - end + if options [opt] == 0 then + opts [count] = opt + else + if i == #arg then + exit_with_error ("Missed value for option `" .. a .. "'\n", 1) + end - optarg [count] = arg [i+1] - opts [count] = opt - i = i + 1 - end - end - count = count + 1 + optarg [count] = arg [i+1] + opts [count] = opt + i = i + 1 + end + end + count = count + 1 elseif a:sub (1, 1) == "-" then - local j - for j=2,a:len () do - local opt = canonize (options, a:sub (j, j)) - - if options [opt] == 0 then - opts [count] = opt - count = count + 1 - elseif a:len () == j then - if i == #arg then - exit_with_error ("Missed value for option `-" .. opt .. "'\n", 1) - end - - optarg [count] = arg [i+1] - opts [count] = opt - i = i + 1 - count = count + 1 - break - else - optarg [count] = a:sub (j+1) - opts [count] = opt - count = count + 1 - break - end - end + local j + for j=2,a:len () do + local opt = canonize (options, a:sub (j, j)) + + if options [opt] == 0 then + opts [count] = opt + count = count + 1 + elseif a:len () == j then + if i == #arg then + exit_with_error ("Missed value for option `-" .. opt .. "'\n", 1) + end + + optarg [count] = arg [i+1] + opts [count] = opt + i = i + 1 + count = count + 1 + break + else + optarg [count] = a:sub (j+1) + opts [count] = opt + count = count + 1 + break + end + end else - break + break end i = i + 1 @@ -156,9 +156,9 @@ function get_opts (arg, sh_opts, long_opts) local opts,optind,optarg = get_ordered_opts (arg, sh_opts, long_opts) for i,v in ipairs (opts) do if optarg [i] then - ret [v] = optarg [i] + ret [v] = optarg [i] else - ret [v] = 1 + ret [v] = 1 end end diff --git a/src/lib/protocol/datagram.lua b/src/lib/protocol/datagram.lua index f281ae5f07..dabfe0c5b4 100644 --- a/src/lib/protocol/datagram.lua +++ b/src/lib/protocol/datagram.lua @@ -49,15 +49,15 @@ local datagram = subClass(nil) -- Create a datagram from a packet or from scratch (if p == nil). The -- class argument is only relevant for parsing and can be set to the --- header class of the the outermost packet header. +-- header class of the the outermost packet header. local function init (o, p, class) if not o._recycled then o._parse = { stack = {}, index = 0 } o._packet = ffi.new("struct packet *[1]") elseif o._parse.stack[1] then for i, _ in ipairs(o._parse.stack) do - o._parse.stack[i]:free() - o._parse.stack[i] = nil + o._parse.stack[i]:free() + o._parse.stack[i] = nil end o._parse.index = 0 end @@ -184,14 +184,14 @@ function datagram:pop (n) assert(n <= parse.index) for i = 1, parse.index do if i <= n then - local proto = parse.stack[i] + local proto = parse.stack[i] n_bytes = n_bytes + proto:sizeof() - proto:free() + proto:free() end if i+n <= parse.index then - parse.stack[i] = parse.stack[i+n] + parse.stack[i] = parse.stack[i+n] else - parse.stack[i] = nil + parse.stack[i] = nil end end parse.index = parse.index - n diff --git a/src/lib/protocol/ethernet.lua b/src/lib/protocol/ethernet.lua index 2bfd708547..40d47e2911 100644 --- a/src/lib/protocol/ethernet.lua +++ b/src/lib/protocol/ethernet.lua @@ -20,7 +20,7 @@ local ethernet = subClass(header) ethernet._name = "ethernet" ethernet._header_type = ether_header_t ethernet._header_ptr_type = ffi.typeof("$*", ether_header_t) -ethernet._ulp = { +ethernet._ulp = { class_map = { [0x0800] = "lib.protocol.ipv4", [0x86dd] = "lib.protocol.ipv6", @@ -43,9 +43,9 @@ function ethernet:pton (p) local i = 0 for v in p:split(":") do if string.match(v:lower(), '^[0-9a-f][0-9a-f]$') then - result[i] = tonumber("0x"..v) + result[i] = tonumber("0x"..v) else - error("invalid mac address "..p) + error("invalid mac address "..p) end i = i+1 end diff --git a/src/lib/protocol/gre.lua b/src/lib/protocol/gre.lua index 25f7621598..6ae665a9f0 100644 --- a/src/lib/protocol/gre.lua +++ b/src/lib/protocol/gre.lua @@ -20,20 +20,20 @@ local gre = subClass(header) local gre_t = ffi.typeof[[ struct { - uint16_t bits; // Flags, version - uint16_t protocol; + uint16_t bits; // Flags, version + uint16_t protocol; } ]] local subclasses = { csum = "lib.protocol.gre_csum", - key = "lib.protocol.gre_key", - csum_key = "lib.protocol.gre_csum_key" } + key = "lib.protocol.gre_key", + csum_key = "lib.protocol.gre_csum_key" } -- Class variables gre._name = "gre" gre._header_type = gre_t gre._header_ptr_type = ffi.typeof("$*", gre_t) -gre._ulp = { +gre._ulp = { class_map = { [0x6558] = "lib.protocol.ethernet" }, method = 'protocol' } @@ -46,14 +46,14 @@ function gre:new (config) local type = nil if config then if config.checksum then - type = 'csum' + type = 'csum' end if config.key ~= nil then - if type then - type = 'csum_key' - else - type = 'key' - end + if type then + type = 'csum_key' + else + type = 'key' + end end end @@ -84,9 +84,9 @@ function gre:new_from_mem (mem, size) end if bitfield(16, parse_mem[0], 'bits', 2, 1) == 1 then if type then - type = 'csum_key' + type = 'csum_key' else - type = 'key' + type = 'key' end has_key = true end @@ -108,8 +108,8 @@ local function checksum(header, payload, length) header.csum = 0; header.reserved1 = 0; local csum = ipsum(payload, length, - bit.bnot(ipsum(ffi.cast("uint8_t *", header), - ffi.sizeof(header), 0))) + bit.bnot(ipsum(ffi.cast("uint8_t *", header), + ffi.sizeof(header), 0))) header.csum = csum_in return csum end diff --git a/src/lib/protocol/gre_csum.lua b/src/lib/protocol/gre_csum.lua index 7b558015a0..1e74df5e7f 100644 --- a/src/lib/protocol/gre_csum.lua +++ b/src/lib/protocol/gre_csum.lua @@ -11,10 +11,10 @@ local gre_csum = subClass(gre) local gre_csum_t = ffi.typeof[[ struct { - uint16_t bits; // Flags, version - uint16_t protocol; - uint16_t csum; - uint16_t reserved1; + uint16_t bits; // Flags, version + uint16_t protocol; + uint16_t csum; + uint16_t reserved1; } ]] diff --git a/src/lib/protocol/gre_csum_key.lua b/src/lib/protocol/gre_csum_key.lua index 8b868f6886..c9cae5c619 100644 --- a/src/lib/protocol/gre_csum_key.lua +++ b/src/lib/protocol/gre_csum_key.lua @@ -10,11 +10,11 @@ local gre_csum_key = subClass(gre) local gre_csum_key_t = ffi.typeof[[ struct { - uint16_t bits; // Flags, version - uint16_t protocol; - uint16_t csum; - uint16_t reserved1; - uint32_t key; + uint16_t bits; // Flags, version + uint16_t protocol; + uint16_t csum; + uint16_t reserved1; + uint32_t key; } ]] diff --git a/src/lib/protocol/gre_key.lua b/src/lib/protocol/gre_key.lua index b1b650468b..1e01a6fcba 100644 --- a/src/lib/protocol/gre_key.lua +++ b/src/lib/protocol/gre_key.lua @@ -10,9 +10,9 @@ local gre_key = subClass(gre) local gre_key_t = ffi.typeof[[ struct { - uint16_t bits; // Flags, version - uint16_t protocol; - uint32_t key; + uint16_t bits; // Flags, version + uint16_t protocol; + uint32_t key; } ]] diff --git a/src/lib/protocol/header.lua b/src/lib/protocol/header.lua index edc5eaf154..ab59aada0e 100644 --- a/src/lib/protocol/header.lua +++ b/src/lib/protocol/header.lua @@ -42,7 +42,7 @@ -- ethernet._name = "ethernet" -- ethernet._header_type = ether_header_t -- ethernet._header_ptr_type = ffi.typeof("$*", ether_header_t) --- ethernet._ulp = { +-- ethernet._ulp = { -- class_map = { [0x86dd] = "lib.protocol.ipv6" }, -- method = 'type' } -- @@ -147,7 +147,7 @@ end -- default equality method, can be overriden in the ancestors function header:eq (other) return (ffi.string(self._header[0], self:sizeof()) == - ffi.string(other._header[0],self:sizeof())) + ffi.string(other._header[0],self:sizeof())) end -- Copy the header to some location in memory (usually a packet @@ -179,9 +179,9 @@ function header:upper_layer () local class = self._ulp.class_map[self[method](self)] if class then if package.loaded[class] then - return package.loaded[class] + return package.loaded[class] else - return require(class) + return require(class) end else return nil diff --git a/src/lib/protocol/icmp/header.lua b/src/lib/protocol/icmp/header.lua index 635b67ed31..1b85accf47 100644 --- a/src/lib/protocol/icmp/header.lua +++ b/src/lib/protocol/icmp/header.lua @@ -11,9 +11,9 @@ local ipsum = require("lib.checksum").ipsum local icmp_t = ffi.typeof[[ struct { - uint8_t type; - uint8_t code; - int16_t checksum; + uint8_t type; + uint8_t code; + int16_t checksum; } __attribute__((packed)) ]] @@ -23,9 +23,9 @@ local icmp = subClass(header) icmp._name = "icmp" icmp._header_type = icmp_t icmp._header_ptr_type = ffi.typeof("$*", icmp_t) -icmp._ulp = { +icmp._ulp = { class_map = { [135] = "lib.protocol.icmp.nd.ns", - [136] = "lib.protocol.icmp.nd.na" }, + [136] = "lib.protocol.icmp.nd.na" }, method = "type" } -- Class methods @@ -66,7 +66,7 @@ local function checksum(header, payload, length, ipv6) local csum_rcv = header.checksum header.checksum = 0 csum = ipsum(ffi.cast("uint8_t *", header), - ffi.sizeof(header), bit.bnot(csum)) + ffi.sizeof(header), bit.bnot(csum)) header.checksum = csum_rcv -- Add ICMP payload return ipsum(payload, length, bit.bnot(csum)) diff --git a/src/lib/protocol/icmp/nd/na.lua b/src/lib/protocol/icmp/nd/na.lua index d13bf1280d..b33ec8e7ea 100644 --- a/src/lib/protocol/icmp/nd/na.lua +++ b/src/lib/protocol/icmp/nd/na.lua @@ -8,8 +8,8 @@ local na = subClass(nd_header) local na_t = ffi.typeof[[ struct { - uint32_t flags; - uint8_t target[16]; + uint32_t flags; + uint8_t target[16]; } __attribute__((packed)) ]] diff --git a/src/lib/protocol/icmp/nd/ns.lua b/src/lib/protocol/icmp/nd/ns.lua index 4847aedef7..b54f1b773f 100644 --- a/src/lib/protocol/icmp/nd/ns.lua +++ b/src/lib/protocol/icmp/nd/ns.lua @@ -5,11 +5,11 @@ local nd_header = require("lib.protocol.icmp.nd.header") local ns_t = ffi.typeof[[ struct { - uint32_t reserved; - uint8_t target[16]; + uint32_t reserved; + uint8_t target[16]; } ]] - + local ns = subClass(nd_header) -- Class variables diff --git a/src/lib/protocol/icmp/nd/options/lladdr.lua b/src/lib/protocol/icmp/nd/options/lladdr.lua index 3685f1b04e..163dacc893 100644 --- a/src/lib/protocol/icmp/nd/options/lladdr.lua +++ b/src/lib/protocol/icmp/nd/options/lladdr.lua @@ -5,7 +5,7 @@ local lladdr = subClass(nil) local lladdr_t = ffi.typeof[[ struct { - uint8_t lladdr[6]; + uint8_t lladdr[6]; } ]] diff --git a/src/lib/protocol/icmp/nd/options/tlv.lua b/src/lib/protocol/icmp/nd/options/tlv.lua index 233d8b80f1..92c07246e7 100644 --- a/src/lib/protocol/icmp/nd/options/tlv.lua +++ b/src/lib/protocol/icmp/nd/options/tlv.lua @@ -5,8 +5,8 @@ local tlv = subClass(nil) ffi.cdef[[ typedef struct { - uint8_t type; - uint8_t length; + uint8_t type; + uint8_t length; } tlv_t __attribute__((packed)) ]] @@ -45,12 +45,12 @@ function tlv:new_from_mem (mem, size) local o = tlv:superClass().new(self) local tlv_t_size = ffi.sizeof(tlv_t) assert(tlv_t_size <= size) - local tlv = ffi.cast(tlv_ptr_t, mem) + local tlv = ffi.cast(tlv_ptr_t, mem) assert(o._types[tlv.type], "tlv: unsupported type") o._name = o._types[tlv.type].name local class = o._types[tlv.type].class o._option = require(class):new_from_mem(mem + tlv_t_size, - size - tlv_t_size) + size - tlv_t_size) local t = ffi.typeof("struct { tlv_t tlv; uint8_t data[$]; }", size-tlv_t_size) o._tlv = ffi.cast(ffi.typeof("$*", t), mem) return o diff --git a/src/lib/protocol/ipv4.lua b/src/lib/protocol/ipv4.lua index 1bff53d1f9..c8f97a6850 100644 --- a/src/lib/protocol/ipv4.lua +++ b/src/lib/protocol/ipv4.lua @@ -145,7 +145,7 @@ end function ipv4:checksum () self:header().checksum = C.htons(ipsum(ffi.cast("uint8_t *", self:header()), - self:sizeof(), 0)) + self:sizeof(), 0)) return C.ntohs(self:header().checksum) end diff --git a/src/lib/protocol/ipv6.lua b/src/lib/protocol/ipv6.lua index 4c103fdd41..d64df481ca 100644 --- a/src/lib/protocol/ipv6.lua +++ b/src/lib/protocol/ipv6.lua @@ -16,23 +16,23 @@ local defaults = { local ipv6hdr_t = ffi.typeof[[ struct { - uint32_t v_tc_fl; // version, tc, flow_label - uint16_t payload_length; - uint8_t next_header; - uint8_t hop_limit; - uint8_t src_ip[16]; - uint8_t dst_ip[16]; + uint32_t v_tc_fl; // version, tc, flow_label + uint16_t payload_length; + uint8_t next_header; + uint8_t hop_limit; + uint8_t src_ip[16]; + uint8_t dst_ip[16]; } __attribute__((packed)) ]] local ipv6hdr_pseudo_t = ffi.typeof[[ struct { - char src_ip[16]; - char dst_ip[16]; - uint16_t ulp_zero; - uint16_t ulp_length; - uint8_t zero[3]; - uint8_t next_header; + char src_ip[16]; + char dst_ip[16]; + uint16_t ulp_zero; + uint16_t ulp_length; + uint8_t zero[3]; + uint8_t next_header; } __attribute__((packed)) ]] diff --git a/src/lib/protocol/keyed_ipv6_tunnel.lua b/src/lib/protocol/keyed_ipv6_tunnel.lua index 5d5f3c17cd..e552cd6858 100644 --- a/src/lib/protocol/keyed_ipv6_tunnel.lua +++ b/src/lib/protocol/keyed_ipv6_tunnel.lua @@ -19,35 +19,35 @@ local header = require("lib.protocol.header") ffi.cdef[[ typedef union { - uint8_t cookie[8]; - uint64_t cookie_64bit; + uint8_t cookie[8]; + uint64_t cookie_64bit; } cookie_t; ]] local tunnel_header_t = ffi.typeof[[ struct { - uint32_t session_id; - cookie_t cookie; + uint32_t session_id; + cookie_t cookie; } __attribute__((packed)) ]] local tunnel = subClass(header) local cookie_t = ffi.metatype(ffi.typeof("cookie_t"), - { - __tostring = - function (c) - local s = { "0x" } - for i = 0, 7 do - table.insert(s, string.format("%02x", c.cookie[i])) - end - return table.concat(s) - end, - __eq = - function(lhs, rhs) - return rhs and lhs.cookie_64bit == rhs.cookie_64bit - end - }) + { + __tostring = + function (c) + local s = { "0x" } + for i = 0, 7 do + table.insert(s, string.format("%02x", c.cookie[i])) + end + return table.concat(s) + end, + __eq = + function(lhs, rhs) + return rhs and lhs.cookie_64bit == rhs.cookie_64bit + end + }) -- Class variables tunnel._name = "keyed ipv6 tunnel" @@ -78,7 +78,7 @@ end function tunnel:new_cookie (s) assert(type(s) == 'string' and string.len(s) == 8, - 'invalid cookie') + 'invalid cookie') local c = cookie_t() ffi.copy(c.cookie, s, 8) return c diff --git a/src/lib/protocol/tcp.lua b/src/lib/protocol/tcp.lua index cb1212395f..5f07c39ca6 100644 --- a/src/lib/protocol/tcp.lua +++ b/src/lib/protocol/tcp.lua @@ -152,7 +152,7 @@ function tcp:checksum (payload, length, ip) -- Add TCP header h.checksum = 0 csum = ipsum(ffi.cast("uint8_t *", h), - self:sizeof(), bit.bnot(csum)) + self:sizeof(), bit.bnot(csum)) -- Add TCP payload h.checksum = C.htons(ipsum(payload, length, bit.bnot(csum))) end diff --git a/src/lib/protocol/udp.lua b/src/lib/protocol/udp.lua index 7566f85b2e..89fb50f861 100644 --- a/src/lib/protocol/udp.lua +++ b/src/lib/protocol/udp.lua @@ -70,7 +70,7 @@ function udp:checksum (payload, length, ip) -- Add UDP header h.checksum = 0 csum = ipsum(ffi.cast("uint8_t *", h), - self:sizeof(), bit.bnot(csum)) + self:sizeof(), bit.bnot(csum)) -- Add UDP payload h.checksum = C.htons(ipsum(payload, length, bit.bnot(csum))) end diff --git a/src/lib/virtio/net_device.lua b/src/lib/virtio/net_device.lua index 0e3417ceee..58b040be30 100644 --- a/src/lib/virtio/net_device.lua +++ b/src/lib/virtio/net_device.lua @@ -78,10 +78,10 @@ function VirtioNetDevice:new(owner) virtq = {}, rx = {}, tx = { - p = nil, - tx_mrg_hdr = ffi.new("struct virtio_net_hdr_mrg_rxbuf*[1]") , - data_sent = nil, - finished = nil + p = nil, + tx_mrg_hdr = ffi.new("struct virtio_net_hdr_mrg_rxbuf*[1]") , + data_sent = nil, + finished = nil } } @@ -147,14 +147,14 @@ function VirtioNetDevice:rx_packet_end(header_id, total_size, rx_p) local l = self.owner.output.tx if l then if band(self.rx_hdr_flags, C.VIO_NET_HDR_F_NEEDS_CSUM) ~= 0 and - -- Bounds-check the checksum area - self.rx_hdr_csum_start <= rx_p.length - 2 and - self.rx_hdr_csum_offset <= rx_p.length - 2 + -- Bounds-check the checksum area + self.rx_hdr_csum_start <= rx_p.length - 2 and + self.rx_hdr_csum_offset <= rx_p.length - 2 then - checksum.finish_packet( - rx_p.data + self.rx_hdr_csum_start, - rx_p.length - self.rx_hdr_csum_start, - self.rx_hdr_csum_offset) + checksum.finish_packet( + rx_p.data + self.rx_hdr_csum_start, + rx_p.length - self.rx_hdr_csum_start, + self.rx_hdr_csum_offset) end link.transmit(l, rx_p) else diff --git a/src/program/snabbnfv/traffic/traffic.lua b/src/program/snabbnfv/traffic/traffic.lua index 5bda3b4586..8eec4bf541 100644 --- a/src/program/snabbnfv/traffic/traffic.lua +++ b/src/program/snabbnfv/traffic/traffic.lua @@ -42,23 +42,23 @@ function run (args) os.exit(1) end if loadreportinterval > 0 then - local t = timer.new("nfvloadreport", engine.report_load, loadreportinterval*1e9, 'repeating') - timer.activate(t) + local t = timer.new("nfvloadreport", engine.report_load, loadreportinterval*1e9, 'repeating') + timer.activate(t) end if linkreportinterval > 0 then - local t = timer.new("nfvlinkreport", engine.report_links, linkreportinterval*1e9, 'repeating') - timer.activate(t) + local t = timer.new("nfvlinkreport", engine.report_links, linkreportinterval*1e9, 'repeating') + timer.activate(t) end if debugreportinterval > 0 then - local t = timer.new("nfvdebugreport", engine.report_apps, debugreportinterval*1e9, 'repeating') - timer.activate(t) + local t = timer.new("nfvdebugreport", engine.report_apps, debugreportinterval*1e9, 'repeating') + timer.activate(t) end if benchpackets then - print("snabbnfv traffic starting (benchmark mode)") - bench(pciaddr, confpath, sockpath, benchpackets) + print("snabbnfv traffic starting (benchmark mode)") + bench(pciaddr, confpath, sockpath, benchpackets) else - print("snabbnfv traffic starting") - traffic(pciaddr, confpath, sockpath) + print("snabbnfv traffic starting") + traffic(pciaddr, confpath, sockpath) end else print("Wrong number of arguments: " .. tonumber(#args)) @@ -78,9 +78,9 @@ function traffic (pciaddr, confpath, sockpath) while true do local mtime2 = C.stat_mtime(confpath) if mtime2 ~= mtime then - print("Loading " .. confpath) - engine.configure(nfvconfig.load(confpath, pciaddr, sockpath)) - mtime = mtime2 + print("Loading " .. confpath) + engine.configure(nfvconfig.load(confpath, pciaddr, sockpath)) + mtime = mtime2 end engine.main({duration=1, no_report=true}) -- Flush buffered log messages every 1s diff --git a/src/program/snsh/snsh.lua b/src/program/snsh/snsh.lua index 7aefea012b..396d335f01 100644 --- a/src/program/snsh/snsh.lua +++ b/src/program/snsh/snsh.lua @@ -27,18 +27,18 @@ function run (parameters) function opt.i (arg) start_repl = true noop = false end function opt.j (arg) if arg:match("^v") then - local file = arg:match("^v=(.*)") - if file == '' then file = nil end - require("jit.v").start(file) - elseif arg:match("^p") then - local opts, file = arg:match("^p=([^,]*),?(.*)") - if file == '' then file = nil end - require("jit.p").start(opts, file) - profiling = true + local file = arg:match("^v=(.*)") + if file == '' then file = nil end + require("jit.v").start(file) + elseif arg:match("^p") then + local opts, file = arg:match("^p=([^,]*),?(.*)") + if file == '' then file = nil end + require("jit.p").start(opts, file) + profiling = true elseif arg:match("^dump") then - local opts, file = arg:match("^dump=([^,]*),?(.*)") - if file == '' then file = nil end - require("jit.dump").on(opts, file) + local opts, file = arg:match("^dump=([^,]*),?(.*)") + if file == '' then file = nil end + require("jit.dump").on(opts, file) end end function opt.e (arg) @@ -56,10 +56,10 @@ function run (parameters) if #parameters > 0 then run_script(parameters) elseif noop then - print(usage) + print(usage) main.exit(1) end - + if start_repl then repl() end if profiling then require("jit.p").stop() end end