blob: 7e9dfec74733a96033457221681b50f358c85ada [file] [log] [blame]
alshabibaebe7752015-04-07 17:45:42 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 */
16package org.onosproject.driver.pipeline;
17
alshabib2a441c62015-04-13 18:39:38 -070018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Saurav Das3d038262015-04-23 12:36:58 -070022
alshabibaebe7752015-04-07 17:45:42 -070023import org.onlab.osgi.ServiceDirectory;
24import org.onlab.packet.Ethernet;
Saurav Das3d038262015-04-23 12:36:58 -070025import org.onlab.packet.IPv4;
alshabibaebe7752015-04-07 17:45:42 -070026import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
alshabib2a441c62015-04-13 18:39:38 -070028import org.onlab.util.KryoNamespace;
alshabibaebe7752015-04-07 17:45:42 -070029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.net.DeviceId;
alshabib2a441c62015-04-13 18:39:38 -070032import org.onosproject.net.behaviour.NextGroup;
alshabibaebe7752015-04-07 17:45:42 -070033import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070034import org.onosproject.net.behaviour.PipelinerContext;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070035import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabibaebe7752015-04-07 17:45:42 -070036import org.onosproject.net.flow.DefaultFlowRule;
37import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.FlowRuleOperations;
41import org.onosproject.net.flow.FlowRuleOperationsContext;
42import org.onosproject.net.flow.FlowRuleService;
43import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
alshabib910aff12015-04-09 16:55:57 -070045import org.onosproject.net.flow.criteria.Criteria;
46import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070047import org.onosproject.net.flow.criteria.EthCriterion;
48import org.onosproject.net.flow.criteria.EthTypeCriterion;
49import org.onosproject.net.flow.criteria.IPCriterion;
50import org.onosproject.net.flow.criteria.IPProtocolCriterion;
51import org.onosproject.net.flow.criteria.PortCriterion;
52import org.onosproject.net.flow.criteria.VlanIdCriterion;
alshabibaebe7752015-04-07 17:45:42 -070053import org.onosproject.net.flowobjective.FilteringObjective;
alshabib2a441c62015-04-13 18:39:38 -070054import org.onosproject.net.flowobjective.FlowObjectiveStore;
alshabibaebe7752015-04-07 17:45:42 -070055import org.onosproject.net.flowobjective.ForwardingObjective;
56import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070057import org.onosproject.net.flowobjective.Objective;
alshabib2a441c62015-04-13 18:39:38 -070058import org.onosproject.net.flowobjective.ObjectiveError;
59import org.onosproject.net.group.DefaultGroupBucket;
60import org.onosproject.net.group.DefaultGroupDescription;
61import org.onosproject.net.group.DefaultGroupKey;
62import org.onosproject.net.group.Group;
63import org.onosproject.net.group.GroupBucket;
64import org.onosproject.net.group.GroupBuckets;
65import org.onosproject.net.group.GroupDescription;
66import org.onosproject.net.group.GroupEvent;
67import org.onosproject.net.group.GroupKey;
68import org.onosproject.net.group.GroupListener;
69import org.onosproject.net.group.GroupService;
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -050070import org.onosproject.net.meter.MeterService;
alshabibaebe7752015-04-07 17:45:42 -070071import org.slf4j.Logger;
72
73import java.util.Collection;
alshabib2a441c62015-04-13 18:39:38 -070074import java.util.Collections;
Sho SHIMIZU45906042016-01-13 23:05:54 -080075import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070076import java.util.Set;
77import java.util.concurrent.Executors;
78import java.util.concurrent.ScheduledExecutorService;
79import java.util.concurrent.TimeUnit;
80import java.util.stream.Collectors;
alshabibaebe7752015-04-07 17:45:42 -070081
alshabib2a441c62015-04-13 18:39:38 -070082import static org.onlab.util.Tools.groupedThreads;
alshabibaebe7752015-04-07 17:45:42 -070083import static org.slf4j.LoggerFactory.getLogger;
84
85/**
alshabib2a441c62015-04-13 18:39:38 -070086 * OpenvSwitch emulation of the Corsa pipeline handler.
alshabibaebe7752015-04-07 17:45:42 -070087 */
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070088public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabibaebe7752015-04-07 17:45:42 -070089
Saurav Das3ea46622015-04-22 14:01:34 -070090 protected static final int MAC_TABLE = 0;
alshabibd17abc22015-04-21 18:26:35 -070091 protected static final int VLAN_MPLS_TABLE = 1;
92 protected static final int VLAN_TABLE = 2;
93 //protected static final int MPLS_TABLE = 3;
94 protected static final int ETHER_TABLE = 4;
95 protected static final int COS_MAP_TABLE = 5;
96 protected static final int FIB_TABLE = 6;
97 protected static final int LOCAL_TABLE = 9;
98
99
Saurav Dasd8b97002015-05-14 23:42:49 -0700100 protected static final int CONTROLLER_PRIORITY = 255;
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500101 protected static final int DROP_PRIORITY = 0;
102 protected static final int HIGHEST_PRIORITY = 0xffff;
alshabibaebe7752015-04-07 17:45:42 -0700103
104 private final Logger log = getLogger(getClass());
105
106 private ServiceDirectory serviceDirectory;
Saurav Dasd8b97002015-05-14 23:42:49 -0700107 protected FlowRuleService flowRuleService;
alshabibaebe7752015-04-07 17:45:42 -0700108 private CoreService coreService;
alshabib2a441c62015-04-13 18:39:38 -0700109 private GroupService groupService;
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500110 protected MeterService meterService;
alshabib2a441c62015-04-13 18:39:38 -0700111 private FlowObjectiveStore flowObjectiveStore;
Saurav Dasd8b97002015-05-14 23:42:49 -0700112 protected DeviceId deviceId;
113 protected ApplicationId appId;
alshabibaebe7752015-04-07 17:45:42 -0700114
alshabib2a441c62015-04-13 18:39:38 -0700115 private KryoNamespace appKryo = new KryoNamespace.Builder()
116 .register(GroupKey.class)
117 .register(DefaultGroupKey.class)
118 .register(CorsaGroup.class)
119 .register(byte[].class)
120 .build();
121
122 private Cache<GroupKey, NextObjective> pendingGroups;
123
124 private ScheduledExecutorService groupChecker =
125 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner",
126 "ovs-corsa-%d"));
127
alshabibaebe7752015-04-07 17:45:42 -0700128 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700129 public void init(DeviceId deviceId, PipelinerContext context) {
130 this.serviceDirectory = context.directory();
alshabibaebe7752015-04-07 17:45:42 -0700131 this.deviceId = deviceId;
132
alshabib2a441c62015-04-13 18:39:38 -0700133 pendingGroups = CacheBuilder.newBuilder()
134 .expireAfterWrite(20, TimeUnit.SECONDS)
135 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
136 if (notification.getCause() == RemovalCause.EXPIRED) {
137 fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED);
138 }
139 }).build();
140
141 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
alshabibaebe7752015-04-07 17:45:42 -0700142
143 coreService = serviceDirectory.get(CoreService.class);
144 flowRuleService = serviceDirectory.get(FlowRuleService.class);
alshabib2a441c62015-04-13 18:39:38 -0700145 groupService = serviceDirectory.get(GroupService.class);
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500146 meterService = serviceDirectory.get(MeterService.class);
alshabib2a441c62015-04-13 18:39:38 -0700147 flowObjectiveStore = context.store();
148
149 groupService.addListener(new InnerGroupListener());
alshabibaebe7752015-04-07 17:45:42 -0700150
151 appId = coreService.registerApplication(
152 "org.onosproject.driver.OVSCorsaPipeline");
153
Saurav Das100e3b82015-04-30 11:12:10 -0700154 initializePipeline();
alshabibaebe7752015-04-07 17:45:42 -0700155 }
156
157 @Override
alshabib2a441c62015-04-13 18:39:38 -0700158 public void filter(FilteringObjective filteringObjective) {
159 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
160 processFilter(filteringObjective,
161 filteringObjective.op() == Objective.Operation.ADD,
162 filteringObjective.appId());
163 } else {
164 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
165 }
166 }
alshabib910aff12015-04-09 16:55:57 -0700167
alshabib2a441c62015-04-13 18:39:38 -0700168 @Override
169 public void forward(ForwardingObjective fwd) {
170 Collection<FlowRule> rules;
171 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
172
173 rules = processForward(fwd);
174 switch (fwd.op()) {
175 case ADD:
176 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800177 .filter(Objects::nonNull)
alshabib2a441c62015-04-13 18:39:38 -0700178 .forEach(flowBuilder::add);
179 break;
180 case REMOVE:
181 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800182 .filter(Objects::nonNull)
alshabib2a441c62015-04-13 18:39:38 -0700183 .forEach(flowBuilder::remove);
184 break;
185 default:
186 fail(fwd, ObjectiveError.UNKNOWN);
187 log.warn("Unknown forwarding type {}", fwd.op());
188 }
189
190
191 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
192 @Override
193 public void onSuccess(FlowRuleOperations ops) {
194 pass(fwd);
195 }
196
197 @Override
198 public void onError(FlowRuleOperations ops) {
199 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
200 }
201 }));
alshabib910aff12015-04-09 16:55:57 -0700202
203 }
204
alshabib2a441c62015-04-13 18:39:38 -0700205 @Override
206 public void next(NextObjective nextObjective) {
207 switch (nextObjective.type()) {
208 case SIMPLE:
209 Collection<TrafficTreatment> treatments = nextObjective.next();
210 if (treatments.size() == 1) {
211 TrafficTreatment treatment = treatments.iterator().next();
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500212 treatment = processNextTreatment(treatment);
alshabib2a441c62015-04-13 18:39:38 -0700213 GroupBucket bucket =
214 DefaultGroupBucket.createIndirectGroupBucket(treatment);
215 final GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
216 GroupDescription groupDescription
217 = new DefaultGroupDescription(deviceId,
218 GroupDescription.Type.INDIRECT,
219 new GroupBuckets(Collections
220 .singletonList(bucket)),
221 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700222 null, // let group service determine group id
alshabib2a441c62015-04-13 18:39:38 -0700223 nextObjective.appId());
224 groupService.addGroup(groupDescription);
225 pendingGroups.put(key, nextObjective);
226 }
227 break;
228 case HASHED:
229 case BROADCAST:
230 case FAILOVER:
231 fail(nextObjective, ObjectiveError.UNSUPPORTED);
232 log.warn("Unsupported next objective type {}", nextObjective.type());
233 break;
234 default:
235 fail(nextObjective, ObjectiveError.UNKNOWN);
236 log.warn("Unknown next objective type {}", nextObjective.type());
237 }
238
239 }
240
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500241 /* Hook for altering the NextObjective treatment */
242 protected TrafficTreatment processNextTreatment(TrafficTreatment treatment) {
243 return treatment; /* Keep treatment as is for OVSCorsaPipeline */
244 }
245
alshabib2a441c62015-04-13 18:39:38 -0700246 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
247 switch (fwd.flag()) {
248 case SPECIFIC:
249 return processSpecific(fwd);
250 case VERSATILE:
251 return processVersatile(fwd);
252 default:
253 fail(fwd, ObjectiveError.UNKNOWN);
254 log.warn("Unknown forwarding flag {}", fwd.flag());
255 }
256 return Collections.emptySet();
257 }
258
259 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das3d038262015-04-23 12:36:58 -0700260 log.debug("Processing versatile forwarding objective");
261 TrafficSelector selector = fwd.selector();
262
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700263 EthTypeCriterion ethType =
264 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das3d038262015-04-23 12:36:58 -0700265 if (ethType == null) {
266 log.error("Versatile forwarding objective must include ethType");
267 fail(fwd, ObjectiveError.UNKNOWN);
268 return Collections.emptySet();
269 }
alshabibcaf1ca22015-06-25 15:18:16 -0700270 if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) {
Saurav Das3d038262015-04-23 12:36:58 -0700271 log.warn("Driver automatically handles ARP packets by punting to controller "
272 + " from ETHER table");
273 pass(fwd);
274 return Collections.emptySet();
alshabibcaf1ca22015-06-25 15:18:16 -0700275 } else if (ethType.ethType().toShort() == Ethernet.TYPE_LLDP ||
276 ethType.ethType().toShort() == Ethernet.TYPE_BSN) {
Saurav Das3d038262015-04-23 12:36:58 -0700277 log.warn("Driver currently does not currently handle LLDP packets");
278 fail(fwd, ObjectiveError.UNSUPPORTED);
279 return Collections.emptySet();
alshabibcaf1ca22015-06-25 15:18:16 -0700280 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700281 IPCriterion ipSrc = (IPCriterion) selector
Saurav Das3d038262015-04-23 12:36:58 -0700282 .getCriterion(Criterion.Type.IPV4_SRC);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700283 IPCriterion ipDst = (IPCriterion) selector
Saurav Das3d038262015-04-23 12:36:58 -0700284 .getCriterion(Criterion.Type.IPV4_DST);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700285 IPProtocolCriterion ipProto = (IPProtocolCriterion) selector
Saurav Das3d038262015-04-23 12:36:58 -0700286 .getCriterion(Criterion.Type.IP_PROTO);
287 if (ipSrc != null) {
Saurav Dasbd7f7422015-04-23 16:31:47 -0700288 log.warn("Driver does not currently handle matching Src IP");
Saurav Das3d038262015-04-23 12:36:58 -0700289 fail(fwd, ObjectiveError.UNSUPPORTED);
290 return Collections.emptySet();
291 }
292 if (ipDst != null) {
293 log.error("Driver handles Dst IP matching as specific forwarding "
294 + "objective, not versatile");
295 fail(fwd, ObjectiveError.UNSUPPORTED);
296 return Collections.emptySet();
297 }
298 if (ipProto != null && ipProto.protocol() == IPv4.PROTOCOL_TCP) {
299 log.warn("Driver automatically punts all packets reaching the "
300 + "LOCAL table to the controller");
301 pass(fwd);
302 return Collections.emptySet();
303 }
304 }
305
306 log.warn("Driver does not support given versatile forwarding objective");
alshabib2a441c62015-04-13 18:39:38 -0700307 fail(fwd, ObjectiveError.UNSUPPORTED);
308 return Collections.emptySet();
309 }
310
311 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das3d038262015-04-23 12:36:58 -0700312 log.debug("Processing specific forwarding objective");
alshabib2a441c62015-04-13 18:39:38 -0700313 TrafficSelector selector = fwd.selector();
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500314
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700315 EthTypeCriterion ethType =
316 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500317 if (ethType != null) {
318 short et = ethType.ethType().toShort();
319 if (et == Ethernet.TYPE_IPV4) {
320 return processSpecificRoute(fwd);
321 } else if (et == Ethernet.TYPE_VLAN) {
322 /* The ForwardingObjective must specify VLAN ethtype in order to use the Transit Circuit */
323 return processSpecificSwitch(fwd);
324 }
alshabib2a441c62015-04-13 18:39:38 -0700325 }
326
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500327 fail(fwd, ObjectiveError.UNSUPPORTED);
328 return Collections.emptySet();
329 }
330
331 private Collection<FlowRule> processSpecificRoute(ForwardingObjective fwd) {
alshabib2a441c62015-04-13 18:39:38 -0700332 TrafficSelector filteredSelector =
333 DefaultTrafficSelector.builder()
334 .matchEthType(Ethernet.TYPE_IPV4)
335 .matchIPDst(
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500336 ((IPCriterion) fwd.selector().getCriterion(Criterion.Type.IPV4_DST)).ip())
alshabib2a441c62015-04-13 18:39:38 -0700337 .build();
338
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500339 TrafficTreatment.Builder tb = processSpecificRoutingTreatment();
alshabib2a441c62015-04-13 18:39:38 -0700340
sanghodde53d12015-04-30 10:34:41 -0700341 if (fwd.nextId() != null) {
342 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
343 GroupKey key = appKryo.deserialize(next.data());
344 Group group = groupService.getGroup(deviceId, key);
345 if (group == null) {
346 log.warn("The group left!");
347 fail(fwd, ObjectiveError.GROUPMISSING);
348 return Collections.emptySet();
349 }
350 tb.group(group.id());
alshabib2a441c62015-04-13 18:39:38 -0700351 }
352
alshabibd17abc22015-04-21 18:26:35 -0700353 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
354 .fromApp(fwd.appId())
355 .withPriority(fwd.priority())
356 .forDevice(deviceId)
357 .withSelector(filteredSelector)
sanghodde53d12015-04-30 10:34:41 -0700358 .withTreatment(tb.build());
alshabibd17abc22015-04-21 18:26:35 -0700359
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500360 ruleBuilder = processSpecificRoutingRule(ruleBuilder);
361
alshabibd17abc22015-04-21 18:26:35 -0700362 if (fwd.permanent()) {
363 ruleBuilder.makePermanent();
364 } else {
365 ruleBuilder.makeTemporary(fwd.timeout());
366 }
367
alshabibd17abc22015-04-21 18:26:35 -0700368 return Collections.singletonList(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700369 }
370
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500371 /* Hook for modifying Route traffic treatment */
372 protected TrafficTreatment.Builder processSpecificRoutingTreatment() {
373 return DefaultTrafficTreatment.builder();
374 }
375
376 /* Hook for modifying Route flow rule */
377 protected FlowRule.Builder processSpecificRoutingRule(FlowRule.Builder rb) {
378 return rb.forTable(FIB_TABLE);
379 }
380
381 protected Collection<FlowRule> processSpecificSwitch(ForwardingObjective fwd) {
382 /* Not supported by until CorsaPipelineV3 */
383 log.warn("Vlan switching not supported in ovs-corsa driver");
384 fail(fwd, ObjectiveError.UNSUPPORTED);
385 return Collections.emptySet();
386 }
387
388 protected void processFilter(FilteringObjective filt, boolean install,
alshabib910aff12015-04-09 16:55:57 -0700389 ApplicationId applicationId) {
Saurav Dascfd63d22015-04-13 16:08:24 -0700390 // This driver only processes filtering criteria defined with switch
391 // ports as the key
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700392 PortCriterion p;
Saurav Dascfd63d22015-04-13 16:08:24 -0700393 if (!filt.key().equals(Criteria.dummy()) &&
394 filt.key().type() == Criterion.Type.IN_PORT) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700395 p = (PortCriterion) filt.key();
Saurav Dascfd63d22015-04-13 16:08:24 -0700396 } else {
397 log.warn("No key defined in filtering objective from app: {}. Not"
398 + "processing filtering objective", applicationId);
alshabib2a441c62015-04-13 18:39:38 -0700399 fail(filt, ObjectiveError.UNKNOWN);
400 return;
alshabib910aff12015-04-09 16:55:57 -0700401 }
Saurav Dascfd63d22015-04-13 16:08:24 -0700402 // convert filtering conditions for switch-intfs into flowrules
403 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
404 for (Criterion c : filt.conditions()) {
405 if (c.type() == Criterion.Type.ETH_DST) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700406 EthCriterion e = (EthCriterion) c;
Saurav Dascfd63d22015-04-13 16:08:24 -0700407 log.debug("adding rule for MAC: {}", e.mac());
408 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
409 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
410 selector.matchEthDst(e.mac());
alshabibd17abc22015-04-21 18:26:35 -0700411 treatment.transition(VLAN_MPLS_TABLE);
412 FlowRule rule = DefaultFlowRule.builder()
413 .forDevice(deviceId)
414 .withSelector(selector.build())
415 .withTreatment(treatment.build())
416 .withPriority(CONTROLLER_PRIORITY)
417 .fromApp(applicationId)
418 .makePermanent()
Saurav Das3ea46622015-04-22 14:01:34 -0700419 .forTable(MAC_TABLE).build();
Saurav Dascfd63d22015-04-13 16:08:24 -0700420 ops = install ? ops.add(rule) : ops.remove(rule);
421 } else if (c.type() == Criterion.Type.VLAN_VID) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700422 VlanIdCriterion v = (VlanIdCriterion) c;
Saurav Dascfd63d22015-04-13 16:08:24 -0700423 log.debug("adding rule for VLAN: {}", v.vlanId());
424 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
425 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
426 selector.matchVlanId(v.vlanId());
427 selector.matchInPort(p.port());
alshabibd17abc22015-04-21 18:26:35 -0700428 treatment.transition(ETHER_TABLE);
Saurav Dascfd63d22015-04-13 16:08:24 -0700429 treatment.deferred().popVlan();
alshabibd17abc22015-04-21 18:26:35 -0700430 FlowRule rule = DefaultFlowRule.builder()
431 .forDevice(deviceId)
432 .withSelector(selector.build())
433 .withTreatment(treatment.build())
434 .withPriority(CONTROLLER_PRIORITY)
435 .fromApp(applicationId)
436 .makePermanent()
437 .forTable(VLAN_TABLE).build();
Saurav Dascfd63d22015-04-13 16:08:24 -0700438 ops = install ? ops.add(rule) : ops.remove(rule);
439 } else if (c.type() == Criterion.Type.IPV4_DST) {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700440 IPCriterion ip = (IPCriterion) c;
Saurav Dascfd63d22015-04-13 16:08:24 -0700441 log.debug("adding rule for IP: {}", ip.ip());
442 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
443 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
444 selector.matchEthType(Ethernet.TYPE_IPV4);
445 selector.matchIPDst(ip.ip());
alshabibd17abc22015-04-21 18:26:35 -0700446 treatment.transition(LOCAL_TABLE);
447 FlowRule rule = DefaultFlowRule.builder()
448 .forDevice(deviceId)
449 .withSelector(selector.build())
450 .withTreatment(treatment.build())
451 .withPriority(HIGHEST_PRIORITY)
452 .fromApp(applicationId)
453 .makePermanent()
454 .forTable(FIB_TABLE).build();
455
Saurav Dascfd63d22015-04-13 16:08:24 -0700456 ops = install ? ops.add(rule) : ops.remove(rule);
457 } else {
458 log.warn("Driver does not currently process filtering condition"
459 + " of type: {}", c.type());
alshabib2a441c62015-04-13 18:39:38 -0700460 fail(filt, ObjectiveError.UNSUPPORTED);
Saurav Dascfd63d22015-04-13 16:08:24 -0700461 }
462 }
463 // apply filtering flow rules
464 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
465 @Override
466 public void onSuccess(FlowRuleOperations ops) {
alshabib2a441c62015-04-13 18:39:38 -0700467 pass(filt);
Saurav Das3ea46622015-04-22 14:01:34 -0700468 log.info("Applied filtering rules");
Saurav Dascfd63d22015-04-13 16:08:24 -0700469 }
470
471 @Override
472 public void onError(FlowRuleOperations ops) {
alshabib2a441c62015-04-13 18:39:38 -0700473 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das3ea46622015-04-22 14:01:34 -0700474 log.info("Failed to apply filtering rules");
Saurav Dascfd63d22015-04-13 16:08:24 -0700475 }
476 }));
alshabibaebe7752015-04-07 17:45:42 -0700477 }
478
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500479 protected void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800480 obj.context().ifPresent(context -> context.onSuccess(obj));
alshabibaebe7752015-04-07 17:45:42 -0700481 }
482
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500483 protected void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800484 obj.context().ifPresent(context -> context.onError(obj, error));
alshabibaebe7752015-04-07 17:45:42 -0700485 }
486
Jonathan Gravel8c9f7bc2015-12-03 15:46:20 -0500487 protected void initializePipeline() {
Saurav Das3ea46622015-04-22 14:01:34 -0700488 processMacTable(true);
489 processVlanMplsTable(true);
490 processVlanTable(true);
491 processEtherTable(true);
492 processCosTable(true);
493 processFibTable(true);
494 processLocalTable(true);
alshabibaebe7752015-04-07 17:45:42 -0700495 }
496
Saurav Das3ea46622015-04-22 14:01:34 -0700497 private void processMacTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700498 TrafficSelector.Builder selector;
499 TrafficTreatment.Builder treatment;
500
501 // Bcast rule
502 selector = DefaultTrafficSelector.builder();
503 treatment = DefaultTrafficTreatment.builder();
504
505 selector.matchEthDst(MacAddress.BROADCAST);
alshabibd17abc22015-04-21 18:26:35 -0700506 treatment.transition(VLAN_MPLS_TABLE);
alshabibaebe7752015-04-07 17:45:42 -0700507
alshabibd17abc22015-04-21 18:26:35 -0700508 FlowRule rule = DefaultFlowRule.builder()
509 .forDevice(deviceId)
510 .withSelector(selector.build())
511 .withTreatment(treatment.build())
512 .withPriority(CONTROLLER_PRIORITY)
513 .fromApp(appId)
514 .makePermanent()
Saurav Das3ea46622015-04-22 14:01:34 -0700515 .forTable(MAC_TABLE).build();
alshabibd17abc22015-04-21 18:26:35 -0700516
alshabibaebe7752015-04-07 17:45:42 -0700517
518 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
519
520 ops = install ? ops.add(rule) : ops.remove(rule);
521
522
alshabibaebe7752015-04-07 17:45:42 -0700523 //Drop rule
524 selector = DefaultTrafficSelector.builder();
525 treatment = DefaultTrafficTreatment.builder();
526
527 treatment.drop();
528
alshabibd17abc22015-04-21 18:26:35 -0700529 rule = DefaultFlowRule.builder()
530 .forDevice(deviceId)
531 .withSelector(selector.build())
532 .withTreatment(treatment.build())
533 .withPriority(DROP_PRIORITY)
534 .fromApp(appId)
535 .makePermanent()
Saurav Das3ea46622015-04-22 14:01:34 -0700536 .forTable(MAC_TABLE).build();
alshabibd17abc22015-04-21 18:26:35 -0700537
alshabibaebe7752015-04-07 17:45:42 -0700538
539 ops = install ? ops.add(rule) : ops.remove(rule);
540
541 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
542 @Override
543 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700544 log.info("Provisioned mac table");
alshabibaebe7752015-04-07 17:45:42 -0700545 }
546
547 @Override
548 public void onError(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700549 log.info("Failed to provision mac table");
alshabibaebe7752015-04-07 17:45:42 -0700550 }
551 }));
552
553 }
554
Saurav Dasd8b97002015-05-14 23:42:49 -0700555 protected void processVlanMplsTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700556 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
557 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
558 .builder();
559 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
560 FlowRule rule;
561
562 selector.matchVlanId(VlanId.ANY);
alshabibd17abc22015-04-21 18:26:35 -0700563 treatment.transition(VLAN_TABLE);
alshabibaebe7752015-04-07 17:45:42 -0700564
alshabibd17abc22015-04-21 18:26:35 -0700565 rule = DefaultFlowRule.builder()
566 .forDevice(deviceId)
567 .withSelector(selector.build())
568 .withTreatment(treatment.build())
569 .withPriority(CONTROLLER_PRIORITY)
570 .fromApp(appId)
571 .makePermanent()
572 .forTable(VLAN_MPLS_TABLE).build();
573
alshabibaebe7752015-04-07 17:45:42 -0700574
575 ops = install ? ops.add(rule) : ops.remove(rule);
576
577 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
578 @Override
579 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700580 log.info("Provisioned vlan/mpls table");
alshabibaebe7752015-04-07 17:45:42 -0700581 }
582
583 @Override
584 public void onError(FlowRuleOperations ops) {
585 log.info(
Saurav Das3ea46622015-04-22 14:01:34 -0700586 "Failed to provision vlan/mpls table");
alshabibaebe7752015-04-07 17:45:42 -0700587 }
588 }));
589
590 }
591
Saurav Das3ea46622015-04-22 14:01:34 -0700592 private void processVlanTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700593 TrafficSelector.Builder selector;
594 TrafficTreatment.Builder treatment;
595 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
596 FlowRule rule;
597
598
alshabibaebe7752015-04-07 17:45:42 -0700599 //Drop rule
600 selector = DefaultTrafficSelector.builder();
601 treatment = DefaultTrafficTreatment.builder();
602
603 treatment.drop();
604
alshabibd17abc22015-04-21 18:26:35 -0700605 rule = DefaultFlowRule.builder()
606 .forDevice(deviceId)
607 .withSelector(selector.build())
608 .withTreatment(treatment.build())
609 .withPriority(DROP_PRIORITY)
610 .fromApp(appId)
611 .makePermanent()
612 .forTable(VLAN_TABLE).build();
alshabibaebe7752015-04-07 17:45:42 -0700613
614 ops = install ? ops.add(rule) : ops.remove(rule);
615
616 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
617 @Override
618 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700619 log.info("Provisioned vlan table");
alshabibaebe7752015-04-07 17:45:42 -0700620 }
621
622 @Override
623 public void onError(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700624 log.info("Failed to provision vlan table");
alshabibaebe7752015-04-07 17:45:42 -0700625 }
626 }));
627 }
628
Saurav Das3ea46622015-04-22 14:01:34 -0700629 private void processEtherTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700630 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
631 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
632 .builder();
633 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
634 FlowRule rule;
635
636 selector.matchEthType(Ethernet.TYPE_ARP);
637 treatment.punt();
638
alshabibd17abc22015-04-21 18:26:35 -0700639 rule = DefaultFlowRule.builder()
640 .forDevice(deviceId)
641 .withSelector(selector.build())
642 .withTreatment(treatment.build())
643 .withPriority(CONTROLLER_PRIORITY)
644 .fromApp(appId)
645 .makePermanent()
646 .forTable(ETHER_TABLE).build();
alshabibaebe7752015-04-07 17:45:42 -0700647
648 ops = install ? ops.add(rule) : ops.remove(rule);
649
650 selector = DefaultTrafficSelector.builder();
651 treatment = DefaultTrafficTreatment.builder();
652
653 selector.matchEthType(Ethernet.TYPE_IPV4);
alshabibd17abc22015-04-21 18:26:35 -0700654 treatment.transition(COS_MAP_TABLE);
alshabibaebe7752015-04-07 17:45:42 -0700655
alshabibd17abc22015-04-21 18:26:35 -0700656 rule = DefaultFlowRule.builder()
657 .forDevice(deviceId)
658 .withPriority(CONTROLLER_PRIORITY)
659 .withSelector(selector.build())
660 .withTreatment(treatment.build())
661 .fromApp(appId)
662 .makePermanent()
663 .forTable(ETHER_TABLE).build();
alshabibaebe7752015-04-07 17:45:42 -0700664
665 ops = install ? ops.add(rule) : ops.remove(rule);
666
667 //Drop rule
668 selector = DefaultTrafficSelector.builder();
669 treatment = DefaultTrafficTreatment.builder();
670
671 treatment.drop();
672
alshabibd17abc22015-04-21 18:26:35 -0700673 rule = DefaultFlowRule.builder()
674 .forDevice(deviceId)
675 .withSelector(selector.build())
676 .withTreatment(treatment.build())
677 .withPriority(DROP_PRIORITY)
678 .fromApp(appId)
679 .makePermanent()
680 .forTable(ETHER_TABLE).build();
681
alshabibaebe7752015-04-07 17:45:42 -0700682
683 ops = install ? ops.add(rule) : ops.remove(rule);
684
685 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
686 @Override
687 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700688 log.info("Provisioned ether table");
alshabibaebe7752015-04-07 17:45:42 -0700689 }
690
691 @Override
692 public void onError(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700693 log.info("Failed to provision ether table");
alshabibaebe7752015-04-07 17:45:42 -0700694 }
695 }));
696
697 }
698
Saurav Das3ea46622015-04-22 14:01:34 -0700699 private void processCosTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700700 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
701 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
702 .builder();
703 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
704 FlowRule rule;
705
alshabibd17abc22015-04-21 18:26:35 -0700706 treatment.transition(FIB_TABLE);
alshabibaebe7752015-04-07 17:45:42 -0700707
alshabibd17abc22015-04-21 18:26:35 -0700708 rule = DefaultFlowRule.builder()
709 .forDevice(deviceId)
710 .withSelector(selector.build())
711 .withTreatment(treatment.build())
712 .withPriority(DROP_PRIORITY)
713 .fromApp(appId)
714 .makePermanent()
715 .forTable(COS_MAP_TABLE).build();
alshabibaebe7752015-04-07 17:45:42 -0700716
717 ops = install ? ops.add(rule) : ops.remove(rule);
718
719 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
720 @Override
721 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700722 log.info("Provisioned cos table");
alshabibaebe7752015-04-07 17:45:42 -0700723 }
724
725 @Override
726 public void onError(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700727 log.info("Failed to provision cos table");
alshabibaebe7752015-04-07 17:45:42 -0700728 }
729 }));
730
731 }
732
Saurav Das3ea46622015-04-22 14:01:34 -0700733 private void processFibTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700734 TrafficSelector.Builder selector;
735 TrafficTreatment.Builder treatment;
736 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
737 FlowRule rule;
738
739 //Drop rule
740 selector = DefaultTrafficSelector.builder();
741 treatment = DefaultTrafficTreatment.builder();
742
743 treatment.drop();
744
alshabibd17abc22015-04-21 18:26:35 -0700745 rule = DefaultFlowRule.builder()
746 .forDevice(deviceId)
747 .withSelector(selector.build())
748 .withTreatment(treatment.build())
749 .withPriority(DROP_PRIORITY)
750 .fromApp(appId)
751 .makePermanent()
752 .forTable(FIB_TABLE).build();
alshabibaebe7752015-04-07 17:45:42 -0700753
754 ops = install ? ops.add(rule) : ops.remove(rule);
755
756 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
757 @Override
758 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700759 log.info("Provisioned FIB table");
alshabibaebe7752015-04-07 17:45:42 -0700760 }
761
762 @Override
763 public void onError(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700764 log.info("Failed to provision FIB table");
alshabibaebe7752015-04-07 17:45:42 -0700765 }
766 }));
767 }
768
Saurav Das3ea46622015-04-22 14:01:34 -0700769 private void processLocalTable(boolean install) {
alshabibaebe7752015-04-07 17:45:42 -0700770 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
771 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
772 .builder();
773 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
774 FlowRule rule;
775
776 treatment.punt();
777
alshabibd17abc22015-04-21 18:26:35 -0700778 rule = DefaultFlowRule.builder()
779 .forDevice(deviceId)
780 .withSelector(selector.build())
781 .withTreatment(treatment.build())
782 .withPriority(CONTROLLER_PRIORITY)
783 .fromApp(appId)
784 .makePermanent()
785 .forTable(LOCAL_TABLE).build();
alshabibaebe7752015-04-07 17:45:42 -0700786
787 ops = install ? ops.add(rule) : ops.remove(rule);
788
789 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
790 @Override
791 public void onSuccess(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700792 log.info("Provisioned Local table");
alshabibaebe7752015-04-07 17:45:42 -0700793 }
794
795 @Override
796 public void onError(FlowRuleOperations ops) {
Saurav Das3ea46622015-04-22 14:01:34 -0700797 log.info("Failed to provision Local table");
alshabibaebe7752015-04-07 17:45:42 -0700798 }
799 }));
800 }
801
alshabib2a441c62015-04-13 18:39:38 -0700802 private class InnerGroupListener implements GroupListener {
803 @Override
804 public void event(GroupEvent event) {
805 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
806 GroupKey key = event.subject().appCookie();
807
808 NextObjective obj = pendingGroups.getIfPresent(key);
809 if (obj != null) {
810 flowObjectiveStore.putNextGroup(obj.id(), new CorsaGroup(key));
811 pass(obj);
812 pendingGroups.invalidate(key);
813 }
814 }
815 }
816 }
817
818
819 private class GroupChecker implements Runnable {
820
821 @Override
822 public void run() {
823 Set<GroupKey> keys = pendingGroups.asMap().keySet().stream()
824 .filter(key -> groupService.getGroup(deviceId, key) != null)
825 .collect(Collectors.toSet());
826
827 keys.stream().forEach(key -> {
828 NextObjective obj = pendingGroups.getIfPresent(key);
829 if (obj == null) {
830 return;
831 }
832 pass(obj);
833 pendingGroups.invalidate(key);
Saurav Dasbd7f7422015-04-23 16:31:47 -0700834 log.info("Heard back from group service for group {}. "
835 + "Applying pending forwarding objectives", obj.id());
alshabib2a441c62015-04-13 18:39:38 -0700836 flowObjectiveStore.putNextGroup(obj.id(), new CorsaGroup(key));
837 });
838 }
839 }
840
841 private class CorsaGroup implements NextGroup {
842
843 private final GroupKey key;
844
845 public CorsaGroup(GroupKey key) {
846 this.key = key;
847 }
848
Saurav Dase3274c82015-05-24 17:21:56 -0700849 @SuppressWarnings("unused")
alshabib2a441c62015-04-13 18:39:38 -0700850 public GroupKey key() {
851 return key;
852 }
853
854 @Override
855 public byte[] data() {
856 return appKryo.serialize(key);
857 }
858
859 }
alshabibaebe7752015-04-07 17:45:42 -0700860}