-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathofp_client_v4.erl
More file actions
357 lines (333 loc) · 14.9 KB
/
ofp_client_v4.erl
File metadata and controls
357 lines (333 loc) · 14.9 KB
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
%%------------------------------------------------------------------------------
%% Copyright 2012 FlowForwarding.org
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%-----------------------------------------------------------------------------
%% @author Erlang Solutions Ltd. <openflow@erlang-solutions.com>
%% @copyright 2012 FlowForwarding.org
%% @doc OpenFlow v4 specific functions for the client.
-module(ofp_client_v4).
-export([create_error/2,
create_role/2,
extract_role/1,
create_async/1,
extract_async/1,
filter_out_message/3,
type_atom/1,
add_aux_id/2,
split_multipart/1,
split_big_multipart/1
]).
-include("of_protocol.hrl").
-include("ofp_v4.hrl").
%% @doc Create an error message.
-spec create_error(atom(), atom()) -> ofp_error_msg().
create_error(Type, Code) ->
#ofp_error_msg{type = Type,
code = Code}.
%% @doc Create role change message.
-spec create_role(atom(), integer()) -> ofp_role_reply().
create_role(Role, GenId) ->
#ofp_role_reply{role = Role,
generation_id = GenId}.
%% @doc Extract role change information.
-spec extract_role(ofp_role_request()) -> {atom(), integer()}.
extract_role(#ofp_role_request{role = Role,
generation_id = GenId}) ->
{Role, GenId}.
%% @doc Create async filters message.
-spec create_async(#async_config{}) -> #ofp_get_async_reply{}.
create_async(#async_config{
master_equal_packet_in = MEP,
master_equal_port_status = MES,
master_equal_flow_removed = MEF,
slave_packet_in = SP,
slave_port_status = SS,
slave_flow_removed = SF}) ->
%% Ensure that we don't try to send v5 values
MEP4 = MEP -- [table_miss, apply_action, action_set, group, packet_out],
SP4 = SP -- [table_miss, apply_action, action_set, group, packet_out],
#ofp_get_async_reply{packet_in_mask = {MEP4, SP4},
port_status_mask = {MES, SS},
flow_removed_mask = {MEF, SF}}.
%% @doc Extract async filters information.
-spec extract_async(#ofp_set_async{}) -> #async_config{}.
extract_async(#ofp_set_async{packet_in_mask = {MEP, SP},
port_status_mask = {MES, SS},
flow_removed_mask = {MEF, SF}}) ->
#async_config{
master_equal_packet_in = MEP,
master_equal_port_status = MES,
master_equal_flow_removed = MEF,
slave_packet_in = SP,
slave_port_status = SS,
slave_flow_removed = SF
}.
-spec filter_out_message(#ofp_message{},
master | slave | equal,
#async_config{}) -> boolean().
filter_out_message(#ofp_message{type = Type, body = Body}, Role, Filter) ->
{PacketInFilter, PortStatusFilter, FlowRemovedFilter} =
case Role of
slave ->
{Filter#async_config.slave_packet_in,
Filter#async_config.slave_port_status,
Filter#async_config.slave_flow_removed};
_Else ->
{Filter#async_config.master_equal_packet_in,
Filter#async_config.master_equal_port_status,
Filter#async_config.master_equal_flow_removed}
end,
case Type of
packet_in ->
Reason = Body#ofp_packet_in.reason,
should_filter_out(Reason, PacketInFilter);
port_status ->
Reason = Body#ofp_port_status.reason,
should_filter_out(Reason, PortStatusFilter);
flow_removed ->
Reason = Body#ofp_flow_removed.reason,
should_filter_out(Reason, FlowRemovedFilter);
_Other ->
false
end.
-spec type_atom(ofp_message_body()) -> atom().
type_atom(#ofp_error_msg{}) ->
error;
type_atom(#ofp_error_msg_experimenter{}) ->
error;
type_atom(#ofp_echo_request{}) ->
echo_request;
type_atom(#ofp_echo_reply{}) ->
echo_reply;
type_atom(#ofp_experimenter{}) ->
experimenter;
type_atom(#ofp_features_request{}) ->
features_request;
type_atom(#ofp_features_reply{}) ->
features_reply;
type_atom(#ofp_get_config_request{}) ->
get_config_request;
type_atom(#ofp_get_config_reply{}) ->
get_config_reply;
type_atom(#ofp_set_config{}) ->
set_config;
type_atom(#ofp_packet_in{}) ->
packet_in;
type_atom(#ofp_flow_removed{}) ->
flow_removed;
type_atom(#ofp_port_status{}) ->
port_status;
type_atom(#ofp_packet_out{}) ->
packet_out;
type_atom(#ofp_flow_mod{}) ->
flow_mod;
type_atom(#ofp_group_mod{}) ->
group_mod;
type_atom(#ofp_port_mod{}) ->
port_mod;
type_atom(#ofp_table_mod{}) ->
table_mod;
type_atom(#ofp_desc_request{}) ->
multipart_request;
type_atom(#ofp_desc_reply{}) ->
multipart_reply;
type_atom(#ofp_flow_stats_request{}) ->
multipart_request;
type_atom(#ofp_flow_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_aggregate_stats_request{}) ->
multipart_request;
type_atom(#ofp_aggregate_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_table_stats_request{}) ->
multipart_request;
type_atom(#ofp_table_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_table_features_request{}) ->
multipart_request;
type_atom(#ofp_table_features_reply{}) ->
multipart_reply;
type_atom(#ofp_port_stats_request{}) ->
multipart_request;
type_atom(#ofp_port_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_port_desc_request{}) ->
multipart_request;
type_atom(#ofp_port_desc_reply{}) ->
multipart_reply;
type_atom(#ofp_queue_stats_request{}) ->
multipart_request;
type_atom(#ofp_queue_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_group_stats_request{}) ->
multipart_request;
type_atom(#ofp_group_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_group_desc_request{}) ->
multipart_request;
type_atom(#ofp_group_desc_reply{}) ->
multipart_reply;
type_atom(#ofp_group_features_request{}) ->
multipart_request;
type_atom(#ofp_group_features_reply{}) ->
multipart_reply;
type_atom(#ofp_meter_stats_request{}) ->
multipart_request;
type_atom(#ofp_meter_stats_reply{}) ->
multipart_reply;
type_atom(#ofp_meter_config_request{}) ->
multipart_request;
type_atom(#ofp_meter_config_reply{}) ->
multipart_reply;
type_atom(#ofp_meter_features_request{}) ->
multipart_request;
type_atom(#ofp_meter_features_reply{}) ->
multipart_reply;
type_atom(#ofp_experimenter_request{}) ->
multipart_request;
type_atom(#ofp_experimenter_reply{}) ->
multipart_reply;
type_atom(#ofp_barrier_request{}) ->
barrier_request;
type_atom(#ofp_barrier_reply{}) ->
barrier_reply;
type_atom(#ofp_queue_get_config_request{}) ->
queue_get_config_request;
type_atom(#ofp_queue_get_config_reply{}) ->
queue_get_config_reply;
type_atom(#ofp_role_request{}) ->
role_request;
type_atom(#ofp_role_reply{}) ->
role_reply;
type_atom(#ofp_get_async_request{}) ->
get_async_request;
type_atom(#ofp_get_async_reply{}) ->
get_async_reply;
type_atom(#ofp_set_async{}) ->
set_async;
type_atom(#ofp_meter_mod{}) ->
meter_mod.
add_aux_id(#ofp_features_reply{} = Reply, Id) ->
Reply#ofp_features_reply{auxiliary_id = Id}.
split_multipart(#ofp_message{body = #ofp_table_features_reply{}} = Message) ->
Features = Message#ofp_message.body#ofp_table_features_reply.body,
split_table_features(Message#ofp_message{body = []}, Features, []);
split_multipart(Message) ->
[Message].
split_table_features(_Header, [], Messages) ->
lists:reverse(Messages);
split_table_features(Header, Feats, Messages) ->
{FirstTen, Rest} = split2(10, Feats),
Flags = case Rest of
[] ->
[];
_Else ->
[more]
end,
TableFeatures = #ofp_table_features_reply{flags = Flags,
body = FirstTen},
NewMessage = Header#ofp_message{body = TableFeatures},
split_table_features(Header, Rest, [NewMessage | Messages]).
split2(N, List) ->
split2(N, List, []).
split2(0, Tail, Head) ->
{lists:reverse(Head), Tail};
split2(_, [], Head) ->
{lists:reverse(Head), []};
split2(N, [X | Tail], Head) ->
split2(N - 1, Tail, [X | Head]).
%% False inner boddies, cant be split into reliable complete messages.
multipart_inner_body(#ofp_desc_request {} = _Msg) -> false;
multipart_inner_body(#ofp_desc_reply {} = _Msg) -> false;
multipart_inner_body(#ofp_flow_stats_request {} = _Msg) -> false;
multipart_inner_body(#ofp_flow_stats_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_aggregate_stats_request{} = _Msg) -> false;
multipart_inner_body(#ofp_aggregate_stats_reply {} = _Msg) -> false;
multipart_inner_body(#ofp_table_stats_request {} = _Msg) -> false;
multipart_inner_body(#ofp_table_stats_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_table_features_request {} = _Msg) -> false;
multipart_inner_body(#ofp_table_features_reply {} = _Msg) -> false;
multipart_inner_body(#ofp_port_stats_request {} = _Msg) -> false;
multipart_inner_body(#ofp_port_stats_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_port_desc_request {} = _Msg) -> false;
multipart_inner_body(#ofp_port_desc_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_queue_stats_request {} = _Msg) -> false;
multipart_inner_body(#ofp_queue_stats_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_group_stats_request {} = _Msg) -> false;
multipart_inner_body(#ofp_group_stats_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_group_desc_request {} = _Msg) -> false;
multipart_inner_body(#ofp_group_desc_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_group_features_request {} = _Msg) -> false;
multipart_inner_body(#ofp_group_features_reply {} = _Msg) -> false;
multipart_inner_body(#ofp_meter_stats_request {} = _Msg) -> false;
multipart_inner_body(#ofp_meter_stats_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_meter_config_request {} = _Msg) -> false;
multipart_inner_body(#ofp_meter_config_reply { body = InnerBody } = _Msg)-> {body,InnerBody};
multipart_inner_body(#ofp_meter_features_request {} = _Msg) -> false;
multipart_inner_body(#ofp_meter_features_reply {} = _Msg) -> false;
multipart_inner_body(#ofp_experimenter_request {} = _Msg) -> false;
multipart_inner_body(#ofp_experimenter_reply {} = _Msg) -> false.
remove_inner_body(#ofp_flow_stats_reply {} = Body) -> Body#ofp_flow_stats_reply { body = [] };
remove_inner_body(#ofp_table_stats_reply {} = Body) -> Body#ofp_table_stats_reply { body = [] };
remove_inner_body(#ofp_port_stats_reply {} = Body) -> Body#ofp_port_stats_reply { body = [] };
remove_inner_body(#ofp_port_desc_reply {} = Body) -> Body#ofp_port_desc_reply { body = [] };
remove_inner_body(#ofp_queue_stats_reply {} = Body) -> Body#ofp_queue_stats_reply { body = [] };
remove_inner_body(#ofp_group_stats_reply {} = Body) -> Body#ofp_group_stats_reply { body = [] };
remove_inner_body(#ofp_group_desc_reply {} = Body) -> Body#ofp_group_desc_reply { body = [] };
remove_inner_body(#ofp_meter_stats_reply {} = Body) -> Body#ofp_meter_stats_reply { body = [] };
remove_inner_body(#ofp_meter_config_reply {} = Body) -> Body#ofp_meter_config_reply { body = [] }.
reasemble_inner_body(#ofp_flow_stats_reply {} = Body,InnerBody) -> Body#ofp_flow_stats_reply { body = InnerBody };
reasemble_inner_body(#ofp_table_stats_reply {} = Body,InnerBody) -> Body#ofp_table_stats_reply { body = InnerBody };
reasemble_inner_body(#ofp_port_stats_reply {} = Body,InnerBody) -> Body#ofp_port_stats_reply { body = InnerBody };
reasemble_inner_body(#ofp_port_desc_reply {} = Body,InnerBody) -> Body#ofp_port_desc_reply { body = InnerBody };
reasemble_inner_body(#ofp_queue_stats_reply {} = Body,InnerBody) -> Body#ofp_queue_stats_reply { body = InnerBody };
reasemble_inner_body(#ofp_group_stats_reply {} = Body,InnerBody) -> Body#ofp_group_stats_reply { body = InnerBody };
reasemble_inner_body(#ofp_group_desc_reply {} = Body,InnerBody) -> Body#ofp_group_desc_reply { body = InnerBody };
reasemble_inner_body(#ofp_meter_stats_reply {} = Body,InnerBody) -> Body#ofp_meter_stats_reply { body = InnerBody };
reasemble_inner_body(#ofp_meter_config_reply {} = Body,InnerBody) -> Body#ofp_meter_config_reply { body = InnerBody }.
%% SPLIT THE MESSAGE INTO 2 bits, the Body and the rest,
%% Encode and parse the REST, and use that size as the
%% Deduction for the maximum size.... ( - 16 )
split_big_multipart(#ofp_message{type = _Type, version = V, body = Body} = Message) ->
case multipart_inner_body(Body) of
false ->
false;
{body,InnerBody} ->
SkeletonBody = Message#ofp_message{ body = remove_inner_body(Body) },
[ Message#ofp_message{body = reasemble_inner_body(Body,Chunk) } ||
Chunk <- split_big_multipart_structs(SkeletonBody,InnerBody,[],V) ]
end.
split_big_multipart_structs(SkeletonBody,Body,Chunks,Version) ->
Module = encode_module(Version),
case split_structs_chunks(Body,[],0,Module) of
{ok,{[],Chunk}} -> [Chunk|Chunks];
{ok,{Rest,Chunk}} -> split_big_multipart_structs(SkeletonBody,Rest,[Chunk|Chunks],Version)
end.
split_structs_chunks([],Results,_TotalSize,_Module) ->
{ok,{[],Results}};
split_structs_chunks([H|T],Results,TotalSize,Module) ->
Bin = Module:encode_struct(H),
NewTotalSize = byte_size(Bin) + TotalSize,
case NewTotalSize < ( ?OFPM_MAX_SIZE - 16 ) of %% 16 bytes for the ofp_message bin encasing the actual multipart message etc
true -> split_structs_chunks(T,[H|Results],NewTotalSize,Module);
false -> {ok,{[H|T],Results}}
end.
encode_module(3) ->
ofp_v3_encode;
encode_module(4) ->
ofp_v4_encode;
encode_module(5) ->
ofp_v5_encode.
should_filter_out(Reason, Filter) ->
not lists:member(Reason, Filter).