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