hammer2 - wire up msg transaction state machine
[dragonfly.git] / sys / vfs / hammer2 / 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 #ifndef VFS_HAMMER2_NETWORK_H_
36 #define VFS_HAMMER2_NETWORK_H_
37
38 #ifndef _VFS_HAMMER2_DISK_H_
39 #include "hammer2_disk.h"
40 #endif
41
42 /*
43  * Mesh network protocol structures.
44  *
45  * The mesh is constructed from point-to-point streaming links with varying
46  * levels of interconnectedness, forming a graph.  When a link is established
47  * link id #0 is reserved for link-level communications.  This link is used
48  * for authentication, registration, ping, further link id negotiations,
49  * spanning tree, and so on.
50  *
51  * The spanning tree forms a weighted shortest-path-first graph amongst
52  * those nodes with sufficient administrative rights to relay between
53  * registrations.  Each link maintains a full reachability set, aggregates
54  * it, and retransmits via the shortest path.  However, leaf nodes (even leaf
55  * nodes with multiple connections) can opt not to be part of the spanning
56  * tree and typically (due to administrative rights) their registrations
57  * are not reported to other leafs.
58  *
59  * All message responses follow the SAME PATH that the original message
60  * followed, but in reverse.  This is an absolute requirement since messages
61  * expecting replies record persistent state at each hop.
62  *
63  * Message state is handled by the CREATE, DELETE, REPLY, and ABORT
64  * flags.  Message state is typically recorded at the end points and
65  * at each hop until a DELETE is received from both sides.
66  *
67  * One-way messages such as those used by spanning tree commands are not
68  * recorded.  These are sent without the CREATE, DELETE, or ABORT flags set.
69  * ABORT is not supported for one-off messages.  The REPLY bit can be used
70  * to distinguish between command and status if desired.
71  *
72  * Persistent-state messages are messages which require a reply to be
73  * returned.  These messages can also consist of multiple message elements
74  * for the command or reply or both (or neither).  The command message
75  * sequence sets CREATE on the first message and DELETE on the last message.
76  * A single message command sets both (CREATE|DELETE).  The reply message
77  * sequence works the same way but of course also sets the REPLY bit.
78  *
79  * Persistent-state messages can be aborted by sending a message element
80  * with the ABORT flag set.  This flag can be combined with either or both
81  * the CREATE and DELETE flags.  When combined with the CREATE flag the
82  * command is treated as non-blocking but still executes.  Whem combined
83  * with the DELETE flag no additional message elements are required.
84  *
85  * ABORT SPECIAL CASE - Mid-stream aborts.  A mid-stream abort can be sent
86  * when supported by the sender by sending an ABORT message with neither
87  * CREATE or DELETE set.  This effectively turns the message into a
88  * non-blocking message (but depending on what is being represented can also
89  * cut short prior data elements in the stream).
90  *
91  * ABORT SPECIAL CASE - Abort-after-DELETE.  Persistent messages have to be
92  * abortable if the stream/pipe/whatever is lost.  In this situation any
93  * forwarding relay needs to unconditionally abort commands and replies that
94  * are still active.  This is done by sending an ABORT|DELETE even in
95  * situations where a DELETE has already been sent in that direction.  This
96  * is done, for example, when links are in a half-closed state.  In this
97  * situation it is possible for the abort request to race a transition to the
98  * fully closed state.  ABORT|DELETE messages which race the fully closed
99  * state are expected to be discarded by the other end.
100  *
101  *
102  *                      NEGOTIATION OF {source} AND {target}
103  *
104  * In this discussion 'originator' describes the original sender of a message
105  * and not the relays inbetween, while 'sender' describes the last relay.
106  * The two mean the same thing only when the originator IS the last relay.
107  *
108  * The {source} field is sender-localized.  The sender assigns this field
109  * based on which connection the message originally came from.  The initial
110  * message as sent by the originator sets source=0.  This also means that a
111  * leaf connection will always send messages with source=0.
112  *
113  * The {source} field must be re-localized at each hop, since messages
114  * coming from multiple connections to a node will use conflicting
115  * {source} values.  This can lead to linkid exhaustion which is discussed
116  * a few paragraphs down.
117  *
118  * The {target} field is sender-allocated.  Messages sent to {target} are
119  * preceeded by a FORGE message to {target} which associates a registration
120  * with {target}, or UNFORGE to delete the associtation.
121  *
122  * The msgid field is 32 bits (remember some messages have long-lived
123  * persistent state so this is important!).  One-way messages always use
124  * msgid=0.
125  *
126  *                              LINKID EXHAUSTION
127  *
128  * Because {source} must be re-localized at each hop it is possible to run
129  * out of link identifiers.  At the same time we want to allow millions of
130  * client/leaf connections, and 'millions' is a lot bigger than 65535.
131  *
132  * We also have a problem with the persistent message state... If a single
133  * client's vnode cache has a million vnodes that can represent a million
134  * persistent cache states.  Multiply by a million clients and ... oops!
135  *
136  * To solve these problems leafs connect into protocol-aggregators rather
137  * than directly to the cluster.  The linkid and core message protocols only
138  * occur within the cluster and not by the leafs.  A leaf can still connect
139  * to multiple aggregators for redundancy if it desires but may have to
140  * pick and choose which inodes go where since acquiring a cache state lock
141  * over one connection will cause conflicts to be invalidated on the other.
142  * In otherwords, there are limitations to this approach.
143  *
144  * A protocol aggregator takes any number of connections and aggregates
145  * the operations down to a single linkid.  For example, this means that
146  * the protocol aggregator is responsible for maintaining all the cache
147  * state and performing crunches to reduce the overall amount of state
148  * down to something the cluster core can handle.
149  *
150  * --
151  *
152  * All message headers are 32-byte aligned and sized (all command and
153  * response structures must be 32-byte aligned), and all transports must
154  * support message headers up to HAMMER2_MSGHDR_MAX.  The msg structure
155  * can handle up to 8160 bytes but to keep things fairly clean we limit
156  * message headers to 2048 bytes.
157  *
158  * Any in-band data is padded to a 32-byte alignment and placed directly
159  * after the extended header (after the higher-level cmd/rep structure).
160  * The actual unaligned size of the in-band data is encoded in the aux_bytes
161  * field in this case.  Maximum data sizes are negotiated during registration.
162  *
163  * Use of out-of-band data must be negotiated.  In this case bit 31 of
164  * aux_bytes will be set and the remaining bits will contain information
165  * specific to the out-of-band transfer (such as DMA channel, slot, etc).
166  *
167  * (must be 32 bytes exactly to match the alignment requirement and to
168  *  support pad records in shared-memory FIFO schemes)
169  */
170 struct hammer2_msg_hdr {
171         uint16_t        magic;          /* sanity, synchronization, endian */
172         uint16_t        icrc1;          /* base header crc &salt on */
173         uint32_t        salt;           /* random salt helps crypto/replay */
174
175         uint16_t        source;         /* source linkid */
176         uint16_t        target;         /* target linkid */
177         uint32_t        msgid;          /* message id */
178
179         uint32_t        cmd;            /* flags | cmd | hdr_size / 32 */
180         uint16_t        error;          /* error field */
181         uint16_t        resv05;
182
183         uint16_t        icrc2;          /* extended header crc (after base) */
184         uint16_t        aux_bytes;      /* aux data descriptor or size / 32 */
185         uint32_t        aux_icrc;       /* aux data iscsi crc */
186 };
187
188 typedef struct hammer2_msg_hdr hammer2_msg_hdr_t;
189
190 #define HAMMER2_MSGHDR_MAGIC            0x4832
191 #define HAMMER2_MSGHDR_MAGIC_REV        0x3248
192 #define HAMMER2_MSGHDR_CRCOFF           offsetof(hammer2_msg_hdr_t, salt)
193 #define HAMMER2_MSGHDR_CRCBYTES         (sizeof(hammer2_msg_hdr_t) -    \
194                                          HAMMER2_MSGHDR_CRCOFF)
195
196 /*
197  * Administrative protocol limits.
198  */
199 #define HAMMER2_MSGHDR_MAX              2048    /* msg struct max is 8192-32 */
200 #define HAMMER2_MSGAUX_MAX              65536   /* msg struct max is 2MB-32 */
201 #define HAMMER2_MSGBUF_SIZE             (HAMMER2_MSGHDR_MAX * 4)
202 #define HAMMER2_MSGBUF_MASK             (HAMMER2_MSGBUF_SIZE - 1)
203
204 /*
205  * The message (cmd) field also encodes various flags and the total size
206  * of the message header.  This allows the protocol processors to validate
207  * persistency and structural settings for every command simply by
208  * switch()ing on the (cmd) field.
209  */
210 #define HAMMER2_MSGF_CREATE             0x80000000U     /* msg start */
211 #define HAMMER2_MSGF_DELETE             0x40000000U     /* msg end */
212 #define HAMMER2_MSGF_REPLY              0x20000000U     /* reply path */
213 #define HAMMER2_MSGF_ABORT              0x10000000U     /* abort req */
214 #define HAMMER2_MSGF_AUXOOB             0x08000000U     /* aux-data is OOB */
215 #define HAMMER2_MSGF_FLAG2              0x04000000U
216 #define HAMMER2_MSGF_FLAG1              0x02000000U
217 #define HAMMER2_MSGF_FLAG0              0x01000000U
218
219 #define HAMMER2_MSGF_FLAGS              0xFF000000U     /* all flags */
220 #define HAMMER2_MSGF_PROTOS             0x00F00000U     /* all protos */
221 #define HAMMER2_MSGF_CMDS               0x000FFF00U     /* all cmds */
222 #define HAMMER2_MSGF_SIZE               0x000000FFU     /* N*32 */
223
224 #define HAMMER2_MSGF_CMDSWMASK          (HAMMER2_MSGF_CMDS |    \
225                                          HAMMER2_MSGF_SIZE |    \
226                                          HAMMER2_MSGF_PROTOS |  \
227                                          HAMMER2_MSGF_REPLY)
228
229 #define HAMMER2_MSG_PROTO_LNK           0x00000000U
230 #define HAMMER2_MSG_PROTO_DBG           0x00100000U
231 #define HAMMER2_MSG_PROTO_CAC           0x00200000U
232 #define HAMMER2_MSG_PROTO_QRM           0x00300000U
233 #define HAMMER2_MSG_PROTO_BLK           0x00400000U
234 #define HAMMER2_MSG_PROTO_VOP           0x00500000U
235
236 /*
237  * Message command constructors, sans flags
238  */
239 #define HAMMER2_MSG_ALIGN               32
240 #define HAMMER2_MSG_ALIGNMASK           (HAMMER2_MSG_ALIGN - 1)
241 #define HAMMER2_MSG_DOALIGN(bytes)      (((bytes) + HAMMER2_MSG_ALIGNMASK) & \
242                                          ~HAMMER2_MSG_ALIGNMASK)
243 #define HAMMER2_MSG_HDR_ENCODE(elm)     ((sizeof(struct elm) +          \
244                                           HAMMER2_MSG_ALIGNMASK) /      \
245                                          HAMMER2_MSG_ALIGN)
246
247 #define HAMMER2_MSG_LNK(cmd, elm)       (HAMMER2_MSG_PROTO_LNK |        \
248                                          ((cmd) << 8) |                 \
249                                          HAMMER2_MSG_HDR_ENCODE(elm))
250
251 #define HAMMER2_MSG_DBG(cmd, elm)       (HAMMER2_MSG_PROTO_DBG |        \
252                                          ((cmd) << 8) |                 \
253                                          HAMMER2_MSG_HDR_ENCODE(elm))
254
255 #define HAMMER2_MSG_CAC(cmd, elm)       (HAMMER2_MSG_PROTO_CAC |        \
256                                          ((cmd) << 8) |                 \
257                                          HAMMER2_MSG_HDR_ENCODE(elm))
258
259 #define HAMMER2_MSG_QRM(cmd, elm)       (HAMMER2_MSG_PROTO_QRM |        \
260                                          ((cmd) << 8) |                 \
261                                          HAMMER2_MSG_HDR_ENCODE(elm))
262
263 #define HAMMER2_MSG_BLK(cmd, elm)       (HAMMER2_MSG_PROTO_BLK |        \
264                                          ((cmd) << 8) |                 \
265                                          HAMMER2_MSG_HDR_ENCODE(elm))
266
267 #define HAMMER2_MSG_VOP(cmd, elm)       (HAMMER2_MSG_PROTO_VOP |        \
268                                          ((cmd) << 8) |                 \
269                                          HAMMER2_MSG_HDR_ENCODE(elm))
270
271 /*
272  * Link layer ops basically talk to just the other side of a direct
273  * connection.
274  *
275  * PAD          - One-way message on link-0, ignored by target.  Used to
276  *                pad message buffers on shared-memory transports.  Not
277  *                typically used with TCP.
278  *
279  * AUTHn        - Authenticate the connection, negotiate administrative
280  *                rights & encryption, protocol class, etc.  Only PAD and
281  *                AUTH messages (not even PING) are accepted until
282  *                authentication is complete.  This message also identifies
283  *                the host.
284  *
285  * PING         - One-way message on link-0, keep-alive, run by both sides
286  *                typically 1/sec on idle link, link is lost after 10 seconds
287  *                of inactivity.
288  *
289  * HSPAN        - One-way message on link-0, host-spanning tree message.
290  *                Connection and authentication status is propagated using
291  *                these messages on a per-connection basis.  Works like SPAN
292  *                but is only used for general status.  See the hammer2
293  *                'rinfo' command.
294  *
295  * SPAN         - One-way message on link-0, spanning tree message adds,
296  *                drops, or updates a remote registration.  Sent by both
297  *                sides, delta changes only.  Visbility into remote
298  *                registrations may be limited and received registrations
299  *                may be filtered depending on administrative controls.
300  *
301  *                A multiply-connected node maintains SPAN information on
302  *                each link independently and then retransmits an aggregation
303  *                of the shortest-weighted path for each registration to
304  *                all links when a received change adjusts the path.
305  *
306  *                The leaf protocol also uses this to make a PFS available
307  *                to the cluster (e.g. on-mount).
308  */
309 #define HAMMER2_LNK_PAD         HAMMER2_MSG_LNK(0x000, hammer2_msg_hdr)
310 #define HAMMER2_LNK_PING        HAMMER2_MSG_LNK(0x001, hammer2_msg_hdr)
311 #define HAMMER2_LNK_AUTH        HAMMER2_MSG_LNK(0x010, hammer2_lnk_auth)
312 #define HAMMER2_LNK_HSPAN       HAMMER2_MSG_LNK(0x011, hammer2_lnk_hspan)
313 #define HAMMER2_LNK_SPAN        HAMMER2_MSG_LNK(0x012, hammer2_lnk_span)
314 #define HAMMER2_LNK_ERROR       HAMMER2_MSG_LNK(0xFFF, hammer2_msg_hdr)
315
316 /*
317  * Debug layer ops operate on any link
318  *
319  * SHELL        - Persist stream, access the debug shell on the target
320  *                registration.  Multiple shells can be operational.
321  */
322 #define HAMMER2_DBG_SHELL       HAMMER2_MSG_DBG(0x001, hammer2_dbg_shell)
323
324 struct hammer2_dbg_shell {
325         hammer2_msg_hdr_t       head;
326 };
327 typedef struct hammer2_dbg_shell hammer2_dbg_shell_t;
328
329 /*
330  * Cache layer ops operate on any link, link-0 may be used when the
331  * directly connected target is the desired registration.
332  *
333  * LOCK         - Persist state, blockable, abortable.
334  *
335  *                Obtain cache state (MODIFIED, EXCLUSIVE, SHARED, or INVAL)
336  *                in any of three domains (TREE, INUM, ATTR, DIRENT) for a
337  *                particular key relative to cache state already owned.
338  *
339  *                TREE - Effects entire sub-tree at the specified element
340  *                       and will cause existing cache state owned by
341  *                       other nodes to be adjusted such that the request
342  *                       can be granted.
343  *
344  *                INUM - Only effects inode creation/deletion of an existing
345  *                       element or a new element, by inumber and/or name.
346  *                       typically can be held for very long periods of time
347  *                       (think the vnode cache), directly relates to
348  *                       hammer2_chain structures representing inodes.
349  *
350  *                ATTR - Only effects an inode's attributes, such as
351  *                       ownership, modes, etc.  Used for lookups, chdir,
352  *                       open, etc.  mtime has no affect.
353  *
354  *                DIRENT - Only affects an inode's attributes plus the
355  *                       attributes or names related to any directory entry
356  *                       directly under this inode (non-recursively).  Can
357  *                       be retained for medium periods of time when doing
358  *                       directory scans.
359  *
360  *                This function may block and can be aborted.  You may be
361  *                granted cache state that is more broad than the state you
362  *                requested (e.g. a different set of domains and/or an element
363  *                at a higher layer in the tree).  When quorum operations
364  *                are used you may have to reconcile these grants to the
365  *                lowest common denominator.
366  *
367  *                In order to grant your request either you or the target
368  *                (or both) may have to obtain a quorum agreement.  Deadlock
369  *                resolution may be required.  When doing it yourself you
370  *                will typically maintain an active message to each master
371  *                node in the system.  You can only grant the cache state
372  *                when a quorum of nodes agree.
373  *
374  *                The cache state includes transaction id information which
375  *                can be used to resolve data requests.
376  */
377 #define HAMMER2_CAC_LOCK        HAMMER2_MSG_CAC(0x001, hammer2_cac_lock)
378
379 /*
380  * Quorum layer ops operate on any link, link-0 may be used when the
381  * directly connected target is the desired registration.
382  *
383  * COMMIT       - Persist state, blockable, abortable
384  *
385  *                Issue a COMMIT in two phases.  A quorum must acknowledge
386  *                the operation to proceed to phase-2.  Message-update to
387  *                proceed to phase-2.
388  */
389 #define HAMMER2_QRM_COMMIT      HAMMER2_MSG_QRM(0x001, hammer2_qrm_commit)
390
391 /*
392  * General message errors
393  *
394  *      0x00 - 0x1F     Local iocomm errors
395  *      0x20 - 0x2F     Global errors
396  */
397 #define HAMMER2_MSG_ERR_UNKNOWN         0x20
398
399 union hammer2_any {
400         char                    buf[HAMMER2_MSGHDR_MAX];
401         hammer2_msg_hdr_t       head;
402 };
403
404 typedef union hammer2_any hammer2_any_t;
405
406 #endif