Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Move container attachment from sh to Go #2307

Merged
merged 14 commits into from
May 26, 2016
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
29 changes: 6 additions & 23 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package common

import (
"fmt"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"net"
"os"
"runtime"
"strings"

"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"

weavenet "github.com/weaveworks/weave/net"
)

// Assert test is true, panic otherwise
Expand All @@ -25,25 +27,6 @@ func ErrorMessages(errors []error) string {
return strings.Join(result, "\n")
}

func WithNetNS(ns netns.NsHandle, work func() error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

oldNs, err := netns.Get()
if err == nil {
defer oldNs.Close()

err = netns.Set(ns)
if err == nil {
defer netns.Set(oldNs)

err = work()
}
}

return err
}

type NetDev struct {
Name string
MAC net.HardwareAddr
Expand All @@ -63,7 +46,7 @@ func FindNetDevs(processID int, match func(link netlink.Link) bool) ([]NetDev, e
}
defer ns.Close()

err = WithNetNS(ns, func() error {
err = weavenet.WithNetNS(ns, func() error {
return forEachLink(func(link netlink.Link) error {
if match(link) {
netDev, err := linkToNetDev(link)
Expand Down
2 changes: 1 addition & 1 deletion common/arp.go → net/arp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package net

import "fmt"
import "io"
Expand Down
53 changes: 53 additions & 0 deletions net/bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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
}
}
52 changes: 52 additions & 0 deletions net/ethtool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net

import "fmt"
import "syscall"
import "unsafe"

const (
SIOCETHTOOL = 0x8946 // linux/sockios.h
ETHTOOL_STXCSUM = 0x00000017 // linux/ethtool.h
IFNAMSIZ = 16 // linux/if.h
)

// linux/if.h 'struct ifreq'
type IFReqData struct {
Name [IFNAMSIZ]byte
Data uintptr
}

// linux/ethtool.h 'struct ethtool_value'
type EthtoolValue struct {
Cmd uint32
Data uint32
}

// Disable TX checksum offload on specified interface
func EthtoolTXOff(name string) error {
if len(name)+1 > IFNAMSIZ {
return fmt.Errorf("name too long")
}

value := EthtoolValue{ETHTOOL_STXCSUM, 0}
request := IFReqData{Data: uintptr(unsafe.Pointer(&value))}

copy(request.Name[:], name)

socket, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return err
}
defer syscall.Close(socket)

_, _, errno := syscall.RawSyscall(syscall.SYS_IOCTL,
uintptr(socket),
uintptr(SIOCETHTOOL),
uintptr(unsafe.Pointer(&request)))

if errno != 0 {
return errno
}

return nil
}
37 changes: 37 additions & 0 deletions net/netns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net

import (
"runtime"

"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)

func WithNetNS(ns netns.NsHandle, work func() error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

oldNs, err := netns.Get()
if err == nil {
defer oldNs.Close()

err = netns.Set(ns)
if err == nil {
defer netns.Set(oldNs)

err = work()
}
}

return err
}

func WithNetNSLink(ns netns.NsHandle, ifName string, work func(link netlink.Link) error) error {
return WithNetNS(ns, func() error {
link, err := netlink.LinkByName(ifName)
if err != nil {
return err
}
return work(link)
})
}
14 changes: 14 additions & 0 deletions net/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net
import (
"fmt"
"net"
"os"

"github.com/vishvananda/netlink"
)
Expand Down Expand Up @@ -55,3 +56,16 @@ func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(r netlink.Rou
}
return nil
}

func AddRoute(link netlink.Link, scope netlink.Scope, dst *net.IPNet, gw net.IP) error {
err := netlink.RouteAdd(&netlink.Route{
LinkIndex: link.Attrs().Index,
Scope: scope,
Dst: dst,
Gw: gw,
})
if os.IsExist(err) { // squash duplicate route errors

This comment was marked as abuse.

This comment was marked as abuse.

err = nil
}
return err
}
Loading