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 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 33 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 34 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 35 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 36 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 37 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 38 | import org.projectfloodlight.openflow.protocol.OFType; |
| 39 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 40 | import org.projectfloodlight.openflow.types.OFPort; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 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, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 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; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 57 | private OFFactory factory; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 58 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 59 | private IEventChannel<Long, PacketOutNotification> packetOutEventChannel; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 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()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 74 | for (OFPortDesc port : sw.getEnabledPorts()) { |
| 75 | // XXX S fix this to int |
| 76 | localPorts.put(sw.getId(), port.getPortNo().getShortPortNumber()); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 79 | Multimap<Long, Short> outPorts = value.calculateOutPorts( |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 80 | localPorts, topology); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 81 | sendPacketToSwitches(outPorts, value.getPacketData()); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void entryUpdated(PacketOutNotification value) { |
| 86 | entryAdded(value); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void entryRemoved(PacketOutNotification value) { |
| 91 | // Not used |
| 92 | } |
| 93 | } |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 94 | |
| 95 | public PacketModule() { |
| 96 | listeners = new CopyOnWriteArrayList<>(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void registerPacketListener(IPacketListener listener) { |
| 101 | listeners.addIfAbsent(listener); |
| 102 | } |
| 103 | |
| 104 | @Override |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 105 | public void sendPacket(Ethernet eth, SwitchPort switchPort) { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 106 | SinglePacketOutNotification notification = |
| 107 | new SinglePacketOutNotification(eth.serialize(), 0, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 108 | switchPort.dpid().value(), switchPort.port().shortValue()); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 109 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 110 | // TODO We shouldn't care what the destination MAC is |
| 111 | long dstMac = eth.getDestinationMAC().toLong(); |
| 112 | packetOutEventChannel.addTransientEntry(dstMac, notification); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 116 | public void sendPacket(Ethernet eth, List<SwitchPort> switchPorts) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 117 | // TODO Auto-generated method stub |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 118 | throw new UnsupportedOperationException("Not yet implemented"); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 122 | public void broadcastPacketOutEdge(Ethernet eth) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 123 | // TODO Auto-generated method stub |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 124 | throw new UnsupportedOperationException("Not yet implemented"); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 128 | public void broadcastPacketOutEdge(Ethernet eth, SwitchPort inSwitchPort) { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 129 | BroadcastPacketOutNotification notification = |
| 130 | new BroadcastPacketOutNotification(eth.serialize(), 0, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 131 | inSwitchPort.dpid().value(), inSwitchPort.port().shortValue()); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 132 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 133 | long dstMac = eth.getDestinationMAC().toLong(); |
| 134 | packetOutEventChannel.addTransientEntry(dstMac, notification); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String getName() { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 139 | return "packetmodule"; |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 159 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 160 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 161 | short inport = (short) cntx.getStorage() |
| 162 | .get(IFloodlightProviderService.CONTEXT_PI_INPORT); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 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; |
| 166 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 167 | topology.acquireReadLock(); |
| 168 | Dpid dpid = new Dpid(sw.getId()); |
| 169 | PortNumber p = new PortNumber(inport); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 170 | topologySwitch = topology.getSwitch(dpid); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 171 | inPort = topology.getPort(dpid, p); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 172 | } finally { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 173 | topology.releaseReadLock(); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 176 | if (topologySwitch == null || inPort == null) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 177 | // 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] | 178 | // topology yet |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 179 | return Command.CONTINUE; |
| 180 | } |
| 181 | |
| 182 | for (IPacketListener listener : listeners) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 183 | listener.receive(topologySwitch, inPort, eth); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return Command.CONTINUE; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 191 | List<Class<? extends IFloodlightService>> services = new ArrayList<>(); |
| 192 | services.add(IPacketService.class); |
| 193 | return services; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 198 | getServiceImpls() { |
| 199 | |
| 200 | Map<Class<? extends IFloodlightService>, IFloodlightService> serviceImpls = new HashMap<>(); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 201 | serviceImpls.put(IPacketService.class, this); |
| 202 | return serviceImpls; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 207 | List<Class<? extends IFloodlightService>> dependencies = new ArrayList<>(); |
| 208 | dependencies.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 209 | dependencies.add(ITopologyService.class); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 210 | dependencies.add(IDatagridService.class); |
| 211 | dependencies.add(IFlowPusherService.class); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 212 | return dependencies; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void init(FloodlightModuleContext context) |
| 217 | throws FloodlightModuleException { |
| 218 | floodlightProvider = |
| 219 | context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 220 | topology = context.getServiceImpl(ITopologyService.class) |
| 221 | .getTopology(); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 222 | datagrid = context.getServiceImpl(IDatagridService.class); |
| 223 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 224 | factory = floodlightProvider.getOFMessageFactory_10(); |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void startUp(FloodlightModuleContext context) { |
| 229 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 230 | packetOutEventChannel = datagrid.addListener(PACKET_OUT_CHANNEL_NAME, |
| 231 | packetOutEventHandler, |
| 232 | Long.class, |
| 233 | PacketOutNotification.class); |
| 234 | } |
| 235 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 236 | private void sendPacketToSwitches(Multimap<Long, Short> outPorts, |
| 237 | byte[] packetData) { |
| 238 | for (Long dpid : outPorts.keySet()) { |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 239 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 240 | |
| 241 | if (sw == null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 242 | log.warn("Switch {} not found when sending packet", dpid); |
| 243 | continue; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 246 | List<OFAction> actions = new ArrayList<>(); |
| 247 | for (Short port : outPorts.get(dpid)) { |
| 248 | actions.add(factory.actions().output(OFPort.of(port), Short.MAX_VALUE)); |
| 249 | } |
| 250 | |
| 251 | OFPacketOut po = factory.buildPacketOut() |
| 252 | .setData(packetData) |
| 253 | .setActions(actions) |
| 254 | .build(); |
| 255 | |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 256 | flowPusher.add(sw, po); |
| 257 | } |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 258 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 259 | |
Jonathan Hart | 1f75cae | 2014-04-09 17:24:09 -0700 | [diff] [blame] | 260 | } |