Slide 1

Slide 1 text

BitVisor Summit 6 Implementation and Current Status of 'mruby in BitVisor’ 中⽥ 裕貴 松原 克弥 公⽴はこだて未来⼤学 2017年12⽉5⽇

Slide 2

Slide 2 text

⾃⼰紹介 中⽥ 裕貴(なかた ゆうき) • Twitter: chikuwa_IT • 公⽴はこだて未来⼤学 システム情報科学部 2年 • システムソフトウェア研究室居候 松原 克弥(まつばら かつや) • 公⽴はこだて未来⼤学 システムソフトウェア研究室主宰 • BitVisor歴10年超 • 今回の仕掛け⼈ 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 2

Slide 3

Slide 3 text

背景 • ハードウェアプラットフォームのプログラマブル化 • SDN/ホワイトボックス・スイッチ • FPGAエクストリーム・コンピューティング • ディープラーニングでの活⽤ • 組み込み可能プログラミング⾔語フレームワークの登場 • Lua • ECL (Embeddable Common-Lisp) • micropython • mruby 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 3

Slide 4

Slide 4 text

⽬的 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 4 • BitVisorの主な(追加)処理 • OS・デバイスI/Oの監視・加⼯ • Alkanet, DeviceDisEnabler, ADvisor, ‥ • アプリケーションの隔離実⾏ • ⼩清⽔他, “BitVisor による動画像を対象とした著作権保護”, BitVisor Summit, 2012. mrubyをBitVisorに組み込んで、Rubyによるエレガントな記 述を可能にする 引⽤:https://www.ruby-lang.org/ja/

Slide 5

Slide 5 text

システムアーキテクチャ 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 5 mrubyのVMを BitVisoコアに リンク mrbgemsも 必要なものを 移植 rubyコードは、 バイトコンパイル したものを Cコード内に include & interactive shell (mirb) も移植

Slide 6

Slide 6 text

記述例(未確定) • マルチキャスト通信を遮断 • 「01:00:5E」から始まる宛先MACアドレスのパケットか どうかを判別 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 6 def is_multicast_packet(header) header.map!{|i| i.to_s(16)}.join(“:”) if header =~ /^01:00:5E/ then Bitvisor.print(“A packet dropped:#{header}¥n”) true

Slide 7

Slide 7 text

技術的課題 1. libc関数呼び出し 2. 浮動⼩数点演算 3. BitVisor 内部インタフェースへのアクセス 4. interactive shell (mrib) の移植 5. mrbgemの移植 6. サンプル処理 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 7

Slide 8

Slide 8 text

1. libc関数呼び出しの対処 • あるものは、仕様の違いをラッピング ないものは、(場合によっては、ダミーを)作成 • strtol • strtoul • abort • exit • fprintf • memprintf • fwrite • strncmp • memmove • memchr • atoi 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 8 • strchr • __errno_location • __stack_chk_fail • exit

Slide 9

Slide 9 text

メモリアロケータのオーバライド • mruby実装で⽤意されているオーバライド関数でメモリ アロケータをBitVisorのものに置き換え(+仕様調整) • アロケータを”mrb_open_allocf”で指定 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 9 void *mrb_allocate(struct mrb_state *mrb, void *p, size_t size, void *ud) { if (size == 0){ if(p != 0 ) free(p); return NULL; }else { return realloc(p, size); } } BitVisorのfree()の仕様が 標準的なlibcのものと異なる (ML [BitVisor-devel:108] 参照)

Slide 10

Slide 10 text

2. 浮動⼩数点演算の排除 • float, doubleを整数型にキャスト、浮動⼩数点演算関数を 無効化 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 10 typedef long mrb_float; # define double long ... # define fmod(x,y) (x) # define pow(x,y) (x) # define log10(x) (x) # define floor(x) (x) # define ceil(x) (x) # define isinf(x) 0 # define isnan(x) 0 # define isfinite(x) 0 ... mrubyの最新コードでは、 「MRB_WITHOUT_FLOAT」 ビルドオプション設定により、 浮動⼩数点関連の処理が すべて排除可能に! 参照: https://twitter.com/pandax381/status/926685657958817792

Slide 11

Slide 11 text

Another Solution: Berkeley SoftFloat の導⼊ • Berkeley SoftFloatを使⽤ • 16ビットの半精度、32ビットの単精度、 64ビットの倍精度、 80ビットの拡張倍精度倍精度、128ビットの四倍精度に対応 • mruby内の浮動⼩数点演算をラップ • 置き換え対象はnumeric.c、string.c、gc.c、vm.c、parse.y • mathライブラリ関数log, powなどは⾃作 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 11

Slide 12

Slide 12 text

3. BitVisor内部APIのインタフェー ス • Bitvisorクラスを作って、メソッドとしてラッピング関数 を登録 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 12 bitvisor = mrb_define_class(mrb, "Bitvisor", mrb->object_class); mrb_define_class_method(mrb,bitvisor, "print", bitvisor_print,ARGS_REQ(1)); mrb_define_class_method(mrb,bitvisor, "get_time",bitvisor_get_time,ARGS_NONE()); mrb_define_class_method(mrb,bitvisor, "set_schedule",bitvisor_set_schedule, ARGS_NONE()); ...

Slide 13

Slide 13 text

4. Interactive Shell (mirb) の移植 dbgsh経由でmirbとインタラクションできるようにする • 現在は、Rite VM等をBitVisorコアへリンク • dbgshの実体はプロセスで動作 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 13 mirb_msghandler(int m, int c, struct msgbuf *buf,int bufcnt) { ... p=buf->base; mrb_ret_value = mrb_load_string_cxt(mirb,p,mrb_cxt); if(mrb_fixnum_p(mrb_ret_value)){ int intbuf= mrb_fixnum(mrb_ret_value); printf(">> %d¥n",intbuf); } if(mrb_string_p(mrb_ret_value)){ char *charbuf = RSTRING_PTR(mrb_ret_value); printf(">> %s¥n",charbuf); }

Slide 14

Slide 14 text

5. 正規表現mrbgemの移植 「mruby-pcre-regexp」を移植 • 基本的には、mruby本体(VM)の移植と同じ⽅法 • libc関数 • 浮動⼩数点演算 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 14

Slide 15

Slide 15 text

6. サンプル処理の実装 • 送信Ethernetパケットの監視 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 15 prev_addr = Bitvisor.get_dest_macaddr.map{|i| i.to_s(16)}.join(":") loop do Bitvisor.set_schedule cur_addr = Bitvisor.get_dest_macaddr.map{|i| i.to_s(16)}.join(":") if cur_addr != prevaddr then Bitvisor.print("MAC addr of dest = #{cur_addr}¥n") prev_addr =cur_addr end end

Slide 16

Slide 16 text

実装状況 「とりあえず動作」の段階 • mrubyを個別にビルドしてBitVisorへリンク • ラッピングできているBitVisor内部APIは限定的 • mirbが複数⾏対応できていない • 1⾏毎に実⾏ • mrbgemパッケージも⾜りない 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 16

Slide 17

Slide 17 text

デモ:dbgsh経由のmrib動作 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 17 10.times do |n|; Bitvisor.print(“#{n} Hello”); end

Slide 18

Slide 18 text

まとめと今後の展望 • mrubyランタイムをBitVisorへ移植 • 必要なlibc関数を作成 • 浮動⼩数点演算への対処 • BitVisor 内部APIのラッピング • dbgsh向けmirbインタフェースの実装 • 正規表現mrbgemの移植 • サンプル処理で動作を確認 今後の展望 • mruby VMのプロセス化 • 性能評価 • キラーアプリの開拓 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 18

Slide 19

Slide 19 text

github.com/chikuwait/bitvisor_mruby 2017.12.5 Implementation and Current Status of 'mruby in BitVisor' 19