本系列教程是我在学习SDN课程的时候写下的笔记, 偏向实践, 环境为Mininet+openflow(POX 控制器)
POX是一个python编写的控制器,仅仅支持opneflow1.0
关于POX的资料非常少,唯一有参考价值的是官方的Wiki(英文):
https://openflow.stanford.edu/display/ONL/POX+Wiki.html
并且这个文档真的很难看懂,必须结合源代码和examples才能慢慢搞懂POX的使用, 本篇文章我尽量总结一些POX的使用方法。
1. 快速总览POX:
POX主要包括 : Listener, Control logic, Messenger.
首先要确定需要POX 监听何种event(e.g. ConnectionUp, PacketIn...)
然后使用logic来区分不同的flow并且执行相应action
最后给siwtches发message来安装新的rule到flow tables里面。
深入到代码里:
def lanch():
这个类似于main()函数,会自动执行。“This is the function where the application registers all event listeners, and creates objects of any application class. ”
Listeners:
There are two common ways for an application to register with the controller for events.
Register callback functions for specific events thrown by either the OpenFlow handler module or specific modules like Topology Discovery
From the launch or from the init of a class, perform core.openflow.addListenerByName(“EVENTNAME”, CALLBACK_FUNC, PRIORITY)
Register object with the OpenFlow handler module or specific modules like Topology Discovery
From typically the init of a class, perform addListeners(self). Once this is added, the controller will look for a function with the name _handle_EVENTNAME(self, event). This method is automatically registered as an event handler. [1]
解析packets:
POX provides several primitives to parse well-known packets. The ethernet packet received through a packet_in event can be extracted using this command and then the match for the flow rules can be created using the from_packet function:
packet = event.parsed
src_mac = packet.src
dst_mac = packet.dst
if packet.type == ethernet.IP_TYPE:
ipv4_packet = event.parsed.find("ipv4")
# Do more processing of the IPv4 packet
src_ip = ipv4_packet.srcip
src_ip = ipv4_packet.dstip
POX provides a simple way to construct the match headers from an incoming packet:
match = of.ofp_match.from_packet(packet) [1]
发message给switch:
Sending packet_out, flow_mod and other OpenFlow messages to program the switch is simple with POX. The steps are to construct the appropriate message object and populate it with the decision fields and the send to the switch using the connection object self.connection.send(). Here is an example from the hub implementation:
msg = of.ofp_packet_out() # Create packet out message
msg.buffer_id = event.ofp.buffer_id # Use the incoming packet as the data for the packet out
msg.in_port = packet_in.in_port # Set the in_port so that the switch knows
msg.match = of.ofp_match.from_packet(packet)
# Add an action to send to the specified port
action = of.ofp_action_output(port = of.OFPP_FLOOD)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg) [1]
2. 处理事件event
POX event 符合 publish/subscribe paradigm
会自动handle监听的对应事件
事件event主要来源于siwtch的message, 所有的events都具有以下属性
图1:event属性PacketIn
Fired when the controller receives an OpenFlow packet-in message (ofp_packet_in / OFPT_PACKET_IN) from a switch, which indicates that a packet arriving at a switch port has either failed to match all entries in the table, or the matching entry included an action specifying to send the packet to the controller.
ConnectionUp:
it's simply fired in response to the establishment of a new control channel with a switch.
Ref:
[1].http://sdnhub.org/tutorials/pox/
[2].https://openflow.stanford.edu/display/ONL/POX+Wiki.html#POXWiki-OpenFlowMessages
网友评论