Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.flowcache; |
| 2 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 6 | import java.util.Collections; |
| 7 | import java.util.Comparator; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 8 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 9 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 10 | import java.util.LinkedList; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 11 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 12 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 13 | import java.util.Random; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 14 | import java.util.concurrent.Executors; |
| 15 | import java.util.concurrent.ScheduledExecutorService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 16 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 17 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 19 | import net.floodlightcontroller.core.INetMapStorage; |
| 20 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 21 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
Pankaj Berde | d007974 | 2013-03-27 17:53:25 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 26 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 27 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 28 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 29 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 30 | import net.floodlightcontroller.flowcache.web.FlowWebRoutable; |
| 31 | import net.floodlightcontroller.restserver.IRestApiService; |
| 32 | import net.floodlightcontroller.util.CallerId; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 33 | import net.floodlightcontroller.util.DataPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 34 | import net.floodlightcontroller.util.DataPathEndpoints; |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 35 | import net.floodlightcontroller.util.Dpid; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 36 | import net.floodlightcontroller.util.FlowEntry; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 37 | import net.floodlightcontroller.util.FlowEntryAction; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 38 | import net.floodlightcontroller.util.FlowEntryId; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 39 | import net.floodlightcontroller.util.FlowEntryMatch; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 40 | import net.floodlightcontroller.util.FlowEntrySwitchState; |
| 41 | import net.floodlightcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 42 | import net.floodlightcontroller.util.FlowId; |
| 43 | import net.floodlightcontroller.util.FlowPath; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 44 | import net.floodlightcontroller.util.IPv4Net; |
| 45 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 46 | import net.floodlightcontroller.util.OFMessageDamper; |
| 47 | import net.floodlightcontroller.util.Port; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 48 | import net.floodlightcontroller.util.SwitchPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 49 | import net.onrc.onos.util.GraphDBConnection; |
| 50 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
| 51 | |
| 52 | import org.openflow.protocol.OFFlowMod; |
| 53 | import org.openflow.protocol.OFMatch; |
| 54 | import org.openflow.protocol.OFPacketOut; |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 55 | import org.openflow.protocol.OFPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 56 | import org.openflow.protocol.OFType; |
| 57 | import org.openflow.protocol.action.OFAction; |
| 58 | import org.openflow.protocol.action.OFActionOutput; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 62 | |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 63 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 64 | |
| 65 | public GraphDBConnection conn; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 66 | |
| 67 | protected IRestApiService restApi; |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 68 | protected volatile IFloodlightProviderService floodlightProvider; |
| 69 | protected volatile ITopoRouteService topoRouteService; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 70 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 71 | |
| 72 | protected OFMessageDamper messageDamper; |
| 73 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 74 | // |
| 75 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 76 | // The local copy should go away! |
| 77 | // |
| 78 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 79 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
| 80 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 81 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 82 | public static final short PRIORITY_DEFAULT = 100; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 83 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 84 | // Flow Entry ID generation state |
| 85 | private static Random randomGenerator = new Random(); |
| 86 | private static int nextFlowEntryIdPrefix = 0; |
| 87 | private static int nextFlowEntryIdSuffix = 0; |
| 88 | private static long nextFlowEntryId = 0; |
| 89 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 90 | private static long measurementFlowId = 100000; |
| 91 | private static String measurementFlowIdStr = "0x186a0"; // 100000 |
| 92 | private long modifiedMeasurementFlowTime = 0; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 93 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 94 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 95 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 96 | |
| 97 | // The periodic task(s) |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 98 | private ScheduledExecutorService mapReaderScheduler; |
| 99 | private ScheduledExecutorService shortestPathReconcileScheduler; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 100 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 101 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 102 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 103 | try { |
| 104 | runImpl(); |
| 105 | } catch (Exception e) { |
| 106 | log.debug("Exception processing All Flow Entries from the Network MAP: ", e); |
| 107 | conn.endTx(Transaction.ROLLBACK); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private void runImpl() { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 113 | long startTime = System.nanoTime(); |
| 114 | int counterAllFlowEntries = 0; |
| 115 | int counterMyNotUpdatedFlowEntries = 0; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 116 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 117 | if (floodlightProvider == null) { |
| 118 | log.debug("FloodlightProvider service not found!"); |
| 119 | return; |
| 120 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 121 | Map<Long, IOFSwitch> mySwitches = |
| 122 | floodlightProvider.getSwitches(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 123 | LinkedList<IFlowEntry> addFlowEntries = |
| 124 | new LinkedList<IFlowEntry>(); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 125 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 126 | new LinkedList<IFlowEntry>(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 127 | |
| 128 | // |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 129 | // Fetch all Flow Entries and select only my Flow Entries |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 130 | // that need to be updated into the switches. |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 131 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 132 | boolean processed_measurement_flow = false; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 133 | Iterable<IFlowEntry> allFlowEntries = |
| 134 | conn.utils().getAllFlowEntries(conn); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 135 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 136 | counterAllFlowEntries++; |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 137 | String switchState = flowEntryObj.getSwitchState(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 138 | if ((switchState == null) || |
| 139 | (! switchState.equals("FE_SWITCH_NOT_UPDATED"))) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 140 | continue; // Ignore the entry: nothing to do |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 141 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 142 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 143 | String dpidStr = flowEntryObj.getSwitchDpid(); |
| 144 | if (dpidStr == null) |
| 145 | continue; |
| 146 | Dpid dpid = new Dpid(dpidStr); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 147 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 148 | if (mySwitch == null) |
| 149 | continue; // Ignore the entry: not my switch |
| 150 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 151 | IFlowPath flowObj = |
| 152 | conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj); |
| 153 | if (flowObj == null) |
| 154 | continue; // Should NOT happen |
| 155 | if (flowObj.getFlowId() == null) |
| 156 | continue; // Invalid entry |
| 157 | |
| 158 | // |
| 159 | // NOTE: For now we process the DELETE before the ADD |
| 160 | // to cover the more common scenario. |
| 161 | // TODO: This is error prone and needs to be fixed! |
| 162 | // |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 163 | String userState = flowEntryObj.getUserState(); |
| 164 | if (userState == null) |
| 165 | continue; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 166 | if (userState.equals("FE_USER_DELETE")) { |
| 167 | // An entry that needs to be deleted. |
| 168 | deleteFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 169 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
| 170 | } else { |
| 171 | addFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 172 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 173 | counterMyNotUpdatedFlowEntries++; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 174 | // Code for measurement purpose |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 175 | // TODO: Commented-out for now |
| 176 | /* |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 177 | { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 178 | if (flowObj.getFlowId().equals(measurementFlowIdStr)) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 179 | processed_measurement_flow = true; |
| 180 | } |
| 181 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 182 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 185 | // |
| 186 | // Process the Flow Entries that need to be added |
| 187 | // |
| 188 | for (IFlowEntry flowEntryObj : addFlowEntries) { |
| 189 | IFlowPath flowObj = |
| 190 | conn.utils().getFlowPathByFlowEntry(conn, |
| 191 | flowEntryObj); |
| 192 | if (flowObj == null) |
| 193 | continue; // Should NOT happen |
| 194 | if (flowObj.getFlowId() == null) |
| 195 | continue; // Invalid entry |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 196 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 197 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 198 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 199 | if (mySwitch == null) |
| 200 | continue; // Shouldn't happen |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 201 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 202 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 203 | |
| 204 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 205 | // Delete all Flow Entries marked for deletion from the |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 206 | // Network MAP. |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 207 | // |
| 208 | // TODO: We should use the OpenFlow Barrier mechanism |
| 209 | // to check for errors, and delete the Flow Entries after the |
| 210 | // Barrier message is received. |
| 211 | // |
| 212 | while (! deleteFlowEntries.isEmpty()) { |
| 213 | IFlowEntry flowEntryObj = deleteFlowEntries.poll(); |
| 214 | IFlowPath flowObj = |
| 215 | conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj); |
| 216 | if (flowObj == null) { |
| 217 | log.debug("Did not find FlowPath to be deleted"); |
| 218 | continue; |
| 219 | } |
| 220 | flowObj.removeFlowEntry(flowEntryObj); |
| 221 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 222 | } |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 223 | |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 224 | conn.endTx(Transaction.COMMIT); |
| 225 | |
| 226 | if (processed_measurement_flow) { |
| 227 | long estimatedTime = |
| 228 | System.nanoTime() - modifiedMeasurementFlowTime; |
| 229 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 230 | (double)estimatedTime / 1000000000 + " sec"; |
| 231 | log.debug(logMsg); |
| 232 | } |
| 233 | |
| 234 | long estimatedTime = System.nanoTime() - startTime; |
| 235 | double rate = 0.0; |
| 236 | if (estimatedTime > 0) |
| 237 | rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime; |
| 238 | String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + |
| 239 | counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + |
| 240 | counterMyNotUpdatedFlowEntries + " in " + |
| 241 | (double)estimatedTime / 1000000000 + " sec: " + |
| 242 | rate + " paths/s"; |
| 243 | log.debug(logMsg); |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | final Runnable shortestPathReconcile = new Runnable() { |
| 248 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 249 | try { |
| 250 | runImpl(); |
| 251 | } catch (Exception e) { |
| 252 | log.debug("Exception processing All Flows from the Network MAP: ", e); |
| 253 | conn.endTx(Transaction.ROLLBACK); |
| 254 | return; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private void runImpl() { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 259 | long startTime = System.nanoTime(); |
| 260 | int counterAllFlowPaths = 0; |
| 261 | int counterMyFlowPaths = 0; |
| 262 | |
| 263 | if (floodlightProvider == null) { |
| 264 | log.debug("FloodlightProvider service not found!"); |
| 265 | return; |
| 266 | } |
| 267 | Map<Long, IOFSwitch> mySwitches = |
| 268 | floodlightProvider.getSwitches(); |
| 269 | LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>(); |
| 270 | |
| 271 | boolean processed_measurement_flow = false; |
| 272 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 273 | // |
| 274 | // Fetch and recompute the Shortest Path for those |
| 275 | // Flow Paths this controller is responsible for. |
| 276 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 277 | topoRouteService.prepareShortestPathTopo(); |
| 278 | Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 279 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 280 | counterAllFlowPaths++; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 281 | if (flowPathObj == null) |
| 282 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 283 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 284 | String srcDpidStr = flowPathObj.getSrcSwitch(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 285 | if (srcDpidStr == null) |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 286 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 287 | Dpid srcDpid = new Dpid(srcDpidStr); |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 288 | // |
| 289 | // Use the source DPID as a heuristic to decide |
| 290 | // which controller is responsible for maintaining the |
| 291 | // shortest path. |
| 292 | // NOTE: This heuristic is error-prone: if the switch |
| 293 | // goes away and no controller is responsible for that |
| 294 | // switch, then the original Flow Path is not cleaned-up |
| 295 | // |
| 296 | IOFSwitch mySwitch = mySwitches.get(srcDpid.value()); |
| 297 | if (mySwitch == null) |
| 298 | continue; // Ignore: not my responsibility |
| 299 | |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 300 | // Test the Data Path Summary string |
| 301 | String dataPathSummaryStr = flowPathObj.getDataPathSummary(); |
| 302 | if (dataPathSummaryStr == null) |
| 303 | continue; // Could be invalid entry? |
| 304 | if (dataPathSummaryStr.isEmpty()) |
| 305 | continue; // No need to maintain this flow |
| 306 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 307 | // |
| 308 | // Test whether we need to complete the Flow cleanup, |
| 309 | // if the Flow has been deleted by the user. |
| 310 | // |
| 311 | String flowUserState = flowPathObj.getUserState(); |
| 312 | if ((flowUserState != null) |
| 313 | && flowUserState.equals("FE_USER_DELETE")) { |
| 314 | Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries(); |
| 315 | boolean empty = true; // TODO: an ugly hack |
| 316 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 317 | empty = false; |
| 318 | break; |
| 319 | } |
| 320 | if (empty) |
| 321 | deleteFlows.add(flowPathObj); |
| 322 | } |
| 323 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 324 | // Fetch the fields needed to recompute the shortest path |
| 325 | Short srcPortShort = flowPathObj.getSrcPort(); |
| 326 | String dstDpidStr = flowPathObj.getDstSwitch(); |
| 327 | Short dstPortShort = flowPathObj.getDstPort(); |
| 328 | if ((srcPortShort == null) || |
| 329 | (dstDpidStr == null) || |
| 330 | (dstPortShort == null)) { |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | Port srcPort = new Port(srcPortShort); |
| 335 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 336 | Port dstPort = new Port(dstPortShort); |
| 337 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 338 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 339 | |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 340 | counterMyFlowPaths++; |
| 341 | |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 342 | // |
| 343 | // NOTE: Using here the regular getShortestPath() method |
| 344 | // won't work here, because that method calls internally |
| 345 | // "conn.endTx(Transaction.COMMIT)", and that will |
| 346 | // invalidate all handlers to the Titan database. |
| 347 | // If we want to experiment with calling here |
| 348 | // getShortestPath(), we need to refactor that code |
| 349 | // to avoid closing the transaction. |
| 350 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 351 | DataPath dataPath = |
| 352 | topoRouteService.getTopoShortestPath(srcSwitchPort, |
| 353 | dstSwitchPort); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 354 | if (dataPath == null) { |
| 355 | // We need the DataPath to compare the paths |
| 356 | dataPath = new DataPath(); |
| 357 | dataPath.setSrcPort(srcSwitchPort); |
| 358 | dataPath.setDstPort(dstSwitchPort); |
| 359 | } |
| 360 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 361 | String newDataPathSummaryStr = dataPath.dataPathSummary(); |
| 362 | if (dataPathSummaryStr.equals(newDataPathSummaryStr)) |
| 363 | continue; // Nothing changed |
| 364 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 365 | reconcileFlow(flowPathObj, dataPath); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 366 | } |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 367 | |
| 368 | // |
| 369 | // Delete all leftover Flows marked for deletion from the |
| 370 | // Network MAP. |
| 371 | // |
| 372 | while (! deleteFlows.isEmpty()) { |
| 373 | IFlowPath flowPathObj = deleteFlows.poll(); |
| 374 | conn.utils().removeFlowPath(conn, flowPathObj); |
| 375 | } |
| 376 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 377 | topoRouteService.dropShortestPathTopo(); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 378 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 379 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 380 | |
| 381 | if (processed_measurement_flow) { |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 382 | long estimatedTime = |
| 383 | System.nanoTime() - modifiedMeasurementFlowTime; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 384 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 385 | (double)estimatedTime / 1000000000 + " sec"; |
| 386 | log.debug(logMsg); |
| 387 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 388 | |
| 389 | long estimatedTime = System.nanoTime() - startTime; |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 390 | double rate = 0.0; |
| 391 | if (estimatedTime > 0) |
| 392 | rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 393 | String logMsg = "MEASUREMENT: Processed AllFlowPaths: " + |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 394 | counterAllFlowPaths + " MyFlowPaths: " + |
| 395 | counterMyFlowPaths + " in " + |
| 396 | (double)estimatedTime / 1000000000 + " sec: " + |
| 397 | rate + " paths/s"; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 398 | log.debug(logMsg); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 399 | } |
| 400 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 401 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 402 | //final ScheduledFuture<?> mapReaderHandle = |
| 403 | //mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 404 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 405 | //final ScheduledFuture<?> shortestPathReconcileHandle = |
| 406 | //shortestPathReconcileScheduler.scheduleAtFixedRate(shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 407 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 408 | @Override |
| 409 | public void init(String conf) { |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 410 | conn = GraphDBConnection.getInstance(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | public void finalize() { |
| 414 | close(); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void close() { |
| 419 | conn.close(); |
| 420 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 421 | |
| 422 | @Override |
| 423 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 424 | Collection<Class<? extends IFloodlightService>> l = |
| 425 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 426 | l.add(IFlowService.class); |
| 427 | return l; |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 432 | getServiceImpls() { |
| 433 | Map<Class<? extends IFloodlightService>, |
| 434 | IFloodlightService> m = |
| 435 | new HashMap<Class<? extends IFloodlightService>, |
| 436 | IFloodlightService>(); |
| 437 | m.put(IFlowService.class, this); |
| 438 | return m; |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public Collection<Class<? extends IFloodlightService>> |
| 443 | getModuleDependencies() { |
| 444 | Collection<Class<? extends IFloodlightService>> l = |
| 445 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 446 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 447 | l.add(ITopoRouteService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 448 | l.add(IRestApiService.class); |
| 449 | return l; |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public void init(FloodlightModuleContext context) |
| 454 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 455 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 456 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 457 | topoRouteService = context.getServiceImpl(ITopoRouteService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 458 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 459 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 460 | EnumSet.of(OFType.FLOW_MOD), |
| 461 | OFMESSAGE_DAMPER_TIMEOUT); |
| 462 | // TODO: An ugly hack! |
| 463 | String conf = "/tmp/cassandra.titan"; |
| 464 | this.init(conf); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 465 | |
| 466 | mapReaderScheduler = Executors.newScheduledThreadPool(1); |
| 467 | shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 470 | private long getNextFlowEntryId() { |
| 471 | // |
| 472 | // Generate the next Flow Entry ID. |
| 473 | // NOTE: For now, the higher 32 bits are random, and |
| 474 | // the lower 32 bits are sequential. |
| 475 | // In the future, we need a better allocation mechanism. |
| 476 | // |
| 477 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 478 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 479 | nextFlowEntryIdSuffix = 0; |
| 480 | } else { |
| 481 | nextFlowEntryIdSuffix++; |
| 482 | } |
| 483 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 484 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 485 | return result; |
| 486 | } |
| 487 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 488 | @Override |
| 489 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 490 | restApi.addRestletRoutable(new FlowWebRoutable()); |
| 491 | |
| 492 | // Initialize the Flow Entry ID generator |
| 493 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 494 | |
| 495 | mapReaderScheduler.scheduleAtFixedRate( |
| 496 | mapReader, 3, 3, TimeUnit.SECONDS); |
| 497 | shortestPathReconcileScheduler.scheduleAtFixedRate( |
| 498 | shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Add a flow. |
| 503 | * |
| 504 | * Internally, ONOS will automatically register the installer for |
| 505 | * receiving Flow Path Notifications for that path. |
| 506 | * |
| 507 | * @param flowPath the Flow Path to install. |
| 508 | * @param flowId the return-by-reference Flow ID as assigned internally. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 509 | * @param dataPathSummaryStr the data path summary string if the added |
| 510 | * flow will be maintained internally, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 511 | * @return true on success, otherwise false. |
| 512 | */ |
| 513 | @Override |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 514 | public boolean addFlow(FlowPath flowPath, FlowId flowId, |
| 515 | String dataPathSummaryStr) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 516 | /* |
| 517 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 518 | if (flowPath.flowId().value() == measurementFlowId) { |
| 519 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 520 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 521 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 522 | |
| 523 | IFlowPath flowObj = null; |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 524 | boolean found = false; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 525 | try { |
| 526 | if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId())) |
| 527 | != null) { |
| 528 | log.debug("Adding FlowPath with FlowId {}: found existing FlowPath", |
| 529 | flowPath.flowId().toString()); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 530 | found = true; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 531 | } else { |
| 532 | flowObj = conn.utils().newFlowPath(conn); |
| 533 | log.debug("Adding FlowPath with FlowId {}: creating new FlowPath", |
| 534 | flowPath.flowId().toString()); |
| 535 | } |
| 536 | } catch (Exception e) { |
| 537 | // TODO: handle exceptions |
| 538 | conn.endTx(Transaction.ROLLBACK); |
| 539 | log.error(":addFlow FlowId:{} failed", |
| 540 | flowPath.flowId().toString()); |
| 541 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 542 | if (flowObj == null) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 543 | log.error(":addFlow FlowId:{} failed: Flow object not created", |
| 544 | flowPath.flowId().toString()); |
| 545 | conn.endTx(Transaction.ROLLBACK); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 546 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 547 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 548 | |
| 549 | // |
| 550 | // Set the Flow key: |
| 551 | // - flowId |
| 552 | // |
| 553 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 554 | flowObj.setType("flow"); |
| 555 | |
| 556 | // |
| 557 | // Set the Flow attributes: |
| 558 | // - flowPath.installerId() |
| 559 | // - flowPath.dataPath().srcPort() |
| 560 | // - flowPath.dataPath().dstPort() |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 561 | // - flowPath.matchEthernetFrameType() |
| 562 | // - flowPath.matchSrcIPv4Net() |
| 563 | // - flowPath.matchDstIPv4Net() |
| 564 | // - flowPath.matchSrcMac() |
| 565 | // - flowPath.matchDstMac() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 566 | // |
| 567 | flowObj.setInstallerId(flowPath.installerId().toString()); |
| 568 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 569 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 570 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 571 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 572 | if (flowPath.flowEntryMatch().matchEthernetFrameType()) { |
| 573 | flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType()); |
| 574 | } |
| 575 | if (flowPath.flowEntryMatch().matchSrcIPv4Net()) { |
| 576 | flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString()); |
| 577 | } |
| 578 | if (flowPath.flowEntryMatch().matchDstIPv4Net()) { |
| 579 | flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString()); |
| 580 | } |
| 581 | if (flowPath.flowEntryMatch().matchSrcMac()) { |
| 582 | flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString()); |
| 583 | } |
| 584 | if (flowPath.flowEntryMatch().matchDstMac()) { |
| 585 | flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString()); |
| 586 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 587 | |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 588 | if (dataPathSummaryStr != null) { |
| 589 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 590 | } else { |
| 591 | flowObj.setDataPathSummary(""); |
| 592 | } |
| 593 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 594 | if (found) |
| 595 | flowObj.setUserState("FE_USER_MODIFY"); |
| 596 | else |
| 597 | flowObj.setUserState("FE_USER_ADD"); |
| 598 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 599 | // Flow edges: |
| 600 | // HeadFE |
| 601 | |
| 602 | |
| 603 | // |
| 604 | // Flow Entries: |
| 605 | // flowPath.dataPath().flowEntries() |
| 606 | // |
| 607 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 608 | if (addFlowEntry(flowObj, flowEntry) == null) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 609 | conn.endTx(Transaction.ROLLBACK); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 610 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 611 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 612 | } |
| 613 | conn.endTx(Transaction.COMMIT); |
| 614 | |
| 615 | // |
| 616 | // TODO: We need a proper Flow ID allocation mechanism. |
| 617 | // |
| 618 | flowId.setValue(flowPath.flowId().value()); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 619 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 620 | return true; |
| 621 | } |
| 622 | |
| 623 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 624 | * Add a flow entry to the Network MAP. |
| 625 | * |
| 626 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 627 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 628 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 629 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 630 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 631 | // Flow edges |
| 632 | // HeadFE (TODO) |
| 633 | |
| 634 | // |
| 635 | // Assign the FlowEntry ID. |
| 636 | // |
| 637 | if ((flowEntry.flowEntryId() == null) || |
| 638 | (flowEntry.flowEntryId().value() == 0)) { |
| 639 | long id = getNextFlowEntryId(); |
| 640 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 641 | } |
| 642 | |
| 643 | IFlowEntry flowEntryObj = null; |
| 644 | boolean found = false; |
| 645 | try { |
| 646 | if ((flowEntryObj = |
| 647 | conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) { |
| 648 | log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry", |
| 649 | flowEntry.flowEntryId().toString()); |
| 650 | found = true; |
| 651 | } else { |
| 652 | flowEntryObj = conn.utils().newFlowEntry(conn); |
| 653 | log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry", |
| 654 | flowEntry.flowEntryId().toString()); |
| 655 | } |
| 656 | } catch (Exception e) { |
| 657 | log.error(":addFlow FlowEntryId:{} failed", |
| 658 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 659 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 660 | } |
| 661 | if (flowEntryObj == null) { |
| 662 | log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created", |
| 663 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 664 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // |
| 668 | // Set the Flow Entry key: |
| 669 | // - flowEntry.flowEntryId() |
| 670 | // |
| 671 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 672 | flowEntryObj.setType("flow_entry"); |
| 673 | |
| 674 | // |
| 675 | // Set the Flow Entry Edges and attributes: |
| 676 | // - Switch edge |
| 677 | // - InPort edge |
| 678 | // - OutPort edge |
| 679 | // |
| 680 | // - flowEntry.flowEntryMatch() |
| 681 | // - flowEntry.flowEntryActions() |
| 682 | // - flowEntry.dpid() |
| 683 | // - flowEntry.flowEntryUserState() |
| 684 | // - flowEntry.flowEntrySwitchState() |
| 685 | // - flowEntry.flowEntryErrorState() |
| 686 | // - flowEntry.matchInPort() |
| 687 | // - flowEntry.matchEthernetFrameType() |
| 688 | // - flowEntry.matchSrcIPv4Net() |
| 689 | // - flowEntry.matchDstIPv4Net() |
| 690 | // - flowEntry.matchSrcMac() |
| 691 | // - flowEntry.matchDstMac() |
| 692 | // - flowEntry.actionOutput() |
| 693 | // |
| 694 | ISwitchObject sw = |
| 695 | conn.utils().searchSwitch(conn, flowEntry.dpid().toString()); |
| 696 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
| 697 | flowEntryObj.setSwitch(sw); |
| 698 | if (flowEntry.flowEntryMatch().matchInPort()) { |
| 699 | IPortObject inport = |
| 700 | conn.utils().searchPort(conn, flowEntry.dpid().toString(), |
| 701 | flowEntry.flowEntryMatch().inPort().value()); |
| 702 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
| 703 | flowEntryObj.setInPort(inport); |
| 704 | } |
| 705 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) { |
| 706 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 707 | } |
| 708 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) { |
| 709 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 710 | } |
| 711 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) { |
| 712 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 713 | } |
| 714 | if (flowEntry.flowEntryMatch().matchSrcMac()) { |
| 715 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 716 | } |
| 717 | if (flowEntry.flowEntryMatch().matchDstMac()) { |
| 718 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 719 | } |
| 720 | |
| 721 | for (FlowEntryAction fa : flowEntry.flowEntryActions()) { |
| 722 | if (fa.actionOutput() != null) { |
| 723 | IPortObject outport = |
| 724 | conn.utils().searchPort(conn, |
| 725 | flowEntry.dpid().toString(), |
| 726 | fa.actionOutput().port().value()); |
| 727 | flowEntryObj.setActionOutput(fa.actionOutput().port().value()); |
| 728 | flowEntryObj.setOutPort(outport); |
| 729 | } |
| 730 | } |
| 731 | // TODO: Hacks with hard-coded state names! |
| 732 | if (found) |
| 733 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 734 | else |
| 735 | flowEntryObj.setUserState("FE_USER_ADD"); |
| 736 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 737 | // |
| 738 | // TODO: Take care of the FlowEntryErrorState. |
| 739 | // |
| 740 | |
| 741 | // Flow Entries edges: |
| 742 | // Flow |
| 743 | // NextFE (TODO) |
| 744 | if (! found) { |
| 745 | flowObj.addFlowEntry(flowEntryObj); |
| 746 | flowEntryObj.setFlow(flowObj); |
| 747 | } |
| 748 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 749 | return flowEntryObj; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 753 | * Delete a previously added flow. |
| 754 | * |
| 755 | * @param flowId the Flow ID of the flow to delete. |
| 756 | * @return true on success, otherwise false. |
| 757 | */ |
| 758 | @Override |
| 759 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 760 | /* |
| 761 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 762 | if (flowId.value() == measurementFlowId) { |
| 763 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 764 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 765 | */ |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 766 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 767 | IFlowPath flowObj = null; |
| 768 | // |
| 769 | // We just mark the entries for deletion, |
| 770 | // and let the switches remove each individual entry after |
| 771 | // it has been removed from the switches. |
| 772 | // |
| 773 | try { |
| 774 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 775 | != null) { |
| 776 | log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath", |
| 777 | flowId.toString()); |
| 778 | } else { |
| 779 | log.debug("Deleting FlowPath with FlowId {}: FlowPath not found", |
| 780 | flowId.toString()); |
| 781 | } |
| 782 | } catch (Exception e) { |
| 783 | // TODO: handle exceptions |
| 784 | conn.endTx(Transaction.ROLLBACK); |
| 785 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
| 786 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 787 | if (flowObj == null) { |
| 788 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 789 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 790 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 791 | |
| 792 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 793 | // Find and mark for deletion all Flow Entries, |
| 794 | // and the Flow itself. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 795 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 796 | flowObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 797 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 798 | boolean empty = true; // TODO: an ugly hack |
| 799 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 800 | empty = false; |
| 801 | // flowObj.removeFlowEntry(flowEntryObj); |
| 802 | // conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 803 | flowEntryObj.setUserState("FE_USER_DELETE"); |
| 804 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 805 | } |
| 806 | // Remove from the database empty flows |
| 807 | if (empty) |
| 808 | conn.utils().removeFlowPath(conn, flowObj); |
| 809 | conn.endTx(Transaction.COMMIT); |
| 810 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 811 | return true; |
| 812 | } |
| 813 | |
| 814 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 815 | * Clear the state for a previously added flow. |
| 816 | * |
| 817 | * @param flowId the Flow ID of the flow to clear. |
| 818 | * @return true on success, otherwise false. |
| 819 | */ |
| 820 | @Override |
| 821 | public boolean clearFlow(FlowId flowId) { |
| 822 | IFlowPath flowObj = null; |
| 823 | try { |
| 824 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 825 | != null) { |
| 826 | log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath", |
| 827 | flowId.toString()); |
| 828 | } else { |
| 829 | log.debug("Clearing FlowPath with FlowId {}: FlowPath not found", |
| 830 | flowId.toString()); |
| 831 | } |
| 832 | } catch (Exception e) { |
| 833 | // TODO: handle exceptions |
| 834 | conn.endTx(Transaction.ROLLBACK); |
| 835 | log.error(":clearFlow FlowId:{} failed", flowId.toString()); |
| 836 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 837 | if (flowObj == null) { |
| 838 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 839 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 840 | } |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 841 | |
| 842 | // |
| 843 | // Remove all Flow Entries |
| 844 | // |
| 845 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 846 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 847 | flowObj.removeFlowEntry(flowEntryObj); |
| 848 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 849 | } |
| 850 | // Remove the Flow itself |
| 851 | conn.utils().removeFlowPath(conn, flowObj); |
| 852 | conn.endTx(Transaction.COMMIT); |
| 853 | |
| 854 | return true; |
| 855 | } |
| 856 | |
| 857 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 858 | * Get a previously added flow. |
| 859 | * |
| 860 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 861 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 862 | */ |
| 863 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 864 | public FlowPath getFlow(FlowId flowId) { |
| 865 | IFlowPath flowObj = null; |
| 866 | try { |
| 867 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 868 | != null) { |
| 869 | log.debug("Get FlowPath with FlowId {}: found existing FlowPath", |
| 870 | flowId.toString()); |
| 871 | } else { |
| 872 | log.debug("Get FlowPath with FlowId {}: FlowPath not found", |
| 873 | flowId.toString()); |
| 874 | } |
| 875 | } catch (Exception e) { |
| 876 | // TODO: handle exceptions |
| 877 | conn.endTx(Transaction.ROLLBACK); |
| 878 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 879 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 880 | if (flowObj == null) { |
| 881 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 882 | return null; // Flow not found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 883 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 884 | |
| 885 | // |
| 886 | // Extract the Flow state |
| 887 | // |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 888 | FlowPath flowPath = extractFlowPath(flowObj); |
| 889 | conn.endTx(Transaction.COMMIT); |
| 890 | |
| 891 | return flowPath; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * Get all previously added flows by a specific installer for a given |
| 896 | * data path endpoints. |
| 897 | * |
| 898 | * @param installerId the Caller ID of the installer of the flow to get. |
| 899 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 900 | * @return the Flow Paths if found, otherwise null. |
| 901 | */ |
| 902 | @Override |
| 903 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 904 | DataPathEndpoints dataPathEndpoints) { |
| 905 | // |
| 906 | // TODO: The implementation below is not optimal: |
| 907 | // We fetch all flows, and then return only the subset that match |
| 908 | // the query conditions. |
| 909 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 910 | // the flows as appropriate. |
| 911 | // |
| 912 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 913 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 914 | |
| 915 | if (allFlows == null) { |
| 916 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 917 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 920 | for (FlowPath flow : allFlows) { |
| 921 | // |
| 922 | // TODO: String-based comparison is sub-optimal. |
| 923 | // We are using it for now to save us the extra work of |
Pavlin Radoslavov | c4e76a6 | 2013-03-06 10:52:41 -0800 | [diff] [blame] | 924 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 925 | // |
| 926 | if (! flow.installerId().toString().equals(installerId.toString())) |
| 927 | continue; |
| 928 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 929 | continue; |
| 930 | } |
| 931 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 932 | continue; |
| 933 | } |
| 934 | flowPaths.add(flow); |
| 935 | } |
| 936 | |
| 937 | if (flowPaths.isEmpty()) { |
| 938 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 939 | } else { |
| 940 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints); |
| 941 | } |
| 942 | |
| 943 | return flowPaths; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * Get all installed flows by all installers for given data path endpoints. |
| 948 | * |
| 949 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 950 | * @return the Flow Paths if found, otherwise null. |
| 951 | */ |
| 952 | @Override |
| 953 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
| 954 | // |
| 955 | // TODO: The implementation below is not optimal: |
| 956 | // We fetch all flows, and then return only the subset that match |
| 957 | // the query conditions. |
| 958 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 959 | // the flows as appropriate. |
| 960 | // |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 961 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 962 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 963 | |
| 964 | if (allFlows == null) { |
| 965 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 966 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 969 | for (FlowPath flow : allFlows) { |
| 970 | // |
| 971 | // TODO: String-based comparison is sub-optimal. |
| 972 | // We are using it for now to save us the extra work of |
Pavlin Radoslavov | c4e76a6 | 2013-03-06 10:52:41 -0800 | [diff] [blame] | 973 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 974 | // |
| 975 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 976 | continue; |
| 977 | } |
| 978 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 979 | continue; |
| 980 | } |
| 981 | flowPaths.add(flow); |
| 982 | } |
| 983 | |
| 984 | if (flowPaths.isEmpty()) { |
| 985 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 986 | } else { |
| 987 | log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints); |
| 988 | } |
| 989 | |
| 990 | return flowPaths; |
| 991 | } |
| 992 | |
| 993 | /** |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 994 | * Get summary of all installed flows by all installers in a given range |
| 995 | * |
| 996 | * @param flowId the data path endpoints of the flows to get. |
| 997 | * @param maxFlows: the maximum number of flows to be returned |
| 998 | * @return the Flow Paths if found, otherwise null. |
| 999 | */ |
| 1000 | @Override |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1001 | public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) { |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1002 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1003 | // TODO: The implementation below is not optimal: |
| 1004 | // We fetch all flows, and then return only the subset that match |
| 1005 | // the query conditions. |
| 1006 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1007 | // the flows as appropriate. |
| 1008 | // |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1009 | //ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1010 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1011 | ArrayList<IFlowPath> flowPathsWithoutFlowEntries = getAllFlowsWithoutFlowEntries(); |
| 1012 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1013 | Collections.sort(flowPathsWithoutFlowEntries, |
| 1014 | new Comparator<IFlowPath>(){ |
| 1015 | @Override |
| 1016 | public int compare(IFlowPath first, IFlowPath second) { |
| 1017 | // TODO Auto-generated method stub |
| 1018 | long result = new FlowId(first.getFlowId()).value() |
| 1019 | - new FlowId(second.getFlowId()).value(); |
| 1020 | if (result > 0) return 1; |
| 1021 | else if (result < 0) return -1; |
| 1022 | else return 0; |
| 1023 | } |
| 1024 | } |
| 1025 | ); |
| 1026 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1027 | return flowPathsWithoutFlowEntries; |
| 1028 | |
| 1029 | /* |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1030 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1031 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1032 | if (allFlows == null) { |
| 1033 | log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1034 | return flowPaths; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Umesh Krishnaswamy | 244b4ae | 2013-03-29 12:05:15 -0700 | [diff] [blame] | 1037 | Collections.sort(allFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1038 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1039 | for (FlowPath flow : allFlows) { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1040 | flow.setFlowEntryMatch(null); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1041 | |
Pavlin Radoslavov | 96b4342 | 2013-04-04 19:14:56 -0700 | [diff] [blame] | 1042 | // start from desired flowId |
| 1043 | if (flow.flowId().value() < flowId.value()) { |
| 1044 | continue; |
| 1045 | } |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1046 | |
| 1047 | // Summarize by making null flow entry fields that are not relevant to report |
| 1048 | for (FlowEntry flowEntry : flow.dataPath().flowEntries()) { |
| 1049 | flowEntry.setFlowEntryActions(null); |
| 1050 | flowEntry.setFlowEntryMatch(null); |
| 1051 | } |
| 1052 | |
| 1053 | flowPaths.add(flow); |
| 1054 | if (maxFlows != 0 && flowPaths.size() >= maxFlows) { |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | if (flowPaths.isEmpty()) { |
| 1060 | log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1061 | } else { |
| 1062 | log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows); |
| 1063 | } |
| 1064 | |
| 1065 | return flowPaths; |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1066 | */ |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /** |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1070 | * Get all installed flows by all installers. |
| 1071 | * |
| 1072 | * @return the Flow Paths if found, otherwise null. |
| 1073 | */ |
| 1074 | @Override |
| 1075 | public ArrayList<FlowPath> getAllFlows() { |
| 1076 | Iterable<IFlowPath> flowPathsObj = null; |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1077 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1078 | |
| 1079 | try { |
| 1080 | if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) { |
| 1081 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1082 | } else { |
| 1083 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1084 | } |
| 1085 | } catch (Exception e) { |
| 1086 | // TODO: handle exceptions |
| 1087 | conn.endTx(Transaction.ROLLBACK); |
| 1088 | log.error(":getAllFlowPaths failed"); |
| 1089 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1090 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1091 | conn.endTx(Transaction.COMMIT); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1092 | return flowPaths; // No Flows found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1093 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1094 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1095 | for (IFlowPath flowObj : flowPathsObj) { |
| 1096 | // |
| 1097 | // Extract the Flow state |
| 1098 | // |
| 1099 | FlowPath flowPath = extractFlowPath(flowObj); |
Pavlin Radoslavov | 3f2af73 | 2013-03-29 15:29:35 -0700 | [diff] [blame] | 1100 | if (flowPath != null) |
| 1101 | flowPaths.add(flowPath); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | conn.endTx(Transaction.COMMIT); |
| 1105 | |
| 1106 | return flowPaths; |
| 1107 | } |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1108 | |
| 1109 | public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(){ |
| 1110 | Iterable<IFlowPath> flowPathsObj = null; |
| 1111 | ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>(); |
| 1112 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1113 | |
| 1114 | try { |
| 1115 | if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) { |
| 1116 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1117 | } else { |
| 1118 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1119 | } |
| 1120 | } catch (Exception e) { |
| 1121 | // TODO: handle exceptions |
| 1122 | conn.endTx(Transaction.ROLLBACK); |
| 1123 | log.error(":getAllFlowPaths failed"); |
| 1124 | } |
| 1125 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1126 | return new ArrayList<IFlowPath>(); // No Flows found |
| 1127 | } |
| 1128 | |
| 1129 | for (IFlowPath flowObj : flowPathsObj){ |
| 1130 | flowPathsObjArray.add(flowObj); |
| 1131 | } |
| 1132 | /* |
| 1133 | for (IFlowPath flowObj : flowPathsObj) { |
| 1134 | // |
| 1135 | // Extract the Flow state |
| 1136 | // |
| 1137 | FlowPath flowPath = extractFlowPath(flowObj); |
| 1138 | if (flowPath != null) |
| 1139 | flowPaths.add(flowPath); |
| 1140 | } |
| 1141 | */ |
| 1142 | |
| 1143 | //conn.endTx(Transaction.COMMIT); |
| 1144 | |
| 1145 | return flowPathsObjArray; |
| 1146 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1147 | |
| 1148 | /** |
| 1149 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 1150 | * |
| 1151 | * @param flowObj the object to extract the Flow Path State from. |
| 1152 | * @return the extracted Flow Path State. |
| 1153 | */ |
| 1154 | private FlowPath extractFlowPath(IFlowPath flowObj) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1155 | // |
| 1156 | // Extract the Flow state |
| 1157 | // |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1158 | String flowIdStr = flowObj.getFlowId(); |
| 1159 | String installerIdStr = flowObj.getInstallerId(); |
| 1160 | String srcSwitchStr = flowObj.getSrcSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1161 | Short srcPortShort = flowObj.getSrcPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1162 | String dstSwitchStr = flowObj.getDstSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1163 | Short dstPortShort = flowObj.getDstPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1164 | |
| 1165 | if ((flowIdStr == null) || |
| 1166 | (installerIdStr == null) || |
| 1167 | (srcSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1168 | (srcPortShort == null) || |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1169 | (dstSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1170 | (dstPortShort == null)) { |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1171 | // TODO: A work-around, becauuse of some bogus database objects |
| 1172 | return null; |
| 1173 | } |
| 1174 | |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1175 | FlowPath flowPath = new FlowPath(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1176 | flowPath.setFlowId(new FlowId(flowIdStr)); |
| 1177 | flowPath.setInstallerId(new CallerId(installerIdStr)); |
| 1178 | flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1179 | flowPath.dataPath().srcPort().setPort(new Port(srcPortShort)); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1180 | flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1181 | flowPath.dataPath().dstPort().setPort(new Port(dstPortShort)); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1182 | // |
| 1183 | // Extract the match conditions common for all Flow Entries |
| 1184 | // |
| 1185 | { |
| 1186 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1187 | Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1188 | if (matchEthernetFrameType != null) |
| 1189 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1190 | String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1191 | if (matchSrcIPv4Net != null) |
| 1192 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1193 | String matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1194 | if (matchDstIPv4Net != null) |
| 1195 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1196 | String matchSrcMac = flowObj.getMatchSrcMac(); |
| 1197 | if (matchSrcMac != null) |
| 1198 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1199 | String matchDstMac = flowObj.getMatchDstMac(); |
| 1200 | if (matchDstMac != null) |
| 1201 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1202 | flowPath.setFlowEntryMatch(match); |
| 1203 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1204 | |
| 1205 | // |
| 1206 | // Extract all Flow Entries |
| 1207 | // |
| 1208 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1209 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1210 | FlowEntry flowEntry = extractFlowEntry(flowEntryObj); |
| 1211 | if (flowEntry == null) |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1212 | continue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1213 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 1214 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1215 | |
| 1216 | return flowPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1217 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1218 | |
| 1219 | /** |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1220 | * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry. |
| 1221 | * |
| 1222 | * @param flowEntryObj the object to extract the Flow Entry State from. |
| 1223 | * @return the extracted Flow Entry State. |
| 1224 | */ |
| 1225 | private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) { |
| 1226 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1227 | String switchDpidStr = flowEntryObj.getSwitchDpid(); |
| 1228 | String userState = flowEntryObj.getUserState(); |
| 1229 | String switchState = flowEntryObj.getSwitchState(); |
| 1230 | |
| 1231 | if ((flowEntryIdStr == null) || |
| 1232 | (switchDpidStr == null) || |
| 1233 | (userState == null) || |
| 1234 | (switchState == null)) { |
| 1235 | // TODO: A work-around, becauuse of some bogus database objects |
| 1236 | return null; |
| 1237 | } |
| 1238 | |
| 1239 | FlowEntry flowEntry = new FlowEntry(); |
| 1240 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr)); |
| 1241 | flowEntry.setDpid(new Dpid(switchDpidStr)); |
| 1242 | |
| 1243 | // |
| 1244 | // Extract the match conditions |
| 1245 | // |
| 1246 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1247 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1248 | if (matchInPort != null) |
| 1249 | match.enableInPort(new Port(matchInPort)); |
| 1250 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1251 | if (matchEthernetFrameType != null) |
| 1252 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1253 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1254 | if (matchSrcIPv4Net != null) |
| 1255 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1256 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1257 | if (matchDstIPv4Net != null) |
| 1258 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1259 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1260 | if (matchSrcMac != null) |
| 1261 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1262 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1263 | if (matchDstMac != null) |
| 1264 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1265 | flowEntry.setFlowEntryMatch(match); |
| 1266 | |
| 1267 | // |
| 1268 | // Extract the actions |
| 1269 | // |
| 1270 | ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>(); |
| 1271 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1272 | if (actionOutputPort != null) { |
| 1273 | FlowEntryAction action = new FlowEntryAction(); |
| 1274 | action.setActionOutput(new Port(actionOutputPort)); |
| 1275 | actions.add(action); |
| 1276 | } |
| 1277 | flowEntry.setFlowEntryActions(actions); |
| 1278 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 1279 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 1280 | // |
| 1281 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
| 1282 | // and FlowEntryErrorState. |
| 1283 | // |
| 1284 | return flowEntry; |
| 1285 | } |
| 1286 | |
| 1287 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1288 | * Add and maintain a shortest-path flow. |
| 1289 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1290 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1291 | * |
| 1292 | * @param flowPath the Flow Path with the endpoints and the match |
| 1293 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1294 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1295 | */ |
| 1296 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1297 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1298 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1299 | // Don't do the shortest path computation here. |
| 1300 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1301 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1302 | |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1303 | // We need the DataPath to populate the Network MAP |
| 1304 | DataPath dataPath = new DataPath(); |
| 1305 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 1306 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1307 | |
| 1308 | // |
| 1309 | // Prepare the computed Flow Path |
| 1310 | // |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1311 | FlowPath computedFlowPath = new FlowPath(); |
| 1312 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 1313 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
| 1314 | computedFlowPath.setDataPath(dataPath); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1315 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1316 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1317 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1318 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1319 | if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1320 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1321 | |
| 1322 | // TODO: Mark the flow for maintenance purpose |
| 1323 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1324 | return (computedFlowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1328 | * Reconcile a flow. |
| 1329 | * |
| 1330 | * @param flowObj the flow that needs to be reconciliated. |
| 1331 | * @param newDataPath the new data path to use. |
| 1332 | * @return true on success, otherwise false. |
| 1333 | */ |
| 1334 | public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
| 1335 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 1336 | |
| 1337 | // |
| 1338 | // Set the incoming port matching and the outgoing port output |
| 1339 | // actions for each flow entry. |
| 1340 | // |
| 1341 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
| 1342 | // Set the incoming port matching |
| 1343 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 1344 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 1345 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 1346 | |
| 1347 | // Set the outgoing port output action |
| 1348 | ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions(); |
| 1349 | if (flowEntryActions == null) { |
| 1350 | flowEntryActions = new ArrayList<FlowEntryAction>(); |
| 1351 | flowEntry.setFlowEntryActions(flowEntryActions); |
| 1352 | } |
| 1353 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1354 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
| 1355 | flowEntryActions.add(flowEntryAction); |
| 1356 | } |
| 1357 | |
| 1358 | // |
| 1359 | // Remove the old Flow Entries, and add the new Flow Entries |
| 1360 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1361 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1362 | LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>(); |
| 1363 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1364 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1365 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1366 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1367 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1368 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | // |
| 1372 | // Set the Data Path Summary |
| 1373 | // |
| 1374 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 1375 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 1376 | |
| 1377 | return true; |
| 1378 | } |
| 1379 | |
| 1380 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1381 | * Reconcile all flows in a set. |
| 1382 | * |
| 1383 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 1384 | */ |
| 1385 | public void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
| 1386 | if (! flowObjSet.iterator().hasNext()) |
| 1387 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 1388 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1392 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1393 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1394 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1395 | * @param flowObj the flow path object for the flow entry to install. |
| 1396 | * @param flowEntryObj the flow entry object to install. |
| 1397 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1398 | */ |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1399 | public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
| 1400 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1401 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1402 | if (flowEntryIdStr == null) |
| 1403 | return false; |
| 1404 | FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1405 | String userState = flowEntryObj.getUserState(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1406 | if (userState == null) |
| 1407 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1408 | |
| 1409 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1410 | // Create the Open Flow Flow Modification Entry to push |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1411 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1412 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1413 | .getMessage(OFType.FLOW_MOD); |
| 1414 | long cookie = flowEntryId.value(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1415 | |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1416 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1417 | if (userState.equals("FE_USER_ADD")) { |
| 1418 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1419 | } else if (userState.equals("FE_USER_MODIFY")) { |
| 1420 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1421 | } else if (userState.equals("FE_USER_DELETE")) { |
| 1422 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1423 | } else { |
| 1424 | // Unknown user state. Ignore the entry |
| 1425 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1426 | flowEntryId.toString(), userState); |
| 1427 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1431 | // Fetch the match conditions. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1432 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1433 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1434 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1435 | // condition set. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1436 | // |
| 1437 | OFMatch match = new OFMatch(); |
| 1438 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1439 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1440 | // Match the Incoming Port |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1441 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1442 | if (matchInPort != null) { |
| 1443 | match.setInputPort(matchInPort); |
| 1444 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1445 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1446 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1447 | // Match the Ethernet Frame Type |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1448 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1449 | if (matchEthernetFrameType == null) |
| 1450 | matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1451 | if (matchEthernetFrameType != null) { |
| 1452 | match.setDataLayerType(matchEthernetFrameType); |
| 1453 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1454 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1455 | |
| 1456 | // Match the Source IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1457 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1458 | if (matchSrcIPv4Net == null) |
| 1459 | matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1460 | if (matchSrcIPv4Net != null) { |
| 1461 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 1462 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1463 | |
| 1464 | // Natch the Destination IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1465 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1466 | if (matchDstIPv4Net == null) |
| 1467 | matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1468 | if (matchDstIPv4Net != null) { |
| 1469 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 1470 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1471 | |
| 1472 | // Match the Source MAC address |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1473 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1474 | if (matchSrcMac == null) |
| 1475 | matchSrcMac = flowObj.getMatchSrcMac(); |
| 1476 | if (matchSrcMac != null) { |
| 1477 | match.setDataLayerSource(matchSrcMac); |
| 1478 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1479 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1480 | |
| 1481 | // Match the Destination MAC address |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1482 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1483 | if (matchDstMac == null) |
| 1484 | matchDstMac = flowObj.getMatchDstMac(); |
| 1485 | if (matchDstMac != null) { |
| 1486 | match.setDataLayerDestination(matchDstMac); |
| 1487 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1488 | } |
| 1489 | |
| 1490 | // |
| 1491 | // Fetch the actions |
| 1492 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1493 | // TODO: For now we support only the "OUTPUT" actions. |
| 1494 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1495 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1496 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1497 | if (actionOutputPort != null) { |
| 1498 | OFActionOutput action = new OFActionOutput(); |
| 1499 | // XXX: The max length is hard-coded for now |
| 1500 | action.setMaxLength((short)0xffff); |
| 1501 | action.setPort(actionOutputPort); |
| 1502 | actions.add(action); |
| 1503 | } |
| 1504 | |
| 1505 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1506 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1507 | .setPriority(PRIORITY_DEFAULT) |
| 1508 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1509 | .setCookie(cookie) |
| 1510 | .setCommand(flowModCommand) |
| 1511 | .setMatch(match) |
| 1512 | .setActions(actions) |
| 1513 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 1514 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1515 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1516 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1517 | if (actionOutputPort != null) |
| 1518 | fm.setOutPort(actionOutputPort); |
| 1519 | } |
| 1520 | |
| 1521 | // |
| 1522 | // TODO: Set the following flag |
| 1523 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1524 | // See method ForwardingBase::pushRoute() |
| 1525 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1526 | |
| 1527 | // |
| 1528 | // Write the message to the switch |
| 1529 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1530 | log.debug("MEASUREMENT: Installing flow entry " + userState + |
| 1531 | " into switch DPID: " + |
| 1532 | mySwitch.getStringId() + |
| 1533 | " flowEntryId: " + flowEntryId.toString() + |
| 1534 | " srcMac: " + matchSrcMac + " dstMac: " + matchDstMac + |
| 1535 | " inPort: " + matchInPort + " outPort: " + actionOutputPort |
| 1536 | ); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1537 | try { |
| 1538 | messageDamper.write(mySwitch, fm, null); |
| 1539 | mySwitch.flush(); |
| 1540 | // |
| 1541 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1542 | // to check for errors, and update the SwitchState |
| 1543 | // for a flow entry after the Barrier message is |
| 1544 | // is received. |
| 1545 | // |
| 1546 | flowEntryObj.setSwitchState("FE_SWITCH_UPDATED"); |
| 1547 | } catch (IOException e) { |
| 1548 | log.error("Failure writing flow mod from network map", e); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1549 | return false; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | return true; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * Install a Flow Entry on a switch. |
| 1557 | * |
| 1558 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1559 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1560 | * @param flowEntry the flow entry to install. |
| 1561 | * @return true on success, otherwise false. |
| 1562 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1563 | public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1564 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1565 | // |
| 1566 | // Create the OpenFlow Flow Modification Entry to push |
| 1567 | // |
| 1568 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1569 | .getMessage(OFType.FLOW_MOD); |
| 1570 | long cookie = flowEntry.flowEntryId().value(); |
| 1571 | |
| 1572 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1573 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 1574 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1575 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 1576 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1577 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 1578 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1579 | } else { |
| 1580 | // Unknown user state. Ignore the entry |
| 1581 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1582 | flowEntry.flowEntryId().toString(), |
| 1583 | flowEntry.flowEntryUserState()); |
| 1584 | return false; |
| 1585 | } |
| 1586 | |
| 1587 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1588 | // Fetch the match conditions. |
| 1589 | // |
| 1590 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1591 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1592 | // condition set. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1593 | // |
| 1594 | OFMatch match = new OFMatch(); |
| 1595 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1596 | FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch(); |
| 1597 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 1598 | |
| 1599 | // Match the Incoming Port |
| 1600 | Port matchInPort = flowEntryMatch.inPort(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1601 | if (matchInPort != null) { |
| 1602 | match.setInputPort(matchInPort.value()); |
| 1603 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1604 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1605 | |
| 1606 | // Match the Ethernet Frame Type |
| 1607 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
| 1608 | if ((matchEthernetFrameType == null) && (flowPathMatch != null)) { |
| 1609 | matchEthernetFrameType = flowPathMatch.ethernetFrameType(); |
| 1610 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1611 | if (matchEthernetFrameType != null) { |
| 1612 | match.setDataLayerType(matchEthernetFrameType); |
| 1613 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1614 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1615 | |
| 1616 | // Match the Source IPv4 Network prefix |
| 1617 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
| 1618 | if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) { |
| 1619 | matchSrcIPv4Net = flowPathMatch.srcIPv4Net(); |
| 1620 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1621 | if (matchSrcIPv4Net != null) { |
| 1622 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 1623 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1624 | |
| 1625 | // Natch the Destination IPv4 Network prefix |
| 1626 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
| 1627 | if ((matchDstIPv4Net == null) && (flowPathMatch != null)) { |
| 1628 | matchDstIPv4Net = flowPathMatch.dstIPv4Net(); |
| 1629 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1630 | if (matchDstIPv4Net != null) { |
| 1631 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 1632 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1633 | |
| 1634 | // Match the Source MAC address |
| 1635 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
| 1636 | if ((matchSrcMac == null) && (flowPathMatch != null)) { |
| 1637 | matchSrcMac = flowPathMatch.srcMac(); |
| 1638 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1639 | if (matchSrcMac != null) { |
| 1640 | match.setDataLayerSource(matchSrcMac.toString()); |
| 1641 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1642 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1643 | |
| 1644 | // Match the Destination MAC address |
| 1645 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
| 1646 | if ((matchDstMac == null) && (flowPathMatch != null)) { |
| 1647 | matchDstMac = flowPathMatch.dstMac(); |
| 1648 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1649 | if (matchDstMac != null) { |
| 1650 | match.setDataLayerDestination(matchDstMac.toString()); |
| 1651 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1652 | } |
| 1653 | |
| 1654 | // |
| 1655 | // Fetch the actions |
| 1656 | // |
| 1657 | // TODO: For now we support only the "OUTPUT" actions. |
| 1658 | // |
| 1659 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1660 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1661 | ArrayList<FlowEntryAction> flowEntryActions = |
| 1662 | flowEntry.flowEntryActions(); |
| 1663 | for (FlowEntryAction flowEntryAction : flowEntryActions) { |
| 1664 | FlowEntryAction.ActionOutput actionOutput = |
| 1665 | flowEntryAction.actionOutput(); |
| 1666 | if (actionOutput != null) { |
| 1667 | short actionOutputPort = actionOutput.port().value(); |
| 1668 | OFActionOutput action = new OFActionOutput(); |
| 1669 | // XXX: The max length is hard-coded for now |
| 1670 | action.setMaxLength((short)0xffff); |
| 1671 | action.setPort(actionOutputPort); |
| 1672 | actions.add(action); |
| 1673 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1674 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1675 | fm.setOutPort(actionOutputPort); |
| 1676 | } |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1681 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1682 | .setPriority(PRIORITY_DEFAULT) |
| 1683 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1684 | .setCookie(cookie) |
| 1685 | .setCommand(flowModCommand) |
| 1686 | .setMatch(match) |
| 1687 | .setActions(actions) |
| 1688 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 1689 | |
| 1690 | // |
| 1691 | // TODO: Set the following flag |
| 1692 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1693 | // See method ForwardingBase::pushRoute() |
| 1694 | // |
| 1695 | |
| 1696 | // |
| 1697 | // Write the message to the switch |
| 1698 | // |
| 1699 | try { |
| 1700 | messageDamper.write(mySwitch, fm, null); |
| 1701 | mySwitch.flush(); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1702 | // |
| 1703 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1704 | // to check for errors, and update the SwitchState |
| 1705 | // for a flow entry after the Barrier message is |
| 1706 | // is received. |
| 1707 | // |
| 1708 | // TODO: The FlowEntry Object in Titan should be set |
| 1709 | // to FE_SWITCH_UPDATED. |
| 1710 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1711 | } catch (IOException e) { |
| 1712 | log.error("Failure writing flow mod from network map", e); |
| 1713 | return false; |
| 1714 | } |
| 1715 | return true; |
| 1716 | } |
| 1717 | |
| 1718 | /** |
| 1719 | * Remove a Flow Entry from a switch. |
| 1720 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1721 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1722 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1723 | * @param flowEntry the flow entry to remove. |
| 1724 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1725 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1726 | public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1727 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1728 | // |
| 1729 | // The installFlowEntry() method implements both installation |
| 1730 | // and removal of flow entries. |
| 1731 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1732 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /** |
| 1736 | * Install a Flow Entry on a remote controller. |
| 1737 | * |
| 1738 | * TODO: We need it now: Jono |
| 1739 | * - For now it will make a REST call to the remote controller. |
| 1740 | * - Internally, it needs to know the name of the remote controller. |
| 1741 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1742 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1743 | * @param flowEntry the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1744 | * @return true on success, otherwise false. |
| 1745 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1746 | public boolean installRemoteFlowEntry(FlowPath flowPath, |
| 1747 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1748 | // TODO: We need it now: Jono |
| 1749 | // - For now it will make a REST call to the remote controller. |
| 1750 | // - Internally, it needs to know the name of the remote controller. |
| 1751 | return true; |
| 1752 | } |
| 1753 | |
| 1754 | /** |
| 1755 | * Remove a flow entry on a remote controller. |
| 1756 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1757 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1758 | * @param flowEntry the flow entry to remove. |
| 1759 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1760 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1761 | public boolean removeRemoteFlowEntry(FlowPath flowPath, |
| 1762 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1763 | // |
| 1764 | // The installRemoteFlowEntry() method implements both installation |
| 1765 | // and removal of flow entries. |
| 1766 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1767 | return (installRemoteFlowEntry(flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1768 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1769 | } |