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()), |
Gustavo Silva | 620d6e3 | 2021-01-22 13:48:30 -0300 | [diff] [blame^] | 41 | PPPoED(0x8863, "pppoed", org.onlab.packet.PPPoED.deserializer()), |
Charles Chan | 64c2dfd | 2018-07-24 11:58:08 -0700 | [diff] [blame] | 42 | SLOW(0x8809, "slow", org.onlab.packet.Slow.deserializer()), |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 43 | UNKNOWN(0, "unknown", null); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | private final EthType etherType; |
| 47 | private final String type; |
| 48 | private final Deserializer<?> deserializer; |
| 49 | |
| 50 | /** |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 51 | * Constructs a new ethertype. |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 52 | * |
| 53 | * @param ethType The actual ethertype |
| 54 | * @param type it's textual representation |
| 55 | * @param deserializer a parser for this ethertype |
| 56 | */ |
| 57 | EtherType(int ethType, String type, Deserializer<?> deserializer) { |
| 58 | this.etherType = new EthType(ethType); |
| 59 | this.type = type; |
| 60 | this.deserializer = deserializer; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 61 | } |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 62 | |
| 63 | public EthType ethType() { |
| 64 | return etherType; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | return type; |
| 70 | } |
| 71 | |
| 72 | public Deserializer<?> deserializer() { |
| 73 | return deserializer; |
| 74 | } |
| 75 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 76 | public static EtherType lookup(short etherType) { |
| 77 | for (EtherType ethType : EtherType.values()) { |
| 78 | if (ethType.ethType().toShort() == etherType) { |
| 79 | return ethType; |
| 80 | } |
| 81 | } |
| 82 | return UNKNOWN; |
| 83 | } |
| 84 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 87 | |
| 88 | private final short etherType; |
| 89 | |
| 90 | /** |
| 91 | * Builds the EthType. |
| 92 | * |
| 93 | * @param etherType an integer representing an ethtype |
| 94 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 95 | public EthType(int etherType) { |
| 96 | this.etherType = (short) (etherType & 0xFFFF); |
| 97 | } |
| 98 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Builds the EthType. |
| 101 | * |
| 102 | * @param etherType a short representing the ethtype |
| 103 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 104 | public EthType(short etherType) { |
| 105 | this.etherType = etherType; |
| 106 | } |
| 107 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Returns the short value for this ethtype. |
| 110 | * |
| 111 | * @return a short value |
| 112 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 113 | public short toShort() { |
| 114 | return etherType; |
| 115 | } |
| 116 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Looks up the ethertype by it's numerical representation |
| 119 | * and returns it's textual format. |
| 120 | * |
| 121 | * @param etherType the short value of the ethertype |
| 122 | * @return a textual representation |
| 123 | */ |
| 124 | public EtherType lookup(short etherType) { |
| 125 | for (EtherType ethType : EtherType.values()) { |
| 126 | if (ethType.ethType().toShort() == etherType) { |
| 127 | return ethType; |
| 128 | } |
| 129 | } |
| 130 | return null; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean equals(Object o) { |
| 135 | if (this == o) { |
| 136 | return true; |
| 137 | } |
| 138 | if (o == null || getClass() != o.getClass()) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | EthType ethType = (EthType) o; |
| 143 | |
| 144 | if (etherType != ethType.etherType) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public int hashCode() { |
| 153 | return (int) etherType; |
| 154 | } |
| 155 | |
| 156 | public String toString() { |
| 157 | EtherType ethType = lookup(this.etherType); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 158 | return (ethType == null ? String.format("0x%04x", etherType) : |
| 159 | ethType.toString()); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 160 | } |
| 161 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 162 | } |