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