Committed by
Gerrit Code Review
Expose cookie information in packet context
Change-Id: I7f2cb331a19aeca1a578aade6488a6480d15496c
Showing
6 changed files
with
71 additions
and
4 deletions
... | @@ -20,6 +20,7 @@ import org.onlab.packet.Ethernet; | ... | @@ -20,6 +20,7 @@ import org.onlab.packet.Ethernet; |
20 | 20 | ||
21 | import java.nio.ByteBuffer; | 21 | import java.nio.ByteBuffer; |
22 | import java.util.Objects; | 22 | import java.util.Objects; |
23 | +import java.util.Optional; | ||
23 | 24 | ||
24 | import static com.google.common.base.MoreObjects.toStringHelper; | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
25 | 26 | ||
... | @@ -31,6 +32,7 @@ public final class DefaultInboundPacket implements InboundPacket { | ... | @@ -31,6 +32,7 @@ public final class DefaultInboundPacket implements InboundPacket { |
31 | private final ConnectPoint receivedFrom; | 32 | private final ConnectPoint receivedFrom; |
32 | private final Ethernet parsed; | 33 | private final Ethernet parsed; |
33 | private final ByteBuffer unparsed; | 34 | private final ByteBuffer unparsed; |
35 | + private final Optional<Long> cookie; | ||
34 | 36 | ||
35 | /** | 37 | /** |
36 | * Creates an immutable inbound packet. | 38 | * Creates an immutable inbound packet. |
... | @@ -41,9 +43,23 @@ public final class DefaultInboundPacket implements InboundPacket { | ... | @@ -41,9 +43,23 @@ public final class DefaultInboundPacket implements InboundPacket { |
41 | */ | 43 | */ |
42 | public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed, | 44 | public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed, |
43 | ByteBuffer unparsed) { | 45 | ByteBuffer unparsed) { |
46 | + this(receivedFrom, parsed, unparsed, Optional.empty()); | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * Creates an immutable inbound packet with cookie. | ||
51 | + * | ||
52 | + * @param receivedFrom connection point where received | ||
53 | + * @param parsed parsed ethernet frame | ||
54 | + * @param unparsed unparsed raw bytes | ||
55 | + * @param cookie cookie | ||
56 | + */ | ||
57 | + public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed, | ||
58 | + ByteBuffer unparsed, Optional<Long> cookie) { | ||
44 | this.receivedFrom = receivedFrom; | 59 | this.receivedFrom = receivedFrom; |
45 | this.parsed = parsed; | 60 | this.parsed = parsed; |
46 | this.unparsed = unparsed; | 61 | this.unparsed = unparsed; |
62 | + this.cookie = cookie; | ||
47 | } | 63 | } |
48 | 64 | ||
49 | @Override | 65 | @Override |
... | @@ -63,6 +79,11 @@ public final class DefaultInboundPacket implements InboundPacket { | ... | @@ -63,6 +79,11 @@ public final class DefaultInboundPacket implements InboundPacket { |
63 | } | 79 | } |
64 | 80 | ||
65 | @Override | 81 | @Override |
82 | + public Optional<Long> cookie() { | ||
83 | + return cookie; | ||
84 | + } | ||
85 | + | ||
86 | + @Override | ||
66 | public int hashCode() { | 87 | public int hashCode() { |
67 | return Objects.hash(receivedFrom, parsed, unparsed); | 88 | return Objects.hash(receivedFrom, parsed, unparsed); |
68 | } | 89 | } | ... | ... |
... | @@ -19,6 +19,7 @@ import org.onosproject.net.ConnectPoint; | ... | @@ -19,6 +19,7 @@ import org.onosproject.net.ConnectPoint; |
19 | import org.onlab.packet.Ethernet; | 19 | import org.onlab.packet.Ethernet; |
20 | 20 | ||
21 | import java.nio.ByteBuffer; | 21 | import java.nio.ByteBuffer; |
22 | +import java.util.Optional; | ||
22 | 23 | ||
23 | /** | 24 | /** |
24 | * Represents a data packet intercepted from an infrastructure device. | 25 | * Represents a data packet intercepted from an infrastructure device. |
... | @@ -47,4 +48,10 @@ public interface InboundPacket { | ... | @@ -47,4 +48,10 @@ public interface InboundPacket { |
47 | */ | 48 | */ |
48 | ByteBuffer unparsed(); | 49 | ByteBuffer unparsed(); |
49 | 50 | ||
51 | + /** | ||
52 | + * Returns the cookie in the packet in message. | ||
53 | + * | ||
54 | + * @return optional flow cookie | ||
55 | + */ | ||
56 | + Optional<Long> cookie(); | ||
50 | } | 57 | } | ... | ... |
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | package org.onosproject.net.packet; | 16 | package org.onosproject.net.packet; |
17 | 17 | ||
18 | import java.nio.ByteBuffer; | 18 | import java.nio.ByteBuffer; |
19 | +import java.util.Optional; | ||
19 | 20 | ||
20 | import org.junit.Test; | 21 | import org.junit.Test; |
21 | import org.onlab.packet.Ethernet; | 22 | import org.onlab.packet.Ethernet; |
... | @@ -41,15 +42,22 @@ public class DefaultInboundPacketTest { | ... | @@ -41,15 +42,22 @@ public class DefaultInboundPacketTest { |
41 | final DefaultInboundPacket packet1 = | 42 | final DefaultInboundPacket packet1 = |
42 | new DefaultInboundPacket(connectPoint("d1", 1), | 43 | new DefaultInboundPacket(connectPoint("d1", 1), |
43 | eth, | 44 | eth, |
44 | - byteBuffer); | 45 | + byteBuffer, |
46 | + Optional.of(1L)); | ||
45 | final DefaultInboundPacket sameAsPacket1 = | 47 | final DefaultInboundPacket sameAsPacket1 = |
46 | new DefaultInboundPacket(connectPoint("d1", 1), | 48 | new DefaultInboundPacket(connectPoint("d1", 1), |
47 | eth, | 49 | eth, |
48 | - byteBuffer); | 50 | + byteBuffer, |
51 | + Optional.of(1L)); | ||
49 | final DefaultInboundPacket packet2 = | 52 | final DefaultInboundPacket packet2 = |
50 | new DefaultInboundPacket(connectPoint("d2", 1), | 53 | new DefaultInboundPacket(connectPoint("d2", 1), |
51 | eth, | 54 | eth, |
52 | byteBuffer); | 55 | byteBuffer); |
56 | + final DefaultInboundPacket sameAsPacket2 = | ||
57 | + new DefaultInboundPacket(connectPoint("d2", 1), | ||
58 | + eth, | ||
59 | + byteBuffer, | ||
60 | + Optional.empty()); | ||
53 | /** | 61 | /** |
54 | * Checks that the DefaultInboundPacket class is immutable. | 62 | * Checks that the DefaultInboundPacket class is immutable. |
55 | */ | 63 | */ |
... | @@ -65,7 +73,7 @@ public class DefaultInboundPacketTest { | ... | @@ -65,7 +73,7 @@ public class DefaultInboundPacketTest { |
65 | public void testEquals() { | 73 | public void testEquals() { |
66 | new EqualsTester() | 74 | new EqualsTester() |
67 | .addEqualityGroup(packet1, sameAsPacket1) | 75 | .addEqualityGroup(packet1, sameAsPacket1) |
68 | - .addEqualityGroup(packet2) | 76 | + .addEqualityGroup(packet2, sameAsPacket2) |
69 | .testEquals(); | 77 | .testEquals(); |
70 | } | 78 | } |
71 | 79 | ||
... | @@ -77,5 +85,6 @@ public class DefaultInboundPacketTest { | ... | @@ -77,5 +85,6 @@ public class DefaultInboundPacketTest { |
77 | assertThat(packet1.receivedFrom(), equalTo(connectPoint("d1", 1))); | 85 | assertThat(packet1.receivedFrom(), equalTo(connectPoint("d1", 1))); |
78 | assertThat(packet1.parsed(), equalTo(eth)); | 86 | assertThat(packet1.parsed(), equalTo(eth)); |
79 | assertThat(packet1.unparsed(), notNullValue()); | 87 | assertThat(packet1.unparsed(), notNullValue()); |
88 | + assertThat(packet1.cookie(), equalTo(Optional.of(1L))); | ||
80 | } | 89 | } |
81 | } | 90 | } | ... | ... |
... | @@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory; | ... | @@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory; |
30 | 30 | ||
31 | import java.nio.BufferUnderflowException; | 31 | import java.nio.BufferUnderflowException; |
32 | import java.util.Collections; | 32 | import java.util.Collections; |
33 | +import java.util.Optional; | ||
33 | import java.util.concurrent.atomic.AtomicBoolean; | 34 | import java.util.concurrent.atomic.AtomicBoolean; |
34 | 35 | ||
35 | import static org.onosproject.security.AppGuard.checkPermission; | 36 | import static org.onosproject.security.AppGuard.checkPermission; |
... | @@ -179,4 +180,14 @@ public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext | ... | @@ -179,4 +180,14 @@ public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext |
179 | return isBuffered; | 180 | return isBuffered; |
180 | } | 181 | } |
181 | 182 | ||
183 | + @Override | ||
184 | + public Optional<Long> cookie() { | ||
185 | + checkPermission(PACKET_READ); | ||
186 | + if (pktin.getVersion() != OFVersion.OF_10) { | ||
187 | + return Optional.of(pktin.getCookie().getValue()); | ||
188 | + } else { | ||
189 | + return Optional.empty(); | ||
190 | + } | ||
191 | + } | ||
192 | + | ||
182 | } | 193 | } | ... | ... |
... | @@ -18,6 +18,8 @@ package org.onosproject.openflow.controller; | ... | @@ -18,6 +18,8 @@ package org.onosproject.openflow.controller; |
18 | import org.onlab.packet.Ethernet; | 18 | import org.onlab.packet.Ethernet; |
19 | import org.projectfloodlight.openflow.types.OFPort; | 19 | import org.projectfloodlight.openflow.types.OFPort; |
20 | 20 | ||
21 | +import java.util.Optional; | ||
22 | + | ||
21 | /** | 23 | /** |
22 | * A representation of a packet context which allows any provider | 24 | * A representation of a packet context which allows any provider |
23 | * to view a packet in event, but may block the response to the | 25 | * to view a packet in event, but may block the response to the |
... | @@ -29,12 +31,14 @@ public interface OpenFlowPacketContext { | ... | @@ -29,12 +31,14 @@ public interface OpenFlowPacketContext { |
29 | /** | 31 | /** |
30 | * Blocks further responses (ie. send() calls) on this | 32 | * Blocks further responses (ie. send() calls) on this |
31 | * packet in event. | 33 | * packet in event. |
34 | + * | ||
32 | * @return true if blocks | 35 | * @return true if blocks |
33 | */ | 36 | */ |
34 | boolean block(); | 37 | boolean block(); |
35 | 38 | ||
36 | /** | 39 | /** |
37 | * Checks whether the packet has been handled. | 40 | * Checks whether the packet has been handled. |
41 | + * | ||
38 | * @return true if handled, false otherwise. | 42 | * @return true if handled, false otherwise. |
39 | */ | 43 | */ |
40 | boolean isHandled(); | 44 | boolean isHandled(); |
... | @@ -47,12 +51,14 @@ public interface OpenFlowPacketContext { | ... | @@ -47,12 +51,14 @@ public interface OpenFlowPacketContext { |
47 | 51 | ||
48 | /** | 52 | /** |
49 | * Build the packet out in response to this packet in event. | 53 | * Build the packet out in response to this packet in event. |
54 | + * | ||
50 | * @param outPort the out port to send to packet out of. | 55 | * @param outPort the out port to send to packet out of. |
51 | */ | 56 | */ |
52 | void build(OFPort outPort); | 57 | void build(OFPort outPort); |
53 | 58 | ||
54 | /** | 59 | /** |
55 | * Build the packet out in response to this packet in event. | 60 | * Build the packet out in response to this packet in event. |
61 | + * | ||
56 | * @param ethFrame the actual packet to send out. | 62 | * @param ethFrame the actual packet to send out. |
57 | * @param outPort the out port to send to packet out of. | 63 | * @param outPort the out port to send to packet out of. |
58 | */ | 64 | */ |
... | @@ -60,31 +66,43 @@ public interface OpenFlowPacketContext { | ... | @@ -60,31 +66,43 @@ public interface OpenFlowPacketContext { |
60 | 66 | ||
61 | /** | 67 | /** |
62 | * Provided a handle onto the parsed payload. | 68 | * Provided a handle onto the parsed payload. |
69 | + * | ||
63 | * @return the parsed form of the payload. | 70 | * @return the parsed form of the payload. |
64 | */ | 71 | */ |
65 | Ethernet parsed(); | 72 | Ethernet parsed(); |
66 | 73 | ||
67 | /** | 74 | /** |
68 | * Provide an unparsed copy of the data. | 75 | * Provide an unparsed copy of the data. |
76 | + * | ||
69 | * @return the unparsed form of the payload. | 77 | * @return the unparsed form of the payload. |
70 | */ | 78 | */ |
71 | byte[] unparsed(); | 79 | byte[] unparsed(); |
72 | 80 | ||
73 | /** | 81 | /** |
74 | * Provide the dpid of the switch where the packet in arrived. | 82 | * Provide the dpid of the switch where the packet in arrived. |
83 | + * | ||
75 | * @return the dpid of the switch. | 84 | * @return the dpid of the switch. |
76 | */ | 85 | */ |
77 | Dpid dpid(); | 86 | Dpid dpid(); |
78 | 87 | ||
79 | /** | 88 | /** |
80 | * Provide the port on which the packet arrived. | 89 | * Provide the port on which the packet arrived. |
90 | + * | ||
81 | * @return the port | 91 | * @return the port |
82 | */ | 92 | */ |
83 | Integer inPort(); | 93 | Integer inPort(); |
84 | 94 | ||
85 | /** | 95 | /** |
86 | * Indicates that this packet is buffered at the switch. | 96 | * Indicates that this packet is buffered at the switch. |
97 | + * | ||
87 | * @return buffer indication | 98 | * @return buffer indication |
88 | */ | 99 | */ |
89 | boolean isBuffered(); | 100 | boolean isBuffered(); |
101 | + | ||
102 | + /** | ||
103 | + * Provide the cookie in the packet in message. | ||
104 | + * | ||
105 | + * @return optional flow cookie | ||
106 | + */ | ||
107 | + Optional<Long> cookie(); | ||
90 | } | 108 | } | ... | ... |
... | @@ -156,7 +156,8 @@ public class OpenFlowPacketProvider extends AbstractProvider implements PacketPr | ... | @@ -156,7 +156,8 @@ public class OpenFlowPacketProvider extends AbstractProvider implements PacketPr |
156 | 156 | ||
157 | DefaultInboundPacket inPkt = new DefaultInboundPacket( | 157 | DefaultInboundPacket inPkt = new DefaultInboundPacket( |
158 | new ConnectPoint(id, PortNumber.portNumber(pktCtx.inPort())), | 158 | new ConnectPoint(id, PortNumber.portNumber(pktCtx.inPort())), |
159 | - pktCtx.parsed(), ByteBuffer.wrap(pktCtx.unparsed())); | 159 | + pktCtx.parsed(), ByteBuffer.wrap(pktCtx.unparsed()), |
160 | + pktCtx.cookie()); | ||
160 | 161 | ||
161 | DefaultOutboundPacket outPkt = null; | 162 | DefaultOutboundPacket outPkt = null; |
162 | if (!pktCtx.isBuffered()) { | 163 | if (!pktCtx.isBuffered()) { | ... | ... |
-
Please register or login to post a comment