#!/usr/bin/env python
#
# This is intended to be run by isi_psi_d to generate the network configuration
# for Infinity.
#
# Format of a PCI address is
#
#    dom:bus:dev:func
#
# domain, bus, and device must be specified.  func can be a function number or
# '-' to denote a multifunction card where one or more functions can be a
# network interface.

import argparse
import fcntl
import re
import struct
import sys

import isi.app.lib.procs as procs
import isi.hw.psi as psi
import isi.misc as misc
import isi.net.ifaddrs as ifaddrs
from isi.sys.socket_ext import AF_LINK

BACKEND_PCI_ADDRESS_KEY = "hw.config.backend_pci_address"
FRONTEND_PCI_ADDRESS_KEY = "hw.config.frontend_pci_address"
MANAGEMENT_PCI_ADDRESS_KEY = "hw.config.management_pci_address"

debug_only = False

parser = argparse.ArgumentParser(
    description='Initialize the PSI Network interface configuration')
parser.add_argument('-d', dest='debug_only', action='store_true',
    help="Print out the configuration that would be set, don't actually set.")

def main():
    global debug_only
    debug_only = parser.parse_args().debug_only

    # XXX PSI PIPELINE: This checks a CTO attribute, it shouldn't in the future.
    if psi.getString("hw.cto.attributes.chassis.name") != "INFINITY":
        if debug_only:
            sys.stderr.write("This script is intended only for Infinity for now.\n")
        return 0

    pci_init()

    # Ignore these keys not existing.
    # It might be better if we checked for Infinity itself.
    be_pci = psi.getString(BACKEND_PCI_ADDRESS_KEY)
    fe_pci = psi.getString(FRONTEND_PCI_ADDRESS_KEY)
    mgmt_pci = psi.getString(MANAGEMENT_PCI_ADDRESS_KEY)

    # IFACE_KEYS is a dict of psi keys and values.
    # The keys are the psi keys for Isilon's currently 3 networking categories
    # Values are tuples of the value and if its an internal/backend interface
    # ex: psi key: (psi value, backend/internal interface boolean)
    IFACE_KEYS = {
        BACKEND_PCI_ADDRESS_KEY : (be_pci, True),
        FRONTEND_PCI_ADDRESS_KEY : (fe_pci, False),
        MANAGEMENT_PCI_ADDRESS_KEY : (mgmt_pci, False)
    }

    for interface_key, value  in IFACE_KEYS.iteritems():
        psi_val, internal_nic = value
        if psi_val is not None:
            generate_config(psi_val, internal_nic)
        else:
            sys.stderr.write("WARNING: {} is not set!\n".format(interface_key))

    # Try to set the infiniband psi cto key. See bug 180455
    node_has_infiniband_present = False
    for iface in ifaddrs.getifaddrs():
        addr = iface.addr
        if addr and addr[1] == AF_LINK:
            data = iface.data
            if data and data.type == ifaddrs.IFT_INFINIBAND:
                node_has_infiniband_present = True
                break

    psi_set_bool("hw.cto.attributes.infiniband.present",
                 node_has_infiniband_present)

    # Set sys.config.net if all is good, else return an error
    if fe_pci is not None and be_pci is not None and mgmt_pci is not None:
        psi_set_bool("sys.net.config", True)
    else:
        return 1


def get_dbdf(addr):
    expr = re.compile(r"(?P<dom>\d+):(?P<bus>\d+):(?P<dev>\d+):(?P<func>([-]|\d+))")
    matches = expr.match(addr)
    func = matches.group("func")
    if func == "-":
        f = -1
    else:
        f = int(func)
    domain = int(matches.group("dom"))
    bus = int(matches.group("bus"))
    device = int(matches.group("dev"))
    return domain, bus, device, f

def get_if_list():
    rc,output = procs.get_cmd_output(["/sbin/ifconfig -l"])
    if rc:
        return []
    return output[0].split()

def split_ifname(name):
    idx = ""
    for i in name[::-1]:
        if i.isdigit():
            idx = idx + i
        else:
            break
    return name.rstrip("0123456789"), int(idx[::-1])

# Chelsio cards have their ports as newbus devices, children of the PCI newbus device
def handle_chelsio(bdf, internal=False):
    # XXX PSI PIPELINE - This special-casing needs to be generalized in the
    # future, to handle other possible cases.

    # All T580 card IDs (modulo second digit)
    T580_CARD_IDs = [0x500d, 0x5010, 0x5013, 0x5014, 0x5016, 0x5084,
            0x5085, 0x5086, 0x5087, 0x5093, 0x5096, 0x5098, 0x5099]
    def is_child_of(c, p):
        name_split = split_ifname(c)
        try:
            if misc.sysctlbyname("dev.%s.%d.%%parent" % name_split) == p:
                return True
        except Exception:
            return False
        return False

    devname = pci_dev_name(*bdf)
    devid = pci_dev_device(*bdf)

    # T580 cards are enumerated "backwards" with respect to all others
    isT580 = False
    # Second digit seems to indicate type of the T580 card, so ignore it, we
    # already know it's ethernet
    if (devid & 0xF0FF) in T580_CARD_IDs:
        isT580 = True

    children = [x for x in get_if_list() if is_child_of(x, devname)]
    children.sort()
    for i in children:
        name_split = split_ifname(i)
        psi_set_bool("dev.%s.%d.internal" % name_split, internal)
        idx = children.index(i)
        if isT580:
            idx = len(children) - idx - 1
        psi_set_int("dev.%s.%d.panel_order" % name_split, idx)

# mlx4/5 creates sysctl nodes under hw.<name>
# We can determine the PCI device via hw.<name>.conf.device_name
def handle_mlx4_core(bdf, internal=False):
    def mlx4_child(c, d):
        try:
            if misc.sysctlbyname("hw.%s.conf.device_name" % (c)) == d:
                return True
        except Exception:
            pass
        return False
    devname = pci_dev_name(*bdf)
    children = [x for x in get_if_list() if mlx4_child(x, devname)]
    for i in children:
        name_split = split_ifname(i)
        psi_set_bool("dev.%s.%d.internal" % name_split, internal)
        psi_set_int("dev.%s.%d.panel_order" % name_split, name_split[1])

# XXX PSI PIPELINE: Assumptions here which are true for now, but may not be in the future.
# Infiniband devices are in-order (top to bottom), so panel order stays the same
def handle_infiniband():
    # Only permit a single infiniband card, and call it backend
    iflist = [x for x in get_if_list() if x.strip("0123456789") == "ib"]
    for i in iflist:
        name_split = split_ifname(i)
        psi_set_bool("dev.%s.%d.internal" % name_split, True)
        psi_set_int("dev.%s.%d.panel_order" % name_split, name_split[1])

# Special-case NICs
# * Chelsio cards have their ports as newbus devices, children of the PCI newbus device
# * Mellanox NICs create sysctl OIDs at hw.<ifname>, with a link to the device
#   owner at hw.<ifname>.conf.device_name
SPECIAL_CASES = {
    0x1425: handle_chelsio,
    0x15b3: handle_mlx4_core,
    }

def generate_config_internal(bdf_tuple, internal=False):
    dev_type = pci_dev_type(*bdf_tuple)
    dev_subtype = pci_dev_subclass(*bdf_tuple)
    # Some Mellanox Infiniband cards show up as type 'network' subtype 'other'
    if (not (dev_type == PCIC_NIC and dev_subtype == PCIS_ETHERNET) and
            not (dev_type == PCIC_NIC and dev_subtype == PCIS_OTHERNIC) and
            not (dev_type == PCIC_SERIAL and dev_subtype == PCIS_INFINIBAND)):
        sys.stderr.write("Warning: PCI device at %d:%d:%d:%d is not a network device" %
                bdf_tuple)
        return
    if dev_subtype == PCIS_INFINIBAND or dev_subtype == PCIS_OTHERNIC:
        handle_infiniband()
        return

    dev_vendor = pci_dev_vendor(*bdf_tuple)

    # The device is either special-cased, or it's a 1-to-1 mapping of devname
    # to ifnet name.
    if dev_vendor in SPECIAL_CASES:
        SPECIAL_CASES[dev_vendor](bdf_tuple, internal=internal)
    else:
        name = pci_dev_name(*bdf_tuple)
        name_split = split_ifname(name)
        psi_set_bool("dev.%s.%d.internal" % name_split, internal)
        psi_set_int("dev.%s.%d.panel_order" % name_split, name_split[1])

def generate_config(addr, internal=False):
    bdf_tuple = get_dbdf(addr)
    if bdf_tuple[3] == -1:
        # If the function is -1 (check all functions) iterate over the full
        # allowable PCI function space (PCI allows up to 8 functions per
        # device, per the spec)
        for i in range(0, 8):
            # Find out if the function actually exists
            try:
                pci_read_reg(bdf_tuple[0], bdf_tuple[1], bdf_tuple[2], i, 0, 4)
            except Exception:
                break
            generate_config_internal((bdf_tuple[0], bdf_tuple[1], bdf_tuple[2], i),
                internal=internal)
    else:
        generate_config_internal(bdf_tuple, internal=internal)

# Light wrappers around the psi.set*() functions, to allow debug mode (print, don't act)
def psi_set_int(key, val):
    if debug_only:
        print "%s=%d" % (key, val)
    else:
        return psi.setInt(key, val)

def psi_set_bool(key, val):
    if debug_only:
        print "%s=%s" % (key, val)
    else:
        return psi.setBool(key, val)


# No user serviceable parts beyond this point

#
# Interfacing with the PCI bus
#
#
# XXX: This should eventually be made into a proper PCI class/module, so that
# (a) we're not passing around tuples of dbdf, and (b) to make it reusable by
# test, and anyone else who may want to use PCI access from python.

pci_dev = None

# These ioctls are derived from sys/pciio.h
PCIOCGETCONF = 3224399877
PCIOCREAD = 3222564866
PCIOCWRITE = 3222564867
PCIOCATTACHED = 3222564868

# See dev/pci/pcireg.h for these registers and the constants below.
PCIR_CLASS = 11
PCIR_SUBCLASS = 10

PCIC_NIC = 2
PCIS_ETHERNET = 0
PCIS_OTHERNIC = 0x80
PCIC_SERIAL = 12
PCIS_INFINIBAND = 6

def pci_init():
    global pci_dev
    pci_dev = file("/dev/pci", "w")

def pci_read_reg(dom, bus, dev, func, reg, width):
    s = struct.pack("=IBBBxiiI", dom, bus, dev, func, reg, width, 0)
    r = fcntl.ioctl(pci_dev, PCIOCREAD, s)
    return struct.unpack("=IBBBxiiI", r)[6]

def pci_write_reg(dom, bus, dev, func, reg, width, data):
    if width == 1:
        s = struct.pack("=IBBBxiiI", dom, bus, dev, func, reg, width, data)
    elif width == 2:
        s = struct.pack("=IBBBxiiI", dom, bus, dev, func, reg, width, data)
    elif width == 4:
        s = struct.pack("=IBBBxiiI", dom, bus, dev, func, reg, width, data)
    else:
        sys.stderr.write("Invalid width specified: ", width)
        return
    fcntl.ioctl(pci_dev, PCIOCWRITE, s)

def pci_dev_attached(dom, bus, dev, func):
    s = struct.pack("=IBBBxiiI", dom, bus, dev, func, 0, 0, 0)
    r = fcntl.ioctl(pci_dev, PCIOCATTACHED, s)
    return struct.unpack("=IBBBxiiI", r)[6]

def pci_dev_subclass(dom, bus, dev, func):
    return pci_read_reg(dom, bus, dev, func, PCIR_SUBCLASS, 1)

def pci_dev_type(dom, bus, dev, func):
    return pci_read_reg(dom, bus, dev, func, PCIR_CLASS, 1)

def pci_dev_vendor(dom, bus, dev, func):
    return pci_read_reg(dom, bus, dev, func, 0, 2)

def pci_dev_device(dom, bus, dev, func):
    return pci_read_reg(dom, bus, dev, func, 2, 2)

def pci_dev_name(dom, bus, dev, func):
    cmd = "/usr/sbin/pciconf -l | /usr/bin/grep %d:%d:%d:%d:" % \
        (dom, bus, dev, func)
    rc,output = procs.get_cmd_output(cmd)
    if rc:
        return None
    return output[0].split("@")[0]

if __name__ == "__main__":
    sys.exit(main())
