Skip to content

Commit 6ad3bd3

Browse files
ldegiothom-sd
authored andcommitted
big snaplen port range (#1256)
* added the ability to specify a set of ports where data is captured with bigger snaplen (20000) * fixes based on gianluca's review * new chisel: udp_extract * Fix snprintf placeholder for size_t/{u,}int64_t (#1279) We're using the wrong placeholders in some calls to printf-like functions. This problem was identified when we started building with -Wextra. This change use %zu for size_t, PRIu64 for uint64_t and PRId64 for int64_t.
1 parent 800319e commit 6ad3bd3

File tree

13 files changed

+223
-1
lines changed

13 files changed

+223
-1
lines changed

driver/bpf/filler_helpers.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ static __always_inline u32 bpf_compute_snaplen(struct filler_data *data,
357357
return 2000;
358358
} else if (dport == PPM_PORT_STATSD) {
359359
return 2000;
360+
} else if (data->settings->fullcapture_port_range_end != 0 &&
361+
((sport >= data->settings->fullcapture_port_range_start && sport <= data->settings->fullcapture_port_range_end) ||
362+
(dport >= data->settings->fullcapture_port_range_start && dport <= data->settings->fullcapture_port_range_end)
363+
)) {
364+
/*
365+
* mpegts detection
366+
*/
367+
return RW_MAX_FULLCAPTURE_PORT_SNAPLEN;
360368
} else {
361369
if (lookahead_size >= 5) {
362370
u32 buf = *(u32 *)&get_buf(0);

driver/bpf/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ struct sysdig_bpf_settings {
206206
bool dropping_mode;
207207
bool is_dropping;
208208
bool tracers_enabled;
209+
uint16_t fullcapture_port_range_start;
210+
uint16_t fullcapture_port_range_end;
209211
} __attribute__((packed));
210212

211213
struct tail_context {

driver/main.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ static int ppm_open(struct inode *inode, struct file *filp)
428428
consumer->do_dynamic_snaplen = false;
429429
consumer->need_to_insert_drop_e = 0;
430430
consumer->need_to_insert_drop_x = 0;
431+
consumer->fullcapture_port_range_start = 0;
432+
consumer->fullcapture_port_range_end = 0;
431433
bitmap_fill(g_events_mask, PPM_EVENT_MAX); /* Enable all syscall to be passed to userspace */
432434
reset_ring_buffer(ring);
433435
ring->open = true;
@@ -883,6 +885,22 @@ static long ppm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
883885
ret = 0;
884886
goto cleanup_ioctl;
885887
}
888+
case PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE:
889+
{
890+
u32 encoded_port_range;
891+
892+
vpr_info("PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE, consumer %p\n", consumer_id);
893+
encoded_port_range = (u32)arg;
894+
895+
consumer->fullcapture_port_range_start = encoded_port_range & 0xFFFF;
896+
consumer->fullcapture_port_range_end = encoded_port_range >> 16;
897+
898+
pr_info("new fullcapture_port_range_start: %d\n", (int)consumer->fullcapture_port_range_start);
899+
pr_info("new fullcapture_port_range_end: %d\n", (int)consumer->fullcapture_port_range_end);
900+
901+
ret = 0;
902+
goto cleanup_ioctl;
903+
}
886904
case PPM_IOCTL_MASK_ZERO_EVENTS:
887905
{
888906
vpr_info("PPM_IOCTL_MASK_ZERO_EVENTS, consumer %p\n", consumer_id);

driver/ppm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ struct ppm_consumer_t {
7272
volatile int need_to_insert_drop_e;
7373
volatile int need_to_insert_drop_x;
7474
struct list_head node;
75+
uint16_t fullcapture_port_range_start;
76+
uint16_t fullcapture_port_range_end;
7577
};
7678

7779
#define STR_STORAGE_SIZE PAGE_SIZE

driver/ppm_events.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ inline u32 compute_snaplen(struct event_filler_arguments *args, char *buf, u32 l
382382
} else if (dport == PPM_PORT_STATSD) {
383383
sockfd_put(sock);
384384
return 2000;
385+
} else if (args->consumer->fullcapture_port_range_end != 0 &&
386+
((sport >= args->consumer->fullcapture_port_range_start && sport <= args->consumer->fullcapture_port_range_end) ||
387+
(dport >= args->consumer->fullcapture_port_range_start && dport <= args->consumer->fullcapture_port_range_end)
388+
)) {
389+
/*
390+
* mpegts detection
391+
*/
392+
sockfd_put(sock);
393+
return RW_MAX_FULLCAPTURE_PORT_SNAPLEN;
385394
} else {
386395
if (lookahead_size >= 5) {
387396
if (*(u32 *)buf == g_http_get_intval ||

driver/ppm_events_public.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ struct ppm_evt_hdr {
14531453
#define PPM_IOCTL_ENABLE_PAGE_FAULTS _IO(PPM_IOCTL_MAGIC, 19)
14541454
#define PPM_IOCTL_GET_N_TRACEPOINT_HIT _IO(PPM_IOCTL_MAGIC, 20)
14551455
#define PPM_IOCTL_GET_PROBE_VERSION _IO(PPM_IOCTL_MAGIC, 21)
1456+
#define PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE _IO(PPM_IOCTL_MAGIC, 22)
14561457
#endif // CYGWING_AGENT
14571458

14581459
extern const struct ppm_name_value socket_families[];
@@ -1590,5 +1591,6 @@ struct ppm_event_entry {
15901591

15911592
#define RW_SNAPLEN 80
15921593
#define RW_MAX_SNAPLEN PPM_MAX_ARG_SIZE
1594+
#define RW_MAX_FULLCAPTURE_PORT_SNAPLEN 16000
15931595

15941596
#endif /* EVENTS_PUBLIC_H_ */

userspace/libscap/scap.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,3 +2000,62 @@ bool scap_check_suppressed_tid(scap_t *handle, int64_t tid)
20002000

20012001
return (stid != NULL);
20022002
}
2003+
2004+
int32_t scap_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end)
2005+
{
2006+
//
2007+
// Not supported on files
2008+
//
2009+
if(handle->m_mode != SCAP_MODE_LIVE)
2010+
{
2011+
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_set_fullcapture_port_range not supported on this scap mode");
2012+
return SCAP_FAILURE;
2013+
}
2014+
2015+
#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT)
2016+
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME);
2017+
return SCAP_FAILURE;
2018+
#else
2019+
2020+
if(handle->m_bpf)
2021+
{
2022+
return scap_bpf_set_fullcapture_port_range(handle, range_start, range_end);
2023+
}
2024+
else
2025+
{
2026+
//
2027+
// Encode the port range
2028+
//
2029+
uint32_t arg = (range_end << 16) + range_start;
2030+
2031+
//
2032+
// Beam the value down to the module
2033+
//
2034+
if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE, arg))
2035+
{
2036+
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_set_fullcapture_port_range failed");
2037+
ASSERT(false);
2038+
return SCAP_FAILURE;
2039+
}
2040+
2041+
{
2042+
uint32_t j;
2043+
2044+
//
2045+
// Force a flush of the read buffers, so we don't capture events with the old snaplen
2046+
//
2047+
for(j = 0; j < handle->m_ndevs; j++)
2048+
{
2049+
scap_readbuf(handle,
2050+
j,
2051+
&handle->m_devs[j].m_sn_next_event,
2052+
&handle->m_devs[j].m_sn_len);
2053+
2054+
handle->m_devs[j].m_sn_len = 0;
2055+
}
2056+
}
2057+
}
2058+
2059+
return SCAP_SUCCESS;
2060+
#endif
2061+
}

userspace/libscap/scap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ int32_t scap_get_n_tracepoint_hit(scap_t* handle, long* ret);
10071007
typedef struct wh_t wh_t;
10081008
wh_t* scap_get_wmi_handle(scap_t* handle);
10091009
#endif
1010+
int32_t scap_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end);
10101011

10111012
#ifdef __cplusplus
10121013
}

userspace/libscap/scap_bpf.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,28 @@ int32_t scap_bpf_set_snaplen(scap_t* handle, uint32_t snaplen)
881881
return SCAP_SUCCESS;
882882
}
883883

884+
int32_t scap_bpf_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end)
885+
{
886+
struct sysdig_bpf_settings settings;
887+
int k = 0;
888+
889+
if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0)
890+
{
891+
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0");
892+
return SCAP_FAILURE;
893+
}
894+
895+
settings.fullcapture_port_range_start = range_start;
896+
settings.fullcapture_port_range_end = range_end;
897+
if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0)
898+
{
899+
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0");
900+
return SCAP_FAILURE;
901+
}
902+
903+
return SCAP_SUCCESS;
904+
}
905+
884906
int32_t scap_bpf_disable_dynamic_snaplen(scap_t* handle)
885907
{
886908
struct sysdig_bpf_settings settings;
@@ -1199,6 +1221,8 @@ static int32_t set_default_settings(scap_t *handle)
11991221
settings.page_faults = false;
12001222
settings.dropping_mode = false;
12011223
settings.is_dropping = false;
1224+
settings.fullcapture_port_range_start = 0;
1225+
settings.fullcapture_port_range_end = 0;
12021226

12031227
int k = 0;
12041228
if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0)

userspace/libscap/scap_bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int32_t scap_bpf_start_capture(scap_t *handle);
3838
int32_t scap_bpf_stop_capture(scap_t *handle);
3939
int32_t scap_bpf_close(scap_t *handle);
4040
int32_t scap_bpf_set_snaplen(scap_t* handle, uint32_t snaplen);
41+
int32_t scap_bpf_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end);
4142
int32_t scap_bpf_enable_dynamic_snaplen(scap_t* handle);
4243
int32_t scap_bpf_disable_dynamic_snaplen(scap_t* handle);
4344
int32_t scap_bpf_enable_page_faults(scap_t* handle);

0 commit comments

Comments
 (0)