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