History

psample: Add additional metadata attributes

Extend psample to report the following attributes when available: * Output traffic class as a 16-bit value * Output traffic class occupancy in bytes as a 64-bit value * End-to-end latency of the packet in nanoseconds resolution * Software timestamp in nanoseconds resolution (always available) * Packet's protocol. Needed for packet dissection in user space (always available)

Ido Schimmel committed 5 years ago

Commit 07e1a581 parent a03e99d3 files · +52 −1Browse files

Unified · Split

3 files
  1. include/net/psample.h +7 −0
  2. include/uapi/linux/psample.h +7 −0
  3. net/psample/psample.c +38 −1
include/net/psample.h+7−0
@@ -18,6 +18,13 @@ struct psample_metadata { · 17 unmodified lines
18 u32 trunc_size;18 u32 trunc_size;
19 int in_ifindex;19 int in_ifindex;
20 int out_ifindex;20 int out_ifindex;
21 u16 out_tc;
22 u64 out_tc_occ; /* bytes */
23 u64 latency; /* nanoseconds */
24 u8 out_tc_valid:1,
25 out_tc_occ_valid:1,
26 latency_valid:1,
27 unused:5;
21};28};
22 29
23struct psample_group *psample_group_get(struct net *net, u32 group_num);30struct psample_group *psample_group_get(struct net *net, u32 group_num);
include/uapi/linux/psample.h+7−0
@@ -16,6 +16,13 @@ enum { · 15 unmodified lines
16 /* commands attributes */16 /* commands attributes */
17 PSAMPLE_ATTR_GROUP_REFCOUNT,17 PSAMPLE_ATTR_GROUP_REFCOUNT,
18 18
19 PSAMPLE_ATTR_PAD,
20 PSAMPLE_ATTR_OUT_TC, /* u16 */
21 PSAMPLE_ATTR_OUT_TC_OCC, /* u64, bytes */
22 PSAMPLE_ATTR_LATENCY, /* u64, nanoseconds */
23 PSAMPLE_ATTR_TIMESTAMP, /* u64, nanoseconds */
24 PSAMPLE_ATTR_PROTO, /* u16 */
25
19 __PSAMPLE_ATTR_MAX26 __PSAMPLE_ATTR_MAX
20};27};
21 28
net/psample/psample.c+38−1
@@ -8,6 +8,7 @@ · 7 unmodified lines
8#include <linux/kernel.h>8#include <linux/kernel.h>
9#include <linux/skbuff.h>9#include <linux/skbuff.h>
10#include <linux/module.h>10#include <linux/module.h>
11#include <linux/timekeeping.h>
11#include <net/net_namespace.h>12#include <net/net_namespace.h>
12#include <net/sock.h>13#include <net/sock.h>
13#include <net/netlink.h>14#include <net/netlink.h>
@@ -358,6 +359,7 @@ static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info) · 344 unmodified lines
358void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,359void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
359 u32 sample_rate, const struct psample_metadata *md)360 u32 sample_rate, const struct psample_metadata *md)
360{361{
362 ktime_t tstamp = ktime_get_real();
361 int out_ifindex = md->out_ifindex;363 int out_ifindex = md->out_ifindex;
362 int in_ifindex = md->in_ifindex;364 int in_ifindex = md->in_ifindex;
363 u32 trunc_size = md->trunc_size;365 u32 trunc_size = md->trunc_size;
@@ -372,10 +374,15 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · 8 unmodified lines
372 374
373 meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +375 meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
374 (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +376 (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
377 (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) +
378 (md->out_tc_occ_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
379 (md->latency_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
375 nla_total_size(sizeof(u32)) + /* sample_rate */380 nla_total_size(sizeof(u32)) + /* sample_rate */
376 nla_total_size(sizeof(u32)) + /* orig_size */381 nla_total_size(sizeof(u32)) + /* orig_size */
377 nla_total_size(sizeof(u32)) + /* group_num */382 nla_total_size(sizeof(u32)) + /* group_num */
378 nla_total_size(sizeof(u32)); /* seq */383 nla_total_size(sizeof(u32)) + /* seq */
384 nla_total_size_64bit(sizeof(u64)) + /* timestamp */
385 nla_total_size(sizeof(u16)); /* protocol */
379 386
380#ifdef CONFIG_INET387#ifdef CONFIG_INET
381 tun_info = skb_tunnel_info(skb);388 tun_info = skb_tunnel_info(skb);
@@ -425,6 +432,36 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · 43 unmodified lines
425 if (unlikely(ret < 0))432 if (unlikely(ret < 0))
426 goto error;433 goto error;
427 434
435 if (md->out_tc_valid) {
436 ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OUT_TC, md->out_tc);
437 if (unlikely(ret < 0))
438 goto error;
439 }
440
441 if (md->out_tc_occ_valid) {
442 ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_OUT_TC_OCC,
443 md->out_tc_occ, PSAMPLE_ATTR_PAD);
444 if (unlikely(ret < 0))
445 goto error;
446 }
447
448 if (md->latency_valid) {
449 ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_LATENCY,
450 md->latency, PSAMPLE_ATTR_PAD);
451 if (unlikely(ret < 0))
452 goto error;
453 }
454
455 ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_TIMESTAMP,
456 ktime_to_ns(tstamp), PSAMPLE_ATTR_PAD);
457 if (unlikely(ret < 0))
458 goto error;
459
460 ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_PROTO,
461 be16_to_cpu(skb->protocol));
462 if (unlikely(ret < 0))
463 goto error;
464
428 if (data_len) {465 if (data_len) {
429 int nla_len = nla_total_size(data_len);466 int nla_len = nla_total_size(data_len);
430 struct nlattr *nla;467 struct nlattr *nla;