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