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