sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [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.openstackswitching; |
| 17 | |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 18 | import org.onlab.packet.ARP; |
| 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 23 | import org.onosproject.net.flow.TrafficTreatment; |
| 24 | import org.onosproject.net.packet.DefaultOutboundPacket; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 25 | import org.onosproject.net.packet.InboundPacket; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 26 | import org.onosproject.net.packet.OutboundPacket; |
| 27 | import org.onosproject.net.packet.PacketService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 30 | import java.nio.ByteBuffer; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame^] | 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 33 | |
| 34 | /** |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 35 | * Handles ARP packet from VMs. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 36 | */ |
| 37 | public class OpenstackArpHandler { |
| 38 | |
| 39 | private static Logger log = LoggerFactory |
| 40 | .getLogger(OpenstackArpHandler.class); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 41 | private PacketService packetService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame^] | 42 | private OpenstackRestHandler restHandler; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 45 | * Returns OpenstackArpHandler reference. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 46 | * |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame^] | 47 | * @param restHandler rest API handler reference |
| 48 | * @param packetService PacketService reference |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 49 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame^] | 50 | public OpenstackArpHandler(OpenstackRestHandler restHandler, PacketService packetService) { |
| 51 | this.restHandler = checkNotNull(restHandler); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 52 | this.packetService = packetService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Processes ARP packets. |
| 57 | * |
| 58 | * @param pkt ARP request packet |
| 59 | */ |
| 60 | public void processPacketIn(InboundPacket pkt) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 61 | Ethernet ethernet = pkt.parsed(); |
| 62 | ARP arp = (ARP) ethernet.getPayload(); |
| 63 | |
| 64 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 65 | byte[] srcMacAddress = arp.getSenderHardwareAddress(); |
| 66 | byte[] srcIPAddress = arp.getSenderProtocolAddress(); |
| 67 | byte[] dstIPAddress = arp.getTargetProtocolAddress(); |
| 68 | |
| 69 | //Searches the Dst MAC Address based on openstackPortMap |
| 70 | MacAddress macAddress = null; |
| 71 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame^] | 72 | OpenstackPort openstackPort = restHandler.getPorts().stream(). |
| 73 | filter(e -> e.fixedIps().containsValue(Ip4Address.valueOf( |
| 74 | dstIPAddress))).findAny().orElse(null); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 75 | |
| 76 | if (openstackPort != null) { |
| 77 | macAddress = openstackPort.macAddress(); |
| 78 | log.debug("Found MACAddress: {}", macAddress.toString()); |
| 79 | } else { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | //Creates a response packet |
| 84 | ARP arpReply = new ARP(); |
| 85 | arpReply.setOpCode(ARP.OP_REPLY) |
| 86 | .setHardwareAddressLength(arp.getHardwareAddressLength()) |
| 87 | .setHardwareType(arp.getHardwareType()) |
| 88 | .setProtocolAddressLength(arp.getProtocolAddressLength()) |
| 89 | .setProtocolType(arp.getProtocolType()) |
| 90 | .setSenderHardwareAddress(macAddress.toBytes()) |
| 91 | .setSenderProtocolAddress(dstIPAddress) |
| 92 | .setTargetHardwareAddress(srcMacAddress) |
| 93 | .setTargetProtocolAddress(srcIPAddress); |
| 94 | |
| 95 | //Sends a response packet |
| 96 | ethernet.setDestinationMACAddress(srcMacAddress) |
| 97 | .setSourceMACAddress(macAddress) |
| 98 | .setEtherType(Ethernet.TYPE_ARP) |
| 99 | .setPayload(arpReply); |
| 100 | |
| 101 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 102 | builder.setOutput(pkt.receivedFrom().port()); |
| 103 | OutboundPacket packet = new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), |
| 104 | builder.build(), ByteBuffer.wrap(ethernet.serialize())); |
| 105 | packetService.emit(packet); |
| 106 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 107 | } |
| 108 | } |