From ccb24e1766f27ad7895f7dc31453c6b9e21623a1 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 24 Aug 2015 11:03:36 -0600 Subject: [PATCH] drivers/net: Add a basic prototype for a local loopback device --- ChangeLog | 2 + drivers/net/Kconfig | 5 +- drivers/net/Make.defs | 6 +- drivers/net/loopback.c | 648 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 659 insertions(+), 2 deletions(-) create mode 100644 drivers/net/loopback.c diff --git a/ChangeLog b/ChangeLog index 32a4086f3b..4a1244b08a 100755 --- a/ChangeLog +++ b/ChangeLog @@ -10877,3 +10877,5 @@ a local loopback device and link layer (2015-08-24). * libc/netdb, net/loopback, include/nuttx/net: Add NetDB support for the local loopback device (2015-08-24). + * drivers/net/loopback.c: Add basic prototype of a local loopback + device (2015-08-24). diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index 370b35cc5d..9806298da3 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -8,8 +8,11 @@ comment "General Ethernet MAC Driver Options" config NETDEV_LOOPBACK bool "Local loopback support" default n + select NET_NOINTS ---help--- - Add support for the local network loopback device, lo. + Add support for the local network loopback device, lo. Any additional + networking devices that are enabled must be compatible with + CONFIG_NET_NOINTS. config NETDEV_MULTINIC bool "Multiple network interface support" diff --git a/drivers/net/Make.defs b/drivers/net/Make.defs index 1af4356565..e09162c00c 100644 --- a/drivers/net/Make.defs +++ b/drivers/net/Make.defs @@ -1,7 +1,7 @@ ############################################################################ # drivers/net/Make.defs # -# Copyright (C) 2007, 2010-2012 Gregory Nutt. All rights reserved. +# Copyright (C) 2007, 2010-2012, 2015 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -39,6 +39,10 @@ ifeq ($(CONFIG_NET),y) # Include network interface drivers +ifeq ($(CONFIG_NETDEV_LOOPBACK),y) + CSRCS += loopback.c +endif + ifeq ($(CONFIG_NET_DM90x0),y) CSRCS += dm90x0.c endif diff --git a/drivers/net/loopback.c b/drivers/net/loopback.c new file mode 100644 index 0000000000..a3d0779043 --- /dev/null +++ b/drivers/net/loopback.c @@ -0,0 +1,648 @@ +/**************************************************************************** + * drivers/net/loopback.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NETDEV_LOOBACK) + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_NET_PKT +# include +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_NET_NOINTS +# error CONFIG_NET_NOINTS must be selected +#endif + +#ifndef CONFIG_SCHED_HPWORK +# error High priority work queue support is required (CONFIG_SCHED_HPWORK) +#endif + +/* TX poll delay = 1 seconds. CLK_TCK is the number of clock ticks per second */ + +#define LO_WDDELAY (1*CLK_TCK) +#define LO_POLLHSEC (1*2) + +/* This is a helper pointer for accessing the contents of the Ethernet header */ + +#define BUF ((struct eth_hdr_s *)priv->lo_dev.d_buf) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The lo_driver_s encapsulates all state information for a single hardware + * interface + */ + +struct lo_driver_s +{ + bool lo_bifup; /* true:ifup false:ifdown */ + WDOG_ID lo_polldog; /* TX poll timer */ + struct work_s lo_work; /* For deferring work to the work queue */ + + /* This holds the information visible to uIP/NuttX */ + + struct net_driver_s lo_dev; /* Interface understood by uIP */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct lo_driver_s g_loopback; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* Loop back logic */ + +static void lo_loopback(FAR struct lo_driver_s *priv); + +/* Polling logic */ + +static int lo_txpoll(FAR struct net_driver_s *dev); +static inline void lo_poll(FAR struct lo_driver_s *priv); +static void lo_poll_work(FAR void *arg); +static void lo_poll_expiry(int argc, uint32_t arg, ...); + +/* NuttX callback functions */ + +static int lo_ifup(FAR struct net_driver_s *dev); +static int lo_ifdown(FAR struct net_driver_s *dev); +static inline void lo_txavail_process(FAR struct lo_driver_s *priv); +static void lo_txavail_work(FAR void *arg); +static int lo_txavail(FAR struct net_driver_s *dev); +#if defined(CONFIG_NET_IGMP) || defined(CONFIG_NET_ICMPv6) +static int lo_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac); +#ifdef CONFIG_NET_IGMP +static int lo_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac); +#endif +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: lo_loopback + * + * Description: + * An interrupt was received indicating the availability of a new RX packet + * + * Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by interrupt handling logic. + * + ****************************************************************************/ + +static void lo_loopback(FAR struct lo_driver_s *priv) +{ +#ifdef CONFIG_NET_PKT + /* When packet sockets are enabled, feed the frame into the packet tap */ + + pkt_input(&priv->lo_dev); +#endif + + /* We only accept IP packets of the configured type and ARP packets */ + +#ifdef CONFIG_NET_IPv4 + if (BUF->type == HTONS(ETHTYPE_IP)) + { + nllvdbg("IPv4 frame\n"); + ipv4_input(&priv->lo_dev); + + /* If the above function invocation resulted in data that should be + * sent out on the network, the field d_len will set to a value > 0. + */ + + if (priv->lo_dev.d_len > 0) + { + /* Loop the packet back to the network. + * REVISIST: The must be a limit to the depth of recursion. + */ + + lo_loopback(priv); + } + } + else +#endif +#ifdef CONFIG_NET_IPv6 + if (BUF->type == HTONS(ETHTYPE_IP6)) + { + nllvdbg("Iv6 frame\n"); + ipv6_input(&priv->lo_dev); + + /* If the above function invocation resulted in data that should be + * sent out on the network, the field d_len will set to a value > 0. + */ + + if (priv->lo_dev.d_len > 0) + { + /* Loop the packet back to the netork + * REVISIST: The must be a limit to the depth of recursion. + */ + + lo_loopback(priv); + } + } + else +#endif + { + ndbg("WARNING: Unrecognized packet type dropped: %04x\n", BUF->type); + } +#endif +} + +/**************************************************************************** + * Function: lo_txpoll + * + * Description: + * Check if the network has any outgoing packets ready to send. This is + * a callback from devif_poll(). devif_poll() will be called only during + * normal TX polling. + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * OK on success; a negated errno on failure + * + * Assumptions: + * May or may not be called from an interrupt handler. In either case, + * global interrupts are disabled, either explicitly or indirectly through + * interrupt handling logic. + * + ****************************************************************************/ + +static int lo_txpoll(FAR struct net_driver_s *dev) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private; + + /* Check if the polling resulted in data that should be sent, i.e., tht + * the field d_len is set to a value > 0. + */ + + if (priv->lo_dev.d_len > 0) + { + /* Loop the packet back to the host */ + + lo_loopback(priv); + } + + return 0; +} + +/**************************************************************************** + * Function: lo_poll + * + * Description: + * Perform the periodic poll. This may be called either from watchdog + * timer logic or from the worker thread, depending upon the configuration. + * + * Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static inline void lo_poll(FAR struct lo_driver_s *priv) +{ + /* Perform the poll */ + + (void)devif_timer(&priv->lo_dev, lo_txpoll, LO_POLLHSEC); + + /* Setup the watchdog poll timer again */ + + (void)wd_start(priv->lo_polldog, LO_WDDELAY, lo_poll_expiry, 1, priv); +} + +/**************************************************************************** + * Function: lo_poll_work + * + * Description: + * Perform periodic polling from the worker thread + * + * Parameters: + * arg - The argument passed when work_queue() as called. + * + * Returned Value: + * OK on success + * + * Assumptions: + * Ethernet interrupts are disabled + * + ****************************************************************************/ + +#ifdef CONFIG_NET_NOINTS +static void lo_poll_work(FAR void *arg) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)arg; + net_lock_t state; + + /* Perform the poll */ + + state = net_lock(); + lo_poll(priv); + net_unlock(state); +} +#endif + +/**************************************************************************** + * Function: lo_poll_expiry + * + * Description: + * Periodic timer handler. Called from the timer interrupt handler. + * + * Parameters: + * argc - The number of available arguments + * arg - The first argument + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by the watchdog logic. + * + ****************************************************************************/ + +static void lo_poll_expiry(int argc, uint32_t arg, ...) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)arg; + + /* Is our single work structure available? It may not be if there are + * pending interrupt actions. + */ + + if (work_available(&priv->lo_work)) + { + /* Schedule to perform the interrupt processing on the worker thread. */ + + work_queue(HPWORK, &priv->lo_work, lo_poll_work, priv, 0); + } + else + { + /* No.. Just re-start the watchdog poll timer, missing one polling + * cycle. + */ + + (void)wd_start(priv->lo_polldog, LO_WDDELAY, lo_poll_expiry, 1, arg); + } +} + +/**************************************************************************** + * Function: lo_ifup + * + * Description: + * NuttX Callback: Bring up the Ethernet interface when an IP address is + * provided + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static int lo_ifup(FAR struct net_driver_s *dev) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private; + +#ifdef CONFIG_NET_IPv4 + ndbg("Bringing up: %d.%d.%d.%d\n", + dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff, + (dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24); +#endif +#ifdef CONFIG_NET_IPv6 + ndbg("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", + dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2], + dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5], + dev->d_ipv6addr[6], dev->d_ipv6addr[7]); +#endif + + /* Set and activate a timer process */ + + (void)wd_start(priv->lo_polldog, LO_WDDELAY, lo_poll_expiry, 1, (uint32_t)priv); + + priv->lo_bifup = true; + return OK; +} + +/**************************************************************************** + * Function: lo_ifdown + * + * Description: + * NuttX Callback: Stop the interface. + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static int lo_ifdown(FAR struct net_driver_s *dev) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private; + + /* Cancel the TX poll timer and TX timeout timers */ + + wd_cancel(priv->lo_polldog); + + /* Mark the device "down" */ + + priv->lo_bifup = false; + return OK; +} + +/**************************************************************************** + * Function: lo_txavail_process + * + * Description: + * Perform an out-of-cycle poll. + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Called in normal user mode + * + ****************************************************************************/ + +static inline void lo_txavail_process(FAR struct lo_driver_s *priv) +{ + /* Ignore the notification if the interface is not yet up */ + + if (priv->lo_bifup) + { + /* If so, then poll the network for new XMIT data */ + + (void)devif_poll(&priv->lo_dev, lo_txpoll); + } +} + +/**************************************************************************** + * Function: lo_txavail_work + * + * Description: + * Perform an out-of-cycle poll on the worker thread. + * + * Parameters: + * arg - Reference to the NuttX driver state structure (cast to void*) + * + * Returned Value: + * None + * + * Assumptions: + * Called on the higher priority worker thread. + * + ****************************************************************************/ + +static void lo_txavail_work(FAR void *arg) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)arg; + net_lock_t state; + + /* Perform the poll */ + + state = net_lock(); + lo_txavail_process(priv); + net_unlock(state); +} + +/**************************************************************************** + * Function: lo_txavail + * + * Description: + * Driver callback invoked when new TX data is available. This is a + * stimulus perform an out-of-cycle poll and, thereby, reduce the TX + * latency. + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Called in normal user mode + * + ****************************************************************************/ + +static int lo_txavail(FAR struct net_driver_s *dev) +{ + FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private; + + /* Is our single work structure available? It may not be if there are + * pending interrupt actions and we will have to ignore the Tx + * availability action. + */ + + if (work_available(&priv->lo_work)) + { + /* Schedule to serialize the poll on the worker thread. */ + + work_queue(HPWORK, &priv->lo_work, lo_txavail_work, priv, 0); + } + + return OK; +} + +/**************************************************************************** + * Function: lo_addmac + * + * Description: + * NuttX Callback: Add the specified MAC address to the hardware multicast + * address filtering + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * mac - The MAC address to be added + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#if defined(CONFIG_NET_IGMP) || defined(CONFIG_NET_ICMPv6) +static int lo_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac) +{ + /* There is no multicast support in the loopback driver */ + + return OK; +} +#endif + +/**************************************************************************** + * Function: lo_rmmac + * + * Description: + * NuttX Callback: Remove the specified MAC address from the hardware multicast + * address filtering + * + * Parameters: + * dev - Reference to the NuttX driver state structure + * mac - The MAC address to be removed + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IGMP +static int lo_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac) +{ + /* There is no multicast support in the loopback driver */ + + return OK; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: localhost_initialize + * + * Description: + * Initialize the Ethernet controller and driver + * + * Parameters: + * intf - In the case where there are multiple EMACs, this value + * identifies which EMAC is to be initialized. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + * Assumptions: + * + ****************************************************************************/ + +int localhost_initialize(void) +{ + FAR struct lo_driver_s *priv; + + /* Get the interface structure associated with this interface number. */ + + priv = &g_loopback; + + /* Initialize the driver structure */ + + memset(priv, 0, sizeof(struct lo_driver_s)); + priv->lo_dev.d_ifup = lo_ifup; /* I/F up (new IP address) callback */ + priv->lo_dev.d_ifdown = lo_ifdown; /* I/F down callback */ + priv->lo_dev.d_txavail = lo_txavail; /* New TX data callback */ +#ifdef CONFIG_NET_IGMP + priv->lo_dev.d_addmac = lo_addmac; /* Add multicast MAC address */ + priv->lo_dev.d_rmmac = lo_rmmac; /* Remove multicast MAC address */ +#endif + priv->lo_dev.d_private = (void*)g_loopback; /* Used to recover private state from dev */ + + /* Create a watchdog for timing polling for and timing of transmissions */ + + priv->lo_polldog = wd_create(); /* Create periodic poll timer */ + + /* Register the loopabck device with the OS so that socket IOCTLs can b + * performed. + */ + + (void)netdev_register(&priv->lo_dev, NET_LL_LOOPBACK); + + /* Set the local loopback IP address */ + +#ifdef CONFIG_NET_IPv4 + net_ipv4addr_copy(dev->d_ipaddr, g_lo_ipv4addr); + net_ipv4addr_copy(dev->d_draddr, g_lo_ipv4addr); + net_ipv4addr_copy(dev->d_netmask, g_lo_ipv4mask); +#endif +#ifdef CONFIG_NET_IPv6 + net_ipv6addr_hdrcopy(dev->d_ipv6addr, g_lo_ipv6addr) + net_ipv6addr_hdrcopy(dev->d_ipv6draddr, g_lo_ipv6addr) + net_ipv6addr_hdrcopy(dev->d_ipv6netmask, g_ipv6_allzeroaddr) +#endif + /* Put the network in the UP state */ + + return lo_ifup(&priv->lo_dev); +} + +#endif /* CONFIG_NET && CONFIG_NETDEV_LOOBACK */