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