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