Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 3 | import java.util.Arrays; |
| 4 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 5 | import javax.annotation.Nonnull; |
| 6 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | import org.jboss.netty.buffer.ChannelBuffer; |
| 8 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 9 | import org.projectfloodlight.openflow.util.HexString; |
| 10 | |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 11 | import com.google.common.base.Preconditions; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 12 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 13 | import com.google.common.primitives.Longs; |
| 14 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 15 | /** |
| 16 | * Wrapper around a 6 byte mac address. |
| 17 | * |
kjwon15 | 7bc8540 | 2015-02-12 15:07:42 +0900 | [diff] [blame] | 18 | * @author Andreas Wundsam {@literal <}andreas.wundsam@bigswitch.com{@literal >} |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | public class MacAddress implements OFValueType<MacAddress> { |
| 22 | static final int MacAddrLen = 6; |
| 23 | private final long rawValue; |
| 24 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 25 | private final static long NONE_VAL = 0x0L; |
| 26 | public static final MacAddress NONE = new MacAddress(NONE_VAL); |
| 27 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 28 | private final static long BROADCAST_VAL = 0x0000FFFFFFFFFFFFL; |
| 29 | public static final MacAddress BROADCAST = new MacAddress(BROADCAST_VAL); |
| 30 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 31 | public static final MacAddress NO_MASK = MacAddress.of(0xFFFFFFFFFFFFFFFFl); |
| 32 | public static final MacAddress FULL_MASK = MacAddress.of(0x0); |
| 33 | |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 34 | private static final long LLDP_MAC_ADDRESS_MASK = 0xfffffffffff0L; |
| 35 | private static final long LLDP_MAC_ADDRESS_VALUE = 0x0180c2000000L; |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 36 | |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 37 | private static final String FORMAT_ERROR = "Mac address is not well-formed. " + |
| 38 | "It must consist of 6 hex digit pairs separated by colons: "; |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 39 | private static final int MAC_STRING_LENGTH = 6 * 2 + 5; |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 40 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 41 | private MacAddress(final long rawValue) { |
| 42 | this.rawValue = rawValue; |
| 43 | } |
| 44 | |
| 45 | public static MacAddress of(final byte[] address) { |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 46 | if (address.length != MacAddrLen) |
| 47 | throw new IllegalArgumentException( |
| 48 | "Mac address byte array must be exactly 6 bytes long; length = " + address.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 49 | long raw = |
| 50 | (address[0] & 0xFFL) << 40 | (address[1] & 0xFFL) << 32 |
| 51 | | (address[2] & 0xFFL) << 24 | (address[3] & 0xFFL) << 16 |
| 52 | | (address[4] & 0xFFL) << 8 | (address[5] & 0xFFL); |
| 53 | return MacAddress.of(raw); |
| 54 | } |
| 55 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 56 | public static MacAddress of(long raw) { |
| 57 | raw &= BROADCAST_VAL; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 58 | if(raw == NONE_VAL) |
| 59 | return NONE; |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 60 | if (raw == BROADCAST_VAL) |
| 61 | return BROADCAST; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 62 | return new MacAddress(raw); |
| 63 | } |
| 64 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 65 | /** Parse a mac adress from the canonical string representation as |
| 66 | * 6 hex bytes separated by colons (01:02:03:04:05:06). |
| 67 | * |
| 68 | * @param macString - a mac address in canonical string representation |
| 69 | * @return the parsed MacAddress |
| 70 | * @throws IllegalArgumentException if macString is not a valid mac adddress |
| 71 | */ |
| 72 | @Nonnull |
| 73 | public static MacAddress of(@Nonnull final String macString) throws IllegalArgumentException { |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 74 | if (macString == null) { |
| 75 | throw new NullPointerException("macString must not be null"); |
| 76 | } |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 77 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 78 | int index = 0; |
| 79 | int shift = 40; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 80 | long raw = 0; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 81 | |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 82 | if (macString.length() != MAC_STRING_LENGTH) { |
| 83 | throw new IllegalArgumentException(FORMAT_ERROR + macString); |
| 84 | } |
| 85 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 86 | while (shift >= 0) { |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 87 | int digit1 = Character.digit(macString.charAt(index++), 16); |
| 88 | int digit2 = Character.digit(macString.charAt(index++), 16); |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 89 | if ((digit1 < 0) || (digit2 < 0)) |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 90 | throw new IllegalArgumentException(FORMAT_ERROR + macString); |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 91 | raw |= ((long) (digit1 << 4 | digit2)) << shift; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 92 | |
| 93 | if (shift == 0) |
| 94 | break; |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 95 | if (macString.charAt(index++) != ':') |
| 96 | throw new IllegalArgumentException(FORMAT_ERROR + macString); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 97 | shift -= 8; |
| 98 | } |
| 99 | return MacAddress.of(raw); |
| 100 | } |
| 101 | |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Creates a {@link MacAddress} from a {@link DatapathId}. This factory |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 104 | * method assumes that the first two bytes of the {@link DatapathId} are 0 bytes. |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 105 | * @param dpid the {@link DatapathId} to create the {@link MacAddress} from |
| 106 | * @return a {@link MacAddress} derived from the supplied {@link DatapathId} |
| 107 | */ |
| 108 | public static MacAddress of(@Nonnull DatapathId dpid) { |
| 109 | Preconditions.checkNotNull(dpid, "dpid must not be null"); |
| 110 | |
Sovietaced | 729bc17 | 2015-07-23 21:06:04 -0400 | [diff] [blame] | 111 | long raw = dpid.getLong(); |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 112 | |
Sovietaced | 729bc17 | 2015-07-23 21:06:04 -0400 | [diff] [blame] | 113 | // Mask out valid bytes |
| 114 | if( (raw & ~BROADCAST_VAL) != 0L) { |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 115 | throw new IllegalArgumentException("First two bytes of supplied " |
Sovietaced | 729bc17 | 2015-07-23 21:06:04 -0400 | [diff] [blame] | 116 | + "Datapathid must be 0"); |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 117 | } |
Sovietaced | 729bc17 | 2015-07-23 21:06:04 -0400 | [diff] [blame] | 118 | return of(raw); |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Andreas Wundsam | 4e2469e | 2014-02-17 15:32:43 -0800 | [diff] [blame] | 121 | private volatile byte[] bytesCache = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 122 | |
| 123 | public byte[] getBytes() { |
| 124 | if (bytesCache == null) { |
| 125 | synchronized (this) { |
| 126 | if (bytesCache == null) { |
| 127 | bytesCache = |
| 128 | new byte[] { (byte) ((rawValue >> 40) & 0xFF), |
| 129 | (byte) ((rawValue >> 32) & 0xFF), |
| 130 | (byte) ((rawValue >> 24) & 0xFF), |
| 131 | (byte) ((rawValue >> 16) & 0xFF), |
| 132 | (byte) ((rawValue >> 8) & 0xFF), |
| 133 | (byte) ((rawValue >> 0) & 0xFF) }; |
| 134 | } |
| 135 | } |
| 136 | } |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 137 | return Arrays.copyOf(bytesCache, bytesCache.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Returns {@code true} if the MAC address is the broadcast address. |
| 142 | * @return {@code true} if the MAC address is the broadcast address. |
| 143 | */ |
| 144 | public boolean isBroadcast() { |
| 145 | return this == BROADCAST; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns {@code true} if the MAC address is a multicast address. |
| 150 | * @return {@code true} if the MAC address is a multicast address. |
| 151 | */ |
| 152 | public boolean isMulticast() { |
| 153 | if (isBroadcast()) { |
| 154 | return false; |
| 155 | } |
| 156 | return (rawValue & (0x01L << 40)) != 0; |
| 157 | } |
| 158 | |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 159 | /** |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 160 | * Returns {@code true} if the MAC address is an LLDP mac address. |
| 161 | * @return {@code true} if the MAC address is an LLDP mac address. |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 162 | */ |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 163 | public boolean isLLDPAddress() { |
| 164 | return (rawValue & LLDP_MAC_ADDRESS_MASK) == LLDP_MAC_ADDRESS_VALUE; |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 167 | @Override |
| 168 | public int getLength() { |
| 169 | return MacAddrLen; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public String toString() { |
| 174 | return HexString.toHexString(rawValue, 6); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public int hashCode() { |
| 179 | final int prime = 31; |
| 180 | int result = 1; |
| 181 | result = prime * result + (int) (rawValue ^ (rawValue >>> 32)); |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean equals(final Object obj) { |
| 187 | if (this == obj) |
| 188 | return true; |
| 189 | if (obj == null) |
| 190 | return false; |
| 191 | if (getClass() != obj.getClass()) |
| 192 | return false; |
| 193 | MacAddress other = (MacAddress) obj; |
| 194 | if (rawValue != other.rawValue) |
| 195 | return false; |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | public long getLong() { |
| 200 | return rawValue; |
| 201 | } |
| 202 | |
| 203 | public void write6Bytes(ChannelBuffer c) { |
| 204 | c.writeInt((int) (this.rawValue >> 16)); |
| 205 | c.writeShort((int) this.rawValue & 0xFFFF); |
| 206 | } |
| 207 | |
| 208 | public static MacAddress read6Bytes(ChannelBuffer c) throws OFParseError { |
| 209 | long raw = c.readUnsignedInt() << 16 | c.readUnsignedShort(); |
| 210 | return MacAddress.of(raw); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public MacAddress applyMask(MacAddress mask) { |
| 215 | return MacAddress.of(this.rawValue & mask.rawValue); |
| 216 | } |
| 217 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 218 | @Override |
| 219 | public int compareTo(MacAddress o) { |
| 220 | return Longs.compare(rawValue, o.rawValue); |
| 221 | } |
| 222 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 223 | @Override |
| 224 | public void putTo(PrimitiveSink sink) { |
| 225 | sink.putInt((int) (this.rawValue >> 16)); |
| 226 | sink.putShort((short) (this.rawValue & 0xFFFF)); |
| 227 | } |
| 228 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 229 | |
| 230 | |
| 231 | } |