Subversion Repositories shark

Rev

Rev 552 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
551 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * This program is free software; you can redistribute it and/or modify
21
 * it under the terms of the GNU General Public License as published by
22
 * the Free Software Foundation; either version 2 of the License, or
23
 * (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
 */
34
 
35
#include <ll/sys/types.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <tracer.h>
39
#include <unistd.h>
40
 
41
#include <FTrace_udp.h>
42
#include <FTrace_OSD.h>
43
#include <FTrace_chunk.h>
44
 
45
#include <kernel/kern.h>
46
 
47
#include <drivers/udpip.h>
48
 
49
#define TRACER_UDP_DEBUG
50
 
51
#define UDP_MAXSIZE 1000
52
#define TRACER_PORT 20000
53
 
54
#define DELAY_LOOP 10000000
55
 
56
BYTE pkt[UDP_MAXSIZE];
57
int total_pkt_size = 0;
58
 
59
int TracerUDPInit = 0;
60
UDP_ADDR target,local;
61
char local_ip[20],target_ip[20];
62
int socket;
63
 
64
PID udp_task;
65
 
66
struct FTrace_udp_header {
67
  char id[12];
68
  unsigned int pkt_number;
69
  unsigned int number_of_events;
70
  unsigned int total_size;
71
};
72
 
73
//Init UDP, if flag = 1 init the network driver
74
int FTrace_OSD_init_udp(int flag, char *l_ip, char *t_ip) {
75
 
76
  struct net_model m = net_base;
77
 
78
  strcpy(local_ip,l_ip);
79
  strcpy(target_ip,t_ip);
80
 
81
  if (flag) {
82
 
83
    net_setudpip(m, local_ip, "255.255.255.255");
84
 
85
    if (net_init(&m) != 1) {
86
      return -1;
87
    }
88
 
89
  }
90
 
91
  ip_str2addr(local_ip,&(local.s_addr));
92
  local.s_port = TRACER_PORT;
93
  socket = udp_bind(&local, NULL);
94
 
95
  ip_str2addr(target_ip,&(target.s_addr));
96
  target.s_port = TRACER_PORT;
97
 
98
  sleep(1);
99
 
100
  TracerUDPInit = 1;
101
 
102
  return 0;
103
 
104
}
105
 
106
int send_udp_event(void *p, int size) {
107
 
108
  static BYTE *current = pkt;
109
  static int events_number = 0;
110
  static int packet_number = 0;
111
 
112
  struct FTrace_udp_header head = {"TRACER-V1.0",0,0,0};
113
  struct FTrace_udp_header *phead = (struct FTrace_udp_header *)(pkt);
114
 
115
  int i;
116
 
117
  if ((total_pkt_size + size < UDP_MAXSIZE) && p != NULL ) {
118
 
119
    if (total_pkt_size == 0) {
120
 
121
      memcpy(pkt,&head,sizeof(struct FTrace_udp_header));
122
 
123
      current = pkt;
124
      current += sizeof(struct FTrace_udp_header);
125
 
126
      total_pkt_size += sizeof(struct FTrace_udp_header);
127
      phead->total_size = total_pkt_size;
128
 
129
      packet_number++;
130
      phead->pkt_number = packet_number;
131
 
132
    }
133
 
134
    events_number++;
135
    phead->number_of_events = events_number;
136
 
137
    total_pkt_size += size;
138
    phead->total_size = total_pkt_size;
139
 
140
    //Copy the event
141
    memcpy(current,p,size);
142
    current += size;
143
 
144
    return 0;
145
 
146
  } else {
147
 
148
    #ifdef TRACER_UDP_DEBUG
149
      cprintf("Packet Sent - Nr %d Size %d\n",packet_number,total_pkt_size);
150
    #endif
151
 
152
    if (TracerUDPInit == 1) {
153
      udp_sendto(socket, pkt, total_pkt_size, &target);
154
      for (i=0;i<DELAY_LOOP;i++);
155
    }
156
 
157
    events_number = 0;
158
    total_pkt_size = 0;
159
    current = pkt;
160
 
161
    if (p != NULL) send_udp_event(p,size);
162
 
163
    return 0;    
164
 
165
  }
166
 
167
  return -1;
168
 
169
}
170
 
171
//Sender Task
172
TASK udp_sender(void *arg)
173
{
174
  FTrace_Chunk_Ptr c = (FTrace_Chunk_Ptr)(arg);
175
 
176
  DWORD total_size = (DWORD)(c->size) + (DWORD)(c->emergency_size);
177
  DWORD start = (DWORD)(c->osd + FTRACE_OSD_CHUNK_HEAD);
178
  DWORD current = start;
179
  DWORD count = 0;  
180
 
181
  while (current+16 <= start+total_size) {
182
 
183
        if (*(DWORD *)(current) != 0) {
184
                send_udp_event((void *)(current), 16);
185
                count++;
186
        }
187
 
188
        current += 16;
189
 
190
  }
191
 
192
  // Flush the buffer out
193
  send_udp_event(NULL, 0);
194
 
195
  #ifdef TRACER_UDP_DEBUG
196
    cprintf("Total Chunk Event Sent: %d\n",(int)count);
197
  #endif  
198
 
199
  return 0;  
200
 
201
}
202
 
203
int FTrace_OSD_create_udp_task(FTrace_Chunk_Ptr c)
204
{
205
 
206
  NRT_TASK_MODEL st;
207
 
208
  nrt_task_default_model(st);
209
  nrt_task_def_arg(st,(void *)c);
210
 
211
  udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
212
  if (udp_task == NIL) {
213
    cprintf("Error creating UDP Sender\n");
214
    sys_end();
215
  }
216
 
217
  task_activate(udp_task);
218
 
219
  return 0;
220
 
221
}