blob: 48b49305716bd728e69477db6b430c34e7781746 [file] [log] [blame]
Sage Weil5cd068c2010-07-07 08:38:17 -07001#ifndef CEPH_CRUSH_CRUSH_H
2#define CEPH_CRUSH_CRUSH_H
Sage Weil5ecc0a02009-10-06 11:31:11 -07003
Ilya Dryomovb459be72015-06-12 13:21:07 +03004#ifdef __KERNEL__
5# include <linux/types.h>
6#else
7# include "crush_compat.h"
8#endif
Sage Weil5ecc0a02009-10-06 11:31:11 -07009
10/*
11 * CRUSH is a pseudo-random data distribution algorithm that
12 * efficiently distributes input values (typically, data objects)
13 * across a heterogeneous, structured storage cluster.
14 *
15 * The algorithm was originally described in detail in this paper
16 * (although the algorithm has evolved somewhat since then):
17 *
18 * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
19 *
20 * LGPL2
21 */
22
23
24#define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */
25
Sage Weil5ecc0a02009-10-06 11:31:11 -070026#define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */
Ilya Dryomovb459be72015-06-12 13:21:07 +030027#define CRUSH_MAX_RULESET (1<<8) /* max crush ruleset number */
28#define CRUSH_MAX_RULES CRUSH_MAX_RULESET /* should be the same as max rulesets */
Sage Weil5ecc0a02009-10-06 11:31:11 -070029
Ilya Dryomovb459be72015-06-12 13:21:07 +030030#define CRUSH_MAX_DEVICE_WEIGHT (100u * 0x10000u)
31#define CRUSH_MAX_BUCKET_WEIGHT (65535u * 0x10000u)
Sage Weil5ecc0a02009-10-06 11:31:11 -070032
Ilya Dryomov9a3b4902013-12-24 21:19:25 +020033#define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */
34#define CRUSH_ITEM_NONE 0x7fffffff /* no result */
Ilya Dryomovc6d98a62013-12-24 21:19:25 +020035
Sage Weil5ecc0a02009-10-06 11:31:11 -070036/*
37 * CRUSH uses user-defined "rules" to describe how inputs should be
38 * mapped to devices. A rule consists of sequence of steps to perform
39 * to generate the set of output devices.
40 */
41struct crush_rule_step {
42 __u32 op;
43 __s32 arg1;
44 __s32 arg2;
45};
46
47/* step op codes */
48enum {
49 CRUSH_RULE_NOOP = 0,
50 CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
51 CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
52 /* arg2 = type */
53 CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
54 CRUSH_RULE_EMIT = 4, /* no args */
Ilya Dryomov917edad2013-12-24 21:19:26 +020055 CRUSH_RULE_CHOOSELEAF_FIRSTN = 6,
56 CRUSH_RULE_CHOOSELEAF_INDEP = 7,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +020057
Ilya Dryomovcc10df42013-12-24 21:19:26 +020058 CRUSH_RULE_SET_CHOOSE_TRIES = 8, /* override choose_total_tries */
Ilya Dryomov917edad2013-12-24 21:19:26 +020059 CRUSH_RULE_SET_CHOOSELEAF_TRIES = 9, /* override chooseleaf_descend_once */
Ilya Dryomovf046bf92013-12-24 21:19:27 +020060 CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES = 10,
61 CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES = 11,
Ilya Dryomovd83ed852014-03-19 16:58:37 +020062 CRUSH_RULE_SET_CHOOSELEAF_VARY_R = 12
Sage Weil5ecc0a02009-10-06 11:31:11 -070063};
64
65/*
66 * for specifying choose num (arg1) relative to the max parameter
67 * passed to do_rule
68 */
69#define CRUSH_CHOOSE_N 0
70#define CRUSH_CHOOSE_N_MINUS(x) (-(x))
71
72/*
73 * The rule mask is used to describe what the rule is intended for.
74 * Given a ruleset and size of output set, we search through the
75 * rule list for a matching rule_mask.
76 */
77struct crush_rule_mask {
78 __u8 ruleset;
79 __u8 type;
80 __u8 min_size;
81 __u8 max_size;
82};
83
84struct crush_rule {
85 __u32 len;
86 struct crush_rule_mask mask;
87 struct crush_rule_step steps[0];
88};
89
90#define crush_rule_size(len) (sizeof(struct crush_rule) + \
91 (len)*sizeof(struct crush_rule_step))
92
93
94
95/*
96 * A bucket is a named container of other items (either devices or
97 * other buckets). Items within a bucket are chosen using one of a
98 * few different algorithms. The table summarizes how the speed of
99 * each option measures up against mapping stability when items are
100 * added or removed.
101 *
102 * Bucket Alg Speed Additions Removals
103 * ------------------------------------------------
104 * uniform O(1) poor poor
105 * list O(n) optimal poor
106 * tree O(log n) good good
Ilya Dryomov958a2762015-04-14 16:54:52 +0300107 * straw O(n) better better
108 * straw2 O(n) optimal optimal
Sage Weil5ecc0a02009-10-06 11:31:11 -0700109 */
110enum {
111 CRUSH_BUCKET_UNIFORM = 1,
112 CRUSH_BUCKET_LIST = 2,
113 CRUSH_BUCKET_TREE = 3,
Ilya Dryomov958a2762015-04-14 16:54:52 +0300114 CRUSH_BUCKET_STRAW = 4,
115 CRUSH_BUCKET_STRAW2 = 5,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700116};
Sage Weilc6cf7262009-11-06 16:39:26 -0800117extern const char *crush_bucket_alg_name(int alg);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700118
Ilya Dryomovb459be72015-06-12 13:21:07 +0300119/*
120 * although tree was a legacy algorithm, it has been buggy, so
121 * exclude it.
122 */
123#define CRUSH_LEGACY_ALLOWED_BUCKET_ALGS ( \
124 (1 << CRUSH_BUCKET_UNIFORM) | \
125 (1 << CRUSH_BUCKET_LIST) | \
126 (1 << CRUSH_BUCKET_STRAW))
127
Sage Weil5ecc0a02009-10-06 11:31:11 -0700128struct crush_bucket {
129 __s32 id; /* this'll be negative */
130 __u16 type; /* non-zero; type=0 is reserved for devices */
Sage Weilfb690392009-11-07 20:18:22 -0800131 __u8 alg; /* one of CRUSH_BUCKET_* */
132 __u8 hash; /* which hash function to use, CRUSH_HASH_* */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700133 __u32 weight; /* 16-bit fixed point */
134 __u32 size; /* num items */
135 __s32 *items;
136
137 /*
138 * cached random permutation: used for uniform bucket and for
139 * the linear search fallback for the other bucket types.
140 */
141 __u32 perm_x; /* @x for which *perm is defined */
142 __u32 perm_n; /* num elements of *perm that are permuted/defined */
143 __u32 *perm;
144};
145
146struct crush_bucket_uniform {
147 struct crush_bucket h;
148 __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
149};
150
151struct crush_bucket_list {
152 struct crush_bucket h;
153 __u32 *item_weights; /* 16-bit fixed point */
154 __u32 *sum_weights; /* 16-bit fixed point. element i is sum
155 of weights 0..i, inclusive */
156};
157
158struct crush_bucket_tree {
159 struct crush_bucket h; /* note: h.size is _tree_ size, not number of
160 actual items */
161 __u8 num_nodes;
162 __u32 *node_weights;
163};
164
165struct crush_bucket_straw {
166 struct crush_bucket h;
167 __u32 *item_weights; /* 16-bit fixed point */
168 __u32 *straws; /* 16-bit fixed point */
169};
170
Ilya Dryomov958a2762015-04-14 16:54:52 +0300171struct crush_bucket_straw2 {
172 struct crush_bucket h;
173 __u32 *item_weights; /* 16-bit fixed point */
174};
175
Sage Weil5ecc0a02009-10-06 11:31:11 -0700176
177
178/*
179 * CRUSH map includes all buckets, rules, etc.
180 */
181struct crush_map {
182 struct crush_bucket **buckets;
183 struct crush_rule **rules;
184
Sage Weil5ecc0a02009-10-06 11:31:11 -0700185 __s32 max_buckets;
186 __u32 max_rules;
187 __s32 max_devices;
Sage Weil546f04e2012-07-30 18:15:23 -0700188
189 /* choose local retries before re-descent */
190 __u32 choose_local_tries;
191 /* choose local attempts using a fallback permutation before
192 * re-descent */
193 __u32 choose_local_fallback_tries;
Ilya Dryomovb459be72015-06-12 13:21:07 +0300194 /* choose attempts before giving up */
Sage Weil546f04e2012-07-30 18:15:23 -0700195 __u32 choose_total_tries;
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200196 /* attempt chooseleaf inner descent once for firstn mode; on
197 * reject retry outer descent. Note that this does *not*
198 * apply to a collision: in that case we will retry as we used
199 * to. */
Jim Schutt1604f482012-11-30 09:15:25 -0700200 __u32 chooseleaf_descend_once;
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200201
202 /* if non-zero, feed r into chooseleaf, bit-shifted right by (r-1)
203 * bits. a value of 1 is best for new clusters. for legacy clusters
204 * that want to limit reshuffling, a value of 3 or 4 will make the
205 * mappings line up a bit better with previous mappings. */
206 __u8 chooseleaf_vary_r;
Ilya Dryomovb459be72015-06-12 13:21:07 +0300207
208#ifndef __KERNEL__
209 /*
210 * version 0 (original) of straw_calc has various flaws. version 1
211 * fixes a few of them.
212 */
213 __u8 straw_calc_version;
214
215 /*
216 * allowed bucket algs is a bitmask, here the bit positions
217 * are CRUSH_BUCKET_*. note that these are *bits* and
218 * CRUSH_BUCKET_* values are not, so we need to or together (1
219 * << CRUSH_BUCKET_WHATEVER). The 0th bit is not used to
220 * minimize confusion (bucket type values start at 1).
221 */
222 __u32 allowed_bucket_algs;
223
224 __u32 *choose_tries;
225#endif
Sage Weil5ecc0a02009-10-06 11:31:11 -0700226};
227
228
229/* crush.c */
Sage Weil8b12d472012-05-07 15:38:35 -0700230extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700231extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
232extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
233extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
234extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
Ilya Dryomov958a2762015-04-14 16:54:52 +0300235extern void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700236extern void crush_destroy_bucket(struct crush_bucket *b);
Ilya Dryomovbfb16d72013-12-24 21:19:24 +0200237extern void crush_destroy_rule(struct crush_rule *r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700238extern void crush_destroy(struct crush_map *map);
239
Sage Weilf671d4c2012-05-07 15:36:49 -0700240static inline int crush_calc_tree_node(int i)
241{
242 return ((i+1) << 1)-1;
243}
244
Sage Weil5ecc0a02009-10-06 11:31:11 -0700245#endif