Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RubyでXDPプログラミング
Search
Yukimitsu Izawa
January 18, 2025
0
3
RubyでXDPプログラミング
北陸三県.rb Lightning Talks in Kanazawa (2025/01/18) での発表資料。
RubyでBCC経由でXDPプログラミングをするまでの紹介。
Yukimitsu Izawa
January 18, 2025
Tweet
Share
More Decks by Yukimitsu Izawa
See All by Yukimitsu Izawa
軽くて強い素材を求めて(DCFのススメ)
izawa
1
90
指紋認証でsshログイン
izawa
0
290
展示会に出展してみた
izawa
0
160
エアコンの操作についての話
izawa
0
660
SlackにZoomの使用状況を表示したい
izawa
0
190
Kindleページめくり君 Ver.2
izawa
0
210
災害グッズとして見たときのキャンプ用品
izawa
0
280
アレクサがカメラ女子になった日
izawa
0
290
2019年上半期のキャンプお買い物ベスト8
izawa
0
310
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
Designing Experiences People Love
moore
139
23k
Typedesign – Prime Four
hannesfritz
40
2.5k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
570
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
19
2.3k
How GitHub (no longer) Works
holman
312
140k
How STYLIGHT went responsive
nonsquared
96
5.3k
Into the Great Unknown - MozCon
thekraken
34
1.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Building an army of robots
kneath
302
45k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Transcript
ҪᖒΏ͖Έ݄ͭࡾݝSC-JHIUOJOH5BMLTJO,BOB[BXB 3VCZͰ9%1ϓϩάϥϛϯά
ࣗݾհ w :PV5VCFS ‣ .Z0VUEPPS-JGF IUUQTLBOB[BXBDBNQ w ത࢜ ใՊֶ
‣ ΠϯλʔωοτΛͬͨૄ݁߹ࢄγεςϜͷݚڀΛ͍ͯ͠·͢ w גࣜձࣾΫϧΟοτऔక$00݉ࢧࣾ w $PEFGPS,BOB[BXBཧࣄ w ిࢠ࡞φΠϑ੍࡞ϨβʔΫϥϑτͳͲɺͷΛ࡞Δͷ͕झຯͰ͢
F#1'͍ͬͯͬͯ·͔͢ʁ w FYUFOEFE#FSLFMFZ1BDLFU'JMUFSͷུ w LFSOFM෦ͰϢʔβۭؒͷϓϩάϥϜΛಈ͔͢Έ w ઐ༻ͷ໋ྩηοτͰۦಈ͢Δ7.্ͰΒͤΔ
None
9%1͍ͬͯͬͯ·͔͢ʁ w F9QSFTT%BUB1BUIͷུ w F#1'ϕʔεͷߴύέοτॲཧٕज़ w JOHSFTTύέοτͷॲཧ༻్ w LFSOFMͷSFDPNQJMFແ͠ͰΧʔωϧ෦ͷॲཧΛॊೈʹมߋͰ͖Δ w
5$1*1ελοΫΑΓલஈͰ࣮ߦ͞ΕΔ TL@CV ff ΑΓલ ˡϙΠϯτ
9%1ͷΈ /*$ 5$1*14UBDL 9%1FOWJSPONFOU 9%1ॲཧ 9%1@%301 9%1@1"44 9%1@59 /*$%SJWFS w
/*$ͷυϥΠόͰ࣮ߦ w ύέοτ౸ணຖʹ)PPL͞ΕΔ w ύέοτͷॲ۰ΛܾΊΔ ‣ 9%1@1"44*14UBDLʹ্͛Δ ‣ 9%1@%301ࣺͯΔ ‣ 9%1@59ड৴ͨ͠/*$͔ΒૹΔ ‣ 9%1@3&%*3&$5ผͷ/*$͔ΒૹΔ
։ൃͷखॱ 9%1͕༗ޮͳΧʔωϧͱ։ൃڥΛ४උ͢Δ $ݴޠͰ9%1ϓϩάϥϜΛهड़͢Δ DMBOHͰ#1'όΠτίʔυʹίϯύΠϧ͢Δ Χʔωϧʹϩʔυ͢Δ
9%1͕༗ޮͳΧʔωϧͱ։ൃڥΛ४උ͢Δ w ৽͍͠LFSOFMͷ-JOVYڥΛ࡞Δͷ͕Ұ൪ૣ͍ w 6CVOUV-544FSWFSσϑΥϧτͰF#1'͕FOBCMFʹͳ͍ͬͯΔͷ ͰɺͱΓ͋͑ͣ͜ΕͰ͍͍Μ͡Όͳ͍ʁ w ৄ͓͘͠άάΓ͍ͩ͘͞
$ݴޠͰ9%1ϓϩάϥϜΛهड़͢Δ w ҎԼͷ੍ͷԼͰهड़͢Δඞཁ͕͋Δ ‣ ໋ྩʹ্ݶ . ‣ ແݶϧʔϓېࢭ ‣
౸ୡෆՄೳͳهड़ېࢭ ‣ ϝϞϦνΣοΫͨ͠ϝϞϦͷΈΞΫηεՄೳ
DMBOHͰ#1'όΠτίʔυʹίϯύΠϧ͢Δ w UBSHFUΛCQGʹͯ͠DMBOHͰίϯύΠϧ DMBOH0UBSHFUCQGDTBNQMFDPTBNQMFP
Χʔωϧʹϩʔυ͢Δ w JQSPVUFͰ/*$ͷυϥΠόʹϩʔυ JQMJOLTFUEFWFUIYEQPCKTBNQMFP
#$$Λ͏ͱΑΓ؆୯ʹ w #1'$PNQJMFS$PMMFDUJPOͷུ w F#1'ͷϓϩάϥϜΛΑΓ؆қʹهड़͢ΔͨΊͷϑϨʔϜϫʔΫϥΠϒϥϦ w ཪͰDMBOH--7.ΛݺΜͰ͍Δ w 1ZUIPOͱ͔-VBͳͲͷ4DSJQU͔Βར༻Ͱ͖Δ
3C#$$ IUUQTHJUIVCDPNVE[VSBSCCDD w 3VCZͰ#$$͢ΔͨΊͷHFN VE[VSBࢯ࡞ w 3VCZΞιγΤʔγϣϯͷ։ൃॿͰ࡞ΒΕͨͦ͏
൵͍͠ݱ࣮
൵͠ΈΛΓӽ͑ͯ w 3C#$$HFNΛ దʹ 9%1ʹରԠͤͯ͞ΈͨΑ🎊
ૣɺαϯϓϧϓϩάϥϜ w 9%1ʹରԠͤͨ͞3C#$$HFNΛͬͯɺ w ʮҎ্Ͱͷഒͷγʔέϯε൪߸ͷ͍ͭͨQJOHʹͷΈԠ͢Δ-JOVY ,FSOFMʯΛ࡞ͬͯΈΑ͏ ‣ JQUBCMFTͰهड़Ͱ͖ͳ͍Α͏ͳෳࡶͳϧʔϧ ‣ ୯७ϚονͰͳ͘ܭࢉ݁ՌʹΑͬͯڍಈΛม͑Δ
1 require 'rbbcc' 2 include RbBCC 3 4 5 print
"loading..." 6 STDOUT.flush 7 8 b = BCC.new(text: <<BPF) 9 10 #include <uapi/linux/bpf.h> 11 #include <linux/ip.h> 12 #include <linux/icmp.h> 13 14 15 int xdp_drop_icmp(struct xdp_md *ctx) { 16 void* data_end = (void*)(long)ctx->data_end; 17 void* data = (void*)(long)ctx->data; 18 struct ethhdr *eth = data; 19 u32 protocol; 20 u16 sequence; 21 u64 nh_off = sizeof(*eth); 22 23 // for validator 24 if (data + nh_off > data_end) 25 return XDP_PASS; 26 27 if (eth->h_proto == htons(ETH_P_IP)) { 28 struct iphdr *iph = data + nh_off; 29 29 30 // for validator 31 if ((void*)&iph[1] > data_end) 32 return XDP_PASS; 33 34 protocol = iph->protocol; 35 if (protocol == 1) { /* ICMP */ 36 struct icmphdr *icmph = data + nh_off + iph->ihl * 4; 37 38 // for validator 39 if ((void*)&icmph[1] > data_end) 40 return XDP_PASS; 41 42 if (icmph->type == 8) { /* ECHO REQUEST */ 43 if (icmph->un.echo.sequence > 0 && 44 icmph->un.echo.sequence % 3 == 0) { 45 return XDP_PASS; 46 } 47 else return XDP_DROP; 48 } 49 } 50 } 51 return XDP_PASS; 52 } 53 BPF 54 55 fn = b.load_func("xdp_drop_icmp", BPF::XDP) 56 puts "done." 57 58 b.attach_xdp("eth0", "xdp_drop_icmp") 59 sleep(20) 60 b.remove_xdp("eth0") αϯϓϧϓϩάϥϜYEQ@JDNQSC
σϞ
Ҏ্ɺ͋Γ͕ͱ͏͍͟͝·ͨ͠