Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.packetservice; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 6 | import java.util.HashSet; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 7 | import java.util.List; |
| 8 | import java.util.Map; |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 9 | import java.util.Set; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 10 | import java.util.concurrent.CopyOnWriteArrayList; |
| 11 | |
| 12 | import net.floodlightcontroller.core.FloodlightContext; |
| 13 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 14 | import net.floodlightcontroller.core.IOFMessageListener; |
| 15 | import net.floodlightcontroller.core.IOFSwitch; |
| 16 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 17 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 18 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 20 | import net.onrc.onos.api.packet.IPacketListener; |
| 21 | import net.onrc.onos.api.packet.IPacketService; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 23 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 24 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
| 25 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.main.config.IConfigInfoService; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.topology.ITopologyService; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.topology.Port; |
| 30 | import net.onrc.onos.core.topology.Switch; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.topology.MutableTopology; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.util.Dpid; |
| 33 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 35 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 37 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 38 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 39 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 40 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 41 | import org.projectfloodlight.openflow.protocol.OFType; |
| 42 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 43 | import org.projectfloodlight.openflow.types.OFPort; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
| 47 | import com.google.common.collect.HashMultimap; |
| 48 | import com.google.common.collect.Multimap; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 49 | |
| 50 | public class PacketModule implements IOFMessageListener, IPacketService, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 51 | IFloodlightModule { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(PacketModule.class); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 53 | |
| 54 | private final CopyOnWriteArrayList<IPacketListener> listeners; |
| 55 | |
| 56 | private IFloodlightProviderService floodlightProvider; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 57 | private MutableTopology mutableTopology; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 58 | private IDatagridService datagrid; |
| 59 | private IFlowPusherService flowPusher; |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 60 | private IConfigInfoService configService; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 61 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 62 | private IEventChannel<Long, PacketOutNotification> packetOutEventChannel; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 63 | |
| 64 | private static final String PACKET_OUT_CHANNEL_NAME = |
| 65 | "onos.packet_out"; |
| 66 | |
| 67 | private PacketOutEventHandler packetOutEventHandler = |
| 68 | new PacketOutEventHandler(); |
| 69 | |
| 70 | private class PacketOutEventHandler implements |
| 71 | IEventChannelListener<Long, PacketOutNotification> { |
| 72 | |
| 73 | @Override |
| 74 | public void entryAdded(PacketOutNotification value) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 75 | Multimap<Long, Short> localPorts = HashMultimap.create(); |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 76 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 77 | for (IOFSwitch sw : floodlightProvider.getSwitches().values()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 78 | for (OFPortDesc port : sw.getEnabledPorts()) { |
| 79 | // XXX S fix this to int |
| 80 | localPorts.put(sw.getId(), port.getPortNo().getShortPortNumber()); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 83 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 84 | Multimap<Long, Short> outPorts = value.calculateOutPorts( |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 85 | localPorts, mutableTopology); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 86 | sendPacketToSwitches(outPorts, value.getPacketData()); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void entryUpdated(PacketOutNotification value) { |
| 91 | entryAdded(value); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void entryRemoved(PacketOutNotification value) { |
| 96 | // Not used |
| 97 | } |
| 98 | } |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 99 | |
| 100 | public PacketModule() { |
| 101 | listeners = new CopyOnWriteArrayList<>(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void registerPacketListener(IPacketListener listener) { |
| 106 | listeners.addIfAbsent(listener); |
| 107 | } |
| 108 | |
| 109 | @Override |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 110 | public void sendPacket(Ethernet eth, SwitchPort switchPort) { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 111 | SinglePacketOutNotification notification = |
| 112 | new SinglePacketOutNotification(eth.serialize(), 0, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 113 | switchPort.getDpid().value(), switchPort.getPortNumber().shortValue()); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 114 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 115 | // TODO We shouldn't care what the destination MAC is |
| 116 | long dstMac = eth.getDestinationMAC().toLong(); |
| 117 | packetOutEventChannel.addTransientEntry(dstMac, notification); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 121 | public void sendPacket(Ethernet eth, List<SwitchPort> switchPorts) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 122 | // TODO Auto-generated method stub |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 123 | throw new UnsupportedOperationException("Not yet implemented"); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 127 | public void broadcastPacketOutInternalEdge(Ethernet eth) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 128 | // TODO Auto-generated method stub |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 129 | throw new UnsupportedOperationException("Not yet implemented"); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 133 | public void broadcastPacketOutInternalEdge(Ethernet eth, SwitchPort inSwitchPort) { |
| 134 | |
pingping-lin | 9af35cd | 2014-09-05 15:22:39 -0700 | [diff] [blame] | 135 | Set<SwitchPort> blacklistSwitchPorts = new HashSet<SwitchPort>(); |
| 136 | Set<SwitchPort> externalSwitchPorts = configService.getExternalSwitchPorts(); |
| 137 | if (externalSwitchPorts != null) { |
| 138 | blacklistSwitchPorts.addAll(externalSwitchPorts); |
| 139 | } |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 140 | blacklistSwitchPorts.add(inSwitchPort); |
| 141 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 142 | BroadcastPacketOutNotification notification = |
| 143 | new BroadcastPacketOutNotification(eth.serialize(), 0, |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 144 | blacklistSwitchPorts); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 145 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 146 | long dstMac = eth.getDestinationMAC().toLong(); |
| 147 | packetOutEventChannel.addTransientEntry(dstMac, notification); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public String getName() { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 152 | return "packetmodule"; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Command receive(IOFSwitch sw, OFMessage msg, |
| 167 | FloodlightContext cntx) { |
| 168 | if (!(msg instanceof OFPacketIn)) { |
| 169 | return Command.CONTINUE; |
| 170 | } |
| 171 | |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 172 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 173 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 174 | // FIXME losing port number precision |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 175 | short inport = (short) cntx.getStorage() |
| 176 | .get(IFloodlightProviderService.CONTEXT_PI_INPORT); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 177 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 178 | Switch topologySwitch; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 179 | Port inPort; |
| 180 | try { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 181 | mutableTopology.acquireReadLock(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 182 | Dpid dpid = new Dpid(sw.getId()); |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 183 | PortNumber p = PortNumber.uint16(inport); |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 184 | topologySwitch = mutableTopology.getSwitch(dpid); |
| 185 | inPort = mutableTopology.getPort(dpid, p); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 186 | } finally { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 187 | mutableTopology.releaseReadLock(); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 190 | if (topologySwitch == null || inPort == null) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 191 | // We can't send packets for switches or ports that aren't in the |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 192 | // topology yet |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 193 | return Command.CONTINUE; |
| 194 | } |
| 195 | |
| 196 | for (IPacketListener listener : listeners) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 197 | listener.receive(topologySwitch, inPort, eth); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return Command.CONTINUE; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 205 | List<Class<? extends IFloodlightService>> services = new ArrayList<>(); |
| 206 | services.add(IPacketService.class); |
| 207 | return services; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 212 | getServiceImpls() { |
| 213 | |
| 214 | Map<Class<? extends IFloodlightService>, IFloodlightService> serviceImpls = new HashMap<>(); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 215 | serviceImpls.put(IPacketService.class, this); |
| 216 | return serviceImpls; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 221 | List<Class<? extends IFloodlightService>> dependencies = new ArrayList<>(); |
| 222 | dependencies.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 223 | dependencies.add(ITopologyService.class); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 224 | dependencies.add(IDatagridService.class); |
| 225 | dependencies.add(IFlowPusherService.class); |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 226 | dependencies.add(IConfigInfoService.class); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 227 | return dependencies; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void init(FloodlightModuleContext context) |
| 232 | throws FloodlightModuleException { |
| 233 | floodlightProvider = |
| 234 | context.getServiceImpl(IFloodlightProviderService.class); |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 235 | mutableTopology = context.getServiceImpl(ITopologyService.class) |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 236 | .getTopology(); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 237 | datagrid = context.getServiceImpl(IDatagridService.class); |
| 238 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
pingping-lin | 0426dee | 2014-08-27 15:03:17 -0700 | [diff] [blame] | 239 | configService = context.getServiceImpl(IConfigInfoService.class); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void startUp(FloodlightModuleContext context) { |
| 244 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 245 | packetOutEventChannel = datagrid.addListener(PACKET_OUT_CHANNEL_NAME, |
| 246 | packetOutEventHandler, |
| 247 | Long.class, |
| 248 | PacketOutNotification.class); |
| 249 | } |
| 250 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 251 | private void sendPacketToSwitches(Multimap<Long, Short> outPorts, |
| 252 | byte[] packetData) { |
| 253 | for (Long dpid : outPorts.keySet()) { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 254 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 255 | |
| 256 | if (sw == null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 257 | log.warn("Switch {} not found when sending packet", dpid); |
| 258 | continue; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 261 | OFFactory factory = sw.getFactory(); |
| 262 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 263 | List<OFAction> actions = new ArrayList<>(); |
| 264 | for (Short port : outPorts.get(dpid)) { |
| 265 | actions.add(factory.actions().output(OFPort.of(port), Short.MAX_VALUE)); |
| 266 | } |
| 267 | |
| 268 | OFPacketOut po = factory.buildPacketOut() |
| 269 | .setData(packetData) |
| 270 | .setActions(actions) |
| 271 | .build(); |
| 272 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 273 | flowPusher.add(new Dpid(sw.getId()), po); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 274 | } |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 275 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 276 | |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 277 | } |