Skip to content

Commit 3074383

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: filter: make register naming more comprehensible
The current code is a bit hard to parse on which registers can be used, how they are mapped and all play together. It makes much more sense to define this a bit more clearly so that the code is a bit more intuitive. This patch cleans this up, and makes naming a bit more consistent among the code. This also allows for moving some of the defines into the header file. Clearing of A and X registers in __sk_run_filter() do not get a particular register name assigned as they have not an 'official' function, but rather just result from the concrete initial mapping of old BPF programs. Since for BPF helper functions for BPF_CALL we already use small letters, so be consistent here as well. No functional changes. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5bcfedf commit 3074383

2 files changed

Lines changed: 145 additions & 114 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,47 @@
4040
/* Placeholder/dummy for 0 */
4141
#define BPF_0 0
4242

43+
/* Register numbers */
44+
enum {
45+
BPF_REG_0 = 0,
46+
BPF_REG_1,
47+
BPF_REG_2,
48+
BPF_REG_3,
49+
BPF_REG_4,
50+
BPF_REG_5,
51+
BPF_REG_6,
52+
BPF_REG_7,
53+
BPF_REG_8,
54+
BPF_REG_9,
55+
BPF_REG_10,
56+
__MAX_BPF_REG,
57+
};
58+
4359
/* BPF has 10 general purpose 64-bit registers and stack frame. */
44-
#define MAX_BPF_REG 11
60+
#define MAX_BPF_REG __MAX_BPF_REG
61+
62+
/* ArgX, context and stack frame pointer register positions. Note,
63+
* Arg1, Arg2, Arg3, etc are used as argument mappings of function
64+
* calls in BPF_CALL instruction.
65+
*/
66+
#define BPF_REG_ARG1 BPF_REG_1
67+
#define BPF_REG_ARG2 BPF_REG_2
68+
#define BPF_REG_ARG3 BPF_REG_3
69+
#define BPF_REG_ARG4 BPF_REG_4
70+
#define BPF_REG_ARG5 BPF_REG_5
71+
#define BPF_REG_CTX BPF_REG_6
72+
#define BPF_REG_FP BPF_REG_10
73+
74+
/* Additional register mappings for converted user programs. */
75+
#define BPF_REG_A BPF_REG_0
76+
#define BPF_REG_X BPF_REG_7
77+
#define BPF_REG_TMP BPF_REG_8
4578

4679
/* BPF program can access up to 512 bytes of stack space. */
4780
#define MAX_BPF_STACK 512
4881

49-
/* Arg1, context and stack frame pointer register positions. */
50-
#define ARG1_REG 1
51-
#define CTX_REG 6
52-
#define FP_REG 10
82+
/* Macro to invoke filter function. */
83+
#define SK_RUN_FILTER(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
5384

5485
struct sock_filter_int {
5586
__u8 code; /* opcode */
@@ -100,9 +131,6 @@ static inline unsigned int sk_filter_size(unsigned int proglen)
100131
#define sk_filter_proglen(fprog) \
101132
(fprog->len * sizeof(fprog->filter[0]))
102133

103-
#define SK_RUN_FILTER(filter, ctx) \
104-
(*filter->bpf_func)(ctx, filter->insnsi)
105-
106134
int sk_filter(struct sock *sk, struct sk_buff *skb);
107135

108136
u32 sk_run_filter_int_seccomp(const struct seccomp_data *ctx,

0 commit comments

Comments
 (0)