blob: a30c42c0e81f44fc4a266f0482dd1d9109f0920d [file] [log] [blame]
Jonathan Hartdf207092015-12-10 11:19:25 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 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;
20import com.google.common.collect.HashMultimap;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Multimap;
23import com.google.common.collect.Multiset;
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import 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;
Jonathan Hartca47cd72015-12-13 12:31:09 -080032import org.onlab.packet.VlanId;
Jonathan Hartdf207092015-12-10 11:19:25 -080033import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
35import org.onosproject.incubator.net.intf.Interface;
36import org.onosproject.incubator.net.intf.InterfaceService;
37import org.onosproject.net.DeviceId;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080038import org.onosproject.net.config.NetworkConfigEvent;
39import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartdf207092015-12-10 11:19:25 -080040import org.onosproject.net.config.NetworkConfigService;
41import org.onosproject.net.device.DeviceEvent;
42import org.onosproject.net.device.DeviceListener;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flowobjective.DefaultFilteringObjective;
50import org.onosproject.net.flowobjective.DefaultForwardingObjective;
51import org.onosproject.net.flowobjective.DefaultNextObjective;
52import org.onosproject.net.flowobjective.FilteringObjective;
53import org.onosproject.net.flowobjective.FlowObjectiveService;
54import org.onosproject.net.flowobjective.ForwardingObjective;
55import org.onosproject.net.flowobjective.NextObjective;
56import org.onosproject.net.flowobjective.Objective;
57import org.onosproject.net.flowobjective.ObjectiveContext;
58import org.onosproject.net.flowobjective.ObjectiveError;
59import org.onosproject.routing.FibEntry;
60import org.onosproject.routing.FibListener;
61import org.onosproject.routing.FibUpdate;
62import org.onosproject.routing.RoutingService;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080063import org.onosproject.routing.config.RouterConfig;
Jonathan Hartdf207092015-12-10 11:19:25 -080064import org.slf4j.Logger;
65import org.slf4j.LoggerFactory;
66
67import java.util.Collection;
68import java.util.HashMap;
69import java.util.Map;
Jonathan Hartdf207092015-12-10 11:19:25 -080070import java.util.Set;
71
72/**
73 * Programs routes to a single OpenFlow switch.
74 */
Jonathan Hartc22e8472015-11-17 18:25:45 -080075@Component(immediate = true, enabled = false)
76public class SingleSwitchFibInstaller {
Jonathan Hartdf207092015-12-10 11:19:25 -080077
78 private final Logger log = LoggerFactory.getLogger(getClass());
79
80 private static final int PRIORITY_OFFSET = 100;
81 private static final int PRIORITY_MULTIPLIER = 5;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected RoutingService routingService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected InterfaceService interfaceService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected NetworkConfigService networkConfigService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected FlowObjectiveService flowObjectiveService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected DeviceService deviceService;
100
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800101 private InternalDeviceListener deviceListener;
Jonathan Hartdf207092015-12-10 11:19:25 -0800102
103 // Device id of data-plane switch - should be learned from config
104 private DeviceId deviceId;
105
106 private ApplicationId appId;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800107 private ApplicationId routerAppId;
Jonathan Hartdf207092015-12-10 11:19:25 -0800108
109 // Reference count for how many times a next hop is used by a route
110 private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create();
111
112 // Mapping from prefix to its current next hop
113 private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap();
114
115 // Mapping from next hop IP to next hop object containing group info
116 private final Map<IpAddress, Integer> nextHops = Maps.newHashMap();
117
118 // Stores FIB updates that are waiting for groups to be set up
119 private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create();
120
121
122 @Activate
123 protected void activate() {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800124 // TODO why are there two of the same app ID?
125 routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID);
Jonathan Hartdf207092015-12-10 11:19:25 -0800126
Jonathan Hartc22e8472015-11-17 18:25:45 -0800127 appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
Jonathan Hartdf207092015-12-10 11:19:25 -0800128
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800129 deviceListener = new InternalDeviceListener();
Jonathan Hartdf207092015-12-10 11:19:25 -0800130 deviceService.addListener(deviceListener);
131
132 routingService.addFibListener(new InternalFibListener());
133 routingService.start();
134
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800135 updateConfig();
Jonathan Hartdf207092015-12-10 11:19:25 -0800136
137 log.info("Started");
138 }
139
140 @Deactivate
141 protected void deactivate() {
142 routingService.stop();
143
144 deviceService.removeListener(deviceListener);
145
146 //processIntfFilters(false, configService.getInterfaces()); //TODO necessary?
147
148 log.info("Stopped");
149 }
150
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800151 private void updateConfig() {
152 RouterConfig routerConfig =
153 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800154
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800155 if (routerConfig == null) {
156 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800157 return;
158 }
159
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800160 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
Jonathan Hartdf207092015-12-10 11:19:25 -0800161
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800162 log.info("Router device ID is {}", deviceId);
163
164 updateDevice();
165 }
166
167 private void updateDevice() {
168 if (deviceId != null && deviceService.isAvailable(deviceId)) {
169 processIntfFilters(true, interfaceService.getInterfaces());
Jonathan Hartdf207092015-12-10 11:19:25 -0800170 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800171 }
172
173 private void updateFibEntry(Collection<FibUpdate> updates) {
174 Map<FibEntry, Integer> toInstall = new HashMap<>(updates.size());
175
176 for (FibUpdate update : updates) {
177 FibEntry entry = update.entry();
178
179 addNextHop(entry);
180
181 Integer nextId;
182 synchronized (pendingUpdates) {
183 nextId = nextHops.get(entry.nextHopIp());
184 }
185
186 toInstall.put(update.entry(), nextId);
187 }
188
189 installFlows(toInstall);
190 }
191
192 private void installFlows(Map<FibEntry, Integer> entriesToInstall) {
193
194 for (Map.Entry<FibEntry, Integer> entry : entriesToInstall.entrySet()) {
195 FibEntry fibEntry = entry.getKey();
196 Integer nextId = entry.getValue();
197
198 flowObjectiveService.forward(deviceId,
199 generateRibForwardingObj(fibEntry.prefix(), nextId).add());
200 log.trace("Sending forwarding objective {} -> nextId:{}", fibEntry, nextId);
201 }
202
203 }
204
205 private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) {
206
207 for (FibUpdate update : withdraws) {
208 FibEntry entry = update.entry();
209 //Integer nextId = nextHops.get(entry.nextHopIp());
210
211 /* Group group = deleteNextHop(entry.prefix());
212 if (group == null) {
213 log.warn("Group not found when deleting {}", entry);
214 return;
215 }*/
216
217 flowObjectiveService.forward(deviceId,
218 generateRibForwardingObj(entry.prefix(), null).remove());
219
220 }
221
222 }
223
224 private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
225 Integer nextId) {
226 TrafficSelector selector = DefaultTrafficSelector.builder()
227 .matchEthType(Ethernet.TYPE_IPV4)
228 .matchIPDst(prefix)
229 .build();
230
231 int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
232
233 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
234 .fromApp(appId)
235 .makePermanent()
236 .withSelector(selector)
237 .withPriority(priority)
238 .withFlag(ForwardingObjective.Flag.SPECIFIC);
239
240 if (nextId == null) {
241 // Route withdraws are not specified with next hops. Generating
242 // dummy treatment as there is no equivalent nextId info.
243 fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
244 } else {
245 fwdBuilder.nextStep(nextId);
246 }
247 return fwdBuilder;
248 }
249
250 private synchronized void addNextHop(FibEntry entry) {
251 prefixToNextHop.put(entry.prefix(), entry.nextHopIp());
252 if (nextHopsCount.count(entry.nextHopIp()) == 0) {
253 // There was no next hop in the multiset
254
255 Interface egressIntf = interfaceService.getMatchingInterface(entry.nextHopIp());
256 if (egressIntf == null) {
257 log.warn("no egress interface found for {}", entry);
258 return;
259 }
260
261 NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp());
262
263 NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey);
264
Jonathan Hartca47cd72015-12-13 12:31:09 -0800265 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800266 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800267 .setEthDst(nextHop.mac());
268
269 if (!egressIntf.vlan().equals(VlanId.NONE)) {
270 treatment.pushVlan()
271 .setVlanId(egressIntf.vlan())
272 .setVlanPcp((byte) 0);
273 }
274
275 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800276
277 int nextId = flowObjectiveService.allocateNextId();
278
279 NextObjective nextObjective = DefaultNextObjective.builder()
280 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800281 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800282 .withType(NextObjective.Type.SIMPLE)
283 .fromApp(appId)
284 .add(); // TODO add callbacks
285
286 flowObjectiveService.next(deviceId, nextObjective);
287
288 nextHops.put(nextHop.ip(), nextId);
289
290 }
291
292 nextHopsCount.add(entry.nextHopIp());
293 }
294
295 /*private synchronized Group deleteNextHop(IpPrefix prefix) {
296 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
297 NextHop nextHop = nextHops.get(nextHopIp);
298 if (nextHop == null) {
299 log.warn("No next hop found when removing prefix {}", prefix);
300 return null;
301 }
302
303 Group group = groupService.getGroup(deviceId,
304 new DefaultGroupKey(appKryo.
305 serialize(nextHop.group())));
306
307 // FIXME disabling group deletes for now until we verify the logic is OK
308 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
309 // There was one or less next hops, so there are now none
310
311 log.debug("removing group for next hop {}", nextHop);
312
313 nextHops.remove(nextHopIp);
314
315 groupService.removeGroup(deviceId,
316 new DefaultGroupKey(appKryo.build().serialize(nextHop.group())),
317 appId);
318 }
319
320 return group;
321 }*/
322
323 private void processIntfFilters(boolean install, Set<Interface> intfs) {
324 log.info("Processing {} router interfaces", intfs.size());
325 for (Interface intf : intfs) {
326 if (!intf.connectPoint().deviceId().equals(deviceId)) {
327 // Ignore interfaces if they are not on the router switch
328 continue;
329 }
330
331 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
332 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
333 .addCondition(Criteria.matchEthDst(intf.mac()))
334 .addCondition(Criteria.matchVlanId(intf.vlan()));
Jonathan Hart6344f572015-12-15 08:26:25 -0800335
336 fob.withPriority(PRIORITY_OFFSET);
337
Jonathan Hartdf207092015-12-10 11:19:25 -0800338 fob.permit().fromApp(appId);
339 flowObjectiveService.filter(
340 deviceId,
341 fob.add(new ObjectiveContext() {
342 @Override
343 public void onSuccess(Objective objective) {
344 log.info("Successfully installed interface based "
345 + "filtering objectives for intf {}", intf);
346 }
347
348 @Override
349 public void onError(Objective objective,
350 ObjectiveError error) {
351 log.error("Failed to install interface filters for intf {}: {}",
352 intf, error);
353 // TODO something more than just logging
354 }
355 }));
356 }
357 }
358
359 private class InternalFibListener implements FibListener {
360
361 @Override
362 public void update(Collection<FibUpdate> updates,
363 Collection<FibUpdate> withdraws) {
Jonathan Hartc22e8472015-11-17 18:25:45 -0800364 SingleSwitchFibInstaller.this.deleteFibEntry(withdraws);
365 SingleSwitchFibInstaller.this.updateFibEntry(updates);
Jonathan Hartdf207092015-12-10 11:19:25 -0800366 }
367 }
368
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800369 /**
370 * Listener for device events used to trigger driver setup when a device is
371 * (re)detected.
372 */
373 private class InternalDeviceListener implements DeviceListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800374 @Override
375 public void event(DeviceEvent event) {
376 switch (event.type()) {
377 case DEVICE_ADDED:
378 case DEVICE_AVAILABILITY_CHANGED:
379 if (deviceService.isAvailable(event.subject().id())) {
380 log.info("Device connected {}", event.subject().id());
381 if (event.subject().id().equals(deviceId)) {
382 processIntfFilters(true, interfaceService.getInterfaces());
383 }
384 }
385 break;
386 // TODO other cases
387 case DEVICE_UPDATED:
388 case DEVICE_REMOVED:
389 case DEVICE_SUSPENDED:
390 case PORT_ADDED:
391 case PORT_UPDATED:
392 case PORT_REMOVED:
393 default:
394 break;
395 }
396 }
397 }
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800398
399 /**
400 * Listener for network config events.
401 */
402 private class InternalNetworkConfigListener implements NetworkConfigListener {
403 @Override
404 public void event(NetworkConfigEvent event) {
405 if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
406 switch (event.type()) {
407 case CONFIG_ADDED:
408 case CONFIG_UPDATED:
409 updateConfig();
410 break;
411 case CONFIG_REGISTERED:
412 case CONFIG_UNREGISTERED:
413 case CONFIG_REMOVED:
414 default:
415 break;
416 }
417 }
418 }
419 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800420}