11package neighbor
22
33import (
4- "log"
54 "net"
65 "sync"
76 "time"
87
8+ "github.com/tomvil/neigh2route/internal/logger"
99 "github.com/tomvil/neigh2route/pkg/netutils"
1010 "github.com/vishvananda/netlink"
1111)
@@ -47,7 +47,7 @@ func (nm *NeighborManager) AddNeighbor(ip net.IP, linkIndex int) {
4747 return
4848 }
4949
50- log . Printf ("Neighbor %s link index changed, re-adding neighbor" , ip .String ())
50+ logger . Info ("Neighbor %s link index changed, re-adding neighbor" , ip .String ())
5151 shouldRemoveNeighbor = true
5252 }
5353
@@ -62,11 +62,11 @@ func (nm *NeighborManager) AddNeighbor(ip net.IP, linkIndex int) {
6262 }
6363
6464 if err := netutils .AddRoute (ip , linkIndex ); err != nil {
65- log . Printf ("Failed to add route for neighbor %s: %v" , ip .String (), err )
65+ logger . Info ("Failed to add route for neighbor %s: %v" , ip .String (), err )
6666 return
6767 }
6868
69- log . Printf ("Added neighbor %s" , ip .String ())
69+ logger . Info ("Added neighbor %s" , ip .String ())
7070}
7171
7272func (nm * NeighborManager ) RemoveNeighbor (ip net.IP , linkIndex int ) {
@@ -76,7 +76,7 @@ func (nm *NeighborManager) RemoveNeighbor(ip net.IP, linkIndex int) {
7676 for i , n := range nm .reachableNeighbors {
7777 if n .ip .Equal (ip ) && n .linkIndex == linkIndex {
7878 nm .reachableNeighbors = append (nm .reachableNeighbors [:i ], nm .reachableNeighbors [i + 1 :]... )
79- log . Printf ("Removed neighbor %s" , ip .String ())
79+ logger . Info ("Removed neighbor %s" , ip .String ())
8080 shouldRemoveRoute = true
8181 break
8282 }
@@ -85,7 +85,7 @@ func (nm *NeighborManager) RemoveNeighbor(ip net.IP, linkIndex int) {
8585
8686 if shouldRemoveRoute {
8787 if err := netutils .RemoveRoute (ip , linkIndex ); err != nil {
88- log . Printf ("Failed to remove route for neighbor %s: %v" , ip .String (), err )
88+ logger . Info ("Failed to remove route for neighbor %s: %v" , ip .String (), err )
8989 return
9090 }
9191 }
@@ -115,21 +115,21 @@ func (nm *NeighborManager) InitializeNeighborTable() error {
115115 return err
116116 }
117117
118- log . Printf ("Initializing neighbor table with %d neighbors" , len (neighbors ))
118+ logger . Info ("Initializing neighbor table with %d neighbors" , len (neighbors ))
119119
120120 for _ , n := range neighbors {
121121 if n .IP .IsLinkLocalUnicast () {
122- log . Printf ("Skipping link-local neighbor with IP=%s, LinkIndex=%d" , n .IP , n .LinkIndex )
122+ logger . Info ("Skipping link-local neighbor with IP=%s, LinkIndex=%d" , n .IP , n .LinkIndex )
123123 continue
124124 }
125125
126126 if (n .State & (netlink .NUD_REACHABLE | netlink .NUD_STALE )) != 0 && ! nm .isNeighborExternallyLearned (n .Flags ) {
127- log . Printf ("Adding neighbor with IP=%s, LinkIndex=%d" , n .IP , n .LinkIndex )
127+ logger . Info ("Adding neighbor with IP=%s, LinkIndex=%d" , n .IP , n .LinkIndex )
128128 nm .AddNeighbor (n .IP , n .LinkIndex )
129129 }
130130 }
131131
132- log . Printf ("Neighbor table initialized finished" )
132+ logger . Info ("Neighbor table initialized finished" )
133133
134134 return nil
135135}
@@ -140,7 +140,7 @@ func (nm *NeighborManager) MonitorNeighbors() {
140140 defer close (done )
141141
142142 if err := netlink .NeighSubscribe (updates , done ); err != nil {
143- log . Fatalf ("Failed to subscribe to neighbor updates: %v (interface: %s, index: %d)" ,
143+ logger . Error ("Failed to subscribe to neighbor updates: %v (interface: %s, index: %d)" ,
144144 err , nm .targetInterface , nm .targetInterfaceIndex )
145145 }
146146
@@ -153,7 +153,7 @@ func (nm *NeighborManager) MonitorNeighbors() {
153153 continue
154154 }
155155
156- log . Printf ("Received neighbor update: IP=%s, State=%s, Flags=%s, LinkIndex=%d" ,
156+ logger . Debug ("Received neighbor update: IP=%s, State=%s, Flags=%s, LinkIndex=%d" ,
157157 update .Neigh .IP , neighborStateToString (update .Neigh .State ), neighborFlagsToString (update .Neigh .Flags ), update .Neigh .LinkIndex )
158158
159159 if (update .Neigh .State & (netlink .NUD_REACHABLE | netlink .NUD_STALE )) != 0 && ! nm .isNeighborExternallyLearned (update .Neigh .Flags ) {
@@ -180,7 +180,7 @@ func (nm *NeighborManager) SendPings() {
180180 go func (n Neighbor ) {
181181 defer wg .Done ()
182182 if err := netutils .Ping (n .ip .String ()); err != nil {
183- log . Printf ("Failed to ping neighbor %s: %v" , n .ip .String (), err )
183+ logger . Info ("Failed to ping neighbor %s: %v" , n .ip .String (), err )
184184 }
185185 }(n )
186186 }
@@ -196,9 +196,9 @@ func (nm *NeighborManager) Cleanup() {
196196
197197 for _ , n := range nm .reachableNeighbors {
198198 if err := netutils .RemoveRoute (n .ip , n .linkIndex ); err != nil {
199- log . Printf ("Failed to remove route for neighbor %s: %v" , n .ip .String (), err )
199+ logger . Info ("Failed to remove route for neighbor %s: %v" , n .ip .String (), err )
200200 continue
201201 }
202- log . Printf ("Removed route for neighbor %s" , n .ip .String ())
202+ logger . Info ("Removed route for neighbor %s" , n .ip .String ())
203203 }
204204}
0 commit comments