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 | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.serializers.KryoSerializer; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 41 | import org.onosproject.store.service.ConsistentMap; |
| 42 | import org.onosproject.store.service.Serializer; |
| 43 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 44 | import org.onosproject.store.service.Versioned; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 47 | import java.util.HashSet; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame^] | 48 | import java.util.List; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 49 | import java.util.Set; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 50 | import java.util.concurrent.ExecutorService; |
| 51 | import java.util.concurrent.Executors; |
| 52 | |
| 53 | import static org.onlab.util.Tools.groupedThreads; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Distributed packet store implementation allowing packets to be sent to |
| 58 | * remote instances. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
| 62 | public class DistributedPacketStore |
| 63 | extends AbstractStore<PacketEvent, PacketStoreDelegate> |
| 64 | implements PacketStore { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 68 | // TODO: make this configurable. |
| 69 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 70 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 72 | protected MastershipService mastershipService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 75 | protected ClusterService clusterService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 78 | protected ClusterCommunicationService communicationService; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 79 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected StorageService storageService; |
| 82 | |
| 83 | private PacketRequestTracker tracker; |
| 84 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 85 | private static final MessageSubject PACKET_OUT_SUBJECT = |
| 86 | new MessageSubject("packet-out"); |
| 87 | |
| 88 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 89 | @Override |
| 90 | protected void setupKryoPool() { |
| 91 | serializerPool = KryoNamespace.newBuilder() |
| 92 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 93 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 94 | .build(); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 95 | } |
| 96 | }; |
| 97 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 98 | private ExecutorService messageHandlingExecutor; |
| 99 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 100 | @Activate |
| 101 | public void activate() { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 102 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 103 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 104 | groupedThreads("onos/store/packet", "message-handlers")); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 105 | |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 106 | communicationService.<OutboundPacket>addSubscriber(PACKET_OUT_SUBJECT, |
| 107 | SERIALIZER::decode, |
| 108 | packet -> notifyDelegate(new PacketEvent(Type.EMIT, packet)), |
| 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 | |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 137 | communicationService.unicast(packet, PACKET_OUT_SUBJECT, SERIALIZER::encode, master) |
| 138 | .whenComplete((r, error) -> { |
| 139 | if (error != null) { |
| 140 | log.warn("Failed to send packet-out to {}", master, error); |
| 141 | } |
| 142 | }); |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 143 | } |
| 144 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public boolean requestPackets(PacketRequest request) { |
| 147 | return tracker.add(request); |
| 148 | } |
| 149 | |
| 150 | @Override |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 151 | public boolean cancelPackets(PacketRequest request) { |
| 152 | return tracker.remove(request); |
| 153 | } |
| 154 | |
| 155 | @Override |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame^] | 156 | public List<PacketRequest> existingRequests() { |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 157 | return tracker.requests(); |
| 158 | } |
| 159 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 160 | private class PacketRequestTracker { |
| 161 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 162 | private ConsistentMap<TrafficSelector, Set<PacketRequest>> requests; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 163 | |
| 164 | public PacketRequestTracker() { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 165 | requests = storageService.<TrafficSelector, Set<PacketRequest>>consistentMapBuilder() |
| 166 | .withName("onos-packet-requests") |
Thomas Vachuska | 19f1229 | 2015-04-20 16:29:15 -0700 | [diff] [blame] | 167 | .withPartitionsDisabled() |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 168 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 169 | .build(); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | public boolean add(PacketRequest request) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 173 | Versioned<Set<PacketRequest>> old = requests.get(request.selector()); |
| 174 | if (old != null && old.value().contains(request)) { |
| 175 | return false; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 176 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 177 | // FIXME: add retry logic using a random delay |
| 178 | Set<PacketRequest> newSet = new HashSet<>(); |
| 179 | newSet.add(request); |
| 180 | if (old == null) { |
| 181 | return requests.putIfAbsent(request.selector(), newSet) == null; |
| 182 | } |
| 183 | newSet.addAll(old.value()); |
| 184 | return requests.replace(request.selector(), old.version(), newSet); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | public boolean remove(PacketRequest request) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 188 | Versioned<Set<PacketRequest>> old = requests.get(request.selector()); |
| 189 | if (old == null || !old.value().contains(request)) { |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 192 | // FIXME: add retry logic using a random delay |
| 193 | Set<PacketRequest> newSet = new HashSet<>(old.value()); |
| 194 | newSet.remove(request); |
| 195 | if (newSet.isEmpty()) { |
| 196 | return requests.remove(request.selector(), old.version()); |
| 197 | } |
| 198 | return requests.replace(request.selector(), old.version(), newSet); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame^] | 201 | public List<PacketRequest> requests() { |
| 202 | List<PacketRequest> list = Lists.newArrayList(); |
| 203 | requests.values().forEach(v -> list.addAll(v.value())); |
| 204 | list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue()); |
| 205 | return list; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 209 | } |