blob: 520d67093f0a71384579dbf9de2ad7a25a2933d4 [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 */
33@SuppressWarnings("rawtypes")
alshabib289652c2014-09-07 19:09:28 -070034public class ONLabLddp extends LLDP {
alshabibc4901cd2014-09-05 16:50:40 -070035
alshabibdf652ad2014-09-09 11:53:19 -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 *
232 * @param sw the switch instance
233 */
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) {
247 int portNumber = port;
alshabibc4901cd2014-09-05 16:50:40 -0700248 this.setPortTLV(portNumber);
249 }
250
251 /**
252 * Serializes full LLDP packet to byte array. Need to set both switch and
253 * port before you can serialize.
254 */
255 @Override
256 public byte[] serialize() {
257 return super.serialize();
258 }
259
260 /**
261 * Checks if LLDP packet has correct size, LLDP multicast address, and
262 * ethertype. Packet assumed to have Ethernet header.
263 *
264 * @param packet
265 * @return true if packet is LLDP, false otherwise
266 */
267 public static boolean isLLDP(final byte[] packet) {
268 // Does packet exist and does it have the mininum size?
269 if (packet == null || packet.length < MINIMUM_LLDP_SIZE) {
270 return false;
271 }
272
273 // Packet has LLDP multicast destination address?
274 final ByteBuffer bb = ByteBuffer.wrap(packet);
275 final byte[] dst = new byte[6];
276 bb.get(dst);
277
alshabib289652c2014-09-07 19:09:28 -0700278 if (!(Arrays.equals(dst, ONLabLddp.LLDP_NICIRA)
279 || Arrays.equals(dst, ONLabLddp.LLDP_MULTICAST) || Arrays.equals(
280 dst, ONLabLddp.BDDP_MULTICAST))) {
alshabibc4901cd2014-09-05 16:50:40 -0700281
282 return false;
283 }
284
285 // Fetch ethertype, skip VLAN tag if it's there
286 short etherType = bb.getShort(ETHERTYPE_OFFSET);
287 if (etherType == ETHERTYPE_VLAN) {
288 etherType = bb.getShort(ETHERTYPE_OFFSET + 4);
289 }
290
291 // Check ethertype
292 if (etherType == Ethernet.TYPE_LLDP) {
293 return true;
294 }
295 if (etherType == Ethernet.TYPE_BSN) {
296 return true;
297 }
298
299 return false;
300
301 }
302
303 /**
304 * Checks if packet has size of OVX-generated LLDP, and correctness of two
305 * organizationally specific TLVs that use ON.Lab's OUI. Assumes packet is
306 * valid LLDP packet
307 *
308 * @param packet
309 * @return
310 */
311 public static boolean isOVXLLDP(byte[] packet) {
312 if (packet.length < OVX_LLDP_SIZE) {
313 return false;
314 }
315
316 // Extra offset due to VLAN tag
317 final ByteBuffer bb = ByteBuffer.wrap(packet);
318 int offset = 0;
319 if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP
320 && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) {
321 offset = 4;
322 }
323
324 // Compare packet's organizationally specific TLVs to the expected
325 // values
326 for (int i = 0; i < OUI_TLV.length; i++) {
327 if (packet[NAME_TLV_OFFSET + offset + i] != OUI_TLV[i]) {
328 return false;
329 }
330 }
331
332 return true;
333 }
334
335 /**
336 * Extracts dpid and port from OVX-generated LLDP packet.
337 *
338 * @param packet
339 * @return Dpid and port
340 */
alshabibdf652ad2014-09-09 11:53:19 -0700341 public static DPIDandPort parseLLDP(final byte[] packet) {
alshabibc4901cd2014-09-05 16:50:40 -0700342 final ByteBuffer bb = ByteBuffer.wrap(packet);
343
344 // Extra offset due to VLAN tag
345 int offset = 0;
346 if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP
347 && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) {
348 offset = 4;
349 }
350
alshabibdf652ad2014-09-09 11:53:19 -0700351 final int port = bb.getInt(PORT_OFFSET + offset);
alshabibc4901cd2014-09-05 16:50:40 -0700352 final long dpid = bb.getLong(DPID_OFFSET + offset);
353
alshabibdf652ad2014-09-09 11:53:19 -0700354 return new DPIDandPort(dpid, port);
alshabibc4901cd2014-09-05 16:50:40 -0700355 }
alshabibdf652ad2014-09-09 11:53:19 -0700356
357 public static class DPIDandPort {
358
359 private final long dpid;
360 private final int port;
361
362 public DPIDandPort(long dpid, int port) {
363 this.dpid = dpid;
364 this.port = port;
365 }
366
367 public long getDpid() {
368 return this.dpid;
369 }
370
371 public int getPort() {
372 return this.port;
373 }
374
375 }
376
alshabibc4901cd2014-09-05 16:50:40 -0700377}