nvme-cli 插件架构

记录一下,对nvme-cli 的插件架构的理解

  • 主要是利用define 和 undefine,反复编辑 COMMAND_LIST ENTRY PLUGIN
  • 真正的 dell-nvme.h 文件是宏展开后的效果,下面给demo
demo-nvme.h
// Stage 1: 函数原型

static int cmd1_impl(int argc, char **argv, struct command *acmd, struct plugin *plugin);
static int cmd2_impl(int argc, char **argv, struct command *acmd, struct plugin *plugin);

// Stage 2: 命令结构体
static struct command cmd1_cmd = {
    .name = "cmd1",
    .help = "Command 1",
    .fn = cmd1_impl,
    .alias = NULL};
static struct command cmd2_cmd = {
    .name = "cmd2",
    .help = "Command 2",
    .fn = cmd2_impl,
    .alias = "c2"};

// Stage 3: 命令列表
static struct command *commands[] = {
    &cmd1_cmd,
    &cmd2_cmd,
    NULL};

// Stage 4: 插件注册
static struct plugin plugin = {
    .name = "my_plugin",
    .desc = "plugin_description",
    .version = "1.0",
    .commands = commands};

static void init(void)
{
    register_extension(&plugin);
}
  • 为了避免每次#include 这个头文件都会 去重新展开成头文件,用#ifdef CREATE_CMD 来控制,只有当你想要将宏展开成头文件定义的时候,才定义此宏
  • __attribute__((constructor)) 是gcc的新特性,自动调用,可以将这些插件都挂到链表上,链表是一个全局变量,
  • 还有一个问题,第一次#include inspur-nvme.h 那些宏的定义都还没有展开呢 CMD_HEADER_MULTI_READ 此时就没有定义。直接执行下面的 #include "define_cmd.h"
posted @ 2025-11-13 17:58  年华似水゛  阅读(7)  评论(0)    收藏  举报