Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | package org.onlab.packet; |
| 17 | |
| 18 | /** |
| 19 | * Represents the DiffServ classes defined by the IPv4/IPv6 DSCP value. |
| 20 | * DSCP occupies the 6 most-significant bits of the IPv4/IPv6 DS field |
| 21 | */ |
| 22 | public enum DscpClass { |
| 23 | |
| 24 | BE((short) 0b000000), |
| 25 | AF11((short) 0b001010), |
| 26 | AF12((short) 0b001100), |
| 27 | AF13((short) 0b001110), |
| 28 | AF21((short) 0b010010), |
| 29 | AF22((short) 0b010100), |
| 30 | AF23((short) 0b010110), |
| 31 | AF31((short) 0b011010), |
| 32 | AF32((short) 0b011100), |
| 33 | AF33((short) 0b011110), |
| 34 | AF41((short) 0b100010), |
| 35 | AF42((short) 0b100100), |
| 36 | AF43((short) 0b100110), |
| 37 | CS1((short) 0b001000), |
| 38 | CS2((short) 0b010000), |
| 39 | CS3((short) 0b011000), |
| 40 | CS4((short) 0b100000), |
| 41 | CS5((short) 0b101000), |
| 42 | CS6((short) 0b110000), |
| 43 | CS7((short) 0b111000), |
| 44 | EF((short) 0b101110); |
| 45 | |
| 46 | private static final short IP_PREC_MASK = 0b111000; |
| 47 | private static final short IP_PREC_RSHIFT = 3; |
| 48 | private static final short DROP_PREC_MASK = 0b000110; |
| 49 | private static final short DROP_PREC_RSHIFT = 1; |
| 50 | |
| 51 | private short value; |
| 52 | |
| 53 | DscpClass(short value) { |
| 54 | this.value = value; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the DSCP class Enum corresponding to the specified short. |
| 59 | * |
| 60 | * @param value the short value of the DSCP class |
| 61 | * @return the DSCP class Enum corresponding to the specified short |
| 62 | * @throws IllegalArgumentException if the short provided does not |
| 63 | * correspond to an DSCP class Enum value |
| 64 | */ |
| 65 | public static DscpClass fromShort(short value) { |
| 66 | for (DscpClass b : DscpClass.values()) { |
| 67 | if (value == b.value) { |
| 68 | return b; |
| 69 | } |
| 70 | } |
| 71 | throw new IllegalArgumentException("DSCP class " + value + " is not valid"); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the short value of this DSCP class Enum. |
| 76 | * |
| 77 | * @return the short value of this DSCP class Enum |
| 78 | */ |
| 79 | public short getValue() { |
| 80 | return value; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the corresponding IP precedence. |
| 85 | * |
| 86 | * @return the corresponding IP precedence |
| 87 | */ |
| 88 | public IPPrecedence getIPPrecedence() { |
| 89 | return IPPrecedence.fromShort((short) ((value & IP_PREC_MASK) >> IP_PREC_RSHIFT)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the corresponding drop precedence. |
| 94 | * |
| 95 | * @return the corresponding drop precedence |
| 96 | */ |
| 97 | public short getDropPrecedence() { |
| 98 | return (short) ((value & DROP_PREC_MASK) >> DROP_PREC_RSHIFT); |
| 99 | } |
| 100 | } |