3 /* amok_saturate - Link saturating facilities (for ALNeM's BW testing) */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003, 2004 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "amok/Bandwidth/bandwidth_private.h"
13 XBT_LOG_EXTERNAL_CATEGORY(bw);
14 XBT_LOG_DEFAULT_CATEGORY(bw);
17 /* ***************************************************************************
19 * ***************************************************************************/
21 xbt_error_t grasbw_saturate_start(const char* from_name,unsigned int from_port,
22 const char* to_name,unsigned int to_port,
23 unsigned int msgSize, unsigned int timeout) {
32 if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
33 fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while contacting peer\n",
34 __FILE__,__LINE__,xbt_error_name(errcode));
37 if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t))) ||
38 !(target=(msgHost_t*)malloc(sizeof(msgHost_t)))) {
39 fprintf(stderr,"%s:%d:saturate_start(): Malloc error\n",__FILE__,__LINE__);
40 gras_sock_close(sock);
44 request->timeout=timeout;
45 request->msgSize=msgSize;
47 strcpy(target->host,to_name);
50 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_START, 2,
53 fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while sending the request.\n",
54 __FILE__,__LINE__,xbt_error_name(errcode));
55 gras_sock_close(sock);
58 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STARTED,&answer))) {
59 fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while waiting for the ACK.\n",
60 __FILE__,__LINE__,xbt_error_name(errcode));
61 gras_sock_close(sock);
65 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
66 fprintf(stderr,"%s:%d:saturate_start(): Peer reported error %s (%s).\n",
67 __FILE__,__LINE__,xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
68 gras_msg_free(answer);
69 gras_sock_close(sock);
73 gras_msg_free(answer);
74 gras_sock_close(sock);
78 int grasbw_cbSatStart(gras_msg_t *msg) {
82 double start; /* time to timeout */
84 /* specification of the test to run */
85 char* to_name=gras_msg_ctn(msg,0,0,msgHost_t).host;
86 unsigned int to_port=gras_msg_ctn(msg,0,0,msgHost_t).port;
88 unsigned int msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
89 unsigned int timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
90 unsigned int raw_port;
98 fprintf(stderr,"grasbw_cbSatStart(sd=%p)\n",msg->sock);
99 fprintf(stderr,"(server=%d,raw=%d,fromPID=%d,toPID=%d,toHost=%p,toPort=%d,toChan=%d)\n",
100 msg->sock->server_sock,msg->sock->raw_sock,msg->sock->from_PID,
101 msg->sock->to_PID,msg->sock->to_host,msg->sock->to_port,msg->sock->to_chan);
104 /* Negociate the saturation with the peer */
105 if((errcode=gras_sock_client_open(to_name,to_port,&sock))) {
106 fprintf(stderr,"cbSatStart(): Error %s encountered while contacting peer\n",
107 xbt_error_name(errcode));
108 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
109 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
110 errcode,"Cannot contact peer.\n");
113 if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t)))) {
114 fprintf(stderr,"cbSatStart(): Malloc error\n");
115 gras_sock_close(sock);
116 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
117 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
118 malloc_error,"Cannot build request.\n");
122 request->timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
123 request->msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
125 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_BEGIN, 1,
127 fprintf(stderr,"cbSatStart(): Error %s encountered while sending the request.\n",
128 xbt_error_name(errcode));
129 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
130 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
131 errcode,"Cannot send request.\n");
132 gras_sock_close(sock);
136 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_BEGUN,&answer))) {
137 fprintf(stderr,"cbSatStart(): Error %s encountered while waiting for the ACK.\n",
138 xbt_error_name(errcode));
139 gras_sock_close(sock);
141 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
142 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
144 "Cannot receive the ACK.\n");
148 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
149 fprintf(stderr,"cbSatStart(): Peer reported error %s (%s).\n",
150 xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
152 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
153 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
155 "Peer repported '%s'.\n",gras_msg_ctn(answer,0,0,msgError_t).errmsg);
156 gras_msg_free(answer);
157 gras_sock_close(sock);
161 raw_port=gras_msg_ctn(answer,1,0,SatExp_t).port;
163 if ((errcode=gras_rawsock_client_open(to_name,raw_port,msgSize,&raw))) {
164 fprintf(stderr,"cbSatStart(): Error %s while opening raw socket to %s:%d.\n",
165 xbt_error_name(errcode),to_name,gras_msg_ctn(answer,1,0,SatExp_t).port);
167 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
168 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
169 errcode,"Cannot open raw socket.\n");
170 gras_sock_close(sock);
174 /* send a train of data before repporting that XP is started */
175 if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
176 fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
177 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
178 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
179 errcode,"Cannot raw send.\n");
180 gras_sock_close(sock);
181 gras_rawsock_close(raw);
185 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
186 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
187 no_error,"Saturation started");
188 gras_msg_free(answer);
191 /* Do the saturation until we get a SAT_STOP message or until we timeout the whole XP*/
193 while (gras_msg_wait(0,GRASMSG_SAT_STOP,&msg)==timeout_error &&
194 gras_time()-start < timeout) {
195 if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
196 fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
197 /* our error message do not interess anyone. SAT_STOP will do nothing. */
198 gras_sock_close(sock);
199 gras_rawsock_close(raw);
203 if (gras_time()-start > timeout) {
204 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW\n");
205 gras_sock_close(sock);
206 gras_rawsock_close(raw);
210 /* Handle the SAT_STOP which broke the previous while */
212 if ((errcode=gras_msg_new_and_send(sock, GRASMSG_SAT_END,0))) {
213 fprintf(stderr,"cbSatStart(): Cannot tell peer to stop saturation\n");
215 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
216 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
217 errcode,"Sending SAT_END to peer failed.\n");
218 gras_sock_close(sock);
219 gras_rawsock_close(raw);
223 if ((errcode=gras_msg_wait(60,GRASMSG_SAT_ENDED,&answer))) {
224 fprintf(stderr,"cbSatStart(): Peer didn't ACK the end\n");
226 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
227 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
228 errcode,"Receiving SAT_ENDED from peer failed.\n");
229 gras_sock_close(sock);
230 gras_rawsock_close(raw);
233 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
234 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
237 gras_sock_close(sock);
238 gras_rawsock_close(raw);
239 gras_msg_free(answer);
245 int grasbw_cbSatBegin(gras_msg_t *msg) {
248 double start; /* timer */
250 unsigned int msgSize=gras_msg_ctn(msg,0,0,SatExp_t).msgSize;
251 unsigned int timeout=gras_msg_ctn(msg,0,0,SatExp_t).timeout;
256 if (!(request=(SatExp_t*)malloc(sizeof(SatExp_t))) ||
257 !(error=(msgError_t *)malloc(sizeof(msgError_t)))) {
258 fprintf(stderr,"cbSatBegin(): Malloc error\n");
259 grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
260 "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
261 malloc_error,"Malloc error");
265 if ((errcode=gras_rawsock_server_open(6666,8000,msgSize,&raw))) {
266 fprintf(stderr,"cbSatBegin(): Error %s encountered while opening a raw socket\n",
267 xbt_error_name(errcode));
268 grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
269 "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
270 errcode,"Cannot open raw socket");
273 request->port=gras_rawsock_get_peer_port(raw);
274 request->msgSize=msgSize;
275 error->errcode=no_error;
276 error->errmsg[0]='\0';
277 if ((errcode=gras_msg_new_and_send(msg->sock,GRASMSG_SAT_BEGUN,2,
280 fprintf(stderr,"cbSatBegin(): Error %s encountered while send ACK to peer\n",
281 xbt_error_name(errcode));
287 while (gras_msg_wait(0,GRASMSG_SAT_END,&msg)==timeout_error &&
288 gras_time() - start < timeout) {
289 errcode=gras_rawsock_recv(raw,msgSize,msgSize,1);
290 if (errcode != timeout_error && errcode != no_error) {
291 fprintf(stderr,"cbSatBegin: Failure %s during raw receive\n",xbt_error_name(errcode));
292 /* our error message do not interess anyone. SAT_END will do nothing. */
293 /* (if timeout'ed, it may be because the sender stopped emission. so survive it) */
297 if (gras_time()-start > timeout) {
298 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW.\n");
299 gras_rawsock_close(raw);
303 grasRepportError(msg->sock,GRASMSG_SAT_ENDED,1,
304 "cbSatBegin: Cannot send SAT_ENDED.\n",
306 gras_rawsock_close(raw);
311 xbt_error_t grasbw_saturate_stop(const char* from_name,unsigned int from_port,
312 const char* to_name,unsigned int to_port) {
317 if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
318 fprintf(stderr,"saturate_stop(): Error %s encountered while contacting peer\n",
319 xbt_error_name(errcode));
323 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_STOP,0))) {
324 fprintf(stderr,"saturate_stop(): Error %s encountered while sending request\n",
325 xbt_error_name(errcode));
326 gras_sock_close(sock);
330 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STOPPED,&answer))) {
331 fprintf(stderr,"saturate_stop(): Error %s encountered while receiving ACK\n",
332 xbt_error_name(errcode));
333 gras_sock_close(sock);
337 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
338 fprintf(stderr,"saturate_stop(): Peer reported error %s (%s).\n",
339 xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
340 gras_msg_free(answer);
341 gras_sock_close(sock);
345 gras_msg_free(answer);
346 gras_sock_close(sock);