mirror of
https://github.com/zeek/zeek.git
synced 2025-10-15 04:58:21 +00:00
Add a new plugin test with verbose IO source output
This is mostly for documentation/verification purposes of how the IO loop currently does draining and when it picks up FD based (non packet) IO source. For example, it shows that currently FD based sources are processed fairly delayed and that we now also process two timeout sources that are ready.
This commit is contained in:
parent
46c432dc8b
commit
38c4611c7e
5 changed files with 842 additions and 0 deletions
45
testing/btest/plugins/iosource-plugin.zeek
Normal file
45
testing/btest/plugins/iosource-plugin.zeek
Normal file
|
@ -0,0 +1,45 @@
|
|||
# @TEST-EXEC: ${DIST}/auxil/zeek-aux/plugin-support/init-plugin -u . Demo Iosource
|
||||
# @TEST-EXEC: cp -r %DIR/iosource-plugin/* .
|
||||
|
||||
# @TEST-EXEC: ./configure --zeek-dist=${DIST} && make
|
||||
#
|
||||
# @TEST-EXEC: ZEEK_PLUGIN_PATH=`pwd` zeek -Bmain-loop -b %INPUT -r $TRACES/wikipedia.trace > output
|
||||
# @TEST-EXEC: TEST_DIFF_CANONIFIER= btest-diff output
|
||||
|
||||
@load-plugin Demo::Iosource
|
||||
|
||||
global flushes = 0;
|
||||
global packets = 0;
|
||||
|
||||
# Default is 100 for pcaps, but that only triggers a single Poll() when
|
||||
# reading wikipedia.trace. Tune it down a bit so Process on the FdSources
|
||||
# is called more often.
|
||||
redef io_poll_interval_default = 10;
|
||||
|
||||
event zeek_init() {
|
||||
print network_time(), "zeek_init";
|
||||
}
|
||||
|
||||
event network_time_init() {
|
||||
print network_time(), "network_time_init";
|
||||
}
|
||||
|
||||
event raw_packet(p: raw_pkt_hdr)
|
||||
{
|
||||
++packets;
|
||||
print network_time(), "raw_packet", packets;
|
||||
}
|
||||
|
||||
event event_queue_flush_point() {
|
||||
++flushes;
|
||||
print network_time(), "event_queue_flush_point", flushes;
|
||||
}
|
||||
|
||||
event net_done(ts: time) {
|
||||
print network_time(), "net_done", ts;
|
||||
}
|
||||
|
||||
event zeek_done() {
|
||||
print network_time(), "zeek_done";
|
||||
print network_time(), "flushes", flushes, "packets", packets;
|
||||
}
|
0
testing/btest/plugins/iosource-plugin/.btest-ignore
Normal file
0
testing/btest/plugins/iosource-plugin/.btest-ignore
Normal file
69
testing/btest/plugins/iosource-plugin/src/Plugin.cc
Normal file
69
testing/btest/plugins/iosource-plugin/src/Plugin.cc
Normal file
|
@ -0,0 +1,69 @@
|
|||
|
||||
#include "Plugin.h"
|
||||
|
||||
namespace zeek::run_state
|
||||
{
|
||||
extern double processing_start_time;
|
||||
}
|
||||
|
||||
namespace btest::plugin::Demo_Iosource
|
||||
{
|
||||
Plugin plugin;
|
||||
}
|
||||
|
||||
using namespace btest::plugin::Demo_Iosource;
|
||||
|
||||
zeek::plugin::Configuration Plugin::Configure()
|
||||
{
|
||||
EnableHook(zeek::plugin::HOOK_DRAIN_EVENTS, 0);
|
||||
|
||||
zeek::plugin::Configuration config;
|
||||
config.name = "Demo::Iosource";
|
||||
config.description = "This is a iosource";
|
||||
config.version.major = 1;
|
||||
config.version.minor = 0;
|
||||
config.version.patch = 0;
|
||||
return config;
|
||||
}
|
||||
|
||||
void Plugin::InitPostScript()
|
||||
{
|
||||
std::fprintf(stdout, "%.6f InitPostScript\n", zeek::run_state::network_time);
|
||||
ts1 = new TimeoutSource("timeout-source-1");
|
||||
ts2 = new TimeoutSource("timeout-source-2");
|
||||
fd1 = new FdSource("fd-source-1");
|
||||
fd2 = new FdSource("fd-source-2");
|
||||
}
|
||||
|
||||
void Plugin::HookDrainEvents()
|
||||
{
|
||||
++round;
|
||||
if ( zeek::run_state::processing_start_time != 0.0 ) // ignore drains from dispatch_packet
|
||||
return;
|
||||
//
|
||||
std::fprintf(stdout, "%.6f HookDrainEvents %d\n", zeek::run_state::network_time, round);
|
||||
|
||||
if ( (round % 9) == 0 )
|
||||
{
|
||||
std::fprintf(stdout, "%.6f Firing %s\n", zeek::run_state::network_time, ts1->Tag());
|
||||
ts1->Fire();
|
||||
}
|
||||
|
||||
if ( (round % 19) == 0 )
|
||||
{
|
||||
std::fprintf(stdout, "%.6f Firing %s\n", zeek::run_state::network_time, ts2->Tag());
|
||||
ts2->Fire();
|
||||
}
|
||||
|
||||
if ( (round % 19) == 0 )
|
||||
{
|
||||
std::fprintf(stdout, "%.6f Firing %s\n", zeek::run_state::network_time, fd1->Tag());
|
||||
fd1->Fire();
|
||||
}
|
||||
|
||||
if ( (round % 23) == 0 )
|
||||
{
|
||||
std::fprintf(stdout, "%.6f Firing %s\n", zeek::run_state::network_time, fd2->Tag());
|
||||
fd2->Fire();
|
||||
}
|
||||
}
|
83
testing/btest/plugins/iosource-plugin/src/Plugin.h
Normal file
83
testing/btest/plugins/iosource-plugin/src/Plugin.h
Normal file
|
@ -0,0 +1,83 @@
|
|||
|
||||
#pragma once
|
||||
#include <zeek/Flare.h>
|
||||
#include <zeek/plugin/Plugin.h>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "zeek/iosource/Manager.h"
|
||||
|
||||
namespace btest::plugin::Demo_Iosource
|
||||
{
|
||||
|
||||
class TimeoutSource : public zeek::iosource::IOSource
|
||||
{
|
||||
public:
|
||||
TimeoutSource(std::string_view ident) : ident(ident)
|
||||
{
|
||||
zeek::iosource_mgr->Register(this, true /* don't count */);
|
||||
};
|
||||
|
||||
void Process() override
|
||||
{
|
||||
std::fprintf(stdout, "%.3f %s TimeoutSource.Process()\n", zeek::run_state::network_time,
|
||||
ident.c_str());
|
||||
on = false;
|
||||
}
|
||||
|
||||
double GetNextTimeout() override { return on ? 0.0 : 0.1; };
|
||||
|
||||
const char* Tag() override { return ident.c_str(); };
|
||||
|
||||
void Fire() { on = true; }
|
||||
|
||||
private:
|
||||
std::string ident;
|
||||
bool on = false;
|
||||
};
|
||||
|
||||
class FdSource : public zeek::iosource::IOSource
|
||||
{
|
||||
public:
|
||||
FdSource(std::string_view ident) : ident(ident)
|
||||
{
|
||||
zeek::iosource_mgr->Register(this, true /* don't count */);
|
||||
if ( ! zeek::iosource_mgr->RegisterFd(flare.FD(), this) )
|
||||
zeek::reporter->FatalError("Failed to register flare FD");
|
||||
}
|
||||
|
||||
void Process() override
|
||||
{
|
||||
std::fprintf(stdout, "%.3f %s FdSource.Process()\n", zeek::run_state::network_time,
|
||||
ident.c_str());
|
||||
flare.Extinguish();
|
||||
}
|
||||
|
||||
double GetNextTimeout() override { return -1; }
|
||||
|
||||
const char* Tag() override { return ident.c_str(); };
|
||||
|
||||
void Fire() { flare.Fire(); }
|
||||
|
||||
private:
|
||||
std::string ident;
|
||||
zeek::detail::Flare flare;
|
||||
};
|
||||
|
||||
class Plugin : public zeek::plugin::Plugin
|
||||
{
|
||||
protected:
|
||||
zeek::plugin::Configuration Configure() override;
|
||||
void InitPostScript() override;
|
||||
void HookDrainEvents() override;
|
||||
|
||||
private:
|
||||
int round = 0;
|
||||
TimeoutSource* ts1 = nullptr;
|
||||
TimeoutSource* ts2 = nullptr;
|
||||
FdSource* fd1 = nullptr;
|
||||
FdSource* fd2 = nullptr;
|
||||
};
|
||||
|
||||
extern Plugin plugin;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue