| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008-2012 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <inttypes.h> | ||
| 14 | #include <arpa/inet.h> | ||
| 15 | |||
| 16 | #include <libnftnl/udata.h> | ||
| 17 | |||
| 18 | #include <rule.h> | ||
| 19 | #include <expression.h> | ||
| 20 | #include <gmputil.h> | ||
| 21 | #include <utils.h> | ||
| 22 | |||
| 23 | 722 | static enum byteorder get_key_byteorder(const struct expr *e) | |
| 24 | { | ||
| 25 | 722 | enum datatypes basetype = expr_basetype(e)->type; | |
| 26 | |||
| 27 |
2/3✓ Branch 0 (3→4) taken 714 times.
✓ Branch 1 (3→5) taken 8 times.
✗ Branch 2 (3→6) not taken.
|
722 | switch (basetype) { |
| 28 | 714 | case TYPE_INTEGER: | |
| 29 | /* For ranges, integers MUST be in BYTEORDER_BIG_ENDIAN. | ||
| 30 | * If the LHS (lookup key, e.g. 'meta mark', is host endian, | ||
| 31 | * a byteorder expression is injected to convert the register | ||
| 32 | * content before lookup. | ||
| 33 | */ | ||
| 34 | 714 | return BYTEORDER_BIG_ENDIAN; | |
| 35 | 8 | case TYPE_STRING: | |
| 36 | 8 | return BYTEORDER_HOST_ENDIAN; | |
| 37 | ✗ | default: | |
| 38 | ✗ | break; | |
| 39 | } | ||
| 40 | |||
| 41 | ✗ | return BYTEORDER_INVALID; | |
| 42 | } | ||
| 43 | |||
| 44 | 195281 | static void interval_expr_copy(struct expr *dst, struct expr *src) | |
| 45 | { | ||
| 46 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 195281 times.
|
195281 | if (src->comment) |
| 47 | ✗ | dst->comment = xstrdup(src->comment); | |
| 48 |
2/2✓ Branch 0 (5→6) taken 22 times.
✓ Branch 1 (5→7) taken 195259 times.
|
195281 | if (src->timeout) |
| 49 | 22 | dst->timeout = src->timeout; | |
| 50 |
2/2✓ Branch 0 (7→8) taken 556 times.
✓ Branch 1 (7→9) taken 194725 times.
|
195281 | if (src->expiration) |
| 51 | 556 | dst->expiration = src->expiration; | |
| 52 | |||
| 53 | 195281 | list_splice_init(&src->stmt_list, &dst->stmt_list); | |
| 54 | 195281 | } | |
| 55 | |||
| 56 | 767 | static void set_elem_add(const struct set *set, struct expr *init, mpz_t value, | |
| 57 | uint32_t flags, enum byteorder byteorder) | ||
| 58 | { | ||
| 59 | struct expr *expr; | ||
| 60 | |||
| 61 | 767 | expr = constant_expr_alloc(&internal_location, set->key->dtype, | |
| 62 | 767 | byteorder, set->key->len, NULL); | |
| 63 | 767 | mpz_set(expr->value, value); | |
| 64 | 767 | expr = set_elem_expr_alloc(&internal_location, expr); | |
| 65 | 767 | expr->flags = flags; | |
| 66 | |||
| 67 | 767 | set_expr_add(init, expr); | |
| 68 | 767 | } | |
| 69 | |||
| 70 | 722 | struct expr *get_set_intervals(const struct set *set, const struct expr *init) | |
| 71 | { | ||
| 72 | 722 | enum byteorder byteorder = get_key_byteorder(set->key); | |
| 73 | struct expr *new_init; | ||
| 74 | mpz_t low, high; | ||
| 75 | struct expr *i; | ||
| 76 | |||
| 77 | 722 | mpz_init2(low, set->key->len); | |
| 78 | 722 | mpz_init2(high, set->key->len); | |
| 79 | |||
| 80 | 722 | new_init = set_expr_alloc(&internal_location, NULL); | |
| 81 | |||
| 82 |
4/6✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 722 times.
✗ Branch 2 (27→28) not taken.
✓ Branch 3 (27→29) taken 1454 times.
✓ Branch 4 (29→9) taken 732 times.
✓ Branch 5 (29→30) taken 722 times.
|
1454 | list_for_each_entry(i, &expr_set(init)->expressions, list) { |
| 83 |
2/4✓ Branch 0 (9→10) taken 697 times.
✗ Branch 1 (9→12) not taken.
✗ Branch 2 (9→17) not taken.
✓ Branch 3 (9→20) taken 35 times.
|
732 | switch (i->key->etype) { |
| 84 | 697 | case EXPR_VALUE: | |
| 85 | 697 | set_elem_add(set, new_init, i->key->value, | |
| 86 | 697 | i->flags, byteorder); | |
| 87 | 697 | break; | |
| 88 | ✗ | case EXPR_CONCAT: | |
| 89 | ✗ | set_expr_add(new_init, expr_clone(i)); | |
| 90 | ✗ | i->flags |= EXPR_F_INTERVAL_END; | |
| 91 | ✗ | set_expr_add(new_init, expr_clone(i)); | |
| 92 | ✗ | break; | |
| 93 | ✗ | case EXPR_SET_ELEM_CATCHALL: | |
| 94 | ✗ | set_expr_add(new_init, expr_clone(i)); | |
| 95 | ✗ | break; | |
| 96 | 35 | default: | |
| 97 | 35 | range_expr_value_low(low, i); | |
| 98 | 35 | set_elem_add(set, new_init, low, 0, i->byteorder); | |
| 99 | 35 | range_expr_value_high(high, i); | |
| 100 | 35 | mpz_add_ui(high, high, 1); | |
| 101 | 35 | set_elem_add(set, new_init, high, | |
| 102 | 35 | EXPR_F_INTERVAL_END, i->byteorder); | |
| 103 | 35 | break; | |
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | 722 | mpz_clear(low); | |
| 108 | 722 | mpz_clear(high); | |
| 109 | |||
| 110 | 722 | return new_init; | |
| 111 | } | ||
| 112 | |||
| 113 | 166854409 | static struct expr *expr_value(struct expr *expr) | |
| 114 | { | ||
| 115 |
3/4✓ Branch 0 (2→3) taken 521278 times.
✓ Branch 1 (2→4) taken 166333014 times.
✓ Branch 2 (2→5) taken 117 times.
✗ Branch 3 (2→6) not taken.
|
166854409 | switch (expr->etype) { |
| 116 | 521278 | case EXPR_MAPPING: | |
| 117 | 521278 | return expr->left->key; | |
| 118 | 166333014 | case EXPR_SET_ELEM: | |
| 119 | 166333014 | return expr->key; | |
| 120 | 117 | case EXPR_VALUE: | |
| 121 | 117 | return expr; | |
| 122 | ✗ | default: | |
| 123 | ✗ | BUG("invalid expression type %s", expr_name(expr)); | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | 725 | static struct expr *get_set_interval_find(const struct set *cache_set, | |
| 128 | struct expr *left, | ||
| 129 | struct expr *right) | ||
| 130 | { | ||
| 131 | 725 | const struct set *set = cache_set; | |
| 132 | 725 | struct expr *range = NULL; | |
| 133 | struct expr *i, *key; | ||
| 134 | mpz_t val; | ||
| 135 | |||
| 136 | 725 | mpz_init2(val, set->key->len); | |
| 137 | |||
| 138 |
4/6✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 725 times.
✗ Branch 2 (25→26) not taken.
✓ Branch 3 (25→27) taken 348687 times.
✓ Branch 4 (27→6) taken 348655 times.
✓ Branch 5 (27→28) taken 32 times.
|
348687 | list_for_each_entry(i, &expr_set(set->init)->expressions, list) { |
| 139 | 348655 | key = expr_value(i); | |
| 140 |
2/3✓ Branch 0 (7→8) taken 328300 times.
✓ Branch 1 (7→11) taken 20355 times.
✗ Branch 2 (7→23) not taken.
|
348655 | switch (key->etype) { |
| 141 | 328300 | case EXPR_VALUE: | |
| 142 |
2/2✓ Branch 0 (9→10) taken 327719 times.
✓ Branch 1 (9→11) taken 581 times.
|
328300 | if (expr_basetype(i->key)->type != TYPE_STRING) |
| 143 | 327719 | break; | |
| 144 | /* string type, check if its a range (wildcard). */ | ||
| 145 | /* fall-through */ | ||
| 146 | case EXPR_PREFIX: | ||
| 147 | case EXPR_RANGE: | ||
| 148 | 20936 | range_expr_value_low(val, i); | |
| 149 |
3/4✓ Branch 0 (12→13) taken 20936 times.
✗ Branch 1 (12→16) not taken.
✓ Branch 2 (14→15) taken 20242 times.
✓ Branch 3 (14→16) taken 694 times.
|
20936 | if (left && mpz_cmp(expr_value(left)->value, val)) |
| 150 | 20242 | break; | |
| 151 | |||
| 152 | 694 | range_expr_value_high(val, i); | |
| 153 |
4/4✓ Branch 0 (17→18) taken 29 times.
✓ Branch 1 (17→21) taken 665 times.
✓ Branch 2 (19→20) taken 1 times.
✓ Branch 3 (19→21) taken 28 times.
|
694 | if (right && mpz_cmp(expr_value(right)->value, val)) |
| 154 | 1 | break; | |
| 155 | |||
| 156 | 693 | range = expr_clone(i); | |
| 157 | 693 | goto out; | |
| 158 | ✗ | default: | |
| 159 | ✗ | break; | |
| 160 | } | ||
| 161 | } | ||
| 162 | 32 | out: | |
| 163 | 725 | mpz_clear(val); | |
| 164 | |||
| 165 | 725 | return range; | |
| 166 | } | ||
| 167 | |||
| 168 | − | static struct expr *__expr_to_set_elem(struct expr *low, struct expr *expr) | |
| 169 | { | ||
| 170 | − | struct expr *elem = set_elem_expr_alloc(&low->location, expr); | |
| 171 | |||
| 172 | − | if (low->etype == EXPR_MAPPING) { | |
| 173 | − | interval_expr_copy(elem, low->left); | |
| 174 | |||
| 175 | − | elem = mapping_expr_alloc(&low->location, elem, | |
| 176 | − | expr_clone(low->right)); | |
| 177 | } else { | ||
| 178 | − | interval_expr_copy(elem, low); | |
| 179 | } | ||
| 180 | − | elem->flags |= EXPR_F_KERNEL; | |
| 181 | |||
| 182 | − | return elem; | |
| 183 | } | ||
| 184 | |||
| 185 | 28 | static struct expr *expr_to_set_elem(struct expr *e) | |
| 186 | 28 | { | |
| 187 | 28 | unsigned int len = div_round_up(e->len, BITS_PER_BYTE); | |
| 188 | unsigned int str_len; | ||
| 189 | 28 | char data[len + 1]; | |
| 190 | struct expr *expr; | ||
| 191 | |||
| 192 |
2/2✓ Branch 0 (4→5) taken 21 times.
✓ Branch 1 (4→7) taken 7 times.
|
28 | if (expr_basetype(expr_value(e))->type != TYPE_STRING) |
| 193 | 21 | return expr_clone(e); | |
| 194 | |||
| 195 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 7 times.
|
7 | mpz_export_data(data, expr_value(e)->value, BYTEORDER_BIG_ENDIAN, len); |
| 196 | |||
| 197 | 7 | str_len = strnlen(data, len); | |
| 198 |
2/4✓ Branch 0 (11→12) taken 7 times.
✗ Branch 1 (11→13) not taken.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 7 times.
|
7 | if (str_len >= len || str_len == 0) |
| 199 | ✗ | return expr_clone(e); | |
| 200 | |||
| 201 | 7 | data[str_len] = '*'; | |
| 202 | |||
| 203 | 7 | expr = constant_expr_alloc(&e->location, e->dtype, | |
| 204 | BYTEORDER_HOST_ENDIAN, | ||
| 205 | 7 | (str_len + 1) * BITS_PER_BYTE, data); | |
| 206 | |||
| 207 | 7 | return __expr_to_set_elem(e, expr); | |
| 208 | } | ||
| 209 | |||
| 210 | 721 | static void set_expr_add_splice(struct expr *compound, struct expr *expr, struct expr *orig) | |
| 211 | { | ||
| 212 | struct expr *elem; | ||
| 213 | |||
| 214 |
2/3✓ Branch 0 (2→3) taken 717 times.
✓ Branch 1 (2→6) taken 4 times.
✗ Branch 2 (2→9) not taken.
|
721 | switch (expr->etype) { |
| 215 | 717 | case EXPR_SET_ELEM: | |
| 216 | 717 | list_splice_init(&orig->stmt_list, &expr->stmt_list); | |
| 217 | 717 | set_expr_add(compound, expr); | |
| 218 | 717 | break; | |
| 219 | 4 | case EXPR_MAPPING: | |
| 220 | 4 | list_splice_init(&orig->left->stmt_list, &expr->left->stmt_list); | |
| 221 | 4 | set_expr_add(compound, expr); | |
| 222 | 4 | break; | |
| 223 | ✗ | default: | |
| 224 | ✗ | elem = set_elem_expr_alloc(&orig->location, expr); | |
| 225 | ✗ | list_splice_init(&orig->stmt_list, &elem->stmt_list); | |
| 226 | ✗ | set_expr_add(compound, elem); | |
| 227 | ✗ | break; | |
| 228 | } | ||
| 229 | 721 | } | |
| 230 | |||
| 231 | 715 | int get_set_decompose(struct set *cache_set, struct set *set) | |
| 232 | { | ||
| 233 | struct expr *i, *next, *range; | ||
| 234 | 715 | struct expr *left = NULL; | |
| 235 | struct expr *new_init; | ||
| 236 | |||
| 237 | 715 | new_init = set_expr_alloc(&internal_location, set); | |
| 238 | |||
| 239 |
4/6✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 715 times.
✗ Branch 2 (29→30) not taken.
✓ Branch 3 (29→31) taken 1468 times.
✓ Branch 4 (31→6) taken 757 times.
✓ Branch 5 (31→32) taken 711 times.
|
1468 | list_for_each_entry_safe(i, next, &expr_set(set->init)->expressions, list) { |
| 240 |
3/4✓ Branch 0 (6→7) taken 32 times.
✓ Branch 1 (6→21) taken 725 times.
✓ Branch 2 (7→8) taken 32 times.
✗ Branch 3 (7→21) not taken.
|
757 | if (i->flags & EXPR_F_INTERVAL_END && left) { |
| 241 | 32 | list_del(&left->list); | |
| 242 | 32 | list_del(&i->list); | |
| 243 | 32 | mpz_sub_ui(i->key->value, i->key->value, 1); | |
| 244 | 32 | range = get_set_interval_find(cache_set, left, i); | |
| 245 |
2/2✓ Branch 0 (12→13) taken 4 times.
✓ Branch 1 (12→17) taken 28 times.
|
32 | if (!range) { |
| 246 | 4 | expr_free(left); | |
| 247 | 4 | expr_free(i); | |
| 248 | 4 | expr_free(new_init); | |
| 249 | 4 | errno = ENOENT; | |
| 250 | 4 | return -1; | |
| 251 | } | ||
| 252 | |||
| 253 | 28 | set_expr_add_splice(new_init, range, left); | |
| 254 | |||
| 255 | 28 | expr_free(left); | |
| 256 | 28 | expr_free(i); | |
| 257 | |||
| 258 | 28 | left = NULL; | |
| 259 | } else { | ||
| 260 |
2/2✓ Branch 0 (21→22) taken 9 times.
✓ Branch 1 (21→27) taken 716 times.
|
725 | if (left) { |
| 261 | 9 | range = get_set_interval_find(cache_set, | |
| 262 | left, NULL); | ||
| 263 | |||
| 264 |
1/2✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 9 times.
|
9 | if (range) |
| 265 | ✗ | set_expr_add_splice(new_init, range, left); | |
| 266 | else | ||
| 267 | 9 | set_expr_add_splice(new_init, | |
| 268 | expr_to_set_elem(left), left); | ||
| 269 | } | ||
| 270 | 725 | left = i; | |
| 271 | } | ||
| 272 | } | ||
| 273 |
2/2✓ Branch 0 (32→33) taken 684 times.
✓ Branch 1 (32→38) taken 27 times.
|
711 | if (left) { |
| 274 | 684 | range = get_set_interval_find(cache_set, left, NULL); | |
| 275 |
2/2✓ Branch 0 (34→35) taken 665 times.
✓ Branch 1 (34→36) taken 19 times.
|
684 | if (range) |
| 276 | 665 | set_expr_add_splice(new_init, range, left); | |
| 277 | else | ||
| 278 | 19 | set_expr_add_splice(new_init, expr_to_set_elem(left), left); | |
| 279 | } | ||
| 280 | |||
| 281 | 711 | expr_free(set->init); | |
| 282 | 711 | set->init = new_init; | |
| 283 | |||
| 284 | 711 | return 0; | |
| 285 | } | ||
| 286 | |||
| 287 | 195274 | static bool range_is_prefix(const mpz_t range) | |
| 288 | { | ||
| 289 | mpz_t tmp; | ||
| 290 | bool ret; | ||
| 291 | |||
| 292 | 195274 | mpz_init_set(tmp, range); | |
| 293 | 195274 | mpz_add_ui(tmp, tmp, 1); | |
| 294 | 195274 | mpz_and(tmp, range, tmp); | |
| 295 |
3/4✓ Branch 0 (5→6) taken 195274 times.
✗ Branch 1 (5→8) not taken.
✓ Branch 2 (6→7) taken 151145 times.
✓ Branch 3 (6→8) taken 44129 times.
|
195274 | ret = !mpz_cmp_ui(tmp, 0); |
| 296 | 195274 | mpz_clear(tmp); | |
| 297 | 195274 | return ret; | |
| 298 | } | ||
| 299 | |||
| 300 | 51847645 | static int expr_value_cmp(const void *p1, const void *p2) | |
| 301 | { | ||
| 302 | 51847645 | struct expr *e1 = *(void * const *)p1; | |
| 303 | 51847645 | struct expr *e2 = *(void * const *)p2; | |
| 304 | int ret; | ||
| 305 | |||
| 306 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 51847645 times.
|
51847645 | if (expr_value(e1)->etype == EXPR_CONCAT) |
| 307 | ✗ | return -1; | |
| 308 | |||
| 309 | 51847645 | ret = mpz_cmp(expr_value(e1)->value, expr_value(e2)->value); | |
| 310 |
2/2✓ Branch 0 (7→8) taken 276111 times.
✓ Branch 1 (7→12) taken 51571534 times.
|
51847645 | if (ret == 0) { |
| 311 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 276111 times.
|
276111 | if (e1->flags & EXPR_F_INTERVAL_END) |
| 312 | ✗ | return -1; | |
| 313 |
1/2✓ Branch 0 (10→11) taken 276111 times.
✗ Branch 1 (10→12) not taken.
|
276111 | else if (e2->flags & EXPR_F_INTERVAL_END) |
| 314 | 276111 | return 1; | |
| 315 | } | ||
| 316 | |||
| 317 | 51571534 | return ret; | |
| 318 | } | ||
| 319 | |||
| 320 | /* Given start and end elements of a range, check if it can be represented as | ||
| 321 | * a single netmask, and if so, how long, by returning zero or a positive value. | ||
| 322 | */ | ||
| 323 | 13140 | static int range_mask_len(const mpz_t start, const mpz_t end, unsigned int len) | |
| 324 | { | ||
| 325 | mpz_t tmp_start, tmp_end; | ||
| 326 | int ret; | ||
| 327 | |||
| 328 | 13140 | mpz_init_set(tmp_start, start); | |
| 329 | 13140 | mpz_init_set(tmp_end, end); | |
| 330 | |||
| 331 | 13140 | while (mpz_cmp(tmp_start, tmp_end) <= 0 && | |
| 332 |
5/6✓ Branch 0 (7→8) taken 352195 times.
✗ Branch 1 (7→11) not taken.
✓ Branch 2 (8→9) taken 351011 times.
✓ Branch 3 (8→11) taken 1184 times.
✓ Branch 4 (9→10) taken 339055 times.
✓ Branch 5 (9→11) taken 11956 times.
|
352195 | !mpz_tstbit(tmp_start, 0) && mpz_tstbit(tmp_end, 0) && |
| 333 |
1/2✓ Branch 0 (10→5) taken 339055 times.
✗ Branch 1 (10→11) not taken.
|
339055 | len--) { |
| 334 | 339055 | mpz_fdiv_q_2exp(tmp_start, tmp_start, 1); | |
| 335 | 339055 | mpz_fdiv_q_2exp(tmp_end, tmp_end, 1); | |
| 336 | } | ||
| 337 | |||
| 338 |
2/2✓ Branch 0 (11→12) taken 5897 times.
✓ Branch 1 (11→13) taken 7243 times.
|
13140 | ret = !mpz_cmp(tmp_start, tmp_end) ? (int)len : -1; |
| 339 | |||
| 340 | 13140 | mpz_clear(tmp_start); | |
| 341 | 13140 | mpz_clear(tmp_end); | |
| 342 | |||
| 343 | 13140 | return ret; | |
| 344 | } | ||
| 345 | |||
| 346 | /* Given a set with two elements (start and end), transform them into a | ||
| 347 | * concatenation of ranges. That is, from a list of start expressions and a list | ||
| 348 | * of end expressions, form a list of start - end expressions. | ||
| 349 | */ | ||
| 350 | 1161 | void concat_range_aggregate(struct expr *set) | |
| 351 | { | ||
| 352 | 1161 | struct expr *i, *start = NULL, *end, *r1, *r2, *next, *r1_next, *tmp; | |
| 353 | struct list_head *r2_next; | ||
| 354 | int prefix_len, free_r1; | ||
| 355 | mpz_t range, p; | ||
| 356 | |||
| 357 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1161 times.
✗ Branch 2 (76→77) not taken.
✓ Branch 3 (76→78) taken 15937 times.
✓ Branch 4 (78→5) taken 14776 times.
✓ Branch 5 (78→79) taken 1161 times.
|
15937 | list_for_each_entry_safe(i, next, &expr_set(set)->expressions, list) { |
| 358 |
2/2✓ Branch 0 (5→6) taken 7389 times.
✓ Branch 1 (5→7) taken 7387 times.
|
14776 | if (!start) { |
| 359 | 7389 | start = i; | |
| 360 | 7389 | continue; | |
| 361 | } | ||
| 362 | 7387 | end = i; | |
| 363 | |||
| 364 | /* Walk over r1 (start expression) and r2 (end) in parallel, | ||
| 365 | * form ranges between corresponding r1 and r2 expressions, | ||
| 366 | * store them by replacing r2 expressions, and free r1 | ||
| 367 | * expressions. | ||
| 368 | */ | ||
| 369 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 7387 times.
|
7387 | r2 = list_first_entry(&expr_concat(expr_value(end))->expressions, |
| 370 | struct expr, list); | ||
| 371 |
4/6✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 7387 times.
✗ Branch 2 (68→69) not taken.
✓ Branch 3 (68→70) taken 24181 times.
✓ Branch 4 (71→16) taken 16794 times.
✓ Branch 5 (71→72) taken 7387 times.
|
24181 | list_for_each_entry_safe(r1, r1_next, |
| 372 | &expr_concat(expr_value(start))->expressions, | ||
| 373 | list) { | ||
| 374 | 16794 | bool string_type = false; | |
| 375 | |||
| 376 | 16794 | mpz_init(range); | |
| 377 | 16794 | mpz_init(p); | |
| 378 | |||
| 379 | 16794 | r2_next = r2->list.next; | |
| 380 | 16794 | free_r1 = 0; | |
| 381 | |||
| 382 |
2/2✓ Branch 0 (18→19) taken 3654 times.
✓ Branch 1 (18→20) taken 13140 times.
|
16794 | if (!mpz_cmp(r1->value, r2->value)) { |
| 383 | 3654 | free_r1 = 1; | |
| 384 | 3654 | goto next; | |
| 385 | } | ||
| 386 | |||
| 387 |
2/2✓ Branch 0 (21→22) taken 74 times.
✓ Branch 1 (21→30) taken 13066 times.
|
13140 | if (expr_basetype(r1)->type == TYPE_STRING && |
| 388 |
1/2✓ Branch 0 (23→24) taken 74 times.
✗ Branch 1 (23→30) not taken.
|
74 | expr_basetype(r2)->type == TYPE_STRING) { |
| 389 | 74 | string_type = true; | |
| 390 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 74 times.
|
74 | mpz_switch_byteorder(r1->value, r1->len / BITS_PER_BYTE); |
| 391 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→29) taken 74 times.
|
74 | mpz_switch_byteorder(r2->value, r2->len / BITS_PER_BYTE); |
| 392 | } | ||
| 393 | |||
| 394 | 13140 | mpz_sub(range, r2->value, r1->value); | |
| 395 | 13140 | mpz_sub_ui(range, range, 1); | |
| 396 | 13140 | mpz_and(p, r1->value, range); | |
| 397 | |||
| 398 | /* Check if we are forced, or if it's anyway preferable, | ||
| 399 | * to express the range as a wildcard string, or two points | ||
| 400 | * instead of a netmask. | ||
| 401 | */ | ||
| 402 | 13140 | prefix_len = range_mask_len(r1->value, r2->value, | |
| 403 | r1->len); | ||
| 404 |
2/2✓ Branch 0 (34→35) taken 74 times.
✓ Branch 1 (34→41) taken 13066 times.
|
13140 | if (string_type) { |
| 405 |
1/2✗ Branch 0 (35→36) not taken.
✓ Branch 1 (35→37) taken 74 times.
|
74 | mpz_switch_byteorder(r1->value, r1->len / BITS_PER_BYTE); |
| 406 |
1/2✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 74 times.
|
74 | mpz_switch_byteorder(r2->value, r2->len / BITS_PER_BYTE); |
| 407 | } | ||
| 408 | |||
| 409 |
2/2✓ Branch 0 (41→42) taken 5897 times.
✓ Branch 1 (41→51) taken 7243 times.
|
13140 | if (prefix_len >= 0 && |
| 410 |
4/4✓ Branch 0 (42→43) taken 1086 times.
✓ Branch 1 (42→51) taken 4811 times.
✓ Branch 2 (43→44) taken 74 times.
✓ Branch 3 (43→51) taken 1012 times.
|
5897 | (prefix_len % BITS_PER_BYTE) == 0 && |
| 411 | 74 | string_type) { | |
| 412 | 74 | unsigned int str_len = prefix_len / BITS_PER_BYTE; | |
| 413 | 74 | char data[str_len + 2]; | |
| 414 | |||
| 415 |
1/2✗ Branch 0 (44→45) not taken.
✓ Branch 1 (44→46) taken 74 times.
|
74 | mpz_export_data(data, r1->value, BYTEORDER_HOST_ENDIAN, str_len); |
| 416 | 74 | data[str_len] = '*'; | |
| 417 | |||
| 418 | 74 | tmp = constant_expr_alloc(&r1->location, r1->dtype, | |
| 419 | BYTEORDER_HOST_ENDIAN, | ||
| 420 | 74 | (str_len + 1) * BITS_PER_BYTE, data); | |
| 421 | 74 | tmp->len = r2->len; | |
| 422 | 74 | list_replace(&r2->list, &tmp->list); | |
| 423 | 74 | r2_next = tmp->list.next; | |
| 424 | 74 | expr_free(r2); | |
| 425 | 74 | free_r1 = 1; | |
| 426 | 74 | goto next; | |
| 427 | } | ||
| 428 | |||
| 429 |
2/2✓ Branch 0 (51→52) taken 5823 times.
✓ Branch 1 (51→54) taken 7243 times.
|
13066 | if (prefix_len < 0 || |
| 430 |
2/2✓ Branch 0 (53→54) taken 137 times.
✓ Branch 1 (53→57) taken 5686 times.
|
5823 | !datatype_prefix_notation(r1->dtype)) { |
| 431 | 7380 | tmp = range_expr_alloc(&r1->location, r1, | |
| 432 | r2); | ||
| 433 | |||
| 434 | 7380 | list_replace(&r2->list, &tmp->list); | |
| 435 | 7380 | r2_next = tmp->list.next; | |
| 436 | } else { | ||
| 437 | 5686 | tmp = prefix_expr_alloc(&r1->location, r1, | |
| 438 | prefix_len); | ||
| 439 | 5686 | tmp->len = r2->len; | |
| 440 | |||
| 441 | 5686 | list_replace(&r2->list, &tmp->list); | |
| 442 | 5686 | r2_next = tmp->list.next; | |
| 443 | 5686 | expr_free(r2); | |
| 444 | } | ||
| 445 | |||
| 446 | 16794 | next: | |
| 447 | 16794 | mpz_clear(p); | |
| 448 | 16794 | mpz_clear(range); | |
| 449 | |||
| 450 | 16794 | r2 = list_entry(r2_next, typeof(*r2), list); | |
| 451 | 16794 | concat_expr_remove(expr_value(start), r1); | |
| 452 | |||
| 453 |
2/2✓ Branch 0 (64→65) taken 3728 times.
✓ Branch 1 (64→66) taken 13066 times.
|
16794 | if (free_r1) |
| 454 | 3728 | expr_free(r1); | |
| 455 | } | ||
| 456 | |||
| 457 | 7387 | set_expr_remove(set, start); | |
| 458 | 7387 | expr_free(start); | |
| 459 | 7387 | start = NULL; | |
| 460 | } | ||
| 461 | 1161 | } | |
| 462 | |||
| 463 | 150096 | static struct expr *interval_to_prefix(struct expr *low, struct expr *i, const mpz_t range) | |
| 464 | { | ||
| 465 | unsigned int prefix_len; | ||
| 466 | struct expr *prefix; | ||
| 467 | |||
| 468 | 150096 | prefix_len = expr_value(i)->len - mpz_scan0(range, 0); | |
| 469 | 150096 | prefix = prefix_expr_alloc(&low->location, | |
| 470 | 150096 | expr_clone(expr_value(low)), | |
| 471 | prefix_len); | ||
| 472 | 150096 | prefix->len = expr_value(i)->len; | |
| 473 | |||
| 474 | 150096 | return __expr_to_set_elem(low, prefix); | |
| 475 | } | ||
| 476 | |||
| 477 | 362 | static struct expr *interval_to_string(struct expr *low, struct expr *i, const mpz_t range) | |
| 478 | 362 | { | |
| 479 | 362 | unsigned int len = div_round_up(i->len, BITS_PER_BYTE); | |
| 480 | unsigned int prefix_len, str_len; | ||
| 481 | 362 | char data[len + 2]; | |
| 482 | struct expr *expr; | ||
| 483 | |||
| 484 | 362 | prefix_len = expr_value(i)->len - mpz_scan0(range, 0); | |
| 485 | |||
| 486 |
2/4✓ Branch 0 (3→4) taken 362 times.
✗ Branch 1 (3→5) not taken.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→7) taken 362 times.
|
362 | if (prefix_len > i->len || prefix_len % BITS_PER_BYTE) |
| 487 | ✗ | return interval_to_prefix(low, i, range); | |
| 488 | |||
| 489 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 362 times.
|
362 | mpz_export_data(data, expr_value(low)->value, BYTEORDER_BIG_ENDIAN, len); |
| 490 | |||
| 491 | 362 | str_len = strnlen(data, len); | |
| 492 |
2/4✓ Branch 0 (11→12) taken 362 times.
✗ Branch 1 (11→13) not taken.
✗ Branch 2 (12→13) not taken.
✓ Branch 3 (12→15) taken 362 times.
|
362 | if (str_len >= len || str_len == 0) |
| 493 | ✗ | return interval_to_prefix(low, i, range); | |
| 494 | |||
| 495 | 362 | data[str_len] = '*'; | |
| 496 | |||
| 497 | 362 | expr = constant_expr_alloc(&low->location, low->dtype, | |
| 498 | BYTEORDER_HOST_ENDIAN, | ||
| 499 | len * BITS_PER_BYTE, data); | ||
| 500 | |||
| 501 | 362 | return __expr_to_set_elem(low, expr); | |
| 502 | } | ||
| 503 | |||
| 504 | 44816 | static struct expr *interval_to_range(struct expr *low, struct expr *i, mpz_t range) | |
| 505 | { | ||
| 506 | struct expr *tmp; | ||
| 507 | |||
| 508 | 44816 | tmp = constant_expr_alloc(&low->location, low->dtype, | |
| 509 | 44816 | low->byteorder, expr_value(low)->len, | |
| 510 | NULL); | ||
| 511 | |||
| 512 | 44816 | mpz_add(range, range, expr_value(low)->value); | |
| 513 | 44816 | mpz_set(tmp->value, range); | |
| 514 | |||
| 515 | 44816 | tmp = range_expr_alloc(&low->location, | |
| 516 | 44816 | expr_clone(expr_value(low)), | |
| 517 | tmp); | ||
| 518 | |||
| 519 | 44816 | return __expr_to_set_elem(low, tmp); | |
| 520 | } | ||
| 521 | |||
| 522 | static void | ||
| 523 | 3407548 | add_interval(struct expr *set, struct expr *low, struct expr *i) | |
| 524 | { | ||
| 525 | struct expr *expr; | ||
| 526 | mpz_t range, p; | ||
| 527 | |||
| 528 | 3407548 | mpz_init(range); | |
| 529 | 3407548 | mpz_init(p); | |
| 530 | |||
| 531 | 3407548 | mpz_sub(range, expr_value(i)->value, expr_value(low)->value); | |
| 532 |
2/2✓ Branch 0 (7→8) taken 3407487 times.
✓ Branch 1 (7→9) taken 61 times.
|
3407548 | if (i->etype != EXPR_VALUE) |
| 533 | 3407487 | mpz_sub_ui(range, range, 1); | |
| 534 | |||
| 535 | 3407548 | mpz_and(p, expr_value(low)->value, range); | |
| 536 | |||
| 537 |
3/4✓ Branch 0 (11→12) taken 3407548 times.
✗ Branch 1 (11→22) not taken.
✓ Branch 2 (12→13) taken 3212274 times.
✓ Branch 3 (12→22) taken 195274 times.
|
3407548 | if (!mpz_cmp_ui(range, 0)) { |
| 538 |
2/2✓ Branch 0 (14→15) taken 12959 times.
✓ Branch 1 (14→21) taken 3199315 times.
|
3212274 | if (expr_basetype(low)->type == TYPE_STRING) |
| 539 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 12959 times.
|
12959 | mpz_switch_byteorder(expr_value(low)->value, |
| 540 | expr_value(low)->len / BITS_PER_BYTE); | ||
| 541 | 3212274 | low->flags |= EXPR_F_KERNEL; | |
| 542 | 3212274 | expr = expr_get(low); | |
| 543 |
5/6✓ Branch 0 (23→24) taken 151145 times.
✓ Branch 1 (23→34) taken 44129 times.
✓ Branch 2 (24→25) taken 151145 times.
✗ Branch 3 (24→34) not taken.
✓ Branch 4 (25→26) taken 150480 times.
✓ Branch 5 (25→34) taken 665 times.
|
195274 | } else if (range_is_prefix(range) && !mpz_cmp_ui(p, 0)) { |
| 544 | |||
| 545 |
2/2✓ Branch 0 (27→28) taken 150096 times.
✓ Branch 1 (27→29) taken 384 times.
|
300960 | if (datatype_prefix_notation(i->dtype)) |
| 546 | 150096 | expr = interval_to_prefix(low, i, range); | |
| 547 |
2/2✓ Branch 0 (30→31) taken 362 times.
✓ Branch 1 (30→32) taken 22 times.
|
384 | else if (expr_basetype(i)->type == TYPE_STRING) |
| 548 | 362 | expr = interval_to_string(low, i, range); | |
| 549 | else | ||
| 550 | 22 | expr = interval_to_range(low, i, range); | |
| 551 | } else | ||
| 552 | 44794 | expr = interval_to_range(low, i, range); | |
| 553 | |||
| 554 | 3407548 | set_expr_add(set, expr); | |
| 555 | |||
| 556 | 3407548 | mpz_clear(range); | |
| 557 | 3407548 | mpz_clear(p); | |
| 558 | 3407548 | } | |
| 559 | |||
| 560 | 4139 | void interval_map_decompose(struct expr *set) | |
| 561 | { | ||
| 562 | 4139 | struct expr *i, *next, *low = NULL, *end, *catchall = NULL, *key; | |
| 563 | struct expr **elements, **ranges; | ||
| 564 | unsigned int n, m, size; | ||
| 565 | bool interval; | ||
| 566 | |||
| 567 |
3/4✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4139 times.
✓ Branch 2 (4→5) taken 125 times.
✓ Branch 3 (4→6) taken 4014 times.
|
4139 | if (expr_set(set)->size == 0) |
| 568 | 125 | return; | |
| 569 | |||
| 570 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 4014 times.
|
4014 | elements = xmalloc_array(expr_set(set)->size, sizeof(struct expr *)); |
| 571 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 4014 times.
|
4014 | ranges = xmalloc_array(expr_set(set)->size * 2, sizeof(struct expr *)); |
| 572 | |||
| 573 | /* Sort elements */ | ||
| 574 | 4014 | n = 0; | |
| 575 |
4/6✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 4014 times.
✗ Branch 2 (26→27) not taken.
✓ Branch 3 (26→28) taken 6822729 times.
✓ Branch 4 (28→15) taken 6818715 times.
✓ Branch 5 (28→29) taken 4014 times.
|
6822729 | list_for_each_entry_safe(i, next, &expr_set(set)->expressions, list) { |
| 576 | 6818715 | key = NULL; | |
| 577 |
2/2✓ Branch 0 (15→16) taken 6796014 times.
✓ Branch 1 (15→17) taken 22701 times.
|
6818715 | if (i->etype == EXPR_SET_ELEM) |
| 578 | 6796014 | key = i->key; | |
| 579 |
1/2✓ Branch 0 (17→18) taken 22701 times.
✗ Branch 1 (17→19) not taken.
|
22701 | else if (i->etype == EXPR_MAPPING) |
| 580 | 22701 | key = i->left->key; | |
| 581 | |||
| 582 |
3/4✓ Branch 0 (19→20) taken 6818715 times.
✗ Branch 1 (19→23) not taken.
✓ Branch 2 (20→21) taken 17 times.
✓ Branch 3 (20→23) taken 6818698 times.
|
6818715 | if (key && expr_type_catchall(key)) { |
| 583 | 17 | list_del(&i->list); | |
| 584 | 17 | catchall = i; | |
| 585 | 17 | continue; | |
| 586 | } | ||
| 587 | 6818698 | set_expr_remove(set, i); | |
| 588 | 6818698 | elements[n++] = i; | |
| 589 | } | ||
| 590 | 4014 | qsort(elements, n, sizeof(elements[0]), expr_value_cmp); | |
| 591 | 4014 | size = n; | |
| 592 | |||
| 593 | /* Transform points (single values) into half-closed intervals */ | ||
| 594 | 4014 | n = 0; | |
| 595 | 4014 | interval = false; | |
| 596 |
2/2✓ Branch 0 (38→31) taken 6818698 times.
✓ Branch 1 (38→39) taken 4014 times.
|
6822712 | for (m = 0; m < size; m++) { |
| 597 | 6818698 | i = elements[m]; | |
| 598 | |||
| 599 |
2/2✓ Branch 0 (31→32) taken 3411144 times.
✓ Branch 1 (31→33) taken 3407554 times.
|
6818698 | if (i->flags & EXPR_F_INTERVAL_END) |
| 600 | 3411144 | interval = false; | |
| 601 |
2/2✓ Branch 0 (33→34) taken 38 times.
✓ Branch 1 (33→36) taken 3407516 times.
|
3407554 | else if (interval) { |
| 602 | 38 | end = expr_clone(i); | |
| 603 | 38 | end->flags |= EXPR_F_INTERVAL_END; | |
| 604 | 38 | ranges[n++] = end; | |
| 605 | } else | ||
| 606 | 3407516 | interval = true; | |
| 607 | |||
| 608 | 6818698 | ranges[n++] = i; | |
| 609 | } | ||
| 610 | 4014 | size = n; | |
| 611 | |||
| 612 |
2/2✓ Branch 0 (51→40) taken 6818736 times.
✓ Branch 1 (51→52) taken 4014 times.
|
6822750 | for (n = 0; n < size; n++) { |
| 613 | 6818736 | i = ranges[n]; | |
| 614 | |||
| 615 |
2/2✓ Branch 0 (40→41) taken 3411249 times.
✓ Branch 1 (40→45) taken 3407487 times.
|
6818736 | if (low == NULL) { |
| 616 |
2/2✓ Branch 0 (41→42) taken 3695 times.
✓ Branch 1 (41→44) taken 3407554 times.
|
3411249 | if (i->flags & EXPR_F_INTERVAL_END) { |
| 617 | /* | ||
| 618 | * End of interval mark | ||
| 619 | */ | ||
| 620 | 3695 | expr_free(i); | |
| 621 | 3695 | continue; | |
| 622 | } else { | ||
| 623 | /* | ||
| 624 | * Start a new interval | ||
| 625 | */ | ||
| 626 | 3407554 | low = i; | |
| 627 | 3407554 | continue; | |
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | 3407487 | add_interval(set, low, i); | |
| 632 | |||
| 633 |
1/2✓ Branch 0 (46→47) taken 3407487 times.
✗ Branch 1 (46→49) not taken.
|
3407487 | if (i->flags & EXPR_F_INTERVAL_END) { |
| 634 | 3407487 | expr_free(low); | |
| 635 | 3407487 | low = NULL; | |
| 636 | } | ||
| 637 | 3407487 | expr_free(i); | |
| 638 | } | ||
| 639 | |||
| 640 |
2/2✓ Branch 0 (52→53) taken 3947 times.
✓ Branch 1 (52→54) taken 67 times.
|
4014 | if (!low) /* no unclosed interval at end */ |
| 641 | 3947 | goto out; | |
| 642 | |||
| 643 | 67 | i = constant_expr_alloc(&low->location, low->dtype, | |
| 644 | 67 | low->byteorder, expr_value(low)->len, NULL); | |
| 645 | 67 | mpz_bitmask(i->value, i->len); | |
| 646 | |||
| 647 |
2/2✓ Branch 0 (58→59) taken 6 times.
✓ Branch 1 (58→60) taken 61 times.
|
67 | if (!mpz_cmp(i->value, expr_value(low)->value)) { |
| 648 | 6 | set_expr_add(set, low); | |
| 649 | } else { | ||
| 650 | 61 | add_interval(set, low, i); | |
| 651 | 61 | expr_free(low); | |
| 652 | } | ||
| 653 | |||
| 654 | 67 | expr_free(i); | |
| 655 | |||
| 656 | 4014 | out: | |
| 657 |
2/2✓ Branch 0 (63→64) taken 17 times.
✓ Branch 1 (63→65) taken 3997 times.
|
4014 | if (catchall) { |
| 658 | 17 | catchall->flags |= EXPR_F_KERNEL; | |
| 659 | 17 | set_expr_add(set, catchall); | |
| 660 | } | ||
| 661 | |||
| 662 | 4014 | free(ranges); | |
| 663 | 4014 | free(elements); | |
| 664 | } | ||
| 665 |