BESS【12】Writing Your Own Module

Overview

In general, writing a custom BESS module is very simple:

  1. Add new message types in protobuf/module_msg.proto if necessary. The message types are used as arguments for module initialization and commands.

  2. Define and implement the custom module class in core/modules/[new_module].[cc|h].

Message Type

Message types for module initialization and command arguments are defined in protobuf/module_msg.proto. Built In Modules and Ports documents all built-in types. You may need to define new types for your custom module. Types follow the naming convention below:

  • Init argument: [ModuleName]Arg (e.g. QueueArg)
  • Command argument: [ModuleName]Command[CommandName]Arg (e.g. BPFCommandClearArg)

Module Class

The module class should inherit Module from core/module.h. A minimum module class should include either ProcessBatch() or RunTask() function:

class MinimalModule final : public Module {
  public:
    CommandResponse Init(const bess::pb::MinimalModuleArg &arg);

    // Pick one
    struct task_result RunTask(void *arg) override;
    void ProcessBatch(bess::PacketBatch *batch) override;
}

Finally, register the module class by appending this line to the cc file:

ADD_MODULE(MinimalModule, "minimal", "description of your module")

Command

In addition to Init(), modules may be configured via commands. To add a command, first define a static const variable cmds as well as command handlers inside the module class definition.

class MinimalModule final : public Module {
 public:
  ...
  static const Commands cmds;

  CommandResponse CommandAdd(const bess::pb::MinimalModuleCommandArg &arg);
  CommandResponse CommandClear(const bess::pb::EmptyArg &arg);
  ...
};

Then initialize this variable in the cc file:

const Commands MinimalModule::cmds = {
    {"add", "MinimalModuleCommandArg", MODULE_CMD_FUNC(&MinimalModule::CommandAdd), 0},
    {"clear", "EmptyArg", MODULE_CMD_FUNC(&MinimalModule::CommandClear), 0}};

Commands is simply a typedef of std::vector<struct Command>. The last field of struct Command denotes whether the command is thread-safe or not.

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茉帅,一起剝皮案震驚了整個濱河市箍铲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌济瓢,老刑警劉巖跪者,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件棵帽,死亡現(xiàn)場離奇詭異,居然都是意外死亡渣玲,警方通過查閱死者的電腦和手機逗概,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來忘衍,“玉大人逾苫,你說我怎么就攤上這事∶兜觯” “怎么了铅搓?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長搀捷。 經(jīng)常有香客問我星掰,道長,這世上最難降的妖魔是什么嫩舟? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任氢烘,我火速辦了婚禮,結果婚禮上至壤,老公的妹妹穿的比我還像新娘威始。我一直安慰自己,他們只是感情好像街,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布黎棠。 她就那樣靜靜地躺著,像睡著了一般镰绎。 火紅的嫁衣襯著肌膚如雪脓斩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天畴栖,我揣著相機與錄音随静,去河邊找鬼。 笑死,一個胖子當著我的面吹牛燎猛,可吹牛的內(nèi)容都是我干的恋捆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼重绷,長吁一口氣:“原來是場噩夢啊……” “哼沸停!你這毒婦竟也來了?” 一聲冷哼從身側響起昭卓,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤愤钾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后候醒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體能颁,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年倒淫,在試婚紗的時候發(fā)現(xiàn)自己被綠了伙菊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡敌土,死狀恐怖占业,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情纯赎,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布南蹂,位于F島的核電站犬金,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏六剥。R本人自食惡果不足惜晚顷,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望疗疟。 院中可真熱鬧该默,春花似錦、人聲如沸策彤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽裹刮。三九已至庞瘸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間违霞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工涧郊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留癞谒,地道東北人底燎。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓双仍,卻偏偏與公主長得像桌吃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子茅诱,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354