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