net: Introduce psample, a new genetlink channel for packet sampling
authorYotam Gigi <yotamg@mellanox.com>
Mon, 23 Jan 2017 10:07:08 +0000 (11:07 +0100)
committerDavid S. Miller <davem@davemloft.net>
Tue, 24 Jan 2017 18:44:28 +0000 (13:44 -0500)
Add a general way for kernel modules to sample packets, without being tied
to any specific subsystem. This netlink channel can be used by tc,
iptables, etc. and allow to standardize packet sampling in the kernel.

For every sampled packet, the psample module adds the following metadata
fields:

PSAMPLE_ATTR_IIFINDEX - the packets input ifindex, if applicable

PSAMPLE_ATTR_OIFINDEX - the packet output ifindex, if applicable

PSAMPLE_ATTR_ORIGSIZE - the packet's original size, in case it has been
   truncated during sampling

PSAMPLE_ATTR_SAMPLE_GROUP - the packet's sample group, which is set by the
   user who initiated the sampling. This field allows the user to
   differentiate between several samplers working simultaneously and
   filter packets relevant to him

PSAMPLE_ATTR_GROUP_SEQ - sequence counter of last sent packet. The
   sequence is kept for each group

PSAMPLE_ATTR_SAMPLE_RATE - the sampling rate used for sampling the packets

PSAMPLE_ATTR_DATA - the actual packet bits

The sampled packets are sent to the PSAMPLE_NL_MCGRP_SAMPLE multicast
group. In addition, add the GET_GROUPS netlink command which allows the
user to see the current sample groups, their refcount and sequence number.
This command currently supports only netlink dump mode.

Signed-off-by: Yotam Gigi <yotamg@mellanox.com>
Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Reviewed-by: Jamal Hadi Salim <jhs@mojatatu.com>
Reviewed-by: Simon Horman <simon.horman@netronome.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
MAINTAINERS
include/net/psample.h [new file with mode: 0644]
include/uapi/linux/Kbuild
include/uapi/linux/psample.h [new file with mode: 0644]
net/Kconfig
net/Makefile
net/psample/Kconfig [new file with mode: 0644]
net/psample/Makefile [new file with mode: 0644]
net/psample/psample.c [new file with mode: 0644]

index 3c84a8fecc097412f29f1a8960895f50e735a07e..d76fccd092662679ff6b693c0589316fe4a80237 100644 (file)
@@ -9957,6 +9957,13 @@ L:       linuxppc-dev@lists.ozlabs.org
 S:     Maintained
 F:     drivers/block/ps3vram.c
 
+PSAMPLE PACKET SAMPLING SUPPORT:
+M:     Yotam Gigi <yotamg@mellanox.com>
+S:     Maintained
+F:     net/psample
+F:     include/net/psample.h
+F:     include/uapi/linux/psample.h
+
 PSTORE FILESYSTEM
 M:     Anton Vorontsov <anton@enomsg.org>
 M:     Colin Cross <ccross@android.com>
diff --git a/include/net/psample.h b/include/net/psample.h
new file mode 100644 (file)
index 0000000..8888b0e
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef __NET_PSAMPLE_H
+#define __NET_PSAMPLE_H
+
+#include <uapi/linux/psample.h>
+#include <linux/module.h>
+#include <linux/list.h>
+
+struct psample_group {
+       struct list_head list;
+       struct net *net;
+       u32 group_num;
+       u32 refcount;
+       u32 seq;
+};
+
+struct psample_group *psample_group_get(struct net *net, u32 group_num);
+void psample_group_put(struct psample_group *group);
+
+#if IS_ENABLED(CONFIG_PSAMPLE)
+
+void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
+                          u32 trunc_size, int in_ifindex, int out_ifindex,
+                          u32 sample_rate);
+
+#else
+
+static inline void psample_sample_packet(struct psample_group *group,
+                                        struct sk_buff *skb, u32 trunc_size,
+                                        int in_ifindex, int out_ifindex,
+                                        u32 sample_rate)
+{
+}
+
+#endif
+
+#endif /* __NET_PSAMPLE_H */
index e600b50be77e8757eebeddca15b14f2dc2825954..80ad741a42fa8f74816f0f9056370282781a243d 100644 (file)
@@ -305,6 +305,7 @@ header-y += netrom.h
 header-y += net_namespace.h
 header-y += net_tstamp.h
 header-y += nfc.h
+header-y += psample.h
 header-y += nfs2.h
 header-y += nfs3.h
 header-y += nfs4.h
diff --git a/include/uapi/linux/psample.h b/include/uapi/linux/psample.h
new file mode 100644 (file)
index 0000000..ed48996
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef __UAPI_PSAMPLE_H
+#define __UAPI_PSAMPLE_H
+
+enum {
+       /* sampled packet metadata */
+       PSAMPLE_ATTR_IIFINDEX,
+       PSAMPLE_ATTR_OIFINDEX,
+       PSAMPLE_ATTR_ORIGSIZE,
+       PSAMPLE_ATTR_SAMPLE_GROUP,
+       PSAMPLE_ATTR_GROUP_SEQ,
+       PSAMPLE_ATTR_SAMPLE_RATE,
+       PSAMPLE_ATTR_DATA,
+
+       /* commands attributes */
+       PSAMPLE_ATTR_GROUP_REFCOUNT,
+
+       __PSAMPLE_ATTR_MAX
+};
+
+enum psample_command {
+       PSAMPLE_CMD_SAMPLE,
+       PSAMPLE_CMD_GET_GROUP,
+       PSAMPLE_CMD_NEW_GROUP,
+       PSAMPLE_CMD_DEL_GROUP,
+};
+
+/* Can be overridden at runtime by module option */
+#define PSAMPLE_ATTR_MAX (__PSAMPLE_ATTR_MAX - 1)
+
+#define PSAMPLE_NL_MCGRP_CONFIG_NAME "config"
+#define PSAMPLE_NL_MCGRP_SAMPLE_NAME "packets"
+#define PSAMPLE_GENL_NAME "psample"
+#define PSAMPLE_GENL_VERSION 1
+
+#endif
index 92ae1500d9e14cbd2105aaca555a2d56d55f77be..ce4aee69fc0dd4d1e725ec47c0fe6cbe95e59568 100644 (file)
@@ -390,6 +390,7 @@ source "net/9p/Kconfig"
 source "net/caif/Kconfig"
 source "net/ceph/Kconfig"
 source "net/nfc/Kconfig"
+source "net/psample/Kconfig"
 
 config LWTUNNEL
        bool "Network light weight tunnels"
index 5d6e0e5ff7f8cdbd4f7a9f5f9ee6e8349bb10ec5..7d41de48310ec6e3e402bc53f5a1de8b076861a5 100644 (file)
@@ -70,6 +70,7 @@ obj-$(CONFIG_DNS_RESOLVER)    += dns_resolver/
 obj-$(CONFIG_CEPH_LIB)         += ceph/
 obj-$(CONFIG_BATMAN_ADV)       += batman-adv/
 obj-$(CONFIG_NFC)              += nfc/
+obj-$(CONFIG_PSAMPLE)          += psample/
 obj-$(CONFIG_OPENVSWITCH)      += openvswitch/
 obj-$(CONFIG_VSOCKETS) += vmw_vsock/
 obj-$(CONFIG_MPLS)             += mpls/
diff --git a/net/psample/Kconfig b/net/psample/Kconfig
new file mode 100644 (file)
index 0000000..d850246
--- /dev/null
@@ -0,0 +1,15 @@
+#
+# psample packet sampling configuration
+#
+
+menuconfig PSAMPLE
+       depends on NET
+       tristate "Packet-sampling netlink channel"
+       default n
+       help
+         Say Y here to add support for packet-sampling netlink channel
+         This netlink channel allows transferring packets alongside some
+         metadata to userspace.
+
+         To compile this support as a module, choose M here: the module will
+         be called psample.
diff --git a/net/psample/Makefile b/net/psample/Makefile
new file mode 100644 (file)
index 0000000..609b0a7
--- /dev/null
@@ -0,0 +1,5 @@
+#
+# Makefile for the psample netlink channel
+#
+
+obj-$(CONFIG_PSAMPLE) += psample.o
diff --git a/net/psample/psample.c b/net/psample/psample.c
new file mode 100644 (file)
index 0000000..8aa58a9
--- /dev/null
@@ -0,0 +1,301 @@
+/*
+ * net/psample/psample.c - Netlink channel for packet sampling
+ * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/skbuff.h>
+#include <linux/module.h>
+#include <net/net_namespace.h>
+#include <net/sock.h>
+#include <net/netlink.h>
+#include <net/genetlink.h>
+#include <net/psample.h>
+#include <linux/spinlock.h>
+
+#define PSAMPLE_MAX_PACKET_SIZE 0xffff
+
+static LIST_HEAD(psample_groups_list);
+static DEFINE_SPINLOCK(psample_groups_lock);
+
+/* multicast groups */
+enum psample_nl_multicast_groups {
+       PSAMPLE_NL_MCGRP_CONFIG,
+       PSAMPLE_NL_MCGRP_SAMPLE,
+};
+
+static const struct genl_multicast_group psample_nl_mcgrps[] = {
+       [PSAMPLE_NL_MCGRP_CONFIG] = { .name = PSAMPLE_NL_MCGRP_CONFIG_NAME },
+       [PSAMPLE_NL_MCGRP_SAMPLE] = { .name = PSAMPLE_NL_MCGRP_SAMPLE_NAME },
+};
+
+static struct genl_family psample_nl_family __ro_after_init;
+
+static int psample_group_nl_fill(struct sk_buff *msg,
+                                struct psample_group *group,
+                                enum psample_command cmd, u32 portid, u32 seq,
+                                int flags)
+{
+       void *hdr;
+       int ret;
+
+       hdr = genlmsg_put(msg, portid, seq, &psample_nl_family, flags, cmd);
+       if (!hdr)
+               return -EMSGSIZE;
+
+       ret = nla_put_u32(msg, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
+       if (ret < 0)
+               goto error;
+
+       ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_REFCOUNT, group->refcount);
+       if (ret < 0)
+               goto error;
+
+       ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_SEQ, group->seq);
+       if (ret < 0)
+               goto error;
+
+       genlmsg_end(msg, hdr);
+       return 0;
+
+error:
+       genlmsg_cancel(msg, hdr);
+       return -EMSGSIZE;
+}
+
+static int psample_nl_cmd_get_group_dumpit(struct sk_buff *msg,
+                                          struct netlink_callback *cb)
+{
+       struct psample_group *group;
+       int start = cb->args[0];
+       int idx = 0;
+       int err;
+
+       spin_lock(&psample_groups_lock);
+       list_for_each_entry(group, &psample_groups_list, list) {
+               if (!net_eq(group->net, sock_net(msg->sk)))
+                       continue;
+               if (idx < start) {
+                       idx++;
+                       continue;
+               }
+               err = psample_group_nl_fill(msg, group, PSAMPLE_CMD_NEW_GROUP,
+                                           NETLINK_CB(cb->skb).portid,
+                                           cb->nlh->nlmsg_seq, NLM_F_MULTI);
+               if (err)
+                       break;
+               idx++;
+       }
+
+       spin_unlock(&psample_groups_lock);
+       cb->args[0] = idx;
+       return msg->len;
+}
+
+static const struct genl_ops psample_nl_ops[] = {
+       {
+               .cmd = PSAMPLE_CMD_GET_GROUP,
+               .dumpit = psample_nl_cmd_get_group_dumpit,
+               /* can be retrieved by unprivileged users */
+       }
+};
+
+static struct genl_family psample_nl_family __ro_after_init = {
+       .name           = PSAMPLE_GENL_NAME,
+       .version        = PSAMPLE_GENL_VERSION,
+       .maxattr        = PSAMPLE_ATTR_MAX,
+       .netnsok        = true,
+       .module         = THIS_MODULE,
+       .mcgrps         = psample_nl_mcgrps,
+       .ops            = psample_nl_ops,
+       .n_ops          = ARRAY_SIZE(psample_nl_ops),
+       .n_mcgrps       = ARRAY_SIZE(psample_nl_mcgrps),
+};
+
+static void psample_group_notify(struct psample_group *group,
+                                enum psample_command cmd)
+{
+       struct sk_buff *msg;
+       int err;
+
+       msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
+       if (!msg)
+               return;
+
+       err = psample_group_nl_fill(msg, group, cmd, 0, 0, NLM_F_MULTI);
+       if (!err)
+               genlmsg_multicast_netns(&psample_nl_family, group->net, msg, 0,
+                                       PSAMPLE_NL_MCGRP_CONFIG, GFP_ATOMIC);
+       else
+               nlmsg_free(msg);
+}
+
+static struct psample_group *psample_group_create(struct net *net,
+                                                 u32 group_num)
+{
+       struct psample_group *group;
+
+       group = kzalloc(sizeof(*group), GFP_ATOMIC);
+       if (!group)
+               return NULL;
+
+       group->net = net;
+       group->group_num = group_num;
+       list_add_tail(&group->list, &psample_groups_list);
+
+       psample_group_notify(group, PSAMPLE_CMD_NEW_GROUP);
+       return group;
+}
+
+static void psample_group_destroy(struct psample_group *group)
+{
+       psample_group_notify(group, PSAMPLE_CMD_DEL_GROUP);
+       list_del(&group->list);
+       kfree(group);
+}
+
+static struct psample_group *
+psample_group_lookup(struct net *net, u32 group_num)
+{
+       struct psample_group *group;
+
+       list_for_each_entry(group, &psample_groups_list, list)
+               if ((group->group_num == group_num) && (group->net == net))
+                       return group;
+       return NULL;
+}
+
+struct psample_group *psample_group_get(struct net *net, u32 group_num)
+{
+       struct psample_group *group;
+
+       spin_lock(&psample_groups_lock);
+
+       group = psample_group_lookup(net, group_num);
+       if (!group) {
+               group = psample_group_create(net, group_num);
+               if (!group)
+                       goto out;
+       }
+       group->refcount++;
+
+out:
+       spin_unlock(&psample_groups_lock);
+       return group;
+}
+EXPORT_SYMBOL_GPL(psample_group_get);
+
+void psample_group_put(struct psample_group *group)
+{
+       spin_lock(&psample_groups_lock);
+
+       if (--group->refcount == 0)
+               psample_group_destroy(group);
+
+       spin_unlock(&psample_groups_lock);
+}
+EXPORT_SYMBOL_GPL(psample_group_put);
+
+void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
+                          u32 trunc_size, int in_ifindex, int out_ifindex,
+                          u32 sample_rate)
+{
+       struct sk_buff *nl_skb;
+       int data_len;
+       int meta_len;
+       void *data;
+       int ret;
+
+       meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
+                  (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
+                  nla_total_size(sizeof(u32)) +        /* sample_rate */
+                  nla_total_size(sizeof(u32)) +        /* orig_size */
+                  nla_total_size(sizeof(u32)) +        /* group_num */
+                  nla_total_size(sizeof(u32));         /* seq */
+
+       data_len = min(skb->len, trunc_size);
+       if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
+               data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
+                           - NLA_ALIGNTO;
+
+       nl_skb = genlmsg_new(meta_len + data_len, GFP_ATOMIC);
+       if (unlikely(!nl_skb))
+               return;
+
+       data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0,
+                          PSAMPLE_CMD_SAMPLE);
+       if (unlikely(!data))
+               goto error;
+
+       if (in_ifindex) {
+               ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_IIFINDEX, in_ifindex);
+               if (unlikely(ret < 0))
+                       goto error;
+       }
+
+       if (out_ifindex) {
+               ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OIFINDEX, out_ifindex);
+               if (unlikely(ret < 0))
+                       goto error;
+       }
+
+       ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_RATE, sample_rate);
+       if (unlikely(ret < 0))
+               goto error;
+
+       ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_ORIGSIZE, skb->len);
+       if (unlikely(ret < 0))
+               goto error;
+
+       ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
+       if (unlikely(ret < 0))
+               goto error;
+
+       ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++);
+       if (unlikely(ret < 0))
+               goto error;
+
+       if (data_len) {
+               int nla_len = nla_total_size(data_len);
+               struct nlattr *nla;
+
+               nla = (struct nlattr *)skb_put(nl_skb, nla_len);
+               nla->nla_type = PSAMPLE_ATTR_DATA;
+               nla->nla_len = nla_attr_size(data_len);
+
+               if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
+                       goto error;
+       }
+
+       genlmsg_end(nl_skb, data);
+       genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
+                               PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
+
+       return;
+error:
+       pr_err_ratelimited("Could not create psample log message\n");
+       nlmsg_free(nl_skb);
+}
+EXPORT_SYMBOL_GPL(psample_sample_packet);
+
+static int __init psample_module_init(void)
+{
+       return genl_register_family(&psample_nl_family);
+}
+
+static void __exit psample_module_exit(void)
+{
+       genl_unregister_family(&psample_nl_family);
+}
+
+module_init(psample_module_init);
+module_exit(psample_module_exit);
+
+MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
+MODULE_DESCRIPTION("netlink channel for packet sampling");
+MODULE_LICENSE("GPL v2");