jaegonkim | 1179d83 | 2017-06-13 19:30:55 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
jaegonkim | 1179d83 | 2017-06-13 19:30:55 +0900 | [diff] [blame] | 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 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
| 20 | |
| 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 22 | import static org.onlab.packet.PacketUtils.checkInput; |
| 23 | |
| 24 | /** |
| 25 | * Representation of a VXLAN(Virtual eXtensible Local Area Network) packet. |
| 26 | */ |
| 27 | public 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 | |
| 65 | @Override |
| 66 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 67 | |
| 68 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 69 | |
| 70 | if (bb.remaining() < VXLAN_HEADER_LENGTH) { |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | this.flags = bb.get(); |
| 75 | bb.get(this.rsvd1); |
| 76 | bb.get(this.vni); |
| 77 | this.rsvd2 = bb.get(); |
| 78 | |
| 79 | Deserializer<? extends IPacket> deserializer = Data.deserializer(); |
| 80 | |
| 81 | try { |
| 82 | this.payload = deserializer.deserialize(data, bb.position(), |
| 83 | bb.limit() - bb.position()); |
| 84 | this.payload.setParent(this); |
| 85 | } catch (DeserializationException e) { |
| 86 | return this; |
| 87 | } |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns VNI(VXLAN Network Identifier). |
| 93 | * |
| 94 | * @return the VNI |
| 95 | */ |
| 96 | public int getVni() { |
| 97 | return (vni[0] << (BYTE_SHIFT * 2)) + (vni[1] << BYTE_SHIFT) + vni[2]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set VNI. |
| 102 | * |
| 103 | * @param vni the VNI to set( 24 bits ) |
| 104 | * @return this |
| 105 | */ |
| 106 | public VXLAN setVni(int vni) { |
| 107 | this.vni[0] = (byte) ((vni >> (BYTE_SHIFT * 2)) & BYTE_MASK); |
| 108 | this.vni[1] = (byte) ((vni >> BYTE_SHIFT) & BYTE_MASK); |
| 109 | this.vni[2] = (byte) (vni & BYTE_MASK); |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Return flags. |
| 115 | * |
| 116 | * @return the flags |
| 117 | */ |
| 118 | public byte getFlag() { |
| 119 | return this.flags; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set flags. |
| 124 | * |
| 125 | * @param flags the flags to set( 8 bits ) |
| 126 | * @return this |
| 127 | */ |
| 128 | public VXLAN setFlag(byte flags) { |
| 129 | this.flags = flags; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int hashCode() { |
| 135 | final int prime = 2521; |
| 136 | int result = super.hashCode(); |
| 137 | result = prime * result + this.getVni(); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public boolean equals(final Object obj) { |
| 143 | if (this == obj) { |
| 144 | return true; |
| 145 | } |
| 146 | if (!super.equals(obj)) { |
| 147 | return false; |
| 148 | } |
| 149 | if (!(obj instanceof VXLAN)) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | final VXLAN other = (VXLAN) obj; |
| 154 | if (this.getVni() != other.getVni()) { |
| 155 | return false; |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Returns the deserializer closure (used by upper layer deserializer). |
| 163 | * |
| 164 | * @return the deserializer closure |
| 165 | */ |
| 166 | public static Deserializer<VXLAN> deserializer() { |
| 167 | return (data, offset, length) -> { |
| 168 | checkInput(data, offset, length, VXLAN_HEADER_LENGTH); |
| 169 | |
| 170 | VXLAN vxlan = new VXLAN(); |
| 171 | |
| 172 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 173 | vxlan.flags = bb.get(); |
| 174 | bb.get(vxlan.rsvd1); |
| 175 | bb.get(vxlan.vni); |
| 176 | vxlan.rsvd2 = bb.get(); |
| 177 | |
| 178 | Deserializer<? extends IPacket> deserializer = Data.deserializer(); |
| 179 | |
| 180 | vxlan.payload = deserializer.deserialize(data, bb.position(), |
| 181 | bb.limit() - bb.position()); |
| 182 | vxlan.payload.setParent(vxlan); |
| 183 | |
| 184 | return vxlan; |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String toString() { |
| 190 | return toStringHelper(getClass()) |
| 191 | .add("vni", Integer.toString(getVni())) |
| 192 | .add("flags", Byte.toString(getFlag())) |
| 193 | .toString(); |
| 194 | } |
| 195 | |
| 196 | } |