hammer2 - cluster / libdmsg circuit work
[dragonfly.git] / lib / libdmsg / service.c
CommitLineData
0c3a8cd0
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "dmsg_local.h"
37
0d20ec8a 38static void master_auth_signal(dmsg_iocom_t *iocom);
0c3a8cd0 39static void master_auth_rxmsg(dmsg_msg_t *msg);
0d20ec8a 40static void master_link_signal(dmsg_iocom_t *iocom);
0c3a8cd0
MD
41static void master_link_rxmsg(dmsg_msg_t *msg);
42
43/*
44 * Service an accepted connection (runs as a pthread)
45 *
46 * (also called from a couple of other places)
47 */
48void *
49dmsg_master_service(void *data)
50{
51 dmsg_master_service_info_t *info = data;
52 dmsg_iocom_t iocom;
53
54 if (info->detachme)
55 pthread_detach(pthread_self());
56
57 dmsg_iocom_init(&iocom, info->fd, -1,
58 master_auth_signal,
59 master_auth_rxmsg,
11f7caf4 60 info->dbgmsg_callback,
0c3a8cd0
MD
61 NULL);
62 dmsg_iocom_core(&iocom);
63
64 fprintf(stderr,
65 "iocom on fd %d terminated error rx=%d, tx=%d\n",
66 info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
67 close(info->fd);
68 info->fd = -1; /* safety */
185ace93
MD
69 if (info->exit_callback)
70 info->exit_callback(info->handle);
0c3a8cd0
MD
71 free(info);
72
73 return (NULL);
74}
75
76/************************************************************************
77 * AUTHENTICATION *
78 ************************************************************************
79 *
80 * Callback via dmsg_iocom_core().
81 *
82 * Additional messaging-based authentication must occur before normal
83 * message operation. The connection has already been encrypted at
84 * this point.
85 */
86static void master_auth_conn_rx(dmsg_msg_t *msg);
87
88static
89void
0d20ec8a 90master_auth_signal(dmsg_iocom_t *iocom)
0c3a8cd0
MD
91{
92 dmsg_msg_t *msg;
93
94 /*
95 * Transmit LNK_CONN, enabling the SPAN protocol if both sides
96 * agree.
97 *
98 * XXX put additional authentication states here?
99 */
0d20ec8a
MD
100 msg = dmsg_msg_alloc(&iocom->circuit0, 0,
101 DMSG_LNK_CONN | DMSGF_CREATE,
0c3a8cd0
MD
102 master_auth_conn_rx, NULL);
103 msg->any.lnk_conn.peer_mask = (uint64_t)-1;
104 msg->any.lnk_conn.peer_type = DMSG_PEER_CLUSTER;
0d20ec8a 105 msg->any.lnk_conn.pfs_mask = (uint64_t)-1;
0c3a8cd0
MD
106
107 dmsg_msg_write(msg);
108
0d20ec8a 109 dmsg_iocom_restate(iocom,
0c3a8cd0
MD
110 master_link_signal,
111 master_link_rxmsg,
112 NULL);
113}
114
115static
116void
117master_auth_conn_rx(dmsg_msg_t *msg)
118{
119 if (msg->any.head.cmd & DMSGF_DELETE)
120 dmsg_msg_reply(msg, 0);
121}
122
123static
124void
125master_auth_rxmsg(dmsg_msg_t *msg __unused)
126{
127}
128
129/************************************************************************
130 * POST-AUTHENTICATION SERVICE MSGS *
131 ************************************************************************
132 *
133 * Callback via dmsg_iocom_core().
134 */
135static
136void
0d20ec8a 137master_link_signal(dmsg_iocom_t *iocom)
0c3a8cd0 138{
0d20ec8a 139 dmsg_msg_lnk_signal(iocom);
0c3a8cd0
MD
140}
141
142static
143void
144master_link_rxmsg(dmsg_msg_t *msg)
145{
146 dmsg_state_t *state;
147 uint32_t cmd;
148
149 /*
150 * If the message state has a function established we just
151 * call the function, otherwise we call the appropriate
152 * link-level protocol related to the original command and
153 * let it sort it out.
154 *
155 * Non-transactional one-off messages, on the otherhand,
156 * might have REPLY set.
157 */
158 state = msg->state;
159 cmd = state ? state->msg->any.head.cmd : msg->any.head.cmd;
160
161 fprintf(stderr, "service-receive: %s\n", dmsg_msg_str(msg));
162
163 if (state && state->func) {
164 assert(state->func != NULL);
165 state->func(msg);
166 } else {
167 switch(cmd & DMSGF_PROTOS) {
168 case DMSG_PROTO_LNK:
169 dmsg_msg_lnk(msg);
170 break;
171 case DMSG_PROTO_DBG:
172 dmsg_msg_dbg(msg);
173 break;
174 default:
175 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
176 break;
177 }
178 }
179}
180
181/*
182 * This is called from the master node to process a received debug
183 * shell command. We process the command, outputting the results,
184 * then finish up by outputting another prompt.
185 */
186void
187dmsg_msg_dbg(dmsg_msg_t *msg)
188{
189 switch(msg->any.head.cmd & DMSGF_CMDSWMASK) {
190 case DMSG_DBG_SHELL:
191 /*
192 * This is a command which we must process.
193 * When we are finished we generate a final reply.
194 */
195 if (msg->aux_data)
196 msg->aux_data[msg->aux_size - 1] = 0;
0d20ec8a 197 msg->iocom->dbgmsg_callback(msg);
0c3a8cd0
MD
198 dmsg_msg_reply(msg, 0);
199 break;
200 case DMSG_DBG_SHELL | DMSGF_REPLY:
201 /*
202 * A reply just prints out the string. No newline is added
203 * (it is expected to be embedded if desired).
204 */
205 if (msg->aux_data)
206 msg->aux_data[msg->aux_size - 1] = 0;
207 if (msg->aux_data)
208 write(2, msg->aux_data, strlen(msg->aux_data));
209 break;
210 default:
211 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
212 break;
213 }
214}
215
216/*
217 * Returns text debug output to the original defined by (msg). (msg) is
218 * not modified and stays intact. We use a one-way message with REPLY set
219 * to distinguish between a debug command and debug terminal output.
220 *
0d20ec8a
MD
221 * To prevent loops circuit_printf() can filter the message (cmd) related
222 * to the circuit_printf(). We filter out DBG messages.
0c3a8cd0
MD
223 */
224void
0d20ec8a 225dmsg_circuit_printf(dmsg_circuit_t *circuit, const char *ctl, ...)
0c3a8cd0
MD
226{
227 dmsg_msg_t *rmsg;
228 va_list va;
229 char buf[1024];
230 size_t len;
231
232 va_start(va, ctl);
233 vsnprintf(buf, sizeof(buf), ctl, va);
234 va_end(va);
235 len = strlen(buf) + 1;
236
0d20ec8a
MD
237 rmsg = dmsg_msg_alloc(circuit, len,
238 DMSG_DBG_SHELL | DMSGF_REPLY,
239 NULL, NULL);
0c3a8cd0
MD
240 bcopy(buf, rmsg->aux_data, len);
241
242 dmsg_msg_write(rmsg);
243}