sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [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.segmentrouting; |
| 17 | |
| 18 | import org.onlab.packet.ARP; |
| 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.Port; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 31 | import org.onosproject.net.packet.InboundPacket; |
| 32 | import org.onosproject.net.HostId; |
| 33 | import org.onosproject.net.packet.OutboundPacket; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import java.nio.ByteBuffer; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 38 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | |
| 41 | public class ArpHandler { |
| 42 | |
| 43 | private static Logger log = LoggerFactory.getLogger(ArpHandler.class); |
| 44 | |
| 45 | private SegmentRoutingManager srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 46 | private DeviceConfiguration config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Creates an ArpHandler object. |
| 50 | * |
| 51 | * @param srManager SegmentRoutingManager object |
| 52 | */ |
| 53 | public ArpHandler(SegmentRoutingManager srManager) { |
| 54 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 55 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Processes incoming ARP packets. |
| 60 | * If it is an ARP request to router itself or known hosts, |
| 61 | * then it sends ARP response. |
| 62 | * If it is an ARP request to unknown hosts in its own subnet, |
| 63 | * then it flood the ARP request to the ports. |
| 64 | * If it is an ARP response, then set a flow rule for the host |
| 65 | * and forward any IP packets to the host in the packet buffer to the host. |
| 66 | * |
| 67 | * @param pkt incoming packet |
| 68 | */ |
| 69 | public void processPacketIn(InboundPacket pkt) { |
| 70 | |
| 71 | Ethernet ethernet = pkt.parsed(); |
| 72 | ARP arp = (ARP) ethernet.getPayload(); |
| 73 | |
| 74 | ConnectPoint connectPoint = pkt.receivedFrom(); |
| 75 | PortNumber inPort = connectPoint.port(); |
| 76 | DeviceId deviceId = connectPoint.deviceId(); |
| 77 | byte[] senderMacAddressByte = arp.getSenderHardwareAddress(); |
| 78 | Ip4Address hostIpAddress = Ip4Address.valueOf(arp.getSenderProtocolAddress()); |
| 79 | |
| 80 | srManager.routingRulePopulator.populateIpRuleForHost(deviceId, hostIpAddress, MacAddress. |
| 81 | valueOf(senderMacAddressByte), inPort); |
| 82 | |
| 83 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 84 | handleArpRequest(deviceId, connectPoint, ethernet); |
| 85 | } else { |
| 86 | srManager.ipHandler.forwardPackets(deviceId, hostIpAddress); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void handleArpRequest(DeviceId deviceId, ConnectPoint inPort, Ethernet payload) { |
| 91 | |
| 92 | ARP arpRequest = (ARP) payload.getPayload(); |
| 93 | HostId targetHostId = HostId.hostId(MacAddress.valueOf( |
| 94 | arpRequest.getTargetHardwareAddress())); |
| 95 | |
| 96 | // ARP request for router |
| 97 | if (isArpReqForRouter(deviceId, arpRequest)) { |
| 98 | Ip4Address targetAddress = Ip4Address.valueOf(arpRequest.getTargetProtocolAddress()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 99 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 100 | sendArpResponse(arpRequest, config.getRouterMacForAGatewayIp(targetAddress)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 101 | // ARP request for known hosts |
| 102 | } else if (srManager.hostService.getHost(targetHostId) != null) { |
| 103 | MacAddress targetMac = srManager.hostService.getHost(targetHostId).mac(); |
| 104 | sendArpResponse(arpRequest, targetMac); |
| 105 | |
| 106 | // ARP request for unknown host in the subnet |
| 107 | } else if (isArpReqForSubnet(deviceId, arpRequest)) { |
| 108 | flood(payload, inPort); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | private boolean isArpReqForRouter(DeviceId deviceId, ARP arpRequest) { |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 114 | List<Ip4Address> gatewayIpAddresses = config.getSubnetGatewayIps(deviceId); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 115 | if (gatewayIpAddresses != null) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 116 | Ip4Address targetProtocolAddress = Ip4Address.valueOf(arpRequest |
| 117 | .getTargetProtocolAddress()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 118 | if (gatewayIpAddresses.contains(targetProtocolAddress)) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | private boolean isArpReqForSubnet(DeviceId deviceId, ARP arpRequest) { |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 126 | return config.getSubnets(deviceId).stream() |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 127 | .anyMatch((prefix)-> |
| 128 | prefix.contains(Ip4Address. |
| 129 | valueOf(arpRequest. |
| 130 | getTargetProtocolAddress()))); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sends an APR request for the target IP address to all ports except in-port. |
| 135 | * |
| 136 | * @param deviceId Switch device ID |
| 137 | * @param targetAddress target IP address for ARP |
| 138 | * @param inPort in-port |
| 139 | */ |
| 140 | public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
| 141 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 142 | byte[] senderMacAddress = config.getDeviceMac(deviceId).toBytes(); |
| 143 | byte[] senderIpAddress = config.getRouterIp(deviceId).toOctets(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | |
| 145 | ARP arpRequest = new ARP(); |
| 146 | arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 147 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 148 | .setHardwareAddressLength( |
| 149 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 150 | .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH) |
| 151 | .setOpCode(ARP.OP_REQUEST) |
| 152 | .setSenderHardwareAddress(senderMacAddress) |
| 153 | .setTargetHardwareAddress(MacAddress.ZERO.toBytes()) |
| 154 | .setSenderProtocolAddress(senderIpAddress) |
| 155 | .setTargetProtocolAddress(targetAddress.toOctets()); |
| 156 | |
| 157 | Ethernet eth = new Ethernet(); |
| 158 | eth.setDestinationMACAddress(MacAddress.BROADCAST.toBytes()) |
| 159 | .setSourceMACAddress(senderMacAddress) |
| 160 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest); |
| 161 | |
| 162 | flood(eth, inPort); |
| 163 | } |
| 164 | |
| 165 | private void sendArpResponse(ARP arpRequest, MacAddress targetMac) { |
| 166 | |
| 167 | ARP arpReply = new ARP(); |
| 168 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 169 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 170 | .setHardwareAddressLength( |
| 171 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 172 | .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH) |
| 173 | .setOpCode(ARP.OP_REPLY) |
| 174 | .setSenderHardwareAddress(targetMac.toBytes()) |
| 175 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 176 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 177 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 178 | |
| 179 | Ethernet eth = new Ethernet(); |
| 180 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
| 181 | .setSourceMACAddress(targetMac.toBytes()) |
| 182 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply); |
| 183 | |
| 184 | |
| 185 | HostId dstId = HostId.hostId(MacAddress.valueOf( |
| 186 | arpReply.getTargetHardwareAddress())); |
| 187 | Host dst = srManager.hostService.getHost(dstId); |
| 188 | if (dst == null) { |
| 189 | log.warn("Cannot send ARP response to unknown device"); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 194 | setOutput(dst.location().port()).build(); |
| 195 | OutboundPacket packet = new DefaultOutboundPacket(dst.location().deviceId(), |
| 196 | treatment, ByteBuffer.wrap(eth.serialize())); |
| 197 | |
| 198 | srManager.packetService.emit(packet); |
| 199 | } |
| 200 | |
| 201 | private void flood(Ethernet request, ConnectPoint inPort) { |
| 202 | TrafficTreatment.Builder builder; |
| 203 | ByteBuffer buf = ByteBuffer.wrap(request.serialize()); |
| 204 | |
| 205 | for (Port port: srManager.deviceService.getPorts(inPort.deviceId())) { |
| 206 | if (!port.number().equals(inPort.port()) && |
| 207 | port.number().toLong() > 0) { |
| 208 | builder = DefaultTrafficTreatment.builder(); |
| 209 | builder.setOutput(port.number()); |
| 210 | srManager.packetService.emit(new DefaultOutboundPacket(inPort.deviceId(), |
| 211 | builder.build(), buf)); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | } |