| 1 |
/* |
| 2 |
* Copyright (C) by Argonne National Laboratory |
| 3 |
* See COPYRIGHT in top-level directory |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "tcp_impl.h" |
| 7 |
|
| 8 |
/* MPID_nem_tcp_get_conninfo -- This function takes a VC |
| 9 |
pointer as input and outputs the sockaddr, pg_id, and pg_rank of |
| 10 |
the remote process associated with this VC. [NOTE: I'm not sure |
| 11 |
yet, if the pg_id parameters will be char* or char**. I'd like to |
| 12 |
avoid a copy on this.] */ |
| 13 |
int MPID_nem_tcp_get_conninfo(struct MPIDI_VC *vc, struct sockaddr_in *addr, char **pg_id, |
| 14 |
int *pg_rank) |
| 15 |
{ |
| 16 |
int mpi_errno = MPI_SUCCESS; |
| 17 |
MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc); |
| 18 |
|
| 19 |
*addr = vc_tcp->sock_id; |
| 20 |
*pg_id = (char *) vc->pg->id; |
| 21 |
*pg_rank = vc->pg_rank; |
| 22 |
|
| 23 |
return mpi_errno; |
| 24 |
} |
| 25 |
|
| 26 |
/* MPID_nem_tcp_get_vc_from_conninfo -- This function takes |
| 27 |
the pg_id and pg_rank and returns the corresponding VC. */ |
| 28 |
int MPID_nem_tcp_get_vc_from_conninfo(char *pg_id, int pg_rank, struct MPIDI_VC **vc) |
| 29 |
{ |
| 30 |
int mpi_errno = MPI_SUCCESS; |
| 31 |
MPIDI_PG_t *pg; |
| 32 |
|
| 33 |
MPIR_FUNC_ENTER; |
| 34 |
|
| 35 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, |
| 36 |
(MPL_DBG_FDEST, "pg_id=%s pg_rank=%d", pg_id, pg_rank)); |
| 37 |
|
| 38 |
mpi_errno = MPIDI_PG_Find(pg_id, &pg); |
| 39 |
MPIR_ERR_CHECK(mpi_errno); |
| 40 |
|
| 41 |
MPIR_ERR_CHKINTERNAL(pg == NULL, mpi_errno, "invalid PG"); |
| 42 |
MPIR_ERR_CHKINTERNAL(pg_rank < 0 || |
| 43 |
pg_rank > MPIDI_PG_Get_size(pg), mpi_errno, "invalid pg_rank"); |
| 44 |
|
| 45 |
MPIDI_PG_Get_vc_set_active(pg, pg_rank, vc); |
| 46 |
|
| 47 |
fn_exit: |
| 48 |
MPIR_FUNC_EXIT; |
| 49 |
return mpi_errno; |
| 50 |
fn_fail: |
| 51 |
goto fn_exit; |
| 52 |
} |
| 53 |
|
| 54 |
int MPID_nem_tcp_set_sockopts(int fd) |
| 55 |
{ |
| 56 |
int mpi_errno = MPI_SUCCESS; |
| 57 |
int option, flags; |
| 58 |
int ret; |
| 59 |
socklen_t len; |
| 60 |
#ifdef HAVE_ERROR_CHECKING |
| 61 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 62 |
#endif |
| 63 |
|
| 64 |
/* fprintf(stdout, __func__ " Enter\n"); fflush(stdout); */ |
| 65 |
/* I heard you have to read the options after setting them in some implementations */ |
| 66 |
|
| 67 |
option = 1; |
| 68 |
len = sizeof(int); |
| 69 |
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &option, len); |
| 70 |
MPIR_ERR_CHKANDJUMP2(ret == -1, mpi_errno, MPI_ERR_OTHER, "**fail", "**fail %s %d", |
| 71 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 72 |
ret = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &option, &len); |
| 73 |
MPIR_ERR_CHKANDJUMP2(ret == -1, mpi_errno, MPI_ERR_OTHER, "**fail", "**fail %s %d", |
| 74 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 75 |
|
| 76 |
flags = fcntl(fd, F_GETFL, 0); |
| 77 |
MPIR_ERR_CHKANDJUMP2(flags == -1, mpi_errno, MPI_ERR_OTHER, "**fail", "**fail %s %d", |
| 78 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 79 |
ret = fcntl(fd, F_SETFL, flags | SO_REUSEADDR); |
| 80 |
MPIR_ERR_CHKANDJUMP2(ret == -1, mpi_errno, MPI_ERR_OTHER, "**fail", "**fail %s %d", |
| 81 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 82 |
|
| 83 |
flags = fcntl(fd, F_GETFL, 0); |
| 84 |
MPIR_ERR_CHKANDJUMP2(flags == -1, mpi_errno, MPI_ERR_OTHER, "**fail", "**fail %s %d", |
| 85 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 86 |
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 87 |
MPIR_ERR_CHKANDJUMP2(ret == -1, mpi_errno, MPI_ERR_OTHER, "**fail", "**fail %s %d", |
| 88 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 89 |
|
| 90 |
fn_exit: |
| 91 |
/* fprintf(stdout, __func__ " Exit\n"); fflush(stdout); */ |
| 92 |
return mpi_errno; |
| 93 |
fn_fail: |
| 94 |
/* fprintf(stdout, "failure. mpi_errno = %d\n", mpi_errno); */ |
| 95 |
goto fn_exit; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/* |
| 100 |
MPID_NEM_TCP_SOCK_ERROR_EOF : connection failed |
| 101 |
MPID_NEM_TCP_SOCK_CONNECTED : socket connected (connection success) |
| 102 |
MPID_NEM_TCP_SOCK_NOEVENT : No event on socket |
| 103 |
|
| 104 |
N1: some implementations do not set POLLERR when there is a pending error on socket. |
| 105 |
So, solution is to check for readability/writeablility and then call getsockopt. |
| 106 |
Again, getsockopt behaves differently in different implementations which is handled |
| 107 |
safely here (per Stevens-Unix Network Programming) |
| 108 |
|
| 109 |
N2: As far as the socket code is concerned, it doesn't really differentiate whether |
| 110 |
there is an error in the socket or whether the peer has closed it (i.e we have received |
| 111 |
EOF and hence recv returns 0). Either way, we deccide the socket fd is not usable any |
| 112 |
more. So, same return code is used. |
| 113 |
A design decision is not to write also, if the peer has closed the socket. Please note that |
| 114 |
write will still be successful, even if the peer has sent us FIN. Only the subsequent |
| 115 |
write will fail. So, this function is made tight enough and this should be called |
| 116 |
before doing any read/write at least in the connection establishment state machine code. |
| 117 |
|
| 118 |
N3: return code MPID_NEM_TCP_SOCK_NOEVENT is used only by the code that wants to |
| 119 |
know whether the connect is still not complete after a non-blocking connect is issued. |
| 120 |
|
| 121 |
TODO: Make this a macro for performance, if needed based on the usage. |
| 122 |
FIXME : Above comments are inconsistent now with the changes. No check for EOF is |
| 123 |
actually done now in this function. |
| 124 |
*/ |
| 125 |
|
| 126 |
int MPID_nem_tcp_check_sock_status(const struct pollfd *const plfd) |
| 127 |
{ |
| 128 |
int rc = MPID_NEM_TCP_SOCK_NOEVENT; |
| 129 |
#ifdef MPL_USE_DBG_LOGGING |
| 130 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 131 |
#endif |
| 132 |
|
| 133 |
if (plfd->revents & POLLERR) { |
| 134 |
rc = MPID_NEM_TCP_SOCK_ERROR_EOF; |
| 135 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, (MPL_DBG_FDEST, "POLLERR on socket")); |
| 136 |
goto fn_exit; |
| 137 |
} |
| 138 |
if (plfd->revents & POLLIN || plfd->revents & POLLOUT) { |
| 139 |
int error = 0; |
| 140 |
socklen_t n = sizeof(error); |
| 141 |
|
| 142 |
n = sizeof(error); |
| 143 |
if (getsockopt(plfd->fd, SOL_SOCKET, SO_ERROR, &error, &n) < 0 || error != 0) { |
| 144 |
rc = MPID_NEM_TCP_SOCK_ERROR_EOF; /* (N1) */ |
| 145 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, |
| 146 |
(MPL_DBG_FDEST, "getsockopt failure. error=%d:%s", error, |
| 147 |
MPIR_Strerror(error, strerrbuf, MPIR_STRERROR_BUF_SIZE))); |
| 148 |
goto fn_exit; |
| 149 |
} |
| 150 |
rc = MPID_NEM_TCP_SOCK_CONNECTED; |
| 151 |
} |
| 152 |
fn_exit: |
| 153 |
return rc; |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
*/ |
| 158 |
int MPID_nem_tcp_is_sock_connected(int fd) |
| 159 |
{ |
| 160 |
int rc = FALSE; |
| 161 |
char buf[1]; |
| 162 |
int buf_len = sizeof(buf) / sizeof(buf[0]), error = 0; |
| 163 |
size_t ret_recv; |
| 164 |
socklen_t n = sizeof(error); |
| 165 |
#ifdef MPL_USE_DBG_LOGGING |
| 166 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 167 |
#endif |
| 168 |
|
| 169 |
n = sizeof(error); |
| 170 |
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &n) < 0 || error != 0) { |
| 171 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, |
| 172 |
(MPL_DBG_FDEST, "getsockopt failure. error=%d:%s", error, |
| 173 |
MPIR_Strerror(error, strerrbuf, MPIR_STRERROR_BUF_SIZE))); |
| 174 |
rc = FALSE; /* error */ |
| 175 |
goto fn_exit; |
| 176 |
} |
| 177 |
|
| 178 |
CHECK_EINTR(ret_recv, recv(fd, buf, buf_len, MSG_PEEK)); |
| 179 |
if (ret_recv == 0) |
| 180 |
rc = FALSE; |
| 181 |
else |
| 182 |
rc = TRUE; |
| 183 |
fn_exit: |
| 184 |
return rc; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* --BEGIN ERROR HANDLING-- */ |
| 189 |
void MPID_nem_tcp_vc_dbg_print_sendq(FILE * stream, MPIDI_VC_t * vc) |
| 190 |
{ |
| 191 |
int i; |
| 192 |
MPIR_Request *sreq; |
| 193 |
MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc); |
| 194 |
|
| 195 |
fprintf(stream, ".. sc=%p fd=%d vc_tcp->state=%d\n", vc_tcp->sc, |
| 196 |
(vc_tcp->sc ? vc_tcp->sc->fd : -1), vc_tcp->state); |
| 197 |
|
| 198 |
/* This function violates any abstraction in the queues, since there's no |
| 199 |
* good way to print them without inspecting the internals. */ |
| 200 |
sreq = vc_tcp->send_queue.head; |
| 201 |
i = 0; |
| 202 |
while (sreq) { |
| 203 |
fprintf(stream, "....[%d] sreq=%p ctx=%#x rank=%d tag=%d\n", i, sreq, |
| 204 |
sreq->dev.match.parts.context_id, |
| 205 |
sreq->dev.match.parts.rank, sreq->dev.match.parts.tag); |
| 206 |
++i; |
| 207 |
sreq = sreq->next; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/* --END ERROR HANDLING-- */ |