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