X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=datapath%2Fdatapath.c;h=218d1080fecadedcdd5c08862722c6e8b6faa197;hb=23cad98c869ef5b7682f68c41db6403c1e855f5e;hp=2be0ce12c6669dee6970c0e632a9edd6c93628f0;hpb=dd851cbbcc5a1b3c8ad5c10a47ddca1c510c1879;p=openvswitch diff --git a/datapath/datapath.c b/datapath/datapath.c index 2be0ce12..218d1080 100644 --- a/datapath/datapath.c +++ b/datapath/datapath.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2007, 2008, 2009, 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 @@ -53,8 +53,6 @@ #include "table.h" #include "vport-internal_dev.h" -#include "compat.h" - int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd); EXPORT_SYMBOL(dp_ioctl_hook); @@ -96,7 +94,14 @@ static struct datapath *get_dp_locked(int dp_idx) static struct tbl *get_table_protected(struct datapath *dp) { - return rcu_dereference_protected(dp->table, lockdep_is_held(&dp->mutex)); + return rcu_dereference_protected(dp->table, + lockdep_is_held(&dp->mutex)); +} + +static struct vport *get_vport_protected(struct datapath *dp, u16 port_no) +{ + return rcu_dereference_protected(dp->ports[port_no], + lockdep_is_held(&dp->mutex)); } /* Must be called with rcu_read_lock or RTNL lock. */ @@ -120,7 +125,7 @@ static int dp_fill_ifinfo(struct sk_buff *skb, const struct vport *port, int event, unsigned int flags) { - const struct datapath *dp = port->dp; + struct datapath *dp = port->dp; int ifindex = vport_get_ifindex(port); int iflink = vport_get_iflink(port); struct ifinfomsg *hdr; @@ -146,7 +151,7 @@ static int dp_fill_ifinfo(struct sk_buff *skb, NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port)); NLA_PUT_U32(skb, IFLA_MASTER, - vport_get_ifindex(rtnl_dereference(dp->ports[ODPP_LOCAL]))); + vport_get_ifindex(get_vport_protected(dp, ODPP_LOCAL))); NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port)); #ifdef IFLA_OPERSTATE NLA_PUT_U8(skb, IFLA_OPERSTATE, @@ -218,7 +223,7 @@ static int create_dp(int dp_idx, const char __user *devnamep) goto err; } } else { - snprintf(devname, sizeof devname, "of%d", dp_idx); + snprintf(devname, sizeof(devname), "of%d", dp_idx); } rtnl_lock(); @@ -235,11 +240,12 @@ static int create_dp(int dp_idx, const char __user *devnamep) goto err_put_module; err = -ENOMEM; - dp = kzalloc(sizeof *dp, GFP_KERNEL); + dp = kzalloc(sizeof(*dp), GFP_KERNEL); if (dp == NULL) goto err_put_module; INIT_LIST_HEAD(&dp->port_list); mutex_init(&dp->mutex); + mutex_lock(&dp->mutex); dp->dp_idx = dp_idx; for (i = 0; i < DP_N_QUEUES; i++) skb_queue_head_init(&dp->queues[i]); @@ -278,16 +284,18 @@ static int create_dp(int dp_idx, const char __user *devnamep) rcu_assign_pointer(dps[dp_idx], dp); dp_sysfs_add_dp(dp); + mutex_unlock(&dp->mutex); mutex_unlock(&dp_mutex); rtnl_unlock(); return 0; err_destroy_local_port: - dp_detach_port(dp->ports[ODPP_LOCAL]); + dp_detach_port(get_vport_protected(dp, ODPP_LOCAL)); err_destroy_table: - tbl_destroy(dp->table, NULL); + tbl_destroy(get_table_protected(dp), NULL); err_free_dp: + mutex_unlock(&dp->mutex); kfree(dp); err_put_module: module_put(THIS_MODULE); @@ -298,52 +306,54 @@ err: return err; } -static void do_destroy_dp(struct datapath *dp) +static void destroy_dp_rcu(struct rcu_head *rcu) { - struct vport *p, *n; + struct datapath *dp = container_of(rcu, struct datapath, rcu); int i; - list_for_each_entry_safe (p, n, &dp->port_list, node) - if (p->port_no != ODPP_LOCAL) - dp_detach_port(p); - - dp_sysfs_del_dp(dp); - - rcu_assign_pointer(dps[dp->dp_idx], NULL); - - dp_detach_port(dp->ports[ODPP_LOCAL]); - - tbl_destroy(dp->table, flow_free_tbl); - for (i = 0; i < DP_N_QUEUES; i++) skb_queue_purge(&dp->queues[i]); + + tbl_destroy((struct tbl __force *)dp->table, flow_free_tbl); free_percpu(dp->stats_percpu); kobject_put(&dp->ifobj); - module_put(THIS_MODULE); } static int destroy_dp(int dp_idx) { struct datapath *dp; - int err; + int err = 0; + struct vport *p, *n; rtnl_lock(); mutex_lock(&dp_mutex); dp = get_dp(dp_idx); - err = -ENODEV; - if (!dp) - goto err_unlock; + if (!dp) { + err = -ENODEV; + goto out; + } - do_destroy_dp(dp); - err = 0; + mutex_lock(&dp->mutex); -err_unlock: + list_for_each_entry_safe (p, n, &dp->port_list, node) + if (p->port_no != ODPP_LOCAL) + dp_detach_port(p); + + dp_sysfs_del_dp(dp); + rcu_assign_pointer(dps[dp->dp_idx], NULL); + dp_detach_port(get_vport_protected(dp, ODPP_LOCAL)); + + mutex_unlock(&dp->mutex); + call_rcu(&dp->rcu, destroy_dp_rcu); + module_put(THIS_MODULE); + +out: mutex_unlock(&dp_mutex); rtnl_unlock(); return err; } -/* Called with RTNL lock and dp_mutex. */ +/* Called with RTNL lock and dp->mutex. */ static int new_vport(struct datapath *dp, struct odp_port *odp_port, int port_no) { struct vport_parms parms; @@ -379,7 +389,7 @@ static int attach_port(int dp_idx, struct odp_port __user *portp) int err; err = -EFAULT; - if (copy_from_user(&port, portp, sizeof port)) + if (copy_from_user(&port, portp, sizeof(port))) goto out; port.devname[IFNAMSIZ - 1] = '\0'; port.type[VPORT_TYPE_SIZE - 1] = '\0'; @@ -402,7 +412,7 @@ got_port_no: goto out_unlock_dp; set_internal_devs_mtu(dp); - dp_sysfs_add_if(dp->ports[port_no]); + dp_sysfs_add_if(get_vport_protected(dp, port_no)); err = put_user(port_no, &portp->port); @@ -453,7 +463,7 @@ static int detach_port(int dp_idx, int port_no) if (!dp) goto out_unlock_rtnl; - p = dp->ports[port_no]; + p = get_vport_protected(dp, port_no); err = -ENOENT; if (!p) goto out_unlock_dp; @@ -572,11 +582,11 @@ static int queue_control_packets(struct sk_buff *skb, struct sk_buff_head *queue nskb = skb->next; skb->next = NULL; - err = skb_cow(skb, sizeof *header); + err = skb_cow(skb, sizeof(*header)); if (err) goto err_kfree_skbs; - header = (struct odp_msg*)__skb_push(skb, sizeof *header); + header = (struct odp_msg*)__skb_push(skb, sizeof(*header)); header->type = queue_no; header->length = skb->len; header->port = port_no; @@ -666,74 +676,75 @@ static int flush_flows(struct datapath *dp) static int validate_actions(const struct nlattr *actions, u32 actions_len) { - const struct nlattr *a; - int rem; - - nla_for_each_attr(a, actions, actions_len, rem) { - static const u32 action_lens[ODPAT_MAX + 1] = { - [ODPAT_OUTPUT] = 4, - [ODPAT_CONTROLLER] = 8, - [ODPAT_SET_DL_TCI] = 2, - [ODPAT_STRIP_VLAN] = 0, - [ODPAT_SET_DL_SRC] = ETH_ALEN, - [ODPAT_SET_DL_DST] = ETH_ALEN, - [ODPAT_SET_NW_SRC] = 4, - [ODPAT_SET_NW_DST] = 4, - [ODPAT_SET_NW_TOS] = 1, - [ODPAT_SET_TP_SRC] = 2, - [ODPAT_SET_TP_DST] = 2, - [ODPAT_SET_TUNNEL] = 8, - [ODPAT_SET_PRIORITY] = 4, - [ODPAT_POP_PRIORITY] = 0, - [ODPAT_DROP_SPOOFED_ARP] = 0, - }; - int type = nla_type(a); - - if (type > ODPAT_MAX || nla_len(a) != action_lens[type]) - return -EINVAL; - - switch (type) { + const struct nlattr *a; + int rem; + + nla_for_each_attr(a, actions, actions_len, rem) { + static const u32 action_lens[ODPAT_MAX + 1] = { + [ODPAT_OUTPUT] = 4, + [ODPAT_CONTROLLER] = 8, + [ODPAT_SET_DL_TCI] = 2, + [ODPAT_STRIP_VLAN] = 0, + [ODPAT_SET_DL_SRC] = ETH_ALEN, + [ODPAT_SET_DL_DST] = ETH_ALEN, + [ODPAT_SET_NW_SRC] = 4, + [ODPAT_SET_NW_DST] = 4, + [ODPAT_SET_NW_TOS] = 1, + [ODPAT_SET_TP_SRC] = 2, + [ODPAT_SET_TP_DST] = 2, + [ODPAT_SET_TUNNEL] = 8, + [ODPAT_SET_PRIORITY] = 4, + [ODPAT_POP_PRIORITY] = 0, + [ODPAT_DROP_SPOOFED_ARP] = 0, + }; + int type = nla_type(a); + + if (type > ODPAT_MAX || nla_len(a) != action_lens[type]) + return -EINVAL; + + switch (type) { case ODPAT_UNSPEC: return -EINVAL; - case ODPAT_CONTROLLER: - case ODPAT_STRIP_VLAN: - case ODPAT_SET_DL_SRC: - case ODPAT_SET_DL_DST: - case ODPAT_SET_NW_SRC: - case ODPAT_SET_NW_DST: - case ODPAT_SET_TP_SRC: - case ODPAT_SET_TP_DST: - case ODPAT_SET_TUNNEL: - case ODPAT_SET_PRIORITY: - case ODPAT_POP_PRIORITY: - case ODPAT_DROP_SPOOFED_ARP: - /* No validation needed. */ - break; - - case ODPAT_OUTPUT: - if (nla_get_u32(a) >= DP_MAX_PORTS) - return -EINVAL; - - case ODPAT_SET_DL_TCI: + case ODPAT_CONTROLLER: + case ODPAT_STRIP_VLAN: + case ODPAT_SET_DL_SRC: + case ODPAT_SET_DL_DST: + case ODPAT_SET_NW_SRC: + case ODPAT_SET_NW_DST: + case ODPAT_SET_TP_SRC: + case ODPAT_SET_TP_DST: + case ODPAT_SET_TUNNEL: + case ODPAT_SET_PRIORITY: + case ODPAT_POP_PRIORITY: + case ODPAT_DROP_SPOOFED_ARP: + /* No validation needed. */ + break; + + case ODPAT_OUTPUT: + if (nla_get_u32(a) >= DP_MAX_PORTS) + return -EINVAL; + break; + + case ODPAT_SET_DL_TCI: if (nla_get_be16(a) & htons(VLAN_CFI_MASK)) return -EINVAL; - break; + break; - case ODPAT_SET_NW_TOS: - if (nla_get_u8(a) & INET_ECN_MASK) - return -EINVAL; - break; + case ODPAT_SET_NW_TOS: + if (nla_get_u8(a) & INET_ECN_MASK) + return -EINVAL; + break; - default: - return -EOPNOTSUPP; - } - } + default: + return -EOPNOTSUPP; + } + } - if (rem > 0) - return -EINVAL; + if (rem > 0) + return -EINVAL; - return 0; + return 0; } static struct sw_flow_actions *get_actions(const struct odp_flow *flow) @@ -816,6 +827,7 @@ static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf, struct tbl_node *flow_node; struct sw_flow *flow; struct tbl *table; + struct sw_flow_actions *acts = NULL; int error; u32 hash; @@ -824,8 +836,6 @@ static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf, flow_node = tbl_lookup(table, &uf->flow.key, hash, flow_cmp); if (!flow_node) { /* No such flow. */ - struct sw_flow_actions *acts; - error = -ENOENT; if (!(uf->flags & ODPPF_CREATE)) goto error; @@ -899,7 +909,7 @@ static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf, return 0; error_free_flow_acts: - kfree(flow->sf_acts); + kfree(acts); error_free_flow: flow->sf_acts = NULL; flow_put(flow); @@ -1000,7 +1010,7 @@ static int del_flow(struct datapath *dp, struct odp_flow __user *ufp) struct odp_flow uf; int error; - if (copy_from_user(&uf, ufp, sizeof uf)) + if (copy_from_user(&uf, ufp, sizeof(uf))) return -EFAULT; flow = do_del_flow(dp, &uf.key); @@ -1023,7 +1033,7 @@ static int do_query_flows(struct datapath *dp, const struct odp_flowvec *flowvec struct tbl_node *flow_node; int error; - if (copy_from_user(&uf, ufp, sizeof uf)) + if (copy_from_user(&uf, ufp, sizeof(uf))) return -EFAULT; flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp); @@ -1051,7 +1061,7 @@ static int list_flow(struct tbl_node *node, void *cbdata_) struct odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++]; int error; - if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key)) + if (copy_to_user(&ufp->key, &flow->key, sizeof(flow->key))) return -EFAULT; error = answer_query(cbdata->dp, flow, 0, ufp); if (error) @@ -1088,7 +1098,7 @@ static int do_flowvec_ioctl(struct datapath *dp, unsigned long argp, int retval; uflowvec = (struct odp_flowvec __user *)argp; - if (copy_from_user(&flowvec, uflowvec, sizeof flowvec)) + if (copy_from_user(&flowvec, uflowvec, sizeof(flowvec))) return -EFAULT; if (flowvec.n_flows > INT_MAX / sizeof(struct odp_flow)) @@ -1173,7 +1183,7 @@ static int execute_packet(struct datapath *dp, const struct odp_execute __user * { struct odp_execute execute; - if (copy_from_user(&execute, executep, sizeof execute)) + if (copy_from_user(&execute, executep, sizeof(execute))) return -EFAULT; return do_execute(dp, &execute); @@ -1210,7 +1220,7 @@ static int get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp) } stats.max_miss_queue = DP_MAX_QUEUE_LEN; stats.max_action_queue = DP_MAX_QUEUE_LEN; - return copy_to_user(statsp, &stats, sizeof stats) ? -EFAULT : 0; + return copy_to_user(statsp, &stats, sizeof(stats)) ? -EFAULT : 0; } /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports */ @@ -1258,61 +1268,48 @@ static int put_port(const struct vport *p, struct odp_port __user *uop) { struct odp_port op; - memset(&op, 0, sizeof op); + memset(&op, 0, sizeof(op)); rcu_read_lock(); - strncpy(op.devname, vport_get_name(p), sizeof op.devname); - strncpy(op.type, vport_get_type(p), sizeof op.type); + strncpy(op.devname, vport_get_name(p), sizeof(op.devname)); + strncpy(op.type, vport_get_type(p), sizeof(op.type)); vport_get_config(p, op.config); rcu_read_unlock(); op.port = p->port_no; - return copy_to_user(uop, &op, sizeof op) ? -EFAULT : 0; + return copy_to_user(uop, &op, sizeof(op)) ? -EFAULT : 0; } static int query_port(struct datapath *dp, struct odp_port __user *uport) { struct odp_port port; + struct vport *vport; - if (copy_from_user(&port, uport, sizeof port)) + if (copy_from_user(&port, uport, sizeof(port))) return -EFAULT; if (port.devname[0]) { - struct vport *vport; - int err = 0; - port.devname[IFNAMSIZ - 1] = '\0'; vport_lock(); - rcu_read_lock(); - vport = vport_locate(port.devname); - if (!vport) { - err = -ENODEV; - goto error_unlock; - } - if (vport->dp != dp) { - err = -ENOENT; - goto error_unlock; - } - - port.port = vport->port_no; - -error_unlock: - rcu_read_unlock(); vport_unlock(); - if (err) - return err; + if (!vport) + return -ENODEV; + if (vport->dp != dp) + return -ENOENT; } else { if (port.port >= DP_MAX_PORTS) return -EINVAL; - if (!dp->ports[port.port]) + + vport = get_vport_protected(dp, port.port); + if (!vport) return -ENOENT; } - return put_port(dp->ports[port.port], uport); + return put_port(vport, uport); } static int do_list_ports(struct datapath *dp, struct odp_port __user *uports, @@ -1337,7 +1334,7 @@ static int list_ports(struct datapath *dp, struct odp_portvec __user *upv) struct odp_portvec pv; int retval; - if (copy_from_user(&pv, upv, sizeof pv)) + if (copy_from_user(&pv, upv, sizeof(pv))) return -EFAULT; retval = do_list_ports(dp, (struct odp_port __user __force *)pv.ports, @@ -1523,7 +1520,7 @@ static int compat_list_ports(struct datapath *dp, struct compat_odp_portvec __us struct compat_odp_portvec pv; int retval; - if (copy_from_user(&pv, upv, sizeof pv)) + if (copy_from_user(&pv, upv, sizeof(pv))) return -EFAULT; retval = do_list_ports(dp, compat_ptr(pv.ports), pv.n_ports); @@ -1643,7 +1640,7 @@ static int compat_list_flow(struct tbl_node *node, void *cbdata_) struct compat_odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++]; int error; - if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key)) + if (copy_to_user(&ufp->key, &flow->key, sizeof(flow->key))) return -EFAULT; error = compat_answer_query(cbdata->dp, flow, 0, ufp); if (error) @@ -1683,8 +1680,8 @@ static int compat_flowvec_ioctl(struct datapath *dp, unsigned long argp, int retval; uflowvec = compat_ptr(argp); - if (!access_ok(VERIFY_WRITE, uflowvec, sizeof *uflowvec) || - copy_from_user(&flowvec, uflowvec, sizeof flowvec)) + if (!access_ok(VERIFY_WRITE, uflowvec, sizeof(*uflowvec)) || + copy_from_user(&flowvec, uflowvec, sizeof(flowvec))) return -EFAULT; if (flowvec.n_flows > INT_MAX / sizeof(struct compat_odp_flow)) @@ -1998,6 +1995,7 @@ static unsigned int openvswitch_poll(struct file *file, poll_table *wait) } static struct file_operations openvswitch_fops = { + .owner = THIS_MODULE, .read = openvswitch_read, .poll = openvswitch_poll, .unlocked_ioctl = openvswitch_ioctl,