blob: b5abaecd3ba4cc004684a2168979b4269bdabc49 [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 */
Jian Li5ecfd1a2018-12-10 11:41:03 +090079 public static NiriraConnTrackTreatmentBuilder
80 niciraConnTrackTreatmentBuilder(DriverService ds, DeviceId id) {
sangho072c4dd2017-05-17 10:45:21 +090081 return new NiriraConnTrackTreatmentBuilder(ds, id);
82 }
83
84 /**
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070085 * 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);
sangho072c4dd2017-05-17 10:45:21 +090096 if (device != null && !device.is(ExtensionTreatmentResolver.class)) {
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070097 log.error("The extension treatment is not supported");
98 return null;
99 }
sangho072c4dd2017-05-17 10:45:21 +0900100
Ray Milkey74e59132018-01-17 15:24:52 -0800101 if (device == null) {
102 return null;
103 }
104
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700105 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
Jian Li5ecfd1a2018-12-10 11:41:03 +0900106 ExtensionTreatment treatment =
107 resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700108 try {
109 treatment.setPropertyValue(TUNNEL_DST, remoteIp);
110 return treatment;
111 } catch (ExtensionPropertyException e) {
Jian Li5ecfd1a2018-12-10 11:41:03 +0900112 log.warn("Failed to get tunnelDst extension treatment for {} " +
113 "because of {}", deviceId, e);
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700114 return null;
115 }
116 }
sangho072c4dd2017-05-17 10:45:21 +0900117
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 Li5ecfd1a2018-12-10 11:41:03 +0900127 public static ExtensionSelector buildCtExtensionSelector(DriverService driverService,
128 DeviceId deviceId,
129 long ctState,
130 long ctSateMask) {
sangho072c4dd2017-05-17 10:45:21 +0900131 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 Parka3ffbdb2018-11-28 13:51:39 +0900140 log.error("Failed to set nicira match CT state because of {}", e);
sangho072c4dd2017-05-17 10:45:21 +0900141 return null;
142 }
143
144 return extensionSelector;
145 }
146
147 /**
sangho072c4dd2017-05-17 10:45:21 +0900148 * 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 Li5ecfd1a2018-12-10 11:41:03 +0900155 public static long computeCtStateFlag(boolean isTracking,
156 boolean isNew,
157 boolean isEstablished) {
sangho072c4dd2017-05-17 10:45:21 +0900158 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 Li5ecfd1a2018-12-10 11:41:03 +0900185 public static long computeCtMaskFlag(boolean isTracking,
186 boolean isNew,
187 boolean isEstablished) {
sangho072c4dd2017-05-17 10:45:21 +0900188 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 Li5ecfd1a2018-12-10 11:41:03 +0900221 private NiriraConnTrackTreatmentBuilder(DriverService driverService,
222 DeviceId deviceId) {
sangho072c4dd2017-05-17 10:45:21 +0900223 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 Li5ecfd1a2018-12-10 11:41:03 +0900291 ExtensionTreatment natTreatment = etr.getExtensionInstruction(
292 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type());
sangho072c4dd2017-05-17 10:45:21 +0900293 try {
294 if (natAddress != null) {
295 natTreatment.setPropertyValue(CT_FLAGS, 1);
Jian Li5ecfd1a2018-12-10 11:41:03 +0900296 natTreatment.setPropertyValue(CT_PRESENT_FLAGS,
297 buildPresentFlag(false, true));
sangho072c4dd2017-05-17 10:45:21 +0900298 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 Parka3ffbdb2018-11-28 13:51:39 +0900305 log.error("Failed to set NAT due to error : {}", e);
sangho072c4dd2017-05-17 10:45:21 +0900306 return null;
307 }
308
Jian Li5ecfd1a2018-12-10 11:41:03 +0900309 ExtensionTreatment ctTreatment = etr.getExtensionInstruction(
310 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type());
sangho072c4dd2017-05-17 10:45:21 +0900311 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 Parka3ffbdb2018-11-28 13:51:39 +0900321 log.error("Failed to set CT due to error : {}", e);
sangho072c4dd2017-05-17 10:45:21 +0900322 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 Moonb3eb84d2016-07-27 19:10:52 -0700343}