netdevice.h

Go to the documentation of this file.
00001 #ifndef _GPXE_NETDEVICE_H
00002 #define _GPXE_NETDEVICE_H
00003 
00004 /** @file
00005  *
00006  * Network device management
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011 
00012 #include <stdint.h>
00013 #include <gpxe/list.h>
00014 #include <gpxe/tables.h>
00015 #include <gpxe/refcnt.h>
00016 #include <gpxe/settings.h>
00017 
00018 struct io_buffer;
00019 struct net_device;
00020 struct net_protocol;
00021 struct ll_protocol;
00022 struct device;
00023 
00024 /** Maximum length of a hardware address
00025  *
00026  * The longest currently-supported link-layer address is for IPoIB.
00027  */
00028 #define MAX_HW_ADDR_LEN 8
00029 
00030 /** Maximum length of a link-layer address
00031  *
00032  * The longest currently-supported link-layer address is for IPoIB.
00033  */
00034 #define MAX_LL_ADDR_LEN 20
00035 
00036 /** Maximum length of a link-layer header
00037  *
00038  * The longest currently-supported link-layer header is for 802.11: a
00039  * 24-byte frame header plus an 8-byte 802.3 LLC/SNAP header.  (The
00040  * IPoIB link-layer pseudo-header doesn't actually include link-layer
00041  * addresses; see ipoib.c for details).
00042  */
00043 #define MAX_LL_HEADER_LEN 32
00044 
00045 /** Maximum length of a network-layer address */
00046 #define MAX_NET_ADDR_LEN 4
00047 
00048 /**
00049  * A network-layer protocol
00050  *
00051  */
00052 struct net_protocol {
00053         /** Protocol name */
00054         const char *name;
00055         /**
00056          * Process received packet
00057          *
00058          * @v iobuf     I/O buffer
00059          * @v netdev    Network device
00060          * @v ll_source Link-layer source address
00061          *
00062          * This method takes ownership of the I/O buffer.
00063          */
00064         int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
00065                        const void *ll_source );
00066         /**
00067          * Transcribe network-layer address
00068          *
00069          * @v net_addr  Network-layer address
00070          * @ret string  Human-readable transcription of address
00071          *
00072          * This method should convert the network-layer address into a
00073          * human-readable format (e.g. dotted quad notation for IPv4).
00074          *
00075          * The buffer used to hold the transcription is statically
00076          * allocated.
00077          */
00078         const char * ( *ntoa ) ( const void * net_addr );
00079         /** Network-layer protocol
00080          *
00081          * This is an ETH_P_XXX constant, in network-byte order
00082          */
00083         uint16_t net_proto;
00084         /** Network-layer address length */
00085         uint8_t net_addr_len;
00086 };
00087 
00088 /**
00089  * A link-layer protocol
00090  *
00091  */
00092 struct ll_protocol {
00093         /** Protocol name */
00094         const char *name;
00095         /**
00096          * Add link-layer header
00097          *
00098          * @v netdev            Network device
00099          * @v iobuf             I/O buffer
00100          * @v ll_dest           Link-layer destination address
00101          * @v ll_source         Source link-layer address
00102          * @v net_proto         Network-layer protocol, in network-byte order
00103          * @ret rc              Return status code
00104          */
00105         int ( * push ) ( struct net_device *netdev, struct io_buffer *iobuf,
00106                          const void *ll_dest, const void *ll_source,
00107                          uint16_t net_proto );
00108         /**
00109          * Remove link-layer header
00110          *
00111          * @v netdev            Network device
00112          * @v iobuf             I/O buffer
00113          * @ret ll_dest         Link-layer destination address
00114          * @ret ll_source       Source link-layer address
00115          * @ret net_proto       Network-layer protocol, in network-byte order
00116          * @ret rc              Return status code
00117          */
00118         int ( * pull ) ( struct net_device *netdev, struct io_buffer *iobuf,
00119                          const void **ll_dest, const void **ll_source,
00120                          uint16_t *net_proto );
00121         /**
00122          * Initialise link-layer address
00123          *
00124          * @v hw_addr           Hardware address
00125          * @v ll_addr           Link-layer address to fill in
00126          */
00127         void ( * init_addr ) ( const void *hw_addr, void *ll_addr );
00128         /**
00129          * Transcribe link-layer address
00130          *
00131          * @v ll_addr           Link-layer address
00132          * @ret string          Human-readable transcription of address
00133          *
00134          * This method should convert the link-layer address into a
00135          * human-readable format.
00136          *
00137          * The buffer used to hold the transcription is statically
00138          * allocated.
00139          */
00140         const char * ( * ntoa ) ( const void *ll_addr );
00141         /**
00142          * Hash multicast address
00143          *
00144          * @v af                Address family
00145          * @v net_addr          Network-layer address
00146          * @v ll_addr           Link-layer address to fill in
00147          * @ret rc              Return status code
00148          */
00149         int ( * mc_hash ) ( unsigned int af, const void *net_addr,
00150                             void *ll_addr );
00151         /**
00152          * Generate Ethernet-compatible compressed link-layer address
00153          *
00154          * @v ll_addr           Link-layer address
00155          * @v eth_addr          Ethernet-compatible address to fill in
00156          */
00157         int ( * eth_addr ) ( const void *ll_addr, void *eth_addr );
00158         /** Link-layer protocol
00159          *
00160          * This is an ARPHRD_XXX constant, in network byte order.
00161          */
00162         uint16_t ll_proto;
00163         /** Hardware address length */
00164         uint8_t hw_addr_len;
00165         /** Link-layer address length */
00166         uint8_t ll_addr_len;
00167         /** Link-layer header length */
00168         uint8_t ll_header_len;
00169 };
00170 
00171 /** Network device operations */
00172 struct net_device_operations {
00173         /** Open network device
00174          *
00175          * @v netdev    Network device
00176          * @ret rc      Return status code
00177          *
00178          * This method should allocate RX I/O buffers and enable
00179          * the hardware to start transmitting and receiving packets.
00180          */
00181         int ( * open ) ( struct net_device *netdev );
00182         /** Close network device
00183          *
00184          * @v netdev    Network device
00185          *
00186          * This method should stop the flow of packets, and free up
00187          * any packets that are currently in the device's TX queue.
00188          */
00189         void ( * close ) ( struct net_device *netdev );
00190         /** Transmit packet
00191          *
00192          * @v netdev    Network device
00193          * @v iobuf     I/O buffer
00194          * @ret rc      Return status code
00195          *
00196          * This method should cause the hardware to initiate
00197          * transmission of the I/O buffer.
00198          *
00199          * If this method returns success, the I/O buffer remains
00200          * owned by the net device's TX queue, and the net device must
00201          * eventually call netdev_tx_complete() to free the buffer.
00202          * If this method returns failure, the I/O buffer is
00203          * immediately released; the failure is interpreted as
00204          * "failure to enqueue buffer".
00205          *
00206          * This method is guaranteed to be called only when the device
00207          * is open.
00208          */
00209         int ( * transmit ) ( struct net_device *netdev,
00210                              struct io_buffer *iobuf );
00211         /** Poll for completed and received packets
00212          *
00213          * @v netdev    Network device
00214          *
00215          * This method should cause the hardware to check for
00216          * completed transmissions and received packets.  Any received
00217          * packets should be delivered via netdev_rx().
00218          *
00219          * This method is guaranteed to be called only when the device
00220          * is open.
00221          */
00222         void ( * poll ) ( struct net_device *netdev );
00223         /** Enable or disable interrupts
00224          *
00225          * @v netdev    Network device
00226          * @v enable    Interrupts should be enabled
00227          */
00228         void ( * irq ) ( struct net_device *netdev, int enable );
00229 };
00230 
00231 /** Network device error */
00232 struct net_device_error {
00233         /** Error status code */
00234         int rc;
00235         /** Error count */
00236         unsigned int count;
00237 };
00238 
00239 /** Maximum number of unique errors that we will keep track of */
00240 #define NETDEV_MAX_UNIQUE_ERRORS 4
00241 
00242 /** Network device statistics */
00243 struct net_device_stats {
00244         /** Count of successful completions */
00245         unsigned int good;
00246         /** Count of error completions */
00247         unsigned int bad;
00248         /** Error breakdowns */
00249         struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS];
00250 };
00251 
00252 /**
00253  * A network device
00254  *
00255  * This structure represents a piece of networking hardware.  It has
00256  * properties such as a link-layer address and methods for
00257  * transmitting and receiving raw packets.
00258  *
00259  * Note that this structure must represent a generic network device,
00260  * not just an Ethernet device.
00261  */
00262 struct net_device {
00263         /** Reference counter */
00264         struct refcnt refcnt;
00265         /** List of network devices */
00266         struct list_head list;
00267         /** List of open network devices */
00268         struct list_head open_list;
00269         /** Name of this network device */
00270         char name[8];
00271         /** Underlying hardware device */
00272         struct device *dev;
00273 
00274         /** Network device operations */
00275         struct net_device_operations *op;
00276 
00277         /** Link-layer protocol */
00278         struct ll_protocol *ll_protocol;
00279         /** Hardware address
00280          *
00281          * This is an address which is an intrinsic property of the
00282          * hardware, e.g. an address held in EEPROM.
00283          *
00284          * Note that the hardware address may not be the same length
00285          * as the link-layer address.
00286          */
00287         uint8_t hw_addr[MAX_HW_ADDR_LEN];
00288         /** Link-layer address
00289          *
00290          * This is the current link-layer address assigned to the
00291          * device.  It can be changed at runtime.
00292          */
00293         uint8_t ll_addr[MAX_LL_ADDR_LEN];
00294         /** Link-layer broadcast address */
00295         const uint8_t *ll_broadcast;
00296 
00297         /** Current device state
00298          *
00299          * This is the bitwise-OR of zero or more NETDEV_XXX constants.
00300          */
00301         unsigned int state;
00302         /** Link status code
00303          *
00304          * Zero indicates that the link is up; any other value
00305          * indicates the error preventing link-up.
00306          */
00307         int link_rc;
00308         /** Maximum packet length
00309          *
00310          * This length includes any link-layer headers.
00311          */
00312         size_t max_pkt_len;
00313         /** TX packet queue */
00314         struct list_head tx_queue;
00315         /** RX packet queue */
00316         struct list_head rx_queue;
00317         /** TX statistics */
00318         struct net_device_stats tx_stats;
00319         /** RX statistics */
00320         struct net_device_stats rx_stats;
00321 
00322         /** Configuration settings applicable to this device */
00323         struct generic_settings settings;
00324 
00325         /** Driver private data */
00326         void *priv;
00327 };
00328 
00329 /** Network device is open */
00330 #define NETDEV_OPEN 0x0001
00331 
00332 /** Network device interrupts are enabled */
00333 #define NETDEV_IRQ_ENABLED 0x0002
00334 
00335 /** Link-layer protocol table */
00336 #define LL_PROTOCOLS __table ( struct ll_protocol, "ll_protocols" )
00337 
00338 /** Declare a link-layer protocol */
00339 #define __ll_protocol  __table_entry ( LL_PROTOCOLS, 01 )
00340 
00341 /** Network-layer protocol table */
00342 #define NET_PROTOCOLS __table ( struct net_protocol, "net_protocols" )
00343 
00344 /** Declare a network-layer protocol */
00345 #define __net_protocol __table_entry ( NET_PROTOCOLS, 01 )
00346 
00347 extern struct list_head net_devices;
00348 extern struct net_device_operations null_netdev_operations;
00349 extern struct settings_operations netdev_settings_operations;
00350 
00351 /**
00352  * Initialise a network device
00353  *
00354  * @v netdev            Network device
00355  * @v op                Network device operations
00356  */
00357 static inline void netdev_init ( struct net_device *netdev,
00358                                  struct net_device_operations *op ) {
00359         netdev->op = op;
00360 }
00361 
00362 /**
00363  * Stop using a network device
00364  *
00365  * @v netdev            Network device
00366  *
00367  * Drivers should call this method immediately before the final call
00368  * to netdev_put().
00369  */
00370 static inline void netdev_nullify ( struct net_device *netdev ) {
00371         netdev->op = &null_netdev_operations;
00372 }
00373 
00374 /**
00375  * Get printable network device link-layer address
00376  *
00377  * @v netdev            Network device
00378  * @ret name            Link-layer address
00379  */
00380 static inline const char * netdev_addr ( struct net_device *netdev ) {
00381         return netdev->ll_protocol->ntoa ( netdev->ll_addr );
00382 }
00383 
00384 /** Iterate over all network devices */
00385 #define for_each_netdev( netdev ) \
00386         list_for_each_entry ( (netdev), &net_devices, list )
00387 
00388 /** There exist some network devices
00389  *
00390  * @ret existence       Existence of network devices
00391  */
00392 static inline int have_netdevs ( void ) {
00393         return ( ! list_empty ( &net_devices ) );
00394 }
00395 
00396 /**
00397  * Get reference to network device
00398  *
00399  * @v netdev            Network device
00400  * @ret netdev          Network device
00401  */
00402 static inline __attribute__ (( always_inline )) struct net_device *
00403 netdev_get ( struct net_device *netdev ) {
00404         ref_get ( &netdev->refcnt );
00405         return netdev;
00406 }
00407 
00408 /**
00409  * Drop reference to network device
00410  *
00411  * @v netdev            Network device
00412  */
00413 static inline __attribute__ (( always_inline )) void
00414 netdev_put ( struct net_device *netdev ) {
00415         ref_put ( &netdev->refcnt );
00416 }
00417 
00418 /**
00419  * Get driver private area for this network device
00420  *
00421  * @v netdev            Network device
00422  * @ret priv            Driver private area for this network device
00423  */
00424 static inline __attribute__ (( always_inline )) void *
00425 netdev_priv ( struct net_device *netdev ) {
00426         return netdev->priv;
00427 }
00428 
00429 /**
00430  * Get per-netdevice configuration settings block
00431  *
00432  * @v netdev            Network device
00433  * @ret settings        Settings block
00434  */
00435 static inline __attribute__ (( always_inline )) struct settings *
00436 netdev_settings ( struct net_device *netdev ) {
00437         return &netdev->settings.settings;
00438 }
00439 
00440 /**
00441  * Initialise a per-netdevice configuration settings block
00442  *
00443  * @v generics          Generic settings block
00444  * @v refcnt            Containing object reference counter, or NULL
00445  * @v name              Settings block name
00446  */
00447 static inline __attribute__ (( always_inline )) void
00448 netdev_settings_init ( struct net_device *netdev ) {
00449         generic_settings_init ( &netdev->settings,
00450                                 &netdev->refcnt, netdev->name );
00451         netdev->settings.settings.op = &netdev_settings_operations;
00452 }
00453 
00454 /**
00455  * Mark network device as having link up
00456  *
00457  * @v netdev            Network device
00458  */
00459 static inline __attribute__ (( always_inline )) void
00460 netdev_link_up ( struct net_device *netdev ) {
00461         netdev->link_rc = 0;
00462 }
00463 
00464 /**
00465  * Mark network device as having link down due to a specific error
00466  *
00467  * @v netdev            Network device
00468  * @v rc                Link status code
00469  */
00470 static inline __attribute__ (( always_inline )) void
00471 netdev_link_err ( struct net_device *netdev, int rc ) {
00472         netdev->link_rc = rc;
00473 }
00474 
00475 /**
00476  * Check link state of network device
00477  *
00478  * @v netdev            Network device
00479  * @ret link_up         Link is up
00480  */
00481 static inline __attribute__ (( always_inline )) int
00482 netdev_link_ok ( struct net_device *netdev ) {
00483         return ( netdev->link_rc == 0 );
00484 }
00485 
00486 /**
00487  * Check whether or not network device is open
00488  *
00489  * @v netdev            Network device
00490  * @v is_open           Network device is open
00491  */
00492 static inline __attribute__ (( always_inline )) int
00493 netdev_is_open ( struct net_device *netdev ) {
00494         return ( netdev->state & NETDEV_OPEN );
00495 }
00496 
00497 /**
00498  * Check whether or not network device interrupts are currently enabled
00499  *
00500  * @v netdev            Network device
00501  * @v irq_enabled       Network device interrupts are enabled
00502  */
00503 static inline __attribute__ (( always_inline )) int
00504 netdev_irq_enabled ( struct net_device *netdev ) {
00505         return ( netdev->state & NETDEV_IRQ_ENABLED );
00506 }
00507 
00508 extern void netdev_link_down ( struct net_device *netdev );
00509 extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
00510 extern void netdev_tx_complete_err ( struct net_device *netdev,
00511                                  struct io_buffer *iobuf, int rc );
00512 extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
00513 extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
00514 extern void netdev_rx_err ( struct net_device *netdev,
00515                             struct io_buffer *iobuf, int rc );
00516 extern void netdev_poll ( struct net_device *netdev );
00517 extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
00518 extern struct net_device * alloc_netdev ( size_t priv_size );
00519 extern int register_netdev ( struct net_device *netdev );
00520 extern int netdev_open ( struct net_device *netdev );
00521 extern void netdev_close ( struct net_device *netdev );
00522 extern void unregister_netdev ( struct net_device *netdev );
00523 extern void netdev_irq ( struct net_device *netdev, int enable );
00524 extern struct net_device * find_netdev ( const char *name );
00525 extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
00526                                                      unsigned int location );
00527 extern struct net_device * last_opened_netdev ( void );
00528 extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
00529                     struct net_protocol *net_protocol, const void *ll_dest );
00530 extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
00531                     uint16_t net_proto, const void *ll_source );
00532 
00533 /**
00534  * Complete network transmission
00535  *
00536  * @v netdev            Network device
00537  * @v iobuf             I/O buffer
00538  *
00539  * The packet must currently be in the network device's TX queue.
00540  */
00541 static inline void netdev_tx_complete ( struct net_device *netdev,
00542                                         struct io_buffer *iobuf ) {
00543         netdev_tx_complete_err ( netdev, iobuf, 0 );
00544 }
00545 
00546 /**
00547  * Complete network transmission
00548  *
00549  * @v netdev            Network device
00550  *
00551  * Completes the oldest outstanding packet in the TX queue.
00552  */
00553 static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
00554         netdev_tx_complete_next_err ( netdev, 0 );
00555 }
00556 
00557 #endif /* _GPXE_NETDEVICE_H */

Generated on Tue Apr 6 20:01:08 2010 for gPXE by  doxygen 1.5.7.1