selftests/bpf: fix test_reuseport_array on s390
authorIlya Leoshkevich <iii@linux.ibm.com>
Wed, 3 Jul 2019 11:50:34 +0000 (13:50 +0200)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 8 Jul 2019 23:10:23 +0000 (01:10 +0200)
Fix endianness issue: passing a pointer to 64-bit fd as a 32-bit key
does not work on big-endian architectures. So cast fd to 32-bits when
necessary.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/testing/selftests/bpf/test_maps.c

index a3fbc571280a92b20a00b32cf2ce59b2f662652c..5443b9bd75ed7c2d772a65a73b763f7b3a9ca572 100644 (file)
@@ -1418,7 +1418,7 @@ static void test_map_wronly(void)
        assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
 }
 
-static void prepare_reuseport_grp(int type, int map_fd,
+static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size,
                                  __s64 *fds64, __u64 *sk_cookies,
                                  unsigned int n)
 {
@@ -1428,6 +1428,8 @@ static void prepare_reuseport_grp(int type, int map_fd,
        const int optval = 1;
        unsigned int i;
        u64 sk_cookie;
+       void *value;
+       __s32 fd32;
        __s64 fd64;
        int err;
 
@@ -1449,8 +1451,14 @@ static void prepare_reuseport_grp(int type, int map_fd,
                      "err:%d errno:%d\n", err, errno);
 
                /* reuseport_array does not allow unbound sk */
-               err = bpf_map_update_elem(map_fd, &index0, &fd64,
-                                         BPF_ANY);
+               if (map_elem_size == sizeof(__u64))
+                       value = &fd64;
+               else {
+                       assert(map_elem_size == sizeof(__u32));
+                       fd32 = (__s32)fd64;
+                       value = &fd32;
+               }
+               err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY);
                CHECK(err != -1 || errno != EINVAL,
                      "reuseport array update unbound sk",
                      "sock_type:%d err:%d errno:%d\n",
@@ -1478,7 +1486,7 @@ static void prepare_reuseport_grp(int type, int map_fd,
                         * reuseport_array does not allow
                         * non-listening tcp sk.
                         */
-                       err = bpf_map_update_elem(map_fd, &index0, &fd64,
+                       err = bpf_map_update_elem(map_fd, &index0, value,
                                                  BPF_ANY);
                        CHECK(err != -1 || errno != EINVAL,
                              "reuseport array update non-listening sk",
@@ -1541,7 +1549,7 @@ static void test_reuseport_array(void)
        for (t = 0; t < ARRAY_SIZE(types); t++) {
                type = types[t];
 
-               prepare_reuseport_grp(type, map_fd, grpa_fds64,
+               prepare_reuseport_grp(type, map_fd, sizeof(__u64), grpa_fds64,
                                      grpa_cookies, ARRAY_SIZE(grpa_fds64));
 
                /* Test BPF_* update flags */
@@ -1649,7 +1657,8 @@ static void test_reuseport_array(void)
                                sizeof(__u32), sizeof(__u32), array_size, 0);
        CHECK(map_fd == -1, "reuseport array create",
              "map_fd:%d, errno:%d\n", map_fd, errno);
-       prepare_reuseport_grp(SOCK_STREAM, map_fd, &fd64, &sk_cookie, 1);
+       prepare_reuseport_grp(SOCK_STREAM, map_fd, sizeof(__u32), &fd64,
+                             &sk_cookie, 1);
        fd = fd64;
        err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
        CHECK(err == -1, "reuseport array update 32 bit fd",