Linux Multipath代码分析

本文详细分析了Linux内核中的Multipath技术,包括Device Mapper的原理、Multipath的基本概念、路径管理和切换机制,以及Multipath的实现细节。重点探讨了Device Mapper的内核对象、用户态功能、路径发现、状态检查和事件处理流程,同时介绍了Multipath的配置选项和关键函数实现。通过对代码的深入剖析,揭示了Linux Multipath在高可用性和负载均衡方面的工作原理。
摘要由CSDN通过智能技术生成


前面两部分主要摘抄于其它的论文和资料,后面部分是对代码的分析结果。

1       Multipath概述

   DM-Multipathing (DM-MPIO) provides I/O failover and load-balancing within Linux for block devices. By utilizing device-mapper, multipathd provides the host-side logic to use multiple paths of a redundant network to provide continuous availability and higher bandwidth connectivity between the host server and the block-level device.

                         

2       Multipath基本原理

2.1     Device Mapper

在内核中它通过一个一个模块化的 target driver 插件实现对 IO 请求的过滤或者重新定向等工作,当前已经实现的 target driver 插件包括软 raid、软加密、逻辑卷条带、多路径、镜像、快照等,图中 linear、mirror、snapshot、multipath 表示的就是这些 target driver。

 Device mapper 进一步体现了在 Linux 内核设计中策略和机制分离的原则,将所有与策略相关的工作放到用户空间完成,内核中主要提供完成这些策略所需要的机制。Device mapper 用户空间相关部分主要负责配置具体的策略和控制逻辑,比如逻辑设备和哪些物理设备建立映射,怎么建立这些映射关系等等,而具体过滤和重定向 IO 请求的工作由内核中相关代码完成。因此整个 device mapper 机制由两部分组成,内核空间的 device mapper 驱动、用户空间的device mapper 库以及它提供的 dmsetup 工具。

 

 

2.1.1     Device Mapper内核对象

 

mapped_device: device mapper生成的逻辑块设备,map指向dm_table

dm_table: mapped_device和物理设备target的映射,按照B树组织dm_target,便于IO请求映射的查找

dm_target: 记录映射的mapped device的逻辑区域的开始地址和范围,type指向target设备的驱动类型(target_type),private记录具体设备的数据。

target_type multipath_target: mulitpath类型的target的driver,包括创建、销毁device的方法。

Device mapper本质功能就是根据映射关系和target driver描述的IO处理规则,将IO请求从逻辑设备mapped device转发相应的target device上。Device mapper处理所有从内核中块一级IO子系统的generic_make_request和submit_bio接口中定向到mapped device的所有块读写IO请求。IO请求在device mapper的设备树中通过请求转发从上到下地进行处理。当一个bio请求在设备树中的mapped deivce向下层转发时,一个或者多个bio的克隆被创建并发送给下层target device。target driver结束某个bio请求后,将表示结束该bio请求的事件上报给它上层的mapped device,该过程在各个层次上进行直到该事件最终上传到根mapped device的为止,然后device mapper结束根mapped device上原始bio请求,结束整个IO请求过程。

2.1.2     Device Mapper用户态功能

(1)           发现每个mapped device相关的target device;

(2)           根据配置信息创建映射表;

(3)           将用户空间构建好的映射表传入内核,让内核构建该mapped device对应的dm_table结构;

(4)           保存当前的映射信息,以便未来重新构建。

2.2     Multipath

The Device Mapper multipath module of the Linux kernel:multipath_target

The multipath-tools userspace package:multipath-tools take care of automatic path discovery and grouping as well as automated path retesting, so that a previously failed path is automatically reinstated when it becomes healthy again. This minimizes the need for administrator attention in a production environment. Include Multipathd service and multipath(CLI)

 

 

 

2.2.1     multipath_target

主要提供的方法(每种类型的target都必须提供):

multipath_ctr:构建target device 的方法

multipath_dtr:删除target device 的方法

multipath_map:Target的映射IO请求的方法,调用map_io

multipath_status:获取当前target device状态的访问

multipath_message: Target 处理用户消息的方法,根据具体的消息类型会调用fail_path, queue_if_no_path, switch_pg_num等方法

multipath_end_io:Target结束IO请求的方法

 

3       Multipath实现分析

3.1     对象

Multipath

Pathgroup(优先组)

Path

配置文件中path_grouping_policy项如果设定为failover,则每个path都在不同的pathgroup,如果设置为multibus,则所有的path都在一个pathgroup。

3.2     配置

  path_grouping_policy

path_checker:指定用于决定路径状态的默认方法,包括:readsector0\directio

failback: 路径出错恢复正常后切换策略,包括immediate/manual/数字(代表时间)

no_path_retry:指定所有路径都失败后应该再试图尝试的次数。Queue/fail/数字(代表次数)

path_selector:指定用来决定下一个 I/O 操作所使用路径的默认算法。Round-robin/queue-length。

Prio: 指定要获得路径优先值所需调用的默认程序及参数。默认是const,即优先值都为1

Hardware_handler(device配置):指定将在切换路径组群或者处理 I/O 错误时用来执行硬件具体动作的模块。配置值:emc/hp_sw/rdac。默认为”0”

3.3     事件机制

The multipath daemon learns of and reacts to changes in both the current block device configuration and the kernel resident multipathing configuration.

The addition of a new path or the removal of an already existing path to an already managed block device is detected over a netlink socket as a uevent triggered callback which adds or removes the path to or from the set of paths which will be actively tested.

Changes to the kernel resident multipathing state are detected as device-mapper generated event callbacks. Events of this kind invole block io errors, path state change, and changes i

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值