Skip to content

Commit c2f4f55

Browse files
DanielTimLeeAlexei Starovoitov
authored and
Alexei Starovoitov
committed
samples/bpf: replace BPF programs header with net_shared.h
This commit applies "net_shared.h" to BPF programs to remove existing network related header dependencies. Also, this commit removes unnecessary headers before applying "vmlinux.h" to the BPF programs. Mostly, endianness conversion function has been applied to the source. In addition, several macros have been defined to fulfill the INET, TC-related constants. Signed-off-by: Daniel T. Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e69fe84 commit c2f4f55

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

samples/bpf/net_shared.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#ifndef _NET_SHARED_H
33
#define _NET_SHARED_H
44

5+
#define AF_INET 2
6+
#define AF_INET6 10
7+
58
#define ETH_ALEN 6
69
#define ETH_P_802_3_MIN 0x0600
710
#define ETH_P_8021Q 0x8100
@@ -11,6 +14,9 @@
1114
#define ETH_P_ARP 0x0806
1215
#define IPPROTO_ICMPV6 58
1316

17+
#define TC_ACT_OK 0
18+
#define TC_ACT_SHOT 2
19+
1420
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
1521
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1622
#define bpf_ntohs(x) __builtin_bswap16(x)

samples/bpf/sock_flags_kern.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
#include "net_shared.h"
12
#include <uapi/linux/bpf.h>
2-
#include <linux/socket.h>
33
#include <linux/net.h>
44
#include <uapi/linux/in.h>
55
#include <uapi/linux/in6.h>
@@ -17,10 +17,10 @@ int bpf_prog1(struct bpf_sock *sk)
1717
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
1818
bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
1919

20-
/* block PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
20+
/* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
2121
* ie., make ping6 fail
2222
*/
23-
if (sk->family == PF_INET6 &&
23+
if (sk->family == AF_INET6 &&
2424
sk->type == SOCK_DGRAM &&
2525
sk->protocol == IPPROTO_ICMPV6)
2626
return 0;
@@ -35,10 +35,10 @@ int bpf_prog2(struct bpf_sock *sk)
3535

3636
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
3737

38-
/* block PF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
38+
/* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
3939
* ie., make ping fail
4040
*/
41-
if (sk->family == PF_INET &&
41+
if (sk->family == AF_INET &&
4242
sk->type == SOCK_DGRAM &&
4343
sk->protocol == IPPROTO_ICMP)
4444
return 0;

samples/bpf/test_cgrp2_tc_kern.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
* License as published by the Free Software Foundation.
66
*/
77
#define KBUILD_MODNAME "foo"
8-
#include <uapi/linux/if_ether.h>
9-
#include <uapi/linux/in6.h>
8+
#include "net_shared.h"
109
#include <uapi/linux/ipv6.h>
11-
#include <uapi/linux/pkt_cls.h>
1210
#include <uapi/linux/bpf.h>
1311
#include <bpf/bpf_helpers.h>
1412

@@ -42,7 +40,7 @@ int handle_egress(struct __sk_buff *skb)
4240
if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
4341
return TC_ACT_OK;
4442

45-
if (eth->h_proto != htons(ETH_P_IPV6) ||
43+
if (eth->h_proto != bpf_htons(ETH_P_IPV6) ||
4644
ip6h->nexthdr != IPPROTO_ICMPV6) {
4745
bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
4846
eth->h_proto, ip6h->nexthdr);

samples/bpf/test_lwt_bpf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* General Public License for more details.
1111
*/
1212

13+
#include "net_shared.h"
1314
#include <stdint.h>
1415
#include <stddef.h>
1516
#include <linux/bpf.h>
@@ -176,7 +177,7 @@ static inline int __do_push_ll_and_redirect(struct __sk_buff *skb)
176177
printk("skb_change_head() failed: %d", ret);
177178
}
178179

179-
ehdr.h_proto = __constant_htons(ETH_P_IP);
180+
ehdr.h_proto = bpf_htons(ETH_P_IP);
180181
memcpy(&ehdr.h_source, &smac, 6);
181182
memcpy(&ehdr.h_dest, &dmac, 6);
182183

0 commit comments

Comments
 (0)