blob: e5de8122b17a09b452b5c97500025da97dc1e3a7 [file] [log] [blame]
Jonathan Hartf5829202015-02-12 09:37:02 -08001/*
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.bgprouter;
17
18import com.google.common.collect.ConcurrentHashMultiset;
Jonathan Hart7baba072015-02-23 14:27:59 -080019import com.google.common.collect.HashMultimap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Jonathan Hartf5829202015-02-12 09:37:02 -080022import com.google.common.collect.Multiset;
Saurav Dasfbe25c52015-03-04 11:12:00 -080023
Jonathan Hartf5829202015-02-12 09:37:02 -080024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.onlab.packet.Ethernet;
alshabib10580802015-02-18 18:30:33 -080030import org.onlab.packet.MacAddress;
Jonathan Hart7baba072015-02-23 14:27:59 -080031import org.onlab.packet.IpAddress;
32import org.onlab.packet.IpPrefix;
Saurav Dasfbe25c52015-03-04 11:12:00 -080033import org.onlab.packet.VlanId;
Jonathan Hartf5829202015-02-12 09:37:02 -080034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.net.DeviceId;
alshabib346b5b32015-03-06 00:42:16 -080037import org.onosproject.net.PortNumber;
Jonathan Hartf5829202015-02-12 09:37:02 -080038import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
alshabib10580802015-02-18 18:30:33 -080042import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleOperationsContext;
Jonathan Hartf5829202015-02-12 09:37:02 -080044import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Saurav Dasfbe25c52015-03-04 11:12:00 -080047import org.onosproject.net.flow.FlowRule.Type;
Jonathan Hartf5829202015-02-12 09:37:02 -080048import org.onosproject.net.group.DefaultGroupBucket;
49import org.onosproject.net.group.DefaultGroupDescription;
50import org.onosproject.net.group.Group;
51import org.onosproject.net.group.GroupBucket;
52import org.onosproject.net.group.GroupBuckets;
53import org.onosproject.net.group.GroupDescription;
Jonathan Hart7baba072015-02-23 14:27:59 -080054import org.onosproject.net.group.GroupEvent;
Jonathan Hartf5829202015-02-12 09:37:02 -080055import org.onosproject.net.group.GroupKey;
Jonathan Hart7baba072015-02-23 14:27:59 -080056import org.onosproject.net.group.GroupListener;
Jonathan Hartf5829202015-02-12 09:37:02 -080057import org.onosproject.net.group.GroupService;
Saurav Dasfbe25c52015-03-04 11:12:00 -080058import org.onosproject.net.host.InterfaceIpAddress;
Jonathan Hartf5829202015-02-12 09:37:02 -080059import org.onosproject.net.packet.PacketService;
Jonathan Hart7baba072015-02-23 14:27:59 -080060import org.onosproject.routing.FibEntry;
Jonathan Hart2da1e602015-02-18 19:09:24 -080061import org.onosproject.routing.FibListener;
62import org.onosproject.routing.FibUpdate;
63import org.onosproject.routing.RoutingService;
Saurav Dasfbe25c52015-03-04 11:12:00 -080064import org.onosproject.routing.config.BgpSpeaker;
Jonathan Hart2da1e602015-02-18 19:09:24 -080065import org.onosproject.routing.config.Interface;
66import org.onosproject.routing.config.RoutingConfigurationService;
Saurav Dasfbe25c52015-03-04 11:12:00 -080067import org.onosproject.config.NetworkConfigService;
Jonathan Hartf5829202015-02-12 09:37:02 -080068import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
71import java.util.Collection;
72import java.util.Collections;
Saurav Dasfbe25c52015-03-04 11:12:00 -080073import java.util.HashSet;
Jonathan Hartf5829202015-02-12 09:37:02 -080074import java.util.Map;
Saurav Dasfbe25c52015-03-04 11:12:00 -080075import java.util.Set;
Jonathan Hartf5829202015-02-12 09:37:02 -080076
77/**
78 * BgpRouter component.
79 */
80@Component(immediate = true)
81public class BgpRouter {
82
83 private static final Logger log = LoggerFactory.getLogger(BgpRouter.class);
84
85 private static final String BGP_ROUTER_APP = "org.onosproject.bgprouter";
86
87 private static final int PRIORITY = 1;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected FlowRuleService flowService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected GroupService groupService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected RoutingService routingService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected RoutingConfigurationService configService;
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected PacketService packetService;
106
Saurav Dasfbe25c52015-03-04 11:12:00 -0800107 //
108 // NOTE: Unused reference - needed to guarantee that the
109 // NetworkConfigReader component is activated and the network configuration
110 // is read.
111 //
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected NetworkConfigService networkConfigService;
114
Jonathan Hartf5829202015-02-12 09:37:02 -0800115 private ApplicationId appId;
116
Jonathan Hart7baba072015-02-23 14:27:59 -0800117 // Reference count for how many times a next hop is used by a route
118 private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create();
119
120 // Mapping from prefix to its current next hop
121 private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap();
122
123 // Mapping from next hop IP to next hop object containing group info
124 private final Map<IpAddress, NextHop> nextHops = Maps.newHashMap();
125
126 // Stores FIB updates that are waiting for groups to be set up
127 private final Multimap<GroupKey, FibEntry> pendingUpdates = HashMultimap.create();
Jonathan Hartf5829202015-02-12 09:37:02 -0800128
Saurav Dasfbe25c52015-03-04 11:12:00 -0800129 // Device id of data-plane switch - should be learned from config
130 private DeviceId deviceId;
131
132 // Device id of control-plane switch (OVS) connected to BGP Speaker - should be
133 // learned from config
134 private DeviceId ctrlDeviceId;
Jonathan Hartf5829202015-02-12 09:37:02 -0800135
Jonathan Hart7baba072015-02-23 14:27:59 -0800136 private final GroupListener groupListener = new InternalGroupListener();
137
Jonathan Hartf5829202015-02-12 09:37:02 -0800138 private TunnellingConnectivityManager connectivityManager;
139
alshabib10580802015-02-18 18:30:33 -0800140 private InternalTableHandler provisionStaticTables = new InternalTableHandler();
141
Jonathan Hartf5829202015-02-12 09:37:02 -0800142 @Activate
143 protected void activate() {
Jonathan Hartf5829202015-02-12 09:37:02 -0800144 appId = coreService.registerApplication(BGP_ROUTER_APP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800145 getDeviceConfiguration(configService.getBgpSpeakers());
Jonathan Hartf5829202015-02-12 09:37:02 -0800146
Jonathan Hart7baba072015-02-23 14:27:59 -0800147 groupService.addListener(groupListener);
148
Saurav Dasfbe25c52015-03-04 11:12:00 -0800149 provisionStaticTables.provision(true, configService.getInterfaces());
alshabib10580802015-02-18 18:30:33 -0800150
Jonathan Hartf5829202015-02-12 09:37:02 -0800151 connectivityManager = new TunnellingConnectivityManager(appId,
152 configService,
153 packetService);
154
155 routingService.start(new InternalFibListener());
156
157 connectivityManager.start();
158
159 log.info("BgpRouter started");
160 }
161
162 @Deactivate
163 protected void deactivate() {
164 routingService.stop();
165 connectivityManager.stop();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800166 provisionStaticTables.provision(false, configService.getInterfaces());
Jonathan Hartf5829202015-02-12 09:37:02 -0800167
Jonathan Hart7baba072015-02-23 14:27:59 -0800168 groupService.removeListener(groupListener);
169
Jonathan Hartf5829202015-02-12 09:37:02 -0800170 log.info("BgpRouter stopped");
171 }
172
Saurav Dasfbe25c52015-03-04 11:12:00 -0800173 private void getDeviceConfiguration(Map<String, BgpSpeaker> bgps) {
174 if (bgps == null || bgps.values().isEmpty()) {
175 log.error("BGP speakers configuration is missing");
176 return;
177 }
178 for (BgpSpeaker s : bgps.values()) {
179 ctrlDeviceId = s.connectPoint().deviceId();
180 if (s.interfaceAddresses() == null || s.interfaceAddresses().isEmpty()) {
181 log.error("BGP Router must have interfaces configured");
182 return;
183 }
184 deviceId = s.interfaceAddresses().get(0).connectPoint().deviceId();
185 break;
186 }
187 log.info("Router dpid: {}", deviceId);
188 log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
189 }
190
Jonathan Hartf5829202015-02-12 09:37:02 -0800191 private void updateFibEntry(Collection<FibUpdate> updates) {
192 for (FibUpdate update : updates) {
Jonathan Hart7baba072015-02-23 14:27:59 -0800193 FibEntry entry = update.entry();
Jonathan Hartf5829202015-02-12 09:37:02 -0800194
Jonathan Hart7baba072015-02-23 14:27:59 -0800195 addNextHop(entry);
Jonathan Hartf5829202015-02-12 09:37:02 -0800196
Jonathan Hart7baba072015-02-23 14:27:59 -0800197 Group group;
198 synchronized (pendingUpdates) {
199 NextHop nextHop = nextHops.get(entry.nextHopIp());
200 group = groupService.getGroup(deviceId, nextHop.group());
Jonathan Hartf5829202015-02-12 09:37:02 -0800201
Jonathan Hart7baba072015-02-23 14:27:59 -0800202 if (group == null) {
203 log.debug("Adding pending flow {}", update.entry());
204 pendingUpdates.put(nextHop.group(), update.entry());
205 continue;
206 }
Jonathan Hartf5829202015-02-12 09:37:02 -0800207 }
208
Jonathan Hart7baba072015-02-23 14:27:59 -0800209 installFlow(update.entry(), group);
Jonathan Hartf5829202015-02-12 09:37:02 -0800210 }
211 }
212
Jonathan Hart7baba072015-02-23 14:27:59 -0800213 private void installFlow(FibEntry entry, Group group) {
214 TrafficSelector selector = DefaultTrafficSelector.builder()
215 .matchEthType(Ethernet.TYPE_IPV4)
216 .matchIPDst(entry.prefix())
217 .build();
Jonathan Hartf5829202015-02-12 09:37:02 -0800218
Jonathan Hart7baba072015-02-23 14:27:59 -0800219 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
220 .group(group.id())
221 .build();
222
223 FlowRule flowRule = new DefaultFlowRule(deviceId, selector, treatment,
224 PRIORITY, appId, 0, true,
225 FlowRule.Type.IP);
226
227 flowService.applyFlowRules(flowRule);
228 }
229
230 private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) {
231 for (FibUpdate update : withdraws) {
232 FibEntry entry = update.entry();
233
234 deleteNextHop(entry.prefix());
Jonathan Hartf5829202015-02-12 09:37:02 -0800235
236 TrafficSelector selector = DefaultTrafficSelector.builder()
Jonathan Hart7baba072015-02-23 14:27:59 -0800237 .matchEthType(Ethernet.TYPE_IPV4)
Jonathan Hartf5829202015-02-12 09:37:02 -0800238 .matchIPDst(update.entry().prefix())
239 .build();
240
241 FlowRule flowRule = new DefaultFlowRule(deviceId, selector, null,
242 PRIORITY, appId, 0, true,
243 FlowRule.Type.IP);
244
245 flowService.removeFlowRules(flowRule);
246 }
247 }
248
Jonathan Hart7baba072015-02-23 14:27:59 -0800249 private synchronized void addNextHop(FibEntry entry) {
250 prefixToNextHop.put(entry.prefix(), entry.nextHopIp());
251 if (nextHopsCount.count(entry.nextHopIp()) == 0) {
Jonathan Hartf5829202015-02-12 09:37:02 -0800252 // There was no next hop in the multiset
253
Jonathan Hart7baba072015-02-23 14:27:59 -0800254 Interface egressIntf = configService.getMatchingInterface(entry.nextHopIp());
Jonathan Hartf5829202015-02-12 09:37:02 -0800255 if (egressIntf == null) {
Jonathan Hart7baba072015-02-23 14:27:59 -0800256 log.warn("no egress interface found for {}", entry);
Jonathan Hartf5829202015-02-12 09:37:02 -0800257 return;
258 }
259
Jonathan Hart7baba072015-02-23 14:27:59 -0800260 NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp());
261
262 NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey);
Jonathan Hartf5829202015-02-12 09:37:02 -0800263
264 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
265 .setEthSrc(egressIntf.mac())
266 .setEthDst(nextHop.mac())
267 .setVlanId(egressIntf.vlan())
268 .setOutput(egressIntf.connectPoint().port())
269 .build();
270
271 GroupBucket bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment);
272
273 GroupDescription groupDescription
274 = new DefaultGroupDescription(deviceId,
275 GroupDescription.Type.INDIRECT,
276 new GroupBuckets(Collections
277 .singletonList(bucket)),
278 groupKey,
279 appId);
280
281 groupService.addGroup(groupDescription);
Jonathan Hart7baba072015-02-23 14:27:59 -0800282
283 nextHops.put(nextHop.ip(), nextHop);
284
Jonathan Hartf5829202015-02-12 09:37:02 -0800285 }
Jonathan Hart7baba072015-02-23 14:27:59 -0800286
287 nextHopsCount.add(entry.nextHopIp());
Jonathan Hartf5829202015-02-12 09:37:02 -0800288 }
289
Jonathan Hart7baba072015-02-23 14:27:59 -0800290 private synchronized void deleteNextHop(IpPrefix prefix) {
291 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
292 NextHop nextHop = nextHops.get(nextHopIp);
293 if (nextHop == null) {
294 log.warn("No next hop found when removing prefix {}", prefix);
295 return;
296 }
297
298 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
Jonathan Hartf5829202015-02-12 09:37:02 -0800299 // There was one or less next hops, so there are now none
300
Jonathan Hart7baba072015-02-23 14:27:59 -0800301 log.debug("removing group for next hop {}", nextHop);
Jonathan Hartf5829202015-02-12 09:37:02 -0800302
Jonathan Hart7baba072015-02-23 14:27:59 -0800303 nextHops.remove(nextHopIp);
304
305 groupService.removeGroup(deviceId, nextHop.group(), appId);
Jonathan Hartf5829202015-02-12 09:37:02 -0800306 }
307 }
308
309 private class InternalFibListener implements FibListener {
310
311 @Override
312 public void update(Collection<FibUpdate> updates,
313 Collection<FibUpdate> withdraws) {
314 BgpRouter.this.deleteFibEntry(withdraws);
315 BgpRouter.this.updateFibEntry(updates);
316 }
317 }
alshabib10580802015-02-18 18:30:33 -0800318
319 private class InternalTableHandler {
320
321 private static final int CONTROLLER_PRIORITY = 255;
322 private static final int DROP_PRIORITY = 0;
Saurav Dasfbe25c52015-03-04 11:12:00 -0800323 private static final int HIGHEST_PRIORITY = 0xffff;
324 private Set<InterfaceIpAddress> intfIps = new HashSet<InterfaceIpAddress>();
325 private Set<MacAddress> intfMacs = new HashSet<MacAddress>();
alshabib346b5b32015-03-06 00:42:16 -0800326 private Map<PortNumber, VlanId> portVlanPair = Maps.newHashMap();
alshabib10580802015-02-18 18:30:33 -0800327
Saurav Dasfbe25c52015-03-04 11:12:00 -0800328 public void provision(boolean install, Set<Interface> intfs) {
329 getIntefaceConfig(intfs);
alshabib10580802015-02-18 18:30:33 -0800330 processTableZero(install);
331 processTableOne(install);
332 processTableTwo(install);
Saurav Dascbe6de32015-03-01 18:30:46 -0800333 processTableFour(install);
alshabib10580802015-02-18 18:30:33 -0800334 processTableFive(install);
335 processTableSix(install);
336 processTableNine(install);
337
338 }
339
Saurav Dasfbe25c52015-03-04 11:12:00 -0800340 private void getIntefaceConfig(Set<Interface> intfs) {
341 log.info("Processing {} router interfaces", intfs.size());
342 for (Interface intf : intfs) {
343 intfIps.addAll(intf.ipAddresses());
344 intfMacs.add(intf.mac());
alshabib346b5b32015-03-06 00:42:16 -0800345 portVlanPair.put(intf.connectPoint().port(), intf.vlan());
Saurav Dasfbe25c52015-03-04 11:12:00 -0800346 }
347 }
348
alshabib10580802015-02-18 18:30:33 -0800349 private void processTableZero(boolean install) {
350 TrafficSelector.Builder selector;
351 TrafficTreatment.Builder treatment;
352
Saurav Dasfbe25c52015-03-04 11:12:00 -0800353 // Bcast rule
alshabib10580802015-02-18 18:30:33 -0800354 selector = DefaultTrafficSelector.builder();
355 treatment = DefaultTrafficTreatment.builder();
356
357 selector.matchEthDst(MacAddress.BROADCAST);
358 treatment.transition(FlowRule.Type.VLAN_MPLS);
359
360 FlowRule rule = new DefaultFlowRule(deviceId, selector.build(),
Jonathan Hart7baba072015-02-23 14:27:59 -0800361 treatment.build(),
362 CONTROLLER_PRIORITY, appId, 0,
Saurav Dascbe6de32015-03-01 18:30:46 -0800363 true, FlowRule.Type.FIRST);
alshabib10580802015-02-18 18:30:33 -0800364
365 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
366
367 ops = install ? ops.add(rule) : ops.remove(rule);
368
Saurav Dasfbe25c52015-03-04 11:12:00 -0800369 // Interface MACs
370 for (MacAddress mac : intfMacs) {
371 log.debug("adding rule for MAC: {}", mac);
372 selector = DefaultTrafficSelector.builder();
373 treatment = DefaultTrafficTreatment.builder();
374
375 selector.matchEthDst(mac);
376 treatment.transition(FlowRule.Type.VLAN_MPLS);
377
378 rule = new DefaultFlowRule(deviceId, selector.build(),
379 treatment.build(),
380 CONTROLLER_PRIORITY, appId, 0,
381 true, FlowRule.Type.FIRST);
382
383 ops = install ? ops.add(rule) : ops.remove(rule);
384 }
385
alshabib10580802015-02-18 18:30:33 -0800386 //Drop rule
387 selector = DefaultTrafficSelector.builder();
388 treatment = DefaultTrafficTreatment.builder();
389
390 treatment.drop();
391
392 rule = new DefaultFlowRule(deviceId, selector.build(),
Jonathan Hart7baba072015-02-23 14:27:59 -0800393 treatment.build(), DROP_PRIORITY, appId,
Saurav Dascbe6de32015-03-01 18:30:46 -0800394 0, true, FlowRule.Type.FIRST);
alshabib10580802015-02-18 18:30:33 -0800395
396 ops = install ? ops.add(rule) : ops.remove(rule);
397
398 flowService.apply(ops.build(new FlowRuleOperationsContext() {
399 @Override
400 public void onSuccess(FlowRuleOperations ops) {
401 log.info("Provisioned default table for bgp router");
402 }
403
404 @Override
405 public void onError(FlowRuleOperations ops) {
406 log.info("Failed to provision default table for bgp router");
407 }
408 }));
409
410 }
411
412 private void processTableOne(boolean install) {
413 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Jonathan Hart7baba072015-02-23 14:27:59 -0800414 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
415 .builder();
alshabib10580802015-02-18 18:30:33 -0800416 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
417 FlowRule rule;
418
alshabib10580802015-02-18 18:30:33 -0800419
420 selector.matchEthType(Ethernet.TYPE_VLAN);
421 treatment.transition(FlowRule.Type.VLAN);
422
423 rule = new DefaultFlowRule(deviceId, selector.build(),
424 treatment.build(), CONTROLLER_PRIORITY,
425 appId, 0, true, FlowRule.Type.VLAN_MPLS);
426
427 ops = install ? ops.add(rule) : ops.remove(rule);
428
alshabib10580802015-02-18 18:30:33 -0800429 flowService.apply(ops.build(new FlowRuleOperationsContext() {
430 @Override
431 public void onSuccess(FlowRuleOperations ops) {
432 log.info("Provisioned vlan/mpls table for bgp router");
433 }
434
435 @Override
436 public void onError(FlowRuleOperations ops) {
Jonathan Hart7baba072015-02-23 14:27:59 -0800437 log.info(
438 "Failed to provision vlan/mpls table for bgp router");
alshabib10580802015-02-18 18:30:33 -0800439 }
440 }));
441
442 }
443
444 private void processTableTwo(boolean install) {
Saurav Dasfbe25c52015-03-04 11:12:00 -0800445 TrafficSelector.Builder selector;
446 TrafficTreatment.Builder treatment;
alshabib10580802015-02-18 18:30:33 -0800447 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
448 FlowRule rule;
449
Saurav Dasfbe25c52015-03-04 11:12:00 -0800450 //Interface Vlans
alshabib346b5b32015-03-06 00:42:16 -0800451 for (Map.Entry<PortNumber, VlanId> portVlan : portVlanPair.entrySet()) {
452 log.debug("adding rule for VLAN: {}", portVlan);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800453 selector = DefaultTrafficSelector.builder();
454 treatment = DefaultTrafficTreatment.builder();
455
alshabib346b5b32015-03-06 00:42:16 -0800456 selector.matchVlanId(portVlan.getValue());
457 selector.matchInPort(portVlan.getKey());
Saurav Dasfbe25c52015-03-04 11:12:00 -0800458 treatment.transition(Type.ETHER);
alshabib346b5b32015-03-06 00:42:16 -0800459 treatment.deferred().popVlan();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800460
461 rule = new DefaultFlowRule(deviceId, selector.build(),
462 treatment.build(), CONTROLLER_PRIORITY, appId,
463 0, true, FlowRule.Type.VLAN);
464
465 ops = install ? ops.add(rule) : ops.remove(rule);
466 }
467
alshabib10580802015-02-18 18:30:33 -0800468 //Drop rule
Saurav Dasfbe25c52015-03-04 11:12:00 -0800469 selector = DefaultTrafficSelector.builder();
470 treatment = DefaultTrafficTreatment.builder();
alshabib10580802015-02-18 18:30:33 -0800471
472 treatment.drop();
473
474 rule = new DefaultFlowRule(deviceId, selector.build(),
Jonathan Hart7baba072015-02-23 14:27:59 -0800475 treatment.build(), DROP_PRIORITY, appId,
476 0, true, FlowRule.Type.VLAN);
alshabib10580802015-02-18 18:30:33 -0800477
478 ops = install ? ops.add(rule) : ops.remove(rule);
479
480 flowService.apply(ops.build(new FlowRuleOperationsContext() {
481 @Override
482 public void onSuccess(FlowRuleOperations ops) {
483 log.info("Provisioned vlan table for bgp router");
484 }
485
486 @Override
487 public void onError(FlowRuleOperations ops) {
488 log.info("Failed to provision vlan table for bgp router");
489 }
490 }));
491 }
492
Saurav Dascbe6de32015-03-01 18:30:46 -0800493 private void processTableFour(boolean install) {
alshabib10580802015-02-18 18:30:33 -0800494 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Jonathan Hart7baba072015-02-23 14:27:59 -0800495 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
496 .builder();
alshabib10580802015-02-18 18:30:33 -0800497 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
498 FlowRule rule;
499
500 selector.matchEthType(Ethernet.TYPE_ARP);
501 treatment.punt();
502
503 rule = new DefaultFlowRule(deviceId, selector.build(),
504 treatment.build(), CONTROLLER_PRIORITY,
505 appId, 0, true, FlowRule.Type.ETHER);
506
507 ops = install ? ops.add(rule) : ops.remove(rule);
508
509 selector = DefaultTrafficSelector.builder();
510 treatment = DefaultTrafficTreatment.builder();
511
512 selector.matchEthType(Ethernet.TYPE_IPV4);
513 treatment.transition(FlowRule.Type.COS);
514
515 rule = new DefaultFlowRule(deviceId, selector.build(),
516 treatment.build(), CONTROLLER_PRIORITY,
517 appId, 0, true, FlowRule.Type.ETHER);
518
519 ops = install ? ops.add(rule) : ops.remove(rule);
520
521 //Drop rule
522 selector = DefaultTrafficSelector.builder();
523 treatment = DefaultTrafficTreatment.builder();
524
525 treatment.drop();
526
527 rule = new DefaultFlowRule(deviceId, selector.build(),
Jonathan Hart7baba072015-02-23 14:27:59 -0800528 treatment.build(), DROP_PRIORITY, appId,
Saurav Dascbe6de32015-03-01 18:30:46 -0800529 0, true, FlowRule.Type.ETHER);
alshabib10580802015-02-18 18:30:33 -0800530
531 ops = install ? ops.add(rule) : ops.remove(rule);
532
533 flowService.apply(ops.build(new FlowRuleOperationsContext() {
534 @Override
535 public void onSuccess(FlowRuleOperations ops) {
536 log.info("Provisioned ether table for bgp router");
537 }
538
539 @Override
540 public void onError(FlowRuleOperations ops) {
541 log.info("Failed to provision ether table for bgp router");
542 }
543 }));
544
alshabib10580802015-02-18 18:30:33 -0800545 }
546
547 private void processTableFive(boolean install) {
548 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Jonathan Hart7baba072015-02-23 14:27:59 -0800549 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
550 .builder();
alshabib10580802015-02-18 18:30:33 -0800551 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
552 FlowRule rule;
553
554 treatment.transition(FlowRule.Type.IP);
555
556 rule = new DefaultFlowRule(deviceId, selector.build(),
Jonathan Hart7baba072015-02-23 14:27:59 -0800557 treatment.build(), DROP_PRIORITY, appId,
558 0, true, FlowRule.Type.COS);
alshabib10580802015-02-18 18:30:33 -0800559
560 ops = install ? ops.add(rule) : ops.remove(rule);
561
562 flowService.apply(ops.build(new FlowRuleOperationsContext() {
563 @Override
564 public void onSuccess(FlowRuleOperations ops) {
565 log.info("Provisioned cos table for bgp router");
566 }
567
568 @Override
569 public void onError(FlowRuleOperations ops) {
570 log.info("Failed to provision cos table for bgp router");
571 }
572 }));
573
574 }
575
576 private void processTableSix(boolean install) {
Saurav Dasfbe25c52015-03-04 11:12:00 -0800577 TrafficSelector.Builder selector;
578 TrafficTreatment.Builder treatment;
alshabib10580802015-02-18 18:30:33 -0800579 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
580 FlowRule rule;
581
Saurav Dasfbe25c52015-03-04 11:12:00 -0800582
583 //Interface IPs
584 for (InterfaceIpAddress ipAddr : intfIps) {
585 log.debug("adding rule for IPs: {}", ipAddr.ipAddress());
586 selector = DefaultTrafficSelector.builder();
587 treatment = DefaultTrafficTreatment.builder();
588
589 selector.matchEthType(Ethernet.TYPE_IPV4);
590 selector.matchIPDst(IpPrefix.valueOf(ipAddr.ipAddress(), 32));
591 treatment.transition(Type.ACL);
592
593 rule = new DefaultFlowRule(deviceId, selector.build(),
594 treatment.build(), HIGHEST_PRIORITY, appId,
595 0, true, FlowRule.Type.IP);
596
597 ops = install ? ops.add(rule) : ops.remove(rule);
598 }
599
600
alshabib10580802015-02-18 18:30:33 -0800601 //Drop rule
Saurav Dasfbe25c52015-03-04 11:12:00 -0800602 selector = DefaultTrafficSelector.builder();
603 treatment = DefaultTrafficTreatment.builder();
alshabib10580802015-02-18 18:30:33 -0800604
605 treatment.drop();
606
607 rule = new DefaultFlowRule(deviceId, selector.build(),
Jonathan Hart7baba072015-02-23 14:27:59 -0800608 treatment.build(), DROP_PRIORITY, appId,
609 0, true, FlowRule.Type.IP);
alshabib10580802015-02-18 18:30:33 -0800610
611 ops = install ? ops.add(rule) : ops.remove(rule);
612
613 flowService.apply(ops.build(new FlowRuleOperationsContext() {
614 @Override
615 public void onSuccess(FlowRuleOperations ops) {
616 log.info("Provisioned FIB table for bgp router");
617 }
618
619 @Override
620 public void onError(FlowRuleOperations ops) {
621 log.info("Failed to provision FIB table for bgp router");
622 }
623 }));
624 }
625
626 private void processTableNine(boolean install) {
627 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
Jonathan Hart7baba072015-02-23 14:27:59 -0800628 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
629 .builder();
alshabib10580802015-02-18 18:30:33 -0800630 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
631 FlowRule rule;
632
633 treatment.punt();
634
635 rule = new DefaultFlowRule(deviceId, selector.build(),
636 treatment.build(), CONTROLLER_PRIORITY,
Saurav Dasfa2fa932015-03-03 11:29:48 -0800637 appId, 0, true, FlowRule.Type.DEFAULT);
alshabib10580802015-02-18 18:30:33 -0800638
639 ops = install ? ops.add(rule) : ops.remove(rule);
640
641 flowService.apply(ops.build(new FlowRuleOperationsContext() {
642 @Override
643 public void onSuccess(FlowRuleOperations ops) {
644 log.info("Provisioned Local table for bgp router");
645 }
646
647 @Override
648 public void onError(FlowRuleOperations ops) {
649 log.info("Failed to provision Local table for bgp router");
650 }
651 }));
652 }
Jonathan Hart7baba072015-02-23 14:27:59 -0800653 }
alshabib10580802015-02-18 18:30:33 -0800654
Jonathan Hart7baba072015-02-23 14:27:59 -0800655 private class InternalGroupListener implements GroupListener {
656
657 @Override
658 public void event(GroupEvent event) {
659 Group group = event.subject();
660
661 if (event.type() == GroupEvent.Type.GROUP_ADDED ||
662 event.type() == GroupEvent.Type.GROUP_UPDATED) {
663 synchronized (pendingUpdates) {
664 pendingUpdates.removeAll(group.appCookie())
665 .forEach((entry) -> installFlow(entry, group));
666 }
667 }
668 }
alshabib10580802015-02-18 18:30:33 -0800669 }
Jonathan Hartf5829202015-02-12 09:37:02 -0800670}