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; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
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; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 25 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.Host; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 29 | import org.onosproject.net.HostId; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 30 | import org.onosproject.net.host.HostService; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 31 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 32 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 36 | import java.util.Set; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 38 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 39 | import static org.onosproject.incubator.net.neighbour.NeighbourMessageType.REQUEST; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 40 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Handler of ARP packets that responses or forwards ARP packets that |
| 43 | * are sent to the controller. |
| 44 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame^] | 45 | public class ArpHandler extends SegmentRoutingNeighbourHandler { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 46 | |
| 47 | private static Logger log = LoggerFactory.getLogger(ArpHandler.class); |
| 48 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Creates an ArpHandler object. |
| 51 | * |
| 52 | * @param srManager SegmentRoutingManager object |
| 53 | */ |
| 54 | public ArpHandler(SegmentRoutingManager srManager) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame^] | 55 | super(srManager); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Processes incoming ARP packets. |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 60 | * |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 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. |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 67 | * <p> |
| 68 | * Note: We handles all ARP packet in, even for those ARP packets between |
| 69 | * hosts in the same subnet. |
| 70 | * For an ARP packet with broadcast destination MAC, |
| 71 | * 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] | 72 | * other switches will flood the packets directly in the data plane without |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 73 | * packet in. |
| 74 | * We can deal with both cases. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 75 | * |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 76 | * @param pkt incoming ARP packet and context information |
| 77 | * @param hostService the host service |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 78 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 79 | public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 80 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 81 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 82 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 83 | if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) { |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 84 | // Ignore ARP packets come from suppressed ports |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 85 | pkt.drop(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 89 | if (!validateArpSpa(pkt)) { |
Charles Chan | ee891c5 | 2016-11-08 16:32:13 -0800 | [diff] [blame] | 90 | log.debug("Ignore ARP packet discovered on {} with unexpected src protocol address {}.", |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 91 | pkt.inPort(), pkt.sender().getIp4Address()); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 92 | pkt.drop(); |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 96 | if (pkt.type() == REQUEST) { |
| 97 | handleArpRequest(pkt, hostService); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 98 | } else { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 99 | handleArpReply(pkt, hostService); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 103 | private void handleArpRequest(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 104 | // ARP request for router. Send ARP reply. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 105 | if (isArpForRouter(pkt)) { |
| 106 | MacAddress targetMac = config.getRouterMacForAGatewayIp(pkt.target().getIp4Address()); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame^] | 107 | sendResponse(pkt, targetMac, hostService); |
Charles Chan | bbc8d90 | 2015-10-15 10:48:13 -0700 | [diff] [blame] | 108 | } else { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 109 | Set<Host> hosts = hostService.getHostsByIp(pkt.target()); |
| 110 | if (hosts.size() > 1) { |
| 111 | log.warn("More than one host with the same ip {}", pkt.target()); |
| 112 | } |
| 113 | Host targetHost = hosts.stream().findFirst().orElse(null); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 114 | // 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] | 115 | if (targetHost != null) { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 116 | pkt.forward(targetHost.location()); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 117 | // ARP request for unknown host in the subnet. Flood in the subnet. |
| 118 | } else { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 119 | flood(pkt); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 123 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 124 | private void handleArpReply(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 125 | // ARP reply for router. Process all pending IP packets. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 126 | if (isArpForRouter(pkt)) { |
| 127 | Ip4Address hostIpAddress = pkt.sender().getIp4Address(); |
| 128 | srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 129 | } else { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 130 | HostId targetHostId = HostId.hostId(pkt.dstMac(), pkt.vlan()); |
| 131 | Host targetHost = hostService.getHost(targetHostId); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 132 | // ARP reply for known hosts. Forward to the host. |
| 133 | if (targetHost != null) { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 134 | pkt.forward(targetHost.location()); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 135 | // ARP reply for unknown host, Flood in the subnet. |
| 136 | } else { |
| 137 | // Don't flood to non-edge ports |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 138 | if (pkt.vlan().equals( |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 139 | VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET))) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 140 | return; |
| 141 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 142 | flood(pkt); |
Charles Chan | bbc8d90 | 2015-10-15 10:48:13 -0700 | [diff] [blame] | 143 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Check if the source protocol address of an ARP packet belongs to the same |
| 149 | * subnet configured on the port it is seen. |
| 150 | * |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 151 | * @param pkt ARP packet and context information |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 152 | * @return true if the source protocol address belongs to the configured subnet |
| 153 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 154 | private boolean validateArpSpa(NeighbourMessageContext pkt) { |
| 155 | Ip4Address spa = pkt.sender().getIp4Address(); |
| 156 | Set<IpPrefix> subnet = config.getPortSubnets(pkt.inPort().deviceId(), pkt.inPort().port()) |
| 157 | .stream() |
| 158 | .filter(ipPrefix -> ipPrefix.isIp4() && ipPrefix.contains(spa)) |
| 159 | .collect(Collectors.toSet()); |
| 160 | return !subnet.isEmpty(); |
Charles Chan | cbdc9be | 2016-10-17 18:03:37 -0700 | [diff] [blame] | 161 | } |
| 162 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 163 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 164 | private boolean isArpForRouter(NeighbourMessageContext pkt) { |
| 165 | Ip4Address targetProtocolAddress = pkt.target().getIp4Address(); |
| 166 | Set<IpAddress> gatewayIpAddresses = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 167 | try { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 168 | if (targetProtocolAddress.equals(config.getRouterIpv4(pkt.inPort().deviceId()))) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 171 | gatewayIpAddresses = config.getPortIPs(pkt.inPort().deviceId()); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 172 | } catch (DeviceConfigNotFoundException e) { |
| 173 | log.warn(e.getMessage() + " Aborting check for router IP in processing arp"); |
| 174 | } |
| 175 | if (gatewayIpAddresses != null && |
| 176 | gatewayIpAddresses.contains(targetProtocolAddress)) { |
| 177 | return true; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Sends an APR request for the target IP address to all ports except in-port. |
| 184 | * |
| 185 | * @param deviceId Switch device ID |
| 186 | * @param targetAddress target IP address for ARP |
| 187 | * @param inPort in-port |
| 188 | */ |
| 189 | public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame^] | 190 | byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 191 | byte[] senderIpAddress = new byte[Ip4Address.BYTE_LENGTH]; |
| 192 | /* |
| 193 | * Retrieves device info. |
| 194 | */ |
| 195 | getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress); |
| 196 | /* |
| 197 | * Creates the request. |
| 198 | */ |
| 199 | Ethernet arpRequest = ARP.buildArpRequest( |
| 200 | senderMacAddress, |
| 201 | senderIpAddress, |
| 202 | targetAddress.toOctets(), |
| 203 | VlanId.NO_VID |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 204 | ); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame^] | 205 | flood(arpRequest, inPort, targetAddress); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 206 | } |
| 207 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 208 | } |