Branch data Line data Source code
1 : : /* SPDX-License-Identifier: GPL-2.0-only */
2 : : /*
3 : : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : : */
5 : :
6 : : #include "cgen/xdp.h"
7 : :
8 : : #include <linux/bpf.h>
9 : : #include <linux/bpf_common.h>
10 : :
11 : : #include <stddef.h>
12 : : #include <stdint.h>
13 : :
14 : : #include <bpfilter/flavor.h>
15 : : #include <bpfilter/helper.h>
16 : : #include <bpfilter/logger.h>
17 : : #include <bpfilter/verdict.h>
18 : :
19 : : #include "cgen/codegen.h"
20 : : #include "cgen/packet.h"
21 : : #include "cgen/stub.h"
22 : : #include "filter.h"
23 : :
24 : : /**
25 : : * Generate XDP program prologue.
26 : : *
27 : : * @param program Program to generate the prologue for. Must not be NULL.
28 : : * @return 0 on success, or negative errno value on error.
29 : : */
30 : 222 : static int _bf_xdp_gen_inline_prologue(struct bf_codegen *program)
31 : : {
32 : : int r;
33 : :
34 : : assert(program);
35 : :
36 : : // Calculate the packet size and store it into the runtime context
37 [ + - ]: 222 : EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
38 : : offsetof(struct xdp_md, data)));
39 [ + - ]: 222 : EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
40 : : offsetof(struct xdp_md, data_end)));
41 [ + - ]: 222 : EMIT(program, BPF_ALU64_REG(BPF_SUB, BPF_REG_3, BPF_REG_2));
42 [ + - ]: 222 : EMIT(program,
43 : : BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_3, BF_PROG_CTX_OFF(pkt_size)));
44 : :
45 : : // Store the ingress ifindex into the runtime context
46 [ + - ]: 222 : EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
47 : : offsetof(struct xdp_md, ingress_ifindex)));
48 [ + - ]: 222 : EMIT(program,
49 : : BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, BF_PROG_CTX_OFF(ifindex)));
50 : :
51 : 222 : r = bf_stub_make_ctx_xdp_dynptr(program, BPF_REG_1);
52 [ + - ]: 222 : if (r)
53 : : return r;
54 : :
55 : 222 : r = bf_stub_parse_l2_ethhdr(program);
56 [ + - ]: 222 : if (r)
57 : : return r;
58 : :
59 : 222 : r = bf_stub_parse_l3_hdr(program);
60 [ + - ]: 222 : if (r)
61 : : return r;
62 : :
63 : 222 : r = bf_stub_parse_l4_hdr(program);
64 : : if (r)
65 : : return r;
66 : :
67 : : return 0;
68 : : }
69 : :
70 : 221 : static int _bf_xdp_gen_inline_epilogue(struct bf_codegen *program)
71 : : {
72 : : (void)program;
73 : :
74 : 221 : return 0;
75 : : }
76 : :
77 : : /**
78 : : * @brief Generate bytecode to redirect a packet using XDP.
79 : : *
80 : : * XDP redirect only supports egress direction - the packet is always
81 : : * transmitted out of the target interface. The BPF_F_INGRESS flag is
82 : : * ignored by XDP's bpf_redirect().
83 : : *
84 : : * @param program Program to generate bytecode for. Can't be NULL.
85 : : * @param ifindex Target interface index.
86 : : * @param dir Direction (must be BF_REDIRECT_EGRESS for XDP).
87 : : * @return 0 on success, or a negative errno value on failure.
88 : : */
89 : 4 : static int _bf_xdp_gen_inline_redirect(struct bf_codegen *program,
90 : : uint32_t ifindex,
91 : : enum bf_redirect_dir dir)
92 : : {
93 : : assert(program);
94 : :
95 [ + + ]: 4 : if (dir != BF_REDIRECT_EGRESS)
96 [ + - ]: 1 : return bf_err_r(-ENOTSUP, "XDP redirect only supports 'out' direction");
97 : :
98 : : // bpf_redirect(ifindex, flags) - flags are ignored for XDP
99 [ + - ]: 3 : EMIT(program, BPF_MOV64_IMM(BPF_REG_1, ifindex));
100 [ + - ]: 3 : EMIT(program, BPF_MOV64_IMM(BPF_REG_2, 0));
101 [ + - ]: 3 : EMIT(program, BPF_EMIT_CALL(BPF_FUNC_redirect));
102 : :
103 : : // Return value from bpf_redirect() is the action (XDP_REDIRECT on success)
104 : 3 : EMIT(program, BPF_EXIT_INSN());
105 : :
106 : : return 0;
107 : : }
108 : :
109 : : /**
110 : : * Convert a standard verdict into a return value.
111 : : *
112 : : * @param verdict Verdict to convert. Must be valid.
113 : : * @param ret_code XDP return code. Can't be NULL.
114 : : * @return 0 on success, or a negative errno value on failure.
115 : : */
116 : 1335 : static int _bf_xdp_get_verdict(enum bf_verdict verdict, int *ret_code)
117 : : {
118 : : assert(ret_code);
119 : :
120 [ + + - ]: 1335 : switch (verdict) {
121 : 908 : case BF_VERDICT_ACCEPT:
122 : : case BF_VERDICT_NEXT:
123 : 908 : *ret_code = XDP_PASS;
124 : 908 : return 0;
125 : 427 : case BF_VERDICT_DROP:
126 : 427 : *ret_code = XDP_DROP;
127 : 427 : return 0;
128 : : default:
129 : : return -ENOTSUP;
130 : : }
131 : : }
132 : :
133 : : const struct bf_flavor_ops bf_flavor_ops_xdp = {
134 : : .gen_inline_prologue = _bf_xdp_gen_inline_prologue,
135 : : .gen_inline_epilogue = _bf_xdp_gen_inline_epilogue,
136 : : .gen_inline_redirect = _bf_xdp_gen_inline_redirect,
137 : : .get_verdict = _bf_xdp_get_verdict,
138 : : .gen_inline_matcher = bf_packet_gen_inline_matcher,
139 : : .gen_inline_log = bf_packet_gen_inline_log,
140 : : };
|