]> git.openfabrics.org - ~shefty/libibverbs.git/commitdiff
Include htonll() and nothll() in <infiniband/arch.h>
authorRoland Dreier <rolandd@cisco.com>
Wed, 7 Sep 2005 20:01:31 +0000 (20:01 +0000)
committerRoland Dreier <rolandd@cisco.com>
Thu, 9 Nov 2006 19:35:57 +0000 (11:35 -0800)
Move htonll() and ntohll() from libmthca into libibverb's
<infiniband/arch.h>, and use them in ibv_get_device_guid() to
avoid pointer aliasing (which some versions of gcc miscompile).

Signed-off-by: Roland Dreier <rolandd@cisco.com>
ChangeLog
include/infiniband/arch.h
src/device.c

index 41626e9db3b7f9d818caca0c48d13329b68d915d..8e0f6901129529d2a1762b341cfb21a26ca8675d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2005-09-07  Roland Dreier  <roland@cisco.com>
+
+       * src/device.c (ibv_get_device_guid): Use htonll() instead of
+       relying on pointer aliasing (which seems to break for some gcc
+       versions).
+
+       * include/infiniband/arch.h: Add htonll() and ntohll() functions.
+
 2005-09-06  Roland Dreier  <roland@cisco.com>
 
        * include/infiniband/kern-abi.h, include/infiniband/verbs.h,
index b582d18383c12f9958cffbd6e7f991c36b484be9..e6eb8c8cf3cf122caae5caa4ce003974a64e21f8 100644 (file)
 #ifndef INFINIBAND_ARCH_H
 #define INFINIBAND_ARCH_H
 
+#include <endian.h>
+#include <byteswap.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+static inline uint64_t htonll(uint64_t x) { return bswap_64(x); }
+static inline uint64_t ntohll(uint64_t x) { return bswap_64(x); }
+#elif __BYTE_ORDER == __BIG_ENDIAN
+static inline uint64_t htonll(uint64_t x) { return x; }
+static inline uint64_t ntohll(uint64_t x) { return x; }
+#endif
+
 /*
  * Architecture-specific defines.  Currently, an architecture is
  * required to implement the following operations:
index 19a8c988bffb128b02437c2c2d47f113e14fe35a..82afcdd35063408a465ba1d24d987d9cf65f869d 100644 (file)
@@ -44,6 +44,8 @@
 #include <unistd.h>
 #include <alloca.h>
 
+#include <infiniband/arch.h>
+
 #include "ibverbs.h"
 
 static struct dlist *device_list;
@@ -63,7 +65,8 @@ const char *ibv_get_device_name(struct ibv_device *device)
 uint64_t ibv_get_device_guid(struct ibv_device *device)
 {
        struct sysfs_attribute *attr;
-       uint16_t guid[4];
+       uint64_t guid = 0;
+       uint16_t parts[4];
        int i;
 
        attr = sysfs_get_classdev_attr(device->ibdev, "node_guid");
@@ -71,13 +74,13 @@ uint64_t ibv_get_device_guid(struct ibv_device *device)
                return 0;
 
        if (sscanf(attr->value, "%hx:%hx:%hx:%hx",
-                  guid, guid + 1, guid + 2, guid + 3) != 4)
+                  parts, parts + 1, parts + 2, parts + 3) != 4)
                return 0;
 
        for (i = 0; i < 4; ++i)
-               guid[i] = htons(guid[i]);
+               guid = (guid << 16) | parts[i];
 
-       return *(uint64_t *) guid;
+       return htonll(guid);
 }
 
 struct ibv_context *ibv_open_device(struct ibv_device *device)