Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp431841rwl; Wed, 29 Mar 2023 04:07:57 -0700 (PDT) X-Google-Smtp-Source: AKy350YkrneO9EInkuva4pdlph1rYRtudjbFDI+ALrEMSLJ/zq056dTZn4jBsYGZVdqt+ArXi16o X-Received: by 2002:aa7:9732:0:b0:5cd:229e:f1d8 with SMTP id k18-20020aa79732000000b005cd229ef1d8mr17862788pfg.4.1680088077367; Wed, 29 Mar 2023 04:07:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680088077; cv=none; d=google.com; s=arc-20160816; b=v7W0lVNRg2QjwZfZSc1PEtsypgH+j20pzEydytoSdJ1IrrIL5tWW4rFc00nDg1aRYq m9gfJRKSNT2YC85SMS5QPxO4z15oJ8OvxLjU57+xKj2t3+EtiAnEsI8bL7y3/H9ak59t ZY9NM4ylxuJ+8CWTTdzicdb+CFvGTTjgM2ceORJEiRYa0kVy4vOgM4V8lwIH6ddmm/9K /Z+tW8CEgLcC+GlS9PKrRaKTihpnh30nswDlVgdjJm+Vpoj3HCk3E6AocJGB4I320cbc vgAjv3ELFiYydw9M9MF36qCIEUcWxfQ9dMFmMDi1Ly97jkmwBsGUnactmjRsT4QYLa7B rrNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sgkQXYn3YvfKra4NCcSBXfsEMksh5BTkjMQUOsqzQ+E=; b=Vp6wHVoQHxEhh77RRzsRu1ZeeBsVXVfTIWxdWPfuK8aoeovjicSxJC7vfOSkNT78Kd YAmKbJqSdSNaOkGEMDJUNXzxn2Luyu1O+dtgvP4we2ymI/bY1SfEnOTk6IhH6EuXjYwe mxjblkZZZJNL7Yren77R1m8DBMfQ02CAbV/IE6nbqXFJMazuYJYLtHXwaEPkog4B3ari zrCyLITrPIUQgM4P6edeFybEAj3DoKQoQTJ/bC5+KDGc1YPxINH76zp0FhJwPbdxVwRB 0tal5uCQl2GoqvDLEFszvqhWLt9Tw27GZtHT7jDU5Yd0gijfxbgtenoQWqJ6srT5zgqB aD2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@daynix-com.20210112.gappssmtp.com header.s=20210112 header.b=PyXqtCFO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x6-20020a056a00188600b006265c3b7053si30289782pfh.200.2023.03.29.04.07.44; Wed, 29 Mar 2023 04:07:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@daynix-com.20210112.gappssmtp.com header.s=20210112 header.b=PyXqtCFO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229601AbjC2LDq (ORCPT + 99 others); Wed, 29 Mar 2023 07:03:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229575AbjC2LDo (ORCPT ); Wed, 29 Mar 2023 07:03:44 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54AAC1A7 for ; Wed, 29 Mar 2023 04:03:42 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id ek18so61538141edb.6 for ; Wed, 29 Mar 2023 04:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20210112.gappssmtp.com; s=20210112; t=1680087821; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sgkQXYn3YvfKra4NCcSBXfsEMksh5BTkjMQUOsqzQ+E=; b=PyXqtCFO9sUJPCeSPkIEmRh8FWBqXOozOdxoBOtp13ZPwfu2XqgpcQHD9xXGxYH8nF FdxSb6DoaLJEo+PNa/u7phzZ1sKHGiYH8RZgZ/BMDdtg6kLf3QJ1N6Bpmtdu+2eyDCU7 88SI0rm1IFIcqZwPFlYZYgd4dacEUDmopzYRfR4sq7U+oHHBlIMB2uSP4KBctQ0lweF7 TPhYelCVdkqatgMfUDEik7jwHrbD95WYipLN1rwpRIB0up2diBsHISSMmGS6Po+YueYH EWvIuaO9Gmyw+lVTRBvkxRCVSPEMg72Bvl3JTPNxj/JoPQclpSvVYCX3fv9Ia7EBcYuH auYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680087821; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sgkQXYn3YvfKra4NCcSBXfsEMksh5BTkjMQUOsqzQ+E=; b=K81EEFSR/GEve5IbBryReC5MumLScPbvP8RA5Dbf/glkO3b8XLyBm8w4b/0Z+M7780 gFv4CWOfZ6HwHO8fb/q48aoz2FuoSejBhV6pxET2qHN2jDJ1YxYyAWJnNKRHcHwJ0jK9 ApjVYLHyk+RJOqTysTmlXL4SSAup4vARJNikT5Tpp3NxqaTDndcx/Pmb1UC6n1bkieJK MCupCxpUwXFTAf6OuYY6MZU0ncRkjpuck+0m2PtX/DWhEZOlug1jMjf/Jb/sPdsLJNuL sntajKXwuh5UkLn3IeUC6WMl0j50gqvtARcOw7+EYpqmIqBB9mnVYgfm37YPiMoPe3gi oiPA== X-Gm-Message-State: AAQBX9caQ6kxo9ha7i7Bp+WNsg8hRtd6hd1i9rYbSRGQXpAavL6nGb6i UzLRIeaW+u1lYvkzn358TsWLZA== X-Received: by 2002:a17:906:e0c5:b0:931:4f2c:4e83 with SMTP id gl5-20020a170906e0c500b009314f2c4e83mr19049817ejb.63.1680087820834; Wed, 29 Mar 2023 04:03:40 -0700 (PDT) Received: from localhost.localdomain (178-133-100-41.mobile.vf-ua.net. [178.133.100.41]) by smtp.gmail.com with ESMTPSA id md12-20020a170906ae8c00b008e68d2c11d8sm16406975ejb.218.2023.03.29.04.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 04:03:40 -0700 (PDT) From: Andrew Melnychenko To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, jasowang@redhat.com, yoshfuji@linux-ipv6.org, dsahern@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org Cc: yan@daynix.com, yuri.benditovich@daynix.com Subject: [RFC PATCH 1/5] ebpf: Added eBPF initialization by fds and map update. Date: Wed, 29 Mar 2023 13:45:42 +0300 Message-Id: <20230329104546.108016-2-andrew@daynix.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230329104546.108016-1-andrew@daynix.com> References: <20230329104546.108016-1-andrew@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Changed eBPF map updates through mmaped array. Mmaped arrays provide direct access to map data. It should omit using bpf_map_update_elem() call, which may require capabilities that are not present. Signed-off-by: Andrew Melnychenko --- ebpf/ebpf_rss-stub.c | 6 +++ ebpf/ebpf_rss.c | 120 ++++++++++++++++++++++++++++++++++--------- ebpf/ebpf_rss.h | 10 ++++ 3 files changed, 113 insertions(+), 23 deletions(-) diff --git a/ebpf/ebpf_rss-stub.c b/ebpf/ebpf_rss-stub.c index e71e229190..8d7fae2ad9 100644 --- a/ebpf/ebpf_rss-stub.c +++ b/ebpf/ebpf_rss-stub.c @@ -28,6 +28,12 @@ bool ebpf_rss_load(struct EBPFRSSContext *ctx) return false; } +bool ebpf_rss_load_fds(struct EBPFRSSContext *ctx, int program_fd, + int config_fd, int toeplitz_fd, int table_fd) +{ + return false; +} + bool ebpf_rss_set_all(struct EBPFRSSContext *ctx, struct EBPFRSSConfig *config, uint16_t *indirections_table, uint8_t *toeplitz_key) { diff --git a/ebpf/ebpf_rss.c b/ebpf/ebpf_rss.c index cee658c158..08015fecb1 100644 --- a/ebpf/ebpf_rss.c +++ b/ebpf/ebpf_rss.c @@ -27,19 +27,68 @@ void ebpf_rss_init(struct EBPFRSSContext *ctx) { if (ctx != NULL) { ctx->obj = NULL; + ctx->program_fd = -1; } } bool ebpf_rss_is_loaded(struct EBPFRSSContext *ctx) { - return ctx != NULL && ctx->obj != NULL; + return ctx != NULL && (ctx->obj != NULL || ctx->program_fd != -1); +} + +static bool ebpf_rss_mmap(struct EBPFRSSContext *ctx) +{ + if (!ebpf_rss_is_loaded(ctx)) { + return false; + } + + ctx->mmap_configuration = mmap(NULL, qemu_real_host_page_size(), + PROT_READ | PROT_WRITE, MAP_SHARED, + ctx->map_configuration, 0); + if (ctx->mmap_configuration == MAP_FAILED) { + trace_ebpf_error("eBPF RSS", "can not mmap eBPF configuration array"); + return false; + } + ctx->mmap_toeplitz_key = mmap(NULL, qemu_real_host_page_size(), + PROT_READ | PROT_WRITE, MAP_SHARED, + ctx->map_toeplitz_key, 0); + if (ctx->mmap_toeplitz_key == MAP_FAILED) { + trace_ebpf_error("eBPF RSS", "can not mmap eBPF toeplitz key"); + goto toeplitz_fail; + } + ctx->mmap_indirections_table = mmap(NULL, qemu_real_host_page_size(), + PROT_READ | PROT_WRITE, MAP_SHARED, + ctx->map_indirections_table, 0); + if (ctx->mmap_indirections_table == MAP_FAILED) { + trace_ebpf_error("eBPF RSS", "can not mmap eBPF indirection table"); + goto indirection_fail; + } + + return true; + +indirection_fail: + munmap(ctx->mmap_toeplitz_key, qemu_real_host_page_size()); +toeplitz_fail: + munmap(ctx->mmap_configuration, qemu_real_host_page_size()); + return false; +} + +static void ebpf_rss_munmap(struct EBPFRSSContext *ctx) +{ + if (!ebpf_rss_is_loaded(ctx)) { + return; + } + + munmap(ctx->mmap_indirections_table, qemu_real_host_page_size()); + munmap(ctx->mmap_toeplitz_key, qemu_real_host_page_size()); + munmap(ctx->mmap_configuration, qemu_real_host_page_size()); } bool ebpf_rss_load(struct EBPFRSSContext *ctx) { struct rss_bpf *rss_bpf_ctx; - if (ctx == NULL) { + if (ctx == NULL || ebpf_rss_is_loaded(ctx)) { return false; } @@ -66,26 +115,51 @@ bool ebpf_rss_load(struct EBPFRSSContext *ctx) ctx->map_toeplitz_key = bpf_map__fd( rss_bpf_ctx->maps.tap_rss_map_toeplitz_key); + if (!ebpf_rss_mmap(ctx)) { + goto error; + } + return true; error: rss_bpf__destroy(rss_bpf_ctx); ctx->obj = NULL; + ctx->program_fd = -1; return false; } -static bool ebpf_rss_set_config(struct EBPFRSSContext *ctx, - struct EBPFRSSConfig *config) +bool ebpf_rss_load_fds(struct EBPFRSSContext *ctx, int program_fd, + int config_fd, int toeplitz_fd, int table_fd) { - uint32_t map_key = 0; + if (ctx == NULL || ebpf_rss_is_loaded(ctx)) { + return false; + } - if (!ebpf_rss_is_loaded(ctx)) { + if (program_fd < 0 || config_fd < 0 || toeplitz_fd < 0 || table_fd < 0) { return false; } - if (bpf_map_update_elem(ctx->map_configuration, - &map_key, config, 0) < 0) { + + ctx->program_fd = program_fd; + ctx->map_configuration = config_fd; + ctx->map_toeplitz_key = toeplitz_fd; + ctx->map_indirections_table = table_fd; + + if (!ebpf_rss_mmap(ctx)) { + ctx->program_fd = -1; return false; } + + return true; +} + +static bool ebpf_rss_set_config(struct EBPFRSSContext *ctx, + struct EBPFRSSConfig *config) +{ + if (!ebpf_rss_is_loaded(ctx)) { + return false; + } + + memcpy(ctx->mmap_configuration, config, sizeof(*config)); return true; } @@ -93,27 +167,19 @@ static bool ebpf_rss_set_indirections_table(struct EBPFRSSContext *ctx, uint16_t *indirections_table, size_t len) { - uint32_t i = 0; - if (!ebpf_rss_is_loaded(ctx) || indirections_table == NULL || len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { return false; } - for (; i < len; ++i) { - if (bpf_map_update_elem(ctx->map_indirections_table, &i, - indirections_table + i, 0) < 0) { - return false; - } - } + memcpy(ctx->mmap_indirections_table, indirections_table, + sizeof(*indirections_table) * len); return true; } static bool ebpf_rss_set_toepliz_key(struct EBPFRSSContext *ctx, uint8_t *toeplitz_key) { - uint32_t map_key = 0; - /* prepare toeplitz key */ uint8_t toe[VIRTIO_NET_RSS_MAX_KEY_SIZE] = {}; @@ -123,10 +189,7 @@ static bool ebpf_rss_set_toepliz_key(struct EBPFRSSContext *ctx, memcpy(toe, toeplitz_key, VIRTIO_NET_RSS_MAX_KEY_SIZE); *(uint32_t *)toe = ntohl(*(uint32_t *)toe); - if (bpf_map_update_elem(ctx->map_toeplitz_key, &map_key, toe, - 0) < 0) { - return false; - } + memcpy(ctx->mmap_toeplitz_key, toe, VIRTIO_NET_RSS_MAX_KEY_SIZE); return true; } @@ -160,6 +223,17 @@ void ebpf_rss_unload(struct EBPFRSSContext *ctx) return; } - rss_bpf__destroy(ctx->obj); + ebpf_rss_munmap(ctx); + + if (ctx->obj) { + rss_bpf__destroy(ctx->obj); + } else { + close(ctx->program_fd); + close(ctx->map_configuration); + close(ctx->map_toeplitz_key); + close(ctx->map_indirections_table); + } + ctx->obj = NULL; + ctx->program_fd = -1; } diff --git a/ebpf/ebpf_rss.h b/ebpf/ebpf_rss.h index bf3f2572c7..239242b0d2 100644 --- a/ebpf/ebpf_rss.h +++ b/ebpf/ebpf_rss.h @@ -14,12 +14,19 @@ #ifndef QEMU_EBPF_RSS_H #define QEMU_EBPF_RSS_H +#define EBPF_RSS_MAX_FDS 4 + struct EBPFRSSContext { void *obj; int program_fd; int map_configuration; int map_toeplitz_key; int map_indirections_table; + + /* mapped eBPF maps for direct access to omit bpf_map_update_elem() */ + void *mmap_configuration; + void *mmap_toeplitz_key; + void *mmap_indirections_table; }; struct EBPFRSSConfig { @@ -36,6 +43,9 @@ bool ebpf_rss_is_loaded(struct EBPFRSSContext *ctx); bool ebpf_rss_load(struct EBPFRSSContext *ctx); +bool ebpf_rss_load_fds(struct EBPFRSSContext *ctx, int program_fd, + int config_fd, int toeplitz_fd, int table_fd); + bool ebpf_rss_set_all(struct EBPFRSSContext *ctx, struct EBPFRSSConfig *config, uint16_t *indirections_table, uint8_t *toeplitz_key); -- 2.39.1