Browse Source

[Settings] Use a settings applicator to configure IPv4 routes.

tags/v0.9.4
Michael Brown 16 years ago
parent
commit
aec9b8a41b
7 changed files with 53 additions and 88 deletions
  1. 0
    3
      src/include/gpxe/dhcp.h
  2. 0
    5
      src/include/gpxe/ip.h
  3. 11
    0
      src/include/gpxe/netdevice.h
  4. 36
    32
      src/net/ipv4.c
  5. 4
    3
      src/net/netdevice.c
  6. 0
    43
      src/net/udp/dhcp.c
  7. 2
    2
      src/usr/dhcpmgmt.c

+ 0
- 3
src/include/gpxe/dhcp.h View File

@@ -557,7 +557,4 @@ extern int create_dhcp_response ( struct net_device *netdev, int msgtype,
557 557
 extern int start_dhcp ( struct job_interface *job, struct net_device *netdev,
558 558
 			int (*register_options) ( struct net_device *,
559 559
 						  struct dhcp_option_block * ));
560
-extern int dhcp_configure_netdev ( struct net_device *netdev,
561
-				   struct dhcp_option_block *options );
562
-
563 560
 #endif /* _GPXE_DHCP_H */

+ 0
- 5
src/include/gpxe/ip.h View File

@@ -77,9 +77,4 @@ extern struct list_head ipv4_miniroutes;
77 77
 
78 78
 extern struct net_protocol ipv4_protocol;
79 79
 
80
-extern int add_ipv4_address ( struct net_device *netdev,
81
-			      struct in_addr address, struct in_addr netmask,
82
-			      struct in_addr gateway );
83
-extern void del_ipv4_address ( struct net_device *netdev );
84
-
85 80
 #endif /* _GPXE_IP_H */

+ 11
- 0
src/include/gpxe/netdevice.h View File

@@ -341,6 +341,17 @@ netdev_priv ( struct net_device *netdev ) {
341 341
         return netdev->priv;
342 342
 }
343 343
 
344
+/**
345
+ * Get per-netdevice configuration settings block
346
+ *
347
+ * @v netdev		Network device
348
+ * @ret settings	Settings block
349
+ */
350
+static inline __attribute__ (( always_inline )) struct settings *
351
+netdev_settings ( struct net_device *netdev ) {
352
+	return &netdev->settings;
353
+}
354
+
344 355
 extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
345 356
 extern void netdev_tx_complete_err ( struct net_device *netdev,
346 357
 				 struct io_buffer *iobuf, int rc );

+ 36
- 32
src/net/ipv4.c View File

@@ -12,6 +12,8 @@
12 12
 #include <gpxe/netdevice.h>
13 13
 #include <gpxe/ip.h>
14 14
 #include <gpxe/tcpip.h>
15
+#include <gpxe/dhcp.h>
16
+#include <gpxe/settings.h>
15 17
 
16 18
 /** @file
17 19
  *
@@ -94,45 +96,47 @@ static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
94 96
 }
95 97
 
96 98
 /**
97
- * Add IPv4 interface
98
- *
99
- * @v netdev	Network device
100
- * @v address	IPv4 address
101
- * @v netmask	Subnet mask
102
- * @v gateway	Gateway address (or @c INADDR_NONE for no gateway)
103
- * @ret rc	Return status code
99
+ * Create IPv4 routing table
104 100
  *
101
+ * @ret rc		Return status code
105 102
  */
106
-int add_ipv4_address ( struct net_device *netdev, struct in_addr address,
107
-		       struct in_addr netmask, struct in_addr gateway ) {
103
+static int ipv4_create_routes ( void ) {
108 104
 	struct ipv4_miniroute *miniroute;
109
-
110
-	/* Clear any existing address for this net device */
111
-	del_ipv4_address ( netdev );
112
-
113
-	/* Add new miniroute */
114
-	miniroute = add_ipv4_miniroute ( netdev, address, netmask, gateway );
115
-	if ( ! miniroute )
116
-		return -ENOMEM;
105
+	struct ipv4_miniroute *tmp;
106
+	struct net_device *netdev;
107
+	struct settings *settings;
108
+	struct in_addr address = { 0 };
109
+	struct in_addr netmask = { 0 };
110
+	struct in_addr gateway = { INADDR_NONE };
111
+
112
+	/* Delete all existing routes */
113
+	list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
114
+		del_ipv4_miniroute ( miniroute );
115
+
116
+	/* Create a route for each configured network device */
117
+	for_each_netdev ( netdev ) {
118
+		settings = netdev_settings ( netdev );
119
+		address.s_addr = 0;
120
+		fetch_ipv4_setting ( settings, DHCP_EB_YIADDR, &address );
121
+		netmask.s_addr = 0;
122
+		fetch_ipv4_setting ( settings, DHCP_SUBNET_MASK, &netmask );
123
+		gateway.s_addr = INADDR_NONE;
124
+		fetch_ipv4_setting ( settings, DHCP_ROUTERS, &gateway );
125
+		if ( address.s_addr ) {
126
+			miniroute = add_ipv4_miniroute ( netdev, address,
127
+							 netmask, gateway );
128
+			if ( ! miniroute )
129
+				return -ENOMEM;
130
+		}
131
+	}
117 132
 
118 133
 	return 0;
119 134
 }
120 135
 
121
-/**
122
- * Remove IPv4 interface
123
- *
124
- * @v netdev	Network device
125
- */
126
-void del_ipv4_address ( struct net_device *netdev ) {
127
-	struct ipv4_miniroute *miniroute;
128
-
129
-	list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
130
-		if ( miniroute->netdev == netdev ) {
131
-			del_ipv4_miniroute ( miniroute );
132
-			break;
133
-		}
134
-	}
135
-}
136
+/** IPv4 settings applicator */
137
+struct settings_applicator ipv4_settings_applicator __settings_applicator = {
138
+	.apply = ipv4_create_routes,
139
+};
136 140
 
137 141
 /**
138 142
  * Perform IPv4 routing

+ 4
- 3
src/net/netdevice.c View File

@@ -266,7 +266,7 @@ struct net_device * alloc_netdev ( size_t priv_size ) {
266 266
 		netdev->refcnt.free = free_netdev;
267 267
 		INIT_LIST_HEAD ( &netdev->tx_queue );
268 268
 		INIT_LIST_HEAD ( &netdev->rx_queue );
269
-		settings_init ( &netdev->settings,
269
+		settings_init ( netdev_settings ( netdev ),
270 270
 				&netdev_settings_operations, &netdev->refcnt,
271 271
 				netdev->name );
272 272
 		netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
@@ -292,7 +292,8 @@ int register_netdev ( struct net_device *netdev ) {
292 292
 		   ifindex++ );
293 293
 
294 294
 	/* Register per-netdev configuration settings */
295
-	if ( ( rc = register_settings ( &netdev->settings, NULL ) ) != 0 ) {
295
+	if ( ( rc = register_settings ( netdev_settings ( netdev ),
296
+					NULL ) ) != 0 ) {
296 297
 		DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
297 298
 		       netdev, strerror ( rc ) );
298 299
 		return rc;
@@ -369,7 +370,7 @@ void unregister_netdev ( struct net_device *netdev ) {
369 370
 	netdev_close ( netdev );
370 371
 
371 372
 	/* Unregister per-netdev configuration settings */
372
-	unregister_settings ( &netdev->settings );
373
+	unregister_settings ( netdev_settings ( netdev ) );
373 374
 
374 375
 	/* Remove from device list */
375 376
 	list_del ( &netdev->list );

+ 0
- 43
src/net/udp/dhcp.c View File

@@ -1025,46 +1025,3 @@ int start_dhcp ( struct job_interface *job, struct net_device *netdev,
1025 1025
 	ref_put ( &dhcp->refcnt );
1026 1026
 	return rc;
1027 1027
 }
1028
-
1029
-/****************************************************************************
1030
- *
1031
- * Network device configurator
1032
- *
1033
- */
1034
-
1035
-/**
1036
- * Configure network device from DHCP options
1037
- *
1038
- * @v netdev		Network device
1039
- * @v options		DHCP options block
1040
- * @ret rc		Return status code
1041
- */
1042
-int dhcp_configure_netdev ( struct net_device *netdev,
1043
-			    struct dhcp_option_block *options ) {
1044
-	struct in_addr address = { 0 };
1045
-	struct in_addr netmask = { 0 };
1046
-	struct in_addr gateway = { INADDR_NONE };
1047
-	int rc;
1048
-
1049
-	/* Retrieve IP address configuration */
1050
-	find_dhcp_ipv4_option ( options, DHCP_EB_YIADDR, &address );
1051
-	find_dhcp_ipv4_option ( options, DHCP_SUBNET_MASK, &netmask );
1052
-	find_dhcp_ipv4_option ( options, DHCP_ROUTERS, &gateway );
1053
-
1054
-	/* Do nothing unless we have at least an IP address to use */
1055
-	if ( ! address.s_addr )
1056
-		return 0;
1057
-
1058
-	/* Clear any existing routing table entry */
1059
-	del_ipv4_address ( netdev );
1060
-
1061
-	/* Set up new IP address configuration */
1062
-	if ( ( rc = add_ipv4_address ( netdev, address, netmask,
1063
-				       gateway ) ) != 0 ) {
1064
-		DBG ( "Could not configure %s with DHCP results: %s\n",
1065
-		      netdev->name, strerror ( rc ) );
1066
-		return rc;
1067
-	}
1068
-
1069
-	return 0;
1070
-}

+ 2
- 2
src/usr/dhcpmgmt.c View File

@@ -32,11 +32,11 @@
32 32
  *
33 33
  */
34 34
 
35
-static int dhcp_success ( struct net_device *netdev,
35
+static int dhcp_success ( struct net_device *netdev __unused,
36 36
 			  struct dhcp_option_block *options ) {
37 37
 	DBGC ( options, "DHCP client registering options %p\n", options );
38 38
 	register_dhcp_options ( options );
39
-	return dhcp_configure_netdev ( netdev, options );
39
+	return 0;
40 40
 }
41 41
 
42 42
 int dhcp ( struct net_device *netdev ) {

Loading…
Cancel
Save