From e3ef473cd33817a7f9cbc92bc92d3b6594eed225 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Fri, 5 Feb 2016 14:19:55 +0000 Subject: [PATCH] Utility function to disable tx checksum offload --- net/ethtool.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 net/ethtool.go diff --git a/net/ethtool.go b/net/ethtool.go new file mode 100644 index 00000000..34209f0d --- /dev/null +++ b/net/ethtool.go @@ -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 +}