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