qdevice-net-cmap.c 5.1 KB

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