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