Allow several pci for of_port_mapping. Log enhancement
[osm/openvim.git] / floodlight.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 ##
5 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
6 # This file is part of openvim
7 # All Rights Reserved.
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 #
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: nfvlabs@tid.es
23 ##
24
25 """
26 Implement the plugging for floodligth openflow controller
27 It creates the class OF_conn to create dataplane connections
28 with static rules based on packet destination MAC address
29 """
30
31 __author__ = "Pablo Montes, Alfonso Tierno"
32 __date__ = "$28-oct-2014 12:07:15$"
33
34 import json
35 import requests
36 import logging
37 import openflow_conn
38
39
40 class OF_conn(openflow_conn.OpenflowConn):
41 """
42 Openflow Connector for Floodlight.
43 No MAC learning is used
44 version 0.9 or 1.X is autodetected
45 version 1.X is in progress, not finished!!!
46 """
47
48 def __init__(self, params):
49 """
50 Constructor
51 :param self:
52 :param params: dictionay with the following keys:
53 of_dpid: DPID to use for this controller
54 of_ip: controller IP address
55 of_port: controller TCP port
56 of_version: version, can be "0.9" or "1.X". By default it is autodetected
57 of_debug: debug level for logging. Default to ERROR
58 other keys are ignored
59 :return: Raise an ValueError exception if same parameter is missing or wrong
60 """
61 # check params
62 if "of_ip" not in params or params["of_ip"] == None or "of_port" not in params or params["of_port"] == None:
63 raise ValueError("IP address and port must be provided")
64
65 openflow_conn.OpenflowConn.__init__(self, params)
66
67 self.name = "Floodlight"
68 self.dpid = str(params["of_dpid"])
69 self.url = "http://%s:%s" % (str(params["of_ip"]), str(params["of_port"]))
70
71 self.pp2ofi = {} # From Physical Port to OpenFlow Index
72 self.ofi2pp = {} # From OpenFlow Index to Physical Port
73 self.headers = {'content-type': 'application/json', 'Accept': 'application/json'}
74 self.version = None
75 self.logger = logging.getLogger('vim.OF.FL')
76 self.logger.setLevel(getattr(logging, params.get("of_debug", "ERROR")))
77 self._set_version(params.get("of_version"))
78
79 def _set_version(self, version):
80 """
81 set up a version of the controller.
82 Depending on the version it fills the self.ver_names with the naming used in this version
83 :param version: Openflow controller version
84 :return: Raise an ValueError exception if same parameter is missing or wrong
85 """
86 # static version names
87 if version == None:
88 self.version = None
89 elif version == "0.9":
90 self.version = version
91 self.name = "Floodlightv0.9"
92 self.ver_names = {
93 "dpid": "dpid",
94 "URLmodifier": "staticflowentrypusher",
95 "destmac": "dst-mac",
96 "vlanid": "vlan-id",
97 "inport": "ingress-port",
98 "setvlan": "set-vlan-id",
99 "stripvlan": "strip-vlan",
100 }
101 elif version[0] == "1": # version 1.X
102 self.version = version
103 self.name = "Floodlightv1.X"
104 self.ver_names = {
105 "dpid": "switchDPID",
106 "URLmodifier": "staticflowpusher",
107 "destmac": "eth_dst",
108 "vlanid": "eth_vlan_vid",
109 "inport": "in_port",
110 "setvlan": "set_vlan_vid",
111 "stripvlan": "strip_vlan",
112 }
113 else:
114 raise ValueError("Invalid version for floodlight controller")
115
116 def get_of_switches(self):
117 """
118 Obtain a a list of switches or DPID detected by this controller
119 :return: list where each element a tuple pair (DPID, IP address)
120 Raise an OpenflowconnConnectionException or OpenflowconnConnectionException exception if same
121 parameter is missing or wrong
122 """
123 try:
124 of_response = requests.get(self.url + "/wm/core/controller/switches/json", headers=self.headers)
125 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
126 if of_response.status_code != 200:
127 self.logger.warning("get_of_switches " + error_text)
128 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
129 self.logger.debug("get_of_switches " + error_text)
130 info = of_response.json()
131 if type(info) != list and type(info) != tuple:
132 self.logger.error("get_of_switches. Unexpected response not a list %s", str(type(info)))
133 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, not a list. Wrong version?")
134 if len(info) == 0:
135 return info
136 # autodiscover version
137 if self.version == None:
138 if 'dpid' in info[0] and 'inetAddress' in info[0]:
139 self._set_version("0.9")
140 elif 'switchDPID' in info[0] and 'inetAddress' in info[0]:
141 self._set_version("1.X")
142 else:
143 self.logger.error(
144 "get_of_switches. Unexpected response, not found 'dpid' or 'switchDPID' field: %s",
145 str(info[0]))
146 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, not found 'dpid' or "
147 "'switchDPID' field. Wrong version?")
148
149 switch_list = []
150 for switch in info:
151 switch_list.append((switch[self.ver_names["dpid"]], switch['inetAddress']))
152 return switch_list
153 except requests.exceptions.RequestException as e:
154 error_text = type(e).__name__ + ": " + str(e)
155 self.logger.error("get_of_switches " + error_text)
156 raise openflow_conn.OpenflowconnConnectionException(error_text)
157 except ValueError as e:
158 # ValueError in the case that JSON can not be decoded
159 error_text = type(e).__name__ + ": " + str(e)
160 self.logger.error("get_of_switches " + error_text)
161 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
162
163 def get_of_rules(self, translate_of_ports=True):
164 """
165 Obtain the rules inserted at openflow controller
166 :param translate_of_ports: if True it translates ports from openflow index to physical switch name
167 :return: dict if ok: with the rule name as key and value is another dictionary with the following content:
168 priority: rule priority
169 name: rule name (present also as the master dict key)
170 ingress_port: match input port of the rule
171 dst_mac: match destination mac address of the rule, can be missing or None if not apply
172 vlan_id: match vlan tag of the rule, can be missing or None if not apply
173 actions: list of actions, composed by a pair tuples:
174 (vlan, None/int): for stripping/setting a vlan tag
175 (out, port): send to this port
176 switch: DPID, all
177 Raise an openflowconnUnexpectedResponse exception if fails with text_error
178 """
179
180 try:
181 # get translation, autodiscover version
182 if len(self.ofi2pp) == 0:
183 self.obtain_port_correspondence()
184
185 of_response = requests.get(self.url + "/wm/%s/list/%s/json" % (self.ver_names["URLmodifier"], self.dpid),
186 headers=self.headers)
187 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
188 if of_response.status_code != 200:
189 self.logger.warning("get_of_rules " + error_text)
190 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
191 self.logger.debug("get_of_rules " + error_text)
192 info = of_response.json()
193 if type(info) != dict:
194 self.logger.error("get_of_rules. Unexpected response not a dict %s", str(type(info)))
195 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, not a dict. Wrong version?")
196 rule_dict = {}
197 for switch, switch_info in info.iteritems():
198 if switch_info == None:
199 continue
200 if str(switch) != self.dpid:
201 continue
202 for name, details in switch_info.iteritems():
203 rule = {}
204 rule["switch"] = str(switch)
205 # rule["active"] = "true"
206 rule["priority"] = int(details["priority"])
207 if self.version[0] == "0":
208 if translate_of_ports:
209 rule["ingress_port"] = self.ofi2pp[details["match"]["inputPort"]]
210 else:
211 rule["ingress_port"] = str(details["match"]["inputPort"])
212 dst_mac = details["match"]["dataLayerDestination"]
213 if dst_mac != "00:00:00:00:00:00":
214 rule["dst_mac"] = dst_mac
215 vlan = details["match"]["dataLayerVirtualLan"]
216 if vlan != -1:
217 rule["vlan_id"] = vlan
218 actionlist = []
219 for action in details["actions"]:
220 if action["type"] == "OUTPUT":
221 if translate_of_ports:
222 port = self.ofi2pp[action["port"]]
223 else:
224 port = action["port"]
225 actionlist.append(("out", port))
226 elif action["type"] == "STRIP_VLAN":
227 actionlist.append(("vlan", None))
228 elif action["type"] == "SET_VLAN_ID":
229 actionlist.append(("vlan", action["virtualLanIdentifier"]))
230 else:
231 actionlist.append((action["type"], str(action)))
232 self.logger.warning("get_of_rules() Unknown action in rule %s: %s", rule["name"],
233 str(action))
234 rule["actions"] = actionlist
235 elif self.version[0] == "1":
236 if translate_of_ports:
237 rule["ingress_port"] = self.ofi2pp[details["match"]["in_port"]]
238 else:
239 rule["ingress_port"] = details["match"]["in_port"]
240 if "eth_dst" in details["match"]:
241 dst_mac = details["match"]["eth_dst"]
242 if dst_mac != "00:00:00:00:00:00":
243 rule["dst_mac"] = dst_mac
244 if "eth_vlan_vid" in details["match"]:
245 vlan = int(details["match"]["eth_vlan_vid"], 16) & 0xFFF
246 rule["vlan_id"] = str(vlan)
247 actionlist = []
248 for action in details["instructions"]["instruction_apply_actions"]:
249 if action == "output":
250 if translate_of_ports:
251 port = self.ofi2pp[details["instructions"]["instruction_apply_actions"]["output"]]
252 else:
253 port = details["instructions"]["instruction_apply_actions"]["output"]
254 actionlist.append(("out", port))
255 elif action == "strip_vlan":
256 actionlist.append(("vlan", None))
257 elif action == "set_vlan_vid":
258 actionlist.append(
259 ("vlan", details["instructions"]["instruction_apply_actions"]["set_vlan_vid"]))
260 else:
261 self.logger.error("get_of_rules Unknown action in rule %s: %s", rule["name"],
262 str(action))
263 # actionlist.append( (action, str(details["instructions"]["instruction_apply_actions"]) ))
264 rule_dict[str(name)] = rule
265 return rule_dict
266 except requests.exceptions.RequestException as e:
267 error_text = type(e).__name__ + ": " + str(e)
268 self.logger.error("get_of_rules " + error_text)
269 raise openflow_conn.OpenflowconnConnectionException(error_text)
270 except ValueError as e:
271 # ValueError in the case that JSON can not be decoded
272 error_text = type(e).__name__ + ": " + str(e)
273 self.logger.error("get_of_rules " + error_text)
274 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
275
276 def obtain_port_correspondence(self):
277 """
278 Obtain the correspondence between physical and openflow port names
279 :return: dictionary: with physical name as key, openflow name as value
280 Raise an openflowconnUnexpectedResponse exception if fails with text_error
281 """
282 try:
283 of_response = requests.get(self.url + "/wm/core/controller/switches/json", headers=self.headers)
284 # print vim_response.status_code
285 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
286 if of_response.status_code != 200:
287 self.logger.warning("obtain_port_correspondence " + error_text)
288 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
289 self.logger.debug("obtain_port_correspondence " + error_text)
290 info = of_response.json()
291
292 if type(info) != list and type(info) != tuple:
293 raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow response, not a list. "
294 "Wrong version?")
295
296 index = -1
297 if len(info) > 0:
298 # autodiscover version
299 if self.version == None:
300 if 'dpid' in info[0] and 'ports' in info[0]:
301 self._set_version("0.9")
302 elif 'switchDPID' in info[0]:
303 self._set_version("1.X")
304 else:
305 raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow response, "
306 "Wrong version?")
307
308 for i in range(0, len(info)):
309 if info[i][self.ver_names["dpid"]] == self.dpid:
310 index = i
311 break
312 if index == -1:
313 text = "DPID '" + self.dpid + "' not present in controller " + self.url
314 # print self.name, ": get_of_controller_info ERROR", text
315 raise openflow_conn.OpenflowconnUnexpectedResponse(text)
316 else:
317 if self.version[0] == "0":
318 ports = info[index]["ports"]
319 else: # version 1.X
320 of_response = requests.get(self.url + "/wm/core/switch/%s/port-desc/json" % self.dpid,
321 headers=self.headers)
322 # print vim_response.status_code
323 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
324 if of_response.status_code != 200:
325 self.logger.warning("obtain_port_correspondence " + error_text)
326 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
327 self.logger.debug("obtain_port_correspondence " + error_text)
328 info = of_response.json()
329 if type(info) != dict:
330 raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow port-desc response, "
331 "not a dict. Wrong version?")
332 if "portDesc" not in info:
333 raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow port-desc response, "
334 "'portDesc' not found. Wrong version?")
335 if type(info["portDesc"]) != list and type(info["portDesc"]) != tuple:
336 raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow port-desc response at "
337 "'portDesc', not a list. Wrong version?")
338 ports = info["portDesc"]
339 for port in ports:
340 self.pp2ofi[str(port["name"])] = str(port["portNumber"])
341 self.ofi2pp[port["portNumber"]] = str(port["name"])
342 # print self.name, ": get_of_controller_info ports:", self.pp2ofi
343 return self.pp2ofi
344 except requests.exceptions.RequestException as e:
345 error_text = type(e).__name__ + ": " + str(e)
346 self.logger.error("obtain_port_correspondence " + error_text)
347 raise openflow_conn.OpenflowconnConnectionException(error_text)
348 except ValueError as e:
349 # ValueError in the case that JSON can not be decoded
350 error_text = type(e).__name__ + ": " + str(e)
351 self.logger.error("obtain_port_correspondence " + error_text)
352 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
353
354 def del_flow(self, flow_name):
355 """
356 Delete an existing rule
357 :param flow_name: this is the rule name
358 :return: None if ok
359 Raise an openflowconnUnexpectedResponse exception if fails with text_error
360 """
361 try:
362
363 # Raise an openflowconnUnexpectedResponse exception if fails with text_error
364 # autodiscover version
365
366 if self.version == None:
367 self.get_of_switches()
368
369 of_response = requests.delete(self.url + "/wm/%s/json" % self.ver_names["URLmodifier"],
370 headers=self.headers,
371 data='{"switch":"%s","name":"%s"}' % (self.dpid, flow_name)
372 )
373 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
374 if of_response.status_code != 200:
375 self.logger.warning("del_flow " + error_text)
376 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
377 self.logger.debug("del_flow OK " + error_text)
378 return None
379
380 except requests.exceptions.RequestException as e:
381 error_text = type(e).__name__ + ": " + str(e)
382 self.logger.error("del_flow " + error_text)
383 raise openflow_conn.OpenflowconnConnectionException(error_text)
384
385 def new_flow(self, data):
386 """
387 Insert a new static rule
388 :param data: dictionary with the following content:
389 priority: rule priority
390 name: rule name
391 ingress_port: match input port of the rule
392 dst_mac: match destination mac address of the rule, missing or None if not apply
393 vlan_id: match vlan tag of the rule, missing or None if not apply
394 actions: list of actions, composed by a pair tuples with these posibilities:
395 ('vlan', None/int): for stripping/setting a vlan tag
396 ('out', port): send to this port
397 :return: None if ok
398 Raise an openflowconnUnexpectedResponse exception if fails with text_error
399 """
400 # get translation, autodiscover version
401 if len(self.pp2ofi) == 0:
402 self.obtain_port_correspondence()
403
404 try:
405 # We have to build the data for the floodlight call from the generic data
406 sdata = {'active': "true", "name": data["name"]}
407 if data.get("priority"):
408 sdata["priority"] = str(data["priority"])
409 if data.get("vlan_id"):
410 sdata[self.ver_names["vlanid"]] = data["vlan_id"]
411 if data.get("dst_mac"):
412 sdata[self.ver_names["destmac"]] = data["dst_mac"]
413 sdata['switch'] = self.dpid
414 if not data['ingress_port'] in self.pp2ofi:
415 error_text = 'Error. Port ' + data['ingress_port'] + ' is not present in the switch'
416 self.logger.warning("new_flow " + error_text)
417 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
418
419 sdata[self.ver_names["inport"]] = self.pp2ofi[data['ingress_port']]
420 sdata['actions'] = ""
421
422 for action in data['actions']:
423 if len(sdata['actions']) > 0:
424 sdata['actions'] += ','
425 if action[0] == "vlan":
426 if action[1] == None:
427 sdata['actions'] += self.ver_names["stripvlan"]
428 else:
429 sdata['actions'] += self.ver_names["setvlan"] + "=" + str(action[1])
430 elif action[0] == 'out':
431 sdata['actions'] += "output=" + self.pp2ofi[action[1]]
432
433 of_response = requests.post(self.url + "/wm/%s/json" % self.ver_names["URLmodifier"],
434 headers=self.headers, data=json.dumps(sdata))
435 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
436 if of_response.status_code != 200:
437 self.logger.warning("new_flow " + error_text)
438 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
439 self.logger.debug("new_flow OK" + error_text)
440 return None
441
442 except requests.exceptions.RequestException as e:
443 error_text = type(e).__name__ + ": " + str(e)
444 self.logger.error("new_flow " + error_text)
445 raise openflow_conn.OpenflowconnConnectionException(error_text)
446
447 def clear_all_flows(self):
448 """
449 Delete all existing rules
450 :return: None if ok
451 Raise an openflowconnUnexpectedResponse exception if fails with text_error
452 """
453
454 try:
455 # autodiscover version
456 if self.version == None:
457 sw_list = self.get_of_switches()
458 if len(sw_list) == 0: # empty
459 return None
460
461 url = self.url + "/wm/%s/clear/%s/json" % (self.ver_names["URLmodifier"], self.dpid)
462 of_response = requests.get(url)
463 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
464 if of_response.status_code < 200 or of_response.status_code >= 300:
465 self.logger.warning("clear_all_flows " + error_text)
466 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
467 self.logger.debug("clear_all_flows OK " + error_text)
468 return None
469 except requests.exceptions.RequestException as e:
470 error_text = type(e).__name__ + ": " + str(e)
471 self.logger.error("clear_all_flows " + error_text)
472 raise openflow_conn.OpenflowconnConnectionException(error_text)
473