GCC Code Coverage Report


Directory: ./
File: src/optimize.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 598 644 92.9%
Functions: 36 36 100.0%
Branches: 323 387 83.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Pablo Neira Ayuso <pablo@netfilter.org>
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 (or any
6 * later) as published by the Free Software Foundation.
7 */
8
9 /* Funded through the NGI0 PET Fund established by NLnet (https://nlnet.nl)
10 * with support from the European Commission's Next Generation Internet
11 * programme.
12 */
13
14 #include <nft.h>
15
16 #include <errno.h>
17 #include <inttypes.h>
18 #include <nftables.h>
19 #include <parser.h>
20 #include <expression.h>
21 #include <statement.h>
22 #include <utils.h>
23 #include <erec.h>
24 #include <linux/netfilter.h>
25
26 #define MAX_STMTS 32
27
28 struct optimize_ctx {
29 struct stmt *stmt[MAX_STMTS];
30 uint32_t num_stmts;
31
32 struct stmt ***stmt_matrix;
33 struct rule **rule;
34 uint32_t num_rules;
35 };
36
37 − static bool __expr_cmp(const struct expr *expr_a, const struct expr *expr_b)
38 {
39 − if (expr_a->etype != expr_b->etype)
40 − return false;
41 − if (expr_a->len != expr_b->len)
42 − return false;
43
44 − switch (expr_a->etype) {
45 − case EXPR_PAYLOAD:
46 − if (expr_a->payload.base != expr_b->payload.base)
47 − return false;
48 − if (expr_a->payload.offset != expr_b->payload.offset)
49 − return false;
50 − if (expr_a->payload.desc != expr_b->payload.desc)
51 − return false;
52 − if (expr_a->payload.inner_desc != expr_b->payload.inner_desc)
53 − return false;
54 − if (expr_a->payload.tmpl != expr_b->payload.tmpl)
55 − return false;
56 − break;
57 − case EXPR_EXTHDR:
58 − if (expr_a->exthdr.desc != expr_b->exthdr.desc)
59 − return false;
60 − if (expr_a->exthdr.tmpl != expr_b->exthdr.tmpl)
61 − return false;
62 − break;
63 − case EXPR_META:
64 − if (expr_a->meta.key != expr_b->meta.key)
65 − return false;
66 − if (expr_a->meta.base != expr_b->meta.base)
67 − return false;
68 − if (expr_a->meta.inner_desc != expr_b->meta.inner_desc)
69 − return false;
70 − break;
71 − case EXPR_CT:
72 − if (expr_a->ct.key != expr_b->ct.key)
73 − return false;
74 − if (expr_a->ct.base != expr_b->ct.base)
75 − return false;
76 − if (expr_a->ct.direction != expr_b->ct.direction)
77 − return false;
78 − if (expr_a->ct.nfproto != expr_b->ct.nfproto)
79 − return false;
80 − break;
81 − case EXPR_RT:
82 − if (expr_a->rt.key != expr_b->rt.key)
83 − return false;
84 − break;
85 − case EXPR_SOCKET:
86 − if (expr_a->socket.key != expr_b->socket.key)
87 − return false;
88 − if (expr_a->socket.level != expr_b->socket.level)
89 − return false;
90 − break;
91 − case EXPR_OSF:
92 − if (expr_a->osf.ttl != expr_b->osf.ttl)
93 − return false;
94 − if (expr_a->osf.flags != expr_b->osf.flags)
95 − return false;
96 − break;
97 − case EXPR_XFRM:
98 − if (expr_a->xfrm.key != expr_b->xfrm.key)
99 − return false;
100 − if (expr_a->xfrm.direction != expr_b->xfrm.direction)
101 − return false;
102 − break;
103 − case EXPR_FIB:
104 − if (expr_a->fib.flags != expr_b->fib.flags)
105 − return false;
106 − if (expr_a->fib.result != expr_b->fib.result)
107 − return false;
108 − break;
109 − case EXPR_NUMGEN:
110 − if (expr_a->numgen.type != expr_b->numgen.type)
111 − return false;
112 − if (expr_a->numgen.mod != expr_b->numgen.mod)
113 − return false;
114 − if (expr_a->numgen.offset != expr_b->numgen.offset)
115 − return false;
116 − break;
117 − case EXPR_HASH:
118 − if (expr_a->hash.mod != expr_b->hash.mod)
119 − return false;
120 − if (expr_a->hash.seed_set != expr_b->hash.seed_set)
121 − return false;
122 − if (expr_a->hash.seed != expr_b->hash.seed)
123 − return false;
124 − if (expr_a->hash.offset != expr_b->hash.offset)
125 − return false;
126 − if (expr_a->hash.type != expr_b->hash.type)
127 − return false;
128 − break;
129 − case EXPR_BINOP:
130 − if (!__expr_cmp(expr_a->left, expr_b->left))
131 − return false;
132
133 − return __expr_cmp(expr_a->right, expr_b->right);
134 − case EXPR_SYMBOL:
135 − if (expr_a->symtype != expr_b->symtype)
136 − return false;
137 − if (expr_a->symtype != SYMBOL_VALUE)
138 − return false;
139
140 − return !strcmp(expr_a->identifier, expr_b->identifier);
141 − case EXPR_VALUE:
142 − return !mpz_cmp(expr_a->value, expr_b->value);
143 − default:
144 − return false;
145 }
146
147 − return true;
148 }
149
150 52 static bool is_bitmask(const struct expr *expr)
151 {
152
2/3
✓ Branch 0 (2→3) taken 20 times.
✓ Branch 1 (2→8) taken 32 times.
✗ Branch 2 (2→9) not taken.
52 switch (expr->etype) {
153 20 case EXPR_BINOP:
154
1/2
✓ Branch 0 (3→4) taken 20 times.
✗ Branch 1 (3→7) not taken.
20 if (expr->op == OP_OR &&
155
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 20 times.
20 !is_bitmask(expr->left))
156 ✗ return false;
157
158 20 return is_bitmask(expr->right);
159 32 case EXPR_VALUE:
160 case EXPR_SYMBOL:
161 32 return true;
162 ✗ default:
163 ✗ break;
164 }
165
166 ✗ return false;
167 }
168
169 272 static bool stmt_expr_supported(const struct expr *expr)
170 {
171
3/3
✓ Branch 0 (2→3) taken 248 times.
✓ Branch 1 (2→4) taken 12 times.
✓ Branch 2 (2→8) taken 12 times.
272 switch (expr->right->etype) {
172 248 case EXPR_SYMBOL:
173 case EXPR_RANGE_SYMBOL:
174 case EXPR_RANGE:
175 case EXPR_RANGE_VALUE:
176 case EXPR_PREFIX:
177 case EXPR_SET:
178 case EXPR_LIST:
179 case EXPR_VALUE:
180 248 return true;
181 12 case EXPR_BINOP:
182
1/2
✓ Branch 0 (5→6) taken 12 times.
✗ Branch 1 (5→7) not taken.
12 if (is_bitmask(expr->right))
183 12 return true;
184 ✗ break;
185 12 default:
186 12 break;
187 }
188
189 12 return false;
190 }
191
192 258 static bool expr_symbol_set(const struct expr *expr)
193 {
194
2/2
✓ Branch 0 (2→3) taken 186 times.
✓ Branch 1 (2→5) taken 72 times.
444 return expr->right->etype == EXPR_SYMBOL &&
195
2/2
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→5) taken 182 times.
186 expr->right->symtype == SYMBOL_SET;
196 }
197
198 − static bool __stmt_type_eq(const struct stmt *stmt_a, const struct stmt *stmt_b,
199 bool fully_compare)
200 {
201 struct expr *expr_a, *expr_b;
202
203 − if (stmt_a->type != stmt_b->type)
204 − return false;
205
206 − switch (stmt_a->type) {
207 − case STMT_EXPRESSION:
208 − expr_a = stmt_a->expr;
209 − expr_b = stmt_b->expr;
210
211 − if (expr_a->op != expr_b->op)
212 − return false;
213 − if (expr_a->op != OP_IMPLICIT && expr_a->op != OP_EQ)
214 − return false;
215
216 − if (fully_compare) {
217 − if (!stmt_expr_supported(expr_a) ||
218 − !stmt_expr_supported(expr_b))
219 − return false;
220
221 − if (expr_symbol_set(expr_a) ||
222 − expr_symbol_set(expr_b))
223 − return false;
224 }
225
226 − return __expr_cmp(expr_a->left, expr_b->left);
227 − case STMT_COUNTER:
228 case STMT_NOTRACK:
229 − break;
230 − case STMT_VERDICT:
231 − if (!fully_compare)
232 − break;
233
234 − expr_a = stmt_a->expr;
235 − expr_b = stmt_b->expr;
236
237 − if (expr_a->etype != expr_b->etype)
238 − return false;
239
240 − if (expr_a->etype == EXPR_MAP &&
241 − expr_b->etype == EXPR_MAP)
242 − return __expr_cmp(expr_a->map, expr_b->map);
243 − break;
244 − case STMT_LOG:
245 − if (stmt_a->log.snaplen != stmt_b->log.snaplen ||
246 − stmt_a->log.group != stmt_b->log.group ||
247 − stmt_a->log.qthreshold != stmt_b->log.qthreshold ||
248 − stmt_a->log.level != stmt_b->log.level ||
249 − stmt_a->log.logflags != stmt_b->log.logflags ||
250 − stmt_a->log.flags != stmt_b->log.flags)
251 − return false;
252
253 − if (!!stmt_a->log.prefix ^ !!stmt_b->log.prefix)
254 − return false;
255
256 − if (!stmt_a->log.prefix)
257 − return true;
258
259 − if (strcmp(stmt_a->log.prefix, stmt_b->log.prefix))
260 − return false;
261 − break;
262 − case STMT_REJECT:
263 − if (stmt_a->reject.family != stmt_b->reject.family ||
264 − stmt_a->reject.type != stmt_b->reject.type ||
265 − stmt_a->reject.icmp_code != stmt_b->reject.icmp_code)
266 − return false;
267
268 − if (!!stmt_a->reject.expr ^ !!stmt_b->reject.expr)
269 − return false;
270
271 − if (!stmt_a->reject.expr)
272 − return true;
273
274 − if (!__expr_cmp(stmt_a->reject.expr, stmt_b->reject.expr))
275 − return false;
276 − break;
277 − case STMT_NAT:
278 − if (stmt_a->nat.type != stmt_b->nat.type ||
279 − stmt_a->nat.flags != stmt_b->nat.flags ||
280 − stmt_a->nat.family != stmt_b->nat.family ||
281 − stmt_a->nat.type_flags != stmt_b->nat.type_flags)
282 − return false;
283
284 − switch (stmt_a->nat.type) {
285 − case NFT_NAT_SNAT:
286 case NFT_NAT_DNAT:
287 − if ((stmt_a->nat.addr &&
288 − stmt_a->nat.addr->etype != EXPR_SYMBOL &&
289 − stmt_a->nat.addr->etype != EXPR_RANGE) ||
290 − (stmt_b->nat.addr &&
291 − stmt_b->nat.addr->etype != EXPR_SYMBOL &&
292 − stmt_b->nat.addr->etype != EXPR_RANGE) ||
293 − (stmt_a->nat.proto &&
294 − stmt_a->nat.proto->etype != EXPR_SYMBOL &&
295 − stmt_a->nat.proto->etype != EXPR_RANGE) ||
296 − (stmt_b->nat.proto &&
297 − stmt_b->nat.proto->etype != EXPR_SYMBOL &&
298 − stmt_b->nat.proto->etype != EXPR_RANGE))
299 − return false;
300 − break;
301 − case NFT_NAT_MASQ:
302 − break;
303 − case NFT_NAT_REDIR:
304 − if ((stmt_a->nat.proto &&
305 − stmt_a->nat.proto->etype != EXPR_SYMBOL &&
306 − stmt_a->nat.proto->etype != EXPR_RANGE) ||
307 − (stmt_b->nat.proto &&
308 − stmt_b->nat.proto->etype != EXPR_SYMBOL &&
309 − stmt_b->nat.proto->etype != EXPR_RANGE))
310 − return false;
311
312 /* it should be possible to infer implicit redirections
313 * such as:
314 *
315 * tcp dport 1234 redirect
316 * tcp dport 3456 redirect to :7890
317 * merge:
318 * redirect to tcp dport map { 1234 : 1234, 3456 : 7890 }
319 *
320 * currently not implemented.
321 */
322 − if (fully_compare &&
323 − stmt_a->nat.type == NFT_NAT_REDIR &&
324 − stmt_b->nat.type == NFT_NAT_REDIR &&
325 − (!!stmt_a->nat.proto ^ !!stmt_b->nat.proto))
326 − return false;
327
328 − break;
329 − default:
330 − assert(0);
331 }
332
333 − return true;
334 − default:
335 /* ... Merging anything else is yet unsupported. */
336 − return false;
337 }
338
339 − return true;
340 }
341
342 27 static bool expr_verdict_eq(const struct expr *expr_a, const struct expr *expr_b)
343 {
344 char chain_a[NFT_CHAIN_MAXNAMELEN];
345 char chain_b[NFT_CHAIN_MAXNAMELEN];
346
347
2/2
✓ Branch 0 (2→3) taken 7 times.
✓ Branch 1 (2→4) taken 20 times.
27 if (expr_a->verdict != expr_b->verdict)
348 7 return false;
349
3/4
✓ Branch 0 (4→5) taken 6 times.
✓ Branch 1 (4→14) taken 14 times.
✓ Branch 2 (5→6) taken 6 times.
✗ Branch 3 (5→14) not taken.
20 if (expr_a->chain && expr_b->chain) {
350
1/2
✓ Branch 0 (6→7) taken 6 times.
✗ Branch 1 (6→8) not taken.
6 if (expr_a->chain->etype != EXPR_VALUE ||
351
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 6 times.
6 expr_a->chain->etype != expr_b->chain->etype)
352 ✗ return false;
353 6 expr_chain_export(expr_a->chain, chain_a);
354 6 expr_chain_export(expr_b->chain, chain_b);
355
1/2
✓ Branch 0 (11→12) taken 6 times.
✗ Branch 1 (11→13) not taken.
6 if (strcmp(chain_a, chain_b))
356 6 return false;
357
2/4
✓ Branch 0 (14→15) taken 14 times.
✗ Branch 1 (14→16) not taken.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 14 times.
14 } else if (expr_a->chain || expr_b->chain) {
358 ✗ return false;
359 }
360
361 14 return true;
362 }
363
364 30 static bool stmt_verdict_eq(const struct stmt *stmt_a, const struct stmt *stmt_b)
365 {
366 struct expr *expr_a, *expr_b;
367
368
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 30 times.
30 assert (stmt_a->type == STMT_VERDICT);
369
370 30 expr_a = stmt_a->expr;
371 30 expr_b = stmt_b->expr;
372
2/2
✓ Branch 0 (4→5) taken 27 times.
✓ Branch 1 (4→8) taken 3 times.
30 if (expr_a->etype == EXPR_VERDICT &&
373
1/2
✓ Branch 0 (5→6) taken 27 times.
✗ Branch 1 (5→8) not taken.
27 expr_b->etype == EXPR_VERDICT)
374 27 return expr_verdict_eq(expr_a, expr_b);
375
376
1/2
✓ Branch 0 (8→9) taken 3 times.
✗ Branch 1 (8→12) not taken.
3 if (expr_a->etype == EXPR_MAP &&
377
1/2
✓ Branch 0 (9→10) taken 3 times.
✗ Branch 1 (9→12) not taken.
3 expr_b->etype == EXPR_MAP)
378 3 return __expr_cmp(expr_a->map, expr_b->map);
379
380 ✗ return false;
381 }
382
383 509 static bool stmt_type_find(struct optimize_ctx *ctx, const struct stmt *stmt)
384 {
385 509 bool unsupported_exists = false;
386 uint32_t i;
387
388
2/2
✓ Branch 0 (9→3) taken 1111 times.
✓ Branch 1 (9→10) taken 205 times.
1316 for (i = 0; i < ctx->num_stmts; i++) {
389
2/2
✓ Branch 0 (3→4) taken 27 times.
✓ Branch 1 (3→5) taken 1084 times.
1111 if (ctx->stmt[i]->type == STMT_INVALID)
390 27 unsupported_exists = true;
391
392
2/2
✓ Branch 0 (6→7) taken 304 times.
✓ Branch 1 (6→8) taken 807 times.
1111 if (__stmt_type_eq(stmt, ctx->stmt[i], false))
393 304 return true;
394 }
395
396
2/2
✓ Branch 0 (10→11) taken 201 times.
✓ Branch 1 (10→12) taken 4 times.
205 switch (stmt->type) {
397 201 case STMT_EXPRESSION:
398 case STMT_VERDICT:
399 case STMT_COUNTER:
400 case STMT_NOTRACK:
401 case STMT_LOG:
402 case STMT_NAT:
403 case STMT_REJECT:
404 201 break;
405 4 default:
406 /* add unsupported statement only once to statement matrix. */
407
2/2
✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→14) taken 3 times.
4 if (unsupported_exists)
408 1 return true;
409 3 break;
410 }
411
412 204 return false;
413 }
414
415 210 static int rule_collect_stmts(struct optimize_ctx *ctx, struct rule *rule)
416 {
417 const struct stmt_ops *ops;
418 struct stmt *stmt, *clone;
419
420
2/2
✓ Branch 0 (43→3) taken 509 times.
✓ Branch 1 (43→44) taken 210 times.
719 list_for_each_entry(stmt, &rule->stmts, list) {
421
2/2
✓ Branch 0 (4→5) taken 305 times.
✓ Branch 1 (4→6) taken 204 times.
509 if (stmt_type_find(ctx, stmt))
422 305 continue;
423
424 /* No refcounter available in statement objects, clone it to
425 * to store in the array of selectors.
426 */
427 204 ops = stmt_ops(stmt);
428 204 clone = stmt_alloc(&internal_location, ops);
429
7/7
✓ Branch 0 (8→9) taken 110 times.
✓ Branch 1 (8→14) taken 50 times.
✓ Branch 2 (8→16) taken 9 times.
✓ Branch 3 (8→17) taken 1 times.
✓ Branch 4 (8→21) taken 26 times.
✓ Branch 5 (8→35) taken 5 times.
✓ Branch 6 (8→39) taken 3 times.
204 switch (stmt->type) {
430 110 case STMT_EXPRESSION:
431
2/2
✓ Branch 0 (9→10) taken 7 times.
✓ Branch 1 (9→12) taken 103 times.
110 if (stmt->expr->op != OP_IMPLICIT &&
432
2/2
✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→12) taken 5 times.
7 stmt->expr->op != OP_EQ) {
433 2 clone->type = STMT_INVALID;
434 2 break;
435 }
436
2/2
✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→14) taken 107 times.
108 if (stmt->expr->left->etype == EXPR_CONCAT) {
437 1 clone->type = STMT_INVALID;
438 1 break;
439 }
440 /* fall-through */
441 case STMT_VERDICT:
442 157 clone->expr = expr_get(stmt->expr);
443 157 break;
444 9 case STMT_COUNTER:
445 case STMT_NOTRACK:
446 9 break;
447 1 case STMT_LOG:
448 1 memcpy(&clone->log, &stmt->log, sizeof(clone->log));
449
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→20) taken 1 times.
1 if (stmt->log.prefix)
450 ✗ clone->log.prefix = xstrdup(stmt->log.prefix);
451 1 break;
452 26 case STMT_NAT:
453
2/2
✓ Branch 0 (21→22) taken 14 times.
✓ Branch 1 (21→24) taken 12 times.
26 if ((stmt->nat.addr &&
454
2/2
✓ Branch 0 (22→23) taken 13 times.
✓ Branch 1 (22→27) taken 1 times.
14 (stmt->nat.addr->etype == EXPR_MAP ||
455
2/2
✓ Branch 0 (23→24) taken 12 times.
✓ Branch 1 (23→27) taken 1 times.
13 stmt->nat.addr->etype == EXPR_VARIABLE)) ||
456
2/2
✓ Branch 0 (24→25) taken 16 times.
✓ Branch 1 (24→28) taken 8 times.
24 (stmt->nat.proto &&
457
1/2
✓ Branch 0 (25→26) taken 16 times.
✗ Branch 1 (25→27) not taken.
16 (stmt->nat.proto->etype == EXPR_MAP ||
458
1/2
✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→28) taken 16 times.
16 stmt->nat.proto->etype == EXPR_VARIABLE))) {
459 2 clone->type = STMT_INVALID;
460 2 break;
461 }
462 24 clone->nat.type = stmt->nat.type;
463 24 clone->nat.family = stmt->nat.family;
464
2/2
✓ Branch 0 (28→29) taken 12 times.
✓ Branch 1 (28→31) taken 12 times.
24 if (stmt->nat.addr)
465 12 clone->nat.addr = expr_clone(stmt->nat.addr);
466
2/2
✓ Branch 0 (31→32) taken 16 times.
✓ Branch 1 (31→34) taken 8 times.
24 if (stmt->nat.proto)
467 16 clone->nat.proto = expr_clone(stmt->nat.proto);
468 24 clone->nat.flags = stmt->nat.flags;
469 24 clone->nat.type_flags = stmt->nat.type_flags;
470 24 break;
471 5 case STMT_REJECT:
472
2/2
✓ Branch 0 (35→36) taken 1 times.
✓ Branch 1 (35→38) taken 4 times.
5 if (stmt->reject.expr)
473 1 clone->reject.expr = expr_get(stmt->reject.expr);
474 5 clone->reject.type = stmt->reject.type;
475 5 clone->reject.icmp_code = stmt->reject.icmp_code;
476 5 clone->reject.family = stmt->reject.family;
477 5 break;
478 3 default:
479 3 clone->type = STMT_INVALID;
480 3 break;
481 }
482
483 204 ctx->stmt[ctx->num_stmts++] = clone;
484
1/2
✗ Branch 0 (40→41) not taken.
✓ Branch 1 (40→42) taken 204 times.
204 if (ctx->num_stmts >= MAX_STMTS)
485 ✗ return -1;
486 }
487
488 210 return 0;
489 }
490
491 9 static int unsupported_in_stmt_matrix(const struct optimize_ctx *ctx)
492 {
493 uint32_t i;
494
495
1/2
✓ Branch 0 (6→3) taken 27 times.
✗ Branch 1 (6→7) not taken.
27 for (i = 0; i < ctx->num_stmts; i++) {
496
2/2
✓ Branch 0 (3→4) taken 9 times.
✓ Branch 1 (3→5) taken 18 times.
27 if (ctx->stmt[i]->type == STMT_INVALID)
497 9 return i;
498 }
499 /* this should not happen. */
500 ✗ return -1;
501 }
502
503 509 static int cmd_stmt_find_in_stmt_matrix(struct optimize_ctx *ctx, struct stmt *stmt)
504 {
505 uint32_t i;
506
507
2/2
✓ Branch 0 (7→3) taken 1327 times.
✓ Branch 1 (7→8) taken 9 times.
1336 for (i = 0; i < ctx->num_stmts; i++) {
508
2/2
✓ Branch 0 (4→5) taken 500 times.
✓ Branch 1 (4→6) taken 827 times.
1327 if (__stmt_type_eq(stmt, ctx->stmt[i], false))
509 500 return i;
510 }
511
512 9 return -1;
513 }
514
515 static struct stmt unsupported_stmt = {
516 .type = STMT_INVALID,
517 };
518
519 210 static void rule_build_stmt_matrix_stmts(struct optimize_ctx *ctx,
520 struct rule *rule, uint32_t *i)
521 {
522 struct stmt *stmt;
523 int k;
524
525
2/2
✓ Branch 0 (11→3) taken 509 times.
✓ Branch 1 (11→12) taken 210 times.
719 list_for_each_entry(stmt, &rule->stmts, list) {
526 509 k = cmd_stmt_find_in_stmt_matrix(ctx, stmt);
527
2/2
✓ Branch 0 (4→5) taken 9 times.
✓ Branch 1 (4→9) taken 500 times.
509 if (k < 0) {
528 9 k = unsupported_in_stmt_matrix(ctx);
529
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 9 times.
9 assert(k >= 0);
530 9 ctx->stmt_matrix[*i][k] = &unsupported_stmt;
531 9 continue;
532 }
533 500 ctx->stmt_matrix[*i][k] = stmt;
534 }
535 210 ctx->rule[(*i)++] = rule;
536 210 }
537
538 41 static int stmt_verdict_find(const struct optimize_ctx *ctx)
539 {
540 uint32_t i;
541
542
2/2
✓ Branch 0 (7→3) taken 119 times.
✓ Branch 1 (7→8) taken 1 times.
120 for (i = 0; i < ctx->num_stmts; i++) {
543
2/2
✓ Branch 0 (3→4) taken 79 times.
✓ Branch 1 (3→5) taken 40 times.
119 if (ctx->stmt[i]->type != STMT_VERDICT)
544 79 continue;
545
546 40 return i;
547 }
548
549 1 return -1;
550 }
551
552 struct merge {
553 /* interval of rules to be merged */
554 uint32_t rule_from;
555 uint32_t num_rules;
556 /* statements to be merged (index relative to statement matrix) */
557 uint32_t stmt[MAX_STMTS];
558 uint32_t num_stmts;
559 /* merge has been invalidated */
560 bool skip;
561 };
562
563 6 static void merge_expr_stmts(const struct optimize_ctx *ctx,
564 uint32_t from, uint32_t to,
565 const struct merge *merge,
566 struct stmt *stmt_a)
567 {
568 struct expr *expr_a, *expr_b, *set, *elem;
569 struct stmt *stmt_b;
570 uint32_t i;
571
572 6 set = set_expr_alloc(&internal_location, NULL);
573
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6 times.
6 expr_set(set)->set_flags |= NFT_SET_ANONYMOUS;
574
575 6 expr_a = stmt_a->expr->right;
576 6 elem = set_elem_expr_alloc(&internal_location, expr_get(expr_a));
577 6 set_expr_add(set, elem);
578
579
2/2
✓ Branch 0 (13→9) taken 14 times.
✓ Branch 1 (13→14) taken 6 times.
20 for (i = from + 1; i <= to; i++) {
580 14 stmt_b = ctx->stmt_matrix[i][merge->stmt[0]];
581 14 expr_b = stmt_b->expr->right;
582 14 elem = set_elem_expr_alloc(&internal_location, expr_get(expr_b));
583 14 set_expr_add(set, elem);
584 }
585
586 6 expr_free(stmt_a->expr->right);
587 6 stmt_a->expr->right = set;
588 6 }
589
590 3 static void merge_vmap(const struct optimize_ctx *ctx,
591 struct stmt *stmt_a, const struct stmt *stmt_b)
592 {
593 struct expr *mappings, *mapping, *expr;
594
595 3 mappings = stmt_b->expr->mappings;
596
4/6
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 3 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 10 times.
✓ Branch 4 (10→5) taken 7 times.
✓ Branch 5 (10→11) taken 3 times.
10 list_for_each_entry(expr, &expr_set(mappings)->expressions, list) {
597 7 mapping = expr_clone(expr);
598 7 set_expr_add(stmt_a->expr->mappings, mapping);
599 }
600 3 }
601
602 3 static void merge_verdict_stmts(const struct optimize_ctx *ctx,
603 uint32_t from, uint32_t to,
604 const struct merge *merge,
605 struct stmt *stmt_a)
606 {
607 struct stmt *stmt_b;
608 uint32_t i;
609
610
2/2
✓ Branch 0 (13→3) taken 3 times.
✓ Branch 1 (13→14) taken 3 times.
6 for (i = from + 1; i <= to; i++) {
611 3 stmt_b = ctx->stmt_matrix[i][merge->stmt[0]];
612
1/2
✓ Branch 0 (3→4) taken 3 times.
✗ Branch 1 (3→10) not taken.
3 switch (stmt_b->type) {
613 3 case STMT_VERDICT:
614
1/2
✓ Branch 0 (4→5) taken 3 times.
✗ Branch 1 (4→6) not taken.
3 switch (stmt_b->expr->etype) {
615 3 case EXPR_MAP:
616 3 merge_vmap(ctx, stmt_a, stmt_b);
617 3 break;
618 ✗ default:
619 ✗ assert(0);
620 }
621 3 break;
622 ✗ default:
623 ✗ assert(0);
624 break;
625 }
626 }
627 3 }
628
629 9 static void merge_stmts(const struct optimize_ctx *ctx,
630 uint32_t from, uint32_t to, const struct merge *merge)
631 {
632 9 struct stmt *stmt_a = ctx->stmt_matrix[from][merge->stmt[0]];
633
634
2/3
✓ Branch 0 (2→3) taken 6 times.
✓ Branch 1 (2→4) taken 3 times.
✗ Branch 2 (2→6) not taken.
9 switch (stmt_a->type) {
635 6 case STMT_EXPRESSION:
636 6 merge_expr_stmts(ctx, from, to, merge, stmt_a);
637 6 break;
638 3 case STMT_VERDICT:
639 3 merge_verdict_stmts(ctx, from, to, merge, stmt_a);
640 3 break;
641 ✗ default:
642 ✗ assert(0);
643 }
644 9 }
645
646 − static void __merge_concat(const struct optimize_ctx *ctx, uint32_t i,
647 const struct merge *merge, struct list_head *concat_list)
648 {
649 struct expr *concat, *next, *expr, *concat_clone, *clone;
650 − LIST_HEAD(pending_list);
651 struct stmt *stmt_a;
652 uint32_t k;
653
654 − concat = concat_expr_alloc(&internal_location);
655 − list_add(&concat->list, concat_list);
656
657 − for (k = 0; k < merge->num_stmts; k++) {
658 − list_for_each_entry_safe(concat, next, concat_list, list) {
659 − stmt_a = ctx->stmt_matrix[i][merge->stmt[k]];
660 − switch (stmt_a->expr->right->etype) {
661 − case EXPR_SET:
662 − list_for_each_entry(expr, &expr_set(stmt_a->expr->right)->expressions, list) {
663 − concat_clone = expr_clone(concat);
664 − clone = expr_clone(expr->key);
665 − concat_expr_add(concat_clone, clone);
666 − list_add_tail(&concat_clone->list, &pending_list);
667 }
668 − list_del(&concat->list);
669 − expr_free(concat);
670 − break;
671 − case EXPR_SYMBOL:
672 case EXPR_VALUE:
673 case EXPR_PREFIX:
674 case EXPR_RANGE_SYMBOL:
675 case EXPR_RANGE:
676 case EXPR_RANGE_VALUE:
677 − clone = expr_clone(stmt_a->expr->right);
678 − concat_expr_add(concat, clone);
679 − break;
680 − case EXPR_LIST:
681 − list_for_each_entry(expr, &expr_list(stmt_a->expr->right)->expressions, list) {
682 − concat_clone = expr_clone(concat);
683 − clone = expr_clone(expr);
684 − concat_expr_add(concat_clone, clone);
685 − list_add_tail(&concat_clone->list, &pending_list);
686 }
687 − list_del(&concat->list);
688 − expr_free(concat);
689 − break;
690 − default:
691 − assert(0);
692 break;
693 }
694 }
695 − list_splice_init(&pending_list, concat_list);
696 }
697 − }
698
699 − static void __merge_concat_stmts(const struct optimize_ctx *ctx, uint32_t i,
700 const struct merge *merge, struct expr *set)
701 {
702 struct expr *concat, *next, *elem;
703 − LIST_HEAD(concat_list);
704
705 − __merge_concat(ctx, i, merge, &concat_list);
706
707 − list_for_each_entry_safe(concat, next, &concat_list, list) {
708 − list_del(&concat->list);
709 − elem = set_elem_expr_alloc(&internal_location, concat);
710 − set_expr_add(set, elem);
711 }
712 − }
713
714 8 static void merge_concat_stmts(const struct optimize_ctx *ctx,
715 uint32_t from, uint32_t to,
716 const struct merge *merge)
717 {
718 struct stmt *stmt, *stmt_a;
719 struct expr *concat, *set;
720 uint32_t i, k;
721
722 8 stmt = ctx->stmt_matrix[from][merge->stmt[0]];
723 /* build concatenation of selectors, eg. ifname . ip daddr . tcp dport */
724 8 concat = concat_expr_alloc(&internal_location);
725
726
2/2
✓ Branch 0 (7→4) taken 19 times.
✓ Branch 1 (7→8) taken 8 times.
27 for (k = 0; k < merge->num_stmts; k++) {
727 19 stmt_a = ctx->stmt_matrix[from][merge->stmt[k]];
728 19 concat_expr_add(concat, expr_get(stmt_a->expr->left));
729 }
730 8 expr_free(stmt->expr->left);
731 8 stmt->expr->left = concat;
732
733 /* build set data contenation, eg. { eth0 . 1.1.1.1 . 22 } */
734 8 set = set_expr_alloc(&internal_location, NULL);
735
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 8 times.
8 expr_set(set)->set_flags |= NFT_SET_ANONYMOUS;
736
737
2/2
✓ Branch 0 (15→13) taken 21 times.
✓ Branch 1 (15→16) taken 8 times.
29 for (i = from; i <= to; i++)
738 21 __merge_concat_stmts(ctx, i, merge, set);
739
740 8 expr_free(stmt->expr->right);
741 8 stmt->expr->right = set;
742
743
2/2
✓ Branch 0 (21→18) taken 11 times.
✓ Branch 1 (21→22) taken 8 times.
19 for (k = 1; k < merge->num_stmts; k++) {
744 11 stmt_a = ctx->stmt_matrix[from][merge->stmt[k]];
745 11 list_del(&stmt_a->list);
746 11 stmt_free(stmt_a);
747 }
748 8 }
749
750 17 static void build_verdict_map(struct expr *expr, struct stmt *verdict,
751 struct expr *set, struct stmt *counter)
752 {
753 struct expr *item, *elem, *mapping;
754 struct stmt *counter_elem;
755
756
3/4
✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→19) taken 1 times.
✓ Branch 2 (2→36) taken 14 times.
✗ Branch 3 (2→44) not taken.
17 switch (expr->etype) {
757 2 case EXPR_LIST:
758
4/6
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2 times.
✗ Branch 2 (15→16) not taken.
✓ Branch 3 (15→17) taken 6 times.
✓ Branch 4 (17→6) taken 4 times.
✓ Branch 5 (17→18) taken 2 times.
6 list_for_each_entry(item, &expr_list(expr)->expressions, list) {
759 4 elem = set_elem_expr_alloc(&internal_location, expr_get(item));
760
2/2
✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→11) taken 2 times.
4 if (counter) {
761 2 counter_elem = counter_stmt_alloc(&counter->location);
762 2 list_add_tail(&counter_elem->list, &elem->stmt_list);
763 }
764
765 4 mapping = mapping_expr_alloc(&internal_location, elem,
766 expr_get(verdict->expr));
767 4 set_expr_add(set, mapping);
768 }
769 2 stmt_free(counter);
770 2 break;
771 1 case EXPR_SET:
772
4/6
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 1 times.
✗ Branch 2 (31→32) not taken.
✓ Branch 3 (31→33) taken 2 times.
✓ Branch 4 (33→22) taken 1 times.
✓ Branch 5 (33→34) taken 1 times.
2 list_for_each_entry(item, &expr_set(expr)->expressions, list) {
773 1 elem = set_elem_expr_alloc(&internal_location, expr_get(item->key));
774
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→27) taken 1 times.
1 if (counter) {
775 ✗ counter_elem = counter_stmt_alloc(&counter->location);
776 ✗ list_add_tail(&counter_elem->list, &elem->stmt_list);
777 }
778
779 1 mapping = mapping_expr_alloc(&internal_location, elem,
780 expr_get(verdict->expr));
781 1 set_expr_add(set, mapping);
782 }
783 1 stmt_free(counter);
784 1 break;
785 14 case EXPR_PREFIX:
786 case EXPR_RANGE_SYMBOL:
787 case EXPR_RANGE:
788 case EXPR_RANGE_VALUE:
789 case EXPR_VALUE:
790 case EXPR_SYMBOL:
791 case EXPR_CONCAT:
792 14 elem = set_elem_expr_alloc(&internal_location, expr_get(expr));
793
2/2
✓ Branch 0 (38→39) taken 3 times.
✓ Branch 1 (38→40) taken 11 times.
14 if (counter)
794 3 list_add_tail(&counter->list, &elem->stmt_list);
795
796 14 mapping = mapping_expr_alloc(&internal_location, elem,
797 expr_get(verdict->expr));
798 14 set_expr_add(set, mapping);
799 14 break;
800 ✗ default:
801 ✗ assert(0);
802 break;
803 }
804 17 }
805
806 7 static void remove_counter(const struct optimize_ctx *ctx, uint32_t from)
807 {
808 struct stmt *stmt;
809 uint32_t i;
810
811 /* remove counter statement */
812
2/2
✓ Branch 0 (9→3) taken 41 times.
✓ Branch 1 (9→10) taken 7 times.
48 for (i = 0; i < ctx->num_stmts; i++) {
813 41 stmt = ctx->stmt_matrix[from][i];
814
2/2
✓ Branch 0 (3→4) taken 23 times.
✓ Branch 1 (3→5) taken 18 times.
41 if (!stmt)
815 23 continue;
816
817
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 18 times.
18 if (stmt->type == STMT_COUNTER) {
818 ✗ list_del(&stmt->list);
819 ✗ stmt_free(stmt);
820 }
821 }
822 7 }
823
824 34 static struct stmt *zap_counter(const struct optimize_ctx *ctx, uint32_t from)
825 {
826 struct stmt *stmt;
827 uint32_t i;
828
829 /* remove counter statement */
830
2/2
✓ Branch 0 (9→3) taken 101 times.
✓ Branch 1 (9→10) taken 28 times.
129 for (i = 0; i < ctx->num_stmts; i++) {
831 101 stmt = ctx->stmt_matrix[from][i];
832
2/2
✓ Branch 0 (3→4) taken 20 times.
✓ Branch 1 (3→5) taken 81 times.
101 if (!stmt)
833 20 continue;
834
835
2/2
✓ Branch 0 (5→6) taken 6 times.
✓ Branch 1 (5→8) taken 75 times.
81 if (stmt->type == STMT_COUNTER) {
836 6 list_del(&stmt->list);
837 6 return stmt;
838 }
839 }
840
841 28 return NULL;
842 }
843
844 8 static void merge_stmts_vmap(const struct optimize_ctx *ctx,
845 uint32_t from, uint32_t to,
846 const struct merge *merge)
847 {
848 8 struct stmt *stmt_a = ctx->stmt_matrix[from][merge->stmt[0]];
849 struct stmt *stmt_b, *verdict_a, *verdict_b, *stmt;
850 struct expr *expr_a, *expr_b, *expr, *left, *set;
851 struct stmt *counter;
852 uint32_t i;
853 int k;
854
855 8 k = stmt_verdict_find(ctx);
856
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 8 times.
8 assert(k >= 0);
857
858 8 set = set_expr_alloc(&internal_location, NULL);
859
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 8 times.
8 expr_set(set)->set_flags |= NFT_SET_ANONYMOUS;
860
861 8 expr_a = stmt_a->expr->right;
862 8 verdict_a = ctx->stmt_matrix[from][k];
863 8 counter = zap_counter(ctx, from);
864 8 build_verdict_map(expr_a, verdict_a, set, counter);
865
866
2/2
✓ Branch 0 (14→11) taken 9 times.
✓ Branch 1 (14→15) taken 8 times.
17 for (i = from + 1; i <= to; i++) {
867 9 stmt_b = ctx->stmt_matrix[i][merge->stmt[0]];
868 9 expr_b = stmt_b->expr->right;
869 9 verdict_b = ctx->stmt_matrix[i][k];
870 9 counter = zap_counter(ctx, i);
871 9 build_verdict_map(expr_b, verdict_b, set, counter);
872 }
873
874 8 left = expr_get(stmt_a->expr->left);
875 8 expr = map_expr_alloc(&internal_location, left, set);
876 8 stmt = verdict_stmt_alloc(&internal_location, expr);
877
878 8 list_add(&stmt->list, &stmt_a->list);
879 8 list_del(&stmt_a->list);
880 8 stmt_free(stmt_a);
881 8 list_del(&verdict_a->list);
882 8 stmt_free(verdict_a);
883 8 }
884
885 − static void __merge_concat_stmts_vmap(const struct optimize_ctx *ctx,
886 uint32_t i, const struct merge *merge,
887 struct expr *set, struct stmt *verdict)
888 {
889 struct expr *concat, *next, *elem, *mapping;
890 struct stmt *counter, *counter_elem;
891 − LIST_HEAD(concat_list);
892
893 − counter = zap_counter(ctx, i);
894 − __merge_concat(ctx, i, merge, &concat_list);
895
896 − list_for_each_entry_safe(concat, next, &concat_list, list) {
897 − list_del(&concat->list);
898 − elem = set_elem_expr_alloc(&internal_location, concat);
899 − if (counter) {
900 − counter_elem = counter_stmt_alloc(&counter->location);
901 − list_add_tail(&counter_elem->list, &elem->stmt_list);
902 }
903
904 − mapping = mapping_expr_alloc(&internal_location, elem,
905 expr_get(verdict->expr));
906 − set_expr_add(set, mapping);
907 }
908 − stmt_free(counter);
909 − }
910
911 5 static void merge_concat_stmts_vmap(const struct optimize_ctx *ctx,
912 uint32_t from, uint32_t to,
913 const struct merge *merge)
914 {
915 5 struct stmt *orig_stmt = ctx->stmt_matrix[from][merge->stmt[0]];
916 struct stmt *stmt, *stmt_a, *verdict;
917 struct expr *concat_a, *expr, *set;
918 uint32_t i;
919 int k;
920
921 5 k = stmt_verdict_find(ctx);
922
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 5 times.
5 assert(k >= 0);
923
924 /* build concatenation of selectors, eg. ifname . ip daddr . tcp dport */
925 5 concat_a = concat_expr_alloc(&internal_location);
926
2/2
✓ Branch 0 (10→7) taken 10 times.
✓ Branch 1 (10→11) taken 5 times.
15 for (i = 0; i < merge->num_stmts; i++) {
927 10 stmt_a = ctx->stmt_matrix[from][merge->stmt[i]];
928 10 concat_expr_add(concat_a, expr_get(stmt_a->expr->left));
929 }
930
931 /* build set data contenation, eg. { eth0 . 1.1.1.1 . 22 : accept } */
932 5 set = set_expr_alloc(&internal_location, NULL);
933
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 5 times.
5 expr_set(set)->set_flags |= NFT_SET_ANONYMOUS;
934
935
2/2
✓ Branch 0 (17→15) taken 17 times.
✓ Branch 1 (17→18) taken 5 times.
22 for (i = from; i <= to; i++) {
936 17 verdict = ctx->stmt_matrix[i][k];
937 17 __merge_concat_stmts_vmap(ctx, i, merge, set, verdict);
938 }
939
940 5 expr = map_expr_alloc(&internal_location, concat_a, set);
941 5 stmt = verdict_stmt_alloc(&internal_location, expr);
942
943 5 list_add(&stmt->list, &orig_stmt->list);
944 5 list_del(&orig_stmt->list);
945 5 stmt_free(orig_stmt);
946
947
2/2
✓ Branch 0 (27→24) taken 5 times.
✓ Branch 1 (27→28) taken 5 times.
10 for (i = 1; i < merge->num_stmts; i++) {
948 5 stmt_a = ctx->stmt_matrix[from][merge->stmt[i]];
949 5 list_del(&stmt_a->list);
950 5 stmt_free(stmt_a);
951 }
952
953 5 verdict = ctx->stmt_matrix[from][k];
954 5 list_del(&verdict->list);
955 5 stmt_free(verdict);
956 5 }
957
958 28 static bool stmt_verdict_cmp(const struct optimize_ctx *ctx,
959 uint32_t from, uint32_t to)
960 {
961 struct stmt *stmt_a, *stmt_b;
962 uint32_t i;
963 int k;
964
965 28 k = stmt_verdict_find(ctx);
966
2/2
✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 27 times.
28 if (k < 0)
967 1 return true;
968
969
2/2
✓ Branch 0 (16→6) taken 40 times.
✓ Branch 1 (16→17) taken 14 times.
54 for (i = from; i + 1 <= to; i++) {
970 40 stmt_a = ctx->stmt_matrix[i][k];
971 40 stmt_b = ctx->stmt_matrix[i + 1][k];
972
3/4
✓ Branch 0 (6→7) taken 10 times.
✓ Branch 1 (6→9) taken 30 times.
✓ Branch 2 (7→8) taken 10 times.
✗ Branch 3 (7→9) not taken.
40 if (!stmt_a && !stmt_b)
973 10 continue;
974
2/4
✓ Branch 0 (9→10) taken 30 times.
✗ Branch 1 (9→11) not taken.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→12) taken 30 times.
30 if (!stmt_a || !stmt_b)
975 ✗ return false;
976
2/2
✓ Branch 0 (13→14) taken 13 times.
✓ Branch 1 (13→15) taken 17 times.
30 if (!stmt_verdict_eq(stmt_a, stmt_b))
977 13 return false;
978 }
979
980 14 return true;
981 }
982
983 7 static int stmt_nat_type(const struct optimize_ctx *ctx, int from,
984 enum nft_nat_etypes *nat_type)
985 {
986 uint32_t j;
987
988
1/2
✓ Branch 0 (8→3) taken 34 times.
✗ Branch 1 (8→9) not taken.
34 for (j = 0; j < ctx->num_stmts; j++) {
989
2/2
✓ Branch 0 (3→4) taken 16 times.
✓ Branch 1 (3→5) taken 18 times.
34 if (!ctx->stmt_matrix[from][j])
990 16 continue;
991
992
2/2
✓ Branch 0 (5→6) taken 7 times.
✓ Branch 1 (5→7) taken 11 times.
18 if (ctx->stmt_matrix[from][j]->type == STMT_NAT) {
993 7 *nat_type = ctx->stmt_matrix[from][j]->nat.type;
994 7 return 0;
995 }
996 }
997
998 ✗ return -1;
999 }
1000
1001 7 static int stmt_nat_find(const struct optimize_ctx *ctx, int from)
1002 {
1003 enum nft_nat_etypes nat_type;
1004 uint32_t i;
1005
1006
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 7 times.
7 if (stmt_nat_type(ctx, from, &nat_type) < 0)
1007 ✗ return -1;
1008
1009
1/2
✓ Branch 0 (11→6) taken 34 times.
✗ Branch 1 (11→12) not taken.
34 for (i = 0; i < ctx->num_stmts; i++) {
1010
2/2
✓ Branch 0 (6→7) taken 8 times.
✓ Branch 1 (6→8) taken 26 times.
34 if (ctx->stmt[i]->type != STMT_NAT ||
1011
2/2
✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 7 times.
8 ctx->stmt[i]->nat.type != nat_type)
1012 27 continue;
1013
1014 7 return i;
1015 }
1016
1017 ✗ return -1;
1018 }
1019
1020 15 static struct expr *stmt_nat_expr(struct stmt *nat_stmt)
1021 {
1022 struct expr *nat_expr;
1023
1024
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 15 times.
15 assert(nat_stmt->type == STMT_NAT);
1025
1026
2/2
✓ Branch 0 (4→5) taken 9 times.
✓ Branch 1 (4→14) taken 6 times.
15 if (nat_stmt->nat.proto) {
1027
2/2
✓ Branch 0 (5→6) taken 7 times.
✓ Branch 1 (5→11) taken 2 times.
9 if (nat_stmt->nat.addr) {
1028 7 nat_expr = concat_expr_alloc(&internal_location);
1029 7 concat_expr_add(nat_expr, expr_get(nat_stmt->nat.addr));
1030 7 concat_expr_add(nat_expr, expr_get(nat_stmt->nat.proto));
1031 } else {
1032 2 nat_expr = expr_get(nat_stmt->nat.proto);
1033 }
1034 9 expr_free(nat_stmt->nat.proto);
1035 9 nat_stmt->nat.proto = NULL;
1036 } else {
1037 6 nat_expr = expr_get(nat_stmt->nat.addr);
1038 }
1039
1040
1/2
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 15 times.
15 assert(nat_expr);
1041
1042 15 return nat_expr;
1043 }
1044
1045 4 static void merge_nat(const struct optimize_ctx *ctx,
1046 uint32_t from, uint32_t to,
1047 const struct merge *merge)
1048 {
1049 struct expr *expr, *set, *elem, *nat_expr, *mapping, *left;
1050 4 int k, family = NFPROTO_UNSPEC;
1051 struct stmt *stmt, *nat_stmt;
1052 uint32_t i;
1053
1054 4 k = stmt_nat_find(ctx, from);
1055
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4 times.
4 assert(k >= 0);
1056
1057 4 set = set_expr_alloc(&internal_location, NULL);
1058
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 4 times.
4 expr_set(set)->set_flags |= NFT_SET_ANONYMOUS;
1059
1060
2/2
✓ Branch 0 (15→9) taken 8 times.
✓ Branch 1 (15→16) taken 4 times.
12 for (i = from; i <= to; i++) {
1061 8 stmt = ctx->stmt_matrix[i][merge->stmt[0]];
1062 8 expr = stmt->expr->right;
1063
1064 8 nat_stmt = ctx->stmt_matrix[i][k];
1065 8 nat_expr = stmt_nat_expr(nat_stmt);
1066
1067 8 elem = set_elem_expr_alloc(&internal_location, expr_get(expr));
1068 8 mapping = mapping_expr_alloc(&internal_location, elem, nat_expr);
1069 8 set_expr_add(set, mapping);
1070 }
1071
1072 4 stmt = ctx->stmt_matrix[from][merge->stmt[0]];
1073 4 left = expr_get(stmt->expr->left);
1074
1/2
✓ Branch 0 (17→18) taken 4 times.
✗ Branch 1 (17→22) not taken.
4 if (left->etype == EXPR_PAYLOAD) {
1075
2/2
✓ Branch 0 (18→19) taken 2 times.
✓ Branch 1 (18→20) taken 2 times.
4 if (left->payload.desc == &proto_ip)
1076 2 family = NFPROTO_IPV4;
1077
1/2
✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 2 times.
2 else if (left->payload.desc == &proto_ip6)
1078 ✗ family = NFPROTO_IPV6;
1079 }
1080 4 expr = map_expr_alloc(&internal_location, left, set);
1081
1082 4 nat_stmt = ctx->stmt_matrix[from][k];
1083
1/2
✓ Branch 0 (23→24) taken 4 times.
✗ Branch 1 (23→25) not taken.
4 if (nat_stmt->nat.family == NFPROTO_UNSPEC)
1084 4 nat_stmt->nat.family = family;
1085
1086 4 expr_free(nat_stmt->nat.addr);
1087
2/2
✓ Branch 0 (26→27) taken 1 times.
✓ Branch 1 (26→28) taken 3 times.
4 if (nat_stmt->nat.type == NFT_NAT_REDIR)
1088 1 nat_stmt->nat.proto = expr;
1089 else
1090 3 nat_stmt->nat.addr = expr;
1091
1092 4 remove_counter(ctx, from);
1093 4 list_del(&stmt->list);
1094 4 stmt_free(stmt);
1095 4 }
1096
1097 3 static void merge_concat_nat(const struct optimize_ctx *ctx,
1098 uint32_t from, uint32_t to,
1099 const struct merge *merge)
1100 {
1101 struct expr *expr, *set, *elem, *nat_expr, *mapping, *left, *concat;
1102 3 int k, family = NFPROTO_UNSPEC;
1103 struct stmt *stmt, *nat_stmt;
1104 uint32_t i, j;
1105
1106 3 k = stmt_nat_find(ctx, from);
1107
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 3 times.
3 assert(k >= 0);
1108
1109 3 set = set_expr_alloc(&internal_location, NULL);
1110
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 3 times.
3 expr_set(set)->set_flags |= NFT_SET_ANONYMOUS;
1111
1112
2/2
✓ Branch 0 (20→9) taken 7 times.
✓ Branch 1 (20→21) taken 3 times.
10 for (i = from; i <= to; i++) {
1113
1114 7 concat = concat_expr_alloc(&internal_location);
1115
2/2
✓ Branch 0 (14→11) taken 17 times.
✓ Branch 1 (14→15) taken 7 times.
24 for (j = 0; j < merge->num_stmts; j++) {
1116 17 stmt = ctx->stmt_matrix[i][merge->stmt[j]];
1117 17 expr = stmt->expr->right;
1118 17 concat_expr_add(concat, expr_get(expr));
1119 }
1120
1121 7 nat_stmt = ctx->stmt_matrix[i][k];
1122 7 nat_expr = stmt_nat_expr(nat_stmt);
1123
1124 7 elem = set_elem_expr_alloc(&internal_location, concat);
1125 7 mapping = mapping_expr_alloc(&internal_location, elem, nat_expr);
1126 7 set_expr_add(set, mapping);
1127 }
1128
1129 3 concat = concat_expr_alloc(&internal_location);
1130
2/2
✓ Branch 0 (31→23) taken 7 times.
✓ Branch 1 (31→32) taken 3 times.
10 for (j = 0; j < merge->num_stmts; j++) {
1131 7 stmt = ctx->stmt_matrix[from][merge->stmt[j]];
1132 7 left = stmt->expr->left;
1133
2/2
✓ Branch 0 (23→24) taken 6 times.
✓ Branch 1 (23→28) taken 1 times.
7 if (left->etype == EXPR_PAYLOAD) {
1134
2/2
✓ Branch 0 (24→25) taken 3 times.
✓ Branch 1 (24→26) taken 3 times.
6 if (left->payload.desc == &proto_ip)
1135 3 family = NFPROTO_IPV4;
1136
1/2
✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→28) taken 3 times.
3 else if (left->payload.desc == &proto_ip6)
1137 ✗ family = NFPROTO_IPV6;
1138 }
1139 7 concat_expr_add(concat, expr_get(left));
1140 }
1141 3 expr = map_expr_alloc(&internal_location, concat, set);
1142
1143 3 nat_stmt = ctx->stmt_matrix[from][k];
1144
1/2
✓ Branch 0 (33→34) taken 3 times.
✗ Branch 1 (33→35) not taken.
3 if (nat_stmt->nat.family == NFPROTO_UNSPEC)
1145 3 nat_stmt->nat.family = family;
1146
1147 3 expr_free(nat_stmt->nat.addr);
1148 3 nat_stmt->nat.addr = expr;
1149
1150 3 remove_counter(ctx, from);
1151
2/2
✓ Branch 0 (41→38) taken 7 times.
✓ Branch 1 (41→42) taken 3 times.
10 for (j = 0; j < merge->num_stmts; j++) {
1152 7 stmt = ctx->stmt_matrix[from][merge->stmt[j]];
1153 7 list_del(&stmt->list);
1154 7 stmt_free(stmt);
1155 }
1156 3 }
1157
1158 96 static void rule_optimize_print(struct output_ctx *octx,
1159 const struct rule *rule)
1160 {
1161 96 const struct location *loc = &rule->location;
1162 96 const struct input_descriptor *indesc = loc->indesc;
1163 96 const char *line = "";
1164 char buf[1024];
1165
1166
1/5
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 96 times.
✗ Branch 2 (2→5) not taken.
✗ Branch 3 (2→6) not taken.
✗ Branch 4 (2→7) not taken.
96 switch (indesc->type) {
1167 ✗ case INDESC_BUFFER:
1168 case INDESC_CLI:
1169 ✗ line = indesc->data;
1170 ✗ *strchrnul(line, '\n') = '\0';
1171 ✗ break;
1172 96 case INDESC_STDIN:
1173 96 line = indesc->data;
1174 96 line += loc->line_offset;
1175 96 *strchrnul(line, '\n') = '\0';
1176 96 break;
1177 ✗ case INDESC_FILE:
1178 ✗ line = line_location(indesc, loc, buf, sizeof(buf));
1179 ✗ break;
1180 ✗ case INDESC_INTERNAL:
1181 case INDESC_NETLINK:
1182 ✗ break;
1183 ✗ default:
1184 ✗ BUG("invalid input descriptor type %u", indesc->type);
1185 }
1186
1187 96 print_location(octx->error_fp, indesc, loc);
1188 96 fprintf(octx->error_fp, "%s\n", line);
1189 96 }
1190
1191 enum {
1192 MERGE_BY_VERDICT,
1193 MERGE_BY_NAT_MAP,
1194 MERGE_BY_NAT,
1195 };
1196
1197 37 static uint32_t merge_stmt_type(const struct optimize_ctx *ctx,
1198 uint32_t from, uint32_t to)
1199 {
1200 const struct stmt *stmt;
1201 uint32_t i, j;
1202
1203
2/2
✓ Branch 0 (15→3) taken 86 times.
✓ Branch 1 (15→16) taken 28 times.
114 for (i = from; i <= to; i++) {
1204
2/2
✓ Branch 0 (13→4) taken 364 times.
✓ Branch 1 (13→14) taken 77 times.
441 for (j = 0; j < ctx->num_stmts; j++) {
1205 364 stmt = ctx->stmt_matrix[i][j];
1206
2/2
✓ Branch 0 (4→5) taken 137 times.
✓ Branch 1 (4→6) taken 227 times.
364 if (!stmt)
1207 137 continue;
1208
2/2
✓ Branch 0 (6→7) taken 9 times.
✓ Branch 1 (6→12) taken 218 times.
227 if (stmt->type == STMT_NAT) {
1209
2/2
✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→9) taken 7 times.
9 if ((stmt->nat.type == NFT_NAT_REDIR &&
1210
2/2
✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 1 times.
2 !stmt->nat.proto) ||
1211
2/2
✓ Branch 0 (9→10) taken 1 times.
✓ Branch 1 (9→11) taken 7 times.
8 stmt->nat.type == NFT_NAT_MASQ)
1212 2 return MERGE_BY_NAT;
1213
1214 7 return MERGE_BY_NAT_MAP;
1215 }
1216 }
1217 }
1218
1219 /* merge by verdict, even if no verdict is specified. */
1220 28 return MERGE_BY_VERDICT;
1221 }
1222
1223 37 static void merge_rules(const struct optimize_ctx *ctx,
1224 uint32_t from, uint32_t to,
1225 const struct merge *merge,
1226 struct output_ctx *octx)
1227 {
1228 uint32_t merge_type;
1229 bool same_verdict;
1230 uint32_t i;
1231
1232 37 merge_type = merge_stmt_type(ctx, from, to);
1233
1234
3/4
✓ Branch 0 (3→4) taken 28 times.
✓ Branch 1 (3→13) taken 7 times.
✓ Branch 2 (3→17) taken 2 times.
✗ Branch 3 (3→21) not taken.
37 switch (merge_type) {
1235 28 case MERGE_BY_VERDICT:
1236 28 same_verdict = stmt_verdict_cmp(ctx, from, to);
1237
2/2
✓ Branch 0 (5→6) taken 13 times.
✓ Branch 1 (5→9) taken 15 times.
28 if (merge->num_stmts > 1) {
1238
2/2
✓ Branch 0 (6→7) taken 8 times.
✓ Branch 1 (6→8) taken 5 times.
13 if (same_verdict)
1239 8 merge_concat_stmts(ctx, from, to, merge);
1240 else
1241 5 merge_concat_stmts_vmap(ctx, from, to, merge);
1242 } else {
1243
2/2
✓ Branch 0 (9→10) taken 7 times.
✓ Branch 1 (9→11) taken 8 times.
15 if (same_verdict)
1244 7 merge_stmts(ctx, from, to, merge);
1245 else
1246 8 merge_stmts_vmap(ctx, from, to, merge);
1247 }
1248 28 break;
1249 7 case MERGE_BY_NAT_MAP:
1250
2/2
✓ Branch 0 (13→14) taken 3 times.
✓ Branch 1 (13→15) taken 4 times.
7 if (merge->num_stmts > 1)
1251 3 merge_concat_nat(ctx, from, to, merge);
1252 else
1253 4 merge_nat(ctx, from, to, merge);
1254 7 break;
1255 2 case MERGE_BY_NAT:
1256
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 2 times.
2 if (merge->num_stmts > 1)
1257 ✗ merge_concat_stmts(ctx, from, to, merge);
1258 else
1259 2 merge_stmts(ctx, from, to, merge);
1260 2 break;
1261 ✗ default:
1262 ✗ assert(0);
1263 }
1264
1265
2/2
✓ Branch 0 (23→24) taken 2 times.
✓ Branch 1 (23→25) taken 35 times.
37 if (ctx->rule[from]->comment) {
1266 2 free_const(ctx->rule[from]->comment);
1267 2 ctx->rule[from]->comment = NULL;
1268 }
1269
1270 37 octx->flags |= NFT_CTX_OUTPUT_STATELESS;
1271
1272 37 fprintf(octx->error_fp, "Merging:\n");
1273 37 rule_optimize_print(octx, ctx->rule[from]);
1274
1275
2/2
✓ Branch 0 (32→28) taken 59 times.
✓ Branch 1 (32→33) taken 37 times.
96 for (i = from + 1; i <= to; i++) {
1276 59 rule_optimize_print(octx, ctx->rule[i]);
1277 59 list_del(&ctx->rule[i]->list);
1278 59 rule_free(ctx->rule[i]);
1279 }
1280
1281 37 fprintf(octx->error_fp, "into:\n\t");
1282 37 rule_print(ctx->rule[from], octx);
1283 37 fprintf(octx->error_fp, "\n");
1284
1285 37 octx->flags &= ~NFT_CTX_OUTPUT_STATELESS;
1286 37 }
1287
1288 455 static bool stmt_type_eq(const struct stmt *stmt_a, const struct stmt *stmt_b)
1289 {
1290
4/4
✓ Branch 0 (2→3) taken 151 times.
✓ Branch 1 (2→5) taken 304 times.
✓ Branch 2 (3→4) taken 149 times.
✓ Branch 3 (3→5) taken 2 times.
455 if (!stmt_a && !stmt_b)
1291 149 return true;
1292
2/2
✓ Branch 0 (5→6) taken 2 times.
✓ Branch 1 (5→7) taken 304 times.
306 else if (!stmt_a)
1293 2 return false;
1294
2/2
✓ Branch 0 (7→8) taken 50 times.
✓ Branch 1 (7→9) taken 254 times.
304 else if (!stmt_b)
1295 50 return false;
1296
1297 254 return __stmt_type_eq(stmt_a, stmt_b, true);
1298 }
1299
1300 455 static bool stmt_is_mergeable(const struct stmt *stmt)
1301 {
1302
2/2
✓ Branch 0 (2→3) taken 151 times.
✓ Branch 1 (2→4) taken 304 times.
455 if (!stmt)
1303 151 return false;
1304
1305
3/3
✓ Branch 0 (4→5) taken 70 times.
✓ Branch 1 (4→8) taken 193 times.
✓ Branch 2 (4→9) taken 41 times.
304 switch (stmt->type) {
1306 70 case STMT_VERDICT:
1307
2/2
✓ Branch 0 (5→6) taken 12 times.
✓ Branch 1 (5→7) taken 58 times.
70 if (stmt->expr->etype == EXPR_MAP)
1308 12 return true;
1309 58 break;
1310 193 case STMT_EXPRESSION:
1311 case STMT_NAT:
1312 193 return true;
1313 41 default:
1314 41 break;
1315 }
1316
1317 99 return false;
1318 }
1319
1320 155 static bool rules_eq(const struct optimize_ctx *ctx, int i, int j)
1321 {
1322 155 uint32_t k, mergeable = 0;
1323
1324
2/2
✓ Branch 0 (10→3) taken 455 times.
✓ Branch 1 (10→11) taken 75 times.
530 for (k = 0; k < ctx->num_stmts; k++) {
1325
2/2
✓ Branch 0 (4→5) taken 205 times.
✓ Branch 1 (4→6) taken 250 times.
455 if (stmt_is_mergeable(ctx->stmt_matrix[i][k]))
1326 205 mergeable++;
1327
1328
2/2
✓ Branch 0 (7→8) taken 80 times.
✓ Branch 1 (7→9) taken 375 times.
455 if (!stmt_type_eq(ctx->stmt_matrix[i][k], ctx->stmt_matrix[j][k]))
1329 80 return false;
1330 }
1331
1332
2/2
✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→13) taken 74 times.
75 if (mergeable == 0)
1333 1 return false;
1334
1335 74 return true;
1336 }
1337
1338 74 static int chain_optimize(struct nft_ctx *nft, struct list_head *rules)
1339 {
1340 struct optimize_ctx *ctx;
1341 74 uint32_t num_merges = 0;
1342 struct merge *merge;
1343 uint32_t i, j, m, k;
1344 struct rule *rule;
1345 int ret;
1346
1347 74 ctx = xzalloc(sizeof(*ctx));
1348
1349 /* Step 1: collect statements in rules */
1350
2/2
✓ Branch 0 (8→4) taken 210 times.
✓ Branch 1 (8→9) taken 74 times.
284 list_for_each_entry(rule, rules, list) {
1351 210 ret = rule_collect_stmts(ctx, rule);
1352
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 210 times.
210 if (ret < 0)
1353 ✗ goto err;
1354
1355 210 ctx->num_rules++;
1356 }
1357
1358 74 ctx->rule = xzalloc(sizeof(*ctx->rule) * ctx->num_rules);
1359 74 ctx->stmt_matrix = xzalloc(sizeof(*ctx->stmt_matrix) * ctx->num_rules);
1360
2/2
✓ Branch 0 (14→12) taken 210 times.
✓ Branch 1 (14→15) taken 74 times.
284 for (i = 0; i < ctx->num_rules; i++)
1361 210 ctx->stmt_matrix[i] = xzalloc_array(MAX_STMTS,
1362 sizeof(**ctx->stmt_matrix));
1363
1364 74 merge = xzalloc(sizeof(*merge) * ctx->num_rules);
1365
1366 /* Step 2: Build matrix of statements */
1367 74 i = 0;
1368
2/2
✓ Branch 0 (19→17) taken 210 times.
✓ Branch 1 (19→20) taken 74 times.
284 list_for_each_entry(rule, rules, list)
1369 210 rule_build_stmt_matrix_stmts(ctx, rule, &i);
1370
1371 /* Step 3: Look for common selectors for possible rule mergers */
1372
2/2
✓ Branch 0 (36→21) taken 136 times.
✓ Branch 1 (36→37) taken 54 times.
190 for (i = 0; i < ctx->num_rules; i++) {
1373
2/2
✓ Branch 0 (31→22) taken 155 times.
✓ Branch 1 (31→32) taken 55 times.
210 for (j = i + 1; j < ctx->num_rules; j++) {
1374
2/2
✓ Branch 0 (23→24) taken 81 times.
✓ Branch 1 (23→27) taken 74 times.
155 if (!rules_eq(ctx, i, j)) {
1375
2/2
✓ Branch 0 (24→25) taken 19 times.
✓ Branch 1 (24→26) taken 62 times.
81 if (merge[num_merges].num_rules > 0)
1376 19 num_merges++;
1377
1378 81 i = j - 1;
1379 81 break;
1380 }
1381
2/2
✓ Branch 0 (27→28) taken 35 times.
✓ Branch 1 (27→29) taken 39 times.
74 if (merge[num_merges].num_rules > 0) {
1382 35 merge[num_merges].num_rules++;
1383 } else {
1384 39 merge[num_merges].rule_from = i;
1385 39 merge[num_merges].num_rules = 2;
1386 }
1387 }
1388
4/4
✓ Branch 0 (32→33) taken 55 times.
✓ Branch 1 (32→35) taken 81 times.
✓ Branch 2 (33→34) taken 20 times.
✓ Branch 3 (33→35) taken 35 times.
136 if (j == ctx->num_rules && merge[num_merges].num_rules > 0) {
1389 20 num_merges++;
1390 20 break;
1391 }
1392 }
1393
1394 /* Step 4: Invalidate merge in case of duplicated keys in set/map. */
1395
2/2
✓ Branch 0 (59→38) taken 39 times.
✓ Branch 1 (59→60) taken 74 times.
113 for (k = 0; k < num_merges; k++) {
1396 uint32_t r1, r2;
1397
1398 39 i = merge[k].rule_from;
1399
1400
2/2
✓ Branch 0 (57→39) taken 113 times.
✓ Branch 1 (57→58) taken 39 times.
152 for (r1 = i; r1 < i + merge[k].num_rules; r1++) {
1401
2/2
✓ Branch 0 (55→40) taken 217 times.
✓ Branch 1 (55→56) taken 113 times.
330 for (r2 = r1 + 1; r2 < i + merge[k].num_rules; r2++) {
1402 217 bool match_same_value = true, match_seen = false;
1403
1404
2/2
✓ Branch 0 (50→41) taken 829 times.
✓ Branch 1 (50→51) taken 217 times.
1046 for (m = 0; m < ctx->num_stmts; m++) {
1405
2/2
✓ Branch 0 (41→42) taken 246 times.
✓ Branch 1 (41→43) taken 583 times.
829 if (!ctx->stmt_matrix[r1][m])
1406 246 continue;
1407
1408
2/2
✓ Branch 0 (43→44) taken 284 times.
✓ Branch 1 (43→48) taken 299 times.
583 switch (ctx->stmt_matrix[r1][m]->type) {
1409 284 case STMT_EXPRESSION:
1410 284 match_seen = true;
1411
2/2
✓ Branch 0 (45→46) taken 254 times.
✓ Branch 1 (45→47) taken 30 times.
284 if (!__expr_cmp(ctx->stmt_matrix[r1][m]->expr->right,
1412 284 ctx->stmt_matrix[r2][m]->expr->right))
1413 254 match_same_value = false;
1414 284 break;
1415 299 default:
1416 299 break;
1417 }
1418 }
1419
4/4
✓ Branch 0 (51→52) taken 214 times.
✓ Branch 1 (51→54) taken 3 times.
✓ Branch 2 (52→53) taken 8 times.
✓ Branch 3 (52→54) taken 206 times.
217 if (match_seen && match_same_value)
1420 8 merge[k].skip = true;
1421 }
1422 }
1423 }
1424
1425 /* Step 5: Infer how to merge the candidate rules */
1426
2/2
✓ Branch 0 (76→61) taken 39 times.
✓ Branch 1 (76→77) taken 74 times.
113 for (k = 0; k < num_merges; k++) {
1427
2/2
✓ Branch 0 (61→62) taken 2 times.
✓ Branch 1 (61→63) taken 37 times.
39 if (merge[k].skip)
1428 2 continue;
1429
1430 37 i = merge[k].rule_from;
1431
1432
2/2
✓ Branch 0 (73→64) taken 166 times.
✓ Branch 1 (73→74) taken 37 times.
203 for (m = 0; m < ctx->num_stmts; m++) {
1433
2/2
✓ Branch 0 (64→65) taken 69 times.
✓ Branch 1 (64→66) taken 97 times.
166 if (!ctx->stmt_matrix[i][m])
1434 69 continue;
1435
3/3
✓ Branch 0 (66→67) taken 54 times.
✓ Branch 1 (66→68) taken 25 times.
✓ Branch 2 (66→71) taken 18 times.
97 switch (ctx->stmt_matrix[i][m]->type) {
1436 54 case STMT_EXPRESSION:
1437 54 merge[k].stmt[merge[k].num_stmts++] = m;
1438 54 break;
1439 25 case STMT_VERDICT:
1440
2/2
✓ Branch 0 (68→69) taken 3 times.
✓ Branch 1 (68→70) taken 22 times.
25 if (ctx->stmt_matrix[i][m]->expr->etype == EXPR_MAP)
1441 3 merge[k].stmt[merge[k].num_stmts++] = m;
1442 25 break;
1443 18 default:
1444 18 break;
1445 }
1446 }
1447
1448 37 j = merge[k].num_rules - 1;
1449 37 merge_rules(ctx, i, i + j, &merge[k], &nft->output);
1450 }
1451 74 ret = 0;
1452
2/2
✓ Branch 0 (79→78) taken 210 times.
✓ Branch 1 (79→80) taken 74 times.
284 for (i = 0; i < ctx->num_rules; i++)
1453 210 free(ctx->stmt_matrix[i]);
1454
1455 74 free(ctx->stmt_matrix);
1456 74 free(merge);
1457 74 err:
1458
2/2
✓ Branch 0 (84→82) taken 204 times.
✓ Branch 1 (84→85) taken 74 times.
278 for (i = 0; i < ctx->num_stmts; i++)
1459 204 stmt_free(ctx->stmt[i]);
1460
1461 74 free(ctx->rule);
1462 74 free(ctx);
1463
1464 74 return ret;
1465 }
1466
1467 27 static int cmd_optimize(struct nft_ctx *nft, struct cmd *cmd)
1468 {
1469 struct table *table;
1470 struct chain *chain;
1471 27 int ret = 0;
1472
1473
2/2
✓ Branch 0 (2→3) taken 26 times.
✓ Branch 1 (2→12) taken 1 times.
27 switch (cmd->obj) {
1474 26 case CMD_OBJ_TABLE:
1475 26 table = cmd->table;
1476
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 26 times.
26 if (!table)
1477 ✗ break;
1478
1479
2/2
✓ Branch 0 (10→6) taken 74 times.
✓ Branch 1 (10→11) taken 26 times.
100 list_for_each_entry(chain, &table->chains, list) {
1480
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 74 times.
74 if (chain->flags & CHAIN_F_HW_OFFLOAD)
1481 ✗ continue;
1482
1483 74 chain_optimize(nft, &chain->rules);
1484 }
1485 26 break;
1486 1 default:
1487 1 break;
1488 }
1489
1490 27 return ret;
1491 }
1492
1493 25 int nft_optimize(struct nft_ctx *nft, struct list_head *cmds)
1494 {
1495 struct cmd *cmd;
1496 25 int ret = 0;
1497
1498
2/2
✓ Branch 0 (8→3) taken 28 times.
✓ Branch 1 (8→9) taken 25 times.
53 list_for_each_entry(cmd, cmds, list) {
1499
2/2
✓ Branch 0 (3→4) taken 27 times.
✓ Branch 1 (3→6) taken 1 times.
28 switch (cmd->op) {
1500 27 case CMD_ADD:
1501 27 ret = cmd_optimize(nft, cmd);
1502 27 break;
1503 1 default:
1504 1 break;
1505 }
1506 }
1507
1508 25 return ret;
1509 }
1510