Browse Source

Merge pull request #7199 from heyitsanthony/netutil-test-arch

pkg/netutil: use native byte ordering
Anthony Romano 9 years ago
parent
commit
5aab92414f
4 changed files with 58 additions and 7 deletions
  1. 16 0
      pkg/cpuutil/doc.go
  2. 36 0
      pkg/cpuutil/endian.go
  3. 1 1
      pkg/netutil/routes.go
  4. 5 6
      pkg/netutil/routes_linux.go

+ 16 - 0
pkg/cpuutil/doc.go

@@ -0,0 +1,16 @@
+// Copyright 2017 The etcd Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package cpuutil provides facilities for detecting cpu-specific features.
+package cpuutil

+ 36 - 0
pkg/cpuutil/endian.go

@@ -0,0 +1,36 @@
+// Copyright 2017 The etcd Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cpuutil
+
+import (
+	"encoding/binary"
+	"unsafe"
+)
+
+const intWidth int = int(unsafe.Sizeof(0))
+
+var byteOrder binary.ByteOrder
+
+// ByteOrder returns the byte order for the CPU's native endianness.
+func ByteOrder() binary.ByteOrder { return byteOrder }
+
+func init() {
+	var i int = 0x1
+	if v := (*[intWidth]byte)(unsafe.Pointer(&i)); v[0] == 0 {
+		byteOrder = binary.BigEndian
+	} else {
+		byteOrder = binary.LittleEndian
+	}
+}

+ 1 - 1
pkg/netutil/routes.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // See the License for the specific language governing permissions and
 // limitations under the License.
 // limitations under the License.
 
 
-// +build !linux !386,!amd64
+// +build !linux
 
 
 package netutil
 package netutil
 
 

+ 5 - 6
pkg/netutil/routes_linux.go

@@ -13,9 +13,6 @@
 // limitations under the License.
 // limitations under the License.
 
 
 // +build linux
 // +build linux
-// +build 386 amd64
-
-// TODO support native endian but without using "unsafe"
 
 
 package netutil
 package netutil
 
 
@@ -25,6 +22,8 @@ import (
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 	"syscall"
 	"syscall"
+
+	"github.com/coreos/etcd/pkg/cpuutil"
 )
 )
 
 
 var errNoDefaultRoute = fmt.Errorf("could not find default route")
 var errNoDefaultRoute = fmt.Errorf("could not find default route")
@@ -80,7 +79,7 @@ func getDefaultRoute() (*syscall.NetlinkMessage, error) {
 			continue
 			continue
 		}
 		}
 		buf := bytes.NewBuffer(m.Data[:syscall.SizeofRtMsg])
 		buf := bytes.NewBuffer(m.Data[:syscall.SizeofRtMsg])
-		if rerr := binary.Read(buf, binary.LittleEndian, &rtmsg); rerr != nil {
+		if rerr := binary.Read(buf, cpuutil.ByteOrder(), &rtmsg); rerr != nil {
 			continue
 			continue
 		}
 		}
 		if rtmsg.Dst_len == 0 {
 		if rtmsg.Dst_len == 0 {
@@ -109,7 +108,7 @@ func getIface(idx uint32) (*syscall.NetlinkMessage, error) {
 			continue
 			continue
 		}
 		}
 		buf := bytes.NewBuffer(m.Data[:syscall.SizeofIfAddrmsg])
 		buf := bytes.NewBuffer(m.Data[:syscall.SizeofIfAddrmsg])
-		if rerr := binary.Read(buf, binary.LittleEndian, &ifaddrmsg); rerr != nil {
+		if rerr := binary.Read(buf, cpuutil.ByteOrder(), &ifaddrmsg); rerr != nil {
 			continue
 			continue
 		}
 		}
 		if ifaddrmsg.Index == idx {
 		if ifaddrmsg.Index == idx {
@@ -164,7 +163,7 @@ func parsePREFSRC(m *syscall.NetlinkMessage) (host string, oif uint32, err error
 			host = net.IP(attr.Value).String()
 			host = net.IP(attr.Value).String()
 		}
 		}
 		if attr.Attr.Type == syscall.RTA_OIF {
 		if attr.Attr.Type == syscall.RTA_OIF {
-			oif = binary.LittleEndian.Uint32(attr.Value)
+			oif = cpuutil.ByteOrder().Uint32(attr.Value)
 		}
 		}
 		if host != "" && oif != uint32(0) {
 		if host != "" && oif != uint32(0) {
 			break
 			break