blob: 246a3bbbd56ff630022d5a5fe7e84929aec448d3 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-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 Ventreb6a7f342016-11-26 21:05:22 -080022import org.onlab.packet.IpPrefix;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.MacAddress;
Charles Chanf4586112015-11-09 16:37:23 -080024import org.onlab.packet.VlanId;
Pier Ventreb6a7f342016-11-26 21:05:22 -080025import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Host;
Pier Ventreb6a7f342016-11-26 21:05:22 -080029import org.onosproject.net.HostId;
sangho80f11cb2015-04-01 13:05:26 -070030import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficTreatment;
Pier Ventreb6a7f342016-11-26 21:05:22 -080032import org.onosproject.net.host.HostService;
sangho80f11cb2015-04-01 13:05:26 -070033import org.onosproject.net.packet.DefaultOutboundPacket;
Charles Chan319d1a22015-11-03 10:42:14 -080034import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
35import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chandebfea32016-10-24 14:52:01 -070036import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
sangho80f11cb2015-04-01 13:05:26 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import java.nio.ByteBuffer;
Saurav Dasc28b3432015-10-30 17:45:38 -070041import java.util.Set;
Pier Ventreb6a7f342016-11-26 21:05:22 -080042import java.util.stream.Collectors;
Saurav Dasc28b3432015-10-30 17:45:38 -070043
sangho80f11cb2015-04-01 13:05:26 -070044import static com.google.common.base.Preconditions.checkNotNull;
Pier Ventreb6a7f342016-11-26 21:05:22 -080045import static org.onosproject.incubator.net.neighbour.NeighbourMessageType.REQUEST;
sangho80f11cb2015-04-01 13:05:26 -070046
Charles Chanb7f75ac2016-01-11 18:28:54 -080047/**
48 * Handler of ARP packets that responses or forwards ARP packets that
49 * are sent to the controller.
50 */
sangho80f11cb2015-04-01 13:05:26 -070051public class ArpHandler {
52
53 private static Logger log = LoggerFactory.getLogger(ArpHandler.class);
54
55 private SegmentRoutingManager srManager;
sangho9b169e32015-04-14 16:27:13 -070056 private DeviceConfiguration config;
sangho80f11cb2015-04-01 13:05:26 -070057
58 /**
59 * Creates an ArpHandler object.
60 *
61 * @param srManager SegmentRoutingManager object
62 */
63 public ArpHandler(SegmentRoutingManager srManager) {
64 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070065 this.config = checkNotNull(srManager.deviceConfiguration);
sangho80f11cb2015-04-01 13:05:26 -070066 }
67
68 /**
69 * Processes incoming ARP packets.
Charles Chanf4586112015-11-09 16:37:23 -080070 *
sangho80f11cb2015-04-01 13:05:26 -070071 * If it is an ARP request to router itself or known hosts,
72 * then it sends ARP response.
73 * If it is an ARP request to unknown hosts in its own subnet,
74 * then it flood the ARP request to the ports.
75 * If it is an ARP response, then set a flow rule for the host
76 * and forward any IP packets to the host in the packet buffer to the host.
Charles Chanf4586112015-11-09 16:37:23 -080077 * <p>
78 * Note: We handles all ARP packet in, even for those ARP packets between
79 * hosts in the same subnet.
80 * For an ARP packet with broadcast destination MAC,
81 * some switches pipelines will send it to the controller due to table miss,
Saurav Das7b1b4882016-02-05 13:15:20 -080082 * other switches will flood the packets directly in the data plane without
Charles Chanf4586112015-11-09 16:37:23 -080083 * packet in.
84 * We can deal with both cases.
sangho80f11cb2015-04-01 13:05:26 -070085 *
Pier Ventreb6a7f342016-11-26 21:05:22 -080086 * @param pkt incoming ARP packet and context information
87 * @param hostService the host service
sangho80f11cb2015-04-01 13:05:26 -070088 */
Pier Ventreb6a7f342016-11-26 21:05:22 -080089 public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) {
Charles Chancbdc9be2016-10-17 18:03:37 -070090
Charles Chandebfea32016-10-24 14:52:01 -070091 SegmentRoutingAppConfig appConfig = srManager.cfgService
92 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Pier Ventreb6a7f342016-11-26 21:05:22 -080093 if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) {
Charles Chandebfea32016-10-24 14:52:01 -070094 // Ignore ARP packets come from suppressed ports
95 return;
96 }
97
Pier Ventreb6a7f342016-11-26 21:05:22 -080098 if (!validateArpSpa(pkt)) {
Charles Chanee891c52016-11-08 16:32:13 -080099 log.debug("Ignore ARP packet discovered on {} with unexpected src protocol address {}.",
Pier Ventreb6a7f342016-11-26 21:05:22 -0800100 pkt.inPort(), pkt.sender().getIp4Address());
Charles Chancbdc9be2016-10-17 18:03:37 -0700101 return;
102 }
103
Pier Ventreb6a7f342016-11-26 21:05:22 -0800104 if (pkt.type() == REQUEST) {
105 handleArpRequest(pkt, hostService);
sangho80f11cb2015-04-01 13:05:26 -0700106 } else {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800107 handleArpReply(pkt, hostService);
sangho80f11cb2015-04-01 13:05:26 -0700108 }
109 }
110
Pier Ventreb6a7f342016-11-26 21:05:22 -0800111 private void handleArpRequest(NeighbourMessageContext pkt, HostService hostService) {
Charles Chanf4586112015-11-09 16:37:23 -0800112 // ARP request for router. Send ARP reply.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800113 if (isArpForRouter(pkt)) {
114 MacAddress targetMac = config.getRouterMacForAGatewayIp(pkt.target().getIp4Address());
115 sendArpResponse(pkt, targetMac, hostService);
Charles Chanbbc8d902015-10-15 10:48:13 -0700116 } else {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800117 Set<Host> hosts = hostService.getHostsByIp(pkt.target());
118 if (hosts.size() > 1) {
119 log.warn("More than one host with the same ip {}", pkt.target());
120 }
121 Host targetHost = hosts.stream().findFirst().orElse(null);
Charles Chanf4586112015-11-09 16:37:23 -0800122 // ARP request for known hosts. Send proxy ARP reply on behalf of the target.
Charles Chanbbc8d902015-10-15 10:48:13 -0700123 if (targetHost != null) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800124 pkt.forward(targetHost.location());
Charles Chanf4586112015-11-09 16:37:23 -0800125 // ARP request for unknown host in the subnet. Flood in the subnet.
126 } else {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800127 flood(pkt);
Charles Chanf4586112015-11-09 16:37:23 -0800128 }
129 }
130 }
sangho80f11cb2015-04-01 13:05:26 -0700131
Pier Ventreb6a7f342016-11-26 21:05:22 -0800132 private void handleArpReply(NeighbourMessageContext pkt, HostService hostService) {
Charles Chanf4586112015-11-09 16:37:23 -0800133 // ARP reply for router. Process all pending IP packets.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800134 if (isArpForRouter(pkt)) {
135 Ip4Address hostIpAddress = pkt.sender().getIp4Address();
136 srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress);
Charles Chanf4586112015-11-09 16:37:23 -0800137 } else {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800138 HostId targetHostId = HostId.hostId(pkt.dstMac(), pkt.vlan());
139 Host targetHost = hostService.getHost(targetHostId);
Charles Chanf4586112015-11-09 16:37:23 -0800140 // ARP reply for known hosts. Forward to the host.
141 if (targetHost != null) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800142 pkt.forward(targetHost.location());
Charles Chanf4586112015-11-09 16:37:23 -0800143 // ARP reply for unknown host, Flood in the subnet.
144 } else {
145 // Don't flood to non-edge ports
Pier Ventreb6a7f342016-11-26 21:05:22 -0800146 if (pkt.vlan().equals(
Saurav Das2d94d312015-11-24 23:21:05 -0800147 VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET))) {
Charles Chanf4586112015-11-09 16:37:23 -0800148 return;
149 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800150 flood(pkt);
Charles Chanbbc8d902015-10-15 10:48:13 -0700151 }
sangho80f11cb2015-04-01 13:05:26 -0700152 }
153 }
154
Charles Chancbdc9be2016-10-17 18:03:37 -0700155 /**
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 *
Pier Ventreb6a7f342016-11-26 21:05:22 -0800159 * @param pkt ARP packet and context information
Charles Chancbdc9be2016-10-17 18:03:37 -0700160 * @return true if the source protocol address belongs to the configured subnet
161 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800162 private boolean validateArpSpa(NeighbourMessageContext pkt) {
163 Ip4Address spa = pkt.sender().getIp4Address();
164 Set<IpPrefix> subnet = config.getPortSubnets(pkt.inPort().deviceId(), pkt.inPort().port())
165 .stream()
166 .filter(ipPrefix -> ipPrefix.isIp4() && ipPrefix.contains(spa))
167 .collect(Collectors.toSet());
168 return !subnet.isEmpty();
Charles Chancbdc9be2016-10-17 18:03:37 -0700169 }
170
sangho80f11cb2015-04-01 13:05:26 -0700171
Pier Ventreb6a7f342016-11-26 21:05:22 -0800172 private boolean isArpForRouter(NeighbourMessageContext pkt) {
173 Ip4Address targetProtocolAddress = pkt.target().getIp4Address();
174 Set<IpAddress> gatewayIpAddresses = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800175 try {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800176 if (targetProtocolAddress.equals(config.getRouterIpv4(pkt.inPort().deviceId()))) {
sangho80f11cb2015-04-01 13:05:26 -0700177 return true;
178 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800179 gatewayIpAddresses = config.getPortIPs(pkt.inPort().deviceId());
Saurav Das2d94d312015-11-24 23:21:05 -0800180 } catch (DeviceConfigNotFoundException e) {
181 log.warn(e.getMessage() + " Aborting check for router IP in processing arp");
182 }
183 if (gatewayIpAddresses != null &&
184 gatewayIpAddresses.contains(targetProtocolAddress)) {
185 return true;
sangho80f11cb2015-04-01 13:05:26 -0700186 }
187 return false;
188 }
189
sangho80f11cb2015-04-01 13:05:26 -0700190 /**
191 * Sends an APR request for the target IP address to all ports except in-port.
192 *
193 * @param deviceId Switch device ID
194 * @param targetAddress target IP address for ARP
195 * @param inPort in-port
196 */
197 public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) {
Charles Chan319d1a22015-11-03 10:42:14 -0800198 byte[] senderMacAddress;
199 byte[] senderIpAddress;
sangho80f11cb2015-04-01 13:05:26 -0700200
Charles Chan319d1a22015-11-03 10:42:14 -0800201 try {
202 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventreb6a7f342016-11-26 21:05:22 -0800203 senderIpAddress = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address())
204 .toOctets();
Charles Chan319d1a22015-11-03 10:42:14 -0800205 } catch (DeviceConfigNotFoundException e) {
206 log.warn(e.getMessage() + " Aborting sendArpRequest.");
207 return;
208 }
sangho80f11cb2015-04-01 13:05:26 -0700209
210 ARP arpRequest = new ARP();
211 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
212 .setProtocolType(ARP.PROTO_TYPE_IP)
213 .setHardwareAddressLength(
214 (byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
215 .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH)
216 .setOpCode(ARP.OP_REQUEST)
217 .setSenderHardwareAddress(senderMacAddress)
218 .setTargetHardwareAddress(MacAddress.ZERO.toBytes())
219 .setSenderProtocolAddress(senderIpAddress)
220 .setTargetProtocolAddress(targetAddress.toOctets());
221
222 Ethernet eth = new Ethernet();
223 eth.setDestinationMACAddress(MacAddress.BROADCAST.toBytes())
224 .setSourceMACAddress(senderMacAddress)
225 .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest);
226
Pier Ventreb6a7f342016-11-26 21:05:22 -0800227 flood(eth, inPort);
sangho80f11cb2015-04-01 13:05:26 -0700228 }
229
Pier Ventreb6a7f342016-11-26 21:05:22 -0800230 private void sendArpResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService) {
231 HostId dstId = HostId.hostId(pkt.srcMac(), pkt.vlan());
232 Host dst = hostService.getHost(dstId);
sangho80f11cb2015-04-01 13:05:26 -0700233 if (dst == null) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800234 log.warn("Cannot send ARP response to host {} - does not exist in the store",
235 dstId);
sangho80f11cb2015-04-01 13:05:26 -0700236 return;
237 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800238 pkt.reply(targetMac);
sangho80f11cb2015-04-01 13:05:26 -0700239 }
240
Charles Chanf4586112015-11-09 16:37:23 -0800241 /**
242 * Remove VLAN tag and flood to all ports in the same subnet.
243 *
244 * @param packet packet to be flooded
245 * @param inPort where the packet comes from
246 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800247 private void flood(Ethernet packet, ConnectPoint inPort) {
Charles Chanf4586112015-11-09 16:37:23 -0800248 Ip4Address targetProtocolAddress = Ip4Address.valueOf(
249 ((ARP) packet.getPayload()).getTargetProtocolAddress()
250 );
sangho80f11cb2015-04-01 13:05:26 -0700251
Saurav Das52d4ed72016-03-28 19:00:18 -0700252 try {
253 srManager.deviceConfiguration
Pier Ventreb6a7f342016-11-26 21:05:22 -0800254 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
255 if (subnet.contains(targetProtocolAddress)) {
256 ports.stream()
257 .filter(port -> port != inPort.port())
258 .forEach(port -> {
259 forward(packet, new ConnectPoint(inPort.deviceId(), port));
260 });
261 }
262 });
263 } catch (DeviceConfigNotFoundException e) {
264 log.warn(e.getMessage()
265 + " Cannot flood in subnet as device config not available"
266 + " for device: " + inPort.deviceId());
267 }
268 }
269
270 /**
271 * Remove VLAN tag and flood to all ports in the same subnet.
272 *
273 * @param pkt arp packet to be flooded
274 */
275 private void flood(NeighbourMessageContext pkt) {
276 try {
277 srManager.deviceConfiguration
278 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
279 if (subnet.contains(pkt.target())) {
Saurav Das52d4ed72016-03-28 19:00:18 -0700280 ports.stream()
Pier Ventreb6a7f342016-11-26 21:05:22 -0800281 .filter(port -> port != pkt.inPort().port())
Saurav Das52d4ed72016-03-28 19:00:18 -0700282 .forEach(port -> {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800283 ConnectPoint outPoint = new ConnectPoint(
284 pkt.inPort().deviceId(),
285 port
286 );
287 pkt.forward(outPoint);
Saurav Das52d4ed72016-03-28 19:00:18 -0700288 });
289 }
290 });
291 } catch (DeviceConfigNotFoundException e) {
292 log.warn(e.getMessage()
293 + " Cannot flood in subnet as device config not available"
Pier Ventreb6a7f342016-11-26 21:05:22 -0800294 + " for device: " + pkt.inPort().deviceId());
Saurav Das52d4ed72016-03-28 19:00:18 -0700295 }
sangho80f11cb2015-04-01 13:05:26 -0700296 }
297
Charles Chanf4586112015-11-09 16:37:23 -0800298 /**
299 * Remove VLAN tag and packet out to given port.
300 *
301 * Note: In current implementation, we expect all communication with
302 * end hosts within a subnet to be untagged.
303 * <p>
304 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
305 * removed before egress.
306 * <p>
307 * For those pipelines that do not assign internal VLAN, the packet remains
308 * untagged.
309 *
310 * @param packet packet to be forwarded
311 * @param outPort where the packet should be forwarded
312 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800313 private void forward(Ethernet packet, ConnectPoint outPort) {
Charles Chanf4586112015-11-09 16:37:23 -0800314 packet.setEtherType(Ethernet.TYPE_ARP);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800315
Charles Chanf4586112015-11-09 16:37:23 -0800316 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
317
318 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
319 tbuilder.setOutput(outPort.port());
320 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
321 tbuilder.build(), buf));
322 }
sangho80f11cb2015-04-01 13:05:26 -0700323}