[fault] Generalise NETDEV_DISCARD_RATE fault injection mechanism

Provide a generic inject_fault() function that can be used to inject
random faults with configurable probabilities.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2015-07-22 02:56:49 +01:00
parent 9546b0c17b
commit d0325b1da6
6 changed files with 110 additions and 8 deletions

View File

@@ -39,6 +39,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/device.h>
#include <ipxe/errortab.h>
#include <ipxe/profile.h>
#include <ipxe/fault.h>
#include <ipxe/vlan.h>
#include <ipxe/netdevice.h>
@@ -303,11 +304,8 @@ int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
}
/* Discard packet (for test purposes) if applicable */
if ( ( NETDEV_DISCARD_RATE > 0 ) &&
( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
rc = -EAGAIN;
if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
goto err;
}
/* Transmit packet */
if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
@@ -457,14 +455,14 @@ static void netdev_tx_flush ( struct net_device *netdev ) {
* function takes ownership of the I/O buffer.
*/
void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
int rc;
DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
netdev->name, 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 );
if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
netdev_rx_err ( netdev, iobuf, rc );
return;
}