📄 dn_nsp_out.c
字号:
/* * DECnet An implementation of the DECnet protocol suite for the LINUX * operating system. DECnet is implemented using the BSD Socket * interface as the means of communication with the user level. * * DECnet Network Services Protocol (Output) * * Author: Eduardo Marcelo Serrat <emserrat@geocities.com> * * Changes: * * Steve Whitehouse: Split into dn_nsp_in.c and dn_nsp_out.c from * original dn_nsp.c. * Steve Whitehouse: Updated to work with my new routing architecture. * Steve Whitehouse: Added changes from Eduardo Serrat's patches. * Steve Whitehouse: Now conninits have the "return" bit set. * Steve Whitehouse: Fixes to check alloc'd skbs are non NULL! * Moved output state machine into one function * Steve Whitehouse: New output state machine * Paul Koning: Connect Confirm message fix. * Eduardo Serrat: Fix to stop dn_nsp_do_disc() sending malformed packets. * Steve Whitehouse: dn_nsp_output() and friends needed a spring clean *//****************************************************************************** (c) 1995-1998 E.M. Serrat emserrat@geocities.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.*******************************************************************************/#include <linux/errno.h>#include <linux/types.h>#include <linux/socket.h>#include <linux/in.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/string.h>#include <linux/sockios.h>#include <linux/net.h>#include <linux/netdevice.h>#include <linux/inet.h>#include <linux/route.h>#include <net/sock.h>#include <asm/segment.h>#include <asm/system.h>#include <linux/fcntl.h>#include <linux/mm.h>#include <linux/termios.h> #include <linux/interrupt.h>#include <linux/proc_fs.h>#include <linux/stat.h>#include <linux/init.h>#include <linux/poll.h>#include <linux/if_packet.h>#include <net/neighbour.h>#include <net/dst.h>#include <net/dn_nsp.h>#include <net/dn_dev.h>#include <net/dn_route.h>static int nsp_backoff[NSP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };/* * If sk == NULL, then we assume that we are supposed to be making * a routing layer skb. If sk != NULL, then we are supposed to be * creating an skb for the NSP layer. * * The eventual aim is for each socket to have a cached header size * for its outgoing packets, and to set hdr from this when sk != NULL. */struct sk_buff *dn_alloc_skb(struct sock *sk, int size, int pri){ struct sk_buff *skb; int hdr = 64; if ((skb = alloc_skb(size + hdr, pri)) == NULL) return NULL; skb->protocol = __constant_htons(ETH_P_DNA_RT); skb->pkt_type = PACKET_OUTGOING; if (sk) skb_set_owner_w(skb, sk); skb_reserve(skb, hdr); return skb;}/* * Wrapper for the above, for allocs of data skbs. We try and get the * whole size thats been asked for (plus 11 bytes of header). If this * fails, then we try for any size over 16 bytes for SOCK_STREAMS. */struct sk_buff *dn_alloc_send_skb(struct sock *sk, int *size, int noblock, int *err){ int space; int len; struct sk_buff *skb = NULL; *err = 0; while(skb == NULL) { if (signal_pending(current)) { *err = ERESTARTSYS; break; } if (sk->shutdown & SEND_SHUTDOWN) { *err = EINVAL; break; } if (sk->err) break; len = *size + 11; space = sk->sndbuf - atomic_read(&sk->wmem_alloc); if (space < len) { if ((sk->socket->type == SOCK_STREAM) && (space >= (16 + 11))) len = space; } if (space < len) { set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags); if (noblock) { *err = EWOULDBLOCK; break; } clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags); SOCK_SLEEP_PRE(sk) if ((sk->sndbuf - atomic_read(&sk->wmem_alloc)) < len) schedule(); SOCK_SLEEP_POST(sk) continue; } if ((skb = dn_alloc_skb(sk, len, sk->allocation)) == NULL) continue; *size = len - 11; } return skb;}/* * Calculate persist timer based upon the smoothed round * trip time and the variance. Backoff according to the * nsp_backoff[] array. */unsigned long dn_nsp_persist(struct sock *sk){ struct dn_scp *scp = DN_SK(sk); unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1; t *= nsp_backoff[scp->nsp_rxtshift]; if (t < HZ) t = HZ; if (t > (600*HZ)) t = (600*HZ); if (scp->nsp_rxtshift < NSP_MAXRXTSHIFT) scp->nsp_rxtshift++; /* printk(KERN_DEBUG "rxtshift %lu, t=%lu\n", scp->nsp_rxtshift, t); */ return t;}/* * This is called each time we get an estimate for the rtt * on the link. */static void dn_nsp_rtt(struct sock *sk, long rtt){ struct dn_scp *scp = DN_SK(sk); long srtt = (long)scp->nsp_srtt; long rttvar = (long)scp->nsp_rttvar; long delta; /* * If the jiffies clock flips over in the middle of timestamp * gathering this value might turn out negative, so we make sure * that is it always positive here. */ if (rtt < 0) rtt = -rtt; /* * Add new rtt to smoothed average */ delta = ((rtt << 3) - srtt); srtt += (delta >> 3); if (srtt >= 1) scp->nsp_srtt = (unsigned long)srtt; else scp->nsp_srtt = 1; /* * Add new rtt varience to smoothed varience */ delta >>= 1; rttvar += ((((delta>0)?(delta):(-delta)) - rttvar) >> 2); if (rttvar >= 1) scp->nsp_rttvar = (unsigned long)rttvar; else scp->nsp_rttvar = 1; /* printk(KERN_DEBUG "srtt=%lu rttvar=%lu\n", scp->nsp_srtt, scp->nsp_rttvar); */}/** * dn_nsp_clone_and_send - Send a data packet by cloning it * @skb: The packet to clone and transmit * @gfp: memory allocation flag * * Clone a queued data or other data packet and transmit it. * * Returns: The number of times the packet has been sent previously */static inline unsigned dn_nsp_clone_and_send(struct sk_buff *skb, int gfp){ struct dn_skb_cb *cb = DN_SKB_CB(skb); struct sk_buff *skb2; int ret = 0; if ((skb2 = skb_clone(skb, gfp)) != NULL) { ret = cb->xmit_count; cb->xmit_count++; cb->stamp = jiffies; skb2->sk = skb->sk; dn_nsp_send(skb2); } return ret;}/** * dn_nsp_output - Try and send something from socket queues * @sk: The socket whose queues are to be investigated * @gfp: The memory allocation flags * * Try and send the packet on the end of the data and other data queues. * Other data gets priority over data, and if we retransmit a packet we * reduce the window by dividing it in two. * */void dn_nsp_output(struct sock *sk){ struct dn_scp *scp = DN_SK(sk); struct sk_buff *skb; unsigned reduce_win = 0; /* * First we check for otherdata/linkservice messages */ if ((skb = skb_peek(&scp->other_xmit_queue)) != NULL) reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC); /* * If we may not send any data, we don't. * If we are still trying to get some other data down the * channel, we don't try and send any data. */ if (reduce_win || (scp->flowrem_sw != DN_SEND)) goto recalc_window; if ((skb = skb_peek(&scp->data_xmit_queue)) != NULL) reduce_win = dn_nsp_clone_and_send(skb, GFP_ATOMIC); /* * If we've sent any frame more than once, we cut the * send window size in half. There is always a minimum * window size of one available. */recalc_window: if (reduce_win) { scp->snd_window >>= 1; if (scp->snd_window < NSP_MIN_WINDOW) scp->snd_window = NSP_MIN_WINDOW; }}int dn_nsp_xmit_timeout(struct sock *sk){ struct dn_scp *scp = DN_SK(sk); dn_nsp_output(sk); if (skb_queue_len(&scp->data_xmit_queue) || skb_queue_len(&scp->other_xmit_queue)) scp->persist = dn_nsp_persist(sk); return 0;}static inline unsigned char *dn_mk_common_header(struct dn_scp *scp, struct sk_buff *skb, unsigned char msgflag, int len){ unsigned char *ptr = skb_push(skb, len); if (len < 5) BUG(); *ptr++ = msgflag; *((unsigned short *)ptr) = scp->addrrem; ptr += 2; *((unsigned short *)ptr) = scp->addrloc; ptr += 2; return ptr;}static unsigned short *dn_mk_ack_header(struct sock *sk, struct sk_buff *skb, unsigned char msgflag, int hlen, int other){ struct dn_scp *scp = DN_SK(sk); unsigned short acknum = scp->numdat_rcv & 0x0FFF; unsigned short ackcrs = scp->numoth_rcv & 0x0FFF; unsigned short *ptr; if (hlen < 9) BUG(); scp->ackxmt_dat = acknum; scp->ackxmt_oth = ackcrs; acknum |= 0x8000; ackcrs |= 0x8000; /* If this is an "other data/ack" message, swap acknum and ackcrs */ if (other) { unsigned short tmp = acknum; acknum = ackcrs; ackcrs = tmp; } /* Set "cross subchannel" bit in ackcrs */ ackcrs |= 0x2000; ptr = (unsigned short *)dn_mk_common_header(scp, skb, msgflag, hlen); *ptr++ = dn_htons(acknum); *ptr++ = dn_htons(ackcrs); return ptr;}void dn_nsp_queue_xmit(struct sock *sk, struct sk_buff *skb, int gfp, int oth){ struct dn_scp *scp = DN_SK(sk); struct dn_skb_cb *cb = DN_SKB_CB(skb); unsigned long t = ((scp->nsp_srtt >> 2) + scp->nsp_rttvar) >> 1; /* * Slow start: If we have been idle for more than
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -