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"; |
| 51 | private static final String CT = "niciraCt"; |
| 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"; |
| 57 | |
| 58 | private static final int ADDRESS_MIN_FLAG = 0; |
| 59 | private static final int ADDRESS_MAX_FLAG = 1; |
| 60 | private static final int PORT_MIN_FLAG = 2; |
| 61 | private static final int PORT_MAX_FLAG = 3; |
| 62 | |
| 63 | // Refer to http://openvswitch.org/support/dist-docs/ovs-fields.7.txt for the values |
| 64 | public static final long CT_STATE_NONE = 0; |
| 65 | public static final long CT_STATE_NEW = 0x01; |
| 66 | public static final long CT_STATE_EST = 0x02; |
| 67 | public static final long CT_STATE_NOT_TRK = 0x20; |
| 68 | public static final long CT_STATE_TRK = 0x20; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 69 | |
| 70 | private RulePopulatorUtil() { |
| 71 | } |
| 72 | |
| 73 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 74 | * Returns a builder for OVS Connection Tracking feature actions. |
| 75 | * |
| 76 | * @param ds DriverService |
| 77 | * @param id DeviceId |
| 78 | * @return a builder for OVS Connection Tracking feature actions |
| 79 | */ |
| 80 | public static NiriraConnTrackTreatmentBuilder niciraConnTrackTreatmentBuilder(DriverService ds, DeviceId id) { |
| 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); |
| 106 | ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
| 107 | try { |
| 108 | treatment.setPropertyValue(TUNNEL_DST, remoteIp); |
| 109 | return treatment; |
| 110 | } catch (ExtensionPropertyException e) { |
| 111 | log.warn("Failed to get tunnelDst extension treatment for {}", deviceId); |
| 112 | return null; |
| 113 | } |
| 114 | } |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Builds OVS ConnTrack matches. |
| 118 | * |
| 119 | * @param driverService driver service |
| 120 | * @param deviceId device ID |
| 121 | * @param ctState connection tracking sate masking value |
| 122 | * @param ctSateMask connection tracking sate masking value |
| 123 | * @return OVS ConnTrack extension match |
| 124 | */ |
| 125 | public static ExtensionSelector buildCtExtensionSelector(DriverService driverService, DeviceId deviceId, |
| 126 | long ctState, long ctSateMask) { |
| 127 | DriverHandler handler = driverService.createHandler(deviceId); |
| 128 | ExtensionSelectorResolver esr = handler.behaviour(ExtensionSelectorResolver.class); |
| 129 | |
| 130 | ExtensionSelector extensionSelector = esr.getExtensionSelector( |
| 131 | ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_STATE.type()); |
| 132 | try { |
| 133 | extensionSelector.setPropertyValue(CT_STATE, ctState); |
| 134 | extensionSelector.setPropertyValue(CT_STATE_MASK, ctSateMask); |
| 135 | } catch (Exception e) { |
| 136 | log.error("Failed to set nicira match CT state"); |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | return extensionSelector; |
| 141 | } |
| 142 | |
| 143 | /** |
sangho | 072c4dd | 2017-05-17 10:45:21 +0900 | [diff] [blame] | 144 | * Computes ConnTack State flag values. |
| 145 | * |
| 146 | * @param isTracking true for +trk, false for -trk |
| 147 | * @param isNew true for +new, false for nothing |
| 148 | * @param isEstablished true for +est, false for nothing |
| 149 | * @return ConnTrack State flags |
| 150 | */ |
| 151 | public static long computeCtStateFlag(boolean isTracking, boolean isNew, boolean isEstablished) { |
| 152 | long ctMaskFlag = 0x00; |
| 153 | |
| 154 | if (isTracking) { |
| 155 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 156 | } |
| 157 | |
| 158 | if (isNew) { |
| 159 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 160 | ctMaskFlag = ctMaskFlag | CT_STATE_NEW; |
| 161 | } |
| 162 | |
| 163 | if (isEstablished) { |
| 164 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 165 | ctMaskFlag = ctMaskFlag | CT_STATE_EST; |
| 166 | } |
| 167 | |
| 168 | return ctMaskFlag; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Computes ConnTrack State mask values. |
| 173 | * |
| 174 | * @param isTracking true for setting +trk/-trk value, false for otherwise |
| 175 | * @param isNew true for setting +new value, false for otherwise |
| 176 | * @param isEstablished true for setting +est value, false for otherwise |
| 177 | * @return ConnTrack State Mask value |
| 178 | */ |
| 179 | public static long computeCtMaskFlag(boolean isTracking, boolean isNew, boolean isEstablished) { |
| 180 | long ctMaskFlag = 0x00; |
| 181 | |
| 182 | if (isTracking) { |
| 183 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 184 | } |
| 185 | |
| 186 | if (isNew) { |
| 187 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 188 | ctMaskFlag = ctMaskFlag | CT_STATE_NEW; |
| 189 | } |
| 190 | |
| 191 | if (isEstablished) { |
| 192 | ctMaskFlag = ctMaskFlag | CT_STATE_TRK; |
| 193 | ctMaskFlag = ctMaskFlag | CT_STATE_EST; |
| 194 | } |
| 195 | |
| 196 | return ctMaskFlag; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Builder class for OVS Connection Tracking feature actions. |
| 201 | */ |
| 202 | public static final class NiriraConnTrackTreatmentBuilder { |
| 203 | |
| 204 | private DriverService driverService; |
| 205 | private DeviceId deviceId; |
| 206 | private IpAddress natAddress = null; |
| 207 | private int zone; |
| 208 | private boolean commit; |
| 209 | private short table = -1; |
| 210 | private boolean natAction; |
| 211 | |
| 212 | |
| 213 | private NiriraConnTrackTreatmentBuilder(DriverService driverService, DeviceId deviceId) { |
| 214 | this.driverService = driverService; |
| 215 | this.deviceId = deviceId; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Sets commit flag. |
| 220 | * |
| 221 | * @param c true if commit, false if not. |
| 222 | * @return NiriraConnTrackTreatmentBuilder object |
| 223 | */ |
| 224 | public NiriraConnTrackTreatmentBuilder commit(boolean c) { |
| 225 | this.commit = c; |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets zone number. |
| 231 | * |
| 232 | * @param z zone number |
| 233 | * @return NiriraConnTrackTreatmentBuilder object |
| 234 | */ |
| 235 | public NiriraConnTrackTreatmentBuilder zone(int z) { |
| 236 | this.zone = z; |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Sets recirculation table number. |
| 242 | * |
| 243 | * @param t table number to restart |
| 244 | * @return NiriraConnTrackTreatmentBuilder object |
| 245 | */ |
| 246 | public NiriraConnTrackTreatmentBuilder table(short t) { |
| 247 | this.table = t; |
| 248 | return this; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Sets IP address for NAT. |
| 253 | * |
| 254 | * @param ip NAT IP address |
| 255 | * @return NiriraConnTrackTreatmentBuilder object |
| 256 | */ |
| 257 | public NiriraConnTrackTreatmentBuilder natIp(IpAddress ip) { |
| 258 | this.natAddress = ip; |
| 259 | return this; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Sets the flag for NAT action. |
| 264 | * |
| 265 | * @param nat nat action is included if true, no nat action otherwise |
| 266 | * @return NiriraConnTrackTreatmentBuilder object |
| 267 | */ |
| 268 | public NiriraConnTrackTreatmentBuilder natAction(boolean nat) { |
| 269 | this.natAction = nat; |
| 270 | return this; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Builds extension treatment for OVS ConnTack and NAT feature. |
| 275 | * |
| 276 | * @return ExtensionTreatment object |
| 277 | */ |
| 278 | public ExtensionTreatment build() { |
| 279 | DriverHandler handler = driverService.createHandler(deviceId); |
| 280 | ExtensionTreatmentResolver etr = handler.behaviour(ExtensionTreatmentResolver.class); |
| 281 | |
| 282 | ExtensionTreatment natTreatment |
| 283 | = etr.getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type()); |
| 284 | try { |
| 285 | if (natAddress != null) { |
| 286 | natTreatment.setPropertyValue(CT_FLAGS, 1); |
| 287 | natTreatment.setPropertyValue(CT_PRESENT_FLAGS, buildPresentFlag(false, true)); |
| 288 | natTreatment.setPropertyValue(CT_IPADDRESS_MIN, natAddress); |
| 289 | natTreatment.setPropertyValue(CT_IPADDRESS_MAX, natAddress); |
| 290 | } else { |
| 291 | natTreatment.setPropertyValue(CT_FLAGS, 0); |
| 292 | natTreatment.setPropertyValue(CT_PRESENT_FLAGS, 0); |
| 293 | } |
| 294 | } catch (Exception e) { |
| 295 | log.error("Failed to set NAT due to error : {}", e.getMessage()); |
| 296 | return null; |
| 297 | } |
| 298 | |
| 299 | ExtensionTreatment ctTreatment |
| 300 | = etr.getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type()); |
| 301 | try { |
| 302 | List<ExtensionTreatment> nat = new ArrayList<>(); |
| 303 | if (natAction) { |
| 304 | nat.add(natTreatment); |
| 305 | } |
| 306 | ctTreatment.setPropertyValue(CT_FLAGS, commit ? 1 : 0); |
| 307 | ctTreatment.setPropertyValue(CT_ZONE, zone); |
| 308 | ctTreatment.setPropertyValue(CT_TABLE, table > -1 ? table : 0xff); |
| 309 | ctTreatment.setPropertyValue("nestedActions", nat); |
| 310 | } catch (Exception e) { |
| 311 | log.error("Failed to set CT due to error : {}", e.getMessage()); |
| 312 | return null; |
| 313 | } |
| 314 | |
| 315 | return ctTreatment; |
| 316 | } |
| 317 | |
| 318 | private int buildPresentFlag(boolean isPortPresent, boolean isAddressPresent) { |
| 319 | |
| 320 | int presentFlag = 0; |
| 321 | |
| 322 | if (isPortPresent) { |
| 323 | presentFlag = presentFlag | 1 << PORT_MIN_FLAG | 1 << PORT_MAX_FLAG; |
| 324 | } |
| 325 | |
| 326 | if (isAddressPresent) { |
| 327 | presentFlag = 1 << ADDRESS_MIN_FLAG | 1 << ADDRESS_MAX_FLAG; |
| 328 | } |
| 329 | |
| 330 | return presentFlag; |
| 331 | } |
| 332 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 333 | } |