David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4d08470 | 2017-08-03 22:45:58 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 16 | package org.opencord.igmpproxy; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.onlab.packet.Ip4Address; |
| 20 | import org.onosproject.net.DeviceId; |
Esin Karaman | 305908c | 2020-02-24 14:42:52 +0000 | [diff] [blame^] | 21 | import org.onosproject.net.PortNumber; |
| 22 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 27 | * State machine for whole IGMP process. The state machine is implemented on |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 28 | * RFC 2236 "6. Host State Diagram". |
| 29 | */ |
| 30 | public final class StateMachine { |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 31 | |
| 32 | private static final String GROUP = "Group"; |
| 33 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 34 | private StateMachine() { |
| 35 | |
| 36 | } |
| 37 | private static Map<String, SingleStateMachine> map = Maps.newConcurrentMap(); |
| 38 | |
| 39 | private static String getId(DeviceId devId, Ip4Address groupIp) { |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 40 | return devId.toString() + GROUP + groupIp.toString(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | private static SingleStateMachine get(DeviceId devId, Ip4Address groupIp) { |
| 44 | String id = getId(devId, groupIp); |
| 45 | return map.get(id); |
| 46 | } |
| 47 | |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 48 | public static void destroySingle(DeviceId devId, Ip4Address groupIp) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 49 | SingleStateMachine machine = get(devId, groupIp); |
| 50 | if (null == machine) { |
| 51 | return; |
| 52 | } |
| 53 | machine.cancelTimer(); |
| 54 | map.remove(getId(devId, groupIp)); |
| 55 | } |
| 56 | |
Esin Karaman | 305908c | 2020-02-24 14:42:52 +0000 | [diff] [blame^] | 57 | public static boolean join(DeviceId devId, Ip4Address groupIp, Ip4Address srcIP, PortNumber upLinkPort) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 58 | SingleStateMachine machine = get(devId, groupIp); |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 59 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 60 | if (null == machine) { |
Esin Karaman | 305908c | 2020-02-24 14:42:52 +0000 | [diff] [blame^] | 61 | machine = new SingleStateMachine(devId, groupIp, srcIP, upLinkPort); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 62 | map.put(getId(devId, groupIp), machine); |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 63 | |
| 64 | boolean shouldSendJoin = true; |
| 65 | if (IgmpManager.isIgmpOnPodBasis() && |
| 66 | groupListenedByOtherDevices(devId, groupIp)) { |
| 67 | // unset the flag if igmp messages are evaluated on POD basis |
| 68 | // and there are already active members of this group |
| 69 | // across the entire POD |
| 70 | shouldSendJoin = false; |
| 71 | } |
| 72 | machine.join(shouldSendJoin); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | machine.increaseCounter(); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | public static boolean leave(DeviceId devId, Ip4Address groupIp) { |
| 80 | SingleStateMachine machine = get(devId, groupIp); |
| 81 | if (null == machine) { |
| 82 | return false; |
| 83 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 84 | |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 85 | machine.decreaseCounter(); |
| 86 | // make sure machine instance still exists. |
| 87 | // it may be removed by the preceding thread |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 88 | if (machine.getCounter() == 0) { |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 89 | boolean shouldSendLeave = true; |
| 90 | if (IgmpManager.isIgmpOnPodBasis() && |
| 91 | groupListenedByOtherDevices(devId, groupIp)) { |
| 92 | // unset the flag if igmp messages are evaluated on POD basis |
| 93 | // and there are still active members of this group |
| 94 | // across the entire POD |
| 95 | shouldSendLeave = false; |
| 96 | } |
| 97 | machine.leave(shouldSendLeave); |
| 98 | destroySingle(devId, groupIp); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | static void specialQuery(DeviceId devId, Ip4Address groupIp, int maxResp) { |
| 105 | SingleStateMachine machine = get(devId, groupIp); |
| 106 | if (null == machine) { |
| 107 | return; |
| 108 | } |
| 109 | machine.query(maxResp); |
| 110 | } |
| 111 | |
| 112 | static void generalQuery(DeviceId devId, int maxResp) { |
| 113 | for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) { |
| 114 | SingleStateMachine machine = entry.getValue(); |
| 115 | if (devId.equals(machine.getDeviceId())) { |
| 116 | machine.query(maxResp); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 121 | static void generalQuery(int maxResp) { |
| 122 | for (Map.Entry<String, SingleStateMachine> entry : map.entrySet()) { |
| 123 | SingleStateMachine machine = entry.getValue(); |
| 124 | machine.query(maxResp); |
| 125 | } |
| 126 | } |
| 127 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 128 | public static Set<Map.Entry<String, SingleStateMachine>> entrySet() { |
| 129 | return map.entrySet(); |
| 130 | } |
| 131 | |
| 132 | public static void timeOut(DeviceId devId, Ip4Address groupIp) { |
| 133 | SingleStateMachine machine = get(devId, groupIp); |
| 134 | if (null == machine) { |
| 135 | return; |
| 136 | } |
| 137 | machine.timeOut(); |
| 138 | } |
| 139 | |
| 140 | public static void clearMap() { |
| 141 | map.clear(); |
| 142 | } |
| 143 | |
Esin Karaman | a05342e | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 144 | /** |
| 145 | * @param devId id of the device being excluded |
| 146 | * @param groupIp group IP address |
| 147 | * @return true if this group has at least one listener connected to |
| 148 | * any device in the map except for the device specified; false otherwise. |
| 149 | */ |
| 150 | private static boolean groupListenedByOtherDevices(DeviceId devId, Ip4Address groupIp) { |
| 151 | for (SingleStateMachine machine : map.values()) { |
| 152 | if (machine.getDeviceId().equals(devId)) { |
| 153 | continue; |
| 154 | } |
| 155 | if (machine.getGroupIp().equals(groupIp)) { |
| 156 | //means group is being listened by other peers in the domain |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 163 | } |