blob: ebd95ede7fccfddefc5367c7f649e823f24f4b33 [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;
19import org.onlab.packet.Ethernet;
sanghofb3b5012016-11-10 15:47:53 +090020import org.onlab.packet.MacAddress;
danielc8620b12015-11-30 15:50:59 +090021import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.behaviour.Pipeliner;
25import org.onosproject.net.behaviour.PipelinerContext;
26import org.onosproject.net.flow.DefaultFlowRule;
27import org.onosproject.net.flow.DefaultTrafficSelector;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.FlowRule;
30import org.onosproject.net.flow.FlowRuleOperations;
31import org.onosproject.net.flow.FlowRuleOperationsContext;
32import org.onosproject.net.flow.FlowRuleService;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.flow.criteria.Criterion;
36import org.onosproject.net.flow.criteria.EthTypeCriterion;
sanghofb3b5012016-11-10 15:47:53 +090037import org.onosproject.net.flow.criteria.IPCriterion;
38import org.onosproject.net.flow.criteria.PortCriterion;
39import org.onosproject.net.flow.criteria.TunnelIdCriterion;
danielc8620b12015-11-30 15:50:59 +090040import org.onosproject.net.flow.criteria.UdpPortCriterion;
sanghofb3b5012016-11-10 15:47:53 +090041import org.onosproject.net.flow.instructions.Instruction;
danielc8620b12015-11-30 15:50:59 +090042import org.onosproject.net.flowobjective.FilteringObjective;
43import org.onosproject.net.flowobjective.FlowObjectiveStore;
44import org.onosproject.net.flowobjective.ForwardingObjective;
45import org.onosproject.net.flowobjective.NextObjective;
46import org.onosproject.net.flowobjective.Objective;
47import org.onosproject.net.flowobjective.ObjectiveError;
48import org.slf4j.Logger;
49
sanghofb3b5012016-11-10 15:47:53 +090050import java.util.Optional;
danielc8620b12015-11-30 15:50:59 +090051
52import static org.slf4j.LoggerFactory.getLogger;
53
54/**
55 * Driver for OpenstackSwitching.
56 */
57public class OpenstackPipeline extends DefaultSingleTablePipeline
58 implements Pipeliner {
59
60 private final Logger log = getLogger(getClass());
61 private CoreService coreService;
62 private ServiceDirectory serviceDirectory;
63 protected FlowObjectiveStore flowObjectiveStore;
64 protected DeviceId deviceId;
65 protected ApplicationId appId;
66 protected FlowRuleService flowRuleService;
67
sanghofb3b5012016-11-10 15:47:53 +090068 protected static final int SRC_VNI_TABLE = 0;
69 protected static final int ACL_TABLE = 1;
70 protected static final int CT_TABLE = 2;
71 protected static final int JUMP_TABLE = 3;
72 protected static final int ROUTING_TABLE = 4;
73 protected static final int FORWARDING_TABLE = 5;
74 protected static final int DUMMY_TABLE = 10;
75 protected static final int LAST_TABLE = FORWARDING_TABLE;
danielc8620b12015-11-30 15:50:59 +090076
77 private static final int DROP_PRIORITY = 0;
sanghofb3b5012016-11-10 15:47:53 +090078 private static final int HIGH_PRIORITY = 30000;
danielc8620b12015-11-30 15:50:59 +090079 private static final int TIME_OUT = 0;
80 private static final int DHCP_SERVER_PORT = 67;
sanghofb3b5012016-11-10 15:47:53 +090081 private static final String VIRTUAL_GATEWAY_MAC = "fe:00:00:00:00:02";
danielc8620b12015-11-30 15:50:59 +090082
83
84 @Override
85 public void init(DeviceId deviceId, PipelinerContext context) {
86 super.init(deviceId, context);
87 this.serviceDirectory = context.directory();
88 this.deviceId = deviceId;
89
90 coreService = serviceDirectory.get(CoreService.class);
91 flowRuleService = serviceDirectory.get(FlowRuleService.class);
92 flowObjectiveStore = context.store();
93
94 appId = coreService.registerApplication(
95 "org.onosproject.driver.OpenstackPipeline");
96
97 initializePipeline();
98 }
99
100 @Override
101 public void filter(FilteringObjective filteringObjective) {
102 super.filter(filteringObjective);
103 }
104
105 @Override
106 public void next(NextObjective nextObjective) {
107 super.next(nextObjective);
108 }
109
110 @Override
111 public void forward(ForwardingObjective forwardingObjective) {
sanghofb3b5012016-11-10 15:47:53 +0900112 FlowRule flowRule;
danielc8620b12015-11-30 15:50:59 +0900113
sanghofb3b5012016-11-10 15:47:53 +0900114 switch (forwardingObjective.flag()) {
115 case SPECIFIC:
116 flowRule = processSpecific(forwardingObjective);
danielc8620b12015-11-30 15:50:59 +0900117 break;
sanghofb3b5012016-11-10 15:47:53 +0900118 case VERSATILE:
119 flowRule = processVersatile(forwardingObjective);
danielc8620b12015-11-30 15:50:59 +0900120 break;
121 default:
122 fail(forwardingObjective, ObjectiveError.UNKNOWN);
sanghofb3b5012016-11-10 15:47:53 +0900123 log.warn("Unknown forwarding flag {}", forwardingObjective.flag());
124 return;
danielc8620b12015-11-30 15:50:59 +0900125 }
126
sanghofb3b5012016-11-10 15:47:53 +0900127 if (forwardingObjective.op().equals(Objective.Operation.ADD)) {
128 applyRules(true, flowRule);
129 } else {
130 applyRules(false, flowRule);
131 }
danielc8620b12015-11-30 15:50:59 +0900132
danielc8620b12015-11-30 15:50:59 +0900133 }
134
135 private void initializePipeline() {
sanghoc6ae1eb2017-02-08 17:14:01 +0900136 //TODO: For now, we do not support security group feature temporarily.
137 connectTables(SRC_VNI_TABLE, JUMP_TABLE); // Table 0 -> Table 3
sanghofb3b5012016-11-10 15:47:53 +0900138 //FIXME CT table needs to be reconstructed using OVS 2.5 connection tracking feature.
139 connectTables(CT_TABLE, JUMP_TABLE); // Table 2 -> Table 3
140 setUpTableMissEntry(ACL_TABLE);
141 setupJumpTable();
danielc8620b12015-11-30 15:50:59 +0900142 }
143
sanghofb3b5012016-11-10 15:47:53 +0900144 private void connectTables(int fromTable, int toTable) {
danielc8620b12015-11-30 15:50:59 +0900145 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
146 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
147
sanghofb3b5012016-11-10 15:47:53 +0900148 treatment.transition(toTable);
149
150 FlowRule flowRule = DefaultFlowRule.builder()
151 .forDevice(deviceId)
152 .withSelector(selector.build())
153 .withTreatment(treatment.build())
154 .withPriority(DROP_PRIORITY)
155 .fromApp(appId)
156 .makePermanent()
157 .forTable(fromTable)
158 .build();
159
160 applyRules(true, flowRule);
161 }
162
163 private void setUpTableMissEntry(int table) {
164 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
165 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
166
167 treatment.drop();
168
169 FlowRule flowRule = DefaultFlowRule.builder()
170 .forDevice(deviceId)
171 .withSelector(selector.build())
172 .withTreatment(treatment.build())
173 .withPriority(DROP_PRIORITY)
174 .fromApp(appId)
175 .makePermanent()
176 .forTable(table)
177 .build();
178
179 applyRules(true, flowRule);
180 }
181
182 private void setupJumpTable() {
183 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
184 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
185
186 selector.matchEthDst(MacAddress.valueOf(VIRTUAL_GATEWAY_MAC));
187 treatment.transition(ROUTING_TABLE);
188
189 FlowRule flowRule = DefaultFlowRule.builder()
190 .forDevice(deviceId)
191 .withSelector(selector.build())
192 .withTreatment(treatment.build())
193 .withPriority(HIGH_PRIORITY)
194 .fromApp(appId)
195 .makePermanent()
196 .forTable(JUMP_TABLE)
197 .build();
198
199 applyRules(true, flowRule);
200
201 selector = DefaultTrafficSelector.builder();
202 treatment = DefaultTrafficTreatment.builder();
203
danielc8620b12015-11-30 15:50:59 +0900204 treatment.transition(FORWARDING_TABLE);
205
sanghofb3b5012016-11-10 15:47:53 +0900206 flowRule = DefaultFlowRule.builder()
danielc8620b12015-11-30 15:50:59 +0900207 .forDevice(deviceId)
208 .withSelector(selector.build())
209 .withTreatment(treatment.build())
210 .withPriority(DROP_PRIORITY)
211 .fromApp(appId)
212 .makePermanent()
sanghofb3b5012016-11-10 15:47:53 +0900213 .forTable(JUMP_TABLE)
danielc8620b12015-11-30 15:50:59 +0900214 .build();
215
sanghofb3b5012016-11-10 15:47:53 +0900216 applyRules(true, flowRule);
sangho90088532016-02-25 18:06:12 +0900217 }
218
danielc8620b12015-11-30 15:50:59 +0900219 private void applyRules(boolean install, FlowRule flowRule) {
220 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
221
222 flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule);
223
224 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
225 @Override
226 public void onSuccess(FlowRuleOperations ops) {
227 log.debug("Provisioned vni or forwarding table");
228 }
229
230 @Override
231 public void onError(FlowRuleOperations ops) {
232 log.debug("Failed to privision vni or forwarding table");
233 }
234 }));
235 }
236
sanghofb3b5012016-11-10 15:47:53 +0900237 private FlowRule processVersatile(ForwardingObjective forwardingObjective) {
danielc8620b12015-11-30 15:50:59 +0900238 log.debug("Processing versatile forwarding objective");
239
240 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
241 .forDevice(deviceId)
242 .withSelector(forwardingObjective.selector())
243 .withTreatment(forwardingObjective.treatment())
244 .withPriority(forwardingObjective.priority())
245 .fromApp(forwardingObjective.appId());
246
247 if (forwardingObjective.permanent()) {
248 ruleBuilder.makePermanent();
249 } else {
250 ruleBuilder.makeTemporary(TIME_OUT);
251 }
252
253 //ARP & DHCP Rule
254 EthTypeCriterion ethCriterion =
255 (EthTypeCriterion) forwardingObjective.selector().getCriterion(Criterion.Type.ETH_TYPE);
256 UdpPortCriterion udpPortCriterion = (UdpPortCriterion) forwardingObjective
257 .selector().getCriterion(Criterion.Type.UDP_DST);
258 if (ethCriterion != null) {
259 if (ethCriterion.ethType().toShort() == Ethernet.TYPE_ARP ||
260 ethCriterion.ethType().toShort() == Ethernet.TYPE_LLDP) {
sanghofb3b5012016-11-10 15:47:53 +0900261 ruleBuilder.forTable(SRC_VNI_TABLE);
262 return ruleBuilder.build();
danielc8620b12015-11-30 15:50:59 +0900263 } else if (udpPortCriterion != null && udpPortCriterion.udpPort().toInt() == DHCP_SERVER_PORT) {
sanghofb3b5012016-11-10 15:47:53 +0900264 ruleBuilder.forTable(SRC_VNI_TABLE);
265 return ruleBuilder.build();
danielc8620b12015-11-30 15:50:59 +0900266 }
267 }
sanghofb3b5012016-11-10 15:47:53 +0900268
269 return null;
danielc8620b12015-11-30 15:50:59 +0900270 }
271
sanghofb3b5012016-11-10 15:47:53 +0900272 private FlowRule processSpecific(ForwardingObjective forwardingObjective) {
danielc8620b12015-11-30 15:50:59 +0900273 log.debug("Processing specific forwarding objective");
274
sanghofb3b5012016-11-10 15:47:53 +0900275 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
276
277
278 Optional<Instruction> group = forwardingObjective.treatment().immediate().stream()
279 .filter(i -> i.type() == Instruction.Type.GROUP).findAny();
280 int tableType = tableType(forwardingObjective);
281 if (tableType != LAST_TABLE && !group.isPresent()) {
282 treatment.transition(nextTable(tableType));
283 }
284 forwardingObjective.treatment().allInstructions().stream()
285 .filter(i -> i.type() != Instruction.Type.NOACTION).forEach(treatment::add);
286
danielc8620b12015-11-30 15:50:59 +0900287 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
288 .forDevice(deviceId)
289 .withSelector(forwardingObjective.selector())
sanghofb3b5012016-11-10 15:47:53 +0900290 .withTreatment(treatment.build())
danielc8620b12015-11-30 15:50:59 +0900291 .withPriority(forwardingObjective.priority())
sanghofb3b5012016-11-10 15:47:53 +0900292 .fromApp(forwardingObjective.appId())
293 .forTable(tableType);
danielc8620b12015-11-30 15:50:59 +0900294
295 if (forwardingObjective.permanent()) {
296 ruleBuilder.makePermanent();
297 } else {
298 ruleBuilder.makeTemporary(TIME_OUT);
299 }
300
sanghofb3b5012016-11-10 15:47:53 +0900301 return ruleBuilder.build();
danielc8620b12015-11-30 15:50:59 +0900302 }
303
sanghofb3b5012016-11-10 15:47:53 +0900304 int tableType(ForwardingObjective fo) {
danielc8620b12015-11-30 15:50:59 +0900305
sanghofb3b5012016-11-10 15:47:53 +0900306 IPCriterion ipSrc = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_SRC);
307 IPCriterion ipDst = (IPCriterion) fo.selector().getCriterion(Criterion.Type.IPV4_DST);
308 TunnelIdCriterion tunnelId =
309 (TunnelIdCriterion) fo.selector().getCriterion(Criterion.Type.TUNNEL_ID);
310 PortCriterion inPort = (PortCriterion) fo.selector().getCriterion(Criterion.Type.IN_PORT);
311 Optional<Instruction> output = fo.treatment().immediate().stream()
312 .filter(i -> i.type() == Instruction.Type.OUTPUT).findAny();
313 Optional<Instruction> group = fo.treatment().immediate().stream()
314 .filter(i -> i.type() == Instruction.Type.GROUP).findAny();
315
316 // TODO: Add the Connection Tracking Table
317 if (inPort != null) {
318 return SRC_VNI_TABLE;
319 } else if (output.isPresent()) {
320 return FORWARDING_TABLE;
321 } else if ((ipSrc != null && ipSrc.ip().prefixLength() == 32 &&
322 ipDst != null && ipDst.ip().prefixLength() == 32) ||
323 (ipSrc != null && ipSrc.ip().prefixLength() == 32 && ipDst == null) ||
324 (ipDst != null && ipDst.ip().prefixLength() == 32 && ipSrc == null)) {
325 return ACL_TABLE;
326 } else if ((tunnelId != null && ipSrc != null && ipDst != null) || group.isPresent()) {
327 return ROUTING_TABLE;
328 }
329
330 return DUMMY_TABLE;
331 }
332
333 int nextTable(int baseTable) {
334
335 return baseTable + 1;
danielc8620b12015-11-30 15:50:59 +0900336 }
337
338 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800339 obj.context().ifPresent(context -> context.onError(obj, error));
danielc8620b12015-11-30 15:50:59 +0900340 }
341}
342