Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | ******************************************************************************/ |
| 16 | package net.onrc.onos.core.packet; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import net.onrc.onos.core.util.SwitchPort; |
| 24 | |
| 25 | import org.apache.commons.lang.ArrayUtils; |
| 26 | |
| 27 | import com.google.common.base.Charsets; |
| 28 | |
| 29 | /** |
| 30 | * LLDP packets ONOS uses for discovery of physical network topology. |
| 31 | * Refer to IEEE Std 802.1ABTM-2009 for more information. |
| 32 | * |
| 33 | */ |
| 34 | public class OnosLldp extends LLDP { |
| 35 | |
| 36 | // ON.Lab OUI and ONOS name for organizationally specific TLVs |
| 37 | static final byte[] ONLAB_OUI = {(byte) 0xa4, 0x23, 0x05}; |
| 38 | public static final String ONOS_NAME = "ONOSVirteX"; |
| 39 | static final byte[] LLDP_NICIRA = {0x01, 0x23, 0x20, 0x00, 0x00, |
| 40 | 0x01}; |
| 41 | static final byte[] LLDP_MULTICAST = {0x01, (byte) 0x80, |
| 42 | (byte) 0xc2, 0x00, 0x00, 0x0e}; |
| 43 | static final byte[] BDDP_MULTICAST = {(byte) 0xff, (byte) 0xff, |
| 44 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
| 45 | public static final short ETHERTYPE_VLAN = (short) 0x8100; |
| 46 | |
| 47 | // TLV constants: type, size and subtype |
| 48 | // Organizationally specific TLV also have packet offset and contents of TLV |
| 49 | // header |
| 50 | private static final byte CHASSIS_TLV_TYPE = 1; |
| 51 | private static final byte CHASSIS_TLV_SIZE = 7; |
| 52 | private static final byte CHASSIS_TLV_SUBTYPE = 4; |
| 53 | |
| 54 | private static final byte PORT_TLV_TYPE = 2; |
| 55 | private static final byte PORT_TLV_SIZE = 3; |
| 56 | private static final byte PORT_TLV_SUBTYPE = 2; |
| 57 | |
| 58 | private static final byte TTL_TLV_TYPE = 3; |
| 59 | private static final byte TTL_TLV_SIZE = 2; |
| 60 | |
| 61 | private static final byte NAME_TLV_TYPE = 127; |
| 62 | // 4 = OUI (3) + subtype (1) |
| 63 | private static final byte NAME_TLV_SIZE = (byte) (4 + OnosLldp.ONOS_NAME.length()); |
| 64 | private static final byte NAME_TLV_SUBTYPE = 1; |
| 65 | private static final short NAME_TLV_OFFSET = 32; |
| 66 | private static final short NAME_TLV_HEADER = (short) ((NAME_TLV_TYPE << 9) | (NAME_TLV_SIZE & 0xff)); |
| 67 | // Contents of full name TLV |
| 68 | private static final byte[] NAME_TLV = ByteBuffer.allocate(NAME_TLV_SIZE + 2) |
| 69 | .putShort(NAME_TLV_HEADER).put(ONLAB_OUI).put(NAME_TLV_SUBTYPE) |
| 70 | .put(ONOS_NAME.getBytes(Charsets.UTF_8)).array(); |
| 71 | |
| 72 | private static final byte DPID_TLV_TYPE = 127; |
| 73 | private static final byte DPID_TLV_SIZE = (byte) (12); // 12 = OUI (3) + subtype |
| 74 | // (1) + dpid (8) |
| 75 | private static final byte DPID_TLV_SUBTYPE = 2; |
| 76 | private static final short DPID_TLV_HEADER = (short) ((DPID_TLV_TYPE << 9) | DPID_TLV_SIZE); |
| 77 | // Contents of dpid TLV |
| 78 | // Note that this does *not* contain the actual dpid since we cannot match |
| 79 | // on it |
| 80 | private static final byte[] DPID_TLV = ByteBuffer.allocate(DPID_TLV_SIZE + 2 - 8) |
| 81 | .putShort(DPID_TLV_HEADER).put(ONLAB_OUI).put(DPID_TLV_SUBTYPE) |
| 82 | .array(); |
| 83 | |
| 84 | // Pre-built contents of both organizationally specific TLVs |
| 85 | private static final byte[] OUI_TLV = ArrayUtils.addAll(NAME_TLV, DPID_TLV); |
| 86 | |
| 87 | // Default switch, port number and TTL |
| 88 | private static final byte[] DEFAULT_DPID = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 89 | 0x00, 0x00 }; |
| 90 | private static final short DEFAULT_PORT = 0; |
| 91 | private static final short DEFAULT_TTL = 120; // in seconds |
| 92 | |
| 93 | // Minimum and ONOS-generated LLDP packet sizes |
| 94 | private static final short MINIMUM_LLDP_SIZE = 61; |
| 95 | // Add 12 for 2-byte header of each TLV and a single EndOfLLDPTLV |
| 96 | private static final short ONOS_LLDP_SIZE = (short) (CHASSIS_TLV_SIZE |
| 97 | + PORT_TLV_SIZE + TTL_TLV_SIZE + NAME_TLV_SIZE + DPID_TLV_SIZE + 12); |
| 98 | |
| 99 | // Direction TLVs are used to indicate if the LLDPs were sent |
| 100 | // periodically or in response to a received LLDP |
| 101 | private static final byte TLV_DIRECTION_TYPE = 0x73; |
| 102 | private static final short TLV_DIRECTION_LENGTH = 1; // 1 byte |
| 103 | private static final byte[] TLV_DIRECTION_VALUE_FORWARD = {0x01}; |
| 104 | private static final byte[] TLV_DIRECTION_VALUE_REVERSE = {0x02}; |
| 105 | private static final LLDPTLV FORWARD_TLV |
| 106 | = new LLDPTLV(). |
| 107 | setType(TLV_DIRECTION_TYPE). |
| 108 | setLength(TLV_DIRECTION_LENGTH). |
| 109 | setValue(TLV_DIRECTION_VALUE_FORWARD); |
| 110 | |
| 111 | private static final LLDPTLV REVERSE_TLV |
| 112 | = new LLDPTLV(). |
| 113 | setType(TLV_DIRECTION_TYPE). |
| 114 | setLength(TLV_DIRECTION_LENGTH). |
| 115 | setValue(TLV_DIRECTION_VALUE_REVERSE); |
| 116 | |
| 117 | // Field offsets in ONOS-generated LLDP |
| 118 | private static final short ETHERTYPE_OFFSET = 12; |
| 119 | private static final short PORT_OFFSET = 26; |
| 120 | private static final short DPID_OFFSET = 54; |
| 121 | |
| 122 | // Private member fields |
| 123 | // Byte arrays for TLV information string |
| 124 | private byte[] chassisId = new byte[CHASSIS_TLV_SIZE]; |
| 125 | private byte[] portId = new byte[PORT_TLV_SIZE]; |
| 126 | private byte[] ttl = new byte[TTL_TLV_SIZE]; |
| 127 | private byte[] ouiName = new byte[NAME_TLV_SIZE]; |
| 128 | private byte[] ouiDpid = new byte[DPID_TLV_SIZE]; |
| 129 | |
| 130 | // TLVs |
| 131 | private LLDPTLV chassisTLV; |
| 132 | private LLDPTLV portTLV; |
| 133 | private LLDPTLV ttlTLV; |
| 134 | private LLDPTLV ouiNameTLV; |
| 135 | private LLDPTLV ouiDpidTLV; |
| 136 | private List<LLDPTLV> optionalTLVList; |
| 137 | |
| 138 | /** |
| 139 | * Instantiates a new ONOS LDDP message. |
| 140 | */ |
| 141 | public OnosLldp() { |
| 142 | // Create TLVs |
| 143 | this.chassisTLV = new LLDPTLV(); |
| 144 | this.portTLV = new LLDPTLV(); |
| 145 | this.ttlTLV = new LLDPTLV(); |
| 146 | this.ouiNameTLV = new LLDPTLV(); |
| 147 | this.ouiDpidTLV = new LLDPTLV(); |
| 148 | this.optionalTLVList = new LinkedList<LLDPTLV>(); |
| 149 | this.optionalTLVList.add(this.ouiNameTLV); |
| 150 | this.optionalTLVList.add(this.ouiDpidTLV); |
| 151 | |
| 152 | // Add TLVs to LLDP packet |
| 153 | this.setChassisId(this.chassisTLV); |
| 154 | this.setPortId(this.portTLV); |
| 155 | this.setTtl(this.ttlTLV); |
| 156 | this.setOptionalTLVList(this.optionalTLVList); |
| 157 | |
| 158 | // Set TLVs to default values |
| 159 | this.setChassisTLV(DEFAULT_DPID); |
| 160 | this.setPortTLV(DEFAULT_PORT); |
| 161 | this.setTTLTLV(DEFAULT_TTL); |
| 162 | this.setOUIName(OnosLldp.ONOS_NAME); |
| 163 | this.setOUIDpid(DEFAULT_DPID); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Sets chassis TLV. Note that we can only put 6 bytes in the chassis ID, so |
| 168 | * we use another organizationally specific TLV to put the full dpid (see |
| 169 | * setOUIDpid()). |
| 170 | * |
| 171 | * @param dpid the switch DPID |
| 172 | */ |
| 173 | private void setChassisTLV(final byte[] dpid) { |
| 174 | ByteBuffer bb = ByteBuffer.wrap(this.chassisId); |
| 175 | bb.put(CHASSIS_TLV_SUBTYPE); |
| 176 | for (int i = 2; i < 8; i++) { |
| 177 | bb.put(dpid[i]); |
| 178 | } |
| 179 | |
| 180 | this.chassisTLV.setLength(CHASSIS_TLV_SIZE); |
| 181 | this.chassisTLV.setType(CHASSIS_TLV_TYPE); |
| 182 | this.chassisTLV.setValue(this.chassisId); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets port TLV. |
| 187 | * |
| 188 | * @param portNumber the port number |
| 189 | */ |
| 190 | private void setPortTLV(final short portNumber) { |
| 191 | ByteBuffer bb = ByteBuffer.wrap(this.portId); |
| 192 | bb.put(PORT_TLV_SUBTYPE); |
| 193 | bb.putShort(portNumber); |
| 194 | |
| 195 | this.portTLV.setLength(PORT_TLV_SIZE); |
| 196 | this.portTLV.setType(PORT_TLV_TYPE); |
| 197 | this.portTLV.setValue(this.portId); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets Time To Live TLV. |
| 202 | * |
| 203 | * @param time the time to live |
| 204 | */ |
| 205 | private void setTTLTLV(final short time) { |
| 206 | ByteBuffer bb = ByteBuffer.wrap(this.ttl); |
| 207 | bb.putShort(time); |
| 208 | |
| 209 | this.ttlTLV.setLength(TTL_TLV_SIZE); |
| 210 | this.ttlTLV.setType(TTL_TLV_TYPE); |
| 211 | this.ttlTLV.setValue(this.ttl); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Sets organizationally specific TLV for ONOS name (subtype 1). |
| 216 | * |
| 217 | * @param name the name |
| 218 | */ |
| 219 | private void setOUIName(final String name) { |
| 220 | ByteBuffer bb = ByteBuffer.wrap(ouiName); |
| 221 | bb.put(OnosLldp.ONLAB_OUI); |
| 222 | bb.put(NAME_TLV_SUBTYPE); |
| 223 | bb.put(name.getBytes(Charsets.UTF_8)); |
| 224 | |
| 225 | this.ouiNameTLV.setLength(NAME_TLV_SIZE); |
| 226 | this.ouiNameTLV.setType(NAME_TLV_TYPE); |
| 227 | this.ouiNameTLV.setValue(ouiName); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Sets organizationally specific TLV for ONOS full dpid (subtype 2). |
| 232 | * |
| 233 | * @param dpid the switch DPID |
| 234 | */ |
| 235 | private void setOUIDpid(final byte[] dpid) { |
| 236 | ByteBuffer bb = ByteBuffer.wrap(ouiDpid); |
| 237 | bb.put(OnosLldp.ONLAB_OUI); |
| 238 | bb.put(DPID_TLV_SUBTYPE); |
| 239 | bb.put(dpid); |
| 240 | |
| 241 | this.ouiDpidTLV.setLength(DPID_TLV_SIZE); |
| 242 | this.ouiDpidTLV.setType(DPID_TLV_TYPE); |
| 243 | this.ouiDpidTLV.setValue(ouiDpid); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Sets switch DPID in LLDP packet. |
| 248 | * |
| 249 | * @param sw the switch dpid |
| 250 | */ |
| 251 | public void setSwitch(Long dpid) { |
| 252 | final byte[] byteDpid = ByteBuffer.allocate(8).putLong(dpid) |
| 253 | .array(); |
| 254 | this.setChassisTLV(byteDpid); |
| 255 | this.setOUIDpid(byteDpid); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * Sets the port number in LLDP packet. |
| 261 | * |
| 262 | * @param port the port number |
| 263 | */ |
| 264 | public void setPort(short portNumber) { |
| 265 | this.setPortTLV(portNumber); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Sets whether this is a forward or reverse LLDP. |
| 270 | * |
| 271 | * @param isReverse true if reverse, false if forward |
| 272 | */ |
| 273 | public void setReverse(boolean isReverse) { |
| 274 | optionalTLVList.add((isReverse) ? REVERSE_TLV : FORWARD_TLV); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Serializes full LLDP packet to byte array. |
| 279 | * |
| 280 | * @return the serialized packet |
| 281 | */ |
| 282 | public byte[] serialize() { |
| 283 | return super.serialize(); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Checks if LLDP packet has correct size, LLDP multicast address, and |
| 288 | * ethertype. Packet assumed to have Ethernet header. |
| 289 | * |
| 290 | * @param packet full packet starting from the Ethernet header |
| 291 | * @return true if packet is LLDP, false otherwise |
| 292 | */ |
| 293 | public static boolean isLLDP(final byte[] packet) { |
| 294 | // Does packet exist and does it have the mininum size? |
| 295 | if (packet == null || packet.length < MINIMUM_LLDP_SIZE) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | // Packet has LLDP multicast destination address? |
| 300 | final ByteBuffer bb = ByteBuffer.wrap(packet); |
| 301 | final byte[] dst = new byte[6]; |
| 302 | bb.get(dst); |
| 303 | |
| 304 | if (!(Arrays.equals(dst, OnosLldp.LLDP_NICIRA) |
| 305 | || Arrays.equals(dst, OnosLldp.LLDP_MULTICAST) || Arrays.equals( |
| 306 | dst, OnosLldp.BDDP_MULTICAST))) { |
| 307 | |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | // Fetch ethertype, skip VLAN tag if it's there |
| 312 | short etherType = bb.getShort(ETHERTYPE_OFFSET); |
| 313 | if (etherType == ETHERTYPE_VLAN) { |
| 314 | etherType = bb.getShort(ETHERTYPE_OFFSET + 4); |
| 315 | } |
| 316 | |
| 317 | // Check ethertype |
| 318 | if (etherType == Ethernet.TYPE_LLDP) { |
| 319 | return true; |
| 320 | } |
| 321 | if (etherType == Ethernet.TYPE_BSN) { |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | return false; |
| 326 | |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Checks if packet has size of ONOS-generated LLDP, and correctness of two |
| 331 | * organizationally specific TLVs that use ON.Lab's OUI. Assumes packet is |
| 332 | * valid LLDP packet |
| 333 | * |
| 334 | * @param packet full packet starting from the Ethernet header |
| 335 | * @return true if this is an ONOS-generated LLDP, otherwise false |
| 336 | */ |
| 337 | public static boolean isOnosLldp(byte[] packet) { |
| 338 | if (packet.length < ONOS_LLDP_SIZE) { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | // Extra offset due to VLAN tag |
| 343 | final ByteBuffer bb = ByteBuffer.wrap(packet); |
| 344 | int offset = 0; |
| 345 | if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP |
| 346 | && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) { |
| 347 | offset = 4; |
| 348 | } |
| 349 | |
| 350 | // Compare packet's organizationally specific TLVs to the expected |
| 351 | // values |
| 352 | for (int i = 0; i < OUI_TLV.length; i++) { |
| 353 | if (packet[NAME_TLV_OFFSET + offset + i] != OUI_TLV[i]) { |
| 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Extracts dpid and port from ONOS-generated LLDP packet. |
| 363 | * |
| 364 | * @param packet full packet started at the Ethernet header |
| 365 | * @return switchport switch and port info from the DPID and Port TLVs |
| 366 | */ |
| 367 | public static SwitchPort extractSwitchPort(final byte[] packet) { |
| 368 | final ByteBuffer bb = ByteBuffer.wrap(packet); |
| 369 | |
| 370 | // Extra offset due to VLAN tag |
| 371 | int offset = 0; |
| 372 | if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP |
| 373 | && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) { |
| 374 | offset = 4; |
| 375 | } |
| 376 | |
| 377 | final short port = bb.getShort(PORT_OFFSET + offset); |
| 378 | final long dpid = bb.getLong(DPID_OFFSET + offset); |
| 379 | |
| 380 | return new SwitchPort(dpid, port); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Checks if the LLDP is a reverse LLDP (i.e. sent in response to receiving |
| 385 | * an LLDP on the link). This information is stored in the Direction TLV. |
| 386 | * |
| 387 | * @param lldp parsed LLDP packet |
| 388 | * @return true if the LLDP is a reverse LLDP, otherwise false |
| 389 | */ |
| 390 | public static boolean isReverse(final LLDP lldp) { |
| 391 | for (LLDPTLV lldpTlv : lldp.getOptionalTLVList()) { |
| 392 | if ((lldpTlv.getType() == TLV_DIRECTION_TYPE) && |
| 393 | Arrays.equals(lldpTlv.getValue(), TLV_DIRECTION_VALUE_REVERSE)) { |
| 394 | return true; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public boolean equals(Object other) { |
| 403 | if (other == this) { |
| 404 | return true; |
| 405 | } |
| 406 | if (!super.equals(other)) { |
| 407 | return false; |
| 408 | } |
| 409 | // |
| 410 | // NOTE: Subclasses are are considered as change of identity, hence |
| 411 | // equals() will return false if the class type doesn't match. |
| 412 | // |
| 413 | if (getClass() != other.getClass()) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | OnosLldp otherLldp = (OnosLldp) other; |
| 418 | |
| 419 | if (!this.chassisTLV.equals(otherLldp.chassisTLV)) { |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | if (!this.portTLV.equals(otherLldp.portTLV)) { |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | if (!this.ttlTLV.equals(otherLldp.ttlTLV)) { |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | if (!this.ouiNameTLV.equals(otherLldp.ouiNameTLV)) { |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | if (!this.ouiDpidTLV.equals(otherLldp.ouiDpidTLV)) { |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public int hashCode() { |
| 444 | final int prime = 31; |
| 445 | int result = 1; |
| 446 | result = prime * result + chassisTLV.hashCode(); |
| 447 | result = prime * result + portTLV.hashCode(); |
| 448 | result = prime * result + ttlTLV.hashCode(); |
| 449 | result = prime * result + ouiNameTLV.hashCode(); |
| 450 | result = prime * result + ouiDpidTLV.hashCode(); |
| 451 | |
| 452 | return result; |
| 453 | } |
| 454 | } |