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