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