Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame^] | 3 | import java.util.ArrayList; |
| 4 | |
Andreas Wundsam | 9539f92 | 2013-10-24 13:07:30 -0700 | [diff] [blame] | 5 | import javax.annotation.concurrent.Immutable; |
| 6 | |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 7 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 8 | /** User-facing object representing a bitmap of ports that can be matched on. |
| 9 | * This is implemented by the custom BSN OXM type of_oxm_bsn_in_ports_182. |
| 10 | * |
| 11 | * You can call set() on the builder for all the Ports you want to match on |
| 12 | * and unset to exclude the port. |
| 13 | * |
| 14 | * <b>Implementation note:</b> to comply with the matching semantics of OXM (which is a logical "AND" not "OR") |
| 15 | * the underlying match uses a data format which is very unintuitive. The value is always |
| 16 | * 0, and the mask has the bits set for the ports that should <b>NOT</b> be included in the |
| 17 | * range. |
| 18 | * |
| 19 | * For the curious: We transformed the bitmap (a logical OR) problem into a logical |
| 20 | * AND NOT problem. |
| 21 | * |
| 22 | * We logically mean: Inport is 1 OR 3 |
| 23 | * We technically say: Inport IS NOT 2 AND IS NOT 4 AND IS NOT 5 AND IS NOT .... |
| 24 | * The second term cannot be represented in OXM, the second can. |
| 25 | * |
| 26 | * That said, all that craziness is hidden from the user of this object. |
Andreas Wundsam | 9539f92 | 2013-10-24 13:07:30 -0700 | [diff] [blame] | 27 | * |
| 28 | * <h2>Usage</h2> |
| 29 | * OFPortBitmap is meant to be used with MatchField <tt>BSN_IN_PORTS_128</tt> in place |
| 30 | * of the raw type Masked<OFBitMask128>. |
| 31 | * |
| 32 | * <h3>Example:</h3>: |
| 33 | * <pre> |
| 34 | * OFPortBitMap portBitMap; |
| 35 | * Match.Builder matchBuilder; |
| 36 | * // initialize |
| 37 | * matchBuilder.setMasked(MatchField.BSN_IN_PORTS_128, portBitmap); |
| 38 | * </pre> |
| 39 | * |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 40 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 41 | */ |
Andreas Wundsam | 9539f92 | 2013-10-24 13:07:30 -0700 | [diff] [blame] | 42 | @Immutable |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 43 | public class OFPortBitMap extends Masked<OFBitMask128> { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 44 | |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 45 | private OFPortBitMap(OFBitMask128 mask) { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 46 | super(OFBitMask128.NONE, mask); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 49 | /** @return whether or not the given port is logically included in the |
| 50 | * match, i.e., whether a packet from in-port <emph>port</emph> be matched by |
| 51 | * this OXM. |
| 52 | */ |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 53 | public boolean isOn(OFPort port) { |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 54 | // see the implementation note above about the logical inversion of the mask |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 55 | return !(this.mask.isOn(port.getPortNumber())); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 58 | public static OFPortBitMap ofPorts(OFPort... ports) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 59 | Builder builder = new Builder(); |
| 60 | for (OFPort port: ports) { |
| 61 | builder.set(port); |
| 62 | } |
| 63 | return builder.build(); |
| 64 | } |
| 65 | |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame^] | 66 | public static OFPortBitMap of(OFBitMask128 mask) { |
| 67 | return new OFPortBitMap(mask); |
| 68 | } |
| 69 | |
| 70 | public Iterable<OFPort> getOnPorts() { |
| 71 | ArrayList<OFPort> ports = new ArrayList<>(); |
| 72 | for(int i=0; i < 127; i++) { |
| 73 | if(!(this.mask.isOn(i))) { |
| 74 | ports.add(OFPort.of(i)); |
| 75 | } |
| 76 | } |
| 77 | return ports; |
| 78 | } |
| 79 | |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 80 | @Override |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 81 | public boolean equals(Object obj) { |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 82 | if (!(obj instanceof OFPortBitMap)) |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 83 | return false; |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 84 | OFPortBitMap other = (OFPortBitMap)obj; |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 85 | return (other.value.equals(this.value) && other.mask.equals(this.mask)); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int hashCode() { |
| 90 | return 619 * mask.hashCode() + 257 * value.hashCode(); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public static class Builder { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 94 | private long raw1 = -1, raw2 = -1; |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 95 | |
| 96 | public Builder() { |
| 97 | |
| 98 | } |
| 99 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 100 | /** @return whether or not the given port is logically included in the |
| 101 | * match, i.e., whether a packet from in-port <emph>port</emph> be matched by |
| 102 | * this OXM. |
| 103 | */ |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 104 | public boolean isOn(OFPort port) { |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 105 | // see the implementation note above about the logical inversion of the mask |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 106 | return !(OFBitMask128.isBitOn(raw1, raw2, port.getPortNumber())); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 109 | /** remove this port from the match, i.e., packets from this in-port |
| 110 | * will NOT be matched. |
| 111 | */ |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 112 | public Builder unset(OFPort port) { |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 113 | // see the implementation note above about the logical inversion of the mask |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 114 | int bit = port.getPortNumber(); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 115 | if (bit < 0 || bit > 127) |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 116 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 117 | else if (bit == 127) |
| 118 | // the highest order bit in the bitmask is reserved. The switch will |
| 119 | // set that bit for all ports >= 127. The reason is that we don't want |
| 120 | // the OFPortMap to match all ports out of its range (i.e., a packet |
| 121 | // coming in on port 181 would match *any* OFPortMap). |
| 122 | throw new IndexOutOfBoundsException("The highest order bit in the bitmask is reserved."); |
| 123 | else if (bit < 64) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 124 | raw2 |= ((long)1 << bit); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 125 | } else { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 126 | raw1 |= ((long)1 << (bit - 64)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 127 | } |
| 128 | return this; |
| 129 | } |
| 130 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 131 | /** add this port from the match, i.e., packets from this in-port |
| 132 | * will NOT be matched. |
| 133 | */ |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 134 | public Builder set(OFPort port) { |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 135 | // see the implementation note above about the logical inversion of the mask |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 136 | int bit = port.getPortNumber(); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 137 | if (bit < 0 || bit > 127) |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 138 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 139 | else if (bit == 127) |
| 140 | // the highest order bit in the bitmask is reserved. The switch will |
| 141 | // set that bit for all ports >= 127. The reason is that we don't want |
| 142 | // the OFPortMap to match all ports out of its range (i.e., a packet |
| 143 | // coming in on port 181 would match *any* OFPortMap). |
| 144 | throw new IndexOutOfBoundsException("The highest order bit in the bitmask is reserved."); |
| 145 | else if (bit < 64) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 146 | raw2 &= ~((long)1 << bit); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 147 | } else { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 148 | raw1 &= ~((long)1 << (bit - 64)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 149 | } |
| 150 | return this; |
| 151 | } |
| 152 | |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 153 | public OFPortBitMap build() { |
| 154 | return new OFPortBitMap(OFBitMask128.of(raw1, raw2)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | } |