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