3 /* amok_saturate - Link saturating facilities (for ALNeM's BW testing) */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "amok/Bandwidth/bandwidth_private.h"
13 XBT_LOG_EXTERNAL_CATEGORY(bw);
14 XBT_LOG_DEFAULT_CATEGORY(bw);
16 /* ***************************************************************************
18 * ***************************************************************************/
20 xbt_error_t grasbw_saturate_start(const char* from_name,unsigned int from_port,
21 const char* to_name,unsigned int to_port,
22 unsigned int msgSize, unsigned int timeout) {
31 if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
32 fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while contacting peer\n",
33 __FILE__,__LINE__,xbt_error_name(errcode));
36 if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t))) ||
37 !(target=(msgHost_t*)malloc(sizeof(msgHost_t)))) {
38 fprintf(stderr,"%s:%d:saturate_start(): Malloc error\n",__FILE__,__LINE__);
39 gras_sock_close(sock);
43 request->timeout=timeout;
44 request->msgSize=msgSize;
46 strcpy(target->host,to_name);
49 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_START, 2,
52 fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while sending the request.\n",
53 __FILE__,__LINE__,xbt_error_name(errcode));
54 gras_sock_close(sock);
57 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STARTED,&answer))) {
58 fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while waiting for the ACK.\n",
59 __FILE__,__LINE__,xbt_error_name(errcode));
60 gras_sock_close(sock);
64 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
65 fprintf(stderr,"%s:%d:saturate_start(): Peer reported error %s (%s).\n",
66 __FILE__,__LINE__,xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
67 gras_msg_free(answer);
68 gras_sock_close(sock);
72 gras_msg_free(answer);
73 gras_sock_close(sock);
77 int grasbw_cbSatStart(gras_msg_t *msg) {
81 double start; /* time to timeout */
83 /* specification of the test to run */
84 char* to_name=gras_msg_ctn(msg,0,0,msgHost_t).host;
85 unsigned int to_port=gras_msg_ctn(msg,0,0,msgHost_t).port;
87 unsigned int msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
88 unsigned int timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
89 unsigned int raw_port;
97 fprintf(stderr,"grasbw_cbSatStart(sd=%p)\n",msg->sock);
98 fprintf(stderr,"(server=%d,raw=%d,fromPID=%d,toPID=%d,toHost=%p,toPort=%d,toChan=%d)\n",
99 msg->sock->server_sock,msg->sock->raw_sock,msg->sock->from_PID,
100 msg->sock->to_PID,msg->sock->to_host,msg->sock->to_port,msg->sock->to_chan);
103 /* Negociate the saturation with the peer */
104 if((errcode=gras_sock_client_open(to_name,to_port,&sock))) {
105 fprintf(stderr,"cbSatStart(): Error %s encountered while contacting peer\n",
106 xbt_error_name(errcode));
107 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
108 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
109 errcode,"Cannot contact peer.\n");
112 if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t)))) {
113 fprintf(stderr,"cbSatStart(): Malloc error\n");
114 gras_sock_close(sock);
115 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
116 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
117 malloc_error,"Cannot build request.\n");
121 request->timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
122 request->msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
124 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_BEGIN, 1,
126 fprintf(stderr,"cbSatStart(): Error %s encountered while sending the request.\n",
127 xbt_error_name(errcode));
128 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
129 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
130 errcode,"Cannot send request.\n");
131 gras_sock_close(sock);
135 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_BEGUN,&answer))) {
136 fprintf(stderr,"cbSatStart(): Error %s encountered while waiting for the ACK.\n",
137 xbt_error_name(errcode));
138 gras_sock_close(sock);
140 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
141 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
143 "Cannot receive the ACK.\n");
147 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
148 fprintf(stderr,"cbSatStart(): Peer reported error %s (%s).\n",
149 xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
151 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
152 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
154 "Peer repported '%s'.\n",gras_msg_ctn(answer,0,0,msgError_t).errmsg);
155 gras_msg_free(answer);
156 gras_sock_close(sock);
160 raw_port=gras_msg_ctn(answer,1,0,SatExp_t).port;
162 if ((errcode=gras_rawsock_client_open(to_name,raw_port,msgSize,&raw))) {
163 fprintf(stderr,"cbSatStart(): Error %s while opening raw socket to %s:%d.\n",
164 xbt_error_name(errcode),to_name,gras_msg_ctn(answer,1,0,SatExp_t).port);
166 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
167 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
168 errcode,"Cannot open raw socket.\n");
169 gras_sock_close(sock);
173 /* send a train of data before repporting that XP is started */
174 if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
175 fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
176 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
177 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
178 errcode,"Cannot raw send.\n");
179 gras_sock_close(sock);
180 gras_rawsock_close(raw);
184 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
185 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
186 no_error,"Saturation started");
187 gras_msg_free(answer);
190 /* Do the saturation until we get a SAT_STOP message or until we timeout the whole XP*/
192 while (gras_msg_wait(0,GRASMSG_SAT_STOP,&msg)==timeout_error &&
193 gras_time()-start < timeout) {
194 if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
195 fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
196 /* our error message do not interess anyone. SAT_STOP will do nothing. */
197 gras_sock_close(sock);
198 gras_rawsock_close(raw);
202 if (gras_time()-start > timeout) {
203 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW\n");
204 gras_sock_close(sock);
205 gras_rawsock_close(raw);
209 /* Handle the SAT_STOP which broke the previous while */
211 if ((errcode=gras_msg_new_and_send(sock, GRASMSG_SAT_END,0))) {
212 fprintf(stderr,"cbSatStart(): Cannot tell peer to stop saturation\n");
214 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
215 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
216 errcode,"Sending SAT_END to peer failed.\n");
217 gras_sock_close(sock);
218 gras_rawsock_close(raw);
222 if ((errcode=gras_msg_wait(60,GRASMSG_SAT_ENDED,&answer))) {
223 fprintf(stderr,"cbSatStart(): Peer didn't ACK the end\n");
225 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
226 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
227 errcode,"Receiving SAT_ENDED from peer failed.\n");
228 gras_sock_close(sock);
229 gras_rawsock_close(raw);
232 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
233 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
236 gras_sock_close(sock);
237 gras_rawsock_close(raw);
238 gras_msg_free(answer);
244 int grasbw_cbSatBegin(gras_msg_t *msg) {
247 double start; /* timer */
249 unsigned int msgSize=gras_msg_ctn(msg,0,0,SatExp_t).msgSize;
250 unsigned int timeout=gras_msg_ctn(msg,0,0,SatExp_t).timeout;
255 if (!(request=(SatExp_t*)malloc(sizeof(SatExp_t))) ||
256 !(error=(msgError_t *)malloc(sizeof(msgError_t)))) {
257 fprintf(stderr,"cbSatBegin(): Malloc error\n");
258 grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
259 "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
260 malloc_error,"Malloc error");
264 if ((errcode=gras_rawsock_server_open(6666,8000,msgSize,&raw))) {
265 fprintf(stderr,"cbSatBegin(): Error %s encountered while opening a raw socket\n",
266 xbt_error_name(errcode));
267 grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
268 "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
269 errcode,"Cannot open raw socket");
272 request->port=gras_rawsock_get_peer_port(raw);
273 request->msgSize=msgSize;
274 error->errcode=no_error;
275 error->errmsg[0]='\0';
276 if ((errcode=gras_msg_new_and_send(msg->sock,GRASMSG_SAT_BEGUN,2,
279 fprintf(stderr,"cbSatBegin(): Error %s encountered while send ACK to peer\n",
280 xbt_error_name(errcode));
286 while (gras_msg_wait(0,GRASMSG_SAT_END,&msg)==timeout_error &&
287 gras_time() - start < timeout) {
288 errcode=gras_rawsock_recv(raw,msgSize,msgSize,1);
289 if (errcode != timeout_error && errcode != no_error) {
290 fprintf(stderr,"cbSatBegin: Failure %s during raw receive\n",xbt_error_name(errcode));
291 /* our error message do not interess anyone. SAT_END will do nothing. */
292 /* (if timeout'ed, it may be because the sender stopped emission. so survive it) */
296 if (gras_time()-start > timeout) {
297 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW.\n");
298 gras_rawsock_close(raw);
302 grasRepportError(msg->sock,GRASMSG_SAT_ENDED,1,
303 "cbSatBegin: Cannot send SAT_ENDED.\n",
305 gras_rawsock_close(raw);
310 xbt_error_t grasbw_saturate_stop(const char* from_name,unsigned int from_port,
311 const char* to_name,unsigned int to_port) {
316 if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
317 fprintf(stderr,"saturate_stop(): Error %s encountered while contacting peer\n",
318 xbt_error_name(errcode));
322 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_STOP,0))) {
323 fprintf(stderr,"saturate_stop(): Error %s encountered while sending request\n",
324 xbt_error_name(errcode));
325 gras_sock_close(sock);
329 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STOPPED,&answer))) {
330 fprintf(stderr,"saturate_stop(): Error %s encountered while receiving ACK\n",
331 xbt_error_name(errcode));
332 gras_sock_close(sock);
336 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
337 fprintf(stderr,"saturate_stop(): Peer reported error %s (%s).\n",
338 xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
339 gras_msg_free(answer);
340 gras_sock_close(sock);
344 gras_msg_free(answer);
345 gras_sock_close(sock);