corosync  2.4.5
exec/cmap.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2012 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 
35 #include <config.h>
36 
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <poll.h>
43 #include <assert.h>
44 
45 #include <qb/qbloop.h>
46 #include <qb/qbipc_common.h>
47 
48 #include <corosync/corotypes.h>
49 #include <corosync/corodefs.h>
50 #include <corosync/list.h>
51 #include <corosync/mar_gen.h>
52 #include <corosync/ipc_cmap.h>
53 #include <corosync/logsys.h>
54 #include <corosync/coroapi.h>
55 #include <corosync/icmap.h>
56 
57 #include "service.h"
58 
59 LOGSYS_DECLARE_SUBSYS ("CMAP");
60 
61 #define MAX_REQ_EXEC_CMAP_MCAST_ITEMS 32
62 #define ICMAP_VALUETYPE_NOT_EXIST 0
63 
67 };
68 
69 typedef uint64_t cmap_iter_handle_t;
70 typedef uint64_t cmap_track_handle_t;
71 
73  void *conn;
76 };
77 
80 };
81 
85 };
86 
87 static struct corosync_api_v1 *api;
88 
89 static char *cmap_exec_init_fn (struct corosync_api_v1 *corosync_api);
90 static int cmap_exec_exit_fn(void);
91 
92 static int cmap_lib_init_fn (void *conn);
93 static int cmap_lib_exit_fn (void *conn);
94 
95 static void message_handler_req_lib_cmap_set(void *conn, const void *message);
96 static void message_handler_req_lib_cmap_delete(void *conn, const void *message);
97 static void message_handler_req_lib_cmap_get(void *conn, const void *message);
98 static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message);
99 static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message);
100 static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message);
101 static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message);
102 static void message_handler_req_lib_cmap_track_add(void *conn, const void *message);
103 static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message);
104 
105 static void cmap_notify_fn(int32_t event,
106  const char *key_name,
107  struct icmap_notify_value new_val,
108  struct icmap_notify_value old_val,
109  void *user_data);
110 
111 static void message_handler_req_exec_cmap_mcast(
112  const void *message,
113  unsigned int nodeid);
114 
115 static void exec_cmap_mcast_endian_convert(void *message);
116 
117 /*
118  * Reson is subtype of message. argc is number of items in argv array. Argv is array
119  * of strings (key names) which will be send to wire. There can be maximum
120  * MAX_REQ_EXEC_CMAP_MCAST_ITEMS items (for more items, CS_ERR_TOO_MANY_GROUPS
121  * error is returned). If key is not found, item has type ICMAP_VALUETYPE_NOT_EXIST
122  * and length zero.
123  */
124 static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[]);
125 
126 static void cmap_sync_init (
127  const unsigned int *trans_list,
128  size_t trans_list_entries,
129  const unsigned int *member_list,
130  size_t member_list_entries,
131  const struct memb_ring_id *ring_id);
132 
133 static int cmap_sync_process (void);
134 static void cmap_sync_activate (void);
135 static void cmap_sync_abort (void);
136 
137 static void cmap_config_version_track_cb(
138  int32_t event,
139  const char *key_name,
140  struct icmap_notify_value new_value,
141  struct icmap_notify_value old_value,
142  void *user_data);
143 
144 /*
145  * Library Handler Definition
146  */
147 static struct corosync_lib_handler cmap_lib_engine[] =
148 {
149  { /* 0 */
150  .lib_handler_fn = message_handler_req_lib_cmap_set,
151  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
152  },
153  { /* 1 */
154  .lib_handler_fn = message_handler_req_lib_cmap_delete,
155  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
156  },
157  { /* 2 */
158  .lib_handler_fn = message_handler_req_lib_cmap_get,
159  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
160  },
161  { /* 3 */
162  .lib_handler_fn = message_handler_req_lib_cmap_adjust_int,
163  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
164  },
165  { /* 4 */
166  .lib_handler_fn = message_handler_req_lib_cmap_iter_init,
167  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
168  },
169  { /* 5 */
170  .lib_handler_fn = message_handler_req_lib_cmap_iter_next,
171  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
172  },
173  { /* 6 */
174  .lib_handler_fn = message_handler_req_lib_cmap_iter_finalize,
175  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
176  },
177  { /* 7 */
178  .lib_handler_fn = message_handler_req_lib_cmap_track_add,
179  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
180  },
181  { /* 8 */
182  .lib_handler_fn = message_handler_req_lib_cmap_track_delete,
183  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
184  },
185 };
186 
187 static struct corosync_exec_handler cmap_exec_engine[] =
188 {
189  { /* 0 - MESSAGE_REQ_EXEC_CMAP_MCAST */
190  .exec_handler_fn = message_handler_req_exec_cmap_mcast,
191  .exec_endian_convert_fn = exec_cmap_mcast_endian_convert
192  },
193 };
194 
196  .name = "corosync configuration map access",
197  .id = CMAP_SERVICE,
198  .priority = 1,
199  .private_data_size = sizeof(struct cmap_conn_info),
200  .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
201  .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
202  .lib_init_fn = cmap_lib_init_fn,
203  .lib_exit_fn = cmap_lib_exit_fn,
204  .lib_engine = cmap_lib_engine,
205  .lib_engine_count = sizeof (cmap_lib_engine) / sizeof (struct corosync_lib_handler),
206  .exec_init_fn = cmap_exec_init_fn,
207  .exec_exit_fn = cmap_exec_exit_fn,
208  .exec_engine = cmap_exec_engine,
209  .exec_engine_count = sizeof (cmap_exec_engine) / sizeof (struct corosync_exec_handler),
210  .sync_init = cmap_sync_init,
211  .sync_process = cmap_sync_process,
212  .sync_activate = cmap_sync_activate,
213  .sync_abort = cmap_sync_abort
214 };
215 
217 {
218  return (&cmap_service_engine);
219 }
220 
222  mar_name_t key_name __attribute__((aligned(8)));
223  mar_uint8_t value_type __attribute__((aligned(8)));
224  mar_size_t value_len __attribute__((aligned(8)));
225  uint8_t value[] __attribute__((aligned(8)));
226 };
227 
229  struct qb_ipc_request_header header __attribute__((aligned(8)));
230  mar_uint8_t reason __attribute__((aligned(8)));
231  mar_uint8_t no_items __attribute__((aligned(8)));
232  mar_uint8_t reserved1 __attribute__((aligned(8)));
233  mar_uint8_t reserver2 __attribute__((aligned(8)));
234  /*
235  * Following are array of req_exec_cmap_mcast_item alligned to 8 bytes
236  */
237 };
238 
239 static size_t cmap_sync_trans_list_entries = 0;
240 static size_t cmap_sync_member_list_entries = 0;
241 static uint64_t cmap_highest_config_version_received = 0;
242 static uint64_t cmap_my_config_version = 0;
243 static int cmap_first_sync = 1;
244 static icmap_track_t cmap_config_version_track;
245 
246 static void cmap_config_version_track_cb(
247  int32_t event,
248  const char *key_name,
249  struct icmap_notify_value new_value,
250  struct icmap_notify_value old_value,
251  void *user_data)
252 {
253  const char *key = "totem.config_version";
254  cs_error_t ret;
255 
256  ENTER();
257 
258  if (icmap_get_uint64("totem.config_version", &cmap_my_config_version) != CS_OK) {
259  cmap_my_config_version = 0;
260  }
261 
262 
263  ret = cmap_mcast_send(CMAP_MCAST_REASON_NEW_CONFIG_VERSION, 1, (char **)&key);
264  if (ret != CS_OK) {
265  log_printf(LOGSYS_LEVEL_ERROR, "Can't inform other nodes about new config version");
266  }
267 
268  LEAVE();
269 }
270 
271 static int cmap_exec_exit_fn(void)
272 {
273 
274  if (icmap_track_delete(cmap_config_version_track) != CS_OK) {
275  log_printf(LOGSYS_LEVEL_ERROR, "Can't delete config_version icmap tracker");
276  }
277 
278  return 0;
279 }
280 
281 static char *cmap_exec_init_fn (
282  struct corosync_api_v1 *corosync_api)
283 {
284  cs_error_t ret;
285 
286  api = corosync_api;
287 
288  ret = icmap_track_add("totem.config_version",
290  cmap_config_version_track_cb,
291  NULL,
292  &cmap_config_version_track);
293 
294  if (ret != CS_OK) {
295  return ((char *)"Can't add config_version icmap tracker");
296  }
297 
298  return (NULL);
299 }
300 
301 static int cmap_lib_init_fn (void *conn)
302 {
303  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
304 
305  log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p", conn);
306 
307  api->ipc_refcnt_inc(conn);
308 
309  memset(conn_info, 0, sizeof(*conn_info));
310  hdb_create(&conn_info->iter_db);
311  hdb_create(&conn_info->track_db);
312 
313  return (0);
314 }
315 
316 static int cmap_lib_exit_fn (void *conn)
317 {
318  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
319  hdb_handle_t iter_handle = 0;
320  icmap_iter_t *iter;
321  hdb_handle_t track_handle = 0;
322  icmap_track_t *track;
323 
324  log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p", conn);
325 
326  hdb_iterator_reset(&conn_info->iter_db);
327  while (hdb_iterator_next(&conn_info->iter_db,
328  (void*)&iter, &iter_handle) == 0) {
329 
330  icmap_iter_finalize(*iter);
331 
332  (void)hdb_handle_put (&conn_info->iter_db, iter_handle);
333  }
334 
335  hdb_destroy(&conn_info->iter_db);
336 
337  hdb_iterator_reset(&conn_info->track_db);
338  while (hdb_iterator_next(&conn_info->track_db,
339  (void*)&track, &track_handle) == 0) {
340 
341  free(icmap_track_get_user_data(*track));
342 
343  icmap_track_delete(*track);
344 
345  (void)hdb_handle_put (&conn_info->track_db, track_handle);
346  }
347  hdb_destroy(&conn_info->track_db);
348 
349  api->ipc_refcnt_dec(conn);
350 
351  return (0);
352 }
353 
354 static void cmap_sync_init (
355  const unsigned int *trans_list,
356  size_t trans_list_entries,
357  const unsigned int *member_list,
358  size_t member_list_entries,
359  const struct memb_ring_id *ring_id)
360 {
361 
362  cmap_sync_trans_list_entries = trans_list_entries;
363  cmap_sync_member_list_entries = member_list_entries;
364 
365  if (icmap_get_uint64("totem.config_version", &cmap_my_config_version) != CS_OK) {
366  cmap_my_config_version = 0;
367  }
368 
369  cmap_highest_config_version_received = cmap_my_config_version;
370 }
371 
372 static int cmap_sync_process (void)
373 {
374  const char *key = "totem.config_version";
375  cs_error_t ret;
376 
377  ret = cmap_mcast_send(CMAP_MCAST_REASON_SYNC, 1, (char **)&key);
378 
379  return (ret == CS_OK ? 0 : -1);
380 }
381 
382 static void cmap_sync_activate (void)
383 {
384 
385  if (cmap_sync_trans_list_entries == 0) {
386  log_printf(LOGSYS_LEVEL_DEBUG, "Single node sync -> no action");
387 
388  return ;
389  }
390 
391  if (cmap_first_sync == 1) {
392  cmap_first_sync = 0;
393  } else {
394  log_printf(LOGSYS_LEVEL_DEBUG, "Not first sync -> no action");
395 
396  return ;
397  }
398 
399  if (cmap_my_config_version == 0) {
400  log_printf(LOGSYS_LEVEL_DEBUG, "My config version is 0 -> no action");
401 
402  return ;
403  }
404 
405  if (cmap_highest_config_version_received != cmap_my_config_version) {
407  "Received config version (%"PRIu64") is different than my config version (%"PRIu64")! Exiting",
408  cmap_highest_config_version_received, cmap_my_config_version);
409  api->shutdown_request();
410  return ;
411  }
412 }
413 
414 static void cmap_sync_abort (void)
415 {
416 
417 
418 }
419 
420 static void message_handler_req_lib_cmap_set(void *conn, const void *message)
421 {
422  const struct req_lib_cmap_set *req_lib_cmap_set = message;
424  cs_error_t ret;
425 
426  if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
427  ret = CS_ERR_ACCESS;
428  } else {
429  ret = icmap_set((char *)req_lib_cmap_set->key_name.value, &req_lib_cmap_set->value,
430  req_lib_cmap_set->value_len, req_lib_cmap_set->type);
431  }
432 
433  memset(&res_lib_cmap_set, 0, sizeof(res_lib_cmap_set));
434  res_lib_cmap_set.header.size = sizeof(res_lib_cmap_set);
436  res_lib_cmap_set.header.error = ret;
437 
439 }
440 
441 static void message_handler_req_lib_cmap_delete(void *conn, const void *message)
442 {
443  const struct req_lib_cmap_set *req_lib_cmap_set = message;
445  cs_error_t ret;
446 
447  if (icmap_is_key_ro((char *)req_lib_cmap_set->key_name.value)) {
448  ret = CS_ERR_ACCESS;
449  } else {
450  ret = icmap_delete((char *)req_lib_cmap_set->key_name.value);
451  }
452 
453  memset(&res_lib_cmap_delete, 0, sizeof(res_lib_cmap_delete));
454  res_lib_cmap_delete.header.size = sizeof(res_lib_cmap_delete);
456  res_lib_cmap_delete.header.error = ret;
457 
459 }
460 
461 static void message_handler_req_lib_cmap_get(void *conn, const void *message)
462 {
463  const struct req_lib_cmap_get *req_lib_cmap_get = message;
465  struct res_lib_cmap_get error_res_lib_cmap_get;
466  cs_error_t ret;
467  size_t value_len;
468  size_t res_lib_cmap_get_size;
470  void *value;
471 
472  value_len = req_lib_cmap_get->value_len;
473 
474  res_lib_cmap_get_size = sizeof(*res_lib_cmap_get) + value_len;
475  res_lib_cmap_get = malloc(res_lib_cmap_get_size);
476  if (res_lib_cmap_get == NULL) {
477  ret = CS_ERR_NO_MEMORY;
478  goto error_exit;
479  }
480 
481  memset(res_lib_cmap_get, 0, res_lib_cmap_get_size);
482 
483  if (value_len > 0) {
484  value = res_lib_cmap_get->value;
485  } else {
486  value = NULL;
487  }
488 
489  ret = icmap_get((char *)req_lib_cmap_get->key_name.value,
490  value,
491  &value_len,
492  &type);
493 
494  if (ret != CS_OK) {
495  free(res_lib_cmap_get);
496  goto error_exit;
497  }
498 
499  res_lib_cmap_get->header.size = res_lib_cmap_get_size;
500  res_lib_cmap_get->header.id = MESSAGE_RES_CMAP_GET;
501  res_lib_cmap_get->header.error = ret;
502  res_lib_cmap_get->type = type;
503  res_lib_cmap_get->value_len = value_len;
504 
505  api->ipc_response_send(conn, res_lib_cmap_get, res_lib_cmap_get_size);
506  free(res_lib_cmap_get);
507 
508  return ;
509 
510 error_exit:
511  memset(&error_res_lib_cmap_get, 0, sizeof(error_res_lib_cmap_get));
512  error_res_lib_cmap_get.header.size = sizeof(error_res_lib_cmap_get);
513  error_res_lib_cmap_get.header.id = MESSAGE_RES_CMAP_GET;
514  error_res_lib_cmap_get.header.error = ret;
515 
516  api->ipc_response_send(conn, &error_res_lib_cmap_get, sizeof(error_res_lib_cmap_get));
517 }
518 
519 static void message_handler_req_lib_cmap_adjust_int(void *conn, const void *message)
520 {
521  const struct req_lib_cmap_adjust_int *req_lib_cmap_adjust_int = message;
523  cs_error_t ret;
524 
525  if (icmap_is_key_ro((char *)req_lib_cmap_adjust_int->key_name.value)) {
526  ret = CS_ERR_ACCESS;
527  } else {
528  ret = icmap_adjust_int((char *)req_lib_cmap_adjust_int->key_name.value,
529  req_lib_cmap_adjust_int->step);
530  }
531 
533  res_lib_cmap_adjust_int.header.size = sizeof(res_lib_cmap_adjust_int);
535  res_lib_cmap_adjust_int.header.error = ret;
536 
538 }
539 
540 static void message_handler_req_lib_cmap_iter_init(void *conn, const void *message)
541 {
542  const struct req_lib_cmap_iter_init *req_lib_cmap_iter_init = message;
544  cs_error_t ret;
545  icmap_iter_t iter;
546  icmap_iter_t *hdb_iter;
547  cmap_iter_handle_t handle = 0ULL;
548  const char *prefix;
549  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
550 
551  if (req_lib_cmap_iter_init->prefix.length > 0) {
552  prefix = (char *)req_lib_cmap_iter_init->prefix.value;
553  } else {
554  prefix = NULL;
555  }
556 
557  iter = icmap_iter_init(prefix);
558  if (iter == NULL) {
559  ret = CS_ERR_NO_SECTIONS;
560  goto reply_send;
561  }
562 
563  ret = hdb_error_to_cs(hdb_handle_create(&conn_info->iter_db, sizeof(iter), &handle));
564  if (ret != CS_OK) {
565  goto reply_send;
566  }
567 
568  ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db, handle, (void *)&hdb_iter));
569  if (ret != CS_OK) {
570  goto reply_send;
571  }
572 
573  *hdb_iter = iter;
574 
575  (void)hdb_handle_put (&conn_info->iter_db, handle);
576 
577 reply_send:
578  memset(&res_lib_cmap_iter_init, 0, sizeof(res_lib_cmap_iter_init));
579  res_lib_cmap_iter_init.header.size = sizeof(res_lib_cmap_iter_init);
581  res_lib_cmap_iter_init.header.error = ret;
582  res_lib_cmap_iter_init.iter_handle = handle;
583 
585 }
586 
587 static void message_handler_req_lib_cmap_iter_next(void *conn, const void *message)
588 {
589  const struct req_lib_cmap_iter_next *req_lib_cmap_iter_next = message;
591  cs_error_t ret;
592  icmap_iter_t *iter;
593  size_t value_len = 0;
594  icmap_value_types_t type = 0;
595  const char *res = NULL;
596  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
597 
598  ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
599  req_lib_cmap_iter_next->iter_handle, (void *)&iter));
600  if (ret != CS_OK) {
601  goto reply_send;
602  }
603 
604  res = icmap_iter_next(*iter, &value_len, &type);
605  if (res == NULL) {
606  ret = CS_ERR_NO_SECTIONS;
607  }
608 
609  (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_next->iter_handle);
610 
611 reply_send:
612  memset(&res_lib_cmap_iter_next, 0, sizeof(res_lib_cmap_iter_next));
613  res_lib_cmap_iter_next.header.size = sizeof(res_lib_cmap_iter_next);
615  res_lib_cmap_iter_next.header.error = ret;
616 
617  if (res != NULL) {
618  res_lib_cmap_iter_next.value_len = value_len;
620 
621  memcpy(res_lib_cmap_iter_next.key_name.value, res, strlen(res));
622  res_lib_cmap_iter_next.key_name.length = strlen(res);
623  }
624 
626 }
627 
628 static void message_handler_req_lib_cmap_iter_finalize(void *conn, const void *message)
629 {
632  cs_error_t ret;
633  icmap_iter_t *iter;
634  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
635 
636  ret = hdb_error_to_cs(hdb_handle_get(&conn_info->iter_db,
637  req_lib_cmap_iter_finalize->iter_handle, (void *)&iter));
638  if (ret != CS_OK) {
639  goto reply_send;
640  }
641 
642  icmap_iter_finalize(*iter);
643 
644  (void)hdb_handle_destroy(&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
645 
646  (void)hdb_handle_put (&conn_info->iter_db, req_lib_cmap_iter_finalize->iter_handle);
647 
648 reply_send:
652  res_lib_cmap_iter_finalize.header.error = ret;
653 
655 }
656 
657 static void cmap_notify_fn(int32_t event,
658  const char *key_name,
659  struct icmap_notify_value new_val,
660  struct icmap_notify_value old_val,
661  void *user_data)
662 {
663  struct cmap_track_user_data *cmap_track_user_data = (struct cmap_track_user_data *)user_data;
665  struct iovec iov[3];
666 
668 
669  res_lib_cmap_notify_callback.header.size = sizeof(res_lib_cmap_notify_callback) + new_val.len + old_val.len;
671  res_lib_cmap_notify_callback.header.error = CS_OK;
672 
673  res_lib_cmap_notify_callback.new_value_type = new_val.type;
674  res_lib_cmap_notify_callback.old_value_type = old_val.type;
675  res_lib_cmap_notify_callback.new_value_len = new_val.len;
676  res_lib_cmap_notify_callback.old_value_len = old_val.len;
677  res_lib_cmap_notify_callback.event = event;
678  res_lib_cmap_notify_callback.key_name.length = strlen(key_name);
679  res_lib_cmap_notify_callback.track_inst_handle = cmap_track_user_data->track_inst_handle;
680 
681  memcpy(res_lib_cmap_notify_callback.key_name.value, key_name, strlen(key_name));
682 
683  iov[0].iov_base = (char *)&res_lib_cmap_notify_callback;
684  iov[0].iov_len = sizeof(res_lib_cmap_notify_callback);
685  iov[1].iov_base = (char *)new_val.data;
686  iov[1].iov_len = new_val.len;
687  iov[2].iov_base = (char *)old_val.data;
688  iov[2].iov_len = old_val.len;
689 
690  api->ipc_dispatch_iov_send(cmap_track_user_data->conn, iov, 3);
691 }
692 
693 static void message_handler_req_lib_cmap_track_add(void *conn, const void *message)
694 {
695  const struct req_lib_cmap_track_add *req_lib_cmap_track_add = message;
697  cs_error_t ret;
698  cmap_track_handle_t handle = 0;
699  icmap_track_t track = NULL;
700  icmap_track_t *hdb_track;
701  struct cmap_track_user_data *cmap_track_user_data;
702  const char *key_name;
703 
704  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
705 
706  cmap_track_user_data = malloc(sizeof(*cmap_track_user_data));
707  if (cmap_track_user_data == NULL) {
708  ret = CS_ERR_NO_MEMORY;
709 
710  goto reply_send;
711  }
712  memset(cmap_track_user_data, 0, sizeof(*cmap_track_user_data));
713 
714  if (req_lib_cmap_track_add->key_name.length > 0) {
715  key_name = (char *)req_lib_cmap_track_add->key_name.value;
716  } else {
717  key_name = NULL;
718  }
719 
720  ret = icmap_track_add(key_name,
721  req_lib_cmap_track_add->track_type,
722  cmap_notify_fn,
723  cmap_track_user_data,
724  &track);
725  if (ret != CS_OK) {
726  free(cmap_track_user_data);
727 
728  goto reply_send;
729  }
730 
731  ret = hdb_error_to_cs(hdb_handle_create(&conn_info->track_db, sizeof(track), &handle));
732  if (ret != CS_OK) {
733  free(cmap_track_user_data);
734 
735  goto reply_send;
736  }
737 
738  ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db, handle, (void *)&hdb_track));
739  if (ret != CS_OK) {
740  free(cmap_track_user_data);
741 
742  goto reply_send;
743  }
744 
745  *hdb_track = track;
746  cmap_track_user_data->conn = conn;
747  cmap_track_user_data->track_handle = handle;
748  cmap_track_user_data->track_inst_handle = req_lib_cmap_track_add->track_inst_handle;
749 
750  (void)hdb_handle_put (&conn_info->track_db, handle);
751 
752 reply_send:
753  memset(&res_lib_cmap_track_add, 0, sizeof(res_lib_cmap_track_add));
754  res_lib_cmap_track_add.header.size = sizeof(res_lib_cmap_track_add);
756  res_lib_cmap_track_add.header.error = ret;
757  res_lib_cmap_track_add.track_handle = handle;
758 
760 }
761 
762 static void message_handler_req_lib_cmap_track_delete(void *conn, const void *message)
763 {
766  cs_error_t ret;
767  icmap_track_t *track;
768  struct cmap_conn_info *conn_info = (struct cmap_conn_info *)api->ipc_private_data_get (conn);
769  uint64_t track_inst_handle = 0;
770 
771  ret = hdb_error_to_cs(hdb_handle_get(&conn_info->track_db,
772  req_lib_cmap_track_delete->track_handle, (void *)&track));
773  if (ret != CS_OK) {
774  goto reply_send;
775  }
776 
777  track_inst_handle = ((struct cmap_track_user_data *)icmap_track_get_user_data(*track))->track_inst_handle;
778 
779  free(icmap_track_get_user_data(*track));
780 
781  ret = icmap_track_delete(*track);
782 
783  (void)hdb_handle_put (&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
784  (void)hdb_handle_destroy(&conn_info->track_db, req_lib_cmap_track_delete->track_handle);
785 
786 reply_send:
790  res_lib_cmap_track_delete.header.error = ret;
791  res_lib_cmap_track_delete.track_inst_handle = track_inst_handle;
792 
794 }
795 
796 static cs_error_t cmap_mcast_send(enum cmap_mcast_reason reason, int argc, char *argv[])
797 {
798  int i;
799  size_t value_len;
800  icmap_value_types_t value_type;
801  cs_error_t err;
802  size_t item_len;
803  size_t msg_len = 0;
805  struct req_exec_cmap_mcast_item *item = NULL;
806  struct iovec req_exec_cmap_iovec[MAX_REQ_EXEC_CMAP_MCAST_ITEMS + 1];
807 
808  ENTER();
809 
810  if (argc > MAX_REQ_EXEC_CMAP_MCAST_ITEMS) {
811  return (CS_ERR_TOO_MANY_GROUPS);
812  }
813 
814  memset(req_exec_cmap_iovec, 0, sizeof(req_exec_cmap_iovec));
815 
816  for (i = 0; i < argc; i++) {
817  err = icmap_get(argv[i], NULL, &value_len, &value_type);
818  if (err != CS_OK && err != CS_ERR_NOT_EXIST) {
819  goto free_mem;
820  }
821  if (err == CS_ERR_NOT_EXIST) {
822  value_type = ICMAP_VALUETYPE_NOT_EXIST;
823  value_len = 0;
824  }
825 
826  item_len = MAR_ALIGN_UP(sizeof(*item) + value_len, 8);
827 
828  item = malloc(item_len);
829  if (item == NULL) {
830  goto free_mem;
831  }
832  memset(item, 0, item_len);
833 
834  item->value_type = value_type;
835  item->value_len = value_len;
836  item->key_name.length = strlen(argv[i]);
837  strcpy((char *)item->key_name.value, argv[i]);
838 
839  if (value_type != ICMAP_VALUETYPE_NOT_EXIST) {
840  err = icmap_get(argv[i], item->value, &value_len, &value_type);
841  if (err != CS_OK) {
842  goto free_mem;
843  }
844  }
845 
846  req_exec_cmap_iovec[i + 1].iov_base = item;
847  req_exec_cmap_iovec[i + 1].iov_len = item_len;
848  msg_len += item_len;
849 
850  qb_log(LOG_TRACE, "Item %u - type %u, len %zu", i, item->value_type, item->value_len);
851 
852  item = NULL;
853  }
854 
855  memset(&req_exec_cmap_mcast, 0, sizeof(req_exec_cmap_mcast));
856  req_exec_cmap_mcast.header.size = sizeof(req_exec_cmap_mcast) + msg_len;
857  req_exec_cmap_mcast.reason = reason;
858  req_exec_cmap_mcast.no_items = argc;
859  req_exec_cmap_iovec[0].iov_base = &req_exec_cmap_mcast;
860  req_exec_cmap_iovec[0].iov_len = sizeof(req_exec_cmap_mcast);
861 
862  qb_log(LOG_TRACE, "Sending %u items (%u iovec) for reason %u", argc, argc + 1, reason);
863  err = (api->totem_mcast(req_exec_cmap_iovec, argc + 1, TOTEM_AGREED) == 0 ? CS_OK : CS_ERR_MESSAGE_ERROR);
864 
865 free_mem:
866  for (i = 0; i < argc; i++) {
867  free(req_exec_cmap_iovec[i + 1].iov_base);
868  }
869 
870  free(item);
871 
872  LEAVE();
873  return (err);
874 }
875 
876 static struct req_exec_cmap_mcast_item *cmap_mcast_item_find(
877  const void *message,
878  char *key)
879 {
880  const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
881  int i;
882  const char *p;
883  struct req_exec_cmap_mcast_item *item;
884  mar_uint16_t key_name_len;
885 
886  p = (const char *)message + sizeof(*req_exec_cmap_mcast);
887 
888  for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
889  item = (struct req_exec_cmap_mcast_item *)p;
890 
891  key_name_len = item->key_name.length;
892  if (strlen(key) == key_name_len && strcmp((char *)item->key_name.value, key) == 0) {
893  return (item);
894  }
895 
896  p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
897  }
898 
899  return (NULL);
900 }
901 
902 static void message_handler_req_exec_cmap_mcast_reason_sync_nv(
903  enum cmap_mcast_reason reason,
904  const void *message,
905  unsigned int nodeid)
906 {
907  char member_config_version[ICMAP_KEYNAME_MAXLEN];
908  uint64_t config_version = 0;
909  struct req_exec_cmap_mcast_item *item;
910  mar_size_t value_len;
911 
912  ENTER();
913 
914  item = cmap_mcast_item_find(message, (char *)"totem.config_version");
915  if (item != NULL) {
916  value_len = item->value_len;
917 
918  if (item->value_type == ICMAP_VALUETYPE_NOT_EXIST) {
919  config_version = 0;
920  }
921 
922  if (item->value_type == ICMAP_VALUETYPE_UINT64) {
923  memcpy(&config_version, item->value, value_len);
924  }
925  }
926 
927  qb_log(LOG_TRACE, "Received config version %"PRIu64" from node %x", config_version, nodeid);
928 
929  if (nodeid != api->totem_nodeid_get() &&
930  config_version > cmap_highest_config_version_received) {
931  cmap_highest_config_version_received = config_version;
932  }
933 
934  snprintf(member_config_version, ICMAP_KEYNAME_MAXLEN,
935  "runtime.totem.pg.mrp.srp.members.%u.config_version", nodeid);
936  icmap_set_uint64(member_config_version, config_version);
937 
938  LEAVE();
939 }
940 
941 static void message_handler_req_exec_cmap_mcast(
942  const void *message,
943  unsigned int nodeid)
944 {
945  const struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
946 
947  ENTER();
948 
949  switch (req_exec_cmap_mcast->reason) {
951  message_handler_req_exec_cmap_mcast_reason_sync_nv(req_exec_cmap_mcast->reason,
952  message, nodeid);
953 
954  break;
956  message_handler_req_exec_cmap_mcast_reason_sync_nv(req_exec_cmap_mcast->reason,
957  message, nodeid);
958 
959  break;
960  default:
961  qb_log(LOG_TRACE, "Received mcast with unknown reason %u", req_exec_cmap_mcast->reason);
962  };
963 
964  LEAVE();
965 }
966 
967 static void exec_cmap_mcast_endian_convert(void *message)
968 {
969  struct req_exec_cmap_mcast *req_exec_cmap_mcast = message;
970  const char *p;
971  int i;
972  struct req_exec_cmap_mcast_item *item;
973  uint16_t u16;
974  uint32_t u32;
975  uint64_t u64;
976  float flt;
977  double dbl;
978 
979  swab_coroipc_request_header_t(&req_exec_cmap_mcast->header);
980 
981  p = (const char *)message + sizeof(*req_exec_cmap_mcast);
982 
983  for (i = 0; i < req_exec_cmap_mcast->no_items; i++) {
984  item = (struct req_exec_cmap_mcast_item *)p;
985 
986  swab_mar_uint16_t(&item->key_name.length);
987  swab_mar_size_t(&item->value_len);
988 
989  switch (item->value_type) {
992  memcpy(&u16, item->value, sizeof(u16));
993  u16 = swab16(u16);
994  memcpy(item->value, &u16, sizeof(u16));
995  break;
998  memcpy(&u32, item->value, sizeof(u32));
999  u32 = swab32(u32);
1000  memcpy(item->value, &u32, sizeof(u32));
1001  break;
1002  case ICMAP_VALUETYPE_INT64:
1004  memcpy(&u64, item->value, sizeof(u64));
1005  u64 = swab64(u64);
1006  memcpy(item->value, &u64, sizeof(u64));
1007  break;
1008  case ICMAP_VALUETYPE_FLOAT:
1009  memcpy(&flt, item->value, sizeof(flt));
1010  swabflt(&flt);
1011  memcpy(item->value, &flt, sizeof(flt));
1012  break;
1014  memcpy(&dbl, item->value, sizeof(dbl));
1015  swabdbl(&dbl);
1016  memcpy(item->value, &dbl, sizeof(dbl));
1017  break;
1018  }
1019 
1020  p += MAR_ALIGN_UP(sizeof(*item) + item->value_len, 8);
1021  }
1022 }