1 /* buf trp (transport) - buffered transport using the TCP one */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <string.h> /* memset */
14 #include "xbt/sysdep.h"
16 #include "gras/Transport/transport_private.h"
17 #include "gras/Msg/msg_interface.h" /* listener_close_socket */
19 /* FIXME maybe READV is sometime a good thing? */
27 #define MIN(a,b) ((a)<(b)?(a):(b))
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_tcp, gras_trp,
31 "TCP buffered transport");
34 *** Specific socket part
37 typedef enum { buffering_buf, buffering_iov } buffering_kind;
42 int pos; /* for receive; not exchanged over the net */
46 struct gras_trp_bufdata_ {
48 gras_trp_buf_t in_buf;
49 gras_trp_buf_t out_buf;
53 xbt_dynar_t out_buf_v;
61 /*****************************/
62 /****[ SOCKET MANAGEMENT ]****/
63 /*****************************/
64 /* we exchange port number on client side on socket creation,
65 so we need to be able to talk right now. */
66 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, const char *data,
67 unsigned long int size);
68 static int gras_trp_tcp_recv(gras_socket_t sock, char *data,
69 unsigned long int size);
72 static int _gras_tcp_proto_number(void);
74 static XBT_INLINE void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
78 struct sockaddr_in addr;
80 struct in_addr *haddr;
81 int size = sock->buf_size;
82 uint32_t myport = htonl(((gras_trp_procdata_t)
83 gras_libdata_by_id(gras_trp_libdata_id))->myport);
85 sock->incoming = 1; /* TCP sockets are duplex'ed */
87 sock->sd = socket(AF_INET, SOCK_STREAM, 0);
90 THROW1(system_error, 0, "Failed to create socket: %s",
91 sock_errstr(sock_errno));
95 (sock->sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size))
96 || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *) &size,
98 VERB1("setsockopt failed, cannot set buffer size: %s",
99 sock_errstr(sock_errno));
102 he = gethostbyname(sock->peer_name);
104 THROW2(system_error, 0, "Failed to lookup hostname %s: %s",
105 sock->peer_name, sock_errstr(sock_errno));
108 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
110 memset(&addr, 0, sizeof(struct sockaddr_in));
111 memcpy(&addr.sin_addr, haddr, sizeof(struct in_addr));
112 addr.sin_family = AF_INET;
113 addr.sin_port = htons(sock->peer_port);
115 if (connect(sock->sd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
117 THROW3(system_error, 0,
118 "Failed to connect socket to %s:%d (%s)",
119 sock->peer_name, sock->peer_port, sock_errstr(sock_errno));
122 gras_trp_tcp_send(sock, (char *) &myport, sizeof(uint32_t));
123 DEBUG1("peerport sent to %d", sock->peer_port);
125 VERB4("Connect to %s:%d (sd=%d, port %d here)",
126 sock->peer_name, sock->peer_port, sock->sd, sock->port);
130 * gras_trp_sock_socket_server:
132 * Open a socket used to receive messages.
134 static XBT_INLINE void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
137 int size = sock->buf_size;
139 struct sockaddr_in server;
141 sock->outgoing = 1; /* TCP => duplex mode */
143 server.sin_port = htons((u_short) sock->port);
144 server.sin_addr.s_addr = INADDR_ANY;
145 server.sin_family = AF_INET;
146 if ((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
147 THROW1(system_error, 0, "Socket allocation failed: %s",
148 sock_errstr(sock_errno));
151 (sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)))
152 THROW1(system_error, 0,
153 "setsockopt failed, cannot condition the socket: %s",
154 sock_errstr(sock_errno));
156 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF,
157 (char *) &size, sizeof(size))
158 || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF,
159 (char *) &size, sizeof(size))) {
160 VERB1("setsockopt failed, cannot set buffer size: %s",
161 sock_errstr(sock_errno));
164 if (bind(sock->sd, (struct sockaddr *) &server, sizeof(server)) == -1) {
166 THROW2(system_error, 0,
167 "Cannot bind to port %d: %s", sock->port, sock_errstr(sock_errno));
170 DEBUG2("Listen on port %d (sd=%d)", sock->port, sock->sd);
171 if (listen(sock->sd, 5) < 0) {
173 THROW2(system_error, 0,
174 "Cannot listen on port %d: %s",
175 sock->port, sock_errstr(sock_errno));
178 VERB2("Openned a server socket on port %d (sd=%d)", sock->port, sock->sd);
181 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock)
185 struct sockaddr_in peer_in;
186 socklen_t peer_in_len = sizeof(peer_in);
193 socklen_t s = sizeof(int);
198 gras_trp_socket_new(1, &res);
200 sd = accept(sock->sd, (struct sockaddr *) &peer_in, &peer_in_len);
201 tmp_errno = sock_errno;
204 gras_socket_close(sock);
205 THROW1(system_error, 0,
206 "Accept failed (%s). Droping server socket.",
207 sock_errstr(tmp_errno));
210 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s)
211 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i,
213 THROW1(system_error, 0,
214 "setsockopt failed, cannot condition the socket: %s",
215 sock_errstr(tmp_errno));
217 res->buf_size = sock->buf_size;
218 size = sock->buf_size;
219 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size))
220 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)))
221 VERB1("setsockopt failed, cannot set buffer size: %s",
222 sock_errstr(tmp_errno));
224 res->plugin = sock->plugin;
225 res->incoming = sock->incoming;
226 res->outgoing = sock->outgoing;
231 gras_trp_tcp_recv(res, (char *) &hisport, sizeof(hisport));
232 res->peer_port = ntohl(hisport);
233 DEBUG1("peerport %d received", res->peer_port);
235 /* FIXME: Lock to protect inet_ntoa */
236 if (((struct sockaddr *) &peer_in)->sa_family != AF_INET) {
237 res->peer_name = (char *) strdup("unknown");
239 struct in_addr addrAsInAddr;
242 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
244 tmp = inet_ntoa(addrAsInAddr);
246 res->peer_name = (char *) strdup(tmp);
248 res->peer_name = (char *) strdup("unknown");
252 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name, res->peer_port, sd);
253 xbt_dynar_push(((gras_trp_procdata_t)
254 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
260 static void gras_trp_sock_socket_close(gras_socket_t sock)
264 return; /* close only once */
266 VERB1("close tcp connection %d", sock->sd);
268 /* ask the listener to close the socket */
269 gras_msg_listener_close_socket(sock->sd);
272 /************************************/
273 /****[ end of SOCKET MANAGEMENT ]****/
274 /************************************/
277 /************************************/
278 /****[ UNBUFFERED DATA EXCHANGE ]****/
279 /************************************/
280 /* Temptation to merge this with file data exchange is great,
281 but doesn't work on BillWare (see tcp_write() in portable.h) */
282 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
284 unsigned long int size)
290 status = tcp_write(sock->sd, data, (size_t) size);
291 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
295 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
297 if (errno == EINTR || errno == EAGAIN)
301 THROW4(system_error, 0, "write(%d,%p,%ld) failed: %s",
302 sock->sd, data, size, sock_errstr(sock_errno));
309 THROW1(system_error, 0, "file descriptor closed (%s)",
310 sock_errstr(sock_errno));
315 static XBT_INLINE int
316 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
318 unsigned long int size,
319 unsigned long int bufsize)
325 data[0] = sock->recvd_val;
334 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
335 sock->sd, data + got, bufsize, got,
336 hexa_str((unsigned char *) data, got, 0));
337 status = tcp_read(sock->sd, data + got, (size_t) bufsize);
340 THROW7(system_error, 0,
341 "read(%d,%p,%d) from %s:%d failed: %s; got %d so far", sock->sd,
342 data + got, (int) size, gras_socket_peer_name(sock),
343 gras_socket_peer_port(sock), sock_errstr(sock_errno), got);
345 DEBUG2("Got %d more bytes (%s)", status,
346 hexa_str((unsigned char *) data + got, status, 0));
352 THROW1(system_error, errno,
353 "Socket closed by remote side (got %d bytes before this)", got);
360 static int gras_trp_tcp_recv(gras_socket_t sock,
361 char *data, unsigned long int size)
363 return gras_trp_tcp_recv_withbuffer(sock, data, size, size);
367 /*******************************************/
368 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
369 /*******************************************/
371 /**********************************/
372 /****[ BUFFERED DATA EXCHANGE ]****/
373 /**********************************/
375 /* Make sure the data is sent */
376 static void gras_trp_bufiov_flush(gras_socket_t sock)
382 gras_trp_bufdata_t *data = sock->bufdata;
386 if (data->out == buffering_buf) {
387 if (XBT_LOG_ISENABLED(gras_trp_tcp, xbt_log_priority_debug))
388 hexa_print("chunk to send ",
389 (unsigned char *) data->out_buf.data, data->out_buf.size);
390 if ((data->out_buf.size - data->out_buf.pos) != 0) {
391 DEBUG3("Send the chunk (size=%d) to %s:%d", data->out_buf.size,
392 gras_socket_peer_name(sock), gras_socket_peer_port(sock));
393 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
394 VERB1("Chunk sent (size=%d)", data->out_buf.size);
395 data->out_buf.size = 0;
399 if (data->out == buffering_iov) {
400 DEBUG0("Flush out iov");
401 vect = sock->bufdata->out_buf_v;
402 if ((size = xbt_dynar_length(vect))) {
403 DEBUG1("Flush %d chunks out of this socket", size);
404 writev(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
405 xbt_dynar_reset(vect);
407 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
410 if (data->in == buffering_iov) {
411 DEBUG0("Flush in iov");
412 vect = sock->bufdata->in_buf_v;
413 if ((size = xbt_dynar_length(vect))) {
414 DEBUG1("Get %d chunks from of this socket", size);
415 readv(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
416 xbt_dynar_reset(vect);
423 gras_trp_buf_send(gras_socket_t sock,
425 unsigned long int size, int stable_ignored)
428 gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
433 while (chunk_pos < size) {
434 /* size of the chunk to receive in that shot */
436 min(size - chunk_pos, data->buffsize - data->out_buf.size);
437 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
438 (int) data->out_buf.size,
439 ((int) data->out_buf.size) + thissize - 1, size,
440 hexa_str((unsigned char *) chunk, thissize, 0));
442 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos,
445 data->out_buf.size += thissize;
446 chunk_pos += thissize;
447 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
448 data->out_buf.size, size - chunk_pos, size,
449 hexa_str((unsigned char *) chunk, chunk_pos, 0));
451 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
452 gras_trp_bufiov_flush(sock);
459 gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size)
462 gras_trp_bufdata_t *data = sock->bufdata;
463 long int chunk_pos = 0;
467 while (chunk_pos < size) {
468 /* size of the chunk to receive in that shot */
471 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
473 DEBUG2("Get more data (size=%d,bufsize=%d)",
474 (int) MIN(size - chunk_pos, data->buffsize),
475 (int) data->buffsize);
479 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
480 MIN(size - chunk_pos, data->buffsize),
483 data->in_buf.pos = 0;
486 thissize = min(size - chunk_pos, data->in_buf.size - data->in_buf.pos);
487 memcpy(chunk + chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
489 data->in_buf.pos += thissize;
490 chunk_pos += thissize;
491 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
492 data->in_buf.pos, size - chunk_pos, size,
493 hexa_str((unsigned char *) chunk, chunk_pos, 0));
495 /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
496 sock->moredata = (data->in_buf.size > data->in_buf.pos);
497 DEBUG1("There is %smore data", (sock->moredata ? "" : "no "));
503 /*****************************************/
504 /****[ end of BUFFERED DATA EXCHANGE ]****/
505 /*****************************************/
507 /********************************/
508 /****[ VECTOR DATA EXCHANGE ]****/
509 /********************************/
512 gras_trp_iov_send(gras_socket_t sock,
513 const char *chunk, unsigned long int size, int stable)
516 gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
519 DEBUG1("Buffer one chunk to be sent later (%s)",
520 hexa_str((char *) chunk, size, 0));
522 elm.iov_len = (size_t) size;
525 /* data storage won't last until flush. Save it in a buffer if we can */
527 if (size > data->buffsize - data->out_buf.size) {
529 flush the socket, using data in its actual storage */
530 elm.iov_base = (void *) chunk;
531 xbt_dynar_push(data->out_buf_v, &elm);
533 gras_trp_bufiov_flush(sock);
536 /* buffer big enough:
537 copy data into it, and chain it for upcoming writev */
538 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
539 elm.iov_base = (void *) (data->out_buf.data + data->out_buf.size);
540 data->out_buf.size += size;
542 xbt_dynar_push(data->out_buf_v, &elm);
546 /* data storage stable. Chain it */
548 elm.iov_base = (void *) chunk;
549 xbt_dynar_push(data->out_buf_v, &elm);
554 gras_trp_iov_recv(gras_socket_t sock, char *chunk, unsigned long int size)
558 DEBUG0("Buffer one chunk to be received later");
559 elm.iov_base = (void *) chunk;
560 elm.iov_len = (size_t) size;
561 xbt_dynar_push(sock->bufdata->in_buf_v, &elm);
567 /***************************************/
568 /****[ end of VECTOR DATA EXCHANGE ]****/
569 /***************************************/
573 *** Prototypes of BUFFERED
576 void gras_trp_buf_socket_client(gras_trp_plugin_t self, gras_socket_t sock);
577 void gras_trp_buf_socket_server(gras_trp_plugin_t self, gras_socket_t sock);
578 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
580 void gras_trp_buf_socket_close(gras_socket_t sd);
583 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock)
585 gras_trp_bufdata_t *data = xbt_new(gras_trp_bufdata_t, 1);
587 data->buffsize = 100 * 1024; /* 100k */
589 data->in_buf.size = 0;
590 data->in_buf.data = xbt_malloc(data->buffsize);
591 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
593 data->out_buf.size = 0;
594 data->out_buf.data = xbt_malloc(data->buffsize);
595 data->out_buf.pos = data->out_buf.size;
598 data->in_buf_v = data->out_buf_v = NULL;
599 data->in_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
600 data->out_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
601 data->out = buffering_iov;
603 data->out = buffering_buf;
606 data->in = buffering_buf;
608 sock->bufdata = data;
615 void gras_trp_tcp_setup(gras_trp_plugin_t plug)
618 plug->socket_client = gras_trp_buf_socket_client;
619 plug->socket_server = gras_trp_buf_socket_server;
620 plug->socket_accept = gras_trp_buf_socket_accept;
621 plug->socket_close = gras_trp_buf_socket_close;
624 plug->send = gras_trp_iov_send;
626 plug->send = gras_trp_buf_send;
628 plug->recv = gras_trp_buf_recv;
630 plug->raw_send = gras_trp_tcp_send;
631 plug->raw_recv = gras_trp_tcp_recv;
633 plug->flush = gras_trp_bufiov_flush;
639 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
640 /* OUT */ gras_socket_t sock)
643 gras_trp_sock_socket_client(NULL, sock);
644 gras_trp_buf_init_sock(sock);
648 * gras_trp_buf_socket_server:
650 * Open a socket used to receive messages.
652 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
653 /* OUT */ gras_socket_t sock)
656 gras_trp_sock_socket_server(NULL, sock);
657 gras_trp_buf_init_sock(sock);
660 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock)
662 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
665 void gras_trp_buf_socket_close(gras_socket_t sock)
667 gras_trp_bufdata_t *data = sock->bufdata;
669 if (data->in_buf.size != data->in_buf.pos) {
670 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
671 data->in_buf.size - data->in_buf.pos,
672 data->in_buf.size, data->in_buf.pos);
674 if (data->in_buf.data)
675 free(data->in_buf.data);
677 if (data->out_buf.size != data->out_buf.pos) {
678 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
679 data->in_buf.size, data->out_buf.size);
680 gras_trp_bufiov_flush(sock);
682 if (data->out_buf.data)
683 free(data->out_buf.data);
686 if (data->in_buf_v) {
687 if (xbt_dynar_length(data->in_buf_v))
688 WARN0("Socket closed, but some bytes were unread");
689 xbt_dynar_free(&data->in_buf_v);
691 if (data->out_buf_v) {
692 if (xbt_dynar_length(data->out_buf_v)) {
693 DEBUG0("Flush the socket before closing");
694 gras_trp_bufiov_flush(sock);
696 xbt_dynar_free(&data->out_buf_v);
701 gras_trp_sock_socket_close(sock);
704 /****************************/
705 /****[ HELPER FUNCTIONS ]****/
706 /****************************/
709 * Returns the tcp protocol number from the network protocol data base.
711 * getprotobyname() is not thread safe. We need to lock it.
713 static int _gras_tcp_proto_number(void)
715 struct protoent *fetchedEntry;
716 static int returnValue = 0;
718 if (returnValue == 0) {
719 fetchedEntry = getprotobyname("tcp");
720 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
721 returnValue = fetchedEntry->p_proto;
727 #ifdef HAVE_WINSOCK_H
728 #define RETSTR( x ) case x: return #x
730 const char *gras_wsa_err2string(int err)
739 RETSTR(WSAEWOULDBLOCK);
740 RETSTR(WSAEINPROGRESS);
743 RETSTR(WSAEDESTADDRREQ);
745 RETSTR(WSAEPROTOTYPE);
746 RETSTR(WSAENOPROTOOPT);
747 RETSTR(WSAEPROTONOSUPPORT);
748 RETSTR(WSAESOCKTNOSUPPORT);
749 RETSTR(WSAEOPNOTSUPP);
750 RETSTR(WSAEPFNOSUPPORT);
751 RETSTR(WSAEAFNOSUPPORT);
752 RETSTR(WSAEADDRINUSE);
753 RETSTR(WSAEADDRNOTAVAIL);
755 RETSTR(WSAENETUNREACH);
756 RETSTR(WSAENETRESET);
757 RETSTR(WSAECONNABORTED);
758 RETSTR(WSAECONNRESET);
762 RETSTR(WSAESHUTDOWN);
763 RETSTR(WSAETOOMANYREFS);
764 RETSTR(WSAETIMEDOUT);
765 RETSTR(WSAECONNREFUSED);
767 RETSTR(WSAENAMETOOLONG);
768 RETSTR(WSAEHOSTDOWN);
769 RETSTR(WSAEHOSTUNREACH);
770 RETSTR(WSAENOTEMPTY);
776 RETSTR(WSASYSNOTREADY);
777 RETSTR(WSAVERNOTSUPPORTED);
778 RETSTR(WSANOTINITIALISED);
783 RETSTR(WSAECANCELLED);
784 RETSTR(WSAEINVALIDPROCTABLE);
785 RETSTR(WSAEINVALIDPROVIDER);
786 RETSTR(WSASYSCALLFAILURE);
787 RETSTR(WSASERVICE_NOT_FOUND);
788 RETSTR(WSATYPE_NOT_FOUND);
789 RETSTR(WSA_E_NO_MORE);
790 RETSTR(WSA_E_CANCELLED);
792 #endif /* HAVE_WINSOCK2 */
794 RETSTR(WSAHOST_NOT_FOUND);
795 RETSTR(WSATRY_AGAIN);
796 RETSTR(WSANO_RECOVERY);
799 return "unknown WSA error";
801 #endif /* HAVE_WINSOCK_H */
803 /***********************************/
804 /****[ end of HELPER FUNCTIONS ]****/
805 /***********************************/