2 * Copyright © 2016, 2017 Hewlett Packard Enterprise, Co. 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.netvirt.vpnmanager;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.liblldp.EtherTypes;
15 import org.opendaylight.genius.mdsalutil.ActionInfo;
16 import org.opendaylight.genius.mdsalutil.MDSALUtil;
17 import org.opendaylight.genius.mdsalutil.NWUtil;
18 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
19 import org.opendaylight.genius.mdsalutil.packet.ARP;
20 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class ArpUtils {
27 private static final Logger LOG = LoggerFactory.getLogger(ArpUtils.class);
29 public static TransmitPacketInput createArpRequestInput(BigInteger dpnId, long groupId, byte[] abySenderMAC,
30 byte[] abySenderIpAddress, byte[] abyTargetIpAddress) {
31 return createArpRequestInput(dpnId, groupId, abySenderMAC, abySenderIpAddress, abyTargetIpAddress, null);
34 public static TransmitPacketInput createArpRequestInput(BigInteger dpnId, byte[] abySenderMAC,
35 byte[] abySenderIpAddress, byte[] abyTargetIpAddress, NodeConnectorRef ingress) {
36 return createArpRequestInput(dpnId, null, abySenderMAC, (byte[]) null, abySenderIpAddress, abyTargetIpAddress,
37 ingress, new ArrayList<ActionInfo>());
40 public static TransmitPacketInput createArpRequestInput(BigInteger dpnId, Long groupId, byte[] abySenderMAC,
41 byte[] abySenderIpAddress, byte[] abyTargetIpAddress, NodeConnectorRef ingress) {
42 List<ActionInfo> lstActionInfo = new ArrayList<ActionInfo>();
43 return createArpRequestInput(dpnId, groupId, abySenderMAC, null, abySenderIpAddress, abyTargetIpAddress,
44 ingress, lstActionInfo);
47 public static TransmitPacketInput createArpRequestInput(BigInteger dpnId, Long groupId, byte[] abySenderMAC,
48 byte[] abyTargetMAC, byte[] abySenderIpAddress, byte[] abyTargetIpAddress, NodeConnectorRef ingress,
49 List<ActionInfo> lstActionInfo) {
51 LOG.info("SubnetRoutePacketInHandler: sendArpRequest dpnId {}, actions {},"
52 + " groupId {}, senderIPAddress {}, targetIPAddress {}",
53 dpnId, lstActionInfo, groupId, NWUtil.toStringIpAddress(abySenderIpAddress),
54 NWUtil.toStringIpAddress(abyTargetIpAddress));
55 if (abySenderIpAddress != null) {
59 byte[] targetMac = abyTargetMAC != null ? abyTargetMAC : VpnConstants.MAC_Broadcast;
60 arpPacket = createARPPacket(ARP.REQUEST, abySenderMAC, abySenderIpAddress, targetMac, abyTargetIpAddress);
61 ethPacket = createEthernetPacket(abySenderMAC, VpnConstants.EthernetDestination_Broadcast, arpPacket);
62 if (groupId != null) {
63 lstActionInfo.add(new ActionGroup(groupId));
65 if (ingress != null) {
66 return MDSALUtil.getPacketOutFromController(lstActionInfo, ethPacket, dpnId.longValue(), ingress);
68 return MDSALUtil.getPacketOutDefault(lstActionInfo, ethPacket, dpnId);
71 LOG.info("SubnetRoutePacketInHandler: Unable to send ARP request because client port has no IP ");
76 public static byte[] getMacInBytes(String macAddress) {
77 String[] macAddressParts = macAddress.split(":");
79 // convert hex string to byte values
80 byte[] macAddressBytes = new byte[6];
81 for (int i = 0; i < 6; i++) {
82 Integer hex = Integer.parseInt(macAddressParts[i], 16);
83 macAddressBytes[i] = hex.byteValue();
86 return macAddressBytes;
89 private static byte[] createEthernetPacket(byte[] sourceMAC, byte[] targetMAC, byte[] arp) {
90 Ethernet ethernet = new Ethernet();
91 byte[] rawEthPkt = null;
93 ethernet.setSourceMACAddress(sourceMAC);
94 ethernet.setDestinationMACAddress(targetMAC);
95 ethernet.setEtherType(EtherTypes.ARP.shortValue());
96 ethernet.setRawPayload(arp);
97 rawEthPkt = ethernet.serialize();
98 } catch (Exception ex) {
100 "VPNUtil: Serialized Ethernet packet with sourceMacAddress {} targetMacAddress {} exception ",
101 sourceMAC, targetMAC, ex);
106 private static byte[] createARPPacket(short opCode, byte[] senderMacAddress, byte[] senderIP,
107 byte[] targetMacAddress, byte[] targetIP) {
109 byte[] rawArpPkt = null;
111 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
112 arp.setProtocolType(EtherTypes.IPv4.shortValue());
113 arp.setHardwareAddressLength((byte) 6);
114 arp.setProtocolAddressLength((byte) 4);
115 arp.setOpCode(opCode);
116 arp.setSenderHardwareAddress(senderMacAddress);
117 arp.setSenderProtocolAddress(senderIP);
118 arp.setTargetHardwareAddress(targetMacAddress);
119 arp.setTargetProtocolAddress(targetIP);
120 rawArpPkt = arp.serialize();
121 } catch (Exception ex) {
122 LOG.error("VPNUtil: Serialized ARP packet with senderIp {} targetIP {} exception ", senderIP,