From patchwork Mon May 17 22:53:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Banshchikov X-Patchwork-Id: 12263317 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C86DFC43462 for ; Mon, 17 May 2021 22:54:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ACC8661261 for ; Mon, 17 May 2021 22:54:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344308AbhEQWza (ORCPT ); Mon, 17 May 2021 18:55:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344333AbhEQWzR (ORCPT ); Mon, 17 May 2021 18:55:17 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4180C06138C for ; Mon, 17 May 2021 15:54:00 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id z19-20020a7bc7d30000b029017521c1fb75so421902wmk.0 for ; Mon, 17 May 2021 15:54:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ubique-spb-ru.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6BZu86G+a+CQ5lboyHSi67mKyj7jNYy4dIGKIAp22DI=; b=ZjDR7WQTSVwGdyZSJpRB3DAGB9Oy/YR5OWbYZIgEtMADji3qezP+4HBn1y/Zs2+Cd/ s338aOt6EQJfilz9zcRXqF0Vqq95qv7a54JjTbh7ZUHUiXBxGanQzaDYrqUEqwz8Vg0o OcITSS5XvHNd3vooD3eV3q/uiRZafuEE6HhWbsKKLHJYisaufnfx4PG0QV4ZIjD129ls LWtR0Uq4eWhrO2j18sur44cESeExi5nahOI3kR8RbOMGJDI9rCA4gNS9YFt548aDXLjn sL9DdMc3UYtaNY40MK8yG0iUwhY6Ua3NmWH+Q1D9Ufqr0Odr6YWs1O8n+IONw90G4/QI 0JlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6BZu86G+a+CQ5lboyHSi67mKyj7jNYy4dIGKIAp22DI=; b=bL/aBaLxt9ZNzlBr+JM5CZkNYT4YTcQYnx3BJPqApZQvb7LEHe2my07DK+leSYmc0V yXoWjR98OILnKYk7cjfrjSeQkE9jXECsVHYNnB2UwYlhvshC8zF39MDiJUaMkcqMJTUB JU8wg6oVZEgFrgGPA4SH+sVdfNsnXM46A/DdI/YQ2UHlyjfVFGqybl4ALYvRBxTcKTBp DtqIxx5JM9zAEECn2B388gXiSE9SmZP88C+idDBnKUxqG8BbHyw9OW95WdmSDdnMeqX4 foqieBnHctpcsuRlfPfKQXzS6qhPgGGcx53+XDTvd9vDArmP8cQ/B8EHEyN9khXCrgtk uAAg== X-Gm-Message-State: AOAM531lURfLMRtPf9eQKooJ8N/i4g/k+v1qDnX9fOki8g9FaYpEOWpa 8UKx7tqtLGMN72g/YpbWYr6eYOKpkAUHnljuWu4= X-Google-Smtp-Source: ABdhPJxoSs8hoHf5GGDborHjkwA6ABbZNXeATkpzR/G2JBWzu29Ki9PbnrX25EsDtvv2IpHkrIXXdg== X-Received: by 2002:a1c:4d05:: with SMTP id o5mr1832282wmh.131.1621292039409; Mon, 17 May 2021 15:53:59 -0700 (PDT) Received: from localhost ([154.21.15.43]) by smtp.gmail.com with ESMTPSA id p7sm18925555wrt.24.2021.05.17.15.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 15:53:59 -0700 (PDT) From: Dmitrii Banshchikov To: bpf@vger.kernel.org Cc: Dmitrii Banshchikov , ast@kernel.org, davem@davemloft.net, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, netdev@vger.kernel.org, rdna@fb.com Subject: [PATCH bpf-next 11/11] bpfilter: Handle setsockopts Date: Tue, 18 May 2021 02:53:08 +0400 Message-Id: <20210517225308.720677-12-me@ubique.spb.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210517225308.720677-1-me@ubique.spb.ru> References: <20210517225308.720677-1-me@ubique.spb.ru> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Use earlier introduced infrastructure for and handle setsockopt(2) calls. Signed-off-by: Dmitrii Banshchikov --- net/bpfilter/main.c | 99 ++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/net/bpfilter/main.c b/net/bpfilter/main.c index 05e1cfc1e5cd..19c8c2d7ef87 100644 --- a/net/bpfilter/main.c +++ b/net/bpfilter/main.c @@ -1,64 +1,71 @@ // SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021 Telegram FZ-LLC + */ + #define _GNU_SOURCE -#include + +#include #include + #include -#include -#include -#include -#include "../../include/uapi/linux/bpf.h" -#include -#include "msgfmt.h" +#include -FILE *debug_f; +#include "bflog.h" +#include "context.h" +#include "io.h" +#include "msgfmt.h" +#include "sockopt.h" -static int handle_get_cmd(struct mbox_request *cmd) +static int setup_context(struct context *ctx) { - switch (cmd->cmd) { - case 0: - return 0; - default: - break; - } - return -ENOPROTOOPT; -} + ctx->log_file = fopen("/dev/kmsg", "w"); + if (!ctx->log_file) + return -errno; -static int handle_set_cmd(struct mbox_request *cmd) -{ - return -ENOPROTOOPT; + setvbuf(ctx->log_file, 0, _IOLBF, 0); + ctx->log_level = BFLOG_LEVEL_NOTICE; + + return 0; } -static void loop(void) +static void loop(struct context *ctx) { - while (1) { - struct mbox_request req; - struct mbox_reply reply; - int n; - - n = read(0, &req, sizeof(req)); - if (n != sizeof(req)) { - fprintf(debug_f, "invalid request %d\n", n); - return; - } - - reply.status = req.is_set ? - handle_set_cmd(&req) : - handle_get_cmd(&req); - - n = write(1, &reply, sizeof(reply)); - if (n != sizeof(reply)) { - fprintf(debug_f, "reply failed %d\n", n); - return; - } + struct mbox_request req; + struct mbox_reply reply; + int err; + + for (;;) { + err = read_exact(STDIN_FILENO, &req, sizeof(req)); + if (err) + BFLOG_FATAL(ctx, "cannot read request: %s\n", strerror(-err)); + + reply.status = handle_sockopt_request(ctx, &req); + + err = write_exact(STDOUT_FILENO, &reply, sizeof(reply)); + if (err) + BFLOG_FATAL(ctx, "cannot write reply: %s\n", strerror(-err)); } } int main(void) { - debug_f = fopen("/dev/kmsg", "w"); - setvbuf(debug_f, 0, _IOLBF, 0); - fprintf(debug_f, "Started bpfilter\n"); - loop(); - fclose(debug_f); + struct context ctx; + int err; + + err = create_context(&ctx); + if (err) + return err; + + err = setup_context(&ctx); + if (err) { + free_context(&ctx); + return err; + } + + BFLOG_NOTICE(&ctx, "started\n"); + + loop(&ctx); + return 0; }