Commit c64dbf3b authored by 's avatar

Support mapi_get_device_info function in DiMAPI


git-svn-id: file:///home/svn/mapi/trunk@1251 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent a1fc3148
......@@ -339,6 +339,7 @@ struct as specified in mapi.h:
typedef struct mapi_flow_info {
uid_t uid; /* UID of user running the flow */
int fd; /* flow descriptor */
int devid; /* ID of the device used by the flow */
char device[MAPI_STR_LENGTH]; /* name of device used by the flow */
unsigned num_functions; /* number of applied functions */
time_t start; /* start of flow */
......
......@@ -655,6 +655,7 @@ int mapi_create_flow(const char *dev)
hflow->scope_fd=rflow->fd;
hflow->dev=strdup(k);
flist_append(rflow->host_flowlist, ++idgen, hflow);
hflow->id=idgen;
flist_append(h->flows, --negfdseed, hflow);
hflow->fd=negfdseed;
hflow->dbuf=(struct dmapiipcbuf *)malloc(sizeof(struct dmapiipcbuf));
......@@ -2763,7 +2764,7 @@ int mapi_get_next_function_info(int fd,int fid, mapi_function_info_t *info)
functdescr_t *f;
#endif
pthread_once(&mapi_is_initialized, (void*)mapi_init);
// pthread_once(&mapi_is_initialized, (void*)mapi_init);
if (!minit) {
DEBUG_CMD(printf("Not initialized! [%s:%d]\n",__FILE__,__LINE__));
......@@ -2918,6 +2919,7 @@ int mapi_get_flow_info(int fd, mapi_flow_info_t *info)
switch(hflow->dbuf->cmd) {
case GET_FLOW_INFO_ACK:
memcpy(info,hflow->dbuf->data,sizeof(mapi_flow_info_t));
info->devid=fd;
continue;
case ERROR_ACK:
local_err = MAPI_FLOW_INFO_ERR;
......@@ -2984,7 +2986,7 @@ int mapi_get_next_flow_info(int fd, mapi_flow_info_t *info)
flowdescr_t* flow;
#endif
pthread_once(&mapi_is_initialized, (void*)mapi_init);
// pthread_once(&mapi_is_initialized, (void*)mapi_init);
if(fd<=0){
DEBUG_CMD(printf("Invalid Flow descriptor [%s:%d]\n",__FILE__,__LINE__));
......@@ -3019,6 +3021,7 @@ int mapi_get_next_flow_info(int fd, mapi_flow_info_t *info)
switch(hflow->dbuf->cmd) {
case GET_FLOW_INFO_ACK:
memcpy(info,hflow->dbuf->data,sizeof(mapi_flow_info_t));
info->devid=fd;
continue;
case ERROR_ACK:
local_err = MAPI_FLOW_INFO_ERR;
......@@ -3168,9 +3171,49 @@ int mapi_get_next_library_info(int libid, mapi_lib_info_t *info)
int mapi_get_next_device_info(int devid, mapi_device_info_t *info)
{
struct mapiipcbuf qbuf;
#ifdef DIMAPI
remote_flowdescr_t* rflow;
host_flow* hflow;
flist_node_t* fnode;
int i=0;
#endif
// pthread_once(&mapi_is_initialized, (void*)mapi_init);
#ifdef DIMAPI
if ((rflow=flist_get(remote_flowlist,devid))!=NULL) { //device is for a remote flow
for (fnode=flist_head(rflow->host_flowlist); fnode!=NULL; fnode=flist_next(fnode)) {
hflow=(host_flow*)fnode->data;
hflow->dbuf->cmd=GET_NEXT_DEVICE_INFO;
hflow->dbuf->fd=hflow->fd;
hflow->dbuf->length=BASIC_SIZE;
}
if (mapiipc_remote_write_to_all(rflow)<0){
local_err = MCOM_SOCKET_ERROR;
return -1;
}
//wait results
for (fnode=flist_head(rflow->host_flowlist); fnode!=NULL; fnode=flist_next(fnode)) {
hflow=(host_flow*)fnode->data;
switch(hflow->dbuf->cmd) {
case GET_DEVICE_INFO_ACK:
memcpy(&info[i++],hflow->dbuf->data,sizeof(mapi_device_info_t));
continue;
case GET_DEVICE_INFO_NACK:
local_err = MAPI_DEVICE_INFO_ERR;
return -1;
default:
local_err = MAPI_DEVICE_INFO_ERR;
return -1;
}
}
return 0;
}
#endif
pthread_once(&mapi_is_initialized, (void*)mapi_init);
qbuf.mtype=1;
qbuf.cmd=GET_NEXT_DEVICE_INFO;
qbuf.fd=devid;
......@@ -3201,9 +3244,49 @@ int mapi_get_next_device_info(int devid, mapi_device_info_t *info)
int mapi_get_device_info(int devid, mapi_device_info_t *info)
{
struct mapiipcbuf qbuf;
#ifdef DIMAPI
remote_flowdescr_t* rflow;
host_flow* hflow;
flist_node_t* fnode;
int i=0;
#endif
// pthread_once(&mapi_is_initialized, (void*)mapi_init);
#ifdef DIMAPI
if ((rflow=flist_get(remote_flowlist,devid))!=NULL) { //device is for a remote flow
for (fnode=flist_head(rflow->host_flowlist); fnode!=NULL; fnode=flist_next(fnode)) {
hflow=(host_flow*)fnode->data;
hflow->dbuf->cmd=GET_DEVICE_INFO;
hflow->dbuf->fd=hflow->fd;
hflow->dbuf->length=BASIC_SIZE;
}
if (mapiipc_remote_write_to_all(rflow)<0){
local_err = MCOM_SOCKET_ERROR;
return -1;
}
//wait results
for (fnode=flist_head(rflow->host_flowlist); fnode!=NULL; fnode=flist_next(fnode)) {
hflow=(host_flow*)fnode->data;
switch(hflow->dbuf->cmd) {
case GET_DEVICE_INFO_ACK:
memcpy(&info[i++],hflow->dbuf->data,sizeof(mapi_device_info_t));
continue;
case GET_DEVICE_INFO_NACK:
local_err = MAPI_DEVICE_INFO_ERR;
return -1;
default:
local_err = MAPI_DEVICE_INFO_ERR;
return -1;
}
}
return 0;
}
#endif
pthread_once(&mapi_is_initialized, (void*)mapi_init);
qbuf.mtype=1;
qbuf.cmd=GET_DEVICE_INFO;
qbuf.fd=devid;
......
......@@ -75,7 +75,7 @@ enum mapi_flow_status {
typedef struct mapi_flow_info {
uid_t uid; //UID of user running the flow
int fd; //Flow descriptor
int devid; // ID of the device used by the flow
int devid; // ID of the device used by the flow
char device[MAPI_STR_LENGTH]; //Name of device used by the flow
unsigned num_functions; //Number of functions applied to the flow
time_t start; //Start of flow
......
......@@ -319,6 +319,7 @@ void *handle_request(void *arg) {
int ac_fl_size=0;
mapi_function_info_t funct_info;
mapi_flow_info_t flow_info;
mapi_device_info_t device_info;
struct timeval tv; /*used for timestamping results when produced */
struct mapipkt *pkt;
int errno;
......@@ -531,6 +532,40 @@ void *handle_request(void *arg) {
dbuf->length = BASIC_SIZE+sizeof(mapi_flow_info_t);
}
break;
case GET_DEVICE_INFO:
fprintf(stdout,"GET_DEVICE_INFO\n");
if(mapi_get_flow_info(dbuf->fd, &flow_info)){
dbuf->cmd = GET_DEVICE_INFO_NACK;
dbuf->length = BASIC_SIZE;
}
if(mapi_get_device_info(flow_info.devid, &device_info)<0){
dbuf->cmd = GET_DEVICE_INFO_NACK;
dbuf->length = BASIC_SIZE;
}
else{
dbuf->cmd = GET_DEVICE_INFO_ACK;
memcpy(dbuf->data,&device_info,sizeof(mapi_device_info_t));
dbuf->length = BASIC_SIZE+sizeof(mapi_device_info_t);
}
break;
case GET_NEXT_DEVICE_INFO:
fprintf(stdout,"GET_NEXT_DEVICE_INFO\n");
if(mapi_get_flow_info(dbuf->fd, &flow_info)){
dbuf->cmd = GET_DEVICE_INFO_NACK;
dbuf->length = BASIC_SIZE;
}
if(mapi_get_next_device_info(flow_info.devid, &device_info)<0){
dbuf->cmd = GET_DEVICE_INFO_NACK;
dbuf->length = BASIC_SIZE;
}
else{
dbuf->cmd = GET_DEVICE_INFO_ACK;
memcpy(dbuf->data,&device_info,sizeof(mapi_device_info_t));
dbuf->length = BASIC_SIZE+sizeof(mapi_device_info_t);
}
break;
case GET_FUNCTION_INFO:
fprintf(stdout,"GET_FUNCTION_INFO\n");
if(mapi_get_function_info(dbuf->fd, dbuf->fid, &funct_info)){
......@@ -554,7 +589,7 @@ void *handle_request(void *arg) {
memcpy(dbuf->data,&funct_info,sizeof(mapi_function_info_t));
dbuf->length = BASIC_SIZE+sizeof(mapi_function_info_t);
}
break; break;
break;
case GET_NEXT_FLOW_INFO:
fprintf(stdout,"GET_NEXT_FLOW_INFO\n");
if(mapi_get_next_flow_info(dbuf->fd, &flow_info)){
......
#include <stdio.h>
#include <unistd.h>
#include "mapi.h"
#include "test.h"
int main(MAPI_UNUSED int argc, char *argv[])
{
int fd, ok;
mapi_flow_info_t info;
mapi_device_info_t device_info;
int err_no =0 , flag=0;
char error[512];
if(!argv[1])
{
printf("\nwrong arguments\n");
return -1;
}
if ((fd = mapi_create_flow(argv[1]))<0){
fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
if((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
fprintf(stderr,"Could not apply function PKT_COUNTER\n");
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
if(mapi_connect(fd)<0){
fprintf(stderr, "Connecting to flow failed %d\n", fd);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
if((ok = mapi_get_flow_info(fd, &info))<0){
fprintf(stderr, "Getting flow info failed on fd:%d\n", fd);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
printf("\n\t devid = %d", info.devid);
printf("\n");
DOT;
if((ok = mapi_get_device_info(info.devid, &device_info))<0){
fprintf(stderr, "Getting device info failed on devid:%d\n", info.devid);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
printf("\ndevice info : ");
printf("\n\t id = %d", device_info.id);
printf("\n\t device = %s", device_info.device);
printf("\n\t name: %s", device_info.name);
printf("\n\t alias = %s", device_info.alias);
printf("\n\t description = %s", device_info.description);
printf("\n\t link_speed = %d", device_info.link_speed);
printf("\n\t mpls = %d", device_info.mpls);
printf("\n\t vlan = %d", device_info.vlan);
printf("\n");
/*
* Sanity checks
*/
if(info.num_functions != 1){
fprintf(stderr, "WARNING: Sanity check failed %d functions found\n" , info.num_functions);
}
if(mapi_close_flow(fd)<0){
fprintf(stderr,"Close flow failed\n");
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
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