Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -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 | */ |
| 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; |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame^] | 40 | import org.onosproject.store.serializers.StoreSerializer; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 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 | |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame^] | 63 | protected final StoreSerializer serializer = StoreSerializer.using( |
| 64 | KryoNamespace.newBuilder() |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 65 | .register(KryoNamespaces.API) |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame^] | 66 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 67 | .register(ArpResponseMessage.class) |
| 68 | .register(ByteBuffer.class) |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame^] | 69 | .build("ProxyArpStore")); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 70 | |
| 71 | private ProxyArpStoreDelegate delegate; |
| 72 | |
| 73 | private Map<HostId, ArpResponseMessage> pendingMessages = Maps.newConcurrentMap(); |
| 74 | |
| 75 | private ExecutorService executor = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 76 | newFixedThreadPool(4, groupedThreads("onos/arp", "sender-%d", log)); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 77 | |
| 78 | private NodeId localNodeId; |
| 79 | |
| 80 | private HostListener hostListener = new InternalHostListener(); |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected MastershipService mastershipService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected ClusterService clusterService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected ClusterCommunicationService commService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected HostService hostService; |
| 93 | |
| 94 | |
| 95 | @Activate |
| 96 | protected void activate() { |
| 97 | localNodeId = clusterService.getLocalNode().id(); |
| 98 | hostService.addListener(hostListener); |
| 99 | commService.addSubscriber(ARP_RESPONSE_MESSAGE, serializer::decode, |
| 100 | this::processArpResponse, executor); |
| 101 | log.info("Started"); |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | protected void deactivate() { |
| 106 | commService.removeSubscriber(ARP_RESPONSE_MESSAGE); |
| 107 | hostService.removeListener(hostListener); |
| 108 | log.info("Stopped"); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void forward(ConnectPoint outPort, Host subject, ByteBuffer packet) { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 113 | /*NodeId nodeId = mastershipService.getMasterFor(outPort.deviceId()); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 114 | if (nodeId.equals(localNodeId)) { |
| 115 | if (delegate != null) { |
| 116 | delegate.emitResponse(outPort, packet); |
| 117 | } |
| 118 | } else { |
| 119 | log.info("Forwarding ARP response from {} to {}", subject.id(), outPort); |
| 120 | commService.unicast(new ArpResponseMessage(outPort, subject, packet.array()), |
| 121 | ARP_RESPONSE_MESSAGE, serializer::encode, nodeId); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 122 | }*/ |
| 123 | //FIXME: Code above may be unnecessary and therefore cluster messaging |
| 124 | // and pendingMessages could be pruned as well. |
| 125 | delegate.emitResponse(outPort, packet); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void setDelegate(ProxyArpStoreDelegate delegate) { |
| 130 | this.delegate = delegate; |
| 131 | } |
| 132 | |
| 133 | // Processes the incoming ARP response message. |
| 134 | private void processArpResponse(ArpResponseMessage msg) { |
| 135 | pendingMessages.put(msg.subject.id(), msg); |
| 136 | if (hostService.getHost(msg.subject.id()) != null) { |
| 137 | checkPendingArps(msg.subject.id()); |
| 138 | } |
| 139 | // FIXME: figure out pruning so stuff does not build up |
| 140 | } |
| 141 | |
| 142 | // Checks for pending ARP response message for the specified host. |
| 143 | // If one exists, emit response via delegate. |
| 144 | private void checkPendingArps(HostId id) { |
| 145 | ArpResponseMessage msg = pendingMessages.remove(id); |
| 146 | if (msg != null && delegate != null) { |
| 147 | log.info("Emitting ARP response from {} to {}", id, msg.outPort); |
| 148 | delegate.emitResponse(msg.outPort, ByteBuffer.wrap(msg.packet)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Message carrying an ARP response. |
| 153 | private static class ArpResponseMessage { |
| 154 | private ConnectPoint outPort; |
| 155 | private Host subject; |
| 156 | private byte[] packet; |
| 157 | |
| 158 | public ArpResponseMessage(ConnectPoint outPort, Host subject, byte[] packet) { |
| 159 | this.outPort = outPort; |
| 160 | this.subject = subject; |
| 161 | this.packet = packet; |
| 162 | } |
| 163 | |
| 164 | private ArpResponseMessage() { |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private class InternalHostListener implements HostListener { |
| 169 | @Override |
| 170 | public void event(HostEvent event) { |
| 171 | checkPendingArps(event.subject().id()); |
| 172 | } |
| 173 | } |
| 174 | } |