Debian Codegraph @

src/mpid/ch3/channels/nemesis/netmod/tcp/socksm.c

raw
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #define SOCKSM_H_DEFGLOBALS_
7
8 #include "tcp_impl.h"
9 #include "socksm.h"
10
11 /* FIXME trace/log all the state transitions */
12
13 typedef struct freenode {
14 int index;
15 struct freenode *next;
16 } freenode_t;
17
18 static struct {
19 freenode_t *head, *tail;
20 } freeq = {
21 NULL, NULL};
22
23 static int g_tbl_size = 0;
24 static int g_tbl_capacity = CONN_PLFD_TBL_INIT_SIZE;
25
26 static sockconn_t *g_sc_tbl = NULL;
27 struct pollfd *MPID_nem_tcp_plfd_tbl = NULL;
28
29 sockconn_t MPID_nem_tcp_g_lstn_sc;
30 struct pollfd MPID_nem_tcp_g_lstn_plfd;
31
32 /* We define this in order to trick the compiler into including
33 information about the MPID_nem_tcp_vc_area type. This is
34 needed to easily expand the VC_FIELD macro in a debugger. The
35 'unused' attribute keeps the compiler from complaining. The 'used'
36 attribute makes sure the symbol is added in the lib, even if it's
37 unused */
38 static MPID_nem_tcp_vc_area *dummy_vc_area ATTRIBUTE((unused, used)) = NULL;
39
40 #define MAX_SKIP_POLLS_INACTIVE 512 /* something bigger */
41 #define MAX_SKIP_POLLS_ACTIVE 128 /* something smaller */
42 static int MPID_nem_tcp_skip_polls = MAX_SKIP_POLLS_INACTIVE;
43
44 /* Debug function to dump the sockconn table. This is intended to be
45 called from a debugger. The 'unused' attribute keeps the compiler
46 from complaining. The 'used' attribute makes sure the function is
47 added in the lib, despite the fact that it's unused. */
48 static void dbg_print_sc_tbl(FILE * stream, int print_free_entries) ATTRIBUTE((unused, used));
49
50 #define MPID_NEM_TCP_RECV_MAX_PKT_LEN 1024
51 static char *recv_buf;
52
53 static struct {
54 handler_func_t sc_state_handler;
55 short sc_state_plfd_events;
56 } sc_state_info[CONN_STATE_SIZE];
57
58 #define IS_WRITEABLE(plfd) \
59 (plfd->revents & POLLOUT) ? 1 : 0
60
61 #define IS_READABLE(plfd) \
62 (plfd->revents & POLLIN) ? 1 : 0
63
64 #define IS_READ_WRITEABLE(plfd) \
65 (plfd->revents & POLLIN && plfd->revents & POLLOUT) ? 1 : 0
66
67 #define IS_SAME_PGID(id1, id2) \
68 (strcmp(id1, id2) == 0)
69
70
71 #define INIT_SC_ENTRY(sc, ind) \
72 do { \
73 (sc)->fd = CONN_INVALID_FD; \
74 (sc)->index = ind; \
75 (sc)->vc = NULL; \
76 (sc)->pg_is_set = FALSE; \
77 (sc)->is_tmpvc = FALSE; \
78 (sc)->state.cstate = CONN_STATE_UNDEFINED; \
79 CHANGE_STATE(sc, CONN_STATE_TS_CLOSED); \
80 } while (0)
81
82 #define INIT_POLLFD_ENTRY(plfd) \
83 do { \
84 (plfd)->fd = CONN_INVALID_FD; \
85 (plfd)->events = POLLIN; \
86 } while (0)
87
88
89 /* --BEGIN ERROR HANDLING-- */
90 /* This function can be called from a debugger to dump the contents of the
91 g_sc_tbl to the given stream. If print_free_entries is true entries
92 0..g_tbl_capacity will be printed. Otherwise, only 0..g_tbl_size will be
93 shown. */
94 static void dbg_print_sc_tbl(FILE * stream, int print_free_entries)
95 {
96 int i;
97 sockconn_t *sc;
98
99 fprintf(stream, "========================================\n");
100 for (i = 0; i < (print_free_entries ? g_tbl_capacity : g_tbl_size); ++i) {
101 sc = &g_sc_tbl[i];
102 #define TF(_b) ((_b) ? "TRUE" : "FALSE")
103 fprintf(stream, "[%d] ptr=%p idx=%d fd=%d state=%s\n",
104 i, sc, sc->index, sc->fd, CONN_STATE_TO_STRING(sc->state.cstate));
105 fprintf(stream, "....pg_is_set=%s is_same_pg=%s is_tmpvc=%s pg_rank=%d pg_id=%s\n",
106 TF(sc->pg_is_set), TF(sc->is_same_pg), TF(sc->is_tmpvc), sc->pg_rank, sc->pg_id);
107 #undef TF
108 }
109 fprintf(stream, "========================================\n");
110 }
111
112 /* --END ERROR HANDLING-- */
113
114 static int find_free_entry(int *index);
115 static int close_cleanup_and_free_sc_plfd(sockconn_t * const sc);
116 static int cleanup_and_free_sc_plfd(sockconn_t * const sc);
117 static int error_closed(struct MPIDI_VC *const vc, int req_errno);
118
119 static inline int is_same_connection(sockconn_t * sc1, sockconn_t * sc2)
120 {
121
122 /* Returns TRUE iff sc1 and sc2 connect the same processes */
123
124 /* if pg_is_set is TRUE, then either it's the same pg, or pg_id is not NULL */
125 MPIR_Assert(!sc1->pg_is_set || sc1->is_same_pg || sc1->pg_id != NULL);
126 MPIR_Assert(!sc2->pg_is_set || sc2->is_same_pg || sc2->pg_id != NULL);
127
128 /* if it's a tmpvc, the pg should not be set */
129 MPIR_Assert(!sc1->is_tmpvc || !sc1->pg_is_set);
130 MPIR_Assert(!sc1->is_tmpvc || !sc1->pg_is_set);
131
132 return !sc1->is_tmpvc && !sc2->is_tmpvc &&
133 sc1->pg_is_set && sc2->pg_is_set &&
134 sc1->pg_rank == sc2->pg_rank &&
135 ((sc1->is_same_pg && sc2->is_same_pg) ||
136 (!sc1->is_same_pg && !sc2->is_same_pg && IS_SAME_PGID(sc1->pg_id, sc2->pg_id)));
137 }
138
139
140 static int alloc_sc_plfd_tbls(void)
141 {
142 int i, mpi_errno = MPI_SUCCESS, idx = -1;
143 MPIR_CHKPMEM_DECL();
144
145 MPIR_Assert(g_sc_tbl == NULL);
146 MPIR_Assert(MPID_nem_tcp_plfd_tbl == NULL);
147
148 MPIR_CHKPMEM_MALLOC(g_sc_tbl, g_tbl_capacity * sizeof(sockconn_t), MPL_MEM_ADDRESS);
149 MPIR_CHKPMEM_MALLOC(MPID_nem_tcp_plfd_tbl, g_tbl_capacity * sizeof(struct pollfd),
150 MPL_MEM_ADDRESS);
151 #if defined(MPICH_DEBUG_MEMINIT)
152 /* We initialize the arrays in order to eliminate spurious valgrind errors
153 * that occur when poll(2) returns 0. See valgrind bugzilla#158425 and
154 * remove this code if the fix ever gets into a release of valgrind.
155 * [goodell@ 2007-02-25] */
156 memset(MPID_nem_tcp_plfd_tbl, 0, g_tbl_capacity * sizeof(struct pollfd));
157 #endif
158
159 for (i = 0; i < g_tbl_capacity; i++) {
160 INIT_SC_ENTRY(((sockconn_t *) & g_sc_tbl[i]), i);
161 INIT_POLLFD_ENTRY(((struct pollfd *) &MPID_nem_tcp_plfd_tbl[i]));
162 }
163 MPIR_CHKPMEM_COMMIT();
164
165 mpi_errno = find_free_entry(&idx);
166 MPIR_ERR_CHECK(mpi_errno);
167
168 MPIR_Assert(0 == idx); /* assumed in other parts of this file */
169 MPIR_Memcpy(&g_sc_tbl[idx], &MPID_nem_tcp_g_lstn_sc, sizeof(MPID_nem_tcp_g_lstn_sc));
170 MPIR_Memcpy(&MPID_nem_tcp_plfd_tbl[idx], &MPID_nem_tcp_g_lstn_plfd,
171 sizeof(MPID_nem_tcp_g_lstn_plfd));
172 MPIR_Assert(MPID_nem_tcp_plfd_tbl[idx].fd == g_sc_tbl[idx].fd);
173 MPIR_Assert(MPID_nem_tcp_plfd_tbl[idx].events == POLLIN);
174
175 fn_exit:
176 return mpi_errno;
177 fn_fail:
178 MPIR_CHKPMEM_REAP();
179 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
180 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
181 goto fn_exit;
182 }
183
184 static int free_sc_plfd_tbls(void)
185 {
186 int mpi_errno = MPI_SUCCESS;
187
188 MPL_free(g_sc_tbl);
189 MPL_free(MPID_nem_tcp_plfd_tbl);
190 return mpi_errno;
191 }
192
193 /*
194 Reason for not doing realloc for both sc and plfd tables :
195 Either both the tables have to be expanded or both should remain the same size, if
196 enough memory could not be allocated, as we have only one set of variables to control
197 the size of the tables. Also, it is not useful to expand one table and leave the other
198 at the same size, 'coz of memory allocation failures.
199 */
200 static int expand_sc_plfd_tbls(void)
201 {
202 int mpi_errno = MPI_SUCCESS;
203 sockconn_t *new_sc_tbl = NULL;
204 struct pollfd *new_plfd_tbl = NULL;
205 int new_capacity = g_tbl_capacity + CONN_PLFD_TBL_GROW_SIZE, i;
206 MPIR_CHKPMEM_DECL();
207
208 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, (MPL_DBG_FDEST, "expand_sc_plfd_tbls Entry"));
209 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
210 (MPL_DBG_FDEST, "expand_sc_plfd_tbls b4 g_sc_tbl[0].fd=%d", g_sc_tbl[0].fd));
211 MPIR_CHKPMEM_MALLOC(new_sc_tbl, new_capacity * sizeof(sockconn_t), MPL_MEM_ADDRESS);
212 MPIR_CHKPMEM_MALLOC(new_plfd_tbl, new_capacity * sizeof(struct pollfd), MPL_MEM_ADDRESS);
213
214 MPIR_Memcpy(new_sc_tbl, g_sc_tbl, g_tbl_capacity * sizeof(sockconn_t));
215 MPIR_Memcpy(new_plfd_tbl, MPID_nem_tcp_plfd_tbl, g_tbl_capacity * sizeof(struct pollfd));
216
217 /* VCs have pointers to entries in the sc table. These
218 * are updated here after the expand. */
219 for (i = 1; i < g_tbl_capacity; i++) { /* i=0 = listening socket fd won't have a VC pointer */
220 sockconn_t *new_sc = &new_sc_tbl[i];
221 sockconn_t *sc = &g_sc_tbl[i];
222 MPIDI_VC_t *vc = sc->vc;
223 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
224
225 /* It's important to only make the assignment if the sc address in the
226 * vc matches the old sc address, otherwise we can corrupt the vc's
227 * state in certain head-to-head situations. */
228 if (vc && vc_tcp->sc && vc_tcp->sc == sc) {
229 ASSIGN_SC_TO_VC(vc_tcp, new_sc);
230 }
231 }
232
233 MPL_free(g_sc_tbl);
234 MPL_free(MPID_nem_tcp_plfd_tbl);
235 g_sc_tbl = new_sc_tbl;
236 MPID_nem_tcp_plfd_tbl = new_plfd_tbl;
237 for (i = g_tbl_capacity; i < new_capacity; i++) {
238 INIT_SC_ENTRY(((sockconn_t *) & g_sc_tbl[i]), i);
239 INIT_POLLFD_ENTRY(((struct pollfd *) &MPID_nem_tcp_plfd_tbl[i]));
240 }
241 g_tbl_capacity = new_capacity;
242
243 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
244 (MPL_DBG_FDEST, "expand_sc_plfd_tbls af g_sc_tbl[0].fd=%d", g_sc_tbl[0].fd));
245 for (i = 0; i < g_tbl_capacity; ++i) {
246 sockconn_t *sc = &g_sc_tbl[i];
247 MPIDI_VC_t *vc = sc->vc;
248 MPIR_AssertDeclValue(MPID_nem_tcp_vc_area * vc_tcp, VC_TCP(vc));
249 /* sockconn_t *dbg_sc = g_sc_tbl[i].vc ? VC_FIELD(g_sc_tbl[i].vc, sc) : (sockconn_t*)(-1); */
250
251 /* The state is only valid if the FD is valid. The VC field is only
252 * valid if the state is valid and COMMRDY. */
253 MPIR_Assert(MPID_nem_tcp_plfd_tbl[i].fd == CONN_INVALID_FD ||
254 sc->state.cstate != CONN_STATE_TS_COMMRDY || vc_tcp->sc == sc);
255 }
256
257
258 MPIR_CHKPMEM_COMMIT();
259 fn_exit:
260 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, (MPL_DBG_FDEST, "expand_sc_plfd_tbls Exit"));
261 return mpi_errno;
262 fn_fail:
263 MPIR_CHKPMEM_REAP();
264 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
265 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
266 goto fn_exit;
267 }
268
269 /*
270 Finds the first free entry in the connection table/pollfd table. Note that both the
271 tables are indexed the same. i.e. each entry in one table corresponds to the
272 entry of the same index in the other table. If an entry is valid in one table, then
273 it is valid in the other table too.
274
275 This function finds the first free entry in both the tables by checking the queue of
276 free elements. If the free queue is empty, then it returns the next available slot
277 in the tables. If the size of the slot is already full, then this expands the table
278 and then returns the next available slot
279 */
280 static int find_free_entry(int *idx)
281 {
282 int mpi_errno = MPI_SUCCESS;
283 freenode_t *node;
284
285 if (!Q_EMPTY(freeq)) {
286 Q_DEQUEUE(&freeq, ((freenode_t **) & node));
287 *idx = node->index;
288 MPL_free(node);
289 goto fn_exit;
290 }
291
292 if (g_tbl_size == g_tbl_capacity) {
293 mpi_errno = expand_sc_plfd_tbls();
294 if (mpi_errno != MPI_SUCCESS)
295 goto fn_fail;
296 }
297
298 MPIR_Assert(g_tbl_capacity > g_tbl_size);
299 *idx = g_tbl_size;
300 ++g_tbl_size;
301
302 fn_exit:
303 /* This function is the closest thing we have to a constructor, so we throw
304 * in a couple of initializers here in case the caller is sloppy about his
305 * assumptions. */
306 INIT_SC_ENTRY(&g_sc_tbl[*idx], *idx);
307 INIT_POLLFD_ENTRY(&MPID_nem_tcp_plfd_tbl[*idx]);
308 return mpi_errno;
309 fn_fail:
310 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
311 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
312 goto fn_exit;
313 }
314
315 /* Note:
316 fnd_sc is returned only for certain states. If it is not returned for a state,
317 the handler function can simply pass NULL as the second argument.
318 */
319 static int found_better_sc(sockconn_t * sc, sockconn_t ** fnd_sc)
320 {
321 int i, found = FALSE;
322
323 MPIR_FUNC_ENTER;
324
325 /* tmpvc's can never match a better sc */
326 if (sc->is_tmpvc) {
327 found = FALSE;
328 goto fn_exit;
329 }
330
331 /* if we don't know our own pg info, how can we look for a better SC? */
332 MPIR_Assert(sc->pg_is_set);
333
334 for (i = 0; i < g_tbl_size && !found; i++) {
335 sockconn_t *iter_sc = &g_sc_tbl[i];
336 MPID_nem_tcp_Conn_State_t istate = iter_sc->state.cstate;
337
338 if (iter_sc != sc && iter_sc->fd != CONN_INVALID_FD && is_same_connection(iter_sc, sc)) {
339 switch (sc->state.cstate) {
340 case CONN_STATE_TC_C_CNTD:
341 MPIR_Assert(fnd_sc == NULL);
342 if (istate == CONN_STATE_TS_COMMRDY ||
343 istate == CONN_STATE_TA_C_RANKRCVD || istate == CONN_STATE_TC_C_TMPVCSENT)
344 found = TRUE;
345 break;
346 case CONN_STATE_TA_C_RANKRCVD:
347 MPIR_Assert(fnd_sc != NULL);
348 if (istate == CONN_STATE_TS_COMMRDY || istate == CONN_STATE_TC_C_RANKSENT) {
349 found = TRUE;
350 *fnd_sc = iter_sc;
351 }
352 break;
353 case CONN_STATE_TA_C_TMPVCRCVD:
354 MPIR_Assert(fnd_sc != NULL);
355 if (istate == CONN_STATE_TS_COMMRDY || istate == CONN_STATE_TC_C_TMPVCSENT) {
356 found = TRUE;
357 *fnd_sc = iter_sc;
358 }
359 break;
360 /* Add code for other states here, if need be. */
361 default:
362 /* FIXME: need to handle error condition better */
363 MPIR_Assert(0);
364 break;
365 }
366 }
367 }
368
369 fn_exit:
370 if (found) {
371 if (fnd_sc) {
372 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
373 (MPL_DBG_FDEST,
374 "found_better_sc(sc=%p (%s), *fnd_sc=%p (%s)) found=TRUE", sc,
375 CONN_STATE_STR[sc->state.cstate], *fnd_sc,
376 (*fnd_sc ? CONN_STATE_STR[(*fnd_sc)->state.cstate] : "N/A")));
377 } else {
378 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
379 (MPL_DBG_FDEST, "found_better_sc(sc=%p (%s), fnd_sc=(nil)) found=TRUE",
380 sc, CONN_STATE_STR[sc->state.cstate]));
381 }
382 } else {
383 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
384 (MPL_DBG_FDEST, "found_better_sc(sc=%p (%s), *fnd_sc=N/A) found=FALSE",
385 sc, CONN_STATE_STR[sc->state.cstate]));
386 }
387 MPIR_FUNC_EXIT;
388 return found;
389 }
390
391
392 static int vc_is_in_shutdown(MPIDI_VC_t * vc)
393 {
394 int retval = FALSE;
395 MPIR_Assert(vc != NULL);
396 if (vc->state == MPIDI_VC_STATE_REMOTE_CLOSE ||
397 vc->state == MPIDI_VC_STATE_CLOSE_ACKED ||
398 vc->state == MPIDI_VC_STATE_CLOSED ||
399 vc->state == MPIDI_VC_STATE_LOCAL_CLOSE ||
400 vc->state == MPIDI_VC_STATE_INACTIVE ||
401 vc->state == MPIDI_VC_STATE_INACTIVE_CLOSED || vc->state == MPIDI_VC_STATE_MORIBUND) {
402 retval = TRUE;
403 }
404
405 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
406 (MPL_DBG_FDEST, "vc_is_in_shutdown(%p)=%s", vc, (retval ? "TRUE" : "FALSE")));
407 return retval;
408 }
409
410 static int send_id_info(const sockconn_t * const sc)
411 {
412 int mpi_errno = MPI_SUCCESS;
413 MPIDI_nem_tcp_idinfo_t id_info;
414 MPIDI_nem_tcp_header_t hdr;
415 struct iovec iov[3];
416 int buf_size, iov_cnt = 2;
417 ssize_t offset;
418 size_t pg_id_len = 0;
419 #ifdef HAVE_ERROR_CHECKING
420 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
421 #endif
422
423 MPIR_FUNC_ENTER;
424
425 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
426 (MPL_DBG_FDEST, "my_pg->id=%s my_pg->rank=%d, sc->pg_rank=%d sc->is_same_pg=%s",
427 (char *) MPIDI_Process.my_pg->id, MPIDI_Process.my_pg_rank, sc->pg_rank,
428 (sc->is_same_pg ? "TRUE" : "FALSE")));
429 if (!sc->is_same_pg)
430 pg_id_len = strlen(MPIDI_Process.my_pg->id) + 1;
431
432 /* store ending NULL also */
433 /* FIXME better keep pg_id_len itself as part of MPIDI_Process.my_pg structure to */
434 /* avoid computing the length of string every time this function is called. */
435
436 MPL_VG_MEM_INIT(&hdr, sizeof(hdr));
437
438 hdr.pkt_type = MPIDI_NEM_TCP_SOCKSM_PKT_ID_INFO;
439 hdr.datalen = sizeof(MPIDI_nem_tcp_idinfo_t) + pg_id_len;
440 id_info.pg_rank = MPIDI_Process.my_pg_rank;
441
442 iov[0].iov_base = (void *) & hdr;
443 iov[0].iov_len = sizeof(hdr);
444 iov[1].iov_base = (void *) & id_info;
445 iov[1].iov_len = sizeof(id_info);
446 buf_size = sizeof(hdr) + sizeof(id_info);
447
448 if (!sc->is_same_pg) {
449 iov[2].iov_base = MPIDI_Process.my_pg->id;
450 iov[2].iov_len = pg_id_len;
451 buf_size += pg_id_len;
452 ++iov_cnt;
453 }
454
455 offset = MPL_large_writev(sc->fd, iov, iov_cnt);
456 MPIR_ERR_CHKANDJUMP1(offset == -1 &&
457 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**write", "**write %s",
458 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
459 MPIR_ERR_CHKANDJUMP1(offset != buf_size, mpi_errno, MPI_ERR_OTHER, "**write", "**write %s",
460 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
461 /* FIXME log appropriate error */
462 /* FIXME-Z1 socket is just connected and we are sending a few bytes. So, there should not */
463 /* be a problem of partial data only being written to. If partial data written, */
464 /* handle this. */
465
466 fn_exit:
467 MPIR_FUNC_EXIT;
468 return mpi_errno;
469 fn_fail:
470 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
471 (MPL_DBG_FDEST, "failure. mpi_errno = %d, offset=%lld, errno=%d %s", mpi_errno,
472 (long long) offset, errno, MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)));
473 goto fn_exit;
474 }
475
476
477 static int send_tmpvc_info(const sockconn_t * const sc)
478 {
479 int mpi_errno = MPI_SUCCESS;
480 MPIDI_nem_tcp_portinfo_t port_info;
481 MPIDI_nem_tcp_header_t hdr;
482 struct iovec iov[3];
483 int buf_size, iov_cnt = 2;
484 ssize_t offset;
485 #ifdef HAVE_ERROR_CHECKING
486 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
487 #endif
488
489 MPIR_FUNC_ENTER;
490
491 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
492 (MPL_DBG_FDEST, "my->pg_rank=%d, sc->pg_rank=%d", MPIDI_Process.my_pg_rank,
493 sc->pg_rank));
494
495 /* store ending NULL also */
496 /* FIXME better keep pg_id_len itself as part of MPIDI_Process.my_pg structure to */
497 /* avoid computing the length of string every time this function is called. */
498
499 MPL_VG_MEM_INIT(&hdr, sizeof(hdr));
500
501 hdr.pkt_type = MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_INFO;
502 hdr.datalen = sizeof(MPIDI_nem_tcp_portinfo_t);
503 port_info.port_name_tag = sc->vc->port_name_tag;
504
505 iov[0].iov_base = (void *) & hdr;
506 iov[0].iov_len = sizeof(hdr);
507 iov[1].iov_base = (void *) & port_info;
508 iov[1].iov_len = sizeof(port_info);
509 buf_size = sizeof(hdr) + sizeof(port_info);
510
511 offset = MPL_large_writev(sc->fd, iov, iov_cnt);
512 MPIR_ERR_CHKANDJUMP1(offset == -1 &&
513 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**write", "**write %s",
514 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
515 MPIR_ERR_CHKANDJUMP1(offset != buf_size, mpi_errno, MPI_ERR_OTHER, "**write", "**write %s",
516 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
517 /* FIXME log appropriate error */
518 /* FIXME-Z1 socket is just connected and we are sending a few bytes. So, there should not */
519 /* be a problem of partial data only being written to. If partial data written, */
520 /* handle this. */
521
522 fn_exit:
523 MPIR_FUNC_EXIT;
524 return mpi_errno;
525 fn_fail:
526 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
527 (MPL_DBG_FDEST, "failure. mpi_errno = %d, offset=%lld, errno=%d %s", mpi_errno,
528 (long long) offset, errno, MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)));
529 goto fn_exit;
530 }
531
532 static int recv_id_or_tmpvc_info(sockconn_t * const sc, int *got_sc_eof)
533 {
534 int mpi_errno = MPI_SUCCESS;
535 MPIDI_nem_tcp_header_t hdr;
536 int iov_cnt = 1;
537 size_t pg_id_len = 0;
538 ssize_t nread;
539 int hdr_len = sizeof(MPIDI_nem_tcp_header_t);
540 struct iovec iov[2];
541 char *pg_id = NULL;
542 #ifdef HAVE_ERROR_CHECKING
543 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
544 #endif
545
546 MPIR_CHKPMEM_DECL();
547 MPIR_CHKLMEM_DECL();
548
549 MPIR_FUNC_ENTER;
550
551 *got_sc_eof = 0;
552
553 CHECK_EINTR(nread, read(sc->fd, &hdr, hdr_len));
554
555 /* The other side closed this connection (hopefully as part of a
556 * head-to-head resolution. */
557 if (0 == nread) {
558 *got_sc_eof = 1;
559 goto fn_exit;
560 }
561 MPIR_ERR_CHKANDJUMP1(nread == -1 &&
562 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**read", "**read %s",
563 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
564 MPIR_ERR_CHKANDJUMP(nread != hdr_len, mpi_errno, MPI_ERR_OTHER, "**read"); /* FIXME-Z1 */
565 MPIR_Assert(hdr.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_INFO ||
566 hdr.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_INFO);
567 MPIR_Assert(hdr.datalen != 0);
568
569 if (hdr.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_INFO) {
570 iov[0].iov_base = (void *) &(sc->pg_rank);
571 iov[0].iov_len = sizeof(sc->pg_rank);
572 pg_id_len = hdr.datalen - sizeof(MPIDI_nem_tcp_idinfo_t);
573 if (pg_id_len != 0) {
574 MPIR_CHKLMEM_MALLOC(pg_id, pg_id_len);
575 iov[1].iov_base = (void *) pg_id;
576 iov[1].iov_len = pg_id_len;
577 ++iov_cnt;
578 }
579 nread = MPL_large_readv(sc->fd, iov, iov_cnt);
580 MPIR_ERR_CHKANDJUMP1(nread == -1 &&
581 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**read", "**read %s",
582 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
583 MPIR_ERR_CHKANDJUMP(nread != hdr.datalen, mpi_errno, MPI_ERR_OTHER, "**read"); /* FIXME-Z1 */
584 if (pg_id_len == 0) {
585 sc->is_same_pg = TRUE;
586 mpi_errno = MPID_nem_tcp_get_vc_from_conninfo(MPIDI_Process.my_pg->id,
587 sc->pg_rank, &sc->vc);
588 MPIR_ERR_CHECK(mpi_errno);
589 sc->pg_id = NULL;
590 } else {
591 sc->is_same_pg = FALSE;
592 mpi_errno = MPID_nem_tcp_get_vc_from_conninfo(pg_id, sc->pg_rank, &sc->vc);
593 MPIR_ERR_CHECK(mpi_errno);
594 sc->pg_id = sc->vc->pg->id;
595 }
596
597 { /* Added this block sp we can declare pointers to tcp private area of vc */
598 MPIDI_VC_t *sc_vc = sc->vc;
599 MPID_nem_tcp_vc_area *sc_vc_tcp = VC_TCP(sc_vc);
600
601 MPIR_Assert(sc_vc != NULL);
602 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
603 (MPL_DBG_FDEST,
604 "about to incr sc_ref_count sc=%p sc->vc=%p sc_ref_count=%d", sc,
605 sc_vc, sc_vc_tcp->sc_ref_count));
606 ++sc_vc_tcp->sc_ref_count;
607 }
608
609 /* very important, without this is_same_connection() will always fail */
610 sc->pg_is_set = TRUE;
611 MPIR_Assert(!sc->is_tmpvc);
612
613 MPL_DBG_MSG_FMT(MPIDI_CH3_DBG_CHANNEL, VERBOSE,
614 (MPL_DBG_FDEST,
615 "PKT_ID_INFO: sc->pg_rank=%d sc->is_same_pg=%d sc->fd=%d, sc->vc=%p, sc=%p",
616 sc->pg_rank, sc->is_same_pg, sc->fd, sc->vc, sc));
617 } else if (hdr.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_INFO) {
618 MPIDI_VC_t *vc;
619 MPID_nem_tcp_vc_area *vc_tcp;
620
621 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
622 (MPL_DBG_FDEST, "PKT_TMPVC_INFO: sc->fd=%d", sc->fd));
623 /* create a new VC */
624 MPIR_CHKPMEM_MALLOC(vc, sizeof(MPIDI_VC_t), MPL_MEM_ADDRESS);
625 /* --BEGIN ERROR HANDLING-- */
626 if (vc == NULL) {
627 mpi_errno =
628 MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, __func__, __LINE__, MPI_ERR_OTHER,
629 "**nomem", NULL);
630 goto fn_fail;
631 }
632 /* --END ERROR HANDLING-- */
633
634 vc_tcp = VC_TCP(vc);
635
636 MPIDI_VC_Init(vc, NULL, 0);
637 vc_tcp->state = MPID_NEM_TCP_VC_STATE_CONNECTED; /* FIXME: is it needed ? */
638 sc->vc = vc;
639 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
640 (MPL_DBG_FDEST,
641 "about to incr sc_ref_count sc=%p sc->vc=%p sc_ref_count=%d", sc, sc->vc,
642 vc_tcp->sc_ref_count));
643 ++vc_tcp->sc_ref_count;
644
645 ASSIGN_SC_TO_VC(vc_tcp, sc);
646
647 /* get the port's tag from the packet and stash it in the VC */
648 iov[0].iov_base = (void *) &(sc->vc->port_name_tag);
649 iov[0].iov_len = sizeof(sc->vc->port_name_tag);
650
651 nread = MPL_large_readv(sc->fd, iov, iov_cnt);
652 MPIR_ERR_CHKANDJUMP1(nread == -1 &&
653 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**read", "**read %s",
654 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
655 MPIR_ERR_CHKANDJUMP(nread != hdr.datalen, mpi_errno, MPI_ERR_OTHER, "**read"); /* FIXME-Z1 */
656 sc->is_same_pg = FALSE;
657 sc->pg_id = NULL;
658 sc->is_tmpvc = TRUE;
659
660 MPL_DBG_MSG_FMT(MPIDI_CH3_DBG_CHANNEL, VERBOSE,
661 (MPL_DBG_FDEST, "enqueuing on acceptq vc=%p, sc->fd=%d, tag=%d", vc, sc->fd,
662 sc->vc->port_name_tag));
663 MPIDI_CH3I_Acceptq_enqueue(vc, sc->vc->port_name_tag);
664
665 MPIR_Assert(!sc->pg_is_set);
666 }
667
668 MPIR_CHKPMEM_COMMIT();
669 fn_exit:
670 MPIR_CHKLMEM_FREEALL();
671 MPIR_FUNC_EXIT;
672 return mpi_errno;
673 fn_fail:
674 MPIR_CHKPMEM_REAP();
675 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
676 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
677 goto fn_exit;
678 }
679
680 /*
681 This function is used to send commands that don't have data but just only
682 the header.
683 */
684 static int send_cmd_pkt(int fd, MPIDI_nem_tcp_socksm_pkt_type_t pkt_type)
685 {
686 int mpi_errno = MPI_SUCCESS;
687 ssize_t offset;
688 MPIDI_nem_tcp_header_t pkt;
689 int pkt_len = sizeof(MPIDI_nem_tcp_header_t);
690 #ifdef HAVE_ERROR_CHECKING
691 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
692 #endif
693
694 MPIR_Assert(pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_ACK ||
695 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK ||
696 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_ACK ||
697 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_NAK ||
698 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_CLOSED);
699
700 MPL_VG_MEM_INIT(&pkt, sizeof(pkt));
701
702 pkt.pkt_type = pkt_type;
703 pkt.datalen = 0;
704
705 CHECK_EINTR(offset, write(fd, &pkt, pkt_len));
706 MPIR_ERR_CHKANDJUMP1(offset == -1 &&
707 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**write", "**write %s",
708 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
709 MPIR_ERR_CHKANDJUMP1(offset != pkt_len, mpi_errno, MPI_ERR_OTHER, "**write", "**write %s",
710 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); /* FIXME-Z1 */
711 fn_exit:
712 return mpi_errno;
713 fn_fail:
714 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
715 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
716 goto fn_exit;
717 }
718
719
720 /*
721 This function is used to recv commands that don't have data but just only
722 the header.
723 */
724 static int recv_cmd_pkt(int fd, MPIDI_nem_tcp_socksm_pkt_type_t * pkt_type)
725 {
726 int mpi_errno = MPI_SUCCESS;
727 ssize_t nread;
728 MPIDI_nem_tcp_header_t pkt;
729 int pkt_len = sizeof(MPIDI_nem_tcp_header_t);
730 #ifdef HAVE_ERROR_CHECKING
731 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
732 #endif
733
734 MPIR_FUNC_ENTER;
735
736 CHECK_EINTR(nread, read(fd, &pkt, pkt_len));
737 MPIR_ERR_CHKANDJUMP1(nread == -1 &&
738 errno != EAGAIN, mpi_errno, MPI_ERR_OTHER, "**read", "**read %s",
739 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
740 MPIR_ERR_CHKANDJUMP2(nread != pkt_len, mpi_errno, MPI_ERR_OTHER, "**read", "**read %d %s", nread,
741 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); /* FIXME-Z1 */
742 MPIR_Assert(pkt.datalen == 0);
743 MPIR_Assert(pkt.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_ACK ||
744 pkt.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK ||
745 pkt.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_ACK ||
746 pkt.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_NAK ||
747 pkt.pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_CLOSED);
748
749 *pkt_type = pkt.pkt_type;
750 fn_exit:
751 MPIR_FUNC_EXIT;
752 return mpi_errno;
753 fn_fail:
754 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
755 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
756 goto fn_exit;
757 }
758
759
760 /* Initiates connection protocol to remote process. It's OK to call
761 this function with the VC in an error state; an appropriate error
762 code will be returned. */
763 int MPID_nem_tcp_connect(struct MPIDI_VC *const vc)
764 {
765 MPID_nem_tcp_vc_area *const vc_tcp = VC_TCP(vc);
766 sockconn_t *sc = NULL;
767 struct pollfd *plfd = NULL;
768 int idx = -1;
769 int mpi_errno = MPI_SUCCESS;
770 #ifdef HAVE_ERROR_CHECKING
771 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
772 #endif
773
774 MPIR_CHKLMEM_DECL();
775
776 MPIR_FUNC_ENTER;
777
778 MPIR_Assert(vc != NULL);
779
780 /* Handle error case */
781 if (vc_tcp->state == MPID_NEM_TCP_VC_STATE_ERROR || vc->state == MPIDI_VC_STATE_MORIBUND) {
782 MPIR_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**vc_in_error_state");
783 }
784
785 /* We have an active connection, start polling more often */
786 MPID_nem_tcp_skip_polls = MAX_SKIP_POLLS_ACTIVE;
787
788 /* only update VC state when it is not being closed.
789 * Note that we still need change state here if the VC is passively
790 * connected (i.e., server in dynamic process connection) */
791 if (vc->state == MPIDI_VC_STATE_INACTIVE)
792 MPIDI_CHANGE_VC_STATE(vc, ACTIVE);
793
794 if (vc_tcp->state == MPID_NEM_TCP_VC_STATE_DISCONNECTED) {
795 struct sockaddr_in *sock_addr;
796 struct in_addr addr;
797 int rc = 0;
798
799 if (vc_tcp->connect_retry_count > MPIDI_NEM_TCP_MAX_CONNECT_RETRIES) {
800 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "exceeded retries, closing sc");
801 MPIR_ERR_SET1(mpi_errno, MPI_ERR_OTHER, "**exceeded_connect_tries",
802 "**exceeded_connect_tries %d", vc->pg_rank);
803 mpi_errno = error_closed(vc, mpi_errno);
804 MPIR_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**tcp_cleanup_fail");
805 goto fn_fail;
806 }
807
808 ++vc_tcp->connect_retry_count;
809
810 MPIR_Assert(vc_tcp->sc == NULL);
811 mpi_errno = find_free_entry(&idx);
812 MPIR_ERR_CHECK(mpi_errno);
813
814 sc = &g_sc_tbl[idx];
815 plfd = &MPID_nem_tcp_plfd_tbl[idx];
816
817 /* FIXME:
818 * We need to set addr and port using bc.
819 * If a process is dynamically spawned, vc->pg is NULL.
820 * In that case, same procedure is done
821 * in MPID_nem_tcp_connect_to_root()
822 */
823 if (vc->pg != NULL) { /* VC is not a temporary one */
824 char *bc;
825 int val_max_sz;
826
827 val_max_sz = MPIR_pmi_max_val_size();
828
829 MPIR_CHKLMEM_MALLOC(bc, val_max_sz);
830
831 sc->is_tmpvc = FALSE;
832
833 mpi_errno = vc->pg->getConnInfo(vc->pg_rank, bc, val_max_sz, vc->pg);
834 MPIR_ERR_CHECK(mpi_errno);
835
836 mpi_errno = MPID_nem_tcp_get_addr_port_from_bc(bc, &addr, &(vc_tcp->sock_id.sin_port));
837 vc_tcp->sock_id.sin_addr.s_addr = addr.s_addr;
838 MPIR_ERR_CHECK(mpi_errno);
839 } else {
840 sc->is_tmpvc = TRUE;
841 MPIR_Assert(!sc->pg_is_set);
842 }
843
844 sock_addr = &(vc_tcp->sock_id);
845
846 CHECK_EINTR(sc->fd, socket(AF_INET, SOCK_STREAM, 0));
847 MPIR_ERR_CHKANDJUMP2(sc->fd == -1, mpi_errno, MPI_ERR_OTHER, "**sock_create",
848 "**sock_create %s %d", MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE),
849 errno);
850
851 plfd->fd = sc->fd;
852 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
853 (MPL_DBG_FDEST,
854 "sc->fd=%d, plfd->events=%d, plfd->revents=%d, vc=%p, sc=%p", sc->fd,
855 plfd->events, plfd->revents, vc, sc));
856 mpi_errno = MPID_nem_tcp_set_sockopts(sc->fd);
857 MPIR_ERR_CHECK(mpi_errno);
858
859 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
860 (MPL_DBG_FDEST, "connecting to 0x%08X:%d", sock_addr->sin_addr.s_addr,
861 sock_addr->sin_port));
862 rc = connect(sc->fd, (SA *) sock_addr, sizeof(*sock_addr));
863 /* connect should not be called with CHECK_EINTR macro */
864 MPIR_ERR_CHKANDJUMP2(rc < 0 &&
865 errno != EINPROGRESS, mpi_errno, MPI_ERR_OTHER, "**sock_connect",
866 "**sock_connect %d %s", errno,
867 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
868
869 if (rc == 0) {
870 CHANGE_STATE(sc, CONN_STATE_TC_C_CNTD);
871 } else {
872 CHANGE_STATE(sc, CONN_STATE_TC_C_CNTING);
873 }
874
875 vc_tcp->state = MPID_NEM_TCP_VC_STATE_CONNECTED;
876 sc->pg_rank = vc->pg_rank;
877
878 if (vc->pg != NULL) { /* normal (non-dynamic) connection */
879 if (IS_SAME_PGID(vc->pg->id, MPIDI_Process.my_pg->id)) {
880 sc->is_same_pg = TRUE;
881 sc->pg_id = NULL;
882 } else {
883 sc->is_same_pg = FALSE;
884 sc->pg_id = vc->pg->id;
885 }
886 /* very important, without this is_same_connection() will always fail */
887 sc->pg_is_set = TRUE;
888 MPIR_Assert(!sc->is_tmpvc);
889 } else { /* (vc->pg == NULL), dynamic proc connection - temp vc */
890 MPIR_Assert(sc->is_tmpvc);
891 }
892
893 ASSIGN_SC_TO_VC(vc_tcp, sc);
894 sc->vc = vc;
895 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
896 (MPL_DBG_FDEST,
897 "about to incr sc_ref_count sc=%p sc->vc=%p sc_ref_count=%d", sc, sc->vc,
898 vc_tcp->sc_ref_count));
899 ++vc_tcp->sc_ref_count;
900 } else if (vc_tcp->state == MPID_NEM_TCP_VC_STATE_CONNECTED) {
901 sc = vc_tcp->sc;
902 MPIR_Assert(sc != NULL);
903 /* Do nothing here, the caller just needs to wait for the connection
904 * state machine to work its way through the states. Doing something at
905 * this point will almost always just mess up any head-to-head
906 * resolution. */
907 } else {
908 /* We already handled the error case at the top of the routine. */
909 MPIR_Assertp(0);
910 }
911
912 fn_exit:
913 /* MPID_nem_tcp_connpoll(); FIXME-Imp should be called? */
914 MPIR_CHKLMEM_FREEALL();
915 MPIR_FUNC_EXIT;
916 return mpi_errno;
917 fn_fail:
918 if (idx != -1)
919 close_cleanup_and_free_sc_plfd(&g_sc_tbl[idx]); /* ignore error return */
920 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
921 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
922 goto fn_exit;
923 }
924
925 static int cleanup_and_free_sc_plfd(sockconn_t * const sc)
926 {
927 int mpi_errno = MPI_SUCCESS;
928
929 MPIR_FUNC_ENTER;
930
931 if (sc == NULL)
932 goto fn_exit;
933
934 MPIDI_VC_t *const sc_vc = sc->vc;
935 const int idx = sc->index;
936 struct pollfd *const plfd = &MPID_nem_tcp_plfd_tbl[sc->index];
937 freenode_t *node;
938 MPIR_CHKPMEM_DECL();
939
940 if (sc_vc) {
941 MPID_nem_tcp_vc_area *const sc_vc_tcp = VC_TCP(sc_vc);
942
943 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
944 (MPL_DBG_FDEST,
945 "about to decr sc_ref_count sc=%p sc->vc=%p sc_ref_count=%d", sc, sc_vc,
946 sc_vc_tcp->sc_ref_count));
947 MPIR_Assert(sc_vc_tcp->sc_ref_count > 0);
948 --sc_vc_tcp->sc_ref_count;
949
950 if (sc_vc_tcp->sc == sc) { /* this vc may be connecting/accepting with another sc e.g., this sc lost the tie-breaker */
951 sc_vc_tcp->state = MPID_NEM_TCP_VC_STATE_DISCONNECTED;
952 ASSIGN_SC_TO_VC(sc_vc_tcp, NULL);
953 }
954 }
955
956 CHANGE_STATE(sc, CONN_STATE_TS_CLOSED);
957
958 INIT_SC_ENTRY(sc, idx);
959 INIT_POLLFD_ENTRY(plfd);
960
961 MPIR_CHKPMEM_MALLOC(node, sizeof(freenode_t), MPL_MEM_OTHER);
962 node->index = idx;
963 Q_ENQUEUE(&freeq, node);
964
965 MPIR_CHKPMEM_COMMIT();
966 fn_exit:
967 MPIR_FUNC_EXIT;
968 return mpi_errno;
969 fn_fail:
970 MPIR_CHKPMEM_REAP();
971 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
972 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
973 goto fn_exit;
974 }
975
976 /* Called to transition an sc to CLOSED, and free associated
977 resources. This might be done as part of a ch3 close protocol,
978 because the sc lost in a head-to-head connection, or because there
979 was an error associated with the connection. */
980 int close_cleanup_and_free_sc_plfd(sockconn_t * const sc)
981 {
982 int mpi_errno = MPI_SUCCESS;
983 int mpi_errno2 = MPI_SUCCESS;
984 int rc;
985 #ifdef HAVE_ERROR_CHECKING
986 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
987 #endif
988
989 MPIR_FUNC_ENTER;
990
991 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
992 (MPL_DBG_FDEST, "vc=%p, sc=%p, closing fd=%d", sc->vc, sc, sc->fd));
993
994 if (sc == NULL)
995 goto fn_exit;
996
997 CHECK_EINTR(rc, close(sc->fd));
998 if (rc == -1 && errno != EAGAIN && errno != EBADF)
999 MPIR_ERR_SET1(mpi_errno, MPI_ERR_OTHER, "**close", "**close %s",
1000 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
1001
1002 mpi_errno2 = cleanup_and_free_sc_plfd(sc);
1003 if (mpi_errno2)
1004 MPIR_ERR_ADD(mpi_errno, mpi_errno2);
1005
1006 fn_exit:
1007 MPIR_FUNC_EXIT;
1008 return mpi_errno;
1009 }
1010
1011
1012 /* this function is called when vc->state becomes CLOSED or when we need
1013 to clean up after we restart from a checkpoint */
1014 /* FIXME XXX DJG do we need to do anything here to ensure that the final
1015 close(TRUE) packet has made it into a writev call? The code might have a
1016 race for queued messages. */
1017 int MPID_nem_tcp_cleanup(struct MPIDI_VC *const vc)
1018 {
1019 int mpi_errno = MPI_SUCCESS, i;
1020 MPID_nem_tcp_vc_area *const vc_tcp = VC_TCP(vc);
1021
1022 MPIR_FUNC_ENTER;
1023
1024 if (vc_tcp->sc != NULL) {
1025 mpi_errno = close_cleanup_and_free_sc_plfd(vc_tcp->sc);
1026 MPIR_ERR_CHECK(mpi_errno);
1027 }
1028
1029 i = 0;
1030 while (vc_tcp->sc_ref_count > 0 && i < g_tbl_size) {
1031 if (g_sc_tbl[i].vc == vc) {
1032 /* We've found a proto-connection that doesn't yet have enough
1033 * information to resolve the head-to-head situation. If we don't
1034 * clean him up he'll end up accessing the about-to-be-freed vc. */
1035 mpi_errno = close_cleanup_and_free_sc_plfd(&g_sc_tbl[i]);
1036 MPIR_ERR_CHECK(mpi_errno);
1037 MPIR_Assert(g_sc_tbl[i].vc == NULL);
1038 }
1039 ++i;
1040 }
1041
1042 /* close_cleanup_and_free_sc_plfd can technically cause a reconnect on a per-sc basis, but I
1043 * don't think that it can happen when cleanup is called. Let's
1044 * assert this for now and remove it if we prove that it can happen. */
1045 MPIR_Assert(vc_tcp->sc_ref_count == 0);
1046
1047 fn_exit:
1048 MPIR_FUNC_EXIT;
1049 return mpi_errno;
1050 fn_fail:
1051 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1052 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1053 goto fn_exit;
1054 }
1055
1056 /* This cleans up and frees connection resources for all VCs. Note
1057 that we didn't free or close any connections before taking the
1058 checkpoint, so we need to do that on restart (except we don't close
1059 the sockets because they should already be closed). */
1060 int MPID_nem_tcp_ckpt_cleanup(void)
1061 {
1062 int mpi_errno = MPI_SUCCESS;
1063 int i;
1064
1065 MPIR_FUNC_ENTER;
1066
1067 for (i = 0; i < MPIDI_Process.my_pg->size; ++i) {
1068 MPIDI_VC_t *vc;
1069 if (i == MPIDI_Process.my_pg_rank)
1070 continue;
1071 MPIDI_PG_Get_vc(MPIDI_Process.my_pg, i, &vc);
1072 {
1073 MPIDI_CH3I_VC *vc_ch = &vc->ch;
1074 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
1075 if (vc_ch->is_local || vc_tcp->sc == NULL)
1076 continue;
1077
1078 /* cleanup vc */
1079 mpi_errno = cleanup_and_free_sc_plfd(vc_tcp->sc);
1080 MPIR_ERR_CHECK(mpi_errno);
1081 }
1082 }
1083
1084 fn_exit:
1085 MPIR_FUNC_EXIT;
1086 return mpi_errno;
1087 fn_fail:
1088
1089 goto fn_exit;
1090 }
1091
1092
1093 static int state_tc_c_cnting_handler(struct pollfd *const plfd, sockconn_t * const sc)
1094 {
1095 int mpi_errno = MPI_SUCCESS;
1096 int status;
1097
1098 MPIR_FUNC_ENTER;
1099
1100 status = MPID_nem_tcp_check_sock_status(plfd);
1101
1102 if (status == MPID_NEM_TCP_SOCK_CONNECTED) {
1103 CHANGE_STATE(sc, CONN_STATE_TC_C_CNTD);
1104 } else if (status == MPID_NEM_TCP_SOCK_ERROR_EOF) {
1105 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "state_tc_c_cnting_handler(): closing sc");
1106 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1107 } else { /* status == MPID_NEM_TCP_SOCK_NOEVENT */
1108 /*
1109 * Still connecting... let it. While still connecting, even if
1110 * a duplicate connection exists and this connection can be closed, it can get
1111 * tricky. close/shutdown on a unconnected fd will fail anyways. So, let it either
1112 * connect or let the connect itself fail on the fd before making a transition
1113 * from this state. However, we are relying on the time taken by connect to
1114 * report an error. If we want control over that time, fix the code to poll for
1115 * that amount of time or change the socket option to control the time-out of
1116 * connect.
1117 */
1118 }
1119
1120 MPIR_FUNC_EXIT;
1121 return mpi_errno;
1122 }
1123
1124 static int state_tc_c_cntd_handler(struct pollfd *const plfd, sockconn_t * const sc)
1125 {
1126 int mpi_errno = MPI_SUCCESS;
1127
1128 MPIR_FUNC_ENTER;
1129
1130 if (found_better_sc(sc, NULL)) {
1131 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "state_tc_c_cntd_handler(): closing sc");
1132 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1133 goto fn_exit;
1134 }
1135
1136 if (IS_WRITEABLE(plfd)) {
1137 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "inside if (IS_WRITEABLE(plfd))");
1138 if (!sc->is_tmpvc) { /* normal connection */
1139 mpi_errno = send_id_info(sc);
1140 if (mpi_errno) {
1141 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1142 "state_tc_c_cntd_handler(): closing sc");
1143 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1144 mpi_errno = MPI_SUCCESS; /* don't return an error if we fail in the connect protocol, just retry. */
1145 goto fn_exit;
1146 }
1147
1148 CHANGE_STATE(sc, CONN_STATE_TC_C_RANKSENT);
1149 } else { /* temp VC */
1150 mpi_errno = send_tmpvc_info(sc);
1151 MPIR_ERR_CHECK(mpi_errno);
1152
1153 CHANGE_STATE(sc, CONN_STATE_TC_C_TMPVCSENT);
1154 }
1155 } else {
1156 /* Remain in the same state */
1157 }
1158 fn_exit:
1159 MPIR_FUNC_EXIT;
1160 return mpi_errno;
1161 fn_fail:
1162 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1163 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1164 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1165 goto fn_exit;
1166 }
1167
1168 static int state_c_ranksent_handler(struct pollfd *const plfd, sockconn_t * const sc)
1169 {
1170 MPIDI_VC_t *const sc_vc = sc->vc;
1171 MPID_nem_tcp_vc_area *const sc_vc_tcp = VC_TCP(sc_vc);
1172 int mpi_errno = MPI_SUCCESS;
1173 MPIDI_nem_tcp_socksm_pkt_type_t pkt_type;
1174
1175 MPIR_FUNC_ENTER;
1176
1177 if (IS_READABLE(plfd)) {
1178 mpi_errno = recv_cmd_pkt(sc->fd, &pkt_type);
1179 MPIR_ERR_CHECK(mpi_errno);
1180
1181 MPIR_Assert(pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_ACK ||
1182 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK ||
1183 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_CLOSED);
1184
1185 switch (pkt_type) {
1186 case MPIDI_NEM_TCP_SOCKSM_PKT_ID_ACK:
1187 CHANGE_STATE(sc, CONN_STATE_TS_COMMRDY);
1188 ASSIGN_SC_TO_VC(sc_vc_tcp, sc);
1189
1190 MPID_nem_tcp_conn_est(sc_vc);
1191 sc_vc_tcp->connect_retry_count = 0; /* successfully connected, reset connection retry count */
1192 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1193 (MPL_DBG_FDEST,
1194 "c_ranksent_handler(): connection established (sc=%p, sc->vc=%p, fd=%d)",
1195 sc, sc->vc, sc->fd));
1196 break;
1197 case MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK:
1198 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "received NAK, closing sc");
1199 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1200 break;
1201 case MPIDI_NEM_TCP_SOCKSM_PKT_CLOSED:
1202 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "received CLOSED, closing sc");
1203 mpi_errno = MPIDI_CH3U_Handle_connection(sc_vc, MPIDI_VC_EVENT_TERMINATED);
1204 MPIR_ERR_CHECK(mpi_errno);
1205 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1206 break;
1207 default:
1208 MPIR_Assert(0);
1209 }
1210 }
1211
1212 fn_exit:
1213 MPIR_FUNC_EXIT;
1214 return mpi_errno;
1215 fn_fail:
1216 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1217 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1218 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1219 mpi_errno = MPI_SUCCESS; /* don't return an error if we fail in the connect protocol, just retry. */
1220 goto fn_exit;
1221 }
1222
1223 static int state_c_tmpvcsent_handler(struct pollfd *const plfd, sockconn_t * const sc)
1224 {
1225 MPIDI_VC_t *const sc_vc = sc->vc;
1226 MPID_nem_tcp_vc_area *const sc_vc_tcp = VC_TCP(sc_vc);
1227 int mpi_errno = MPI_SUCCESS;
1228 MPIDI_nem_tcp_socksm_pkt_type_t pkt_type;
1229
1230 MPIR_FUNC_ENTER;
1231
1232 if (IS_READABLE(plfd)) {
1233 mpi_errno = recv_cmd_pkt(sc->fd, &pkt_type);
1234 if (mpi_errno) {
1235 int mpi_errno2 = MPI_SUCCESS;
1236 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "error sending cmd pkt, closing sc");
1237 mpi_errno2 = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1238 if (mpi_errno2) {
1239 MPIR_ERR_SET(mpi_errno2, MPI_ERR_OTHER, "**tcp_cleanup_fail");
1240 if (mpi_errno2)
1241 MPIR_ERR_ADD(mpi_errno, mpi_errno2);
1242 }
1243 MPIR_ERR_SET(mpi_errno, MPI_ERR_OTHER, "**tmpvc_connect_fail");
1244 /* no head-to-head issues to deal with, if we failed to recv the
1245 * packet then there really was a problem */
1246 } else {
1247 MPIR_Assert(pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_ACK ||
1248 pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_NAK);
1249
1250 if (pkt_type == MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_ACK) {
1251 CHANGE_STATE(sc, CONN_STATE_TS_COMMRDY);
1252 ASSIGN_SC_TO_VC(sc_vc_tcp, sc);
1253 MPID_nem_tcp_conn_est(sc_vc);
1254 sc_vc_tcp->connect_retry_count = 0; /* successfully connected, reset connection retry count */
1255 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1256 (MPL_DBG_FDEST,
1257 "c_tmpvcsent_handler(): connection established (fd=%d, sc=%p, sc->vc=%p)",
1258 sc->fd, sc, sc_vc));
1259 } else { /* pkt_type must be MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK */
1260 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1261 (MPL_DBG_FDEST, "state_c_tmpvcsent_handler() 2: closing sc"));
1262 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "received NAK, closing sc");
1263 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1264 }
1265 }
1266 }
1267
1268 MPIR_FUNC_EXIT;
1269 return mpi_errno;
1270 }
1271
1272 static int state_l_cntd_handler(struct pollfd *const plfd, sockconn_t * const sc)
1273 {
1274 int mpi_errno = MPI_SUCCESS;
1275 int status;
1276 int got_sc_eof = 0;
1277
1278 MPIR_FUNC_ENTER;
1279
1280 status = MPID_nem_tcp_check_sock_status(plfd);
1281 if (status == MPID_NEM_TCP_SOCK_ERROR_EOF) {
1282 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "Received EOF, closing sc");
1283 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1284 goto fn_exit;
1285 }
1286
1287 /* We have an active connection, start polling more often */
1288 MPID_nem_tcp_skip_polls = MAX_SKIP_POLLS_ACTIVE;
1289
1290 if (IS_READABLE(plfd)) {
1291 mpi_errno = recv_id_or_tmpvc_info(sc, &got_sc_eof);
1292 MPIR_ERR_CHECK(mpi_errno);
1293
1294 if (got_sc_eof) {
1295 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "got eof, closing sc");
1296 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1297 goto fn_exit;
1298 }
1299
1300 if (!sc->is_tmpvc) {
1301 CHANGE_STATE(sc, CONN_STATE_TA_C_RANKRCVD);
1302 } else {
1303 CHANGE_STATE(sc, CONN_STATE_TA_C_TMPVCRCVD);
1304 }
1305 } else {
1306 /* remain in same state */
1307 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1308 (MPL_DBG_FDEST, "!IS_READABLE(plfd) fd=%d events=%#x revents=%#x", plfd->fd,
1309 plfd->events, plfd->revents));
1310 }
1311
1312 fn_exit:
1313 MPIR_FUNC_EXIT;
1314 return mpi_errno;
1315 fn_fail:
1316 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1317 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1318 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1319 mpi_errno = MPI_SUCCESS; /* don't return an error if we fail in the connect protocol, just retry. */
1320 goto fn_exit;
1321 }
1322
1323 /*
1324 Returns TRUE, if the process(self) wins against the remote process
1325 FALSE, otherwise
1326 */
1327 static int do_i_win(sockconn_t * rmt_sc)
1328 {
1329 int win = FALSE;
1330
1331 MPIR_FUNC_ENTER;
1332
1333 MPIR_Assert(rmt_sc->pg_is_set);
1334
1335 if (rmt_sc->is_same_pg) {
1336 if (MPIDI_Process.my_pg_rank > rmt_sc->pg_rank)
1337 win = TRUE;
1338 } else {
1339 if (strcmp(MPIDI_Process.my_pg->id, rmt_sc->pg_id) > 0)
1340 win = TRUE;
1341 }
1342
1343 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1344 (MPL_DBG_FDEST,
1345 "do_i_win(rmt_sc=%p (%s)) win=%s is_same_pg=%s my_pg_rank=%d rmt_pg_rank=%d",
1346 rmt_sc, CONN_STATE_STR[rmt_sc->state.cstate], (win ? "TRUE" : "FALSE"),
1347 (rmt_sc->is_same_pg ? "TRUE" : "FALSE"), MPIDI_Process.my_pg_rank,
1348 rmt_sc->pg_rank));
1349 MPIR_FUNC_EXIT;
1350 return win;
1351 }
1352
1353 static int state_l_rankrcvd_handler(struct pollfd *const plfd, sockconn_t * const sc)
1354 {
1355 MPIDI_VC_t *const sc_vc = sc->vc;
1356 MPID_nem_tcp_vc_area *const sc_vc_tcp = VC_TCP(sc_vc);
1357 int mpi_errno = MPI_SUCCESS;
1358 int status;
1359 sockconn_t *fnd_sc = NULL;
1360 int snd_nak = FALSE;
1361
1362 MPIR_FUNC_ENTER;
1363
1364 status = MPID_nem_tcp_check_sock_status(plfd);
1365 if (status == MPID_NEM_TCP_SOCK_ERROR_EOF)
1366 goto fn_fail;
1367
1368 if (IS_WRITEABLE(plfd)) {
1369
1370 if (sc_vc->state == MPIDI_VC_STATE_INACTIVE_CLOSED) {
1371 mpi_errno = send_cmd_pkt(sc->fd, MPIDI_NEM_TCP_SOCKSM_PKT_CLOSED);
1372 MPIR_ERR_CHECK(mpi_errno);
1373 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1374 }
1375
1376 if (found_better_sc(sc, &fnd_sc)) {
1377 if (fnd_sc->state.cstate == CONN_STATE_TS_COMMRDY)
1378 snd_nak = TRUE;
1379 else if (fnd_sc->state.cstate == CONN_STATE_TC_C_RANKSENT)
1380 snd_nak = do_i_win(sc);
1381 }
1382
1383 if (snd_nak) {
1384 mpi_errno = send_cmd_pkt(sc->fd, MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK);
1385 MPIR_ERR_CHECK(mpi_errno);
1386 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "lost head-to-head, closing sc");
1387 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1388 } else {
1389 /* The following line is _crucial_ to correct operation. We need to
1390 * ensure that all head-to-head resolution has completed before we
1391 * move to COMMRDY and send any pending messages. If we don't this
1392 * connection could shut down before the other connection has a
1393 * chance to finish the connect protocol. That can lead to all
1394 * kinds of badness, including zombie connections, segfaults, and
1395 * accessing PG/VC info that is no longer present. */
1396 if (sc_vc_tcp->sc_ref_count > 1)
1397 goto fn_exit;
1398
1399 mpi_errno = send_cmd_pkt(sc->fd, MPIDI_NEM_TCP_SOCKSM_PKT_ID_ACK);
1400 MPIR_ERR_CHECK(mpi_errno);
1401
1402 CHANGE_STATE(sc, CONN_STATE_TS_COMMRDY);
1403 ASSIGN_SC_TO_VC(sc_vc_tcp, sc);
1404 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1405 (MPL_DBG_FDEST,
1406 "connection established: sc=%p, sc->vc=%p, sc->fd=%d, is_same_pg=%s, pg_rank=%d",
1407 sc, sc_vc, sc->fd, (sc->is_same_pg ? "TRUE" : "FALSE"), sc->pg_rank));
1408 MPID_nem_tcp_conn_est(sc_vc);
1409 sc_vc_tcp->connect_retry_count = 0; /* successfully connected, reset connection retry count */
1410 }
1411 }
1412
1413 fn_exit:
1414 MPIR_FUNC_EXIT;
1415 return mpi_errno;
1416 fn_fail:
1417 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1418 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1419 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1420 mpi_errno = MPI_SUCCESS; /* don't return an error if we fail in the connect protocol, just retry. */
1421 goto fn_exit;
1422 }
1423
1424 static int state_l_tmpvcrcvd_handler(struct pollfd *const plfd, sockconn_t * const sc)
1425 {
1426 MPIDI_VC_t *const sc_vc = sc->vc;
1427 MPID_nem_tcp_vc_area *const sc_vc_tcp = VC_TCP(sc_vc);
1428 int mpi_errno = MPI_SUCCESS;
1429 int status;
1430
1431 MPIR_FUNC_ENTER;
1432
1433 status = MPID_nem_tcp_check_sock_status(plfd);
1434 if (status == MPID_NEM_TCP_SOCK_ERROR_EOF) {
1435 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "Received EOF, closing sc");
1436 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1437 goto fn_exit;
1438 }
1439 /* we don't want to perform any h2h resolution for temp vcs */
1440 if (IS_WRITEABLE(plfd)) {
1441 mpi_errno = send_cmd_pkt(sc->fd, MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_ACK);
1442 MPIR_ERR_CHECK(mpi_errno);
1443
1444 CHANGE_STATE(sc, CONN_STATE_TS_COMMRDY);
1445 ASSIGN_SC_TO_VC(sc_vc_tcp, sc);
1446 MPID_nem_tcp_conn_est(sc_vc);
1447 sc_vc_tcp->connect_retry_count = 0; /* successfully connected, reset connection retry count */
1448 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1449 (MPL_DBG_FDEST, "fd=%d: TMPVC_ACK sent, connection established!", sc->fd));
1450 }
1451
1452 fn_exit:
1453 MPIR_FUNC_EXIT;
1454 return mpi_errno;
1455 fn_fail:
1456 {
1457 int cleanup_errno = MPI_SUCCESS;
1458 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1459 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1460 mpi_errno = close_cleanup_and_free_sc_plfd(sc); /* QUIESCENT */
1461 if (cleanup_errno) {
1462 MPIR_ERR_SET(cleanup_errno, MPI_ERR_OTHER, "**tcp_cleanup_fail");
1463 MPIR_ERR_ADD(mpi_errno, cleanup_errno);
1464 }
1465 goto fn_exit;
1466 }
1467 }
1468
1469 static int MPID_nem_tcp_recv_handler(sockconn_t * const sc)
1470 {
1471 /* as a result of handling a message, the sc table might get
1472 * expanded making the sc pointer invalid, so we save all the info
1473 * we need from sc here, and don't reference sc directly after
1474 * that. */
1475 const int sc_fd = sc->fd;
1476 MPIDI_VC_t *const sc_vc = sc->vc;
1477 MPIR_AssertDeclValue(MPID_nem_tcp_vc_area * const sc_vc_tcp, VC_TCP(sc_vc));
1478 int mpi_errno = MPI_SUCCESS;
1479 ssize_t bytes_recvd;
1480 #ifdef HAVE_ERROR_CHECKING
1481 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
1482 #endif
1483
1484 MPIR_FUNC_ENTER;
1485
1486 if (sc_vc->ch.recv_active == NULL) {
1487 /* receive a new message */
1488 CHECK_EINTR(bytes_recvd, recv(sc_fd, recv_buf, MPID_NEM_TCP_RECV_MAX_PKT_LEN, 0));
1489 if (bytes_recvd <= 0) {
1490 if (bytes_recvd == -1 && errno == EAGAIN) /* handle this fast */
1491 goto fn_exit;
1492
1493 if (bytes_recvd == 0) {
1494 MPIR_Assert(sc != NULL);
1495 MPIR_Assert(sc_vc != NULL);
1496 /* sc->vc->sc will be NULL if sc->vc->state == _INACTIVE */
1497 MPIR_Assert(sc_vc_tcp->sc == NULL || sc_vc_tcp->sc == sc);
1498
1499 if (vc_is_in_shutdown(sc_vc)) {
1500 /* there's currently no hook for CH3 to tell nemesis/tcp
1501 * that we are in the middle of a disconnection dance. So
1502 * if we don't check to see if we are currently
1503 * disconnecting, then we end up with a potential race where
1504 * the other side performs a tcp close() before we do and we
1505 * blow up here. */
1506 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1507 "other side closed, but we're shutting down, closing sc");
1508 /* it's really not an error, but we're calling
1509 * cleanup_on_error because it does what we want it to */
1510 mpi_errno = MPID_nem_tcp_cleanup_on_error(sc_vc, MPI_SUCCESS);
1511 goto fn_exit;
1512 } else {
1513 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE,
1514 "ERROR: sock (fd=%d) is closed: bytes_recvd == 0", sc->fd);
1515 MPIR_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**sock_closed");
1516 }
1517 } else {
1518 MPIR_ERR_SETANDJUMP1(mpi_errno, MPI_ERR_OTHER, "**read", "**read %s",
1519 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
1520 }
1521 }
1522
1523 MPL_DBG_MSG_FMT(MPIDI_CH3_DBG_CHANNEL, VERBOSE,
1524 (MPL_DBG_FDEST, "New recv %" PRIdPTR " (fd=%d, vc=%p, sc=%p)", bytes_recvd,
1525 sc_fd, sc_vc, sc));
1526
1527 mpi_errno = MPID_nem_handle_pkt(sc_vc, recv_buf, bytes_recvd);
1528 if (mpi_errno)
1529 MPIR_ERR_POP_LABEL(mpi_errno, fn_noncomm_fail);
1530 } else {
1531 /* there is a pending receive, receive it directly into the user buffer */
1532 MPIDI_CH3I_VC *const sc_vc_ch = &sc_vc->ch;
1533 MPIR_Request *const rreq = sc_vc_ch->recv_active;
1534 struct iovec *iov = &rreq->dev.iov[rreq->dev.iov_offset];
1535 int (*reqFn) (MPIDI_VC_t *, MPIR_Request *, int *);
1536
1537 MPIR_Assert(rreq->dev.iov_count > 0);
1538
1539 bytes_recvd = MPL_large_readv(sc_fd, iov, rreq->dev.iov_count);
1540 if (bytes_recvd <= 0) {
1541 if (bytes_recvd == -1 && errno == EAGAIN) /* handle this fast */
1542 goto fn_exit;
1543
1544 if (bytes_recvd == 0) {
1545 MPIR_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**sock_closed");
1546 } else {
1547 MPIR_ERR_SETANDJUMP1(mpi_errno, MPI_ERR_OTHER, "**read", "**read %s",
1548 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
1549 }
1550 }
1551
1552 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "Cont recv %ld", (long int) bytes_recvd);
1553
1554 /* update the iov */
1555 for (iov = &rreq->dev.iov[rreq->dev.iov_offset];
1556 iov < &rreq->dev.iov[rreq->dev.iov_offset + rreq->dev.iov_count]; ++iov) {
1557 if (bytes_recvd < iov->iov_len) {
1558 iov->iov_base = (char *) iov->iov_base + bytes_recvd;
1559 iov->iov_len -= bytes_recvd;
1560 rreq->dev.iov_count =
1561 (int) (&rreq->dev.iov[rreq->dev.iov_offset + rreq->dev.iov_count] - iov);
1562 rreq->dev.iov_offset = iov - rreq->dev.iov;
1563 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "bytes_recvd = %ld",
1564 (long int) bytes_recvd);
1565 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "iov len = %ld",
1566 (long int) iov->iov_len);
1567 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "iov_offset = %lld",
1568 (long long) rreq->dev.iov_offset);
1569 goto fn_exit;
1570 }
1571 bytes_recvd -= iov->iov_len;
1572 }
1573
1574 /* the whole iov has been received */
1575
1576 reqFn = rreq->dev.OnDataAvail;
1577 if (!reqFn) {
1578 MPIR_Assert(MPIDI_Request_get_type(rreq) != MPIDI_REQUEST_TYPE_GET_RESP);
1579 mpi_errno = MPID_Request_complete(rreq);
1580 MPIR_ERR_CHECK(mpi_errno);
1581 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "...complete");
1582 sc_vc_ch->recv_active = NULL;
1583 } else {
1584 int complete = 0;
1585
1586 mpi_errno = reqFn(sc_vc, rreq, &complete);
1587 if (mpi_errno)
1588 MPIR_ERR_POP_LABEL(mpi_errno, fn_noncomm_fail);
1589
1590 if (complete) {
1591 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "...complete");
1592 sc_vc_ch->recv_active = NULL;
1593 } else {
1594 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "...not complete");
1595 }
1596 }
1597 }
1598
1599 fn_exit:
1600 MPIR_FUNC_EXIT;
1601 return mpi_errno;
1602 fn_fail: /* comm related failures jump here */
1603 {
1604
1605 MPIR_ERR_SET1(mpi_errno, MPIX_ERR_PROC_FAILED, "**comm_fail", "**comm_fail %d",
1606 sc_vc->pg_rank);
1607 mpi_errno = MPID_nem_tcp_cleanup_on_error(sc_vc, mpi_errno);
1608 if (mpi_errno) {
1609 MPIR_ERR_SET(mpi_errno, MPI_ERR_OTHER, "**tcp_cleanup_fail");
1610 }
1611 }
1612 fn_noncomm_fail: /* NON-comm related failures jump here */
1613 goto fn_exit;
1614
1615 }
1616
1617 static int state_commrdy_handler(struct pollfd *const plfd, sockconn_t * const sc)
1618 {
1619 int mpi_errno = MPI_SUCCESS;
1620 /* save references to sc->vc etc because if the sc table is
1621 * expanded inside recv_handler, this pointer to sc will be
1622 * invalid. */
1623 MPIDI_VC_t *sc_vc = sc->vc;
1624 MPID_nem_tcp_vc_area *sc_vc_tcp = VC_TCP(sc_vc);
1625
1626 MPIR_FUNC_ENTER;
1627
1628 MPL_DBG_MSG_P(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "vc = %p", sc->vc);
1629 if (IS_READABLE(plfd)) {
1630 mpi_errno = MPID_nem_tcp_recv_handler(sc);
1631 MPIR_ERR_CHECK(mpi_errno);
1632 }
1633 if (IS_WRITEABLE(plfd) && !MPIDI_CH3I_Sendq_empty(sc_vc_tcp->send_queue)) {
1634 mpi_errno = MPID_nem_tcp_send_queued(sc_vc, &sc_vc_tcp->send_queue);
1635 MPIR_ERR_CHECK(mpi_errno);
1636 /* check to see if this VC is waiting for outstanding sends to
1637 * finish in order to terminate */
1638 if (sc_vc->state == MPIDI_VC_STATE_CLOSED && MPIDI_CH3I_Sendq_empty(sc_vc_tcp->send_queue)) {
1639 mpi_errno = MPID_nem_tcp_vc_terminated(sc_vc);
1640 MPIR_ERR_CHECK(mpi_errno);
1641 }
1642 }
1643 fn_exit:
1644 MPIR_FUNC_EXIT;
1645 return mpi_errno;
1646 fn_fail:
1647 goto fn_exit;
1648
1649 }
1650
1651 int MPID_nem_tcp_sm_init(void)
1652 {
1653 int mpi_errno = MPI_SUCCESS;
1654 MPIR_CHKPMEM_DECL();
1655 /* Set the appropriate handlers */
1656 sc_state_info[CONN_STATE_TS_CLOSED].sc_state_handler = NULL;
1657 sc_state_info[CONN_STATE_TC_C_CNTING].sc_state_handler = state_tc_c_cnting_handler;
1658 sc_state_info[CONN_STATE_TC_C_CNTD].sc_state_handler = state_tc_c_cntd_handler;
1659 sc_state_info[CONN_STATE_TC_C_RANKSENT].sc_state_handler = state_c_ranksent_handler;
1660 sc_state_info[CONN_STATE_TC_C_TMPVCSENT].sc_state_handler = state_c_tmpvcsent_handler;
1661 sc_state_info[CONN_STATE_TA_C_CNTD].sc_state_handler = state_l_cntd_handler;
1662 sc_state_info[CONN_STATE_TA_C_RANKRCVD].sc_state_handler = state_l_rankrcvd_handler;
1663 sc_state_info[CONN_STATE_TA_C_TMPVCRCVD].sc_state_handler = state_l_tmpvcrcvd_handler;
1664 sc_state_info[CONN_STATE_TS_COMMRDY].sc_state_handler = state_commrdy_handler;
1665
1666 /* Set the appropriate states */
1667 sc_state_info[CONN_STATE_TS_CLOSED].sc_state_plfd_events = 0;
1668 sc_state_info[CONN_STATE_TC_C_CNTING].sc_state_plfd_events = POLLOUT | POLLIN;
1669 sc_state_info[CONN_STATE_TC_C_CNTD].sc_state_plfd_events = POLLOUT | POLLIN;
1670 sc_state_info[CONN_STATE_TC_C_RANKSENT].sc_state_plfd_events = POLLIN;
1671 sc_state_info[CONN_STATE_TC_C_TMPVCSENT].sc_state_plfd_events = POLLIN;
1672 sc_state_info[CONN_STATE_TA_C_CNTD].sc_state_plfd_events = POLLIN;
1673 sc_state_info[CONN_STATE_TA_C_RANKRCVD].sc_state_plfd_events = POLLOUT | POLLIN;
1674 sc_state_info[CONN_STATE_TA_C_TMPVCRCVD].sc_state_plfd_events = POLLOUT | POLLIN;
1675 sc_state_info[CONN_STATE_TS_COMMRDY].sc_state_plfd_events = POLLIN;
1676
1677 /* Allocate the PLFD table */
1678 g_tbl_size = 0;
1679 g_tbl_capacity = CONN_PLFD_TBL_INIT_SIZE;
1680 g_sc_tbl = NULL;
1681 MPID_nem_tcp_plfd_tbl = NULL;
1682 alloc_sc_plfd_tbls();
1683
1684 MPIR_CHKPMEM_MALLOC(recv_buf, MPID_NEM_TCP_RECV_MAX_PKT_LEN, MPL_MEM_BUFFER);
1685 MPIR_CHKPMEM_COMMIT();
1686
1687 fn_exit:
1688 return mpi_errno;
1689 fn_fail:
1690 MPIR_CHKPMEM_REAP();
1691 goto fn_exit;
1692 }
1693
1694 int MPID_nem_tcp_sm_finalize(void)
1695 {
1696 freenode_t *node;
1697
1698 /* walk the freeq and free all the elements */
1699 while (!Q_EMPTY(freeq)) {
1700 Q_DEQUEUE(&freeq, ((freenode_t **) & node));
1701 MPL_free(node);
1702 }
1703
1704 free_sc_plfd_tbls();
1705
1706 MPL_free(recv_buf);
1707
1708 return MPI_SUCCESS;
1709 }
1710
1711 /*
1712 N1: create a new listener fd?? While doing so, if we bind it to the same port used before,
1713 then it is ok. Else,the new port number(and thus the business card) has to be communicated
1714 to the other processes (in same and different pg's), which is not quite simple to do.
1715 Evaluate the need for it by testing and then do it, if needed.
1716
1717 */
1718 int MPID_nem_tcp_connpoll(int in_blocking_poll)
1719 {
1720 int mpi_errno = MPI_SUCCESS, n, i;
1721 static int num_skipped_polls = 0;
1722
1723 /* num_polled is needed b/c the call to it_sc->handler() can change the
1724 * size of the table, which leads to iterating over invalid revents. */
1725 int num_polled = g_tbl_size;
1726 #ifdef HAVE_ERROR_CHECKING
1727 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
1728 #endif
1729
1730 if (num_polled) {
1731 MPIR_Assert(MPID_nem_tcp_plfd_tbl != NULL);
1732 MPIR_Assert(g_sc_tbl != NULL);
1733 }
1734
1735 /* To improve shared memory performance, we don't call the poll()
1736 * system call every time. The MPID_nem_tcp_skip_polls value is
1737 * changed depending on whether we have any active connections.
1738 * We only skip polls when we're in a blocking progress loop in
1739 * order to avoid poor performance if the user does a "MPI_Test();
1740 * compute();" loop waiting for a req to complete. */
1741 if (in_blocking_poll && num_skipped_polls++ < MPID_nem_tcp_skip_polls)
1742 goto fn_exit;
1743 num_skipped_polls = 0;
1744
1745 CHECK_EINTR(n, poll(MPID_nem_tcp_plfd_tbl, num_polled, 0));
1746 MPIR_ERR_CHKANDJUMP1(n == -1, mpi_errno, MPI_ERR_OTHER, "**poll", "**poll %s",
1747 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
1748 /* MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, (MPL_DBG_FDEST, "some sc fd poll event")); */
1749 for (i = 0; i < num_polled; i++) {
1750 struct pollfd *it_plfd = &MPID_nem_tcp_plfd_tbl[i];
1751 sockconn_t *it_sc = &g_sc_tbl[i];
1752
1753 if (it_plfd->fd != CONN_INVALID_FD && it_plfd->revents != 0) {
1754 /* We could check for POLLHUP here, but HUP/HUP+EOF is not erroneous
1755 * on many platforms, including modern Linux. */
1756 if (it_plfd->revents & POLLERR || it_plfd->revents & POLLNVAL) {
1757 int req_errno = MPI_SUCCESS;
1758
1759 /* See if we can get a specific error for this fd
1760 * (Stevens Network Programming Vol 1, pg 184) */
1761 #ifdef HAVE_ERROR_CHECKING
1762 ssize_t rc;
1763 char dummy;
1764 const char *err_str = "UNKNOWN";
1765 rc = read(it_plfd->fd, &dummy, 1);
1766 if (rc < 0)
1767 err_str = MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE);
1768 #endif
1769 MPL_DBG_MSG(MPIDI_NEM_TCP_DBG_DET, VERBOSE, "error polling fd, closing sc");
1770 if (it_sc->vc) {
1771 MPIR_ERR_SET2(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail",
1772 "**comm_fail %d %s", it_sc->vc->pg_rank, err_str);
1773 mpi_errno = MPID_nem_tcp_cleanup_on_error(it_sc->vc, req_errno);
1774 MPIR_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**tcp_cleanup_fail");
1775 } else {
1776 MPIR_ERR_SET2(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail_conn",
1777 "**comm_fail_conn %s %s", CONN_STATE_STR[it_sc->state.cstate],
1778 err_str);
1779 mpi_errno = close_cleanup_and_free_sc_plfd(it_sc);
1780 MPIR_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**tcp_cleanup_fail");
1781 }
1782 continue;
1783 }
1784
1785 mpi_errno = it_sc->handler(it_plfd, it_sc);
1786 MPIR_ERR_CHECK(mpi_errno);
1787 /* note that g_sc_tbl might have been expanded while
1788 * inside handler, so the it_plfd and it_sc pointers may
1789 * be invalid at this point. */
1790 }
1791 }
1792
1793 fn_exit:
1794 return mpi_errno;
1795 fn_fail:
1796 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1797 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1798 goto fn_exit;
1799 }
1800
1801 /*
1802 FIXME
1803 1.Check for accept error.
1804 2.If listening socket dies, create a new listening socket so that future connects
1805 can use that. In that case, how to tell other processes about the new listening port. It's
1806 probably a design issue at MPI level.
1807
1808 N1: There might be a timing window where poll on listen_fd was successful and there was
1809 a connection in the queue. Assume there was only one connection in the listen queue and
1810 before we called accept, the peer had reset the connection. On receiving a TCP RST,
1811 some implementations remove the connection from the listen queue. So, accept on
1812 a non-blocking fd would return error with EWOULDBLOCK.
1813
1814 N2: The peer might have reset or closed the connection. In some implementations,
1815 even if the connection is reset by the peer, accept is successful. By POSIX standard,
1816 if the connection is closed by the peer, accept will still be successful. So, soon
1817 after accept, check whether the new fd is really connected (i.e neither reset nor
1818 EOF received from peer).
1819 Now, it is decided not to check for this condition at this point. After all, in the next
1820 state, anyhow we can close the socket, if we receive an EOF.
1821
1822 N3: find_free_entry is called within the while loop. It may cause the table to expand. So,
1823 the arguments passed for this callback function may get invalidated. So, it is imperative
1824 that we obtain sc pointer and plfd pointer every time within the while loop.
1825 Accordingly, the parameters are named unused1 and unused2 for clarity.
1826 */
1827 int MPID_nem_tcp_state_listening_handler(struct pollfd *const unused_1, sockconn_t * const unused_2)
1828 /* listener fd poll struct and sockconn structure */
1829 {
1830 int mpi_errno = MPI_SUCCESS;
1831 int connfd;
1832 socklen_t len;
1833 SA_IN rmt_addr;
1834 sockconn_t *l_sc;
1835 #ifdef HAVE_ERROR_CHECKING
1836 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
1837 #endif
1838
1839 MPIR_FUNC_ENTER;
1840
1841 while (1) {
1842 l_sc = &g_sc_tbl[0]; /* N3 Important */
1843 len = sizeof(SA_IN);
1844 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, (MPL_DBG_FDEST, "before accept"));
1845 if ((connfd = accept(l_sc->fd, (SA *) & rmt_addr, &len)) < 0) {
1846 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1847 (MPL_DBG_FDEST,
1848 "after accept, l_sc=%p lstnfd=%d connfd=%d, errno=%d:%s ", l_sc,
1849 l_sc->fd, connfd, errno,
1850 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)));
1851 if (errno == EINTR)
1852 continue;
1853 else if (errno == EWOULDBLOCK || errno == EAGAIN)
1854 break; /* no connection in the listen queue. get out of here.(N1) */
1855
1856 MPIR_ERR_SETANDJUMP1(mpi_errno, MPI_ERR_OTHER, "**sock_accept", "**sock_accept %s",
1857 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
1858 } else {
1859 int idx = -1;
1860 struct pollfd *plfd;
1861 sockconn_t *sc;
1862
1863 MPID_nem_tcp_set_sockopts(connfd); /* (N2) */
1864 mpi_errno = find_free_entry(&idx);
1865 MPIR_ERR_CHECK(mpi_errno);
1866 sc = &g_sc_tbl[idx];
1867 plfd = &MPID_nem_tcp_plfd_tbl[idx];
1868
1869 sc->fd = plfd->fd = connfd;
1870 sc->pg_rank = CONN_INVALID_RANK;
1871 sc->pg_is_set = FALSE;
1872 sc->is_tmpvc = 0;
1873
1874 CHANGE_STATE(sc, CONN_STATE_TA_C_CNTD);
1875
1876 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, (MPL_DBG_FDEST, "accept success, added to table, connfd=%d, sc->vc=%p, sc=%p plfd->events=%#x", connfd, sc->vc, sc, plfd->events)); /* sc->vc should be NULL at this point */
1877 }
1878 }
1879
1880 fn_exit:
1881 MPIR_FUNC_EXIT;
1882 return mpi_errno;
1883 fn_fail:
1884 MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE,
1885 (MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno));
1886 goto fn_exit;
1887 }
1888
1889 /* This is called whenever a vc has been closed as a result of an
1890 error. The VC is put into an error state and cannot be opened
1891 again. */
1892 static int error_closed(struct MPIDI_VC *const vc, int req_errno)
1893 {
1894 int mpi_errno = MPI_SUCCESS;
1895 MPID_nem_tcp_vc_area *const vc_tcp = VC_TCP(vc);
1896
1897 MPIR_FUNC_ENTER;
1898
1899 vc_tcp->state = MPID_NEM_TCP_VC_STATE_ERROR;
1900
1901 mpi_errno = MPIDI_CH3U_Handle_connection(vc, MPIDI_VC_EVENT_TERMINATED);
1902 MPIR_ERR_CHECK(mpi_errno);
1903
1904 mpi_errno = MPID_nem_tcp_error_out_send_queue(vc, req_errno);
1905 MPIR_ERR_CHECK(mpi_errno);
1906
1907 fn_exit:
1908 MPIR_FUNC_EXIT;
1909 return mpi_errno;
1910 fn_fail:
1911 goto fn_exit;
1912 }
1913
1914 /* This is called when an communication error has occurred on a VC to
1915 close the VC and release associated resources.
1916 Any outstanding requests will have MPI_ERROR set to req_errno */
1917 int MPID_nem_tcp_cleanup_on_error(MPIDI_VC_t * const vc, int req_errno)
1918 {
1919 int mpi_errno = MPI_SUCCESS;
1920 int mpi_errno2 = MPI_SUCCESS;
1921
1922 MPIR_FUNC_ENTER;
1923
1924 mpi_errno = MPID_nem_tcp_cleanup(vc);
1925 /* not jumping on error, keep going */
1926
1927 mpi_errno2 = error_closed(vc, req_errno);
1928 if (mpi_errno2)
1929 MPIR_ERR_ADD(mpi_errno, mpi_errno2);
1930
1931 MPIR_FUNC_EXIT;
1932 return mpi_errno;
1933 }