blob: 0466e3bca700f149b4c0ae9bc0626762505af7da [file] [log] [blame]
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -07003 *
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 Li26949762018-03-30 15:46:37 +090016package org.onosproject.openstacknetworking.util;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070017
18import org.onlab.packet.Ip4Address;
sangho072c4dd2017-05-17 10:45:21 +090019import org.onlab.packet.IpAddress;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070020import org.onosproject.net.Device;
21import org.onosproject.net.DeviceId;
sangho072c4dd2017-05-17 10:45:21 +090022import org.onosproject.net.behaviour.ExtensionSelectorResolver;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070023import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
24import org.onosproject.net.device.DeviceService;
sangho072c4dd2017-05-17 10:45:21 +090025import org.onosproject.net.driver.DriverHandler;
26import org.onosproject.net.driver.DriverService;
sangho072c4dd2017-05-17 10:45:21 +090027import org.onosproject.net.flow.criteria.ExtensionSelector;
28import org.onosproject.net.flow.criteria.ExtensionSelectorType;
sangho1aaa7882017-05-31 13:22:47 +090029import org.onosproject.net.flow.instructions.ExtensionPropertyException;
30import org.onosproject.net.flow.instructions.ExtensionTreatment;
sangho072c4dd2017-05-17 10:45:21 +090031import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070032import org.slf4j.Logger;
33
sangho072c4dd2017-05-17 10:45:21 +090034import java.util.ArrayList;
35import java.util.List;
36
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070037import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST;
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * Provides common methods to help populating flow rules for SONA applications.
42 */
43public final class RulePopulatorUtil {
44
Ray Milkey9c9cde42018-01-12 14:22:06 -080045 private static final Logger log = getLogger(RulePopulatorUtil.class);
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070046
47 private static final String TUNNEL_DST = "tunnelDst";
sangho072c4dd2017-05-17 10:45:21 +090048 private static final String CT_FLAGS = "flags";
49 private static final String CT_ZONE = "zone";
50 private static final String CT_TABLE = "recircTable";
sangho072c4dd2017-05-17 10:45:21 +090051 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 Moonb3eb84d2016-07-27 19:10:52 -070068
69 private RulePopulatorUtil() {
70 }
71
72 /**
sangho072c4dd2017-05-17 10:45:21 +090073 * 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 */
79 public static NiriraConnTrackTreatmentBuilder niciraConnTrackTreatmentBuilder(DriverService ds, DeviceId id) {
80 return new NiriraConnTrackTreatmentBuilder(ds, id);
81 }
82
83 /**
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070084 * Returns tunnel destination extension treatment object.
85 *
86 * @param deviceService driver service
87 * @param deviceId device id to apply this treatment
88 * @param remoteIp tunnel destination ip address
89 * @return extension treatment
90 */
91 public static ExtensionTreatment buildExtension(DeviceService deviceService,
92 DeviceId deviceId,
93 Ip4Address remoteIp) {
94 Device device = deviceService.getDevice(deviceId);
sangho072c4dd2017-05-17 10:45:21 +090095 if (device != null && !device.is(ExtensionTreatmentResolver.class)) {
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070096 log.error("The extension treatment is not supported");
97 return null;
98 }
sangho072c4dd2017-05-17 10:45:21 +090099
Ray Milkey74e59132018-01-17 15:24:52 -0800100 if (device == null) {
101 return null;
102 }
103
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700104 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
105 ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
106 try {
107 treatment.setPropertyValue(TUNNEL_DST, remoteIp);
108 return treatment;
109 } catch (ExtensionPropertyException e) {
Daniel Parka3ffbdb2018-11-28 13:51:39 +0900110 log.warn("Failed to get tunnelDst extension treatment for {} because of {}", deviceId, e);
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700111 return null;
112 }
113 }
sangho072c4dd2017-05-17 10:45:21 +0900114
115 /**
116 * Builds OVS ConnTrack matches.
117 *
118 * @param driverService driver service
119 * @param deviceId device ID
120 * @param ctState connection tracking sate masking value
121 * @param ctSateMask connection tracking sate masking value
122 * @return OVS ConnTrack extension match
123 */
124 public static ExtensionSelector buildCtExtensionSelector(DriverService driverService, DeviceId deviceId,
125 long ctState, long ctSateMask) {
126 DriverHandler handler = driverService.createHandler(deviceId);
127 ExtensionSelectorResolver esr = handler.behaviour(ExtensionSelectorResolver.class);
128
129 ExtensionSelector extensionSelector = esr.getExtensionSelector(
130 ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_STATE.type());
131 try {
132 extensionSelector.setPropertyValue(CT_STATE, ctState);
133 extensionSelector.setPropertyValue(CT_STATE_MASK, ctSateMask);
134 } catch (Exception e) {
Daniel Parka3ffbdb2018-11-28 13:51:39 +0900135 log.error("Failed to set nicira match CT state because of {}", e);
sangho072c4dd2017-05-17 10:45:21 +0900136 return null;
137 }
138
139 return extensionSelector;
140 }
141
142 /**
sangho072c4dd2017-05-17 10:45:21 +0900143 * Computes ConnTack State flag values.
144 *
145 * @param isTracking true for +trk, false for -trk
146 * @param isNew true for +new, false for nothing
147 * @param isEstablished true for +est, false for nothing
148 * @return ConnTrack State flags
149 */
150 public static long computeCtStateFlag(boolean isTracking, boolean isNew, boolean isEstablished) {
151 long ctMaskFlag = 0x00;
152
153 if (isTracking) {
154 ctMaskFlag = ctMaskFlag | CT_STATE_TRK;
155 }
156
157 if (isNew) {
158 ctMaskFlag = ctMaskFlag | CT_STATE_TRK;
159 ctMaskFlag = ctMaskFlag | CT_STATE_NEW;
160 }
161
162 if (isEstablished) {
163 ctMaskFlag = ctMaskFlag | CT_STATE_TRK;
164 ctMaskFlag = ctMaskFlag | CT_STATE_EST;
165 }
166
167 return ctMaskFlag;
168 }
169
170 /**
171 * Computes ConnTrack State mask values.
172 *
173 * @param isTracking true for setting +trk/-trk value, false for otherwise
174 * @param isNew true for setting +new value, false for otherwise
175 * @param isEstablished true for setting +est value, false for otherwise
176 * @return ConnTrack State Mask value
177 */
178 public static long computeCtMaskFlag(boolean isTracking, boolean isNew, boolean isEstablished) {
179 long ctMaskFlag = 0x00;
180
181 if (isTracking) {
182 ctMaskFlag = ctMaskFlag | CT_STATE_TRK;
183 }
184
185 if (isNew) {
186 ctMaskFlag = ctMaskFlag | CT_STATE_TRK;
187 ctMaskFlag = ctMaskFlag | CT_STATE_NEW;
188 }
189
190 if (isEstablished) {
191 ctMaskFlag = ctMaskFlag | CT_STATE_TRK;
192 ctMaskFlag = ctMaskFlag | CT_STATE_EST;
193 }
194
195 return ctMaskFlag;
196 }
197
198 /**
199 * Builder class for OVS Connection Tracking feature actions.
200 */
201 public static final class NiriraConnTrackTreatmentBuilder {
202
203 private DriverService driverService;
204 private DeviceId deviceId;
205 private IpAddress natAddress = null;
206 private int zone;
207 private boolean commit;
208 private short table = -1;
209 private boolean natAction;
210
211
212 private NiriraConnTrackTreatmentBuilder(DriverService driverService, DeviceId deviceId) {
213 this.driverService = driverService;
214 this.deviceId = deviceId;
215 }
216
217 /**
218 * Sets commit flag.
219 *
220 * @param c true if commit, false if not.
221 * @return NiriraConnTrackTreatmentBuilder object
222 */
223 public NiriraConnTrackTreatmentBuilder commit(boolean c) {
224 this.commit = c;
225 return this;
226 }
227
228 /**
229 * Sets zone number.
230 *
231 * @param z zone number
232 * @return NiriraConnTrackTreatmentBuilder object
233 */
234 public NiriraConnTrackTreatmentBuilder zone(int z) {
235 this.zone = z;
236 return this;
237 }
238
239 /**
240 * Sets recirculation table number.
241 *
242 * @param t table number to restart
243 * @return NiriraConnTrackTreatmentBuilder object
244 */
245 public NiriraConnTrackTreatmentBuilder table(short t) {
246 this.table = t;
247 return this;
248 }
249
250 /**
251 * Sets IP address for NAT.
252 *
253 * @param ip NAT IP address
254 * @return NiriraConnTrackTreatmentBuilder object
255 */
256 public NiriraConnTrackTreatmentBuilder natIp(IpAddress ip) {
257 this.natAddress = ip;
258 return this;
259 }
260
261 /**
262 * Sets the flag for NAT action.
263 *
264 * @param nat nat action is included if true, no nat action otherwise
265 * @return NiriraConnTrackTreatmentBuilder object
266 */
267 public NiriraConnTrackTreatmentBuilder natAction(boolean nat) {
268 this.natAction = nat;
269 return this;
270 }
271
272 /**
273 * Builds extension treatment for OVS ConnTack and NAT feature.
274 *
275 * @return ExtensionTreatment object
276 */
277 public ExtensionTreatment build() {
278 DriverHandler handler = driverService.createHandler(deviceId);
279 ExtensionTreatmentResolver etr = handler.behaviour(ExtensionTreatmentResolver.class);
280
281 ExtensionTreatment natTreatment
282 = etr.getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type());
283 try {
284 if (natAddress != null) {
285 natTreatment.setPropertyValue(CT_FLAGS, 1);
286 natTreatment.setPropertyValue(CT_PRESENT_FLAGS, buildPresentFlag(false, true));
287 natTreatment.setPropertyValue(CT_IPADDRESS_MIN, natAddress);
288 natTreatment.setPropertyValue(CT_IPADDRESS_MAX, natAddress);
289 } else {
290 natTreatment.setPropertyValue(CT_FLAGS, 0);
291 natTreatment.setPropertyValue(CT_PRESENT_FLAGS, 0);
292 }
293 } catch (Exception e) {
Daniel Parka3ffbdb2018-11-28 13:51:39 +0900294 log.error("Failed to set NAT due to error : {}", e);
sangho072c4dd2017-05-17 10:45:21 +0900295 return null;
296 }
297
298 ExtensionTreatment ctTreatment
299 = etr.getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type());
300 try {
301 List<ExtensionTreatment> nat = new ArrayList<>();
302 if (natAction) {
303 nat.add(natTreatment);
304 }
305 ctTreatment.setPropertyValue(CT_FLAGS, commit ? 1 : 0);
306 ctTreatment.setPropertyValue(CT_ZONE, zone);
307 ctTreatment.setPropertyValue(CT_TABLE, table > -1 ? table : 0xff);
308 ctTreatment.setPropertyValue("nestedActions", nat);
309 } catch (Exception e) {
Daniel Parka3ffbdb2018-11-28 13:51:39 +0900310 log.error("Failed to set CT due to error : {}", e);
sangho072c4dd2017-05-17 10:45:21 +0900311 return null;
312 }
313
314 return ctTreatment;
315 }
316
317 private int buildPresentFlag(boolean isPortPresent, boolean isAddressPresent) {
318
319 int presentFlag = 0;
320
321 if (isPortPresent) {
322 presentFlag = presentFlag | 1 << PORT_MIN_FLAG | 1 << PORT_MAX_FLAG;
323 }
324
325 if (isAddressPresent) {
326 presentFlag = 1 << ADDRESS_MIN_FLAG | 1 << ADDRESS_MAX_FLAG;
327 }
328
329 return presentFlag;
330 }
331 }
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700332}