blob: 4023b1b52296a7546e62fce49f9ef6d102d1d67e [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
4#include <linux/types.h>
5
6/*
7 * CRUSH is a pseudo-random data distribution algorithm that
8 * efficiently distributes input values (typically, data objects)
9 * across a heterogeneous, structured storage cluster.
10 *
11 * The algorithm was originally described in detail in this paper
12 * (although the algorithm has evolved somewhat since then):
13 *
14 * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
15 *
16 * LGPL2
17 */
18
19
20#define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */
21
Sage Weil5ecc0a02009-10-06 11:31:11 -070022#define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */
Sage Weil5ecc0a02009-10-06 11:31:11 -070023
24
Ilya Dryomov9a3b4902013-12-24 21:19:25 +020025#define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */
26#define CRUSH_ITEM_NONE 0x7fffffff /* no result */
Ilya Dryomovc6d98a62013-12-24 21:19:25 +020027
Sage Weil5ecc0a02009-10-06 11:31:11 -070028/*
29 * CRUSH uses user-defined "rules" to describe how inputs should be
30 * mapped to devices. A rule consists of sequence of steps to perform
31 * to generate the set of output devices.
32 */
33struct crush_rule_step {
34 __u32 op;
35 __s32 arg1;
36 __s32 arg2;
37};
38
39/* step op codes */
40enum {
41 CRUSH_RULE_NOOP = 0,
42 CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
43 CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
44 /* arg2 = type */
45 CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
46 CRUSH_RULE_EMIT = 4, /* no args */
47 CRUSH_RULE_CHOOSE_LEAF_FIRSTN = 6,
48 CRUSH_RULE_CHOOSE_LEAF_INDEP = 7,
49};
50
51/*
52 * for specifying choose num (arg1) relative to the max parameter
53 * passed to do_rule
54 */
55#define CRUSH_CHOOSE_N 0
56#define CRUSH_CHOOSE_N_MINUS(x) (-(x))
57
58/*
59 * The rule mask is used to describe what the rule is intended for.
60 * Given a ruleset and size of output set, we search through the
61 * rule list for a matching rule_mask.
62 */
63struct crush_rule_mask {
64 __u8 ruleset;
65 __u8 type;
66 __u8 min_size;
67 __u8 max_size;
68};
69
70struct crush_rule {
71 __u32 len;
72 struct crush_rule_mask mask;
73 struct crush_rule_step steps[0];
74};
75
76#define crush_rule_size(len) (sizeof(struct crush_rule) + \
77 (len)*sizeof(struct crush_rule_step))
78
79
80
81/*
82 * A bucket is a named container of other items (either devices or
83 * other buckets). Items within a bucket are chosen using one of a
84 * few different algorithms. The table summarizes how the speed of
85 * each option measures up against mapping stability when items are
86 * added or removed.
87 *
88 * Bucket Alg Speed Additions Removals
89 * ------------------------------------------------
90 * uniform O(1) poor poor
91 * list O(n) optimal poor
92 * tree O(log n) good good
93 * straw O(n) optimal optimal
94 */
95enum {
96 CRUSH_BUCKET_UNIFORM = 1,
97 CRUSH_BUCKET_LIST = 2,
98 CRUSH_BUCKET_TREE = 3,
99 CRUSH_BUCKET_STRAW = 4
100};
Sage Weilc6cf7262009-11-06 16:39:26 -0800101extern const char *crush_bucket_alg_name(int alg);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700102
103struct crush_bucket {
104 __s32 id; /* this'll be negative */
105 __u16 type; /* non-zero; type=0 is reserved for devices */
Sage Weilfb690392009-11-07 20:18:22 -0800106 __u8 alg; /* one of CRUSH_BUCKET_* */
107 __u8 hash; /* which hash function to use, CRUSH_HASH_* */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700108 __u32 weight; /* 16-bit fixed point */
109 __u32 size; /* num items */
110 __s32 *items;
111
112 /*
113 * cached random permutation: used for uniform bucket and for
114 * the linear search fallback for the other bucket types.
115 */
116 __u32 perm_x; /* @x for which *perm is defined */
117 __u32 perm_n; /* num elements of *perm that are permuted/defined */
118 __u32 *perm;
119};
120
121struct crush_bucket_uniform {
122 struct crush_bucket h;
123 __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
124};
125
126struct crush_bucket_list {
127 struct crush_bucket h;
128 __u32 *item_weights; /* 16-bit fixed point */
129 __u32 *sum_weights; /* 16-bit fixed point. element i is sum
130 of weights 0..i, inclusive */
131};
132
133struct crush_bucket_tree {
134 struct crush_bucket h; /* note: h.size is _tree_ size, not number of
135 actual items */
136 __u8 num_nodes;
137 __u32 *node_weights;
138};
139
140struct crush_bucket_straw {
141 struct crush_bucket h;
142 __u32 *item_weights; /* 16-bit fixed point */
143 __u32 *straws; /* 16-bit fixed point */
144};
145
146
147
148/*
149 * CRUSH map includes all buckets, rules, etc.
150 */
151struct crush_map {
152 struct crush_bucket **buckets;
153 struct crush_rule **rules;
154
Sage Weil5ecc0a02009-10-06 11:31:11 -0700155 __s32 max_buckets;
156 __u32 max_rules;
157 __s32 max_devices;
Sage Weil546f04e2012-07-30 18:15:23 -0700158
159 /* choose local retries before re-descent */
160 __u32 choose_local_tries;
161 /* choose local attempts using a fallback permutation before
162 * re-descent */
163 __u32 choose_local_fallback_tries;
164 /* choose attempts before giving up */
165 __u32 choose_total_tries;
Jim Schutt1604f482012-11-30 09:15:25 -0700166 /* attempt chooseleaf inner descent once; on failure retry outer descent */
167 __u32 chooseleaf_descend_once;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700168};
169
170
171/* crush.c */
Sage Weil8b12d472012-05-07 15:38:35 -0700172extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700173extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
174extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
175extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
176extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
177extern void crush_destroy_bucket(struct crush_bucket *b);
Ilya Dryomovbfb16d72013-12-24 21:19:24 +0200178extern void crush_destroy_rule(struct crush_rule *r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700179extern void crush_destroy(struct crush_map *map);
180
Sage Weilf671d4c2012-05-07 15:36:49 -0700181static inline int crush_calc_tree_node(int i)
182{
183 return ((i+1) << 1)-1;
184}
185
Sage Weil5ecc0a02009-10-06 11:31:11 -0700186#endif