Repository navigation
Expand file tree
/
Copy pathreactor.lua
More file actions
74 lines (61 loc) · 1.88 KB
/
Copy pathreactor.lua
File metadata and controls
74 lines (61 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
--
local lunatik = require("lunatik")
local netlink = require("netlink")
local channel = require("netlink.channel")
local thread = require("thread")
local linux = require("linux")
local af = require("linux.socket").af
local scope = require("linux.rtnetlink").scope
local format = string.format
local NAME <const> = "netfailover" -- the table control.lua shares, and the channel family
local WATCHED <const> = "dummy0" -- primary uplink to watch
local TABLE <const> = 200 -- backup routing table id
local DST <const> = string.char(192, 0, 2, 1) -- 192.0.2.1
local DST_LEN <const> = 32
local LO <const> = 1 -- loopback ifindex
local CMD <const> = 1 -- channel genl command
local POLL_MS <const> = 200
local backup = {
family = af.INET,
dst = DST,
dst_len = DST_LEN,
oif = LO,
table = TABLE,
scope = scope.LINK,
}
local family = channel.new(NAME)
local reroute = {}
function reroute.down(route)
route:add(backup)
return "backup route installed"
end
function reroute.up(route)
route:del(backup)
return "backup route removed"
end
local function isdown()
local events = lunatik._ENV[NAME]
return events ~= nil and events[WATCHED] ~= nil
end
local function announce(state, change)
local event = format("%s %s: %s", WATCHED, state, change)
family:multicast(CMD, event)
print("netfailover: " .. event)
end
local function react()
local route <close> = netlink.rt.route()
local rerouted = false
while not thread.shouldstop() do
local down = isdown()
if down ~= rerouted then
local state = down and "down" or "up"
announce(state, reroute[state](route))
rerouted = down
end
linux.schedule(POLL_MS)
end
end
return react