📄 ppp_io.c
字号:
if( (cyg_io_get_config( tp->t_handle, CYG_IO_GET_CONFIG_SERIAL_BUFFER_INFO, &info, &ilen ) == 0) &&
((info.tx_bufsize-info.tx_count) >= elen) )
{
if( cyg_io_write( tp->t_handle, endseq, &elen) != 0 ) {
done = 0;
}
}
else
{
done = 0;
}
}
#endif
splx(s);
if (done)
sc->sc_stats.ppp_obytes += q - endseq;
} // if (done && m->m_next == NULL)
if (!done) {
/* remember where we got to */
m->m_data = start;
m->m_len = len;
break;
}
/* Finished with this mbuf; free it and move on. */
MFREE(m, m2);
m = m2;
if (m == NULL) {
/* Finished a packet */
break;
}
sc->sc_outfcs = pppfcs(sc->sc_outfcs, mtod(m, u_char *), m->m_len);
} // for(;;)
/*
* If m == NULL, we have finished a packet.
* If m != NULL, we've either done as much work this time
* as we need to, or else we've filled up the output queue.
*/
sc->sc_outm = m;
if (m)
break;
}
#ifndef __ECOS
/* Call pppstart to start output again if necessary. */
s = spltty();
pppstart(tp);
/*
* This timeout is needed for operation on a pseudo-tty,
* because the pty code doesn't call pppstart after it has
* drained the t_outq.
*/
if (!idle && (sc->sc_flags & SC_TIMEOUT) == 0) {
sc->sc_ch = timeout(ppp_timeout, (void *) sc, 1);
sc->sc_flags |= SC_TIMEOUT;
}
splx(s);
#else
s = spltty();
if( sc->sc_outm != NULL &&
cyg_thread_self() != tp->tx_thread )
{
cyg_semaphore_post( &tp->tx_sem );
}
splx(s);
#endif
}
//==========================================================================
/*
* This gets called when a received packet is placed on
* the inq, at splsoftnet. The pppd daemon is to be woken up to do a read().
*/
static void
pppasyncctlp(sc)
struct ppp_softc *sc;
{
struct tty *tp;
int s;
s = spltty();
tp = (struct tty *) sc->sc_devp;
tp->pppd_wakeup = 1;
if( tp->pppd_thread != cyg_thread_self() )
cyg_thread_release( tp->pppd_thread );
splx(s);
}
//==========================================================================
/*
* Allocate enough mbuf to handle current MRU.
*/
static void
pppgetm(sc)
register struct ppp_softc *sc;
{
struct mbuf *m, **mp;
int len;
mp = &sc->sc_m;
for (len = sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN; len > 0; ){
if ((m = *mp) == NULL) {
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL)
break;
*mp = m;
MCLGET(m, M_DONTWAIT);
}
len -= M_DATASIZE(m);
mp = &m->m_next;
}
}
//==========================================================================
/*
* tty interface receiver interrupt.
*/
static unsigned paritytab[8] = {
0x96696996, 0x69969669, 0x69969669, 0x96696996,
0x69969669, 0x96696996, 0x96696996, 0x69969669
};
/*
* Called when character is available from device driver.
* Only guaranteed to be at splsofttty() or spltty()
* This is safe to be called while the upper half's netisr is preempted.
*/
int
cyg_ppp_pppinput(c, tp)
int c;
register struct tty *tp;
{
register struct ppp_softc *sc;
struct mbuf *m;
int ilen, s;
sc = (struct ppp_softc *) tp->t_sc;
if (sc == NULL || tp != (struct tty *) sc->sc_devp)
return 0;
// ++tk_nin;
++sc->sc_stats.ppp_ibytes;
s = spltty();
if (c & 0x80)
sc->sc_flags |= SC_RCV_B7_1;
else
sc->sc_flags |= SC_RCV_B7_0;
if (paritytab[c >> 5] & (1 << (c & 0x1F)))
sc->sc_flags |= SC_RCV_ODDP;
else
sc->sc_flags |= SC_RCV_EVNP;
splx(s);
if (sc->sc_flags & SC_LOG_RAWIN)
ppplogchar(sc, c);
if (c == PPP_FLAG) {
ilen = sc->sc_ilen;
sc->sc_ilen = 0;
if (sc->sc_rawin_count > 0)
ppplogchar(sc, -1);
/*
* If SC_ESCAPED is set, then we've seen the packet
* abort sequence "}~".
*/
if (sc->sc_flags & (SC_FLUSH | SC_ESCAPED)
|| (ilen > 0 && sc->sc_fcs != PPP_GOODFCS)) {
s = spltty();
sc->sc_flags |= SC_PKTLOST; /* note the dropped packet */
if ((sc->sc_flags & (SC_FLUSH | SC_ESCAPED)) == 0){
#if 1
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: bad fcs %x, pkt len %d\n",
sc->sc_if.if_unit, sc->sc_fcs, ilen);
#endif
sc->sc_if.if_ierrors++;
sc->sc_stats.ppp_ierrors++;
} else
sc->sc_flags &= ~(SC_FLUSH | SC_ESCAPED);
splx(s);
return 0;
}
if (ilen < PPP_HDRLEN + PPP_FCSLEN) {
if (ilen) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: too short (%d)\n", sc->sc_if.if_unit, ilen);
s = spltty();
sc->sc_if.if_ierrors++;
sc->sc_stats.ppp_ierrors++;
sc->sc_flags |= SC_PKTLOST;
splx(s);
}
return 0;
}
/*
* Remove FCS trailer. Somewhat painful...
*/
ilen -= 2;
if (--sc->sc_mc->m_len == 0) {
for (m = sc->sc_m; m->m_next != sc->sc_mc; m = m->m_next)
;
sc->sc_mc = m;
}
sc->sc_mc->m_len--;
/* excise this mbuf chain */
m = sc->sc_m;
sc->sc_m = sc->sc_mc->m_next;
sc->sc_mc->m_next = NULL;
ppppktin(sc, m, sc->sc_flags & SC_PKTLOST);
if (sc->sc_flags & SC_PKTLOST) {
s = spltty();
sc->sc_flags &= ~SC_PKTLOST;
splx(s);
}
pppgetm(sc);
return 0;
}
if (sc->sc_flags & SC_FLUSH) {
if (sc->sc_flags & SC_LOG_FLUSH)
ppplogchar(sc, c);
return 0;
}
if (c < 0x20 && (sc->sc_rasyncmap & (1 << c)))
return 0;
s = spltty();
if (sc->sc_flags & SC_ESCAPED) {
sc->sc_flags &= ~SC_ESCAPED;
c ^= PPP_TRANS;
} else if (c == PPP_ESCAPE) {
sc->sc_flags |= SC_ESCAPED;
splx(s);
return 0;
}
splx(s);
/*
* Initialize buffer on first octet received.
* First octet could be address or protocol (when compressing
* address/control).
* Second octet is control.
* Third octet is first or second (when compressing protocol)
* octet of protocol.
* Fourth octet is second octet of protocol.
*/
if (sc->sc_ilen == 0) {
/* reset the first input mbuf */
if (sc->sc_m == NULL) {
pppgetm(sc);
if (sc->sc_m == NULL) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: no input mbufs!\n", sc->sc_if.if_unit);
goto flush;
}
}
m = sc->sc_m;
m->m_len = 0;
m->m_data = M_DATASTART(sc->sc_m);
sc->sc_mc = m;
sc->sc_mp = mtod(m, char *);
sc->sc_fcs = PPP_INITFCS;
if (c != PPP_ALLSTATIONS) {
if (sc->sc_flags & SC_REJ_COMP_AC) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: garbage received: 0x%x (need 0xFF)\n",
sc->sc_if.if_unit, c);
goto flush;
}
*sc->sc_mp++ = PPP_ALLSTATIONS;
*sc->sc_mp++ = PPP_UI;
sc->sc_ilen += 2;
m->m_len += 2;
}
}
if (sc->sc_ilen == 1 && c != PPP_UI) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: missing UI (0x3), got 0x%x\n",
sc->sc_if.if_unit, c);
goto flush;
}
if (sc->sc_ilen == 2 && (c & 1) == 1) {
/* a compressed protocol */
*sc->sc_mp++ = 0;
sc->sc_ilen++;
sc->sc_mc->m_len++;
}
if (sc->sc_ilen == 3 && (c & 1) == 0) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: bad protocol %x\n", sc->sc_if.if_unit,
(sc->sc_mp[-1] << 8) + c);
goto flush;
}
/* packet beyond configured mru? */
if (++sc->sc_ilen > sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: packet too big\n", sc->sc_if.if_unit);
goto flush;
}
/* is this mbuf full? */
m = sc->sc_mc;
if (M_TRAILINGSPACE(m) <= 0) {
if (m->m_next == NULL) {
pppgetm(sc);
if (m->m_next == NULL) {
if (sc->sc_flags & SC_DEBUG)
diag_printf("ppp%d: too few input mbufs!\n", sc->sc_if.if_unit);
goto flush;
}
}
sc->sc_mc = m = m->m_next;
m->m_len = 0;
m->m_data = M_DATASTART(m);
sc->sc_mp = mtod(m, char *);
}
++m->m_len;
*sc->sc_mp++ = c;
sc->sc_fcs = PPP_FCS(sc->sc_fcs, c);
return 0;
flush:
if (!(sc->sc_flags & SC_FLUSH)) {
s = spltty();
sc->sc_if.if_ierrors++;
sc->sc_stats.ppp_ierrors++;
sc->sc_flags |= SC_FLUSH;
splx(s);
if (sc->sc_flags & SC_LOG_FLUSH)
ppplogchar(sc, c);
}
return 0;
}
//==========================================================================
#define MAX_DUMP_BYTES 128
static void
ppplogchar(sc, c)
struct ppp_softc *sc;
int c;
{
if (c >= 0)
sc->sc_rawin[sc->sc_rawin_count++] = c;
if (sc->sc_rawin_count >= sizeof(sc->sc_rawin)
|| (c < 0 && sc->sc_rawin_count > 0)) {
diag_printf("ppp%d input:\n");
diag_vdump_buf_with_offset( diag_printf, sc->sc_rawin, sc->sc_rawin_count, sc->sc_rawin );
sc->sc_rawin_count = 0;
}
}
//==========================================================================
// End of ppp_io.c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -