Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowprogrammer; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | import java.util.concurrent.ExecutionException; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 11 | import java.util.concurrent.Future; |
| 12 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 13 | import org.openflow.protocol.OFFlowMod; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 14 | import org.openflow.protocol.OFMatch; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 15 | import org.openflow.protocol.OFPort; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 16 | import org.openflow.protocol.OFStatisticsRequest; |
| 17 | import org.openflow.protocol.statistics.OFFlowStatisticsReply; |
| 18 | import org.openflow.protocol.statistics.OFFlowStatisticsRequest; |
| 19 | import org.openflow.protocol.statistics.OFStatistics; |
| 20 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 25 | import net.onrc.onos.graph.GraphDBOperation; |
| 26 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 27 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.util.Dpid; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 30 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 31 | /** |
| 32 | * FlowSynchronizer is a sub-module of FlowProgrammer to keep switches' flow table |
| 33 | * synchronized with GraphDB. FlowSynchronizer periodically read flow tables from |
| 34 | * switches and compare them with GraphDB to drop unnecessary flows and/or to install |
| 35 | * missing flows. FlowSynchronizer also watch the event of addition/deletion of switches |
| 36 | * and start synchronization. |
| 37 | * @author Brian |
| 38 | * |
| 39 | */ |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 40 | public class FlowSynchronizer implements IFlowSyncService { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 41 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 42 | private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 43 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 44 | private GraphDBOperation dbHandler; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 45 | protected IFlowPusherService pusher; |
| 46 | private Map<IOFSwitch, Thread> switchThreads; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 47 | |
| 48 | public FlowSynchronizer() { |
| 49 | dbHandler = new GraphDBOperation(""); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 50 | switchThreads = new HashMap<IOFSwitch, Thread>(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 53 | @Override |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 54 | public void synchronize(IOFSwitch sw) { |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 55 | Synchronizer sync = new Synchronizer(sw); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 56 | Thread t = new Thread(sync); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 57 | switchThreads.put(sw, t); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 58 | t.start(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 59 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 60 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 61 | @Override |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 62 | public void interrupt(IOFSwitch sw) { |
| 63 | Thread t = switchThreads.remove(sw); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 64 | if(t != null) { |
| 65 | t.interrupt(); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 66 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 69 | public void init(IFlowPusherService pusherService) { |
| 70 | pusher = pusherService; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 73 | /** |
| 74 | * Synchronizer represents main thread of synchronization. |
| 75 | * @author Brian |
| 76 | * |
| 77 | */ |
| 78 | protected class Synchronizer implements Runnable { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 79 | IOFSwitch sw; |
| 80 | ISwitchObject swObj; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 81 | |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 82 | public Synchronizer(IOFSwitch sw) { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 83 | this.sw = sw; |
| 84 | Dpid dpid = new Dpid(sw.getId()); |
| 85 | this.swObj = dbHandler.searchSwitch(dpid.toString()); |
| 86 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 87 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public void run() { |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 90 | // TODO: stop adding other flow entries while synchronizing |
| 91 | //pusher.suspend(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 92 | Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph(); |
| 93 | Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 94 | compare(graphEntries, switchEntries); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 95 | //pusher.resume(sw); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 96 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 97 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 98 | /** |
| 99 | * Compare flows entries in GraphDB and switch to pick up necessary messages. |
| 100 | * After picking up, picked messages are added to FlowPusher. |
| 101 | * @param graphEntries Flow entries in GraphDB. |
| 102 | * @param switchEntries Flow entries in switch. |
| 103 | */ |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 104 | private void compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 105 | int added = 0, removed = 0, skipped = 0; |
| 106 | for(FlowEntryWrapper entry : switchEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 107 | if(graphEntries.contains(entry)) { |
| 108 | graphEntries.remove(entry); |
| 109 | skipped++; |
| 110 | } |
| 111 | else { |
| 112 | // remove flow entry from the switch |
| 113 | entry.removeFromSwitch(sw); |
| 114 | removed++; |
| 115 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 116 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 117 | for(FlowEntryWrapper entry : graphEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 118 | // add flow entry to switch |
| 119 | entry.addToSwitch(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 120 | added++; |
| 121 | } |
| 122 | log.debug("Flow entries added "+ added + ", " + |
| 123 | "Flow entries removed "+ removed + ", " + |
| 124 | "Flow entries skipped " + skipped); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 125 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 126 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 127 | /** |
| 128 | * Read GraphDB to get FlowEntries associated with a switch. |
| 129 | * @return set of FlowEntries |
| 130 | */ |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 131 | private Set<FlowEntryWrapper> getFlowEntriesFromGraph() { |
| 132 | Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 133 | for(IFlowEntry entry : swObj.getFlowEntries()) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 134 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 135 | entries.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 136 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 137 | return entries; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 138 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 139 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 140 | /** |
| 141 | * Read flow table from switch and derive FlowEntries from table. |
| 142 | * @return set of FlowEntries |
| 143 | */ |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 144 | private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 145 | |
| 146 | int lengthU = 0; |
| 147 | OFMatch match = new OFMatch(); |
| 148 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 149 | |
| 150 | OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest(); |
| 151 | stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE |
| 152 | stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES) |
| 153 | stat.setMatch(match); |
| 154 | List<OFStatistics> stats = new ArrayList<OFStatistics>(); |
| 155 | stats.add(stat); |
| 156 | lengthU += stat.getLength(); |
| 157 | |
| 158 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 159 | req.setStatisticType(OFStatisticsType.FLOW); |
| 160 | req.setStatistics(stats); |
| 161 | lengthU += req.getLengthU(); |
| 162 | req.setLengthU(lengthU); |
| 163 | |
| 164 | List<OFStatistics> entries = null; |
| 165 | try { |
| 166 | Future<List<OFStatistics>> dfuture = sw.getStatistics(req); |
| 167 | entries = dfuture.get(); |
| 168 | } catch (IOException e) { |
| 169 | // TODO Auto-generated catch block |
| 170 | e.printStackTrace(); |
| 171 | } catch (InterruptedException e) { |
| 172 | // TODO Auto-generated catch block |
| 173 | e.printStackTrace(); |
| 174 | } catch (ExecutionException e) { |
| 175 | // TODO Auto-generated catch block |
| 176 | e.printStackTrace(); |
| 177 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 178 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 179 | Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 180 | for(OFStatistics result : entries){ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 181 | OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 182 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 183 | results.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 184 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 185 | return results; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 186 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 187 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 190 | /** |
| 191 | * FlowEntryWrapper represents abstract FlowEntry which is embodied by IFlowEntry |
| 192 | * (from GraphDB) or OFFlowStatisticsReply (from switch). |
| 193 | * @author Brian |
| 194 | * |
| 195 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 196 | class FlowEntryWrapper { |
| 197 | FlowEntryId id; |
| 198 | IFlowEntry iflowEntry; |
| 199 | OFFlowStatisticsReply statisticsReply; |
| 200 | |
| 201 | public FlowEntryWrapper(IFlowEntry entry) { |
| 202 | iflowEntry = entry; |
| 203 | id = new FlowEntryId(entry.getFlowEntryId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 206 | public FlowEntryWrapper(OFFlowStatisticsReply entry) { |
| 207 | statisticsReply = entry; |
| 208 | id = new FlowEntryId(entry.getCookie()); |
| 209 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 210 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 211 | /** |
| 212 | * Install this FlowEntry to a switch via FlowPusher. |
| 213 | * @param sw |
| 214 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 215 | public void addToSwitch(IOFSwitch sw) { |
| 216 | if(iflowEntry != null) { |
| 217 | pusher.add(sw, iflowEntry.getFlow(), iflowEntry); |
| 218 | } |
| 219 | else if(statisticsReply != null) { |
| 220 | log.error("Adding existing flow entry {} to sw {}", |
| 221 | statisticsReply.getCookie(), sw.getId()); |
| 222 | } |
| 223 | } |
| 224 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame^] | 225 | /** |
| 226 | * Remove this FlowEntry from a switch via FlowPusher. |
| 227 | * @param sw |
| 228 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 229 | public void removeFromSwitch(IOFSwitch sw){ |
| 230 | if(iflowEntry != null) { |
| 231 | log.error("Removing non-existent flow entry {} from sw {}", |
| 232 | iflowEntry.getFlowEntryId(), sw.getId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 233 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 234 | } |
| 235 | else if(statisticsReply != null) { |
| 236 | // Convert Statistics Reply to Flow Mod, then write it |
| 237 | OFFlowMod fm = new OFFlowMod(); |
| 238 | fm.setCookie(statisticsReply.getCookie()); |
| 239 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT); |
| 240 | fm.setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 241 | fm.setMatch(statisticsReply.getMatch()); |
| 242 | fm.setPriority(statisticsReply.getPriority()); |
| 243 | fm.setOutPort(OFPort.OFPP_NONE); |
| 244 | pusher.add(sw, fm); |
| 245 | } |
| 246 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 247 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 248 | /** |
| 249 | * Return the hash code of the Flow Entry ID |
| 250 | */ |
| 251 | @Override |
| 252 | public int hashCode() { |
| 253 | return id.hashCode(); |
| 254 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 255 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Returns true of the object is another Flow Entry ID with |
| 258 | * the same value; otherwise, returns false. |
| 259 | * |
| 260 | * @param Object to compare |
| 261 | */ |
| 262 | @Override |
| 263 | public boolean equals(Object obj){ |
| 264 | if(obj.getClass() == this.getClass()) { |
| 265 | FlowEntryWrapper entry = (FlowEntryWrapper) obj; |
| 266 | // TODO: we need to actually compare the match + actions |
| 267 | return this.id.equals(entry.id); |
| 268 | } |
| 269 | return false; |
| 270 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 271 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 272 | @Override |
| 273 | public String toString() { |
| 274 | return id.toString(); |
| 275 | } |
| 276 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 277 | |
| 278 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 281 | |