net_if.h

Go to the documentation of this file.
00001 /*##############################################################################
00002 
00003 nIP - nano IP stack
00004 
00005 File        : net_if.h
00006 
00007 Description : Network interface (header).
00008 
00009 Copyright notice:
00010 
00011 Copyright (C) 2005 -
00012 Andreas Dittrich, dittrich@informatik.hu-berlin.de
00013 Jon Kowal, kowal@informatik.hu-berlin.de
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 
00029 ##############################################################################*/
00030 
00031 #ifndef _NIP_NET_IF_H
00032  #define _NIP_NET_IF_H
00033 
00034 #include "nip_init.h"
00035 #include "mem.h"
00036 #include "os_core.h"
00037 //#include "net/ip.h"
00038 
00039 #ifndef NIP_NET_IF_COUNT
00040         /** Define number of Network Interfaces. Defaults to 1. */
00041         #define NIP_NET_IF_COUNT          1
00042 #endif
00043 
00044 #ifndef NIP_NET_IF_TRANS_COUNT
00045         /** Define maximum number of queued transmissions per interface
00046          * Defaults to 2.*/
00047         #define NIP_NET_IF_TRANS_COUNT 2
00048 #endif
00049 
00050 #define NIP_NET_MAX_TRANSMISSIONS NIP_NET_IF_TRANS_COUNT
00051 
00052 #ifndef NIP_MAX_HW_ADDR_SIZE
00053         /** Define maximum hardware address size. Defaults to 6 for ethernet addressing.*/
00054         #define NIP_MAX_HW_ADDR_SIZE      6
00055 #endif
00056 
00057 #ifndef NIP_MAX_NET_ADDR_SIZE
00058         /** Define maximum network address size. Defaults to 4 for ip addressing.*/
00059         #define NIP_MAX_NET_ADDR_SIZE     4
00060 #endif
00061 
00062 #ifndef NIP_NET_LOOPBACK_ENABLE
00063         /** Enable local loopback of network packages */
00064         #define NIP_NET_LOOPBACK_ENABLE  1
00065 #endif
00066 
00067 /** to be used as NULL indicator on transmission ids */
00068 #define NIP_NET_NULL_TRANSMISSION    NIP_NET_MAX_TRANSMISSIONS
00069 
00070 // Define type of transmission ids
00071 #if   ( NIP_NET_MAX_TRANSMISSIONS <  255   )
00072         typedef uint8_t nip_net_trans_id_t;
00073 #elif ( NIP_NET_MAX_TRANSMISSIONS <  65535 )
00074         typedef uint16_t nip_net_trans_id_t;
00075 #else
00076         typedef uint32_t nip_net_trans_id_t;
00077 #endif
00078 
00079 
00080 
00081 // Define type of Network Interface ID
00082 #if ( NIP_NET_IF_COUNT < 0xFF )
00083         typedef uint8_t nip_net_if_id_t;
00084         #define NIP_NET_NO_IF   0xFF
00085 #elif ( NIP_NET_IF_COUNT < 0xFFFF )
00086         typedef uint16_t nip_net_if_id_t;
00087         #define NIP_NET_NO_IF   0xFFFF
00088 #else
00089         typedef uint32_t nip_net_if_id_t;
00090         #define NIP_NET_NO_IF   0xFFFFFFFF
00091 #endif
00092 
00093 
00094 /** @name Physical Interface Flags
00095  *
00096  * Interface flags (IFF) are used to influence the behavior of network interfaces.
00097  * @warning The flags should be set using the nip_set_if_flags function and may
00098  * not be altered directly, as that might not be noticed by the interface.
00099  */
00100 //@{
00101 #define NIP_PHY_IFF_UP        0x01  /**< Interface is active and ready to transfer packets. */
00102 #define NIP_PHY_IFF_BROADCAST 0x02  /**< Interface allows broadasting. */
00103 #define NIP_PHY_IFF_MULTICAST 0x04  /**< Interface allows multicasting. */
00104 #define NIP_PHY_IFF_NOARP     0x08  /**< Interface does not use ARP. Hard_header method takes care of addressing. */
00105 #define NIP_PHY_IFF_LOOPBACK  0x10  /**< Interface does local loopback. \todo implement loopback! */
00106 #define NIP_PHY_IFF_CNT_OVW   0x20  /**< Interface should count receive buffer overruns. */
00107 #define NIP_PHY_IFF_CNT_RXE   0x40  /**< Interface should count receive errors. */
00108 #define NIP_PHY_IFF_CNT_TXE   0x80  /**< Interface should count transmit errors. */
00109 
00110 //@}
00111 
00112 /** @name Macros for Interface statistics */
00113 //@{
00114 
00115 /** Macro to count receive buffer overruns */
00116 #ifdef NIP_NET_IF_CNT_OVERRUN
00117         #define NIP_PHY_LOG_OVW(net_if_ptr) net_if_ptr->phy_conf.stats.cnt_overrun++
00118 #else
00119         #define NIP_PHY_LOG_OVW(net_if_ptr)
00120 #endif
00121 
00122 /** Macro to count receive errors */
00123 #ifdef NIP_NET_IF_CNT_RXE
00124         #define NIP_PHY_LOG_RXE(net_if_ptr) net_if_ptr->phy_conf.stats.cnt_rx_err++
00125 #else
00126         #define NIP_PHY_LOG_RXE(net_if_ptr)
00127 #endif
00128 
00129 /** Macro to count transmit errors */
00130 #ifdef NIP_NET_IF_CNT_TXE
00131         #define NIP_PHY_LOG_TXE(net_if_ptr) net_if_ptr->phy_conf.stats.cnt_tx_err++
00132 #else
00133         #define NIP_PHY_LOG_TXE(net_if_ptr)
00134 #endif
00135 
00136 
00137 //@}
00138 
00139 /** Type to be used when referencing IFFs */
00140 typedef uint8_t nip_phy_if_flags_t;
00141 
00142 /** \file net_if.h \todo remove Physical Interface function typedefs, if code works without these typedefs */
00143 /*typedef uint8_t nip_net_if_send( nip_net_if_id_t net_if_id );
00144 typedef uint8_t nip_net_if_receive( nip_net_if_id_t net_if_id );
00145 */
00146 
00147 /** Network Interface status */
00148 typedef enum
00149 {
00150         NIP_NET_IF_IDLE,        /**< idle -- ready to send or receive data */
00151         NIP_NET_IF_RECEIVING,   /**< busy -- receiving data */
00152         NIP_NET_IF_ROUTE,       /**< transmission waiting to be routed */
00153         NIP_NET_IF_ROUTING,     /**< transmission busy routing */
00154         NIP_NET_IF_ROUTED,      /**< transmission has been routed */
00155         NIP_NET_IF_RESOLV_ADDR, /**< waiting for hardware address to be resolved */
00156         NIP_NET_IF_RTS,         /**< ready to send, hardware address resolved */
00157         NIP_NET_IF_SENDING,     /**< busy -- sending data */
00158         NIP_NET_IF_SEND_HNF,    /**< sending data failed, Host not found, waiting to be unlocked.*/
00159         NIP_NET_IF_SEND_NRT,    /**< sending data failed, No route to host, waiting to be unlocked.*/
00160         NIP_NET_IF_DONE         /**< sending/receiving data successful, waiting to be unlocked.*/
00161 }
00162 #ifndef DOXYGEN
00163 __attribute__((packed))
00164 #endif
00165 nip_net_trans_status_t;
00166 
00167 
00168 /** Physical layer interface types.
00169  * @note Type numbers are taken from ARP section of "Assigned Numbers" RFC and
00170  * may be used for various applications (ARP, DHCP,...).
00171  */
00172 typedef enum
00173 {
00174         NIP_PHY_IF_OTHER,       /**< Other Network Interface. Use this type if non of the below apply. */
00175         NIP_PHY_IF_ETH = 1      /**< Ethernet interface */
00176 }
00177 #ifndef DOXYGEN
00178 __attribute__((packed))
00179 #endif
00180 nip_phy_if_type_t;
00181 
00182 typedef union
00183 {
00184         /** parameters for ip*/
00185         struct
00186         {
00187                 uint8_t dst_nw_addr[NIP_MAX_NET_ADDR_SIZE];  /**< network (IP) address of transmission destination. */
00188                 uint8_t src_nw_addr[NIP_MAX_NET_ADDR_SIZE];  /**< network (IP) address of transmission source. */
00189                 uint8_t ll_nw_addr[NIP_MAX_NET_ADDR_SIZE];   /**< network (IP) address of Local Link target. */
00190                 uint8_t ttl;
00191                 uint16_t id;
00192                 uint8_t protocol;
00193         }
00194         #ifndef DOXYGEN
00195         __attribute__((packed))
00196         #endif
00197         ip;
00198 
00199         /** parameters for tcp*/
00200         struct
00201         {
00202                 uint8_t  dst_nw_addr[NIP_MAX_NET_ADDR_SIZE];  /**< network (IP) address of transmission destination. */
00203                 uint8_t  src_nw_addr[NIP_MAX_NET_ADDR_SIZE];  /**< network (IP) address of transmission source. */
00204                 uint16_t dst_port;
00205                 uint16_t src_port;
00206         }
00207         #ifndef DOXYGEN
00208         __attribute__((packed))
00209         #endif
00210         tcp;
00211 
00212 
00213 
00214 }
00215 #ifndef DOXYGEN
00216 __attribute__((packed))
00217 #endif
00218 nip_net_if_trans_params_t;
00219 
00220 /** flags to define transmission behavior. */
00221 typedef struct
00222 {
00223         uint8_t free_payload:1; /**< free payload upon transmission reset */
00224         uint8_t reset_status:1; /**< reset status upon transmission reset */
00225         uint8_t ll_broadcast:1; /**< local link broadcast */
00226         uint8_t deliverlocal:1; /**< transmit packet locally */
00227         uint8_t forward:1;      /**< forward packet on transmission's interface */
00228         uint8_t unused:3;       /**< unused flag space */
00229 }
00230 #ifndef DOXYGEN
00231 __attribute__((packed))
00232 #endif
00233 nip_net_if_trans_flags_t;
00234 
00235 /** Network Interface transmission */
00236 typedef struct
00237 {
00238         nip_net_if_id_t     if_id;       /**< id of network interface to transmit on */
00239         nip_net_trans_status_t status;      /**< Transmission status */
00240         nip_time_t          time;        /**< This is needed for ARP timeouts, etc. */
00241         uint8_t             retrans_cnt; /**< Retransmission Counter */
00242         uint16_t            header_size; /**< header size */
00243         uint16_t            payload_size;/**< payload size */
00244         uint16_t            payload_off; /**< offset of payload in payload buffer */
00245         nip_mem_handle_t    header; /**< Handle to buffer containing a transmission header. */
00246         nip_mem_handle_t    payload;/**< Handle to buffer containing the payload to be transmitted. */
00247         nip_net_if_trans_params_t params;/**< transmission params */
00248         nip_net_if_trans_flags_t  flags; /**< transmission flags */
00249 }
00250 #ifndef DOXYGEN
00251 __attribute__((packed))
00252 #endif
00253 nip_net_if_trans_t;
00254 
00255 
00256 typedef uint16_t nip_net_stats_cnt_t;
00257 
00258 /** Network Interface statistics */
00259 typedef struct
00260 {
00261         #ifdef NIP_NET_IF_CNT_OVERRUN
00262                 nip_net_stats_cnt_t cnt_overrun;   /**< Number of receive buffer overruns. */
00263         #endif
00264 
00265         #ifdef NIP_NET_IF_CNT_TXE
00266                 nip_net_stats_cnt_t cnt_tx_err;    /**< Number of transmit errors. */
00267         #endif
00268 
00269         #ifdef NIP_NET_IF_CNT_RXE
00270                 nip_net_stats_cnt_t cnt_rx_err;    /**< Number of transmit errors. */
00271         #endif
00272 
00273         #ifdef NIP_NET_IF_CNT_DISCARDED
00274                 nip_net_stats_cnt_t cnt_discarded; /**< Number of discarded packets. */
00275         #endif
00276 
00277         #ifdef NIP_NET_IF_CNT_RECEIVED
00278                 nip_net_stats_cnt_t cnt_received;  /**< Number of received packets. */
00279         #endif
00280 
00281 }
00282 #ifndef DOXYGEN
00283 __attribute__((packed))
00284 #endif
00285 nip_net_if_stats_t;
00286 
00287 
00288 /** Physical layer driver interface. Some of the properties are in the style of
00289   * Linux device driver attributes and methods.
00290   * \todo add interface functions
00291   */
00292 typedef struct
00293 {
00294         nip_phy_if_type_t  type;  /**< Interface type which will be used by ARP for address resolution. */
00295         nip_net_if_stats_t stats; /**< Physical Interface statistics (packet loss, etc) */
00296         nip_phy_if_flags_t flags; /**< Physical Interface Flags  */
00297         uint8_t            hw_addr[NIP_MAX_HW_ADDR_SIZE]; /**< Hardware address */
00298         uint8_t            hw_brdcst_addr[NIP_MAX_HW_ADDR_SIZE]; /**< Hardware broadcast address */
00299         uint8_t            hw_addr_size; /** Size of hardware address */
00300         uint16_t           rx_bytes; /** bytes pending to be read from phy memory. After read-initialization this will be the packet length.*/
00301         uint16_t           rx_pos;   /** position in receive packet buffer */
00302         uint16_t           tx_bytes; /** bytes still to be sent in current transmission */
00303         uint16_t           tx_pos;   /** position in transmit packet buffer */
00304 
00305         nip_success_t (*open)( nip_net_if_id_t net_if_id ); /**< Opens network interface.
00306              * The open method should register any system resource it needs (I/O
00307                  * ports, IRQ, etc)
00308                  * @return
00309                  *   -  NIP_SUCCESS if network interface has been opened, or
00310                  *   -  NIP_ERROR if opening network interface failed.
00311                  */
00312 
00313         nip_success_t (*close)( nip_net_if_id_t net_if_id );/**< Stops network interface.
00314                  * Reverses anything that's been done at open.
00315                  * @return
00316                  *   -  NIP_SUCCESS if network interface has been closed, or
00317                  *   -  NIP_ERROR if closing network interface failed.
00318                  */
00319 
00320         nip_error_t (*hard_send_init) (nip_net_if_id_t net_if_id, void *ll_target_addr,
00321                 uint16_t type, void *daddr, void *saddr, uint16_t len);
00322                 /**< Builds hardware header and initializes packet transmission. If
00323                  * daddr is NULL, the destination address should be resolved using the
00324                  * given ll_target_addr. For ethernet devices ARP is being used.
00325                  * Ethernet device drivers can rely on nip_arp_lookup( uint8_t* addr)
00326                  * to be available. If the Address cannot be resolved locally the hardware
00327                  * specific address resolution mechanism should be initiated and the
00328                  * return value set to NIP_RESOLVING_ADDR. It's up to the caller to handle
00329                  * the return value correctly and retry transmission after a reasonable
00330                  * timeout.
00331                  * @return
00332                  *   - NIP_E_OK             transmission initialized successfully
00333                  *   - NIP_E_RESOLVING_ADDR address resolution started, or
00334                  *   - NIP_E_PARAM_FAILED   source or destination address was invalid
00335                  *   - NIP_E_HARD_ERROR     error connecting to network hardware
00336                  */
00337 
00338         uint16_t (*hard_send) (nip_net_if_id_t net_if_id, uint8_t* buffer, uint16_t count);
00339                 /**< Transmit count bytes from given buffer.
00340                  * @return
00341                  *   - number of bytes transmitted, or
00342                  *   - 0 if error occured
00343                  */
00344 
00345         uint16_t (*hard_read) (nip_net_if_id_t net_if_id, uint8_t* buffer, uint16_t count);
00346                 /**< Read data from interface and copy it into recv_buf. If size is 0 the current
00347                  * reception will be terminated, all pending bytes discarded and network interface
00348                  * reset to read next packet.
00349                  * @return number of bytes copied.
00350                  */
00351 
00352         nip_success_t (*hard_read_init) ( nip_net_if_id_t net_if_id );
00353                 /**< Initialize Reading packet from interface.
00354                  * @return packet length or 0 if packet buffer is empty.
00355                  */
00356 
00357         void (*hard_disp_recv)( void );
00358                 /**< Function to be called by dispatcher to initiate reception of data */
00359 
00360 }
00361 #ifndef DOXYGEN
00362 __attribute__((packed))
00363 #endif
00364 nip_phy_if_t;
00365 
00366 typedef enum
00367 {
00368         NIP_IP_CONF_STAT_UNCONFIGURED = 0,
00369         NIP_IP_CONF_STAT_CONFIGURED
00370 }
00371 #ifndef DOXYGEN
00372 __attribute__((packed))
00373 #endif
00374 nip_if_ip_conf_stat_t;
00375 
00376 /** Network Interface IP configuration */
00377 typedef struct nip_if_ip_conf
00378 {
00379         nip_if_ip_conf_stat_t state;
00380         uint8_t addr[4];
00381         uint8_t brdcst_addr[4];
00382         uint8_t subnet_mask[4];
00383         uint8_t std_gateway[4];
00384         #if NIP_DNS_ENABLE == 1
00385         uint8_t name_server[4];
00386         #endif
00387 }
00388 #ifndef DOXYGEN
00389 __attribute__((packed))
00390 #endif
00391 nip_if_ip_conf_t;
00392 
00393 /** Network Interface */
00394 typedef struct nip_net_if
00395 {
00396         nip_net_if_id_t    id;       /**< Network Interface ID */
00397         nip_if_ip_conf_t   ip_conf;  /**< Network Interface IP configuration */
00398         nip_phy_if_t       phy_conf; /**< Physical layer interface */
00399 }
00400 #ifndef DOXYGEN
00401 __attribute__((packed))
00402 #endif
00403 nip_net_if_t;
00404 
00405 #define NIP_IP_ADDR( if_id ) nip_net_if_list[if_id].ip_conf.addr
00406 
00407 extern nip_net_if_t nip_net_if_list[];
00408 extern nip_net_if_trans_t nip_net_trans_list[];
00409 
00410 
00411 nip_net_if_id_t nip_net_if_create( nip_phy_if_type_t type );
00412 void            nip_net_if_free  ( nip_net_if_id_t id );
00413 nip_success_t   nip_set_if_flags ( nip_net_if_id_t id, nip_phy_if_flags_t flags);
00414 nip_net_if_t   *nip_net_if_ptr   ( nip_net_if_id_t );
00415 nip_success_t   nip_net_if_up    ( nip_net_if_id_t );
00416 nip_success_t   nip_net_if_down  ( nip_net_if_id_t );
00417 void nip_reset_trans( nip_net_if_trans_t *trans );
00418 
00419 #endif /* _NIP_NET_IF_H */
00420 

Generated on Thu Jul 10 01:09:29 2008 for NIP by  doxygen 1.5.5