3 /* transport - low level communication */
5 /* Copyright (c) 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. */
13 int gras_opt_trp_nomoredata_on_close=0;
18 #include "gras/Transport/transport_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp,gras,"Conveying bytes over the network");
21 XBT_LOG_NEW_SUBCATEGORY(gras_trp_meas,gras_trp,"Conveying bytes over the network without formating for perf measurements");
22 static short int _gras_trp_started = 0;
24 static xbt_dict_t _gras_trp_plugins; /* All registered plugins */
25 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
28 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
31 gras_trp_plugin_t plug = xbt_new0(s_gras_trp_plugin_t, 1);
33 DEBUG1("Create plugin %s",name);
35 plug->name=xbt_strdup(name);
40 if (e.category == mismatch_error) {
41 /* SG plugin raise mismatch when in RL mode (and vice versa) */
52 xbt_dict_set(_gras_trp_plugins, name, plug, gras_trp_plugin_free);
55 void gras_trp_init(void){
56 if (!_gras_trp_started) {
57 /* make room for all plugins */
58 _gras_trp_plugins=xbt_dict_new();
60 #ifdef HAVE_WINSOCK2_H
61 /* initialize the windows mechanism */
63 WORD wVersionRequested;
66 wVersionRequested = MAKEWORD( 2, 0 );
67 xbt_assert0(WSAStartup( wVersionRequested, &wsaData ) == 0,
68 "Cannot find a usable WinSock DLL");
70 /* Confirm that the WinSock DLL supports 2.0.*/
71 /* Note that if the DLL supports versions greater */
72 /* than 2.0 in addition to 2.0, it will still return */
73 /* 2.0 in wVersion since that is the version we */
76 xbt_assert0(LOBYTE( wsaData.wVersion ) == 2 &&
77 HIBYTE( wsaData.wVersion ) == 0,
78 "Cannot find a usable WinSock DLL");
79 INFO0("Found and initialized winsock2");
80 } /* The WinSock DLL is acceptable. Proceed. */
83 xbt_assert0(WSAStartup( 0x0101, &wsaData ) == 0,
84 "Cannot find a usable WinSock DLL");
85 INFO0("Found and initialized winsock");
90 gras_trp_plugin_new("file",gras_trp_file_setup);
91 gras_trp_plugin_new("sg",gras_trp_sg_setup);
92 gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
100 DEBUG1("gras_trp value %d",_gras_trp_started);
101 if (_gras_trp_started == 0) {
105 if ( --_gras_trp_started == 0 ) {
106 #ifdef HAVE_WINSOCK_H
107 if ( WSACleanup() == SOCKET_ERROR ) {
108 if ( WSAGetLastError() == WSAEINPROGRESS ) {
109 WSACancelBlockingCall();
115 /* Delete the plugins */
116 xbt_dict_free(&_gras_trp_plugins);
121 void gras_trp_plugin_free(void *p) {
122 gras_trp_plugin_t plug = p;
127 } else if (plug->data) {
128 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
139 * gras_trp_socket_new:
141 * Malloc a new socket, and initialize it with defaults
143 void gras_trp_socket_new(int incoming,
144 gras_socket_t *dst) {
146 gras_socket_t sock=xbt_new0(s_gras_socket_t,1);
148 VERB1("Create a new socket (%p)", (void*)sock);
152 sock->incoming = incoming ? 1:0;
153 sock->outgoing = incoming ? 0:1;
154 sock->accepting = incoming ? 1:0;
162 sock->peer_port = -1;
163 sock->peer_name = NULL;
164 sock->peer_proc = NULL;
167 sock->bufdata = NULL;
171 xbt_dynar_push(((gras_trp_procdata_t)
172 gras_libdata_by_id(gras_trp_libdata_id))->sockets,dst);
177 * @brief Opens a server socket and makes it ready to be listened to.
178 * @param port: port on which you want to listen
179 * @param buf_size: size of the buffer (in byte) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change)
180 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
182 * In real life, you'll get a TCP socket.
185 gras_socket_server_ext(unsigned short port,
187 unsigned long int buf_size,
191 gras_trp_plugin_t trp;
194 DEBUG2("Create a server socket from plugin %s on port %d",
195 gras_if_RL() ? "tcp" : "sg",
197 trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp");
199 /* defaults settings */
200 gras_trp_socket_new(1,&sock);
203 sock->buf_size = buf_size>0 ? buf_size : 32*1024;
204 sock->meas = measurement;
206 /* Call plugin socket creation function */
207 DEBUG1("Prepare socket with plugin (fct=%p)",trp->socket_server);
209 trp->socket_server(trp, sock);
210 DEBUG3("in=%c out=%c accept=%c",
211 sock->incoming?'y':'n',
212 sock->outgoing?'y':'n',
213 sock->accepting?'y':'n');
216 gras_socket_t sock_iter;
217 xbt_dynar_t socks = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
218 xbt_dynar_foreach(socks, cursor, sock_iter) {
219 if (sock_iter==sock) {
220 xbt_dynar_cursor_rm(socks,&cursor);
228 ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport = port;
232 * @brief Opens a server socket on any port in the given range
234 * @param minport: first port we will try
235 * @param maxport: last port we will try
236 * @param buf_size: size of the buffer (in byte) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change)
237 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
239 * If none of the provided ports works, raises the exception got when trying the last possibility
242 gras_socket_server_range(unsigned short minport, unsigned short maxport,
243 unsigned long int buf_size, int measurement) {
246 gras_socket_t res=NULL;
249 for (port=minport; port<maxport;port ++) {
251 res=gras_socket_server_ext(port,buf_size,measurement);
264 * @brief Opens a client socket to a remote host.
265 * @param host: who you want to connect to
266 * @param port: where you want to connect to on this host
267 * @param buf_size: size of the buffer (in bytes) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change)
268 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
270 * In real life, you'll get a TCP socket.
273 gras_socket_client_ext(const char *host,
276 unsigned long int buf_size,
280 gras_trp_plugin_t trp;
283 trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp");
285 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
286 /* defaults settings */
287 gras_trp_socket_new(0,&sock);
289 sock->peer_port = port;
290 sock->peer_name = (char*)strdup(host?host:"localhost");
291 sock->buf_size = buf_size>0 ? buf_size: 32*1024;
292 sock->meas = measurement;
294 /* plugin-specific */
296 (*trp->socket_client)(trp, sock);
297 DEBUG3("in=%c out=%c accept=%c",
298 sock->incoming?'y':'n',
299 sock->outgoing?'y':'n',
300 sock->accepting?'y':'n');
302 xbt_dynar_pop(((gras_trp_procdata_t)
303 gras_libdata_by_id(gras_trp_libdata_id))->sockets,NULL);
312 * gras_socket_server:
314 * Opens a server socket and make it ready to be listened to.
315 * In real life, you'll get a TCP socket.
318 gras_socket_server(unsigned short port) {
319 return gras_socket_server_ext(port,32*1024,0);
322 /** @brief Opens a client socket to a remote host */
324 gras_socket_client(const char *host,
325 unsigned short port) {
326 return gras_socket_client_ext(host,port,0,0);
329 /** @brief Opens a client socket to a remote host specified as '\a host:\a port' */
331 gras_socket_client_from_string(const char *host) {
332 xbt_peer_t p = xbt_peer_from_string(host);
333 gras_socket_t res = gras_socket_client_ext(p->name,p->port,0,0);
338 /** \brief Close socket */
339 void gras_socket_close(gras_socket_t sock) {
340 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
341 gras_socket_t sock_iter;
345 VERB1("Close %p",sock);
346 if (sock == _gras_lastly_selected_socket) {
347 xbt_assert0(!gras_opt_trp_nomoredata_on_close || !sock->moredata,
348 "Closing a socket having more data in buffer while the nomoredata_on_close option is activated");
351 CRITICAL0("Closing a socket having more data in buffer. Option nomoredata_on_close disabled, so continuing.");
352 _gras_lastly_selected_socket=NULL;
355 /* FIXME: Issue an event when the socket is closed */
356 DEBUG1("sockets pointer before %p",sockets);
358 xbt_dynar_foreach(sockets,cursor,sock_iter) {
359 if (sock == sock_iter) {
360 DEBUG2("remove sock cursor %d dize %lu\n",cursor,xbt_dynar_length(sockets));
361 xbt_dynar_cursor_rm(sockets,&cursor);
362 if (sock->plugin->socket_close)
363 (* sock->plugin->socket_close)(sock);
365 /* free the memory */
367 free(sock->peer_name);
370 xbt_dynar_cursor_unlock(sockets);
374 WARN1("Ignoring request to free an unknown socket (%p). Execution stack:",sock);
375 xbt_backtrace_display();
383 * Send a bunch of bytes from on socket
384 * (stable if we know the storage will keep as is until the next trp_flush)
387 gras_trp_send(gras_socket_t sd, char *data, long int size, int stable) {
388 xbt_assert0(sd->outgoing,"Socket not suited for data send");
389 (*sd->plugin->send)(sd,data,size,stable);
392 * gras_trp_chunk_recv:
394 * Receive a bunch of bytes from a socket
397 gras_trp_recv(gras_socket_t sd, char *data, long int size) {
398 xbt_assert0(sd->incoming,"Socket not suited for data receive");
399 (sd->plugin->recv)(sd,data,size);
405 * Make sure all pending communications are done
408 gras_trp_flush(gras_socket_t sd) {
409 if (sd->plugin->flush)
410 (sd->plugin->flush)(sd);
414 gras_trp_plugin_get_by_name(const char *name){
415 return xbt_dict_get(_gras_trp_plugins,name);
418 int gras_socket_my_port (gras_socket_t sock) {
421 int gras_socket_peer_port(gras_socket_t sock) {
422 return sock->peer_port;
424 char *gras_socket_peer_name(gras_socket_t sock) {
425 return sock->peer_name;
427 char *gras_socket_peer_proc(gras_socket_t sock) {
428 return sock->peer_proc;
431 void gras_socket_peer_proc_set(gras_socket_t sock,char*peer_proc) {
432 sock->peer_proc = peer_proc;
435 /** \brief Check if the provided socket is a measurement one (or a regular one) */
436 int gras_socket_is_meas(gras_socket_t sock) {
440 /** \brief Send a chunk of (random) data over a measurement socket
442 * @param peer measurement socket to use for the experiment
443 * @param timeout timeout (in seconds)
444 * @param msg_size size of each chunk sent over the socket (in bytes).
445 * @param msg_amount how many of these packets you want to send.
447 * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on
448 * each side of the socket should be paired.
450 * The exchanged data is zeroed to make sure it's initialized, but
451 * there is no way to control what is sent (ie, you cannot use these
452 * functions to exchange data out of band).
454 * @warning: in SimGrid version 3.1 and previous, the numerical arguments
455 * were the total amount of data to send and the msg_size. This
456 * was changed for the fool wanting to send more than MAXINT
457 * bytes in a fat pipe.
459 void gras_socket_meas_send(gras_socket_t peer,
460 unsigned int timeout,
461 unsigned long int msg_size,
462 unsigned long int msg_amount) {
464 unsigned long int sent_sofar;
469 chunk=xbt_malloc0(msg_size);
471 xbt_assert0(peer->meas,"Asked to send measurement data on a regular socket");
472 xbt_assert0(peer->outgoing,"Socket not suited for data send (was created with gras_socket_server(), not gras_socket_client())");
474 for (sent_sofar=0; sent_sofar < msg_amount; sent_sofar++) {
475 CDEBUG5(gras_trp_meas,"Sent %lu msgs of %lu (size of each: %ld) to %s:%d",
476 sent_sofar,msg_amount,msg_size,
477 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
478 (*peer->plugin->raw_send)(peer,chunk,msg_size);
480 CDEBUG5(gras_trp_meas,"Sent %lu msgs of %lu (size of each: %ld) to %s:%d",
481 sent_sofar,msg_amount,msg_size,
482 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
490 /** \brief Receive a chunk of data over a measurement socket
492 * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on
493 * each side of the socket should be paired.
495 * @warning: in SimGrid version 3.1 and previous, the numerical arguments
496 * were the total amount of data to send and the msg_size. This
497 * was changed for the fool wanting to send more than MAXINT
498 * bytes in a fat pipe.
500 void gras_socket_meas_recv(gras_socket_t peer,
501 unsigned int timeout,
502 unsigned long int msg_size,
503 unsigned long int msg_amount){
506 unsigned long int got_sofar;
511 chunk = xbt_malloc(msg_size);
513 xbt_assert0(peer->meas,
514 "Asked to receive measurement data on a regular socket");
515 xbt_assert0(peer->incoming,"Socket not suited for data receive");
517 for (got_sofar=0; got_sofar < msg_amount; got_sofar ++) {
518 CDEBUG5(gras_trp_meas,"Recvd %ld msgs of %lu (size of each: %ld) from %s:%d",
519 got_sofar,msg_amount,msg_size,
520 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
521 (peer->plugin->raw_recv)(peer,chunk,msg_size);
523 CDEBUG5(gras_trp_meas,"Recvd %ld msgs of %lu (size of each: %ld) from %s:%d",
524 got_sofar,msg_amount,msg_size,
525 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
533 * \brief Something similar to the good old accept system call.
535 * Make sure that there is someone speaking to the provided server socket.
536 * In RL, it does an accept(2) and return the result as last argument.
537 * In SG, as accepts are useless, it returns the provided argument as result.
538 * You should thus test whether (peer != accepted) before closing both of them.
540 * You should only call this on measurement sockets. It is automatically
541 * done for regular sockets, but you usually want more control about
542 * what's going on with measurement sockets.
544 gras_socket_t gras_socket_meas_accept(gras_socket_t peer){
547 xbt_assert0(peer->meas,
548 "No need to accept on non-measurement sockets (it's automatic)");
550 if (!peer->accepting) {
551 /* nothing to accept here (must be in SG) */
552 /* BUG: FIXME: this is BAD! it makes tricky to free the accepted socket*/
556 res = (peer->plugin->socket_accept)(peer);
557 res->meas = peer->meas;
558 CDEBUG1(gras_trp_meas,"meas_accepted onto %d",res->sd);
565 * Creating procdata for this module
567 static void *gras_trp_procdata_new() {
568 gras_trp_procdata_t res = xbt_new(s_gras_trp_procdata_t,1);
570 res->name = xbt_strdup("gras_trp");
572 res->sockets = xbt_dynar_new_sync(sizeof(gras_socket_t*), NULL);
579 * Freeing procdata for this module
581 static void gras_trp_procdata_free(void *data) {
582 gras_trp_procdata_t res = (gras_trp_procdata_t)data;
584 xbt_dynar_free(&( res->sockets ));
589 void gras_trp_socketset_dump(const char *name) {
590 gras_trp_procdata_t procdata =
591 (gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
596 INFO1("** Dump the socket set %s",name);
597 xbt_dynar_foreach(procdata->sockets, it, s) {
598 INFO4(" %p -> %s:%d %s",
599 s,gras_socket_peer_name(s),gras_socket_peer_port(s),
600 s->valid?"(valid)":"(peer dead)");
602 INFO1("** End of socket set %s",name);
606 * Module registration
608 int gras_trp_libdata_id;
609 void gras_trp_register() {
610 gras_trp_libdata_id = gras_procdata_add("gras_trp",gras_trp_procdata_new, gras_trp_procdata_free);
613 int gras_os_myport(void) {
614 return ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport;