Skip to content
netdev.h 14.7 KiB
Newer Older
patacongo's avatar
patacongo committed
/****************************************************************************
 * include/nuttx/net/netdev.h
 * Defines architecture-specific device driver interfaces to the uIP network.
patacongo's avatar
patacongo committed
 *
 *   Copyright (C) 2007, 2009, 2011-2015 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
patacongo's avatar
patacongo committed
 *
 * Derived largely from portions of uIP with has a similar BSD-styple license:
 *
 *   Copyright (c) 2001-2003, Adam Dunkels.
 *   All rights reserved.
 *
 * 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.
 *
 ****************************************************************************/

#ifndef __INCLUDE_NUTTX_NET_NETDEV_H
#define __INCLUDE_NUTTX_NET_NETDEV_H

/****************************************************************************
 * Included Files
 ****************************************************************************/
patacongo's avatar
patacongo committed

#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdint.h>
patacongo's avatar
patacongo committed
#include <net/if.h>
patacongo's avatar
patacongo committed

#include <net/ethernet.h>
#include <arpa/inet.h>

patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_IGMP
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed

#include <nuttx/net/netconfig.h>
patacongo's avatar
patacongo committed

/****************************************************************************
 * Pre-processor Definitions
patacongo's avatar
patacongo committed
 ****************************************************************************/

/****************************************************************************
 * Public Types
 ****************************************************************************/

/* This structure collects information that is specific to a specific network
 * interface driver.  If the hardware platform supports only a single instance
 * of this structure.
 */

struct net_driver_s
  /* This link is used to maintain a single-linked list of ethernet drivers.
   * Must be the first field in the structure due to blink type casting.
   */

#if CONFIG_NSOCKET_DESCRIPTORS > 0
  FAR struct net_driver_s *flink;

  /* This is the name of network device assigned when netdev_register was called.
   * This name is only used to support socket ioctl lookups by device name
   * Examples: "eth0"
   */

  char d_ifname[IFNAMSIZ];
#endif

  /* Drivers interface flags.  See IFF_* definitions in include/net/if.h */

  uint8_t d_flags;

#ifdef CONFIG_NET_MULTILINK
  /* Multi network devices using multiple data links protocols are selected */

  uint8_t d_lltype;         /* See enum net_datalink_e */
  uint8_t d_llhdrlen;       /* Link layer header size */
  uint16_t d_mtu;           /* Maximum packet size */
#ifdef CONFIG_NET_TCP
  uint16_t d_recvwndo;      /* TCP receive window size */
#endif
#ifdef CONFIG_NET_ETHERNET
  struct ether_addr d_mac;  /* Device MAC address */
  net_ipaddr_t d_ipaddr;    /* Host IP address assigned to the network interface */
  net_ipaddr_t d_draddr;    /* Default router IP address */
  net_ipaddr_t d_netmask;   /* Network subnet mask */
patacongo's avatar
patacongo committed
  /* The d_buf array is used to hold incoming and outgoing packets. The device
   * driver should place incoming data into this buffer. When sending data,
   * the device driver should read the link level headers and the TCP/IP
   * headers from this buffer. The size of the link level headers is
   * configured by the NET_LL_HDRLEN(dev) define.
patacongo's avatar
patacongo committed
   * uIP will handle only a single buffer for both incoming and outgoing
   * packets.  However, the drive design may be concurrently send and
   * filling separate, break-off buffers if CONFIG_NET_MULTIBUFFER is
   * defined.  That buffer management must be controlled by the driver.
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_MULTIBUFFER
  uint8_t *d_buf;
#else
  uint8_t d_buf[MAX_NET_DEV_MTU + CONFIG_NET_GUARDSIZE];
patacongo's avatar
patacongo committed
#endif

  /* d_appdata points to the location where application data can be read from
   * or written into a packet.
   */

  uint8_t *d_appdata;

  /* This is a pointer into d_buf where a user application may append
   * data to be sent.
   */

  uint8_t *d_snddata;
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCPURGDATA
  /* This pointer points to any urgent TCP data that has been received. Only
   * present if compiled with support for urgent data (CONFIG_NET_TCPURGDATA).
   */

  uint8_t *d_urgdata;
patacongo's avatar
patacongo committed

  /* Length of the (received) urgent data */

  uint16_t d_urglen;
patacongo's avatar
patacongo committed
#endif

/* The length of the packet in the d_buf buffer.
 *
 * Holds the length of the packet in the d_buf buffer.
 *
 * When the network device driver calls the uIP input function,
 * d_len should be set to the length of the packet in the d_buf
 * buffer.
 *
 * When sending packets, the device driver should use the contents of
 * the d_len variable to determine the length of the outgoing
 * packet.
 */

  uint16_t d_len;
  /* When d_buf contains outgoing xmit data, d_sndlen is non-zero and represents
   * the amount of application data after d_snddata
  uint16_t d_sndlen;
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  /* IGMP group list */

patacongo's avatar
patacongo committed
  sq_queue_t grplist;
patacongo's avatar
patacongo committed
#endif

patacongo's avatar
patacongo committed
  /* Driver callbacks */

  int (*d_ifup)(FAR struct net_driver_s *dev);
  int (*d_ifdown)(FAR struct net_driver_s *dev);
  int (*d_txavail)(FAR struct net_driver_s *dev);
  int (*d_rxavail)(FAR struct net_driver_s *dev);
#ifdef CONFIG_NET_IGMP
  int (*d_addmac)(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
  int (*d_rmmac)(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
  int (*d_ioctl)(FAR struct net_driver_s *dev, int cmd, long arg);
patacongo's avatar
patacongo committed

  /* Drivers may attached device-specific, private information */

  void *d_private;
typedef int (*devif_poll_callback_t)(FAR struct net_driver_s *dev);
/****************************************************************************
 * Public Variables
 ****************************************************************************/

/****************************************************************************
patacongo's avatar
patacongo committed
 * Public Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * uIP device driver functions
patacongo's avatar
patacongo committed
 *
 * These functions are used by a network device driver for interacting
 * with uIP.
 *
 * Process an incoming IP packet.
patacongo's avatar
patacongo committed
 *
 * This function should be called when the device driver has received
 * a packet from the network. The packet from the device driver must
 * be present in the d_buf buffer, and the length of the packet
 * should be placed in the d_len field.
patacongo's avatar
patacongo committed
 *
 * When the function returns, there may be an outbound packet placed
 * in the d_buf packet buffer. If so, the d_len field is set to
patacongo's avatar
patacongo committed
 * the length of the packet. If no packet is to be sent out, the
 * d_len field is set to 0.
patacongo's avatar
patacongo committed
 *
 * The usual way of calling the function is presented by the source
 * code below.
 *
 *     dev->d_len = devicedriver_poll();
 *         ipv4_input(dev);
 *         if (dev->d_len > 0)
 *           {
 *             devicedriver_send();
 *           }
patacongo's avatar
patacongo committed
 *       }
 *
 * Note: If you are writing a uIP device driver that needs ARP
 * (Address Resolution Protocol), e.g., when running uIP over
 * Ethernet, you will need to call the uIP ARP code before calling
 * this function:
 *
 *     #define BUF ((struct eth_hdr_s *)&dev->d_buf[0])
 *     dev->d_len = ethernet_devicedrver_poll();
Gregory Nutt's avatar
Gregory Nutt committed
 *         if (BUF->type == HTONS(ETHTYPE_IP))
 *             ipv4_input(dev);
Gregory Nutt's avatar
Gregory Nutt committed
 *         else if (BUF->type == HTONS(ETHTYPE_ARP))
 *             if (dev->d_len > 0)
 *               {
 *                 devicedriver_send();
 *               }
 *           }
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

#ifdef CONFIG_NET_IPv4
int ipv4_input(FAR struct net_driver_s *dev);
#endif

#ifdef CONFIG_NET_IPv6
int ipv6_input(FAR struct net_driver_s *dev);
#endif
patacongo's avatar
patacongo committed

/****************************************************************************
 * Polling of connections
patacongo's avatar
patacongo committed
 *
 * These functions will traverse each active uIP connection structure and
 * perform appropriate operations:  devif_timer() will perform TCP timer
 * operations (and UDP polling operations); devif_poll() will perform TCP
 * and UDP polling operations. The CAN driver MUST implement logic to
 * periodically call devif_timer(); devif_poll() may be called asynchronously
 * from the network driver can accept another outgoing packet.
patacongo's avatar
patacongo committed
 *
 * In both cases, these functions will call the provided callback function
 * for every active connection. Polling will continue until all connections
 * have been polled or until the user-supplied function returns a non-zero
 * value (which it should do only if it cannot accept further write data).
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 * When the callback function is called, there may be an outbound packet
 * waiting for service in the uIP packet buffer, and if so the d_len field
 * is set to a value larger than zero. The device driver should then send
 * out the packet.
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 * Example:
 *   int driver_callback(FAR struct net_driver_s *dev)
patacongo's avatar
patacongo committed
 *   {
 *     if (dev->d_len > 0)
patacongo's avatar
patacongo committed
 *         devicedriver_send();
 *         return 1; <-- Terminates polling if necessary
patacongo's avatar
patacongo committed
 *       }
patacongo's avatar
patacongo committed
 *     return 0;
 *   }
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 *   ...
 *   devif_poll(dev, driver_callback);
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 * Note: If you are writing a uIP device driver that needs ARP (Address
 * Resolution Protocol), e.g., when running uIP over Ethernet, you will
 * need to call the arp_out() function in the callback function
patacongo's avatar
patacongo committed
 * before sending the packet:
patacongo's avatar
patacongo committed
 *
 *   int driver_callback(FAR struct net_driver_s *dev)
patacongo's avatar
patacongo committed
 *   {
 *     if (dev->d_len > 0)
patacongo's avatar
patacongo committed
 *         devicedriver_send();
 *         return 1; <-- Terminates polling if necessary
patacongo's avatar
patacongo committed
 *       }
patacongo's avatar
patacongo committed
 *     return 0;
 *   }
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

int devif_poll(FAR struct net_driver_s *dev, devif_poll_callback_t callback);
int devif_timer(FAR struct net_driver_s *dev, devif_poll_callback_t callback, int hsec);
/****************************************************************************
 * Carrier detection
 *
 * Call netdev_carrier_on when the carrier has become available and the device
 * is ready to receive/transmit packets.
 *
Gregory Nutt's avatar
Gregory Nutt committed
 * Call detdev_carrier_off when the carrier disappeared and the device has
 * moved into non operational state.
 *
 ****************************************************************************/
int netdev_carrier_on(FAR struct net_driver_s *dev);
int netdev_carrier_off(FAR struct net_driver_s *dev);
patacongo's avatar
patacongo committed

/****************************************************************************
 * Name: net_chksum
patacongo's avatar
patacongo committed
 *
 * Description:
 *   Calculate the Internet checksum over a buffer.
patacongo's avatar
patacongo committed
 *
 *   The Internet checksum is the one's complement of the one's complement
 *   sum of all 16-bit words in the buffer.
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 *
 *   If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
 *   provided by architecture-specific logic.
patacongo's avatar
patacongo committed
 *
 * Input Parameters:
patacongo's avatar
patacongo committed
 *
 *   buf - A pointer to the buffer over which the checksum is to be computed.
patacongo's avatar
patacongo committed
 *
 *   len - The length of the buffer over which the checksum is to be computed.
patacongo's avatar
patacongo committed
 *
 * Returned Value:
 *   The Internet checksum of the buffer.
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

uint16_t net_chksum(FAR uint16_t *data, uint16_t len);
patacongo's avatar
patacongo committed

/****************************************************************************
 * Name: net_incr32
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 *
 *   Carry out a 32-bit addition.
 *
 *   By defining CONFIG_NET_ARCH_INCR32, the architecture can replace
 *   net_incr32 with hardware assisted solutions.
 *
 * Input Parameters:
 *   op32 - A pointer to a 4-byte array representing a 32-bit integer in
 *          network byte order (big endian).  This value may not be word
 *          aligned. The value pointed to by op32 is modified in place
 *
 *   op16 - A 16-bit integer in host byte order.
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

void net_incr32(FAR uint8_t *op32, uint16_t op16);
patacongo's avatar
patacongo committed

/****************************************************************************
patacongo's avatar
patacongo committed
 *
 *   Calculate the IPv4 header checksum of the packet header in d_buf.
patacongo's avatar
patacongo committed
 *
 *   The IPv4 header checksum is the Internet checksum of the 20 bytes of
 *   the IPv4 header.
patacongo's avatar
patacongo committed
 *
 *   If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
 *   provided by architecture-specific logic.
 *
 * Returned Value:
 *   The IPv4 header checksum of the IPv4 header in the d_buf buffer.
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

#ifdef CONFIG_NET_IPv4
uint16_t ipv4_chksum(FAR struct net_driver_s *dev);
#endif

/****************************************************************************
 * Name: ipv6_chksum
 *
 * Description:
 *   Calculate the IPv6 header checksum of the packet header in d_buf.
 *
 *   The IPv6 header checksum is the Internet checksum of the 40 bytes of
 *   the IPv6 header.
 *
 *   If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
 *   provided by architecture-specific logic.
 *
 * Returned Value:
 *   The IPv6 header checksum of the IPv6 header in the d_buf buffer.
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

#ifdef CONFIG_NET_IPv6
uint16_t ipv6_chksum(FAR struct net_driver_s *dev);
#endif
#endif /* __INCLUDE_NUTTX_NET_NETDEV_H */