X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=datapath%2Fvport-patch.c;h=1c4d2c5e2d3b90942d27ebdc2cf457f2e651390c;hb=f915f1a8ca180828983ef22cf2fd21b8f010b972;hp=b0ae3a070f6713c38c3761d1a65bc73653bda191;hpb=7237e4f4b6155eb4854cebc0a45fe845f0950b40;p=sliver-openvswitch.git diff --git a/datapath/vport-patch.c b/datapath/vport-patch.c index b0ae3a070..1c4d2c5e2 100644 --- a/datapath/vport-patch.c +++ b/datapath/vport-patch.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Nicira Networks. + * Copyright (c) 2010, 2011 Nicira Networks. * Distributed under the terms of the GNU GPL version 2. * * Significant portions of this file may be copied from parts of the Linux @@ -9,30 +9,29 @@ #include #include #include +#include #include "datapath.h" #include "vport.h" #include "vport-generic.h" -struct device_config { +struct patch_config { struct rcu_head rcu; + char peer_name[IFNAMSIZ]; unsigned char eth_addr[ETH_ALEN]; - unsigned int mtu; }; struct patch_vport { + struct rcu_head rcu; + char name[IFNAMSIZ]; /* Protected by RTNL lock. */ - char peer_name[IFNAMSIZ]; struct hlist_node hash_node; - /* Protected by RCU. */ - struct vport *peer; - - /* Protected by RCU. */ - struct device_config *devconf; + struct vport __rcu *peer; + struct patch_config __rcu *patchconf; }; /* Protected by RTNL lock. */ @@ -49,18 +48,18 @@ static inline struct patch_vport *patch_vport_priv(const struct vport *vport) /* RCU callback. */ static void free_config(struct rcu_head *rcu) { - struct device_config *c = container_of(rcu, struct device_config, rcu); + struct patch_config *c = container_of(rcu, struct patch_config, rcu); kfree(c); } static void assign_config_rcu(struct vport *vport, - struct device_config *new_config) + struct patch_config *new_config) { struct patch_vport *patch_vport = patch_vport_priv(vport); - struct device_config *old_config; + struct patch_config *old_config; - old_config = rcu_dereference(patch_vport->devconf); - rcu_assign_pointer(patch_vport->devconf, new_config); + old_config = rtnl_dereference(patch_vport->patchconf); + rcu_assign_pointer(patch_vport->patchconf, new_config); call_rcu(&old_config->rcu, free_config); } @@ -85,17 +84,35 @@ static void patch_exit(void) kfree(peer_table); } -static int set_config(struct vport *vport, const void *config) +static const struct nla_policy patch_policy[ODP_PATCH_ATTR_MAX + 1] = { +#ifdef HAVE_NLA_NUL_STRING + [ODP_PATCH_ATTR_PEER] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, +#endif +}; + +static int patch_set_config(struct vport *vport, const struct nlattr *options, + struct patch_config *patchconf) { struct patch_vport *patch_vport = patch_vport_priv(vport); - char peer_name[IFNAMSIZ]; + struct nlattr *a[ODP_PATCH_ATTR_MAX + 1]; + const char *peer_name; + int err; + + if (!options) + return -EINVAL; - strlcpy(peer_name, config, IFNAMSIZ); + err = nla_parse_nested(a, ODP_PATCH_ATTR_MAX, options, patch_policy); + if (err) + return err; + + if (!a[ODP_PATCH_ATTR_PEER] || VERIFY_NUL_STRING(a[ODP_PATCH_ATTR_PEER], IFNAMSIZ - 1)) + return -EINVAL; + peer_name = nla_data(a[ODP_PATCH_ATTR_PEER]); if (!strcmp(patch_vport->name, peer_name)) return -EINVAL; - strcpy(patch_vport->peer_name, peer_name); + strcpy(patchconf->peer_name, peer_name); return 0; } @@ -104,6 +121,8 @@ static struct vport *patch_create(const struct vport_parms *parms) { struct vport *vport; struct patch_vport *patch_vport; + const char *peer_name; + struct patch_config *patchconf; int err; vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms); @@ -116,46 +135,42 @@ static struct vport *patch_create(const struct vport_parms *parms) strcpy(patch_vport->name, parms->name); - err = set_config(vport, parms->config); - if (err) - goto error_free_vport; - - patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL); - if (!patch_vport->devconf) { + patchconf = kmalloc(sizeof(struct patch_config), GFP_KERNEL); + if (!patchconf) { err = -ENOMEM; goto error_free_vport; } - vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr); + err = patch_set_config(vport, parms->options, patchconf); + if (err) + goto error_free_patchconf; - /* Make the default MTU fairly large so that it doesn't become the - * bottleneck on systems using jumbo frames. */ - patch_vport->devconf->mtu = 65535; + vport_gen_rand_ether_addr(patchconf->eth_addr); - hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name)); + rcu_assign_pointer(patch_vport->patchconf, patchconf); - rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name)); + peer_name = patchconf->peer_name; + hlist_add_head(&patch_vport->hash_node, hash_bucket(peer_name)); + rcu_assign_pointer(patch_vport->peer, vport_locate(peer_name)); update_peers(patch_vport->name, vport); return vport; +error_free_patchconf: + kfree(patchconf); error_free_vport: vport_free(vport); error: return ERR_PTR(err); } -static int patch_modify(struct vport *vport, struct odp_port *port) +static void free_port_rcu(struct rcu_head *rcu) { - int err = set_config(vport, port->config); - if (!err) { - struct patch_vport *patch_vport = patch_vport_priv(vport); + struct patch_vport *patch_vport = container_of(rcu, + struct patch_vport, rcu); - hlist_del(&patch_vport->hash_node); - rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name)); - hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name)); - } - return err; + kfree((struct patch_config __force *)patch_vport->patchconf); + vport_free(vport_from_priv(patch_vport)); } static int patch_destroy(struct vport *vport) @@ -163,16 +178,42 @@ static int patch_destroy(struct vport *vport) struct patch_vport *patch_vport = patch_vport_priv(vport); update_peers(patch_vport->name, NULL); - rcu_assign_pointer(patch_vport->peer, NULL); - hlist_del(&patch_vport->hash_node); + call_rcu(&patch_vport->rcu, free_port_rcu); - synchronize_rcu(); + return 0; +} - kfree(patch_vport->devconf); - vport_free(vport); +static int patch_set_options(struct vport *vport, struct nlattr *options) +{ + struct patch_vport *patch_vport = patch_vport_priv(vport); + struct patch_config *patchconf; + int err; + + patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf), + sizeof(struct patch_config), GFP_KERNEL); + if (!patchconf) { + err = -ENOMEM; + goto error; + } + + err = patch_set_config(vport, options, patchconf); + if (err) + goto error_free; + + assign_config_rcu(vport, patchconf); + + hlist_del(&patch_vport->hash_node); + + rcu_assign_pointer(patch_vport->peer, vport_locate(patchconf->peer_name)); + hlist_add_head(&patch_vport->hash_node, hash_bucket(patchconf->peer_name)); return 0; + +error_free: + kfree(patchconf); +error: + return err; } static void update_peers(const char *name, struct vport *vport) @@ -181,37 +222,27 @@ static void update_peers(const char *name, struct vport *vport) struct patch_vport *peer_vport; struct hlist_node *node; - hlist_for_each_entry(peer_vport, node, bucket, hash_node) - if (!strcmp(peer_vport->peer_name, name)) - rcu_assign_pointer(peer_vport->peer, vport); -} - -static int patch_set_mtu(struct vport *vport, int mtu) -{ - struct patch_vport *patch_vport = patch_vport_priv(vport); - struct device_config *devconf; - - devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL); - if (!devconf) - return -ENOMEM; - - devconf->mtu = mtu; - assign_config_rcu(vport, devconf); + hlist_for_each_entry(peer_vport, node, bucket, hash_node) { + const char *peer_name; - return 0; + peer_name = rtnl_dereference(peer_vport->patchconf)->peer_name; + if (!strcmp(peer_name, name)) + rcu_assign_pointer(peer_vport->peer, vport); + } } static int patch_set_addr(struct vport *vport, const unsigned char *addr) { struct patch_vport *patch_vport = patch_vport_priv(vport); - struct device_config *devconf; + struct patch_config *patchconf; - devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL); - if (!devconf) + patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf), + sizeof(struct patch_config), GFP_KERNEL); + if (!patchconf) return -ENOMEM; - memcpy(devconf->eth_addr, addr, ETH_ALEN); - assign_config_rcu(vport, devconf); + memcpy(patchconf->eth_addr, addr, ETH_ALEN); + assign_config_rcu(vport, patchconf); return 0; } @@ -226,13 +257,15 @@ static const char *patch_get_name(const struct vport *vport) static const unsigned char *patch_get_addr(const struct vport *vport) { const struct patch_vport *patch_vport = patch_vport_priv(vport); - return rcu_dereference(patch_vport->devconf)->eth_addr; + return rcu_dereference_rtnl(patch_vport->patchconf)->eth_addr; } -static int patch_get_mtu(const struct vport *vport) +static int patch_get_options(const struct vport *vport, struct sk_buff *skb) { - const struct patch_vport *patch_vport = patch_vport_priv(vport); - return rcu_dereference(patch_vport->devconf)->mtu; + struct patch_vport *patch_vport = patch_vport_priv(vport); + struct patch_config *patchconf = rcu_dereference_rtnl(patch_vport->patchconf); + + return nla_put_string(skb, ODP_PATCH_ATTR_PEER, patchconf->peer_name); } static int patch_send(struct vport *vport, struct sk_buff *skb) @@ -253,20 +286,19 @@ static int patch_send(struct vport *vport, struct sk_buff *skb) } const struct vport_ops patch_vport_ops = { - .type = "patch", + .type = ODP_VPORT_TYPE_PATCH, .flags = VPORT_F_GEN_STATS, .init = patch_init, .exit = patch_exit, .create = patch_create, - .modify = patch_modify, .destroy = patch_destroy, - .set_mtu = patch_set_mtu, .set_addr = patch_set_addr, .get_name = patch_get_name, .get_addr = patch_get_addr, + .get_options = patch_get_options, + .set_options = patch_set_options, .get_dev_flags = vport_gen_get_dev_flags, .is_running = vport_gen_is_running, .get_operstate = vport_gen_get_operstate, - .get_mtu = patch_get_mtu, .send = patch_send, };