qdevice-net-cmap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2015 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. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <stdio.h>
  37. #include <netdb.h>
  38. #include "qdevice-net-cmap.h"
  39. static uint32_t
  40. qdevice_net_cmap_autogenerate_node_id(const char *addr, int clear_node_high_byte)
  41. {
  42. struct addrinfo *ainfo;
  43. struct addrinfo ahints;
  44. int ret, i;
  45. memset(&ahints, 0, sizeof(ahints));
  46. ahints.ai_socktype = SOCK_DGRAM;
  47. ahints.ai_protocol = IPPROTO_UDP;
  48. /*
  49. * Hardcoded AF_INET because autogenerated nodeid is valid only for ipv4
  50. */
  51. ahints.ai_family = AF_INET;
  52. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  53. if (ret != 0)
  54. return (0);
  55. if (ainfo->ai_family != AF_INET) {
  56. freeaddrinfo(ainfo);
  57. return (0);
  58. }
  59. memcpy(&i, &((struct sockaddr_in *)ainfo->ai_addr)->sin_addr, sizeof(struct in_addr));
  60. freeaddrinfo(ainfo);
  61. ret = htonl(i);
  62. if (clear_node_high_byte) {
  63. ret &= 0x7FFFFFFF;
  64. }
  65. return (ret);
  66. }
  67. int
  68. qdevice_net_cmap_get_nodelist(cmap_handle_t cmap_handle, struct node_list *list)
  69. {
  70. cs_error_t cs_err;
  71. cmap_iter_handle_t iter_handle;
  72. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  73. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  74. int res;
  75. int ret_value;
  76. unsigned int node_pos;
  77. uint32_t node_id;
  78. uint32_t data_center_id;
  79. char *tmp_str;
  80. char *addr0_str;
  81. int clear_node_high_byte;
  82. ret_value = 0;
  83. node_list_init(list);
  84. cs_err = cmap_iter_init(cmap_handle, "nodelist.node.", &iter_handle);
  85. if (cs_err != CS_OK) {
  86. return (-1);
  87. }
  88. while ((cs_err = cmap_iter_next(cmap_handle, iter_handle, key_name, NULL, NULL)) == CS_OK) {
  89. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  90. if (res != 2) {
  91. continue;
  92. }
  93. if (strcmp(tmp_key, "ring0_addr") != 0) {
  94. continue;
  95. }
  96. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  97. cs_err = cmap_get_uint32(cmap_handle, tmp_key, &node_id);
  98. if (cs_err == CS_ERR_NOT_EXIST) {
  99. /*
  100. * Nodeid doesn't exists -> autogenerate node id
  101. */
  102. clear_node_high_byte = 0;
  103. if (cmap_get_string(cmap_handle, "totem.clear_node_high_bit",
  104. &tmp_str) == CS_OK) {
  105. if (strcmp (tmp_str, "yes") == 0) {
  106. clear_node_high_byte = 1;
  107. }
  108. free(tmp_str);
  109. }
  110. if (cmap_get_string(cmap_handle, key_name, &addr0_str) != CS_OK) {
  111. return (-1);
  112. }
  113. node_id = qdevice_net_cmap_autogenerate_node_id(addr0_str, clear_node_high_byte);
  114. free(addr0_str);
  115. } else if (cs_err != CS_OK) {
  116. ret_value = -1;
  117. goto iter_finalize;
  118. }
  119. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.datacenterid", node_pos);
  120. if (cmap_get_uint32(cmap_handle, tmp_key, &data_center_id) != CS_OK) {
  121. data_center_id = 0;
  122. }
  123. if (node_list_add(list, node_id, data_center_id, TLV_NODE_STATE_NOT_SET) == NULL) {
  124. ret_value = -1;
  125. goto iter_finalize;
  126. }
  127. }
  128. iter_finalize:
  129. cmap_iter_finalize(cmap_handle, iter_handle);
  130. if (ret_value != 0) {
  131. node_list_free(list);
  132. }
  133. return (ret_value);
  134. }
  135. int
  136. qdevice_net_cmap_get_config_version(cmap_handle_t cmap_handle, uint64_t *config_version)
  137. {
  138. int res;
  139. if (cmap_get_uint64(cmap_handle, "totem.config_version", config_version) == CS_OK) {
  140. res = 1;
  141. } else {
  142. *config_version = 0;
  143. res = 0;
  144. }
  145. return (res);
  146. }