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_auth_signal(hammer2_router_t *router);
40 static void master_auth_rxmsg(hammer2_msg_t *msg);
41 static void master_link_signal(hammer2_router_t *router);
42 static void master_link_rxmsg(hammer2_msg_t *msg);
43 static void master_reconnect(const char *mntpt);
46 * Start-up the master listener daemon for the machine.
48 * The master listener serves as a rendezvous point in the cluster, accepting
49 * connections, performing registrations and authentications, maintaining
50 * the spanning tree, and keeping track of message state so disconnects can
51 * be handled properly.
53 * Once authenticated only low-level messaging protocols (which includes
54 * tracking persistent messages) are handled by this daemon. This daemon
55 * does not run the higher level quorum or locking protocols.
57 * This daemon can also be told to maintain connections to other nodes,
58 * forming a messaging backbone, which in turn allows PFS's (if desired) to
59 * simply connect to the master daemon via localhost if desired.
60 * Backbones are specified via /etc/hammer2.conf.
65 struct sockaddr_in lsin;
70 * Acquire socket and set options
72 if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
73 fprintf(stderr, "master_listen: socket(): %s\n",
78 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
81 * Setup listen port and try to bind. If the bind fails we assume
82 * that a master listener process is already running and silently
85 bzero(&lsin, sizeof(lsin));
86 lsin.sin_family = AF_INET;
87 lsin.sin_addr.s_addr = INADDR_ANY;
88 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
89 if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
93 "master listen: daemon already running\n");
98 fprintf(stderr, "master listen: startup\n");
102 * Fork and disconnect the controlling terminal and parent process,
103 * executing the specified function as a pthread.
105 * Returns to the original process which can then continue running.
106 * In debug mode this call will create the pthread without forking
107 * and set NormalExit to 0, instead of fork.
109 hammer2_demon(master_accept, (void *)(intptr_t)lfd);
116 * Master listen/accept thread. Accept connections on the master socket,
117 * starting a pthread for each one.
121 master_accept(void *data)
123 struct sockaddr_in asin;
126 hammer2_master_service_info_t *info;
127 int lfd = (int)(intptr_t)data;
131 struct statfs *mntbuf = NULL;
132 struct statvfs *mntvbuf = NULL;
135 * Nobody waits for us
137 setproctitle("hammer2 master listen");
138 pthread_detach(pthread_self());
141 * Scan existing hammer2 mounts and reconnect to them using
142 * HAMMER2IOC_RECLUSTER.
144 count = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
145 for (i = 0; i < count; ++i) {
146 if (strcmp(mntbuf[i].f_fstypename, "hammer2") == 0)
147 master_reconnect(mntbuf[i].f_mntonname);
151 * Accept connections and create pthreads to handle them after
156 fd = accept(lfd, (struct sockaddr *)&asin, &alen);
163 fprintf(stderr, "master_accept: accept fd %d\n", fd);
164 info = malloc(sizeof(*info));
165 bzero(info, sizeof(*info));
168 pthread_create(&thread, NULL, master_service, info);
174 * Normally the mount program supplies a cluster communications
175 * descriptor to the hammer2 vfs on mount, but if you kill the service
176 * daemon and restart it that link will be lost.
178 * This procedure attempts to [re]connect to existing mounts when
179 * the service daemon is started up before going into its accept
184 master_reconnect(const char *mntpt)
186 struct hammer2_ioc_recluster recls;
187 hammer2_master_service_info_t *info;
192 fd = open(mntpt, O_RDONLY);
194 fprintf(stderr, "reconnect %s: no access to mount\n", mntpt);
197 if (pipe(pipefds) < 0) {
198 fprintf(stderr, "reconnect %s: pipe() failed\n", mntpt);
201 bzero(&recls, sizeof(recls));
202 recls.fd = pipefds[0];
203 if (ioctl(fd, HAMMER2IOC_RECLUSTER, &recls) < 0) {
204 fprintf(stderr, "reconnect %s: ioctl failed\n", mntpt);
212 info = malloc(sizeof(*info));
213 bzero(info, sizeof(*info));
214 info->fd = pipefds[1];
216 pthread_create(&thread, NULL, master_service, info);
220 * Service an accepted connection (runs as a pthread)
222 * (also called from a couple of other places)
225 master_service(void *data)
227 hammer2_master_service_info_t *info = data;
228 hammer2_iocom_t iocom;
231 pthread_detach(pthread_self());
233 hammer2_iocom_init(&iocom, info->fd, -1,
237 hammer2_iocom_core(&iocom);
240 "iocom on fd %d terminated error rx=%d, tx=%d\n",
241 info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
243 info->fd = -1; /* safety */
249 /************************************************************************
251 ************************************************************************
253 * Callback via hammer2_iocom_core().
255 * Additional messaging-based authentication must occur before normal
256 * message operation. The connection has already been encrypted at
259 static void master_auth_conn_rx(hammer2_msg_t *msg);
263 master_auth_signal(hammer2_router_t *router)
268 * Transmit LNK_CONN, enabling the SPAN protocol if both sides
271 * XXX put additional authentication states here
273 msg = hammer2_msg_alloc(router, 0, HAMMER2_LNK_CONN |
275 master_auth_conn_rx, NULL);
276 snprintf(msg->any.lnk_conn.label, sizeof(msg->any.lnk_conn.label), "*");
277 hammer2_msg_write(msg);
279 hammer2_router_restate(router,
287 master_auth_conn_rx(hammer2_msg_t *msg)
289 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
290 hammer2_msg_reply(msg, 0);
295 master_auth_rxmsg(hammer2_msg_t *msg __unused)
299 /************************************************************************
300 * POST-AUTHENTICATION SERVICE MSGS *
301 ************************************************************************
303 * Callback via hammer2_iocom_core().
307 master_link_signal(hammer2_router_t *router)
309 hammer2_msg_lnk_signal(router);
314 master_link_rxmsg(hammer2_msg_t *msg)
316 hammer2_state_t *state;
320 * If the message state has a function established we just
321 * call the function, otherwise we call the appropriate
322 * link-level protocol related to the original command and
323 * let it sort it out.
325 * Non-transactional one-off messages, on the otherhand,
326 * might have REPLY set.
329 cmd = state ? state->msg->any.head.cmd : msg->any.head.cmd;
331 fprintf(stderr, "service-receive: %s\n", hammer2_msg_str(msg));
333 if (state && state->func) {
334 assert(state->func != NULL);
337 switch(cmd & HAMMER2_MSGF_PROTOS) {
338 case HAMMER2_MSG_PROTO_LNK:
339 hammer2_msg_lnk(msg);
341 case HAMMER2_MSG_PROTO_DBG:
342 hammer2_msg_dbg(msg);
345 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);