2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services.arp;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.math.BigInteger;
13 import java.util.Map.Entry;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.atomic.AtomicBoolean;
22 import java.util.concurrent.atomic.AtomicLong;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.GatewayMacResolver;
27 import org.opendaylight.ovsdb.openstack.netvirt.api.GatewayMacResolverListener;
28 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
29 import org.opendaylight.ovsdb.openstack.netvirt.providers.NetvirtProvidersProvider;
30 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
31 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
69 import org.opendaylight.yangtools.yang.common.RpcResult;
70 import org.osgi.framework.BundleContext;
71 import org.osgi.framework.ServiceReference;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
75 import com.google.common.base.Preconditions;
76 import com.google.common.collect.ImmutableList;
77 import com.google.common.util.concurrent.FutureCallback;
78 import com.google.common.util.concurrent.Futures;
79 import com.google.common.util.concurrent.JdkFutureAdapters;
80 import com.google.common.util.concurrent.ListenableFuture;
81 import com.google.common.util.concurrent.ListeningExecutorService;
82 import com.google.common.util.concurrent.MoreExecutors;
86 * @author Anil Vishnoi (avishnoi@Brocade.com)
89 public class GatewayMacResolverService extends AbstractServiceInstance
90 implements ConfigInterface, GatewayMacResolver,PacketProcessingListener {
92 private static final Logger LOG = LoggerFactory.getLogger(GatewayMacResolverService.class);
93 private static final short TABLE_FOR_ARP_FLOW = 0;
94 private static final String ARP_REPLY_TO_CONTROLLER_FLOW_NAME = "GatewayArpReplyRouter";
95 private static final int ARP_REPLY_TO_CONTROLLER_FLOW_PRIORITY = 10000;
96 private static final Instruction SEND_TO_CONTROLLER_INSTRUCTION;
97 private ArpSender arpSender;
98 private SalFlowService flowService;
99 private final AtomicLong flowCookie = new AtomicLong();
100 private final ConcurrentMap<Ipv4Address, ArpResolverMetadata> gatewayToArpMetadataMap =
101 new ConcurrentHashMap<>();
102 private static final int ARP_WATCH_BROTHERS = 10;
103 private static final int WAIT_CYCLES = 3;
104 private static final int PER_CYCLE_WAIT_DURATION = 1000;
105 private static final int REFRESH_INTERVAL = 10;
106 private final ListeningExecutorService arpWatcherWall = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(ARP_WATCH_BROTHERS));
107 private final ScheduledExecutorService gatewayMacRefresherPool = Executors.newScheduledThreadPool(1);
108 private final ScheduledExecutorService refreshRequester = Executors.newSingleThreadScheduledExecutor();
109 private AtomicBoolean initializationDone = new AtomicBoolean(false);
112 ApplyActions applyActions = new ApplyActionsBuilder().setAction(
113 ImmutableList.of(ArpFlowFactory.createSendToControllerAction(0))).build();
114 SEND_TO_CONTROLLER_INSTRUCTION = new InstructionBuilder().setOrder(0)
115 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(applyActions).build())
119 public GatewayMacResolverService(){
120 super(Service.GATEWAY_RESOLVER);
123 public GatewayMacResolverService(Service service){
128 if(!initializationDone.get()){
129 initializationDone.set(true);
130 ProviderContext providerContext = NetvirtProvidersProvider.getProviderContext();
131 checkNotNull(providerContext);
132 PacketProcessingService packetProcessingService = providerContext.getRpcService(PacketProcessingService.class);
133 if (packetProcessingService != null) {
134 LOG.debug("{} was found.", PacketProcessingService.class.getSimpleName());
135 this.arpSender = new ArpSender(packetProcessingService);
137 LOG.error("Missing service {}", PacketProcessingService.class.getSimpleName());
138 this.arpSender = null;
140 flowService = providerContext.getRpcService(SalFlowService.class);
141 refreshRequester.scheduleWithFixedDelay(new Runnable(){
145 if (!gatewayToArpMetadataMap.isEmpty()){
146 for(final Entry<Ipv4Address, ArpResolverMetadata> gatewayToArpMetadataEntry : gatewayToArpMetadataMap.entrySet()){
147 final Ipv4Address gatewayIp = gatewayToArpMetadataEntry.getKey();
148 final ArpResolverMetadata gatewayMetaData = gatewayToArpMetadataEntry.getValue();
149 gatewayMacRefresherPool.schedule(new Runnable(){
154 final Node externalNetworkBridge = getExternalBridge(gatewayMetaData.getExternalNetworkBridgeDpid());
155 if(externalNetworkBridge == null){
156 LOG.error("MAC address for gateway {} can not be resolved, because external bridge {} "
157 + "is not connected to controller.",gatewayIp.getValue(),gatewayMetaData.getExternalNetworkBridgeDpid() );
160 LOG.debug("Refresh Gateway Mac for gateway {} using source ip {} and mac {} for ARP request",
161 gatewayIp.getValue(),gatewayMetaData.getArpRequestSourceIp().getValue(),gatewayMetaData.getArpRequestSourceMacAddress().getValue());
163 sendGatewayArpRequest(externalNetworkBridge,gatewayIp,gatewayMetaData.getArpRequestSourceIp(), gatewayMetaData.getArpRequestSourceMacAddress());
165 }, 1, TimeUnit.SECONDS);
169 }, REFRESH_INTERVAL, REFRESH_INTERVAL, TimeUnit.SECONDS);
173 * Method do following actions:
174 * 1. Install flow to direct ARP response packet to controller
175 * 2. Send ARP request packet out on all port of the given External network bridge.
176 * 3. Cache the flow that need to be removed once ARP resolution is done.
177 * 4. Return listenable future so that user can add callback to get the MacAddress
178 * @param externalNetworkBridgeDpid Broadcast ARP request packet on this bridge
179 * @param gatewayIp IP address for which MAC need to be resolved
180 * @param sourceIpAddress Source Ip address for the ARP request packet
181 * @param sourceMacAddress Source Mac address for the ARP request packet
182 * @param periodicRefresh Enable/Disable periodic refresh of the Gateway Mac address
183 * NOTE:Periodic refresh is not supported yet.
184 * @return Future object
187 public ListenableFuture<MacAddress> resolveMacAddress(
188 final GatewayMacResolverListener gatewayMacResolverListener, final Long externalNetworkBridgeDpid,
189 final Ipv4Address gatewayIp, final Ipv4Address sourceIpAddress, final MacAddress sourceMacAddress,
190 final Boolean periodicRefresh){
191 Preconditions.checkNotNull(sourceIpAddress);
192 Preconditions.checkNotNull(sourceMacAddress);
193 Preconditions.checkNotNull(gatewayIp);
195 LOG.info("Trigger Mac resolution for gateway {}, using source ip {} and mac {}",
196 gatewayIp.getValue(),sourceIpAddress.getValue(),sourceMacAddress.getValue());
199 if(gatewayToArpMetadataMap.containsKey(gatewayIp)){
200 if(gatewayToArpMetadataMap.get(gatewayIp).getGatewayMacAddress() != null){
201 return arpWatcherWall.submit(new Callable<MacAddress>(){
204 public MacAddress call() throws Exception {
205 return gatewayToArpMetadataMap.get(gatewayIp).getGatewayMacAddress();
210 gatewayToArpMetadataMap.put(gatewayIp,new ArpResolverMetadata(gatewayMacResolverListener,
211 externalNetworkBridgeDpid, gatewayIp,sourceIpAddress,sourceMacAddress,periodicRefresh));
215 final Node externalNetworkBridge = getExternalBridge(externalNetworkBridgeDpid);
216 if(externalNetworkBridge == null){
217 LOG.error("MAC address for gateway {} can not be resolved, because external bridge {} "
218 + "is not connected to controller.",gatewayIp.getValue(),externalNetworkBridgeDpid );
222 sendGatewayArpRequest(externalNetworkBridge,gatewayIp,sourceIpAddress, sourceMacAddress);
224 //Wait for MacAddress population in cache
225 return waitForMacAddress(gatewayIp);
228 private Node getExternalBridge(final Long externalNetworkBridgeDpid){
229 final String nodeName = OPENFLOW + externalNetworkBridgeDpid;
231 return getOpenFlowNode(nodeName);
234 private void sendGatewayArpRequest(final Node externalNetworkBridge,final Ipv4Address gatewayIp,
235 final Ipv4Address sourceIpAddress, final MacAddress sourceMacAddress){
236 final ArpMessageAddress senderAddress = new ArpMessageAddress(sourceMacAddress,sourceIpAddress);
238 //Build arp reply router flow
239 final Flow arpReplyToControllerFlow = createArpReplyToControllerFlow(senderAddress, gatewayIp);
241 final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(Nodes.class)
242 .child(Node.class, externalNetworkBridge.getKey())
244 final InstanceIdentifier<Flow> flowIid = createFlowIid(arpReplyToControllerFlow, nodeIid);
245 final NodeRef nodeRef = new NodeRef(nodeIid);
248 Future<RpcResult<AddFlowOutput>> addFlowResult = flowService.addFlow(new AddFlowInputBuilder(
249 arpReplyToControllerFlow).setFlowRef(new FlowRef(flowIid)).setNode(nodeRef).build());
250 //wait for flow installation
251 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(addFlowResult),
252 new FutureCallback<RpcResult<AddFlowOutput>>() {
255 public void onSuccess(RpcResult<AddFlowOutput> result) {
256 if (!result.isSuccessful()) {
257 LOG.warn("Flow to route ARP Reply to Controller is not installed successfully : {} \nErrors: {}", flowIid,result.getErrors());
260 LOG.debug("Flow to route ARP Reply to Controller installed successfully : {}", flowIid);
262 ArpResolverMetadata gatewayArpMetadata = gatewayToArpMetadataMap.get(gatewayIp);
263 if (gatewayArpMetadata == null) {
264 LOG.warn("No metadata found for gatewayIp: {}", gatewayIp);
269 gatewayArpMetadata.setFlowToRemove(new RemoveFlowInputBuilder(arpReplyToControllerFlow).setNode(nodeRef).build());
271 //get MAC DA for ARP packets
272 MacAddress arpRequestDestMacAddress = gatewayArpMetadata.getArpRequestDestMacAddress();
274 //Send ARP request packets
275 for (NodeConnector egressNc : externalNetworkBridge.getNodeConnector()) {
276 KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> egressNcIid = nodeIid.child(
277 NodeConnector.class, new NodeConnectorKey(egressNc.getId()));
278 ListenableFuture<RpcResult<Void>> futureSendArpResult = arpSender.sendArp(
279 senderAddress, gatewayIp, arpRequestDestMacAddress, egressNcIid);
280 Futures.addCallback(futureSendArpResult, logResult(gatewayIp, egressNcIid));
285 public void onFailure(Throwable t) {
286 LOG.warn("ARP Reply to Controller flow was not created: {}", flowIid, t);
292 private ListenableFuture<MacAddress> waitForMacAddress(final Ipv4Address gatewayIp){
294 return arpWatcherWall.submit(new Callable<MacAddress>(){
297 public MacAddress call() throws Exception {
298 for(int cycle = 0;cycle < WAIT_CYCLES;cycle++){
299 //Sleep before checking mac address, so meanwhile ARP request packets
300 // will be broadcasted on the bridge.
301 Thread.sleep(PER_CYCLE_WAIT_DURATION);
302 ArpResolverMetadata arpResolverMetadata = gatewayToArpMetadataMap.get(gatewayIp);
303 if(arpResolverMetadata != null && arpResolverMetadata.getGatewayMacAddress() != null){
304 if(!arpResolverMetadata.isPeriodicRefresh()){
305 return gatewayToArpMetadataMap.remove(gatewayIp).getGatewayMacAddress();
307 return arpResolverMetadata.getGatewayMacAddress();
315 private Flow createArpReplyToControllerFlow(final ArpMessageAddress senderAddress, final Ipv4Address ipForRequestedMac) {
316 checkNotNull(senderAddress);
317 checkNotNull(ipForRequestedMac);
318 FlowBuilder arpFlow = new FlowBuilder().setTableId(TABLE_FOR_ARP_FLOW)
319 .setFlowName(ARP_REPLY_TO_CONTROLLER_FLOW_NAME)
320 .setPriority(ARP_REPLY_TO_CONTROLLER_FLOW_PRIORITY)
321 .setBufferId(OFConstants.OFP_NO_BUFFER)
324 .setCookie(new FlowCookie(BigInteger.valueOf(flowCookie.incrementAndGet())))
325 .setFlags(new FlowModFlags(false, false, false, false, false));
327 EthernetMatch ethernetMatch = ArpFlowFactory.createEthernetMatch(senderAddress.getHardwareAddress());
328 ArpMatch arpMatch = ArpFlowFactory.createArpMatch(senderAddress, ipForRequestedMac);
329 Match match = new MatchBuilder().setEthernetMatch(ethernetMatch).setLayer3Match(arpMatch).build();
330 arpFlow.setMatch(match);
331 arpFlow.setInstructions(new InstructionsBuilder().setInstruction(
332 ImmutableList.of(SEND_TO_CONTROLLER_INSTRUCTION)).build());
333 arpFlow.setId(createFlowId(ipForRequestedMac));
334 return arpFlow.build();
337 private FlowId createFlowId(Ipv4Address ipForRequestedMac) {
338 String flowId = ARP_REPLY_TO_CONTROLLER_FLOW_NAME + "|" + ipForRequestedMac.getValue();
339 return new FlowId(flowId);
342 private static InstanceIdentifier<Flow> createFlowIid(Flow flow, InstanceIdentifier<Node> nodeIid) {
343 return nodeIid.builder()
344 .augmentation(FlowCapableNode.class)
345 .child(Table.class, new TableKey(flow.getTableId()))
346 .child(Flow.class, new FlowKey(flow.getId()))
350 private FutureCallback<RpcResult<Void>> logResult(final Ipv4Address tpa,
351 final KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> egressNcIid) {
352 return new FutureCallback<RpcResult<Void>>() {
355 public void onSuccess(RpcResult<Void> result) {
356 LOG.debug("ARP Request for IP {} was sent from {}.", tpa.getValue(), egressNcIid);
360 public void onFailure(Throwable t) {
361 LOG.warn("ARP Request for IP {} was NOT sent from {}.", tpa.getValue(), egressNcIid);
367 public void onPacketReceived(PacketReceived potentialArp) {
368 Arp arp = ArpResolverUtils.getArpFrom(potentialArp);
370 if (arp.getOperation() != ArpOperation.REPLY.intValue()) {
371 LOG.trace("Packet is not ARP REPLY packet.");
374 if (LOG.isTraceEnabled()) {
375 LOG.trace("ARP REPLY received - {}", ArpUtils.getArpToStringFormat(arp));
377 NodeKey nodeKey = potentialArp.getIngress().getValue().firstKeyOf(Node.class, NodeKey.class);
378 if (nodeKey == null) {
379 LOG.info("Unknown source node of ARP packet: {}", potentialArp);
382 Ipv4Address gatewayIpAddress = ArpUtils.bytesToIp(arp.getSenderProtocolAddress());
383 MacAddress gatewayMacAddress = ArpUtils.bytesToMac(arp.getSenderHardwareAddress());
384 ArpResolverMetadata candidateGatewayIp = gatewayToArpMetadataMap.get(gatewayIpAddress);
385 if(candidateGatewayIp != null){
386 LOG.debug("Resolved MAC for Gateway Ip {} is {}",gatewayIpAddress.getValue(),gatewayMacAddress.getValue());
387 candidateGatewayIp.setGatewayMacAddress(gatewayMacAddress);
388 flowService.removeFlow(candidateGatewayIp.getFlowToRemove());
394 public void setDependencies(BundleContext bundleContext,
395 ServiceReference serviceReference) {
396 super.setDependencies(bundleContext.getServiceReference(GatewayMacResolver.class.getName()), this);
401 public void setDependencies(Object impl) {}
404 public void stopPeriodicRefresh(Ipv4Address gatewayIp) {
406 gatewayToArpMetadataMap.remove(gatewayIp);