blob: 4abd4897ecca610f29c40f6a34f9bbe3f831092d [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;
Saurav Das49cb5a12016-01-16 22:54:07 -080037import org.onosproject.net.ConnectPoint;
Jonathan Hartdf207092015-12-10 11:19:25 -080038import org.onosproject.net.DeviceId;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080039import org.onosproject.net.config.NetworkConfigEvent;
40import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartdf207092015-12-10 11:19:25 -080041import org.onosproject.net.config.NetworkConfigService;
42import org.onosproject.net.device.DeviceEvent;
43import org.onosproject.net.device.DeviceListener;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.flow.DefaultTrafficSelector;
46import org.onosproject.net.flow.DefaultTrafficTreatment;
47import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.flow.criteria.Criteria;
50import org.onosproject.net.flowobjective.DefaultFilteringObjective;
51import org.onosproject.net.flowobjective.DefaultForwardingObjective;
52import org.onosproject.net.flowobjective.DefaultNextObjective;
53import org.onosproject.net.flowobjective.FilteringObjective;
54import org.onosproject.net.flowobjective.FlowObjectiveService;
55import org.onosproject.net.flowobjective.ForwardingObjective;
56import org.onosproject.net.flowobjective.NextObjective;
57import org.onosproject.net.flowobjective.Objective;
58import org.onosproject.net.flowobjective.ObjectiveContext;
59import org.onosproject.net.flowobjective.ObjectiveError;
60import org.onosproject.routing.FibEntry;
61import org.onosproject.routing.FibListener;
62import org.onosproject.routing.FibUpdate;
63import org.onosproject.routing.RoutingService;
Jonathan Hartb3fa42c2016-01-13 09:50:43 -080064import org.onosproject.routing.config.RouterConfig;
Jonathan Hartdf207092015-12-10 11:19:25 -080065import org.slf4j.Logger;
66import org.slf4j.LoggerFactory;
67
68import java.util.Collection;
69import java.util.HashMap;
Jonathan Hart883fd372016-02-10 14:36:15 -080070import java.util.List;
Jonathan Hartdf207092015-12-10 11:19:25 -080071import java.util.Map;
Jonathan Hartdf207092015-12-10 11:19:25 -080072import java.util.Set;
Jonathan Hart883fd372016-02-10 14:36:15 -080073import java.util.stream.Collectors;
Jonathan Hartdf207092015-12-10 11:19:25 -080074
75/**
76 * Programs routes to a single OpenFlow switch.
77 */
Jonathan Hartc22e8472015-11-17 18:25:45 -080078@Component(immediate = true, enabled = false)
79public class SingleSwitchFibInstaller {
Jonathan Hartdf207092015-12-10 11:19:25 -080080
81 private final Logger log = LoggerFactory.getLogger(getClass());
82
83 private static final int PRIORITY_OFFSET = 100;
84 private static final int PRIORITY_MULTIPLIER = 5;
85
Saurav Das49cb5a12016-01-16 22:54:07 -080086 public static final short ASSIGNED_VLAN = 4094;
87
Jonathan Hartdf207092015-12-10 11:19:25 -080088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected CoreService coreService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected RoutingService routingService;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected InterfaceService interfaceService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected NetworkConfigService networkConfigService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected FlowObjectiveService flowObjectiveService;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected DeviceService deviceService;
105
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800106 private InternalDeviceListener deviceListener;
Jonathan Hartdf207092015-12-10 11:19:25 -0800107
108 // Device id of data-plane switch - should be learned from config
109 private DeviceId deviceId;
110
Saurav Das49cb5a12016-01-16 22:54:07 -0800111 private ConnectPoint controlPlaneConnectPoint;
112
Jonathan Hart883fd372016-02-10 14:36:15 -0800113 private List<String> interfaces;
114
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800115 private ApplicationId routerAppId;
Jonathan Hartdf207092015-12-10 11:19:25 -0800116
117 // Reference count for how many times a next hop is used by a route
118 private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create();
119
120 // Mapping from prefix to its current next hop
121 private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap();
122
123 // Mapping from next hop IP to next hop object containing group info
124 private final Map<IpAddress, Integer> nextHops = Maps.newHashMap();
125
126 // Stores FIB updates that are waiting for groups to be set up
127 private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create();
128
129
130 @Activate
131 protected void activate() {
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800132 routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID);
Jonathan Hartdf207092015-12-10 11:19:25 -0800133
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800134 deviceListener = new InternalDeviceListener();
Jonathan Hartdf207092015-12-10 11:19:25 -0800135 deviceService.addListener(deviceListener);
136
137 routingService.addFibListener(new InternalFibListener());
138 routingService.start();
139
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800140 updateConfig();
Jonathan Hartdf207092015-12-10 11:19:25 -0800141
142 log.info("Started");
143 }
144
145 @Deactivate
146 protected void deactivate() {
147 routingService.stop();
148
149 deviceService.removeListener(deviceListener);
150
151 //processIntfFilters(false, configService.getInterfaces()); //TODO necessary?
152
153 log.info("Stopped");
154 }
155
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800156 private void updateConfig() {
157 RouterConfig routerConfig =
158 networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
Jonathan Hartdf207092015-12-10 11:19:25 -0800159
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800160 if (routerConfig == null) {
161 log.info("Router config not available");
Jonathan Hartdf207092015-12-10 11:19:25 -0800162 return;
163 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800164 controlPlaneConnectPoint = routerConfig.getControlPlaneConnectPoint();
165 log.info("Control Plane Connect Point: {}", controlPlaneConnectPoint);
Jonathan Hartdf207092015-12-10 11:19:25 -0800166
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800167 deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800168 log.info("Router device ID is {}", deviceId);
169
Jonathan Hart883fd372016-02-10 14:36:15 -0800170 interfaces = routerConfig.getInterfaces();
171 log.info("Using interfaces: {}", interfaces.isEmpty() ? "all" : interfaces);
172
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800173 updateDevice();
174 }
175
176 private void updateDevice() {
177 if (deviceId != null && deviceService.isAvailable(deviceId)) {
Jonathan Hart883fd372016-02-10 14:36:15 -0800178
179 Set<Interface> intfs;
180 if (interfaces.isEmpty()) {
181 intfs = interfaceService.getInterfaces();
182 } else {
183 // TODO need to fix by making interface names globally unique
184 intfs = interfaceService.getInterfaces().stream()
185 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
186 .filter(intf -> interfaces.contains(intf.name()))
187 .collect(Collectors.toSet());
188 }
189
190 processIntfFilters(true, intfs);
Jonathan Hartdf207092015-12-10 11:19:25 -0800191 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800192 }
193
194 private void updateFibEntry(Collection<FibUpdate> updates) {
195 Map<FibEntry, Integer> toInstall = new HashMap<>(updates.size());
196
197 for (FibUpdate update : updates) {
198 FibEntry entry = update.entry();
199
200 addNextHop(entry);
201
202 Integer nextId;
203 synchronized (pendingUpdates) {
204 nextId = nextHops.get(entry.nextHopIp());
205 }
206
207 toInstall.put(update.entry(), nextId);
208 }
209
210 installFlows(toInstall);
211 }
212
213 private void installFlows(Map<FibEntry, Integer> entriesToInstall) {
214
215 for (Map.Entry<FibEntry, Integer> entry : entriesToInstall.entrySet()) {
216 FibEntry fibEntry = entry.getKey();
217 Integer nextId = entry.getValue();
218
219 flowObjectiveService.forward(deviceId,
220 generateRibForwardingObj(fibEntry.prefix(), nextId).add());
221 log.trace("Sending forwarding objective {} -> nextId:{}", fibEntry, nextId);
222 }
223
224 }
225
226 private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) {
227
228 for (FibUpdate update : withdraws) {
229 FibEntry entry = update.entry();
230 //Integer nextId = nextHops.get(entry.nextHopIp());
231
232 /* Group group = deleteNextHop(entry.prefix());
233 if (group == null) {
234 log.warn("Group not found when deleting {}", entry);
235 return;
236 }*/
237
238 flowObjectiveService.forward(deviceId,
239 generateRibForwardingObj(entry.prefix(), null).remove());
240
241 }
242
243 }
244
245 private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
246 Integer nextId) {
247 TrafficSelector selector = DefaultTrafficSelector.builder()
248 .matchEthType(Ethernet.TYPE_IPV4)
249 .matchIPDst(prefix)
250 .build();
251
252 int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
253
254 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
Saurav Das49cb5a12016-01-16 22:54:07 -0800255 .fromApp(routerAppId)
Jonathan Hartdf207092015-12-10 11:19:25 -0800256 .makePermanent()
257 .withSelector(selector)
258 .withPriority(priority)
259 .withFlag(ForwardingObjective.Flag.SPECIFIC);
260
261 if (nextId == null) {
262 // Route withdraws are not specified with next hops. Generating
263 // dummy treatment as there is no equivalent nextId info.
264 fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
265 } else {
266 fwdBuilder.nextStep(nextId);
267 }
268 return fwdBuilder;
269 }
270
271 private synchronized void addNextHop(FibEntry entry) {
272 prefixToNextHop.put(entry.prefix(), entry.nextHopIp());
273 if (nextHopsCount.count(entry.nextHopIp()) == 0) {
274 // There was no next hop in the multiset
275
276 Interface egressIntf = interfaceService.getMatchingInterface(entry.nextHopIp());
277 if (egressIntf == null) {
278 log.warn("no egress interface found for {}", entry);
279 return;
280 }
281
282 NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp());
283
284 NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey);
285
Jonathan Hartca47cd72015-12-13 12:31:09 -0800286 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800287 .setEthSrc(egressIntf.mac())
Jonathan Hartca47cd72015-12-13 12:31:09 -0800288 .setEthDst(nextHop.mac());
289
Saurav Das49cb5a12016-01-16 22:54:07 -0800290 TrafficSelector.Builder metabuilder = null;
Jonathan Hartca47cd72015-12-13 12:31:09 -0800291 if (!egressIntf.vlan().equals(VlanId.NONE)) {
292 treatment.pushVlan()
293 .setVlanId(egressIntf.vlan())
294 .setVlanPcp((byte) 0);
Saurav Das49cb5a12016-01-16 22:54:07 -0800295 } else {
296 // untagged outgoing port may require internal vlan in some pipelines
297 metabuilder = DefaultTrafficSelector.builder();
298 metabuilder.matchVlanId(VlanId.vlanId(ASSIGNED_VLAN));
Jonathan Hartca47cd72015-12-13 12:31:09 -0800299 }
300
301 treatment.setOutput(egressIntf.connectPoint().port());
Jonathan Hartdf207092015-12-10 11:19:25 -0800302
303 int nextId = flowObjectiveService.allocateNextId();
Saurav Das49cb5a12016-01-16 22:54:07 -0800304 NextObjective.Builder nextBuilder = DefaultNextObjective.builder()
Jonathan Hartdf207092015-12-10 11:19:25 -0800305 .withId(nextId)
Jonathan Hartca47cd72015-12-13 12:31:09 -0800306 .addTreatment(treatment.build())
Jonathan Hartdf207092015-12-10 11:19:25 -0800307 .withType(NextObjective.Type.SIMPLE)
Saurav Das49cb5a12016-01-16 22:54:07 -0800308 .fromApp(routerAppId);
309 if (metabuilder != null) {
310 nextBuilder.withMeta(metabuilder.build());
311 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800312
Saurav Das49cb5a12016-01-16 22:54:07 -0800313 NextObjective nextObjective = nextBuilder.add(); // TODO add callbacks
Jonathan Hartdf207092015-12-10 11:19:25 -0800314 flowObjectiveService.next(deviceId, nextObjective);
315
316 nextHops.put(nextHop.ip(), nextId);
317
318 }
319
320 nextHopsCount.add(entry.nextHopIp());
321 }
322
323 /*private synchronized Group deleteNextHop(IpPrefix prefix) {
324 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
325 NextHop nextHop = nextHops.get(nextHopIp);
326 if (nextHop == null) {
327 log.warn("No next hop found when removing prefix {}", prefix);
328 return null;
329 }
330
331 Group group = groupService.getGroup(deviceId,
332 new DefaultGroupKey(appKryo.
333 serialize(nextHop.group())));
334
335 // FIXME disabling group deletes for now until we verify the logic is OK
336 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
337 // There was one or less next hops, so there are now none
338
339 log.debug("removing group for next hop {}", nextHop);
340
341 nextHops.remove(nextHopIp);
342
343 groupService.removeGroup(deviceId,
344 new DefaultGroupKey(appKryo.build().serialize(nextHop.group())),
345 appId);
346 }
347
348 return group;
349 }*/
350
351 private void processIntfFilters(boolean install, Set<Interface> intfs) {
352 log.info("Processing {} router interfaces", intfs.size());
353 for (Interface intf : intfs) {
354 if (!intf.connectPoint().deviceId().equals(deviceId)) {
355 // Ignore interfaces if they are not on the router switch
356 continue;
357 }
358
359 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
Saurav Das49cb5a12016-01-16 22:54:07 -0800360 // first add filter for the interface
Jonathan Hartdf207092015-12-10 11:19:25 -0800361 fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
Saurav Das49cb5a12016-01-16 22:54:07 -0800362 .addCondition(Criteria.matchEthDst(intf.mac()))
363 .addCondition(Criteria.matchVlanId(intf.vlan()));
Jonathan Hart6344f572015-12-15 08:26:25 -0800364 fob.withPriority(PRIORITY_OFFSET);
Saurav Das49cb5a12016-01-16 22:54:07 -0800365 if (intf.vlan() == VlanId.NONE) {
366 TrafficTreatment tt = DefaultTrafficTreatment.builder()
367 .pushVlan().setVlanId(VlanId.vlanId(ASSIGNED_VLAN)).build();
368 fob.withMeta(tt);
369 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800370
Saurav Das49cb5a12016-01-16 22:54:07 -0800371 fob.permit().fromApp(routerAppId);
372 sendFilteringObjective(install, fob, intf);
373 if (controlPlaneConnectPoint != null) {
374 // then add the same mac/vlan filters for control-plane connect point
375 fob.withKey(Criteria.matchInPort(controlPlaneConnectPoint.port()));
376 sendFilteringObjective(install, fob, intf);
377 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800378 }
379 }
380
Saurav Das49cb5a12016-01-16 22:54:07 -0800381 private void sendFilteringObjective(boolean install, FilteringObjective.Builder fob,
382 Interface intf) {
383 flowObjectiveService.filter(
384 deviceId,
385 fob.add(new ObjectiveContext() {
386 @Override
387 public void onSuccess(Objective objective) {
388 log.info("Successfully installed interface based "
389 + "filtering objectives for intf {}", intf);
390 }
391
392 @Override
393 public void onError(Objective objective,
394 ObjectiveError error) {
395 log.error("Failed to install interface filters for intf {}: {}",
396 intf, error);
397 // TODO something more than just logging
398 }
399 }));
400 }
401
Jonathan Hartdf207092015-12-10 11:19:25 -0800402 private class InternalFibListener implements FibListener {
403
404 @Override
405 public void update(Collection<FibUpdate> updates,
406 Collection<FibUpdate> withdraws) {
Jonathan Hartc22e8472015-11-17 18:25:45 -0800407 SingleSwitchFibInstaller.this.deleteFibEntry(withdraws);
408 SingleSwitchFibInstaller.this.updateFibEntry(updates);
Jonathan Hartdf207092015-12-10 11:19:25 -0800409 }
410 }
411
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800412 /**
413 * Listener for device events used to trigger driver setup when a device is
414 * (re)detected.
415 */
416 private class InternalDeviceListener implements DeviceListener {
Jonathan Hartdf207092015-12-10 11:19:25 -0800417 @Override
418 public void event(DeviceEvent event) {
419 switch (event.type()) {
420 case DEVICE_ADDED:
421 case DEVICE_AVAILABILITY_CHANGED:
422 if (deviceService.isAvailable(event.subject().id())) {
423 log.info("Device connected {}", event.subject().id());
424 if (event.subject().id().equals(deviceId)) {
Charles Chanf555a732016-02-15 15:37:15 -0800425 updateDevice();
Jonathan Hartdf207092015-12-10 11:19:25 -0800426 }
427 }
428 break;
429 // TODO other cases
430 case DEVICE_UPDATED:
431 case DEVICE_REMOVED:
432 case DEVICE_SUSPENDED:
433 case PORT_ADDED:
434 case PORT_UPDATED:
435 case PORT_REMOVED:
436 default:
437 break;
438 }
439 }
440 }
Jonathan Hartb3fa42c2016-01-13 09:50:43 -0800441
442 /**
443 * Listener for network config events.
444 */
445 private class InternalNetworkConfigListener implements NetworkConfigListener {
446 @Override
447 public void event(NetworkConfigEvent event) {
448 if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
449 switch (event.type()) {
450 case CONFIG_ADDED:
451 case CONFIG_UPDATED:
452 updateConfig();
453 break;
454 case CONFIG_REGISTERED:
455 case CONFIG_UNREGISTERED:
456 case CONFIG_REMOVED:
457 default:
458 break;
459 }
460 }
461 }
462 }
Jonathan Hartdf207092015-12-10 11:19:25 -0800463}