blob: 47dbeed2f778a816493f9bcff141f5d303b239fe [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001package org.onlab.packet;
2
3import java.nio.ByteBuffer;
4import java.util.HashMap;
5import java.util.Map;
6
Jonathan Hart2a655752015-04-07 16:46:33 -07007import static org.onlab.packet.PacketUtils.checkInput;
8
sanghob35a6192015-04-01 13:05:26 -07009public class MPLS extends BasePacket {
Jonathan Hart2a655752015-04-07 16:46:33 -070010 public static final int HEADER_LENGTH = 4;
11
sanghob35a6192015-04-01 13:05:26 -070012 public static final byte PROTOCOL_IPV4 = 0x1;
13 public static final byte PROTOCOL_MPLS = 0x6;
Jonathan Hart2a655752015-04-07 16:46:33 -070014 static Map<Byte, Deserializer<? extends IPacket>> protocolDeserializerMap
15 = new HashMap<>();
sanghob35a6192015-04-01 13:05:26 -070016
17 static {
Jonathan Hart2a655752015-04-07 16:46:33 -070018 protocolDeserializerMap.put(PROTOCOL_IPV4, IPv4.deserializer());
19 protocolDeserializerMap.put(PROTOCOL_MPLS, MPLS.deserializer());
sanghob35a6192015-04-01 13:05:26 -070020 }
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 Hart2a655752015-04-07 16:46:33 -070065 Deserializer<? extends IPacket> deserializer;
66 if (protocolDeserializerMap.containsKey(this.protocol)) {
67 deserializer = protocolDeserializerMap.get(this.protocol);
sanghob35a6192015-04-01 13:05:26 -070068 } else {
Jonathan Hart2a655752015-04-07 16:46:33 -070069 deserializer = Data.deserializer();
sanghob35a6192015-04-01 13:05:26 -070070 }
Jonathan Hart2a655752015-04-07 16:46:33 -070071 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 }
sanghob35a6192015-04-01 13:05:26 -070077
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 Vachuskae10f56b2015-04-15 18:20:08 -070093 * @param label MPLS label
sanghob35a6192015-04-01 13:05:26 -070094 */
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 Hart2a655752015-04-07 16:46:33 -0700117 /**
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 }
sanghob35a6192015-04-01 13:05:26 -0700147}