From patchwork Thu Sep 16 22:26:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 99041 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id A41F8A0C47; Fri, 17 Sep 2021 00:27:07 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 34ECE4111C; Fri, 17 Sep 2021 00:26:48 +0200 (CEST) Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by mails.dpdk.org (Postfix) with ESMTP id BBFF8410F7 for ; Fri, 17 Sep 2021 00:26:44 +0200 (CEST) Received: by mail-pj1-f45.google.com with SMTP id j1so5574455pjv.3 for ; Thu, 16 Sep 2021 15:26:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4vaE/EiLwrmc4bOiTRxz3zUDK47Pr9QU3JL0LOFt1VY=; b=LjwRP5a8oV13mfUucmLxEh3iahkYqijxkSojTlUkJVieGaVY8PSnhZ2S7nS6absppC 1oEHMYejadt03aZ5O1FtPJqzjtaYdRkeMYV8BgCZdSh6KyqJsAbKdEJJ1tUMhqcRLiyP ctQNIBHtDLUp63TcFyU+v1zJ0LufiTG4VdnOfTzwKcNMqIost7d5v7pwWRyPNXd6G0TK inkZO+nXkZACZ4TzKfKpyACCIOzvVlU52zKnuftVhWnFXb6l9VOeEYN72enPulCZEh2m OfzE+PEdr6plBYwfXL7M82Vtlyk52AW3mkqJqURMacl9e5Yh3zQdzk5SwoOLiAHTlf30 14gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4vaE/EiLwrmc4bOiTRxz3zUDK47Pr9QU3JL0LOFt1VY=; b=b7xCjX7TSQtMOfxOY/I/4GcHX2Xg0QgmxiX0EIPGUjMrHtGLllYgOmaxh1sdTwQ+0k QYWhl9jNlZGyZ3F6d38yUePq5IHmx8jryxvxfQbFLiQNL1+KVOsFUBwmO7ZhQK49SnmV QrhQ5vKlGUSJPKE1kdr+iVcu2+aZCNyh6CRgu/xCy41UAVpnPoNV8WJuxXBvqy6pMjLf OIUkGPsIsZsfY3gve5GJtGi/4YZFlnUu0FsdvG/iFXby/VtaLXiBF9R877staS4+hBeP 1GBMXsv1gErrIv8xqTFT75NzvXuujjJ/fYWAOLXZzptigf1f3dQDAbYNhPHHRsaQku+x d5WA== X-Gm-Message-State: AOAM532UOC2Ayu09Kv0qtTrUmGhdFMoYumHTF/1GW3tY9bDT9ZhZvaFZ o7xK998KzZYESWQn6/ebT+OVr+RB9V+mIQ== X-Google-Smtp-Source: ABdhPJxfl7HzN6dGiIsh6pUEXHxflfgJ3+VM0fHbOAN+OHoKv+9bPC2gmrRNiuHZAbjI+9kLRm0y+A== X-Received: by 2002:a17:90b:3b87:: with SMTP id pc7mr17400596pjb.232.1631831203485; Thu, 16 Sep 2021 15:26:43 -0700 (PDT) Received: from hermes.local (204-195-33-123.wavecable.com. [204.195.33.123]) by smtp.gmail.com with ESMTPSA id 3sm3937169pfp.112.2021.09.16.15.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 15:26:42 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Konstantin Ananyev Date: Thu, 16 Sep 2021 15:26:23 -0700 Message-Id: <20210916222630.71543-6-stephen@networkplumber.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210916222630.71543-1-stephen@networkplumber.org> References: <20210903004732.109023-1-stephen@networkplumber.org> <20210916222630.71543-1-stephen@networkplumber.org> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v10 05/12] bpf: add function to dump eBPF instructions X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" When debugging converted (and other) programs it is useful to see disassembled eBPF output. Signed-off-by: Stephen Hemminger Acked-by: Konstantin Ananyev --- lib/bpf/bpf_dump.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 +++++ lib/bpf/version.map | 1 + 4 files changed, 155 insertions(+) create mode 100644 lib/bpf/bpf_dump.c diff --git a/lib/bpf/bpf_dump.c b/lib/bpf/bpf_dump.c new file mode 100644 index 000000000000..b86977b96d08 --- /dev/null +++ b/lib/bpf/bpf_dump.c @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 Stephen Hemminger + * Based on filter2xdp + * Copyright (C) 2017 Tobias Klauser + */ + +#include +#include + +#include "rte_bpf.h" + +#define BPF_OP_INDEX(x) (BPF_OP(x) >> 4) +#define BPF_SIZE_INDEX(x) (BPF_SIZE(x) >> 3) + +static const char *const class_tbl[] = { + [BPF_LD] = "ld", [BPF_LDX] = "ldx", [BPF_ST] = "st", + [BPF_STX] = "stx", [BPF_ALU] = "alu", [BPF_JMP] = "jmp", + [BPF_RET] = "ret", [BPF_MISC] = "alu64", +}; + +static const char *const alu_op_tbl[16] = { + [BPF_ADD >> 4] = "add", [BPF_SUB >> 4] = "sub", + [BPF_MUL >> 4] = "mul", [BPF_DIV >> 4] = "div", + [BPF_OR >> 4] = "or", [BPF_AND >> 4] = "and", + [BPF_LSH >> 4] = "lsh", [BPF_RSH >> 4] = "rsh", + [BPF_NEG >> 4] = "neg", [BPF_MOD >> 4] = "mod", + [BPF_XOR >> 4] = "xor", [EBPF_MOV >> 4] = "mov", + [EBPF_ARSH >> 4] = "arsh", [EBPF_END >> 4] = "endian", +}; + +static const char *const size_tbl[] = { + [BPF_W >> 3] = "w", + [BPF_H >> 3] = "h", + [BPF_B >> 3] = "b", + [EBPF_DW >> 3] = "dw", +}; + +static const char *const jump_tbl[16] = { + [BPF_JA >> 4] = "ja", [BPF_JEQ >> 4] = "jeq", + [BPF_JGT >> 4] = "jgt", [BPF_JGE >> 4] = "jge", + [BPF_JSET >> 4] = "jset", [EBPF_JNE >> 4] = "jne", + [EBPF_JSGT >> 4] = "jsgt", [EBPF_JSGE >> 4] = "jsge", + [EBPF_CALL >> 4] = "call", [EBPF_EXIT >> 4] = "exit", +}; + +void rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len) +{ + uint32_t i; + + for (i = 0; i < len; ++i) { + const struct ebpf_insn *ins = buf + i; + uint8_t cls = BPF_CLASS(ins->code); + const char *op, *postfix = ""; + + fprintf(f, " L%u:\t", i); + + switch (cls) { + default: + fprintf(f, "unimp 0x%x // class: %s\n", + ins->code, class_tbl[cls]); + break; + case BPF_ALU: + postfix = "32"; + /* fall through */ + case EBPF_ALU64: + op = alu_op_tbl[BPF_OP_INDEX(ins->code)]; + if (BPF_SRC(ins->code) == BPF_X) + fprintf(f, "%s%s r%u, r%u\n", op, postfix, ins->dst_reg, + ins->src_reg); + else + fprintf(f, "%s%s r%u, #0x%x\n", op, postfix, + ins->dst_reg, ins->imm); + break; + case BPF_LD: + op = "ld"; + postfix = size_tbl[BPF_SIZE_INDEX(ins->code)]; + if (ins->code == (BPF_LD | BPF_IMM | EBPF_DW)) { + uint64_t val; + + val = (uint32_t)ins[0].imm | + (uint64_t)(uint32_t)ins[1].imm << 32; + fprintf(f, "%s%s r%d, #0x%"PRIx64"\n", + op, postfix, ins->dst_reg, val); + i++; + } else if (BPF_MODE(ins->code) == BPF_IMM) + fprintf(f, "%s%s r%d, #0x%x\n", op, postfix, + ins->dst_reg, ins->imm); + else if (BPF_MODE(ins->code) == BPF_ABS) + fprintf(f, "%s%s r%d, [%d]\n", op, postfix, + ins->dst_reg, ins->imm); + else if (BPF_MODE(ins->code) == BPF_IND) + fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix, + ins->dst_reg, ins->src_reg, ins->imm); + else + fprintf(f, "// BUG: LD opcode 0x%02x in eBPF insns\n", + ins->code); + break; + case BPF_LDX: + op = "ldx"; + postfix = size_tbl[BPF_SIZE_INDEX(ins->code)]; + fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix, ins->dst_reg, + ins->src_reg, ins->off); + break; + case BPF_ST: + op = "st"; + postfix = size_tbl[BPF_SIZE_INDEX(ins->code)]; + if (BPF_MODE(ins->code) == BPF_MEM) + fprintf(f, "%s%s [r%d + %d], #0x%x\n", op, postfix, + ins->dst_reg, ins->off, ins->imm); + else + fprintf(f, "// BUG: ST opcode 0x%02x in eBPF insns\n", + ins->code); + break; + case BPF_STX: + op = "stx"; + postfix = size_tbl[BPF_SIZE_INDEX(ins->code)]; + fprintf(f, "%s%s [r%d + %d], r%u\n", op, postfix, + ins->dst_reg, ins->off, ins->src_reg); + break; +#define L(pc, off) ((int)(pc) + 1 + (off)) + case BPF_JMP: + op = jump_tbl[BPF_OP_INDEX(ins->code)]; + if (op == NULL) + fprintf(f, "invalid jump opcode: %#x\n", ins->code); + else if (BPF_OP(ins->code) == BPF_JA) + fprintf(f, "%s L%d\n", op, L(i, ins->off)); + else if (BPF_OP(ins->code) == EBPF_EXIT) + fprintf(f, "%s\n", op); + else + fprintf(f, "%s r%u, #0x%x, L%d\n", op, ins->dst_reg, + ins->imm, L(i, ins->off)); + break; + case BPF_RET: + fprintf(f, "// BUG: RET opcode 0x%02x in eBPF insns\n", + ins->code); + break; + } + } +} diff --git a/lib/bpf/meson.build b/lib/bpf/meson.build index 54f7610ae990..5b5585173aeb 100644 --- a/lib/bpf/meson.build +++ b/lib/bpf/meson.build @@ -2,6 +2,7 @@ # Copyright(c) 2018 Intel Corporation sources = files('bpf.c', + 'bpf_dump.c', 'bpf_exec.c', 'bpf_load.c', 'bpf_pkt.c', diff --git a/lib/bpf/rte_bpf.h b/lib/bpf/rte_bpf.h index 2f23e272a376..0d0a84b130a0 100644 --- a/lib/bpf/rte_bpf.h +++ b/lib/bpf/rte_bpf.h @@ -198,6 +198,20 @@ rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[], int rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit); +/** + * Dump epf instructions to a file. + * + * @param f + * A pointer to a file for output + * @param buf + * A pointer to BPF instructions + * @param len + * Number of BPF instructions to dump. + */ +__rte_experimental +void +rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len); + #ifdef RTE_PORT_PCAP struct bpf_program; diff --git a/lib/bpf/version.map b/lib/bpf/version.map index 47082d5003ef..3b953f2f4592 100644 --- a/lib/bpf/version.map +++ b/lib/bpf/version.map @@ -19,4 +19,5 @@ EXPERIMENTAL { global: rte_bpf_convert; + rte_bpf_dump; };