blob: 66767a0c28e274534bd76aeb27babf8dc56ca65e [file] [log] [blame]
Jonathan Hartdf207092015-12-10 11:19:25 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hartdf207092015-12-10 11:19:25 -08003 *
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 Hartc22e8472015-11-17 18:25:45 -080017package org.onosproject.routing.impl;
Jonathan Hartdf207092015-12-10 11:19:25 -080018
19import com.google.common.collect.ConcurrentHashMultiset;
Jonathan Hartdf207092015-12-10 11:19:25 -080020import com.google.common.collect.Maps;
Jonathan Hartdf207092015-12-10 11:19:25 -080021import com.google.common.collect.Multiset;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hartb9401902016-02-02 18:46:01 -080025import org.apache.felix.scr.annotations.Modified;
26import org.apache.felix.scr.annotations.Property;
Jonathan Hartdf207092015-12-10 11:19:25 -080027import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.onlab.packet.Ethernet;
30import org.onlab.packet.IpAddress;
31import org.onlab.packet.IpPrefix;
Charles Chand55e84d2016-03-30 17:54:24 -070032import org.onlab.packet.MacAddress;
Jonathan Hartca47cd72015-12-13 12:31:09 -080033import org.onlab.packet.VlanId;
Jonathan Hartb9401902016-02-02 18:46:01 -080034import org.onlab.util.Tools;
gaurava2e61a52016-05-05 03:39:31 +053035import org.onosproject.app.ApplicationService;
Jonathan Hartb9401902016-02-02 18:46:01 -080036import org.onosproject.cfg.ComponentConfigService;
Jonathan Hartdf207092015-12-10 11:19:25 -080037import org.onosproject.core.ApplicationId;
38import org.onosproject.core.CoreService;
Charles Chand55e84d2016-03-30 17:54:24 -070039import org.onosproject.incubator.net.config.basics.McastConfig;
Jonathan Hartdf207092015-12-10 11:19:25 -080040import org.onosproject.incubator.net.intf.Interface;
gaurav164cf6d2016-03-25 21:43:04 +053041import org.onosproject.incubator.net.intf.InterfaceEvent;
42import org.onosproject.incubator.net.intf.InterfaceListener;
Jonathan Hartdf207092015-12-10 11:19:25 -080043import org.onosproject.incubator.net.intf.InterfaceService;
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070044import org.onosproject.incubator.net.routing.ResolvedRoute;
45import org.onosproject.incubator.net.routing.RouteEvent;
46import org.onosproject.incubator.net.routing.RouteListener;
47import org.onosproject.incubator.net.routing.RouteService;
Saurav Das49cb5a12016-01-16 22:54:07 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hartdf207092015-12-10 11:19:25 -080049import org.onosproject.net.DeviceId;
Charles Chand55e84d2016-03-30 17:54:24 -070050import org.onosproject.net.config.ConfigFactory;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080051import org.onosproject.net.config.NetworkConfigEvent;
52import org.onosproject.net.config.NetworkConfigListener;
Charles Chand55e84d2016-03-30 17:54:24 -070053import org.onosproject.net.config.NetworkConfigRegistry;
Jonathan Hartdf207092015-12-10 11:19:25 -080054import org.onosproject.net.config.NetworkConfigService;
Charles Chand55e84d2016-03-30 17:54:24 -070055import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartdf207092015-12-10 11:19:25 -080056import org.onosproject.net.device.DeviceEvent;
57import org.onosproject.net.device.DeviceListener;
58import org.onosproject.net.device.DeviceService;
59import org.onosproject.net.flow.DefaultTrafficSelector;
60import org.onosproject.net.flow.DefaultTrafficTreatment;
61import org.onosproject.net.flow.TrafficSelector;
62import org.onosproject.net.flow.TrafficTreatment;
63import org.onosproject.net.flow.criteria.Criteria;
64import org.onosproject.net.flowobjective.DefaultFilteringObjective;
65import org.onosproject.net.flowobjective.DefaultForwardingObjective;
66import org.onosproject.net.flowobjective.DefaultNextObjective;
Jonathan Harta2eb9ff2016-04-13 21:27:06 -070067import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Jonathan Hartdf207092015-12-10 11:19:25 -080068import org.onosproject.net.flowobjective.FilteringObjective;
69import org.onosproject.net.flowobjective.FlowObjectiveService;
70import org.onosproject.net.flowobjective.ForwardingObjective;
71import org.onosproject.net.flowobjective.NextObjective;
Jonathan Hartdf207092015-12-10 11:19:25 -080072import org.onosproject.net.flowobjective.ObjectiveContext;
Jonathan Hartdf207092015-12-10 11:19:25 -080073import org.onosproject.routing.RoutingService;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080074import org.onosproject.routing.config.RouterConfig;
Jonathan Hartb9401902016-02-02 18:46:01 -080075import org.osgi.service.component.ComponentContext;
Jonathan Hartdf207092015-12-10 11:19:25 -080076import org.slf4j.Logger;
77import org.slf4j.LoggerFactory;
78
Jonathan Hartb9401902016-02-02 18:46:01 -080079import java.util.Dictionary;
Jonathan Hart883fd372016-02-10 14:36:15 -080080import java.util.List;
Jonathan Hartdf207092015-12-10 11:19:25 -080081import java.util.Map;
Jonathan Hartdf207092015-12-10 11:19:25 -080082import java.util.Set;
Jonathan Hart883fd372016-02-10 14:36:15 -080083import java.util.stream.Collectors;
Jonathan Hartdf207092015-12-10 11:19:25 -080084
85/**
86 * Programs routes to a single OpenFlow switch.
87 */
Jonathan Hartc22e8472015-11-17 18:25:45 -080088@Component(immediate = true, enabled = false)
89public class SingleSwitchFibInstaller {
Jonathan Hartdf207092015-12-10 11:19:25 -080090
91 private final Logger log = LoggerFactory.getLogger(getClass());
gaurava2e61a52016-05-05 03:39:31 +053092 private static final String APP_NAME = "org.onosproject.vrouter";
Jonathan Hartdf207092015-12-10 11:19:25 -080093
94 private static final int PRIORITY_OFFSET = 100;
95 private static final int PRIORITY_MULTIPLIER = 5;
96
Saurav Das49cb5a12016-01-16 22:54:07 -080097 public static final short ASSIGNED_VLAN = 4094;
98
Jonathan Hartdf207092015-12-10 11:19:25 -080099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected CoreService coreService;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700103 protected RouteService routeService;
Jonathan Hartdf207092015-12-10 11:19:25 -0800104
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 Chand55e84d2016-03-30 17:54:24 -0700112 protected NetworkConfigRegistry networkConfigRegistry;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartb9401902016-02-02 18:46:01 -0800115 protected ComponentConfigService componentConfigService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdf207092015-12-10 11:19:25 -0800118 protected FlowObjectiveService flowObjectiveService;
119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected DeviceService deviceService;
122
gaurava2e61a52016-05-05 03:39:31 +0530123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected ApplicationService applicationService;
125
Jonathan Hartb9401902016-02-02 18:46:01 -0800126 @Property(name = "routeToNextHop", boolValue = false,
127 label = "Install a /32 route to each next hop")
128 private boolean routeToNextHop = false;
129
Jonathan Hartdf207092015-12-10 11:19:25 -0800130 // Device id of data-plane switch - should be learned from config
131 private DeviceId deviceId;
132
Saurav Das49cb5a12016-01-16 22:54:07 -0800133 private ConnectPoint controlPlaneConnectPoint;
134
Jonathan Hart883fd372016-02-10 14:36:15 -0800135 private List<String> interfaces;
136
Charles Chand55e84d2016-03-30 17:54:24 -0700137 private ApplicationId coreAppId;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800138 private ApplicationId routerAppId;
gaurava2e61a52016-05-05 03:39:31 +0530139 private ApplicationId vrouterAppId;
Jonathan Hartdf207092015-12-10 11:19:25 -0800140
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 Hart25bd6682016-06-03 10:45:47 -0700150 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 Hartdf207092015-12-10 11:19:25 -0800154
Charles Chand55e84d2016-03-30 17:54:24 -0700155 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 Hartdf207092015-12-10 11:19:25 -0800164 @Activate
Jonathan Hartb9401902016-02-02 18:46:01 -0800165 protected void activate(ComponentContext context) {
Jonathan Hartb9401902016-02-02 18:46:01 -0800166 componentConfigService.registerProperties(getClass());
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800167 modified(context);
168
Charles Chand55e84d2016-03-30 17:54:24 -0700169 coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME);
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800170 routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID);
gaurava2e61a52016-05-05 03:39:31 +0530171 vrouterAppId = coreService.registerApplication(APP_NAME);
Jonathan Hartb9401902016-02-02 18:46:01 -0800172
Charles Chand55e84d2016-03-30 17:54:24 -0700173 networkConfigRegistry.registerConfigFactory(mcastConfigFactory);
174
Jonathan Hart25bd6682016-06-03 10:45:47 -0700175 networkConfigService.addListener(configListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800176 deviceService.addListener(deviceListener);
gaurav164cf6d2016-03-25 21:43:04 +0530177 interfaceService.addListener(internalInterfaceList);
178
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800179 updateConfig();
Jonathan Hartdf207092015-12-10 11:19:25 -0800180
Charles Chan00d8b5f2016-12-04 17:17:39 -0800181 // FIXME: There can be an issue when this component is deactivated before vRouter.
182 // This will be addressed in CORD-710.
Jonathan Hartf8035d32016-06-16 16:23:26 -0700183 applicationService.registerDeactivateHook(vrouterAppId, () -> cleanUp());
gaurava2e61a52016-05-05 03:39:31 +0530184
Jonathan Hartdf207092015-12-10 11:19:25 -0800185 log.info("Started");
186 }
187
188 @Deactivate
189 protected void deactivate() {
Charles Chan00d8b5f2016-12-04 17:17:39 -0800190 // FIXME: This will also remove flows when an instance goes down.
191 // This is a temporary solution and should be addressed in CORD-710.
192 cleanUp();
193
Jonathan Hartdf207092015-12-10 11:19:25 -0800194 deviceService.removeListener(deviceListener);
gaurav164cf6d2016-03-25 21:43:04 +0530195 interfaceService.removeListener(internalInterfaceList);
Jonathan Hart25bd6682016-06-03 10:45:47 -0700196 networkConfigService.removeListener(configListener);
gaurav164cf6d2016-03-25 21:43:04 +0530197
Jonathan Hartb9401902016-02-02 18:46:01 -0800198 componentConfigService.unregisterProperties(getClass(), false);
199
Jonathan Hartdf207092015-12-10 11:19:25 -0800200 log.info("Stopped");
201 }
202
Jonathan Hartb9401902016-02-02 18:46:01 -0800203 @Modified
204 protected void modified(ComponentContext context) {
205 Dictionary<?, ?> properties = context.getProperties();
206 if (properties == null) {
207 return;
208 }
209
210 String strRouteToNextHop = Tools.get(properties, "routeToNextHop");
211 routeToNextHop = Boolean.parseBoolean(strRouteToNextHop);
212
213 log.info("routeToNextHop set to {}", routeToNextHop);
214 }
215
gaurava2e61a52016-05-05 03:39:31 +0530216 //remove filtering objectives and routes before deactivate.
217 private void cleanUp() {
gaurav25118892016-08-24 05:35:03 +0530218 //remove the route listener
219 routeService.removeListener(routeListener);
220
gaurava2e61a52016-05-05 03:39:31 +0530221 //clean up the routes.
222 for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) {
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700223 deleteRoute(new ResolvedRoute(routes.getKey(), null, null, null));
gaurava2e61a52016-05-05 03:39:31 +0530224 }
gaurav25118892016-08-24 05:35:03 +0530225
gaurava2e61a52016-05-05 03:39:31 +0530226 //clean up the filtering objective for interfaces.
227 Set<Interface> intfs = getInterfaces();
gaurav25118892016-08-24 05:35:03 +0530228 if (!intfs.isEmpty()) {
229 processIntfFilters(false, intfs);
230 }
gaurava2e61a52016-05-05 03:39:31 +0530231 }
232
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800233 private void updateConfig() {
234 RouterConfig routerConfig =
235 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800236
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800237 if (routerConfig == null) {
238 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800239 return;
240 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800241 controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint();
242 log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint);
Jonathan Hartdf207092015-12-10 11:19:25 -0800243
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800244 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800245 log.info("Router device ID is {}", deviceId);
246
Jonathan Hart883fd372016-02-10 14:36:15 -0800247 interfaces = routerConfig.getInterfaces();
248 log.info("Using interfaces: {}", interfaces.isEmpty() ? "all" : interfaces);
249
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700250 routeService.addListener(routeListener);
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800251 updateDevice();
252 }
253
gaurav25118892016-08-24 05:35:03 +0530254 //remove the filtering objective for interfaces which are no longer part of vRouter config.
255 private void removeFilteringObjectives(NetworkConfigEvent event) {
256 RouterConfig prevRouterConfig = (RouterConfig) event.prevConfig().get();
257 List<String> prevInterfaces = prevRouterConfig.getInterfaces();
258
259 Set<Interface> previntfs = filterInterfaces(prevInterfaces);
260 //if previous interface list is empty it means filtering objectives are
261 //installed for all the interfaces.
262 if (previntfs.isEmpty() && !interfaces.isEmpty()) {
263 Set<Interface> allIntfs = interfaceService.getInterfaces();
264 for (Interface allIntf : allIntfs) {
265 if (!interfaces.contains(allIntf.name())) {
266 processIntfFilter(false, allIntf);
267 }
268 }
269 return;
270 }
271
272 //remove the filtering objective for the interfaces which are not
273 //part of updated interfaces list.
274 for (Interface prevIntf : previntfs) {
275 if (!interfaces.contains(prevIntf.name())) {
276 processIntfFilter(false, prevIntf);
277 }
278 }
279 }
280
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800281 private void updateDevice() {
282 if (deviceId != null && deviceService.isAvailable(deviceId)) {
gaurava2e61a52016-05-05 03:39:31 +0530283 Set<Interface> intfs = getInterfaces();
Jonathan Hart883fd372016-02-10 14:36:15 -0800284 processIntfFilters(true, intfs);
Jonathan Hartdf207092015-12-10 11:19:25 -0800285 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800286 }
287
gaurava2e61a52016-05-05 03:39:31 +0530288 private Set<Interface> getInterfaces() {
289 Set<Interface> intfs;
Charles Chan14ed0b62016-12-03 22:40:36 -0800290 if (interfaces == null || interfaces.isEmpty()) {
gaurava2e61a52016-05-05 03:39:31 +0530291 intfs = interfaceService.getInterfaces();
292 } else {
293 // TODO need to fix by making interface names globally unique
gaurav25118892016-08-24 05:35:03 +0530294 intfs = filterInterfaces(interfaces);
gaurava2e61a52016-05-05 03:39:31 +0530295 }
296 return intfs;
297 }
298
gaurav25118892016-08-24 05:35:03 +0530299 private Set<Interface> filterInterfaces(List<String> interfaces) {
Charles Chan00d8b5f2016-12-04 17:17:39 -0800300 return interfaceService.getInterfaces().stream()
gaurav25118892016-08-24 05:35:03 +0530301 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
302 .filter(intf -> interfaces.contains(intf.name()))
303 .collect(Collectors.toSet());
gaurav25118892016-08-24 05:35:03 +0530304 }
305
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700306 private void updateRoute(ResolvedRoute route) {
307 addNextHop(route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800308
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700309 Integer nextId;
310 synchronized (this) {
311 nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800312 }
313
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700314 flowObjectiveService.forward(deviceId,
315 generateRibForwardingObj(route.prefix(), nextId).add());
316 log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId);
Jonathan Hartdf207092015-12-10 11:19:25 -0800317 }
318
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700319 private synchronized void deleteRoute(ResolvedRoute route) {
320 //Integer nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800321
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700322 /* Group group = deleteNextHop(route.prefix());
323 if (group == null) {
324 log.warn("Group not found when deleting {}", route);
325 return;
326 }*/
Jonathan Hartdf207092015-12-10 11:19:25 -0800327
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700328 flowObjectiveService.forward(deviceId,
329 generateRibForwardingObj(route.prefix(), null).remove());
Jonathan Hartdf207092015-12-10 11:19:25 -0800330 }
331
332 private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
333 Integer nextId) {
334 TrafficSelector selector = DefaultTrafficSelector.builder()
335 .matchEthType(Ethernet.TYPE_IPV4)
336 .matchIPDst(prefix)
337 .build();
338
339 int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
340
341 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
Saurav Das49cb5a12016-01-16 22:54:07 -0800342 .fromApp(routerAppId)
Jonathan Hartdf207092015-12-10 11:19:25 -0800343 .makePermanent()
344 .withSelector(selector)
345 .withPriority(priority)
346 .withFlag(ForwardingObjective.Flag.SPECIFIC);
347
348 if (nextId == null) {
349 // Route withdraws are not specified with next hops. Generating
350 // dummy treatment as there is no equivalent nextId info.
351 fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
352 } else {
353 fwdBuilder.nextStep(nextId);
354 }
355 return fwdBuilder;
356 }
357
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700358 private synchronized void addNextHop(ResolvedRoute route) {
359 prefixToNextHop.put(route.prefix(), route.nextHop());
360 if (nextHopsCount.count(route.nextHop()) == 0) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800361 // There was no next hop in the multiset
362
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700363 Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800364 if (egressIntf == null) {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700365 log.warn("no egress interface found for {}", route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800366 return;
367 }
368
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700369 NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800370
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700371 NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey);
Jonathan Hartdf207092015-12-10 11:19:25 -0800372
Jonathan Hartca47cd72015-12-13 12:31:09 -0800373 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800374 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800375 .setEthDst(nextHop.mac());
376
Saurav Das49cb5a12016-01-16 22:54:07 -0800377 TrafficSelector.Builder metabuilder = null;
Jonathan Hartca47cd72015-12-13 12:31:09 -0800378 if (!egressIntf.vlan().equals(VlanId.NONE)) {
379 treatment.pushVlan()
380 .setVlanId(egressIntf.vlan())
381 .setVlanPcp((byte) 0);
Saurav Das49cb5a12016-01-16 22:54:07 -0800382 } else {
383 // untagged outgoing port may require internal vlan in some pipelines
384 metabuilder = DefaultTrafficSelector.builder();
385 metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN));
Jonathan Hartca47cd72015-12-13 12:31:09 -0800386 }
387
388 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800389
390 int nextId = flowObjectiveService.allocateNextId();
Saurav Das49cb5a12016-01-16 22:54:07 -0800391 NextObjective.Builder nextBuilder = DefaultNextObjective.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800392 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800393 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800394 .withType(NextObjective.Type.SIMPLE)
Saurav Das49cb5a12016-01-16 22:54:07 -0800395 .fromApp(routerAppId);
396 if (metabuilder != null) {
397 nextBuilder.withMeta(metabuilder.build());
398 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800399
Saurav Das49cb5a12016-01-16 22:54:07 -0800400 NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks
Jonathan Hartdf207092015-12-10 11:19:25 -0800401 flowObjectiveService.next(deviceId, nextObjective);
402
403 nextHops.put(nextHop.ip(), nextId);
404
Jonathan Hartb9401902016-02-02 18:46:01 -0800405 if (routeToNextHop) {
406 // Install route to next hop
407 ForwardingObjective fob =
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700408 generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add();
Jonathan Hartb9401902016-02-02 18:46:01 -0800409 flowObjectiveService.forward(deviceId, fob);
410 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800411 }
412
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700413 nextHopsCount.add(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800414 }
415
416 /*private synchronized Group deleteNextHop(IpPrefix prefix) {
417 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
418 NextHop nextHop = nextHops.get(nextHopIp);
419 if (nextHop == null) {
420 log.warn("No next hop found when removing prefix {}", prefix);
421 return null;
422 }
423
424 Group group = groupService.getGroup(deviceId,
425 new DefaultGroupKey(appKryo.
426 serialize(nextHop.group())));
427
428 // FIXME disabling group deletes for now until we verify the logic is OK
429 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
430 // There was one or less next hops, so there are now none
431
432 log.debug("removing group for next hop {}", nextHop);
433
434 nextHops.remove(nextHopIp);
435
436 groupService.removeGroup(deviceId,
437 new DefaultGroupKey(appKryo.build().serialize(nextHop.group())),
438 appId);
439 }
440
441 return group;
442 }*/
443
444 private void processIntfFilters(boolean install, Set<Interface> intfs) {
445 log.info("Processing {} router interfaces", intfs.size());
446 for (Interface intf : intfs) {
447 if (!intf.connectPoint().deviceId().equals(deviceId)) {
448 // Ignore interfaces if they are not on the router switch
449 continue;
450 }
451
gaurav164cf6d2016-03-25 21:43:04 +0530452 createFilteringObjective(install, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700453 createMcastFilteringObjective(install, intf);
gaurav164cf6d2016-03-25 21:43:04 +0530454 }
455 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800456
gaurav164cf6d2016-03-25 21:43:04 +0530457 //process filtering objective for interface add/remove.
458 private void processIntfFilter(boolean install, Interface intf) {
459
460 if (!intf.connectPoint().deviceId().equals(deviceId)) {
461 // Ignore interfaces if they are not on the router switch
462 return;
463 }
gaurav25118892016-08-24 05:35:03 +0530464 if (!interfaces.contains(intf.name()) && install) {
465 return;
466 }
gaurav164cf6d2016-03-25 21:43:04 +0530467
468 createFilteringObjective(install, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700469 createMcastFilteringObjective(install, intf);
gaurav164cf6d2016-03-25 21:43:04 +0530470 }
471
472 //create filtering objective for interface
473 private void createFilteringObjective(boolean install, Interface intf) {
Charles Chand55e84d2016-03-30 17:54:24 -0700474 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
475 VlanId.vlanId(ASSIGNED_VLAN) :
476 egressVlan();
gaurav164cf6d2016-03-25 21:43:04 +0530477
478 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
479 // first add filter for the interface
480 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
481 .addCondition(Criteria.matchEthDst(intf.mac()))
482 .addCondition(Criteria.matchVlanId(intf.vlan()));
483 fob.withPriority(PRIORITY_OFFSET);
484 if (intf.vlan() == VlanId.NONE) {
485 TrafficTreatment tt = DefaultTrafficTreatment.builder()
Charles Chand55e84d2016-03-30 17:54:24 -0700486 .pushVlan().setVlanId(assignedVlan).build();
gaurav164cf6d2016-03-25 21:43:04 +0530487 fob.withMeta(tt);
488 }
gaurav164cf6d2016-03-25 21:43:04 +0530489 fob.permit().fromApp(routerAppId);
490 sendFilteringObjective(install, fob, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700491
gaurav164cf6d2016-03-25 21:43:04 +0530492 if (controlPlaneConnectPoint != null) {
493 // then add the same mac/vlan filters for control-plane connect point
494 fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port()));
Saurav Das49cb5a12016-01-16 22:54:07 -0800495 sendFilteringObjective(install, fob, intf);
Jonathan Hartdf207092015-12-10 11:19:25 -0800496 }
497 }
498
Charles Chand55e84d2016-03-30 17:54:24 -0700499 //create filtering objective for multicast traffic
500 private void createMcastFilteringObjective(boolean install, Interface intf) {
501 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
502 VlanId.vlanId(ASSIGNED_VLAN) :
503 egressVlan();
504
505 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
506 // first add filter for the interface
507 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
508 .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST,
509 MacAddress.IPV4_MULTICAST_MASK))
510 .addCondition(Criteria.matchVlanId(ingressVlan()));
511 fob.withPriority(PRIORITY_OFFSET);
512 TrafficTreatment tt = DefaultTrafficTreatment.builder()
513 .pushVlan().setVlanId(assignedVlan).build();
514 fob.withMeta(tt);
515
516 fob.permit().fromApp(routerAppId);
517 sendFilteringObjective(install, fob, intf);
518 }
519
Saurav Das49cb5a12016-01-16 22:54:07 -0800520 private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob,
521 Interface intf) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800522
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700523 ObjectiveContext context = new DefaultObjectiveContext(
524 (objective) -> log.info("Installed filter for interface {}", intf),
525 (objective, error) ->
526 log.error("Failed to install filter for interface {}: {}", intf, error));
gaurav164cf6d2016-03-25 21:43:04 +0530527
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700528 FilteringObjective filter = install ? fob.add(context) : fob.remove(context);
529
530 flowObjectiveService.filter(deviceId, filter);
Saurav Das49cb5a12016-01-16 22:54:07 -0800531 }
532
Charles Chand55e84d2016-03-30 17:54:24 -0700533 private VlanId ingressVlan() {
534 McastConfig mcastConfig =
535 networkConfigService.getConfig(coreAppId, McastConfig.class);
536 return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE;
537 }
538
539 private VlanId egressVlan() {
540 McastConfig mcastConfig =
541 networkConfigService.getConfig(coreAppId, McastConfig.class);
542 return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE;
543 }
544
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700545 private class InternalRouteListener implements RouteListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800546 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700547 public void event(RouteEvent event) {
548 ResolvedRoute route = event.subject();
549 switch (event.type()) {
550 case ROUTE_ADDED:
551 case ROUTE_UPDATED:
552 updateRoute(route);
553 break;
554 case ROUTE_REMOVED:
555 deleteRoute(route);
556 break;
557 default:
558 break;
559 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800560 }
561 }
562
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800563 /**
564 * Listener for device events used to trigger driver setup when a device is
565 * (re)detected.
566 */
567 private class InternalDeviceListener implements DeviceListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800568 @Override
569 public void event(DeviceEvent event) {
570 switch (event.type()) {
571 case DEVICE_ADDED:
572 case DEVICE_AVAILABILITY_CHANGED:
573 if (deviceService.isAvailable(event.subject().id())) {
574 log.info("Device connected {}", event.subject().id());
575 if (event.subject().id().equals(deviceId)) {
Charles Chanf555a732016-02-15 15:37:15 -0800576 updateDevice();
Jonathan Hartdf207092015-12-10 11:19:25 -0800577 }
578 }
579 break;
580 // TODO other cases
581 case DEVICE_UPDATED:
582 case DEVICE_REMOVED:
583 case DEVICE_SUSPENDED:
584 case PORT_ADDED:
585 case PORT_UPDATED:
586 case PORT_REMOVED:
587 default:
588 break;
589 }
590 }
591 }
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800592
593 /**
594 * Listener for network config events.
595 */
596 private class InternalNetworkConfigListener implements NetworkConfigListener {
597 @Override
598 public void event(NetworkConfigEvent event) {
Jonathan Hart25bd6682016-06-03 10:45:47 -0700599 if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800600 switch (event.type()) {
601 case CONFIG_ADDED:
602 case CONFIG_UPDATED:
603 updateConfig();
gaurav25118892016-08-24 05:35:03 +0530604 if (event.prevConfig().isPresent()) {
605 removeFilteringObjectives(event);
606 }
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800607 break;
608 case CONFIG_REGISTERED:
gaurav25118892016-08-24 05:35:03 +0530609 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800610 case CONFIG_UNREGISTERED:
gaurav25118892016-08-24 05:35:03 +0530611 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800612 case CONFIG_REMOVED:
gaurav25118892016-08-24 05:35:03 +0530613 cleanUp();
614 break;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800615 default:
616 break;
617 }
618 }
619 }
620 }
gaurav164cf6d2016-03-25 21:43:04 +0530621
622 private class InternalInterfaceListener implements InterfaceListener {
gaurav164cf6d2016-03-25 21:43:04 +0530623 @Override
624 public void event(InterfaceEvent event) {
625 Interface intf = event.subject();
626 switch (event.type()) {
627 case INTERFACE_ADDED:
628 if (intf != null) {
629 processIntfFilter(true, intf);
630 }
631 break;
632 case INTERFACE_UPDATED:
633 break;
634 case INTERFACE_REMOVED:
635 if (intf != null) {
636 processIntfFilter(false, intf);
637 }
638 break;
639 default:
640 break;
641 }
642 }
643 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800644}