Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 22 | import static org.onlab.packet.PacketUtils.checkInput; |
| 23 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | public class MPLS extends BasePacket { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 25 | public static final int HEADER_LENGTH = 4; |
| 26 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 27 | public static final byte PROTOCOL_IPV4 = 0x1; |
| 28 | public static final byte PROTOCOL_MPLS = 0x6; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 29 | static Map<Byte, Deserializer<? extends IPacket>> protocolDeserializerMap |
| 30 | = new HashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | |
| 32 | static { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 33 | protocolDeserializerMap.put(PROTOCOL_IPV4, IPv4.deserializer()); |
| 34 | protocolDeserializerMap.put(PROTOCOL_MPLS, MPLS.deserializer()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | } |
| 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 80 | Deserializer<? extends IPacket> deserializer; |
| 81 | if (protocolDeserializerMap.containsKey(this.protocol)) { |
| 82 | deserializer = protocolDeserializerMap.get(this.protocol); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 83 | } else { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 84 | deserializer = Data.deserializer(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 85 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 86 | 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 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 92 | |
| 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 Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 108 | * @param label MPLS label |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 109 | */ |
| 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 132 | /** |
| 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 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 162 | } |