Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Merge 1ebf35a into deb84fd
Browse files Browse the repository at this point in the history
  • Loading branch information
cemsbr committed Nov 3, 2017
2 parents deb84fd + 1ebf35a commit e99081a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 25 deletions.
57 changes: 41 additions & 16 deletions kytos/core/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import json
import logging

from pyof.v0x01.common.phy_port import PortFeatures
from pyof.v0x01.common.phy_port import PortFeatures as PortFeatures01
from pyof.v0x04.common.port import PortFeatures as PortFeatures04

from kytos.core.constants import CONNECTION_TIMEOUT, FLOOD_TIMEOUT
from kytos.core.helpers import now
Expand Down Expand Up @@ -112,26 +113,47 @@ def update_endpoint(self, endpoint):
self.add_endpoint(endpoint)

def get_speed(self):
"""Return the link speed in bits per second, None otherwise.
"""Return the link speed in bytes per second, None otherwise.
Returns:
int: Link speed in bits per second.
int, None: Link speed in bytes per second or ``None``.
"""
speed = self._get_v0x01_v0x04_speed()
if speed is None and self.switch.connection.protocol.version == 0x04:
speed = self._get_v0x04_speed()
if speed is None:
LOG.warning("No speed port %s, sw %s, feats %s", self.port_number,
self.switch.dpid[-3:], self.features)
else:
return speed

def _get_v0x01_v0x04_speed(self):
"""Check against all values of v0x01. They're part of v0x04."""
fts = self.features
pfts = PortFeatures
pfts = PortFeatures01
if fts and fts & pfts.OFPPF_10GB_FD:
return 10 * 10**9
return 10 * 10**9 / 8
elif fts and fts & (pfts.OFPPF_1GB_HD | pfts.OFPPF_1GB_FD):
return 10**9
return 10**9 / 8
elif fts and fts & (pfts.OFPPF_100MB_HD | pfts.OFPPF_100MB_FD):
return 100 * 10**6
return 100 * 10**6 / 8
elif fts and fts & (pfts.OFPPF_10MB_HD | pfts.OFPPF_10MB_FD):
return 10 * 10**6
else:
LOG.warning("No speed port %s, sw %s, feats %s", self.port_number,
self.switch.dpid[-3:], self.features)
return None
return 10 * 10**6 / 8

def _get_v0x04_speed(self):
"""Check against higher enums of v0x04.
Must be called after :meth:`get_v0x01_speed` returns ``None``.
"""
fts = self.features
pfts = PortFeatures04
if fts and fts & pfts.OFPPF_1TB_FD:
return 10**12 / 8
elif fts and fts & pfts.OFPPF_100GB_FD:
return 100 * 10**9 / 8
elif fts and fts & pfts.OFPPF_40GB_FD:
return 40 * 10**9 / 8

def get_hr_speed(self):
"""Return Human-Readable string for link speed.
Expand All @@ -143,14 +165,17 @@ def get_hr_speed(self):
speed = self.get_speed()
if speed is None:
return ''
elif speed >= 10**9:
speed *= 8
if speed == 10**12:
return '1 Tbps'
if speed >= 10**9:
return '{} Gbps'.format(round(speed / 10**9))
return '{} Mbps'.format(round(speed / 10**6))

def as_dict(self):
"""Return a dictionary with Interface attributes.
Example of output:
Speed is in bytes/sec. Example of output (100 Gbps):
.. code-block:: python3
Expand All @@ -162,7 +187,7 @@ def as_dict(self):
'type': 'interface',
'nni': False,
'uni': True,
'speed': '350 Mbps'}
'speed': 12500000000}
Returns:
dict: Dictionary filled with interface attributes.
Expand All @@ -176,7 +201,7 @@ def as_dict(self):
'type': 'interface',
'nni': self.nni,
'uni': self.uni,
'speed': self.get_hr_speed()}
'speed': self.get_speed()}
if self.stats:
iface_dict['stats'] = self.stats.as_dict()
return iface_dict
Expand Down
39 changes: 30 additions & 9 deletions tests/test_core/test_interface.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Interface tests."""
import logging
import unittest
from unittest.mock import Mock

from pyof.v0x01.common.phy_port import PortFeatures
from pyof.v0x04.common.port import PortFeatures

from kytos.core.switch import Interface, Switch

Expand All @@ -15,6 +16,8 @@ class TestInterface(unittest.TestCase):
def setUp(self):
"""Create interface object."""
switch = Switch('dpid')
switch.connection = Mock()
switch.connection.protocol.version = 0x04
self.iface = Interface('name', 42, switch)

def test_speed_feature_none(self):
Expand All @@ -29,26 +32,44 @@ def test_speed_feature_zero(self):
self.assertIsNone(self.iface.get_speed())
self.assertEqual('', self.iface.get_hr_speed())

def test_1_tera_speed(self):
"""1Tb link."""
self.iface.features = PortFeatures.OFPPF_1TB_FD
self.assertEqual(10**12 / 8, self.iface.get_speed())
self.assertEqual('1 Tbps', self.iface.get_hr_speed())

def test_100_giga_speed(self):
"""100Gb link."""
self.iface.features = PortFeatures.OFPPF_100GB_FD
self.assertEqual(100 * 10**9 / 8, self.iface.get_speed())
self.assertEqual('100 Gbps', self.iface.get_hr_speed())

def test_40_giga_speed(self):
"""40Gb link."""
self.iface.features = PortFeatures.OFPPF_40GB_FD
self.assertEqual(40 * 10**9 / 8, self.iface.get_speed())
self.assertEqual('40 Gbps', self.iface.get_hr_speed())

def test_10_giga_speed(self):
"""10GB link."""
"""10Gb link."""
self.iface.features = PortFeatures.OFPPF_10GB_FD
self.assertEqual(10 * 10**9, self.iface.get_speed())
self.assertEqual(10 * 10**9 / 8, self.iface.get_speed())
self.assertEqual('10 Gbps', self.iface.get_hr_speed())

def test_1_giga_speed(self):
"""1GB link."""
"""1Gb link."""
self.iface.features = PortFeatures.OFPPF_1GB_FD
self.assertEqual(10**9, self.iface.get_speed())
self.assertEqual(10**9 / 8, self.iface.get_speed())
self.assertEqual('1 Gbps', self.iface.get_hr_speed())

def test_100_mega_speed(self):
"""100MB link."""
"""100Mb link."""
self.iface.features = PortFeatures.OFPPF_100MB_FD
self.assertEqual(100 * 10**6, self.iface.get_speed())
self.assertEqual(100 * 10**6 / 8, self.iface.get_speed())
self.assertEqual('100 Mbps', self.iface.get_hr_speed())

def test_10_mega_speed(self):
"""10MB link."""
"""10Mb link."""
self.iface.features = PortFeatures.OFPPF_10MB_FD
self.assertEqual(10 * 10**6, self.iface.get_speed())
self.assertEqual(10 * 10**6 / 8, self.iface.get_speed())
self.assertEqual('10 Mbps', self.iface.get_hr_speed())

0 comments on commit e99081a

Please sign in to comment.