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