Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.instructions; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 19 | import org.onlab.packet.MacAddress; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 20 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Abstraction of a single traffic treatment step. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 25 | */ |
| 26 | public abstract class L3ModificationInstruction implements Instruction { |
| 27 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 28 | private static final String SEPARATOR = ":"; |
| 29 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Represents the type of traffic treatment. |
| 32 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 33 | public enum L3SubType { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 34 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 35 | * IPv4 src modification. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 36 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 37 | IPV4_SRC, |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 40 | * IPv4 dst modification. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 41 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 42 | IPV4_DST, |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 45 | * 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. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 61 | */ |
| 62 | DEC_TTL, |
| 63 | |
| 64 | /** |
| 65 | * Copy TTL out. |
| 66 | */ |
| 67 | TTL_OUT, |
| 68 | |
| 69 | /** |
| 70 | * Copy TTL in. |
| 71 | */ |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 72 | 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 | /** |
souvikdas95 | c94223a | 2020-09-23 00:32:12 -0500 | [diff] [blame] | 85 | * ARP IP dst modification. |
| 86 | */ |
| 87 | ARP_TPA, |
| 88 | |
| 89 | /** |
| 90 | * ARP Ether dst modification. |
| 91 | */ |
| 92 | ARP_THA, |
| 93 | |
| 94 | /** |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 95 | * Arp operation modification. |
| 96 | */ |
Thiago Santos | 4a69ef8 | 2018-08-21 21:18:05 -0700 | [diff] [blame] | 97 | ARP_OP, |
| 98 | |
| 99 | /** |
| 100 | * IP DSCP operation modification. |
| 101 | */ |
| 102 | IP_DSCP |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the subtype of the modification instruction. |
| 107 | * @return type of instruction |
| 108 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 109 | public abstract L3SubType subtype(); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 110 | |
| 111 | @Override |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 112 | public final Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 113 | return Type.L3MODIFICATION; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Represents a L3 src/dst modification instruction. |
| 118 | */ |
| 119 | public static final class ModIPInstruction extends L3ModificationInstruction { |
| 120 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 121 | private final L3SubType subtype; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 122 | private final IpAddress ip; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 123 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 124 | ModIPInstruction(L3SubType subType, IpAddress addr) { |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 125 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 126 | this.subtype = subType; |
| 127 | this.ip = addr; |
| 128 | } |
| 129 | |
| 130 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 131 | public L3SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 132 | return this.subtype; |
| 133 | } |
| 134 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 135 | public IpAddress ip() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 136 | return this.ip; |
| 137 | } |
| 138 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 141 | return subtype().toString() + SEPARATOR + ip; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 146 | return Objects.hash(type(), subtype(), ip); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 147 | } |
| 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; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 156 | return Objects.equals(ip, that.ip) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 157 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 158 | } |
| 159 | return false; |
| 160 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 161 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 162 | |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 163 | /** |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 164 | * 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 Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 188 | return subtype().toString() + SEPARATOR + ip; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 189 | } |
| 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 Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 235 | return subtype().toString() + SEPARATOR + mac; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 236 | } |
| 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 Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 282 | return subtype().toString() + SEPARATOR + op; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 283 | } |
| 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 Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 305 | * 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 Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 314 | * Creates a new flow mod instruction. |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 315 | * |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 316 | * @param flowLabel the IPv6 flow label to set in the treatment (20 bits) |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 317 | */ |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 318 | ModIPv6FlowLabelInstruction(int flowLabel) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 319 | 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 Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 338 | return subtype().toString() + SEPARATOR + Long.toHexString(flowLabel); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 339 | } |
| 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 HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 354 | return Objects.equals(flowLabel, that.flowLabel); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 355 | } |
| 356 | return false; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 361 | * Represents a L3 TTL modification instruction. |
| 362 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 363 | public static final class ModTtlInstruction extends L3ModificationInstruction { |
| 364 | |
| 365 | private final L3SubType subtype; |
| 366 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 367 | ModTtlInstruction(L3SubType subtype) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 368 | this.subtype = subtype; |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public L3SubType subtype() { |
| 373 | return this.subtype; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 378 | return subtype().toString(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 379 | } |
| 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 HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 391 | if (obj instanceof ModTtlInstruction) { |
| 392 | ModTtlInstruction that = (ModTtlInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 393 | return Objects.equals(this.subtype(), that.subtype()); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 394 | } |
| 395 | return false; |
| 396 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 397 | } |
Thiago Santos | 4a69ef8 | 2018-08-21 21:18:05 -0700 | [diff] [blame] | 398 | |
| 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 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 444 | } |