aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python/usrp_mpm
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-05-16 15:19:32 -0700
committerMartin Braun <martin.braun@ettus.com>2017-12-22 15:03:53 -0800
commit7cc40f9d12a53ef9dfda98047599e79f57a1ca36 (patch)
treef4d837914c0bb419185b59d0e2c4790326374474 /mpm/python/usrp_mpm
parente10817781df505332d15fe4a7ea48efccad796d3 (diff)
downloaduhd-7cc40f9d12a53ef9dfda98047599e79f57a1ca36.tar.gz
uhd-7cc40f9d12a53ef9dfda98047599e79f57a1ca36.tar.bz2
uhd-7cc40f9d12a53ef9dfda98047599e79f57a1ca36.zip
mpm: Added ethernet dispatcher code for N310
Diffstat (limited to 'mpm/python/usrp_mpm')
-rw-r--r--mpm/python/usrp_mpm/CMakeLists.txt1
-rw-r--r--mpm/python/usrp_mpm/ethtable.py117
-rw-r--r--mpm/python/usrp_mpm/net.py (renamed from mpm/python/usrp_mpm/periph_manager/net.py)48
-rw-r--r--mpm/python/usrp_mpm/periph_manager/CMakeLists.txt1
4 files changed, 165 insertions, 2 deletions
diff --git a/mpm/python/usrp_mpm/CMakeLists.txt b/mpm/python/usrp_mpm/CMakeLists.txt
index 4d48d455e..a470f8e67 100644
--- a/mpm/python/usrp_mpm/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/CMakeLists.txt
@@ -27,6 +27,7 @@ SET(USRP_MPM_TOP_FILES
${CMAKE_CURRENT_SOURCE_DIR}/rpc_server.py
${CMAKE_CURRENT_SOURCE_DIR}/sysfs_gpio.py
${CMAKE_CURRENT_SOURCE_DIR}/mpmtypes.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.py
${CMAKE_CURRENT_SOURCE_DIR}/uio.py
)
LIST(APPEND USRP_MPM_FILES ${USRP_MPM_TOP_FILES})
diff --git a/mpm/python/usrp_mpm/ethtable.py b/mpm/python/usrp_mpm/ethtable.py
new file mode 100644
index 000000000..aa57c6d01
--- /dev/null
+++ b/mpm/python/usrp_mpm/ethtable.py
@@ -0,0 +1,117 @@
+#
+# Copyright 2017 Ettus Research (National Instruments)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+"""
+Ethernet dispatcher table control
+"""
+
+import netaddr
+from .mpmlog import get_logger
+from .uio import UIO
+from .net import get_mac_addr
+
+class EthDispatcherTable(object):
+ """
+ Controls an Ethernet dispatcher table.
+ """
+ DEFAULT_VITA_PORT = (49153, 49154)
+ # Address offsets:
+ OWN_IP_OFFSET = 0x0000
+ OWN_PORT_OFFSET = (0x0004, 0x0008)
+ SID_IP_OFFSET = 0x1000
+ SID_PORT_MAC_HI_OFFSET = 0x1400
+ SID_MAC_LO_OFFSET = 0x1800
+
+ def __init__(self, label):
+ self.log = get_logger(label)
+ self._regs = UIO(label=label, read_only=False)
+ self.poke32 = self._regs.poke32
+ self.peek32 = self._regs.peek32
+
+ def set_ipv4_addr(self, ip_addr):
+ """
+ Set the own IPv4 address for this Ethernet dispatcher.
+ Outgoing packets will have this IP address.
+ """
+ self.log.debug("Setting my own IP address to `{}'".format(ip_addr))
+ ip_addr_int = int(netaddr.IPAddress(ip_addr))
+ self.poke32(self.OWN_IP_OFFSET, ip_addr_int)
+
+ def set_vita_port(self, port_value=None, port_idx=None):
+ """
+ Set the port that is used for incoming VITA traffic. This is used to
+ distinguish traffic that goes to the FPGA from that going to the ARM.
+ """
+ port_idx = port_idx or 0
+ port_value = port_value or self.DEFAULT_VITA_PORT[port_idx]
+ assert port_idx in (0, 1)
+ port_reg_addr = self.OWN_PORT_OFFSET[port_idx]
+ self.poke32(port_reg_addr, port_value)
+
+ def set_route(self, sid, ip_addr, udp_port, mac_addr=None):
+ """
+ Sets up routing in the Ethernet dispatcher. From sid, only the
+ destination part is important. After this call, any CHDR packet
+ reaching this Ethernet dispatcher will get routed to `ip_addr' and
+ `udp_port'.
+
+ It automatically looks up the MAC address of the destination unless a
+ MAC address is given.
+
+ sid -- Full SID, but only destination part matters.
+ ip_addr -- IPv4 destination address. String format ("1.2.3.4").
+ udp_addr -- Destination UDP port.
+ mac_addr -- If given, this will replace an ARP lookup for the MAC
+ address. String format, ("aa:bb:cc:dd:ee:ff"), case does
+ not matter.
+ """
+ udp_port = int(udp_port)
+ if mac_addr is None:
+ mac_addr = get_mac_addr(ip_addr)
+ if mac_addr is None:
+ self.log.error(
+ "Could not resolve a MAC address for IP address `{}'".format(ip_addr)
+ )
+ self.log.debug(
+ "Routing SID `{sid}' to IP address `{ip}', " \
+ "MAC address `{mac}', port `{port}'".format(
+ sid=str(sid), ip=ip_addr, mac=mac_addr, port=udp_port
+ )
+ )
+ ip_addr_int = int(netaddr.IPAddress(ip_addr))
+ mac_addr_int = int(netaddr.EUI(mac_addr))
+ dst_ep = sid.dst_ep
+ sid_offset = 4 * dst_ep
+ def poke_and_trace(addr, data):
+ " Do a poke32() and log.trace() "
+ self.log.trace("Writing to address 0x{:04X}: 0x{:04X}".format(
+ addr, data
+ ))
+ self.poke32(addr, data)
+ poke_and_trace(
+ self.SID_IP_OFFSET + sid_offset,
+ ip_addr_int
+ )
+ poke_and_trace(
+ self.SID_MAC_LO_OFFSET + sid_offset,
+ mac_addr_int & 0xFFFFFFFF,
+ )
+ poke_and_trace(
+ self.SID_PORT_MAC_HI_OFFSET + sid_offset,
+ (udp_port << 16) | (mac_addr_int >> 32)
+ )
+
+
diff --git a/mpm/python/usrp_mpm/periph_manager/net.py b/mpm/python/usrp_mpm/net.py
index 4eb1890cb..29c1bcab9 100644
--- a/mpm/python/usrp_mpm/periph_manager/net.py
+++ b/mpm/python/usrp_mpm/net.py
@@ -20,7 +20,52 @@ N310 implementation module
import itertools
import socket
from pyroute2 import IPRoute
-from ..mpmlog import get_logger
+from .mpmlog import get_logger
+
+
+def get_valid_interfaces(iface_list):
+ """
+ Given a list of interfaces (['eth1', 'eth2'] for example), return the
+ subset that contains actually valid entries.
+ Interfaces are checked for if they actually exist, and if so, if they're up.
+ """
+ ipr = IPRoute()
+ valid_ifaces = []
+ for iface in iface_list:
+ valid_iface_idx = ipr.link_lookup(ifname=iface)
+ if len(valid_iface_idx) == 0:
+ continue
+ valid_iface_idx = valid_iface_idx[0]
+ link_info = ipr.get_links(valid_iface_idx)[0]
+ if link_info.get_attr('IFLA_OPERSTATE') == 'UP':
+ assert link_info.get_attr('IFLA_IFNAME') == iface
+ valid_ifaces.append(iface)
+ ipr.close()
+ return valid_ifaces
+
+
+def get_iface_info(ifname):
+ """
+ Given an interface name (e.g. 'eth1'), return a dictionary with the
+ following keys:
+ - ip_addr: Main IPv4 address
+ - ip_addrs: List of valid IPv4 addresses
+ - mac_addr: MAC address
+
+ All values are stored as strings.
+ """
+ ipr = IPRoute()
+ try:
+ link_info = ipr.get_links(ipr.link_lookup(ifname=ifname))[0]
+ except IndexError:
+ raise LookupError("Could not identify interface `{}'".format(ifname))
+ mac_addr = link_info.get_attr('IFLA_ADDRESS')
+ ip_addrs = get_iface_addrs(mac_addr)
+ return {
+ 'mac_addr': mac_addr,
+ 'ip_addr': ip_addrs[0],
+ 'ip_addrs': ip_addrs,
+ }
def get_iface_addrs(mac_addr):
"""
@@ -36,6 +81,7 @@ def get_iface_addrs(mac_addr):
if addr.get('index', None) == link]
# flatten possibly nested list
addresses = list(itertools.chain.from_iterable(addresses))
+ ip2.close()
return addresses
diff --git a/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt b/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt
index f4bc1d1d2..49ca8e411 100644
--- a/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt
@@ -24,7 +24,6 @@ SET(USRP_MPM_PERIPHMGR_FILES
${CMAKE_CURRENT_SOURCE_DIR}/base.py
${CMAKE_CURRENT_SOURCE_DIR}/n310.py
${CMAKE_CURRENT_SOURCE_DIR}/test.py
- ${CMAKE_CURRENT_SOURCE_DIR}/net.py
${CMAKE_CURRENT_SOURCE_DIR}/udev.py
)
LIST(APPEND USRP_MPM_FILES ${USRP_MPM_PERIPHMGR_FILES})