2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11 import java.nio.ByteBuffer;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.Deque;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Map.Entry;
20 import java.util.Timer;
21 import java.util.TimerTask;
22 import java.util.concurrent.BlockingQueue;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25 import java.util.concurrent.CopyOnWriteArraySet;
26 import java.util.concurrent.LinkedBlockingDeque;
27 import java.util.concurrent.LinkedBlockingQueue;
29 import org.eclipse.osgi.framework.console.CommandInterpreter;
30 import org.eclipse.osgi.framework.console.CommandProvider;
31 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
32 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsListener;
33 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsManager;
34 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
35 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
36 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match;
37 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsReply;
38 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsRequest;
39 import org.opendaylight.controller.sal.core.Node;
40 import org.opendaylight.controller.sal.core.NodeConnector;
41 import org.opendaylight.controller.sal.core.Property;
42 import org.opendaylight.controller.sal.core.UpdateType;
43 import org.opendaylight.controller.sal.utils.HexEncode;
44 import org.openflow.protocol.OFError;
45 import org.openflow.protocol.OFMatch;
46 import org.openflow.protocol.OFPort;
47 import org.openflow.protocol.OFStatisticsRequest;
48 import org.openflow.protocol.statistics.OFAggregateStatisticsRequest;
49 import org.openflow.protocol.statistics.OFFlowStatisticsReply;
50 import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
51 import org.openflow.protocol.statistics.OFPortStatisticsReply;
52 import org.openflow.protocol.statistics.OFPortStatisticsRequest;
53 import org.openflow.protocol.statistics.OFQueueStatisticsRequest;
54 import org.openflow.protocol.statistics.OFStatistics;
55 import org.openflow.protocol.statistics.OFStatisticsType;
56 import org.openflow.protocol.statistics.OFTableStatistics;
57 import org.openflow.protocol.statistics.OFVendorStatistics;
58 import org.openflow.util.HexString;
59 import org.osgi.framework.BundleContext;
60 import org.osgi.framework.FrameworkUtil;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 * It periodically polls the different OF statistics from the OF switches and
66 * caches them for quick retrieval for the above layers' modules It also
67 * provides an API to directly query the switch about the statistics
69 public class OFStatisticsManager implements IOFStatisticsManager,
70 IInventoryShimExternalListener, CommandProvider {
71 private static final Logger log = LoggerFactory.getLogger(OFStatisticsManager.class);
72 private static final int INITIAL_SIZE = 64;
73 private static final long FLOW_STATS_PERIOD = 10000;
74 private static final long DESC_STATS_PERIOD = 60000;
75 private static final long PORT_STATS_PERIOD = 5000;
76 private static final long TABLE_STATS_PERIOD = 10000;
77 private static final long TICK = 1000;
78 private static short statisticsTickNumber = (short) (FLOW_STATS_PERIOD / TICK);
79 private static short descriptionTickNumber = (short) (DESC_STATS_PERIOD / TICK);
80 private static short portTickNumber = (short) (PORT_STATS_PERIOD / TICK);
81 private static short tableTickNumber = (short) (TABLE_STATS_PERIOD / TICK);
82 private static short factoredSamples = (short) 2;
83 private static short counter = 1;
84 private IController controller = null;
85 private ConcurrentMap<Long, List<OFStatistics>> flowStatistics;
86 private ConcurrentMap<Long, List<OFStatistics>> descStatistics;
87 private ConcurrentMap<Long, List<OFStatistics>> portStatistics;
88 private ConcurrentMap<Long, List<OFStatistics>> tableStatistics;
89 private List<OFStatistics> dummyList;
90 private ConcurrentMap<Long, StatisticsTicks> statisticsTimerTicks;
91 protected BlockingQueue<StatsRequest> pendingStatsRequests;
92 protected BlockingQueue<Long> switchPortStatsUpdated;
93 private Thread statisticsCollector;
94 private Thread txRatesUpdater;
95 private Timer statisticsTimer;
96 private TimerTask statisticsTimerTask;
97 private ConcurrentMap<Long, Boolean> switchSupportsVendorExtStats;
98 // Per port sampled (every portStatsPeriod) transmit rate
99 private Map<Long, Map<Short, TxRates>> txRates;
100 private Set<IOFStatisticsListener> statisticsListeners;
103 * The object containing the latest factoredSamples tx rate samples for a
106 protected class TxRates {
107 // contains the latest factoredSamples sampled transmitted bytes
108 Deque<Long> sampledTxBytes;
111 sampledTxBytes = new LinkedBlockingDeque<Long>();
114 public void update(Long txBytes) {
116 * Based on how many samples our average works on, we might have to
117 * remove the oldest sample
119 if (sampledTxBytes.size() == factoredSamples) {
120 sampledTxBytes.removeLast();
123 // Add the latest sample to the top of the queue
124 sampledTxBytes.addFirst(txBytes);
128 * Returns the average transmit rate in bps
130 * @return the average transmit rate [bps]
132 public long getAverageTxRate() {
135 * If we cannot provide the value for the time window length set
137 if (sampledTxBytes.size() < factoredSamples) {
140 long increment = sampledTxBytes.getFirst() - sampledTxBytes
142 long timePeriod = factoredSamples * PORT_STATS_PERIOD / TICK;
143 average = (8L * increment) / timePeriod;
148 public void setController(IController core) {
149 this.controller = core;
152 public void unsetController(IController core) {
153 if (this.controller == core) {
154 this.controller = null;
159 * Function called by the dependency manager when all the required
160 * dependencies are satisfied
164 flowStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
165 descStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
166 portStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
167 tableStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
168 dummyList = new ArrayList<OFStatistics>(1);
169 statisticsTimerTicks = new ConcurrentHashMap<Long, StatisticsTicks>(INITIAL_SIZE);
170 pendingStatsRequests = new LinkedBlockingQueue<StatsRequest>(INITIAL_SIZE);
171 switchPortStatsUpdated = new LinkedBlockingQueue<Long>(INITIAL_SIZE);
172 switchSupportsVendorExtStats = new ConcurrentHashMap<Long, Boolean>(INITIAL_SIZE);
173 txRates = new HashMap<Long, Map<Short, TxRates>>(INITIAL_SIZE);
174 statisticsListeners = new CopyOnWriteArraySet<IOFStatisticsListener>();
176 configStatsPollIntervals();
178 // Initialize managed timers
179 statisticsTimer = new Timer();
180 statisticsTimerTask = new TimerTask() {
187 // Initialize Statistics collector thread
188 statisticsCollector = new Thread(new Runnable() {
193 StatsRequest req = pendingStatsRequests.take();
194 queryStatisticsInternal(req.switchId, req.type);
195 } catch (InterruptedException e) {
196 log.warn("Flow Statistics Collector thread "
201 }, "Statistics Collector");
203 // Initialize Tx Rate Updater thread
204 txRatesUpdater = new Thread(new Runnable() {
209 long switchId = switchPortStatsUpdated.take();
210 updatePortsTxRate(switchId);
211 } catch (InterruptedException e) {
212 log.warn("TX Rate Updater thread interrupted", e);
216 }, "TX Rate Updater");
220 * Function called by the dependency manager when at least one dependency
221 * become unsatisfied or when the component is shutting down because for
222 * example bundle is being stopped.
229 * Function called by dependency manager after "init ()" is called and after
230 * the services provided by the class are registered in the service registry
234 // Start managed timers
235 statisticsTimer.scheduleAtFixedRate(statisticsTimerTask, 0, TICK);
237 // Start statistics collector thread
238 statisticsCollector.start();
240 // Start bandwidth utilization computer thread
241 txRatesUpdater.start();
244 registerWithOSGIConsole();
248 * Function called by the dependency manager before the services exported by
249 * the component are unregistered, this will be followed by a "destroy ()"
254 // Stop managed timers
255 statisticsTimer.cancel();
258 public void setStatisticsListener(IOFStatisticsListener s) {
259 this.statisticsListeners.add(s);
262 public void unsetStatisticsListener(IOFStatisticsListener s) {
264 this.statisticsListeners.remove(s);
268 private void registerWithOSGIConsole() {
269 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
270 bundleContext.registerService(CommandProvider.class.getName(), this, null);
273 private static class StatsRequest {
274 protected Long switchId;
275 protected OFStatisticsType type;
277 public StatsRequest(Long d, OFStatisticsType t) {
283 public String toString() {
284 return "SReq = {switchId=" + switchId + ", type=" + type + "}";
288 public int hashCode() {
289 final int prime = 31;
291 result = prime * result
292 + ((switchId == null) ? 0 : switchId.hashCode());
293 result = prime * result + ((type == null) ? 0 : type.ordinal());
298 public boolean equals(Object obj) {
305 if (getClass() != obj.getClass()) {
308 StatsRequest other = (StatsRequest) obj;
309 if (switchId == null) {
310 if (other.switchId != null) {
313 } else if (!switchId.equals(other.switchId)) {
316 if (type != other.type) {
323 private void addStatisticsTicks(Long switchId) {
324 switchSupportsVendorExtStats.put(switchId, Boolean.TRUE); // Assume
330 statisticsTimerTicks.put(switchId, new StatisticsTicks(true));
331 log.debug("Added Switch {} to target pool",
332 HexString.toHexString(switchId.longValue()));
335 protected static class StatisticsTicks {
336 private short flowStatisticsTicks;
337 private short descriptionTicks;
338 private short portStatisticsTicks;
339 private short tableStatisticsTicks;
341 public StatisticsTicks(boolean scattered) {
343 // scatter bursts by statisticsTickPeriod
346 } // being paranoid here
347 flowStatisticsTicks = (short) (1 + counter
348 % statisticsTickNumber);
349 descriptionTicks = (short) (1 + counter % descriptionTickNumber);
350 portStatisticsTicks = (short) (1 + counter % portTickNumber);
351 tableStatisticsTicks = (short) (1 + counter % tableTickNumber);
353 flowStatisticsTicks = statisticsTickNumber;
354 descriptionTicks = descriptionTickNumber;
355 portStatisticsTicks = portTickNumber;
356 tableStatisticsTicks = tableTickNumber;
360 public boolean decrementFlowTicksIsZero() {
361 // Please ensure no code is inserted between the if check and the
362 // flowStatisticsTicks reset
363 if (--flowStatisticsTicks == 0) {
364 flowStatisticsTicks = statisticsTickNumber;
370 public boolean decrementDescTicksIsZero() {
371 // Please ensure no code is inserted between the if check and the
372 // descriptionTicks reset
373 if (--descriptionTicks == 0) {
374 descriptionTicks = descriptionTickNumber;
380 public boolean decrementPortTicksIsZero() {
381 // Please ensure no code is inserted between the if check and the
382 // descriptionTicks reset
383 if (--portStatisticsTicks == 0) {
384 portStatisticsTicks = portTickNumber;
390 public boolean decrementTableTicksIsZero() {
391 // Please ensure no code is inserted between the if check and the
392 // descriptionTicks reset
393 if(--tableStatisticsTicks == 0) {
394 tableStatisticsTicks = tableTickNumber;
401 public String toString() {
402 return "{fT=" + flowStatisticsTicks + ",dT=" + descriptionTicks
403 + ",pT=" + portStatisticsTicks + ",tT=" + tableStatisticsTicks + "}";
407 private void printInfoMessage(String type, StatsRequest request) {
408 log.info("{} stats request not inserted for switch: {}. Queue size: {}. Collector state: {}.",
409 new Object[] {type, HexString.toHexString(request.switchId), pendingStatsRequests.size(),
410 statisticsCollector.getState().toString() });
413 protected void decrementTicks() {
414 StatsRequest request = null;
415 for (Map.Entry<Long, StatisticsTicks> entry : statisticsTimerTicks
417 StatisticsTicks clock = entry.getValue();
418 Long switchId = entry.getKey();
419 if (clock.decrementFlowTicksIsZero()) {
420 request = (switchSupportsVendorExtStats.get(switchId) == Boolean.TRUE) ?
421 new StatsRequest(switchId, OFStatisticsType.VENDOR) :
422 new StatsRequest(switchId, OFStatisticsType.FLOW);
423 // If a request for this switch is already in the queue, skip to
424 // add this new request
425 if (!pendingStatsRequests.contains(request)
426 && false == pendingStatsRequests.offer(request)) {
427 printInfoMessage("Flow", request);
431 if (clock.decrementDescTicksIsZero()) {
432 request = new StatsRequest(switchId, OFStatisticsType.DESC);
433 // If a request for this switch is already in the queue, skip to
434 // add this new request
435 if (!pendingStatsRequests.contains(request)
436 && false == pendingStatsRequests.offer(request)) {
437 printInfoMessage("Description", request);
441 if (clock.decrementPortTicksIsZero()) {
442 request = new StatsRequest(switchId, OFStatisticsType.PORT);
443 // If a request for this switch is already in the queue, skip to
444 // add this new request
445 if (!pendingStatsRequests.contains(request)
446 && false == pendingStatsRequests.offer(request)) {
447 printInfoMessage("Port", request);
451 if(clock.decrementTableTicksIsZero()) {
452 request = new StatsRequest(switchId, OFStatisticsType.TABLE);
453 // If a request for this switch is already in the queue, skip to
454 // add this new request
455 if (!pendingStatsRequests.contains(request)
456 && false == pendingStatsRequests.offer(request)) {
457 printInfoMessage("Table", request);
463 private void removeStatsRequestTasks(Long switchId) {
464 log.debug("Cleaning Statistics database for switch {}",
465 HexEncode.longToHexString(switchId));
466 // To be safe, let's attempt removal of both VENDOR and FLOW request. It
468 pendingStatsRequests.remove(new StatsRequest(switchId,
469 OFStatisticsType.VENDOR));
470 pendingStatsRequests.remove(new StatsRequest(switchId,
471 OFStatisticsType.FLOW));
472 pendingStatsRequests.remove(new StatsRequest(switchId,
473 OFStatisticsType.DESC));
474 pendingStatsRequests.remove(new StatsRequest(switchId,
475 OFStatisticsType.PORT));
476 pendingStatsRequests.remove(new StatsRequest(switchId,
477 OFStatisticsType.TABLE));
478 // Take care of the TX rate databases
479 switchPortStatsUpdated.remove(switchId);
480 txRates.remove(switchId);
483 private void clearFlowStatsAndTicks(Long switchId) {
484 statisticsTimerTicks.remove(switchId);
485 removeStatsRequestTasks(switchId);
486 flowStatistics.remove(switchId);
487 log.debug("Statistics removed for switch {}",
488 HexString.toHexString(switchId));
491 private void queryStatisticsInternal(Long switchId, OFStatisticsType statType) {
493 // Query the switch on all matches
494 List<OFStatistics> values = this.fetchStatisticsFromSwitch(switchId, statType, null);
496 // If got a valid response update local cache and notify listeners
497 if (values != null && !values.isEmpty()) {
501 flowStatistics.put(switchId, values);
502 notifyFlowUpdate(switchId, values);
506 descStatistics.put(switchId, values);
507 // Notify who may be interested in a description change
508 notifyDescriptionUpdate(switchId, values);
511 // Overwrite cache with new port statistics for this switch
512 portStatistics.put(switchId, values);
514 // Wake up the thread which maintains the TX byte counters for
516 switchPortStatsUpdated.offer(switchId);
517 notifyPortUpdate(switchId, values);
521 tableStatistics.put(switchId, values);
522 notifyTableUpdate(switchId, values);
529 private void notifyDescriptionUpdate(Long switchId, List<OFStatistics> values) {
530 for (IOFStatisticsListener l : this.statisticsListeners) {
531 l.descriptionStatisticsRefreshed(switchId, values);
535 private void notifyFlowUpdate(Long switchId, List<OFStatistics> values) {
536 if (values.get(0) instanceof OFVendorStatistics) {
537 values = this.v6StatsListToOFStatsList(values);
540 if (!values.isEmpty()) { //possiblly filtered out by v6StatsListToOFStatsList()
541 for (IOFStatisticsListener l : this.statisticsListeners) {
542 l.flowStatisticsRefreshed(switchId, values);
547 private void notifyPortUpdate(Long switchId, List<OFStatistics> values) {
548 for (IOFStatisticsListener l : this.statisticsListeners) {
549 l.portStatisticsRefreshed(switchId, values);
553 private void notifyTableUpdate(Long switchId, List<OFStatistics> values) {
554 for (IOFStatisticsListener l : this.statisticsListeners) {
555 l.tableStatisticsRefreshed(switchId, values);
560 * Generic function to get the statistics form an OF switch
562 @SuppressWarnings("unchecked")
563 private List<OFStatistics> fetchStatisticsFromSwitch(Long switchId,
564 OFStatisticsType statsType, Object target) {
565 List<OFStatistics> values = null;
567 ISwitch sw = controller.getSwitch(switchId);
570 OFStatisticsRequest req = new OFStatisticsRequest();
571 req.setStatisticType(statsType);
572 int requestLength = req.getLengthU();
574 if (statsType == OFStatisticsType.FLOW) {
575 OFMatch match = null;
576 if (target == null) {
578 match = new OFMatch();
579 match.setWildcards(0xffffffff);
580 } else if (!(target instanceof OFMatch)) {
582 log.warn("Invalid target type for Flow stats request: {}",
586 // Specific flow request
587 match = (OFMatch) target;
589 OFFlowStatisticsRequest specificReq = new OFFlowStatisticsRequest();
590 specificReq.setMatch(match);
591 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
592 specificReq.setTableId((byte) 0xff);
593 req.setStatistics(Collections
594 .singletonList((OFStatistics) specificReq));
595 requestLength += specificReq.getLength();
597 } else if (statsType == OFStatisticsType.VENDOR) {
598 V6StatsRequest specificReq = new V6StatsRequest();
599 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
600 specificReq.setTableId((byte) 0xff);
601 req.setStatistics(Collections
602 .singletonList((OFStatistics) specificReq));
603 requestLength += specificReq.getLength();
605 } else if (statsType == OFStatisticsType.AGGREGATE) {
606 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
607 OFMatch match = new OFMatch();
608 match.setWildcards(0xffffffff);
609 specificReq.setMatch(match);
610 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
611 specificReq.setTableId((byte) 0xff);
612 req.setStatistics(Collections
613 .singletonList((OFStatistics) specificReq));
614 requestLength += specificReq.getLength();
616 } else if (statsType == OFStatisticsType.PORT) {
618 if (target == null) {
620 targetPort = OFPort.OFPP_NONE.getValue();
621 } else if (!(target instanceof Short)) {
623 log.warn("Invalid target type for Port stats request: {}",
627 // Specific port request
628 targetPort = (Short) target;
630 OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest();
631 specificReq.setPortNumber(targetPort);
632 req.setStatistics(Collections
633 .singletonList((OFStatistics) specificReq));
634 requestLength += specificReq.getLength();
636 } else if (statsType == OFStatisticsType.QUEUE) {
637 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
638 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
639 specificReq.setQueueId(0xffffffff);
640 req.setStatistics(Collections
641 .singletonList((OFStatistics) specificReq));
642 requestLength += specificReq.getLength();
644 } else if (statsType == OFStatisticsType.DESC) {
646 } else if (statsType == OFStatisticsType.TABLE) {
648 if (!(target instanceof Byte)) {
650 log.warn("Invalid table id for table stats request: {}",
654 byte targetTable = (Byte) target;
655 OFTableStatistics specificReq = new OFTableStatistics();
656 specificReq.setTableId(targetTable);
657 req.setStatistics(Collections
658 .singletonList((OFStatistics) specificReq));
659 requestLength += specificReq.getLength();
663 req.setLengthU(requestLength);
664 Object result = sw.getStatistics(req);
666 if (result == null) {
667 log.warn("Request Timed Out for ({}) from switch {}", type,
668 HexString.toHexString(switchId));
669 } else if (result instanceof OFError) {
670 log.warn("Switch {} failed to handle ({}) stats request: {}",
671 new Object[] { HexString.toHexString(switchId), type,
672 Utils.getOFErrorString((OFError) result) });
673 if (this.switchSupportsVendorExtStats.get(switchId) == Boolean.TRUE) {
675 "Switching back to regular Flow stats requests for switch {}",
676 HexString.toHexString(switchId));
677 this.switchSupportsVendorExtStats.put(switchId,
681 values = (List<OFStatistics>) result;
688 public List<OFStatistics> getOFFlowStatistics(Long switchId) {
689 List<OFStatistics> list = flowStatistics.get(switchId);
692 * Check on emptiness as interference between add and get is still
693 * possible on the inner list (the concurrentMap entry's value)
695 return (list == null || list.isEmpty()) ? this.dummyList
696 : (list.get(0) instanceof OFVendorStatistics) ? this
697 .v6StatsListToOFStatsList(list) : list;
701 public List<OFStatistics> getOFFlowStatistics(Long switchId, OFMatch ofMatch, short priority) {
702 List<OFStatistics> statsList = flowStatistics.get(switchId);
705 * Check on emptiness as interference between add and get is still
706 * possible on the inner list (the concurrentMap entry's value)
708 if (statsList == null || statsList.isEmpty()) {
709 return this.dummyList;
712 if (statsList.get(0) instanceof OFVendorStatistics) {
714 * Caller could provide regular OF match when we instead pull the
715 * vendor statistics from this node Caller is not supposed to know
716 * whether this switch supports vendor extensions statistics
719 V6Match targetMatch = (ofMatch instanceof V6Match) ? (V6Match) ofMatch
720 : new V6Match(ofMatch);
722 List<OFStatistics> targetList = v6StatsListToOFStatsList(statsList);
723 for (OFStatistics stats : targetList) {
724 V6StatsReply v6Stats = (V6StatsReply) stats;
725 V6Match v6Match = v6Stats.getMatch();
726 if (v6Stats.getPriority() == priority && v6Match.equals(targetMatch)) {
727 List<OFStatistics> list = new ArrayList<OFStatistics>();
733 for (OFStatistics stats : statsList) {
734 OFFlowStatisticsReply flowStats = (OFFlowStatisticsReply) stats;
735 if (flowStats.getPriority() == priority && flowStats.getMatch().equals(ofMatch)) {
736 List<OFStatistics> list = new ArrayList<OFStatistics>();
742 return this.dummyList;
746 * Converts the v6 vendor statistics to the OFStatistics
748 private List<OFStatistics> v6StatsListToOFStatsList(
749 List<OFStatistics> statistics) {
750 List<OFStatistics> v6statistics = new ArrayList<OFStatistics>();
751 if (statistics != null && !statistics.isEmpty()) {
752 for (OFStatistics stats : statistics) {
753 if (stats instanceof OFVendorStatistics) {
754 List<OFStatistics> r = getV6ReplyStatistics((OFVendorStatistics) stats);
756 v6statistics.addAll(r);
764 private static List<OFStatistics> getV6ReplyStatistics(
765 OFVendorStatistics stat) {
766 int length = stat.getLength();
767 List<OFStatistics> results = new ArrayList<OFStatistics>();
769 return null; // Nicira Hdr is 12 bytes. We need atleast that much
770 ByteBuffer data = ByteBuffer.allocate(length);
773 if (log.isTraceEnabled()) {
774 log.trace("getV6ReplyStatistics: Buffer BYTES ARE {}",
775 HexString.toHexString(data.array()));
778 int vendor = data.getInt(); // first 4 bytes is vendor id.
779 if (vendor != V6StatsRequest.NICIRA_VENDOR_ID) {
780 log.warn("Unexpected vendor id: 0x{}", Integer.toHexString(vendor));
783 // go ahead by 8 bytes which is 8 bytes of 0
784 data.getLong(); // should be all 0's
785 length -= 12; // 4 bytes Nicira Hdr + 8 bytes from above line have
789 V6StatsReply v6statsreply;
792 v6statsreply = new V6StatsReply();
793 min_len = v6statsreply.getLength();
794 if (length < v6statsreply.getLength())
796 v6statsreply.setActionFactory(stat.getActionFactory());
797 v6statsreply.readFrom(data);
798 if (v6statsreply.getLength() < min_len)
800 v6statsreply.setVendorId(vendor);
801 log.trace("V6StatsReply: {}", v6statsreply);
802 length -= v6statsreply.getLength();
803 results.add(v6statsreply);
809 public List<OFStatistics> queryStatistics(Long switchId,
810 OFStatisticsType statType, Object target) {
812 * Caller does not know and it is not supposed to know whether this
813 * switch supports vendor extension. We adjust the target for him
815 if (statType == OFStatisticsType.FLOW) {
816 if (switchSupportsVendorExtStats.get(switchId) == Boolean.TRUE) {
817 statType = OFStatisticsType.VENDOR;
821 List<OFStatistics> list = this.fetchStatisticsFromSwitch(switchId, statType,
824 return (list == null) ? null :
825 (statType == OFStatisticsType.VENDOR) ? v6StatsListToOFStatsList(list) : list;
829 public List<OFStatistics> getOFDescStatistics(Long switchId) {
830 if (!descStatistics.containsKey(switchId))
831 return this.dummyList;
833 return descStatistics.get(switchId);
837 public List<OFStatistics> getOFPortStatistics(Long switchId) {
838 if (!portStatistics.containsKey(switchId)) {
839 return this.dummyList;
842 return portStatistics.get(switchId);
846 public List<OFStatistics> getOFPortStatistics(Long switchId, short portId) {
847 if (!portStatistics.containsKey(switchId)) {
848 return this.dummyList;
850 List<OFStatistics> list = new ArrayList<OFStatistics>(1);
851 for (OFStatistics stats : portStatistics.get(switchId)) {
852 if (((OFPortStatisticsReply) stats).getPortNumber() == portId) {
861 public List<OFStatistics> getOFTableStatistics(Long switchId) {
862 if (!tableStatistics.containsKey(switchId)) {
863 return this.dummyList;
866 return tableStatistics.get(switchId);
870 public List<OFStatistics> getOFTableStatistics(Long switchId, Byte tableId) {
871 if (!tableStatistics.containsKey(switchId)) {
872 return this.dummyList;
875 List<OFStatistics> list = new ArrayList<OFStatistics>(1);
876 for (OFStatistics stats : tableStatistics.get(switchId)) {
877 if (((OFTableStatistics) stats).getTableId() == tableId) {
886 public int getFlowsNumber(long switchId) {
887 return this.flowStatistics.get(switchId).size();
891 * InventoryShim replay for us all the switch addition which happened before
895 public void updateNode(Node node, UpdateType type, Set<Property> props) {
896 Long switchId = (Long) node.getID();
899 addStatisticsTicks(switchId);
902 clearFlowStatsAndTicks(switchId);
908 public void updateNodeConnector(NodeConnector nodeConnector,
909 UpdateType type, Set<Property> props) {
914 * Update the cached port rates for this switch with the latest retrieved
915 * port transmit byte count
919 private synchronized void updatePortsTxRate(long switchId) {
920 List<OFStatistics> newPortStatistics = this.portStatistics.get(switchId);
921 if (newPortStatistics == null) {
924 Map<Short, TxRates> rates = this.txRates.get(switchId);
926 // First time rates for this switch are added
927 rates = new HashMap<Short, TxRates>();
928 txRates.put(switchId, rates);
930 for (OFStatistics stats : newPortStatistics) {
931 OFPortStatisticsReply newPortStat = (OFPortStatisticsReply) stats;
932 short port = newPortStat.getPortNumber();
933 TxRates portRatesHolder = rates.get(port);
934 if (portRatesHolder == null) {
935 // First time rates for this port are added
936 portRatesHolder = new TxRates();
937 rates.put(port, portRatesHolder);
939 // Get and store the number of transmitted bytes for this port
940 // And handle the case where agent does not support the counter
941 long transmitBytes = newPortStat.getTransmitBytes();
942 long value = (transmitBytes < 0) ? 0 : transmitBytes;
943 portRatesHolder.update(value);
948 public synchronized long getTransmitRate(Long switchId, Short port) {
950 if (switchId == null || port == null) {
953 Map<Short, TxRates> perSwitch = txRates.get(switchId);
954 if (perSwitch == null) {
957 TxRates portRates = perSwitch.get(port);
958 if (portRates == null) {
961 return portRates.getAverageTxRate();
965 * Manual switch name configuration code
968 public String getHelp() {
969 StringBuffer help = new StringBuffer();
970 help.append("---OF Statistics Manager utilities---\n");
971 help.append("\t ofdumpstatsmgr - "
972 + "Print Internal Stats Mgr db\n");
973 help.append("\t ofstatsmgrintervals <fP> <pP> <dP> <tP> (all in seconds) - "
974 + "Set/Show flow/port/dedscription stats poll intervals\n");
975 return help.toString();
978 private boolean isValidSwitchId(String switchId) {
979 String regexDatapathID = "^([0-9a-fA-F]{1,2}[:-]){7}[0-9a-fA-F]{1,2}$";
980 String regexDatapathIDLong = "^[0-9a-fA-F]{1,16}$";
982 return (switchId != null && (switchId.matches(regexDatapathID) || switchId
983 .matches(regexDatapathIDLong)));
986 public long getSwitchIDLong(String switchId) {
988 String switchString = "0";
990 if (isValidSwitchId(switchId)) {
991 if (switchId.contains(":")) {
992 // Handle the 00:00:AA:BB:CC:DD:EE:FF notation
993 switchString = switchId.replace(":", "");
994 } else if (switchId.contains("-")) {
995 // Handle the 00-00-AA-BB-CC-DD-EE-FF notation
996 switchString = switchId.replace("-", "");
998 // Handle the 0123456789ABCDEF notation
999 switchString = switchId;
1002 return Long.parseLong(switchString, radix);
1006 * Internal information dump code
1008 private String prettyPrintSwitchMap(ConcurrentMap<Long, StatisticsTicks> map) {
1009 StringBuffer buffer = new StringBuffer();
1011 for (Entry<Long, StatisticsTicks> entry : map.entrySet()) {
1012 buffer.append(HexString.toHexString(entry.getKey()) + "="
1013 + entry.getValue().toString() + " ");
1016 return buffer.toString();
1019 public void _ofdumpstatsmgr(CommandInterpreter ci) {
1020 ci.println("Global Counter: " + counter);
1021 ci.println("Timer Ticks: " + prettyPrintSwitchMap(statisticsTimerTicks));
1022 ci.println("PendingStatsQueue: " + pendingStatsRequests);
1023 ci.println("PendingStatsQueue size: " + pendingStatsRequests.size());
1024 ci.println("Stats Collector alive: " + statisticsCollector.isAlive());
1025 ci.println("Stats Collector State: "
1026 + statisticsCollector.getState().toString());
1027 ci.println("StatsTimer: " + statisticsTimer.toString());
1028 ci.println("Flow Stats Period: " + statisticsTickNumber + " s");
1029 ci.println("Desc Stats Period: " + descriptionTickNumber + " s");
1030 ci.println("Port Stats Period: " + portTickNumber + " s");
1031 ci.println("Table Stats Period: " + tableTickNumber + " s");
1034 public void _resetSwitchCapability(CommandInterpreter ci) {
1035 String sidString = ci.nextArgument();
1037 if (sidString == null) {
1038 ci.println("Insert the switch id (numeric value)");
1042 sid = Long.valueOf(sidString);
1043 this.switchSupportsVendorExtStats.put(sid, Boolean.TRUE);
1044 ci.println("Vendor capability for switch " + sid + " set to "
1045 + this.switchSupportsVendorExtStats.get(sid));
1046 } catch (NumberFormatException e) {
1047 ci.println("Invalid switch id. Has to be numeric.");
1052 public void _ofbw(CommandInterpreter ci) {
1053 String sidString = ci.nextArgument();
1055 if (sidString == null) {
1056 ci.println("Insert the switch id (numeric value)");
1060 sid = Long.valueOf(sidString);
1061 } catch (NumberFormatException e) {
1062 ci.println("Invalid switch id. Has to be numeric.");
1065 Map<Short, TxRates> thisSwitchRates = txRates.get(sid);
1066 ci.println("Bandwidth utilization (" + factoredSamples
1067 * portTickNumber + " sec average) for switch "
1068 + HexEncode.longToHexString(sid) + ":");
1069 if (thisSwitchRates == null) {
1070 ci.println("Not available");
1072 for (Entry<Short, TxRates> entry : thisSwitchRates.entrySet()) {
1073 ci.println("Port: " + entry.getKey() + ": "
1074 + entry.getValue().getAverageTxRate() + " bps");
1080 public void _txratewindow(CommandInterpreter ci) {
1081 String averageWindow = ci.nextArgument();
1083 if (averageWindow == null) {
1084 ci.println("Insert the length in seconds of the median "
1085 + "window for tx rate");
1086 ci.println("Current: " + factoredSamples * portTickNumber + " secs");
1090 seconds = Short.valueOf(averageWindow);
1091 } catch (NumberFormatException e) {
1092 ci.println("Invalid period.");
1094 OFStatisticsManager.factoredSamples = (short) (seconds / portTickNumber);
1095 ci.println("New: " + factoredSamples * portTickNumber + " secs");
1098 public void _ofstatsmgrintervals(CommandInterpreter ci) {
1099 String flowStatsInterv = ci.nextArgument();
1100 String portStatsInterv = ci.nextArgument();
1101 String descStatsInterv = ci.nextArgument();
1102 String tableStatsInterv = ci.nextArgument();
1104 if (flowStatsInterv == null || portStatsInterv == null
1105 || descStatsInterv == null) {
1106 ci.println("Usage: ofstatsmgrintervals <fP> <pP> <dP> <tP> (all in seconds)");
1107 ci.println("Current Values: fP=" + statisticsTickNumber + "sec pP="
1108 + portTickNumber + "sec dP=" + descriptionTickNumber + "sec tP=" + tableTickNumber + " sec");
1111 Short fP, pP, dP, tP;
1113 fP = Short.parseShort(flowStatsInterv);
1114 pP = Short.parseShort(portStatsInterv);
1115 dP = Short.parseShort(descStatsInterv);
1116 tP = Short.parseShort(tableStatsInterv);
1117 } catch (Exception e) {
1118 ci.println("Invalid format values: " + e.getMessage());
1122 if (pP <= 1 || fP <= 1 || dP <= 1 || tP <= 1) {
1123 ci.println("Invalid values. fP, pP, dP, tP have to be greater than 1.");
1127 statisticsTickNumber = fP;
1128 portTickNumber = pP;
1129 descriptionTickNumber = dP;
1130 tableTickNumber = tP;
1132 ci.println("New Values: fP=" + statisticsTickNumber + "s pP="
1133 + portTickNumber + "s dP=" + descriptionTickNumber + "s tP="
1134 + tableTickNumber + "s");
1138 * This method retrieves user configurations from config.ini and updates
1139 * statisticsTickNumber/portTickNumber/descriptionTickNumber accordingly.
1141 private void configStatsPollIntervals() {
1142 String fsStr = System.getProperty("of.flowStatsPollInterval");
1143 String psStr = System.getProperty("of.portStatsPollInterval");
1144 String dsStr = System.getProperty("of.descStatsPollInterval");
1145 String tsStr = System.getProperty("of.tableStatsPollInterval");
1146 Short fs, ps, ds, ts;
1148 if (fsStr != null) {
1150 fs = Short.parseShort(fsStr);
1152 statisticsTickNumber = fs;
1154 } catch (Exception e) {
1158 if (psStr != null) {
1160 ps = Short.parseShort(psStr);
1162 portTickNumber = ps;
1164 } catch (Exception e) {
1168 if (dsStr != null) {
1170 ds = Short.parseShort(dsStr);
1172 descriptionTickNumber = ds;
1174 } catch (Exception e) {
1178 if (tsStr != null) {
1180 ts = Short.parseShort(tsStr);
1182 tableTickNumber = ts;
1184 } catch (Exception e) {