From 9d77f19064597afe0c2e5af73c84df57b8e9b2f5 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 28 Dec 2010 23:03:58 -0800 Subject: [PATCH] netdev: Use shash and smap functions instead of inlined substitutes. This simplifies the code and makes it easier to extend in upcoming commits. Reviewed by Justin Pettit. --- lib/netdev-provider.h | 8 +---- lib/netdev.c | 73 ++++--------------------------------------- 2 files changed, 7 insertions(+), 74 deletions(-) diff --git a/lib/netdev-provider.h b/lib/netdev-provider.h index 58d51d7b..93b6ab87 100644 --- a/lib/netdev-provider.h +++ b/lib/netdev-provider.h @@ -29,11 +29,6 @@ extern "C" { #endif -struct arg { - char *key; - char *value; -}; - /* A network device (e.g. an Ethernet device). * * This structure should be treated as opaque by network device @@ -44,8 +39,7 @@ struct netdev_dev { this device. */ int ref_cnt; /* Times this devices was opened. */ struct shash_node *node; /* Pointer to element in global map. */ - struct arg *args; /* Argument list from last config. */ - int n_args; /* Number of arguments in 'args'. */ + struct shash args; /* Argument list from last config. */ }; void netdev_dev_init(struct netdev_dev *, const char *name, diff --git a/lib/netdev.c b/lib/netdev.c index 122525a0..c676b2b8 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -191,73 +191,11 @@ netdev_enumerate_types(struct svec *types) } } -/* Compares 'args' to those used to those used by 'dev'. Returns true - * if the arguments are the same, false otherwise. Does not update the - * values stored in 'dev'. */ -static bool -compare_device_args(const struct netdev_dev *dev, const struct shash *args) -{ - const struct shash_node **new_args; - bool result = true; - int i; - - if (shash_count(args) != dev->n_args) { - return false; - } - - new_args = shash_sort(args); - for (i = 0; i < dev->n_args; i++) { - if (strcmp(dev->args[i].key, new_args[i]->name) || - strcmp(dev->args[i].value, new_args[i]->data)) { - result = false; - goto finish; - } - } - -finish: - free(new_args); - return result; -} - -static int -compare_args(const void *a_, const void *b_) -{ - const struct arg *a = a_; - const struct arg *b = b_; - return strcmp(a->key, b->key); -} - void update_device_args(struct netdev_dev *dev, const struct shash *args) { - struct shash_node *node; - int i; - - if (dev->n_args) { - for (i = 0; i < dev->n_args; i++) { - free(dev->args[i].key); - free(dev->args[i].value); - } - - free(dev->args); - dev->n_args = 0; - } - - if (!args || shash_is_empty(args)) { - return; - } - - dev->n_args = shash_count(args); - dev->args = xmalloc(dev->n_args * sizeof *dev->args); - - i = 0; - SHASH_FOR_EACH(node, args) { - dev->args[i].key = xstrdup(node->name); - dev->args[i].value = xstrdup(node->data); - i++; - } - - qsort(dev->args, dev->n_args, sizeof *dev->args, compare_args); + smap_destroy(&dev->args); + smap_clone(&dev->args, args); } /* Opens the network device named 'name' (e.g. "eth0") and returns zero if @@ -305,7 +243,7 @@ netdev_open(struct netdev_options *options, struct netdev **netdevp) update_device_args(netdev_dev, options->args); } else if (!shash_is_empty(options->args) && - !compare_device_args(netdev_dev, options->args)) { + !smap_equal(&netdev_dev->args, options->args)) { VLOG_WARN("%s: attempted to open already open netdev with " "different arguments", options->name); @@ -351,7 +289,7 @@ netdev_reconfigure(struct netdev *netdev, const struct shash *args) } if (netdev_dev->netdev_class->reconfigure) { - if (!compare_device_args(netdev_dev, args)) { + if (!smap_equal(&netdev_dev->args, args)) { update_device_args(netdev_dev, args); return netdev_dev->netdev_class->reconfigure(netdev_dev, args); } @@ -1345,6 +1283,7 @@ netdev_dev_init(struct netdev_dev *netdev_dev, const char *name, netdev_dev->netdev_class = netdev_class; netdev_dev->name = xstrdup(name); netdev_dev->node = shash_add(&netdev_dev_shash, name, netdev_dev); + shash_init(&netdev_dev->args); } /* Undoes the results of initialization. @@ -1362,7 +1301,7 @@ netdev_dev_uninit(struct netdev_dev *netdev_dev, bool destroy) assert(!netdev_dev->ref_cnt); shash_delete(&netdev_dev_shash, netdev_dev->node); - update_device_args(netdev_dev, NULL); + smap_destroy(&netdev_dev->args); if (destroy) { netdev_dev->netdev_class->destroy(netdev_dev); -- 2.30.2