Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Yuta HIGUCHI | 7b41dc9 | 2017-06-22 19:37:06 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
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; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Property; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Service; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 30 | import org.onosproject.core.CoreService; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceEvent; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceListener; |
| 33 | import org.onosproject.net.device.DeviceService; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 36 | import org.onosproject.openflow.controller.Dpid; |
| 37 | import org.onosproject.openflow.controller.OpenFlowController; |
| 38 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 39 | import org.onosproject.openflow.controller.OpenFlowMessageListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 41 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 42 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 43 | import org.onosproject.openflow.controller.PacketListener; |
| 44 | import org.onosproject.openflow.controller.RoleState; |
| 45 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 46 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry; |
| 48 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 50 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.protocol.OFFactories; |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFFlowLightweightStatsEntry; |
| 53 | import org.projectfloodlight.openflow.protocol.OFFlowLightweightStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 55 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 57 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 58 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 59 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 61 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.OFQueueStatsEntry; |
| 66 | import org.projectfloodlight.openflow.protocol.OFQueueStatsReply; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFTableStatsEntry; |
| 70 | import org.projectfloodlight.openflow.protocol.OFTableStatsReply; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 72 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 73 | import org.slf4j.Logger; |
| 74 | import org.slf4j.LoggerFactory; |
| 75 | |
Yuta HIGUCHI | 7b41dc9 | 2017-06-22 19:37:06 -0700 | [diff] [blame] | 76 | import java.util.ArrayList; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 77 | import java.util.Collection; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 78 | import java.util.Collections; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 79 | import java.util.List; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 80 | import java.util.Optional; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 81 | import java.util.Set; |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 82 | import java.util.concurrent.CompletableFuture; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 83 | import java.util.concurrent.ConcurrentHashMap; |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 84 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 85 | import java.util.concurrent.CopyOnWriteArraySet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 86 | import java.util.concurrent.ExecutorService; |
| 87 | import java.util.concurrent.Executors; |
| 88 | import java.util.concurrent.locks.Lock; |
| 89 | import java.util.concurrent.locks.ReentrantLock; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 90 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 91 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | e71494d | 2016-11-15 14:45:25 -0800 | [diff] [blame] | 92 | import static org.onosproject.net.Device.Type.CONTROLLER; |
| 93 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 94 | import static org.onosproject.openflow.controller.Dpid.dpid; |
Ozge AYAZ | 60aded2 | 2017-06-20 08:35:30 +0000 | [diff] [blame] | 95 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 96 | |
| 97 | @Component(immediate = true) |
| 98 | @Service |
| 99 | public class OpenFlowControllerImpl implements OpenFlowController { |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 100 | private static final String APP_ID = "org.onosproject.openflow-base"; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 101 | private static final String DEFAULT_OFPORT = "6633,6653"; |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 102 | private static final int DEFAULT_WORKER_THREADS = 0; |
Andrea Campanella | 86e0c56 | 2017-11-23 16:38:24 +0100 | [diff] [blame] | 103 | protected static final String SCHEME = "of"; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 104 | |
| 105 | private static final Logger log = |
| 106 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 107 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 109 | protected CoreService coreService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 112 | protected DriverService driverService; |
| 113 | |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected ComponentConfigService cfgService; |
| 116 | |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected DeviceService deviceService; |
| 119 | |
| 120 | |
Brian O'Connor | e2a399e | 2015-09-22 15:32:50 -0700 | [diff] [blame] | 121 | @Property(name = "openflowPorts", value = DEFAULT_OFPORT, |
| 122 | label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653") |
| 123 | private String openflowPorts = DEFAULT_OFPORT; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 124 | |
| 125 | @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS, |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 126 | label = "Number of controller worker threads") |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 127 | private int workerThreads = DEFAULT_WORKER_THREADS; |
| 128 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 129 | protected ExecutorService executorMsgs = |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 130 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d", log)); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 131 | |
| 132 | private final ExecutorService executorBarrier = |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 133 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d", log)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 134 | |
Prince Pereira | e779803 | 2016-07-08 16:31:58 +0530 | [diff] [blame] | 135 | //Separate executor thread for handling error messages and barrier replies for same failed |
| 136 | // transactions to avoid context switching of thread |
| 137 | protected ExecutorService executorErrorMsgs = |
| 138 | Executors.newSingleThreadExecutor(groupedThreads("onos/of", "event-error-msg-%d", log)); |
| 139 | |
| 140 | //concurrent hashmap to track failed transactions |
| 141 | protected ConcurrentMap<Long, Boolean> errorMsgs = |
| 142 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 143 | protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 144 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 145 | protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 146 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 147 | protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 148 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 149 | |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 150 | // Key: dpid, value: map with key: long (XID), value: completable future |
| 151 | protected ConcurrentMap<Dpid, ConcurrentMap<Long, CompletableFuture<OFMessage>>> responses = |
| 152 | new ConcurrentHashMap<>(); |
| 153 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 154 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 155 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 156 | |
| 157 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 158 | ArrayListMultimap.create(); |
| 159 | |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 160 | protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 161 | |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 162 | protected Set<OpenFlowMessageListener> ofMessageListener = new CopyOnWriteArraySet<>(); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 163 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 164 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 165 | ArrayListMultimap.create(); |
| 166 | |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 167 | protected Multimap<Dpid, OFFlowLightweightStatsEntry> fullFlowLightweightStats = |
| 168 | ArrayListMultimap.create(); |
| 169 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 170 | protected Multimap<Dpid, OFTableStatsEntry> fullTableStats = |
| 171 | ArrayListMultimap.create(); |
| 172 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 173 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 174 | ArrayListMultimap.create(); |
| 175 | |
| 176 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 177 | ArrayListMultimap.create(); |
| 178 | |
Yuta HIGUCHI | 7b41dc9 | 2017-06-22 19:37:06 -0700 | [diff] [blame] | 179 | // deprecated in 1.11.0, no longer referenced from anywhere |
| 180 | @Deprecated |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 181 | protected Multimap<Dpid, OFPortStatsEntry> fullPortStats = |
| 182 | ArrayListMultimap.create(); |
| 183 | |
Ozge AYAZ | 60aded2 | 2017-06-20 08:35:30 +0000 | [diff] [blame] | 184 | protected Multimap<Dpid, OFQueueStatsEntry> fullQueueStats = |
| 185 | ArrayListMultimap.create(); |
| 186 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 187 | private final Controller ctrl = new Controller(); |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 188 | private InternalDeviceListener listener = new InternalDeviceListener(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 189 | |
| 190 | @Activate |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 191 | public void activate(ComponentContext context) { |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 192 | coreService.registerApplication(APP_ID, this::cleanup); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 193 | cfgService.registerProperties(getClass()); |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 194 | deviceService.addListener(listener); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 195 | ctrl.setConfigParams(context.getProperties()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 196 | ctrl.start(agent, driverService); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 199 | private void cleanup() { |
| 200 | // Close listening channel and all OF channels. Clean information about switches |
| 201 | // before deactivating |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 202 | ctrl.stop(); |
| 203 | connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch); |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 204 | connectedSwitches.clear(); |
| 205 | activeMasterSwitches.clear(); |
| 206 | activeEqualSwitches.clear(); |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 207 | } |
| 208 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 209 | @Deactivate |
| 210 | public void deactivate() { |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 211 | deviceService.removeListener(listener); |
Thiago Santos | 6172540 | 2016-08-05 17:58:56 -0300 | [diff] [blame] | 212 | cleanup(); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 213 | cfgService.unregisterProperties(getClass(), false); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 216 | @Modified |
| 217 | public void modified(ComponentContext context) { |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 218 | ctrl.stop(); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 219 | ctrl.setConfigParams(context.getProperties()); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 220 | ctrl.start(agent, driverService); |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 221 | } |
| 222 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 223 | @Override |
| 224 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 225 | return connectedSwitches.values(); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 230 | return activeMasterSwitches.values(); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 235 | return activeEqualSwitches.values(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 240 | return connectedSwitches.get(dpid); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 245 | return activeMasterSwitches.get(dpid); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 250 | return activeEqualSwitches.get(dpid); |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 255 | if (!ofSwitchListener.contains(listener)) { |
| 256 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 262 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 266 | public void addMessageListener(OpenFlowMessageListener listener) { |
| 267 | ofMessageListener.add(listener); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void removeMessageListener(OpenFlowMessageListener listener) { |
| 272 | ofMessageListener.remove(listener); |
| 273 | } |
| 274 | |
| 275 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 276 | public void addPacketListener(int priority, PacketListener listener) { |
| 277 | ofPacketListener.put(priority, listener); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void removePacketListener(PacketListener listener) { |
| 282 | ofPacketListener.values().remove(listener); |
| 283 | } |
| 284 | |
| 285 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 286 | public void addEventListener(OpenFlowEventListener listener) { |
| 287 | ofEventListener.add(listener); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void removeEventListener(OpenFlowEventListener listener) { |
| 292 | ofEventListener.remove(listener); |
| 293 | } |
| 294 | |
| 295 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 296 | public void write(Dpid dpid, OFMessage msg) { |
| 297 | this.getSwitch(dpid).sendMsg(msg); |
| 298 | } |
| 299 | |
| 300 | @Override |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 301 | public CompletableFuture<OFMessage> writeResponse(Dpid dpid, OFMessage msg) { |
| 302 | write(dpid, msg); |
| 303 | |
| 304 | ConcurrentMap<Long, CompletableFuture<OFMessage>> xids = |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 305 | responses.computeIfAbsent(dpid, k -> new ConcurrentHashMap<>()); |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 306 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 307 | CompletableFuture<OFMessage> future = new CompletableFuture<>(); |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 308 | xids.put(msg.getXid(), future); |
| 309 | |
| 310 | return future; |
| 311 | } |
| 312 | |
| 313 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 314 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 315 | OpenFlowSwitch sw = this.getSwitch(dpid); |
Laszlo Papp | b68fe7e | 2017-11-24 17:06:59 +0000 | [diff] [blame] | 316 | if (log.isTraceEnabled()) { |
| 317 | log.trace("Processing message from switch {} via openflow: {}", dpid, msg); |
| 318 | } |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 319 | |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 320 | // Check if someone is waiting for this message |
| 321 | ConcurrentMap<Long, CompletableFuture<OFMessage>> xids = responses.get(dpid); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 322 | if (xids != null) { |
| 323 | CompletableFuture<OFMessage> future = xids.remove(msg.getXid()); |
| 324 | if (future != null) { |
| 325 | future.complete(msg); |
| 326 | } |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 327 | } |
| 328 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 329 | switch (msg.getType()) { |
| 330 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 331 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 332 | l.portChanged(dpid, (OFPortStatus) msg); |
| 333 | } |
| 334 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 335 | case FEATURES_REPLY: |
| 336 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 337 | l.switchChanged(dpid); |
| 338 | } |
| 339 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 340 | case PACKET_IN: |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 341 | if (sw == null) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 342 | log.error("Ignoring PACKET_IN, switch {} is not found", dpid); |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 343 | break; |
| 344 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 345 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
Ozge AYAZ | 60aded2 | 2017-06-20 08:35:30 +0000 | [diff] [blame] | 346 | .packetContextFromPacketIn(sw, (OFPacketIn) msg); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 347 | for (PacketListener p : ofPacketListener.values()) { |
| 348 | p.handlePacket(pktCtx); |
| 349 | } |
| 350 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 351 | // TODO: Consider using separate threadpool for sensitive messages. |
| 352 | // ie. Back to back error could cause us to starve. |
| 353 | case FLOW_REMOVED: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 354 | executorMsgs.execute(new OFMessageHandler(dpid, msg)); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 355 | break; |
Prince Pereira | e779803 | 2016-07-08 16:31:58 +0530 | [diff] [blame] | 356 | case ERROR: |
| 357 | log.debug("Received error message from {}: {}", dpid, msg); |
| 358 | errorMsgs.putIfAbsent(msg.getXid(), true); |
| 359 | executorErrorMsgs.execute(new OFMessageHandler(dpid, msg)); |
| 360 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 361 | case STATS_REPLY: |
Yuta HIGUCHI | 7b41dc9 | 2017-06-22 19:37:06 -0700 | [diff] [blame] | 362 | processStatsReply(dpid, (OFStatsReply) msg); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 363 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 364 | case BARRIER_REPLY: |
Prince Pereira | e779803 | 2016-07-08 16:31:58 +0530 | [diff] [blame] | 365 | if (errorMsgs.containsKey(msg.getXid())) { |
| 366 | //To make oferror msg handling and corresponding barrier reply serialized, |
| 367 | // executorErrorMsgs is used for both transaction |
| 368 | errorMsgs.remove(msg.getXid()); |
| 369 | executorErrorMsgs.execute(new OFMessageHandler(dpid, msg)); |
| 370 | } else { |
| 371 | executorBarrier.execute(new OFMessageHandler(dpid, msg)); |
| 372 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 373 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 374 | case EXPERIMENTER: |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 375 | if (sw == null) { |
| 376 | log.error("Switch {} is not found", dpid); |
| 377 | break; |
| 378 | } |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 379 | long experimenter = ((OFExperimenter) msg).getExperimenter(); |
| 380 | if (experimenter == 0x748771) { |
| 381 | // LINC-OE port stats |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 382 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 383 | OFPortStatus.Builder portStatus = sw.factory().buildPortStatus(); |
| 384 | OFPortDesc.Builder portDesc = sw.factory().buildPortDesc(); |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 385 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 386 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 387 | .setName(circuitPortStatus.getName()) |
| 388 | .setConfig(circuitPortStatus.getConfig()) |
| 389 | .setState(circuitPortStatus.getState()); |
| 390 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 391 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 392 | l.portChanged(dpid, portStatus.build()); |
| 393 | } |
| 394 | } else { |
| 395 | log.warn("Handling experimenter type {} not yet implemented", |
| 396 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 397 | } |
| 398 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 399 | default: |
| 400 | log.warn("Handling message type {} not yet implemented {}", |
| 401 | msg.getType(), msg); |
| 402 | } |
| 403 | } |
| 404 | |
Yuta HIGUCHI | 7b41dc9 | 2017-06-22 19:37:06 -0700 | [diff] [blame] | 405 | private void processStatsReply(Dpid dpid, OFStatsReply reply) { |
| 406 | switch (reply.getStatsType()) { |
| 407 | case QUEUE: |
| 408 | Collection<OFQueueStatsEntry> queueStatsEntries = publishQueueStats(dpid, (OFQueueStatsReply) reply); |
| 409 | if (queueStatsEntries != null) { |
| 410 | OFQueueStatsReply.Builder rep = |
| 411 | OFFactories.getFactory(reply.getVersion()).buildQueueStatsReply(); |
| 412 | rep.setEntries(ImmutableList.copyOf(queueStatsEntries)); |
| 413 | rep.setXid(reply.getXid()); |
| 414 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 415 | } |
| 416 | break; |
| 417 | |
| 418 | case PORT_DESC: |
| 419 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 420 | l.switchChanged(dpid); |
| 421 | } |
| 422 | break; |
| 423 | |
| 424 | case FLOW: |
| 425 | Collection<OFFlowStatsEntry> flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 426 | if (flowStats != null) { |
| 427 | OFFlowStatsReply.Builder rep = |
| 428 | OFFactories.getFactory(reply.getVersion()).buildFlowStatsReply(); |
| 429 | rep.setEntries(ImmutableList.copyOf(flowStats)); |
| 430 | rep.setXid(reply.getXid()); |
| 431 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 432 | } |
| 433 | break; |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 434 | case FLOW_LIGHTWEIGHT: |
| 435 | Collection<OFFlowLightweightStatsEntry> flowLightweightStats = |
| 436 | publishFlowStatsLightweight(dpid, (OFFlowLightweightStatsReply) reply); |
| 437 | if (flowLightweightStats != null) { |
| 438 | OFFlowLightweightStatsReply.Builder rep = |
| 439 | OFFactories.getFactory(reply.getVersion()).buildFlowLightweightStatsReply(); |
| 440 | rep.setEntries(ImmutableList.copyOf(flowLightweightStats)); |
| 441 | rep.setXid(reply.getXid()); |
| 442 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 443 | } |
| 444 | break; |
Yuta HIGUCHI | 7b41dc9 | 2017-06-22 19:37:06 -0700 | [diff] [blame] | 445 | case TABLE: |
| 446 | Collection<OFTableStatsEntry> tableStats = publishTableStats(dpid, (OFTableStatsReply) reply); |
| 447 | if (tableStats != null) { |
| 448 | OFTableStatsReply.Builder rep = |
| 449 | OFFactories.getFactory(reply.getVersion()).buildTableStatsReply(); |
| 450 | rep.setEntries(ImmutableList.copyOf(tableStats)); |
| 451 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 452 | } |
| 453 | break; |
| 454 | |
| 455 | case GROUP: |
| 456 | Collection<OFGroupStatsEntry> groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 457 | if (groupStats != null) { |
| 458 | OFGroupStatsReply.Builder rep = |
| 459 | OFFactories.getFactory(reply.getVersion()).buildGroupStatsReply(); |
| 460 | rep.setEntries(ImmutableList.copyOf(groupStats)); |
| 461 | rep.setXid(reply.getXid()); |
| 462 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 463 | } |
| 464 | break; |
| 465 | |
| 466 | case GROUP_DESC: |
| 467 | Collection<OFGroupDescStatsEntry> groupDescStats = publishGroupDescStats(dpid, |
| 468 | (OFGroupDescStatsReply) reply); |
| 469 | if (groupDescStats != null) { |
| 470 | OFGroupDescStatsReply.Builder rep = |
| 471 | OFFactories.getFactory(reply.getVersion()).buildGroupDescStatsReply(); |
| 472 | rep.setEntries(ImmutableList.copyOf(groupDescStats)); |
| 473 | rep.setXid(reply.getXid()); |
| 474 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 475 | } |
| 476 | break; |
| 477 | |
| 478 | case PORT: |
| 479 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
| 480 | break; |
| 481 | |
| 482 | case METER: |
| 483 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
| 484 | break; |
| 485 | |
| 486 | case EXPERIMENTER: |
| 487 | if (reply instanceof OFCalientFlowStatsReply) { |
| 488 | OpenFlowSwitch sw = this.getSwitch(dpid); |
| 489 | // Convert Calient flow statistics to regular flow stats |
| 490 | // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API |
| 491 | if (sw == null) { |
| 492 | log.error("Switch {} is not found", dpid); |
| 493 | break; |
| 494 | } |
| 495 | OFFlowStatsReply.Builder fsr = sw.factory().buildFlowStatsReply(); |
| 496 | List<OFFlowStatsEntry> entries = new ArrayList<>(); |
| 497 | for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) reply).getEntries()) { |
| 498 | |
| 499 | // Single instruction, i.e., output to port |
| 500 | OFActionOutput action = sw.factory() |
| 501 | .actions() |
| 502 | .buildOutput() |
| 503 | .setPort(entry.getOutPort()) |
| 504 | .build(); |
| 505 | OFInstruction instruction = sw.factory() |
| 506 | .instructions() |
| 507 | .applyActions(Collections.singletonList(action)); |
| 508 | OFFlowStatsEntry fs = sw.factory().buildFlowStatsEntry() |
| 509 | .setMatch(entry.getMatch()) |
| 510 | .setTableId(entry.getTableId()) |
| 511 | .setDurationSec(entry.getDurationSec()) |
| 512 | .setDurationNsec(entry.getDurationNsec()) |
| 513 | .setPriority(entry.getPriority()) |
| 514 | .setIdleTimeout(entry.getIdleTimeout()) |
| 515 | .setHardTimeout(entry.getHardTimeout()) |
| 516 | .setFlags(entry.getFlags()) |
| 517 | .setCookie(entry.getCookie()) |
| 518 | .setInstructions(Collections.singletonList(instruction)) |
| 519 | .build(); |
| 520 | entries.add(fs); |
| 521 | } |
| 522 | fsr.setEntries(entries); |
| 523 | |
| 524 | flowStats = publishFlowStats(dpid, fsr.build()); |
| 525 | if (flowStats != null) { |
| 526 | OFFlowStatsReply.Builder rep = |
| 527 | sw.factory().buildFlowStatsReply(); |
| 528 | rep.setEntries(ImmutableList.copyOf(flowStats)); |
| 529 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
| 530 | } |
| 531 | } else { |
| 532 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
| 533 | } |
| 534 | break; |
| 535 | default: |
| 536 | log.warn("Discarding unknown stats reply type {}", reply.getStatsType()); |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 541 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 542 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 543 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 544 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 545 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 546 | return fullFlowStats.removeAll(dpid); |
| 547 | } |
| 548 | return null; |
| 549 | } |
| 550 | |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 551 | private synchronized Collection<OFFlowLightweightStatsEntry> publishFlowStatsLightweight( |
| 552 | Dpid dpid, |
| 553 | OFFlowLightweightStatsReply reply) { |
| 554 | //TODO: Get rid of synchronized |
| 555 | fullFlowLightweightStats.putAll(dpid, reply.getEntries()); |
| 556 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 557 | return fullFlowLightweightStats.removeAll(dpid); |
| 558 | } |
| 559 | return null; |
| 560 | } |
| 561 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 562 | private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid, |
| 563 | OFTableStatsReply reply) { |
| 564 | //TODO: Get rid of synchronized |
| 565 | fullTableStats.putAll(dpid, reply.getEntries()); |
| 566 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 567 | return fullTableStats.removeAll(dpid); |
| 568 | } |
| 569 | return null; |
| 570 | } |
| 571 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 572 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 573 | OFGroupStatsReply reply) { |
| 574 | //TODO: Get rid of synchronized |
| 575 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 576 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 577 | return fullGroupStats.removeAll(dpid); |
| 578 | } |
| 579 | return null; |
| 580 | } |
| 581 | |
| 582 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 583 | OFGroupDescStatsReply reply) { |
| 584 | //TODO: Get rid of synchronized |
| 585 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 586 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 587 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 588 | } |
| 589 | return null; |
| 590 | } |
| 591 | |
Ozge AYAZ | 60aded2 | 2017-06-20 08:35:30 +0000 | [diff] [blame] | 592 | private synchronized Collection<OFQueueStatsEntry> publishQueueStats(Dpid dpid, OFQueueStatsReply reply) { |
| 593 | fullQueueStats.putAll(dpid, reply.getEntries()); |
| 594 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 595 | return fullQueueStats.removeAll(dpid); |
| 596 | } |
| 597 | return null; |
| 598 | } |
| 599 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 600 | @Override |
| 601 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 602 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 603 | if (sw == null) { |
| 604 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 605 | return; |
| 606 | } |
| 607 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 610 | class InternalDeviceListener implements DeviceListener { |
| 611 | |
| 612 | @Override |
| 613 | public boolean isRelevant(DeviceEvent event) { |
Andrea Campanella | 86e0c56 | 2017-11-23 16:38:24 +0100 | [diff] [blame] | 614 | return event.subject().type() != CONTROLLER && event.type() == DEVICE_REMOVED |
| 615 | && event.subject().id().uri().getScheme().equals(SCHEME); |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public void event(DeviceEvent event) { |
| 620 | switch (event.type()) { |
| 621 | case DEVICE_ADDED: |
| 622 | break; |
| 623 | case DEVICE_AVAILABILITY_CHANGED: |
| 624 | break; |
| 625 | case DEVICE_REMOVED: |
| 626 | // Device administratively removed, disconnect |
| 627 | Optional.ofNullable(getSwitch(dpid(event.subject().id().uri()))) |
| 628 | .ifPresent(OpenFlowSwitch::disconnectSwitch); |
| 629 | break; |
| 630 | case DEVICE_SUSPENDED: |
| 631 | break; |
| 632 | case DEVICE_UPDATED: |
| 633 | break; |
| 634 | case PORT_ADDED: |
| 635 | break; |
| 636 | case PORT_REMOVED: |
| 637 | break; |
| 638 | case PORT_STATS_UPDATED: |
| 639 | break; |
| 640 | case PORT_UPDATED: |
| 641 | break; |
| 642 | default: |
| 643 | break; |
| 644 | |
| 645 | } |
| 646 | |
| 647 | } |
| 648 | |
| 649 | } |
| 650 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 651 | /** |
| 652 | * Implementation of an OpenFlow Agent which is responsible for |
| 653 | * keeping track of connected switches and the state in which |
| 654 | * they are. |
| 655 | */ |
| 656 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 657 | |
| 658 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 659 | private final Lock switchLock = new ReentrantLock(); |
| 660 | |
| 661 | @Override |
| 662 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 663 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 664 | if (connectedSwitches.get(dpid) != null) { |
| 665 | log.error("Trying to add connectedSwitch but found a previous " |
| 666 | + "value for dpid: {}", dpid); |
| 667 | return false; |
| 668 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 669 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 670 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 671 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 672 | l.switchAdded(dpid); |
| 673 | } |
| 674 | return true; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public boolean validActivation(Dpid dpid) { |
| 680 | if (connectedSwitches.get(dpid) == null) { |
| 681 | log.error("Trying to activate switch but is not in " |
| 682 | + "connected switches: dpid {}. Aborting ..", |
| 683 | dpid); |
| 684 | return false; |
| 685 | } |
| 686 | if (activeMasterSwitches.get(dpid) != null || |
| 687 | activeEqualSwitches.get(dpid) != null) { |
| 688 | log.error("Trying to activate switch but it is already " |
| 689 | + "activated: dpid {}. Found in activeMaster: {} " |
Ray Milkey | 6bc43c2 | 2015-11-06 13:22:38 -0800 | [diff] [blame] | 690 | + "Found in activeEqual: {}. Aborting ..", |
| 691 | dpid, |
| 692 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 693 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 694 | return false; |
| 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | @Override |
| 701 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 702 | switchLock.lock(); |
| 703 | try { |
| 704 | if (!validActivation(dpid)) { |
| 705 | return false; |
| 706 | } |
| 707 | activeMasterSwitches.put(dpid, sw); |
| 708 | return true; |
| 709 | } finally { |
| 710 | switchLock.unlock(); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | @Override |
| 715 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 716 | switchLock.lock(); |
| 717 | try { |
| 718 | if (!validActivation(dpid)) { |
| 719 | return false; |
| 720 | } |
| 721 | activeEqualSwitches.put(dpid, sw); |
| 722 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 723 | return true; |
| 724 | } finally { |
| 725 | switchLock.unlock(); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | @Override |
| 730 | public void transitionToMasterSwitch(Dpid dpid) { |
| 731 | switchLock.lock(); |
| 732 | try { |
| 733 | if (activeMasterSwitches.containsKey(dpid)) { |
| 734 | return; |
| 735 | } |
| 736 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 737 | if (sw == null) { |
| 738 | sw = getSwitch(dpid); |
| 739 | if (sw == null) { |
| 740 | log.error("Transition to master called on sw {}, but switch " |
| 741 | + "was not found in controller-cache", dpid); |
| 742 | return; |
| 743 | } |
| 744 | } |
| 745 | log.info("Transitioned switch {} to MASTER", dpid); |
| 746 | activeMasterSwitches.put(dpid, sw); |
| 747 | } finally { |
| 748 | switchLock.unlock(); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | |
| 753 | @Override |
| 754 | public void transitionToEqualSwitch(Dpid dpid) { |
| 755 | switchLock.lock(); |
| 756 | try { |
| 757 | if (activeEqualSwitches.containsKey(dpid)) { |
| 758 | return; |
| 759 | } |
| 760 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 761 | if (sw == null) { |
| 762 | sw = getSwitch(dpid); |
| 763 | if (sw == null) { |
| 764 | log.error("Transition to equal called on sw {}, but switch " |
| 765 | + "was not found in controller-cache", dpid); |
| 766 | return; |
| 767 | } |
| 768 | } |
| 769 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 770 | activeEqualSwitches.put(dpid, sw); |
| 771 | } finally { |
| 772 | switchLock.unlock(); |
| 773 | } |
| 774 | |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public void removeConnectedSwitch(Dpid dpid) { |
| 779 | connectedSwitches.remove(dpid); |
| 780 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 781 | if (sw == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 782 | log.debug("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 783 | sw = activeEqualSwitches.remove(dpid); |
| 784 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 785 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 786 | l.switchRemoved(dpid); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 791 | public void processDownstreamMessage(Dpid dpid, List<OFMessage> m) { |
| 792 | for (OpenFlowMessageListener listener : ofMessageListener) { |
| 793 | listener.handleOutgoingMessage(dpid, m); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | |
| 798 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 799 | public void processMessage(Dpid dpid, OFMessage m) { |
| 800 | processPacket(dpid, m); |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 801 | |
| 802 | for (OpenFlowMessageListener listener : ofMessageListener) { |
| 803 | listener.handleIncomingMessage(dpid, m); |
| 804 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 805 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 806 | |
| 807 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 808 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 809 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 810 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 815 | /** |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 816 | * OpenFlow message handler. |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 817 | */ |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 818 | protected final class OFMessageHandler implements Runnable { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 819 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 820 | final OFMessage msg; |
| 821 | final Dpid dpid; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 822 | |
| 823 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 824 | this.msg = msg; |
| 825 | this.dpid = dpid; |
| 826 | } |
| 827 | |
| 828 | @Override |
| 829 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 830 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 831 | listener.handleMessage(dpid, msg); |
| 832 | } |
| 833 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 834 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 835 | } |