[dpdk-dev] [PATCH 5/5] bpf: x86 JIT support for packet data loadinstructions
Ananyev, Konstantin
konstantin.ananyev at intel.com
Mon May 25 01:08:57 CEST 2020
> >
> > +/*
> > + * helper function, used by emit_ld_mbuf().
> > + * generates code for 'fast_path':
> > + * calculate load offset and check is it inside first packet segment.
> > + */
> > +static void
> > +emit_ldmb_fast_path(struct bpf_jit_state *st, const uint32_t
> > rg[EBPF_REG_7],
> > + uint32_t sreg, uint32_t mode, uint32_t sz, uint32_t imm,
> > + const int32_t ofs[LDMB_OFS_NUM])
> > +{
> > + /* make R2 contain *off* value */
> > +
> > + if (sreg != rg[EBPF_REG_2]) {
> > + emit_mov_imm(st, EBPF_ALU64 | EBPF_MOV | BPF_K,
> > + rg[EBPF_REG_2], imm);
> > + if (mode == BPF_IND)
> > + emit_alu_reg(st, EBPF_ALU64 | BPF_ADD | BPF_X,
> > + sreg, rg[EBPF_REG_2]);
> > + } else
> > + /* BPF_IND with sreg == R2 */
> > + emit_alu_imm(st, EBPF_ALU64 | BPF_ADD | BPF_K,
> > + rg[EBPF_REG_2], imm);
> > +
> > + /* R3 = mbuf->data_len */
> > + emit_ld_reg(st, BPF_LDX | BPF_MEM | BPF_H,
> > + rg[EBPF_REG_6], rg[EBPF_REG_3],
> > + offsetof(struct rte_mbuf, data_len));
> > +
> > + /* R3 = R3 - R2 */
> > + emit_alu_reg(st, EBPF_ALU64 | BPF_SUB | BPF_X,
> > + rg[EBPF_REG_2], rg[EBPF_REG_3]);
> > +
> > + /* JSLE R3, <sz> <slow_path> */
> > + emit_cmp_imm(st, EBPF_ALU64, rg[EBPF_REG_3], sz);
> > + emit_abs_jcc(st, BPF_JMP | EBPF_JSLE | BPF_K, ofs[LDMB_SLP_OFS]);
> > +
> > + /* R3 = mbuf->data_off */
> > + emit_ld_reg(st, BPF_LDX | BPF_MEM | BPF_H,
> > + rg[EBPF_REG_6], rg[EBPF_REG_3],
> > + offsetof(struct rte_mbuf, data_off));
> > +
> > + /* R0 = mbuf->buf_addr */
> > + emit_ld_reg(st, BPF_LDX | BPF_MEM | EBPF_DW,
> > + rg[EBPF_REG_6], rg[EBPF_REG_0],
> > + offsetof(struct rte_mbuf, buf_addr));
> > +
> > + /* R0 = R0 + R3 */
> > + emit_alu_reg(st, EBPF_ALU64 | BPF_ADD | BPF_X,
> > + rg[EBPF_REG_3], rg[EBPF_REG_0]);
> > +
> > + /* R0 = R0 + R2 */
> > + emit_alu_reg(st, EBPF_ALU64 | BPF_ADD | BPF_X,
> > + rg[EBPF_REG_2], rg[EBPF_REG_0]);
> > +
> > + /* JMP <fin_part> */
> > + emit_abs_jmp(st, ofs[LDMB_FIN_OFS]);
> > +}
> > +
> > +/*
> > + * helper function, used by emit_ld_mbuf().
> > + * generates code for 'slow_path':
> > + * call __rte_pktmbuf_read() and check return value.
> > + */
> > +static void
> > +emit_ldmb_slow_path(struct bpf_jit_state *st, const uint32_t
> > rg[EBPF_REG_7],
> > + uint32_t sz)
> > +{
> > + /* make R3 contain *len* value (1/2/4) */
> > +
> > + emit_mov_imm(st, EBPF_ALU64 | EBPF_MOV | BPF_K, rg[EBPF_REG_3], sz);
> > +
> > + /* make R4 contain (RBP - ldmb.stack_ofs) */
> > +
> > + emit_mov_reg(st, EBPF_ALU64 | EBPF_MOV | BPF_X, RBP, rg[EBPF_REG_4]);
> > + emit_alu_imm(st, EBPF_ALU64 | BPF_SUB | BPF_K, rg[EBPF_REG_4],
> > + st->ldmb.stack_ofs);
> > +
> > + /* make R1 contain mbuf ptr */
> > +
> > + emit_mov_reg(st, EBPF_ALU64 | EBPF_MOV | BPF_X,
> > + rg[EBPF_REG_6], rg[EBPF_REG_1]);
> > +
> > + /* call rte_pktmbuf_read */
> > + emit_call(st, (uintptr_t)__rte_pktmbuf_read);
> > +
> > + /* check that return value (R0) is not zero */
> > + emit_tst_reg(st, EBPF_ALU64, rg[EBPF_REG_0], rg[EBPF_REG_0]);
> > + emit_abs_jcc(st, BPF_JMP | BPF_JEQ | BPF_K, st->exit.off);
> > +}
> > +
> > +/*
> > + * helper function, used by emit_ld_mbuf().
> > + * generates final part of code for BPF_ABS/BPF_IND load:
> > + * perform data load and endianness conversion.
> > + * expects dreg to contain valid data pointer.
> > + */
> > +static void
> > +emit_ldmb_fin(struct bpf_jit_state *st, uint32_t dreg, uint32_t opsz,
> > + uint32_t sz)
> > +{
> > + emit_ld_reg(st, BPF_LDX | BPF_MEM | opsz, dreg, dreg, 0);
> > + if (sz != sizeof(uint8_t))
> > + emit_be2le(st, dreg, sz * CHAR_BIT);
> > +}
> > +
> > +/*
> > + * emit code for BPF_ABS/BPF_IND load.
> > + * generates the following construction:
> > + * fast_path:
> > + * off = ins->sreg + ins->imm
> > + * if (off + ins->opsz < mbuf->data_len)
> > + * goto slow_path;
>
> I am not an eBPF expert, but I am not sure this is correct.
>
> I think it should be > instead of <. Also, it looks like you actually emit:
> if (mbuf->data_len - off <= ins->opsz)
> goto slow_path;
>
> Could you please double check that both the comment and the emitted code has the comparison turning the correct way.
Ack, should be:
if (mbuf->data_len - off < ins->opsz)
Will send v2.
More information about the dev
mailing list