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