yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.NodeId; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.incubator.net.virtual.AbstractVnetService; |
| 25 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 26 | import org.onosproject.incubator.net.virtual.VirtualNetworkPacketStore; |
| 27 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 28 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProviderService; |
| 29 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProvider; |
| 30 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProviderService; |
| 31 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
| 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 38 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 39 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 40 | import org.onosproject.net.flowobjective.Objective; |
| 41 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 42 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 43 | import org.onosproject.net.packet.DefaultPacketRequest; |
| 44 | import org.onosproject.net.packet.OutboundPacket; |
| 45 | import org.onosproject.net.packet.PacketContext; |
| 46 | import org.onosproject.net.packet.PacketEvent; |
| 47 | import org.onosproject.net.packet.PacketPriority; |
| 48 | import org.onosproject.net.packet.PacketProcessor; |
| 49 | import org.onosproject.net.packet.PacketProcessorEntry; |
| 50 | import org.onosproject.net.packet.PacketRequest; |
| 51 | import org.onosproject.net.packet.PacketService; |
| 52 | import org.onosproject.net.packet.PacketStoreDelegate; |
| 53 | import org.onosproject.net.provider.ProviderId; |
| 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
| 57 | import java.util.List; |
| 58 | import java.util.Optional; |
| 59 | import java.util.Set; |
| 60 | |
| 61 | public class VirtualNetworkPacketManager extends AbstractVnetService |
| 62 | implements PacketService { |
| 63 | |
| 64 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 65 | |
| 66 | private final VirtualNetworkService manager; |
| 67 | |
| 68 | protected VirtualNetworkPacketStore store; |
| 69 | private final List<ProcessorEntry> processors = Lists.newCopyOnWriteArrayList(); |
| 70 | |
| 71 | private NodeId localNodeId; |
| 72 | |
| 73 | private DeviceService deviceService; |
| 74 | private FlowObjectiveService objectiveService; |
| 75 | |
| 76 | private VirtualProviderRegistryService providerRegistryService = null; |
| 77 | |
| 78 | private InternalPacketProviderService providerService = null; |
| 79 | |
| 80 | public VirtualNetworkPacketManager(VirtualNetworkService virtualNetworkManager, |
| 81 | NetworkId networkId) { |
| 82 | super(virtualNetworkManager, networkId); |
| 83 | this.manager = virtualNetworkManager; |
| 84 | |
| 85 | //Set node id as same as the node hosting virtual manager |
| 86 | ClusterService clusterService = serviceDirectory.get(ClusterService.class); |
| 87 | this.localNodeId = clusterService.getLocalNode().id(); |
| 88 | |
| 89 | this.store = serviceDirectory.get(VirtualNetworkPacketStore.class); |
| 90 | this.store.setDelegate(networkId(), new InternalStoreDelegate()); |
| 91 | |
| 92 | this.deviceService = manager.get(networkId(), DeviceService.class); |
| 93 | this.objectiveService = manager.get(networkId(), FlowObjectiveService.class); |
| 94 | |
| 95 | providerRegistryService = |
| 96 | serviceDirectory.get(VirtualProviderRegistryService.class); |
| 97 | providerService = new InternalPacketProviderService(); |
| 98 | providerRegistryService.registerProviderService(networkId(), providerService); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void addProcessor(PacketProcessor processor, int priority) { |
| 103 | ProcessorEntry entry = new ProcessorEntry(processor, priority); |
| 104 | |
| 105 | // Insert the new processor according to its priority. |
| 106 | int i = 0; |
| 107 | for (; i < processors.size(); i++) { |
| 108 | if (priority < processors.get(i).priority()) { |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | processors.add(i, entry); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void removeProcessor(PacketProcessor processor) { |
| 117 | // Remove the processor entry. |
| 118 | for (int i = 0; i < processors.size(); i++) { |
| 119 | if (processors.get(i).processor() == processor) { |
| 120 | processors.remove(i); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public List<PacketProcessorEntry> getProcessors() { |
| 128 | return ImmutableList.copyOf(processors); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void requestPackets(TrafficSelector selector, PacketPriority priority, ApplicationId appId) { |
| 133 | PacketRequest request = new DefaultPacketRequest(selector, priority, appId, |
| 134 | localNodeId, Optional.empty()); |
| 135 | store.requestPackets(networkId(), request); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void requestPackets(TrafficSelector selector, PacketPriority priority, |
| 140 | ApplicationId appId, Optional<DeviceId> deviceId) { |
| 141 | PacketRequest request = |
| 142 | new DefaultPacketRequest(selector, priority, appId, |
| 143 | localNodeId, deviceId); |
| 144 | |
| 145 | store.requestPackets(networkId(), request); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void cancelPackets(TrafficSelector selector, PacketPriority priority, ApplicationId appId) { |
| 150 | PacketRequest request = new DefaultPacketRequest(selector, priority, appId, |
| 151 | localNodeId, Optional.empty()); |
| 152 | store.cancelPackets(networkId(), request); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void cancelPackets(TrafficSelector selector, PacketPriority priority, |
| 157 | ApplicationId appId, Optional<DeviceId> deviceId) { |
| 158 | PacketRequest request = new DefaultPacketRequest(selector, priority, |
| 159 | appId, localNodeId, |
| 160 | deviceId); |
| 161 | store.cancelPackets(networkId(), request); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public List<PacketRequest> getRequests() { |
| 166 | return store.existingRequests(networkId()); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void emit(OutboundPacket packet) { |
| 171 | store.emit(networkId(), packet); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Personalized packet provider service issued to the supplied provider. |
| 176 | */ |
| 177 | private class InternalPacketProviderService |
| 178 | extends AbstractVirtualProviderService<VirtualPacketProvider> |
| 179 | implements VirtualPacketProviderService { |
| 180 | |
| 181 | protected InternalPacketProviderService() { |
| 182 | super(); |
| 183 | |
| 184 | Set<ProviderId> providerIds = |
| 185 | providerRegistryService.getProvidersByService(this); |
| 186 | ProviderId providerId = providerIds.stream().findFirst().get(); |
| 187 | VirtualPacketProvider provider = (VirtualPacketProvider) |
| 188 | providerRegistryService.getProvider(providerId); |
| 189 | setProvider(provider); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void processPacket(PacketContext context) { |
| 194 | // TODO filter packets sent to processors based on registrations |
| 195 | for (ProcessorEntry entry : processors) { |
| 196 | try { |
| 197 | long start = System.nanoTime(); |
| 198 | entry.processor().process(context); |
| 199 | entry.addNanos(System.nanoTime() - start); |
| 200 | } catch (Exception e) { |
| 201 | log.warn("Packet processor {} threw an exception", entry.processor(), e); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Entity for tracking stats for a packet processor. |
| 210 | */ |
| 211 | private class ProcessorEntry implements PacketProcessorEntry { |
| 212 | private final PacketProcessor processor; |
| 213 | private final int priority; |
| 214 | private long invocations = 0; |
| 215 | private long nanos = 0; |
| 216 | |
| 217 | public ProcessorEntry(PacketProcessor processor, int priority) { |
| 218 | this.processor = processor; |
| 219 | this.priority = priority; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public PacketProcessor processor() { |
| 224 | return processor; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public int priority() { |
| 229 | return priority; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public long invocations() { |
| 234 | return invocations; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public long totalNanos() { |
| 239 | return nanos; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public long averageNanos() { |
| 244 | return invocations > 0 ? nanos / invocations : 0; |
| 245 | } |
| 246 | |
| 247 | void addNanos(long nanos) { |
| 248 | this.nanos += nanos; |
| 249 | this.invocations++; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private void localEmit(NetworkId networkId, OutboundPacket packet) { |
| 254 | Device device = deviceService.getDevice(packet.sendThrough()); |
| 255 | if (device == null) { |
| 256 | return; |
| 257 | } |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 258 | VirtualPacketProvider packetProvider = providerService.provider(); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 259 | |
| 260 | if (packetProvider != null) { |
| 261 | packetProvider.emit(networkId, packet); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Internal callback from the packet store. |
| 267 | */ |
| 268 | protected class InternalStoreDelegate implements PacketStoreDelegate { |
| 269 | @Override |
| 270 | public void notify(PacketEvent event) { |
| 271 | localEmit(networkId(), event.subject()); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void requestPackets(PacketRequest request) { |
| 276 | DeviceId deviceid = request.deviceId().orElse(null); |
| 277 | |
| 278 | if (deviceid != null) { |
| 279 | pushRule(deviceService.getDevice(deviceid), request); |
| 280 | } else { |
| 281 | pushToAllDevices(request); |
| 282 | } |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 283 | |
| 284 | providerService.provider().startPacketHandling(networkId); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void cancelPackets(PacketRequest request) { |
| 289 | DeviceId deviceid = request.deviceId().orElse(null); |
| 290 | |
| 291 | if (deviceid != null) { |
| 292 | removeRule(deviceService.getDevice(deviceid), request); |
| 293 | } else { |
| 294 | removeFromAllDevices(request); |
| 295 | } |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 296 | |
| 297 | providerService.provider().stopPacketHandling(networkId); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Pushes packet intercept flow rules to the device. |
| 303 | * |
| 304 | * @param device the device to push the rules to |
| 305 | * @param request the packet request |
| 306 | */ |
| 307 | private void pushRule(Device device, PacketRequest request) { |
Claudine Chiu | 93ce3e8 | 2017-02-18 14:28:22 -0500 | [diff] [blame] | 308 | if (!device.type().equals(Device.Type.VIRTUAL)) { |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
| 312 | ForwardingObjective forwarding = createBuilder(request) |
| 313 | .add(new ObjectiveContext() { |
| 314 | @Override |
| 315 | public void onError(Objective objective, ObjectiveError error) { |
| 316 | log.warn("Failed to install packet request {} to {}: {}", |
| 317 | request, device.id(), error); |
| 318 | } |
| 319 | }); |
| 320 | |
| 321 | objectiveService.forward(device.id(), forwarding); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Removes packet intercept flow rules from the device. |
| 326 | * |
| 327 | * @param device the device to remove the rules deom |
| 328 | * @param request the packet request |
| 329 | */ |
| 330 | private void removeRule(Device device, PacketRequest request) { |
Claudine Chiu | 93ce3e8 | 2017-02-18 14:28:22 -0500 | [diff] [blame] | 331 | if (!device.type().equals(Device.Type.VIRTUAL)) { |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | ForwardingObjective forwarding = createBuilder(request) |
| 335 | .remove(new ObjectiveContext() { |
| 336 | @Override |
| 337 | public void onError(Objective objective, ObjectiveError error) { |
| 338 | log.warn("Failed to withdraw packet request {} from {}: {}", |
| 339 | request, device.id(), error); |
| 340 | } |
| 341 | }); |
| 342 | objectiveService.forward(device.id(), forwarding); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Pushes a packet request flow rule to all devices. |
| 347 | * |
| 348 | * @param request the packet request |
| 349 | */ |
| 350 | private void pushToAllDevices(PacketRequest request) { |
| 351 | log.debug("Pushing packet request {} to all devices", request); |
| 352 | for (Device device : deviceService.getDevices()) { |
| 353 | pushRule(device, request); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Removes packet request flow rule from all devices. |
| 359 | * |
| 360 | * @param request the packet request |
| 361 | */ |
| 362 | private void removeFromAllDevices(PacketRequest request) { |
| 363 | deviceService.getAvailableDevices().forEach(d -> removeRule(d, request)); |
| 364 | } |
| 365 | |
| 366 | private DefaultForwardingObjective.Builder createBuilder(PacketRequest request) { |
| 367 | return DefaultForwardingObjective.builder() |
| 368 | .withPriority(request.priority().priorityValue()) |
| 369 | .withSelector(request.selector()) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 370 | .fromApp(request.appId()) |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 371 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 372 | .withTreatment(DefaultTrafficTreatment.builder().punt().build()) |
| 373 | .makePermanent(); |
| 374 | } |
| 375 | } |