diff options
author | Yunchih Chen <yunchih.cat@gmail.com> | 2018-03-18 19:57:44 +0800 |
---|---|---|
committer | Yunchih Chen <yunchih.cat@gmail.com> | 2018-03-18 19:57:44 +0800 |
commit | 7f43f3ee5075b784ce458427c44ae1c78e0019f2 (patch) | |
tree | 87ade8f838709ea53a7e411a4e6e669cec03f60b | |
parent | 20cf5af5efee4685271b083de23b6e66ba48d3f1 (diff) | |
download | nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.tar nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.tar.gz nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.tar.bz2 nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.tar.lz nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.tar.xz nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.tar.zst nfcollect-7f43f3ee5075b784ce458427c44ae1c78e0019f2.zip |
Consistent variable / type naming
-rw-r--r-- | bin/nfcollect.c | 6 | ||||
-rw-r--r-- | bin/nfextract.c | 2 | ||||
-rw-r--r-- | include/collect.h | 6 | ||||
-rw-r--r-- | include/commit.h | 4 | ||||
-rw-r--r-- | include/common.h | 6 | ||||
-rw-r--r-- | include/extract.h | 2 | ||||
-rw-r--r-- | include/main.h | 33 | ||||
-rw-r--r-- | lib/collect.c | 36 | ||||
-rw-r--r-- | lib/commit.c | 22 | ||||
-rw-r--r-- | lib/common.c | 8 | ||||
-rw-r--r-- | lib/extract.c | 30 |
11 files changed, 78 insertions, 77 deletions
diff --git a/bin/nfcollect.c b/bin/nfcollect.c index 85645a4..82a6914 100644 --- a/bin/nfcollect.c +++ b/bin/nfcollect.c @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) { uint32_t i, max_commit_worker = 0, storage_size = 0; uint32_t trunk_cnt = 0, trunk_size = 0; uint32_t entries_max; - nflog_global_t g; + nfl_global_t g; int nfl_group_id = -1; char *compression_flag = NULL, *storage_dir = NULL; @@ -131,8 +131,8 @@ int main(int argc, char *argv[]) { sem_init(g.nfl_commit_queue, 0, max_commit_worker); // Set up nflog receiver worker - nflog_state_t **trunks = - (nflog_state_t **)calloc(trunk_cnt, sizeof(void *)); + nfl_state_t **trunks = + (nfl_state_t **)calloc(trunk_cnt, sizeof(void *)); nfl_commit_init(trunk_cnt); debug("Worker started, entries_max = %d, trunk_cnt = %d", entries_max, diff --git a/bin/nfextract.c b/bin/nfextract.c index b667733..8d8ef2e 100644 --- a/bin/nfextract.c +++ b/bin/nfextract.c @@ -57,7 +57,7 @@ static void sig_handler(int signo) { } static void extract_each(const char *storage_dir, const char *filename) { - nflog_state_t trunk; + nfl_state_t trunk; // Build full path char *fullpath = malloc(strlen(storage_dir) + strlen(filename) + 2); diff --git a/include/collect.h b/include/collect.h index 15e65e8..2f75f4a 100644 --- a/include/collect.h +++ b/include/collect.h @@ -2,6 +2,6 @@ #include "main.h" void *nfl_collect_worker(void *targs); -void nfl_state_init(nflog_state_t **nf, uint32_t id, uint32_t entries_max, - nflog_global_t *g); -void nfl_state_free(nflog_state_t *nf); +void nfl_state_init(nfl_state_t **nf, uint32_t id, uint32_t entries_max, + nfl_global_t *g); +void nfl_state_free(nfl_state_t *nf); diff --git a/include/commit.h b/include/commit.h index 8181f89..e01d020 100644 --- a/include/commit.h +++ b/include/commit.h @@ -3,8 +3,8 @@ #include "common.h" void nfl_commit_init(); -void nfl_commit_worker(nflog_header_t *header, nflog_entry_t *store, - enum nflog_compression_t compression_opt, +void nfl_commit_worker(nfl_header_t *header, nfl_entry_t *store, + enum nfl_compression_t compression_opt, const char *filename); #endif diff --git a/include/common.h b/include/common.h index 9b54143..893bd22 100644 --- a/include/common.h +++ b/include/common.h @@ -6,9 +6,9 @@ int nfl_check_dir(const char *storage_dir); int nfl_storage_match_index(const char *fn); const char *nfl_get_filename(const char *dir, int id); uint32_t nfl_get_filesize(FILE *f); -uint32_t nfl_header_cksum(nflog_header_t *header); +uint32_t nfl_header_cksum(nfl_header_t *header); void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt, uint32_t *trunk_size); void nfl_cal_entries(uint32_t trunk_size, uint32_t *entries_cnt); -void nfl_format_output(char *output, nflog_entry_t *entry); -int nfl_setup_compression(const char *flag, enum nflog_compression_t *opt); +void nfl_format_output(char *output, nfl_entry_t *entry); +int nfl_setup_compression(const char *flag, enum nfl_compression_t *opt); diff --git a/include/extract.h b/include/extract.h index 233f557..bb57511 100644 --- a/include/extract.h +++ b/include/extract.h @@ -1,4 +1,4 @@ #pragma once #include "common.h" -int nfl_extract_worker(const char *filename, nflog_state_t *state); +int nfl_extract_worker(const char *filename, nfl_state_t *state); diff --git a/include/main.h b/include/main.h index 2203e38..c14adef 100644 --- a/include/main.h +++ b/include/main.h @@ -79,25 +79,26 @@ #define CEIL_DIV(a, b) (((a) + (b)-1) / (b)) #define NEXT(i, l) ((i + 1) % l) #define PREV(i, l) ((i - 1) % l) -#define TRUNK_SIZE_BY_PAGE (150) // 150 pages +// #define TRUNK_SIZE_BY_PAGE (150) // 150 pages +#define TRUNK_SIZE_BY_PAGE (2) // 150 pages #define MAX_TRUNK_ID (80) #define STORAGE_PREFIX "nflog_storage" -enum nflog_compression_t { COMPRESS_NONE, COMPRESS_LZ4, COMPRESS_ZSTD }; -typedef struct __attribute__((packed)) _nflog_header_t { +enum nfl_compression_t { COMPRESS_NONE, COMPRESS_LZ4, COMPRESS_ZSTD }; +typedef struct __attribute__((packed)) _nfl_header_t { uint32_t id; /* 0 4 */ uint32_t n_entries; /* 4 4 */ uint32_t max_n_entries; /* 8 4 */ uint32_t cksum; /* 12 4 */ - enum nflog_compression_t compression_opt; /* 16 4 */ + enum nfl_compression_t compression_opt; /* 16 4 */ time_t start_time; /* 20 8 */ time_t end_time; /* 28 8 */ /* size: 36, cachelines: 1, members: 7 */ /* last cacheline: 36 bytes */ -} nflog_header_t; +} nfl_header_t; -typedef struct __attribute__((packed)) _nflog_entry_t { +typedef struct __attribute__((packed)) _nfl_entry_t { // current timestamp since UNIX epoch time_t timestamp; /* 0 8 */ @@ -123,19 +124,19 @@ typedef struct __attribute__((packed)) _nflog_entry_t { uint16_t dport; /* 22 2 */ /* size: 24, cachelines: 1, members: 8 */ -} nflog_entry_t; +} nfl_entry_t; -typedef struct _nflog_global_t { +typedef struct _nfl_global_t { sem_t *nfl_commit_queue; uint16_t nfl_group_id; const char *storage_dir; - enum nflog_compression_t compression_opt; -} nflog_global_t; + enum nfl_compression_t compression_opt; +} nfl_global_t; -typedef struct _nflog_state_t { - nflog_global_t *global; - nflog_header_t *header; - nflog_entry_t *store; +typedef struct _nfl_state_t { + nfl_global_t *global; + nfl_header_t *header; + nfl_entry_t *store; struct nflog_handle *nfl_fd; struct nflog_g_handle *nfl_group_fd; @@ -144,9 +145,9 @@ typedef struct _nflog_state_t { pthread_cond_t has_finished_cond; pthread_mutex_t has_finished_lock; pthread_t thread; -} nflog_state_t; +} nfl_state_t; // only copy size of ipv4 header + tcp header -static const int nflog_recv_size = sizeof(struct iphdr) + sizeof(struct tcphdr); +static const int nfl_recv_size = sizeof(struct iphdr) + sizeof(struct tcphdr); #endif // _MAIN_H diff --git a/lib/collect.c b/lib/collect.c index a8a46bd..0fdbe52 100644 --- a/lib/collect.c +++ b/lib/collect.c @@ -31,17 +31,17 @@ #include <sys/types.h> // u_int32_t for libnetfilter_log #include <time.h> -nflog_global_t g; +nfl_global_t g; -static void nfl_init(nflog_state_t *nf); +static void nfl_init(nfl_state_t *nf); static void *nfl_start_commit_worker(void *targs); -static void nfl_commit(nflog_state_t *nf); -static void nfl_state_free(nflog_state_t *nf); +static void nfl_commit(nfl_state_t *nf); +static void nfl_state_free(nfl_state_t *nf); static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, struct nflog_data *nfa, void *_nf) { register const struct iphdr *iph; - register nflog_entry_t *entry; + register nfl_entry_t *entry; const struct tcphdr *tcph; const struct udphdr *udph; char *payload; @@ -49,7 +49,7 @@ static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, uint32_t uid; int payload_len = nflog_get_payload(nfa, &payload); - nflog_state_t *nf = (nflog_state_t *)_nf; + nfl_state_t *nf = (nfl_state_t *)_nf; pthread_testcancel(); /* cancellation point */ @@ -107,7 +107,7 @@ static int handle_packet(struct nflog_g_handle *gh, struct nfgenmsg *nfmsg, return 0; } -static void nfl_init(nflog_state_t *nf) { +static void nfl_init(nfl_state_t *nf) { // open nflog ERR((nf->nfl_fd = nflog_open()) == NULL, "nflog_open") debug("Opening nflog communication file descriptor"); @@ -120,18 +120,18 @@ static void nfl_init(nflog_state_t *nf) { /* ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, sizeof(struct * iphdr) + 4) < 0, */ - ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, nflog_recv_size) < + ERR(nflog_set_mode(nf->nfl_group_fd, NFULNL_COPY_PACKET, nfl_recv_size) < 0, "Could not set copy mode"); nflog_callback_register(nf->nfl_group_fd, &handle_packet, nf); debug("Registering nflog callback"); - memcpy(&g, nf->global, sizeof(nflog_global_t)); + memcpy(&g, nf->global, sizeof(nfl_global_t)); } void *nfl_collect_worker(void *targs) { - nflog_state_t *nf = (nflog_state_t *)targs; + nfl_state_t *nf = (nfl_state_t *)targs; nfl_init(nf); int fd = nflog_fd(nf->nfl_fd); @@ -172,14 +172,14 @@ void *nfl_collect_worker(void *targs) { * Committer */ -static void nfl_commit(nflog_state_t *nf) { +static void nfl_commit(nfl_state_t *nf) { pthread_t tid; pthread_create(&tid, NULL, nfl_start_commit_worker, (void *)nf); pthread_detach(tid); } static void *nfl_start_commit_worker(void *targs) { - nflog_state_t *nf = (nflog_state_t *)targs; + nfl_state_t *nf = (nfl_state_t *)targs; const char *filename = nfl_get_filename(g.storage_dir, nf->header->id); debug("Comm worker #%u: thread started.", nf->header->id); @@ -204,13 +204,13 @@ static void *nfl_start_commit_worker(void *targs) { * State managers */ -void nfl_state_init(nflog_state_t **nf, uint32_t id, uint32_t entries_max, - nflog_global_t *g) { +void nfl_state_init(nfl_state_t **nf, uint32_t id, uint32_t entries_max, + nfl_global_t *g) { assert(nf); if (unlikely(*nf == NULL)) { - *nf = (nflog_state_t *)malloc(sizeof(nflog_state_t)); + *nf = (nfl_state_t *)malloc(sizeof(nfl_state_t)); (*nf)->global = g; - (*nf)->header = (nflog_header_t *)malloc(sizeof(nflog_header_t)); + (*nf)->header = (nfl_header_t *)malloc(sizeof(nfl_header_t)); (*nf)->header->id = id; (*nf)->header->n_entries = 0; (*nf)->header->max_n_entries = entries_max; @@ -234,10 +234,10 @@ void nfl_state_init(nflog_state_t **nf, uint32_t id, uint32_t entries_max, // consume physical memory before we fill the buffer. // Instead, fill entries with 0 on the fly, to squeeze // more space for compression. - (*nf)->store = (nflog_entry_t *)malloc(sizeof(nflog_entry_t) * entries_max); + (*nf)->store = (nfl_entry_t *)malloc(sizeof(nfl_entry_t) * entries_max); } -static void nfl_state_free(nflog_state_t *nf) { +static void nfl_state_free(nfl_state_t *nf) { // Free only packet store and leave the rest intact free((void *)nf->store); } diff --git a/lib/commit.c b/lib/commit.c index 13fd747..ca90598 100644 --- a/lib/commit.c +++ b/lib/commit.c @@ -3,25 +3,25 @@ #include <string.h> #include <zstd.h> -static void nfl_commit_default(FILE *f, nflog_entry_t *store, +static void nfl_commit_default(FILE *f, nfl_entry_t *store, uint32_t store_size); -static void nfl_commit_lz4(FILE *f, nflog_entry_t *store, uint32_t store_size); -static void nfl_commit_zstd(FILE *f, nflog_entry_t *store, uint32_t store_size); +static void nfl_commit_lz4(FILE *f, nfl_entry_t *store, uint32_t store_size); +static void nfl_commit_zstd(FILE *f, nfl_entry_t *store, uint32_t store_size); void nfl_commit_init() { /* TODO */ } -static void nfl_commit_default(FILE *f, nflog_entry_t *store, +static void nfl_commit_default(FILE *f, nfl_entry_t *store, uint32_t store_size) { uint32_t written; written = fwrite(store, 1, store_size, f); ERR(written != store_size, strerror(errno)); } -static void nfl_commit_lz4(FILE *f, nflog_entry_t *store, uint32_t store_size) { +static void nfl_commit_lz4(FILE *f, nfl_entry_t *store, uint32_t store_size) { /* TODO */ } -static void nfl_commit_zstd(FILE *f, nflog_entry_t *store, +static void nfl_commit_zstd(FILE *f, nfl_entry_t *store, uint32_t store_size) { size_t const bufsize = ZSTD_compressBound(store_size); void *buf; @@ -35,8 +35,8 @@ static void nfl_commit_zstd(FILE *f, nflog_entry_t *store, free(buf); } -void nfl_commit_worker(nflog_header_t *header, nflog_entry_t *store, - enum nflog_compression_t compression_opt, +void nfl_commit_worker(nfl_header_t *header, nfl_entry_t *store, + enum nfl_compression_t compression_opt, const char *filename) { FILE *f; uint32_t written; @@ -45,11 +45,11 @@ void nfl_commit_worker(nflog_header_t *header, nflog_entry_t *store, ERR((f = fopen(filename, "wb")) == NULL, strerror(errno)); // commit header - written = fwrite(header, 1, sizeof(nflog_header_t), f); - ERR(written != sizeof(nflog_header_t), strerror(errno)); + written = fwrite(header, 1, sizeof(nfl_header_t), f); + ERR(written != sizeof(nfl_header_t), strerror(errno)); // commit store - uint32_t store_size = sizeof(nflog_entry_t) * header->max_n_entries; + uint32_t store_size = sizeof(nfl_entry_t) * header->max_n_entries; switch(compression_opt) { case COMPRESS_NONE: debug("Comm worker #%u: commit without compression\n", header->id) diff --git a/lib/common.c b/lib/common.c index 344f531..4380f27 100644 --- a/lib/common.c +++ b/lib/common.c @@ -71,7 +71,7 @@ uint32_t nfl_get_filesize(FILE *f) { return size; } -uint32_t nfl_header_cksum(nflog_header_t *header) { +uint32_t nfl_header_cksum(nfl_header_t *header) { #define H(s) (0x9e3779b9 + (s<< 6) + (s>> 1)) register uint64_t s = 3784672181; s ^= H(header->id); @@ -103,10 +103,10 @@ void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt, void nfl_cal_entries(uint32_t trunk_size, uint32_t *entries_cnt) { assert(entries_cnt); *entries_cnt = - (trunk_size - sizeof(nflog_header_t)) / sizeof(nflog_entry_t); + (trunk_size - sizeof(nfl_header_t)) / sizeof(nfl_entry_t); } -void nfl_format_output(char *output, nflog_entry_t *entry) { +void nfl_format_output(char *output, nfl_entry_t *entry) { sprintf(output, " " "t=%ld\t" "daddr=%s\t" @@ -119,7 +119,7 @@ void nfl_format_output(char *output, nflog_entry_t *entry) { entry->sport, entry->dport); } -int nfl_setup_compression(const char *flag, enum nflog_compression_t *opt) { +int nfl_setup_compression(const char *flag, enum nfl_compression_t *opt) { if (flag == NULL) { *opt = COMPRESS_NONE; } else if (!strcmp(flag, "zstd") || !strcmp(flag, "zstandard")) { diff --git a/lib/extract.c b/lib/extract.c index be8dbc4..61bf340 100644 --- a/lib/extract.c +++ b/lib/extract.c @@ -6,11 +6,11 @@ #define ZSTD_STATIC_LINKING_ONLY // ZSTD_findDecompressedSize #include <zstd.h> -static int nfl_extract_default(FILE *f, nflog_state_t *state); -static int nfl_extract_zstd(FILE *f, nflog_state_t *state); -static int nfl_extract_lz4(FILE *f, nflog_state_t *state); +static int nfl_extract_default(FILE *f, nfl_state_t *state); +static int nfl_extract_zstd(FILE *f, nfl_state_t *state); +static int nfl_extract_lz4(FILE *f, nfl_state_t *state); -static int nfl_verify_header(nflog_header_t *header) { +static int nfl_verify_header(nfl_header_t *header) { if(header->cksum != nfl_header_cksum(header)) return -1; @@ -28,16 +28,16 @@ static int nfl_verify_header(nflog_header_t *header) { return 0; } -static int nfl_extract_default(FILE *f, nflog_state_t *state) { - fread(state->store, state->header->n_entries, sizeof(nflog_entry_t), f); +static int nfl_extract_default(FILE *f, nfl_state_t *state) { + fread(state->store, state->header->n_entries, sizeof(nfl_entry_t), f); WARN_RETURN(ferror(f), "%s", strerror(errno)); return 0; } -static int nfl_extract_zstd(FILE *f, nflog_state_t *state) { +static int nfl_extract_zstd(FILE *f, nfl_state_t *state) { char *buf; - size_t const compressed_size = nfl_get_filesize(f) - sizeof(nflog_header_t), - expected_decom_size = state->header->n_entries * sizeof(nflog_entry_t); + size_t const compressed_size = nfl_get_filesize(f) - sizeof(nfl_header_t), + expected_decom_size = state->header->n_entries * sizeof(nfl_entry_t); ERR(!(buf = malloc(compressed_size)), "zstd: cannot malloc"); fread(buf, compressed_size, 1, f); @@ -60,23 +60,23 @@ static int nfl_extract_zstd(FILE *f, nflog_state_t *state) { return 0; } -static int nfl_extract_lz4(FILE *f, nflog_state_t *state) { +static int nfl_extract_lz4(FILE *f, nfl_state_t *state) { /* TODO */ return 0; } -int nfl_extract_worker(const char *filename, nflog_state_t *state) { +int nfl_extract_worker(const char *filename, nfl_state_t *state) { FILE *f; int got = 0, ret = 0; - nflog_header_t *h; + nfl_header_t *h; debug("Extracting from file %s", filename); ERR((f = fopen(filename, "rb")) == NULL, "extract worker"); /* ERR(nfl_check_file(f) < 0, "extract worker"); */ // Read header - ERR(!(state->header = malloc(sizeof(nflog_header_t))), "extract malloc header"); - got = fread(state->header, sizeof(nflog_header_t), 1, f); + ERR(!(state->header = malloc(sizeof(nfl_header_t))), "extract malloc header"); + got = fread(state->header, sizeof(nfl_header_t), 1, f); h = state->header; // Check header validity @@ -84,7 +84,7 @@ int nfl_extract_worker(const char *filename, nflog_state_t *state) { WARN_RETURN(!got || nfl_verify_header(h) < 0, "File %s has corrupted header.", filename); // Read body - ERR(!(state->store = malloc(sizeof(nflog_entry_t) * h->n_entries)), "extract malloc store"); + ERR(!(state->store = malloc(sizeof(nfl_entry_t) * h->n_entries)), "extract malloc store"); switch(h->compression_opt) { case COMPRESS_NONE: debug("Extract worker #%u: extract without compression\n", h->id) |