From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id F06B7A0C58; Wed, 8 Sep 2021 23:51:27 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DA9E241150; Wed, 8 Sep 2021 23:51:05 +0200 (CEST) Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by mails.dpdk.org (Postfix) with ESMTP id 6E2F541145 for ; Wed, 8 Sep 2021 23:51:02 +0200 (CEST) Received: by mail-pj1-f53.google.com with SMTP id c13-20020a17090a558d00b00198e6497a4fso2180239pji.4 for ; Wed, 08 Sep 2021 14:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ia3BuntjOiQCgcO1Qmby8Kit/gtvf7Pszy+whlGA8Mk=; b=HSI2U8sZaFaUMNBAfJT84uhMhm0/K0mEmO0Pzh4oomct9qn7q1yjp2kPHsvu9eQGeV 8xg/o0UNg7hDjQHVPo5NCG+t3JYtKefqSEmQjRa5iuiM26fdypGnVfCP6++gGwkpgsOT apGYCkt4jvuIPWQMppOhyBuURR9YNEb4IjrkeXkx9IcQMmI4uxdo3R6T1WbER49Cc7Xw bvVh1nuHTY0aT2d4DbLIdNiy+qW5AaCY3z4WaZn6zj7kUQY50lWm8O08709DIhUpM0ZS DT1Dtfwz9t2ij2B4G0HjhPVzZc5bqQqetiXP1kWjDqb7Q4JZMJQxG8dDjA+rDe2jqhZZ 1GxA== 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=ia3BuntjOiQCgcO1Qmby8Kit/gtvf7Pszy+whlGA8Mk=; b=Nv2qnalEeyZIm8mlwW9q6RigfhuYAxy62BV1inJrsBSwmLlNjW/2JtnebTwODqQD4k 3kWtDS0xipROBZzVCkagbNL6iWHKsOBOHUcqgUiRjrRoNfqJvmsBgI3sl4b7abw2DssO az1MNv335d6lAWqzIk+d9Q8mLHzNyVh/6qkOk15sQKjkh6Aoo9bO9MkARFeJPBqgLU1a dq4uHrM3Vw8O06F7XOpnJZv5PX2I91z2ofaQvcESPixkTCJinY3n+wp3+32dpuLe/EwM KpydbSMP70jk87EGoLInlpvFv6EMs6KnFVyYgqqtdkN5XAB5Se+PJ6Em0HeG0nxuhsjZ xsoQ== X-Gm-Message-State: AOAM533bb/oXkJ+Pdc/OxwsEZmvAPy+e7r63RKPOD0AserCkJHTPHt2t WmyTcIYhDPzyrFH6NYJh/HFgo4N4zsRN0g== X-Google-Smtp-Source: ABdhPJzi4Iw3hBtVLR9i7twmc661eO39zRdKebX/5tRcK1k3NINeAvE0dJe5gCcPnqnlR7YW5XjoLw== X-Received: by 2002:a17:902:9682:b0:138:e2f9:720d with SMTP id n2-20020a170902968200b00138e2f9720dmr320458plp.57.1631137861088; Wed, 08 Sep 2021 14:51:01 -0700 (PDT) Received: from hermes.local (204-195-33-123.wavecable.com. [204.195.33.123]) by smtp.gmail.com with ESMTPSA id a80sm151923pfd.179.2021.09.08.14.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Sep 2021 14:51:00 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Wed, 8 Sep 2021 14:50:48 -0700 Message-Id: <20210908215053.74628-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210908215053.74628-1-stephen@networkplumber.org> References: <20210903004732.109023-1-stephen@networkplumber.org> <20210908215053.74628-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v5 4/9] 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 --- lib/bpf/bpf_dump.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ lib/bpf/meson.build | 1 + lib/bpf/rte_bpf.h | 14 ++++++ lib/bpf/version.map | 1 + 4 files changed, 134 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..a6a431e64903 --- /dev/null +++ b/lib/bpf/bpf_dump.c @@ -0,0 +1,118 @@ +/* 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", +}; + +static void ebpf_dump(FILE *f, const struct ebpf_insn insn, size_t n) +{ + const char *op, *postfix = ""; + uint8_t cls = BPF_CLASS(insn.code); + + fprintf(f, " L%zu:\t", n); + + switch (cls) { + default: + fprintf(f, "unimp 0x%x // class: %s\n", insn.code, + class_tbl[cls]); + break; + case BPF_ALU: + postfix = "32"; + /* fall through */ + case EBPF_ALU64: + op = alu_op_tbl[BPF_OP_INDEX(insn.code)]; + if (BPF_SRC(insn.code) == BPF_X) + fprintf(f, "%s%s r%u, r%u\n", op, postfix, insn.dst_reg, + insn.src_reg); + else + fprintf(f, "%s%s r%u, #0x%x\n", op, postfix, + insn.dst_reg, insn.imm); + break; + case BPF_LD: + op = "ld"; + postfix = size_tbl[BPF_SIZE_INDEX(insn.code)]; + if (BPF_MODE(insn.code) == BPF_IMM) + fprintf(f, "%s%s r%d, #0x%x\n", op, postfix, + insn.dst_reg, insn.imm); + else if (BPF_MODE(insn.code) == BPF_ABS) + fprintf(f, "%s%s r%d, [%d]\n", op, postfix, + insn.dst_reg, insn.imm); + else if (BPF_MODE(insn.code) == BPF_IND) + fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix, + insn.dst_reg, insn.src_reg, insn.imm); + else + fprintf(f, "// BUG: LD opcode 0x%02x in eBPF insns\n", + insn.code); + break; + case BPF_LDX: + op = "ldx"; + postfix = size_tbl[BPF_SIZE_INDEX(insn.code)]; + fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix, insn.dst_reg, + insn.src_reg, insn.off); + break; +#define L(pc, off) ((int)(pc) + 1 + (off)) + case BPF_JMP: + op = jump_tbl[BPF_OP_INDEX(insn.code)]; + if (op == NULL) + fprintf(f, "invalid jump opcode: %#x\n", insn.code); + else if (BPF_OP(insn.code) == BPF_JA) + fprintf(f, "%s L%d\n", op, L(n, insn.off)); + else if (BPF_OP(insn.code) == EBPF_EXIT) + fprintf(f, "%s\n", op); + else + fprintf(f, "%s r%u, #0x%x, L%d\n", op, insn.dst_reg, + insn.imm, L(n, insn.off)); + break; + case BPF_RET: + fprintf(f, "// BUG: RET opcode 0x%02x in eBPF insns\n", + insn.code); + break; + } +} + +void rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len) +{ + uint32_t i; + + for (i = 0; i < len; ++i) + ebpf_dump(f, buf[i], i); +} 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; }; -- 2.30.2