ovs源码分析

源码版本

ovs 内核部分的代码在linux内核的 /net/openswitch目录下,应用层控制面代码在ovs项目中。

  • Linux kernel: version 6.2.0

  • Ovs: v3.4.1

总体架构

整体结构图

ovs的架构如下图所示,主要由内核datapath、vswitchd、ovsdb以及用户空间的ovs-vsctl/ovs-ofctl/ovs-dpctl等组成。

  • vswitchd是一个守护进程,是ovs的管理和控制服务,通过unix socket将配置信息保存到ovsdb,并通过netlink和内核模块交互。

  • ovsdb则是ovs的数据库,保存了ovs配置信息。

  • datapath是负责数据交换的内核模块,比如把接受端口收到的包放到流表中进行匹配,并执行匹配后的动作等。它在初始化和port binding的时候注册钩子函数,把端口的报文处理接管到内核模块。

实机环境

ovs 守护进程

root@node1:~# ps -aux|grep vswitchd
root       906  3.5  0.7 531888 74196 ?        S<Lsl 17:40   0:01 ovs-vswitchd unix:/var/run/openvswitch/db.sock -vconsole:emer -vsyslog:err -vfile:info --mlockall --no-chdir --log-file=/var/log/openvswitch/ovs-vswitchd.log --pidfile=/var/run/openvswitch/ovs-vswitchd.pid --detach

ovsdb 守护进程

root@node1:~# ps -aux|grep ovsdb
root      5904  0.0  0.0  14900  3400 ?        S<s  17:40   0:00 ovsdb-server: monitoring pid 5905 (healthy)
root      5905  0.4  0.0  16220  8392 ?        S<   17:40   0:00 ovsdb-server /etc/openvswitch/conf.db -vconsole:emer -vsyslog:err -vfile:info --remote=punix:/var/run/openvswitch/db.sock --private-key=db:Open_vSwitch,SSL,private_key --certificate=db:Open_vSwitch,SSL,certificate --bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert --no-chdir --log-file=/var/log/openvswitch/ovsdb-server.log --pidfile=/var/run/openvswitch/ovsdb-server.pid --detach --monitor
root      6925  0.0  0.0  14904  3408 ?        Ss   17:40   0:00 ovsdb-server: monitoring pid 120 (healthy)
root      6926  0.1  0.0 163628  8080 ?        Sl   17:40   0:00 ovsdb-server -vconsole:off -vfile:info --log-file=/var/log/ovn/ovsdb-server-nb.log --remote=punix:/var/run/ovn/ovnnb_db.sock --pidfile=/var/run/ovn/ovnnb_db.pid --unixctl=/var/run/ovn/ovnnb_db.ctl --detach --monitor --remote=db:OVN_Northbound,NB_Global,connections --private-key=db:OVN_Northbound,SSL,private_key --certificate=db:OVN_Northbound,SSL,certificate --ca-cert=db:OVN_Northbound,SSL,ca_cert --ssl-protocols=db:OVN_Northbound,SSL,ssl_protocols --ssl-ciphers=db:OVN_Northbound,SSL,ssl_ciphers --remote=ptcp:6641:[::] /etc/ovn/ovnnb_db.db
root      7066  0.0  0.0  14904  3408 ?        Ss   17:40   0:00 ovsdb-server: monitoring pid 143 (healthy)
root      7068  0.1  0.1 165384 10172 ?        Sl   17:40   0:00 ovsdb-server -vconsole:off -vfile:info --log-file=/var/log/ovn/ovsdb-server-sb.log --remote=punix:/var/run/ovn/ovnsb_db.sock --pidfile=/var/run/ovn/ovnsb_db.pid --unixctl=/var/run/ovn/ovnsb_db.ctl --detach --monitor --remote=db:OVN_Southbound,SB_Global,connections --private-key=db:OVN_Southbound,SSL,private_key --certificate=db:OVN_Southbound,SSL,certificate --ca-cert=db:OVN_Southbound,SSL,ca_cert --ssl-protocols=db:OVN_Southbound,SSL,ssl_protocols --ssl-ciphers=db:OVN_Southbound,SSL,ssl_ciphers --remote=ptcp:6642:[::] /etc/ovn/ovnsb_db.db
root     20535  0.0  0.0  12300  2816 pts/0    S+   17:42   0:00 grep --color=auto ovsdb

ovs内核模块

root@node1:~# lsmod |grep openvswitch
openvswitch           212992  18
nsh                    12288  1 openvswitch
nf_conncount           24576  1 openvswitch
nf_nat                 61440  4 ip6table_nat,openvswitch,iptable_nat,xt_MASQUERADE
nf_conntrack          208896  8 xt_conntrack,nf_nat,nfnetlink_cttimeout,openvswitch,nf_conntrack_netlink,nf_conncount,xt_MASQUERADE,ip_vs
libcrc32c              12288  5 nf_conntrack,nf_nat,openvswitch,nf_tables,ip_vs

 

关键概念

Datapath

使用kubesphere 开启ovn 安装的单节点集群。

Datapath(数据通路):在网络交换机和路由器中,数据通路是指转发数据包的路径。在 OVS 中,数据通路是用于处理和转发数据包的核心组件,查询到的网桥 br-int, 是ovs在所在节点创建的虚拟网桥。

VPort

VPort 实现:在 OVS 中,VPort 是通过内核模块和用户态进程协同工作来实现的。内核模块负责处理数据包的转发和接收,而用户态进程则负责控制和管理 VPort。

VPort 标识:每个 VPort 都有一个唯一的标识符,用于在 OVS 数据结构中进行识别和管理。

VPort 的使用场景

  • 数据包处理:当数据包到达一个 VPort 时,内核模块会将数据包从物理设备或其他 VPort 接收,并根据流表规则进行处理。处理可能包括数据包转发、过滤、NAT 等操作。

  • 与虚拟机连接:VPort 通常用于连接虚拟机或容器。通过 VPort,虚拟机可以与 OVS 进行通信,实现虚拟网络的构建和管理。

  • 虚拟交换机间通信:除了连接虚拟机外,VPort 也可以用于连接不同的虚拟交换机,实现虚拟网络之间的通信。

  • 管理和配置:用户态进程负责管理和配置 VPort,包括创建、删除、绑定到特定虚拟机等操作。

总的来说,VPort 在 OVS 中扮演了连接虚拟网络设备的重要角色,通过 VPort,OVS 能够实现虚拟网络的构建、管理和数据包处理。

  • 其中internal 接口类型是一个特定的端口类型,通常会被标记为 type: internal。它的流量通常不会直接暴露到物理网络中。

  • 这种类型的接口通常不具备物理网络接口的一些功能,例如无需设置 MAC 地址或与物理设备的接口绑定。

端口特性

下面是查询到的ovs具体场景中的网卡

root@node1:~# ovs-vsctl show
7fb85a99-5ae5-4321-938f-fc647f81c52d
    Bridge br-int                    //网桥
        fail_mode: secure
        datapath_type: system
        Port br-int
            Interface br-int         //代表网桥本身的端口,其他端口的流量会通过流入该端口进入ovs
                type: internal       
        Port mirror0
            Interface mirror0       // 主要用于调试数据的流量镜像端口(ovn创建,非ovs核心功能端口)
                type: internal
        Port "9c9076b62a19_h"        // 连接业务容器的端口
            Interface "9c9076b62a19_h"    
        Port c42b8d13d6ea_h
            Interface c42b8d13d6ea_h // 连接业务容器的端口
        Port "8f5f5cc5dc66_h"
            Interface "8f5f5cc5dc66_h"
        Port ovn0                   // ovn创建的作为网关的端口(ovn创建,非ovs核心功能端口)
            Interface ovn0
                type: internal
       //...
    ovs_version: "2.17.3"

对于OVS来讲,几种网卡类型的特性如下

1) netdev: 通用网卡设备 eth0 veth

接收: 一个nedev在L2收到报文后会直接通过ovs接收函数处理,不会再走传统内核协议栈.

发送: ovs中的一条流指定从该netdev发出的时候就通过该网卡设备发送

2) internal: 一种虚拟网卡设备

接收: 当从系统发出的报文路由查找通过该设备发送的时候,就进入ovs接收处理函数

发送: ovs中的一条流指定从该internal设备发出的时候,该报文被重新注入内核协议栈

3) gre device: gre设备. 不管用户态创建多少个gre tunnel, 在内核态有且只有一个gre设备

接收: 当系统收到gre报文后,传递给L4层解析gre header, 然后传递给ovs接收处理函数

发送: ovs中的一条流制定从该gre设备发送, 报文会根据流表规则加上gre头以及外层包裹ip,查找路由发送

br-int端口

  1. 基本功能

  • 作为虚拟设备的接口 Interface br-int 是与网桥 br-int 关联的虚拟网络设备,代表网桥自身。通过它,主机可以与网桥通信。

  • 配置和控制网桥 作为网桥的内部接口,它用于接收管理流量(如配置命令)或处理流量转发规则。


  1. 工作原理

  • 桥接点 br-int 作为集成网桥,通过 Interface br-int 与主机的网络栈交互。主机发送到 br-int 的数据包,实际上会通过 Interface br-int 进入 OVS 数据路径。

  • 辅助实现内部通信 br-int 网桥的所有端口(如连接容器的端口)之间的数据流量最终会通过 br-int 的转发逻辑实现。


  1. 使用场景

  • OpenStack 和 OVN 在云平台(如 OpenStack)和虚拟网络(如 OVN)中,br-int 是虚拟机和容器网络的核心桥梁。通过 Interface br-int,虚拟机、容器、以及外部网关可以完成通信。

  • 调试和监控 系统管理员可以使用工具(如 tcpdump)在 Interface br-int 上抓包,检查流量流动或排查问题。


  1. 示例作用

假设一个虚拟机通过一个端口(如 tap0)连接到 br-int 网桥。流量路径如下:

  1. 数据从虚拟机发出,经 tap0 端口进入 br-int

  2. br-int 根据流表规则处理数据流,可能转发到其他端口。

  3. 如果数据需要发到主机,流量会通过 Interface br-int 进入主机网络栈。


总结

Interface br-int 的作用可以概括为:

  1. 作为网桥 br-int 的内部接口,与主机网络栈交互。

  2. 提供一个管理和调试的入口。

  3. 在虚拟化环境中支持

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值