Floodlight OpenFlow Controller: Using the Static Flow Entry Pusher
Floodlight OpenFlow Controller: Using the Static Flow Entry Pusher: The first post in this tutorial can be found at Tutorial to Build a FloodLight SDN OpenFlow Controller .For those looking to do things with early SDN / OpenFlow controllers this post may be of some use to kickstart your efforts. I am posting a Java class below that Cory Fowler and I put together. There are lots of good guides out there but what exactly I was looking for was unavaialble at the time.
Packet-in Logic
The goal was to get the framework together for this project, but I didn't see anything out there that did it from within Floodlight with a Java module. Fortunately, the guys from BigSwitch built a nice module that abstracted some of the nuances of doing a FlowMod like OFAction fields, setting lengths, flushing between flows and the dozens of other little gotchas that need layers of abstraction to simplify.
Using the Floodlight Static Flow Entry Pusher Service
What this allows for is to proactively instantiate a static flow to define a predetermined path based on a match and then the corresponding action.- Flow comes into the OpenFlow enabled switch.
- Switch looks in its forwarding table to see if there is a match. If there is a match the switch forwards that packet per the forwarding table instruction. The matches can either be a coarse match like anything destined to IPv4 10.0.0.0/8 ---> do this action, or as fine as anything matching {port 80, mac addr 0000.1111.2222, dest_ipv4 10.1.1.1/32, (any other tuple in the header) etc}. Think traditional _access-lists_.
- If there is not a matching flow in the switch an event is triggered creating a packet-in packet to the controller where the logic in the code below could be processed.
- After the controller determines an action it then creates a packet-out packet that tells the switch what action to take with that flow and subsequent packets in that same flow.
- String name: The name variable is an arbitrary name, but it must be unique to the static flow. If you use the same name it will be overwritten. Very similar to access list line numbers.
- OFFlowMod fm: fm is the flow to push.
- String swDpid: Is the switch datapath ID (DPID) to push it to. The Mininet default DPID is 00:00:00:00:00:00:00:01.
- Floodlight - Lots of nice documents for developers to understand how to get started. The Floodlight listserv is very helpful also.
- Beacon - David has lots of good tutorials at the Beacon OpenFlow homepage. FloodLight was built on top of Ericksons controller Beacon. David is a nice guy that does an amazing job answering questions on the Beacon forum. Pretty neat to have the chance to ask questions directly from a pioneer like him. He has just updated his development tutorial so take a look at Creating a Learning Switch and help with feedback on the Beacon forum if interested.
- POX - For those more comfortable with programming with Python have a look at Murphy McCauley's project POX. Murphy is another very accessible community oriented researcher.
- For those wanting to just get a basic understanding of OpenFlow or even just watch a video of it and don't care about the nitty gritty programming details, this tutorial I did a while back. It needs a makeover but its probably enough to get the point. I need to update it but its just basic usage of the RESTful Floodlight API.
- Brad Casemore
- Greg Ferro
- Ivan Pepelnjak
- Roy Chua
- OpenStack Quantum: Take a look at a real life networking API in production today in the OpenStack project.
The method to add a flow requires three parameters addFlow(String name, OFFlowMod fm, String swDpid);
//net.floodlightcontroller.names is a created package for something else, not part of FloodLight to make sure there isn't any confusion.
package net.floodlightcontroller.names;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.openflow.protocol.OFFlowMod;
import org.openflow.protocol.OFMatch;
import org.openflow.protocol.OFMessage;
import org.openflow.protocol.OFPacketIn;
import org.openflow.protocol.OFType;
import org.openflow.protocol.action.OFAction;
import org.openflow.protocol.action.OFActionOutput;
import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.IOFMessageListener;
import net.floodlightcontroller.core.IOFSwitch;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPv4;
import net.floodlightcontroller.staticflowentry.IStaticFlowEntryPusherService;
//import net.floodlightcontroller.staticflowentry.StaticFlowEntryPusher;
public class Names implements IFloodlightModule, IOFMessageListener {
protected IFloodlightProviderService floodlightProvider;
protected IStaticFlowEntryPusherService staticFlowEntryPusher;
@Override
public String getName() {
return "Names";
}
@Override
public boolean isCallbackOrderingPrereq(OFType type, String name) {
return false;
}
@Override
public boolean isCallbackOrderingPostreq(OFType type, String name) {
return false;
}
@Override
public net.floodlightcontroller.core.IListener.Command receive(
IOFSwitch sw, OFMessage msg, FloodlightContext cntx ) {
String dp = "00:00:00:00:00:00:00:01";
OFPacketIn pi = (OFPacketIn) msg;
OFMatch match = new OFMatch();
match.loadFromPacket(pi.getPacketData(), (short) 0);
System.out.println("$$$$$$$$$OFMATCH$$$$$$$$$$$");
System.out.println(match);
System.out.println("$$$$$$$$$IP-Destination$$$$$$$$$$$");
System.out.println("$$$$$$$$$IP-Destination$$$$$$$$$$$");
System.out.println(IPv4.fromIPv4Address(match.getNetworkDestination()));
//IPv4-To
List actionsTo = new ArrayList();
// Declare the flow
OFFlowMod fmTo = new OFFlowMod();
fmTo.setType(OFType.FLOW_MOD);
// Declare the action
OFAction outputTo = new OFActionOutput((short) 2);
actionsTo.add(outputTo);
// Declare the match
OFMatch mTo = new OFMatch();
mTo.setNetworkDestination(IPv4.toIPv4Address("10.0.0.3"));
mTo.setDataLayerType(Ethernet.TYPE_IPv4);
fmTo.setActions(actionsTo);
fmTo.setMatch(mTo);
// Push the flow
staticFlowEntryPusher.addFlow("FlowTo", fmTo, dp);
//IPv4-From
List actionsFrom = new ArrayList();
// Declare the flow
OFFlowMod fmFrom = new OFFlowMod();
fmFrom.setType(OFType.FLOW_MOD);
// Declare the action
OFAction outputFrom = new OFActionOutput((short) 1);
actionsFrom.add(outputFrom);
// Declare the match
OFMatch mFrom = new OFMatch();
mFrom.setNetworkDestination(IPv4.toIPv4Address("10.0.0.2"));
mFrom.setDataLayerType(Ethernet.TYPE_IPv4);
fmFrom.setActions(actionsFrom);
fmFrom.setMatch(mFrom);
// Push the flow
staticFlowEntryPusher.addFlow("FlowFrom", fmFrom, dp);
//ToArp
List actionsToArp = new ArrayList();
// Declare the flow
OFFlowMod fmToArp = new OFFlowMod();
fmToArp.setType(OFType.FLOW_MOD);
// Declare the action
OFAction outputToArp = new OFActionOutput((short) 2);
actionsToArp.add(outputToArp);
// Declare the match
OFMatch mToArp = new OFMatch();
mToArp.setNetworkDestination(IPv4.toIPv4Address("10.0.0.3"));
mToArp.setDataLayerType(Ethernet.TYPE_ARP);
fmToArp.setActions(actionsToArp);
fmToArp.setMatch(mToArp);
// Push the flow
staticFlowEntryPusher.addFlow("FlowToArp", fmToArp, dp);
//ArpFrom
List actionsFromArp = new ArrayList();
// Declare the flow
OFFlowMod fmFromArp = new OFFlowMod();
fmFromArp.setType(OFType.FLOW_MOD);
// Declare the action
OFAction outputFromArp = new OFActionOutput((short) 1);
actionsFromArp.add(outputFromArp);
// Declare the match
OFMatch mFromArp = new OFMatch();
mFromArp.setNetworkDestination(IPv4.toIPv4Address("10.0.0.2"));
mFromArp.setDataLayerType(Ethernet.TYPE_ARP);
fmFromArp.setActions(actionsFromArp);
fmFromArp.setMatch(mFromArp);
// Push the flow
staticFlowEntryPusher.addFlow("FlowFromArp", fmFromArp, dp);
System.out.println("$$$$$$$$$OFMATCH$$$$$$$$$$$");
System.out.println(mTo);
return Command.CONTINUE;
}
@Override
public Collection<Class> getModuleServices() {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<Class, IFloodlightService> getServiceImpls() {
// TODO Auto-generated method stub
return null;
}
//Service Class l.add(IStaticFlowEntryPusherService.class);
@Override
public Collection<Class> getModuleDependencies() {
Collection<Class> l = new ArrayList<Class>();
l.add(IFloodlightProviderService.class);
l.add(IStaticFlowEntryPusherService.class);
return l;
}
// Context object SFP Class
@Override
public void init(FloodlightModuleContext context)
throws FloodlightModuleException {
floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
staticFlowEntryPusher = context.getServiceImpl(IStaticFlowEntryPusherService.class);
}
@Override
public void startUp(FloodlightModuleContext context) {
floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
}
}Testing the Class with Mininet
For testing I normally use Mininet. If you look at the first post you can see the video and how to attach mininet switches to the controller running in Eclipse and use Xterm to source traffic from your simulated hosts.Additional OpenFlow SDN Development Resources
Here are some open source OpenFlow controller projects and links to their documentation tutorials and guides.Programming applications directly into OpenFlow primitives is likely not the path for any semblance of DevOps networking gets in to. There will be additional layers that will enable :Policy in -> Flows out. These additional layers are cropping up in the form of APIs and additional languages that are creating the SDN policy wood chipper to create flows. I discussed a couple of those projects in this post: Software Defined Network (SDN) -Looking Toward 2013
Here are some additional reading on networking APIs from some friends:
Learn more about Floodlight development on the Floodlight-Dev Listserv
[http://networkstatic.net/wp-content/uploads/2013/02/openflow-spec-v1.3.0.pdf](https://networkstatic.net/wp-content/uploads/2013/02/openflow-spec-v1.3.0.pdf)
Hope this helps add some documentation for those looking to dig under the hood a bit and create their own networking applications for research or a network that fits their needs. Thanks for stopping by.
Download OpenFlow v1.3