54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package net
|
|
|
|
import (
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
type BridgeType int
|
|
|
|
const (
|
|
WeaveBridgeName = "weave"
|
|
DatapathName = "datapath"
|
|
|
|
None BridgeType = iota
|
|
Bridge
|
|
Fastdp
|
|
BridgedFastdp
|
|
Inconsistent
|
|
)
|
|
|
|
func DetectBridgeType(weaveBridgeName, datapathName string) BridgeType {
|
|
bridge, _ := netlink.LinkByName(weaveBridgeName)
|
|
datapath, _ := netlink.LinkByName(datapathName)
|
|
|
|
switch {
|
|
case bridge == nil && datapath == nil:
|
|
return None
|
|
case isBridge(bridge) && datapath == nil:
|
|
return Bridge
|
|
case isDatapath(bridge) && datapath == nil:
|
|
return Fastdp
|
|
case isDatapath(datapath) && isBridge(bridge):
|
|
return BridgedFastdp
|
|
default:
|
|
return Inconsistent
|
|
}
|
|
}
|
|
|
|
func isBridge(link netlink.Link) bool {
|
|
_, isBridge := link.(*netlink.Bridge)
|
|
return isBridge
|
|
}
|
|
|
|
func isDatapath(link netlink.Link) bool {
|
|
switch link.(type) {
|
|
case *netlink.GenericLink:
|
|
return link.Type() == "openvswitch"
|
|
case *netlink.Device:
|
|
// Assume it's our openvswitch device, and the kernel has not been updated to report the kind.
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|