Ver código fonte

[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 9 anos atrás
pai
commit
d0325b1da6

+ 19
- 0
src/config/fault.h Ver arquivo

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 Ver arquivo

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

+ 53
- 0
src/core/fault.c Ver arquivo

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 Ver arquivo

68
 #define ERRFILE_fbcon		       ( ERRFILE_CORE | 0x001c0000 )
68
 #define ERRFILE_fbcon		       ( ERRFILE_CORE | 0x001c0000 )
69
 #define ERRFILE_ansicol		       ( ERRFILE_CORE | 0x001d0000 )
69
 #define ERRFILE_ansicol		       ( ERRFILE_CORE | 0x001d0000 )
70
 #define ERRFILE_ansicoldef	       ( ERRFILE_CORE | 0x001e0000 )
70
 #define ERRFILE_ansicoldef	       ( ERRFILE_CORE | 0x001e0000 )
71
+#define ERRFILE_fault		       ( ERRFILE_CORE | 0x001f0000 )
71
 
72
 
72
 #define ERRFILE_eisa		     ( ERRFILE_DRIVER | 0x00000000 )
73
 #define ERRFILE_eisa		     ( ERRFILE_DRIVER | 0x00000000 )
73
 #define ERRFILE_isa		     ( ERRFILE_DRIVER | 0x00010000 )
74
 #define ERRFILE_isa		     ( ERRFILE_DRIVER | 0x00010000 )

+ 32
- 0
src/include/ipxe/fault.h Ver arquivo

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 Ver arquivo

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

Carregando…
Cancelar
Salvar