pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
proxy_common.c
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2022 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <glib.h>
13 #include <unistd.h>
14 
15 #include <crm/crm.h>
16 #include <crm/msg_xml.h>
17 #include <crm/services.h>
18 #include <crm/common/mainloop.h>
19 
20 #include <crm/pengine/status.h>
21 #include <crm/cib.h>
22 #include <crm/lrmd.h>
23 #include <crm/lrmd_internal.h>
24 
25 int lrmd_internal_proxy_send(lrmd_t * lrmd, xmlNode *msg);
26 GHashTable *proxy_table = NULL;
27 
28 static void
29 remote_proxy_notify_destroy(lrmd_t *lrmd, const char *session_id)
30 {
31  /* sending to the remote node that an ipc connection has been destroyed */
32  xmlNode *msg = create_xml_node(NULL, T_LRMD_IPC_PROXY);
34  crm_xml_add(msg, F_LRMD_IPC_SESSION, session_id);
35  lrmd_internal_proxy_send(lrmd, msg);
36  free_xml(msg);
37 }
38 
45 void
47 {
48  xmlNode *msg = create_xml_node(NULL, T_LRMD_IPC_PROXY);
50  lrmd_internal_proxy_send(lrmd, msg);
51  free_xml(msg);
52 }
53 
60 void
62 {
63  xmlNode *msg = create_xml_node(NULL, T_LRMD_IPC_PROXY);
65  lrmd_internal_proxy_send(lrmd, msg);
66  free_xml(msg);
67 }
68 
69 void
71 {
72  /* sending to the remote node an event msg. */
73  xmlNode *event = create_xml_node(NULL, T_LRMD_IPC_PROXY);
76  add_message_xml(event, F_LRMD_IPC_MSG, msg);
77  crm_log_xml_explicit(event, "EventForProxy");
78  lrmd_internal_proxy_send(proxy->lrm, event);
79  free_xml(event);
80 }
81 
82 void
83 remote_proxy_relay_response(remote_proxy_t *proxy, xmlNode *msg, int msg_id)
84 {
85  /* sending to the remote node a response msg. */
86  xmlNode *response = create_xml_node(NULL, T_LRMD_IPC_PROXY);
88  crm_xml_add(response, F_LRMD_IPC_SESSION, proxy->session_id);
89  crm_xml_add_int(response, F_LRMD_IPC_MSG_ID, msg_id);
90  add_message_xml(response, F_LRMD_IPC_MSG, msg);
91  lrmd_internal_proxy_send(proxy->lrm, response);
92  free_xml(response);
93 }
94 
95 static void
96 remote_proxy_end_session(remote_proxy_t *proxy)
97 {
98  if (proxy == NULL) {
99  return;
100  }
101  crm_trace("ending session ID %s", proxy->session_id);
102 
103  if (proxy->source) {
105  }
106 }
107 
108 void
110 {
111  remote_proxy_t *proxy = data;
112 
113  crm_trace("freed proxy session ID %s", proxy->session_id);
114  free(proxy->node_name);
115  free(proxy->session_id);
116  free(proxy);
117 }
118 
119 int
120 remote_proxy_dispatch(const char *buffer, ssize_t length, gpointer userdata)
121 {
122  // Async responses from cib and friends to clients via pacemaker-remoted
123  xmlNode *xml = NULL;
124  uint32_t flags = 0;
125  remote_proxy_t *proxy = userdata;
126 
127  xml = string2xml(buffer);
128  if (xml == NULL) {
129  crm_warn("Received a NULL msg from IPC service.");
130  return 1;
131  }
132 
133  flags = crm_ipc_buffer_flags(proxy->ipc);
135  crm_trace("Passing response back to %.8s on %s: %.200s - request id: %d", proxy->session_id, proxy->node_name, buffer, proxy->last_request_id);
136  remote_proxy_relay_response(proxy, xml, proxy->last_request_id);
137  proxy->last_request_id = 0;
138 
139  } else {
140  crm_trace("Passing event back to %.8s on %s: %.200s", proxy->session_id, proxy->node_name, buffer);
141  remote_proxy_relay_event(proxy, xml);
142  }
143  free_xml(xml);
144  return 1;
145 }
146 
147 
148 void
149 remote_proxy_disconnected(gpointer userdata)
150 {
151  remote_proxy_t *proxy = userdata;
152 
153  crm_trace("destroying %p", proxy);
154 
155  proxy->source = NULL;
156  proxy->ipc = NULL;
157 
158  if(proxy->lrm) {
159  remote_proxy_notify_destroy(proxy->lrm, proxy->session_id);
160  proxy->lrm = NULL;
161  }
162 
163  g_hash_table_remove(proxy_table, proxy->session_id);
164 }
165 
167 remote_proxy_new(lrmd_t *lrmd, struct ipc_client_callbacks *proxy_callbacks,
168  const char *node_name, const char *session_id, const char *channel)
169 {
170  remote_proxy_t *proxy = NULL;
171 
172  if(channel == NULL) {
173  crm_err("No channel specified to proxy");
174  remote_proxy_notify_destroy(lrmd, session_id);
175  return NULL;
176  }
177 
178  proxy = calloc(1, sizeof(remote_proxy_t));
179 
180  proxy->node_name = strdup(node_name);
181  proxy->session_id = strdup(session_id);
182  proxy->lrm = lrmd;
183 
185  && !strcmp(pcmk__message_name(channel), CRM_SYSTEM_CRMD)) {
186  // The controller doesn't need to connect to itself
187  proxy->is_local = TRUE;
188 
189  } else {
190  proxy->source = mainloop_add_ipc_client(channel, G_PRIORITY_LOW, 0, proxy, proxy_callbacks);
191  proxy->ipc = mainloop_get_ipc_client(proxy->source);
192  if (proxy->source == NULL) {
193  remote_proxy_free(proxy);
194  remote_proxy_notify_destroy(lrmd, session_id);
195  return NULL;
196  }
197  }
198 
199  crm_trace("new remote proxy client established to %s on %s, session id %s",
200  channel, node_name, session_id);
201  g_hash_table_insert(proxy_table, proxy->session_id, proxy);
202 
203  return proxy;
204 }
205 
206 void
207 remote_proxy_cb(lrmd_t *lrmd, const char *node_name, xmlNode *msg)
208 {
209  const char *op = crm_element_value(msg, F_LRMD_IPC_OP);
210  const char *session = crm_element_value(msg, F_LRMD_IPC_SESSION);
211  remote_proxy_t *proxy = g_hash_table_lookup(proxy_table, session);
212  int msg_id = 0;
213 
214  /* sessions are raw ipc connections to IPC,
215  * all we do is proxy requests/responses exactly
216  * like they are given to us at the ipc level. */
217 
218  CRM_CHECK(op != NULL, return);
219  CRM_CHECK(session != NULL, return);
220 
222  /* This is msg from remote ipc client going to real ipc server */
223 
224  if (pcmk__str_eq(op, LRMD_IPC_OP_DESTROY, pcmk__str_casei)) {
225  remote_proxy_end_session(proxy);
226 
227  } else if (pcmk__str_eq(op, LRMD_IPC_OP_REQUEST, pcmk__str_casei)) {
228  int flags = 0;
229  xmlNode *request = get_message_xml(msg, F_LRMD_IPC_MSG);
230  const char *name = crm_element_value(msg, F_LRMD_IPC_CLIENT);
231 
232  CRM_CHECK(request != NULL, return);
233 
234  if (proxy == NULL) {
235  /* proxy connection no longer exists */
236  remote_proxy_notify_destroy(lrmd, session);
237  return;
238  }
239 
240  // Controller requests MUST be handled by the controller, not us
241  CRM_CHECK(proxy->is_local == FALSE,
242  remote_proxy_end_session(proxy); return);
243 
244  if (!crm_ipc_connected(proxy->ipc)) {
245  remote_proxy_end_session(proxy);
246  return;
247  }
248  proxy->last_request_id = 0;
250  crm_xml_add(request, XML_ACL_TAG_ROLE, "pacemaker-remote");
251 
252  CRM_ASSERT(node_name);
253  pcmk__update_acl_user(request, F_LRMD_IPC_USER, node_name);
254 
256  const char *type = crm_element_value(request, F_TYPE);
257  int rc = 0;
258 
259  if (pcmk__str_eq(type, T_ATTRD, pcmk__str_casei)
260  && crm_element_value(request,
261  PCMK__XA_ATTR_NODE_NAME) == NULL
266  pcmk__xe_add_node(request, proxy->node_name, 0);
267  }
268 
269  rc = crm_ipc_send(proxy->ipc, request, flags, 5000, NULL);
270 
271  if(rc < 0) {
272  xmlNode *op_reply = create_xml_node(NULL, "nack");
273 
274  crm_err("Could not relay %s request %d from %s to %s for %s: %s (%d)",
275  op, msg_id, proxy->node_name, crm_ipc_name(proxy->ipc), name, pcmk_strerror(rc), rc);
276 
277  /* Send a n'ack so the caller doesn't block */
278  crm_xml_add(op_reply, "function", __func__);
279  crm_xml_add_int(op_reply, "line", __LINE__);
280  crm_xml_add_int(op_reply, "rc", rc);
281  remote_proxy_relay_response(proxy, op_reply, msg_id);
282  free_xml(op_reply);
283 
284  } else {
285  crm_trace("Relayed %s request %d from %s to %s for %s",
286  op, msg_id, proxy->node_name, crm_ipc_name(proxy->ipc), name);
287  proxy->last_request_id = msg_id;
288  }
289 
290  } else {
291  int rc = pcmk_ok;
292  xmlNode *op_reply = NULL;
293  // @COMPAT pacemaker_remoted <= 1.1.10
294 
295  crm_trace("Relaying %s request %d from %s to %s for %s",
296  op, msg_id, proxy->node_name, crm_ipc_name(proxy->ipc), name);
297 
298  rc = crm_ipc_send(proxy->ipc, request, flags, 10000, &op_reply);
299  if(rc < 0) {
300  crm_err("Could not relay %s request %d from %s to %s for %s: %s (%d)",
301  op, msg_id, proxy->node_name, crm_ipc_name(proxy->ipc), name, pcmk_strerror(rc), rc);
302  } else {
303  crm_trace("Relayed %s request %d from %s to %s for %s",
304  op, msg_id, proxy->node_name, crm_ipc_name(proxy->ipc), name);
305  }
306 
307  if(op_reply) {
308  remote_proxy_relay_response(proxy, op_reply, msg_id);
309  free_xml(op_reply);
310  }
311  }
312  } else {
313  crm_err("Unknown proxy operation: %s", op);
314  }
315 }
Services API.
#define T_ATTRD
Definition: msg_xml.h:107
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:238
#define PCMK__ATTRD_CMD_UPDATE_DELAY
Definition: crm_internal.h:112
void remote_proxy_disconnected(gpointer userdata)
Definition: proxy_common.c:149
A dumping ground.
#define F_TYPE
Definition: msg_xml.h:91
const char * pcmk_strerror(int rc)
Definition: results.c:149
#define PCMK__ATTRD_CMD_UPDATE_BOTH
Definition: crm_internal.h:111
#define F_LRMD_IPC_CLIENT
Definition: lrmd.h:119
#define LRMD_IPC_OP_SHUTDOWN_NACK
Definition: lrmd.h:114
char data[0]
Definition: cpg.c:55
#define T_LRMD_IPC_PROXY
Definition: lrmd.h:128
#define F_LRMD_IPC_SESSION
Definition: lrmd.h:118
const char * name
Definition: cib.c:26
#define PCMK__XA_TASK
Definition: crm_internal.h:92
void remote_proxy_ack_shutdown(lrmd_t *lrmd)
Definition: proxy_common.c:46
void remote_proxy_relay_response(remote_proxy_t *proxy, xmlNode *msg, int msg_id)
Definition: proxy_common.c:83
#define LRMD_IPC_OP_DESTROY
Definition: lrmd.h:108
#define LRMD_IPC_OP_RESPONSE
Definition: lrmd.h:111
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
Definition: nvpair.c:349
uint32_t last_request_id
Definition: lrmd_internal.h:62
const char * pcmk__update_acl_user(xmlNode *request, const char *field, const char *peer_user)
Definition: acl.c:790
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:302
Resource agent executor.
char * crm_system_name
Definition: utils.c:51
enum crm_ais_msg_types type
Definition: cpg.c:48
void remote_proxy_nack_shutdown(lrmd_t *lrmd)
Definition: proxy_common.c:61
uint32_t crm_ipc_buffer_flags(crm_ipc_t *client)
Definition: ipc_client.c:1220
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:483
Wrappers for and extensions to glib mainloop.
#define F_LRMD_IPC_OP
Definition: lrmd.h:116
xmlNode * string2xml(const char *input)
Definition: xml.c:800
#define F_LRMD_IPC_MSG_ID
Definition: lrmd.h:122
void remote_proxy_free(gpointer data)
Definition: proxy_common.c:109
remote_proxy_t * remote_proxy_new(lrmd_t *lrmd, struct ipc_client_callbacks *proxy_callbacks, const char *node_name, const char *session_id, const char *channel)
Definition: proxy_common.c:167
#define XML_ACL_TAG_ROLE
Definition: msg_xml.h:431
#define crm_warn(fmt, args...)
Definition: logging.h:382
void remote_proxy_relay_event(remote_proxy_t *proxy, xmlNode *msg)
Definition: proxy_common.c:70
#define LRMD_IPC_OP_REQUEST
Definition: lrmd.h:110
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:447
int remote_proxy_dispatch(const char *buffer, ssize_t length, gpointer userdata)
Definition: proxy_common.c:120
#define crm_trace(fmt, args...)
Definition: logging.h:387
#define crm_log_xml_explicit(xml, text)
Definition: logging.h:397
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition: util.h:99
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:638
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:944
#define PCMK__ATTRD_CMD_UPDATE
Definition: crm_internal.h:110
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
Definition: messages.c:160
void free_xml(xmlNode *child)
Definition: xml.c:783
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:957
xmlNode * get_message_xml(const xmlNode *msg, const char *field)
Definition: messages.c:154
const char * pcmk__message_name(const char *name)
Get name to be used as identifier for cluster messages.
Definition: messages.c:180
#define CRM_SYSTEM_CRMD
Definition: crm.h:104
crm_ipc_t * ipc
Definition: lrmd_internal.h:60
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc_client.c:1067
int lrmd_internal_proxy_send(lrmd_t *lrmd, xmlNode *msg)
Definition: lrmd_client.c:1927
#define LRMD_IPC_OP_SHUTDOWN_ACK
Definition: lrmd.h:113
#define F_LRMD_IPC_MSG_FLAGS
Definition: lrmd.h:123
#define PCMK__XA_ATTR_NODE_NAME
Definition: crm_internal.h:68
const char * crm_ipc_name(crm_ipc_t *client)
Definition: ipc_client.c:1234
Cluster status and scheduling.
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:950
#define crm_err(fmt, args...)
Definition: logging.h:381
#define CRM_ASSERT(expr)
Definition: results.h:42
Cluster Configuration.
mainloop_io_t * source
Definition: lrmd_internal.h:61
Definition: lrmd.h:530
#define pcmk_ok
Definition: results.h:68
#define F_LRMD_IPC_USER
Definition: lrmd.h:120
GHashTable * proxy_table
Definition: proxy_common.c:26
#define LRMD_IPC_OP_EVENT
Definition: lrmd.h:109
mainloop_io_t * mainloop_add_ipc_client(const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
Definition: mainloop.c:919
void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid)
Definition: nodes.c:15
int crm_ipc_send(crm_ipc_t *client, const xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
Send an IPC XML message.
Definition: ipc_client.c:1307
gboolean is_local
Definition: lrmd_internal.h:58
uint64_t flags
Definition: remote.c:215
void remote_proxy_cb(lrmd_t *lrmd, const char *node_name, xmlNode *msg)
Definition: proxy_common.c:207
#define F_LRMD_IPC_MSG
Definition: lrmd.h:121