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