最后一个属性就指向了上面所述的操作集。若使用TCP协议,ops就是inet_stream_ops,若是UDP协议,ops就是inet_dgram_ops。
short type属性的取值可以是如下值:
- enum sock_type {
- SOCK_DGRAM = 1,
- SOCK_STREAM = 2,
- SOCK_RAW = 3,
- SOCK_RDM = 4,
- SOCK_SEQPACKET = 5,
- SOCK_DCCP = 6,
- SOCK_PACKET = 10,
- };
传输层的协议操作集结构体定义:
- struct proto {
- void (*close)(struct sock *sk,
- long timeout);
- int (*connect)(struct sock *sk,
- struct sockaddr *uaddr,
- int addr_len);
- int (*disconnect)(struct sock *sk, int flags);
- struct sock * (*accept) (struct sock *sk, int flags, int *err);
- int (*ioctl)(struct sock *sk, int cmd,
- unsigned long arg);
- int (*init)(struct sock *sk);
- void (*destroy)(struct sock *sk);
- void (*shutdown)(struct sock *sk, int how);
- int (*setsockopt)(struct sock *sk, int level,
- int optname, char __user *optval,
- unsigned int optlen);
- int (*getsockopt)(struct sock *sk, int level,
- int optname, char __user *optval,
- int __user *option);
- #ifdef CONFIG_COMPAT
- int (*compat_setsockopt)(struct sock *sk,
- int level,
- int optname, char __user *optval,
- unsigned int optlen);
- int (*compat_getsockopt)(struct sock *sk,
- int level,
- int optname, char __user *optval,
- int __user *option);
- int (*compat_ioctl)(struct sock *sk,
- unsigned int cmd, unsigned long arg);
- #endif
- int (*sendmsg)(struct kiocb *iocb, struct sock *sk,
- struct msghdr *msg, size_t len);
- int (*recvmsg)(struct kiocb *iocb, struct sock *sk,
- struct msghdr *msg,
- size_t len, int noblock, int flags,
- int *addr_len);
- int (*sendpage)(struct sock *sk, struct page *page,
- int offset, size_t size, int flags);
- int (*bind)(struct sock *sk,
- struct sockaddr *uaddr, int addr_len);
- int (*backlog_rcv) (struct sock *sk,
- struct sk_buff *skb);
- /* Keeping track of sk's, looking them up, and port selection methods. */
- void (*hash)(struct sock *sk);
- void (*unhash)(struct sock *sk);
- void (*rehash)(struct sock *sk);
- int (*get_port)(struct sock *sk, unsigned short snum);
- void (*clear_sk)(struct sock *sk, int size);
- /* Keeping track of sockets in use */
- #ifdef CONFIG_PROC_FS
- unsigned int inuse_idx;
- #endif
- /* Memory pressure */
- void (*enter_memory_pressure)(struct sock *sk);
- atomic_long_t *memory_allocated; /* Current allocated memory. */
- struct percpu_counter *sockets_allocated; /* Current number of sockets. */
- /*
- * Pressure flag: try to collapse.
- * Technical note: it is used by multiple contexts non atomically.
- * All the __sk_mem_schedule() is of this nature: accounting
- * is strict, actions are advisory and have some latency.
- */
- int *memory_pressure;
- long *sysctl_mem;
- int *sysctl_wmem;
- int *sysctl_rmem;
- int max_header;
- bool no_autobind;
- struct kmem_cache *slab;
- unsigned int obj_size;
- int slab_flags;
- struct percpu_counter *orphan_count;
- struct request_sock_ops *rsk_prot;
- struct timewait_sock_ops *twsk_prot;
- union {
- struct inet_hashinfo *hashinfo;
- struct udp_table *udp_table;
- struct raw_hashinfo *raw_hash;
- } h;
- struct module *owner;
- char name[32];
- struct list_head node;
- #ifdef SOCK_REFCNT_DEBUG
- atomic_t socks;
- #endif
- };
TCP协议的操作集定义如下:
- struct proto tcp_prot = {
- .name = "TCP",
- .owner = THIS_MODULE,
- .close = tcp_close,
- .connect = tcp_v4_connect,
- .disconnect = tcp_disconnect,
- .accept = inet_csk_accept,
- .ioctl = tcp_ioctl,
- .init = tcp_v4_init_sock,
- .destroy = tcp_v4_destroy_sock,
- .shutdown = tcp_shutdown,
- .setsockopt = tcp_setsockopt,
- .getsockopt = tcp_getsockopt,
- .recvmsg = tcp_recvmsg,
- .sendmsg = tcp_sendmsg,
- .sendpage = tcp_sendpage,
- .backlog_rcv = tcp_v4_do_rcv,
- .hash = inet_hash,
- .unhash = inet_unhash,
- .get_port = inet_csk_get_port,
- .enter_memory_pressure = tcp_enter_memory_pressure,
- .sockets_allocated = &tcp_sockets_allocated,
- .orphan_count = &tcp_orphan_count,
- .memory_allocated = &tcp_memory_allocated,
- .memory_pressure = &tcp_memory_pressure,
- .sysctl_mem = sysctl_tcp_mem,
- .sysctl_wmem = sysctl_tcp_wmem,
- .sysctl_rmem = sysctl_tcp_rmem,
- .max_header = MAX_TCP_HEADER,
- .obj_size = sizeof(struct tcp_sock),
- .slab_flags = SLAB_DESTROY_BY_RCU,
- .twsk_prot = &tcp_timewait_sock_ops,
- .rsk_prot = &tcp_request_sock_ops,
- .h.hashinfo = &tcp_hashinfo,
- .no_autobind = true,
- #ifdef CONFIG_COMPAT
- .compat_setsockopt = compat_tcp_setsockopt,
- .compat_getsockopt = compat_tcp_getsockopt,
- #endif
- };
- struct proto udp_prot = {
- .name = "UDP",
- .owner = THIS_MODULE,
- .close = udp_lib_close,
- .connect = ip4_datagram_connect,
- .disconnect = udp_disconnect,
- .ioctl = udp_ioctl,
- .destroy = udp_destroy_sock,
- .setsockopt = udp_setsockopt,
- .getsockopt = udp_getsockopt,
- .sendmsg = udp_sendmsg,
- .recvmsg = udp_recvmsg,
- .sendpage = udp_sendpage,
- .backlog_rcv = __udp_queue_rcv_skb,
- .hash = udp_lib_hash,
- .unhash = udp_lib_unhash,
- .rehash = udp_v4_rehash,
- .get_port = udp_v4_get_port,
- .memory_allocated = &udp_memory_allocated,
- .sysctl_mem = sysctl_udp_mem,
- .sysctl_wmem = &sysctl_udp_wmem_min,
- .sysctl_rmem = &sysctl_udp_rmem_min,
- .obj_size = sizeof(struct udp_sock),
- .slab_flags = SLAB_DESTROY_BY_RCU,
- .h.udp_table = &udp_table,
- #ifdef CONFIG_COMPAT
- .compat_setsockopt = compat_udp_setsockopt,
- .compat_getsockopt = compat_udp_getsockopt,
- #endif
- .clear_sk = sk_prot_clear_portaddr_nulls,
- };
现在介绍struct socket结构体中一个属性struct sock类型的结构体指针,这个结构体就是传输层的套接字,所有套接字通过该结构来使用网络协议的所有服务。定义如下:
- struct sock {
- /*
- * Now struct inet_timewait_sock also uses sock_common, so please just
- * don't add nothing before this first member (__sk_common) --acme
- */
- struct sock_common __sk_common;
- #define sk_node __sk_common.skc_node
- #define sk_nulls_node __sk_common.skc_nulls_node
- #define sk_refcnt __sk_common.skc_refcnt
- #define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping
- #define sk_dontcopy_begin __sk_common.skc_dontcopy_begin
- #define sk_dontcopy_end __sk_common.skc_dontcopy_end
- #define sk_hash __sk_common.skc_hash
- #define sk_family __sk_common.skc_family
- #define sk_state __sk_common.skc_state
- #define sk_reuse __sk_common.skc_reuse
- #define sk_bound_dev_if __sk_common.skc_bound_dev_if
- #define sk_bind_node __sk_common.skc_bind_node
- #define sk_prot __sk_common.skc_prot
- #define sk_net __sk_common.skc_net
- socket_lock_t sk_lock;
- struct sk_buff_head sk_receive_queue;
- /*
- * The backlog queue is special, it is always used with
- * the per-socket spinlock held and requires low latency
- * access. Therefore we special case it's implementation.
- * Note : rmem_alloc is in this structure to fill a hole
- * on 64bit arches, not because its logically part of
- * backlog.
- */
- struct {
- atomic_t rmem_alloc;
- int len;
- struct sk_buff *head;
- struct sk_buff *tail;
- } sk_backlog;
- #define sk_rmem_alloc sk_backlog.rmem_alloc
- int sk_forward_alloc;
- #ifdef CONFIG_RPS
- __u32 sk_rxhash;
- #endif
- atomic_t sk_drops;
- int sk_rcvbuf;
- struct sk_filter __rcu *sk_filter;
- struct socket_wq __rcu *sk_wq;
- #ifdef CONFIG_NET_DMA
- struct sk_buff_head sk_async_wait_queue;
- #endif
- #ifdef CONFIG_XFRM
- struct xfrm_policy *sk_policy[2];
- #endif
- unsigned long sk_flags;
- struct dst_entry *sk_dst_cache;
- spinlock_t sk_dst_lock;
- atomic_t sk_wmem_alloc;
- atomic_t sk_omem_alloc;
- int sk_sndbuf;
- struct sk_buff_head sk_write_queue;
- kmemcheck_bitfield_begin(flags);
- unsigned int sk_shutdown : 2,
- sk_no_check : 2,
- sk_userlocks : 4,
- sk_protocol : 8,
- sk_type : 16;
- kmemcheck_bitfield_end(flags);
- int sk_wmem_queued;
- gfp_t sk_allocation;
- int sk_route_caps;
- int sk_route_nocaps;
- int sk_gso_type;
- unsigned int sk_gso_max_size;
- int sk_rcvlowat;
- unsigned long sk_lingertime;
- struct sk_buff_head sk_error_queue;
- struct proto *sk_prot_creator;
- rwlock_t sk_callback_lock;
- int sk_err,
- sk_err_soft;
- unsigned short sk_ack_backlog;
- unsigned short sk_max_ack_backlog;
- __u32 sk_priority;
- struct pid *sk_peer_pid;
- const struct cred *sk_peer_cred;
- long sk_rcvtimeo;
- long sk_sndtimeo;
- void *sk_protinfo;
- struct timer_list sk_timer;
- ktime_t sk_stamp;
- struct socket *sk_socket;
- void *sk_user_data;
- struct page *sk_sndmsg_page;
- struct sk_buff *sk_send_head;
- __u32 sk_sndmsg_off;
- int sk_write_pending;
- #ifdef CONFIG_SECURITY
- void *sk_security;
- #endif
- __u32 sk_mark;
- u32 sk_classid;
- void (*sk_state_change)(struct sock *sk);
- void (*sk_data_ready)(struct sock *sk, int bytes);
- void (*sk_write_space)(struct sock *sk);
- void (*sk_error_report)(struct sock *sk);
- int (*sk_backlog_rcv)(struct sock *sk,
- struct sk_buff *skb);
- void (*sk_destruct)(struct sock *sk);
- };
若要将协议族操作集和具体协议操作集整合起来为IP协议提供接口,就需要下面的结构体定义:
- struct inet_protosw {
- struct list_head list;
- /* These two fields form the lookup key. */
- unsigned short type; /* This is the 2nd argument to socket(2). */
- unsigned short protocol; /* This is the L4 protocol number. */
- struct proto *prot;
- const struct proto_ops *ops;
- char no_check; /* checksum on rcv/xmit/none? */
- unsigned char flags; /* See INET_PROTOSW_* below. */
- };
- static struct inet_protosw inetsw_array[] =
- {
- {
- .type = SOCK_STREAM,
- .protocol = IPPROTO_TCP,
- .prot = &tcp_prot,
- .ops = &inet_stream_ops,
- .no_check = 0,
- .flags = INET_PROTOSW_PERMANENT |
- INET_PROTOSW_ICSK,
- },
- {
- .type = SOCK_DGRAM,
- .protocol = IPPROTO_UDP,
- .prot = &udp_prot,
- .ops = &inet_dgram_ops,
- .no_check = UDP_CSUM_DEFAULT,
- .flags = INET_PROTOSW_PERMANENT,
- },
- {
- .type = SOCK_DGRAM,
- .protocol = IPPROTO_ICMP,
- .prot = &ping_prot,
- .ops = &inet_dgram_ops,
- .no_check = UDP_CSUM_DEFAULT,
- .flags = INET_PROTOSW_REUSE,
- },
- {
- .type = SOCK_RAW,
- .protocol = IPPROTO_IP, /* wild card */
- .prot = &raw_prot,
- .ops = &inet_sockraw_ops,
- .no_check = UDP_CSUM_DEFAULT,
- .flags = INET_PROTOSW_REUSE,
- }
- };
- static struct list_head inetsw[SOCK_MAX];
下篇将分析套接字的操作函数。更多请查看 Linux内核--网络内核实现分析
