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