mapidagdrv.c 12.8 KB
Newer Older
Arne Øslebø's avatar
Arne Øslebø committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <pcap.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <netinet/in.h>
#include "mapi.h"
#include "mapidrv.h"
#include "mapidlib.h"

#include "dagnew.h"
#include "dagapi.h"
#include "mapidevices.h"
#include "flist.h"
#include "debug.h"


#define DAGSTR "dag"

Arne Øslebø's avatar
Arne Øslebø committed
27
#define BUFSIZE 131072
Arne Øslebø's avatar
Arne Øslebø committed
28
29
30
31
32
33
34
35
36
37
38
39
40

__attribute__ ((constructor)) void init ();
__attribute__ ((destructor))  void fini ();

typedef struct dag_instance {
  pthread_attr_t th_attr;
  pthread_t th_proc;
  int dagfd;
  int eventset;
  void *buf;
  int file;
  char *name;
  int id;
's avatar
committed
41
  mapi_offline_device_status_t *offline_status;
Arne Øslebø's avatar
Arne Øslebø committed
42
43
  mapid_hw_info_t hwinfo;
  mapidlib_instance_t mapidlib;
Arne Øslebø's avatar
Arne Øslebø committed
44
  int active; //Workaround for possible race condition when deleting a driver
Arne Øslebø's avatar
Arne Øslebø committed
45
46
47
48
49
50
51
52
53
54
55
56
57
} dag_instance_t;

flist_t *devlist;

/* for mapidlib errorcode */
int 
mapidrv_get_errno(int devid,int fd)
{
  dag_instance_t *i=flist_get(devlist,devid);
  return mapid_get_errno(&i->mapidlib,fd);
}

int
58
mapidrv_apply_function (int devid,int fd, char* function, mapiFunctArg *fargs)
Arne Øslebø's avatar
Arne Øslebø committed
59
60
{
  dag_instance_t *i=flist_get(devlist,devid);
61
  return mapid_apply_function(&i->mapidlib,fd, function, fargs);
Arne Øslebø's avatar
Arne Øslebø committed
62
63
}

64
int mapidrv_add_device(mapi_offline_device_status_t *olstatus,char *devname, int file,int devid, global_function_list_t *gflist)
Arne Øslebø's avatar
Arne Øslebø committed
65
66
{
  dag_instance_t *i=malloc(sizeof(dag_instance_t));
67

Arne Øslebø's avatar
Arne Øslebø committed
68
69
70
71
72
73
  i->name=strdup(devname);
  i->id=devid;
  i->dagfd=-1;
  i->file=file;
  i->th_proc=0;
  i->hwinfo.offline=0;
74
  i->hwinfo.gflist=gflist;
's avatar
committed
75
  i->offline_status = olstatus;
Arne Øslebø's avatar
Arne Øslebø committed
76
  i->active=1;
's avatar
committed
77
78
79
  if(devid<0)
  	i->hwinfo.offline = 1;
 
Arne Øslebø's avatar
Arne Øslebø committed
80
81
82
83
84
85
86
87
  DEBUG_CMD(printf("Added device %d: %s [%s:%d]\n",devid,devname,__FILE__,__LINE__));

  flist_append(devlist,devid,i);
  mapid_init(&i->mapidlib);  
  return 0;
}

int mapidrv_delete_device(int devid)
Arne Øslebø's avatar
Arne Øslebø committed
88
{	
Arne Øslebø's avatar
Arne Øslebø committed
89
90
91
92
  dag_instance_t *i=flist_remove(devlist,devid,FLIST_LEAVE_DATA);
  
  if (i!=NULL) {
    int err=0;
Arne Øslebø's avatar
Arne Øslebø committed
93
    i->active=0;
Arne Øslebø's avatar
Arne Øslebø committed
94
95
96
97
98
99
100
101
102
103
104
105

    if (i->th_proc && pthread_equal(i->th_proc, pthread_self())==0) {
      DEBUG_CMD(printf("Calling thread != th_proc (%lu != %lu), canceling [%s:%d]\n",i->th_proc,pthread_self(),__FILE__,__LINE__));
      fflush(stdout);
      
      if ((err=pthread_cancel(i->th_proc))!=0) {
        if (!(i->hwinfo.offline==1 && err==ESRCH)) {
          WARNING_CMD(printf("Could not cancel thread for devid %d (%s) [%s:%d]\n",devid,strerror(err),__FILE__,__LINE__));
          fflush(stdout);
        }
      }
      
Arne Øslebø's avatar
Arne Øslebø committed
106
      /*      if ((err=pthread_join(i->th_proc,NULL))!=0) {
Arne Øslebø's avatar
Arne Øslebø committed
107
108
109
110
        if (!(i->hwinfo.offline==1 && err==ESRCH)) {
          WARNING_CMD(printf("Could not join thread for devid %d (%s) [%s:%d]\n",devid,strerror(err),__FILE__,__LINE__));
          fflush(stdout);
        }
Arne Øslebø's avatar
Arne Øslebø committed
111
	}*/
Arne Øslebø's avatar
Arne Øslebø committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    }
   
    if (i->hwinfo.offline==0) {
      if (i->dagfd >= 0) {
        dag_stop(i->dagfd);
        dag_close(i->dagfd);
        DEBUG_CMD(printf("Closed dag device [%s:%d]\n",__FILE__,__LINE__));
      }
    } else {
      if (i->file) {
        close(i->file);
        DEBUG_CMD(printf("Closed file [%s:%d]\n",__FILE__,__LINE__));
      }
    }
Arne Øslebø's avatar
Arne Øslebø committed
126

Arne Øslebø's avatar
Arne Øslebø committed
127
128
    mapid_destroy(&i->mapidlib);
    free(i->name);
's avatar
committed
129
130
    if(i->offline_status != NULL)
      *(i->offline_status) = DEVICE_DELETED;
Arne Øslebø's avatar
Arne Øslebø committed
131
    free(i);
's avatar
committed
132
  } 
Arne Øslebø's avatar
Arne Øslebø committed
133
134
135
136
137

  return 0;
}

static unsigned 
138
process_pkts(void *buf,unsigned len, dag_instance_t *i)
Arne Øslebø's avatar
Arne Øslebø committed
139
140
141
142
{
    unsigned c = 0;
  int rlen = 0;
  dag_record_t *rec;
143
  unsigned char *packet;
Arne Øslebø's avatar
Arne Øslebø committed
144
145
146
147
148
  mapid_pkthdr_t mhdr;

 rec = (dag_record_t *) buf;
  rlen = ntohs (rec->rlen);

Arne Øslebø's avatar
Arne Øslebø committed
149
150
151
  if(!i->active)
    return len;

Arne Øslebø's avatar
Arne Øslebø committed
152
      mapid_lock(&i->mapidlib);
Arne Øslebø's avatar
Arne Øslebø committed
153
  while (c + rlen < len && i->active) 
Arne Øslebø's avatar
Arne Øslebø committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    {
      char *p = buf;
      buf = p + rlen;
      c += rlen;
      if (rec->flags.trunc)
      	WARNING_CMD(fprintf (stderr, "Warning: buffer overflow [%s:%d]\n",__FILE__,__LINE__));
      
      packet=(unsigned char*)rec+18;
      mhdr.caplen=ntohs(rec->rlen);
      mhdr.wlen=ntohs(rec->wlen)-4;
      mhdr.ts=rec->ts;
      mhdr.ifindex=rec->flags.iface;
      mapid_process_pkt(&i->mapidlib,(unsigned char*)rec,packet,&mhdr); 
      
      //      if(c+sizeof(dag_record_t)>len-sizeof(dag_record_t)*2)
      //break;
      rec = (dag_record_t *) buf;
      rlen = ntohs (rec->rlen);
      i->hwinfo.pkts++;
    }
Arne Øslebø's avatar
Arne Øslebø committed
174
     mapid_unlock(&i->mapidlib);
Arne Øslebø's avatar
Arne Øslebø committed
175
176
177
178
179
180

  return len - c;

}

static unsigned 
181
process_pkts_offline(void *buf,unsigned len, dag_instance_t *i)
Arne Øslebø's avatar
Arne Øslebø committed
182
183
184
185
{
    unsigned c = 0;
  int rlen = 0;
  dag_record_t *rec;
186
  unsigned char *packet;
Arne Øslebø's avatar
Arne Øslebø committed
187
188
  mapid_pkthdr_t mhdr;

Arne Øslebø's avatar
Arne Øslebø committed
189
  rec = (dag_record_t *) buf;
Arne Øslebø's avatar
Arne Øslebø committed
190
  rlen = ntohs (rec->rlen);
Arne Øslebø's avatar
Arne Øslebø committed
191
  
192
  mapid_lock(&i->mapidlib);
Arne Øslebø's avatar
Arne Øslebø committed
193
  while (c + rlen <= len && rlen!=0) 
Arne Øslebø's avatar
Arne Øslebø committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    {
      char *p = buf;
      buf = p + rlen;
      c += rlen;
      if (rec->flags.trunc)
      	WARNING_CMD(fprintf (stderr, "Warning: buffer overflow [%s:%d]\n",__FILE__,__LINE__));
      
      packet=(unsigned char*)rec+18;
      mhdr.caplen=ntohs(rec->rlen);
      mhdr.wlen=ntohs(rec->wlen)-4;
      mhdr.ts=rec->ts;
      mhdr.ifindex=rec->flags.iface;
      mapid_process_pkt(&i->mapidlib,(unsigned char*)rec,packet,&mhdr); 
      
      //      if(c+sizeof(dag_record_t)>len-sizeof(dag_record_t)*2)
      //break;
      rec = (dag_record_t *) buf;
      rlen = ntohs (rec->rlen);
      i->hwinfo.pkts++;
    }
214
  mapid_unlock(&i->mapidlib);
Arne Øslebø's avatar
Arne Øslebø committed
215
216
217
218
219
220
221
222
223
224
225
226

  return len - c;

}

static void
mapidrv_offline_proc_loop(int devid)
{
  char buf[BUFSIZE];
  char *b=buf;
  int left=0,c;
  dag_instance_t *i=flist_get(devlist,devid);
227
  int err;
's avatar
committed
228
229
  
  *(i->offline_status) = DEVICE_READING;
Arne Øslebø's avatar
Arne Øslebø committed
230
231
232
233
234
235
236
237
238
239
240
241
242

  if ((err=pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) != 0) {
     ERROR_CMD(fprintf(stderr, "pthread_setcanceltype failed (%s) [%s:%d]\n",strerror(err),__FILE__,__LINE__));
     return;
  }
  
  if ((err=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) != 0) {
     ERROR_CMD(fprintf(stderr, "pthread_setcancelstate (%s) failed [%s:%d]\n",strerror(err),__FILE__,__LINE__));
     return;
  }

  c=read(i->file,b,BUFSIZE);
  while(c>0) {
243
    left=process_pkts_offline(buf,c+left,i);
Arne Øslebø's avatar
Arne Øslebø committed
244
245
246
247
248
249
250
251
    //Copy last bytes to beginning of buffer
    memcpy(buf,b+c-left,left);
    b=buf+left;

    c=read(i->file,b,BUFSIZE-left);
  }
  
  mapid_finished(&i->mapidlib);
's avatar
committed
252
253
  DEBUG_CMD(printf("Finished reading file [%s:%d], pkts: %llu\n",__FILE__,__LINE__,i->hwinfo.pkts));
  *(i->offline_status) = DEVICE_FINISHED;
Arne Øslebø's avatar
Arne Øslebø committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
}

static void
mapidrv_proc_loop (int devid)
{
  unsigned bottom = 0, top, diff;
  dag_instance_t *i=flist_get(devlist,devid);
  int err;

  if ((err=pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) != 0) {
     ERROR_CMD(fprintf(stderr, "pthread_setcanceltype failed (%s) [%s:%d]\n",strerror(err),__FILE__,__LINE__));
     return;
  }
  
  if ((err=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) != 0) {
     ERROR_CMD(fprintf(stderr, "pthread_setcancelstate (%s) failed [%s:%d]\n",strerror(err),__FILE__,__LINE__));
     return;
  }

  while (1)
    {
      top = dag_offset (i->dagfd, &bottom, 0);
      diff = top - bottom;
277
      bottom = top - process_pkts (((char *) i->buf + bottom), diff,i);
Arne Øslebø's avatar
Arne Øslebø committed
278
279
280
281
    }
 }

int
282
mapidrv_read_results (int devid,int fd, int fid, mapid_result_t** result)
Arne Øslebø's avatar
Arne Øslebø committed
283
284
{
  dag_instance_t *i=flist_get(devlist,devid);
285
  return mapid_read_results(&i->mapidlib,fd,fid,result);
Arne Øslebø's avatar
Arne Øslebø committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
}

mapid_funct_info_t* mapidrv_get_flow_functions(int devid,int fd)
{
  dag_instance_t *i=flist_get(devlist,devid);
  return mapid_get_flow_functions(&i->mapidlib,fd);
}

int mapidrv_get_flow_info(int devid,int fd,mapi_flow_info_t *info) {
  dag_instance_t *i=flist_get(devlist,devid);
  return mapid_get_flow_info(&i->mapidlib,fd,info);
}

int
mapidrv_create_offline_flow (int devid, int format,int fd,char **devtype)
{
  dag_instance_t *i=flist_get(devlist,devid);

  *devtype=MAPI_DEVICE_DAG;
's avatar
committed
305
  i->hwinfo.offline=4;
Arne Øslebø's avatar
Arne Øslebø committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

  if(format==MFF_DAG_ERF) {
    //This should be read from the file
    i->hwinfo.link_type=DLT_EN10MB;
    i->hwinfo.cap_length=1500;
    i->hwinfo.devtype=MAPI_DEVICE_DAG;
    i->hwinfo.devid=i->id;
    i->hwinfo.pkts=0;

    DEBUG_CMD(printf("Reading from trace file: %s [%s:%d]\n",i->name,__FILE__,__LINE__));

    return mapid_add_flow(&i->mapidlib,fd,&i->hwinfo,NULL);
  }

  return 0;
}

int
mapidrv_create_flow (int devid, int fd, char **devtype)
{
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  volatile uint8_t *dagiom;
  volatile daggpp_t *gpp;
  dag_instance_t *i;
  unsigned slen=0;
  dag_reg_t *regs;
  uint32_t regn, gpp_base;
  dag_reg_t result[DAG_REG_MAX_ENTRIES];
  
  if(devid<0)
    {
      dag_instance_t *i=flist_get(devlist,devid);
      
      *devtype=MAPI_DEVICE_DAG;
      i->hwinfo.offline=1;
      
      //This should be read from the file
      i->hwinfo.link_type=DLT_EN10MB;
      i->hwinfo.cap_length=1500;
      i->hwinfo.devtype=MAPI_DEVICE_DAG;
      i->hwinfo.devid=i->id;
      i->hwinfo.pkts=0;
      
      DEBUG_CMD(printf("Reading from trace file: %s [%s:%d]\n",i->name,__FILE__,__LINE__));
      
      return mapid_add_flow(&i->mapidlib,fd,&i->hwinfo,NULL);
      
      
      return 0;
      
    }
  
  
  
359
  i=flist_get(devlist,devid);
360
  
Arne Øslebø's avatar
Arne Øslebø committed
361
  i->hwinfo.offline=0;
362
  
Arne Øslebø's avatar
Arne Øslebø committed
363
  *devtype=MAPI_DEVICE_DAG;
364
  
Arne Øslebø's avatar
Arne Øslebø committed
365
366
  //Open device if it is not already open
  if (i->dagfd < 0)
367
    {
Arne Øslebø's avatar
Arne Øslebø committed
368
      if ((i->dagfd = dag_open (i->name)) < 0)	{
369
370
	ERROR_CMD(fprintf (stderr, "dag_open %s: %s [%s:%d]\n", i->name, strerror (errno),__FILE__,__LINE__));
	return DAGDRV_OPEN_ERR;
Arne Øslebø's avatar
Arne Øslebø committed
371
      }
372
      
Arne Øslebø's avatar
Arne Øslebø committed
373
      i->hwinfo.cap_length=0;
374
      
375
376
377
378
379
380
381
382
383
384
385
386
387
388
      regs = dag_regs (i->dagfd);
      regn = 0;
      if ((dag_reg_table_find(regs, 0, DAG_REG_GPP, result, &regn)) || (!regn))
	gpp_base = 0;
      else
	gpp_base = DAG_REG_ADDR(*result);
      if (gpp_base) {
	/* memory mapped area */      
	dagiom = dag_iom(i->dagfd);
	if (((*(volatile uint32_t *)dagiom >> 16) & 0xff) != 0x03) {
	  gpp = (daggpp_t *) (dagiom + gpp_base);	/* WARNING: race */
	  slen = gpp->snaplen;
	}
      }
Arne Øslebø's avatar
Arne Øslebø committed
389
390
391
392
393
394
395
396
      i->hwinfo.cap_length=slen;
      
      if (i->hwinfo.cap_length==0) {
        WARNING_CMD(printf("WARNING: Could not get info hardware-info, using default =1500 [%s:%d]\n",__FILE__,__LINE__));
        i->hwinfo.cap_length=1500;
      }
      
      switch (dag_linktype(i->dagfd)) {
397
398
399
400
401
402
403
404
405
      default:
      case TYPE_ETH:
	i->hwinfo.link_type=DLT_EN10MB;
	break;
	
      case TYPE_ATM:
	i->hwinfo.link_type=DLT_ATM_RFC1483;
	break;
	
Arne Øslebø's avatar
Arne Øslebø committed
406
        /* can be both DLT_CHDLC and DLT_PPP_SERIAL?? */
407
408
409
      case TYPE_HDLC_POS:
	i->hwinfo.link_type=DLT_CHDLC;
	break;
Arne Øslebø's avatar
Arne Øslebø committed
410
      }    
411
      
Arne Øslebø's avatar
Arne Øslebø committed
412
413
414
415
      DEBUG_CMD(printf("From dag HW: cap_length=%u linktype=%u [%s:%d]\n",i->hwinfo.cap_length,i->hwinfo.link_type,__FILE__,__LINE__));
      i->hwinfo.devtype=MAPI_DEVICE_DAG;
      i->hwinfo.devid=i->id;
      i->hwinfo.pkts=0;
416
      
Arne Øslebø's avatar
Arne Øslebø committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
      //Initialize DAG buffer
      if ((i->buf = dag_mmap (i->dagfd)) == MAP_FAILED)
    	{
    	  ERROR_CMD(fprintf (stderr, "dag_mmap failed [%s:%d]\n",__FILE__,__LINE__));
    	  return DAGDRV_MMAP_ERR;
    	}
      
      if (dag_start (i->dagfd) < 0)
    	{
    	  ERROR_CMD(fprintf (stderr, "dag_start failed [%s:%d]\n",__FILE__,__LINE__));
    	  return DAGDRV_START_ERR;
    	}
      
      //Start processing thread
      if (pthread_attr_init (&i->th_attr) != 0)
    	{
    	  ERROR_CMD(fprintf (stderr, "pthread_attr_init failed [%s:%d]\n",__FILE__,__LINE__));
    	  return DAGDRV_PTHR_ERR;
    	}
436
      
Arne Øslebø's avatar
Arne Øslebø committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
      if (pthread_create
	  (&i->th_proc, &i->th_attr, (void *) mapidrv_proc_loop, (void*)devid) != 0)
    	{
    	  ERROR_CMD(fprintf (stderr, "pthread_create failed [%s:%d]\n",__FILE__,__LINE__));
    	  return DAGDRV_PTHR_ERR;
    	}    
    }
  return   mapid_add_flow(&i->mapidlib,fd,&i->hwinfo,NULL);
}

int
mapidrv_connect (int devid,int fd)
{
  dag_instance_t *i=flist_get(devlist,devid);
Arne Øslebø's avatar
Arne Øslebø committed
451
  if(i==NULL)
Arne Øslebø's avatar
Arne Øslebø committed
452
  	return -1;
's avatar
committed
453
454
455
  return mapid_connect(&i->mapidlib,fd);

}
Arne Øslebø's avatar
Arne Øslebø committed
456

's avatar
committed
457
458
459
460
461
int 
mapidrv_start_offline_device( int devid)
{
  dag_instance_t *i = flist_get(devlist,devid);
  
Arne Øslebø's avatar
Arne Øslebø committed
462
463
464
  if(i->hwinfo.offline==1) {
    if (pthread_attr_init (&i->th_attr) != 0)
      {
's avatar
committed
465
466
		ERROR_CMD(fprintf (stderr, "pthread_attr_init failed [%s:%d]\n",__FILE__,__LINE__));
		return DAGDRV_PTHR_ERR;
Arne Øslebø's avatar
Arne Øslebø committed
467
468
      }
    if (pthread_create
's avatar
committed
469
		(&i->th_proc, &i->th_attr, (void *) mapidrv_offline_proc_loop, (void *)i->id) != 0)
Arne Øslebø's avatar
Arne Øslebø committed
470
      {
's avatar
committed
471
472
		ERROR_CMD(fprintf (stderr, "pthread_create failed [%s:%d]\n",__FILE__,__LINE__));
		return DAGDRV_PTHR_ERR;
Arne Øslebø's avatar
Arne Øslebø committed
473
474
      }    
  }
's avatar
committed
475
  return 0;
Arne Øslebø's avatar
Arne Øslebø committed
476
477
478
479
480
481
482
483
484
485
486
487
}



int
mapidrv_close_flow (int devid,int fd)
{
  dag_instance_t *i=flist_get(devlist,devid);
  return mapid_close_flow(&i->mapidlib,fd);
}

int 
488
mapidrv_load_library(MAPI_UNUSED int devid,char* lib)
Arne Øslebø's avatar
Arne Øslebø committed
489
490
491
492
493
{
  return mapid_load_library(lib);
}


's avatar
committed
494
495
496



's avatar
committed
497
mapi_function_def_mini_t*
's avatar
committed
498
499
500
501
502
mapidrv_get_function_info(int libnumber,int functionnumber)
{
	return mapid_get_function_info(libnumber, functionnumber);
}

503
unsigned char*
's avatar
committed
504
505
506
507
508
509
510
511
512
513
514
515
516
mapidrv_get_lib_name(int libnumber)
{
	return mapid_get_lib_name(libnumber);
}









Arne Øslebø's avatar
Arne Øslebø committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
__attribute__ ((constructor))
     void init ()
{
  devlist=malloc(sizeof(flist_t));
  flist_init(devlist);
  DEBUG_CMD(printf ("DAG driver loaded [%s:%d]\n",__FILE__,__LINE__));
}

__attribute__ ((destructor))
     void fini ()
{
  free(devlist);
  DEBUG_CMD(printf ("DAG driver unloaded [%s:%d]\n",__FILE__,__LINE__));
}