2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
38 static void *master_accept(void *data);
39 static void *master_service(void *data);
40 static void master_auth_rx(hammer2_iocom_t *iocom);
41 static void master_auth_tx(hammer2_iocom_t *iocom);
42 static void master_link_rx(hammer2_iocom_t *iocom);
43 static void master_link_tx(hammer2_iocom_t *iocom);
45 static void hammer2_lnk_span(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
48 * Start-up the master listener daemon for the machine.
50 * The master listener serves as a rendezvous point in the cluster, accepting
51 * connections, performing registrations and authentications, maintaining
52 * the spanning tree, and keeping track of message state so disconnects can
53 * be handled properly.
55 * Once authenticated only low-level messaging protocols (which includes
56 * tracking persistent messages) are handled by this daemon. This daemon
57 * does not run the higher level quorum or locking protocols.
59 * This daemon can also be told to maintain connections to other nodes,
60 * forming a messaging backbone, which in turn allows PFS's (if desired) to
61 * simply connect to the master daemon via localhost if desired.
62 * Backbones are specified via /etc/hammer2.conf.
67 struct sockaddr_in lsin;
72 * Acquire socket and set options
74 if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
75 fprintf(stderr, "master_listen: socket(): %s\n",
80 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
83 * Setup listen port and try to bind. If the bind fails we assume
84 * that a master listener process is already running and silently
87 bzero(&lsin, sizeof(lsin));
88 lsin.sin_family = AF_INET;
89 lsin.sin_addr.s_addr = INADDR_ANY;
90 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
91 if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
95 "master listen: daemon already running\n");
100 fprintf(stderr, "master listen: startup\n");
104 * Fork and disconnect the controlling terminal and parent process,
105 * executing the specified function as a pthread.
107 * Returns to the original process which can then continue running.
108 * In debug mode this call will create the pthread without forking
109 * and set NormalExit to 0, instead of fork.
111 hammer2_demon(master_accept, (void *)(intptr_t)lfd);
118 * Master listen/accept thread. Accept connections on the master socket,
119 * starting a pthread for each one.
123 master_accept(void *data)
125 struct sockaddr_in asin;
128 int lfd = (int)(intptr_t)data;
132 * Nobody waits for us
134 setproctitle("hammer2 master listen");
135 pthread_detach(pthread_self());
138 * Accept connections and create pthreads to handle them after
143 fd = accept(lfd, (struct sockaddr *)&asin, &alen);
150 fprintf(stderr, "master_accept: accept fd %d\n", fd);
151 pthread_create(&thread, NULL,
152 master_service, (void *)(intptr_t)fd);
158 * Service an accepted connection (runs as a pthread)
162 master_service(void *data)
164 hammer2_iocom_t iocom;
167 fd = (int)(intptr_t)data;
168 hammer2_iocom_init(&iocom, fd, -1);
169 hammer2_iocom_core(&iocom, master_auth_rx, master_auth_tx, NULL);
172 "iocom on fd %d terminated error rx=%d, tx=%d\n",
173 fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
179 /************************************************************************
181 ************************************************************************
183 * Callback via hammer2_iocom_core().
185 * Additional messaging-based authentication must occur before normal
186 * message operation. The connection has already been encrypted at
191 master_auth_rx(hammer2_iocom_t *iocom __unused)
194 iocom->recvmsg_callback = master_link_rx;
195 iocom->sendmsg_callback = master_link_tx;
200 master_auth_tx(hammer2_iocom_t *iocom __unused)
203 iocom->recvmsg_callback = master_link_rx;
204 iocom->sendmsg_callback = master_link_tx;
207 /************************************************************************
208 * POST-AUTHENTICATION SERVICE MSGS *
209 ************************************************************************
211 * Callback via hammer2_iocom_core().
215 master_link_rx(hammer2_iocom_t *iocom)
220 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
221 (msg = hammer2_ioq_read(iocom)) != NULL) {
223 * Switch on the transactional cmd, that is the original
224 * msg->any.head.cmd that opened the transaction. The actual
225 * msg might be different. The original msg cannot have
226 * REPLY set by definition (but of course the currenet msg
227 * might), so we don't bother with case statements for REPLY
228 * for command sequences we expet to be transactional.
230 * Non-transactional one-off messages, on the otherhand,
231 * might have REPLY set.
234 cmd = msg->state->msg->any.head.cmd;
236 "MSGRX persist=%08x cmd=%08x error %d\n",
237 cmd, msg->any.head.cmd, msg->any.head.error);
239 cmd = msg->any.head.cmd;
241 "MSGRX persist=-------- cmd=%08x error %d\n",
242 cmd, msg->any.head.error);
245 switch(cmd & HAMMER2_MSGF_CMDSWMASK) {
246 case HAMMER2_LNK_ERROR:
248 * A non-transactional error is formulated when
249 * the socket or pipe disconnects. Ignore it.
252 case HAMMER2_LNK_SPAN:
254 * Messages related to the LNK_SPAN transaction.
256 hammer2_lnk_span(iocom, msg);
258 case HAMMER2_DBG_SHELL:
259 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
261 * Non-transactional DBG messages.
263 hammer2_shell_remote(iocom, msg);
266 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
269 hammer2_state_cleanuprx(iocom, msg);
271 if (iocom->ioq_rx.error) {
273 "master_recv: comm error %d\n",
274 iocom->ioq_rx.error);
279 * Callback from hammer2_iocom_core() when messages might be transmittable
284 master_link_tx(hammer2_iocom_t *iocom)
286 hammer2_iocom_flush(iocom);
290 * Receive a message which is part of a LNK_SPAN transaction. Keep in
291 * mind that only the original CREATE is utilizing the lnk_span message
294 * We will get called for CREATE, DELETE, and intermediate states (including
295 * errors), and in particular we will get called with an error if the link
296 * is lost in the middle of the transaction.
300 hammer2_lnk_span(hammer2_iocom_t *iocom __unused, hammer2_msg_t *msg)
304 switch(msg->any.head.cmd & HAMMER2_MSGF_TRANSMASK) {
305 case HAMMER2_LNK_SPAN | HAMMER2_MSGF_CREATE:
308 hammer2_uuid_to_str(&msg->any.lnk_span.pfs_id, &alloc),
309 msg->any.lnk_span.label);
312 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE:
313 fprintf(stderr, "LNK_SPAN: Terminated with error\n");
317 "LNK_SPAN: Unknown msg %08x\n", msg->any.head.cmd);