alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 1 | package org.onlab.onos.of.controller; |
| 2 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 5 | import java.util.Collections; |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 6 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 7 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 8 | import org.onlab.packet.Ethernet; |
| 9 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 10 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 11 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 12 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 13 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 14 | import org.projectfloodlight.openflow.types.OFBufferId; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 15 | import org.projectfloodlight.openflow.types.OFPort; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 16 | import org.slf4j.Logger; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 17 | |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 18 | public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 19 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 20 | private final Logger log = getLogger(getClass()); |
| 21 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 22 | private final AtomicBoolean free = new AtomicBoolean(true); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 23 | private boolean isBuilt = false; |
| 24 | private final OpenFlowSwitch sw; |
| 25 | private final OFPacketIn pktin; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 26 | private OFPacketOut pktout = null; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 27 | |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 28 | private DefaultOpenFlowPacketContext(OpenFlowSwitch s, OFPacketIn pkt) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 29 | this.sw = s; |
| 30 | this.pktin = pkt; |
| 31 | } |
| 32 | |
| 33 | @Override |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 34 | public void send() { |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 35 | if (blocked() && isBuilt) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 36 | sw.sendMsg(pktout); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 37 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Override |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 41 | public synchronized void build(OFPort outPort) { |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 42 | if (isBuilt) { |
| 43 | return; |
| 44 | } |
| 45 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 46 | OFAction act = buildOutput(outPort.getPortNumber()); |
| 47 | pktout = builder.setXid(pktin.getXid()) |
alshabib | 632a78c | 2014-09-15 10:06:05 -0700 | [diff] [blame^] | 48 | .setInPort(pktin.getInPort()) |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 49 | .setBufferId(pktin.getBufferId()) |
| 50 | .setActions(Collections.singletonList(act)) |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 51 | .build(); |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 52 | isBuilt = true; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public synchronized void build(Ethernet ethFrame, OFPort outPort) { |
| 57 | if (isBuilt) { |
| 58 | return; |
| 59 | } |
| 60 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 61 | OFAction act = buildOutput(outPort.getPortNumber()); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 62 | pktout = builder.setXid(pktin.getXid()) |
| 63 | .setBufferId(OFBufferId.NO_BUFFER) |
| 64 | .setActions(Collections.singletonList(act)) |
| 65 | .setData(ethFrame.serialize()) |
| 66 | .build(); |
| 67 | isBuilt = true; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Ethernet parsed() { |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 72 | Ethernet eth = new Ethernet(); |
| 73 | eth.deserialize(pktin.getData(), 0, pktin.getTotalLen()); |
| 74 | return eth; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Dpid dpid() { |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 79 | return new Dpid(sw.getId()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 80 | } |
| 81 | |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 82 | public static OpenFlowPacketContext packetContextFromPacketIn(OpenFlowSwitch s, OFPacketIn pkt) { |
| 83 | return new DefaultOpenFlowPacketContext(s, pkt); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 84 | } |
| 85 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public Integer inPort() { |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 88 | try { |
| 89 | return pktin.getInPort().getPortNumber(); |
| 90 | } catch (UnsupportedOperationException e) { |
| 91 | return pktin.getMatch().get(MatchField.IN_PORT).getPortNumber(); |
| 92 | } |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 93 | } |
| 94 | |
alshabib | 9ee6817 | 2014-09-09 14:45:14 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public byte[] unparsed() { |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 97 | |
alshabib | 9ee6817 | 2014-09-09 14:45:14 -0700 | [diff] [blame] | 98 | return pktin.getData().clone(); |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 99 | |
alshabib | 9ee6817 | 2014-09-09 14:45:14 -0700 | [diff] [blame] | 100 | } |
| 101 | |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 102 | private OFActionOutput buildOutput(Integer port) { |
| 103 | OFActionOutput act = sw.factory().actions() |
| 104 | .buildOutput() |
| 105 | .setPort(OFPort.of(port)) |
| 106 | .build(); |
| 107 | return act; |
| 108 | } |
| 109 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public boolean blocked() { |
| 112 | return free.getAndSet(false); |
| 113 | } |
| 114 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 115 | } |