败笔_小败吧 关注:18贴子:1,289
  • 3回复贴,共1
package net.floodlightcontroller.rotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.projectfloodlight.openflow.protocol.OFFactories;
import org.projectfloodlight.openflow.protocol.OFFactory;
import org.projectfloodlight.openflow.protocol.OFFlowAdd;
import org.projectfloodlight.openflow.protocol.OFMessage;
import org.projectfloodlight.openflow.protocol.OFPacketIn;
import org.projectfloodlight.openflow.protocol.OFPacketOut;
import org.projectfloodlight.openflow.protocol.OFType;
import org.projectfloodlight.openflow.protocol.OFVersion;
import org.projectfloodlight.openflow.protocol.action.OFAction;
import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
import org.projectfloodlight.openflow.protocol.action.OFActions;
import org.projectfloodlight.openflow.protocol.match.Match;
import org.projectfloodlight.openflow.protocol.match.MatchField;
import org.projectfloodlight.openflow.protocol.ver10.OFFactoryVer10;
import org.projectfloodlight.openflow.types.EthType;
import org.projectfloodlight.openflow.types.IPv4Address;
import org.projectfloodlight.openflow.types.IpProtocol;
import org.projectfloodlight.openflow.types.MacAddress;
import org.projectfloodlight.openflow.types.OFBufferId;
import org.projectfloodlight.openflow.types.OFPort;
import org.projectfloodlight.openflow.types.TransportPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ARP;
import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPacket;
import net.floodlightcontroller.packet.IPv4;
public class Rotation implements IFloodlightModule, IOFMessageListener {
protected IFloodlightProviderService floodlightProvider;
protected static Logger logger;
private final static int SERVER_VIRTUAL_IP = IPv4.toIPv4Address("10.0.0.254");//Server virtual IP
private final static byte[] SERVER_VIRTUAL_MAC = Ethernet.toMACAddress("00:00:00:00:00:FE");//Server virtual MAC
private final static int SERVER_PORT = 80;//Server port
private final static short HARD_TIMEOUT = 10;
private final static short IDLE_TIMEOUT = 10;
private static class Server{
private int ip;
private byte[] mac;
private int phyPort;
public Server(String ip,String mac,int port){
this.ip = IPv4.toIPv4Address(ip);
this.mac = Ethernet.toMACAddress(mac);
this.phyPort = port;
}
public int getIP(){
return this.ip;
}
public byte[] getMAC() {
return this.mac;
}
public int getPhysicalPort(){
return this.phyPort;
}
}
//ServerA and ServerB
final static Server[] SERVERS = {
new Server("10.0.0.1", "00:00:00:00:00:01", 2),
new Server("10.0.0.2", "00:00:00:00:00:02", 3)
};
private int lastServer = 0;
//Determines the next server to which a flow should be sent.
private Server getNextServer(){
lastServer = (lastServer + 1) % SERVERS.length;
return SERVERS[lastServer];
}
@Override
public String getName() {
return Rotation.class.getSimpleName();
}
@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) {
switch (msg.getType()) {
case PACKET_IN://Received PacketIn Message
OFPacketIn packetIn = (OFPacketIn)msg;
byte[] packetInData = packetIn.getData();
//The Data of Ethernet frame in PacketIn Message
Ethernet ethInPacketIn = new Ethernet();
ethInPacketIn.deserialize(packetInData, 0, packetIn.getTotalLen());
logger.info("Received PacketIn message,the Data:"+ethInPacketIn.toString());
if (ethInPacketIn.getEtherType() == Ethernet.TYPE_ARP) {//Received ARP Packet
handlerARPRequest(sw,packetIn ,ethInPacketIn, cntx);
}else if (ethInPacketIn.getEtherType()==Ethernet.TYPE_IPv4) {//Received IPv4 Packet
loadBalanceFlow(sw, packetIn, ethInPacketIn, cntx);
}
break;
default:
break;
}
return Command.CONTINUE;
}
/**
* Handler the ARP request
* @param sw
* @param packetIn
* @param ethInPacketIn
* @param cntx
*/
private void handlerARPRequest(IOFSwitch sw,OFPacketIn packetIn ,Ethernet ethInPacketIn, FloodlightContext cntx){
if (!(ethInPacketIn.getPayload() instanceof ARP)) {
return;
}
logger.info("Received an ARP request from :" + ethInPacketIn.getSourceMACAddress().toString());
ARP arpRequest = (ARP)ethInPacketIn.getPayload();
//logger.info("Received an ARP request Payload from :" + ethInPacketIn.getSourceMACAddress().toString());
//logger.info(arpRequest.getTargetProtocolAddress().toString() + SERVER_VIRTUAL_IP);
//Judge the ARP TargetIPAddress is the Server virtual IP address
if(IPv4Address.of(arpRequest.getTargetProtocolAddress()).compareTo(IPv4Address.of(SERVER_VIRTUAL_IP))==0){
logger.info("Received an ARP request for the Virtual Server:"+IPv4Address.of(arpRequest.getTargetProtocolAddress()).toString());
//Generate ARP Reply
IPacket arpReply = new Ethernet()
.setSourceMACAddress(SERVER_VIRTUAL_MAC)
.setDestinationMACAddress(ethInPacketIn.getSourceMACAddress())
.setEtherType(Ethernet.TYPE_ARP)
.setPriorityCode(ethInPacketIn.getPriorityCode())
.setPayload(
new ARP()
.setHardwareType(ARP.HW_TYPE_ETHERNET)
.setProtocolType(ARP.PROTO_TYPE_IP)
.setHardwareAddressLength((byte)6)
.setProtocolAddressLength((byte)4)
.setOpCode(ARP.OP_REPLY)
.setSenderHardwareAddress(SERVER_VIRTUAL_MAC)
.setSenderProtocolAddress(SERVER_VIRTUAL_IP)
.setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
.setTargetProtocolAddress(arpRequest.getSenderProtocolAddress())
);
//Send ARP Reply out to the Switch
OFFactory my10Factory = OFFactories.getFactory(OFVersion.OF_10);
ArrayList<OFAction> arpActionList = new ArrayList<OFAction>();
//Set output actions
OFActions arpActions = my10Factory.actions();
OFActionOutput output = arpActions.buildOutput()
.setPort(packetIn.getInPort())
.setMaxLen((short)0xFFFF)
.build();
arpActionList.add(output);
//Generate a PacketOut Message
OFPacketOut packetOut = my10Factory.buildPacketOut()
.setBufferId(OFBufferId.NO_BUFFER)
.setInPort(OFPort.ANY)
.setActions(arpActionList)
.setData(arpReply.serialize())
.build();
logger.info("Send a ARP Reply");
sw.write(packetOut);
sw.flush();
}
}
/**
* Handler the packet for the Server,to modify the filed and to add flow
* @param sw
* @param packetIn
* @param ethInPacketIn
* @param cntx
*/
private void loadBalanceFlow(IOFSwitch sw,OFPacketIn packetIn ,Ethernet ethInPacketIn, FloodlightContext cntx){
if (!(ethInPacketIn.getPayload() instanceof IPv4)) {
return;
}
IPv4 ipv4InPacketIn = (IPv4)(ethInPacketIn.getPayload());//The Data of IPv4 in PacketIn Packet
if(ipv4InPacketIn.getDestinationAddress().getInt()==SERVER_VIRTUAL_IP){
logger.info("Received an IPv4 packet destioned for the Virtual Server:"+ipv4InPacketIn.getDestinationAddress().toString());
//IPv4 packet Handler
//Create a flow table modification message to add a rule
OFFactoryVer10 my10Factory = new OFFactoryVer10();
Match match1 = my10Factory.buildMatchV1()//The Match for rule
.setExact(MatchField.ETH_TYPE, EthType.IPv4)
.setExact(MatchField.ETH_DST, MacAddress.of(SERVER_VIRTUAL_MAC))
.setExact(MatchField.IP_PROTO, IpProtocol.TCP)
.setExact(MatchField.IPV4_DST, IPv4Address.of(SERVER_VIRTUAL_IP))
.setExact(MatchField.TCP_DST, TransportPort.of(SERVER_PORT))
.build();
ArrayList<OFAction> actionList1 = new ArrayList<OFAction>();//Action List
Server server = getNextServer();
logger.info("The current Server is :" + server.getIP());
OFActions actions1 = my10Factory.actions();
OFActionSetDlDst setDlDst = actions1//Set the Destination MAC Address
.buildSetDlDst()
.setDlAddr(MacAddress.of(server.getMAC()))
.build();
OFActionSetNwDst setNwDst = actions1//Set the Destination IP Address
.buildSetNwDst()
.setNwAddr(IPv4Address.of(server.getIP()))
.build();
OFActionOutput output = actions1//Set output port
.buildOutput()
.setPort(OFPort.ofInt(server.getPhysicalPort()))
.build();
actionList1.add(setDlDst);
actionList1.add(setNwDst);
actionList1.add(output);
OFFlowAdd flowRule1 = my10Factory.buildFlowAdd()//Add Flow Message-(Modify-Mod Message)
.setBufferId(OFBufferId.NO_BUFFER)
.setHardTimeout(HARD_TIMEOUT)
.setIdleTimeout(IDLE_TIMEOUT)
.setPriority(32768)
.setMatch(match1)
.setActions(actionList1)
.build();
logger.info("The Match is :" + match1.toString());
logger.info("Add A flow rule:"+flowRule1.toString());
sw.write(flowRule1);
//Create a flow table modification message to add a rule for the reverse direction
OFActions actions2 = my10Factory.actions();
Match match2 = my10Factory.buildMatchV1()
.setExact(MatchField.ETH_TYPE, EthType.IPv4)
.setExact(MatchField.ETH_SRC, MacAddress.of(server.getMAC()))
.setExact(MatchField.ETH_DST, ethInPacketIn.getSourceMACAddress())
.setExact(MatchField.IPV4_SRC, IPv4Address.of(server.getIP()))
.setExact(MatchField.IPV4_DST, ipv4InPacketIn.getSourceAddress())
.setExact(MatchField.IN_PORT, OFPort.of(server.getPhysicalPort()))
.setExact(MatchField.IP_PROTO, IpProtocol.TCP)
.build();
ArrayList<OFAction> actionList2 = new ArrayList<OFAction>();
OFActionSetDlSrc setDlSrc = actions2//Set Source MAC Address
.buildSetDlSrc()
.setDlAddr(MacAddress.of(SERVER_VIRTUAL_MAC))
.build();
OFActionSetNwSrc setNwSrc = actions2//Set Source IP Address
.buildSetNwSrc()
.setNwAddr(IPv4Address.of(SERVER_VIRTUAL_IP))
.build();
OFActionOutput reverseOutput = actions2//Set output port
.buildOutput()
.setPort(packetIn.getInPort())
.build();
actionList2.add(setDlSrc);
actionList2.add(setNwSrc);
actionList2.add(reverseOutput);
OFFlowAdd flowRule2 = my10Factory.buildFlowAdd()
.setBufferId(OFBufferId.NO_BUFFER)
.setHardTimeout(HARD_TIMEOUT)
.setIdleTimeout(IDLE_TIMEOUT)
.setPriority(32767)
.setMatch(match2)
.setActions(actionList2)
.build();
logger.info("The Match is :" + match2.toString());
logger.info("Add A flow rule:"+flowRule2.toString());
sw.write(flowRule2);
//Send Controller handling Data to Server
pushPacket(sw, packetIn, actionList1);
}
}
/**
* Send the Packet which is in the PacketIn Message or which buffered in Switch
* @param sw
* @param packetIn
* @param actionList
*/
private void pushPacket(IOFSwitch sw,OFPacketIn packetIn,ArrayList<OFAction> actionList){
OFFactory my10Factory = OFFactories.getFactory(OFVersion.OF_10);
OFPacketOut.Builder packetOutBuilder = my10Factory.buildPacketOut()
.setBufferId(packetIn.getBufferId())
.setInPort(packetIn.getInPort())
.setActions(actionList);
//Set packet data
if(packetIn.getBufferId() == OFBufferId.NO_BUFFER){
byte[] packetData = packetIn.getData();
packetOutBuilder.setData(packetData);
}
OFPacketOut packetOut = packetOutBuilder.build();
sw.write(packetOut);
}
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
return null;
}
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
return null;
}
@Override
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
l.add(IFloodlightProviderService.class);
return l;
}
@Override
public void init(FloodlightModuleContext context)
throws FloodlightModuleException {
floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
logger = LoggerFactory.getLogger(Rotation.class);
}
@Override
public void startUp(FloodlightModuleContext context)
throws FloodlightModuleException {
floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
}
}


IP属地:浙江1楼2015-05-13 21:50回复
    package net.floodlightcontroller.core.web;
    import java.io.IOException;
    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.OFPacketOut;
    import org.openflow.protocol.OFPort;
    import org.openflow.protocol.OFType;
    import org.openflow.protocol.action.OFAction;
    import org.openflow.protocol.action.OFActionDataLayerDestination;
    import org.openflow.protocol.action.OFActionDataLayerSource;
    import org.openflow.protocol.action.OFActionNetworkLayerDestination;
    import org.openflow.protocol.action.OFActionNetworkLayerSource;
    import org.openflow.protocol.action.OFActionOutput;
    import org.openflow.util.U16;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    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.ARP;
    import net.floodlightcontroller.packet.Ethernet;
    import net.floodlightcontroller.packet.IPacket;
    import net.floodlightcontroller.packet.IPv4;


    IP属地:浙江2楼2015-05-14 21:44
    回复
      然而并没有什么软用


      IP属地:浙江来自Android客户端3楼2015-05-16 10:53
      收起回复