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