blob: dbb5df51b359f7f75ef4e468f21edcafc5d8848b [file] [log] [blame]
danielc8620b12015-11-30 15:50:59 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
danielc8620b12015-11-30 15:50:59 +09003 *
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 Moon21d17ba2015-12-04 16:16:25 -080016package org.onosproject.driver.pipeline;
danielc8620b12015-11-30 15:50:59 +090017
18import org.onlab.osgi.ServiceDirectory;
sanghofb3b5012016-11-10 15:47:53 +090019import org.onlab.packet.MacAddress;
danielc8620b12015-11-30 15:50:59 +090020import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.behaviour.Pipeliner;
24import org.onosproject.net.behaviour.PipelinerContext;
25import org.onosproject.net.flow.DefaultFlowRule;
26import org.onosproject.net.flow.DefaultTrafficSelector;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.FlowRuleOperations;
30import org.onosproject.net.flow.FlowRuleOperationsContext;
31import org.onosproject.net.flow.FlowRuleService;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criterion;
sanghofb3b5012016-11-10 15:47:53 +090035import org.onosproject.net.flow.criteria.IPCriterion;
36import org.onosproject.net.flow.criteria.PortCriterion;
37import org.onosproject.net.flow.criteria.TunnelIdCriterion;
sanghofb3b5012016-11-10 15:47:53 +090038import org.onosproject.net.flow.instructions.Instruction;
danielc8620b12015-11-30 15:50:59 +090039import org.onosproject.net.flowobjective.FilteringObjective;
40import org.onosproject.net.flowobjective.FlowObjectiveStore;
41import org.onosproject.net.flowobjective.ForwardingObjective;
42import org.onosproject.net.flowobjective.NextObjective;
43import org.onosproject.net.flowobjective.Objective;
44import org.onosproject.net.flowobjective.ObjectiveError;
45import org.slf4j.Logger;
46
sanghofb3b5012016-11-10 15:47:53 +090047import java.util.Optional;
danielc8620b12015-11-30 15:50:59 +090048
49import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Driver for OpenstackSwitching.
53 */
54public class OpenstackPipeline extends DefaultSingleTablePipeline
55 implements Pipeliner {
56
57 private final Logger log = getLogger(getClass());
danielc8620b12015-11-30 15:50:59 +090058 protected FlowObjectiveStore flowObjectiveStore;
59 protected DeviceId deviceId;
60 protected ApplicationId appId;
61 protected FlowRuleService flowRuleService;
62
Hyunsun Moon4e252f22017-02-18 02:07:49 +090063 private static final int SRC_VNI_TABLE = 0;
sangho6a9ff0d2017-03-27 11:23:37 +090064 private static final int ACL_TABLE = 1;
65 private static final int JUMP_TABLE = 2;
66 private static final int ROUTING_TABLE = 3;
67 private static final int FORWARDING_TABLE = 4;
Hyunsun Moon4e252f22017-02-18 02:07:49 +090068 private static final int DUMMY_TABLE = 10;
69 private static final int LAST_TABLE = FORWARDING_TABLE;
danielc8620b12015-11-30 15:50:59 +090070
71 private static final int DROP_PRIORITY = 0;
sanghofb3b5012016-11-10 15:47:53 +090072 private static final int HIGH_PRIORITY = 30000;
danielc8620b12015-11-30 15:50:59 +090073 private static final int TIME_OUT = 0;
sanghofb3b5012016-11-10 15:47:53 +090074 private static final String VIRTUAL_GATEWAY_MAC = "fe:00:00:00:00:02";
danielc8620b12015-11-30 15:50:59 +090075
76
77 @Override
78 public void init(DeviceId deviceId, PipelinerContext context) {
79 super.init(deviceId, context);
Hyunsun Moon4e252f22017-02-18 02:07:49 +090080 ServiceDirectory serviceDirectory = context.directory();
danielc8620b12015-11-30 15:50:59 +090081 this.deviceId = deviceId;
82
Hyunsun Moon4e252f22017-02-18 02:07:49 +090083 CoreService coreService = serviceDirectory.get(CoreService.class);
danielc8620b12015-11-30 15:50:59 +090084 flowRuleService = serviceDirectory.get(FlowRuleService.class);
85 flowObjectiveStore = context.store();
86
87 appId = coreService.registerApplication(
88 "org.onosproject.driver.OpenstackPipeline");
89
90 initializePipeline();
91 }
92
93 @Override
94 public void filter(FilteringObjective filteringObjective) {
95 super.filter(filteringObjective);
96 }
97
98 @Override
99 public void next(NextObjective nextObjective) {
100 super.next(nextObjective);
101 }
102
103 @Override
104 public void forward(ForwardingObjective forwardingObjective) {
sanghofb3b5012016-11-10 15:47:53 +0900105 FlowRule flowRule;
danielc8620b12015-11-30 15:50:59 +0900106
sanghofb3b5012016-11-10 15:47:53 +0900107 switch (forwardingObjective.flag()) {
108 case SPECIFIC:
109 flowRule = processSpecific(forwardingObjective);
danielc8620b12015-11-30 15:50:59 +0900110 break;
sanghofb3b5012016-11-10 15:47:53 +0900111 case VERSATILE:
112 flowRule = processVersatile(forwardingObjective);
danielc8620b12015-11-30 15:50:59 +0900113 break;
114 default:
115 fail(forwardingObjective, ObjectiveError.UNKNOWN);
sanghofb3b5012016-11-10 15:47:53 +0900116 log.warn("Unknown forwarding flag {}", forwardingObjective.flag());
117 return;
danielc8620b12015-11-30 15:50:59 +0900118 }
119
sanghofb3b5012016-11-10 15:47:53 +0900120 if (forwardingObjective.op().equals(Objective.Operation.ADD)) {
121 applyRules(true, flowRule);
122 } else {
123 applyRules(false, flowRule);
124 }
danielc8620b12015-11-30 15:50:59 +0900125
danielc8620b12015-11-30 15:50:59 +0900126 }
127
128 private void initializePipeline() {
sangho6a9ff0d2017-03-27 11:23:37 +0900129 connectTables(SRC_VNI_TABLE, ACL_TABLE);
130 connectTables(ACL_TABLE, JUMP_TABLE);
131 setUpTableMissEntry(ACL_TABLE);
sanghofb3b5012016-11-10 15:47:53 +0900132 setupJumpTable();
danielc8620b12015-11-30 15:50:59 +0900133 }
134
sanghofb3b5012016-11-10 15:47:53 +0900135 private void connectTables(int fromTable, int toTable) {
danielc8620b12015-11-30 15:50:59 +0900136 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
137 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
138
sanghofb3b5012016-11-10 15:47:53 +0900139 treatment.transition(toTable);
140
141 FlowRule flowRule = DefaultFlowRule.builder()
142 .forDevice(deviceId)
143 .withSelector(selector.build())
144 .withTreatment(treatment.build())
145 .withPriority(DROP_PRIORITY)
146 .fromApp(appId)
147 .makePermanent()
148 .forTable(fromTable)
149 .build();
150
151 applyRules(true, flowRule);
152 }
153
sangho6a9ff0d2017-03-27 11:23:37 +0900154 private void setUpTableMissEntry(int table) {
155 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
156 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
157
158 treatment.drop();
159
160 FlowRule flowRule = DefaultFlowRule.builder()
161 .forDevice(deviceId)
162 .withSelector(selector.build())
163 .withTreatment(treatment.build())
164 .withPriority(DROP_PRIORITY)
165 .fromApp(appId)
166 .makePermanent()
167 .forTable(table)
168 .build();
169
170 applyRules(true, flowRule);
171 }
172
sanghofb3b5012016-11-10 15:47:53 +0900173 private void setupJumpTable() {
174 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
175 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
176
177 selector.matchEthDst(MacAddress.valueOf(VIRTUAL_GATEWAY_MAC));
178 treatment.transition(ROUTING_TABLE);
179
180 FlowRule flowRule = DefaultFlowRule.builder()
181 .forDevice(deviceId)
182 .withSelector(selector.build())
183 .withTreatment(treatment.build())
184 .withPriority(HIGH_PRIORITY)
185 .fromApp(appId)
186 .makePermanent()
187 .forTable(JUMP_TABLE)
188 .build();
189
190 applyRules(true, flowRule);
191
192 selector = DefaultTrafficSelector.builder();
193 treatment = DefaultTrafficTreatment.builder();
194
danielc8620b12015-11-30 15:50:59 +0900195 treatment.transition(FORWARDING_TABLE);
196
sanghofb3b5012016-11-10 15:47:53 +0900197 flowRule = DefaultFlowRule.builder()
danielc8620b12015-11-30 15:50:59 +0900198 .forDevice(deviceId)
199 .withSelector(selector.build())
200 .withTreatment(treatment.build())
201 .withPriority(DROP_PRIORITY)
202 .fromApp(appId)
203 .makePermanent()
sanghofb3b5012016-11-10 15:47:53 +0900204 .forTable(JUMP_TABLE)
danielc8620b12015-11-30 15:50:59 +0900205 .build();
206
sanghofb3b5012016-11-10 15:47:53 +0900207 applyRules(true, flowRule);
sangho90088532016-02-25 18:06:12 +0900208 }
209
danielc8620b12015-11-30 15:50:59 +0900210 private void applyRules(boolean install, FlowRule flowRule) {
211 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
212
213 flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule);
214
215 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
216 @Override
217 public void onSuccess(FlowRuleOperations ops) {
218 log.debug("Provisioned vni or forwarding table");
219 }
220
221 @Override
222 public void onError(FlowRuleOperations ops) {
223 log.debug("Failed to privision vni or forwarding table");
224 }
225 }));
226 }
227
sanghofb3b5012016-11-10 15:47:53 +0900228 private FlowRule processVersatile(ForwardingObjective forwardingObjective) {
danielc8620b12015-11-30 15:50:59 +0900229 log.debug("Processing versatile forwarding objective");
230
231 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
232 .forDevice(deviceId)
233 .withSelector(forwardingObjective.selector())
234 .withTreatment(forwardingObjective.treatment())
235 .withPriority(forwardingObjective.priority())
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900236 .fromApp(forwardingObjective.appId())
237 .forTable(SRC_VNI_TABLE);
danielc8620b12015-11-30 15:50:59 +0900238
239 if (forwardingObjective.permanent()) {
240 ruleBuilder.makePermanent();
241 } else {
242 ruleBuilder.makeTemporary(TIME_OUT);
243 }
244
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900245 return ruleBuilder.build();
danielc8620b12015-11-30 15:50:59 +0900246 }
247
sanghofb3b5012016-11-10 15:47:53 +0900248 private FlowRule processSpecific(ForwardingObjective forwardingObjective) {
danielc8620b12015-11-30 15:50:59 +0900249 log.debug("Processing specific forwarding objective");
250
sanghofb3b5012016-11-10 15:47:53 +0900251 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
sanghofb3b5012016-11-10 15:47:53 +0900252 Optional<Instruction> group = forwardingObjective.treatment().immediate().stream()
253 .filter(i -> i.type() == Instruction.Type.GROUP).findAny();
254 int tableType = tableType(forwardingObjective);
255 if (tableType != LAST_TABLE && !group.isPresent()) {
256 treatment.transition(nextTable(tableType));
257 }
258 forwardingObjective.treatment().allInstructions().stream()
259 .filter(i -> i.type() != Instruction.Type.NOACTION).forEach(treatment::add);
260
danielc8620b12015-11-30 15:50:59 +0900261 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
262 .forDevice(deviceId)
263 .withSelector(forwardingObjective.selector())
sanghofb3b5012016-11-10 15:47:53 +0900264 .withTreatment(treatment.build())
danielc8620b12015-11-30 15:50:59 +0900265 .withPriority(forwardingObjective.priority())
sanghofb3b5012016-11-10 15:47:53 +0900266 .fromApp(forwardingObjective.appId())
267 .forTable(tableType);
danielc8620b12015-11-30 15:50:59 +0900268
269 if (forwardingObjective.permanent()) {
270 ruleBuilder.makePermanent();
271 } else {
272 ruleBuilder.makeTemporary(TIME_OUT);
273 }
274
sanghofb3b5012016-11-10 15:47:53 +0900275 return ruleBuilder.build();
danielc8620b12015-11-30 15:50:59 +0900276 }
277
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900278 private int tableType(ForwardingObjective fo) {
danielc8620b12015-11-30 15:50:59 +0900279
sanghofb3b5012016-11-10 15:47:53 +0900280 IPCriterion ipSrc = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_SRC);
281 IPCriterion ipDst = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_DST);
282 TunnelIdCriterion tunnelId =
283 (TunnelIdCriterion) fo.selector().getCriterion(Criterion.Type.TUNNEL_ID);
284 PortCriterion inPort = (PortCriterion) fo.selector().getCriterion(Criterion.Type.IN_PORT);
285 Optional<Instruction> output = fo.treatment().immediate().stream()
286 .filter(i -> i.type() == Instruction.Type.OUTPUT).findAny();
287 Optional<Instruction> group = fo.treatment().immediate().stream()
288 .filter(i -> i.type() == Instruction.Type.GROUP).findAny();
289
290 // TODO: Add the Connection Tracking Table
291 if (inPort != null) {
292 return SRC_VNI_TABLE;
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900293 } else if ((tunnelId != null && ipSrc != null && ipDst != null) ||
294 (ipSrc != null && group.isPresent())) {
sanghofb3b5012016-11-10 15:47:53 +0900295 return ROUTING_TABLE;
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900296 } else if (output.isPresent() || (ipDst != null && group.isPresent())) {
297 return FORWARDING_TABLE;
sangho6a9ff0d2017-03-27 11:23:37 +0900298 } else if ((ipSrc != null && ipSrc.ip().prefixLength() == 32 &&
299 ipDst != null && ipDst.ip().prefixLength() == 32) ||
300 (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst == null) ||
301 (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc == null) ||
302 (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc != null) ||
303 (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst != null)) {
304 return ACL_TABLE;
sanghofb3b5012016-11-10 15:47:53 +0900305 }
306
307 return DUMMY_TABLE;
308 }
309
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900310 private int nextTable(int baseTable) {
sanghofb3b5012016-11-10 15:47:53 +0900311 return baseTable + 1;
danielc8620b12015-11-30 15:50:59 +0900312 }
313
314 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800315 obj.context().ifPresent(context -> context.onError(obj, error));
danielc8620b12015-11-30 15:50:59 +0900316 }
317}
318