blob: 6aa0dae04f08d1061142af4023000d83896be169 [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);
Jian Li5c09e212018-10-24 18:23:58 +0900203 connectTables(deviceId, Constants.VTAP_INBOUND_TABLE, Constants.DHCP_TABLE);
Jian Lia0778172018-07-16 22:50:19 +0900204
Jian Li5c09e212018-10-24 18:23:58 +0900205 // for DHCP and vTag table transition
206 connectTables(deviceId, Constants.DHCP_TABLE, Constants.VTAG_TABLE);
207
208 // for vTag and ARP table transition
209 connectTables(deviceId, Constants.VTAG_TABLE, Constants.ARP_TABLE);
210
211 // for ARP and ACL table transition
212 connectTables(deviceId, Constants.ARP_TABLE, Constants.ACL_TABLE);
213
214 // for ACL and JUMP table transition
Jian Lia0778172018-07-16 22:50:19 +0900215 connectTables(deviceId, Constants.ACL_TABLE, Constants.JUMP_TABLE);
216
217 // for JUMP table transition
218 // we need JUMP table for bypassing routing table which contains large
219 // amount of flow rules which might cause performance degradation during
220 // table lookup
221 setupJumpTable(deviceId);
222
223 // for outbound table transition
224 connectTables(deviceId, Constants.STAT_OUTBOUND_TABLE, Constants.VTAP_OUTBOUND_TABLE);
225 connectTables(deviceId, Constants.VTAP_OUTBOUND_TABLE, Constants.FORWARDING_TABLE);
226
227 // for FLAT outbound table transition
228 connectTables(deviceId, Constants.STAT_FLAT_OUTBOUND_TABLE, Constants.VTAP_FLAT_OUTBOUND_TABLE);
229 connectTables(deviceId, Constants.VTAP_FLAT_OUTBOUND_TABLE, Constants.FLAT_TABLE);
230 }
231
sanghodbee2332017-05-18 09:59:16 +0900232 private void setupJumpTable(DeviceId deviceId) {
233 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
234 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
235
236 selector.matchEthDst(Constants.DEFAULT_GATEWAY_MAC);
237 treatment.transition(Constants.ROUTING_TABLE);
238
239 FlowRule flowRule = DefaultFlowRule.builder()
240 .forDevice(deviceId)
241 .withSelector(selector.build())
242 .withTreatment(treatment.build())
243 .withPriority(HIGH_PRIORITY)
244 .fromApp(appId)
245 .makePermanent()
246 .forTable(Constants.JUMP_TABLE)
247 .build();
248
sanghodc375372017-06-08 10:41:30 +0900249 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900250
251 selector = DefaultTrafficSelector.builder();
252 treatment = DefaultTrafficTreatment.builder();
253
Jian Li8abf2fe2018-06-12 18:42:30 +0900254 treatment.transition(Constants.STAT_OUTBOUND_TABLE);
sanghodbee2332017-05-18 09:59:16 +0900255
256 flowRule = DefaultFlowRule.builder()
257 .forDevice(deviceId)
258 .withSelector(selector.build())
259 .withTreatment(treatment.build())
260 .withPriority(DROP_PRIORITY)
261 .fromApp(appId)
262 .makePermanent()
263 .forTable(Constants.JUMP_TABLE)
264 .build();
265
sanghodc375372017-06-08 10:41:30 +0900266 applyRule(flowRule, true);
sanghodbee2332017-05-18 09:59:16 +0900267 }
268
269 private class InternalOpenstackNodeListener implements OpenstackNodeListener {
270
271 @Override
Jian Lia0778172018-07-16 22:50:19 +0900272 public boolean isRelevant(OpenstackNodeEvent event) {
273 // do not allow to proceed without leadership
274 NodeId leader = leadershipService.getLeader(appId.name());
275 return Objects.equals(localNodeId, leader) &&
276 event.subject().type().equals(COMPUTE);
277 }
278
279 @Override
sanghodbee2332017-05-18 09:59:16 +0900280 public void event(OpenstackNodeEvent event) {
281 OpenstackNode osNode = event.subject();
sanghodbee2332017-05-18 09:59:16 +0900282
283 switch (event.type()) {
Hyunsun Moon0d457362017-06-27 17:19:41 +0900284 case OPENSTACK_NODE_COMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900285 deviceEventExecutor.execute(() -> {
286 log.info("COMPLETE node {} is detected", osNode.hostname());
Jian Lia0778172018-07-16 22:50:19 +0900287 initializePipeline(osNode.intgBridge());
sanghodbee2332017-05-18 09:59:16 +0900288 });
289 break;
Hyunsun Moon0d457362017-06-27 17:19:41 +0900290 case OPENSTACK_NODE_CREATED:
291 case OPENSTACK_NODE_UPDATED:
292 case OPENSTACK_NODE_REMOVED:
293 case OPENSTACK_NODE_INCOMPLETE:
sanghodbee2332017-05-18 09:59:16 +0900294 default:
Hyunsun Moon0d457362017-06-27 17:19:41 +0900295 // do nothing
sanghodbee2332017-05-18 09:59:16 +0900296 break;
297 }
298 }
sanghodbee2332017-05-18 09:59:16 +0900299 }
300}