Browse Source

[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>
tags/v1.20.1
Michael Brown 8 years ago
parent
commit
d0325b1da6
6 changed files with 110 additions and 8 deletions
  1. 19
    0
      src/config/fault.h
  2. 0
    1
      src/config/general.h
  3. 53
    0
      src/core/fault.c
  4. 1
    0
      src/include/ipxe/errfile.h
  5. 32
    0
      src/include/ipxe/fault.h
  6. 5
    7
      src/net/netdevice.c

+ 19
- 0
src/config/fault.h View File

@@ -0,0 +1,19 @@
1
+#ifndef CONFIG_FAULT_H
2
+#define CONFIG_FAULT_H
3
+
4
+/** @file
5
+ *
6
+ * Fault injection
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
+
12
+#include <config/defaults.h>
13
+
14
+/* Drop every N transmitted or received network packets */
15
+#define	NETDEV_DISCARD_RATE 0
16
+
17
+#include <config/local/fault.h>
18
+
19
+#endif /* CONFIG_FAULT_H */

+ 0
- 1
src/config/general.h View File

@@ -156,7 +156,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
156 156
  *
157 157
  */
158 158
 
159
-#define	NETDEV_DISCARD_RATE 0	/* Drop every N packets (0=>no drop) */
160 159
 #undef	BUILD_SERIAL		/* Include an automatic build serial
161 160
 				 * number.  Add "bs" to the list of
162 161
 				 * make targets.  For example:

+ 53
- 0
src/core/fault.c View File

@@ -0,0 +1,53 @@
1
+/*
2
+ * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ *
19
+ * You can also choose to distribute this program under the terms of
20
+ * the Unmodified Binary Distribution Licence (as given in the file
21
+ * COPYING.UBDL), provided that you have satisfied its requirements.
22
+ */
23
+
24
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
+
26
+#include <stdlib.h>
27
+#include <errno.h>
28
+#include <ipxe/fault.h>
29
+
30
+/** @file
31
+ *
32
+ * Fault injection
33
+ *
34
+ */
35
+
36
+/**
37
+ * Inject fault with a specified probability
38
+ *
39
+ * @v rate		Reciprocal of fault probability (must be non-zero)
40
+ * @ret rc		Return status code
41
+ */
42
+int inject_fault_nonzero ( unsigned int rate ) {
43
+
44
+	/* Do nothing unless we want to inject a fault now */
45
+	if ( ( random() % rate ) != 0 )
46
+		return 0;
47
+
48
+	/* Generate error number here so that faults can be injected
49
+	 * into files that don't themselves have error file
50
+	 * identifiers (via errfile.h).
51
+	 */
52
+	return -EFAULT;
53
+}

+ 1
- 0
src/include/ipxe/errfile.h View File

@@ -68,6 +68,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
68 68
 #define ERRFILE_fbcon		       ( ERRFILE_CORE | 0x001c0000 )
69 69
 #define ERRFILE_ansicol		       ( ERRFILE_CORE | 0x001d0000 )
70 70
 #define ERRFILE_ansicoldef	       ( ERRFILE_CORE | 0x001e0000 )
71
+#define ERRFILE_fault		       ( ERRFILE_CORE | 0x001f0000 )
71 72
 
72 73
 #define ERRFILE_eisa		     ( ERRFILE_DRIVER | 0x00000000 )
73 74
 #define ERRFILE_isa		     ( ERRFILE_DRIVER | 0x00010000 )

+ 32
- 0
src/include/ipxe/fault.h View File

@@ -0,0 +1,32 @@
1
+#ifndef _IPXE_FAULT_H
2
+#define _IPXE_FAULT_H
3
+
4
+/** @file
5
+ *
6
+ * Fault injection
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
+
12
+#include <config/fault.h>
13
+
14
+extern int inject_fault_nonzero ( unsigned int rate );
15
+
16
+/**
17
+ * Inject fault with a specified probability
18
+ *
19
+ * @v rate		Reciprocal of fault probability (zero for no faults)
20
+ * @ret rc		Return status code
21
+ */
22
+static inline __attribute__ (( always_inline )) int
23
+inject_fault ( unsigned int rate ) {
24
+
25
+	/* Force dead code elimination in non-fault-injecting builds */
26
+	if ( rate == 0 )
27
+		return 0;
28
+
29
+	return inject_fault_nonzero ( rate );
30
+}
31
+
32
+#endif /* _IPXE_FAULT_H */

+ 5
- 7
src/net/netdevice.c View File

@@ -39,6 +39,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
39 39
 #include <ipxe/device.h>
40 40
 #include <ipxe/errortab.h>
41 41
 #include <ipxe/profile.h>
42
+#include <ipxe/fault.h>
42 43
 #include <ipxe/vlan.h>
43 44
 #include <ipxe/netdevice.h>
44 45
 
@@ -303,11 +304,8 @@ int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
303 304
 	}
304 305
 
305 306
 	/* Discard packet (for test purposes) if applicable */
306
-	if ( ( NETDEV_DISCARD_RATE > 0 ) &&
307
-	     ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
308
-		rc = -EAGAIN;
307
+	if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
309 308
 		goto err;
310
-	}
311 309
 
312 310
 	/* Transmit packet */
313 311
 	if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
@@ -457,14 +455,14 @@ static void netdev_tx_flush ( struct net_device *netdev ) {
457 455
  * function takes ownership of the I/O buffer.
458 456
  */
459 457
 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
458
+	int rc;
460 459
 
461 460
 	DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
462 461
 		netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
463 462
 
464 463
 	/* Discard packet (for test purposes) if applicable */
465
-	if ( ( NETDEV_DISCARD_RATE > 0 ) &&
466
-	     ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
467
-		netdev_rx_err ( netdev, iobuf, -EAGAIN );
464
+	if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
465
+		netdev_rx_err ( netdev, iobuf, rc );
468 466
 		return;
469 467
 	}
470 468
 

Loading…
Cancel
Save