blob: 9edbdb88ecc534ba644c81d15b894483be17f9cb [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
alshabib7410fea2014-09-16 13:48:39 -070017
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070018import org.onlab.packet.IpAddress;
lishuai3cce60b2015-12-01 19:35:16 +080019import org.onlab.packet.MacAddress;
alshabib7410fea2014-09-16 13:48:39 -070020
Jonathan Hartc7840bd2016-01-21 23:26:29 -080021import java.util.Objects;
22
alshabib7410fea2014-09-16 13:48:39 -070023/**
24 * Abstraction of a single traffic treatment step.
alshabib7410fea2014-09-16 13:48:39 -070025 */
26public abstract class L3ModificationInstruction implements Instruction {
27
Jonathan Hartc7840bd2016-01-21 23:26:29 -080028 private static final String SEPARATOR = ":";
29
alshabib7410fea2014-09-16 13:48:39 -070030 /**
31 * Represents the type of traffic treatment.
32 */
alshabib35edb1a2014-09-16 17:44:44 -070033 public enum L3SubType {
alshabib7410fea2014-09-16 13:48:39 -070034 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080035 * IPv4 src modification.
alshabib7410fea2014-09-16 13:48:39 -070036 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080037 IPV4_SRC,
alshabib7410fea2014-09-16 13:48:39 -070038
39 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080040 * IPv4 dst modification.
alshabib7410fea2014-09-16 13:48:39 -070041 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080042 IPV4_DST,
sangho3f97a17d2015-01-29 22:56:29 -080043
44 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080045 * IPv6 src modification.
46 */
47 IPV6_SRC,
48
49 /**
50 * IPv6 dst modification.
51 */
52 IPV6_DST,
53
54 /**
55 * IPv6 flow label modification.
56 */
57 IPV6_FLABEL,
58
59 /**
60 * Decrement TTL.
sangho3f97a17d2015-01-29 22:56:29 -080061 */
62 DEC_TTL,
63
64 /**
65 * Copy TTL out.
66 */
67 TTL_OUT,
68
69 /**
70 * Copy TTL in.
71 */
lishuai3cce60b2015-12-01 19:35:16 +080072 TTL_IN,
73
74 /**
75 * ARP IP src modification.
76 */
77 ARP_SPA,
78
79 /**
80 * ARP Ether src modification.
81 */
82 ARP_SHA,
83
84 /**
souvikdas95c94223a2020-09-23 00:32:12 -050085 * ARP IP dst modification.
86 */
87 ARP_TPA,
88
89 /**
90 * ARP Ether dst modification.
91 */
92 ARP_THA,
93
94 /**
lishuai3cce60b2015-12-01 19:35:16 +080095 * Arp operation modification.
96 */
Thiago Santos4a69ef82018-08-21 21:18:05 -070097 ARP_OP,
98
99 /**
100 * IP DSCP operation modification.
101 */
102 IP_DSCP
alshabib7410fea2014-09-16 13:48:39 -0700103 }
104
105 /**
106 * Returns the subtype of the modification instruction.
107 * @return type of instruction
108 */
alshabib35edb1a2014-09-16 17:44:44 -0700109 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -0700110
111 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800112 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -0700113 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -0700114 }
115
116 /**
117 * Represents a L3 src/dst modification instruction.
118 */
119 public static final class ModIPInstruction extends L3ModificationInstruction {
120
alshabib35edb1a2014-09-16 17:44:44 -0700121 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700122 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -0700123
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800124 ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700125
alshabib7410fea2014-09-16 13:48:39 -0700126 this.subtype = subType;
127 this.ip = addr;
128 }
129
130 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700131 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700132 return this.subtype;
133 }
134
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700135 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -0700136 return this.ip;
137 }
138
alshabib99b8fdc2014-09-25 14:30:22 -0700139 @Override
140 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800141 return subtype().toString() + SEPARATOR + ip;
alshabib99b8fdc2014-09-25 14:30:22 -0700142 }
143
alshabib8ca53902014-10-07 13:11:17 -0700144 @Override
145 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800146 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -0700147 }
148
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj) {
152 return true;
153 }
154 if (obj instanceof ModIPInstruction) {
155 ModIPInstruction that = (ModIPInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700156 return Objects.equals(ip, that.ip) &&
alshabib2020b892014-10-20 15:47:08 -0700157 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700158 }
159 return false;
160 }
sangho3f97a17d2015-01-29 22:56:29 -0800161 }
alshabib8ca53902014-10-07 13:11:17 -0700162
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800163 /**
lishuai3cce60b2015-12-01 19:35:16 +0800164 * Represents a L3 ARP IP src/dst modification instruction.
165 */
166 public static final class ModArpIPInstruction extends L3ModificationInstruction {
167
168 private final L3SubType subtype;
169 private final IpAddress ip;
170
171 ModArpIPInstruction(L3SubType subType, IpAddress addr) {
172
173 this.subtype = subType;
174 this.ip = addr;
175 }
176
177 @Override
178 public L3SubType subtype() {
179 return this.subtype;
180 }
181
182 public IpAddress ip() {
183 return this.ip;
184 }
185
186 @Override
187 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800188 return subtype().toString() + SEPARATOR + ip;
lishuai3cce60b2015-12-01 19:35:16 +0800189 }
190
191 @Override
192 public int hashCode() {
193 return Objects.hash(type(), subtype(), ip);
194 }
195
196 @Override
197 public boolean equals(Object obj) {
198 if (this == obj) {
199 return true;
200 }
201 if (obj instanceof ModArpIPInstruction) {
202 ModArpIPInstruction that = (ModArpIPInstruction) obj;
203 return Objects.equals(ip, that.ip) &&
204 Objects.equals(this.subtype(), that.subtype());
205 }
206 return false;
207 }
208 }
209
210 /**
211 * Represents a L3 ARP Ether src/dst modification instruction.
212 */
213 public static final class ModArpEthInstruction extends L3ModificationInstruction {
214
215 private final L3SubType subtype;
216 private final MacAddress mac;
217
218 ModArpEthInstruction(L3SubType subType, MacAddress addr) {
219
220 this.subtype = subType;
221 this.mac = addr;
222 }
223
224 @Override
225 public L3SubType subtype() {
226 return this.subtype;
227 }
228
229 public MacAddress mac() {
230 return this.mac;
231 }
232
233 @Override
234 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800235 return subtype().toString() + SEPARATOR + mac;
lishuai3cce60b2015-12-01 19:35:16 +0800236 }
237
238 @Override
239 public int hashCode() {
240 return Objects.hash(type(), subtype(), mac);
241 }
242
243 @Override
244 public boolean equals(Object obj) {
245 if (this == obj) {
246 return true;
247 }
248 if (obj instanceof ModArpEthInstruction) {
249 ModArpEthInstruction that = (ModArpEthInstruction) obj;
250 return Objects.equals(mac, that.mac) &&
251 Objects.equals(this.subtype(), that.subtype());
252 }
253 return false;
254 }
255 }
256
257 /**
258 * Represents a L3 ARP operation modification instruction.
259 */
260 public static final class ModArpOpInstruction extends L3ModificationInstruction {
261
262 private final L3SubType subtype;
263 private final short op;
264
265 ModArpOpInstruction(L3SubType subType, short op) {
266
267 this.subtype = subType;
268 this.op = op;
269 }
270
271 @Override
272 public L3SubType subtype() {
273 return this.subtype;
274 }
275
276 public long op() {
277 return this.op;
278 }
279
280 @Override
281 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800282 return subtype().toString() + SEPARATOR + op;
lishuai3cce60b2015-12-01 19:35:16 +0800283 }
284
285 @Override
286 public int hashCode() {
287 return Objects.hash(type(), subtype(), op);
288 }
289
290 @Override
291 public boolean equals(Object obj) {
292 if (this == obj) {
293 return true;
294 }
295 if (obj instanceof ModArpOpInstruction) {
296 ModArpOpInstruction that = (ModArpOpInstruction) obj;
297 return Objects.equals(op, that.op) &&
298 Objects.equals(this.subtype(), that.subtype());
299 }
300 return false;
301 }
302 }
303
304 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800305 * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction
306 * (20 bits unsigned integer).
307 */
308 public static final class ModIPv6FlowLabelInstruction
309 extends L3ModificationInstruction {
310 private static final int MASK = 0xfffff;
311 private final int flowLabel; // IPv6 flow label: 20 bits
312
313 /**
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800314 * Creates a new flow mod instruction.
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800315 *
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800316 * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800317 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800318 ModIPv6FlowLabelInstruction(int flowLabel) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800319 this.flowLabel = flowLabel & MASK;
320 }
321
322 @Override
323 public L3SubType subtype() {
324 return L3SubType.IPV6_FLABEL;
325 }
326
327 /**
328 * Gets the IPv6 flow label to set in the treatment.
329 *
330 * @return the IPv6 flow label to set in the treatment (20 bits)
331 */
332 public int flowLabel() {
333 return this.flowLabel;
334 }
335
336 @Override
337 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800338 return subtype().toString() + SEPARATOR + Long.toHexString(flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800339 }
340
341 @Override
342 public int hashCode() {
343 return Objects.hash(type(), subtype(), flowLabel);
344 }
345
346 @Override
347 public boolean equals(Object obj) {
348 if (this == obj) {
349 return true;
350 }
351 if (obj instanceof ModIPv6FlowLabelInstruction) {
352 ModIPv6FlowLabelInstruction that =
353 (ModIPv6FlowLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800354 return Objects.equals(flowLabel, that.flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800355 }
356 return false;
357 }
358 }
359
360 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800361 * Represents a L3 TTL modification instruction.
362 */
sangho3f97a17d2015-01-29 22:56:29 -0800363 public static final class ModTtlInstruction extends L3ModificationInstruction {
364
365 private final L3SubType subtype;
366
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800367 ModTtlInstruction(L3SubType subtype) {
sangho3f97a17d2015-01-29 22:56:29 -0800368 this.subtype = subtype;
369 }
370
371 @Override
372 public L3SubType subtype() {
373 return this.subtype;
374 }
375
376 @Override
377 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800378 return subtype().toString();
sangho3f97a17d2015-01-29 22:56:29 -0800379 }
380
381 @Override
382 public int hashCode() {
383 return Objects.hash(type(), subtype());
384 }
385
386 @Override
387 public boolean equals(Object obj) {
388 if (this == obj) {
389 return true;
390 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800391 if (obj instanceof ModTtlInstruction) {
392 ModTtlInstruction that = (ModTtlInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800393 return Objects.equals(this.subtype(), that.subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800394 }
395 return false;
396 }
alshabib7410fea2014-09-16 13:48:39 -0700397 }
Thiago Santos4a69ef82018-08-21 21:18:05 -0700398
399 /**
400 * Represents a L3 DSCP modification instruction.
401 */
402 public static final class ModDscpInstruction extends L3ModificationInstruction {
403
404 private final L3SubType subtype;
405 private final byte dscp;
406
407 ModDscpInstruction(L3SubType subtype, byte dscp) {
408 this.subtype = subtype;
409 this.dscp = dscp;
410 }
411
412 @Override
413 public L3SubType subtype() {
414 return this.subtype;
415 }
416
417 @Override
418 public String toString() {
419 return subtype().toString() + SEPARATOR + dscp();
420 }
421
422 @Override
423 public int hashCode() {
424 return Objects.hash(type(), subtype(), dscp());
425 }
426
427 public byte dscp() {
428 return this.dscp;
429 }
430
431 @Override
432 public boolean equals(Object obj) {
433 if (this == obj) {
434 return true;
435 }
436 if (obj instanceof ModDscpInstruction) {
437 ModDscpInstruction that = (ModDscpInstruction) obj;
438 return Objects.equals(this.subtype(), that.subtype()) &&
439 Objects.equals(this.dscp(), that.dscp());
440 }
441 return false;
442 }
443 }
alshabib7410fea2014-09-16 13:48:39 -0700444}