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