Skip to content

Commit

Permalink
psample: Encapsulate packet metadata in a struct
Browse files Browse the repository at this point in the history
Currently, callers of psample_sample_packet() pass three metadata
attributes: Ingress port, egress port and truncated size. Subsequent
patches are going to add more attributes (e.g., egress queue occupancy),
which also need an indication whether they are valid or not.

Encapsulate packet metadata in a struct in order to keep the number of
arguments reasonable.

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
idosch authored and davem330 committed Mar 14, 2021
1 parent c6baf7e commit a03e99d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions drivers/net/ethernet/mellanox/mlxsw/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ void mlxsw_sp_sample_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
{
struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
struct mlxsw_sp_port_sample *sample;
u32 size;
struct psample_metadata md = {};

if (unlikely(!mlxsw_sp_port)) {
dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
Expand All @@ -2229,9 +2229,9 @@ void mlxsw_sp_sample_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
sample = rcu_dereference(mlxsw_sp_port->sample);
if (!sample)
goto out_unlock;
size = sample->truncate ? sample->trunc_size : skb->len;
psample_sample_packet(sample->psample_group, skb, size,
mlxsw_sp_port->dev->ifindex, 0, sample->rate);
md.trunc_size = sample->truncate ? sample->trunc_size : skb->len;
md.in_ifindex = mlxsw_sp_port->dev->ifindex;
psample_sample_packet(sample->psample_group, skb, sample->rate, &md);
out_unlock:
rcu_read_unlock();
out:
Expand Down
14 changes: 9 additions & 5 deletions include/net/psample.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ struct psample_group {
struct rcu_head rcu;
};

struct psample_metadata {
u32 trunc_size;
int in_ifindex;
int out_ifindex;
};

struct psample_group *psample_group_get(struct net *net, u32 group_num);
void psample_group_take(struct psample_group *group);
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);
u32 sample_rate, const struct psample_metadata *md);

#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)
struct sk_buff *skb, u32 sample_rate,
const struct psample_metadata *md)
{
}

Expand Down
6 changes: 4 additions & 2 deletions net/psample/psample.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,11 @@ static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info)
#endif

void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
u32 trunc_size, int in_ifindex, int out_ifindex,
u32 sample_rate)
u32 sample_rate, const struct psample_metadata *md)
{
int out_ifindex = md->out_ifindex;
int in_ifindex = md->in_ifindex;
u32 trunc_size = md->trunc_size;
#ifdef CONFIG_INET
struct ip_tunnel_info *tun_info;
#endif
Expand Down
16 changes: 6 additions & 10 deletions net/sched/act_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,8 @@ static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
{
struct tcf_sample *s = to_sample(a);
struct psample_group *psample_group;
struct psample_metadata md = {};
int retval;
int size;
int iif;
int oif;

tcf_lastuse_update(&s->tcf_tm);
bstats_cpu_update(this_cpu_ptr(s->common.cpu_bstats), skb);
Expand All @@ -172,20 +170,18 @@ static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
/* randomly sample packets according to rate */
if (psample_group && (prandom_u32() % s->rate == 0)) {
if (!skb_at_tc_ingress(skb)) {
iif = skb->skb_iif;
oif = skb->dev->ifindex;
md.in_ifindex = skb->skb_iif;
md.out_ifindex = skb->dev->ifindex;
} else {
iif = skb->dev->ifindex;
oif = 0;
md.in_ifindex = skb->dev->ifindex;
}

/* on ingress, the mac header gets popped, so push it back */
if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
skb_push(skb, skb->mac_len);

size = s->truncate ? s->trunc_size : skb->len;
psample_sample_packet(psample_group, skb, size, iif, oif,
s->rate);
md.trunc_size = s->truncate ? s->trunc_size : skb->len;
psample_sample_packet(psample_group, skb, s->rate, &md);

if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
skb_pull(skb, skb->mac_len);
Expand Down

0 comments on commit a03e99d

Please sign in to comment.