1. 程式人生 > >網絡協議源碼分析

網絡協議源碼分析

packet 端口 數據結構 就是 slots int ask tab ash

從UDP開始看吧,udp_rcv時,數據包已經經過了驅動,網絡層的層層過濾來到了傳輸層,在這裏還是要經過層層的考驗才會進入到最終socket

重要數據結構:udp_table:

67 /**
68 * struct udp_table - UDP table
69 * 套接字都是本地創建的
70 * @hash: hash table, sockets are hashed on (local port) 本地接收的
71 * @hash2: hash table, sockets are hashed on (local port, local address) 端口號和IP地址作為鍵值的索引
72 * @mask: number of slots in hash tables, minus 1
73 * @log: log2(number of slots in hash table)
74 */
75 struct udp_table {
76 struct udp_hslot *hash;
77 struct udp_hslot *hash2;
78 unsigned int mask;
79 unsigned int log;
80 };

54 /**
55 * struct udp_hslot - UDP hash slot
56 *
57 * @head: head of list of sockets
58 * @count: number of sockets in ‘head‘ list
59 * @lock: spinlock protecting changes to head/count
60 */
61 struct udp_hslot {
62 struct hlist_head head;
63 int count;
64 spinlock_t lock;
65 } __attribute__((aligned(2 * sizeof(long))));
66
67 /**
68 * struct udp_table - UDP table

inet_iif

__udp_lib_lookup

怎麽樣判斷一個數據包在udp_table->hash中的哪個位置?根據接受到的數據包的目的端口地址,也就是說目的端口地址非常之重要;根據目的端口找到slot,然後遍歷slot上所有的套接字 

socket->sk_receive_queue 

the to call receive_from to get packet from the kernel

網絡協議源碼分析