Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
Jian Li | 2694976 | 2018-03-30 15:46:37 +0900 | [diff] [blame] | 16 | package org.onosproject.openstacknetworking.util; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 20 | import org.onlab.packet.TpPort; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 23 | import org.onosproject.net.behaviour.ExtensionSelectorResolver; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 25 | import org.onosproject.net.device.DeviceService; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 26 | import org.onosproject.net.driver.DriverHandler; |
| 27 | import org.onosproject.net.driver.DriverService; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 28 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
| 29 | import org.onosproject.net.flow.criteria.ExtensionSelectorType; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 30 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 31 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 38 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Provides common methods to help populating flow rules for SONA applications. |
| 43 | */ |
| 44 | public final class RulePopulatorUtil { |
| 45 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 46 | private static final Logger log = getLogger(RulePopulatorUtil.class); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 47 | |
| 48 | private static final String TUNNEL_DST = "tunnelDst"; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 49 | private static final String CT_FLAGS = "flags"; |
| 50 | private static final String CT_ZONE = "zone"; |
| 51 | private static final String CT_TABLE = "recircTable"; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 52 | private static final String CT_STATE = "ctState"; |
| 53 | private static final String CT_STATE_MASK = "ctStateMask"; |
| 54 | private static final String CT_PRESENT_FLAGS = "presentFlags"; |
| 55 | private static final String CT_IPADDRESS_MIN = "ipAddressMin"; |
| 56 | private static final String CT_IPADDRESS_MAX = "ipAddressMax"; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 57 | private static final String CT_PORT_MIN = "portMin"; |
| 58 | private static final String CT_PORT_MAX = "portMax"; |
| 59 | private static final String CT_NESTED_ACTIONS = "nestedActions"; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 60 | |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 61 | public static final int CT_NAT_SRC_FLAG = 0; |
| 62 | public static final int CT_NAT_DST_FLAG = 1; |
| 63 | public static final int CT_NAT_PERSISTENT_FLAG = 2; |
| 64 | public static final int CT_NAT_PROTO_HASH_FLAG = 3; |
| 65 | public static final int CT_NAT_PROTO_RANDOM_FLAG = 4; |
| 66 | |
| 67 | private static final int ADDRESS_V4_MIN_FLAG = 0; |
| 68 | private static final int ADDRESS_V4_MAX_FLAG = 1; |
| 69 | private static final int ADDRESS_V6_MIN_FLAG = 2; |
| 70 | private static final int ADDRESS_V6_MAX_FLAG = 3; |
| 71 | private static final int PORT_MIN_FLAG = 4; |
| 72 | private static final int PORT_MAX_FLAG = 5; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 73 | |
| 74 | // Refer to http://openvswitch.org/support/dist-docs/ovs-fields.7.txt for the values |
| 75 | public static final long CT_STATE_NONE = 0; |
| 76 | public static final long CT_STATE_NEW = 0x01; |
| 77 | public static final long CT_STATE_EST = 0x02; |
| 78 | public static final long CT_STATE_NOT_TRK = 0x20; |
| 79 | public static final long CT_STATE_TRK = 0x20; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 80 | |
| 81 | private RulePopulatorUtil() { |
| 82 | } |
| 83 | |
| 84 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 85 | * Returns a builder for OVS Connection Tracking feature actions. |
| 86 | * |
| 87 | * @param ds DriverService |
| 88 | * @param id DeviceId |
| 89 | * @return a builder for OVS Connection Tracking feature actions |
| 90 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 91 | public static NiciraConnTrackTreatmentBuilder |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 92 | niciraConnTrackTreatmentBuilder(DriverService ds, DeviceId id) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 93 | return new NiciraConnTrackTreatmentBuilder(ds, id); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 97 | * Returns tunnel destination extension treatment object. |
| 98 | * |
| 99 | * @param deviceService driver service |
| 100 | * @param deviceId device id to apply this treatment |
| 101 | * @param remoteIp tunnel destination ip address |
| 102 | * @return extension treatment |
| 103 | */ |
| 104 | public static ExtensionTreatment buildExtension(DeviceService deviceService, |
| 105 | DeviceId deviceId, |
| 106 | Ip4Address remoteIp) { |
| 107 | Device device = deviceService.getDevice(deviceId); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 108 | if (device != null && !device.is(ExtensionTreatmentResolver.class)) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 109 | log.error("The extension treatment is not supported"); |
| 110 | return null; |
| 111 | } |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 112 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 113 | if (device == null) { |
| 114 | return null; |
| 115 | } |
| 116 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 117 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 118 | ExtensionTreatment treatment = |
| 119 | resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 120 | try { |
| 121 | treatment.setPropertyValue(TUNNEL_DST, remoteIp); |
| 122 | return treatment; |
| 123 | } catch (ExtensionPropertyException e) { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 124 | log.warn("Failed to get tunnelDst extension treatment for {} " + |
| 125 | "because of {}", deviceId, e); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 126 | return null; |
| 127 | } |
| 128 | } |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Builds OVS ConnTrack matches. |
| 132 | * |
| 133 | * @param driverService driver service |
| 134 | * @param deviceId device ID |
| 135 | * @param ctState connection tracking sate masking value |
| 136 | * @param ctSateMask connection tracking sate masking value |
| 137 | * @return OVS ConnTrack extension match |
| 138 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 139 | public static ExtensionSelector buildCtExtensionSelector(DriverService driverService, |
| 140 | DeviceId deviceId, |
| 141 | long ctState, |
| 142 | long ctSateMask) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 143 | DriverHandler handler = driverService.createHandler(deviceId); |
| 144 | ExtensionSelectorResolver esr = handler.behaviour(ExtensionSelectorResolver.class); |
| 145 | |
| 146 | ExtensionSelector extensionSelector = esr.getExtensionSelector( |
| 147 | ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_STATE.type()); |
| 148 | try { |
| 149 | extensionSelector.setPropertyValue(CT_STATE, ctState); |
| 150 | extensionSelector.setPropertyValue(CT_STATE_MASK, ctSateMask); |
| 151 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 152 | log.error("Failed to set nicira match CT state because of {}", e); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 153 | return null; |
| 154 | } |
| 155 | |
| 156 | return extensionSelector; |
| 157 | } |
| 158 | |
| 159 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 160 | * Computes ConnTack State flag values. |
| 161 | * |
| 162 | * @param isTracking true for +trk, false for -trk |
| 163 | * @param isNew true for +new, false for nothing |
| 164 | * @param isEstablished true for +est, false for nothing |
| 165 | * @return ConnTrack State flags |
| 166 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 167 | public static long computeCtStateFlag(boolean isTracking, |
| 168 | boolean isNew, |
| 169 | boolean isEstablished) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 170 | long ctMaskFlag = 0x00; |
| 171 | |
| 172 | if (isTracking) { |
| 173 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 174 | } |
| 175 | |
| 176 | if (isNew) { |
| 177 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 178 | ctMaskFlag = ctMaskFlag | CT_STATE_NEW; |
| 179 | } |
| 180 | |
| 181 | if (isEstablished) { |
| 182 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 183 | ctMaskFlag = ctMaskFlag | CT_STATE_EST; |
| 184 | } |
| 185 | |
| 186 | return ctMaskFlag; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Computes ConnTrack State mask values. |
| 191 | * |
| 192 | * @param isTracking true for setting +trk/-trk value, false for otherwise |
| 193 | * @param isNew true for setting +new value, false for otherwise |
| 194 | * @param isEstablished true for setting +est value, false for otherwise |
| 195 | * @return ConnTrack State Mask value |
| 196 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 197 | public static long computeCtMaskFlag(boolean isTracking, |
| 198 | boolean isNew, |
| 199 | boolean isEstablished) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 200 | long ctMaskFlag = 0x00; |
| 201 | |
| 202 | if (isTracking) { |
| 203 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 204 | } |
| 205 | |
| 206 | if (isNew) { |
| 207 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 208 | ctMaskFlag = ctMaskFlag | CT_STATE_NEW; |
| 209 | } |
| 210 | |
| 211 | if (isEstablished) { |
| 212 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 213 | ctMaskFlag = ctMaskFlag | CT_STATE_EST; |
| 214 | } |
| 215 | |
| 216 | return ctMaskFlag; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Builder class for OVS Connection Tracking feature actions. |
| 221 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 222 | public static final class NiciraConnTrackTreatmentBuilder { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 223 | |
| 224 | private DriverService driverService; |
| 225 | private DeviceId deviceId; |
| 226 | private IpAddress natAddress = null; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 227 | private TpPort natPortMin = null; |
| 228 | private TpPort natPortMax = null; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 229 | private int zone; |
| 230 | private boolean commit; |
| 231 | private short table = -1; |
| 232 | private boolean natAction; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 233 | private int natFlag; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 234 | |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 235 | // private constructor |
| 236 | private NiciraConnTrackTreatmentBuilder(DriverService driverService, |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 237 | DeviceId deviceId) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 238 | this.driverService = driverService; |
| 239 | this.deviceId = deviceId; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Sets commit flag. |
| 244 | * |
| 245 | * @param c true if commit, false if not. |
| 246 | * @return NiriraConnTrackTreatmentBuilder object |
| 247 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 248 | public NiciraConnTrackTreatmentBuilder commit(boolean c) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 249 | this.commit = c; |
| 250 | return this; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Sets zone number. |
| 255 | * |
| 256 | * @param z zone number |
| 257 | * @return NiriraConnTrackTreatmentBuilder object |
| 258 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 259 | public NiciraConnTrackTreatmentBuilder zone(int z) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 260 | this.zone = z; |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Sets recirculation table number. |
| 266 | * |
| 267 | * @param t table number to restart |
| 268 | * @return NiriraConnTrackTreatmentBuilder object |
| 269 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 270 | public NiciraConnTrackTreatmentBuilder table(short t) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 271 | this.table = t; |
| 272 | return this; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Sets IP address for NAT. |
| 277 | * |
| 278 | * @param ip NAT IP address |
| 279 | * @return NiriraConnTrackTreatmentBuilder object |
| 280 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 281 | public NiciraConnTrackTreatmentBuilder natIp(IpAddress ip) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 282 | this.natAddress = ip; |
| 283 | return this; |
| 284 | } |
| 285 | |
| 286 | /** |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 287 | * Sets min port for NAT. |
| 288 | * |
| 289 | * @param port port number |
| 290 | * @return NiciraConnTrackTreatmentBuilder object |
| 291 | */ |
| 292 | public NiciraConnTrackTreatmentBuilder natPortMin(TpPort port) { |
| 293 | this.natPortMin = port; |
| 294 | return this; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Sets max port for NAT. |
| 299 | * |
| 300 | * @param port port number |
| 301 | * @return NiciraConnTrackTreatmentBuilder object |
| 302 | */ |
| 303 | public NiciraConnTrackTreatmentBuilder natPortMax(TpPort port) { |
| 304 | this.natPortMax = port; |
| 305 | return this; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Sets NAT flags. |
| 310 | * SRC NAT: 1 << 0 |
| 311 | * DST NAT: 1 << 1 |
| 312 | * PERSISTENT NAT: 1 << 2 |
| 313 | * PROTO_HASH NAT: 1 << 3 |
| 314 | * PROTO_RANDOM NAT : 1 << 4 |
| 315 | * |
| 316 | * @param flag flag value |
| 317 | * @return NiciraConnTrackTreatmentBuilder object |
| 318 | */ |
| 319 | public NiciraConnTrackTreatmentBuilder natFlag(int flag) { |
| 320 | this.natFlag = 1 << flag; |
| 321 | return this; |
| 322 | } |
| 323 | |
| 324 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 325 | * Sets the flag for NAT action. |
| 326 | * |
| 327 | * @param nat nat action is included if true, no nat action otherwise |
| 328 | * @return NiriraConnTrackTreatmentBuilder object |
| 329 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 330 | public NiciraConnTrackTreatmentBuilder natAction(boolean nat) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 331 | this.natAction = nat; |
| 332 | return this; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Builds extension treatment for OVS ConnTack and NAT feature. |
| 337 | * |
| 338 | * @return ExtensionTreatment object |
| 339 | */ |
| 340 | public ExtensionTreatment build() { |
| 341 | DriverHandler handler = driverService.createHandler(deviceId); |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 342 | ExtensionTreatmentResolver etr = |
| 343 | handler.behaviour(ExtensionTreatmentResolver.class); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 344 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 345 | ExtensionTreatment natTreatment = etr.getExtensionInstruction( |
| 346 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type()); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 347 | try { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 348 | |
| 349 | if (natAddress == null && natPortMin == null && natPortMax == null) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 350 | natTreatment.setPropertyValue(CT_FLAGS, 0); |
| 351 | natTreatment.setPropertyValue(CT_PRESENT_FLAGS, 0); |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 352 | } else { |
| 353 | natTreatment.setPropertyValue(CT_FLAGS, this.natFlag); |
| 354 | |
| 355 | natTreatment.setPropertyValue(CT_PRESENT_FLAGS, |
| 356 | buildPresentFlag((natPortMin != null && natPortMax != null), |
| 357 | natAddress != null)); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 358 | } |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 359 | |
| 360 | if (natAddress != null) { |
| 361 | natTreatment.setPropertyValue(CT_IPADDRESS_MIN, natAddress); |
| 362 | natTreatment.setPropertyValue(CT_IPADDRESS_MAX, natAddress); |
| 363 | } |
| 364 | |
| 365 | if (natPortMin != null) { |
| 366 | natTreatment.setPropertyValue(CT_PORT_MIN, natPortMin.toInt()); |
| 367 | } |
| 368 | |
| 369 | if (natPortMax != null) { |
| 370 | natTreatment.setPropertyValue(CT_PORT_MAX, natPortMax.toInt()); |
| 371 | } |
| 372 | |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 373 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 374 | log.error("Failed to set NAT due to error : {}", e); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 375 | return null; |
| 376 | } |
| 377 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 378 | ExtensionTreatment ctTreatment = etr.getExtensionInstruction( |
| 379 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type()); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 380 | try { |
| 381 | List<ExtensionTreatment> nat = new ArrayList<>(); |
| 382 | if (natAction) { |
| 383 | nat.add(natTreatment); |
| 384 | } |
| 385 | ctTreatment.setPropertyValue(CT_FLAGS, commit ? 1 : 0); |
| 386 | ctTreatment.setPropertyValue(CT_ZONE, zone); |
| 387 | ctTreatment.setPropertyValue(CT_TABLE, table > -1 ? table : 0xff); |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 388 | ctTreatment.setPropertyValue(CT_NESTED_ACTIONS, nat); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 389 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 390 | log.error("Failed to set CT due to error : {}", e); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 391 | return null; |
| 392 | } |
| 393 | |
| 394 | return ctTreatment; |
| 395 | } |
| 396 | |
| 397 | private int buildPresentFlag(boolean isPortPresent, boolean isAddressPresent) { |
| 398 | |
| 399 | int presentFlag = 0; |
| 400 | |
| 401 | if (isPortPresent) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 402 | presentFlag = 1 << PORT_MIN_FLAG | 1 << PORT_MAX_FLAG; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (isAddressPresent) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame^] | 406 | // TODO: need to support IPv6 address |
| 407 | presentFlag = presentFlag | 1 << ADDRESS_V4_MIN_FLAG | 1 << ADDRESS_V4_MAX_FLAG; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | return presentFlag; |
| 411 | } |
| 412 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 413 | } |