Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.store.proxyarp.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 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; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.mastership.MastershipService; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.Host; |
| 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.host.HostEvent; |
| 33 | import org.onosproject.net.host.HostListener; |
| 34 | import org.onosproject.net.host.HostService; |
| 35 | import org.onosproject.net.proxyarp.ProxyArpStore; |
| 36 | import org.onosproject.net.proxyarp.ProxyArpStoreDelegate; |
| 37 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.serializers.KryoSerializer; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | import java.nio.ByteBuffer; |
| 45 | import java.util.Map; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | |
| 48 | import static org.onlab.util.BoundedThreadPool.newFixedThreadPool; |
| 49 | import static org.onlab.util.Tools.groupedThreads; |
| 50 | |
| 51 | /** |
| 52 | * Implementation of proxy ARP distribution mechanism. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | @Service |
| 56 | public class DistributedProxyArpStore implements ProxyArpStore { |
| 57 | |
| 58 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
| 60 | private static final MessageSubject ARP_RESPONSE_MESSAGE = |
| 61 | new MessageSubject("onos-arp-response"); |
| 62 | |
| 63 | protected final KryoSerializer serializer = new KryoSerializer() { |
| 64 | @Override |
| 65 | protected void setupKryoPool() { |
| 66 | serializerPool = KryoNamespace.newBuilder() |
| 67 | .register(KryoNamespaces.API) |
| 68 | .register(ArpResponseMessage.class) |
| 69 | .register(ByteBuffer.class) |
| 70 | .build(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | private ProxyArpStoreDelegate delegate; |
| 75 | |
| 76 | private Map<HostId, ArpResponseMessage> pendingMessages = Maps.newConcurrentMap(); |
| 77 | |
| 78 | private ExecutorService executor = |
| 79 | newFixedThreadPool(4, groupedThreads("onos/arp", "sender-%d")); |
| 80 | |
| 81 | private NodeId localNodeId; |
| 82 | |
| 83 | private HostListener hostListener = new InternalHostListener(); |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected MastershipService mastershipService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected ClusterService clusterService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected ClusterCommunicationService commService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected HostService hostService; |
| 96 | |
| 97 | |
| 98 | @Activate |
| 99 | protected void activate() { |
| 100 | localNodeId = clusterService.getLocalNode().id(); |
| 101 | hostService.addListener(hostListener); |
| 102 | commService.addSubscriber(ARP_RESPONSE_MESSAGE, serializer::decode, |
| 103 | this::processArpResponse, executor); |
| 104 | log.info("Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | protected void deactivate() { |
| 109 | commService.removeSubscriber(ARP_RESPONSE_MESSAGE); |
| 110 | hostService.removeListener(hostListener); |
| 111 | log.info("Stopped"); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void forward(ConnectPoint outPort, Host subject, ByteBuffer packet) { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 116 | /*NodeId nodeId = mastershipService.getMasterFor(outPort.deviceId()); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 117 | if (nodeId.equals(localNodeId)) { |
| 118 | if (delegate != null) { |
| 119 | delegate.emitResponse(outPort, packet); |
| 120 | } |
| 121 | } else { |
| 122 | log.info("Forwarding ARP response from {} to {}", subject.id(), outPort); |
| 123 | commService.unicast(new ArpResponseMessage(outPort, subject, packet.array()), |
| 124 | ARP_RESPONSE_MESSAGE, serializer::encode, nodeId); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 125 | }*/ |
| 126 | //FIXME: Code above may be unnecessary and therefore cluster messaging |
| 127 | // and pendingMessages could be pruned as well. |
| 128 | delegate.emitResponse(outPort, packet); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void setDelegate(ProxyArpStoreDelegate delegate) { |
| 133 | this.delegate = delegate; |
| 134 | } |
| 135 | |
| 136 | // Processes the incoming ARP response message. |
| 137 | private void processArpResponse(ArpResponseMessage msg) { |
| 138 | pendingMessages.put(msg.subject.id(), msg); |
| 139 | if (hostService.getHost(msg.subject.id()) != null) { |
| 140 | checkPendingArps(msg.subject.id()); |
| 141 | } |
| 142 | // FIXME: figure out pruning so stuff does not build up |
| 143 | } |
| 144 | |
| 145 | // Checks for pending ARP response message for the specified host. |
| 146 | // If one exists, emit response via delegate. |
| 147 | private void checkPendingArps(HostId id) { |
| 148 | ArpResponseMessage msg = pendingMessages.remove(id); |
| 149 | if (msg != null && delegate != null) { |
| 150 | log.info("Emitting ARP response from {} to {}", id, msg.outPort); |
| 151 | delegate.emitResponse(msg.outPort, ByteBuffer.wrap(msg.packet)); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Message carrying an ARP response. |
| 156 | private static class ArpResponseMessage { |
| 157 | private ConnectPoint outPort; |
| 158 | private Host subject; |
| 159 | private byte[] packet; |
| 160 | |
| 161 | public ArpResponseMessage(ConnectPoint outPort, Host subject, byte[] packet) { |
| 162 | this.outPort = outPort; |
| 163 | this.subject = subject; |
| 164 | this.packet = packet; |
| 165 | } |
| 166 | |
| 167 | private ArpResponseMessage() { |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private class InternalHostListener implements HostListener { |
| 172 | @Override |
| 173 | public void event(HostEvent event) { |
| 174 | checkPendingArps(event.subject().id()); |
| 175 | } |
| 176 | } |
| 177 | } |