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