Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.packet.impl; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 25 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 29 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.packet.OutboundPacket; |
| 31 | import org.onosproject.net.packet.PacketEvent; |
| 32 | import org.onosproject.net.packet.PacketEvent.Type; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 33 | import org.onosproject.net.packet.PacketRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.packet.PacketStore; |
| 35 | import org.onosproject.net.packet.PacketStoreDelegate; |
| 36 | import org.onosproject.store.AbstractStore; |
| 37 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 39 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 40 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 41 | import org.onosproject.store.serializers.KryoNamespaces; |
| 42 | import org.onosproject.store.serializers.KryoSerializer; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 43 | import org.onosproject.store.service.ConsistentMap; |
| 44 | import org.onosproject.store.service.Serializer; |
| 45 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 46 | import org.onosproject.store.service.Versioned; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 49 | import java.util.HashSet; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 50 | import java.util.Set; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 51 | import java.util.concurrent.ExecutorService; |
| 52 | import java.util.concurrent.Executors; |
| 53 | |
| 54 | import static org.onlab.util.Tools.groupedThreads; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Distributed packet store implementation allowing packets to be sent to |
| 59 | * remote instances. |
| 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class DistributedPacketStore |
| 64 | extends AbstractStore<PacketEvent, PacketStoreDelegate> |
| 65 | implements PacketStore { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 69 | // TODO: make this configurable. |
| 70 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 71 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 73 | protected MastershipService mastershipService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 76 | protected ClusterService clusterService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 79 | protected ClusterCommunicationService communicationService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 80 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected StorageService storageService; |
| 83 | |
| 84 | private PacketRequestTracker tracker; |
| 85 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 86 | private static final MessageSubject PACKET_OUT_SUBJECT = |
| 87 | new MessageSubject("packet-out"); |
| 88 | |
| 89 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 90 | @Override |
| 91 | protected void setupKryoPool() { |
| 92 | serializerPool = KryoNamespace.newBuilder() |
| 93 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 94 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 95 | .build(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 99 | private ExecutorService messageHandlingExecutor; |
| 100 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 101 | @Activate |
| 102 | public void activate() { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 103 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 104 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 105 | groupedThreads("onos/store/packet", "message-handlers")); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 106 | |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 107 | communicationService.addSubscriber(PACKET_OUT_SUBJECT, |
| 108 | new InternalClusterMessageHandler(), |
| 109 | messageHandlingExecutor); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 110 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 111 | tracker = new PacketRequestTracker(); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 112 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 113 | log.info("Started"); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 118 | communicationService.removeSubscriber(PACKET_OUT_SUBJECT); |
| 119 | messageHandlingExecutor.shutdown(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 120 | log.info("Stopped"); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void emit(OutboundPacket packet) { |
| 125 | NodeId myId = clusterService.getLocalNode().id(); |
| 126 | NodeId master = mastershipService.getMasterFor(packet.sendThrough()); |
| 127 | |
Jonathan Hart | 7466d61 | 2014-11-24 17:09:53 -0800 | [diff] [blame] | 128 | if (master == null) { |
| 129 | return; |
| 130 | } |
| 131 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 132 | if (myId.equals(master)) { |
| 133 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 134 | return; |
| 135 | } |
| 136 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 137 | // TODO check unicast return value |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 138 | communicationService.unicast(packet, PACKET_OUT_SUBJECT, SERIALIZER::encode, master); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 139 | // error log: log.warn("Failed to send packet-out to {}", master); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 140 | } |
| 141 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public boolean requestPackets(PacketRequest request) { |
| 144 | return tracker.add(request); |
| 145 | } |
| 146 | |
| 147 | @Override |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 148 | public boolean cancelPackets(PacketRequest request) { |
| 149 | return tracker.remove(request); |
| 150 | } |
| 151 | |
| 152 | @Override |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 153 | public Set<PacketRequest> existingRequests() { |
| 154 | return tracker.requests(); |
| 155 | } |
| 156 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Handles incoming cluster messages. |
| 159 | */ |
| 160 | private class InternalClusterMessageHandler implements ClusterMessageHandler { |
| 161 | @Override |
| 162 | public void handle(ClusterMessage message) { |
| 163 | if (!message.subject().equals(PACKET_OUT_SUBJECT)) { |
| 164 | log.warn("Received message with wrong subject: {}", message); |
| 165 | } |
| 166 | |
| 167 | OutboundPacket packet = SERIALIZER.decode(message.payload()); |
| 168 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 169 | } |
| 170 | } |
| 171 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 172 | private class PacketRequestTracker { |
| 173 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 174 | private ConsistentMap<TrafficSelector, Set<PacketRequest>> requests; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 175 | |
| 176 | public PacketRequestTracker() { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 177 | requests = storageService.<TrafficSelector, Set<PacketRequest>>consistentMapBuilder() |
| 178 | .withName("onos-packet-requests") |
Thomas Vachuska | 19f1229 | 2015-04-20 16:29:15 -0700 | [diff] [blame] | 179 | .withPartitionsDisabled() |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 180 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 181 | .build(); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public boolean add(PacketRequest request) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 185 | Versioned<Set<PacketRequest>> old = requests.get(request.selector()); |
| 186 | if (old != null && old.value().contains(request)) { |
| 187 | return false; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 188 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 189 | // FIXME: add retry logic using a random delay |
| 190 | Set<PacketRequest> newSet = new HashSet<>(); |
| 191 | newSet.add(request); |
| 192 | if (old == null) { |
| 193 | return requests.putIfAbsent(request.selector(), newSet) == null; |
| 194 | } |
| 195 | newSet.addAll(old.value()); |
| 196 | return requests.replace(request.selector(), old.version(), newSet); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | public boolean remove(PacketRequest request) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 200 | Versioned<Set<PacketRequest>> old = requests.get(request.selector()); |
| 201 | if (old == null || !old.value().contains(request)) { |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 204 | // FIXME: add retry logic using a random delay |
| 205 | Set<PacketRequest> newSet = new HashSet<>(old.value()); |
| 206 | newSet.remove(request); |
| 207 | if (newSet.isEmpty()) { |
| 208 | return requests.remove(request.selector(), old.version()); |
| 209 | } |
| 210 | return requests.replace(request.selector(), old.version(), newSet); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | public Set<PacketRequest> requests() { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame^] | 214 | ImmutableSet.Builder<PacketRequest> builder = ImmutableSet.builder(); |
| 215 | requests.values().forEach(v -> builder.addAll(v.value())); |
| 216 | return builder.build(); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 220 | } |