blob: 4b2479e3db00208ee1784e295439f0ad1c478ea7 [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 Hartb3fa42c2016-01-13 09:50:43 -0800130 private InternalDeviceListener deviceListener;
Jonathan Hartdf207092015-12-10 11:19:25 -0800131
132 // Device id of data-plane switch - should be learned from config
133 private DeviceId deviceId;
134
Saurav Das49cb5a12016-01-16 22:54:07 -0800135 private ConnectPoint controlPlaneConnectPoint;
136
Jonathan Hart883fd372016-02-10 14:36:15 -0800137 private List<String> interfaces;
138
Charles Chand55e84d2016-03-30 17:54:24 -0700139 private ApplicationId coreAppId;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800140 private ApplicationId routerAppId;
gaurava2e61a52016-05-05 03:39:31 +0530141 private ApplicationId vrouterAppId;
Jonathan Hartdf207092015-12-10 11:19:25 -0800142
143 // Reference count for how many times a next hop is used by a route
144 private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create();
145
146 // Mapping from prefix to its current next hop
147 private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap();
148
149 // Mapping from next hop IP to next hop object containing group info
150 private final Map<IpAddress, Integer> nextHops = Maps.newHashMap();
151
gaurav164cf6d2016-03-25 21:43:04 +0530152 //interface object for event
153 private InternalInterfaceListener internalInterfaceList = new InternalInterfaceListener();
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700154 private InternalRouteListener routeListener = new InternalRouteListener();
Jonathan Hartdf207092015-12-10 11:19:25 -0800155
Charles Chand55e84d2016-03-30 17:54:24 -0700156 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
157 new ConfigFactory<ApplicationId, McastConfig>(SubjectFactories.APP_SUBJECT_FACTORY,
158 McastConfig.class, "multicast") {
159 @Override
160 public McastConfig createConfig() {
161 return new McastConfig();
162 }
163 };
164
Jonathan Hartdf207092015-12-10 11:19:25 -0800165 @Activate
Jonathan Hartb9401902016-02-02 18:46:01 -0800166 protected void activate(ComponentContext context) {
Jonathan Hartb9401902016-02-02 18:46:01 -0800167 componentConfigService.registerProperties(getClass());
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800168 modified(context);
169
Charles Chand55e84d2016-03-30 17:54:24 -0700170 coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME);
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800171 routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID);
gaurava2e61a52016-05-05 03:39:31 +0530172 vrouterAppId = coreService.registerApplication(APP_NAME);
Jonathan Hartb9401902016-02-02 18:46:01 -0800173
Charles Chand55e84d2016-03-30 17:54:24 -0700174 networkConfigRegistry.registerConfigFactory(mcastConfigFactory);
175
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800176 deviceListener = new InternalDeviceListener();
Jonathan Hartdf207092015-12-10 11:19:25 -0800177 deviceService.addListener(deviceListener);
178
gaurav164cf6d2016-03-25 21:43:04 +0530179 interfaceService.addListener(internalInterfaceList);
180
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800181 updateConfig();
Jonathan Hartdf207092015-12-10 11:19:25 -0800182
gaurava2e61a52016-05-05 03:39:31 +0530183 applicationService.registerDeactivateHook(vrouterAppId, () -> {
184 this.cleanUp();
185 });
186
Jonathan Hartdf207092015-12-10 11:19:25 -0800187 log.info("Started");
188 }
189
190 @Deactivate
191 protected void deactivate() {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700192 routeService.removeListener(routeListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800193 deviceService.removeListener(deviceListener);
gaurav164cf6d2016-03-25 21:43:04 +0530194 interfaceService.removeListener(internalInterfaceList);
195
Jonathan Hartdf207092015-12-10 11:19:25 -0800196 //processIntfFilters(false, configService.getInterfaces()); //TODO necessary?
197
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() {
218 //clean up the routes.
219 for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) {
220 deleteRoute(new ResolvedRoute(routes.getKey(), null, null));
221 }
222 //clean up the filtering objective for interfaces.
223 Set<Interface> intfs = getInterfaces();
224 processIntfFilters(false, intfs);
225 }
226
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800227 private void updateConfig() {
228 RouterConfig routerConfig =
229 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800230
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800231 if (routerConfig == null) {
232 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800233 return;
234 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800235 controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint();
236 log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint);
Jonathan Hartdf207092015-12-10 11:19:25 -0800237
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800238 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800239 log.info("Router device ID is {}", deviceId);
240
Jonathan Hart883fd372016-02-10 14:36:15 -0800241 interfaces = routerConfig.getInterfaces();
242 log.info("Using interfaces: {}", interfaces.isEmpty() ? "all" : interfaces);
243
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700244 routeService.addListener(routeListener);
245
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800246 updateDevice();
247 }
248
249 private void updateDevice() {
250 if (deviceId != null && deviceService.isAvailable(deviceId)) {
gaurava2e61a52016-05-05 03:39:31 +0530251 Set<Interface> intfs = getInterfaces();
Jonathan Hart883fd372016-02-10 14:36:15 -0800252 processIntfFilters(true, intfs);
Jonathan Hartdf207092015-12-10 11:19:25 -0800253 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800254 }
255
gaurava2e61a52016-05-05 03:39:31 +0530256 private Set<Interface> getInterfaces() {
257 Set<Interface> intfs;
258 if (interfaces.isEmpty()) {
259 intfs = interfaceService.getInterfaces();
260 } else {
261 // TODO need to fix by making interface names globally unique
262 intfs = interfaceService.getInterfaces().stream()
263 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
264 .filter(intf -> interfaces.contains(intf.name()))
265 .collect(Collectors.toSet());
266 }
267 return intfs;
268 }
269
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700270 private void updateRoute(ResolvedRoute route) {
271 addNextHop(route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800272
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700273 Integer nextId;
274 synchronized (this) {
275 nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800276 }
277
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700278 flowObjectiveService.forward(deviceId,
279 generateRibForwardingObj(route.prefix(), nextId).add());
280 log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId);
Jonathan Hartdf207092015-12-10 11:19:25 -0800281 }
282
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700283 private synchronized void deleteRoute(ResolvedRoute route) {
284 //Integer nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800285
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700286 /* Group group = deleteNextHop(route.prefix());
287 if (group == null) {
288 log.warn("Group not found when deleting {}", route);
289 return;
290 }*/
Jonathan Hartdf207092015-12-10 11:19:25 -0800291
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700292 flowObjectiveService.forward(deviceId,
293 generateRibForwardingObj(route.prefix(), null).remove());
Jonathan Hartdf207092015-12-10 11:19:25 -0800294 }
295
296 private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
297 Integer nextId) {
298 TrafficSelector selector = DefaultTrafficSelector.builder()
299 .matchEthType(Ethernet.TYPE_IPV4)
300 .matchIPDst(prefix)
301 .build();
302
303 int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
304
305 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
Saurav Das49cb5a12016-01-16 22:54:07 -0800306 .fromApp(routerAppId)
Jonathan Hartdf207092015-12-10 11:19:25 -0800307 .makePermanent()
308 .withSelector(selector)
309 .withPriority(priority)
310 .withFlag(ForwardingObjective.Flag.SPECIFIC);
311
312 if (nextId == null) {
313 // Route withdraws are not specified with next hops. Generating
314 // dummy treatment as there is no equivalent nextId info.
315 fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
316 } else {
317 fwdBuilder.nextStep(nextId);
318 }
319 return fwdBuilder;
320 }
321
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700322 private synchronized void addNextHop(ResolvedRoute route) {
323 prefixToNextHop.put(route.prefix(), route.nextHop());
324 if (nextHopsCount.count(route.nextHop()) == 0) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800325 // There was no next hop in the multiset
326
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700327 Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800328 if (egressIntf == null) {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700329 log.warn("no egress interface found for {}", route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800330 return;
331 }
332
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700333 NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800334
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700335 NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey);
Jonathan Hartdf207092015-12-10 11:19:25 -0800336
Jonathan Hartca47cd72015-12-13 12:31:09 -0800337 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800338 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800339 .setEthDst(nextHop.mac());
340
Saurav Das49cb5a12016-01-16 22:54:07 -0800341 TrafficSelector.Builder metabuilder = null;
Jonathan Hartca47cd72015-12-13 12:31:09 -0800342 if (!egressIntf.vlan().equals(VlanId.NONE)) {
343 treatment.pushVlan()
344 .setVlanId(egressIntf.vlan())
345 .setVlanPcp((byte) 0);
Saurav Das49cb5a12016-01-16 22:54:07 -0800346 } else {
347 // untagged outgoing port may require internal vlan in some pipelines
348 metabuilder = DefaultTrafficSelector.builder();
349 metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN));
Jonathan Hartca47cd72015-12-13 12:31:09 -0800350 }
351
352 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800353
354 int nextId = flowObjectiveService.allocateNextId();
Saurav Das49cb5a12016-01-16 22:54:07 -0800355 NextObjective.Builder nextBuilder = DefaultNextObjective.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800356 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800357 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800358 .withType(NextObjective.Type.SIMPLE)
Saurav Das49cb5a12016-01-16 22:54:07 -0800359 .fromApp(routerAppId);
360 if (metabuilder != null) {
361 nextBuilder.withMeta(metabuilder.build());
362 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800363
Saurav Das49cb5a12016-01-16 22:54:07 -0800364 NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks
Jonathan Hartdf207092015-12-10 11:19:25 -0800365 flowObjectiveService.next(deviceId, nextObjective);
366
367 nextHops.put(nextHop.ip(), nextId);
368
Jonathan Hartb9401902016-02-02 18:46:01 -0800369 if (routeToNextHop) {
370 // Install route to next hop
371 ForwardingObjective fob =
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700372 generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add();
Jonathan Hartb9401902016-02-02 18:46:01 -0800373 flowObjectiveService.forward(deviceId, fob);
374 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800375 }
376
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700377 nextHopsCount.add(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800378 }
379
380 /*private synchronized Group deleteNextHop(IpPrefix prefix) {
381 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
382 NextHop nextHop = nextHops.get(nextHopIp);
383 if (nextHop == null) {
384 log.warn("No next hop found when removing prefix {}", prefix);
385 return null;
386 }
387
388 Group group = groupService.getGroup(deviceId,
389 new DefaultGroupKey(appKryo.
390 serialize(nextHop.group())));
391
392 // FIXME disabling group deletes for now until we verify the logic is OK
393 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
394 // There was one or less next hops, so there are now none
395
396 log.debug("removing group for next hop {}", nextHop);
397
398 nextHops.remove(nextHopIp);
399
400 groupService.removeGroup(deviceId,
401 new DefaultGroupKey(appKryo.build().serialize(nextHop.group())),
402 appId);
403 }
404
405 return group;
406 }*/
407
408 private void processIntfFilters(boolean install, Set<Interface> intfs) {
409 log.info("Processing {} router interfaces", intfs.size());
410 for (Interface intf : intfs) {
411 if (!intf.connectPoint().deviceId().equals(deviceId)) {
412 // Ignore interfaces if they are not on the router switch
413 continue;
414 }
415
gaurav164cf6d2016-03-25 21:43:04 +0530416 createFilteringObjective(install, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700417 createMcastFilteringObjective(install, intf);
gaurav164cf6d2016-03-25 21:43:04 +0530418 }
419 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800420
gaurav164cf6d2016-03-25 21:43:04 +0530421 //process filtering objective for interface add/remove.
422 private void processIntfFilter(boolean install, Interface intf) {
423
424 if (!intf.connectPoint().deviceId().equals(deviceId)) {
425 // Ignore interfaces if they are not on the router switch
426 return;
427 }
428
429 createFilteringObjective(install, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700430 createMcastFilteringObjective(install, intf);
gaurav164cf6d2016-03-25 21:43:04 +0530431 }
432
433 //create filtering objective for interface
434 private void createFilteringObjective(boolean install, Interface intf) {
Charles Chand55e84d2016-03-30 17:54:24 -0700435 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
436 VlanId.vlanId(ASSIGNED_VLAN) :
437 egressVlan();
gaurav164cf6d2016-03-25 21:43:04 +0530438
439 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
440 // first add filter for the interface
441 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
442 .addCondition(Criteria.matchEthDst(intf.mac()))
443 .addCondition(Criteria.matchVlanId(intf.vlan()));
444 fob.withPriority(PRIORITY_OFFSET);
445 if (intf.vlan() == VlanId.NONE) {
446 TrafficTreatment tt = DefaultTrafficTreatment.builder()
Charles Chand55e84d2016-03-30 17:54:24 -0700447 .pushVlan().setVlanId(assignedVlan).build();
gaurav164cf6d2016-03-25 21:43:04 +0530448 fob.withMeta(tt);
449 }
gaurav164cf6d2016-03-25 21:43:04 +0530450 fob.permit().fromApp(routerAppId);
451 sendFilteringObjective(install, fob, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700452
gaurav164cf6d2016-03-25 21:43:04 +0530453 if (controlPlaneConnectPoint != null) {
454 // then add the same mac/vlan filters for control-plane connect point
455 fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port()));
Saurav Das49cb5a12016-01-16 22:54:07 -0800456 sendFilteringObjective(install, fob, intf);
Jonathan Hartdf207092015-12-10 11:19:25 -0800457 }
458 }
459
Charles Chand55e84d2016-03-30 17:54:24 -0700460 //create filtering objective for multicast traffic
461 private void createMcastFilteringObjective(boolean install, Interface intf) {
462 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
463 VlanId.vlanId(ASSIGNED_VLAN) :
464 egressVlan();
465
466 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
467 // first add filter for the interface
468 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
469 .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST,
470 MacAddress.IPV4_MULTICAST_MASK))
471 .addCondition(Criteria.matchVlanId(ingressVlan()));
472 fob.withPriority(PRIORITY_OFFSET);
473 TrafficTreatment tt = DefaultTrafficTreatment.builder()
474 .pushVlan().setVlanId(assignedVlan).build();
475 fob.withMeta(tt);
476
477 fob.permit().fromApp(routerAppId);
478 sendFilteringObjective(install, fob, intf);
479 }
480
Saurav Das49cb5a12016-01-16 22:54:07 -0800481 private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob,
482 Interface intf) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800483
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700484 ObjectiveContext context = new DefaultObjectiveContext(
485 (objective) -> log.info("Installed filter for interface {}", intf),
486 (objective, error) ->
487 log.error("Failed to install filter for interface {}: {}", intf, error));
gaurav164cf6d2016-03-25 21:43:04 +0530488
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700489 FilteringObjective filter = install ? fob.add(context) : fob.remove(context);
490
491 flowObjectiveService.filter(deviceId, filter);
Saurav Das49cb5a12016-01-16 22:54:07 -0800492 }
493
Charles Chand55e84d2016-03-30 17:54:24 -0700494 private VlanId ingressVlan() {
495 McastConfig mcastConfig =
496 networkConfigService.getConfig(coreAppId, McastConfig.class);
497 return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE;
498 }
499
500 private VlanId egressVlan() {
501 McastConfig mcastConfig =
502 networkConfigService.getConfig(coreAppId, McastConfig.class);
503 return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE;
504 }
505
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700506 private class InternalRouteListener implements RouteListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800507 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700508 public void event(RouteEvent event) {
509 ResolvedRoute route = event.subject();
510 switch (event.type()) {
511 case ROUTE_ADDED:
512 case ROUTE_UPDATED:
513 updateRoute(route);
514 break;
515 case ROUTE_REMOVED:
516 deleteRoute(route);
517 break;
518 default:
519 break;
520 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800521 }
522 }
523
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800524 /**
525 * Listener for device events used to trigger driver setup when a device is
526 * (re)detected.
527 */
528 private class InternalDeviceListener implements DeviceListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800529 @Override
530 public void event(DeviceEvent event) {
531 switch (event.type()) {
532 case DEVICE_ADDED:
533 case DEVICE_AVAILABILITY_CHANGED:
534 if (deviceService.isAvailable(event.subject().id())) {
535 log.info("Device connected {}", event.subject().id());
536 if (event.subject().id().equals(deviceId)) {
Charles Chanf555a732016-02-15 15:37:15 -0800537 updateDevice();
Jonathan Hartdf207092015-12-10 11:19:25 -0800538 }
539 }
540 break;
541 // TODO other cases
542 case DEVICE_UPDATED:
543 case DEVICE_REMOVED:
544 case DEVICE_SUSPENDED:
545 case PORT_ADDED:
546 case PORT_UPDATED:
547 case PORT_REMOVED:
548 default:
549 break;
550 }
551 }
552 }
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800553
554 /**
555 * Listener for network config events.
556 */
557 private class InternalNetworkConfigListener implements NetworkConfigListener {
558 @Override
559 public void event(NetworkConfigEvent event) {
560 if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
561 switch (event.type()) {
562 case CONFIG_ADDED:
563 case CONFIG_UPDATED:
564 updateConfig();
565 break;
566 case CONFIG_REGISTERED:
567 case CONFIG_UNREGISTERED:
568 case CONFIG_REMOVED:
569 default:
570 break;
571 }
572 }
573 }
574 }
gaurav164cf6d2016-03-25 21:43:04 +0530575
576 private class InternalInterfaceListener implements InterfaceListener {
gaurav164cf6d2016-03-25 21:43:04 +0530577 @Override
578 public void event(InterfaceEvent event) {
579 Interface intf = event.subject();
580 switch (event.type()) {
581 case INTERFACE_ADDED:
582 if (intf != null) {
583 processIntfFilter(true, intf);
584 }
585 break;
586 case INTERFACE_UPDATED:
587 break;
588 case INTERFACE_REMOVED:
589 if (intf != null) {
590 processIntfFilter(false, intf);
591 }
592 break;
593 default:
594 break;
595 }
596 }
597 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800598}