Skip to content
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

Add field for interface description #18

Merged
merged 1 commit into from
Sep 29, 2022
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.19
require github.com/sirupsen/logrus v1.9.0

require (
github.com/test-network-function/l2discovery-exports v0.0.0-20220926190927-5f72a82c96d4
github.com/test-network-function/l2discovery-exports v0.0.0-20220929081721-4e9a42510716
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/test-network-function/l2discovery-exports v0.0.0-20220926190927-5f72a82c96d4 h1:j9+gxKhtniE/BML+3p+CIlUHqQc+/6OmF0BVE+C5fOw=
github.com/test-network-function/l2discovery-exports v0.0.0-20220926190927-5f72a82c96d4/go.mod h1:LXzJLrM5Ao0j4pN/HnlYrBQDpO3TCtlfiK4HmRk2ps8=
github.com/test-network-function/l2discovery-exports v0.0.0-20220929081721-4e9a42510716 h1:BtDYu0HufnnH/V3jaTVtGoa7Ku4+CtGULIgIjomvoPg=
github.com/test-network-function/l2discovery-exports v0.0.0-20220929081721-4e9a42510716/go.mod h1:LXzJLrM5Ao0j4pN/HnlYrBQDpO3TCtlfiK4HmRk2ps8=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
37 changes: 26 additions & 11 deletions l2discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func main() {
select {}
}

func sendProbe(iface exports.Iface) {
func sendProbe(iface *exports.Iface) {
fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, syscall.ETH_P_ALL)
defer syscall.Close(fd)
if err != nil {
Expand Down Expand Up @@ -204,7 +204,7 @@ func sendProbe(iface exports.Iface) {
logrus.Tracef("Sent packet")
}

func RecvFrame(iface exports.Iface, macsExist map[string]bool) {
func RecvFrame(iface *exports.Iface, macsExist map[string]bool) {
const (
recvTimeout = 2
recvBufferSize = 1024
Expand Down Expand Up @@ -238,7 +238,7 @@ func RecvFrame(iface exports.Iface, macsExist map[string]bool) {
MacsPerIface[aFrame.Type] = make(map[string]*exports.Neighbors)
}
if _, ok := MacsPerIface[aFrame.Type][iface.IfName]; !ok {
aNeighbors := exports.Neighbors{Local: iface, Remote: make(map[string]bool)}
aNeighbors := exports.Neighbors{Local: *iface, Remote: make(map[string]bool)}
MacsPerIface[aFrame.Type][iface.IfName] = &aNeighbors
}
if MacsPerIface[aFrame.Type][iface.IfName].Local.IfMac != aFrame.MacSa {
Expand All @@ -250,7 +250,7 @@ func RecvFrame(iface exports.Iface, macsExist map[string]bool) {
}
}

func RecordAllLocal(iface exports.Iface) {
func RecordAllLocal(iface *exports.Iface) {
const (
localInterfaces = "0000"
)
Expand All @@ -259,7 +259,7 @@ func RecordAllLocal(iface exports.Iface) {
MacsPerIface[localInterfaces] = make(map[string]*exports.Neighbors)
}
if _, ok := MacsPerIface[localInterfaces][iface.IfName]; !ok {
aNeighbors := exports.Neighbors{Local: iface, Remote: make(map[string]bool)}
aNeighbors := exports.Neighbors{Local: *iface, Remote: make(map[string]bool)}
MacsPerIface[localInterfaces][iface.IfName] = &aNeighbors
}
mu.Unlock()
Expand All @@ -279,22 +279,22 @@ func PrintLog() {
}
}

func sendProbeForever(iface exports.Iface) {
func sendProbeForever(iface *exports.Iface) {
for {
sendProbe(iface)
time.Sleep(time.Second * 1)
}
}

func getIfs() (macs map[string]exports.Iface, macsExist map[string]bool, err error) {
func getIfs() (macs map[string]*exports.Iface, macsExist map[string]bool, err error) {
const (
ifCommand = "ip -details -json link show"
)
stdout, stderr, err := runLocalCommand(ifCommand)
if err != nil || stderr != "" {
return macs, macsExist, fmt.Errorf("could not execute ip command, err=%s stderr=%s", err, stderr)
}
macs = make(map[string]exports.Iface)
macs = make(map[string]*exports.Iface)
macsExist = make(map[string]bool)
aIPOut := []*ipOut{}
if err := json.Unmarshal([]byte(stdout), &aIPOut); err != nil {
Expand All @@ -308,17 +308,20 @@ func getIfs() (macs map[string]exports.Iface, macsExist map[string]bool, err err
address, _ := getPci(aIfRaw.Ifname)
ptpCaps, _ := getPtpCaps(aIfRaw.Ifname, runLocalCommand)
aIface := exports.Iface{IfName: aIfRaw.Ifname, IfMac: exports.Mac{Data: strings.ToUpper(aIfRaw.Address)}, IfIndex: aIfRaw.Ifindex, IfPci: address, IfPTPCaps: ptpCaps, IfUp: aIfRaw.Operstate == "UP"}
macs[aIfRaw.Ifname] = aIface
macs[aIfRaw.Ifname] = &aIface
macsExist[strings.ToUpper(aIfRaw.Address)] = true
}
return macs, macsExist, nil
}

func getPci(ifaceName string) (aPciAddress exports.PCIAddress, err error) {
const (
ethtoolBaseCommand = "ethtool -i "
ethtoolBaseCommand = "ethtool -i"
lscpiCommand = "lspci -s"
newLineCharacter = "\n"
emptySpaceSeparator = " "
)
aCommand := ethtoolBaseCommand + ifaceName
aCommand := fmt.Sprintf("%s %s", ethtoolBaseCommand, ifaceName)
stdout, stderr, err := RunLocalCommand(aCommand)
if err != nil || stderr != "" {
return aPciAddress, fmt.Errorf("could not execute ethtool command, err=%s stderr=%s", err, stderr)
Expand All @@ -330,6 +333,18 @@ func getPci(ifaceName string) (aPciAddress exports.PCIAddress, err error) {
aPciAddress.Function = string(r.ExpandString([]byte{}, "$2", stdout, submatches))
}

aCommand = fmt.Sprintf("%s %s.%s", lscpiCommand, aPciAddress.Device, aPciAddress.Function)
stdout, stderr, err = RunLocalCommand(aCommand)
if err != nil || stderr != "" {
return aPciAddress, fmt.Errorf("could not execute lspci command, err=%s stderr=%s", err, stderr)
}
stdout = strings.TrimSuffix(stdout, newLineCharacter)
spaceIndex := strings.Index(stdout, emptySpaceSeparator)
if spaceIndex != -1 {
description := stdout[spaceIndex+1:]
aPciAddress.Description = description
}

return aPciAddress, nil
}

Expand Down