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