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