forked from hasbel/Ditra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.py
97 lines (77 loc) · 2.76 KB
/
messages.py
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
# Copyright (c) 2015 Hassib Belhaj-Hassine <hassib.belhaj at tum.de>
"""
This module wraps loxi to provide the functions and classes used by
ditra to generate and parse OpenFlow messages
"""
import random
import struct
from loxi import of12 as of
def generate_xid():
"""Return a randomly generated a 64bits xid for OF messages"""
return random.randrange(1, 0xffffffff)
def separate_messages(packet):
"""Separate a TCP packet into smaller one-message packets
Gets a TCP packet that may contain multiple aggregated OpenFlow
messages and separate it into smaller packets each containing
exactly one message. returns a list.
"""
packet_list = []
while packet:
msg_length= struct.unpack("!H",packet[2:4])[0]
message = packet[0:msg_length]
packet = packet[msg_length:]
packet_list.append(message)
return packet_list
def parse(packet):
"""Parse a binary packet into a high level OFObjects
Get a packet containing data of a single OF message in a binary
format as received by the socket and returns the corresponding
OFObject.
"""
return of.message.parse_message(packet)
def get_message_type(message):
"""return a string representation of type of message
If message is an OFObject , return a string representation of
the OFtype of message. Message might also be a string, for example
if we are doing the handshake with the proxy, in that case return
the message itself.
"""
if isinstance(message, str):
return "'%s'" % message
return of.const.ofp_type_map[message.type]
# OpenFlow messages
of_hello = of.message.hello(
xid=generate_xid())
of_features_request = of.message.features_request(
xid=generate_xid())
of_echo_reply = of.message.echo_reply(
xid=generate_xid())
of_equal_role_request = of.message.role_request(
xid=generate_xid(),
role=of.const.OFPCR_ROLE_EQUAL)
of_master_role_request = of.message.role_request(
xid=generate_xid(),
role=of.const.OFPCR_ROLE_MASTER)
of_set_config = of.message.set_config(
xid=generate_xid(),
miss_send_len=128)
# FlowMod message to add the dummy flow
of_flow_add = of.message.flow_add(
xid=generate_xid(),
cookie=1991, hard_timeout=1,
flags=of.const.OFPFF_SEND_FLOW_REM,
buffer_id=of.const.OFP_NO_BUFFER,
out_port=of.const.OFPP_ANY,
out_group=of.const.OFPG_ANY,
match=of.match([of.oxm.in_port(1991),]),
instructions=[])
# FlowMod message to add the dummy flow
of_flow_delete = of.message.flow_delete(
xid=generate_xid(),
cookie=1991, hard_timeout=1,
flags=of.const.OFPFF_SEND_FLOW_REM,
buffer_id=of.const.OFP_NO_BUFFER,
out_port=of.const.OFPP_ANY,
out_group=of.const.OFPG_ANY,
match=of.match([of.oxm.in_port(1991),]),
instructions=[])