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

refactor(userspace/libsinsp)!: file descriptor tables for better OOP design and extensibility #1637

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 32 additions & 33 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ int lua_cbacks::get_machine_info(lua_State *ls)

int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool barebone)
{
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;
uint32_t j;
sinsp_filter_compiler* compiler = NULL;
sinsp_filter* filter = NULL;
Expand Down Expand Up @@ -789,22 +788,22 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
{
bool match = false;

for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
{
fdtable->loop([&](int64_t fd, sinsp_fdinfo& fdi) {
tevt.m_tinfo = &tinfo;
tevt.m_fdinfo = &(fdit->second);
tevt.m_fdinfo = &fdi;
tscapevt.tid = tinfo.m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdit->first;
tevt.m_tinfo->m_lastevent_fd = fd;

if(filter->run(&tevt))
{
match = true;
break;
return false; // break out of the loop
}

tevt.m_tinfo->m_lastevent_fd = tlefd;
}
return true;
});

if(!match)
{
Expand Down Expand Up @@ -913,19 +912,19 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb

if(include_fds)
{
for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
{
fdtable->loop([&](int64_t fd, sinsp_fdinfo& fdi) {
tevt.m_tinfo = &tinfo;
tevt.m_fdinfo = &(fdit->second);
tevt.m_fdinfo = &fdi;
tscapevt.tid = tinfo.m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdit->first;
tevt.m_tinfo->m_lastevent_fd = fd;

if(filter != NULL)
{
if(filter->run(&tevt) == false)
{
continue;
// continue the loop to next iteration
return true;
}
}

Expand All @@ -935,14 +934,14 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
if(!barebone)
{
lua_pushliteral(ls, "name");
lua_pushstring(ls, fdit->second.tostring_clean().c_str());
lua_pushstring(ls, fdi.tostring_clean().c_str());
lua_settable(ls, -3);
lua_pushliteral(ls, "type");
lua_pushstring(ls, fdit->second.get_typestring());
lua_pushstring(ls, fdi.get_typestring());
lua_settable(ls, -3);
}

scap_fd_type evt_type = fdit->second.m_type;
scap_fd_type evt_type = fdi.m_type;
if(evt_type == SCAP_FD_IPV4_SOCK || evt_type == SCAP_FD_IPV4_SERVSOCK ||
evt_type == SCAP_FD_IPV6_SOCK || evt_type == SCAP_FD_IPV6_SERVSOCK)
{
Expand All @@ -957,38 +956,38 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
{
include_client = true;
af = AF_INET;
cip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4info.m_fields.m_sip);
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4info.m_fields.m_dip);
cport = fdit->second.m_sockinfo.m_ipv4info.m_fields.m_sport;
sport = fdit->second.m_sockinfo.m_ipv4info.m_fields.m_dport;
is_server = fdit->second.is_role_server();
cip = (uint8_t*)&(fdi.m_sockinfo.m_ipv4info.m_fields.m_sip);
sip = (uint8_t*)&(fdi.m_sockinfo.m_ipv4info.m_fields.m_dip);
cport = fdi.m_sockinfo.m_ipv4info.m_fields.m_sport;
sport = fdi.m_sockinfo.m_ipv4info.m_fields.m_dport;
is_server = fdi.is_role_server();
}
else if (evt_type == SCAP_FD_IPV4_SERVSOCK)
{
include_client = false;
af = AF_INET;
cip = NULL;
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4serverinfo.m_ip);
sport = fdit->second.m_sockinfo.m_ipv4serverinfo.m_port;
sip = (uint8_t*)&(fdi.m_sockinfo.m_ipv4serverinfo.m_ip);
sport = fdi.m_sockinfo.m_ipv4serverinfo.m_port;
is_server = true;
}
else if (evt_type == SCAP_FD_IPV6_SOCK)
{
include_client = true;
af = AF_INET6;
cip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6info.m_fields.m_sip);
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6info.m_fields.m_dip);
cport = fdit->second.m_sockinfo.m_ipv6info.m_fields.m_sport;
sport = fdit->second.m_sockinfo.m_ipv6info.m_fields.m_dport;
is_server = fdit->second.is_role_server();
cip = (uint8_t*)&(fdi.m_sockinfo.m_ipv6info.m_fields.m_sip);
sip = (uint8_t*)&(fdi.m_sockinfo.m_ipv6info.m_fields.m_dip);
cport = fdi.m_sockinfo.m_ipv6info.m_fields.m_sport;
sport = fdi.m_sockinfo.m_ipv6info.m_fields.m_dport;
is_server = fdi.is_role_server();
}
else
{
include_client = false;
af = AF_INET6;
cip = NULL;
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6serverinfo.m_ip);
sport = fdit->second.m_sockinfo.m_ipv6serverinfo.m_port;
sip = (uint8_t*)&(fdi.m_sockinfo.m_ipv6serverinfo.m_ip);
sport = fdi.m_sockinfo.m_ipv6serverinfo.m_port;
is_server = true;
}

Expand Down Expand Up @@ -1039,7 +1038,7 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb

// l4proto
const char* l4ps;
scap_l4_proto l4p = fdit->second.get_l4proto();
scap_l4_proto l4p = fdi.get_l4proto();

switch(l4p)
{
Expand Down Expand Up @@ -1069,8 +1068,9 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
// is_server
string l4proto;

lua_rawseti(ls,-2, (uint32_t)fdit->first);
}
lua_rawseti(ls,-2, (uint32_t)fd);
return true;
});
}


Expand Down Expand Up @@ -1114,7 +1114,6 @@ int lua_cbacks::get_thread_table_barebone_nofds(lua_State *ls)
int lua_cbacks::get_container_table(lua_State *ls)
{
#ifndef _WIN32
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;
uint32_t j;
sinsp_evt tevt;

Expand Down
1 change: 1 addition & 0 deletions userspace/chisel/chisel_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ void chisel_table::process_proctable(sinsp_evt* evt)
tevt.m_cpuid = 0;
tevt.m_evtnum = 0;
tevt.m_pevt = &tscapevt;
tevt.m_fdinfo_ref.reset();
tevt.m_fdinfo = NULL;

threadtable->loop([&] (sinsp_threadinfo& tinfo) {
Expand Down
9 changes: 5 additions & 4 deletions userspace/libsinsp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ int sinsp_evt::render_fd_json(Json::Value *ret, int64_t fd, const char** resolve

if(fd >= 0)
{
sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd);
sinsp_fdinfo *fdinfo = tinfo->get_fd(fd);
if(fdinfo)
{
char tch = fdinfo->get_typechar();
Expand Down Expand Up @@ -695,7 +695,7 @@ char* sinsp_evt::render_fd(int64_t fd, const char** resolved_str, sinsp_evt::par

if(fd >= 0)
{
sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd);
sinsp_fdinfo *fdinfo = tinfo->get_fd(fd);
if(fdinfo)
{
char tch = fdinfo->get_typechar();
Expand Down Expand Up @@ -1295,7 +1295,7 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s
int64_t fd = 0;
memcpy(&fd, param->m_val + pos, sizeof(uint64_t));

sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd);
sinsp_fdinfo *fdinfo = tinfo->get_fd(fd);
if(fdinfo)
{
tch = fdinfo->get_typechar();
Expand Down Expand Up @@ -2111,11 +2111,12 @@ bool sinsp_evt::clone_event(sinsp_evt &dest, const sinsp_evt &src)

// fd info
dest.m_fdinfo = nullptr;
dest.m_fdinfo_ref.reset();
if (src.m_fdinfo != nullptr)
{
//m_fdinfo_ref is only used to keep a handle to this
// copy of the fdinfo which was copied from the global fdinfo table
dest.m_fdinfo_ref.reset(new sinsp_fdinfo_t(*src.m_fdinfo));
dest.m_fdinfo_ref = std::move(src.m_fdinfo->clone());
dest.m_fdinfo = dest.m_fdinfo_ref.get();
}
dest.m_fdinfo_name_changed = src.m_fdinfo_name_changed;
Expand Down
13 changes: 9 additions & 4 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limitations under the License.
#include <libsinsp/gen_filter.h>
#include <libsinsp/settings.h>
#include <libsinsp/sinsp_exception.h>
#include <libsinsp/fdinfo.h>

class sinsp;
class sinsp_threadinfo;
Expand Down Expand Up @@ -370,7 +371,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event

\note For events that are not I/O related, get_fd_info() returns NULL.
*/
inline sinsp_fdinfo_t* get_fd_info() const
inline sinsp_fdinfo* get_fd_info() const
{
return m_fdinfo;
}
Expand Down Expand Up @@ -507,6 +508,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
m_flags = EF_NONE;
m_info = &(m_event_info_table[m_pevt->type]);
m_fdinfo = NULL;
m_fdinfo_ref.reset();
m_fdinfo_name_changed = false;
m_iosize = 0;
m_poriginal_evt = NULL;
Expand All @@ -519,6 +521,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
m_tinfo_ref.reset();
m_tinfo = NULL;
m_fdinfo = NULL;
m_fdinfo_ref.reset();
m_fdinfo_name_changed = false;
}
inline void init(uint8_t* evdata, uint16_t cpuid)
Expand All @@ -528,6 +531,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
m_info = &(m_event_info_table[m_pevt->type]);
m_tinfo_ref.reset();
m_tinfo = NULL;
m_fdinfo_ref.reset();
m_fdinfo = NULL;
m_fdinfo_name_changed = false;
m_iosize = 0;
Expand All @@ -539,12 +543,13 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
inline void init(scap_evt *scap_event,
ppm_event_info * ppm_event,
sinsp_threadinfo *threadinfo,
sinsp_fdinfo_t *fdinfo)
sinsp_fdinfo *fdinfo)
{
m_pevt = scap_event;
m_info = ppm_event;
m_tinfo_ref.reset(); // we don't own the threadinfo so don't try to manage its lifetime
m_tinfo = threadinfo;
m_tinfo_ref.reset();
m_fdinfo = fdinfo;
m_source_idx = sinsp_no_event_source_idx;
m_source_name = sinsp_no_event_source_name;
Expand Down Expand Up @@ -684,7 +689,7 @@ VISIBILITY_PRIVATE
// it should either be null, or point to the same place as m_tinfo
std::shared_ptr<sinsp_threadinfo> m_tinfo_ref;
sinsp_threadinfo* m_tinfo;
sinsp_fdinfo_t* m_fdinfo;
sinsp_fdinfo* m_fdinfo;

// If true, then the associated fdinfo changed names as a part
// of parsing this event.
Expand All @@ -696,7 +701,7 @@ VISIBILITY_PRIVATE
bool m_filtered_out;
const struct ppm_event_info* m_event_info_table;

std::shared_ptr<sinsp_fdinfo_t> m_fdinfo_ref;
std::shared_ptr<sinsp_fdinfo> m_fdinfo_ref;
// For some exit events, the "path" argument from the
// corresponding enter event is stored here.
std::unordered_map<std::string, std::string> m_enter_path_param;
Expand Down
Loading