mapicombo6drv.c 7.03 KB
Newer Older
1
2
3
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Arne Øslebø's avatar
Arne Øslebø committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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 <sys/mman.h>
#include <sys/shm.h>
#include <net/bpf.h>
#include <sys/time.h>
#include "mapi.h"
#include "mapidrv.h"
#include "mapidlib.h"
#include "mapid.h"
#include "mapidevices.h"
#include "flist.h"
's avatar
committed
23

Arne Øslebø's avatar
Arne Øslebø committed
24
#include "libscampi.h"
's avatar
committed
25
#include "libfilter.h"       /* scampi_reset_classification() */
Arne Øslebø's avatar
Arne Øslebø committed
26
27
28
29
30
31
32
33
34
35
36

/* Suppress inclusion of types.h, because <linux/types.h> was already included */
#define _TYPES_H 1
#include "libscampi.h"
#include "scampi_kernel.h"  // for packet descriptor

#define COMBO6STR "scampi"

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

's avatar
committed
37
38
function_manipulation_t* manipulation;

Arne Øslebø's avatar
Arne Øslebø committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
typedef struct combo6_instance {
  char *name; //name of device
  int id; //Device ID
  int offline; //1 if this is an offline device
  pthread_attr_t th_attr;
  pthread_t th_proc;
  int combo6fd;
  mapid_hw_info_t hwinfo;
  mapidlib_instance_t mapidlib;
} combo6_instance_t;

flist_t *devlist;

int debug=0;

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

62
63
64
65
66
67
68
69
#ifdef WITH_AUTHENTICATION
int mapidrv_authenticate(int devid, int fd, char *vo)
{
	combo6_instance_t *i = flist_get(devlist, devid);
	return mapid_authenticate(&i->mapidlib, fd, vo);
}
#endif

Arne Øslebø's avatar
Arne Øslebø committed
70
71
72
73
74
75
76
int
mapidrv_connect (int devid, int fd)
{
  combo6_instance_t *i=flist_get(devlist,devid);
  return mapid_connect(&i->mapidlib, fd);
}

's avatar
committed
77
78
79
80
81
82
83
84
85
int
mapidrv_apply_function (int devid,int fd,int pid, char* function, mapiFunctArg *fargs)
{
  combo6_instance_t *i=flist_get(devlist,devid);
  return mapid_apply_function(&i->mapidlib,fd,pid, function, fargs, manipulation);
}

/*
//Deprecated.
Arne Øslebø's avatar
Arne Øslebø committed
86
87
88
89
90
91
92
93
int
mapidrv_apply_function (int devid, int fd, char* function, mapiFunctArg *fargs)
{
    combo6_instance_t *i=flist_get(devlist,devid);
    // Sven
    // printf("mapicombo6drv:mapidrv_apply_function(): calling mapid_apply_function()\n");
    return mapid_apply_function(&i->mapidlib, fd, function, fargs);
}
's avatar
committed
94
*/
Arne Øslebø's avatar
Arne Øslebø committed
95

's avatar
committed
96
int mapidrv_add_device(mapi_offline_device_status_t *olstatus,char *devname, int file, int devid, function_manipulation_t* manip)
Arne Øslebø's avatar
Arne Øslebø committed
97
{
's avatar
committed
98
	manipulation = manip;
Arne Øslebø's avatar
Arne Øslebø committed
99
100
101
102
103
104
105
106
107
108
109
  combo6_instance_t *i=malloc(sizeof(combo6_instance_t));
  i->name=strdup(devname);
  i->id=devid;
  i->combo6fd=0;
  i->offline=0;
  i->hwinfo.ff_list=malloc(sizeof(flist_t));
  flist_init(i->hwinfo.ff_list);
#ifdef DEBUG
  printf("Added device %d: %s\n",devid,devname);
#endif
  flist_append(devlist,devid,i);
110
111
112
#ifdef WITH_AUTHENTICATION
  mapid_init(&i->mapidlib, &i->hwinfo);
#else
Arne Øslebø's avatar
Arne Øslebø committed
113
  mapid_init(&i->mapidlib);
114
#endif
Arne Øslebø's avatar
Arne Øslebø committed
115
116
117
118
  return 0;
}

static void
119
mapidrv_proc_loop (void *arg)
Arne Øslebø's avatar
Arne Øslebø committed
120
{
121
    int devid = *(int *)arg;
Arne Øslebø's avatar
Arne Øslebø committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    unsigned char *dev_pkt;
    unsigned char *link_pkt;
    mapid_pkthdr_t mhdr;
    struct scampi_mmap_packet_descriptor *descriptor;
    combo6_instance_t *i=flist_get(devlist,devid);
    char *fce="mapidrv_proc_loop()";

  while (1)
    {
	if (debug)
	  printf("Debug: combo6fd: %d\n", i->combo6fd);
        
	dev_pkt=scampi_get_next_packet(i->combo6fd, &descriptor);
        if (dev_pkt==NULL) {
          fprintf(stderr, "%s: scampi_get_next_packet() failed\n", fce);
          exit(-1);
        }
	link_pkt=(unsigned char*)dev_pkt+(descriptor->hh_size);
	mhdr.caplen=(descriptor->size)-(descriptor->hh_size);
	mhdr.wlen=(descriptor->size)-(descriptor->hh_size);
	mhdr.ts=descriptor->timestamp;
	mhdr.ifindex=descriptor->if_index;

145
146
147
148
#ifdef PRECLASSIFICATION
        mhdr.preclassification = 0;     /* process all packets */
#endif

Arne Øslebø's avatar
Arne Øslebø committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	if (debug) {
          unsigned char *p;
          int j;
          p=(unsigned char*)link_pkt;
          printf("%s: Ethernet and IP headers: ", fce);
          for (j=0; j<34; j++) {
            printf("%02x", *p++);
            if (((j-3)%4)==0)
              printf(" ");
          }
          printf("\n");
        }

        mapid_process_pkt(&i->mapidlib,dev_pkt,link_pkt,&mhdr);
        i->hwinfo.pkts++;
    }
}

int
's avatar
committed
168
mapidrv_read_results (int devid, int fd, int fid,int pid, mapid_result_t** result)
Arne Øslebø's avatar
Arne Øslebø committed
169
170
{
    combo6_instance_t *i=flist_get(devlist,devid);
's avatar
committed
171
    return mapid_read_results(&i->mapidlib,fd,fid,pid,result);
Arne Øslebø's avatar
Arne Øslebø committed
172
173
174
175
176
177
178
}

int
mapidrv_create_flow (int devid, int fd, char **devtype)
{
  combo6_instance_t *i=flist_get(devlist,devid);
  int ret;
's avatar
committed
179
  int pid;
Arne Øslebø's avatar
Arne Øslebø committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  char c6all[256]="COMBO6_ALL";
  
  *devtype=MAPI_DEVICE_SCAMPI;

  //Open device if it is not already open
  if (!i->combo6fd)
    {
      if ((i->combo6fd = scampi_open (i->name)) < 0)   {
        fprintf (stderr, "scampi_open(%s): %s\n", i->name, strerror (errno));
        return COMBO6_OPEN_ERR;
      }
      if (debug)
        printf("Debug: combo6fd: %d\n", i->combo6fd);

      //This should be read from the hardware
      i->hwinfo.link_type=DLT_EN10MB;
      i->hwinfo.cap_length=1500;
      i->hwinfo.devtype=MAPI_DEVICE_SCAMPI;
      i->hwinfo.adapterinfo=&i->combo6fd;
      i->hwinfo.devid=i->id;
      i->hwinfo.pkts=0;

      if (debug) {
        struct timeval tv;
        struct timezone tz;

        if (gettimeofday(&tv, &tz)<0)
          fprintf(stderr, "gettimeofday() failed\n");
        else
          printf("after scampi_open(): time: %d.%d\n", (int)(tv.tv_sec), 
                 (int)(tv.tv_usec));
      }

      //Start processing thread
      if (pthread_attr_init (&i->th_attr) != 0)
        {
          fprintf (stderr, "pthread_attr_init failed");
          return COMBO6_PTHR_ERR;
        }
      if (pthread_create
220
          (&i->th_proc, &i->th_attr, (void *) mapidrv_proc_loop, (void *) &devid) != 0)
Arne Øslebø's avatar
Arne Øslebø committed
221
222
223
224
225
226
227
        {
          fprintf (stderr, "pthread_create failed");
          return COMBO6_PTHR_ERR;
        }
    }

  ret=mapid_add_flow(&i->mapidlib, fd,&i->hwinfo,NULL);
's avatar
committed
228
  mapidrv_apply_function(i->hwinfo.devid,fd,pid,c6all,"");
Arne Øslebø's avatar
Arne Øslebø committed
229

230
231
232
  /* Reset mechanism of processing filters to be downloaded to Combo6 card */
  scampi_reset_classification(DIRECT);
  
Arne Øslebø's avatar
Arne Øslebø committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  return ret;
}

int mapidrv_load_library(int devid, char* lib)
{
  return mapid_load_library(lib);
}

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

mapid_funct_info_t* mapidrv_get_flow_functions(int devid, int fd)
{
  // return NULL;
  combo6_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) {
  combo6_instance_t *i=flist_get(devlist,devid);
  return mapid_get_flow_info(&i->mapidlib,fd,info);
}





's avatar
committed
264
mapi_function_def_mini_t*
's avatar
committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
mapidrv_get_function_info(int libnumber,int functionnumber)
{
	return mapid_get_function_info(libnumber, functionnumber);
}

char*
mapidrv_get_lib_name(int libnumber)
{
	return mapid_get_lib_name(libnumber);
}








Arne Øslebø's avatar
Arne Øslebø committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
__attribute__ ((constructor))
     void init ()
{
  // mapid_init();
  devlist=malloc(sizeof(flist_t));
  flist_init(devlist);
  printf ("Combo6 driver loaded\n");
}

__attribute__ ((destructor))
     void fini ()
{
  printf ("Combo6 driver unloaded\n");
}