blob: ebe8b39670a1698dacebfbec9a852f94dec3a758 [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;
daniel parkac348902017-04-26 13:49:05 +090038import org.onosproject.net.flow.criteria.VlanIdCriterion;
sanghofb3b5012016-11-10 15:47:53 +090039import org.onosproject.net.flow.instructions.Instruction;
danielc8620b12015-11-30 15:50:59 +090040import org.onosproject.net.flowobjective.FilteringObjective;
41import org.onosproject.net.flowobjective.FlowObjectiveStore;
42import org.onosproject.net.flowobjective.ForwardingObjective;
43import org.onosproject.net.flowobjective.NextObjective;
44import org.onosproject.net.flowobjective.Objective;
45import org.onosproject.net.flowobjective.ObjectiveError;
46import org.slf4j.Logger;
47
sanghofb3b5012016-11-10 15:47:53 +090048import java.util.Optional;
danielc8620b12015-11-30 15:50:59 +090049
50import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Driver for OpenstackSwitching.
54 */
55public class OpenstackPipeline extends DefaultSingleTablePipeline
56 implements Pipeliner {
57
58 private final Logger log = getLogger(getClass());
danielc8620b12015-11-30 15:50:59 +090059 protected FlowObjectiveStore flowObjectiveStore;
60 protected DeviceId deviceId;
61 protected ApplicationId appId;
62 protected FlowRuleService flowRuleService;
63
Hyunsun Moon4e252f22017-02-18 02:07:49 +090064 private static final int SRC_VNI_TABLE = 0;
sangho6a9ff0d2017-03-27 11:23:37 +090065 private static final int ACL_TABLE = 1;
66 private static final int JUMP_TABLE = 2;
67 private static final int ROUTING_TABLE = 3;
68 private static final int FORWARDING_TABLE = 4;
Hyunsun Moon4e252f22017-02-18 02:07:49 +090069 private static final int DUMMY_TABLE = 10;
70 private static final int LAST_TABLE = FORWARDING_TABLE;
danielc8620b12015-11-30 15:50:59 +090071
72 private static final int DROP_PRIORITY = 0;
sanghofb3b5012016-11-10 15:47:53 +090073 private static final int HIGH_PRIORITY = 30000;
danielc8620b12015-11-30 15:50:59 +090074 private static final int TIME_OUT = 0;
sanghofb3b5012016-11-10 15:47:53 +090075 private static final String VIRTUAL_GATEWAY_MAC = "fe:00:00:00:00:02";
danielc8620b12015-11-30 15:50:59 +090076
77
78 @Override
79 public void init(DeviceId deviceId, PipelinerContext context) {
80 super.init(deviceId, context);
Hyunsun Moon4e252f22017-02-18 02:07:49 +090081 ServiceDirectory serviceDirectory = context.directory();
danielc8620b12015-11-30 15:50:59 +090082 this.deviceId = deviceId;
83
Hyunsun Moon4e252f22017-02-18 02:07:49 +090084 CoreService coreService = serviceDirectory.get(CoreService.class);
danielc8620b12015-11-30 15:50:59 +090085 flowRuleService = serviceDirectory.get(FlowRuleService.class);
86 flowObjectiveStore = context.store();
87
88 appId = coreService.registerApplication(
89 "org.onosproject.driver.OpenstackPipeline");
90
91 initializePipeline();
92 }
93
94 @Override
95 public void filter(FilteringObjective filteringObjective) {
96 super.filter(filteringObjective);
97 }
98
99 @Override
100 public void next(NextObjective nextObjective) {
101 super.next(nextObjective);
102 }
103
104 @Override
105 public void forward(ForwardingObjective forwardingObjective) {
sanghofb3b5012016-11-10 15:47:53 +0900106 FlowRule flowRule;
danielc8620b12015-11-30 15:50:59 +0900107
sanghofb3b5012016-11-10 15:47:53 +0900108 switch (forwardingObjective.flag()) {
109 case SPECIFIC:
110 flowRule = processSpecific(forwardingObjective);
danielc8620b12015-11-30 15:50:59 +0900111 break;
sanghofb3b5012016-11-10 15:47:53 +0900112 case VERSATILE:
113 flowRule = processVersatile(forwardingObjective);
danielc8620b12015-11-30 15:50:59 +0900114 break;
115 default:
116 fail(forwardingObjective, ObjectiveError.UNKNOWN);
sanghofb3b5012016-11-10 15:47:53 +0900117 log.warn("Unknown forwarding flag {}", forwardingObjective.flag());
118 return;
danielc8620b12015-11-30 15:50:59 +0900119 }
120
sanghofb3b5012016-11-10 15:47:53 +0900121 if (forwardingObjective.op().equals(Objective.Operation.ADD)) {
122 applyRules(true, flowRule);
123 } else {
124 applyRules(false, flowRule);
125 }
danielc8620b12015-11-30 15:50:59 +0900126
danielc8620b12015-11-30 15:50:59 +0900127 }
128
129 private void initializePipeline() {
sangho6a9ff0d2017-03-27 11:23:37 +0900130 connectTables(SRC_VNI_TABLE, ACL_TABLE);
131 connectTables(ACL_TABLE, JUMP_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);
daniel parkac348902017-04-26 13:49:05 +0900284 VlanIdCriterion vlanId = (VlanIdCriterion) fo.selector().getCriterion(Criterion.Type.VLAN_VID);
sanghofb3b5012016-11-10 15:47:53 +0900285 PortCriterion inPort = (PortCriterion) fo.selector().getCriterion(Criterion.Type.IN_PORT);
286 Optional<Instruction> output = fo.treatment().immediate().stream()
287 .filter(i -> i.type() == Instruction.Type.OUTPUT).findAny();
288 Optional<Instruction> group = fo.treatment().immediate().stream()
289 .filter(i -> i.type() == Instruction.Type.GROUP).findAny();
290
291 // TODO: Add the Connection Tracking Table
292 if (inPort != null) {
293 return SRC_VNI_TABLE;
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900294 } else if ((tunnelId != null && ipSrc != null && ipDst != null) ||
daniel parkac348902017-04-26 13:49:05 +0900295 (vlanId != null && ipSrc != null && ipDst != null) ||
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900296 (ipSrc != null && group.isPresent())) {
sanghofb3b5012016-11-10 15:47:53 +0900297 return ROUTING_TABLE;
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900298 } else if (output.isPresent() || (ipDst != null && group.isPresent())) {
299 return FORWARDING_TABLE;
sangho6a9ff0d2017-03-27 11:23:37 +0900300 } else if ((ipSrc != null && ipSrc.ip().prefixLength() == 32 &&
301 ipDst != null && ipDst.ip().prefixLength() == 32) ||
302 (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst == null) ||
303 (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc == null) ||
304 (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc != null) ||
305 (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst != null)) {
306 return ACL_TABLE;
sanghofb3b5012016-11-10 15:47:53 +0900307 }
308
309 return DUMMY_TABLE;
310 }
311
Hyunsun Moon4e252f22017-02-18 02:07:49 +0900312 private int nextTable(int baseTable) {
sanghofb3b5012016-11-10 15:47:53 +0900313 return baseTable + 1;
danielc8620b12015-11-30 15:50:59 +0900314 }
315
316 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800317 obj.context().ifPresent(context -> context.onError(obj, error));
danielc8620b12015-11-30 15:50:59 +0900318 }
319}
320