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