#include <linux/if_vlan.h>
#include <linux/in.h>
#include <linux/ip.h>
+#include <linux/jhash.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/etherdevice.h>
static void destroy_dp_rcu(struct rcu_head *rcu)
{
struct datapath *dp = container_of(rcu, struct datapath, rcu);
- int i;
-
- 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);
if (unlikely(!flow_node)) {
struct dp_upcall_info upcall;
- upcall.type = _ODPL_MISS_NR;
+ upcall.cmd = ODP_PACKET_CMD_MISS;
upcall.key = &key;
upcall.userdata = 0;
upcall.sample_pool = 0;
*(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
}
-/* Append each packet in 'skb' list to 'queue'. There will be only one packet
- * unless we broke up a GSO packet. */
+static struct genl_family dp_packet_genl_family;
+#define PACKET_N_MC_GROUPS 16
+
+static int packet_mc_group(struct datapath *dp, u8 cmd)
+{
+ BUILD_BUG_ON_NOT_POWER_OF_2(PACKET_N_MC_GROUPS);
+ return jhash_2words(dp->dp_idx, cmd, 0) & (PACKET_N_MC_GROUPS - 1);
+}
+
+/* Send each packet in the 'skb' list to userspace for 'dp' as directed by
+ * 'upcall_info'. There will be only one packet unless we broke up a GSO
+ * packet.
+ */
static int queue_control_packets(struct datapath *dp, struct sk_buff *skb,
const struct dp_upcall_info *upcall_info)
{
+ u32 group = packet_mc_group(dp, upcall_info->cmd);
struct sk_buff *nskb;
int port_no;
int err;
port_no = ODPP_LOCAL;
do {
- struct odp_packet *upcall;
+ struct odp_header *upcall;
struct sk_buff *user_skb; /* to be queued to userspace */
struct nlattr *nla;
unsigned int len;
nskb = skb->next;
skb->next = NULL;
- len = sizeof(struct odp_packet);
+ len = sizeof(struct odp_header);
len += nla_total_size(4); /* ODP_PACKET_ATTR_TYPE. */
len += nla_total_size(skb->len);
len += nla_total_size(FLOW_BUFSIZE);
if (upcall_info->actions_len)
len += nla_total_size(upcall_info->actions_len);
- user_skb = alloc_skb(len, GFP_ATOMIC);
- if (!user_skb)
+ user_skb = genlmsg_new(len, GFP_ATOMIC);
+ if (!user_skb) {
+ netlink_set_err(INIT_NET_GENL_SOCK, 0, group, -ENOBUFS);
goto err_kfree_skbs;
+ }
- upcall = (struct odp_packet *)__skb_put(user_skb, sizeof(*upcall));
+ upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 0, upcall_info->cmd);
upcall->dp_idx = dp->dp_idx;
- nla_put_u32(user_skb, ODP_PACKET_ATTR_TYPE, upcall_info->type);
-
nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_KEY);
flow_to_nlattrs(upcall_info->key, user_skb);
nla_nest_end(user_skb, nla);
else
skb_copy_bits(skb, 0, nla_data(nla), skb->len);
- upcall->len = user_skb->len;
- skb_queue_tail(&dp->queues[upcall_info->type], user_skb);
+ err = genlmsg_multicast(user_skb, 0, group, GFP_ATOMIC);
+ if (err)
+ goto err_kfree_skbs;
kfree_skb(skb);
skb = nskb;
return err;
}
+/* Generic Netlink multicast groups for upcalls.
+ *
+ * We really want three unique multicast groups per datapath, but we can't even
+ * get one, because genl_register_mc_group() takes genl_lock, which is also
+ * held during Generic Netlink message processing, so trying to acquire
+ * multicast groups during ODP_DP_NEW processing deadlocks. Instead, we
+ * preallocate a few groups and use them round-robin for datapaths. Collision
+ * isn't fatal--multicast listeners should check that the family is the one
+ * that they want and discard others--but it wastes time and memory to receive
+ * unwanted messages.
+ */
+static struct genl_multicast_group packet_mc_groups[PACKET_N_MC_GROUPS];
+
+static struct genl_family dp_packet_genl_family = {
+ .id = GENL_ID_GENERATE,
+ .hdrsize = sizeof(struct odp_header),
+ .name = ODP_PACKET_FAMILY,
+ .version = 1,
+ .maxattr = ODP_PACKET_ATTR_MAX
+};
+
+static int packet_register_mc_groups(void)
+{
+ int i;
+
+ for (i = 0; i < PACKET_N_MC_GROUPS; i++) {
+ struct genl_multicast_group *group = &packet_mc_groups[i];
+ int error;
+
+ sprintf(group->name, "packet%d", i);
+ error = genl_register_mc_group(&dp_packet_genl_family, group);
+ if (error)
+ return error;
+ }
+ return 0;
+}
+
int dp_upcall(struct datapath *dp, struct sk_buff *skb, const struct dp_upcall_info *upcall_info)
{
struct dp_stats_percpu *stats;
- struct sk_buff_head *queue;
int err;
WARN_ON_ONCE(skb_shared(skb));
- BUG_ON(upcall_info->type >= DP_N_QUEUES);
-
- queue = &dp->queues[upcall_info->type];
- err = -ENOBUFS;
- if (skb_queue_len(queue) >= DP_MAX_QUEUE_LEN)
- goto err_kfree_skb;
forward_ip_summed(skb);
}
}
- err = queue_control_packets(dp, skb, upcall_info);
- wake_up_interruptible(&dp->waitqueue);
- return err;
+ return queue_control_packets(dp, skb, upcall_info);
err_kfree_skb:
kfree_skb(skb);
return 0;
}
-static const struct nla_policy execute_policy[ODP_PACKET_ATTR_MAX + 1] = {
- [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
- [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
-};
-
-static int execute_packet(const struct odp_packet __user *uodp_packet)
+static int odp_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
{
- struct nlattr *a[ODP_PACKET_ATTR_MAX + 1];
- struct odp_packet *odp_packet;
- struct sk_buff *skb, *packet;
+ struct odp_header *odp_header = info->userhdr;
+ struct nlattr **a = info->attrs;
+ struct sk_buff *packet;
unsigned int actions_len;
struct nlattr *actions;
struct sw_flow_key key;
struct datapath *dp;
struct ethhdr *eth;
bool is_frag;
- u32 len;
int err;
- if (get_user(len, &uodp_packet->len))
- return -EFAULT;
- if (len < sizeof(struct odp_packet))
- return -EINVAL;
-
- skb = alloc_skb(len, GFP_KERNEL);
- if (!skb)
- return -ENOMEM;
-
- err = -EFAULT;
- if (copy_from_user(__skb_put(skb, len), uodp_packet, len))
- goto exit_free_skb;
-
- odp_packet = (struct odp_packet *)skb->data;
- err = -EINVAL;
- if (odp_packet->len != len)
- goto exit_free_skb;
-
- __skb_pull(skb, sizeof(struct odp_packet));
- err = nla_parse(a, ODP_PACKET_ATTR_MAX, (struct nlattr *)skb->data,
- skb->len, execute_policy);
- if (err)
- goto exit_free_skb;
-
err = -EINVAL;
if (!a[ODP_PACKET_ATTR_PACKET] || !a[ODP_PACKET_ATTR_ACTIONS] ||
nla_len(a[ODP_PACKET_ATTR_PACKET]) < ETH_HLEN)
- goto exit_free_skb;
+ goto exit;
actions = nla_data(a[ODP_PACKET_ATTR_ACTIONS]);
actions_len = nla_len(a[ODP_PACKET_ATTR_ACTIONS]);
err = validate_actions(actions, actions_len);
if (err)
- goto exit_free_skb;
+ goto exit;
packet = skb_clone(skb, GFP_KERNEL);
err = -ENOMEM;
if (!packet)
- goto exit_free_skb;
+ goto exit;
packet->data = nla_data(a[ODP_PACKET_ATTR_PACKET]);
packet->len = nla_len(a[ODP_PACKET_ATTR_PACKET]);
err = flow_extract(packet, -1, &key, &is_frag);
if (err)
- goto exit_free_skb;
+ goto exit;
rcu_read_lock();
- dp = get_dp(odp_packet->dp_idx);
+ dp = get_dp(odp_header->dp_idx);
err = -ENODEV;
if (dp)
err = execute_actions(dp, packet, &key, actions, actions_len);
rcu_read_unlock();
-exit_free_skb:
- kfree_skb(skb);
+exit:
return err;
}
+static const struct nla_policy packet_policy[ODP_PACKET_ATTR_MAX + 1] = {
+ [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
+ [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
+};
+
+static struct genl_ops dp_packet_genl_ops[] = {
+ { .cmd = ODP_PACKET_CMD_EXECUTE,
+ .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
+ .policy = packet_policy,
+ .doit = odp_packet_cmd_execute
+ }
+};
+
static void get_dp_stats(struct datapath *dp, struct odp_stats *stats)
{
int i;
}
}
-static int get_listen_mask(const struct file *f)
-{
- return (long)f->private_data;
-}
-
-static void set_listen_mask(struct file *f, int listen_mask)
-{
- f->private_data = (void*)(long)listen_mask;
-}
-
static const struct nla_policy flow_policy[ODP_FLOW_ATTR_MAX + 1] = {
[ODP_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
[ODP_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
}
static const struct nla_policy datapath_policy[ODP_DP_ATTR_MAX + 1] = {
+#ifdef HAVE_NLA_NUL_STRING
[ODP_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
+#endif
[ODP_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
[ODP_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
};
-/* Called with genl_mutex. */
-static int copy_datapath_to_user(void __user *dst, struct datapath *dp, uint32_t total_len)
+static struct genl_family dp_datapath_genl_family = {
+ .id = GENL_ID_GENERATE,
+ .hdrsize = sizeof(struct odp_header),
+ .name = ODP_DATAPATH_FAMILY,
+ .version = 1,
+ .maxattr = ODP_DP_ATTR_MAX
+};
+
+static struct genl_multicast_group dp_datapath_multicast_group = {
+ .name = ODP_DATAPATH_MCGROUP
+};
+
+static int odp_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
+ u32 pid, u32 seq, u32 flags, u8 cmd)
{
- struct odp_datapath *odp_datapath;
- struct sk_buff *skb;
+ struct odp_header *odp_header;
struct nlattr *nla;
int err;
- skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
- err = -ENOMEM;
- if (!skb)
- goto exit;
+ odp_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
+ flags, cmd);
+ if (!odp_header)
+ goto error;
- odp_datapath = (struct odp_datapath*)__skb_put(skb, sizeof(struct odp_datapath));
- odp_datapath->dp_idx = dp->dp_idx;
- odp_datapath->total_len = total_len;
+ odp_header->dp_idx = dp->dp_idx;
rcu_read_lock();
err = nla_put_string(skb, ODP_DP_ATTR_NAME, dp_name(dp));
if (dp->sflow_probability)
NLA_PUT_U32(skb, ODP_DP_ATTR_SAMPLING, dp->sflow_probability);
- if (skb->len > total_len)
+ nla = nla_nest_start(skb, ODP_DP_ATTR_MCGROUPS);
+ if (!nla)
goto nla_put_failure;
+ NLA_PUT_U32(skb, ODP_PACKET_CMD_MISS, packet_mc_group(dp, ODP_PACKET_CMD_MISS));
+ NLA_PUT_U32(skb, ODP_PACKET_CMD_ACTION, packet_mc_group(dp, ODP_PACKET_CMD_ACTION));
+ NLA_PUT_U32(skb, ODP_PACKET_CMD_SAMPLE, packet_mc_group(dp, ODP_PACKET_CMD_SAMPLE));
+ nla_nest_end(skb, nla);
- odp_datapath->len = skb->len;
- err = copy_to_user(dst, skb->data, skb->len) ? -EFAULT : 0;
- goto exit_free_skb;
+ return genlmsg_end(skb, odp_header);
nla_put_failure:
- err = -EMSGSIZE;
-exit_free_skb:
- kfree_skb(skb);
-exit:
- return err;
+ genlmsg_cancel(skb, odp_header);
+error:
+ return -EMSGSIZE;
}
-/* Called with genl_mutex. */
-static struct sk_buff *copy_datapath_from_user(struct odp_datapath __user *uodp_datapath, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
+static struct sk_buff *odp_dp_cmd_build_info(struct datapath *dp, u32 pid,
+ u32 seq, u8 cmd)
{
- struct odp_datapath *odp_datapath;
struct sk_buff *skb;
- u32 len;
- int err;
-
- if (get_user(len, &uodp_datapath->len))
- return ERR_PTR(-EFAULT);
- if (len < sizeof(struct odp_datapath))
- return ERR_PTR(-EINVAL);
+ int retval;
- skb = alloc_skb(len, GFP_KERNEL);
+ skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!skb)
return ERR_PTR(-ENOMEM);
- err = -EFAULT;
- if (copy_from_user(__skb_put(skb, len), uodp_datapath, len))
- goto error_free_skb;
-
- odp_datapath = (struct odp_datapath *)skb->data;
- err = -EINVAL;
- if (odp_datapath->len != len)
- goto error_free_skb;
-
- err = nla_parse(a, ODP_DP_ATTR_MAX,
- (struct nlattr *)(skb->data + sizeof(struct odp_datapath)),
- skb->len - sizeof(struct odp_datapath), datapath_policy);
- if (err)
- goto error_free_skb;
+ retval = odp_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
+ if (retval < 0) {
+ kfree_skb(skb);
+ return ERR_PTR(retval);
+ }
+ return skb;
+}
+static int odp_dp_cmd_validate(struct nlattr *a[ODP_DP_ATTR_MAX + 1])
+{
if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
u32 frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
- err = -EINVAL;
if (frags != ODP_DP_FRAG_ZERO && frags != ODP_DP_FRAG_DROP)
- goto error_free_skb;
+ return -EINVAL;
}
- err = VERIFY_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
- if (err)
- goto error_free_skb;
-
- return skb;
-
-error_free_skb:
- kfree_skb(skb);
- return ERR_PTR(err);
+ return VERIFY_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
}
/* Called with genl_mutex and optionally with RTNL lock also. */
-static struct datapath *lookup_datapath(struct odp_datapath *odp_datapath, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
+static struct datapath *lookup_datapath(struct odp_header *odp_header, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
{
if (!a[ODP_DP_ATTR_NAME]) {
- struct datapath *dp = get_dp(odp_datapath->dp_idx);
+ struct datapath *dp = get_dp(odp_header->dp_idx);
if (!dp)
return ERR_PTR(-ENODEV);
return dp;
dp->sflow_probability = nla_get_u32(a[ODP_DP_ATTR_SAMPLING]);
}
-static int new_datapath(struct odp_datapath __user *uodp_datapath)
+static int odp_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
{
- struct nlattr *a[ODP_DP_ATTR_MAX + 1];
- struct odp_datapath *odp_datapath;
+ struct nlattr **a = info->attrs;
+ struct odp_header *odp_header = info->userhdr;
struct vport_parms parms;
- struct sk_buff *skb;
+ struct sk_buff *reply;
struct datapath *dp;
struct vport *vport;
int dp_idx;
int err;
- int i;
-
- skb = copy_datapath_from_user(uodp_datapath, a);
- err = PTR_ERR(skb);
- if (IS_ERR(skb))
- goto err;
- odp_datapath = (struct odp_datapath *)skb->data;
err = -EINVAL;
if (!a[ODP_DP_ATTR_NAME])
- goto err_free_skb;
+ goto err;
+
+ err = odp_dp_cmd_validate(a);
+ if (err)
+ goto err;
rtnl_lock();
err = -ENODEV;
if (!try_module_get(THIS_MODULE))
goto err_unlock_rtnl;
- dp_idx = odp_datapath->dp_idx;
+ dp_idx = odp_header->dp_idx;
if (dp_idx < 0) {
err = -EFBIG;
for (dp_idx = 0; dp_idx < ARRAY_SIZE(dps); dp_idx++) {
goto err_put_module;
INIT_LIST_HEAD(&dp->port_list);
dp->dp_idx = dp_idx;
- for (i = 0; i < DP_N_QUEUES; i++)
- skb_queue_head_init(&dp->queues[i]);
- init_waitqueue_head(&dp->waitqueue);
/* Initialize kobject for bridge. This will be added as
* /sys/class/net/<devname>/brif later, if sysfs is enabled. */
change_datapath(dp, a);
+ reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
+ err = PTR_ERR(reply);
+ if (IS_ERR(reply))
+ goto err_destroy_local_port;
+
rcu_assign_pointer(dps[dp_idx], dp);
dp_sysfs_add_dp(dp);
rtnl_unlock();
+ genl_notify(reply, genl_info_net(info), info->snd_pid,
+ dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
return 0;
err_destroy_local_port:
module_put(THIS_MODULE);
err_unlock_rtnl:
rtnl_unlock();
-err_free_skb:
- kfree_skb(skb);
err:
return err;
}
-static int del_datapath(struct odp_datapath __user *uodp_datapath)
+static int odp_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
{
- struct nlattr *a[ODP_DP_ATTR_MAX + 1];
struct vport *vport, *next_vport;
+ struct sk_buff *reply;
struct datapath *dp;
- struct sk_buff *skb;
int err;
- skb = copy_datapath_from_user(uodp_datapath, a);
- err = PTR_ERR(skb);
- if (IS_ERR(skb))
+ err = odp_dp_cmd_validate(info->attrs);
+ if (err)
goto exit;
rtnl_lock();
- dp = lookup_datapath((struct odp_datapath *)skb->data, a);
+ dp = lookup_datapath(info->userhdr, info->attrs);
err = PTR_ERR(dp);
if (IS_ERR(dp))
- goto exit_free;
+ goto exit_unlock;
+
+ reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_DEL);
+ err = PTR_ERR(reply);
+ if (IS_ERR(reply))
+ goto exit_unlock;
list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
if (vport->port_no != ODPP_LOCAL)
call_rcu(&dp->rcu, destroy_dp_rcu);
module_put(THIS_MODULE);
+ genl_notify(reply, genl_info_net(info), info->snd_pid,
+ dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
err = 0;
-exit_free:
- kfree_skb(skb);
+exit_unlock:
rtnl_unlock();
exit:
return err;
}
-static int set_datapath(struct odp_datapath __user *uodp_datapath)
+static int odp_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
{
- struct nlattr *a[ODP_DP_ATTR_MAX + 1];
+ struct sk_buff *reply;
struct datapath *dp;
- struct sk_buff *skb;
int err;
- skb = copy_datapath_from_user(uodp_datapath, a);
- err = PTR_ERR(skb);
- if (IS_ERR(skb))
- goto exit;
+ err = odp_dp_cmd_validate(info->attrs);
+ if (err)
+ return err;
- dp = lookup_datapath((struct odp_datapath *)skb->data, a);
- err = PTR_ERR(dp);
+ dp = lookup_datapath(info->userhdr, info->attrs);
if (IS_ERR(dp))
- goto exit_free;
+ return PTR_ERR(dp);
- change_datapath(dp, a);
- err = 0;
+ change_datapath(dp, info->attrs);
-exit_free:
- kfree_skb(skb);
-exit:
- return err;
+ reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
+ if (IS_ERR(reply)) {
+ err = PTR_ERR(reply);
+ netlink_set_err(INIT_NET_GENL_SOCK, 0,
+ dp_datapath_multicast_group.id, err);
+ return 0;
+ }
+
+ genl_notify(reply, genl_info_net(info), info->snd_pid,
+ dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
+ return 0;
}
-static int get_datapath(struct odp_datapath __user *uodp_datapath)
+static int odp_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
{
- struct nlattr *a[ODP_DP_ATTR_MAX + 1];
- struct odp_datapath *odp_datapath;
+ struct sk_buff *reply;
struct datapath *dp;
- struct sk_buff *skb;
int err;
- skb = copy_datapath_from_user(uodp_datapath, a);
- err = PTR_ERR(skb);
- if (IS_ERR(skb))
- goto exit;
- odp_datapath = (struct odp_datapath *)skb->data;
-
- dp = lookup_datapath(odp_datapath, a);
+ err = odp_dp_cmd_validate(info->attrs);
+ if (err)
+ return err;
- err = PTR_ERR(dp);
+ dp = lookup_datapath(info->userhdr, info->attrs);
if (IS_ERR(dp))
- goto exit_free;
+ return PTR_ERR(dp);
- err = copy_datapath_to_user(uodp_datapath, dp, odp_datapath->total_len);
-exit_free:
- kfree_skb(skb);
-exit:
- return err;
+ reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
+ if (IS_ERR(reply))
+ return PTR_ERR(reply);
+
+ return genlmsg_reply(reply, info);
}
-static int dump_datapath(struct odp_datapath __user *uodp_datapath)
+static int odp_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
- struct nlattr *a[ODP_DP_ATTR_MAX + 1];
- struct odp_datapath *odp_datapath;
- struct sk_buff *skb;
u32 dp_idx;
- int err;
- skb = copy_datapath_from_user(uodp_datapath, a);
- err = PTR_ERR(skb);
- if (IS_ERR(skb))
- goto exit;
- odp_datapath = (struct odp_datapath *)skb->data;
-
- err = -ENODEV;
- for (dp_idx = odp_datapath->dp_idx; dp_idx < ARRAY_SIZE(dps); dp_idx++) {
+ for (dp_idx = cb->args[0]; dp_idx < ARRAY_SIZE(dps); dp_idx++) {
struct datapath *dp = get_dp(dp_idx);
if (!dp)
continue;
-
- err = copy_datapath_to_user(uodp_datapath, dp, odp_datapath->total_len);
- break;
+ if (odp_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
+ cb->nlh->nlmsg_seq, NLM_F_MULTI,
+ ODP_DP_CMD_NEW) < 0)
+ break;
}
- kfree_skb(skb);
-exit:
- return err;
+
+ cb->args[0] = dp_idx;
+ return skb->len;
}
+static struct genl_ops dp_datapath_genl_ops[] = {
+ { .cmd = ODP_DP_CMD_NEW,
+ .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
+ .policy = datapath_policy,
+ .doit = odp_dp_cmd_new
+ },
+ { .cmd = ODP_DP_CMD_DEL,
+ .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
+ .policy = datapath_policy,
+ .doit = odp_dp_cmd_del
+ },
+ { .cmd = ODP_DP_CMD_GET,
+ .flags = 0, /* OK for unprivileged users. */
+ .policy = datapath_policy,
+ .doit = odp_dp_cmd_get,
+ .dumpit = odp_dp_cmd_dump
+ },
+ { .cmd = ODP_DP_CMD_SET,
+ .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
+ .policy = datapath_policy,
+ .doit = odp_dp_cmd_set,
+ },
+};
+
static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
[ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
[ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
static long openvswitch_ioctl(struct file *f, unsigned int cmd,
unsigned long argp)
{
- int listeners;
int err;
genl_lock();
-
- /* Handle commands with special locking requirements up front. */
switch (cmd) {
- case ODP_DP_NEW:
- err = new_datapath((struct odp_datapath __user *)argp);
- goto exit;
-
- case ODP_DP_GET:
- err = get_datapath((struct odp_datapath __user *)argp);
- goto exit;
-
- case ODP_DP_DEL:
- err = del_datapath((struct odp_datapath __user *)argp);
- goto exit;
-
- case ODP_DP_SET:
- err = set_datapath((struct odp_datapath __user *)argp);
- goto exit;
-
- case ODP_DP_DUMP:
- err = dump_datapath((struct odp_datapath __user *)argp);
- goto exit;
-
case ODP_VPORT_NEW:
err = attach_vport((struct odp_vport __user *)argp);
goto exit;
err = dump_flow((struct odp_flow __user *)argp);
goto exit;
- case ODP_EXECUTE:
- err = execute_packet((struct odp_packet __user *)argp);
- goto exit;
- }
-
- switch (cmd) {
- case ODP_GET_LISTEN_MASK:
- err = put_user(get_listen_mask(f), (int __user *)argp);
- break;
-
- case ODP_SET_LISTEN_MASK:
- err = get_user(listeners, (int __user *)argp);
- if (err)
- break;
- err = -EINVAL;
- if (listeners & ~ODPL_ALL)
- break;
- err = 0;
- set_listen_mask(f, listeners);
- break;
-
default:
err = -ENOIOCTLCMD;
break;
return err;
}
-static int dp_has_packet_of_interest(struct datapath *dp, int listeners)
-{
- int i;
- for (i = 0; i < DP_N_QUEUES; i++) {
- if (listeners & (1 << i) && !skb_queue_empty(&dp->queues[i]))
- return 1;
- }
- return 0;
-}
-
#ifdef CONFIG_COMPAT
static long openvswitch_compat_ioctl(struct file *f, unsigned int cmd, unsigned long argp)
{
/* Ioctls that don't need any translation at all. */
return openvswitch_ioctl(f, cmd, argp);
- case ODP_DP_NEW:
- case ODP_DP_GET:
- case ODP_DP_DEL:
- case ODP_DP_SET:
- case ODP_DP_DUMP:
case ODP_VPORT_NEW:
case ODP_VPORT_DEL:
case ODP_VPORT_GET:
case ODP_FLOW_GET:
case ODP_FLOW_SET:
case ODP_FLOW_DUMP:
- case ODP_SET_LISTEN_MASK:
- case ODP_GET_LISTEN_MASK:
- case ODP_EXECUTE:
/* Ioctls that just need their pointer argument extended. */
return openvswitch_ioctl(f, cmd, (unsigned long)compat_ptr(argp));
}
#endif
-static struct sk_buff *openvswitch_try_read(struct file *f, struct datapath *dp)
-{
- int listeners = get_listen_mask(f);
- int i;
+static struct file_operations openvswitch_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = openvswitch_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = openvswitch_compat_ioctl,
+#endif
+};
- for (i = 0; i < DP_N_QUEUES; i++) {
- if (listeners & (1 << i)) {
- struct sk_buff *skb = skb_dequeue(&dp->queues[i]);
- if (skb)
- return skb;
- }
- }
+static int major;
- if (f->f_flags & O_NONBLOCK)
- return ERR_PTR(-EAGAIN);
+struct genl_family_and_ops {
+ struct genl_family *family;
+ struct genl_ops *ops;
+ int n_ops;
+ struct genl_multicast_group *group;
+};
- wait_event_interruptible(dp->waitqueue,
- dp_has_packet_of_interest(dp, listeners));
+static const struct genl_family_and_ops dp_genl_families[] = {
+ { &dp_datapath_genl_family,
+ dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
+ &dp_datapath_multicast_group },
+ { &dp_packet_genl_family,
+ dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
+ NULL },
+};
- if (signal_pending(current))
- return ERR_PTR(-ERESTARTSYS);
+static void dp_unregister_genl(int n_families)
+{
+ int i;
- return NULL;
+ for (i = 0; i < n_families; i++) {
+ genl_unregister_family(dp_genl_families[i].family);
+ }
}
-static ssize_t openvswitch_read(struct file *f, char __user *buf,
- size_t nbytes, loff_t *ppos)
+static int dp_register_genl(void)
{
- int dp_idx = iminor(f->f_dentry->d_inode);
- struct datapath *dp;
- struct sk_buff *skb;
- struct iovec iov;
- int retval;
-
- genl_lock();
-
- dp = get_dp(dp_idx);
- retval = -ENODEV;
- if (!dp)
- goto error;
-
- retval = 0;
- if (nbytes == 0 || !get_listen_mask(f))
- goto error;
+ int n_registered;
+ int err;
+ int i;
- do {
- skb = openvswitch_try_read(f, dp);
- } while (!skb);
+ n_registered = 0;
+ for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
+ const struct genl_family_and_ops *f = &dp_genl_families[i];
- genl_unlock();
- if (IS_ERR(skb))
- return PTR_ERR(skb);
+ err = genl_register_family_with_ops(f->family, f->ops,
+ f->n_ops);
+ if (err)
+ goto error;
+ n_registered++;
- iov.iov_base = buf;
- iov.iov_len = min_t(size_t, skb->len, nbytes);
- retval = skb_copy_datagram_iovec(skb, 0, &iov, iov.iov_len);
- if (!retval)
- retval = skb->len;
+ if (f->group) {
+ err = genl_register_mc_group(f->family, f->group);
+ if (err)
+ goto error;
+ }
+ }
- kfree_skb(skb);
- return retval;
+ err = packet_register_mc_groups();
+ if (err)
+ goto error;
+ return 0;
error:
- genl_unlock();
- return retval;
-}
-
-static unsigned int openvswitch_poll(struct file *file, poll_table *wait)
-{
- int dp_idx = iminor(file->f_dentry->d_inode);
- struct datapath *dp;
- unsigned int mask;
-
- genl_lock();
- dp = get_dp(dp_idx);
- if (dp) {
- mask = 0;
- poll_wait(file, &dp->waitqueue, wait);
- if (dp_has_packet_of_interest(dp, get_listen_mask(file)))
- mask |= POLLIN | POLLRDNORM;
- } else {
- mask = POLLIN | POLLRDNORM | POLLHUP;
- }
- genl_unlock();
-
- return mask;
+ dp_unregister_genl(n_registered);
+ return err;
}
-static struct file_operations openvswitch_fops = {
- .owner = THIS_MODULE,
- .read = openvswitch_read,
- .poll = openvswitch_poll,
- .unlocked_ioctl = openvswitch_ioctl,
-#ifdef CONFIG_COMPAT
- .compat_ioctl = openvswitch_compat_ioctl,
-#endif
-};
-
-static int major;
-
static int __init dp_init(void)
{
struct sk_buff *dummy_skb;
if (err < 0)
goto error_unreg_notifier;
+ err = dp_register_genl();
+ if (err < 0)
+ goto error_unreg_chrdev;
+
return 0;
+error_unreg_chrdev:
+ unregister_chrdev(major, "openvswitch");
error_unreg_notifier:
unregister_netdevice_notifier(&dp_device_notifier);
error_vport_exit:
static void dp_cleanup(void)
{
rcu_barrier();
+ dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
unregister_chrdev(major, "openvswitch");
unregister_netdevice_notifier(&dp_device_notifier);
vport_exit();