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