This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 673
Move container attachment from sh to Go #2307
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f11d91b
Extract veth creation and LinkSetUp to net package
bboreham d59c170
Remove veth on error
bboreham c458dea
Slight cleanup
bboreham 9f8b946
Obtain IP address before setting up veth
bboreham 9aaa7f1
initialize via sub-function
bboreham 0257274
Extract basic veth setup
bboreham 85023e0
Extract AttachContainer() function
bboreham 4fc2835
Extract AddRoute function
bboreham e3ef473
Utility function to disable tx checksum offload
awh ac14b19
Move attach() and detach() from shell script to Go
bboreham 161446d
Move error detection for attach() into Go
bboreham 8717fd1
Use 'peer' and 'veth' instead of 'guest' and 'local', after review co…
bboreham 2702b85
Improve veth name construction; use a constant instead of "ethwe"
bboreham 6dd109a
Only do `ethtool tx off` in bridged (pcap) mode.
bboreham 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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package common | ||
package net | ||
|
||
import "fmt" | ||
import "io" | ||
|
This file contains 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,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 | ||
} | ||
} |
This file contains 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,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 | ||
} |
This file contains 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,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) | ||
}) | ||
} |
This file contains 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
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.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.