Sage Weil | 5cd068c | 2010-07-07 08:38:17 -0700 | [diff] [blame] | 1 | #ifndef CEPH_CRUSH_CRUSH_H |
| 2 | #define CEPH_CRUSH_CRUSH_H |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 3 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 4 | #ifdef __KERNEL__ |
| 5 | # include <linux/types.h> |
| 6 | #else |
| 7 | # include "crush_compat.h" |
| 8 | #endif |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 9 | |
| 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 Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 26 | #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */ |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 27 | #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 Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 29 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 30 | #define CRUSH_MAX_DEVICE_WEIGHT (100u * 0x10000u) |
| 31 | #define CRUSH_MAX_BUCKET_WEIGHT (65535u * 0x10000u) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 32 | |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 33 | #define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */ |
| 34 | #define CRUSH_ITEM_NONE 0x7fffffff /* no result */ |
Ilya Dryomov | c6d98a6 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 35 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 36 | /* |
| 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 | */ |
| 41 | struct crush_rule_step { |
| 42 | __u32 op; |
| 43 | __s32 arg1; |
| 44 | __s32 arg2; |
| 45 | }; |
| 46 | |
| 47 | /* step op codes */ |
| 48 | enum { |
| 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 Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 55 | CRUSH_RULE_CHOOSELEAF_FIRSTN = 6, |
| 56 | CRUSH_RULE_CHOOSELEAF_INDEP = 7, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 57 | |
Ilya Dryomov | cc10df4 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 58 | CRUSH_RULE_SET_CHOOSE_TRIES = 8, /* override choose_total_tries */ |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 59 | CRUSH_RULE_SET_CHOOSELEAF_TRIES = 9, /* override chooseleaf_descend_once */ |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 60 | CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES = 10, |
| 61 | CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES = 11, |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 62 | CRUSH_RULE_SET_CHOOSELEAF_VARY_R = 12, |
| 63 | CRUSH_RULE_SET_CHOOSELEAF_STABLE = 13 |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * for specifying choose num (arg1) relative to the max parameter |
| 68 | * passed to do_rule |
| 69 | */ |
| 70 | #define CRUSH_CHOOSE_N 0 |
| 71 | #define CRUSH_CHOOSE_N_MINUS(x) (-(x)) |
| 72 | |
| 73 | /* |
| 74 | * The rule mask is used to describe what the rule is intended for. |
| 75 | * Given a ruleset and size of output set, we search through the |
| 76 | * rule list for a matching rule_mask. |
| 77 | */ |
| 78 | struct crush_rule_mask { |
| 79 | __u8 ruleset; |
| 80 | __u8 type; |
| 81 | __u8 min_size; |
| 82 | __u8 max_size; |
| 83 | }; |
| 84 | |
| 85 | struct crush_rule { |
| 86 | __u32 len; |
| 87 | struct crush_rule_mask mask; |
| 88 | struct crush_rule_step steps[0]; |
| 89 | }; |
| 90 | |
| 91 | #define crush_rule_size(len) (sizeof(struct crush_rule) + \ |
| 92 | (len)*sizeof(struct crush_rule_step)) |
| 93 | |
| 94 | |
| 95 | |
| 96 | /* |
| 97 | * A bucket is a named container of other items (either devices or |
| 98 | * other buckets). Items within a bucket are chosen using one of a |
| 99 | * few different algorithms. The table summarizes how the speed of |
| 100 | * each option measures up against mapping stability when items are |
| 101 | * added or removed. |
| 102 | * |
| 103 | * Bucket Alg Speed Additions Removals |
| 104 | * ------------------------------------------------ |
| 105 | * uniform O(1) poor poor |
| 106 | * list O(n) optimal poor |
| 107 | * tree O(log n) good good |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 108 | * straw O(n) better better |
| 109 | * straw2 O(n) optimal optimal |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 110 | */ |
| 111 | enum { |
| 112 | CRUSH_BUCKET_UNIFORM = 1, |
| 113 | CRUSH_BUCKET_LIST = 2, |
| 114 | CRUSH_BUCKET_TREE = 3, |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 115 | CRUSH_BUCKET_STRAW = 4, |
| 116 | CRUSH_BUCKET_STRAW2 = 5, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 117 | }; |
Sage Weil | c6cf726 | 2009-11-06 16:39:26 -0800 | [diff] [blame] | 118 | extern const char *crush_bucket_alg_name(int alg); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 119 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 120 | /* |
| 121 | * although tree was a legacy algorithm, it has been buggy, so |
| 122 | * exclude it. |
| 123 | */ |
| 124 | #define CRUSH_LEGACY_ALLOWED_BUCKET_ALGS ( \ |
| 125 | (1 << CRUSH_BUCKET_UNIFORM) | \ |
| 126 | (1 << CRUSH_BUCKET_LIST) | \ |
| 127 | (1 << CRUSH_BUCKET_STRAW)) |
| 128 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 129 | struct crush_bucket { |
| 130 | __s32 id; /* this'll be negative */ |
| 131 | __u16 type; /* non-zero; type=0 is reserved for devices */ |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 132 | __u8 alg; /* one of CRUSH_BUCKET_* */ |
| 133 | __u8 hash; /* which hash function to use, CRUSH_HASH_* */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 134 | __u32 weight; /* 16-bit fixed point */ |
| 135 | __u32 size; /* num items */ |
| 136 | __s32 *items; |
| 137 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Ilya Dryomov | 069f322 | 2017-06-22 19:44:05 +0200 | [diff] [blame^] | 140 | /** @ingroup API |
| 141 | * |
| 142 | * Replacement weights for each item in a bucket. The size of the |
| 143 | * array must be exactly the size of the straw2 bucket, just as the |
| 144 | * item_weights array. |
| 145 | * |
| 146 | */ |
| 147 | struct crush_weight_set { |
| 148 | __u32 *weights; /*!< 16.16 fixed point weights |
| 149 | in the same order as items */ |
| 150 | __u32 size; /*!< size of the __weights__ array */ |
| 151 | }; |
| 152 | |
| 153 | /** @ingroup API |
| 154 | * |
| 155 | * Replacement weights and ids for a given straw2 bucket, for |
| 156 | * placement purposes. |
| 157 | * |
| 158 | * When crush_do_rule() chooses the Nth item from a straw2 bucket, the |
| 159 | * replacement weights found at __weight_set[N]__ are used instead of |
| 160 | * the weights from __item_weights__. If __N__ is greater than |
| 161 | * __weight_set_size__, the weights found at __weight_set_size-1__ are |
| 162 | * used instead. For instance if __weight_set__ is: |
| 163 | * |
| 164 | * [ [ 0x10000, 0x20000 ], // position 0 |
| 165 | * [ 0x20000, 0x40000 ] ] // position 1 |
| 166 | * |
| 167 | * choosing the 0th item will use position 0 weights [ 0x10000, 0x20000 ] |
| 168 | * choosing the 1th item will use position 1 weights [ 0x20000, 0x40000 ] |
| 169 | * choosing the 2th item will use position 1 weights [ 0x20000, 0x40000 ] |
| 170 | * etc. |
| 171 | * |
| 172 | */ |
| 173 | struct crush_choose_arg { |
| 174 | __s32 *ids; /*!< values to use instead of items */ |
| 175 | __u32 ids_size; /*!< size of the __ids__ array */ |
| 176 | struct crush_weight_set *weight_set; /*!< weight replacements for |
| 177 | a given position */ |
| 178 | __u32 weight_set_size; /*!< size of the __weight_set__ array */ |
| 179 | }; |
| 180 | |
| 181 | /** @ingroup API |
| 182 | * |
| 183 | * Replacement weights and ids for each bucket in the crushmap. The |
| 184 | * __size__ of the __args__ array must be exactly the same as the |
| 185 | * __map->max_buckets__. |
| 186 | * |
| 187 | * The __crush_choose_arg__ at index N will be used when choosing |
| 188 | * an item from the bucket __map->buckets[N]__ bucket, provided it |
| 189 | * is a straw2 bucket. |
| 190 | * |
| 191 | */ |
| 192 | struct crush_choose_arg_map { |
| 193 | struct crush_choose_arg *args; /*!< replacement for each bucket |
| 194 | in the crushmap */ |
| 195 | __u32 size; /*!< size of the __args__ array */ |
| 196 | }; |
| 197 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 198 | struct crush_bucket_uniform { |
| 199 | struct crush_bucket h; |
| 200 | __u32 item_weight; /* 16-bit fixed point; all items equally weighted */ |
| 201 | }; |
| 202 | |
| 203 | struct crush_bucket_list { |
| 204 | struct crush_bucket h; |
| 205 | __u32 *item_weights; /* 16-bit fixed point */ |
| 206 | __u32 *sum_weights; /* 16-bit fixed point. element i is sum |
| 207 | of weights 0..i, inclusive */ |
| 208 | }; |
| 209 | |
| 210 | struct crush_bucket_tree { |
| 211 | struct crush_bucket h; /* note: h.size is _tree_ size, not number of |
| 212 | actual items */ |
| 213 | __u8 num_nodes; |
| 214 | __u32 *node_weights; |
| 215 | }; |
| 216 | |
| 217 | struct crush_bucket_straw { |
| 218 | struct crush_bucket h; |
| 219 | __u32 *item_weights; /* 16-bit fixed point */ |
| 220 | __u32 *straws; /* 16-bit fixed point */ |
| 221 | }; |
| 222 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 223 | struct crush_bucket_straw2 { |
| 224 | struct crush_bucket h; |
| 225 | __u32 *item_weights; /* 16-bit fixed point */ |
| 226 | }; |
| 227 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 228 | |
| 229 | |
| 230 | /* |
| 231 | * CRUSH map includes all buckets, rules, etc. |
| 232 | */ |
| 233 | struct crush_map { |
| 234 | struct crush_bucket **buckets; |
| 235 | struct crush_rule **rules; |
| 236 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 237 | __s32 max_buckets; |
| 238 | __u32 max_rules; |
| 239 | __s32 max_devices; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 240 | |
| 241 | /* choose local retries before re-descent */ |
| 242 | __u32 choose_local_tries; |
| 243 | /* choose local attempts using a fallback permutation before |
| 244 | * re-descent */ |
| 245 | __u32 choose_local_fallback_tries; |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 246 | /* choose attempts before giving up */ |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 247 | __u32 choose_total_tries; |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 248 | /* attempt chooseleaf inner descent once for firstn mode; on |
| 249 | * reject retry outer descent. Note that this does *not* |
| 250 | * apply to a collision: in that case we will retry as we used |
| 251 | * to. */ |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 252 | __u32 chooseleaf_descend_once; |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 253 | |
| 254 | /* if non-zero, feed r into chooseleaf, bit-shifted right by (r-1) |
| 255 | * bits. a value of 1 is best for new clusters. for legacy clusters |
| 256 | * that want to limit reshuffling, a value of 3 or 4 will make the |
| 257 | * mappings line up a bit better with previous mappings. */ |
| 258 | __u8 chooseleaf_vary_r; |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 259 | |
Ilya Dryomov | dc6ae6d | 2016-01-31 14:36:07 +0100 | [diff] [blame] | 260 | /* if true, it makes chooseleaf firstn to return stable results (if |
| 261 | * no local retry) so that data migrations would be optimal when some |
| 262 | * device fails. */ |
| 263 | __u8 chooseleaf_stable; |
| 264 | |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 265 | /* |
| 266 | * This value is calculated after decode or construction by |
| 267 | * the builder. It is exposed here (rather than having a |
| 268 | * 'build CRUSH working space' function) so that callers can |
| 269 | * reserve a static buffer, allocate space on the stack, or |
| 270 | * otherwise avoid calling into the heap allocator if they |
| 271 | * want to. The size of the working space depends on the map, |
| 272 | * while the size of the scratch vector passed to the mapper |
| 273 | * depends on the size of the desired result set. |
| 274 | * |
| 275 | * Nothing stops the caller from allocating both in one swell |
| 276 | * foop and passing in two points, though. |
| 277 | */ |
| 278 | size_t working_size; |
| 279 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 280 | #ifndef __KERNEL__ |
| 281 | /* |
| 282 | * version 0 (original) of straw_calc has various flaws. version 1 |
| 283 | * fixes a few of them. |
| 284 | */ |
| 285 | __u8 straw_calc_version; |
| 286 | |
| 287 | /* |
| 288 | * allowed bucket algs is a bitmask, here the bit positions |
| 289 | * are CRUSH_BUCKET_*. note that these are *bits* and |
| 290 | * CRUSH_BUCKET_* values are not, so we need to or together (1 |
| 291 | * << CRUSH_BUCKET_WHATEVER). The 0th bit is not used to |
| 292 | * minimize confusion (bucket type values start at 1). |
| 293 | */ |
| 294 | __u32 allowed_bucket_algs; |
| 295 | |
| 296 | __u32 *choose_tries; |
| 297 | #endif |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | |
| 301 | /* crush.c */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 302 | extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 303 | extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b); |
| 304 | extern void crush_destroy_bucket_list(struct crush_bucket_list *b); |
| 305 | extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b); |
| 306 | extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b); |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 307 | extern void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 308 | extern void crush_destroy_bucket(struct crush_bucket *b); |
Ilya Dryomov | bfb16d7 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 309 | extern void crush_destroy_rule(struct crush_rule *r); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 310 | extern void crush_destroy(struct crush_map *map); |
| 311 | |
Sage Weil | f671d4c | 2012-05-07 15:36:49 -0700 | [diff] [blame] | 312 | static inline int crush_calc_tree_node(int i) |
| 313 | { |
| 314 | return ((i+1) << 1)-1; |
| 315 | } |
| 316 | |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 317 | /* |
| 318 | * These data structures are private to the CRUSH implementation. They |
| 319 | * are exposed in this header file because builder needs their |
| 320 | * definitions to calculate the total working size. |
| 321 | * |
| 322 | * Moving this out of the crush map allow us to treat the CRUSH map as |
| 323 | * immutable within the mapper and removes the requirement for a CRUSH |
| 324 | * map lock. |
| 325 | */ |
| 326 | struct crush_work_bucket { |
| 327 | __u32 perm_x; /* @x for which *perm is defined */ |
| 328 | __u32 perm_n; /* num elements of *perm that are permuted/defined */ |
| 329 | __u32 *perm; /* Permutation of the bucket's items */ |
| 330 | }; |
| 331 | |
| 332 | struct crush_work { |
| 333 | struct crush_work_bucket **work; /* Per-bucket working store */ |
| 334 | }; |
| 335 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 336 | #endif |