Subversion Repositories shark

Rev

Rev 551 | Rev 561 | Go to most recent revision | Details | Compare with Previous | 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
 
552 giacomo 66
int sending_flag = 0;
67
 
551 giacomo 68
struct FTrace_udp_header {
69
  char id[12];
70
  unsigned int pkt_number;
71
  unsigned int number_of_events;
72
  unsigned int total_size;
73
};
74
 
75
//Init UDP, if flag = 1 init the network driver
76
int FTrace_OSD_init_udp(int flag, char *l_ip, char *t_ip) {
77
 
78
  struct net_model m = net_base;
79
 
80
  strcpy(local_ip,l_ip);
81
  strcpy(target_ip,t_ip);
82
 
83
  if (flag) {
84
 
85
    net_setudpip(m, local_ip, "255.255.255.255");
86
 
87
    if (net_init(&m) != 1) {
88
      return -1;
89
    }
90
 
91
  }
92
 
93
  ip_str2addr(local_ip,&(local.s_addr));
94
  local.s_port = TRACER_PORT;
95
  socket = udp_bind(&local, NULL);
96
 
97
  ip_str2addr(target_ip,&(target.s_addr));
98
  target.s_port = TRACER_PORT;
99
 
100
  sleep(1);
101
 
102
  TracerUDPInit = 1;
103
 
104
  return 0;
105
 
106
}
107
 
108
int send_udp_event(void *p, int size) {
109
 
110
  static BYTE *current = pkt;
111
  static int events_number = 0;
112
  static int packet_number = 0;
113
 
114
  struct FTrace_udp_header head = {"TRACER-V1.0",0,0,0};
115
  struct FTrace_udp_header *phead = (struct FTrace_udp_header *)(pkt);
116
 
117
  int i;
118
 
119
  if ((total_pkt_size + size < UDP_MAXSIZE) && p != NULL ) {
120
 
121
    if (total_pkt_size == 0) {
122
 
123
      memcpy(pkt,&head,sizeof(struct FTrace_udp_header));
124
 
125
      current = pkt;
126
      current += sizeof(struct FTrace_udp_header);
127
 
128
      total_pkt_size += sizeof(struct FTrace_udp_header);
129
      phead->total_size = total_pkt_size;
130
 
131
      packet_number++;
132
      phead->pkt_number = packet_number;
133
 
134
    }
135
 
136
    events_number++;
137
    phead->number_of_events = events_number;
138
 
139
    total_pkt_size += size;
140
    phead->total_size = total_pkt_size;
141
 
142
    //Copy the event
143
    memcpy(current,p,size);
144
    current += size;
145
 
146
    return 0;
147
 
148
  } else {
149
 
150
    #ifdef TRACER_UDP_DEBUG
151
      cprintf("Packet Sent - Nr %d Size %d\n",packet_number,total_pkt_size);
152
    #endif
153
 
154
    if (TracerUDPInit == 1) {
155
      udp_sendto(socket, pkt, total_pkt_size, &target);
156
      for (i=0;i<DELAY_LOOP;i++);
157
    }
158
 
159
    events_number = 0;
160
    total_pkt_size = 0;
161
    current = pkt;
162
 
163
    if (p != NULL) send_udp_event(p,size);
164
 
165
    return 0;    
166
 
167
  }
168
 
169
  return -1;
170
 
171
}
172
 
173
//Sender Task
174
TASK udp_sender(void *arg)
175
{
176
  FTrace_Chunk_Ptr c = (FTrace_Chunk_Ptr)(arg);
177
 
178
  DWORD total_size = (DWORD)(c->size) + (DWORD)(c->emergency_size);
179
  DWORD start = (DWORD)(c->osd + FTRACE_OSD_CHUNK_HEAD);
180
  DWORD current = start;
181
  DWORD count = 0;  
182
 
183
  while (current+16 <= start+total_size) {
184
 
185
        if (*(DWORD *)(current) != 0) {
186
                send_udp_event((void *)(current), 16);
187
                count++;
188
        }
189
 
190
        current += 16;
191
 
192
  }
193
 
194
  // Flush the buffer out
195
  send_udp_event(NULL, 0);
196
 
552 giacomo 197
  sending_flag = 0;
198
 
551 giacomo 199
  #ifdef TRACER_UDP_DEBUG
200
    cprintf("Total Chunk Event Sent: %d\n",(int)count);
201
  #endif  
202
 
203
  return 0;  
204
 
205
}
206
 
207
int FTrace_OSD_create_udp_task(FTrace_Chunk_Ptr c)
208
{
209
 
210
  NRT_TASK_MODEL st;
211
 
552 giacomo 212
  sending_flag = 1;
213
 
551 giacomo 214
  nrt_task_default_model(st);
215
  nrt_task_def_arg(st,(void *)c);
216
 
217
  udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
218
  if (udp_task == NIL) {
219
    cprintf("Error creating UDP Sender\n");
220
    sys_end();
221
  }
222
 
223
  task_activate(udp_task);
224
 
225
  return 0;
226
 
227
}