Function Description The psample.c file implements a Netlink channel for packet sampling, which allows transferring packets alongside some metadata to userspace. It includes group management and data transmission logic.
static int psample_group_nl_fill(struct sk_buff *msg,
struct psample_group *group,
enum psample_command cmd,
u32 portid, u32 seq, int flags)
{
void *hdr;
int ret;
hdr = genlmsg_put(msg, portid, seq, &psample_nl_family, flags, cmd);
if (!hdr)
return -EMSGSIZE;
ret = nla_put_u32(msg, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
if (ret < 0)
goto error;
ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_REFCOUNT, group->refcount);
if (ret < 0)
goto error;
ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_SEQ, group->seq);
if (ret < 0)
goto error;
genlmsg_end(msg, hdr);
return 0;
error:
genlmsg_cancel(msg, hdr);
return -EMSGSIZE;
}
This function fills a Netlink message with information about a specific sample group and sends it to userspace. It uses genlmsg_put and nla_put_u32 to construct and add necessary attributes.
void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
u32 trunc_size, int in_ifindex, int out_ifindex,
u32 sample_rate)
{
data_len = min(skb->len, trunc_size);
if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN - NLA_ALIGNTO;
nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
if (unlikely(!nl_skb))
return;
data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0, PSAMPLE_CMD_SAMPLE);
if (unlikely(!data))
goto error;
genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
return;
error:
pr_err_ratelimited("Could not create psample log message\n");
nlmsg_free(nl_skb);
}
The psample_sample_packet function creates a new Netlink message containing the truncated packet data and associated metadata, and multicasts it to all user-space processes listening to the sample group.
Open Capabilities - Custom Message Filling: Developers can extend the psample_group_nl_fill function to customize the message content sent to userspace. - Sample Packet Handling: The psample_sample_packet function allows developers to customize how sample packets and their metadata are handled. - Group Management: Functions such as psample_group_create, psample_group_destroy, and psample_group_lookup provide the ability to create, destroy, and look up sample groups. - State Notification: The psample_group_notify function can be used to notify userspace when the state of a sample group changes. - Query Interface: The psample_nl_cmd_get_group_dumpit function provides an interface for querying all existing sample groups and their states.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。