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.connection.IPluginOutConnectionService;
40 import org.opendaylight.controller.sal.core.Node;
41 import org.opendaylight.controller.sal.core.NodeConnector;
42 import org.opendaylight.controller.sal.core.Property;
43 import org.opendaylight.controller.sal.core.UpdateType;
44 import org.opendaylight.controller.sal.utils.HexEncode;
45 import org.openflow.protocol.OFError;
46 import org.openflow.protocol.OFMatch;
47 import org.openflow.protocol.OFPort;
48 import org.openflow.protocol.OFStatisticsRequest;
49 import org.openflow.protocol.statistics.OFAggregateStatisticsRequest;
50 import org.openflow.protocol.statistics.OFFlowStatisticsReply;
51 import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
52 import org.openflow.protocol.statistics.OFPortStatisticsReply;
53 import org.openflow.protocol.statistics.OFPortStatisticsRequest;
54 import org.openflow.protocol.statistics.OFQueueStatisticsRequest;
55 import org.openflow.protocol.statistics.OFStatistics;
56 import org.openflow.protocol.statistics.OFStatisticsType;
57 import org.openflow.protocol.statistics.OFTableStatistics;
58 import org.openflow.protocol.statistics.OFVendorStatistics;
59 import org.openflow.util.HexString;
60 import org.osgi.framework.BundleContext;
61 import org.osgi.framework.FrameworkUtil;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * It periodically polls the different OF statistics from the OF switches and
67 * caches them for quick retrieval for the above layers' modules It also
68 * provides an API to directly query the switch about the statistics
70 public class OFStatisticsManager implements IOFStatisticsManager,
71 IInventoryShimExternalListener, CommandProvider {
72 private static final Logger log = LoggerFactory.getLogger(OFStatisticsManager.class);
73 private static final int INITIAL_SIZE = 64;
74 private static final long FLOW_STATS_PERIOD = 10000;
75 private static final long DESC_STATS_PERIOD = 60000;
76 private static final long PORT_STATS_PERIOD = 5000;
77 private static final long TABLE_STATS_PERIOD = 10000;
78 private static final long TICK = 1000;
79 private static short statisticsTickNumber = (short) (FLOW_STATS_PERIOD / TICK);
80 private static short descriptionTickNumber = (short) (DESC_STATS_PERIOD / TICK);
81 private static short portTickNumber = (short) (PORT_STATS_PERIOD / TICK);
82 private static short tableTickNumber = (short) (TABLE_STATS_PERIOD / TICK);
83 private static short factoredSamples = (short) 2;
84 private static short counter = 1;
85 private IController controller = null;
86 private ConcurrentMap<Long, List<OFStatistics>> flowStatistics;
87 private ConcurrentMap<Long, List<OFStatistics>> descStatistics;
88 private ConcurrentMap<Long, List<OFStatistics>> portStatistics;
89 private ConcurrentMap<Long, List<OFStatistics>> tableStatistics;
90 private List<OFStatistics> dummyList;
91 private ConcurrentMap<Long, StatisticsTicks> statisticsTimerTicks;
92 protected BlockingQueue<StatsRequest> pendingStatsRequests;
93 protected BlockingQueue<Long> switchPortStatsUpdated;
94 private Thread statisticsCollector;
95 private Thread txRatesUpdater;
96 private Timer statisticsTimer;
97 private TimerTask statisticsTimerTask;
98 private ConcurrentMap<Long, Boolean> switchSupportsVendorExtStats;
99 // Per port sampled (every portStatsPeriod) transmit rate
100 private Map<Long, Map<Short, TxRates>> txRates;
101 private Set<IOFStatisticsListener> statisticsListeners = new CopyOnWriteArraySet<IOFStatisticsListener>();
104 * The object containing the latest factoredSamples tx rate samples for a
107 protected class TxRates {
108 // contains the latest factoredSamples sampled transmitted bytes
109 Deque<Long> sampledTxBytes;
112 sampledTxBytes = new LinkedBlockingDeque<Long>();
115 public void update(Long txBytes) {
117 * Based on how many samples our average works on, we might have to
118 * remove the oldest sample
120 if (sampledTxBytes.size() == factoredSamples) {
121 sampledTxBytes.removeLast();
124 // Add the latest sample to the top of the queue
125 sampledTxBytes.addFirst(txBytes);
129 * Returns the average transmit rate in bps
131 * @return the average transmit rate [bps]
133 public long getAverageTxRate() {
136 * If we cannot provide the value for the time window length set
138 if (sampledTxBytes.size() < factoredSamples) {
141 long increment = sampledTxBytes.getFirst() - sampledTxBytes
143 long timePeriod = factoredSamples * PORT_STATS_PERIOD / TICK;
144 average = (8L * increment) / timePeriod;
149 public void setController(IController core) {
150 this.controller = core;
153 public void unsetController(IController core) {
154 if (this.controller == core) {
155 this.controller = null;
159 private short getStatsQueueSize() {
160 String statsQueueSizeStr = System.getProperty("of.statsQueueSize");
161 short statsQueueSize = INITIAL_SIZE;
162 if (statsQueueSizeStr != null) {
164 statsQueueSize = Short.parseShort(statsQueueSizeStr);
165 if (statsQueueSize <= 0) {
166 statsQueueSize = INITIAL_SIZE;
168 } catch (Exception e) {
171 return statsQueueSize;
174 IPluginOutConnectionService connectionPluginOutService;
175 void setIPluginOutConnectionService(IPluginOutConnectionService s) {
176 connectionPluginOutService = s;
179 void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
180 if (connectionPluginOutService == s) {
181 connectionPluginOutService = null;
186 * Function called by the dependency manager when all the required
187 * dependencies are satisfied
191 flowStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
192 descStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
193 portStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
194 tableStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
195 dummyList = new ArrayList<OFStatistics>(1);
196 pendingStatsRequests = new LinkedBlockingQueue<StatsRequest>(getStatsQueueSize());
197 statisticsTimerTicks = new ConcurrentHashMap<Long, StatisticsTicks>(INITIAL_SIZE);
198 switchPortStatsUpdated = new LinkedBlockingQueue<Long>(INITIAL_SIZE);
199 switchSupportsVendorExtStats = new ConcurrentHashMap<Long, Boolean>(INITIAL_SIZE);
200 txRates = new HashMap<Long, Map<Short, TxRates>>(INITIAL_SIZE);
202 configStatsPollIntervals();
204 // Initialize managed timers
205 statisticsTimer = new Timer("Statistics Timer Ticks");
206 statisticsTimerTask = new TimerTask() {
213 // Initialize Statistics collector thread
214 statisticsCollector = new Thread(new Runnable() {
219 StatsRequest req = pendingStatsRequests.take();
220 queryStatisticsInternal(req.switchId, req.type);
221 } catch (InterruptedException e) {
222 log.warn("Flow Statistics Collector thread "
228 }, "Statistics Collector");
230 // Initialize Tx Rate Updater thread
231 txRatesUpdater = new Thread(new Runnable() {
236 long switchId = switchPortStatsUpdated.take();
237 updatePortsTxRate(switchId);
238 } catch (InterruptedException e) {
239 log.warn("TX Rate Updater thread interrupted", e);
244 }, "TX Rate Updater");
248 * Function called by the dependency manager when at least one dependency
249 * become unsatisfied or when the component is shutting down because for
250 * example bundle is being stopped.
254 statisticsListeners.clear();
258 * Function called by dependency manager after "init ()" is called and after
259 * the services provided by the class are registered in the service registry
263 // Start managed timers
264 statisticsTimer.scheduleAtFixedRate(statisticsTimerTask, 0, TICK);
266 // Start statistics collector thread
267 statisticsCollector.start();
269 // Start bandwidth utilization computer thread
270 txRatesUpdater.start();
273 registerWithOSGIConsole();
277 * Function called by the dependency manager before the services exported by
278 * the component are unregistered, this will be followed by a "destroy ()"
283 // Stop managed timers
284 statisticsTimer.cancel();
287 public void setStatisticsListener(IOFStatisticsListener s) {
288 this.statisticsListeners.add(s);
291 public void unsetStatisticsListener(IOFStatisticsListener s) {
293 this.statisticsListeners.remove(s);
297 private void registerWithOSGIConsole() {
298 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
299 bundleContext.registerService(CommandProvider.class.getName(), this, null);
302 private static class StatsRequest {
303 protected Long switchId;
304 protected OFStatisticsType type;
306 public StatsRequest(Long d, OFStatisticsType t) {
312 public String toString() {
313 return "SReq = {switchId=" + switchId + ", type=" + type + "}";
317 public int hashCode() {
318 final int prime = 31;
320 result = prime * result
321 + ((switchId == null) ? 0 : switchId.hashCode());
322 result = prime * result + ((type == null) ? 0 : type.ordinal());
327 public boolean equals(Object obj) {
334 if (getClass() != obj.getClass()) {
337 StatsRequest other = (StatsRequest) obj;
338 if (switchId == null) {
339 if (other.switchId != null) {
342 } else if (!switchId.equals(other.switchId)) {
345 if (type != other.type) {
352 private void addStatisticsTicks(Long switchId) {
353 switchSupportsVendorExtStats.put(switchId, Boolean.TRUE); // Assume
359 statisticsTimerTicks.put(switchId, new StatisticsTicks(true));
360 log.debug("Added Switch {} to target pool",
361 HexString.toHexString(switchId.longValue()));
364 protected static class StatisticsTicks {
365 private short flowStatisticsTicks;
366 private short descriptionTicks;
367 private short portStatisticsTicks;
368 private short tableStatisticsTicks;
370 public StatisticsTicks(boolean scattered) {
372 // scatter bursts by statisticsTickPeriod
375 } // being paranoid here
376 flowStatisticsTicks = (short) (1 + counter
377 % statisticsTickNumber);
378 descriptionTicks = (short) (1 + counter % descriptionTickNumber);
379 portStatisticsTicks = (short) (1 + counter % portTickNumber);
380 tableStatisticsTicks = (short) (1 + counter % tableTickNumber);
382 flowStatisticsTicks = statisticsTickNumber;
383 descriptionTicks = descriptionTickNumber;
384 portStatisticsTicks = portTickNumber;
385 tableStatisticsTicks = tableTickNumber;
389 public boolean decrementFlowTicksIsZero() {
390 // Please ensure no code is inserted between the if check and the
391 // flowStatisticsTicks reset
392 if (--flowStatisticsTicks == 0) {
393 flowStatisticsTicks = statisticsTickNumber;
399 public boolean decrementDescTicksIsZero() {
400 // Please ensure no code is inserted between the if check and the
401 // descriptionTicks reset
402 if (--descriptionTicks == 0) {
403 descriptionTicks = descriptionTickNumber;
409 public boolean decrementPortTicksIsZero() {
410 // Please ensure no code is inserted between the if check and the
411 // descriptionTicks reset
412 if (--portStatisticsTicks == 0) {
413 portStatisticsTicks = portTickNumber;
419 public boolean decrementTableTicksIsZero() {
420 // Please ensure no code is inserted between the if check and the
421 // descriptionTicks reset
422 if(--tableStatisticsTicks == 0) {
423 tableStatisticsTicks = tableTickNumber;
430 public String toString() {
431 return "{fT=" + flowStatisticsTicks + ",dT=" + descriptionTicks
432 + ",pT=" + portStatisticsTicks + ",tT=" + tableStatisticsTicks + "}";
436 private void printInfoMessage(String type, StatsRequest request) {
437 log.info("{} stats request not inserted for switch: {}. Queue size: {}. Collector state: {}.",
438 new Object[] {type, HexString.toHexString(request.switchId), pendingStatsRequests.size(),
439 statisticsCollector.getState().toString() });
442 protected void decrementTicks() {
443 StatsRequest request = null;
444 for (Map.Entry<Long, StatisticsTicks> entry : statisticsTimerTicks
446 StatisticsTicks clock = entry.getValue();
447 Long switchId = entry.getKey();
448 if (clock.decrementFlowTicksIsZero()) {
449 request = (switchSupportsVendorExtStats.get(switchId) == Boolean.TRUE) ?
450 new StatsRequest(switchId, OFStatisticsType.VENDOR) :
451 new StatsRequest(switchId, OFStatisticsType.FLOW);
452 // If a request for this switch is already in the queue, skip to
453 // add this new request
454 if (!pendingStatsRequests.contains(request)
455 && false == pendingStatsRequests.offer(request)) {
456 printInfoMessage("Flow", request);
460 if (clock.decrementDescTicksIsZero()) {
461 request = new StatsRequest(switchId, OFStatisticsType.DESC);
462 // If a request for this switch is already in the queue, skip to
463 // add this new request
464 if (!pendingStatsRequests.contains(request)
465 && false == pendingStatsRequests.offer(request)) {
466 printInfoMessage("Description", request);
470 if (clock.decrementPortTicksIsZero()) {
471 request = new StatsRequest(switchId, OFStatisticsType.PORT);
472 // If a request for this switch is already in the queue, skip to
473 // add this new request
474 if (!pendingStatsRequests.contains(request)
475 && false == pendingStatsRequests.offer(request)) {
476 printInfoMessage("Port", request);
480 if(clock.decrementTableTicksIsZero()) {
481 request = new StatsRequest(switchId, OFStatisticsType.TABLE);
482 // If a request for this switch is already in the queue, skip to
483 // add this new request
484 if (!pendingStatsRequests.contains(request)
485 && false == pendingStatsRequests.offer(request)) {
486 printInfoMessage("Table", request);
492 private void removeStatsRequestTasks(Long switchId) {
493 log.debug("Cleaning Statistics database for switch {}",
494 HexEncode.longToHexString(switchId));
495 // To be safe, let's attempt removal of both VENDOR and FLOW request. It
497 pendingStatsRequests.remove(new StatsRequest(switchId,
498 OFStatisticsType.VENDOR));
499 pendingStatsRequests.remove(new StatsRequest(switchId,
500 OFStatisticsType.FLOW));
501 pendingStatsRequests.remove(new StatsRequest(switchId,
502 OFStatisticsType.DESC));
503 pendingStatsRequests.remove(new StatsRequest(switchId,
504 OFStatisticsType.PORT));
505 pendingStatsRequests.remove(new StatsRequest(switchId,
506 OFStatisticsType.TABLE));
507 // Take care of the TX rate databases
508 switchPortStatsUpdated.remove(switchId);
509 txRates.remove(switchId);
512 private void clearFlowStatsAndTicks(Long switchId) {
513 statisticsTimerTicks.remove(switchId);
514 removeStatsRequestTasks(switchId);
515 flowStatistics.remove(switchId);
516 log.debug("Statistics removed for switch {}",
517 HexString.toHexString(switchId));
520 private void queryStatisticsInternal(Long switchId, OFStatisticsType statType) {
522 // Query the switch on all matches
523 List<OFStatistics> values = this.fetchStatisticsFromSwitch(switchId, statType, null);
525 // If got a valid response update local cache and notify listeners
526 if (values != null && !values.isEmpty()) {
530 flowStatistics.put(switchId, values);
531 notifyFlowUpdate(switchId, values);
535 descStatistics.put(switchId, values);
536 // Notify who may be interested in a description change
537 notifyDescriptionUpdate(switchId, values);
540 // Overwrite cache with new port statistics for this switch
541 portStatistics.put(switchId, values);
543 // Wake up the thread which maintains the TX byte counters for
545 switchPortStatsUpdated.offer(switchId);
546 notifyPortUpdate(switchId, values);
550 tableStatistics.put(switchId, values);
551 notifyTableUpdate(switchId, values);
558 private void notifyDescriptionUpdate(Long switchId, List<OFStatistics> values) {
559 for (IOFStatisticsListener l : this.statisticsListeners) {
560 l.descriptionStatisticsRefreshed(switchId, values);
564 private void notifyFlowUpdate(Long switchId, List<OFStatistics> values) {
565 if (values.get(0) instanceof OFVendorStatistics) {
566 values = this.v6StatsListToOFStatsList(values);
569 for (IOFStatisticsListener l : this.statisticsListeners) {
570 l.flowStatisticsRefreshed(switchId, values);
575 private void notifyPortUpdate(Long switchId, List<OFStatistics> values) {
576 for (IOFStatisticsListener l : this.statisticsListeners) {
577 l.portStatisticsRefreshed(switchId, values);
581 private void notifyTableUpdate(Long switchId, List<OFStatistics> values) {
582 for (IOFStatisticsListener l : this.statisticsListeners) {
583 l.tableStatisticsRefreshed(switchId, values);
588 * Generic function to get the statistics form an OF switch
590 @SuppressWarnings("unchecked")
591 private List<OFStatistics> fetchStatisticsFromSwitch(Long switchId,
592 OFStatisticsType statsType, Object target) {
593 List<OFStatistics> values = null;
595 ISwitch sw = controller.getSwitch(switchId);
598 OFStatisticsRequest req = new OFStatisticsRequest();
599 req.setStatisticType(statsType);
600 int requestLength = req.getLengthU();
602 if (statsType == OFStatisticsType.FLOW) {
603 OFMatch match = null;
604 if (target == null) {
606 match = new OFMatch();
607 match.setWildcards(0xffffffff);
608 } else if (!(target instanceof OFMatch)) {
610 log.warn("Invalid target type for Flow stats request: {}",
614 // Specific flow request
615 match = (OFMatch) target;
617 OFFlowStatisticsRequest specificReq = new OFFlowStatisticsRequest();
618 specificReq.setMatch(match);
619 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
620 specificReq.setTableId((byte) 0xff);
621 req.setStatistics(Collections
622 .singletonList((OFStatistics) specificReq));
623 requestLength += specificReq.getLength();
625 } else if (statsType == OFStatisticsType.VENDOR) {
626 V6StatsRequest specificReq = new V6StatsRequest();
627 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
628 specificReq.setTableId((byte) 0xff);
629 req.setStatistics(Collections
630 .singletonList((OFStatistics) specificReq));
631 requestLength += specificReq.getLength();
633 } else if (statsType == OFStatisticsType.AGGREGATE) {
634 OFAggregateStatisticsRequest specificReq = new OFAggregateStatisticsRequest();
635 OFMatch match = new OFMatch();
636 match.setWildcards(0xffffffff);
637 specificReq.setMatch(match);
638 specificReq.setOutPort(OFPort.OFPP_NONE.getValue());
639 specificReq.setTableId((byte) 0xff);
640 req.setStatistics(Collections
641 .singletonList((OFStatistics) specificReq));
642 requestLength += specificReq.getLength();
644 } else if (statsType == OFStatisticsType.PORT) {
646 if (target == null) {
648 targetPort = OFPort.OFPP_NONE.getValue();
649 } else if (!(target instanceof Short)) {
651 log.warn("Invalid target type for Port stats request: {}",
655 // Specific port request
656 targetPort = (Short) target;
658 OFPortStatisticsRequest specificReq = new OFPortStatisticsRequest();
659 specificReq.setPortNumber(targetPort);
660 req.setStatistics(Collections
661 .singletonList((OFStatistics) specificReq));
662 requestLength += specificReq.getLength();
664 } else if (statsType == OFStatisticsType.QUEUE) {
665 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
666 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
667 specificReq.setQueueId(0xffffffff);
668 req.setStatistics(Collections
669 .singletonList((OFStatistics) specificReq));
670 requestLength += specificReq.getLength();
672 } else if (statsType == OFStatisticsType.DESC) {
674 } else if (statsType == OFStatisticsType.TABLE) {
676 if (!(target instanceof Byte)) {
678 log.warn("Invalid table id for table stats request: {}",
682 byte targetTable = (Byte) target;
683 OFTableStatistics specificReq = new OFTableStatistics();
684 specificReq.setTableId(targetTable);
685 req.setStatistics(Collections
686 .singletonList((OFStatistics) specificReq));
687 requestLength += specificReq.getLength();
691 req.setLengthU(requestLength);
692 Object result = sw.getStatistics(req);
694 if (result == null) {
695 log.warn("Request Timed Out for ({}) from switch {}", type,
696 HexString.toHexString(switchId));
697 } else if (result instanceof OFError) {
698 log.warn("Switch {} failed to handle ({}) stats request: {}",
699 new Object[] { HexString.toHexString(switchId), type,
700 Utils.getOFErrorString((OFError) result) });
701 if (this.switchSupportsVendorExtStats.get(switchId) == Boolean.TRUE) {
703 "Switching back to regular Flow stats requests for switch {}",
704 HexString.toHexString(switchId));
705 this.switchSupportsVendorExtStats.put(switchId,
709 values = (List<OFStatistics>) result;
716 public List<OFStatistics> getOFFlowStatistics(Long switchId) {
717 List<OFStatistics> list = flowStatistics.get(switchId);
720 * Check on emptiness as interference between add and get is still
721 * possible on the inner list (the concurrentMap entry's value)
723 return (list == null || list.isEmpty()) ? this.dummyList
724 : (list.get(0) instanceof OFVendorStatistics) ? this
725 .v6StatsListToOFStatsList(list) : list;
729 public List<OFStatistics> getOFFlowStatistics(Long switchId, OFMatch ofMatch, short priority) {
730 List<OFStatistics> statsList = flowStatistics.get(switchId);
733 * Check on emptiness as interference between add and get is still
734 * possible on the inner list (the concurrentMap entry's value)
736 if (statsList == null || statsList.isEmpty()) {
737 return this.dummyList;
740 if (statsList.get(0) instanceof OFVendorStatistics) {
742 * Caller could provide regular OF match when we instead pull the
743 * vendor statistics from this node Caller is not supposed to know
744 * whether this switch supports vendor extensions statistics
747 V6Match targetMatch = (ofMatch instanceof V6Match) ? (V6Match) ofMatch
748 : new V6Match(ofMatch);
750 List<OFStatistics> targetList = v6StatsListToOFStatsList(statsList);
751 for (OFStatistics stats : targetList) {
752 V6StatsReply v6Stats = (V6StatsReply) stats;
753 V6Match v6Match = v6Stats.getMatch();
754 if (v6Stats.getPriority() == priority && v6Match.equals(targetMatch)) {
755 List<OFStatistics> list = new ArrayList<OFStatistics>();
761 for (OFStatistics stats : statsList) {
762 OFFlowStatisticsReply flowStats = (OFFlowStatisticsReply) stats;
763 if (flowStats.getPriority() == priority && flowStats.getMatch().equals(ofMatch)) {
764 List<OFStatistics> list = new ArrayList<OFStatistics>();
770 return this.dummyList;
774 * Converts the v6 vendor statistics to the OFStatistics
776 private List<OFStatistics> v6StatsListToOFStatsList(
777 List<OFStatistics> statistics) {
778 List<OFStatistics> v6statistics = new ArrayList<OFStatistics>();
779 if (statistics != null && !statistics.isEmpty()) {
780 for (OFStatistics stats : statistics) {
781 if (stats instanceof OFVendorStatistics) {
782 List<OFStatistics> r = getV6ReplyStatistics((OFVendorStatistics) stats);
784 v6statistics.addAll(r);
792 private static List<OFStatistics> getV6ReplyStatistics(
793 OFVendorStatistics stat) {
794 int length = stat.getLength();
795 List<OFStatistics> results = new ArrayList<OFStatistics>();
797 return null; // Nicira Hdr is 12 bytes. We need atleast that much
798 ByteBuffer data = ByteBuffer.allocate(length);
801 if (log.isTraceEnabled()) {
802 log.trace("getV6ReplyStatistics: Buffer BYTES ARE {}",
803 HexString.toHexString(data.array()));
806 int vendor = data.getInt(); // first 4 bytes is vendor id.
807 if (vendor != V6StatsRequest.NICIRA_VENDOR_ID) {
808 log.warn("Unexpected vendor id: 0x{}", Integer.toHexString(vendor));
811 // go ahead by 8 bytes which is 8 bytes of 0
812 data.getLong(); // should be all 0's
813 length -= 12; // 4 bytes Nicira Hdr + 8 bytes from above line have
817 V6StatsReply v6statsreply;
820 v6statsreply = new V6StatsReply();
821 min_len = v6statsreply.getLength();
822 if (length < v6statsreply.getLength())
824 v6statsreply.setActionFactory(stat.getActionFactory());
825 v6statsreply.readFrom(data);
826 if (v6statsreply.getLength() < min_len)
828 v6statsreply.setVendorId(vendor);
829 log.trace("V6StatsReply: {}", v6statsreply);
830 length -= v6statsreply.getLength();
831 results.add(v6statsreply);
837 public List<OFStatistics> queryStatistics(Long switchId,
838 OFStatisticsType statType, Object target) {
840 * Caller does not know and it is not supposed to know whether this
841 * switch supports vendor extension. We adjust the target for him
843 if (statType == OFStatisticsType.FLOW) {
844 if (switchSupportsVendorExtStats.get(switchId) == Boolean.TRUE) {
845 statType = OFStatisticsType.VENDOR;
849 List<OFStatistics> list = this.fetchStatisticsFromSwitch(switchId, statType,
852 return (list == null) ? null :
853 (statType == OFStatisticsType.VENDOR) ? v6StatsListToOFStatsList(list) : list;
857 public List<OFStatistics> getOFDescStatistics(Long switchId) {
858 if (!descStatistics.containsKey(switchId))
859 return this.dummyList;
861 return descStatistics.get(switchId);
865 public List<OFStatistics> getOFPortStatistics(Long switchId) {
866 if (!portStatistics.containsKey(switchId)) {
867 return this.dummyList;
870 return portStatistics.get(switchId);
874 public List<OFStatistics> getOFPortStatistics(Long switchId, short portId) {
875 if (!portStatistics.containsKey(switchId)) {
876 return this.dummyList;
878 List<OFStatistics> list = new ArrayList<OFStatistics>(1);
879 for (OFStatistics stats : portStatistics.get(switchId)) {
880 if (((OFPortStatisticsReply) stats).getPortNumber() == portId) {
889 public List<OFStatistics> getOFTableStatistics(Long switchId) {
890 if (!tableStatistics.containsKey(switchId)) {
891 return this.dummyList;
894 return tableStatistics.get(switchId);
898 public List<OFStatistics> getOFTableStatistics(Long switchId, Byte tableId) {
899 if (!tableStatistics.containsKey(switchId)) {
900 return this.dummyList;
903 List<OFStatistics> list = new ArrayList<OFStatistics>(1);
904 for (OFStatistics stats : tableStatistics.get(switchId)) {
905 if (((OFTableStatistics) stats).getTableId() == tableId) {
914 public int getFlowsNumber(long switchId) {
915 return this.flowStatistics.get(switchId).size();
919 * InventoryShim replay for us all the switch addition which happened before
923 public void updateNode(Node node, UpdateType type, Set<Property> props) {
924 Long switchId = (Long) node.getID();
927 addStatisticsTicks(switchId);
930 clearFlowStatsAndTicks(switchId);
936 public void updateNodeConnector(NodeConnector nodeConnector,
937 UpdateType type, Set<Property> props) {
942 * Update the cached port rates for this switch with the latest retrieved
943 * port transmit byte count
947 private synchronized void updatePortsTxRate(long switchId) {
948 List<OFStatistics> newPortStatistics = this.portStatistics.get(switchId);
949 if (newPortStatistics == null) {
952 Map<Short, TxRates> rates = this.txRates.get(switchId);
954 // First time rates for this switch are added
955 rates = new HashMap<Short, TxRates>();
956 txRates.put(switchId, rates);
958 for (OFStatistics stats : newPortStatistics) {
959 OFPortStatisticsReply newPortStat = (OFPortStatisticsReply) stats;
960 short port = newPortStat.getPortNumber();
961 TxRates portRatesHolder = rates.get(port);
962 if (portRatesHolder == null) {
963 // First time rates for this port are added
964 portRatesHolder = new TxRates();
965 rates.put(port, portRatesHolder);
967 // Get and store the number of transmitted bytes for this port
968 // And handle the case where agent does not support the counter
969 long transmitBytes = newPortStat.getTransmitBytes();
970 long value = (transmitBytes < 0) ? 0 : transmitBytes;
971 portRatesHolder.update(value);
976 public synchronized long getTransmitRate(Long switchId, Short port) {
978 if (switchId == null || port == null) {
981 Map<Short, TxRates> perSwitch = txRates.get(switchId);
982 if (perSwitch == null) {
985 TxRates portRates = perSwitch.get(port);
986 if (portRates == null) {
989 return portRates.getAverageTxRate();
993 * Manual switch name configuration code
996 public String getHelp() {
997 StringBuffer help = new StringBuffer();
998 help.append("---OF Statistics Manager utilities---\n");
999 help.append("\t ofdumpstatsmgr - "
1000 + "Print Internal Stats Mgr db\n");
1001 help.append("\t ofstatsmgrintervals <fP> <pP> <dP> <tP> (all in seconds) - "
1002 + "Set/Show flow/port/dedscription stats poll intervals\n");
1003 return help.toString();
1006 private boolean isValidSwitchId(String switchId) {
1007 String regexDatapathID = "^([0-9a-fA-F]{1,2}[:-]){7}[0-9a-fA-F]{1,2}$";
1008 String regexDatapathIDLong = "^[0-9a-fA-F]{1,16}$";
1010 return (switchId != null && (switchId.matches(regexDatapathID) || switchId
1011 .matches(regexDatapathIDLong)));
1014 public long getSwitchIDLong(String switchId) {
1016 String switchString = "0";
1018 if (isValidSwitchId(switchId)) {
1019 if (switchId.contains(":")) {
1020 // Handle the 00:00:AA:BB:CC:DD:EE:FF notation
1021 switchString = switchId.replace(":", "");
1022 } else if (switchId.contains("-")) {
1023 // Handle the 00-00-AA-BB-CC-DD-EE-FF notation
1024 switchString = switchId.replace("-", "");
1026 // Handle the 0123456789ABCDEF notation
1027 switchString = switchId;
1030 return Long.parseLong(switchString, radix);
1034 * Internal information dump code
1036 private String prettyPrintSwitchMap(ConcurrentMap<Long, StatisticsTicks> map) {
1037 StringBuffer buffer = new StringBuffer();
1039 for (Entry<Long, StatisticsTicks> entry : map.entrySet()) {
1040 buffer.append(HexString.toHexString(entry.getKey()) + "="
1041 + entry.getValue().toString() + " ");
1044 return buffer.toString();
1047 public void _ofdumpstatsmgr(CommandInterpreter ci) {
1048 ci.println("Global Counter: " + counter);
1049 ci.println("Timer Ticks: " + prettyPrintSwitchMap(statisticsTimerTicks));
1050 ci.println("PendingStatsQueue: " + pendingStatsRequests);
1051 ci.println("PendingStatsQueue size: " + pendingStatsRequests.size());
1052 ci.println("Stats Collector alive: " + statisticsCollector.isAlive());
1053 ci.println("Stats Collector State: "
1054 + statisticsCollector.getState().toString());
1055 ci.println("StatsTimer: " + statisticsTimer.toString());
1056 ci.println("Flow Stats Period: " + statisticsTickNumber + " s");
1057 ci.println("Desc Stats Period: " + descriptionTickNumber + " s");
1058 ci.println("Port Stats Period: " + portTickNumber + " s");
1059 ci.println("Table Stats Period: " + tableTickNumber + " s");
1062 public void _resetSwitchCapability(CommandInterpreter ci) {
1063 String sidString = ci.nextArgument();
1065 if (sidString == null) {
1066 ci.println("Insert the switch id (numeric value)");
1070 sid = Long.valueOf(sidString);
1071 this.switchSupportsVendorExtStats.put(sid, Boolean.TRUE);
1072 ci.println("Vendor capability for switch " + sid + " set to "
1073 + this.switchSupportsVendorExtStats.get(sid));
1074 } catch (NumberFormatException e) {
1075 ci.println("Invalid switch id. Has to be numeric.");
1080 public void _ofbw(CommandInterpreter ci) {
1081 String sidString = ci.nextArgument();
1083 if (sidString == null) {
1084 ci.println("Insert the switch id (numeric value)");
1088 sid = Long.valueOf(sidString);
1089 } catch (NumberFormatException e) {
1090 ci.println("Invalid switch id. Has to be numeric.");
1093 Map<Short, TxRates> thisSwitchRates = txRates.get(sid);
1094 ci.println("Bandwidth utilization (" + factoredSamples
1095 * portTickNumber + " sec average) for switch "
1096 + HexEncode.longToHexString(sid) + ":");
1097 if (thisSwitchRates == null) {
1098 ci.println("Not available");
1100 for (Entry<Short, TxRates> entry : thisSwitchRates.entrySet()) {
1101 ci.println("Port: " + entry.getKey() + ": "
1102 + entry.getValue().getAverageTxRate() + " bps");
1108 public void _txratewindow(CommandInterpreter ci) {
1109 String averageWindow = ci.nextArgument();
1111 if (averageWindow == null) {
1112 ci.println("Insert the length in seconds of the median "
1113 + "window for tx rate");
1114 ci.println("Current: " + factoredSamples * portTickNumber + " secs");
1118 seconds = Short.valueOf(averageWindow);
1119 } catch (NumberFormatException e) {
1120 ci.println("Invalid period.");
1122 OFStatisticsManager.factoredSamples = (short) (seconds / portTickNumber);
1123 ci.println("New: " + factoredSamples * portTickNumber + " secs");
1126 public void _ofstatsmgrintervals(CommandInterpreter ci) {
1127 String flowStatsInterv = ci.nextArgument();
1128 String portStatsInterv = ci.nextArgument();
1129 String descStatsInterv = ci.nextArgument();
1130 String tableStatsInterv = ci.nextArgument();
1132 if (flowStatsInterv == null || portStatsInterv == null
1133 || descStatsInterv == null) {
1134 ci.println("Usage: ofstatsmgrintervals <fP> <pP> <dP> <tP> (all in seconds)");
1135 ci.println("Current Values: fP=" + statisticsTickNumber + "sec pP="
1136 + portTickNumber + "sec dP=" + descriptionTickNumber + "sec tP=" + tableTickNumber + " sec");
1139 Short fP, pP, dP, tP;
1141 fP = Short.parseShort(flowStatsInterv);
1142 pP = Short.parseShort(portStatsInterv);
1143 dP = Short.parseShort(descStatsInterv);
1144 tP = Short.parseShort(tableStatsInterv);
1145 } catch (Exception e) {
1146 ci.println("Invalid format values: " + e.getMessage());
1150 if (pP <= 1 || fP <= 1 || dP <= 1 || tP <= 1) {
1151 ci.println("Invalid values. fP, pP, dP, tP have to be greater than 1.");
1155 statisticsTickNumber = fP;
1156 portTickNumber = pP;
1157 descriptionTickNumber = dP;
1158 tableTickNumber = tP;
1160 ci.println("New Values: fP=" + statisticsTickNumber + "s pP="
1161 + portTickNumber + "s dP=" + descriptionTickNumber + "s tP="
1162 + tableTickNumber + "s");
1166 * This method retrieves user configurations from config.ini and updates
1167 * statisticsTickNumber/portTickNumber/descriptionTickNumber accordingly.
1169 private void configStatsPollIntervals() {
1170 String fsStr = System.getProperty("of.flowStatsPollInterval");
1171 String psStr = System.getProperty("of.portStatsPollInterval");
1172 String dsStr = System.getProperty("of.descStatsPollInterval");
1173 String tsStr = System.getProperty("of.tableStatsPollInterval");
1174 Short fs, ps, ds, ts;
1176 if (fsStr != null) {
1178 fs = Short.parseShort(fsStr);
1180 statisticsTickNumber = fs;
1182 } catch (Exception e) {
1186 if (psStr != null) {
1188 ps = Short.parseShort(psStr);
1190 portTickNumber = ps;
1192 } catch (Exception e) {
1196 if (dsStr != null) {
1198 ds = Short.parseShort(dsStr);
1200 descriptionTickNumber = ds;
1202 } catch (Exception e) {
1206 if (tsStr != null) {
1208 ts = Short.parseShort(tsStr);
1210 tableTickNumber = ts;
1212 } catch (Exception e) {