Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: GPL-2.0-only
config HYPERV_NET
tristate "Microsoft Hyper-V virtual network driver"
depends on HYPERV_VMBUS
select UCS2_STRING
select NLS
help
Select this option to enable the Hyper-V virtual network driver.
+4
View File
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_HYPERV_NET) += hv_netvsc.o
hv_netvsc-y := netvsc_drv.o netvsc.o rndis_filter.o netvsc_trace.o netvsc_bpf.o
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+292
View File
@@ -0,0 +1,292 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2019, Microsoft Corporation.
*
* Author:
* Haiyang Zhang <haiyangz@microsoft.com>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/netpoll.h>
#include <linux/bpf.h>
#include <linux/bpf_trace.h>
#include <linux/kernel.h>
#include <net/xdp.h>
#include <linux/mutex.h>
#include <linux/rtnetlink.h>
#include "hyperv_net.h"
u32 netvsc_run_xdp(struct net_device *ndev, struct netvsc_channel *nvchan,
struct xdp_buff *xdp)
{
struct netvsc_stats_rx *rx_stats = &nvchan->rx_stats;
void *data = nvchan->rsc.data[0];
u32 len = nvchan->rsc.len[0];
struct page *page = NULL;
struct bpf_prog *prog;
u32 act = XDP_PASS;
bool drop = true;
xdp->data_hard_start = NULL;
rcu_read_lock();
prog = rcu_dereference(nvchan->bpf_prog);
if (!prog)
goto out;
/* Ensure that the below memcpy() won't overflow the page buffer. */
if (len > ndev->mtu + ETH_HLEN) {
act = XDP_DROP;
goto out;
}
/* allocate page buffer for data */
page = alloc_page(GFP_ATOMIC);
if (!page) {
act = XDP_DROP;
goto out;
}
xdp_init_buff(xdp, PAGE_SIZE, &nvchan->xdp_rxq);
xdp_prepare_buff(xdp, page_address(page), NETVSC_XDP_HDRM, len, false);
memcpy(xdp->data, data, len);
act = bpf_prog_run_xdp(prog, xdp);
switch (act) {
case XDP_PASS:
case XDP_TX:
drop = false;
break;
case XDP_DROP:
break;
case XDP_REDIRECT:
if (!xdp_do_redirect(ndev, xdp, prog)) {
nvchan->xdp_flush = true;
drop = false;
u64_stats_update_begin(&rx_stats->syncp);
rx_stats->xdp_redirect++;
rx_stats->packets++;
rx_stats->bytes += nvchan->rsc.pktlen;
u64_stats_update_end(&rx_stats->syncp);
break;
} else {
u64_stats_update_begin(&rx_stats->syncp);
rx_stats->xdp_drop++;
u64_stats_update_end(&rx_stats->syncp);
}
fallthrough;
case XDP_ABORTED:
trace_xdp_exception(ndev, prog, act);
break;
default:
bpf_warn_invalid_xdp_action(ndev, prog, act);
}
out:
rcu_read_unlock();
if (page && drop) {
__free_page(page);
xdp->data_hard_start = NULL;
}
return act;
}
unsigned int netvsc_xdp_fraglen(unsigned int len)
{
return SKB_DATA_ALIGN(len) +
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
}
struct bpf_prog *netvsc_xdp_get(struct netvsc_device *nvdev)
{
return rtnl_dereference(nvdev->chan_table[0].bpf_prog);
}
int netvsc_xdp_set(struct net_device *dev, struct bpf_prog *prog,
struct netlink_ext_ack *extack,
struct netvsc_device *nvdev)
{
struct bpf_prog *old_prog;
int buf_max, i;
old_prog = netvsc_xdp_get(nvdev);
if (!old_prog && !prog)
return 0;
buf_max = NETVSC_XDP_HDRM + netvsc_xdp_fraglen(dev->mtu + ETH_HLEN);
if (prog && buf_max > PAGE_SIZE) {
netdev_err(dev, "XDP: mtu:%u too large, buf_max:%u\n",
dev->mtu, buf_max);
NL_SET_ERR_MSG_MOD(extack, "XDP: mtu too large");
return -EOPNOTSUPP;
}
if (prog && (dev->features & NETIF_F_LRO)) {
netdev_err(dev, "XDP: not support LRO\n");
NL_SET_ERR_MSG_MOD(extack, "XDP: not support LRO");
return -EOPNOTSUPP;
}
if (prog)
bpf_prog_add(prog, nvdev->num_chn - 1);
for (i = 0; i < nvdev->num_chn; i++)
rcu_assign_pointer(nvdev->chan_table[i].bpf_prog, prog);
if (old_prog)
for (i = 0; i < nvdev->num_chn; i++)
bpf_prog_put(old_prog);
return 0;
}
int netvsc_vf_setxdp(struct net_device *vf_netdev, struct bpf_prog *prog)
{
struct netdev_bpf xdp;
int ret;
ASSERT_RTNL();
if (!vf_netdev)
return 0;
if (!vf_netdev->netdev_ops->ndo_bpf)
return 0;
memset(&xdp, 0, sizeof(xdp));
if (prog)
bpf_prog_inc(prog);
xdp.command = XDP_SETUP_PROG;
xdp.prog = prog;
ret = netif_xdp_propagate(vf_netdev, &xdp);
if (ret && prog)
bpf_prog_put(prog);
return ret;
}
int netvsc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
{
struct net_device_context *ndevctx = netdev_priv(dev);
struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
struct netlink_ext_ack *extack = bpf->extack;
int ret;
if (!nvdev || nvdev->destroy) {
return -ENODEV;
}
switch (bpf->command) {
case XDP_SETUP_PROG:
ret = netvsc_xdp_set(dev, bpf->prog, extack, nvdev);
if (ret)
return ret;
ret = netvsc_vf_setxdp(vf_netdev, bpf->prog);
if (ret) {
netdev_err(dev, "vf_setxdp failed:%d\n", ret);
NL_SET_ERR_MSG_MOD(extack, "vf_setxdp failed");
netvsc_xdp_set(dev, NULL, extack, nvdev);
}
return ret;
default:
return -EINVAL;
}
}
static int netvsc_ndoxdp_xmit_fm(struct net_device *ndev,
struct xdp_frame *frame, u16 q_idx)
{
struct sk_buff *skb;
skb = xdp_build_skb_from_frame(frame, ndev);
if (unlikely(!skb))
return -ENOMEM;
netvsc_get_hash(skb, netdev_priv(ndev));
skb_record_rx_queue(skb, q_idx);
netvsc_xdp_xmit(skb, ndev);
return 0;
}
int netvsc_ndoxdp_xmit(struct net_device *ndev, int n,
struct xdp_frame **frames, u32 flags)
{
struct net_device_context *ndev_ctx = netdev_priv(ndev);
const struct net_device_ops *vf_ops;
struct netvsc_stats_tx *tx_stats;
struct netvsc_device *nvsc_dev;
struct net_device *vf_netdev;
int i, count = 0;
u16 q_idx;
/* Don't transmit if netvsc_device is gone */
nvsc_dev = rcu_dereference_bh(ndev_ctx->nvdev);
if (unlikely(!nvsc_dev || nvsc_dev->destroy))
return 0;
/* If VF is present and up then redirect packets to it.
* Skip the VF if it is marked down or has no carrier.
* If netpoll is in uses, then VF can not be used either.
*/
vf_netdev = rcu_dereference_bh(ndev_ctx->vf_netdev);
if (vf_netdev && netif_running(vf_netdev) &&
netif_carrier_ok(vf_netdev) && !netpoll_tx_running(ndev) &&
vf_netdev->netdev_ops->ndo_xdp_xmit &&
ndev_ctx->data_path_is_vf) {
vf_ops = vf_netdev->netdev_ops;
return vf_ops->ndo_xdp_xmit(vf_netdev, n, frames, flags);
}
q_idx = smp_processor_id() % ndev->real_num_tx_queues;
for (i = 0; i < n; i++) {
if (netvsc_ndoxdp_xmit_fm(ndev, frames[i], q_idx))
break;
count++;
}
tx_stats = &nvsc_dev->chan_table[q_idx].tx_stats;
u64_stats_update_begin(&tx_stats->syncp);
tx_stats->xdp_xmit += count;
u64_stats_update_end(&tx_stats->syncp);
return count;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/netdevice.h>
#include "hyperv_net.h"
#define CREATE_TRACE_POINTS
#include "netvsc_trace.h"
@@ -0,0 +1,182 @@
/* SPDX-License-Identifier: GPL-2.0 */
#if !defined(_NETVSC_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
#define _NETVSC_TRACE_H
#include <linux/tracepoint.h>
#undef TRACE_SYSTEM
#define TRACE_SYSTEM netvsc
#define TRACE_INCLUDE_FILE netvsc_trace
TRACE_DEFINE_ENUM(RNDIS_MSG_PACKET);
TRACE_DEFINE_ENUM(RNDIS_MSG_INDICATE);
TRACE_DEFINE_ENUM(RNDIS_MSG_INIT);
TRACE_DEFINE_ENUM(RNDIS_MSG_INIT_C);
TRACE_DEFINE_ENUM(RNDIS_MSG_HALT);
TRACE_DEFINE_ENUM(RNDIS_MSG_QUERY);
TRACE_DEFINE_ENUM(RNDIS_MSG_QUERY_C);
TRACE_DEFINE_ENUM(RNDIS_MSG_SET);
TRACE_DEFINE_ENUM(RNDIS_MSG_SET_C);
TRACE_DEFINE_ENUM(RNDIS_MSG_RESET);
TRACE_DEFINE_ENUM(RNDIS_MSG_RESET_C);
TRACE_DEFINE_ENUM(RNDIS_MSG_KEEPALIVE);
TRACE_DEFINE_ENUM(RNDIS_MSG_KEEPALIVE_C);
#define show_rndis_type(type) \
__print_symbolic(type, \
{ RNDIS_MSG_PACKET, "PACKET" }, \
{ RNDIS_MSG_INDICATE, "INDICATE", }, \
{ RNDIS_MSG_INIT, "INIT", }, \
{ RNDIS_MSG_INIT_C, "INIT_C", }, \
{ RNDIS_MSG_HALT, "HALT", }, \
{ RNDIS_MSG_QUERY, "QUERY", }, \
{ RNDIS_MSG_QUERY_C, "QUERY_C", }, \
{ RNDIS_MSG_SET, "SET", }, \
{ RNDIS_MSG_SET_C, "SET_C", }, \
{ RNDIS_MSG_RESET, "RESET", }, \
{ RNDIS_MSG_RESET_C, "RESET_C", }, \
{ RNDIS_MSG_KEEPALIVE, "KEEPALIVE", }, \
{ RNDIS_MSG_KEEPALIVE_C, "KEEPALIVE_C", })
DECLARE_EVENT_CLASS(rndis_msg_class,
TP_PROTO(const struct net_device *ndev, u16 q,
const struct rndis_message *msg),
TP_ARGS(ndev, q, msg),
TP_STRUCT__entry(
__string( name, ndev->name )
__field( u16, queue )
__field( u32, req_id )
__field( u32, msg_type )
__field( u32, msg_len )
),
TP_fast_assign(
__assign_str(name);
__entry->queue = q;
__entry->req_id = msg->msg.init_req.req_id;
__entry->msg_type = msg->ndis_msg_type;
__entry->msg_len = msg->msg_len;
),
TP_printk("dev=%s q=%u req=%#x type=%s msg_len=%u",
__get_str(name), __entry->queue, __entry->req_id,
show_rndis_type(__entry->msg_type), __entry->msg_len)
);
DEFINE_EVENT(rndis_msg_class, rndis_send,
TP_PROTO(const struct net_device *ndev, u16 q,
const struct rndis_message *msg),
TP_ARGS(ndev, q, msg)
);
DEFINE_EVENT(rndis_msg_class, rndis_recv,
TP_PROTO(const struct net_device *ndev, u16 q,
const struct rndis_message *msg),
TP_ARGS(ndev, q, msg)
);
TRACE_DEFINE_ENUM(NVSP_MSG_TYPE_INIT);
TRACE_DEFINE_ENUM(NVSP_MSG_TYPE_INIT_COMPLETE);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_NDIS_VER);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_RECV_BUF);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_REVOKE_RECV_BUF);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_SEND_BUF);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_REVOKE_SEND_BUF);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_RNDIS_PKT);
TRACE_DEFINE_ENUM(NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE);
TRACE_DEFINE_ENUM(NVSP_MSG2_TYPE_SEND_NDIS_CONFIG);
TRACE_DEFINE_ENUM(NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION);
TRACE_DEFINE_ENUM(NVSP_MSG4_TYPE_SWITCH_DATA_PATH);
TRACE_DEFINE_ENUM(NVSP_MSG5_TYPE_SUBCHANNEL);
TRACE_DEFINE_ENUM(NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE);
#define show_nvsp_type(type) \
__print_symbolic(type, \
{ NVSP_MSG_TYPE_INIT, "INIT" }, \
{ NVSP_MSG_TYPE_INIT_COMPLETE, "INIT_COMPLETE" }, \
{ NVSP_MSG1_TYPE_SEND_NDIS_VER, "SEND_NDIS_VER" }, \
{ NVSP_MSG1_TYPE_SEND_RECV_BUF, "SEND_RECV_BUF" }, \
{ NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE, "SEND_RECV_BUF_COMPLETE" }, \
{ NVSP_MSG1_TYPE_REVOKE_RECV_BUF, "REVOKE_RECV_BUF" }, \
{ NVSP_MSG1_TYPE_SEND_SEND_BUF, "SEND_SEND_BUF" }, \
{ NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE, "SEND_SEND_BUF_COMPLETE" }, \
{ NVSP_MSG1_TYPE_REVOKE_SEND_BUF, "REVOKE_SEND_BUF" }, \
{ NVSP_MSG1_TYPE_SEND_RNDIS_PKT, "SEND_RNDIS_PKT" }, \
{ NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE, "SEND_RNDIS_PKT_COMPLETE" },\
{ NVSP_MSG2_TYPE_SEND_NDIS_CONFIG, "SEND_NDIS_CONFIG" }, \
{ NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION, "SEND_VF_ASSOCIATION" }, \
{ NVSP_MSG4_TYPE_SWITCH_DATA_PATH, "SWITCH_DATA_PATH" }, \
{ NVSP_MSG5_TYPE_SUBCHANNEL, "SUBCHANNEL" }, \
{ NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE, "SEND_INDIRECTION_TABLE" })
TRACE_EVENT(nvsp_send,
TP_PROTO(const struct net_device *ndev,
const struct nvsp_message *msg),
TP_ARGS(ndev, msg),
TP_STRUCT__entry(
__string( name, ndev->name )
__field( u32, msg_type )
),
TP_fast_assign(
__assign_str(name);
__entry->msg_type = msg->hdr.msg_type;
),
TP_printk("dev=%s type=%s",
__get_str(name),
show_nvsp_type(__entry->msg_type))
);
TRACE_EVENT(nvsp_send_pkt,
TP_PROTO(const struct net_device *ndev,
const struct vmbus_channel *chan,
const struct nvsp_1_message_send_rndis_packet *rpkt),
TP_ARGS(ndev, chan, rpkt),
TP_STRUCT__entry(
__string( name, ndev->name )
__field( u16, qid )
__field( u32, channel_type )
__field( u32, section_index )
__field( u32, section_size )
),
TP_fast_assign(
__assign_str(name);
__entry->qid = chan->offermsg.offer.sub_channel_index;
__entry->channel_type = rpkt->channel_type;
__entry->section_index = rpkt->send_buf_section_index;
__entry->section_size = rpkt->send_buf_section_size;
),
TP_printk("dev=%s qid=%u type=%s section=%u size=%d",
__get_str(name), __entry->qid,
__entry->channel_type ? "CONTROL" : "DATA",
__entry->section_index, __entry->section_size)
);
TRACE_EVENT(nvsp_recv,
TP_PROTO(const struct net_device *ndev,
const struct vmbus_channel *chan,
const struct nvsp_message *msg),
TP_ARGS(ndev, chan, msg),
TP_STRUCT__entry(
__string( name, ndev->name )
__field( u16, qid )
__field( u32, msg_type )
),
TP_fast_assign(
__assign_str(name);
__entry->qid = chan->offermsg.offer.sub_channel_index;
__entry->msg_type = msg->hdr.msg_type;
),
TP_printk("dev=%s qid=%u type=%s",
__get_str(name), __entry->qid,
show_nvsp_type(__entry->msg_type))
);
#endif /* _NETVSC_TRACE_H */
/* This part must be outside protection */
#undef TRACE_INCLUDE_PATH
#define TRACE_INCLUDE_PATH ../../drivers/net/hyperv
#include <trace/define_trace.h>
File diff suppressed because it is too large Load Diff