blob: 6ef44be70024e18aad6e890f14b6908243ca46b1 [file] [log] [blame]
Brian O'Connor8c166a72013-11-14 18:41:48 -08001package net.onrc.onos.ofcontroller.flowprogrammer;
Brian O'Connora8e49802013-10-30 20:49:59 -07002
3import java.io.IOException;
4import java.util.ArrayList;
Brian O'Connora8e49802013-10-30 20:49:59 -07005import java.util.HashMap;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Map;
9import java.util.Set;
Naoki Shiota2bdda572013-12-09 15:05:21 -080010import java.util.concurrent.Callable;
Brian O'Connora8e49802013-10-30 20:49:59 -070011import java.util.concurrent.ExecutionException;
Brian O'Connora8e49802013-10-30 20:49:59 -070012import java.util.concurrent.Future;
Naoki Shiota2bdda572013-12-09 15:05:21 -080013import java.util.concurrent.FutureTask;
Brian O'Connora8e49802013-10-30 20:49:59 -070014
Brian O'Connor0d6ba512013-11-05 15:17:44 -080015import org.openflow.protocol.OFFlowMod;
Brian O'Connora8e49802013-10-30 20:49:59 -070016import org.openflow.protocol.OFMatch;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080017import org.openflow.protocol.OFPort;
Brian O'Connora8e49802013-10-30 20:49:59 -070018import org.openflow.protocol.OFStatisticsRequest;
19import org.openflow.protocol.statistics.OFFlowStatisticsReply;
20import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
21import org.openflow.protocol.statistics.OFStatistics;
22import org.openflow.protocol.statistics.OFStatisticsType;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Brian O'Connora8e49802013-10-30 20:49:59 -070026import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connora8e49802013-10-30 20:49:59 -070027import net.onrc.onos.graph.GraphDBOperation;
28import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -080030import net.onrc.onos.ofcontroller.flowmanager.FlowDatabaseOperation;
Brian O'Connora8e49802013-10-30 20:49:59 -070031import net.onrc.onos.ofcontroller.util.Dpid;
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -080032import net.onrc.onos.ofcontroller.util.FlowEntry;
Brian O'Connora8e49802013-10-30 20:49:59 -070033import net.onrc.onos.ofcontroller.util.FlowEntryId;
34
Naoki Shiotae3199732013-11-25 16:14:43 -080035/**
Naoki Shiotab485d412013-11-26 12:04:19 -080036 * FlowSynchronizer is an implementation of FlowSyncService.
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -080037 * In addition to IFlowSyncService, FlowSynchronizer periodically reads flow
38 * tables from switches and compare them with GraphDB to drop unnecessary
39 * flows and/or to install missing flows.
Naoki Shiotae3199732013-11-25 16:14:43 -080040 * @author Brian
41 *
42 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080043public class FlowSynchronizer implements IFlowSyncService {
Brian O'Connora8e49802013-10-30 20:49:59 -070044
Brian O'Connorea1efbe2013-11-25 22:57:43 -080045 private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class);
Brian O'Connore46492e2013-11-14 21:11:50 -080046
Brian O'Connor8c166a72013-11-14 18:41:48 -080047 private GraphDBOperation dbHandler;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080048 protected IFlowPusherService pusher;
Naoki Shiota2bdda572013-12-09 15:05:21 -080049 private Map<IOFSwitch, FutureTask<SyncResult>> switchThreads;
Brian O'Connore46492e2013-11-14 21:11:50 -080050
51 public FlowSynchronizer() {
52 dbHandler = new GraphDBOperation("");
Naoki Shiota2bdda572013-12-09 15:05:21 -080053 switchThreads = new HashMap<IOFSwitch, FutureTask<SyncResult>>();
Brian O'Connore46492e2013-11-14 21:11:50 -080054 }
55
Brian O'Connorea1efbe2013-11-25 22:57:43 -080056 @Override
Naoki Shiota2bdda572013-12-09 15:05:21 -080057 public Future<SyncResult> synchronize(IOFSwitch sw) {
Pavlin Radoslavovf8c78552013-11-26 10:56:59 -080058 Synchronizer sync = new Synchronizer(sw);
Naoki Shiota2bdda572013-12-09 15:05:21 -080059 FutureTask<SyncResult> task = new FutureTask<SyncResult>(sync);
60 switchThreads.put(sw, task);
61 task.run();
62 return task;
Brian O'Connore46492e2013-11-14 21:11:50 -080063 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -080064
Brian O'Connore46492e2013-11-14 21:11:50 -080065 @Override
Brian O'Connorea1efbe2013-11-25 22:57:43 -080066 public void interrupt(IOFSwitch sw) {
Naoki Shiota2bdda572013-12-09 15:05:21 -080067 FutureTask<SyncResult> t = switchThreads.remove(sw);
Brian O'Connore46492e2013-11-14 21:11:50 -080068 if(t != null) {
Naoki Shiota2bdda572013-12-09 15:05:21 -080069 t.cancel(true);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080070 }
Brian O'Connore46492e2013-11-14 21:11:50 -080071 }
72
Naoki Shiotab485d412013-11-26 12:04:19 -080073 /**
74 * Initialize Synchronizer.
75 * @param pusherService FlowPusherService used for sending messages.
76 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080077 public void init(IFlowPusherService pusherService) {
78 pusher = pusherService;
Brian O'Connore46492e2013-11-14 21:11:50 -080079 }
80
Naoki Shiotae3199732013-11-25 16:14:43 -080081 /**
82 * Synchronizer represents main thread of synchronization.
83 * @author Brian
84 *
85 */
Naoki Shiota2bdda572013-12-09 15:05:21 -080086 protected class Synchronizer implements Callable<SyncResult> {
Brian O'Connora8e49802013-10-30 20:49:59 -070087 IOFSwitch sw;
88 ISwitchObject swObj;
Brian O'Connore46492e2013-11-14 21:11:50 -080089
Pavlin Radoslavovf8c78552013-11-26 10:56:59 -080090 public Synchronizer(IOFSwitch sw) {
Brian O'Connora8e49802013-10-30 20:49:59 -070091 this.sw = sw;
92 Dpid dpid = new Dpid(sw.getId());
93 this.swObj = dbHandler.searchSwitch(dpid.toString());
94 }
Brian O'Connore46492e2013-11-14 21:11:50 -080095
Brian O'Connor321a5d32013-12-09 18:11:35 -080096 double graphIDTime, switchTime, compareTime, graphEntryTime, extractTime, pushTime, totalTime;
Brian O'Connora8e49802013-10-30 20:49:59 -070097 @Override
Naoki Shiota2bdda572013-12-09 15:05:21 -080098 public SyncResult call() {
Brian O'Connorea1efbe2013-11-25 22:57:43 -080099 // TODO: stop adding other flow entries while synchronizing
100 //pusher.suspend(sw);
Brian O'Connor321a5d32013-12-09 18:11:35 -0800101 long start = System.nanoTime();
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800102 Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph();
Brian O'Connor321a5d32013-12-09 18:11:35 -0800103 long step1 = System.nanoTime();
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800104 Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch();
Brian O'Connor321a5d32013-12-09 18:11:35 -0800105 long step2 = System.nanoTime();
Naoki Shiota2bdda572013-12-09 15:05:21 -0800106 SyncResult result = compare(graphEntries, switchEntries);
Brian O'Connor321a5d32013-12-09 18:11:35 -0800107 long step3 = System.nanoTime();
108 graphIDTime = (step1 - start);
109 switchTime = (step2 - step1);
110 compareTime = (step3 - step2);
111 totalTime = (step3 - start);
112 outputTime();
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800113 //pusher.resume(sw);
Naoki Shiota2bdda572013-12-09 15:05:21 -0800114
115 return result;
Brian O'Connora8e49802013-10-30 20:49:59 -0700116 }
Brian O'Connor321a5d32013-12-09 18:11:35 -0800117
118 private void outputTime() {
119 double div = Math.pow(10, 6); //convert nanoseconds to ms
120 graphIDTime /= div;
121 switchTime /= div;
122 compareTime = (compareTime - graphEntryTime - extractTime - pushTime) / div;
123 graphEntryTime /= div;
124 extractTime /= div;
125 pushTime /= div;
Brian O'Connor8f7f8582013-12-11 15:48:07 -0800126 totalTime /= div;
Brian O'Connor321a5d32013-12-09 18:11:35 -0800127 log.debug("Sync time (ms):" +
128 graphIDTime + "," +
129 switchTime + "," +
130 compareTime + "," +
131 graphEntryTime + "," +
132 extractTime + "," +
133 pushTime + "," +
134 totalTime);
135 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800136
Naoki Shiotae3199732013-11-25 16:14:43 -0800137 /**
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800138 * Compare flows entries in GraphDB and switch to pick up necessary
139 * messages.
Naoki Shiotae3199732013-11-25 16:14:43 -0800140 * After picking up, picked messages are added to FlowPusher.
141 * @param graphEntries Flow entries in GraphDB.
142 * @param switchEntries Flow entries in switch.
143 */
Naoki Shiota2bdda572013-12-09 15:05:21 -0800144 private SyncResult compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) {
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800145 int added = 0, removed = 0, skipped = 0;
146 for(FlowEntryWrapper entry : switchEntries) {
Brian O'Connore46492e2013-11-14 21:11:50 -0800147 if(graphEntries.contains(entry)) {
148 graphEntries.remove(entry);
149 skipped++;
150 }
151 else {
152 // remove flow entry from the switch
153 entry.removeFromSwitch(sw);
154 removed++;
155 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700156 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800157 for(FlowEntryWrapper entry : graphEntries) {
Brian O'Connore46492e2013-11-14 21:11:50 -0800158 // add flow entry to switch
159 entry.addToSwitch(sw);
Brian O'Connor321a5d32013-12-09 18:11:35 -0800160 graphEntryTime += entry.dbTime;
161 extractTime += entry.extractTime;
162 pushTime += entry.pushTime;
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800163 added++;
164 }
165 log.debug("Flow entries added "+ added + ", " +
166 "Flow entries removed "+ removed + ", " +
167 "Flow entries skipped " + skipped);
Naoki Shiota2bdda572013-12-09 15:05:21 -0800168
169 return new SyncResult(added, removed, skipped);
Brian O'Connora8e49802013-10-30 20:49:59 -0700170 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800171
Naoki Shiotae3199732013-11-25 16:14:43 -0800172 /**
173 * Read GraphDB to get FlowEntries associated with a switch.
174 * @return set of FlowEntries
175 */
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800176 private Set<FlowEntryWrapper> getFlowEntriesFromGraph() {
177 Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700178 for(IFlowEntry entry : swObj.getFlowEntries()) {
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800179 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
180 entries.add(fe);
Brian O'Connora8e49802013-10-30 20:49:59 -0700181 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800182 return entries;
Brian O'Connora8e49802013-10-30 20:49:59 -0700183 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800184
Naoki Shiotae3199732013-11-25 16:14:43 -0800185 /**
186 * Read flow table from switch and derive FlowEntries from table.
187 * @return set of FlowEntries
188 */
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800189 private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() {
Brian O'Connora8e49802013-10-30 20:49:59 -0700190
191 int lengthU = 0;
192 OFMatch match = new OFMatch();
193 match.setWildcards(OFMatch.OFPFW_ALL);
194
195 OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest();
196 stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE
197 stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES)
198 stat.setMatch(match);
199 List<OFStatistics> stats = new ArrayList<OFStatistics>();
200 stats.add(stat);
201 lengthU += stat.getLength();
202
203 OFStatisticsRequest req = new OFStatisticsRequest();
204 req.setStatisticType(OFStatisticsType.FLOW);
205 req.setStatistics(stats);
206 lengthU += req.getLengthU();
207 req.setLengthU(lengthU);
208
209 List<OFStatistics> entries = null;
210 try {
211 Future<List<OFStatistics>> dfuture = sw.getStatistics(req);
212 entries = dfuture.get();
213 } catch (IOException e) {
214 // TODO Auto-generated catch block
215 e.printStackTrace();
216 } catch (InterruptedException e) {
217 // TODO Auto-generated catch block
218 e.printStackTrace();
219 } catch (ExecutionException e) {
220 // TODO Auto-generated catch block
221 e.printStackTrace();
222 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800223
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800224 Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700225 for(OFStatistics result : entries){
Brian O'Connora8e49802013-10-30 20:49:59 -0700226 OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result;
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800227 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
228 results.add(fe);
Brian O'Connora8e49802013-10-30 20:49:59 -0700229 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800230 return results;
Brian O'Connora8e49802013-10-30 20:49:59 -0700231 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800232
Brian O'Connora8e49802013-10-30 20:49:59 -0700233 }
234
Naoki Shiotae3199732013-11-25 16:14:43 -0800235 /**
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800236 * FlowEntryWrapper represents abstract FlowEntry which is embodied
237 * by FlowEntryId (from GraphDB) or OFFlowStatisticsReply (from switch).
Naoki Shiotae3199732013-11-25 16:14:43 -0800238 * @author Brian
239 *
240 */
Brian O'Connore46492e2013-11-14 21:11:50 -0800241 class FlowEntryWrapper {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800242 FlowEntryId flowEntryId;
Brian O'Connore46492e2013-11-14 21:11:50 -0800243 OFFlowStatisticsReply statisticsReply;
244
245 public FlowEntryWrapper(IFlowEntry entry) {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800246 flowEntryId = new FlowEntryId(entry.getFlowEntryId());
Brian O'Connora8e49802013-10-30 20:49:59 -0700247 }
248
Brian O'Connore46492e2013-11-14 21:11:50 -0800249 public FlowEntryWrapper(OFFlowStatisticsReply entry) {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800250 flowEntryId = new FlowEntryId(entry.getCookie());
Brian O'Connore46492e2013-11-14 21:11:50 -0800251 statisticsReply = entry;
Brian O'Connore46492e2013-11-14 21:11:50 -0800252 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700253
Naoki Shiotae3199732013-11-25 16:14:43 -0800254 /**
255 * Install this FlowEntry to a switch via FlowPusher.
Naoki Shiotab485d412013-11-26 12:04:19 -0800256 * @param sw Switch to which flow will be installed.
Naoki Shiotae3199732013-11-25 16:14:43 -0800257 */
Brian O'Connor321a5d32013-12-09 18:11:35 -0800258 double dbTime, extractTime, pushTime;
Brian O'Connore46492e2013-11-14 21:11:50 -0800259 public void addToSwitch(IOFSwitch sw) {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800260 if (statisticsReply != null) {
261 log.error("Error adding existing flow entry {} to sw {}",
Brian O'Connore46492e2013-11-14 21:11:50 -0800262 statisticsReply.getCookie(), sw.getId());
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800263 return;
Brian O'Connore46492e2013-11-14 21:11:50 -0800264 }
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800265
Brian O'Connor321a5d32013-12-09 18:11:35 -0800266 double startDB = System.nanoTime();
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800267 // Get the Flow Entry state from the Network Graph
268 IFlowEntry iFlowEntry = null;
269 try {
270 iFlowEntry = dbHandler.searchFlowEntry(flowEntryId);
271 } catch (Exception e) {
272 log.error("Error finding flow entry {} in Network Graph",
273 flowEntryId);
274 return;
275 }
276 if (iFlowEntry == null) {
277 log.error("Cannot add flow entry {} to sw {} : flow entry not found",
278 flowEntryId, sw.getId());
279 return;
280 }
Brian O'Connor321a5d32013-12-09 18:11:35 -0800281 dbTime = System.nanoTime() - startDB;
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800282
Brian O'Connor321a5d32013-12-09 18:11:35 -0800283 double startExtract = System.nanoTime();
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -0800284 FlowEntry flowEntry =
285 FlowDatabaseOperation.extractFlowEntry(iFlowEntry);
286 if (flowEntry == null) {
287 log.error("Cannot add flow entry {} to sw {} : flow entry cannot be extracted",
288 flowEntryId, sw.getId());
289 return;
290 }
Brian O'Connor321a5d32013-12-09 18:11:35 -0800291 extractTime = System.nanoTime() - startExtract;
292
293 double startPush = System.nanoTime();
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800294 pusher.pushFlowEntry(sw, flowEntry);
Brian O'Connor321a5d32013-12-09 18:11:35 -0800295 pushTime = System.nanoTime() - startPush;
Brian O'Connore46492e2013-11-14 21:11:50 -0800296 }
297
Naoki Shiotae3199732013-11-25 16:14:43 -0800298 /**
299 * Remove this FlowEntry from a switch via FlowPusher.
Naoki Shiotab485d412013-11-26 12:04:19 -0800300 * @param sw Switch from which flow will be removed.
Naoki Shiotae3199732013-11-25 16:14:43 -0800301 */
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800302 public void removeFromSwitch(IOFSwitch sw) {
303 if (statisticsReply == null) {
304 log.error("Error removing non-existent flow entry {} from sw {}",
305 flowEntryId, sw.getId());
306 return;
307 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700308
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800309 // Convert Statistics Reply to Flow Mod, then write it
310 OFFlowMod fm = new OFFlowMod();
311 fm.setCookie(statisticsReply.getCookie());
312 fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT);
313 fm.setLengthU(OFFlowMod.MINIMUM_LENGTH);
314 fm.setMatch(statisticsReply.getMatch());
315 fm.setPriority(statisticsReply.getPriority());
316 fm.setOutPort(OFPort.OFPP_NONE);
317
318 pusher.add(sw, fm);
Brian O'Connore46492e2013-11-14 21:11:50 -0800319 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700320
Brian O'Connore46492e2013-11-14 21:11:50 -0800321 /**
322 * Return the hash code of the Flow Entry ID
323 */
324 @Override
325 public int hashCode() {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800326 return flowEntryId.hashCode();
Brian O'Connore46492e2013-11-14 21:11:50 -0800327 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700328
Brian O'Connore46492e2013-11-14 21:11:50 -0800329 /**
330 * Returns true of the object is another Flow Entry ID with
331 * the same value; otherwise, returns false.
332 *
333 * @param Object to compare
Naoki Shiotab485d412013-11-26 12:04:19 -0800334 * @return true if the object has the same Flow Entry ID.
Brian O'Connore46492e2013-11-14 21:11:50 -0800335 */
336 @Override
337 public boolean equals(Object obj){
338 if(obj.getClass() == this.getClass()) {
339 FlowEntryWrapper entry = (FlowEntryWrapper) obj;
340 // TODO: we need to actually compare the match + actions
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800341 return this.flowEntryId.equals(entry.flowEntryId);
Brian O'Connore46492e2013-11-14 21:11:50 -0800342 }
343 return false;
344 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800345
Brian O'Connore46492e2013-11-14 21:11:50 -0800346 @Override
347 public String toString() {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800348 return flowEntryId.toString();
Brian O'Connore46492e2013-11-14 21:11:50 -0800349 }
350 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800351}