前面六章讲的都是软件怎么收包。这一章讲另一条路:
让网卡自己收包、自己判断、自己转发。这就是 rte_flow。它是 DPDK 18.11 时代出现、到今天已经成了
现代 DPDK 数据面的核心 API 的那套东西。读完之后你应该能回答:
- 一条 rte_flow 规则由哪两段组成?它们分别回答什么问题?
- group 和 priority 有什么区别?为什么 group 需要 JUMP 才能串起来?
- 规则的四个生命周期 API 是什么?销毁有什么顺序要求?
- 网卡不支持某条规则时会怎样?(答案不是「软件兜底」)
本文所有行号基于 /Users/growduduan/cwork/dpdk(27.03.0-rc0)。
7.1 为什么要有 rte_flow
在 rte_flow 之前,每个包都要进 CPU:收包、查表、改头、发回网卡。
就算只是「按 MAC 转发」这种最简单的活,也是 CPU 全程参与。
rte_flow 把这件事变成:
收益:CPU 不碰绝大多数包,带宽能上到几百 Gbps。
代价:表达能力受限 —— 硬件能匹配的东西是有限的,而且每款网卡支持的项不一样。
7.2 一条规则的两段式
一条 rte_flow 规则由两个平行数组组成:
struct rte_flow_item patterns[MAX_PATTERN_NUM] = {0}; /* 匹配什么 */
struct rte_flow_action actions[MAX_ACTION_NUM] = {0}; /* 做什么 */
examples/flow_filtering/flow_skeleton.c:59-60。
7.2.1 pattern:匹配什么
struct rte_flow_item {
enum rte_flow_item_type type; /* Type of item. */
const void *spec; /* Pointer to item specification. */
const void *last; /* Pointer to last item's specification. */
const void *mask; /* Pointer to bitmask to apply to specs. */
};
lib/ethdev/rte_flow.h:2085。
| 字段 | 含义 |
|---|---|
type |
匹配项的类型(ETH / IPV4 / TCP / UDP / VLAN …) |
spec |
匹配的具体值 |
last |
闭区间的上界 —— 用来匹配一段范围(比如端口区间) |
mask |
位掩码,先于 spec 生效 —— 用来做通配 |
type 的枚举 enum rte_flow_item_type 在 lib/ethdev/rte_flow.h:153。
7.2.2 action:做什么
struct rte_flow_action {
enum rte_flow_action_type type; /* Action type. */
const void *conf; /* Pointer to action configuration. */
};
lib/ethdev/rte_flow.h:4146。
动作链以 RTE_FLOW_ACTION_TYPE_END 收尾(:4142 的注释说明了这一点)。
| 常见 action | 作用 |
|---|---|
QUEUE |
转到某个队列(最常用,等价于软件里的路由查表结果) |
DROP |
丢包 |
JUMP |
跨到另一个 group |
MARK |
打标记(供后续规则或软件匹配) |
FLAG |
设置标志位 |
VLAN / MPLS |
改协议头 |
RULE |
插入一条子规则(嵌套匹配) |
7.2.3 一个完整的例子
examples/flow_filtering/jump_flow.c:17-38:
struct rte_flow_attr flow_attr = {
.ingress = 1,
.group = 0,
};
struct rte_flow_action_jump jump = {
.group = group_id,
};
/* Set up jump action to target group */
actions[0].type = RTE_FLOW_ACTION_TYPE_JUMP;
actions[0].conf = &jump;
actions[1].type = RTE_FLOW_ACTION_TYPE_END;
/* match on ethernet */
patterns[0].type = RTE_FLOW_ITEM_TYPE_ETH;
patterns[1].type = RTE_FLOW_ITEM_TYPE_END;
/* Validate the rule and create it. */
if (rte_flow_validate(port_id, &flow_attr, patterns, actions, error) == 0)
flow = rte_flow_create(port_id, &flow_attr, patterns, actions, error);
注意最后两行:validate 先试一次,成功了才 create。
这是 rte_flow 的标准范式 —— 先验证再创建,
避免在硬件上写了一半失败留下半成品。
7.3 group 与 priority:规则怎么组织
struct rte_flow_attr {
uint32_t group;
uint32_t priority;
uint32_t ingress:1;
uint32_t egress:1;
uint32_t transfer:1;
uint32_t reserved:29;
};
lib/ethdev/rte_flow.h:89。
7.3.1 group 是层级,不是分类
头文件里的注释写得很清楚(:91-99):
A group is a superset of multiple rules.
The default group is 0 and is processed for all packets.
Rules in other groups are processed only if the group is chained
by a jump action from a previously matched rule.
It means the group hierarchy is made by the flow rules,
and the group 0 is the hierarchy root.
三点:
- group 0 是根,对所有包都处理
- 其他 group 不会被自动处理 —— 必须由前一条规则用 JUMP 显式跳过去
- group 层级由规则自己构成,DPDK 不替你建
⚠️ 注释里还有一句:没有自动的死循环保护。
两个 group 互相 JUMP 会让网卡卡死或行为未定义。写规则时要自己保证没有环。
7.3.2 priority 是组内优先级
uint32_t priority; /**< Rule priority level within group. */
:101。
注释(:65-71)里有两条关键限制:
| 规则 | 说明 |
|---|---|
| 优先级可以任意取值 | 不需要连续,不需要从 0 开始 |
| 同 group 同 priority 的多条规则都匹配时 | 结果是未定义的 —— 可能走任意一条路径,可能重复执行,甚至出错 |
实践:同一个 (group, priority) 下只放一条规则。
这是最容易被忽略、后果又最严重的约束。
7.3.3 方向位
| 位 | 含义 |
|---|---|
ingress |
规则作用于入向流量 |
egress |
规则作用于出向流量 |
transfer |
规则作用在「设备端点之间」的流量上,不是某个 DPDK 端口 |
transfer 是 22.11 之后加的,用于在 VF / representor 场景里做跨端点重定向。
7.4 规则的四个生命周期 API
| API | 声明 | 实现 | 作用 |
|---|---|---|---|
rte_flow_validate() |
rte_flow.h:4547 |
— | 只验证不创建,返回错误码 |
rte_flow_create() |
:4574 |
rte_flow.c:484 |
真正下到硬件 |
rte_flow_query() |
:4672 |
rte_flow.c:597 |
查规则的命中数、统计 |
rte_flow_destroy() |
:4601 |
rte_flow.c:515 |
删除规则 |
7.4.1 驱动侧:ops 表
struct rte_flow_ops {
lib/ethdev/rte_flow_driver.h:39。PMD 要实现的关键回调:
| 回调 | 行号 | 作用 |
|---|---|---|
validate |
:41 |
检查这条规则我支不支持 |
create |
:48 |
下到硬件 |
destroy |
:55 |
从硬件删掉 |
flush |
:60 |
清空这个 group |
query |
:64 |
查统计 |
get_aged_flows |
:82 |
查老化掉的规则(转发表定期同步用) |
rte_flow_create() 的实现(lib/ethdev/rte_flow.c:484)就是取 ops、转发:
ops = rte_flow_ops_get(port_id, ...);
...
flow = ops->create(port_id, attr, patterns, actions, error);
7.4.2 销毁有顺序要求
rte_flow.h:4586-4587 的注释:
要求按创建的逆序销毁。
因为 JUMP 建立的是 group 之间的引用 —— 跳转到 group 1 的规则,
必须先销毁跳过去的那些,再销毁跳的这一条。
实践:维护一个栈,每建一条规则就压栈,销毁时从栈顶弹。
别自己写个队列去管理规则的生命周期。
7.5 一个必须知道的误解:没有软件兜底
很多文章会说「网卡不支持就自动走软件」。
DPDK 没有这个机制。
$ grep -rn "rte_flow_table" lib/ --include="*.h" --include="*.c"
lib/ethdev/rte_flow.h:5707 (只是枚举里的一项)
lib/ethdev/rte_flow.h:5725 (同上)
rte_flow_table 只存在于 mlx5 和 cnxk 驱动内部,是那两家自己实现的
硬件表,不是通用软件引擎。lib/table/ 里的 rte_swx_table.h 是 SWX 软件交换机的表,
跟 rte_flow 无关。
所以正确的做法是:
if (rte_flow_validate(port_id, &attr, patterns, actions, &error) < 0) {
/* 这条规则硬件不支持 —— 自己在软件里实现同样的逻辑 */
return software_fallback(...);
}
先 validate,根据返回值决定走硬件还是软件。
这是 rte_flow 唯一正确的降级方式。
7.6 模板化 API:批量下规则
批量下成百上千条规则时,逐条 rte_flow_create() 会很慢
(每条都要一次硬件命令往返)。
模板化 API 把「规则的形状」和「规则的内容」分开:
| API | 作用 |
|---|---|
rte_flow_pattern_template_create() |
创建一个 pattern 模板 |
rte_flow_actions_template_create() |
创建一个 action 模板 |
rte_flow_template_table_create() |
把模板组成一张表 |
rte_flow_async_create() |
异步插入规则,不阻塞 |
用法见 examples/flow_filtering/flow_skeleton.c:43-52:
return rte_flow_async_create(port_id,
1, /* Flow queue used to insert the rule. */
attr,
table,
patterns,
0, /* Pattern template index in the table. */
actions,
0, /* Actions template index in the table. */
0, /* user data */
error);
26.11 还给 flow item 加了默认掩码与规格结构体大小查询:
RTE_FLOW_CONV_OP_ITEM_DEFAULT_MASK_PTR与RTE_FLOW_CONV_OP_ITEM_SIZE
(lib/ethdev/rte_flow.h:4428,4446,接口是rte_flow_conv():4836)。
写工具(比如控制面)时用得上。
7.7 本章要点回顾
- 规则 = pattern 数组 + action 数组(
examples/flow_filtering/flow_skeleton.c:59-60) - group 0 是根,其他 group 必须靠 JUMP 显式跳过去,没有死循环保护
- 同 (group, priority) 下只放一条规则,否则行为未定义
- 四个生命周期 API:validate → create → query → destroy,销毁要按创建的逆序
- 没有通用软件兜底 ——
rte_flow_table只在 mlx5/cnxk 驱动里 - 先 validate 再 create,按 validate 的结果决定硬件还是软件
7.8 接下来
- 08 章:rte_graph 与 rte_node —— 把转发逻辑组织成一张图,
由 DPDK 调度这张图上的节点,26.11 刚重写过它的调度算法。 - 09 章:rte_eventdev 与 dispatcher —— 第三种编程模型:
事件驱动,包不再属于某个队列,而是被投递给订阅它的处理者。