Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- Refuse commands longer than the target CONFIG_LINE_MAX.
- Reopen serial ports that disappear on target reset (USB re-enumeration).
- Do not treat a target idle at its prompt as a busy loop.
- Add the console over a NuttX netdev console (UDP or CAN ISO-TP) for sim, QEMU
and real hardware (``netdev`` device).

## 0.0.3

Expand Down
2 changes: 2 additions & 0 deletions Documentation/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Device Interface

ntfc.device.common
ntfc.device.host
ntfc.device.netconsole
ntfc.device.netdev
ntfc.device.nuttx
ntfc.device.qemu
ntfc.device.renode
Expand Down
175 changes: 171 additions & 4 deletions Documentation/config-yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ This file defines device-under-test (DUT) setup and global configuration.
cores: # List of product cores
core0: # Core0 entry
name: 'core0-name'
device: 'sim|qemu|renode|serial'
device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration

core1: # Core1 entry
name: 'core1-name'
device: 'sim|qemu|renode|serial'
device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration


Expand All @@ -37,15 +37,15 @@ This file defines device-under-test (DUT) setup and global configuration.
cores:
core0:
name: 'core-name'
device: 'sim|qemu|renode|serial'
device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration

product1:
name: "product1-name" # Product 1 identifier
cores:
core0:
name: 'core-name'
device: 'sim|qemu|renode|serial'
device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration


Expand Down Expand Up @@ -272,6 +272,169 @@ For real hardware with UART communication:
- DATABITS: 5, 6, 7, or 8
- STOPBITS: 1, 1.5, or 2

Netdev Console
==============

The ``netconsole`` core option runs the NuttX console over a network
transport (``CONFIG_CONSOLE_NETDEV``) instead of a serial port or the
emulator's pty. The transport is implemented in
:mod:`ntfc.device.netconsole`. It can be used in two ways:

- with the spawned devices (``sim`` and ``qemu``, through
:class:`ntfc.device.host.DeviceHost`): add ``netconsole`` to the core;
the child pty output is kept in the debug log;
- with real hardware: use ``device: 'netdev'``
(:class:`ntfc.device.netdev.DeviceNetdev`). NTFC only talks to the board
over the transport; the board is reset and powered off with the
``reboot`` and ``poweroff`` system commands, as for a serial device.

.. code-block:: yaml

cores:
core0:
name: 'main'
device: 'netdev'
reboot: 'st-flash reset'
boot_timeout: 30
netconsole:
type: 'udp'
port: 55000

The UDP target address is learned from the console output, so the board
must print something after the transport is started: use a ``reboot``
command that resets the board.

NTFC opens the transport before it spawns the target, so the console
output sent at boot is not lost. Both transports carry datagrams, so a
command is split into datagrams of ``chunk`` bytes that fit the target
receive buffer (``CONFIG_CONSOLE_NETDEV_RXBUFSIZE``, 256 by default).

UDP
---

.. code-block:: yaml

cores:
core0:
name: 'main'
device: 'qemu'
netconsole:
type: 'udp'
port: 55000 # CONFIG_CONSOLE_NETDEV_PORT
bind: '0.0.0.0' # optional
chunk: 128 # optional

NTFC listens on ``port`` for the datagrams the target sends to
``CONFIG_CONSOLE_NETDEV_IPADDR`` and replies to the address the last
datagram came from, so the target address is not configured. Commands
written before the target sent anything are dropped.

.. list-table::
:header-rows: 1

* - Key
- Description
* - ``port``
- UDP port, ``CONFIG_CONSOLE_NETDEV_PORT``. Defaults to ``55000``
* - ``bind``
- (Optional) Host address to listen on. Defaults to ``0.0.0.0``
* - ``chunk``
- (Optional) Bytes per datagram. Defaults to ``128``

CAN ISO-TP
----------

.. code-block:: yaml

cores:
core0:
name: 'main'
device: 'qemu'
netconsole:
type: 'isotp'
interface: 'can0'
rx_id: 0x7d0 # target TX ID, CONFIG_CONSOLE_NETDEV_PORT
tx_id: 0x7d1 # target RX ID, CONFIG_CONSOLE_NETDEV_PORT + 1
fd: false # true when the target has CONFIG_NET_CAN_CANFD

The host uses a Linux ``can_isotp`` socket, so the ``can_isotp`` kernel
module must be loaded (``sudo modprobe can_isotp``).

.. list-table::
:header-rows: 1

* - Key
- Description
* - ``interface``
- Host CAN interface. Defaults to ``can0``
* - ``rx_id``
- CAN ID the target sends on, ``CONFIG_CONSOLE_NETDEV_PORT``.
Defaults to ``2000`` (``0x7d0``)
* - ``tx_id``
- CAN ID the target receives on, ``CONFIG_CONSOLE_NETDEV_PORT + 1``.
Defaults to ``rx_id + 1``
* - ``fd``
- (Optional) Use CAN FD frames, required when the target is built with
``CONFIG_NET_CAN_CANFD``. Defaults to ``false``
* - ``chunk``
- (Optional) Bytes per ISO-TP message. Defaults to ``128``

Example: qemu-intel64
---------------------

The NuttX ``qemu-intel64:eth_console`` configuration runs the console over
UDP on an e1000 card: the target is ``192.168.8.104`` and sends the console
to ``192.168.8.1`` port ``55000``. Give the host that address on a TAP
interface (or on a bridge the TAP interface is attached to):

.. code-block:: bash

sudo ip tuntap add dev tap0 mode tap user $USER
sudo ip addr add 192.168.8.1/24 dev tap0
sudo ip link set tap0 up

.. code-block:: yaml

cores:
core0:
name: 'main'
device: 'qemu'
exec_path: 'qemu-system-x86_64'
exec_args: '-m 2G -nographic -serial mon:stdio
-device e1000,netdev=n0
-netdev tap,id=n0,ifname=tap0,script=no,downscript=no'
netconsole:
type: 'udp'
port: 55000

For ISO-TP, build the target with ``CONFIG_NET_CAN_ISOTP``,
``CONFIG_NET_CAN_SOCK_OPTS`` and a PCI CAN controller (``CONFIG_CAN_KVASER``
with ``CONFIG_CAN_KVASER_SOCKET``) and connect the QEMU CAN bus to the host
interface ``can0`` (a ``vcan`` interface works as well):

.. code-block:: bash

sudo modprobe can_isotp
sudo ip link add dev can0 type vcan
sudo ip link set can0 up

.. code-block:: yaml

cores:
core0:
name: 'main'
device: 'qemu'
exec_path: 'qemu-system-x86_64'
exec_args: '-m 2G -nographic -serial mon:stdio
-object can-bus,id=canbus0
-object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0
-device kvaser_pci,canbus=canbus0'
netconsole:
type: 'isotp'
interface: 'can0'
rx_id: 0x7d0
tx_id: 0x7d1

Configuration Approaches
========================

Expand Down Expand Up @@ -518,6 +681,10 @@ These fields are parsed by :class:`ntfc.coreconfig.CoreConfig`.
* - ``kv``
- Per-core Kconfig overrides applied before build. Overrides matching
keys from global ``config.kv``
* - ``netconsole``
- (Optional) Run the console over a NuttX netdev console (UDP or CAN
ISO-TP) instead of the emulator pty (sim/qemu only).
See `Netdev Console`_

Linux Targets
=============
Expand Down
9 changes: 8 additions & 1 deletion Documentation/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ product: # many products can be supported in tests (pro
cores: # Cores configuration
core0: # Main core (core 0)
name: '' # Core name
device: '' # Choose a device: sim, qemu, renode, serial
device: '' # Choose a device: sim, qemu, renode, serial, netdev
exec_path: '' # Path of emulator execution for QEMU targets eg: `qemu-system-x86_64`
# Renode launcher for renode targets, eg: `renode`
# Path of serial port for serial targets, eg: `/dev/ttyACM0`
Expand Down Expand Up @@ -99,6 +99,13 @@ product: # many products can be supported in tests (pro
# Keep false for the default byte-wise behavior.
# Enable for sim or a serial transport with flow control.
# Target serial RX buffer must hold a whole command line.
netconsole: # (optional, required for netdev) NuttX netdev console instead of
type: 'udp' # the sim/qemu pty or for real HW: 'udp' or 'isotp'
port: 55000 # udp: CONFIG_CONSOLE_NETDEV_PORT, target address is learned
bind: '0.0.0.0' # udp: (optional) host address to listen on
chunk: 128 # (optional) bytes per datagram (< CONFIG_CONSOLE_NETDEV_RXBUFSIZE)
# isotp keys: interface (can0), rx_id (target TX ID = PORT),
# tx_id (target RX ID, rx_id + 1), fd (CAN FD, false)
dcmake: # (optional) Defines passed to CMake build
DEFINE1: "VALUE1"
DEFINE2: "VALUE2"
Expand Down
10 changes: 10 additions & 0 deletions src/ntfc/coreconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ def line_buffered(self) -> bool:
"""Return whether commands are sent in one transport write."""
return bool(self._config.get("line_buffered", False))

@property
def netconsole(self) -> Optional[Dict[str, Any]]:
"""Return the netdev console section, or None to use the pty."""
cfg = self._config.get("netconsole", None)
if cfg is None:
return None
if not isinstance(cfg, dict):
raise TypeError("netconsole must be a mapping")
return cfg

def kv_check(self, cfg: str) -> Any:
"""Check Kconfig option and return its value.

Expand Down
2 changes: 2 additions & 0 deletions src/ntfc/device/getdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from typing import TYPE_CHECKING, Callable, Dict

from .netdev import DeviceNetdev
from .qemu import DeviceQemu
from .renode import DeviceRenode
from .serial import DeviceSerial
Expand All @@ -38,6 +39,7 @@
"qemu": DeviceQemu,
"serial": DeviceSerial,
"renode": DeviceRenode,
"netdev": DeviceNetdev,
}

###############################################################################
Expand Down
36 changes: 34 additions & 2 deletions src/ntfc/device/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ntfc.log.logger import logger

from .common import DeviceCommon
from .netconsole import get_netconsole

if TYPE_CHECKING:
from ntfc.coreconfig import CoreConfig
Expand All @@ -55,6 +56,8 @@ def __init__(self, conf: "CoreConfig"):
self._child = None
self._cwd = conf.exec_cwd
self._cmd: Optional[List[str]] = None
# console over a network transport instead of the child pty
self._netcon = get_netconsole(conf.netconsole)

@property
def pid(self) -> Optional[int]:
Expand All @@ -81,6 +84,9 @@ def _dev_is_health_priv(self) -> bool:
if not self._child.isalive():
return False

if self._netcon and not self._netcon.is_open:
return False

return True

def _dev_reopen(self) -> pexpect.spawn:
Expand All @@ -102,6 +108,10 @@ def _write(self, data: bytes) -> None:
if data[-1] != ord("\n"):
data += self.NEWLINE_PAD

if self._netcon:
self._netcon.write(data)
return

if self._conf.line_buffered:
self._child.send(data)
return
Expand All @@ -116,6 +126,11 @@ def _write_ctrl(self, c: str) -> None:
return

assert self._child

if self._netcon:
self._netcon.write(bytes([ord(c.upper()) & 0x1F]))
return

self._child.sendcontrol(c)

def _read(self) -> bytes: # pragma: no cover
Expand All @@ -125,10 +140,18 @@ def _read(self) -> bytes: # pragma: no cover

try:
assert self._child
return self._child.read_nonblocking(size=5120, timeout=0)
data = self._child.read_nonblocking(size=5120, timeout=0)

except (pexpect.TIMEOUT, pexpect.EOF):
return b""
data = b""

if self._netcon:
# the child pty is not the console: keep its output in the log
if data:
logger.debug(f"{self.name}: {data!r}")
return self._netcon.read()

return data

def _kill_process_group(self, process: pexpect.spawn) -> None:
"""Kill process group."""
Expand Down Expand Up @@ -165,6 +188,10 @@ def host_open(self, cmd: List[str], uptime: int = 0) -> pexpect.spawn:
# we need command to reopen file in the case of crash
self._cmd = cmd

if self._netcon:
# listen before the target boots, it sends its console at once
self._netcon.start()

logger.info(f"spawn cmd: {''.join(cmd)}")
child = pexpect.spawn(
"".join(cmd), timeout=10, maxread=20000, cwd=self._cwd
Expand Down Expand Up @@ -199,6 +226,8 @@ def _post_spawn(self) -> None:
def _stop_impl(self) -> None:
"""Stop host device and kill the underlying process."""
if not self._child:
if self._netcon:
self._netcon.stop()
return

if self._child.isalive(): # pragma: no cover
Expand All @@ -212,6 +241,9 @@ def _stop_impl(self) -> None:

self._child = None

if self._netcon:
self._netcon.stop()

logger.info("host device closed")

@property
Expand Down
Loading
Loading