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
1818 u32 trunc_size;
1919 int in_ifindex;
2020 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;
2128 };
2229
2330 struct 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
1616 /* commands attributes */
1717 PSAMPLE_ATTR_GROUP_REFCOUNT,
1818
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+
1926 __PSAMPLE_ATTR_MAX
2027 };
2128
net/psample/psample.c+38−1
@@ -8,6 +8,7 @@ · 7 unmodified lines
88 #include <linux/kernel.h>
99 #include <linux/skbuff.h>
1010 #include <linux/module.h>
11+#include <linux/timekeeping.h>
1112 #include <net/net_namespace.h>
1213 #include <net/sock.h>
1314 #include <net/netlink.h>
@@ -358,6 +359,7 @@ static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info) · 344 unmodified lines
358359 void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
359360 u32 sample_rate, const struct psample_metadata *md)
360361 {
362+ ktime_t tstamp = ktime_get_real();
361363 int out_ifindex = md->out_ifindex;
362364 int in_ifindex = md->in_ifindex;
363365 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
372374
373375 meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
374376 (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) +
375380 nla_total_size(sizeof(u32)) + /* sample_rate */
376381 nla_total_size(sizeof(u32)) + /* orig_size */
377382 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 */
379386
380387 #ifdef CONFIG_INET
381388 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
425432 if (unlikely(ret < 0))
426433 goto error;
427434
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+
428465 if (data_len) {
429466 int nla_len = nla_total_size(data_len);
430467 struct nlattr *nla;