blob: b0de5bf27f0019fe0c67a2789f2dc17e085e1f24 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.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
Jonathan Harta99ec672014-04-03 11:30:34 -070015import net.floodlightcontroller.core.IOFSwitch;
16import net.onrc.onos.core.flowprogrammer.IFlowPusherService.MsgPriority;
Jonathan Harta99ec672014-04-03 11:30:34 -070017import net.onrc.onos.core.util.FlowEntryId;
18
Brian O'Connor0d6ba512013-11-05 15:17:44 -080019import org.openflow.protocol.OFFlowMod;
Brian O'Connora8e49802013-10-30 20:49:59 -070020import org.openflow.protocol.OFMatch;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080021import org.openflow.protocol.OFPort;
Brian O'Connora8e49802013-10-30 20:49:59 -070022import org.openflow.protocol.OFStatisticsRequest;
23import org.openflow.protocol.statistics.OFFlowStatisticsReply;
24import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
25import org.openflow.protocol.statistics.OFStatistics;
26import org.openflow.protocol.statistics.OFStatisticsType;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Naoki Shiotae3199732013-11-25 16:14:43 -080030/**
Naoki Shiotab485d412013-11-26 12:04:19 -080031 * FlowSynchronizer is an implementation of FlowSyncService.
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -080032 * In addition to IFlowSyncService, FlowSynchronizer periodically reads flow
33 * tables from switches and compare them with GraphDB to drop unnecessary
34 * flows and/or to install missing flows.
Naoki Shiotae3199732013-11-25 16:14:43 -080035 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080036public class FlowSynchronizer implements IFlowSyncService {
Brian O'Connora8e49802013-10-30 20:49:59 -070037
Brian O'Connorea1efbe2013-11-25 22:57:43 -080038 private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class);
Brian O'Connore46492e2013-11-14 21:11:50 -080039
Pavlin Radoslavov4cf8ee52014-03-26 18:19:58 -070040 // TODO: fix when FlowSynchronizer is refactored
Pavlin Radoslavov697f6982014-03-26 15:55:19 -070041 // private DBOperation dbHandler;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080042 protected IFlowPusherService pusher;
Ray Milkey8e5170e2014-04-02 12:09:55 -070043 private Map<IOFSwitch, FutureTask<SyncResult>> switchThreads;
Brian O'Connore46492e2013-11-14 21:11:50 -080044
45 public FlowSynchronizer() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070046 // TODO: fix when FlowSynchronizer is refactored
47 // dbHandler = GraphDBManager.getDBOperation();
48 switchThreads = new HashMap<IOFSwitch, FutureTask<SyncResult>>();
Brian O'Connore46492e2013-11-14 21:11:50 -080049 }
50
Brian O'Connorea1efbe2013-11-25 22:57:43 -080051 @Override
Naoki Shiota2bdda572013-12-09 15:05:21 -080052 public Future<SyncResult> synchronize(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070053 Synchronizer sync = new Synchronizer(sw);
54 FutureTask<SyncResult> task = new FutureTask<SyncResult>(sync);
55 switchThreads.put(sw, task);
56 task.run();
57 return task;
Brian O'Connore46492e2013-11-14 21:11:50 -080058 }
Ray Milkey8e5170e2014-04-02 12:09:55 -070059
Brian O'Connore46492e2013-11-14 21:11:50 -080060 @Override
Brian O'Connorea1efbe2013-11-25 22:57:43 -080061 public void interrupt(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070062 FutureTask<SyncResult> t = switchThreads.remove(sw);
63 if (t != null) {
64 t.cancel(true);
65 }
Brian O'Connore46492e2013-11-14 21:11:50 -080066 }
67
Naoki Shiotab485d412013-11-26 12:04:19 -080068 /**
69 * Initialize Synchronizer.
Ray Milkey8e5170e2014-04-02 12:09:55 -070070 *
Naoki Shiotab485d412013-11-26 12:04:19 -080071 * @param pusherService FlowPusherService used for sending messages.
72 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080073 public void init(IFlowPusherService pusherService) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070074 pusher = pusherService;
Brian O'Connore46492e2013-11-14 21:11:50 -080075 }
76
Naoki Shiotae3199732013-11-25 16:14:43 -080077 /**
78 * Synchronizer represents main thread of synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080079 */
Ray Milkey8e5170e2014-04-02 12:09:55 -070080 protected class Synchronizer implements Callable<SyncResult> {
81 IOFSwitch sw;
82 // TODO: fix when FlowSynchronizer is refactored
83 // ISwitchObject swObj;
Brian O'Connore46492e2013-11-14 21:11:50 -080084
Ray Milkey8e5170e2014-04-02 12:09:55 -070085 public Synchronizer(IOFSwitch sw) {
86 this.sw = sw;
Ray Milkey8e5170e2014-04-02 12:09:55 -070087 // TODO: fix when FlowSynchronizer is refactored
Pavlin Radoslavov3255ae52014-04-09 13:01:54 -070088 // Dpid dpid = new Dpid(sw.getId());
Ray Milkey8e5170e2014-04-02 12:09:55 -070089 // this.swObj = dbHandler.searchSwitch(dpid.toString());
90 }
Brian O'Connore46492e2013-11-14 21:11:50 -080091
Ray Milkey8e5170e2014-04-02 12:09:55 -070092 double graphIDTime, switchTime, compareTime, graphEntryTime, extractTime, pushTime, totalTime;
Brian O'Connore46492e2013-11-14 21:11:50 -080093
Ray Milkey8e5170e2014-04-02 12:09:55 -070094 @Override
95 public SyncResult call() {
96 pusher.suspend(sw);
97 try {
98 long start = System.nanoTime();
99 Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph();
100 long step1 = System.nanoTime();
101 Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch();
102 if (switchEntries == null) {
103 log.debug("getFlowEntriesFromSwitch() failed");
104 return null;
105 }
106 long step2 = System.nanoTime();
107 SyncResult result = compare(graphEntries, switchEntries);
108 long step3 = System.nanoTime();
109 graphIDTime = (step1 - start);
110 switchTime = (step2 - step1);
111 compareTime = (step3 - step2);
112 totalTime = (step3 - start);
113 outputTime();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800114
Ray Milkey8e5170e2014-04-02 12:09:55 -0700115 return result;
116 } finally {
117 pusher.resume(sw);
118 }
119 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800120
Ray Milkey8e5170e2014-04-02 12:09:55 -0700121 private void outputTime() {
122 double div = Math.pow(10, 6); //convert nanoseconds to ms
123 graphIDTime /= div;
124 switchTime /= div;
125 compareTime = (compareTime - graphEntryTime - extractTime - pushTime) / div;
126 graphEntryTime /= div;
127 extractTime /= div;
128 pushTime /= div;
129 totalTime /= div;
130 log.debug("Sync time (ms):{},{},{},{},{},{},{}"
131 , graphIDTime
132 , switchTime
133 , compareTime
134 , graphEntryTime
135 , extractTime
136 , pushTime
137 , totalTime);
138 }
Pavlin Radoslavov4cf8ee52014-03-26 18:19:58 -0700139
Ray Milkey8e5170e2014-04-02 12:09:55 -0700140 /**
141 * Compare flows entries in GraphDB and switch to pick up necessary
142 * messages.
143 * After picking up, picked messages are added to FlowPusher.
144 *
145 * @param graphEntries Flow entries in GraphDB.
146 * @param switchEntries Flow entries in switch.
147 */
148 private SyncResult compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) {
149 int added = 0, removed = 0, skipped = 0;
150 for (FlowEntryWrapper entry : switchEntries) {
151 if (graphEntries.contains(entry)) {
152 graphEntries.remove(entry);
153 skipped++;
154 } else {
155 // remove flow entry from the switch
156 entry.removeFromSwitch(sw);
157 removed++;
158 }
159 }
160 for (FlowEntryWrapper entry : graphEntries) {
161 // add flow entry to switch
162 entry.addToSwitch(sw);
163 graphEntryTime += entry.dbTime;
164 extractTime += entry.extractTime;
165 pushTime += entry.pushTime;
166 added++;
167 }
168 log.debug("Flow entries added {}, " +
169 "Flow entries removed {}, " +
170 "Flow entries skipped {}"
171 , added
172 , removed
173 , skipped);
Brian O'Connore46492e2013-11-14 21:11:50 -0800174
Ray Milkey8e5170e2014-04-02 12:09:55 -0700175 return new SyncResult(added, removed, skipped);
176 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700177
Ray Milkey8e5170e2014-04-02 12:09:55 -0700178 /**
179 * Read GraphDB to get FlowEntries associated with a switch.
180 *
181 * @return set of FlowEntries
182 */
183 private Set<FlowEntryWrapper> getFlowEntriesFromGraph() {
184 Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700185
Ray Milkey8e5170e2014-04-02 12:09:55 -0700186 // TODO: fix when FlowSynchronizer is refactored
187 /*
188 for(IFlowEntry entry : swObj.getFlowEntries()) {
189 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
190 entries.add(fe);
191 }
192 */
193 return entries;
194 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700195
Ray Milkey8e5170e2014-04-02 12:09:55 -0700196 /**
197 * Read flow table from switch and derive FlowEntries from table.
198 *
199 * @return set of FlowEntries
200 */
201 private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() {
Brian O'Connora8e49802013-10-30 20:49:59 -0700202
Ray Milkey8e5170e2014-04-02 12:09:55 -0700203 int lengthU = 0;
204 OFMatch match = new OFMatch();
205 match.setWildcards(OFMatch.OFPFW_ALL);
Brian O'Connore46492e2013-11-14 21:11:50 -0800206
Ray Milkey8e5170e2014-04-02 12:09:55 -0700207 OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest();
208 stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE
209 stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES)
210 stat.setMatch(match);
211 List<OFStatistics> stats = new ArrayList<OFStatistics>();
212 stats.add(stat);
213 lengthU += stat.getLength();
214
215 OFStatisticsRequest req = new OFStatisticsRequest();
216 req.setStatisticType(OFStatisticsType.FLOW);
217 req.setStatistics(stats);
218 lengthU += req.getLengthU();
219 req.setLengthU(lengthU);
220
221 List<OFStatistics> entries = null;
222 try {
223 Future<List<OFStatistics>> dfuture = sw.getStatistics(req);
224 entries = dfuture.get();
225 } catch (IOException e) {
226 // TODO Auto-generated catch block
227 e.printStackTrace();
228 return null;
229 } catch (InterruptedException e) {
230 // TODO Auto-generated catch block
231 e.printStackTrace();
232 return null;
233 } catch (ExecutionException e) {
234 // TODO Auto-generated catch block
235 e.printStackTrace();
236 return null;
237 }
238
239 Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>();
240 for (OFStatistics result : entries) {
241 OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result;
242 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
243 results.add(fe);
244 }
245 return results;
246 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800247
Brian O'Connora8e49802013-10-30 20:49:59 -0700248 }
249
Naoki Shiotae3199732013-11-25 16:14:43 -0800250 /**
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800251 * FlowEntryWrapper represents abstract FlowEntry which is embodied
252 * by FlowEntryId (from GraphDB) or OFFlowStatisticsReply (from switch).
Naoki Shiotae3199732013-11-25 16:14:43 -0800253 */
Brian O'Connore46492e2013-11-14 21:11:50 -0800254 class FlowEntryWrapper {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700255 FlowEntryId flowEntryId;
256 // TODO: fix when FlowSynchronizer is refactored
257 // IFlowEntry iFlowEntry;
258 OFFlowStatisticsReply statisticsReply;
Naoki Shiotaf74d5f32014-01-09 21:29:38 -0800259
Brian O'Connore46492e2013-11-14 21:11:50 -0800260
Ray Milkey8e5170e2014-04-02 12:09:55 -0700261 // TODO: fix when FlowSynchronizer is refactored
262 /*
263 public FlowEntryWrapper(IFlowEntry entry) {
264 flowEntryId = new FlowEntryId(entry.getFlowEntryId());
265 iFlowEntry = entry;
266 }
267 */
Brian O'Connora8e49802013-10-30 20:49:59 -0700268
Ray Milkey8e5170e2014-04-02 12:09:55 -0700269 public FlowEntryWrapper(OFFlowStatisticsReply entry) {
270 flowEntryId = new FlowEntryId(entry.getCookie());
271 statisticsReply = entry;
272 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700273
Ray Milkey8e5170e2014-04-02 12:09:55 -0700274 /**
275 * Install this FlowEntry to a switch via FlowPusher.
276 *
277 * @param sw Switch to which flow will be installed.
278 */
279 double dbTime, extractTime, pushTime;
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800280
Ray Milkey8e5170e2014-04-02 12:09:55 -0700281 public void addToSwitch(IOFSwitch sw) {
282 if (statisticsReply != null) {
283 log.error("Error adding existing flow entry {} to sw {}",
284 statisticsReply.getCookie(), sw.getId());
285 return;
Naoki Shiotaf74d5f32014-01-09 21:29:38 -0800286 }
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800287
Ray Milkey8e5170e2014-04-02 12:09:55 -0700288 double startDB = System.nanoTime();
289 // Get the Flow Entry state from the Network Graph
290 // TODO: fix when FlowSynchronizer is refactored
291 /*
292 if (iFlowEntry == null) {
293 try {
294 // TODO: fix when FlowSynchronizer is refactored
295 iFlowEntry = dbHandler.searchFlowEntry(flowEntryId);
296 } catch (Exception e) {
297 log.error("Error finding flow entry {} in Network Graph",
298 flowEntryId);
299 return;
300 }
301 }
302 */
303 dbTime = System.nanoTime() - startDB;
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800304
Ray Milkey8e5170e2014-04-02 12:09:55 -0700305 //
306 // TODO: The old FlowDatabaseOperation class is gone, so the code
307 //
308 /*
309 double startExtract = System.nanoTime();
310 FlowEntry flowEntry =
311 FlowDatabaseOperation.extractFlowEntry(iFlowEntry);
312 if (flowEntry == null) {
313 log.error("Cannot add flow entry {} to sw {} : flow entry cannot be extracted",
314 flowEntryId, sw.getId());
315 return;
316 }
317 extractTime = System.nanoTime() - startExtract;
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800318
Ray Milkey8e5170e2014-04-02 12:09:55 -0700319 double startPush = System.nanoTime();
320 pusher.pushFlowEntry(sw, flowEntry, MsgPriority.HIGH);
321 pushTime = System.nanoTime() - startPush;
322 */
323 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700324
Ray Milkey8e5170e2014-04-02 12:09:55 -0700325 /**
326 * Remove this FlowEntry from a switch via FlowPusher.
327 *
328 * @param sw Switch from which flow will be removed.
329 */
330 public void removeFromSwitch(IOFSwitch sw) {
331 if (statisticsReply == null) {
332 log.error("Error removing non-existent flow entry {} from sw {}",
333 flowEntryId, sw.getId());
334 return;
335 }
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800336
Ray Milkey8e5170e2014-04-02 12:09:55 -0700337 // Convert Statistics Reply to Flow Mod, then write it
338 OFFlowMod fm = new OFFlowMod();
339 fm.setCookie(statisticsReply.getCookie());
340 fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT);
341 fm.setLengthU(OFFlowMod.MINIMUM_LENGTH);
342 fm.setMatch(statisticsReply.getMatch());
343 fm.setPriority(statisticsReply.getPriority());
344 fm.setOutPort(OFPort.OFPP_NONE);
Brian O'Connora8e49802013-10-30 20:49:59 -0700345
Ray Milkey8e5170e2014-04-02 12:09:55 -0700346 pusher.add(sw, fm, MsgPriority.HIGH);
347 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700348
Ray Milkey8e5170e2014-04-02 12:09:55 -0700349 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700350 * Return the hash code of the Flow Entry ID.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700351 */
352 @Override
353 public int hashCode() {
354 return flowEntryId.hashCode();
355 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800356
Ray Milkey8e5170e2014-04-02 12:09:55 -0700357 /**
358 * Returns true of the object is another Flow Entry ID with
359 * the same value; otherwise, returns false.
360 *
Sho SHIMIZUa1199fa2014-06-10 18:11:12 -0700361 * @param obj to compare
Ray Milkey8e5170e2014-04-02 12:09:55 -0700362 * @return true if the object has the same Flow Entry ID.
363 */
364 @Override
365 public boolean equals(Object obj) {
366 if (obj != null && obj.getClass() == this.getClass()) {
367 FlowEntryWrapper entry = (FlowEntryWrapper) obj;
368 // TODO: we need to actually compare the match + actions
369 return this.flowEntryId.equals(entry.flowEntryId);
370 }
371 return false;
372 }
373
374 @Override
375 public String toString() {
376 return flowEntryId.toString();
377 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800378 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800379}