cluster - Major kernel component work (diskiocom, xdisk, kdmsg)
[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 61 NULL);
f306de83
MD
62 if (info->label) {
63 dmsg_iocom_label(&iocom, "%s", info->label);
64 free(info->label);
65 info->label = NULL;
66 }
0c3a8cd0
MD
67 dmsg_iocom_core(&iocom);
68
69 fprintf(stderr,
70 "iocom on fd %d terminated error rx=%d, tx=%d\n",
71 info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
72 close(info->fd);
73 info->fd = -1; /* safety */
185ace93
MD
74 if (info->exit_callback)
75 info->exit_callback(info->handle);
0c3a8cd0
MD
76 free(info);
77
78 return (NULL);
79}
80
81/************************************************************************
82 * AUTHENTICATION *
83 ************************************************************************
84 *
85 * Callback via dmsg_iocom_core().
86 *
87 * Additional messaging-based authentication must occur before normal
88 * message operation. The connection has already been encrypted at
89 * this point.
90 */
91static void master_auth_conn_rx(dmsg_msg_t *msg);
92
93static
94void
0d20ec8a 95master_auth_signal(dmsg_iocom_t *iocom)
0c3a8cd0
MD
96{
97 dmsg_msg_t *msg;
98
99 /*
100 * Transmit LNK_CONN, enabling the SPAN protocol if both sides
101 * agree.
102 *
103 * XXX put additional authentication states here?
104 */
0d20ec8a
MD
105 msg = dmsg_msg_alloc(&iocom->circuit0, 0,
106 DMSG_LNK_CONN | DMSGF_CREATE,
0c3a8cd0
MD
107 master_auth_conn_rx, NULL);
108 msg->any.lnk_conn.peer_mask = (uint64_t)-1;
109 msg->any.lnk_conn.peer_type = DMSG_PEER_CLUSTER;
0d20ec8a 110 msg->any.lnk_conn.pfs_mask = (uint64_t)-1;
0c3a8cd0
MD
111
112 dmsg_msg_write(msg);
113
0d20ec8a 114 dmsg_iocom_restate(iocom,
0c3a8cd0
MD
115 master_link_signal,
116 master_link_rxmsg,
117 NULL);
118}
119
120static
121void
122master_auth_conn_rx(dmsg_msg_t *msg)
123{
124 if (msg->any.head.cmd & DMSGF_DELETE)
125 dmsg_msg_reply(msg, 0);
126}
127
128static
129void
130master_auth_rxmsg(dmsg_msg_t *msg __unused)
131{
132}
133
134/************************************************************************
135 * POST-AUTHENTICATION SERVICE MSGS *
136 ************************************************************************
137 *
138 * Callback via dmsg_iocom_core().
139 */
140static
141void
0d20ec8a 142master_link_signal(dmsg_iocom_t *iocom)
0c3a8cd0 143{
0d20ec8a 144 dmsg_msg_lnk_signal(iocom);
0c3a8cd0
MD
145}
146
147static
148void
149master_link_rxmsg(dmsg_msg_t *msg)
150{
151 dmsg_state_t *state;
152 uint32_t cmd;
153
154 /*
155 * If the message state has a function established we just
156 * call the function, otherwise we call the appropriate
157 * link-level protocol related to the original command and
158 * let it sort it out.
159 *
160 * Non-transactional one-off messages, on the otherhand,
161 * might have REPLY set.
162 */
163 state = msg->state;
8d6d37b8 164 cmd = state ? state->icmd : msg->any.head.cmd;
0c3a8cd0
MD
165
166 if (state && state->func) {
167 assert(state->func != NULL);
168 state->func(msg);
169 } else {
170 switch(cmd & DMSGF_PROTOS) {
171 case DMSG_PROTO_LNK:
172 dmsg_msg_lnk(msg);
173 break;
174 case DMSG_PROTO_DBG:
175 dmsg_msg_dbg(msg);
176 break;
177 default:
178 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
179 break;
180 }
181 }
182}
183
184/*
185 * This is called from the master node to process a received debug
186 * shell command. We process the command, outputting the results,
187 * then finish up by outputting another prompt.
188 */
189void
190dmsg_msg_dbg(dmsg_msg_t *msg)
191{
192 switch(msg->any.head.cmd & DMSGF_CMDSWMASK) {
193 case DMSG_DBG_SHELL:
194 /*
195 * This is a command which we must process.
196 * When we are finished we generate a final reply.
197 */
198 if (msg->aux_data)
199 msg->aux_data[msg->aux_size - 1] = 0;
0d20ec8a 200 msg->iocom->dbgmsg_callback(msg);
8d6d37b8 201 dmsg_msg_reply(msg, 0); /* XXX send prompt instead */
0c3a8cd0
MD
202 break;
203 case DMSG_DBG_SHELL | DMSGF_REPLY:
204 /*
205 * A reply just prints out the string. No newline is added
206 * (it is expected to be embedded if desired).
207 */
208 if (msg->aux_data)
209 msg->aux_data[msg->aux_size - 1] = 0;
210 if (msg->aux_data)
211 write(2, msg->aux_data, strlen(msg->aux_data));
212 break;
213 default:
214 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
215 break;
216 }
217}
218
219/*
220 * Returns text debug output to the original defined by (msg). (msg) is
221 * not modified and stays intact. We use a one-way message with REPLY set
222 * to distinguish between a debug command and debug terminal output.
223 *
0d20ec8a
MD
224 * To prevent loops circuit_printf() can filter the message (cmd) related
225 * to the circuit_printf(). We filter out DBG messages.
0c3a8cd0
MD
226 */
227void
0d20ec8a 228dmsg_circuit_printf(dmsg_circuit_t *circuit, const char *ctl, ...)
0c3a8cd0
MD
229{
230 dmsg_msg_t *rmsg;
231 va_list va;
232 char buf[1024];
233 size_t len;
234
235 va_start(va, ctl);
236 vsnprintf(buf, sizeof(buf), ctl, va);
237 va_end(va);
238 len = strlen(buf) + 1;
239
0d20ec8a
MD
240 rmsg = dmsg_msg_alloc(circuit, len,
241 DMSG_DBG_SHELL | DMSGF_REPLY,
242 NULL, NULL);
0c3a8cd0
MD
243 bcopy(buf, rmsg->aux_data, len);
244
245 dmsg_msg_write(rmsg);
246}