Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #include "tcp_impl.h"
7
8 #define NUM_PREALLOC_SENDQ 10
9 #define MAX_SEND_IOV 10
10
11 typedef struct MPID_nem_tcp_send_q_element {
12 struct MPID_nem_tcp_send_q_element *next;
13 size_t len; /* number of bytes left to send */
14 char *start; /* pointer to next byte to send */
15 MPID_nem_cell_ptr_t cell;
16 } MPID_nem_tcp_send_q_element_t;
17
18 static struct {
19 MPID_nem_tcp_send_q_element_t *top;
20 } free_buffers = {
21 0};
22
23 #define ALLOC_Q_ELEMENT(e) do { \
24 if (S_EMPTY (free_buffers)) \
25 { \
26 MPIR_CHKPMEM_MALLOC (*(e), sizeof(MPID_nem_tcp_send_q_element_t), MPL_MEM_BUFFER); \
27 } \
28 else \
29 { \
30 S_POP (&free_buffers, e); \
31 } \
32 } while (0)
33
34 /* FREE_Q_ELEMENTS() frees a list if elements starting at e0 through e1 */
35 #define FREE_Q_ELEMENTS(e0, e1) S_PUSH_MULTIPLE (&free_buffers, e0, e1)
36 #define FREE_Q_ELEMENT(e) S_PUSH (&free_buffers, e)
37
38 int MPID_nem_tcp_send_init(void)
39 {
40 int mpi_errno = MPI_SUCCESS;
41 int i;
42 MPIR_CHKPMEM_DECL();
43
44 /* preallocate sendq elements */
45 for (i = 0; i < NUM_PREALLOC_SENDQ; ++i) {
46 MPID_nem_tcp_send_q_element_t *e;
47
48 MPIR_CHKPMEM_MALLOC(e, sizeof(MPID_nem_tcp_send_q_element_t), MPL_MEM_BUFFER);
49 S_PUSH(&free_buffers, e);
50 }
51
52 MPIR_CHKPMEM_COMMIT();
53 return mpi_errno;
54 fn_fail:
55 MPIR_CHKPMEM_REAP();
56 return mpi_errno;
57 }
58
59 int MPID_nem_tcp_send_queued(MPIDI_VC_t * vc, MPIDI_nem_tcp_request_queue_t * send_queue)
60 {
61 int mpi_errno = MPI_SUCCESS;
62 MPIR_Request *sreq;
63 intptr_t offset;
64 struct iovec *iov;
65 int complete;
66 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
67 #ifdef HAVE_ERROR_CHECKING
68 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
69 #endif
70
71 MPIR_FUNC_ENTER;
72
73 MPL_DBG_MSG_P(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "vc = %p", vc);
74 MPIR_Assert(vc != NULL);
75
76 if (MPIDI_CH3I_Sendq_empty(*send_queue))
77 goto fn_exit;
78
79 while (!MPIDI_CH3I_Sendq_empty(*send_queue)) {
80 sreq = MPIDI_CH3I_Sendq_head(*send_queue);
81 MPL_DBG_MSG_P(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "Sending %p", sreq);
82
83 iov = &sreq->dev.iov[sreq->dev.iov_offset];
84
85 offset = MPL_large_writev(vc_tcp->sc->fd, iov, sreq->dev.iov_count);
86 if (offset == 0) {
87 int req_errno = MPI_SUCCESS;
88
89 MPIR_ERR_SET(req_errno, MPI_ERR_OTHER, "**sock_closed");
90 MPIR_ERR_SET1(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail", "**comm_fail %d",
91 vc->pg_rank);
92 mpi_errno = MPID_nem_tcp_cleanup_on_error(vc, req_errno);
93 MPIR_ERR_CHECK(mpi_errno);
94 goto fn_exit; /* this vc is closed now, just bail out */
95 }
96 if (offset == -1) {
97 if (errno == EAGAIN) {
98 offset = 0;
99 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "EAGAIN");
100 break;
101 } else {
102 int req_errno = MPI_SUCCESS;
103 MPIR_ERR_SET1(req_errno, MPI_ERR_OTHER, "**writev", "**writev %s",
104 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
105 MPIR_ERR_SET1(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail", "**comm_fail %d",
106 vc->pg_rank);
107 mpi_errno = MPID_nem_tcp_cleanup_on_error(vc, req_errno);
108 MPIR_ERR_CHECK(mpi_errno);
109 goto fn_exit; /* this vc is closed now, just bail out */
110 }
111 }
112 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "write %" PRIdPTR, offset);
113
114 complete = 1;
115 for (iov = &sreq->dev.iov[sreq->dev.iov_offset];
116 iov < &sreq->dev.iov[sreq->dev.iov_offset + sreq->dev.iov_count]; ++iov) {
117 if (offset < iov->iov_len) {
118 iov->iov_base = (char *) iov->iov_base + offset;
119 iov->iov_len -= offset;
120 /* iov_count should be equal to the number of iov's remaining */
121 sreq->dev.iov_count -= ((iov - sreq->dev.iov) - sreq->dev.iov_offset);
122 sreq->dev.iov_offset = iov - sreq->dev.iov;
123 complete = 0;
124 break;
125 }
126 offset -= iov->iov_len;
127 }
128 if (!complete) {
129 /* writev couldn't write the entire iov, give up for now */
130 break;
131 } else {
132 /* sent whole message */
133 int (*reqFn) (MPIDI_VC_t *, MPIR_Request *, int *);
134
135 reqFn = sreq->dev.OnDataAvail;
136 if (!reqFn) {
137 MPIR_Assert(MPIDI_Request_get_type(sreq) != MPIDI_REQUEST_TYPE_GET_RESP);
138 mpi_errno = MPID_Request_complete(sreq);
139 MPIR_ERR_CHECK(mpi_errno);
140 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, ".... complete");
141 MPIDI_CH3I_Sendq_dequeue(send_queue, &sreq);
142 continue;
143 }
144
145 complete = 0;
146 mpi_errno = reqFn(vc, sreq, &complete);
147 MPIR_ERR_CHECK(mpi_errno);
148
149 if (complete) {
150 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, ".... complete");
151 MPIDI_CH3I_Sendq_dequeue(send_queue, &sreq);
152 continue;
153 }
154 sreq->dev.iov_offset = 0;
155 }
156 }
157
158 if (MPIDI_CH3I_Sendq_empty(*send_queue))
159 UNSET_PLFD(vc_tcp);
160
161 fn_exit:
162 MPIR_FUNC_EXIT;
163 return mpi_errno;
164 fn_fail:
165 goto fn_exit;
166 }
167
168 int MPID_nem_tcp_send_finalize(void)
169 {
170 int mpi_errno = MPI_SUCCESS;
171
172 while (!S_EMPTY(free_buffers)) {
173 MPID_nem_tcp_send_q_element_t *e;
174 S_POP(&free_buffers, &e);
175 MPL_free(e);
176 }
177 return mpi_errno;
178 }
179
180 /* MPID_nem_tcp_conn_est -- this function is called when the
181 connection is finally established to send any pending sends */
182 int MPID_nem_tcp_conn_est(MPIDI_VC_t * vc)
183 {
184 int mpi_errno = MPI_SUCCESS;
185 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
186
187 MPIR_FUNC_ENTER;
188
189 /* only update VC state when it is not being closed.
190 * Note that we still need change state here if the VC is passively
191 * connected (i.e., server in dynamic process connection) */
192 if (vc->state == MPIDI_VC_STATE_INACTIVE)
193 MPIDI_CHANGE_VC_STATE(vc, ACTIVE);
194
195 if (!MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
196 SET_PLFD(vc_tcp);
197 mpi_errno = MPID_nem_tcp_send_queued(vc, &vc_tcp->send_queue);
198 MPIR_ERR_CHECK(mpi_errno);
199 }
200
201 fn_fail:
202 MPIR_FUNC_EXIT;
203 return mpi_errno;
204 }
205
206 /* Common routine that transfers iovs and handles any failure.
207 * Called by sending functions. */
208 static inline int tcp_large_writev(MPIDI_VC_t * vc, const struct iovec * iov, int iov_n,
209 intptr_t * offset_ptr)
210 {
211 int mpi_errno = MPI_SUCCESS;
212 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
213 #ifdef HAVE_ERROR_CHECKING
214 char strerrbuf[MPIR_STRERROR_BUF_SIZE];
215 #endif
216
217 *offset_ptr = MPL_large_writev(vc_tcp->sc->fd, iov, iov_n);
218 if (*offset_ptr == 0) {
219 int req_errno = MPI_SUCCESS;
220 MPIR_ERR_SET(req_errno, MPI_ERR_OTHER, "**sock_closed");
221 MPIR_ERR_SET1(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail", "**comm_fail %d",
222 vc->pg_rank);
223 mpi_errno = MPID_nem_tcp_cleanup_on_error(vc, req_errno);
224 MPIR_ERR_CHECK(mpi_errno);
225 goto fn_fail;
226 }
227 if (*offset_ptr == -1) {
228 if (errno == EAGAIN)
229 *offset_ptr = 0;
230 else {
231 int req_errno = MPI_SUCCESS;
232 MPIR_ERR_SET1(req_errno, MPI_ERR_OTHER, "**writev", "**writev %s",
233 MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE));
234 MPIR_ERR_SET1(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail",
235 "**comm_fail %d", vc->pg_rank);
236 mpi_errno = MPID_nem_tcp_cleanup_on_error(vc, req_errno);
237 MPIR_ERR_CHECK(mpi_errno);
238 goto fn_fail;
239 }
240 }
241
242 fn_exit:
243 return mpi_errno;
244 fn_fail:
245 goto fn_exit;
246 }
247
248 /* Common routine that completes finished send request.
249 * Called by sending functions. */
250 static inline int tcp_complete_sreq(MPIDI_VC_t * vc, MPIR_Request * sreq, int *complete_ptr)
251 {
252 int mpi_errno = MPI_SUCCESS;
253 int (*reqFn) (MPIDI_VC_t *, MPIR_Request *, int *);
254
255 reqFn = sreq->dev.OnDataAvail;
256 if (!reqFn) {
257 MPIR_Assert(MPIDI_Request_get_type(sreq) != MPIDI_REQUEST_TYPE_GET_RESP);
258 mpi_errno = MPID_Request_complete(sreq);
259 MPIR_ERR_CHECK(mpi_errno);
260 *complete_ptr = 1;
261 } else {
262 *complete_ptr = 0;
263 mpi_errno = reqFn(vc, sreq, complete_ptr);
264 MPIR_ERR_CHECK(mpi_errno);
265 }
266
267 fn_exit:
268 return mpi_errno;
269 fn_fail:
270 goto fn_exit;
271 }
272
273 /* Common routine that enqueues unfinished send request.
274 * Called by sending functions. If paused_send is true, always enqueue into
275 * send_queue even if the vc is in a paused state. */
276 static inline int tcp_enqueue_sreq(MPIDI_VC_t * vc, MPIR_Request * sreq, bool paused_send)
277 {
278 int mpi_errno = MPI_SUCCESS;
279 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
280
281 MPIR_Assert(sreq->dev.iov_count >= 1 && sreq->dev.iov[0].iov_len > 0);
282
283 if (!paused_send && MPID_nem_tcp_vc_send_paused(vc_tcp)) {
284 MPIDI_CH3I_Sendq_enqueue(&vc_tcp->paused_send_queue, sreq);
285 } else {
286 if (MPID_nem_tcp_vc_is_connected(vc_tcp)) {
287 if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
288 /* this will be the first send on the queue: queue it and set the write flag on the pollfd */
289 MPIDI_CH3I_Sendq_enqueue(&vc_tcp->send_queue, sreq);
290 SET_PLFD(vc_tcp);
291 } else {
292 /* there are other sends in the queue before this one: try to send from the queue */
293 MPIDI_CH3I_Sendq_enqueue(&vc_tcp->send_queue, sreq);
294 mpi_errno = MPID_nem_tcp_send_queued(vc, &vc_tcp->send_queue);
295 MPIR_ERR_CHECK(mpi_errno);
296 }
297 } else {
298 MPIDI_CH3I_Sendq_enqueue(&vc_tcp->send_queue, sreq);
299 }
300 }
301
302 fn_exit:
303 return mpi_errno;
304 fn_fail:
305 goto fn_exit;
306 }
307
308 int MPID_nem_tcp_iStartContigMsg(MPIDI_VC_t * vc, void *hdr, intptr_t hdr_sz, void *data,
309 intptr_t data_sz, MPIR_Request ** sreq_ptr)
310 {
311 int mpi_errno = MPI_SUCCESS;
312 MPIR_Request *sreq = NULL;
313 intptr_t offset = 0;
314 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
315
316 MPIR_FUNC_ENTER;
317
318 MPIR_Assert(hdr_sz <= sizeof(MPIDI_CH3_Pkt_t));
319
320 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "tcp_iStartContigMsg");
321 MPIDI_DBG_Print_packet((MPIDI_CH3_Pkt_t *) hdr);
322
323 if (!MPID_nem_tcp_vc_send_paused(vc_tcp)) {
324 if (MPID_nem_tcp_vc_is_connected(vc_tcp)) {
325 if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
326 struct iovec iov[2];
327
328 iov[0].iov_base = hdr;
329 iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t);
330 iov[1].iov_base = data;
331 iov[1].iov_len = data_sz;
332
333 mpi_errno = tcp_large_writev(vc, iov, 2, &offset);
334 MPIR_ERR_CHECK(mpi_errno);
335 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "write %" PRIdPTR, offset);
336
337 if (offset == sizeof(MPIDI_CH3_Pkt_t) + data_sz) {
338 /* sent whole message */
339 *sreq_ptr = NULL;
340 goto fn_exit;
341 }
342 }
343 } else {
344 /* state may be DISCONNECTED or ERROR. Calling tcp_connect in an ERROR state will return an
345 * appropriate error code. */
346 mpi_errno = MPID_nem_tcp_connect(vc);
347 MPIR_ERR_CHECK(mpi_errno);
348 }
349 }
350
351 /* create and enqueue request */
352 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "enqueuing");
353
354 /* create a request */
355 sreq = MPIR_Request_create(MPIR_REQUEST_KIND__SEND);
356 MPIR_Assert(sreq != NULL);
357 MPIR_Object_set_ref(sreq, 2);
358
359 sreq->dev.OnDataAvail = 0;
360 sreq->ch.vc = vc;
361 sreq->dev.iov_offset = 0;
362
363 if (offset < sizeof(MPIDI_CH3_Pkt_t)) {
364 sreq->dev.pending_pkt = *(MPIDI_CH3_Pkt_t *) hdr;
365 sreq->dev.iov[0].iov_base = (char *) &sreq->dev.pending_pkt + offset;
366 sreq->dev.iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t) - offset;
367 if (data_sz) {
368 sreq->dev.iov[1].iov_base = data;
369 sreq->dev.iov[1].iov_len = data_sz;
370 sreq->dev.iov_count = 2;
371 } else
372 sreq->dev.iov_count = 1;
373 } else {
374 sreq->dev.iov[0].iov_base = (char *) data + (offset - sizeof(MPIDI_CH3_Pkt_t));
375 sreq->dev.iov[0].iov_len = data_sz - (offset - sizeof(MPIDI_CH3_Pkt_t));
376 sreq->dev.iov_count = 1;
377 }
378
379 mpi_errno = tcp_enqueue_sreq(vc, sreq, false);
380 MPIR_ERR_CHECK(mpi_errno);
381
382 *sreq_ptr = sreq;
383
384 fn_exit:
385 MPIR_FUNC_EXIT;
386 return mpi_errno;
387 fn_fail:
388 goto fn_exit;
389 }
390
391 /* This sends the message even if the vc is in a paused state */
392 int MPID_nem_tcp_iStartContigMsg_paused(MPIDI_VC_t * vc, void *hdr, intptr_t hdr_sz, void *data,
393 intptr_t data_sz, MPIR_Request ** sreq_ptr)
394 {
395 int mpi_errno = MPI_SUCCESS;
396 MPIR_Request *sreq = NULL;
397 intptr_t offset = 0;
398 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
399
400 MPIR_FUNC_ENTER;
401
402 MPIR_Assert(hdr_sz <= sizeof(MPIDI_CH3_Pkt_t));
403
404 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "tcp_iStartContigMsg");
405 MPIDI_DBG_Print_packet((MPIDI_CH3_Pkt_t *) hdr);
406
407 if (MPID_nem_tcp_vc_is_connected(vc_tcp)) {
408 if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
409 struct iovec iov[2];
410
411 iov[0].iov_base = hdr;
412 iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t);
413 iov[1].iov_base = data;
414 iov[1].iov_len = data_sz;
415
416 mpi_errno = tcp_large_writev(vc, iov, 2, &offset);
417 MPIR_ERR_CHECK(mpi_errno);
418 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "write %" PRIdPTR, offset);
419
420 if (offset == sizeof(MPIDI_CH3_Pkt_t) + data_sz) {
421 /* sent whole message */
422 *sreq_ptr = NULL;
423 goto fn_exit;
424 }
425 }
426 } else {
427 /* state may be DISCONNECTED or ERROR. Calling tcp_connect in an ERROR state will return an
428 * appropriate error code. */
429 mpi_errno = MPID_nem_tcp_connect(vc);
430 MPIR_ERR_CHECK(mpi_errno);
431 }
432
433 /* create and enqueue request */
434 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "enqueuing");
435
436 /* create a request */
437 sreq = MPIR_Request_create(MPIR_REQUEST_KIND__SEND);
438 MPIR_Assert(sreq != NULL);
439 MPIR_Object_set_ref(sreq, 2);
440
441 sreq->dev.OnDataAvail = 0;
442 sreq->ch.vc = vc;
443 sreq->dev.iov_offset = 0;
444
445 if (offset < sizeof(MPIDI_CH3_Pkt_t)) {
446 sreq->dev.pending_pkt = *(MPIDI_CH3_Pkt_t *) hdr;
447 sreq->dev.iov[0].iov_base = (char *) &sreq->dev.pending_pkt + offset;
448 sreq->dev.iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t) - offset;
449 if (data_sz) {
450 sreq->dev.iov[1].iov_base = data;
451 sreq->dev.iov[1].iov_len = data_sz;
452 sreq->dev.iov_count = 2;
453 } else
454 sreq->dev.iov_count = 1;
455 } else {
456 sreq->dev.iov[0].iov_base = (char *) data + (offset - sizeof(MPIDI_CH3_Pkt_t));
457 sreq->dev.iov[0].iov_len = data_sz - (offset - sizeof(MPIDI_CH3_Pkt_t));
458 sreq->dev.iov_count = 1;
459 }
460
461 mpi_errno = tcp_enqueue_sreq(vc, sreq, true);
462 MPIR_ERR_CHECK(mpi_errno);
463
464 *sreq_ptr = sreq;
465
466 fn_exit:
467 MPIR_FUNC_EXIT;
468 return mpi_errno;
469 fn_fail:
470 goto fn_exit;
471 }
472
473 int MPID_nem_tcp_iSendContig(MPIDI_VC_t * vc, MPIR_Request * sreq, void *hdr, intptr_t hdr_sz,
474 void *data, intptr_t data_sz)
475 {
476 int mpi_errno = MPI_SUCCESS;
477 intptr_t offset = 0;
478 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
479
480 MPIR_FUNC_ENTER;
481
482 MPIR_Assert(hdr_sz <= sizeof(MPIDI_CH3_Pkt_t));
483
484 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "tcp_iSendContig");
485
486 MPIDI_DBG_Print_packet((MPIDI_CH3_Pkt_t *) hdr);
487
488 if (!MPID_nem_tcp_vc_send_paused(vc_tcp)) {
489 if (MPID_nem_tcp_vc_is_connected(vc_tcp)) {
490 if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
491 struct iovec iov[3];
492 int iov_n = 0;
493
494 iov[iov_n].iov_base = hdr;
495 iov[iov_n].iov_len = sizeof(MPIDI_CH3_Pkt_t);
496 iov_n++;
497
498 iov[iov_n].iov_base = data;
499 iov[iov_n].iov_len = data_sz;
500 iov_n++;
501
502 mpi_errno = tcp_large_writev(vc, iov, iov_n, &offset);
503 MPIR_ERR_CHECK(mpi_errno);
504 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "write %" PRIdPTR, offset);
505
506 if (offset == sizeof(MPIDI_CH3_Pkt_t) + data_sz) {
507 /* sent whole message, complete request */
508 int complete = 0;
509 mpi_errno = tcp_complete_sreq(vc, sreq, &complete);
510 MPIR_ERR_CHECK(mpi_errno);
511 if (complete) {
512 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, ".... complete");
513 goto fn_exit;
514 } else
515 goto enqueue_request; /* not completed: more to send */
516 }
517 }
518 } else {
519 /* state may be DISCONNECTED or ERROR. Calling tcp_connect in an ERROR state will return an
520 * appropriate error code. */
521 mpi_errno = MPID_nem_tcp_connect(vc);
522 MPIR_ERR_CHECK(mpi_errno);
523 }
524 }
525
526
527 /* save iov */
528 sreq->dev.iov_count = 0;
529 if (offset < sizeof(MPIDI_CH3_Pkt_t)) {
530 sreq->dev.pending_pkt = *(MPIDI_CH3_Pkt_t *) hdr;
531
532 sreq->dev.iov[sreq->dev.iov_count].iov_base = (char *) &sreq->dev.pending_pkt + offset;
533 sreq->dev.iov[sreq->dev.iov_count].iov_len = sizeof(MPIDI_CH3_Pkt_t) - offset;
534 sreq->dev.iov_count++;
535
536 if (data_sz) {
537 sreq->dev.iov[sreq->dev.iov_count].iov_base = data;
538 sreq->dev.iov[sreq->dev.iov_count].iov_len = data_sz;
539 sreq->dev.iov_count++;
540 }
541 } else {
542 sreq->dev.iov[sreq->dev.iov_count].iov_base =
543 (char *) data + (offset - sizeof(MPIDI_CH3_Pkt_t));
544 sreq->dev.iov[sreq->dev.iov_count].iov_len =
545 data_sz - (offset - sizeof(MPIDI_CH3_Pkt_t));
546 sreq->dev.iov_count++;
547 }
548
549 enqueue_request:
550 /* enqueue request */
551 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "enqueuing");
552 sreq->ch.vc = vc;
553 sreq->dev.iov_offset = 0;
554
555 mpi_errno = tcp_enqueue_sreq(vc, sreq, false);
556 MPIR_ERR_CHECK(mpi_errno);
557
558 fn_exit:
559 MPIR_FUNC_EXIT;
560 return mpi_errno;
561 fn_fail:
562 goto fn_exit;
563 }
564
565
566 int MPID_nem_tcp_iSendIov(MPIDI_VC_t * vc, MPIR_Request * sreq, void *hdr, intptr_t hdr_sz,
567 struct iovec * iov, int n_iov)
568 {
569 int mpi_errno = MPI_SUCCESS, i;
570 intptr_t offset = 0, data_sz = 0;
571 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
572 MPIR_FUNC_ENTER;
573
574 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "tcp_iSendIov");
575
576 if (!MPID_nem_tcp_vc_send_paused(vc_tcp)) {
577 if (MPID_nem_tcp_vc_is_connected(vc_tcp)) {
578 if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
579 struct iovec tcp_iov[MPL_IOV_LIMIT];
580 int tcp_iov_n = 1 + n_iov;
581
582 MPIR_Assert(tcp_iov_n >= 0 && tcp_iov_n < MPL_IOV_LIMIT);
583
584 /* Merge header into iov */
585 tcp_iov[0].iov_base = hdr;
586 tcp_iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t);
587 for (i = 0; i < n_iov; i++) {
588 tcp_iov[i + 1].iov_base = iov[i].iov_base;
589 tcp_iov[i + 1].iov_len = iov[i].iov_len;
590 data_sz += iov[i].iov_len;
591 }
592
593 mpi_errno = tcp_large_writev(vc, tcp_iov, tcp_iov_n, &offset);
594 MPIR_ERR_CHECK(mpi_errno);
595 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "write %" PRIdPTR, offset);
596
597 if (offset == sizeof(MPIDI_CH3_Pkt_t) + data_sz) {
598 /* sent whole message, complete request */
599 int complete = 0;
600 mpi_errno = tcp_complete_sreq(vc, sreq, &complete);
601 MPIR_ERR_CHECK(mpi_errno);
602 if (complete) {
603 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, ".... complete");
604 goto fn_exit;
605 } else
606 goto enqueue_request; /* not completed: more to send */
607 }
608 }
609 } else {
610 /* state may be DISCONNECTED or ERROR. Calling tcp_connect in an ERROR state will return an
611 * appropriate error code. */
612 mpi_errno = MPID_nem_tcp_connect(vc);
613 MPIR_ERR_CHECK(mpi_errno);
614 }
615 }
616
617 if (offset < sizeof(MPIDI_CH3_Pkt_t)) {
618 /* save pending header */
619 sreq->dev.pending_pkt = *(MPIDI_CH3_Pkt_t *) hdr;
620 sreq->dev.iov[0].iov_base = (char *) &sreq->dev.pending_pkt + offset;
621 sreq->dev.iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t) - offset;
622 sreq->dev.iov_count = 1;
623
624 /* save whole iov */
625 if (n_iov > 0) {
626 sreq->dev.iov_count += n_iov;
627 for (i = 0; i < n_iov; i++) {
628 sreq->dev.iov[i + 1].iov_base = iov[i].iov_base;
629 sreq->dev.iov[i + 1].iov_len = iov[i].iov_len;
630 }
631 }
632 } else {
633 /* header has sent out, save any unsent portion of iov */
634 sreq->dev.iov_count = 0;
635 offset -= sizeof(MPIDI_CH3_Pkt_t);
636 for (i = 0; i < n_iov; i++) {
637 if (offset < iov[i].iov_len) { /* unsent iov starts */
638 sreq->dev.iov[sreq->dev.iov_count].iov_base =
639 (void *) ((char *) iov[i].iov_base + offset);
640 sreq->dev.iov[sreq->dev.iov_count].iov_len = iov[i].iov_len - offset;
641 offset = 0;
642 sreq->dev.iov_count++;
643 } else
644 offset -= iov[i].iov_len; /* sent iov */
645 }
646 }
647
648 enqueue_request:
649 /* enqueue request */
650 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "enqueuing");
651 sreq->ch.vc = vc;
652 sreq->dev.iov_offset = 0;
653
654 mpi_errno = tcp_enqueue_sreq(vc, sreq, false);
655 MPIR_ERR_CHECK(mpi_errno);
656
657 fn_exit:
658 MPIR_FUNC_EXIT;
659 return mpi_errno;
660 fn_fail:
661 goto fn_exit;
662 }
663
664 int MPID_nem_tcp_SendNoncontig(MPIDI_VC_t * vc, MPIR_Request * sreq, void *header, intptr_t hdr_sz,
665 struct iovec * hdr_iov, int n_hdr_iov)
666 {
667 int mpi_errno = MPI_SUCCESS;
668 int iov_n;
669 struct iovec iov[MPL_IOV_LIMIT];
670 struct iovec *iov_p;
671 intptr_t offset;
672 int complete;
673 MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc);
674 int iov_offset;
675
676 MPIR_FUNC_ENTER;
677 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "tcp_SendNoncontig");
678 MPIR_Assert(hdr_sz <= sizeof(MPIDI_CH3_Pkt_t));
679
680 iov_n = 0;
681
682 iov[iov_n].iov_base = header;
683 iov[iov_n].iov_len = sizeof(MPIDI_CH3_Pkt_t);
684 iov_n++;
685
686 if (n_hdr_iov > 0) {
687 int i;
688 /* merge extended header iovs into iov array.
689 * ensure at least 1 iov is left for data. */
690 MPIR_Assert(MPL_IOV_LIMIT - iov_n - n_hdr_iov > 0);
691 for (i = 0; i < n_hdr_iov; i++) {
692 iov[iov_n].iov_base = hdr_iov[i].iov_base;
693 iov[iov_n].iov_len = hdr_iov[i].iov_len;
694 iov_n++;
695 }
696 }
697
698 iov_offset = iov_n;
699
700 /* Set iov_n to remaining capacity. It will be set to actual num of iovs loaded on output. */
701 iov_n = MPL_IOV_LIMIT - iov_offset;
702 mpi_errno = MPIDI_CH3U_Request_load_send_iov(sreq, &iov[iov_offset], &iov_n);
703 MPIR_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|loadsendiov");
704
705 iov_n += iov_offset;
706 offset = 0;
707
708 if (!MPID_nem_tcp_vc_send_paused(vc_tcp)) {
709 if (MPID_nem_tcp_vc_is_connected(vc_tcp)) {
710 if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
711 mpi_errno = tcp_large_writev(vc, iov, iov_n, &offset);
712 MPIR_ERR_CHECK(mpi_errno);
713 MPL_DBG_MSG_D(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "write noncontig %" PRIdPTR, offset);
714 }
715 } else {
716 /* state may be DISCONNECTED or ERROR. Calling tcp_connect in an ERROR state will return an
717 * appropriate error code. */
718 mpi_errno = MPID_nem_tcp_connect(vc);
719 MPIR_ERR_CHECK(mpi_errno);
720 }
721 }
722
723 if (offset < iov[0].iov_len) {
724 /* header was not yet sent, save it in req */
725 sreq->dev.pending_pkt = *(MPIDI_CH3_Pkt_t *) header;
726 iov[0].iov_base = (void *) & sreq->dev.pending_pkt;
727 iov[0].iov_len = sizeof(MPIDI_CH3_Pkt_t);
728 }
729
730 /* check if whole iov was sent, and save any unsent portion of iov */
731 sreq->dev.iov_count = 0;
732 complete = 1;
733 for (iov_p = &iov[0]; iov_p < &iov[iov_n]; ++iov_p) {
734 if (offset < iov_p->iov_len) {
735 sreq->dev.iov[sreq->dev.iov_count].iov_base =
736 (void *) ((char *) iov_p->iov_base + offset);
737 sreq->dev.iov[sreq->dev.iov_count].iov_len = iov_p->iov_len - offset;
738 offset = 0;
739 ++sreq->dev.iov_count;
740 complete = 0;
741 } else
742 offset -= iov_p->iov_len;
743 }
744
745 if (complete) {
746 /* sent whole iov, complete request */
747 int req_complete = 0;
748 mpi_errno = tcp_complete_sreq(vc, sreq, &req_complete);
749 MPIR_ERR_CHECK(mpi_errno);
750
751 if (req_complete) {
752 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, ".... complete");
753 goto fn_exit;
754 }
755 }
756
757 /* enqueue request */
758 MPL_DBG_MSG(MPIDI_CH3_DBG_CHANNEL, VERBOSE, "enqueuing");
759 sreq->ch.vc = vc;
760 sreq->dev.iov_offset = 0;
761
762 mpi_errno = tcp_enqueue_sreq(vc, sreq, false);
763 MPIR_ERR_CHECK(mpi_errno);
764
765 fn_exit:
766 MPIR_FUNC_EXIT;
767 return mpi_errno;
768 fn_fail:
769 MPIR_Request_free(sreq);
770 goto fn_exit;
771 }
772
773 int MPID_nem_tcp_error_out_send_queue(struct MPIDI_VC *const vc, int req_errno)
774 {
775 int mpi_errno = MPI_SUCCESS;
776 MPIR_Request *req;
777 MPID_nem_tcp_vc_area *const vc_tcp = VC_TCP(vc);
778
779 MPIR_FUNC_ENTER;
780
781 /* we don't call onDataAvail or onFinal handlers because this is
782 * an error condition and we just want to mark them as complete */
783
784 /* send queue */
785 while (!MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) {
786 MPIDI_CH3I_Sendq_dequeue(&vc_tcp->send_queue, &req);
787 req->status.MPI_ERROR = req_errno;
788
789 mpi_errno = MPID_Request_complete(req);
790 MPIR_ERR_CHECK(mpi_errno);
791 }
792
793 /* paused send queue */
794 while (!MPIDI_CH3I_Sendq_empty(vc_tcp->paused_send_queue)) {
795 MPIDI_CH3I_Sendq_dequeue(&vc_tcp->paused_send_queue, &req);
796 req->status.MPI_ERROR = req_errno;
797
798 mpi_errno = MPID_Request_complete(req);
799 MPIR_ERR_CHECK(mpi_errno);
800 }
801
802 fn_exit:
803 MPIR_FUNC_EXIT;
804 return mpi_errno;
805 fn_fail:
806 goto fn_exit;
807 }