【OVS2.5.0源码分析】normal action精确流表生成和刷新过程分析

本文详细探讨了OVS2.5.0中,首包到达时如何基于目的MAC生成精确流表规则,并在后续过程中如何通过upcall_xlate函数和revalidate线程实现流表的刷新,确保网络流量的正确转发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首包精确流表生成

首包到达OVS交换机时,尚未建立基于目的mac的流表规则,需要upcall到用户态进行学习,此时生成的规则是把报文flood到其他端口。

1、upcall_xlate函数

    upcall->dump_seq = seq_read(udpif->dump_seq);
    upcall->reval_seq = seq_read(udpif->reval_seq);
    xlate_actions(&xin, &upcall->xout); 
    upcall->xout_initialized = true;

2、xlate_actions函数

<pre name="code" class="cpp">   /* The bridge is now known so obtain its table version. */
    ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);

    if (!xin->ofpacts && !ctx.rule) {
        ctx.rule = rule_dpif_lookup_from_table(       //检索流表
            ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
            ctx.xin->resubmit_stats, &ctx.table_id,
            flow->in_port.ofp_port, true, true);
        if (ctx.xin->resubmit_stats) {
            rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
        }
        if (ctx.xin->xcache) {
            struct xc_entry *entry;

            entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
            entry->u.rule = ctx.rule;
            rule_dpif_ref(ctx.rule);
        }

        if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
            ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
        }
    }
    xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);

    /* Get the proximate input port of the packet.  (If xin->recirc,
     * flow->in_port is the ultimate input port of the packet.) */
    struct xport *in_port = get_ofp_port(xbridge,
                                         ctx.base_flow.in_port.ofp_port);

    /* Tunnel stats only for non-recirculated packets. */
    if (!xin->recirc && in_port && in_port->is_tunnel) {
        if (ctx.xin->resubmit_stats) {
            netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
            if (in_port->bfd) {
                bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
            }
        }
        if (ctx.xin->xcache) {
            struct xc_entry *entry;

            entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
            entry->u.dev.rx = netdev_ref(in_port->netdev);
            entry->u.dev.bfd = bfd_ref(in_port->bfd);
        }
    }

    if (!xin->recirc && process_special(&ctx, in_port)) {
        /* process_special() did all the processing for this packet.
         *
         * We do not perform special processing on recirculated packets, as
         * recirculated packets are not really received by the bridge.*/
    } else if (in_port && in_port->xbundle
               && xbundle_mirror_out(xbridge, in_port->xbundle)) {
        if (ctx.xin->packet != NULL) {
            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
            VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
                         "%s, which is reserved exclusively for mirroring",
                         ctx.xbridge->name, in_port->xbundle->name);
        }
    } else {
        /* Sampling is done only for packets really received by th
<think>好的,我现在需要帮助用户分析Open vSwitch(OVS)的源码实现。用户已经提供了一些相关的引用资料,我需要结合这些资料来组织一个详细的回答。首先,我应该先理解用户的需求,他们可能是一个刚开始接触OVS源码的开发者或研究人员,想要了解源码的结构关键模块。 根据引用资料,OVS的源码结构包括ovs-vswitchd守护进程、ovsdb-server数据库、内核模块等。用户提供的引用[1]提到了源码文件布局,我需要先介绍这一点,让用户有一个整体的认识。然后,根据引用[2]、[3]、[4],网桥(Bridge)的实现、vswitchd的功能以及主循环的结构都是关键部分。我需要将这些部分逐一展开,说明它们的功能相互关系。 接下来,我需要确保回答符合用户给出的格式要求,比如使用中文,正确应用行内独立公式的LaTeX格式(虽然目前可能用不上数学公式,但如果有需要的话要记得)。另外,用户要求生成相关问题,这些问题需要与源码分析相关,能够引导用户进一步探索。 在组织内容时,可能需要分模块介绍,比如源码结构、核心组件、数据路径处理、主循环机制等。每个部分需要结合引用中的具体信息,比如bridge_run()bridge_wait()的作用,以及ovs-vswitchd的主循环结构。同时,要确保引用标识正确添加到对应的段落末尾,例如引用[^1]、[^2]等。 还要注意用户提到的“系统级指令”中的要求,比如行内数学达式用$...$,独立公式用$$...$$,不过当前问题可能涉及较少的数学内容,但如果有涉及的地方需要正确应用。另外,回答需要结构清晰,分步骤说明,保证真实可靠,所以需要准确引用提供的资料,并正确标注来源。 最后,生成的相关问题应该针对OVS源码的深入分析,比如数据路径实现、与内核模块的交互、性能优化等,这样可以帮助用户进一步研究。需要确保问题具有针对性,能够引导用户探索源码的不同方面。 总结来说,回答的结构应该包括源码结构概述、核心模块分析(如网桥、vswitchd)、主循环机制、数据路径处理,以及源码分析方法,每个部分结合引用资料进行说明,并在最后生成相关问题。需要注意格式正确,引用标识正确放置,语言简洁明了,帮助用户逐步理解OVS的源码实现。</think>### Open vSwitch源码实现分析 #### 1. 源码结构概述 Open vSwitch(OVS)的源码组织主要分为以下模块: - **ovs-vswitchd**:用户空间守护进程,负责处理交换逻辑管理[^4] - **ovsdb-server**:轻量级数据库,存储交换机配置状态信息 - **内核模块**:实现快速数据路径(如`datapath/linux`目录) - **协议支持**:包括OpenFlow、NetFlow等协议实现 引用[1]指出,2.17.1版本源码目录结构清晰,核心模块集中在`lib`、`ofproto`、`vswitchd`等目录中[^1]。 #### 2. 核心组件分析 **(1) 网桥(Bridge)实现** - 通过`struct bridge`示虚拟网桥,包含端口、、QoS策略等配置[^2] - 功能实现比Linux Bridge更强大,支持: $$ \text{OpenFlow协议} + \text{多级} + \text{SDN扩展} $$ - 关键数据结构关联: ```c struct bridge → struct ofproto → struct flow_table ``` **(2) vswitchd主循环** 主程序通过事件驱动模型运行: ```c while (!exiting) { bridge_run(); // 处理网桥状态更新 unixctl_server_run();// 执行控制命令 netdev_run(); // 处理网络设备事件 poll_block(); // 等待事件触发[^4] } ``` 其中`bridge_wait()`实现同步机制,协调OVSDB事务、接口通知等事件[^3]。 #### 3. 数据路径处理 - **快速路径**:内核模块处理常规数据包转发 - **慢速路径**:用户空间处理复杂匹配OpenFlow规则 - 量匹配公式: 设数据包特征为$P=\{src\_mac, dst\_mac, ...\}$,规则为$F=\{match\_fields, actions\}$,则: $$ \exists F_i \in FlowTable,\ s.t.\ P \subseteq match\_fields(F_i) $$ #### 4. 源码分析方法论 建议采用以下步骤: 1. 从`ovs-vswitchd/main.c`跟踪主循环 2. 分析`bridge_init()`初始化程 3. 研究`ofproto`模块的OpenFlow实现 4. 对照OVSDB模式文件`vswitch.ovsschema` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值