/** * Generates a DHCP request OFPacketIn. * @param hostMac The host MAC address of for the request. * @return An OFPacketIn that contains a DHCP request packet. */ public static OFPacketIn DhcpDiscoveryRequestOFPacketIn(IOFSwitch sw, MacAddress hostMac) { byte[] serializedPacket = DhcpDiscoveryRequestEthernet(hostMac).serialize(); OFFactory factory = sw.getOFFactory(); OFPacketIn.Builder packetInBuilder = factory.buildPacketIn(); if (factory.getVersion() == OFVersion.OF_10) { packetInBuilder .setInPort(OFPort.of(1)) .setData(serializedPacket) .setReason(OFPacketInReason.NO_MATCH); } else { packetInBuilder .setMatch(factory.buildMatch().setExact(MatchField.IN_PORT, OFPort.of(1)).build()) .setData(serializedPacket) .setReason(OFPacketInReason.NO_MATCH); } return packetInBuilder.build(); }
public void dispatchMessage(IOFSwitch sw, OFMessage msg, FloodlightContext bc) { List<IOFMessageListener> theListeners = listeners.get(msg.getType()).getOrderedListeners(); if (theListeners != null) { Command result = Command.CONTINUE; Iterator<IOFMessageListener> it = theListeners.iterator(); if (OFType.PACKET_IN.equals(msg.getType())) { OFPacketIn pi = (OFPacketIn)msg; Ethernet eth = new Ethernet(); eth.deserialize(pi.getData(), 0, pi.getData().length); IFloodlightProviderService.bcStore.put(bc, IFloodlightProviderService.CONTEXT_PI_PAYLOAD, eth); } while (it.hasNext() && !Command.STOP.equals(result)) { result = it.next().receive(sw, msg, bc); } } // paag for (IControllerCompletionListener listener:completionListeners) listener.onMessageConsumed(sw, msg, bc); }
private OFMessage createHubPacketOut(IOFSwitch sw, OFMessage msg) { OFPacketIn pi = (OFPacketIn) msg; OFPacketOut.Builder pob = sw.getOFFactory().buildPacketOut(); pob.setBufferId(pi.getBufferId()).setXid(pi.getXid()).setInPort((pi.getVersion().compareTo(OFVersion.OF_12) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT))); // set actions OFActionOutput.Builder actionBuilder = sw.getOFFactory().actions().buildOutput(); actionBuilder.setPort(OFPort.FLOOD); pob.setActions(Collections.singletonList((OFAction) actionBuilder.build())); // set data if it is included in the packetin if (pi.getBufferId() == OFBufferId.NO_BUFFER) { byte[] packetData = pi.getData(); pob.setData(packetData); } return pob.build(); }
private OFPacketIn createPacketIn(String srcMAC, String dstMAC, String srcIp, String dstIp, short vlan) { IPacket testPacket = new Ethernet() .setDestinationMACAddress(dstMAC) .setSourceMACAddress(srcMAC) .setVlanID(vlan) .setEtherType(EthType.IPv4) .setPayload( new IPv4() .setTtl((byte) 128) .setSourceAddress(srcIp) .setDestinationAddress(dstIp) .setPayload(new UDP() .setSourcePort((short) 5000) .setDestinationPort((short) 5001) .setPayload(new Data(new byte[] {0x01})))); byte[] testPacketSerialized = testPacket.serialize(); OFPacketIn pi; // build out input packet pi = OFFactories.getFactory(OFVersion.OF_13).buildPacketIn() .setBufferId(OFBufferId.NO_BUFFER) .setData(testPacketSerialized) .setReason(OFPacketInReason.NO_MATCH) .build(); return pi; }
/** * If the packet-in switch port is disabled for all data traffic, then * the packet will be dropped. Otherwise, the packet will follow the * normal processing chain. * @param sw * @param pi * @param cntx * @return */ protected Command dropFilter(DatapathId sw, OFPacketIn pi, FloodlightContext cntx) { Command result = Command.CONTINUE; OFPort inPort = (pi.getVersion().compareTo(OFVersion.OF_12) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)); // If the input port is not allowed for data traffic, drop everything. // BDDP packets will not reach this stage. if (isAllowed(sw, inPort) == false) { if (log.isTraceEnabled()) { log.trace("Ignoring packet because of topology " + "restriction on switch={}, port={}", sw.getLong(), inPort.getPortNumber()); result = Command.STOP; } } return result; }
protected Command processPacketInMessage(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx) { // get the packet-in switch. Ethernet eth = IFloodlightProviderService.bcStore. get(cntx,IFloodlightProviderService.CONTEXT_PI_PAYLOAD); if (eth.getPayload() instanceof BSN) { BSN bsn = (BSN) eth.getPayload(); if (bsn == null) return Command.STOP; if (bsn.getPayload() == null) return Command.STOP; // It could be a packet other than BSN LLDP, therefore // continue with the regular processing. if (bsn.getPayload() instanceof LLDP == false) return Command.CONTINUE; doFloodBDDP(sw.getId(), pi, cntx); return Command.STOP; } else { return dropFilter(sw.getId(), pi, cntx); } }
@Override public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { if (!this.enabled) { return Command.CONTINUE; } switch (msg.getType()) { case PACKET_IN: IRoutingDecision decision = null; if (cntx != null) { decision = IRoutingDecision.rtStore.get(cntx, IRoutingDecision.CONTEXT_DECISION); return this.processPacketInMessage(sw, (OFPacketIn) msg, decision, cntx); } break; default: break; } return Command.CONTINUE; }
public void dispatchMessage(IOFSwitch sw, OFMessage msg, FloodlightContext bc) { List<IOFMessageListener> theListeners = listeners.get(msg.getType()).getOrderedListeners(); if (theListeners != null) { Command result = Command.CONTINUE; Iterator<IOFMessageListener> it = theListeners.iterator(); if (OFType.PACKET_IN.equals(msg.getType())) { OFPacketIn pi = (OFPacketIn)msg; Ethernet eth = new Ethernet(); eth.deserialize(pi.getData(), 0, pi.getData().length); IFloodlightProviderService.bcStore.put(bc, IFloodlightProviderService.CONTEXT_PI_PAYLOAD, eth); } while (it.hasNext() && !Command.STOP.equals(result)) { result = it.next().receive(sw, msg, bc); } } }
/** * Test dispatch of messages while in MASTER role */ @Test public void testMessageDispatchMaster() throws Exception { testInitialMoveToMasterWithRole(); // Send packet in. expect dispatch OFPacketIn pi = factory.buildPacketIn() .setReason(OFPacketInReason.NO_MATCH) .build(); reset(switchManager); switchManager.handleMessage(sw, pi, null); expectLastCall().once(); replay(switchManager); switchHandler.processOFMessage(pi); // TODO: many more to go }
public void updateTopologyMappings(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx) { Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,IFloodlightProviderService.CONTEXT_PI_PAYLOAD); if (eth.getPayload() instanceof IPv4) { IPv4 ip_pkt = (IPv4) eth.getPayload(); if (ip_pkt.getSourceAddress().getInt() > 0) { IpToMac.put(ip_pkt.getSourceAddress(), eth.getSourceMACAddress()); IpToSwitch.put(ip_pkt.getSourceAddress(),new SwitchHostInfo(sw,pi.getMatch().get(MatchField.IN_PORT))); } } else if (eth.getPayload() instanceof ARP) { ARP arp_pkt = (ARP) eth.getPayload(); if (IPv4Address.of(arp_pkt.getSenderProtocolAddress()).getInt() > 0) { if (!IPv4Address.of(arp_pkt.getSenderProtocolAddress()).toString().contentEquals("10.0.0.111")) {// ignore crafted requests from switches IpToMac.put(IPv4Address.of(arp_pkt.getSenderProtocolAddress()), eth.getSourceMACAddress()); IpToSwitch.put(IPv4Address.of(arp_pkt.getSenderProtocolAddress()),new SwitchHostInfo(sw,pi.getMatch().get(MatchField.IN_PORT))); } } } }
@Override public net.floodlightcontroller.core.IListener.Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,IFloodlightProviderService.CONTEXT_PI_PAYLOAD); oTopologyManager.updateTopologyMappings(sw, (OFPacketIn) msg, cntx); //log.debug("receive {}",eth); if ((eth.getPayload() instanceof ARP)) { handleARP(sw, (OFPacketIn) msg, cntx); } else if (eth.getPayload() instanceof IPv4) { handleIP(sw, (OFPacketIn) msg, cntx); } else { //handleCbench(sw, (OFPacketIn) msg, cntx); //log.warn("could not handle packet {}",eth.toString()); } return Command.CONTINUE; }
/** * Writes an OFPacketOut message to a switch. * @param sw The switch to write the PacketOut to. * @param packetInMessage The corresponding PacketIn. * @param egressPort The switchport to output the PacketOut. */ private void writePacketOutForPacketIn(IOFSwitch sw, OFPacketIn packetInMessage, OFPort egressPort) { OFPacketOut.Builder pob = sw.getOFFactory().buildPacketOut(); // Set buffer_id, in_port, actions_len pob.setBufferId(packetInMessage.getBufferId()); pob.setInPort(packetInMessage.getVersion().compareTo(OFVersion.OF_12) < 0 ? packetInMessage.getInPort() : packetInMessage.getMatch().get(MatchField.IN_PORT)); // set actions List<OFAction> actions = new ArrayList<OFAction>(1); actions.add(sw.getOFFactory().actions().buildOutput().setPort(egressPort).setMaxLen(0xffFFffFF).build()); pob.setActions(actions); // set data - only if buffer_id == -1 if (packetInMessage.getBufferId() == OFBufferId.NO_BUFFER) { byte[] packetData = packetInMessage.getData(); pob.setData(packetData); } // and write it out counterPacketOut.increment(); sw.write(pob.build()); }
@Override @LogMessageDoc(level="WARN", message="Received PacketIn from switch {} while" + "being slave. Reasserting slave role.", explanation="The switch has receive a PacketIn despite being " + "in slave role indicating inconsistent controller roles", recommendation="This situation can occurs transiently during role" + " changes. If, however, the condition persists or happens" + " frequently this indicates a role inconsistency. " + LogMessageDoc.CHECK_CONTROLLER ) void processOFPacketIn(OFPacketIn m) { // we don't expect packetIn while slave, reassert we are slave switchManagerCounters.packetInWhileSwitchIsSlave.increment(); log.warn("Received PacketIn from switch {} while" + "being slave. Reasserting slave role.", sw); reassertRole(OFControllerRole.ROLE_SLAVE); }
@Test public void testDhcp() { IOFMessageListener listener = getVirtualNetworkListener(); Ethernet dhcpPacket = PacketFactory.DhcpDiscoveryRequestEthernet(mac1); OFPacketIn dhcpPacketOf = PacketFactory.DhcpDiscoveryRequestOFPacketIn(sw1, mac1); cntx = new FloodlightContext(); IFloodlightProviderService.bcStore.put(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD, dhcpPacket); Command ret = listener.receive(sw1, dhcpPacketOf, cntx); assertTrue(ret == Command.CONTINUE); }
@Override public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext context) { switch (msg.getType()) { case PACKET_IN: return handlePacketIn(sw, (OFPacketIn) msg, context); default: break; } return Command.CONTINUE; }
public static FloodlightContext parseAndAnnotate(FloodlightContext bc, OFMessage m) { if (OFType.PACKET_IN.equals(m.getType())) { OFPacketIn pi = (OFPacketIn)m; Ethernet eth = new Ethernet(); eth.deserialize(pi.getData(), 0, pi.getData().length); IFloodlightProviderService.bcStore.put(bc, IFloodlightProviderService.CONTEXT_PI_PAYLOAD, eth); } return bc; }
@Override void processOFPacketIn(OFChannelHandler h, OFPacketIn m) { // OFPacketOut out = // h.sw.factory().buildPacketOut() // .setXid(m.getXid()) // .setBufferId(m.getBufferId()).build(); // h.sw.sendMsg(out); h.dispatchMessage(m); }
@Test public void handlePacket() { OFPacketIn pkt = sw.factory().buildPacketIn() .setBufferId(OFBufferId.NO_BUFFER) .setInPort(OFPort.NO_MASK) .setReason(OFPacketInReason.INVALID_TTL) .build(); controller.processPacket(null, pkt); assertNotNull("message unprocessed", registry.ctx); }
@Override public void processPacket(Dpid dpid, OFMessage msg) { OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext. packetContextFromPacketIn(sw, (OFPacketIn) msg); pktListener.handlePacket(pktCtx); }
@Override public void packetInProcess(Dpid dpid, OFPacketIn packetIn, OpenFlowPacketContext pktCtx) { //exclude abnormal packet -- Jinwoo Kim //only store ipv4 packet to DB try { short etherType = convertPacketContextToInboundPacket(pktCtx).parsed().getEtherType(); if (etherType != 2048) { return; } } catch (Exception e) { log.error(e.toString()); } InboundPacket ip = convertPacketContextToInboundPacket(pktCtx); PacketInFeature pif = new PacketInFeature(); UnitPacketInInformation ufii; Date date = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()); if (packetIn.getVersion() == OFVersion.OF_13) { FeatureIndex pfi = matchToFeatureIndex(packetIn.getMatch()); pfi = extractElementsFromInboundPkt(pfi, ip); ufii = new UnitPacketInInformation(packetIn.getTotalLen(), packetIn.getReason().ordinal(), pfi); // in case of OF_10, just store dummy match value // Jinwoo kim } else { ufii = new UnitPacketInInformation(); // return; } ufii.setDate(date); FeatureIndex fi = new FeatureIndex(); fi.setSwitchDatapathId(dpid.value()); fi.setSwitchPortId(toIntExact(ip.receivedFrom().port().toLong())); pif.addFeatureData(fi, ufii); providerService.packetInFeatureHandler(pif); }
@Override public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { switch (msg.getType()) { case PACKET_IN: cntIncoming.increment(); return this.processPacketInMessage(sw, (OFPacketIn) msg, cntx); default: break; } return Command.CONTINUE; }
protected void doDropFlow(IOFSwitch sw, OFPacketIn pi, IRoutingDecision decision, FloodlightContext cntx) { OFPort inPort = (pi.getVersion().compareTo(OFVersion.OF_12) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)); Match m = createMatchFromPacket(sw, inPort, cntx); OFFlowMod.Builder fmb = sw.getOFFactory().buildFlowAdd(); // this will be a drop-flow; a flow that will not output to any ports List<OFAction> actions = new ArrayList<OFAction>(); // set no action to drop U64 cookie = AppCookie.makeCookie(FORWARDING_APP_ID, 0); log.info("Droppingggg"); fmb.setCookie(cookie) .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) .setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setMatch(m) .setPriority(FLOWMOD_DEFAULT_PRIORITY); FlowModUtils.setActions(fmb, actions, sw); try { if (log.isDebugEnabled()) { log.debug("write drop flow-mod sw={} match={} flow-mod={}", new Object[] { sw, m, fmb.build() }); } boolean dampened = messageDamper.write(sw, fmb.build()); log.debug("OFMessage dampened: {}", dampened); } catch (IOException e) { log.error("Failure writing drop flow mod", e); } }
/** * Creates a OFPacketOut with the OFPacketIn data that is flooded on all ports unless * the port is blocked, in which case the packet will be dropped. * @param sw The switch that receives the OFPacketIn * @param pi The OFPacketIn that came to the switch * @param cntx The FloodlightContext associated with this OFPacketIn */ protected void doFlood(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx) { OFPort inPort = (pi.getVersion().compareTo(OFVersion.OF_12) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)); // Set Action to flood OFPacketOut.Builder pob = sw.getOFFactory().buildPacketOut(); List<OFAction> actions = new ArrayList<OFAction>(); Set<OFPort> broadcastPorts = this.topologyService.getSwitchBroadcastPorts(sw.getId()); if (broadcastPorts == null) { log.debug("BroadcastPorts returned null. Assuming single switch w/no links."); /* Must be a single-switch w/no links */ broadcastPorts = Collections.singleton(OFPort.FLOOD); } for (OFPort p : broadcastPorts) { if (p.equals(inPort)) continue; actions.add(sw.getOFFactory().actions().output(p, Integer.MAX_VALUE)); } pob.setActions(actions); // log.info("actions {}",actions); // set buffer-id, in-port and packet-data based on packet-in pob.setBufferId(OFBufferId.NO_BUFFER); pob.setInPort(inPort); pob.setData(pi.getData()); try { if (log.isTraceEnabled()) { log.trace("Writing flood PacketOut switch={} packet-in={} packet-out={}", new Object[] {sw, pi, pob.build()}); } messageDamper.write(sw, pob.build()); } catch (IOException e) { log.error("Failure writing PacketOut switch={} packet-in={} packet-out={}", new Object[] {sw, pi, pob.build()}, e); } return; }
private OFMessage createHubFlowMod(IOFSwitch sw, OFMessage msg) { OFPacketIn pi = (OFPacketIn) msg; OFFlowAdd.Builder fmb = sw.getOFFactory().buildFlowAdd(); fmb.setBufferId(pi.getBufferId()) .setXid(pi.getXid()); // set actions OFActionOutput.Builder actionBuilder = sw.getOFFactory().actions().buildOutput(); actionBuilder.setPort(OFPort.FLOOD); fmb.setActions(Collections.singletonList((OFAction) actionBuilder.build())); return fmb.build(); }
@Override public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { switch (msg.getType()) { case PACKET_IN: return this.processPacketInMessage(sw, (OFPacketIn) msg, cntx); case FLOW_REMOVED: return this.processFlowRemovedMessage(sw, (OFFlowRemoved) msg); case ERROR: log.info("received an error {} from switch {}", msg, sw); return Command.CONTINUE; default: log.error("received an unexpected message {} from switch {}", msg, sw); return Command.CONTINUE; } }
/** * Processes an OFPacketIn message and decides if the OFPacketIn should be dropped * or the processing should continue. * @param sw The switch the PacketIn came from. * @param msg The OFPacketIn message from the switch. * @param cntx The FloodlightContext for this message. * @return Command.CONTINUE if processing should be continued, Command.STOP otherwise. */ protected Command processPacketIn(IOFSwitch sw, OFPacketIn msg, FloodlightContext cntx) { Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); Command ret = Command.STOP; String srcNetwork = macToGuid.get(eth.getSourceMACAddress()); // If the host is on an unknown network we deny it. // We make exceptions for ARP and DHCP. if (eth.isBroadcast() || eth.isMulticast() || isDefaultGateway(eth) || isDhcpPacket(eth)) { ret = Command.CONTINUE; } else if (srcNetwork == null) { log.trace("Blocking traffic from host {} because it is not attached to any network.", eth.getSourceMACAddress().toString()); ret = Command.STOP; } else if (oneSameNetwork(eth.getSourceMACAddress(), eth.getDestinationMACAddress())) { // if they are on the same network continue ret = Command.CONTINUE; } if (log.isTraceEnabled()) log.trace("Results for flow between {} and {} is {}", new Object[] {eth.getSourceMACAddress(), eth.getDestinationMACAddress(), ret}); /* * TODO - figure out how to still detect gateways while using * drop mods if (ret == Command.STOP) { if (!(eth.getPayload() instanceof ARP)) doDropFlow(sw, msg, cntx); } */ return ret; }
/** * Writes a FlowMod to a switch that inserts a drop flow. * @param sw The switch to write the FlowMod to. * @param pi The corresponding OFPacketIn. Used to create the OFMatch structure. * @param cntx The FloodlightContext that gets passed to the switch. */ protected void doDropFlow(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx) { if (log.isTraceEnabled()) { log.trace("doDropFlow pi={} srcSwitch={}", new Object[] { pi, sw }); } if (sw == null) { log.warn("Switch is null, not installing drop flowmod for PacketIn {}", pi); return; } // Create flow-mod based on packet-in and src-switch OFFlowMod.Builder fmb = sw.getOFFactory().buildFlowModify(); List<OFAction> actions = new ArrayList<OFAction>(); // no actions = drop U64 cookie = AppCookie.makeCookie(APP_ID, 0); fmb.setCookie(cookie) .setIdleTimeout(ForwardingBase.FLOWMOD_DEFAULT_IDLE_TIMEOUT) .setHardTimeout(ForwardingBase.FLOWMOD_DEFAULT_HARD_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setMatch(pi.getMatch()) .setActions(actions); if (log.isTraceEnabled()) { log.trace("write drop flow-mod srcSwitch={} match={} " + "pi={} flow-mod={}", new Object[] {sw, pi.getMatch(), pi, fmb.build()}); } sw.write(fmb.build()); return; }
@Override public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { switch (msg.getType()) { case PACKET_IN: ctrIncoming.increment(); return this.handlePacketIn(sw.getId(), (OFPacketIn) msg, cntx); default: break; } return Command.CONTINUE; }
@Override public net.floodlightcontroller.core.IListener.Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { switch (msg.getType()) { case PACKET_IN: return processPacketIn(sw, (OFPacketIn)msg, cntx); default: break; } log.warn("Received unexpected message {}", msg); return Command.CONTINUE; }
/** * used to send proxy Arp for load balanced service requests * @param IOFSwitch sw * @param OFPacketIn pi * @param FloodlightContext cntx * @param String vipId */ protected void vipProxyArpReply(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx, String vipId) { log.debug("vipProxyArpReply"); Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); // retrieve original arp to determine host configured gw IP address if (! (eth.getPayload() instanceof ARP)) return; ARP arpRequest = (ARP) eth.getPayload(); // have to do proxy arp reply since at this point we cannot determine the requesting application type // generate proxy ARP reply IPacket arpReply = new Ethernet() .setSourceMACAddress(vips.get(vipId).proxyMac) .setDestinationMACAddress(eth.getSourceMACAddress()) .setEtherType(EthType.ARP) .setVlanID(eth.getVlanID()) .setPriorityCode(eth.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(vips.get(vipId).proxyMac) .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) .setTargetHardwareAddress(eth.getSourceMACAddress()) .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress())); // push ARP reply out pushPacket(arpReply, sw, OFBufferId.NO_BUFFER, OFPort.ANY, (pi.getVersion().compareTo(OFVersion.OF_12) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)), cntx, true); log.debug("proxy ARP reply pushed as {}", IPv4.fromIPv4Address(vips.get(vipId).address)); return; }
/** * Writes an OFPacketOut message to a switch. * * @param sw * The switch to write the PacketOut to. * @param packetInMessage * The corresponding PacketIn. * @param egressPort * The switchport to output the PacketOut. */ public static void writePacketOutForPacketIn(IOFSwitch sw, OFPacketIn packetInMessage, OFPort egressPort) { OFPacketOut.Builder pob = sw.getOFFactory().buildPacketOut(); // Set buffer_id, in_port, actions_len pob.setBufferId(packetInMessage.getBufferId()); pob.setInPort(packetInMessage.getVersion().compareTo(OFVersion.OF_12) < 0 ? packetInMessage .getInPort() : packetInMessage.getMatch().get( MatchField.IN_PORT)); // set actions List<OFAction> actions = new ArrayList<OFAction>(1); actions.add(sw.getOFFactory().actions().buildOutput() .setPort(egressPort).setMaxLen(0xffFFffFF).build()); pob.setActions(actions); // set data - only if buffer_id == -1 if (packetInMessage.getBufferId() == OFBufferId.NO_BUFFER) { byte[] packetData = packetInMessage.getData(); pob.setData(packetData); } // and write it out sw.write(pob.build()); }