Subversion Repositories shark

Rev

Rev 561 | Rev 907 | 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
 
778 giacomo 54
#define DELAY_USEC 5000
551 giacomo 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
  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
778 giacomo 149
      cprintf(".");
551 giacomo 150
    #endif
151
 
152
    if (TracerUDPInit == 1) {
153
      udp_sendto(socket, pkt, total_pkt_size, &target);
778 giacomo 154
      usleep(DELAY_USEC);
551 giacomo 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
 
561 giacomo 183
        if (*(WORD *)(current) != 0) {
551 giacomo 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
 
552 giacomo 195
  sending_flag = 0;
196
 
551 giacomo 197
  #ifdef TRACER_UDP_DEBUG
778 giacomo 198
    cprintf("\nTotal Chunk Event Sent: %d\n",(int)count);
551 giacomo 199
  #endif  
200
 
201
  return 0;  
202
 
203
}
204
 
205
int FTrace_OSD_create_udp_task(FTrace_Chunk_Ptr c)
206
{
207
 
208
  NRT_TASK_MODEL st;
209
 
552 giacomo 210
  sending_flag = 1;
211
 
551 giacomo 212
  nrt_task_default_model(st);
213
  nrt_task_def_arg(st,(void *)c);
214
 
215
  udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
216
  if (udp_task == NIL) {
217
    cprintf("Error creating UDP Sender\n");
218
    sys_end();
219
  }
220
 
221
  task_activate(udp_task);
222
 
223
  return 0;
224
 
225
}