Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rustで作るLinuxトレーサ / libbpf-core-with-rust

Rustで作るLinuxトレーサ / libbpf-core-with-rust

@RustのLT会 Shinjuku.rs #15
https://forcia.connpass.com/event/206698/

KONDO Uchio

April 27, 2021
Tweet

More Decks by KONDO Uchio

Other Decks in Technology

Transcript

  1. F#1'ͱ͔͍͏ࠓྲྀߦͬͯΔ΍ͭ
    6DIJP,POEP(.01FQBCP *OD
    4IJOKVLVST
    3VTUͰ࡞Δ-JOVYτϨʔα
    1IPUPCZ)FSCFSU#JFTFSIUUQTQJYBCBZDPNJNBHFTJE

    View full-size slide

  2. γχΞɾϓϦϯγύϧΤϯδχΞ
    ۙ౻ Ӊஐ࿕ / @udzura
    https://blog.udzura.jp/
    Uchio Kondo
    ٕज़෦ ٕज़ج൫νʔϜ @ GMOϖύϘ / ෱Ԭ
    ΤϯδχΞΧϑΣʢ෱Ԭࢢ੺ẂנจԽձؗʣ αϙʔλʔ
    Duolingo Diamond Leaguer 💎
    #Rust #Ruby #mruby #Containers #eBPF #CRIU #Seccomp
    #RubyKaigi #CloudNativeDays #Zumba #γϨϯ #FitBoxing2

    View full-size slide

  3. ࠓճ࿩͢΋ͷҎ֎ʹ࡞ͬͨ΋ͷ
    •octx
    wίϚϯυҰൃͰࡶʹ(JU)VCͷBDUJWJUZΛ$47ʹͯ͘͠ΕΔ΍ͭ
    ͦͷ··#JH2VFSZͰϩʔυՄೳɻ
    wPDUPDSBC SFRXFTU UPLJP

    View full-size slide

  4. 5P$
    w࡞Ζ͏ͱ͍ͯ͠Δπʔϧͷ࿩
    wF#1'ͱ͸
    wࠓࠓͰ͖͍ͯΔ΋ͷ
    wMJCCQGSTͷ࢖͍ํ
    w3VTUͰΑ͔ͬͨ఺

    View full-size slide

  5. F#1'
    wFYUFOEFE#FSLMFZ1BLFU'JMUFS
    w೥͔Β-JOVYͰಋೖʢݩͷ࣮૷͸-JOVYͰ͸͔Βʣ
    wMJCQDBQͰ࢖ΘΕ͍͕ͯͨɺTFDDPNQ΍ͦͷଞΧʔωϧػೳͰར༻
    w-JOVYΧʔωϧ಺෦Ͱಈ͘ϓϩάϥϜΛɺϢʔβϥϯυͰهड़
    wػೳʹ੍ݶ͕͋ΓɺϑΟϧλϦϯά΍ूܭ͕ओͳ໾ׂ
    wৄࡉ͸ʮ͓͍ͯͭ͘͠Α͘ͳΔF#1'ʯͰݕࡧ

    View full-size slide

  6. F#1'ʹΑΔτϨʔα
    wF#1'ͷେ͖ͳ༻్ͷҰͭʹɺΧʔωϧ಺෦Ͱͷؔ਺ݺͼग़͠΍
    ੩తͳΠϕϯτͳͲΛτϨʔε͠ɺूܭ͢Δ͜ͱ͕͋Δɻ
    w#4%4PMBSJTͰ͍͏%5SBDF
    wCQGUSBDFίϚϯυ΍
    #$$ #1'$PNQJMFS$PMMFDUJPO
    ͷ։ൃ
    IUUQXXXCSFOEBOHSFHHDPNCMPHCQGQFSGPSNBODFUPPMTCPPLIUNM

    View full-size slide

  7. #1'$03&
    wͦΕ·Ͱͷ#1'πʔϧ͸ɺͦͷ৔Ͱ#1'ϓϩάϥϜΛੜ੒ɺίϯύΠ
    ϧ͢ΔͳͲʢॊೈͰ͸͋Δ͕ʣΦʔόʔϔου͕͋ͬͨɻ
    wͦͷ݁Ռɺಈ࡞ʹDMBOHόΠφϦ͕ඞཁͩͬͨΓɺಋೖ͕ϋʔυ
    w΋͏গ͠खܰʹಈ͔ͤΔΑ͏ɺΧʔωϧͷܕ৘ใΛऔಘ͢Δ#5'΍ɺ
    $03& $PNQJMF0ODF 3VO&WFSZXIFSF
    ͳͲͷٕज़͕ग़ͯ͘Δ
    w$03&ʹͳΔͱϫϯόΠφϦπʔϧ Χʔωϧͷઃఆ͍͔ͭ͘
    ΛΠ
    ϯετʔϧ͢Ε͹τϨʔε͕Ͱ͖ΔΑ͏ʹɻ

    View full-size slide

  8. ࣮૷ͷྫ
    w#1'ͷϓϩάϥϜΛϏϧυ͢Δ
    $Ͱॻ͘ඞཁ͕͋ΔɻϓϩάϥϜͷҰ෦
    ίϯύΠϧ࣌ͷΦϓγϣϯ

    View full-size slide

  9. ࣮૷ͷྫ
    w#1'ϓϩάϥϜͷΦϒδΣΫτ͔ΒTLFMʢϔομʣΛੜ੒͠ɺ
    Ϣʔβϥϯυͷ$ϓϩάϥϜ͔Β࢖͑ΔΑ͏ʹ͢Δ
    CQGUPPMͱ͍͏ίϚϯυͰੜ੒
    ϓϩάϥϜͷόΠφϦࣗମ͕ϔομʹຒΊࠐ·ΕΔ

    View full-size slide

  10. ࣮૷ͷྫ
    wͦͷϔομΛ࢖ͬͯόΠφϦπʔϧΛ࡞Δɻ
    w$ݴޠͰɻ
    wͪͳΈʹCDDͰͷSFGFSFODF࣮૷
    w IUUQTHJUIVCDPNJPWJTPSCDDUSFFNBTUFSMJCCQGUPPMT

    View full-size slide

  11. ग़དྷ্͕ͬͨόΠφϦͷΠϝʔδ
    Ϣʔβϥϯυ
    Χʔωϧ಺෦
    ϓϩάϥϜʢϫϯόΠφϦʣ
    #1'ϓϩάϥϜ
    ϩʔυ
    σʔλ΍ΓऔΓ
    ϓϩάϥϜ
    ࣮ߦɾूܭ
    Φϓγϣϯͷղੳ
    ग़ྗ Ճ޻
    #1'
    ࣮ߦ࣌ݕࠪ
    #1'.BQ
    1FSGCV⒎FS
    ར༻ऀ

    View full-size slide

  12. ͜͜·Ͱͷͬ͘͟Γͨ͠·ͱΊ
    wF#1'͍ͬͯ͏-JOVYͷτϨʔαͳͲΛ࡞Δศརٕज़͕͋Δ
    wF#1'πʔϧ͸ɺҎલ͸DMBOHͰͦͷ৔ͰίϯύΠϧ͍͕ͯͨ͠ɺ
    ࠷ۙϫϯόΠφϦͰ഑ΕΔΑ͏ʹͳͬͨ ཁ৽͍͠Χʔωϧ

    wͦ͏͢ΔͱίϚϯυϥΠϯπʔϧ෦෼΋ؚΊͯϫϯόΠφϦʹ͍ͨ͠
    wͦͷSFGFSFODF࣮૷͸$ݴޠɻ$ݴޠɻ
    ʮ1SPEVDUJPOͰ͸#$$ΑΓMJCCQG#1'$03&Λ࢖͏ํ͕ྑ͍ʯ
    IUUQTJUJTLKIBUFOBCMPHDPNFOUSZͳͲ΋ࢀর

    View full-size slide

  13. Ϣʔβϥϯυ΋$ݴޠͰ͋Δඞཁ
    wͬͯɺຊ౰ʹ͋Δʁ
    w$ݴޠͷ೉͠͞
    wΦϓγϣϯύʔεҰͭͰ΋จࣈྻͷѻ͍ʹۤ࿑͢Δ
    wͪΐͬͱͨ͜͠ͱͰ΋ෆ҆શͳίʔυ͕
    wϝϞϦͷѻ͍͕ϝϞϦ͕
    w#1'ϓϩάϥϜ͸࢓ํͳ͍ʢWFSJpFS΋͋Δʣ͚ͲɺϢʔβϥϯυ΋
    $ͬͯେม͡Όͳ͍ʁ

    View full-size slide

  14. ͭͬͨ͘΋ͷ

    View full-size slide

  15. DPOUBJOFSUSBDFST
    wίϯςφؔ܎ͷτϨʔεʹಛԽͨ͠πʔϧ܈Λ࡞Ζ͏ͱ͍ͯ͠Δ
    w൚༻తͳ΋ͷ͸͢Ͱʹ͋ΔͷͰ
    w3VTUMJCCQGST
    w$03&ͳͷͰΧʔωϧ͕
    ͔ͳΓ৽͘͠ͳ͍ͱͰ͕͢
    IUUQTHJUIVCDPNVE[VSBDPOUBJOFSUSBDFSTɹ

    View full-size slide

  16. DPOTUBU

    wίϯςφ୯ҐͰͷ
    γεςϜίʔϧݺͼग़͠ͷ
    ౷ܭΛදࣔ͢Δ

    View full-size slide

  17. VOTIBSFTOPPQ

    wVOTIBSF
    ͱ͍͏ɺίϯςφΛ࡞੒͢Δ্ͰॏཁͳγεςϜίʔϧ
    ͷݺͼग़͠ͱɺͦͷϑϥάΛτϨʔε͢Δ

    View full-size slide

  18. DPOCJPHSBQI

    wίϯςφ୯ҐͰɺCMPDL*0ͷঢ়گΛՄࢹԽ͢Δɻ
    wάϥϑදࣔͰ͸ͬͪΌ͚͗ͨ͢ͷͰɺ΋͏গ͠ݟ΍͍͢දࣔΛࠓޙ࣮
    ૷͍ͨ͠ʜ

    View full-size slide

  19. %FNP
    wDPOTUBU

    ࠓࢥ͏ͱQFSGͰ΋Ͱ͖Δؾ΋
    ҰԠύϑΥʔϚϯεʹ༏ΕΔ
    ͸ͣ
    ˞ಈը͸Ͳ͔͜ʹΞοϓ͠·͢

    View full-size slide

  20. MJCCQGSTͱ͸
    wF#1'ͷػೳΛ΋͏গ͠ΞΫηε͠΍͘͢ϥοϓͨ͠ϥΠϒϥϦͱ͠
    ͯMJCCQGͱ͍͏΋ͷ͕͋Δɻ
    w#1'ϓϩάϥϜͷϩʔυ
    w#1'.BQ΍QFSGCV⒎FSͳͲσʔλͷ΍ΓऔΓͷΠϯλϑΣʔε
    wMJCCQG͸$ϥΠϒϥϦͳͷͰɺ3VTU͔Β࢖͍΍ͨ͘͢͠΋ͷ͕
    MJCCQGSTɻ

    View full-size slide

  21. جຊతͳ࢖͍ํ
    wCQGϓϩάϥϜΛॻ͘
    w cargo libbpf buildͰίϯύΠϧͰ͖Δ
    w cargo libbpf genͰ3VTUͷTLFMΛੜ੒
    w͋ͱ͸ͦͷTLFMΛ࢖ͬͯ3VTUϓϩάϥϜΛॻ͘
    wMJCCQGSTͷػೳʹՃ͑ͯɺTUSVDUPQU DUSMD BOZIPX ͦͷଞී௨ͷ
    3VTUͷίϚϯυϥΠϯπʔϧͱͯ͠ϓϩάϥϜΛॻ͚͹0,

    View full-size slide

  22. NBJOͷྫ
    ϏϧμʔΛܦ༝ͯ͠
    TLFMΛ։͘
    ύϥϝʔλΛೖΕͯ
    TLFMΛϩʔυɺΞλον
    F#1'ͷΠϯλϑΣʔε͔Β
    σʔλΛऔಘɺදࣔ

    View full-size slide

  23. σʔλදࣔͷྫʢQFSGCV⒎FSʣ
    wσʔλදࣔ༻ͷίʔϧόοΫؔ਺Λఆٛɻσʔλࣗମ͸&[u8]Ͱ
    ΍ͬͯ͘ΔͷͰɺQMBJODSBUFͰม׵ͷඞཁ͕͋Δɻ
    SFQS D
    ͳߏ଄ମʹ1MBJOΛ࣮૷

    View full-size slide

  24. σʔλදࣔͷྫʢ#1'.BQʣ
    w#1'.BQͱ͍͏σʔλूੵମΛܦ༝ͯ͠ɺΧʔωϧͰूΊͨσʔλ
    ʹϢʔβϥϯυ͔ΒΞΫηεɺදࣔͰ͖Δɻ
    ߏ଄ମఆٛΛ$3VTUͰ߹ΘͤΔ LFZWBMVFͱ΋ʹ&[u8]ͳͷͰɺ͜͜Ͱ΋ม׵

    View full-size slide

  25. Ϣʔβϥϯυ͔Β#1'.BQΛߋ৽
    wඵຖʹΧ΢ϯλϦηοτ͢ΔΑ͏ͳϓϩάϥϜΛॻ͚Δɻ
    ॻ͚Δ͚ͲɺMap::update()͕LFZWBMVFͱ΋ʹ&[u8]Λཁٻ͢Δͷ
    Ͱɺ͜͜΋ม׵͔͠΋VOTBGF
    plain::as_bytes()ͰͰ͖Δ͚Ͳ

    View full-size slide

  26. <3VTU௿ϨΠϠϝϦοτ>

    View full-size slide

  27. θϩίετந৅Խͷڧ྽͞
    w؆ܿʹએݴతʹॻ͍ͯ΋ɺεϐʔυͷ͜ͱΛؾʹ͠ͳ͍͍ͯ͘ɺͱ͍
    ͏ͷ͸͍͍ମݧɻ
    wΤϥʔΛResultͰѻ͑Δ͚ͩͰ΋ੜ࢈ੑΊͬͪΌߴ͍
    w΋ͪΖΜϝϞϦ΋ʢجຊతʹ͸ʣ҆શ
    wίϚϯυϥΠϯπʔϧͰ͸TUSVDUPQU͕͍͍
    wͱʹ͔͘TUSVDUPQU͕͍͍ʢճ໨ʣ

    View full-size slide

  28. ๛෋ͳDSBUFΛ࢖͑Δ
    wίΞͰͳ͍෦෼ͷ࠶ൃ໌͸ਏ͍ͷͰɺ͍͍ײ͡ͷ࣮૷ʹ৐͔ͬΓ͍ͨ
    wΦϓγϣϯύʔαɺ''*ɺՄࢹԽ
    wUFYUQMPUT͍͍
    wDBSHP͕ͦ΋ͦ΋
    ͍͍ɺXZDBUT

    View full-size slide

  29. ''*ʹڧ͍
    wࠓճͷMJCCQGͷΑ͏ʹɺ$ݴޠϕʔεʢ͔͠΋-JOVYΧʔωϧͱҰॹʹ
    ϝϯς͞Ε͍ͯΔʣͷͰ͔͍ϥΠϒϥϦΛૉ௚ʹ࢖͑Δͷ͸͍͍
    w3VTU$Ͱߏ଄ମɺϙΠϯλΛ΍ΓͱΓ͠΍͍͢ͷ͸ॿ͔Δ
    w࣌ʹ͸VOTBGFʹͳΔ͚Ͳ

    View full-size slide

  30. ࠔͬͯΔ͜ͱͱ͔ࠓޙ

    View full-size slide

  31. ࠓޙͷ՝୊
    wͱ͸͍͑VOTBGF͕ଟ͍ؾ͕͢ΔɺͳΜͱ͔ͳΒͳ͍͔ͳ
    w1MBJODSBUFʹཔ͍ͬͯΔͱ͜ΖΛ΋ͬͱ͏·͘Ӆ͢ͱ͍͍ͷ͔΋
    wͱ͍͏͔ɺMJCCQG@ST͕&[u8]Λཁٻ͗͢͠
    w''*पΓͷػೳ͸·ͩௐ΂࢝Ίͨ͹͔ΓͰɺ$ݴޠΈ͍ͨͳॻ͖ํʹ
    ͳͬͯ͠·͏ɻཁݚڀɻ

    View full-size slide

  32. ͦͷଞ΍Γ͍ͨ͜ͱ
    wπʔϧͱͯ͠΋͏গ͠খճΓΛྑ͍ͨ͘͠ɻ
    wΦϓγϣϯ૿΍ͨ͠Γɺ΋ͬͱ৭ʑͳϝτϦοΫΛऔΓ͍ͨ
    wӳޠͱ͔ͰهࣄΛॻ͍ͨํ͕ड͚ͦ͏ɻॻ͘ɻ
    w͋ͱ͸ɺࣗ෼Ͱ͋·Γ࢖͑ͯͳ͍ͷͰ࢖ͬͯΈΔɻ

    View full-size slide