File: btl_sctp_frag.c

package info (click to toggle)
openmpi 1.6.5-9.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 91,652 kB
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (639 lines) | stat: -rw-r--r-- 23,513 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2006 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2009      Cisco Systems, Inc.  All rights reserved.
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 *
 * In windows, many of the socket functions return an EWOULDBLOCK
 * instead of \ things like EAGAIN, EINPROGRESS, etc. It has been
 * verified that this will \ not conflict with other error codes that
 * are returned by these functions \ under UNIX/Linux environments 
 */

#include "ompi_config.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif  /* HAVE_UNISTD_H */

#include "opal/opal_socket_errno.h"
#include "opal/include/opal_stdint.h"
#include "ompi/mca/btl/base/btl_base_error.h"
#include "btl_sctp_frag.h" 
#include "btl_sctp_endpoint.h"

#include "btl_sctp.h"
#include "btl_sctp_addr.h"
#include "btl_sctp_utils.h"
#include <sys/socket.h>
#include <netinet/sctp.h>

static void mca_btl_sctp_frag_common_constructor(mca_btl_sctp_frag_t* frag) 
{ 
    frag->base.des_src = NULL;
    frag->base.des_src_cnt = 0;
    frag->base.des_dst = NULL;
    frag->base.des_dst_cnt = 0;
}

static void mca_btl_sctp_frag_eager_constructor(mca_btl_sctp_frag_t* frag) 
{ 
    frag->size = mca_btl_sctp_module.super.btl_eager_limit;   
    frag->my_list = &mca_btl_sctp_component.sctp_frag_eager;
    mca_btl_sctp_frag_common_constructor(frag); 
}

static void mca_btl_sctp_frag_max_constructor(mca_btl_sctp_frag_t* frag) 
{ 
    frag->size = mca_btl_sctp_module.super.btl_max_send_size; 
    frag->my_list = &mca_btl_sctp_component.sctp_frag_max;
    mca_btl_sctp_frag_common_constructor(frag); 
}

static void mca_btl_sctp_frag_user_constructor(mca_btl_sctp_frag_t* frag) 
{ 
    frag->size = 0; 
    frag->my_list = &mca_btl_sctp_component.sctp_frag_user;
    mca_btl_sctp_frag_common_constructor(frag); 
}


OBJ_CLASS_INSTANCE(
        mca_btl_sctp_frag_t, 
        mca_btl_base_descriptor_t, 
        NULL, 
        NULL); 

OBJ_CLASS_INSTANCE(
        mca_btl_sctp_frag_eager_t, 
        mca_btl_base_descriptor_t, 
        mca_btl_sctp_frag_eager_constructor, 
        NULL); 

OBJ_CLASS_INSTANCE(
        mca_btl_sctp_frag_max_t, 
        mca_btl_base_descriptor_t, 
        mca_btl_sctp_frag_max_constructor, 
        NULL); 

OBJ_CLASS_INSTANCE(
        mca_btl_sctp_frag_user_t, 
        mca_btl_base_descriptor_t, 
        mca_btl_sctp_frag_user_constructor, 
        NULL); 



/**
 * int mca_btl_sctp_frag_get_msg_size(mca_btl_sctp_frag_t *frag)
 * -------------------------------------------
 *  Returns the full size of the message to send (stored in the iov array)
 *  including all header information.
 */

int mca_btl_sctp_frag_get_msg_size(mca_btl_sctp_frag_t *frag) {
    int count;
    int size = 0;

    for(count = 0; count < (int) frag->iov_cnt; count++) {
        size += frag->iov_ptr[count].iov_len;
    }

    return size;
}



/**
 * mca_btl_sctp_frag_large_send(mca_btl_sctp_frag_t* frag, int sd)
 * ---------------------------------------------------------------
 *  Send a frag that is too large to send in one call to a vector write.
 */

bool mca_btl_sctp_frag_large_send(mca_btl_sctp_frag_t* frag, int sd, int iov_fragment, int *amt_sent) {
    int done = 0;
    int count_down = 0;
    int cnt = -1;
    int data_sent = 0;
    int to_send;
    struct sockaddr_in btl_sockaddr;

    *amt_sent = 0;

    /* Determine full size of message that needs to be sent. */
    count_down = mca_btl_sctp_frag_get_msg_size(frag);
        
    /* Setup addressing information. */
    btl_sockaddr = mca_btl_sctp_utils_sockaddr_from_frag(frag);

    while(!done) {
        if(frag->iov_ptr->iov_len == 0) {
            /* Just used to jump over the 3rd empty iovec in the array. Open MPI
             * sets up their message as shown above and so this is added for
             * potential compatibility? 
             */
            frag->iov_ptr++;
        }

        if(frag->iov_ptr->iov_len <= MCA_BTL_SCTP_MAX_FRAG_SIZE) {
            to_send = frag->iov_ptr->iov_len;
        } else { /* iov_ptr->iov_len > MCA_BTL_SCTP_MAX_FRAG_SIZE */
            to_send = MCA_BTL_SCTP_MAX_FRAG_SIZE;
        }
            
        if(mca_btl_sctp_component.sctp_if_11) {
            cnt = sctp_sendmsg(sd, frag->iov_ptr->iov_base, to_send, 0, 0, 0, 0, 0, 0, 0 );
        } else {
            cnt = sctp_sendmsg(sd, frag->iov_ptr->iov_base, to_send, (struct sockaddr *)&btl_sockaddr, 
                    sizeof(btl_sockaddr), 0, 0, 0, 0, 0 );
        }
                                   
                    
        if(cnt >= 0) {
            SCTP_BTL_ERROR(("mca_btl_sctp_frag_large_send() sent %d bytes.\n",cnt));
        } else {
            /* cnt < 0  */
            switch(opal_socket_errno) {
            case EINTR:
            case EWOULDBLOCK:
                if(data_sent) {SCTP_BTL_ERROR(("leaving large_send (data_sent = %d)\n",data_sent));}
                cnt=0;
                break;
            case EFAULT:
                BTL_ERROR(("sctp_sendmsg error (%p, %" PRIsize_t ")\n\t%s(%" PRIsize_t ")\n",
                           frag->iov_ptr[0].iov_base, frag->iov_ptr[0].iov_len,
                           strerror(opal_socket_errno), frag->iov_cnt));
            default:
                {
                    BTL_ERROR(("sctp_sendmsg failed with errno=%d", opal_socket_errno));
                    mca_btl_sctp_endpoint_close(frag->endpoint);
                    return false;
                }
            }
        }

        if(cnt > 0) {
            /* update frag book-keeping with each iteration */

            /* SCTP sends all or nothing */
            assert(to_send == cnt);
                
            data_sent += cnt;
            *amt_sent = data_sent;
            if(frag->iov_ptr->iov_len <= MCA_BTL_SCTP_MAX_FRAG_SIZE)
            {
                /* completed sending this vector element */
                    
                assert(cnt == (int) frag->iov_ptr->iov_len);
                    
                frag->iov_ptr++;
                frag->iov_idx++;
                frag->iov_cnt--;                    
            }
            else /* iov_ptr->iov_len > MCA_BTL_SCTP_MAX_FRAG_SIZE */
            {
                /* sent only a portion of this vector element */
                    
                assert(cnt < (int) frag->iov_ptr->iov_len);
                assert(cnt == MCA_BTL_SCTP_MAX_FRAG_SIZE);
                
                frag->iov_ptr->iov_base = (ompi_iov_base_ptr_t)
                    (((unsigned char*)frag->iov_ptr->iov_base) + cnt);
                frag->iov_ptr->iov_len -= cnt;                    
            }
        }
        if(cnt == 0) { /* just in case nothing can be sent, we'll go back to the
                      *  progress function */
            return false;
        }
        if(data_sent == count_down) {
            /* If data_sent == count_down then we're done. */
            done = 1;
        }
    }

    return (done == 1);
}


/**
 * mca_btl_sctp_frag_send(mca_btl_sctp_frag_t* frag, int sd)
 * ---------------------------------------------------------------
 *  Send a message frag.
 */

bool mca_btl_sctp_frag_send(mca_btl_sctp_frag_t* frag, int sd)
{
    int zero=0,cnt=-1;
    size_t i, num_vecs;
    int large_message_send_return;
    int large_vector;

    /* Check each iov_len field in frag.iov[] and see is any of them are
     * above 64K.
     */

    size_t count;
    int large_msg = 0;

    for(count = 0; count < frag->iov_cnt; count++) {
        zero += frag->iov_ptr[count].iov_len;

        /* True if we have a message that is too long to send in one shot via
         * SCTP.
         */
        if(frag->iov_ptr[count].iov_len > MCA_BTL_SCTP_MAX_FRAG_SIZE) {
            large_msg = 1;  /* Set large message flag to true. */
            large_vector = count;
            break;
        }
    }

    /* if only an empty iov element remains, let it fall
     *  through in order to decrement the count */
    if(0 == zero) {
        cnt = 0; /* don't try to send */
    }
            


    /* non-blocking write, but continue if interrupted */
    if(large_msg) {
        large_message_send_return = mca_btl_sctp_frag_large_send(frag, sd, large_vector, &cnt);
    }
    else if(!large_msg) {
        /* Setup addressing information. */
        socklen_t len;
        struct sockaddr_in btl_sockaddr;
        btl_sockaddr = mca_btl_sctp_utils_sockaddr_from_frag(frag);
        len = sizeof(struct sockaddr_in);

        while(cnt < 0) {
            if(mca_btl_sctp_component.sctp_if_11) {
                cnt = mca_btl_sctp_utils_writev(sd, frag->iov_ptr, frag->iov_cnt, 0, 0, 0);
            } else {
                cnt = mca_btl_sctp_utils_writev(sd, frag->iov_ptr, frag->iov_cnt, (struct sockaddr *)&btl_sockaddr, len, 0);
            }
                    
            if(cnt >= 0) {
                SCTP_BTL_ERROR(("mca_btl_sctp_frag_send() sd=%d, sent %d bytes.\n",sd, cnt));
            } else {
                /* cnt < 0  */
                switch(opal_socket_errno) {
                case EINTR:
                case EWOULDBLOCK:
                    return false;
                case EFAULT:
                    BTL_ERROR(("mca_btl_sctp_utils_writev error (%p, %" PRIsize_t ")\n\t%s(%" PRIsize_t ")\n",
                               frag->iov_ptr[0].iov_base, frag->iov_ptr[0].iov_len,
                               strerror(opal_socket_errno), frag->iov_cnt));
                default:
                    {
                        BTL_ERROR(("mca_btl_sctp_utils_writev failed with errno=%d", opal_socket_errno));
                        mca_btl_sctp_endpoint_close(frag->endpoint);
                        return false;
                    }
                }
            }
        }
        

        /* if the write didn't complete - update the iovec state */
        num_vecs = frag->iov_cnt;
        for(i=0; i<num_vecs; i++) {
            if(cnt >= (int)frag->iov_ptr->iov_len) {
                cnt -= frag->iov_ptr->iov_len;
                frag->iov_ptr++;
                frag->iov_idx++;
                frag->iov_cnt--;
            } else {
                frag->iov_ptr->iov_base = (ompi_iov_base_ptr_t)
                    (((unsigned char*)frag->iov_ptr->iov_base) + cnt);
                frag->iov_ptr->iov_len -= cnt;
                break;
            }
        }
        
    }
        
    return (frag->iov_cnt == 0);        
}


/**
 * bool mca_btl_sctp_frag_recv(mca_btl_sctp_frag_t *frag, int sd, char *buf, int len)
 * ----------------------------------------------------------------------------------
 *  Recv message frag.
 */

bool mca_btl_sctp_frag_recv(mca_btl_sctp_frag_t* frag, int sd, char *buf, int len)
{
    if(mca_btl_sctp_component.sctp_if_11) {
        /* 1 to 1 */
        int cnt;
        size_t i, num_vecs;
        mca_btl_base_endpoint_t* btl_endpoint = frag->endpoint;

repeat11:
        num_vecs = frag->iov_cnt;
#if MCA_BTL_SCTP_ENDPOINT_CACHE
        if( 0 != btl_endpoint->endpoint_cache_length ) {
            size_t length = btl_endpoint->endpoint_cache_length;
            /* It's strange at the first look but cnt have to be set to the full amount of data available.
             * After going to advance_iov_position11 we will use cnt to detect if there is still some
             * data pending.
             */
            cnt = btl_endpoint->endpoint_cache_length;
            for( i = 0; i < frag->iov_cnt; i++ ) {
                if( length > frag->iov_ptr[i].iov_len )
                    length = frag->iov_ptr[0].iov_len;
                memcpy( frag->iov_ptr[i].iov_base, btl_endpoint->endpoint_cache_pos, length );
                btl_endpoint->endpoint_cache_pos += length;
                btl_endpoint->endpoint_cache_length -= length;
                length = btl_endpoint->endpoint_cache_length;
                if( 0 == length ) {
                    btl_endpoint->endpoint_cache_pos = btl_endpoint->endpoint_cache;
                    break;
                }
            }
            goto advance_iov_position11;
        }
        /* What's happens if all iovecs are used by the fragment ? It still work, as we reserve one
         * iovec for the caching in the fragment structure (the +1).
         */
        frag->iov_ptr[num_vecs].iov_base = btl_endpoint->endpoint_cache;
        frag->iov_ptr[num_vecs].iov_len  = mca_btl_sctp_component.sctp_endpoint_cache;
        num_vecs++;
#endif  /* MCA_BTL_SCTP_ENDPOINT_CACHE */

        /* non-blocking read, but continue if interrupted */
        cnt = -1;
        while( cnt < 0 ) {
            cnt = readv(sd, frag->iov_ptr, num_vecs);
            if(cnt >= 0) {SCTP_BTL_ERROR(("readv (sd=%d) %d bytes\n", sd, cnt));}
            if(cnt < 0) {
                switch(opal_socket_errno) {
                    case EINTR:
                        continue;
                    case ECONNRESET:
                    case EBADF:
                        close(sd);
                    case EWOULDBLOCK:
                        return false;
                    case EFAULT:
                        opal_output( 0, "mca_btl_sctp_frag_recv: readv error (%p, %d)\n\t%s(%d)\n",
                                frag->iov_ptr[0].iov_base, (int) frag->iov_ptr[0].iov_len,
                                strerror(opal_socket_errno), (int) frag->iov_cnt );
                    default:
                        opal_output(0, "mca_btl_sctp_frag_recv: readv failed with errno=%d",
                                opal_socket_errno);
                        mca_btl_sctp_endpoint_close(btl_endpoint);
                        return false;
                }
            }
            if( cnt == 0 ) {
                return false;
            }
            goto advance_iov_position11;
        };

advance_iov_position11:
        /* if the write didn't complete - update the iovec state */
        num_vecs = frag->iov_cnt;
        for( i = 0; i < num_vecs; i++ ) {
            if( cnt >= (int)frag->iov_ptr->iov_len ) {
                cnt -= frag->iov_ptr->iov_len;
                frag->iov_idx++;
                frag->iov_ptr++;
                frag->iov_cnt--;
            } else {
                frag->iov_ptr->iov_base = (ompi_iov_base_ptr_t)
                    (((unsigned char*)frag->iov_ptr->iov_base) + cnt);
                frag->iov_ptr->iov_len -= cnt;
                cnt = 0;
                break;
            }
        }

#if MCA_BTL_SCTP_ENDPOINT_CACHE
        btl_endpoint->endpoint_cache_length = cnt;
#endif  /* MCA_BTL_SCTP_ENDPOINT_CACHE */

        /* read header */
        if(frag->iov_cnt == 0) {
            if (btl_endpoint->endpoint_nbo) {
                MCA_BTL_SCTP_HDR_NTOH(frag->hdr);
            }
            switch(frag->hdr.type) {
                case MCA_BTL_SCTP_HDR_TYPE_SEND:
                    if(frag->iov_idx == 1 && frag->hdr.size) {
                        frag->iov[1].iov_base = (IOVBASE_TYPE*)(frag+1);
                        frag->iov[1].iov_len = frag->hdr.size;
                        frag->segments[0].seg_addr.pval = frag+1;
                        frag->segments[0].seg_len = frag->hdr.size;
                        frag->iov_cnt++;
                        goto repeat11;
                    }
                    break;
                case MCA_BTL_SCTP_HDR_TYPE_PUT:
                    if(frag->iov_idx == 1) {
                        frag->iov[1].iov_base = (IOVBASE_TYPE*)frag->segments;
                        frag->iov[1].iov_len = frag->hdr.count * sizeof(mca_btl_base_segment_t);
                        frag->iov_cnt++;
                        goto repeat11;
                    } else if (frag->iov_idx == 2) {
                        for(i=0; i<frag->hdr.count; i++) {
                            frag->iov[i+2].iov_base = (IOVBASE_TYPE*)ompi_ptr_ltop(frag->segments[i].seg_addr.lval);
                            frag->iov[i+2].iov_len = frag->segments[i].seg_len;
                            frag->iov_cnt++;
                        }
                        goto repeat11;
                    }
                    break;
                case MCA_BTL_SCTP_HDR_TYPE_GET:
                default:
                    break;
            }
            return true;
        }
        return false;
    }

    else {
        int cnt;
        size_t i, num_vecs;
        mca_btl_base_endpoint_t* btl_endpoint = frag->endpoint;

        /* Ugly way of getting my own macro in to jump back into the recv_handler
         * and progress engine so that I can do my next read from the socket. 
         */
        int done = 0;   

repeat:
        /* In other words, we've packed the frag with the data from buf and need
         * to return to the recv_handler and the subsequent progress engine to
         * get another piece of data... hence the notion of 'done.' */
        if(done) {
            goto ret_false;
        }

        num_vecs = frag->iov_cnt;
#if MCA_BTL_SCTP_ENDPOINT_CACHE
        if( 0 != btl_endpoint->endpoint_cache_length ) {
            size_t length = btl_endpoint->endpoint_cache_length;
            /* It's strange at the first look but cnt have to be set to the full amount of data available.
             * After going to advance_iov_position we will use cnt to detect if there is still some
             * data pending.
             */
            cnt = btl_endpoint->endpoint_cache_length;
            for( i = 0; i < frag->iov_cnt; i++ ) {
                if( length > frag->iov_ptr[i].iov_len )
                    length = frag->iov_ptr[0].iov_len;
                memcpy( frag->iov_ptr[i].iov_base, btl_endpoint->endpoint_cache_pos, length );
                btl_endpoint->endpoint_cache_pos += length;
                btl_endpoint->endpoint_cache_length -= length;
                length = btl_endpoint->endpoint_cache_length;
                if( 0 == length ) {
                    btl_endpoint->endpoint_cache_pos = btl_endpoint->endpoint_cache;
                    break;
                }
            }
            goto advance_iov_position;
        }

        frag->iov_ptr[num_vecs].iov_base = btl_endpoint->endpoint_cache;
        frag->iov_ptr[num_vecs].iov_len  = mca_btl_sctp_component.sctp_endpoint_cache;
        num_vecs++;
#endif  /* MCA_BTL_SCTP_ENDPOINT_CACHE */

        /* non-blocking read, but continue if interrupted */
        cnt = -1;
        while( cnt < 0 ) {

            /* Replaces the traditional readv() of the endpoint_recv_handler. */
            memcpy(frag->iov_ptr->iov_base, buf, len);
            cnt = len;

            if(cnt < 0) {
                /* TODO move full error handling code to recv_handler */
                /* never happens. len would have to passed in -1... */
                /* ...plus I don't think the errno from the sctp_recvmsg
                 *  will percolate this far (reset at other syscalls)!
                 */
                switch(opal_socket_errno) {
                    case EINTR:
                    case EWOULDBLOCK:
                        return false;
                    case EFAULT:
                        opal_output( 0, "mca_btl_sctp_frag_recv: error (%p, %d)\n\t%s(%d)\n",
                                frag->iov_ptr[0].iov_base, (int) frag->iov_ptr[0].iov_len,
                                strerror(opal_socket_errno), (int) frag->iov_cnt );
                    default:
                        opal_output(0, "mca_btl_sctp_frag_recv: failed with errno=%d",
                                opal_socket_errno);
                        mca_btl_sctp_endpoint_close(btl_endpoint);
                        return false;
                }
            }
            if( cnt == 0 ) {
                return false;
            }
            goto advance_iov_position;
        };

advance_iov_position:
        /* if the write didn't complete - update the iovec state */
        num_vecs = frag->iov_cnt;
        for( i = 0; i < num_vecs; i++ ) {
            if( cnt >= (int)frag->iov_ptr->iov_len ) {
                cnt -= frag->iov_ptr->iov_len;
                frag->iov_idx++;
                frag->iov_ptr++;
                frag->iov_cnt--;
            } else {
                frag->iov_ptr->iov_base = (ompi_iov_base_ptr_t)
                    (((unsigned char*)frag->iov_ptr->iov_base) + cnt);
                frag->iov_ptr->iov_len -= cnt;
                cnt = 0;
                break;
            }
        }

ret_false:
        /* NOT SURE IF I NEED THIS BLOCK... */
        /* Further... the reason I do an 'if(done)' check here is that the code
         * beneath gets executed along with the 'goto advance_iov_position' which
         * is hit elsewhere.
         */
        if(done) {
            frag->iov_ptr->iov_base = (ompi_iov_base_ptr_t)
                (((unsigned char*)frag->iov_ptr->iov_base) + cnt);
            frag->iov_ptr->iov_len -= cnt;
            cnt = 0;
        }
        /* ...UP TO HERE. */
#if MCA_BTL_SCTP_ENDPOINT_CACHE
        btl_endpoint->endpoint_cache_length = cnt;
#endif  /* MCA_BTL_SCTP_ENDPOINT_CACHE */

        /* read header */
        if(frag->iov_cnt == 0) {
            if (btl_endpoint->endpoint_nbo) {
                MCA_BTL_SCTP_HDR_NTOH(frag->hdr);
            }
            switch(frag->hdr.type) {
                case MCA_BTL_SCTP_HDR_TYPE_SEND:
                    if(frag->iov_idx == 1 && frag->hdr.size) {
                        frag->iov[1].iov_base = (IOVBASE_TYPE*)(frag+1);
                        frag->iov[1].iov_len = frag->hdr.size;
                        frag->segments[0].seg_addr.pval = frag+1;
                        frag->segments[0].seg_len = frag->hdr.size;
                        frag->iov_cnt++;
                        done = 1;
                        goto repeat;
                    }
                    break;
                case MCA_BTL_SCTP_HDR_TYPE_PUT:
                    if(frag->iov_idx == 1) {
                        frag->iov[1].iov_base = (IOVBASE_TYPE*)frag->segments;
                        frag->iov[1].iov_len = frag->hdr.count * sizeof(mca_btl_base_segment_t);
                        frag->iov_cnt++;
                        done = 1;
                        goto repeat;
                    } else if (frag->iov_idx == 2) {
                        for(i=0; i<frag->hdr.count; i++) {
                            frag->iov[i+2].iov_base = (IOVBASE_TYPE*)ompi_ptr_ltop(frag->segments[i].seg_addr.lval);
                            frag->iov[i+2].iov_len = frag->segments[i].seg_len;
                            frag->iov_cnt++;
                        }
                        done = 1;
                        goto repeat;
                    }
                    break;
                case MCA_BTL_SCTP_HDR_TYPE_GET:
                default:
                    break;
            }
            return true;
        }
        return false;
    }
}