Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ func Start(ccfg state.CentralCfg, ncfg state.LocalCfg, logLevel slog.Level, conf
}

n := &Nylon{
Trace: &NylonTrace{},
Router: &NylonRouter{},
Trace: &NylonTrace{},
ConfigState: state.ConfigState{
CentralCfg: ccfg,
LocalCfg: ncfg,
Expand Down
4 changes: 2 additions & 2 deletions core/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func HandleNylonIPCGet(n *Nylon, rw *bufio.ReadWriter) error {
// print forward table
sb.WriteString("\n\nForward Table:\n")
rt = make([]string, 0)
for prefix, route := range n.Router.ForwardTable.All() {
for prefix, route := range n.router.ForwardTable.All() {
rt = append(rt, fmt.Sprintf(" - %s via %s", prefix, route.Nh))
}
slices.Sort(rt)
Expand All @@ -156,7 +156,7 @@ func HandleNylonIPCGet(n *Nylon, rw *bufio.ReadWriter) error {
// print exit table
sb.WriteString("\n\nExit Table:\n")
rt = make([]string, 0)
for prefix, route := range n.Router.ExitTable.All() {
for prefix, route := range n.router.ExitTable.All() {
rt = append(rt, fmt.Sprintf(" - %s via %s", prefix, route.Nh))
}
slices.Sort(rt)
Expand Down
18 changes: 14 additions & 4 deletions core/nylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ import (
"github.com/encodeous/nylon/polyamide/device"
"github.com/encodeous/nylon/polyamide/tun"
"github.com/encodeous/nylon/state"
"github.com/gaissmai/bart"
"github.com/jellydator/ttlcache/v3"
)

// Nylon struct must be thread safe, since it can receive packets through PolyReceiver
type Nylon struct {
Trace *NylonTrace
Router *NylonRouter
RouterState *state.RouterState

router struct {
LastStarvationRequest time.Time
IO map[state.NodeId]*IOPending

// ForwardTable contains the full routing table
ForwardTable bart.Table[RouteTableEntry]
// ExitTable contains only routes to services hosted on this node
ExitTable bart.Table[RouteTableEntry]
log *slog.Logger
}

DispatchChannel chan func() error
state.ConfigState
Context context.Context
Expand All @@ -46,7 +56,7 @@ func (n *Nylon) Init() error {
if err != nil {
return err
}
err = n.Router.Init(n)
err = n.InitRouter()
if err != nil {
return err
}
Expand Down Expand Up @@ -141,7 +151,7 @@ func (n *Nylon) Cleanup() error {
ph.Stop()
}

n.Router.Cleanup()
n.CleanupRouter()
n.Trace.Cleanup()

return n.cleanupWireGuard()
Expand Down
8 changes: 3 additions & 5 deletions core/nylon_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func handleProbePing(n *Nylon, node state.NodeId, ep conn.Endpoint) {
if node == n.LocalCfg.Id {
return
}
r := n.Router
// check if link exists
for _, neigh := range n.RouterState.Neighbours {
for _, dep := range neigh.Eps {
Expand All @@ -86,7 +85,7 @@ func handleProbePing(n *Nylon, node state.NodeId, ep conn.Endpoint) {
dep.WgEndpoint = ep

if !dep.IsActive() {
r.UpdateNeighbour(node)
n.UpdateNeighbour(node)
}
dep.Renew()

Expand All @@ -104,14 +103,13 @@ func handleProbePing(n *Nylon, node state.NodeId, ep conn.Endpoint) {
newEp.Renew()
neigh.Eps = append(neigh.Eps, newEp)
// push route update to improve convergence time
r.UpdateNeighbour(node)
n.UpdateNeighbour(node)
return
}
}
}

func handleProbePong(n *Nylon, node state.NodeId, token uint64, ep conn.Endpoint) {
r := n.Router
// check if link exists
for _, neigh := range n.RouterState.Neighbours {
for _, dpLink := range neigh.Eps {
Expand All @@ -132,7 +130,7 @@ func handleProbePong(n *Nylon, node state.NodeId, token uint64, ep conn.Endpoint
// update wireguard endpoint
dpLink.WgEndpoint = ep

ComputeRoutes(n.RouterState, r)
ComputeRoutes(n.RouterState, n)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion core/nylon_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func nylonGc(n *Nylon) error {
neigh.Eps = neigh.Eps[:count]
}

err := n.Router.GcRouter()
err := n.GcRouter()
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions core/nylon_passive.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func (n *Nylon) initPassiveClient() error {
}

func scanPassivePeers(n *Nylon) error {
r := n.Router
for _, peer := range n.Device.GetPeers() {
nid := n.FindNodeBy(state.NyPublicKey(peer.GetPublicKey()))

Expand Down Expand Up @@ -44,9 +43,9 @@ func scanPassivePeers(n *Nylon) error {
if n.IsClient(*nid) {
// we have a passive client
for _, newPrefix := range ncfg.Prefixes {
recentlyAdvertised := r.hasRecentlyAdvertised(newPrefix.GetPrefix())
recentlyAdvertised := n.hasRecentlyAdvertised(newPrefix.GetPrefix())
if recentlyUpdated || !hasOtherAdvertisers && recentlyAdvertised {
r.updatePassiveClient(n, newPrefix, *nid, !recentlyUpdated)
n.updatePassiveClient(newPrefix, *nid, !recentlyUpdated)
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions core/nylon_tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
// polyamide traffic control for nylon

func (n *Nylon) InstallTC() {
r := n.Router
t := n.Trace

if state.DBG_trace_tc {
Expand Down Expand Up @@ -54,7 +53,7 @@ func (n *Nylon) InstallTC() {
})
// forward only outgoing packets based on the routing table
n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
entry, ok := r.ForwardTable.Lookup(packet.GetDst())
entry, ok := n.router.ForwardTable.Lookup(packet.GetDst())
if ok && !packet.Incoming() {
packet.ToPeer = entry.Peer
if state.DBG_trace_tc {
Expand All @@ -67,7 +66,7 @@ func (n *Nylon) InstallTC() {
} else {
// forward packets based on the routing table
n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
entry, ok := r.ForwardTable.Lookup(packet.GetDst())
entry, ok := n.router.ForwardTable.Lookup(packet.GetDst())
if ok {
packet.ToPeer = entry.Peer
if state.DBG_trace_tc {
Expand Down Expand Up @@ -102,7 +101,7 @@ func (n *Nylon) InstallTC() {

// bounce back packets destined for the current node
n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
entry, ok := r.ExitTable.Lookup(packet.GetDst())
entry, ok := n.router.ExitTable.Lookup(packet.GetDst())
// we should only accept packets destined to us, but not our passive clients
if ok && entry.Nh == n.LocalCfg.Id {
if state.DBG_trace_tc {
Expand Down Expand Up @@ -180,15 +179,15 @@ func (n *Nylon) handleNylonPacket(packet []byte, endpoint conn.Endpoint, peer *d
switch pkt.Type.(type) {
case *protocol.Ny_SeqnoRequestOp:
n.Dispatch(func() error {
return n.Router.routerHandleSeqnoRequest(*neigh, pkt.GetSeqnoRequestOp())
return n.routerHandleSeqnoRequest(*neigh, pkt.GetSeqnoRequestOp())
})
case *protocol.Ny_RouteOp:
n.Dispatch(func() error {
return n.Router.routerHandleRouteUpdate(*neigh, pkt.GetRouteOp())
return n.routerHandleRouteUpdate(*neigh, pkt.GetRouteOp())
})
case *protocol.Ny_AckRetractOp:
n.Dispatch(func() error {
return n.Router.routerHandleAckRetract(*neigh, pkt.GetAckRetractOp())
return n.routerHandleAckRetract(*neigh, pkt.GetAckRetractOp())
})
case *protocol.Ny_ProbeOp:
handleProbe(n, pkt.GetProbeOp(), endpoint, peer, *neigh)
Expand Down
3 changes: 1 addition & 2 deletions core/nylon_wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ func UpdateWireGuard(n *Nylon) error {

// configure changed route table entries
if !n.NoNetConfigure {
router := n.Router
newEntries := router.ComputeSysRouteTable()
newEntries := n.ComputeSysRouteTable()
oldEntries := n.prevInstalledRoutes
for _, oldEntry := range oldEntries {
if !slices.Contains(newEntries, oldEntry) {
Expand Down
Loading
Loading