Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
net: Introduce psample, a new genetlink channel for packet sampling
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
yotamgi authored and davem330 committed Jan 24, 2017
1 parent d36db83 commit 6ae0a62
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 0 deletions.
7 changes: 7 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
36 changes: 36 additions & 0 deletions include/net/psample.h
Original file line number Diff line number Diff line change
@@ -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 */
1 change: 1 addition & 0 deletions include/uapi/linux/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions include/uapi/linux/psample.h
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions net/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions net/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
15 changes: 15 additions & 0 deletions net/psample/Kconfig
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions net/psample/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# Makefile for the psample netlink channel
#

obj-$(CONFIG_PSAMPLE) += psample.o
Loading

0 comments on commit 6ae0a62

Please sign in to comment.