Pavlin Radoslavov

Added new methods IPv4.getDscp() / IPv4.setDscp()

and IPv4.getEcn() / IPv4.setEcn().

Those can be used to get/set the DSCP and ECN bits in the IPv4 header
without explicit bit manipulation at the caller.

Change-Id: Ia7c5779abae5c4fc7a343e3f7ef3355eb7e86e3d
...@@ -505,10 +505,9 @@ public class ReactiveForwarding { ...@@ -505,10 +505,9 @@ public class ReactiveForwarding {
505 .matchIPProtocol(ipv4Protocol); 505 .matchIPProtocol(ipv4Protocol);
506 506
507 if (matchIpv4Dscp) { 507 if (matchIpv4Dscp) {
508 - int dscp = ipv4Packet.getDiffServ() >>> 2; 508 + byte dscp = ipv4Packet.getDscp();
509 - int ecn = ipv4Packet.getDiffServ() % 4; 509 + byte ecn = ipv4Packet.getEcn();
510 - builder.matchIPDscp((byte) (dscp)) 510 + builder.matchIPDscp(dscp).matchIPEcn(ecn);
511 - .matchIPEcn((byte) (ecn));
512 } 511 }
513 512
514 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) { 513 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
......
...@@ -41,6 +41,10 @@ public class IPv4 extends BasePacket { ...@@ -41,6 +41,10 @@ public class IPv4 extends BasePacket {
41 IPv4.PROTOCOL_CLASS_MAP.put(IPv4.PROTOCOL_UDP, UDP.class); 41 IPv4.PROTOCOL_CLASS_MAP.put(IPv4.PROTOCOL_UDP, UDP.class);
42 } 42 }
43 43
44 + private static final byte DSCP_MASK = 0x3f;
45 + private static final byte DSCP_OFFSET = 2;
46 + private static final byte ECN_MASK = 0x3;
47 +
44 protected byte version; 48 protected byte version;
45 protected byte headerLength; 49 protected byte headerLength;
46 protected byte diffServ; 50 protected byte diffServ;
...@@ -91,15 +95,61 @@ public class IPv4 extends BasePacket { ...@@ -91,15 +95,61 @@ public class IPv4 extends BasePacket {
91 } 95 }
92 96
93 /** 97 /**
94 - * @return the diffServ 98 + * Gets the DSCP value (6 bits).
99 + *
100 + * @return the DSCP value (6 bits)
101 + */
102 + public byte getDscp() {
103 + return (byte) ((this.diffServ >>> DSCP_OFFSET) & DSCP_MASK);
104 + }
105 +
106 + /**
107 + * Sets the DSCP value (6 bits).
108 + *
109 + * @param dscp the DSCP value (6 bits)
110 + * @return this
111 + */
112 + public IPv4 setDscp(byte dscp) {
113 + this.diffServ &= ~(DSCP_MASK << DSCP_OFFSET);
114 + this.diffServ |= (dscp & DSCP_MASK) << DSCP_OFFSET;
115 + return this;
116 + }
117 +
118 + /**
119 + * Gets the ECN value (2 bits).
120 + *
121 + * @return the ECN value (2 bits)
122 + */
123 + public byte getEcn() {
124 + return (byte) (this.diffServ & ECN_MASK);
125 + }
126 +
127 + /**
128 + * Sets the ECN value (2 bits).
129 + *
130 + * @param ecn the ECN value (2 bits)
131 + * @return this
132 + */
133 + public IPv4 setEcn(byte ecn) {
134 + this.diffServ &= ~ECN_MASK;
135 + this.diffServ |= (ecn & ECN_MASK);
136 + return this;
137 + }
138 +
139 + /**
140 + * Gets the DiffServ octet (including the DSCP and ECN bits).
141 + *
142 + * @return the diffServ octet (including the DSCP and ECN bits)
95 */ 143 */
96 public byte getDiffServ() { 144 public byte getDiffServ() {
97 return this.diffServ; 145 return this.diffServ;
98 } 146 }
99 147
100 /** 148 /**
101 - * @param diffServ 149 + * Sets the DiffServ octet (including the DSCP and ECN bits).
102 - * the diffServ to set 150 + *
151 + * @param diffServ the diffServ octet to set (including the DSCP and ECN
152 + * bits)
103 * @return this 153 * @return this
104 */ 154 */
105 public IPv4 setDiffServ(final byte diffServ) { 155 public IPv4 setDiffServ(final byte diffServ) {
......