Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.proxyarp; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 2 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 3 | import java.net.InetAddress; |
| 4 | import java.net.UnknownHostException; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 5 | import java.nio.ByteBuffer; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 9 | import java.util.Iterator; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 10 | import java.util.List; |
| 11 | import java.util.Map; |
| 12 | import java.util.Set; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 13 | import java.util.Timer; |
| 14 | import java.util.TimerTask; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 15 | |
| 16 | import net.floodlightcontroller.core.FloodlightContext; |
| 17 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 18 | import net.floodlightcontroller.core.IOFMessageListener; |
| 19 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 20 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 21 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 22 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 23 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 24 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 25 | import net.onrc.onos.apps.bgproute.Interface; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 27 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 28 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.devicemanager.IOnosDeviceService; |
| 30 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.main.config.IConfigInfoService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.packet.ARP; |
| 33 | import net.onrc.onos.core.packet.Ethernet; |
| 34 | import net.onrc.onos.core.packet.IPv4; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.topology.Device; |
| 36 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 37 | import net.onrc.onos.core.topology.NetworkGraph; |
| 38 | import net.onrc.onos.core.topology.Switch; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.util.Dpid; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 41 | |
| 42 | import org.openflow.protocol.OFMessage; |
| 43 | import org.openflow.protocol.OFPacketIn; |
| 44 | import org.openflow.protocol.OFPacketOut; |
| 45 | import org.openflow.protocol.OFPort; |
| 46 | import org.openflow.protocol.OFType; |
| 47 | import org.openflow.protocol.action.OFAction; |
| 48 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 49 | import org.openflow.util.HexString; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 53 | import com.google.common.collect.HashMultimap; |
| 54 | import com.google.common.collect.Multimaps; |
| 55 | import com.google.common.collect.SetMultimap; |
| 56 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 57 | public class ProxyArpManager implements IProxyArpService, IOFMessageListener, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | IFloodlightModule { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 59 | private static final Logger log = LoggerFactory |
| 60 | .getLogger(ProxyArpManager.class); |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 61 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 62 | private static final long ARP_TIMER_PERIOD = 100; // ms |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 63 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 64 | private static final int ARP_REQUEST_TIMEOUT = 2000; // ms |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 65 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 66 | private IFloodlightProviderService floodlightProvider; |
| 67 | private IDatagridService datagrid; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 68 | private IEventChannel<Long, ArpReplyNotification> arpReplyEventChannel; |
| 69 | private IEventChannel<Long, BroadcastPacketOutNotification> broadcastPacketOutEventChannel; |
| 70 | private IEventChannel<Long, SinglePacketOutNotification> singlePacketOutEventChannel; |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 71 | private static final String ARP_REPLY_CHANNEL_NAME = "onos.arp_reply"; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 72 | private static final String BROADCAST_PACKET_OUT_CHANNEL_NAME = "onos.broadcast_packet_out"; |
| 73 | private static final String SINGLE_PACKET_OUT_CHANNEL_NAME = "onos.single_packet_out"; |
| 74 | private ArpReplyEventHandler arpReplyEventHandler = new ArpReplyEventHandler(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | private BroadcastPacketOutEventHandler broadcastPacketOutEventHandler = new BroadcastPacketOutEventHandler(); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 76 | private SinglePacketOutEventHandler singlePacketOutEventHandler = new SinglePacketOutEventHandler(); |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 77 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 78 | private IConfigInfoService configService; |
| 79 | private IRestApiService restApi; |
| 80 | private IFlowPusherService flowPusher; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | |
| 82 | private INetworkGraphService networkGraphService; |
| 83 | private NetworkGraph networkGraph; |
| 84 | private IOnosDeviceService onosDeviceService; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 85 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 86 | private short vlan; |
| 87 | private static final short NO_VLAN = 0; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 88 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 89 | private SetMultimap<InetAddress, ArpRequest> arpRequests; |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 90 | |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 91 | private class BroadcastPacketOutEventHandler implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | IEventChannelListener<Long, BroadcastPacketOutNotification> { |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public void entryAdded(BroadcastPacketOutNotification value) { |
| 96 | if (log.isTraceEnabled()) { |
| 97 | log.trace("entryAdded ip{}, sw {}, port {}, packet {}", value.getTargetAddress(), value.getInSwitch(), value.getInPort(), value.packet.length); |
| 98 | } |
| 99 | BroadcastPacketOutNotification notification = (BroadcastPacketOutNotification) value; |
| 100 | broadcastArpRequestOutMyEdge(notification.packet, |
| 101 | notification.getInSwitch(), |
| 102 | notification.getInPort()); |
| 103 | |
| 104 | // set timestamp |
| 105 | ByteBuffer buffer = ByteBuffer.allocate(4); |
| 106 | buffer.putInt(notification.getTargetAddress()); |
| 107 | InetAddress addr = null; |
| 108 | try { |
| 109 | addr = InetAddress.getByAddress(buffer.array()); |
| 110 | } catch (UnknownHostException e) { |
| 111 | log.error("Exception:", e); |
| 112 | } |
| 113 | |
| 114 | if (addr != null) { |
| 115 | for (ArpRequest request : arpRequests.get(addr)) { |
| 116 | request.setRequestTime(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void entryUpdated(BroadcastPacketOutNotification value) { |
| 123 | log.debug("entryUpdated"); |
| 124 | // TODO: For now, entryUpdated() is processed as entryAdded() |
| 125 | entryAdded(value); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void entryRemoved(BroadcastPacketOutNotification value) { |
| 130 | log.debug("entryRemoved"); |
| 131 | // TODO: Not implemented. Revisit when this module is refactored |
| 132 | } |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 133 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 135 | private class SinglePacketOutEventHandler implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | IEventChannelListener<Long, SinglePacketOutNotification> { |
| 137 | @Override |
| 138 | public void entryAdded(SinglePacketOutNotification packetOutNotification) { |
| 139 | log.debug("entryAdded"); |
| 140 | SinglePacketOutNotification notification = |
| 141 | (SinglePacketOutNotification) packetOutNotification; |
| 142 | sendArpRequestOutPort(notification.packet, |
| 143 | notification.getOutSwitch(), |
| 144 | notification.getOutPort()); |
| 145 | |
| 146 | // set timestamp |
| 147 | ByteBuffer buffer = ByteBuffer.allocate(4); |
| 148 | buffer.putInt(notification.getTargetAddress()); |
| 149 | InetAddress addr = null; |
| 150 | try { |
| 151 | addr = InetAddress.getByAddress(buffer.array()); |
| 152 | } catch (UnknownHostException e) { |
| 153 | log.error("Exception:", e); |
| 154 | } |
| 155 | |
| 156 | if (addr != null) { |
| 157 | for (ArpRequest request : arpRequests.get(addr)) { |
| 158 | request.setRequestTime(); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void entryUpdated(SinglePacketOutNotification packetOutNotification) { |
| 165 | log.debug("entryUpdated"); |
| 166 | // TODO: For now, entryUpdated() is processed as entryAdded() |
| 167 | entryAdded(packetOutNotification); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void entryRemoved(SinglePacketOutNotification packetOutNotification) { |
| 172 | log.debug("entryRemoved"); |
| 173 | // TODO: Not implemented. Revisit when this module is refactored |
| 174 | } |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 177 | private class ArpReplyEventHandler implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | IEventChannelListener<Long, ArpReplyNotification> { |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 179 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public void entryAdded(ArpReplyNotification arpReply) { |
| 182 | log.debug("Received ARP reply notification for ip {}, mac {}", |
| 183 | arpReply.getTargetAddress(), arpReply.getTargetMacAddress()); |
| 184 | ByteBuffer buffer = ByteBuffer.allocate(4); |
| 185 | buffer.putInt(arpReply.getTargetAddress()); |
| 186 | InetAddress addr = null; |
| 187 | try { |
| 188 | addr = InetAddress.getByAddress(buffer.array()); |
| 189 | } catch (UnknownHostException e) { |
| 190 | log.error("Exception:", e); |
| 191 | } |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 192 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 193 | if (addr != null) { |
| 194 | sendArpReplyToWaitingRequesters(addr, |
| 195 | arpReply.getTargetMacAddress()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void entryUpdated(ArpReplyNotification arpReply) { |
| 201 | // TODO: For now, entryUpdated() is processed as entryAdded() |
| 202 | entryAdded(arpReply); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void entryRemoved(ArpReplyNotification arpReply) { |
| 207 | // TODO: Not implemented. Revisit when this module is refactored |
| 208 | } |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 211 | private static class ArpRequest { |
| 212 | private final IArpRequester requester; |
| 213 | private final boolean retry; |
| 214 | private boolean sent = false; |
| 215 | private long requestTime; |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 216 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 217 | public ArpRequest(IArpRequester requester, boolean retry) { |
| 218 | this.requester = requester; |
| 219 | this.retry = retry; |
| 220 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 221 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 222 | public ArpRequest(ArpRequest old) { |
| 223 | this.requester = old.requester; |
| 224 | this.retry = old.retry; |
| 225 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 226 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 227 | public boolean isExpired() { |
| 228 | return sent |
| 229 | && ((System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT); |
| 230 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 231 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 232 | public boolean shouldRetry() { |
| 233 | return retry; |
| 234 | } |
| 235 | |
| 236 | public void dispatchReply(InetAddress ipAddress, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | MACAddress replyMacAddress) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 238 | requester.arpResponse(ipAddress, replyMacAddress); |
| 239 | } |
| 240 | |
| 241 | public void setRequestTime() { |
| 242 | this.requestTime = System.currentTimeMillis(); |
| 243 | this.sent = true; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private class HostArpRequester implements IArpRequester { |
| 248 | private final ARP arpRequest; |
| 249 | private final long dpid; |
| 250 | private final short port; |
| 251 | |
| 252 | public HostArpRequester(ARP arpRequest, long dpid, short port) { |
| 253 | this.arpRequest = arpRequest; |
| 254 | this.dpid = dpid; |
| 255 | this.port = port; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 260 | ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, |
| 261 | macAddress); |
| 262 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | |
| 264 | public ARP getArpRequest() { |
| 265 | return arpRequest; |
| 266 | } |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 271 | Collection<Class<? extends IFloodlightService>> l = |
| 272 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 273 | l.add(IProxyArpService.class); |
| 274 | return l; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 279 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 280 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 281 | m.put(IProxyArpService.class, this); |
| 282 | return m; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 287 | Collection<Class<? extends IFloodlightService>> dependencies = |
| 288 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 289 | dependencies.add(IFloodlightProviderService.class); |
| 290 | dependencies.add(IRestApiService.class); |
| 291 | dependencies.add(IDatagridService.class); |
| 292 | dependencies.add(IConfigInfoService.class); |
| 293 | dependencies.add(IFlowPusherService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 294 | dependencies.add(INetworkGraphService.class); |
| 295 | dependencies.add(IOnosDeviceService.class); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 296 | return dependencies; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void init(FloodlightModuleContext context) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 301 | this.floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 302 | this.configService = context.getServiceImpl(IConfigInfoService.class); |
| 303 | this.restApi = context.getServiceImpl(IRestApiService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 304 | this.datagrid = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 305 | this.flowPusher = context.getServiceImpl(IFlowPusherService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 306 | this.networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
| 307 | this.onosDeviceService = context.getServiceImpl(IOnosDeviceService.class); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 308 | |
| 309 | // arpCache = new ArpCache(); |
| 310 | |
| 311 | arpRequests = Multimaps.synchronizedSetMultimap(HashMultimap |
| 312 | .<InetAddress, ArpRequest>create()); |
| 313 | |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void startUp(FloodlightModuleContext context) { |
| 318 | this.vlan = configService.getVlan(); |
| 319 | log.info("vlan set to {}", this.vlan); |
| 320 | |
| 321 | restApi.addRestletRoutable(new ArpWebRoutable()); |
| 322 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | networkGraph = networkGraphService.getNetworkGraph(); |
| 324 | |
| 325 | // |
| 326 | // Event notification setup: channels and event handlers |
| 327 | // |
| 328 | broadcastPacketOutEventChannel = datagrid.addListener(BROADCAST_PACKET_OUT_CHANNEL_NAME, |
| 329 | broadcastPacketOutEventHandler, |
| 330 | Long.class, |
| 331 | BroadcastPacketOutNotification.class); |
| 332 | |
| 333 | singlePacketOutEventChannel = datagrid.addListener(SINGLE_PACKET_OUT_CHANNEL_NAME, |
| 334 | singlePacketOutEventHandler, |
| 335 | Long.class, |
| 336 | SinglePacketOutNotification.class); |
| 337 | |
| 338 | arpReplyEventChannel = datagrid.addListener(ARP_REPLY_CHANNEL_NAME, |
| 339 | arpReplyEventHandler, |
| 340 | Long.class, |
| 341 | ArpReplyNotification.class); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 342 | |
| 343 | Timer arpTimer = new Timer("arp-processing"); |
| 344 | arpTimer.scheduleAtFixedRate(new TimerTask() { |
| 345 | @Override |
| 346 | public void run() { |
| 347 | doPeriodicArpProcessing(); |
| 348 | } |
| 349 | }, 0, ARP_TIMER_PERIOD); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Function that runs periodically to manage the asynchronous request mechanism. |
| 354 | * It basically cleans up old ARP requests if we don't get a response for them. |
| 355 | * The caller can designate that a request should be retried indefinitely, and |
| 356 | * this task will handle that as well. |
| 357 | */ |
| 358 | private void doPeriodicArpProcessing() { |
| 359 | SetMultimap<InetAddress, ArpRequest> retryList = HashMultimap |
| 360 | .<InetAddress, ArpRequest>create(); |
| 361 | |
| 362 | // Have to synchronize externally on the Multimap while using an |
| 363 | // iterator, |
| 364 | // even though it's a synchronizedMultimap |
| 365 | synchronized (arpRequests) { |
| 366 | Iterator<Map.Entry<InetAddress, ArpRequest>> it = arpRequests |
| 367 | .entries().iterator(); |
| 368 | |
| 369 | while (it.hasNext()) { |
| 370 | Map.Entry<InetAddress, ArpRequest> entry = it.next(); |
| 371 | ArpRequest request = entry.getValue(); |
| 372 | if (request.isExpired()) { |
| 373 | log.debug("Cleaning expired ARP request for {}", entry |
| 374 | .getKey().getHostAddress()); |
| 375 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 376 | // If the ARP request is expired and then delete the device |
| 377 | // TODO check whether this is OK from this thread |
| 378 | HostArpRequester requester = (HostArpRequester) request.requester; |
| 379 | ARP req = requester.getArpRequest(); |
| 380 | Device targetDev = networkGraph.getDeviceByMac(MACAddress.valueOf(req.getTargetHardwareAddress())); |
| 381 | if (targetDev != null) { |
| 382 | onosDeviceService.deleteOnosDeviceByMac(MACAddress.valueOf(req.getTargetHardwareAddress())); |
| 383 | if (log.isDebugEnabled()) { |
| 384 | log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDev.getMacAddress()); |
| 385 | } |
| 386 | } |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 387 | |
| 388 | it.remove(); |
| 389 | |
| 390 | if (request.shouldRetry()) { |
| 391 | retryList.put(entry.getKey(), request); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | for (Map.Entry<InetAddress, Collection<ArpRequest>> entry : retryList |
| 398 | .asMap().entrySet()) { |
| 399 | |
| 400 | InetAddress address = entry.getKey(); |
| 401 | |
| 402 | log.debug("Resending ARP request for {}", address.getHostAddress()); |
| 403 | |
| 404 | // Only ARP requests sent by the controller will have the retry flag |
| 405 | // set, so for now we can just send a new ARP request for that |
| 406 | // address. |
| 407 | sendArpRequestForAddress(address); |
| 408 | |
| 409 | for (ArpRequest request : entry.getValue()) { |
| 410 | arpRequests.put(address, new ArpRequest(request)); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public String getName() { |
| 417 | return "proxyarpmanager"; |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 422 | if (type == OFType.PACKET_IN) { |
| 423 | return "devicemanager".equals(name) |
| 424 | || "onosdevicemanager".equals(name); |
| 425 | } else { |
| 426 | return false; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 432 | return type == OFType.PACKET_IN && "onosforwarding".equals(name); |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 437 | if (!(msg instanceof OFPacketIn)) { |
| 438 | return Command.CONTINUE; |
| 439 | } |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 440 | |
| 441 | OFPacketIn pi = (OFPacketIn) msg; |
| 442 | |
| 443 | Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 444 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 445 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 446 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 447 | ARP arp = (ARP) eth.getPayload(); |
| 448 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 449 | handleArpRequest(sw, pi, arp, eth); |
| 450 | } else if (arp.getOpCode() == ARP.OP_REPLY) { |
| 451 | // For replies we simply send a notification via Hazelcast |
| 452 | sendArpReplyNotification(eth, pi); |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 453 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 454 | // handleArpReply(sw, pi, arp); |
| 455 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 456 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 457 | // Stop ARP packets here |
| 458 | return Command.STOP; |
| 459 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 460 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 461 | // Propagate everything else |
| 462 | return Command.CONTINUE; |
| 463 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 464 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 465 | private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 466 | Ethernet eth) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 467 | if (log.isTraceEnabled()) { |
| 468 | log.trace("ARP request received for {}", |
| 469 | inetAddressToString(arp.getTargetProtocolAddress())); |
| 470 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 471 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 472 | InetAddress target; |
| 473 | try { |
| 474 | target = InetAddress.getByAddress(arp.getTargetProtocolAddress()); |
| 475 | } catch (UnknownHostException e) { |
| 476 | log.debug("Invalid address in ARP request", e); |
| 477 | return; |
| 478 | } |
Pavlin Radoslavov | 4cf8ee5 | 2014-03-26 18:19:58 -0700 | [diff] [blame] | 479 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 480 | if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) { |
| 481 | // If the request came from outside our network, we only care if |
| 482 | // it was a request for one of our interfaces. |
| 483 | if (configService.isInterfaceAddress(target)) { |
| 484 | log.trace( |
| 485 | "ARP request for our interface. Sending reply {} => {}", |
| 486 | target.getHostAddress(), |
| 487 | configService.getRouterMacAddress()); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 488 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 489 | sendArpReply(arp, sw.getId(), pi.getInPort(), |
| 490 | configService.getRouterMacAddress()); |
| 491 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 492 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 495 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 496 | // MACAddress macAddress = arpCache.lookup(target); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 497 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 498 | arpRequests.put(target, new ArpRequest( |
| 499 | new HostArpRequester(arp, sw.getId(), pi.getInPort()), false)); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 500 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 501 | Device targetDevice = networkGraph.getDeviceByMac(MACAddress.valueOf(arp.getTargetHardwareAddress())); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 502 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 503 | if (targetDevice == null) { |
| 504 | if (log.isTraceEnabled()) { |
| 505 | log.trace("No device info found for {} - broadcasting", |
| 506 | target.getHostAddress()); |
| 507 | } |
Jonathan Hart | 18ad950 | 2013-12-15 18:28:00 -0800 | [diff] [blame] | 508 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 509 | // We don't know the device so broadcast the request out |
| 510 | BroadcastPacketOutNotification key = |
| 511 | new BroadcastPacketOutNotification(eth.serialize(), |
| 512 | ByteBuffer.wrap(arp.getTargetProtocolAddress()).getInt(), sw.getId(), pi.getInPort()); |
| 513 | log.debug("broadcastPacketOutEventChannel mac {}, ip {}, dpid {}, port {}, paket {}", eth.getSourceMAC().toLong(), |
| 514 | ByteBuffer.wrap(arp.getTargetProtocolAddress()).getInt(), sw.getId(), pi.getInPort(), eth.serialize().length); |
| 515 | broadcastPacketOutEventChannel.addTransientEntry(eth.getDestinationMAC().toLong(), key); |
| 516 | } else { |
| 517 | // Even if the device exists in our database, we do not reply to |
| 518 | // the request directly, but check whether the device is still valid |
| 519 | MACAddress macAddress = MACAddress.valueOf(arp.getTargetHardwareAddress()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 520 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 521 | if (log.isTraceEnabled()) { |
| 522 | log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}", |
| 523 | new Object[]{ |
| 524 | inetAddressToString(arp.getTargetProtocolAddress()), |
| 525 | macAddress, |
| 526 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
| 527 | } |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 528 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 529 | // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress); |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 530 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 531 | Iterable<net.onrc.onos.core.topology.Port> outPorts = targetDevice.getAttachmentPoints(); |
| 532 | |
| 533 | if (!outPorts.iterator().hasNext()) { |
| 534 | if (log.isTraceEnabled()) { |
| 535 | log.trace("Device {} exists but is not connected to any ports" + |
| 536 | " - broadcasting", macAddress); |
| 537 | } |
| 538 | |
| 539 | // BroadcastPacketOutNotification key = |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 540 | // new BroadcastPacketOutNotification(eth.serialize(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 541 | // target, sw.getId(), pi.getInPort()); |
| 542 | // broadcastPacketOutEventChannel.addTransientEntry(eth.getDestinationMAC().toLong(), key); |
| 543 | } else { |
| 544 | for (net.onrc.onos.core.topology.Port portObject : outPorts) { |
| 545 | //long outSwitch = 0; |
| 546 | //short outPort = 0; |
| 547 | |
| 548 | if (portObject.getOutgoingLink() != null || portObject.getIncomingLink() != null) { |
| 549 | continue; |
| 550 | } |
| 551 | |
| 552 | short outPort = portObject.getNumber().shortValue(); |
| 553 | Switch outSwitchObject = portObject.getSwitch(); |
| 554 | long outSwitch = outSwitchObject.getDpid(); |
| 555 | |
| 556 | if (log.isTraceEnabled()) { |
| 557 | log.trace("Probing device {} on port {}/{}", |
| 558 | new Object[]{macAddress, |
| 559 | HexString.toHexString(outSwitch), outPort}); |
| 560 | } |
| 561 | |
| 562 | SinglePacketOutNotification key = |
| 563 | new SinglePacketOutNotification(eth.serialize(), |
| 564 | ByteBuffer.wrap(target.getAddress()).getInt(), outSwitch, outPort); |
| 565 | singlePacketOutEventChannel.addTransientEntry(eth.getDestinationMAC().toLong(), key); |
| 566 | } |
| 567 | } |
| 568 | } |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 569 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 570 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 571 | // Not used because device manager currently updates the database |
| 572 | // for ARP replies. May be useful in the future. |
| 573 | private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp) { |
| 574 | if (log.isTraceEnabled()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 575 | log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[]{ |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 576 | inetAddressToString(arp.getSenderProtocolAddress()), |
| 577 | HexString.toHexString(arp.getSenderHardwareAddress()), |
| 578 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
| 579 | } |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 580 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 581 | InetAddress senderIpAddress; |
| 582 | try { |
| 583 | senderIpAddress = InetAddress.getByAddress(arp |
| 584 | .getSenderProtocolAddress()); |
| 585 | } catch (UnknownHostException e) { |
| 586 | log.debug("Invalid address in ARP reply", e); |
| 587 | return; |
| 588 | } |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 589 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 590 | MACAddress senderMacAddress = MACAddress.valueOf(arp |
| 591 | .getSenderHardwareAddress()); |
| 592 | |
| 593 | // See if anyone's waiting for this ARP reply |
| 594 | Set<ArpRequest> requests = arpRequests.get(senderIpAddress); |
| 595 | |
| 596 | // Synchronize on the Multimap while using an iterator for one of the |
| 597 | // sets |
| 598 | List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>( |
| 599 | requests.size()); |
| 600 | synchronized (arpRequests) { |
| 601 | Iterator<ArpRequest> it = requests.iterator(); |
| 602 | while (it.hasNext()) { |
| 603 | ArpRequest request = it.next(); |
| 604 | it.remove(); |
| 605 | requestsToSend.add(request); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Don't hold an ARP lock while dispatching requests |
| 610 | for (ArpRequest request : requestsToSend) { |
| 611 | request.dispatchReply(senderIpAddress, senderMacAddress); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | private void sendArpRequestForAddress(InetAddress ipAddress) { |
| 616 | // TODO what should the sender IP address and MAC address be if no |
| 617 | // IP addresses are configured? Will there ever be a need to send |
| 618 | // ARP requests from the controller in that case? |
| 619 | // All-zero MAC address doesn't seem to work - hosts don't respond to it |
| 620 | |
| 621 | byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0}; |
| 622 | byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 623 | byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01}; |
| 624 | byte[] broadcastMac = {(byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 625 | (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
| 626 | |
| 627 | ARP arpRequest = new ARP(); |
| 628 | |
| 629 | arpRequest |
| 630 | .setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 631 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 632 | .setHardwareAddressLength( |
| 633 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 634 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 635 | .setOpCode(ARP.OP_REQUEST).setTargetHardwareAddress(zeroMac) |
| 636 | .setTargetProtocolAddress(ipAddress.getAddress()); |
| 637 | |
| 638 | MACAddress routerMacAddress = configService.getRouterMacAddress(); |
| 639 | // TODO hack for now as it's unclear what the MAC address should be |
| 640 | byte[] senderMacAddress = genericNonZeroMac; |
| 641 | if (routerMacAddress != null) { |
| 642 | senderMacAddress = routerMacAddress.toBytes(); |
| 643 | } |
| 644 | arpRequest.setSenderHardwareAddress(senderMacAddress); |
| 645 | |
| 646 | byte[] senderIPAddress = zeroIpv4; |
| 647 | Interface intf = configService.getOutgoingInterface(ipAddress); |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 648 | if (intf == null) { |
| 649 | // TODO handle the case where the controller needs to send an ARP |
| 650 | // request but there's not IP configuration. In this case the |
| 651 | // request should be broadcast out all edge ports in the network. |
| 652 | log.warn("Sending ARP requests with default configuration " |
| 653 | + "not supported"); |
| 654 | return; |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 657 | senderIPAddress = intf.getIpAddress().getAddress(); |
| 658 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 659 | arpRequest.setSenderProtocolAddress(senderIPAddress); |
| 660 | |
| 661 | Ethernet eth = new Ethernet(); |
| 662 | eth.setSourceMACAddress(senderMacAddress) |
| 663 | .setDestinationMACAddress(broadcastMac) |
| 664 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpRequest); |
| 665 | |
| 666 | if (vlan != NO_VLAN) { |
| 667 | eth.setVlanID(vlan).setPriorityCode((byte) 0); |
| 668 | } |
| 669 | |
| 670 | // sendArpRequestToSwitches(ipAddress, eth.serialize()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 671 | SinglePacketOutNotification key = |
| 672 | new SinglePacketOutNotification(eth.serialize(), ByteBuffer.wrap(ipAddress.getAddress()).getInt(), |
| 673 | intf.getDpid(), intf.getPort()); |
| 674 | singlePacketOutEventChannel.addTransientEntry(MACAddress.valueOf(senderMacAddress).toLong(), key); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 675 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 676 | |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 677 | private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 678 | sendArpRequestToSwitches(dstAddress, arpRequest, 0, |
| 679 | OFPort.OFPP_NONE.getValue()); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | private void sendArpRequestToSwitches(InetAddress dstAddress, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 683 | byte[] arpRequest, long inSwitch, short inPort) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 684 | |
| 685 | if (configService.hasLayer3Configuration()) { |
| 686 | Interface intf = configService.getOutgoingInterface(dstAddress); |
| 687 | if (intf == null) { |
| 688 | // TODO here it should be broadcast out all non-interface edge |
| 689 | // ports. |
| 690 | // I think we can assume that if it's not a request for an |
| 691 | // external |
| 692 | // network, it's an ARP for a host in our own network. So we |
| 693 | // want to |
| 694 | // send it out all edge ports that don't have an interface |
| 695 | // configured |
| 696 | // to ensure it reaches all hosts in our network. |
| 697 | log.debug("No interface found to send ARP request for {}", |
| 698 | dstAddress.getHostAddress()); |
| 699 | } else { |
| 700 | sendArpRequestOutPort(arpRequest, intf.getDpid(), |
| 701 | intf.getPort()); |
| 702 | } |
| 703 | } else { |
| 704 | // broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort); |
| 705 | broadcastArpRequestOutMyEdge(arpRequest, inSwitch, inPort); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | private void sendArpReplyNotification(Ethernet eth, OFPacketIn pi) { |
| 710 | ARP arp = (ARP) eth.getPayload(); |
| 711 | |
| 712 | if (log.isTraceEnabled()) { |
| 713 | log.trace("Sending ARP reply for {} to other ONOS instances", |
| 714 | inetAddressToString(arp.getSenderProtocolAddress())); |
| 715 | } |
| 716 | |
| 717 | InetAddress targetAddress; |
| 718 | |
| 719 | try { |
| 720 | targetAddress = InetAddress.getByAddress(arp |
| 721 | .getSenderProtocolAddress()); |
| 722 | } catch (UnknownHostException e) { |
| 723 | log.error("Unknown host", e); |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | MACAddress mac = new MACAddress(arp.getSenderHardwareAddress()); |
| 728 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 729 | ArpReplyNotification key = |
| 730 | new ArpReplyNotification(ByteBuffer.wrap(targetAddress.getAddress()).getInt(), mac); |
| 731 | log.debug("ArpReplyNotification ip {}, mac{}", ByteBuffer.wrap(targetAddress.getAddress()).getInt(), mac); |
| 732 | arpReplyEventChannel.addTransientEntry(mac.toLong(), key); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | private void broadcastArpRequestOutMyEdge(byte[] arpRequest, long inSwitch, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 736 | short inPort) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 737 | List<SwitchPort> switchPorts = new ArrayList<SwitchPort>(); |
| 738 | |
| 739 | for (IOFSwitch sw : floodlightProvider.getSwitches().values()) { |
| 740 | |
| 741 | OFPacketOut po = new OFPacketOut(); |
| 742 | po.setInPort(OFPort.OFPP_NONE).setBufferId(-1) |
| 743 | .setPacketData(arpRequest); |
| 744 | |
| 745 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 746 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 747 | Switch graphSw = networkGraph.getSwitch(sw.getId()); |
| 748 | Collection<net.onrc.onos.core.topology.Port> ports = graphSw.getPorts(); |
| 749 | |
| 750 | if (ports == null) { |
| 751 | continue; |
| 752 | } |
| 753 | |
| 754 | for (net.onrc.onos.core.topology.Port portObject : ports) { |
| 755 | if (portObject.getOutgoingLink() == null && portObject.getNumber() > 0) { |
| 756 | Long portNumber = portObject.getNumber(); |
| 757 | |
| 758 | if (sw.getId() == inSwitch && portNumber.shortValue() == inPort) { |
| 759 | // This is the port that the ARP message came in, |
| 760 | // so don't broadcast out this port |
| 761 | continue; |
| 762 | } |
| 763 | switchPorts.add(new SwitchPort(new Dpid(sw.getId()), |
| 764 | new net.onrc.onos.core.util.Port(portNumber.shortValue()))); |
| 765 | actions.add(new OFActionOutput(portNumber.shortValue())); |
| 766 | } |
| 767 | } |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 768 | |
| 769 | po.setActions(actions); |
| 770 | short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 771 | po.setActionsLength(actionsLength); |
| 772 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
| 773 | + arpRequest.length); |
| 774 | |
| 775 | flowPusher.add(sw, po); |
| 776 | } |
| 777 | |
| 778 | if (log.isTraceEnabled()) { |
| 779 | log.trace("Broadcast ARP request to: {}", switchPorts); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) { |
| 784 | if (log.isTraceEnabled()) { |
| 785 | log.trace("Sending ARP request out {}/{}", |
| 786 | HexString.toHexString(dpid), port); |
| 787 | } |
| 788 | |
| 789 | OFPacketOut po = new OFPacketOut(); |
| 790 | po.setInPort(OFPort.OFPP_NONE).setBufferId(-1) |
| 791 | .setPacketData(arpRequest); |
| 792 | |
| 793 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 794 | actions.add(new OFActionOutput(port)); |
| 795 | po.setActions(actions); |
| 796 | short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 797 | po.setActionsLength(actionsLength); |
| 798 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
| 799 | + arpRequest.length); |
| 800 | |
| 801 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 802 | |
| 803 | if (sw == null) { |
| 804 | log.warn("Switch not found when sending ARP request"); |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | flowPusher.add(sw, po); |
| 809 | } |
| 810 | |
| 811 | private void sendArpReply(ARP arpRequest, long dpid, short port, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 812 | MACAddress targetMac) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 813 | if (log.isTraceEnabled()) { |
| 814 | log.trace( |
| 815 | "Sending reply {} => {} to {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 816 | new Object[]{ |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 817 | inetAddressToString(arpRequest |
| 818 | .getTargetProtocolAddress()), |
| 819 | targetMac, |
| 820 | inetAddressToString(arpRequest |
| 821 | .getSenderProtocolAddress())}); |
| 822 | } |
| 823 | |
| 824 | ARP arpReply = new ARP(); |
| 825 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 826 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 827 | .setHardwareAddressLength( |
| 828 | (byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 829 | .setProtocolAddressLength((byte) IPv4.ADDRESS_LENGTH) |
| 830 | .setOpCode(ARP.OP_REPLY) |
| 831 | .setSenderHardwareAddress(targetMac.toBytes()) |
| 832 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 833 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 834 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 835 | |
| 836 | Ethernet eth = new Ethernet(); |
| 837 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
| 838 | .setSourceMACAddress(targetMac.toBytes()) |
| 839 | .setEtherType(Ethernet.TYPE_ARP).setPayload(arpReply); |
| 840 | |
| 841 | if (vlan != NO_VLAN) { |
| 842 | eth.setVlanID(vlan).setPriorityCode((byte) 0); |
| 843 | } |
| 844 | |
| 845 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 846 | actions.add(new OFActionOutput(port)); |
| 847 | |
| 848 | OFPacketOut po = new OFPacketOut(); |
| 849 | po.setInPort(OFPort.OFPP_NONE) |
| 850 | .setBufferId(-1) |
| 851 | .setPacketData(eth.serialize()) |
| 852 | .setActions(actions) |
| 853 | .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH) |
| 854 | .setLengthU( |
| 855 | OFPacketOut.MINIMUM_LENGTH |
| 856 | + OFActionOutput.MINIMUM_LENGTH |
| 857 | + po.getPacketData().length); |
| 858 | |
| 859 | List<OFMessage> msgList = new ArrayList<OFMessage>(); |
| 860 | msgList.add(po); |
| 861 | |
| 862 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 863 | |
| 864 | if (sw == null) { |
| 865 | log.warn("Switch {} not found when sending ARP reply", |
| 866 | HexString.toHexString(dpid)); |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | flowPusher.add(sw, po); |
| 871 | } |
| 872 | |
| 873 | private String inetAddressToString(byte[] bytes) { |
| 874 | try { |
| 875 | return InetAddress.getByAddress(bytes).getHostAddress(); |
| 876 | } catch (UnknownHostException e) { |
| 877 | log.debug("Invalid IP address", e); |
| 878 | return ""; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * IProxyArpService methods |
| 884 | */ |
| 885 | |
| 886 | @Override |
| 887 | public MACAddress getMacAddress(InetAddress ipAddress) { |
| 888 | // return arpCache.lookup(ipAddress); |
| 889 | return null; |
| 890 | } |
| 891 | |
| 892 | @Override |
| 893 | public void sendArpRequest(InetAddress ipAddress, IArpRequester requester, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 894 | boolean retry) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 895 | arpRequests.put(ipAddress, new ArpRequest(requester, retry)); |
| 896 | |
| 897 | // Sanity check to make sure we don't send a request for our own address |
| 898 | if (!configService.isInterfaceAddress(ipAddress)) { |
| 899 | sendArpRequestForAddress(ipAddress); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | @Override |
| 904 | public List<String> getMappings() { |
| 905 | return new ArrayList<String>(); |
| 906 | } |
| 907 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 908 | private void sendArpReplyToWaitingRequesters(InetAddress address, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 909 | MACAddress mac) { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 910 | log.debug("Sending ARP reply for {} to requesters", |
| 911 | address.getHostAddress()); |
| 912 | |
| 913 | // See if anyone's waiting for this ARP reply |
| 914 | Set<ArpRequest> requests = arpRequests.get(address); |
| 915 | |
| 916 | // Synchronize on the Multimap while using an iterator for one of the |
| 917 | // sets |
| 918 | List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>( |
| 919 | requests.size()); |
| 920 | synchronized (arpRequests) { |
| 921 | Iterator<ArpRequest> it = requests.iterator(); |
| 922 | while (it.hasNext()) { |
| 923 | ArpRequest request = it.next(); |
| 924 | it.remove(); |
| 925 | requestsToSend.add(request); |
| 926 | } |
| 927 | } |
| 928 | |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 929 | //TODO here, comment outed from long time ago. I will check if we need it later. |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 930 | /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP( |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 931 | InetAddresses.coerceToInteger(address)); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 932 | |
| 933 | MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress()); |
| 934 | |
| 935 | log.debug("Found {} at {} in network map", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 936 | address.getHostAddress(), mac);*/ |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 937 | |
| 938 | // Don't hold an ARP lock while dispatching requests |
| 939 | for (ArpRequest request : requestsToSend) { |
| 940 | request.dispatchReply(address, mac); |
| 941 | } |
| 942 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 943 | } |