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

[part 1/n] Introduce vtable-based dispatch #213

Merged
merged 15 commits into from
Jun 6, 2022
Merged
38 changes: 29 additions & 9 deletions userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,13 @@ list(APPEND targetfiles
syscall_info_table.c
../../driver/dynamic_params_table.c
../../driver/event_table.c
../../driver/flags_table.c)

if(NOT APPLE)
list(APPEND targetfiles
scap_udig.c)
endif()

../../driver/flags_table.c
ringbuffer/devset.c)

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
list(APPEND targetfiles
scap_bpf.c
scap_kmod.c
../../driver/syscall_table.c
../../driver/fillers_table.c)

Expand All @@ -117,8 +113,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "SunOS")
socket nsl)
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_libraries(scap
elf
rt)
elf)
elseif (WIN32)
target_link_libraries(scap
Ws2_32.lib)
Expand Down Expand Up @@ -163,3 +158,28 @@ if(CREATE_TEST_TARGETS AND NOT WIN32)
# Add unit test directories
add_subdirectory(test)
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_subdirectory(linux)
FedeDP marked this conversation as resolved.
Show resolved Hide resolved
include_directories(linux)
elseif(WIN32)
add_subdirectory(win32)
include_directories(win32)
endif()

add_definitions(-DHAS_ENGINE_NOOP)
add_subdirectory(engine/noop)
# don't link the noop engine to libscap directly,
# it's a helper library for other engines (it's completely useless on its own)

add_definitions(-DHAS_ENGINE_NODRIVER)
add_subdirectory(engine/nodriver)
target_link_libraries(scap scap_engine_nodriver)

add_definitions(-DHAS_ENGINE_SOURCE_PLUGIN)
add_subdirectory(engine/source_plugin)
target_link_libraries(scap scap_engine_source_plugin)

add_definitions(-DHAS_ENGINE_UDIG)
add_subdirectory(engine/udig)
target_link_libraries(scap scap_engine_udig)
3 changes: 3 additions & 0 deletions userspace/libscap/engine/nodriver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(${LIBSCAP_INCLUDE_DIRS} ../noop)
add_library(scap_engine_nodriver nodriver.c)
target_link_libraries(scap_engine_nodriver scap_engine_noop)
71 changes: 71 additions & 0 deletions userspace/libscap/engine/nodriver/nodriver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright (C) 2022 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include <stdio.h>
#include <stdlib.h>

#include "nodriver.h"
#include "noop.h"

#include "scap.h"
#include "scap-int.h"
#include "../common/strlcpy.h"
#include "gettimeofday.h"
#include "sleep.h"

static struct nodriver_engine* alloc_handle(scap_t* main_handle, char* lasterr_ptr)
{
struct nodriver_engine *engine = calloc(1, sizeof(struct nodriver_engine));
if(engine)
{
engine->m_lasterr = lasterr_ptr;
}
return engine;
}

static int32_t next(struct scap_engine_handle handle, scap_evt** pevent, uint16_t* pcpuid)
{
static scap_evt evt;
evt.len = 0;
evt.tid = -1;
evt.type = PPME_SCAPEVENT_X;
evt.nparams = 0;

sleep_ms(100);

evt.ts = get_timestamp_ns();
*pevent = &evt;
return SCAP_SUCCESS;
}

const struct scap_vtable scap_nodriver_engine = {
.name = "nodriver",
.mode = SCAP_MODE_NODRIVER,

.alloc_handle = alloc_handle,
.init = NULL,
.free_handle = noop_free_handle,
.close = noop_close_engine,
.next = next,
.start_capture = noop_start_capture,
.stop_capture = noop_stop_capture,
.configure = noop_configure,
.get_stats = noop_get_stats,
.get_n_tracepoint_hit = noop_get_n_tracepoint_hit,
.get_n_devs = noop_get_n_devs,
.get_max_buf_used = noop_get_max_buf_used,
};
28 changes: 28 additions & 0 deletions userspace/libscap/engine/nodriver/nodriver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright (C) 2022 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
#pragma once

#include <stdint.h>

struct scap;

struct nodriver_engine
{
char* m_lasterr;
};

#define SCAP_HANDLE_T struct nodriver_engine
2 changes: 2 additions & 0 deletions userspace/libscap/engine/noop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include_directories(${LIBSCAP_INCLUDE_DIRS})
add_library(scap_engine_noop noop.c)
121 changes: 121 additions & 0 deletions userspace/libscap/engine/noop/noop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright (C) 2022 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include <stdio.h>
#include <stdlib.h>

struct noop_engine
{
char *m_lasterr;
};

#define SCAP_HANDLE_T struct noop_engine

#include "noop.h"
#include "scap.h"
#include "scap-int.h"
#include "../common/strlcpy.h"

struct noop_engine* noop_alloc_handle(scap_t* main_handle, char* lasterr_ptr)
{
struct noop_engine *engine = calloc(1, sizeof(struct noop_engine));
if(engine)
{
engine->m_lasterr = lasterr_ptr;
}
return engine;
}

void noop_free_handle(struct scap_engine_handle engine)
{
free(engine.m_handle);
}

int noop_close_engine(struct scap_engine_handle engine)
{
return SCAP_SUCCESS;
}

int32_t noop_next(struct scap_engine_handle handle, scap_evt** pevent, uint16_t* pcpuid)
{
return SCAP_EOF;
}

int32_t noop_start_capture(struct scap_engine_handle engine)
{
return SCAP_SUCCESS;
}

int32_t noop_stop_capture(struct scap_engine_handle engine)
{
return SCAP_SUCCESS;
}

int32_t unimplemented_op(char* err, size_t err_size)
{
strlcpy(err, "Operation not implemented", err_size);
return SCAP_FAILURE;
}

int32_t noop_configure(struct scap_engine_handle engine, enum scap_setting setting, unsigned long arg1, unsigned long arg2)
{
// the open path disables dropping mode so report success even if we
// don't really support it
if(setting == SCAP_SAMPLING_RATIO && arg2 == 0)
{
return SCAP_SUCCESS;
}
return unimplemented_op(engine.m_handle->m_lasterr, SCAP_LASTERR_SIZE);
}

int32_t noop_get_stats(struct scap_engine_handle engine, scap_stats* stats)
{
return SCAP_SUCCESS;
}

int32_t noop_get_n_tracepoint_hit(struct scap_engine_handle engine, long* ret)
{
return SCAP_NOT_SUPPORTED;
}

uint32_t noop_get_n_devs(struct scap_engine_handle engine)
{
return 0;
}

uint64_t noop_get_max_buf_used(struct scap_engine_handle engine)
{
return 0;
}

const struct scap_vtable scap_noop_engine = {
.name = "noop",
.mode = SCAP_MODE_NODRIVER,

.alloc_handle = noop_alloc_handle,
.init = NULL,
.free_handle = noop_free_handle,
.close = noop_close_engine,
.next = noop_next,
.start_capture = noop_start_capture,
.stop_capture = noop_stop_capture,
.configure = noop_configure,
.get_stats = noop_get_stats,
.get_n_tracepoint_hit = noop_get_n_tracepoint_hit,
.get_n_devs = noop_get_n_devs,
.get_max_buf_used = noop_get_max_buf_used,
};
40 changes: 40 additions & 0 deletions userspace/libscap/engine/noop/noop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright (C) 2022 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#pragma once

#include "engine_handle.h"
#include "scap_vtable.h"

typedef struct scap scap_t;
typedef struct ppm_evt_hdr scap_evt;
typedef struct scap_stats scap_stats;

struct noop_engine* noop_alloc_handle(scap_t* main_handle, char* lasterr_ptr);
void noop_free_handle(struct scap_engine_handle engine);
int noop_close_engine(struct scap_engine_handle engine);
int32_t noop_next(struct scap_engine_handle handle, scap_evt** pevent, uint16_t* pcpuid);
int32_t noop_start_capture(struct scap_engine_handle engine);
int32_t noop_stop_capture(struct scap_engine_handle engine);
int32_t unimplemented_op(char* err, size_t err_size);
int32_t noop_configure(struct scap_engine_handle engine, enum scap_setting setting, unsigned long arg1, unsigned long arg2);
int32_t noop_get_stats(struct scap_engine_handle engine, scap_stats* stats);
int32_t noop_get_n_tracepoint_hit(struct scap_engine_handle engine, long* ret);
uint32_t noop_get_n_devs(struct scap_engine_handle engine);
uint64_t noop_get_max_buf_used(struct scap_engine_handle engine);

extern const struct scap_vtable scap_noop_engine;
3 changes: 3 additions & 0 deletions userspace/libscap/engine/source_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(${LIBSCAP_INCLUDE_DIRS} ../noop)
add_library(scap_engine_source_plugin source_plugin.c)
target_link_libraries(scap_engine_source_plugin scap_engine_noop)
Loading