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