blob: 6765a12fc08add7454fb1adc6144e62256edbe9e [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001"""This module holds the network.
2
3Copyright(C) 2009, Stanford University
4Date October 2009
5Created by ykk
6"""
7import random
8import openflow
9
10class network:
11 """Class holding information about OpenFlow network
12 """
13 def __init__(self):
14 """Initialize
15 """
16 ##List of switches
17 self.switches = []
18 ##Dictionary of links
19 self.links = {}
20 ##Reference to connections
21 self.connections = openflow.connections()
22
23 def add_switch(self, sw):
24 """Add switch to network
25 """
26 self.switches.append(sw)
27 self.connections.add_connection(sw, sw.connection)
28
29 def add_link(self, link):
30 """Add link to network
31 """
32 try:
33 self.links[link.switch1,link.switch2].append(link)
34 except KeyError:
35 self.links[link.switch1,link.switch2] = []
36 self.links[link.switch1,link.switch2].append(link)
37
38class link:
39 """Class to hold information about link
40
41 Copyright(C) 2009, Stanford University
42 Date November 2009
43 Created by ykk
44 """
45 def __init__(self, switch1, switch2):
46 """Initialize link between specified switches
47 """
48 ##Reference to first switch
49 self.switch1 = switch1
50 ##Reference to second switch
51 self.switch2 = switch2
52
53class switch:
54 """Class holding information about OpenFlow switch
55
56 Copyright(C) 2009, Stanford University
57 Date October 2009
58 Created by ykk
59 """
60 def __init__(self, miss_send_len=128,
61 sock=None, dpid=None, n_buffers=100, n_tables=1,
62 capability=None):
63 """Initialize switch
64 """
65 ##Socket to controller
66 self.sock = sock
67 ##Datapath id of switch
68 if (dpid != None):
69 self.datapath_id = dpid
70 else:
71 self.datapath_id = random.randrange(1, pow(2,64))
72 ##Number of buffers
73 self.n_buffers = n_buffers
74 ##Number of tables
75 self.n_tables= n_tables
76 ##Capabilities
77 if (isinstance(capability, switch_capabilities)):
78 self.capability = capability
79 else:
80 self.capability = switch_capabilities(miss_send_len)
81 ##Valid Actions
82 self.valid_actions = 0
83 ##List of port
84 self.port = []
85
86class switch_capabilities:
87 """Class to hold switch capabilities
88 """
89 def __init__(self, miss_send_len=128):
90 """Initialize
91
92 Copyright(C) 2009, Stanford University
93 Date October 2009
94 Created by ykk
95 """
96 ##Capabilities support by datapath
97 self.flow_stats = True
98 self.table_stats = True
99 self.port_stats = True
100 self.stp = True
101 self.multi_phy_tx = True
102 self.ip_resam = False
103 ##Switch config
104 self.send_exp = None
105 self.ip_frag = 0
106 self.miss_send_len = miss_send_len
107 ##Valid actions
108 self.act_output = True
109 self.act_set_vlan_vid = True
110 self.act_set_vlan_pcp = True
111 self.act_strip_vlan = True
112 self.act_set_dl_src = True
113 self.act_set_dl_dst = True
114 self.act_set_nw_src = True
115 self.act_set_nw_dst = True
116 self.act_set_tp_src = True
117 self.act_set_tp_dst = True
118 self.act_vendor = False
119
120 def get_capability(self, ofmsg):
121 """Return value for uint32_t capability field
122 """
123 value = 0
124 if (self.flow_stats):
125 value += ofmsg.get_value("OFPC_FLOW_STATS")
126 if (self.table_stats):
127 value += ofmsg.get_value("OFPC_TABLE_STATS")
128 if (self.port_stats):
129 value += ofmsg.get_value("OFPC_PORT_STATS")
130 if (self.stp):
131 value += ofmsg.get_value("OFPC_STP")
132 if (self.multi_phy_tx):
133 value += ofmsg.get_value("OFPC_MULTI_PHY_TX")
134 if (self.ip_resam):
135 value += ofmsg.get_value("OFPC_IP_REASM")
136 return value
137
138 def get_actions(self, ofmsg):
139 """Return value for uint32_t action field
140 """
141 value = 0
142 if (self.act_output):
143 value += (1 << (ofmsg.get_value("OFPAT_OUTPUT")+1))
144 if (self.act_set_vlan_vid):
145 value += (1 << (ofmsg.get_value("OFPAT_SET_VLAN_VID")+1))
146 if (self.act_set_vlan_pcp):
147 value += (1 << (ofmsg.get_value("OFPAT_SET_VLAN_PCP")+1))
148 if (self.act_strip_vlan):
149 value += (1 << (ofmsg.get_value("OFPAT_STRIP_VLAN")+1))
150 if (self.act_set_dl_src):
151 value += (1 << (ofmsg.get_value("OFPAT_SET_DL_SRC")+1))
152 if (self.act_set_dl_dst):
153 value += (1 << (ofmsg.get_value("OFPAT_SET_DL_DST")+1))
154 if (self.act_set_nw_src):
155 value += (1 << (ofmsg.get_value("OFPAT_SET_NW_SRC")+1))
156 if (self.act_set_nw_dst):
157 value += (1 << (ofmsg.get_value("OFPAT_SET_NW_DST")+1))
158 if (self.act_set_tp_src):
159 value += (1 << (ofmsg.get_value("OFPAT_SET_TP_SRC")+1))
160 if (self.act_set_tp_dst):
161 value += (1 << (ofmsg.get_value("OFPAT_SET_TP_DST")+1))
162 return value
163
164class port:
165 """Class to hold information about port
166
167 Copyright(C) 2009, Stanford University
168 Date October 2009
169 Created by ykk
170 """
171 def __init__(self, port_no, stp=(2 << 8), hw_addr=None, name=""):
172 """Initialize
173 """
174 ##Port properties
175 self.port_no = port_no
176 if (hw_addr != None):
177 self.hw_addr = hw_addr
178 else:
179 self.hw_addr = random.randrange(1, pow(2,48))
180 self.name = name
181 ##Port config
182 self.port_down = False
183 self.no_stp = False
184 self.no_recv = False
185 self.no_recv_stp = False
186 self.no_flood = False
187 self.no_fwd = False
188 self.no_packet_in = False
189 #Port state
190 self.link_down = False
191 self.stp = stp