qdevice-net-cmap.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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,
  117. clear_node_high_byte);
  118. free(addr0_str);
  119. } else if (cs_err != CS_OK) {
  120. ret_value = -1;
  121. goto iter_finalize;
  122. }
  123. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.datacenterid", node_pos);
  124. if (cmap_get_uint32(cmap_handle, tmp_key, &data_center_id) != CS_OK) {
  125. data_center_id = 0;
  126. }
  127. if (node_list_add(list, node_id, data_center_id, TLV_NODE_STATE_NOT_SET) == NULL) {
  128. ret_value = -1;
  129. goto iter_finalize;
  130. }
  131. }
  132. iter_finalize:
  133. cmap_iter_finalize(cmap_handle, iter_handle);
  134. if (ret_value != 0) {
  135. node_list_free(list);
  136. }
  137. return (ret_value);
  138. }
  139. int
  140. qdevice_net_cmap_get_config_version(cmap_handle_t cmap_handle, uint64_t *config_version)
  141. {
  142. int res;
  143. if (cmap_get_uint64(cmap_handle, "totem.config_version", config_version) == CS_OK) {
  144. res = 1;
  145. } else {
  146. *config_version = 0;
  147. res = 0;
  148. }
  149. return (res);
  150. }
  151. void
  152. qdevice_net_cmap_init(cmap_handle_t *handle)
  153. {
  154. cs_error_t res;
  155. int no_retries;
  156. no_retries = 0;
  157. while ((res = cmap_initialize(handle)) == CS_ERR_TRY_AGAIN &&
  158. no_retries++ < QDEVICE_NET_MAX_CS_TRY_AGAIN) {
  159. poll(NULL, 0, 1000);
  160. }
  161. if (res != CS_OK) {
  162. errx(1, "Failed to initialize the cmap API. Error %s", cs_strerror(res));
  163. }
  164. }