-
Notifications
You must be signed in to change notification settings - Fork 192
/
meter_table.c
350 lines (278 loc) · 11.7 KB
/
meter_table.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* Copyright (c) 2012, Applistar, Vietnam
* Copyright (c) 2012, CPqD, Brazil
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Ericsson Research nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
#include <sys/types.h>
#include "compiler.h"
#include "meter_table.h"
#include "datapath.h"
#include "dp_actions.h"
#include "hmap.h"
#include "list.h"
#include "packet.h"
#include "util.h"
#include "openflow/openflow.h"
#include "oflib/ofl.h"
#include "oflib/ofl-messages.h"
#include "vlog.h"
#define LOG_MODULE VLM_meter_t
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
/* Creates a meter table. */
struct meter_table *
meter_table_create(struct datapath *dp) {
struct meter_table *table;
table = xmalloc(sizeof(struct meter_table));
table->dp = dp;
table->entries_num = 0;
hmap_init(&table->meter_entries);
table->features = xmalloc(sizeof(struct ofl_meter_features));
table->features->max_meter = DEFAULT_MAX_METER;
table->features->max_bands = DEFAULT_MAX_BAND_PER_METER;
table->features->max_color = DEFAULT_MAX_METER_COLOR;
table->features->capabilities = OFPMF_KBPS | OFPMF_BURST | OFPMF_STATS; /* Rate value in kb/s (kilo-bit per second).
Do burst size. Collect statistics.*/
table->features->band_types = 1;
return table;
}
void
meter_table_destroy(struct meter_table *table) {
struct meter_entry *entry, *next;
HMAP_FOR_EACH_SAFE(entry, next, struct meter_entry, node, &table->meter_entries) {
meter_entry_destroy(entry);
}
///////////////////////////free features
free(table);
}
/* Returns the meter with the given ID. */
struct meter_entry *
meter_table_find(struct meter_table *table, uint32_t meter_id) {
struct hmap_node *hnode;
hnode = hmap_first_with_hash(&table->meter_entries, meter_id);
if (hnode == NULL) {
return NULL;
}
return CONTAINER_OF(hnode, struct meter_entry, node);
}
void
meter_table_apply(struct meter_table *table, struct packet **packet, uint32_t meter_id) {
struct meter_entry *entry;
entry = meter_table_find(table, meter_id);
if (entry == NULL) {
VLOG_WARN_RL(LOG_MODULE, &rl, "Trying to execute non-existing meter (%u).", meter_id);
return;
}
meter_entry_apply(entry, packet);
}
/* Handles meter_mod messages with ADD command. */
static ofl_err
meter_table_add(struct meter_table *table, struct ofl_msg_meter_mod *mod) {
struct meter_entry *entry;
if (hmap_first_with_hash(&table->meter_entries, mod->meter_id) != NULL) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_METER_EXISTS);
}
if (table->entries_num == DEFAULT_MAX_METER) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_OUT_OF_METERS);
}
if (table->bands_num + mod->meter_bands_num > METER_TABLE_MAX_BANDS) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_OUT_OF_BANDS);
}
entry = meter_entry_create(table->dp, table, mod);
hmap_insert(&table->meter_entries, &entry->node, entry->stats->meter_id);
table->entries_num++;
table->bands_num += entry->stats->meter_bands_num;
ofl_msg_free_meter_mod(mod, false);
return 0;
}
/* Handles meter_mod messages with MODIFY command. */
static ofl_err
meter_table_modify(struct meter_table *table, struct ofl_msg_meter_mod *mod) {
struct meter_entry *entry, *new_entry;
entry = meter_table_find(table, mod->meter_id);
if (entry == NULL) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_UNKNOWN_METER);
}
if (table->bands_num - entry->config->meter_bands_num + mod->meter_bands_num > METER_TABLE_MAX_BANDS) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_OUT_OF_BANDS);
}
new_entry = meter_entry_create(table->dp, table, mod);
hmap_remove(&table->meter_entries, &entry->node);
hmap_insert_fast(&table->meter_entries, &new_entry->node, mod->meter_id);
table->bands_num = table->bands_num - entry->config->meter_bands_num + new_entry->stats->meter_bands_num;
/* keep flow references from old meter entry */
list_replace(&new_entry->flow_refs, &entry->flow_refs);
list_init(&entry->flow_refs);
new_entry->stats->flow_count = entry->stats->flow_count;
new_entry->stats->packet_in_count = entry->stats->packet_in_count;
new_entry->stats->byte_in_count = entry->stats->byte_in_count;
new_entry->stats->duration_sec = entry->stats->duration_sec;
new_entry->stats->duration_nsec = entry->stats->duration_nsec;
meter_entry_destroy(entry);
ofl_msg_free_meter_mod(mod, false);
return 0;
}
/* Handles meter_mod messages with DELETE command. */
static ofl_err
meter_table_delete(struct meter_table *table, struct ofl_msg_meter_mod *mod) {
if (mod->meter_id == OFPM_ALL) {
struct meter_entry *entry, *next;
HMAP_FOR_EACH_SAFE(entry, next, struct meter_entry, node, &table->meter_entries) {
meter_entry_destroy(entry);
}
hmap_destroy(&table->meter_entries);
hmap_init(&table->meter_entries);
table->entries_num = 0;
table->bands_num = 0;
ofl_msg_free_meter_mod(mod, false);
return 0;
} else {
struct meter_entry *entry;
entry = meter_table_find(table, mod->meter_id);
if (entry != NULL) {
table->entries_num--;
table->bands_num -= entry->stats->meter_bands_num;
hmap_remove(&table->meter_entries, &entry->node);
meter_entry_destroy(entry);
}
ofl_msg_free_meter_mod(mod, false);
return 0;
}
}
ofl_err
meter_table_handle_meter_mod(struct meter_table *table, struct ofl_msg_meter_mod *mod,
const struct sender *sender) {
if(sender->remote->role == OFPCR_ROLE_SLAVE)
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_IS_SLAVE);
switch (mod->command) {
case (OFPMC_ADD): {
return meter_table_add(table, mod);
}
case (OFPMC_MODIFY): {
return meter_table_modify(table, mod);
}
case (OFPMC_DELETE): {
return meter_table_delete(table, mod);
}
default: {
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
}
}
}
ofl_err
meter_table_handle_stats_request_meter(struct meter_table *table,
struct ofl_msg_multipart_meter_request *msg,
const struct sender *sender UNUSED) {
struct meter_entry *entry;
if (msg->meter_id == OFPM_ALL) {
entry = NULL;
} else {
entry = meter_table_find(table, msg->meter_id);
if (entry == NULL) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_UNKNOWN_METER);
}
}
{
struct ofl_msg_multipart_reply_meter reply =
{{{.type = OFPT_MULTIPART_REPLY},
.type = OFPMP_METER, .flags = 0x0000},
.stats_num = msg->meter_id == OFPM_ALL ? table->entries_num : 1,
.stats = xmalloc(sizeof(struct ofl_meter_stats *) * (msg->meter_id == OFPM_ALL ? table->entries_num : 1))
};
if (msg->meter_id == OFPM_ALL) {
struct meter_entry *e;
size_t i = 0;
HMAP_FOR_EACH(e, struct meter_entry, node, &table->meter_entries) {
meter_entry_update(e);
reply.stats[i] = e->stats;
i++;
}
} else {
meter_entry_update(entry);
reply.stats[0] = entry->stats;
}
dp_send_message(table->dp, (struct ofl_msg_header *)&reply, sender);
free(reply.stats);
ofl_msg_free((struct ofl_msg_header *)msg, table->dp->exp);
return 0;
}
}
ofl_err
meter_table_handle_stats_request_meter_conf(struct meter_table *table,
struct ofl_msg_multipart_meter_request *msg UNUSED,
const struct sender *sender) {
struct meter_entry *entry;
struct ofl_msg_multipart_reply_meter_conf reply;
if (msg->meter_id == OFPM_ALL) {
entry = NULL;
} else {
entry = meter_table_find(table, msg->meter_id);
if (entry == NULL) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_UNKNOWN_METER);
}
}
reply.header.header.type = OFPT_MULTIPART_REPLY;
reply.header.type = OFPMP_METER_CONFIG;
reply.header.flags = 0x0000;
reply.stats_num = table->entries_num;
reply.stats = xmalloc(sizeof(struct ofl_meter_config *) *
(msg->meter_id == OFPM_ALL ? table->entries_num : 1));
if (msg->meter_id == OFPM_ALL) {
struct meter_entry *e;
size_t i = 0;
HMAP_FOR_EACH(e, struct meter_entry, node, &table->meter_entries) {
reply.stats[i] = e->config;
i++;
}
} else {
reply.stats[0] = entry->config;
}
dp_send_message(table->dp, (struct ofl_msg_header *)&reply, sender);
free(reply.stats);
ofl_msg_free((struct ofl_msg_header *)msg, table->dp->exp);
return 0;
}
ofl_err
meter_table_handle_features_request(struct meter_table *table,
struct ofl_msg_multipart_request_header *msg UNUSED,
const struct sender *sender) {
struct ofl_msg_multipart_reply_meter_features reply =
{{{.type = OFPT_MULTIPART_REPLY},
.type = OFPMP_METER_FEATURES, .flags = 0x0000},
.features = table->features
};
dp_send_message(table->dp, (struct ofl_msg_header *)&reply, sender);
ofl_msg_free((struct ofl_msg_header *)msg, table->dp->exp);
return 0;
}
void
meter_table_add_tokens(struct meter_table *table){
struct meter_entry *entry;
HMAP_FOR_EACH(entry, struct meter_entry, node, &table->meter_entries){
refill_bucket(entry);
}
}