-
Notifications
You must be signed in to change notification settings - Fork 269
feat: lldp #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ym
wants to merge
15
commits into
dev
Choose a base branch
from
feat/lldp
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: lldp #948
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f0595ff
fix: await sleep needs to be called inside async function
ym 366f7f3
feat: add LLDP support
ym fd44ff4
feat: add LLDP transmit support
ym 3e39361
feat: add LLDP neighbors store and update network settings
ym 9f4acce
fix: crash when stopping LLDP receiver
ym 03f781f
chore: skip TLV end check for LLDP shutdown frame
ym 621be3c
fix: PR issues
ym b49d67c
fix: RX goroutine cleanup
ym 19bc958
chore: update submit button loading state
ym 8957a65
feat(lldp): support multiple management address TLVs
ym 15484f8
fix(ui): use source in LLDP neighbor key
ym 826e515
fix(lldp): use mutexes to protect state
ym 1e51842
fix(lldp): golangci-lint issues
ym 7ea59ba
refactor: restructures LLDP service for safer state management (#961)
IDisposable d295ccb
Merge branch 'dev' into feat/lldp
IDisposable File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package lldp | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "os" | ||
| "syscall" | ||
| "unsafe" | ||
|
|
||
| "github.com/google/gopacket/afpacket" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| const ( | ||
| afPacketBufferSize = 2 // in MiB | ||
| afPacketSnaplen = 9216 | ||
| ) | ||
|
|
||
| func afPacketComputeSize( | ||
| targetSizeMb int, | ||
| snaplen int, | ||
| pageSize int, | ||
| ) ( | ||
| frameSize int, | ||
| blockSize int, | ||
| numBlocks int, | ||
| err error, | ||
| ) { | ||
| if snaplen < pageSize { | ||
| frameSize = pageSize / (pageSize / snaplen) | ||
| } else { | ||
| frameSize = (snaplen/pageSize + 1) * pageSize | ||
| } | ||
|
|
||
| // 128 is the default from the gopacket library so just use that | ||
| blockSize = frameSize * 128 | ||
| numBlocks = (targetSizeMb * 1024 * 1024) / blockSize | ||
|
|
||
| if numBlocks == 0 { | ||
| return 0, 0, 0, fmt.Errorf("interface buffersize is too small") | ||
| } | ||
|
|
||
| return frameSize, blockSize, numBlocks, nil | ||
| } | ||
|
|
||
| func afPacketNewTPacket(ifName string) (*afpacket.TPacket, error) { | ||
| szFrame, szBlock, numBlocks, err := afPacketComputeSize( | ||
| afPacketBufferSize, | ||
| afPacketSnaplen, | ||
| os.Getpagesize(), | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return afpacket.NewTPacket( | ||
| afpacket.OptInterface(ifName), | ||
| afpacket.OptFrameSize(szFrame), | ||
| afpacket.OptBlockSize(szBlock), | ||
| afpacket.OptNumBlocks(numBlocks), | ||
| afpacket.OptAddVLANHeader(false), | ||
| afpacket.SocketRaw, | ||
| afpacket.TPacketVersion3, | ||
| ) | ||
| } | ||
|
|
||
| type ifreq struct { | ||
| ifrName [IFNAMSIZ]byte | ||
| ifrHwaddr syscall.RawSockaddr | ||
| } | ||
|
|
||
| func addMulticastAddr(ifName string, addr net.HardwareAddr) error { | ||
| fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer syscall.Close(fd) | ||
|
|
||
| var name [IFNAMSIZ]byte | ||
| copy(name[:], []byte(ifName)) | ||
|
|
||
| ifr := &ifreq{ | ||
| ifrName: name, | ||
| ifrHwaddr: toRawSockaddr(addr), | ||
| } | ||
|
|
||
| _, _, ep := unix.Syscall( | ||
| unix.SYS_IOCTL, uintptr(fd), | ||
| unix.SIOCADDMULTI, uintptr(unsafe.Pointer(ifr)), | ||
| ) | ||
|
|
||
| if ep != 0 { | ||
| return syscall.Errno(ep) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //go:build arm && linux | ||
|
|
||
| package lldp | ||
|
|
||
| import ( | ||
| "net" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func toRawSockaddr(mac net.HardwareAddr) (sockaddr syscall.RawSockaddr) { | ||
| for i, n := range mac { | ||
| sockaddr.Data[i] = uint8(n) | ||
| } | ||
| return | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //go:build !arm && linux | ||
|
|
||
| package lldp | ||
|
|
||
| import ( | ||
| "net" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func toRawSockaddr(mac net.HardwareAddr) (sockaddr syscall.RawSockaddr) { | ||
| for i, n := range mac { | ||
| sockaddr.Data[i] = int8(n) | ||
| } | ||
| return | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package lldp | ||
|
|
||
| import "golang.org/x/net/bpf" | ||
|
|
||
| // from lldpd | ||
| // https://github.com/lldpd/lldpd/blob/9034c9332cca0c8b1a20e1287f0e5fed81f7eb2a/src/daemon/lldpd.h#L246 | ||
| var bpfFilter = []bpf.RawInstruction{ | ||
| {0x30, 0, 0, 0x00000000}, {0x54, 0, 0, 0x00000001}, {0x15, 0, 16, 0x00000001}, | ||
| {0x28, 0, 0, 0x0000000c}, {0x15, 0, 6, 0x000088cc}, | ||
| {0x20, 0, 0, 0x00000002}, {0x15, 2, 0, 0xc200000e}, | ||
| {0x15, 1, 0, 0xc2000003}, {0x15, 0, 2, 0xc2000000}, | ||
| {0x28, 0, 0, 0x00000000}, {0x15, 12, 13, 0x00000180}, | ||
| {0x20, 0, 0, 0x00000002}, {0x15, 0, 2, 0x52cccccc}, | ||
| {0x28, 0, 0, 0x00000000}, {0x15, 8, 9, 0x000001e0}, | ||
| {0x15, 1, 0, 0x0ccccccc}, {0x15, 0, 2, 0x81000100}, | ||
| {0x28, 0, 0, 0x00000000}, {0x15, 4, 5, 0x00000100}, | ||
| {0x20, 0, 0, 0x00000002}, {0x15, 0, 3, 0x2b000000}, | ||
| {0x28, 0, 0, 0x00000000}, {0x15, 0, 1, 0x000000e0}, | ||
| {0x6, 0, 0, 0x00040000}, | ||
| {0x6, 0, 0, 0x00000000}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package lldp | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/google/gopacket" | ||
| "github.com/google/gopacket/afpacket" | ||
| "github.com/jellydator/ttlcache/v3" | ||
| "github.com/jetkvm/kvm/internal/logging" | ||
| "github.com/rs/zerolog" | ||
| ) | ||
|
|
||
| var defaultLogger = logging.GetSubsystemLogger("lldp") | ||
|
|
||
| type LLDP struct { | ||
| l *zerolog.Logger | ||
| tPacket *afpacket.TPacket | ||
| pktSource *gopacket.PacketSource | ||
|
|
||
| enableRx bool | ||
| enableTx bool | ||
|
|
||
| packets chan gopacket.Packet | ||
|
Check failure on line 23 in internal/lldp/lldp.go
|
||
| interfaceName string | ||
| stop chan struct{} | ||
|
Check failure on line 25 in internal/lldp/lldp.go
|
||
| onChange func(neighbors []Neighbor) | ||
|
Check failure on line 26 in internal/lldp/lldp.go
|
||
|
|
||
| neighbors *ttlcache.Cache[string, Neighbor] | ||
| } | ||
|
|
||
| type LLDPOptions struct { | ||
| InterfaceName string | ||
| EnableRx bool | ||
| EnableTx bool | ||
| OnChange func(neighbors []Neighbor) | ||
| Logger *zerolog.Logger | ||
| } | ||
|
|
||
| func NewLLDP(opts *LLDPOptions) *LLDP { | ||
| if opts.Logger == nil { | ||
| opts.Logger = defaultLogger | ||
| } | ||
|
|
||
| if opts.InterfaceName == "" { | ||
| opts.Logger.Fatal().Msg("InterfaceName is required") | ||
| } | ||
|
|
||
| return &LLDP{ | ||
| interfaceName: opts.InterfaceName, | ||
| enableRx: opts.EnableRx, | ||
| enableTx: opts.EnableTx, | ||
| l: opts.Logger, | ||
| neighbors: ttlcache.New(ttlcache.WithTTL[string, Neighbor](1 * time.Hour)), | ||
| } | ||
| } | ||
|
|
||
| func (l *LLDP) Start() error { | ||
| if l.enableRx { | ||
| l.l.Info().Msg("setting up AF_PACKET") | ||
| if err := l.setUpCapture(); err != nil { | ||
| l.l.Error().Err(err).Msg("unable to set up AF_PACKET") | ||
| return err | ||
| } | ||
|
|
||
| if err := l.startCapture(); err != nil { | ||
| l.l.Error().Err(err).Msg("unable to start capture") | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| go l.neighbors.Start() | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package lldp | ||
|
|
||
| import "time" | ||
|
|
||
| type Neighbor struct { | ||
| Mac string `json:"mac"` | ||
| Source string `json:"source"` | ||
| ChassisID string `json:"chassis_id"` | ||
| PortID string `json:"port_id"` | ||
| PortDescription string `json:"port_description"` | ||
| SystemName string `json:"system_name"` | ||
| SystemDescription string `json:"system_description"` | ||
| TTL uint16 `json:"ttl"` | ||
| ManagementAddress string `json:"management_address"` | ||
| Values map[string]string `json:"values"` | ||
| } | ||
|
|
||
| func (l *LLDP) addNeighbor(mac string, neighbor Neighbor, ttl time.Duration) { | ||
| logger := l.l.With(). | ||
| Str("mac", mac). | ||
| Interface("neighbor", neighbor). | ||
| Logger() | ||
|
|
||
| current_neigh := l.neighbors.Get(mac) | ||
| if current_neigh != nil { | ||
| current_source := current_neigh.Value().Source | ||
| if current_source == "lldp" && neighbor.Source != "lldp" { | ||
| logger.Info().Msg("skip updating neighbor, as LLDP has higher priority") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| logger.Info().Msg("adding neighbor") | ||
| l.neighbors.Set(mac, neighbor, ttl) | ||
| } | ||
|
|
||
| func (l *LLDP) deleteNeighbor(mac string) { | ||
| logger := l.l.With(). | ||
| Str("mac", mac). | ||
| Logger() | ||
|
|
||
| logger.Info().Msg("deleting neighbor") | ||
| l.neighbors.Delete(mac) | ||
| } | ||
|
|
||
| func (l *LLDP) GetNeighbors() []Neighbor { | ||
| items := l.neighbors.Items() | ||
| neighbors := make([]Neighbor, 0, len(items)) | ||
|
|
||
| for _, item := range items { | ||
| neighbors = append(neighbors, item.Value()) | ||
| } | ||
|
|
||
| return neighbors | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.