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
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Music & Morning Musume
bryan
46
6.3k
Producing Creativity
orderedlist
PRO
343
39k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Facilitating Awesome Meetings
lara
51
6.2k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
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
σϞ
Ҏ্ɺ͋Γ͕ͱ͏͍͟͝·ͨ͠