Commit 05bbf2fb authored by 's avatar
Browse files

test

git-svn-id: file:///home/svn/mapi/trunk@914 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent e5b1174f
......@@ -13,20 +13,18 @@
#include "mapi.h"
#define SLEEP_TIME 1 //sleeping time
#define SLEEP_TIME 1 //sleeping time
#define TIMEOUT 500 //time to drop an unmatched flow
#define HASHTABLE_SIZE 4096
#define MAX_INTERFACES 16
#define RRD_FILENAME "packetloss.rrd"
static void terminate();
int fd, fid1;
mapi_results_t *results;
unsigned long long totalpkloss = 0,pkcount0 = 0, pkcount1 = 0;
unsigned long long totalpkloss = 0, pkcount0 = 0, pkcount1 = 0;
int with_ncurses = 1;
struct hash_bucket {
struct hash_bucket *next;
struct flow_data data;
......@@ -44,8 +42,8 @@ void my_rrd_update(char *, char *);
void my_rrd_graph(char *filename, char *data); /* not used */
int create_cgi(int argc, char **argv);
int main(int argc, char *argv[]){
unsigned int *i, count, j;
unsigned long long pkloss = 0;
char argvs[500];
......@@ -65,12 +63,14 @@ int main(int argc, char *argv[]){
tmp1 = strchr(argv[1], ':');
tmp2 = strchr(argv[2], ':');
if( (tmp1 == NULL) || (tmp2 == NULL) ) {
fprintf(stderr, "Error!\n You must provide an interface for each IP!\n");
exit(1);
}
sprintf(argvs, "%s, %s", argv[1], argv[2]);
fd=mapi_create_flow(argvs);
fd = mapi_create_flow(argvs);
if(fd<=0){
printf("CREATE_FLOW ERROR\n");
exit(0);
......@@ -78,46 +78,50 @@ int main(int argc, char *argv[]){
*tmp1 = *tmp2 = '\0';
sprintf(argvs, "host %s and host %s", argv[1], argv[2]);
*tmp1 = *tmp2 = ':';
fid1=mapi_apply_function(fd,"BPF_FILTER", argvs);
fid1 = mapi_apply_function(fd, "BPF_FILTER", argvs);
if(fid1<0){
printf("APPLY BPF FILTER ERROR\n");
exit(0);
}
exit(0);
}
fid1=mapi_apply_function(fd,"EXPIRED_FLOWS");
fid1 = mapi_apply_function(fd, "EXPIRED_FLOWS");
if(fid1<0){
printf("APPLY EXPIRED_FLOWS FILTER ERROR\n");
exit(0);
}
if(mapi_connect(fd) <0){
printf("MAPI_CONNECT ERROR\n");
exit(0);
}
if(with_ncurses) {
initscr(); // start curses mode
getmaxyx(stdscr,row,col); // get the number of rows and columns
if(with_ncurses){
initscr(); // start curses mode
getmaxyx(stdscr, row, col); // get the number of rows and columns
}
my_rrd_create(RRD_FILENAME, 2);
create_cgi(argc, argv);
/* Infinite Loop */
while(1)
{
while(1){
sleep(SLEEP_TIME);
p = sample;
results = (mapi_results_t*)mapi_read_results(fd,fid1);
results = (mapi_results_t*)mapi_read_results(fd, fid1);
for(j = 0; j < 2; j++) {
i = (unsigned int *)(results[j].res);
for(count = *i, i=(unsigned int *)((char *)i+sizeof(unsigned int)); count > 0 ; count--) {
for(count = *i, i = (unsigned int *)((char *)i+sizeof(unsigned int)); count > 0 ; count--) {
if( (pkloss = add_bucket((struct flow_data *)i, j) )!= 0 ) {
totalpkloss += pkloss;
}
if( j == 0 )
pkcount0 += ((struct flow_data *)i)->packets_count;
else if( j == 1 )
pkcount1 += ((struct flow_data *)i)->packets_count;
i = (unsigned int *)((struct flow_data *)i + 1);
if( j == 0 )
pkcount0 += ((struct flow_data *)i)->packets_count;
else if( j == 1 )
pkcount1 += ((struct flow_data *)i)->packets_count;
i = (unsigned int *)((struct flow_data *)i + 1);
}
}
if(with_ncurses) {
......@@ -129,11 +133,11 @@ int main(int argc, char *argv[]){
p += snprintf(p, 16, ":0");
my_rrd_update(RRD_FILENAME, sample);
}
return 0;
}
void drop_old_flows(void) {
struct hash_bucket *tmp, *prev;
int i;
struct timeval *tp;
......@@ -151,8 +155,10 @@ void drop_old_flows(void) {
}
for(i = 0; i < HASHTABLE_SIZE; ++i) {
tmp = hashtable[i];
prev = NULL;
while(tmp != NULL) {
if(current_ts - tmp->data.timestamp > ((unsigned long long)(TIMEOUT))<<32) {
if( prev != NULL )
......@@ -168,6 +174,7 @@ void drop_old_flows(void) {
}
void terminate() {
int i;
struct hash_bucket *tmp;
......@@ -175,10 +182,25 @@ void terminate() {
endwin(); /* end curses */
fprintf(stdout, "Terminated\n");
for(i = 0; i < HASHTABLE_SIZE; ++i) {
tmp = hashtable[i];
while(tmp != NULL) {
fprintf(stdout, "flow[%d] : saddr = %d.%d.%d.%d, daddr = %d.%d.%d.%d, sport = %d, dport = %d, packets = %lld\n", tmp->flow, (u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte1, (u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte2, (u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte3, (u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte4, (u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte1, (u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte2, (u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte3, (u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte4, ((struct flow_data *)&(tmp->data))->sport, ((struct flow_data *)&(tmp->data))->dport, ((struct flow_data *)&(tmp->data))->packets_count);
fprintf(stdout, "flow[%d] : saddr = %d.%d.%d.%d, daddr = %d.%d.%d.%d, sport = %d, dport = %d, packets = %lld\n", tmp->flow,
(u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte1,
(u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte2,
(u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte3,
(u_int32_t)((struct flow_data *)&(tmp->data))->saddr.byte4,
(u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte1,
(u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte2,
(u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte3,
(u_int32_t)((struct flow_data *)&(tmp->data))->daddr.byte4,
((struct flow_data *)&(tmp->data))->sport,
((struct flow_data *)&(tmp->data))->dport,
((struct flow_data *)&(tmp->data))->packets_count);
tmp = tmp->next;
}
}
......@@ -186,6 +208,7 @@ void terminate() {
fprintf(stdout, "Total Packets lost = %lld (%.2lf%%)\n", totalpkloss, (float)(((float)(totalpkloss*100))/((float)pkcount1)));
else
fprintf(stdout, "Total Packets lost = %lld\n", totalpkloss);
fprintf(stdout, "Flow[0] send %lld packets.\n", pkcount0);
fprintf(stdout, "Flow[1] send %lld packets.\n", pkcount1);
mapi_close_flow(fd);
......@@ -193,6 +216,7 @@ void terminate() {
}
int compare_ip(ip_addr ip1, ip_addr ip2) {
if( (ip1.byte1 == ip2.byte1) && (ip1.byte2 == ip2.byte2) && (ip1.byte3 == ip2.byte3) && (ip1.byte4 == ip2.byte4) )
return(1);
......@@ -229,6 +253,7 @@ struct hash_bucket *compare_bucket(struct hash_bucket *bucket, struct flow_data
}
unsigned long long add_bucket(struct flow_data *data, unsigned int flow) {
unsigned int value = 0, pos, ret;
ip_addr saddr = data->saddr, daddr = data->daddr;
u_short sport = data->sport, dport = data->dport;
......@@ -239,12 +264,14 @@ unsigned long long add_bucket(struct flow_data *data, unsigned int flow) {
tp = (struct timeval *)malloc(sizeof(struct timeval));
if( (data->ptcl == IPPROTO_UDP) || (data->ptcl == IPPROTO_TCP) ) {
value = ((unsigned int)((saddr.byte1 + saddr.byte2 + saddr.byte3 + saddr.byte4)*sport + (daddr.byte1 + daddr.byte2 + daddr.byte3 + daddr.byte4)*dport))/137;
value = ((unsigned int)((saddr.byte1 + saddr.byte2 + saddr.byte3 + saddr.byte4)*sport +
(daddr.byte1 + daddr.byte2 + daddr.byte3 + daddr.byte4)*dport))/137;
}
else
value = ((unsigned int)((saddr.byte1 + saddr.byte2 + saddr.byte3 + saddr.byte4) + (daddr.byte1 + daddr.byte2 + daddr.byte3 + daddr.byte4)))/137;
value = ((unsigned int)((saddr.byte1 + saddr.byte2 + saddr.byte3 + saddr.byte4) +
(daddr.byte1 + daddr.byte2 + daddr.byte3 + daddr.byte4)))/137;
pos = value%HASHTABLE_SIZE;
// Flow already exist?
if((tmp = compare_bucket(hashtable[pos], data)) != NULL) {
if(tmp->flow == flow) {
......@@ -261,7 +288,7 @@ unsigned long long add_bucket(struct flow_data *data, unsigned int flow) {
ret = tmp->data.packets_count - data->packets_count;
}
else
ret = data->packets_count -tmp->data.packets_count;
ret = data->packets_count - tmp->data.packets_count;
// remove node first
prev = hashtable[pos];
// if tmp == head
......@@ -285,14 +312,15 @@ unsigned long long add_bucket(struct flow_data *data, unsigned int flow) {
tmp->value = value;
tmp->flow = flow;
tmp->next = NULL;
if(gettimeofday(tp, NULL) ==0) {
// calculate the current timestamp
current_ts = (((unsigned long long)tp->tv_sec)<<32)+((tp->tv_usec*4295) & 0xffffffff);
tmp->data.timestamp = current_ts;
}
prev = hashtable[pos];
if( prev != NULL) {
tmp2 = prev->next;
while(tmp2 != NULL) {
......@@ -306,10 +334,10 @@ unsigned long long add_bucket(struct flow_data *data, unsigned int flow) {
}
//fprintf(stdout, "flow = %d, packets = %llu\n", flow, data->packets_count);
return(0);
}
void my_rrd_create(char *filename, int num) {
int ret, i;
char start[20];
char tmp[64];
......@@ -324,6 +352,7 @@ void my_rrd_create(char *filename, int num) {
argv[4] = strdup("1");
argv[5] = filename;
argv[6] = strdup("DS:total:GAUGE:600:-100000000:10000000000000");
for (i=0; i<num; ++i) {
snprintf(tmp, 64, "DS:flow%d:GAUGE:600:-100000000:10000000000000", i+1);
argv[7+i] = strdup(tmp);
......@@ -333,6 +362,7 @@ void my_rrd_create(char *filename, int num) {
optind = opterr = 0; /* reset optind/opterr */
ret = rrd_create(9+num-1,argv); /* try to create rrd */
if(rrd_test_error()) { /* look for errors */
printf("rrd_create: %s\n",rrd_get_error());
rrd_clear_error();
......@@ -340,44 +370,48 @@ void my_rrd_create(char *filename, int num) {
}
void my_rrd_update(char *filename, char *data) {
int ret, argc = 3;
char *argv[] = { /* update template */
"update",
NULL, /* 3 filename */
NULL, /* 4 data */
NULL, /* 3 filename */
NULL, /* 4 data */
NULL
};
argv[1] = filename; /* set filename */
argv[2] = data; /* set data */
argv[2] = data; /* set data */
optind = opterr = 0; /* reset optind/opterr */
ret = rrd_update(argc,argv); /* try to update rrd */
optind = opterr = 0; /* reset optind/opterr */
ret = rrd_update(argc,argv); /* try to update rrd */
//printf("rrd_update: %d\n", ret); /* print result */
if(rrd_test_error()) { /* look for errors */
if(rrd_test_error()) { /* look for errors */
printf("rrd_update: %s\n",rrd_get_error());
rrd_clear_error();
rrd_clear_error();
}
}
void my_rrd_graph(char *filename, char *data) {
int ret, argc = 3;
char *argv[] = { /* update template */
"update",
NULL, /* 3 filename */
NULL, /* 4 data */
NULL, /* 3 filename */
NULL, /* 4 data */
NULL
};
argv[1] = filename; /* set filename */
argv[2] = data; /* set data */
argv[2] = data; /* set data */
optind = opterr = 0; /* reset optind/opterr */
ret = rrd_update(argc,argv); /* try to update rrd */
optind = opterr = 0; /* reset optind/opterr */
ret = rrd_update(argc,argv); /* try to update rrd */
//printf("rrd_update: %d\n", ret); /* print result */
if(rrd_test_error()) { /* look for errors */
if(rrd_test_error()) { /* look for errors */
printf("rrd_graph: %s\n",rrd_get_error());
rrd_clear_error();
rrd_clear_error();
}
}
......@@ -411,6 +445,7 @@ window.location = 'http://139.91.70.50/cgi-bin/packetloss.cgi';\n\
int create_cgi(int argc, char **argv) {
FILE *fp;
char cwd[512], *p;
static char *colors[] = {"FFFF00", "0066FF", "00EE00", "FF0000", "FF9900", "9900FF", "00EEFF"};
......@@ -420,11 +455,13 @@ int create_cgi(int argc, char **argv) {
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
chmod("exprflows.cgi", S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
fprintf(fp, "%s", cgi1);
fprintf(fp, " DEF:total=%s/%s:total:AVERAGE\n", cwd, RRD_FILENAME);
for(i=1; i<argc; ++i) {
fprintf(fp, " DEF:flow%d=%s/%s:flow%d:AVERAGE\n", i, cwd, RRD_FILENAME, i);
}
......@@ -436,5 +473,6 @@ int create_cgi(int argc, char **argv) {
fprintf(fp, " AREA:total#FFFF00>\n");
fprintf(fp, "</P>\n</cetner>\n</BODY>\n</HTML>\n");
fclose(fp);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment