Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.impl; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ConcurrentHashMultiset; |
| 20 | import com.google.common.collect.HashMultimap; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.collect.Multiset; |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.onlab.packet.Ethernet; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.incubator.net.intf.Interface; |
| 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 37 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigService; |
| 42 | import org.onosproject.net.device.DeviceEvent; |
| 43 | import org.onosproject.net.device.DeviceListener; |
| 44 | import org.onosproject.net.device.DeviceService; |
| 45 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.flow.criteria.Criteria; |
| 50 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 51 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 52 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 53 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 54 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 55 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 56 | import org.onosproject.net.flowobjective.NextObjective; |
| 57 | import org.onosproject.net.flowobjective.Objective; |
| 58 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 59 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 60 | import org.onosproject.routing.FibEntry; |
| 61 | import org.onosproject.routing.FibListener; |
| 62 | import org.onosproject.routing.FibUpdate; |
| 63 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 64 | import org.onosproject.routing.config.RouterConfig; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 65 | import org.slf4j.Logger; |
| 66 | import org.slf4j.LoggerFactory; |
| 67 | |
| 68 | import java.util.Collection; |
| 69 | import java.util.HashMap; |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 70 | import java.util.List; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 71 | import java.util.Map; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 72 | import java.util.Set; |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 73 | import java.util.stream.Collectors; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Programs routes to a single OpenFlow switch. |
| 77 | */ |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 78 | @Component(immediate = true, enabled = false) |
| 79 | public class SingleSwitchFibInstaller { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 80 | |
| 81 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 82 | |
| 83 | private static final int PRIORITY_OFFSET = 100; |
| 84 | private static final int PRIORITY_MULTIPLIER = 5; |
| 85 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 86 | public static final short ASSIGNED_VLAN = 4094; |
| 87 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected CoreService coreService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected RoutingService routingService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected InterfaceService interfaceService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected NetworkConfigService networkConfigService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected FlowObjectiveService flowObjectiveService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected DeviceService deviceService; |
| 105 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 106 | private InternalDeviceListener deviceListener; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 107 | |
| 108 | // Device id of data-plane switch - should be learned from config |
| 109 | private DeviceId deviceId; |
| 110 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 111 | private ConnectPoint controlPlaneConnectPoint; |
| 112 | |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 113 | private List<String> interfaces; |
| 114 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 115 | private ApplicationId routerAppId; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 116 | |
| 117 | // 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, Integer> nextHops = Maps.newHashMap(); |
| 125 | |
| 126 | // Stores FIB updates that are waiting for groups to be set up |
| 127 | private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create(); |
| 128 | |
| 129 | |
| 130 | @Activate |
| 131 | protected void activate() { |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 132 | routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 133 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 134 | deviceListener = new InternalDeviceListener(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 135 | deviceService.addListener(deviceListener); |
| 136 | |
| 137 | routingService.addFibListener(new InternalFibListener()); |
| 138 | routingService.start(); |
| 139 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 140 | updateConfig(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 141 | |
| 142 | log.info("Started"); |
| 143 | } |
| 144 | |
| 145 | @Deactivate |
| 146 | protected void deactivate() { |
| 147 | routingService.stop(); |
| 148 | |
| 149 | deviceService.removeListener(deviceListener); |
| 150 | |
| 151 | //processIntfFilters(false, configService.getInterfaces()); //TODO necessary? |
| 152 | |
| 153 | log.info("Stopped"); |
| 154 | } |
| 155 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 156 | private void updateConfig() { |
| 157 | RouterConfig routerConfig = |
| 158 | networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 159 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 160 | if (routerConfig == null) { |
| 161 | log.info("Router config not available"); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 162 | return; |
| 163 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 164 | controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint(); |
| 165 | log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 166 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 167 | deviceId = routerConfig.getControlPlaneConnectPoint().deviceId(); |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 168 | log.info("Router device ID is {}", deviceId); |
| 169 | |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 170 | interfaces = routerConfig.getInterfaces(); |
| 171 | log.info("Using interfaces: {}", interfaces.isEmpty() ? "all" : interfaces); |
| 172 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 173 | updateDevice(); |
| 174 | } |
| 175 | |
| 176 | private void updateDevice() { |
| 177 | if (deviceId != null && deviceService.isAvailable(deviceId)) { |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 178 | |
| 179 | Set<Interface> intfs; |
| 180 | if (interfaces.isEmpty()) { |
| 181 | intfs = interfaceService.getInterfaces(); |
| 182 | } else { |
| 183 | // TODO need to fix by making interface names globally unique |
| 184 | intfs = interfaceService.getInterfaces().stream() |
| 185 | .filter(intf -> intf.connectPoint().deviceId().equals(deviceId)) |
| 186 | .filter(intf -> interfaces.contains(intf.name())) |
| 187 | .collect(Collectors.toSet()); |
| 188 | } |
| 189 | |
| 190 | processIntfFilters(true, intfs); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 191 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private void updateFibEntry(Collection<FibUpdate> updates) { |
| 195 | Map<FibEntry, Integer> toInstall = new HashMap<>(updates.size()); |
| 196 | |
| 197 | for (FibUpdate update : updates) { |
| 198 | FibEntry entry = update.entry(); |
| 199 | |
| 200 | addNextHop(entry); |
| 201 | |
| 202 | Integer nextId; |
| 203 | synchronized (pendingUpdates) { |
| 204 | nextId = nextHops.get(entry.nextHopIp()); |
| 205 | } |
| 206 | |
| 207 | toInstall.put(update.entry(), nextId); |
| 208 | } |
| 209 | |
| 210 | installFlows(toInstall); |
| 211 | } |
| 212 | |
| 213 | private void installFlows(Map<FibEntry, Integer> entriesToInstall) { |
| 214 | |
| 215 | for (Map.Entry<FibEntry, Integer> entry : entriesToInstall.entrySet()) { |
| 216 | FibEntry fibEntry = entry.getKey(); |
| 217 | Integer nextId = entry.getValue(); |
| 218 | |
| 219 | flowObjectiveService.forward(deviceId, |
| 220 | generateRibForwardingObj(fibEntry.prefix(), nextId).add()); |
| 221 | log.trace("Sending forwarding objective {} -> nextId:{}", fibEntry, nextId); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) { |
| 227 | |
| 228 | for (FibUpdate update : withdraws) { |
| 229 | FibEntry entry = update.entry(); |
| 230 | //Integer nextId = nextHops.get(entry.nextHopIp()); |
| 231 | |
| 232 | /* Group group = deleteNextHop(entry.prefix()); |
| 233 | if (group == null) { |
| 234 | log.warn("Group not found when deleting {}", entry); |
| 235 | return; |
| 236 | }*/ |
| 237 | |
| 238 | flowObjectiveService.forward(deviceId, |
| 239 | generateRibForwardingObj(entry.prefix(), null).remove()); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | |
| 245 | private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix, |
| 246 | Integer nextId) { |
| 247 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 248 | .matchEthType(Ethernet.TYPE_IPV4) |
| 249 | .matchIPDst(prefix) |
| 250 | .build(); |
| 251 | |
| 252 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 253 | |
| 254 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 255 | .fromApp(routerAppId) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 256 | .makePermanent() |
| 257 | .withSelector(selector) |
| 258 | .withPriority(priority) |
| 259 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 260 | |
| 261 | if (nextId == null) { |
| 262 | // Route withdraws are not specified with next hops. Generating |
| 263 | // dummy treatment as there is no equivalent nextId info. |
| 264 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 265 | } else { |
| 266 | fwdBuilder.nextStep(nextId); |
| 267 | } |
| 268 | return fwdBuilder; |
| 269 | } |
| 270 | |
| 271 | private synchronized void addNextHop(FibEntry entry) { |
| 272 | prefixToNextHop.put(entry.prefix(), entry.nextHopIp()); |
| 273 | if (nextHopsCount.count(entry.nextHopIp()) == 0) { |
| 274 | // There was no next hop in the multiset |
| 275 | |
| 276 | Interface egressIntf = interfaceService.getMatchingInterface(entry.nextHopIp()); |
| 277 | if (egressIntf == null) { |
| 278 | log.warn("no egress interface found for {}", entry); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp()); |
| 283 | |
| 284 | NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey); |
| 285 | |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 286 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 287 | .setEthSrc(egressIntf.mac()) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 288 | .setEthDst(nextHop.mac()); |
| 289 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 290 | TrafficSelector.Builder metabuilder = null; |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 291 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 292 | treatment.pushVlan() |
| 293 | .setVlanId(egressIntf.vlan()) |
| 294 | .setVlanPcp((byte) 0); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 295 | } else { |
| 296 | // untagged outgoing port may require internal vlan in some pipelines |
| 297 | metabuilder = DefaultTrafficSelector.builder(); |
| 298 | metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN)); |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | treatment.setOutput(egressIntf.connectPoint().port()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 302 | |
| 303 | int nextId = flowObjectiveService.allocateNextId(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 304 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 305 | .withId(nextId) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 306 | .addTreatment(treatment.build()) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 307 | .withType(NextObjective.Type.SIMPLE) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 308 | .fromApp(routerAppId); |
| 309 | if (metabuilder != null) { |
| 310 | nextBuilder.withMeta(metabuilder.build()); |
| 311 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 312 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 313 | NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 314 | flowObjectiveService.next(deviceId, nextObjective); |
| 315 | |
| 316 | nextHops.put(nextHop.ip(), nextId); |
| 317 | |
| 318 | } |
| 319 | |
| 320 | nextHopsCount.add(entry.nextHopIp()); |
| 321 | } |
| 322 | |
| 323 | /*private synchronized Group deleteNextHop(IpPrefix prefix) { |
| 324 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 325 | NextHop nextHop = nextHops.get(nextHopIp); |
| 326 | if (nextHop == null) { |
| 327 | log.warn("No next hop found when removing prefix {}", prefix); |
| 328 | return null; |
| 329 | } |
| 330 | |
| 331 | Group group = groupService.getGroup(deviceId, |
| 332 | new DefaultGroupKey(appKryo. |
| 333 | serialize(nextHop.group()))); |
| 334 | |
| 335 | // FIXME disabling group deletes for now until we verify the logic is OK |
| 336 | if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
| 337 | // There was one or less next hops, so there are now none |
| 338 | |
| 339 | log.debug("removing group for next hop {}", nextHop); |
| 340 | |
| 341 | nextHops.remove(nextHopIp); |
| 342 | |
| 343 | groupService.removeGroup(deviceId, |
| 344 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 345 | appId); |
| 346 | } |
| 347 | |
| 348 | return group; |
| 349 | }*/ |
| 350 | |
| 351 | private void processIntfFilters(boolean install, Set<Interface> intfs) { |
| 352 | log.info("Processing {} router interfaces", intfs.size()); |
| 353 | for (Interface intf : intfs) { |
| 354 | if (!intf.connectPoint().deviceId().equals(deviceId)) { |
| 355 | // Ignore interfaces if they are not on the router switch |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 360 | // first add filter for the interface |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 361 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 362 | .addCondition(Criteria.matchEthDst(intf.mac())) |
| 363 | .addCondition(Criteria.matchVlanId(intf.vlan())); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 364 | fob.withPriority(PRIORITY_OFFSET); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 365 | if (intf.vlan() == VlanId.NONE) { |
| 366 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 367 | .pushVlan().setVlanId(VlanId.vlanId(ASSIGNED_VLAN)).build(); |
| 368 | fob.withMeta(tt); |
| 369 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 370 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 371 | fob.permit().fromApp(routerAppId); |
| 372 | sendFilteringObjective(install, fob, intf); |
| 373 | if (controlPlaneConnectPoint != null) { |
| 374 | // then add the same mac/vlan filters for control-plane connect point |
| 375 | fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port())); |
| 376 | sendFilteringObjective(install, fob, intf); |
| 377 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 381 | private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob, |
| 382 | Interface intf) { |
| 383 | flowObjectiveService.filter( |
| 384 | deviceId, |
| 385 | fob.add(new ObjectiveContext() { |
| 386 | @Override |
| 387 | public void onSuccess(Objective objective) { |
| 388 | log.info("Successfully installed interface based " |
| 389 | + "filtering objectives for intf {}", intf); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void onError(Objective objective, |
| 394 | ObjectiveError error) { |
| 395 | log.error("Failed to install interface filters for intf {}: {}", |
| 396 | intf, error); |
| 397 | // TODO something more than just logging |
| 398 | } |
| 399 | })); |
| 400 | } |
| 401 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 402 | private class InternalFibListener implements FibListener { |
| 403 | |
| 404 | @Override |
| 405 | public void update(Collection<FibUpdate> updates, |
| 406 | Collection<FibUpdate> withdraws) { |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 407 | SingleSwitchFibInstaller.this.deleteFibEntry(withdraws); |
| 408 | SingleSwitchFibInstaller.this.updateFibEntry(updates); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 412 | /** |
| 413 | * Listener for device events used to trigger driver setup when a device is |
| 414 | * (re)detected. |
| 415 | */ |
| 416 | private class InternalDeviceListener implements DeviceListener { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 417 | @Override |
| 418 | public void event(DeviceEvent event) { |
| 419 | switch (event.type()) { |
| 420 | case DEVICE_ADDED: |
| 421 | case DEVICE_AVAILABILITY_CHANGED: |
| 422 | if (deviceService.isAvailable(event.subject().id())) { |
| 423 | log.info("Device connected {}", event.subject().id()); |
| 424 | if (event.subject().id().equals(deviceId)) { |
Charles Chan | f555a73 | 2016-02-15 15:37:15 -0800 | [diff] [blame^] | 425 | updateDevice(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | break; |
| 429 | // TODO other cases |
| 430 | case DEVICE_UPDATED: |
| 431 | case DEVICE_REMOVED: |
| 432 | case DEVICE_SUSPENDED: |
| 433 | case PORT_ADDED: |
| 434 | case PORT_UPDATED: |
| 435 | case PORT_REMOVED: |
| 436 | default: |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | } |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * Listener for network config events. |
| 444 | */ |
| 445 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 446 | @Override |
| 447 | public void event(NetworkConfigEvent event) { |
| 448 | if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) { |
| 449 | switch (event.type()) { |
| 450 | case CONFIG_ADDED: |
| 451 | case CONFIG_UPDATED: |
| 452 | updateConfig(); |
| 453 | break; |
| 454 | case CONFIG_REGISTERED: |
| 455 | case CONFIG_UNREGISTERED: |
| 456 | case CONFIG_REMOVED: |
| 457 | default: |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 463 | } |