summaryrefslogtreecommitdiffstats
path: root/src/cache.c
blob: 532ef425906ad02aa56eb0c63365eace8076bde5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2019 Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 (or any
 * later) as published by the Free Software Foundation.
 */

#include <expression.h>
#include <statement.h>
#include <rule.h>
#include <erec.h>
#include <utils.h>

static unsigned int evaluate_cache_add(struct cmd *cmd)
{
	unsigned int completeness = CMD_INVALID;

	switch (cmd->obj) {
	case CMD_OBJ_SETELEM:
	case CMD_OBJ_SET:
	case CMD_OBJ_CHAIN:
	case CMD_OBJ_FLOWTABLE:
		completeness = cmd->op;
		break;
	case CMD_OBJ_RULE:
		if (cmd->handle.index.id)
			completeness = CMD_LIST;
		break;
	default:
		break;
	}

	return completeness;
}

static unsigned int evaluate_cache_del(struct cmd *cmd)
{
	unsigned int completeness = CMD_INVALID;

	switch (cmd->obj) {
	case CMD_OBJ_SETELEM:
		completeness = cmd->op;
		break;
	default:
		break;
	}

	return completeness;
}

static unsigned int evaluate_cache_flush(struct cmd *cmd)
{
	unsigned int completeness = CMD_INVALID;

	switch (cmd->obj) {
	case CMD_OBJ_SET:
	case CMD_OBJ_MAP:
	case CMD_OBJ_METER:
		completeness = cmd->op;
		break;
	default:
		break;
	}

	return completeness;
}

static unsigned int evaluate_cache_rename(struct cmd *cmd)
{
	unsigned int completeness = CMD_INVALID;

	switch (cmd->obj) {
	case CMD_OBJ_CHAIN:
		completeness = cmd->op;
		break;
	default:
		break;
	}

	return completeness;
}

int cache_evaluate(struct nft_ctx *nft, struct list_head *cmds)
{
	unsigned int echo_completeness = CMD_INVALID;
	unsigned int completeness = CMD_INVALID;
	struct cmd *cmd;

	list_for_each_entry(cmd, cmds, list) {
		switch (cmd->op) {
		case CMD_ADD:
		case CMD_INSERT:
		case CMD_REPLACE:
			if (nft_output_echo(&nft->output))
				echo_completeness = cmd->op;

			/* Fall through */
		case CMD_CREATE:
			completeness = evaluate_cache_add(cmd);
			break;
		case CMD_DELETE:
			completeness = evaluate_cache_del(cmd);
			break;
		case CMD_GET:
		case CMD_LIST:
		case CMD_RESET:
		case CMD_EXPORT:
		case CMD_MONITOR:
			completeness = cmd->op;
			break;
		case CMD_FLUSH:
			completeness = evaluate_cache_flush(cmd);
			break;
		case CMD_RENAME:
			completeness = evaluate_cache_rename(cmd);
			break;
		case CMD_DESCRIBE:
		case CMD_IMPORT:
			break;
		default:
			break;
		}
	}

	return max(completeness, echo_completeness);
}