blob: 71c5803ecd6783e0f5be3b849ff241d803ee3eb2 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 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 */
sanghob35a6192015-04-01 13:05:26 -070016package org.onlab.packet;
17
18import java.nio.ByteBuffer;
19import java.util.HashMap;
20import java.util.Map;
21
Jonathan Hart2a655752015-04-07 16:46:33 -070022import static org.onlab.packet.PacketUtils.checkInput;
23
sanghob35a6192015-04-01 13:05:26 -070024public class MPLS extends BasePacket {
Jonathan Hart2a655752015-04-07 16:46:33 -070025 public static final int HEADER_LENGTH = 4;
26
sanghob35a6192015-04-01 13:05:26 -070027 public static final byte PROTOCOL_IPV4 = 0x1;
28 public static final byte PROTOCOL_MPLS = 0x6;
Jonathan Hart2a655752015-04-07 16:46:33 -070029 static Map<Byte, Deserializer<? extends IPacket>> protocolDeserializerMap
30 = new HashMap<>();
sanghob35a6192015-04-01 13:05:26 -070031
32 static {
Jonathan Hart2a655752015-04-07 16:46:33 -070033 protocolDeserializerMap.put(PROTOCOL_IPV4, IPv4.deserializer());
34 protocolDeserializerMap.put(PROTOCOL_MPLS, MPLS.deserializer());
sanghob35a6192015-04-01 13:05:26 -070035 }
36
37 protected int label; //20bits
38 protected byte bos; //1bit
39 protected byte ttl; //8bits
40 protected byte protocol;
41
42 /**
43 * Default constructor that sets the version to 4.
44 */
45 public MPLS() {
46 super();
47 this.bos = 1;
48 this.protocol = PROTOCOL_IPV4;
49 }
50
51 @Override
52 public byte[] serialize() {
53 byte[] payloadData = null;
54 if (payload != null) {
55 payload.setParent(this);
56 payloadData = payload.serialize();
57 }
58
59 byte[] data = new byte[(4 + ((payloadData != null) ? payloadData.length : 0)) ];
60 ByteBuffer bb = ByteBuffer.wrap(data);
61
62 bb.putInt(((this.label & 0x000fffff) << 12) | ((this.bos & 0x1) << 8 | (this.ttl & 0xff)));
63 if (payloadData != null) {
64 bb.put(payloadData);
65 }
66
67 return data;
68 }
69
70 @Override
71 public IPacket deserialize(byte[] data, int offset, int length) {
72 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
73
74 int mplsheader = bb.getInt();
75 this.label = ((mplsheader & 0xfffff000) >> 12);
76 this.bos = (byte) ((mplsheader & 0x00000100) >> 8);
77 this.bos = (byte) (mplsheader & 0x000000ff);
78 this.protocol = (this.bos == 1) ? PROTOCOL_IPV4 : PROTOCOL_MPLS;
79
Jonathan Hart2a655752015-04-07 16:46:33 -070080 Deserializer<? extends IPacket> deserializer;
81 if (protocolDeserializerMap.containsKey(this.protocol)) {
82 deserializer = protocolDeserializerMap.get(this.protocol);
sanghob35a6192015-04-01 13:05:26 -070083 } else {
Jonathan Hart2a655752015-04-07 16:46:33 -070084 deserializer = Data.deserializer();
sanghob35a6192015-04-01 13:05:26 -070085 }
Jonathan Hart2a655752015-04-07 16:46:33 -070086 try {
87 this.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position());
88 this.payload.setParent(this);
89 } catch (DeserializationException e) {
90 return this;
91 }
sanghob35a6192015-04-01 13:05:26 -070092
93 return this;
94 }
95
96 /**
97 * Returns the MPLS label.
98 *
99 * @return MPLS label
100 */
101 public int getLabel() {
102 return label;
103 }
104
105 /**
106 * Sets the MPLS label.
107 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -0700108 * @param label MPLS label
sanghob35a6192015-04-01 13:05:26 -0700109 */
110 public void setLabel(int label) {
111 this.label = label;
112 }
113
114 /**
115 * Returns the MPLS TTL of the packet.
116 *
117 * @return MPLS TTL of the packet
118 */
119 public byte getTtl() {
120 return ttl;
121 }
122
123 /**
124 * Sets the MPLS TTL of the packet.
125 *
126 * @param ttl MPLS TTL
127 */
128 public void setTtl(byte ttl) {
129 this.ttl = ttl;
130 }
131
Jonathan Hart2a655752015-04-07 16:46:33 -0700132 /**
133 * Deserializer function for MPLS packets.
134 *
135 * @return deserializer function
136 */
137 public static Deserializer<MPLS> deserializer() {
138 return (data, offset, length) -> {
139 checkInput(data, offset, length, HEADER_LENGTH);
140
141 MPLS mpls = new MPLS();
142 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
143
144 int mplsheader = bb.getInt();
145 mpls.label = ((mplsheader & 0xfffff000) >>> 12);
146 mpls.bos = (byte) ((mplsheader & 0x00000100) >> 8);
147 mpls.ttl = (byte) (mplsheader & 0x000000ff);
148 mpls.protocol = (mpls.bos == 1) ? PROTOCOL_IPV4 : PROTOCOL_MPLS;
149
150 Deserializer<? extends IPacket> deserializer;
151 if (protocolDeserializerMap.containsKey(mpls.protocol)) {
152 deserializer = protocolDeserializerMap.get(mpls.protocol);
153 } else {
154 deserializer = Data.deserializer();
155 }
156 mpls.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position());
157 mpls.payload.setParent(mpls);
158
159 return mpls;
160 };
161 }
sanghob35a6192015-04-01 13:05:26 -0700162}