Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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.provider.of.flow.impl; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 18 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalCause; |
| 22 | import com.google.common.cache.RemovalNotification; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.CompletedBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowEntry; |
| 34 | import org.onosproject.net.flow.FlowRule; |
| 35 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 36 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowRuleProvider; |
| 38 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 39 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 40 | import org.onosproject.net.provider.AbstractProvider; |
| 41 | import org.onosproject.net.provider.ProviderId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.openflow.controller.Dpid; |
| 43 | import org.onosproject.openflow.controller.OpenFlowController; |
| 44 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 45 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 46 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 47 | import org.onosproject.openflow.controller.RoleState; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFActionType; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 50 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 51 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 53 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 55 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFInstructionType; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 58 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 62 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 63 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 66 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions; |
| 67 | import org.projectfloodlight.openflow.types.OFPort; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 70 | import java.util.Collections; |
| 71 | import java.util.List; |
| 72 | import java.util.Map; |
| 73 | import java.util.Optional; |
| 74 | import java.util.Set; |
| 75 | import java.util.concurrent.TimeUnit; |
| 76 | import java.util.stream.Collectors; |
| 77 | |
| 78 | import static org.slf4j.LoggerFactory.getLogger; |
| 79 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 80 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Provider which uses an OpenFlow controller to detect network |
| 83 | * end-station hosts. |
| 84 | */ |
| 85 | @Component(immediate = true) |
| 86 | public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider { |
| 87 | |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 88 | private static final int LOWEST_PRIORITY = 0; |
| 89 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 90 | private final Logger log = getLogger(getClass()); |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected FlowRuleProviderRegistry providerRegistry; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected OpenFlowController controller; |
| 97 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 98 | |
| 99 | private FlowRuleProviderService providerService; |
| 100 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 101 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 102 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 103 | private Cache<Long, InternalCacheEntry> pendingBatches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 104 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 105 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
| 106 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 107 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Creates an OpenFlow host provider. |
| 110 | */ |
| 111 | public OpenFlowRuleProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 112 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Activate |
| 116 | public void activate() { |
| 117 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 118 | controller.addListener(listener); |
| 119 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 120 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 121 | pendingBatches = CacheBuilder.newBuilder() |
| 122 | .expireAfterWrite(10, TimeUnit.SECONDS) |
| 123 | .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> { |
| 124 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 125 | providerService.batchOperationCompleted(notification.getKey(), |
| 126 | notification.getValue().failedCompletion()); |
| 127 | } |
| 128 | }).build(); |
| 129 | |
| 130 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 131 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 132 | FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL); |
| 133 | fsc.start(); |
| 134 | collectors.put(new Dpid(sw.getId()), fsc); |
| 135 | } |
| 136 | |
| 137 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 138 | log.info("Started"); |
| 139 | } |
| 140 | |
| 141 | @Deactivate |
| 142 | public void deactivate() { |
| 143 | providerRegistry.unregister(this); |
| 144 | providerService = null; |
| 145 | |
| 146 | log.info("Stopped"); |
| 147 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 148 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public void applyFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 151 | for (FlowRule flowRule : flowRules) { |
| 152 | applyRule(flowRule); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 153 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 154 | } |
| 155 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 156 | private void applyRule(FlowRule flowRule) { |
| 157 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 158 | if (flowRule.type() == FlowRule.Type.DEFAULT) { |
| 159 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 160 | Optional.empty()).buildFlowAdd()); |
| 161 | } else { |
| 162 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 163 | Optional.empty()).buildFlowAdd(), getTableType(flowRule.type())); |
| 164 | } |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 165 | } |
| 166 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 167 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public void removeFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 170 | for (FlowRule flowRule : flowRules) { |
| 171 | removeRule(flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 172 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 173 | |
| 174 | } |
| 175 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 176 | private void removeRule(FlowRule flowRule) { |
| 177 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 178 | if (flowRule.type() == FlowRule.Type.DEFAULT) { |
| 179 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 180 | Optional.empty()).buildFlowDel()); |
| 181 | } else { |
| 182 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 183 | Optional.empty()).buildFlowDel(), getTableType(flowRule.type())); |
| 184 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 185 | } |
| 186 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 187 | @Override |
| 188 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 189 | // TODO: optimize using the ApplicationId |
| 190 | removeFlowRule(flowRules); |
| 191 | } |
| 192 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 193 | @Override |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 194 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 195 | public void executeBatch(FlowRuleBatchOperation batch) { |
| 196 | |
| 197 | pendingBatches.put(batch.id(), new InternalCacheEntry(batch)); |
| 198 | |
| 199 | |
| 200 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(batch.deviceId().uri())); |
| 201 | OFFlowMod mod; |
| 202 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 203 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 204 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 205 | FlowModBuilder builder = |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 206 | FlowModBuilder.builder(fbe.target(), sw.factory(), |
| 207 | Optional.of(batch.id())); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 208 | switch (fbe.operator()) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 209 | case ADD: |
| 210 | mod = builder.buildFlowAdd(); |
| 211 | break; |
| 212 | case REMOVE: |
| 213 | mod = builder.buildFlowDel(); |
| 214 | break; |
| 215 | case MODIFY: |
| 216 | mod = builder.buildFlowMod(); |
| 217 | break; |
| 218 | default: |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 219 | log.error("Unsupported batch operation {}; skipping flowmod {}", |
| 220 | fbe.operator(), fbe); |
| 221 | continue; |
| 222 | } |
| 223 | sw.sendMsg(mod); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 224 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 225 | OFBarrierRequest.Builder builder = sw.factory() |
| 226 | .buildBarrierRequest() |
| 227 | .setXid(batch.id()); |
| 228 | sw.sendMsg(builder.build()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 229 | } |
| 230 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 231 | private OpenFlowSwitch.TableType getTableType(FlowRule.Type type) { |
| 232 | switch (type) { |
| 233 | case IP: |
| 234 | return OpenFlowSwitch.TableType.IP; |
| 235 | case MPLS: |
| 236 | return OpenFlowSwitch.TableType.MPLS; |
| 237 | case ACL: |
| 238 | return OpenFlowSwitch.TableType.ACL; |
| 239 | default: |
| 240 | return OpenFlowSwitch.TableType.NONE; |
| 241 | } |
| 242 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 243 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 244 | |
| 245 | |
| 246 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 247 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 248 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 249 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public void switchAdded(Dpid dpid) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 252 | FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 253 | fsc.start(); |
| 254 | collectors.put(dpid, fsc); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void switchRemoved(Dpid dpid) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 259 | FlowStatsCollector collector = collectors.remove(dpid); |
| 260 | if (collector != null) { |
| 261 | collector.stop(); |
| 262 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 266 | public void switchChanged(Dpid dpid) { |
| 267 | } |
| 268 | |
| 269 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 270 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 271 | //TODO: Decide whether to evict flows internal store. |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 276 | switch (msg.getType()) { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 277 | case FLOW_REMOVED: |
| 278 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 279 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 280 | FlowEntry fr = new FlowEntryBuilder(dpid, removed).build(); |
| 281 | providerService.flowRemoved(fr); |
| 282 | break; |
| 283 | case STATS_REPLY: |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 284 | if (((OFStatsReply) msg).getStatsType() != OFStatsType.FLOW) { |
| 285 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg); |
| 286 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 287 | break; |
| 288 | case BARRIER_REPLY: |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 289 | try { |
| 290 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
| 291 | if (entry != null) { |
| 292 | providerService.batchOperationCompleted(msg.getXid(), entry.completed()); |
| 293 | } else { |
| 294 | log.warn("Received unknown Barrier Reply: {}", msg.getXid()); |
| 295 | } |
| 296 | } finally { |
| 297 | pendingBatches.invalidate(msg.getXid()); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 298 | } |
| 299 | break; |
| 300 | case ERROR: |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 301 | log.warn("received Error message {} from {}", msg, dpid); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 302 | |
| 303 | OFErrorMsg error = (OFErrorMsg) msg; |
| 304 | if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) { |
| 305 | OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error; |
| 306 | if (fmFailed.getData().getParsedMessage().isPresent()) { |
| 307 | OFMessage m = fmFailed.getData().getParsedMessage().get(); |
| 308 | OFFlowMod fm = (OFFlowMod) m; |
| 309 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
| 310 | if (entry != null) { |
| 311 | entry.appendFailure(new FlowEntryBuilder(dpid, fm).build()); |
| 312 | } else { |
| 313 | log.error("No matching batch for this error: {}", error); |
| 314 | } |
| 315 | } else { |
| 316 | //FIXME: Potentially add flowtracking to avoid this message. |
| 317 | log.error("Flow installation failed but switch didn't" + |
| 318 | " tell us which one."); |
| 319 | } |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 320 | } else { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 321 | log.warn("Received error {}", error); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 322 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 323 | |
| 324 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 325 | default: |
| 326 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | } |
| 330 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 331 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 332 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 333 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 334 | // Do nothing here for now. |
| 335 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 336 | |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 337 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 338 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 339 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 340 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 341 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
| 342 | .filter(entry -> !tableMissRule(dpid, entry)) |
| 343 | .map(entry -> new FlowEntryBuilder(dpid, entry).build()) |
| 344 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 345 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 346 | providerService.pushFlowMetrics(did, flowEntries); |
| 347 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 348 | } |
| 349 | |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 350 | private boolean tableMissRule(Dpid dpid, OFFlowStatsEntry reply) { |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 351 | if (reply.getMatch().getMatchFields().iterator().hasNext()) { |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 352 | return false; |
| 353 | } |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 354 | if (reply.getVersion().equals(OFVersion.OF_10)) { |
| 355 | return reply.getPriority() == LOWEST_PRIORITY |
| 356 | && reply.getActions().isEmpty(); |
| 357 | } |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 358 | for (OFInstruction ins : reply.getInstructions()) { |
| 359 | if (ins.getType() == OFInstructionType.APPLY_ACTIONS) { |
| 360 | OFInstructionApplyActions apply = (OFInstructionApplyActions) ins; |
| 361 | List<OFAction> acts = apply.getActions(); |
| 362 | for (OFAction act : acts) { |
| 363 | if (act.getType() == OFActionType.OUTPUT) { |
| 364 | OFActionOutput out = (OFActionOutput) act; |
| 365 | if (out.getPort() == OFPort.CONTROLLER) { |
| 366 | return true; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | return false; |
| 373 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 374 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 375 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 376 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 377 | /** |
| 378 | * The internal cache entry holding the original request as well |
| 379 | * as accumulating the any failures along the way. |
| 380 | * |
| 381 | * If this entry is evicted from the cache then the entire operation |
| 382 | * is considered failed. Otherwise, only the failures reported by the device |
| 383 | * will be propagated up. |
| 384 | */ |
| 385 | private class InternalCacheEntry { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 386 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 387 | private final FlowRuleBatchOperation operation; |
| 388 | private final Set<FlowRule> failures = Sets.newConcurrentHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 389 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 390 | public InternalCacheEntry(FlowRuleBatchOperation operation) { |
| 391 | this.operation = operation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 394 | /** |
| 395 | * Appends a failed rule to the set of failed items. |
| 396 | * @param rule the failed rule |
| 397 | */ |
| 398 | public void appendFailure(FlowRule rule) { |
| 399 | failures.add(rule); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 402 | /** |
| 403 | * Fails the entire batch and returns the failed operation. |
| 404 | * @return the failed operation |
| 405 | */ |
| 406 | public CompletedBatchOperation failedCompletion() { |
| 407 | Set<FlowRule> fails = operation.getOperations().stream() |
| 408 | .map(op -> op.target()).collect(Collectors.toSet()); |
| 409 | return new CompletedBatchOperation(false, Collections.unmodifiableSet(fails), operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame^] | 412 | /** |
| 413 | * Returns the completed operation and whether the batch suceeded. |
| 414 | * @return the completed operation |
| 415 | */ |
| 416 | public CompletedBatchOperation completed() { |
| 417 | return new CompletedBatchOperation(failures.isEmpty(), |
| 418 | Collections.unmodifiableSet(failures), operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 419 | } |
| 420 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 421 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 422 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 423 | } |