feat(interfaces): add transmission and reception metrics to BaseInterface

This commit is contained in:
2026-01-02 17:48:15 -06:00
parent f80d50c27b
commit 8325666301
2 changed files with 101 additions and 30 deletions

View File

@@ -39,6 +39,10 @@ type NetworkInterface interface {
SendLinkPacket([]byte, []byte, time.Time) error SendLinkPacket([]byte, []byte, time.Time) error
SetPacketCallback(PacketCallback) SetPacketCallback(PacketCallback)
GetPacketCallback() PacketCallback GetPacketCallback() PacketCallback
GetTxBytes() uint64
GetRxBytes() uint64
GetTxPackets() uint64
GetRxPackets() uint64
} }
// BaseInterface provides common implementation for network interfaces // BaseInterface provides common implementation for network interfaces
@@ -56,9 +60,11 @@ type BaseInterface struct {
MTU int MTU int
Bitrate int64 Bitrate int64
TxBytes uint64 TxBytes uint64
RxBytes uint64 RxBytes uint64
lastTx time.Time TxPackets uint64
RxPackets uint64
lastTx time.Time
Mutex sync.RWMutex Mutex sync.RWMutex
Owner interface{} Owner interface{}
@@ -125,6 +131,30 @@ func (i *BaseInterface) GetPacketCallback() PacketCallback {
return i.PacketCallback return i.PacketCallback
} }
func (i *BaseInterface) GetTxBytes() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.TxBytes
}
func (i *BaseInterface) GetRxBytes() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.RxBytes
}
func (i *BaseInterface) GetTxPackets() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.TxPackets
}
func (i *BaseInterface) GetRxPackets() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.RxPackets
}
func (i *BaseInterface) Detach() { func (i *BaseInterface) Detach() {
i.Mutex.Lock() i.Mutex.Lock()
defer i.Mutex.Unlock() defer i.Mutex.Unlock()
@@ -160,10 +190,20 @@ func (i *BaseInterface) GetConn() net.Conn {
} }
func (i *BaseInterface) Send(data []byte, address string) error { func (i *BaseInterface) Send(data []byte, address string) error {
i.Mutex.Lock()
i.TxBytes += uint64(len(data))
i.TxPackets++
i.lastTx = time.Now()
i.Mutex.Unlock()
return i.ProcessOutgoing(data) return i.ProcessOutgoing(data)
} }
func (i *BaseInterface) ProcessIncoming(data []byte) { func (i *BaseInterface) ProcessIncoming(data []byte) {
i.Mutex.Lock()
i.RxBytes += uint64(len(data))
i.RxPackets++
i.Mutex.Unlock()
if i.PacketCallback != nil { if i.PacketCallback != nil {
i.PacketCallback(data, i) i.PacketCallback(data, i)
} }

View File

@@ -56,20 +56,22 @@ type Interface interface {
} }
type BaseInterface struct { type BaseInterface struct {
Name string Name string
Mode common.InterfaceMode Mode common.InterfaceMode
Type common.InterfaceType Type common.InterfaceType
Online bool Online bool
Enabled bool Enabled bool
Detached bool Detached bool
IN bool IN bool
OUT bool OUT bool
MTU int MTU int
Bitrate int64 Bitrate int64
TxBytes uint64 TxBytes uint64
RxBytes uint64 RxBytes uint64
lastTx time.Time TxPackets uint64
lastRx time.Time RxPackets uint64
lastTx time.Time
lastRx time.Time
Mutex sync.RWMutex Mutex sync.RWMutex
packetCallback common.PacketCallback packetCallback common.PacketCallback
@@ -77,18 +79,22 @@ type BaseInterface struct {
func NewBaseInterface(name string, ifType common.InterfaceType, enabled bool) BaseInterface { func NewBaseInterface(name string, ifType common.InterfaceType, enabled bool) BaseInterface {
return BaseInterface{ return BaseInterface{
Name: name, Name: name,
Mode: common.IF_MODE_FULL, Mode: common.IF_MODE_FULL,
Type: ifType, Type: ifType,
Online: false, Online: false,
Enabled: enabled, Enabled: enabled,
Detached: false, Detached: false,
IN: false, IN: false,
OUT: false, OUT: false,
MTU: common.DEFAULT_MTU, MTU: common.DEFAULT_MTU,
Bitrate: BITRATE_MINIMUM, Bitrate: BITRATE_MINIMUM,
lastTx: time.Now(), TxBytes: 0,
lastRx: time.Now(), RxBytes: 0,
TxPackets: 0,
RxPackets: 0,
lastTx: time.Now(),
lastRx: time.Now(),
} }
} }
@@ -107,6 +113,7 @@ func (i *BaseInterface) GetPacketCallback() common.PacketCallback {
func (i *BaseInterface) ProcessIncoming(data []byte) { func (i *BaseInterface) ProcessIncoming(data []byte) {
i.Mutex.Lock() i.Mutex.Lock()
i.RxBytes += uint64(len(data)) i.RxBytes += uint64(len(data))
i.RxPackets++
i.Mutex.Unlock() i.Mutex.Unlock()
i.Mutex.RLock() i.Mutex.RLock()
@@ -126,6 +133,7 @@ func (i *BaseInterface) ProcessOutgoing(data []byte) error {
i.Mutex.Lock() i.Mutex.Lock()
i.TxBytes += uint64(len(data)) i.TxBytes += uint64(len(data))
i.TxPackets++
i.Mutex.Unlock() i.Mutex.Unlock()
debug.Log(debug.DEBUG_VERBOSE, "Interface processed outgoing packet", "name", i.Name, "bytes", len(data), "total_tx", i.TxBytes) debug.Log(debug.DEBUG_VERBOSE, "Interface processed outgoing packet", "name", i.Name, "bytes", len(data), "total_tx", i.TxBytes)
@@ -221,6 +229,30 @@ func (i *BaseInterface) IsDetached() bool {
return i.Detached return i.Detached
} }
func (i *BaseInterface) GetTxBytes() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.TxBytes
}
func (i *BaseInterface) GetRxBytes() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.RxBytes
}
func (i *BaseInterface) GetTxPackets() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.TxPackets
}
func (i *BaseInterface) GetRxPackets() uint64 {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.RxPackets
}
func (i *BaseInterface) Start() error { func (i *BaseInterface) Start() error {
return nil return nil
} }
@@ -272,7 +304,6 @@ func (i *BaseInterface) updateBandwidthStats(bytes uint64) {
i.Mutex.Lock() i.Mutex.Lock()
defer i.Mutex.Unlock() defer i.Mutex.Unlock()
i.TxBytes += bytes
i.lastTx = time.Now() i.lastTx = time.Now()
debug.Log(debug.DEBUG_VERBOSE, "Interface updated bandwidth stats", "name", i.Name, "tx_bytes", i.TxBytes, "last_tx", i.lastTx) debug.Log(debug.DEBUG_VERBOSE, "Interface updated bandwidth stats", "name", i.Name, "tx_bytes", i.TxBytes, "last_tx", i.lastTx)