This repository was archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer_cfg.c
192 lines (150 loc) · 4.24 KB
/
peer_cfg.c
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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2022, Intel Corporation */
/*
* peer_cfg.c -- librpma peer-configuration-related implementations
*/
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include "librpma.h"
#include "log_internal.h"
#include "debug.h"
#ifdef TEST_MOCK_ALLOC
#include "cmocka_alloc.h"
#endif
#ifdef ATOMIC_OPERATIONS_SUPPORTED
#include <stdatomic.h>
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
#define SUPPORTED2STR(var) ((var) ? "supported" : "unsupported")
static bool RPMA_DEFAULT_DIRECT_WRITE_TO_PMEM = false;
struct rpma_peer_cfg {
#ifdef ATOMIC_OPERATIONS_SUPPORTED
_Atomic
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
bool direct_write_to_pmem;
};
/* public librpma API */
/*
* rpma_peer_cfg_new -- create a new peer configuration object
*/
int
rpma_peer_cfg_new(struct rpma_peer_cfg **pcfg_ptr)
{
RPMA_DEBUG_TRACE;
RPMA_FAULT_INJECTION(RPMA_E_NOMEM, {});
if (pcfg_ptr == NULL)
return RPMA_E_INVAL;
struct rpma_peer_cfg *cfg = malloc(sizeof(struct rpma_peer_cfg));
if (cfg == NULL)
return RPMA_E_NOMEM;
/* set default values */
#ifdef ATOMIC_OPERATIONS_SUPPORTED
atomic_init(&cfg->direct_write_to_pmem, RPMA_DEFAULT_DIRECT_WRITE_TO_PMEM);
#else
cfg->direct_write_to_pmem = RPMA_DEFAULT_DIRECT_WRITE_TO_PMEM;
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
*pcfg_ptr = cfg;
return 0;
}
/*
* rpma_peer_cfg_delete -- delete the peer configuration object
*/
int
rpma_peer_cfg_delete(struct rpma_peer_cfg **pcfg_ptr)
{
RPMA_DEBUG_TRACE;
if (pcfg_ptr == NULL)
return RPMA_E_INVAL;
free(*pcfg_ptr);
*pcfg_ptr = NULL;
RPMA_FAULT_INJECTION(RPMA_E_INVAL, {});
return 0;
}
/*
* rpma_peer_cfg_set_direct_write_to_pmem -- declare if direct write to PMEM is supported
*/
int
rpma_peer_cfg_set_direct_write_to_pmem(struct rpma_peer_cfg *pcfg, bool supported)
{
RPMA_DEBUG_TRACE;
RPMA_FAULT_INJECTION(RPMA_E_INVAL, {});
if (pcfg == NULL)
return RPMA_E_INVAL;
#ifdef ATOMIC_OPERATIONS_SUPPORTED
atomic_store_explicit(&pcfg->direct_write_to_pmem, supported, __ATOMIC_SEQ_CST);
#else
pcfg->direct_write_to_pmem = supported;
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
return 0;
}
/*
* rpma_peer_cfg_get_direct_write_to_pmem -- check if direct write to PMEM is supported
*/
int
rpma_peer_cfg_get_direct_write_to_pmem(const struct rpma_peer_cfg *pcfg, bool *supported)
{
RPMA_DEBUG_TRACE;
RPMA_FAULT_INJECTION(RPMA_E_INVAL, {});
if (pcfg == NULL || supported == NULL)
return RPMA_E_INVAL;
#ifdef ATOMIC_OPERATIONS_SUPPORTED
*supported = atomic_load_explicit((_Atomic bool *)&pcfg->direct_write_to_pmem,
__ATOMIC_SEQ_CST);
#else
*supported = pcfg->direct_write_to_pmem;
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
return 0;
}
/*
* rpma_peer_cfg_get_descriptor -- get a descriptor of a peer configuration
*/
int
rpma_peer_cfg_get_descriptor(const struct rpma_peer_cfg *pcfg, void *desc)
{
RPMA_DEBUG_TRACE;
RPMA_FAULT_INJECTION(RPMA_E_INVAL, {});
if (pcfg == NULL || desc == NULL)
return RPMA_E_INVAL;
bool direct_write_to_pmem;
rpma_peer_cfg_get_direct_write_to_pmem(pcfg, &direct_write_to_pmem);
*((uint8_t *)desc) = (uint8_t)direct_write_to_pmem;
return 0;
}
/*
* rpma_peer_cfg_get_descriptor_size -- get size of the peer configuration descriptor
*/
int
rpma_peer_cfg_get_descriptor_size(const struct rpma_peer_cfg *pcfg, size_t *desc_size)
{
RPMA_DEBUG_TRACE;
RPMA_FAULT_INJECTION(RPMA_E_INVAL, {});
if (pcfg == NULL || desc_size == NULL)
return RPMA_E_INVAL;
*desc_size = sizeof(uint8_t);
return 0;
}
/*
* rpma_peer_cfg_from_descriptor -- create a peer configuration from a descriptor
*/
int
rpma_peer_cfg_from_descriptor(const void *desc, size_t desc_size, struct rpma_peer_cfg **pcfg_ptr)
{
RPMA_DEBUG_TRACE;
RPMA_FAULT_INJECTION(RPMA_E_INVAL, {});
if (desc == NULL || pcfg_ptr == NULL)
return RPMA_E_INVAL;
if (desc_size < sizeof(uint8_t)) {
RPMA_LOG_ERROR(
"incorrect size of the descriptor: %i bytes (should be at least %i bytes)",
desc_size, sizeof(uint8_t));
return RPMA_E_INVAL;
}
struct rpma_peer_cfg *cfg = malloc(sizeof(struct rpma_peer_cfg));
if (cfg == NULL)
return RPMA_E_NOMEM;
cfg->direct_write_to_pmem = *(uint8_t *)desc;
*pcfg_ptr = cfg;
RPMA_LOG_INFO("INFO: Direct Write To PMem is %s", SUPPORTED2STR(cfg->direct_write_to_pmem));
return 0;
}