]> git.ozlabs.org Git - petitboot/commitdiff
config: Split interface configuration from network configuration
authorJeremy Kerr <jk@ozlabs.org>
Thu, 8 Aug 2013 05:54:19 +0000 (13:54 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 8 Aug 2013 08:28:13 +0000 (16:28 +0800)
This change moves the interface configuration into its own 'struct
interface_config'. We also remove the _config suffix from the network
and interface members.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/network.c
lib/pb-config/pb-config.c
lib/pb-config/pb-config.h
lib/pb-config/storage-powerpc-nvram.c
lib/pb-config/storage-test.c

index 23bf1ee190e13070195d0a3d09cc8c2eda811a11..910649eeb742690851a11fd6c51bf17fb9d7c07f 100644 (file)
@@ -53,7 +53,7 @@ struct network {
        bool            dry_run;
 };
 
-static const struct network_config *find_config_by_hwaddr(
+static const struct interface_config *find_config_by_hwaddr(
                uint8_t *hwaddr)
 {
        const struct config *config;
@@ -63,11 +63,11 @@ static const struct network_config *find_config_by_hwaddr(
        if (!config)
                return NULL;
 
-       for (i = 0; i < config->n_network_configs; i++) {
-               struct network_config *netconf = config->network_configs[i];
+       for (i = 0; i < config->network.n_interfaces; i++) {
+               struct interface_config *ifconf = config->network.interfaces[i];
 
-               if (!memcmp(netconf->hwaddr, hwaddr, HWADDR_SIZE))
-                       return netconf;
+               if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
+                       return ifconf;
        }
 
        return NULL;
@@ -191,7 +191,7 @@ static void configure_interface_dhcp(struct network *network,
 
 static void configure_interface_static(struct network *network,
                struct interface *interface,
-               const struct network_config *config)
+               const struct interface_config *config)
 {
        const char *addr_argv[] = {
                pb_system_apps.ip,
@@ -242,7 +242,7 @@ static void configure_interface_static(struct network *network,
 static void configure_interface(struct network *network,
                struct interface *interface, bool up, bool link)
 {
-       const struct network_config *config = NULL;
+       const struct interface_config *config = NULL;
 
        if (interface->state == IFSTATE_IGNORED)
                return;
index 6398a690e4966fef69740728170f25fa7f2b2a61..5cd303b06d257e6292fc49a87f918ba24308db78 100644 (file)
@@ -13,8 +13,8 @@ static struct config_storage  *storage;
 static void config_set_defaults(struct config *config)
 {
        config->autoboot_enabled = true;
-       config->network_configs = NULL;
-       config->n_network_configs = 0;
+       config->network.interfaces = NULL;
+       config->network.n_interfaces = 0;
 }
 
 static void dump_config(struct config *config)
@@ -29,27 +29,27 @@ static void dump_config(struct config *config)
        if (config->n_network_configs > 0)
                pb_log(" network configuration:\n");
 
-       for (i = 0; i < config->n_network_configs; i++) {
-               struct network_config *netconf = config->network_configs[i];
+       for (i = 0; i < config->network.n_interfaces; i++) {
+               struct interface_config *ifconf =
+                       config->network.interfaces[i];
 
                pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
-                               netconf->hwaddr[0], netconf->hwaddr[1],
-                               netconf->hwaddr[2], netconf->hwaddr[3],
-                               netconf->hwaddr[4], netconf->hwaddr[5]);
+                               ifconf->hwaddr[0], ifconf->hwaddr[1],
+                               ifconf->hwaddr[2], ifconf->hwaddr[3],
+                               ifconf->hwaddr[4], ifconf->hwaddr[5]);
 
-               if (netconf->ignore) {
+               if (ifconf->ignore) {
                        pb_log("   ignore\n");
                        continue;
                }
 
-               if (netconf->method == CONFIG_METHOD_DHCP) {
+               if (ifconf->method == CONFIG_METHOD_DHCP) {
                        pb_log("   dhcp\n");
 
-               } else if (netconf->method == CONFIG_METHOD_STATIC) {
+               } else if (ifconf->method == CONFIG_METHOD_STATIC) {
                        pb_log("   static:\n");
-                       pb_log("    ip:  %s\n", netconf->static_config.address);
-                       pb_log("    gw:  %s\n", netconf->static_config.gateway);
-                       pb_log("    dns: %s\n", netconf->static_config.dns);
+                       pb_log("    ip:  %s\n", ifconf->static_config.address);
+                       pb_log("    gw:  %s\n", ifconf->static_config.gateway);
 
                }
        }
index c377087414dbc30c788a6d4ac88fc475e7ba8961..748b409ada3c04a5ff85602c55b33fc53291685a 100644 (file)
@@ -6,7 +6,7 @@
 
 #define HWADDR_SIZE    6
 
-struct network_config {
+struct interface_config {
        uint8_t hwaddr[HWADDR_SIZE];
        bool    ignore;
        enum {
@@ -24,10 +24,14 @@ struct network_config {
        };
 };
 
+struct network_config {
+       struct interface_config **interfaces;
+       int                     n_interfaces;
+};
+
 struct config {
        bool                    autoboot_enabled;
-       struct network_config   **network_configs;
-       int                     n_network_configs;
+       struct network_config   network;
 };
 
 
index 4b1647955996dcc17633caca9c7c655285ebd9d2..e6bf7df5544408900d5b1c24fc0f9d608a076798 100644 (file)
@@ -188,7 +188,7 @@ static const char *get_param(struct powerpc_nvram_storage *nv,
        return NULL;
 }
 
-static int parse_hwaddr(struct network_config *config, char *str)
+static int parse_hwaddr(struct interface_config *ifconf, char *str)
 {
        int i;
 
@@ -207,65 +207,76 @@ static int parse_hwaddr(struct network_config *config, char *str)
                if (endp != byte + 2)
                        return -1;
 
-               config->hwaddr[i] = x & 0xff;
+               ifconf->hwaddr[i] = x & 0xff;
        }
 
        return 0;
 }
 
-static int parse_one_network_config(struct network_config *config,
+static int parse_one_interface_config(struct config *config,
                char *confstr)
 {
+       struct interface_config *ifconf;
        char *tok, *saveptr;
 
+       ifconf = talloc(config, struct interface_config);
+
        if (!confstr || !strlen(confstr))
-               return -1;
+               goto out_err;
 
        /* first token should be the mac address */
        tok = strtok_r(confstr, ",", &saveptr);
        if (!tok)
-               return -1;
+               goto out_err;
 
-       if (parse_hwaddr(config, tok))
-               return -1;
+       if (parse_hwaddr(ifconf, tok))
+               goto out_err;
 
        /* second token is the method */
        tok = strtok_r(NULL, ",", &saveptr);
        if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
-               config->ignore = true;
-               return 0;
-       }
+               ifconf->ignore = true;
 
-       if (!strcmp(tok, "dhcp")) {
-               config->method = CONFIG_METHOD_DHCP;
+       } else if (!strcmp(tok, "dhcp")) {
+               ifconf->method = CONFIG_METHOD_DHCP;
 
        } else if (!strcmp(tok, "static")) {
-               config->method = CONFIG_METHOD_STATIC;
+               ifconf->method = CONFIG_METHOD_STATIC;
 
                /* ip/mask, [optional] gateway, [optional] dns */
                tok = strtok_r(NULL, ",", &saveptr);
                if (!tok)
-                       return -1;
-               config->static_config.address =
-                       talloc_strdup(config, tok);
+                       goto out_err;
+               ifconf->static_config.address =
+                       talloc_strdup(ifconf, tok);
 
                tok = strtok_r(NULL, ",", &saveptr);
                if (tok) {
-                       config->static_config.gateway =
-                               talloc_strdup(config, tok);
+                       ifconf->static_config.gateway =
+                               talloc_strdup(ifconf, tok);
                        tok = strtok_r(NULL, ",", &saveptr);
                }
 
                if (tok) {
-                       config->static_config.dns =
+                       ifconf->static_config.dns =
                                talloc_strdup(config, tok);
                }
        } else {
                pb_log("Unknown network configuration method %s\n", tok);
-               return -1;
+               goto out_err;
        }
 
+       config->network.interfaces = talloc_realloc(config,
+                       config->network.interfaces,
+                       struct interface_config *,
+                       ++config->network.n_interfaces);
+
+       config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
+
        return 0;
+out_err:
+       talloc_free(ifconf);
+       return -1;
 }
 
 static void populate_network_config(struct powerpc_nvram_storage *nv,
@@ -282,29 +293,14 @@ static void populate_network_config(struct powerpc_nvram_storage *nv,
        val = talloc_strdup(config, cval);
 
        for (i = 0; ; i++) {
-               struct network_config *netconf;
                char *tok, *saveptr;
-               int rc;
 
                tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
                if (!tok)
                        break;
 
-               netconf = talloc(nv, struct network_config);
-
-               rc = parse_one_network_config(netconf, tok);
-               if (rc) {
-                       talloc_free(netconf);
-                       continue;
-               }
-
-               config->network_configs = talloc_realloc(nv,
-                                               config->network_configs,
-                                               struct network_config *,
-                                               ++config->n_network_configs);
+               parse_one_interface_config(config, tok);
 
-               config->network_configs[config->n_network_configs - 1] =
-                                               netconf;
        }
 
        talloc_free(val);
index ba4e952d94ac0f64b4b26091feb646d6540544c4..bdb1d0dec2153c46d7c5a49f25dcdef13a7a6ad0 100644 (file)
@@ -7,12 +7,12 @@
 #include "pb-config.h"
 #include "storage.h"
 
-struct network_config net1 = {
+struct interface_config net1 = {
        .hwaddr = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
        .method = CONFIG_METHOD_DHCP,
 };
 
-struct network_config net2 = {
+struct interface_config net2 = {
        .hwaddr = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x56 },
        .method = CONFIG_METHOD_STATIC,
        .static_config = {
@@ -21,14 +21,16 @@ struct network_config net2 = {
        },
 };
 
-struct network_config *network_configs[] = { &net1, &net2 };
+struct interface_config *interface_configs[] = { &net1, &net2 };
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
 struct config test_config = {
        .autoboot_enabled = true,
-       .network_configs = network_configs,
-       .n_network_configs = ARRAY_SIZE(network_configs),
+       .network = {
+               .interfaces = interface_configs,
+               .n_interfaces = ARRAY_SIZE(interface_configs),
+       }
 };
 
 struct test_storage {