Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #15 from ImoStocky/devel-ffilter
Browse files Browse the repository at this point in the history
Devel ffilter
  • Loading branch information
tpoder1 committed Jul 30, 2017
2 parents 0ef7113 + 7100996 commit 0511152
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 35 deletions.
9 changes: 6 additions & 3 deletions examples/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

AM_CFLAGS = -I$(top_srcdir)/include

bin_PROGRAMS = lnf_ex01_writer lnf_ex02_reader lnf_ex03_aggreg lnf_ex05_memtrans lnf_ex06_readreset \
bin_PROGRAMS = lnf_ex01_writer lnf_ex02_reader lnf_ex03_aggreg lnf_ex03_coreperf lnf_ex05_memtrans lnf_ex06_readreset \
lnf_ex07_filter lnf_ex08_statistics lnf_ex09_memlookup lnf_ex10_listmode lnf_ex11_memclean \
lnf_ex12_rec_raw lnf_ex13_ring_write lnf_ex14_ring_read

Expand All @@ -17,6 +17,9 @@ lnf_ex02_reader_LDADD = ../src/.libs/libnf.a
lnf_ex03_aggreg_SOURCES = lnf_ex03_aggreg.c
lnf_ex03_aggreg_LDADD = ../src/.libs/libnf.a

lnf_ex03_coreperf_SOURCES = lnf_ex03_coreperf.c
lnf_ex03_coreperf_LDADD = ../src/.libs/libnf.a

if LNF_THREADS
bin_PROGRAMS += lnf_ex04_threads
lnf_ex04_threads_SOURCES = lnf_ex04_threads.c
Expand Down Expand Up @@ -48,8 +51,8 @@ lnf_ex12_rec_raw_SOURCES = lnf_ex12_rec_raw.c
lnf_ex12_rec_raw_LDADD = ../src/.libs/libnf.a

lnf_ex13_ring_write_SOURCES = lnf_ex13_ring_write.c
lnf_ex13_ring_write_LDADD = ../src/.libs/libnf.a
lnf_ex13_ring_write_LDADD = ../src/.libs/libnf.a -lrt

lnf_ex14_ring_read_SOURCES = lnf_ex14_ring_read.c
lnf_ex14_ring_read_LDADD = ../src/.libs/libnf.a
lnf_ex14_ring_read_LDADD = ../src/.libs/libnf.a -lrt

1 change: 1 addition & 0 deletions examples/lnf_ex02_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ int main(int argc, char **argv) {

match1 = 0;
match2 = 0;

if (filter) {
if (lnf_filter_match(filterp1, recp)) {
if1++;
Expand Down
187 changes: 187 additions & 0 deletions examples/lnf_ex03_coreperf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//
// Created by istoffa on 5/16/17.
//
/*
Copyright (c) 2013-2015, Tomas Podermanski
This file is part of libnf.net project.
Libnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Libnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libnf. If not, see <http://www.gnu.org/licenses/>.
*/

/* Simple reader of nfdump files. To demostrate functionality */
/* records are matched against two filters */

#include <libnf.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>


#define FILENAME "./test-file.tmp"
#define FILTER1 "src port > 80"

#define LLUI long long unsigned int

int main(int argc, char **argv)
{

lnf_file_t *filep;
lnf_rec_t *recp;
lnf_filter_t *filterp1;
lnf_brec1_t brec;
char *filter1 = FILTER1;
uint32_t input, output;
char buf[LNF_MAX_STRING];
int res;
char use_v2 = 1;

int i = 0;
int match1 = 0;

int print = 1;
int filter = 1;
int fget = 1;
char *filename = FILENAME;
char *fifname = NULL;
char program[200];
int c;

FILE *filters = NULL;

while ((c = getopt(argc, argv, "pnPF:Gf:1:")) != -1) {
switch (c) {
case 'p':
print = 0;
break;
case 'n':
use_v2 = 0;
break;
case 'P':
print = 0;
break;
case 'F':
fifname = optarg;
break;
case 'f':
filename = optarg;
break;
case '1':
filter1 = optarg;
break;
case '?':
printf("Usage: %s [ -p ] [ -f <output file name> ] [ -1 <filter1> ]\n", argv[0]);
printf(" -P : do not print records to stdout\n");
printf(" -F : supply file with filters to test, each line is in form '%%d %%s' - group number and filter\n");
printf(" -n : use stock nfdump core\n");
exit(1);
}
}



if ((filters = fopen(fifname, "r")) == NULL) {
fprintf(stderr, "Can not open file with test filters %s\n", fifname);
exit(1);
}

printf("Set, Nodes, duration sec, performance records/sec, matches\n");

int set = 0;
int nodes;

while (!feof(filters)) {

if (lnf_open(&filep, filename, LNF_READ, NULL) != LNF_OK) {
fprintf(stderr, "Can not open file %s\n", filename);
exit(1);
}

set++;

if (fscanf(filters, "%d", &nodes) < 1) {
break;
}
fgets(&program[0], sizeof(program), filters);

if ((!use_v2 || ((res = lnf_filter_init_v2(&filterp1, &program[0])) != LNF_OK)))
if(use_v2 || (res = lnf_filter_init(&filterp1, &program[0])) != LNF_OK)
{
fprintf(stderr, "Can not init filter '%s'\n", &program[0]);
if (res == LNF_ERR_OTHER_MSG) {
lnf_error(buf, LNF_MAX_STRING);
fprintf(stderr, "%s\n", buf);
}
continue;
}

lnf_rec_init(&recp);

clock_t t;
clock_t total = 0;

int i = 0;
long int count = 0;
int match;

while (lnf_read(filep, recp) != LNF_EOF) {

if (fget) {
lnf_rec_fget(recp, LNF_FLD_INPUT, &input);

lnf_rec_fget(recp, LNF_FLD_BREC1, &brec);
lnf_rec_fget(recp, LNF_FLD_INPUT, &input);
lnf_rec_fget(recp, LNF_FLD_OUTPUT, &output);
}
count++;

t = clock();
match = ((lnf_filter_match(filterp1, recp)) == 1);
total += clock() - t;

i += match;
if (print && match) {
char sbuf[INET6_ADDRSTRLEN];
char dbuf[INET6_ADDRSTRLEN];

inet_ntop(AF_INET6, &brec.srcaddr, sbuf, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &brec.dstaddr, dbuf, INET6_ADDRSTRLEN);

printf("%s :%d -> %s :%d %d -> %d %llu %llu %llu\n",
sbuf, brec.srcport,
dbuf, brec.dstport,
input, output,
(LLUI) brec.pkts, (LLUI) brec.bytes, (LLUI) brec.flows);
}
}
printf("%d,\t%d,\t%lf,\t%.0lf,\t%d\n",
set, nodes, ((double) total) / CLOCKS_PER_SEC,
count*((CLOCKS_PER_SEC)/((double)total)),
i);

lnf_rec_free(recp);
lnf_filter_free(filterp1);
lnf_close(filep);
}

if(filters)
fclose(filters);

return 0;
}

4 changes: 2 additions & 2 deletions examples/lnf_ex07_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ int main(int argc, char **argv) {
if (res == LNF_ERR_OTHER_MSG) {
lnf_error(buf, LNF_MAX_STRING);
fprintf(stderr, "RES: %s\n", buf);
filterp1 = NULL;
}
filterp1 = NULL;
}

if ((res = lnf_filter_init_v2(&filterp2, filter)) != LNF_OK) {
Expand All @@ -90,8 +90,8 @@ int main(int argc, char **argv) {
if (res == LNF_ERR_OTHER_MSG) {
lnf_error(buf, LNF_MAX_STRING);
fprintf(stderr, "RES: %s\n", buf);
filterp2 = NULL;
}
filterp2 = NULL;
}

lnf_rec_init(&recp);
Expand Down
4 changes: 4 additions & 0 deletions include/libnf.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ typedef struct lnf_brec1_s {
#define LNF_FLD_SAMPLER_MODE 0x49
#define LNF_FLD_SAMPLER_INTERVAL 0x4a
#define LNF_FLD_SAMPLER_ID 0x4b
/* Added stub for compatibility, bears same identification as ADDR variants */
#define LNF_FLD_SRCNET 0x4c
#define LNF_FLD_DSTNET 0x4d

/* computed and extra fields */
#define LNF_FLD_CALC_DURATION 0xA0 /* computed : duration in msec */
Expand All @@ -198,6 +201,7 @@ typedef struct lnf_brec1_s {
#define LNF_FLD_PAIR_AS 0xC2 /* src as + dst as */
#define LNF_FLD_PAIR_IF 0xC3 /* in if + out if */
#define LNF_FLD_PAIR_VLAN 0xC4 /* src vlan + dst vlan */
#define LNF_FLD_PAIR_NET 0xC5 /* src ip + dst ip compatibility*/


#define LNF_FLD_TERM_ 0xFF /* ID of last field */
Expand Down
9 changes: 5 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ endif

#SUBDIRS = ffilter

EXTRA_DIST = libnf_internal.h bit_array.h hash_table.h heap_sort.h list_sort.h xxhash.h fields.h lnf_filter.h
EXTRA_DIST = libnf_internal.h bit_array.h hash_table.h heap_sort.h list_sort.h xxhash.h fields.h \
lnf_filter.h literals.h


CLEANFILES = lex.yy.c
Expand All @@ -28,17 +29,17 @@ CLEANFILES = lex.yy.c
# Target for the libnfdump library
lib_LTLIBRARIES = libnf.la
libnf_la_SOURCES = libnf.c bit_array.c heap_sort.c list_sort.c hash_table.c xxhash.c memheap.c fields.c \
nffile.c nfx.c minilzo.c nf_common.c ipconv.c lnf_filter.c
nffile.c nfx.c minilzo.c nf_common.c ipconv.c lnf_filter.c literals.c

# Nfdump filter sources
BUILT_SOURCES = grammar.h
EXTRA_DIST += scanner.c grammar.c
libnf_la_SOURCES += nftree.c grammar.y scanner.l

# Libnf flow filter sources
EXTRA_DIST += ffilter/ffilter_gram.c ffilter/ffilter_lex.c ffilter/ffilter_gram.h ffilter/ffilter_internal.h ffilter/ffilter.h
EXTRA_DIST += ffilter/ffilter_gram.c ffilter/ffilter_lex.c ffilter/ffilter_gram.h ffilter/ffilter_internal.h ffilter/ffilter.h ffilter/fcore.h
BUILT_SOURCES += ffilter/ffilter_gram.h
libnf_la_SOURCES += ffilter/ffilter_gram.y ffilter/ffilter_lex.l ffilter/ffilter.c
libnf_la_SOURCES += ffilter/ffilter_gram.y ffilter/ffilter_lex.l ffilter/ffilter.c ffilter/fcore.c
include_HEADERS = ffilter/ffilter.h

# rinbuffer code
Expand Down
Loading

0 comments on commit 0511152

Please sign in to comment.