Skip to content

Commit

Permalink
Merge pull request #479 from Igalia/move-file-exists
Browse files Browse the repository at this point in the history
Move file_exists, dir_exists and nic_exists functions to lwutil
  • Loading branch information
dpino authored Oct 14, 2016
2 parents 12f569e + d100475 commit bd2ea9e
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 119 deletions.
22 changes: 22 additions & 0 deletions src/apps/lwaftr/lwutil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module(..., package.seeall)

local constants = require("apps.lwaftr.constants")

local S = require("syscall")
local bit = require("bit")
local ffi = require("ffi")
local lib = require("core.lib")
Expand Down Expand Up @@ -94,3 +95,24 @@ function write_to_file(filename, content)
fd:write(content)
fd:close()
end

function fatal (msg)
print(msg)
main.exit(1)
end

function file_exists(path)
local stat = S.stat(path)
return stat and stat.isreg
end

function dir_exists(path)
local stat = S.stat(path)
return stat and stat.isdir
end

function nic_exists(pci_addr)
local devices="/sys/bus/pci/devices"
return dir_exists(("%s/%s"):format(devices, pci_addr)) or
dir_exists(("%s/0000:%s"):format(devices, pci_addr))
end
3 changes: 3 additions & 0 deletions src/program/lwaftr/loadtest/loadtest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ local PcapReader = require("apps.pcap.pcap").PcapReader
local lib = require("core.lib")
local numa = require("lib.numa")
local promise = require("program.lwaftr.loadtest.promise")
local lwutil = require("apps.lwaftr.lwutil")

local fatal = lwutil.fatal

local WARM_UP_BIT_RATE = 5e9
local WARM_UP_TIME = 2
Expand Down
105 changes: 63 additions & 42 deletions src/program/lwaftr/monitor/monitor.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module(..., package.seeall)

local S = require("syscall")
local ffi = require("ffi")
local ipv4 = require("lib.protocol.ipv4")
local lib = require("core.lib")
local lwtypes = require("apps.lwaftr.lwtypes")
local lwutil = require("apps.lwaftr.lwutil")
local shm = require("core.shm")

local uint32_ptr_t = ffi.typeof('uint32_t*')
local fatal = lwutil.fatal

local long_opts = {
help = "h"
Expand All @@ -15,11 +16,6 @@ local long_opts = {
local MIRROR_NOTHING = "0.0.0.0"
local MIRROR_EVERYTHING = "255.255.255.255"

local function fatal (msg)
print(msg)
main.exit(1)
end

local function usage (code)
print(require("program.lwaftr.monitor.README_inc"))
main.exit(code)
Expand All @@ -32,6 +28,8 @@ local function parse_args (args)
end
args = lib.dogetopt(args, handlers, "h", long_opts)
if #args < 1 or #args > 2 then usage(1) end

-- Return address and pid.
if #args == 1 then
local maybe_pid = tonumber(args[1])
if maybe_pid then
Expand All @@ -42,63 +40,86 @@ local function parse_args (args)
return args[1], args[2]
end

local function ipv4_to_num (addr)
local arr = ipv4:pton(addr)
return arr[3] * 2^24 + arr[2] * 2^16 + arr[1] * 2^8 + arr[0]
end

-- TODO: Refactor to a common library.
local function file_exists(path)
local stat = S.stat(path)
return stat and stat.isreg
local function find_pid_by_id (id)
for _, pid in ipairs(shm.children("/")) do
local path = "/"..pid.."/nic/id"
if shm.exists(path) then
local lwaftr_id = shm.open(path, lwtypes.lwaftr_id_type)
if ffi.string(lwaftr_id.value) == id then
return pid
end
end
end
end

local function find_lwaftr_process (pid)
local function find_mirror_path (pid)
-- Check process has v4v6_mirror defined.
if pid then
pid = assert(tonumber(pid), ("Incorrect PID value: '%s'"):format(pid))
local v4v6_mirror = "/"..pid.."/v4v6_mirror"
if not file_exists(shm.root..v4v6_mirror) then
-- Pid is an id.
if not tonumber(pid) then
pid = find_pid_by_id(pid)
if not pid then
fatal("Invalid lwAFTR id '%s'"):format(pid)
end
end
-- Pid exists?
if not shm.exists("/"..pid) then
fatal(("No Snabb instance with pid '%d'"):format(pid))
end
-- Check process has v4v6_mirror defined.
local path = "/"..pid.."/v4v6_mirror"
if not shm.exists(path) then
fatal(("lwAFTR process '%d' is not running in mirroring mode"):format(pid))
end
return v4v6_mirror
return path, pid
end

-- Return first process which has v4v6_mirror defined.
for _, pid in ipairs(shm.children("/")) do
pid = tonumber(pid)
if pid then
local v4v6_mirror = "/"..pid.."/v4v6_mirror"
if file_exists(shm.root..v4v6_mirror) then
return v4v6_mirror
end
local path = "/"..pid.."/v4v6_mirror"
if shm.exists(path) then
return path, pid
end
end
end

function run (args)
local action, pid = parse_args(args)
local path = find_lwaftr_process(pid)
if not path then
fatal("Couldn't find lwAFTR process running in mirroring mode")
local function set_mirror_address (address, path)
local function ipv4_to_num (addr)
local arr = ipv4:pton(addr)
return arr[3] * 2^24 + arr[2] * 2^16 + arr[1] * 2^8 + arr[0]
end

local ipv4_address
if action == "none" then
-- Validate address.
if address == "none" then
print("Monitor none")
ipv4_address = MIRROR_NOTHING
elseif action == "all" then
address = MIRROR_NOTHING
elseif address == "all" then
print("Monitor all")
ipv4_address = MIRROR_EVERYTHING
address = MIRROR_EVERYTHING
else
assert(ipv4:pton(action),
("Invalid action or incorrect IPv4 address: '%s'"):format(action))
ipv4_address = action
print(("Mirror address set to '%s'"):format(ipv4_address))
if not ipv4:pton(address) then
fatal(("Invalid action or incorrect IPv4 address: '%s'"):format(address))
end
end

local ipv4_num = ipv4_to_num(ipv4_address)
-- Set v4v6_mirror.
local ipv4_num = ipv4_to_num(address)
local v4v6_mirror = shm.open(path, "struct { uint32_t ipv4; }")
v4v6_mirror.ipv4 = ipv4_num
shm.unmap(v4v6_mirror)
end

function run (args)
local address, pid = parse_args(args)
local path, pid_number = find_mirror_path(pid)
if not path then
fatal("Couldn't find lwAFTR process running in mirroring mode")
end

set_mirror_address(address, path)
io.write(("Mirror address set to '%s'"):format(address))
if not tonumber(pid) then
io.write((" in PID '%d'"):format(pid_number))
end
io.write("\n")
end
26 changes: 4 additions & 22 deletions src/program/lwaftr/run/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,16 @@ local lib = require("core.lib")
local numa = require("lib.numa")
local setup = require("program.lwaftr.setup")
local ingress_drop_monitor = require("lib.timers.ingress_drop_monitor")
local lwutil = require("apps.lwaftr.lwutil")

local fatal, file_exists = lwutil.fatal, lwutil.file_exists
local nic_exists = lwutil.nic_exists

local function show_usage(exit_code)
print(require("program.lwaftr.run.README_inc"))
if exit_code then main.exit(exit_code) end
end

local function fatal(msg)
show_usage()
print('error: '..msg)
main.exit(1)
end

local function file_exists(path)
local stat = S.stat(path)
return stat and stat.isreg
end

local function dir_exists(path)
local stat = S.stat(path)
return stat and stat.isdir
end

local function nic_exists(pci_addr)
local devices="/sys/bus/pci/devices"
return dir_exists(("%s/%s"):format(devices, pci_addr)) or
dir_exists(("%s/0000:%s"):format(devices, pci_addr))
end

function parse_args(args)
if #args == 0 then show_usage(1) end
local conf_file, v4, v6
Expand Down
8 changes: 3 additions & 5 deletions src/program/lwaftr/run_nohw/run_nohw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ local RawSocket = require("apps.socket.raw").RawSocket
local LwAftr = require("apps.lwaftr.lwaftr").LwAftr
local lib = require("core.lib")
local S = require("syscall")
local lwutil = require("apps.lwaftr.lwutil")

local file_exists = lwutil.file_exists

local function check(flag, fmt, ...)
if not flag then
Expand All @@ -14,11 +17,6 @@ local function check(flag, fmt, ...)
end
end

local function file_exists(path)
local stat = S.stat(path)
return stat and stat.isreg
end

local function parse_args(args)
local verbosity = 0
local conf_file, b4_if, inet_if
Expand Down
15 changes: 3 additions & 12 deletions src/program/snabbvmx/lwaftr/lwaftr.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
module(..., package.seeall)

local S = require("syscall")
local config = require("core.config")
local ingress_drop_monitor = require("lib.timers.ingress_drop_monitor")
local lib = require("core.lib")
local lwcounter = require("apps.lwaftr.lwcounter")
local lwtypes = require("apps.lwaftr.lwtypes")
local lwutil = require("apps.lwaftr.lwutil")
local setup = require("program.snabbvmx.lwaftr.setup")
local shm = require("core.shm")

local fatal, file_exists = lwutil.fatal, lwutil.file_exists

local DEFAULT_MTU = 9500

local function show_usage (exit_code)
print(require("program.snabbvmx.lwaftr.README_inc"))
main.exit(exit_code)
end

-- TODO: Duplicated in other source files. Move to a common place.
local function fatal (msg)
print(msg)
main.exit(1)
end

local function file_exists (path)
local stat = S.stat(path)
return stat and stat.isreg
end

local function set_ring_buffer_size(ring_buffer_size)
print(("Ring buffer size set to %d"):format(ring_buffer_size))
require('apps.intel.intel10g').num_descriptors = ring_buffer_size
Expand Down
27 changes: 3 additions & 24 deletions src/program/snabbvmx/lwaftr/setup.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module(..., package.seeall)

local PcapFilter = require("apps.packet_filter.pcap_filter").PcapFilter
local S = require("syscall")
local V4V6 = require("apps.lwaftr.V4V6").V4V6
local VhostUser = require("apps.vhost.vhost_user").VhostUser
local basic_apps = require("apps.basic.basic_apps")
Expand All @@ -13,37 +12,22 @@ local ipv6_apps = require("apps.lwaftr.ipv6_apps")
local lib = require("core.lib")
local lwaftr = require("apps.lwaftr.lwaftr")
local lwcounter = require("apps.lwaftr.lwcounter")
local lwutil = require("apps.lwaftr.lwutil")
local nh_fwd = require("apps.lwaftr.nh_fwd")
local pci = require("lib.hardware.pci")
local raw = require("apps.socket.raw")
local tap = require("apps.tap.tap")
local pcap = require("apps.pcap.pcap")

local fatal, file_exists = lwutil.fatal, lwutil.file_exists
local dir_exists, nic_exists = lwutil.dir_exists, lwutil.nic_exists
local yesno = lib.yesno

-- TODO: Duplicated in other source files. Move to a common place.
local function dir_exists (path)
local stat = S.stat(path)
return stat and stat.isdir
end

-- TODO: Duplicated in other source files. Move to a common place.
local function nic_exists (pci_addr)
local devices="/sys/bus/pci/devices"
return dir_exists(("%s/%s"):format(devices, pci_addr)) or
dir_exists(("%s/0000:%s"):format(devices, pci_addr))
end

local function net_exists (pci_addr)
local devices="/sys/class/net"
return dir_exists(("%s/%s"):format(devices, pci_addr))
end

local function fatal (msg)
print(msg)
main.exit(1)
end

local function load_driver (pciaddr)
local device_info = pci.device_info(pciaddr)
return require(device_info.driver).driver
Expand Down Expand Up @@ -293,11 +277,6 @@ function lwaftr_app(c, conf, lwconf, sock_path)
end
end

local function file_exists (path)
local stat = S.stat(path)
return stat and stat.isreg
end

local function load_conf (conf_filename)
local function load_lwaftr_config (conf, conf_filename)
local filename = conf.lwaftr
Expand Down
9 changes: 3 additions & 6 deletions src/program/snabbvmx/nexthop/nexthop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ local S = require("syscall")
local ethernet = require("lib.protocol.ethernet")
local ffi = require("ffi")
local lib = require("core.lib")
local lwutil = require("apps.lwaftr.lwutil")
local shm = require("core.shm")

local file_exists = lwutil.file_exists

local macaddress_t = ffi.typeof[[
struct { uint8_t ether[6]; }
]]
Expand All @@ -19,12 +22,6 @@ local function usage (code)
main.exit(code)
end

-- TODO: Refactor to a general common purpose library.
local function file_exists(path)
local stat = S.stat(path)
return stat and stat.isreg
end

local function parse_args (args)
local handlers = {}
function handlers.h (arg) usage(0) end
Expand Down
Loading

0 comments on commit bd2ea9e

Please sign in to comment.