sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 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; |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Prefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Host; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 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; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 34 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 35 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.nio.ByteBuffer; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 40 | import java.util.Set; |
| 41 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Handler of ARP packets that responses or forwards ARP packets that |
| 46 | * are sent to the controller. |
| 47 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 48 | public class ArpHandler { |
| 49 | |
| 50 | private static Logger log = LoggerFactory.getLogger(ArpHandler.class); |
| 51 | |
| 52 | private SegmentRoutingManager srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 53 | private DeviceConfiguration config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Creates an ArpHandler object. |
| 57 | * |
| 58 | * @param srManager SegmentRoutingManager object |
| 59 | */ |
| 60 | public ArpHandler(SegmentRoutingManager srManager) { |
| 61 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 62 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Processes incoming ARP packets. |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 67 | * |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 68 | * If it is an ARP request to router itself or known hosts, |
| 69 | * then it sends ARP response. |
| 70 | * If it is an ARP request to unknown hosts in its own subnet, |
| 71 | * then it flood the ARP request to the ports. |
| 72 | * If it is an ARP response, then set a flow rule for the host |
| 73 | * and forward any IP packets to the host in the packet buffer to the host. |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 74 | * <p> |
| 75 | * Note: We handles all ARP packet in, even for those ARP packets between |
| 76 | * hosts in the same subnet. |
| 77 | * For an ARP packet with broadcast destination MAC, |
| 78 | * some switches pipelines will send it to the controller due to table miss, |
Saurav Das | 7b1b488 | 2016-02-05 13:15:20 -0800 | [diff] [blame] | 79 | * other switches will flood the packets directly in the data plane without |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 80 | * packet in. |
| 81 | * We can deal with both cases. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 82 | * |
| 83 | * @param pkt incoming packet |
| 84 | */ |
| 85 | public void processPacketIn(InboundPacket pkt) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 86 | Ethernet ethernet = pkt.parsed(); |
| 87 | ARP arp = (ARP) ethernet.getPayload(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 88 | ConnectPoint connectPoint = pkt.receivedFrom(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 89 | DeviceId deviceId = connectPoint.deviceId(); |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 90 | |
| 91 | if (!validateArpSpa(connectPoint, arp)) { |
Charles Chan | ee891c5 | 2016-11-08 16:32:13 -0800 | [diff] [blame] | 92 | log.debug("Ignore ARP packet discovered on {} with unexpected src protocol address {}.", |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 93 | connectPoint, Ip4Address.valueOf(arp.getSenderProtocolAddress())); |
| 94 | return; |
| 95 | } |
| 96 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 97 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 98 | handleArpRequest(deviceId, connectPoint, ethernet); |
| 99 | } else { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 100 | handleArpReply(deviceId, connectPoint, ethernet); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | private void handleArpRequest(DeviceId deviceId, ConnectPoint inPort, Ethernet payload) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 105 | ARP arpRequest = (ARP) payload.getPayload(); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 106 | VlanId vlanId = VlanId.vlanId(payload.getVlanID()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 107 | HostId targetHostId = HostId.hostId(MacAddress.valueOf( |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 108 | arpRequest.getTargetHardwareAddress()), |
| 109 | vlanId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 110 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 111 | // ARP request for router. Send ARP reply. |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 112 | if (isArpForRouter(deviceId, arpRequest)) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | Ip4Address targetAddress = Ip4Address.valueOf(arpRequest.getTargetProtocolAddress()); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 114 | sendArpResponse(arpRequest, config.getRouterMacForAGatewayIp(targetAddress), vlanId); |
Charles Chan | bbc8d90 | 2015-10-15 10:48:13 -0700 | [diff] [blame] | 115 | } else { |
| 116 | Host targetHost = srManager.hostService.getHost(targetHostId); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 117 | // ARP request for known hosts. Send proxy ARP reply on behalf of the target. |
Charles Chan | bbc8d90 | 2015-10-15 10:48:13 -0700 | [diff] [blame] | 118 | if (targetHost != null) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 119 | removeVlanAndForward(payload, targetHost.location()); |
| 120 | // ARP request for unknown host in the subnet. Flood in the subnet. |
| 121 | } else { |
| 122 | removeVlanAndFlood(payload, inPort); |
| 123 | } |
| 124 | } |
| 125 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 126 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 127 | private void handleArpReply(DeviceId deviceId, ConnectPoint inPort, Ethernet payload) { |
| 128 | ARP arpReply = (ARP) payload.getPayload(); |
| 129 | VlanId vlanId = VlanId.vlanId(payload.getVlanID()); |
| 130 | HostId targetHostId = HostId.hostId(MacAddress.valueOf( |
| 131 | arpReply.getTargetHardwareAddress()), |
| 132 | vlanId); |
| 133 | |
| 134 | // ARP reply for router. Process all pending IP packets. |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 135 | if (isArpForRouter(deviceId, arpReply)) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 136 | Ip4Address hostIpAddress = Ip4Address.valueOf(arpReply.getSenderProtocolAddress()); |
| 137 | srManager.ipHandler.forwardPackets(deviceId, hostIpAddress); |
| 138 | } else { |
| 139 | Host targetHost = srManager.hostService.getHost(targetHostId); |
| 140 | // ARP reply for known hosts. Forward to the host. |
| 141 | if (targetHost != null) { |
| 142 | removeVlanAndForward(payload, targetHost.location()); |
| 143 | // ARP reply for unknown host, Flood in the subnet. |
| 144 | } else { |
| 145 | // Don't flood to non-edge ports |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 146 | if (vlanId.equals( |
| 147 | VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET))) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | removeVlanAndFlood(payload, inPort); |
Charles Chan | bbc8d90 | 2015-10-15 10:48:13 -0700 | [diff] [blame] | 151 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Check if the source protocol address of an ARP packet belongs to the same |
| 157 | * subnet configured on the port it is seen. |
| 158 | * |
| 159 | * @param connectPoint connect point where the ARP packet is seen |
| 160 | * @param arpPacket ARP packet |
| 161 | * @return true if the source protocol address belongs to the configured subnet |
| 162 | */ |
| 163 | private boolean validateArpSpa(ConnectPoint connectPoint, ARP arpPacket) { |
| 164 | Ip4Address spa = Ip4Address.valueOf(arpPacket.getSenderProtocolAddress()); |
| 165 | Ip4Prefix subnet = config.getPortSubnet(connectPoint.deviceId(), connectPoint.port()); |
Charles Chan | ee891c5 | 2016-11-08 16:32:13 -0800 | [diff] [blame] | 166 | return subnet != null && subnet.contains(spa); |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 167 | } |
| 168 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 169 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 170 | private boolean isArpForRouter(DeviceId deviceId, ARP arpMsg) { |
| 171 | Ip4Address targetProtocolAddress = Ip4Address.valueOf( |
| 172 | arpMsg.getTargetProtocolAddress()); |
| 173 | Set<Ip4Address> gatewayIpAddresses = null; |
| 174 | try { |
| 175 | if (targetProtocolAddress.equals(config.getRouterIp(deviceId))) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 176 | return true; |
| 177 | } |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 178 | gatewayIpAddresses = config.getPortIPs(deviceId); |
| 179 | } catch (DeviceConfigNotFoundException e) { |
| 180 | log.warn(e.getMessage() + " Aborting check for router IP in processing arp"); |
| 181 | } |
| 182 | if (gatewayIpAddresses != null && |
| 183 | gatewayIpAddresses.contains(targetProtocolAddress)) { |
| 184 | return true; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Sends an APR request for the target IP address to all ports except in-port. |
| 191 | * |
| 192 | * @param deviceId Switch device ID |
| 193 | * @param targetAddress target IP address for ARP |
| 194 | * @param inPort in-port |
| 195 | */ |
| 196 | public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 197 | byte[] senderMacAddress; |
| 198 | byte[] senderIpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 199 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 200 | try { |
| 201 | senderMacAddress = config.getDeviceMac(deviceId).toBytes(); |
| 202 | senderIpAddress = config.getRouterIp(deviceId).toOctets(); |
| 203 | } catch (DeviceConfigNotFoundException e) { |
| 204 | log.warn(e.getMessage() + " Aborting sendArpRequest."); |
| 205 | return; |
| 206 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 207 | |
| 208 | ARP arpRequest = new ARP(); |
| 209 | arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 210 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 211 | .setHardwareAddressLength( |
| 212 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 213 | .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH) |
| 214 | .setOpCode(ARP.OP_REQUEST) |
| 215 | .setSenderHardwareAddress(senderMacAddress) |
| 216 | .setTargetHardwareAddress(MacAddress.ZERO.toBytes()) |
| 217 | .setSenderProtocolAddress(senderIpAddress) |
| 218 | .setTargetProtocolAddress(targetAddress.toOctets()); |
| 219 | |
| 220 | Ethernet eth = new Ethernet(); |
| 221 | eth.setDestinationMACAddress(MacAddress.BROADCAST.toBytes()) |
| 222 | .setSourceMACAddress(senderMacAddress) |
| 223 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest); |
| 224 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 225 | removeVlanAndFlood(eth, inPort); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 228 | private void sendArpResponse(ARP arpRequest, MacAddress targetMac, VlanId vlanId) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 229 | ARP arpReply = new ARP(); |
| 230 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 231 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 232 | .setHardwareAddressLength( |
| 233 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 234 | .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH) |
| 235 | .setOpCode(ARP.OP_REPLY) |
| 236 | .setSenderHardwareAddress(targetMac.toBytes()) |
| 237 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 238 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 239 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 240 | |
| 241 | Ethernet eth = new Ethernet(); |
| 242 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
| 243 | .setSourceMACAddress(targetMac.toBytes()) |
| 244 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply); |
| 245 | |
Flavio Castro | 99c8dfb | 2016-01-14 14:53:33 -0800 | [diff] [blame] | 246 | MacAddress hostMac = MacAddress.valueOf(arpReply.getTargetHardwareAddress()); |
| 247 | HostId dstId = HostId.hostId(hostMac, vlanId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 248 | Host dst = srManager.hostService.getHost(dstId); |
| 249 | if (dst == null) { |
Flavio Castro | 99c8dfb | 2016-01-14 14:53:33 -0800 | [diff] [blame] | 250 | log.warn("Cannot send ARP response to host {}", dstId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
| 254 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 255 | setOutput(dst.location().port()).build(); |
| 256 | OutboundPacket packet = new DefaultOutboundPacket(dst.location().deviceId(), |
| 257 | treatment, ByteBuffer.wrap(eth.serialize())); |
| 258 | |
| 259 | srManager.packetService.emit(packet); |
| 260 | } |
| 261 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 262 | /** |
| 263 | * Remove VLAN tag and flood to all ports in the same subnet. |
| 264 | * |
| 265 | * @param packet packet to be flooded |
| 266 | * @param inPort where the packet comes from |
| 267 | */ |
| 268 | private void removeVlanAndFlood(Ethernet packet, ConnectPoint inPort) { |
| 269 | Ip4Address targetProtocolAddress = Ip4Address.valueOf( |
| 270 | ((ARP) packet.getPayload()).getTargetProtocolAddress() |
| 271 | ); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 272 | |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 273 | try { |
| 274 | srManager.deviceConfiguration |
| 275 | .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> { |
| 276 | if (subnet.contains(targetProtocolAddress)) { |
| 277 | ports.stream() |
| 278 | .filter(port -> port != inPort.port()) |
| 279 | .forEach(port -> { |
| 280 | removeVlanAndForward(packet, |
| 281 | new ConnectPoint(inPort.deviceId(), port)); |
| 282 | }); |
| 283 | } |
| 284 | }); |
| 285 | } catch (DeviceConfigNotFoundException e) { |
| 286 | log.warn(e.getMessage() |
| 287 | + " Cannot flood in subnet as device config not available" |
| 288 | + " for device: " + inPort.deviceId()); |
| 289 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 292 | /** |
| 293 | * Remove VLAN tag and packet out to given port. |
| 294 | * |
| 295 | * Note: In current implementation, we expect all communication with |
| 296 | * end hosts within a subnet to be untagged. |
| 297 | * <p> |
| 298 | * For those pipelines that internally assigns a VLAN, the VLAN tag will be |
| 299 | * removed before egress. |
| 300 | * <p> |
| 301 | * For those pipelines that do not assign internal VLAN, the packet remains |
| 302 | * untagged. |
| 303 | * |
| 304 | * @param packet packet to be forwarded |
| 305 | * @param outPort where the packet should be forwarded |
| 306 | */ |
| 307 | private void removeVlanAndForward(Ethernet packet, ConnectPoint outPort) { |
| 308 | packet.setEtherType(Ethernet.TYPE_ARP); |
| 309 | packet.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 310 | ByteBuffer buf = ByteBuffer.wrap(packet.serialize()); |
| 311 | |
| 312 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 313 | tbuilder.setOutput(outPort.port()); |
| 314 | srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
| 315 | tbuilder.build(), buf)); |
| 316 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 317 | } |