HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 4 | import java.io.PrintWriter; |
| 5 | import java.io.StringWriter; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 8 | import java.util.Collections; |
| 9 | import java.util.Comparator; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 10 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 11 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 12 | import java.util.LinkedList; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 13 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 14 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 15 | import java.util.Random; |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 16 | import java.util.concurrent.ConcurrentLinkedQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 17 | import java.util.concurrent.Executors; |
| 18 | import java.util.concurrent.ScheduledExecutorService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 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; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 23 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 24 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 26 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 27 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 28 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 29 | import net.floodlightcontroller.util.OFMessageDamper; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 30 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
| 32 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 33 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 34 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 35 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 36 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService; |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable; |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.routing.TopoRouteService; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.util.CallerId; |
| 40 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 41 | import net.onrc.onos.ofcontroller.util.DataPathEndpoints; |
| 42 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 43 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 44 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 45 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.*; |
| 46 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 47 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 48 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 49 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 50 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
| 51 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 52 | import net.onrc.onos.ofcontroller.util.FlowPath; |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 53 | import net.onrc.onos.ofcontroller.util.FlowPathFlags; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 54 | import net.onrc.onos.ofcontroller.util.IPv4Net; |
| 55 | import net.onrc.onos.ofcontroller.util.Port; |
| 56 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 57 | |
| 58 | import org.openflow.protocol.OFFlowMod; |
| 59 | import org.openflow.protocol.OFMatch; |
| 60 | import org.openflow.protocol.OFPacketOut; |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 61 | import org.openflow.protocol.OFPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 62 | import org.openflow.protocol.OFType; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 63 | import org.openflow.protocol.action.*; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | import org.slf4j.LoggerFactory; |
| 66 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 67 | |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 68 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 69 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 70 | protected GraphDBOperation op; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 71 | |
| 72 | protected IRestApiService restApi; |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 73 | protected volatile IFloodlightProviderService floodlightProvider; |
| 74 | protected volatile ITopoRouteService topoRouteService; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 75 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 76 | |
| 77 | protected OFMessageDamper messageDamper; |
| 78 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 79 | // |
| 80 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 81 | // The local copy should go away! |
| 82 | // |
| 83 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 84 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
| 85 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 86 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 87 | public static final short PRIORITY_DEFAULT = 100; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 88 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 89 | // Flow Entry ID generation state |
| 90 | private static Random randomGenerator = new Random(); |
| 91 | private static int nextFlowEntryIdPrefix = 0; |
| 92 | private static int nextFlowEntryIdSuffix = 0; |
| 93 | private static long nextFlowEntryId = 0; |
| 94 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 95 | // State for measurement purpose |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 96 | private static long measurementFlowId = 100000; |
| 97 | private static String measurementFlowIdStr = "0x186a0"; // 100000 |
| 98 | private long modifiedMeasurementFlowTime = 0; |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 99 | // |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 100 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 101 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 102 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 103 | |
| 104 | // The periodic task(s) |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 105 | private ScheduledExecutorService mapReaderScheduler; |
| 106 | private ScheduledExecutorService shortestPathReconcileScheduler; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 107 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 108 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 109 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 110 | try { |
| 111 | runImpl(); |
| 112 | } catch (Exception e) { |
| 113 | log.debug("Exception processing All Flow Entries from the Network MAP: ", e); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 114 | op.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private void runImpl() { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 120 | long startTime = System.nanoTime(); |
| 121 | int counterAllFlowEntries = 0; |
| 122 | int counterMyNotUpdatedFlowEntries = 0; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 123 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 124 | if (floodlightProvider == null) { |
| 125 | log.debug("FloodlightProvider service not found!"); |
| 126 | return; |
| 127 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 128 | Map<Long, IOFSwitch> mySwitches = |
| 129 | floodlightProvider.getSwitches(); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 130 | if (mySwitches.isEmpty()) { |
Pankaj Berde | 85a877b | 2013-10-03 18:26:35 -0700 | [diff] [blame^] | 131 | log.trace("No switches controlled"); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 132 | return; |
| 133 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 134 | LinkedList<IFlowEntry> addFlowEntries = |
| 135 | new LinkedList<IFlowEntry>(); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 136 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 137 | new LinkedList<IFlowEntry>(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 138 | |
| 139 | // |
Pankaj Berde | a2e14a9 | 2013-04-15 11:59:15 -0700 | [diff] [blame] | 140 | // Fetch all Flow Entries which need to be updated and select only my Flow Entries |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 141 | // that need to be updated into the switches. |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 142 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 143 | boolean processed_measurement_flow = false; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 144 | Iterable<IFlowEntry> allFlowEntries = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 145 | op.getAllSwitchNotUpdatedFlowEntries(); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 146 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 147 | counterAllFlowEntries++; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 148 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 149 | String dpidStr = flowEntryObj.getSwitchDpid(); |
| 150 | if (dpidStr == null) |
| 151 | continue; |
| 152 | Dpid dpid = new Dpid(dpidStr); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 153 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 154 | if (mySwitch == null) |
| 155 | continue; // Ignore the entry: not my switch |
| 156 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 157 | IFlowPath flowObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 158 | op.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 159 | if (flowObj == null) |
| 160 | continue; // Should NOT happen |
| 161 | if (flowObj.getFlowId() == null) |
| 162 | continue; // Invalid entry |
| 163 | |
| 164 | // |
| 165 | // NOTE: For now we process the DELETE before the ADD |
| 166 | // to cover the more common scenario. |
| 167 | // TODO: This is error prone and needs to be fixed! |
| 168 | // |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 169 | String userState = flowEntryObj.getUserState(); |
| 170 | if (userState == null) |
| 171 | continue; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 172 | if (userState.equals("FE_USER_DELETE")) { |
| 173 | // An entry that needs to be deleted. |
| 174 | deleteFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 175 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
| 176 | } else { |
| 177 | addFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 178 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 179 | counterMyNotUpdatedFlowEntries++; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 180 | // Code for measurement purpose |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 181 | // TODO: Commented-out for now |
| 182 | /* |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 183 | { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 184 | if (flowObj.getFlowId().equals(measurementFlowIdStr)) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 185 | processed_measurement_flow = true; |
| 186 | } |
| 187 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 188 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 191 | // |
| 192 | // Process the Flow Entries that need to be added |
| 193 | // |
| 194 | for (IFlowEntry flowEntryObj : addFlowEntries) { |
| 195 | IFlowPath flowObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 196 | op.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 197 | if (flowObj == null) |
| 198 | continue; // Should NOT happen |
| 199 | if (flowObj.getFlowId() == null) |
| 200 | continue; // Invalid entry |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 201 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 202 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 203 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 204 | if (mySwitch == null) |
| 205 | continue; // Shouldn't happen |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 206 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 207 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 208 | |
| 209 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 210 | // Delete all Flow Entries marked for deletion from the |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 211 | // Network MAP. |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 212 | // |
| 213 | // TODO: We should use the OpenFlow Barrier mechanism |
| 214 | // to check for errors, and delete the Flow Entries after the |
| 215 | // Barrier message is received. |
| 216 | // |
| 217 | while (! deleteFlowEntries.isEmpty()) { |
| 218 | IFlowEntry flowEntryObj = deleteFlowEntries.poll(); |
| 219 | IFlowPath flowObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 220 | op.getFlowPathByFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 221 | if (flowObj == null) { |
| 222 | log.debug("Did not find FlowPath to be deleted"); |
| 223 | continue; |
| 224 | } |
| 225 | flowObj.removeFlowEntry(flowEntryObj); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 226 | op.removeFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 227 | } |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 228 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 229 | op.commit(); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 230 | |
| 231 | if (processed_measurement_flow) { |
| 232 | long estimatedTime = |
| 233 | System.nanoTime() - modifiedMeasurementFlowTime; |
| 234 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 235 | (double)estimatedTime / 1000000000 + " sec"; |
| 236 | log.debug(logMsg); |
| 237 | } |
| 238 | |
| 239 | long estimatedTime = System.nanoTime() - startTime; |
| 240 | double rate = 0.0; |
| 241 | if (estimatedTime > 0) |
| 242 | rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime; |
| 243 | String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + |
| 244 | counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + |
| 245 | counterMyNotUpdatedFlowEntries + " in " + |
| 246 | (double)estimatedTime / 1000000000 + " sec: " + |
| 247 | rate + " paths/s"; |
| 248 | log.debug(logMsg); |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | final Runnable shortestPathReconcile = new Runnable() { |
| 253 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 254 | try { |
| 255 | runImpl(); |
| 256 | } catch (Exception e) { |
| 257 | log.debug("Exception processing All Flows from the Network MAP: ", e); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 258 | op.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | private void runImpl() { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 264 | long startTime = System.nanoTime(); |
| 265 | int counterAllFlowPaths = 0; |
| 266 | int counterMyFlowPaths = 0; |
| 267 | |
| 268 | if (floodlightProvider == null) { |
| 269 | log.debug("FloodlightProvider service not found!"); |
| 270 | return; |
| 271 | } |
| 272 | Map<Long, IOFSwitch> mySwitches = |
| 273 | floodlightProvider.getSwitches(); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 274 | if (mySwitches.isEmpty()) { |
Pankaj Berde | 85a877b | 2013-10-03 18:26:35 -0700 | [diff] [blame^] | 275 | log.trace("No switches controlled"); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 276 | return; |
| 277 | } |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 278 | LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>(); |
| 279 | |
| 280 | boolean processed_measurement_flow = false; |
| 281 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 282 | // |
| 283 | // Fetch and recompute the Shortest Path for those |
| 284 | // Flow Paths this controller is responsible for. |
| 285 | // |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 286 | Map<Long, ?> shortestPathTopo = |
| 287 | topoRouteService.prepareShortestPathTopo(); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 288 | Iterable<IFlowPath> allFlowPaths = op.getAllFlowPaths(); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 289 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 290 | counterAllFlowPaths++; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 291 | if (flowPathObj == null) |
| 292 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 293 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 294 | String srcDpidStr = flowPathObj.getSrcSwitch(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 295 | if (srcDpidStr == null) |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 296 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 297 | Dpid srcDpid = new Dpid(srcDpidStr); |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 298 | // |
| 299 | // Use the source DPID as a heuristic to decide |
| 300 | // which controller is responsible for maintaining the |
| 301 | // shortest path. |
| 302 | // NOTE: This heuristic is error-prone: if the switch |
| 303 | // goes away and no controller is responsible for that |
| 304 | // switch, then the original Flow Path is not cleaned-up |
| 305 | // |
| 306 | IOFSwitch mySwitch = mySwitches.get(srcDpid.value()); |
| 307 | if (mySwitch == null) |
| 308 | continue; // Ignore: not my responsibility |
| 309 | |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 310 | // Test the Data Path Summary string |
| 311 | String dataPathSummaryStr = flowPathObj.getDataPathSummary(); |
| 312 | if (dataPathSummaryStr == null) |
| 313 | continue; // Could be invalid entry? |
| 314 | if (dataPathSummaryStr.isEmpty()) |
| 315 | continue; // No need to maintain this flow |
| 316 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 317 | // |
| 318 | // Test whether we need to complete the Flow cleanup, |
| 319 | // if the Flow has been deleted by the user. |
| 320 | // |
| 321 | String flowUserState = flowPathObj.getUserState(); |
| 322 | if ((flowUserState != null) |
| 323 | && flowUserState.equals("FE_USER_DELETE")) { |
| 324 | Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries(); |
| 325 | boolean empty = true; // TODO: an ugly hack |
| 326 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 327 | empty = false; |
| 328 | break; |
| 329 | } |
| 330 | if (empty) |
| 331 | deleteFlows.add(flowPathObj); |
| 332 | } |
| 333 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 334 | // Fetch the fields needed to recompute the shortest path |
| 335 | Short srcPortShort = flowPathObj.getSrcPort(); |
| 336 | String dstDpidStr = flowPathObj.getDstSwitch(); |
| 337 | Short dstPortShort = flowPathObj.getDstPort(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 338 | Long flowPathFlagsLong = flowPathObj.getFlowPathFlags(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 339 | if ((srcPortShort == null) || |
| 340 | (dstDpidStr == null) || |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 341 | (dstPortShort == null) || |
| 342 | (flowPathFlagsLong == null)) { |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 343 | continue; |
| 344 | } |
| 345 | |
| 346 | Port srcPort = new Port(srcPortShort); |
| 347 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 348 | Port dstPort = new Port(dstPortShort); |
| 349 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 350 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 351 | FlowPathFlags flowPathFlags = new FlowPathFlags(flowPathFlagsLong); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 352 | |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 353 | counterMyFlowPaths++; |
| 354 | |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 355 | // |
| 356 | // NOTE: Using here the regular getShortestPath() method |
| 357 | // won't work here, because that method calls internally |
| 358 | // "conn.endTx(Transaction.COMMIT)", and that will |
| 359 | // invalidate all handlers to the Titan database. |
| 360 | // If we want to experiment with calling here |
| 361 | // getShortestPath(), we need to refactor that code |
| 362 | // to avoid closing the transaction. |
| 363 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 364 | DataPath dataPath = |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 365 | topoRouteService.getTopoShortestPath(shortestPathTopo, |
| 366 | srcSwitchPort, |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 367 | dstSwitchPort); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 368 | if (dataPath == null) { |
| 369 | // We need the DataPath to compare the paths |
| 370 | dataPath = new DataPath(); |
| 371 | dataPath.setSrcPort(srcSwitchPort); |
| 372 | dataPath.setDstPort(dstSwitchPort); |
| 373 | } |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 374 | dataPath.applyFlowPathFlags(flowPathFlags); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 375 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 376 | String newDataPathSummaryStr = dataPath.dataPathSummary(); |
| 377 | if (dataPathSummaryStr.equals(newDataPathSummaryStr)) |
| 378 | continue; // Nothing changed |
| 379 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 380 | reconcileFlow(flowPathObj, dataPath); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 381 | } |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 382 | |
| 383 | // |
| 384 | // Delete all leftover Flows marked for deletion from the |
| 385 | // Network MAP. |
| 386 | // |
| 387 | while (! deleteFlows.isEmpty()) { |
| 388 | IFlowPath flowPathObj = deleteFlows.poll(); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 389 | op.removeFlowPath(flowPathObj); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 392 | topoRouteService.dropShortestPathTopo(shortestPathTopo); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 393 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 394 | op.commit(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 395 | |
| 396 | if (processed_measurement_flow) { |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 397 | long estimatedTime = |
| 398 | System.nanoTime() - modifiedMeasurementFlowTime; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 399 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 400 | (double)estimatedTime / 1000000000 + " sec"; |
| 401 | log.debug(logMsg); |
| 402 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 403 | |
| 404 | long estimatedTime = System.nanoTime() - startTime; |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 405 | double rate = 0.0; |
| 406 | if (estimatedTime > 0) |
| 407 | rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 408 | String logMsg = "MEASUREMENT: Processed AllFlowPaths: " + |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 409 | counterAllFlowPaths + " MyFlowPaths: " + |
| 410 | counterMyFlowPaths + " in " + |
| 411 | (double)estimatedTime / 1000000000 + " sec: " + |
| 412 | rate + " paths/s"; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 413 | log.debug(logMsg); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 414 | } |
| 415 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 416 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 417 | //final ScheduledFuture<?> mapReaderHandle = |
| 418 | //mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 419 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 420 | //final ScheduledFuture<?> shortestPathReconcileHandle = |
| 421 | //shortestPathReconcileScheduler.scheduleAtFixedRate(shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 422 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 423 | @Override |
| 424 | public void init(String conf) { |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 425 | op = new GraphDBOperation(conf); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 426 | topoRouteService = new TopoRouteService(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | public void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 430 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void close() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 435 | op.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 436 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 437 | |
| 438 | @Override |
| 439 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 440 | Collection<Class<? extends IFloodlightService>> l = |
| 441 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 442 | l.add(IFlowService.class); |
| 443 | return l; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 448 | getServiceImpls() { |
| 449 | Map<Class<? extends IFloodlightService>, |
| 450 | IFloodlightService> m = |
| 451 | new HashMap<Class<? extends IFloodlightService>, |
| 452 | IFloodlightService>(); |
| 453 | m.put(IFlowService.class, this); |
| 454 | return m; |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public Collection<Class<? extends IFloodlightService>> |
| 459 | getModuleDependencies() { |
| 460 | Collection<Class<? extends IFloodlightService>> l = |
| 461 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 462 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 463 | l.add(IRestApiService.class); |
| 464 | return l; |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public void init(FloodlightModuleContext context) |
| 469 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 470 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 471 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 472 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 473 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 474 | EnumSet.of(OFType.FLOW_MOD), |
| 475 | OFMESSAGE_DAMPER_TIMEOUT); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 476 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 477 | // TODO: An ugly hack! |
| 478 | String conf = "/tmp/cassandra.titan"; |
| 479 | this.init(conf); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 480 | |
| 481 | mapReaderScheduler = Executors.newScheduledThreadPool(1); |
| 482 | shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 485 | private synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 486 | // |
| 487 | // Generate the next Flow Entry ID. |
| 488 | // NOTE: For now, the higher 32 bits are random, and |
| 489 | // the lower 32 bits are sequential. |
| 490 | // In the future, we need a better allocation mechanism. |
| 491 | // |
| 492 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 493 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 494 | nextFlowEntryIdSuffix = 0; |
| 495 | } else { |
| 496 | nextFlowEntryIdSuffix++; |
| 497 | } |
| 498 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 499 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 500 | return result; |
| 501 | } |
| 502 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 503 | @Override |
| 504 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 505 | restApi.addRestletRoutable(new FlowWebRoutable()); |
| 506 | |
| 507 | // Initialize the Flow Entry ID generator |
| 508 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 509 | |
| 510 | mapReaderScheduler.scheduleAtFixedRate( |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 511 | mapReader, 3, 3, TimeUnit.SECONDS); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 512 | shortestPathReconcileScheduler.scheduleAtFixedRate( |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 513 | shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Add a flow. |
| 518 | * |
| 519 | * Internally, ONOS will automatically register the installer for |
| 520 | * receiving Flow Path Notifications for that path. |
| 521 | * |
| 522 | * @param flowPath the Flow Path to install. |
| 523 | * @param flowId the return-by-reference Flow ID as assigned internally. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 524 | * @param dataPathSummaryStr the data path summary string if the added |
| 525 | * flow will be maintained internally, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 526 | * @return true on success, otherwise false. |
| 527 | */ |
| 528 | @Override |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 529 | public boolean addFlow(FlowPath flowPath, FlowId flowId, |
| 530 | String dataPathSummaryStr) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 531 | /* |
| 532 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 533 | if (flowPath.flowId().value() == measurementFlowId) { |
| 534 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 535 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 536 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 537 | |
| 538 | IFlowPath flowObj = null; |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 539 | boolean found = false; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 540 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 541 | if ((flowObj = op.searchFlowPath(flowPath.flowId())) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 542 | != null) { |
| 543 | log.debug("Adding FlowPath with FlowId {}: found existing FlowPath", |
| 544 | flowPath.flowId().toString()); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 545 | found = true; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 546 | } else { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 547 | flowObj = op.newFlowPath(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 548 | log.debug("Adding FlowPath with FlowId {}: creating new FlowPath", |
| 549 | flowPath.flowId().toString()); |
| 550 | } |
| 551 | } catch (Exception e) { |
| 552 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 553 | op.rollback(); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 554 | |
| 555 | StringWriter sw = new StringWriter(); |
| 556 | e.printStackTrace(new PrintWriter(sw)); |
| 557 | String stacktrace = sw.toString(); |
| 558 | |
| 559 | log.error(":addFlow FlowId:{} failed: {}", |
| 560 | flowPath.flowId().toString(), |
| 561 | stacktrace); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 562 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 563 | if (flowObj == null) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 564 | log.error(":addFlow FlowId:{} failed: Flow object not created", |
| 565 | flowPath.flowId().toString()); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 566 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 567 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 568 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 569 | |
| 570 | // |
| 571 | // Set the Flow key: |
| 572 | // - flowId |
| 573 | // |
| 574 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 575 | flowObj.setType("flow"); |
| 576 | |
| 577 | // |
| 578 | // Set the Flow attributes: |
| 579 | // - flowPath.installerId() |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 580 | // - flowPath.flowPathFlags() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 581 | // - flowPath.dataPath().srcPort() |
| 582 | // - flowPath.dataPath().dstPort() |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 583 | // - flowPath.matchSrcMac() |
| 584 | // - flowPath.matchDstMac() |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 585 | // - flowPath.matchEthernetFrameType() |
| 586 | // - flowPath.matchVlanId() |
| 587 | // - flowPath.matchVlanPriority() |
| 588 | // - flowPath.matchSrcIPv4Net() |
| 589 | // - flowPath.matchDstIPv4Net() |
| 590 | // - flowPath.matchIpProto() |
| 591 | // - flowPath.matchIpToS() |
| 592 | // - flowPath.matchSrcTcpUdpPort() |
| 593 | // - flowPath.matchDstTcpUdpPort() |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 594 | // - flowPath.flowEntryActions() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 595 | // |
| 596 | flowObj.setInstallerId(flowPath.installerId().toString()); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 597 | flowObj.setFlowPathFlags(flowPath.flowPathFlags().flags()); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 598 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 599 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 600 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 601 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 602 | if (flowPath.flowEntryMatch().matchSrcMac()) { |
| 603 | flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString()); |
| 604 | } |
| 605 | if (flowPath.flowEntryMatch().matchDstMac()) { |
| 606 | flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString()); |
| 607 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 608 | if (flowPath.flowEntryMatch().matchEthernetFrameType()) { |
| 609 | flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType()); |
| 610 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 611 | if (flowPath.flowEntryMatch().matchVlanId()) { |
| 612 | flowObj.setMatchVlanId(flowPath.flowEntryMatch().vlanId()); |
| 613 | } |
| 614 | if (flowPath.flowEntryMatch().matchVlanPriority()) { |
| 615 | flowObj.setMatchVlanPriority(flowPath.flowEntryMatch().vlanPriority()); |
| 616 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 617 | if (flowPath.flowEntryMatch().matchSrcIPv4Net()) { |
| 618 | flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString()); |
| 619 | } |
| 620 | if (flowPath.flowEntryMatch().matchDstIPv4Net()) { |
| 621 | flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString()); |
| 622 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 623 | if (flowPath.flowEntryMatch().matchIpProto()) { |
| 624 | flowObj.setMatchIpProto(flowPath.flowEntryMatch().ipProto()); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 625 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 626 | if (flowPath.flowEntryMatch().matchIpToS()) { |
| 627 | flowObj.setMatchIpToS(flowPath.flowEntryMatch().ipToS()); |
| 628 | } |
| 629 | if (flowPath.flowEntryMatch().matchSrcTcpUdpPort()) { |
| 630 | flowObj.setMatchSrcTcpUdpPort(flowPath.flowEntryMatch().srcTcpUdpPort()); |
| 631 | } |
| 632 | if (flowPath.flowEntryMatch().matchDstTcpUdpPort()) { |
| 633 | flowObj.setMatchDstTcpUdpPort(flowPath.flowEntryMatch().dstTcpUdpPort()); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 634 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 635 | if (! flowPath.flowEntryActions().actions().isEmpty()) { |
| 636 | flowObj.setActions(flowPath.flowEntryActions().toString()); |
| 637 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 638 | |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 639 | if (dataPathSummaryStr != null) { |
| 640 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 641 | } else { |
| 642 | flowObj.setDataPathSummary(""); |
| 643 | } |
| 644 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 645 | if (found) |
| 646 | flowObj.setUserState("FE_USER_MODIFY"); |
| 647 | else |
| 648 | flowObj.setUserState("FE_USER_ADD"); |
| 649 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 650 | // Flow edges: |
| 651 | // HeadFE |
| 652 | |
| 653 | |
| 654 | // |
| 655 | // Flow Entries: |
| 656 | // flowPath.dataPath().flowEntries() |
| 657 | // |
| 658 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 659 | if (addFlowEntry(flowObj, flowEntry) == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 660 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 661 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 662 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 663 | } |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 664 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 665 | |
| 666 | // |
| 667 | // TODO: We need a proper Flow ID allocation mechanism. |
| 668 | // |
| 669 | flowId.setValue(flowPath.flowId().value()); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 670 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 671 | return true; |
| 672 | } |
| 673 | |
| 674 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 675 | * Add a flow entry to the Network MAP. |
| 676 | * |
| 677 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 678 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 679 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 680 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 681 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 682 | // Flow edges |
| 683 | // HeadFE (TODO) |
| 684 | |
| 685 | // |
| 686 | // Assign the FlowEntry ID. |
| 687 | // |
| 688 | if ((flowEntry.flowEntryId() == null) || |
| 689 | (flowEntry.flowEntryId().value() == 0)) { |
| 690 | long id = getNextFlowEntryId(); |
| 691 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 692 | } |
| 693 | |
| 694 | IFlowEntry flowEntryObj = null; |
| 695 | boolean found = false; |
| 696 | try { |
| 697 | if ((flowEntryObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 698 | op.searchFlowEntry(flowEntry.flowEntryId())) != null) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 699 | log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry", |
| 700 | flowEntry.flowEntryId().toString()); |
| 701 | found = true; |
| 702 | } else { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 703 | flowEntryObj = op.newFlowEntry(); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 704 | log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry", |
| 705 | flowEntry.flowEntryId().toString()); |
| 706 | } |
| 707 | } catch (Exception e) { |
| 708 | log.error(":addFlow FlowEntryId:{} failed", |
| 709 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 710 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 711 | } |
| 712 | if (flowEntryObj == null) { |
| 713 | log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created", |
| 714 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 715 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | // |
| 719 | // Set the Flow Entry key: |
| 720 | // - flowEntry.flowEntryId() |
| 721 | // |
| 722 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 723 | flowEntryObj.setType("flow_entry"); |
| 724 | |
| 725 | // |
| 726 | // Set the Flow Entry Edges and attributes: |
| 727 | // - Switch edge |
| 728 | // - InPort edge |
| 729 | // - OutPort edge |
| 730 | // |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 731 | // - flowEntry.dpid() |
| 732 | // - flowEntry.flowEntryUserState() |
| 733 | // - flowEntry.flowEntrySwitchState() |
| 734 | // - flowEntry.flowEntryErrorState() |
| 735 | // - flowEntry.matchInPort() |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 736 | // - flowEntry.matchSrcMac() |
| 737 | // - flowEntry.matchDstMac() |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 738 | // - flowEntry.matchEthernetFrameType() |
| 739 | // - flowEntry.matchVlanId() |
| 740 | // - flowEntry.matchVlanPriority() |
| 741 | // - flowEntry.matchSrcIPv4Net() |
| 742 | // - flowEntry.matchDstIPv4Net() |
| 743 | // - flowEntry.matchIpProto() |
| 744 | // - flowEntry.matchIpToS() |
| 745 | // - flowEntry.matchSrcTcpUdpPort() |
| 746 | // - flowEntry.matchDstTcpUdpPort() |
Pavlin Radoslavov | c1bafd1 | 2013-07-12 17:00:35 -0700 | [diff] [blame] | 747 | // - flowEntry.actionOutputPort() |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 748 | // - flowEntry.actions() |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 749 | // |
| 750 | ISwitchObject sw = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 751 | op.searchSwitch(flowEntry.dpid().toString()); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 752 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
| 753 | flowEntryObj.setSwitch(sw); |
| 754 | if (flowEntry.flowEntryMatch().matchInPort()) { |
| 755 | IPortObject inport = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 756 | op.searchPort(flowEntry.dpid().toString(), |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 757 | flowEntry.flowEntryMatch().inPort().value()); |
| 758 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
| 759 | flowEntryObj.setInPort(inport); |
| 760 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 761 | if (flowEntry.flowEntryMatch().matchSrcMac()) { |
| 762 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 763 | } |
| 764 | if (flowEntry.flowEntryMatch().matchDstMac()) { |
| 765 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 766 | } |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 767 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) { |
| 768 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 769 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 770 | if (flowEntry.flowEntryMatch().matchVlanId()) { |
| 771 | flowEntryObj.setMatchVlanId(flowEntry.flowEntryMatch().vlanId()); |
| 772 | } |
| 773 | if (flowEntry.flowEntryMatch().matchVlanPriority()) { |
| 774 | flowEntryObj.setMatchVlanPriority(flowEntry.flowEntryMatch().vlanPriority()); |
| 775 | } |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 776 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) { |
| 777 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 778 | } |
| 779 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) { |
| 780 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 781 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 782 | if (flowEntry.flowEntryMatch().matchIpProto()) { |
| 783 | flowEntryObj.setMatchIpProto(flowEntry.flowEntryMatch().ipProto()); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 784 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 785 | if (flowEntry.flowEntryMatch().matchIpToS()) { |
| 786 | flowEntryObj.setMatchIpToS(flowEntry.flowEntryMatch().ipToS()); |
| 787 | } |
| 788 | if (flowEntry.flowEntryMatch().matchSrcTcpUdpPort()) { |
| 789 | flowEntryObj.setMatchSrcTcpUdpPort(flowEntry.flowEntryMatch().srcTcpUdpPort()); |
| 790 | } |
| 791 | if (flowEntry.flowEntryMatch().matchDstTcpUdpPort()) { |
| 792 | flowEntryObj.setMatchDstTcpUdpPort(flowEntry.flowEntryMatch().dstTcpUdpPort()); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 795 | for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 796 | if (fa.actionOutput() != null) { |
| 797 | IPortObject outport = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 798 | op.searchPort(flowEntry.dpid().toString(), |
| 799 | fa.actionOutput().port().value()); |
Pavlin Radoslavov | c1bafd1 | 2013-07-12 17:00:35 -0700 | [diff] [blame] | 800 | flowEntryObj.setActionOutputPort(fa.actionOutput().port().value()); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 801 | flowEntryObj.setOutPort(outport); |
| 802 | } |
| 803 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 804 | if (! flowEntry.flowEntryActions().isEmpty()) { |
| 805 | flowEntryObj.setActions(flowEntry.flowEntryActions().toString()); |
| 806 | } |
| 807 | |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 808 | // TODO: Hacks with hard-coded state names! |
| 809 | if (found) |
| 810 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 811 | else |
| 812 | flowEntryObj.setUserState("FE_USER_ADD"); |
| 813 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 814 | // |
| 815 | // TODO: Take care of the FlowEntryErrorState. |
| 816 | // |
| 817 | |
| 818 | // Flow Entries edges: |
| 819 | // Flow |
| 820 | // NextFE (TODO) |
| 821 | if (! found) { |
| 822 | flowObj.addFlowEntry(flowEntryObj); |
| 823 | flowEntryObj.setFlow(flowObj); |
| 824 | } |
| 825 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 826 | return flowEntryObj; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 830 | * Delete all previously added flows. |
| 831 | * |
| 832 | * @return true on success, otherwise false. |
| 833 | */ |
| 834 | @Override |
| 835 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 836 | List<Thread> threads = new LinkedList<Thread>(); |
| 837 | final ConcurrentLinkedQueue<FlowId> concurrentAllFlowIds = |
| 838 | new ConcurrentLinkedQueue<FlowId>(); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 839 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 840 | // Get all Flow IDs |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 841 | Iterable<IFlowPath> allFlowPaths = op.getAllFlowPaths(); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 842 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 843 | if (flowPathObj == null) |
| 844 | continue; |
| 845 | String flowIdStr = flowPathObj.getFlowId(); |
| 846 | if (flowIdStr == null) |
| 847 | continue; |
| 848 | FlowId flowId = new FlowId(flowIdStr); |
| 849 | concurrentAllFlowIds.add(flowId); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 852 | // Delete all flows one-by-one |
| 853 | for (FlowId flowId : concurrentAllFlowIds) |
| 854 | deleteFlow(flowId); |
| 855 | |
| 856 | /* |
| 857 | * TODO: A faster mechanism to delete the Flow Paths by using |
| 858 | * a number of threads. Commented-out for now. |
| 859 | */ |
| 860 | /* |
| 861 | // |
| 862 | // Create the threads to delete the Flow Paths |
| 863 | // |
| 864 | for (int i = 0; i < 10; i++) { |
| 865 | Thread thread = new Thread(new Runnable() { |
| 866 | @Override |
| 867 | public void run() { |
| 868 | while (true) { |
| 869 | FlowId flowId = concurrentAllFlowIds.poll(); |
| 870 | if (flowId == null) |
| 871 | return; |
| 872 | deleteFlow(flowId); |
| 873 | } |
| 874 | }}, "Delete All Flow Paths"); |
| 875 | threads.add(thread); |
| 876 | } |
| 877 | |
| 878 | // Start processing |
| 879 | for (Thread thread : threads) { |
| 880 | thread.start(); |
| 881 | } |
| 882 | |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 883 | // Wait for all threads to complete |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 884 | for (Thread thread : threads) { |
| 885 | try { |
| 886 | thread.join(); |
| 887 | } catch (InterruptedException e) { |
| 888 | log.debug("Exception waiting for a thread to delete a Flow Path: ", e); |
| 889 | } |
| 890 | } |
| 891 | */ |
| 892 | |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 893 | return true; |
| 894 | } |
| 895 | |
| 896 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 897 | * Delete a previously added flow. |
| 898 | * |
| 899 | * @param flowId the Flow ID of the flow to delete. |
| 900 | * @return true on success, otherwise false. |
| 901 | */ |
| 902 | @Override |
| 903 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 904 | /* |
| 905 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 906 | if (flowId.value() == measurementFlowId) { |
| 907 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 908 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 909 | */ |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 910 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 911 | IFlowPath flowObj = null; |
| 912 | // |
| 913 | // We just mark the entries for deletion, |
| 914 | // and let the switches remove each individual entry after |
| 915 | // it has been removed from the switches. |
| 916 | // |
| 917 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 918 | if ((flowObj = op.searchFlowPath(flowId)) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 919 | != null) { |
| 920 | log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath", |
| 921 | flowId.toString()); |
| 922 | } else { |
| 923 | log.debug("Deleting FlowPath with FlowId {}: FlowPath not found", |
| 924 | flowId.toString()); |
| 925 | } |
| 926 | } catch (Exception e) { |
| 927 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 928 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 929 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
| 930 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 931 | if (flowObj == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 932 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 933 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 934 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 935 | |
| 936 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 937 | // Find and mark for deletion all Flow Entries, |
| 938 | // and the Flow itself. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 939 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 940 | flowObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 941 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 942 | boolean empty = true; // TODO: an ugly hack |
| 943 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 944 | empty = false; |
| 945 | // flowObj.removeFlowEntry(flowEntryObj); |
| 946 | // conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 947 | flowEntryObj.setUserState("FE_USER_DELETE"); |
| 948 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 949 | } |
| 950 | // Remove from the database empty flows |
| 951 | if (empty) |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 952 | op.removeFlowPath(flowObj); |
| 953 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 954 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 955 | return true; |
| 956 | } |
| 957 | |
| 958 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 959 | * Clear the state for all previously added flows. |
| 960 | * |
| 961 | * @return true on success, otherwise false. |
| 962 | */ |
| 963 | @Override |
| 964 | public boolean clearAllFlows() { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 965 | List<FlowId> allFlowIds = new LinkedList<FlowId>(); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 966 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 967 | // Get all Flow IDs |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 968 | Iterable<IFlowPath> allFlowPaths = op.getAllFlowPaths(); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 969 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 970 | if (flowPathObj == null) |
| 971 | continue; |
| 972 | String flowIdStr = flowPathObj.getFlowId(); |
| 973 | if (flowIdStr == null) |
| 974 | continue; |
| 975 | FlowId flowId = new FlowId(flowIdStr); |
| 976 | allFlowIds.add(flowId); |
| 977 | } |
| 978 | |
| 979 | // Clear all flows one-by-one |
| 980 | for (FlowId flowId : allFlowIds) { |
| 981 | clearFlow(flowId); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 988 | * Clear the state for a previously added flow. |
| 989 | * |
| 990 | * @param flowId the Flow ID of the flow to clear. |
| 991 | * @return true on success, otherwise false. |
| 992 | */ |
| 993 | @Override |
| 994 | public boolean clearFlow(FlowId flowId) { |
| 995 | IFlowPath flowObj = null; |
| 996 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 997 | if ((flowObj = op.searchFlowPath(flowId)) |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 998 | != null) { |
| 999 | log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath", |
| 1000 | flowId.toString()); |
| 1001 | } else { |
| 1002 | log.debug("Clearing FlowPath with FlowId {}: FlowPath not found", |
| 1003 | flowId.toString()); |
| 1004 | } |
| 1005 | } catch (Exception e) { |
| 1006 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1007 | op.rollback(); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 1008 | log.error(":clearFlow FlowId:{} failed", flowId.toString()); |
| 1009 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1010 | if (flowObj == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1011 | op.commit(); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 1012 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1013 | } |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 1014 | |
| 1015 | // |
| 1016 | // Remove all Flow Entries |
| 1017 | // |
| 1018 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1019 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 1020 | flowObj.removeFlowEntry(flowEntryObj); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1021 | op.removeFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 1022 | } |
| 1023 | // Remove the Flow itself |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1024 | op.removeFlowPath(flowObj); |
| 1025 | op.commit(); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 1026 | |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1031 | * Get a previously added flow. |
| 1032 | * |
| 1033 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1034 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1035 | */ |
| 1036 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1037 | public FlowPath getFlow(FlowId flowId) { |
| 1038 | IFlowPath flowObj = null; |
| 1039 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1040 | if ((flowObj = op.searchFlowPath(flowId)) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1041 | != null) { |
| 1042 | log.debug("Get FlowPath with FlowId {}: found existing FlowPath", |
| 1043 | flowId.toString()); |
| 1044 | } else { |
| 1045 | log.debug("Get FlowPath with FlowId {}: FlowPath not found", |
| 1046 | flowId.toString()); |
| 1047 | } |
| 1048 | } catch (Exception e) { |
| 1049 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1050 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1051 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 1052 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1053 | if (flowObj == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1054 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1055 | return null; // Flow not found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1056 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1057 | |
| 1058 | // |
| 1059 | // Extract the Flow state |
| 1060 | // |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1061 | FlowPath flowPath = extractFlowPath(flowObj); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1062 | op.commit(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1063 | |
| 1064 | return flowPath; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * Get all previously added flows by a specific installer for a given |
| 1069 | * data path endpoints. |
| 1070 | * |
| 1071 | * @param installerId the Caller ID of the installer of the flow to get. |
| 1072 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 1073 | * @return the Flow Paths if found, otherwise null. |
| 1074 | */ |
| 1075 | @Override |
| 1076 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 1077 | DataPathEndpoints dataPathEndpoints) { |
| 1078 | // |
| 1079 | // TODO: The implementation below is not optimal: |
| 1080 | // We fetch all flows, and then return only the subset that match |
| 1081 | // the query conditions. |
| 1082 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1083 | // the flows as appropriate. |
| 1084 | // |
| 1085 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1086 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1087 | |
| 1088 | if (allFlows == null) { |
| 1089 | 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] | 1090 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1093 | for (FlowPath flow : allFlows) { |
| 1094 | // |
| 1095 | // TODO: String-based comparison is sub-optimal. |
| 1096 | // 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] | 1097 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1098 | // |
| 1099 | if (! flow.installerId().toString().equals(installerId.toString())) |
| 1100 | continue; |
| 1101 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 1102 | continue; |
| 1103 | } |
| 1104 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 1105 | continue; |
| 1106 | } |
| 1107 | flowPaths.add(flow); |
| 1108 | } |
| 1109 | |
| 1110 | if (flowPaths.isEmpty()) { |
| 1111 | 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] | 1112 | } else { |
| 1113 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints); |
| 1114 | } |
| 1115 | |
| 1116 | return flowPaths; |
| 1117 | } |
| 1118 | |
| 1119 | /** |
| 1120 | * Get all installed flows by all installers for given data path endpoints. |
| 1121 | * |
| 1122 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 1123 | * @return the Flow Paths if found, otherwise null. |
| 1124 | */ |
| 1125 | @Override |
| 1126 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
| 1127 | // |
| 1128 | // TODO: The implementation below is not optimal: |
| 1129 | // We fetch all flows, and then return only the subset that match |
| 1130 | // the query conditions. |
| 1131 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1132 | // the flows as appropriate. |
| 1133 | // |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1134 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1135 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1136 | |
| 1137 | if (allFlows == null) { |
| 1138 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1139 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1142 | for (FlowPath flow : allFlows) { |
| 1143 | // |
| 1144 | // TODO: String-based comparison is sub-optimal. |
| 1145 | // 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] | 1146 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1147 | // |
| 1148 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 1149 | continue; |
| 1150 | } |
| 1151 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 1152 | continue; |
| 1153 | } |
| 1154 | flowPaths.add(flow); |
| 1155 | } |
| 1156 | |
| 1157 | if (flowPaths.isEmpty()) { |
| 1158 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1159 | } else { |
| 1160 | log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints); |
| 1161 | } |
| 1162 | |
| 1163 | return flowPaths; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1167 | * Get summary of all installed flows by all installers in a given range |
| 1168 | * |
| 1169 | * @param flowId the data path endpoints of the flows to get. |
| 1170 | * @param maxFlows: the maximum number of flows to be returned |
| 1171 | * @return the Flow Paths if found, otherwise null. |
| 1172 | */ |
| 1173 | @Override |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1174 | public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) { |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1175 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1176 | // TODO: The implementation below is not optimal: |
| 1177 | // We fetch all flows, and then return only the subset that match |
| 1178 | // the query conditions. |
| 1179 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1180 | // the flows as appropriate. |
| 1181 | // |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1182 | //ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1183 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1184 | ArrayList<IFlowPath> flowPathsWithoutFlowEntries = getAllFlowsWithoutFlowEntries(); |
| 1185 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1186 | Collections.sort(flowPathsWithoutFlowEntries, |
| 1187 | new Comparator<IFlowPath>(){ |
| 1188 | @Override |
| 1189 | public int compare(IFlowPath first, IFlowPath second) { |
| 1190 | // TODO Auto-generated method stub |
| 1191 | long result = new FlowId(first.getFlowId()).value() |
| 1192 | - new FlowId(second.getFlowId()).value(); |
| 1193 | if (result > 0) return 1; |
| 1194 | else if (result < 0) return -1; |
| 1195 | else return 0; |
| 1196 | } |
| 1197 | } |
| 1198 | ); |
| 1199 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1200 | return flowPathsWithoutFlowEntries; |
| 1201 | |
| 1202 | /* |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1203 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1204 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1205 | if (allFlows == null) { |
| 1206 | log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1207 | return flowPaths; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Umesh Krishnaswamy | 244b4ae | 2013-03-29 12:05:15 -0700 | [diff] [blame] | 1210 | Collections.sort(allFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1211 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1212 | for (FlowPath flow : allFlows) { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1213 | flow.setFlowEntryMatch(null); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1214 | |
Pavlin Radoslavov | 96b4342 | 2013-04-04 19:14:56 -0700 | [diff] [blame] | 1215 | // start from desired flowId |
| 1216 | if (flow.flowId().value() < flowId.value()) { |
| 1217 | continue; |
| 1218 | } |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1219 | |
| 1220 | // Summarize by making null flow entry fields that are not relevant to report |
| 1221 | for (FlowEntry flowEntry : flow.dataPath().flowEntries()) { |
| 1222 | flowEntry.setFlowEntryActions(null); |
| 1223 | flowEntry.setFlowEntryMatch(null); |
| 1224 | } |
| 1225 | |
| 1226 | flowPaths.add(flow); |
| 1227 | if (maxFlows != 0 && flowPaths.size() >= maxFlows) { |
| 1228 | break; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (flowPaths.isEmpty()) { |
| 1233 | log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1234 | } else { |
| 1235 | log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows); |
| 1236 | } |
| 1237 | |
| 1238 | return flowPaths; |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1239 | */ |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /** |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1243 | * Get all installed flows by all installers. |
| 1244 | * |
| 1245 | * @return the Flow Paths if found, otherwise null. |
| 1246 | */ |
| 1247 | @Override |
| 1248 | public ArrayList<FlowPath> getAllFlows() { |
| 1249 | Iterable<IFlowPath> flowPathsObj = null; |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1250 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1251 | |
| 1252 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1253 | if ((flowPathsObj = op.getAllFlowPaths()) != null) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1254 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1255 | } else { |
| 1256 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1257 | } |
| 1258 | } catch (Exception e) { |
| 1259 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1260 | op.rollback(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1261 | log.error(":getAllFlowPaths failed"); |
| 1262 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1263 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1264 | op.commit(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1265 | return flowPaths; // No Flows found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1266 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1267 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1268 | for (IFlowPath flowObj : flowPathsObj) { |
| 1269 | // |
| 1270 | // Extract the Flow state |
| 1271 | // |
| 1272 | FlowPath flowPath = extractFlowPath(flowObj); |
Pavlin Radoslavov | 3f2af73 | 2013-03-29 15:29:35 -0700 | [diff] [blame] | 1273 | if (flowPath != null) |
| 1274 | flowPaths.add(flowPath); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1277 | op.commit(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1278 | |
| 1279 | return flowPaths; |
| 1280 | } |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1281 | |
| 1282 | public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(){ |
| 1283 | Iterable<IFlowPath> flowPathsObj = null; |
| 1284 | ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>(); |
| 1285 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1286 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1287 | op.commit(); |
Jonathan Hart | e6e9187 | 2013-04-13 11:10:32 -0700 | [diff] [blame] | 1288 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1289 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1290 | if ((flowPathsObj = op.getAllFlowPaths()) != null) { |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1291 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1292 | } else { |
| 1293 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1294 | } |
| 1295 | } catch (Exception e) { |
| 1296 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1297 | op.rollback(); |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1298 | log.error(":getAllFlowPaths failed"); |
| 1299 | } |
| 1300 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1301 | return new ArrayList<IFlowPath>(); // No Flows found |
| 1302 | } |
| 1303 | |
| 1304 | for (IFlowPath flowObj : flowPathsObj){ |
| 1305 | flowPathsObjArray.add(flowObj); |
| 1306 | } |
| 1307 | /* |
| 1308 | for (IFlowPath flowObj : flowPathsObj) { |
| 1309 | // |
| 1310 | // Extract the Flow state |
| 1311 | // |
| 1312 | FlowPath flowPath = extractFlowPath(flowObj); |
| 1313 | if (flowPath != null) |
| 1314 | flowPaths.add(flowPath); |
| 1315 | } |
| 1316 | */ |
| 1317 | |
| 1318 | //conn.endTx(Transaction.COMMIT); |
| 1319 | |
| 1320 | return flowPathsObjArray; |
| 1321 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1322 | |
| 1323 | /** |
| 1324 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 1325 | * |
| 1326 | * @param flowObj the object to extract the Flow Path State from. |
| 1327 | * @return the extracted Flow Path State. |
| 1328 | */ |
| 1329 | private FlowPath extractFlowPath(IFlowPath flowObj) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1330 | // |
| 1331 | // Extract the Flow state |
| 1332 | // |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1333 | String flowIdStr = flowObj.getFlowId(); |
| 1334 | String installerIdStr = flowObj.getInstallerId(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 1335 | Long flowPathFlags = flowObj.getFlowPathFlags(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1336 | String srcSwitchStr = flowObj.getSrcSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1337 | Short srcPortShort = flowObj.getSrcPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1338 | String dstSwitchStr = flowObj.getDstSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1339 | Short dstPortShort = flowObj.getDstPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1340 | |
| 1341 | if ((flowIdStr == null) || |
| 1342 | (installerIdStr == null) || |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 1343 | (flowPathFlags == null) || |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1344 | (srcSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1345 | (srcPortShort == null) || |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1346 | (dstSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1347 | (dstPortShort == null)) { |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1348 | // TODO: A work-around, becauuse of some bogus database objects |
| 1349 | return null; |
| 1350 | } |
| 1351 | |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1352 | FlowPath flowPath = new FlowPath(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1353 | flowPath.setFlowId(new FlowId(flowIdStr)); |
| 1354 | flowPath.setInstallerId(new CallerId(installerIdStr)); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 1355 | flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags)); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1356 | flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1357 | flowPath.dataPath().srcPort().setPort(new Port(srcPortShort)); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1358 | flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1359 | flowPath.dataPath().dstPort().setPort(new Port(dstPortShort)); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1360 | // |
| 1361 | // Extract the match conditions common for all Flow Entries |
| 1362 | // |
| 1363 | { |
| 1364 | FlowEntryMatch match = new FlowEntryMatch(); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1365 | String matchSrcMac = flowObj.getMatchSrcMac(); |
| 1366 | if (matchSrcMac != null) |
| 1367 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1368 | String matchDstMac = flowObj.getMatchDstMac(); |
| 1369 | if (matchDstMac != null) |
| 1370 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1371 | Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1372 | if (matchEthernetFrameType != null) |
| 1373 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1374 | Short matchVlanId = flowObj.getMatchVlanId(); |
| 1375 | if (matchVlanId != null) |
| 1376 | match.enableVlanId(matchVlanId); |
| 1377 | Byte matchVlanPriority = flowObj.getMatchVlanPriority(); |
| 1378 | if (matchVlanPriority != null) |
| 1379 | match.enableVlanPriority(matchVlanPriority); |
| 1380 | String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1381 | if (matchSrcIPv4Net != null) |
| 1382 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1383 | String matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1384 | if (matchDstIPv4Net != null) |
| 1385 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1386 | Byte matchIpProto = flowObj.getMatchIpProto(); |
| 1387 | if (matchIpProto != null) |
| 1388 | match.enableIpProto(matchIpProto); |
| 1389 | Byte matchIpToS = flowObj.getMatchIpToS(); |
| 1390 | if (matchIpToS != null) |
| 1391 | match.enableIpToS(matchIpToS); |
| 1392 | Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort(); |
| 1393 | if (matchSrcTcpUdpPort != null) |
| 1394 | match.enableSrcTcpUdpPort(matchSrcTcpUdpPort); |
| 1395 | Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort(); |
| 1396 | if (matchDstTcpUdpPort != null) |
| 1397 | match.enableDstTcpUdpPort(matchDstTcpUdpPort); |
| 1398 | |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1399 | flowPath.setFlowEntryMatch(match); |
| 1400 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1401 | // |
| 1402 | // Extract the actions for the first Flow Entry |
| 1403 | // |
| 1404 | { |
| 1405 | String actionsStr = flowObj.getActions(); |
| 1406 | if (actionsStr != null) { |
| 1407 | FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr); |
| 1408 | flowPath.setFlowEntryActions(flowEntryActions); |
| 1409 | } |
| 1410 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1411 | |
| 1412 | // |
| 1413 | // Extract all Flow Entries |
| 1414 | // |
| 1415 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1416 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1417 | FlowEntry flowEntry = extractFlowEntry(flowEntryObj); |
| 1418 | if (flowEntry == null) |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1419 | continue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1420 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 1421 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1422 | |
| 1423 | return flowPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1424 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1425 | |
| 1426 | /** |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1427 | * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry. |
| 1428 | * |
| 1429 | * @param flowEntryObj the object to extract the Flow Entry State from. |
| 1430 | * @return the extracted Flow Entry State. |
| 1431 | */ |
| 1432 | private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) { |
| 1433 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1434 | String switchDpidStr = flowEntryObj.getSwitchDpid(); |
| 1435 | String userState = flowEntryObj.getUserState(); |
| 1436 | String switchState = flowEntryObj.getSwitchState(); |
| 1437 | |
| 1438 | if ((flowEntryIdStr == null) || |
| 1439 | (switchDpidStr == null) || |
| 1440 | (userState == null) || |
| 1441 | (switchState == null)) { |
| 1442 | // TODO: A work-around, becauuse of some bogus database objects |
| 1443 | return null; |
| 1444 | } |
| 1445 | |
| 1446 | FlowEntry flowEntry = new FlowEntry(); |
| 1447 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr)); |
| 1448 | flowEntry.setDpid(new Dpid(switchDpidStr)); |
| 1449 | |
| 1450 | // |
| 1451 | // Extract the match conditions |
| 1452 | // |
| 1453 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1454 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1455 | if (matchInPort != null) |
| 1456 | match.enableInPort(new Port(matchInPort)); |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1457 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1458 | if (matchSrcMac != null) |
| 1459 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1460 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1461 | if (matchDstMac != null) |
| 1462 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1463 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1464 | if (matchEthernetFrameType != null) |
| 1465 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1466 | Short matchVlanId = flowEntryObj.getMatchVlanId(); |
| 1467 | if (matchVlanId != null) |
| 1468 | match.enableVlanId(matchVlanId); |
| 1469 | Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority(); |
| 1470 | if (matchVlanPriority != null) |
| 1471 | match.enableVlanPriority(matchVlanPriority); |
| 1472 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1473 | if (matchSrcIPv4Net != null) |
| 1474 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1475 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1476 | if (matchDstIPv4Net != null) |
| 1477 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1478 | Byte matchIpProto = flowEntryObj.getMatchIpProto(); |
| 1479 | if (matchIpProto != null) |
| 1480 | match.enableIpProto(matchIpProto); |
| 1481 | Byte matchIpToS = flowEntryObj.getMatchIpToS(); |
| 1482 | if (matchIpToS != null) |
| 1483 | match.enableIpToS(matchIpToS); |
| 1484 | Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort(); |
| 1485 | if (matchSrcTcpUdpPort != null) |
| 1486 | match.enableSrcTcpUdpPort(matchSrcTcpUdpPort); |
| 1487 | Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort(); |
| 1488 | if (matchDstTcpUdpPort != null) |
| 1489 | match.enableDstTcpUdpPort(matchDstTcpUdpPort); |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1490 | flowEntry.setFlowEntryMatch(match); |
| 1491 | |
| 1492 | // |
| 1493 | // Extract the actions |
| 1494 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1495 | FlowEntryActions actions = new FlowEntryActions(); |
| 1496 | String actionsStr = flowEntryObj.getActions(); |
| 1497 | if (actionsStr != null) |
| 1498 | actions = new FlowEntryActions(actionsStr); |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1499 | flowEntry.setFlowEntryActions(actions); |
| 1500 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 1501 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 1502 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1503 | // TODO: Take care of FlowEntryErrorState. |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1504 | // |
| 1505 | return flowEntry; |
| 1506 | } |
| 1507 | |
| 1508 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1509 | * Add and maintain a shortest-path flow. |
| 1510 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1511 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1512 | * |
| 1513 | * @param flowPath the Flow Path with the endpoints and the match |
| 1514 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1515 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1516 | */ |
| 1517 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1518 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1519 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1520 | // Don't do the shortest path computation here. |
| 1521 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1522 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1523 | |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1524 | // We need the DataPath to populate the Network MAP |
| 1525 | DataPath dataPath = new DataPath(); |
| 1526 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 1527 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1528 | |
| 1529 | // |
| 1530 | // Prepare the computed Flow Path |
| 1531 | // |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1532 | FlowPath computedFlowPath = new FlowPath(); |
| 1533 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 1534 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 1535 | computedFlowPath.setFlowPathFlags(new FlowPathFlags(flowPath.flowPathFlags().flags())); |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1536 | computedFlowPath.setDataPath(dataPath); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1537 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1538 | computedFlowPath.setFlowEntryActions(new FlowEntryActions(flowPath.flowEntryActions())); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1539 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1540 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1541 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1542 | if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1543 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1544 | |
| 1545 | // TODO: Mark the flow for maintenance purpose |
| 1546 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1547 | return (computedFlowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1551 | * Reconcile a flow. |
| 1552 | * |
| 1553 | * @param flowObj the flow that needs to be reconciliated. |
| 1554 | * @param newDataPath the new data path to use. |
| 1555 | * @return true on success, otherwise false. |
| 1556 | */ |
| 1557 | public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
| 1558 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 1559 | |
| 1560 | // |
| 1561 | // Set the incoming port matching and the outgoing port output |
| 1562 | // actions for each flow entry. |
| 1563 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1564 | int idx = 0; |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1565 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
| 1566 | // Set the incoming port matching |
| 1567 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 1568 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 1569 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 1570 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1571 | // |
| 1572 | // Set the actions |
| 1573 | // |
| 1574 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 1575 | // |
| 1576 | // If the first Flow Entry, copy the Flow Path actions to it |
| 1577 | // |
| 1578 | if (idx == 0) { |
| 1579 | String actionsStr = flowObj.getActions(); |
| 1580 | if (actionsStr != null) { |
| 1581 | FlowEntryActions flowActions = new FlowEntryActions(actionsStr); |
| 1582 | for (FlowEntryAction action : flowActions.actions()) |
| 1583 | flowEntryActions.addAction(action); |
| 1584 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1585 | } |
Pavlin Radoslavov | 282f4ff | 2013-07-18 11:21:37 -0700 | [diff] [blame] | 1586 | idx++; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1587 | // |
| 1588 | // Add the outgoing port output action |
| 1589 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1590 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1591 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1592 | flowEntryActions.addAction(flowEntryAction); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // |
| 1596 | // Remove the old Flow Entries, and add the new Flow Entries |
| 1597 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1598 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1599 | LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>(); |
| 1600 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1601 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1602 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1603 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1604 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1605 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | // |
| 1609 | // Set the Data Path Summary |
| 1610 | // |
| 1611 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 1612 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 1613 | |
| 1614 | return true; |
| 1615 | } |
| 1616 | |
| 1617 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1618 | * Reconcile all flows in a set. |
| 1619 | * |
| 1620 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 1621 | */ |
| 1622 | public void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
| 1623 | if (! flowObjSet.iterator().hasNext()) |
| 1624 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 1625 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1629 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1630 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1631 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1632 | * @param flowObj the flow path object for the flow entry to install. |
| 1633 | * @param flowEntryObj the flow entry object to install. |
| 1634 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1635 | */ |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1636 | public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
| 1637 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1638 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1639 | if (flowEntryIdStr == null) |
| 1640 | return false; |
| 1641 | FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1642 | String userState = flowEntryObj.getUserState(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1643 | if (userState == null) |
| 1644 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1645 | |
| 1646 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1647 | // Create the Open Flow Flow Modification Entry to push |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1648 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1649 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1650 | .getMessage(OFType.FLOW_MOD); |
| 1651 | long cookie = flowEntryId.value(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1652 | |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1653 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1654 | if (userState.equals("FE_USER_ADD")) { |
| 1655 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1656 | } else if (userState.equals("FE_USER_MODIFY")) { |
| 1657 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1658 | } else if (userState.equals("FE_USER_DELETE")) { |
| 1659 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1660 | } else { |
| 1661 | // Unknown user state. Ignore the entry |
| 1662 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1663 | flowEntryId.toString(), userState); |
| 1664 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1668 | // Fetch the match conditions. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1669 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1670 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1671 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1672 | // condition set. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1673 | // |
| 1674 | OFMatch match = new OFMatch(); |
| 1675 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1676 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1677 | // Match the Incoming Port |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1678 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1679 | if (matchInPort != null) { |
| 1680 | match.setInputPort(matchInPort); |
| 1681 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1682 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1683 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1684 | // Match the Source MAC address |
| 1685 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1686 | if (matchSrcMac == null) |
| 1687 | matchSrcMac = flowObj.getMatchSrcMac(); |
| 1688 | if (matchSrcMac != null) { |
| 1689 | match.setDataLayerSource(matchSrcMac); |
| 1690 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1691 | } |
| 1692 | |
| 1693 | // Match the Destination MAC address |
| 1694 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1695 | if (matchDstMac == null) |
| 1696 | matchDstMac = flowObj.getMatchDstMac(); |
| 1697 | if (matchDstMac != null) { |
| 1698 | match.setDataLayerDestination(matchDstMac); |
| 1699 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1700 | } |
| 1701 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1702 | // Match the Ethernet Frame Type |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1703 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1704 | if (matchEthernetFrameType == null) |
| 1705 | matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1706 | if (matchEthernetFrameType != null) { |
| 1707 | match.setDataLayerType(matchEthernetFrameType); |
| 1708 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1709 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1710 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1711 | // Match the VLAN ID |
| 1712 | Short matchVlanId = flowEntryObj.getMatchVlanId(); |
| 1713 | if (matchVlanId == null) |
| 1714 | matchVlanId = flowObj.getMatchVlanId(); |
| 1715 | if (matchVlanId != null) { |
| 1716 | match.setDataLayerVirtualLan(matchVlanId); |
| 1717 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 1718 | } |
| 1719 | |
| 1720 | // Match the VLAN priority |
| 1721 | Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority(); |
| 1722 | if (matchVlanPriority == null) |
| 1723 | matchVlanPriority = flowObj.getMatchVlanPriority(); |
| 1724 | if (matchVlanPriority != null) { |
| 1725 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 1726 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 1727 | } |
| 1728 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1729 | // Match the Source IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1730 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1731 | if (matchSrcIPv4Net == null) |
| 1732 | matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1733 | if (matchSrcIPv4Net != null) { |
| 1734 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 1735 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1736 | |
| 1737 | // Natch the Destination IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1738 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1739 | if (matchDstIPv4Net == null) |
| 1740 | matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1741 | if (matchDstIPv4Net != null) { |
| 1742 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 1743 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1744 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1745 | // Match the IP protocol |
| 1746 | Byte matchIpProto = flowEntryObj.getMatchIpProto(); |
| 1747 | if (matchIpProto == null) |
| 1748 | matchIpProto = flowObj.getMatchIpProto(); |
| 1749 | if (matchIpProto != null) { |
Pavlin Radoslavov | 3e69d7d | 2013-07-09 14:49:13 -0700 | [diff] [blame] | 1750 | match.setNetworkProtocol(matchIpProto); |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1751 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1752 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1753 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1754 | // Match the IP ToS (DSCP field, 6 bits) |
| 1755 | Byte matchIpToS = flowEntryObj.getMatchIpToS(); |
| 1756 | if (matchIpToS == null) |
| 1757 | matchIpToS = flowObj.getMatchIpToS(); |
| 1758 | if (matchIpToS != null) { |
| 1759 | match.setNetworkTypeOfService(matchIpToS); |
| 1760 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 1761 | } |
| 1762 | |
| 1763 | // Match the Source TCP/UDP port |
| 1764 | Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort(); |
| 1765 | if (matchSrcTcpUdpPort == null) |
| 1766 | matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort(); |
| 1767 | if (matchSrcTcpUdpPort != null) { |
| 1768 | match.setTransportSource(matchSrcTcpUdpPort); |
| 1769 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 1770 | } |
| 1771 | |
| 1772 | // Match the Destination TCP/UDP port |
| 1773 | Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort(); |
| 1774 | if (matchDstTcpUdpPort == null) |
| 1775 | matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort(); |
| 1776 | if (matchDstTcpUdpPort != null) { |
| 1777 | match.setTransportDestination(matchDstTcpUdpPort); |
| 1778 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | // |
| 1782 | // Fetch the actions |
| 1783 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1784 | Short actionOutputPort = null; |
| 1785 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 1786 | int actionsLen = 0; |
| 1787 | FlowEntryActions flowEntryActions = null; |
| 1788 | String actionsStr = flowEntryObj.getActions(); |
| 1789 | if (actionsStr != null) |
| 1790 | flowEntryActions = new FlowEntryActions(actionsStr); |
| 1791 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 1792 | ActionOutput actionOutput = action.actionOutput(); |
| 1793 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 1794 | ActionSetVlanPriority actionSetVlanPriority = action.actionSetVlanPriority(); |
| 1795 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 1796 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action.actionSetEthernetSrcAddr(); |
| 1797 | ActionSetEthernetAddr actionSetEthernetDstAddr = action.actionSetEthernetDstAddr(); |
| 1798 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action.actionSetIPv4SrcAddr(); |
| 1799 | ActionSetIPv4Addr actionSetIPv4DstAddr = action.actionSetIPv4DstAddr(); |
| 1800 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 1801 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action.actionSetTcpUdpSrcPort(); |
| 1802 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action.actionSetTcpUdpDstPort(); |
| 1803 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 1804 | |
| 1805 | if (actionOutput != null) { |
| 1806 | actionOutputPort = actionOutput.port().value(); |
| 1807 | // XXX: The max length is hard-coded for now |
| 1808 | OFActionOutput ofa = |
| 1809 | new OFActionOutput(actionOutput.port().value(), |
| 1810 | (short)0xffff); |
| 1811 | openFlowActions.add(ofa); |
| 1812 | actionsLen += ofa.getLength(); |
| 1813 | } |
| 1814 | |
| 1815 | if (actionSetVlanId != null) { |
| 1816 | OFActionVirtualLanIdentifier ofa = |
| 1817 | new OFActionVirtualLanIdentifier(actionSetVlanId.vlanId()); |
| 1818 | openFlowActions.add(ofa); |
| 1819 | actionsLen += ofa.getLength(); |
| 1820 | } |
| 1821 | |
| 1822 | if (actionSetVlanPriority != null) { |
| 1823 | OFActionVirtualLanPriorityCodePoint ofa = |
| 1824 | new OFActionVirtualLanPriorityCodePoint(actionSetVlanPriority.vlanPriority()); |
| 1825 | openFlowActions.add(ofa); |
| 1826 | actionsLen += ofa.getLength(); |
| 1827 | } |
| 1828 | |
| 1829 | if (actionStripVlan != null) { |
| 1830 | if (actionStripVlan.stripVlan() == true) { |
| 1831 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 1832 | openFlowActions.add(ofa); |
| 1833 | actionsLen += ofa.getLength(); |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | if (actionSetEthernetSrcAddr != null) { |
| 1838 | OFActionDataLayerSource ofa = |
| 1839 | new OFActionDataLayerSource(actionSetEthernetSrcAddr.addr().toBytes()); |
| 1840 | openFlowActions.add(ofa); |
| 1841 | actionsLen += ofa.getLength(); |
| 1842 | } |
| 1843 | |
| 1844 | if (actionSetEthernetDstAddr != null) { |
| 1845 | OFActionDataLayerDestination ofa = |
| 1846 | new OFActionDataLayerDestination(actionSetEthernetDstAddr.addr().toBytes()); |
| 1847 | openFlowActions.add(ofa); |
| 1848 | actionsLen += ofa.getLength(); |
| 1849 | } |
| 1850 | |
| 1851 | if (actionSetIPv4SrcAddr != null) { |
| 1852 | OFActionNetworkLayerSource ofa = |
| 1853 | new OFActionNetworkLayerSource(actionSetIPv4SrcAddr.addr().value()); |
| 1854 | openFlowActions.add(ofa); |
| 1855 | actionsLen += ofa.getLength(); |
| 1856 | } |
| 1857 | |
| 1858 | if (actionSetIPv4DstAddr != null) { |
| 1859 | OFActionNetworkLayerDestination ofa = |
| 1860 | new OFActionNetworkLayerDestination(actionSetIPv4DstAddr.addr().value()); |
| 1861 | openFlowActions.add(ofa); |
| 1862 | actionsLen += ofa.getLength(); |
| 1863 | } |
| 1864 | |
| 1865 | if (actionSetIpToS != null) { |
| 1866 | OFActionNetworkTypeOfService ofa = |
| 1867 | new OFActionNetworkTypeOfService(actionSetIpToS.ipToS()); |
| 1868 | openFlowActions.add(ofa); |
| 1869 | actionsLen += ofa.getLength(); |
| 1870 | } |
| 1871 | |
| 1872 | if (actionSetTcpUdpSrcPort != null) { |
| 1873 | OFActionTransportLayerSource ofa = |
| 1874 | new OFActionTransportLayerSource(actionSetTcpUdpSrcPort.port()); |
| 1875 | openFlowActions.add(ofa); |
| 1876 | actionsLen += ofa.getLength(); |
| 1877 | } |
| 1878 | |
| 1879 | if (actionSetTcpUdpDstPort != null) { |
| 1880 | OFActionTransportLayerDestination ofa = |
| 1881 | new OFActionTransportLayerDestination(actionSetTcpUdpDstPort.port()); |
| 1882 | openFlowActions.add(ofa); |
| 1883 | actionsLen += ofa.getLength(); |
| 1884 | } |
| 1885 | |
| 1886 | if (actionEnqueue != null) { |
| 1887 | OFActionEnqueue ofa = |
| 1888 | new OFActionEnqueue(actionEnqueue.port().value(), |
| 1889 | actionEnqueue.queueId()); |
| 1890 | openFlowActions.add(ofa); |
| 1891 | actionsLen += ofa.getLength(); |
| 1892 | } |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1896 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1897 | .setPriority(PRIORITY_DEFAULT) |
| 1898 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1899 | .setCookie(cookie) |
| 1900 | .setCommand(flowModCommand) |
| 1901 | .setMatch(match) |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 1902 | .setActions(openFlowActions) |
| 1903 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1904 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1905 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1906 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1907 | if (actionOutputPort != null) |
| 1908 | fm.setOutPort(actionOutputPort); |
| 1909 | } |
| 1910 | |
| 1911 | // |
| 1912 | // TODO: Set the following flag |
| 1913 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1914 | // See method ForwardingBase::pushRoute() |
| 1915 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1916 | |
| 1917 | // |
| 1918 | // Write the message to the switch |
| 1919 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1920 | log.debug("MEASUREMENT: Installing flow entry " + userState + |
| 1921 | " into switch DPID: " + |
| 1922 | mySwitch.getStringId() + |
| 1923 | " flowEntryId: " + flowEntryId.toString() + |
| 1924 | " srcMac: " + matchSrcMac + " dstMac: " + matchDstMac + |
| 1925 | " inPort: " + matchInPort + " outPort: " + actionOutputPort |
| 1926 | ); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1927 | try { |
| 1928 | messageDamper.write(mySwitch, fm, null); |
| 1929 | mySwitch.flush(); |
| 1930 | // |
| 1931 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1932 | // to check for errors, and update the SwitchState |
| 1933 | // for a flow entry after the Barrier message is |
| 1934 | // is received. |
| 1935 | // |
| 1936 | flowEntryObj.setSwitchState("FE_SWITCH_UPDATED"); |
| 1937 | } catch (IOException e) { |
| 1938 | log.error("Failure writing flow mod from network map", e); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1939 | return false; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | return true; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Install a Flow Entry on a switch. |
| 1947 | * |
| 1948 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1949 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1950 | * @param flowEntry the flow entry to install. |
| 1951 | * @return true on success, otherwise false. |
| 1952 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1953 | public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1954 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1955 | // |
| 1956 | // Create the OpenFlow Flow Modification Entry to push |
| 1957 | // |
| 1958 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1959 | .getMessage(OFType.FLOW_MOD); |
| 1960 | long cookie = flowEntry.flowEntryId().value(); |
| 1961 | |
| 1962 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1963 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 1964 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1965 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 1966 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1967 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 1968 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1969 | } else { |
| 1970 | // Unknown user state. Ignore the entry |
| 1971 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1972 | flowEntry.flowEntryId().toString(), |
| 1973 | flowEntry.flowEntryUserState()); |
| 1974 | return false; |
| 1975 | } |
| 1976 | |
| 1977 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1978 | // Fetch the match conditions. |
| 1979 | // |
| 1980 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1981 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1982 | // condition set. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1983 | // |
| 1984 | OFMatch match = new OFMatch(); |
| 1985 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1986 | FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch(); |
| 1987 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 1988 | |
| 1989 | // Match the Incoming Port |
| 1990 | Port matchInPort = flowEntryMatch.inPort(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1991 | if (matchInPort != null) { |
| 1992 | match.setInputPort(matchInPort.value()); |
| 1993 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1994 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1995 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 1996 | // Match the Source MAC address |
| 1997 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
| 1998 | if ((matchSrcMac == null) && (flowPathMatch != null)) { |
| 1999 | matchSrcMac = flowPathMatch.srcMac(); |
| 2000 | } |
| 2001 | if (matchSrcMac != null) { |
| 2002 | match.setDataLayerSource(matchSrcMac.toString()); |
| 2003 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 2004 | } |
| 2005 | |
| 2006 | // Match the Destination MAC address |
| 2007 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
| 2008 | if ((matchDstMac == null) && (flowPathMatch != null)) { |
| 2009 | matchDstMac = flowPathMatch.dstMac(); |
| 2010 | } |
| 2011 | if (matchDstMac != null) { |
| 2012 | match.setDataLayerDestination(matchDstMac.toString()); |
| 2013 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 2014 | } |
| 2015 | |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2016 | // Match the Ethernet Frame Type |
| 2017 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
| 2018 | if ((matchEthernetFrameType == null) && (flowPathMatch != null)) { |
| 2019 | matchEthernetFrameType = flowPathMatch.ethernetFrameType(); |
| 2020 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2021 | if (matchEthernetFrameType != null) { |
| 2022 | match.setDataLayerType(matchEthernetFrameType); |
| 2023 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 2024 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2025 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 2026 | // Match the VLAN ID |
| 2027 | Short matchVlanId = flowEntryMatch.vlanId(); |
| 2028 | if ((matchVlanId == null) && (flowPathMatch != null)) { |
| 2029 | matchVlanId = flowPathMatch.vlanId(); |
| 2030 | } |
| 2031 | if (matchVlanId != null) { |
| 2032 | match.setDataLayerVirtualLan(matchVlanId); |
| 2033 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 2034 | } |
| 2035 | |
| 2036 | // Match the VLAN priority |
| 2037 | Byte matchVlanPriority = flowEntryMatch.vlanPriority(); |
| 2038 | if ((matchVlanPriority == null) && (flowPathMatch != null)) { |
| 2039 | matchVlanPriority = flowPathMatch.vlanPriority(); |
| 2040 | } |
| 2041 | if (matchVlanPriority != null) { |
| 2042 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 2043 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 2044 | } |
| 2045 | |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2046 | // Match the Source IPv4 Network prefix |
| 2047 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
| 2048 | if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) { |
| 2049 | matchSrcIPv4Net = flowPathMatch.srcIPv4Net(); |
| 2050 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2051 | if (matchSrcIPv4Net != null) { |
| 2052 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 2053 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2054 | |
| 2055 | // Natch the Destination IPv4 Network prefix |
| 2056 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
| 2057 | if ((matchDstIPv4Net == null) && (flowPathMatch != null)) { |
| 2058 | matchDstIPv4Net = flowPathMatch.dstIPv4Net(); |
| 2059 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2060 | if (matchDstIPv4Net != null) { |
| 2061 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 2062 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2063 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 2064 | // Match the IP protocol |
| 2065 | Byte matchIpProto = flowEntryMatch.ipProto(); |
| 2066 | if ((matchIpProto == null) && (flowPathMatch != null)) { |
| 2067 | matchIpProto = flowPathMatch.ipProto(); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2068 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 2069 | if (matchIpProto != null) { |
| 2070 | match.setNetworkProtocol(matchIpProto); |
| 2071 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2072 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2073 | |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 2074 | // Match the IP ToS (DSCP field, 6 bits) |
| 2075 | Byte matchIpToS = flowEntryMatch.ipToS(); |
| 2076 | if ((matchIpToS == null) && (flowPathMatch != null)) { |
| 2077 | matchIpToS = flowPathMatch.ipToS(); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2078 | } |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 2079 | if (matchIpToS != null) { |
| 2080 | match.setNetworkTypeOfService(matchIpToS); |
| 2081 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 2082 | } |
| 2083 | |
| 2084 | // Match the Source TCP/UDP port |
| 2085 | Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort(); |
| 2086 | if ((matchSrcTcpUdpPort == null) && (flowPathMatch != null)) { |
| 2087 | matchSrcTcpUdpPort = flowPathMatch.srcTcpUdpPort(); |
| 2088 | } |
| 2089 | if (matchSrcTcpUdpPort != null) { |
| 2090 | match.setTransportSource(matchSrcTcpUdpPort); |
| 2091 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 2092 | } |
| 2093 | |
| 2094 | // Match the Destination TCP/UDP port |
| 2095 | Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort(); |
| 2096 | if ((matchDstTcpUdpPort == null) && (flowPathMatch != null)) { |
| 2097 | matchDstTcpUdpPort = flowPathMatch.dstTcpUdpPort(); |
| 2098 | } |
| 2099 | if (matchDstTcpUdpPort != null) { |
| 2100 | match.setTransportDestination(matchDstTcpUdpPort); |
| 2101 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | // |
| 2105 | // Fetch the actions |
| 2106 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2107 | Short actionOutputPort = null; |
| 2108 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 2109 | int actionsLen = 0; |
| 2110 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2111 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2112 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 2113 | ActionOutput actionOutput = action.actionOutput(); |
| 2114 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 2115 | ActionSetVlanPriority actionSetVlanPriority = action.actionSetVlanPriority(); |
| 2116 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 2117 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action.actionSetEthernetSrcAddr(); |
| 2118 | ActionSetEthernetAddr actionSetEthernetDstAddr = action.actionSetEthernetDstAddr(); |
| 2119 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action.actionSetIPv4SrcAddr(); |
| 2120 | ActionSetIPv4Addr actionSetIPv4DstAddr = action.actionSetIPv4DstAddr(); |
| 2121 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 2122 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action.actionSetTcpUdpSrcPort(); |
| 2123 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action.actionSetTcpUdpDstPort(); |
| 2124 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 2125 | |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2126 | if (actionOutput != null) { |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2127 | actionOutputPort = actionOutput.port().value(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2128 | // XXX: The max length is hard-coded for now |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2129 | OFActionOutput ofa = |
| 2130 | new OFActionOutput(actionOutput.port().value(), |
| 2131 | (short)0xffff); |
| 2132 | openFlowActions.add(ofa); |
| 2133 | actionsLen += ofa.getLength(); |
| 2134 | } |
| 2135 | |
| 2136 | if (actionSetVlanId != null) { |
| 2137 | OFActionVirtualLanIdentifier ofa = |
| 2138 | new OFActionVirtualLanIdentifier(actionSetVlanId.vlanId()); |
| 2139 | openFlowActions.add(ofa); |
| 2140 | actionsLen += ofa.getLength(); |
| 2141 | } |
| 2142 | |
| 2143 | if (actionSetVlanPriority != null) { |
| 2144 | OFActionVirtualLanPriorityCodePoint ofa = |
| 2145 | new OFActionVirtualLanPriorityCodePoint(actionSetVlanPriority.vlanPriority()); |
| 2146 | openFlowActions.add(ofa); |
| 2147 | actionsLen += ofa.getLength(); |
| 2148 | } |
| 2149 | |
| 2150 | if (actionStripVlan != null) { |
| 2151 | if (actionStripVlan.stripVlan() == true) { |
| 2152 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 2153 | openFlowActions.add(ofa); |
| 2154 | actionsLen += ofa.getLength(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2155 | } |
| 2156 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2157 | |
| 2158 | if (actionSetEthernetSrcAddr != null) { |
| 2159 | OFActionDataLayerSource ofa = |
| 2160 | new OFActionDataLayerSource(actionSetEthernetSrcAddr.addr().toBytes()); |
| 2161 | openFlowActions.add(ofa); |
| 2162 | actionsLen += ofa.getLength(); |
| 2163 | } |
| 2164 | |
| 2165 | if (actionSetEthernetDstAddr != null) { |
| 2166 | OFActionDataLayerDestination ofa = |
| 2167 | new OFActionDataLayerDestination(actionSetEthernetDstAddr.addr().toBytes()); |
| 2168 | openFlowActions.add(ofa); |
| 2169 | actionsLen += ofa.getLength(); |
| 2170 | } |
| 2171 | |
| 2172 | if (actionSetIPv4SrcAddr != null) { |
| 2173 | OFActionNetworkLayerSource ofa = |
| 2174 | new OFActionNetworkLayerSource(actionSetIPv4SrcAddr.addr().value()); |
| 2175 | openFlowActions.add(ofa); |
| 2176 | actionsLen += ofa.getLength(); |
| 2177 | } |
| 2178 | |
| 2179 | if (actionSetIPv4DstAddr != null) { |
| 2180 | OFActionNetworkLayerDestination ofa = |
| 2181 | new OFActionNetworkLayerDestination(actionSetIPv4DstAddr.addr().value()); |
| 2182 | openFlowActions.add(ofa); |
| 2183 | actionsLen += ofa.getLength(); |
| 2184 | } |
| 2185 | |
| 2186 | if (actionSetIpToS != null) { |
| 2187 | OFActionNetworkTypeOfService ofa = |
| 2188 | new OFActionNetworkTypeOfService(actionSetIpToS.ipToS()); |
| 2189 | openFlowActions.add(ofa); |
| 2190 | actionsLen += ofa.getLength(); |
| 2191 | } |
| 2192 | |
| 2193 | if (actionSetTcpUdpSrcPort != null) { |
| 2194 | OFActionTransportLayerSource ofa = |
| 2195 | new OFActionTransportLayerSource(actionSetTcpUdpSrcPort.port()); |
| 2196 | openFlowActions.add(ofa); |
| 2197 | actionsLen += ofa.getLength(); |
| 2198 | } |
| 2199 | |
| 2200 | if (actionSetTcpUdpDstPort != null) { |
| 2201 | OFActionTransportLayerDestination ofa = |
| 2202 | new OFActionTransportLayerDestination(actionSetTcpUdpDstPort.port()); |
| 2203 | openFlowActions.add(ofa); |
| 2204 | actionsLen += ofa.getLength(); |
| 2205 | } |
| 2206 | |
| 2207 | if (actionEnqueue != null) { |
| 2208 | OFActionEnqueue ofa = |
| 2209 | new OFActionEnqueue(actionEnqueue.port().value(), |
| 2210 | actionEnqueue.queueId()); |
| 2211 | openFlowActions.add(ofa); |
| 2212 | actionsLen += ofa.getLength(); |
| 2213 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 2217 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 2218 | .setPriority(PRIORITY_DEFAULT) |
| 2219 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 2220 | .setCookie(cookie) |
| 2221 | .setCommand(flowModCommand) |
| 2222 | .setMatch(match) |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2223 | .setActions(openFlowActions) |
| 2224 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
| 2225 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 2226 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 2227 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 2228 | if (actionOutputPort != null) |
| 2229 | fm.setOutPort(actionOutputPort); |
| 2230 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2231 | |
| 2232 | // |
| 2233 | // TODO: Set the following flag |
| 2234 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 2235 | // See method ForwardingBase::pushRoute() |
| 2236 | // |
| 2237 | |
| 2238 | // |
| 2239 | // Write the message to the switch |
| 2240 | // |
| 2241 | try { |
| 2242 | messageDamper.write(mySwitch, fm, null); |
| 2243 | mySwitch.flush(); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 2244 | // |
| 2245 | // TODO: We should use the OpenFlow Barrier mechanism |
| 2246 | // to check for errors, and update the SwitchState |
| 2247 | // for a flow entry after the Barrier message is |
| 2248 | // is received. |
| 2249 | // |
| 2250 | // TODO: The FlowEntry Object in Titan should be set |
| 2251 | // to FE_SWITCH_UPDATED. |
| 2252 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2253 | } catch (IOException e) { |
| 2254 | log.error("Failure writing flow mod from network map", e); |
| 2255 | return false; |
| 2256 | } |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
| 2260 | /** |
| 2261 | * Remove a Flow Entry from a switch. |
| 2262 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 2263 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2264 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 2265 | * @param flowEntry the flow entry to remove. |
| 2266 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2267 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2268 | public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 2269 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 2270 | // |
| 2271 | // The installFlowEntry() method implements both installation |
| 2272 | // and removal of flow entries. |
| 2273 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2274 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /** |
| 2278 | * Install a Flow Entry on a remote controller. |
| 2279 | * |
| 2280 | * TODO: We need it now: Jono |
| 2281 | * - For now it will make a REST call to the remote controller. |
| 2282 | * - Internally, it needs to know the name of the remote controller. |
| 2283 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2284 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 2285 | * @param flowEntry the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2286 | * @return true on success, otherwise false. |
| 2287 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2288 | public boolean installRemoteFlowEntry(FlowPath flowPath, |
| 2289 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2290 | // TODO: We need it now: Jono |
| 2291 | // - For now it will make a REST call to the remote controller. |
| 2292 | // - Internally, it needs to know the name of the remote controller. |
| 2293 | return true; |
| 2294 | } |
| 2295 | |
| 2296 | /** |
| 2297 | * Remove a flow entry on a remote controller. |
| 2298 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2299 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 2300 | * @param flowEntry the flow entry to remove. |
| 2301 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2302 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2303 | public boolean removeRemoteFlowEntry(FlowPath flowPath, |
| 2304 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 2305 | // |
| 2306 | // The installRemoteFlowEntry() method implements both installation |
| 2307 | // and removal of flow entries. |
| 2308 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 2309 | return (installRemoteFlowEntry(flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2310 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2311 | } |