/
chain.c
249 lines (223 loc) · 7.9 KB
/
chain.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
* Junior University
*
* We are making the OpenFlow specification and associated documentation
* (Software) available for public use and benefit with the expectation
* that others will use, modify and enhance the Software and contribute
* those enhancements back to the community. However, since we would
* like to make the Software available for broadest use, with as few
* restrictions as possible permission is hereby granted, free of
* charge, to any person obtaining a copy of this Software to deal in
* the Software under the copyrights without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* The name and trademarks of copyright holder(s) may NOT be used in
* advertising or publicity pertaining to the Software or any
* derivatives without specific, written prior permission.
*/
#include <config.h>
#include "chain.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include "switch-flow.h"
#include "table.h"
#define THIS_MODULE VLM_chain
#include "vlog.h"
/* Attempts to append 'table' to the set of tables in 'chain'. Returns 0 or
* negative error. If 'table' is null it is assumed that table creation failed
* due to out-of-memory. */
static int add_table(struct sw_chain *chain, struct sw_table *table, int emerg)
{
if (table == NULL)
return -ENOMEM;
if (chain->n_tables >= CHAIN_MAX_TABLES) {
VLOG_ERR("too many tables in chain\n");
table->destroy(table);
return -ENOBUFS;
}
if (emerg)
chain->emerg_table = table;
else
chain->tables[chain->n_tables++] = table;
return 0;
}
/* Creates and returns a new chain. Returns NULL if the chain cannot be
* created. */
struct sw_chain *chain_create(struct datapath *dp)
{
struct sw_chain *chain = calloc(1, sizeof *chain);
if (chain == NULL)
return NULL;
chain->dp = dp;
if (add_table(chain, table_hash2_create(0x1EDC6F41, TABLE_HASH_MAX_FLOWS,
0x741B8CD7, TABLE_HASH_MAX_FLOWS),
0)
|| add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS), 0)
|| add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS), 1)) {
chain_destroy(chain);
return NULL;
}
return chain;
}
/* Searches 'chain' for a flow matching 'key', which must not have any wildcard
* fields. Returns the flow if successful, otherwise a null pointer. */
struct sw_flow *
chain_lookup(struct sw_chain *chain, const struct sw_flow_key *key, int emerg)
{
int i;
assert(!key->wildcards);
if (emerg) {
struct sw_table *t = chain->emerg_table;
struct sw_flow *flow = t->lookup(t, key);
t->n_lookup++;
if (flow) {
t->n_matched++;
return flow;
}
} else {
for (i = 0; i < chain->n_tables; i++) {
struct sw_table *t = chain->tables[i];
struct sw_flow *flow = t->lookup(t, key);
t->n_lookup++;
if (flow) {
t->n_matched++;
return flow;
}
}
}
return NULL;
}
/* Inserts 'flow' into 'chain', replacing any duplicate flow. Returns 0 if
* successful or a negative error.
*
* If successful, 'flow' becomes owned by the chain, otherwise it is retained
* by the caller. */
int
chain_insert(struct sw_chain *chain, struct sw_flow *flow, int emerg)
{
int i;
if (emerg) {
struct sw_table *t = chain->emerg_table;
if (t->insert(t, flow))
return 0;
} else {
for (i = 0; i < chain->n_tables; i++) {
struct sw_table *t = chain->tables[i];
if (t->insert(t, flow))
return 0;
}
}
return -ENOBUFS;
}
/* Modifies actions in 'chain' that match 'key'. If 'strict' set, wildcards
* and priority must match. Returns the number of flows that were modified.
*
* Expensive in the general case as currently implemented, since it requires
* iterating through the entire contents of each table for keys that contain
* wildcards. Relatively cheap for fully specified keys. */
int
chain_modify(struct sw_chain *chain, const struct sw_flow_key *key,
uint16_t priority, int strict,
const struct ofp_action_header *actions, size_t actions_len,
int emerg)
{
int count = 0;
int i;
if (emerg) {
struct sw_table *t = chain->emerg_table;
count += t->modify(t, key, priority, strict, actions, actions_len);
} else {
for (i = 0; i < chain->n_tables; i++) {
struct sw_table *t = chain->tables[i];
count += t->modify(t, key, priority, strict, actions, actions_len);
}
}
return count;
}
/* Checks whether the chain has an entry with the same priority which conflicts
* with 'key'. If 'strict' set, wildcards should also match. If 'strict' is not
* set, comparison is done 'module wildcards'.
*
* Returns 'true' if such an entry exists, 'false' otherwise. */
int
chain_has_conflict(struct sw_chain *chain, const struct sw_flow_key *key,
uint16_t priority, int strict)
{
int i;
for (i = 0; i < chain->n_tables; i++) {
struct sw_table *t = chain->tables[i];
if (t->has_conflict(t, key, priority, strict)) {
return true;
}
}
return false;
}
/* Deletes from 'chain' any and all flows that match 'key'. If 'out_port'
* is not OFPP_NONE, then matching entries must have that port as an
* argument for an output action. If 'strict" is set, then wildcards and
* priority must match. Returns the number of flows that were deleted.
*
* Expensive in the general case as currently implemented, since it requires
* iterating through the entire contents of each table for keys that contain
* wildcards. Relatively cheap for fully specified keys. */
int
chain_delete(struct sw_chain *chain, const struct sw_flow_key *key,
uint16_t out_port, uint16_t priority, int strict, int emerg)
{
int count = 0;
int i;
if (emerg) {
struct sw_table *t = chain->emerg_table;
count += t->delete(chain->dp, t, key, out_port, priority, strict);
} else {
for (i = 0; i < chain->n_tables; i++) {
struct sw_table *t = chain->tables[i];
count += t->delete(chain->dp, t, key, out_port, priority, strict);
}
}
return count;
}
/* Deletes timed-out flow entries from all the tables in 'chain' and appends
* the deleted flows to 'deleted'.
*
* Expensive as currently implemented, since it iterates through the entire
* contents of each table. */
void
chain_timeout(struct sw_chain *chain, struct list *deleted)
{
int i;
for (i = 0; i < chain->n_tables; i++) {
struct sw_table *t = chain->tables[i];
t->timeout(t, deleted);
}
}
/* Destroys 'chain', which must not have any users. */
void
chain_destroy(struct sw_chain *chain)
{
int i;
struct sw_table *t;
for (i = 0; i < chain->n_tables; i++) {
t = chain->tables[i];
t->destroy(t);
}
t = chain->emerg_table;
t->destroy(t);
free(chain);
}