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