Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sbin / hammer2 / network.h
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 /***************************************************************************
37  *                              LOW LEVEL MESSAGING                        *
38  ***************************************************************************
39  *
40  * hammer2_msg - A standalone copy of a message, typically referenced by
41  *               or embedded in other structures, or used with I/O queues.
42  *
43  * These structures are strictly temporary, so they do not have to be
44  * particularly optimized for size.  All possible message headers are
45  * directly embedded (any), and the message may contain a reference
46  * to allocated auxillary data.  The structure is recycled quite often
47  * by a connection.
48  *
49  * This structure is typically not used for storing persistent message
50  * state (see hammer2_persist for that).
51  */
52 struct hammer2_iocom;
53 struct hammer2_persist;
54
55 struct hammer2_msg {
56         struct hammer2_iocom *iocom;
57         struct hammer2_persist  *persist;
58         TAILQ_ENTRY(hammer2_msg) entry; /* queue */
59         char            *aux_data;      /* aux-data if any */
60         int             aux_size;
61         int             flags;
62         hammer2_any_t   any;            /* raw extended msg header */
63 };
64
65 typedef struct hammer2_msg hammer2_msg_t;
66
67 TAILQ_HEAD(hammer2_msg_queue, hammer2_msg);
68 typedef struct hammer2_msg_queue hammer2_msg_queue_t;
69
70 #define HAMMER2_MSGX_BSWAPPED   0x0001
71
72 /*
73  * hammer2_ioq - An embedded component of hammer2_connect, holds state
74  * for the buffering and parsing of incoming and outgoing messages.
75  */
76 struct hammer2_ioq {
77         enum { HAMMER2_MSGQ_STATE_HEADER1,
78                HAMMER2_MSGQ_STATE_HEADER2,
79                HAMMER2_MSGQ_STATE_AUXDATA1,
80                HAMMER2_MSGQ_STATE_AUXDATA2,
81                HAMMER2_MSGQ_STATE_ERROR } state;
82         int             fifo_beg;               /* buffered data */
83         int             fifo_end;
84         int             hbytes;                 /* header size */
85         int             abytes;                 /* aux_data size */
86         int             error;
87         int             seq;                    /* salt sequencer */
88         int             msgcount;
89         hammer2_msg_t   *msg;
90         hammer2_msg_queue_t msgq;
91 };
92
93 typedef struct hammer2_ioq hammer2_ioq_t;
94
95 #define HAMMER2_IOQ_ERROR_SYNC  1               /* bad magic / out of sync */
96 #define HAMMER2_IOQ_ERROR_EOF   2               /* unexpected EOF */
97 #define HAMMER2_IOQ_ERROR_SOCK  3               /* read() error on socket */
98 #define HAMMER2_IOQ_ERROR_FIELD 4               /* invalid field */
99 #define HAMMER2_IOQ_ERROR_HCRC  5               /* core header crc bad */
100 #define HAMMER2_IOQ_ERROR_XCRC  6               /* ext header crc bad */
101 #define HAMMER2_IOQ_ERROR_ACRC  7               /* aux data crc bad */
102 #define HAMMER2_IOQ_ERROR_STATE 8               /* bad state */
103
104 #define HAMMER2_IOQ_MAXIOVEC    16
105
106 /*
107  * hammer2_iocom - governs a messaging stream connection
108  */
109 struct hammer2_iocom {
110         hammer2_ioq_t   ioq_rx;
111         hammer2_ioq_t   ioq_tx;
112         hammer2_msg_queue_t freeq;              /* free msgs hdr only */
113         hammer2_msg_queue_t freeq_aux;          /* free msgs w/aux_data */
114         void    (*recvmsg_callback)(struct hammer2_iocom *);
115         void    (*sendmsg_callback)(struct hammer2_iocom *);
116         void    (*altmsg_callback)(struct hammer2_iocom *);
117         int     sock_fd;                        /* comm socket or pipe */
118         int     alt_fd;                         /* thread signal, tty, etc */
119         int     flags;
120         char    rxbuf[HAMMER2_MSGBUF_SIZE];     /* for ioq_rx only */
121 };
122
123 typedef struct hammer2_iocom hammer2_iocom_t;
124
125 #define HAMMER2_IOCOMF_EOF      0x00000001      /* EOF or ERROR on desc */
126 #define HAMMER2_IOCOMF_RREQ     0x00000002      /* request read-data event */
127 #define HAMMER2_IOCOMF_WREQ     0x00000004      /* request write-avail event */
128 #define HAMMER2_IOCOMF_WIDLE    0x00000008      /* request write-avail event */
129 #define HAMMER2_IOCOMF_SIGNAL   0x00000010
130
131 /***************************************************************************
132  *                              HIGH LEVEL MESSAGING                       *
133  ***************************************************************************
134  *
135  * Persistent state is stored via the hammer2_persist structure.
136  */
137 struct hammer2_persist {
138         uint32_t        lcmd;           /* recent command direction */
139         uint32_t        lrep;           /* recent reply direction */
140 };
141
142 typedef struct hammer2_persist hammer2_persist_t;
143
144 #if 0
145
146
147
148 /*
149  * The global registration structure consolidates information accumulated
150  * via the spanning tree algorithm and tells us which connection (link)
151  * is the best path to get to any given registration.
152  *
153  * glob_node    - Splay entry for this registration in the global index
154  *                of all registrations.
155  *
156  * glob_entry   - tailq entry when this registration's best_span element
157  *                has changed state.
158  *
159  * span_list    - Head of a simple list of spanning tree entries which
160  *                we use to determine the best link.
161  *
162  * best_span    - Which of the span structure on span_list is the best
163  *                one.
164  *
165  * source_root  - Splay tree root indexing all mesasges sent from this
166  *                registration.  The messages are indexed by
167  *                {linkid,msgid} XXX
168  *
169  * target_root  - Splay tree root indexing all messages being sent to
170  *                this registration.  The messages are indexed by
171  *                {linkid,msgid}. XXX
172  *
173  *
174  * Whenever spanning tree data causes a registration's best_link field to
175  * change that registration is transmitted as spanning tree data to every
176  * active link.  Note that pure clients to the cluster, of which there can
177  * be millions, typically do not transmit spanning tree data to each other.
178  *
179  * Each registration is assigned a unique linkid local to the node (another
180  * node might assign a different linkid to the same registration).  This
181  * linkid must be persistent as long as messages are active and is used
182  * to identify the message source and target.
183  */
184 TAILQ_HEAD(hammer2_span_list, hammer2_span);
185 typedef struct hammer2_span_list hammer2_span_list_t;
186
187 struct hammer2_reg {
188         SPLAY_ENTRY(hammer2_reg) glob_node;     /* index of registrations */
189         TAILQ_ENTRY(hammer2_reg) glob_entry;    /* when modified */
190         hammer2_span_list_t     span_list;      /* list of hammer2_span's */
191         hammer2_span_t          *best_span;     /* best span entry */
192         hammer2_pmsg_splay_head_t source_root;  /* msgs sent from reg */
193         hammer2_pmsg_splay_head_t target_root;  /* msgs sent to reg */
194         uuid_t  pfs_id;                         /* key field */
195         uuid_t  pfs_fsid;                       /* key field */
196         uint32_t linkid;
197         int     flags;
198         int     refs;
199 };
200
201 #define HAMMER2_PROTO_REGF_MODIFIED     0x0001
202
203 /*
204  * Each link (connection) collects spanning tree data received via the
205  * link and stores it in these span structures.
206  */
207 struct hammer2_span {
208         TAILQ_ENTRY(hammer2_span)       span_entry;     /* from hammer2_reg */
209         SPLAY_ENTRY(hammer2_span)       span_node;      /* from hammer2_link */
210         hammer2_reg_t                   *reg;
211         hammer2_link_t                  *link;
212         int                             weight;
213 };
214
215 /*
216  * Most hammer2 messages represent transactions and have persistent state
217  * which must be recorded.  Some messages, such as cache states and inode
218  * representations are very long-lasting transactions.
219  *
220  * Each node in the graph must keep track of the message state in order
221  * to perform the proper action when a connection is lost.  To do this
222  * the message is indexed on the source and target (global) registration,
223  * and the actual span element the message was received on and transmitted
224  * to is recorded (allowing us to retrieve the physical links involved).
225  *
226  * The {source_reg, target_reg, msgid} uniquely identifies a message.  Any
227  * streaming operations using the same msgid use the same rendezvous.
228  *
229  * It is important to note that recorded state must use the same physical
230  * link (and thus the same chain of links across the graph) as was 'forged'
231  * by the initial message for that msgid.  If the source span a message is
232  * received on does not match the recorded source, or the recorded target
233  * is no longer routeable, the message will be returned or generate an ABORT
234  * with LINKFAIL as appropriate.
235  */
236 struct hammer2_pmsg {
237         SPLAY_ENTRY(hammer2_pmsg) source_reg;
238         SPLAY_ENTRY(hammer2_pmsg) target_reg;
239         hammer2_span_t  *source;
240         hammer2_span_t  *target;
241         uint16_t        msgid;
242 };
243
244 #endif