##socket type
socket is a widely used in kernel/net. There are many kind of sockets with different ‘family/proto’. They can be divided by family, and under each family, there are many kinds of proto. such as inet(ipv4)/stream, netlink/(xfrm, rt).
##Data struct && method
###net_proto_family
The net_proto_family is used to register a socket family.
1 | 216 struct net_proto_family { |
such as inet(ipv4), netlink, unix_socket, pf_packet(tcpdump socket) …
EX: netlink family
1 | 2082 static const struct net_proto_family netlink_family_ops = { |
All the socket families are registered to net_families by int sock_register(const struct net_proto_family *ops)
1 | 158 static DEFINE_SPINLOCK(net_family_lock); |
See later section for detail.
###struct proto_ops
proto_ops is used to define the detail operation, such as bind/connect/accept and so on. every socket has a specificed proto_ops.
1 | 162 struct proto_ops { |
###struct proto
1 | 800 struct proto { |
Currently, I only found the obj_size/slab is useful.
1 | 876 struct kmem_cache *slab; |
When create a socket in socket_create, we alloc a struct sock, in fact, we alloc a memory whose size is obj_size from the slab.
ex:netlink_sock, its first element is struct sock sk;, thus we can use nlk_sk to get a struct netlink_sock from a struct sock.
1 | 67 struct netlink_sock { |
1 | 96 static inline struct netlink_sock *nlk_sk(struct sock *sk) |
sometimes struct proto_ops will call the struct proto
ex: .getsockopt (todo)
###socket vs sock
What is sock used for? sock is the internel representation “network layer representation of sockets” Which is used in kernel internel.
What is socket used for? “struct socket – general BSD socket” It is a very high layer abtractation, has very little and common information, such as: state, file(socket is a file for user space), flag, ops(proto ops). each of them has a pointer in its struct which points to peer.
PIC for sock and socket
###socket->sk
struct socket - general BSD socket
1 | 139 struct socket { |
void sock_init_data(struct socket *sock, struct sock *sk)
1 | 2077 void sock_init_data(struct socket *sock, struct sock *sk) |
##Socket Related Initialization
###net_families
and socket_register
each of the socket family initialization, sock_register is called to register a handler.
1 | [junwei@junwei]$ grep sock_register net/ -Rwn |
1 | 2466 /** |
##socket related system call
###syscall
In kennel(ver 3.4), the syscall are defined in net/socket.c
1 | [junwei@junwei linux-3.4]$ grep SYSCALL_DEFINE net/socket.c -n |
ex: netlink socket register.
1 | 2129 static int __init netlink_proto_init(void) |
###socket system call socket
sock_create
is a very basic and important method.
When create a socket, we dedicate the its family/proto, The create method of the corresponding net_proto_family is called, and set the socket->ops with corresponding struct proto_ops.
1 | 139 struct socket |
See detail:
SYSCALL_DEFINE3(socket
1 | 1325 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) |
int sock_create(...)
1 | 1313 int sock_create(int family, int type, int protocol, struct socket **res) |
1 | 1199 int __sock_create(struct net *net, int family, int type, int protocol, |
NOTE:
1 | 1260 pf = rcu_dereference(net_families[family]); |
What is pf->create(…)?
For netlink socket, pf->create is netlink_create.
socket system call listen
Most of them are simple, and just call respond function by ops->xxx.
ex: listen
1 | 1476 SYSCALL_DEFINE2(listen, int, fd, int, backlog) |
The import ops is inited in function sock_create
ex: the ops in netlink socket.
1 | 430 static int netlink_create(struct net *net, struct socket *sock, int protocol, |
1 | 402 static int __netlink_create(struct net *net, struct socket *sock, |
other socket system call
the socket related sytem call has similar source, just like sock->ops->listen(…)