Skip to content

Commit

Permalink
Merge branch 'bpf-selftest-endianess-fixes'
Browse files Browse the repository at this point in the history
Ilya Leoshkevich says:

====================
Patch 1 is a preparatory commit, which introduces 64-bit endianness
conversion functions.

Patch 2 fixes reading the wrong byte of an int.

Patch 3 improves error reporting.

Patch 4 uses the new conversion functions to fix wrong endianness of
immediates.

v1->v2: Use bpf_ntohl and bpf_be64_to_cpu, drop __bpf_le64_to_cpu.
v2->v3: Split bpf_be64_to_cpu introduction into a separate patch.
        Use the new functions in test_lwt_seg6local.c and
	test_seg6_loop.c.
v3->v4: Improved commit message, split fixes that are not related to
        each other into separate patches.
====================

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
Daniel Borkmann committed Sep 3, 2019
2 parents 504792e + 3ec2a0e commit 110509d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 61 deletions.
14 changes: 14 additions & 0 deletions tools/testing/selftests/bpf/bpf_endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
# define __bpf_htonl(x) __builtin_bswap32(x)
# define __bpf_constant_ntohl(x) ___constant_swab32(x)
# define __bpf_constant_htonl(x) ___constant_swab32(x)
# define __bpf_be64_to_cpu(x) __builtin_bswap64(x)
# define __bpf_cpu_to_be64(x) __builtin_bswap64(x)
# define __bpf_constant_be64_to_cpu(x) ___constant_swab64(x)
# define __bpf_constant_cpu_to_be64(x) ___constant_swab64(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define __bpf_ntohs(x) (x)
# define __bpf_htons(x) (x)
Expand All @@ -38,6 +42,10 @@
# define __bpf_htonl(x) (x)
# define __bpf_constant_ntohl(x) (x)
# define __bpf_constant_htonl(x) (x)
# define __bpf_be64_to_cpu(x) (x)
# define __bpf_cpu_to_be64(x) (x)
# define __bpf_constant_be64_to_cpu(x) (x)
# define __bpf_constant_cpu_to_be64(x) (x)
#else
# error "Fix your compiler's __BYTE_ORDER__?!"
#endif
Expand All @@ -54,5 +62,11 @@
#define bpf_ntohl(x) \
(__builtin_constant_p(x) ? \
__bpf_constant_ntohl(x) : __bpf_ntohl(x))
#define bpf_cpu_to_be64(x) \
(__builtin_constant_p(x) ? \
__bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x))
#define bpf_be64_to_cpu(x) \
(__builtin_constant_p(x) ? \
__bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x))

#endif /* __BPF_ENDIAN__ */
16 changes: 6 additions & 10 deletions tools/testing/selftests/bpf/progs/test_lwt_seg6local.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

#define SR6_FLAG_ALERT (1 << 4)

#define htonll(x) ((bpf_htonl(1)) == 1 ? (x) : ((uint64_t)bpf_htonl((x) & \
0xFFFFFFFF) << 32) | bpf_htonl((x) >> 32))
#define ntohll(x) ((bpf_ntohl(1)) == 1 ? (x) : ((uint64_t)bpf_ntohl((x) & \
0xFFFFFFFF) << 32) | bpf_ntohl((x) >> 32))
#define BPF_PACKET_HEADER __attribute__((packed))

struct ip6_t {
Expand Down Expand Up @@ -276,8 +272,8 @@ int has_egr_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh)
return 0;

// check if egress TLV value is correct
if (ntohll(egr_addr.hi) == 0xfd00000000000000 &&
ntohll(egr_addr.lo) == 0x4)
if (bpf_be64_to_cpu(egr_addr.hi) == 0xfd00000000000000 &&
bpf_be64_to_cpu(egr_addr.lo) == 0x4)
return 1;
}

Expand Down Expand Up @@ -308,8 +304,8 @@ int __encap_srh(struct __sk_buff *skb)

#pragma clang loop unroll(full)
for (unsigned long long lo = 0; lo < 4; lo++) {
seg->lo = htonll(4 - lo);
seg->hi = htonll(hi);
seg->lo = bpf_cpu_to_be64(4 - lo);
seg->hi = bpf_cpu_to_be64(hi);
seg = (struct ip6_addr_t *)((char *)seg + sizeof(*seg));
}

Expand Down Expand Up @@ -349,8 +345,8 @@ int __add_egr_x(struct __sk_buff *skb)
if (err)
return BPF_DROP;

addr.lo = htonll(lo);
addr.hi = htonll(hi);
addr.lo = bpf_cpu_to_be64(lo);
addr.hi = bpf_cpu_to_be64(hi);
err = bpf_lwt_seg6_action(skb, SEG6_LOCAL_ACTION_END_X,
(void *)&addr, sizeof(addr));
if (err)
Expand Down
8 changes: 2 additions & 6 deletions tools/testing/selftests/bpf/progs/test_seg6_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

#define SR6_FLAG_ALERT (1 << 4)

#define htonll(x) ((bpf_htonl(1)) == 1 ? (x) : ((uint64_t)bpf_htonl((x) & \
0xFFFFFFFF) << 32) | bpf_htonl((x) >> 32))
#define ntohll(x) ((bpf_ntohl(1)) == 1 ? (x) : ((uint64_t)bpf_ntohl((x) & \
0xFFFFFFFF) << 32) | bpf_ntohl((x) >> 32))
#define BPF_PACKET_HEADER __attribute__((packed))

struct ip6_t {
Expand Down Expand Up @@ -251,8 +247,8 @@ int __add_egr_x(struct __sk_buff *skb)
if (err)
return BPF_DROP;

addr.lo = htonll(lo);
addr.hi = htonll(hi);
addr.lo = bpf_cpu_to_be64(lo);
addr.hi = bpf_cpu_to_be64(hi);
err = bpf_lwt_seg6_action(skb, SEG6_LOCAL_ACTION_END_X,
(void *)&addr, sizeof(addr));
if (err)
Expand Down
Loading

0 comments on commit 110509d

Please sign in to comment.