10 Star 18 Fork 52

openEuler/opensource-intern

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
psample.rst 3.40 KB
一键复制 编辑 原始数据 按行查看 历史

psample

### psample.c (Packet Sampling)

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.

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openeuler/opensource-intern.git
git@gitee.com:openeuler/opensource-intern.git
openeuler
opensource-intern
opensource-intern
master

搜索帮助