Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 2 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 4 | import java.util.HashMap; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 5 | import java.util.Map; |
| 6 | import java.util.Queue; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 7 | |
| 8 | import org.openflow.protocol.OFMessage; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 9 | |
| 10 | import net.floodlightcontroller.core.FloodlightContext; |
| 11 | import net.floodlightcontroller.core.IOFSwitch; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * FlowPusher intermediates flow_mod sent from FlowManager/FlowSync to switches. |
| 15 | * FlowPusher controls the rate of sending flow_mods so that connection doesn't overflow. |
| 16 | * @author Naoki Shiota |
| 17 | * |
| 18 | */ |
| 19 | public class FlowPusher { |
| 20 | private FloodlightContext context; |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 21 | private FlowQueueTable flowQueueTable = null; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 22 | private Thread thread; |
| 23 | |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Represents state of queue. |
| 26 | * This is used for calculation of rate. |
| 27 | * @author Naoki Shiota |
| 28 | * |
| 29 | */ |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 30 | private static class RateInfo { |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 31 | long last_sent_time = 0; |
| 32 | long last_sent_size = 0; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 33 | } |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 34 | |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 35 | private Map<Long, RateInfo> queue_rateinfos = |
| 36 | new HashMap<Long, RateInfo>(); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 37 | |
| 38 | private class FlowPusherProcess implements Runnable { |
| 39 | @Override |
| 40 | public void run() { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 41 | if (flowQueueTable == null) { |
| 42 | return; |
| 43 | } |
| 44 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 45 | while (true) { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 46 | for (IOFSwitch sw : flowQueueTable.getSwitches()) { |
| 47 | // Skip if queue is suspended |
| 48 | if (flowQueueTable.isQueueSusupended(sw)) { |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | // Skip if queue is locked |
| 53 | if (! flowQueueTable.lockQueueIfAvailable(sw)) { |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | long dpid = sw.getId(); |
| 58 | Queue<OFMessage> queue = flowQueueTable.getQueue(sw); |
| 59 | |
| 60 | if (queue == null) { |
| 61 | flowQueueTable.unlockQueue(sw); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 62 | continue; |
| 63 | } |
| 64 | |
| 65 | OFMessage msg = queue.poll(); |
| 66 | if (msg == null) { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 67 | flowQueueTable.unlockQueue(sw); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 68 | continue; |
| 69 | } |
| 70 | |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 71 | RateInfo state = queue_rateinfos.get(dpid); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 72 | if (state == null) { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 73 | queue_rateinfos.put(dpid, new RateInfo()); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // check sending rate and determine it to be sent or not |
| 77 | long current_time = System.nanoTime(); |
| 78 | long rate = state.last_sent_size / (current_time - state.last_sent_time); |
| 79 | |
| 80 | // if need to send, call IOFSwitch#write() |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 81 | if (rate < flowQueueTable.getQueueRate(sw)) { |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 82 | try { |
| 83 | sw.write(msg, context); |
| 84 | state.last_sent_time = current_time; |
| 85 | state.last_sent_size = msg.getLengthU(); |
| 86 | } catch (IOException e) { |
| 87 | // TODO Auto-generated catch block |
| 88 | e.printStackTrace(); |
| 89 | } |
| 90 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 91 | |
| 92 | flowQueueTable.unlockQueue(sw); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // sleep while all queues are empty |
| 96 | boolean sleep = true; |
| 97 | do { |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 98 | // TODO check if queues are empty |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 99 | } while (sleep); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 104 | public FlowPusher(FlowQueueTable table, FloodlightContext context) { |
| 105 | flowQueueTable = table; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 106 | this.context = context; |
| 107 | } |
| 108 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 109 | public void startProcess() { |
| 110 | thread = new Thread(new FlowPusherProcess()); |
| 111 | thread.start(); |
| 112 | } |
| 113 | |
| 114 | public void stopProcess() { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 115 | if (thread != null && thread.isAlive()) { |
| 116 | // TODO tell thread to halt |
| 117 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 118 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 119 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 120 | } |