2015-08-24 19:03:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/net/loopback.c
|
|
|
|
*
|
2016-11-29 23:44:23 +01:00
|
|
|
* Copyright (C) 2015-2016 Gregory Nutt. All rights reserved.
|
2015-08-24 19:03:36 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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 <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2015-08-26 04:09:12 +02:00
|
|
|
#include <net/if.h>
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/wdog.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
2020-02-11 01:35:02 +01:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2015-08-24 19:03:36 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#include <nuttx/net/loopback.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
# include <nuttx/net/pkt.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
#ifdef CONFIG_NET_LOOPBACK
|
2017-11-05 19:25:58 +01:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
/* We need to have the work queue to handle interrupts */
|
2016-11-18 16:22:49 +01:00
|
|
|
|
|
|
|
#if !defined(CONFIG_SCHED_WORKQUEUE)
|
|
|
|
# error Worker thread support is required (CONFIG_SCHED_WORKQUEUE)
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
/* TX poll delay = 1 seconds.
|
|
|
|
* CLK_TCK is the number of clock ticks per second
|
|
|
|
*/
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
#define LO_WDDELAY (1*CLK_TCK)
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
/* This is a helper pointer for accessing the contents of the IP header */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2015-08-24 21:49:12 +02:00
|
|
|
#define IPv4BUF ((FAR struct ipv4_hdr_s *)priv->lo_dev.d_buf)
|
|
|
|
#define IPv6BUF ((FAR struct ipv6_hdr_s *)priv->lo_dev.d_buf)
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
2015-08-27 19:05:53 +02:00
|
|
|
bool lo_txdone; /* One RX packet was looped back */
|
2020-08-04 12:31:31 +02:00
|
|
|
struct wdog_s lo_polldog; /* TX poll timer */
|
2017-03-03 21:45:09 +01:00
|
|
|
struct work_s lo_work; /* For deferring poll work to the work queue */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* This holds the information visible to the NuttX network */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
struct net_driver_s lo_dev; /* Interface understood by the network */
|
2015-08-24 19:03:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct lo_driver_s g_loopback;
|
2020-02-11 01:35:02 +01:00
|
|
|
static uint8_t g_iobuffer[NET_LO_PKTSIZE + CONFIG_NET_GUARDSIZE];
|
2015-08-26 18:33:44 +02:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Polling logic */
|
|
|
|
|
|
|
|
static int lo_txpoll(FAR struct net_driver_s *dev);
|
|
|
|
static void lo_poll_work(FAR void *arg);
|
2020-08-09 20:29:35 +02:00
|
|
|
static void lo_poll_expiry(wdparm_t arg);
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/* NuttX callback functions */
|
|
|
|
|
|
|
|
static int lo_ifup(FAR struct net_driver_s *dev);
|
|
|
|
static int lo_ifdown(FAR struct net_driver_s *dev);
|
|
|
|
static void lo_txavail_work(FAR void *arg);
|
|
|
|
static int lo_txavail(FAR struct net_driver_s *dev);
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2017-09-19 23:00:46 +02:00
|
|
|
static int lo_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
|
2015-08-24 19:03:36 +02:00
|
|
|
static int lo_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_txpoll
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-08-25 00:08:17 +02:00
|
|
|
* Check if the network has any outgoing packets ready to send. This is
|
2015-08-27 19:05:53 +02:00
|
|
|
* a callback from devif_poll() or devif_timer(). devif_poll() will be
|
|
|
|
* called only during normal TX polling.
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-25 00:08:17 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-08-25 00:08:17 +02:00
|
|
|
* OK on success; a negated errno on failure
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-08-25 00:08:17 +02:00
|
|
|
* May or may not be called from an interrupt handler. In either case,
|
|
|
|
* the network is locked.
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-25 00:08:17 +02:00
|
|
|
static int lo_txpoll(FAR struct net_driver_s *dev)
|
2015-08-24 19:03:36 +02:00
|
|
|
{
|
2015-08-25 00:08:17 +02:00
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private;
|
|
|
|
|
2015-08-24 23:16:03 +02:00
|
|
|
/* Loop while there is data "sent", i.e., while d_len > 0. That should be
|
|
|
|
* the case upon entry here and while the processing of the IPv4/6 packet
|
|
|
|
* generates a new packet to be sent. Sending, of course, just means
|
|
|
|
* relaying back through the network for this driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (priv->lo_dev.d_len > 0)
|
|
|
|
{
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXPACKETS(&priv->lo_dev);
|
|
|
|
NETDEV_RXPACKETS(&priv->lo_dev);
|
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
#ifdef CONFIG_NET_PKT
|
2020-08-06 19:41:45 +02:00
|
|
|
/* When packet sockets are enabled, feed the frame into the tap */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2015-08-24 23:16:03 +02:00
|
|
|
pkt_input(&priv->lo_dev);
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
|
2015-08-24 23:16:03 +02:00
|
|
|
/* We only accept IP packets of the configured type and ARP packets */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-08-24 23:23:09 +02:00
|
|
|
if ((IPv4BUF->vhl & IP_VERSION_MASK) == IPv4_VERSION)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("IPv4 frame\n");
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXIPV4(&priv->lo_dev);
|
2015-08-24 23:23:09 +02:00
|
|
|
ipv4_input(&priv->lo_dev);
|
|
|
|
}
|
|
|
|
else
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-08-24 23:23:09 +02:00
|
|
|
if ((IPv6BUF->vtc & IP_VERSION_MASK) == IPv6_VERSION)
|
|
|
|
{
|
2020-03-03 16:11:57 +01:00
|
|
|
ninfo("IPv6 frame\n");
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXIPV6(&priv->lo_dev);
|
2015-08-24 23:23:09 +02:00
|
|
|
ipv6_input(&priv->lo_dev);
|
|
|
|
}
|
|
|
|
else
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
2015-08-24 23:23:09 +02:00
|
|
|
{
|
2017-10-24 19:23:08 +02:00
|
|
|
nwarn("WARNING: Unrecognized IP version\n");
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXDROPPED(&priv->lo_dev);
|
2015-08-24 23:23:09 +02:00
|
|
|
priv->lo_dev.d_len = 0;
|
|
|
|
}
|
2015-08-27 19:05:53 +02:00
|
|
|
|
|
|
|
priv->lo_txdone = true;
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXDONE(&priv->lo_dev);
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_poll_work
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform periodic polling from the worker thread
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* arg - The argument passed when work_queue() as called.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-08-25 00:08:17 +02:00
|
|
|
* The network is locked
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lo_poll_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)arg;
|
|
|
|
|
|
|
|
/* Perform the poll */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-08-27 19:05:53 +02:00
|
|
|
priv->lo_txdone = false;
|
2020-01-02 17:49:34 +01:00
|
|
|
devif_timer(&priv->lo_dev, LO_WDDELAY, lo_txpoll);
|
2015-08-24 23:23:09 +02:00
|
|
|
|
2015-08-27 19:05:53 +02:00
|
|
|
/* Was something received and looped back? */
|
|
|
|
|
|
|
|
while (priv->lo_txdone)
|
|
|
|
{
|
|
|
|
/* Yes, poll again for more TX data */
|
|
|
|
|
|
|
|
priv->lo_txdone = false;
|
2020-01-02 17:49:34 +01:00
|
|
|
devif_poll(&priv->lo_dev, lo_txpoll);
|
2015-08-27 19:05:53 +02:00
|
|
|
}
|
|
|
|
|
2015-08-24 23:23:09 +02:00
|
|
|
/* Setup the watchdog poll timer again */
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
wd_start(&priv->lo_polldog, LO_WDDELAY, lo_poll_expiry, (wdparm_t)priv);
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_poll_expiry
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Periodic timer handler. Called from the timer interrupt handler.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2020-08-09 20:29:35 +02:00
|
|
|
* arg - The argument
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-08-25 00:08:17 +02:00
|
|
|
* The network is locked.
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void lo_poll_expiry(wdparm_t arg)
|
2015-08-24 19:03:36 +02:00
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)arg;
|
|
|
|
|
2017-03-03 21:45:09 +01:00
|
|
|
/* Schedule to perform the interrupt processing on the worker thread. */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2017-11-05 19:25:58 +01:00
|
|
|
work_queue(LPWORK, &priv->lo_work, lo_poll_work, priv, 0);
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_ifup
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Bring up the Ethernet interface when an IP address is
|
|
|
|
* provided
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* 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
|
2016-06-12 16:31:22 +02:00
|
|
|
ninfo("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);
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2016-06-12 16:31:22 +02:00
|
|
|
ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
|
2019-09-24 18:06:42 +02:00
|
|
|
ntohs(dev->d_ipv6addr[0]), ntohs(dev->d_ipv6addr[1]),
|
|
|
|
ntohs(dev->d_ipv6addr[2]), ntohs(dev->d_ipv6addr[3]),
|
|
|
|
ntohs(dev->d_ipv6addr[4]), ntohs(dev->d_ipv6addr[5]),
|
|
|
|
ntohs(dev->d_ipv6addr[6]), ntohs(dev->d_ipv6addr[7]));
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set and activate a timer process */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->lo_polldog, LO_WDDELAY,
|
2020-08-09 20:29:35 +02:00
|
|
|
lo_poll_expiry, (wdparm_t)priv);
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
priv->lo_bifup = true;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_ifdown
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Stop the interface.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* 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 */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->lo_polldog);
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/* Mark the device "down" */
|
|
|
|
|
|
|
|
priv->lo_bifup = false;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_txavail_work
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform an out-of-cycle poll on the worker thread.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* 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;
|
|
|
|
|
2015-08-25 00:08:17 +02:00
|
|
|
/* Ignore the notification if the interface is not yet up */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-08-25 00:08:17 +02:00
|
|
|
if (priv->lo_bifup)
|
|
|
|
{
|
2015-08-27 19:05:53 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* If so, then poll the network for new XMIT data */
|
2015-08-25 00:08:17 +02:00
|
|
|
|
2015-08-27 19:05:53 +02:00
|
|
|
priv->lo_txdone = false;
|
2020-01-02 17:49:34 +01:00
|
|
|
devif_poll(&priv->lo_dev, lo_txpoll);
|
2015-08-27 19:05:53 +02:00
|
|
|
}
|
|
|
|
while (priv->lo_txdone);
|
2015-08-25 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_txavail
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* 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. */
|
|
|
|
|
2017-11-05 19:25:58 +01:00
|
|
|
work_queue(LPWORK, &priv->lo_work, lo_txavail_work, priv, 0);
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_addmac
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Add the specified MAC address to the hardware multicast
|
|
|
|
* address filtering
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
* mac - The MAC address to be added
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-08-24 19:03:36 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_rmmac
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 18:51:25 +01:00
|
|
|
* NuttX Callback: Remove the specified MAC address from the hardware
|
|
|
|
* multicast address filtering
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
* mac - The MAC address to be removed
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-08-24 19:03:36 +02:00
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: localhost_initialize
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-08-09 14:59:55 +02:00
|
|
|
* Initialize the localhost, loopback network driver
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-09 14:59:55 +02:00
|
|
|
* None
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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));
|
2015-08-26 18:33:44 +02:00
|
|
|
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 */
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-08-26 18:33:44 +02:00
|
|
|
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_buf = g_iobuffer; /* Attach the IO buffer */
|
2020-08-06 19:41:45 +02:00
|
|
|
priv->lo_dev.d_private = priv; /* Used to recover private state from dev */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/* Register the loopabck device with the OS so that socket IOCTLs can b
|
|
|
|
* performed.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
netdev_register(&priv->lo_dev, NET_LL_LOOPBACK);
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/* Set the local loopback IP address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-08-24 21:49:12 +02:00
|
|
|
net_ipv4addr_copy(priv->lo_dev.d_ipaddr, g_lo_ipv4addr);
|
|
|
|
net_ipv4addr_copy(priv->lo_dev.d_draddr, g_lo_ipv4addr);
|
|
|
|
net_ipv4addr_copy(priv->lo_dev.d_netmask, g_lo_ipv4mask);
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
2015-08-26 04:09:12 +02:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-10-04 23:04:00 +02:00
|
|
|
net_ipv6addr_copy(priv->lo_dev.d_ipv6addr, g_lo_ipv6addr);
|
|
|
|
net_ipv6addr_copy(priv->lo_dev.d_ipv6draddr, g_lo_ipv6addr);
|
2017-08-09 14:59:55 +02:00
|
|
|
net_ipv6addr_copy(priv->lo_dev.d_ipv6netmask, g_lo_ipv6mask);
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
2015-08-26 04:09:12 +02:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/* Put the network in the UP state */
|
|
|
|
|
2015-08-26 04:09:12 +02:00
|
|
|
priv->lo_dev.d_flags = IFF_UP;
|
2015-08-24 19:03:36 +02:00
|
|
|
return lo_ifup(&priv->lo_dev);
|
|
|
|
}
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
#endif /* CONFIG_NET_LOOPBACK */
|