blob: a25123eda17bc35fb5227a656967145ec1d3385b [file] [log] [blame]
Harshada Chaundkara4b40292019-03-18 21:11:09 +00001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.packetthrottle;
17
18import com.google.common.collect.ImmutableMap;
19import org.apache.felix.scr.annotations.Modified;
20import org.apache.felix.scr.annotations.Reference;
21import org.apache.felix.scr.annotations.ReferenceCardinality;
22import org.apache.felix.scr.annotations.Property;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.net.packet.PacketInFilter;
29import org.onosproject.net.packet.PacketService;
30import org.onosproject.net.packet.packetfilter.DefaultPacketInFilter;
31import org.onosproject.net.packet.packetfilter.ArpPacketClassifier;
32import org.onosproject.net.packet.packetfilter.Dhcp6IndirectPacketClassifier;
33import org.onosproject.net.packet.packetfilter.Dhcp6DirectPacketClassifier;
34import org.onosproject.net.packet.packetfilter.DhcpPacketClassifier;
35import org.onosproject.net.packet.packetfilter.NAPacketClassifier;
36import org.onosproject.net.packet.packetfilter.NSPacketClassifier;
37import org.onosproject.net.packet.packetfilter.IcmpPacketClassifier;
38import org.onosproject.net.packet.packetfilter.Icmp6PacketClassifier;
39import org.onosproject.packetthrottle.api.PacketThrottleService;
40
41
42import org.osgi.service.component.ComponentContext;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46import java.util.Dictionary;
47import java.util.HashMap;
48import java.util.Map;
49
50import static com.google.common.base.Strings.isNullOrEmpty;
51import static org.onlab.util.Tools.get;
52
53/**
54 * Manage the packet throttle for various type of packets.
55 */
56@Component(immediate = true)
57@Service
58public class PacketThrottleManager implements PacketThrottleService {
59
60 private final Logger log = LoggerFactory.getLogger(getClass());
61
62 protected static final String ARP_FILTER = "arpFilter";
63 protected static final String DHCP_FILTER = "dhcpFilter";
64 protected static final String NS_FILTER = "nsFilter";
65 protected static final String NA_FILTER = "naFilter";
66 protected static final String DHCP6_DIRECT_FILTER = "dhcp6DirectFilter";
67 protected static final String DHCP6_INDIRECT_FILTER = "dhcp6IndirectFilter";
68 protected static final String ICMP_FILTER = "icmpFilter";
69 protected static final String ICMP6_FILTER = "icmp6Filter";
70
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 PacketService packetService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected ComponentConfigService configService;
77
78 private static final int PPS_ARP = 100;
79 private static final int PPS_DHCP = 100;
80 private static final int PPS_NS = 100;
81 private static final int PPS_NA = 100;
82 private static final int PPS_DHCP6_DIRECT = 100;
83 private static final int PPS_DHCP6_INDIRECT = 100;
84 private static final int PPS_ICMP = 100;
85 private static final int PPS_ICMP6 = 100;
86
87 private static final int WIN_SIZE_ARP_MS = 500;
88 private static final int WIN_SIZE_DHCP_MS = 500;
89 private static final int WIN_SIZE_NS_MS = 500;
90 private static final int WIN_SIZE_NA_MS = 500;
91 private static final int WIN_SIZE_DHCP6_DIRECT_MS = 500;
92 private static final int WIN_SIZE_DHCP6_INDIRECT_MS = 500;
93 private static final int WIN_SIZE_ICMP_MS = 500;
94 private static final int WIN_SIZE_ICMP6_MS = 500;
95
96 private static final int GUARD_TIME_ARP_SEC = 10;
97 private static final int GUARD_TIME_DHCP_SEC = 10;
98 private static final int GUARD_TIME_NS_SEC = 10;
99 private static final int GUARD_TIME_NA_SEC = 10;
100 private static final int GUARD_TIME_DHCP6_DIRECT_SEC = 10;
101 private static final int GUARD_TIME_DHCP6_INDIRECT_SEC = 10;
102 private static final int GUARD_TIME_ICMP_SEC = 10;
103 private static final int GUARD_TIME_ICMP6_SEC = 10;
104
105 private static final int WIN_THRES_ARP = 10;
106 private static final int WIN_THRES_DHCP = 10;
107 private static final int WIN_THRES_NS = 10;
108 private static final int WIN_THRES_NA = 10;
109 private static final int WIN_THRES_DHCP6_DIRECT = 10;
110 private static final int WIN_THRES_DHCP6_INDIRECT = 10;
111 private static final int WIN_THRES_ICMP = 10;
112 private static final int WIN_THRES_ICMP6 = 10;
113
114 /**
115 * Parameter to set packet per second rate for all filter types.
116 */
117 @Property(name = "ppsArp", intValue = PPS_ARP,
118 label = "Packet per second for the ARP packet")
119 private int ppsArp = PPS_ARP;
120
121 @Property(name = "ppsDhcp", intValue = PPS_DHCP,
122 label = "Packet per second for the DHCP packet")
123 private int ppsDhcp = PPS_DHCP;
124
125 @Property(name = "ppsNs", intValue = PPS_NS,
126 label = "Packet per second for the NS packet")
127 private int ppsNs = PPS_NS;
128
129 @Property(name = "ppsNa", intValue = PPS_NA,
130 label = "Packet per second for the NA packet")
131 private int ppsNa = PPS_NA;
132
133 @Property(name = "ppsDhcp6Direct", intValue = PPS_DHCP6_DIRECT,
134 label = "Packet per second for the DHCP6 Direct message")
135 private int ppsDhcp6Direct = PPS_DHCP6_DIRECT;
136
137 @Property(name = "ppsDhcp6Indirect", intValue = PPS_DHCP6_INDIRECT,
138 label = "Packet per second for the DHCP6 indirect message")
139 private int ppsDhcp6Indirect = PPS_DHCP6_INDIRECT;
140
141 @Property(name = "ppsIcmp", intValue = PPS_ICMP,
142 label = "Packet per second for the ICMP message")
143 private int ppsIcmp = PPS_ICMP;
144
145 @Property(name = "ppsIcmp6", intValue = PPS_ICMP6,
146 label = "Packet per second for the ICMP6 message")
147 private int ppsIcmp6 = PPS_ICMP6;
148
149
150 /**
151 * Parameter to set window size in milli seconds to check overflow of packets.
152 */
153
154 @Property(name = "winSizeArp", intValue = WIN_SIZE_ARP_MS,
155 label = "Window size for the ARP packet in milliseconds")
156 private int winSizeArp = WIN_SIZE_ARP_MS;
157
158 @Property(name = "winSizeDhcp", intValue = WIN_SIZE_DHCP_MS,
159 label = "Window size for the DHCP packet in milliseconds")
160 private int winSizeDhcp = WIN_SIZE_DHCP_MS;
161
162 @Property(name = "winSizeNs", intValue = WIN_SIZE_NS_MS,
163 label = "Window size for the NS packet in milliseconds")
164 private int winSizeNs = WIN_SIZE_NS_MS;
165
166 @Property(name = "winSizeNa", intValue = WIN_SIZE_NA_MS,
167 label = "Window size for the NA packet in milliseconds")
168 private int winSizeNa = WIN_SIZE_NA_MS;
169
170 @Property(name = "winSizeDhcp6Direct", intValue = WIN_SIZE_DHCP6_DIRECT_MS,
171 label = "Window size for the DHCP6 Direct message in milliseconds")
172 private int winSizeDhcp6Direct = WIN_SIZE_DHCP6_DIRECT_MS;
173
174 @Property(name = "winSizeDhcp6Indirect", intValue = WIN_SIZE_DHCP6_INDIRECT_MS,
175 label = "Window size for the DHCP6 indirect message in milliseconds")
176 private int winSizeDhcp6Indirect = WIN_SIZE_DHCP6_INDIRECT_MS;
177
178 @Property(name = "winSizeIcmp", intValue = WIN_SIZE_ICMP_MS,
179 label = "Window size for the ICMP message in milliseconds")
180 private int winSizeIcmp = WIN_SIZE_ICMP_MS;
181
182 @Property(name = "winSizeIcmp6", intValue = WIN_SIZE_ICMP6_MS,
183 label = "Window size for the ICMP6 message in milliseconds")
184 private int winSizeIcmp6 = WIN_SIZE_ICMP6_MS;
185
186 /**
187 * Time duration for which no more packets will be processed for a given filter type
188 * provided consecutive overflow windows happens.
189 */
190
191 @Property(name = "guardTimeArp", intValue = GUARD_TIME_ARP_SEC,
192 label = "Guard time for the ARP packet in seconds")
193 private int guardTimeArp = GUARD_TIME_ARP_SEC;
194
195 @Property(name = "guardTimeDhcp", intValue = GUARD_TIME_DHCP_SEC,
196 label = "Guard time for the DHCP packet in seconds")
197 private int guardTimeDhcp = GUARD_TIME_DHCP_SEC;
198
199 @Property(name = "guardTimeNs", intValue = GUARD_TIME_NS_SEC,
200 label = "Guard time for the NS packet in seconds")
201 private int guardTimeNs = GUARD_TIME_NS_SEC;
202
203 @Property(name = "guardTimeNa", intValue = GUARD_TIME_NA_SEC,
204 label = "Guard time for the NA packet in seconds")
205 private int guardTimeNa = GUARD_TIME_NA_SEC;
206
207 @Property(name = "guardTimeDhcp6Direct", intValue = GUARD_TIME_DHCP6_DIRECT_SEC,
208 label = "Guard time for the DHCP6 Direct message in seconds")
209 private int guardTimeDhcp6Direct = GUARD_TIME_DHCP6_DIRECT_SEC;
210
211 @Property(name = "guardTimeDhcp6Indirect", intValue = GUARD_TIME_DHCP6_INDIRECT_SEC,
212 label = "Guard time for the DHCP6 indirect message in seconds")
213 private int guardTimeDhcp6Indirect = GUARD_TIME_DHCP6_INDIRECT_SEC;
214
215 @Property(name = "guardTimeIcmp", intValue = GUARD_TIME_ICMP_SEC,
216 label = "Guard time for the ICMP message in seconds")
217 private int guardTimeIcmp = GUARD_TIME_ICMP_SEC;
218
219 @Property(name = "guardTimeIcmp6", intValue = GUARD_TIME_ICMP6_SEC,
220 label = "Guard time for the ICMP6 message in seconds")
221 private int guardTimeIcmp6 = GUARD_TIME_ICMP6_SEC;
222
223 /**
224 * Consecutive overflow window threshold.
225 */
226
227 @Property(name = "winThresArp", intValue = WIN_THRES_ARP,
228 label = "Window drop threshold for the ARP packet")
229 private int winThresArp = WIN_THRES_ARP;
230
231 @Property(name = "winThresDhcp", intValue = WIN_THRES_DHCP,
232 label = "Window drop threshold for the DHCP packet")
233 private int winThresDhcp = WIN_THRES_DHCP;
234
235 @Property(name = "winThresNs", intValue = WIN_THRES_NS,
236 label = "Window drop threshold for the NS packet")
237 private int winThresNs = WIN_THRES_NS;
238
239 @Property(name = "winThresNa", intValue = WIN_THRES_NA,
240 label = "Window drop threshold for the NA packet")
241 private int winThresNa = WIN_THRES_NA;
242
243 @Property(name = "winThresDhcp6Direct", intValue = WIN_THRES_DHCP6_DIRECT,
244 label = "Window drop threshold for the DHCP6 Direct message")
245 private int winThresDhcp6Direct = WIN_THRES_DHCP6_DIRECT;
246
247 @Property(name = "winThresDhcp6Indirect", intValue = WIN_THRES_DHCP6_INDIRECT,
248 label = "Window drop threshold for the DHCP6 indirect message")
249 private int winThresDhcp6Indirect = WIN_THRES_DHCP6_INDIRECT;
250
251 @Property(name = "winThresIcmp", intValue = WIN_THRES_ICMP,
252 label = "Window drop threshold for the ICMP message")
253 private int winThresIcmp = WIN_THRES_ICMP;
254
255 @Property(name = "winThresIcmp6", intValue = WIN_THRES_ICMP6,
256 label = "Window drop threshold for the ICMP6 message")
257 private int winThresIcmp6 = WIN_THRES_ICMP6;
258
259
260
261
262 private Map<String, PacketInFilter> mapCounterFilter = new HashMap<>();
263
264 @Activate
265 protected void activate() {
266 log.info("Started");
267 configService.registerProperties(getClass());
268 createAllFilters();
269 }
270
271 @Deactivate
272 protected void deactivate() {
273 configService.unregisterProperties(getClass(), false);
274 removeAllFilters();
275 log.info("Stopped");
276 }
277
278 private void checkChangeInPps(ComponentContext context) {
279 Dictionary<?, ?> properties = context.getProperties();
280 int newPpsArp, newPpsDhcp, newPpsNs, newPpsNa, newPpsDhcp6Direct;
281 int newPpsDhcp6Indirect, newPpsIcmp, newPpsIcmp6;
282 try {
283 String s = get(properties, "ppsArp");
284 newPpsArp = isNullOrEmpty(s) ? ppsArp : Integer.parseInt(s.trim());
285
286 s = get(properties, "ppsDhcp");
287 newPpsDhcp = isNullOrEmpty(s) ? ppsDhcp : Integer.parseInt(s.trim());
288
289 s = get(properties, "ppsNs");
290 newPpsNs = isNullOrEmpty(s) ? ppsNs : Integer.parseInt(s.trim());
291
292 s = get(properties, "ppsNa");
293 newPpsNa = isNullOrEmpty(s) ? ppsNa : Integer.parseInt(s.trim());
294
295 s = get(properties, "ppsDhcp6Direct");
296 newPpsDhcp6Direct = isNullOrEmpty(s) ? ppsDhcp6Direct : Integer.parseInt(s.trim());
297
298 s = get(properties, "ppsDhcp6Indirect");
299 newPpsDhcp6Indirect = isNullOrEmpty(s) ? ppsDhcp6Indirect : Integer.parseInt(s.trim());
300
301 s = get(properties, "ppsIcmp");
302 newPpsIcmp = isNullOrEmpty(s) ? ppsIcmp : Integer.parseInt(s.trim());
303
304 s = get(properties, "ppsIcmp6");
305 newPpsIcmp6 = isNullOrEmpty(s) ? ppsIcmp6 : Integer.parseInt(s.trim());
306 } catch (NumberFormatException | ClassCastException e) {
307 newPpsArp = PPS_ARP;
308 newPpsDhcp = PPS_DHCP;
309 newPpsNs = PPS_NS;
310 newPpsNa = PPS_NA;
311 newPpsDhcp6Direct = PPS_DHCP6_DIRECT;
312 newPpsDhcp6Indirect = PPS_DHCP6_INDIRECT;
313 newPpsIcmp = PPS_ICMP;
314 newPpsIcmp6 = PPS_ICMP6;
315 }
316 if (newPpsArp != ppsArp) {
317 ppsArp = newPpsArp;
318 mapCounterFilter.get(ARP_FILTER).setPps(ppsArp);
319 }
320 if (newPpsDhcp != ppsDhcp) {
321 ppsDhcp = newPpsDhcp;
322 mapCounterFilter.get(DHCP_FILTER).setPps(ppsDhcp);
323 }
324 if (newPpsNs != ppsNs) {
325 ppsNs = newPpsNs;
326 mapCounterFilter.get(NS_FILTER).setPps(ppsNs);
327 }
328 if (newPpsNa != ppsNa) {
329 ppsNa = newPpsNa;
330 mapCounterFilter.get(NA_FILTER).setPps(ppsNa);
331 }
332 if (newPpsDhcp6Direct != ppsDhcp6Direct) {
333 ppsDhcp6Direct = newPpsDhcp6Direct;
334 mapCounterFilter.get(DHCP6_DIRECT_FILTER).setPps(ppsDhcp6Direct);
335 }
336 if (newPpsDhcp6Indirect != ppsDhcp6Indirect) {
337 ppsDhcp6Indirect = newPpsDhcp6Indirect;
338 mapCounterFilter.get(DHCP6_INDIRECT_FILTER).setPps(ppsDhcp6Indirect);
339 }
340 if (newPpsIcmp != ppsIcmp) {
341 ppsIcmp = newPpsIcmp;
342 mapCounterFilter.get(ICMP_FILTER).setPps(ppsIcmp);
343 }
344 if (newPpsIcmp6 != ppsIcmp6) {
345 ppsIcmp6 = newPpsIcmp6;
346 mapCounterFilter.get(ICMP6_FILTER).setPps(ppsIcmp6);
347 }
348 }
349
350 private void checkChangeInWinSize(ComponentContext context) {
351 Dictionary<?, ?> properties = context.getProperties();
352
353 int newWinSizeArp, newWinSizeDhcp, newWinSizeNs, newWinSizeNa;
354 int newWinSizeDhcp6Direct, newWinSizeDhcp6Indirect, newWinSizeIcmp, newWinSizeIcmp6;
355 try {
356 String s = get(properties, "winSizeArp");
357 newWinSizeArp = isNullOrEmpty(s) ? winSizeArp : Integer.parseInt(s.trim());
358
359 s = get(properties, "winSizeDhcp");
360 newWinSizeDhcp = isNullOrEmpty(s) ? winSizeDhcp : Integer.parseInt(s.trim());
361
362 s = get(properties, "winSizeNs");
363 newWinSizeNs = isNullOrEmpty(s) ? winSizeNs : Integer.parseInt(s.trim());
364
365 s = get(properties, "winSizeNa");
366 newWinSizeNa = isNullOrEmpty(s) ? winSizeNa : Integer.parseInt(s.trim());
367
368 s = get(properties, "winSizeDhcp6Direct");
369 newWinSizeDhcp6Direct = isNullOrEmpty(s) ? winSizeDhcp6Direct : Integer.parseInt(s.trim());
370
371 s = get(properties, "winSizeDhcp6Indirect");
372 newWinSizeDhcp6Indirect = isNullOrEmpty(s) ? winSizeDhcp6Indirect : Integer.parseInt(s.trim());
373
374 s = get(properties, "winSizeIcmp");
375 newWinSizeIcmp = isNullOrEmpty(s) ? winSizeIcmp : Integer.parseInt(s.trim());
376
377 s = get(properties, "winSizeIcmp6");
378 newWinSizeIcmp6 = isNullOrEmpty(s) ? winSizeIcmp6 : Integer.parseInt(s.trim());
379 } catch (NumberFormatException | ClassCastException e) {
380 newWinSizeArp = WIN_SIZE_ARP_MS;
381 newWinSizeDhcp = WIN_SIZE_DHCP_MS;
382 newWinSizeNs = WIN_SIZE_NS_MS;
383 newWinSizeNa = WIN_SIZE_NA_MS;
384 newWinSizeDhcp6Direct = WIN_SIZE_DHCP6_DIRECT_MS;
385 newWinSizeDhcp6Indirect = WIN_SIZE_DHCP6_INDIRECT_MS;
386 newWinSizeIcmp = WIN_SIZE_ICMP_MS;
387 newWinSizeIcmp6 = WIN_SIZE_ICMP6_MS;
388 }
389 if (newWinSizeArp != winSizeArp) {
390 winSizeArp = newWinSizeArp;
391 mapCounterFilter.get(ARP_FILTER).setWinSize(winSizeArp);
392 }
393 if (newWinSizeDhcp != winSizeDhcp) {
394 winSizeDhcp = newWinSizeDhcp;
395 mapCounterFilter.get(DHCP_FILTER).setWinSize(winSizeDhcp);
396 }
397 if (newWinSizeNs != winSizeNs) {
398 winSizeNs = newWinSizeNs;
399 mapCounterFilter.get(NS_FILTER).setWinSize(winSizeNs);
400 }
401 if (newWinSizeNa != winSizeNa) {
402 winSizeNa = newWinSizeNa;
403 mapCounterFilter.get(NA_FILTER).setWinSize(winSizeNa);
404 }
405 if (newWinSizeDhcp6Direct != winSizeDhcp6Direct) {
406 winSizeDhcp6Direct = newWinSizeDhcp6Direct;
407 mapCounterFilter.get(DHCP6_DIRECT_FILTER).setWinSize(winSizeDhcp6Direct);
408 }
409 if (newWinSizeDhcp6Indirect != winSizeDhcp6Indirect) {
410 winSizeDhcp6Indirect = newWinSizeDhcp6Indirect;
411 mapCounterFilter.get(DHCP6_INDIRECT_FILTER).setWinSize(winSizeDhcp6Indirect);
412 }
413 if (newWinSizeIcmp != winSizeIcmp) {
414 winSizeIcmp = newWinSizeIcmp;
415 mapCounterFilter.get(ICMP_FILTER).setWinSize(winSizeIcmp);
416 }
417 if (newWinSizeIcmp6 != winSizeIcmp6) {
418 winSizeIcmp6 = newWinSizeIcmp6;
419 mapCounterFilter.get(ICMP6_FILTER).setWinSize(winSizeIcmp6);
420 }
421
422 }
423
424 private void checkChangeInGuardTime(ComponentContext context) {
425 Dictionary<?, ?> properties = context.getProperties();
426 int newGuardTimeArp, newGuardTimeDhcp, newGuardTimeNs, newGuardTimeNa;
427 int newGuardTimeDhcp6Direct, newGuardTimeDhcp6Indirect, newGuardTimeIcmp, newGuardTimeIcmp6;
428 try {
429 String s = get(properties, "guardTimeArp");
430 newGuardTimeArp = isNullOrEmpty(s) ? guardTimeArp : Integer.parseInt(s.trim());
431
432 s = get(properties, "guardTimeDhcp");
433 newGuardTimeDhcp = isNullOrEmpty(s) ? guardTimeDhcp : Integer.parseInt(s.trim());
434
435 s = get(properties, "guardTimeNs");
436 newGuardTimeNs = isNullOrEmpty(s) ? guardTimeNs : Integer.parseInt(s.trim());
437
438 s = get(properties, "guardTimeNa");
439 newGuardTimeNa = isNullOrEmpty(s) ? guardTimeNa : Integer.parseInt(s.trim());
440
441 s = get(properties, "guardTimeDhcp6Direct");
442 newGuardTimeDhcp6Direct = isNullOrEmpty(s) ? guardTimeDhcp6Direct : Integer.parseInt(s.trim());
443
444 s = get(properties, "guardTimeDhcp6Indirect");
445 newGuardTimeDhcp6Indirect = isNullOrEmpty(s) ? guardTimeDhcp6Indirect : Integer.parseInt(s.trim());
446
447 s = get(properties, "guardTimeIcmp");
448 newGuardTimeIcmp = isNullOrEmpty(s) ? guardTimeIcmp : Integer.parseInt(s.trim());
449
450 s = get(properties, "guardTimeIcmp6");
451 newGuardTimeIcmp6 = isNullOrEmpty(s) ? guardTimeIcmp6 : Integer.parseInt(s.trim());
452 } catch (NumberFormatException | ClassCastException e) {
453
454 newGuardTimeArp = GUARD_TIME_ARP_SEC;
455 newGuardTimeDhcp = GUARD_TIME_DHCP_SEC;
456 newGuardTimeNs = GUARD_TIME_NS_SEC;
457 newGuardTimeNa = GUARD_TIME_NA_SEC;
458 newGuardTimeDhcp6Direct = GUARD_TIME_DHCP6_DIRECT_SEC;
459 newGuardTimeDhcp6Indirect = GUARD_TIME_DHCP6_INDIRECT_SEC;
460 newGuardTimeIcmp = GUARD_TIME_ICMP_SEC;
461 newGuardTimeIcmp6 = GUARD_TIME_ICMP6_SEC;
462 }
463 if (newGuardTimeArp != guardTimeArp) {
464 guardTimeArp = newGuardTimeArp;
465 mapCounterFilter.get(ARP_FILTER).setGuardTime(guardTimeArp);
466 }
467 if (newGuardTimeDhcp != guardTimeDhcp) {
468 guardTimeDhcp = newGuardTimeDhcp;
469 mapCounterFilter.get(DHCP_FILTER).setGuardTime(guardTimeDhcp);
470 }
471 if (newGuardTimeNs != guardTimeNs) {
472 guardTimeNs = newGuardTimeNs;
473 mapCounterFilter.get(NS_FILTER).setGuardTime(guardTimeNs);
474 }
475 if (newGuardTimeNa != guardTimeNa) {
476 guardTimeNa = newGuardTimeNa;
477 mapCounterFilter.get(NA_FILTER).setGuardTime(guardTimeNa);
478 }
479 if (newGuardTimeDhcp6Direct != guardTimeDhcp6Direct) {
480 guardTimeDhcp6Direct = newGuardTimeDhcp6Direct;
481 mapCounterFilter.get(DHCP6_DIRECT_FILTER).setGuardTime(guardTimeDhcp6Direct);
482 }
483 if (newGuardTimeDhcp6Indirect != guardTimeDhcp6Indirect) {
484 guardTimeDhcp6Indirect = newGuardTimeDhcp6Indirect;
485 mapCounterFilter.get(DHCP6_INDIRECT_FILTER).setGuardTime(guardTimeDhcp6Indirect);
486 }
487 if (newGuardTimeIcmp != guardTimeIcmp) {
488 guardTimeIcmp = newGuardTimeIcmp;
489 mapCounterFilter.get(ICMP_FILTER).setGuardTime(guardTimeIcmp);
490 }
491 if (newGuardTimeIcmp6 != guardTimeIcmp6) {
492 guardTimeIcmp6 = newGuardTimeIcmp6;
493 mapCounterFilter.get(ICMP6_FILTER).setGuardTime(guardTimeIcmp6);
494 }
495
496
497 }
498
499 private void checkChangeInWinThres(ComponentContext context) {
500 Dictionary<?, ?> properties = context.getProperties();
501
502 int newWinThresArp, newWinThresDhcp, newWinThresNs, newWinThresNa;
503 int newWinThresDhcp6Direct, newWinThresDhcp6Indirect, newWinThresIcmp, newWinThresIcmp6;
504 try {
505
506 String s = get(properties, "winThresArp");
507 newWinThresArp = isNullOrEmpty(s) ? winThresArp : Integer.parseInt(s.trim());
508
509 s = get(properties, "winThresDhcp");
510 newWinThresDhcp = isNullOrEmpty(s) ? winThresDhcp : Integer.parseInt(s.trim());
511
512 s = get(properties, "winThresNs");
513 newWinThresNs = isNullOrEmpty(s) ? winThresNs : Integer.parseInt(s.trim());
514
515 s = get(properties, "winThresNa");
516 newWinThresNa = isNullOrEmpty(s) ? winThresNa : Integer.parseInt(s.trim());
517
518 s = get(properties, "winThresDhcp6Direct");
519 newWinThresDhcp6Direct = isNullOrEmpty(s) ? winThresDhcp6Direct : Integer.parseInt(s.trim());
520
521 s = get(properties, "winThresDhcp6Indirect");
522 newWinThresDhcp6Indirect = isNullOrEmpty(s) ? winThresDhcp6Indirect : Integer.parseInt(s.trim());
523
524 s = get(properties, "winThresIcmp");
525 newWinThresIcmp = isNullOrEmpty(s) ? winThresIcmp : Integer.parseInt(s.trim());
526
527 s = get(properties, "winThresIcmp6");
528 newWinThresIcmp6 = isNullOrEmpty(s) ? winThresIcmp6 : Integer.parseInt(s.trim());
529
530 } catch (NumberFormatException | ClassCastException e) {
531 newWinThresArp = WIN_THRES_ARP;
532 newWinThresDhcp = WIN_THRES_DHCP;
533 newWinThresNs = WIN_THRES_NS;
534 newWinThresNa = WIN_THRES_NA;
535 newWinThresDhcp6Direct = WIN_THRES_DHCP6_DIRECT;
536 newWinThresDhcp6Indirect = WIN_THRES_DHCP6_INDIRECT;
537 newWinThresIcmp = WIN_THRES_ICMP;
538 newWinThresIcmp6 = WIN_THRES_ICMP6;
539
540 }
541
542 if (newWinThresArp != winThresArp) {
543 winThresArp = newWinThresArp;
544 mapCounterFilter.get(ARP_FILTER).setWinThres(winThresArp);
545 }
546 if (newWinThresDhcp != winThresDhcp) {
547 winThresDhcp = newWinThresDhcp;
548 mapCounterFilter.get(DHCP_FILTER).setWinThres(winThresDhcp);
549 }
550 if (newWinThresNs != winThresNs) {
551 winThresNs = newWinThresNs;
552 mapCounterFilter.get(NS_FILTER).setWinThres(winThresNs);
553 }
554 if (newWinThresNa != winThresNa) {
555 winThresNa = newWinThresNa;
556 mapCounterFilter.get(NA_FILTER).setWinThres(winThresNa);
557 }
558 if (newWinThresDhcp6Direct != winThresDhcp6Direct) {
559 winThresDhcp6Direct = newWinThresDhcp6Direct;
560 mapCounterFilter.get(DHCP6_DIRECT_FILTER).setWinThres(winThresDhcp6Direct);
561 }
562 if (newWinThresDhcp6Indirect != winThresDhcp6Indirect) {
563 winThresDhcp6Indirect = newWinThresDhcp6Indirect;
564 mapCounterFilter.get(DHCP6_INDIRECT_FILTER).setWinThres(winThresDhcp6Indirect);
565 }
566 if (newWinThresIcmp != winThresIcmp) {
567 winThresIcmp = newWinThresIcmp;
568 mapCounterFilter.get(ICMP_FILTER).setWinThres(winThresIcmp);
569 }
570 if (newWinThresIcmp6 != winThresIcmp6) {
571 winThresIcmp6 = newWinThresIcmp6;
572 mapCounterFilter.get(ICMP6_FILTER).setWinThres(winThresIcmp6);
573 }
574
575 }
576
577 @Modified
578 private void modified(ComponentContext context) {
579 if (context == null) {
580 log.info("Default config");
581 return;
582 }
583
584 checkChangeInPps(context);
585 checkChangeInWinSize(context);
586 checkChangeInGuardTime(context);
587 checkChangeInWinThres(context);
588
589 log.info("Reconfigured ppsArp: {} ppsDhcp: {} ppsNs: {} ppsNa: {} " +
590 "ppsDhcp6Direct: {} ppsDhcp6Indirect: {} ppsIcmp: {} ppsIcmp6: {}",
591 ppsArp, ppsDhcp, ppsNs, ppsNa, ppsDhcp6Direct, ppsDhcp6Indirect,
592 ppsIcmp, ppsIcmp6);
593
594 log.info("Reconfigured winSizeArp: {} winSizeDhcp: {} winSizeNs: {} winSizeNa: {} " +
595 "winSizeDhcp6Direct: {} winSizeDhcp6Indirect: {} winSizeIcmp: {} winSizeIcmp6: {}",
596 winSizeArp, winSizeDhcp, winSizeNs, winSizeNa, winSizeDhcp6Direct,
597 winSizeDhcp6Indirect, winSizeIcmp, winSizeIcmp6);
598
599 log.info("Reconfigured guardTimeArp: {} guardTimeDhcp: {} guardTimeNs: {} guardTimeNa: {} " +
600 "guardTimeDhcp6Direct: {} guardTimeDhcp6Indirect: {} guardTimeIcmp: {} guardTimeIcmp6: {}",
601 guardTimeArp, guardTimeDhcp, guardTimeNs, guardTimeNa, guardTimeDhcp6Direct,
602 guardTimeDhcp6Indirect, guardTimeIcmp, guardTimeIcmp6);
603
604 log.info("Reconfigured winThresArp: {} winThresDhcp: {} winThresNs: {} winThresNa: {} " +
605 "winThresDhcp6Direct: {} winThresDhcp6Indirect: {} winThresIcmp: {} winThresIcmp6: {}",
606 winThresArp, winThresDhcp, winThresNs, winThresNa, winThresDhcp6Direct,
607 winThresDhcp6Indirect, winThresIcmp, winThresIcmp6);
608 }
609
610 /**
611 * Create all required filters.
612 */
613 private void createAllFilters() {
614 DefaultPacketInFilter filter;
615 ArpPacketClassifier arp = new ArpPacketClassifier();
616 filter = new DefaultPacketInFilter(ppsArp, winSizeArp, guardTimeArp, winThresArp, ARP_FILTER, arp);
617 packetService.addFilter(filter);
618 mapCounterFilter.put(filter.name(), filter);
619 DhcpPacketClassifier dhcp4 = new DhcpPacketClassifier();
620 filter = new DefaultPacketInFilter(ppsDhcp, winSizeDhcp, guardTimeDhcp, winThresDhcp, DHCP_FILTER, dhcp4);
621 packetService.addFilter(filter);
622 mapCounterFilter.put(filter.name(), filter);
623 Dhcp6DirectPacketClassifier dhcp6Direct = new Dhcp6DirectPacketClassifier();
624 filter = new DefaultPacketInFilter(ppsDhcp6Direct, winSizeDhcp6Direct, guardTimeDhcp6Direct,
625 winThresDhcp6Direct, DHCP6_DIRECT_FILTER, dhcp6Direct);
626 packetService.addFilter(filter);
627 mapCounterFilter.put(filter.name(), filter);
628 Dhcp6IndirectPacketClassifier dhcp6Indirect = new Dhcp6IndirectPacketClassifier();
629 filter = new DefaultPacketInFilter(ppsDhcp6Direct, winSizeDhcp6Direct, guardTimeDhcp6Direct,
630 winThresDhcp6Direct, DHCP6_INDIRECT_FILTER, dhcp6Indirect);
631 packetService.addFilter(filter);
632 mapCounterFilter.put(filter.name(), filter);
633 NAPacketClassifier na = new NAPacketClassifier();
634 filter = new DefaultPacketInFilter(ppsNa, winSizeNa, guardTimeNa, winThresNa, NA_FILTER, na);
635 packetService.addFilter(filter);
636 mapCounterFilter.put(filter.name(), filter);
637 NSPacketClassifier ns = new NSPacketClassifier();
638 filter = new DefaultPacketInFilter(ppsNs, winSizeNs, guardTimeNs, winThresNs, NS_FILTER, ns);
639 packetService.addFilter(filter);
640 mapCounterFilter.put(filter.name(), filter);
641 IcmpPacketClassifier icmp = new IcmpPacketClassifier();
642 filter = new DefaultPacketInFilter(ppsIcmp, winSizeIcmp, guardTimeIcmp, winThresIcmp, ICMP_FILTER, icmp);
643 packetService.addFilter(filter);
644 mapCounterFilter.put(filter.name(), filter);
645 Icmp6PacketClassifier icmp6 = new Icmp6PacketClassifier();
646 filter = new DefaultPacketInFilter(ppsIcmp6, winSizeIcmp6, guardTimeIcmp6, winThresIcmp6, ICMP6_FILTER, icmp6);
647 packetService.addFilter(filter);
648 mapCounterFilter.put(filter.name(), filter);
649 }
650
651 /**
652 * Delete all the filters.
653 */
654 private void removeAllFilters() {
655 packetService.clearFilters();
656 mapCounterFilter.clear();
657 }
658
659 @Override
660 public Map<String, PacketInFilter> filterMap() {
661 return ImmutableMap.copyOf(mapCounterFilter);
662 }
663
664
665
666}