blob: ecfcbd85799be2836c8b6ef8650900f36c4274e0 [file] [log] [blame]
alshabibc4901cd2014-09-05 16:50:40 -07001/*******************************************************************************
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 ******************************************************************************/
16package org.onlab.packet;
17
18import java.nio.ByteBuffer;
19import java.util.Arrays;
20import java.util.LinkedList;
21import java.util.List;
22
23import org.apache.commons.lang.ArrayUtils;
alshabibdf652ad2014-09-09 11:53:19 -070024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
alshabibc4901cd2014-09-05 16:50:40 -070026
27
28/**
29 * LLDP packets OpenVirteX uses for discovery of physical network topology.
30 * Refer to IEEE Std 802.1ABTM-2009 for more information.
31 *
32 */
alshabib7911a052014-10-16 17:49:37 -070033@Deprecated
alshabib289652c2014-09-07 19:09:28 -070034public class ONLabLddp extends LLDP {
alshabibc4901cd2014-09-05 16:50:40 -070035
alshabib7235d092014-09-09 16:46:27 -070036 private static final Logger LOG = LoggerFactory.getLogger(ONLabLddp.class);
alshabibc4901cd2014-09-05 16:50:40 -070037 // ON.Lab OUI and OVX name for organizationally specific TLVs
38 public static final byte[] ONLAB_OUI = {(byte) 0xa4, 0x23, 0x05};
39 public static final String OVX_NAME = "OpenVirteX";
40 public static final byte[] LLDP_NICIRA = {0x01, 0x23, 0x20, 0x00, 0x00,
41 0x01};
42 public static final byte[] LLDP_MULTICAST = {0x01, (byte) 0x80,
43 (byte) 0xc2, 0x00, 0x00, 0x0e};
44 public static final byte[] BDDP_MULTICAST = {(byte) 0xff, (byte) 0xff,
45 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
46 public static final short ETHERTYPE_VLAN = (short) 0x8100;
47
48 // TLV constants: type, size and subtype
49 // Organizationally specific TLV also have packet offset and contents of TLV
50 // header
51 private static final byte CHASSIS_TLV_TYPE = 1;
52 private static final byte CHASSIS_TLV_SIZE = 7;
53 private static final byte CHASSIS_TLV_SUBTYPE = 4;
54
55 private static final byte PORT_TLV_TYPE = 2;
alshabibdf652ad2014-09-09 11:53:19 -070056 private static final byte PORT_TLV_SIZE = 5;
alshabibc4901cd2014-09-05 16:50:40 -070057 private static final byte PORT_TLV_SUBTYPE = 2;
58
59 private static final byte TTL_TLV_TYPE = 3;
60 private static final byte TTL_TLV_SIZE = 2;
61
62 private static final byte NAME_TLV_TYPE = 127;
63 // 4 = OUI (3) + subtype (1)
alshabib289652c2014-09-07 19:09:28 -070064 private static final byte NAME_TLV_SIZE = (byte) (4 + ONLabLddp.OVX_NAME.length());
alshabibc4901cd2014-09-05 16:50:40 -070065 private static final byte NAME_TLV_SUBTYPE = 1;
alshabibdf652ad2014-09-09 11:53:19 -070066 private static final short NAME_TLV_OFFSET = 34;
alshabibc4901cd2014-09-05 16:50:40 -070067 private static final short NAME_TLV_HEADER = (short) ((NAME_TLV_TYPE << 9) | NAME_TLV_SIZE);
68 // Contents of full name TLV
69 private static final byte[] NAME_TLV = ByteBuffer.allocate(NAME_TLV_SIZE + 2)
70 .putShort(NAME_TLV_HEADER).put(ONLAB_OUI).put(NAME_TLV_SUBTYPE)
71 .put(OVX_NAME.getBytes()).array();
72
73 private static final byte DPID_TLV_TYPE = 127;
74 private static final byte DPID_TLV_SIZE = (byte) (12); // 12 = OUI (3) + subtype
75 // (1) + dpid (8)
76 private static final byte DPID_TLV_SUBTYPE = 2;
77 private static final short DPID_TLV_HEADER = (short) ((DPID_TLV_TYPE << 9) | DPID_TLV_SIZE);
78 // Contents of dpid TLV
79 // Note that this does *not* contain the actual dpid since we cannot match
80 // on it
81 private static final byte[] DPID_TLV = ByteBuffer.allocate(DPID_TLV_SIZE + 2 - 8)
82 .putShort(DPID_TLV_HEADER).put(ONLAB_OUI).put(DPID_TLV_SUBTYPE)
83 .array();
84
85 // Pre-built contents of both organizationally specific TLVs
86 private static final byte[] OUI_TLV = ArrayUtils.addAll(NAME_TLV, DPID_TLV);
87
88 // Default switch, port number and TTL
89 private static final byte[] DEFAULT_DPID = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00 };
alshabibdf652ad2014-09-09 11:53:19 -070091 private static final int DEFAULT_PORT = 0;
alshabibc4901cd2014-09-05 16:50:40 -070092 private static final short DEFAULT_TTL = 120; // in seconds
93
94 // Minimum and OVX-generated LLDP packet sizes
95 private static final short MINIMUM_LLDP_SIZE = 61;
96 // Add 12 for 2-byte header of each TLV and a single EndOfLLDPTLV
97 private static final short OVX_LLDP_SIZE = (short) (CHASSIS_TLV_SIZE
98 + PORT_TLV_SIZE + TTL_TLV_SIZE + NAME_TLV_SIZE + DPID_TLV_SIZE + 12);
99
100 // Field offsets in OVX-generated LLDP
101 private static final short ETHERTYPE_OFFSET = 12;
102 private static final short PORT_OFFSET = 26;
alshabibdf652ad2014-09-09 11:53:19 -0700103 private static final short DPID_OFFSET = 56;
alshabibc4901cd2014-09-05 16:50:40 -0700104
105 // Private member fields
106 // Byte arrays for TLV information string
107 private ByteBuffer bb;
108 private final byte[] chassisId = new byte[CHASSIS_TLV_SIZE];
109 private final byte[] portId = new byte[PORT_TLV_SIZE];
110 private final byte[] ttl = new byte[TTL_TLV_SIZE];
111 private final byte[] ouiName = new byte[NAME_TLV_SIZE];
112 private final byte[] ouiDpid = new byte[DPID_TLV_SIZE];
113
114 // TLVs
115 private final LLDPTLV chassisTLV;
116 private final LLDPTLV portTLV;
117 private final LLDPTLV ttlTLV;
118 private final LLDPTLV ouiNameTLV;
119 private final LLDPTLV ouiDpidTLV;
120 private final List<LLDPTLV> optionalTLVList;
121
122 /**
123 * Instantiates a new OVX LDDP message.
124 */
alshabib289652c2014-09-07 19:09:28 -0700125 public ONLabLddp() {
alshabibc4901cd2014-09-05 16:50:40 -0700126 // Create TLVs
127 this.chassisTLV = new LLDPTLV();
128 this.portTLV = new LLDPTLV();
129 this.ttlTLV = new LLDPTLV();
130 this.ouiNameTLV = new LLDPTLV();
131 this.ouiDpidTLV = new LLDPTLV();
132 this.optionalTLVList = new LinkedList<LLDPTLV>();
133 this.optionalTLVList.add(this.ouiNameTLV);
134 this.optionalTLVList.add(this.ouiDpidTLV);
135
136 // Add TLVs to LLDP packet
137 this.setChassisId(this.chassisTLV);
138 this.setPortId(this.portTLV);
139 this.setTtl(this.ttlTLV);
140 this.setOptionalTLVList(this.optionalTLVList);
141
142 // Set TLVs to default values
143 this.setChassisTLV(DEFAULT_DPID);
144 this.setPortTLV(DEFAULT_PORT);
145 this.setTTLTLV(DEFAULT_TTL);
alshabib289652c2014-09-07 19:09:28 -0700146 this.setOUIName(ONLabLddp.OVX_NAME);
alshabibc4901cd2014-09-05 16:50:40 -0700147 this.setOUIDpid(DEFAULT_DPID);
148 }
149
150 /**
151 * Sets chassis TLV. Note that we can only put 6 bytes in the chassis ID, so
152 * we use another organizationally specific TLV to put the full dpid (see
153 * setOUIDpid()).
154 *
155 * @param dpid the switch DPID
156 */
157 private void setChassisTLV(final byte[] dpid) {
158 this.bb = ByteBuffer.wrap(this.chassisId);
159 this.bb.put(CHASSIS_TLV_SUBTYPE);
160 for (int i = 2; i < 8; i++) {
161 bb.put(dpid[i]);
162 }
163 this.chassisTLV.setLength(CHASSIS_TLV_SIZE);
164 this.chassisTLV.setType(CHASSIS_TLV_TYPE);
165 this.chassisTLV.setValue(this.chassisId);
166 }
167
168 /**
169 * Sets port TLV.
170 *
171 * @param portNumber the port number
172 */
alshabibdf652ad2014-09-09 11:53:19 -0700173 private void setPortTLV(final int portNumber) {
alshabibc4901cd2014-09-05 16:50:40 -0700174 this.bb = ByteBuffer.wrap(this.portId);
175 this.bb.put(PORT_TLV_SUBTYPE);
alshabibdf652ad2014-09-09 11:53:19 -0700176 this.bb.putInt(portNumber);
alshabibc4901cd2014-09-05 16:50:40 -0700177
178 this.portTLV.setLength(PORT_TLV_SIZE);
179 this.portTLV.setType(PORT_TLV_TYPE);
180 this.portTLV.setValue(this.portId);
181 }
182
183 /**
184 * Sets Time To Live TLV.
185 *
186 * @param time the time to live
187 */
188 private void setTTLTLV(final short time) {
189 this.bb = ByteBuffer.wrap(this.ttl);
190 this.bb.putShort(time);
191
192 this.ttlTLV.setLength(TTL_TLV_SIZE);
193 this.ttlTLV.setType(TTL_TLV_TYPE);
194 this.ttlTLV.setValue(this.ttl);
195 }
196
197 /**
198 * Set. organizationally specific TLV for OVX name (subtype 1).
199 *
200 * @param name the name
201 */
202 private void setOUIName(final String name) {
203 this.bb = ByteBuffer.wrap(ouiName);
alshabib289652c2014-09-07 19:09:28 -0700204 this.bb.put(ONLabLddp.ONLAB_OUI);
alshabibc4901cd2014-09-05 16:50:40 -0700205 this.bb.put(NAME_TLV_SUBTYPE);
206 this.bb.put(name.getBytes());
207
208 this.ouiNameTLV.setLength(NAME_TLV_SIZE);
209 this.ouiNameTLV.setType(NAME_TLV_TYPE);
210 this.ouiNameTLV.setValue(ouiName);
211 }
212
213 /**
214 * Sets organizationally specific TLV for OVX full dpid (subtype 2).
215 *
216 * @param dpid the switch DPID
217 */
218 private void setOUIDpid(final byte[] dpid) {
219 this.bb = ByteBuffer.wrap(ouiDpid);
alshabib289652c2014-09-07 19:09:28 -0700220 this.bb.put(ONLabLddp.ONLAB_OUI);
alshabibc4901cd2014-09-05 16:50:40 -0700221 this.bb.put(DPID_TLV_SUBTYPE);
222 this.bb.put(dpid);
223
224 this.ouiDpidTLV.setLength(DPID_TLV_SIZE);
225 this.ouiDpidTLV.setType(DPID_TLV_TYPE);
226 this.ouiDpidTLV.setValue(ouiDpid);
227 }
228
229 /**
230 * Sets switch DPID in LLDP packet.
231 *
tom5f18cf32014-09-13 14:10:57 -0700232 * @param dp the switch instance
alshabibc4901cd2014-09-05 16:50:40 -0700233 */
234 public void setSwitch(long dp) {
235 final byte[] dpid = ByteBuffer.allocate(8).putLong(dp)
236 .array();
237 this.setChassisTLV(dpid);
238 this.setOUIDpid(dpid);
239 }
240
241 /**
242 * Sets port in LLDP packet.
243 *
244 * @param port the port instance
245 */
alshabibdf652ad2014-09-09 11:53:19 -0700246 public void setPort(int port) {
tom5f18cf32014-09-13 14:10:57 -0700247 this.setPortTLV(port);
alshabibc4901cd2014-09-05 16:50:40 -0700248 }
249
250 /**
251 * Serializes full LLDP packet to byte array. Need to set both switch and
252 * port before you can serialize.
253 */
254 @Override
255 public byte[] serialize() {
256 return super.serialize();
257 }
258
259 /**
260 * Checks if LLDP packet has correct size, LLDP multicast address, and
261 * ethertype. Packet assumed to have Ethernet header.
262 *
tom5f18cf32014-09-13 14:10:57 -0700263 * @param packet packet data
alshabibc4901cd2014-09-05 16:50:40 -0700264 * @return true if packet is LLDP, false otherwise
265 */
266 public static boolean isLLDP(final byte[] packet) {
267 // Does packet exist and does it have the mininum size?
268 if (packet == null || packet.length < MINIMUM_LLDP_SIZE) {
269 return false;
270 }
271
272 // Packet has LLDP multicast destination address?
273 final ByteBuffer bb = ByteBuffer.wrap(packet);
274 final byte[] dst = new byte[6];
275 bb.get(dst);
276
alshabib289652c2014-09-07 19:09:28 -0700277 if (!(Arrays.equals(dst, ONLabLddp.LLDP_NICIRA)
278 || Arrays.equals(dst, ONLabLddp.LLDP_MULTICAST) || Arrays.equals(
279 dst, ONLabLddp.BDDP_MULTICAST))) {
alshabibc4901cd2014-09-05 16:50:40 -0700280
281 return false;
282 }
283
284 // Fetch ethertype, skip VLAN tag if it's there
285 short etherType = bb.getShort(ETHERTYPE_OFFSET);
286 if (etherType == ETHERTYPE_VLAN) {
287 etherType = bb.getShort(ETHERTYPE_OFFSET + 4);
288 }
289
290 // Check ethertype
291 if (etherType == Ethernet.TYPE_LLDP) {
292 return true;
293 }
294 if (etherType == Ethernet.TYPE_BSN) {
295 return true;
296 }
297
298 return false;
299
300 }
301
302 /**
303 * Checks if packet has size of OVX-generated LLDP, and correctness of two
304 * organizationally specific TLVs that use ON.Lab's OUI. Assumes packet is
305 * valid LLDP packet
306 *
tom5f18cf32014-09-13 14:10:57 -0700307 * @param packet packet data
308 * @return eth type or -1
alshabibc4901cd2014-09-05 16:50:40 -0700309 */
alshabib9ee68172014-09-09 14:45:14 -0700310 public static short isOVXLLDP(byte[] packet) {
alshabibc4901cd2014-09-05 16:50:40 -0700311 if (packet.length < OVX_LLDP_SIZE) {
alshabib9ee68172014-09-09 14:45:14 -0700312 return -1;
alshabibc4901cd2014-09-05 16:50:40 -0700313 }
314
315 // Extra offset due to VLAN tag
316 final ByteBuffer bb = ByteBuffer.wrap(packet);
317 int offset = 0;
alshabib9ee68172014-09-09 14:45:14 -0700318 short ethType = bb.getShort(ETHERTYPE_OFFSET);
319 if (ethType != Ethernet.TYPE_LLDP
320 && ethType != Ethernet.TYPE_BSN) {
alshabibc4901cd2014-09-05 16:50:40 -0700321 offset = 4;
alshabib9ee68172014-09-09 14:45:14 -0700322 ethType = bb.getShort(ETHERTYPE_OFFSET + offset);
323 if (ethType != Ethernet.TYPE_LLDP
324 && ethType != Ethernet.TYPE_BSN) {
325 return -1;
326 }
alshabibc4901cd2014-09-05 16:50:40 -0700327 }
328
329 // Compare packet's organizationally specific TLVs to the expected
330 // values
331 for (int i = 0; i < OUI_TLV.length; i++) {
332 if (packet[NAME_TLV_OFFSET + offset + i] != OUI_TLV[i]) {
alshabib9ee68172014-09-09 14:45:14 -0700333 return -1;
alshabibc4901cd2014-09-05 16:50:40 -0700334 }
335 }
336
alshabib9ee68172014-09-09 14:45:14 -0700337 return ethType;
alshabibc4901cd2014-09-05 16:50:40 -0700338 }
339
340 /**
341 * Extracts dpid and port from OVX-generated LLDP packet.
342 *
tom5f18cf32014-09-13 14:10:57 -0700343 * @param packet packet data
alshabibc4901cd2014-09-05 16:50:40 -0700344 * @return Dpid and port
345 */
alshabibdf652ad2014-09-09 11:53:19 -0700346 public static DPIDandPort parseLLDP(final byte[] packet) {
alshabibc4901cd2014-09-05 16:50:40 -0700347 final ByteBuffer bb = ByteBuffer.wrap(packet);
348
349 // Extra offset due to VLAN tag
350 int offset = 0;
351 if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP
352 && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) {
353 offset = 4;
354 }
355
alshabibdf652ad2014-09-09 11:53:19 -0700356 final int port = bb.getInt(PORT_OFFSET + offset);
alshabibc4901cd2014-09-05 16:50:40 -0700357 final long dpid = bb.getLong(DPID_OFFSET + offset);
358
alshabibdf652ad2014-09-09 11:53:19 -0700359 return new DPIDandPort(dpid, port);
alshabibc4901cd2014-09-05 16:50:40 -0700360 }
alshabibdf652ad2014-09-09 11:53:19 -0700361
362 public static class DPIDandPort {
363
364 private final long dpid;
365 private final int port;
366
367 public DPIDandPort(long dpid, int port) {
368 this.dpid = dpid;
369 this.port = port;
370 }
371
372 public long getDpid() {
373 return this.dpid;
374 }
375
376 public int getPort() {
377 return this.port;
378 }
379
380 }
381
alshabibc4901cd2014-09-05 16:50:40 -0700382}