blob: 00924cdc63980e7dd8c18769961d420afbd758b1 [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
Jonathan Hartf8035d32016-06-16 16:23:26 -0700181 applicationService.registerDeactivateHook(vrouterAppId, () -> cleanUp());
gaurava2e61a52016-05-05 03:39:31 +0530182
Jonathan Hartdf207092015-12-10 11:19:25 -0800183 log.info("Started");
184 }
185
186 @Deactivate
187 protected void deactivate() {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700188 routeService.removeListener(routeListener);
Jonathan Hartdf207092015-12-10 11:19:25 -0800189 deviceService.removeListener(deviceListener);
gaurav164cf6d2016-03-25 21:43:04 +0530190 interfaceService.removeListener(internalInterfaceList);
Jonathan Hart25bd6682016-06-03 10:45:47 -0700191 networkConfigService.removeListener(configListener);
gaurav164cf6d2016-03-25 21:43:04 +0530192
Jonathan Hartb9401902016-02-02 18:46:01 -0800193 componentConfigService.unregisterProperties(getClass(), false);
194
Jonathan Hartdf207092015-12-10 11:19:25 -0800195 log.info("Stopped");
196 }
197
Jonathan Hartb9401902016-02-02 18:46:01 -0800198 @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
gaurava2e61a52016-05-05 03:39:31 +0530211 //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 Hartb3fa42c2016-01-13 09:50:43 -0800222 private void updateConfig() {
223 RouterConfig routerConfig =
224 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800225
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800226 if (routerConfig == null) {
227 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800228 return;
229 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800230 controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint();
231 log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint);
Jonathan Hartdf207092015-12-10 11:19:25 -0800232
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800233 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800234 log.info("Router device ID is {}", deviceId);
235
Jonathan Hart883fd372016-02-10 14:36:15 -0800236 interfaces = routerConfig.getInterfaces();
237 log.info("Using interfaces: {}", interfaces.isEmpty() ? "all" : interfaces);
238
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700239 routeService.addListener(routeListener);
240
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800241 updateDevice();
242 }
243
244 private void updateDevice() {
245 if (deviceId != null && deviceService.isAvailable(deviceId)) {
gaurava2e61a52016-05-05 03:39:31 +0530246 Set<Interface> intfs = getInterfaces();
Jonathan Hart883fd372016-02-10 14:36:15 -0800247 processIntfFilters(true, intfs);
Jonathan Hartdf207092015-12-10 11:19:25 -0800248 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800249 }
250
gaurava2e61a52016-05-05 03:39:31 +0530251 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 Harta2eb9ff2016-04-13 21:27:06 -0700265 private void updateRoute(ResolvedRoute route) {
266 addNextHop(route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800267
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700268 Integer nextId;
269 synchronized (this) {
270 nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800271 }
272
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700273 flowObjectiveService.forward(deviceId,
274 generateRibForwardingObj(route.prefix(), nextId).add());
275 log.trace("Sending forwarding objective {} -> nextId:{}", route, nextId);
Jonathan Hartdf207092015-12-10 11:19:25 -0800276 }
277
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700278 private synchronized void deleteRoute(ResolvedRoute route) {
279 //Integer nextId = nextHops.get(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800280
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700281 /* Group group = deleteNextHop(route.prefix());
282 if (group == null) {
283 log.warn("Group not found when deleting {}", route);
284 return;
285 }*/
Jonathan Hartdf207092015-12-10 11:19:25 -0800286
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700287 flowObjectiveService.forward(deviceId,
288 generateRibForwardingObj(route.prefix(), null).remove());
Jonathan Hartdf207092015-12-10 11:19:25 -0800289 }
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 Das49cb5a12016-01-16 22:54:07 -0800301 .fromApp(routerAppId)
Jonathan Hartdf207092015-12-10 11:19:25 -0800302 .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 Harta2eb9ff2016-04-13 21:27:06 -0700317 private synchronized void addNextHop(ResolvedRoute route) {
318 prefixToNextHop.put(route.prefix(), route.nextHop());
319 if (nextHopsCount.count(route.nextHop()) == 0) {
Jonathan Hartdf207092015-12-10 11:19:25 -0800320 // There was no next hop in the multiset
321
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700322 Interface egressIntf = interfaceService.getMatchingInterface(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800323 if (egressIntf == null) {
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700324 log.warn("no egress interface found for {}", route);
Jonathan Hartdf207092015-12-10 11:19:25 -0800325 return;
326 }
327
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700328 NextHopGroupKey groupKey = new NextHopGroupKey(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800329
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700330 NextHop nextHop = new NextHop(route.nextHop(), route.nextHopMac(), groupKey);
Jonathan Hartdf207092015-12-10 11:19:25 -0800331
Jonathan Hartca47cd72015-12-13 12:31:09 -0800332 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800333 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800334 .setEthDst(nextHop.mac());
335
Saurav Das49cb5a12016-01-16 22:54:07 -0800336 TrafficSelector.Builder metabuilder = null;
Jonathan Hartca47cd72015-12-13 12:31:09 -0800337 if (!egressIntf.vlan().equals(VlanId.NONE)) {
338 treatment.pushVlan()
339 .setVlanId(egressIntf.vlan())
340 .setVlanPcp((byte) 0);
Saurav Das49cb5a12016-01-16 22:54:07 -0800341 } else {
342 // untagged outgoing port may require internal vlan in some pipelines
343 metabuilder = DefaultTrafficSelector.builder();
344 metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN));
Jonathan Hartca47cd72015-12-13 12:31:09 -0800345 }
346
347 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800348
349 int nextId = flowObjectiveService.allocateNextId();
Saurav Das49cb5a12016-01-16 22:54:07 -0800350 NextObjective.Builder nextBuilder = DefaultNextObjective.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800351 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800352 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800353 .withType(NextObjective.Type.SIMPLE)
Saurav Das49cb5a12016-01-16 22:54:07 -0800354 .fromApp(routerAppId);
355 if (metabuilder != null) {
356 nextBuilder.withMeta(metabuilder.build());
357 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800358
Saurav Das49cb5a12016-01-16 22:54:07 -0800359 NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks
Jonathan Hartdf207092015-12-10 11:19:25 -0800360 flowObjectiveService.next(deviceId, nextObjective);
361
362 nextHops.put(nextHop.ip(), nextId);
363
Jonathan Hartb9401902016-02-02 18:46:01 -0800364 if (routeToNextHop) {
365 // Install route to next hop
366 ForwardingObjective fob =
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700367 generateRibForwardingObj(IpPrefix.valueOf(route.nextHop(), 32), nextId).add();
Jonathan Hartb9401902016-02-02 18:46:01 -0800368 flowObjectiveService.forward(deviceId, fob);
369 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800370 }
371
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700372 nextHopsCount.add(route.nextHop());
Jonathan Hartdf207092015-12-10 11:19:25 -0800373 }
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
gaurav164cf6d2016-03-25 21:43:04 +0530411 createFilteringObjective(install, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700412 createMcastFilteringObjective(install, intf);
gaurav164cf6d2016-03-25 21:43:04 +0530413 }
414 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800415
gaurav164cf6d2016-03-25 21:43:04 +0530416 //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 Chand55e84d2016-03-30 17:54:24 -0700425 createMcastFilteringObjective(install, intf);
gaurav164cf6d2016-03-25 21:43:04 +0530426 }
427
428 //create filtering objective for interface
429 private void createFilteringObjective(boolean install, Interface intf) {
Charles Chand55e84d2016-03-30 17:54:24 -0700430 VlanId assignedVlan = (egressVlan().equals(VlanId.NONE)) ?
431 VlanId.vlanId(ASSIGNED_VLAN) :
432 egressVlan();
gaurav164cf6d2016-03-25 21:43:04 +0530433
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 Chand55e84d2016-03-30 17:54:24 -0700442 .pushVlan().setVlanId(assignedVlan).build();
gaurav164cf6d2016-03-25 21:43:04 +0530443 fob.withMeta(tt);
444 }
gaurav164cf6d2016-03-25 21:43:04 +0530445 fob.permit().fromApp(routerAppId);
446 sendFilteringObjective(install, fob, intf);
Charles Chand55e84d2016-03-30 17:54:24 -0700447
gaurav164cf6d2016-03-25 21:43:04 +0530448 if (controlPlaneConnectPoint != null) {
449 // then add the same mac/vlan filters for control-plane connect point
450 fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port()));
Saurav Das49cb5a12016-01-16 22:54:07 -0800451 sendFilteringObjective(install, fob, intf);
Jonathan Hartdf207092015-12-10 11:19:25 -0800452 }
453 }
454
Charles Chand55e84d2016-03-30 17:54:24 -0700455 //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 Das49cb5a12016-01-16 22:54:07 -0800476 private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob,
477 Interface intf) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800478
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700479 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));
gaurav164cf6d2016-03-25 21:43:04 +0530483
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700484 FilteringObjective filter = install ? fob.add(context) : fob.remove(context);
485
486 flowObjectiveService.filter(deviceId, filter);
Saurav Das49cb5a12016-01-16 22:54:07 -0800487 }
488
Charles Chand55e84d2016-03-30 17:54:24 -0700489 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 Harta2eb9ff2016-04-13 21:27:06 -0700501 private class InternalRouteListener implements RouteListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800502 @Override
Jonathan Harta2eb9ff2016-04-13 21:27:06 -0700503 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 Hartdf207092015-12-10 11:19:25 -0800516 }
517 }
518
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800519 /**
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 Hartdf207092015-12-10 11:19:25 -0800524 @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 Chanf555a732016-02-15 15:37:15 -0800532 updateDevice();
Jonathan Hartdf207092015-12-10 11:19:25 -0800533 }
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 Hartb3fa42c2016-01-13 09:50:43 -0800548
549 /**
550 * Listener for network config events.
551 */
552 private class InternalNetworkConfigListener implements NetworkConfigListener {
553 @Override
554 public void event(NetworkConfigEvent event) {
Jonathan Hart25bd6682016-06-03 10:45:47 -0700555 if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800556 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 }
gaurav164cf6d2016-03-25 21:43:04 +0530570
571 private class InternalInterfaceListener implements InterfaceListener {
gaurav164cf6d2016-03-25 21:43:04 +0530572 @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 Hartdf207092015-12-10 11:19:25 -0800593}