Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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)

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Ido Schimmel and committed by
David S. Miller
07e1a580 a03e99d3

+52 -1
+7
include/net/psample.h
··· 18 18 u32 trunc_size; 19 19 int in_ifindex; 20 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 23 30 struct psample_group *psample_group_get(struct net *net, u32 group_num);
+7
include/uapi/linux/psample.h
··· 16 16 /* commands attributes */ 17 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 26 __PSAMPLE_ATTR_MAX 20 27 }; 21 28
+38 -1
net/psample/psample.c
··· 8 8 #include <linux/kernel.h> 9 9 #include <linux/skbuff.h> 10 10 #include <linux/module.h> 11 + #include <linux/timekeeping.h> 11 12 #include <net/net_namespace.h> 12 13 #include <net/sock.h> 13 14 #include <net/netlink.h> ··· 359 358 void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, 360 359 u32 sample_rate, const struct psample_metadata *md) 361 360 { 361 + ktime_t tstamp = ktime_get_real(); 362 362 int out_ifindex = md->out_ifindex; 363 363 int in_ifindex = md->in_ifindex; 364 364 u32 trunc_size = md->trunc_size; ··· 374 372 375 373 meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) + 376 374 (out_ifindex ? nla_total_size(sizeof(u16)) : 0) + 375 + (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) + 376 + (md->out_tc_occ_valid ? nla_total_size_64bit(sizeof(u64)) : 0) + 377 + (md->latency_valid ? nla_total_size_64bit(sizeof(u64)) : 0) + 377 378 nla_total_size(sizeof(u32)) + /* sample_rate */ 378 379 nla_total_size(sizeof(u32)) + /* orig_size */ 379 380 nla_total_size(sizeof(u32)) + /* group_num */ 380 - nla_total_size(sizeof(u32)); /* seq */ 381 + nla_total_size(sizeof(u32)) + /* seq */ 382 + nla_total_size_64bit(sizeof(u64)) + /* timestamp */ 383 + nla_total_size(sizeof(u16)); /* protocol */ 381 384 382 385 #ifdef CONFIG_INET 383 386 tun_info = skb_tunnel_info(skb); ··· 429 422 goto error; 430 423 431 424 ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++); 425 + if (unlikely(ret < 0)) 426 + goto error; 427 + 428 + if (md->out_tc_valid) { 429 + ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OUT_TC, md->out_tc); 430 + if (unlikely(ret < 0)) 431 + goto error; 432 + } 433 + 434 + if (md->out_tc_occ_valid) { 435 + ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_OUT_TC_OCC, 436 + md->out_tc_occ, PSAMPLE_ATTR_PAD); 437 + if (unlikely(ret < 0)) 438 + goto error; 439 + } 440 + 441 + if (md->latency_valid) { 442 + ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_LATENCY, 443 + md->latency, PSAMPLE_ATTR_PAD); 444 + if (unlikely(ret < 0)) 445 + goto error; 446 + } 447 + 448 + ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_TIMESTAMP, 449 + ktime_to_ns(tstamp), PSAMPLE_ATTR_PAD); 450 + if (unlikely(ret < 0)) 451 + goto error; 452 + 453 + ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_PROTO, 454 + be16_to_cpu(skb->protocol)); 432 455 if (unlikely(ret < 0)) 433 456 goto error; 434 457