blob: c5aaee7ca412922261ce09fbb6b7ec5c84b0868e [file] [log] [blame]
David K. Bainbridged77028f2017-08-01 12:47:55 -07001/*
Brian O'Connor4d084702017-08-03 22:45:58 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridged77028f2017-08-01 12:47:55 -07003 *
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 han81a38b92017-03-10 18:41:44 +080016package org.opencord.igmpproxy;
17
18import org.onlab.packet.Ethernet;
19import org.onlab.packet.Ip4Address;
20import org.onosproject.net.DeviceId;
21
22import java.util.Random;
Esin Karamana05342e2019-09-17 13:01:25 +000023import java.util.concurrent.atomic.AtomicInteger;
ke han81a38b92017-03-10 18:41:44 +080024
25/**
David K. Bainbridged77028f2017-08-01 12:47:55 -070026 * State machine for single IGMP group member. The state machine is implemented on
ke han81a38b92017-03-10 18:41:44 +080027 * RFC 2236 "6. Host State Diagram".
28 */
29public class SingleStateMachine {
Vignesh Ethirajd1957c92019-11-18 11:52:49 +000030 // Only for tests purposes
31 static boolean sendQuery = true;
32
ke han81a38b92017-03-10 18:41:44 +080033 static final int STATE_NON = 0;
34 static final int STATE_DELAY = 1;
35 static final int STATE_IDLE = 2;
36 static final int TRANSITION_JOIN = 0;
37 static final int TRANSITION_LEAVE = 1;
38 static final int TRANSITION_QUERY = 2;
39 static final int TRANSITION_TIMEOUT = 3;
40 static final int DEFAULT_MAX_RESP = 0xfffffff;
41 static final int DEFAULT_COUNT = 1;
42 private DeviceId devId;
43 private Ip4Address groupIp;
44 private Ip4Address srcIp;
45
Esin Karamana05342e2019-09-17 13:01:25 +000046 private AtomicInteger count = new AtomicInteger(DEFAULT_COUNT);
ke han81a38b92017-03-10 18:41:44 +080047 private int timerId = IgmpTimer.INVALID_TIMER_ID;
48 private int timeOut = DEFAULT_MAX_RESP;
49 private State[] states =
50 {
51 new NonMember(), new DelayMember(), new IdleMember()
52 };
53 private int[] nonTransition =
54 {STATE_DELAY, STATE_NON, STATE_NON, STATE_NON};
55 private int[] delayTransition =
56 {STATE_DELAY, STATE_NON, STATE_DELAY, STATE_IDLE};
57 private int[] idleTransition =
58 {STATE_IDLE, STATE_NON, STATE_DELAY, STATE_IDLE};
59 //THE TRANSITION TABLE
60 private int[][] transition =
61 {nonTransition, delayTransition, idleTransition};
62 private int currentState = STATE_NON;
63
64 public SingleStateMachine(DeviceId devId, Ip4Address groupIp, Ip4Address src) {
65 this.devId = devId;
66 this.groupIp = groupIp;
67 this.srcIp = src;
68 }
69
Esin Karamana05342e2019-09-17 13:01:25 +000070 public Ip4Address getGroupIp() {
71 return groupIp;
72 }
ke han81a38b92017-03-10 18:41:44 +080073
74 public DeviceId getDeviceId() {
75 return devId;
76 }
77 public boolean increaseCounter() {
Esin Karamana05342e2019-09-17 13:01:25 +000078 count.incrementAndGet();
ke han81a38b92017-03-10 18:41:44 +080079 return true;
80 }
81
82 public boolean decreaseCounter() {
Esin Karamana05342e2019-09-17 13:01:25 +000083 if (count.get() > 0) {
84 count.decrementAndGet();
ke han81a38b92017-03-10 18:41:44 +080085 return true;
86 } else {
87 return false;
88 }
89 }
90
91 public int getCounter() {
Esin Karamana05342e2019-09-17 13:01:25 +000092 return count.get();
ke han81a38b92017-03-10 18:41:44 +080093 }
94 public int currentState() {
95 return currentState;
96 }
97
98 private void next(int msg) {
99 currentState = transition[currentState][msg];
100 }
101
Esin Karamana05342e2019-09-17 13:01:25 +0000102 public void join(boolean messageOutAllowed) {
103 states[currentState].join(messageOutAllowed);
ke han81a38b92017-03-10 18:41:44 +0800104 next(TRANSITION_JOIN);
105 }
106
Esin Karamana05342e2019-09-17 13:01:25 +0000107 public void leave(boolean messageOutAllowed) {
108 states[currentState].leave(messageOutAllowed);
ke han81a38b92017-03-10 18:41:44 +0800109 next(TRANSITION_LEAVE);
110 }
111
112 public void query(int maxResp) {
113 states[currentState].query(maxResp);
114 next(TRANSITION_QUERY);
115 }
116
117 public void timeOut() {
118 states[currentState].timeOut();
119 next(TRANSITION_TIMEOUT);
120 }
121
122 int getTimeOut(int maxTimeOut) {
123 Random random = new Random();
124 return Math.abs(random.nextInt()) % maxTimeOut;
125 }
126
127 protected void cancelTimer() {
128 if (IgmpTimer.INVALID_TIMER_ID != timerId) {
129 IgmpTimer.cancel(timerId);
130 }
131 }
132
133 class State {
Esin Karamana05342e2019-09-17 13:01:25 +0000134 public void join(boolean messageOutAllowed) {
ke han81a38b92017-03-10 18:41:44 +0800135 }
136
Esin Karamana05342e2019-09-17 13:01:25 +0000137 public void leave(boolean messageOutAllowed) {
138 if (messageOutAllowed) {
139 Ethernet eth = IgmpSender.getInstance().buildIgmpV3Leave(groupIp, srcIp);
140 IgmpSender.getInstance().sendIgmpPacketUplink(eth, devId);
141 }
ke han81a38b92017-03-10 18:41:44 +0800142 }
143
144 public void query(int maxResp) {
145 }
146
147 public void timeOut() {
148 }
149
150 }
151
152 class NonMember extends State {
Esin Karamana05342e2019-09-17 13:01:25 +0000153 public void join(boolean messageOutAllowed) {
154 if (messageOutAllowed) {
155 Ethernet eth = IgmpSender.getInstance().buildIgmpV3Join(groupIp, srcIp);
156 IgmpSender.getInstance().sendIgmpPacketUplink(eth, devId);
157 timeOut = getTimeOut(IgmpManager.getUnsolicitedTimeout());
158 timerId = IgmpTimer.start(SingleStateMachine.this, timeOut);
159 }
ke han81a38b92017-03-10 18:41:44 +0800160 }
161 }
162
163 class DelayMember extends State {
164 public void query(int maxResp) {
165 if (maxResp < timeOut) {
166 timeOut = getTimeOut(maxResp);
167 timerId = IgmpTimer.reset(timerId, SingleStateMachine.this, timeOut);
168 }
169 }
170
171 public void timeOut() {
Vignesh Ethirajd1957c92019-11-18 11:52:49 +0000172 if (sendQuery) {
173 Ethernet eth = IgmpSender.getInstance().buildIgmpV3ResponseQuery(groupIp, srcIp);
174 IgmpSender.getInstance().sendIgmpPacketUplink(eth, devId);
175 timeOut = DEFAULT_MAX_RESP;
176 }
ke han81a38b92017-03-10 18:41:44 +0800177 }
178
179 }
180
181 class IdleMember extends State {
182 public void query(int maxResp) {
183 timeOut = getTimeOut(maxResp);
184 timerId = IgmpTimer.start(SingleStateMachine.this, timeOut);
185 }
186 }
187}