File: main.cpp

package info (click to toggle)
bpfilter 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,076 kB
  • sloc: ansic: 30,397; sh: 1,383; cpp: 959; python: 495; yacc: 385; lex: 194; makefile: 9
file content (139 lines) | stat: -rw-r--r-- 3,217 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
 */

#include <benchmark/benchmark.h>
#include <cerrno>
#include <cstring>
#include <exception>
#include <format>
#include <span>
#include <unistd.h>

#include "benchmark.hpp"

namespace
{

void loadChainLargeSet(::benchmark::State &state)
{
    ::bf::Chain chain(::bf::config.bfcli);

    chain.insertRuleIPv4Set(state.range(0));

    for (auto _: state) {
        chain.apply();
    }
}

BENCHMARK(loadChainLargeSet)
    ->Arg(10000)
    ->Arg(100000)
    ->Arg(200000)
    ->Arg(500000)
    ->Iterations(10)
    ->Unit(benchmark::kMillisecond);

void firstRuleDropCounter(::benchmark::State &state)
{
    ::bf::Chain chain(::bf::config.bfcli);
    chain << "rule meta.l3_proto ipv6 counter DROP";
    chain.apply();
    auto prog = chain.getProgram();

    benchLoop(state)
    {
        if (prog.run(::bf::CGROUP_DROP, ::bf::pkt_local_ip6_tcp) < 0)
            state.SkipWithError("benchmark run failed");
    }

    state.counters["nInsn"] = prog.nInsn();
}

BENCHMARK(firstRuleDropCounter);

void dropAfterXRules(::benchmark::State &state)
{
    ::bf::Chain chain(::bf::config.bfcli);

    for (int i = 0; i < state.range(0); ++i)
        chain << std::format("rule meta.dport {} counter ACCEPT", i + 1);

    chain.apply();
    auto prog = chain.getProgram();

    benchLoop(state)
    {
        if (prog.run(::bf::CGROUP_DROP, ::bf::pkt_local_ip6_tcp) < 0)
            state.SkipWithError("benchmark run failed");
    }

    state.counters["nInsn"] = prog.nInsn();
}

BENCHMARK(dropAfterXRules)
    ->Arg(8)
    ->Arg(32)
    ->Arg(128)
    ->Arg(512)
    ->Arg(2048);
} // namespace

void adhocBenchmark(::benchmark::State &state, const ::std::string &ruleset)
{
    ::bf::Chain chain(::bf::config.bfcli);
    chain.repeat(ruleset, ::bf::config.adhocRepeat);
    chain.apply();
    auto prog = chain.getProgram();

    benchLoop(state)
    {
        if (prog.run(::bf::CGROUP_DROP, ::bf::pkt_local_ip6_tcp) < 0)
            state.SkipWithError("benchmark run failed");
    }

    state.counters["nInsn"] = prog.nInsn();
}

int main(int argc, char *argv[])
{
    if (geteuid() != 0) {
        err("the benchmark should be run as root");
        return -EPERM;
    }

    if (::bf::disableASLR(argv) < 0)
        return -1;

    if (::bf::setup(std::span<char *>(argv, argc)) < 0)
        return -1;

    ::benchmark::Initialize(&argc, argv, nullptr);

    if (!::bf::config.adhoc)
        ::bf::restorePermissions(::bf::config.outfile);

    if (::bf::config.adhoc) {
        ::benchmark::RegisterBenchmark("bf_adhoc", adhocBenchmark,
                                       *::bf::config.adhoc);
    }

    try {
        if (::bf::config.runDaemon) {
            auto daemon = bf::Daemon(
                ::bf::config.bpfilter,
                bf::Daemon::Options().transient().noIptables().noNftables());
            ::benchmark::RunSpecifiedBenchmarks();
        } else {
            ::benchmark::RunSpecifiedBenchmarks();
        }
    } catch (const ::std::exception &e) {
        err("failed to run benchmark: {}", e.what());
        return -1;
    }

    ::benchmark::Shutdown();

    return 0;
}