Parcourir la source

[ipv4] Redefine IP address constants to avoid unnecessary byte swapping

Redefine various IPv4 address constants and testing macros to avoid
unnecessary byte swapping at runtime, and slightly rename the macros
to prevent code from accidentally using the old definitions.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown il y a 9 ans
Parent
révision
6efcabd415
3 fichiers modifiés avec 44 ajouts et 37 suppressions
  1. 15
    9
      src/include/ipxe/in.h
  2. 8
    8
      src/net/ipv4.c
  3. 21
    20
      src/tests/ipv4_test.c

+ 15
- 9
src/include/ipxe/in.h Voir le fichier

@@ -4,6 +4,7 @@
4 4
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5 5
 
6 6
 #include <stdint.h>
7
+#include <byteswap.h>
7 8
 #include <ipxe/socket.h>
8 9
 
9 10
 /* Protocol numbers */
@@ -15,17 +16,22 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
15 16
 
16 17
 /* IP address constants */
17 18
 
18
-#define INADDR_NONE 0xffffffff
19
+#define INADDR_NONE		htonl ( 0xffffffff )
19 20
 
20
-#define INADDR_BROADCAST 0xffffffff
21
+#define INADDR_BROADCAST	htonl ( 0xffffffff )
21 22
 
22
-#define	IN_CLASSA(addr)		( ( (addr) & 0x80000000 ) == 0x00000000 )
23
-#define	IN_CLASSA_NET		0xff000000
24
-#define	IN_CLASSB(addr)		( ( (addr) & 0xc0000000 ) == 0x80000000 )
25
-#define	IN_CLASSB_NET		0xffff0000
26
-#define	IN_CLASSC(addr)		( ( (addr) & 0xe0000000 ) == 0xc0000000 )
27
-#define	IN_CLASSC_NET		0xffffff00
28
-#define IN_MULTICAST(addr)	( ( (addr) & 0xf0000000 ) == 0xe0000000 )
23
+#define	INADDR_NET_CLASSA	htonl ( 0xff000000 )
24
+#define	INADDR_NET_CLASSB	htonl ( 0xffff0000 )
25
+#define	INADDR_NET_CLASSC	htonl ( 0xffffff00 )
26
+
27
+#define	IN_IS_CLASSA( addr ) \
28
+	( ( (addr) & htonl ( 0x80000000 ) ) == htonl ( 0x00000000 ) )
29
+#define	IN_IS_CLASSB( addr ) \
30
+	( ( (addr) & htonl ( 0xc0000000 ) ) == htonl ( 0x80000000 ) )
31
+#define	IN_IS_CLASSC( addr ) \
32
+	( ( (addr) & htonl ( 0xe0000000 ) ) == htonl ( 0xc0000000 ) )
33
+#define IN_IS_MULTICAST( addr ) \
34
+	( ( (addr) & htonl ( 0xf0000000 ) ) == htonl ( 0xe0000000 ) )
29 35
 
30 36
 /**
31 37
  * IP address structure

+ 8
- 8
src/net/ipv4.c Voir le fichier

@@ -314,7 +314,7 @@ static int ipv4_tx ( struct io_buffer *iobuf,
314 314
 	if ( sin_src )
315 315
 		iphdr->src = sin_src->sin_addr;
316 316
 	if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
317
-	     ( ! IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) &&
317
+	     ( ! IN_IS_MULTICAST ( next_hop.s_addr ) ) &&
318 318
 	     ( ( miniroute = ipv4_route ( &next_hop ) ) != NULL ) ) {
319 319
 		iphdr->src = miniroute->address;
320 320
 		netmask = miniroute->netmask;
@@ -353,7 +353,7 @@ static int ipv4_tx ( struct io_buffer *iobuf,
353 353
 		/* Broadcast address */
354 354
 		ipv4_stats.out_bcast_pkts++;
355 355
 		ll_dest = netdev->ll_broadcast;
356
-	} else if ( IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) {
356
+	} else if ( IN_IS_MULTICAST ( next_hop.s_addr ) ) {
357 357
 		/* Multicast address */
358 358
 		ipv4_stats.out_mcast_pkts++;
359 359
 		if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
@@ -816,12 +816,12 @@ static int ipv4_create_routes ( void ) {
816 816
 		fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
817 817
 		/* Calculate default netmask, if necessary */
818 818
 		if ( ! netmask.s_addr ) {
819
-			if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
820
-				netmask.s_addr = htonl ( IN_CLASSA_NET );
821
-			} else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
822
-				netmask.s_addr = htonl ( IN_CLASSB_NET );
823
-			} else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
824
-				netmask.s_addr = htonl ( IN_CLASSC_NET );
819
+			if ( IN_IS_CLASSA ( address.s_addr ) ) {
820
+				netmask.s_addr = INADDR_NET_CLASSA;
821
+			} else if ( IN_IS_CLASSB ( address.s_addr ) ) {
822
+				netmask.s_addr = INADDR_NET_CLASSB;
823
+			} else if ( IN_IS_CLASSC ( address.s_addr ) ) {
824
+				netmask.s_addr = INADDR_NET_CLASSC;
825 825
 			}
826 826
 		}
827 827
 		/* Get default gateway, if present */

+ 21
- 20
src/tests/ipv4_test.c Voir le fichier

@@ -39,7 +39,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
39 39
 #include <ipxe/test.h>
40 40
 
41 41
 /** Define inline IPv4 address */
42
-#define IPV4(a,b,c,d) ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
42
+#define IPV4(a,b,c,d) \
43
+	htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
43 44
 
44 45
 /**
45 46
  * Report an inet_ntoa() test result
@@ -51,7 +52,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
51 52
  */
52 53
 static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
53 54
 			    unsigned int line ) {
54
-	struct in_addr in = { .s_addr = htonl ( addr ) };
55
+	struct in_addr in = { .s_addr = addr };
55 56
 	char *actual;
56 57
 
57 58
 	/* Format address */
@@ -81,7 +82,7 @@ static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
81 82
 	/* Parse address */
82 83
 	okx ( inet_aton ( text, &actual ) != 0, file, line );
83 84
 	DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
84
-	okx ( ntohl ( actual.s_addr ) == addr, file, line );
85
+	okx ( actual.s_addr == addr, file, line );
85 86
 };
86 87
 #define inet_aton_ok( text, addr ) \
87 88
 	inet_aton_okx ( text, addr, __FILE__, __LINE__ )
@@ -110,23 +111,23 @@ static void inet_aton_fail_okx ( const char *text, const char *file,
110 111
 static void ipv4_test_exec ( void ) {
111 112
 
112 113
 	/* Address testing macros */
113
-	ok (   IN_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
114
-	ok ( ! IN_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
115
-	ok ( ! IN_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
116
-	ok ( ! IN_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
117
-	ok (   IN_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
118
-	ok ( ! IN_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
119
-	ok ( ! IN_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
120
-	ok ( ! IN_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
121
-	ok (   IN_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
122
-	ok ( ! IN_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
123
-	ok ( ! IN_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
124
-	ok ( ! IN_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
125
-	ok ( ! IN_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
126
-	ok ( ! IN_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
127
-	ok (   IN_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
128
-	ok (   IN_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
129
-	ok (   IN_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
114
+	ok (   IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
115
+	ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
116
+	ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
117
+	ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
118
+	ok (   IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
119
+	ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
120
+	ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
121
+	ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
122
+	ok (   IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
123
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
124
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
125
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
126
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
127
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
128
+	ok (   IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
129
+	ok (   IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
130
+	ok (   IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
130 131
 
131 132
 	/* inet_ntoa() tests */
132 133
 	inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );

Chargement…
Annuler
Enregistrer