blob: 9e451c699b7d73e69e40548ab03a63151a83803b [file] [log] [blame]
jaegonkim1179d832017-06-13 19:30:55 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
jaegonkim1179d832017-06-13 19:30:55 +09003 *
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
17package org.onlab.packet;
18
19import java.nio.ByteBuffer;
20
21import static com.google.common.base.MoreObjects.toStringHelper;
22import static org.onlab.packet.PacketUtils.checkInput;
23
24/**
25 * Representation of a VXLAN(Virtual eXtensible Local Area Network) packet.
26 */
27public class VXLAN extends BasePacket {
28
29 private static final short VXLAN_HEADER_LENGTH = 8;
30 private static final int BYTE_SHIFT = 8;
31 private static final int BYTE_MASK = 0xff;
32
33 protected byte flags = 0;
34 protected byte[] rsvd1 = new byte[] {0, 0, 0}; // reserved filed
35 protected byte[] vni = new byte[] {0, 0, 0};
36 protected byte rsvd2 = 0; // reserved field
37
38 /**
39 * Serializes the packet.
40 */
41 @Override
42 public byte[] serialize() {
43
44 byte[] payloadData = null;
45 if (this.payload != null) {
46 this.payload.setParent(this);
47 payloadData = this.payload.serialize();
48 }
49
50 int length = VXLAN_HEADER_LENGTH + (payloadData == null ? 0 : payloadData.length);
51
52 final byte[] data = new byte[length];
53 final ByteBuffer bb = ByteBuffer.wrap(data);
54 bb.put(this.flags);
55 bb.put(this.rsvd1);
56 bb.put(this.vni);
57 bb.put(this.rsvd2);
58 if (payloadData != null) {
59 bb.put(payloadData);
60 }
61
62 return data;
63 }
64
jaegonkim1179d832017-06-13 19:30:55 +090065 /**
66 * Returns VNI(VXLAN Network Identifier).
67 *
68 * @return the VNI
69 */
70 public int getVni() {
71 return (vni[0] << (BYTE_SHIFT * 2)) + (vni[1] << BYTE_SHIFT) + vni[2];
72 }
73
74 /**
75 * Set VNI.
76 *
77 * @param vni the VNI to set( 24 bits )
78 * @return this
79 */
80 public VXLAN setVni(int vni) {
81 this.vni[0] = (byte) ((vni >> (BYTE_SHIFT * 2)) & BYTE_MASK);
82 this.vni[1] = (byte) ((vni >> BYTE_SHIFT) & BYTE_MASK);
83 this.vni[2] = (byte) (vni & BYTE_MASK);
84 return this;
85 }
86
87 /**
88 * Return flags.
89 *
90 * @return the flags
91 */
92 public byte getFlag() {
93 return this.flags;
94 }
95
96 /**
97 * Set flags.
98 *
99 * @param flags the flags to set( 8 bits )
100 * @return this
101 */
102 public VXLAN setFlag(byte flags) {
103 this.flags = flags;
104 return this;
105 }
106
107 @Override
108 public int hashCode() {
109 final int prime = 2521;
110 int result = super.hashCode();
111 result = prime * result + this.getVni();
112 return result;
113 }
114
115 @Override
116 public boolean equals(final Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (!super.equals(obj)) {
121 return false;
122 }
123 if (!(obj instanceof VXLAN)) {
124 return false;
125 }
126
127 final VXLAN other = (VXLAN) obj;
128 if (this.getVni() != other.getVni()) {
129 return false;
130 }
131 return true;
132 }
133
134
135 /**
136 * Returns the deserializer closure (used by upper layer deserializer).
137 *
138 * @return the deserializer closure
139 */
140 public static Deserializer<VXLAN> deserializer() {
141 return (data, offset, length) -> {
142 checkInput(data, offset, length, VXLAN_HEADER_LENGTH);
143
144 VXLAN vxlan = new VXLAN();
145
146 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
147 vxlan.flags = bb.get();
148 bb.get(vxlan.rsvd1);
149 bb.get(vxlan.vni);
150 vxlan.rsvd2 = bb.get();
151
152 Deserializer<? extends IPacket> deserializer = Data.deserializer();
153
154 vxlan.payload = deserializer.deserialize(data, bb.position(),
155 bb.limit() - bb.position());
156 vxlan.payload.setParent(vxlan);
157
158 return vxlan;
159 };
160 }
161
162 @Override
163 public String toString() {
164 return toStringHelper(getClass())
165 .add("vni", Integer.toString(getVni()))
166 .add("flags", Byte.toString(getFlag()))
167 .toString();
168 }
169
170}