]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[netdevice] Provide a test mechanism for discarding packets at random
authorMichael Brown <mcb30@ipxe.org>
Tue, 20 Jul 2010 19:52:08 +0000 (20:52 +0100)
committerMichael Brown <mcb30@ipxe.org>
Tue, 20 Jul 2010 19:58:10 +0000 (20:58 +0100)
Setting NETDEV_DISCARD_RATE to a non-zero value will cause one in
every NETDEV_DISCARD_RATE packets to be discarded at random on both
the transmit and receive datapaths, allowing the robustness of
upper-layer network protocols to be tested even in simulation
environments that provide wholly reliable packet transmission.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/config/general.h
src/net/netdevice.c

index c9bf726140bc7fff763436a1f6e04dd789d2c613..623138f55e056a957fdb70a7e53599d80e560736 100644 (file)
@@ -134,6 +134,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
  *
  */
 
+#define        NETDEV_DISCARD_RATE 0   /* Drop every N packets (0=>no drop) */
 #undef BUILD_SERIAL            /* Include an automatic build serial
                                 * number.  Add "bs" to the list of
                                 * make targets.  For example:
index 01fa1d55e93247754040e50137543f6d9fb0af65..3fa966600b4a5dacc97d437f6e280f542f5f1269 100644 (file)
@@ -24,6 +24,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #include <byteswap.h>
 #include <string.h>
 #include <errno.h>
+#include <config/general.h>
 #include <ipxe/if_ether.h>
 #include <ipxe/iobuf.h>
 #include <ipxe/tables.h>
@@ -126,13 +127,23 @@ int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
        DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
               netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
 
+       /* Enqueue packet */
        list_add_tail ( &iobuf->list, &netdev->tx_queue );
 
+       /* Avoid calling transmit() on unopened network devices */
        if ( ! netdev_is_open ( netdev ) ) {
                rc = -ENETUNREACH;
                goto err;
        }
-               
+
+       /* Discard packet (for test purposes) if applicable */
+       if ( ( NETDEV_DISCARD_RATE > 0 ) &&
+            ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
+               rc = -EAGAIN;
+               goto err;
+       }
+
+       /* Transmit packet */
        if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
                goto err;
 
@@ -218,6 +229,13 @@ void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
        DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
               netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
 
+       /* Discard packet (for test purposes) if applicable */
+       if ( ( NETDEV_DISCARD_RATE > 0 ) &&
+            ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
+               netdev_rx_err ( netdev, iobuf, -EAGAIN );
+               return;
+       }
+
        /* Enqueue packet */
        list_add_tail ( &iobuf->list, &netdev->rx_queue );