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
|
/*
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* This utility provides a sliding receive window implementation. It uses a
* circular queue to order incoming messages based on the message ID in the
* transport protocol.
*/
#if !defined(FI_RECVWIN_H)
#define FI_RECVWIN_H
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <ofi.h>
#include <ofi_rbuf.h>
#define OFI_DECL_RECVWIN_BUF(entrytype, name, id_type) \
OFI_DECLARE_CIRQUE(entrytype, recvwin_cirq); \
struct name { \
id_type exp_msg_id; \
id_type win_size; \
struct recvwin_cirq *pending; \
}; \
\
static inline struct name * \
ofi_recvwin_buf_alloc(struct name *recvq, unsigned int size) \
{ \
assert(size == roundup_power_of_two(size)); \
recvq->exp_msg_id = 0; \
recvq->win_size = size; \
recvq->pending = recvwin_cirq_create(recvq->win_size); \
return recvq; \
} \
\
static inline void \
ofi_recvwin_free(struct name *recvq) \
{ \
recvwin_cirq_free(recvq->pending); \
} \
\
static inline int \
ofi_recvwin_id_valid(struct name *recvq, id_type id) \
{ \
return ofi_recvwin_id_valid_ ## id_type (recvq, id); \
} \
\
static inline int \
ofi_recvwin_id_processed(struct name *recvq, id_type id) \
{ \
return ofi_recvwin_id_processed_ ## id_type (recvq, id); \
} \
\
static inline int \
ofi_recvwin_queue_msg(struct name *recvq, entrytype * msg, id_type id) \
{ \
size_t write_idx; \
\
assert(ofi_recvwin_id_valid(recvq, id)); \
write_idx = (ofi_cirque_rindex(recvq->pending) \
+ (id - recvq->exp_msg_id)) \
& recvq->pending->size_mask; \
recvq->pending->buf[write_idx] = *msg; \
ofi_cirque_commit(recvq->pending); \
return 0; \
} \
\
static inline entrytype * \
ofi_recvwin_get_msg(struct name *recvq, id_type id) \
{ \
size_t read_idx; \
\
assert(ofi_recvwin_id_valid(recvq, id)); \
read_idx = (ofi_cirque_rindex(recvq->pending) \
+ (id - recvq->exp_msg_id)) \
& recvq->pending->size_mask; \
return &recvq->pending->buf[read_idx]; \
} \
\
static inline entrytype * \
ofi_recvwin_get_next_msg(struct name *recvq) \
{ \
if (ofi_cirque_head(recvq->pending)) { \
ofi_recvwin_exp_inc(recvq); \
return ofi_cirque_remove(recvq->pending); \
} else { \
return NULL; \
} \
} \
\
static inline void \
ofi_recvwin_slide(struct name *recvq) \
{ \
ofi_recvwin_exp_inc(recvq); \
ofi_cirque_discard(recvq->pending); \
ofi_cirque_commit(recvq->pending); \
}
#define ofi_recvwin_peek(rq) (ofi_cirque_head(rq->pending))
#define ofi_recvwin_is_empty(rq) (ofi_cirque_isempty(rq->pending))
#define ofi_recvwin_exp_inc(rq) ((rq)->exp_msg_id++)
#define ofi_recvwin_is_exp(rq, id) ((rq)->exp_msg_id == id)
#define ofi_recvwin_next_exp_id(rq) ((rq)->exp_msg_id)
/*
* When exp_msg_id on the receiver has not wrapped around but the sender ID has
* we need to allow the IDs starting from 0 that are valid. These macros use
* the overflow of exp_msg_id to validate that.
*/
#define ofi_recvwin_id_valid_uint32_t(rq, id) \
ofi_val32_inrange(rq->exp_msg_id, rq->win_size, id)
#define ofi_recvwin_id_valid_uint64_t(rq, id) \
ofi_val64_inrange(rq->exp_msg_id, rq->win_size, id)
#define ofi_recvwin_id_processed_uint32_t(rq, id) \
ofi_val32_gt(rq->exp_msg_id, id)
#define ofi_recvwin_id_processed_uint64_t(rq, id) \
ofi_val64_gt(rq->exp_msg_id, id)
#endif /* FI_RECVWIN_H */
|