sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | package org.onlab.packet; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Map; |
| 6 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 7 | import static org.onlab.packet.PacketUtils.checkInput; |
| 8 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 9 | public class MPLS extends BasePacket { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 10 | public static final int HEADER_LENGTH = 4; |
| 11 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 12 | public static final byte PROTOCOL_IPV4 = 0x1; |
| 13 | public static final byte PROTOCOL_MPLS = 0x6; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 14 | static Map<Byte, Deserializer<? extends IPacket>> protocolDeserializerMap |
| 15 | = new HashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 16 | |
| 17 | static { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 18 | protocolDeserializerMap.put(PROTOCOL_IPV4, IPv4.deserializer()); |
| 19 | protocolDeserializerMap.put(PROTOCOL_MPLS, MPLS.deserializer()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | protected int label; //20bits |
| 23 | protected byte bos; //1bit |
| 24 | protected byte ttl; //8bits |
| 25 | protected byte protocol; |
| 26 | |
| 27 | /** |
| 28 | * Default constructor that sets the version to 4. |
| 29 | */ |
| 30 | public MPLS() { |
| 31 | super(); |
| 32 | this.bos = 1; |
| 33 | this.protocol = PROTOCOL_IPV4; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public byte[] serialize() { |
| 38 | byte[] payloadData = null; |
| 39 | if (payload != null) { |
| 40 | payload.setParent(this); |
| 41 | payloadData = payload.serialize(); |
| 42 | } |
| 43 | |
| 44 | byte[] data = new byte[(4 + ((payloadData != null) ? payloadData.length : 0)) ]; |
| 45 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 46 | |
| 47 | bb.putInt(((this.label & 0x000fffff) << 12) | ((this.bos & 0x1) << 8 | (this.ttl & 0xff))); |
| 48 | if (payloadData != null) { |
| 49 | bb.put(payloadData); |
| 50 | } |
| 51 | |
| 52 | return data; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 57 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 58 | |
| 59 | int mplsheader = bb.getInt(); |
| 60 | this.label = ((mplsheader & 0xfffff000) >> 12); |
| 61 | this.bos = (byte) ((mplsheader & 0x00000100) >> 8); |
| 62 | this.bos = (byte) (mplsheader & 0x000000ff); |
| 63 | this.protocol = (this.bos == 1) ? PROTOCOL_IPV4 : PROTOCOL_MPLS; |
| 64 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 65 | Deserializer<? extends IPacket> deserializer; |
| 66 | if (protocolDeserializerMap.containsKey(this.protocol)) { |
| 67 | deserializer = protocolDeserializerMap.get(this.protocol); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 68 | } else { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 69 | deserializer = Data.deserializer(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 70 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 71 | try { |
| 72 | this.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 73 | this.payload.setParent(this); |
| 74 | } catch (DeserializationException e) { |
| 75 | return this; |
| 76 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 77 | |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the MPLS label. |
| 83 | * |
| 84 | * @return MPLS label |
| 85 | */ |
| 86 | public int getLabel() { |
| 87 | return label; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets the MPLS label. |
| 92 | * |
Thomas Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 93 | * @param label MPLS label |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 94 | */ |
| 95 | public void setLabel(int label) { |
| 96 | this.label = label; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the MPLS TTL of the packet. |
| 101 | * |
| 102 | * @return MPLS TTL of the packet |
| 103 | */ |
| 104 | public byte getTtl() { |
| 105 | return ttl; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets the MPLS TTL of the packet. |
| 110 | * |
| 111 | * @param ttl MPLS TTL |
| 112 | */ |
| 113 | public void setTtl(byte ttl) { |
| 114 | this.ttl = ttl; |
| 115 | } |
| 116 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Deserializer function for MPLS packets. |
| 119 | * |
| 120 | * @return deserializer function |
| 121 | */ |
| 122 | public static Deserializer<MPLS> deserializer() { |
| 123 | return (data, offset, length) -> { |
| 124 | checkInput(data, offset, length, HEADER_LENGTH); |
| 125 | |
| 126 | MPLS mpls = new MPLS(); |
| 127 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 128 | |
| 129 | int mplsheader = bb.getInt(); |
| 130 | mpls.label = ((mplsheader & 0xfffff000) >>> 12); |
| 131 | mpls.bos = (byte) ((mplsheader & 0x00000100) >> 8); |
| 132 | mpls.ttl = (byte) (mplsheader & 0x000000ff); |
| 133 | mpls.protocol = (mpls.bos == 1) ? PROTOCOL_IPV4 : PROTOCOL_MPLS; |
| 134 | |
| 135 | Deserializer<? extends IPacket> deserializer; |
| 136 | if (protocolDeserializerMap.containsKey(mpls.protocol)) { |
| 137 | deserializer = protocolDeserializerMap.get(mpls.protocol); |
| 138 | } else { |
| 139 | deserializer = Data.deserializer(); |
| 140 | } |
| 141 | mpls.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 142 | mpls.payload.setParent(mpls); |
| 143 | |
| 144 | return mpls; |
| 145 | }; |
| 146 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 147 | } |