pyspaに来ています

Linuxでは一つのネットワークインターフェイスに複数のIPアドレスを紐付ける方法として、

  • エイリアスを使う方法 (バージョン2.0からサポート)
  • ip(8)コマンドのaddr addサブコマンドを使う方法 (バージョン2.2からサポート)

があるのに、残念なことに前者しか今日まで知らなかったのでそれを書く。

この違いは、

  • ioctl(2)によるインターフェイスの操作
  • netlink(7)によるインターフェイスの操作

というところにも表れているので、それぞれの方法でeth0のIPアドレスを取得する方法を調べてみた。

ioctlを使う方法:

非常に単純。この方法だと紐づいているIPアドレスを1つしか取得できないことがわかる。

netlinkソケットを使う方法:

なんでこんな複雑なんだろうって思う。ドキュメントがないので結局カーネルのソース読んだし。

途中で調べた事も書いておく。

Linuxにおけるネットワークデバイスとアドレスの表現

ネットワークデバイスはnet_device構造体(linux/netdevice.h) で表現されている。

struct net_device {
    char			name[IFNAMSIZ];
	struct hlist_node	name_hlist;
	char 			*ifalias;
	unsigned long		mem_end;	
	unsigned long		mem_start;	
	unsigned long		base_addr;	
	unsigned int		irq;		
	unsigned long		state;
	struct list_head	dev_list;
	struct list_head	napi_list;
	struct list_head	unreg_list;
	netdev_features_t	features;
	netdev_features_t	hw_features;
	netdev_features_t	wanted_features;
	netdev_features_t	vlan_features;

    ...

	struct in_device __rcu	*ip_ptr;	
	struct inet6_dev __rcu	*ip6_ptr;

    ...
};

IPv4インターフェイスの情報はip_ptrが参照するin_device構造体(linux/inetdevice.h)に、IPv6インターフェイスの情報はip6_ptrが参照するinet6_dev構造体(include/net/if_inet6.h)に定義されている。

struct in_device:

struct in_device {
    struct net_device	*dev;
	atomic_t		refcnt;
	int			dead;
	struct in_ifaddr	*ifa_list;	/* IP ifaddr chain		*/
	struct ip_mc_list __rcu	*mc_list;	/* IP multicast filter chain    */
	int			mc_count;	/* Number of installed mcasts	*/
	spinlock_t		mc_tomb_lock;
	struct ip_mc_list	*mc_tomb;
	unsigned long		mr_v1_seen;
	unsigned long		mr_v2_seen;
	unsigned long		mr_maxdelay;
	unsigned char		mr_qrv;
	unsigned char		mr_gq_running;
	unsigned char		mr_ifc_count;
	struct timer_list	mr_gq_timer;	/* general query timer */
	struct timer_list	mr_ifc_timer;	/* interface change timer */

	struct neigh_parms	*arp_parms;
	struct ipv4_devconf	cnf;
	struct rcu_head		rcu_head;
};

struct inet6_dev:

struct inet6_dev {
    struct net_device	*dev;

	struct list_head	addr_list;

	struct ifmcaddr6	*mc_list;
	struct ifmcaddr6	*mc_tomb;
	spinlock_t		mc_lock;
	unsigned char		mc_qrv;
	unsigned char		mc_gq_running;
	unsigned char		mc_ifc_count;
	unsigned long		mc_v1_seen;
	unsigned long		mc_maxdelay;
	struct timer_list	mc_gq_timer;	/* general query timer */
	struct timer_list	mc_ifc_timer;	/* interface change timer */

	struct ifacaddr6	*ac_list;
	rwlock_t		lock;
	atomic_t		refcnt;
	__u32			if_flags;
	int			dead;

    ...

	struct neigh_parms	*nd_parms;
	struct inet6_dev	*next;
	struct ipv6_devconf	cnf;
	struct ipv6_devstat	stats;
	unsigned long		tstamp; /* ipv6InterfaceTable update timestamp */
	struct rcu_head		rcu;
};

in_device構造体に紐づいているIPアドレスのリストはifa_listで、inet6_dev構造体に紐づいているIPアドレスのリストはaddr_listで表現されている。

IPv4IPv6で全然構造が違うのが面白い。

エイリアスを使った場合も、そうでない場合もどちらもそれぞれのIPアドレスがリストのエントリとして格納される。

グリー株式会社を退職していました

グリー株式会社を退職していました。2006年12月29日が最終出社日でした。

最終日は例年を上回る寒さでしたが、六本木は晴れ、抜けるような空の青さが印象的でした。有給を消化するはずが、結局最後まで吉田くんが手がけていたお正月特集の仕掛けに時間を費やしていたので、本当にここで出社しなくなるのだろうか、と自問するほどでした。

在職期間は契約社員時代も含め1年半とちょっと短かったのですが、その中でもたくさんのことを経験することができました。サイトの初リニューアル作業に始まり、DBの最適化、出たばかりのSennaを使った全文検索システムや画像サーバクラスタの構築など、責任のある仕事を丸ごと任せてもらえたのは大きかったです。一緒にやっているメンバーには、自分のささいなミスでたくさん迷惑をかけました。ごめんなさい。

一方で、何か大きな成果を残すことができたかというと、謙遜ではなく、あまり胸を張れるところではないなあというところが若干の心残りではあります。

一緒にやってきたチームのメンバーのみんなとの出会いはもちろん、CTO の藤本さんと一緒に仕事できたのは一生の思い出になっただろうと思います。特に、休日出勤中にうっかり本番データをTRUNCATEしたときに、プライベートで忙しいにも関わらず出社してフォローをしてくださるなど面倒見の良さには感銘を受けましたし、何より自分自身、困ったときに何度もサポートしていただき心強かった。後に辞めていったメンバーがみんな藤本さんの名前を口にしながら退職の挨拶をしていると聞いて、今もなお畏敬の念を拭う事ができません。

退職理由は語ran新しいことを始めてみたいというのが理由でした。結果として、Webサービスをそれまでと同じように作っていたらできなかったであろう得難い経験をする事ができたので、今でも退職したことは後悔していません。ただ、もし今も在職していたら、それはそれで別の得難い経験ができていたのではないかと思わせる、そんな組織だったと思います。なお、今はどんな形でも会社に関わっていません。

みなさん、本当にありがとうございました。
なおやさん、テンプレをありがとうございました。

InnokoDB storage engine plugin

InnoDB Deep Talk #1なのにうっかり casual な話をしてしまったという話です。

InnokoDB is a MySQL storage engine plugin that enables one to fetch the 猪子san's recent tweets in a SQL manner.

https://launchpad.net/innokodb

会場で公開デバッグに付き合ってくださった斯波さん初め皆様ありがとうございました。

MySQLがらみの他の作品:
ストアドを使って、Webスクレイピングをしよう!

やったーmod_vimできたよー

Vim scriptはウェブアプリケーション記述言語やったんやー

Plack とかよくわかんないのでさくっと Apache で使えるようにしてみました。

mod_vim

httpd.conf はこんな感じ

LoadModule vim_module mod_vim.so
DocumentRoot .

Listen 8080

PidFile /tmp/pid
LockFile /tmp/lock
ErrorLog /tmp/error_log

VimDisplay :0
VimVersion 7.2
VimEncoding UTF-8

<Location />
    SetHandler vim
    VimExpr vimplack#handle(@@)
</Location>

ビルド方法は、まあ README に書いておけよって感じですが

make top_builddir=/usr/share/apache2/build

みたいにしてください、apr-json が別途必要です。

mattnさんのサンプルはこんな感じに動いています。

f:id:moriyoshi:20120209193608p:image

% ab -n 500 -c 20 http://localhost:8080/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


Server Software:        Apache/2.2.20
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        348 bytes

Concurrency Level:      20
Time taken for tests:   0.587 seconds
Complete requests:      500
Failed requests:        0
Write errors:           0
Total transferred:      246500 bytes
HTML transferred:       174000 bytes
Requests per second:    851.90 [#/sec] (mean)
Time per request:       23.477 [ms] (mean)
Time per request:       1.174 [ms] (mean, across all concurrent requests)
Transfer rate:          410.14 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   22  14.0     20     229
Processing:     0    1   3.4      0      31
Waiting:        0    1   3.4      0      31
Total:          4   23  14.1     21     248

Percentage of the requests served within a certain time (ms)
  50%     21
  66%     22
  75%     23
  80%     25
  90%     28
  95%     32
  98%     52
  99%     98
 100%    248 (longest request)

Enjoy!

JavaScriptで任意のテキストをバイト列からラスタ画像化する、またはその逆

id:monjudohid:nishiohirokazu と話してて作ったんだけどとりあえず使い道なかったので共有します。

テキストをレンダリングして画像にするという話ではなく、

  1. テキストを UTF-8 に変換する
  2. UTF-8 化されたバイト列であるテキストをラスタ画像 (ImageData) にする
  3. 画像を PNG 形式にする

ということをする話です。

f:id:moriyoshi:20120107194512p:image

デモサイト:
http://moriyoshi.github.com/textimgenc/

ソースはこちら:
http://github.com/moriyoshi/textimgenc/