tlv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _TLV_H_
  35. #define _TLV_H_
  36. #include <sys/types.h>
  37. #include <inttypes.h>
  38. #include "dynar.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. enum tlv_opt_type {
  43. TLV_OPT_MSG_SEQ_NUMBER = 0,
  44. TLV_OPT_CLUSTER_NAME = 1,
  45. TLV_OPT_TLS_SUPPORTED = 2,
  46. TLV_OPT_TLS_CLIENT_CERT_REQUIRED = 3,
  47. TLV_OPT_SUPPORTED_MESSAGES = 4,
  48. TLV_OPT_SUPPORTED_OPTIONS = 5,
  49. TLV_OPT_REPLY_ERROR_CODE = 6,
  50. TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE = 7,
  51. TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE = 8,
  52. TLV_OPT_NODE_ID = 9,
  53. TLV_OPT_SUPPORTED_DECISION_ALGORITHMS = 10,
  54. TLV_OPT_DECISION_ALGORITHM = 11,
  55. TLV_OPT_HEARTBEAT_INTERVAL = 12,
  56. TLV_OPT_RING_ID = 13,
  57. TLV_OPT_CONFIG_VERSION = 14,
  58. TLV_OPT_DATA_CENTER_ID = 15,
  59. TLV_OPT_NODE_STATE = 16,
  60. TLV_OPT_NODE_INFO = 17,
  61. TLV_OPT_NODE_LIST_TYPE = 18,
  62. TLV_OPT_VOTE = 19,
  63. TLV_OPT_QUORATE = 20,
  64. TLV_OPT_TIE_BREAKER = 21,
  65. TLV_OPT_HEURISTICS = 22,
  66. TLV_OPT_KEEP_ACTIVE_PARTITION_TIE_BREAKER = 23,
  67. };
  68. enum tlv_tls_supported {
  69. TLV_TLS_UNSUPPORTED = 0,
  70. TLV_TLS_SUPPORTED = 1,
  71. TLV_TLS_REQUIRED = 2,
  72. };
  73. enum tlv_reply_error_code {
  74. TLV_REPLY_ERROR_CODE_NO_ERROR = 0,
  75. TLV_REPLY_ERROR_CODE_UNSUPPORTED_NEEDED_MESSAGE = 1,
  76. TLV_REPLY_ERROR_CODE_UNSUPPORTED_NEEDED_OPTION = 2,
  77. TLV_REPLY_ERROR_CODE_TLS_REQUIRED = 3,
  78. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE = 4,
  79. TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG = 5,
  80. TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED = 6,
  81. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION = 7,
  82. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE = 8,
  83. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG = 9,
  84. TLV_REPLY_ERROR_CODE_INTERNAL_ERROR = 10,
  85. TLV_REPLY_ERROR_CODE_INIT_REQUIRED = 11,
  86. TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM = 12,
  87. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL = 13,
  88. TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM_MESSAGE = 14,
  89. TLV_REPLY_ERROR_CODE_TIE_BREAKER_DIFFERS_FROM_OTHER_NODES = 15,
  90. TLV_REPLY_ERROR_CODE_ALGORITHM_DIFFERS_FROM_OTHER_NODES = 16,
  91. TLV_REPLY_ERROR_CODE_DUPLICATE_NODE_ID = 17,
  92. TLV_REPLY_ERROR_CODE_INVALID_CONFIG_NODE_LIST = 18,
  93. TLV_REPLY_ERROR_CODE_INVALID_MEMBERSHIP_NODE_LIST = 19,
  94. };
  95. enum tlv_decision_algorithm_type {
  96. TLV_DECISION_ALGORITHM_TYPE_TEST = 0,
  97. TLV_DECISION_ALGORITHM_TYPE_FFSPLIT = 1,
  98. TLV_DECISION_ALGORITHM_TYPE_2NODELMS = 2,
  99. TLV_DECISION_ALGORITHM_TYPE_LMS = 3,
  100. };
  101. struct tlv_ring_id {
  102. uint32_t node_id;
  103. uint64_t seq;
  104. };
  105. enum tlv_node_state {
  106. TLV_NODE_STATE_NOT_SET = 0,
  107. TLV_NODE_STATE_MEMBER = 1,
  108. TLV_NODE_STATE_DEAD = 2,
  109. TLV_NODE_STATE_LEAVING = 3,
  110. };
  111. enum tlv_node_list_type {
  112. TLV_NODE_LIST_TYPE_INITIAL_CONFIG = 0,
  113. TLV_NODE_LIST_TYPE_CHANGED_CONFIG = 1,
  114. TLV_NODE_LIST_TYPE_MEMBERSHIP = 2,
  115. TLV_NODE_LIST_TYPE_QUORUM = 3,
  116. };
  117. enum tlv_vote {
  118. TLV_VOTE_UNDEFINED = 0,
  119. TLV_VOTE_ACK = 1,
  120. TLV_VOTE_NACK = 2,
  121. TLV_VOTE_ASK_LATER = 3,
  122. TLV_VOTE_WAIT_FOR_REPLY = 4,
  123. TLV_VOTE_NO_CHANGE = 5,
  124. };
  125. enum tlv_quorate {
  126. TLV_QUORATE_INQUORATE = 0,
  127. TLV_QUORATE_QUORATE = 1,
  128. };
  129. enum tlv_tie_breaker_mode {
  130. TLV_TIE_BREAKER_MODE_LOWEST = 1,
  131. TLV_TIE_BREAKER_MODE_HIGHEST = 2,
  132. TLV_TIE_BREAKER_MODE_NODE_ID = 3,
  133. };
  134. struct tlv_tie_breaker {
  135. enum tlv_tie_breaker_mode mode;
  136. uint32_t node_id;
  137. };
  138. struct tlv_node_info {
  139. uint32_t node_id;
  140. uint32_t data_center_id; /* 0 - data center id was not set */
  141. enum tlv_node_state node_state; /* TLV_NODE_STATE_NOT_SET - state was not set */
  142. };
  143. enum tlv_heuristics {
  144. TLV_HEURISTICS_UNDEFINED = 0,
  145. TLV_HEURISTICS_PASS = 1,
  146. TLV_HEURISTICS_FAIL = 2,
  147. };
  148. enum tlv_keep_active_partition_tie_breaker {
  149. TLV_KEEP_ACTIVE_PARTITION_TIE_BREAKER_DISABLED = 0,
  150. TLV_KEEP_ACTIVE_PARTITION_TIE_BREAKER_ENABLED = 1,
  151. };
  152. struct tlv_iterator {
  153. const char *msg;
  154. size_t msg_len;
  155. size_t current_pos;
  156. size_t msg_header_len;
  157. int iter_next_called;
  158. };
  159. extern int tlv_add(struct dynar *msg, enum tlv_opt_type opt_type,
  160. uint16_t opt_len, const void *value);
  161. extern int tlv_add_u32(struct dynar *msg, enum tlv_opt_type opt_type,
  162. uint32_t u32);
  163. extern int tlv_add_u8(struct dynar *msg, enum tlv_opt_type opt_type,
  164. uint8_t u8);
  165. extern int tlv_add_u16(struct dynar *msg, enum tlv_opt_type opt_type,
  166. uint16_t u16);
  167. extern int tlv_add_u64(struct dynar *msg, enum tlv_opt_type opt_type,
  168. uint64_t u64);
  169. extern int tlv_add_string(struct dynar *msg, enum tlv_opt_type opt_type,
  170. const char *str);
  171. extern int tlv_add_u16_array(struct dynar *msg, enum tlv_opt_type opt_type,
  172. const uint16_t *array, size_t array_size);
  173. extern int tlv_add_supported_options(struct dynar *msg,
  174. const enum tlv_opt_type *supported_options, size_t no_supported_options);
  175. extern int tlv_add_msg_seq_number(struct dynar *msg,
  176. uint32_t msg_seq_number);
  177. extern int tlv_add_cluster_name(struct dynar *msg, const char *cluster_name);
  178. extern int tlv_add_tls_supported(struct dynar *msg,
  179. enum tlv_tls_supported tls_supported);
  180. extern int tlv_add_tls_client_cert_required(struct dynar *msg,
  181. int tls_client_cert_required);
  182. extern int tlv_add_reply_error_code(struct dynar *msg,
  183. enum tlv_reply_error_code error_code);
  184. extern int tlv_add_node_id(struct dynar *msg, uint32_t node_id);
  185. extern int tlv_add_server_maximum_request_size(struct dynar *msg,
  186. size_t server_maximum_request_size);
  187. extern int tlv_add_server_maximum_reply_size(struct dynar *msg,
  188. size_t server_maximum_reply_size);
  189. extern int tlv_add_supported_decision_algorithms(struct dynar *msg,
  190. const enum tlv_decision_algorithm_type *supported_algorithms, size_t no_supported_algorithms);
  191. extern int tlv_add_decision_algorithm(struct dynar *msg,
  192. enum tlv_decision_algorithm_type decision_algorithm);
  193. extern int tlv_add_heartbeat_interval(struct dynar *msg,
  194. uint32_t heartbeat_interval);
  195. extern int tlv_add_ring_id(struct dynar *msg,
  196. const struct tlv_ring_id *ring_id);
  197. extern int tlv_add_tie_breaker(struct dynar *msg,
  198. const struct tlv_tie_breaker *tie_breaker);
  199. extern int tlv_add_config_version(struct dynar *msg,
  200. uint64_t config_version);
  201. extern int tlv_add_data_center_id(struct dynar *msg,
  202. uint32_t data_center_id);
  203. extern int tlv_add_node_state(struct dynar *msg,
  204. enum tlv_node_state node_state);
  205. extern int tlv_add_node_info(struct dynar *msg,
  206. const struct tlv_node_info *node_info);
  207. extern int tlv_add_node_list_type(struct dynar *msg,
  208. enum tlv_node_list_type node_list_type);
  209. extern int tlv_add_vote(struct dynar *msg, enum tlv_vote vote);
  210. extern int tlv_add_quorate(struct dynar *msg, enum tlv_quorate quorate);
  211. extern int tlv_add_heuristics(struct dynar *msg,
  212. enum tlv_heuristics heuristics);
  213. extern int tlv_add_keep_active_partition_tie_breaker(struct dynar *msg,
  214. enum tlv_keep_active_partition_tie_breaker enabled);
  215. extern void tlv_iter_init_str(const char *msg, size_t msg_len,
  216. size_t msg_header_len, struct tlv_iterator *tlv_iter);
  217. extern void tlv_iter_init(const struct dynar *msg, size_t msg_header_len,
  218. struct tlv_iterator *tlv_iter);
  219. extern enum tlv_opt_type tlv_iter_get_type(const struct tlv_iterator *tlv_iter);
  220. extern uint16_t tlv_iter_get_len(const struct tlv_iterator *tlv_iter);
  221. extern const char *tlv_iter_get_data(const struct tlv_iterator *tlv_iter);
  222. extern int tlv_iter_next(struct tlv_iterator *tlv_iter);
  223. extern int tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res);
  224. extern int tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter,
  225. enum tlv_tls_supported *tls_supported);
  226. extern int tlv_iter_decode_u32(struct tlv_iterator *tlv_iter,
  227. uint32_t *res);
  228. extern int tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str,
  229. size_t *str_len);
  230. extern int tlv_iter_decode_client_cert_required(
  231. struct tlv_iterator *tlv_iter, uint8_t *client_cert_required);
  232. extern int tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter,
  233. uint16_t **u16a, size_t *no_items);
  234. extern int tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter,
  235. enum tlv_opt_type **supported_options, size_t *no_supported_options);
  236. extern int tlv_iter_decode_supported_decision_algorithms(
  237. struct tlv_iterator *tlv_iter,
  238. enum tlv_decision_algorithm_type **supported_decision_algorithms,
  239. size_t *no_supported_decision_algorithms);
  240. extern int tlv_iter_decode_u16(struct tlv_iterator *tlv_iter,
  241. uint16_t *u16);
  242. extern int tlv_iter_decode_u64(struct tlv_iterator *tlv_iter,
  243. uint64_t *u64);
  244. extern int tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter,
  245. enum tlv_reply_error_code *reply_error_code);
  246. extern int tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter,
  247. enum tlv_decision_algorithm_type *decision_algorithm);
  248. extern int tlv_iter_decode_ring_id(struct tlv_iterator *tlv_iter,
  249. struct tlv_ring_id *ring_id);
  250. extern int tlv_iter_decode_tie_breaker(struct tlv_iterator *tlv_iter,
  251. struct tlv_tie_breaker *tie_breaker);
  252. extern int tlv_iter_decode_node_state(struct tlv_iterator *tlv_iter,
  253. enum tlv_node_state *node_state);
  254. extern int tlv_iter_decode_node_info(struct tlv_iterator *tlv_iter,
  255. struct tlv_node_info *node_info);
  256. extern int tlv_iter_decode_node_list_type(struct tlv_iterator *tlv_iter,
  257. enum tlv_node_list_type *node_list_type);
  258. extern int tlv_iter_decode_vote(struct tlv_iterator *tlv_iter,
  259. enum tlv_vote *vote);
  260. extern int tlv_iter_decode_quorate(struct tlv_iterator *tlv_iter,
  261. enum tlv_quorate *quorate);
  262. extern int tlv_iter_decode_heuristics(struct tlv_iterator *tlv_iter,
  263. enum tlv_heuristics *heuristics);
  264. extern int tlv_iter_decode_keep_active_partition_tie_breaker(
  265. struct tlv_iterator *tlv_iter,
  266. enum tlv_keep_active_partition_tie_breaker *keep_active_partition_tie_breaker);
  267. extern void tlv_get_supported_options(enum tlv_opt_type **supported_options,
  268. size_t *no_supported_options);
  269. extern int tlv_ring_id_eq(const struct tlv_ring_id *rid1,
  270. const struct tlv_ring_id *rid2);
  271. extern int tlv_tie_breaker_eq(const struct tlv_tie_breaker *tb1,
  272. const struct tlv_tie_breaker *tb2);
  273. extern const char *tlv_vote_to_str(enum tlv_vote vote);
  274. extern const char *tlv_node_state_to_str(enum tlv_node_state state);
  275. extern const char *tlv_tls_supported_to_str(enum tlv_tls_supported tls_supported);
  276. extern const char *tlv_decision_algorithm_type_to_str(
  277. enum tlv_decision_algorithm_type algorithm);
  278. extern const char *tlv_heuristics_to_str(enum tlv_heuristics heuristics);
  279. extern const char *tlv_keep_active_partition_tie_breaker_to_str(
  280. enum tlv_keep_active_partition_tie_breaker kap_tb);
  281. /*
  282. * Compare h1 and h2. Return -1 if h1 < h2, 0 if h1 == h2 and 1 if h1 > h2
  283. */
  284. extern int tlv_heuristics_cmp(enum tlv_heuristics h1, enum tlv_heuristics h2);
  285. #ifdef __cplusplus
  286. }
  287. #endif
  288. #endif /* _TLV_H_ */