⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svcsock.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * linux/net/sunrpc/svcsock.c * * These are the RPC server socket internals. * * The server scheduling algorithm does not always distribute the load * evenly when servicing a single client. May need to modify the * svc_sock_enqueue procedure... * * TCP support is largely untested and may be a little slow. The problem * is that we currently do two separate recvfrom's, one for the 4-byte * record length, and the second for the actual record. This could possibly * be improved by always reading a minimum size of around 100 bytes and * tucking any superfluous bytes away in a temporary store. Still, that * leaves write requests out in the rain. An alternative may be to peek at * the first skb in the queue, and if it matches the next TCP sequence * number, to extract the record marker. Yuck. * * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */#include <linux/sched.h>#include <linux/errno.h>#include <linux/fcntl.h>#include <linux/net.h>#include <linux/in.h>#include <linux/inet.h>#include <linux/udp.h>#include <linux/version.h>#include <linux/unistd.h>#include <linux/malloc.h>#include <linux/netdevice.h>#include <linux/skbuff.h>#include <net/sock.h>#include <net/checksum.h>#include <net/ip.h>#include <asm/uaccess.h>#include <linux/sunrpc/types.h>#include <linux/sunrpc/xdr.h>#include <linux/sunrpc/svcsock.h>#include <linux/sunrpc/stats.h>/* SMP locking strategy: * * 	svc_sock->sk_lock and svc_serv->sv_lock protect their *	respective structures. * *	Antideadlock ordering is sk_lock --> sv_lock. */#define RPCDBG_FACILITY	RPCDBG_SVCSOCKstatic struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,					 int *errp, int pmap_reg);static void		svc_udp_data_ready(struct sock *, int);static int		svc_udp_recvfrom(struct svc_rqst *);static int		svc_udp_sendto(struct svc_rqst *);/* * Queue up an idle server thread.  Must have serv->sv_lock held. */static inline voidsvc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp){	rpc_append_list(&serv->sv_threads, rqstp);}/* * Dequeue an nfsd thread.  Must have serv->sv_lock held. */static inline voidsvc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp){	rpc_remove_list(&serv->sv_threads, rqstp);}/* * Release an skbuff after use */static inline voidsvc_release_skb(struct svc_rqst *rqstp){	struct sk_buff *skb = rqstp->rq_skbuff;	if (!skb)		return;	rqstp->rq_skbuff = NULL;	dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);	skb_free_datagram(rqstp->rq_sock->sk_sk, skb);}/* * Queue up a socket with data pending. If there are idle nfsd * processes, wake 'em up. * * This must be called with svsk->sk_lock held. */static voidsvc_sock_enqueue(struct svc_sock *svsk){	struct svc_serv	*serv = svsk->sk_server;	struct svc_rqst	*rqstp;	/* NOTE: Local BH is already disabled by our caller. */	spin_lock(&serv->sv_lock);	if (serv->sv_threads && serv->sv_sockets)		printk(KERN_ERR			"svc_sock_enqueue: threads and sockets both waiting??\n");	if (svsk->sk_busy) {		/* Don't enqueue socket while daemon is receiving */		dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);		goto out_unlock;	}	/* Mark socket as busy. It will remain in this state until the	 * server has processed all pending data and put the socket back	 * on the idle list.	 */	svsk->sk_busy = 1;	if ((rqstp = serv->sv_threads) != NULL) {		dprintk("svc: socket %p served by daemon %p\n",			svsk->sk_sk, rqstp);		svc_serv_dequeue(serv, rqstp);		if (rqstp->rq_sock)			printk(KERN_ERR 				"svc_sock_enqueue: server %p, rq_sock=%p!\n",				rqstp, rqstp->rq_sock);		rqstp->rq_sock = svsk;		svsk->sk_inuse++;		wake_up(&rqstp->rq_wait);	} else {		dprintk("svc: socket %p put into queue\n", svsk->sk_sk);		rpc_append_list(&serv->sv_sockets, svsk);		svsk->sk_qued = 1;	}out_unlock:	spin_unlock(&serv->sv_lock);}/* * Dequeue the first socket.  Must be called with the serv->sv_lock held. */static inline struct svc_sock *svc_sock_dequeue(struct svc_serv *serv){	struct svc_sock	*svsk;	if ((svsk = serv->sv_sockets) != NULL)		rpc_remove_list(&serv->sv_sockets, svsk);	if (svsk) {		dprintk("svc: socket %p dequeued, inuse=%d\n",			svsk->sk_sk, svsk->sk_inuse);		svsk->sk_qued = 0;	}	return svsk;}/* * Having read count bytes from a socket, check whether it * needs to be re-enqueued. */static inline voidsvc_sock_received(struct svc_sock *svsk, int count){	spin_lock_bh(&svsk->sk_lock);	if ((svsk->sk_data -= count) < 0) {		printk(KERN_NOTICE "svc: sk_data negative!\n");		svsk->sk_data = 0;	}	svsk->sk_rqstp = NULL; /* XXX */	svsk->sk_busy = 0;	if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {		dprintk("svc: socket %p re-enqueued after receive\n",						svsk->sk_sk);		svc_sock_enqueue(svsk);	}	spin_unlock_bh(&svsk->sk_lock);}/* * Dequeue a new connection. */static inline voidsvc_sock_accepted(struct svc_sock *svsk){	spin_lock_bh(&svsk->sk_lock);        svsk->sk_busy = 0;        svsk->sk_conn--;        if (svsk->sk_conn || svsk->sk_data || svsk->sk_close) {                dprintk("svc: socket %p re-enqueued after accept\n",						svsk->sk_sk);                svc_sock_enqueue(svsk);        }	spin_unlock_bh(&svsk->sk_lock);}/* * Release a socket after use. */static inline voidsvc_sock_release(struct svc_rqst *rqstp){	struct svc_sock	*svsk = rqstp->rq_sock;	if (!svsk)		return;	svc_release_skb(rqstp);	rqstp->rq_sock = NULL;	if (!--(svsk->sk_inuse) && svsk->sk_dead) {		dprintk("svc: releasing dead socket\n");		sock_release(svsk->sk_sock);		kfree(svsk);	}}/* * External function to wake up a server waiting for data */voidsvc_wake_up(struct svc_serv *serv){	struct svc_rqst	*rqstp;	spin_lock_bh(&serv->sv_lock);	if ((rqstp = serv->sv_threads) != NULL) {		dprintk("svc: daemon %p woken up.\n", rqstp);		/*		svc_serv_dequeue(serv, rqstp);		rqstp->rq_sock = NULL;		 */		wake_up(&rqstp->rq_wait);	}	spin_unlock_bh(&serv->sv_lock);}/* * Generic sendto routine */static intsvc_sendto(struct svc_rqst *rqstp, struct iovec *iov, int nr){	mm_segment_t	oldfs;	struct svc_sock	*svsk = rqstp->rq_sock;	struct socket	*sock = svsk->sk_sock;	struct msghdr	msg;	int		i, buflen, len;	for (i = buflen = 0; i < nr; i++)		buflen += iov[i].iov_len;	msg.msg_name    = &rqstp->rq_addr;	msg.msg_namelen = sizeof(rqstp->rq_addr);	msg.msg_iov     = iov;	msg.msg_iovlen  = nr;	msg.msg_control = NULL;	msg.msg_controllen = 0;	msg.msg_flags	= MSG_DONTWAIT;	oldfs = get_fs(); set_fs(KERNEL_DS);	len = sock_sendmsg(sock, &msg, buflen);	set_fs(oldfs);	dprintk("svc: socket %p sendto([%p %Zu... ], %d, %d) = %d\n",			rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, nr, buflen, len);	return len;}/* * Check input queue length */static intsvc_recv_available(struct svc_sock *svsk){	mm_segment_t	oldfs;	struct socket	*sock = svsk->sk_sock;	int		avail, err;	oldfs = get_fs(); set_fs(KERNEL_DS);	err = sock->ops->ioctl(sock, TIOCINQ, (unsigned long) &avail);	set_fs(oldfs);	return (err >= 0)? avail : err;}/* * Generic recvfrom routine. */static intsvc_recvfrom(struct svc_rqst *rqstp, struct iovec *iov, int nr, int buflen){	mm_segment_t	oldfs;	struct msghdr	msg;	struct socket	*sock;	int		len, alen;	rqstp->rq_addrlen = sizeof(rqstp->rq_addr);	sock = rqstp->rq_sock->sk_sock;	msg.msg_name    = &rqstp->rq_addr;	msg.msg_namelen = sizeof(rqstp->rq_addr);	msg.msg_iov     = iov;	msg.msg_iovlen  = nr;	msg.msg_control = NULL;	msg.msg_controllen = 0;	msg.msg_flags	= MSG_DONTWAIT;	oldfs = get_fs(); set_fs(KERNEL_DS);	len = sock_recvmsg(sock, &msg, buflen, MSG_DONTWAIT);	set_fs(oldfs);	/* sock_recvmsg doesn't fill in the name/namelen, so we must..	 * possibly we should cache this in the svc_sock structure	 * at accept time. FIXME	 */	alen = sizeof(rqstp->rq_addr);	sock->ops->getname(sock, (struct sockaddr *)&rqstp->rq_addr, &alen, 1);	dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",		rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);	return len;}/* * INET callback when data has been received on the socket. */static voidsvc_udp_data_ready(struct sock *sk, int count){	struct svc_sock	*svsk = (struct svc_sock *)(sk->user_data);	if (!svsk)		goto out;	dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",		svsk, sk, count, svsk->sk_busy);	spin_lock_bh(&svsk->sk_lock);	svsk->sk_data = 1;	svc_sock_enqueue(svsk);	spin_unlock_bh(&svsk->sk_lock); out:	if (sk->sleep && waitqueue_active(sk->sleep))		wake_up_interruptible(sk->sleep);}/* * Receive a datagram from a UDP socket. */static intsvc_udp_recvfrom(struct svc_rqst *rqstp){	struct svc_sock	*svsk = rqstp->rq_sock;	struct svc_serv	*serv = svsk->sk_server;	struct sk_buff	*skb;	u32		*data;	int		err, len;	svsk->sk_data = 0;	while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {		svc_sock_received(svsk, 0);		if (err == -EAGAIN)			return err;		/* possibly an icmp error */		dprintk("svc: recvfrom returned error %d\n", -err);	}	if (skb->ip_summed != CHECKSUM_UNNECESSARY) {		unsigned int csum = skb->csum;		csum = csum_partial(skb->h.raw, skb->len, csum);		if ((unsigned short)csum_fold(csum)) {			skb_free_datagram(svsk->sk_sk, skb);			svc_sock_received(svsk, 0);			return 0;		}	}	/* There may be more data */	svsk->sk_data = 1;	len  = skb->len - sizeof(struct udphdr);	data = (u32 *) (skb->h.raw + sizeof(struct udphdr));	rqstp->rq_skbuff      = skb;	rqstp->rq_argbuf.base = data;	rqstp->rq_argbuf.buf  = data;	rqstp->rq_argbuf.len  = (len >> 2);	/* rqstp->rq_resbuf      = rqstp->rq_defbuf; */	rqstp->rq_prot        = IPPROTO_UDP;	/* Get sender address */	rqstp->rq_addr.sin_family = AF_INET;	rqstp->rq_addr.sin_port = skb->h.uh->source;	rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;	if (serv->sv_stats)		serv->sv_stats->netudpcnt++;	/* One down, maybe more to go... */	svsk->sk_sk->stamp = skb->stamp;	svc_sock_received(svsk, 0);	return len;}static intsvc_udp_sendto(struct svc_rqst *rqstp){	struct svc_buf	*bufp = &rqstp->rq_resbuf;	int		error;	/* Set up the first element of the reply iovec.	 * Any other iovecs that may be in use have been taken	 * care of by the server implementation itself.	 */	/* bufp->base = bufp->area; */	bufp->iov[0].iov_base = bufp->base;	bufp->iov[0].iov_len  = bufp->len << 2;	error = svc_sendto(rqstp, bufp->iov, bufp->nriov);	if (error == -ECONNREFUSED)		/* ICMP error on earlier request. */		error = svc_sendto(rqstp, bufp->iov, bufp->nriov);	else if (error == -EAGAIN)		/* Ignore and wait for re-xmit */		error = 0;	return error;}static intsvc_udp_init(struct svc_sock *svsk){	svsk->sk_sk->data_ready = svc_udp_data_ready;	svsk->sk_recvfrom = svc_udp_recvfrom;	svsk->sk_sendto = svc_udp_sendto;	return 0;}/* * A state change on a listening socket means there's a connection * pending. */static voidsvc_tcp_state_change1(struct sock *sk){	struct svc_sock	*svsk;	dprintk("svc: socket %p TCP (listen) state change %d\n",			sk, sk->state);	if  (sk->state != TCP_ESTABLISHED) {		/* Aborted connection, SYN_RECV or whatever... */		goto out;	}	if (!(svsk = (struct svc_sock *) sk->user_data)) {		printk("svc: socket %p: no user data\n", sk);		goto out;	}	spin_lock_bh(&svsk->sk_lock);	svsk->sk_conn++;	svc_sock_enqueue(svsk);	spin_unlock_bh(&svsk->sk_lock); out:	if (sk->sleep && waitqueue_active(sk->sleep))		wake_up_interruptible_all(sk->sleep);}/* * A state change on a connected socket means it's dying or dead. */static voidsvc_tcp_state_change2(struct sock *sk){	struct svc_sock	*svsk;	dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",			sk, sk->state, sk->user_data);	if (!(svsk = (struct svc_sock *) sk->user_data)) {		printk("svc: socket %p: no user data\n", sk);		goto out;	}	spin_lock_bh(&svsk->sk_lock);	svsk->sk_close = 1;	svc_sock_enqueue(svsk);	spin_unlock_bh(&svsk->sk_lock); out:	if (sk->sleep && waitqueue_active(sk->sleep))		wake_up_interruptible_all(sk->sleep);}static voidsvc_tcp_data_ready(struct sock *sk, int count){	struct svc_sock *	svsk;	dprintk("svc: socket %p TCP data ready (svsk %p)\n",			sk, sk->user_data);	if (!(svsk = (struct svc_sock *)(sk->user_data)))		goto out;	spin_lock_bh(&svsk->sk_lock);	svsk->sk_data++;	svc_sock_enqueue(svsk);	spin_unlock_bh(&svsk->sk_lock); out:	if (sk->sleep && waitqueue_active(sk->sleep))		wake_up_interruptible(sk->sleep);}/* * Accept a TCP connection */static voidsvc_tcp_accept(struct svc_sock *svsk){	struct sockaddr_in sin;	struct svc_serv	*serv = svsk->sk_server;	struct socket	*sock = svsk->sk_sock;	struct socket	*newsock;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -