blob: 3affb9a0661a8e2ae71a8400a6941ad6836aa695 [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()),
Gustavo Silva2bcc8052021-01-22 13:48:30 -030041 PPPoED(0x8863, "pppoed", org.onlab.packet.PPPoED.deserializer()),
Charles Chan64c2dfd2018-07-24 11:58:08 -070042 SLOW(0x8809, "slow", org.onlab.packet.Slow.deserializer()),
Jonathan Hart4a60bb32015-06-30 15:31:20 -070043 UNKNOWN(0, "unknown", null);
alshabib7b808c52015-06-26 14:22:24 -070044
45
46 private final EthType etherType;
47 private final String type;
48 private final Deserializer<?> deserializer;
49
50 /**
Jonathan Hart3e594642015-10-20 17:31:24 -070051 * Constructs a new ethertype.
alshabib7b808c52015-06-26 14:22:24 -070052 *
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;
alshabibcaf1ca22015-06-25 15:18:16 -070061 }
alshabib7b808c52015-06-26 14:22:24 -070062
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 Hart4a60bb32015-06-30 15:31:20 -070076 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
alshabibcaf1ca22015-06-25 15:18:16 -070085 }
86
alshabib7b808c52015-06-26 14:22:24 -070087
88 private final short etherType;
89
90 /**
91 * Builds the EthType.
92 *
93 * @param etherType an integer representing an ethtype
94 */
alshabibcaf1ca22015-06-25 15:18:16 -070095 public EthType(int etherType) {
96 this.etherType = (short) (etherType & 0xFFFF);
97 }
98
alshabib7b808c52015-06-26 14:22:24 -070099 /**
100 * Builds the EthType.
101 *
102 * @param etherType a short representing the ethtype
103 */
alshabibcaf1ca22015-06-25 15:18:16 -0700104 public EthType(short etherType) {
105 this.etherType = etherType;
106 }
107
alshabib7b808c52015-06-26 14:22:24 -0700108 /**
109 * Returns the short value for this ethtype.
110 *
111 * @return a short value
112 */
alshabibcaf1ca22015-06-25 15:18:16 -0700113 public short toShort() {
114 return etherType;
115 }
116
alshabib7b808c52015-06-26 14:22:24 -0700117 /**
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;
alshabibcaf1ca22015-06-25 15:18:16 -0700131 }
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);
alshabib7b808c52015-06-26 14:22:24 -0700158 return (ethType == null ? String.format("0x%04x", etherType) :
159 ethType.toString());
alshabibcaf1ca22015-06-25 15:18:16 -0700160 }
161
alshabibcaf1ca22015-06-25 15:18:16 -0700162}