Skip to content

Commit

Permalink
Merge pull request weaveworks#2504 from weaveworks/check-tx-off
Browse files Browse the repository at this point in the history
Check current tx value before trying to set it
  • Loading branch information
rade authored Sep 8, 2016
2 parents 3d12e80 + c69e140 commit 6a52840
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions net/ethtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "unsafe"

const (
SIOCETHTOOL = 0x8946 // linux/sockios.h
ETHTOOL_GTXCSUM = 0x00000016 // linux/ethtool.h
ETHTOOL_STXCSUM = 0x00000017 // linux/ethtool.h
IFNAMSIZ = 16 // linux/if.h
)
Expand All @@ -22,31 +23,38 @@ type EthtoolValue struct {
Data uint32
}

func ioctlEthtool(fd int, argp uintptr) error {
_, _, errno := syscall.RawSyscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(SIOCETHTOOL), argp)
if errno != 0 {
return errno
}
return nil
}

// 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)))
// Request current value
value := EthtoolValue{Cmd: ETHTOOL_GTXCSUM}
request := IFReqData{Data: uintptr(unsafe.Pointer(&value))}
copy(request.Name[:], name)

if errno != 0 {
return errno
if err := ioctlEthtool(socket, uintptr(unsafe.Pointer(&request))); err != nil {
return err
}
if value.Data == 0 { // if already off, don't try to change
return nil
}

return nil
value = EthtoolValue{ETHTOOL_STXCSUM, 0}
return ioctlEthtool(socket, uintptr(unsafe.Pointer(&request)))
}

0 comments on commit 6a52840

Please sign in to comment.