From 2d69029a837e289dd5bf4976b16b20c7785f2a6f Mon Sep 17 00:00:00 2001 From: Dhammika Pathirana Date: Tue, 24 Jun 2014 08:24:23 -0700 Subject: [PATCH] Add netlink hooks to delete a bridge dev Signed-off-by: Dhammika Pathirana Docker-DCO-1.1-Signed-off-by: Dhammika Pathirana (github: dhammika) --- netlink/netlink_linux.go | 38 ++++++++++++++++++++++++++++++++++ netlink/netlink_unsupported.go | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/netlink/netlink_linux.go b/netlink/netlink_linux.go index 14e30aa0..f5018404 100644 --- a/netlink/netlink_linux.go +++ b/netlink/netlink_linux.go @@ -20,7 +20,9 @@ const ( VETH_INFO_PEER = 1 IFLA_NET_NS_FD = 28 SIOC_BRADDBR = 0x89a0 + SIOC_BRDELBR = 0x89a1 SIOC_BRADDIF = 0x89a2 + SIOC_BRDELIF = 0x89a3 ) var nextSeqNr uint32 @@ -35,6 +37,11 @@ type ifreqIndex struct { IfruIndex int32 } +type ifreqFlags struct { + IfrnName [IFNAMSIZ]byte + Ifruflags uint16 +} + func nativeEndian() binary.ByteOrder { var x uint32 = 0x01020304 if *(*byte)(unsafe.Pointer(&x)) == 0x01 { @@ -921,6 +928,37 @@ func CreateBridge(name string, setMacAddr bool) error { return nil } +// Delete the actual bridge device. +func DeleteBridge(name string) error { + s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_IP) + if err != nil { + // ipv6 issue, creating with ipv4 + s, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_IP) + if err != nil { + return err + } + } + defer syscall.Close(s) + + nameBytePtr, err := syscall.BytePtrFromString(name) + if err != nil { + return err + } + + var ifr ifreqFlags + copy(ifr.IfrnName[:], []byte(name)) + if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), + syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifr))); err != 0 { + return err + } + + if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), + SIOC_BRDELBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 { + return err + } + return nil +} + // Add a slave to abridge device. This is more backward-compatible than // netlink.NetworkSetMaster and works on RHEL 6. func AddToBridge(iface, master *net.Interface) error { diff --git a/netlink/netlink_unsupported.go b/netlink/netlink_unsupported.go index 13593456..67076441 100644 --- a/netlink/netlink_unsupported.go +++ b/netlink/netlink_unsupported.go @@ -67,6 +67,10 @@ func CreateBridge(name string, setMacAddr bool) error { return ErrNotImplemented } +func DeleteBridge(name string) error { + return ErrNotImplemented +} + func AddToBridge(iface, master *net.Interface) error { return ErrNotImplemented }