-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcc_of_action.c
421 lines (303 loc) · 11.9 KB
/
cc_of_action.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* cc_of_msg_action functions.
*
* Author: qiang wang <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "cc_of_msg_action.h"
openflow_actions *
create_actions() {
openflow_actions *actions;
log_debug_for_cc( "Creating an empty actions list." );
actions = ( openflow_actions * ) xmalloc( sizeof( openflow_actions ) );
if ( create_list( &actions->list ) == false ) {
//assert( 0 );
}
actions->n_actions = 0;
return actions;
}
bool
delete_actions( openflow_actions *actions ) {
list_element *element;
log_debug_for_cc( "Deleting an actions list." );
//assert( actions != NULL );
log_debug_for_cc( "# of actions = %d.", actions->n_actions );
element = actions->list;
while ( element != NULL ) {
free( element->data );
element = element->next;
}
delete_list( actions->list );
free( actions );
return true;
}
bool
append_action_output( openflow_actions *actions, const uint16_t port, const uint16_t max_len ) {
bool ret;
struct ofp_action_output *action_output;
log_debug_for_cc( "Appending an output action ( port = %u, max_len = %u ).", port, max_len );
//assert( actions != NULL );
action_output = ( struct ofp_action_output * ) malloc( sizeof( struct ofp_action_output ) );
action_output->type = OFPAT_OUTPUT;
action_output->len = sizeof( struct ofp_action_output );
action_output->port = port;
action_output->max_len = max_len;
ret = append_to_tail( &actions->list, ( void * ) action_output );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_set_vlan_vid( openflow_actions *actions, const uint16_t vlan_vid ) {
bool ret;
struct ofp_action_vlan_vid *action_vlan_vid;
log_debug_for_cc( "Appending a set vlan action ( vlan_vid = %#x ).", vlan_vid );
//assert( actions != NULL );
//assert( ( vlan_vid & ~VLAN_VID_MASK ) == 0 );
action_vlan_vid = ( struct ofp_action_vlan_vid * ) malloc( sizeof( struct ofp_action_vlan_vid ) );
action_vlan_vid->type = OFPAT_SET_VLAN_VID;
action_vlan_vid->len = sizeof( struct ofp_action_vlan_vid );
action_vlan_vid->vlan_vid = vlan_vid;
ret = append_to_tail( &actions->list, ( void * ) action_vlan_vid );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_set_vlan_pcp( openflow_actions *actions, const uint8_t vlan_pcp ) {
bool ret;
struct ofp_action_vlan_pcp *action_vlan_pcp;
log_debug_for_cc( "Appending a set vlan pcp action ( vlan_pcp = %#x ).", vlan_pcp );
//assert( actions != NULL );
//assert( ( vlan_pcp & ~VLAN_PCP_MASK ) == 0 );
action_vlan_pcp = ( struct ofp_action_vlan_pcp * ) malloc( sizeof( struct ofp_action_vlan_pcp ) );
action_vlan_pcp->type = OFPAT_SET_VLAN_PCP;
action_vlan_pcp->len = sizeof( struct ofp_action_vlan_pcp );
action_vlan_pcp->vlan_pcp = vlan_pcp;
ret = append_to_tail( &actions->list, ( void * ) action_vlan_pcp );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_strip_vlan( openflow_actions *actions ) {
bool ret;
struct ofp_action_header *action_strip_vlan;
log_debug_for_cc( "Appending a strip vlan action." );
//assert( actions != NULL );
action_strip_vlan = ( struct ofp_action_header * ) malloc( sizeof( struct ofp_action_header ) );
action_strip_vlan->type = OFPAT_STRIP_VLAN;
action_strip_vlan->len = sizeof( struct ofp_action_header );
ret = append_to_tail( &actions->list, ( void * ) action_strip_vlan );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
static bool
append_action_set_dl_addr( openflow_actions *actions, const uint16_t type,
const uint8_t hw_addr[ OFP_ETH_ALEN ] ) {
bool ret;
struct ofp_action_dl_addr *action_dl_addr;
log_debug_for_cc( "Appending a set dl_src/dl_dst action ( type = %#x, hw_addr = %02x:%02x:%02x:%02x:%02x:%02x ).",
type, hw_addr[ 0 ], hw_addr[ 1 ], hw_addr[ 2 ], hw_addr[ 3 ], hw_addr[ 4 ], hw_addr[ 5 ] );
//assert( actions != NULL );
action_dl_addr = ( struct ofp_action_dl_addr * ) malloc( sizeof( struct ofp_action_dl_addr ) );
action_dl_addr->type = type;
action_dl_addr->len = sizeof( struct ofp_action_dl_addr );
memcpy( action_dl_addr->dl_addr, hw_addr, OFP_ETH_ALEN );
ret = append_to_tail( &actions->list, ( void * ) action_dl_addr );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_set_dl_src( openflow_actions *actions, const uint8_t hw_addr[ OFP_ETH_ALEN ] ) {
log_debug_for_cc( "Appending a set dl_src action ( hw_addr ");
//assert( actions != NULL );
return append_action_set_dl_addr( actions, OFPAT_SET_DL_SRC, hw_addr );
}
bool
append_action_set_dl_dst( openflow_actions *actions, const uint8_t hw_addr[ OFP_ETH_ALEN ] ) {
log_debug_for_cc( "Appending a set dl_dst action ( hw_addr = %02x:%02x:%02x:%02x:%02x:%02x ).",
hw_addr[ 0 ], hw_addr[ 1 ], hw_addr[ 2 ], hw_addr[ 3 ], hw_addr[ 4 ], hw_addr[ 5 ] );
//assert( actions != NULL );
return append_action_set_dl_addr( actions, OFPAT_SET_DL_DST, hw_addr );
}
static bool
append_action_set_nw_addr( openflow_actions *actions, const uint16_t type, const uint32_t nw_addr ) {
bool ret;
char addr_str[ 16 ];
struct in_addr addr;
struct ofp_action_nw_addr *action_nw_addr;
addr.s_addr = htonl( nw_addr );
memset( addr_str, '\0', sizeof( addr_str ) );
inet_ntop( AF_INET, &addr, addr_str, sizeof( addr_str ) );
log_debug_for_cc( "Appending a set nw_src/nw_dst action ( type = %#x, nw_addr = %s ).", type, addr_str );
//assert( actions != NULL );
action_nw_addr = ( struct ofp_action_nw_addr * ) malloc( sizeof( struct ofp_action_nw_addr ) );
action_nw_addr->type = type;
action_nw_addr->len = sizeof( struct ofp_action_nw_addr );
action_nw_addr->nw_addr = nw_addr;
ret = append_to_tail( &actions->list, ( void * ) action_nw_addr );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_set_nw_src( openflow_actions *actions, const uint32_t nw_addr ) {
char addr_str[ 16 ];
struct in_addr addr;
addr.s_addr = htonl( nw_addr );
memset( addr_str, '\0', sizeof( addr_str ) );
inet_ntop( AF_INET, &addr, addr_str, sizeof( addr_str ) );
log_debug_for_cc( "Appending a set nw_src action ( nw_addr = %s ).", addr_str );
//assert( actions != NULL );
return append_action_set_nw_addr( actions, OFPAT_SET_NW_SRC, nw_addr );
}
bool
append_action_set_nw_dst( openflow_actions *actions, const uint32_t nw_addr ) {
char addr_str[ 16 ];
struct in_addr addr;
addr.s_addr = htonl( nw_addr );
memset( addr_str, '\0', sizeof( addr_str ) );
inet_ntop( AF_INET, &addr, addr_str, sizeof( addr_str ) );
log_debug_for_cc( "Appending a set nw_dst action ( nw_addr = %s ).", addr_str );
//assert( actions != NULL );
return append_action_set_nw_addr( actions, OFPAT_SET_NW_DST, nw_addr );
}
bool
append_action_set_nw_tos( openflow_actions *actions, const uint8_t nw_tos ) {
bool ret;
struct ofp_action_nw_tos *action_nw_tos;
log_debug_for_cc( "Appending a set nw_tos action ( nw_tos = %#x ).", nw_tos );
//assert( actions != NULL );
//assert( ( nw_tos & ~NW_TOS_MASK ) == 0 );
action_nw_tos = ( struct ofp_action_nw_tos * ) malloc( sizeof( struct ofp_action_nw_tos ) );
action_nw_tos->type = OFPAT_SET_NW_TOS;
action_nw_tos->len = sizeof( struct ofp_action_nw_tos );
action_nw_tos->nw_tos = nw_tos;
ret = append_to_tail( &actions->list, ( void * ) action_nw_tos );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
static bool
append_action_set_tp_port( openflow_actions *actions, const uint16_t type, const uint16_t tp_port ) {
bool ret;
struct ofp_action_tp_port *action_tp_port;
log_debug_for_cc( "Appending a set tp_src/tp_dst action ( type = %#x, tp_port = %u ).", type, tp_port );
//assert( actions != NULL );
action_tp_port = ( struct ofp_action_tp_port * ) malloc( sizeof( struct ofp_action_tp_port ) );
action_tp_port->type = type;
action_tp_port->len = sizeof( struct ofp_action_tp_port );
action_tp_port->tp_port = tp_port;
ret = append_to_tail( &actions->list, ( void * ) action_tp_port );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_set_tp_src( openflow_actions *actions, const uint16_t tp_port ) {
log_debug_for_cc( "Appending a set tp_src action ( tp_port = %u ).", tp_port );
//assert( actions != NULL );
return append_action_set_tp_port( actions, OFPAT_SET_TP_SRC, tp_port );
}
bool
append_action_set_tp_dst( openflow_actions *actions, const uint16_t tp_port ) {
log_debug_for_cc( "Appending a set tp_dst action ( tp_port = %u ).", tp_port );
//assert( actions != NULL );
return append_action_set_tp_port( actions, OFPAT_SET_TP_DST, tp_port );
}
bool
append_action_enqueue( openflow_actions *actions, const uint16_t port, const uint32_t queue_id ) {
bool ret;
struct ofp_action_enqueue *action_enqueue;
log_debug_for_cc( "Appending an enqueue action ( port = %u, queue_id = %u ).", port, queue_id );
//assert( actions != NULL );
action_enqueue = ( struct ofp_action_enqueue * ) malloc( sizeof( struct ofp_action_enqueue ) );
action_enqueue->type = OFPAT_ENQUEUE;
action_enqueue->len = sizeof( struct ofp_action_enqueue );
action_enqueue->port = port;
action_enqueue->queue_id = queue_id;
ret = append_to_tail( &actions->list, ( void * ) action_enqueue );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
bool
append_action_vendor( openflow_actions *actions, const uint32_t vendor, const buffer *body ) {
bool ret;
uint16_t body_length = 0;
struct ofp_action_vendor_header *action_vendor;
if ( ( body != NULL ) && ( body->length > 0 ) ) {
body_length = ( uint16_t ) body->length;
}
log_debug_for_cc( "Appending a vendor action ( vendor = %#" PRIx32 ", body length = %u ).", vendor, body_length );
//assert( actions != NULL );
action_vendor = ( struct ofp_action_vendor_header * )
malloc( sizeof( struct ofp_action_vendor_header ) + body_length );
action_vendor->type = OFPAT_VENDOR;
action_vendor->len = ( uint16_t ) ( sizeof( struct ofp_action_vendor_header ) + body_length );
action_vendor->vendor = vendor;
if ( body_length > 0 ) {
memcpy( ( char * ) action_vendor + sizeof( struct ofp_action_vendor_header ), body->data, body_length );
}
ret = append_to_tail( &actions->list, ( void * ) action_vendor );
if ( ret ) {
actions->n_actions++;
}
return ret;
}
static uint16_t
get_actions_length( const openflow_actions *actions ) {
int actions_length = 0;
struct ofp_action_header *action_header;
list_element *action;
log_debug_for_cc( "Calculating the total length of actions." );
//assert( actions != NULL );
action = actions->list;
while ( action != NULL ) {
action_header = ( struct ofp_action_header * ) action->data;
if ( ( action_header->type <= OFPAT_ENQUEUE ) || ( action_header->type == OFPAT_VENDOR ) ) {
actions_length += action_header->len;
}
else {
//critical( "Undefined action type ( type = %#x ).", action_header->type );
//assert( 0 );
log_err_for_cc("Too many actions");
return CC_ERROR;
}
action = action->next;
}
log_debug_for_cc( "Total length of actions = %d.", actions_length );
if ( actions_length > USHRT_MAX ) {
//critical( "Too many actions ( # of actions = %d, actions length = %d ).",
actions->n_actions, actions_length );
//assert( 0 );
log_err_for_cc("Too many actions");
return CC_ERROR;
}
return ( uint16_t ) actions_length;
}