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