Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: user provided threads and packets - v5 #11769

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

jasonish
Copy link
Member

Update of #11711 to show how packet dropping might be handled.

amirabell and others added 14 commits September 12, 2024 12:51
Add library source and runmode modules. Reorganized
library example to create a worker thread and replay a pcap
file using the library mode.
No API layer is added at this stage.

Edits by Jason Ish:
- fix guard
- add copyright/license headers
To keep the simple example simple, move the lib based capture method
example to its own example.
- the generated binaries for lib examples
- LSP files
- man pages
Worker threads not created by Suricata, but instead a library user
should not be joined, as Suricata does not have access to their thread
handle, and it may in-fact be an unjoinable thread, such as the main
process.

When the thread ID is 0, assume the thread is "externally" managed,
but still mark is as dead to satisfy Suricata's view of the thread.
Use the more conventional "--" command line handling to separate the
arguments. The first set will be passed to Suricata, and the args
after "--" will be handled by the example. Currently this is a single
PCAP filename, but will be extended to a list of PCAP filenames.
Also use a proper return type (ThreadVars *).
Update ThreadVars creation in lib mode to have the worker_id provided
by the user.
In the library capture example, show how the packet counter can be
updated.
The variable run is not needed, we can just run an infinite loop and
break when needed.
Refactor TmThreadsSlotPktAcqLoop for user provided thread by breaking
out the init and finish code into their own functions.

For user provided threads, Suricata should not "drive" the thread, but
the setup and finish code is the same.

The finish function is exported so it can be called by the user
application when its receive loop or equivalent is done.
Also remove function to set the library mode. This is easy enough to
do with SCRunmodeSet, and we don't want to add a specific setter for
each and every runmode.
Add a release packet callback where the action can be checked for drop.
Comment on lines +94 to +98
PKT_SET_SRC(p, PKT_SRC_WIRE);
p->ts = SCTIME_FROM_TIMEVAL(&pkthdr.ts);
p->datalink = datalink;
p->livedev = device;
p->ReleasePacket = ReleasePacket;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These all need functions to avoid internal access to packet.

usleep(100);

SuricataPostInit();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need something like SuricataMainLoop here.

Comment on lines +116 to +117
int TmModuleLibHandlePacket(ThreadVars *tv, LiveDevice *device, const uint8_t *data, int datalink,
struct timeval ts, uint32_t len, uint32_t tenant_id, uint32_t flags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now go. I think most users of the lib who want to drive their own packets through won't be satisfied with this abstraction where they need a better handle on the Packet pre and post, and perform IPS as can be seen with the updated example in this PR.

Comment on lines +121 to +125
if (data == NULL) {
TmThreadsSetFlag(tv, THV_CAPTURE_INJECT_PKT);
TmThreadsCaptureHandleTimeout(tv, NULL);
SCReturnInt(TM_ECODE_OK);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@victorjulien Any guidelines as when a custom capture method should push something like this through?

Comment on lines +132 to +137
/* If we are processing a PCAP and it is the first packet we need to set the timestamp. */
SCTime_t timestamp = SCTIME_FROM_TIMEVAL(&ts);
if (!time_set && !TimeModeIsLive()) {
TmThreadsInitThreadsTimestamp(timestamp);
time_set = true;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left this out of the example app, I think if a library user is doing offline mode, they could "prime" this early by peaking into their packet source, or locking on some conditional until set. Anyways, when the threads and packets are in the users control there are a few ways to do. Just need documentation and it should still be in the example.

Comment on lines -230 to +205
static void *TmThreadsSlotPktAcqLoop(void *td)
static bool TmThreadsSlotPktAcqLoopInit(ThreadVars *tv)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@victorjulien Would like your eyes on how I broke this function up as a user driving there thread needs this to return, but then do the finish stuff later on.

Copy link

codecov bot commented Sep 12, 2024

Codecov Report

Attention: Patch coverage is 24.53988% with 123 lines in your changes missing coverage. Please review.

Project coverage is 82.49%. Comparing base (31bed10) to head (fe89a69).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11769      +/-   ##
==========================================
- Coverage   82.62%   82.49%   -0.14%     
==========================================
  Files         919      921       +2     
  Lines      248979   249105     +126     
==========================================
- Hits       205722   205499     -223     
- Misses      43257    43606     +349     
Flag Coverage Δ
fuzzcorpus 60.28% <7.97%> (-0.59%) ⬇️
livemode 18.71% <23.31%> (-0.01%) ⬇️
pcap 44.12% <23.92%> (-0.02%) ⬇️
suricata-verify 61.84% <23.31%> (-0.05%) ⬇️
unittests 58.97% <7.97%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

ERROR:

ERROR: QA failed on SURI_TLPR1_alerts_cmp.

Pipeline 22592

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants