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/

2cf373725ded741824c50fd571eda6e1?s=128

KONDO Uchio

April 27, 2021
Tweet

Transcript

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

  2. γχΞɾϓϦϯγύϧΤϯδχΞ ۙ౻ Ӊஐ࿕ / @udzura https://blog.udzura.jp/ Uchio Kondo ٕज़෦ ٕज़ج൫νʔϜ

    @ GMOϖύϘ / ෱Ԭ ΤϯδχΞΧϑΣʢ෱Ԭࢢ੺ẂנจԽձؗʣ αϙʔλʔ Duolingo Diamond Leaguer 💎 #Rust #Ruby #mruby #Containers #eBPF #CRIU #Seccomp #RubyKaigi #CloudNativeDays #Zumba #γϨϯ #FitBoxing2
  3. ࠓճ࿩͢΋ͷҎ֎ʹ࡞ͬͨ΋ͷ •octx wίϚϯυҰൃͰࡶʹ(JU)VCͷBDUJWJUZΛ$47ʹͯ͘͠ΕΔ΍ͭ ͦͷ··#JH2VFSZͰϩʔυՄೳɻ wPDUPDSBC SFRXFTU UPLJP

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

  5. F#1'

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

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

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

    Χʔωϧͷઃఆ͍͔ͭ͘ ΛΠ ϯετʔϧ͢Ε͹τϨʔε͕Ͱ͖ΔΑ͏ʹɻ
  9. ࣮૷ͷྫ w#1'ͷϓϩάϥϜΛϏϧυ͢Δ $Ͱॻ͘ඞཁ͕͋ΔɻϓϩάϥϜͷҰ෦ ίϯύΠϧ࣌ͷΦϓγϣϯ

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

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

  12. ग़དྷ্͕ͬͨόΠφϦͷΠϝʔδ Ϣʔβϥϯυ Χʔωϧ಺෦ ϓϩάϥϜʢϫϯόΠφϦʣ #1'ϓϩάϥϜ ϩʔυ σʔλ΍ΓऔΓ ϓϩάϥϜ ࣮ߦɾूܭ Φϓγϣϯͷղੳ

    ग़ྗ Ճ޻ #1' ࣮ߦ࣌ݕࠪ #1'.BQ 1FSGCV⒎FS ར༻ऀ
  13. ͜͜·Ͱͷͬ͘͟Γͨ͠·ͱΊ wF#1'͍ͬͯ͏-JOVYͷτϨʔαͳͲΛ࡞Δศརٕज़͕͋Δ wF#1'πʔϧ͸ɺҎલ͸DMBOHͰͦͷ৔ͰίϯύΠϧ͍͕ͯͨ͠ɺ ࠷ۙϫϯόΠφϦͰ഑ΕΔΑ͏ʹͳͬͨ ཁ৽͍͠Χʔωϧ  wͦ͏͢ΔͱίϚϯυϥΠϯπʔϧ෦෼΋ؚΊͯϫϯόΠφϦʹ͍ͨ͠ wͦͷSFGFSFODF࣮૷͸$ݴޠɻ$ݴޠɻ ʮ1SPEVDUJPOͰ͸#$$ΑΓMJCCQG #1'$03&Λ࢖͏ํ͕ྑ͍ʯ

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

  15. ͦ͜Ͱ3VTU

  16. ͭͬͨ͘΋ͷ

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

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

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

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

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

  22. MJCCQGST

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

  24. جຊతͳ࢖͍ํ wCQGϓϩάϥϜΛॻ͘ w cargo libbpf buildͰίϯύΠϧͰ͖Δ w cargo libbpf genͰ3VTUͷTLFMΛੜ੒

    w͋ͱ͸ͦͷTLFMΛ࢖ͬͯ3VTUϓϩάϥϜΛॻ͘ wMJCCQGSTͷػೳʹՃ͑ͯɺTUSVDUPQU DUSMD BOZIPX ͦͷଞී௨ͷ 3VTUͷίϚϯυϥΠϯπʔϧͱͯ͠ϓϩάϥϜΛॻ͚͹0,
  25. NBJOͷྫ ϏϧμʔΛܦ༝ͯ͠ TLFMΛ։͘ ύϥϝʔλΛೖΕͯ TLFMΛϩʔυɺΞλον F#1'ͷΠϯλϑΣʔε͔Β σʔλΛऔಘɺදࣔ

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

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

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

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

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

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

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

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

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

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