blob: a1289c4625885ae42427035ccbede00dfb7c84b8 [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;
Jian Lia0778172018-07-16 22:50:19 +090025import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.LeadershipService;
27import org.onosproject.cluster.NodeId;
sanghodbee2332017-05-18 09:59:16 +090028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.FlowRuleOperations;
36import org.onosproject.net.flow.FlowRuleOperationsContext;
37import org.onosproject.net.flow.FlowRuleService;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
sanghodbee2332017-05-18 09:59:16 +090040import org.onosproject.openstacknetworking.api.Constants;
41import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService;
Hyunsun Moon0d457362017-06-27 17:19:41 +090042import org.onosproject.openstacknode.api.OpenstackNode;
43import org.onosproject.openstacknode.api.OpenstackNodeEvent;
44import org.onosproject.openstacknode.api.OpenstackNodeListener;
45import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghodbee2332017-05-18 09:59:16 +090046import org.slf4j.Logger;
47
Jian Lia0778172018-07-16 22:50:19 +090048import java.util.Objects;
sanghodbee2332017-05-18 09:59:16 +090049import java.util.concurrent.ExecutorService;
50import java.util.concurrent.Executors;
51
52import static org.onlab.util.Tools.groupedThreads;
53import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
Jian Lia0778172018-07-16 22:50:19 +090054import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
sanghodbee2332017-05-18 09:59:16 +090055import static org.slf4j.LoggerFactory.getLogger;
56
57/**
58 * Sets flow rules directly using FlowRuleService.
59 */
60@Service
61@Component(immediate = true)
62public class OpenstackFlowRuleManager implements OpenstackFlowRuleService {
63
64 private final Logger log = getLogger(getClass());
65
66 private static final int DROP_PRIORITY = 0;
67 private static final int HIGH_PRIORITY = 30000;
sanghodc375372017-06-08 10:41:30 +090068 private static final int TIMEOUT_SNAT_RULE = 60;
sanghodbee2332017-05-18 09:59:16 +090069
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected FlowRuleService flowRuleService;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected CoreService coreService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Lia0778172018-07-16 22:50:19 +090077 protected ClusterService clusterService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected LeadershipService leadershipService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
sanghodbee2332017-05-18 09:59:16 +090083 protected OpenstackNodeService osNodeService;
84
85 private final ExecutorService deviceEventExecutor =
Jian Lia0778172018-07-16 22:50:19 +090086 Executors.newSingleThreadExecutor(groupedThreads(
87 getClass().getSimpleName(), "device-event"));
88 private final OpenstackNodeListener internalNodeListener =
89 new InternalOpenstackNodeListener();
sanghodbee2332017-05-18 09:59:16 +090090
91 private ApplicationId appId;
Jian Lia0778172018-07-16 22:50:19 +090092 private NodeId localNodeId;
sanghodbee2332017-05-18 09:59:16 +090093
94 @Activate
95 protected void activate() {
96 appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID);
97 coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID);
98 osNodeService.addListener(internalNodeListener);
Jian Lia0778172018-07-16 22:50:19 +090099 localNodeId = clusterService.getLocalNode().id();
100 leadershipService.runForLeadership(appId.name());
101 osNodeService.completeNodes(COMPUTE)
sanghoc395d0f2017-07-11 10:05:25 +0900102 .forEach(node -> initializePipeline(node.intgBridge()));
103
sanghodbee2332017-05-18 09:59:16 +0900104 log.info("Started");
105 }
106
107 @Deactivate
108 protected void deactivate() {
109 osNodeService.removeListener(internalNodeListener);
Jian Lia0778172018-07-16 22:50:19 +0900110 leadershipService.withdraw(appId.name());
sanghodbee2332017-05-18 09:59:16 +0900111 deviceEventExecutor.shutdown();
112
113 log.info("Stopped");
114 }
115
116 @Override
sanghodc375372017-06-08 10:41:30 +0900117 public void setRule(ApplicationId appId,
Jian Lia0778172018-07-16 22:50:19 +0900118 DeviceId deviceId,
119 TrafficSelector selector,
120 TrafficTreatment treatment,
121 int priority,
122 int tableType,
123 boolean install) {
sanghodbee2332017-05-18 09:59:16 +0900124
sanghodc375372017-06-08 10:41:30 +0900125 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
sanghodbee2332017-05-18 09:59:16 +0900126 .forDevice(deviceId)
sanghodc375372017-06-08 10:41:30 +0900127 .withSelector(selector)
128 .withTreatment(treatment)
129 .withPriority(priority)
130 .fromApp(appId)
sanghodbee2332017-05-18 09:59:16 +0900131 .forTable(tableType);
132
sanghodc375372017-06-08 10:41:30 +0900133 if (priority == Constants.PRIORITY_SNAT_RULE) {
134 flowRuleBuilder.makeTemporary(TIMEOUT_SNAT_RULE);
sanghodbee2332017-05-18 09:59:16 +0900135 } else {
sanghodc375372017-06-08 10:41:30 +0900136 flowRuleBuilder.makePermanent();
sanghodbee2332017-05-18 09:59:16 +0900137 }
138
sanghodc375372017-06-08 10:41:30 +0900139 applyRule(flowRuleBuilder.build(), install);
sanghodbee2332017-05-18 09:59:16 +0900140 }
141
sangho3dd2a8b2017-07-19 15:54:31 +0900142 @Override
143 public void connectTables(DeviceId deviceId, int fromTable, int toTable) {
sanghodbee2332017-05-18 09:59:16 +0900144 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
145 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
146
147 treatment.transition(toTable);
148
149 FlowRule flowRule = DefaultFlowRule.builder()
150 .forDevice(deviceId)
151 .withSelector(selector.build())
152 .withTreatment(treatment.build())
153 .withPriority(DROP_PRIORITY)
154 .fromApp(appId)
155 .makePermanent()
156 .forTable(fromTable)
157 .build();
158
sanghodc375372017-06-08 10:41:30 +0900159 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900160 }
161
sangho3dd2a8b2017-07-19 15:54:31 +0900162 @Override
163 public void setUpTableMissEntry(DeviceId deviceId, int table) {
sanghodbee2332017-05-18 09:59:16 +0900164 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
sanghodc375372017-06-08 10:41:30 +0900179 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900180 }
181
Jian Lia0778172018-07-16 22:50:19 +0900182 private void applyRule(FlowRule flowRule, boolean install) {
183 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
184
185 flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule);
186
187 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
188 @Override
189 public void onSuccess(FlowRuleOperations ops) {
190 log.debug("Provisioned vni or forwarding table");
191 }
192
193 @Override
194 public void onError(FlowRuleOperations ops) {
195 log.debug("Failed to provision vni or forwarding table");
196 }
197 }));
198 }
199
200 protected void initializePipeline(DeviceId deviceId) {
201 // for inbound table transition
202 connectTables(deviceId, Constants.STAT_INBOUND_TABLE, Constants.VTAP_INBOUND_TABLE);
203 connectTables(deviceId, Constants.VTAP_INBOUND_TABLE, Constants.DHCP_ARP_TABLE);
204
205 // for vTag and ACL table transition
206 connectTables(deviceId, Constants.DHCP_ARP_TABLE, Constants.VTAG_TABLE);
207 connectTables(deviceId, Constants.VTAG_TABLE, Constants.ACL_TABLE);
208 connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE);
209
210 // for JUMP table transition
211 // we need JUMP table for bypassing routing table which contains large
212 // amount of flow rules which might cause performance degradation during
213 // table lookup
214 setupJumpTable(deviceId);
215
216 // for outbound table transition
217 connectTables(deviceId, Constants.STAT_OUTBOUND_TABLE, Constants.VTAP_OUTBOUND_TABLE);
218 connectTables(deviceId, Constants.VTAP_OUTBOUND_TABLE, Constants.FORWARDING_TABLE);
219
220 // for FLAT outbound table transition
221 connectTables(deviceId, Constants.STAT_FLAT_OUTBOUND_TABLE, Constants.VTAP_FLAT_OUTBOUND_TABLE);
222 connectTables(deviceId, Constants.VTAP_FLAT_OUTBOUND_TABLE, Constants.FLAT_TABLE);
223 }
224
sanghodbee2332017-05-18 09:59:16 +0900225 private void setupJumpTable(DeviceId deviceId) {
226 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
227 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
228
229 selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC);
230 treatment.transition(Constants.ROUTING_TABLE);
231
232 FlowRule flowRule = DefaultFlowRule.builder()
233 .forDevice(deviceId)
234 .withSelector(selector.build())
235 .withTreatment(treatment.build())
236 .withPriority(HIGH_PRIORITY)
237 .fromApp(appId)
238 .makePermanent()
239 .forTable(Constants.JUMP_TABLE)
240 .build();
241
sanghodc375372017-06-08 10:41:30 +0900242 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900243
244 selector = DefaultTrafficSelector.builder();
245 treatment = DefaultTrafficTreatment.builder();
246
Jian Li8abf2fe2018-06-12 18:42:30 +0900247 treatment.transition(Constants.STAT_OUTBOUND_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900248
249 flowRule = DefaultFlowRule.builder()
250 .forDevice(deviceId)
251 .withSelector(selector.build())
252 .withTreatment(treatment.build())
253 .withPriority(DROP_PRIORITY)
254 .fromApp(appId)
255 .makePermanent()
256 .forTable(Constants.JUMP_TABLE)
257 .build();
258
sanghodc375372017-06-08 10:41:30 +0900259 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900260 }
261
262 private class InternalOpenstackNodeListener implements OpenstackNodeListener {
263
264 @Override
Jian Lia0778172018-07-16 22:50:19 +0900265 public boolean isRelevant(OpenstackNodeEvent event) {
266 // do not allow to proceed without leadership
267 NodeId leader = leadershipService.getLeader(appId.name());
268 return Objects.equals(localNodeId, leader) &&
269 event.subject().type().equals(COMPUTE);
270 }
271
272 @Override
sanghodbee2332017-05-18 09:59:16 +0900273 public void event(OpenstackNodeEvent event) {
274 OpenstackNode osNode = event.subject();
sanghodbee2332017-05-18 09:59:16 +0900275
276 switch (event.type()) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900277 case OPENSTACK_NODE_COMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900278 deviceEventExecutor.execute(() -> {
279 log.info("COMPLETE node {} is detected", osNode.hostname());
Jian Lia0778172018-07-16 22:50:19 +0900280 initializePipeline(osNode.intgBridge());
sanghodbee2332017-05-18 09:59:16 +0900281 });
282 break;
Hyunsun Moon0d457362017-06-27 17:19:41 +0900283 case OPENSTACK_NODE_CREATED:
284 case OPENSTACK_NODE_UPDATED:
285 case OPENSTACK_NODE_REMOVED:
286 case OPENSTACK_NODE_INCOMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900287 default:
Hyunsun Moon0d457362017-06-27 17:19:41 +0900288 // do nothing
sanghodbee2332017-05-18 09:59:16 +0900289 break;
290 }
291 }
sanghodbee2332017-05-18 09:59:16 +0900292 }
293}