| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- /*
- * vi: set autoindent tabstop=4 shiftwidth=4 :
- * Copyright (c) 2004-2005 MontaVista Software, Inc.
- * Copyright (c) 2006-2007, 2009 Red Hat, Inc.
- *
- * All rights reserved.
- *
- * Author: Steven Dake (sdake@redhat.com)
- *
- * This software licensed under BSD license, the text of which follows:
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * - Neither the name of the MontaVista Software, Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- /*
- * Provides an extended virtual synchrony API using the corosync executive
- */
- #include <config.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <errno.h>
- #include <corosync/corotypes.h>
- #include <corosync/coroipc_types.h>
- #include <corosync/coroipcc.h>
- #include <corosync/corodefs.h>
- #include <corosync/hdb.h>
- #include <corosync/evs.h>
- #include <corosync/ipc_evs.h>
- #include "util.h"
- #undef MIN
- #define MIN(x,y) ((x) < (y) ? (x) : (y))
- struct evs_inst {
- void *ipc_ctx;
- int finalize;
- evs_callbacks_t callbacks;
- pthread_mutex_t response_mutex;
- pthread_mutex_t dispatch_mutex;
- };
- static void evs_instance_destructor (void *instance);
- DECLARE_HDB_DATABASE (evs_handle_t_db, evs_instance_destructor);
- /*
- * Clean up function for an evt instance (saEvtInitialize) handle
- */
- static void evs_instance_destructor (void *instance)
- {
- struct evs_inst *evs_inst = instance;
- pthread_mutex_destroy (&evs_inst->response_mutex);
- pthread_mutex_destroy (&evs_inst->dispatch_mutex);
- }
- /**
- * @defgroup evs_coroipcc The extended virtual synchrony passthrough API
- * @ingroup coroipcc
- *
- * @{
- */
- /**
- * test
- * @param handle The handle of evs initialize
- * @param callbacks The callbacks for evs_initialize
- * @returns EVS_OK
- */
- evs_error_t evs_initialize (
- evs_handle_t *handle,
- evs_callbacks_t *callbacks)
- {
- cs_error_t error;
- struct evs_inst *evs_inst;
- error = hdb_error_to_cs(hdb_handle_create (&evs_handle_t_db, sizeof (struct evs_inst), handle));
- if (error != CS_OK) {
- goto error_no_destroy;
- }
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, *handle, (void *)&evs_inst));
- if (error != CS_OK) {
- goto error_destroy;
- }
- error = coroipcc_service_connect (
- COROSYNC_SOCKET_NAME,
- EVS_SERVICE,
- IPC_REQUEST_SIZE,
- IPC_RESPONSE_SIZE,
- IPC_DISPATCH_SIZE,
- &evs_inst->ipc_ctx);
- if (error != EVS_OK) {
- goto error_put_destroy;
- }
- memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
- pthread_mutex_init (&evs_inst->response_mutex, NULL);
- pthread_mutex_init (&evs_inst->dispatch_mutex, NULL);
- hdb_handle_put (&evs_handle_t_db, *handle);
- return (CS_OK);
- error_put_destroy:
- hdb_handle_put (&evs_handle_t_db, *handle);
- error_destroy:
- hdb_handle_destroy (&evs_handle_t_db, *handle);
- error_no_destroy:
- return (error);
- }
- evs_error_t evs_finalize (
- evs_handle_t handle)
- {
- struct evs_inst *evs_inst;
- cs_error_t error;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- pthread_mutex_lock (&evs_inst->response_mutex);
- /*
- * Another thread has already started finalizing
- */
- if (evs_inst->finalize) {
- pthread_mutex_unlock (&evs_inst->response_mutex);
- hdb_handle_put (&evs_handle_t_db, handle);
- return (EVS_ERR_BAD_HANDLE);
- }
- evs_inst->finalize = 1;
- coroipcc_service_disconnect (evs_inst->ipc_ctx);
- pthread_mutex_unlock (&evs_inst->response_mutex);
- hdb_handle_destroy (&evs_handle_t_db, handle);
- hdb_handle_put (&evs_handle_t_db, handle);
- return (EVS_OK);
- }
- evs_error_t evs_fd_get (
- evs_handle_t handle,
- int *fd)
- {
- cs_error_t error;
- struct evs_inst *evs_inst;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- *fd = coroipcc_fd_get (evs_inst->ipc_ctx);
- hdb_handle_put (&evs_handle_t_db, handle);
- return (CS_OK);
- }
- evs_error_t evs_dispatch (
- evs_handle_t handle,
- cs_dispatch_flags_t dispatch_types)
- {
- int timeout = -1;
- cs_error_t error;
- int cont = 1; /* always continue do loop except when set to 0 */
- int dispatch_avail;
- struct evs_inst *evs_inst;
- struct res_evs_confchg_callback *res_evs_confchg_callback;
- struct res_evs_deliver_callback *res_evs_deliver_callback;
- evs_callbacks_t callbacks;
- coroipc_response_header_t *dispatch_data;
- int ignore_dispatch = 0;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- /*
- * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
- * wait indefinately for SA_DISPATCH_BLOCKING
- */
- if (dispatch_types == EVS_DISPATCH_ALL) {
- timeout = 0;
- }
- do {
- pthread_mutex_lock (&evs_inst->dispatch_mutex);
- dispatch_avail = coroipcc_dispatch_get (
- evs_inst->ipc_ctx,
- (void **)&dispatch_data,
- timeout);
- pthread_mutex_unlock (&evs_inst->dispatch_mutex);
- if (dispatch_avail == 0 && dispatch_types == EVS_DISPATCH_ALL) {
- break; /* exit do while cont is 1 loop */
- } else
- if (dispatch_avail == 0) {
- continue; /* next dispatch event */
- }
- if (dispatch_avail == -1) {
- if (evs_inst->finalize == 1) {
- error = CS_OK;
- } else {
- error = CS_ERR_LIBRARY;
- }
- goto error_put;
- }
- /*
- * Make copy of callbacks, message data, unlock instance, and call callback
- * A risk of this dispatch method is that the callback routines may
- * operate at the same time that evsFinalize has been called.
- */
- memcpy (&callbacks, &evs_inst->callbacks, sizeof (evs_callbacks_t));
- /*
- * Dispatch incoming message
- */
- switch (dispatch_data->id) {
- case MESSAGE_RES_EVS_DELIVER_CALLBACK:
- res_evs_deliver_callback = (struct res_evs_deliver_callback *)dispatch_data;
- callbacks.evs_deliver_fn (
- res_evs_deliver_callback->local_nodeid,
- &res_evs_deliver_callback->msg,
- res_evs_deliver_callback->msglen);
- break;
- case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
- res_evs_confchg_callback = (struct res_evs_confchg_callback *)dispatch_data;
- callbacks.evs_confchg_fn (
- res_evs_confchg_callback->member_list,
- res_evs_confchg_callback->member_list_entries,
- res_evs_confchg_callback->left_list,
- res_evs_confchg_callback->left_list_entries,
- res_evs_confchg_callback->joined_list,
- res_evs_confchg_callback->joined_list_entries);
- break;
- default:
- coroipcc_dispatch_put (evs_inst->ipc_ctx);
- error = CS_ERR_LIBRARY;
- goto error_put;
- break;
- }
- coroipcc_dispatch_put (evs_inst->ipc_ctx);
- /*
- * Determine if more messages should be processed
- * */
- switch (dispatch_types) {
- case EVS_DISPATCH_ONE:
- if (ignore_dispatch) {
- ignore_dispatch = 0;
- } else {
- cont = 0;
- }
- break;
- case EVS_DISPATCH_ALL:
- if (ignore_dispatch) {
- ignore_dispatch = 0;
- }
- break;
- case EVS_DISPATCH_BLOCKING:
- break;
- }
- } while (cont);
- error_put:
- hdb_handle_put (&evs_handle_t_db, handle);
- return (error);
- }
- evs_error_t evs_join (
- evs_handle_t handle,
- const struct evs_group *groups,
- size_t group_entries)
- {
- evs_error_t error;
- struct evs_inst *evs_inst;
- struct iovec iov[2];
- struct req_lib_evs_join req_lib_evs_join;
- struct res_lib_evs_join res_lib_evs_join;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != EVS_OK) {
- return (error);
- }
- req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
- (group_entries * sizeof (struct evs_group));
- req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
- req_lib_evs_join.group_entries = group_entries;
- iov[0].iov_base = &req_lib_evs_join;
- iov[0].iov_len = sizeof (struct req_lib_evs_join);
- iov[1].iov_base = (void*) groups; /* cast away const */
- iov[1].iov_len = (group_entries * sizeof (struct evs_group));
- pthread_mutex_lock (&evs_inst->response_mutex);
- error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov, 2,
- &res_lib_evs_join, sizeof (struct res_lib_evs_join));
- pthread_mutex_unlock (&evs_inst->response_mutex);
- if (error != CS_OK) {
- goto error_exit;
- }
- error = res_lib_evs_join.header.error;
- error_exit:
- hdb_handle_put (&evs_handle_t_db, handle);
- return (error);
- }
- evs_error_t evs_leave (
- evs_handle_t handle,
- const struct evs_group *groups,
- size_t group_entries)
- {
- evs_error_t error;
- struct evs_inst *evs_inst;
- struct iovec iov[2];
- struct req_lib_evs_leave req_lib_evs_leave;
- struct res_lib_evs_leave res_lib_evs_leave;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
- (group_entries * sizeof (struct evs_group));
- req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
- req_lib_evs_leave.group_entries = group_entries;
- iov[0].iov_base = &req_lib_evs_leave;
- iov[0].iov_len = sizeof (struct req_lib_evs_leave);
- iov[1].iov_base = (void *) groups; /* cast away const */
- iov[1].iov_len = (group_entries * sizeof (struct evs_group));
- pthread_mutex_lock (&evs_inst->response_mutex);
- error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov, 2,
- &res_lib_evs_leave, sizeof (struct res_lib_evs_leave));
- pthread_mutex_unlock (&evs_inst->response_mutex);
- if (error != CS_OK) {
- goto error_exit;
- }
- error = res_lib_evs_leave.header.error;
- error_exit:
- hdb_handle_put (&evs_handle_t_db, handle);
- return (error);
- }
- evs_error_t evs_mcast_joined (
- evs_handle_t handle,
- evs_guarantee_t guarantee,
- const struct iovec *iovec,
- unsigned int iov_len)
- {
- int i;
- evs_error_t error;
- struct evs_inst *evs_inst;
- struct iovec iov[64];
- struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
- struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
- size_t msg_len = 0;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- for (i = 0; i < iov_len; i++ ) {
- msg_len += iovec[i].iov_len;
- }
- req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
- msg_len;
- req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
- req_lib_evs_mcast_joined.guarantee = guarantee;
- req_lib_evs_mcast_joined.msg_len = msg_len;
- iov[0].iov_base = &req_lib_evs_mcast_joined;
- iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
- memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
- pthread_mutex_lock (&evs_inst->response_mutex);
- error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov,
- iov_len + 1,
- &res_lib_evs_mcast_joined,
- sizeof (struct res_lib_evs_mcast_joined));
- pthread_mutex_unlock (&evs_inst->response_mutex);
- if (error != CS_OK) {
- goto error_exit;
- }
- error = res_lib_evs_mcast_joined.header.error;
- error_exit:
- hdb_handle_put (&evs_handle_t_db, handle);
- return (error);
- }
- evs_error_t evs_mcast_groups (
- evs_handle_t handle,
- evs_guarantee_t guarantee,
- const struct evs_group *groups,
- size_t group_entries,
- const struct iovec *iovec,
- unsigned int iov_len)
- {
- int i;
- evs_error_t error;
- struct evs_inst *evs_inst;
- struct iovec iov[64]; /* FIXME: what if iov_len > 62 ? use malloc */
- struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
- struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
- size_t msg_len = 0;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- for (i = 0; i < iov_len; i++) {
- msg_len += iovec[i].iov_len;
- }
- req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
- (group_entries * sizeof (struct evs_group)) + msg_len;
- req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
- req_lib_evs_mcast_groups.guarantee = guarantee;
- req_lib_evs_mcast_groups.msg_len = msg_len;
- req_lib_evs_mcast_groups.group_entries = group_entries;
- iov[0].iov_base = &req_lib_evs_mcast_groups;
- iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
- iov[1].iov_base = (void *) groups; /* cast away const */
- iov[1].iov_len = (group_entries * sizeof (struct evs_group));
- memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
- pthread_mutex_lock (&evs_inst->response_mutex);
- error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov,
- iov_len + 2,
- &res_lib_evs_mcast_groups,
- sizeof (struct res_lib_evs_mcast_groups));
- pthread_mutex_unlock (&evs_inst->response_mutex);
- if (error != CS_OK) {
- goto error_exit;
- }
- error = res_lib_evs_mcast_groups.header.error;
- error_exit:
- hdb_handle_put (&evs_handle_t_db, handle);
- return (error);
- }
- evs_error_t evs_membership_get (
- evs_handle_t handle,
- unsigned int *local_nodeid,
- unsigned int *member_list,
- size_t *member_list_entries)
- {
- evs_error_t error;
- struct evs_inst *evs_inst;
- struct iovec iov;
- struct req_lib_evs_membership_get req_lib_evs_membership_get;
- struct res_lib_evs_membership_get res_lib_evs_membership_get;
- error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
- if (error != CS_OK) {
- return (error);
- }
- req_lib_evs_membership_get.header.size = sizeof (struct req_lib_evs_membership_get);
- req_lib_evs_membership_get.header.id = MESSAGE_REQ_EVS_MEMBERSHIP_GET;
- iov.iov_base = &req_lib_evs_membership_get;
- iov.iov_len = sizeof (struct req_lib_evs_membership_get);
- pthread_mutex_lock (&evs_inst->response_mutex);
- error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx,
- &iov,
- 1,
- &res_lib_evs_membership_get,
- sizeof (struct res_lib_evs_membership_get));
- pthread_mutex_unlock (&evs_inst->response_mutex);
- if (error != CS_OK) {
- goto error_exit;
- }
- error = res_lib_evs_membership_get.header.error;
- /*
- * Copy results to caller
- */
- if (local_nodeid) {
- *local_nodeid = res_lib_evs_membership_get.local_nodeid;
- }
- *member_list_entries = MIN (*member_list_entries,
- res_lib_evs_membership_get.member_list_entries);
- if (member_list) {
- memcpy (member_list, &res_lib_evs_membership_get.member_list,
- *member_list_entries * sizeof (struct in_addr));
- }
- error_exit:
- hdb_handle_put (&evs_handle_t_db, handle);
- return (error);
- }
- /** @} */
|