Commit 48c860ea authored by 's avatar

PKTBYTECOUNTERFILTER function for getting pkt/byte counts from Combov2 hw with Nific fw.



git-svn-id: file:///home/svn/mapi/trunk@1603 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 6e4ef441
//Sun Jan 23 10:58:51 2005 //Fri Oct 29 15:27:46 2010
//This file was created automatically by createlib.pl //This file was created automatically by createlib.pl
#include <stdio.h> #include <stdio.h>
#include "mapidflib.h" #include "mapidflib.h"
#include "debug.h" #include "debug.h"
char libname[]="COMBO6LIB"; char libname[]="combo6lib";
__attribute__ ((constructor)) void init (); __attribute__ ((constructor)) void init ();
__attribute__ ((destructor)) void fini (); __attribute__ ((destructor)) void fini ();
mapidflib_functionlist_t functions[6]; mapidflib_functionlist_t functions[1];
extern mapidflib_function_def_t * c6all_get_funct_info(); extern mapidflib_function_def_t * pktbytecounterrules_get_funct_info();
extern mapidflib_function_def_t * c6bpf_get_funct_info();
extern mapidflib_function_def_t * c6byte_counter_get_funct_info();
extern mapidflib_function_def_t * c6ifstats_get_funct_info();
extern mapidflib_function_def_t * c6sample_get_funct_info();
extern mapidflib_function_def_t * c6stats_get_funct_info();
mapidflib_functionlist_t* mapidflib_get_function_list() mapidflib_functionlist_t* mapidflib_get_function_list()
{ {
functions[0].def=NULL; functions[0].def=pktbytecounterrules_get_funct_info();
functions[0].def->libname=NULL; functions[0].def->libname=libname;
functions[0].next=NULL; functions[0].next=NULL;
return &functions[0]; return &functions[0];
} }
...@@ -35,12 +30,12 @@ char *mapidflib_get_libname() { ...@@ -35,12 +30,12 @@ char *mapidflib_get_libname() {
__attribute__ ((constructor)) __attribute__ ((constructor))
void init () void init ()
{ {
DEBUG_CMD(printf ("Library COMBO6LIB loaded\n")); DEBUG_CMD(printf ("Library combo6lib loaded\n"));
} }
__attribute__ ((destructor)) __attribute__ ((destructor))
void fini () void fini ()
{ {
DEBUG_CMD(printf ("Library COMBO6LIB unloaded\n")); DEBUG_CMD(printf ("Library combo6lib unloaded\n"));
} }
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <dbus/dbus.h>
#include <combosix.h>
#include <commlbr.h>
#include "debug.h"
#include "mapi_errors.h"
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "mapid.h"
#include "pktbytecounterrules.h"
/*! Classification unit base address */
#define CLA_BASE_ADDRESS 0x1E00000
#define RULES_MAX 2048LLU
#define MAX_INT32 0x100000000LLU
#define MASK_PACKETS_HIGH 0xffffffff00000000LLU
#define MASK_PACKETS_LOW 0x00000000ffffffffLLU
#define MAX_INT36 0x1000000000LLU
#define MASK_BYTES_HIGH 0xfffffff000000000LLU
#define MASK_BYTES_LOW 0x0000000fffffffffLLU
#define DEFAULT_RULE 0x3fffLLU
#define NONIP_RULE 0x3ffeLLU
#define WAIT_READING 1000000LLU
#define BYTES_OFFSET 0x80000
/* global variables - not external */
u_int32_t base_addr = CLA_BASE_ADDRESS; /* default classification unit address */
DBusConnection *dbus_connection; /* D-BUS connection for inter-process communication */
#define BUFFER_SIZE 1024
#define IPC_TIME_TO_WAIT 500
/* own service provided by nificexp */
#define EXPORTER_DBUS_SERVICE_NAME "org.liberouter.nific.exporter"
#define NIFIC_DBUS_INTERFACE_ADL "org.liberouter.nific.adl"
#define NIFIC_DBUS_OBJECT_EXPORTER "/org/liberouter/nific/exporter"
#define NIFIC_DBUS_OBJECT_EXPORTER_MC "/org/liberouter/nific/exporter/monitoringCenters"
#define NIFIC_DBUS_OBJECT_EXPORTER_RULES "/org/liberouter/nific/exporter/rules"
/* possible D-Bus exporter methods */
#define NIFIC_DBUS_SIGNAL_ADL_UPDATE "update"
#define NIFIC_DBUS_METHOD_EXPORTER_SUDDEN_DEATH "suddenDeath"
#define NIFIC_DBUS_METHOD_EXPORTER_PLANNED_DEATH "plannedDeath"
#define EXP_SIGNAL_SUDDEN_DEATH "suddenDeath"
#define EXP_SIGNAL_PLANNED_DEATH "plannedDeath"
struct pktbytecounterrules_inst_struct {
cs_device_t *device;
cs_space_t *counters;
int table_size;
};
typedef struct pktbytecounterrules_rules {
// unsigned int rules;
unsigned long long packets[RULES_MAX];
unsigned long long bytes[RULES_MAX];
} pktbytecounterrules_rules_t;
int ipc_init() {
fprintf(stderr, "ipc init\n");
DBusError dbus_err;
char *signal_filter = NULL;
/* initialise the errors */
dbus_error_init(&dbus_err);
/* connect to the D-Bus */
if ((dbus_connection =
cl_dbus_init(DBUS_BUS_SYSTEM, EXPORTER_DBUS_SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE)) == NULL) {
DEBUG_CMD(Debug_Message("Unable to get %s interface.",
EXPORTER_DBUS_SERVICE_NAME));
return 1;
}
/* and subscribe for listening D-Bus signals from NIFIC configuration daemon */
if ((signal_filter = (char *) malloc(BUFFER_SIZE * sizeof(char))) == NULL) {
DEBUG_CMD(Debug_Message("Cannot allocate memory"));
return 1;
}
snprintf(signal_filter, BUFFER_SIZE, "type='signal',interface='%s'", NIFIC_DBUS_INTERFACE_ADL);
dbus_bus_add_match(dbus_connection, signal_filter, &dbus_err);
dbus_connection_flush(dbus_connection);
if (dbus_error_is_set(&dbus_err)) {
DEBUG_CMD(Debug_Message("D-Bus match error (%s)", dbus_err.message));
dbus_error_free(&dbus_err);
return 1;
}
/* initialise the errors */
dbus_error_init(&dbus_err);
return 0;
}
int ipc_read(DBusMessage ** msg) {
fprintf(stderr, "ipc read\n");
/* non blocking read of the next available message */
dbus_connection_read_write(dbus_connection, IPC_TIME_TO_WAIT);
*msg = dbus_connection_pop_message(dbus_connection);
/* is anything to do? */
if (*msg == NULL) {
return 1;
}
DEBUG_CMD(Debug_Message("message path: %s", dbus_message_get_path(*msg)));
DEBUG_CMD(Debug_Message("message interface: %s", dbus_message_get_interface(*msg)));
DEBUG_CMD(Debug_Message("message member: %s", dbus_message_get_member(*msg)));
DEBUG_CMD(Debug_Message("message destination: %s", dbus_message_get_destination(*msg)));
/* handle standard D-Bus requests */
if (cl_dbus_handlestdif(*msg, dbus_connection, EXPORTER_DBUS_SERVICE_NAME) != 0) {
DEBUG_CMD(Debug_Message("D-Bus standard interface message received"));
return 1;
}
DEBUG_CMD(Debug_Message("Some message received"));
return 0;
}
void
reset_all_counters(cs_device_t * device, cs_space_t * counters)
{
fprintf(stderr, "reset\n");
u_int16_t i = 0;
for (i = 0; i < RULES_MAX; i++) {
cs_space_write_4(device, counters, i * 4, 0);
cs_space_write_4(device, counters, BYTES_OFFSET + (i * 8), 0);
cs_space_write_4(device, counters, BYTES_OFFSET + (i * 8) + 4, 0);
}
}
static int pktbytecounterrules_instance(mapidflib_function_instance_t *instance,
MAPI_UNUSED int fd,
MAPI_UNUSED mapidflib_flow_mod_t *flow_mod) {
// mapiFunctArg* fargs;
// fargs = instance->args;
fprintf(stderr, "instance\n");
instance->def->shm_size = sizeof(pktbytecounterrules_rules_t);
return 0;
}
static int pktbytecounterrules_init(mapidflib_function_instance_t *instance,
MAPI_UNUSED int fd) {
fprintf(stderr, "init\n");
struct pktbytecounterrules_inst_struct *internal_data_ptr;
pktbytecounterrules_rules_t *result_data_ptr;
result_data_ptr = (pktbytecounterrules_rules_t *) instance->result.data;
// mapiFunctArg* fargs;
// fargs = instance->args;
char *path = CS_PATH_DEV(0); /* Path to COMBO device */
if((instance->internal_data = malloc(sizeof(struct pktbytecounterrules_inst_struct))) == NULL) {
fprintf(stderr, "pktbytecounterrules_init(): could not allocate internal data.\n");
return MAPID_MEM_ALLOCATION_ERROR;
}
internal_data_ptr = (struct pktbytecounterrules_inst_struct *) (instance->internal_data);
internal_data_ptr->table_size = 0;
/* attach combo device */
if (cs_attach_noex(&internal_data_ptr->device, path) != 0) {
DEBUG_CMD(Debug_Message("Attaching card failed!"));
}
/* map NIFIC classification unit */
if (cs_space_map(internal_data_ptr->device, &internal_data_ptr->counters, CS_SPACE_FPGA, CS_MAP_ALL, base_addr, 0) != 0) {
DEBUG_CMD(Debug_Message("Failed to map COMBO space at base address 0x%X", base_addr));
}
/* init arrays */
int i;
for (i = 0; i < RULES_MAX; i++) {
result_data_ptr->packets[i] = 0;
result_data_ptr->bytes[i] = 0;
}
/* initialize inter-process communication */
ipc_init();
return 0;
}
static int pktbytecounterrules_get_result(mapidflib_function_instance_t* instance,
mapidflib_result_t **res)
{
fprintf(stderr, "get res\n");
struct pktbytecounterrules_inst_struct *internal_data_ptr;
internal_data_ptr = (struct pktbytecounterrules_inst_struct *) (instance->internal_data);
pktbytecounterrules_rules_t *result_data_ptr;
result_data_ptr = (pktbytecounterrules_rules_t *) instance->result.data;
DBusMessage *msg;
uint32_t rule_count = 0;
uint32_t *sw_ids = NULL;
uint32_t *hw_ids = NULL;
/* read counters */
u_int16_t i = 0;
u_int64_t data = 0;
u_int64_t low = 0;
u_int64_t high = 0;
for (i = 0; i < RULES_MAX; i++) {
/* packets */
data = cs_space_read_4(internal_data_ptr->device, internal_data_ptr->counters, (i * 4));
low = MASK_PACKETS_LOW & result_data_ptr->packets[i];
high = MASK_PACKETS_HIGH & result_data_ptr->packets[i];
/* no overflow */
if (low <= data) {
result_data_ptr->packets[i] = high + data;
}
/* overflow occurred */
else {
result_data_ptr->packets[i] = high + MAX_INT32 + data;
}
if(i==0)fprintf(stderr, "packets[%d] = %llu\n", i, result_data_ptr->packets[i]);
/* bytes */
/* get 36b from HW */
low = cs_space_read_4(internal_data_ptr->device, internal_data_ptr->counters, BYTES_OFFSET + (i * 8));
high = cs_space_read_4(internal_data_ptr->device, internal_data_ptr->counters, BYTES_OFFSET + (i * 8) + 4);
high = high * MAX_INT32;
data = high + low;
low = MASK_BYTES_LOW & result_data_ptr->bytes[i];
high = MASK_BYTES_HIGH & result_data_ptr->bytes[i];
/* no overflow */
if (low <= data) {
result_data_ptr->bytes[i] = high + data;
}
/* overflow occurred */
else {
result_data_ptr->bytes[i] = high + MAX_INT36 + data;
}
if(i==0)fprintf(stderr, "bytes[%d] = %llu\n", i, result_data_ptr->bytes[i]);
}
//result_data_ptr->packets[0] = 69; // DEBUG
*res = &instance->result; // must point to actual value
fprintf(stderr, "got res\n");
return 0;
}
static int pktbytecounterrules_reset(mapidflib_function_instance_t* instance) {
struct pktbytecounterrules_inst_struct *internal_data_ptr;
internal_data_ptr = (struct pktbytecounterrules_inst_struct *) (instance->internal_data);
pktbytecounterrules_rules_t *result_data_ptr;
result_data_ptr = (pktbytecounterrules_rules_t *) instance->result.data;
//internal_data_ptr->pktbytecounterrules_bytes = 0;
//internal_data_ptr->pktbytecounterrules_packets = 0;
reset_all_counters(internal_data_ptr->device, internal_data_ptr->counters);
memset(&result_data_ptr, 0, instance->def->shm_size);
return 0;
}
static int pktbytecounterrules_cleanup(mapidflib_function_instance_t *instance) {
free(instance->internal_data);
return 0;
}
static int pktbytecounterrules_client_read_result(mapidflib_function_instance_t *instance,mapi_result_t *res) {
fprintf(stderr, "get res\n");
return 0;
}
static mapidflib_function_def_t finfo={
"", //libname
"PKTBYTECOUNTERFILTER", //name
"Counts number of packets captured", //descr
"", //argdescr
MAPI_DEVICE_SCAMPI, //devtype
MAPIRES_SHM, //Method for returning results
0, //shm size. Set by instance.
0, //modifies_pkts
0, //filters packets
MAPIOPT_NONE, //Optimization
pktbytecounterrules_instance, //instance
pktbytecounterrules_init, //init
NULL, //process
pktbytecounterrules_get_result, //get_result
pktbytecounterrules_reset,
pktbytecounterrules_cleanup, //cleanup
NULL, //client_init
NULL, //client_read_result
NULL //client_cleanup
};
mapidflib_function_def_t* pktbytecounterrules_get_funct_info();
mapidflib_function_def_t* pktbytecounterrules_get_funct_info() {
return &finfo;
};
#ifndef __PKTBYTE_COUNTER_RULES_H__
#define __PKTBYTE_COUNTER_RULES_H__
#include "mapidflib.h"
mapidflib_function_def_t* c6bpf_get_funct_info();
#endif
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