blob: c69ed145ec5b94142ad8dad0985fdb7dda44bd92 [file] [log] [blame]
alshabibcaf1ca22015-06-25 15:18:16 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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()),
Yi Tseng603d0552017-04-14 14:06:44 -070039 MPLS_MULTICAST(0x8848, "mpls_multicast", org.onlab.packet.MPLS.deserializer()),
Jonathan Hart4a60bb32015-06-30 15:31:20 -070040 EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()),
Charles Chan64c2dfd2018-07-24 11:58:08 -070041 SLOW(0x8809, "slow", org.onlab.packet.Slow.deserializer()),
Jonathan Hart4a60bb32015-06-30 15:31:20 -070042 UNKNOWN(0, "unknown", null);
alshabib7b808c52015-06-26 14:22:24 -070043
44
45 private final EthType etherType;
46 private final String type;
47 private final Deserializer<?> deserializer;
48
49 /**
Jonathan Hart3e594642015-10-20 17:31:24 -070050 * Constructs a new ethertype.
alshabib7b808c52015-06-26 14:22:24 -070051 *
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;
alshabibcaf1ca22015-06-25 15:18:16 -070060 }
alshabib7b808c52015-06-26 14:22:24 -070061
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 Hart4a60bb32015-06-30 15:31:20 -070075 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
alshabibcaf1ca22015-06-25 15:18:16 -070084 }
85
alshabib7b808c52015-06-26 14:22:24 -070086
87 private final short etherType;
88
89 /**
90 * Builds the EthType.
91 *
92 * @param etherType an integer representing an ethtype
93 */
alshabibcaf1ca22015-06-25 15:18:16 -070094 public EthType(int etherType) {
95 this.etherType = (short) (etherType & 0xFFFF);
96 }
97
alshabib7b808c52015-06-26 14:22:24 -070098 /**
99 * Builds the EthType.
100 *
101 * @param etherType a short representing the ethtype
102 */
alshabibcaf1ca22015-06-25 15:18:16 -0700103 public EthType(short etherType) {
104 this.etherType = etherType;
105 }
106
alshabib7b808c52015-06-26 14:22:24 -0700107 /**
108 * Returns the short value for this ethtype.
109 *
110 * @return a short value
111 */
alshabibcaf1ca22015-06-25 15:18:16 -0700112 public short toShort() {
113 return etherType;
114 }
115
alshabib7b808c52015-06-26 14:22:24 -0700116 /**
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;
alshabibcaf1ca22015-06-25 15:18:16 -0700130 }
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);
alshabib7b808c52015-06-26 14:22:24 -0700157 return (ethType == null ? String.format("0x%04x", etherType) :
158 ethType.toString());
alshabibcaf1ca22015-06-25 15:18:16 -0700159 }
160
alshabibcaf1ca22015-06-25 15:18:16 -0700161}