blob: 8f0b7d338ce3b601981387fb29bc98c408c0356c [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
18import org.onlab.packet.ARP;
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.IpAddress;
Pier Ventre10bd8d12016-11-26 21:05:22 -080022import org.onlab.packet.IpPrefix;
sanghob35a6192015-04-01 13:05:26 -070023import org.onlab.packet.MacAddress;
Charles Chan68aa62d2015-11-09 16:37:23 -080024import org.onlab.packet.VlanId;
Pier Ventre10bd8d12016-11-26 21:05:22 -080025import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
sanghob35a6192015-04-01 13:05:26 -070026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Host;
Pier Ventre10bd8d12016-11-26 21:05:22 -080029import org.onosproject.net.HostId;
Pier Ventre10bd8d12016-11-26 21:05:22 -080030import org.onosproject.net.host.HostService;
Charles Chan0b4e6182015-11-03 10:42:14 -080031import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Charles Chan03a73e02016-10-24 14:52:01 -070032import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
sanghob35a6192015-04-01 13:05:26 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Saurav Das837e0bb2015-10-30 17:45:38 -070036import java.util.Set;
Pier Ventre10bd8d12016-11-26 21:05:22 -080037import java.util.stream.Collectors;
Saurav Das837e0bb2015-10-30 17:45:38 -070038
Pier Ventre10bd8d12016-11-26 21:05:22 -080039import static org.onosproject.incubator.net.neighbour.NeighbourMessageType.REQUEST;
sanghob35a6192015-04-01 13:05:26 -070040
Charles Chane849c192016-01-11 18:28:54 -080041/**
42 * Handler of ARP packets that responses or forwards ARP packets that
43 * are sent to the controller.
44 */
Pier Ventre735b8c82016-12-02 08:16:05 -080045public class ArpHandler extends SegmentRoutingNeighbourHandler {
sanghob35a6192015-04-01 13:05:26 -070046
47 private static Logger log = LoggerFactory.getLogger(ArpHandler.class);
48
sanghob35a6192015-04-01 13:05:26 -070049 /**
50 * Creates an ArpHandler object.
51 *
52 * @param srManager SegmentRoutingManager object
53 */
54 public ArpHandler(SegmentRoutingManager srManager) {
Pier Ventre735b8c82016-12-02 08:16:05 -080055 super(srManager);
sanghob35a6192015-04-01 13:05:26 -070056 }
57
58 /**
59 * Processes incoming ARP packets.
Charles Chan68aa62d2015-11-09 16:37:23 -080060 *
sanghob35a6192015-04-01 13:05:26 -070061 * 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 Chan68aa62d2015-11-09 16:37:23 -080067 * <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 Das49831642016-02-05 13:15:20 -080072 * other switches will flood the packets directly in the data plane without
Charles Chan68aa62d2015-11-09 16:37:23 -080073 * packet in.
74 * We can deal with both cases.
sanghob35a6192015-04-01 13:05:26 -070075 *
Pier Ventre10bd8d12016-11-26 21:05:22 -080076 * @param pkt incoming ARP packet and context information
77 * @param hostService the host service
sanghob35a6192015-04-01 13:05:26 -070078 */
Pier Ventre10bd8d12016-11-26 21:05:22 -080079 public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) {
Charles Chand2edd472016-10-17 18:03:37 -070080
Charles Chan03a73e02016-10-24 14:52:01 -070081 SegmentRoutingAppConfig appConfig = srManager.cfgService
82 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Pier Ventre10bd8d12016-11-26 21:05:22 -080083 if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) {
Charles Chan03a73e02016-10-24 14:52:01 -070084 // Ignore ARP packets come from suppressed ports
Pier Ventref4b5fce2016-11-28 16:48:06 -080085 pkt.drop();
Charles Chan03a73e02016-10-24 14:52:01 -070086 return;
87 }
88
Pier Ventre10bd8d12016-11-26 21:05:22 -080089 if (!validateArpSpa(pkt)) {
Charles Chan505dfd82016-11-08 16:32:13 -080090 log.debug("Ignore ARP packet discovered on {} with unexpected src protocol address {}.",
Pier Ventre10bd8d12016-11-26 21:05:22 -080091 pkt.inPort(), pkt.sender().getIp4Address());
Pier Ventref4b5fce2016-11-28 16:48:06 -080092 pkt.drop();
Charles Chand2edd472016-10-17 18:03:37 -070093 return;
94 }
95
Pier Ventre10bd8d12016-11-26 21:05:22 -080096 if (pkt.type() == REQUEST) {
97 handleArpRequest(pkt, hostService);
sanghob35a6192015-04-01 13:05:26 -070098 } else {
Pier Ventre10bd8d12016-11-26 21:05:22 -080099 handleArpReply(pkt, hostService);
sanghob35a6192015-04-01 13:05:26 -0700100 }
101 }
102
Pier Ventre10bd8d12016-11-26 21:05:22 -0800103 private void handleArpRequest(NeighbourMessageContext pkt, HostService hostService) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800104 // ARP request for router. Send ARP reply.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800105 if (isArpForRouter(pkt)) {
106 MacAddress targetMac = config.getRouterMacForAGatewayIp(pkt.target().getIp4Address());
Pier Ventre735b8c82016-12-02 08:16:05 -0800107 sendResponse(pkt, targetMac, hostService);
Charles Chaneb088e62015-10-15 10:48:13 -0700108 } else {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800109 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 Chan68aa62d2015-11-09 16:37:23 -0800114 // ARP request for known hosts. Send proxy ARP reply on behalf of the target.
Charles Chaneb088e62015-10-15 10:48:13 -0700115 if (targetHost != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800116 pkt.forward(targetHost.location());
Charles Chan68aa62d2015-11-09 16:37:23 -0800117 // ARP request for unknown host in the subnet. Flood in the subnet.
118 } else {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800119 flood(pkt);
Charles Chan68aa62d2015-11-09 16:37:23 -0800120 }
121 }
122 }
sanghob35a6192015-04-01 13:05:26 -0700123
Pier Ventre10bd8d12016-11-26 21:05:22 -0800124 private void handleArpReply(NeighbourMessageContext pkt, HostService hostService) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800125 // ARP reply for router. Process all pending IP packets.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800126 if (isArpForRouter(pkt)) {
127 Ip4Address hostIpAddress = pkt.sender().getIp4Address();
128 srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress);
Charles Chan68aa62d2015-11-09 16:37:23 -0800129 } else {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800130 HostId targetHostId = HostId.hostId(pkt.dstMac(), pkt.vlan());
131 Host targetHost = hostService.getHost(targetHostId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800132 // ARP reply for known hosts. Forward to the host.
133 if (targetHost != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800134 pkt.forward(targetHost.location());
Charles Chan68aa62d2015-11-09 16:37:23 -0800135 // ARP reply for unknown host, Flood in the subnet.
136 } else {
137 // Don't flood to non-edge ports
Charles Chan59cc16d2017-02-02 16:20:42 -0800138 if (pkt.vlan().equals(SegmentRoutingManager.INTERNAL_VLAN)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800139 return;
140 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800141 flood(pkt);
Charles Chaneb088e62015-10-15 10:48:13 -0700142 }
sanghob35a6192015-04-01 13:05:26 -0700143 }
144 }
145
Charles Chand2edd472016-10-17 18:03:37 -0700146 /**
147 * Check if the source protocol address of an ARP packet belongs to the same
148 * subnet configured on the port it is seen.
149 *
Pier Ventre10bd8d12016-11-26 21:05:22 -0800150 * @param pkt ARP packet and context information
Charles Chand2edd472016-10-17 18:03:37 -0700151 * @return true if the source protocol address belongs to the configured subnet
152 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800153 private boolean validateArpSpa(NeighbourMessageContext pkt) {
154 Ip4Address spa = pkt.sender().getIp4Address();
155 Set<IpPrefix> subnet = config.getPortSubnets(pkt.inPort().deviceId(), pkt.inPort().port())
156 .stream()
157 .filter(ipPrefix -> ipPrefix.isIp4() && ipPrefix.contains(spa))
158 .collect(Collectors.toSet());
159 return !subnet.isEmpty();
Charles Chand2edd472016-10-17 18:03:37 -0700160 }
161
sanghob35a6192015-04-01 13:05:26 -0700162
Pier Ventre10bd8d12016-11-26 21:05:22 -0800163 private boolean isArpForRouter(NeighbourMessageContext pkt) {
164 Ip4Address targetProtocolAddress = pkt.target().getIp4Address();
165 Set<IpAddress> gatewayIpAddresses = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800166 try {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800167 if (targetProtocolAddress.equals(config.getRouterIpv4(pkt.inPort().deviceId()))) {
sanghob35a6192015-04-01 13:05:26 -0700168 return true;
169 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800170 gatewayIpAddresses = config.getPortIPs(pkt.inPort().deviceId());
Saurav Das4ce45962015-11-24 23:21:05 -0800171 } catch (DeviceConfigNotFoundException e) {
172 log.warn(e.getMessage() + " Aborting check for router IP in processing arp");
173 }
174 if (gatewayIpAddresses != null &&
175 gatewayIpAddresses.contains(targetProtocolAddress)) {
176 return true;
sanghob35a6192015-04-01 13:05:26 -0700177 }
178 return false;
179 }
180
sanghob35a6192015-04-01 13:05:26 -0700181 /**
182 * Sends an APR request for the target IP address to all ports except in-port.
183 *
184 * @param deviceId Switch device ID
185 * @param targetAddress target IP address for ARP
186 * @param inPort in-port
187 */
188 public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800189 byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH];
190 byte[] senderIpAddress = new byte[Ip4Address.BYTE_LENGTH];
191 /*
192 * Retrieves device info.
193 */
Pier Luigia905c0c2017-01-29 12:38:48 -0800194 if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) {
195 log.warn("Aborting sendArpRequest, we cannot get all the information needed");
196 return;
197 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800198 /*
199 * Creates the request.
200 */
201 Ethernet arpRequest = ARP.buildArpRequest(
202 senderMacAddress,
203 senderIpAddress,
204 targetAddress.toOctets(),
205 VlanId.NO_VID
Charles Chan68aa62d2015-11-09 16:37:23 -0800206 );
Pier Ventre735b8c82016-12-02 08:16:05 -0800207 flood(arpRequest, inPort, targetAddress);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800208 }
209
sanghob35a6192015-04-01 13:05:26 -0700210}