Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Multimap; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 28 | import org.onosproject.net.driver.DefaultDriverProviderService; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 29 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 31 | import org.onosproject.openflow.controller.Dpid; |
| 32 | import org.onosproject.openflow.controller.OpenFlowController; |
| 33 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 34 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 35 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 36 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 37 | import org.onosproject.openflow.controller.PacketListener; |
| 38 | import org.onosproject.openflow.controller.RoleState; |
| 39 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 40 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry; |
| 42 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 44 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 46 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 47 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 49 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 50 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 51 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 53 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 56 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 61 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | import org.slf4j.LoggerFactory; |
| 64 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 65 | import java.util.Collection; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 66 | import java.util.Collections; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 67 | import java.util.Dictionary; |
| 68 | import java.util.HashMap; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 69 | import java.util.LinkedList; |
| 70 | import java.util.List; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 71 | import java.util.Map; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 72 | import java.util.Set; |
| 73 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 74 | import java.util.concurrent.CopyOnWriteArraySet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 75 | import java.util.concurrent.ExecutorService; |
| 76 | import java.util.concurrent.Executors; |
| 77 | import java.util.concurrent.locks.Lock; |
| 78 | import java.util.concurrent.locks.ReentrantLock; |
| 79 | |
| 80 | import static org.onlab.util.Tools.groupedThreads; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 81 | |
| 82 | @Component(immediate = true) |
| 83 | @Service |
| 84 | public class OpenFlowControllerImpl implements OpenFlowController { |
| 85 | |
| 86 | private static final Logger log = |
| 87 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 88 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected DriverService driverService; |
| 91 | |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 92 | // References exists merely for sequencing purpose to assure drivers are loaded |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected DefaultDriverProviderService defaultDriverProviderService; |
| 95 | |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 96 | private final ExecutorService executorMsgs = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 97 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d")); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 98 | |
| 99 | private final ExecutorService executorBarrier = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 100 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d")); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 101 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 102 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 103 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 104 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 105 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 106 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 107 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 108 | |
| 109 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 110 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 111 | |
| 112 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 113 | ArrayListMultimap.create(); |
| 114 | |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 115 | protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 116 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 117 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 118 | ArrayListMultimap.create(); |
| 119 | |
| 120 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 121 | ArrayListMultimap.create(); |
| 122 | |
| 123 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 124 | ArrayListMultimap.create(); |
| 125 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 126 | protected Multimap<Dpid, OFPortStatsEntry> fullPortStats = |
| 127 | ArrayListMultimap.create(); |
| 128 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 129 | private final Controller ctrl = new Controller(); |
| 130 | |
| 131 | @Activate |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 132 | public void activate(ComponentContext context) { |
| 133 | Map<String, String> properties = readComponentConfiguration(context); |
| 134 | ctrl.setConfigParams(properties); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 135 | ctrl.start(agent, driverService); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Deactivate |
| 139 | public void deactivate() { |
| 140 | ctrl.stop(); |
| 141 | } |
| 142 | |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Extracts properties from the component configuration context. |
| 145 | * |
| 146 | * @param context the component context |
| 147 | */ |
| 148 | private Map<String, String> readComponentConfiguration(ComponentContext context) { |
| 149 | Dictionary<?, ?> properties = context.getProperties(); |
| 150 | Map<String, String> outProperties = new HashMap<>(); |
| 151 | try { |
| 152 | String strDpid = (String) properties.get("corsaDpid"); |
| 153 | if (strDpid != null) { |
| 154 | outProperties.put("corsaDpid", strDpid); |
| 155 | } |
| 156 | } catch (ClassCastException e) { |
| 157 | return outProperties; |
| 158 | } |
| 159 | return outProperties; |
| 160 | } |
| 161 | |
| 162 | @Modified |
| 163 | public void modified(ComponentContext context) { |
| 164 | // Blank @Modified method to catch modifications to the context. |
| 165 | // If no @Modified method exists, @Activate is called again |
| 166 | // when the context is modified. |
| 167 | } |
| 168 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 171 | return connectedSwitches.values(); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 176 | return activeMasterSwitches.values(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 181 | return activeEqualSwitches.values(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 186 | return connectedSwitches.get(dpid); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 191 | return activeMasterSwitches.get(dpid); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 196 | return activeEqualSwitches.get(dpid); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 201 | if (!ofSwitchListener.contains(listener)) { |
| 202 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 208 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void addPacketListener(int priority, PacketListener listener) { |
| 213 | ofPacketListener.put(priority, listener); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void removePacketListener(PacketListener listener) { |
| 218 | ofPacketListener.values().remove(listener); |
| 219 | } |
| 220 | |
| 221 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 222 | public void addEventListener(OpenFlowEventListener listener) { |
| 223 | ofEventListener.add(listener); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void removeEventListener(OpenFlowEventListener listener) { |
| 228 | ofEventListener.remove(listener); |
| 229 | } |
| 230 | |
| 231 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 232 | public void write(Dpid dpid, OFMessage msg) { |
| 233 | this.getSwitch(dpid).sendMsg(msg); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 238 | Collection<OFFlowStatsEntry> flowStats; |
| 239 | Collection<OFGroupStatsEntry> groupStats; |
| 240 | Collection<OFGroupDescStatsEntry> groupDescStats; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 241 | Collection<OFPortStatsEntry> portStats; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 242 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 243 | switch (msg.getType()) { |
| 244 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 245 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 246 | l.portChanged(dpid, (OFPortStatus) msg); |
| 247 | } |
| 248 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 249 | case FEATURES_REPLY: |
| 250 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 251 | l.switchChanged(dpid); |
| 252 | } |
| 253 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 254 | case PACKET_IN: |
| 255 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
| 256 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 257 | (OFPacketIn) msg); |
| 258 | for (PacketListener p : ofPacketListener.values()) { |
| 259 | p.handlePacket(pktCtx); |
| 260 | } |
| 261 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 262 | // TODO: Consider using separate threadpool for sensitive messages. |
| 263 | // ie. Back to back error could cause us to starve. |
| 264 | case FLOW_REMOVED: |
| 265 | case ERROR: |
| 266 | executorMsgs.submit(new OFMessageHandler(dpid, msg)); |
| 267 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 268 | case STATS_REPLY: |
| 269 | OFStatsReply reply = (OFStatsReply) msg; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 270 | switch (reply.getStatsType()) { |
| 271 | case PORT_DESC: |
| 272 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 273 | l.switchChanged(dpid); |
| 274 | } |
| 275 | break; |
| 276 | case FLOW: |
| 277 | flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 278 | if (flowStats != null) { |
| 279 | OFFlowStatsReply.Builder rep = |
| 280 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 281 | rep.setEntries(Lists.newLinkedList(flowStats)); |
| 282 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 283 | } |
| 284 | break; |
| 285 | case GROUP: |
| 286 | groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 287 | if (groupStats != null) { |
| 288 | OFGroupStatsReply.Builder rep = |
| 289 | OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply(); |
| 290 | rep.setEntries(Lists.newLinkedList(groupStats)); |
| 291 | rep.setXid(reply.getXid()); |
| 292 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 293 | } |
| 294 | break; |
| 295 | case GROUP_DESC: |
| 296 | groupDescStats = publishGroupDescStats(dpid, |
| 297 | (OFGroupDescStatsReply) reply); |
| 298 | if (groupDescStats != null) { |
| 299 | OFGroupDescStatsReply.Builder rep = |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 300 | OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply(); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 301 | rep.setEntries(Lists.newLinkedList(groupDescStats)); |
| 302 | rep.setXid(reply.getXid()); |
| 303 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 304 | } |
| 305 | break; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 306 | case PORT: |
| 307 | executorMsgs.submit(new OFMessageHandler(dpid, reply)); |
| 308 | break; |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame^] | 309 | case METER: |
| 310 | executorMsgs.submit(new OFMessageHandler(dpid, reply)); |
| 311 | break; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 312 | case EXPERIMENTER: |
| 313 | if (reply instanceof OFCalientFlowStatsReply) { |
| 314 | // Convert Calient flow statistics to regular flow stats |
| 315 | // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API |
| 316 | OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply(); |
| 317 | List<OFFlowStatsEntry> entries = new LinkedList<>(); |
| 318 | for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) { |
| 319 | |
| 320 | // Single instruction, i.e., output to port |
| 321 | OFActionOutput action = OFFactories |
| 322 | .getFactory(msg.getVersion()) |
| 323 | .actions() |
| 324 | .buildOutput() |
| 325 | .setPort(entry.getOutPort()) |
| 326 | .build(); |
| 327 | OFInstruction instruction = OFFactories |
| 328 | .getFactory(msg.getVersion()) |
| 329 | .instructions() |
| 330 | .applyActions(Collections.singletonList(action)); |
| 331 | OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry() |
| 332 | .setMatch(entry.getMatch()) |
| 333 | .setTableId(entry.getTableId()) |
| 334 | .setDurationSec(entry.getDurationSec()) |
| 335 | .setDurationNsec(entry.getDurationNsec()) |
| 336 | .setPriority(entry.getPriority()) |
| 337 | .setIdleTimeout(entry.getIdleTimeout()) |
| 338 | .setHardTimeout(entry.getHardTimeout()) |
| 339 | .setFlags(entry.getFlags()) |
| 340 | .setCookie(entry.getCookie()) |
| 341 | .setInstructions(Collections.singletonList(instruction)) |
| 342 | .build(); |
| 343 | entries.add(fs); |
| 344 | } |
| 345 | fsr.setEntries(entries); |
| 346 | |
| 347 | flowStats = publishFlowStats(dpid, fsr.build()); |
| 348 | if (flowStats != null) { |
| 349 | OFFlowStatsReply.Builder rep = |
| 350 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 351 | rep.setEntries(Lists.newLinkedList(flowStats)); |
| 352 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 353 | } |
| 354 | } else { |
| 355 | log.warn("Unsupported stats type : {}", reply.getStatsType()); |
| 356 | } |
| 357 | break; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 358 | default: |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame^] | 359 | log.warn("Discarding unknown stats reply type {}", reply.getStatsType()); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 360 | break; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 361 | } |
| 362 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 363 | case BARRIER_REPLY: |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 364 | executorBarrier.submit(new OFMessageHandler(dpid, msg)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 365 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 366 | case EXPERIMENTER: |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 367 | long experimenter = ((OFExperimenter) msg).getExperimenter(); |
| 368 | if (experimenter == 0x748771) { |
| 369 | // LINC-OE port stats |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 370 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
| 371 | OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus(); |
| 372 | OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc(); |
| 373 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 374 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 375 | .setName(circuitPortStatus.getName()) |
| 376 | .setConfig(circuitPortStatus.getConfig()) |
| 377 | .setState(circuitPortStatus.getState()); |
| 378 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 379 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 380 | l.portChanged(dpid, portStatus.build()); |
| 381 | } |
| 382 | } else { |
| 383 | log.warn("Handling experimenter type {} not yet implemented", |
| 384 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 385 | } |
| 386 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 387 | default: |
| 388 | log.warn("Handling message type {} not yet implemented {}", |
| 389 | msg.getType(), msg); |
| 390 | } |
| 391 | } |
| 392 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 393 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 394 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 395 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 396 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 397 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 398 | return fullFlowStats.removeAll(dpid); |
| 399 | } |
| 400 | return null; |
| 401 | } |
| 402 | |
| 403 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 404 | OFGroupStatsReply reply) { |
| 405 | //TODO: Get rid of synchronized |
| 406 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 407 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 408 | return fullGroupStats.removeAll(dpid); |
| 409 | } |
| 410 | return null; |
| 411 | } |
| 412 | |
| 413 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 414 | OFGroupDescStatsReply reply) { |
| 415 | //TODO: Get rid of synchronized |
| 416 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 417 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 418 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 419 | } |
| 420 | return null; |
| 421 | } |
| 422 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 423 | private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid, |
| 424 | OFPortStatsReply reply) { |
| 425 | fullPortStats.putAll(dpid, reply.getEntries()); |
| 426 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 427 | return fullPortStats.removeAll(dpid); |
| 428 | } |
| 429 | return null; |
| 430 | } |
| 431 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 432 | @Override |
| 433 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 434 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 435 | if (sw == null) { |
| 436 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 437 | return; |
| 438 | } |
| 439 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Implementation of an OpenFlow Agent which is responsible for |
| 444 | * keeping track of connected switches and the state in which |
| 445 | * they are. |
| 446 | */ |
| 447 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 448 | |
| 449 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 450 | private final Lock switchLock = new ReentrantLock(); |
| 451 | |
| 452 | @Override |
| 453 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 454 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 455 | if (connectedSwitches.get(dpid) != null) { |
| 456 | log.error("Trying to add connectedSwitch but found a previous " |
| 457 | + "value for dpid: {}", dpid); |
| 458 | return false; |
| 459 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 460 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 461 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 462 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 463 | l.switchAdded(dpid); |
| 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public boolean validActivation(Dpid dpid) { |
| 471 | if (connectedSwitches.get(dpid) == null) { |
| 472 | log.error("Trying to activate switch but is not in " |
| 473 | + "connected switches: dpid {}. Aborting ..", |
| 474 | dpid); |
| 475 | return false; |
| 476 | } |
| 477 | if (activeMasterSwitches.get(dpid) != null || |
| 478 | activeEqualSwitches.get(dpid) != null) { |
| 479 | log.error("Trying to activate switch but it is already " |
| 480 | + "activated: dpid {}. Found in activeMaster: {} " |
| 481 | + "Found in activeEqual: {}. Aborting ..", new Object[]{ |
| 482 | dpid, |
| 483 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 484 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| 485 | return false; |
| 486 | } |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | @Override |
| 492 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 493 | switchLock.lock(); |
| 494 | try { |
| 495 | if (!validActivation(dpid)) { |
| 496 | return false; |
| 497 | } |
| 498 | activeMasterSwitches.put(dpid, sw); |
| 499 | return true; |
| 500 | } finally { |
| 501 | switchLock.unlock(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 507 | switchLock.lock(); |
| 508 | try { |
| 509 | if (!validActivation(dpid)) { |
| 510 | return false; |
| 511 | } |
| 512 | activeEqualSwitches.put(dpid, sw); |
| 513 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 514 | return true; |
| 515 | } finally { |
| 516 | switchLock.unlock(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void transitionToMasterSwitch(Dpid dpid) { |
| 522 | switchLock.lock(); |
| 523 | try { |
| 524 | if (activeMasterSwitches.containsKey(dpid)) { |
| 525 | return; |
| 526 | } |
| 527 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 528 | if (sw == null) { |
| 529 | sw = getSwitch(dpid); |
| 530 | if (sw == null) { |
| 531 | log.error("Transition to master called on sw {}, but switch " |
| 532 | + "was not found in controller-cache", dpid); |
| 533 | return; |
| 534 | } |
| 535 | } |
| 536 | log.info("Transitioned switch {} to MASTER", dpid); |
| 537 | activeMasterSwitches.put(dpid, sw); |
| 538 | } finally { |
| 539 | switchLock.unlock(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | @Override |
| 545 | public void transitionToEqualSwitch(Dpid dpid) { |
| 546 | switchLock.lock(); |
| 547 | try { |
| 548 | if (activeEqualSwitches.containsKey(dpid)) { |
| 549 | return; |
| 550 | } |
| 551 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 552 | if (sw == null) { |
| 553 | sw = getSwitch(dpid); |
| 554 | if (sw == null) { |
| 555 | log.error("Transition to equal called on sw {}, but switch " |
| 556 | + "was not found in controller-cache", dpid); |
| 557 | return; |
| 558 | } |
| 559 | } |
| 560 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 561 | activeEqualSwitches.put(dpid, sw); |
| 562 | } finally { |
| 563 | switchLock.unlock(); |
| 564 | } |
| 565 | |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public void removeConnectedSwitch(Dpid dpid) { |
| 570 | connectedSwitches.remove(dpid); |
| 571 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 572 | if (sw == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 573 | log.debug("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 574 | sw = activeEqualSwitches.remove(dpid); |
| 575 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 576 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 577 | l.switchRemoved(dpid); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public void processMessage(Dpid dpid, OFMessage m) { |
| 583 | processPacket(dpid, m); |
| 584 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 585 | |
| 586 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 587 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 588 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 589 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 592 | } |
| 593 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 594 | private final class OFMessageHandler implements Runnable { |
| 595 | |
| 596 | private final OFMessage msg; |
| 597 | private final Dpid dpid; |
| 598 | |
| 599 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 600 | this.msg = msg; |
| 601 | this.dpid = dpid; |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 606 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 607 | listener.handleMessage(dpid, msg); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | } |
| 612 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 613 | } |