123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <ipxe/iobuf.h>
- #include <ipxe/retry.h>
- #include <ipxe/timer.h>
- #include <ipxe/malloc.h>
- #include <ipxe/neighbour.h>
-
-
-
-
- #define NEIGHBOUR_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
-
-
- #define NEIGHBOUR_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
-
-
- struct list_head neighbours = LIST_HEAD_INIT ( neighbours );
-
- static void neighbour_expired ( struct retry_timer *timer, int over );
-
-
- static void neighbour_free ( struct refcnt *refcnt ) {
- struct neighbour *neighbour =
- container_of ( refcnt, struct neighbour, refcnt );
-
-
- assert ( list_empty ( &neighbour->tx_queue ) );
-
-
- netdev_put ( neighbour->netdev );
-
-
- free ( neighbour );
- }
-
-
- static struct neighbour * neighbour_create ( struct net_device *netdev,
- struct net_protocol *net_protocol,
- const void *net_dest ) {
- struct neighbour *neighbour;
-
-
- neighbour = zalloc ( sizeof ( *neighbour ) );
- if ( ! neighbour )
- return NULL;
- ref_init ( &neighbour->refcnt, neighbour_free );
- neighbour->netdev = netdev_get ( netdev );
- neighbour->net_protocol = net_protocol;
- memcpy ( neighbour->net_dest, net_dest,
- net_protocol->net_addr_len );
- timer_init ( &neighbour->timer, neighbour_expired, &neighbour->refcnt );
- neighbour->timer.min_timeout = NEIGHBOUR_MIN_TIMEOUT;
- neighbour->timer.max_timeout = NEIGHBOUR_MAX_TIMEOUT;
- INIT_LIST_HEAD ( &neighbour->tx_queue );
-
-
- list_add ( &neighbour->list, &neighbours );
-
- DBGC ( neighbour, "NEIGHBOUR %s %s %s created\n", netdev->name,
- net_protocol->name, net_protocol->ntoa ( net_dest ) );
- return neighbour;
- }
-
-
- static struct neighbour * neighbour_find ( struct net_device *netdev,
- struct net_protocol *net_protocol,
- const void *net_dest ) {
- struct neighbour *neighbour;
-
- list_for_each_entry ( neighbour, &neighbours, list ) {
- if ( ( neighbour->netdev == netdev ) &&
- ( neighbour->net_protocol == net_protocol ) &&
- ( memcmp ( neighbour->net_dest, net_dest,
- net_protocol->net_addr_len ) == 0 ) ) {
-
-
- list_del ( &neighbour->list );
- list_add ( &neighbour->list, &neighbours );
-
- return neighbour;
- }
- }
- return NULL;
- }
-
-
- static void neighbour_discover ( struct neighbour *neighbour,
- struct neighbour_discovery *discovery,
- const void *net_source ) {
- struct net_device *netdev = neighbour->netdev;
- struct net_protocol *net_protocol = neighbour->net_protocol;
-
-
- neighbour->discovery = discovery;
- memcpy ( neighbour->net_source, net_source,
- net_protocol->net_addr_len );
-
-
- start_timer_nodelay ( &neighbour->timer );
-
- DBGC ( neighbour, "NEIGHBOUR %s %s %s discovering via %s\n",
- netdev->name, net_protocol->name,
- net_protocol->ntoa ( neighbour->net_dest ),
- neighbour->discovery->name );
- }
-
-
- static void neighbour_discovered ( struct neighbour *neighbour,
- const void *ll_dest ) {
- struct net_device *netdev = neighbour->netdev;
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- struct net_protocol *net_protocol = neighbour->net_protocol;
- struct io_buffer *iobuf;
- int rc;
-
-
- memcpy ( neighbour->ll_dest, ll_dest, ll_protocol->ll_addr_len );
- DBGC ( neighbour, "NEIGHBOUR %s %s %s is %s %s\n", netdev->name,
- net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
- ll_protocol->name, ll_protocol->ntoa ( neighbour->ll_dest ) );
-
-
- stop_timer ( &neighbour->timer );
-
-
-
- ref_get ( &neighbour->refcnt );
- while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
- struct io_buffer, list )) != NULL){
- DBGC2 ( neighbour, "NEIGHBOUR %s %s %s transmitting deferred "
- "packet\n", netdev->name, net_protocol->name,
- net_protocol->ntoa ( neighbour->net_dest ) );
- list_del ( &iobuf->list );
- if ( ( rc = net_tx ( iobuf, netdev, net_protocol, ll_dest,
- netdev->ll_addr ) ) != 0 ) {
- DBGC ( neighbour, "NEIGHBOUR %s %s %s could not "
- "transmit deferred packet: %s\n",
- netdev->name, net_protocol->name,
- net_protocol->ntoa ( neighbour->net_dest ),
- strerror ( rc ) );
-
- }
- }
- ref_put ( &neighbour->refcnt );
- }
-
-
- static void neighbour_destroy ( struct neighbour *neighbour, int rc ) {
- struct net_device *netdev = neighbour->netdev;
- struct net_protocol *net_protocol = neighbour->net_protocol;
- struct io_buffer *iobuf;
-
-
- list_del ( &neighbour->list );
-
-
- stop_timer ( &neighbour->timer );
-
-
- while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
- struct io_buffer, list )) != NULL){
- DBGC2 ( neighbour, "NEIGHBOUR %s %s %s discarding deferred "
- "packet: %s\n", netdev->name, net_protocol->name,
- net_protocol->ntoa ( neighbour->net_dest ),
- strerror ( rc ) );
- list_del ( &iobuf->list );
- netdev_tx_err ( neighbour->netdev, iobuf, rc );
- }
-
- DBGC ( neighbour, "NEIGHBOUR %s %s %s destroyed: %s\n", netdev->name,
- net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
- strerror ( rc ) );
-
-
- ref_put ( &neighbour->refcnt );
- }
-
-
- static void neighbour_expired ( struct retry_timer *timer, int fail ) {
- struct neighbour *neighbour =
- container_of ( timer, struct neighbour, timer );
- struct net_device *netdev = neighbour->netdev;
- struct net_protocol *net_protocol = neighbour->net_protocol;
- struct neighbour_discovery *discovery =
- neighbour->discovery;
- const void *net_dest = neighbour->net_dest;
- const void *net_source = neighbour->net_source;
- int rc;
-
-
- if ( fail ) {
- neighbour_destroy ( neighbour, -ETIMEDOUT );
- return;
- }
-
-
- start_timer ( &neighbour->timer );
-
-
- if ( ( rc = discovery->tx_request ( netdev, net_protocol, net_dest,
- net_source ) ) != 0 ) {
- DBGC ( neighbour, "NEIGHBOUR %s %s %s could not transmit %s "
- "request: %s\n", netdev->name, net_protocol->name,
- net_protocol->ntoa ( neighbour->net_dest ),
- neighbour->discovery->name, strerror ( rc ) );
-
- return;
- }
- }
-
-
- int neighbour_tx ( struct io_buffer *iobuf, struct net_device *netdev,
- struct net_protocol *net_protocol, const void *net_dest,
- struct neighbour_discovery *discovery,
- const void *net_source, const void *ll_source ) {
- struct neighbour *neighbour;
-
-
- neighbour = neighbour_find ( netdev, net_protocol, net_dest );
- if ( ! neighbour ) {
- neighbour = neighbour_create ( netdev, net_protocol, net_dest );
- if ( ! neighbour )
- return -ENOMEM;
- neighbour_discover ( neighbour, discovery, net_source );
- }
-
-
-
- if ( neighbour_has_ll_dest ( neighbour ) ) {
- return net_tx ( iobuf, netdev, net_protocol, neighbour->ll_dest,
- ll_source );
- } else {
- DBGC2 ( neighbour, "NEIGHBOUR %s %s %s deferring packet\n",
- netdev->name, net_protocol->name,
- net_protocol->ntoa ( net_dest ) );
- list_add_tail ( &iobuf->list, &neighbour->tx_queue );
- return -EAGAIN;
- }
- }
-
-
- int neighbour_update ( struct net_device *netdev,
- struct net_protocol *net_protocol,
- const void *net_dest, const void *ll_dest ) {
- struct neighbour *neighbour;
-
-
- neighbour = neighbour_find ( netdev, net_protocol, net_dest );
- if ( ! neighbour )
- return -ENOENT;
-
-
- neighbour_discovered ( neighbour, ll_dest );
-
- return 0;
- }
-
-
- int neighbour_define ( struct net_device *netdev,
- struct net_protocol *net_protocol,
- const void *net_dest, const void *ll_dest ) {
- struct neighbour *neighbour;
-
-
- neighbour = neighbour_find ( netdev, net_protocol, net_dest );
- if ( ! neighbour ) {
- neighbour = neighbour_create ( netdev, net_protocol, net_dest );
- if ( ! neighbour )
- return -ENOMEM;
- }
-
-
- neighbour_discovered ( neighbour, ll_dest );
-
- return 0;
- }
-
-
- static void neighbour_flush ( struct net_device *netdev ) {
- struct neighbour *neighbour;
- struct neighbour *tmp;
-
-
- if ( ! netdev_is_open ( netdev ) ) {
- list_for_each_entry_safe ( neighbour, tmp, &neighbours, list )
- neighbour_destroy ( neighbour, -ENODEV );
- }
- }
-
-
- struct net_driver neighbour_net_driver __net_driver = {
- .name = "Neighbour",
- .notify = neighbour_flush,
- .remove = neighbour_flush,
- };
-
-
- static unsigned int neighbour_discard ( void ) {
- struct neighbour *neighbour;
-
-
- neighbour = list_last_entry ( &neighbours, struct neighbour, list );
- if ( neighbour ) {
- neighbour_destroy ( neighbour, -ENOBUFS );
- return 1;
- } else {
- return 0;
- }
- }
-
-
- struct cache_discarder neighbour_discarder __cache_discarder (CACHE_EXPENSIVE)={
- .discard = neighbour_discard,
- };
|