alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 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 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 18 | /** |
| 19 | * Representation of an Ethertype. |
| 20 | */ |
| 21 | public class EthType { |
| 22 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 23 | /** |
| 24 | * A list of known ethertypes. Adding a fully defined enum here will |
| 25 | * associated the ethertype with a textual representation and a parsing |
| 26 | * class. |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 27 | */ |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 28 | public enum EtherType { |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 29 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 30 | ARP(0x806, "arp", org.onlab.packet.ARP.deserializer()), |
| 31 | RARP(0x8035, "rarp", org.onlab.packet.ARP.deserializer()), |
| 32 | IPV4(0x800, "ipv4", org.onlab.packet.IPv4.deserializer()), |
| 33 | IPV6(0x86dd, "ipv6", org.onlab.packet.IPv6.deserializer()), |
| 34 | LLDP(0x88cc, "lldp", org.onlab.packet.LLDP.deserializer()), |
| 35 | VLAN(0x8100, "vlan", null), |
Konstantinos Kanonakis | 9215ff2 | 2016-11-04 13:28:11 -0500 | [diff] [blame] | 36 | QINQ(0x88a8, "qinq", null), |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 37 | BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()), |
| 38 | MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()), |
Yi Tseng | 603d055 | 2017-04-14 14:06:44 -0700 | [diff] [blame] | 39 | MPLS_MULTICAST(0x8848, "mpls_multicast", org.onlab.packet.MPLS.deserializer()), |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 40 | EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()), |
Charles Chan | 64c2dfd | 2018-07-24 11:58:08 -0700 | [diff] [blame] | 41 | SLOW(0x8809, "slow", org.onlab.packet.Slow.deserializer()), |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 42 | UNKNOWN(0, "unknown", null); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | private final EthType etherType; |
| 46 | private final String type; |
| 47 | private final Deserializer<?> deserializer; |
| 48 | |
| 49 | /** |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 50 | * Constructs a new ethertype. |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 51 | * |
| 52 | * @param ethType The actual ethertype |
| 53 | * @param type it's textual representation |
| 54 | * @param deserializer a parser for this ethertype |
| 55 | */ |
| 56 | EtherType(int ethType, String type, Deserializer<?> deserializer) { |
| 57 | this.etherType = new EthType(ethType); |
| 58 | this.type = type; |
| 59 | this.deserializer = deserializer; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 60 | } |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 61 | |
| 62 | public EthType ethType() { |
| 63 | return etherType; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public String toString() { |
| 68 | return type; |
| 69 | } |
| 70 | |
| 71 | public Deserializer<?> deserializer() { |
| 72 | return deserializer; |
| 73 | } |
| 74 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 75 | public static EtherType lookup(short etherType) { |
| 76 | for (EtherType ethType : EtherType.values()) { |
| 77 | if (ethType.ethType().toShort() == etherType) { |
| 78 | return ethType; |
| 79 | } |
| 80 | } |
| 81 | return UNKNOWN; |
| 82 | } |
| 83 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 84 | } |
| 85 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 86 | |
| 87 | private final short etherType; |
| 88 | |
| 89 | /** |
| 90 | * Builds the EthType. |
| 91 | * |
| 92 | * @param etherType an integer representing an ethtype |
| 93 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 94 | public EthType(int etherType) { |
| 95 | this.etherType = (short) (etherType & 0xFFFF); |
| 96 | } |
| 97 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Builds the EthType. |
| 100 | * |
| 101 | * @param etherType a short representing the ethtype |
| 102 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 103 | public EthType(short etherType) { |
| 104 | this.etherType = etherType; |
| 105 | } |
| 106 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Returns the short value for this ethtype. |
| 109 | * |
| 110 | * @return a short value |
| 111 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 112 | public short toShort() { |
| 113 | return etherType; |
| 114 | } |
| 115 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Looks up the ethertype by it's numerical representation |
| 118 | * and returns it's textual format. |
| 119 | * |
| 120 | * @param etherType the short value of the ethertype |
| 121 | * @return a textual representation |
| 122 | */ |
| 123 | public EtherType lookup(short etherType) { |
| 124 | for (EtherType ethType : EtherType.values()) { |
| 125 | if (ethType.ethType().toShort() == etherType) { |
| 126 | return ethType; |
| 127 | } |
| 128 | } |
| 129 | return null; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object o) { |
| 134 | if (this == o) { |
| 135 | return true; |
| 136 | } |
| 137 | if (o == null || getClass() != o.getClass()) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | EthType ethType = (EthType) o; |
| 142 | |
| 143 | if (etherType != ethType.etherType) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return (int) etherType; |
| 153 | } |
| 154 | |
| 155 | public String toString() { |
| 156 | EtherType ethType = lookup(this.etherType); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 157 | return (ethType == null ? String.format("0x%04x", etherType) : |
| 158 | ethType.toString()); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 161 | } |