Skip to contentSkip to conversation

net: filter: cleanup A/X name usage

PATCH Applied Alexei Starovoitov 4 participants 6 messages Jun 6, 2014 – Jun 11, 2014

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: filter: cleanup A/X name usage

The macro 'A' used in internal BPF interpreter:
#define A regs[insn->a_reg]
was easily confused with the name of classic BPF register 'A', since 'A' would mean two different things depending on context.

This patch is trying to clean up the naming and clarify its usage in the following way:

- A and X are names of two classic BPF registers

- BPF_REG_A denotes internal BPF register R0 used to map classic register A in internal BPF programs generated from classic

- BPF_REG_X denotes internal BPF register R7 used to map classic register X in internal BPF programs generated from classic

- internal BPF instruction format:
struct sock_filter_int {

        __u8    code;           /* opcode */
        __u8    dst_reg:4;      /* dest register */
        __u8    src_reg:4;      /* source register */
        __s16   off;            /* signed offset */
        __s32   imm;            /* signed immediate constant */

};

- BPF_X/BPF_K is 1 bit used to encode source operand of instruction In classic:
BPF_X - means use register X as source operand
BPF_K - means use 32-bit immediate as source operand
In internal:
BPF_X - means use 'src_reg' register as source operand BPF_K - means use 32-bit immediate as source operand

---
 Documentation/networking/filter.txt |    2 +-
 arch/x86/net/bpf_jit_comp.c         |  260 +++++++++++++++++------------------
 include/linux/filter.h              |  156 +++++++++++----------
 net/core/filter.c                   |  198 +++++++++++++-------------
 4 files changed, 314 insertions(+), 302 deletions(-)
Documentation/networking/filter.txt +1 -1
index 58c443926647..9f49b8690500 100644
@@ -805,7 +805,7 @@ to seccomp_data, for converted BPF filters R1 points to a skb.
 
 A program, that is translated internally consists of the following elements:
 
-  op:16, jt:8, jf:8, k:32    ==>    op:8, a_reg:4, x_reg:4, off:16, imm:32
+  op:16, jt:8, jf:8, k:32    ==>    op:8, dst_reg:4, src_reg:4, off:16, imm:32
 
 So far 87 internal BPF instructions were implemented. 8-bit 'op' opcode field
 has room for new instructions. Some of them may use 16/24/32 byte encoding. New
arch/x86/net/bpf_jit_comp.c +130 -130

530 lines not on this page · raw message

include/linux/filter.h +84 -72

279 lines not on this page · raw message

net/core/filter.c +99 -99

432 lines not on this page · raw message

Signature · 1 line
1.7.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 33 lines · Alexei Starovoitov

The macro 'A' used in internal BPF interpreter:
#define A regs[insn->a_reg]
was easily confused with the name of classic BPF register 'A', since 'A' would mean two different things depending on context.

This patch is trying to clean up the naming and clarify its usage in the following way:

- A and X are names of two classic BPF registers

- BPF_REG_A denotes internal BPF register R0 used to map classic register A in internal BPF programs generated from classic

- BPF_REG_X denotes internal BPF register R7 used to map classic register X in internal BPF programs generated from classic

- internal BPF instruction format:
struct sock_filter_int {

         __u8    code;           /* opcode */
         __u8    dst_reg:4;      /* dest register */
         __u8    src_reg:4;      /* source register */
         __s16   off;            /* signed offset */
         __s32   imm;            /* signed immediate constant */

};

- BPF_X/BPF_K is 1 bit used to encode source operand of instruction In classic:
BPF_X - means use register X as source operand
BPF_K - means use 32-bit immediate as source operand
In internal:
BPF_X - means use 'src_reg' register as source operand BPF_K - means use 32-bit immediate as source operand

Quoted 2 lines

You could also have mentioned in the changelog that you replace direct access to ctx pointer with CTX register, it's valid since we emit a mov from ARG1 to CTX at the beginning which contains ctx. Anyway, looks good to me.

#2 lore raw

Great job!
-Chema

Quoted 47 lines · Daniel Borkmann
Quoted 36 lines · Alexei Starovoitov

The macro 'A' used in internal BPF interpreter:
#define A regs[insn->a_reg]
was easily confused with the name of classic BPF register 'A', since 'A' would mean two different things depending on context.

This patch is trying to clean up the naming and clarify its usage in the following way:

- A and X are names of two classic BPF registers

- BPF_REG_A denotes internal BPF register R0 used to map classic register A
in internal BPF programs generated from classic

- BPF_REG_X denotes internal BPF register R7 used to map classic register X
in internal BPF programs generated from classic

- internal BPF instruction format:
struct sock_filter_int {

         __u8    code;           /* opcode */
         __u8    dst_reg:4;      /* dest register */
         __u8    src_reg:4;      /* source register */
         __s16   off;            /* signed offset */
         __s32   imm;            /* signed immediate constant */

};

- BPF_X/BPF_K is 1 bit used to encode source operand of instruction In classic:
BPF_X - means use register X as source operand
BPF_K - means use 32-bit immediate as source operand
In internal:
BPF_X - means use 'src_reg' register as source operand BPF_K - means use 32-bit immediate as source operand

Quoted 2 lines

You could also have mentioned in the changelog that you replace direct access to ctx pointer with CTX register, it's valid since we emit a mov from ARG1 to CTX at the beginning which contains ctx. Anyway, looks good to me.

#3 lore raw
In reply to Daniel Borkmann
Quoted 41 lines · Daniel Borkmann
Quoted 36 lines · Alexei Starovoitov

The macro 'A' used in internal BPF interpreter:
#define A regs[insn->a_reg]
was easily confused with the name of classic BPF register 'A', since 'A' would mean two different things depending on context.

This patch is trying to clean up the naming and clarify its usage in the following way:

- A and X are names of two classic BPF registers

- BPF_REG_A denotes internal BPF register R0 used to map classic register A
in internal BPF programs generated from classic

- BPF_REG_X denotes internal BPF register R7 used to map classic register X
in internal BPF programs generated from classic

- internal BPF instruction format:
struct sock_filter_int {

         __u8    code;           /* opcode */
         __u8    dst_reg:4;      /* dest register */
         __u8    src_reg:4;      /* source register */
         __s16   off;            /* signed offset */
         __s32   imm;            /* signed immediate constant */

};

- BPF_X/BPF_K is 1 bit used to encode source operand of instruction In classic:
BPF_X - means use register X as source operand
BPF_K - means use 32-bit immediate as source operand
In internal:
BPF_X - means use 'src_reg' register as source operand BPF_K - means use 32-bit immediate as source operand

Quoted 2 lines
Quoted 6 lines

You could also have mentioned in the changelog that you replace direct access to ctx pointer with CTX register, it's valid since we emit a mov from ARG1 to CTX at the beginning which contains ctx. Anyway, looks good to me.

Point taken. Will do more thorough documentation patch to explain eBPF instruction set a bit better. Mainly to clarify encoding. eBPF tried to be as close as possible to classic BPF, since I thought it will make it easier to understand. That's why I reused A/X in names and bits in opcode encoding.
In classic, A is accumulator and X is indeX. In eBPF I thought calling destination register 'a_reg' made sense, since it would indicate that this 'a_reg' is a register that is used as 'accumulator' in the instruction. Like in "bpf_add R2, 5", R2 is Accumulator. In "bpf_add R2, R3", R2 is Accumulator and R3 is indeX
and BPF_X bit in opcode indicates whether 'x_reg' field of the instruction is used. Unfortunately that decision caused the whole A/X naming confusion, that this patch is addressing. src_reg and dst_reg are indeed better names.
The next patch of doc updates hopefully will make encoding clear too.

Thanks guys for reviewing.

#4 lore raw

On 06/09/2014 09:25 PM, Alexei Starovoitov wrote:
...

Quoted 6 lines

Point taken. Will do more thorough documentation patch to explain eBPF instruction set a bit better. Mainly to clarify encoding. eBPF tried to be as close as possible to classic BPF, since I thought it will make it easier to understand. That's why I reused A/X in names and bits in opcode encoding.
In classic, A is accumulator and X is indeX. In eBPF I thought

Quoted 8 lines

calling destination register 'a_reg' made sense, since it would indicate that this 'a_reg' is a register that is used as 'accumulator' in the instruction. Like in "bpf_add R2, 5", R2 is Accumulator. In "bpf_add R2, R3", R2 is Accumulator and R3 is indeX
and BPF_X bit in opcode indicates whether 'x_reg' field of the instruction is used. Unfortunately that decision caused the whole A/X naming confusion, that this patch is addressing. src_reg and dst_reg are indeed better names.

I think both naming conventions (before/after) are fine, imho.

maintainer Applied
#5 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Fri, 6 Jun 2014 14:46:06 -0700

Quoted 33 lines

The macro 'A' used in internal BPF interpreter:
#define A regs[insn->a_reg]
was easily confused with the name of classic BPF register 'A', since 'A' would mean two different things depending on context.

This patch is trying to clean up the naming and clarify its usage in the following way:

- A and X are names of two classic BPF registers

- BPF_REG_A denotes internal BPF register R0 used to map classic register A in internal BPF programs generated from classic

- BPF_REG_X denotes internal BPF register R7 used to map classic register X in internal BPF programs generated from classic

- internal BPF instruction format:
struct sock_filter_int {

        __u8    code;           /* opcode */
        __u8    dst_reg:4;      /* dest register */
        __u8    src_reg:4;      /* source register */
        __s16   off;            /* signed offset */
        __s32   imm;            /* signed immediate constant */

};

- BPF_X/BPF_K is 1 bit used to encode source operand of instruction In classic:
BPF_X - means use register X as source operand
BPF_K - means use 32-bit immediate as source operand
In internal:
BPF_X - means use 'src_reg' register as source operand BPF_K - means use 32-bit immediate as source operand

Quoted 2 lines

Applied.