1
|
|
|
"""Defines actions that may be associated with flows packets.""" |
2
|
|
|
# System imports |
3
|
1 |
|
from enum import IntEnum |
4
|
|
|
|
5
|
|
|
# Local source tree imports |
6
|
1 |
|
from pyof.foundation.base import GenericStruct |
7
|
1 |
|
from pyof.foundation.basic_types import ( |
8
|
|
|
FixedTypeList, Pad, UBInt8, UBInt16, UBInt32) |
9
|
|
|
|
10
|
|
|
# Third-party imports |
11
|
|
|
|
12
|
1 |
|
__all__ = ('ActionExperimenterHeader', 'ActionGroup', 'ActionHeader', |
13
|
|
|
'ActionMPLSTTL', 'ActionNWTTL', 'ActionOutput', 'ActionPopMPLS', |
14
|
|
|
'ActionPush', 'ActionSetField', 'ActionSetQueue', 'ActionType', |
15
|
|
|
'ControllerMaxLen', 'ListOfActions') |
16
|
|
|
|
17
|
|
|
# Enums |
18
|
|
|
|
19
|
|
|
|
20
|
1 |
|
class ActionType(IntEnum): |
21
|
|
|
"""Actions associated with flows and packets.""" |
22
|
|
|
|
23
|
|
|
#: Output to switch port. |
24
|
1 |
|
OFPAT_OUTPUT = 0 |
25
|
|
|
#: Copy TTL "outwards" -- from next-to-outermost to outermost |
26
|
1 |
|
OFPAT_COPY_TTL_OUT = 11 |
27
|
|
|
#: Copy TTL "inwards" -- from outermost to next-to-outermost |
28
|
1 |
|
OFPAT_COPY_TTL_IN = 12 |
29
|
|
|
#: MPLS TTL |
30
|
1 |
|
OFPAT_SET_MPLS_TTL = 15 |
31
|
|
|
#: Decrement MPLS TTL |
32
|
1 |
|
OFPAT_DEC_MPLS_TTL = 16 |
33
|
|
|
#: Push a new VLAN tag |
34
|
1 |
|
OFPAT_PUSH_VLAN = 17 |
35
|
|
|
#: Pop the outer VLAN tag |
36
|
1 |
|
OFPAT_POP_VLAN = 18 |
37
|
|
|
#: Push a new MPLS tag |
38
|
1 |
|
OFPAT_PUSH_MPLS = 19 |
39
|
|
|
#: Pop the outer MPLS tag |
40
|
1 |
|
OFPAT_POP_MPLS = 20 |
41
|
|
|
#: Set queue id when outputting to a port |
42
|
1 |
|
OFPAT_SET_QUEUE = 21 |
43
|
|
|
#: Apply group. |
44
|
1 |
|
OFPAT_GROUP = 22 |
45
|
|
|
#: IP TTL. |
46
|
1 |
|
OFPAT_SET_NW_TTL = 23 |
47
|
|
|
#: Decrement IP TTL. |
48
|
1 |
|
OFPAT_DEC_NW_TTL = 24 |
49
|
|
|
#: Set a header field using OXM TLV format. |
50
|
1 |
|
OFPAT_SET_FIELD = 25 |
51
|
|
|
#: Push a new PBB service tag (I-TAG) |
52
|
1 |
|
OFPAT_PUSH_PBB = 26 |
53
|
|
|
#: Pop the outer PBB service tag (I-TAG) |
54
|
1 |
|
OFPAT_POP_PBB = 27 |
55
|
|
|
#: Experimenter type |
56
|
1 |
|
OFPAT_EXPERIMENTER = 0xffff |
57
|
|
|
|
58
|
|
|
|
59
|
1 |
|
class ControllerMaxLen(IntEnum): |
60
|
|
|
"""A max_len of OFPCML_NO_BUFFER means not to buffer. |
61
|
|
|
|
62
|
|
|
The packet should be sent. |
63
|
|
|
""" |
64
|
|
|
|
65
|
|
|
#: maximum max_len value which can be used to request a specific byte |
66
|
|
|
#: length. |
67
|
1 |
|
OFPCML_MAX = 0xffe5 |
68
|
|
|
#: indicates that no buffering should be applied and the whole packet is to |
69
|
|
|
#: be sent to the controller. |
70
|
1 |
|
OFPCML_NO_BUFFER = 0xffff |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
# Classes |
74
|
|
|
|
75
|
|
|
|
76
|
1 |
|
class ActionExperimenterHeader(GenericStruct): |
77
|
|
|
"""Action structure for OFPAT_EXPERIMENTER.""" |
78
|
|
|
|
79
|
|
|
#: OFPAT_EXPERIMENTER. |
80
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_EXPERIMENTER, enum_ref=ActionType) |
81
|
|
|
#: Length is multiple of 8. |
82
|
1 |
|
length = UBInt16() |
83
|
|
|
#: Experimenter ID which takes the same form as in struct |
84
|
|
|
#: ofp_experimenter_header |
85
|
1 |
|
experimenter = UBInt32() |
86
|
|
|
|
87
|
1 |
|
def __init__(self, length=None, experimenter=None): |
88
|
|
|
"""The constructor just assigns parameters to object attributes. |
89
|
|
|
|
90
|
|
|
Args: |
91
|
|
|
experimenter (int): The experimenter field is the Experimenter ID, |
92
|
|
|
which takes the same form as in struct ofp_experimenter. |
93
|
|
|
""" |
94
|
|
|
super().__init__() |
95
|
|
|
self.length = length |
96
|
|
|
self.experimenter = experimenter |
97
|
|
|
|
98
|
|
|
|
99
|
1 |
|
class ActionGroup(GenericStruct): |
100
|
|
|
"""Action structure for OFPAT_GROUP.""" |
101
|
|
|
|
102
|
|
|
#: OFPAT_GROUP. |
103
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_GROUP, enum_ref=ActionType) |
104
|
|
|
#: Length is 8. |
105
|
1 |
|
length = UBInt16(8) |
106
|
|
|
#: Group identifier. |
107
|
1 |
|
group_id = UBInt32() |
108
|
|
|
|
109
|
1 |
|
def __init__(self, group_id=None): |
110
|
|
|
"""The constructor just assigns parameters to object attributes. |
111
|
|
|
|
112
|
|
|
Args: |
113
|
|
|
group_id (int): The group_id indicates the group used to process |
114
|
|
|
this packet. The set of buckets to apply depends on the group |
115
|
|
|
type. |
116
|
|
|
""" |
117
|
|
|
super().__init__() |
118
|
|
|
self.group_id = group_id |
119
|
|
|
|
120
|
|
|
|
121
|
1 |
|
class ActionHeader(GenericStruct): |
122
|
|
|
"""Action header that is common to all actions. |
123
|
|
|
|
124
|
|
|
The length includes the header and any padding used to make the action |
125
|
|
|
64-bit aligned. |
126
|
|
|
NB: The length of an action *must* always be a multiple of eight. |
127
|
|
|
""" |
128
|
|
|
|
129
|
|
|
#: One of OFPAT_*. |
130
|
1 |
|
action_type = UBInt16(enum_ref=ActionType) |
131
|
|
|
#: Length of action, including this header. This is the length of actions, |
132
|
|
|
#: including any padding to make it 64-bit aligned. |
133
|
1 |
|
length = UBInt16() |
134
|
|
|
#: Pad for 64-bit alignment. |
135
|
1 |
|
pad = Pad(4) |
136
|
|
|
|
137
|
1 |
|
def __init__(self, action_type=None, length=None): |
138
|
|
|
"""The constructor just assigns parameters to object attributes. |
139
|
|
|
|
140
|
|
|
Args: |
141
|
|
|
action_type (~pyof.v0x04.common.action.ActionType): |
142
|
|
|
The type of the action. |
143
|
|
|
length (int): Length of action, including this header. |
144
|
|
|
""" |
145
|
|
|
super().__init__() |
146
|
|
|
self.action_type = action_type |
147
|
|
|
self.length = length |
148
|
|
|
|
149
|
|
|
|
150
|
1 |
|
class ActionMPLSTTL(GenericStruct): |
151
|
|
|
"""Action structure for OFPAT_SET_MPLS_TTL.""" |
152
|
|
|
|
153
|
|
|
#: OFPAT_SET_MPLS_TTL. |
154
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_SET_MPLS_TTL, enum_ref=ActionType) |
155
|
|
|
#: Length is 8. |
156
|
1 |
|
length = UBInt16(8) |
157
|
|
|
#: MPLS TTL |
158
|
1 |
|
mpls_ttl = UBInt8() |
159
|
|
|
#: Padding |
160
|
1 |
|
pad = Pad(3) |
161
|
|
|
|
162
|
1 |
|
def __init__(self, mpls_ttl=None): |
163
|
|
|
"""The constructor just assigns parameters to object attributes. |
164
|
|
|
|
165
|
|
|
Args: |
166
|
|
|
mpls_ttl (int): The mpls_ttl field is the MPLS TTL to set. |
167
|
|
|
""" |
168
|
|
|
super().__init__() |
169
|
|
|
self.mpls_ttl = mpls_ttl |
170
|
|
|
|
171
|
|
|
|
172
|
1 |
|
class ActionNWTTL(GenericStruct): |
173
|
|
|
"""Action structure for OFPAT_SET_NW_TTL.""" |
174
|
|
|
|
175
|
|
|
#: OFPAT_SET_NW_TTL. |
176
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_SET_NW_TTL, enum_ref=ActionType) |
177
|
|
|
#: Length is 8. |
178
|
1 |
|
length = UBInt16(8) |
179
|
|
|
#: IP TTL |
180
|
1 |
|
nw_ttl = UBInt8() |
181
|
|
|
#: Padding |
182
|
1 |
|
pad = Pad(3) |
183
|
|
|
|
184
|
1 |
|
def __init__(self, nw_ttl=None): |
185
|
|
|
"""The constructor just assigns parameters to object attributes. |
186
|
|
|
|
187
|
|
|
Args: |
188
|
|
|
nw_ttl (int): the TTL address to set in the IP header. |
189
|
|
|
""" |
190
|
|
|
super().__init__() |
191
|
|
|
self.nw_ttl = nw_ttl |
192
|
|
|
|
193
|
|
|
|
194
|
1 |
|
class ActionOutput(GenericStruct): |
195
|
|
|
"""Defines the actions output. |
196
|
|
|
|
197
|
|
|
Action structure for :attr:`ActionType.OFPAT_OUTPUT`, which sends packets |
198
|
|
|
out :attr:`port`. When the :attr:`port` is the |
199
|
|
|
:attr:`.Port.OFPP_CONTROLLER`, :attr:`max_length` indicates the max number |
200
|
|
|
of bytes to send. A :attr:`max_length` of zero means no bytes of the packet |
201
|
|
|
should be sent. |
202
|
|
|
""" |
203
|
|
|
|
204
|
|
|
#: OFPAT_OUTPUT. |
205
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_OUTPUT, enum_ref=ActionType) |
206
|
|
|
#: Length is 16. |
207
|
1 |
|
length = UBInt16(16) |
208
|
|
|
#: Output port. |
209
|
1 |
|
port = UBInt16() |
210
|
|
|
#: Max length to send to controller. |
211
|
1 |
|
max_length = UBInt16() |
212
|
|
|
#: Pad to 64 bits. |
213
|
1 |
|
pad = Pad(6) |
214
|
|
|
|
215
|
1 |
|
def __init__(self, action_type=None, length=None, port=None, |
216
|
|
|
max_length=None): |
217
|
|
|
"""The constructor just assigns parameters to object attributes. |
218
|
|
|
|
219
|
|
|
Args: |
220
|
|
|
port (:class:`Port` or :class:`int`): Output port. |
221
|
|
|
max_length (int): Max length to send to controller. |
222
|
|
|
""" |
223
|
|
|
super().__init__() |
224
|
|
|
self.action_type = action_type |
225
|
|
|
self.length = length |
226
|
|
|
self.port = port |
227
|
|
|
self.max_length = max_length |
228
|
|
|
|
229
|
|
|
|
230
|
1 |
|
class ActionPopMPLS(GenericStruct): |
231
|
|
|
"""Action structure for OFPAT_POP_MPLS.""" |
232
|
|
|
|
233
|
|
|
#: OFPAT_POP_MPLS. |
234
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_POP_MPLS, enum_ref=ActionType) |
235
|
|
|
#: Length is 8. |
236
|
1 |
|
length = UBInt16(8) |
237
|
|
|
#: Ethertype |
238
|
1 |
|
ethertype = UBInt16() |
239
|
|
|
#: Padding |
240
|
1 |
|
pad = Pad(2) |
241
|
|
|
|
242
|
1 |
|
def __init__(self, ethertype=None): |
243
|
|
|
"""The constructor just assigns parameters to object attributes. |
244
|
|
|
|
245
|
|
|
Args: |
246
|
|
|
ethertype (int): indicates the Ethertype of the payload. |
247
|
|
|
""" |
248
|
|
|
super().__init__() |
249
|
|
|
self.ethertype = ethertype |
250
|
|
|
|
251
|
|
|
|
252
|
1 |
|
class ActionPush(GenericStruct): |
253
|
|
|
"""Action structure for OFPAT_PUSH_VLAN/MPLS/PBB.""" |
254
|
|
|
|
255
|
|
|
#: OFPAT_PUSH_VLAN/MPLS/PBB. |
256
|
1 |
|
action_type = UBInt16(enum_ref=ActionType) |
257
|
|
|
#: Length is 8. |
258
|
1 |
|
length = UBInt16(8) |
259
|
|
|
#: Ethertype |
260
|
1 |
|
ethertype = UBInt16() |
261
|
|
|
#: Padding |
262
|
1 |
|
pad = Pad(2) |
263
|
|
|
|
264
|
1 |
|
def __init__(self, ethertype=None): |
265
|
|
|
"""The constructor just assigns parameters to object attributes. |
266
|
|
|
|
267
|
|
|
Args: |
268
|
|
|
ethertype (int): indicates the Ethertype of the new tag. |
269
|
|
|
""" |
270
|
|
|
super().__init__() |
271
|
|
|
self.ethertype = ethertype |
272
|
|
|
|
273
|
|
|
|
274
|
1 |
|
class ActionSetField(GenericStruct): |
275
|
|
|
"""Action structure for OFPAT_SET_FIELD.""" |
276
|
|
|
|
277
|
|
|
#: OFPAT_SET_FIELD. |
278
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_SET_FIELD, enum_ref=ActionType) |
279
|
|
|
#: Length is padded to 64 bits. |
280
|
1 |
|
length = UBInt16() |
281
|
|
|
#: OXM TLV - Make compiler happy |
282
|
1 |
|
field1 = UBInt8() |
283
|
1 |
|
field2 = UBInt8() |
284
|
1 |
|
field3 = UBInt8() |
285
|
1 |
|
field4 = UBInt8() |
286
|
|
|
|
287
|
1 |
|
def __init__(self, length=None, field1=None, field2=None, field3=None, |
288
|
|
|
field4=None): |
289
|
|
|
"""The constructor just assigns parameters to object attributes. |
290
|
|
|
|
291
|
|
|
Args: |
292
|
|
|
length (int): length padded to 64 bits, followed by exactly |
293
|
|
|
oxm_len bytes containing a single OXM TLV, then |
294
|
|
|
exactly ((oxm_len + 4) + 7)/8*8 - (oxm_len + 4) |
295
|
|
|
(between 0 and 7) bytes of all-zero bytes |
296
|
|
|
field1 (int): OXM field. |
297
|
|
|
field2 (int): OXM field. |
298
|
|
|
field3 (int): OXM field. |
299
|
|
|
field4 (int): OXM field. |
300
|
|
|
""" |
301
|
|
|
super().__init__() |
302
|
|
|
self.length = length |
303
|
|
|
self.field1 = field1 |
304
|
|
|
self.field2 = field2 |
305
|
|
|
self.field3 = field3 |
306
|
|
|
self.field4 = field4 |
307
|
|
|
|
308
|
|
|
|
309
|
1 |
|
class ActionSetQueue(GenericStruct): |
310
|
|
|
"""Action structure for OFPAT_SET_QUEUE.""" |
311
|
|
|
|
312
|
|
|
#: OFPAT_SET_QUEUE. |
313
|
1 |
|
action_type = UBInt16(ActionType.OFPAT_SET_QUEUE, enum_ref=ActionType) |
314
|
|
|
#: Length is 8. |
315
|
1 |
|
length = UBInt16(8) |
316
|
|
|
#: Queue id for the packets. |
317
|
1 |
|
queue_id = UBInt32() |
318
|
|
|
|
319
|
1 |
|
def __init__(self, queue_id=None): |
320
|
|
|
"""The constructor just assigns parameters to object attributes. |
321
|
|
|
|
322
|
|
|
Args: |
323
|
|
|
queue_id (int): The queue_id send packets to given queue on port. |
324
|
|
|
""" |
325
|
|
|
super().__init__() |
326
|
|
|
self.queue_id = queue_id |
327
|
|
|
|
328
|
|
|
|
329
|
1 |
|
class ListOfActions(FixedTypeList): |
330
|
|
|
"""List of actions. |
331
|
|
|
|
332
|
|
|
Represented by instances of ActionHeader and used on ActionHeader objects. |
333
|
|
|
""" |
334
|
|
|
|
335
|
1 |
|
def __init__(self, items=None): |
336
|
|
|
"""The constructor just assigns parameters to object attributes. |
337
|
|
|
|
338
|
|
|
Args: |
339
|
|
|
items (~pyof.v0x04.common.action.ActionHeader): |
340
|
|
|
Instance or a list of instances. |
341
|
|
|
""" |
342
|
|
|
super().__init__(pyof_class=ActionHeader, items=items) |
343
|
|
|
|