blob: dedc810283378e1bab835ef7c256c099299890c7 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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)
Charles Chaneefdedf2016-05-23 16:45:45 -0700137 .build("CentecV350Pipeline");
Saurav Dase3274c82015-05-24 17:21:56 -0700138
139 private Cache<GroupKey, NextObjective> pendingGroups;
140
141 private ScheduledExecutorService groupChecker =
142 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner",
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700143 "centec-V350-%d", log));
Saurav Dase3274c82015-05-24 17:21:56 -0700144
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
Daniele Moro06aac702021-07-19 22:39:22 +0200292 @Override
293 public void purgeAll(ApplicationId appId) {
294 flowRuleService.purgeFlowRules(deviceId, appId);
295 }
296
Saurav Dase3274c82015-05-24 17:21:56 -0700297 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
298 switch (fwd.flag()) {
299 case SPECIFIC:
300 return processSpecific(fwd);
301 case VERSATILE:
302 return processVersatile(fwd);
303 default:
304 fail(fwd, ObjectiveError.UNKNOWN);
305 log.warn("Unknown forwarding flag {}", fwd.flag());
306 }
307 return Collections.emptySet();
308 }
309
310 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
311 log.warn("Driver does not support versatile forwarding objective");
312 fail(fwd, ObjectiveError.UNSUPPORTED);
313 return Collections.emptySet();
314 }
315
316 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
317 log.debug("Processing specific forwarding objective");
318 TrafficSelector selector = fwd.selector();
319 EthTypeCriterion ethType =
320 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
alshabibcaf1ca22015-06-25 15:18:16 -0700321 if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) {
Saurav Dase3274c82015-05-24 17:21:56 -0700322 fail(fwd, ObjectiveError.UNSUPPORTED);
323 return Collections.emptySet();
324 }
325
326 // Must have metadata as key.
327 TrafficSelector filteredSelector =
328 DefaultTrafficSelector.builder()
329 .matchEthType(Ethernet.TYPE_IPV4)
330 .matchMetadata(DEFAULT_METADATA)
331 .matchIPDst(
332 ((IPCriterion)
333 selector.getCriterion(Criterion.Type.IPV4_DST)).ip())
334 .build();
335
336 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
337
338 if (fwd.nextId() != null) {
339 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
340 GroupKey key = appKryo.deserialize(next.data());
341 Group group = groupService.getGroup(deviceId, key);
342 if (group == null) {
343 log.warn("The group left!");
344 fail(fwd, ObjectiveError.GROUPMISSING);
345 return Collections.emptySet();
346 }
347 tb.group(group.id());
348 }
349
350 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
351 .fromApp(fwd.appId())
352 .withPriority(ROUTE_TABLE_PRIORITY)
353 .forDevice(deviceId)
354 .withSelector(filteredSelector)
355 .withTreatment(tb.build());
356
357 if (fwd.permanent()) {
358 ruleBuilder.makePermanent();
359 } else {
360 ruleBuilder.makeTemporary(fwd.timeout());
361 }
362
363 ruleBuilder.forTable(ROUTE_TABLE);
364
365 return Collections.singletonList(ruleBuilder.build());
366
367 }
368
369 private void processFilter(FilteringObjective filt, boolean install,
370 ApplicationId applicationId) {
371 PortCriterion p;
372 if (!filt.key().equals(Criteria.dummy()) &&
373 filt.key().type() == Criterion.Type.IN_PORT) {
374 p = (PortCriterion) filt.key();
375 } else {
376 log.warn("No key defined in filtering objective from app: {}. Not"
377 + "processing filtering objective", applicationId);
378 fail(filt, ObjectiveError.UNKNOWN);
379 return;
380 }
381
382 // Convert filtering conditions for switch-intfs into flow rules.
383 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
384
385 for (Criterion c : filt.conditions()) {
386 // Here we do a trick to install 2 flow rules to MAC_TABLE and ROUTE_TABLE.
387 if (c.type() == Criterion.Type.ETH_DST) {
388 EthCriterion e = (EthCriterion) c;
389
390 // Install TMAC flow rule.
391 log.debug("adding rule for Termination MAC in Filter Table: {}", e.mac());
392 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
393 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
394 selector.matchEthDst(e.mac());
395 // Add IPv4 matching explicitly since we will redirect it to ROUTE Table
396 // through MAC table.
397 selector.matchEthType(Ethernet.TYPE_IPV4);
398 treatment.transition(MAC_TABLE);
399 FlowRule rule = DefaultFlowRule.builder()
400 .forDevice(deviceId)
401 .withSelector(selector.build())
402 .withTreatment(treatment.build())
403 .withPriority(FILTER_TABLE_TMAC_PRIORITY)
404 .fromApp(applicationId)
405 .makePermanent()
406 .forTable(FILTER_TABLE).build();
407 ops = install ? ops.add(rule) : ops.remove(rule);
408
409 // Must install another rule to direct the IPv4 packets that hit TMAC to
410 // Route table.
411 log.debug("adding rule for Termination MAC in MAC Table: {}", e.mac());
412 selector = DefaultTrafficSelector.builder();
413 treatment = DefaultTrafficTreatment.builder();
414 selector.matchEthDst(e.mac());
415 // MAC_Table must have metadata matching configured, use the default metadata.
416 selector.matchMetadata(DEFAULT_METADATA);
417 treatment.transition(ROUTE_TABLE);
418 rule = DefaultFlowRule.builder()
419 .forDevice(deviceId)
420 .withSelector(selector.build())
421 .withTreatment(treatment.build())
422 .withPriority(MAC_TABLE_PRIORITY)
423 .fromApp(applicationId)
424 .makePermanent()
425 .forTable(MAC_TABLE).build();
426 ops = install ? ops.add(rule) : ops.remove(rule);
427 } else if (c.type() == Criterion.Type.VLAN_VID) {
428 VlanIdCriterion v = (VlanIdCriterion) c;
429 log.debug("adding rule for VLAN: {}", v.vlanId());
430 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
431 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
432 selector.matchVlanId(v.vlanId());
433 selector.matchInPort(p.port());
434 // Although the accepted packets will be sent to filter table, we must
435 // explicitly set goto_table instruction here.
Saurav Das86af8f12015-05-25 23:55:33 -0700436 treatment.writeMetadata(DEFAULT_METADATA, DEFAULT_METADATA_MASK);
437 // set default metadata written by PORT_VLAN Table.
Saurav Dase3274c82015-05-24 17:21:56 -0700438 treatment.transition(FILTER_TABLE);
439 // We do not support strip vlan here, treatment.deferred().popVlan();
Saurav Dase3274c82015-05-24 17:21:56 -0700440 // PORT_VLAN table only accept 0xffff priority since it does exact match only.
441 FlowRule rule = DefaultFlowRule.builder()
442 .forDevice(deviceId)
443 .withSelector(selector.build())
444 .withTreatment(treatment.build())
445 .withPriority(PORT_VLAN_TABLE_PRIORITY)
446 .fromApp(applicationId)
447 .makePermanent()
448 .forTable(PORT_VLAN_TABLE).build();
449 ops = install ? ops.add(rule) : ops.remove(rule);
450 } else if (c.type() == Criterion.Type.IPV4_DST) {
Saurav Das6c44a632015-05-30 22:05:22 -0700451 IPCriterion ipaddr = (IPCriterion) c;
452 log.debug("adding IP filtering rules in FILTER table: {}", ipaddr.ip());
453 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
454 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
455 selector.matchEthType(Ethernet.TYPE_IPV4);
456 selector.matchIPDst(ipaddr.ip()); // router IPs to the controller
457 treatment.setOutput(PortNumber.CONTROLLER);
458 FlowRule rule = DefaultFlowRule.builder()
459 .forDevice(deviceId)
460 .withSelector(selector.build())
461 .withTreatment(treatment.build())
462 .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY)
463 .fromApp(applicationId)
464 .makePermanent()
465 .forTable(FILTER_TABLE).build();
466 ops = install ? ops.add(rule) : ops.remove(rule);
Saurav Dase3274c82015-05-24 17:21:56 -0700467 } else {
468 log.warn("Driver does not currently process filtering condition"
469 + " of type: {}", c.type());
470 fail(filt, ObjectiveError.UNSUPPORTED);
471 }
472 }
473
474 // apply filtering flow rules
475 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
476 @Override
477 public void onSuccess(FlowRuleOperations ops) {
478 pass(filt);
479 log.info("Applied filtering rules");
480 }
481
482 @Override
483 public void onError(FlowRuleOperations ops) {
484 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
485 log.info("Failed to apply filtering rules");
486 }
487 }));
488 }
489
490 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800491 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Dase3274c82015-05-24 17:21:56 -0700492 }
493
494 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800495 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Dase3274c82015-05-24 17:21:56 -0700496 }
497
498 private void initializePipeline() {
499 // CENTEC_V350: PORT_VLAN_TABLE->FILTER_TABLE->MAC_TABLE(TMAC)->ROUTE_TABLE.
500 processPortVlanTable(true);
501 processFilterTable(true);
502 }
503
504 private void processPortVlanTable(boolean install) {
505 // By default the packet are dropped, need install port+vlan by some ways.
506
507 // XXX can we add table-miss-entry to drop? Code says drops by default
508 // XXX TTP description says default goes to table1.
509 // It also says that match is only on vlan -- not port-vlan -- which one is true?
510 }
511
512 private void processFilterTable(boolean install) {
513 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
514 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
515 .builder();
516 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
517 FlowRule rule;
518
519 // Punt ARP packets to controller by default.
520 selector.matchEthType(Ethernet.TYPE_ARP);
521 treatment.punt();
522 rule = DefaultFlowRule.builder()
523 .forDevice(deviceId)
524 .withSelector(selector.build())
525 .withTreatment(treatment.build())
526 .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY)
527 .fromApp(appId)
528 .makePermanent()
529 .forTable(FILTER_TABLE).build();
530 ops = install ? ops.add(rule) : ops.remove(rule);
531
532 // Punt BGP packets to controller directly.
533 selector = DefaultTrafficSelector.builder();
534 treatment = DefaultTrafficTreatment.builder();
535 selector.matchEthType(Ethernet.TYPE_IPV4)
536 .matchIPProtocol(IPv4.PROTOCOL_TCP)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700537 .matchTcpSrc(TpPort.tpPort(BGP_PORT));
Saurav Dase3274c82015-05-24 17:21:56 -0700538 treatment.punt();
539 rule = DefaultFlowRule.builder()
540 .forDevice(deviceId)
541 .withPriority(FILTER_TABLE_HIGHEST_PRIORITY)
542 .withSelector(selector.build())
543 .withTreatment(treatment.build())
544 .fromApp(appId)
545 .makePermanent()
546 .forTable(FILTER_TABLE).build();
547 ops = install ? ops.add(rule) : ops.remove(rule);
548
549 selector = DefaultTrafficSelector.builder();
550 treatment = DefaultTrafficTreatment.builder();
551 selector.matchEthType(Ethernet.TYPE_IPV4)
552 .matchIPProtocol(IPv4.PROTOCOL_TCP)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700553 .matchTcpDst(TpPort.tpPort(BGP_PORT));
Saurav Dase3274c82015-05-24 17:21:56 -0700554 treatment.punt();
555 rule = DefaultFlowRule.builder()
556 .forDevice(deviceId)
557 .withPriority(FILTER_TABLE_HIGHEST_PRIORITY)
558 .withSelector(selector.build())
559 .withTreatment(treatment.build())
560 .fromApp(appId)
561 .makePermanent()
562 .forTable(FILTER_TABLE).build();
563
564 ops = install ? ops.add(rule) : ops.remove(rule);
565
566 // Packet will be discard in PORT_VLAN table, no need to install rule in
567 // filter table.
568
569 // XXX does not tell me if packets are going to be dropped by default in
570 // filter table or not? TTP says it will be dropped by default
571
572 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
573 @Override
574 public void onSuccess(FlowRuleOperations ops) {
575 log.info("Provisioned filter table");
576 }
577
578 @Override
579 public void onError(FlowRuleOperations ops) {
580 log.info("Failed to provision filter table");
581 }
582 }));
583 }
584
585 private class InnerGroupListener implements GroupListener {
586 @Override
587 public void event(GroupEvent event) {
588 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
589 GroupKey key = event.subject().appCookie();
590
591 NextObjective obj = pendingGroups.getIfPresent(key);
592 if (obj != null) {
593 flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key));
594 pass(obj);
595 pendingGroups.invalidate(key);
596 }
597 }
598 }
599 }
600
601
602 private class GroupChecker implements Runnable {
603
604 @Override
605 public void run() {
606 Set<GroupKey> keys = pendingGroups.asMap().keySet().stream()
607 .filter(key -> groupService.getGroup(deviceId, key) != null)
608 .collect(Collectors.toSet());
609
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700610 keys.forEach(key -> {
Saurav Dase3274c82015-05-24 17:21:56 -0700611 NextObjective obj = pendingGroups.getIfPresent(key);
612 if (obj == null) {
613 return;
614 }
615 pass(obj);
616 pendingGroups.invalidate(key);
617 log.info("Heard back from group service for group {}. "
618 + "Applying pending forwarding objectives", obj.id());
619 flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key));
620 });
621 }
622 }
623
624 private class CentecV350Group implements NextGroup {
625
626 private final GroupKey key;
627
628 public CentecV350Group(GroupKey key) {
629 this.key = key;
630 }
631
632 @SuppressWarnings("unused")
633 public GroupKey key() {
634 return key;
635 }
636
637 @Override
638 public byte[] data() {
639 return appKryo.serialize(key);
640 }
641
642 }
Saurav Das24431192016-03-07 19:13:00 -0800643
644 @Override
645 public List<String> getNextMappings(NextGroup nextGroup) {
646 // TODO Implementation deferred to vendor
647 return null;
648 }
Saurav Dase3274c82015-05-24 17:21:56 -0700649}