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 | |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 38 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_LOAD; |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 39 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA; |
| 40 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA; |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 41 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST; |
| 42 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST; |
| 43 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_POP_NSH; |
| 44 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_PUSH_NSH; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 45 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Provides common methods to help populating flow rules for SONA applications. |
| 50 | */ |
| 51 | public final class RulePopulatorUtil { |
| 52 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 53 | private static final Logger log = getLogger(RulePopulatorUtil.class); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 54 | |
| 55 | private static final String TUNNEL_DST = "tunnelDst"; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 56 | private static final String CT_FLAGS = "flags"; |
| 57 | private static final String CT_ZONE = "zone"; |
| 58 | private static final String CT_TABLE = "recircTable"; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 59 | private static final String CT_STATE = "ctState"; |
| 60 | private static final String CT_STATE_MASK = "ctStateMask"; |
| 61 | private static final String CT_PRESENT_FLAGS = "presentFlags"; |
| 62 | private static final String CT_IPADDRESS_MIN = "ipAddressMin"; |
| 63 | private static final String CT_IPADDRESS_MAX = "ipAddressMax"; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 64 | private static final String CT_PORT_MIN = "portMin"; |
| 65 | private static final String CT_PORT_MAX = "portMax"; |
| 66 | private static final String CT_NESTED_ACTIONS = "nestedActions"; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 67 | |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 68 | public static final int CT_NAT_SRC_FLAG = 0; |
| 69 | public static final int CT_NAT_DST_FLAG = 1; |
| 70 | public static final int CT_NAT_PERSISTENT_FLAG = 2; |
| 71 | public static final int CT_NAT_PROTO_HASH_FLAG = 3; |
| 72 | public static final int CT_NAT_PROTO_RANDOM_FLAG = 4; |
| 73 | |
| 74 | private static final int ADDRESS_V4_MIN_FLAG = 0; |
| 75 | private static final int ADDRESS_V4_MAX_FLAG = 1; |
| 76 | private static final int ADDRESS_V6_MIN_FLAG = 2; |
| 77 | private static final int ADDRESS_V6_MAX_FLAG = 3; |
| 78 | private static final int PORT_MIN_FLAG = 4; |
| 79 | private static final int PORT_MAX_FLAG = 5; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 80 | |
| 81 | // Refer to http://openvswitch.org/support/dist-docs/ovs-fields.7.txt for the values |
| 82 | public static final long CT_STATE_NONE = 0; |
| 83 | public static final long CT_STATE_NEW = 0x01; |
| 84 | public static final long CT_STATE_EST = 0x02; |
| 85 | public static final long CT_STATE_NOT_TRK = 0x20; |
| 86 | public static final long CT_STATE_TRK = 0x20; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 87 | |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 88 | private static final String OFF_SET_N_BITS = "ofsNbits"; |
| 89 | private static final String DESTINATION = "dst"; |
| 90 | private static final String VALUE = "value"; |
| 91 | |
| 92 | private static final int OFF_SET_BIT = 0; |
| 93 | private static final int REMAINDER_BIT = 8; |
| 94 | |
| 95 | // layer 3 nicira fields |
| 96 | public static final int NXM_OF_IP_SRC = 0x00000e04; |
| 97 | public static final int NXM_OF_IP_DST = 0x00001004; |
| 98 | public static final int NXM_OF_IP_PROT = 0x00000c01; |
| 99 | |
| 100 | public static final int NXM_NX_IP_TTL = 0x00013a01; |
| 101 | public static final int NXM_NX_IP_FRAG = 0x00013401; |
| 102 | public static final int NXM_OF_ARP_OP = 0x00001e02; |
| 103 | public static final int NXM_OF_ARP_SPA = 0x00002004; |
| 104 | public static final int NXM_OF_ARP_TPA = 0x00002204; |
| 105 | public static final int NXM_NX_ARP_SHA = 0x00012206; |
| 106 | public static final int NXM_NX_ARP_THA = 0x00012406; |
| 107 | |
| 108 | // layer 4 nicira fields |
| 109 | public static final int NXM_OF_TCP_SRC = 0x00001202; |
| 110 | public static final int NXM_OF_TCP_DST = 0x00001402; |
| 111 | public static final int NXM_NX_TCP_FLAGS = 0x00014402; |
| 112 | public static final int NXM_OF_UDP_SRC = 0x00001602; |
| 113 | public static final int NXM_OF_UDP_DST = 0x00001802; |
| 114 | |
| 115 | public static final int NXM_OF_ICMP_TYPE = 0x00001a01; |
| 116 | public static final int NXM_OF_ICMP_CODE = 0x00001c01; |
| 117 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 118 | private RulePopulatorUtil() { |
| 119 | } |
| 120 | |
| 121 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 122 | * Returns a builder for OVS Connection Tracking feature actions. |
| 123 | * |
| 124 | * @param ds DriverService |
| 125 | * @param id DeviceId |
| 126 | * @return a builder for OVS Connection Tracking feature actions |
| 127 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 128 | public static NiciraConnTrackTreatmentBuilder |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 129 | niciraConnTrackTreatmentBuilder(DriverService ds, DeviceId id) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 130 | return new NiciraConnTrackTreatmentBuilder(ds, id); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 134 | * Returns tunnel destination extension treatment object. |
| 135 | * |
| 136 | * @param deviceService driver service |
| 137 | * @param deviceId device id to apply this treatment |
| 138 | * @param remoteIp tunnel destination ip address |
| 139 | * @return extension treatment |
| 140 | */ |
| 141 | public static ExtensionTreatment buildExtension(DeviceService deviceService, |
| 142 | DeviceId deviceId, |
| 143 | Ip4Address remoteIp) { |
| 144 | Device device = deviceService.getDevice(deviceId); |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 145 | if (!checkTreatmentResolver(device)) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 146 | return null; |
| 147 | } |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 148 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 149 | if (device == null) { |
| 150 | return null; |
| 151 | } |
| 152 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 153 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 154 | ExtensionTreatment treatment = |
| 155 | resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 156 | try { |
| 157 | treatment.setPropertyValue(TUNNEL_DST, remoteIp); |
| 158 | return treatment; |
| 159 | } catch (ExtensionPropertyException e) { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 160 | log.warn("Failed to get tunnelDst extension treatment for {} " + |
| 161 | "because of {}", deviceId, e); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 162 | return null; |
| 163 | } |
| 164 | } |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Builds OVS ConnTrack matches. |
| 168 | * |
| 169 | * @param driverService driver service |
| 170 | * @param deviceId device ID |
| 171 | * @param ctState connection tracking sate masking value |
| 172 | * @param ctSateMask connection tracking sate masking value |
| 173 | * @return OVS ConnTrack extension match |
| 174 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 175 | public static ExtensionSelector buildCtExtensionSelector(DriverService driverService, |
| 176 | DeviceId deviceId, |
| 177 | long ctState, |
| 178 | long ctSateMask) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 179 | DriverHandler handler = driverService.createHandler(deviceId); |
| 180 | ExtensionSelectorResolver esr = handler.behaviour(ExtensionSelectorResolver.class); |
| 181 | |
| 182 | ExtensionSelector extensionSelector = esr.getExtensionSelector( |
| 183 | ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_STATE.type()); |
| 184 | try { |
| 185 | extensionSelector.setPropertyValue(CT_STATE, ctState); |
| 186 | extensionSelector.setPropertyValue(CT_STATE_MASK, ctSateMask); |
| 187 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 188 | log.error("Failed to set nicira match CT state because of {}", e); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 189 | return null; |
| 190 | } |
| 191 | |
| 192 | return extensionSelector; |
| 193 | } |
| 194 | |
| 195 | /** |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 196 | * Returns the nicira load extension treatment. |
| 197 | * |
| 198 | * @param device device instance |
| 199 | * @param field field code |
| 200 | * @param value value to load |
| 201 | * @return load extension treatment |
| 202 | */ |
| 203 | public static ExtensionTreatment buildLoadExtension(Device device, |
| 204 | long field, |
| 205 | long value) { |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 206 | if (!checkTreatmentResolver(device)) { |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 207 | return null; |
| 208 | } |
| 209 | |
| 210 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 211 | ExtensionTreatment treatment = |
| 212 | resolver.getExtensionInstruction(NICIRA_LOAD.type()); |
| 213 | |
| 214 | int ofsNbits = OFF_SET_BIT << 6 | (REMAINDER_BIT - 1); |
| 215 | |
| 216 | try { |
| 217 | treatment.setPropertyValue(OFF_SET_N_BITS, ofsNbits); |
| 218 | treatment.setPropertyValue(DESTINATION, field); |
| 219 | treatment.setPropertyValue(VALUE, value); |
| 220 | return treatment; |
| 221 | } catch (ExtensionPropertyException e) { |
| 222 | log.error("Failed to set nicira load extension treatment for {}", |
| 223 | device.id()); |
| 224 | return null; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns the nicira push extension treatment. |
| 230 | * |
| 231 | * @param device device instance |
| 232 | * @return push extension treatment |
| 233 | */ |
| 234 | public static ExtensionTreatment buildPushExtension(Device device) { |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 235 | if (!checkTreatmentResolver(device)) { |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 236 | return null; |
| 237 | } |
| 238 | |
| 239 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 240 | return resolver.getExtensionInstruction(NICIRA_PUSH_NSH.type()); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns the nicira pop extension treatment. |
| 245 | * |
| 246 | * @param device device instance |
| 247 | * @return pop extension treatment |
| 248 | */ |
| 249 | public static ExtensionTreatment buildPopExtension(Device device) { |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 250 | if (!checkTreatmentResolver(device)) { |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 251 | return null; |
| 252 | } |
| 253 | |
| 254 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 255 | return resolver.getExtensionInstruction(NICIRA_POP_NSH.type()); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns the nicira move source MAC to destination MAC extension treatment. |
| 260 | * |
| 261 | * @param device device instance |
| 262 | * @return move extension treatment |
| 263 | */ |
| 264 | public static ExtensionTreatment buildMoveEthSrcToDstExtension(Device device) { |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 265 | if (!checkTreatmentResolver(device)) { |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 266 | return null; |
| 267 | } |
| 268 | |
| 269 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 270 | return resolver.getExtensionInstruction(NICIRA_MOV_ETH_SRC_TO_DST.type()); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Returns the nicira move source IP to destination IP extension treatment. |
| 275 | * |
| 276 | * @param device device instance |
| 277 | * @return move extension treatment |
| 278 | */ |
| 279 | public static ExtensionTreatment buildMoveIpSrcToDstExtension(Device device) { |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 280 | if (!checkTreatmentResolver(device)) { |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 281 | return null; |
| 282 | } |
| 283 | |
| 284 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 285 | return resolver.getExtensionInstruction(NICIRA_MOV_IP_SRC_TO_DST.type()); |
| 286 | } |
| 287 | |
| 288 | /** |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 289 | * Returns the nicira move ARP SHA to THA extension treatment. |
| 290 | * |
| 291 | * @param device device instance |
| 292 | * @return move extension treatment |
| 293 | */ |
| 294 | public static ExtensionTreatment buildMoveArpShaToThaExtension(Device device) { |
| 295 | if (!checkTreatmentResolver(device)) { |
| 296 | return null; |
| 297 | } |
| 298 | |
| 299 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 300 | return resolver.getExtensionInstruction(NICIRA_MOV_ARP_SHA_TO_THA.type()); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Returns the nicira move ARP SPA to TPA extension treatment. |
| 305 | * |
| 306 | * @param device device instance |
| 307 | * @return move extension treatment |
| 308 | */ |
| 309 | public static ExtensionTreatment buildMoveArpSpaToTpaExtension(Device device) { |
| 310 | if (!checkTreatmentResolver(device)) { |
| 311 | return null; |
| 312 | } |
| 313 | |
| 314 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 315 | return resolver.getExtensionInstruction(NICIRA_MOV_ARP_SPA_TO_TPA.type()); |
| 316 | } |
| 317 | |
| 318 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 319 | * Computes ConnTack State flag values. |
| 320 | * |
| 321 | * @param isTracking true for +trk, false for -trk |
| 322 | * @param isNew true for +new, false for nothing |
| 323 | * @param isEstablished true for +est, false for nothing |
| 324 | * @return ConnTrack State flags |
| 325 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 326 | public static long computeCtStateFlag(boolean isTracking, |
| 327 | boolean isNew, |
| 328 | boolean isEstablished) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 329 | long ctMaskFlag = 0x00; |
| 330 | |
| 331 | if (isTracking) { |
| 332 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 333 | } |
| 334 | |
| 335 | if (isNew) { |
| 336 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 337 | ctMaskFlag = ctMaskFlag | CT_STATE_NEW; |
| 338 | } |
| 339 | |
| 340 | if (isEstablished) { |
| 341 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 342 | ctMaskFlag = ctMaskFlag | CT_STATE_EST; |
| 343 | } |
| 344 | |
| 345 | return ctMaskFlag; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Computes ConnTrack State mask values. |
| 350 | * |
| 351 | * @param isTracking true for setting +trk/-trk value, false for otherwise |
| 352 | * @param isNew true for setting +new value, false for otherwise |
| 353 | * @param isEstablished true for setting +est value, false for otherwise |
| 354 | * @return ConnTrack State Mask value |
| 355 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 356 | public static long computeCtMaskFlag(boolean isTracking, |
| 357 | boolean isNew, |
| 358 | boolean isEstablished) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 359 | long ctMaskFlag = 0x00; |
| 360 | |
| 361 | if (isTracking) { |
| 362 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 363 | } |
| 364 | |
| 365 | if (isNew) { |
| 366 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 367 | ctMaskFlag = ctMaskFlag | CT_STATE_NEW; |
| 368 | } |
| 369 | |
| 370 | if (isEstablished) { |
| 371 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 372 | ctMaskFlag = ctMaskFlag | CT_STATE_EST; |
| 373 | } |
| 374 | |
| 375 | return ctMaskFlag; |
| 376 | } |
| 377 | |
Jian Li | 4910c4b | 2019-04-01 18:06:40 +0900 | [diff] [blame^] | 378 | private static boolean checkTreatmentResolver(Device device) { |
| 379 | if (device == null || !device.is(ExtensionTreatmentResolver.class)) { |
| 380 | log.warn("Nicira extension treatment is not supported"); |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 387 | /** |
| 388 | * Builder class for OVS Connection Tracking feature actions. |
| 389 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 390 | public static final class NiciraConnTrackTreatmentBuilder { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 391 | |
| 392 | private DriverService driverService; |
| 393 | private DeviceId deviceId; |
| 394 | private IpAddress natAddress = null; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 395 | private TpPort natPortMin = null; |
| 396 | private TpPort natPortMax = null; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 397 | private int zone; |
| 398 | private boolean commit; |
| 399 | private short table = -1; |
| 400 | private boolean natAction; |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 401 | private int natFlag; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 402 | |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 403 | // private constructor |
| 404 | private NiciraConnTrackTreatmentBuilder(DriverService driverService, |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 405 | DeviceId deviceId) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 406 | this.driverService = driverService; |
| 407 | this.deviceId = deviceId; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Sets commit flag. |
| 412 | * |
| 413 | * @param c true if commit, false if not. |
| 414 | * @return NiriraConnTrackTreatmentBuilder object |
| 415 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 416 | public NiciraConnTrackTreatmentBuilder commit(boolean c) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 417 | this.commit = c; |
| 418 | return this; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Sets zone number. |
| 423 | * |
| 424 | * @param z zone number |
| 425 | * @return NiriraConnTrackTreatmentBuilder object |
| 426 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 427 | public NiciraConnTrackTreatmentBuilder zone(int z) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 428 | this.zone = z; |
| 429 | return this; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Sets recirculation table number. |
| 434 | * |
| 435 | * @param t table number to restart |
| 436 | * @return NiriraConnTrackTreatmentBuilder object |
| 437 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 438 | public NiciraConnTrackTreatmentBuilder table(short t) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 439 | this.table = t; |
| 440 | return this; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Sets IP address for NAT. |
| 445 | * |
| 446 | * @param ip NAT IP address |
| 447 | * @return NiriraConnTrackTreatmentBuilder object |
| 448 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 449 | public NiciraConnTrackTreatmentBuilder natIp(IpAddress ip) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 450 | this.natAddress = ip; |
| 451 | return this; |
| 452 | } |
| 453 | |
| 454 | /** |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 455 | * Sets min port for NAT. |
| 456 | * |
| 457 | * @param port port number |
| 458 | * @return NiciraConnTrackTreatmentBuilder object |
| 459 | */ |
| 460 | public NiciraConnTrackTreatmentBuilder natPortMin(TpPort port) { |
| 461 | this.natPortMin = port; |
| 462 | return this; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Sets max port for NAT. |
| 467 | * |
| 468 | * @param port port number |
| 469 | * @return NiciraConnTrackTreatmentBuilder object |
| 470 | */ |
| 471 | public NiciraConnTrackTreatmentBuilder natPortMax(TpPort port) { |
| 472 | this.natPortMax = port; |
| 473 | return this; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Sets NAT flags. |
| 478 | * SRC NAT: 1 << 0 |
| 479 | * DST NAT: 1 << 1 |
| 480 | * PERSISTENT NAT: 1 << 2 |
| 481 | * PROTO_HASH NAT: 1 << 3 |
| 482 | * PROTO_RANDOM NAT : 1 << 4 |
| 483 | * |
| 484 | * @param flag flag value |
| 485 | * @return NiciraConnTrackTreatmentBuilder object |
| 486 | */ |
| 487 | public NiciraConnTrackTreatmentBuilder natFlag(int flag) { |
| 488 | this.natFlag = 1 << flag; |
| 489 | return this; |
| 490 | } |
| 491 | |
| 492 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 493 | * Sets the flag for NAT action. |
| 494 | * |
| 495 | * @param nat nat action is included if true, no nat action otherwise |
| 496 | * @return NiriraConnTrackTreatmentBuilder object |
| 497 | */ |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 498 | public NiciraConnTrackTreatmentBuilder natAction(boolean nat) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 499 | this.natAction = nat; |
| 500 | return this; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Builds extension treatment for OVS ConnTack and NAT feature. |
| 505 | * |
| 506 | * @return ExtensionTreatment object |
| 507 | */ |
| 508 | public ExtensionTreatment build() { |
| 509 | DriverHandler handler = driverService.createHandler(deviceId); |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 510 | ExtensionTreatmentResolver etr = |
| 511 | handler.behaviour(ExtensionTreatmentResolver.class); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 512 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 513 | ExtensionTreatment natTreatment = etr.getExtensionInstruction( |
| 514 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type()); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 515 | try { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 516 | |
| 517 | if (natAddress == null && natPortMin == null && natPortMax == null) { |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 518 | natTreatment.setPropertyValue(CT_FLAGS, 0); |
| 519 | natTreatment.setPropertyValue(CT_PRESENT_FLAGS, 0); |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 520 | } else { |
| 521 | natTreatment.setPropertyValue(CT_FLAGS, this.natFlag); |
| 522 | |
| 523 | natTreatment.setPropertyValue(CT_PRESENT_FLAGS, |
| 524 | buildPresentFlag((natPortMin != null && natPortMax != null), |
| 525 | natAddress != null)); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 526 | } |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 527 | |
| 528 | if (natAddress != null) { |
| 529 | natTreatment.setPropertyValue(CT_IPADDRESS_MIN, natAddress); |
| 530 | natTreatment.setPropertyValue(CT_IPADDRESS_MAX, natAddress); |
| 531 | } |
| 532 | |
| 533 | if (natPortMin != null) { |
| 534 | natTreatment.setPropertyValue(CT_PORT_MIN, natPortMin.toInt()); |
| 535 | } |
| 536 | |
| 537 | if (natPortMax != null) { |
| 538 | natTreatment.setPropertyValue(CT_PORT_MAX, natPortMax.toInt()); |
| 539 | } |
| 540 | |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 541 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 542 | log.error("Failed to set NAT due to error : {}", e); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 543 | return null; |
| 544 | } |
| 545 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 546 | ExtensionTreatment ctTreatment = etr.getExtensionInstruction( |
| 547 | ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type()); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 548 | try { |
| 549 | List<ExtensionTreatment> nat = new ArrayList<>(); |
| 550 | if (natAction) { |
| 551 | nat.add(natTreatment); |
| 552 | } |
| 553 | ctTreatment.setPropertyValue(CT_FLAGS, commit ? 1 : 0); |
| 554 | ctTreatment.setPropertyValue(CT_ZONE, zone); |
| 555 | ctTreatment.setPropertyValue(CT_TABLE, table > -1 ? table : 0xff); |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 556 | ctTreatment.setPropertyValue(CT_NESTED_ACTIONS, nat); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 557 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 558 | log.error("Failed to set CT due to error : {}", e); |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 559 | return null; |
| 560 | } |
| 561 | |
| 562 | return ctTreatment; |
| 563 | } |
| 564 | |
| 565 | private int buildPresentFlag(boolean isPortPresent, boolean isAddressPresent) { |
| 566 | |
| 567 | int presentFlag = 0; |
| 568 | |
| 569 | if (isPortPresent) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 570 | presentFlag = 1 << PORT_MIN_FLAG | 1 << PORT_MAX_FLAG; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if (isAddressPresent) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 574 | // TODO: need to support IPv6 address |
| 575 | presentFlag = presentFlag | 1 << ADDRESS_V4_MIN_FLAG | 1 << ADDRESS_V4_MAX_FLAG; |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | return presentFlag; |
| 579 | } |
| 580 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 581 | } |