-
Notifications
You must be signed in to change notification settings - Fork 543
/
return_code.h
414 lines (365 loc) · 17.3 KB
/
return_code.h
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
#pragma once
#include <cassert>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include "sai_serialize.h"
#include "status_code_util.h"
extern "C"
{
#include "sai.h"
}
using swss::StatusCode;
// RETURN_IF_ERROR evaluates an expression that returns a ReturnCode. If the
// result is not ok, returns the result. Otherwise, continues.
//
// Example:
// ReturnCode Foo() {...}
// ReturnCode Bar() {
// RETURN_IF_ERROR(Foo());
// return ReturnCode();
// }
#define RETURN_IF_ERROR(expr) \
do \
{ \
ReturnCode RETURN_IF_ERROR_RC_ = expr; \
if (!RETURN_IF_ERROR_RC_.ok()) \
return RETURN_IF_ERROR_RC_; \
} while (0)
// LOG_ERROR_AND_RETURN evaluates an expression that should returns an error
// ReturnCode. Logs the error message in the ReturnCode by calling
// SWSS_LOG_ERROR and returns.
#define LOG_ERROR_AND_RETURN(expr) \
do \
{ \
ReturnCode LOG_ERROR_AND_RETURN_RC_ = expr; \
SWSS_LOG_ERROR("%s", LOG_ERROR_AND_RETURN_RC_.message().c_str()); \
return LOG_ERROR_AND_RETURN_RC_; \
} while (0)
// Same as RETURN_IF_ERROR, plus a call of SWSS_LOG_ERROR for the return code
// error message.
#define LOG_AND_RETURN_IF_ERROR(expr) \
do \
{ \
ReturnCode LOG_AND_RETURN_IF_ERROR_RC_ = expr; \
if (!LOG_AND_RETURN_IF_ERROR_RC_.ok()) \
{ \
SWSS_LOG_ERROR("%s", LOG_AND_RETURN_IF_ERROR_RC_.message().c_str()); \
return LOG_AND_RETURN_IF_ERROR_RC_; \
} \
} while (0)
#define RETURNCODE_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
#define RETURNCODE_MACROS_IMPL_CONCAT_(x, y) RETURNCODE_MACROS_IMPL_CONCAT_INNER_(x, y)
// ASSIGN_OR_RETURN evaluates an expression that returns a ReturnCodeOr. If the
// result is ok, the value is saved to dest. Otherwise, the ReturnCode is
// returned.
//
// Example:
// ReturnCodeOr<int> Foo() {...}
// ReturnCode Bar() {
// ASSIGN_OR_RETURN(int value, Foo());
// std::cout << "value: " << value;
// return ReturnCode();
// }
#define ASSIGN_OR_RETURN(dest, expr) \
auto RETURNCODE_MACROS_IMPL_CONCAT_(ASSIGN_OR_RETURN_RESULT_, __LINE__) = expr; \
if (!RETURNCODE_MACROS_IMPL_CONCAT_(ASSIGN_OR_RETURN_RESULT_, __LINE__).ok()) \
{ \
return RETURNCODE_MACROS_IMPL_CONCAT_(ASSIGN_OR_RETURN_RESULT_, __LINE__).status(); \
} \
dest = std::move(RETURNCODE_MACROS_IMPL_CONCAT_(ASSIGN_OR_RETURN_RESULT_, __LINE__).value())
// CHECK_ERROR_AND_LOG evaluates an expression that returns a sai_status_t. If
// the result is not SAI_STATUS_SUCCESS, it will log an error message.
//
// Example:
// CHECK_ERROR_AND_LOG(
// sai_router_intfs_api->set_router_interface_attribute(...),
// "error message" << " stream");
#define CHECK_ERROR_AND_LOG(expr, msg_stream) \
do \
{ \
sai_status_t CHECK_ERROR_AND_LOG_SAI_ = expr; \
if (CHECK_ERROR_AND_LOG_SAI_ != SAI_STATUS_SUCCESS) \
{ \
std::stringstream CHECK_ERROR_AND_LOG_SS_; \
CHECK_ERROR_AND_LOG_SS_ << msg_stream; \
SWSS_LOG_ERROR("%s SAI_STATUS: %s", CHECK_ERROR_AND_LOG_SS_.str().c_str(), \
sai_serialize_status(CHECK_ERROR_AND_LOG_SAI_).c_str()); \
} \
} while (0)
// CHECK_ERROR_AND_LOG_AND_RETURN evaluates an expression that returns a
// sai_status_t. If the result is not SAI_STATUS_SUCCESS, it will log an error
// message and return a ReturnCode.
//
// Example:
// CHECK_ERROR_AND_LOG_AND_RETURN(
// sai_router_intfs_api->set_router_interface_attribute(...),
// "error message" << " stream");
#define CHECK_ERROR_AND_LOG_AND_RETURN(expr, msg_stream) \
do \
{ \
sai_status_t CHECK_ERROR_AND_LOG_AND_RETURN_SAI_ = expr; \
if (CHECK_ERROR_AND_LOG_AND_RETURN_SAI_ != SAI_STATUS_SUCCESS) \
{ \
ReturnCode CHECK_ERROR_AND_LOG_AND_RETURN_RC_ = ReturnCode(CHECK_ERROR_AND_LOG_AND_RETURN_SAI_) \
<< msg_stream; \
SWSS_LOG_ERROR("%s SAI_STATUS: %s", CHECK_ERROR_AND_LOG_AND_RETURN_RC_.message().c_str(), \
sai_serialize_status(CHECK_ERROR_AND_LOG_AND_RETURN_SAI_).c_str()); \
return CHECK_ERROR_AND_LOG_AND_RETURN_RC_; \
} \
} while (0)
// This macro raises critical state to indicate that something is seriously
// wrong in the system. Currently, this macro just logs an error message.
// TODO: Implement this macro.
#define SWSS_RAISE_CRITICAL_STATE(err_str) \
do \
{ \
std::string err_msge = err_str; \
SWSS_LOG_ERROR("Orchagent is in critical state: %s", err_msge.c_str()); \
} while (0)
// RETURN_INTERNAL_ERROR_AND_RAISE_CRITICAL returns an error status of
// SWSS_RC_INTERNAL. It also logs the error message and reports critical state.
#define RETURN_INTERNAL_ERROR_AND_RAISE_CRITICAL(msg_stream) \
do \
{ \
ReturnCode RETURN_INTERNAL_ERROR_AND_RAISE_CRITICAL_RC_ = ReturnCode(StatusCode::SWSS_RC_INTERNAL) \
<< msg_stream; \
SWSS_LOG_ERROR("%s", RETURN_INTERNAL_ERROR_AND_RAISE_CRITICAL_RC_.message().c_str()); \
SWSS_RAISE_CRITICAL_STATE(RETURN_INTERNAL_ERROR_AND_RAISE_CRITICAL_RC_.message()); \
return RETURN_INTERNAL_ERROR_AND_RAISE_CRITICAL_RC_; \
} while (0)
#define SAI_RANGED_STATUS_IS_INVALID_ATTRIBUTE(x) \
((SAI_STATUS_CODE(x) & ~(0xFFFFL)) == SAI_STATUS_CODE(SAI_STATUS_INVALID_ATTRIBUTE_0))
#define SAI_RANGED_STATUS_IS_INVALID_ATTR_VALUE(x) \
((SAI_STATUS_CODE(x) & ~(0xFFFFL)) == SAI_STATUS_CODE(SAI_STATUS_INVALID_ATTR_VALUE_0))
#define SAI_RANGED_STATUS_IS_ATTR_NOT_IMPLEMENTED(x) \
((SAI_STATUS_CODE(x) & ~(0xFFFFL)) == SAI_STATUS_CODE(SAI_STATUS_ATTR_NOT_IMPLEMENTED_0))
#define SAI_RANGED_STATUS_IS_UNKNOWN_ATTRIBUTE(x) \
((SAI_STATUS_CODE(x) & ~(0xFFFFL)) == SAI_STATUS_CODE(SAI_STATUS_UNKNOWN_ATTRIBUTE_0))
#define SAI_RANGED_STATUS_IS_ATTR_NOT_SUPPORTED(x) \
((SAI_STATUS_CODE(x) & ~(0xFFFFL)) == SAI_STATUS_CODE(SAI_STATUS_ATTR_NOT_SUPPORTED_0))
class ReturnCode
{
public:
ReturnCode()
: status_(StatusCode::SWSS_RC_SUCCESS), stream_(std::ios_base::out | std::ios_base::ate), is_sai_(false)
{
}
ReturnCode(const StatusCode &status, const std::string &message = "")
: status_(status), stream_(std::ios_base::out | std::ios_base::ate), is_sai_(false)
{
stream_ << message;
}
ReturnCode(const sai_status_t &status, const std::string &message = "")
: stream_(std::ios_base::out | std::ios_base::ate), is_sai_(true)
{
// Non-ranged SAI codes that are not included in this lookup map will map to
// SWSS_RC_UNKNOWN. This includes the general SAI failure:
// SAI_STATUS_FAILURE.
static const auto *const saiStatusCodeLookup = new std::unordered_map<sai_status_t, StatusCode>({
{SAI_STATUS_SUCCESS, StatusCode::SWSS_RC_SUCCESS},
{SAI_STATUS_NOT_SUPPORTED, StatusCode::SWSS_RC_UNIMPLEMENTED},
{SAI_STATUS_NO_MEMORY, StatusCode::SWSS_RC_NO_MEMORY},
{SAI_STATUS_INSUFFICIENT_RESOURCES, StatusCode::SWSS_RC_FULL},
{SAI_STATUS_INVALID_PARAMETER, StatusCode::SWSS_RC_INVALID_PARAM},
{SAI_STATUS_ITEM_ALREADY_EXISTS, StatusCode::SWSS_RC_EXISTS},
{SAI_STATUS_ITEM_NOT_FOUND, StatusCode::SWSS_RC_NOT_FOUND},
{SAI_STATUS_TABLE_FULL, StatusCode::SWSS_RC_FULL},
{SAI_STATUS_NOT_IMPLEMENTED, StatusCode::SWSS_RC_UNIMPLEMENTED},
{SAI_STATUS_OBJECT_IN_USE, StatusCode::SWSS_RC_IN_USE},
{SAI_STATUS_NOT_EXECUTED, StatusCode::SWSS_RC_NOT_EXECUTED},
});
if (saiStatusCodeLookup->find(status) == saiStatusCodeLookup->end())
{
// Check for ranged SAI codes.
if (SAI_RANGED_STATUS_IS_INVALID_ATTRIBUTE(status))
{
status_ = StatusCode::SWSS_RC_INVALID_PARAM;
}
else if (SAI_RANGED_STATUS_IS_INVALID_ATTR_VALUE(status))
{
status_ = StatusCode::SWSS_RC_INVALID_PARAM;
}
else if (SAI_RANGED_STATUS_IS_ATTR_NOT_IMPLEMENTED(status))
{
status_ = StatusCode::SWSS_RC_UNIMPLEMENTED;
}
else if (SAI_RANGED_STATUS_IS_UNKNOWN_ATTRIBUTE(status))
{
status_ = StatusCode::SWSS_RC_INVALID_PARAM;
}
else if (SAI_RANGED_STATUS_IS_ATTR_NOT_SUPPORTED(status))
{
status_ = StatusCode::SWSS_RC_UNIMPLEMENTED;
}
else
{
status_ = StatusCode::SWSS_RC_UNKNOWN;
}
}
else
{
status_ = saiStatusCodeLookup->at(status);
}
stream_ << message;
}
ReturnCode(const ReturnCode &return_code) : stream_(std::ios_base::out | std::ios_base::ate)
{
status_ = return_code.status_;
stream_ << return_code.stream_.str();
is_sai_ = return_code.is_sai_;
}
ReturnCode &operator=(const ReturnCode &return_code)
{
status_ = return_code.status_;
stream_.str(return_code.stream_.str());
is_sai_ = return_code.is_sai_;
return *this;
}
~ReturnCode() = default;
bool ok() const
{
return status_ == StatusCode::SWSS_RC_SUCCESS;
}
StatusCode code() const
{
return status_;
}
std::string codeStr() const
{
return swss::statusCodeToStr(status_);
}
std::string message() const
{
if (stream_.str().empty())
{
return codeStr();
}
return stream_.str();
}
ReturnCode &prepend(const std::string &msg)
{
const std::string &tmp = stream_.str();
stream_.str(msg + tmp);
return *this;
}
std::string toString() const
{
return codeStr() + ":" + message();
}
// Whether the ReturnCode is generated from a SAI status code or not.
bool isSai() const
{
return is_sai_;
}
template <typename T> ReturnCode &operator<<(T val)
{
stream_ << val;
return *this;
}
bool operator==(const ReturnCode &x) const
{
return status_ == x.status_ && message() == x.message();
}
bool operator!=(const ReturnCode &x) const
{
return status_ != x.status_ || message() != x.message();
}
bool operator==(const StatusCode &x) const
{
return status_ == x;
}
bool operator!=(const StatusCode &x) const
{
return status_ != x;
}
private:
StatusCode status_;
std::stringstream stream_;
// Whether the ReturnCode is generated from a SAI status code or not.
bool is_sai_;
};
inline bool operator==(const StatusCode &lhs, const ReturnCode &rhs)
{
return lhs == rhs.code();
}
inline bool operator!=(const StatusCode &lhs, const ReturnCode &rhs)
{
return lhs != rhs.code();
}
template <typename T> class ReturnCodeOr
{
public:
using value_type = T;
// Value Constructors.
ReturnCodeOr(const T &value) : return_code_(ReturnCode()), value_(std::unique_ptr<T>(new T(value)))
{
}
ReturnCodeOr(T &&value) : return_code_(ReturnCode()), value_(std::unique_ptr<T>(new T(std::move(value))))
{
}
// ReturnCode constructors.
ReturnCodeOr(const ReturnCode &return_code) : return_code_(return_code)
{
assert(!return_code.ok());
}
// ReturnCode accessors.
bool ok() const
{
return return_code_.ok();
}
const ReturnCode &status() const
{
return return_code_;
}
// Value accessors.
const T &value() const &
{
assert(return_code_.ok());
return *value_;
}
T &value() &
{
assert(return_code_.ok());
return *value_;
}
const T &&value() const &&
{
assert(return_code_.ok());
return std::move(*value_);
}
T &&value() &&
{
assert(return_code_.ok());
return std::move(*value_);
}
const T &operator*() const &
{
return value();
}
T &operator*() &
{
return value();
}
const T &&operator*() const &&
{
return value();
}
T &&operator*() &&
{
return value();
}
const T *operator->() const
{
return value_.get();
}
T *operator->()
{
return value_.get();
}
private:
ReturnCode return_code_;
std::unique_ptr<T> value_;
};