blob: 2e167d65bc05357b33b5ce1132cfcefba96d9654 [file] [log] [blame]
sanghodbee2332017-05-18 09:59:16 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
sanghodbee2332017-05-18 09:59:16 +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 */
16
17package org.onosproject.openstacknetworking.impl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.flow.DefaultFlowRule;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.FlowRuleOperations;
33import org.onosproject.net.flow.FlowRuleOperationsContext;
34import org.onosproject.net.flow.FlowRuleService;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
sanghodbee2332017-05-18 09:59:16 +090037import org.onosproject.openstacknetworking.api.Constants;
38import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService;
Hyunsun Moon0d457362017-06-27 17:19:41 +090039import org.onosproject.openstacknode.api.OpenstackNode;
40import org.onosproject.openstacknode.api.OpenstackNodeEvent;
41import org.onosproject.openstacknode.api.OpenstackNodeListener;
42import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghodbee2332017-05-18 09:59:16 +090043import org.slf4j.Logger;
44
45import java.util.concurrent.ExecutorService;
46import java.util.concurrent.Executors;
47
48import static org.onlab.util.Tools.groupedThreads;
49import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
50import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Sets flow rules directly using FlowRuleService.
54 */
55@Service
56@Component(immediate = true)
57public class OpenstackFlowRuleManager implements OpenstackFlowRuleService {
58
59 private final Logger log = getLogger(getClass());
60
61 private static final int DROP_PRIORITY = 0;
62 private static final int HIGH_PRIORITY = 30000;
sanghodc375372017-06-08 10:41:30 +090063 private static final int TIMEOUT_SNAT_RULE = 60;
sanghodbee2332017-05-18 09:59:16 +090064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected FlowRuleService flowRuleService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected CoreService coreService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected OpenstackNodeService osNodeService;
73
74 private final ExecutorService deviceEventExecutor =
75 Executors.newSingleThreadExecutor(groupedThreads("openstacknetworking", "device-event"));
Hyunsun Moon0d457362017-06-27 17:19:41 +090076 private final OpenstackNodeListener internalNodeListener = new InternalOpenstackNodeListener();
sanghodbee2332017-05-18 09:59:16 +090077
78 private ApplicationId appId;
79
80 @Activate
81 protected void activate() {
82 appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID);
83 coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID);
84 osNodeService.addListener(internalNodeListener);
85
sanghoc395d0f2017-07-11 10:05:25 +090086 osNodeService.completeNodes(OpenstackNode.NodeType.COMPUTE)
87 .forEach(node -> initializePipeline(node.intgBridge()));
88
sanghodbee2332017-05-18 09:59:16 +090089 log.info("Started");
90 }
91
92 @Deactivate
93 protected void deactivate() {
94 osNodeService.removeListener(internalNodeListener);
95 deviceEventExecutor.shutdown();
96
97 log.info("Stopped");
98 }
99
sanghodc375372017-06-08 10:41:30 +0900100
sanghodbee2332017-05-18 09:59:16 +0900101 @Override
sanghodc375372017-06-08 10:41:30 +0900102 public void setRule(ApplicationId appId,
103 DeviceId deviceId,
104 TrafficSelector selector,
105 TrafficTreatment treatment,
106 int priority,
107 int tableType,
108 boolean install) {
sanghodbee2332017-05-18 09:59:16 +0900109
sanghodc375372017-06-08 10:41:30 +0900110 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
sanghodbee2332017-05-18 09:59:16 +0900111 .forDevice(deviceId)
sanghodc375372017-06-08 10:41:30 +0900112 .withSelector(selector)
113 .withTreatment(treatment)
114 .withPriority(priority)
115 .fromApp(appId)
sanghodbee2332017-05-18 09:59:16 +0900116 .forTable(tableType);
117
sanghodc375372017-06-08 10:41:30 +0900118 if (priority == Constants.PRIORITY_SNAT_RULE) {
119 flowRuleBuilder.makeTemporary(TIMEOUT_SNAT_RULE);
sanghodbee2332017-05-18 09:59:16 +0900120 } else {
sanghodc375372017-06-08 10:41:30 +0900121 flowRuleBuilder.makePermanent();
sanghodbee2332017-05-18 09:59:16 +0900122 }
123
sanghodc375372017-06-08 10:41:30 +0900124 applyRule(flowRuleBuilder.build(), install);
sanghodbee2332017-05-18 09:59:16 +0900125 }
126
sanghodc375372017-06-08 10:41:30 +0900127 private void applyRule(FlowRule flowRule, boolean install) {
sanghodbee2332017-05-18 09:59:16 +0900128 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
129
130 flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule);
131
132 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
133 @Override
134 public void onSuccess(FlowRuleOperations ops) {
135 log.debug("Provisioned vni or forwarding table");
136 }
137
138 @Override
139 public void onError(FlowRuleOperations ops) {
140 log.debug("Failed to privision vni or forwarding table");
141 }
142 }));
143 }
144
145 private void initializePipeline(DeviceId deviceId) {
146 connectTables(deviceId, Constants.SRC_VNI_TABLE, Constants.ACL_TABLE);
147 connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900148 setupJumpTable(deviceId);
149 }
150
151 private void connectTables(DeviceId deviceId, int fromTable, int toTable) {
152 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
153 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
154
155 treatment.transition(toTable);
156
157 FlowRule flowRule = DefaultFlowRule.builder()
158 .forDevice(deviceId)
159 .withSelector(selector.build())
160 .withTreatment(treatment.build())
161 .withPriority(DROP_PRIORITY)
162 .fromApp(appId)
163 .makePermanent()
164 .forTable(fromTable)
165 .build();
166
sanghodc375372017-06-08 10:41:30 +0900167 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900168 }
169
170 private void setUpTableMissEntry(DeviceId deviceId, int table) {
171 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
172 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
173
174 treatment.drop();
175
176 FlowRule flowRule = DefaultFlowRule.builder()
177 .forDevice(deviceId)
178 .withSelector(selector.build())
179 .withTreatment(treatment.build())
180 .withPriority(DROP_PRIORITY)
181 .fromApp(appId)
182 .makePermanent()
183 .forTable(table)
184 .build();
185
sanghodc375372017-06-08 10:41:30 +0900186 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900187 }
188
189 private void setupJumpTable(DeviceId deviceId) {
190 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
191 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
192
193 selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC);
194 treatment.transition(Constants.ROUTING_TABLE);
195
196 FlowRule flowRule = DefaultFlowRule.builder()
197 .forDevice(deviceId)
198 .withSelector(selector.build())
199 .withTreatment(treatment.build())
200 .withPriority(HIGH_PRIORITY)
201 .fromApp(appId)
202 .makePermanent()
203 .forTable(Constants.JUMP_TABLE)
204 .build();
205
sanghodc375372017-06-08 10:41:30 +0900206 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900207
208 selector = DefaultTrafficSelector.builder();
209 treatment = DefaultTrafficTreatment.builder();
210
211 treatment.transition(Constants.FORWARDING_TABLE);
212
213 flowRule = DefaultFlowRule.builder()
214 .forDevice(deviceId)
215 .withSelector(selector.build())
216 .withTreatment(treatment.build())
217 .withPriority(DROP_PRIORITY)
218 .fromApp(appId)
219 .makePermanent()
220 .forTable(Constants.JUMP_TABLE)
221 .build();
222
sanghodc375372017-06-08 10:41:30 +0900223 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900224 }
225
226 private class InternalOpenstackNodeListener implements OpenstackNodeListener {
227
228 @Override
229 public void event(OpenstackNodeEvent event) {
230 OpenstackNode osNode = event.subject();
231 // TODO check leadership of the node and make only the leader process
232
233 switch (event.type()) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900234 case OPENSTACK_NODE_COMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900235 deviceEventExecutor.execute(() -> {
236 log.info("COMPLETE node {} is detected", osNode.hostname());
sanghodc375372017-06-08 10:41:30 +0900237 processCompleteNode(event.subject());
sanghodbee2332017-05-18 09:59:16 +0900238 });
239 break;
Hyunsun Moon0d457362017-06-27 17:19:41 +0900240 case OPENSTACK_NODE_CREATED:
241 case OPENSTACK_NODE_UPDATED:
242 case OPENSTACK_NODE_REMOVED:
243 case OPENSTACK_NODE_INCOMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900244 default:
Hyunsun Moon0d457362017-06-27 17:19:41 +0900245 // do nothing
sanghodbee2332017-05-18 09:59:16 +0900246 break;
247 }
248 }
249
250 private void processCompleteNode(OpenstackNode osNode) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900251 if (osNode.type().equals(OpenstackNode.NodeType.COMPUTE)) {
252 initializePipeline(osNode.intgBridge());
sanghodbee2332017-05-18 09:59:16 +0900253 }
254 }
255 }
256}