blob: 2c6e24cf2471056d8938e02c68e6878fe722f572 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Saurav Dase3274c82015-05-24 17:21:56 -070016package org.onosproject.driver.pipeline;
17
18
19import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.cache.RemovalCause;
22import com.google.common.cache.RemovalNotification;
23
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.packet.Ethernet;
26import org.onlab.packet.IPv4;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070027import org.onlab.packet.TpPort;
Saurav Dase3274c82015-05-24 17:21:56 -070028import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.net.DeviceId;
Saurav Das6c44a632015-05-30 22:05:22 -070033import org.onosproject.net.PortNumber;
Saurav Dase3274c82015-05-24 17:21:56 -070034import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleOperationsContext;
44import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flow.criteria.Criterion;
48import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.PortCriterion;
51import org.onosproject.net.flow.criteria.EthTypeCriterion;
52import org.onosproject.net.flow.criteria.IPCriterion;
53import org.onosproject.net.flow.criteria.VlanIdCriterion;
54import org.onosproject.net.flow.instructions.Instruction;
55import org.onosproject.net.flow.instructions.L2ModificationInstruction;
56import org.onosproject.net.flowobjective.FilteringObjective;
57import org.onosproject.net.flowobjective.FlowObjectiveStore;
58import org.onosproject.net.flowobjective.ForwardingObjective;
59import org.onosproject.net.flowobjective.NextObjective;
60import org.onosproject.net.flowobjective.Objective;
61import org.onosproject.net.flowobjective.ObjectiveError;
62import org.onosproject.net.group.DefaultGroupBucket;
63import org.onosproject.net.group.DefaultGroupDescription;
64import org.onosproject.net.group.DefaultGroupKey;
65import org.onosproject.net.group.Group;
66import org.onosproject.net.group.GroupBucket;
67import org.onosproject.net.group.GroupBuckets;
68import org.onosproject.net.group.GroupDescription;
69import org.onosproject.net.group.GroupEvent;
70import org.onosproject.net.group.GroupKey;
71import org.onosproject.net.group.GroupListener;
72import org.onosproject.net.group.GroupService;
73import org.slf4j.Logger;
74
75import java.util.Collection;
76import java.util.Collections;
Sho SHIMIZU45906042016-01-13 23:05:54 -080077import java.util.Objects;
Saurav Dase3274c82015-05-24 17:21:56 -070078import java.util.Set;
79import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.TimeUnit;
82import java.util.stream.Collectors;
83
84import static org.onlab.util.Tools.groupedThreads;
85import static org.slf4j.LoggerFactory.getLogger;
86
87/**
88 * Driver for Centec's V350 switches.
89 */
90public class CentecV350Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
91
92 protected static final int PORT_VLAN_TABLE = 0;
93 protected static final int FILTER_TABLE = 1;
94 // TMAC is configured in MAC Table to redirect packets to ROUTE_TABLE.
95 protected static final int MAC_TABLE = 2;
96 protected static final int ROUTE_TABLE = 3;
97
98 private static final long DEFAULT_METADATA = 100;
Saurav Das6c44a632015-05-30 22:05:22 -070099 private static final long DEFAULT_METADATA_MASK = 0xffffffffffffffffL;
Saurav Dase3274c82015-05-24 17:21:56 -0700100
101 // Priority used in PORT_VLAN Table, the only priority accepted is PORT_VLAN_TABLE_PRIORITY.
102 // The packet passed PORT+VLAN check will goto FILTER Table.
103 private static final int PORT_VLAN_TABLE_PRIORITY = 0xffff;
104
105 // Priority used in Filter Table.
106 private static final int FILTER_TABLE_CONTROLLER_PRIORITY = 500;
107 // TMAC priority should be lower than controller.
108 private static final int FILTER_TABLE_TMAC_PRIORITY = 200;
109 private static final int FILTER_TABLE_HIGHEST_PRIORITY = 0xffff;
110
111 // Priority used in MAC Table.
112 // We do exact matching for DMAC+metadata, so priority is ignored and required to be set to 0xffff.
113 private static final int MAC_TABLE_PRIORITY = 0xffff;
114
115 // Priority used in Route Table.
116 // We do LPM matching in Route Table, so priority is ignored and required to be set to 0xffff.
117 private static final int ROUTE_TABLE_PRIORITY = 0xffff;
118
119 private static final short BGP_PORT = 179;
120
121 private final Logger log = getLogger(getClass());
122
123 private ServiceDirectory serviceDirectory;
124 private FlowRuleService flowRuleService;
125 private CoreService coreService;
126 private GroupService groupService;
127 private FlowObjectiveStore flowObjectiveStore;
128 private DeviceId deviceId;
129 private ApplicationId appId;
130
131 private KryoNamespace appKryo = new KryoNamespace.Builder()
132 .register(GroupKey.class)
133 .register(DefaultGroupKey.class)
134 .register(CentecV350Group.class)
135 .register(byte[].class)
136 .build();
137
138 private Cache<GroupKey, NextObjective> pendingGroups;
139
140 private ScheduledExecutorService groupChecker =
141 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner",
142 "centec-V350-%d"));
143
144 @Override
145 public void init(DeviceId deviceId, PipelinerContext context) {
146 this.serviceDirectory = context.directory();
147 this.deviceId = deviceId;
148
149 pendingGroups = CacheBuilder.newBuilder()
150 .expireAfterWrite(20, TimeUnit.SECONDS)
151 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
152 if (notification.getCause() == RemovalCause.EXPIRED) {
153 fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED);
154 }
155 }).build();
156
157 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
158
159 coreService = serviceDirectory.get(CoreService.class);
160 flowRuleService = serviceDirectory.get(FlowRuleService.class);
161 groupService = serviceDirectory.get(GroupService.class);
162 flowObjectiveStore = context.store();
163
164 groupService.addListener(new InnerGroupListener());
165
166 appId = coreService.registerApplication(
167 "org.onosproject.driver.CentecV350Pipeline");
168
169 initializePipeline();
170 }
171
172 @Override
173 public void filter(FilteringObjective filteringObjective) {
174 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
175 processFilter(filteringObjective,
176 filteringObjective.op() == Objective.Operation.ADD,
177 filteringObjective.appId());
178 } else {
179 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
180 }
181 }
182
183 @Override
184 public void forward(ForwardingObjective fwd) {
185 Collection<FlowRule> rules;
186 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
187
188 rules = processForward(fwd);
189 switch (fwd.op()) {
190 case ADD:
191 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800192 .filter(Objects::nonNull)
Saurav Dase3274c82015-05-24 17:21:56 -0700193 .forEach(flowBuilder::add);
194 break;
195 case REMOVE:
196 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800197 .filter(Objects::nonNull)
Saurav Dase3274c82015-05-24 17:21:56 -0700198 .forEach(flowBuilder::remove);
199 break;
200 default:
201 fail(fwd, ObjectiveError.UNKNOWN);
202 log.warn("Unknown forwarding type {}", fwd.op());
203 }
204
205
206 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
207 @Override
208 public void onSuccess(FlowRuleOperations ops) {
209 pass(fwd);
210 }
211
212 @Override
213 public void onError(FlowRuleOperations ops) {
214 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
215 }
216 }));
217
218 }
219
220 @Override
221 public void next(NextObjective nextObjective) {
222 switch (nextObjective.type()) {
223 case SIMPLE:
224 Collection<TrafficTreatment> treatments = nextObjective.next();
225 if (treatments.size() == 1) {
226 TrafficTreatment treatment = treatments.iterator().next();
227
228 // Since we do not support strip_vlan in PORT_VLAN table, we use mod_vlan
229 // to modify the packet to desired vlan.
230 // Note: if we use push_vlan here, the switch will add a second VLAN tag to the outgoing
231 // packet, which is not what we want.
232 TrafficTreatment.Builder treatmentWithoutPushVlan = DefaultTrafficTreatment.builder();
233 VlanId modVlanId;
234 for (Instruction ins : treatment.allInstructions()) {
235 if (ins.type() == Instruction.Type.L2MODIFICATION) {
236 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
237 switch (l2ins.subtype()) {
238 case ETH_DST:
239 treatmentWithoutPushVlan.setEthDst(
240 ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac());
241 break;
242 case ETH_SRC:
243 treatmentWithoutPushVlan.setEthSrc(
244 ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac());
245 break;
246 case VLAN_ID:
247 modVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
248 treatmentWithoutPushVlan.setVlanId(modVlanId);
249 break;
250 default:
251 break;
252 }
253 } else if (ins.type() == Instruction.Type.OUTPUT) {
254 //long portNum = ((Instructions.OutputInstruction) ins).port().toLong();
255 treatmentWithoutPushVlan.add(ins);
256 } else {
257 // Ignore the vlan_pcp action since it's does matter much.
258 log.warn("Driver does not handle this type of TrafficTreatment"
259 + " instruction in nextObjectives: {}", ins.type());
260 }
261 }
262
263 GroupBucket bucket =
264 DefaultGroupBucket.createIndirectGroupBucket(treatmentWithoutPushVlan.build());
265 final GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
266 GroupDescription groupDescription
267 = new DefaultGroupDescription(deviceId,
268 GroupDescription.Type.INDIRECT,
269 new GroupBuckets(Collections
270 .singletonList(bucket)),
271 key,
272 null, // let group service determine group id
273 nextObjective.appId());
274 groupService.addGroup(groupDescription);
275 pendingGroups.put(key, nextObjective);
276 }
277 break;
278 case HASHED:
279 case BROADCAST:
280 case FAILOVER:
281 fail(nextObjective, ObjectiveError.UNSUPPORTED);
282 log.warn("Unsupported next objective type {}", nextObjective.type());
283 break;
284 default:
285 fail(nextObjective, ObjectiveError.UNKNOWN);
286 log.warn("Unknown next objective type {}", nextObjective.type());
287 }
288
289 }
290
291 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
292 switch (fwd.flag()) {
293 case SPECIFIC:
294 return processSpecific(fwd);
295 case VERSATILE:
296 return processVersatile(fwd);
297 default:
298 fail(fwd, ObjectiveError.UNKNOWN);
299 log.warn("Unknown forwarding flag {}", fwd.flag());
300 }
301 return Collections.emptySet();
302 }
303
304 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
305 log.warn("Driver does not support versatile forwarding objective");
306 fail(fwd, ObjectiveError.UNSUPPORTED);
307 return Collections.emptySet();
308 }
309
310 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
311 log.debug("Processing specific forwarding objective");
312 TrafficSelector selector = fwd.selector();
313 EthTypeCriterion ethType =
314 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
alshabibcaf1ca22015-06-25 15:18:16 -0700315 if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) {
Saurav Dase3274c82015-05-24 17:21:56 -0700316 fail(fwd, ObjectiveError.UNSUPPORTED);
317 return Collections.emptySet();
318 }
319
320 // Must have metadata as key.
321 TrafficSelector filteredSelector =
322 DefaultTrafficSelector.builder()
323 .matchEthType(Ethernet.TYPE_IPV4)
324 .matchMetadata(DEFAULT_METADATA)
325 .matchIPDst(
326 ((IPCriterion)
327 selector.getCriterion(Criterion.Type.IPV4_DST)).ip())
328 .build();
329
330 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
331
332 if (fwd.nextId() != null) {
333 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
334 GroupKey key = appKryo.deserialize(next.data());
335 Group group = groupService.getGroup(deviceId, key);
336 if (group == null) {
337 log.warn("The group left!");
338 fail(fwd, ObjectiveError.GROUPMISSING);
339 return Collections.emptySet();
340 }
341 tb.group(group.id());
342 }
343
344 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
345 .fromApp(fwd.appId())
346 .withPriority(ROUTE_TABLE_PRIORITY)
347 .forDevice(deviceId)
348 .withSelector(filteredSelector)
349 .withTreatment(tb.build());
350
351 if (fwd.permanent()) {
352 ruleBuilder.makePermanent();
353 } else {
354 ruleBuilder.makeTemporary(fwd.timeout());
355 }
356
357 ruleBuilder.forTable(ROUTE_TABLE);
358
359 return Collections.singletonList(ruleBuilder.build());
360
361 }
362
363 private void processFilter(FilteringObjective filt, boolean install,
364 ApplicationId applicationId) {
365 PortCriterion p;
366 if (!filt.key().equals(Criteria.dummy()) &&
367 filt.key().type() == Criterion.Type.IN_PORT) {
368 p = (PortCriterion) filt.key();
369 } else {
370 log.warn("No key defined in filtering objective from app: {}. Not"
371 + "processing filtering objective", applicationId);
372 fail(filt, ObjectiveError.UNKNOWN);
373 return;
374 }
375
376 // Convert filtering conditions for switch-intfs into flow rules.
377 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
378
379 for (Criterion c : filt.conditions()) {
380 // Here we do a trick to install 2 flow rules to MAC_TABLE and ROUTE_TABLE.
381 if (c.type() == Criterion.Type.ETH_DST) {
382 EthCriterion e = (EthCriterion) c;
383
384 // Install TMAC flow rule.
385 log.debug("adding rule for Termination MAC in Filter Table: {}", e.mac());
386 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
387 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
388 selector.matchEthDst(e.mac());
389 // Add IPv4 matching explicitly since we will redirect it to ROUTE Table
390 // through MAC table.
391 selector.matchEthType(Ethernet.TYPE_IPV4);
392 treatment.transition(MAC_TABLE);
393 FlowRule rule = DefaultFlowRule.builder()
394 .forDevice(deviceId)
395 .withSelector(selector.build())
396 .withTreatment(treatment.build())
397 .withPriority(FILTER_TABLE_TMAC_PRIORITY)
398 .fromApp(applicationId)
399 .makePermanent()
400 .forTable(FILTER_TABLE).build();
401 ops = install ? ops.add(rule) : ops.remove(rule);
402
403 // Must install another rule to direct the IPv4 packets that hit TMAC to
404 // Route table.
405 log.debug("adding rule for Termination MAC in MAC Table: {}", e.mac());
406 selector = DefaultTrafficSelector.builder();
407 treatment = DefaultTrafficTreatment.builder();
408 selector.matchEthDst(e.mac());
409 // MAC_Table must have metadata matching configured, use the default metadata.
410 selector.matchMetadata(DEFAULT_METADATA);
411 treatment.transition(ROUTE_TABLE);
412 rule = DefaultFlowRule.builder()
413 .forDevice(deviceId)
414 .withSelector(selector.build())
415 .withTreatment(treatment.build())
416 .withPriority(MAC_TABLE_PRIORITY)
417 .fromApp(applicationId)
418 .makePermanent()
419 .forTable(MAC_TABLE).build();
420 ops = install ? ops.add(rule) : ops.remove(rule);
421 } else if (c.type() == Criterion.Type.VLAN_VID) {
422 VlanIdCriterion v = (VlanIdCriterion) c;
423 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());
428 // Although the accepted packets will be sent to filter table, we must
429 // explicitly set goto_table instruction here.
Saurav Das86af8f12015-05-25 23:55:33 -0700430 treatment.writeMetadata(DEFAULT_METADATA, DEFAULT_METADATA_MASK);
431 // set default metadata written by PORT_VLAN Table.
Saurav Dase3274c82015-05-24 17:21:56 -0700432 treatment.transition(FILTER_TABLE);
433 // We do not support strip vlan here, treatment.deferred().popVlan();
Saurav Dase3274c82015-05-24 17:21:56 -0700434 // PORT_VLAN table only accept 0xffff priority since it does exact match only.
435 FlowRule rule = DefaultFlowRule.builder()
436 .forDevice(deviceId)
437 .withSelector(selector.build())
438 .withTreatment(treatment.build())
439 .withPriority(PORT_VLAN_TABLE_PRIORITY)
440 .fromApp(applicationId)
441 .makePermanent()
442 .forTable(PORT_VLAN_TABLE).build();
443 ops = install ? ops.add(rule) : ops.remove(rule);
444 } else if (c.type() == Criterion.Type.IPV4_DST) {
Saurav Das6c44a632015-05-30 22:05:22 -0700445 IPCriterion ipaddr = (IPCriterion) c;
446 log.debug("adding IP filtering rules in FILTER table: {}", ipaddr.ip());
447 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
448 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
449 selector.matchEthType(Ethernet.TYPE_IPV4);
450 selector.matchIPDst(ipaddr.ip()); // router IPs to the controller
451 treatment.setOutput(PortNumber.CONTROLLER);
452 FlowRule rule = DefaultFlowRule.builder()
453 .forDevice(deviceId)
454 .withSelector(selector.build())
455 .withTreatment(treatment.build())
456 .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY)
457 .fromApp(applicationId)
458 .makePermanent()
459 .forTable(FILTER_TABLE).build();
460 ops = install ? ops.add(rule) : ops.remove(rule);
Saurav Dase3274c82015-05-24 17:21:56 -0700461 } else {
462 log.warn("Driver does not currently process filtering condition"
463 + " of type: {}", c.type());
464 fail(filt, ObjectiveError.UNSUPPORTED);
465 }
466 }
467
468 // apply filtering flow rules
469 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
470 @Override
471 public void onSuccess(FlowRuleOperations ops) {
472 pass(filt);
473 log.info("Applied filtering rules");
474 }
475
476 @Override
477 public void onError(FlowRuleOperations ops) {
478 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
479 log.info("Failed to apply filtering rules");
480 }
481 }));
482 }
483
484 private void pass(Objective obj) {
485 if (obj.context().isPresent()) {
486 obj.context().get().onSuccess(obj);
487 }
488 }
489
490 private void fail(Objective obj, ObjectiveError error) {
491 if (obj.context().isPresent()) {
492 obj.context().get().onError(obj, error);
493 }
494 }
495
496 private void initializePipeline() {
497 // CENTEC_V350: PORT_VLAN_TABLE->FILTER_TABLE->MAC_TABLE(TMAC)->ROUTE_TABLE.
498 processPortVlanTable(true);
499 processFilterTable(true);
500 }
501
502 private void processPortVlanTable(boolean install) {
503 // By default the packet are dropped, need install port+vlan by some ways.
504
505 // XXX can we add table-miss-entry to drop? Code says drops by default
506 // XXX TTP description says default goes to table1.
507 // It also says that match is only on vlan -- not port-vlan -- which one is true?
508 }
509
510 private void processFilterTable(boolean install) {
511 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
512 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
513 .builder();
514 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
515 FlowRule rule;
516
517 // Punt ARP packets to controller by default.
518 selector.matchEthType(Ethernet.TYPE_ARP);
519 treatment.punt();
520 rule = DefaultFlowRule.builder()
521 .forDevice(deviceId)
522 .withSelector(selector.build())
523 .withTreatment(treatment.build())
524 .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY)
525 .fromApp(appId)
526 .makePermanent()
527 .forTable(FILTER_TABLE).build();
528 ops = install ? ops.add(rule) : ops.remove(rule);
529
530 // Punt BGP packets to controller directly.
531 selector = DefaultTrafficSelector.builder();
532 treatment = DefaultTrafficTreatment.builder();
533 selector.matchEthType(Ethernet.TYPE_IPV4)
534 .matchIPProtocol(IPv4.PROTOCOL_TCP)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700535 .matchTcpSrc(TpPort.tpPort(BGP_PORT));
Saurav Dase3274c82015-05-24 17:21:56 -0700536 treatment.punt();
537 rule = DefaultFlowRule.builder()
538 .forDevice(deviceId)
539 .withPriority(FILTER_TABLE_HIGHEST_PRIORITY)
540 .withSelector(selector.build())
541 .withTreatment(treatment.build())
542 .fromApp(appId)
543 .makePermanent()
544 .forTable(FILTER_TABLE).build();
545 ops = install ? ops.add(rule) : ops.remove(rule);
546
547 selector = DefaultTrafficSelector.builder();
548 treatment = DefaultTrafficTreatment.builder();
549 selector.matchEthType(Ethernet.TYPE_IPV4)
550 .matchIPProtocol(IPv4.PROTOCOL_TCP)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700551 .matchTcpDst(TpPort.tpPort(BGP_PORT));
Saurav Dase3274c82015-05-24 17:21:56 -0700552 treatment.punt();
553 rule = DefaultFlowRule.builder()
554 .forDevice(deviceId)
555 .withPriority(FILTER_TABLE_HIGHEST_PRIORITY)
556 .withSelector(selector.build())
557 .withTreatment(treatment.build())
558 .fromApp(appId)
559 .makePermanent()
560 .forTable(FILTER_TABLE).build();
561
562 ops = install ? ops.add(rule) : ops.remove(rule);
563
564 // Packet will be discard in PORT_VLAN table, no need to install rule in
565 // filter table.
566
567 // XXX does not tell me if packets are going to be dropped by default in
568 // filter table or not? TTP says it will be dropped by default
569
570 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
571 @Override
572 public void onSuccess(FlowRuleOperations ops) {
573 log.info("Provisioned filter table");
574 }
575
576 @Override
577 public void onError(FlowRuleOperations ops) {
578 log.info("Failed to provision filter table");
579 }
580 }));
581 }
582
583 private class InnerGroupListener implements GroupListener {
584 @Override
585 public void event(GroupEvent event) {
586 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
587 GroupKey key = event.subject().appCookie();
588
589 NextObjective obj = pendingGroups.getIfPresent(key);
590 if (obj != null) {
591 flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key));
592 pass(obj);
593 pendingGroups.invalidate(key);
594 }
595 }
596 }
597 }
598
599
600 private class GroupChecker implements Runnable {
601
602 @Override
603 public void run() {
604 Set<GroupKey> keys = pendingGroups.asMap().keySet().stream()
605 .filter(key -> groupService.getGroup(deviceId, key) != null)
606 .collect(Collectors.toSet());
607
608 keys.stream().forEach(key -> {
609 NextObjective obj = pendingGroups.getIfPresent(key);
610 if (obj == null) {
611 return;
612 }
613 pass(obj);
614 pendingGroups.invalidate(key);
615 log.info("Heard back from group service for group {}. "
616 + "Applying pending forwarding objectives", obj.id());
617 flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key));
618 });
619 }
620 }
621
622 private class CentecV350Group implements NextGroup {
623
624 private final GroupKey key;
625
626 public CentecV350Group(GroupKey key) {
627 this.key = key;
628 }
629
630 @SuppressWarnings("unused")
631 public GroupKey key() {
632 return key;
633 }
634
635 @Override
636 public byte[] data() {
637 return appKryo.serialize(key);
638 }
639
640 }
641}