Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 1 | """ |
| 2 | OpenFlow action, instruction and bucket list classes |
| 3 | """ |
| 4 | |
| 5 | from action import * |
| 6 | from cstruct import ofp_header |
| 7 | from base_list import ofp_base_list |
| 8 | import copy |
| 9 | |
| 10 | action_object_map = { |
Rich Lane | 6339349 | 2013-01-11 09:21:12 -0800 | [diff] [blame] | 11 | OFPAT_OUTPUT : output, |
| 12 | OFPAT_SET_FIELD : set_field, |
| 13 | OFPAT_COPY_TTL_OUT : copy_ttl_out, |
| 14 | OFPAT_COPY_TTL_IN : copy_ttl_in, |
| 15 | OFPAT_SET_MPLS_TTL : set_mpls_ttl, |
| 16 | OFPAT_DEC_MPLS_TTL : dec_mpls_ttl, |
| 17 | OFPAT_PUSH_VLAN : push_vlan, |
| 18 | OFPAT_POP_VLAN : pop_vlan, |
| 19 | OFPAT_PUSH_MPLS : push_mpls, |
| 20 | OFPAT_POP_MPLS : pop_mpls, |
| 21 | OFPAT_SET_QUEUE : set_queue, |
| 22 | OFPAT_GROUP : group, |
| 23 | OFPAT_SET_NW_TTL : set_nw_ttl, |
| 24 | OFPAT_DEC_NW_TTL : dec_nw_ttl, |
| 25 | OFPAT_EXPERIMENTER : experimenter |
Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | class action_list(ofp_base_list): |
| 29 | """ |
| 30 | Maintain a list of actions |
| 31 | |
| 32 | Data members: |
| 33 | @arg actions: An array of action objects such as action_output, etc. |
| 34 | |
| 35 | Methods: |
| 36 | @arg pack: Pack the structure into a string |
| 37 | @arg unpack: Unpack a string to objects, with proper typing |
| 38 | @arg add: Add an action to the list; you can directly access |
| 39 | the action member, but add will validate that the added object |
| 40 | is an action. |
| 41 | |
| 42 | """ |
| 43 | |
Rich Lane | e3a5972 | 2013-01-11 12:50:25 -0800 | [diff] [blame] | 44 | def __init__(self, actions=None): |
Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 45 | ofp_base_list.__init__(self) |
| 46 | self.actions = self.items |
Rich Lane | e3a5972 | 2013-01-11 12:50:25 -0800 | [diff] [blame] | 47 | if actions: |
| 48 | self.actions.extend(actions) |
Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 49 | self.name = "action" |
| 50 | self.class_list = action_class_list |
| 51 | |
| 52 | def unpack(self, binary_string, bytes=None): |
| 53 | """ |
| 54 | Unpack a list of actions |
| 55 | |
| 56 | Unpack actions from a binary string, creating an array |
| 57 | of objects of the appropriate type |
| 58 | |
| 59 | @param binary_string The string to be unpacked |
| 60 | |
| 61 | @param bytes The total length of the action list in bytes. |
| 62 | Ignored if decode is True. If None and decode is false, the |
| 63 | list is assumed to extend through the entire string. |
| 64 | |
| 65 | @return The remainder of binary_string that was not parsed |
| 66 | |
| 67 | """ |
| 68 | if bytes == None: |
| 69 | bytes = len(binary_string) |
| 70 | bytes_done = 0 |
| 71 | count = 0 |
| 72 | cur_string = binary_string |
| 73 | while bytes_done < bytes: |
| 74 | hdr = ofp_action_header() |
| 75 | hdr.unpack(cur_string) |
| 76 | if hdr.len < OFP_ACTION_HEADER_BYTES: |
| 77 | print "ERROR: Action too short" |
| 78 | break |
| 79 | if not hdr.type in action_object_map.keys(): |
| 80 | print "WARNING: Skipping unknown action ", hdr.type, hdr.len |
| 81 | else: |
| 82 | self.actions.append(action_object_map[hdr.type]()) |
| 83 | self.actions[count].unpack(cur_string) |
| 84 | count += 1 |
| 85 | cur_string = cur_string[hdr.len:] |
| 86 | bytes_done += hdr.len |
| 87 | return cur_string |
| 88 | |