Skip to content

Commit c99a84e

Browse files
Zashasborkmann
authored andcommitted
selftests/bpf: test for seg6local End.BPF action
Add a new test for the seg6local End.BPF action. The following helpers are also tested: - bpf_lwt_push_encap within the LWT BPF IN hook - bpf_lwt_seg6_action - bpf_lwt_seg6_adjust_srh - bpf_lwt_seg6_store_bytes A chain of End.BPF actions is built. The SRH is injected through a LWT BPF IN hook before entering this chain. Each End.BPF action validates the previous one, otherwise the packet is dropped. The test succeeds if the last node in the chain receives the packet and the UDP datagram contained can be retrieved from userspace. Signed-off-by: Mathieu Xhonneux <m.xhonneux@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 004d4b2 commit c99a84e

File tree

5 files changed

+689
-3
lines changed

5 files changed

+689
-3
lines changed

tools/include/uapi/linux/bpf.h

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ enum bpf_prog_type {
141141
BPF_PROG_TYPE_SK_MSG,
142142
BPF_PROG_TYPE_RAW_TRACEPOINT,
143143
BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
144+
BPF_PROG_TYPE_LWT_SEG6LOCAL,
144145
};
145146

146147
enum bpf_attach_type {
@@ -1902,6 +1903,90 @@ union bpf_attr {
19021903
* egress otherwise). This is the only flag supported for now.
19031904
* Return
19041905
* **SK_PASS** on success, or **SK_DROP** on error.
1906+
*
1907+
* int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
1908+
* Description
1909+
* Encapsulate the packet associated to *skb* within a Layer 3
1910+
* protocol header. This header is provided in the buffer at
1911+
* address *hdr*, with *len* its size in bytes. *type* indicates
1912+
* the protocol of the header and can be one of:
1913+
*
1914+
* **BPF_LWT_ENCAP_SEG6**
1915+
* IPv6 encapsulation with Segment Routing Header
1916+
* (**struct ipv6_sr_hdr**). *hdr* only contains the SRH,
1917+
* the IPv6 header is computed by the kernel.
1918+
* **BPF_LWT_ENCAP_SEG6_INLINE**
1919+
* Only works if *skb* contains an IPv6 packet. Insert a
1920+
* Segment Routing Header (**struct ipv6_sr_hdr**) inside
1921+
* the IPv6 header.
1922+
*
1923+
* A call to this helper is susceptible to change the underlaying
1924+
* packet buffer. Therefore, at load time, all checks on pointers
1925+
* previously done by the verifier are invalidated and must be
1926+
* performed again, if the helper is used in combination with
1927+
* direct packet access.
1928+
* Return
1929+
* 0 on success, or a negative error in case of failure.
1930+
*
1931+
* int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len)
1932+
* Description
1933+
* Store *len* bytes from address *from* into the packet
1934+
* associated to *skb*, at *offset*. Only the flags, tag and TLVs
1935+
* inside the outermost IPv6 Segment Routing Header can be
1936+
* modified through this helper.
1937+
*
1938+
* A call to this helper is susceptible to change the underlaying
1939+
* packet buffer. Therefore, at load time, all checks on pointers
1940+
* previously done by the verifier are invalidated and must be
1941+
* performed again, if the helper is used in combination with
1942+
* direct packet access.
1943+
* Return
1944+
* 0 on success, or a negative error in case of failure.
1945+
*
1946+
* int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta)
1947+
* Description
1948+
* Adjust the size allocated to TLVs in the outermost IPv6
1949+
* Segment Routing Header contained in the packet associated to
1950+
* *skb*, at position *offset* by *delta* bytes. Only offsets
1951+
* after the segments are accepted. *delta* can be as well
1952+
* positive (growing) as negative (shrinking).
1953+
*
1954+
* A call to this helper is susceptible to change the underlaying
1955+
* packet buffer. Therefore, at load time, all checks on pointers
1956+
* previously done by the verifier are invalidated and must be
1957+
* performed again, if the helper is used in combination with
1958+
* direct packet access.
1959+
* Return
1960+
* 0 on success, or a negative error in case of failure.
1961+
*
1962+
* int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len)
1963+
* Description
1964+
* Apply an IPv6 Segment Routing action of type *action* to the
1965+
* packet associated to *skb*. Each action takes a parameter
1966+
* contained at address *param*, and of length *param_len* bytes.
1967+
* *action* can be one of:
1968+
*
1969+
* **SEG6_LOCAL_ACTION_END_X**
1970+
* End.X action: Endpoint with Layer-3 cross-connect.
1971+
* Type of *param*: **struct in6_addr**.
1972+
* **SEG6_LOCAL_ACTION_END_T**
1973+
* End.T action: Endpoint with specific IPv6 table lookup.
1974+
* Type of *param*: **int**.
1975+
* **SEG6_LOCAL_ACTION_END_B6**
1976+
* End.B6 action: Endpoint bound to an SRv6 policy.
1977+
* Type of param: **struct ipv6_sr_hdr**.
1978+
* **SEG6_LOCAL_ACTION_END_B6_ENCAP**
1979+
* End.B6.Encap action: Endpoint bound to an SRv6
1980+
* encapsulation policy.
1981+
* Type of param: **struct ipv6_sr_hdr**.
1982+
*
1983+
* A call to this helper is susceptible to change the underlaying
1984+
* packet buffer. Therefore, at load time, all checks on pointers
1985+
* previously done by the verifier are invalidated and must be
1986+
* performed again, if the helper is used in combination with
1987+
* direct packet access.
1988+
* Return
1989+
* 0 on success, or a negative error in case of failure.
19051990
*/
19061991
#define __BPF_FUNC_MAPPER(FN) \
19071992
FN(unspec), \
@@ -1976,7 +2061,11 @@ union bpf_attr {
19762061
FN(fib_lookup), \
19772062
FN(sock_hash_update), \
19782063
FN(msg_redirect_hash), \
1979-
FN(sk_redirect_hash),
2064+
FN(sk_redirect_hash), \
2065+
FN(lwt_push_encap), \
2066+
FN(lwt_seg6_store_bytes), \
2067+
FN(lwt_seg6_adjust_srh), \
2068+
FN(lwt_seg6_action),
19802069

19812070
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
19822071
* function eBPF program intends to call
@@ -2043,6 +2132,12 @@ enum bpf_hdr_start_off {
20432132
BPF_HDR_START_NET,
20442133
};
20452134

2135+
/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */
2136+
enum bpf_lwt_encap_mode {
2137+
BPF_LWT_ENCAP_SEG6,
2138+
BPF_LWT_ENCAP_SEG6_INLINE
2139+
};
2140+
20462141
/* user accessible mirror of in-kernel sk_buff.
20472142
* new fields can only be added to the end of this structure
20482143
*/

tools/testing/selftests/bpf/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
3333
sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
3434
sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \
3535
test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \
36-
test_get_stack_rawtp.o test_sockmap_kern.o test_sockhash_kern.o
36+
test_get_stack_rawtp.o test_sockmap_kern.o test_sockhash_kern.o \
37+
test_lwt_seg6local.o
3738

3839
# Order correspond to 'make run_tests' order
3940
TEST_PROGS := test_kmod.sh \
@@ -42,7 +43,8 @@ TEST_PROGS := test_kmod.sh \
4243
test_xdp_meta.sh \
4344
test_offload.py \
4445
test_sock_addr.sh \
45-
test_tunnel.sh
46+
test_tunnel.sh \
47+
test_lwt_seg6local.sh
4648

4749
# Compile but not part of 'make run_tests'
4850
TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =
114114
static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params,
115115
int plen, __u32 flags) =
116116
(void *) BPF_FUNC_fib_lookup;
117+
static int (*bpf_lwt_push_encap)(void *ctx, unsigned int type, void *hdr,
118+
unsigned int len) =
119+
(void *) BPF_FUNC_lwt_push_encap;
120+
static int (*bpf_lwt_seg6_store_bytes)(void *ctx, unsigned int offset,
121+
void *from, unsigned int len) =
122+
(void *) BPF_FUNC_lwt_seg6_store_bytes;
123+
static int (*bpf_lwt_seg6_action)(void *ctx, unsigned int action, void *param,
124+
unsigned int param_len) =
125+
(void *) BPF_FUNC_lwt_seg6_action;
126+
static int (*bpf_lwt_seg6_adjust_srh)(void *ctx, unsigned int offset,
127+
unsigned int len) =
128+
(void *) BPF_FUNC_lwt_seg6_adjust_srh;
117129

118130
/* llvm builtin functions that eBPF C program may use to
119131
* emit BPF_LD_ABS and BPF_LD_IND instructions

0 commit comments

Comments
 (0)