blob: d2dfec0f78a916a8850e9340d7ffc699110fffdf [file] [log] [blame]
alshabibcaf1ca22015-06-25 15:18:16 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabibcaf1ca22015-06-25 15:18:16 -07003 *
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 */
16package org.onlab.packet;
17
alshabibcaf1ca22015-06-25 15:18:16 -070018/**
19 * Representation of an Ethertype.
20 */
21public class EthType {
22
alshabib7b808c52015-06-26 14:22:24 -070023 /**
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.
alshabibcaf1ca22015-06-25 15:18:16 -070027 */
alshabib7b808c52015-06-26 14:22:24 -070028 public enum EtherType {
alshabibcaf1ca22015-06-25 15:18:16 -070029
alshabib7b808c52015-06-26 14:22:24 -070030 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 Kanonakis9215ff22016-11-04 13:28:11 -050036 QINQ(0x88a8, "qinq", null),
alshabib7b808c52015-06-26 14:22:24 -070037 BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()),
38 MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
Jonathan Hart4a60bb32015-06-30 15:31:20 -070039 MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
40 EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()),
41 UNKNOWN(0, "unknown", null);
alshabib7b808c52015-06-26 14:22:24 -070042
43
44 private final EthType etherType;
45 private final String type;
46 private final Deserializer<?> deserializer;
47
48 /**
Jonathan Hart3e594642015-10-20 17:31:24 -070049 * Constructs a new ethertype.
alshabib7b808c52015-06-26 14:22:24 -070050 *
51 * @param ethType The actual ethertype
52 * @param type it's textual representation
53 * @param deserializer a parser for this ethertype
54 */
55 EtherType(int ethType, String type, Deserializer<?> deserializer) {
56 this.etherType = new EthType(ethType);
57 this.type = type;
58 this.deserializer = deserializer;
alshabibcaf1ca22015-06-25 15:18:16 -070059 }
alshabib7b808c52015-06-26 14:22:24 -070060
61 public EthType ethType() {
62 return etherType;
63 }
64
65 @Override
66 public String toString() {
67 return type;
68 }
69
70 public Deserializer<?> deserializer() {
71 return deserializer;
72 }
73
Jonathan Hart4a60bb32015-06-30 15:31:20 -070074 public static EtherType lookup(short etherType) {
75 for (EtherType ethType : EtherType.values()) {
76 if (ethType.ethType().toShort() == etherType) {
77 return ethType;
78 }
79 }
80 return UNKNOWN;
81 }
82
alshabibcaf1ca22015-06-25 15:18:16 -070083 }
84
alshabib7b808c52015-06-26 14:22:24 -070085
86 private final short etherType;
87
88 /**
89 * Builds the EthType.
90 *
91 * @param etherType an integer representing an ethtype
92 */
alshabibcaf1ca22015-06-25 15:18:16 -070093 public EthType(int etherType) {
94 this.etherType = (short) (etherType & 0xFFFF);
95 }
96
alshabib7b808c52015-06-26 14:22:24 -070097 /**
98 * Builds the EthType.
99 *
100 * @param etherType a short representing the ethtype
101 */
alshabibcaf1ca22015-06-25 15:18:16 -0700102 public EthType(short etherType) {
103 this.etherType = etherType;
104 }
105
alshabib7b808c52015-06-26 14:22:24 -0700106 /**
107 * Returns the short value for this ethtype.
108 *
109 * @return a short value
110 */
alshabibcaf1ca22015-06-25 15:18:16 -0700111 public short toShort() {
112 return etherType;
113 }
114
alshabib7b808c52015-06-26 14:22:24 -0700115 /**
116 * Looks up the ethertype by it's numerical representation
117 * and returns it's textual format.
118 *
119 * @param etherType the short value of the ethertype
120 * @return a textual representation
121 */
122 public EtherType lookup(short etherType) {
123 for (EtherType ethType : EtherType.values()) {
124 if (ethType.ethType().toShort() == etherType) {
125 return ethType;
126 }
127 }
128 return null;
alshabibcaf1ca22015-06-25 15:18:16 -0700129 }
130
131 @Override
132 public boolean equals(Object o) {
133 if (this == o) {
134 return true;
135 }
136 if (o == null || getClass() != o.getClass()) {
137 return false;
138 }
139
140 EthType ethType = (EthType) o;
141
142 if (etherType != ethType.etherType) {
143 return false;
144 }
145
146 return true;
147 }
148
149 @Override
150 public int hashCode() {
151 return (int) etherType;
152 }
153
154 public String toString() {
155 EtherType ethType = lookup(this.etherType);
alshabib7b808c52015-06-26 14:22:24 -0700156 return (ethType == null ? String.format("0x%04x", etherType) :
157 ethType.toString());
alshabibcaf1ca22015-06-25 15:18:16 -0700158 }
159
alshabibcaf1ca22015-06-25 15:18:16 -0700160}