Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.proxyarp.impl; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.easymock.EasyMock.anyObject; |
| 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; |
| 21 | import static org.easymock.EasyMock.replay; |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 22 | import static org.junit.Assert.assertArrayEquals; |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.ArrayList; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 31 | import java.util.Set; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 35 | import org.onlab.packet.ARP; |
| 36 | import org.onlab.packet.Ethernet; |
| 37 | import org.onlab.packet.Ip4Address; |
| 38 | import org.onlab.packet.Ip4Prefix; |
| 39 | import org.onlab.packet.MacAddress; |
| 40 | import org.onlab.packet.VlanId; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.DefaultHost; |
| 44 | import org.onosproject.net.Device; |
| 45 | import org.onosproject.net.DeviceId; |
| 46 | import org.onosproject.net.Host; |
| 47 | import org.onosproject.net.HostId; |
| 48 | import org.onosproject.net.HostLocation; |
| 49 | import org.onosproject.net.Link; |
| 50 | import org.onosproject.net.Port; |
| 51 | import org.onosproject.net.PortNumber; |
| 52 | import org.onosproject.net.device.DeviceListener; |
| 53 | import org.onosproject.net.device.DeviceService; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.FlowRule; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flow.instructions.Instruction; |
| 57 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 58 | import org.onosproject.net.host.HostService; |
| 59 | import org.onosproject.net.host.InterfaceIpAddress; |
| 60 | import org.onosproject.net.host.PortAddresses; |
| 61 | import org.onosproject.net.link.LinkListener; |
| 62 | import org.onosproject.net.link.LinkService; |
| 63 | import org.onosproject.net.packet.OutboundPacket; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 64 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 65 | import org.onosproject.net.packet.PacketProcessor; |
| 66 | import org.onosproject.net.packet.PacketService; |
| 67 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 68 | |
| 69 | import com.google.common.collect.Sets; |
| 70 | |
| 71 | /** |
| 72 | * Tests for the {@link ProxyArpManager} class. |
| 73 | */ |
| 74 | public class ProxyArpManagerTest { |
| 75 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 76 | private static final int NUM_DEVICES = 6; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 77 | private static final int NUM_PORTS_PER_DEVICE = 3; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 78 | private static final int NUM_ADDRESS_PORTS = NUM_DEVICES / 2; |
| 79 | private static final int NUM_FLOOD_PORTS = 3; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 80 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 81 | private static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1"); |
| 82 | private static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2"); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 83 | |
| 84 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 85 | |
| 86 | private static final VlanId VLAN1 = VlanId.vlanId((short) 1); |
| 87 | private static final VlanId VLAN2 = VlanId.vlanId((short) 2); |
| 88 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:11:00:00:01"); |
| 89 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:22:00:00:02"); |
| 90 | private static final HostId HID1 = HostId.hostId(MAC1, VLAN1); |
| 91 | private static final HostId HID2 = HostId.hostId(MAC2, VLAN1); |
| 92 | |
| 93 | private static final DeviceId DID1 = getDeviceId(1); |
| 94 | private static final DeviceId DID2 = getDeviceId(2); |
| 95 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 96 | private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L); |
| 97 | private static final HostLocation LOC2 = new HostLocation(DID2, P1, 123L); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 98 | private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes(); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 99 | |
| 100 | private ProxyArpManager proxyArp; |
| 101 | |
| 102 | private TestPacketService packetService; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 103 | private DeviceService deviceService; |
| 104 | private LinkService linkService; |
| 105 | private HostService hostService; |
| 106 | |
| 107 | @Before |
| 108 | public void setUp() throws Exception { |
| 109 | proxyArp = new ProxyArpManager(); |
| 110 | packetService = new TestPacketService(); |
| 111 | proxyArp.packetService = packetService; |
| 112 | |
| 113 | // Create a host service mock here. Must be replayed by tests once the |
| 114 | // expectations have been set up |
| 115 | hostService = createMock(HostService.class); |
| 116 | proxyArp.hostService = hostService; |
| 117 | |
| 118 | createTopology(); |
| 119 | proxyArp.deviceService = deviceService; |
| 120 | proxyArp.linkService = linkService; |
| 121 | |
| 122 | proxyArp.activate(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Creates a fake topology to feed into the ARP module. |
| 127 | * <p/> |
| 128 | * The default topology is a unidirectional ring topology. Each switch has |
| 129 | * 3 ports. Ports 2 and 3 have the links to neighbor switches, and port 1 |
| 130 | * is free (edge port). |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 131 | * The first half of the switches have IP addresses configured on their |
| 132 | * free ports (port 1). The second half of the switches have no IP |
| 133 | * addresses configured. |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 134 | */ |
| 135 | private void createTopology() { |
| 136 | deviceService = createMock(DeviceService.class); |
| 137 | linkService = createMock(LinkService.class); |
| 138 | |
| 139 | deviceService.addListener(anyObject(DeviceListener.class)); |
| 140 | linkService.addListener(anyObject(LinkListener.class)); |
| 141 | |
| 142 | createDevices(NUM_DEVICES, NUM_PORTS_PER_DEVICE); |
| 143 | createLinks(NUM_DEVICES); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 144 | addAddressBindings(); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Creates the devices for the fake topology. |
| 149 | */ |
| 150 | private void createDevices(int numDevices, int numPorts) { |
| 151 | List<Device> devices = new ArrayList<>(); |
| 152 | |
| 153 | for (int i = 1; i <= numDevices; i++) { |
| 154 | DeviceId devId = getDeviceId(i); |
| 155 | Device device = createMock(Device.class); |
| 156 | expect(device.id()).andReturn(devId).anyTimes(); |
| 157 | replay(device); |
| 158 | |
| 159 | devices.add(device); |
| 160 | |
| 161 | List<Port> ports = new ArrayList<>(); |
| 162 | for (int j = 1; j <= numPorts; j++) { |
| 163 | Port port = createMock(Port.class); |
| 164 | expect(port.number()).andReturn(PortNumber.portNumber(j)).anyTimes(); |
| 165 | replay(port); |
| 166 | ports.add(port); |
| 167 | } |
| 168 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 169 | expect(deviceService.getPorts(devId)).andReturn(ports).anyTimes(); |
| 170 | expect(deviceService.getDevice(devId)).andReturn(device).anyTimes(); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 173 | expect(deviceService.getDevices()).andReturn(devices).anyTimes(); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 174 | replay(deviceService); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Creates the links for the fake topology. |
| 179 | * NB: Only unidirectional links are created, as for this purpose all we |
| 180 | * need is to occupy the ports with some link. |
| 181 | */ |
| 182 | private void createLinks(int numDevices) { |
| 183 | List<Link> links = new ArrayList<Link>(); |
| 184 | |
| 185 | for (int i = 1; i <= numDevices; i++) { |
| 186 | ConnectPoint src = new ConnectPoint( |
| 187 | getDeviceId(i), |
| 188 | PortNumber.portNumber(2)); |
| 189 | ConnectPoint dst = new ConnectPoint( |
| 190 | getDeviceId((i + 1 > numDevices) ? 1 : i + 1), |
| 191 | PortNumber.portNumber(3)); |
| 192 | |
| 193 | Link link = createMock(Link.class); |
| 194 | expect(link.src()).andReturn(src).anyTimes(); |
| 195 | expect(link.dst()).andReturn(dst).anyTimes(); |
| 196 | replay(link); |
| 197 | |
| 198 | links.add(link); |
| 199 | } |
| 200 | |
| 201 | expect(linkService.getLinks()).andReturn(links).anyTimes(); |
| 202 | replay(linkService); |
| 203 | } |
| 204 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 205 | private void addAddressBindings() { |
| 206 | Set<PortAddresses> addresses = Sets.newHashSet(); |
| 207 | |
| 208 | for (int i = 1; i <= NUM_ADDRESS_PORTS; i++) { |
| 209 | ConnectPoint cp = new ConnectPoint(getDeviceId(i), P1); |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 210 | Ip4Prefix prefix1 = |
| 211 | Ip4Prefix.valueOf("10.0." + (2 * i - 1) + ".0/24"); |
| 212 | Ip4Address addr1 = |
| 213 | Ip4Address.valueOf("10.0." + (2 * i - 1) + ".1"); |
| 214 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("10.0." + (2 * i) + ".0/24"); |
| 215 | Ip4Address addr2 = Ip4Address.valueOf("10.0." + (2 * i) + ".1"); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 216 | InterfaceIpAddress ia1 = new InterfaceIpAddress(addr1, prefix1); |
| 217 | InterfaceIpAddress ia2 = new InterfaceIpAddress(addr2, prefix2); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 218 | PortAddresses pa1 = |
| 219 | new PortAddresses(cp, Sets.newHashSet(ia1), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 220 | MacAddress.valueOf(2 * i - 1), |
| 221 | VlanId.vlanId((short) 1)); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 222 | PortAddresses pa2 = |
| 223 | new PortAddresses(cp, Sets.newHashSet(ia2), |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 224 | MacAddress.valueOf(2 * i), |
| 225 | VlanId.NONE); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 226 | |
| 227 | addresses.add(pa1); |
| 228 | addresses.add(pa2); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 229 | |
| 230 | expect(hostService.getAddressBindingsForPort(cp)) |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 231 | .andReturn(Sets.newHashSet(pa1, pa2)).anyTimes(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | expect(hostService.getAddressBindings()).andReturn(addresses).anyTimes(); |
| 235 | |
| 236 | for (int i = 1; i <= NUM_FLOOD_PORTS; i++) { |
| 237 | ConnectPoint cp = new ConnectPoint(getDeviceId(i + NUM_ADDRESS_PORTS), |
| 238 | P1); |
| 239 | expect(hostService.getAddressBindingsForPort(cp)) |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 240 | .andReturn(Collections.<PortAddresses>emptySet()).anyTimes(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 244 | /** |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame^] | 245 | * Tests {@link ProxyArpManager#isKnown(org.onlab.packet.IpAddress)} in the |
| 246 | * case where the IP address is not known. |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 247 | * Verifies the method returns false. |
| 248 | */ |
| 249 | @Test |
| 250 | public void testNotKnown() { |
| 251 | expect(hostService.getHostsByIp(IP1)).andReturn(Collections.<Host>emptySet()); |
| 252 | replay(hostService); |
| 253 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 254 | assertFalse(proxyArp.isKnown(IP1)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame^] | 258 | * Tests {@link ProxyArpManager#isKnown(org.onlab.packet.IpAddress)} in the |
| 259 | * case where the IP address is known. |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 260 | * Verifies the method returns true. |
| 261 | */ |
| 262 | @Test |
| 263 | public void testKnown() { |
| 264 | Host host1 = createMock(Host.class); |
| 265 | Host host2 = createMock(Host.class); |
| 266 | |
| 267 | expect(hostService.getHostsByIp(IP1)) |
| 268 | .andReturn(Sets.newHashSet(host1, host2)); |
| 269 | replay(hostService); |
| 270 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 271 | assertTrue(proxyArp.isKnown(IP1)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 275 | * Tests {@link ProxyArpManager#reply(Ethernet, ConnectPoint)} in the case where the |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 276 | * destination host is known. |
| 277 | * Verifies the correct ARP reply is sent out the correct port. |
| 278 | */ |
| 279 | @Test |
| 280 | public void testReplyKnown() { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 281 | Host replyer = new DefaultHost(PID, HID1, MAC1, VLAN1, getLocation(4), |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 282 | Collections.singleton(IP1)); |
| 283 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 284 | Host requestor = new DefaultHost(PID, HID2, MAC2, VLAN1, getLocation(5), |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 285 | Collections.singleton(IP2)); |
| 286 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 287 | expect(hostService.getHostsByIp(IP1)) |
| 288 | .andReturn(Collections.singleton(replyer)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 289 | expect(hostService.getHost(HID2)).andReturn(requestor); |
| 290 | |
| 291 | replay(hostService); |
| 292 | |
| 293 | Ethernet arpRequest = buildArp(ARP.OP_REQUEST, MAC2, null, IP2, IP1); |
| 294 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 295 | proxyArp.reply(arpRequest, getLocation(5)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 296 | |
| 297 | assertEquals(1, packetService.packets.size()); |
| 298 | Ethernet arpReply = buildArp(ARP.OP_REPLY, MAC1, MAC2, IP1, IP2); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 299 | verifyPacketOut(arpReply, getLocation(5), packetService.packets.get(0)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 303 | * Tests {@link ProxyArpManager#reply(Ethernet, ConnectPoint)} in the case where the |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 304 | * destination host is not known. |
| 305 | * Verifies the ARP request is flooded out the correct edge ports. |
| 306 | */ |
| 307 | @Test |
| 308 | public void testReplyUnknown() { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 309 | Host requestor = new DefaultHost(PID, HID2, MAC2, VLAN1, getLocation(5), |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 310 | Collections.singleton(IP2)); |
| 311 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 312 | expect(hostService.getHostsByIp(IP1)) |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 313 | .andReturn(Collections.<Host>emptySet()); |
| 314 | expect(hostService.getHost(HID2)).andReturn(requestor); |
| 315 | |
| 316 | replay(hostService); |
| 317 | |
| 318 | Ethernet arpRequest = buildArp(ARP.OP_REQUEST, MAC2, null, IP2, IP1); |
| 319 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 320 | proxyArp.reply(arpRequest, getLocation(6)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 321 | |
| 322 | verifyFlood(arpRequest); |
| 323 | } |
| 324 | |
| 325 | /** |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 326 | * Tests {@link ProxyArpManager#reply(Ethernet, ConnectPoint)} in the case where the |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 327 | * destination host is known for that IP address, but is not on the same |
| 328 | * VLAN as the source host. |
| 329 | * Verifies the ARP request is flooded out the correct edge ports. |
| 330 | */ |
| 331 | @Test |
| 332 | public void testReplyDifferentVlan() { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 333 | Host replyer = new DefaultHost(PID, HID1, MAC1, VLAN2, getLocation(4), |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 334 | Collections.singleton(IP1)); |
| 335 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 336 | Host requestor = new DefaultHost(PID, HID2, MAC2, VLAN1, getLocation(5), |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 337 | Collections.singleton(IP2)); |
| 338 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 339 | expect(hostService.getHostsByIp(IP1)) |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 340 | .andReturn(Collections.singleton(replyer)); |
| 341 | expect(hostService.getHost(HID2)).andReturn(requestor); |
| 342 | |
| 343 | replay(hostService); |
| 344 | |
| 345 | Ethernet arpRequest = buildArp(ARP.OP_REQUEST, MAC2, null, IP2, IP1); |
| 346 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 347 | proxyArp.reply(arpRequest, getLocation(6)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 348 | |
| 349 | verifyFlood(arpRequest); |
| 350 | } |
| 351 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 352 | @Test |
| 353 | public void testReplyToRequestForUs() { |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 354 | Ip4Address theirIp = Ip4Address.valueOf("10.0.1.254"); |
| 355 | Ip4Address ourFirstIp = Ip4Address.valueOf("10.0.1.1"); |
| 356 | Ip4Address ourSecondIp = Ip4Address.valueOf("10.0.2.1"); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 357 | MacAddress firstMac = MacAddress.valueOf(1L); |
| 358 | MacAddress secondMac = MacAddress.valueOf(2L); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 359 | |
| 360 | Host requestor = new DefaultHost(PID, HID2, MAC2, VLAN1, LOC1, |
| 361 | Collections.singleton(theirIp)); |
| 362 | |
| 363 | expect(hostService.getHost(HID2)).andReturn(requestor); |
| 364 | replay(hostService); |
| 365 | |
| 366 | Ethernet arpRequest = buildArp(ARP.OP_REQUEST, MAC2, null, theirIp, ourFirstIp); |
| 367 | |
| 368 | proxyArp.reply(arpRequest, LOC1); |
| 369 | |
| 370 | assertEquals(1, packetService.packets.size()); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 371 | Ethernet arpReply = buildArp(ARP.OP_REPLY, firstMac, MAC2, ourFirstIp, theirIp); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 372 | verifyPacketOut(arpReply, LOC1, packetService.packets.get(0)); |
| 373 | |
| 374 | // Test a request for the second address on that port |
| 375 | packetService.packets.clear(); |
| 376 | arpRequest = buildArp(ARP.OP_REQUEST, MAC2, null, theirIp, ourSecondIp); |
| 377 | |
| 378 | proxyArp.reply(arpRequest, LOC1); |
| 379 | |
| 380 | assertEquals(1, packetService.packets.size()); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 381 | arpReply = buildArp(ARP.OP_REPLY, secondMac, MAC2, ourSecondIp, theirIp); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 382 | verifyPacketOut(arpReply, LOC1, packetService.packets.get(0)); |
| 383 | } |
| 384 | |
| 385 | @Test |
| 386 | public void testReplyExternalPortBadRequest() { |
| 387 | replay(hostService); // no further host service expectations |
| 388 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 389 | Ip4Address theirIp = Ip4Address.valueOf("10.0.1.254"); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 390 | |
| 391 | // Request for a valid external IP address but coming in the wrong port |
| 392 | Ethernet arpRequest = buildArp(ARP.OP_REQUEST, MAC1, null, theirIp, |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 393 | Ip4Address.valueOf("10.0.3.1")); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 394 | proxyArp.reply(arpRequest, LOC1); |
| 395 | assertEquals(0, packetService.packets.size()); |
| 396 | |
| 397 | // Request for a valid internal IP address but coming in an external port |
| 398 | packetService.packets.clear(); |
| 399 | arpRequest = buildArp(ARP.OP_REQUEST, MAC1, null, theirIp, IP1); |
| 400 | proxyArp.reply(arpRequest, LOC1); |
| 401 | assertEquals(0, packetService.packets.size()); |
| 402 | } |
| 403 | |
| 404 | @Test |
| 405 | public void testReplyToRequestFromUs() { |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 406 | Ip4Address ourIp = Ip4Address.valueOf("10.0.1.1"); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 407 | MacAddress ourMac = MacAddress.valueOf(1L); |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 408 | Ip4Address theirIp = Ip4Address.valueOf("10.0.1.100"); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 409 | |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame^] | 410 | expect(hostService.getHostsByIp(theirIp)).andReturn(Collections.emptySet()); |
| 411 | expect(hostService.getHost(HostId.hostId(ourMac, VLAN1))).andReturn(null); |
| 412 | replay(hostService); |
| 413 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 414 | // This is a request from something inside our network (like a BGP |
| 415 | // daemon) to an external host. |
| 416 | Ethernet arpRequest = buildArp(ARP.OP_REQUEST, ourMac, null, ourIp, theirIp); |
| 417 | proxyArp.reply(arpRequest, getLocation(5)); |
| 418 | |
| 419 | assertEquals(1, packetService.packets.size()); |
| 420 | verifyPacketOut(arpRequest, getLocation(1), packetService.packets.get(0)); |
| 421 | |
| 422 | // The same request from a random external port should fail |
| 423 | packetService.packets.clear(); |
| 424 | proxyArp.reply(arpRequest, getLocation(2)); |
| 425 | assertEquals(0, packetService.packets.size()); |
| 426 | } |
| 427 | |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 428 | /** |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 429 | * Tests {@link ProxyArpManager#forward(Ethernet, ConnectPoint)} in the case where the |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 430 | * destination host is known. |
| 431 | * Verifies the correct ARP request is sent out the correct port. |
| 432 | */ |
| 433 | @Test |
| 434 | public void testForwardToHost() { |
| 435 | Host host1 = new DefaultHost(PID, HID1, MAC1, VLAN1, LOC1, |
| 436 | Collections.singleton(IP1)); |
| 437 | |
| 438 | expect(hostService.getHost(HID1)).andReturn(host1); |
| 439 | replay(hostService); |
| 440 | |
| 441 | Ethernet arpRequest = buildArp(ARP.OP_REPLY, MAC2, MAC1, IP2, IP1); |
| 442 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 443 | proxyArp.forward(arpRequest, LOC2); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 444 | |
| 445 | assertEquals(1, packetService.packets.size()); |
| 446 | OutboundPacket packet = packetService.packets.get(0); |
| 447 | |
| 448 | verifyPacketOut(arpRequest, LOC1, packet); |
| 449 | } |
| 450 | |
| 451 | /** |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 452 | * Tests {@link ProxyArpManager#forward(Ethernet, ConnectPoint)} in the case where the |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 453 | * destination host is not known. |
| 454 | * Verifies the correct ARP request is flooded out the correct edge ports. |
| 455 | */ |
| 456 | @Test |
| 457 | public void testForwardFlood() { |
| 458 | expect(hostService.getHost(HID1)).andReturn(null); |
| 459 | replay(hostService); |
| 460 | |
| 461 | Ethernet arpRequest = buildArp(ARP.OP_REPLY, MAC2, MAC1, IP2, IP1); |
| 462 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 463 | proxyArp.forward(arpRequest, getLocation(6)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 464 | |
| 465 | verifyFlood(arpRequest); |
| 466 | } |
| 467 | |
| 468 | /** |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 469 | * Verifies that the given packet was flooded out all available edge ports, |
| 470 | * except for the input port. |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 471 | * |
| 472 | * @param packet the packet that was expected to be flooded |
| 473 | */ |
| 474 | private void verifyFlood(Ethernet packet) { |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 475 | // There should be 1 less than NUM_FLOOD_PORTS; the inPort should be excluded. |
| 476 | assertEquals(NUM_FLOOD_PORTS - 1, packetService.packets.size()); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 477 | |
| 478 | Collections.sort(packetService.packets, |
| 479 | new Comparator<OutboundPacket>() { |
| 480 | @Override |
| 481 | public int compare(OutboundPacket o1, OutboundPacket o2) { |
| 482 | return o1.sendThrough().uri().compareTo(o2.sendThrough().uri()); |
| 483 | } |
| 484 | }); |
| 485 | |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 486 | |
| 487 | for (int i = 0; i < NUM_FLOOD_PORTS - 1; i++) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 488 | ConnectPoint cp = new ConnectPoint(getDeviceId(NUM_ADDRESS_PORTS + i + 1), |
| 489 | PortNumber.portNumber(1)); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 490 | |
| 491 | OutboundPacket outboundPacket = packetService.packets.get(i); |
| 492 | verifyPacketOut(packet, cp, outboundPacket); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Verifies the given packet was sent out the given port. |
| 498 | * |
| 499 | * @param expected the packet that was expected to be sent |
| 500 | * @param outPort the port the packet was expected to be sent out |
| 501 | * @param actual the actual OutboundPacket to verify |
| 502 | */ |
| 503 | private void verifyPacketOut(Ethernet expected, ConnectPoint outPort, |
| 504 | OutboundPacket actual) { |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 505 | assertArrayEquals(expected.serialize(), actual.data().array()); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 506 | assertEquals(1, actual.treatment().instructions().size()); |
| 507 | assertEquals(outPort.deviceId(), actual.sendThrough()); |
| 508 | Instruction instruction = actual.treatment().instructions().get(0); |
| 509 | assertTrue(instruction instanceof OutputInstruction); |
| 510 | assertEquals(outPort.port(), ((OutputInstruction) instruction).port()); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Returns the device ID of the ith device. |
| 515 | * |
| 516 | * @param i device to get the ID of |
| 517 | * @return the device ID |
| 518 | */ |
| 519 | private static DeviceId getDeviceId(int i) { |
| 520 | return DeviceId.deviceId("" + i); |
| 521 | } |
| 522 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 523 | private static HostLocation getLocation(int i) { |
| 524 | return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L); |
| 525 | } |
| 526 | |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 527 | /** |
| 528 | * Builds an ARP packet with the given parameters. |
| 529 | * |
| 530 | * @param opcode opcode of the ARP packet |
| 531 | * @param srcMac source MAC address |
| 532 | * @param dstMac destination MAC address, or null if this is a request |
| 533 | * @param srcIp source IP address |
| 534 | * @param dstIp destination IP address |
| 535 | * @return the ARP packet |
| 536 | */ |
| 537 | private Ethernet buildArp(short opcode, MacAddress srcMac, MacAddress dstMac, |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 538 | Ip4Address srcIp, Ip4Address dstIp) { |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 539 | Ethernet eth = new Ethernet(); |
| 540 | |
| 541 | if (dstMac == null) { |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 542 | eth.setDestinationMACAddress(MacAddress.BROADCAST); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 543 | } else { |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 544 | eth.setDestinationMACAddress(dstMac); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 547 | eth.setSourceMACAddress(srcMac); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 548 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 549 | eth.setVlanID(VLAN1.toShort()); |
| 550 | |
| 551 | ARP arp = new ARP(); |
| 552 | arp.setOpCode(opcode); |
| 553 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 554 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
| 555 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 556 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 557 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 558 | arp.setSenderHardwareAddress(srcMac.toBytes()); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 559 | |
| 560 | if (dstMac == null) { |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 561 | arp.setTargetHardwareAddress(ZERO_MAC_ADDRESS); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 562 | } else { |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 563 | arp.setTargetHardwareAddress(dstMac.toBytes()); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | arp.setSenderProtocolAddress(srcIp.toOctets()); |
| 567 | arp.setTargetProtocolAddress(dstIp.toOctets()); |
| 568 | |
| 569 | eth.setPayload(arp); |
| 570 | return eth; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Test PacketService implementation that simply stores OutboundPackets |
| 575 | * passed to {@link #emit(OutboundPacket)} for later verification. |
| 576 | */ |
| 577 | class TestPacketService implements PacketService { |
| 578 | |
| 579 | List<OutboundPacket> packets = new ArrayList<>(); |
| 580 | |
| 581 | @Override |
| 582 | public void addProcessor(PacketProcessor processor, int priority) { |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public void removeProcessor(PacketProcessor processor) { |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public void emit(OutboundPacket packet) { |
| 591 | packets.add(packet); |
| 592 | } |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 593 | |
| 594 | @Override |
| 595 | public void requestPackets(TrafficSelector selector, |
| 596 | PacketPriority priority, ApplicationId appId) { |
| 597 | } |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 598 | |
| 599 | @Override |
| 600 | public void requestPackets(TrafficSelector selector, |
| 601 | PacketPriority priority, ApplicationId appId, |
| 602 | FlowRule.Type tableType) { |
| 603 | } |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 604 | } |
| 605 | } |