tlv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2015-2017 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. };
  67. enum tlv_tls_supported {
  68. TLV_TLS_UNSUPPORTED = 0,
  69. TLV_TLS_SUPPORTED = 1,
  70. TLV_TLS_REQUIRED = 2,
  71. };
  72. enum tlv_reply_error_code {
  73. TLV_REPLY_ERROR_CODE_NO_ERROR = 0,
  74. TLV_REPLY_ERROR_CODE_UNSUPPORTED_NEEDED_MESSAGE = 1,
  75. TLV_REPLY_ERROR_CODE_UNSUPPORTED_NEEDED_OPTION = 2,
  76. TLV_REPLY_ERROR_CODE_TLS_REQUIRED = 3,
  77. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE = 4,
  78. TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG = 5,
  79. TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED = 6,
  80. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION = 7,
  81. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE = 8,
  82. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG = 9,
  83. TLV_REPLY_ERROR_CODE_INTERNAL_ERROR = 10,
  84. TLV_REPLY_ERROR_CODE_INIT_REQUIRED = 11,
  85. TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM = 12,
  86. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL = 13,
  87. TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM_MESSAGE = 14,
  88. TLV_REPLY_ERROR_CODE_TIE_BREAKER_DIFFERS_FROM_OTHER_NODES = 15,
  89. TLV_REPLY_ERROR_CODE_ALGORITHM_DIFFERS_FROM_OTHER_NODES = 16,
  90. TLV_REPLY_ERROR_CODE_DUPLICATE_NODE_ID = 17,
  91. TLV_REPLY_ERROR_CODE_INVALID_CONFIG_NODE_LIST = 18,
  92. TLV_REPLY_ERROR_CODE_INVALID_MEMBERSHIP_NODE_LIST = 19,
  93. };
  94. enum tlv_decision_algorithm_type {
  95. TLV_DECISION_ALGORITHM_TYPE_TEST = 0,
  96. TLV_DECISION_ALGORITHM_TYPE_FFSPLIT = 1,
  97. TLV_DECISION_ALGORITHM_TYPE_2NODELMS = 2,
  98. TLV_DECISION_ALGORITHM_TYPE_LMS = 3,
  99. };
  100. struct tlv_ring_id {
  101. uint32_t node_id;
  102. uint64_t seq;
  103. };
  104. enum tlv_node_state {
  105. TLV_NODE_STATE_NOT_SET = 0,
  106. TLV_NODE_STATE_MEMBER = 1,
  107. TLV_NODE_STATE_DEAD = 2,
  108. TLV_NODE_STATE_LEAVING = 3,
  109. };
  110. enum tlv_node_list_type {
  111. TLV_NODE_LIST_TYPE_INITIAL_CONFIG = 0,
  112. TLV_NODE_LIST_TYPE_CHANGED_CONFIG = 1,
  113. TLV_NODE_LIST_TYPE_MEMBERSHIP = 2,
  114. TLV_NODE_LIST_TYPE_QUORUM = 3,
  115. };
  116. enum tlv_vote {
  117. TLV_VOTE_UNDEFINED = 0,
  118. TLV_VOTE_ACK = 1,
  119. TLV_VOTE_NACK = 2,
  120. TLV_VOTE_ASK_LATER = 3,
  121. TLV_VOTE_WAIT_FOR_REPLY = 4,
  122. TLV_VOTE_NO_CHANGE = 5,
  123. };
  124. enum tlv_quorate {
  125. TLV_QUORATE_INQUORATE = 0,
  126. TLV_QUORATE_QUORATE = 1,
  127. };
  128. enum tlv_tie_breaker_mode {
  129. TLV_TIE_BREAKER_MODE_LOWEST = 1,
  130. TLV_TIE_BREAKER_MODE_HIGHEST = 2,
  131. TLV_TIE_BREAKER_MODE_NODE_ID = 3,
  132. };
  133. struct tlv_tie_breaker {
  134. enum tlv_tie_breaker_mode mode;
  135. uint32_t node_id;
  136. };
  137. struct tlv_node_info {
  138. uint32_t node_id;
  139. uint32_t data_center_id; /* 0 - data center id was not set */
  140. enum tlv_node_state node_state; /* TLV_NODE_STATE_NOT_SET - state was not set */
  141. };
  142. enum tlv_heuristics {
  143. TLV_HEURISTICS_UNDEFINED = 0,
  144. TLV_HEURISTICS_PASS = 1,
  145. TLV_HEURISTICS_FAIL = 2,
  146. };
  147. struct tlv_iterator {
  148. const char *msg;
  149. size_t msg_len;
  150. size_t current_pos;
  151. size_t msg_header_len;
  152. int iter_next_called;
  153. };
  154. extern int tlv_add(struct dynar *msg, enum tlv_opt_type opt_type,
  155. uint16_t opt_len, const void *value);
  156. extern int tlv_add_u32(struct dynar *msg, enum tlv_opt_type opt_type,
  157. uint32_t u32);
  158. extern int tlv_add_u8(struct dynar *msg, enum tlv_opt_type opt_type,
  159. uint8_t u8);
  160. extern int tlv_add_u16(struct dynar *msg, enum tlv_opt_type opt_type,
  161. uint16_t u16);
  162. extern int tlv_add_u64(struct dynar *msg, enum tlv_opt_type opt_type,
  163. uint64_t u64);
  164. extern int tlv_add_string(struct dynar *msg, enum tlv_opt_type opt_type,
  165. const char *str);
  166. extern int tlv_add_u16_array(struct dynar *msg, enum tlv_opt_type opt_type,
  167. const uint16_t *array, size_t array_size);
  168. extern int tlv_add_supported_options(struct dynar *msg,
  169. const enum tlv_opt_type *supported_options, size_t no_supported_options);
  170. extern int tlv_add_msg_seq_number(struct dynar *msg,
  171. uint32_t msg_seq_number);
  172. extern int tlv_add_cluster_name(struct dynar *msg, const char *cluster_name);
  173. extern int tlv_add_tls_supported(struct dynar *msg,
  174. enum tlv_tls_supported tls_supported);
  175. extern int tlv_add_tls_client_cert_required(struct dynar *msg,
  176. int tls_client_cert_required);
  177. extern int tlv_add_reply_error_code(struct dynar *msg,
  178. enum tlv_reply_error_code error_code);
  179. extern int tlv_add_node_id(struct dynar *msg, uint32_t node_id);
  180. extern int tlv_add_server_maximum_request_size(struct dynar *msg,
  181. size_t server_maximum_request_size);
  182. extern int tlv_add_server_maximum_reply_size(struct dynar *msg,
  183. size_t server_maximum_reply_size);
  184. extern int tlv_add_supported_decision_algorithms(struct dynar *msg,
  185. const enum tlv_decision_algorithm_type *supported_algorithms, size_t no_supported_algorithms);
  186. extern int tlv_add_decision_algorithm(struct dynar *msg,
  187. enum tlv_decision_algorithm_type decision_algorithm);
  188. extern int tlv_add_heartbeat_interval(struct dynar *msg,
  189. uint32_t heartbeat_interval);
  190. extern int tlv_add_ring_id(struct dynar *msg,
  191. const struct tlv_ring_id *ring_id);
  192. extern int tlv_add_tie_breaker(struct dynar *msg,
  193. const struct tlv_tie_breaker *tie_breaker);
  194. extern int tlv_add_config_version(struct dynar *msg,
  195. uint64_t config_version);
  196. extern int tlv_add_data_center_id(struct dynar *msg,
  197. uint32_t data_center_id);
  198. extern int tlv_add_node_state(struct dynar *msg,
  199. enum tlv_node_state node_state);
  200. extern int tlv_add_node_info(struct dynar *msg,
  201. const struct tlv_node_info *node_info);
  202. extern int tlv_add_node_list_type(struct dynar *msg,
  203. enum tlv_node_list_type node_list_type);
  204. extern int tlv_add_vote(struct dynar *msg, enum tlv_vote vote);
  205. extern int tlv_add_quorate(struct dynar *msg, enum tlv_quorate quorate);
  206. extern int tlv_add_heuristics(struct dynar *msg,
  207. enum tlv_heuristics heuristics);
  208. extern void tlv_iter_init_str(const char *msg, size_t msg_len,
  209. size_t msg_header_len, struct tlv_iterator *tlv_iter);
  210. extern void tlv_iter_init(const struct dynar *msg, size_t msg_header_len,
  211. struct tlv_iterator *tlv_iter);
  212. extern enum tlv_opt_type tlv_iter_get_type(const struct tlv_iterator *tlv_iter);
  213. extern uint16_t tlv_iter_get_len(const struct tlv_iterator *tlv_iter);
  214. extern const char *tlv_iter_get_data(const struct tlv_iterator *tlv_iter);
  215. extern int tlv_iter_next(struct tlv_iterator *tlv_iter);
  216. extern int tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res);
  217. extern int tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter,
  218. enum tlv_tls_supported *tls_supported);
  219. extern int tlv_iter_decode_u32(struct tlv_iterator *tlv_iter,
  220. uint32_t *res);
  221. extern int tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str,
  222. size_t *str_len);
  223. extern int tlv_iter_decode_client_cert_required(
  224. struct tlv_iterator *tlv_iter, uint8_t *client_cert_required);
  225. extern int tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter,
  226. uint16_t **u16a, size_t *no_items);
  227. extern int tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter,
  228. enum tlv_opt_type **supported_options, size_t *no_supported_options);
  229. extern int tlv_iter_decode_supported_decision_algorithms(
  230. struct tlv_iterator *tlv_iter,
  231. enum tlv_decision_algorithm_type **supported_decision_algorithms,
  232. size_t *no_supported_decision_algorithms);
  233. extern int tlv_iter_decode_u16(struct tlv_iterator *tlv_iter,
  234. uint16_t *u16);
  235. extern int tlv_iter_decode_u64(struct tlv_iterator *tlv_iter,
  236. uint64_t *u64);
  237. extern int tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter,
  238. enum tlv_reply_error_code *reply_error_code);
  239. extern int tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter,
  240. enum tlv_decision_algorithm_type *decision_algorithm);
  241. extern int tlv_iter_decode_ring_id(struct tlv_iterator *tlv_iter,
  242. struct tlv_ring_id *ring_id);
  243. extern int tlv_iter_decode_tie_breaker(struct tlv_iterator *tlv_iter,
  244. struct tlv_tie_breaker *tie_breaker);
  245. extern int tlv_iter_decode_node_state(struct tlv_iterator *tlv_iter,
  246. enum tlv_node_state *node_state);
  247. extern int tlv_iter_decode_node_info(struct tlv_iterator *tlv_iter,
  248. struct tlv_node_info *node_info);
  249. extern int tlv_iter_decode_node_list_type(struct tlv_iterator *tlv_iter,
  250. enum tlv_node_list_type *node_list_type);
  251. extern int tlv_iter_decode_vote(struct tlv_iterator *tlv_iter,
  252. enum tlv_vote *vote);
  253. extern int tlv_iter_decode_quorate(struct tlv_iterator *tlv_iter,
  254. enum tlv_quorate *quorate);
  255. extern int tlv_iter_decode_heuristics(struct tlv_iterator *tlv_iter,
  256. enum tlv_heuristics *heuristics);
  257. extern void tlv_get_supported_options(enum tlv_opt_type **supported_options,
  258. size_t *no_supported_options);
  259. extern int tlv_ring_id_eq(const struct tlv_ring_id *rid1,
  260. const struct tlv_ring_id *rid2);
  261. extern int tlv_tie_breaker_eq(const struct tlv_tie_breaker *tb1,
  262. const struct tlv_tie_breaker *tb2);
  263. extern const char *tlv_vote_to_str(enum tlv_vote vote);
  264. extern const char *tlv_node_state_to_str(enum tlv_node_state state);
  265. extern const char *tlv_tls_supported_to_str(enum tlv_tls_supported tls_supported);
  266. extern const char *tlv_decision_algorithm_type_to_str(
  267. enum tlv_decision_algorithm_type algorithm);
  268. extern const char *tlv_heuristics_to_str(enum tlv_heuristics heuristics);
  269. /*
  270. * Compare h1 and h2. Return -1 if h1 < h2, 0 if h1 == h2 and 1 if h1 > h2
  271. */
  272. extern int tlv_heuristics_cmp(enum tlv_heuristics h1, enum tlv_heuristics h2);
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276. #endif /* _TLV_H_ */